mac80211: Use a cfg80211_chan_def in ieee80211_hw_conf_chan
[deliverable/linux.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>
28#include <linux/init.h>
29#include <linux/skbuff.h>
30#include <linux/slab.h>
4bc85c13
WYG
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
d4459a99 39#include "commands.h"
6bbb1370 40#include "3945.h"
4bc85c13
WYG
41
42#define RS_NAME "iwl-3945-rs"
43
2eb05816 44static s32 il3945_expected_tpt_g[RATE_COUNT_3945] = {
4bc85c13
WYG
45 7, 13, 35, 58, 0, 0, 76, 104, 130, 168, 191, 202
46};
47
2eb05816 48static s32 il3945_expected_tpt_g_prot[RATE_COUNT_3945] = {
4bc85c13
WYG
49 7, 13, 35, 58, 0, 0, 0, 80, 93, 113, 123, 125
50};
51
2eb05816 52static s32 il3945_expected_tpt_a[RATE_COUNT_3945] = {
4bc85c13
WYG
53 0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186
54};
55
2eb05816 56static s32 il3945_expected_tpt_b[RATE_COUNT_3945] = {
4bc85c13
WYG
57 7, 13, 35, 58, 0, 0, 0, 0, 0, 0, 0, 0
58};
59
e2ebc833 60struct il3945_tpt_entry {
4bc85c13 61 s8 min_rssi;
0c2c8852 62 u8 idx;
4bc85c13
WYG
63};
64
e2ebc833 65static struct il3945_tpt_entry il3945_tpt_table_a[] = {
2d09b062
SG
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}
4bc85c13
WYG
74};
75
e2ebc833 76static struct il3945_tpt_entry il3945_tpt_table_g[] = {
2d09b062
SG
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}
4bc85c13
WYG
87};
88
5d0bef90 89#define RATE_MAX_WINDOW 62
2eb05816 90#define RATE_FLUSH (3*HZ)
5d0bef90
SG
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
e2ebc833 99
e7392364
SG
100static u8
101il3945_get_rate_idx_by_rssi(s32 rssi, enum ieee80211_band band)
4bc85c13 102{
0c2c8852 103 u32 idx = 0;
4bc85c13 104 u32 table_size = 0;
e2ebc833 105 struct il3945_tpt_entry *tpt_table = NULL;
4bc85c13 106
232913b5 107 if (rssi < IL_MIN_RSSI_VAL || rssi > IL_MAX_RSSI_VAL)
e2ebc833 108 rssi = IL_MIN_RSSI_VAL;
4bc85c13
WYG
109
110 switch (band) {
111 case IEEE80211_BAND_2GHZ:
e2ebc833
SG
112 tpt_table = il3945_tpt_table_g;
113 table_size = ARRAY_SIZE(il3945_tpt_table_g);
4bc85c13 114 break;
4bc85c13 115 case IEEE80211_BAND_5GHZ:
e2ebc833
SG
116 tpt_table = il3945_tpt_table_a;
117 table_size = ARRAY_SIZE(il3945_tpt_table_a);
4bc85c13 118 break;
4bc85c13
WYG
119 default:
120 BUG();
121 break;
122 }
123
0c2c8852
SG
124 while (idx < table_size && rssi < tpt_table[idx].min_rssi)
125 idx++;
4bc85c13 126
5d0bef90 127 idx = min(idx, table_size - 1);
4bc85c13 128
0c2c8852 129 return tpt_table[idx].idx;
4bc85c13
WYG
130}
131
e7392364
SG
132static void
133il3945_clear_win(struct il3945_rate_scale_data *win)
4bc85c13 134{
6ce1dc45
SG
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;
4bc85c13
WYG
141}
142
143/**
6ce1dc45 144 * il3945_rate_scale_flush_wins - flush out the rate scale wins
4bc85c13 145 *
6ce1dc45 146 * Returns the number of wins that have gathered data but were
4bc85c13
WYG
147 * not flushed. If there were any that were not flushed, then
148 * reschedule the rate flushing routine.
149 */
e7392364
SG
150static int
151il3945_rate_scale_flush_wins(struct il3945_rs_sta *rs_sta)
4bc85c13
WYG
152{
153 int unflushed = 0;
154 int i;
155 unsigned long flags;
46bc8d4b 156 struct il_priv *il __maybe_unused = rs_sta->il;
4bc85c13
WYG
157
158 /*
159 * For each rate, if we have collected data on that rate
2eb05816 160 * and it has been more than RATE_WIN_FLUSH
ebf0d90d 161 * since we flushed, clear out the gathered stats
4bc85c13 162 */
2eb05816 163 for (i = 0; i < RATE_COUNT_3945; i++) {
4bc85c13
WYG
164 if (!rs_sta->win[i].counter)
165 continue;
166
167 spin_lock_irqsave(&rs_sta->lock, flags);
e7392364
SG
168 if (time_after(jiffies, rs_sta->win[i].stamp + RATE_WIN_FLUSH)) {
169 D_RATE("flushing %d samples of rate " "idx %d\n",
170 rs_sta->win[i].counter, i);
6ce1dc45 171 il3945_clear_win(&rs_sta->win[i]);
4bc85c13
WYG
172 } else
173 unflushed++;
174 spin_unlock_irqrestore(&rs_sta->lock, flags);
175 }
176
177 return unflushed;
178}
179
2eb05816
SG
180#define RATE_FLUSH_MAX 5000 /* msec */
181#define RATE_FLUSH_MIN 50 /* msec */
e2ebc833 182#define IL_AVERAGE_PACKETS 1500
4bc85c13 183
e7392364
SG
184static void
185il3945_bg_rate_scale_flush(unsigned long data)
4bc85c13 186{
e2ebc833 187 struct il3945_rs_sta *rs_sta = (void *)data;
46bc8d4b 188 struct il_priv *il __maybe_unused = rs_sta->il;
4bc85c13
WYG
189 int unflushed = 0;
190 unsigned long flags;
191 u32 packet_count, duration, pps;
192
58de00a4 193 D_RATE("enter\n");
4bc85c13 194
6ce1dc45 195 unflushed = il3945_rate_scale_flush_wins(rs_sta);
4bc85c13
WYG
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
e7392364 208 D_RATE("Tx'd %d packets in %dms\n", packet_count, duration);
4bc85c13
WYG
209
210 /* Determine packets per second */
211 if (duration)
212 pps = (packet_count * 1000) / duration;
213 else
214 pps = 0;
215
216 if (pps) {
e2ebc833 217 duration = (IL_AVERAGE_PACKETS * 1000) / pps;
2eb05816
SG
218 if (duration < RATE_FLUSH_MIN)
219 duration = RATE_FLUSH_MIN;
220 else if (duration > RATE_FLUSH_MAX)
221 duration = RATE_FLUSH_MAX;
4bc85c13 222 } else
2eb05816 223 duration = RATE_FLUSH_MAX;
4bc85c13
WYG
224
225 rs_sta->flush_time = msecs_to_jiffies(duration);
226
e7392364
SG
227 D_RATE("new flush period: %d msec ave %d\n", duration,
228 packet_count);
4bc85c13 229
e7392364
SG
230 mod_timer(&rs_sta->rate_scale_flush,
231 jiffies + rs_sta->flush_time);
4bc85c13
WYG
232
233 rs_sta->last_partial_flush = jiffies;
234 } else {
2eb05816 235 rs_sta->flush_time = RATE_FLUSH;
4bc85c13
WYG
236 rs_sta->flush_pending = 0;
237 }
238 /* If there weren't any unflushed entries, we don't schedule the timer
239 * to run again */
240
241 rs_sta->last_flush = jiffies;
242
243 spin_unlock_irqrestore(&rs_sta->lock, flags);
244
58de00a4 245 D_RATE("leave\n");
4bc85c13
WYG
246}
247
248/**
6ce1dc45 249 * il3945_collect_tx_data - Update the success/failure sliding win
4bc85c13 250 *
6ce1dc45
SG
251 * We keep a sliding win of the last 64 packets transmitted
252 * at this rate. win->data contains the bitmask of successful
4bc85c13
WYG
253 * packets.
254 */
e7392364
SG
255static void
256il3945_collect_tx_data(struct il3945_rs_sta *rs_sta,
257 struct il3945_rate_scale_data *win, int success,
258 int retries, int idx)
4bc85c13
WYG
259{
260 unsigned long flags;
261 s32 fail_count;
46bc8d4b 262 struct il_priv *il __maybe_unused = rs_sta->il;
4bc85c13
WYG
263
264 if (!retries) {
58de00a4 265 D_RATE("leave: retries == 0 -- should be at least 1\n");
4bc85c13
WYG
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
6ce1dc45
SG
273 * history win; anything older isn't really relevant any more.
274 * If we have filled up the sliding win, drop the oldest attempt;
4bc85c13
WYG
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) {
2eb05816 280 if (win->counter >= RATE_MAX_WINDOW) {
4bc85c13
WYG
281
282 /* remove earliest */
2eb05816 283 win->counter = RATE_MAX_WINDOW - 1;
4bc85c13 284
2eb05816
SG
285 if (win->data & (1ULL << (RATE_MAX_WINDOW - 1))) {
286 win->data &= ~(1ULL << (RATE_MAX_WINDOW - 1));
6ce1dc45 287 win->success_counter--;
4bc85c13
WYG
288 }
289 }
290
291 /* Increment frames-attempted counter */
6ce1dc45 292 win->counter++;
4bc85c13
WYG
293
294 /* Shift bitmap by one frame (throw away oldest history),
295 * OR in "1", and increment "success" if this
296 * frame was successful. */
6ce1dc45 297 win->data <<= 1;
4bc85c13 298 if (success > 0) {
6ce1dc45
SG
299 win->success_counter++;
300 win->data |= 0x1;
4bc85c13
WYG
301 success--;
302 }
303
304 retries--;
305 }
306
307 /* Calculate current success ratio, avoid divide-by-0! */
6ce1dc45 308 if (win->counter > 0)
e7392364
SG
309 win->success_ratio =
310 128 * (100 * win->success_counter) / win->counter;
4bc85c13 311 else
6ce1dc45 312 win->success_ratio = IL_INVALID_VALUE;
4bc85c13 313
6ce1dc45 314 fail_count = win->counter - win->success_counter;
4bc85c13
WYG
315
316 /* Calculate average throughput, if we have enough history. */
2eb05816
SG
317 if (fail_count >= RATE_MIN_FAILURE_TH ||
318 win->success_counter >= RATE_MIN_SUCCESS_TH)
e7392364
SG
319 win->average_tpt =
320 ((win->success_ratio * rs_sta->expected_tpt[idx] +
321 64) / 128);
4bc85c13 322 else
6ce1dc45 323 win->average_tpt = IL_INVALID_VALUE;
4bc85c13 324
6ce1dc45
SG
325 /* Tag this win as having been updated */
326 win->stamp = jiffies;
4bc85c13
WYG
327
328 spin_unlock_irqrestore(&rs_sta->lock, flags);
4bc85c13
WYG
329}
330
331/*
332 * Called after adding a new station to initialize rate scaling
333 */
e7392364
SG
334void
335il3945_rs_rate_init(struct il_priv *il, struct ieee80211_sta *sta, u8 sta_id)
4bc85c13 336{
46bc8d4b
SG
337 struct ieee80211_hw *hw = il->hw;
338 struct ieee80211_conf *conf = &il->hw->conf;
e2ebc833
SG
339 struct il3945_sta_priv *psta;
340 struct il3945_rs_sta *rs_sta;
4bc85c13
WYG
341 struct ieee80211_supported_band *sband;
342 int i;
343
58de00a4 344 D_INFO("enter\n");
b16db50a 345 if (sta_id == il->hw_params.bcast_id)
4bc85c13
WYG
346 goto out;
347
e7392364 348 psta = (struct il3945_sta_priv *)sta->drv_priv;
4bc85c13 349 rs_sta = &psta->rs_sta;
675a0b04 350 sband = hw->wiphy->bands[conf->chandef.chan->band];
4bc85c13 351
46bc8d4b 352 rs_sta->il = il;
4bc85c13 353
2eb05816 354 rs_sta->start_rate = RATE_INVALID;
4bc85c13
WYG
355
356 /* default to just 802.11b */
e2ebc833 357 rs_sta->expected_tpt = il3945_expected_tpt_b;
4bc85c13
WYG
358
359 rs_sta->last_partial_flush = jiffies;
360 rs_sta->last_flush = jiffies;
2eb05816 361 rs_sta->flush_time = RATE_FLUSH;
4bc85c13
WYG
362 rs_sta->last_tx_packets = 0;
363
364 rs_sta->rate_scale_flush.data = (unsigned long)rs_sta;
e2ebc833 365 rs_sta->rate_scale_flush.function = il3945_bg_rate_scale_flush;
4bc85c13 366
2eb05816 367 for (i = 0; i < RATE_COUNT_3945; i++)
6ce1dc45 368 il3945_clear_win(&rs_sta->win[i]);
4bc85c13
WYG
369
370 /* TODO: what is a good starting rate for STA? About middle? Maybe not
371 * the lowest or the highest rate.. Could consider using RSSI from
372 * previous packets? Need to have IEEE 802.1X auth succeed immediately
373 * after assoc.. */
374
375 for (i = sband->n_bitrates - 1; i >= 0; i--) {
376 if (sta->supp_rates[sband->band] & (1 << i)) {
377 rs_sta->last_txrate_idx = i;
378 break;
379 }
380 }
381
46bc8d4b 382 il->_3945.sta_supp_rates = sta->supp_rates[sband->band];
e2ebc833 383 /* For 5 GHz band it start at IL_FIRST_OFDM_RATE */
4bc85c13 384 if (sband->band == IEEE80211_BAND_5GHZ) {
e2ebc833 385 rs_sta->last_txrate_idx += IL_FIRST_OFDM_RATE;
5d0bef90 386 il->_3945.sta_supp_rates <<= IL_FIRST_OFDM_RATE;
4bc85c13
WYG
387 }
388
389out:
46bc8d4b 390 il->stations[sta_id].used &= ~IL_STA_UCODE_INPROGRESS;
4bc85c13 391
58de00a4 392 D_INFO("leave\n");
4bc85c13
WYG
393}
394
e7392364
SG
395static void *
396il3945_rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
4bc85c13
WYG
397{
398 return hw->priv;
399}
400
401/* rate scale requires free function to be implemented */
e7392364
SG
402static void
403il3945_rs_free(void *il)
4bc85c13 404{
4bc85c13
WYG
405}
406
e7392364
SG
407static void *
408il3945_rs_alloc_sta(void *il_priv, struct ieee80211_sta *sta, gfp_t gfp)
4bc85c13 409{
e2ebc833 410 struct il3945_rs_sta *rs_sta;
e7392364 411 struct il3945_sta_priv *psta = (void *)sta->drv_priv;
46bc8d4b 412 struct il_priv *il __maybe_unused = il_priv;
4bc85c13 413
58de00a4 414 D_RATE("enter\n");
4bc85c13
WYG
415
416 rs_sta = &psta->rs_sta;
417
418 spin_lock_init(&rs_sta->lock);
419 init_timer(&rs_sta->rate_scale_flush);
420
58de00a4 421 D_RATE("leave\n");
4bc85c13
WYG
422
423 return rs_sta;
424}
425
e7392364
SG
426static void
427il3945_rs_free_sta(void *il_priv, struct ieee80211_sta *sta, void *il_sta)
4bc85c13 428{
46bc8d4b 429 struct il3945_rs_sta *rs_sta = il_sta;
4bc85c13
WYG
430
431 /*
e2ebc833
SG
432 * Be careful not to use any members of il3945_rs_sta (like trying
433 * to use il_priv to print out debugging) since it may not be fully
4bc85c13
WYG
434 * initialized at this point.
435 */
436 del_timer_sync(&rs_sta->rate_scale_flush);
437}
438
4bc85c13 439/**
e2ebc833 440 * il3945_rs_tx_status - Update rate control values based on Tx results
4bc85c13 441 *
e2ebc833 442 * NOTE: Uses il_priv->retry_rate for the # of retries attempted by
4bc85c13
WYG
443 * the hardware for each rate.
444 */
e7392364
SG
445static void
446il3945_rs_tx_status(void *il_rate, struct ieee80211_supported_band *sband,
447 struct ieee80211_sta *sta, void *il_sta,
448 struct sk_buff *skb)
4bc85c13
WYG
449{
450 s8 retries = 0, current_count;
0c2c8852 451 int scale_rate_idx, first_idx, last_idx;
4bc85c13 452 unsigned long flags;
46bc8d4b
SG
453 struct il_priv *il = (struct il_priv *)il_rate;
454 struct il3945_rs_sta *rs_sta = il_sta;
4bc85c13
WYG
455 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
456
58de00a4 457 D_RATE("enter\n");
4bc85c13
WYG
458
459 retries = info->status.rates[0].count;
460 /* Sanity Check for retries */
2eb05816
SG
461 if (retries > RATE_RETRY_TH)
462 retries = RATE_RETRY_TH;
4bc85c13 463
0c2c8852
SG
464 first_idx = sband->bitrates[info->status.rates[0].idx].hw_value;
465 if (first_idx < 0 || first_idx >= RATE_COUNT_3945) {
466 D_RATE("leave: Rate out of bounds: %d\n", first_idx);
4bc85c13
WYG
467 return;
468 }
469
46bc8d4b 470 if (!il_sta) {
58de00a4 471 D_RATE("leave: No STA il data to update!\n");
4bc85c13
WYG
472 return;
473 }
474
475 /* Treat uninitialized rate scaling data same as non-existing. */
46bc8d4b 476 if (!rs_sta->il) {
58de00a4 477 D_RATE("leave: STA il data uninitialized!\n");
4bc85c13
WYG
478 return;
479 }
480
4bc85c13
WYG
481 rs_sta->tx_packets++;
482
0c2c8852
SG
483 scale_rate_idx = first_idx;
484 last_idx = first_idx;
4bc85c13
WYG
485
486 /*
6ce1dc45 487 * Update the win for each rate. We determine which rates
4bc85c13
WYG
488 * were Tx'd based on the total number of retries vs. the number
489 * of retries configured for each rate -- currently set to the
46bc8d4b 490 * il value 'retry_rate' vs. rate specific
4bc85c13 491 *
0c2c8852 492 * On exit from this while loop last_idx indicates the rate
4bc85c13
WYG
493 * at which the frame was finally transmitted (or failed if no
494 * ACK)
495 */
496 while (retries > 1) {
46bc8d4b 497 if ((retries - 1) < il->retry_rate) {
4bc85c13 498 current_count = (retries - 1);
0c2c8852 499 last_idx = scale_rate_idx;
4bc85c13 500 } else {
46bc8d4b 501 current_count = il->retry_rate;
e7392364 502 last_idx = il3945_rs_next_rate(il, scale_rate_idx);
4bc85c13
WYG
503 }
504
505 /* Update this rate accounting for as many retries
506 * as was used for it (per current_count) */
e7392364
SG
507 il3945_collect_tx_data(rs_sta, &rs_sta->win[scale_rate_idx], 0,
508 current_count, scale_rate_idx);
509 D_RATE("Update rate %d for %d retries.\n", scale_rate_idx,
510 current_count);
4bc85c13
WYG
511
512 retries -= current_count;
513
0c2c8852 514 scale_rate_idx = last_idx;
4bc85c13
WYG
515 }
516
0c2c8852 517 /* Update the last idx win with success/failure based on ACK */
e7392364
SG
518 D_RATE("Update rate %d with %s.\n", last_idx,
519 (info->flags & IEEE80211_TX_STAT_ACK) ? "success" : "failure");
520 il3945_collect_tx_data(rs_sta, &rs_sta->win[last_idx],
521 info->flags & IEEE80211_TX_STAT_ACK, 1,
522 last_idx);
4bc85c13 523
6ce1dc45 524 /* We updated the rate scale win -- if its been more than
4bc85c13
WYG
525 * flush_time since the last run, schedule the flush
526 * again */
527 spin_lock_irqsave(&rs_sta->lock, flags);
528
529 if (!rs_sta->flush_pending &&
e7392364 530 time_after(jiffies, rs_sta->last_flush + rs_sta->flush_time)) {
4bc85c13
WYG
531
532 rs_sta->last_partial_flush = jiffies;
533 rs_sta->flush_pending = 1;
534 mod_timer(&rs_sta->rate_scale_flush,
535 jiffies + rs_sta->flush_time);
536 }
537
538 spin_unlock_irqrestore(&rs_sta->lock, flags);
539
58de00a4 540 D_RATE("leave\n");
4bc85c13
WYG
541}
542
e7392364
SG
543static u16
544il3945_get_adjacent_rate(struct il3945_rs_sta *rs_sta, u8 idx, u16 rate_mask,
545 enum ieee80211_band band)
4bc85c13 546{
2eb05816
SG
547 u8 high = RATE_INVALID;
548 u8 low = RATE_INVALID;
46bc8d4b 549 struct il_priv *il __maybe_unused = rs_sta->il;
4bc85c13
WYG
550
551 /* 802.11A walks to the next literal adjacent rate in
552 * the rate table */
553 if (unlikely(band == IEEE80211_BAND_5GHZ)) {
554 int i;
555 u32 mask;
556
557 /* Find the previous rate that is in the rate mask */
0c2c8852 558 i = idx - 1;
4bc85c13
WYG
559 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
560 if (rate_mask & mask) {
561 low = i;
562 break;
563 }
564 }
565
566 /* Find the next rate that is in the rate mask */
0c2c8852 567 i = idx + 1;
e7392364 568 for (mask = (1 << i); i < RATE_COUNT_3945; i++, mask <<= 1) {
4bc85c13
WYG
569 if (rate_mask & mask) {
570 high = i;
571 break;
572 }
573 }
574
575 return (high << 8) | low;
576 }
577
0c2c8852 578 low = idx;
2eb05816 579 while (low != RATE_INVALID) {
4bc85c13 580 if (rs_sta->tgg)
e2ebc833 581 low = il3945_rates[low].prev_rs_tgg;
4bc85c13 582 else
e2ebc833 583 low = il3945_rates[low].prev_rs;
2eb05816 584 if (low == RATE_INVALID)
4bc85c13
WYG
585 break;
586 if (rate_mask & (1 << low))
587 break;
58de00a4 588 D_RATE("Skipping masked lower rate: %d\n", low);
4bc85c13
WYG
589 }
590
0c2c8852 591 high = idx;
2eb05816 592 while (high != RATE_INVALID) {
4bc85c13 593 if (rs_sta->tgg)
e2ebc833 594 high = il3945_rates[high].next_rs_tgg;
4bc85c13 595 else
e2ebc833 596 high = il3945_rates[high].next_rs;
2eb05816 597 if (high == RATE_INVALID)
4bc85c13
WYG
598 break;
599 if (rate_mask & (1 << high))
600 break;
58de00a4 601 D_RATE("Skipping masked higher rate: %d\n", high);
4bc85c13
WYG
602 }
603
604 return (high << 8) | low;
605}
606
607/**
e2ebc833 608 * il3945_rs_get_rate - find the rate for the requested packet
4bc85c13
WYG
609 *
610 * Returns the ieee80211_rate structure allocated by the driver.
611 *
612 * The rate control algorithm has no internal mapping between hw_mode's
613 * rate ordering and the rate ordering used by the rate control algorithm.
614 *
615 * The rate control algorithm uses a single table of rates that goes across
616 * the entire A/B/G spectrum vs. being limited to just one particular
617 * hw_mode.
618 *
0c2c8852 619 * As such, we can't convert the idx obtained below into the hw_mode's
4bc85c13
WYG
620 * rate table and must reference the driver allocated rate table
621 *
622 */
e7392364
SG
623static void
624il3945_rs_get_rate(void *il_r, struct ieee80211_sta *sta, void *il_sta,
625 struct ieee80211_tx_rate_control *txrc)
4bc85c13
WYG
626{
627 struct ieee80211_supported_band *sband = txrc->sband;
628 struct sk_buff *skb = txrc->skb;
2eb05816
SG
629 u8 low = RATE_INVALID;
630 u8 high = RATE_INVALID;
4bc85c13 631 u16 high_low;
0c2c8852 632 int idx;
46bc8d4b 633 struct il3945_rs_sta *rs_sta = il_sta;
6ce1dc45 634 struct il3945_rate_scale_data *win = NULL;
e2ebc833
SG
635 int current_tpt = IL_INVALID_VALUE;
636 int low_tpt = IL_INVALID_VALUE;
637 int high_tpt = IL_INVALID_VALUE;
4bc85c13
WYG
638 u32 fail_count;
639 s8 scale_action = 0;
640 unsigned long flags;
c3371d64 641 u16 rate_mask;
4bc85c13 642 s8 max_rate_idx = -1;
46bc8d4b 643 struct il_priv *il __maybe_unused = (struct il_priv *)il_r;
4bc85c13
WYG
644 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
645
58de00a4 646 D_RATE("enter\n");
4bc85c13
WYG
647
648 /* Treat uninitialized rate scaling data same as non-existing. */
46bc8d4b 649 if (rs_sta && !rs_sta->il) {
58de00a4 650 D_RATE("Rate scaling information not initialized yet.\n");
46bc8d4b 651 il_sta = NULL;
4bc85c13
WYG
652 }
653
46bc8d4b 654 if (rate_control_send_low(sta, il_sta, txrc))
4bc85c13
WYG
655 return;
656
657 rate_mask = sta->supp_rates[sband->band];
658
659 /* get user max rate if set */
660 max_rate_idx = txrc->max_rate_idx;
232913b5 661 if (sband->band == IEEE80211_BAND_5GHZ && max_rate_idx != -1)
e2ebc833 662 max_rate_idx += IL_FIRST_OFDM_RATE;
2eb05816 663 if (max_rate_idx < 0 || max_rate_idx >= RATE_COUNT)
4bc85c13
WYG
664 max_rate_idx = -1;
665
0c2c8852 666 idx = min(rs_sta->last_txrate_idx & 0xffff, RATE_COUNT_3945 - 1);
4bc85c13
WYG
667
668 if (sband->band == IEEE80211_BAND_5GHZ)
e2ebc833 669 rate_mask = rate_mask << IL_FIRST_OFDM_RATE;
4bc85c13
WYG
670
671 spin_lock_irqsave(&rs_sta->lock, flags);
672
673 /* for recent assoc, choose best rate regarding
674 * to rssi value
675 */
2eb05816 676 if (rs_sta->start_rate != RATE_INVALID) {
0c2c8852 677 if (rs_sta->start_rate < idx &&
e7392364 678 (rate_mask & (1 << rs_sta->start_rate)))
0c2c8852 679 idx = rs_sta->start_rate;
2eb05816 680 rs_sta->start_rate = RATE_INVALID;
4bc85c13
WYG
681 }
682
683 /* force user max rate if set by user */
0c2c8852 684 if (max_rate_idx != -1 && max_rate_idx < idx) {
4bc85c13 685 if (rate_mask & (1 << max_rate_idx))
0c2c8852 686 idx = max_rate_idx;
4bc85c13
WYG
687 }
688
0c2c8852 689 win = &(rs_sta->win[idx]);
4bc85c13 690
6ce1dc45 691 fail_count = win->counter - win->success_counter;
4bc85c13 692
2eb05816
SG
693 if (fail_count < RATE_MIN_FAILURE_TH &&
694 win->success_counter < RATE_MIN_SUCCESS_TH) {
4bc85c13
WYG
695 spin_unlock_irqrestore(&rs_sta->lock, flags);
696
58de00a4 697 D_RATE("Invalid average_tpt on rate %d: "
e7392364
SG
698 "counter: %d, success_counter: %d, "
699 "expected_tpt is %sNULL\n", idx, win->counter,
700 win->success_counter,
701 rs_sta->expected_tpt ? "not " : "");
702
703 /* Can't calculate this yet; not enough history */
6ce1dc45 704 win->average_tpt = IL_INVALID_VALUE;
4bc85c13
WYG
705 goto out;
706
707 }
708
6ce1dc45 709 current_tpt = win->average_tpt;
4bc85c13 710
e7392364
SG
711 high_low =
712 il3945_get_adjacent_rate(rs_sta, idx, rate_mask, sband->band);
4bc85c13
WYG
713 low = high_low & 0xff;
714 high = (high_low >> 8) & 0xff;
715
716 /* If user set max rate, dont allow higher than user constrain */
232913b5 717 if (max_rate_idx != -1 && max_rate_idx < high)
2eb05816 718 high = RATE_INVALID;
4bc85c13
WYG
719
720 /* Collect Measured throughputs of adjacent rates */
2eb05816 721 if (low != RATE_INVALID)
4bc85c13
WYG
722 low_tpt = rs_sta->win[low].average_tpt;
723
2eb05816 724 if (high != RATE_INVALID)
4bc85c13
WYG
725 high_tpt = rs_sta->win[high].average_tpt;
726
727 spin_unlock_irqrestore(&rs_sta->lock, flags);
728
729 scale_action = 0;
730
731 /* Low success ratio , need to drop the rate */
2eb05816 732 if (win->success_ratio < RATE_DECREASE_TH || !current_tpt) {
58de00a4 733 D_RATE("decrease rate because of low success_ratio\n");
4bc85c13 734 scale_action = -1;
e7392364
SG
735 /* No throughput measured yet for adjacent rates,
736 * try increase */
737 } else if (low_tpt == IL_INVALID_VALUE && high_tpt == IL_INVALID_VALUE) {
4bc85c13 738
e7392364
SG
739 if (high != RATE_INVALID &&
740 win->success_ratio >= RATE_INCREASE_TH)
4bc85c13 741 scale_action = 1;
2eb05816 742 else if (low != RATE_INVALID)
4bc85c13
WYG
743 scale_action = 0;
744
e7392364
SG
745 /* Both adjacent throughputs are measured, but neither one has
746 * better throughput; we're using the best rate, don't change
747 * it! */
748 } else if (low_tpt != IL_INVALID_VALUE && high_tpt != IL_INVALID_VALUE
749 && low_tpt < current_tpt && high_tpt < current_tpt) {
4bc85c13 750
58de00a4 751 D_RATE("No action -- low [%d] & high [%d] < "
e7392364 752 "current_tpt [%d]\n", low_tpt, high_tpt, current_tpt);
4bc85c13
WYG
753 scale_action = 0;
754
e7392364 755 /* At least one of the rates has better throughput */
4bc85c13 756 } else {
e2ebc833 757 if (high_tpt != IL_INVALID_VALUE) {
4bc85c13
WYG
758
759 /* High rate has better throughput, Increase
760 * rate */
761 if (high_tpt > current_tpt &&
e7392364 762 win->success_ratio >= RATE_INCREASE_TH)
4bc85c13
WYG
763 scale_action = 1;
764 else {
e7392364 765 D_RATE("decrease rate because of high tpt\n");
4bc85c13
WYG
766 scale_action = 0;
767 }
e2ebc833 768 } else if (low_tpt != IL_INVALID_VALUE) {
4bc85c13 769 if (low_tpt > current_tpt) {
e7392364 770 D_RATE("decrease rate because of low tpt\n");
4bc85c13 771 scale_action = -1;
2eb05816 772 } else if (win->success_ratio >= RATE_INCREASE_TH) {
4bc85c13
WYG
773 /* Lower rate has better
774 * throughput,decrease rate */
775 scale_action = 1;
776 }
777 }
778 }
779
780 /* Sanity check; asked for decrease, but success rate or throughput
781 * has been good at old rate. Don't change it. */
2eb05816
SG
782 if (scale_action == -1 && low != RATE_INVALID &&
783 (win->success_ratio > RATE_HIGH_TH ||
232913b5 784 current_tpt > 100 * rs_sta->expected_tpt[low]))
4bc85c13
WYG
785 scale_action = 0;
786
787 switch (scale_action) {
788 case -1:
4bc85c13 789 /* Decrese rate */
2eb05816 790 if (low != RATE_INVALID)
0c2c8852 791 idx = low;
4bc85c13 792 break;
4bc85c13
WYG
793 case 1:
794 /* Increase rate */
2eb05816 795 if (high != RATE_INVALID)
0c2c8852 796 idx = high;
4bc85c13
WYG
797
798 break;
4bc85c13
WYG
799 case 0:
800 default:
801 /* No change */
802 break;
803 }
804
e7392364
SG
805 D_RATE("Selected %d (action %d) - low %d high %d\n", idx, scale_action,
806 low, high);
4bc85c13 807
e7392364 808out:
4bc85c13 809
7c251012 810 if (sband->band == IEEE80211_BAND_5GHZ) {
0c2c8852
SG
811 if (WARN_ON_ONCE(idx < IL_FIRST_OFDM_RATE))
812 idx = IL_FIRST_OFDM_RATE;
813 rs_sta->last_txrate_idx = idx;
814 info->control.rates[0].idx = idx - IL_FIRST_OFDM_RATE;
7c251012 815 } else {
0c2c8852 816 rs_sta->last_txrate_idx = idx;
4bc85c13 817 info->control.rates[0].idx = rs_sta->last_txrate_idx;
7c251012 818 }
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,
889 struct ieee80211_sta *sta, void *il_sta)
4bc85c13
WYG
890{
891}
892
893static struct rate_control_ops rs_ops = {
894 .module = NULL,
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}
This page took 0.498573 seconds and 5 git commands to generate.