Merge branch 'linus' into irq/threaded
[linux-2.6] / drivers / net / wireless / iwlwifi / iwl-3945-rs.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2005 - 2009 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  *  Intel Linux Wireless <ilw@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-commands.h"
40 #include "iwl-3945.h"
41
42 #define RS_NAME "iwl-3945-rs"
43
44 struct iwl3945_rate_scale_data {
45         u64 data;
46         s32 success_counter;
47         s32 success_ratio;
48         s32 counter;
49         s32 average_tpt;
50         unsigned long stamp;
51 };
52
53 struct iwl3945_rs_sta {
54         spinlock_t lock;
55         struct iwl_priv *priv;
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_3945];
68 #ifdef CONFIG_MAC80211_DEBUGFS
69         struct dentry *rs_sta_dbgfs_stats_table_file;
70 #endif
71
72         /* used to be in sta_info */
73         int last_txrate_idx;
74 };
75
76 static s32 iwl3945_expected_tpt_g[IWL_RATE_COUNT_3945] = {
77         7, 13, 35, 58, 0, 0, 76, 104, 130, 168, 191, 202
78 };
79
80 static s32 iwl3945_expected_tpt_g_prot[IWL_RATE_COUNT_3945] = {
81         7, 13, 35, 58, 0, 0, 0, 80, 93, 113, 123, 125
82 };
83
84 static s32 iwl3945_expected_tpt_a[IWL_RATE_COUNT_3945] = {
85         0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186
86 };
87
88 static s32 iwl3945_expected_tpt_b[IWL_RATE_COUNT_3945] = {
89         7, 13, 35, 58, 0, 0, 0, 0, 0, 0, 0, 0
90 };
91
92 struct iwl3945_tpt_entry {
93         s8 min_rssi;
94         u8 index;
95 };
96
97 static struct iwl3945_tpt_entry iwl3945_tpt_table_a[] = {
98         {-60, IWL_RATE_54M_INDEX},
99         {-64, IWL_RATE_48M_INDEX},
100         {-72, IWL_RATE_36M_INDEX},
101         {-80, IWL_RATE_24M_INDEX},
102         {-84, IWL_RATE_18M_INDEX},
103         {-85, IWL_RATE_12M_INDEX},
104         {-87, IWL_RATE_9M_INDEX},
105         {-89, IWL_RATE_6M_INDEX}
106 };
107
108 static struct iwl3945_tpt_entry iwl3945_tpt_table_g[] = {
109         {-60, IWL_RATE_54M_INDEX},
110         {-64, IWL_RATE_48M_INDEX},
111         {-68, IWL_RATE_36M_INDEX},
112         {-80, IWL_RATE_24M_INDEX},
113         {-84, IWL_RATE_18M_INDEX},
114         {-85, IWL_RATE_12M_INDEX},
115         {-86, IWL_RATE_11M_INDEX},
116         {-88, IWL_RATE_5M_INDEX},
117         {-90, IWL_RATE_2M_INDEX},
118         {-92, IWL_RATE_1M_INDEX}
119 };
120
121 #define IWL_RATE_MAX_WINDOW          62
122 #define IWL_RATE_FLUSH           (3*HZ)
123 #define IWL_RATE_WIN_FLUSH       (HZ/2)
124 #define IWL39_RATE_HIGH_TH          11520
125 #define IWL_SUCCESS_UP_TH          8960
126 #define IWL_SUCCESS_DOWN_TH       10880
127 #define IWL_RATE_MIN_FAILURE_TH       6
128 #define IWL_RATE_MIN_SUCCESS_TH       8
129 #define IWL_RATE_DECREASE_TH       1920
130 #define IWL_RATE_RETRY_TH            15
131
132 static u8 iwl3945_get_rate_index_by_rssi(s32 rssi, enum ieee80211_band band)
133 {
134         u32 index = 0;
135         u32 table_size = 0;
136         struct iwl3945_tpt_entry *tpt_table = NULL;
137
138         if ((rssi < IWL_MIN_RSSI_VAL) || (rssi > IWL_MAX_RSSI_VAL))
139                 rssi = IWL_MIN_RSSI_VAL;
140
141         switch (band) {
142         case IEEE80211_BAND_2GHZ:
143                 tpt_table = iwl3945_tpt_table_g;
144                 table_size = ARRAY_SIZE(iwl3945_tpt_table_g);
145                 break;
146
147         case IEEE80211_BAND_5GHZ:
148                 tpt_table = iwl3945_tpt_table_a;
149                 table_size = ARRAY_SIZE(iwl3945_tpt_table_a);
150                 break;
151
152         default:
153                 BUG();
154                 break;
155         }
156
157         while ((index < table_size) && (rssi < tpt_table[index].min_rssi))
158                 index++;
159
160         index = min(index, (table_size - 1));
161
162         return tpt_table[index].index;
163 }
164
165 static void iwl3945_clear_window(struct iwl3945_rate_scale_data *window)
166 {
167         window->data = 0;
168         window->success_counter = 0;
169         window->success_ratio = -1;
170         window->counter = 0;
171         window->average_tpt = IWL_INVALID_VALUE;
172         window->stamp = 0;
173 }
174
175 /**
176  * iwl3945_rate_scale_flush_windows - flush out the rate scale windows
177  *
178  * Returns the number of windows that have gathered data but were
179  * not flushed.  If there were any that were not flushed, then
180  * reschedule the rate flushing routine.
181  */
182 static int iwl3945_rate_scale_flush_windows(struct iwl3945_rs_sta *rs_sta)
183 {
184         int unflushed = 0;
185         int i;
186         unsigned long flags;
187         struct iwl_priv *priv __maybe_unused = rs_sta->priv;
188
189         /*
190          * For each rate, if we have collected data on that rate
191          * and it has been more than IWL_RATE_WIN_FLUSH
192          * since we flushed, clear out the gathered statistics
193          */
194         for (i = 0; i < IWL_RATE_COUNT_3945; i++) {
195                 if (!rs_sta->win[i].counter)
196                         continue;
197
198                 spin_lock_irqsave(&rs_sta->lock, flags);
199                 if (time_after(jiffies, rs_sta->win[i].stamp +
200                                IWL_RATE_WIN_FLUSH)) {
201                         IWL_DEBUG_RATE(priv, "flushing %d samples of rate "
202                                        "index %d\n",
203                                        rs_sta->win[i].counter, i);
204                         iwl3945_clear_window(&rs_sta->win[i]);
205                 } else
206                         unflushed++;
207                 spin_unlock_irqrestore(&rs_sta->lock, flags);
208         }
209
210         return unflushed;
211 }
212
213 #define IWL_RATE_FLUSH_MAX              5000    /* msec */
214 #define IWL_RATE_FLUSH_MIN              50      /* msec */
215 #define IWL_AVERAGE_PACKETS             1500
216
217 static void iwl3945_bg_rate_scale_flush(unsigned long data)
218 {
219         struct iwl3945_rs_sta *rs_sta = (void *)data;
220         struct iwl_priv *priv __maybe_unused = rs_sta->priv;
221         int unflushed = 0;
222         unsigned long flags;
223         u32 packet_count, duration, pps;
224
225         IWL_DEBUG_RATE(priv, "enter\n");
226
227         unflushed = iwl3945_rate_scale_flush_windows(rs_sta);
228
229         spin_lock_irqsave(&rs_sta->lock, flags);
230
231         /* Number of packets Rx'd since last time this timer ran */
232         packet_count = (rs_sta->tx_packets - rs_sta->last_tx_packets) + 1;
233
234         rs_sta->last_tx_packets = rs_sta->tx_packets + 1;
235
236         if (unflushed) {
237                 duration =
238                     jiffies_to_msecs(jiffies - rs_sta->last_partial_flush);
239
240                 IWL_DEBUG_RATE(priv, "Tx'd %d packets in %dms\n",
241                                packet_count, duration);
242
243                 /* Determine packets per second */
244                 if (duration)
245                         pps = (packet_count * 1000) / duration;
246                 else
247                         pps = 0;
248
249                 if (pps) {
250                         duration = (IWL_AVERAGE_PACKETS * 1000) / pps;
251                         if (duration < IWL_RATE_FLUSH_MIN)
252                                 duration = IWL_RATE_FLUSH_MIN;
253                         else if (duration > IWL_RATE_FLUSH_MAX)
254                                 duration = IWL_RATE_FLUSH_MAX;
255                 } else
256                         duration = IWL_RATE_FLUSH_MAX;
257
258                 rs_sta->flush_time = msecs_to_jiffies(duration);
259
260                 IWL_DEBUG_RATE(priv, "new flush period: %d msec ave %d\n",
261                                duration, packet_count);
262
263                 mod_timer(&rs_sta->rate_scale_flush, jiffies +
264                           rs_sta->flush_time);
265
266                 rs_sta->last_partial_flush = jiffies;
267         } else {
268                 rs_sta->flush_time = IWL_RATE_FLUSH;
269                 rs_sta->flush_pending = 0;
270         }
271         /* If there weren't any unflushed entries, we don't schedule the timer
272          * to run again */
273
274         rs_sta->last_flush = jiffies;
275
276         spin_unlock_irqrestore(&rs_sta->lock, flags);
277
278         IWL_DEBUG_RATE(priv, "leave\n");
279 }
280
281 /**
282  * iwl3945_collect_tx_data - Update the success/failure sliding window
283  *
284  * We keep a sliding window of the last 64 packets transmitted
285  * at this rate.  window->data contains the bitmask of successful
286  * packets.
287  */
288 static void iwl3945_collect_tx_data(struct iwl3945_rs_sta *rs_sta,
289                                 struct iwl3945_rate_scale_data *window,
290                                 int success, int retries, int index)
291 {
292         unsigned long flags;
293         s32 fail_count;
294         struct iwl_priv *priv __maybe_unused = rs_sta->priv;
295
296         if (!retries) {
297                 IWL_DEBUG_RATE(priv, "leave: retries == 0 -- should be at least 1\n");
298                 return;
299         }
300
301         spin_lock_irqsave(&rs_sta->lock, flags);
302
303         /*
304          * Keep track of only the latest 62 tx frame attempts in this rate's
305          * history window; anything older isn't really relevant any more.
306          * If we have filled up the sliding window, drop the oldest attempt;
307          * if the oldest attempt (highest bit in bitmap) shows "success",
308          * subtract "1" from the success counter (this is the main reason
309          * we keep these bitmaps!).
310          * */
311         while (retries > 0) {
312                 if (window->counter >= IWL_RATE_MAX_WINDOW) {
313
314                         /* remove earliest */
315                         window->counter = IWL_RATE_MAX_WINDOW - 1;
316
317                         if (window->data & (1ULL << (IWL_RATE_MAX_WINDOW - 1))) {
318                                 window->data &= ~(1ULL << (IWL_RATE_MAX_WINDOW - 1));
319                                 window->success_counter--;
320                         }
321                 }
322
323                 /* Increment frames-attempted counter */
324                 window->counter++;
325
326                 /* Shift bitmap by one frame (throw away oldest history),
327                  * OR in "1", and increment "success" if this
328                  * frame was successful. */
329                 window->data <<= 1;
330                 if (success > 0) {
331                         window->success_counter++;
332                         window->data |= 0x1;
333                         success--;
334                 }
335
336                 retries--;
337         }
338
339         /* Calculate current success ratio, avoid divide-by-0! */
340         if (window->counter > 0)
341                 window->success_ratio = 128 * (100 * window->success_counter)
342                                         / window->counter;
343         else
344                 window->success_ratio = IWL_INVALID_VALUE;
345
346         fail_count = window->counter - window->success_counter;
347
348         /* Calculate average throughput, if we have enough history. */
349         if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
350             (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
351                 window->average_tpt = ((window->success_ratio *
352                                 rs_sta->expected_tpt[index] + 64) / 128);
353         else
354                 window->average_tpt = IWL_INVALID_VALUE;
355
356         /* Tag this window as having been updated */
357         window->stamp = jiffies;
358
359         spin_unlock_irqrestore(&rs_sta->lock, flags);
360
361 }
362
363 static void rs_rate_init(void *priv_r, struct ieee80211_supported_band *sband,
364                          struct ieee80211_sta *sta, void *priv_sta)
365 {
366         struct iwl3945_rs_sta *rs_sta = priv_sta;
367         struct iwl_priv *priv = (struct iwl_priv *)priv_r;
368         int i;
369
370         IWL_DEBUG_RATE(priv, "enter\n");
371
372         /* TODO: what is a good starting rate for STA? About middle? Maybe not
373          * the lowest or the highest rate.. Could consider using RSSI from
374          * previous packets? Need to have IEEE 802.1X auth succeed immediately
375          * after assoc.. */
376
377         for (i = sband->n_bitrates - 1; i >= 0; i--) {
378                 if (sta->supp_rates[sband->band] & (1 << i)) {
379                         rs_sta->last_txrate_idx = i;
380                         break;
381                 }
382         }
383
384         priv->sta_supp_rates = sta->supp_rates[sband->band];
385         /* For 5 GHz band it start at IWL_FIRST_OFDM_RATE */
386         if (sband->band == IEEE80211_BAND_5GHZ) {
387                 rs_sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
388                 priv->sta_supp_rates = priv->sta_supp_rates <<
389                                                 IWL_FIRST_OFDM_RATE;
390         }
391
392
393         IWL_DEBUG_RATE(priv, "leave\n");
394 }
395
396 static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
397 {
398         return hw->priv;
399 }
400
401 /* rate scale requires free function to be implemented */
402 static void rs_free(void *priv)
403 {
404         return;
405 }
406
407 static void *rs_alloc_sta(void *iwl_priv, struct ieee80211_sta *sta, gfp_t gfp)
408 {
409         struct iwl3945_rs_sta *rs_sta;
410         struct iwl3945_sta_priv *psta = (void *) sta->drv_priv;
411         struct iwl_priv *priv = iwl_priv;
412         int i;
413
414         /*
415          * XXX: If it's using sta->drv_priv anyway, it might
416          *      as well just put all the information there.
417          */
418
419         IWL_DEBUG_RATE(priv, "enter\n");
420
421         rs_sta = kzalloc(sizeof(struct iwl3945_rs_sta), gfp);
422         if (!rs_sta) {
423                 IWL_DEBUG_RATE(priv, "leave: ENOMEM\n");
424                 return NULL;
425         }
426
427         psta->rs_sta = rs_sta;
428
429         spin_lock_init(&rs_sta->lock);
430
431         rs_sta->priv = priv;
432
433         rs_sta->start_rate = IWL_RATE_INVALID;
434
435         /* default to just 802.11b */
436         rs_sta->expected_tpt = iwl3945_expected_tpt_b;
437
438         rs_sta->last_partial_flush = jiffies;
439         rs_sta->last_flush = jiffies;
440         rs_sta->flush_time = IWL_RATE_FLUSH;
441         rs_sta->last_tx_packets = 0;
442         rs_sta->ibss_sta_added = 0;
443
444         init_timer(&rs_sta->rate_scale_flush);
445         rs_sta->rate_scale_flush.data = (unsigned long)rs_sta;
446         rs_sta->rate_scale_flush.function = &iwl3945_bg_rate_scale_flush;
447
448         for (i = 0; i < IWL_RATE_COUNT_3945; i++)
449                 iwl3945_clear_window(&rs_sta->win[i]);
450
451         IWL_DEBUG_RATE(priv, "leave\n");
452
453         return rs_sta;
454 }
455
456 static void rs_free_sta(void *iwl_priv, struct ieee80211_sta *sta,
457                         void *priv_sta)
458 {
459         struct iwl3945_sta_priv *psta = (void *) sta->drv_priv;
460         struct iwl3945_rs_sta *rs_sta = priv_sta;
461         struct iwl_priv *priv __maybe_unused = rs_sta->priv;
462
463         psta->rs_sta = NULL;
464
465         IWL_DEBUG_RATE(priv, "enter\n");
466         del_timer_sync(&rs_sta->rate_scale_flush);
467         kfree(rs_sta);
468         IWL_DEBUG_RATE(priv, "leave\n");
469 }
470
471
472 /**
473  * rs_tx_status - Update rate control values based on Tx results
474  *
475  * NOTE: Uses iwl_priv->retry_rate for the # of retries attempted by
476  * the hardware for each rate.
477  */
478 static void rs_tx_status(void *priv_rate, struct ieee80211_supported_band *sband,
479                          struct ieee80211_sta *sta, void *priv_sta,
480                          struct sk_buff *skb)
481 {
482         s8 retries = 0, current_count;
483         int scale_rate_index, first_index, last_index;
484         unsigned long flags;
485         struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
486         struct iwl3945_rs_sta *rs_sta = priv_sta;
487         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
488
489         IWL_DEBUG_RATE(priv, "enter\n");
490
491         retries = info->status.rates[0].count;
492         /* Sanity Check for retries */
493         if (retries > IWL_RATE_RETRY_TH)
494                 retries = IWL_RATE_RETRY_TH;
495
496         first_index = sband->bitrates[info->status.rates[0].idx].hw_value;
497         if ((first_index < 0) || (first_index >= IWL_RATE_COUNT_3945)) {
498                 IWL_DEBUG_RATE(priv, "leave: Rate out of bounds: %d\n", first_index);
499                 return;
500         }
501
502         if (!priv_sta) {
503                 IWL_DEBUG_RATE(priv, "leave: No STA priv data to update!\n");
504                 return;
505         }
506
507         rs_sta->tx_packets++;
508
509         scale_rate_index = first_index;
510         last_index = first_index;
511
512         /*
513          * Update the window for each rate.  We determine which rates
514          * were Tx'd based on the total number of retries vs. the number
515          * of retries configured for each rate -- currently set to the
516          * priv value 'retry_rate' vs. rate specific
517          *
518          * On exit from this while loop last_index indicates the rate
519          * at which the frame was finally transmitted (or failed if no
520          * ACK)
521          */
522         while (retries > 1) {
523                 if ((retries - 1) < priv->retry_rate) {
524                         current_count = (retries - 1);
525                         last_index = scale_rate_index;
526                 } else {
527                         current_count = priv->retry_rate;
528                         last_index = iwl3945_rs_next_rate(priv,
529                                                          scale_rate_index);
530                 }
531
532                 /* Update this rate accounting for as many retries
533                  * as was used for it (per current_count) */
534                 iwl3945_collect_tx_data(rs_sta,
535                                     &rs_sta->win[scale_rate_index],
536                                     0, current_count, scale_rate_index);
537                 IWL_DEBUG_RATE(priv, "Update rate %d for %d retries.\n",
538                                scale_rate_index, current_count);
539
540                 retries -= current_count;
541
542                 scale_rate_index = last_index;
543         }
544
545
546         /* Update the last index window with success/failure based on ACK */
547         IWL_DEBUG_RATE(priv, "Update rate %d with %s.\n",
548                        last_index,
549                        (info->flags & IEEE80211_TX_STAT_ACK) ?
550                        "success" : "failure");
551         iwl3945_collect_tx_data(rs_sta,
552                             &rs_sta->win[last_index],
553                             info->flags & IEEE80211_TX_STAT_ACK, 1, last_index);
554
555         /* We updated the rate scale window -- if its been more than
556          * flush_time since the last run, schedule the flush
557          * again */
558         spin_lock_irqsave(&rs_sta->lock, flags);
559
560         if (!rs_sta->flush_pending &&
561             time_after(jiffies, rs_sta->last_flush +
562                        rs_sta->flush_time)) {
563
564                 rs_sta->last_partial_flush = jiffies;
565                 rs_sta->flush_pending = 1;
566                 mod_timer(&rs_sta->rate_scale_flush,
567                           jiffies + rs_sta->flush_time);
568         }
569
570         spin_unlock_irqrestore(&rs_sta->lock, flags);
571
572         IWL_DEBUG_RATE(priv, "leave\n");
573
574         return;
575 }
576
577 static u16 iwl3945_get_adjacent_rate(struct iwl3945_rs_sta *rs_sta,
578                                  u8 index, u16 rate_mask, enum ieee80211_band band)
579 {
580         u8 high = IWL_RATE_INVALID;
581         u8 low = IWL_RATE_INVALID;
582         struct iwl_priv *priv __maybe_unused = rs_sta->priv;
583
584         /* 802.11A walks to the next literal adjacent rate in
585          * the rate table */
586         if (unlikely(band == IEEE80211_BAND_5GHZ)) {
587                 int i;
588                 u32 mask;
589
590                 /* Find the previous rate that is in the rate mask */
591                 i = index - 1;
592                 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
593                         if (rate_mask & mask) {
594                                 low = i;
595                                 break;
596                         }
597                 }
598
599                 /* Find the next rate that is in the rate mask */
600                 i = index + 1;
601                 for (mask = (1 << i); i < IWL_RATE_COUNT_3945;
602                      i++, mask <<= 1) {
603                         if (rate_mask & mask) {
604                                 high = i;
605                                 break;
606                         }
607                 }
608
609                 return (high << 8) | low;
610         }
611
612         low = index;
613         while (low != IWL_RATE_INVALID) {
614                 if (rs_sta->tgg)
615                         low = iwl3945_rates[low].prev_rs_tgg;
616                 else
617                         low = iwl3945_rates[low].prev_rs;
618                 if (low == IWL_RATE_INVALID)
619                         break;
620                 if (rate_mask & (1 << low))
621                         break;
622                 IWL_DEBUG_RATE(priv, "Skipping masked lower rate: %d\n", low);
623         }
624
625         high = index;
626         while (high != IWL_RATE_INVALID) {
627                 if (rs_sta->tgg)
628                         high = iwl3945_rates[high].next_rs_tgg;
629                 else
630                         high = iwl3945_rates[high].next_rs;
631                 if (high == IWL_RATE_INVALID)
632                         break;
633                 if (rate_mask & (1 << high))
634                         break;
635                 IWL_DEBUG_RATE(priv, "Skipping masked higher rate: %d\n", high);
636         }
637
638         return (high << 8) | low;
639 }
640
641 /**
642  * rs_get_rate - find the rate for the requested packet
643  *
644  * Returns the ieee80211_rate structure allocated by the driver.
645  *
646  * The rate control algorithm has no internal mapping between hw_mode's
647  * rate ordering and the rate ordering used by the rate control algorithm.
648  *
649  * The rate control algorithm uses a single table of rates that goes across
650  * the entire A/B/G spectrum vs. being limited to just one particular
651  * hw_mode.
652  *
653  * As such, we can't convert the index obtained below into the hw_mode's
654  * rate table and must reference the driver allocated rate table
655  *
656  */
657 static void rs_get_rate(void *priv_r, struct ieee80211_sta *sta,
658                         void *priv_sta, struct ieee80211_tx_rate_control *txrc)
659 {
660         struct ieee80211_supported_band *sband = txrc->sband;
661         struct sk_buff *skb = txrc->skb;
662         u8 low = IWL_RATE_INVALID;
663         u8 high = IWL_RATE_INVALID;
664         u16 high_low;
665         int index;
666         struct iwl3945_rs_sta *rs_sta = priv_sta;
667         struct iwl3945_rate_scale_data *window = NULL;
668         int current_tpt = IWL_INVALID_VALUE;
669         int low_tpt = IWL_INVALID_VALUE;
670         int high_tpt = IWL_INVALID_VALUE;
671         u32 fail_count;
672         s8 scale_action = 0;
673         unsigned long flags;
674         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
675         u16 fc;
676         u16 rate_mask = 0;
677         s8 max_rate_idx = -1;
678         struct iwl_priv *priv = (struct iwl_priv *)priv_r;
679         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
680
681         IWL_DEBUG_RATE(priv, "enter\n");
682
683         if (sta)
684                 rate_mask = sta->supp_rates[sband->band];
685
686         /* Send management frames and broadcast/multicast data using lowest
687          * rate. */
688         fc = le16_to_cpu(hdr->frame_control);
689         if ((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA ||
690             is_multicast_ether_addr(hdr->addr1) ||
691             !sta || !priv_sta) {
692                 IWL_DEBUG_RATE(priv, "leave: No STA priv data to update!\n");
693                 if (!rate_mask)
694                         info->control.rates[0].idx =
695                                         rate_lowest_index(sband, NULL);
696                 else
697                         info->control.rates[0].idx =
698                                         rate_lowest_index(sband, sta);
699                 return;
700         }
701
702         /* get user max rate if set */
703         max_rate_idx = txrc->max_rate_idx;
704         if ((sband->band == IEEE80211_BAND_5GHZ) && (max_rate_idx != -1))
705                 max_rate_idx += IWL_FIRST_OFDM_RATE;
706         if ((max_rate_idx < 0) || (max_rate_idx >= IWL_RATE_COUNT))
707                 max_rate_idx = -1;
708
709         index = min(rs_sta->last_txrate_idx & 0xffff, IWL_RATE_COUNT_3945 - 1);
710
711         if (sband->band == IEEE80211_BAND_5GHZ)
712                 rate_mask = rate_mask << IWL_FIRST_OFDM_RATE;
713
714         if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) &&
715             !rs_sta->ibss_sta_added) {
716                 u8 sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
717
718                 if (sta_id == IWL_INVALID_STATION) {
719                         IWL_DEBUG_RATE(priv, "LQ: ADD station %pm\n",
720                                        hdr->addr1);
721                         sta_id = iwl3945_add_station(priv,
722                                     hdr->addr1, 0, CMD_ASYNC);
723                 }
724                 if (sta_id != IWL_INVALID_STATION)
725                         rs_sta->ibss_sta_added = 1;
726         }
727
728         spin_lock_irqsave(&rs_sta->lock, flags);
729
730         /* for recent assoc, choose best rate regarding
731          * to rssi value
732          */
733         if (rs_sta->start_rate != IWL_RATE_INVALID) {
734                 if (rs_sta->start_rate < index &&
735                    (rate_mask & (1 << rs_sta->start_rate)))
736                         index = rs_sta->start_rate;
737                 rs_sta->start_rate = IWL_RATE_INVALID;
738         }
739
740         /* force user max rate if set by user */
741         if ((max_rate_idx != -1) && (max_rate_idx < index)) {
742                 if (rate_mask & (1 << max_rate_idx))
743                         index = max_rate_idx;
744         }
745
746         window = &(rs_sta->win[index]);
747
748         fail_count = window->counter - window->success_counter;
749
750         if (((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
751              (window->success_counter < IWL_RATE_MIN_SUCCESS_TH))) {
752                 spin_unlock_irqrestore(&rs_sta->lock, flags);
753
754                 IWL_DEBUG_RATE(priv, "Invalid average_tpt on rate %d: "
755                                "counter: %d, success_counter: %d, "
756                                "expected_tpt is %sNULL\n",
757                                index,
758                                window->counter,
759                                window->success_counter,
760                                rs_sta->expected_tpt ? "not " : "");
761
762            /* Can't calculate this yet; not enough history */
763                 window->average_tpt = IWL_INVALID_VALUE;
764                 goto out;
765
766         }
767
768         current_tpt = window->average_tpt;
769
770         high_low = iwl3945_get_adjacent_rate(rs_sta, index, rate_mask,
771                                              sband->band);
772         low = high_low & 0xff;
773         high = (high_low >> 8) & 0xff;
774
775         /* If user set max rate, dont allow higher than user constrain */
776         if ((max_rate_idx != -1) && (max_rate_idx < high))
777                 high = IWL_RATE_INVALID;
778
779         /* Collect Measured throughputs of adjacent rates */
780         if (low != IWL_RATE_INVALID)
781                 low_tpt = rs_sta->win[low].average_tpt;
782
783         if (high != IWL_RATE_INVALID)
784                 high_tpt = rs_sta->win[high].average_tpt;
785
786         spin_unlock_irqrestore(&rs_sta->lock, flags);
787
788         scale_action = 0;
789
790         /* Low success ratio , need to drop the rate */
791         if ((window->success_ratio < IWL_RATE_DECREASE_TH) || !current_tpt) {
792                 IWL_DEBUG_RATE(priv, "decrease rate because of low success_ratio\n");
793                 scale_action = -1;
794         /* No throughput measured yet for adjacent rates,
795          * try increase */
796         } else if ((low_tpt == IWL_INVALID_VALUE) &&
797                    (high_tpt == IWL_INVALID_VALUE)) {
798
799                 if (high != IWL_RATE_INVALID && window->success_ratio >= IWL_RATE_INCREASE_TH)
800                         scale_action = 1;
801                 else if (low != IWL_RATE_INVALID)
802                         scale_action = 0;
803
804         /* Both adjacent throughputs are measured, but neither one has
805          * better throughput; we're using the best rate, don't change
806          * it! */
807         } else if ((low_tpt != IWL_INVALID_VALUE) &&
808                  (high_tpt != IWL_INVALID_VALUE) &&
809                  (low_tpt < current_tpt) && (high_tpt < current_tpt)) {
810
811                 IWL_DEBUG_RATE(priv, "No action -- low [%d] & high [%d] < "
812                                "current_tpt [%d]\n",
813                                low_tpt, high_tpt, current_tpt);
814                 scale_action = 0;
815
816         /* At least one of the rates has better throughput */
817         } else {
818                 if (high_tpt != IWL_INVALID_VALUE) {
819
820                         /* High rate has better throughput, Increase
821                          * rate */
822                         if (high_tpt > current_tpt &&
823                                 window->success_ratio >= IWL_RATE_INCREASE_TH)
824                                 scale_action = 1;
825                         else {
826                                 IWL_DEBUG_RATE(priv,
827                                     "decrease rate because of high tpt\n");
828                                 scale_action = 0;
829                         }
830                 } else if (low_tpt != IWL_INVALID_VALUE) {
831                         if (low_tpt > current_tpt) {
832                                 IWL_DEBUG_RATE(priv,
833                                     "decrease rate because of low tpt\n");
834                                 scale_action = -1;
835                         } else if (window->success_ratio >= IWL_RATE_INCREASE_TH) {
836                                 /* Lower rate has better
837                                  * throughput,decrease rate */
838                                 scale_action = 1;
839                         }
840                 }
841         }
842
843         /* Sanity check; asked for decrease, but success rate or throughput
844          * has been good at old rate.  Don't change it. */
845         if ((scale_action == -1) && (low != IWL_RATE_INVALID) &&
846                     ((window->success_ratio > IWL_RATE_HIGH_TH) ||
847                      (current_tpt > (100 * rs_sta->expected_tpt[low]))))
848                 scale_action = 0;
849
850         switch (scale_action) {
851         case -1:
852
853                 /* Decrese rate */
854                 if (low != IWL_RATE_INVALID)
855                         index = low;
856                 break;
857
858         case 1:
859                 /* Increase rate */
860                 if (high != IWL_RATE_INVALID)
861                         index = high;
862
863                 break;
864
865         case 0:
866         default:
867                 /* No change */
868                 break;
869         }
870
871         IWL_DEBUG_RATE(priv, "Selected %d (action %d) - low %d high %d\n",
872                        index, scale_action, low, high);
873
874  out:
875
876         rs_sta->last_txrate_idx = index;
877         if (sband->band == IEEE80211_BAND_5GHZ)
878                 info->control.rates[0].idx = rs_sta->last_txrate_idx -
879                                 IWL_FIRST_OFDM_RATE;
880         else
881                 info->control.rates[0].idx = rs_sta->last_txrate_idx;
882
883         IWL_DEBUG_RATE(priv, "leave: %d\n", index);
884 }
885
886 #ifdef CONFIG_MAC80211_DEBUGFS
887 static int iwl3945_open_file_generic(struct inode *inode, struct file *file)
888 {
889         file->private_data = inode->i_private;
890         return 0;
891 }
892
893 static ssize_t iwl3945_sta_dbgfs_stats_table_read(struct file *file,
894                                                   char __user *user_buf,
895                                                   size_t count, loff_t *ppos)
896 {
897         char *buff;
898         int desc = 0;
899         int j;
900         ssize_t ret;
901         struct iwl3945_rs_sta *lq_sta = file->private_data;
902
903         buff = kmalloc(1024, GFP_KERNEL);
904         if (!buff)
905                 return -ENOMEM;
906
907         desc += sprintf(buff + desc, "tx packets=%d last rate index=%d\n"
908                         "rate=0x%X flush time %d\n",
909                         lq_sta->tx_packets,
910                         lq_sta->last_txrate_idx,
911                         lq_sta->start_rate, jiffies_to_msecs(lq_sta->flush_time));
912         for (j = 0; j < IWL_RATE_COUNT_3945; j++) {
913                 desc += sprintf(buff+desc,
914                                 "counter=%d success=%d %%=%d\n",
915                                 lq_sta->win[j].counter,
916                                 lq_sta->win[j].success_counter,
917                                 lq_sta->win[j].success_ratio);
918         }
919         ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
920         kfree(buff);
921         return ret;
922 }
923
924 static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
925         .read = iwl3945_sta_dbgfs_stats_table_read,
926         .open = iwl3945_open_file_generic,
927 };
928
929 static void iwl3945_add_debugfs(void *priv, void *priv_sta,
930                                 struct dentry *dir)
931 {
932         struct iwl3945_rs_sta *lq_sta = priv_sta;
933
934         lq_sta->rs_sta_dbgfs_stats_table_file =
935                 debugfs_create_file("rate_stats_table", 0600, dir,
936                 lq_sta, &rs_sta_dbgfs_stats_table_ops);
937
938 }
939
940 static void iwl3945_remove_debugfs(void *priv, void *priv_sta)
941 {
942         struct iwl3945_rs_sta *lq_sta = priv_sta;
943         debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
944 }
945 #endif
946
947 static struct rate_control_ops rs_ops = {
948         .module = NULL,
949         .name = RS_NAME,
950         .tx_status = rs_tx_status,
951         .get_rate = rs_get_rate,
952         .rate_init = rs_rate_init,
953         .alloc = rs_alloc,
954         .free = rs_free,
955         .alloc_sta = rs_alloc_sta,
956         .free_sta = rs_free_sta,
957 #ifdef CONFIG_MAC80211_DEBUGFS
958         .add_sta_debugfs = iwl3945_add_debugfs,
959         .remove_sta_debugfs = iwl3945_remove_debugfs,
960 #endif
961
962 };
963
964 void iwl3945_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id)
965 {
966         struct iwl_priv *priv = hw->priv;
967         s32 rssi = 0;
968         unsigned long flags;
969         struct iwl3945_rs_sta *rs_sta;
970         struct ieee80211_sta *sta;
971         struct iwl3945_sta_priv *psta;
972
973         IWL_DEBUG_RATE(priv, "enter\n");
974
975         rcu_read_lock();
976
977         sta = ieee80211_find_sta(hw, priv->stations_39[sta_id].sta.sta.addr);
978         if (!sta) {
979                 rcu_read_unlock();
980                 return;
981         }
982
983         psta = (void *) sta->drv_priv;
984         rs_sta = psta->rs_sta;
985
986         spin_lock_irqsave(&rs_sta->lock, flags);
987
988         rs_sta->tgg = 0;
989         switch (priv->band) {
990         case IEEE80211_BAND_2GHZ:
991                 /* TODO: this always does G, not a regression */
992                 if (priv->active_rxon.flags & RXON_FLG_TGG_PROTECT_MSK) {
993                         rs_sta->tgg = 1;
994                         rs_sta->expected_tpt = iwl3945_expected_tpt_g_prot;
995                 } else
996                         rs_sta->expected_tpt = iwl3945_expected_tpt_g;
997                 break;
998
999         case IEEE80211_BAND_5GHZ:
1000                 rs_sta->expected_tpt = iwl3945_expected_tpt_a;
1001                 break;
1002         case IEEE80211_NUM_BANDS:
1003                 BUG();
1004                 break;
1005         }
1006
1007         spin_unlock_irqrestore(&rs_sta->lock, flags);
1008
1009         rssi = priv->last_rx_rssi;
1010         if (rssi == 0)
1011                 rssi = IWL_MIN_RSSI_VAL;
1012
1013         IWL_DEBUG_RATE(priv, "Network RSSI: %d\n", rssi);
1014
1015         rs_sta->start_rate = iwl3945_get_rate_index_by_rssi(rssi, priv->band);
1016
1017         IWL_DEBUG_RATE(priv, "leave: rssi %d assign rate index: "
1018                        "%d (plcp 0x%x)\n", rssi, rs_sta->start_rate,
1019                        iwl3945_rates[rs_sta->start_rate].plcp);
1020         rcu_read_unlock();
1021 }
1022
1023 int iwl3945_rate_control_register(void)
1024 {
1025         return ieee80211_rate_control_register(&rs_ops);
1026 }
1027
1028 void iwl3945_rate_control_unregister(void)
1029 {
1030         ieee80211_rate_control_unregister(&rs_ops);
1031 }
1032
1033