blob: ea7f459e961ba574a010f2a03d7f3e221bb94ec4 [file] [log] [blame]
Zhu Yib481de92007-09-25 17:54:57 -07001/******************************************************************************
2 *
3 * Copyright(c) 2005 - 2007 Intel Corporation. All rights reserved.
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>
32#include <net/ieee80211.h>
33
34#include <linux/netdevice.h>
35#include <linux/etherdevice.h>
36#include <linux/delay.h>
37
38#include <linux/workqueue.h>
39
Zhu Yi1156b2c2007-09-25 19:34:09 -070040#define IWL 3945
41
Zhu Yib481de92007-09-25 17:54:57 -070042#include "../net/mac80211/ieee80211_rate.h"
43
44#include "iwlwifi.h"
45
46#define RS_NAME "iwl-3945-rs"
47
48struct iwl_rate_scale_data {
49 u64 data;
50 s32 success_counter;
51 s32 success_ratio;
52 s32 counter;
53 s32 average_tpt;
54 unsigned long stamp;
55};
56
57struct iwl_rate_scale_priv {
58 spinlock_t lock;
59 s32 *expected_tpt;
60 unsigned long last_partial_flush;
61 unsigned long last_flush;
62 u32 flush_time;
63 u32 last_tx_packets;
64 u32 tx_packets;
65 u8 tgg;
66 u8 flush_pending;
67 u8 start_rate;
68 u8 ibss_sta_added;
69 struct timer_list rate_scale_flush;
70 struct iwl_rate_scale_data win[IWL_RATE_COUNT];
71};
72
73static s32 iwl_expected_tpt_g[IWL_RATE_COUNT] = {
Mohamed Abbas14577f22007-11-12 11:37:42 +080074 7, 13, 35, 58, 0, 0, 76, 104, 130, 168, 191, 202
Zhu Yib481de92007-09-25 17:54:57 -070075};
76
77static s32 iwl_expected_tpt_g_prot[IWL_RATE_COUNT] = {
Mohamed Abbas14577f22007-11-12 11:37:42 +080078 7, 13, 35, 58, 0, 0, 0, 80, 93, 113, 123, 125
Zhu Yib481de92007-09-25 17:54:57 -070079};
80
81static s32 iwl_expected_tpt_a[IWL_RATE_COUNT] = {
Mohamed Abbas14577f22007-11-12 11:37:42 +080082 0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186
Zhu Yib481de92007-09-25 17:54:57 -070083};
84
85static s32 iwl_expected_tpt_b[IWL_RATE_COUNT] = {
Mohamed Abbas14577f22007-11-12 11:37:42 +080086 7, 13, 35, 58, 0, 0, 0, 0, 0, 0, 0, 0
Zhu Yib481de92007-09-25 17:54:57 -070087};
88
89struct iwl_tpt_entry {
90 s8 min_rssi;
91 u8 index;
92};
93
94static struct iwl_tpt_entry iwl_tpt_table_a[] = {
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
105static struct iwl_tpt_entry iwl_tpt_table_b[] = {
106 {-86, IWL_RATE_11M_INDEX},
107 {-88, IWL_RATE_5M_INDEX},
108 {-90, IWL_RATE_2M_INDEX},
109 {-92, IWL_RATE_1M_INDEX}
110
111};
112
113static struct iwl_tpt_entry iwl_tpt_table_g[] = {
114 {-60, IWL_RATE_54M_INDEX},
115 {-64, IWL_RATE_48M_INDEX},
116 {-68, IWL_RATE_36M_INDEX},
117 {-80, IWL_RATE_24M_INDEX},
118 {-84, IWL_RATE_18M_INDEX},
119 {-85, IWL_RATE_12M_INDEX},
120 {-86, IWL_RATE_11M_INDEX},
121 {-88, IWL_RATE_5M_INDEX},
122 {-90, IWL_RATE_2M_INDEX},
123 {-92, IWL_RATE_1M_INDEX}
124};
125
126#define IWL_RATE_MAX_WINDOW 62
127#define IWL_RATE_FLUSH (3*HZ/10)
128#define IWL_RATE_WIN_FLUSH (HZ/2)
129#define IWL_RATE_HIGH_TH 11520
130#define IWL_RATE_MIN_FAILURE_TH 8
131#define IWL_RATE_MIN_SUCCESS_TH 8
132#define IWL_RATE_DECREASE_TH 1920
133
134static u8 iwl_get_rate_index_by_rssi(s32 rssi, u8 mode)
135{
136 u32 index = 0;
137 u32 table_size = 0;
138 struct iwl_tpt_entry *tpt_table = NULL;
139
140 if ((rssi < IWL_MIN_RSSI_VAL) || (rssi > IWL_MAX_RSSI_VAL))
141 rssi = IWL_MIN_RSSI_VAL;
142
143 switch (mode) {
144 case MODE_IEEE80211G:
145 tpt_table = iwl_tpt_table_g;
146 table_size = ARRAY_SIZE(iwl_tpt_table_g);
147 break;
148
149 case MODE_IEEE80211A:
150 tpt_table = iwl_tpt_table_a;
151 table_size = ARRAY_SIZE(iwl_tpt_table_a);
152 break;
153
154 default:
155 case MODE_IEEE80211B:
156 tpt_table = iwl_tpt_table_b;
157 table_size = ARRAY_SIZE(iwl_tpt_table_b);
158 break;
159 }
160
161 while ((index < table_size) && (rssi < tpt_table[index].min_rssi))
162 index++;
163
164 index = min(index, (table_size - 1));
165
166 return tpt_table[index].index;
167}
168
169static void iwl_clear_window(struct iwl_rate_scale_data *window)
170{
171 window->data = 0;
172 window->success_counter = 0;
173 window->success_ratio = IWL_INVALID_VALUE;
174 window->counter = 0;
175 window->average_tpt = IWL_INVALID_VALUE;
176 window->stamp = 0;
177}
178
179/**
180 * iwl_rate_scale_flush_windows - flush out the rate scale windows
181 *
182 * Returns the number of windows that have gathered data but were
183 * not flushed. If there were any that were not flushed, then
184 * reschedule the rate flushing routine.
185 */
186static int iwl_rate_scale_flush_windows(struct iwl_rate_scale_priv *rs_priv)
187{
188 int unflushed = 0;
189 int i;
190 unsigned long flags;
191
192 /*
193 * For each rate, if we have collected data on that rate
194 * and it has been more than IWL_RATE_WIN_FLUSH
195 * since we flushed, clear out the gathered statistics
196 */
197 for (i = 0; i < IWL_RATE_COUNT; i++) {
198 if (!rs_priv->win[i].counter)
199 continue;
200
201 spin_lock_irqsave(&rs_priv->lock, flags);
202 if (time_after(jiffies, rs_priv->win[i].stamp +
203 IWL_RATE_WIN_FLUSH)) {
204 IWL_DEBUG_RATE("flushing %d samples of rate "
205 "index %d\n",
206 rs_priv->win[i].counter, i);
207 iwl_clear_window(&rs_priv->win[i]);
208 } else
209 unflushed++;
210 spin_unlock_irqrestore(&rs_priv->lock, flags);
211 }
212
213 return unflushed;
214}
215
216#define IWL_RATE_FLUSH_MAX 5000 /* msec */
217#define IWL_RATE_FLUSH_MIN 50 /* msec */
218
219static void iwl_bg_rate_scale_flush(unsigned long data)
220{
221 struct iwl_rate_scale_priv *rs_priv = (void *)data;
222 int unflushed = 0;
223 unsigned long flags;
224 u32 packet_count, duration, pps;
225
226 IWL_DEBUG_RATE("enter\n");
227
228 unflushed = iwl_rate_scale_flush_windows(rs_priv);
229
230 spin_lock_irqsave(&rs_priv->lock, flags);
231
232 rs_priv->flush_pending = 0;
233
234 /* Number of packets Rx'd since last time this timer ran */
235 packet_count = (rs_priv->tx_packets - rs_priv->last_tx_packets) + 1;
236
237 rs_priv->last_tx_packets = rs_priv->tx_packets + 1;
238
239 if (unflushed) {
240 duration =
241 jiffies_to_msecs(jiffies - rs_priv->last_partial_flush);
242/* duration = jiffies_to_msecs(rs_priv->flush_time); */
243
244 IWL_DEBUG_RATE("Tx'd %d packets in %dms\n",
245 packet_count, duration);
246
247 /* Determine packets per second */
248 if (duration)
249 pps = (packet_count * 1000) / duration;
250 else
251 pps = 0;
252
253 if (pps) {
254 duration = IWL_RATE_FLUSH_MAX / pps;
255 if (duration < IWL_RATE_FLUSH_MIN)
256 duration = IWL_RATE_FLUSH_MIN;
257 } else
258 duration = IWL_RATE_FLUSH_MAX;
259
260 rs_priv->flush_time = msecs_to_jiffies(duration);
261
262 IWL_DEBUG_RATE("new flush period: %d msec ave %d\n",
263 duration, packet_count);
264
265 mod_timer(&rs_priv->rate_scale_flush, jiffies +
266 rs_priv->flush_time);
267
268 rs_priv->last_partial_flush = jiffies;
269 }
270
271 /* If there weren't any unflushed entries, we don't schedule the timer
272 * to run again */
273
274 rs_priv->last_flush = jiffies;
275
276 spin_unlock_irqrestore(&rs_priv->lock, flags);
277
278 IWL_DEBUG_RATE("leave\n");
279}
280
281/**
282 * iwl_collect_tx_data - Update the success/failure sliding window
283 *
284 * We keep a sliding window of the last 64 packets transmitted
285 * at this rate. window->data contains the bitmask of successful
286 * packets.
287 */
288static void iwl_collect_tx_data(struct iwl_rate_scale_priv *rs_priv,
289 struct iwl_rate_scale_data *window,
290 int success, int retries)
291{
292 unsigned long flags;
293
294 if (!retries) {
295 IWL_DEBUG_RATE("leave: retries == 0 -- should be at least 1\n");
296 return;
297 }
298
299 while (retries--) {
300 spin_lock_irqsave(&rs_priv->lock, flags);
301
302 /* If we have filled up the window then subtract one from the
303 * success counter if the high-bit is counting toward
304 * success */
305 if (window->counter == IWL_RATE_MAX_WINDOW) {
306 if (window->data & (1ULL << (IWL_RATE_MAX_WINDOW - 1)))
307 window->success_counter--;
308 } else
309 window->counter++;
310
311 /* Slide the window to the left one bit */
312 window->data = (window->data << 1);
313
314 /* If this packet was a success then set the low bit high */
315 if (success) {
316 window->success_counter++;
317 window->data |= 1;
318 }
319
320 /* window->counter can't be 0 -- it is either >0 or
321 * IWL_RATE_MAX_WINDOW */
322 window->success_ratio = 12800 * window->success_counter /
323 window->counter;
324
325 /* Tag this window as having been updated */
326 window->stamp = jiffies;
327
328 spin_unlock_irqrestore(&rs_priv->lock, flags);
329 }
330}
331
332static void rs_rate_init(void *priv_rate, void *priv_sta,
333 struct ieee80211_local *local, struct sta_info *sta)
334{
335 int i;
336
337 IWL_DEBUG_RATE("enter\n");
338
339 /* TODO: what is a good starting rate for STA? About middle? Maybe not
340 * the lowest or the highest rate.. Could consider using RSSI from
341 * previous packets? Need to have IEEE 802.1X auth succeed immediately
342 * after assoc.. */
343
344 for (i = IWL_RATE_COUNT - 1; i >= 0; i--) {
345 if (sta->supp_rates & (1 << i)) {
346 sta->txrate = i;
347 break;
348 }
349 }
350
351 sta->last_txrate = sta->txrate;
352
Mohamed Abbas14577f22007-11-12 11:37:42 +0800353 /* For MODE_IEEE80211A mode it start at IWL_FIRST_OFDM_RATE */
354 if (local->hw.conf.phymode == MODE_IEEE80211A)
355 sta->last_txrate += IWL_FIRST_OFDM_RATE;
356
Zhu Yib481de92007-09-25 17:54:57 -0700357 IWL_DEBUG_RATE("leave\n");
358}
359
360static void *rs_alloc(struct ieee80211_local *local)
361{
362 return local->hw.priv;
363}
364
365/* rate scale requires free function to be implmented */
366static void rs_free(void *priv)
367{
368 return;
369}
370static void rs_clear(void *priv)
371{
372 return;
373}
374
375
376static void *rs_alloc_sta(void *priv, gfp_t gfp)
377{
378 struct iwl_rate_scale_priv *rs_priv;
379 int i;
380
381 IWL_DEBUG_RATE("enter\n");
382
383 rs_priv = kzalloc(sizeof(struct iwl_rate_scale_priv), gfp);
384 if (!rs_priv) {
385 IWL_DEBUG_RATE("leave: ENOMEM\n");
386 return NULL;
387 }
388
389 spin_lock_init(&rs_priv->lock);
390
391 rs_priv->start_rate = IWL_RATE_INVALID;
392
393 /* default to just 802.11b */
394 rs_priv->expected_tpt = iwl_expected_tpt_b;
395
396 rs_priv->last_partial_flush = jiffies;
397 rs_priv->last_flush = jiffies;
398 rs_priv->flush_time = IWL_RATE_FLUSH;
399 rs_priv->last_tx_packets = 0;
400 rs_priv->ibss_sta_added = 0;
401
402 init_timer(&rs_priv->rate_scale_flush);
403 rs_priv->rate_scale_flush.data = (unsigned long)rs_priv;
404 rs_priv->rate_scale_flush.function = &iwl_bg_rate_scale_flush;
405
406 for (i = 0; i < IWL_RATE_COUNT; i++)
407 iwl_clear_window(&rs_priv->win[i]);
408
409 IWL_DEBUG_RATE("leave\n");
410
411 return rs_priv;
412}
413
414static void rs_free_sta(void *priv, void *priv_sta)
415{
416 struct iwl_rate_scale_priv *rs_priv = priv_sta;
417
418 IWL_DEBUG_RATE("enter\n");
419 del_timer_sync(&rs_priv->rate_scale_flush);
420 kfree(rs_priv);
421 IWL_DEBUG_RATE("leave\n");
422}
423
Mohamed Abbas14577f22007-11-12 11:37:42 +0800424
425/*
426 * get ieee prev rate from rate scale table.
427 * for A and B mode we need to overright prev
428 * value
429 */
430static int rs_adjust_next_rate(struct iwl_priv *priv, int rate)
431{
432 int next_rate = iwl_get_prev_ieee_rate(rate);
433
434 switch (priv->phymode) {
435 case MODE_IEEE80211A:
436 if (rate == IWL_RATE_12M_INDEX)
437 next_rate = IWL_RATE_9M_INDEX;
438 else if (rate == IWL_RATE_6M_INDEX)
439 next_rate = IWL_RATE_6M_INDEX;
440 break;
441 case MODE_IEEE80211B:
442 if (rate == IWL_RATE_11M_INDEX_TABLE)
443 next_rate = IWL_RATE_5M_INDEX_TABLE;
444 break;
445 default:
446 break;
447 }
448
449 return next_rate;
450}
Zhu Yib481de92007-09-25 17:54:57 -0700451/**
452 * rs_tx_status - Update rate control values based on Tx results
453 *
454 * NOTE: Uses iwl_priv->retry_rate for the # of retries attempted by
455 * the hardware for each rate.
456 */
457static void rs_tx_status(void *priv_rate,
458 struct net_device *dev,
459 struct sk_buff *skb,
460 struct ieee80211_tx_status *tx_resp)
461{
462 u8 retries, current_count;
463 int scale_rate_index, first_index, last_index;
464 unsigned long flags;
465 struct sta_info *sta;
466 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
467 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
468 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
469 struct iwl_rate_scale_priv *rs_priv;
470
471 IWL_DEBUG_RATE("enter\n");
472
473 retries = tx_resp->retry_count;
474
475 first_index = tx_resp->control.tx_rate;
476 if ((first_index < 0) || (first_index >= IWL_RATE_COUNT)) {
477 IWL_DEBUG_RATE("leave: Rate out of bounds: %0x for %d\n",
478 tx_resp->control.tx_rate, first_index);
479 return;
480 }
481
482 sta = sta_info_get(local, hdr->addr1);
483 if (!sta || !sta->rate_ctrl_priv) {
484 if (sta)
485 sta_info_put(sta);
486 IWL_DEBUG_RATE("leave: No STA priv data to update!\n");
487 return;
488 }
489
490 rs_priv = (void *)sta->rate_ctrl_priv;
491
492 rs_priv->tx_packets++;
493
494 scale_rate_index = first_index;
495 last_index = first_index;
496
497 /*
498 * Update the window for each rate. We determine which rates
499 * were Tx'd based on the total number of retries vs. the number
500 * of retries configured for each rate -- currently set to the
501 * priv value 'retry_rate' vs. rate specific
502 *
503 * On exit from this while loop last_index indicates the rate
504 * at which the frame was finally transmitted (or failed if no
505 * ACK)
506 */
507 while (retries > 0) {
508 if (retries < priv->retry_rate) {
509 current_count = retries;
510 last_index = scale_rate_index;
511 } else {
512 current_count = priv->retry_rate;
Mohamed Abbas14577f22007-11-12 11:37:42 +0800513 last_index = rs_adjust_next_rate(priv,
514 scale_rate_index);
Zhu Yib481de92007-09-25 17:54:57 -0700515 }
516
517 /* Update this rate accounting for as many retries
518 * as was used for it (per current_count) */
519 iwl_collect_tx_data(rs_priv,
520 &rs_priv->win[scale_rate_index],
521 0, current_count);
522 IWL_DEBUG_RATE("Update rate %d for %d retries.\n",
523 scale_rate_index, current_count);
524
525 retries -= current_count;
526
527 if (retries)
528 scale_rate_index =
Mohamed Abbas14577f22007-11-12 11:37:42 +0800529 rs_adjust_next_rate(priv, scale_rate_index);
Zhu Yib481de92007-09-25 17:54:57 -0700530 }
531
Mohamed Abbas14577f22007-11-12 11:37:42 +0800532
Zhu Yib481de92007-09-25 17:54:57 -0700533 /* Update the last index window with success/failure based on ACK */
534 IWL_DEBUG_RATE("Update rate %d with %s.\n",
535 last_index,
536 (tx_resp->flags & IEEE80211_TX_STATUS_ACK) ?
537 "success" : "failure");
538 iwl_collect_tx_data(rs_priv,
539 &rs_priv->win[last_index],
540 tx_resp->flags & IEEE80211_TX_STATUS_ACK, 1);
541
542 /* We updated the rate scale window -- if its been more than
543 * flush_time since the last run, schedule the flush
544 * again */
545 spin_lock_irqsave(&rs_priv->lock, flags);
546
547 if (!rs_priv->flush_pending &&
548 time_after(jiffies, rs_priv->last_partial_flush +
549 rs_priv->flush_time)) {
550
551 rs_priv->flush_pending = 1;
552 mod_timer(&rs_priv->rate_scale_flush,
553 jiffies + rs_priv->flush_time);
554 }
555
556 spin_unlock_irqrestore(&rs_priv->lock, flags);
557
558 sta_info_put(sta);
559
560 IWL_DEBUG_RATE("leave\n");
561
562 return;
563}
564
Zhu Yib481de92007-09-25 17:54:57 -0700565static u16 iwl_get_adjacent_rate(struct iwl_rate_scale_priv *rs_priv,
566 u8 index, u16 rate_mask, int phymode)
567{
568 u8 high = IWL_RATE_INVALID;
569 u8 low = IWL_RATE_INVALID;
570
571 /* 802.11A walks to the next literal adjascent rate in
572 * the rate table */
573 if (unlikely(phymode == MODE_IEEE80211A)) {
574 int i;
575 u32 mask;
576
577 /* Find the previous rate that is in the rate mask */
578 i = index - 1;
579 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
580 if (rate_mask & mask) {
581 low = i;
582 break;
583 }
584 }
585
586 /* Find the next rate that is in the rate mask */
587 i = index + 1;
588 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
589 if (rate_mask & mask) {
590 high = i;
591 break;
592 }
593 }
594
595 return (high << 8) | low;
596 }
597
598 low = index;
599 while (low != IWL_RATE_INVALID) {
600 if (rs_priv->tgg)
601 low = iwl_rates[low].prev_rs_tgg;
602 else
603 low = iwl_rates[low].prev_rs;
604 if (low == IWL_RATE_INVALID)
605 break;
606 if (rate_mask & (1 << low))
607 break;
608 IWL_DEBUG_RATE("Skipping masked lower rate: %d\n", low);
609 }
610
611 high = index;
612 while (high != IWL_RATE_INVALID) {
613 if (rs_priv->tgg)
614 high = iwl_rates[high].next_rs_tgg;
615 else
616 high = iwl_rates[high].next_rs;
617 if (high == IWL_RATE_INVALID)
618 break;
619 if (rate_mask & (1 << high))
620 break;
621 IWL_DEBUG_RATE("Skipping masked higher rate: %d\n", high);
622 }
623
624 return (high << 8) | low;
625}
626
627/**
628 * rs_get_rate - find the rate for the requested packet
629 *
630 * Returns the ieee80211_rate structure allocated by the driver.
631 *
632 * The rate control algorithm has no internal mapping between hw_mode's
633 * rate ordering and the rate ordering used by the rate control algorithm.
634 *
635 * The rate control algorithm uses a single table of rates that goes across
636 * the entire A/B/G spectrum vs. being limited to just one particular
637 * hw_mode.
638 *
639 * As such, we can't convert the index obtained below into the hw_mode's
640 * rate table and must reference the driver allocated rate table
641 *
642 */
Mattias Nissler1abbe492007-12-20 13:50:07 +0100643static void rs_get_rate(void *priv_rate, struct net_device *dev,
644 struct ieee80211_hw_mode *mode, struct sk_buff *skb,
645 struct rate_selection *sel)
Zhu Yib481de92007-09-25 17:54:57 -0700646{
647 u8 low = IWL_RATE_INVALID;
648 u8 high = IWL_RATE_INVALID;
649 u16 high_low;
650 int index;
651 struct iwl_rate_scale_priv *rs_priv;
652 struct iwl_rate_scale_data *window = NULL;
653 int current_tpt = IWL_INVALID_VALUE;
654 int low_tpt = IWL_INVALID_VALUE;
655 int high_tpt = IWL_INVALID_VALUE;
656 u32 fail_count;
657 s8 scale_action = 0;
658 unsigned long flags;
659 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
660 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
661 struct sta_info *sta;
Mattias Nissler1abbe492007-12-20 13:50:07 +0100662 u16 rate_mask;
Zhu Yib481de92007-09-25 17:54:57 -0700663 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
Joe Perches0795af52007-10-03 17:59:30 -0700664 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -0700665
666 IWL_DEBUG_RATE("enter\n");
667
Zhu Yib481de92007-09-25 17:54:57 -0700668 sta = sta_info_get(local, hdr->addr1);
669 if (!sta || !sta->rate_ctrl_priv) {
670 IWL_DEBUG_RATE("leave: No STA priv data to update!\n");
Mattias Nissler1abbe492007-12-20 13:50:07 +0100671 sel->rate = rate_lowest(local, local->oper_hw_mode, sta);
Zhu Yib481de92007-09-25 17:54:57 -0700672 if (sta)
673 sta_info_put(sta);
Mattias Nissler1abbe492007-12-20 13:50:07 +0100674 return;
Zhu Yib481de92007-09-25 17:54:57 -0700675 }
676
677 rate_mask = sta->supp_rates;
Mohamed Abbas14577f22007-11-12 11:37:42 +0800678 index = min(sta->last_txrate & 0xffff, IWL_RATE_COUNT - 1);
679
680 if (priv->phymode == (u8) MODE_IEEE80211A)
681 rate_mask = rate_mask << IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -0700682
683 rs_priv = (void *)sta->rate_ctrl_priv;
684
685 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
686 !rs_priv->ibss_sta_added) {
687 u8 sta_id = iwl_hw_find_station(priv, hdr->addr1);
688
689 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -0700690 IWL_DEBUG_RATE("LQ: ADD station %s\n",
691 print_mac(mac, hdr->addr1));
Zhu Yib481de92007-09-25 17:54:57 -0700692 sta_id = iwl_add_station(priv,
693 hdr->addr1, 0, CMD_ASYNC);
694 }
695 if (sta_id != IWL_INVALID_STATION)
696 rs_priv->ibss_sta_added = 1;
697 }
698
699 spin_lock_irqsave(&rs_priv->lock, flags);
700
701 if (rs_priv->start_rate != IWL_RATE_INVALID) {
702 index = rs_priv->start_rate;
703 rs_priv->start_rate = IWL_RATE_INVALID;
704 }
705
706 window = &(rs_priv->win[index]);
707
708 fail_count = window->counter - window->success_counter;
709
710 if (((fail_count <= IWL_RATE_MIN_FAILURE_TH) &&
711 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH))) {
712 window->average_tpt = IWL_INVALID_VALUE;
713 spin_unlock_irqrestore(&rs_priv->lock, flags);
714
715 IWL_DEBUG_RATE("Invalid average_tpt on rate %d: "
716 "counter: %d, success_counter: %d, "
717 "expected_tpt is %sNULL\n",
718 index,
719 window->counter,
720 window->success_counter,
721 rs_priv->expected_tpt ? "not " : "");
722 goto out;
723
724 }
725
726 window->average_tpt = ((window->success_ratio *
727 rs_priv->expected_tpt[index] + 64) / 128);
728 current_tpt = window->average_tpt;
729
730 high_low = iwl_get_adjacent_rate(rs_priv, index, rate_mask,
731 local->hw.conf.phymode);
732 low = high_low & 0xff;
733 high = (high_low >> 8) & 0xff;
734
735 if (low != IWL_RATE_INVALID)
736 low_tpt = rs_priv->win[low].average_tpt;
737
738 if (high != IWL_RATE_INVALID)
739 high_tpt = rs_priv->win[high].average_tpt;
740
741 spin_unlock_irqrestore(&rs_priv->lock, flags);
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;
748 } else if ((low_tpt == IWL_INVALID_VALUE) &&
749 (high_tpt == IWL_INVALID_VALUE))
750 scale_action = 1;
751 else if ((low_tpt != IWL_INVALID_VALUE) &&
752 (high_tpt != IWL_INVALID_VALUE)
753 && (low_tpt < current_tpt)
754 && (high_tpt < current_tpt)) {
755 IWL_DEBUG_RATE("No action -- low [%d] & high [%d] < "
756 "current_tpt [%d]\n",
757 low_tpt, high_tpt, current_tpt);
758 scale_action = 0;
759 } else {
760 if (high_tpt != IWL_INVALID_VALUE) {
761 if (high_tpt > current_tpt)
762 scale_action = 1;
763 else {
764 IWL_DEBUG_RATE
765 ("decrease rate because of high tpt\n");
766 scale_action = -1;
767 }
768 } else if (low_tpt != IWL_INVALID_VALUE) {
769 if (low_tpt > current_tpt) {
770 IWL_DEBUG_RATE
771 ("decrease rate because of low tpt\n");
772 scale_action = -1;
773 } else
774 scale_action = 1;
775 }
776 }
777
778 if ((window->success_ratio > IWL_RATE_HIGH_TH) ||
779 (current_tpt > window->average_tpt)) {
780 IWL_DEBUG_RATE("No action -- success_ratio [%d] > HIGH_TH or "
781 "current_tpt [%d] > average_tpt [%d]\n",
782 window->success_ratio,
783 current_tpt, window->average_tpt);
784 scale_action = 0;
785 }
786
787 switch (scale_action) {
788 case -1:
789 if (low != IWL_RATE_INVALID)
790 index = low;
791 break;
792
793 case 1:
794 if (high != IWL_RATE_INVALID)
795 index = high;
796
797 break;
798
799 case 0:
800 default:
801 break;
802 }
803
804 IWL_DEBUG_RATE("Selected %d (action %d) - low %d high %d\n",
805 index, scale_action, low, high);
806
807 out:
808
809 sta->last_txrate = index;
Mohamed Abbas14577f22007-11-12 11:37:42 +0800810 if (priv->phymode == (u8) MODE_IEEE80211A)
811 sta->txrate = sta->last_txrate - IWL_FIRST_OFDM_RATE;
812 else
813 sta->txrate = sta->last_txrate;
814
Zhu Yib481de92007-09-25 17:54:57 -0700815 sta_info_put(sta);
816
817 IWL_DEBUG_RATE("leave: %d\n", index);
818
Mattias Nissler1abbe492007-12-20 13:50:07 +0100819 sel->rate = &priv->ieee_rates[index];
Zhu Yib481de92007-09-25 17:54:57 -0700820}
821
822static struct rate_control_ops rs_ops = {
823 .module = NULL,
824 .name = RS_NAME,
825 .tx_status = rs_tx_status,
826 .get_rate = rs_get_rate,
827 .rate_init = rs_rate_init,
828 .clear = rs_clear,
829 .alloc = rs_alloc,
830 .free = rs_free,
831 .alloc_sta = rs_alloc_sta,
832 .free_sta = rs_free_sta,
833};
834
835int iwl_fill_rs_info(struct ieee80211_hw *hw, char *buf, u8 sta_id)
836{
837 struct ieee80211_local *local = hw_to_local(hw);
838 struct iwl_priv *priv = hw->priv;
839 struct iwl_rate_scale_priv *rs_priv;
840 struct sta_info *sta;
841 unsigned long flags;
842 int count = 0, i;
843 u32 samples = 0, success = 0, good = 0;
844 unsigned long now = jiffies;
845 u32 max_time = 0;
846
847 sta = sta_info_get(local, priv->stations[sta_id].sta.sta.addr);
848 if (!sta || !sta->rate_ctrl_priv) {
849 if (sta) {
850 sta_info_put(sta);
851 IWL_DEBUG_RATE("leave - no private rate data!\n");
852 } else
853 IWL_DEBUG_RATE("leave - no station!\n");
854 return sprintf(buf, "station %d not found\n", sta_id);
855 }
856
857 rs_priv = (void *)sta->rate_ctrl_priv;
858 spin_lock_irqsave(&rs_priv->lock, flags);
859 i = IWL_RATE_54M_INDEX;
860 while (1) {
861 u64 mask;
862 int j;
863
864 count +=
865 sprintf(&buf[count], " %2dMbs: ", iwl_rates[i].ieee / 2);
866
867 mask = (1ULL << (IWL_RATE_MAX_WINDOW - 1));
868 for (j = 0; j < IWL_RATE_MAX_WINDOW; j++, mask >>= 1)
869 buf[count++] =
870 (rs_priv->win[i].data & mask) ? '1' : '0';
871
872 samples += rs_priv->win[i].counter;
873 good += rs_priv->win[i].success_counter;
874 success += rs_priv->win[i].success_counter * iwl_rates[i].ieee;
875
876 if (rs_priv->win[i].stamp) {
877 int delta =
878 jiffies_to_msecs(now - rs_priv->win[i].stamp);
879
880 if (delta > max_time)
881 max_time = delta;
882
883 count += sprintf(&buf[count], "%5dms\n", delta);
884 } else
885 buf[count++] = '\n';
886
887 j = iwl_get_prev_ieee_rate(i);
888 if (j == i)
889 break;
890 i = j;
891 }
892 spin_unlock_irqrestore(&rs_priv->lock, flags);
893 sta_info_put(sta);
894
895 /* Display the average rate of all samples taken.
896 *
897 * NOTE: We multiple # of samples by 2 since the IEEE measurement
898 * added from iwl_rates is actually 2X the rate */
899 if (samples)
900 count += sprintf(
901 &buf[count],
902 "\nAverage rate is %3d.%02dMbs over last %4dms\n"
903 "%3d%% success (%d good packets over %d tries)\n",
904 success / (2 * samples), (success * 5 / samples) % 10,
905 max_time, good * 100 / samples, good, samples);
906 else
907 count += sprintf(&buf[count], "\nAverage rate: 0Mbs\n");
908
909 return count;
910}
911
912void iwl_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id)
913{
914 struct iwl_priv *priv = hw->priv;
915 s32 rssi = 0;
916 unsigned long flags;
917 struct ieee80211_local *local = hw_to_local(hw);
918 struct iwl_rate_scale_priv *rs_priv;
919 struct sta_info *sta;
920
921 IWL_DEBUG_RATE("enter\n");
922
923 if (!local->rate_ctrl->ops->name ||
924 strcmp(local->rate_ctrl->ops->name, RS_NAME)) {
925 IWL_WARNING("iwl-3945-rs not selected as rate control algo!\n");
926 IWL_DEBUG_RATE("leave - mac80211 picked the wrong RC algo.\n");
927 return;
928 }
929
930 sta = sta_info_get(local, priv->stations[sta_id].sta.sta.addr);
931 if (!sta || !sta->rate_ctrl_priv) {
932 if (sta)
933 sta_info_put(sta);
934 IWL_DEBUG_RATE("leave - no private rate data!\n");
935 return;
936 }
937
938 rs_priv = (void *)sta->rate_ctrl_priv;
939
940 spin_lock_irqsave(&rs_priv->lock, flags);
941
942 rs_priv->tgg = 0;
943 switch (priv->phymode) {
944 case MODE_IEEE80211G:
945 if (priv->active_rxon.flags & RXON_FLG_TGG_PROTECT_MSK) {
946 rs_priv->tgg = 1;
947 rs_priv->expected_tpt = iwl_expected_tpt_g_prot;
948 } else
949 rs_priv->expected_tpt = iwl_expected_tpt_g;
950 break;
951
952 case MODE_IEEE80211A:
953 rs_priv->expected_tpt = iwl_expected_tpt_a;
954 break;
955
956 default:
957 IWL_WARNING("Invalid phymode. Defaulting to 802.11b\n");
958 case MODE_IEEE80211B:
959 rs_priv->expected_tpt = iwl_expected_tpt_b;
960 break;
961 }
962
963 sta_info_put(sta);
964 spin_unlock_irqrestore(&rs_priv->lock, flags);
965
966 rssi = priv->last_rx_rssi;
967 if (rssi == 0)
968 rssi = IWL_MIN_RSSI_VAL;
969
970 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RATE, "Network RSSI: %d\n", rssi);
971
972 rs_priv->start_rate = iwl_get_rate_index_by_rssi(rssi, priv->phymode);
973
974 IWL_DEBUG_RATE("leave: rssi %d assign rate index: "
975 "%d (plcp 0x%x)\n", rssi, rs_priv->start_rate,
976 iwl_rates[rs_priv->start_rate].plcp);
977}
978
979void iwl_rate_control_register(struct ieee80211_hw *hw)
980{
981 ieee80211_rate_control_register(&rs_ops);
982}
983
984void iwl_rate_control_unregister(struct ieee80211_hw *hw)
985{
986 ieee80211_rate_control_unregister(&rs_ops);
987}
988
989