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
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>
42 #include "../net/mac80211/ieee80211_rate.h"
46 #define RS_NAME "iwl-3945-rs"
48 struct iwl_rate_scale_data {
57 struct iwl_rate_scale_priv {
60 unsigned long last_partial_flush;
61 unsigned long last_flush;
69 struct timer_list rate_scale_flush;
70 struct iwl_rate_scale_data win[IWL_RATE_COUNT];
73 static s32 iwl_expected_tpt_g[IWL_RATE_COUNT] = {
74 0, 0, 76, 104, 130, 168, 191, 202, 7, 13, 35, 58
77 static s32 iwl_expected_tpt_g_prot[IWL_RATE_COUNT] = {
78 0, 0, 0, 80, 93, 113, 123, 125, 7, 13, 35, 58
81 static s32 iwl_expected_tpt_a[IWL_RATE_COUNT] = {
82 40, 57, 72, 98, 121, 154, 177, 186, 0, 0, 0, 0
85 static s32 iwl_expected_tpt_b[IWL_RATE_COUNT] = {
86 0, 0, 0, 0, 0, 0, 0, 0, 7, 13, 35, 58
89 struct iwl_tpt_entry {
94 static 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}
105 static 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}
113 static 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}
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
134 static u8 iwl_get_rate_index_by_rssi(s32 rssi, u8 mode)
138 struct iwl_tpt_entry *tpt_table = NULL;
140 if ((rssi < IWL_MIN_RSSI_VAL) || (rssi > IWL_MAX_RSSI_VAL))
141 rssi = IWL_MIN_RSSI_VAL;
144 case MODE_IEEE80211G:
145 tpt_table = iwl_tpt_table_g;
146 table_size = ARRAY_SIZE(iwl_tpt_table_g);
149 case MODE_IEEE80211A:
150 tpt_table = iwl_tpt_table_a;
151 table_size = ARRAY_SIZE(iwl_tpt_table_a);
155 case MODE_IEEE80211B:
156 tpt_table = iwl_tpt_table_b;
157 table_size = ARRAY_SIZE(iwl_tpt_table_b);
161 while ((index < table_size) && (rssi < tpt_table[index].min_rssi))
164 index = min(index, (table_size - 1));
166 return tpt_table[index].index;
169 static void iwl_clear_window(struct iwl_rate_scale_data *window)
172 window->success_counter = 0;
173 window->success_ratio = IWL_INVALID_VALUE;
175 window->average_tpt = IWL_INVALID_VALUE;
180 * iwl_rate_scale_flush_windows - flush out the rate scale windows
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.
186 static int iwl_rate_scale_flush_windows(struct iwl_rate_scale_priv *rs_priv)
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
197 for (i = 0; i < IWL_RATE_COUNT; i++) {
198 if (!rs_priv->win[i].counter)
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 "
206 rs_priv->win[i].counter, i);
207 iwl_clear_window(&rs_priv->win[i]);
210 spin_unlock_irqrestore(&rs_priv->lock, flags);
216 #define IWL_RATE_FLUSH_MAX 5000 /* msec */
217 #define IWL_RATE_FLUSH_MIN 50 /* msec */
219 static void iwl_bg_rate_scale_flush(unsigned long data)
221 struct iwl_rate_scale_priv *rs_priv = (void *)data;
224 u32 packet_count, duration, pps;
226 IWL_DEBUG_RATE("enter\n");
228 unflushed = iwl_rate_scale_flush_windows(rs_priv);
230 spin_lock_irqsave(&rs_priv->lock, flags);
232 rs_priv->flush_pending = 0;
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;
237 rs_priv->last_tx_packets = rs_priv->tx_packets + 1;
241 jiffies_to_msecs(jiffies - rs_priv->last_partial_flush);
242 /* duration = jiffies_to_msecs(rs_priv->flush_time); */
244 IWL_DEBUG_RATE("Tx'd %d packets in %dms\n",
245 packet_count, duration);
247 /* Determine packets per second */
249 pps = (packet_count * 1000) / duration;
254 duration = IWL_RATE_FLUSH_MAX / pps;
255 if (duration < IWL_RATE_FLUSH_MIN)
256 duration = IWL_RATE_FLUSH_MIN;
258 duration = IWL_RATE_FLUSH_MAX;
260 rs_priv->flush_time = msecs_to_jiffies(duration);
262 IWL_DEBUG_RATE("new flush period: %d msec ave %d\n",
263 duration, packet_count);
265 mod_timer(&rs_priv->rate_scale_flush, jiffies +
266 rs_priv->flush_time);
268 rs_priv->last_partial_flush = jiffies;
271 /* If there weren't any unflushed entries, we don't schedule the timer
274 rs_priv->last_flush = jiffies;
276 spin_unlock_irqrestore(&rs_priv->lock, flags);
278 IWL_DEBUG_RATE("leave\n");
282 * iwl_collect_tx_data - Update the success/failure sliding window
284 * We keep a sliding window of the last 64 packets transmitted
285 * at this rate. window->data contains the bitmask of successful
288 static void iwl_collect_tx_data(struct iwl_rate_scale_priv *rs_priv,
289 struct iwl_rate_scale_data *window,
290 int success, int retries)
295 IWL_DEBUG_RATE("leave: retries == 0 -- should be at least 1\n");
300 spin_lock_irqsave(&rs_priv->lock, flags);
302 /* If we have filled up the window then subtract one from the
303 * success counter if the high-bit is counting toward
305 if (window->counter == IWL_RATE_MAX_WINDOW) {
306 if (window->data & (1ULL << (IWL_RATE_MAX_WINDOW - 1)))
307 window->success_counter--;
311 /* Slide the window to the left one bit */
312 window->data = (window->data << 1);
314 /* If this packet was a success then set the low bit high */
316 window->success_counter++;
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 /
325 /* Tag this window as having been updated */
326 window->stamp = jiffies;
328 spin_unlock_irqrestore(&rs_priv->lock, flags);
332 static void rs_rate_init(void *priv_rate, void *priv_sta,
333 struct ieee80211_local *local, struct sta_info *sta)
337 IWL_DEBUG_RATE("enter\n");
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
344 for (i = IWL_RATE_COUNT - 1; i >= 0; i--) {
345 if (sta->supp_rates & (1 << i)) {
351 sta->last_txrate = sta->txrate;
353 IWL_DEBUG_RATE("leave\n");
356 static void *rs_alloc(struct ieee80211_local *local)
358 return local->hw.priv;
361 /* rate scale requires free function to be implmented */
362 static void rs_free(void *priv)
366 static void rs_clear(void *priv)
372 static void *rs_alloc_sta(void *priv, gfp_t gfp)
374 struct iwl_rate_scale_priv *rs_priv;
377 IWL_DEBUG_RATE("enter\n");
379 rs_priv = kzalloc(sizeof(struct iwl_rate_scale_priv), gfp);
381 IWL_DEBUG_RATE("leave: ENOMEM\n");
385 spin_lock_init(&rs_priv->lock);
387 rs_priv->start_rate = IWL_RATE_INVALID;
389 /* default to just 802.11b */
390 rs_priv->expected_tpt = iwl_expected_tpt_b;
392 rs_priv->last_partial_flush = jiffies;
393 rs_priv->last_flush = jiffies;
394 rs_priv->flush_time = IWL_RATE_FLUSH;
395 rs_priv->last_tx_packets = 0;
396 rs_priv->ibss_sta_added = 0;
398 init_timer(&rs_priv->rate_scale_flush);
399 rs_priv->rate_scale_flush.data = (unsigned long)rs_priv;
400 rs_priv->rate_scale_flush.function = &iwl_bg_rate_scale_flush;
402 for (i = 0; i < IWL_RATE_COUNT; i++)
403 iwl_clear_window(&rs_priv->win[i]);
405 IWL_DEBUG_RATE("leave\n");
410 static void rs_free_sta(void *priv, void *priv_sta)
412 struct iwl_rate_scale_priv *rs_priv = priv_sta;
414 IWL_DEBUG_RATE("enter\n");
415 del_timer_sync(&rs_priv->rate_scale_flush);
417 IWL_DEBUG_RATE("leave\n");
421 * rs_tx_status - Update rate control values based on Tx results
423 * NOTE: Uses iwl_priv->retry_rate for the # of retries attempted by
424 * the hardware for each rate.
426 static void rs_tx_status(void *priv_rate,
427 struct net_device *dev,
429 struct ieee80211_tx_status *tx_resp)
431 u8 retries, current_count;
432 int scale_rate_index, first_index, last_index;
434 struct sta_info *sta;
435 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
436 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
437 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
438 struct iwl_rate_scale_priv *rs_priv;
440 IWL_DEBUG_RATE("enter\n");
442 retries = tx_resp->retry_count;
444 first_index = tx_resp->control.tx_rate;
445 if ((first_index < 0) || (first_index >= IWL_RATE_COUNT)) {
446 IWL_DEBUG_RATE("leave: Rate out of bounds: %0x for %d\n",
447 tx_resp->control.tx_rate, first_index);
451 sta = sta_info_get(local, hdr->addr1);
452 if (!sta || !sta->rate_ctrl_priv) {
455 IWL_DEBUG_RATE("leave: No STA priv data to update!\n");
459 rs_priv = (void *)sta->rate_ctrl_priv;
461 rs_priv->tx_packets++;
463 scale_rate_index = first_index;
464 last_index = first_index;
467 * Update the window for each rate. We determine which rates
468 * were Tx'd based on the total number of retries vs. the number
469 * of retries configured for each rate -- currently set to the
470 * priv value 'retry_rate' vs. rate specific
472 * On exit from this while loop last_index indicates the rate
473 * at which the frame was finally transmitted (or failed if no
476 while (retries > 0) {
477 if (retries < priv->retry_rate) {
478 current_count = retries;
479 last_index = scale_rate_index;
481 current_count = priv->retry_rate;
482 last_index = iwl_get_prev_ieee_rate(scale_rate_index);
485 /* Update this rate accounting for as many retries
486 * as was used for it (per current_count) */
487 iwl_collect_tx_data(rs_priv,
488 &rs_priv->win[scale_rate_index],
490 IWL_DEBUG_RATE("Update rate %d for %d retries.\n",
491 scale_rate_index, current_count);
493 retries -= current_count;
497 iwl_get_prev_ieee_rate(scale_rate_index);
500 /* Update the last index window with success/failure based on ACK */
501 IWL_DEBUG_RATE("Update rate %d with %s.\n",
503 (tx_resp->flags & IEEE80211_TX_STATUS_ACK) ?
504 "success" : "failure");
505 iwl_collect_tx_data(rs_priv,
506 &rs_priv->win[last_index],
507 tx_resp->flags & IEEE80211_TX_STATUS_ACK, 1);
509 /* We updated the rate scale window -- if its been more than
510 * flush_time since the last run, schedule the flush
512 spin_lock_irqsave(&rs_priv->lock, flags);
514 if (!rs_priv->flush_pending &&
515 time_after(jiffies, rs_priv->last_partial_flush +
516 rs_priv->flush_time)) {
518 rs_priv->flush_pending = 1;
519 mod_timer(&rs_priv->rate_scale_flush,
520 jiffies + rs_priv->flush_time);
523 spin_unlock_irqrestore(&rs_priv->lock, flags);
527 IWL_DEBUG_RATE("leave\n");
532 static struct ieee80211_rate *iwl_get_lowest_rate(struct ieee80211_local
535 struct ieee80211_hw_mode *mode = local->oper_hw_mode;
538 for (i = 0; i < mode->num_rates; i++) {
539 struct ieee80211_rate *rate = &mode->rates[i];
541 if (rate->flags & IEEE80211_RATE_SUPPORTED)
545 return &mode->rates[0];
548 static u16 iwl_get_adjacent_rate(struct iwl_rate_scale_priv *rs_priv,
549 u8 index, u16 rate_mask, int phymode)
551 u8 high = IWL_RATE_INVALID;
552 u8 low = IWL_RATE_INVALID;
554 /* 802.11A walks to the next literal adjascent rate in
556 if (unlikely(phymode == MODE_IEEE80211A)) {
560 /* Find the previous rate that is in the rate mask */
562 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
563 if (rate_mask & mask) {
569 /* Find the next rate that is in the rate mask */
571 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
572 if (rate_mask & mask) {
578 return (high << 8) | low;
582 while (low != IWL_RATE_INVALID) {
584 low = iwl_rates[low].prev_rs_tgg;
586 low = iwl_rates[low].prev_rs;
587 if (low == IWL_RATE_INVALID)
589 if (rate_mask & (1 << low))
591 IWL_DEBUG_RATE("Skipping masked lower rate: %d\n", low);
595 while (high != IWL_RATE_INVALID) {
597 high = iwl_rates[high].next_rs_tgg;
599 high = iwl_rates[high].next_rs;
600 if (high == IWL_RATE_INVALID)
602 if (rate_mask & (1 << high))
604 IWL_DEBUG_RATE("Skipping masked higher rate: %d\n", high);
607 return (high << 8) | low;
611 * rs_get_rate - find the rate for the requested packet
613 * Returns the ieee80211_rate structure allocated by the driver.
615 * The rate control algorithm has no internal mapping between hw_mode's
616 * rate ordering and the rate ordering used by the rate control algorithm.
618 * The rate control algorithm uses a single table of rates that goes across
619 * the entire A/B/G spectrum vs. being limited to just one particular
622 * As such, we can't convert the index obtained below into the hw_mode's
623 * rate table and must reference the driver allocated rate table
626 static struct ieee80211_rate *rs_get_rate(void *priv_rate,
627 struct net_device *dev,
629 struct rate_control_extra *extra)
631 u8 low = IWL_RATE_INVALID;
632 u8 high = IWL_RATE_INVALID;
635 struct iwl_rate_scale_priv *rs_priv;
636 struct iwl_rate_scale_data *window = NULL;
637 int current_tpt = IWL_INVALID_VALUE;
638 int low_tpt = IWL_INVALID_VALUE;
639 int high_tpt = IWL_INVALID_VALUE;
643 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
644 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
645 struct sta_info *sta;
647 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
648 DECLARE_MAC_BUF(mac);
650 IWL_DEBUG_RATE("enter\n");
652 memset(extra, 0, sizeof(*extra));
654 fc = le16_to_cpu(hdr->frame_control);
655 if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
656 (is_multicast_ether_addr(hdr->addr1))) {
657 /* Send management frames and broadcast/multicast data using
659 /* TODO: this could probably be improved.. */
660 IWL_DEBUG_RATE("leave: lowest rate (not data or is "
663 return iwl_get_lowest_rate(local);
666 sta = sta_info_get(local, hdr->addr1);
667 if (!sta || !sta->rate_ctrl_priv) {
668 IWL_DEBUG_RATE("leave: No STA priv data to update!\n");
674 rate_mask = sta->supp_rates;
675 index = min(sta->txrate & 0xffff, IWL_RATE_COUNT - 1);
677 rs_priv = (void *)sta->rate_ctrl_priv;
679 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
680 !rs_priv->ibss_sta_added) {
681 u8 sta_id = iwl_hw_find_station(priv, hdr->addr1);
683 if (sta_id == IWL_INVALID_STATION) {
684 IWL_DEBUG_RATE("LQ: ADD station %s\n",
685 print_mac(mac, hdr->addr1));
686 sta_id = iwl_add_station(priv,
687 hdr->addr1, 0, CMD_ASYNC);
689 if (sta_id != IWL_INVALID_STATION)
690 rs_priv->ibss_sta_added = 1;
693 spin_lock_irqsave(&rs_priv->lock, flags);
695 if (rs_priv->start_rate != IWL_RATE_INVALID) {
696 index = rs_priv->start_rate;
697 rs_priv->start_rate = IWL_RATE_INVALID;
700 window = &(rs_priv->win[index]);
702 fail_count = window->counter - window->success_counter;
704 if (((fail_count <= IWL_RATE_MIN_FAILURE_TH) &&
705 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH))) {
706 window->average_tpt = IWL_INVALID_VALUE;
707 spin_unlock_irqrestore(&rs_priv->lock, flags);
709 IWL_DEBUG_RATE("Invalid average_tpt on rate %d: "
710 "counter: %d, success_counter: %d, "
711 "expected_tpt is %sNULL\n",
714 window->success_counter,
715 rs_priv->expected_tpt ? "not " : "");
720 window->average_tpt = ((window->success_ratio *
721 rs_priv->expected_tpt[index] + 64) / 128);
722 current_tpt = window->average_tpt;
724 high_low = iwl_get_adjacent_rate(rs_priv, index, rate_mask,
725 local->hw.conf.phymode);
726 low = high_low & 0xff;
727 high = (high_low >> 8) & 0xff;
729 if (low != IWL_RATE_INVALID)
730 low_tpt = rs_priv->win[low].average_tpt;
732 if (high != IWL_RATE_INVALID)
733 high_tpt = rs_priv->win[high].average_tpt;
735 spin_unlock_irqrestore(&rs_priv->lock, flags);
739 if ((window->success_ratio < IWL_RATE_DECREASE_TH) || !current_tpt) {
740 IWL_DEBUG_RATE("decrease rate because of low success_ratio\n");
742 } else if ((low_tpt == IWL_INVALID_VALUE) &&
743 (high_tpt == IWL_INVALID_VALUE))
745 else if ((low_tpt != IWL_INVALID_VALUE) &&
746 (high_tpt != IWL_INVALID_VALUE)
747 && (low_tpt < current_tpt)
748 && (high_tpt < current_tpt)) {
749 IWL_DEBUG_RATE("No action -- low [%d] & high [%d] < "
750 "current_tpt [%d]\n",
751 low_tpt, high_tpt, current_tpt);
754 if (high_tpt != IWL_INVALID_VALUE) {
755 if (high_tpt > current_tpt)
759 ("decrease rate because of high tpt\n");
762 } else if (low_tpt != IWL_INVALID_VALUE) {
763 if (low_tpt > current_tpt) {
765 ("decrease rate because of low tpt\n");
772 if ((window->success_ratio > IWL_RATE_HIGH_TH) ||
773 (current_tpt > window->average_tpt)) {
774 IWL_DEBUG_RATE("No action -- success_ratio [%d] > HIGH_TH or "
775 "current_tpt [%d] > average_tpt [%d]\n",
776 window->success_ratio,
777 current_tpt, window->average_tpt);
781 switch (scale_action) {
783 if (low != IWL_RATE_INVALID)
788 if (high != IWL_RATE_INVALID)
798 IWL_DEBUG_RATE("Selected %d (action %d) - low %d high %d\n",
799 index, scale_action, low, high);
803 sta->last_txrate = index;
804 sta->txrate = sta->last_txrate;
807 IWL_DEBUG_RATE("leave: %d\n", index);
809 return &priv->ieee_rates[index];
812 static struct rate_control_ops rs_ops = {
815 .tx_status = rs_tx_status,
816 .get_rate = rs_get_rate,
817 .rate_init = rs_rate_init,
821 .alloc_sta = rs_alloc_sta,
822 .free_sta = rs_free_sta,
825 int iwl_fill_rs_info(struct ieee80211_hw *hw, char *buf, u8 sta_id)
827 struct ieee80211_local *local = hw_to_local(hw);
828 struct iwl_priv *priv = hw->priv;
829 struct iwl_rate_scale_priv *rs_priv;
830 struct sta_info *sta;
833 u32 samples = 0, success = 0, good = 0;
834 unsigned long now = jiffies;
837 sta = sta_info_get(local, priv->stations[sta_id].sta.sta.addr);
838 if (!sta || !sta->rate_ctrl_priv) {
841 IWL_DEBUG_RATE("leave - no private rate data!\n");
843 IWL_DEBUG_RATE("leave - no station!\n");
844 return sprintf(buf, "station %d not found\n", sta_id);
847 rs_priv = (void *)sta->rate_ctrl_priv;
848 spin_lock_irqsave(&rs_priv->lock, flags);
849 i = IWL_RATE_54M_INDEX;
855 sprintf(&buf[count], " %2dMbs: ", iwl_rates[i].ieee / 2);
857 mask = (1ULL << (IWL_RATE_MAX_WINDOW - 1));
858 for (j = 0; j < IWL_RATE_MAX_WINDOW; j++, mask >>= 1)
860 (rs_priv->win[i].data & mask) ? '1' : '0';
862 samples += rs_priv->win[i].counter;
863 good += rs_priv->win[i].success_counter;
864 success += rs_priv->win[i].success_counter * iwl_rates[i].ieee;
866 if (rs_priv->win[i].stamp) {
868 jiffies_to_msecs(now - rs_priv->win[i].stamp);
870 if (delta > max_time)
873 count += sprintf(&buf[count], "%5dms\n", delta);
877 j = iwl_get_prev_ieee_rate(i);
882 spin_unlock_irqrestore(&rs_priv->lock, flags);
885 /* Display the average rate of all samples taken.
887 * NOTE: We multiple # of samples by 2 since the IEEE measurement
888 * added from iwl_rates is actually 2X the rate */
892 "\nAverage rate is %3d.%02dMbs over last %4dms\n"
893 "%3d%% success (%d good packets over %d tries)\n",
894 success / (2 * samples), (success * 5 / samples) % 10,
895 max_time, good * 100 / samples, good, samples);
897 count += sprintf(&buf[count], "\nAverage rate: 0Mbs\n");
902 void iwl_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id)
904 struct iwl_priv *priv = hw->priv;
907 struct ieee80211_local *local = hw_to_local(hw);
908 struct iwl_rate_scale_priv *rs_priv;
909 struct sta_info *sta;
911 IWL_DEBUG_RATE("enter\n");
913 if (!local->rate_ctrl->ops->name ||
914 strcmp(local->rate_ctrl->ops->name, RS_NAME)) {
915 IWL_WARNING("iwl-3945-rs not selected as rate control algo!\n");
916 IWL_DEBUG_RATE("leave - mac80211 picked the wrong RC algo.\n");
920 sta = sta_info_get(local, priv->stations[sta_id].sta.sta.addr);
921 if (!sta || !sta->rate_ctrl_priv) {
924 IWL_DEBUG_RATE("leave - no private rate data!\n");
928 rs_priv = (void *)sta->rate_ctrl_priv;
930 spin_lock_irqsave(&rs_priv->lock, flags);
933 switch (priv->phymode) {
934 case MODE_IEEE80211G:
935 if (priv->active_rxon.flags & RXON_FLG_TGG_PROTECT_MSK) {
937 rs_priv->expected_tpt = iwl_expected_tpt_g_prot;
939 rs_priv->expected_tpt = iwl_expected_tpt_g;
942 case MODE_IEEE80211A:
943 rs_priv->expected_tpt = iwl_expected_tpt_a;
947 IWL_WARNING("Invalid phymode. Defaulting to 802.11b\n");
948 case MODE_IEEE80211B:
949 rs_priv->expected_tpt = iwl_expected_tpt_b;
954 spin_unlock_irqrestore(&rs_priv->lock, flags);
956 rssi = priv->last_rx_rssi;
958 rssi = IWL_MIN_RSSI_VAL;
960 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RATE, "Network RSSI: %d\n", rssi);
962 rs_priv->start_rate = iwl_get_rate_index_by_rssi(rssi, priv->phymode);
964 IWL_DEBUG_RATE("leave: rssi %d assign rate index: "
965 "%d (plcp 0x%x)\n", rssi, rs_priv->start_rate,
966 iwl_rates[rs_priv->start_rate].plcp);
969 void iwl_rate_control_register(struct ieee80211_hw *hw)
971 ieee80211_rate_control_register(&rs_ops);
974 void iwl_rate_control_unregister(struct ieee80211_hw *hw)
976 ieee80211_rate_control_unregister(&rs_ops);