]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/net/wireless/iwlwifi/iwl-3945-rs.c
a279bf1dc9b034d6a49f86f98d8d9b6d133cb740
[linux-2.6-omap-h63xx.git] / drivers / net / wireless / iwlwifi / iwl-3945-rs.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2005 - 2008 Intel Corporation. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of version 2 of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
17  *
18  * The full GNU General Public License is included in this distribution in the
19  * file called LICENSE.
20  *
21  * Contact Information:
22  * James P. Ketrenos <ipw2100-admin@linux.intel.com>
23  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24  *
25  *****************************************************************************/
26
27 #include <linux/kernel.h>
28 #include <linux/init.h>
29 #include <linux/skbuff.h>
30 #include <linux/wireless.h>
31 #include <net/mac80211.h>
32
33 #include <linux/netdevice.h>
34 #include <linux/etherdevice.h>
35 #include <linux/delay.h>
36
37 #include <linux/workqueue.h>
38
39 #include "../net/mac80211/rate.h"
40
41 #include "iwl-3945.h"
42
43 #define RS_NAME "iwl-3945-rs"
44
45 struct iwl3945_rate_scale_data {
46         u64 data;
47         s32 success_counter;
48         s32 success_ratio;
49         s32 counter;
50         s32 average_tpt;
51         unsigned long stamp;
52 };
53
54 struct iwl3945_rs_sta {
55         spinlock_t lock;
56         s32 *expected_tpt;
57         unsigned long last_partial_flush;
58         unsigned long last_flush;
59         u32 flush_time;
60         u32 last_tx_packets;
61         u32 tx_packets;
62         u8 tgg;
63         u8 flush_pending;
64         u8 start_rate;
65         u8 ibss_sta_added;
66         struct timer_list rate_scale_flush;
67         struct iwl3945_rate_scale_data win[IWL_RATE_COUNT];
68
69         /* used to be in sta_info */
70         int last_txrate_idx;
71 };
72
73 static s32 iwl3945_expected_tpt_g[IWL_RATE_COUNT] = {
74         7, 13, 35, 58, 0, 0, 76, 104, 130, 168, 191, 202
75 };
76
77 static s32 iwl3945_expected_tpt_g_prot[IWL_RATE_COUNT] = {
78         7, 13, 35, 58, 0, 0, 0, 80, 93, 113, 123, 125
79 };
80
81 static s32 iwl3945_expected_tpt_a[IWL_RATE_COUNT] = {
82         0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186
83 };
84
85 static s32 iwl3945_expected_tpt_b[IWL_RATE_COUNT] = {
86         7, 13, 35, 58, 0, 0, 0, 0, 0, 0, 0, 0
87 };
88
89 struct iwl3945_tpt_entry {
90         s8 min_rssi;
91         u8 index;
92 };
93
94 static struct iwl3945_tpt_entry iwl3945_tpt_table_a[] = {
95         {-60, IWL_RATE_54M_INDEX},
96         {-64, IWL_RATE_48M_INDEX},
97         {-72, IWL_RATE_36M_INDEX},
98         {-80, IWL_RATE_24M_INDEX},
99         {-84, IWL_RATE_18M_INDEX},
100         {-85, IWL_RATE_12M_INDEX},
101         {-87, IWL_RATE_9M_INDEX},
102         {-89, IWL_RATE_6M_INDEX}
103 };
104
105 static struct iwl3945_tpt_entry iwl3945_tpt_table_g[] = {
106         {-60, IWL_RATE_54M_INDEX},
107         {-64, IWL_RATE_48M_INDEX},
108         {-68, IWL_RATE_36M_INDEX},
109         {-80, IWL_RATE_24M_INDEX},
110         {-84, IWL_RATE_18M_INDEX},
111         {-85, IWL_RATE_12M_INDEX},
112         {-86, IWL_RATE_11M_INDEX},
113         {-88, IWL_RATE_5M_INDEX},
114         {-90, IWL_RATE_2M_INDEX},
115         {-92, IWL_RATE_1M_INDEX}
116 };
117
118 #define IWL_RATE_MAX_WINDOW          62
119 #define IWL_RATE_FLUSH        (3*HZ/10)
120 #define IWL_RATE_WIN_FLUSH       (HZ/2)
121 #define IWL_RATE_HIGH_TH          11520
122 #define IWL_RATE_MIN_FAILURE_TH       8
123 #define IWL_RATE_MIN_SUCCESS_TH       8
124 #define IWL_RATE_DECREASE_TH       1920
125
126 static u8 iwl3945_get_rate_index_by_rssi(s32 rssi, enum ieee80211_band band)
127 {
128         u32 index = 0;
129         u32 table_size = 0;
130         struct iwl3945_tpt_entry *tpt_table = NULL;
131
132         if ((rssi < IWL_MIN_RSSI_VAL) || (rssi > IWL_MAX_RSSI_VAL))
133                 rssi = IWL_MIN_RSSI_VAL;
134
135         switch (band) {
136         case IEEE80211_BAND_2GHZ:
137                 tpt_table = iwl3945_tpt_table_g;
138                 table_size = ARRAY_SIZE(iwl3945_tpt_table_g);
139                 break;
140
141         case IEEE80211_BAND_5GHZ:
142                 tpt_table = iwl3945_tpt_table_a;
143                 table_size = ARRAY_SIZE(iwl3945_tpt_table_a);
144                 break;
145
146         default:
147                 BUG();
148                 break;
149         }
150
151         while ((index < table_size) && (rssi < tpt_table[index].min_rssi))
152                 index++;
153
154         index = min(index, (table_size - 1));
155
156         return tpt_table[index].index;
157 }
158
159 static void iwl3945_clear_window(struct iwl3945_rate_scale_data *window)
160 {
161         window->data = 0;
162         window->success_counter = 0;
163         window->success_ratio = -1;
164         window->counter = 0;
165         window->average_tpt = IWL_INV_TPT;
166         window->stamp = 0;
167 }
168
169 /**
170  * iwl3945_rate_scale_flush_windows - flush out the rate scale windows
171  *
172  * Returns the number of windows that have gathered data but were
173  * not flushed.  If there were any that were not flushed, then
174  * reschedule the rate flushing routine.
175  */
176 static int iwl3945_rate_scale_flush_windows(struct iwl3945_rs_sta *rs_sta)
177 {
178         int unflushed = 0;
179         int i;
180         unsigned long flags;
181
182         /*
183          * For each rate, if we have collected data on that rate
184          * and it has been more than IWL_RATE_WIN_FLUSH
185          * since we flushed, clear out the gathered statistics
186          */
187         for (i = 0; i < IWL_RATE_COUNT; i++) {
188                 if (!rs_sta->win[i].counter)
189                         continue;
190
191                 spin_lock_irqsave(&rs_sta->lock, flags);
192                 if (time_after(jiffies, rs_sta->win[i].stamp +
193                                IWL_RATE_WIN_FLUSH)) {
194                         IWL_DEBUG_RATE("flushing %d samples of rate "
195                                        "index %d\n",
196                                        rs_sta->win[i].counter, i);
197                         iwl3945_clear_window(&rs_sta->win[i]);
198                 } else
199                         unflushed++;
200                 spin_unlock_irqrestore(&rs_sta->lock, flags);
201         }
202
203         return unflushed;
204 }
205
206 #define IWL_RATE_FLUSH_MAX              5000    /* msec */
207 #define IWL_RATE_FLUSH_MIN              50      /* msec */
208
209 static void iwl3945_bg_rate_scale_flush(unsigned long data)
210 {
211         struct iwl3945_rs_sta *rs_sta = (void *)data;
212         int unflushed = 0;
213         unsigned long flags;
214         u32 packet_count, duration, pps;
215
216         IWL_DEBUG_RATE("enter\n");
217
218         unflushed = iwl3945_rate_scale_flush_windows(rs_sta);
219
220         spin_lock_irqsave(&rs_sta->lock, flags);
221
222         rs_sta->flush_pending = 0;
223
224         /* Number of packets Rx'd since last time this timer ran */
225         packet_count = (rs_sta->tx_packets - rs_sta->last_tx_packets) + 1;
226
227         rs_sta->last_tx_packets = rs_sta->tx_packets + 1;
228
229         if (unflushed) {
230                 duration =
231                     jiffies_to_msecs(jiffies - rs_sta->last_partial_flush);
232 /*              duration = jiffies_to_msecs(rs_sta->flush_time); */
233
234                 IWL_DEBUG_RATE("Tx'd %d packets in %dms\n",
235                                packet_count, duration);
236
237                 /* Determine packets per second */
238                 if (duration)
239                         pps = (packet_count * 1000) / duration;
240                 else
241                         pps = 0;
242
243                 if (pps) {
244                         duration = IWL_RATE_FLUSH_MAX / pps;
245                         if (duration < IWL_RATE_FLUSH_MIN)
246                                 duration = IWL_RATE_FLUSH_MIN;
247                 } else
248                         duration = IWL_RATE_FLUSH_MAX;
249
250                 rs_sta->flush_time = msecs_to_jiffies(duration);
251
252                 IWL_DEBUG_RATE("new flush period: %d msec ave %d\n",
253                                duration, packet_count);
254
255                 mod_timer(&rs_sta->rate_scale_flush, jiffies +
256                           rs_sta->flush_time);
257
258                 rs_sta->last_partial_flush = jiffies;
259         }
260
261         /* If there weren't any unflushed entries, we don't schedule the timer
262          * to run again */
263
264         rs_sta->last_flush = jiffies;
265
266         spin_unlock_irqrestore(&rs_sta->lock, flags);
267
268         IWL_DEBUG_RATE("leave\n");
269 }
270
271 /**
272  * iwl3945_collect_tx_data - Update the success/failure sliding window
273  *
274  * We keep a sliding window of the last 64 packets transmitted
275  * at this rate.  window->data contains the bitmask of successful
276  * packets.
277  */
278 static void iwl3945_collect_tx_data(struct iwl3945_rs_sta *rs_sta,
279                                 struct iwl3945_rate_scale_data *window,
280                                 int success, int retries)
281 {
282         unsigned long flags;
283
284         if (!retries) {
285                 IWL_DEBUG_RATE("leave: retries == 0 -- should be at least 1\n");
286                 return;
287         }
288
289         while (retries--) {
290                 spin_lock_irqsave(&rs_sta->lock, flags);
291
292                 /* If we have filled up the window then subtract one from the
293                  * success counter if the high-bit is counting toward
294                  * success */
295                 if (window->counter == IWL_RATE_MAX_WINDOW) {
296                         if (window->data & (1ULL << (IWL_RATE_MAX_WINDOW - 1)))
297                                 window->success_counter--;
298                 } else
299                         window->counter++;
300
301                 /* Slide the window to the left one bit */
302                 window->data = (window->data << 1);
303
304                 /* If this packet was a success then set the low bit high */
305                 if (success) {
306                         window->success_counter++;
307                         window->data |= 1;
308                 }
309
310                 /* window->counter can't be 0 -- it is either >0 or
311                  * IWL_RATE_MAX_WINDOW */
312                 window->success_ratio = 12800 * window->success_counter /
313                     window->counter;
314
315                 /* Tag this window as having been updated */
316                 window->stamp = jiffies;
317
318                 spin_unlock_irqrestore(&rs_sta->lock, flags);
319         }
320 }
321
322 static void rs_rate_init(void *priv_rate, void *priv_sta,
323                          struct ieee80211_local *local, struct sta_info *sta)
324 {
325         struct iwl3945_rs_sta *rs_sta = (void *)sta->rate_ctrl_priv;
326         int i;
327
328         IWL_DEBUG_RATE("enter\n");
329
330         /* TODO: what is a good starting rate for STA? About middle? Maybe not
331          * the lowest or the highest rate.. Could consider using RSSI from
332          * previous packets? Need to have IEEE 802.1X auth succeed immediately
333          * after assoc.. */
334
335         for (i = IWL_RATE_COUNT - 1; i >= 0; i--) {
336                 if (sta->sta.supp_rates[local->hw.conf.channel->band] & (1 << i)) {
337                         rs_sta->last_txrate_idx = i;
338                         break;
339                 }
340         }
341
342         /* For 5 GHz band it start at IWL_FIRST_OFDM_RATE */
343         if (local->hw.conf.channel->band == IEEE80211_BAND_5GHZ)
344                 rs_sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
345
346         IWL_DEBUG_RATE("leave\n");
347 }
348
349 static void *rs_alloc(struct ieee80211_local *local)
350 {
351         return local->hw.priv;
352 }
353
354 /* rate scale requires free function to be implemented */
355 static void rs_free(void *priv)
356 {
357         return;
358 }
359 static void rs_clear(void *priv)
360 {
361         return;
362 }
363
364
365 static void *rs_alloc_sta(void *priv, gfp_t gfp)
366 {
367         struct iwl3945_rs_sta *rs_sta;
368         int i;
369
370         IWL_DEBUG_RATE("enter\n");
371
372         rs_sta = kzalloc(sizeof(struct iwl3945_rs_sta), gfp);
373         if (!rs_sta) {
374                 IWL_DEBUG_RATE("leave: ENOMEM\n");
375                 return NULL;
376         }
377
378         spin_lock_init(&rs_sta->lock);
379
380         rs_sta->start_rate = IWL_RATE_INVALID;
381
382         /* default to just 802.11b */
383         rs_sta->expected_tpt = iwl3945_expected_tpt_b;
384
385         rs_sta->last_partial_flush = jiffies;
386         rs_sta->last_flush = jiffies;
387         rs_sta->flush_time = IWL_RATE_FLUSH;
388         rs_sta->last_tx_packets = 0;
389         rs_sta->ibss_sta_added = 0;
390
391         init_timer(&rs_sta->rate_scale_flush);
392         rs_sta->rate_scale_flush.data = (unsigned long)rs_sta;
393         rs_sta->rate_scale_flush.function = &iwl3945_bg_rate_scale_flush;
394
395         for (i = 0; i < IWL_RATE_COUNT; i++)
396                 iwl3945_clear_window(&rs_sta->win[i]);
397
398         IWL_DEBUG_RATE("leave\n");
399
400         return rs_sta;
401 }
402
403 static void rs_free_sta(void *priv, void *priv_sta)
404 {
405         struct iwl3945_rs_sta *rs_sta = priv_sta;
406
407         IWL_DEBUG_RATE("enter\n");
408         del_timer_sync(&rs_sta->rate_scale_flush);
409         kfree(rs_sta);
410         IWL_DEBUG_RATE("leave\n");
411 }
412
413
414 /*
415  * get ieee prev rate from rate scale table.
416  * for A and B mode we need to overright prev
417  * value
418  */
419 static int rs_adjust_next_rate(struct iwl3945_priv *priv, int rate)
420 {
421         int next_rate = iwl3945_get_prev_ieee_rate(rate);
422
423         switch (priv->band) {
424         case IEEE80211_BAND_5GHZ:
425                 if (rate == IWL_RATE_12M_INDEX)
426                         next_rate = IWL_RATE_9M_INDEX;
427                 else if (rate == IWL_RATE_6M_INDEX)
428                         next_rate = IWL_RATE_6M_INDEX;
429                 break;
430 /* XXX cannot be invoked in current mac80211 so not a regression
431         case MODE_IEEE80211B:
432                 if (rate == IWL_RATE_11M_INDEX_TABLE)
433                         next_rate = IWL_RATE_5M_INDEX_TABLE;
434                 break;
435  */
436         default:
437                 break;
438         }
439
440         return next_rate;
441 }
442 /**
443  * rs_tx_status - Update rate control values based on Tx results
444  *
445  * NOTE: Uses iwl3945_priv->retry_rate for the # of retries attempted by
446  * the hardware for each rate.
447  */
448 static void rs_tx_status(void *priv_rate,
449                          struct net_device *dev,
450                          struct sk_buff *skb)
451 {
452         u8 retries, current_count;
453         int scale_rate_index, first_index, last_index;
454         unsigned long flags;
455         struct sta_info *sta;
456         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
457         struct iwl3945_priv *priv = (struct iwl3945_priv *)priv_rate;
458         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
459         struct iwl3945_rs_sta *rs_sta;
460         struct ieee80211_supported_band *sband;
461         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
462
463         IWL_DEBUG_RATE("enter\n");
464
465         sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
466
467
468         retries = info->status.retry_count;
469         first_index = sband->bitrates[info->tx_rate_idx].hw_value;
470         if ((first_index < 0) || (first_index >= IWL_RATE_COUNT)) {
471                 IWL_DEBUG_RATE("leave: Rate out of bounds: %d\n", first_index);
472                 return;
473         }
474
475         rcu_read_lock();
476
477         sta = sta_info_get(local, hdr->addr1);
478         if (!sta || !sta->rate_ctrl_priv) {
479                 rcu_read_unlock();
480                 IWL_DEBUG_RATE("leave: No STA priv data to update!\n");
481                 return;
482         }
483
484         rs_sta = (void *)sta->rate_ctrl_priv;
485
486         rs_sta->tx_packets++;
487
488         scale_rate_index = first_index;
489         last_index = first_index;
490
491         /*
492          * Update the window for each rate.  We determine which rates
493          * were Tx'd based on the total number of retries vs. the number
494          * of retries configured for each rate -- currently set to the
495          * priv value 'retry_rate' vs. rate specific
496          *
497          * On exit from this while loop last_index indicates the rate
498          * at which the frame was finally transmitted (or failed if no
499          * ACK)
500          */
501         while (retries > 0) {
502                 if (retries < priv->retry_rate) {
503                         current_count = retries;
504                         last_index = scale_rate_index;
505                 } else {
506                         current_count = priv->retry_rate;
507                         last_index = rs_adjust_next_rate(priv,
508                                                          scale_rate_index);
509                 }
510
511                 /* Update this rate accounting for as many retries
512                  * as was used for it (per current_count) */
513                 iwl3945_collect_tx_data(rs_sta,
514                                     &rs_sta->win[scale_rate_index],
515                                     0, current_count);
516                 IWL_DEBUG_RATE("Update rate %d for %d retries.\n",
517                                scale_rate_index, current_count);
518
519                 retries -= current_count;
520
521                 if (retries)
522                         scale_rate_index =
523                             rs_adjust_next_rate(priv, scale_rate_index);
524         }
525
526
527         /* Update the last index window with success/failure based on ACK */
528         IWL_DEBUG_RATE("Update rate %d with %s.\n",
529                        last_index,
530                        (info->flags & IEEE80211_TX_STAT_ACK) ?
531                        "success" : "failure");
532         iwl3945_collect_tx_data(rs_sta,
533                             &rs_sta->win[last_index],
534                             info->flags & IEEE80211_TX_STAT_ACK, 1);
535
536         /* We updated the rate scale window -- if its been more than
537          * flush_time since the last run, schedule the flush
538          * again */
539         spin_lock_irqsave(&rs_sta->lock, flags);
540
541         if (!rs_sta->flush_pending &&
542             time_after(jiffies, rs_sta->last_partial_flush +
543                        rs_sta->flush_time)) {
544
545                 rs_sta->flush_pending = 1;
546                 mod_timer(&rs_sta->rate_scale_flush,
547                           jiffies + rs_sta->flush_time);
548         }
549
550         spin_unlock_irqrestore(&rs_sta->lock, flags);
551
552         rcu_read_unlock();
553
554         IWL_DEBUG_RATE("leave\n");
555
556         return;
557 }
558
559 static u16 iwl3945_get_adjacent_rate(struct iwl3945_rs_sta *rs_sta,
560                                  u8 index, u16 rate_mask, enum ieee80211_band band)
561 {
562         u8 high = IWL_RATE_INVALID;
563         u8 low = IWL_RATE_INVALID;
564
565         /* 802.11A walks to the next literal adjacent rate in
566          * the rate table */
567         if (unlikely(band == IEEE80211_BAND_5GHZ)) {
568                 int i;
569                 u32 mask;
570
571                 /* Find the previous rate that is in the rate mask */
572                 i = index - 1;
573                 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
574                         if (rate_mask & mask) {
575                                 low = i;
576                                 break;
577                         }
578                 }
579
580                 /* Find the next rate that is in the rate mask */
581                 i = index + 1;
582                 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
583                         if (rate_mask & mask) {
584                                 high = i;
585                                 break;
586                         }
587                 }
588
589                 return (high << 8) | low;
590         }
591
592         low = index;
593         while (low != IWL_RATE_INVALID) {
594                 if (rs_sta->tgg)
595                         low = iwl3945_rates[low].prev_rs_tgg;
596                 else
597                         low = iwl3945_rates[low].prev_rs;
598                 if (low == IWL_RATE_INVALID)
599                         break;
600                 if (rate_mask & (1 << low))
601                         break;
602                 IWL_DEBUG_RATE("Skipping masked lower rate: %d\n", low);
603         }
604
605         high = index;
606         while (high != IWL_RATE_INVALID) {
607                 if (rs_sta->tgg)
608                         high = iwl3945_rates[high].next_rs_tgg;
609                 else
610                         high = iwl3945_rates[high].next_rs;
611                 if (high == IWL_RATE_INVALID)
612                         break;
613                 if (rate_mask & (1 << high))
614                         break;
615                 IWL_DEBUG_RATE("Skipping masked higher rate: %d\n", high);
616         }
617
618         return (high << 8) | low;
619 }
620
621 /**
622  * rs_get_rate - find the rate for the requested packet
623  *
624  * Returns the ieee80211_rate structure allocated by the driver.
625  *
626  * The rate control algorithm has no internal mapping between hw_mode's
627  * rate ordering and the rate ordering used by the rate control algorithm.
628  *
629  * The rate control algorithm uses a single table of rates that goes across
630  * the entire A/B/G spectrum vs. being limited to just one particular
631  * hw_mode.
632  *
633  * As such, we can't convert the index obtained below into the hw_mode's
634  * rate table and must reference the driver allocated rate table
635  *
636  */
637 static void rs_get_rate(void *priv_rate, struct net_device *dev,
638                         struct ieee80211_supported_band *sband,
639                         struct sk_buff *skb,
640                         struct rate_selection *sel)
641 {
642         u8 low = IWL_RATE_INVALID;
643         u8 high = IWL_RATE_INVALID;
644         u16 high_low;
645         int index;
646         struct iwl3945_rs_sta *rs_sta;
647         struct iwl3945_rate_scale_data *window = NULL;
648         int current_tpt = IWL_INV_TPT;
649         int low_tpt = IWL_INV_TPT;
650         int high_tpt = IWL_INV_TPT;
651         u32 fail_count;
652         s8 scale_action = 0;
653         unsigned long flags;
654         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
655         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
656         struct sta_info *sta;
657         u16 fc, rate_mask;
658         struct iwl3945_priv *priv = (struct iwl3945_priv *)priv_rate;
659         DECLARE_MAC_BUF(mac);
660
661         IWL_DEBUG_RATE("enter\n");
662
663         rcu_read_lock();
664
665         sta = sta_info_get(local, hdr->addr1);
666
667         /* Send management frames and broadcast/multicast data using lowest
668          * rate. */
669         fc = le16_to_cpu(hdr->frame_control);
670         if ((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA ||
671             is_multicast_ether_addr(hdr->addr1) ||
672             !sta || !sta->rate_ctrl_priv) {
673                 IWL_DEBUG_RATE("leave: No STA priv data to update!\n");
674                 sel->rate_idx = rate_lowest_index(local, sband, sta);
675                 rcu_read_unlock();
676                 return;
677         }
678
679         rs_sta = (void *)sta->rate_ctrl_priv;
680
681         rate_mask = sta->sta.supp_rates[sband->band];
682         index = min(rs_sta->last_txrate_idx & 0xffff, IWL_RATE_COUNT - 1);
683
684         if (sband->band == IEEE80211_BAND_5GHZ)
685                 rate_mask = rate_mask << IWL_FIRST_OFDM_RATE;
686
687         if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) &&
688             !rs_sta->ibss_sta_added) {
689                 u8 sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
690
691                 if (sta_id == IWL_INVALID_STATION) {
692                         IWL_DEBUG_RATE("LQ: ADD station %s\n",
693                                        print_mac(mac, hdr->addr1));
694                         sta_id = iwl3945_add_station(priv,
695                                     hdr->addr1, 0, CMD_ASYNC);
696                 }
697                 if (sta_id != IWL_INVALID_STATION)
698                         rs_sta->ibss_sta_added = 1;
699         }
700
701         spin_lock_irqsave(&rs_sta->lock, flags);
702
703         if (rs_sta->start_rate != IWL_RATE_INVALID) {
704                 index = rs_sta->start_rate;
705                 rs_sta->start_rate = IWL_RATE_INVALID;
706         }
707
708         window = &(rs_sta->win[index]);
709
710         fail_count = window->counter - window->success_counter;
711
712         if (((fail_count <= IWL_RATE_MIN_FAILURE_TH) &&
713              (window->success_counter < IWL_RATE_MIN_SUCCESS_TH))) {
714                 window->average_tpt = IWL_INV_TPT;
715                 spin_unlock_irqrestore(&rs_sta->lock, flags);
716
717                 IWL_DEBUG_RATE("Invalid average_tpt on rate %d: "
718                                "counter: %d, success_counter: %d, "
719                                "expected_tpt is %sNULL\n",
720                                index,
721                                window->counter,
722                                window->success_counter,
723                                rs_sta->expected_tpt ? "not " : "");
724                 goto out;
725
726         }
727
728         window->average_tpt = ((window->success_ratio *
729                                 rs_sta->expected_tpt[index] + 64) / 128);
730         current_tpt = window->average_tpt;
731
732         high_low = iwl3945_get_adjacent_rate(rs_sta, index, rate_mask,
733                                              sband->band);
734         low = high_low & 0xff;
735         high = (high_low >> 8) & 0xff;
736
737         if (low != IWL_RATE_INVALID)
738                 low_tpt = rs_sta->win[low].average_tpt;
739
740         if (high != IWL_RATE_INVALID)
741                 high_tpt = rs_sta->win[high].average_tpt;
742
743         spin_unlock_irqrestore(&rs_sta->lock, flags);
744
745         scale_action = 1;
746
747         if ((window->success_ratio < IWL_RATE_DECREASE_TH) || !current_tpt) {
748                 IWL_DEBUG_RATE("decrease rate because of low success_ratio\n");
749                 scale_action = -1;
750         } else if ((low_tpt == IWL_INV_TPT) && (high_tpt == IWL_INV_TPT))
751                 scale_action = 1;
752         else if ((low_tpt != IWL_INV_TPT) && (high_tpt != IWL_INV_TPT) &&
753                  (low_tpt < current_tpt) && (high_tpt < current_tpt)) {
754                 IWL_DEBUG_RATE("No action -- low [%d] & high [%d] < "
755                                "current_tpt [%d]\n",
756                                low_tpt, high_tpt, current_tpt);
757                 scale_action = 0;
758         } else {
759                 if (high_tpt != IWL_INV_TPT) {
760                         if (high_tpt > current_tpt)
761                                 scale_action = 1;
762                         else {
763                                 IWL_DEBUG_RATE
764                                     ("decrease rate because of high tpt\n");
765                                 scale_action = -1;
766                         }
767                 } else if (low_tpt != IWL_INV_TPT) {
768                         if (low_tpt > current_tpt) {
769                                 IWL_DEBUG_RATE
770                                     ("decrease rate because of low tpt\n");
771                                 scale_action = -1;
772                         } else
773                                 scale_action = 1;
774                 }
775         }
776
777         if ((window->success_ratio > IWL_RATE_HIGH_TH) ||
778             (current_tpt > window->average_tpt)) {
779                 IWL_DEBUG_RATE("No action -- success_ratio [%d] > HIGH_TH or "
780                                "current_tpt [%d] > average_tpt [%d]\n",
781                                window->success_ratio,
782                                current_tpt, window->average_tpt);
783                 scale_action = 0;
784         }
785
786         switch (scale_action) {
787         case -1:
788                 if (low != IWL_RATE_INVALID)
789                         index = low;
790                 break;
791
792         case 1:
793                 if (high != IWL_RATE_INVALID)
794                         index = high;
795
796                 break;
797
798         case 0:
799         default:
800                 break;
801         }
802
803         IWL_DEBUG_RATE("Selected %d (action %d) - low %d high %d\n",
804                        index, scale_action, low, high);
805
806  out:
807
808         rs_sta->last_txrate_idx = index;
809         if (sband->band == IEEE80211_BAND_5GHZ)
810                 sel->rate_idx = rs_sta->last_txrate_idx - IWL_FIRST_OFDM_RATE;
811         else
812                 sel->rate_idx = rs_sta->last_txrate_idx;
813
814         rcu_read_unlock();
815
816         IWL_DEBUG_RATE("leave: %d\n", index);
817 }
818
819 static struct rate_control_ops rs_ops = {
820         .module = NULL,
821         .name = RS_NAME,
822         .tx_status = rs_tx_status,
823         .get_rate = rs_get_rate,
824         .rate_init = rs_rate_init,
825         .clear = rs_clear,
826         .alloc = rs_alloc,
827         .free = rs_free,
828         .alloc_sta = rs_alloc_sta,
829         .free_sta = rs_free_sta,
830 };
831
832 int iwl3945_fill_rs_info(struct ieee80211_hw *hw, char *buf, u8 sta_id)
833 {
834         struct ieee80211_local *local = hw_to_local(hw);
835         struct iwl3945_priv *priv = hw->priv;
836         struct iwl3945_rs_sta *rs_sta;
837         struct sta_info *sta;
838         unsigned long flags;
839         int count = 0, i;
840         u32 samples = 0, success = 0, good = 0;
841         unsigned long now = jiffies;
842         u32 max_time = 0;
843
844         rcu_read_lock();
845
846         sta = sta_info_get(local, priv->stations[sta_id].sta.sta.addr);
847         if (!sta || !sta->rate_ctrl_priv) {
848                 if (sta)
849                         IWL_DEBUG_RATE("leave - no private rate data!\n");
850                 else
851                         IWL_DEBUG_RATE("leave - no station!\n");
852                 rcu_read_unlock();
853                 return sprintf(buf, "station %d not found\n", sta_id);
854         }
855
856         rs_sta = (void *)sta->rate_ctrl_priv;
857         spin_lock_irqsave(&rs_sta->lock, flags);
858         i = IWL_RATE_54M_INDEX;
859         while (1) {
860                 u64 mask;
861                 int j;
862
863                 count +=
864                     sprintf(&buf[count], " %2dMbs: ", iwl3945_rates[i].ieee / 2);
865
866                 mask = (1ULL << (IWL_RATE_MAX_WINDOW - 1));
867                 for (j = 0; j < IWL_RATE_MAX_WINDOW; j++, mask >>= 1)
868                         buf[count++] =
869                             (rs_sta->win[i].data & mask) ? '1' : '0';
870
871                 samples += rs_sta->win[i].counter;
872                 good += rs_sta->win[i].success_counter;
873                 success += rs_sta->win[i].success_counter *
874                                                 iwl3945_rates[i].ieee;
875
876                 if (rs_sta->win[i].stamp) {
877                         int delta =
878                             jiffies_to_msecs(now - rs_sta->win[i].stamp);
879
880                         if (delta > max_time)
881                                 max_time = delta;
882
883                         count += sprintf(&buf[count], "%5dms\n", delta);
884                 } else
885                         buf[count++] = '\n';
886
887                 j = iwl3945_get_prev_ieee_rate(i);
888                 if (j == i)
889                         break;
890                 i = j;
891         }
892         spin_unlock_irqrestore(&rs_sta->lock, flags);
893         rcu_read_unlock();
894
895         /* Display the average rate of all samples taken.
896          *
897          * NOTE:  We multiple # of samples by 2 since the IEEE measurement
898          * added from iwl3945_rates is actually 2X the rate */
899         if (samples)
900                 count += sprintf(
901                         &buf[count],
902                         "\nAverage rate is %3d.%02dMbs over last %4dms\n"
903                         "%3d%% success (%d good packets over %d tries)\n",
904                         success / (2 * samples), (success * 5 / samples) % 10,
905                         max_time, good * 100 / samples, good, samples);
906         else
907                 count += sprintf(&buf[count], "\nAverage rate: 0Mbs\n");
908
909         return count;
910 }
911
912 void iwl3945_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id)
913 {
914         struct iwl3945_priv *priv = hw->priv;
915         s32 rssi = 0;
916         unsigned long flags;
917         struct ieee80211_local *local = hw_to_local(hw);
918         struct iwl3945_rs_sta *rs_sta;
919         struct sta_info *sta;
920
921         IWL_DEBUG_RATE("enter\n");
922
923         if (!local->rate_ctrl->ops->name ||
924             strcmp(local->rate_ctrl->ops->name, RS_NAME)) {
925                 IWL_WARNING("iwl-3945-rs not selected as rate control algo!\n");
926                 IWL_DEBUG_RATE("leave - mac80211 picked the wrong RC algo.\n");
927                 return;
928         }
929
930         rcu_read_lock();
931
932         sta = sta_info_get(local, priv->stations[sta_id].sta.sta.addr);
933         if (!sta || !sta->rate_ctrl_priv) {
934                 IWL_DEBUG_RATE("leave - no private rate data!\n");
935                 rcu_read_unlock();
936                 return;
937         }
938
939         rs_sta = (void *)sta->rate_ctrl_priv;
940
941         spin_lock_irqsave(&rs_sta->lock, flags);
942
943         rs_sta->tgg = 0;
944         switch (priv->band) {
945         case IEEE80211_BAND_2GHZ:
946                 /* TODO: this always does G, not a regression */
947                 if (priv->active_rxon.flags & RXON_FLG_TGG_PROTECT_MSK) {
948                         rs_sta->tgg = 1;
949                         rs_sta->expected_tpt = iwl3945_expected_tpt_g_prot;
950                 } else
951                         rs_sta->expected_tpt = iwl3945_expected_tpt_g;
952                 break;
953
954         case IEEE80211_BAND_5GHZ:
955                 rs_sta->expected_tpt = iwl3945_expected_tpt_a;
956                 break;
957         case IEEE80211_NUM_BANDS:
958                 BUG();
959                 break;
960         }
961
962         rcu_read_unlock();
963         spin_unlock_irqrestore(&rs_sta->lock, flags);
964
965         rssi = priv->last_rx_rssi;
966         if (rssi == 0)
967                 rssi = IWL_MIN_RSSI_VAL;
968
969         IWL_DEBUG(IWL_DL_INFO | IWL_DL_RATE, "Network RSSI: %d\n", rssi);
970
971         rs_sta->start_rate = iwl3945_get_rate_index_by_rssi(rssi, priv->band);
972
973         IWL_DEBUG_RATE("leave: rssi %d assign rate index: "
974                        "%d (plcp 0x%x)\n", rssi, rs_sta->start_rate,
975                        iwl3945_rates[rs_sta->start_rate].plcp);
976 }
977
978 int iwl3945_rate_control_register(void)
979 {
980         return ieee80211_rate_control_register(&rs_ops);
981 }
982
983 void iwl3945_rate_control_unregister(void)
984 {
985         ieee80211_rate_control_unregister(&rs_ops);
986 }
987
988