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