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