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