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