1 /******************************************************************************
3 * Copyright(c) 2005 - 2008 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>
33 #include <linux/netdevice.h>
34 #include <linux/etherdevice.h>
35 #include <linux/delay.h>
37 #include <linux/workqueue.h>
39 #include "../net/mac80211/rate.h"
43 #define RS_NAME "iwl-3945-rs"
45 struct iwl3945_rate_scale_data {
54 struct iwl3945_rs_sta {
57 unsigned long last_partial_flush;
58 unsigned long last_flush;
66 struct timer_list rate_scale_flush;
67 struct iwl3945_rate_scale_data win[IWL_RATE_COUNT];
70 static s32 iwl3945_expected_tpt_g[IWL_RATE_COUNT] = {
71 7, 13, 35, 58, 0, 0, 76, 104, 130, 168, 191, 202
74 static s32 iwl3945_expected_tpt_g_prot[IWL_RATE_COUNT] = {
75 7, 13, 35, 58, 0, 0, 0, 80, 93, 113, 123, 125
78 static s32 iwl3945_expected_tpt_a[IWL_RATE_COUNT] = {
79 0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186
82 static s32 iwl3945_expected_tpt_b[IWL_RATE_COUNT] = {
83 7, 13, 35, 58, 0, 0, 0, 0, 0, 0, 0, 0
86 struct iwl3945_tpt_entry {
91 static struct iwl3945_tpt_entry iwl3945_tpt_table_a[] = {
92 {-60, IWL_RATE_54M_INDEX},
93 {-64, IWL_RATE_48M_INDEX},
94 {-72, IWL_RATE_36M_INDEX},
95 {-80, IWL_RATE_24M_INDEX},
96 {-84, IWL_RATE_18M_INDEX},
97 {-85, IWL_RATE_12M_INDEX},
98 {-87, IWL_RATE_9M_INDEX},
99 {-89, IWL_RATE_6M_INDEX}
102 static struct iwl3945_tpt_entry iwl3945_tpt_table_g[] = {
103 {-60, IWL_RATE_54M_INDEX},
104 {-64, IWL_RATE_48M_INDEX},
105 {-68, IWL_RATE_36M_INDEX},
106 {-80, IWL_RATE_24M_INDEX},
107 {-84, IWL_RATE_18M_INDEX},
108 {-85, IWL_RATE_12M_INDEX},
109 {-86, IWL_RATE_11M_INDEX},
110 {-88, IWL_RATE_5M_INDEX},
111 {-90, IWL_RATE_2M_INDEX},
112 {-92, IWL_RATE_1M_INDEX}
115 #define IWL_RATE_MAX_WINDOW 62
116 #define IWL_RATE_FLUSH (3*HZ/10)
117 #define IWL_RATE_WIN_FLUSH (HZ/2)
118 #define IWL_RATE_HIGH_TH 11520
119 #define IWL_RATE_MIN_FAILURE_TH 8
120 #define IWL_RATE_MIN_SUCCESS_TH 8
121 #define IWL_RATE_DECREASE_TH 1920
123 static u8 iwl3945_get_rate_index_by_rssi(s32 rssi, enum ieee80211_band band)
127 struct iwl3945_tpt_entry *tpt_table = NULL;
129 if ((rssi < IWL_MIN_RSSI_VAL) || (rssi > IWL_MAX_RSSI_VAL))
130 rssi = IWL_MIN_RSSI_VAL;
133 case IEEE80211_BAND_2GHZ:
134 tpt_table = iwl3945_tpt_table_g;
135 table_size = ARRAY_SIZE(iwl3945_tpt_table_g);
138 case IEEE80211_BAND_5GHZ:
139 tpt_table = iwl3945_tpt_table_a;
140 table_size = ARRAY_SIZE(iwl3945_tpt_table_a);
148 while ((index < table_size) && (rssi < tpt_table[index].min_rssi))
151 index = min(index, (table_size - 1));
153 return tpt_table[index].index;
156 static void iwl3945_clear_window(struct iwl3945_rate_scale_data *window)
159 window->success_counter = 0;
160 window->success_ratio = -1;
162 window->average_tpt = IWL_INV_TPT;
167 * iwl3945_rate_scale_flush_windows - flush out the rate scale windows
169 * Returns the number of windows that have gathered data but were
170 * not flushed. If there were any that were not flushed, then
171 * reschedule the rate flushing routine.
173 static int iwl3945_rate_scale_flush_windows(struct iwl3945_rs_sta *rs_sta)
180 * For each rate, if we have collected data on that rate
181 * and it has been more than IWL_RATE_WIN_FLUSH
182 * since we flushed, clear out the gathered statistics
184 for (i = 0; i < IWL_RATE_COUNT; i++) {
185 if (!rs_sta->win[i].counter)
188 spin_lock_irqsave(&rs_sta->lock, flags);
189 if (time_after(jiffies, rs_sta->win[i].stamp +
190 IWL_RATE_WIN_FLUSH)) {
191 IWL_DEBUG_RATE("flushing %d samples of rate "
193 rs_sta->win[i].counter, i);
194 iwl3945_clear_window(&rs_sta->win[i]);
197 spin_unlock_irqrestore(&rs_sta->lock, flags);
203 #define IWL_RATE_FLUSH_MAX 5000 /* msec */
204 #define IWL_RATE_FLUSH_MIN 50 /* msec */
206 static void iwl3945_bg_rate_scale_flush(unsigned long data)
208 struct iwl3945_rs_sta *rs_sta = (void *)data;
211 u32 packet_count, duration, pps;
213 IWL_DEBUG_RATE("enter\n");
215 unflushed = iwl3945_rate_scale_flush_windows(rs_sta);
217 spin_lock_irqsave(&rs_sta->lock, flags);
219 rs_sta->flush_pending = 0;
221 /* Number of packets Rx'd since last time this timer ran */
222 packet_count = (rs_sta->tx_packets - rs_sta->last_tx_packets) + 1;
224 rs_sta->last_tx_packets = rs_sta->tx_packets + 1;
228 jiffies_to_msecs(jiffies - rs_sta->last_partial_flush);
229 /* duration = jiffies_to_msecs(rs_sta->flush_time); */
231 IWL_DEBUG_RATE("Tx'd %d packets in %dms\n",
232 packet_count, duration);
234 /* Determine packets per second */
236 pps = (packet_count * 1000) / duration;
241 duration = IWL_RATE_FLUSH_MAX / pps;
242 if (duration < IWL_RATE_FLUSH_MIN)
243 duration = IWL_RATE_FLUSH_MIN;
245 duration = IWL_RATE_FLUSH_MAX;
247 rs_sta->flush_time = msecs_to_jiffies(duration);
249 IWL_DEBUG_RATE("new flush period: %d msec ave %d\n",
250 duration, packet_count);
252 mod_timer(&rs_sta->rate_scale_flush, jiffies +
255 rs_sta->last_partial_flush = jiffies;
258 /* If there weren't any unflushed entries, we don't schedule the timer
261 rs_sta->last_flush = jiffies;
263 spin_unlock_irqrestore(&rs_sta->lock, flags);
265 IWL_DEBUG_RATE("leave\n");
269 * iwl3945_collect_tx_data - Update the success/failure sliding window
271 * We keep a sliding window of the last 64 packets transmitted
272 * at this rate. window->data contains the bitmask of successful
275 static void iwl3945_collect_tx_data(struct iwl3945_rs_sta *rs_sta,
276 struct iwl3945_rate_scale_data *window,
277 int success, int retries)
282 IWL_DEBUG_RATE("leave: retries == 0 -- should be at least 1\n");
287 spin_lock_irqsave(&rs_sta->lock, flags);
289 /* If we have filled up the window then subtract one from the
290 * success counter if the high-bit is counting toward
292 if (window->counter == IWL_RATE_MAX_WINDOW) {
293 if (window->data & (1ULL << (IWL_RATE_MAX_WINDOW - 1)))
294 window->success_counter--;
298 /* Slide the window to the left one bit */
299 window->data = (window->data << 1);
301 /* If this packet was a success then set the low bit high */
303 window->success_counter++;
307 /* window->counter can't be 0 -- it is either >0 or
308 * IWL_RATE_MAX_WINDOW */
309 window->success_ratio = 12800 * window->success_counter /
312 /* Tag this window as having been updated */
313 window->stamp = jiffies;
315 spin_unlock_irqrestore(&rs_sta->lock, flags);
319 static void rs_rate_init(void *priv_rate, void *priv_sta,
320 struct ieee80211_local *local, struct sta_info *sta)
324 IWL_DEBUG_RATE("enter\n");
326 /* TODO: what is a good starting rate for STA? About middle? Maybe not
327 * the lowest or the highest rate.. Could consider using RSSI from
328 * previous packets? Need to have IEEE 802.1X auth succeed immediately
331 for (i = IWL_RATE_COUNT - 1; i >= 0; i--) {
332 if (sta->supp_rates[local->hw.conf.channel->band] & (1 << i)) {
338 sta->last_txrate_idx = sta->txrate_idx;
340 /* For 5 GHz band it start at IWL_FIRST_OFDM_RATE */
341 if (local->hw.conf.channel->band == IEEE80211_BAND_5GHZ)
342 sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
344 IWL_DEBUG_RATE("leave\n");
347 static void *rs_alloc(struct ieee80211_local *local)
349 return local->hw.priv;
352 /* rate scale requires free function to be implemented */
353 static void rs_free(void *priv)
357 static void rs_clear(void *priv)
363 static void *rs_alloc_sta(void *priv, gfp_t gfp)
365 struct iwl3945_rs_sta *rs_sta;
368 IWL_DEBUG_RATE("enter\n");
370 rs_sta = kzalloc(sizeof(struct iwl3945_rs_sta), gfp);
372 IWL_DEBUG_RATE("leave: ENOMEM\n");
376 spin_lock_init(&rs_sta->lock);
378 rs_sta->start_rate = IWL_RATE_INVALID;
380 /* default to just 802.11b */
381 rs_sta->expected_tpt = iwl3945_expected_tpt_b;
383 rs_sta->last_partial_flush = jiffies;
384 rs_sta->last_flush = jiffies;
385 rs_sta->flush_time = IWL_RATE_FLUSH;
386 rs_sta->last_tx_packets = 0;
387 rs_sta->ibss_sta_added = 0;
389 init_timer(&rs_sta->rate_scale_flush);
390 rs_sta->rate_scale_flush.data = (unsigned long)rs_sta;
391 rs_sta->rate_scale_flush.function = &iwl3945_bg_rate_scale_flush;
393 for (i = 0; i < IWL_RATE_COUNT; i++)
394 iwl3945_clear_window(&rs_sta->win[i]);
396 IWL_DEBUG_RATE("leave\n");
401 static void rs_free_sta(void *priv, void *priv_sta)
403 struct iwl3945_rs_sta *rs_sta = priv_sta;
405 IWL_DEBUG_RATE("enter\n");
406 del_timer_sync(&rs_sta->rate_scale_flush);
408 IWL_DEBUG_RATE("leave\n");
413 * get ieee prev rate from rate scale table.
414 * for A and B mode we need to overright prev
417 static int rs_adjust_next_rate(struct iwl3945_priv *priv, int rate)
419 int next_rate = iwl3945_get_prev_ieee_rate(rate);
421 switch (priv->band) {
422 case IEEE80211_BAND_5GHZ:
423 if (rate == IWL_RATE_12M_INDEX)
424 next_rate = IWL_RATE_9M_INDEX;
425 else if (rate == IWL_RATE_6M_INDEX)
426 next_rate = IWL_RATE_6M_INDEX;
428 /* XXX cannot be invoked in current mac80211 so not a regression
429 case MODE_IEEE80211B:
430 if (rate == IWL_RATE_11M_INDEX_TABLE)
431 next_rate = IWL_RATE_5M_INDEX_TABLE;
441 * rs_tx_status - Update rate control values based on Tx results
443 * NOTE: Uses iwl3945_priv->retry_rate for the # of retries attempted by
444 * the hardware for each rate.
446 static void rs_tx_status(void *priv_rate,
447 struct net_device *dev,
449 struct ieee80211_tx_status *tx_resp)
451 u8 retries, current_count;
452 int scale_rate_index, first_index, last_index;
454 struct sta_info *sta;
455 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
456 struct iwl3945_priv *priv = (struct iwl3945_priv *)priv_rate;
457 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
458 struct iwl3945_rs_sta *rs_sta;
459 struct ieee80211_supported_band *sband;
461 IWL_DEBUG_RATE("enter\n");
463 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
466 retries = tx_resp->retry_count;
467 first_index = tx_resp->control.tx_rate->hw_value;
468 if ((first_index < 0) || (first_index >= IWL_RATE_COUNT)) {
469 IWL_DEBUG_RATE("leave: Rate out of bounds: %d\n", first_index);
475 sta = sta_info_get(local, hdr->addr1);
476 if (!sta || !sta->rate_ctrl_priv) {
478 IWL_DEBUG_RATE("leave: No STA priv data to update!\n");
482 rs_sta = (void *)sta->rate_ctrl_priv;
484 rs_sta->tx_packets++;
486 scale_rate_index = first_index;
487 last_index = first_index;
490 * Update the window for each rate. We determine which rates
491 * were Tx'd based on the total number of retries vs. the number
492 * of retries configured for each rate -- currently set to the
493 * priv value 'retry_rate' vs. rate specific
495 * On exit from this while loop last_index indicates the rate
496 * at which the frame was finally transmitted (or failed if no
499 while (retries > 0) {
500 if (retries < priv->retry_rate) {
501 current_count = retries;
502 last_index = scale_rate_index;
504 current_count = priv->retry_rate;
505 last_index = rs_adjust_next_rate(priv,
509 /* Update this rate accounting for as many retries
510 * as was used for it (per current_count) */
511 iwl3945_collect_tx_data(rs_sta,
512 &rs_sta->win[scale_rate_index],
514 IWL_DEBUG_RATE("Update rate %d for %d retries.\n",
515 scale_rate_index, current_count);
517 retries -= current_count;
521 rs_adjust_next_rate(priv, scale_rate_index);
525 /* Update the last index window with success/failure based on ACK */
526 IWL_DEBUG_RATE("Update rate %d with %s.\n",
528 (tx_resp->flags & IEEE80211_TX_STATUS_ACK) ?
529 "success" : "failure");
530 iwl3945_collect_tx_data(rs_sta,
531 &rs_sta->win[last_index],
532 tx_resp->flags & IEEE80211_TX_STATUS_ACK, 1);
534 /* We updated the rate scale window -- if its been more than
535 * flush_time since the last run, schedule the flush
537 spin_lock_irqsave(&rs_sta->lock, flags);
539 if (!rs_sta->flush_pending &&
540 time_after(jiffies, rs_sta->last_partial_flush +
541 rs_sta->flush_time)) {
543 rs_sta->flush_pending = 1;
544 mod_timer(&rs_sta->rate_scale_flush,
545 jiffies + rs_sta->flush_time);
548 spin_unlock_irqrestore(&rs_sta->lock, flags);
552 IWL_DEBUG_RATE("leave\n");
557 static u16 iwl3945_get_adjacent_rate(struct iwl3945_rs_sta *rs_sta,
558 u8 index, u16 rate_mask, enum ieee80211_band band)
560 u8 high = IWL_RATE_INVALID;
561 u8 low = IWL_RATE_INVALID;
563 /* 802.11A walks to the next literal adjacent rate in
565 if (unlikely(band == IEEE80211_BAND_5GHZ)) {
569 /* Find the previous rate that is in the rate mask */
571 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
572 if (rate_mask & mask) {
578 /* Find the next rate that is in the rate mask */
580 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
581 if (rate_mask & mask) {
587 return (high << 8) | low;
591 while (low != IWL_RATE_INVALID) {
593 low = iwl3945_rates[low].prev_rs_tgg;
595 low = iwl3945_rates[low].prev_rs;
596 if (low == IWL_RATE_INVALID)
598 if (rate_mask & (1 << low))
600 IWL_DEBUG_RATE("Skipping masked lower rate: %d\n", low);
604 while (high != IWL_RATE_INVALID) {
606 high = iwl3945_rates[high].next_rs_tgg;
608 high = iwl3945_rates[high].next_rs;
609 if (high == IWL_RATE_INVALID)
611 if (rate_mask & (1 << high))
613 IWL_DEBUG_RATE("Skipping masked higher rate: %d\n", high);
616 return (high << 8) | low;
620 * rs_get_rate - find the rate for the requested packet
622 * Returns the ieee80211_rate structure allocated by the driver.
624 * The rate control algorithm has no internal mapping between hw_mode's
625 * rate ordering and the rate ordering used by the rate control algorithm.
627 * The rate control algorithm uses a single table of rates that goes across
628 * the entire A/B/G spectrum vs. being limited to just one particular
631 * As such, we can't convert the index obtained below into the hw_mode's
632 * rate table and must reference the driver allocated rate table
635 static void rs_get_rate(void *priv_rate, struct net_device *dev,
636 struct ieee80211_supported_band *sband,
638 struct rate_selection *sel)
640 u8 low = IWL_RATE_INVALID;
641 u8 high = IWL_RATE_INVALID;
644 struct iwl3945_rs_sta *rs_sta;
645 struct iwl3945_rate_scale_data *window = NULL;
646 int current_tpt = IWL_INV_TPT;
647 int low_tpt = IWL_INV_TPT;
648 int high_tpt = IWL_INV_TPT;
652 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
653 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
654 struct sta_info *sta;
656 struct iwl3945_priv *priv = (struct iwl3945_priv *)priv_rate;
657 DECLARE_MAC_BUF(mac);
659 IWL_DEBUG_RATE("enter\n");
663 sta = sta_info_get(local, hdr->addr1);
665 /* Send management frames and broadcast/multicast data using lowest
667 fc = le16_to_cpu(hdr->frame_control);
668 if ((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA ||
669 is_multicast_ether_addr(hdr->addr1) ||
670 !sta || !sta->rate_ctrl_priv) {
671 IWL_DEBUG_RATE("leave: No STA priv data to update!\n");
672 sel->rate = rate_lowest(local, sband, sta);
677 rate_mask = sta->supp_rates[sband->band];
678 index = min(sta->last_txrate_idx & 0xffff, IWL_RATE_COUNT - 1);
680 if (sband->band == IEEE80211_BAND_5GHZ)
681 rate_mask = rate_mask << IWL_FIRST_OFDM_RATE;
683 rs_sta = (void *)sta->rate_ctrl_priv;
685 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
686 !rs_sta->ibss_sta_added) {
687 u8 sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
689 if (sta_id == IWL_INVALID_STATION) {
690 IWL_DEBUG_RATE("LQ: ADD station %s\n",
691 print_mac(mac, hdr->addr1));
692 sta_id = iwl3945_add_station(priv,
693 hdr->addr1, 0, CMD_ASYNC);
695 if (sta_id != IWL_INVALID_STATION)
696 rs_sta->ibss_sta_added = 1;
699 spin_lock_irqsave(&rs_sta->lock, flags);
701 if (rs_sta->start_rate != IWL_RATE_INVALID) {
702 index = rs_sta->start_rate;
703 rs_sta->start_rate = IWL_RATE_INVALID;
706 window = &(rs_sta->win[index]);
708 fail_count = window->counter - window->success_counter;
710 if (((fail_count <= IWL_RATE_MIN_FAILURE_TH) &&
711 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH))) {
712 window->average_tpt = IWL_INV_TPT;
713 spin_unlock_irqrestore(&rs_sta->lock, flags);
715 IWL_DEBUG_RATE("Invalid average_tpt on rate %d: "
716 "counter: %d, success_counter: %d, "
717 "expected_tpt is %sNULL\n",
720 window->success_counter,
721 rs_sta->expected_tpt ? "not " : "");
726 window->average_tpt = ((window->success_ratio *
727 rs_sta->expected_tpt[index] + 64) / 128);
728 current_tpt = window->average_tpt;
730 high_low = iwl3945_get_adjacent_rate(rs_sta, index, rate_mask,
732 low = high_low & 0xff;
733 high = (high_low >> 8) & 0xff;
735 if (low != IWL_RATE_INVALID)
736 low_tpt = rs_sta->win[low].average_tpt;
738 if (high != IWL_RATE_INVALID)
739 high_tpt = rs_sta->win[high].average_tpt;
741 spin_unlock_irqrestore(&rs_sta->lock, flags);
745 if ((window->success_ratio < IWL_RATE_DECREASE_TH) || !current_tpt) {
746 IWL_DEBUG_RATE("decrease rate because of low success_ratio\n");
748 } else if ((low_tpt == IWL_INV_TPT) && (high_tpt == IWL_INV_TPT))
750 else if ((low_tpt != IWL_INV_TPT) && (high_tpt != IWL_INV_TPT) &&
751 (low_tpt < current_tpt) && (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);
757 if (high_tpt != IWL_INV_TPT) {
758 if (high_tpt > current_tpt)
762 ("decrease rate because of high tpt\n");
765 } else if (low_tpt != IWL_INV_TPT) {
766 if (low_tpt > current_tpt) {
768 ("decrease rate because of low tpt\n");
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);
784 switch (scale_action) {
786 if (low != IWL_RATE_INVALID)
791 if (high != IWL_RATE_INVALID)
801 IWL_DEBUG_RATE("Selected %d (action %d) - low %d high %d\n",
802 index, scale_action, low, high);
806 sta->last_txrate_idx = index;
807 if (sband->band == IEEE80211_BAND_5GHZ)
808 sta->txrate_idx = sta->last_txrate_idx - IWL_FIRST_OFDM_RATE;
810 sta->txrate_idx = sta->last_txrate_idx;
814 IWL_DEBUG_RATE("leave: %d\n", index);
816 sel->rate = &sband->bitrates[sta->txrate_idx];
819 static struct rate_control_ops rs_ops = {
822 .tx_status = rs_tx_status,
823 .get_rate = rs_get_rate,
824 .rate_init = rs_rate_init,
828 .alloc_sta = rs_alloc_sta,
829 .free_sta = rs_free_sta,
832 int iwl3945_fill_rs_info(struct ieee80211_hw *hw, char *buf, u8 sta_id)
834 struct ieee80211_local *local = hw_to_local(hw);
835 struct iwl3945_priv *priv = hw->priv;
836 struct iwl3945_rs_sta *rs_sta;
837 struct sta_info *sta;
840 u32 samples = 0, success = 0, good = 0;
841 unsigned long now = jiffies;
846 sta = sta_info_get(local, priv->stations[sta_id].sta.sta.addr);
847 if (!sta || !sta->rate_ctrl_priv) {
849 IWL_DEBUG_RATE("leave - no private rate data!\n");
851 IWL_DEBUG_RATE("leave - no station!\n");
853 return sprintf(buf, "station %d not found\n", sta_id);
856 rs_sta = (void *)sta->rate_ctrl_priv;
857 spin_lock_irqsave(&rs_sta->lock, flags);
858 i = IWL_RATE_54M_INDEX;
864 sprintf(&buf[count], " %2dMbs: ", iwl3945_rates[i].ieee / 2);
866 mask = (1ULL << (IWL_RATE_MAX_WINDOW - 1));
867 for (j = 0; j < IWL_RATE_MAX_WINDOW; j++, mask >>= 1)
869 (rs_sta->win[i].data & mask) ? '1' : '0';
871 samples += rs_sta->win[i].counter;
872 good += rs_sta->win[i].success_counter;
873 success += rs_sta->win[i].success_counter *
874 iwl3945_rates[i].ieee;
876 if (rs_sta->win[i].stamp) {
878 jiffies_to_msecs(now - rs_sta->win[i].stamp);
880 if (delta > max_time)
883 count += sprintf(&buf[count], "%5dms\n", delta);
887 j = iwl3945_get_prev_ieee_rate(i);
892 spin_unlock_irqrestore(&rs_sta->lock, flags);
895 /* Display the average rate of all samples taken.
897 * NOTE: We multiple # of samples by 2 since the IEEE measurement
898 * added from iwl3945_rates is actually 2X the rate */
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);
907 count += sprintf(&buf[count], "\nAverage rate: 0Mbs\n");
912 void iwl3945_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id)
914 struct iwl3945_priv *priv = hw->priv;
917 struct ieee80211_local *local = hw_to_local(hw);
918 struct iwl3945_rs_sta *rs_sta;
919 struct sta_info *sta;
921 IWL_DEBUG_RATE("enter\n");
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");
932 sta = sta_info_get(local, priv->stations[sta_id].sta.sta.addr);
933 if (!sta || !sta->rate_ctrl_priv) {
934 IWL_DEBUG_RATE("leave - no private rate data!\n");
939 rs_sta = (void *)sta->rate_ctrl_priv;
941 spin_lock_irqsave(&rs_sta->lock, flags);
944 switch (priv->band) {
945 case IEEE80211_BAND_2GHZ:
946 /* TODO: this always does G, not a regression */
947 if (priv->active_rxon.flags & RXON_FLG_TGG_PROTECT_MSK) {
949 rs_sta->expected_tpt = iwl3945_expected_tpt_g_prot;
951 rs_sta->expected_tpt = iwl3945_expected_tpt_g;
954 case IEEE80211_BAND_5GHZ:
955 rs_sta->expected_tpt = iwl3945_expected_tpt_a;
957 case IEEE80211_NUM_BANDS:
963 spin_unlock_irqrestore(&rs_sta->lock, flags);
965 rssi = priv->last_rx_rssi;
967 rssi = IWL_MIN_RSSI_VAL;
969 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RATE, "Network RSSI: %d\n", rssi);
971 rs_sta->start_rate = iwl3945_get_rate_index_by_rssi(rssi, priv->band);
973 IWL_DEBUG_RATE("leave: rssi %d assign rate index: "
974 "%d (plcp 0x%x)\n", rssi, rs_sta->start_rate,
975 iwl3945_rates[rs_sta->start_rate].plcp);
978 int iwl3945_rate_control_register(void)
980 return ieee80211_rate_control_register(&rs_ops);
983 void iwl3945_rate_control_unregister(void)
985 ieee80211_rate_control_unregister(&rs_ops);