1 /******************************************************************************
3 * Copyright(c) 2005 - 2013 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/slab.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>
40 #include "iwl-op-mode.h"
43 #define RS_NAME "iwl-mvm-rs"
45 #define NUM_TRY_BEFORE_ANT_TOGGLE 1
46 #define IWL_NUMBER_TRY 1
47 #define IWL_HT_NUMBER_TRY 3
49 #define IWL_RATE_MAX_WINDOW 62 /* # tx in history window */
50 #define IWL_RATE_MIN_FAILURE_TH 3 /* min failures to calc tpt */
51 #define IWL_RATE_MIN_SUCCESS_TH 8 /* min successes to calc tpt */
53 /* max allowed rate miss before sync LQ cmd */
54 #define IWL_MISSED_RATE_MAX 15
55 #define RS_STAY_IN_COLUMN_TIMEOUT (5*HZ)
58 static u8 rs_ht_to_legacy[] = {
59 [IWL_RATE_MCS_0_INDEX] = IWL_RATE_6M_INDEX,
60 [IWL_RATE_MCS_1_INDEX] = IWL_RATE_9M_INDEX,
61 [IWL_RATE_MCS_2_INDEX] = IWL_RATE_12M_INDEX,
62 [IWL_RATE_MCS_3_INDEX] = IWL_RATE_18M_INDEX,
63 [IWL_RATE_MCS_4_INDEX] = IWL_RATE_24M_INDEX,
64 [IWL_RATE_MCS_5_INDEX] = IWL_RATE_36M_INDEX,
65 [IWL_RATE_MCS_6_INDEX] = IWL_RATE_48M_INDEX,
66 [IWL_RATE_MCS_7_INDEX] = IWL_RATE_54M_INDEX,
67 [IWL_RATE_MCS_8_INDEX] = IWL_RATE_54M_INDEX,
68 [IWL_RATE_MCS_9_INDEX] = IWL_RATE_54M_INDEX,
71 static const u8 ant_toggle_lookup[] = {
72 [ANT_NONE] = ANT_NONE,
82 #define IWL_DECLARE_RATE_INFO(r, s, rp, rn) \
83 [IWL_RATE_##r##M_INDEX] = { IWL_RATE_##r##M_PLCP, \
84 IWL_RATE_HT_SISO_MCS_##s##_PLCP, \
85 IWL_RATE_HT_MIMO2_MCS_##s##_PLCP, \
86 IWL_RATE_VHT_SISO_MCS_##s##_PLCP, \
87 IWL_RATE_VHT_MIMO2_MCS_##s##_PLCP,\
88 IWL_RATE_##rp##M_INDEX, \
89 IWL_RATE_##rn##M_INDEX }
91 #define IWL_DECLARE_MCS_RATE(s) \
92 [IWL_RATE_MCS_##s##_INDEX] = { IWL_RATE_INVM_PLCP, \
93 IWL_RATE_HT_SISO_MCS_##s##_PLCP, \
94 IWL_RATE_HT_MIMO2_MCS_##s##_PLCP, \
95 IWL_RATE_VHT_SISO_MCS_##s##_PLCP, \
96 IWL_RATE_VHT_MIMO2_MCS_##s##_PLCP, \
97 IWL_RATE_INVM_INDEX, \
102 * rate, ht rate, prev rate, next rate
104 * If there isn't a valid next or previous rate then INV is used which
105 * maps to IWL_RATE_INVALID
108 static const struct iwl_rs_rate_info iwl_rates[IWL_RATE_COUNT] = {
109 IWL_DECLARE_RATE_INFO(1, INV, INV, 2), /* 1mbps */
110 IWL_DECLARE_RATE_INFO(2, INV, 1, 5), /* 2mbps */
111 IWL_DECLARE_RATE_INFO(5, INV, 2, 11), /*5.5mbps */
112 IWL_DECLARE_RATE_INFO(11, INV, 9, 12), /* 11mbps */
113 IWL_DECLARE_RATE_INFO(6, 0, 5, 11), /* 6mbps ; MCS 0 */
114 IWL_DECLARE_RATE_INFO(9, INV, 6, 11), /* 9mbps */
115 IWL_DECLARE_RATE_INFO(12, 1, 11, 18), /* 12mbps ; MCS 1 */
116 IWL_DECLARE_RATE_INFO(18, 2, 12, 24), /* 18mbps ; MCS 2 */
117 IWL_DECLARE_RATE_INFO(24, 3, 18, 36), /* 24mbps ; MCS 3 */
118 IWL_DECLARE_RATE_INFO(36, 4, 24, 48), /* 36mbps ; MCS 4 */
119 IWL_DECLARE_RATE_INFO(48, 5, 36, 54), /* 48mbps ; MCS 5 */
120 IWL_DECLARE_RATE_INFO(54, 6, 48, INV), /* 54mbps ; MCS 6 */
121 IWL_DECLARE_MCS_RATE(7), /* MCS 7 */
122 IWL_DECLARE_MCS_RATE(8), /* MCS 8 */
123 IWL_DECLARE_MCS_RATE(9), /* MCS 9 */
126 static inline u8 rs_extract_rate(u32 rate_n_flags)
128 /* also works for HT because bits 7:6 are zero there */
129 return (u8)(rate_n_flags & RATE_LEGACY_RATE_MSK);
132 static int iwl_hwrate_to_plcp_idx(u32 rate_n_flags)
136 if (rate_n_flags & RATE_MCS_HT_MSK) {
137 idx = rate_n_flags & RATE_HT_MCS_RATE_CODE_MSK;
138 idx += IWL_RATE_MCS_0_INDEX;
140 /* skip 9M not supported in HT*/
141 if (idx >= IWL_RATE_9M_INDEX)
143 if ((idx >= IWL_FIRST_HT_RATE) && (idx <= IWL_LAST_HT_RATE))
145 } else if (rate_n_flags & RATE_MCS_VHT_MSK) {
146 idx = rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK;
147 idx += IWL_RATE_MCS_0_INDEX;
149 /* skip 9M not supported in VHT*/
150 if (idx >= IWL_RATE_9M_INDEX)
152 if ((idx >= IWL_FIRST_VHT_RATE) && (idx <= IWL_LAST_VHT_RATE))
155 /* legacy rate format, search for match in table */
157 u8 legacy_rate = rs_extract_rate(rate_n_flags);
158 for (idx = 0; idx < ARRAY_SIZE(iwl_rates); idx++)
159 if (iwl_rates[idx].plcp == legacy_rate)
166 static void rs_rate_scale_perform(struct iwl_mvm *mvm,
168 struct ieee80211_sta *sta,
169 struct iwl_lq_sta *lq_sta);
170 static void rs_fill_link_cmd(struct iwl_mvm *mvm,
171 struct ieee80211_sta *sta,
172 struct iwl_lq_sta *lq_sta, u32 rate_n_flags);
173 static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search);
174 static const char *rs_pretty_lq_type(enum iwl_table_type type);
175 static const char *rs_pretty_ant(u8 ant);
177 #ifdef CONFIG_MAC80211_DEBUGFS
178 static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
181 static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
187 * The following tables contain the expected throughput metrics for all rates
189 * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
191 * where invalid entries are zeros.
193 * CCK rates are only valid in legacy table and will only be used in G
197 static s32 expected_tpt_legacy[IWL_RATE_COUNT] = {
198 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 0, 0, 0
201 /* Expected TpT tables. 4 indexes:
202 * 0 - NGI, 1 - SGI, 2 - AGG+NGI, 3 - AGG+SGI
204 static s32 expected_tpt_siso_20MHz[4][IWL_RATE_COUNT] = {
205 {0, 0, 0, 0, 42, 0, 76, 102, 124, 159, 183, 193, 202, 216, 0},
206 {0, 0, 0, 0, 46, 0, 82, 110, 132, 168, 192, 202, 210, 225, 0},
207 {0, 0, 0, 0, 49, 0, 97, 145, 192, 285, 375, 420, 464, 551, 0},
208 {0, 0, 0, 0, 54, 0, 108, 160, 213, 315, 415, 465, 513, 608, 0},
211 static s32 expected_tpt_siso_40MHz[4][IWL_RATE_COUNT] = {
212 {0, 0, 0, 0, 77, 0, 127, 160, 184, 220, 242, 250, 257, 269, 275},
213 {0, 0, 0, 0, 83, 0, 135, 169, 193, 229, 250, 257, 264, 275, 280},
214 {0, 0, 0, 0, 101, 0, 199, 295, 389, 570, 744, 828, 911, 1070, 1173},
215 {0, 0, 0, 0, 112, 0, 220, 326, 429, 629, 819, 912, 1000, 1173, 1284},
218 static s32 expected_tpt_siso_80MHz[4][IWL_RATE_COUNT] = {
219 {0, 0, 0, 0, 130, 0, 191, 223, 244, 273, 288, 294, 298, 305, 308},
220 {0, 0, 0, 0, 138, 0, 200, 231, 251, 279, 293, 298, 302, 308, 312},
221 {0, 0, 0, 0, 217, 0, 429, 634, 834, 1220, 1585, 1760, 1931, 2258, 2466},
222 {0, 0, 0, 0, 241, 0, 475, 701, 921, 1343, 1741, 1931, 2117, 2468, 2691},
225 static s32 expected_tpt_mimo2_20MHz[4][IWL_RATE_COUNT] = {
226 {0, 0, 0, 0, 74, 0, 123, 155, 179, 213, 235, 243, 250, 261, 0},
227 {0, 0, 0, 0, 81, 0, 131, 164, 187, 221, 242, 250, 256, 267, 0},
228 {0, 0, 0, 0, 98, 0, 193, 286, 375, 550, 718, 799, 878, 1032, 0},
229 {0, 0, 0, 0, 109, 0, 214, 316, 414, 607, 790, 879, 965, 1132, 0},
232 static s32 expected_tpt_mimo2_40MHz[4][IWL_RATE_COUNT] = {
233 {0, 0, 0, 0, 123, 0, 182, 214, 235, 264, 279, 285, 289, 296, 300},
234 {0, 0, 0, 0, 131, 0, 191, 222, 242, 270, 284, 289, 293, 300, 303},
235 {0, 0, 0, 0, 200, 0, 390, 571, 741, 1067, 1365, 1505, 1640, 1894, 2053},
236 {0, 0, 0, 0, 221, 0, 430, 630, 816, 1169, 1490, 1641, 1784, 2053, 2221},
239 static s32 expected_tpt_mimo2_80MHz[4][IWL_RATE_COUNT] = {
240 {0, 0, 0, 0, 182, 0, 240, 264, 278, 299, 308, 311, 313, 317, 319},
241 {0, 0, 0, 0, 190, 0, 247, 269, 282, 302, 310, 313, 315, 319, 320},
242 {0, 0, 0, 0, 428, 0, 833, 1215, 1577, 2254, 2863, 3147, 3418, 3913, 4219},
243 {0, 0, 0, 0, 474, 0, 920, 1338, 1732, 2464, 3116, 3418, 3705, 4225, 4545},
247 static const struct iwl_rate_mcs_info iwl_rate_mcs[IWL_RATE_COUNT] = {
256 { "24", "16QAM 1/2"},
257 { "36", "16QAM 3/4"},
258 { "48", "64QAM 2/3"},
259 { "54", "64QAM 3/4"},
260 { "60", "64QAM 5/6"},
263 #define MCS_INDEX_PER_STREAM (8)
265 static inline void rs_dump_rate(struct iwl_mvm *mvm, const struct rs_rate *rate,
268 IWL_DEBUG_RATE(mvm, "%s: (%s: %d) ANT: %s BW: %d SGI: %d\n",
269 prefix, rs_pretty_lq_type(rate->type),
270 rate->index, rs_pretty_ant(rate->ant),
271 rate->bw, rate->sgi);
274 static void rs_rate_scale_clear_window(struct iwl_rate_scale_data *window)
277 window->success_counter = 0;
278 window->success_ratio = IWL_INVALID_VALUE;
280 window->average_tpt = IWL_INVALID_VALUE;
283 static inline u8 rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
285 return (ant_type & valid_antenna) == ant_type;
288 #ifdef CONFIG_MAC80211_DEBUGFS
290 * Program the device to use fixed rate for frame transmit
291 * This is for debugging/testing only
292 * once the device start use fixed rate, we need to reload the module
293 * to being back the normal operation.
295 static void rs_program_fix_rate(struct iwl_mvm *mvm,
296 struct iwl_lq_sta *lq_sta)
298 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
299 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
300 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
302 IWL_DEBUG_RATE(mvm, "sta_id %d rate 0x%X\n",
303 lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
305 if (lq_sta->dbg_fixed_rate) {
306 rs_fill_link_cmd(NULL, NULL, lq_sta, lq_sta->dbg_fixed_rate);
307 iwl_mvm_send_lq_cmd(lq_sta->drv, &lq_sta->lq, false);
312 static int rs_tl_turn_on_agg_for_tid(struct iwl_mvm *mvm,
313 struct iwl_lq_sta *lq_data, u8 tid,
314 struct ieee80211_sta *sta)
318 IWL_DEBUG_HT(mvm, "Starting Tx agg: STA: %pM tid: %d\n",
320 ret = ieee80211_start_tx_ba_session(sta, tid, 5000);
321 if (ret == -EAGAIN) {
323 * driver and mac80211 is out of sync
324 * this might be cause by reloading firmware
325 * stop the tx ba session here
327 IWL_ERR(mvm, "Fail start Tx agg on tid: %d\n",
329 ieee80211_stop_tx_ba_session(sta, tid);
334 static void rs_tl_turn_on_agg(struct iwl_mvm *mvm, u8 tid,
335 struct iwl_lq_sta *lq_data,
336 struct ieee80211_sta *sta)
338 if (tid < IWL_MAX_TID_COUNT)
339 rs_tl_turn_on_agg_for_tid(mvm, lq_data, tid, sta);
341 IWL_ERR(mvm, "tid exceeds max TID count: %d/%d\n",
342 tid, IWL_MAX_TID_COUNT);
345 static inline int get_num_of_ant_from_rate(u32 rate_n_flags)
347 return !!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
348 !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
349 !!(rate_n_flags & RATE_MCS_ANT_C_MSK);
353 * Static function to get the expected throughput from an iwl_scale_tbl_info
354 * that wraps a NULL pointer check
356 static s32 get_expected_tpt(struct iwl_scale_tbl_info *tbl, int rs_index)
358 if (tbl->expected_tpt)
359 return tbl->expected_tpt[rs_index];
364 * rs_collect_tx_data - Update the success/failure sliding window
366 * We keep a sliding window of the last 62 packets transmitted
367 * at this rate. window->data contains the bitmask of successful
370 static int rs_collect_tx_data(struct iwl_scale_tbl_info *tbl,
371 int scale_index, int attempts, int successes)
373 struct iwl_rate_scale_data *window = NULL;
374 static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
377 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
380 /* Select window for current tx bit rate */
381 window = &(tbl->win[scale_index]);
383 /* Get expected throughput */
384 tpt = get_expected_tpt(tbl, scale_index);
387 * Keep track of only the latest 62 tx frame attempts in this rate's
388 * history window; anything older isn't really relevant any more.
389 * If we have filled up the sliding window, drop the oldest attempt;
390 * if the oldest attempt (highest bit in bitmap) shows "success",
391 * subtract "1" from the success counter (this is the main reason
392 * we keep these bitmaps!).
394 while (attempts > 0) {
395 if (window->counter >= IWL_RATE_MAX_WINDOW) {
396 /* remove earliest */
397 window->counter = IWL_RATE_MAX_WINDOW - 1;
399 if (window->data & mask) {
400 window->data &= ~mask;
401 window->success_counter--;
405 /* Increment frames-attempted counter */
408 /* Shift bitmap by one frame to throw away oldest history */
411 /* Mark the most recent #successes attempts as successful */
413 window->success_counter++;
421 /* Calculate current success ratio, avoid divide-by-0! */
422 if (window->counter > 0)
423 window->success_ratio = 128 * (100 * window->success_counter)
426 window->success_ratio = IWL_INVALID_VALUE;
428 fail_count = window->counter - window->success_counter;
430 /* Calculate average throughput, if we have enough history. */
431 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
432 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
433 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
435 window->average_tpt = IWL_INVALID_VALUE;
440 /* Convert rs_rate object into ucode rate bitmask */
441 static u32 ucode_rate_from_rs_rate(struct iwl_mvm *mvm,
442 struct rs_rate *rate)
445 int index = rate->index;
447 ucode_rate |= ((rate->ant << RATE_MCS_ANT_POS) &
448 RATE_MCS_ANT_ABC_MSK);
450 if (is_legacy(rate)) {
451 ucode_rate |= iwl_rates[index].plcp;
452 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
453 ucode_rate |= RATE_MCS_CCK_MSK;
458 if (index < IWL_FIRST_HT_RATE || index > IWL_LAST_HT_RATE) {
459 IWL_ERR(mvm, "Invalid HT rate index %d\n", index);
460 index = IWL_LAST_HT_RATE;
462 ucode_rate |= RATE_MCS_HT_MSK;
464 if (is_ht_siso(rate))
465 ucode_rate |= iwl_rates[index].plcp_ht_siso;
466 else if (is_ht_mimo2(rate))
467 ucode_rate |= iwl_rates[index].plcp_ht_mimo2;
470 } else if (is_vht(rate)) {
471 if (index < IWL_FIRST_VHT_RATE || index > IWL_LAST_VHT_RATE) {
472 IWL_ERR(mvm, "Invalid VHT rate index %d\n", index);
473 index = IWL_LAST_VHT_RATE;
475 ucode_rate |= RATE_MCS_VHT_MSK;
476 if (is_vht_siso(rate))
477 ucode_rate |= iwl_rates[index].plcp_vht_siso;
478 else if (is_vht_mimo2(rate))
479 ucode_rate |= iwl_rates[index].plcp_vht_mimo2;
484 IWL_ERR(mvm, "Invalid rate->type %d\n", rate->type);
487 ucode_rate |= rate->bw;
489 ucode_rate |= RATE_MCS_SGI_MSK;
494 /* Convert a ucode rate into an rs_rate object */
495 static int rs_rate_from_ucode_rate(const u32 ucode_rate,
496 enum ieee80211_band band,
497 struct rs_rate *rate)
499 u32 ant_msk = ucode_rate & RATE_MCS_ANT_ABC_MSK;
500 u8 num_of_ant = get_num_of_ant_from_rate(ucode_rate);
503 memset(rate, 0, sizeof(struct rs_rate));
504 rate->index = iwl_hwrate_to_plcp_idx(ucode_rate);
506 if (rate->index == IWL_RATE_INVALID) {
511 rate->ant = (ant_msk >> RATE_MCS_ANT_POS);
514 if (!(ucode_rate & RATE_MCS_HT_MSK) &&
515 !(ucode_rate & RATE_MCS_VHT_MSK)) {
516 if (num_of_ant == 1) {
517 if (band == IEEE80211_BAND_5GHZ)
518 rate->type = LQ_LEGACY_A;
520 rate->type = LQ_LEGACY_G;
527 if (ucode_rate & RATE_MCS_SGI_MSK)
530 rate->bw = ucode_rate & RATE_MCS_CHAN_WIDTH_MSK;
532 if (ucode_rate & RATE_MCS_HT_MSK) {
533 nss = ((ucode_rate & RATE_HT_MCS_NSS_MSK) >>
534 RATE_HT_MCS_NSS_POS) + 1;
537 rate->type = LQ_HT_SISO;
538 WARN_ON_ONCE(num_of_ant != 1);
539 } else if (nss == 2) {
540 rate->type = LQ_HT_MIMO2;
541 WARN_ON_ONCE(num_of_ant != 2);
545 } else if (ucode_rate & RATE_MCS_VHT_MSK) {
546 nss = ((ucode_rate & RATE_VHT_MCS_NSS_MSK) >>
547 RATE_VHT_MCS_NSS_POS) + 1;
550 rate->type = LQ_VHT_SISO;
551 WARN_ON_ONCE(num_of_ant != 1);
552 } else if (nss == 2) {
553 rate->type = LQ_VHT_MIMO2;
554 WARN_ON_ONCE(num_of_ant != 2);
560 WARN_ON_ONCE(rate->bw == RATE_MCS_CHAN_WIDTH_160);
561 WARN_ON_ONCE(rate->bw == RATE_MCS_CHAN_WIDTH_80 &&
567 /* switch to another antenna/antennas and return 1 */
568 /* if no other valid antenna found, return 0 */
569 static int rs_toggle_antenna(u32 valid_ant, u32 *ucode_rate,
570 struct rs_rate *rate)
574 if (!rate->ant || rate->ant > ANT_ABC)
577 if (!rs_is_valid_ant(valid_ant, rate->ant))
580 new_ant_type = ant_toggle_lookup[rate->ant];
582 while ((new_ant_type != rate->ant) &&
583 !rs_is_valid_ant(valid_ant, new_ant_type))
584 new_ant_type = ant_toggle_lookup[new_ant_type];
586 if (new_ant_type == rate->ant)
589 rate->ant = new_ant_type;
591 /* TODO: get rid of ucode_rate here. This should handle only rs_rate */
592 *ucode_rate &= ~RATE_MCS_ANT_ABC_MSK;
593 *ucode_rate |= new_ant_type << RATE_MCS_ANT_POS;
597 static u16 rs_get_supported_rates(struct iwl_lq_sta *lq_sta,
598 struct rs_rate *rate)
601 return lq_sta->active_legacy_rate;
602 else if (is_siso(rate))
603 return lq_sta->active_siso_rate;
604 else if (is_mimo2(rate))
605 return lq_sta->active_mimo2_rate;
611 static u16 rs_get_adjacent_rate(struct iwl_mvm *mvm, u8 index, u16 rate_mask,
614 u8 high = IWL_RATE_INVALID;
615 u8 low = IWL_RATE_INVALID;
617 /* 802.11A or ht walks to the next literal adjacent rate in
619 if (is_type_a_band(rate_type) || !is_type_legacy(rate_type)) {
623 /* Find the previous rate that is in the rate mask */
625 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
626 if (rate_mask & mask) {
632 /* Find the next rate that is in the rate mask */
634 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
635 if (rate_mask & mask) {
641 return (high << 8) | low;
645 while (low != IWL_RATE_INVALID) {
646 low = iwl_rates[low].prev_rs;
647 if (low == IWL_RATE_INVALID)
649 if (rate_mask & (1 << low))
651 IWL_DEBUG_RATE(mvm, "Skipping masked lower rate: %d\n", low);
655 while (high != IWL_RATE_INVALID) {
656 high = iwl_rates[high].next_rs;
657 if (high == IWL_RATE_INVALID)
659 if (rate_mask & (1 << high))
661 IWL_DEBUG_RATE(mvm, "Skipping masked higher rate: %d\n", high);
664 return (high << 8) | low;
667 static u32 rs_get_lower_rate(struct iwl_lq_sta *lq_sta,
668 struct rs_rate *rate,
669 u8 scale_index, u8 ht_possible)
674 u8 switch_to_legacy = 0;
675 struct iwl_mvm *mvm = lq_sta->drv;
677 /* check if we need to switch from HT to legacy rates.
678 * assumption is that mandatory rates (1Mbps or 6Mbps)
679 * are always supported (spec demand) */
680 if (!is_legacy(rate) && (!ht_possible || !scale_index)) {
681 switch_to_legacy = 1;
682 WARN_ON_ONCE(scale_index < IWL_RATE_MCS_0_INDEX &&
683 scale_index > IWL_RATE_MCS_9_INDEX);
684 scale_index = rs_ht_to_legacy[scale_index];
685 if (lq_sta->band == IEEE80211_BAND_5GHZ)
686 rate->type = LQ_LEGACY_A;
688 rate->type = LQ_LEGACY_G;
690 if (num_of_ant(rate->ant) > 1)
692 first_antenna(iwl_fw_valid_tx_ant(mvm->fw));
694 rate->bw = RATE_MCS_CHAN_WIDTH_20;
698 rate_mask = rs_get_supported_rates(lq_sta, rate);
700 /* Mask with station rate restriction */
701 if (is_legacy(rate)) {
702 /* supp_rates has no CCK bits in A mode */
703 if (lq_sta->band == IEEE80211_BAND_5GHZ)
704 rate_mask = (u16)(rate_mask &
705 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
707 rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
710 /* If we switched from HT to legacy, check current rate */
711 if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
716 high_low = rs_get_adjacent_rate(lq_sta->drv, scale_index, rate_mask,
718 low = high_low & 0xff;
720 if (low == IWL_RATE_INVALID)
725 return ucode_rate_from_rs_rate(lq_sta->drv, rate);
728 /* Simple function to compare two rate scale table types */
729 static inline bool rs_rate_match(struct rs_rate *a,
732 return (a->type == b->type) && (a->ant == b->ant) && (a->sgi == b->sgi);
735 static u32 rs_ch_width_from_mac_flags(enum mac80211_rate_control_flags flags)
737 if (flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
738 return RATE_MCS_CHAN_WIDTH_40;
739 else if (flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
740 return RATE_MCS_CHAN_WIDTH_80;
741 else if (flags & IEEE80211_TX_RC_160_MHZ_WIDTH)
742 return RATE_MCS_CHAN_WIDTH_160;
744 return RATE_MCS_CHAN_WIDTH_20;
748 * mac80211 sends us Tx status
750 static void rs_tx_status(void *mvm_r, struct ieee80211_supported_band *sband,
751 struct ieee80211_sta *sta, void *priv_sta,
757 struct iwl_lq_sta *lq_sta = priv_sta;
758 struct iwl_lq_cmd *table;
759 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
760 struct iwl_op_mode *op_mode = (struct iwl_op_mode *)mvm_r;
761 struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
762 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
763 enum mac80211_rate_control_flags mac_flags;
766 struct iwl_scale_tbl_info *curr_tbl, *other_tbl, *tmp_tbl;
768 /* Treat uninitialized rate scaling data same as non-existing. */
770 IWL_DEBUG_RATE(mvm, "Station rate scaling not created yet.\n");
772 } else if (!lq_sta->drv) {
773 IWL_DEBUG_RATE(mvm, "Rate scaling not initialized yet.\n");
777 if (!ieee80211_is_data(hdr->frame_control) ||
778 info->flags & IEEE80211_TX_CTL_NO_ACK)
781 /* This packet was aggregated but doesn't carry status info */
782 if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
783 !(info->flags & IEEE80211_TX_STAT_AMPDU))
787 * Ignore this Tx frame response if its initial rate doesn't match
788 * that of latest Link Quality command. There may be stragglers
789 * from a previous Link Quality command, but we're no longer interested
790 * in those; they're either from the "active" mode while we're trying
791 * to check "search" mode, or a prior "search" mode after we've moved
792 * to a new "search" mode (which might become the new "active" mode).
795 ucode_rate = le32_to_cpu(table->rs_table[0]);
796 rs_rate_from_ucode_rate(ucode_rate, info->band, &rate);
797 if (info->band == IEEE80211_BAND_5GHZ)
798 rate.index -= IWL_FIRST_OFDM_RATE;
799 mac_flags = info->status.rates[0].flags;
800 mac_index = info->status.rates[0].idx;
801 /* For HT packets, map MCS to PLCP */
802 if (mac_flags & IEEE80211_TX_RC_MCS) {
803 /* Remove # of streams */
804 mac_index &= RATE_HT_MCS_RATE_CODE_MSK;
805 if (mac_index >= (IWL_RATE_9M_INDEX - IWL_FIRST_OFDM_RATE))
808 * mac80211 HT index is always zero-indexed; we need to move
809 * HT OFDM rates after CCK rates in 2.4 GHz band
811 if (info->band == IEEE80211_BAND_2GHZ)
812 mac_index += IWL_FIRST_OFDM_RATE;
813 } else if (mac_flags & IEEE80211_TX_RC_VHT_MCS) {
814 mac_index &= RATE_VHT_MCS_RATE_CODE_MSK;
815 if (mac_index >= (IWL_RATE_9M_INDEX - IWL_FIRST_OFDM_RATE))
819 /* Here we actually compare this rate to the latest LQ command */
820 if ((mac_index < 0) ||
821 (rate.sgi != !!(mac_flags & IEEE80211_TX_RC_SHORT_GI)) ||
822 (rate.bw != rs_ch_width_from_mac_flags(mac_flags)) ||
823 (rate.ant != info->status.antenna) ||
824 (!!(ucode_rate & RATE_MCS_HT_MSK) !=
825 !!(mac_flags & IEEE80211_TX_RC_MCS)) ||
826 (!!(ucode_rate & RATE_MCS_VHT_MSK) !=
827 !!(mac_flags & IEEE80211_TX_RC_VHT_MCS)) ||
828 (!!(ucode_rate & RATE_HT_MCS_GF_MSK) !=
829 !!(mac_flags & IEEE80211_TX_RC_GREEN_FIELD)) ||
830 (rate.index != mac_index)) {
832 "initial rate %d does not match %d (0x%x)\n",
833 mac_index, rate.index, ucode_rate);
835 * Since rates mis-match, the last LQ command may have failed.
836 * After IWL_MISSED_RATE_MAX mis-matches, resync the uCode with
839 lq_sta->missed_rate_counter++;
840 if (lq_sta->missed_rate_counter > IWL_MISSED_RATE_MAX) {
841 lq_sta->missed_rate_counter = 0;
842 iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, false);
844 /* Regardless, ignore this status info for outdated rate */
847 /* Rate did match, so reset the missed_rate_counter */
848 lq_sta->missed_rate_counter = 0;
850 /* Figure out if rate scale algorithm is in active or search table */
851 if (rs_rate_match(&rate,
852 &(lq_sta->lq_info[lq_sta->active_tbl].rate))) {
853 curr_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
854 other_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
855 } else if (rs_rate_match(&rate,
856 &lq_sta->lq_info[1 - lq_sta->active_tbl].rate)) {
857 curr_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
858 other_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
861 "Neither active nor search matches tx rate\n");
862 tmp_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
863 rs_dump_rate(mvm, &tmp_tbl->rate, "ACTIVE");
864 tmp_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
865 rs_dump_rate(mvm, &tmp_tbl->rate, "SEARCH");
866 rs_dump_rate(mvm, &rate, "ACTUAL");
869 * no matching table found, let's by-pass the data collection
870 * and continue to perform rate scale to find the rate table
872 rs_stay_in_table(lq_sta, true);
877 * Updating the frame history depends on whether packets were
880 * For aggregation, all packets were transmitted at the same rate, the
881 * first index into rate scale table.
883 if (info->flags & IEEE80211_TX_STAT_AMPDU) {
884 ucode_rate = le32_to_cpu(table->rs_table[0]);
885 rs_rate_from_ucode_rate(ucode_rate, info->band, &rate);
886 rs_collect_tx_data(curr_tbl, rate.index,
887 info->status.ampdu_len,
888 info->status.ampdu_ack_len);
890 /* Update success/fail counts if not searching for new mode */
891 if (lq_sta->stay_in_tbl) {
892 lq_sta->total_success += info->status.ampdu_ack_len;
893 lq_sta->total_failed += (info->status.ampdu_len -
894 info->status.ampdu_ack_len);
898 * For legacy, update frame history with for each Tx retry.
900 retries = info->status.rates[0].count - 1;
901 /* HW doesn't send more than 15 retries */
902 retries = min(retries, 15);
904 /* The last transmission may have been successful */
905 legacy_success = !!(info->flags & IEEE80211_TX_STAT_ACK);
906 /* Collect data for each rate used during failed TX attempts */
907 for (i = 0; i <= retries; ++i) {
908 ucode_rate = le32_to_cpu(table->rs_table[i]);
909 rs_rate_from_ucode_rate(ucode_rate, info->band, &rate);
911 * Only collect stats if retried rate is in the same RS
912 * table as active/search.
914 if (rs_rate_match(&rate, &curr_tbl->rate))
916 else if (rs_rate_match(&rate, &other_tbl->rate))
920 rs_collect_tx_data(tmp_tbl, rate.index, 1,
921 i < retries ? 0 : legacy_success);
924 /* Update success/fail counts if not searching for new mode */
925 if (lq_sta->stay_in_tbl) {
926 lq_sta->total_success += legacy_success;
927 lq_sta->total_failed += retries + (1 - legacy_success);
930 /* The last TX rate is cached in lq_sta; it's set in if/else above */
931 lq_sta->last_rate_n_flags = ucode_rate;
933 /* See if there's a better rate or modulation mode to try. */
934 if (sta && sta->supp_rates[sband->band])
935 rs_rate_scale_perform(mvm, skb, sta, lq_sta);
939 * Begin a period of staying with a selected modulation mode.
940 * Set "stay_in_tbl" flag to prevent any mode switches.
941 * Set frame tx success limits according to legacy vs. high-throughput,
942 * and reset overall (spanning all rates) tx success history statistics.
943 * These control how long we stay using same modulation mode before
944 * searching for a new mode.
946 static void rs_set_stay_in_table(struct iwl_mvm *mvm, u8 is_legacy,
947 struct iwl_lq_sta *lq_sta)
949 IWL_DEBUG_RATE(mvm, "we are staying in the same table\n");
950 lq_sta->stay_in_tbl = 1; /* only place this gets set */
952 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
953 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
954 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
956 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
957 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
958 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
960 lq_sta->table_count = 0;
961 lq_sta->total_failed = 0;
962 lq_sta->total_success = 0;
963 lq_sta->flush_timer = jiffies;
964 lq_sta->action_counter = 0;
968 * Find correct throughput table for given mode of modulation
970 static void rs_set_expected_tpt_table(struct iwl_lq_sta *lq_sta,
971 struct iwl_scale_tbl_info *tbl)
973 /* Used to choose among HT tables */
974 s32 (*ht_tbl_pointer)[IWL_RATE_COUNT];
975 struct rs_rate *rate = &tbl->rate;
977 /* Check for invalid LQ type */
978 if (WARN_ON_ONCE(!is_legacy(rate) && !is_ht(rate) &&
980 tbl->expected_tpt = expected_tpt_legacy;
984 /* Legacy rates have only one table */
985 if (is_legacy(rate)) {
986 tbl->expected_tpt = expected_tpt_legacy;
990 ht_tbl_pointer = expected_tpt_mimo2_20MHz;
991 /* Choose among many HT tables depending on number of streams
992 * (SISO/MIMO2), channel width (20/40/80), SGI, and aggregation
996 case RATE_MCS_CHAN_WIDTH_20:
997 ht_tbl_pointer = expected_tpt_siso_20MHz;
999 case RATE_MCS_CHAN_WIDTH_40:
1000 ht_tbl_pointer = expected_tpt_siso_40MHz;
1002 case RATE_MCS_CHAN_WIDTH_80:
1003 ht_tbl_pointer = expected_tpt_siso_80MHz;
1008 } else if (is_mimo2(rate)) {
1010 case RATE_MCS_CHAN_WIDTH_20:
1011 ht_tbl_pointer = expected_tpt_mimo2_20MHz;
1013 case RATE_MCS_CHAN_WIDTH_40:
1014 ht_tbl_pointer = expected_tpt_mimo2_40MHz;
1016 case RATE_MCS_CHAN_WIDTH_80:
1017 ht_tbl_pointer = expected_tpt_mimo2_80MHz;
1026 if (!rate->sgi && !lq_sta->is_agg) /* Normal */
1027 tbl->expected_tpt = ht_tbl_pointer[0];
1028 else if (rate->sgi && !lq_sta->is_agg) /* SGI */
1029 tbl->expected_tpt = ht_tbl_pointer[1];
1030 else if (!rate->sgi && lq_sta->is_agg) /* AGG */
1031 tbl->expected_tpt = ht_tbl_pointer[2];
1033 tbl->expected_tpt = ht_tbl_pointer[3];
1037 * Find starting rate for new "search" high-throughput mode of modulation.
1038 * Goal is to find lowest expected rate (under perfect conditions) that is
1039 * above the current measured throughput of "active" mode, to give new mode
1040 * a fair chance to prove itself without too many challenges.
1042 * This gets called when transitioning to more aggressive modulation
1043 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
1044 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1045 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1046 * bit rate will typically need to increase, but not if performance was bad.
1048 static s32 rs_get_best_rate(struct iwl_mvm *mvm,
1049 struct iwl_lq_sta *lq_sta,
1050 struct iwl_scale_tbl_info *tbl, /* "search" */
1051 u16 rate_mask, s8 index)
1053 /* "active" values */
1054 struct iwl_scale_tbl_info *active_tbl =
1055 &(lq_sta->lq_info[lq_sta->active_tbl]);
1056 s32 active_sr = active_tbl->win[index].success_ratio;
1057 s32 active_tpt = active_tbl->expected_tpt[index];
1059 /* expected "search" throughput */
1060 s32 *tpt_tbl = tbl->expected_tpt;
1062 s32 new_rate, high, low, start_hi;
1066 new_rate = high = low = start_hi = IWL_RATE_INVALID;
1069 high_low = rs_get_adjacent_rate(mvm, rate, rate_mask,
1072 low = high_low & 0xff;
1073 high = (high_low >> 8) & 0xff;
1076 * Lower the "search" bit rate, to give new "search" mode
1077 * approximately the same throughput as "active" if:
1079 * 1) "Active" mode has been working modestly well (but not
1080 * great), and expected "search" throughput (under perfect
1081 * conditions) at candidate rate is above the actual
1082 * measured "active" throughput (but less than expected
1083 * "active" throughput under perfect conditions).
1085 * 2) "Active" mode has been working perfectly or very well
1086 * and expected "search" throughput (under perfect
1087 * conditions) at candidate rate is above expected
1088 * "active" throughput (under perfect conditions).
1090 if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
1091 ((active_sr > RS_SR_FORCE_DECREASE) &&
1092 (active_sr <= IWL_RATE_HIGH_TH) &&
1093 (tpt_tbl[rate] <= active_tpt))) ||
1094 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
1095 (tpt_tbl[rate] > active_tpt))) {
1096 /* (2nd or later pass)
1097 * If we've already tried to raise the rate, and are
1098 * now trying to lower it, use the higher rate. */
1099 if (start_hi != IWL_RATE_INVALID) {
1100 new_rate = start_hi;
1106 /* Loop again with lower rate */
1107 if (low != IWL_RATE_INVALID)
1110 /* Lower rate not available, use the original */
1114 /* Else try to raise the "search" rate to match "active" */
1116 /* (2nd or later pass)
1117 * If we've already tried to lower the rate, and are
1118 * now trying to raise it, use the lower rate. */
1119 if (new_rate != IWL_RATE_INVALID)
1122 /* Loop again with higher rate */
1123 else if (high != IWL_RATE_INVALID) {
1127 /* Higher rate not available, use the original */
1138 /* Move to the next action and wrap around to the first action in case
1139 * we're at the last action. Assumes actions start at 0.
1141 static inline void rs_move_next_action(struct iwl_scale_tbl_info *tbl,
1144 BUILD_BUG_ON(IWL_LEGACY_FIRST_ACTION != 0);
1145 BUILD_BUG_ON(IWL_SISO_FIRST_ACTION != 0);
1146 BUILD_BUG_ON(IWL_MIMO2_FIRST_ACTION != 0);
1148 tbl->action = (tbl->action + 1) % (last_action + 1);
1151 static u32 rs_bw_from_sta_bw(struct ieee80211_sta *sta)
1153 if (sta->bandwidth >= IEEE80211_STA_RX_BW_80)
1154 return RATE_MCS_CHAN_WIDTH_80;
1155 else if (sta->bandwidth >= IEEE80211_STA_RX_BW_40)
1156 return RATE_MCS_CHAN_WIDTH_40;
1158 return RATE_MCS_CHAN_WIDTH_20;
1161 static bool rs_sgi_allowed(struct rs_rate *rate,
1162 struct ieee80211_sta *sta)
1164 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
1165 struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
1167 if (is_ht20(rate) && (ht_cap->cap &
1168 IEEE80211_HT_CAP_SGI_20))
1170 if (is_ht40(rate) && (ht_cap->cap &
1171 IEEE80211_HT_CAP_SGI_40))
1173 if (is_ht80(rate) && (vht_cap->cap &
1174 IEEE80211_VHT_CAP_SHORT_GI_80))
1181 * Set up search table for MIMO2
1183 static int rs_switch_to_mimo2(struct iwl_mvm *mvm,
1184 struct iwl_lq_sta *lq_sta,
1185 struct ieee80211_sta *sta,
1186 struct iwl_scale_tbl_info *tbl, int index)
1191 if (!sta->ht_cap.ht_supported)
1194 if (sta->smps_mode == IEEE80211_SMPS_STATIC)
1197 /* Need both Tx chains/antennas to support MIMO */
1198 if (num_of_ant(iwl_fw_valid_tx_ant(mvm->fw)) < 2)
1201 IWL_DEBUG_RATE(mvm, "LQ: try to switch to MIMO2\n");
1203 tbl->rate.type = lq_sta->is_vht ? LQ_VHT_MIMO2 : LQ_HT_MIMO2;
1205 tbl->max_search = IWL_MAX_SEARCH;
1206 rate_mask = lq_sta->active_mimo2_rate;
1208 tbl->rate.bw = rs_bw_from_sta_bw(sta);
1209 rs_set_expected_tpt_table(lq_sta, tbl);
1211 rate_idx = rs_get_best_rate(mvm, lq_sta, tbl, rate_mask, index);
1213 IWL_DEBUG_RATE(mvm, "LQ: MIMO2 best rate %d mask %X\n",
1214 rate_idx, rate_mask);
1215 if ((rate_idx == IWL_RATE_INVALID) || !((1 << rate_idx) & rate_mask)) {
1216 IWL_DEBUG_RATE(mvm, "Can't switch with index %d rate mask %x\n",
1217 rate_idx, rate_mask);
1221 tbl->rate.index = rate_idx;
1222 tbl->current_rate = ucode_rate_from_rs_rate(mvm, &tbl->rate);
1224 IWL_DEBUG_RATE(mvm, "LQ: Switch to new mcs %X index\n",
1230 * Set up search table for SISO
1232 static int rs_switch_to_siso(struct iwl_mvm *mvm,
1233 struct iwl_lq_sta *lq_sta,
1234 struct ieee80211_sta *sta,
1235 struct iwl_scale_tbl_info *tbl, int index)
1240 if (!sta->ht_cap.ht_supported)
1243 IWL_DEBUG_RATE(mvm, "LQ: try to switch to SISO\n");
1245 tbl->rate.type = lq_sta->is_vht ? LQ_VHT_SISO : LQ_HT_SISO;
1247 tbl->max_search = IWL_MAX_SEARCH;
1248 rate_mask = lq_sta->active_siso_rate;
1250 tbl->rate.bw = rs_bw_from_sta_bw(sta);
1251 rs_set_expected_tpt_table(lq_sta, tbl);
1252 rate_idx = rs_get_best_rate(mvm, lq_sta, tbl, rate_mask, index);
1254 IWL_DEBUG_RATE(mvm, "LQ: get best rate %d mask %X\n",
1255 rate_idx, rate_mask);
1256 if ((rate_idx == IWL_RATE_INVALID) || !((1 << rate_idx) & rate_mask)) {
1258 "can not switch with index %d rate mask %x\n",
1259 rate_idx, rate_mask);
1263 tbl->rate.index = rate_idx;
1264 tbl->current_rate = ucode_rate_from_rs_rate(mvm, &tbl->rate);
1266 IWL_DEBUG_RATE(mvm, "LQ: Switch to new mcs %X index\n",
1272 * Try to switch to new modulation mode from legacy
1274 static int rs_move_legacy_other(struct iwl_mvm *mvm,
1275 struct iwl_lq_sta *lq_sta,
1276 struct ieee80211_sta *sta,
1279 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1280 struct iwl_scale_tbl_info *search_tbl =
1281 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1282 struct rs_rate *rate = &search_tbl->rate;
1283 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1284 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1285 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1287 u8 valid_tx_ant = iwl_fw_valid_tx_ant(mvm->fw);
1288 u8 tx_chains_num = num_of_ant(valid_tx_ant);
1290 u8 update_search_tbl_counter = 0;
1292 start_action = tbl->action;
1294 lq_sta->action_counter++;
1295 switch (tbl->action) {
1296 case IWL_LEGACY_SWITCH_ANTENNA:
1297 IWL_DEBUG_RATE(mvm, "LQ: Legacy toggle Antenna\n");
1299 if (tx_chains_num <= 1)
1302 /* Don't change antenna if success has been great */
1303 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1306 /* Set up search table to try other antenna */
1307 memcpy(search_tbl, tbl, sz);
1309 if (rs_toggle_antenna(valid_tx_ant,
1310 &search_tbl->current_rate,
1311 &search_tbl->rate)) {
1312 update_search_tbl_counter = 1;
1313 rs_set_expected_tpt_table(lq_sta, search_tbl);
1317 case IWL_LEGACY_SWITCH_SISO:
1318 IWL_DEBUG_RATE(mvm, "LQ: Legacy switch to SISO\n");
1320 /* Set up search table to try SISO */
1321 memcpy(search_tbl, tbl, sz);
1323 ret = rs_switch_to_siso(mvm, lq_sta, sta,
1326 lq_sta->action_counter = 0;
1331 case IWL_LEGACY_SWITCH_MIMO2:
1332 IWL_DEBUG_RATE(mvm, "LQ: Legacy switch to MIMO2\n");
1334 /* Set up search table to try MIMO */
1335 memcpy(search_tbl, tbl, sz);
1339 if (!rs_is_valid_ant(valid_tx_ant,
1343 ret = rs_switch_to_mimo2(mvm, lq_sta, sta,
1346 lq_sta->action_counter = 0;
1353 rs_move_next_action(tbl, IWL_LEGACY_LAST_ACTION);
1355 if (tbl->action == start_action)
1358 rate->type = LQ_NONE;
1362 lq_sta->search_better_tbl = 1;
1363 rs_move_next_action(tbl, IWL_LEGACY_LAST_ACTION);
1364 if (update_search_tbl_counter)
1365 search_tbl->action = tbl->action;
1370 * Try to switch to new modulation mode from SISO
1372 static int rs_move_siso_to_other(struct iwl_mvm *mvm,
1373 struct iwl_lq_sta *lq_sta,
1374 struct ieee80211_sta *sta, int index)
1376 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1377 struct iwl_scale_tbl_info *search_tbl =
1378 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1379 struct rs_rate *rate = &search_tbl->rate;
1380 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1381 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1382 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1384 u8 valid_tx_ant = iwl_fw_valid_tx_ant(mvm->fw);
1385 u8 tx_chains_num = num_of_ant(valid_tx_ant);
1386 u8 update_search_tbl_counter = 0;
1389 if (tbl->action == IWL_SISO_SWITCH_MIMO2 &&
1390 !iwl_mvm_bt_coex_is_mimo_allowed(mvm, sta))
1391 tbl->action = IWL_SISO_SWITCH_ANTENNA;
1393 start_action = tbl->action;
1395 lq_sta->action_counter++;
1396 switch (tbl->action) {
1397 case IWL_SISO_SWITCH_ANTENNA:
1398 IWL_DEBUG_RATE(mvm, "LQ: SISO toggle Antenna\n");
1399 if (tx_chains_num <= 1)
1402 if (window->success_ratio >= IWL_RS_GOOD_RATIO &&
1403 BT_MBOX_MSG(&mvm->last_bt_notif, 3,
1407 memcpy(search_tbl, tbl, sz);
1408 if (rs_toggle_antenna(valid_tx_ant,
1409 &search_tbl->current_rate,
1411 update_search_tbl_counter = 1;
1415 case IWL_SISO_SWITCH_MIMO2:
1416 IWL_DEBUG_RATE(mvm, "LQ: SISO switch to MIMO2\n");
1417 memcpy(search_tbl, tbl, sz);
1421 if (!rs_is_valid_ant(valid_tx_ant,
1425 ret = rs_switch_to_mimo2(mvm, lq_sta, sta,
1430 case IWL_SISO_SWITCH_GI:
1431 if (!rs_sgi_allowed(rate, sta))
1434 IWL_DEBUG_RATE(mvm, "LQ: SISO toggle SGI/NGI\n");
1436 memcpy(search_tbl, tbl, sz);
1437 rate->sgi = !tbl->rate.sgi;
1438 rs_set_expected_tpt_table(lq_sta, search_tbl);
1439 if (tbl->rate.sgi) {
1440 s32 tpt = lq_sta->last_tpt / 100;
1441 if (tpt >= search_tbl->expected_tpt[index])
1444 rate->index = index;
1445 search_tbl->current_rate =
1446 ucode_rate_from_rs_rate(mvm, rate);
1447 update_search_tbl_counter = 1;
1452 rs_move_next_action(tbl, IWL_SISO_LAST_ACTION);
1454 if (tbl->action == start_action)
1457 rate->type = LQ_NONE;
1461 lq_sta->search_better_tbl = 1;
1462 rs_move_next_action(tbl, IWL_SISO_LAST_ACTION);
1463 if (update_search_tbl_counter)
1464 search_tbl->action = tbl->action;
1470 * Try to switch to new modulation mode from MIMO2
1472 static int rs_move_mimo2_to_other(struct iwl_mvm *mvm,
1473 struct iwl_lq_sta *lq_sta,
1474 struct ieee80211_sta *sta, int index)
1476 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1477 struct iwl_scale_tbl_info *search_tbl =
1478 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1479 struct rs_rate *rate = &search_tbl->rate;
1480 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1481 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1483 u8 valid_tx_ant = iwl_fw_valid_tx_ant(mvm->fw);
1484 u8 update_search_tbl_counter = 0;
1487 if (!iwl_mvm_bt_coex_is_mimo_allowed(mvm, sta)) {
1488 IWL_DEBUG_RATE(mvm, "BT COEX force switch to SISO A\n");
1489 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1492 start_action = tbl->action;
1494 lq_sta->action_counter++;
1495 switch (tbl->action) {
1496 case IWL_MIMO2_SWITCH_SISO_A:
1497 case IWL_MIMO2_SWITCH_SISO_B:
1498 IWL_DEBUG_RATE(mvm, "LQ: MIMO2 switch to SISO\n");
1500 /* Set up new search table for SISO */
1501 memcpy(search_tbl, tbl, sz);
1503 if (tbl->action == IWL_MIMO2_SWITCH_SISO_A)
1505 else /* tbl->action == IWL_MIMO2_SWITCH_SISO_B */
1508 if (!rs_is_valid_ant(valid_tx_ant,
1512 ret = rs_switch_to_siso(mvm, lq_sta, sta,
1519 case IWL_MIMO2_SWITCH_GI:
1520 if (!rs_sgi_allowed(rate, sta))
1523 IWL_DEBUG_RATE(mvm, "LQ: MIMO2 toggle SGI/NGI\n");
1525 /* Set up new search table for MIMO2 */
1526 memcpy(search_tbl, tbl, sz);
1527 rate->sgi = !tbl->rate.sgi;
1528 rs_set_expected_tpt_table(lq_sta, search_tbl);
1530 * If active table already uses the fastest possible
1531 * modulation (dual stream with short guard interval),
1532 * and it's working well, there's no need to look
1533 * for a better type of modulation!
1535 if (tbl->rate.sgi) {
1536 s32 tpt = lq_sta->last_tpt / 100;
1537 if (tpt >= search_tbl->expected_tpt[index])
1540 rate->index = index;
1541 search_tbl->current_rate =
1542 ucode_rate_from_rs_rate(mvm, rate);
1543 update_search_tbl_counter = 1;
1548 rs_move_next_action(tbl, IWL_MIMO2_LAST_ACTION);
1550 if (tbl->action == start_action)
1553 rate->type = LQ_NONE;
1556 lq_sta->search_better_tbl = 1;
1557 rs_move_next_action(tbl, IWL_MIMO2_LAST_ACTION);
1558 if (update_search_tbl_counter)
1559 search_tbl->action = tbl->action;
1565 * Check whether we should continue using same modulation mode, or
1566 * begin search for a new mode, based on:
1567 * 1) # tx successes or failures while using this mode
1568 * 2) # times calling this function
1569 * 3) elapsed time in this mode (not used, for now)
1571 static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search)
1573 struct iwl_scale_tbl_info *tbl;
1576 int flush_interval_passed = 0;
1577 struct iwl_mvm *mvm;
1580 active_tbl = lq_sta->active_tbl;
1582 tbl = &(lq_sta->lq_info[active_tbl]);
1584 /* If we've been disallowing search, see if we should now allow it */
1585 if (lq_sta->stay_in_tbl) {
1586 /* Elapsed time using current modulation mode */
1587 if (lq_sta->flush_timer)
1588 flush_interval_passed =
1590 (unsigned long)(lq_sta->flush_timer +
1591 RS_STAY_IN_COLUMN_TIMEOUT));
1594 * Check if we should allow search for new modulation mode.
1595 * If many frames have failed or succeeded, or we've used
1596 * this same modulation for a long time, allow search, and
1597 * reset history stats that keep track of whether we should
1598 * allow a new search. Also (below) reset all bitmaps and
1599 * stats in active history.
1602 (lq_sta->total_failed > lq_sta->max_failure_limit) ||
1603 (lq_sta->total_success > lq_sta->max_success_limit) ||
1604 ((!lq_sta->search_better_tbl) &&
1605 (lq_sta->flush_timer) && (flush_interval_passed))) {
1607 "LQ: stay is expired %d %d %d\n",
1608 lq_sta->total_failed,
1609 lq_sta->total_success,
1610 flush_interval_passed);
1612 /* Allow search for new mode */
1613 lq_sta->stay_in_tbl = 0; /* only place reset */
1614 lq_sta->total_failed = 0;
1615 lq_sta->total_success = 0;
1616 lq_sta->flush_timer = 0;
1618 * Else if we've used this modulation mode enough repetitions
1619 * (regardless of elapsed time or success/failure), reset
1620 * history bitmaps and rate-specific stats for all rates in
1624 lq_sta->table_count++;
1625 if (lq_sta->table_count >=
1626 lq_sta->table_count_limit) {
1627 lq_sta->table_count = 0;
1630 "LQ: stay in table clear win\n");
1631 for (i = 0; i < IWL_RATE_COUNT; i++)
1632 rs_rate_scale_clear_window(
1637 /* If transitioning to allow "search", reset all history
1638 * bitmaps and stats in active table (this will become the new
1639 * "search" table). */
1640 if (!lq_sta->stay_in_tbl) {
1641 for (i = 0; i < IWL_RATE_COUNT; i++)
1642 rs_rate_scale_clear_window(&(tbl->win[i]));
1648 * setup rate table in uCode
1650 static void rs_update_rate_tbl(struct iwl_mvm *mvm,
1651 struct ieee80211_sta *sta,
1652 struct iwl_lq_sta *lq_sta,
1653 struct rs_rate *rate)
1657 ucode_rate = ucode_rate_from_rs_rate(mvm, rate);
1658 rs_fill_link_cmd(mvm, sta, lq_sta, ucode_rate);
1659 iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, false);
1662 static u8 rs_get_tid(struct iwl_lq_sta *lq_data,
1663 struct ieee80211_hdr *hdr)
1665 u8 tid = IWL_MAX_TID_COUNT;
1667 if (ieee80211_is_data_qos(hdr->frame_control)) {
1668 u8 *qc = ieee80211_get_qos_ctl(hdr);
1672 if (unlikely(tid > IWL_MAX_TID_COUNT))
1673 tid = IWL_MAX_TID_COUNT;
1679 * Do rate scaling and search for new modulation mode.
1681 static void rs_rate_scale_perform(struct iwl_mvm *mvm,
1682 struct sk_buff *skb,
1683 struct ieee80211_sta *sta,
1684 struct iwl_lq_sta *lq_sta)
1686 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1687 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1688 int low = IWL_RATE_INVALID;
1689 int high = IWL_RATE_INVALID;
1692 struct iwl_rate_scale_data *window = NULL;
1693 int current_tpt = IWL_INVALID_VALUE;
1694 int low_tpt = IWL_INVALID_VALUE;
1695 int high_tpt = IWL_INVALID_VALUE;
1697 s8 scale_action = 0;
1700 struct iwl_scale_tbl_info *tbl, *tbl1;
1701 u16 rate_scale_index_msk = 0;
1706 u8 tid = IWL_MAX_TID_COUNT;
1707 u8 prev_agg = lq_sta->is_agg;
1708 struct iwl_mvm_sta *sta_priv = (void *)sta->drv_priv;
1709 struct iwl_mvm_tid_data *tid_data;
1710 struct rs_rate *rate;
1712 /* Send management frames and NO_ACK data using lowest rate. */
1713 /* TODO: this could probably be improved.. */
1714 if (!ieee80211_is_data(hdr->frame_control) ||
1715 info->flags & IEEE80211_TX_CTL_NO_ACK)
1718 lq_sta->supp_rates = sta->supp_rates[lq_sta->band];
1720 tid = rs_get_tid(lq_sta, hdr);
1721 if ((tid != IWL_MAX_TID_COUNT) &&
1722 (lq_sta->tx_agg_tid_en & (1 << tid))) {
1723 tid_data = &sta_priv->tid_data[tid];
1724 if (tid_data->state == IWL_AGG_OFF)
1733 * Select rate-scale / modulation-mode table to work with in
1734 * the rest of this function: "search" if searching for better
1735 * modulation mode, or "active" if doing rate scaling within a mode.
1737 if (!lq_sta->search_better_tbl)
1738 active_tbl = lq_sta->active_tbl;
1740 active_tbl = 1 - lq_sta->active_tbl;
1742 tbl = &(lq_sta->lq_info[active_tbl]);
1745 if (prev_agg != lq_sta->is_agg) {
1747 "Aggregation changed: prev %d current %d. Update expected TPT table\n",
1748 prev_agg, lq_sta->is_agg);
1749 rs_set_expected_tpt_table(lq_sta, tbl);
1752 /* current tx rate */
1753 index = lq_sta->last_txrate_idx;
1755 /* rates available for this association, and for modulation mode */
1756 rate_mask = rs_get_supported_rates(lq_sta, rate);
1758 /* mask with station rate restriction */
1759 if (is_legacy(rate)) {
1760 if (lq_sta->band == IEEE80211_BAND_5GHZ)
1761 /* supp_rates has no CCK bits in A mode */
1762 rate_scale_index_msk = (u16) (rate_mask &
1763 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
1765 rate_scale_index_msk = (u16) (rate_mask &
1766 lq_sta->supp_rates);
1769 rate_scale_index_msk = rate_mask;
1772 if (!rate_scale_index_msk)
1773 rate_scale_index_msk = rate_mask;
1775 if (!((1 << index) & rate_scale_index_msk)) {
1776 IWL_ERR(mvm, "Current Rate is not valid\n");
1777 if (lq_sta->search_better_tbl) {
1778 /* revert to active table if search table is not valid*/
1779 rate->type = LQ_NONE;
1780 lq_sta->search_better_tbl = 0;
1781 tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1782 /* get "active" rate info */
1783 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
1784 tbl->rate.index = index;
1785 rs_update_rate_tbl(mvm, sta, lq_sta, &tbl->rate);
1790 /* Get expected throughput table and history window for current rate */
1791 if (!tbl->expected_tpt) {
1792 IWL_ERR(mvm, "tbl->expected_tpt is NULL\n");
1796 /* force user max rate if set by user */
1797 if ((lq_sta->max_rate_idx != -1) &&
1798 (lq_sta->max_rate_idx < index)) {
1799 index = lq_sta->max_rate_idx;
1801 window = &(tbl->win[index]);
1803 "Forcing user max rate %d\n",
1808 window = &(tbl->win[index]);
1811 * If there is not enough history to calculate actual average
1812 * throughput, keep analyzing results of more tx frames, without
1813 * changing rate or mode (bypass most of the rest of this function).
1814 * Set up new rate table in uCode only if old rate is not supported
1815 * in current association (use new rate found above).
1817 fail_count = window->counter - window->success_counter;
1818 if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
1819 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
1821 "(%s: %d): Test Window: succ %d total %d\n",
1822 rs_pretty_lq_type(rate->type),
1823 index, window->success_counter, window->counter);
1825 /* Can't calculate this yet; not enough history */
1826 window->average_tpt = IWL_INVALID_VALUE;
1828 /* Should we stay with this modulation mode,
1829 * or search for a new one? */
1830 rs_stay_in_table(lq_sta, false);
1834 /* Else we have enough samples; calculate estimate of
1835 * actual average throughput */
1836 if (window->average_tpt != ((window->success_ratio *
1837 tbl->expected_tpt[index] + 64) / 128)) {
1838 window->average_tpt = ((window->success_ratio *
1839 tbl->expected_tpt[index] + 64) / 128);
1842 /* If we are searching for better modulation mode, check success. */
1843 if (lq_sta->search_better_tbl) {
1844 /* If good success, continue using the "search" mode;
1845 * no need to send new link quality command, since we're
1846 * continuing to use the setup that we've been trying. */
1847 if (window->average_tpt > lq_sta->last_tpt) {
1849 "SWITCHING TO NEW TABLE SR: %d "
1850 "cur-tpt %d old-tpt %d\n",
1851 window->success_ratio,
1852 window->average_tpt,
1855 if (!is_legacy(rate))
1856 lq_sta->enable_counter = 1;
1858 /* Swap tables; "search" becomes "active" */
1859 lq_sta->active_tbl = active_tbl;
1860 current_tpt = window->average_tpt;
1861 /* Else poor success; go back to mode in "active" table */
1864 "GOING BACK TO THE OLD TABLE: SR %d "
1865 "cur-tpt %d old-tpt %d\n",
1866 window->success_ratio,
1867 window->average_tpt,
1870 /* Nullify "search" table */
1871 rate->type = LQ_NONE;
1873 /* Revert to "active" table */
1874 active_tbl = lq_sta->active_tbl;
1875 tbl = &(lq_sta->lq_info[active_tbl]);
1877 /* Revert to "active" rate and throughput info */
1878 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
1879 current_tpt = lq_sta->last_tpt;
1881 /* Need to set up a new rate table in uCode */
1885 /* Either way, we've made a decision; modulation mode
1886 * search is done, allow rate adjustment next time. */
1887 lq_sta->search_better_tbl = 0;
1888 done_search = 1; /* Don't switch modes below! */
1892 /* (Else) not in search of better modulation mode, try for better
1893 * starting rate, while staying in this mode. */
1894 high_low = rs_get_adjacent_rate(mvm, index, rate_scale_index_msk,
1896 low = high_low & 0xff;
1897 high = (high_low >> 8) & 0xff;
1899 /* If user set max rate, dont allow higher than user constrain */
1900 if ((lq_sta->max_rate_idx != -1) &&
1901 (lq_sta->max_rate_idx < high))
1902 high = IWL_RATE_INVALID;
1904 sr = window->success_ratio;
1906 /* Collect measured throughputs for current and adjacent rates */
1907 current_tpt = window->average_tpt;
1908 if (low != IWL_RATE_INVALID)
1909 low_tpt = tbl->win[low].average_tpt;
1910 if (high != IWL_RATE_INVALID)
1911 high_tpt = tbl->win[high].average_tpt;
1914 "(%s: %d): cur_tpt %d SR %d low %d high %d low_tpt %d high_tpt %d\n",
1915 rs_pretty_lq_type(rate->type), index, current_tpt, sr,
1916 low, high, low_tpt, high_tpt);
1920 /* Too many failures, decrease rate */
1921 if ((sr <= RS_SR_FORCE_DECREASE) || (current_tpt == 0)) {
1923 "decrease rate because of low SR\n");
1925 /* No throughput measured yet for adjacent rates; try increase. */
1926 } else if ((low_tpt == IWL_INVALID_VALUE) &&
1927 (high_tpt == IWL_INVALID_VALUE)) {
1928 if (high != IWL_RATE_INVALID && sr >= IWL_RATE_INCREASE_TH) {
1930 "Good SR and no high rate measurement. "
1933 } else if (low != IWL_RATE_INVALID) {
1935 "Remain in current rate\n");
1940 /* Both adjacent throughputs are measured, but neither one has better
1941 * throughput; we're using the best rate, don't change it! */
1942 else if ((low_tpt != IWL_INVALID_VALUE) &&
1943 (high_tpt != IWL_INVALID_VALUE) &&
1944 (low_tpt < current_tpt) &&
1945 (high_tpt < current_tpt)) {
1947 "Both high and low are worse. "
1952 /* At least one adjacent rate's throughput is measured,
1953 * and may have better performance. */
1955 /* Higher adjacent rate's throughput is measured */
1956 if (high_tpt != IWL_INVALID_VALUE) {
1957 /* Higher rate has better throughput */
1958 if (high_tpt > current_tpt &&
1959 sr >= IWL_RATE_INCREASE_TH) {
1961 "Higher rate is better and good "
1962 "SR. Increate rate\n");
1966 "Higher rate isn't better OR "
1967 "no good SR. Maintain rate\n");
1971 /* Lower adjacent rate's throughput is measured */
1972 } else if (low_tpt != IWL_INVALID_VALUE) {
1973 /* Lower rate has better throughput */
1974 if (low_tpt > current_tpt) {
1976 "Lower rate is better. "
1979 } else if (sr >= IWL_RATE_INCREASE_TH) {
1981 "Lower rate isn't better and "
1982 "good SR. Increase rate\n");
1988 /* Sanity check; asked for decrease, but success rate or throughput
1989 * has been good at old rate. Don't change it. */
1990 if ((scale_action == -1) && (low != IWL_RATE_INVALID) &&
1991 ((sr > IWL_RATE_HIGH_TH) ||
1992 (current_tpt > (100 * tbl->expected_tpt[low])))) {
1994 "Sanity check failed. Maintain rate\n");
1998 /* Force a search in case BT doesn't like us being in MIMO */
1999 if (is_mimo(rate) &&
2000 !iwl_mvm_bt_coex_is_mimo_allowed(mvm, sta)) {
2002 "BT Coex forbids MIMO. Search for new config\n");
2003 rs_stay_in_table(lq_sta, true);
2007 switch (scale_action) {
2009 /* Decrease starting rate, update uCode's rate table */
2010 if (low != IWL_RATE_INVALID) {
2015 "At the bottom rate. Can't decrease\n");
2020 /* Increase starting rate, update uCode's rate table */
2021 if (high != IWL_RATE_INVALID) {
2026 "At the top rate. Can't increase\n");
2037 /* Replace uCode's rate table for the destination station. */
2039 tbl->rate.index = index;
2040 rs_update_rate_tbl(mvm, sta, lq_sta, &tbl->rate);
2043 rs_stay_in_table(lq_sta, false);
2046 * Search for new modulation mode if we're:
2047 * 1) Not changing rates right now
2048 * 2) Not just finishing up a search
2049 * 3) Allowing a new search
2051 if (!update_lq && !done_search &&
2052 !lq_sta->stay_in_tbl && window->counter) {
2053 /* Save current throughput to compare with "search" throughput*/
2054 lq_sta->last_tpt = current_tpt;
2057 "Start Search: update_lq %d done_search %d stay_in_tbl %d win->counter %d\n",
2058 update_lq, done_search, lq_sta->stay_in_tbl,
2060 /* Select a new "search" modulation mode to try.
2061 * If one is found, set up the new "search" table. */
2062 if (is_legacy(&tbl->rate))
2063 rs_move_legacy_other(mvm, lq_sta, sta, index);
2064 else if (is_siso(&tbl->rate))
2065 rs_move_siso_to_other(mvm, lq_sta, sta, index);
2066 else if (is_mimo2(&tbl->rate))
2067 rs_move_mimo2_to_other(mvm, lq_sta, sta, index);
2071 /* If new "search" mode was selected, set up in uCode table */
2072 if (lq_sta->search_better_tbl) {
2073 /* Access the "search" table, clear its history. */
2074 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
2075 for (i = 0; i < IWL_RATE_COUNT; i++)
2076 rs_rate_scale_clear_window(&(tbl->win[i]));
2078 /* Use new "search" start rate */
2079 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
2082 "Switch to SEARCH TABLE: "
2083 "mcs %X (%s: %d)\n",
2085 rs_pretty_lq_type(tbl->rate.type),
2087 rs_fill_link_cmd(mvm, sta, lq_sta, tbl->current_rate);
2088 iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, false);
2094 if (done_search && !lq_sta->stay_in_tbl) {
2095 /* If the "active" (non-search) mode was legacy,
2096 * and we've tried switching antennas,
2097 * but we haven't been able to try HT modes (not available),
2098 * stay with best antenna legacy modulation for a while
2099 * before next round of mode comparisons. */
2100 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
2101 if (is_legacy(&tbl1->rate) && !sta->ht_cap.ht_supported &&
2102 lq_sta->action_counter > tbl1->max_search) {
2103 IWL_DEBUG_RATE(mvm, "LQ: STAY in legacy table\n");
2104 rs_set_stay_in_table(mvm, 1, lq_sta);
2107 /* If we're in an HT mode, and all 3 mode switch actions
2108 * have been tried and compared, stay in this best modulation
2109 * mode for a while before next round of mode comparisons. */
2110 if (lq_sta->enable_counter &&
2111 (lq_sta->action_counter >= tbl1->max_search)) {
2112 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2113 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
2114 (tid != IWL_MAX_TID_COUNT)) {
2115 tid_data = &sta_priv->tid_data[tid];
2116 if (tid_data->state == IWL_AGG_OFF) {
2118 "try to aggregate tid %d\n",
2120 rs_tl_turn_on_agg(mvm, tid,
2124 rs_set_stay_in_table(mvm, 0, lq_sta);
2129 tbl->rate.index = index;
2130 tbl->current_rate = ucode_rate_from_rs_rate(mvm, &tbl->rate);
2131 lq_sta->last_txrate_idx = index;
2135 * rs_initialize_lq - Initialize a station's hardware rate table
2137 * The uCode's station table contains a table of fallback rates
2138 * for automatic fallback during transmission.
2140 * NOTE: This sets up a default set of values. These will be replaced later
2141 * if the driver's iwl-agn-rs rate scaling algorithm is used, instead of
2144 * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
2145 * calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
2146 * which requires station table entry to exist).
2148 static void rs_initialize_lq(struct iwl_mvm *mvm,
2149 struct ieee80211_sta *sta,
2150 struct iwl_lq_sta *lq_sta,
2151 enum ieee80211_band band,
2154 struct iwl_scale_tbl_info *tbl;
2155 struct rs_rate *rate;
2161 if (!sta || !lq_sta)
2164 i = lq_sta->last_txrate_idx;
2166 valid_tx_ant = iwl_fw_valid_tx_ant(mvm->fw);
2168 if (!lq_sta->search_better_tbl)
2169 active_tbl = lq_sta->active_tbl;
2171 active_tbl = 1 - lq_sta->active_tbl;
2173 tbl = &(lq_sta->lq_info[active_tbl]);
2176 if ((i < 0) || (i >= IWL_RATE_COUNT))
2179 ucode_rate = iwl_rates[i].plcp;
2180 rate->ant = first_antenna(valid_tx_ant);
2181 ucode_rate |= rate->ant << RATE_MCS_ANT_POS;
2183 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
2184 ucode_rate |= RATE_MCS_CCK_MSK;
2186 rs_rate_from_ucode_rate(ucode_rate, band, rate);
2187 if (!rs_is_valid_ant(valid_tx_ant, rate->ant))
2188 rs_toggle_antenna(valid_tx_ant, &ucode_rate, rate);
2190 ucode_rate = ucode_rate_from_rs_rate(mvm, rate);
2191 tbl->current_rate = ucode_rate;
2192 rs_set_expected_tpt_table(lq_sta, tbl);
2193 rs_fill_link_cmd(NULL, NULL, lq_sta, ucode_rate);
2194 /* TODO restore station should remember the lq cmd */
2195 iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, init);
2198 static void rs_get_rate(void *mvm_r, struct ieee80211_sta *sta, void *mvm_sta,
2199 struct ieee80211_tx_rate_control *txrc)
2201 struct sk_buff *skb = txrc->skb;
2202 struct ieee80211_supported_band *sband = txrc->sband;
2203 struct iwl_op_mode *op_mode __maybe_unused =
2204 (struct iwl_op_mode *)mvm_r;
2205 struct iwl_mvm *mvm __maybe_unused = IWL_OP_MODE_GET_MVM(op_mode);
2206 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2207 struct iwl_lq_sta *lq_sta = mvm_sta;
2209 /* Get max rate if user set max rate */
2211 lq_sta->max_rate_idx = txrc->max_rate_idx;
2212 if ((sband->band == IEEE80211_BAND_5GHZ) &&
2213 (lq_sta->max_rate_idx != -1))
2214 lq_sta->max_rate_idx += IWL_FIRST_OFDM_RATE;
2215 if ((lq_sta->max_rate_idx < 0) ||
2216 (lq_sta->max_rate_idx >= IWL_RATE_COUNT))
2217 lq_sta->max_rate_idx = -1;
2220 /* Treat uninitialized rate scaling data same as non-existing. */
2221 if (lq_sta && !lq_sta->drv) {
2222 IWL_DEBUG_RATE(mvm, "Rate scaling not initialized yet.\n");
2226 /* Send management frames and NO_ACK data using lowest rate. */
2227 if (rate_control_send_low(sta, mvm_sta, txrc))
2230 iwl_mvm_hwrate_to_tx_rate(lq_sta->last_rate_n_flags,
2231 info->band, &info->control.rates[0]);
2233 info->control.rates[0].count = 1;
2236 static void *rs_alloc_sta(void *mvm_rate, struct ieee80211_sta *sta,
2239 struct iwl_mvm_sta *sta_priv = (struct iwl_mvm_sta *)sta->drv_priv;
2240 struct iwl_op_mode *op_mode __maybe_unused =
2241 (struct iwl_op_mode *)mvm_rate;
2242 struct iwl_mvm *mvm __maybe_unused = IWL_OP_MODE_GET_MVM(op_mode);
2244 IWL_DEBUG_RATE(mvm, "create station rate scale window\n");
2246 return &sta_priv->lq_sta;
2249 static int rs_vht_highest_rx_mcs_index(struct ieee80211_sta_vht_cap *vht_cap,
2252 u16 rx_mcs = le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map) &
2253 (0x3 << (2 * (nss - 1)));
2254 rx_mcs >>= (2 * (nss - 1));
2256 if (rx_mcs == IEEE80211_VHT_MCS_SUPPORT_0_7)
2257 return IWL_RATE_MCS_7_INDEX;
2258 else if (rx_mcs == IEEE80211_VHT_MCS_SUPPORT_0_8)
2259 return IWL_RATE_MCS_8_INDEX;
2260 else if (rx_mcs == IEEE80211_VHT_MCS_SUPPORT_0_9)
2261 return IWL_RATE_MCS_9_INDEX;
2263 WARN_ON_ONCE(rx_mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED);
2267 static void rs_vht_set_enabled_rates(struct ieee80211_sta *sta,
2268 struct ieee80211_sta_vht_cap *vht_cap,
2269 struct iwl_lq_sta *lq_sta)
2272 int highest_mcs = rs_vht_highest_rx_mcs_index(vht_cap, 1);
2274 if (highest_mcs >= IWL_RATE_MCS_0_INDEX) {
2275 for (i = IWL_RATE_MCS_0_INDEX; i <= highest_mcs; i++) {
2276 if (i == IWL_RATE_9M_INDEX)
2279 /* VHT MCS9 isn't valid for 20Mhz for NSS=1,2 */
2280 if (i == IWL_RATE_MCS_9_INDEX &&
2281 sta->bandwidth == IEEE80211_STA_RX_BW_20)
2284 lq_sta->active_siso_rate |= BIT(i);
2288 if (sta->rx_nss < 2)
2291 highest_mcs = rs_vht_highest_rx_mcs_index(vht_cap, 2);
2292 if (highest_mcs >= IWL_RATE_MCS_0_INDEX) {
2293 for (i = IWL_RATE_MCS_0_INDEX; i <= highest_mcs; i++) {
2294 if (i == IWL_RATE_9M_INDEX)
2297 /* VHT MCS9 isn't valid for 20Mhz for NSS=1,2 */
2298 if (i == IWL_RATE_MCS_9_INDEX &&
2299 sta->bandwidth == IEEE80211_STA_RX_BW_20)
2302 lq_sta->active_mimo2_rate |= BIT(i);
2308 * Called after adding a new station to initialize rate scaling
2310 void iwl_mvm_rs_rate_init(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2311 enum ieee80211_band band, bool init)
2314 struct ieee80211_hw *hw = mvm->hw;
2315 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
2316 struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
2317 struct iwl_mvm_sta *sta_priv;
2318 struct iwl_lq_sta *lq_sta;
2319 struct ieee80211_supported_band *sband;
2320 unsigned long supp; /* must be unsigned long for for_each_set_bit */
2322 sta_priv = (struct iwl_mvm_sta *)sta->drv_priv;
2323 lq_sta = &sta_priv->lq_sta;
2324 memset(lq_sta, 0, sizeof(*lq_sta));
2326 sband = hw->wiphy->bands[band];
2328 lq_sta->lq.sta_id = sta_priv->sta_id;
2330 for (j = 0; j < LQ_SIZE; j++)
2331 for (i = 0; i < IWL_RATE_COUNT; i++)
2332 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
2334 lq_sta->flush_timer = 0;
2335 lq_sta->supp_rates = sta->supp_rates[sband->band];
2338 "LQ: *** rate scale station global init for station %d ***\n",
2340 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2341 * the lowest or the highest rate.. Could consider using RSSI from
2342 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2345 lq_sta->max_rate_idx = -1;
2346 lq_sta->missed_rate_counter = IWL_MISSED_RATE_MAX;
2347 lq_sta->band = sband->band;
2349 * active legacy rates as per supported rates bitmap
2351 supp = sta->supp_rates[sband->band];
2352 lq_sta->active_legacy_rate = 0;
2353 for_each_set_bit(i, &supp, BITS_PER_LONG)
2354 lq_sta->active_legacy_rate |= BIT(sband->bitrates[i].hw_value);
2356 /* TODO: should probably account for rx_highest for both HT/VHT */
2357 if (!vht_cap || !vht_cap->vht_supported) {
2358 /* active_siso_rate mask includes 9 MBits (bit 5),
2359 * and CCK (bits 0-3), supp_rates[] does not;
2360 * shift to convert format, force 9 MBits off.
2362 lq_sta->active_siso_rate = ht_cap->mcs.rx_mask[0] << 1;
2363 lq_sta->active_siso_rate |= ht_cap->mcs.rx_mask[0] & 0x1;
2364 lq_sta->active_siso_rate &= ~((u16)0x2);
2365 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
2368 lq_sta->active_mimo2_rate = ht_cap->mcs.rx_mask[1] << 1;
2369 lq_sta->active_mimo2_rate |= ht_cap->mcs.rx_mask[1] & 0x1;
2370 lq_sta->active_mimo2_rate &= ~((u16)0x2);
2371 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2373 lq_sta->is_vht = false;
2375 rs_vht_set_enabled_rates(sta, vht_cap, lq_sta);
2376 lq_sta->is_vht = true;
2380 "SISO-RATE=%X MIMO2-RATE=%X VHT=%d\n",
2381 lq_sta->active_siso_rate,
2382 lq_sta->active_mimo2_rate,
2385 /* These values will be overridden later */
2386 lq_sta->lq.single_stream_ant_msk =
2387 first_antenna(iwl_fw_valid_tx_ant(mvm->fw));
2388 lq_sta->lq.dual_stream_ant_msk = ANT_AB;
2390 /* as default allow aggregation for all tids */
2391 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
2394 /* Set last_txrate_idx to lowest rate */
2395 lq_sta->last_txrate_idx = rate_lowest_index(sband, sta);
2396 if (sband->band == IEEE80211_BAND_5GHZ)
2397 lq_sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
2399 #ifdef CONFIG_MAC80211_DEBUGFS
2400 lq_sta->dbg_fixed_rate = 0;
2403 rs_initialize_lq(mvm, sta, lq_sta, band, init);
2406 static void rs_rate_update(void *mvm_r,
2407 struct ieee80211_supported_band *sband,
2408 struct cfg80211_chan_def *chandef,
2409 struct ieee80211_sta *sta, void *priv_sta,
2413 struct iwl_op_mode *op_mode =
2414 (struct iwl_op_mode *)mvm_r;
2415 struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
2417 /* Stop any ongoing aggregations as rs starts off assuming no agg */
2418 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++)
2419 ieee80211_stop_tx_ba_session(sta, tid);
2421 iwl_mvm_rs_rate_init(mvm, sta, sband->band, false);
2424 static void rs_fill_link_cmd(struct iwl_mvm *mvm,
2425 struct ieee80211_sta *sta,
2426 struct iwl_lq_sta *lq_sta, u32 new_rate)
2428 struct rs_rate rate;
2430 int repeat_rate = 0;
2431 u8 ant_toggle_cnt = 0;
2432 u8 use_ht_possible = 1;
2433 u8 valid_tx_ant = 0;
2434 struct iwl_lq_cmd *lq_cmd = &lq_sta->lq;
2436 /* Override starting rate (index 0) if needed for debug purposes */
2437 rs_dbgfs_set_mcs(lq_sta, &new_rate);
2439 rs_rate_from_ucode_rate(new_rate, lq_sta->band, &rate);
2441 /* How many times should we repeat the initial rate? */
2442 if (is_legacy(&rate)) {
2444 repeat_rate = IWL_NUMBER_TRY;
2446 repeat_rate = min(IWL_HT_NUMBER_TRY,
2447 LINK_QUAL_AGG_DISABLE_START_DEF - 1);
2450 lq_cmd->mimo_delim = is_mimo(&rate) ? 1 : 0;
2452 /* Fill 1st table entry (index 0) */
2453 lq_cmd->rs_table[index] = cpu_to_le32(new_rate);
2455 if (num_of_ant(rate.ant) == 1)
2456 lq_cmd->single_stream_ant_msk = rate.ant;
2457 /* otherwise we don't modify the existing value */
2462 valid_tx_ant = iwl_fw_valid_tx_ant(mvm->fw);
2464 /* Fill rest of rate table */
2465 while (index < LINK_QUAL_MAX_RETRY_NUM) {
2466 /* Repeat initial/next rate.
2467 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2468 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
2469 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
2470 if (is_legacy(&rate)) {
2471 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2474 rs_toggle_antenna(valid_tx_ant,
2479 /* Override next rate if needed for debug purposes */
2480 rs_dbgfs_set_mcs(lq_sta, &new_rate);
2482 /* Fill next table entry */
2483 lq_cmd->rs_table[index] =
2484 cpu_to_le32(new_rate);
2489 rs_rate_from_ucode_rate(new_rate, lq_sta->band, &rate);
2491 /* Indicate to uCode which entries might be MIMO.
2492 * If initial rate was MIMO, this will finally end up
2493 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
2495 lq_cmd->mimo_delim = index;
2498 new_rate = rs_get_lower_rate(lq_sta, &rate, rate.index,
2501 /* How many times should we repeat the next rate? */
2502 if (is_legacy(&rate)) {
2503 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2506 rs_toggle_antenna(valid_tx_ant,
2510 repeat_rate = IWL_NUMBER_TRY;
2512 repeat_rate = IWL_HT_NUMBER_TRY;
2515 /* Don't allow HT rates after next pass.
2516 * rs_get_lower_rate() will change type to LQ_LEGACY_A
2519 use_ht_possible = 0;
2521 /* Override next rate if needed for debug purposes */
2522 rs_dbgfs_set_mcs(lq_sta, &new_rate);
2524 /* Fill next table entry */
2525 lq_cmd->rs_table[index] = cpu_to_le32(new_rate);
2531 lq_cmd->agg_frame_cnt_limit = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
2532 lq_cmd->agg_disable_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
2534 lq_cmd->agg_time_limit =
2535 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
2538 lq_cmd->agg_time_limit =
2539 cpu_to_le16(iwl_mvm_bt_coex_agg_time_limit(mvm, sta));
2542 static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
2546 /* rate scale requires free function to be implemented */
2547 static void rs_free(void *mvm_rate)
2552 static void rs_free_sta(void *mvm_r, struct ieee80211_sta *sta,
2555 struct iwl_op_mode *op_mode __maybe_unused = mvm_r;
2556 struct iwl_mvm *mvm __maybe_unused = IWL_OP_MODE_GET_MVM(op_mode);
2558 IWL_DEBUG_RATE(mvm, "enter\n");
2559 IWL_DEBUG_RATE(mvm, "leave\n");
2562 #ifdef CONFIG_MAC80211_DEBUGFS
2563 static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
2566 struct iwl_mvm *mvm;
2571 valid_tx_ant = iwl_fw_valid_tx_ant(mvm->fw);
2572 if (lq_sta->dbg_fixed_rate) {
2574 ((lq_sta->dbg_fixed_rate & RATE_MCS_ANT_ABC_MSK)
2575 >> RATE_MCS_ANT_POS);
2576 if ((valid_tx_ant & ant_sel_tx) == ant_sel_tx) {
2577 *rate_n_flags = lq_sta->dbg_fixed_rate;
2579 lq_sta->dbg_fixed_rate = 0;
2581 "Invalid antenna selection 0x%X, Valid is 0x%X\n",
2582 ant_sel_tx, valid_tx_ant);
2583 IWL_DEBUG_RATE(mvm, "Fixed rate OFF\n");
2588 static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
2589 const char __user *user_buf, size_t count, loff_t *ppos)
2591 struct iwl_lq_sta *lq_sta = file->private_data;
2592 struct iwl_mvm *mvm;
2599 memset(buf, 0, sizeof(buf));
2600 buf_size = min(count, sizeof(buf) - 1);
2601 if (copy_from_user(buf, user_buf, buf_size))
2604 if (sscanf(buf, "%x", &parsed_rate) == 1)
2605 lq_sta->dbg_fixed_rate = parsed_rate;
2607 lq_sta->dbg_fixed_rate = 0;
2609 rs_program_fix_rate(mvm, lq_sta);
2614 static const char *rs_pretty_lq_type(enum iwl_table_type type)
2616 static const char * const lq_types[] = {
2618 [LQ_LEGACY_A] = "LEGACY_A",
2619 [LQ_LEGACY_G] = "LEGACY_G",
2620 [LQ_HT_SISO] = "HT SISO",
2621 [LQ_HT_MIMO2] = "HT MIMO",
2622 [LQ_VHT_SISO] = "VHT SISO",
2623 [LQ_VHT_MIMO2] = "VHT MIMO",
2626 if (type < LQ_NONE || type >= LQ_MAX)
2629 return lq_types[type];
2632 static const char *rs_pretty_ant(u8 ant)
2634 static const char * const ant_name[] = {
2635 [ANT_NONE] = "None",
2648 return ant_name[ant];
2651 static int rs_pretty_print_rate(char *buf, const u32 rate)
2655 u8 mcs = 0, nss = 0;
2656 u8 ant = (rate & RATE_MCS_ANT_ABC_MSK) >> RATE_MCS_ANT_POS;
2658 if (!(rate & RATE_MCS_HT_MSK) &&
2659 !(rate & RATE_MCS_VHT_MSK)) {
2660 int index = iwl_hwrate_to_plcp_idx(rate);
2662 return sprintf(buf, "Legacy | ANT: %s Rate: %s Mbps\n",
2663 rs_pretty_ant(ant), iwl_rate_mcs[index].mbps);
2666 if (rate & RATE_MCS_VHT_MSK) {
2668 mcs = rate & RATE_VHT_MCS_RATE_CODE_MSK;
2669 nss = ((rate & RATE_VHT_MCS_NSS_MSK)
2670 >> RATE_VHT_MCS_NSS_POS) + 1;
2671 } else if (rate & RATE_MCS_HT_MSK) {
2673 mcs = rate & RATE_HT_MCS_INDEX_MSK;
2675 type = "Unknown"; /* shouldn't happen */
2678 switch (rate & RATE_MCS_CHAN_WIDTH_MSK) {
2679 case RATE_MCS_CHAN_WIDTH_20:
2682 case RATE_MCS_CHAN_WIDTH_40:
2685 case RATE_MCS_CHAN_WIDTH_80:
2688 case RATE_MCS_CHAN_WIDTH_160:
2695 return sprintf(buf, "%s | ANT: %s BW: %s MCS: %d NSS: %d %s%s%s%s%s\n",
2696 type, rs_pretty_ant(ant), bw, mcs, nss,
2697 (rate & RATE_MCS_SGI_MSK) ? "SGI " : "NGI ",
2698 (rate & RATE_MCS_STBC_MSK) ? "STBC " : "",
2699 (rate & RATE_MCS_LDPC_MSK) ? "LDPC " : "",
2700 (rate & RATE_MCS_BF_MSK) ? "BF " : "",
2701 (rate & RATE_MCS_ZLF_MSK) ? "ZLF " : "");
2704 static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
2705 char __user *user_buf, size_t count, loff_t *ppos)
2712 struct iwl_lq_sta *lq_sta = file->private_data;
2713 struct iwl_mvm *mvm;
2714 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
2715 struct rs_rate *rate = &tbl->rate;
2717 buff = kmalloc(2048, GFP_KERNEL);
2721 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
2722 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
2723 lq_sta->total_failed, lq_sta->total_success,
2724 lq_sta->active_legacy_rate);
2725 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
2726 lq_sta->dbg_fixed_rate);
2727 desc += sprintf(buff+desc, "valid_tx_ant %s%s%s\n",
2728 (iwl_fw_valid_tx_ant(mvm->fw) & ANT_A) ? "ANT_A," : "",
2729 (iwl_fw_valid_tx_ant(mvm->fw) & ANT_B) ? "ANT_B," : "",
2730 (iwl_fw_valid_tx_ant(mvm->fw) & ANT_C) ? "ANT_C" : "");
2731 desc += sprintf(buff+desc, "lq type %s\n",
2732 (is_legacy(rate)) ? "legacy" :
2733 is_vht(rate) ? "VHT" : "HT");
2734 if (!is_legacy(rate)) {
2735 desc += sprintf(buff+desc, " %s",
2736 (is_siso(rate)) ? "SISO" : "MIMO2");
2737 desc += sprintf(buff+desc, " %s",
2738 (is_ht20(rate)) ? "20MHz" :
2739 (is_ht40(rate)) ? "40MHz" :
2740 (is_ht80(rate)) ? "80Mhz" : "BAD BW");
2741 desc += sprintf(buff+desc, " %s %s\n",
2742 (rate->sgi) ? "SGI" : "NGI",
2743 (lq_sta->is_agg) ? "AGG on" : "");
2745 desc += sprintf(buff+desc, "last tx rate=0x%X\n",
2746 lq_sta->last_rate_n_flags);
2747 desc += sprintf(buff+desc,
2748 "general: flags=0x%X mimo-d=%d s-ant=0x%x d-ant=0x%x\n",
2750 lq_sta->lq.mimo_delim,
2751 lq_sta->lq.single_stream_ant_msk,
2752 lq_sta->lq.dual_stream_ant_msk);
2754 desc += sprintf(buff+desc,
2755 "agg: time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
2756 le16_to_cpu(lq_sta->lq.agg_time_limit),
2757 lq_sta->lq.agg_disable_start_th,
2758 lq_sta->lq.agg_frame_cnt_limit);
2760 desc += sprintf(buff+desc,
2761 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
2762 lq_sta->lq.initial_rate_index[0],
2763 lq_sta->lq.initial_rate_index[1],
2764 lq_sta->lq.initial_rate_index[2],
2765 lq_sta->lq.initial_rate_index[3]);
2767 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
2768 u32 rate = le32_to_cpu(lq_sta->lq.rs_table[i]);
2769 desc += sprintf(buff+desc,
2773 desc += rs_pretty_print_rate(buff+desc, rate);
2776 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2781 static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
2782 .write = rs_sta_dbgfs_scale_table_write,
2783 .read = rs_sta_dbgfs_scale_table_read,
2784 .open = simple_open,
2785 .llseek = default_llseek,
2787 static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
2788 char __user *user_buf, size_t count, loff_t *ppos)
2794 struct iwl_scale_tbl_info *tbl;
2795 struct rs_rate *rate;
2796 struct iwl_lq_sta *lq_sta = file->private_data;
2798 buff = kmalloc(1024, GFP_KERNEL);
2802 for (i = 0; i < LQ_SIZE; i++) {
2803 tbl = &(lq_sta->lq_info[i]);
2805 desc += sprintf(buff+desc,
2806 "%s type=%d SGI=%d BW=%s DUP=0\n"
2808 lq_sta->active_tbl == i ? "*" : "x",
2811 is_ht20(rate) ? "20Mhz" :
2812 is_ht40(rate) ? "40Mhz" :
2813 is_ht80(rate) ? "80Mhz" : "ERR",
2815 for (j = 0; j < IWL_RATE_COUNT; j++) {
2816 desc += sprintf(buff+desc,
2817 "counter=%d success=%d %%=%d\n",
2818 tbl->win[j].counter,
2819 tbl->win[j].success_counter,
2820 tbl->win[j].success_ratio);
2823 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2828 static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
2829 .read = rs_sta_dbgfs_stats_table_read,
2830 .open = simple_open,
2831 .llseek = default_llseek,
2834 static void rs_add_debugfs(void *mvm, void *mvm_sta, struct dentry *dir)
2836 struct iwl_lq_sta *lq_sta = mvm_sta;
2837 lq_sta->rs_sta_dbgfs_scale_table_file =
2838 debugfs_create_file("rate_scale_table", S_IRUSR | S_IWUSR, dir,
2839 lq_sta, &rs_sta_dbgfs_scale_table_ops);
2840 lq_sta->rs_sta_dbgfs_stats_table_file =
2841 debugfs_create_file("rate_stats_table", S_IRUSR, dir,
2842 lq_sta, &rs_sta_dbgfs_stats_table_ops);
2843 lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
2844 debugfs_create_u8("tx_agg_tid_enable", S_IRUSR | S_IWUSR, dir,
2845 &lq_sta->tx_agg_tid_en);
2848 static void rs_remove_debugfs(void *mvm, void *mvm_sta)
2850 struct iwl_lq_sta *lq_sta = mvm_sta;
2851 debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
2852 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
2853 debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
2858 * Initialization of rate scaling information is done by driver after
2859 * the station is added. Since mac80211 calls this function before a
2860 * station is added we ignore it.
2862 static void rs_rate_init_stub(void *mvm_r,
2863 struct ieee80211_supported_band *sband,
2864 struct cfg80211_chan_def *chandef,
2865 struct ieee80211_sta *sta, void *mvm_sta)
2868 static struct rate_control_ops rs_mvm_ops = {
2871 .tx_status = rs_tx_status,
2872 .get_rate = rs_get_rate,
2873 .rate_init = rs_rate_init_stub,
2876 .alloc_sta = rs_alloc_sta,
2877 .free_sta = rs_free_sta,
2878 .rate_update = rs_rate_update,
2879 #ifdef CONFIG_MAC80211_DEBUGFS
2880 .add_sta_debugfs = rs_add_debugfs,
2881 .remove_sta_debugfs = rs_remove_debugfs,
2885 int iwl_mvm_rate_control_register(void)
2887 return ieee80211_rate_control_register(&rs_mvm_ops);
2890 void iwl_mvm_rate_control_unregister(void)
2892 ieee80211_rate_control_unregister(&rs_mvm_ops);
2896 * iwl_mvm_tx_protection - Gets LQ command, change it to enable/disable
2897 * Tx protection, according to this rquest and previous requests,
2898 * and send the LQ command.
2899 * @mvmsta: The station
2900 * @enable: Enable Tx protection?
2902 int iwl_mvm_tx_protection(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
2905 struct iwl_lq_cmd *lq = &mvmsta->lq_sta.lq;
2907 lockdep_assert_held(&mvm->mutex);
2910 if (mvmsta->tx_protection == 0)
2911 lq->flags |= LQ_FLAG_USE_RTS_MSK;
2912 mvmsta->tx_protection++;
2914 mvmsta->tx_protection--;
2915 if (mvmsta->tx_protection == 0)
2916 lq->flags &= ~LQ_FLAG_USE_RTS_MSK;
2919 return iwl_mvm_send_lq_cmd(mvm, lq, false);