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_1M_INDEX] = IWL_RATE_6M_INDEX,
60 [IWL_RATE_2M_INDEX] = IWL_RATE_6M_INDEX,
61 [IWL_RATE_5M_INDEX] = IWL_RATE_6M_INDEX,
62 [IWL_RATE_11M_INDEX] = IWL_RATE_6M_INDEX,
63 [IWL_RATE_6M_INDEX] = IWL_RATE_6M_INDEX,
64 [IWL_RATE_9M_INDEX] = IWL_RATE_6M_INDEX,
65 [IWL_RATE_12M_INDEX] = IWL_RATE_9M_INDEX,
66 [IWL_RATE_18M_INDEX] = IWL_RATE_12M_INDEX,
67 [IWL_RATE_24M_INDEX] = IWL_RATE_18M_INDEX,
68 [IWL_RATE_36M_INDEX] = IWL_RATE_24M_INDEX,
69 [IWL_RATE_48M_INDEX] = IWL_RATE_36M_INDEX,
70 [IWL_RATE_54M_INDEX] = IWL_RATE_48M_INDEX,
71 [IWL_RATE_60M_INDEX] = IWL_RATE_54M_INDEX,
74 static const u8 ant_toggle_lookup[] = {
75 [ANT_NONE] = ANT_NONE,
85 #define IWL_DECLARE_RATE_INFO(r, s, rp, rn) \
86 [IWL_RATE_##r##M_INDEX] = { IWL_RATE_##r##M_PLCP, \
87 IWL_RATE_HT_SISO_MCS_##s##_PLCP, \
88 IWL_RATE_HT_MIMO2_MCS_##s##_PLCP, \
89 IWL_RATE_VHT_SISO_MCS_##s##_PLCP, \
90 IWL_RATE_VHT_MIMO2_MCS_##s##_PLCP,\
91 IWL_RATE_##rp##M_INDEX, \
92 IWL_RATE_##rn##M_INDEX }
94 #define IWL_DECLARE_MCS_RATE(s) \
95 [IWL_RATE_MCS_##s##_INDEX] = { IWL_RATE_INVM_PLCP, \
96 IWL_RATE_HT_SISO_MCS_##s##_PLCP, \
97 IWL_RATE_HT_MIMO2_MCS_##s##_PLCP, \
98 IWL_RATE_VHT_SISO_MCS_##s##_PLCP, \
99 IWL_RATE_VHT_MIMO2_MCS_##s##_PLCP, \
100 IWL_RATE_INVM_INDEX, \
101 IWL_RATE_INVM_INDEX }
105 * rate, ht rate, prev rate, next rate
107 * If there isn't a valid next or previous rate then INV is used which
108 * maps to IWL_RATE_INVALID
111 static const struct iwl_rs_rate_info iwl_rates[IWL_RATE_COUNT] = {
112 IWL_DECLARE_RATE_INFO(1, INV, INV, 2), /* 1mbps */
113 IWL_DECLARE_RATE_INFO(2, INV, 1, 5), /* 2mbps */
114 IWL_DECLARE_RATE_INFO(5, INV, 2, 11), /*5.5mbps */
115 IWL_DECLARE_RATE_INFO(11, INV, 9, 12), /* 11mbps */
116 IWL_DECLARE_RATE_INFO(6, 0, 5, 11), /* 6mbps ; MCS 0 */
117 IWL_DECLARE_RATE_INFO(9, INV, 6, 11), /* 9mbps */
118 IWL_DECLARE_RATE_INFO(12, 1, 11, 18), /* 12mbps ; MCS 1 */
119 IWL_DECLARE_RATE_INFO(18, 2, 12, 24), /* 18mbps ; MCS 2 */
120 IWL_DECLARE_RATE_INFO(24, 3, 18, 36), /* 24mbps ; MCS 3 */
121 IWL_DECLARE_RATE_INFO(36, 4, 24, 48), /* 36mbps ; MCS 4 */
122 IWL_DECLARE_RATE_INFO(48, 5, 36, 54), /* 48mbps ; MCS 5 */
123 IWL_DECLARE_RATE_INFO(54, 6, 48, INV), /* 54mbps ; MCS 6 */
124 IWL_DECLARE_MCS_RATE(7), /* MCS 7 */
125 IWL_DECLARE_MCS_RATE(8), /* MCS 8 */
126 IWL_DECLARE_MCS_RATE(9), /* MCS 9 */
129 static inline u8 rs_extract_rate(u32 rate_n_flags)
131 /* also works for HT because bits 7:6 are zero there */
132 return (u8)(rate_n_flags & RATE_LEGACY_RATE_MSK);
135 static int iwl_hwrate_to_plcp_idx(u32 rate_n_flags)
139 if (rate_n_flags & RATE_MCS_HT_MSK) {
140 idx = rate_n_flags & RATE_HT_MCS_RATE_CODE_MSK;
141 idx += IWL_RATE_MCS_0_INDEX;
143 /* skip 9M not supported in HT*/
144 if (idx >= IWL_RATE_9M_INDEX)
146 if ((idx >= IWL_FIRST_HT_RATE) && (idx <= IWL_LAST_HT_RATE))
148 } else if (rate_n_flags & RATE_MCS_VHT_MSK) {
149 idx = rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK;
150 idx += IWL_RATE_MCS_0_INDEX;
152 /* skip 9M not supported in VHT*/
153 if (idx >= IWL_RATE_9M_INDEX)
155 if ((idx >= IWL_FIRST_VHT_RATE) && (idx <= IWL_LAST_VHT_RATE))
158 /* legacy rate format, search for match in table */
160 u8 legacy_rate = rs_extract_rate(rate_n_flags);
161 for (idx = 0; idx < ARRAY_SIZE(iwl_rates); idx++)
162 if (iwl_rates[idx].plcp == legacy_rate)
169 static void rs_rate_scale_perform(struct iwl_mvm *mvm,
171 struct ieee80211_sta *sta,
172 struct iwl_lq_sta *lq_sta);
173 static void rs_fill_link_cmd(struct iwl_mvm *mvm,
174 struct ieee80211_sta *sta,
175 struct iwl_lq_sta *lq_sta, u32 rate_n_flags);
176 static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search);
177 static const char *rs_pretty_lq_type(enum iwl_table_type type);
178 static const char *rs_pretty_ant(u8 ant);
180 #ifdef CONFIG_MAC80211_DEBUGFS
181 static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
184 static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
190 * The following tables contain the expected throughput metrics for all rates
192 * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
194 * where invalid entries are zeros.
196 * CCK rates are only valid in legacy table and will only be used in G
200 static s32 expected_tpt_legacy[IWL_RATE_COUNT] = {
201 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 0, 0, 0
204 /* Expected TpT tables. 4 indexes:
205 * 0 - NGI, 1 - SGI, 2 - AGG+NGI, 3 - AGG+SGI
207 static s32 expected_tpt_siso_20MHz[4][IWL_RATE_COUNT] = {
208 {0, 0, 0, 0, 42, 0, 76, 102, 124, 159, 183, 193, 202, 216, 0},
209 {0, 0, 0, 0, 46, 0, 82, 110, 132, 168, 192, 202, 210, 225, 0},
210 {0, 0, 0, 0, 49, 0, 97, 145, 192, 285, 375, 420, 464, 551, 0},
211 {0, 0, 0, 0, 54, 0, 108, 160, 213, 315, 415, 465, 513, 608, 0},
214 static s32 expected_tpt_siso_40MHz[4][IWL_RATE_COUNT] = {
215 {0, 0, 0, 0, 77, 0, 127, 160, 184, 220, 242, 250, 257, 269, 275},
216 {0, 0, 0, 0, 83, 0, 135, 169, 193, 229, 250, 257, 264, 275, 280},
217 {0, 0, 0, 0, 101, 0, 199, 295, 389, 570, 744, 828, 911, 1070, 1173},
218 {0, 0, 0, 0, 112, 0, 220, 326, 429, 629, 819, 912, 1000, 1173, 1284},
221 static s32 expected_tpt_siso_80MHz[4][IWL_RATE_COUNT] = {
222 {0, 0, 0, 0, 130, 0, 191, 223, 244, 273, 288, 294, 298, 305, 308},
223 {0, 0, 0, 0, 138, 0, 200, 231, 251, 279, 293, 298, 302, 308, 312},
224 {0, 0, 0, 0, 217, 0, 429, 634, 834, 1220, 1585, 1760, 1931, 2258, 2466},
225 {0, 0, 0, 0, 241, 0, 475, 701, 921, 1343, 1741, 1931, 2117, 2468, 2691},
228 static s32 expected_tpt_mimo2_20MHz[4][IWL_RATE_COUNT] = {
229 {0, 0, 0, 0, 74, 0, 123, 155, 179, 213, 235, 243, 250, 261, 0},
230 {0, 0, 0, 0, 81, 0, 131, 164, 187, 221, 242, 250, 256, 267, 0},
231 {0, 0, 0, 0, 98, 0, 193, 286, 375, 550, 718, 799, 878, 1032, 0},
232 {0, 0, 0, 0, 109, 0, 214, 316, 414, 607, 790, 879, 965, 1132, 0},
235 static s32 expected_tpt_mimo2_40MHz[4][IWL_RATE_COUNT] = {
236 {0, 0, 0, 0, 123, 0, 182, 214, 235, 264, 279, 285, 289, 296, 300},
237 {0, 0, 0, 0, 131, 0, 191, 222, 242, 270, 284, 289, 293, 300, 303},
238 {0, 0, 0, 0, 200, 0, 390, 571, 741, 1067, 1365, 1505, 1640, 1894, 2053},
239 {0, 0, 0, 0, 221, 0, 430, 630, 816, 1169, 1490, 1641, 1784, 2053, 2221},
242 static s32 expected_tpt_mimo2_80MHz[4][IWL_RATE_COUNT] = {
243 {0, 0, 0, 0, 182, 0, 240, 264, 278, 299, 308, 311, 313, 317, 319},
244 {0, 0, 0, 0, 190, 0, 247, 269, 282, 302, 310, 313, 315, 319, 320},
245 {0, 0, 0, 0, 428, 0, 833, 1215, 1577, 2254, 2863, 3147, 3418, 3913, 4219},
246 {0, 0, 0, 0, 474, 0, 920, 1338, 1732, 2464, 3116, 3418, 3705, 4225, 4545},
250 static const struct iwl_rate_mcs_info iwl_rate_mcs[IWL_RATE_COUNT] = {
259 { "24", "16QAM 1/2"},
260 { "36", "16QAM 3/4"},
261 { "48", "64QAM 2/3"},
262 { "54", "64QAM 3/4"},
263 { "60", "64QAM 5/6"},
266 #define MCS_INDEX_PER_STREAM (8)
268 static inline void rs_dump_rate(struct iwl_mvm *mvm, const struct rs_rate *rate,
271 IWL_DEBUG_RATE(mvm, "%s: (%s: %d) ANT: %s BW: %d SGI: %d\n",
272 prefix, rs_pretty_lq_type(rate->type),
273 rate->index, rs_pretty_ant(rate->ant),
274 rate->bw, rate->sgi);
277 static void rs_rate_scale_clear_window(struct iwl_rate_scale_data *window)
280 window->success_counter = 0;
281 window->success_ratio = IWL_INVALID_VALUE;
283 window->average_tpt = IWL_INVALID_VALUE;
286 static inline u8 rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
288 return (ant_type & valid_antenna) == ant_type;
291 #ifdef CONFIG_MAC80211_DEBUGFS
293 * Program the device to use fixed rate for frame transmit
294 * This is for debugging/testing only
295 * once the device start use fixed rate, we need to reload the module
296 * to being back the normal operation.
298 static void rs_program_fix_rate(struct iwl_mvm *mvm,
299 struct iwl_lq_sta *lq_sta)
301 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
302 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
303 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
305 IWL_DEBUG_RATE(mvm, "sta_id %d rate 0x%X\n",
306 lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
308 if (lq_sta->dbg_fixed_rate) {
309 rs_fill_link_cmd(NULL, NULL, lq_sta, lq_sta->dbg_fixed_rate);
310 iwl_mvm_send_lq_cmd(lq_sta->drv, &lq_sta->lq, false);
315 static int rs_tl_turn_on_agg_for_tid(struct iwl_mvm *mvm,
316 struct iwl_lq_sta *lq_data, u8 tid,
317 struct ieee80211_sta *sta)
321 IWL_DEBUG_HT(mvm, "Starting Tx agg: STA: %pM tid: %d\n",
323 ret = ieee80211_start_tx_ba_session(sta, tid, 5000);
324 if (ret == -EAGAIN) {
326 * driver and mac80211 is out of sync
327 * this might be cause by reloading firmware
328 * stop the tx ba session here
330 IWL_ERR(mvm, "Fail start Tx agg on tid: %d\n",
332 ieee80211_stop_tx_ba_session(sta, tid);
337 static void rs_tl_turn_on_agg(struct iwl_mvm *mvm, u8 tid,
338 struct iwl_lq_sta *lq_data,
339 struct ieee80211_sta *sta)
341 if (tid < IWL_MAX_TID_COUNT)
342 rs_tl_turn_on_agg_for_tid(mvm, lq_data, tid, sta);
344 IWL_ERR(mvm, "tid exceeds max TID count: %d/%d\n",
345 tid, IWL_MAX_TID_COUNT);
348 static inline int get_num_of_ant_from_rate(u32 rate_n_flags)
350 return !!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
351 !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
352 !!(rate_n_flags & RATE_MCS_ANT_C_MSK);
356 * Static function to get the expected throughput from an iwl_scale_tbl_info
357 * that wraps a NULL pointer check
359 static s32 get_expected_tpt(struct iwl_scale_tbl_info *tbl, int rs_index)
361 if (tbl->expected_tpt)
362 return tbl->expected_tpt[rs_index];
367 * rs_collect_tx_data - Update the success/failure sliding window
369 * We keep a sliding window of the last 62 packets transmitted
370 * at this rate. window->data contains the bitmask of successful
373 static int rs_collect_tx_data(struct iwl_scale_tbl_info *tbl,
374 int scale_index, int attempts, int successes)
376 struct iwl_rate_scale_data *window = NULL;
377 static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
380 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
383 /* Select window for current tx bit rate */
384 window = &(tbl->win[scale_index]);
386 /* Get expected throughput */
387 tpt = get_expected_tpt(tbl, scale_index);
390 * Keep track of only the latest 62 tx frame attempts in this rate's
391 * history window; anything older isn't really relevant any more.
392 * If we have filled up the sliding window, drop the oldest attempt;
393 * if the oldest attempt (highest bit in bitmap) shows "success",
394 * subtract "1" from the success counter (this is the main reason
395 * we keep these bitmaps!).
397 while (attempts > 0) {
398 if (window->counter >= IWL_RATE_MAX_WINDOW) {
399 /* remove earliest */
400 window->counter = IWL_RATE_MAX_WINDOW - 1;
402 if (window->data & mask) {
403 window->data &= ~mask;
404 window->success_counter--;
408 /* Increment frames-attempted counter */
411 /* Shift bitmap by one frame to throw away oldest history */
414 /* Mark the most recent #successes attempts as successful */
416 window->success_counter++;
424 /* Calculate current success ratio, avoid divide-by-0! */
425 if (window->counter > 0)
426 window->success_ratio = 128 * (100 * window->success_counter)
429 window->success_ratio = IWL_INVALID_VALUE;
431 fail_count = window->counter - window->success_counter;
433 /* Calculate average throughput, if we have enough history. */
434 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
435 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
436 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
438 window->average_tpt = IWL_INVALID_VALUE;
443 /* Convert rs_rate object into ucode rate bitmask */
444 static u32 ucode_rate_from_rs_rate(struct iwl_mvm *mvm,
445 struct rs_rate *rate)
448 int index = rate->index;
450 ucode_rate |= ((rate->ant << RATE_MCS_ANT_POS) &
451 RATE_MCS_ANT_ABC_MSK);
453 if (is_legacy(rate)) {
454 ucode_rate |= iwl_rates[index].plcp;
455 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
456 ucode_rate |= RATE_MCS_CCK_MSK;
461 if (index < IWL_FIRST_HT_RATE || index > IWL_LAST_HT_RATE) {
462 IWL_ERR(mvm, "Invalid HT rate index %d\n", index);
463 index = IWL_LAST_HT_RATE;
465 ucode_rate |= RATE_MCS_HT_MSK;
467 if (is_ht_siso(rate))
468 ucode_rate |= iwl_rates[index].plcp_ht_siso;
469 else if (is_ht_mimo2(rate))
470 ucode_rate |= iwl_rates[index].plcp_ht_mimo2;
473 } else if (is_vht(rate)) {
474 if (index < IWL_FIRST_VHT_RATE || index > IWL_LAST_VHT_RATE) {
475 IWL_ERR(mvm, "Invalid VHT rate index %d\n", index);
476 index = IWL_LAST_VHT_RATE;
478 ucode_rate |= RATE_MCS_VHT_MSK;
479 if (is_vht_siso(rate))
480 ucode_rate |= iwl_rates[index].plcp_vht_siso;
481 else if (is_vht_mimo2(rate))
482 ucode_rate |= iwl_rates[index].plcp_vht_mimo2;
487 IWL_ERR(mvm, "Invalid rate->type %d\n", rate->type);
490 ucode_rate |= rate->bw;
492 ucode_rate |= RATE_MCS_SGI_MSK;
497 /* Convert a ucode rate into an rs_rate object */
498 static int rs_rate_from_ucode_rate(const u32 ucode_rate,
499 enum ieee80211_band band,
500 struct rs_rate *rate)
502 u32 ant_msk = ucode_rate & RATE_MCS_ANT_ABC_MSK;
503 u8 num_of_ant = get_num_of_ant_from_rate(ucode_rate);
506 memset(rate, 0, sizeof(struct rs_rate));
507 rate->index = iwl_hwrate_to_plcp_idx(ucode_rate);
509 if (rate->index == IWL_RATE_INVALID) {
514 rate->ant = (ant_msk >> RATE_MCS_ANT_POS);
517 if (!(ucode_rate & RATE_MCS_HT_MSK) &&
518 !(ucode_rate & RATE_MCS_VHT_MSK)) {
519 if (num_of_ant == 1) {
520 if (band == IEEE80211_BAND_5GHZ)
521 rate->type = LQ_LEGACY_A;
523 rate->type = LQ_LEGACY_G;
530 if (ucode_rate & RATE_MCS_SGI_MSK)
533 rate->bw = ucode_rate & RATE_MCS_CHAN_WIDTH_MSK;
535 if (ucode_rate & RATE_MCS_HT_MSK) {
536 nss = ((ucode_rate & RATE_HT_MCS_NSS_MSK) >>
537 RATE_HT_MCS_NSS_POS) + 1;
540 rate->type = LQ_HT_SISO;
541 WARN_ON_ONCE(num_of_ant != 1);
542 } else if (nss == 2) {
543 rate->type = LQ_HT_MIMO2;
544 WARN_ON_ONCE(num_of_ant != 2);
548 } else if (ucode_rate & RATE_MCS_VHT_MSK) {
549 nss = ((ucode_rate & RATE_VHT_MCS_NSS_MSK) >>
550 RATE_VHT_MCS_NSS_POS) + 1;
553 rate->type = LQ_VHT_SISO;
554 WARN_ON_ONCE(num_of_ant != 1);
555 } else if (nss == 2) {
556 rate->type = LQ_VHT_MIMO2;
557 WARN_ON_ONCE(num_of_ant != 2);
563 WARN_ON_ONCE(rate->bw == RATE_MCS_CHAN_WIDTH_160);
564 WARN_ON_ONCE(rate->bw == RATE_MCS_CHAN_WIDTH_80 &&
570 /* switch to another antenna/antennas and return 1 */
571 /* if no other valid antenna found, return 0 */
572 static int rs_toggle_antenna(u32 valid_ant, u32 *ucode_rate,
573 struct rs_rate *rate)
577 if (!rate->ant || rate->ant > ANT_ABC)
580 if (!rs_is_valid_ant(valid_ant, rate->ant))
583 new_ant_type = ant_toggle_lookup[rate->ant];
585 while ((new_ant_type != rate->ant) &&
586 !rs_is_valid_ant(valid_ant, new_ant_type))
587 new_ant_type = ant_toggle_lookup[new_ant_type];
589 if (new_ant_type == rate->ant)
592 rate->ant = new_ant_type;
594 /* TODO: get rid of ucode_rate here. This should handle only rs_rate */
595 *ucode_rate &= ~RATE_MCS_ANT_ABC_MSK;
596 *ucode_rate |= new_ant_type << RATE_MCS_ANT_POS;
600 static u16 rs_get_supported_rates(struct iwl_lq_sta *lq_sta,
601 struct rs_rate *rate)
604 return lq_sta->active_legacy_rate;
605 else if (is_siso(rate))
606 return lq_sta->active_siso_rate;
607 else if (is_mimo2(rate))
608 return lq_sta->active_mimo2_rate;
614 static u16 rs_get_adjacent_rate(struct iwl_mvm *mvm, u8 index, u16 rate_mask,
617 u8 high = IWL_RATE_INVALID;
618 u8 low = IWL_RATE_INVALID;
620 /* 802.11A or ht walks to the next literal adjacent rate in
622 if (is_type_a_band(rate_type) || !is_type_legacy(rate_type)) {
626 /* Find the previous rate that is in the rate mask */
628 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
629 if (rate_mask & mask) {
635 /* Find the next rate that is in the rate mask */
637 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
638 if (rate_mask & mask) {
644 return (high << 8) | low;
648 while (low != IWL_RATE_INVALID) {
649 low = iwl_rates[low].prev_rs;
650 if (low == IWL_RATE_INVALID)
652 if (rate_mask & (1 << low))
654 IWL_DEBUG_RATE(mvm, "Skipping masked lower rate: %d\n", low);
658 while (high != IWL_RATE_INVALID) {
659 high = iwl_rates[high].next_rs;
660 if (high == IWL_RATE_INVALID)
662 if (rate_mask & (1 << high))
664 IWL_DEBUG_RATE(mvm, "Skipping masked higher rate: %d\n", high);
667 return (high << 8) | low;
670 static u32 rs_get_lower_rate(struct iwl_lq_sta *lq_sta,
671 struct rs_rate *rate,
672 u8 scale_index, u8 ht_possible)
677 u8 switch_to_legacy = 0;
678 struct iwl_mvm *mvm = lq_sta->drv;
680 /* check if we need to switch from HT to legacy rates.
681 * assumption is that mandatory rates (1Mbps or 6Mbps)
682 * are always supported (spec demand) */
683 if (!is_legacy(rate) && (!ht_possible || !scale_index)) {
684 switch_to_legacy = 1;
685 scale_index = rs_ht_to_legacy[scale_index];
686 if (lq_sta->band == IEEE80211_BAND_5GHZ)
687 rate->type = LQ_LEGACY_A;
689 rate->type = LQ_LEGACY_G;
691 if (num_of_ant(rate->ant) > 1)
693 first_antenna(iwl_fw_valid_tx_ant(mvm->fw));
699 rate_mask = rs_get_supported_rates(lq_sta, rate);
701 /* Mask with station rate restriction */
702 if (is_legacy(rate)) {
703 /* supp_rates has no CCK bits in A mode */
704 if (lq_sta->band == IEEE80211_BAND_5GHZ)
705 rate_mask = (u16)(rate_mask &
706 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
708 rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
711 /* If we switched from HT to legacy, check current rate */
712 if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
717 high_low = rs_get_adjacent_rate(lq_sta->drv, scale_index, rate_mask,
719 low = high_low & 0xff;
721 if (low == IWL_RATE_INVALID)
726 return ucode_rate_from_rs_rate(lq_sta->drv, rate);
729 /* Simple function to compare two rate scale table types */
730 static inline bool rs_rate_match(struct rs_rate *a,
733 return (a->type == b->type) && (a->ant == b->ant) && (a->sgi == b->sgi);
736 static u32 rs_ch_width_from_mac_flags(enum mac80211_rate_control_flags flags)
738 if (flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
739 return RATE_MCS_CHAN_WIDTH_40;
740 else if (flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
741 return RATE_MCS_CHAN_WIDTH_80;
742 else if (flags & IEEE80211_TX_RC_160_MHZ_WIDTH)
743 return RATE_MCS_CHAN_WIDTH_160;
745 return RATE_MCS_CHAN_WIDTH_20;
749 * mac80211 sends us Tx status
751 static void rs_tx_status(void *mvm_r, struct ieee80211_supported_band *sband,
752 struct ieee80211_sta *sta, void *priv_sta,
758 struct iwl_lq_sta *lq_sta = priv_sta;
759 struct iwl_lq_cmd *table;
760 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
761 struct iwl_op_mode *op_mode = (struct iwl_op_mode *)mvm_r;
762 struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
763 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
764 enum mac80211_rate_control_flags mac_flags;
767 struct iwl_scale_tbl_info *curr_tbl, *other_tbl, *tmp_tbl;
769 /* Treat uninitialized rate scaling data same as non-existing. */
771 IWL_DEBUG_RATE(mvm, "Station rate scaling not created yet.\n");
773 } else if (!lq_sta->drv) {
774 IWL_DEBUG_RATE(mvm, "Rate scaling not initialized yet.\n");
778 if (!ieee80211_is_data(hdr->frame_control) ||
779 info->flags & IEEE80211_TX_CTL_NO_ACK)
782 /* This packet was aggregated but doesn't carry status info */
783 if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
784 !(info->flags & IEEE80211_TX_STAT_AMPDU))
788 * Ignore this Tx frame response if its initial rate doesn't match
789 * that of latest Link Quality command. There may be stragglers
790 * from a previous Link Quality command, but we're no longer interested
791 * in those; they're either from the "active" mode while we're trying
792 * to check "search" mode, or a prior "search" mode after we've moved
793 * to a new "search" mode (which might become the new "active" mode).
796 ucode_rate = le32_to_cpu(table->rs_table[0]);
797 rs_rate_from_ucode_rate(ucode_rate, info->band, &rate);
798 if (info->band == IEEE80211_BAND_5GHZ)
799 rate.index -= IWL_FIRST_OFDM_RATE;
800 mac_flags = info->status.rates[0].flags;
801 mac_index = info->status.rates[0].idx;
802 /* For HT packets, map MCS to PLCP */
803 if (mac_flags & IEEE80211_TX_RC_MCS) {
804 /* Remove # of streams */
805 mac_index &= RATE_HT_MCS_RATE_CODE_MSK;
806 if (mac_index >= (IWL_RATE_9M_INDEX - IWL_FIRST_OFDM_RATE))
809 * mac80211 HT index is always zero-indexed; we need to move
810 * HT OFDM rates after CCK rates in 2.4 GHz band
812 if (info->band == IEEE80211_BAND_2GHZ)
813 mac_index += IWL_FIRST_OFDM_RATE;
814 } else if (mac_flags & IEEE80211_TX_RC_VHT_MCS) {
815 mac_index &= RATE_VHT_MCS_RATE_CODE_MSK;
816 if (mac_index >= (IWL_RATE_9M_INDEX - IWL_FIRST_OFDM_RATE))
820 /* Here we actually compare this rate to the latest LQ command */
821 if ((mac_index < 0) ||
822 (rate.sgi != !!(mac_flags & IEEE80211_TX_RC_SHORT_GI)) ||
823 (rate.bw != rs_ch_width_from_mac_flags(mac_flags)) ||
824 (rate.ant != info->status.antenna) ||
825 (!!(ucode_rate & RATE_MCS_HT_MSK) !=
826 !!(mac_flags & IEEE80211_TX_RC_MCS)) ||
827 (!!(ucode_rate & RATE_MCS_VHT_MSK) !=
828 !!(mac_flags & IEEE80211_TX_RC_VHT_MCS)) ||
829 (!!(ucode_rate & RATE_HT_MCS_GF_MSK) !=
830 !!(mac_flags & IEEE80211_TX_RC_GREEN_FIELD)) ||
831 (rate.index != mac_index)) {
833 "initial rate %d does not match %d (0x%x)\n",
834 mac_index, rate.index, ucode_rate);
836 * Since rates mis-match, the last LQ command may have failed.
837 * After IWL_MISSED_RATE_MAX mis-matches, resync the uCode with
840 lq_sta->missed_rate_counter++;
841 if (lq_sta->missed_rate_counter > IWL_MISSED_RATE_MAX) {
842 lq_sta->missed_rate_counter = 0;
843 iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, false);
845 /* Regardless, ignore this status info for outdated rate */
848 /* Rate did match, so reset the missed_rate_counter */
849 lq_sta->missed_rate_counter = 0;
851 /* Figure out if rate scale algorithm is in active or search table */
852 if (rs_rate_match(&rate,
853 &(lq_sta->lq_info[lq_sta->active_tbl].rate))) {
854 curr_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
855 other_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
856 } else if (rs_rate_match(&rate,
857 &lq_sta->lq_info[1 - lq_sta->active_tbl].rate)) {
858 curr_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
859 other_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
862 "Neither active nor search matches tx rate\n");
863 tmp_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
864 rs_dump_rate(mvm, &tmp_tbl->rate, "ACTIVE");
865 tmp_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
866 rs_dump_rate(mvm, &tmp_tbl->rate, "SEARCH");
867 rs_dump_rate(mvm, &rate, "ACTUAL");
870 * no matching table found, let's by-pass the data collection
871 * and continue to perform rate scale to find the rate table
873 rs_stay_in_table(lq_sta, true);
878 * Updating the frame history depends on whether packets were
881 * For aggregation, all packets were transmitted at the same rate, the
882 * first index into rate scale table.
884 if (info->flags & IEEE80211_TX_STAT_AMPDU) {
885 ucode_rate = le32_to_cpu(table->rs_table[0]);
886 rs_rate_from_ucode_rate(ucode_rate, info->band, &rate);
887 rs_collect_tx_data(curr_tbl, rate.index,
888 info->status.ampdu_len,
889 info->status.ampdu_ack_len);
891 /* Update success/fail counts if not searching for new mode */
892 if (lq_sta->stay_in_tbl) {
893 lq_sta->total_success += info->status.ampdu_ack_len;
894 lq_sta->total_failed += (info->status.ampdu_len -
895 info->status.ampdu_ack_len);
899 * For legacy, update frame history with for each Tx retry.
901 retries = info->status.rates[0].count - 1;
902 /* HW doesn't send more than 15 retries */
903 retries = min(retries, 15);
905 /* The last transmission may have been successful */
906 legacy_success = !!(info->flags & IEEE80211_TX_STAT_ACK);
907 /* Collect data for each rate used during failed TX attempts */
908 for (i = 0; i <= retries; ++i) {
909 ucode_rate = le32_to_cpu(table->rs_table[i]);
910 rs_rate_from_ucode_rate(ucode_rate, info->band, &rate);
912 * Only collect stats if retried rate is in the same RS
913 * table as active/search.
915 if (rs_rate_match(&rate, &curr_tbl->rate))
917 else if (rs_rate_match(&rate, &other_tbl->rate))
921 rs_collect_tx_data(tmp_tbl, rate.index, 1,
922 i < retries ? 0 : legacy_success);
925 /* Update success/fail counts if not searching for new mode */
926 if (lq_sta->stay_in_tbl) {
927 lq_sta->total_success += legacy_success;
928 lq_sta->total_failed += retries + (1 - legacy_success);
931 /* The last TX rate is cached in lq_sta; it's set in if/else above */
932 lq_sta->last_rate_n_flags = ucode_rate;
934 /* See if there's a better rate or modulation mode to try. */
935 if (sta && sta->supp_rates[sband->band])
936 rs_rate_scale_perform(mvm, skb, sta, lq_sta);
940 * Begin a period of staying with a selected modulation mode.
941 * Set "stay_in_tbl" flag to prevent any mode switches.
942 * Set frame tx success limits according to legacy vs. high-throughput,
943 * and reset overall (spanning all rates) tx success history statistics.
944 * These control how long we stay using same modulation mode before
945 * searching for a new mode.
947 static void rs_set_stay_in_table(struct iwl_mvm *mvm, u8 is_legacy,
948 struct iwl_lq_sta *lq_sta)
950 IWL_DEBUG_RATE(mvm, "we are staying in the same table\n");
951 lq_sta->stay_in_tbl = 1; /* only place this gets set */
953 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
954 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
955 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
957 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
958 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
959 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
961 lq_sta->table_count = 0;
962 lq_sta->total_failed = 0;
963 lq_sta->total_success = 0;
964 lq_sta->flush_timer = jiffies;
965 lq_sta->action_counter = 0;
969 * Find correct throughput table for given mode of modulation
971 static void rs_set_expected_tpt_table(struct iwl_lq_sta *lq_sta,
972 struct iwl_scale_tbl_info *tbl)
974 /* Used to choose among HT tables */
975 s32 (*ht_tbl_pointer)[IWL_RATE_COUNT];
976 struct rs_rate *rate = &tbl->rate;
978 /* Check for invalid LQ type */
979 if (WARN_ON_ONCE(!is_legacy(rate) && !is_ht(rate) &&
981 tbl->expected_tpt = expected_tpt_legacy;
985 /* Legacy rates have only one table */
986 if (is_legacy(rate)) {
987 tbl->expected_tpt = expected_tpt_legacy;
991 ht_tbl_pointer = expected_tpt_mimo2_20MHz;
992 /* Choose among many HT tables depending on number of streams
993 * (SISO/MIMO2), channel width (20/40/80), SGI, and aggregation
997 case RATE_MCS_CHAN_WIDTH_20:
998 ht_tbl_pointer = expected_tpt_siso_20MHz;
1000 case RATE_MCS_CHAN_WIDTH_40:
1001 ht_tbl_pointer = expected_tpt_siso_40MHz;
1003 case RATE_MCS_CHAN_WIDTH_80:
1004 ht_tbl_pointer = expected_tpt_siso_80MHz;
1009 } else if (is_mimo2(rate)) {
1011 case RATE_MCS_CHAN_WIDTH_20:
1012 ht_tbl_pointer = expected_tpt_mimo2_20MHz;
1014 case RATE_MCS_CHAN_WIDTH_40:
1015 ht_tbl_pointer = expected_tpt_mimo2_40MHz;
1017 case RATE_MCS_CHAN_WIDTH_80:
1018 ht_tbl_pointer = expected_tpt_mimo2_80MHz;
1027 if (!rate->sgi && !lq_sta->is_agg) /* Normal */
1028 tbl->expected_tpt = ht_tbl_pointer[0];
1029 else if (rate->sgi && !lq_sta->is_agg) /* SGI */
1030 tbl->expected_tpt = ht_tbl_pointer[1];
1031 else if (!rate->sgi && lq_sta->is_agg) /* AGG */
1032 tbl->expected_tpt = ht_tbl_pointer[2];
1034 tbl->expected_tpt = ht_tbl_pointer[3];
1038 * Find starting rate for new "search" high-throughput mode of modulation.
1039 * Goal is to find lowest expected rate (under perfect conditions) that is
1040 * above the current measured throughput of "active" mode, to give new mode
1041 * a fair chance to prove itself without too many challenges.
1043 * This gets called when transitioning to more aggressive modulation
1044 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
1045 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1046 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1047 * bit rate will typically need to increase, but not if performance was bad.
1049 static s32 rs_get_best_rate(struct iwl_mvm *mvm,
1050 struct iwl_lq_sta *lq_sta,
1051 struct iwl_scale_tbl_info *tbl, /* "search" */
1052 u16 rate_mask, s8 index)
1054 /* "active" values */
1055 struct iwl_scale_tbl_info *active_tbl =
1056 &(lq_sta->lq_info[lq_sta->active_tbl]);
1057 s32 active_sr = active_tbl->win[index].success_ratio;
1058 s32 active_tpt = active_tbl->expected_tpt[index];
1060 /* expected "search" throughput */
1061 s32 *tpt_tbl = tbl->expected_tpt;
1063 s32 new_rate, high, low, start_hi;
1067 new_rate = high = low = start_hi = IWL_RATE_INVALID;
1070 high_low = rs_get_adjacent_rate(mvm, rate, rate_mask,
1073 low = high_low & 0xff;
1074 high = (high_low >> 8) & 0xff;
1077 * Lower the "search" bit rate, to give new "search" mode
1078 * approximately the same throughput as "active" if:
1080 * 1) "Active" mode has been working modestly well (but not
1081 * great), and expected "search" throughput (under perfect
1082 * conditions) at candidate rate is above the actual
1083 * measured "active" throughput (but less than expected
1084 * "active" throughput under perfect conditions).
1086 * 2) "Active" mode has been working perfectly or very well
1087 * and expected "search" throughput (under perfect
1088 * conditions) at candidate rate is above expected
1089 * "active" throughput (under perfect conditions).
1091 if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
1092 ((active_sr > RS_SR_FORCE_DECREASE) &&
1093 (active_sr <= IWL_RATE_HIGH_TH) &&
1094 (tpt_tbl[rate] <= active_tpt))) ||
1095 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
1096 (tpt_tbl[rate] > active_tpt))) {
1097 /* (2nd or later pass)
1098 * If we've already tried to raise the rate, and are
1099 * now trying to lower it, use the higher rate. */
1100 if (start_hi != IWL_RATE_INVALID) {
1101 new_rate = start_hi;
1107 /* Loop again with lower rate */
1108 if (low != IWL_RATE_INVALID)
1111 /* Lower rate not available, use the original */
1115 /* Else try to raise the "search" rate to match "active" */
1117 /* (2nd or later pass)
1118 * If we've already tried to lower the rate, and are
1119 * now trying to raise it, use the lower rate. */
1120 if (new_rate != IWL_RATE_INVALID)
1123 /* Loop again with higher rate */
1124 else if (high != IWL_RATE_INVALID) {
1128 /* Higher rate not available, use the original */
1139 /* Move to the next action and wrap around to the first action in case
1140 * we're at the last action. Assumes actions start at 0.
1142 static inline void rs_move_next_action(struct iwl_scale_tbl_info *tbl,
1145 BUILD_BUG_ON(IWL_LEGACY_FIRST_ACTION != 0);
1146 BUILD_BUG_ON(IWL_SISO_FIRST_ACTION != 0);
1147 BUILD_BUG_ON(IWL_MIMO2_FIRST_ACTION != 0);
1149 tbl->action = (tbl->action + 1) % (last_action + 1);
1152 static u32 rs_bw_from_sta_bw(struct ieee80211_sta *sta)
1154 if (sta->bandwidth >= IEEE80211_STA_RX_BW_80)
1155 return RATE_MCS_CHAN_WIDTH_80;
1156 else if (sta->bandwidth >= IEEE80211_STA_RX_BW_40)
1157 return RATE_MCS_CHAN_WIDTH_40;
1159 return RATE_MCS_CHAN_WIDTH_20;
1162 static bool rs_sgi_allowed(struct rs_rate *rate,
1163 struct ieee80211_sta *sta)
1165 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
1166 struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
1168 if (is_ht20(rate) && (ht_cap->cap &
1169 IEEE80211_HT_CAP_SGI_20))
1171 if (is_ht40(rate) && (ht_cap->cap &
1172 IEEE80211_HT_CAP_SGI_40))
1174 if (is_ht80(rate) && (vht_cap->cap &
1175 IEEE80211_VHT_CAP_SHORT_GI_80))
1182 * Set up search table for MIMO2
1184 static int rs_switch_to_mimo2(struct iwl_mvm *mvm,
1185 struct iwl_lq_sta *lq_sta,
1186 struct ieee80211_sta *sta,
1187 struct iwl_scale_tbl_info *tbl, int index)
1192 if (!sta->ht_cap.ht_supported)
1195 if (sta->smps_mode == IEEE80211_SMPS_STATIC)
1198 /* Need both Tx chains/antennas to support MIMO */
1199 if (num_of_ant(iwl_fw_valid_tx_ant(mvm->fw)) < 2)
1202 IWL_DEBUG_RATE(mvm, "LQ: try to switch to MIMO2\n");
1204 tbl->rate.type = lq_sta->is_vht ? LQ_VHT_MIMO2 : LQ_HT_MIMO2;
1206 tbl->max_search = IWL_MAX_SEARCH;
1207 rate_mask = lq_sta->active_mimo2_rate;
1209 tbl->rate.bw = rs_bw_from_sta_bw(sta);
1210 rs_set_expected_tpt_table(lq_sta, tbl);
1212 rate_idx = rs_get_best_rate(mvm, lq_sta, tbl, rate_mask, index);
1214 IWL_DEBUG_RATE(mvm, "LQ: MIMO2 best rate %d mask %X\n",
1215 rate_idx, rate_mask);
1216 if ((rate_idx == IWL_RATE_INVALID) || !((1 << rate_idx) & rate_mask)) {
1217 IWL_DEBUG_RATE(mvm, "Can't switch with index %d rate mask %x\n",
1218 rate_idx, rate_mask);
1222 tbl->rate.index = rate_idx;
1223 tbl->current_rate = ucode_rate_from_rs_rate(mvm, &tbl->rate);
1225 IWL_DEBUG_RATE(mvm, "LQ: Switch to new mcs %X index\n",
1231 * Set up search table for SISO
1233 static int rs_switch_to_siso(struct iwl_mvm *mvm,
1234 struct iwl_lq_sta *lq_sta,
1235 struct ieee80211_sta *sta,
1236 struct iwl_scale_tbl_info *tbl, int index)
1241 if (!sta->ht_cap.ht_supported)
1244 IWL_DEBUG_RATE(mvm, "LQ: try to switch to SISO\n");
1246 tbl->rate.type = lq_sta->is_vht ? LQ_VHT_SISO : LQ_HT_SISO;
1248 tbl->max_search = IWL_MAX_SEARCH;
1249 rate_mask = lq_sta->active_siso_rate;
1251 tbl->rate.bw = rs_bw_from_sta_bw(sta);
1252 rs_set_expected_tpt_table(lq_sta, tbl);
1253 rate_idx = rs_get_best_rate(mvm, lq_sta, tbl, rate_mask, index);
1255 IWL_DEBUG_RATE(mvm, "LQ: get best rate %d mask %X\n",
1256 rate_idx, rate_mask);
1257 if ((rate_idx == IWL_RATE_INVALID) || !((1 << rate_idx) & rate_mask)) {
1259 "can not switch with index %d rate mask %x\n",
1260 rate_idx, rate_mask);
1264 tbl->rate.index = rate_idx;
1265 tbl->current_rate = ucode_rate_from_rs_rate(mvm, &tbl->rate);
1267 IWL_DEBUG_RATE(mvm, "LQ: Switch to new mcs %X index\n",
1273 * Try to switch to new modulation mode from legacy
1275 static int rs_move_legacy_other(struct iwl_mvm *mvm,
1276 struct iwl_lq_sta *lq_sta,
1277 struct ieee80211_sta *sta,
1280 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1281 struct iwl_scale_tbl_info *search_tbl =
1282 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1283 struct rs_rate *rate = &search_tbl->rate;
1284 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1285 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1286 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1288 u8 valid_tx_ant = iwl_fw_valid_tx_ant(mvm->fw);
1289 u8 tx_chains_num = num_of_ant(valid_tx_ant);
1291 u8 update_search_tbl_counter = 0;
1293 start_action = tbl->action;
1295 lq_sta->action_counter++;
1296 switch (tbl->action) {
1297 case IWL_LEGACY_SWITCH_ANTENNA:
1298 IWL_DEBUG_RATE(mvm, "LQ: Legacy toggle Antenna\n");
1300 if (tx_chains_num <= 1)
1303 /* Don't change antenna if success has been great */
1304 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1307 /* Set up search table to try other antenna */
1308 memcpy(search_tbl, tbl, sz);
1310 if (rs_toggle_antenna(valid_tx_ant,
1311 &search_tbl->current_rate,
1312 &search_tbl->rate)) {
1313 update_search_tbl_counter = 1;
1314 rs_set_expected_tpt_table(lq_sta, search_tbl);
1318 case IWL_LEGACY_SWITCH_SISO:
1319 IWL_DEBUG_RATE(mvm, "LQ: Legacy switch to SISO\n");
1321 /* Set up search table to try SISO */
1322 memcpy(search_tbl, tbl, sz);
1324 ret = rs_switch_to_siso(mvm, lq_sta, sta,
1327 lq_sta->action_counter = 0;
1332 case IWL_LEGACY_SWITCH_MIMO2:
1333 IWL_DEBUG_RATE(mvm, "LQ: Legacy switch to MIMO2\n");
1335 /* Set up search table to try MIMO */
1336 memcpy(search_tbl, tbl, sz);
1340 if (!rs_is_valid_ant(valid_tx_ant,
1344 ret = rs_switch_to_mimo2(mvm, lq_sta, sta,
1347 lq_sta->action_counter = 0;
1354 rs_move_next_action(tbl, IWL_LEGACY_LAST_ACTION);
1356 if (tbl->action == start_action)
1359 rate->type = LQ_NONE;
1363 lq_sta->search_better_tbl = 1;
1364 rs_move_next_action(tbl, IWL_LEGACY_LAST_ACTION);
1365 if (update_search_tbl_counter)
1366 search_tbl->action = tbl->action;
1371 * Try to switch to new modulation mode from SISO
1373 static int rs_move_siso_to_other(struct iwl_mvm *mvm,
1374 struct iwl_lq_sta *lq_sta,
1375 struct ieee80211_sta *sta, int index)
1377 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1378 struct iwl_scale_tbl_info *search_tbl =
1379 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1380 struct rs_rate *rate = &search_tbl->rate;
1381 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1382 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1383 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1385 u8 valid_tx_ant = iwl_fw_valid_tx_ant(mvm->fw);
1386 u8 tx_chains_num = num_of_ant(valid_tx_ant);
1387 u8 update_search_tbl_counter = 0;
1390 if (tbl->action == IWL_SISO_SWITCH_MIMO2 &&
1391 !iwl_mvm_bt_coex_is_mimo_allowed(mvm, sta))
1392 tbl->action = IWL_SISO_SWITCH_ANTENNA;
1394 start_action = tbl->action;
1396 lq_sta->action_counter++;
1397 switch (tbl->action) {
1398 case IWL_SISO_SWITCH_ANTENNA:
1399 IWL_DEBUG_RATE(mvm, "LQ: SISO toggle Antenna\n");
1400 if (tx_chains_num <= 1)
1403 if (window->success_ratio >= IWL_RS_GOOD_RATIO &&
1404 BT_MBOX_MSG(&mvm->last_bt_notif, 3,
1408 memcpy(search_tbl, tbl, sz);
1409 if (rs_toggle_antenna(valid_tx_ant,
1410 &search_tbl->current_rate,
1412 update_search_tbl_counter = 1;
1416 case IWL_SISO_SWITCH_MIMO2:
1417 IWL_DEBUG_RATE(mvm, "LQ: SISO switch to MIMO2\n");
1418 memcpy(search_tbl, tbl, sz);
1422 if (!rs_is_valid_ant(valid_tx_ant,
1426 ret = rs_switch_to_mimo2(mvm, lq_sta, sta,
1431 case IWL_SISO_SWITCH_GI:
1432 if (!rs_sgi_allowed(rate, sta))
1435 IWL_DEBUG_RATE(mvm, "LQ: SISO toggle SGI/NGI\n");
1437 memcpy(search_tbl, tbl, sz);
1438 rate->sgi = !tbl->rate.sgi;
1439 rs_set_expected_tpt_table(lq_sta, search_tbl);
1440 if (tbl->rate.sgi) {
1441 s32 tpt = lq_sta->last_tpt / 100;
1442 if (tpt >= search_tbl->expected_tpt[index])
1445 rate->index = index;
1446 search_tbl->current_rate =
1447 ucode_rate_from_rs_rate(mvm, rate);
1448 update_search_tbl_counter = 1;
1453 rs_move_next_action(tbl, IWL_SISO_LAST_ACTION);
1455 if (tbl->action == start_action)
1458 rate->type = LQ_NONE;
1462 lq_sta->search_better_tbl = 1;
1463 rs_move_next_action(tbl, IWL_SISO_LAST_ACTION);
1464 if (update_search_tbl_counter)
1465 search_tbl->action = tbl->action;
1471 * Try to switch to new modulation mode from MIMO2
1473 static int rs_move_mimo2_to_other(struct iwl_mvm *mvm,
1474 struct iwl_lq_sta *lq_sta,
1475 struct ieee80211_sta *sta, int index)
1477 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1478 struct iwl_scale_tbl_info *search_tbl =
1479 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1480 struct rs_rate *rate = &search_tbl->rate;
1481 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1482 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1484 u8 valid_tx_ant = iwl_fw_valid_tx_ant(mvm->fw);
1485 u8 update_search_tbl_counter = 0;
1488 if (!iwl_mvm_bt_coex_is_mimo_allowed(mvm, sta)) {
1489 IWL_DEBUG_RATE(mvm, "BT COEX force switch to SISO A\n");
1490 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1493 start_action = tbl->action;
1495 lq_sta->action_counter++;
1496 switch (tbl->action) {
1497 case IWL_MIMO2_SWITCH_SISO_A:
1498 case IWL_MIMO2_SWITCH_SISO_B:
1499 IWL_DEBUG_RATE(mvm, "LQ: MIMO2 switch to SISO\n");
1501 /* Set up new search table for SISO */
1502 memcpy(search_tbl, tbl, sz);
1504 if (tbl->action == IWL_MIMO2_SWITCH_SISO_A)
1506 else /* tbl->action == IWL_MIMO2_SWITCH_SISO_B */
1509 if (!rs_is_valid_ant(valid_tx_ant,
1513 ret = rs_switch_to_siso(mvm, lq_sta, sta,
1520 case IWL_MIMO2_SWITCH_GI:
1521 if (!rs_sgi_allowed(rate, sta))
1524 IWL_DEBUG_RATE(mvm, "LQ: MIMO2 toggle SGI/NGI\n");
1526 /* Set up new search table for MIMO2 */
1527 memcpy(search_tbl, tbl, sz);
1528 rate->sgi = !tbl->rate.sgi;
1529 rs_set_expected_tpt_table(lq_sta, search_tbl);
1531 * If active table already uses the fastest possible
1532 * modulation (dual stream with short guard interval),
1533 * and it's working well, there's no need to look
1534 * for a better type of modulation!
1536 if (tbl->rate.sgi) {
1537 s32 tpt = lq_sta->last_tpt / 100;
1538 if (tpt >= search_tbl->expected_tpt[index])
1541 rate->index = index;
1542 search_tbl->current_rate =
1543 ucode_rate_from_rs_rate(mvm, rate);
1544 update_search_tbl_counter = 1;
1549 rs_move_next_action(tbl, IWL_MIMO2_LAST_ACTION);
1551 if (tbl->action == start_action)
1554 rate->type = LQ_NONE;
1557 lq_sta->search_better_tbl = 1;
1558 rs_move_next_action(tbl, IWL_MIMO2_LAST_ACTION);
1559 if (update_search_tbl_counter)
1560 search_tbl->action = tbl->action;
1566 * Check whether we should continue using same modulation mode, or
1567 * begin search for a new mode, based on:
1568 * 1) # tx successes or failures while using this mode
1569 * 2) # times calling this function
1570 * 3) elapsed time in this mode (not used, for now)
1572 static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search)
1574 struct iwl_scale_tbl_info *tbl;
1577 int flush_interval_passed = 0;
1578 struct iwl_mvm *mvm;
1581 active_tbl = lq_sta->active_tbl;
1583 tbl = &(lq_sta->lq_info[active_tbl]);
1585 /* If we've been disallowing search, see if we should now allow it */
1586 if (lq_sta->stay_in_tbl) {
1587 /* Elapsed time using current modulation mode */
1588 if (lq_sta->flush_timer)
1589 flush_interval_passed =
1591 (unsigned long)(lq_sta->flush_timer +
1592 RS_STAY_IN_COLUMN_TIMEOUT));
1595 * Check if we should allow search for new modulation mode.
1596 * If many frames have failed or succeeded, or we've used
1597 * this same modulation for a long time, allow search, and
1598 * reset history stats that keep track of whether we should
1599 * allow a new search. Also (below) reset all bitmaps and
1600 * stats in active history.
1603 (lq_sta->total_failed > lq_sta->max_failure_limit) ||
1604 (lq_sta->total_success > lq_sta->max_success_limit) ||
1605 ((!lq_sta->search_better_tbl) &&
1606 (lq_sta->flush_timer) && (flush_interval_passed))) {
1608 "LQ: stay is expired %d %d %d\n",
1609 lq_sta->total_failed,
1610 lq_sta->total_success,
1611 flush_interval_passed);
1613 /* Allow search for new mode */
1614 lq_sta->stay_in_tbl = 0; /* only place reset */
1615 lq_sta->total_failed = 0;
1616 lq_sta->total_success = 0;
1617 lq_sta->flush_timer = 0;
1619 * Else if we've used this modulation mode enough repetitions
1620 * (regardless of elapsed time or success/failure), reset
1621 * history bitmaps and rate-specific stats for all rates in
1625 lq_sta->table_count++;
1626 if (lq_sta->table_count >=
1627 lq_sta->table_count_limit) {
1628 lq_sta->table_count = 0;
1631 "LQ: stay in table clear win\n");
1632 for (i = 0; i < IWL_RATE_COUNT; i++)
1633 rs_rate_scale_clear_window(
1638 /* If transitioning to allow "search", reset all history
1639 * bitmaps and stats in active table (this will become the new
1640 * "search" table). */
1641 if (!lq_sta->stay_in_tbl) {
1642 for (i = 0; i < IWL_RATE_COUNT; i++)
1643 rs_rate_scale_clear_window(&(tbl->win[i]));
1649 * setup rate table in uCode
1651 static void rs_update_rate_tbl(struct iwl_mvm *mvm,
1652 struct ieee80211_sta *sta,
1653 struct iwl_lq_sta *lq_sta,
1654 struct rs_rate *rate)
1658 ucode_rate = ucode_rate_from_rs_rate(mvm, rate);
1659 rs_fill_link_cmd(mvm, sta, lq_sta, ucode_rate);
1660 iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, false);
1663 static u8 rs_get_tid(struct iwl_lq_sta *lq_data,
1664 struct ieee80211_hdr *hdr)
1666 u8 tid = IWL_MAX_TID_COUNT;
1668 if (ieee80211_is_data_qos(hdr->frame_control)) {
1669 u8 *qc = ieee80211_get_qos_ctl(hdr);
1673 if (unlikely(tid > IWL_MAX_TID_COUNT))
1674 tid = IWL_MAX_TID_COUNT;
1680 * Do rate scaling and search for new modulation mode.
1682 static void rs_rate_scale_perform(struct iwl_mvm *mvm,
1683 struct sk_buff *skb,
1684 struct ieee80211_sta *sta,
1685 struct iwl_lq_sta *lq_sta)
1687 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1688 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1689 int low = IWL_RATE_INVALID;
1690 int high = IWL_RATE_INVALID;
1693 struct iwl_rate_scale_data *window = NULL;
1694 int current_tpt = IWL_INVALID_VALUE;
1695 int low_tpt = IWL_INVALID_VALUE;
1696 int high_tpt = IWL_INVALID_VALUE;
1698 s8 scale_action = 0;
1701 struct iwl_scale_tbl_info *tbl, *tbl1;
1702 u16 rate_scale_index_msk = 0;
1707 u8 tid = IWL_MAX_TID_COUNT;
1708 u8 prev_agg = lq_sta->is_agg;
1709 struct iwl_mvm_sta *sta_priv = (void *)sta->drv_priv;
1710 struct iwl_mvm_tid_data *tid_data;
1711 struct rs_rate *rate;
1713 /* Send management frames and NO_ACK data using lowest rate. */
1714 /* TODO: this could probably be improved.. */
1715 if (!ieee80211_is_data(hdr->frame_control) ||
1716 info->flags & IEEE80211_TX_CTL_NO_ACK)
1719 lq_sta->supp_rates = sta->supp_rates[lq_sta->band];
1721 tid = rs_get_tid(lq_sta, hdr);
1722 if ((tid != IWL_MAX_TID_COUNT) &&
1723 (lq_sta->tx_agg_tid_en & (1 << tid))) {
1724 tid_data = &sta_priv->tid_data[tid];
1725 if (tid_data->state == IWL_AGG_OFF)
1734 * Select rate-scale / modulation-mode table to work with in
1735 * the rest of this function: "search" if searching for better
1736 * modulation mode, or "active" if doing rate scaling within a mode.
1738 if (!lq_sta->search_better_tbl)
1739 active_tbl = lq_sta->active_tbl;
1741 active_tbl = 1 - lq_sta->active_tbl;
1743 tbl = &(lq_sta->lq_info[active_tbl]);
1746 if (prev_agg != lq_sta->is_agg) {
1748 "Aggregation changed: prev %d current %d. Update expected TPT table\n",
1749 prev_agg, lq_sta->is_agg);
1750 rs_set_expected_tpt_table(lq_sta, tbl);
1753 /* current tx rate */
1754 index = lq_sta->last_txrate_idx;
1756 /* rates available for this association, and for modulation mode */
1757 rate_mask = rs_get_supported_rates(lq_sta, rate);
1759 /* mask with station rate restriction */
1760 if (is_legacy(rate)) {
1761 if (lq_sta->band == IEEE80211_BAND_5GHZ)
1762 /* supp_rates has no CCK bits in A mode */
1763 rate_scale_index_msk = (u16) (rate_mask &
1764 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
1766 rate_scale_index_msk = (u16) (rate_mask &
1767 lq_sta->supp_rates);
1770 rate_scale_index_msk = rate_mask;
1773 if (!rate_scale_index_msk)
1774 rate_scale_index_msk = rate_mask;
1776 if (!((1 << index) & rate_scale_index_msk)) {
1777 IWL_ERR(mvm, "Current Rate is not valid\n");
1778 if (lq_sta->search_better_tbl) {
1779 /* revert to active table if search table is not valid*/
1780 rate->type = LQ_NONE;
1781 lq_sta->search_better_tbl = 0;
1782 tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1783 /* get "active" rate info */
1784 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
1785 tbl->rate.index = index;
1786 rs_update_rate_tbl(mvm, sta, lq_sta, &tbl->rate);
1791 /* Get expected throughput table and history window for current rate */
1792 if (!tbl->expected_tpt) {
1793 IWL_ERR(mvm, "tbl->expected_tpt is NULL\n");
1797 /* force user max rate if set by user */
1798 if ((lq_sta->max_rate_idx != -1) &&
1799 (lq_sta->max_rate_idx < index)) {
1800 index = lq_sta->max_rate_idx;
1802 window = &(tbl->win[index]);
1804 "Forcing user max rate %d\n",
1809 window = &(tbl->win[index]);
1812 * If there is not enough history to calculate actual average
1813 * throughput, keep analyzing results of more tx frames, without
1814 * changing rate or mode (bypass most of the rest of this function).
1815 * Set up new rate table in uCode only if old rate is not supported
1816 * in current association (use new rate found above).
1818 fail_count = window->counter - window->success_counter;
1819 if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
1820 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
1822 "(%s: %d): Test Window: succ %d total %d\n",
1823 rs_pretty_lq_type(rate->type),
1824 index, window->success_counter, window->counter);
1826 /* Can't calculate this yet; not enough history */
1827 window->average_tpt = IWL_INVALID_VALUE;
1829 /* Should we stay with this modulation mode,
1830 * or search for a new one? */
1831 rs_stay_in_table(lq_sta, false);
1835 /* Else we have enough samples; calculate estimate of
1836 * actual average throughput */
1837 if (window->average_tpt != ((window->success_ratio *
1838 tbl->expected_tpt[index] + 64) / 128)) {
1839 window->average_tpt = ((window->success_ratio *
1840 tbl->expected_tpt[index] + 64) / 128);
1843 /* If we are searching for better modulation mode, check success. */
1844 if (lq_sta->search_better_tbl) {
1845 /* If good success, continue using the "search" mode;
1846 * no need to send new link quality command, since we're
1847 * continuing to use the setup that we've been trying. */
1848 if (window->average_tpt > lq_sta->last_tpt) {
1850 "SWITCHING TO NEW TABLE SR: %d "
1851 "cur-tpt %d old-tpt %d\n",
1852 window->success_ratio,
1853 window->average_tpt,
1856 if (!is_legacy(rate))
1857 lq_sta->enable_counter = 1;
1859 /* Swap tables; "search" becomes "active" */
1860 lq_sta->active_tbl = active_tbl;
1861 current_tpt = window->average_tpt;
1862 /* Else poor success; go back to mode in "active" table */
1865 "GOING BACK TO THE OLD TABLE: SR %d "
1866 "cur-tpt %d old-tpt %d\n",
1867 window->success_ratio,
1868 window->average_tpt,
1871 /* Nullify "search" table */
1872 rate->type = LQ_NONE;
1874 /* Revert to "active" table */
1875 active_tbl = lq_sta->active_tbl;
1876 tbl = &(lq_sta->lq_info[active_tbl]);
1878 /* Revert to "active" rate and throughput info */
1879 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
1880 current_tpt = lq_sta->last_tpt;
1882 /* Need to set up a new rate table in uCode */
1886 /* Either way, we've made a decision; modulation mode
1887 * search is done, allow rate adjustment next time. */
1888 lq_sta->search_better_tbl = 0;
1889 done_search = 1; /* Don't switch modes below! */
1893 /* (Else) not in search of better modulation mode, try for better
1894 * starting rate, while staying in this mode. */
1895 high_low = rs_get_adjacent_rate(mvm, index, rate_scale_index_msk,
1897 low = high_low & 0xff;
1898 high = (high_low >> 8) & 0xff;
1900 /* If user set max rate, dont allow higher than user constrain */
1901 if ((lq_sta->max_rate_idx != -1) &&
1902 (lq_sta->max_rate_idx < high))
1903 high = IWL_RATE_INVALID;
1905 sr = window->success_ratio;
1907 /* Collect measured throughputs for current and adjacent rates */
1908 current_tpt = window->average_tpt;
1909 if (low != IWL_RATE_INVALID)
1910 low_tpt = tbl->win[low].average_tpt;
1911 if (high != IWL_RATE_INVALID)
1912 high_tpt = tbl->win[high].average_tpt;
1915 "(%s: %d): cur_tpt %d SR %d low %d high %d low_tpt %d high_tpt %d\n",
1916 rs_pretty_lq_type(rate->type), index, current_tpt, sr,
1917 low, high, low_tpt, high_tpt);
1921 /* Too many failures, decrease rate */
1922 if ((sr <= RS_SR_FORCE_DECREASE) || (current_tpt == 0)) {
1924 "decrease rate because of low SR\n");
1926 /* No throughput measured yet for adjacent rates; try increase. */
1927 } else if ((low_tpt == IWL_INVALID_VALUE) &&
1928 (high_tpt == IWL_INVALID_VALUE)) {
1929 if (high != IWL_RATE_INVALID && sr >= IWL_RATE_INCREASE_TH) {
1931 "Good SR and no high rate measurement. "
1934 } else if (low != IWL_RATE_INVALID) {
1936 "Remain in current rate\n");
1941 /* Both adjacent throughputs are measured, but neither one has better
1942 * throughput; we're using the best rate, don't change it! */
1943 else if ((low_tpt != IWL_INVALID_VALUE) &&
1944 (high_tpt != IWL_INVALID_VALUE) &&
1945 (low_tpt < current_tpt) &&
1946 (high_tpt < current_tpt)) {
1948 "Both high and low are worse. "
1953 /* At least one adjacent rate's throughput is measured,
1954 * and may have better performance. */
1956 /* Higher adjacent rate's throughput is measured */
1957 if (high_tpt != IWL_INVALID_VALUE) {
1958 /* Higher rate has better throughput */
1959 if (high_tpt > current_tpt &&
1960 sr >= IWL_RATE_INCREASE_TH) {
1962 "Higher rate is better and good "
1963 "SR. Increate rate\n");
1967 "Higher rate isn't better OR "
1968 "no good SR. Maintain rate\n");
1972 /* Lower adjacent rate's throughput is measured */
1973 } else if (low_tpt != IWL_INVALID_VALUE) {
1974 /* Lower rate has better throughput */
1975 if (low_tpt > current_tpt) {
1977 "Lower rate is better. "
1980 } else if (sr >= IWL_RATE_INCREASE_TH) {
1982 "Lower rate isn't better and "
1983 "good SR. Increase rate\n");
1989 /* Sanity check; asked for decrease, but success rate or throughput
1990 * has been good at old rate. Don't change it. */
1991 if ((scale_action == -1) && (low != IWL_RATE_INVALID) &&
1992 ((sr > IWL_RATE_HIGH_TH) ||
1993 (current_tpt > (100 * tbl->expected_tpt[low])))) {
1995 "Sanity check failed. Maintain rate\n");
1999 /* Force a search in case BT doesn't like us being in MIMO */
2000 if (is_mimo(rate) &&
2001 !iwl_mvm_bt_coex_is_mimo_allowed(mvm, sta)) {
2003 "BT Coex forbids MIMO. Search for new config\n");
2004 rs_stay_in_table(lq_sta, true);
2008 switch (scale_action) {
2010 /* Decrease starting rate, update uCode's rate table */
2011 if (low != IWL_RATE_INVALID) {
2016 "At the bottom rate. Can't decrease\n");
2021 /* Increase starting rate, update uCode's rate table */
2022 if (high != IWL_RATE_INVALID) {
2027 "At the top rate. Can't increase\n");
2038 /* Replace uCode's rate table for the destination station. */
2040 tbl->rate.index = index;
2041 rs_update_rate_tbl(mvm, sta, lq_sta, &tbl->rate);
2044 rs_stay_in_table(lq_sta, false);
2047 * Search for new modulation mode if we're:
2048 * 1) Not changing rates right now
2049 * 2) Not just finishing up a search
2050 * 3) Allowing a new search
2052 if (!update_lq && !done_search &&
2053 !lq_sta->stay_in_tbl && window->counter) {
2054 /* Save current throughput to compare with "search" throughput*/
2055 lq_sta->last_tpt = current_tpt;
2058 "Start Search: update_lq %d done_search %d stay_in_tbl %d win->counter %d\n",
2059 update_lq, done_search, lq_sta->stay_in_tbl,
2061 /* Select a new "search" modulation mode to try.
2062 * If one is found, set up the new "search" table. */
2063 if (is_legacy(&tbl->rate))
2064 rs_move_legacy_other(mvm, lq_sta, sta, index);
2065 else if (is_siso(&tbl->rate))
2066 rs_move_siso_to_other(mvm, lq_sta, sta, index);
2067 else if (is_mimo2(&tbl->rate))
2068 rs_move_mimo2_to_other(mvm, lq_sta, sta, index);
2072 /* If new "search" mode was selected, set up in uCode table */
2073 if (lq_sta->search_better_tbl) {
2074 /* Access the "search" table, clear its history. */
2075 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
2076 for (i = 0; i < IWL_RATE_COUNT; i++)
2077 rs_rate_scale_clear_window(&(tbl->win[i]));
2079 /* Use new "search" start rate */
2080 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
2083 "Switch to SEARCH TABLE: "
2084 "mcs %X (%s: %d)\n",
2086 rs_pretty_lq_type(tbl->rate.type),
2088 rs_fill_link_cmd(mvm, sta, lq_sta, tbl->current_rate);
2089 iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, false);
2095 if (done_search && !lq_sta->stay_in_tbl) {
2096 /* If the "active" (non-search) mode was legacy,
2097 * and we've tried switching antennas,
2098 * but we haven't been able to try HT modes (not available),
2099 * stay with best antenna legacy modulation for a while
2100 * before next round of mode comparisons. */
2101 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
2102 if (is_legacy(&tbl1->rate) && !sta->ht_cap.ht_supported &&
2103 lq_sta->action_counter > tbl1->max_search) {
2104 IWL_DEBUG_RATE(mvm, "LQ: STAY in legacy table\n");
2105 rs_set_stay_in_table(mvm, 1, lq_sta);
2108 /* If we're in an HT mode, and all 3 mode switch actions
2109 * have been tried and compared, stay in this best modulation
2110 * mode for a while before next round of mode comparisons. */
2111 if (lq_sta->enable_counter &&
2112 (lq_sta->action_counter >= tbl1->max_search)) {
2113 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2114 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
2115 (tid != IWL_MAX_TID_COUNT)) {
2116 tid_data = &sta_priv->tid_data[tid];
2117 if (tid_data->state == IWL_AGG_OFF) {
2119 "try to aggregate tid %d\n",
2121 rs_tl_turn_on_agg(mvm, tid,
2125 rs_set_stay_in_table(mvm, 0, lq_sta);
2130 tbl->rate.index = index;
2131 tbl->current_rate = ucode_rate_from_rs_rate(mvm, &tbl->rate);
2132 lq_sta->last_txrate_idx = index;
2136 * rs_initialize_lq - Initialize a station's hardware rate table
2138 * The uCode's station table contains a table of fallback rates
2139 * for automatic fallback during transmission.
2141 * NOTE: This sets up a default set of values. These will be replaced later
2142 * if the driver's iwl-agn-rs rate scaling algorithm is used, instead of
2145 * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
2146 * calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
2147 * which requires station table entry to exist).
2149 static void rs_initialize_lq(struct iwl_mvm *mvm,
2150 struct ieee80211_sta *sta,
2151 struct iwl_lq_sta *lq_sta,
2152 enum ieee80211_band band,
2155 struct iwl_scale_tbl_info *tbl;
2156 struct rs_rate *rate;
2162 if (!sta || !lq_sta)
2165 i = lq_sta->last_txrate_idx;
2167 valid_tx_ant = iwl_fw_valid_tx_ant(mvm->fw);
2169 if (!lq_sta->search_better_tbl)
2170 active_tbl = lq_sta->active_tbl;
2172 active_tbl = 1 - lq_sta->active_tbl;
2174 tbl = &(lq_sta->lq_info[active_tbl]);
2177 if ((i < 0) || (i >= IWL_RATE_COUNT))
2180 ucode_rate = iwl_rates[i].plcp;
2181 rate->ant = first_antenna(valid_tx_ant);
2182 ucode_rate |= rate->ant << RATE_MCS_ANT_POS;
2184 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
2185 ucode_rate |= RATE_MCS_CCK_MSK;
2187 rs_rate_from_ucode_rate(ucode_rate, band, rate);
2188 if (!rs_is_valid_ant(valid_tx_ant, rate->ant))
2189 rs_toggle_antenna(valid_tx_ant, &ucode_rate, rate);
2191 ucode_rate = ucode_rate_from_rs_rate(mvm, rate);
2192 tbl->current_rate = ucode_rate;
2193 rs_set_expected_tpt_table(lq_sta, tbl);
2194 rs_fill_link_cmd(NULL, NULL, lq_sta, ucode_rate);
2195 /* TODO restore station should remember the lq cmd */
2196 iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, init);
2199 static void rs_get_rate(void *mvm_r, struct ieee80211_sta *sta, void *mvm_sta,
2200 struct ieee80211_tx_rate_control *txrc)
2202 struct sk_buff *skb = txrc->skb;
2203 struct ieee80211_supported_band *sband = txrc->sband;
2204 struct iwl_op_mode *op_mode __maybe_unused =
2205 (struct iwl_op_mode *)mvm_r;
2206 struct iwl_mvm *mvm __maybe_unused = IWL_OP_MODE_GET_MVM(op_mode);
2207 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2208 struct iwl_lq_sta *lq_sta = mvm_sta;
2210 /* Get max rate if user set max rate */
2212 lq_sta->max_rate_idx = txrc->max_rate_idx;
2213 if ((sband->band == IEEE80211_BAND_5GHZ) &&
2214 (lq_sta->max_rate_idx != -1))
2215 lq_sta->max_rate_idx += IWL_FIRST_OFDM_RATE;
2216 if ((lq_sta->max_rate_idx < 0) ||
2217 (lq_sta->max_rate_idx >= IWL_RATE_COUNT))
2218 lq_sta->max_rate_idx = -1;
2221 /* Treat uninitialized rate scaling data same as non-existing. */
2222 if (lq_sta && !lq_sta->drv) {
2223 IWL_DEBUG_RATE(mvm, "Rate scaling not initialized yet.\n");
2227 /* Send management frames and NO_ACK data using lowest rate. */
2228 if (rate_control_send_low(sta, mvm_sta, txrc))
2231 iwl_mvm_hwrate_to_tx_rate(lq_sta->last_rate_n_flags,
2232 info->band, &info->control.rates[0]);
2234 info->control.rates[0].count = 1;
2237 static void *rs_alloc_sta(void *mvm_rate, struct ieee80211_sta *sta,
2240 struct iwl_mvm_sta *sta_priv = (struct iwl_mvm_sta *)sta->drv_priv;
2241 struct iwl_op_mode *op_mode __maybe_unused =
2242 (struct iwl_op_mode *)mvm_rate;
2243 struct iwl_mvm *mvm __maybe_unused = IWL_OP_MODE_GET_MVM(op_mode);
2245 IWL_DEBUG_RATE(mvm, "create station rate scale window\n");
2247 return &sta_priv->lq_sta;
2250 static int rs_vht_highest_rx_mcs_index(struct ieee80211_sta_vht_cap *vht_cap,
2253 u16 rx_mcs = le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map) &
2254 (0x3 << (2 * (nss - 1)));
2255 rx_mcs >>= (2 * (nss - 1));
2257 if (rx_mcs == IEEE80211_VHT_MCS_SUPPORT_0_7)
2258 return IWL_RATE_MCS_7_INDEX;
2259 else if (rx_mcs == IEEE80211_VHT_MCS_SUPPORT_0_8)
2260 return IWL_RATE_MCS_8_INDEX;
2261 else if (rx_mcs == IEEE80211_VHT_MCS_SUPPORT_0_9)
2262 return IWL_RATE_MCS_9_INDEX;
2264 WARN_ON_ONCE(rx_mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED);
2268 static void rs_vht_set_enabled_rates(struct ieee80211_sta *sta,
2269 struct ieee80211_sta_vht_cap *vht_cap,
2270 struct iwl_lq_sta *lq_sta)
2273 int highest_mcs = rs_vht_highest_rx_mcs_index(vht_cap, 1);
2275 if (highest_mcs >= IWL_RATE_MCS_0_INDEX) {
2276 for (i = IWL_RATE_MCS_0_INDEX; i <= highest_mcs; i++) {
2277 if (i == IWL_RATE_9M_INDEX)
2280 /* VHT MCS9 isn't valid for 20Mhz for NSS=1,2 */
2281 if (i == IWL_RATE_MCS_9_INDEX &&
2282 sta->bandwidth == IEEE80211_STA_RX_BW_20)
2285 lq_sta->active_siso_rate |= BIT(i);
2289 if (sta->rx_nss < 2)
2292 highest_mcs = rs_vht_highest_rx_mcs_index(vht_cap, 2);
2293 if (highest_mcs >= IWL_RATE_MCS_0_INDEX) {
2294 for (i = IWL_RATE_MCS_0_INDEX; i <= highest_mcs; i++) {
2295 if (i == IWL_RATE_9M_INDEX)
2298 /* VHT MCS9 isn't valid for 20Mhz for NSS=1,2 */
2299 if (i == IWL_RATE_MCS_9_INDEX &&
2300 sta->bandwidth == IEEE80211_STA_RX_BW_20)
2303 lq_sta->active_mimo2_rate |= BIT(i);
2309 * Called after adding a new station to initialize rate scaling
2311 void iwl_mvm_rs_rate_init(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2312 enum ieee80211_band band, bool init)
2315 struct ieee80211_hw *hw = mvm->hw;
2316 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
2317 struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
2318 struct iwl_mvm_sta *sta_priv;
2319 struct iwl_lq_sta *lq_sta;
2320 struct ieee80211_supported_band *sband;
2321 unsigned long supp; /* must be unsigned long for for_each_set_bit */
2323 sta_priv = (struct iwl_mvm_sta *)sta->drv_priv;
2324 lq_sta = &sta_priv->lq_sta;
2325 memset(lq_sta, 0, sizeof(*lq_sta));
2327 sband = hw->wiphy->bands[band];
2329 lq_sta->lq.sta_id = sta_priv->sta_id;
2331 for (j = 0; j < LQ_SIZE; j++)
2332 for (i = 0; i < IWL_RATE_COUNT; i++)
2333 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
2335 lq_sta->flush_timer = 0;
2336 lq_sta->supp_rates = sta->supp_rates[sband->band];
2339 "LQ: *** rate scale station global init for station %d ***\n",
2341 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2342 * the lowest or the highest rate.. Could consider using RSSI from
2343 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2346 lq_sta->max_rate_idx = -1;
2347 lq_sta->missed_rate_counter = IWL_MISSED_RATE_MAX;
2348 lq_sta->band = sband->band;
2350 * active legacy rates as per supported rates bitmap
2352 supp = sta->supp_rates[sband->band];
2353 lq_sta->active_legacy_rate = 0;
2354 for_each_set_bit(i, &supp, BITS_PER_LONG)
2355 lq_sta->active_legacy_rate |= BIT(sband->bitrates[i].hw_value);
2357 /* TODO: should probably account for rx_highest for both HT/VHT */
2358 if (!vht_cap || !vht_cap->vht_supported) {
2359 /* active_siso_rate mask includes 9 MBits (bit 5),
2360 * and CCK (bits 0-3), supp_rates[] does not;
2361 * shift to convert format, force 9 MBits off.
2363 lq_sta->active_siso_rate = ht_cap->mcs.rx_mask[0] << 1;
2364 lq_sta->active_siso_rate |= ht_cap->mcs.rx_mask[0] & 0x1;
2365 lq_sta->active_siso_rate &= ~((u16)0x2);
2366 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
2369 lq_sta->active_mimo2_rate = ht_cap->mcs.rx_mask[1] << 1;
2370 lq_sta->active_mimo2_rate |= ht_cap->mcs.rx_mask[1] & 0x1;
2371 lq_sta->active_mimo2_rate &= ~((u16)0x2);
2372 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2374 lq_sta->is_vht = false;
2376 rs_vht_set_enabled_rates(sta, vht_cap, lq_sta);
2377 lq_sta->is_vht = true;
2381 "SISO-RATE=%X MIMO2-RATE=%X VHT=%d\n",
2382 lq_sta->active_siso_rate,
2383 lq_sta->active_mimo2_rate,
2386 /* These values will be overridden later */
2387 lq_sta->lq.single_stream_ant_msk =
2388 first_antenna(iwl_fw_valid_tx_ant(mvm->fw));
2389 lq_sta->lq.dual_stream_ant_msk =
2390 iwl_fw_valid_tx_ant(mvm->fw) &
2391 ~first_antenna(iwl_fw_valid_tx_ant(mvm->fw));
2392 if (!lq_sta->lq.dual_stream_ant_msk) {
2393 lq_sta->lq.dual_stream_ant_msk = ANT_AB;
2394 } else if (num_of_ant(iwl_fw_valid_tx_ant(mvm->fw)) == 2) {
2395 lq_sta->lq.dual_stream_ant_msk =
2396 iwl_fw_valid_tx_ant(mvm->fw);
2399 /* as default allow aggregation for all tids */
2400 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
2403 /* Set last_txrate_idx to lowest rate */
2404 lq_sta->last_txrate_idx = rate_lowest_index(sband, sta);
2405 if (sband->band == IEEE80211_BAND_5GHZ)
2406 lq_sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
2408 #ifdef CONFIG_MAC80211_DEBUGFS
2409 lq_sta->dbg_fixed_rate = 0;
2412 rs_initialize_lq(mvm, sta, lq_sta, band, init);
2415 static void rs_rate_update(void *mvm_r,
2416 struct ieee80211_supported_band *sband,
2417 struct cfg80211_chan_def *chandef,
2418 struct ieee80211_sta *sta, void *priv_sta,
2422 struct iwl_op_mode *op_mode =
2423 (struct iwl_op_mode *)mvm_r;
2424 struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
2426 /* Stop any ongoing aggregations as rs starts off assuming no agg */
2427 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++)
2428 ieee80211_stop_tx_ba_session(sta, tid);
2430 iwl_mvm_rs_rate_init(mvm, sta, sband->band, false);
2433 static void rs_fill_link_cmd(struct iwl_mvm *mvm,
2434 struct ieee80211_sta *sta,
2435 struct iwl_lq_sta *lq_sta, u32 new_rate)
2437 struct rs_rate rate;
2439 int repeat_rate = 0;
2440 u8 ant_toggle_cnt = 0;
2441 u8 use_ht_possible = 1;
2442 u8 valid_tx_ant = 0;
2443 struct iwl_lq_cmd *lq_cmd = &lq_sta->lq;
2445 /* Override starting rate (index 0) if needed for debug purposes */
2446 rs_dbgfs_set_mcs(lq_sta, &new_rate);
2448 rs_rate_from_ucode_rate(new_rate, lq_sta->band, &rate);
2450 /* How many times should we repeat the initial rate? */
2451 if (is_legacy(&rate)) {
2453 repeat_rate = IWL_NUMBER_TRY;
2455 repeat_rate = min(IWL_HT_NUMBER_TRY,
2456 LINK_QUAL_AGG_DISABLE_START_DEF - 1);
2459 lq_cmd->mimo_delim = is_mimo(&rate) ? 1 : 0;
2461 /* Fill 1st table entry (index 0) */
2462 lq_cmd->rs_table[index] = cpu_to_le32(new_rate);
2464 if (num_of_ant(rate.ant) == 1)
2465 lq_cmd->single_stream_ant_msk = rate.ant;
2466 else if (num_of_ant(rate.ant) == 2)
2467 lq_cmd->dual_stream_ant_msk = rate.ant;
2468 /* otherwise we don't modify the existing value */
2473 valid_tx_ant = iwl_fw_valid_tx_ant(mvm->fw);
2475 /* Fill rest of rate table */
2476 while (index < LINK_QUAL_MAX_RETRY_NUM) {
2477 /* Repeat initial/next rate.
2478 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2479 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
2480 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
2481 if (is_legacy(&rate)) {
2482 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2485 rs_toggle_antenna(valid_tx_ant,
2490 /* Override next rate if needed for debug purposes */
2491 rs_dbgfs_set_mcs(lq_sta, &new_rate);
2493 /* Fill next table entry */
2494 lq_cmd->rs_table[index] =
2495 cpu_to_le32(new_rate);
2500 rs_rate_from_ucode_rate(new_rate, lq_sta->band, &rate);
2502 /* Indicate to uCode which entries might be MIMO.
2503 * If initial rate was MIMO, this will finally end up
2504 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
2506 lq_cmd->mimo_delim = index;
2509 new_rate = rs_get_lower_rate(lq_sta, &rate, rate.index,
2512 /* How many times should we repeat the next rate? */
2513 if (is_legacy(&rate)) {
2514 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2517 rs_toggle_antenna(valid_tx_ant,
2521 repeat_rate = IWL_NUMBER_TRY;
2523 repeat_rate = IWL_HT_NUMBER_TRY;
2526 /* Don't allow HT rates after next pass.
2527 * rs_get_lower_rate() will change type to LQ_LEGACY_A
2530 use_ht_possible = 0;
2532 /* Override next rate if needed for debug purposes */
2533 rs_dbgfs_set_mcs(lq_sta, &new_rate);
2535 /* Fill next table entry */
2536 lq_cmd->rs_table[index] = cpu_to_le32(new_rate);
2542 lq_cmd->agg_frame_cnt_limit = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
2543 lq_cmd->agg_disable_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
2545 lq_cmd->agg_time_limit =
2546 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
2549 lq_cmd->agg_time_limit =
2550 cpu_to_le16(iwl_mvm_bt_coex_agg_time_limit(mvm, sta));
2553 static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
2557 /* rate scale requires free function to be implemented */
2558 static void rs_free(void *mvm_rate)
2563 static void rs_free_sta(void *mvm_r, struct ieee80211_sta *sta,
2566 struct iwl_op_mode *op_mode __maybe_unused = mvm_r;
2567 struct iwl_mvm *mvm __maybe_unused = IWL_OP_MODE_GET_MVM(op_mode);
2569 IWL_DEBUG_RATE(mvm, "enter\n");
2570 IWL_DEBUG_RATE(mvm, "leave\n");
2573 #ifdef CONFIG_MAC80211_DEBUGFS
2574 static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
2577 struct iwl_mvm *mvm;
2582 valid_tx_ant = iwl_fw_valid_tx_ant(mvm->fw);
2583 if (lq_sta->dbg_fixed_rate) {
2585 ((lq_sta->dbg_fixed_rate & RATE_MCS_ANT_ABC_MSK)
2586 >> RATE_MCS_ANT_POS);
2587 if ((valid_tx_ant & ant_sel_tx) == ant_sel_tx) {
2588 *rate_n_flags = lq_sta->dbg_fixed_rate;
2590 lq_sta->dbg_fixed_rate = 0;
2592 "Invalid antenna selection 0x%X, Valid is 0x%X\n",
2593 ant_sel_tx, valid_tx_ant);
2594 IWL_DEBUG_RATE(mvm, "Fixed rate OFF\n");
2599 static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
2600 const char __user *user_buf, size_t count, loff_t *ppos)
2602 struct iwl_lq_sta *lq_sta = file->private_data;
2603 struct iwl_mvm *mvm;
2610 memset(buf, 0, sizeof(buf));
2611 buf_size = min(count, sizeof(buf) - 1);
2612 if (copy_from_user(buf, user_buf, buf_size))
2615 if (sscanf(buf, "%x", &parsed_rate) == 1)
2616 lq_sta->dbg_fixed_rate = parsed_rate;
2618 lq_sta->dbg_fixed_rate = 0;
2620 rs_program_fix_rate(mvm, lq_sta);
2625 static const char *rs_pretty_lq_type(enum iwl_table_type type)
2627 static const char * const lq_types[] = {
2629 [LQ_LEGACY_A] = "LEGACY_A",
2630 [LQ_LEGACY_G] = "LEGACY_G",
2631 [LQ_HT_SISO] = "HT SISO",
2632 [LQ_HT_MIMO2] = "HT MIMO",
2633 [LQ_VHT_SISO] = "VHT SISO",
2634 [LQ_VHT_MIMO2] = "VHT MIMO",
2637 if (type < LQ_NONE || type >= LQ_MAX)
2640 return lq_types[type];
2643 static const char *rs_pretty_ant(u8 ant)
2645 static const char * const ant_name[] = {
2646 [ANT_NONE] = "None",
2659 return ant_name[ant];
2662 static int rs_pretty_print_rate(char *buf, const u32 rate)
2666 u8 mcs = 0, nss = 0;
2667 u8 ant = (rate & RATE_MCS_ANT_ABC_MSK) >> RATE_MCS_ANT_POS;
2669 if (!(rate & RATE_MCS_HT_MSK) &&
2670 !(rate & RATE_MCS_VHT_MSK)) {
2671 int index = iwl_hwrate_to_plcp_idx(rate);
2673 return sprintf(buf, "Legacy | ANT: %s Rate: %s Mbps\n",
2674 rs_pretty_ant(ant), iwl_rate_mcs[index].mbps);
2677 if (rate & RATE_MCS_VHT_MSK) {
2679 mcs = rate & RATE_VHT_MCS_RATE_CODE_MSK;
2680 nss = ((rate & RATE_VHT_MCS_NSS_MSK)
2681 >> RATE_VHT_MCS_NSS_POS) + 1;
2682 } else if (rate & RATE_MCS_HT_MSK) {
2684 mcs = rate & RATE_HT_MCS_INDEX_MSK;
2686 type = "Unknown"; /* shouldn't happen */
2689 switch (rate & RATE_MCS_CHAN_WIDTH_MSK) {
2690 case RATE_MCS_CHAN_WIDTH_20:
2693 case RATE_MCS_CHAN_WIDTH_40:
2696 case RATE_MCS_CHAN_WIDTH_80:
2699 case RATE_MCS_CHAN_WIDTH_160:
2706 return sprintf(buf, "%s | ANT: %s BW: %s MCS: %d NSS: %d %s%s%s%s%s\n",
2707 type, rs_pretty_ant(ant), bw, mcs, nss,
2708 (rate & RATE_MCS_SGI_MSK) ? "SGI " : "NGI ",
2709 (rate & RATE_MCS_STBC_MSK) ? "STBC " : "",
2710 (rate & RATE_MCS_LDPC_MSK) ? "LDPC " : "",
2711 (rate & RATE_MCS_BF_MSK) ? "BF " : "",
2712 (rate & RATE_MCS_ZLF_MSK) ? "ZLF " : "");
2715 static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
2716 char __user *user_buf, size_t count, loff_t *ppos)
2723 struct iwl_lq_sta *lq_sta = file->private_data;
2724 struct iwl_mvm *mvm;
2725 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
2726 struct rs_rate *rate = &tbl->rate;
2728 buff = kmalloc(2048, GFP_KERNEL);
2732 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
2733 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
2734 lq_sta->total_failed, lq_sta->total_success,
2735 lq_sta->active_legacy_rate);
2736 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
2737 lq_sta->dbg_fixed_rate);
2738 desc += sprintf(buff+desc, "valid_tx_ant %s%s%s\n",
2739 (iwl_fw_valid_tx_ant(mvm->fw) & ANT_A) ? "ANT_A," : "",
2740 (iwl_fw_valid_tx_ant(mvm->fw) & ANT_B) ? "ANT_B," : "",
2741 (iwl_fw_valid_tx_ant(mvm->fw) & ANT_C) ? "ANT_C" : "");
2742 desc += sprintf(buff+desc, "lq type %s\n",
2743 (is_legacy(rate)) ? "legacy" :
2744 is_vht(rate) ? "VHT" : "HT");
2745 if (!is_legacy(rate)) {
2746 desc += sprintf(buff+desc, " %s",
2747 (is_siso(rate)) ? "SISO" : "MIMO2");
2748 desc += sprintf(buff+desc, " %s",
2749 (is_ht20(rate)) ? "20MHz" :
2750 (is_ht40(rate)) ? "40MHz" :
2751 (is_ht80(rate)) ? "80Mhz" : "BAD BW");
2752 desc += sprintf(buff+desc, " %s %s\n",
2753 (rate->sgi) ? "SGI" : "NGI",
2754 (lq_sta->is_agg) ? "AGG on" : "");
2756 desc += sprintf(buff+desc, "last tx rate=0x%X\n",
2757 lq_sta->last_rate_n_flags);
2758 desc += sprintf(buff+desc,
2759 "general: flags=0x%X mimo-d=%d s-ant=0x%x d-ant=0x%x\n",
2761 lq_sta->lq.mimo_delim,
2762 lq_sta->lq.single_stream_ant_msk,
2763 lq_sta->lq.dual_stream_ant_msk);
2765 desc += sprintf(buff+desc,
2766 "agg: time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
2767 le16_to_cpu(lq_sta->lq.agg_time_limit),
2768 lq_sta->lq.agg_disable_start_th,
2769 lq_sta->lq.agg_frame_cnt_limit);
2771 desc += sprintf(buff+desc,
2772 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
2773 lq_sta->lq.initial_rate_index[0],
2774 lq_sta->lq.initial_rate_index[1],
2775 lq_sta->lq.initial_rate_index[2],
2776 lq_sta->lq.initial_rate_index[3]);
2778 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
2779 u32 rate = le32_to_cpu(lq_sta->lq.rs_table[i]);
2780 desc += sprintf(buff+desc,
2784 desc += rs_pretty_print_rate(buff+desc, rate);
2787 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2792 static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
2793 .write = rs_sta_dbgfs_scale_table_write,
2794 .read = rs_sta_dbgfs_scale_table_read,
2795 .open = simple_open,
2796 .llseek = default_llseek,
2798 static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
2799 char __user *user_buf, size_t count, loff_t *ppos)
2805 struct iwl_scale_tbl_info *tbl;
2806 struct rs_rate *rate;
2807 struct iwl_lq_sta *lq_sta = file->private_data;
2809 buff = kmalloc(1024, GFP_KERNEL);
2813 for (i = 0; i < LQ_SIZE; i++) {
2814 tbl = &(lq_sta->lq_info[i]);
2816 desc += sprintf(buff+desc,
2817 "%s type=%d SGI=%d BW=%s DUP=0\n"
2819 lq_sta->active_tbl == i ? "*" : "x",
2822 is_ht20(rate) ? "20Mhz" :
2823 is_ht40(rate) ? "40Mhz" :
2824 is_ht80(rate) ? "80Mhz" : "ERR",
2826 for (j = 0; j < IWL_RATE_COUNT; j++) {
2827 desc += sprintf(buff+desc,
2828 "counter=%d success=%d %%=%d\n",
2829 tbl->win[j].counter,
2830 tbl->win[j].success_counter,
2831 tbl->win[j].success_ratio);
2834 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2839 static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
2840 .read = rs_sta_dbgfs_stats_table_read,
2841 .open = simple_open,
2842 .llseek = default_llseek,
2845 static void rs_add_debugfs(void *mvm, void *mvm_sta, struct dentry *dir)
2847 struct iwl_lq_sta *lq_sta = mvm_sta;
2848 lq_sta->rs_sta_dbgfs_scale_table_file =
2849 debugfs_create_file("rate_scale_table", S_IRUSR | S_IWUSR, dir,
2850 lq_sta, &rs_sta_dbgfs_scale_table_ops);
2851 lq_sta->rs_sta_dbgfs_stats_table_file =
2852 debugfs_create_file("rate_stats_table", S_IRUSR, dir,
2853 lq_sta, &rs_sta_dbgfs_stats_table_ops);
2854 lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
2855 debugfs_create_u8("tx_agg_tid_enable", S_IRUSR | S_IWUSR, dir,
2856 &lq_sta->tx_agg_tid_en);
2859 static void rs_remove_debugfs(void *mvm, void *mvm_sta)
2861 struct iwl_lq_sta *lq_sta = mvm_sta;
2862 debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
2863 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
2864 debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
2869 * Initialization of rate scaling information is done by driver after
2870 * the station is added. Since mac80211 calls this function before a
2871 * station is added we ignore it.
2873 static void rs_rate_init_stub(void *mvm_r,
2874 struct ieee80211_supported_band *sband,
2875 struct cfg80211_chan_def *chandef,
2876 struct ieee80211_sta *sta, void *mvm_sta)
2879 static struct rate_control_ops rs_mvm_ops = {
2882 .tx_status = rs_tx_status,
2883 .get_rate = rs_get_rate,
2884 .rate_init = rs_rate_init_stub,
2887 .alloc_sta = rs_alloc_sta,
2888 .free_sta = rs_free_sta,
2889 .rate_update = rs_rate_update,
2890 #ifdef CONFIG_MAC80211_DEBUGFS
2891 .add_sta_debugfs = rs_add_debugfs,
2892 .remove_sta_debugfs = rs_remove_debugfs,
2896 int iwl_mvm_rate_control_register(void)
2898 return ieee80211_rate_control_register(&rs_mvm_ops);
2901 void iwl_mvm_rate_control_unregister(void)
2903 ieee80211_rate_control_unregister(&rs_mvm_ops);
2907 * iwl_mvm_tx_protection - Gets LQ command, change it to enable/disable
2908 * Tx protection, according to this rquest and previous requests,
2909 * and send the LQ command.
2910 * @mvmsta: The station
2911 * @enable: Enable Tx protection?
2913 int iwl_mvm_tx_protection(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
2916 struct iwl_lq_cmd *lq = &mvmsta->lq_sta.lq;
2918 lockdep_assert_held(&mvm->mutex);
2921 if (mvmsta->tx_protection == 0)
2922 lq->flags |= LQ_FLAG_USE_RTS_MSK;
2923 mvmsta->tx_protection++;
2925 mvmsta->tx_protection--;
2926 if (mvmsta->tx_protection == 0)
2927 lq->flags &= ~LQ_FLAG_USE_RTS_MSK;
2930 return iwl_mvm_send_lq_cmd(mvm, lq, false);