1 /******************************************************************************
3 * Copyright(c) 2005 - 2009 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 *****************************************************************************/
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>
32 #include <linux/netdevice.h>
33 #include <linux/etherdevice.h>
34 #include <linux/delay.h>
36 #include <linux/workqueue.h>
42 #define RS_NAME "iwl-agn-rs"
44 #define NUM_TRY_BEFORE_ANT_TOGGLE 1
45 #define IWL_NUMBER_TRY 1
46 #define IWL_HT_NUMBER_TRY 3
48 #define IWL_RATE_MAX_WINDOW 62 /* # tx in history window */
49 #define IWL_RATE_MIN_FAILURE_TH 6 /* min failures to calc tpt */
50 #define IWL_RATE_MIN_SUCCESS_TH 8 /* min successes to calc tpt */
52 /* max allowed rate miss before sync LQ cmd */
53 #define IWL_MISSED_RATE_MAX 15
54 /* max time to accum history 2 seconds */
55 #define IWL_RATE_SCALE_FLUSH_INTVL (3*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
67 static const u8 ant_toggle_lookup[] = {
68 /*ANT_NONE -> */ ANT_NONE,
71 /*ANT_AB -> */ ANT_BC,
73 /*ANT_AC -> */ ANT_AB,
74 /*ANT_BC -> */ ANT_AC,
75 /*ANT_ABC -> */ ANT_ABC,
79 * struct iwl_rate_scale_data -- tx success history for one rate
81 struct iwl_rate_scale_data {
82 u64 data; /* bitmap of successful frames */
83 s32 success_counter; /* number of frames successful */
84 s32 success_ratio; /* per-cent * 128 */
85 s32 counter; /* number of frames attempted */
86 s32 average_tpt; /* success ratio * expected throughput */
91 * struct iwl_scale_tbl_info -- tx params and success history for all rates
93 * There are two of these in struct iwl_lq_sta,
94 * one for "active", and one for "search".
96 struct iwl_scale_tbl_info {
97 enum iwl_table_type lq_type;
99 u8 is_SGI; /* 1 = short guard interval */
100 u8 is_ht40; /* 1 = 40 MHz channel width */
101 u8 is_dup; /* 1 = duplicated data streams */
102 u8 action; /* change modulation; IWL_[LEGACY/SISO/MIMO]_SWITCH_* */
103 u8 max_search; /* maximun number of tables we can search */
104 s32 *expected_tpt; /* throughput metrics; expected_tpt_G, etc. */
105 u32 current_rate; /* rate_n_flags, uCode API format */
106 struct iwl_rate_scale_data win[IWL_RATE_COUNT]; /* rate histories */
109 struct iwl_traffic_load {
110 unsigned long time_stamp; /* age of the oldest statistics */
111 u32 packet_count[TID_QUEUE_MAX_SIZE]; /* packet count in this time
113 u32 total; /* total num of packets during the
114 * last TID_MAX_TIME_DIFF */
115 u8 queue_count; /* number of queues that has
116 * been used since the last cleanup */
117 u8 head; /* start of the circular buffer */
121 * struct iwl_lq_sta -- driver's rate scaling private structure
123 * Pointer to this gets passed back and forth between driver and mac80211.
126 u8 active_tbl; /* index of active table, range 0-1 */
127 u8 enable_counter; /* indicates HT mode */
128 u8 stay_in_tbl; /* 1: disallow, 0: allow search for new mode */
129 u8 search_better_tbl; /* 1: currently trying alternate mode */
132 /* The following determine when to search for a new mode */
133 u32 table_count_limit;
134 u32 max_failure_limit; /* # failed frames before new search */
135 u32 max_success_limit; /* # successful frames before new search */
137 u32 total_failed; /* total failed frames, any/all rates */
138 u32 total_success; /* total successful frames, any/all rates */
139 u64 flush_timer; /* time staying in mode before new search */
141 u8 action_counter; /* # mode-switch actions tried */
144 enum ieee80211_band band;
147 /* The following are bitmaps of rates; IWL_RATE_6M_MASK, etc. */
149 u16 active_legacy_rate;
150 u16 active_siso_rate;
151 u16 active_mimo2_rate;
152 u16 active_mimo3_rate;
153 u16 active_rate_basic;
154 s8 max_rate_idx; /* Max rate set by user */
155 u8 missed_rate_counter;
157 struct iwl_link_quality_cmd lq;
158 struct iwl_scale_tbl_info lq_info[LQ_SIZE]; /* "active", "search" */
159 struct iwl_traffic_load load[TID_MAX_LOAD_COUNT];
161 #ifdef CONFIG_MAC80211_DEBUGFS
162 struct dentry *rs_sta_dbgfs_scale_table_file;
163 struct dentry *rs_sta_dbgfs_stats_table_file;
164 struct dentry *rs_sta_dbgfs_rate_scale_data_file;
165 struct dentry *rs_sta_dbgfs_tx_agg_tid_en_file;
168 struct iwl_priv *drv;
170 /* used to be in sta_info */
172 /* last tx rate_n_flags */
173 u32 last_rate_n_flags;
176 static void rs_rate_scale_perform(struct iwl_priv *priv,
178 struct ieee80211_sta *sta,
179 struct iwl_lq_sta *lq_sta);
180 static void rs_fill_link_cmd(struct iwl_priv *priv,
181 struct iwl_lq_sta *lq_sta, u32 rate_n_flags);
184 #ifdef CONFIG_MAC80211_DEBUGFS
185 static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
186 u32 *rate_n_flags, int index);
188 static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
189 u32 *rate_n_flags, int index)
194 * Expected throughput metrics for following rates:
195 * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
196 * "G" is the only table that supports CCK (the first 4 rates).
199 static s32 expected_tpt_A[IWL_RATE_COUNT] = {
200 0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186, 186
203 static s32 expected_tpt_G[IWL_RATE_COUNT] = {
204 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 186
207 static s32 expected_tpt_siso20MHz[IWL_RATE_COUNT] = {
208 0, 0, 0, 0, 42, 42, 76, 102, 124, 159, 183, 193, 202
211 static s32 expected_tpt_siso20MHzSGI[IWL_RATE_COUNT] = {
212 0, 0, 0, 0, 46, 46, 82, 110, 132, 168, 192, 202, 211
215 static s32 expected_tpt_mimo2_20MHz[IWL_RATE_COUNT] = {
216 0, 0, 0, 0, 74, 74, 123, 155, 179, 214, 236, 244, 251
219 static s32 expected_tpt_mimo2_20MHzSGI[IWL_RATE_COUNT] = {
220 0, 0, 0, 0, 81, 81, 131, 164, 188, 222, 243, 251, 257
223 static s32 expected_tpt_siso40MHz[IWL_RATE_COUNT] = {
224 0, 0, 0, 0, 77, 77, 127, 160, 184, 220, 242, 250, 257
227 static s32 expected_tpt_siso40MHzSGI[IWL_RATE_COUNT] = {
228 0, 0, 0, 0, 83, 83, 135, 169, 193, 229, 250, 257, 264
231 static s32 expected_tpt_mimo2_40MHz[IWL_RATE_COUNT] = {
232 0, 0, 0, 0, 123, 123, 182, 214, 235, 264, 279, 285, 289
235 static s32 expected_tpt_mimo2_40MHzSGI[IWL_RATE_COUNT] = {
236 0, 0, 0, 0, 131, 131, 191, 222, 242, 270, 284, 289, 293
239 /* Expected throughput metric MIMO3 */
240 static s32 expected_tpt_mimo3_20MHz[IWL_RATE_COUNT] = {
241 0, 0, 0, 0, 99, 99, 153, 186, 208, 239, 256, 263, 268
244 static s32 expected_tpt_mimo3_20MHzSGI[IWL_RATE_COUNT] = {
245 0, 0, 0, 0, 106, 106, 162, 194, 215, 246, 262, 268, 273
248 static s32 expected_tpt_mimo3_40MHz[IWL_RATE_COUNT] = {
249 0, 0, 0, 0, 152, 152, 211, 239, 255, 279, 290, 294, 297
252 static s32 expected_tpt_mimo3_40MHzSGI[IWL_RATE_COUNT] = {
253 0, 0, 0, 0, 160, 160, 219, 245, 261, 284, 294, 297, 300
257 const static struct iwl_rate_mcs_info iwl_rate_mcs[IWL_RATE_COUNT] = {
273 #define MCS_INDEX_PER_STREAM (8)
275 static inline u8 rs_extract_rate(u32 rate_n_flags)
277 return (u8)(rate_n_flags & 0xFF);
280 static void rs_rate_scale_clear_window(struct iwl_rate_scale_data *window)
283 window->success_counter = 0;
284 window->success_ratio = IWL_INVALID_VALUE;
286 window->average_tpt = IWL_INVALID_VALUE;
290 static inline u8 rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
292 return (ant_type & valid_antenna) == ant_type;
296 * removes the old data from the statistics. All data that is older than
297 * TID_MAX_TIME_DIFF, will be deleted.
299 static void rs_tl_rm_old_stats(struct iwl_traffic_load *tl, u32 curr_time)
301 /* The oldest age we want to keep */
302 u32 oldest_time = curr_time - TID_MAX_TIME_DIFF;
304 while (tl->queue_count &&
305 (tl->time_stamp < oldest_time)) {
306 tl->total -= tl->packet_count[tl->head];
307 tl->packet_count[tl->head] = 0;
308 tl->time_stamp += TID_QUEUE_CELL_SPACING;
311 if (tl->head >= TID_QUEUE_MAX_SIZE)
317 * increment traffic load value for tid and also remove
318 * any old values if passed the certain time period
320 static u8 rs_tl_add_packet(struct iwl_lq_sta *lq_data,
321 struct ieee80211_hdr *hdr)
323 u32 curr_time = jiffies_to_msecs(jiffies);
326 struct iwl_traffic_load *tl = NULL;
329 if (ieee80211_is_data_qos(hdr->frame_control)) {
330 u8 *qc = ieee80211_get_qos_ctl(hdr);
333 return MAX_TID_COUNT;
335 tl = &lq_data->load[tid];
337 curr_time -= curr_time % TID_ROUND_VALUE;
339 /* Happens only for the first packet. Initialize the data */
340 if (!(tl->queue_count)) {
342 tl->time_stamp = curr_time;
345 tl->packet_count[0] = 1;
346 return MAX_TID_COUNT;
349 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
350 index = time_diff / TID_QUEUE_CELL_SPACING;
352 /* The history is too long: remove data that is older than */
353 /* TID_MAX_TIME_DIFF */
354 if (index >= TID_QUEUE_MAX_SIZE)
355 rs_tl_rm_old_stats(tl, curr_time);
357 index = (tl->head + index) % TID_QUEUE_MAX_SIZE;
358 tl->packet_count[index] = tl->packet_count[index] + 1;
359 tl->total = tl->total + 1;
361 if ((index + 1) > tl->queue_count)
362 tl->queue_count = index + 1;
368 get the traffic load value for tid
370 static u32 rs_tl_get_load(struct iwl_lq_sta *lq_data, u8 tid)
372 u32 curr_time = jiffies_to_msecs(jiffies);
375 struct iwl_traffic_load *tl = NULL;
377 if (tid >= TID_MAX_LOAD_COUNT)
380 tl = &(lq_data->load[tid]);
382 curr_time -= curr_time % TID_ROUND_VALUE;
384 if (!(tl->queue_count))
387 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
388 index = time_diff / TID_QUEUE_CELL_SPACING;
390 /* The history is too long: remove data that is older than */
391 /* TID_MAX_TIME_DIFF */
392 if (index >= TID_QUEUE_MAX_SIZE)
393 rs_tl_rm_old_stats(tl, curr_time);
398 static void rs_tl_turn_on_agg_for_tid(struct iwl_priv *priv,
399 struct iwl_lq_sta *lq_data, u8 tid,
400 struct ieee80211_sta *sta)
402 if (rs_tl_get_load(lq_data, tid) > IWL_AGG_LOAD_THRESHOLD) {
403 IWL_DEBUG_HT(priv, "Starting Tx agg: STA: %pM tid: %d\n",
405 ieee80211_start_tx_ba_session(priv->hw, sta->addr, tid);
409 static void rs_tl_turn_on_agg(struct iwl_priv *priv, u8 tid,
410 struct iwl_lq_sta *lq_data,
411 struct ieee80211_sta *sta)
413 if ((tid < TID_MAX_LOAD_COUNT))
414 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
415 else if (tid == IWL_AGG_ALL_TID)
416 for (tid = 0; tid < TID_MAX_LOAD_COUNT; tid++)
417 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
420 static inline int get_num_of_ant_from_rate(u32 rate_n_flags)
422 return !!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
423 !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
424 !!(rate_n_flags & RATE_MCS_ANT_C_MSK);
428 * rs_collect_tx_data - Update the success/failure sliding window
430 * We keep a sliding window of the last 62 packets transmitted
431 * at this rate. window->data contains the bitmask of successful
434 static int rs_collect_tx_data(struct iwl_rate_scale_data *windows,
435 int scale_index, s32 tpt, int retries,
438 struct iwl_rate_scale_data *window = NULL;
439 static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
442 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
445 /* Select data for current tx bit rate */
446 window = &(windows[scale_index]);
449 * Keep track of only the latest 62 tx frame attempts in this rate's
450 * history window; anything older isn't really relevant any more.
451 * If we have filled up the sliding window, drop the oldest attempt;
452 * if the oldest attempt (highest bit in bitmap) shows "success",
453 * subtract "1" from the success counter (this is the main reason
454 * we keep these bitmaps!).
456 while (retries > 0) {
457 if (window->counter >= IWL_RATE_MAX_WINDOW) {
459 /* remove earliest */
460 window->counter = IWL_RATE_MAX_WINDOW - 1;
462 if (window->data & mask) {
463 window->data &= ~mask;
464 window->success_counter--;
468 /* Increment frames-attempted counter */
471 /* Shift bitmap by one frame (throw away oldest history),
472 * OR in "1", and increment "success" if this
473 * frame was successful. */
476 window->success_counter++;
484 /* Calculate current success ratio, avoid divide-by-0! */
485 if (window->counter > 0)
486 window->success_ratio = 128 * (100 * window->success_counter)
489 window->success_ratio = IWL_INVALID_VALUE;
491 fail_count = window->counter - window->success_counter;
493 /* Calculate average throughput, if we have enough history. */
494 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
495 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
496 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
498 window->average_tpt = IWL_INVALID_VALUE;
500 /* Tag this window as having been updated */
501 window->stamp = jiffies;
507 * Fill uCode API rate_n_flags field, based on "search" or "active" table.
509 /* FIXME:RS:remove this function and put the flags statically in the table */
510 static u32 rate_n_flags_from_tbl(struct iwl_priv *priv,
511 struct iwl_scale_tbl_info *tbl,
512 int index, u8 use_green)
514 u32 rate_n_flags = 0;
516 if (is_legacy(tbl->lq_type)) {
517 rate_n_flags = iwl_rates[index].plcp;
518 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
519 rate_n_flags |= RATE_MCS_CCK_MSK;
521 } else if (is_Ht(tbl->lq_type)) {
522 if (index > IWL_LAST_OFDM_RATE) {
523 IWL_ERR(priv, "Invalid HT rate index %d\n", index);
524 index = IWL_LAST_OFDM_RATE;
526 rate_n_flags = RATE_MCS_HT_MSK;
528 if (is_siso(tbl->lq_type))
529 rate_n_flags |= iwl_rates[index].plcp_siso;
530 else if (is_mimo2(tbl->lq_type))
531 rate_n_flags |= iwl_rates[index].plcp_mimo2;
533 rate_n_flags |= iwl_rates[index].plcp_mimo3;
535 IWL_ERR(priv, "Invalid tbl->lq_type %d\n", tbl->lq_type);
538 rate_n_flags |= ((tbl->ant_type << RATE_MCS_ANT_POS) &
539 RATE_MCS_ANT_ABC_MSK);
541 if (is_Ht(tbl->lq_type)) {
544 rate_n_flags |= RATE_MCS_DUP_MSK;
546 rate_n_flags |= RATE_MCS_HT40_MSK;
549 rate_n_flags |= RATE_MCS_SGI_MSK;
552 rate_n_flags |= RATE_MCS_GF_MSK;
553 if (is_siso(tbl->lq_type) && tbl->is_SGI) {
554 rate_n_flags &= ~RATE_MCS_SGI_MSK;
555 IWL_ERR(priv, "GF was set with SGI:SISO\n");
563 * Interpret uCode API's rate_n_flags format,
564 * fill "search" or "active" tx mode table.
566 static int rs_get_tbl_info_from_mcs(const u32 rate_n_flags,
567 enum ieee80211_band band,
568 struct iwl_scale_tbl_info *tbl,
571 u32 ant_msk = (rate_n_flags & RATE_MCS_ANT_ABC_MSK);
572 u8 num_of_ant = get_num_of_ant_from_rate(rate_n_flags);
575 *rate_idx = iwl_hwrate_to_plcp_idx(rate_n_flags);
577 if (*rate_idx == IWL_RATE_INVALID) {
581 tbl->is_SGI = 0; /* default legacy setup */
584 tbl->ant_type = (ant_msk >> RATE_MCS_ANT_POS);
585 tbl->lq_type = LQ_NONE;
586 tbl->max_search = IWL_MAX_SEARCH;
588 /* legacy rate format */
589 if (!(rate_n_flags & RATE_MCS_HT_MSK)) {
590 if (num_of_ant == 1) {
591 if (band == IEEE80211_BAND_5GHZ)
598 if (rate_n_flags & RATE_MCS_SGI_MSK)
601 if ((rate_n_flags & RATE_MCS_HT40_MSK) ||
602 (rate_n_flags & RATE_MCS_DUP_MSK))
605 if (rate_n_flags & RATE_MCS_DUP_MSK)
608 mcs = rs_extract_rate(rate_n_flags);
611 if (mcs <= IWL_RATE_SISO_60M_PLCP) {
613 tbl->lq_type = LQ_SISO; /*else NONE*/
615 } else if (mcs <= IWL_RATE_MIMO2_60M_PLCP) {
617 tbl->lq_type = LQ_MIMO2;
620 if (num_of_ant == 3) {
621 tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
622 tbl->lq_type = LQ_MIMO3;
629 /* switch to another antenna/antennas and return 1 */
630 /* if no other valid antenna found, return 0 */
631 static int rs_toggle_antenna(u32 valid_ant, u32 *rate_n_flags,
632 struct iwl_scale_tbl_info *tbl)
636 if (!tbl->ant_type || tbl->ant_type > ANT_ABC)
639 if (!rs_is_valid_ant(valid_ant, tbl->ant_type))
642 new_ant_type = ant_toggle_lookup[tbl->ant_type];
644 while ((new_ant_type != tbl->ant_type) &&
645 !rs_is_valid_ant(valid_ant, new_ant_type))
646 new_ant_type = ant_toggle_lookup[new_ant_type];
648 if (new_ant_type == tbl->ant_type)
651 tbl->ant_type = new_ant_type;
652 *rate_n_flags &= ~RATE_MCS_ANT_ABC_MSK;
653 *rate_n_flags |= new_ant_type << RATE_MCS_ANT_POS;
657 /* in 4965 we don't use greenfield at all */
658 static inline u8 rs_use_green(struct iwl_priv *priv,
659 struct ieee80211_conf *conf)
663 if ((priv->hw_rev & CSR_HW_REV_TYPE_MSK) == CSR_HW_REV_TYPE_4965)
666 is_green = (conf_is_ht(conf) &&
667 priv->current_ht_config.is_green_field &&
668 !priv->current_ht_config.non_GF_STA_present);
673 * rs_get_supported_rates - get the available rates
675 * if management frame or broadcast frame only return
676 * basic available rates.
679 static u16 rs_get_supported_rates(struct iwl_lq_sta *lq_sta,
680 struct ieee80211_hdr *hdr,
681 enum iwl_table_type rate_type)
683 if (hdr && is_multicast_ether_addr(hdr->addr1) &&
684 lq_sta->active_rate_basic)
685 return lq_sta->active_rate_basic;
687 if (is_legacy(rate_type)) {
688 return lq_sta->active_legacy_rate;
690 if (is_siso(rate_type))
691 return lq_sta->active_siso_rate;
692 else if (is_mimo2(rate_type))
693 return lq_sta->active_mimo2_rate;
695 return lq_sta->active_mimo3_rate;
699 static u16 rs_get_adjacent_rate(struct iwl_priv *priv, u8 index, u16 rate_mask,
702 u8 high = IWL_RATE_INVALID;
703 u8 low = IWL_RATE_INVALID;
705 /* 802.11A or ht walks to the next literal adjacent rate in
707 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
711 /* Find the previous rate that is in the rate mask */
713 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
714 if (rate_mask & mask) {
720 /* Find the next rate that is in the rate mask */
722 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
723 if (rate_mask & mask) {
729 return (high << 8) | low;
733 while (low != IWL_RATE_INVALID) {
734 low = iwl_rates[low].prev_rs;
735 if (low == IWL_RATE_INVALID)
737 if (rate_mask & (1 << low))
739 IWL_DEBUG_RATE(priv, "Skipping masked lower rate: %d\n", low);
743 while (high != IWL_RATE_INVALID) {
744 high = iwl_rates[high].next_rs;
745 if (high == IWL_RATE_INVALID)
747 if (rate_mask & (1 << high))
749 IWL_DEBUG_RATE(priv, "Skipping masked higher rate: %d\n", high);
752 return (high << 8) | low;
755 static u32 rs_get_lower_rate(struct iwl_lq_sta *lq_sta,
756 struct iwl_scale_tbl_info *tbl,
757 u8 scale_index, u8 ht_possible)
762 u8 switch_to_legacy = 0;
763 u8 is_green = lq_sta->is_green;
765 /* check if we need to switch from HT to legacy rates.
766 * assumption is that mandatory rates (1Mbps or 6Mbps)
767 * are always supported (spec demand) */
768 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
769 switch_to_legacy = 1;
770 scale_index = rs_ht_to_legacy[scale_index];
771 if (lq_sta->band == IEEE80211_BAND_5GHZ)
776 if (num_of_ant(tbl->ant_type) > 1)
777 tbl->ant_type = ANT_A;/*FIXME:RS*/
781 tbl->max_search = IWL_MAX_SEARCH;
784 rate_mask = rs_get_supported_rates(lq_sta, NULL, tbl->lq_type);
786 /* Mask with station rate restriction */
787 if (is_legacy(tbl->lq_type)) {
788 /* supp_rates has no CCK bits in A mode */
789 if (lq_sta->band == IEEE80211_BAND_5GHZ)
790 rate_mask = (u16)(rate_mask &
791 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
793 rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
796 /* If we switched from HT to legacy, check current rate */
797 if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
802 high_low = rs_get_adjacent_rate(lq_sta->drv, scale_index, rate_mask,
804 low = high_low & 0xff;
806 if (low == IWL_RATE_INVALID)
810 return rate_n_flags_from_tbl(lq_sta->drv, tbl, low, is_green);
814 * mac80211 sends us Tx status
816 static void rs_tx_status(void *priv_r, struct ieee80211_supported_band *sband,
817 struct ieee80211_sta *sta, void *priv_sta,
822 int rs_index, index = 0;
823 struct iwl_lq_sta *lq_sta = priv_sta;
824 struct iwl_link_quality_cmd *table;
825 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
826 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
827 struct ieee80211_hw *hw = priv->hw;
828 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
829 struct iwl_rate_scale_data *window = NULL;
830 struct iwl_rate_scale_data *search_win = NULL;
832 struct iwl_scale_tbl_info tbl_type;
833 struct iwl_scale_tbl_info *curr_tbl, *search_tbl;
837 IWL_DEBUG_RATE_LIMIT(priv, "get frame ack response, update rate scale window\n");
839 if (!ieee80211_is_data(hdr->frame_control) ||
840 info->flags & IEEE80211_TX_CTL_NO_ACK)
843 /* This packet was aggregated but doesn't carry rate scale info */
844 if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
845 !(info->flags & IEEE80211_TX_STAT_AMPDU))
848 if (info->flags & IEEE80211_TX_STAT_AMPDU)
851 retries = info->status.rates[0].count - 1;
856 if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) &&
857 !lq_sta->ibss_sta_added)
861 active_index = lq_sta->active_tbl;
863 curr_tbl = &(lq_sta->lq_info[active_index]);
864 search_tbl = &(lq_sta->lq_info[(1 - active_index)]);
865 window = (struct iwl_rate_scale_data *)&(curr_tbl->win[0]);
866 search_win = (struct iwl_rate_scale_data *)&(search_tbl->win[0]);
869 * Ignore this Tx frame response if its initial rate doesn't match
870 * that of latest Link Quality command. There may be stragglers
871 * from a previous Link Quality command, but we're no longer interested
872 * in those; they're either from the "active" mode while we're trying
873 * to check "search" mode, or a prior "search" mode after we've moved
874 * to a new "search" mode (which might become the new "active" mode).
876 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
877 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
878 if (priv->band == IEEE80211_BAND_5GHZ)
879 rs_index -= IWL_FIRST_OFDM_RATE;
881 if ((info->status.rates[0].idx < 0) ||
882 (tbl_type.is_SGI != !!(info->status.rates[0].flags & IEEE80211_TX_RC_SHORT_GI)) ||
883 (tbl_type.is_ht40 != !!(info->status.rates[0].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)) ||
884 (tbl_type.is_dup != !!(info->status.rates[0].flags & IEEE80211_TX_RC_DUP_DATA)) ||
885 (tbl_type.ant_type != info->antenna_sel_tx) ||
886 (!!(tx_rate & RATE_MCS_HT_MSK) != !!(info->status.rates[0].flags & IEEE80211_TX_RC_MCS)) ||
887 (!!(tx_rate & RATE_MCS_GF_MSK) != !!(info->status.rates[0].flags & IEEE80211_TX_RC_GREEN_FIELD)) ||
888 (hw->wiphy->bands[priv->band]->bitrates[rs_index].bitrate !=
889 hw->wiphy->bands[info->band]->bitrates[info->status.rates[0].idx].bitrate)) {
890 IWL_DEBUG_RATE(priv, "initial rate does not match 0x%x\n", tx_rate);
891 /* the last LQ command could failed so the LQ in ucode not
892 * the same in driver sync up
894 lq_sta->missed_rate_counter++;
895 if (lq_sta->missed_rate_counter > IWL_MISSED_RATE_MAX) {
896 lq_sta->missed_rate_counter = 0;
897 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
902 lq_sta->missed_rate_counter = 0;
903 /* Update frame history window with "failure" for each Tx retry. */
905 /* Look up the rate and other info used for each tx attempt.
906 * Each tx attempt steps one entry deeper in the rate table. */
907 tx_rate = le32_to_cpu(table->rs_table[index].rate_n_flags);
908 rs_get_tbl_info_from_mcs(tx_rate, priv->band,
909 &tbl_type, &rs_index);
911 /* If type matches "search" table,
912 * add failure to "search" history */
913 if ((tbl_type.lq_type == search_tbl->lq_type) &&
914 (tbl_type.ant_type == search_tbl->ant_type) &&
915 (tbl_type.is_SGI == search_tbl->is_SGI)) {
916 if (search_tbl->expected_tpt)
917 tpt = search_tbl->expected_tpt[rs_index];
920 rs_collect_tx_data(search_win, rs_index, tpt, 1, 0);
922 /* Else if type matches "current/active" table,
923 * add failure to "current/active" history */
924 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
925 (tbl_type.ant_type == curr_tbl->ant_type) &&
926 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
927 if (curr_tbl->expected_tpt)
928 tpt = curr_tbl->expected_tpt[rs_index];
931 rs_collect_tx_data(window, rs_index, tpt, 1, 0);
934 /* If not searching for a new mode, increment failed counter
935 * ... this helps determine when to start searching again */
936 if (lq_sta->stay_in_tbl)
937 lq_sta->total_failed++;
944 * Find (by rate) the history window to update with final Tx attempt;
945 * if Tx was successful first try, use original rate,
946 * else look up the rate that was, finally, successful.
948 tx_rate = le32_to_cpu(table->rs_table[index].rate_n_flags);
949 lq_sta->last_rate_n_flags = tx_rate;
950 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
952 /* Update frame history window with "success" if Tx got ACKed ... */
953 status = !!(info->flags & IEEE80211_TX_STAT_ACK);
955 /* If type matches "search" table,
956 * add final tx status to "search" history */
957 if ((tbl_type.lq_type == search_tbl->lq_type) &&
958 (tbl_type.ant_type == search_tbl->ant_type) &&
959 (tbl_type.is_SGI == search_tbl->is_SGI)) {
960 if (search_tbl->expected_tpt)
961 tpt = search_tbl->expected_tpt[rs_index];
964 if (info->flags & IEEE80211_TX_STAT_AMPDU)
965 rs_collect_tx_data(search_win, rs_index, tpt,
966 info->status.ampdu_ack_len,
967 info->status.ampdu_ack_map);
969 rs_collect_tx_data(search_win, rs_index, tpt,
971 /* Else if type matches "current/active" table,
972 * add final tx status to "current/active" history */
973 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
974 (tbl_type.ant_type == curr_tbl->ant_type) &&
975 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
976 if (curr_tbl->expected_tpt)
977 tpt = curr_tbl->expected_tpt[rs_index];
980 if (info->flags & IEEE80211_TX_STAT_AMPDU)
981 rs_collect_tx_data(window, rs_index, tpt,
982 info->status.ampdu_ack_len,
983 info->status.ampdu_ack_map);
985 rs_collect_tx_data(window, rs_index, tpt,
989 /* If not searching for new mode, increment success/failed counter
990 * ... these help determine when to start searching again */
991 if (lq_sta->stay_in_tbl) {
992 if (info->flags & IEEE80211_TX_STAT_AMPDU) {
993 lq_sta->total_success += info->status.ampdu_ack_map;
994 lq_sta->total_failed +=
995 (info->status.ampdu_ack_len - info->status.ampdu_ack_map);
998 lq_sta->total_success++;
1000 lq_sta->total_failed++;
1004 /* See if there's a better rate or modulation mode to try. */
1005 if (sta && sta->supp_rates[sband->band])
1006 rs_rate_scale_perform(priv, skb, sta, lq_sta);
1012 * Begin a period of staying with a selected modulation mode.
1013 * Set "stay_in_tbl" flag to prevent any mode switches.
1014 * Set frame tx success limits according to legacy vs. high-throughput,
1015 * and reset overall (spanning all rates) tx success history statistics.
1016 * These control how long we stay using same modulation mode before
1017 * searching for a new mode.
1019 static void rs_set_stay_in_table(struct iwl_priv *priv, u8 is_legacy,
1020 struct iwl_lq_sta *lq_sta)
1022 IWL_DEBUG_RATE(priv, "we are staying in the same table\n");
1023 lq_sta->stay_in_tbl = 1; /* only place this gets set */
1025 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
1026 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
1027 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
1029 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
1030 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
1031 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
1033 lq_sta->table_count = 0;
1034 lq_sta->total_failed = 0;
1035 lq_sta->total_success = 0;
1036 lq_sta->flush_timer = jiffies;
1037 lq_sta->action_counter = 0;
1041 * Find correct throughput table for given mode of modulation
1043 static void rs_set_expected_tpt_table(struct iwl_lq_sta *lq_sta,
1044 struct iwl_scale_tbl_info *tbl)
1046 if (is_legacy(tbl->lq_type)) {
1047 if (!is_a_band(tbl->lq_type))
1048 tbl->expected_tpt = expected_tpt_G;
1050 tbl->expected_tpt = expected_tpt_A;
1051 } else if (is_siso(tbl->lq_type)) {
1052 if (tbl->is_ht40 && !lq_sta->is_dup)
1054 tbl->expected_tpt = expected_tpt_siso40MHzSGI;
1056 tbl->expected_tpt = expected_tpt_siso40MHz;
1057 else if (tbl->is_SGI)
1058 tbl->expected_tpt = expected_tpt_siso20MHzSGI;
1060 tbl->expected_tpt = expected_tpt_siso20MHz;
1061 } else if (is_mimo2(tbl->lq_type)) {
1062 if (tbl->is_ht40 && !lq_sta->is_dup)
1064 tbl->expected_tpt = expected_tpt_mimo2_40MHzSGI;
1066 tbl->expected_tpt = expected_tpt_mimo2_40MHz;
1067 else if (tbl->is_SGI)
1068 tbl->expected_tpt = expected_tpt_mimo2_20MHzSGI;
1070 tbl->expected_tpt = expected_tpt_mimo2_20MHz;
1071 } else if (is_mimo3(tbl->lq_type)) {
1072 if (tbl->is_ht40 && !lq_sta->is_dup)
1074 tbl->expected_tpt = expected_tpt_mimo3_40MHzSGI;
1076 tbl->expected_tpt = expected_tpt_mimo3_40MHz;
1077 else if (tbl->is_SGI)
1078 tbl->expected_tpt = expected_tpt_mimo3_20MHzSGI;
1080 tbl->expected_tpt = expected_tpt_mimo3_20MHz;
1082 tbl->expected_tpt = expected_tpt_G;
1086 * Find starting rate for new "search" high-throughput mode of modulation.
1087 * Goal is to find lowest expected rate (under perfect conditions) that is
1088 * above the current measured throughput of "active" mode, to give new mode
1089 * a fair chance to prove itself without too many challenges.
1091 * This gets called when transitioning to more aggressive modulation
1092 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
1093 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1094 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1095 * bit rate will typically need to increase, but not if performance was bad.
1097 static s32 rs_get_best_rate(struct iwl_priv *priv,
1098 struct iwl_lq_sta *lq_sta,
1099 struct iwl_scale_tbl_info *tbl, /* "search" */
1100 u16 rate_mask, s8 index)
1102 /* "active" values */
1103 struct iwl_scale_tbl_info *active_tbl =
1104 &(lq_sta->lq_info[lq_sta->active_tbl]);
1105 s32 active_sr = active_tbl->win[index].success_ratio;
1106 s32 active_tpt = active_tbl->expected_tpt[index];
1108 /* expected "search" throughput */
1109 s32 *tpt_tbl = tbl->expected_tpt;
1111 s32 new_rate, high, low, start_hi;
1115 new_rate = high = low = start_hi = IWL_RATE_INVALID;
1118 high_low = rs_get_adjacent_rate(priv, rate, rate_mask,
1121 low = high_low & 0xff;
1122 high = (high_low >> 8) & 0xff;
1125 * Lower the "search" bit rate, to give new "search" mode
1126 * approximately the same throughput as "active" if:
1128 * 1) "Active" mode has been working modestly well (but not
1129 * great), and expected "search" throughput (under perfect
1130 * conditions) at candidate rate is above the actual
1131 * measured "active" throughput (but less than expected
1132 * "active" throughput under perfect conditions).
1134 * 2) "Active" mode has been working perfectly or very well
1135 * and expected "search" throughput (under perfect
1136 * conditions) at candidate rate is above expected
1137 * "active" throughput (under perfect conditions).
1139 if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
1140 ((active_sr > IWL_RATE_DECREASE_TH) &&
1141 (active_sr <= IWL_RATE_HIGH_TH) &&
1142 (tpt_tbl[rate] <= active_tpt))) ||
1143 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
1144 (tpt_tbl[rate] > active_tpt))) {
1146 /* (2nd or later pass)
1147 * If we've already tried to raise the rate, and are
1148 * now trying to lower it, use the higher rate. */
1149 if (start_hi != IWL_RATE_INVALID) {
1150 new_rate = start_hi;
1156 /* Loop again with lower rate */
1157 if (low != IWL_RATE_INVALID)
1160 /* Lower rate not available, use the original */
1164 /* Else try to raise the "search" rate to match "active" */
1166 /* (2nd or later pass)
1167 * If we've already tried to lower the rate, and are
1168 * now trying to raise it, use the lower rate. */
1169 if (new_rate != IWL_RATE_INVALID)
1172 /* Loop again with higher rate */
1173 else if (high != IWL_RATE_INVALID) {
1177 /* Higher rate not available, use the original */
1189 * Set up search table for MIMO2
1191 static int rs_switch_to_mimo2(struct iwl_priv *priv,
1192 struct iwl_lq_sta *lq_sta,
1193 struct ieee80211_conf *conf,
1194 struct ieee80211_sta *sta,
1195 struct iwl_scale_tbl_info *tbl, int index)
1199 s8 is_green = lq_sta->is_green;
1201 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1204 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
1205 == WLAN_HT_CAP_SM_PS_STATIC)
1208 /* Need both Tx chains/antennas to support MIMO */
1209 if (priv->hw_params.tx_chains_num < 2)
1212 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO2\n");
1214 tbl->lq_type = LQ_MIMO2;
1215 tbl->is_dup = lq_sta->is_dup;
1217 tbl->max_search = IWL_MAX_SEARCH;
1218 rate_mask = lq_sta->active_mimo2_rate;
1220 if (iwl_is_ht40_tx_allowed(priv, &sta->ht_cap))
1225 /* FIXME: - don't toggle SGI here
1227 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
1231 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
1237 rs_set_expected_tpt_table(lq_sta, tbl);
1239 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1241 IWL_DEBUG_RATE(priv, "LQ: MIMO2 best rate %d mask %X\n", rate, rate_mask);
1242 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1243 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
1247 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
1249 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
1250 tbl->current_rate, is_green);
1255 * Set up search table for MIMO3
1257 static int rs_switch_to_mimo3(struct iwl_priv *priv,
1258 struct iwl_lq_sta *lq_sta,
1259 struct ieee80211_conf *conf,
1260 struct ieee80211_sta *sta,
1261 struct iwl_scale_tbl_info *tbl, int index)
1265 s8 is_green = lq_sta->is_green;
1267 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1270 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
1271 == WLAN_HT_CAP_SM_PS_STATIC)
1274 /* Need both Tx chains/antennas to support MIMO */
1275 if (priv->hw_params.tx_chains_num < 3)
1278 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO3\n");
1280 tbl->lq_type = LQ_MIMO3;
1281 tbl->is_dup = lq_sta->is_dup;
1283 tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
1284 rate_mask = lq_sta->active_mimo3_rate;
1286 if (iwl_is_ht40_tx_allowed(priv, &sta->ht_cap))
1291 /* FIXME: - don't toggle SGI here
1293 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
1297 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
1303 rs_set_expected_tpt_table(lq_sta, tbl);
1305 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1307 IWL_DEBUG_RATE(priv, "LQ: MIMO3 best rate %d mask %X\n",
1309 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1310 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
1314 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
1316 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
1317 tbl->current_rate, is_green);
1322 * Set up search table for SISO
1324 static int rs_switch_to_siso(struct iwl_priv *priv,
1325 struct iwl_lq_sta *lq_sta,
1326 struct ieee80211_conf *conf,
1327 struct ieee80211_sta *sta,
1328 struct iwl_scale_tbl_info *tbl, int index)
1331 u8 is_green = lq_sta->is_green;
1334 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1337 IWL_DEBUG_RATE(priv, "LQ: try to switch to SISO\n");
1339 tbl->is_dup = lq_sta->is_dup;
1340 tbl->lq_type = LQ_SISO;
1342 tbl->max_search = IWL_MAX_SEARCH;
1343 rate_mask = lq_sta->active_siso_rate;
1345 if (iwl_is_ht40_tx_allowed(priv, &sta->ht_cap))
1350 /* FIXME: - don't toggle SGI here
1352 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
1356 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
1363 tbl->is_SGI = 0; /*11n spec: no SGI in SISO+Greenfield*/
1365 rs_set_expected_tpt_table(lq_sta, tbl);
1366 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1368 IWL_DEBUG_RATE(priv, "LQ: get best rate %d mask %X\n", rate, rate_mask);
1369 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1370 IWL_DEBUG_RATE(priv, "can not switch with index %d rate mask %x\n",
1374 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
1375 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
1376 tbl->current_rate, is_green);
1381 * Try to switch to new modulation mode from legacy
1383 static int rs_move_legacy_other(struct iwl_priv *priv,
1384 struct iwl_lq_sta *lq_sta,
1385 struct ieee80211_conf *conf,
1386 struct ieee80211_sta *sta,
1389 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1390 struct iwl_scale_tbl_info *search_tbl =
1391 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1392 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1393 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1394 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1395 u8 start_action = tbl->action;
1396 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1397 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1399 u8 update_search_tbl_counter = 0;
1401 if (!iwl_ht_enabled(priv))
1402 /* stay in Legacy */
1403 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
1404 else if (iwl_tx_ant_restriction(priv) == IWL_TX_SINGLE &&
1405 tbl->action > IWL_LEGACY_SWITCH_SISO)
1406 tbl->action = IWL_LEGACY_SWITCH_SISO;
1408 lq_sta->action_counter++;
1409 switch (tbl->action) {
1410 case IWL_LEGACY_SWITCH_ANTENNA1:
1411 case IWL_LEGACY_SWITCH_ANTENNA2:
1412 IWL_DEBUG_RATE(priv, "LQ: Legacy toggle Antenna\n");
1414 if ((tbl->action == IWL_LEGACY_SWITCH_ANTENNA1 &&
1415 tx_chains_num <= 1) ||
1416 (tbl->action == IWL_LEGACY_SWITCH_ANTENNA2 &&
1417 tx_chains_num <= 2))
1420 /* Don't change antenna if success has been great */
1421 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1424 /* Set up search table to try other antenna */
1425 memcpy(search_tbl, tbl, sz);
1427 if (rs_toggle_antenna(valid_tx_ant,
1428 &search_tbl->current_rate, search_tbl)) {
1429 update_search_tbl_counter = 1;
1430 rs_set_expected_tpt_table(lq_sta, search_tbl);
1434 case IWL_LEGACY_SWITCH_SISO:
1435 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to SISO\n");
1437 /* Set up search table to try SISO */
1438 memcpy(search_tbl, tbl, sz);
1439 search_tbl->is_SGI = 0;
1440 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
1443 lq_sta->action_counter = 0;
1448 case IWL_LEGACY_SWITCH_MIMO2_AB:
1449 case IWL_LEGACY_SWITCH_MIMO2_AC:
1450 case IWL_LEGACY_SWITCH_MIMO2_BC:
1451 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO2\n");
1453 /* Set up search table to try MIMO */
1454 memcpy(search_tbl, tbl, sz);
1455 search_tbl->is_SGI = 0;
1457 if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AB)
1458 search_tbl->ant_type = ANT_AB;
1459 else if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AC)
1460 search_tbl->ant_type = ANT_AC;
1462 search_tbl->ant_type = ANT_BC;
1464 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1467 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
1470 lq_sta->action_counter = 0;
1475 case IWL_LEGACY_SWITCH_MIMO3_ABC:
1476 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO3\n");
1478 /* Set up search table to try MIMO3 */
1479 memcpy(search_tbl, tbl, sz);
1480 search_tbl->is_SGI = 0;
1482 search_tbl->ant_type = ANT_ABC;
1484 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1487 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1490 lq_sta->action_counter = 0;
1496 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
1497 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
1499 if (tbl->action == start_action)
1503 search_tbl->lq_type = LQ_NONE;
1507 lq_sta->search_better_tbl = 1;
1509 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
1510 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
1511 if (update_search_tbl_counter)
1512 search_tbl->action = tbl->action;
1518 * Try to switch to new modulation mode from SISO
1520 static int rs_move_siso_to_other(struct iwl_priv *priv,
1521 struct iwl_lq_sta *lq_sta,
1522 struct ieee80211_conf *conf,
1523 struct ieee80211_sta *sta, int index)
1525 u8 is_green = lq_sta->is_green;
1526 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1527 struct iwl_scale_tbl_info *search_tbl =
1528 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1529 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1530 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1531 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1532 u8 start_action = tbl->action;
1533 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1534 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1535 u8 update_search_tbl_counter = 0;
1538 if (iwl_tx_ant_restriction(priv) == IWL_TX_SINGLE &&
1539 tbl->action > IWL_SISO_SWITCH_ANTENNA2) {
1541 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1544 lq_sta->action_counter++;
1545 switch (tbl->action) {
1546 case IWL_SISO_SWITCH_ANTENNA1:
1547 case IWL_SISO_SWITCH_ANTENNA2:
1548 IWL_DEBUG_RATE(priv, "LQ: SISO toggle Antenna\n");
1550 if ((tbl->action == IWL_SISO_SWITCH_ANTENNA1 &&
1551 tx_chains_num <= 1) ||
1552 (tbl->action == IWL_SISO_SWITCH_ANTENNA2 &&
1553 tx_chains_num <= 2))
1556 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1559 memcpy(search_tbl, tbl, sz);
1560 if (rs_toggle_antenna(valid_tx_ant,
1561 &search_tbl->current_rate, search_tbl)) {
1562 update_search_tbl_counter = 1;
1566 case IWL_SISO_SWITCH_MIMO2_AB:
1567 case IWL_SISO_SWITCH_MIMO2_AC:
1568 case IWL_SISO_SWITCH_MIMO2_BC:
1569 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO2\n");
1570 memcpy(search_tbl, tbl, sz);
1571 search_tbl->is_SGI = 0;
1573 if (tbl->action == IWL_SISO_SWITCH_MIMO2_AB)
1574 search_tbl->ant_type = ANT_AB;
1575 else if (tbl->action == IWL_SISO_SWITCH_MIMO2_AC)
1576 search_tbl->ant_type = ANT_AC;
1578 search_tbl->ant_type = ANT_BC;
1580 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1583 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
1588 case IWL_SISO_SWITCH_GI:
1589 if (!tbl->is_ht40 &&
1590 !(priv->current_ht_config.sgf &
1594 !(priv->current_ht_config.sgf &
1598 IWL_DEBUG_RATE(priv, "LQ: SISO toggle SGI/NGI\n");
1600 memcpy(search_tbl, tbl, sz);
1606 "SGI was set in GF+SISO\n");
1608 search_tbl->is_SGI = !tbl->is_SGI;
1609 rs_set_expected_tpt_table(lq_sta, search_tbl);
1611 s32 tpt = lq_sta->last_tpt / 100;
1612 if (tpt >= search_tbl->expected_tpt[index])
1615 search_tbl->current_rate =
1616 rate_n_flags_from_tbl(priv, search_tbl,
1618 update_search_tbl_counter = 1;
1620 case IWL_SISO_SWITCH_MIMO3_ABC:
1621 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO3\n");
1622 memcpy(search_tbl, tbl, sz);
1623 search_tbl->is_SGI = 0;
1624 search_tbl->ant_type = ANT_ABC;
1626 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1629 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1636 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
1637 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1639 if (tbl->action == start_action)
1642 search_tbl->lq_type = LQ_NONE;
1646 lq_sta->search_better_tbl = 1;
1648 if (tbl->action > IWL_SISO_SWITCH_MIMO3_ABC)
1649 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1650 if (update_search_tbl_counter)
1651 search_tbl->action = tbl->action;
1657 * Try to switch to new modulation mode from MIMO2
1659 static int rs_move_mimo2_to_other(struct iwl_priv *priv,
1660 struct iwl_lq_sta *lq_sta,
1661 struct ieee80211_conf *conf,
1662 struct ieee80211_sta *sta, int index)
1664 s8 is_green = lq_sta->is_green;
1665 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1666 struct iwl_scale_tbl_info *search_tbl =
1667 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1668 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1669 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1670 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1671 u8 start_action = tbl->action;
1672 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1673 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1674 u8 update_search_tbl_counter = 0;
1677 if ((iwl_tx_ant_restriction(priv) == IWL_TX_SINGLE) &&
1678 (tbl->action < IWL_MIMO2_SWITCH_SISO_A ||
1679 tbl->action > IWL_MIMO2_SWITCH_SISO_C)) {
1680 /* switch in SISO */
1681 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1684 lq_sta->action_counter++;
1685 switch (tbl->action) {
1686 case IWL_MIMO2_SWITCH_ANTENNA1:
1687 case IWL_MIMO2_SWITCH_ANTENNA2:
1688 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle Antennas\n");
1690 if (tx_chains_num <= 2)
1693 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1696 memcpy(search_tbl, tbl, sz);
1697 if (rs_toggle_antenna(valid_tx_ant,
1698 &search_tbl->current_rate, search_tbl)) {
1699 update_search_tbl_counter = 1;
1703 case IWL_MIMO2_SWITCH_SISO_A:
1704 case IWL_MIMO2_SWITCH_SISO_B:
1705 case IWL_MIMO2_SWITCH_SISO_C:
1706 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to SISO\n");
1708 /* Set up new search table for SISO */
1709 memcpy(search_tbl, tbl, sz);
1711 if (tbl->action == IWL_MIMO2_SWITCH_SISO_A)
1712 search_tbl->ant_type = ANT_A;
1713 else if (tbl->action == IWL_MIMO2_SWITCH_SISO_B)
1714 search_tbl->ant_type = ANT_B;
1716 search_tbl->ant_type = ANT_C;
1718 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1721 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
1728 case IWL_MIMO2_SWITCH_GI:
1729 if (!tbl->is_ht40 &&
1730 !(priv->current_ht_config.sgf &
1734 !(priv->current_ht_config.sgf &
1738 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle SGI/NGI\n");
1740 /* Set up new search table for MIMO2 */
1741 memcpy(search_tbl, tbl, sz);
1742 search_tbl->is_SGI = !tbl->is_SGI;
1743 rs_set_expected_tpt_table(lq_sta, search_tbl);
1745 * If active table already uses the fastest possible
1746 * modulation (dual stream with short guard interval),
1747 * and it's working well, there's no need to look
1748 * for a better type of modulation!
1751 s32 tpt = lq_sta->last_tpt / 100;
1752 if (tpt >= search_tbl->expected_tpt[index])
1755 search_tbl->current_rate =
1756 rate_n_flags_from_tbl(priv, search_tbl,
1758 update_search_tbl_counter = 1;
1761 case IWL_MIMO2_SWITCH_MIMO3_ABC:
1762 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to MIMO3\n");
1763 memcpy(search_tbl, tbl, sz);
1764 search_tbl->is_SGI = 0;
1765 search_tbl->ant_type = ANT_ABC;
1767 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1770 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1778 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1779 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
1781 if (tbl->action == start_action)
1784 search_tbl->lq_type = LQ_NONE;
1787 lq_sta->search_better_tbl = 1;
1789 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1790 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
1791 if (update_search_tbl_counter)
1792 search_tbl->action = tbl->action;
1799 * Try to switch to new modulation mode from MIMO3
1801 static int rs_move_mimo3_to_other(struct iwl_priv *priv,
1802 struct iwl_lq_sta *lq_sta,
1803 struct ieee80211_conf *conf,
1804 struct ieee80211_sta *sta, int index)
1806 s8 is_green = lq_sta->is_green;
1807 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1808 struct iwl_scale_tbl_info *search_tbl =
1809 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1810 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1811 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1812 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1813 u8 start_action = tbl->action;
1814 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1815 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1817 u8 update_search_tbl_counter = 0;
1819 if ((iwl_tx_ant_restriction(priv) == IWL_TX_SINGLE) &&
1820 (tbl->action < IWL_MIMO3_SWITCH_SISO_A ||
1821 tbl->action > IWL_MIMO3_SWITCH_SISO_C)) {
1822 /* switch in SISO */
1823 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
1826 lq_sta->action_counter++;
1827 switch (tbl->action) {
1828 case IWL_MIMO3_SWITCH_ANTENNA1:
1829 case IWL_MIMO3_SWITCH_ANTENNA2:
1830 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle Antennas\n");
1832 if (tx_chains_num <= 3)
1835 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1838 memcpy(search_tbl, tbl, sz);
1839 if (rs_toggle_antenna(valid_tx_ant,
1840 &search_tbl->current_rate, search_tbl))
1843 case IWL_MIMO3_SWITCH_SISO_A:
1844 case IWL_MIMO3_SWITCH_SISO_B:
1845 case IWL_MIMO3_SWITCH_SISO_C:
1846 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to SISO\n");
1848 /* Set up new search table for SISO */
1849 memcpy(search_tbl, tbl, sz);
1851 if (tbl->action == IWL_MIMO3_SWITCH_SISO_A)
1852 search_tbl->ant_type = ANT_A;
1853 else if (tbl->action == IWL_MIMO3_SWITCH_SISO_B)
1854 search_tbl->ant_type = ANT_B;
1856 search_tbl->ant_type = ANT_C;
1858 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1861 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
1868 case IWL_MIMO3_SWITCH_MIMO2_AB:
1869 case IWL_MIMO3_SWITCH_MIMO2_AC:
1870 case IWL_MIMO3_SWITCH_MIMO2_BC:
1871 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to MIMO2\n");
1873 memcpy(search_tbl, tbl, sz);
1874 search_tbl->is_SGI = 0;
1875 if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AB)
1876 search_tbl->ant_type = ANT_AB;
1877 else if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AC)
1878 search_tbl->ant_type = ANT_AC;
1880 search_tbl->ant_type = ANT_BC;
1882 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1885 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
1892 case IWL_MIMO3_SWITCH_GI:
1893 if (!tbl->is_ht40 &&
1894 !(priv->current_ht_config.sgf &
1898 !(priv->current_ht_config.sgf &
1902 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle SGI/NGI\n");
1904 /* Set up new search table for MIMO */
1905 memcpy(search_tbl, tbl, sz);
1906 search_tbl->is_SGI = !tbl->is_SGI;
1907 rs_set_expected_tpt_table(lq_sta, search_tbl);
1909 * If active table already uses the fastest possible
1910 * modulation (dual stream with short guard interval),
1911 * and it's working well, there's no need to look
1912 * for a better type of modulation!
1915 s32 tpt = lq_sta->last_tpt / 100;
1916 if (tpt >= search_tbl->expected_tpt[index])
1919 search_tbl->current_rate =
1920 rate_n_flags_from_tbl(priv, search_tbl,
1922 update_search_tbl_counter = 1;
1926 if (tbl->action > IWL_MIMO3_SWITCH_GI)
1927 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
1929 if (tbl->action == start_action)
1932 search_tbl->lq_type = LQ_NONE;
1935 lq_sta->search_better_tbl = 1;
1937 if (tbl->action > IWL_MIMO3_SWITCH_GI)
1938 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
1939 if (update_search_tbl_counter)
1940 search_tbl->action = tbl->action;
1947 * Check whether we should continue using same modulation mode, or
1948 * begin search for a new mode, based on:
1949 * 1) # tx successes or failures while using this mode
1950 * 2) # times calling this function
1951 * 3) elapsed time in this mode (not used, for now)
1953 static void rs_stay_in_table(struct iwl_lq_sta *lq_sta)
1955 struct iwl_scale_tbl_info *tbl;
1958 int flush_interval_passed = 0;
1959 struct iwl_priv *priv;
1962 active_tbl = lq_sta->active_tbl;
1964 tbl = &(lq_sta->lq_info[active_tbl]);
1966 /* If we've been disallowing search, see if we should now allow it */
1967 if (lq_sta->stay_in_tbl) {
1969 /* Elapsed time using current modulation mode */
1970 if (lq_sta->flush_timer)
1971 flush_interval_passed =
1973 (unsigned long)(lq_sta->flush_timer +
1974 IWL_RATE_SCALE_FLUSH_INTVL));
1977 * Check if we should allow search for new modulation mode.
1978 * If many frames have failed or succeeded, or we've used
1979 * this same modulation for a long time, allow search, and
1980 * reset history stats that keep track of whether we should
1981 * allow a new search. Also (below) reset all bitmaps and
1982 * stats in active history.
1984 if ((lq_sta->total_failed > lq_sta->max_failure_limit) ||
1985 (lq_sta->total_success > lq_sta->max_success_limit) ||
1986 ((!lq_sta->search_better_tbl) && (lq_sta->flush_timer)
1987 && (flush_interval_passed))) {
1988 IWL_DEBUG_RATE(priv, "LQ: stay is expired %d %d %d\n:",
1989 lq_sta->total_failed,
1990 lq_sta->total_success,
1991 flush_interval_passed);
1993 /* Allow search for new mode */
1994 lq_sta->stay_in_tbl = 0; /* only place reset */
1995 lq_sta->total_failed = 0;
1996 lq_sta->total_success = 0;
1997 lq_sta->flush_timer = 0;
2000 * Else if we've used this modulation mode enough repetitions
2001 * (regardless of elapsed time or success/failure), reset
2002 * history bitmaps and rate-specific stats for all rates in
2006 lq_sta->table_count++;
2007 if (lq_sta->table_count >=
2008 lq_sta->table_count_limit) {
2009 lq_sta->table_count = 0;
2011 IWL_DEBUG_RATE(priv, "LQ: stay in table clear win\n");
2012 for (i = 0; i < IWL_RATE_COUNT; i++)
2013 rs_rate_scale_clear_window(
2018 /* If transitioning to allow "search", reset all history
2019 * bitmaps and stats in active table (this will become the new
2020 * "search" table). */
2021 if (!lq_sta->stay_in_tbl) {
2022 for (i = 0; i < IWL_RATE_COUNT; i++)
2023 rs_rate_scale_clear_window(&(tbl->win[i]));
2029 * setup rate table in uCode
2030 * return rate_n_flags as used in the table
2032 static u32 rs_update_rate_tbl(struct iwl_priv *priv,
2033 struct iwl_lq_sta *lq_sta,
2034 struct iwl_scale_tbl_info *tbl,
2035 int index, u8 is_green)
2039 /* Update uCode's rate table. */
2040 rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
2041 rs_fill_link_cmd(priv, lq_sta, rate);
2042 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
2048 * Do rate scaling and search for new modulation mode.
2050 static void rs_rate_scale_perform(struct iwl_priv *priv,
2051 struct sk_buff *skb,
2052 struct ieee80211_sta *sta,
2053 struct iwl_lq_sta *lq_sta)
2055 struct ieee80211_hw *hw = priv->hw;
2056 struct ieee80211_conf *conf = &hw->conf;
2057 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2058 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2059 int low = IWL_RATE_INVALID;
2060 int high = IWL_RATE_INVALID;
2063 struct iwl_rate_scale_data *window = NULL;
2064 int current_tpt = IWL_INVALID_VALUE;
2065 int low_tpt = IWL_INVALID_VALUE;
2066 int high_tpt = IWL_INVALID_VALUE;
2068 s8 scale_action = 0;
2071 struct iwl_scale_tbl_info *tbl, *tbl1;
2072 u16 rate_scale_index_msk = 0;
2079 u8 tid = MAX_TID_COUNT;
2080 struct iwl_tid_data *tid_data;
2082 IWL_DEBUG_RATE(priv, "rate scale calculate new rate for skb\n");
2084 /* Send management frames and NO_ACK data using lowest rate. */
2085 /* TODO: this could probably be improved.. */
2086 if (!ieee80211_is_data(hdr->frame_control) ||
2087 info->flags & IEEE80211_TX_CTL_NO_ACK)
2090 if (!sta || !lq_sta)
2093 lq_sta->supp_rates = sta->supp_rates[lq_sta->band];
2095 tid = rs_tl_add_packet(lq_sta, hdr);
2098 * Select rate-scale / modulation-mode table to work with in
2099 * the rest of this function: "search" if searching for better
2100 * modulation mode, or "active" if doing rate scaling within a mode.
2102 if (!lq_sta->search_better_tbl)
2103 active_tbl = lq_sta->active_tbl;
2105 active_tbl = 1 - lq_sta->active_tbl;
2107 tbl = &(lq_sta->lq_info[active_tbl]);
2108 if (is_legacy(tbl->lq_type))
2109 lq_sta->is_green = 0;
2111 lq_sta->is_green = rs_use_green(priv, conf);
2112 is_green = lq_sta->is_green;
2114 /* current tx rate */
2115 index = lq_sta->last_txrate_idx;
2117 IWL_DEBUG_RATE(priv, "Rate scale index %d for type %d\n", index,
2120 /* rates available for this association, and for modulation mode */
2121 rate_mask = rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
2123 IWL_DEBUG_RATE(priv, "mask 0x%04X \n", rate_mask);
2125 /* mask with station rate restriction */
2126 if (is_legacy(tbl->lq_type)) {
2127 if (lq_sta->band == IEEE80211_BAND_5GHZ)
2128 /* supp_rates has no CCK bits in A mode */
2129 rate_scale_index_msk = (u16) (rate_mask &
2130 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
2132 rate_scale_index_msk = (u16) (rate_mask &
2133 lq_sta->supp_rates);
2136 rate_scale_index_msk = rate_mask;
2138 if (!rate_scale_index_msk)
2139 rate_scale_index_msk = rate_mask;
2141 if (!((1 << index) & rate_scale_index_msk)) {
2142 IWL_ERR(priv, "Current Rate is not valid\n");
2143 if (lq_sta->search_better_tbl) {
2144 /* revert to active table if search table is not valid*/
2145 tbl->lq_type = LQ_NONE;
2146 lq_sta->search_better_tbl = 0;
2147 tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
2148 /* get "active" rate info */
2149 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
2150 rate = rs_update_rate_tbl(priv, lq_sta,
2151 tbl, index, is_green);
2156 /* Get expected throughput table and history window for current rate */
2157 if (!tbl->expected_tpt) {
2158 IWL_ERR(priv, "tbl->expected_tpt is NULL\n");
2162 /* force user max rate if set by user */
2163 if ((lq_sta->max_rate_idx != -1) &&
2164 (lq_sta->max_rate_idx < index)) {
2165 index = lq_sta->max_rate_idx;
2167 window = &(tbl->win[index]);
2171 window = &(tbl->win[index]);
2174 * If there is not enough history to calculate actual average
2175 * throughput, keep analyzing results of more tx frames, without
2176 * changing rate or mode (bypass most of the rest of this function).
2177 * Set up new rate table in uCode only if old rate is not supported
2178 * in current association (use new rate found above).
2180 fail_count = window->counter - window->success_counter;
2181 if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
2182 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
2183 IWL_DEBUG_RATE(priv, "LQ: still below TH. succ=%d total=%d "
2185 window->success_counter, window->counter, index);
2187 /* Can't calculate this yet; not enough history */
2188 window->average_tpt = IWL_INVALID_VALUE;
2190 /* Should we stay with this modulation mode,
2191 * or search for a new one? */
2192 rs_stay_in_table(lq_sta);
2197 /* Else we have enough samples; calculate estimate of
2198 * actual average throughput */
2200 BUG_ON(window->average_tpt != ((window->success_ratio *
2201 tbl->expected_tpt[index] + 64) / 128));
2203 /* If we are searching for better modulation mode, check success. */
2204 if (lq_sta->search_better_tbl &&
2205 (iwl_tx_ant_restriction(priv) == IWL_TX_MULTI)) {
2206 /* If good success, continue using the "search" mode;
2207 * no need to send new link quality command, since we're
2208 * continuing to use the setup that we've been trying. */
2209 if (window->average_tpt > lq_sta->last_tpt) {
2211 IWL_DEBUG_RATE(priv, "LQ: SWITCHING TO NEW TABLE "
2212 "suc=%d cur-tpt=%d old-tpt=%d\n",
2213 window->success_ratio,
2214 window->average_tpt,
2217 if (!is_legacy(tbl->lq_type))
2218 lq_sta->enable_counter = 1;
2220 /* Swap tables; "search" becomes "active" */
2221 lq_sta->active_tbl = active_tbl;
2222 current_tpt = window->average_tpt;
2224 /* Else poor success; go back to mode in "active" table */
2227 IWL_DEBUG_RATE(priv, "LQ: GOING BACK TO THE OLD TABLE "
2228 "suc=%d cur-tpt=%d old-tpt=%d\n",
2229 window->success_ratio,
2230 window->average_tpt,
2233 /* Nullify "search" table */
2234 tbl->lq_type = LQ_NONE;
2236 /* Revert to "active" table */
2237 active_tbl = lq_sta->active_tbl;
2238 tbl = &(lq_sta->lq_info[active_tbl]);
2240 /* Revert to "active" rate and throughput info */
2241 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
2242 current_tpt = lq_sta->last_tpt;
2244 /* Need to set up a new rate table in uCode */
2248 /* Either way, we've made a decision; modulation mode
2249 * search is done, allow rate adjustment next time. */
2250 lq_sta->search_better_tbl = 0;
2251 done_search = 1; /* Don't switch modes below! */
2255 /* (Else) not in search of better modulation mode, try for better
2256 * starting rate, while staying in this mode. */
2257 high_low = rs_get_adjacent_rate(priv, index, rate_scale_index_msk,
2259 low = high_low & 0xff;
2260 high = (high_low >> 8) & 0xff;
2262 /* If user set max rate, dont allow higher than user constrain */
2263 if ((lq_sta->max_rate_idx != -1) &&
2264 (lq_sta->max_rate_idx < high))
2265 high = IWL_RATE_INVALID;
2267 sr = window->success_ratio;
2269 /* Collect measured throughputs for current and adjacent rates */
2270 current_tpt = window->average_tpt;
2271 if (low != IWL_RATE_INVALID)
2272 low_tpt = tbl->win[low].average_tpt;
2273 if (high != IWL_RATE_INVALID)
2274 high_tpt = tbl->win[high].average_tpt;
2278 /* Too many failures, decrease rate */
2279 if ((sr <= IWL_RATE_DECREASE_TH) || (current_tpt == 0)) {
2280 IWL_DEBUG_RATE(priv, "decrease rate because of low success_ratio\n");
2283 /* No throughput measured yet for adjacent rates; try increase. */
2284 } else if ((low_tpt == IWL_INVALID_VALUE) &&
2285 (high_tpt == IWL_INVALID_VALUE)) {
2287 if (high != IWL_RATE_INVALID && sr >= IWL_RATE_INCREASE_TH)
2289 else if (low != IWL_RATE_INVALID)
2293 /* Both adjacent throughputs are measured, but neither one has better
2294 * throughput; we're using the best rate, don't change it! */
2295 else if ((low_tpt != IWL_INVALID_VALUE) &&
2296 (high_tpt != IWL_INVALID_VALUE) &&
2297 (low_tpt < current_tpt) &&
2298 (high_tpt < current_tpt))
2301 /* At least one adjacent rate's throughput is measured,
2302 * and may have better performance. */
2304 /* Higher adjacent rate's throughput is measured */
2305 if (high_tpt != IWL_INVALID_VALUE) {
2306 /* Higher rate has better throughput */
2307 if (high_tpt > current_tpt &&
2308 sr >= IWL_RATE_INCREASE_TH) {
2314 /* Lower adjacent rate's throughput is measured */
2315 } else if (low_tpt != IWL_INVALID_VALUE) {
2316 /* Lower rate has better throughput */
2317 if (low_tpt > current_tpt) {
2318 IWL_DEBUG_RATE(priv,
2319 "decrease rate because of low tpt\n");
2321 } else if (sr >= IWL_RATE_INCREASE_TH) {
2327 /* Sanity check; asked for decrease, but success rate or throughput
2328 * has been good at old rate. Don't change it. */
2329 if ((scale_action == -1) && (low != IWL_RATE_INVALID) &&
2330 ((sr > IWL_RATE_HIGH_TH) ||
2331 (current_tpt > (100 * tbl->expected_tpt[low]))))
2333 if (!iwl_ht_enabled(priv) && !is_legacy(tbl->lq_type))
2335 if (iwl_tx_ant_restriction(priv) != IWL_TX_MULTI &&
2336 (is_mimo2(tbl->lq_type) || is_mimo3(tbl->lq_type)))
2338 switch (scale_action) {
2340 /* Decrease starting rate, update uCode's rate table */
2341 if (low != IWL_RATE_INVALID) {
2348 /* Increase starting rate, update uCode's rate table */
2349 if (high != IWL_RATE_INVALID) {
2361 IWL_DEBUG_RATE(priv, "choose rate scale index %d action %d low %d "
2362 "high %d type %d\n",
2363 index, scale_action, low, high, tbl->lq_type);
2366 /* Replace uCode's rate table for the destination station. */
2368 rate = rs_update_rate_tbl(priv, lq_sta,
2369 tbl, index, is_green);
2371 if (iwl_tx_ant_restriction(priv) == IWL_TX_MULTI) {
2372 /* Should we stay with this modulation mode,
2373 * or search for a new one? */
2374 rs_stay_in_table(lq_sta);
2377 * Search for new modulation mode if we're:
2378 * 1) Not changing rates right now
2379 * 2) Not just finishing up a search
2380 * 3) Allowing a new search
2382 if (!update_lq && !done_search && !lq_sta->stay_in_tbl && window->counter) {
2383 /* Save current throughput to compare with "search" throughput*/
2384 lq_sta->last_tpt = current_tpt;
2386 /* Select a new "search" modulation mode to try.
2387 * If one is found, set up the new "search" table. */
2388 if (is_legacy(tbl->lq_type))
2389 rs_move_legacy_other(priv, lq_sta, conf, sta, index);
2390 else if (is_siso(tbl->lq_type))
2391 rs_move_siso_to_other(priv, lq_sta, conf, sta, index);
2392 else if (is_mimo2(tbl->lq_type))
2393 rs_move_mimo2_to_other(priv, lq_sta, conf, sta, index);
2395 rs_move_mimo3_to_other(priv, lq_sta, conf, sta, index);
2397 /* If new "search" mode was selected, set up in uCode table */
2398 if (lq_sta->search_better_tbl) {
2399 /* Access the "search" table, clear its history. */
2400 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
2401 for (i = 0; i < IWL_RATE_COUNT; i++)
2402 rs_rate_scale_clear_window(&(tbl->win[i]));
2404 /* Use new "search" start rate */
2405 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
2407 IWL_DEBUG_RATE(priv, "Switch current mcs: %X index: %d\n",
2408 tbl->current_rate, index);
2409 rs_fill_link_cmd(priv, lq_sta, tbl->current_rate);
2410 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
2415 if (done_search && !lq_sta->stay_in_tbl) {
2416 /* If the "active" (non-search) mode was legacy,
2417 * and we've tried switching antennas,
2418 * but we haven't been able to try HT modes (not available),
2419 * stay with best antenna legacy modulation for a while
2420 * before next round of mode comparisons. */
2421 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
2422 if (is_legacy(tbl1->lq_type) && !conf_is_ht(conf) &&
2423 lq_sta->action_counter > tbl1->max_search) {
2424 IWL_DEBUG_RATE(priv, "LQ: STAY in legacy table\n");
2425 rs_set_stay_in_table(priv, 1, lq_sta);
2428 /* If we're in an HT mode, and all 3 mode switch actions
2429 * have been tried and compared, stay in this best modulation
2430 * mode for a while before next round of mode comparisons. */
2431 if (lq_sta->enable_counter &&
2432 (lq_sta->action_counter >= tbl1->max_search) &&
2433 iwl_ht_enabled(priv)) {
2434 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2435 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
2436 (tid != MAX_TID_COUNT)) {
2438 &priv->stations[lq_sta->lq.sta_id].tid[tid];
2439 if (tid_data->agg.state == IWL_AGG_OFF) {
2440 IWL_DEBUG_RATE(priv,
2441 "try to aggregate tid %d\n",
2443 rs_tl_turn_on_agg(priv, tid,
2447 rs_set_stay_in_table(priv, 0, lq_sta);
2452 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
2454 lq_sta->last_txrate_idx = i;
2460 static void rs_initialize_lq(struct iwl_priv *priv,
2461 struct ieee80211_conf *conf,
2462 struct ieee80211_sta *sta,
2463 struct iwl_lq_sta *lq_sta)
2465 struct iwl_scale_tbl_info *tbl;
2469 u8 use_green = rs_use_green(priv, conf);
2473 if (!sta || !lq_sta)
2476 i = lq_sta->last_txrate_idx;
2478 if ((lq_sta->lq.sta_id == 0xff) &&
2479 (priv->iw_mode == NL80211_IFTYPE_ADHOC))
2482 valid_tx_ant = priv->hw_params.valid_tx_ant;
2484 if (!lq_sta->search_better_tbl)
2485 active_tbl = lq_sta->active_tbl;
2487 active_tbl = 1 - lq_sta->active_tbl;
2489 tbl = &(lq_sta->lq_info[active_tbl]);
2491 if ((i < 0) || (i >= IWL_RATE_COUNT))
2494 rate = iwl_rates[i].plcp;
2495 tbl->ant_type = first_antenna(valid_tx_ant);
2496 rate |= tbl->ant_type << RATE_MCS_ANT_POS;
2498 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
2499 rate |= RATE_MCS_CCK_MSK;
2501 rs_get_tbl_info_from_mcs(rate, priv->band, tbl, &rate_idx);
2502 if (!rs_is_valid_ant(valid_tx_ant, tbl->ant_type))
2503 rs_toggle_antenna(valid_tx_ant, &rate, tbl);
2505 rate = rate_n_flags_from_tbl(priv, tbl, rate_idx, use_green);
2506 tbl->current_rate = rate;
2507 rs_set_expected_tpt_table(lq_sta, tbl);
2508 rs_fill_link_cmd(NULL, lq_sta, rate);
2509 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
2514 static void rs_get_rate(void *priv_r, struct ieee80211_sta *sta, void *priv_sta,
2515 struct ieee80211_tx_rate_control *txrc)
2518 struct sk_buff *skb = txrc->skb;
2519 struct ieee80211_supported_band *sband = txrc->sband;
2520 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
2521 struct ieee80211_conf *conf = &priv->hw->conf;
2522 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2523 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2524 struct iwl_lq_sta *lq_sta = priv_sta;
2527 IWL_DEBUG_RATE_LIMIT(priv, "rate scale calculate new rate for skb\n");
2529 /* Get max rate if user set max rate */
2531 lq_sta->max_rate_idx = txrc->max_rate_idx;
2532 if ((sband->band == IEEE80211_BAND_5GHZ) &&
2533 (lq_sta->max_rate_idx != -1))
2534 lq_sta->max_rate_idx += IWL_FIRST_OFDM_RATE;
2535 if ((lq_sta->max_rate_idx < 0) ||
2536 (lq_sta->max_rate_idx >= IWL_RATE_COUNT))
2537 lq_sta->max_rate_idx = -1;
2540 /* Send management frames and NO_ACK data using lowest rate. */
2541 if (rate_control_send_low(sta, priv_sta, txrc))
2544 rate_idx = lq_sta->last_txrate_idx;
2546 if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) &&
2547 !lq_sta->ibss_sta_added) {
2548 u8 sta_id = iwl_find_station(priv, hdr->addr1);
2550 if (sta_id == IWL_INVALID_STATION) {
2551 IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n",
2553 sta_id = iwl_add_station(priv, hdr->addr1,
2554 false, CMD_ASYNC, NULL);
2556 if ((sta_id != IWL_INVALID_STATION)) {
2557 lq_sta->lq.sta_id = sta_id;
2558 lq_sta->lq.rs_table[0].rate_n_flags = 0;
2559 lq_sta->ibss_sta_added = 1;
2560 rs_initialize_lq(priv, conf, sta, lq_sta);
2564 if (lq_sta->last_rate_n_flags & RATE_MCS_HT_MSK) {
2565 rate_idx -= IWL_FIRST_OFDM_RATE;
2566 /* 6M and 9M shared same MCS index */
2567 rate_idx = (rate_idx > 0) ? (rate_idx - 1) : 0;
2568 if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
2569 IWL_RATE_MIMO3_6M_PLCP)
2570 rate_idx = rate_idx + (2 * MCS_INDEX_PER_STREAM);
2571 else if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
2572 IWL_RATE_MIMO2_6M_PLCP)
2573 rate_idx = rate_idx + MCS_INDEX_PER_STREAM;
2574 info->control.rates[0].flags = IEEE80211_TX_RC_MCS;
2575 if (lq_sta->last_rate_n_flags & RATE_MCS_SGI_MSK)
2576 info->control.rates[0].flags |= IEEE80211_TX_RC_SHORT_GI;
2577 if (lq_sta->last_rate_n_flags & RATE_MCS_DUP_MSK)
2578 info->control.rates[0].flags |= IEEE80211_TX_RC_DUP_DATA;
2579 if (lq_sta->last_rate_n_flags & RATE_MCS_HT40_MSK)
2580 info->control.rates[0].flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
2581 if (lq_sta->last_rate_n_flags & RATE_MCS_GF_MSK)
2582 info->control.rates[0].flags |= IEEE80211_TX_RC_GREEN_FIELD;
2584 if (rate_idx < 0 || rate_idx > IWL_RATE_COUNT)
2585 rate_idx = rate_lowest_index(sband, sta);
2586 else if (sband->band == IEEE80211_BAND_5GHZ)
2587 rate_idx -= IWL_FIRST_OFDM_RATE;
2589 info->control.rates[0].idx = rate_idx;
2593 static void *rs_alloc_sta(void *priv_rate, struct ieee80211_sta *sta,
2596 struct iwl_lq_sta *lq_sta;
2597 struct iwl_priv *priv;
2600 priv = (struct iwl_priv *)priv_rate;
2601 IWL_DEBUG_RATE(priv, "create station rate scale window\n");
2603 lq_sta = kzalloc(sizeof(struct iwl_lq_sta), gfp);
2607 lq_sta->lq.sta_id = 0xff;
2610 for (j = 0; j < LQ_SIZE; j++)
2611 for (i = 0; i < IWL_RATE_COUNT; i++)
2612 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
2617 static void rs_rate_init(void *priv_r, struct ieee80211_supported_band *sband,
2618 struct ieee80211_sta *sta, void *priv_sta)
2621 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
2622 struct ieee80211_conf *conf = &priv->hw->conf;
2623 struct iwl_lq_sta *lq_sta = priv_sta;
2628 lq_sta->flush_timer = 0;
2629 lq_sta->supp_rates = sta->supp_rates[sband->band];
2630 for (j = 0; j < LQ_SIZE; j++)
2631 for (i = 0; i < IWL_RATE_COUNT; i++)
2632 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
2634 IWL_DEBUG_RATE(priv, "LQ: *** rate scale station global init ***\n");
2635 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2636 * the lowest or the highest rate.. Could consider using RSSI from
2637 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2640 lq_sta->ibss_sta_added = 0;
2641 if (priv->iw_mode == NL80211_IFTYPE_AP) {
2642 u8 sta_id = iwl_find_station(priv,
2645 /* for IBSS the call are from tasklet */
2646 IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n", sta->addr);
2648 if (sta_id == IWL_INVALID_STATION) {
2649 IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n", sta->addr);
2650 sta_id = iwl_add_station(priv, sta->addr, false,
2653 if ((sta_id != IWL_INVALID_STATION)) {
2654 lq_sta->lq.sta_id = sta_id;
2655 lq_sta->lq.rs_table[0].rate_n_flags = 0;
2657 /* FIXME: this is w/a remove it later */
2658 priv->assoc_station_added = 1;
2662 lq_sta->max_rate_idx = -1;
2663 lq_sta->missed_rate_counter = IWL_MISSED_RATE_MAX;
2664 lq_sta->is_green = rs_use_green(priv, conf);
2665 lq_sta->active_legacy_rate = priv->active_rate & ~(0x1000);
2666 lq_sta->active_rate_basic = priv->active_rate_basic;
2667 lq_sta->band = priv->band;
2669 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2670 * supp_rates[] does not; shift to convert format, force 9 MBits off.
2672 lq_sta->active_siso_rate = sta->ht_cap.mcs.rx_mask[0] << 1;
2673 lq_sta->active_siso_rate |= sta->ht_cap.mcs.rx_mask[0] & 0x1;
2674 lq_sta->active_siso_rate &= ~((u16)0x2);
2675 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
2678 lq_sta->active_mimo2_rate = sta->ht_cap.mcs.rx_mask[1] << 1;
2679 lq_sta->active_mimo2_rate |= sta->ht_cap.mcs.rx_mask[1] & 0x1;
2680 lq_sta->active_mimo2_rate &= ~((u16)0x2);
2681 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2683 lq_sta->active_mimo3_rate = sta->ht_cap.mcs.rx_mask[2] << 1;
2684 lq_sta->active_mimo3_rate |= sta->ht_cap.mcs.rx_mask[2] & 0x1;
2685 lq_sta->active_mimo3_rate &= ~((u16)0x2);
2686 lq_sta->active_mimo3_rate <<= IWL_FIRST_OFDM_RATE;
2688 IWL_DEBUG_RATE(priv, "SISO-RATE=%X MIMO2-RATE=%X MIMO3-RATE=%X\n",
2689 lq_sta->active_siso_rate,
2690 lq_sta->active_mimo2_rate,
2691 lq_sta->active_mimo3_rate);
2693 /* These values will be overridden later */
2694 lq_sta->lq.general_params.single_stream_ant_msk = ANT_A;
2695 lq_sta->lq.general_params.dual_stream_ant_msk = ANT_AB;
2697 /* as default allow aggregation for all tids */
2698 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
2701 /* Find highest tx rate supported by hardware and destination station */
2702 mask_bit = sta->supp_rates[sband->band];
2703 count = sband->n_bitrates;
2704 if (sband->band == IEEE80211_BAND_5GHZ) {
2705 count += IWL_FIRST_OFDM_RATE;
2706 start_rate = IWL_FIRST_OFDM_RATE;
2707 mask_bit <<= IWL_FIRST_OFDM_RATE;
2710 mask_bit = mask_bit & lq_sta->active_legacy_rate;
2711 lq_sta->last_txrate_idx = 4;
2712 for (i = start_rate; i < count; i++)
2713 if (mask_bit & BIT(i))
2714 lq_sta->last_txrate_idx = i;
2716 rs_initialize_lq(priv, conf, sta, lq_sta);
2719 static void rs_fill_link_cmd(struct iwl_priv *priv,
2720 struct iwl_lq_sta *lq_sta, u32 new_rate)
2722 struct iwl_scale_tbl_info tbl_type;
2725 int repeat_rate = 0;
2726 u8 ant_toggle_cnt = 0;
2727 u8 use_ht_possible = 1;
2728 u8 valid_tx_ant = 0;
2729 struct iwl_link_quality_cmd *lq_cmd = &lq_sta->lq;
2731 /* Override starting rate (index 0) if needed for debug purposes */
2732 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
2734 /* Interpret new_rate (rate_n_flags) */
2735 memset(&tbl_type, 0, sizeof(tbl_type));
2736 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
2737 &tbl_type, &rate_idx);
2739 /* How many times should we repeat the initial rate? */
2740 if (is_legacy(tbl_type.lq_type)) {
2742 repeat_rate = IWL_NUMBER_TRY;
2744 repeat_rate = IWL_HT_NUMBER_TRY;
2747 lq_cmd->general_params.mimo_delimiter =
2748 is_mimo(tbl_type.lq_type) ? 1 : 0;
2750 /* Fill 1st table entry (index 0) */
2751 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
2753 if (num_of_ant(tbl_type.ant_type) == 1) {
2754 lq_cmd->general_params.single_stream_ant_msk =
2756 } else if (num_of_ant(tbl_type.ant_type) == 2) {
2757 lq_cmd->general_params.dual_stream_ant_msk =
2759 } /* otherwise we don't modify the existing value */
2765 valid_tx_ant = priv->hw_params.valid_tx_ant;
2767 /* Fill rest of rate table */
2768 while (index < LINK_QUAL_MAX_RETRY_NUM) {
2769 /* Repeat initial/next rate.
2770 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2771 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
2772 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
2773 if (is_legacy(tbl_type.lq_type)) {
2774 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2777 rs_toggle_antenna(valid_tx_ant,
2778 &new_rate, &tbl_type))
2782 /* Override next rate if needed for debug purposes */
2783 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
2785 /* Fill next table entry */
2786 lq_cmd->rs_table[index].rate_n_flags =
2787 cpu_to_le32(new_rate);
2792 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band, &tbl_type,
2795 /* Indicate to uCode which entries might be MIMO.
2796 * If initial rate was MIMO, this will finally end up
2797 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
2798 if (is_mimo(tbl_type.lq_type))
2799 lq_cmd->general_params.mimo_delimiter = index;
2802 new_rate = rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
2805 /* How many times should we repeat the next rate? */
2806 if (is_legacy(tbl_type.lq_type)) {
2807 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2810 rs_toggle_antenna(valid_tx_ant,
2811 &new_rate, &tbl_type))
2814 repeat_rate = IWL_NUMBER_TRY;
2816 repeat_rate = IWL_HT_NUMBER_TRY;
2819 /* Don't allow HT rates after next pass.
2820 * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
2821 use_ht_possible = 0;
2823 /* Override next rate if needed for debug purposes */
2824 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
2826 /* Fill next table entry */
2827 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
2833 lq_cmd->agg_params.agg_frame_cnt_limit = LINK_QUAL_AGG_FRAME_LIMIT_MAX;
2834 lq_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
2835 lq_cmd->agg_params.agg_time_limit =
2836 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
2839 static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
2843 /* rate scale requires free function to be implemented */
2844 static void rs_free(void *priv_rate)
2849 static void rs_free_sta(void *priv_r, struct ieee80211_sta *sta,
2852 struct iwl_lq_sta *lq_sta = priv_sta;
2853 struct iwl_priv *priv __maybe_unused = priv_r;
2855 IWL_DEBUG_RATE(priv, "enter\n");
2857 IWL_DEBUG_RATE(priv, "leave\n");
2861 #ifdef CONFIG_MAC80211_DEBUGFS
2862 static int open_file_generic(struct inode *inode, struct file *file)
2864 file->private_data = inode->i_private;
2867 static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
2868 u32 *rate_n_flags, int index)
2870 struct iwl_priv *priv;
2875 valid_tx_ant = priv->hw_params.valid_tx_ant;
2876 if (lq_sta->dbg_fixed_rate) {
2878 ((lq_sta->dbg_fixed_rate & RATE_MCS_ANT_ABC_MSK)
2879 >> RATE_MCS_ANT_POS);
2880 if ((valid_tx_ant & ant_sel_tx) == ant_sel_tx) {
2881 *rate_n_flags = lq_sta->dbg_fixed_rate;
2882 IWL_DEBUG_RATE(priv, "Fixed rate ON\n");
2884 lq_sta->dbg_fixed_rate = 0;
2886 "Invalid antenna selection 0x%X, Valid is 0x%X\n",
2887 ant_sel_tx, valid_tx_ant);
2888 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
2891 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
2895 static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
2896 const char __user *user_buf, size_t count, loff_t *ppos)
2898 struct iwl_lq_sta *lq_sta = file->private_data;
2899 struct iwl_priv *priv;
2905 memset(buf, 0, sizeof(buf));
2906 buf_size = min(count, sizeof(buf) - 1);
2907 if (copy_from_user(buf, user_buf, buf_size))
2910 if (sscanf(buf, "%x", &parsed_rate) == 1)
2911 lq_sta->dbg_fixed_rate = parsed_rate;
2913 lq_sta->dbg_fixed_rate = 0;
2915 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
2916 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2917 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2918 lq_sta->active_mimo3_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2920 IWL_DEBUG_RATE(priv, "sta_id %d rate 0x%X\n",
2921 lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
2923 if (lq_sta->dbg_fixed_rate) {
2924 rs_fill_link_cmd(NULL, lq_sta, lq_sta->dbg_fixed_rate);
2925 iwl_send_lq_cmd(lq_sta->drv, &lq_sta->lq, CMD_ASYNC);
2931 static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
2932 char __user *user_buf, size_t count, loff_t *ppos)
2940 struct iwl_lq_sta *lq_sta = file->private_data;
2941 struct iwl_priv *priv;
2942 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
2945 buff = kmalloc(1024, GFP_KERNEL);
2949 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
2950 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
2951 lq_sta->total_failed, lq_sta->total_success,
2952 lq_sta->active_legacy_rate);
2953 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
2954 lq_sta->dbg_fixed_rate);
2955 desc += sprintf(buff+desc, "valid_tx_ant %s%s%s\n",
2956 (priv->hw_params.valid_tx_ant & ANT_A) ? "ANT_A," : "",
2957 (priv->hw_params.valid_tx_ant & ANT_B) ? "ANT_B," : "",
2958 (priv->hw_params.valid_tx_ant & ANT_C) ? "ANT_C" : "");
2959 desc += sprintf(buff+desc, "lq type %s\n",
2960 (is_legacy(tbl->lq_type)) ? "legacy" : "HT");
2961 if (is_Ht(tbl->lq_type)) {
2962 desc += sprintf(buff+desc, " %s",
2963 (is_siso(tbl->lq_type)) ? "SISO" :
2964 ((is_mimo2(tbl->lq_type)) ? "MIMO2" : "MIMO3"));
2965 desc += sprintf(buff+desc, " %s",
2966 (tbl->is_ht40) ? "40MHz" : "20MHz");
2967 desc += sprintf(buff+desc, " %s %s\n", (tbl->is_SGI) ? "SGI" : "",
2968 (lq_sta->is_green) ? "GF enabled" : "");
2970 desc += sprintf(buff+desc, "last tx rate=0x%X\n",
2971 lq_sta->last_rate_n_flags);
2972 desc += sprintf(buff+desc, "general:"
2973 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
2974 lq_sta->lq.general_params.flags,
2975 lq_sta->lq.general_params.mimo_delimiter,
2976 lq_sta->lq.general_params.single_stream_ant_msk,
2977 lq_sta->lq.general_params.dual_stream_ant_msk);
2979 desc += sprintf(buff+desc, "agg:"
2980 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
2981 le16_to_cpu(lq_sta->lq.agg_params.agg_time_limit),
2982 lq_sta->lq.agg_params.agg_dis_start_th,
2983 lq_sta->lq.agg_params.agg_frame_cnt_limit);
2985 desc += sprintf(buff+desc,
2986 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
2987 lq_sta->lq.general_params.start_rate_index[0],
2988 lq_sta->lq.general_params.start_rate_index[1],
2989 lq_sta->lq.general_params.start_rate_index[2],
2990 lq_sta->lq.general_params.start_rate_index[3]);
2992 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
2993 index = iwl_hwrate_to_plcp_idx(
2994 le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags));
2995 if (is_legacy(tbl->lq_type)) {
2996 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps\n",
2997 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
2998 iwl_rate_mcs[index].mbps);
3000 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps (%s)\n",
3001 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
3002 iwl_rate_mcs[index].mbps, iwl_rate_mcs[index].mcs);
3006 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3011 static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
3012 .write = rs_sta_dbgfs_scale_table_write,
3013 .read = rs_sta_dbgfs_scale_table_read,
3014 .open = open_file_generic,
3016 static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
3017 char __user *user_buf, size_t count, loff_t *ppos)
3024 struct iwl_lq_sta *lq_sta = file->private_data;
3026 buff = kmalloc(1024, GFP_KERNEL);
3030 for (i = 0; i < LQ_SIZE; i++) {
3031 desc += sprintf(buff+desc,
3032 "%s type=%d SGI=%d HT40=%d DUP=%d GF=%d\n"
3034 lq_sta->active_tbl == i ? "*" : "x",
3035 lq_sta->lq_info[i].lq_type,
3036 lq_sta->lq_info[i].is_SGI,
3037 lq_sta->lq_info[i].is_ht40,
3038 lq_sta->lq_info[i].is_dup,
3040 lq_sta->lq_info[i].current_rate);
3041 for (j = 0; j < IWL_RATE_COUNT; j++) {
3042 desc += sprintf(buff+desc,
3043 "counter=%d success=%d %%=%d\n",
3044 lq_sta->lq_info[i].win[j].counter,
3045 lq_sta->lq_info[i].win[j].success_counter,
3046 lq_sta->lq_info[i].win[j].success_ratio);
3049 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3054 static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
3055 .read = rs_sta_dbgfs_stats_table_read,
3056 .open = open_file_generic,
3059 static ssize_t rs_sta_dbgfs_rate_scale_data_read(struct file *file,
3060 char __user *user_buf, size_t count, loff_t *ppos)
3066 struct iwl_lq_sta *lq_sta = file->private_data;
3067 struct iwl_priv *priv;
3068 struct iwl_scale_tbl_info *tbl = &lq_sta->lq_info[lq_sta->active_tbl];
3072 if (is_Ht(tbl->lq_type))
3073 desc += sprintf(buff+desc,
3074 "Bit Rate= %d Mb/s\n",
3075 tbl->expected_tpt[lq_sta->last_txrate_idx]);
3077 desc += sprintf(buff+desc,
3078 "Bit Rate= %d Mb/s\n",
3079 iwl_rates[lq_sta->last_txrate_idx].ieee >> 1);
3080 desc += sprintf(buff+desc,
3081 "Signal Level= %d dBm\tNoise Level= %d dBm\n",
3082 priv->last_rx_rssi, priv->last_rx_noise);
3083 desc += sprintf(buff+desc,
3084 "Tsf= 0x%llx\tBeacon time= 0x%08X\n",
3085 priv->last_tsf, priv->last_beacon_time);
3087 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3091 static const struct file_operations rs_sta_dbgfs_rate_scale_data_ops = {
3092 .read = rs_sta_dbgfs_rate_scale_data_read,
3093 .open = open_file_generic,
3096 static void rs_add_debugfs(void *priv, void *priv_sta,
3099 struct iwl_lq_sta *lq_sta = priv_sta;
3100 lq_sta->rs_sta_dbgfs_scale_table_file =
3101 debugfs_create_file("rate_scale_table", 0600, dir,
3102 lq_sta, &rs_sta_dbgfs_scale_table_ops);
3103 lq_sta->rs_sta_dbgfs_stats_table_file =
3104 debugfs_create_file("rate_stats_table", 0600, dir,
3105 lq_sta, &rs_sta_dbgfs_stats_table_ops);
3106 lq_sta->rs_sta_dbgfs_rate_scale_data_file =
3107 debugfs_create_file("rate_scale_data", 0600, dir,
3108 lq_sta, &rs_sta_dbgfs_rate_scale_data_ops);
3109 lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
3110 debugfs_create_u8("tx_agg_tid_enable", 0600, dir,
3111 &lq_sta->tx_agg_tid_en);
3115 static void rs_remove_debugfs(void *priv, void *priv_sta)
3117 struct iwl_lq_sta *lq_sta = priv_sta;
3118 debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
3119 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
3120 debugfs_remove(lq_sta->rs_sta_dbgfs_rate_scale_data_file);
3121 debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
3125 static struct rate_control_ops rs_ops = {
3128 .tx_status = rs_tx_status,
3129 .get_rate = rs_get_rate,
3130 .rate_init = rs_rate_init,
3133 .alloc_sta = rs_alloc_sta,
3134 .free_sta = rs_free_sta,
3135 #ifdef CONFIG_MAC80211_DEBUGFS
3136 .add_sta_debugfs = rs_add_debugfs,
3137 .remove_sta_debugfs = rs_remove_debugfs,
3141 int iwlagn_rate_control_register(void)
3143 return ieee80211_rate_control_register(&rs_ops);
3146 void iwlagn_rate_control_unregister(void)
3148 ieee80211_rate_control_unregister(&rs_ops);