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"
40 #include "iwl-3945-commands.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];
68 #ifdef CONFIG_MAC80211_DEBUGFS
69 struct dentry *rs_sta_dbgfs_stats_table_file;
72 /* used to be in sta_info */
76 static s32 iwl3945_expected_tpt_g[IWL_RATE_COUNT] = {
77 7, 13, 35, 58, 0, 0, 76, 104, 130, 168, 191, 202
80 static s32 iwl3945_expected_tpt_g_prot[IWL_RATE_COUNT] = {
81 7, 13, 35, 58, 0, 0, 0, 80, 93, 113, 123, 125
84 static s32 iwl3945_expected_tpt_a[IWL_RATE_COUNT] = {
85 0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186
88 static s32 iwl3945_expected_tpt_b[IWL_RATE_COUNT] = {
89 7, 13, 35, 58, 0, 0, 0, 0, 0, 0, 0, 0
92 struct iwl3945_tpt_entry {
97 static struct iwl3945_tpt_entry iwl3945_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 iwl3945_tpt_entry iwl3945_tpt_table_g[] = {
109 {-60, IWL_RATE_54M_INDEX},
110 {-64, IWL_RATE_48M_INDEX},
111 {-68, IWL_RATE_36M_INDEX},
112 {-80, IWL_RATE_24M_INDEX},
113 {-84, IWL_RATE_18M_INDEX},
114 {-85, IWL_RATE_12M_INDEX},
115 {-86, IWL_RATE_11M_INDEX},
116 {-88, IWL_RATE_5M_INDEX},
117 {-90, IWL_RATE_2M_INDEX},
118 {-92, IWL_RATE_1M_INDEX}
121 #define IWL_RATE_MAX_WINDOW 62
122 #define IWL_RATE_FLUSH (3*HZ)
123 #define IWL_RATE_WIN_FLUSH (HZ/2)
124 #define IWL_RATE_HIGH_TH 11520
125 #define IWL_SUCCESS_UP_TH 8960
126 #define IWL_SUCCESS_DOWN_TH 10880
127 #define IWL_RATE_MIN_FAILURE_TH 8
128 #define IWL_RATE_MIN_SUCCESS_TH 8
129 #define IWL_RATE_DECREASE_TH 1920
131 static u8 iwl3945_get_rate_index_by_rssi(s32 rssi, enum ieee80211_band band)
135 struct iwl3945_tpt_entry *tpt_table = NULL;
137 if ((rssi < IWL_MIN_RSSI_VAL) || (rssi > IWL_MAX_RSSI_VAL))
138 rssi = IWL_MIN_RSSI_VAL;
141 case IEEE80211_BAND_2GHZ:
142 tpt_table = iwl3945_tpt_table_g;
143 table_size = ARRAY_SIZE(iwl3945_tpt_table_g);
146 case IEEE80211_BAND_5GHZ:
147 tpt_table = iwl3945_tpt_table_a;
148 table_size = ARRAY_SIZE(iwl3945_tpt_table_a);
156 while ((index < table_size) && (rssi < tpt_table[index].min_rssi))
159 index = min(index, (table_size - 1));
161 return tpt_table[index].index;
164 static void iwl3945_clear_window(struct iwl3945_rate_scale_data *window)
167 window->success_counter = 0;
168 window->success_ratio = -1;
170 window->average_tpt = IWL_INV_TPT;
175 * iwl3945_rate_scale_flush_windows - flush out the rate scale windows
177 * Returns the number of windows that have gathered data but were
178 * not flushed. If there were any that were not flushed, then
179 * reschedule the rate flushing routine.
181 static int iwl3945_rate_scale_flush_windows(struct iwl3945_rs_sta *rs_sta)
188 * For each rate, if we have collected data on that rate
189 * and it has been more than IWL_RATE_WIN_FLUSH
190 * since we flushed, clear out the gathered statistics
192 for (i = 0; i < IWL_RATE_COUNT; i++) {
193 if (!rs_sta->win[i].counter)
196 spin_lock_irqsave(&rs_sta->lock, flags);
197 if (time_after(jiffies, rs_sta->win[i].stamp +
198 IWL_RATE_WIN_FLUSH)) {
199 IWL_DEBUG_RATE("flushing %d samples of rate "
201 rs_sta->win[i].counter, i);
202 iwl3945_clear_window(&rs_sta->win[i]);
205 spin_unlock_irqrestore(&rs_sta->lock, flags);
211 #define IWL_RATE_FLUSH_MAX 5000 /* msec */
212 #define IWL_RATE_FLUSH_MIN 50 /* msec */
213 #define IWL_AVERAGE_PACKETS 1500
215 static void iwl3945_bg_rate_scale_flush(unsigned long data)
217 struct iwl3945_rs_sta *rs_sta = (void *)data;
220 u32 packet_count, duration, pps;
222 IWL_DEBUG_RATE("enter\n");
224 unflushed = iwl3945_rate_scale_flush_windows(rs_sta);
226 spin_lock_irqsave(&rs_sta->lock, flags);
228 /* Number of packets Rx'd since last time this timer ran */
229 packet_count = (rs_sta->tx_packets - rs_sta->last_tx_packets) + 1;
231 rs_sta->last_tx_packets = rs_sta->tx_packets + 1;
235 jiffies_to_msecs(jiffies - rs_sta->last_partial_flush);
237 IWL_DEBUG_RATE("Tx'd %d packets in %dms\n",
238 packet_count, duration);
240 /* Determine packets per second */
242 pps = (packet_count * 1000) / duration;
247 duration = (IWL_AVERAGE_PACKETS * 1000) / pps;
248 if (duration < IWL_RATE_FLUSH_MIN)
249 duration = IWL_RATE_FLUSH_MIN;
250 else if (duration > IWL_RATE_FLUSH_MAX)
251 duration = IWL_RATE_FLUSH_MAX;
253 duration = IWL_RATE_FLUSH_MAX;
255 rs_sta->flush_time = msecs_to_jiffies(duration);
257 IWL_DEBUG_RATE("new flush period: %d msec ave %d\n",
258 duration, packet_count);
260 mod_timer(&rs_sta->rate_scale_flush, jiffies +
263 rs_sta->last_partial_flush = jiffies;
265 rs_sta->flush_time = IWL_RATE_FLUSH;
266 rs_sta->flush_pending = 0;
268 /* If there weren't any unflushed entries, we don't schedule the timer
271 rs_sta->last_flush = jiffies;
273 spin_unlock_irqrestore(&rs_sta->lock, flags);
275 IWL_DEBUG_RATE("leave\n");
279 * iwl3945_collect_tx_data - Update the success/failure sliding window
281 * We keep a sliding window of the last 64 packets transmitted
282 * at this rate. window->data contains the bitmask of successful
285 static void iwl3945_collect_tx_data(struct iwl3945_rs_sta *rs_sta,
286 struct iwl3945_rate_scale_data *window,
287 int success, int retries, int index)
293 IWL_DEBUG_RATE("leave: retries == 0 -- should be at least 1\n");
297 spin_lock_irqsave(&rs_sta->lock, flags);
300 /* If we have filled up the window then subtract one from the
301 * success counter if the high-bit is counting toward
303 if (window->counter == IWL_RATE_MAX_WINDOW) {
304 if (window->data & (1ULL << (IWL_RATE_MAX_WINDOW - 1)))
305 window->success_counter--;
309 /* Slide the window to the left one bit */
310 window->data = (window->data << 1);
312 /* If this packet was a success then set the low bit high */
314 window->success_counter++;
318 /* window->counter can't be 0 -- it is either >0 or
319 * IWL_RATE_MAX_WINDOW */
320 window->success_ratio = 12800 * window->success_counter /
323 /* Tag this window as having been updated */
324 window->stamp = jiffies;
328 fail_count = window->counter - window->success_counter;
329 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
330 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
331 window->average_tpt = ((window->success_ratio *
332 rs_sta->expected_tpt[index] + 64) / 128);
334 window->average_tpt = IWL_INV_TPT;
336 spin_unlock_irqrestore(&rs_sta->lock, flags);
340 static void rs_rate_init(void *priv_r, struct ieee80211_supported_band *sband,
341 struct ieee80211_sta *sta, void *priv_sta)
343 struct iwl3945_rs_sta *rs_sta = priv_sta;
344 struct iwl3945_priv *priv = (struct iwl3945_priv *)priv_r;
347 IWL_DEBUG_RATE("enter\n");
349 /* TODO: what is a good starting rate for STA? About middle? Maybe not
350 * the lowest or the highest rate.. Could consider using RSSI from
351 * previous packets? Need to have IEEE 802.1X auth succeed immediately
354 for (i = sband->n_bitrates - 1; i >= 0; i--) {
355 if (sta->supp_rates[sband->band] & (1 << i)) {
356 rs_sta->last_txrate_idx = i;
361 priv->sta_supp_rates = sta->supp_rates[sband->band];
362 /* For 5 GHz band it start at IWL_FIRST_OFDM_RATE */
363 if (sband->band == IEEE80211_BAND_5GHZ) {
364 rs_sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
365 priv->sta_supp_rates = priv->sta_supp_rates <<
370 IWL_DEBUG_RATE("leave\n");
373 static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
378 /* rate scale requires free function to be implemented */
379 static void rs_free(void *priv)
384 static void *rs_alloc_sta(void *priv, struct ieee80211_sta *sta, gfp_t gfp)
386 struct iwl3945_rs_sta *rs_sta;
387 struct iwl3945_sta_priv *psta = (void *) sta->drv_priv;
391 * XXX: If it's using sta->drv_priv anyway, it might
392 * as well just put all the information there.
395 IWL_DEBUG_RATE("enter\n");
397 rs_sta = kzalloc(sizeof(struct iwl3945_rs_sta), gfp);
399 IWL_DEBUG_RATE("leave: ENOMEM\n");
403 psta->rs_sta = rs_sta;
405 spin_lock_init(&rs_sta->lock);
407 rs_sta->start_rate = IWL_RATE_INVALID;
409 /* default to just 802.11b */
410 rs_sta->expected_tpt = iwl3945_expected_tpt_b;
412 rs_sta->last_partial_flush = jiffies;
413 rs_sta->last_flush = jiffies;
414 rs_sta->flush_time = IWL_RATE_FLUSH;
415 rs_sta->last_tx_packets = 0;
416 rs_sta->ibss_sta_added = 0;
418 init_timer(&rs_sta->rate_scale_flush);
419 rs_sta->rate_scale_flush.data = (unsigned long)rs_sta;
420 rs_sta->rate_scale_flush.function = &iwl3945_bg_rate_scale_flush;
422 for (i = 0; i < IWL_RATE_COUNT; i++)
423 iwl3945_clear_window(&rs_sta->win[i]);
425 IWL_DEBUG_RATE("leave\n");
430 static void rs_free_sta(void *priv, struct ieee80211_sta *sta,
433 struct iwl3945_sta_priv *psta = (void *) sta->drv_priv;
434 struct iwl3945_rs_sta *rs_sta = priv_sta;
438 IWL_DEBUG_RATE("enter\n");
439 del_timer_sync(&rs_sta->rate_scale_flush);
441 IWL_DEBUG_RATE("leave\n");
446 * rs_tx_status - Update rate control values based on Tx results
448 * NOTE: Uses iwl3945_priv->retry_rate for the # of retries attempted by
449 * the hardware for each rate.
451 static void rs_tx_status(void *priv_rate, struct ieee80211_supported_band *sband,
452 struct ieee80211_sta *sta, void *priv_sta,
455 s8 retries = 0, current_count;
456 int scale_rate_index, first_index, last_index;
458 struct iwl3945_priv *priv = (struct iwl3945_priv *)priv_rate;
459 struct iwl3945_rs_sta *rs_sta = priv_sta;
460 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
462 IWL_DEBUG_RATE("enter\n");
464 retries = info->status.rates[0].count;
466 first_index = sband->bitrates[info->status.rates[0].idx].hw_value;
467 if ((first_index < 0) || (first_index >= IWL_RATE_COUNT)) {
468 IWL_DEBUG_RATE("leave: Rate out of bounds: %d\n", first_index);
473 IWL_DEBUG_RATE("leave: No STA priv data to update!\n");
477 rs_sta->tx_packets++;
479 scale_rate_index = first_index;
480 last_index = first_index;
483 * Update the window for each rate. We determine which rates
484 * were Tx'd based on the total number of retries vs. the number
485 * of retries configured for each rate -- currently set to the
486 * priv value 'retry_rate' vs. rate specific
488 * On exit from this while loop last_index indicates the rate
489 * at which the frame was finally transmitted (or failed if no
492 while (retries > 1) {
493 if ((retries - 1) < priv->retry_rate) {
494 current_count = (retries - 1);
495 last_index = scale_rate_index;
497 current_count = priv->retry_rate;
498 last_index = iwl3945_rs_next_rate(priv,
502 /* Update this rate accounting for as many retries
503 * as was used for it (per current_count) */
504 iwl3945_collect_tx_data(rs_sta,
505 &rs_sta->win[scale_rate_index],
506 0, current_count, scale_rate_index);
507 IWL_DEBUG_RATE("Update rate %d for %d retries.\n",
508 scale_rate_index, current_count);
510 retries -= current_count;
512 scale_rate_index = last_index;
516 /* Update the last index window with success/failure based on ACK */
517 IWL_DEBUG_RATE("Update rate %d with %s.\n",
519 (info->flags & IEEE80211_TX_STAT_ACK) ?
520 "success" : "failure");
521 iwl3945_collect_tx_data(rs_sta,
522 &rs_sta->win[last_index],
523 info->flags & IEEE80211_TX_STAT_ACK, 1, last_index);
525 /* We updated the rate scale window -- if its been more than
526 * flush_time since the last run, schedule the flush
528 spin_lock_irqsave(&rs_sta->lock, flags);
530 if (!rs_sta->flush_pending &&
531 time_after(jiffies, rs_sta->last_flush +
532 rs_sta->flush_time)) {
534 rs_sta->last_partial_flush = jiffies;
535 rs_sta->flush_pending = 1;
536 mod_timer(&rs_sta->rate_scale_flush,
537 jiffies + rs_sta->flush_time);
540 spin_unlock_irqrestore(&rs_sta->lock, flags);
542 IWL_DEBUG_RATE("leave\n");
547 static u16 iwl3945_get_adjacent_rate(struct iwl3945_rs_sta *rs_sta,
548 u8 index, u16 rate_mask, enum ieee80211_band band)
550 u8 high = IWL_RATE_INVALID;
551 u8 low = IWL_RATE_INVALID;
553 /* 802.11A walks to the next literal adjacent rate in
555 if (unlikely(band == IEEE80211_BAND_5GHZ)) {
559 /* Find the previous rate that is in the rate mask */
561 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
562 if (rate_mask & mask) {
568 /* Find the next rate that is in the rate mask */
570 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
571 if (rate_mask & mask) {
577 return (high << 8) | low;
581 while (low != IWL_RATE_INVALID) {
583 low = iwl3945_rates[low].prev_rs_tgg;
585 low = iwl3945_rates[low].prev_rs;
586 if (low == IWL_RATE_INVALID)
588 if (rate_mask & (1 << low))
590 IWL_DEBUG_RATE("Skipping masked lower rate: %d\n", low);
594 while (high != IWL_RATE_INVALID) {
596 high = iwl3945_rates[high].next_rs_tgg;
598 high = iwl3945_rates[high].next_rs;
599 if (high == IWL_RATE_INVALID)
601 if (rate_mask & (1 << high))
603 IWL_DEBUG_RATE("Skipping masked higher rate: %d\n", high);
606 return (high << 8) | low;
610 * rs_get_rate - find the rate for the requested packet
612 * Returns the ieee80211_rate structure allocated by the driver.
614 * The rate control algorithm has no internal mapping between hw_mode's
615 * rate ordering and the rate ordering used by the rate control algorithm.
617 * The rate control algorithm uses a single table of rates that goes across
618 * the entire A/B/G spectrum vs. being limited to just one particular
621 * As such, we can't convert the index obtained below into the hw_mode's
622 * rate table and must reference the driver allocated rate table
625 static void rs_get_rate(void *priv_r, struct ieee80211_sta *sta,
626 void *priv_sta, struct ieee80211_tx_rate_control *txrc)
628 struct ieee80211_supported_band *sband = txrc->sband;
629 struct sk_buff *skb = txrc->skb;
630 u8 low = IWL_RATE_INVALID;
631 u8 high = IWL_RATE_INVALID;
634 struct iwl3945_rs_sta *rs_sta = priv_sta;
635 struct iwl3945_rate_scale_data *window = NULL;
636 int current_tpt = IWL_INV_TPT;
637 int low_tpt = IWL_INV_TPT;
638 int high_tpt = IWL_INV_TPT;
642 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
645 struct iwl3945_priv *priv = (struct iwl3945_priv *)priv_r;
646 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
648 IWL_DEBUG_RATE("enter\n");
651 rate_mask = sta->supp_rates[sband->band];
653 /* Send management frames and broadcast/multicast data using lowest
655 fc = le16_to_cpu(hdr->frame_control);
656 if ((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA ||
657 is_multicast_ether_addr(hdr->addr1) ||
659 IWL_DEBUG_RATE("leave: No STA priv data to update!\n");
661 info->control.rates[0].idx =
662 rate_lowest_index(sband, NULL);
664 info->control.rates[0].idx =
665 rate_lowest_index(sband, sta);
669 index = min(rs_sta->last_txrate_idx & 0xffff, IWL_RATE_COUNT - 1);
671 if (sband->band == IEEE80211_BAND_5GHZ)
672 rate_mask = rate_mask << IWL_FIRST_OFDM_RATE;
674 if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) &&
675 !rs_sta->ibss_sta_added) {
676 u8 sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
678 if (sta_id == IWL_INVALID_STATION) {
679 IWL_DEBUG_RATE("LQ: ADD station %pm\n",
681 sta_id = iwl3945_add_station(priv,
682 hdr->addr1, 0, CMD_ASYNC);
684 if (sta_id != IWL_INVALID_STATION)
685 rs_sta->ibss_sta_added = 1;
688 spin_lock_irqsave(&rs_sta->lock, flags);
690 /* for recent assoc, choose best rate regarding
693 if (rs_sta->start_rate != IWL_RATE_INVALID) {
694 if (rs_sta->start_rate < index &&
695 (rate_mask & (1 << rs_sta->start_rate)))
696 index = rs_sta->start_rate;
697 rs_sta->start_rate = IWL_RATE_INVALID;
700 window = &(rs_sta->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 spin_unlock_irqrestore(&rs_sta->lock, flags);
708 IWL_DEBUG_RATE("Invalid average_tpt on rate %d: "
709 "counter: %d, success_counter: %d, "
710 "expected_tpt is %sNULL\n",
713 window->success_counter,
714 rs_sta->expected_tpt ? "not " : "");
719 current_tpt = window->average_tpt;
721 high_low = iwl3945_get_adjacent_rate(rs_sta, index, rate_mask,
723 low = high_low & 0xff;
724 high = (high_low >> 8) & 0xff;
726 if (low != IWL_RATE_INVALID)
727 low_tpt = rs_sta->win[low].average_tpt;
729 if (high != IWL_RATE_INVALID)
730 high_tpt = rs_sta->win[high].average_tpt;
732 spin_unlock_irqrestore(&rs_sta->lock, flags);
736 if ((window->success_ratio < IWL_RATE_DECREASE_TH) || !current_tpt) {
737 IWL_DEBUG_RATE("decrease rate because of low success_ratio\n");
739 } else if ((low_tpt == IWL_INV_TPT) && (high_tpt == IWL_INV_TPT))
741 else if ((low_tpt != IWL_INV_TPT) && (high_tpt != IWL_INV_TPT) &&
742 (low_tpt < current_tpt) && (high_tpt < current_tpt)) {
743 IWL_DEBUG_RATE("No action -- low [%d] & high [%d] < "
744 "current_tpt [%d]\n",
745 low_tpt, high_tpt, current_tpt);
748 if (high_tpt != IWL_INV_TPT) {
749 if (high_tpt > current_tpt)
753 ("decrease rate because of high tpt\n");
756 } else if (low_tpt != IWL_INV_TPT) {
757 if (low_tpt > current_tpt) {
759 ("decrease rate because of low tpt\n");
766 if (scale_action == -1) {
767 if (window->success_ratio > IWL_SUCCESS_DOWN_TH)
769 } else if (scale_action == 1) {
770 if (window->success_ratio < IWL_SUCCESS_UP_TH) {
771 IWL_DEBUG_RATE("No action -- success_ratio [%d] < "
772 "SUCCESS UP\n", window->success_ratio);
777 switch (scale_action) {
779 if (low != IWL_RATE_INVALID)
784 if (high != IWL_RATE_INVALID)
794 IWL_DEBUG_RATE("Selected %d (action %d) - low %d high %d\n",
795 index, scale_action, low, high);
799 rs_sta->last_txrate_idx = index;
800 if (sband->band == IEEE80211_BAND_5GHZ)
801 info->control.rates[0].idx = rs_sta->last_txrate_idx -
804 info->control.rates[0].idx = rs_sta->last_txrate_idx;
806 IWL_DEBUG_RATE("leave: %d\n", index);
809 #ifdef CONFIG_MAC80211_DEBUGFS
810 static int iwl3945_open_file_generic(struct inode *inode, struct file *file)
812 file->private_data = inode->i_private;
816 static ssize_t iwl3945_sta_dbgfs_stats_table_read(struct file *file,
817 char __user *user_buf,
818 size_t count, loff_t *ppos)
823 struct iwl3945_rs_sta *lq_sta = file->private_data;
825 desc += sprintf(buff + desc, "tx packets=%d last rate index=%d\n"
826 "rate=0x%X flush time %d\n",
828 lq_sta->last_txrate_idx,
829 lq_sta->start_rate, jiffies_to_msecs(lq_sta->flush_time));
830 for (j = 0; j < IWL_RATE_COUNT; j++) {
831 desc += sprintf(buff+desc,
832 "counter=%d success=%d %%=%d\n",
833 lq_sta->win[j].counter,
834 lq_sta->win[j].success_counter,
835 lq_sta->win[j].success_ratio);
837 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
840 static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
841 .read = iwl3945_sta_dbgfs_stats_table_read,
842 .open = iwl3945_open_file_generic,
845 static void iwl3945_add_debugfs(void *priv, void *priv_sta,
848 struct iwl3945_rs_sta *lq_sta = priv_sta;
850 lq_sta->rs_sta_dbgfs_stats_table_file =
851 debugfs_create_file("rate_stats_table", 0600, dir,
852 lq_sta, &rs_sta_dbgfs_stats_table_ops);
856 static void iwl3945_remove_debugfs(void *priv, void *priv_sta)
858 struct iwl3945_rs_sta *lq_sta = priv_sta;
859 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
863 static struct rate_control_ops rs_ops = {
866 .tx_status = rs_tx_status,
867 .get_rate = rs_get_rate,
868 .rate_init = rs_rate_init,
871 .alloc_sta = rs_alloc_sta,
872 .free_sta = rs_free_sta,
873 #ifdef CONFIG_MAC80211_DEBUGFS
874 .add_sta_debugfs = iwl3945_add_debugfs,
875 .remove_sta_debugfs = iwl3945_remove_debugfs,
880 void iwl3945_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id)
882 struct iwl3945_priv *priv = hw->priv;
885 struct iwl3945_rs_sta *rs_sta;
886 struct ieee80211_sta *sta;
887 struct iwl3945_sta_priv *psta;
889 IWL_DEBUG_RATE("enter\n");
893 sta = ieee80211_find_sta(hw, priv->stations[sta_id].sta.sta.addr);
899 psta = (void *) sta->drv_priv;
900 rs_sta = psta->rs_sta;
902 spin_lock_irqsave(&rs_sta->lock, flags);
905 switch (priv->band) {
906 case IEEE80211_BAND_2GHZ:
907 /* TODO: this always does G, not a regression */
908 if (priv->active_rxon.flags & RXON_FLG_TGG_PROTECT_MSK) {
910 rs_sta->expected_tpt = iwl3945_expected_tpt_g_prot;
912 rs_sta->expected_tpt = iwl3945_expected_tpt_g;
915 case IEEE80211_BAND_5GHZ:
916 rs_sta->expected_tpt = iwl3945_expected_tpt_a;
918 case IEEE80211_NUM_BANDS:
923 spin_unlock_irqrestore(&rs_sta->lock, flags);
925 rssi = priv->last_rx_rssi;
927 rssi = IWL_MIN_RSSI_VAL;
929 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RATE, "Network RSSI: %d\n", rssi);
931 rs_sta->start_rate = iwl3945_get_rate_index_by_rssi(rssi, priv->band);
933 IWL_DEBUG_RATE("leave: rssi %d assign rate index: "
934 "%d (plcp 0x%x)\n", rssi, rs_sta->start_rate,
935 iwl3945_rates[rs_sta->start_rate].plcp);
939 int iwl3945_rate_control_register(void)
941 return ieee80211_rate_control_register(&rs_ops);
944 void iwl3945_rate_control_unregister(void)
946 ieee80211_rate_control_unregister(&rs_ops);