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