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 * Intel Linux Wireless <ilw@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 "iwl-commands.h"
42 #define RS_NAME "iwl-3945-rs"
44 struct iwl3945_rate_scale_data {
53 struct iwl3945_rs_sta {
56 unsigned long last_partial_flush;
57 unsigned long last_flush;
65 struct timer_list rate_scale_flush;
66 struct iwl3945_rate_scale_data win[IWL_RATE_COUNT];
67 #ifdef CONFIG_MAC80211_DEBUGFS
68 struct dentry *rs_sta_dbgfs_stats_table_file;
71 /* used to be in sta_info */
75 static s32 iwl3945_expected_tpt_g[IWL_RATE_COUNT] = {
76 7, 13, 35, 58, 0, 0, 76, 104, 130, 168, 191, 202
79 static s32 iwl3945_expected_tpt_g_prot[IWL_RATE_COUNT] = {
80 7, 13, 35, 58, 0, 0, 0, 80, 93, 113, 123, 125
83 static s32 iwl3945_expected_tpt_a[IWL_RATE_COUNT] = {
84 0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186
87 static s32 iwl3945_expected_tpt_b[IWL_RATE_COUNT] = {
88 7, 13, 35, 58, 0, 0, 0, 0, 0, 0, 0, 0
91 struct iwl3945_tpt_entry {
96 static struct iwl3945_tpt_entry iwl3945_tpt_table_a[] = {
97 {-60, IWL_RATE_54M_INDEX},
98 {-64, IWL_RATE_48M_INDEX},
99 {-72, IWL_RATE_36M_INDEX},
100 {-80, IWL_RATE_24M_INDEX},
101 {-84, IWL_RATE_18M_INDEX},
102 {-85, IWL_RATE_12M_INDEX},
103 {-87, IWL_RATE_9M_INDEX},
104 {-89, IWL_RATE_6M_INDEX}
107 static struct iwl3945_tpt_entry iwl3945_tpt_table_g[] = {
108 {-60, IWL_RATE_54M_INDEX},
109 {-64, IWL_RATE_48M_INDEX},
110 {-68, IWL_RATE_36M_INDEX},
111 {-80, IWL_RATE_24M_INDEX},
112 {-84, IWL_RATE_18M_INDEX},
113 {-85, IWL_RATE_12M_INDEX},
114 {-86, IWL_RATE_11M_INDEX},
115 {-88, IWL_RATE_5M_INDEX},
116 {-90, IWL_RATE_2M_INDEX},
117 {-92, IWL_RATE_1M_INDEX}
120 #define IWL_RATE_MAX_WINDOW 62
121 #define IWL_RATE_FLUSH (3*HZ)
122 #define IWL_RATE_WIN_FLUSH (HZ/2)
123 #define IWL_RATE_HIGH_TH 11520
124 #define IWL_SUCCESS_UP_TH 8960
125 #define IWL_SUCCESS_DOWN_TH 10880
126 #define IWL_RATE_MIN_FAILURE_TH 8
127 #define IWL_RATE_MIN_SUCCESS_TH 8
128 #define IWL_RATE_DECREASE_TH 1920
130 static u8 iwl3945_get_rate_index_by_rssi(s32 rssi, enum ieee80211_band band)
134 struct iwl3945_tpt_entry *tpt_table = NULL;
136 if ((rssi < IWL_MIN_RSSI_VAL) || (rssi > IWL_MAX_RSSI_VAL))
137 rssi = IWL_MIN_RSSI_VAL;
140 case IEEE80211_BAND_2GHZ:
141 tpt_table = iwl3945_tpt_table_g;
142 table_size = ARRAY_SIZE(iwl3945_tpt_table_g);
145 case IEEE80211_BAND_5GHZ:
146 tpt_table = iwl3945_tpt_table_a;
147 table_size = ARRAY_SIZE(iwl3945_tpt_table_a);
155 while ((index < table_size) && (rssi < tpt_table[index].min_rssi))
158 index = min(index, (table_size - 1));
160 return tpt_table[index].index;
163 static void iwl3945_clear_window(struct iwl3945_rate_scale_data *window)
166 window->success_counter = 0;
167 window->success_ratio = -1;
169 window->average_tpt = IWL_INV_TPT;
174 * iwl3945_rate_scale_flush_windows - flush out the rate scale windows
176 * Returns the number of windows that have gathered data but were
177 * not flushed. If there were any that were not flushed, then
178 * reschedule the rate flushing routine.
180 static int iwl3945_rate_scale_flush_windows(struct iwl3945_rs_sta *rs_sta)
187 * For each rate, if we have collected data on that rate
188 * and it has been more than IWL_RATE_WIN_FLUSH
189 * since we flushed, clear out the gathered statistics
191 for (i = 0; i < IWL_RATE_COUNT; i++) {
192 if (!rs_sta->win[i].counter)
195 spin_lock_irqsave(&rs_sta->lock, flags);
196 if (time_after(jiffies, rs_sta->win[i].stamp +
197 IWL_RATE_WIN_FLUSH)) {
198 IWL_DEBUG_RATE("flushing %d samples of rate "
200 rs_sta->win[i].counter, i);
201 iwl3945_clear_window(&rs_sta->win[i]);
204 spin_unlock_irqrestore(&rs_sta->lock, flags);
210 #define IWL_RATE_FLUSH_MAX 5000 /* msec */
211 #define IWL_RATE_FLUSH_MIN 50 /* msec */
212 #define IWL_AVERAGE_PACKETS 1500
214 static void iwl3945_bg_rate_scale_flush(unsigned long data)
216 struct iwl3945_rs_sta *rs_sta = (void *)data;
219 u32 packet_count, duration, pps;
221 IWL_DEBUG_RATE("enter\n");
223 unflushed = iwl3945_rate_scale_flush_windows(rs_sta);
225 spin_lock_irqsave(&rs_sta->lock, flags);
227 /* Number of packets Rx'd since last time this timer ran */
228 packet_count = (rs_sta->tx_packets - rs_sta->last_tx_packets) + 1;
230 rs_sta->last_tx_packets = rs_sta->tx_packets + 1;
234 jiffies_to_msecs(jiffies - rs_sta->last_partial_flush);
236 IWL_DEBUG_RATE("Tx'd %d packets in %dms\n",
237 packet_count, duration);
239 /* Determine packets per second */
241 pps = (packet_count * 1000) / duration;
246 duration = (IWL_AVERAGE_PACKETS * 1000) / pps;
247 if (duration < IWL_RATE_FLUSH_MIN)
248 duration = IWL_RATE_FLUSH_MIN;
249 else if (duration > IWL_RATE_FLUSH_MAX)
250 duration = IWL_RATE_FLUSH_MAX;
252 duration = IWL_RATE_FLUSH_MAX;
254 rs_sta->flush_time = msecs_to_jiffies(duration);
256 IWL_DEBUG_RATE("new flush period: %d msec ave %d\n",
257 duration, packet_count);
259 mod_timer(&rs_sta->rate_scale_flush, jiffies +
262 rs_sta->last_partial_flush = jiffies;
264 rs_sta->flush_time = IWL_RATE_FLUSH;
265 rs_sta->flush_pending = 0;
267 /* If there weren't any unflushed entries, we don't schedule the timer
270 rs_sta->last_flush = jiffies;
272 spin_unlock_irqrestore(&rs_sta->lock, flags);
274 IWL_DEBUG_RATE("leave\n");
278 * iwl3945_collect_tx_data - Update the success/failure sliding window
280 * We keep a sliding window of the last 64 packets transmitted
281 * at this rate. window->data contains the bitmask of successful
284 static void iwl3945_collect_tx_data(struct iwl3945_rs_sta *rs_sta,
285 struct iwl3945_rate_scale_data *window,
286 int success, int retries, int index)
292 IWL_DEBUG_RATE("leave: retries == 0 -- should be at least 1\n");
296 spin_lock_irqsave(&rs_sta->lock, flags);
299 /* If we have filled up the window then subtract one from the
300 * success counter if the high-bit is counting toward
302 if (window->counter == IWL_RATE_MAX_WINDOW) {
303 if (window->data & (1ULL << (IWL_RATE_MAX_WINDOW - 1)))
304 window->success_counter--;
308 /* Slide the window to the left one bit */
309 window->data = (window->data << 1);
311 /* If this packet was a success then set the low bit high */
313 window->success_counter++;
317 /* window->counter can't be 0 -- it is either >0 or
318 * IWL_RATE_MAX_WINDOW */
319 window->success_ratio = 12800 * window->success_counter /
322 /* Tag this window as having been updated */
323 window->stamp = jiffies;
327 fail_count = window->counter - window->success_counter;
328 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
329 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
330 window->average_tpt = ((window->success_ratio *
331 rs_sta->expected_tpt[index] + 64) / 128);
333 window->average_tpt = IWL_INV_TPT;
335 spin_unlock_irqrestore(&rs_sta->lock, flags);
339 static void rs_rate_init(void *priv_r, struct ieee80211_supported_band *sband,
340 struct ieee80211_sta *sta, void *priv_sta)
342 struct iwl3945_rs_sta *rs_sta = priv_sta;
343 struct iwl3945_priv *priv = (struct iwl3945_priv *)priv_r;
346 IWL_DEBUG_RATE("enter\n");
348 /* TODO: what is a good starting rate for STA? About middle? Maybe not
349 * the lowest or the highest rate.. Could consider using RSSI from
350 * previous packets? Need to have IEEE 802.1X auth succeed immediately
353 for (i = sband->n_bitrates - 1; i >= 0; i--) {
354 if (sta->supp_rates[sband->band] & (1 << i)) {
355 rs_sta->last_txrate_idx = i;
360 priv->sta_supp_rates = sta->supp_rates[sband->band];
361 /* For 5 GHz band it start at IWL_FIRST_OFDM_RATE */
362 if (sband->band == IEEE80211_BAND_5GHZ) {
363 rs_sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
364 priv->sta_supp_rates = priv->sta_supp_rates <<
369 IWL_DEBUG_RATE("leave\n");
372 static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
377 /* rate scale requires free function to be implemented */
378 static void rs_free(void *priv)
383 static void *rs_alloc_sta(void *priv, struct ieee80211_sta *sta, gfp_t gfp)
385 struct iwl3945_rs_sta *rs_sta;
386 struct iwl3945_sta_priv *psta = (void *) sta->drv_priv;
390 * XXX: If it's using sta->drv_priv anyway, it might
391 * as well just put all the information there.
394 IWL_DEBUG_RATE("enter\n");
396 rs_sta = kzalloc(sizeof(struct iwl3945_rs_sta), gfp);
398 IWL_DEBUG_RATE("leave: ENOMEM\n");
402 psta->rs_sta = rs_sta;
404 spin_lock_init(&rs_sta->lock);
406 rs_sta->start_rate = IWL_RATE_INVALID;
408 /* default to just 802.11b */
409 rs_sta->expected_tpt = iwl3945_expected_tpt_b;
411 rs_sta->last_partial_flush = jiffies;
412 rs_sta->last_flush = jiffies;
413 rs_sta->flush_time = IWL_RATE_FLUSH;
414 rs_sta->last_tx_packets = 0;
415 rs_sta->ibss_sta_added = 0;
417 init_timer(&rs_sta->rate_scale_flush);
418 rs_sta->rate_scale_flush.data = (unsigned long)rs_sta;
419 rs_sta->rate_scale_flush.function = &iwl3945_bg_rate_scale_flush;
421 for (i = 0; i < IWL_RATE_COUNT; i++)
422 iwl3945_clear_window(&rs_sta->win[i]);
424 IWL_DEBUG_RATE("leave\n");
429 static void rs_free_sta(void *priv, struct ieee80211_sta *sta,
432 struct iwl3945_sta_priv *psta = (void *) sta->drv_priv;
433 struct iwl3945_rs_sta *rs_sta = priv_sta;
437 IWL_DEBUG_RATE("enter\n");
438 del_timer_sync(&rs_sta->rate_scale_flush);
440 IWL_DEBUG_RATE("leave\n");
445 * rs_tx_status - Update rate control values based on Tx results
447 * NOTE: Uses iwl3945_priv->retry_rate for the # of retries attempted by
448 * the hardware for each rate.
450 static void rs_tx_status(void *priv_rate, struct ieee80211_supported_band *sband,
451 struct ieee80211_sta *sta, void *priv_sta,
454 s8 retries = 0, current_count;
455 int scale_rate_index, first_index, last_index;
457 struct iwl3945_priv *priv = (struct iwl3945_priv *)priv_rate;
458 struct iwl3945_rs_sta *rs_sta = priv_sta;
459 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
461 IWL_DEBUG_RATE("enter\n");
463 retries = info->status.rates[0].count;
465 first_index = sband->bitrates[info->status.rates[0].idx].hw_value;
466 if ((first_index < 0) || (first_index >= IWL_RATE_COUNT)) {
467 IWL_DEBUG_RATE("leave: Rate out of bounds: %d\n", first_index);
472 IWL_DEBUG_RATE("leave: No STA priv data to update!\n");
476 rs_sta->tx_packets++;
478 scale_rate_index = first_index;
479 last_index = first_index;
482 * Update the window for each rate. We determine which rates
483 * were Tx'd based on the total number of retries vs. the number
484 * of retries configured for each rate -- currently set to the
485 * priv value 'retry_rate' vs. rate specific
487 * On exit from this while loop last_index indicates the rate
488 * at which the frame was finally transmitted (or failed if no
491 while (retries > 1) {
492 if ((retries - 1) < priv->retry_rate) {
493 current_count = (retries - 1);
494 last_index = scale_rate_index;
496 current_count = priv->retry_rate;
497 last_index = iwl3945_rs_next_rate(priv,
501 /* Update this rate accounting for as many retries
502 * as was used for it (per current_count) */
503 iwl3945_collect_tx_data(rs_sta,
504 &rs_sta->win[scale_rate_index],
505 0, current_count, scale_rate_index);
506 IWL_DEBUG_RATE("Update rate %d for %d retries.\n",
507 scale_rate_index, current_count);
509 retries -= current_count;
511 scale_rate_index = last_index;
515 /* Update the last index window with success/failure based on ACK */
516 IWL_DEBUG_RATE("Update rate %d with %s.\n",
518 (info->flags & IEEE80211_TX_STAT_ACK) ?
519 "success" : "failure");
520 iwl3945_collect_tx_data(rs_sta,
521 &rs_sta->win[last_index],
522 info->flags & IEEE80211_TX_STAT_ACK, 1, last_index);
524 /* We updated the rate scale window -- if its been more than
525 * flush_time since the last run, schedule the flush
527 spin_lock_irqsave(&rs_sta->lock, flags);
529 if (!rs_sta->flush_pending &&
530 time_after(jiffies, rs_sta->last_flush +
531 rs_sta->flush_time)) {
533 rs_sta->last_partial_flush = jiffies;
534 rs_sta->flush_pending = 1;
535 mod_timer(&rs_sta->rate_scale_flush,
536 jiffies + rs_sta->flush_time);
539 spin_unlock_irqrestore(&rs_sta->lock, flags);
541 IWL_DEBUG_RATE("leave\n");
546 static u16 iwl3945_get_adjacent_rate(struct iwl3945_rs_sta *rs_sta,
547 u8 index, u16 rate_mask, enum ieee80211_band band)
549 u8 high = IWL_RATE_INVALID;
550 u8 low = IWL_RATE_INVALID;
552 /* 802.11A walks to the next literal adjacent rate in
554 if (unlikely(band == IEEE80211_BAND_5GHZ)) {
558 /* Find the previous rate that is in the rate mask */
560 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
561 if (rate_mask & mask) {
567 /* Find the next rate that is in the rate mask */
569 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
570 if (rate_mask & mask) {
576 return (high << 8) | low;
580 while (low != IWL_RATE_INVALID) {
582 low = iwl3945_rates[low].prev_rs_tgg;
584 low = iwl3945_rates[low].prev_rs;
585 if (low == IWL_RATE_INVALID)
587 if (rate_mask & (1 << low))
589 IWL_DEBUG_RATE("Skipping masked lower rate: %d\n", low);
593 while (high != IWL_RATE_INVALID) {
595 high = iwl3945_rates[high].next_rs_tgg;
597 high = iwl3945_rates[high].next_rs;
598 if (high == IWL_RATE_INVALID)
600 if (rate_mask & (1 << high))
602 IWL_DEBUG_RATE("Skipping masked higher rate: %d\n", high);
605 return (high << 8) | low;
609 * rs_get_rate - find the rate for the requested packet
611 * Returns the ieee80211_rate structure allocated by the driver.
613 * The rate control algorithm has no internal mapping between hw_mode's
614 * rate ordering and the rate ordering used by the rate control algorithm.
616 * The rate control algorithm uses a single table of rates that goes across
617 * the entire A/B/G spectrum vs. being limited to just one particular
620 * As such, we can't convert the index obtained below into the hw_mode's
621 * rate table and must reference the driver allocated rate table
624 static void rs_get_rate(void *priv_r, struct ieee80211_sta *sta,
625 void *priv_sta, struct ieee80211_tx_rate_control *txrc)
627 struct ieee80211_supported_band *sband = txrc->sband;
628 struct sk_buff *skb = txrc->skb;
629 u8 low = IWL_RATE_INVALID;
630 u8 high = IWL_RATE_INVALID;
633 struct iwl3945_rs_sta *rs_sta = priv_sta;
634 struct iwl3945_rate_scale_data *window = NULL;
635 int current_tpt = IWL_INV_TPT;
636 int low_tpt = IWL_INV_TPT;
637 int high_tpt = IWL_INV_TPT;
641 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
644 struct iwl3945_priv *priv = (struct iwl3945_priv *)priv_r;
645 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
647 IWL_DEBUG_RATE("enter\n");
650 rate_mask = sta->supp_rates[sband->band];
652 /* Send management frames and broadcast/multicast data using lowest
654 fc = le16_to_cpu(hdr->frame_control);
655 if ((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA ||
656 is_multicast_ether_addr(hdr->addr1) ||
658 IWL_DEBUG_RATE("leave: No STA priv data to update!\n");
660 info->control.rates[0].idx =
661 rate_lowest_index(sband, NULL);
663 info->control.rates[0].idx =
664 rate_lowest_index(sband, sta);
668 index = min(rs_sta->last_txrate_idx & 0xffff, IWL_RATE_COUNT - 1);
670 if (sband->band == IEEE80211_BAND_5GHZ)
671 rate_mask = rate_mask << IWL_FIRST_OFDM_RATE;
673 if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) &&
674 !rs_sta->ibss_sta_added) {
675 u8 sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
677 if (sta_id == IWL_INVALID_STATION) {
678 IWL_DEBUG_RATE("LQ: ADD station %pm\n",
680 sta_id = iwl3945_add_station(priv,
681 hdr->addr1, 0, CMD_ASYNC);
683 if (sta_id != IWL_INVALID_STATION)
684 rs_sta->ibss_sta_added = 1;
687 spin_lock_irqsave(&rs_sta->lock, flags);
689 /* for recent assoc, choose best rate regarding
692 if (rs_sta->start_rate != IWL_RATE_INVALID) {
693 if (rs_sta->start_rate < index &&
694 (rate_mask & (1 << rs_sta->start_rate)))
695 index = rs_sta->start_rate;
696 rs_sta->start_rate = IWL_RATE_INVALID;
699 window = &(rs_sta->win[index]);
701 fail_count = window->counter - window->success_counter;
703 if (((fail_count <= IWL_RATE_MIN_FAILURE_TH) &&
704 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH))) {
705 spin_unlock_irqrestore(&rs_sta->lock, flags);
707 IWL_DEBUG_RATE("Invalid average_tpt on rate %d: "
708 "counter: %d, success_counter: %d, "
709 "expected_tpt is %sNULL\n",
712 window->success_counter,
713 rs_sta->expected_tpt ? "not " : "");
718 current_tpt = window->average_tpt;
720 high_low = iwl3945_get_adjacent_rate(rs_sta, index, rate_mask,
722 low = high_low & 0xff;
723 high = (high_low >> 8) & 0xff;
725 if (low != IWL_RATE_INVALID)
726 low_tpt = rs_sta->win[low].average_tpt;
728 if (high != IWL_RATE_INVALID)
729 high_tpt = rs_sta->win[high].average_tpt;
731 spin_unlock_irqrestore(&rs_sta->lock, flags);
735 if ((window->success_ratio < IWL_RATE_DECREASE_TH) || !current_tpt) {
736 IWL_DEBUG_RATE("decrease rate because of low success_ratio\n");
738 } else if ((low_tpt == IWL_INV_TPT) && (high_tpt == IWL_INV_TPT))
740 else if ((low_tpt != IWL_INV_TPT) && (high_tpt != IWL_INV_TPT) &&
741 (low_tpt < current_tpt) && (high_tpt < current_tpt)) {
742 IWL_DEBUG_RATE("No action -- low [%d] & high [%d] < "
743 "current_tpt [%d]\n",
744 low_tpt, high_tpt, current_tpt);
747 if (high_tpt != IWL_INV_TPT) {
748 if (high_tpt > current_tpt)
752 ("decrease rate because of high tpt\n");
755 } else if (low_tpt != IWL_INV_TPT) {
756 if (low_tpt > current_tpt) {
758 ("decrease rate because of low tpt\n");
765 if (scale_action == -1) {
766 if (window->success_ratio > IWL_SUCCESS_DOWN_TH)
768 } else if (scale_action == 1) {
769 if (window->success_ratio < IWL_SUCCESS_UP_TH) {
770 IWL_DEBUG_RATE("No action -- success_ratio [%d] < "
771 "SUCCESS UP\n", window->success_ratio);
776 switch (scale_action) {
778 if (low != IWL_RATE_INVALID)
783 if (high != IWL_RATE_INVALID)
793 IWL_DEBUG_RATE("Selected %d (action %d) - low %d high %d\n",
794 index, scale_action, low, high);
798 rs_sta->last_txrate_idx = index;
799 if (sband->band == IEEE80211_BAND_5GHZ)
800 info->control.rates[0].idx = rs_sta->last_txrate_idx -
803 info->control.rates[0].idx = rs_sta->last_txrate_idx;
805 IWL_DEBUG_RATE("leave: %d\n", index);
808 #ifdef CONFIG_MAC80211_DEBUGFS
809 static int iwl3945_open_file_generic(struct inode *inode, struct file *file)
811 file->private_data = inode->i_private;
815 static ssize_t iwl3945_sta_dbgfs_stats_table_read(struct file *file,
816 char __user *user_buf,
817 size_t count, loff_t *ppos)
822 struct iwl3945_rs_sta *lq_sta = file->private_data;
824 desc += sprintf(buff + desc, "tx packets=%d last rate index=%d\n"
825 "rate=0x%X flush time %d\n",
827 lq_sta->last_txrate_idx,
828 lq_sta->start_rate, jiffies_to_msecs(lq_sta->flush_time));
829 for (j = 0; j < IWL_RATE_COUNT; j++) {
830 desc += sprintf(buff+desc,
831 "counter=%d success=%d %%=%d\n",
832 lq_sta->win[j].counter,
833 lq_sta->win[j].success_counter,
834 lq_sta->win[j].success_ratio);
836 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
839 static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
840 .read = iwl3945_sta_dbgfs_stats_table_read,
841 .open = iwl3945_open_file_generic,
844 static void iwl3945_add_debugfs(void *priv, void *priv_sta,
847 struct iwl3945_rs_sta *lq_sta = priv_sta;
849 lq_sta->rs_sta_dbgfs_stats_table_file =
850 debugfs_create_file("rate_stats_table", 0600, dir,
851 lq_sta, &rs_sta_dbgfs_stats_table_ops);
855 static void iwl3945_remove_debugfs(void *priv, void *priv_sta)
857 struct iwl3945_rs_sta *lq_sta = priv_sta;
858 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
862 static struct rate_control_ops rs_ops = {
865 .tx_status = rs_tx_status,
866 .get_rate = rs_get_rate,
867 .rate_init = rs_rate_init,
870 .alloc_sta = rs_alloc_sta,
871 .free_sta = rs_free_sta,
872 #ifdef CONFIG_MAC80211_DEBUGFS
873 .add_sta_debugfs = iwl3945_add_debugfs,
874 .remove_sta_debugfs = iwl3945_remove_debugfs,
879 void iwl3945_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id)
881 struct iwl3945_priv *priv = hw->priv;
884 struct iwl3945_rs_sta *rs_sta;
885 struct ieee80211_sta *sta;
886 struct iwl3945_sta_priv *psta;
888 IWL_DEBUG_RATE("enter\n");
892 sta = ieee80211_find_sta(hw, priv->stations[sta_id].sta.sta.addr);
898 psta = (void *) sta->drv_priv;
899 rs_sta = psta->rs_sta;
901 spin_lock_irqsave(&rs_sta->lock, flags);
904 switch (priv->band) {
905 case IEEE80211_BAND_2GHZ:
906 /* TODO: this always does G, not a regression */
907 if (priv->active_rxon.flags & RXON_FLG_TGG_PROTECT_MSK) {
909 rs_sta->expected_tpt = iwl3945_expected_tpt_g_prot;
911 rs_sta->expected_tpt = iwl3945_expected_tpt_g;
914 case IEEE80211_BAND_5GHZ:
915 rs_sta->expected_tpt = iwl3945_expected_tpt_a;
917 case IEEE80211_NUM_BANDS:
922 spin_unlock_irqrestore(&rs_sta->lock, flags);
924 rssi = priv->last_rx_rssi;
926 rssi = IWL_MIN_RSSI_VAL;
928 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RATE, "Network RSSI: %d\n", rssi);
930 rs_sta->start_rate = iwl3945_get_rate_index_by_rssi(rssi, priv->band);
932 IWL_DEBUG_RATE("leave: rssi %d assign rate index: "
933 "%d (plcp 0x%x)\n", rssi, rs_sta->start_rate,
934 iwl3945_rates[rs_sta->start_rate].plcp);
938 int iwl3945_rate_control_register(void)
940 return ieee80211_rate_control_register(&rs_ops);
943 void iwl3945_rate_control_unregister(void)
945 ieee80211_rate_control_unregister(&rs_ops);