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 *****************************************************************************/
26 #include <linux/kernel.h>
27 #include <linux/init.h>
28 #include <linux/skbuff.h>
29 #include <linux/wireless.h>
30 #include <net/mac80211.h>
31 #include <net/ieee80211.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/ieee80211_rate.h"
42 #include "iwl-helpers.h"
44 #define RS_NAME "iwl-4965-rs"
46 #define NUM_TRY_BEFORE_ANTENNA_TOGGLE 1
47 #define IWL_NUMBER_TRY 1
48 #define IWL_HT_NUMBER_TRY 3
50 #define IWL_RATE_MAX_WINDOW 62 /* # tx in history window */
51 #define IWL_RATE_MIN_FAILURE_TH 6 /* min failures to calc tpt */
52 #define IWL_RATE_MIN_SUCCESS_TH 8 /* min successes to calc tpt */
54 /* max time to accum history 2 seconds */
55 #define IWL_RATE_SCALE_FLUSH_INTVL (2*HZ)
57 static u8 rs_ht_to_legacy[] = {
58 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
59 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
61 IWL_RATE_6M_INDEX, IWL_RATE_9M_INDEX,
62 IWL_RATE_12M_INDEX, IWL_RATE_18M_INDEX,
63 IWL_RATE_24M_INDEX, IWL_RATE_36M_INDEX,
64 IWL_RATE_48M_INDEX, IWL_RATE_54M_INDEX
69 } __attribute__ ((packed));
72 * struct iwl4965_rate_scale_data -- tx success history for one rate
74 struct iwl4965_rate_scale_data {
75 u64 data; /* bitmap of successful frames */
76 s32 success_counter; /* number of frames successful */
77 s32 success_ratio; /* per-cent * 128 */
78 s32 counter; /* number of frames attempted */
79 s32 average_tpt; /* success ratio * expected throughput */
84 * struct iwl4965_scale_tbl_info -- tx params and success history for all rates
86 * There are two of these in struct iwl_rate_scale_priv,
87 * one for "active", and one for "search".
89 struct iwl4965_scale_tbl_info {
90 enum iwl4965_table_type lq_type;
91 enum iwl4965_antenna_type antenna_type;
92 u8 is_SGI; /* 1 = short guard interval */
93 u8 is_fat; /* 1 = 40 MHz channel width */
94 u8 is_dup; /* 1 = duplicated data streams */
95 u8 action; /* change modulation; IWL_[LEGACY/SISO/MIMO]_SWITCH_* */
96 s32 *expected_tpt; /* throughput metrics; expected_tpt_G, etc. */
97 struct iwl4965_rate current_rate; /* rate_n_flags, uCode API format */
98 struct iwl4965_rate_scale_data win[IWL_RATE_COUNT]; /* rate histories */
102 * struct iwl_rate_scale_priv -- driver's rate scaling private structure
104 * Pointer to this gets passed back and forth between driver and mac80211.
106 struct iwl4965_rate_scale_priv {
107 u8 active_tbl; /* index of active table, range 0-1 */
108 u8 enable_counter; /* indicates HT mode */
109 u8 stay_in_tbl; /* 1: disallow, 0: allow search for new mode */
110 u8 search_better_tbl; /* 1: currently trying alternate mode */
113 /* The following determine when to search for a new mode */
114 u32 table_count_limit;
115 u32 max_failure_limit; /* # failed frames before new search */
116 u32 max_success_limit; /* # successful frames before new search */
118 u32 total_failed; /* total failed frames, any/all rates */
119 u32 total_success; /* total successful frames, any/all rates */
120 u32 flush_timer; /* time staying in mode before new search */
122 u8 action_counter; /* # mode-switch actions tried */
130 /* The following are bitmaps of rates; IWL_RATE_6M_MASK, etc. */
133 u16 active_siso_rate;
134 u16 active_mimo_rate;
135 u16 active_rate_basic;
137 struct iwl4965_link_quality_cmd lq;
138 struct iwl4965_scale_tbl_info lq_info[LQ_SIZE]; /* "active", "search" */
139 #ifdef CONFIG_MAC80211_DEBUGFS
140 struct dentry *rs_sta_dbgfs_scale_table_file;
141 struct dentry *rs_sta_dbgfs_stats_table_file;
142 struct iwl4965_rate dbg_fixed;
143 struct iwl4965_priv *drv;
147 static void rs_rate_scale_perform(struct iwl4965_priv *priv,
148 struct net_device *dev,
149 struct ieee80211_hdr *hdr,
150 struct sta_info *sta);
151 static void rs_fill_link_cmd(struct iwl4965_rate_scale_priv *lq_data,
152 struct iwl4965_rate *tx_mcs,
153 struct iwl4965_link_quality_cmd *tbl);
156 #ifdef CONFIG_MAC80211_DEBUGFS
157 static void rs_dbgfs_set_mcs(struct iwl4965_rate_scale_priv *rs_priv,
158 struct iwl4965_rate *mcs, int index);
160 static void rs_dbgfs_set_mcs(struct iwl4965_rate_scale_priv *rs_priv,
161 struct iwl4965_rate *mcs, int index)
166 * Expected throughput metrics for following rates:
167 * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
168 * "G" is the only table that supports CCK (the first 4 rates).
170 static s32 expected_tpt_A[IWL_RATE_COUNT] = {
171 0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186, 186
174 static s32 expected_tpt_G[IWL_RATE_COUNT] = {
175 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 186
178 static s32 expected_tpt_siso20MHz[IWL_RATE_COUNT] = {
179 0, 0, 0, 0, 42, 42, 76, 102, 124, 159, 183, 193, 202
182 static s32 expected_tpt_siso20MHzSGI[IWL_RATE_COUNT] = {
183 0, 0, 0, 0, 46, 46, 82, 110, 132, 168, 192, 202, 211
186 static s32 expected_tpt_mimo20MHz[IWL_RATE_COUNT] = {
187 0, 0, 0, 0, 74, 74, 123, 155, 179, 214, 236, 244, 251
190 static s32 expected_tpt_mimo20MHzSGI[IWL_RATE_COUNT] = {
191 0, 0, 0, 0, 81, 81, 131, 164, 188, 222, 243, 251, 257
194 static s32 expected_tpt_siso40MHz[IWL_RATE_COUNT] = {
195 0, 0, 0, 0, 77, 77, 127, 160, 184, 220, 242, 250, 257
198 static s32 expected_tpt_siso40MHzSGI[IWL_RATE_COUNT] = {
199 0, 0, 0, 0, 83, 83, 135, 169, 193, 229, 250, 257, 264
202 static s32 expected_tpt_mimo40MHz[IWL_RATE_COUNT] = {
203 0, 0, 0, 0, 123, 123, 182, 214, 235, 264, 279, 285, 289
206 static s32 expected_tpt_mimo40MHzSGI[IWL_RATE_COUNT] = {
207 0, 0, 0, 0, 131, 131, 191, 222, 242, 270, 284, 289, 293
210 static int iwl4965_lq_sync_callback(struct iwl4965_priv *priv,
211 struct iwl4965_cmd *cmd, struct sk_buff *skb)
213 /*We didn't cache the SKB; let the caller free it */
217 static inline u8 iwl4965_rate_get_rate(u32 rate_n_flags)
219 return (u8)(rate_n_flags & 0xFF);
222 static int rs_send_lq_cmd(struct iwl4965_priv *priv,
223 struct iwl4965_link_quality_cmd *lq, u8 flags)
225 #ifdef CONFIG_IWL4965_DEBUG
228 struct iwl4965_host_cmd cmd = {
229 .id = REPLY_TX_LINK_QUALITY_CMD,
230 .len = sizeof(struct iwl4965_link_quality_cmd),
235 if ((lq->sta_id == 0xFF) &&
236 (priv->iw_mode == IEEE80211_IF_TYPE_IBSS))
239 if (lq->sta_id == 0xFF)
240 lq->sta_id = IWL_AP_ID;
242 IWL_DEBUG_RATE("lq station id 0x%x\n", lq->sta_id);
243 IWL_DEBUG_RATE("lq dta 0x%X 0x%X\n",
244 lq->general_params.single_stream_ant_msk,
245 lq->general_params.dual_stream_ant_msk);
246 #ifdef CONFIG_IWL4965_DEBUG
247 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
248 IWL_DEBUG_RATE("lq index %d 0x%X\n",
249 i, lq->rs_table[i].rate_n_flags);
252 if (flags & CMD_ASYNC)
253 cmd.meta.u.callback = iwl4965_lq_sync_callback;
255 if (iwl4965_is_associated(priv) && priv->assoc_station_added &&
256 priv->lq_mngr.lq_ready)
257 return iwl4965_send_cmd(priv, &cmd);
262 static void rs_rate_scale_clear_window(struct iwl4965_rate_scale_data *window)
265 window->success_counter = 0;
266 window->success_ratio = IWL_INVALID_VALUE;
268 window->average_tpt = IWL_INVALID_VALUE;
273 * rs_collect_tx_data - Update the success/failure sliding window
275 * We keep a sliding window of the last 62 packets transmitted
276 * at this rate. window->data contains the bitmask of successful
279 static int rs_collect_tx_data(struct iwl4965_rate_scale_data *windows,
280 int scale_index, s32 tpt, u32 status)
282 struct iwl4965_rate_scale_data *window = NULL;
284 u8 win_size = IWL_RATE_MAX_WINDOW;
287 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
290 /* Select data for current tx bit rate */
291 window = &(windows[scale_index]);
294 * Keep track of only the latest 62 tx frame attempts in this rate's
295 * history window; anything older isn't really relevant any more.
296 * If we have filled up the sliding window, drop the oldest attempt;
297 * if the oldest attempt (highest bit in bitmap) shows "success",
298 * subtract "1" from the success counter (this is the main reason
299 * we keep these bitmaps!).
301 if (window->counter >= win_size) {
302 window->counter = win_size - 1;
304 mask = (mask << (win_size - 1));
305 if ((window->data & mask)) {
306 window->data &= ~mask;
307 window->success_counter = window->success_counter - 1;
311 /* Increment frames-attempted counter */
312 window->counter = window->counter + 1;
314 /* Shift bitmap by one frame (throw away oldest history),
315 * OR in "1", and increment "success" if this frame was successful. */
317 window->data = (mask << 1);
319 window->success_counter = window->success_counter + 1;
323 /* Calculate current success ratio, avoid divide-by-0! */
324 if (window->counter > 0)
325 window->success_ratio = 128 * (100 * window->success_counter)
328 window->success_ratio = IWL_INVALID_VALUE;
330 fail_count = window->counter - window->success_counter;
332 /* Calculate average throughput, if we have enough history. */
333 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
334 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
335 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
337 window->average_tpt = IWL_INVALID_VALUE;
339 /* Tag this window as having been updated */
340 window->stamp = jiffies;
346 * Fill uCode API rate_n_flags field, based on "search" or "active" table.
348 static void rs_mcs_from_tbl(struct iwl4965_rate *mcs_rate,
349 struct iwl4965_scale_tbl_info *tbl,
350 int index, u8 use_green)
352 if (is_legacy(tbl->lq_type)) {
353 mcs_rate->rate_n_flags = iwl4965_rates[index].plcp;
354 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
355 mcs_rate->rate_n_flags |= RATE_MCS_CCK_MSK;
357 } else if (is_siso(tbl->lq_type)) {
358 if (index > IWL_LAST_OFDM_RATE)
359 index = IWL_LAST_OFDM_RATE;
360 mcs_rate->rate_n_flags = iwl4965_rates[index].plcp_siso |
363 if (index > IWL_LAST_OFDM_RATE)
364 index = IWL_LAST_OFDM_RATE;
365 mcs_rate->rate_n_flags = iwl4965_rates[index].plcp_mimo |
369 switch (tbl->antenna_type) {
371 mcs_rate->rate_n_flags |= RATE_MCS_ANT_AB_MSK;
374 mcs_rate->rate_n_flags |= RATE_MCS_ANT_A_MSK;
377 mcs_rate->rate_n_flags |= RATE_MCS_ANT_B_MSK;
383 if (is_legacy(tbl->lq_type))
388 mcs_rate->rate_n_flags |= RATE_MCS_DUP_MSK;
390 mcs_rate->rate_n_flags |= RATE_MCS_FAT_MSK;
393 mcs_rate->rate_n_flags |= RATE_MCS_SGI_MSK;
396 mcs_rate->rate_n_flags |= RATE_MCS_GF_MSK;
397 if (is_siso(tbl->lq_type))
398 mcs_rate->rate_n_flags &= ~RATE_MCS_SGI_MSK;
403 * Interpret uCode API's rate_n_flags format,
404 * fill "search" or "active" tx mode table.
406 static int rs_get_tbl_info_from_mcs(const struct iwl4965_rate *mcs_rate,
407 int phymode, struct iwl4965_scale_tbl_info *tbl,
413 index = iwl4965_rate_index_from_plcp(mcs_rate->rate_n_flags);
415 if (index == IWL_RATE_INVALID) {
419 tbl->is_SGI = 0; /* default legacy setup */
422 tbl->antenna_type = ANT_BOTH; /* default MIMO setup */
424 /* legacy rate format */
425 if (!(mcs_rate->rate_n_flags & RATE_MCS_HT_MSK)) {
426 ant_msk = (mcs_rate->rate_n_flags & RATE_MCS_ANT_AB_MSK);
428 if (ant_msk == RATE_MCS_ANT_AB_MSK)
429 tbl->lq_type = LQ_NONE;
432 if (phymode == MODE_IEEE80211A)
437 if (mcs_rate->rate_n_flags & RATE_MCS_ANT_A_MSK)
438 tbl->antenna_type = ANT_MAIN;
440 tbl->antenna_type = ANT_AUX;
444 /* HT rate format, SISO (might be 20 MHz legacy or 40 MHz fat width) */
445 } else if (iwl4965_rate_get_rate(mcs_rate->rate_n_flags)
446 <= IWL_RATE_SISO_60M_PLCP) {
447 tbl->lq_type = LQ_SISO;
449 ant_msk = (mcs_rate->rate_n_flags & RATE_MCS_ANT_AB_MSK);
450 if (ant_msk == RATE_MCS_ANT_AB_MSK)
451 tbl->lq_type = LQ_NONE;
453 if (mcs_rate->rate_n_flags & RATE_MCS_ANT_A_MSK)
454 tbl->antenna_type = ANT_MAIN;
456 tbl->antenna_type = ANT_AUX;
458 if (mcs_rate->rate_n_flags & RATE_MCS_SGI_MSK)
461 if ((mcs_rate->rate_n_flags & RATE_MCS_FAT_MSK) ||
462 (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK))
465 if (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK)
470 /* HT rate format, MIMO (might be 20 MHz legacy or 40 MHz fat width) */
472 tbl->lq_type = LQ_MIMO;
473 if (mcs_rate->rate_n_flags & RATE_MCS_SGI_MSK)
476 if ((mcs_rate->rate_n_flags & RATE_MCS_FAT_MSK) ||
477 (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK))
480 if (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK)
487 static inline void rs_toggle_antenna(struct iwl4965_rate *new_rate,
488 struct iwl4965_scale_tbl_info *tbl)
490 if (tbl->antenna_type == ANT_AUX) {
491 tbl->antenna_type = ANT_MAIN;
492 new_rate->rate_n_flags &= ~RATE_MCS_ANT_B_MSK;
493 new_rate->rate_n_flags |= RATE_MCS_ANT_A_MSK;
495 tbl->antenna_type = ANT_AUX;
496 new_rate->rate_n_flags &= ~RATE_MCS_ANT_A_MSK;
497 new_rate->rate_n_flags |= RATE_MCS_ANT_B_MSK;
501 static inline u8 rs_use_green(struct iwl4965_priv *priv,
502 struct ieee80211_conf *conf)
504 #ifdef CONFIG_IWL4965_HT
505 return ((conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) &&
506 priv->current_ht_config.is_green_field &&
507 !priv->current_ht_config.non_GF_STA_present);
508 #endif /* CONFIG_IWL4965_HT */
513 * rs_get_supported_rates - get the available rates
515 * if management frame or broadcast frame only return
516 * basic available rates.
519 static void rs_get_supported_rates(struct iwl4965_rate_scale_priv *lq_data,
520 struct ieee80211_hdr *hdr,
521 enum iwl4965_table_type rate_type,
524 if (is_legacy(rate_type))
525 *data_rate = lq_data->active_rate;
527 if (is_siso(rate_type))
528 *data_rate = lq_data->active_siso_rate;
530 *data_rate = lq_data->active_mimo_rate;
533 if (hdr && is_multicast_ether_addr(hdr->addr1) &&
534 lq_data->active_rate_basic)
535 *data_rate = lq_data->active_rate_basic;
538 static u16 rs_get_adjacent_rate(u8 index, u16 rate_mask, int rate_type)
540 u8 high = IWL_RATE_INVALID;
541 u8 low = IWL_RATE_INVALID;
543 /* 802.11A or ht walks to the next literal adjacent rate in
545 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
549 /* Find the previous rate that is in the rate mask */
551 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
552 if (rate_mask & mask) {
558 /* Find the next rate that is in the rate mask */
560 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
561 if (rate_mask & mask) {
567 return (high << 8) | low;
571 while (low != IWL_RATE_INVALID) {
572 low = iwl4965_rates[low].prev_rs;
573 if (low == IWL_RATE_INVALID)
575 if (rate_mask & (1 << low))
577 IWL_DEBUG_RATE("Skipping masked lower rate: %d\n", low);
581 while (high != IWL_RATE_INVALID) {
582 high = iwl4965_rates[high].next_rs;
583 if (high == IWL_RATE_INVALID)
585 if (rate_mask & (1 << high))
587 IWL_DEBUG_RATE("Skipping masked higher rate: %d\n", high);
590 return (high << 8) | low;
593 static void rs_get_lower_rate(struct iwl4965_rate_scale_priv *lq_data,
594 struct iwl4965_scale_tbl_info *tbl, u8 scale_index,
595 u8 ht_possible, struct iwl4965_rate *mcs_rate)
600 u8 switch_to_legacy = 0;
601 u8 is_green = lq_data->is_green;
603 /* check if we need to switch from HT to legacy rates.
604 * assumption is that mandatory rates (1Mbps or 6Mbps)
605 * are always supported (spec demand) */
606 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
607 switch_to_legacy = 1;
608 scale_index = rs_ht_to_legacy[scale_index];
609 if (lq_data->phymode == MODE_IEEE80211A)
614 if ((tbl->antenna_type == ANT_BOTH) ||
615 (tbl->antenna_type == ANT_NONE))
616 tbl->antenna_type = ANT_MAIN;
622 rs_get_supported_rates(lq_data, NULL, tbl->lq_type, &rate_mask);
624 /* Mask with station rate restriction */
625 if (is_legacy(tbl->lq_type)) {
626 /* supp_rates has no CCK bits in A mode */
627 if (lq_data->phymode == (u8) MODE_IEEE80211A)
628 rate_mask = (u16)(rate_mask &
629 (lq_data->supp_rates << IWL_FIRST_OFDM_RATE));
631 rate_mask = (u16)(rate_mask & lq_data->supp_rates);
634 /* If we switched from HT to legacy, check current rate */
635 if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
636 rs_mcs_from_tbl(mcs_rate, tbl, scale_index, is_green);
640 high_low = rs_get_adjacent_rate(scale_index, rate_mask, tbl->lq_type);
641 low = high_low & 0xff;
643 if (low != IWL_RATE_INVALID)
644 rs_mcs_from_tbl(mcs_rate, tbl, low, is_green);
646 rs_mcs_from_tbl(mcs_rate, tbl, scale_index, is_green);
650 * mac80211 sends us Tx status
652 static void rs_tx_status(void *priv_rate,
653 struct net_device *dev,
655 struct ieee80211_tx_status *tx_resp)
659 int rs_index, index = 0;
660 struct iwl4965_rate_scale_priv *lq;
661 struct iwl4965_link_quality_cmd *table;
662 struct sta_info *sta;
663 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
664 struct iwl4965_priv *priv = (struct iwl4965_priv *)priv_rate;
665 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
666 struct iwl4965_rate_scale_data *window = NULL;
667 struct iwl4965_rate_scale_data *search_win = NULL;
668 struct iwl4965_rate tx_mcs;
669 struct iwl4965_scale_tbl_info tbl_type;
670 struct iwl4965_scale_tbl_info *curr_tbl, *search_tbl;
672 u16 fc = le16_to_cpu(hdr->frame_control);
675 IWL_DEBUG_RATE_LIMIT("get frame ack response, update rate scale window\n");
677 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1))
680 retries = tx_resp->retry_count;
686 sta = sta_info_get(local, hdr->addr1);
688 if (!sta || !sta->rate_ctrl_priv) {
694 lq = (struct iwl4965_rate_scale_priv *)sta->rate_ctrl_priv;
696 if (!priv->lq_mngr.lq_ready)
699 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) && !lq->ibss_sta_added)
703 active_index = lq->active_tbl;
705 /* Get mac80211 antenna info */
706 lq->antenna = (lq->valid_antenna & local->hw.conf.antenna_sel_tx);
708 lq->antenna = lq->valid_antenna;
710 /* Ignore mac80211 antenna info for now */
711 lq->antenna = lq->valid_antenna;
713 curr_tbl = &(lq->lq_info[active_index]);
714 search_tbl = &(lq->lq_info[(1 - active_index)]);
715 window = (struct iwl4965_rate_scale_data *)
717 search_win = (struct iwl4965_rate_scale_data *)
718 &(search_tbl->win[0]);
720 tx_mcs.rate_n_flags = tx_resp->control.tx_rate;
722 rs_get_tbl_info_from_mcs(&tx_mcs, priv->phymode,
723 &tbl_type, &rs_index);
724 if ((rs_index < 0) || (rs_index >= IWL_RATE_COUNT)) {
725 IWL_DEBUG_RATE("bad rate index at: %d rate 0x%X\n",
726 rs_index, tx_mcs.rate_n_flags);
732 * Ignore this Tx frame response if its initial rate doesn't match
733 * that of latest Link Quality command. There may be stragglers
734 * from a previous Link Quality command, but we're no longer interested
735 * in those; they're either from the "active" mode while we're trying
736 * to check "search" mode, or a prior "search" mode after we've moved
737 * to a new "search" mode (which might become the new "active" mode).
740 (tx_mcs.rate_n_flags !=
741 le32_to_cpu(table->rs_table[0].rate_n_flags))) {
742 IWL_DEBUG_RATE("initial rate does not match 0x%x 0x%x\n",
744 le32_to_cpu(table->rs_table[0].rate_n_flags));
749 /* Update frame history window with "failure" for each Tx retry. */
751 /* Look up the rate and other info used for each tx attempt.
752 * Each tx attempt steps one entry deeper in the rate table. */
753 tx_mcs.rate_n_flags =
754 le32_to_cpu(table->rs_table[index].rate_n_flags);
755 rs_get_tbl_info_from_mcs(&tx_mcs, priv->phymode,
756 &tbl_type, &rs_index);
758 /* If type matches "search" table,
759 * add failure to "search" history */
760 if ((tbl_type.lq_type == search_tbl->lq_type) &&
761 (tbl_type.antenna_type == search_tbl->antenna_type) &&
762 (tbl_type.is_SGI == search_tbl->is_SGI)) {
763 if (search_tbl->expected_tpt)
764 tpt = search_tbl->expected_tpt[rs_index];
767 rs_collect_tx_data(search_win, rs_index, tpt, 0);
769 /* Else if type matches "current/active" table,
770 * add failure to "current/active" history */
771 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
772 (tbl_type.antenna_type == curr_tbl->antenna_type) &&
773 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
774 if (curr_tbl->expected_tpt)
775 tpt = curr_tbl->expected_tpt[rs_index];
778 rs_collect_tx_data(window, rs_index, tpt, 0);
781 /* If not searching for a new mode, increment failed counter
782 * ... this helps determine when to start searching again */
791 * Find (by rate) the history window to update with final Tx attempt;
792 * if Tx was successful first try, use original rate,
793 * else look up the rate that was, finally, successful.
795 if (!tx_resp->retry_count)
796 tx_mcs.rate_n_flags = tx_resp->control.tx_rate;
798 tx_mcs.rate_n_flags =
799 le32_to_cpu(table->rs_table[index].rate_n_flags);
801 rs_get_tbl_info_from_mcs(&tx_mcs, priv->phymode,
802 &tbl_type, &rs_index);
804 /* Update frame history window with "success" if Tx got ACKed ... */
805 if (tx_resp->flags & IEEE80211_TX_STATUS_ACK)
810 /* If type matches "search" table,
811 * add final tx status to "search" history */
812 if ((tbl_type.lq_type == search_tbl->lq_type) &&
813 (tbl_type.antenna_type == search_tbl->antenna_type) &&
814 (tbl_type.is_SGI == search_tbl->is_SGI)) {
815 if (search_tbl->expected_tpt)
816 tpt = search_tbl->expected_tpt[rs_index];
819 rs_collect_tx_data(search_win,
820 rs_index, tpt, status);
822 /* Else if type matches "current/active" table,
823 * add final tx status to "current/active" history */
824 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
825 (tbl_type.antenna_type == curr_tbl->antenna_type) &&
826 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
827 if (curr_tbl->expected_tpt)
828 tpt = curr_tbl->expected_tpt[rs_index];
831 rs_collect_tx_data(window, rs_index, tpt, status);
834 /* If not searching for new mode, increment success/failed counter
835 * ... these help determine when to start searching again */
836 if (lq->stay_in_tbl) {
843 /* See if there's a better rate or modulation mode to try. */
844 rs_rate_scale_perform(priv, dev, hdr, sta);
849 static u8 rs_is_ant_connected(u8 valid_antenna,
850 enum iwl4965_antenna_type antenna_type)
852 if (antenna_type == ANT_AUX)
853 return ((valid_antenna & 0x2) ? 1:0);
854 else if (antenna_type == ANT_MAIN)
855 return ((valid_antenna & 0x1) ? 1:0);
856 else if (antenna_type == ANT_BOTH)
857 return ((valid_antenna & 0x3) == 0x3);
862 static u8 rs_is_other_ant_connected(u8 valid_antenna,
863 enum iwl4965_antenna_type antenna_type)
865 if (antenna_type == ANT_AUX)
866 return rs_is_ant_connected(valid_antenna, ANT_MAIN);
868 return rs_is_ant_connected(valid_antenna, ANT_AUX);
874 * Begin a period of staying with a selected modulation mode.
875 * Set "stay_in_tbl" flag to prevent any mode switches.
876 * Set frame tx success limits according to legacy vs. high-throughput,
877 * and reset overall (spanning all rates) tx success history statistics.
878 * These control how long we stay using same modulation mode before
879 * searching for a new mode.
881 static void rs_set_stay_in_table(u8 is_legacy,
882 struct iwl4965_rate_scale_priv *lq_data)
884 IWL_DEBUG_HT("we are staying in the same table\n");
885 lq_data->stay_in_tbl = 1; /* only place this gets set */
887 lq_data->table_count_limit = IWL_LEGACY_TABLE_COUNT;
888 lq_data->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
889 lq_data->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
891 lq_data->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
892 lq_data->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
893 lq_data->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
895 lq_data->table_count = 0;
896 lq_data->total_failed = 0;
897 lq_data->total_success = 0;
901 * Find correct throughput table for given mode of modulation
903 static void rs_get_expected_tpt_table(struct iwl4965_rate_scale_priv *lq_data,
904 struct iwl4965_scale_tbl_info *tbl)
906 if (is_legacy(tbl->lq_type)) {
907 if (!is_a_band(tbl->lq_type))
908 tbl->expected_tpt = expected_tpt_G;
910 tbl->expected_tpt = expected_tpt_A;
911 } else if (is_siso(tbl->lq_type)) {
912 if (tbl->is_fat && !lq_data->is_dup)
914 tbl->expected_tpt = expected_tpt_siso40MHzSGI;
916 tbl->expected_tpt = expected_tpt_siso40MHz;
917 else if (tbl->is_SGI)
918 tbl->expected_tpt = expected_tpt_siso20MHzSGI;
920 tbl->expected_tpt = expected_tpt_siso20MHz;
922 } else if (is_mimo(tbl->lq_type)) {
923 if (tbl->is_fat && !lq_data->is_dup)
925 tbl->expected_tpt = expected_tpt_mimo40MHzSGI;
927 tbl->expected_tpt = expected_tpt_mimo40MHz;
928 else if (tbl->is_SGI)
929 tbl->expected_tpt = expected_tpt_mimo20MHzSGI;
931 tbl->expected_tpt = expected_tpt_mimo20MHz;
933 tbl->expected_tpt = expected_tpt_G;
936 #ifdef CONFIG_IWL4965_HT
938 * Find starting rate for new "search" high-throughput mode of modulation.
939 * Goal is to find lowest expected rate (under perfect conditions) that is
940 * above the current measured throughput of "active" mode, to give new mode
941 * a fair chance to prove itself without too many challenges.
943 * This gets called when transitioning to more aggressive modulation
944 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
945 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
946 * to decrease to match "active" throughput. When moving from MIMO to SISO,
947 * bit rate will typically need to increase, but not if performance was bad.
949 static s32 rs_get_best_rate(struct iwl4965_priv *priv,
950 struct iwl4965_rate_scale_priv *lq_data,
951 struct iwl4965_scale_tbl_info *tbl, /* "search" */
952 u16 rate_mask, s8 index, s8 rate)
954 /* "active" values */
955 struct iwl4965_scale_tbl_info *active_tbl =
956 &(lq_data->lq_info[lq_data->active_tbl]);
957 s32 active_sr = active_tbl->win[index].success_ratio;
958 s32 active_tpt = active_tbl->expected_tpt[index];
960 /* expected "search" throughput */
961 s32 *tpt_tbl = tbl->expected_tpt;
963 s32 new_rate, high, low, start_hi;
966 new_rate = high = low = start_hi = IWL_RATE_INVALID;
969 high_low = rs_get_adjacent_rate(rate, rate_mask, tbl->lq_type);
971 low = high_low & 0xff;
972 high = (high_low >> 8) & 0xff;
975 * Lower the "search" bit rate, to give new "search" mode
976 * approximately the same throughput as "active" if:
978 * 1) "Active" mode has been working modestly well (but not
979 * great), and expected "search" throughput (under perfect
980 * conditions) at candidate rate is above the actual
981 * measured "active" throughput (but less than expected
982 * "active" throughput under perfect conditions).
984 * 2) "Active" mode has been working perfectly or very well
985 * and expected "search" throughput (under perfect
986 * conditions) at candidate rate is above expected
987 * "active" throughput (under perfect conditions).
989 if ((((100 * tpt_tbl[rate]) > lq_data->last_tpt) &&
990 ((active_sr > IWL_RATE_DECREASE_TH) &&
991 (active_sr <= IWL_RATE_HIGH_TH) &&
992 (tpt_tbl[rate] <= active_tpt))) ||
993 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
994 (tpt_tbl[rate] > active_tpt))) {
996 /* (2nd or later pass)
997 * If we've already tried to raise the rate, and are
998 * now trying to lower it, use the higher rate. */
999 if (start_hi != IWL_RATE_INVALID) {
1000 new_rate = start_hi;
1006 /* Loop again with lower rate */
1007 if (low != IWL_RATE_INVALID)
1010 /* Lower rate not available, use the original */
1014 /* Else try to raise the "search" rate to match "active" */
1016 /* (2nd or later pass)
1017 * If we've already tried to lower the rate, and are
1018 * now trying to raise it, use the lower rate. */
1019 if (new_rate != IWL_RATE_INVALID)
1022 /* Loop again with higher rate */
1023 else if (high != IWL_RATE_INVALID) {
1027 /* Higher rate not available, use the original */
1037 #endif /* CONFIG_IWL4965_HT */
1039 static inline u8 rs_is_both_ant_supp(u8 valid_antenna)
1041 return (rs_is_ant_connected(valid_antenna, ANT_BOTH));
1045 * Set up search table for MIMO
1047 static int rs_switch_to_mimo(struct iwl4965_priv *priv,
1048 struct iwl4965_rate_scale_priv *lq_data,
1049 struct ieee80211_conf *conf,
1050 struct sta_info *sta,
1051 struct iwl4965_scale_tbl_info *tbl, int index)
1053 #ifdef CONFIG_IWL4965_HT
1056 s8 is_green = lq_data->is_green;
1058 if (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) ||
1059 !sta->ht_info.ht_supported)
1062 IWL_DEBUG_HT("LQ: try to switch to MIMO\n");
1063 tbl->lq_type = LQ_MIMO;
1064 rs_get_supported_rates(lq_data, NULL, tbl->lq_type,
1067 if (priv->current_ht_config.tx_mimo_ps_mode == IWL_MIMO_PS_STATIC)
1070 /* Need both Tx chains/antennas to support MIMO */
1071 if (!rs_is_both_ant_supp(lq_data->antenna))
1074 tbl->is_dup = lq_data->is_dup;
1076 if (priv->current_ht_config.supported_chan_width
1077 == IWL_CHANNEL_WIDTH_40MHZ)
1083 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
1087 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
1092 rs_get_expected_tpt_table(lq_data, tbl);
1094 rate = rs_get_best_rate(priv, lq_data, tbl, rate_mask, index, index);
1096 IWL_DEBUG_HT("LQ: MIMO best rate %d mask %X\n", rate, rate_mask);
1097 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask))
1099 rs_mcs_from_tbl(&tbl->current_rate, tbl, rate, is_green);
1101 IWL_DEBUG_HT("LQ: Switch to new mcs %X index is green %X\n",
1102 tbl->current_rate.rate_n_flags, is_green);
1106 #endif /*CONFIG_IWL4965_HT */
1110 * Set up search table for SISO
1112 static int rs_switch_to_siso(struct iwl4965_priv *priv,
1113 struct iwl4965_rate_scale_priv *lq_data,
1114 struct ieee80211_conf *conf,
1115 struct sta_info *sta,
1116 struct iwl4965_scale_tbl_info *tbl, int index)
1118 #ifdef CONFIG_IWL4965_HT
1120 u8 is_green = lq_data->is_green;
1123 IWL_DEBUG_HT("LQ: try to switch to SISO\n");
1124 if (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) ||
1125 !sta->ht_info.ht_supported)
1128 tbl->is_dup = lq_data->is_dup;
1129 tbl->lq_type = LQ_SISO;
1131 rs_get_supported_rates(lq_data, NULL, tbl->lq_type,
1134 if (priv->current_ht_config.supported_chan_width
1135 == IWL_CHANNEL_WIDTH_40MHZ)
1141 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
1145 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
1153 rs_get_expected_tpt_table(lq_data, tbl);
1154 rate = rs_get_best_rate(priv, lq_data, tbl, rate_mask, index, index);
1156 IWL_DEBUG_HT("LQ: get best rate %d mask %X\n", rate, rate_mask);
1157 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1158 IWL_DEBUG_HT("can not switch with index %d rate mask %x\n",
1162 rs_mcs_from_tbl(&tbl->current_rate, tbl, rate, is_green);
1163 IWL_DEBUG_HT("LQ: Switch to new mcs %X index is green %X\n",
1164 tbl->current_rate.rate_n_flags, is_green);
1169 #endif /*CONFIG_IWL4965_HT */
1173 * Try to switch to new modulation mode from legacy
1175 static int rs_move_legacy_other(struct iwl4965_priv *priv,
1176 struct iwl4965_rate_scale_priv *lq_data,
1177 struct ieee80211_conf *conf,
1178 struct sta_info *sta,
1182 struct iwl4965_scale_tbl_info *tbl =
1183 &(lq_data->lq_info[lq_data->active_tbl]);
1184 struct iwl4965_scale_tbl_info *search_tbl =
1185 &(lq_data->lq_info[(1 - lq_data->active_tbl)]);
1186 struct iwl4965_rate_scale_data *window = &(tbl->win[index]);
1187 u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
1188 (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
1189 u8 start_action = tbl->action;
1192 switch (tbl->action) {
1193 case IWL_LEGACY_SWITCH_ANTENNA:
1194 IWL_DEBUG_HT("LQ Legacy switch Antenna\n");
1196 search_tbl->lq_type = LQ_NONE;
1197 lq_data->action_counter++;
1199 /* Don't change antenna if success has been great */
1200 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1203 /* Don't change antenna if other one is not connected */
1204 if (!rs_is_other_ant_connected(lq_data->antenna,
1208 /* Set up search table to try other antenna */
1209 memcpy(search_tbl, tbl, sz);
1211 rs_toggle_antenna(&(search_tbl->current_rate),
1213 rs_get_expected_tpt_table(lq_data, search_tbl);
1214 lq_data->search_better_tbl = 1;
1217 case IWL_LEGACY_SWITCH_SISO:
1218 IWL_DEBUG_HT("LQ: Legacy switch to SISO\n");
1220 /* Set up search table to try SISO */
1221 memcpy(search_tbl, tbl, sz);
1222 search_tbl->lq_type = LQ_SISO;
1223 search_tbl->is_SGI = 0;
1224 search_tbl->is_fat = 0;
1225 ret = rs_switch_to_siso(priv, lq_data, conf, sta,
1228 lq_data->search_better_tbl = 1;
1229 lq_data->action_counter = 0;
1234 case IWL_LEGACY_SWITCH_MIMO:
1235 IWL_DEBUG_HT("LQ: Legacy switch MIMO\n");
1237 /* Set up search table to try MIMO */
1238 memcpy(search_tbl, tbl, sz);
1239 search_tbl->lq_type = LQ_MIMO;
1240 search_tbl->is_SGI = 0;
1241 search_tbl->is_fat = 0;
1242 search_tbl->antenna_type = ANT_BOTH;
1243 ret = rs_switch_to_mimo(priv, lq_data, conf, sta,
1246 lq_data->search_better_tbl = 1;
1247 lq_data->action_counter = 0;
1253 if (tbl->action > IWL_LEGACY_SWITCH_MIMO)
1254 tbl->action = IWL_LEGACY_SWITCH_ANTENNA;
1256 if (tbl->action == start_action)
1264 if (tbl->action > IWL_LEGACY_SWITCH_MIMO)
1265 tbl->action = IWL_LEGACY_SWITCH_ANTENNA;
1271 * Try to switch to new modulation mode from SISO
1273 static int rs_move_siso_to_other(struct iwl4965_priv *priv,
1274 struct iwl4965_rate_scale_priv *lq_data,
1275 struct ieee80211_conf *conf,
1276 struct sta_info *sta,
1280 u8 is_green = lq_data->is_green;
1281 struct iwl4965_scale_tbl_info *tbl =
1282 &(lq_data->lq_info[lq_data->active_tbl]);
1283 struct iwl4965_scale_tbl_info *search_tbl =
1284 &(lq_data->lq_info[(1 - lq_data->active_tbl)]);
1285 struct iwl4965_rate_scale_data *window = &(tbl->win[index]);
1286 u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
1287 (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
1288 u8 start_action = tbl->action;
1291 lq_data->action_counter++;
1292 switch (tbl->action) {
1293 case IWL_SISO_SWITCH_ANTENNA:
1294 IWL_DEBUG_HT("LQ: SISO SWITCH ANTENNA SISO\n");
1295 search_tbl->lq_type = LQ_NONE;
1296 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1298 if (!rs_is_other_ant_connected(lq_data->antenna,
1302 memcpy(search_tbl, tbl, sz);
1303 search_tbl->action = IWL_SISO_SWITCH_MIMO;
1304 rs_toggle_antenna(&(search_tbl->current_rate),
1306 lq_data->search_better_tbl = 1;
1310 case IWL_SISO_SWITCH_MIMO:
1311 IWL_DEBUG_HT("LQ: SISO SWITCH TO MIMO FROM SISO\n");
1312 memcpy(search_tbl, tbl, sz);
1313 search_tbl->lq_type = LQ_MIMO;
1314 search_tbl->is_SGI = 0;
1315 search_tbl->is_fat = 0;
1316 search_tbl->antenna_type = ANT_BOTH;
1317 ret = rs_switch_to_mimo(priv, lq_data, conf, sta,
1320 lq_data->search_better_tbl = 1;
1324 case IWL_SISO_SWITCH_GI:
1325 IWL_DEBUG_HT("LQ: SISO SWITCH TO GI\n");
1326 memcpy(search_tbl, tbl, sz);
1327 search_tbl->action = 0;
1328 if (search_tbl->is_SGI)
1329 search_tbl->is_SGI = 0;
1331 search_tbl->is_SGI = 1;
1334 lq_data->search_better_tbl = 1;
1335 if ((tbl->lq_type == LQ_SISO) &&
1337 s32 tpt = lq_data->last_tpt / 100;
1338 if (((!tbl->is_fat) &&
1339 (tpt >= expected_tpt_siso20MHz[index])) ||
1341 (tpt >= expected_tpt_siso40MHz[index])))
1342 lq_data->search_better_tbl = 0;
1344 rs_get_expected_tpt_table(lq_data, search_tbl);
1345 rs_mcs_from_tbl(&search_tbl->current_rate,
1346 search_tbl, index, is_green);
1350 if (tbl->action > IWL_SISO_SWITCH_GI)
1351 tbl->action = IWL_SISO_SWITCH_ANTENNA;
1353 if (tbl->action == start_action)
1360 if (tbl->action > IWL_SISO_SWITCH_GI)
1361 tbl->action = IWL_SISO_SWITCH_ANTENNA;
1366 * Try to switch to new modulation mode from MIMO
1368 static int rs_move_mimo_to_other(struct iwl4965_priv *priv,
1369 struct iwl4965_rate_scale_priv *lq_data,
1370 struct ieee80211_conf *conf,
1371 struct sta_info *sta,
1375 s8 is_green = lq_data->is_green;
1376 struct iwl4965_scale_tbl_info *tbl =
1377 &(lq_data->lq_info[lq_data->active_tbl]);
1378 struct iwl4965_scale_tbl_info *search_tbl =
1379 &(lq_data->lq_info[(1 - lq_data->active_tbl)]);
1380 u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
1381 (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
1382 u8 start_action = tbl->action;
1385 lq_data->action_counter++;
1386 switch (tbl->action) {
1387 case IWL_MIMO_SWITCH_ANTENNA_A:
1388 case IWL_MIMO_SWITCH_ANTENNA_B:
1389 IWL_DEBUG_HT("LQ: MIMO SWITCH TO SISO\n");
1391 /* Set up new search table for SISO */
1392 memcpy(search_tbl, tbl, sz);
1393 search_tbl->lq_type = LQ_SISO;
1394 search_tbl->is_SGI = 0;
1395 search_tbl->is_fat = 0;
1396 if (tbl->action == IWL_MIMO_SWITCH_ANTENNA_A)
1397 search_tbl->antenna_type = ANT_MAIN;
1399 search_tbl->antenna_type = ANT_AUX;
1401 ret = rs_switch_to_siso(priv, lq_data, conf, sta,
1404 lq_data->search_better_tbl = 1;
1409 case IWL_MIMO_SWITCH_GI:
1410 IWL_DEBUG_HT("LQ: MIMO SWITCH TO GI\n");
1412 /* Set up new search table for MIMO */
1413 memcpy(search_tbl, tbl, sz);
1414 search_tbl->lq_type = LQ_MIMO;
1415 search_tbl->antenna_type = ANT_BOTH;
1416 search_tbl->action = 0;
1417 if (search_tbl->is_SGI)
1418 search_tbl->is_SGI = 0;
1420 search_tbl->is_SGI = 1;
1421 lq_data->search_better_tbl = 1;
1424 * If active table already uses the fastest possible
1425 * modulation (dual stream with short guard interval),
1426 * and it's working well, there's no need to look
1427 * for a better type of modulation!
1429 if ((tbl->lq_type == LQ_MIMO) &&
1431 s32 tpt = lq_data->last_tpt / 100;
1432 if (((!tbl->is_fat) &&
1433 (tpt >= expected_tpt_mimo20MHz[index])) ||
1435 (tpt >= expected_tpt_mimo40MHz[index])))
1436 lq_data->search_better_tbl = 0;
1438 rs_get_expected_tpt_table(lq_data, search_tbl);
1439 rs_mcs_from_tbl(&search_tbl->current_rate,
1440 search_tbl, index, is_green);
1445 if (tbl->action > IWL_MIMO_SWITCH_GI)
1446 tbl->action = IWL_MIMO_SWITCH_ANTENNA_A;
1448 if (tbl->action == start_action)
1455 if (tbl->action > IWL_MIMO_SWITCH_GI)
1456 tbl->action = IWL_MIMO_SWITCH_ANTENNA_A;
1462 * Check whether we should continue using same modulation mode, or
1463 * begin search for a new mode, based on:
1464 * 1) # tx successes or failures while using this mode
1465 * 2) # times calling this function
1466 * 3) elapsed time in this mode (not used, for now)
1468 static void rs_stay_in_table(struct iwl4965_rate_scale_priv *lq_data)
1470 struct iwl4965_scale_tbl_info *tbl;
1473 int flush_interval_passed = 0;
1475 active_tbl = lq_data->active_tbl;
1477 tbl = &(lq_data->lq_info[active_tbl]);
1479 /* If we've been disallowing search, see if we should now allow it */
1480 if (lq_data->stay_in_tbl) {
1482 /* Elapsed time using current modulation mode */
1483 if (lq_data->flush_timer)
1484 flush_interval_passed =
1486 (unsigned long)(lq_data->flush_timer +
1487 IWL_RATE_SCALE_FLUSH_INTVL));
1489 /* For now, disable the elapsed time criterion */
1490 flush_interval_passed = 0;
1493 * Check if we should allow search for new modulation mode.
1494 * If many frames have failed or succeeded, or we've used
1495 * this same modulation for a long time, allow search, and
1496 * reset history stats that keep track of whether we should
1497 * allow a new search. Also (below) reset all bitmaps and
1498 * stats in active history.
1500 if ((lq_data->total_failed > lq_data->max_failure_limit) ||
1501 (lq_data->total_success > lq_data->max_success_limit) ||
1502 ((!lq_data->search_better_tbl) && (lq_data->flush_timer)
1503 && (flush_interval_passed))) {
1504 IWL_DEBUG_HT("LQ: stay is expired %d %d %d\n:",
1505 lq_data->total_failed,
1506 lq_data->total_success,
1507 flush_interval_passed);
1509 /* Allow search for new mode */
1510 lq_data->stay_in_tbl = 0; /* only place reset */
1511 lq_data->total_failed = 0;
1512 lq_data->total_success = 0;
1513 lq_data->flush_timer = 0;
1516 * Else if we've used this modulation mode enough repetitions
1517 * (regardless of elapsed time or success/failure), reset
1518 * history bitmaps and rate-specific stats for all rates in
1522 lq_data->table_count++;
1523 if (lq_data->table_count >=
1524 lq_data->table_count_limit) {
1525 lq_data->table_count = 0;
1527 IWL_DEBUG_HT("LQ: stay in table clear win\n");
1528 for (i = 0; i < IWL_RATE_COUNT; i++)
1529 rs_rate_scale_clear_window(
1534 /* If transitioning to allow "search", reset all history
1535 * bitmaps and stats in active table (this will become the new
1536 * "search" table). */
1537 if (!lq_data->stay_in_tbl) {
1538 for (i = 0; i < IWL_RATE_COUNT; i++)
1539 rs_rate_scale_clear_window(&(tbl->win[i]));
1545 * Do rate scaling and search for new modulation mode.
1547 static void rs_rate_scale_perform(struct iwl4965_priv *priv,
1548 struct net_device *dev,
1549 struct ieee80211_hdr *hdr,
1550 struct sta_info *sta)
1552 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1553 struct ieee80211_hw *hw = local_to_hw(local);
1554 struct ieee80211_conf *conf = &hw->conf;
1555 int low = IWL_RATE_INVALID;
1556 int high = IWL_RATE_INVALID;
1559 struct iwl4965_rate_scale_data *window = NULL;
1560 int current_tpt = IWL_INVALID_VALUE;
1561 int low_tpt = IWL_INVALID_VALUE;
1562 int high_tpt = IWL_INVALID_VALUE;
1564 s8 scale_action = 0;
1567 struct iwl4965_rate_scale_priv *lq_data;
1568 struct iwl4965_scale_tbl_info *tbl, *tbl1;
1569 u16 rate_scale_index_msk = 0;
1570 struct iwl4965_rate mcs_rate;
1576 IWL_DEBUG_RATE("rate scale calculate new rate for skb\n");
1578 fc = le16_to_cpu(hdr->frame_control);
1579 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1)) {
1580 /* Send management frames and broadcast/multicast data using
1582 /* TODO: this could probably be improved.. */
1586 if (!sta || !sta->rate_ctrl_priv)
1589 if (!priv->lq_mngr.lq_ready) {
1590 IWL_DEBUG_RATE("still rate scaling not ready\n");
1593 lq_data = (struct iwl4965_rate_scale_priv *)sta->rate_ctrl_priv;
1596 * Select rate-scale / modulation-mode table to work with in
1597 * the rest of this function: "search" if searching for better
1598 * modulation mode, or "active" if doing rate scaling within a mode.
1600 if (!lq_data->search_better_tbl)
1601 active_tbl = lq_data->active_tbl;
1603 active_tbl = 1 - lq_data->active_tbl;
1605 tbl = &(lq_data->lq_info[active_tbl]);
1606 is_green = lq_data->is_green;
1608 /* current tx rate */
1609 index = sta->last_txrate;
1611 IWL_DEBUG_RATE("Rate scale index %d for type %d\n", index,
1614 /* rates available for this association, and for modulation mode */
1615 rs_get_supported_rates(lq_data, hdr, tbl->lq_type,
1618 IWL_DEBUG_RATE("mask 0x%04X \n", rate_mask);
1620 /* mask with station rate restriction */
1621 if (is_legacy(tbl->lq_type)) {
1622 if (lq_data->phymode == (u8) MODE_IEEE80211A)
1623 /* supp_rates has no CCK bits in A mode */
1624 rate_scale_index_msk = (u16) (rate_mask &
1625 (lq_data->supp_rates << IWL_FIRST_OFDM_RATE));
1627 rate_scale_index_msk = (u16) (rate_mask &
1628 lq_data->supp_rates);
1631 rate_scale_index_msk = rate_mask;
1633 if (!rate_scale_index_msk)
1634 rate_scale_index_msk = rate_mask;
1636 /* If current rate is no longer supported on current association,
1637 * or user changed preferences for rates, find a new supported rate. */
1638 if (index < 0 || !((1 << index) & rate_scale_index_msk)) {
1639 index = IWL_INVALID_VALUE;
1642 /* get the highest available rate */
1643 for (i = 0; i <= IWL_RATE_COUNT; i++) {
1644 if ((1 << i) & rate_scale_index_msk)
1648 if (index == IWL_INVALID_VALUE) {
1649 IWL_WARNING("Can not find a suitable rate\n");
1654 /* Get expected throughput table and history window for current rate */
1655 if (!tbl->expected_tpt)
1656 rs_get_expected_tpt_table(lq_data, tbl);
1658 window = &(tbl->win[index]);
1661 * If there is not enough history to calculate actual average
1662 * throughput, keep analyzing results of more tx frames, without
1663 * changing rate or mode (bypass most of the rest of this function).
1664 * Set up new rate table in uCode only if old rate is not supported
1665 * in current association (use new rate found above).
1667 fail_count = window->counter - window->success_counter;
1668 if (((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
1669 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH))
1670 || (tbl->expected_tpt == NULL)) {
1671 IWL_DEBUG_RATE("LQ: still below TH succ %d total %d "
1673 window->success_counter, window->counter, index);
1675 /* Can't calculate this yet; not enough history */
1676 window->average_tpt = IWL_INVALID_VALUE;
1678 /* Should we stay with this modulation mode,
1679 * or search for a new one? */
1680 rs_stay_in_table(lq_data);
1682 /* Set up new rate table in uCode, if needed */
1684 rs_mcs_from_tbl(&mcs_rate, tbl, index, is_green);
1685 rs_fill_link_cmd(lq_data, &mcs_rate, &lq_data->lq);
1686 rs_send_lq_cmd(priv, &lq_data->lq, CMD_ASYNC);
1690 /* Else we have enough samples; calculate estimate of
1691 * actual average throughput */
1693 window->average_tpt = ((window->success_ratio *
1694 tbl->expected_tpt[index] + 64) / 128);
1696 /* If we are searching for better modulation mode, check success. */
1697 if (lq_data->search_better_tbl) {
1698 int success_limit = IWL_RATE_SCALE_SWITCH;
1700 /* If good success, continue using the "search" mode;
1701 * no need to send new link quality command, since we're
1702 * continuing to use the setup that we've been trying. */
1703 if ((window->success_ratio > success_limit) ||
1704 (window->average_tpt > lq_data->last_tpt)) {
1705 if (!is_legacy(tbl->lq_type)) {
1706 IWL_DEBUG_HT("LQ: we are switching to HT"
1707 " rate suc %d current tpt %d"
1709 window->success_ratio,
1710 window->average_tpt,
1712 lq_data->enable_counter = 1;
1714 /* Swap tables; "search" becomes "active" */
1715 lq_data->active_tbl = active_tbl;
1716 current_tpt = window->average_tpt;
1718 /* Else poor success; go back to mode in "active" table */
1720 /* Nullify "search" table */
1721 tbl->lq_type = LQ_NONE;
1723 /* Revert to "active" table */
1724 active_tbl = lq_data->active_tbl;
1725 tbl = &(lq_data->lq_info[active_tbl]);
1727 /* Revert to "active" rate and throughput info */
1728 index = iwl4965_rate_index_from_plcp(
1729 tbl->current_rate.rate_n_flags);
1730 current_tpt = lq_data->last_tpt;
1732 /* Need to set up a new rate table in uCode */
1734 IWL_DEBUG_HT("XXY GO BACK TO OLD TABLE\n");
1737 /* Either way, we've made a decision; modulation mode
1738 * search is done, allow rate adjustment next time. */
1739 lq_data->search_better_tbl = 0;
1740 done_search = 1; /* Don't switch modes below! */
1744 /* (Else) not in search of better modulation mode, try for better
1745 * starting rate, while staying in this mode. */
1746 high_low = rs_get_adjacent_rate(index, rate_scale_index_msk,
1748 low = high_low & 0xff;
1749 high = (high_low >> 8) & 0xff;
1751 /* Collect measured throughputs for current and adjacent rates */
1752 current_tpt = window->average_tpt;
1753 if (low != IWL_RATE_INVALID)
1754 low_tpt = tbl->win[low].average_tpt;
1755 if (high != IWL_RATE_INVALID)
1756 high_tpt = tbl->win[high].average_tpt;
1758 /* Assume rate increase */
1761 /* Too many failures, decrease rate */
1762 if ((window->success_ratio <= IWL_RATE_DECREASE_TH) ||
1763 (current_tpt == 0)) {
1764 IWL_DEBUG_RATE("decrease rate because of low success_ratio\n");
1767 /* No throughput measured yet for adjacent rates; try increase. */
1768 } else if ((low_tpt == IWL_INVALID_VALUE) &&
1769 (high_tpt == IWL_INVALID_VALUE))
1772 /* Both adjacent throughputs are measured, but neither one has better
1773 * throughput; we're using the best rate, don't change it! */
1774 else if ((low_tpt != IWL_INVALID_VALUE) &&
1775 (high_tpt != IWL_INVALID_VALUE) &&
1776 (low_tpt < current_tpt) &&
1777 (high_tpt < current_tpt))
1780 /* At least one adjacent rate's throughput is measured,
1781 * and may have better performance. */
1783 /* Higher adjacent rate's throughput is measured */
1784 if (high_tpt != IWL_INVALID_VALUE) {
1785 /* Higher rate has better throughput */
1786 if (high_tpt > current_tpt)
1790 ("decrease rate because of high tpt\n");
1794 /* Lower adjacent rate's throughput is measured */
1795 } else if (low_tpt != IWL_INVALID_VALUE) {
1796 /* Lower rate has better throughput */
1797 if (low_tpt > current_tpt) {
1799 ("decrease rate because of low tpt\n");
1806 /* Sanity check; asked for decrease, but success rate or throughput
1807 * has been good at old rate. Don't change it. */
1808 if (scale_action == -1) {
1809 if ((low != IWL_RATE_INVALID) &&
1810 ((window->success_ratio > IWL_RATE_HIGH_TH) ||
1811 (current_tpt > (100 * tbl->expected_tpt[low]))))
1814 /* Sanity check; asked for increase, but success rate has not been great
1815 * even at old rate, higher rate will be worse. Don't change it. */
1816 } else if ((scale_action == 1) &&
1817 (window->success_ratio < IWL_RATE_INCREASE_TH))
1820 switch (scale_action) {
1822 /* Decrease starting rate, update uCode's rate table */
1823 if (low != IWL_RATE_INVALID) {
1829 /* Increase starting rate, update uCode's rate table */
1830 if (high != IWL_RATE_INVALID) {
1842 IWL_DEBUG_HT("choose rate scale index %d action %d low %d "
1843 "high %d type %d\n",
1844 index, scale_action, low, high, tbl->lq_type);
1847 /* Replace uCode's rate table for the destination station. */
1849 rs_mcs_from_tbl(&mcs_rate, tbl, index, is_green);
1850 rs_fill_link_cmd(lq_data, &mcs_rate, &lq_data->lq);
1851 rs_send_lq_cmd(priv, &lq_data->lq, CMD_ASYNC);
1854 /* Should we stay with this modulation mode, or search for a new one? */
1855 rs_stay_in_table(lq_data);
1858 * Search for new modulation mode if we're:
1859 * 1) Not changing rates right now
1860 * 2) Not just finishing up a search
1861 * 3) Allowing a new search
1863 if (!update_lq && !done_search && !lq_data->stay_in_tbl) {
1864 /* Save current throughput to compare with "search" throughput*/
1865 lq_data->last_tpt = current_tpt;
1867 /* Select a new "search" modulation mode to try.
1868 * If one is found, set up the new "search" table. */
1869 if (is_legacy(tbl->lq_type))
1870 rs_move_legacy_other(priv, lq_data, conf, sta, index);
1871 else if (is_siso(tbl->lq_type))
1872 rs_move_siso_to_other(priv, lq_data, conf, sta, index);
1874 rs_move_mimo_to_other(priv, lq_data, conf, sta, index);
1876 /* If new "search" mode was selected, set up in uCode table */
1877 if (lq_data->search_better_tbl) {
1878 /* Access the "search" table, clear its history. */
1879 tbl = &(lq_data->lq_info[(1 - lq_data->active_tbl)]);
1880 for (i = 0; i < IWL_RATE_COUNT; i++)
1881 rs_rate_scale_clear_window(&(tbl->win[i]));
1883 /* Use new "search" start rate */
1884 index = iwl4965_rate_index_from_plcp(
1885 tbl->current_rate.rate_n_flags);
1887 IWL_DEBUG_HT("Switch current mcs: %X index: %d\n",
1888 tbl->current_rate.rate_n_flags, index);
1889 rs_fill_link_cmd(lq_data, &tbl->current_rate,
1891 rs_send_lq_cmd(priv, &lq_data->lq, CMD_ASYNC);
1894 /* If the "active" (non-search) mode was legacy,
1895 * and we've tried switching antennas,
1896 * but we haven't been able to try HT modes (not available),
1897 * stay with best antenna legacy modulation for a while
1898 * before next round of mode comparisons. */
1899 tbl1 = &(lq_data->lq_info[lq_data->active_tbl]);
1900 if (is_legacy(tbl1->lq_type) &&
1901 #ifdef CONFIG_IWL4965_HT
1902 (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE)) &&
1904 (lq_data->action_counter >= 1)) {
1905 lq_data->action_counter = 0;
1906 IWL_DEBUG_HT("LQ: STAY in legacy table\n");
1907 rs_set_stay_in_table(1, lq_data);
1910 /* If we're in an HT mode, and all 3 mode switch actions
1911 * have been tried and compared, stay in this best modulation
1912 * mode for a while before next round of mode comparisons. */
1913 if (lq_data->enable_counter &&
1914 (lq_data->action_counter >= IWL_ACTION_LIMIT)) {
1915 #ifdef CONFIG_IWL4965_HT_AGG
1916 /* If appropriate, set up aggregation! */
1917 if ((lq_data->last_tpt > TID_AGG_TPT_THREHOLD) &&
1918 (priv->lq_mngr.agg_ctrl.auto_agg)) {
1919 priv->lq_mngr.agg_ctrl.tid_retry =
1921 schedule_work(&priv->agg_work);
1923 #endif /*CONFIG_IWL4965_HT_AGG */
1924 lq_data->action_counter = 0;
1925 rs_set_stay_in_table(0, lq_data);
1929 * Else, don't search for a new modulation mode.
1930 * Put new timestamp in stay-in-modulation-mode flush timer if:
1931 * 1) Not changing rates right now
1932 * 2) Not just finishing up a search
1933 * 3) flush timer is empty
1936 if ((!update_lq) && (!done_search) && (!lq_data->flush_timer))
1937 lq_data->flush_timer = jiffies;
1941 rs_mcs_from_tbl(&tbl->current_rate, tbl, index, is_green);
1943 sta->last_txrate = i;
1945 /* sta->txrate is an index to A mode rates which start
1946 * at IWL_FIRST_OFDM_RATE
1948 if (lq_data->phymode == (u8) MODE_IEEE80211A)
1949 sta->txrate = i - IWL_FIRST_OFDM_RATE;
1957 static void rs_initialize_lq(struct iwl4965_priv *priv,
1958 struct ieee80211_conf *conf,
1959 struct sta_info *sta)
1962 struct iwl4965_rate_scale_priv *lq;
1963 struct iwl4965_scale_tbl_info *tbl;
1966 u8 use_green = rs_use_green(priv, conf);
1967 struct iwl4965_rate mcs_rate;
1969 if (!sta || !sta->rate_ctrl_priv)
1972 lq = (struct iwl4965_rate_scale_priv *)sta->rate_ctrl_priv;
1973 i = sta->last_txrate;
1975 if ((lq->lq.sta_id == 0xff) &&
1976 (priv->iw_mode == IEEE80211_IF_TYPE_IBSS))
1979 if (!lq->search_better_tbl)
1980 active_tbl = lq->active_tbl;
1982 active_tbl = 1 - lq->active_tbl;
1984 tbl = &(lq->lq_info[active_tbl]);
1986 if ((i < 0) || (i >= IWL_RATE_COUNT))
1989 mcs_rate.rate_n_flags = iwl4965_rates[i].plcp ;
1990 mcs_rate.rate_n_flags |= RATE_MCS_ANT_B_MSK;
1991 mcs_rate.rate_n_flags &= ~RATE_MCS_ANT_A_MSK;
1993 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
1994 mcs_rate.rate_n_flags |= RATE_MCS_CCK_MSK;
1996 tbl->antenna_type = ANT_AUX;
1997 rs_get_tbl_info_from_mcs(&mcs_rate, priv->phymode, tbl, &rate_idx);
1998 if (!rs_is_ant_connected(priv->valid_antenna, tbl->antenna_type))
1999 rs_toggle_antenna(&mcs_rate, tbl);
2001 rs_mcs_from_tbl(&mcs_rate, tbl, rate_idx, use_green);
2002 tbl->current_rate.rate_n_flags = mcs_rate.rate_n_flags;
2003 rs_get_expected_tpt_table(lq, tbl);
2004 rs_fill_link_cmd(lq, &mcs_rate, &lq->lq);
2005 rs_send_lq_cmd(priv, &lq->lq, CMD_ASYNC);
2010 static void rs_get_rate(void *priv_rate, struct net_device *dev,
2011 struct ieee80211_hw_mode *mode, struct sk_buff *skb,
2012 struct rate_selection *sel)
2016 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2017 struct ieee80211_conf *conf = &local->hw.conf;
2018 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2019 struct sta_info *sta;
2021 struct iwl4965_priv *priv = (struct iwl4965_priv *)priv_rate;
2022 struct iwl4965_rate_scale_priv *lq;
2024 IWL_DEBUG_RATE_LIMIT("rate scale calculate new rate for skb\n");
2026 sta = sta_info_get(local, hdr->addr1);
2028 /* Send management frames and broadcast/multicast data using lowest
2030 fc = le16_to_cpu(hdr->frame_control);
2031 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1) ||
2032 !sta || !sta->rate_ctrl_priv) {
2033 sel->rate = rate_lowest(local, local->oper_hw_mode, sta);
2039 lq = (struct iwl4965_rate_scale_priv *)sta->rate_ctrl_priv;
2040 i = sta->last_txrate;
2042 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) && !lq->ibss_sta_added) {
2043 u8 sta_id = iwl4965_hw_find_station(priv, hdr->addr1);
2044 DECLARE_MAC_BUF(mac);
2046 if (sta_id == IWL_INVALID_STATION) {
2047 IWL_DEBUG_RATE("LQ: ADD station %s\n",
2048 print_mac(mac, hdr->addr1));
2049 sta_id = iwl4965_add_station_flags(priv, hdr->addr1,
2050 0, CMD_ASYNC, NULL);
2052 if ((sta_id != IWL_INVALID_STATION)) {
2053 lq->lq.sta_id = sta_id;
2054 lq->lq.rs_table[0].rate_n_flags = 0;
2055 lq->ibss_sta_added = 1;
2056 rs_initialize_lq(priv, conf, sta);
2058 if (!lq->ibss_sta_added)
2063 if ((i < 0) || (i > IWL_RATE_COUNT)) {
2064 sel->rate = rate_lowest(local, local->oper_hw_mode, sta);
2069 sel->rate = &priv->ieee_rates[i];
2072 static void *rs_alloc_sta(void *priv, gfp_t gfp)
2074 struct iwl4965_rate_scale_priv *crl;
2077 IWL_DEBUG_RATE("create station rate scale window\n");
2079 crl = kzalloc(sizeof(struct iwl4965_rate_scale_priv), gfp);
2083 crl->lq.sta_id = 0xff;
2086 for (j = 0; j < LQ_SIZE; j++)
2087 for (i = 0; i < IWL_RATE_COUNT; i++)
2088 rs_rate_scale_clear_window(&(crl->lq_info[j].win[i]));
2093 static void rs_rate_init(void *priv_rate, void *priv_sta,
2094 struct ieee80211_local *local,
2095 struct sta_info *sta)
2098 struct ieee80211_conf *conf = &local->hw.conf;
2099 struct ieee80211_hw_mode *mode = local->oper_hw_mode;
2100 struct iwl4965_priv *priv = (struct iwl4965_priv *)priv_rate;
2101 struct iwl4965_rate_scale_priv *crl = priv_sta;
2103 crl->flush_timer = 0;
2104 crl->supp_rates = sta->supp_rates;
2106 for (j = 0; j < LQ_SIZE; j++)
2107 for (i = 0; i < IWL_RATE_COUNT; i++)
2108 rs_rate_scale_clear_window(&(crl->lq_info[j].win[i]));
2110 IWL_DEBUG_RATE("rate scale global init\n");
2111 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2112 * the lowest or the highest rate.. Could consider using RSSI from
2113 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2116 crl->ibss_sta_added = 0;
2117 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
2118 u8 sta_id = iwl4965_hw_find_station(priv, sta->addr);
2119 DECLARE_MAC_BUF(mac);
2121 /* for IBSS the call are from tasklet */
2122 IWL_DEBUG_HT("LQ: ADD station %s\n",
2123 print_mac(mac, sta->addr));
2125 if (sta_id == IWL_INVALID_STATION) {
2126 IWL_DEBUG_RATE("LQ: ADD station %s\n",
2127 print_mac(mac, sta->addr));
2128 sta_id = iwl4965_add_station_flags(priv, sta->addr,
2129 0, CMD_ASYNC, NULL);
2131 if ((sta_id != IWL_INVALID_STATION)) {
2132 crl->lq.sta_id = sta_id;
2133 crl->lq.rs_table[0].rate_n_flags = 0;
2135 /* FIXME: this is w/a remove it later */
2136 priv->assoc_station_added = 1;
2139 /* Find highest tx rate supported by hardware and destination station */
2140 for (i = 0; i < mode->num_rates; i++) {
2141 if ((sta->supp_rates & BIT(i)) &&
2142 (mode->rates[i].flags & IEEE80211_RATE_SUPPORTED))
2145 sta->last_txrate = sta->txrate;
2146 /* For MODE_IEEE80211A, cck rates are at end of rate table */
2147 if (local->hw.conf.phymode == MODE_IEEE80211A)
2148 sta->last_txrate += IWL_FIRST_OFDM_RATE;
2151 crl->valid_antenna = priv->valid_antenna;
2152 crl->antenna = priv->antenna;
2153 crl->is_green = rs_use_green(priv, conf);
2154 crl->active_rate = priv->active_rate;
2155 crl->active_rate &= ~(0x1000);
2156 crl->active_rate_basic = priv->active_rate_basic;
2157 crl->phymode = priv->phymode;
2158 #ifdef CONFIG_IWL4965_HT
2160 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2161 * supp_rates[] does not; shift to convert format, force 9 MBits off.
2163 crl->active_siso_rate = (priv->current_ht_config.supp_mcs_set[0] << 1);
2164 crl->active_siso_rate |=
2165 (priv->current_ht_config.supp_mcs_set[0] & 0x1);
2166 crl->active_siso_rate &= ~((u16)0x2);
2167 crl->active_siso_rate = crl->active_siso_rate << IWL_FIRST_OFDM_RATE;
2170 crl->active_mimo_rate = (priv->current_ht_config.supp_mcs_set[1] << 1);
2171 crl->active_mimo_rate |=
2172 (priv->current_ht_config.supp_mcs_set[1] & 0x1);
2173 crl->active_mimo_rate &= ~((u16)0x2);
2174 crl->active_mimo_rate = crl->active_mimo_rate << IWL_FIRST_OFDM_RATE;
2175 IWL_DEBUG_HT("MIMO RATE 0x%X SISO MASK 0x%X\n", crl->active_siso_rate,
2176 crl->active_mimo_rate);
2177 #endif /*CONFIG_IWL4965_HT*/
2178 #ifdef CONFIG_MAC80211_DEBUGFS
2182 if (priv->assoc_station_added)
2183 priv->lq_mngr.lq_ready = 1;
2185 rs_initialize_lq(priv, conf, sta);
2188 static void rs_fill_link_cmd(struct iwl4965_rate_scale_priv *lq_data,
2189 struct iwl4965_rate *tx_mcs,
2190 struct iwl4965_link_quality_cmd *lq_cmd)
2194 int repeat_rate = 0;
2195 u8 ant_toggle_count = 0;
2196 u8 use_ht_possible = 1;
2197 struct iwl4965_rate new_rate;
2198 struct iwl4965_scale_tbl_info tbl_type = { 0 };
2200 /* Override starting rate (index 0) if needed for debug purposes */
2201 rs_dbgfs_set_mcs(lq_data, tx_mcs, index);
2203 /* Interpret rate_n_flags */
2204 rs_get_tbl_info_from_mcs(tx_mcs, lq_data->phymode,
2205 &tbl_type, &rate_idx);
2207 /* How many times should we repeat the initial rate? */
2208 if (is_legacy(tbl_type.lq_type)) {
2209 ant_toggle_count = 1;
2210 repeat_rate = IWL_NUMBER_TRY;
2212 repeat_rate = IWL_HT_NUMBER_TRY;
2214 lq_cmd->general_params.mimo_delimiter =
2215 is_mimo(tbl_type.lq_type) ? 1 : 0;
2217 /* Fill 1st table entry (index 0) */
2218 lq_cmd->rs_table[index].rate_n_flags =
2219 cpu_to_le32(tx_mcs->rate_n_flags);
2220 new_rate.rate_n_flags = tx_mcs->rate_n_flags;
2222 if (is_mimo(tbl_type.lq_type) || (tbl_type.antenna_type == ANT_MAIN))
2223 lq_cmd->general_params.single_stream_ant_msk
2224 = LINK_QUAL_ANT_A_MSK;
2226 lq_cmd->general_params.single_stream_ant_msk
2227 = LINK_QUAL_ANT_B_MSK;
2232 /* Fill rest of rate table */
2233 while (index < LINK_QUAL_MAX_RETRY_NUM) {
2234 /* Repeat initial/next rate.
2235 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2236 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
2237 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
2238 if (is_legacy(tbl_type.lq_type)) {
2239 if (ant_toggle_count <
2240 NUM_TRY_BEFORE_ANTENNA_TOGGLE)
2243 rs_toggle_antenna(&new_rate, &tbl_type);
2244 ant_toggle_count = 1;
2248 /* Override next rate if needed for debug purposes */
2249 rs_dbgfs_set_mcs(lq_data, &new_rate, index);
2251 /* Fill next table entry */
2252 lq_cmd->rs_table[index].rate_n_flags =
2253 cpu_to_le32(new_rate.rate_n_flags);
2258 rs_get_tbl_info_from_mcs(&new_rate, lq_data->phymode, &tbl_type,
2261 /* Indicate to uCode which entries might be MIMO.
2262 * If initial rate was MIMO, this will finally end up
2263 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
2264 if (is_mimo(tbl_type.lq_type))
2265 lq_cmd->general_params.mimo_delimiter = index;
2268 rs_get_lower_rate(lq_data, &tbl_type, rate_idx,
2269 use_ht_possible, &new_rate);
2271 /* How many times should we repeat the next rate? */
2272 if (is_legacy(tbl_type.lq_type)) {
2273 if (ant_toggle_count < NUM_TRY_BEFORE_ANTENNA_TOGGLE)
2276 rs_toggle_antenna(&new_rate, &tbl_type);
2277 ant_toggle_count = 1;
2279 repeat_rate = IWL_NUMBER_TRY;
2281 repeat_rate = IWL_HT_NUMBER_TRY;
2283 /* Don't allow HT rates after next pass.
2284 * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
2285 use_ht_possible = 0;
2287 /* Override next rate if needed for debug purposes */
2288 rs_dbgfs_set_mcs(lq_data, &new_rate, index);
2290 /* Fill next table entry */
2291 lq_cmd->rs_table[index].rate_n_flags =
2292 cpu_to_le32(new_rate.rate_n_flags);
2298 lq_cmd->general_params.dual_stream_ant_msk = 3;
2299 lq_cmd->agg_params.agg_dis_start_th = 3;
2300 lq_cmd->agg_params.agg_time_limit = cpu_to_le16(4000);
2303 static void *rs_alloc(struct ieee80211_local *local)
2305 return local->hw.priv;
2307 /* rate scale requires free function to be implemented */
2308 static void rs_free(void *priv_rate)
2313 static void rs_clear(void *priv_rate)
2315 struct iwl4965_priv *priv = (struct iwl4965_priv *) priv_rate;
2317 IWL_DEBUG_RATE("enter\n");
2319 priv->lq_mngr.lq_ready = 0;
2320 #ifdef CONFIG_IWL4965_HT
2321 #ifdef CONFIG_IWL4965_HT_AGG
2322 if (priv->lq_mngr.agg_ctrl.granted_ba)
2323 iwl4965_turn_off_agg(priv, TID_ALL_SPECIFIED);
2324 #endif /*CONFIG_IWL4965_HT_AGG */
2325 #endif /* CONFIG_IWL4965_HT */
2327 IWL_DEBUG_RATE("leave\n");
2330 static void rs_free_sta(void *priv, void *priv_sta)
2332 struct iwl4965_rate_scale_priv *rs_priv = priv_sta;
2334 IWL_DEBUG_RATE("enter\n");
2336 IWL_DEBUG_RATE("leave\n");
2340 #ifdef CONFIG_MAC80211_DEBUGFS
2341 static int open_file_generic(struct inode *inode, struct file *file)
2343 file->private_data = inode->i_private;
2346 static void rs_dbgfs_set_mcs(struct iwl4965_rate_scale_priv *rs_priv,
2347 struct iwl4965_rate *mcs, int index)
2351 if (rs_priv->phymode == (u8) MODE_IEEE80211A)
2356 if (rs_priv->dbg_fixed.rate_n_flags) {
2358 mcs->rate_n_flags = rs_priv->dbg_fixed.rate_n_flags;
2360 mcs->rate_n_flags = base_rate;
2361 IWL_DEBUG_RATE("Fixed rate ON\n");
2365 IWL_DEBUG_RATE("Fixed rate OFF\n");
2368 static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
2369 const char __user *user_buf, size_t count, loff_t *ppos)
2371 struct iwl4965_rate_scale_priv *rs_priv = file->private_data;
2376 memset(buf, 0, sizeof(buf));
2377 buf_size = min(count, sizeof(buf) - 1);
2378 if (copy_from_user(buf, user_buf, buf_size))
2381 if (sscanf(buf, "%x", &parsed_rate) == 1)
2382 rs_priv->dbg_fixed.rate_n_flags = parsed_rate;
2384 rs_priv->dbg_fixed.rate_n_flags = 0;
2386 rs_priv->active_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
2387 rs_priv->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2388 rs_priv->active_mimo_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2390 IWL_DEBUG_RATE("sta_id %d rate 0x%X\n",
2391 rs_priv->lq.sta_id, rs_priv->dbg_fixed.rate_n_flags);
2393 if (rs_priv->dbg_fixed.rate_n_flags) {
2394 rs_fill_link_cmd(rs_priv, &rs_priv->dbg_fixed, &rs_priv->lq);
2395 rs_send_lq_cmd(rs_priv->drv, &rs_priv->lq, CMD_ASYNC);
2401 static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
2402 char __user *user_buf, size_t count, loff_t *ppos)
2408 struct iwl4965_rate_scale_priv *rs_priv = file->private_data;
2410 desc += sprintf(buff+desc, "sta_id %d\n", rs_priv->lq.sta_id);
2411 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
2412 rs_priv->total_failed, rs_priv->total_success,
2413 rs_priv->active_rate);
2414 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
2415 rs_priv->dbg_fixed.rate_n_flags);
2416 desc += sprintf(buff+desc, "general:"
2417 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
2418 rs_priv->lq.general_params.flags,
2419 rs_priv->lq.general_params.mimo_delimiter,
2420 rs_priv->lq.general_params.single_stream_ant_msk,
2421 rs_priv->lq.general_params.dual_stream_ant_msk);
2423 desc += sprintf(buff+desc, "agg:"
2424 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
2425 le16_to_cpu(rs_priv->lq.agg_params.agg_time_limit),
2426 rs_priv->lq.agg_params.agg_dis_start_th,
2427 rs_priv->lq.agg_params.agg_frame_cnt_limit);
2429 desc += sprintf(buff+desc,
2430 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
2431 rs_priv->lq.general_params.start_rate_index[0],
2432 rs_priv->lq.general_params.start_rate_index[1],
2433 rs_priv->lq.general_params.start_rate_index[2],
2434 rs_priv->lq.general_params.start_rate_index[3]);
2437 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
2438 desc += sprintf(buff+desc, " rate[%d] 0x%X\n",
2439 i, le32_to_cpu(rs_priv->lq.rs_table[i].rate_n_flags));
2441 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2444 static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
2445 .write = rs_sta_dbgfs_scale_table_write,
2446 .read = rs_sta_dbgfs_scale_table_read,
2447 .open = open_file_generic,
2449 static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
2450 char __user *user_buf, size_t count, loff_t *ppos)
2456 struct iwl4965_rate_scale_priv *rs_priv = file->private_data;
2457 for (i = 0; i < LQ_SIZE; i++) {
2458 desc += sprintf(buff+desc, "%s type=%d SGI=%d FAT=%d DUP=%d\n"
2460 rs_priv->active_tbl == i?"*":"x",
2461 rs_priv->lq_info[i].lq_type,
2462 rs_priv->lq_info[i].is_SGI,
2463 rs_priv->lq_info[i].is_fat,
2464 rs_priv->lq_info[i].is_dup,
2465 rs_priv->lq_info[i].current_rate.rate_n_flags);
2466 for (j = 0; j < IWL_RATE_COUNT; j++) {
2467 desc += sprintf(buff+desc,
2468 "counter=%d success=%d %%=%d\n",
2469 rs_priv->lq_info[i].win[j].counter,
2470 rs_priv->lq_info[i].win[j].success_counter,
2471 rs_priv->lq_info[i].win[j].success_ratio);
2474 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2477 static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
2478 .read = rs_sta_dbgfs_stats_table_read,
2479 .open = open_file_generic,
2482 static void rs_add_debugfs(void *priv, void *priv_sta,
2485 struct iwl4965_rate_scale_priv *rs_priv = priv_sta;
2486 rs_priv->rs_sta_dbgfs_scale_table_file =
2487 debugfs_create_file("rate_scale_table", 0600, dir,
2488 rs_priv, &rs_sta_dbgfs_scale_table_ops);
2489 rs_priv->rs_sta_dbgfs_stats_table_file =
2490 debugfs_create_file("rate_stats_table", 0600, dir,
2491 rs_priv, &rs_sta_dbgfs_stats_table_ops);
2494 static void rs_remove_debugfs(void *priv, void *priv_sta)
2496 struct iwl4965_rate_scale_priv *rs_priv = priv_sta;
2497 debugfs_remove(rs_priv->rs_sta_dbgfs_scale_table_file);
2498 debugfs_remove(rs_priv->rs_sta_dbgfs_stats_table_file);
2502 static struct rate_control_ops rs_ops = {
2505 .tx_status = rs_tx_status,
2506 .get_rate = rs_get_rate,
2507 .rate_init = rs_rate_init,
2511 .alloc_sta = rs_alloc_sta,
2512 .free_sta = rs_free_sta,
2513 #ifdef CONFIG_MAC80211_DEBUGFS
2514 .add_sta_debugfs = rs_add_debugfs,
2515 .remove_sta_debugfs = rs_remove_debugfs,
2519 int iwl4965_fill_rs_info(struct ieee80211_hw *hw, char *buf, u8 sta_id)
2521 struct ieee80211_local *local = hw_to_local(hw);
2522 struct iwl4965_priv *priv = hw->priv;
2523 struct iwl4965_rate_scale_priv *rs_priv;
2524 struct sta_info *sta;
2526 u32 samples = 0, success = 0, good = 0;
2527 unsigned long now = jiffies;
2529 u8 lq_type, antenna;
2531 sta = sta_info_get(local, priv->stations[sta_id].sta.sta.addr);
2532 if (!sta || !sta->rate_ctrl_priv) {
2535 IWL_DEBUG_RATE("leave - no private rate data!\n");
2537 IWL_DEBUG_RATE("leave - no station!\n");
2538 return sprintf(buf, "station %d not found\n", sta_id);
2541 rs_priv = (void *)sta->rate_ctrl_priv;
2543 lq_type = rs_priv->lq_info[rs_priv->active_tbl].lq_type;
2544 antenna = rs_priv->lq_info[rs_priv->active_tbl].antenna_type;
2546 if (is_legacy(lq_type))
2547 i = IWL_RATE_54M_INDEX;
2549 i = IWL_RATE_60M_INDEX;
2553 int active = rs_priv->active_tbl;
2556 sprintf(&buf[count], " %2dMbs: ", iwl4965_rates[i].ieee / 2);
2558 mask = (1ULL << (IWL_RATE_MAX_WINDOW - 1));
2559 for (j = 0; j < IWL_RATE_MAX_WINDOW; j++, mask >>= 1)
2561 (rs_priv->lq_info[active].win[i].data & mask)
2564 samples += rs_priv->lq_info[active].win[i].counter;
2565 good += rs_priv->lq_info[active].win[i].success_counter;
2566 success += rs_priv->lq_info[active].win[i].success_counter *
2567 iwl4965_rates[i].ieee;
2569 if (rs_priv->lq_info[active].win[i].stamp) {
2571 jiffies_to_msecs(now -
2572 rs_priv->lq_info[active].win[i].stamp);
2574 if (delta > max_time)
2577 count += sprintf(&buf[count], "%5dms\n", delta);
2579 buf[count++] = '\n';
2581 j = iwl4965_get_prev_ieee_rate(i);
2587 /* Display the average rate of all samples taken.
2589 * NOTE: We multiply # of samples by 2 since the IEEE measurement
2590 * added from iwl4965_rates is actually 2X the rate */
2592 count += sprintf(&buf[count],
2593 "\nAverage rate is %3d.%02dMbs over last %4dms\n"
2594 "%3d%% success (%d good packets over %d tries)\n",
2595 success / (2 * samples), (success * 5 / samples) % 10,
2596 max_time, good * 100 / samples, good, samples);
2598 count += sprintf(&buf[count], "\nAverage rate: 0Mbs\n");
2599 count += sprintf(&buf[count], "\nrate scale type %d antenna %d "
2600 "active_search %d rate index %d\n", lq_type, antenna,
2601 rs_priv->search_better_tbl, sta->last_txrate);
2607 void iwl4965_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id)
2609 struct iwl4965_priv *priv = hw->priv;
2611 priv->lq_mngr.lq_ready = 1;
2614 void iwl4965_rate_control_register(struct ieee80211_hw *hw)
2616 ieee80211_rate_control_register(&rs_ops);
2619 void iwl4965_rate_control_unregister(struct ieee80211_hw *hw)
2621 ieee80211_rate_control_unregister(&rs_ops);