pcmcia: CompactFlash driver for PA Semi Electra boards
[pv_ops_mirror.git] / drivers / net / wireless / iwlwifi / iwl-3945-rs.c
blobf4aabcf480e46636479ebf00fc4f9d70b95f5ebf
1 /******************************************************************************
3 * Copyright(c) 2005 - 2007 Intel Corporation. All rights reserved.
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.
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.
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
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
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
25 *****************************************************************************/
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>
34 #include <linux/netdevice.h>
35 #include <linux/etherdevice.h>
36 #include <linux/delay.h>
38 #include <linux/workqueue.h>
40 #include <net/mac80211.h>
41 #include <linux/wireless.h>
43 #define IWL 3945
45 #include "../net/mac80211/ieee80211_rate.h"
47 #include "iwlwifi.h"
49 #define RS_NAME "iwl-3945-rs"
51 struct iwl_rate_scale_data {
52 u64 data;
53 s32 success_counter;
54 s32 success_ratio;
55 s32 counter;
56 s32 average_tpt;
57 unsigned long stamp;
60 struct iwl_rate_scale_priv {
61 spinlock_t lock;
62 s32 *expected_tpt;
63 unsigned long last_partial_flush;
64 unsigned long last_flush;
65 u32 flush_time;
66 u32 last_tx_packets;
67 u32 tx_packets;
68 u8 tgg;
69 u8 flush_pending;
70 u8 start_rate;
71 u8 ibss_sta_added;
72 struct timer_list rate_scale_flush;
73 struct iwl_rate_scale_data win[IWL_RATE_COUNT];
76 static s32 iwl_expected_tpt_g[IWL_RATE_COUNT] = {
77 0, 0, 76, 104, 130, 168, 191, 202, 7, 13, 35, 58
80 static s32 iwl_expected_tpt_g_prot[IWL_RATE_COUNT] = {
81 0, 0, 0, 80, 93, 113, 123, 125, 7, 13, 35, 58
84 static s32 iwl_expected_tpt_a[IWL_RATE_COUNT] = {
85 40, 57, 72, 98, 121, 154, 177, 186, 0, 0, 0, 0
88 static s32 iwl_expected_tpt_b[IWL_RATE_COUNT] = {
89 0, 0, 0, 0, 0, 0, 0, 0, 7, 13, 35, 58
92 struct iwl_tpt_entry {
93 s8 min_rssi;
94 u8 index;
97 static struct iwl_tpt_entry iwl_tpt_table_a[] = {
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}
108 static struct iwl_tpt_entry iwl_tpt_table_b[] = {
109 {-86, IWL_RATE_11M_INDEX},
110 {-88, IWL_RATE_5M_INDEX},
111 {-90, IWL_RATE_2M_INDEX},
112 {-92, IWL_RATE_1M_INDEX}
116 static struct iwl_tpt_entry iwl_tpt_table_g[] = {
117 {-60, IWL_RATE_54M_INDEX},
118 {-64, IWL_RATE_48M_INDEX},
119 {-68, IWL_RATE_36M_INDEX},
120 {-80, IWL_RATE_24M_INDEX},
121 {-84, IWL_RATE_18M_INDEX},
122 {-85, IWL_RATE_12M_INDEX},
123 {-86, IWL_RATE_11M_INDEX},
124 {-88, IWL_RATE_5M_INDEX},
125 {-90, IWL_RATE_2M_INDEX},
126 {-92, IWL_RATE_1M_INDEX}
129 #define IWL_RATE_MAX_WINDOW 62
130 #define IWL_RATE_FLUSH (3*HZ/10)
131 #define IWL_RATE_WIN_FLUSH (HZ/2)
132 #define IWL_RATE_HIGH_TH 11520
133 #define IWL_RATE_MIN_FAILURE_TH 8
134 #define IWL_RATE_MIN_SUCCESS_TH 8
135 #define IWL_RATE_DECREASE_TH 1920
137 static u8 iwl_get_rate_index_by_rssi(s32 rssi, u8 mode)
139 u32 index = 0;
140 u32 table_size = 0;
141 struct iwl_tpt_entry *tpt_table = NULL;
143 if ((rssi < IWL_MIN_RSSI_VAL) || (rssi > IWL_MAX_RSSI_VAL))
144 rssi = IWL_MIN_RSSI_VAL;
146 switch (mode) {
147 case MODE_IEEE80211G:
148 tpt_table = iwl_tpt_table_g;
149 table_size = ARRAY_SIZE(iwl_tpt_table_g);
150 break;
152 case MODE_IEEE80211A:
153 tpt_table = iwl_tpt_table_a;
154 table_size = ARRAY_SIZE(iwl_tpt_table_a);
155 break;
157 default:
158 case MODE_IEEE80211B:
159 tpt_table = iwl_tpt_table_b;
160 table_size = ARRAY_SIZE(iwl_tpt_table_b);
161 break;
164 while ((index < table_size) && (rssi < tpt_table[index].min_rssi))
165 index++;
167 index = min(index, (table_size - 1));
169 return tpt_table[index].index;
172 static void iwl_clear_window(struct iwl_rate_scale_data *window)
174 window->data = 0;
175 window->success_counter = 0;
176 window->success_ratio = IWL_INVALID_VALUE;
177 window->counter = 0;
178 window->average_tpt = IWL_INVALID_VALUE;
179 window->stamp = 0;
183 * iwl_rate_scale_flush_windows - flush out the rate scale windows
185 * Returns the number of windows that have gathered data but were
186 * not flushed. If there were any that were not flushed, then
187 * reschedule the rate flushing routine.
189 static int iwl_rate_scale_flush_windows(struct iwl_rate_scale_priv *rs_priv)
191 int unflushed = 0;
192 int i;
193 unsigned long flags;
196 * For each rate, if we have collected data on that rate
197 * and it has been more than IWL_RATE_WIN_FLUSH
198 * since we flushed, clear out the gathered statistics
200 for (i = 0; i < IWL_RATE_COUNT; i++) {
201 if (!rs_priv->win[i].counter)
202 continue;
204 spin_lock_irqsave(&rs_priv->lock, flags);
205 if (time_after(jiffies, rs_priv->win[i].stamp +
206 IWL_RATE_WIN_FLUSH)) {
207 IWL_DEBUG_RATE("flushing %d samples of rate "
208 "index %d\n",
209 rs_priv->win[i].counter, i);
210 iwl_clear_window(&rs_priv->win[i]);
211 } else
212 unflushed++;
213 spin_unlock_irqrestore(&rs_priv->lock, flags);
216 return unflushed;
219 #define IWL_RATE_FLUSH_MAX 5000 /* msec */
220 #define IWL_RATE_FLUSH_MIN 50 /* msec */
222 static void iwl_bg_rate_scale_flush(unsigned long data)
224 struct iwl_rate_scale_priv *rs_priv = (void *)data;
225 int unflushed = 0;
226 unsigned long flags;
227 u32 packet_count, duration, pps;
229 IWL_DEBUG_RATE("enter\n");
231 unflushed = iwl_rate_scale_flush_windows(rs_priv);
233 spin_lock_irqsave(&rs_priv->lock, flags);
235 rs_priv->flush_pending = 0;
237 /* Number of packets Rx'd since last time this timer ran */
238 packet_count = (rs_priv->tx_packets - rs_priv->last_tx_packets) + 1;
240 rs_priv->last_tx_packets = rs_priv->tx_packets + 1;
242 if (unflushed) {
243 duration =
244 jiffies_to_msecs(jiffies - rs_priv->last_partial_flush);
245 /* duration = jiffies_to_msecs(rs_priv->flush_time); */
247 IWL_DEBUG_RATE("Tx'd %d packets in %dms\n",
248 packet_count, duration);
250 /* Determine packets per second */
251 if (duration)
252 pps = (packet_count * 1000) / duration;
253 else
254 pps = 0;
256 if (pps) {
257 duration = IWL_RATE_FLUSH_MAX / pps;
258 if (duration < IWL_RATE_FLUSH_MIN)
259 duration = IWL_RATE_FLUSH_MIN;
260 } else
261 duration = IWL_RATE_FLUSH_MAX;
263 rs_priv->flush_time = msecs_to_jiffies(duration);
265 IWL_DEBUG_RATE("new flush period: %d msec ave %d\n",
266 duration, packet_count);
268 mod_timer(&rs_priv->rate_scale_flush, jiffies +
269 rs_priv->flush_time);
271 rs_priv->last_partial_flush = jiffies;
274 /* If there weren't any unflushed entries, we don't schedule the timer
275 * to run again */
277 rs_priv->last_flush = jiffies;
279 spin_unlock_irqrestore(&rs_priv->lock, flags);
281 IWL_DEBUG_RATE("leave\n");
285 * iwl_collect_tx_data - Update the success/failure sliding window
287 * We keep a sliding window of the last 64 packets transmitted
288 * at this rate. window->data contains the bitmask of successful
289 * packets.
291 static void iwl_collect_tx_data(struct iwl_rate_scale_priv *rs_priv,
292 struct iwl_rate_scale_data *window,
293 int success, int retries)
295 unsigned long flags;
297 if (!retries) {
298 IWL_DEBUG_RATE("leave: retries == 0 -- should be at least 1\n");
299 return;
302 while (retries--) {
303 spin_lock_irqsave(&rs_priv->lock, flags);
305 /* If we have filled up the window then subtract one from the
306 * success counter if the high-bit is counting toward
307 * success */
308 if (window->counter == IWL_RATE_MAX_WINDOW) {
309 if (window->data & (1ULL << (IWL_RATE_MAX_WINDOW - 1)))
310 window->success_counter--;
311 } else
312 window->counter++;
314 /* Slide the window to the left one bit */
315 window->data = (window->data << 1);
317 /* If this packet was a success then set the low bit high */
318 if (success) {
319 window->success_counter++;
320 window->data |= 1;
323 /* window->counter can't be 0 -- it is either >0 or
324 * IWL_RATE_MAX_WINDOW */
325 window->success_ratio = 12800 * window->success_counter /
326 window->counter;
328 /* Tag this window as having been updated */
329 window->stamp = jiffies;
331 spin_unlock_irqrestore(&rs_priv->lock, flags);
335 static void rs_rate_init(void *priv_rate, void *priv_sta,
336 struct ieee80211_local *local, struct sta_info *sta)
338 int i;
340 IWL_DEBUG_RATE("enter\n");
342 /* TODO: what is a good starting rate for STA? About middle? Maybe not
343 * the lowest or the highest rate.. Could consider using RSSI from
344 * previous packets? Need to have IEEE 802.1X auth succeed immediately
345 * after assoc.. */
347 for (i = IWL_RATE_COUNT - 1; i >= 0; i--) {
348 if (sta->supp_rates & (1 << i)) {
349 sta->txrate = i;
350 break;
354 sta->last_txrate = sta->txrate;
356 IWL_DEBUG_RATE("leave\n");
359 static void *rs_alloc(struct ieee80211_local *local)
361 return local->hw.priv;
364 /* rate scale requires free function to be implmented */
365 static void rs_free(void *priv)
367 return;
369 static void rs_clear(void *priv)
371 return;
375 static void *rs_alloc_sta(void *priv, gfp_t gfp)
377 struct iwl_rate_scale_priv *rs_priv;
378 int i;
380 IWL_DEBUG_RATE("enter\n");
382 rs_priv = kzalloc(sizeof(struct iwl_rate_scale_priv), gfp);
383 if (!rs_priv) {
384 IWL_DEBUG_RATE("leave: ENOMEM\n");
385 return NULL;
388 spin_lock_init(&rs_priv->lock);
390 rs_priv->start_rate = IWL_RATE_INVALID;
392 /* default to just 802.11b */
393 rs_priv->expected_tpt = iwl_expected_tpt_b;
395 rs_priv->last_partial_flush = jiffies;
396 rs_priv->last_flush = jiffies;
397 rs_priv->flush_time = IWL_RATE_FLUSH;
398 rs_priv->last_tx_packets = 0;
399 rs_priv->ibss_sta_added = 0;
401 init_timer(&rs_priv->rate_scale_flush);
402 rs_priv->rate_scale_flush.data = (unsigned long)rs_priv;
403 rs_priv->rate_scale_flush.function = &iwl_bg_rate_scale_flush;
405 for (i = 0; i < IWL_RATE_COUNT; i++)
406 iwl_clear_window(&rs_priv->win[i]);
408 IWL_DEBUG_RATE("leave\n");
410 return rs_priv;
413 static void rs_free_sta(void *priv, void *priv_sta)
415 struct iwl_rate_scale_priv *rs_priv = priv_sta;
417 IWL_DEBUG_RATE("enter\n");
418 del_timer_sync(&rs_priv->rate_scale_flush);
419 kfree(rs_priv);
420 IWL_DEBUG_RATE("leave\n");
424 * rs_tx_status - Update rate control values based on Tx results
426 * NOTE: Uses iwl_priv->retry_rate for the # of retries attempted by
427 * the hardware for each rate.
429 static void rs_tx_status(void *priv_rate,
430 struct net_device *dev,
431 struct sk_buff *skb,
432 struct ieee80211_tx_status *tx_resp)
434 u8 retries, current_count;
435 int scale_rate_index, first_index, last_index;
436 unsigned long flags;
437 struct sta_info *sta;
438 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
439 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
440 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
441 struct iwl_rate_scale_priv *rs_priv;
443 IWL_DEBUG_RATE("enter\n");
445 retries = tx_resp->retry_count;
447 first_index = tx_resp->control.tx_rate;
448 if ((first_index < 0) || (first_index >= IWL_RATE_COUNT)) {
449 IWL_DEBUG_RATE("leave: Rate out of bounds: %0x for %d\n",
450 tx_resp->control.tx_rate, first_index);
451 return;
454 sta = sta_info_get(local, hdr->addr1);
455 if (!sta || !sta->rate_ctrl_priv) {
456 if (sta)
457 sta_info_put(sta);
458 IWL_DEBUG_RATE("leave: No STA priv data to update!\n");
459 return;
462 rs_priv = (void *)sta->rate_ctrl_priv;
464 rs_priv->tx_packets++;
466 scale_rate_index = first_index;
467 last_index = first_index;
470 * Update the window for each rate. We determine which rates
471 * were Tx'd based on the total number of retries vs. the number
472 * of retries configured for each rate -- currently set to the
473 * priv value 'retry_rate' vs. rate specific
475 * On exit from this while loop last_index indicates the rate
476 * at which the frame was finally transmitted (or failed if no
477 * ACK)
479 while (retries > 0) {
480 if (retries < priv->retry_rate) {
481 current_count = retries;
482 last_index = scale_rate_index;
483 } else {
484 current_count = priv->retry_rate;
485 last_index = iwl_get_prev_ieee_rate(scale_rate_index);
488 /* Update this rate accounting for as many retries
489 * as was used for it (per current_count) */
490 iwl_collect_tx_data(rs_priv,
491 &rs_priv->win[scale_rate_index],
492 0, current_count);
493 IWL_DEBUG_RATE("Update rate %d for %d retries.\n",
494 scale_rate_index, current_count);
496 retries -= current_count;
498 if (retries)
499 scale_rate_index =
500 iwl_get_prev_ieee_rate(scale_rate_index);
503 /* Update the last index window with success/failure based on ACK */
504 IWL_DEBUG_RATE("Update rate %d with %s.\n",
505 last_index,
506 (tx_resp->flags & IEEE80211_TX_STATUS_ACK) ?
507 "success" : "failure");
508 iwl_collect_tx_data(rs_priv,
509 &rs_priv->win[last_index],
510 tx_resp->flags & IEEE80211_TX_STATUS_ACK, 1);
512 /* We updated the rate scale window -- if its been more than
513 * flush_time since the last run, schedule the flush
514 * again */
515 spin_lock_irqsave(&rs_priv->lock, flags);
517 if (!rs_priv->flush_pending &&
518 time_after(jiffies, rs_priv->last_partial_flush +
519 rs_priv->flush_time)) {
521 rs_priv->flush_pending = 1;
522 mod_timer(&rs_priv->rate_scale_flush,
523 jiffies + rs_priv->flush_time);
526 spin_unlock_irqrestore(&rs_priv->lock, flags);
528 sta_info_put(sta);
530 IWL_DEBUG_RATE("leave\n");
532 return;
535 static struct ieee80211_rate *iwl_get_lowest_rate(struct ieee80211_local
536 *local)
538 struct ieee80211_hw_mode *mode = local->oper_hw_mode;
539 int i;
541 for (i = 0; i < mode->num_rates; i++) {
542 struct ieee80211_rate *rate = &mode->rates[i];
544 if (rate->flags & IEEE80211_RATE_SUPPORTED)
545 return rate;
548 return &mode->rates[0];
551 static u16 iwl_get_adjacent_rate(struct iwl_rate_scale_priv *rs_priv,
552 u8 index, u16 rate_mask, int phymode)
554 u8 high = IWL_RATE_INVALID;
555 u8 low = IWL_RATE_INVALID;
557 /* 802.11A walks to the next literal adjascent rate in
558 * the rate table */
559 if (unlikely(phymode == MODE_IEEE80211A)) {
560 int i;
561 u32 mask;
563 /* Find the previous rate that is in the rate mask */
564 i = index - 1;
565 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
566 if (rate_mask & mask) {
567 low = i;
568 break;
572 /* Find the next rate that is in the rate mask */
573 i = index + 1;
574 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
575 if (rate_mask & mask) {
576 high = i;
577 break;
581 return (high << 8) | low;
584 low = index;
585 while (low != IWL_RATE_INVALID) {
586 if (rs_priv->tgg)
587 low = iwl_rates[low].prev_rs_tgg;
588 else
589 low = iwl_rates[low].prev_rs;
590 if (low == IWL_RATE_INVALID)
591 break;
592 if (rate_mask & (1 << low))
593 break;
594 IWL_DEBUG_RATE("Skipping masked lower rate: %d\n", low);
597 high = index;
598 while (high != IWL_RATE_INVALID) {
599 if (rs_priv->tgg)
600 high = iwl_rates[high].next_rs_tgg;
601 else
602 high = iwl_rates[high].next_rs;
603 if (high == IWL_RATE_INVALID)
604 break;
605 if (rate_mask & (1 << high))
606 break;
607 IWL_DEBUG_RATE("Skipping masked higher rate: %d\n", high);
610 return (high << 8) | low;
614 * rs_get_rate - find the rate for the requested packet
616 * Returns the ieee80211_rate structure allocated by the driver.
618 * The rate control algorithm has no internal mapping between hw_mode's
619 * rate ordering and the rate ordering used by the rate control algorithm.
621 * The rate control algorithm uses a single table of rates that goes across
622 * the entire A/B/G spectrum vs. being limited to just one particular
623 * hw_mode.
625 * As such, we can't convert the index obtained below into the hw_mode's
626 * rate table and must reference the driver allocated rate table
629 static struct ieee80211_rate *rs_get_rate(void *priv_rate,
630 struct net_device *dev,
631 struct sk_buff *skb,
632 struct rate_control_extra *extra)
634 u8 low = IWL_RATE_INVALID;
635 u8 high = IWL_RATE_INVALID;
636 u16 high_low;
637 int index;
638 struct iwl_rate_scale_priv *rs_priv;
639 struct iwl_rate_scale_data *window = NULL;
640 int current_tpt = IWL_INVALID_VALUE;
641 int low_tpt = IWL_INVALID_VALUE;
642 int high_tpt = IWL_INVALID_VALUE;
643 u32 fail_count;
644 s8 scale_action = 0;
645 unsigned long flags;
646 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
647 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
648 struct sta_info *sta;
649 u16 fc, rate_mask;
650 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
651 DECLARE_MAC_BUF(mac);
653 IWL_DEBUG_RATE("enter\n");
655 memset(extra, 0, sizeof(*extra));
657 fc = le16_to_cpu(hdr->frame_control);
658 if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
659 (is_multicast_ether_addr(hdr->addr1))) {
660 /* Send management frames and broadcast/multicast data using
661 * lowest rate. */
662 /* TODO: this could probably be improved.. */
663 IWL_DEBUG_RATE("leave: lowest rate (not data or is "
664 "multicast)\n");
666 return iwl_get_lowest_rate(local);
669 sta = sta_info_get(local, hdr->addr1);
670 if (!sta || !sta->rate_ctrl_priv) {
671 IWL_DEBUG_RATE("leave: No STA priv data to update!\n");
672 if (sta)
673 sta_info_put(sta);
674 return NULL;
677 rate_mask = sta->supp_rates;
678 index = min(sta->txrate & 0xffff, IWL_RATE_COUNT - 1);
680 rs_priv = (void *)sta->rate_ctrl_priv;
682 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
683 !rs_priv->ibss_sta_added) {
684 u8 sta_id = iwl_hw_find_station(priv, hdr->addr1);
686 if (sta_id == IWL_INVALID_STATION) {
687 IWL_DEBUG_RATE("LQ: ADD station %s\n",
688 print_mac(mac, hdr->addr1));
689 sta_id = iwl_add_station(priv,
690 hdr->addr1, 0, CMD_ASYNC);
692 if (sta_id != IWL_INVALID_STATION)
693 rs_priv->ibss_sta_added = 1;
696 spin_lock_irqsave(&rs_priv->lock, flags);
698 if (rs_priv->start_rate != IWL_RATE_INVALID) {
699 index = rs_priv->start_rate;
700 rs_priv->start_rate = IWL_RATE_INVALID;
703 window = &(rs_priv->win[index]);
705 fail_count = window->counter - window->success_counter;
707 if (((fail_count <= IWL_RATE_MIN_FAILURE_TH) &&
708 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH))) {
709 window->average_tpt = IWL_INVALID_VALUE;
710 spin_unlock_irqrestore(&rs_priv->lock, flags);
712 IWL_DEBUG_RATE("Invalid average_tpt on rate %d: "
713 "counter: %d, success_counter: %d, "
714 "expected_tpt is %sNULL\n",
715 index,
716 window->counter,
717 window->success_counter,
718 rs_priv->expected_tpt ? "not " : "");
719 goto out;
723 window->average_tpt = ((window->success_ratio *
724 rs_priv->expected_tpt[index] + 64) / 128);
725 current_tpt = window->average_tpt;
727 high_low = iwl_get_adjacent_rate(rs_priv, index, rate_mask,
728 local->hw.conf.phymode);
729 low = high_low & 0xff;
730 high = (high_low >> 8) & 0xff;
732 if (low != IWL_RATE_INVALID)
733 low_tpt = rs_priv->win[low].average_tpt;
735 if (high != IWL_RATE_INVALID)
736 high_tpt = rs_priv->win[high].average_tpt;
738 spin_unlock_irqrestore(&rs_priv->lock, flags);
740 scale_action = 1;
742 if ((window->success_ratio < IWL_RATE_DECREASE_TH) || !current_tpt) {
743 IWL_DEBUG_RATE("decrease rate because of low success_ratio\n");
744 scale_action = -1;
745 } else if ((low_tpt == IWL_INVALID_VALUE) &&
746 (high_tpt == IWL_INVALID_VALUE))
747 scale_action = 1;
748 else if ((low_tpt != IWL_INVALID_VALUE) &&
749 (high_tpt != IWL_INVALID_VALUE)
750 && (low_tpt < current_tpt)
751 && (high_tpt < current_tpt)) {
752 IWL_DEBUG_RATE("No action -- low [%d] & high [%d] < "
753 "current_tpt [%d]\n",
754 low_tpt, high_tpt, current_tpt);
755 scale_action = 0;
756 } else {
757 if (high_tpt != IWL_INVALID_VALUE) {
758 if (high_tpt > current_tpt)
759 scale_action = 1;
760 else {
761 IWL_DEBUG_RATE
762 ("decrease rate because of high tpt\n");
763 scale_action = -1;
765 } else if (low_tpt != IWL_INVALID_VALUE) {
766 if (low_tpt > current_tpt) {
767 IWL_DEBUG_RATE
768 ("decrease rate because of low tpt\n");
769 scale_action = -1;
770 } else
771 scale_action = 1;
775 if ((window->success_ratio > IWL_RATE_HIGH_TH) ||
776 (current_tpt > window->average_tpt)) {
777 IWL_DEBUG_RATE("No action -- success_ratio [%d] > HIGH_TH or "
778 "current_tpt [%d] > average_tpt [%d]\n",
779 window->success_ratio,
780 current_tpt, window->average_tpt);
781 scale_action = 0;
784 switch (scale_action) {
785 case -1:
786 if (low != IWL_RATE_INVALID)
787 index = low;
788 break;
790 case 1:
791 if (high != IWL_RATE_INVALID)
792 index = high;
794 break;
796 case 0:
797 default:
798 break;
801 IWL_DEBUG_RATE("Selected %d (action %d) - low %d high %d\n",
802 index, scale_action, low, high);
804 out:
806 sta->last_txrate = index;
807 sta->txrate = sta->last_txrate;
808 sta_info_put(sta);
810 IWL_DEBUG_RATE("leave: %d\n", index);
812 return &priv->ieee_rates[index];
815 static struct rate_control_ops rs_ops = {
816 .module = NULL,
817 .name = RS_NAME,
818 .tx_status = rs_tx_status,
819 .get_rate = rs_get_rate,
820 .rate_init = rs_rate_init,
821 .clear = rs_clear,
822 .alloc = rs_alloc,
823 .free = rs_free,
824 .alloc_sta = rs_alloc_sta,
825 .free_sta = rs_free_sta,
828 int iwl_fill_rs_info(struct ieee80211_hw *hw, char *buf, u8 sta_id)
830 struct ieee80211_local *local = hw_to_local(hw);
831 struct iwl_priv *priv = hw->priv;
832 struct iwl_rate_scale_priv *rs_priv;
833 struct sta_info *sta;
834 unsigned long flags;
835 int count = 0, i;
836 u32 samples = 0, success = 0, good = 0;
837 unsigned long now = jiffies;
838 u32 max_time = 0;
840 sta = sta_info_get(local, priv->stations[sta_id].sta.sta.addr);
841 if (!sta || !sta->rate_ctrl_priv) {
842 if (sta) {
843 sta_info_put(sta);
844 IWL_DEBUG_RATE("leave - no private rate data!\n");
845 } else
846 IWL_DEBUG_RATE("leave - no station!\n");
847 return sprintf(buf, "station %d not found\n", sta_id);
850 rs_priv = (void *)sta->rate_ctrl_priv;
851 spin_lock_irqsave(&rs_priv->lock, flags);
852 i = IWL_RATE_54M_INDEX;
853 while (1) {
854 u64 mask;
855 int j;
857 count +=
858 sprintf(&buf[count], " %2dMbs: ", iwl_rates[i].ieee / 2);
860 mask = (1ULL << (IWL_RATE_MAX_WINDOW - 1));
861 for (j = 0; j < IWL_RATE_MAX_WINDOW; j++, mask >>= 1)
862 buf[count++] =
863 (rs_priv->win[i].data & mask) ? '1' : '0';
865 samples += rs_priv->win[i].counter;
866 good += rs_priv->win[i].success_counter;
867 success += rs_priv->win[i].success_counter * iwl_rates[i].ieee;
869 if (rs_priv->win[i].stamp) {
870 int delta =
871 jiffies_to_msecs(now - rs_priv->win[i].stamp);
873 if (delta > max_time)
874 max_time = delta;
876 count += sprintf(&buf[count], "%5dms\n", delta);
877 } else
878 buf[count++] = '\n';
880 j = iwl_get_prev_ieee_rate(i);
881 if (j == i)
882 break;
883 i = j;
885 spin_unlock_irqrestore(&rs_priv->lock, flags);
886 sta_info_put(sta);
888 /* Display the average rate of all samples taken.
890 * NOTE: We multiple # of samples by 2 since the IEEE measurement
891 * added from iwl_rates is actually 2X the rate */
892 if (samples)
893 count += sprintf(
894 &buf[count],
895 "\nAverage rate is %3d.%02dMbs over last %4dms\n"
896 "%3d%% success (%d good packets over %d tries)\n",
897 success / (2 * samples), (success * 5 / samples) % 10,
898 max_time, good * 100 / samples, good, samples);
899 else
900 count += sprintf(&buf[count], "\nAverage rate: 0Mbs\n");
902 return count;
905 void iwl_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id)
907 struct iwl_priv *priv = hw->priv;
908 s32 rssi = 0;
909 unsigned long flags;
910 struct ieee80211_local *local = hw_to_local(hw);
911 struct iwl_rate_scale_priv *rs_priv;
912 struct sta_info *sta;
914 IWL_DEBUG_RATE("enter\n");
916 if (!local->rate_ctrl->ops->name ||
917 strcmp(local->rate_ctrl->ops->name, RS_NAME)) {
918 IWL_WARNING("iwl-3945-rs not selected as rate control algo!\n");
919 IWL_DEBUG_RATE("leave - mac80211 picked the wrong RC algo.\n");
920 return;
923 sta = sta_info_get(local, priv->stations[sta_id].sta.sta.addr);
924 if (!sta || !sta->rate_ctrl_priv) {
925 if (sta)
926 sta_info_put(sta);
927 IWL_DEBUG_RATE("leave - no private rate data!\n");
928 return;
931 rs_priv = (void *)sta->rate_ctrl_priv;
933 spin_lock_irqsave(&rs_priv->lock, flags);
935 rs_priv->tgg = 0;
936 switch (priv->phymode) {
937 case MODE_IEEE80211G:
938 if (priv->active_rxon.flags & RXON_FLG_TGG_PROTECT_MSK) {
939 rs_priv->tgg = 1;
940 rs_priv->expected_tpt = iwl_expected_tpt_g_prot;
941 } else
942 rs_priv->expected_tpt = iwl_expected_tpt_g;
943 break;
945 case MODE_IEEE80211A:
946 rs_priv->expected_tpt = iwl_expected_tpt_a;
947 break;
949 default:
950 IWL_WARNING("Invalid phymode. Defaulting to 802.11b\n");
951 case MODE_IEEE80211B:
952 rs_priv->expected_tpt = iwl_expected_tpt_b;
953 break;
956 sta_info_put(sta);
957 spin_unlock_irqrestore(&rs_priv->lock, flags);
959 rssi = priv->last_rx_rssi;
960 if (rssi == 0)
961 rssi = IWL_MIN_RSSI_VAL;
963 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RATE, "Network RSSI: %d\n", rssi);
965 rs_priv->start_rate = iwl_get_rate_index_by_rssi(rssi, priv->phymode);
967 IWL_DEBUG_RATE("leave: rssi %d assign rate index: "
968 "%d (plcp 0x%x)\n", rssi, rs_priv->start_rate,
969 iwl_rates[rs_priv->start_rate].plcp);
972 void iwl_rate_control_register(struct ieee80211_hw *hw)
974 ieee80211_rate_control_register(&rs_ops);
977 void iwl_rate_control_unregister(struct ieee80211_hw *hw)
979 ieee80211_rate_control_unregister(&rs_ops);