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