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