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 7, 13, 35, 58, 0, 0, 76, 104, 130, 168, 191, 202
77 static s32 iwl_expected_tpt_g_prot[IWL_RATE_COUNT] = {
78 7, 13, 35, 58, 0, 0, 0, 80, 93, 113, 123, 125
81 static s32 iwl_expected_tpt_a[IWL_RATE_COUNT] = {
82 0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186
85 static s32 iwl_expected_tpt_b[IWL_RATE_COUNT] = {
86 7, 13, 35, 58, 0, 0, 0, 0, 0, 0, 0, 0
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 /* 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;
357 IWL_DEBUG_RATE("leave\n");
360 static void *rs_alloc(struct ieee80211_local *local)
362 return local->hw.priv;
365 /* rate scale requires free function to be implmented */
366 static void rs_free(void *priv)
370 static void rs_clear(void *priv)
376 static void *rs_alloc_sta(void *priv, gfp_t gfp)
378 struct iwl_rate_scale_priv *rs_priv;
381 IWL_DEBUG_RATE("enter\n");
383 rs_priv = kzalloc(sizeof(struct iwl_rate_scale_priv), gfp);
385 IWL_DEBUG_RATE("leave: ENOMEM\n");
389 spin_lock_init(&rs_priv->lock);
391 rs_priv->start_rate = IWL_RATE_INVALID;
393 /* default to just 802.11b */
394 rs_priv->expected_tpt = iwl_expected_tpt_b;
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;
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;
406 for (i = 0; i < IWL_RATE_COUNT; i++)
407 iwl_clear_window(&rs_priv->win[i]);
409 IWL_DEBUG_RATE("leave\n");
414 static void rs_free_sta(void *priv, void *priv_sta)
416 struct iwl_rate_scale_priv *rs_priv = priv_sta;
418 IWL_DEBUG_RATE("enter\n");
419 del_timer_sync(&rs_priv->rate_scale_flush);
421 IWL_DEBUG_RATE("leave\n");
426 * get ieee prev rate from rate scale table.
427 * for A and B mode we need to overright prev
430 static int rs_adjust_next_rate(struct iwl_priv *priv, int rate)
432 int next_rate = iwl_get_prev_ieee_rate(rate);
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;
441 case MODE_IEEE80211B:
442 if (rate == IWL_RATE_11M_INDEX_TABLE)
443 next_rate = IWL_RATE_5M_INDEX_TABLE;
452 * rs_tx_status - Update rate control values based on Tx results
454 * NOTE: Uses iwl_priv->retry_rate for the # of retries attempted by
455 * the hardware for each rate.
457 static void rs_tx_status(void *priv_rate,
458 struct net_device *dev,
460 struct ieee80211_tx_status *tx_resp)
462 u8 retries, current_count;
463 int scale_rate_index, first_index, last_index;
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;
471 IWL_DEBUG_RATE("enter\n");
473 retries = tx_resp->retry_count;
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);
482 sta = sta_info_get(local, hdr->addr1);
483 if (!sta || !sta->rate_ctrl_priv) {
486 IWL_DEBUG_RATE("leave: No STA priv data to update!\n");
490 rs_priv = (void *)sta->rate_ctrl_priv;
492 rs_priv->tx_packets++;
494 scale_rate_index = first_index;
495 last_index = first_index;
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
503 * On exit from this while loop last_index indicates the rate
504 * at which the frame was finally transmitted (or failed if no
507 while (retries > 0) {
508 if (retries < priv->retry_rate) {
509 current_count = retries;
510 last_index = scale_rate_index;
512 current_count = priv->retry_rate;
513 last_index = rs_adjust_next_rate(priv,
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],
522 IWL_DEBUG_RATE("Update rate %d for %d retries.\n",
523 scale_rate_index, current_count);
525 retries -= current_count;
529 rs_adjust_next_rate(priv, scale_rate_index);
533 /* Update the last index window with success/failure based on ACK */
534 IWL_DEBUG_RATE("Update rate %d with %s.\n",
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);
542 /* We updated the rate scale window -- if its been more than
543 * flush_time since the last run, schedule the flush
545 spin_lock_irqsave(&rs_priv->lock, flags);
547 if (!rs_priv->flush_pending &&
548 time_after(jiffies, rs_priv->last_partial_flush +
549 rs_priv->flush_time)) {
551 rs_priv->flush_pending = 1;
552 mod_timer(&rs_priv->rate_scale_flush,
553 jiffies + rs_priv->flush_time);
556 spin_unlock_irqrestore(&rs_priv->lock, flags);
560 IWL_DEBUG_RATE("leave\n");
565 static struct ieee80211_rate *iwl_get_lowest_rate(struct ieee80211_local
568 struct ieee80211_hw_mode *mode = local->oper_hw_mode;
571 for (i = 0; i < mode->num_rates; i++) {
572 struct ieee80211_rate *rate = &mode->rates[i];
574 if (rate->flags & IEEE80211_RATE_SUPPORTED)
578 return &mode->rates[0];
581 static u16 iwl_get_adjacent_rate(struct iwl_rate_scale_priv *rs_priv,
582 u8 index, u16 rate_mask, int phymode)
584 u8 high = IWL_RATE_INVALID;
585 u8 low = IWL_RATE_INVALID;
587 /* 802.11A walks to the next literal adjascent rate in
589 if (unlikely(phymode == MODE_IEEE80211A)) {
593 /* Find the previous rate that is in the rate mask */
595 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
596 if (rate_mask & mask) {
602 /* Find the next rate that is in the rate mask */
604 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
605 if (rate_mask & mask) {
611 return (high << 8) | low;
615 while (low != IWL_RATE_INVALID) {
617 low = iwl_rates[low].prev_rs_tgg;
619 low = iwl_rates[low].prev_rs;
620 if (low == IWL_RATE_INVALID)
622 if (rate_mask & (1 << low))
624 IWL_DEBUG_RATE("Skipping masked lower rate: %d\n", low);
628 while (high != IWL_RATE_INVALID) {
630 high = iwl_rates[high].next_rs_tgg;
632 high = iwl_rates[high].next_rs;
633 if (high == IWL_RATE_INVALID)
635 if (rate_mask & (1 << high))
637 IWL_DEBUG_RATE("Skipping masked higher rate: %d\n", high);
640 return (high << 8) | low;
644 * rs_get_rate - find the rate for the requested packet
646 * Returns the ieee80211_rate structure allocated by the driver.
648 * The rate control algorithm has no internal mapping between hw_mode's
649 * rate ordering and the rate ordering used by the rate control algorithm.
651 * The rate control algorithm uses a single table of rates that goes across
652 * the entire A/B/G spectrum vs. being limited to just one particular
655 * As such, we can't convert the index obtained below into the hw_mode's
656 * rate table and must reference the driver allocated rate table
659 static struct ieee80211_rate *rs_get_rate(void *priv_rate,
660 struct net_device *dev,
662 struct rate_control_extra *extra)
664 u8 low = IWL_RATE_INVALID;
665 u8 high = IWL_RATE_INVALID;
668 struct iwl_rate_scale_priv *rs_priv;
669 struct iwl_rate_scale_data *window = NULL;
670 int current_tpt = IWL_INVALID_VALUE;
671 int low_tpt = IWL_INVALID_VALUE;
672 int high_tpt = IWL_INVALID_VALUE;
676 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
677 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
678 struct sta_info *sta;
680 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
681 DECLARE_MAC_BUF(mac);
683 IWL_DEBUG_RATE("enter\n");
685 memset(extra, 0, sizeof(*extra));
687 fc = le16_to_cpu(hdr->frame_control);
688 if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
689 (is_multicast_ether_addr(hdr->addr1))) {
690 /* Send management frames and broadcast/multicast data using
692 /* TODO: this could probably be improved.. */
693 IWL_DEBUG_RATE("leave: lowest rate (not data or is "
696 return iwl_get_lowest_rate(local);
699 sta = sta_info_get(local, hdr->addr1);
700 if (!sta || !sta->rate_ctrl_priv) {
701 IWL_DEBUG_RATE("leave: No STA priv data to update!\n");
707 rate_mask = sta->supp_rates;
708 index = min(sta->last_txrate & 0xffff, IWL_RATE_COUNT - 1);
710 if (priv->phymode == (u8) MODE_IEEE80211A)
711 rate_mask = rate_mask << IWL_FIRST_OFDM_RATE;
713 rs_priv = (void *)sta->rate_ctrl_priv;
715 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
716 !rs_priv->ibss_sta_added) {
717 u8 sta_id = iwl_hw_find_station(priv, hdr->addr1);
719 if (sta_id == IWL_INVALID_STATION) {
720 IWL_DEBUG_RATE("LQ: ADD station %s\n",
721 print_mac(mac, hdr->addr1));
722 sta_id = iwl_add_station(priv,
723 hdr->addr1, 0, CMD_ASYNC);
725 if (sta_id != IWL_INVALID_STATION)
726 rs_priv->ibss_sta_added = 1;
729 spin_lock_irqsave(&rs_priv->lock, flags);
731 if (rs_priv->start_rate != IWL_RATE_INVALID) {
732 index = rs_priv->start_rate;
733 rs_priv->start_rate = IWL_RATE_INVALID;
736 window = &(rs_priv->win[index]);
738 fail_count = window->counter - window->success_counter;
740 if (((fail_count <= IWL_RATE_MIN_FAILURE_TH) &&
741 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH))) {
742 window->average_tpt = IWL_INVALID_VALUE;
743 spin_unlock_irqrestore(&rs_priv->lock, flags);
745 IWL_DEBUG_RATE("Invalid average_tpt on rate %d: "
746 "counter: %d, success_counter: %d, "
747 "expected_tpt is %sNULL\n",
750 window->success_counter,
751 rs_priv->expected_tpt ? "not " : "");
756 window->average_tpt = ((window->success_ratio *
757 rs_priv->expected_tpt[index] + 64) / 128);
758 current_tpt = window->average_tpt;
760 high_low = iwl_get_adjacent_rate(rs_priv, index, rate_mask,
761 local->hw.conf.phymode);
762 low = high_low & 0xff;
763 high = (high_low >> 8) & 0xff;
765 if (low != IWL_RATE_INVALID)
766 low_tpt = rs_priv->win[low].average_tpt;
768 if (high != IWL_RATE_INVALID)
769 high_tpt = rs_priv->win[high].average_tpt;
771 spin_unlock_irqrestore(&rs_priv->lock, flags);
775 if ((window->success_ratio < IWL_RATE_DECREASE_TH) || !current_tpt) {
776 IWL_DEBUG_RATE("decrease rate because of low success_ratio\n");
778 } else if ((low_tpt == IWL_INVALID_VALUE) &&
779 (high_tpt == IWL_INVALID_VALUE))
781 else if ((low_tpt != IWL_INVALID_VALUE) &&
782 (high_tpt != IWL_INVALID_VALUE)
783 && (low_tpt < current_tpt)
784 && (high_tpt < current_tpt)) {
785 IWL_DEBUG_RATE("No action -- low [%d] & high [%d] < "
786 "current_tpt [%d]\n",
787 low_tpt, high_tpt, current_tpt);
790 if (high_tpt != IWL_INVALID_VALUE) {
791 if (high_tpt > current_tpt)
795 ("decrease rate because of high tpt\n");
798 } else if (low_tpt != IWL_INVALID_VALUE) {
799 if (low_tpt > current_tpt) {
801 ("decrease rate because of low tpt\n");
808 if ((window->success_ratio > IWL_RATE_HIGH_TH) ||
809 (current_tpt > window->average_tpt)) {
810 IWL_DEBUG_RATE("No action -- success_ratio [%d] > HIGH_TH or "
811 "current_tpt [%d] > average_tpt [%d]\n",
812 window->success_ratio,
813 current_tpt, window->average_tpt);
817 switch (scale_action) {
819 if (low != IWL_RATE_INVALID)
824 if (high != IWL_RATE_INVALID)
834 IWL_DEBUG_RATE("Selected %d (action %d) - low %d high %d\n",
835 index, scale_action, low, high);
839 sta->last_txrate = index;
840 if (priv->phymode == (u8) MODE_IEEE80211A)
841 sta->txrate = sta->last_txrate - IWL_FIRST_OFDM_RATE;
843 sta->txrate = sta->last_txrate;
847 IWL_DEBUG_RATE("leave: %d\n", index);
849 return &priv->ieee_rates[index];
852 static struct rate_control_ops rs_ops = {
855 .tx_status = rs_tx_status,
856 .get_rate = rs_get_rate,
857 .rate_init = rs_rate_init,
861 .alloc_sta = rs_alloc_sta,
862 .free_sta = rs_free_sta,
865 int iwl_fill_rs_info(struct ieee80211_hw *hw, char *buf, u8 sta_id)
867 struct ieee80211_local *local = hw_to_local(hw);
868 struct iwl_priv *priv = hw->priv;
869 struct iwl_rate_scale_priv *rs_priv;
870 struct sta_info *sta;
873 u32 samples = 0, success = 0, good = 0;
874 unsigned long now = jiffies;
877 sta = sta_info_get(local, priv->stations[sta_id].sta.sta.addr);
878 if (!sta || !sta->rate_ctrl_priv) {
881 IWL_DEBUG_RATE("leave - no private rate data!\n");
883 IWL_DEBUG_RATE("leave - no station!\n");
884 return sprintf(buf, "station %d not found\n", sta_id);
887 rs_priv = (void *)sta->rate_ctrl_priv;
888 spin_lock_irqsave(&rs_priv->lock, flags);
889 i = IWL_RATE_54M_INDEX;
895 sprintf(&buf[count], " %2dMbs: ", iwl_rates[i].ieee / 2);
897 mask = (1ULL << (IWL_RATE_MAX_WINDOW - 1));
898 for (j = 0; j < IWL_RATE_MAX_WINDOW; j++, mask >>= 1)
900 (rs_priv->win[i].data & mask) ? '1' : '0';
902 samples += rs_priv->win[i].counter;
903 good += rs_priv->win[i].success_counter;
904 success += rs_priv->win[i].success_counter * iwl_rates[i].ieee;
906 if (rs_priv->win[i].stamp) {
908 jiffies_to_msecs(now - rs_priv->win[i].stamp);
910 if (delta > max_time)
913 count += sprintf(&buf[count], "%5dms\n", delta);
917 j = iwl_get_prev_ieee_rate(i);
922 spin_unlock_irqrestore(&rs_priv->lock, flags);
925 /* Display the average rate of all samples taken.
927 * NOTE: We multiple # of samples by 2 since the IEEE measurement
928 * added from iwl_rates is actually 2X the rate */
932 "\nAverage rate is %3d.%02dMbs over last %4dms\n"
933 "%3d%% success (%d good packets over %d tries)\n",
934 success / (2 * samples), (success * 5 / samples) % 10,
935 max_time, good * 100 / samples, good, samples);
937 count += sprintf(&buf[count], "\nAverage rate: 0Mbs\n");
942 void iwl_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id)
944 struct iwl_priv *priv = hw->priv;
947 struct ieee80211_local *local = hw_to_local(hw);
948 struct iwl_rate_scale_priv *rs_priv;
949 struct sta_info *sta;
951 IWL_DEBUG_RATE("enter\n");
953 if (!local->rate_ctrl->ops->name ||
954 strcmp(local->rate_ctrl->ops->name, RS_NAME)) {
955 IWL_WARNING("iwl-3945-rs not selected as rate control algo!\n");
956 IWL_DEBUG_RATE("leave - mac80211 picked the wrong RC algo.\n");
960 sta = sta_info_get(local, priv->stations[sta_id].sta.sta.addr);
961 if (!sta || !sta->rate_ctrl_priv) {
964 IWL_DEBUG_RATE("leave - no private rate data!\n");
968 rs_priv = (void *)sta->rate_ctrl_priv;
970 spin_lock_irqsave(&rs_priv->lock, flags);
973 switch (priv->phymode) {
974 case MODE_IEEE80211G:
975 if (priv->active_rxon.flags & RXON_FLG_TGG_PROTECT_MSK) {
977 rs_priv->expected_tpt = iwl_expected_tpt_g_prot;
979 rs_priv->expected_tpt = iwl_expected_tpt_g;
982 case MODE_IEEE80211A:
983 rs_priv->expected_tpt = iwl_expected_tpt_a;
987 IWL_WARNING("Invalid phymode. Defaulting to 802.11b\n");
988 case MODE_IEEE80211B:
989 rs_priv->expected_tpt = iwl_expected_tpt_b;
994 spin_unlock_irqrestore(&rs_priv->lock, flags);
996 rssi = priv->last_rx_rssi;
998 rssi = IWL_MIN_RSSI_VAL;
1000 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RATE, "Network RSSI: %d\n", rssi);
1002 rs_priv->start_rate = iwl_get_rate_index_by_rssi(rssi, priv->phymode);
1004 IWL_DEBUG_RATE("leave: rssi %d assign rate index: "
1005 "%d (plcp 0x%x)\n", rssi, rs_priv->start_rate,
1006 iwl_rates[rs_priv->start_rate].plcp);
1009 void iwl_rate_control_register(struct ieee80211_hw *hw)
1011 ieee80211_rate_control_register(&rs_ops);
1014 void iwl_rate_control_unregister(struct ieee80211_hw *hw)
1016 ieee80211_rate_control_unregister(&rs_ops);