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