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