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 6 /* 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 /* max time to accum history 2 seconds */
56 #define IWL_RATE_SCALE_FLUSH_INTVL (3*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 iwl_lq_sta
*lq_sta
, u32 rate_n_flags
);
175 static void rs_stay_in_table(struct iwl_lq_sta
*lq_sta
, bool force_search
);
178 #ifdef CONFIG_MAC80211_DEBUGFS
179 static void rs_dbgfs_set_mcs(struct iwl_lq_sta
*lq_sta
,
182 static void rs_dbgfs_set_mcs(struct iwl_lq_sta
*lq_sta
,
188 * The following tables contain the expected throughput metrics for all rates
190 * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
192 * where invalid entries are zeros.
194 * CCK rates are only valid in legacy table and will only be used in G
198 static s32 expected_tpt_legacy
[IWL_RATE_COUNT
] = {
199 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 0, 0, 0
202 /* Expected TpT tables. 4 indexes:
203 * 0 - NGI, 1 - SGI, 2 - AGG+NGI, 3 - AGG+SGI
205 static s32 expected_tpt_siso_20MHz
[4][IWL_RATE_COUNT
] = {
206 {0, 0, 0, 0, 42, 0, 76, 102, 124, 159, 183, 193, 202, 216, 0},
207 {0, 0, 0, 0, 46, 0, 82, 110, 132, 168, 192, 202, 210, 225, 0},
208 {0, 0, 0, 0, 49, 0, 97, 145, 192, 285, 375, 420, 464, 551, 0},
209 {0, 0, 0, 0, 54, 0, 108, 160, 213, 315, 415, 465, 513, 608, 0},
212 static s32 expected_tpt_siso_40MHz
[4][IWL_RATE_COUNT
] = {
213 {0, 0, 0, 0, 77, 0, 127, 160, 184, 220, 242, 250, 257, 269, 275},
214 {0, 0, 0, 0, 83, 0, 135, 169, 193, 229, 250, 257, 264, 275, 280},
215 {0, 0, 0, 0, 101, 0, 199, 295, 389, 570, 744, 828, 911, 1070, 1173},
216 {0, 0, 0, 0, 112, 0, 220, 326, 429, 629, 819, 912, 1000, 1173, 1284},
219 static s32 expected_tpt_siso_80MHz
[4][IWL_RATE_COUNT
] = {
220 {0, 0, 0, 0, 130, 0, 191, 223, 244, 273, 288, 294, 298, 305, 308},
221 {0, 0, 0, 0, 138, 0, 200, 231, 251, 279, 293, 298, 302, 308, 312},
222 {0, 0, 0, 0, 217, 0, 429, 634, 834, 1220, 1585, 1760, 1931, 2258, 2466},
223 {0, 0, 0, 0, 241, 0, 475, 701, 921, 1343, 1741, 1931, 2117, 2468, 2691},
226 static s32 expected_tpt_mimo2_20MHz
[4][IWL_RATE_COUNT
] = {
227 {0, 0, 0, 0, 74, 0, 123, 155, 179, 213, 235, 243, 250, 261, 0},
228 {0, 0, 0, 0, 81, 0, 131, 164, 187, 221, 242, 250, 256, 267, 0},
229 {0, 0, 0, 0, 98, 0, 193, 286, 375, 550, 718, 799, 878, 1032, 0},
230 {0, 0, 0, 0, 109, 0, 214, 316, 414, 607, 790, 879, 965, 1132, 0},
233 static s32 expected_tpt_mimo2_40MHz
[4][IWL_RATE_COUNT
] = {
234 {0, 0, 0, 0, 123, 0, 182, 214, 235, 264, 279, 285, 289, 296, 300},
235 {0, 0, 0, 0, 131, 0, 191, 222, 242, 270, 284, 289, 293, 300, 303},
236 {0, 0, 0, 0, 200, 0, 390, 571, 741, 1067, 1365, 1505, 1640, 1894, 2053},
237 {0, 0, 0, 0, 221, 0, 430, 630, 816, 1169, 1490, 1641, 1784, 2053, 2221},
240 static s32 expected_tpt_mimo2_80MHz
[4][IWL_RATE_COUNT
] = {
241 {0, 0, 0, 0, 182, 0, 240, 264, 278, 299, 308, 311, 313, 317, 319},
242 {0, 0, 0, 0, 190, 0, 247, 269, 282, 302, 310, 313, 315, 319, 320},
243 {0, 0, 0, 0, 428, 0, 833, 1215, 1577, 2254, 2863, 3147, 3418, 3913, 4219},
244 {0, 0, 0, 0, 474, 0, 920, 1338, 1732, 2464, 3116, 3418, 3705, 4225, 4545},
248 static const struct iwl_rate_mcs_info iwl_rate_mcs
[IWL_RATE_COUNT
] = {
257 { "24", "16QAM 1/2"},
258 { "36", "16QAM 3/4"},
259 { "48", "64QAM 2/3"},
260 { "54", "64QAM 3/4"},
261 { "60", "64QAM 5/6"},
264 #define MCS_INDEX_PER_STREAM (8)
266 static void rs_rate_scale_clear_window(struct iwl_rate_scale_data
*window
)
269 window
->success_counter
= 0;
270 window
->success_ratio
= IWL_INVALID_VALUE
;
272 window
->average_tpt
= IWL_INVALID_VALUE
;
276 static inline u8
rs_is_valid_ant(u8 valid_antenna
, u8 ant_type
)
278 return (ant_type
& valid_antenna
) == ant_type
;
281 #ifdef CONFIG_MAC80211_DEBUGFS
283 * Program the device to use fixed rate for frame transmit
284 * This is for debugging/testing only
285 * once the device start use fixed rate, we need to reload the module
286 * to being back the normal operation.
288 static void rs_program_fix_rate(struct iwl_mvm
*mvm
,
289 struct iwl_lq_sta
*lq_sta
)
291 lq_sta
->active_legacy_rate
= 0x0FFF; /* 1 - 54 MBits, includes CCK */
292 lq_sta
->active_siso_rate
= 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
293 lq_sta
->active_mimo2_rate
= 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
295 IWL_DEBUG_RATE(mvm
, "sta_id %d rate 0x%X\n",
296 lq_sta
->lq
.sta_id
, lq_sta
->dbg_fixed_rate
);
298 if (lq_sta
->dbg_fixed_rate
) {
299 rs_fill_link_cmd(NULL
, lq_sta
, lq_sta
->dbg_fixed_rate
);
300 iwl_mvm_send_lq_cmd(lq_sta
->drv
, &lq_sta
->lq
, CMD_ASYNC
, false);
305 static int rs_tl_turn_on_agg_for_tid(struct iwl_mvm
*mvm
,
306 struct iwl_lq_sta
*lq_data
, u8 tid
,
307 struct ieee80211_sta
*sta
)
312 * Don't create TX aggregation sessions when in high
313 * BT traffic, as they would just be disrupted by BT.
315 if (le32_to_cpu(mvm
->last_bt_notif
.bt_activity_grading
) >= 2) {
316 IWL_DEBUG_COEX(mvm
, "BT traffic (%d), no aggregation allowed\n",
317 BT_MBOX_MSG(&mvm
->last_bt_notif
,
322 IWL_DEBUG_HT(mvm
, "Starting Tx agg: STA: %pM tid: %d\n",
324 ret
= ieee80211_start_tx_ba_session(sta
, tid
, 5000);
325 if (ret
== -EAGAIN
) {
327 * driver and mac80211 is out of sync
328 * this might be cause by reloading firmware
329 * stop the tx ba session here
331 IWL_ERR(mvm
, "Fail start Tx agg on tid: %d\n",
333 ieee80211_stop_tx_ba_session(sta
, tid
);
338 static void rs_tl_turn_on_agg(struct iwl_mvm
*mvm
, u8 tid
,
339 struct iwl_lq_sta
*lq_data
,
340 struct ieee80211_sta
*sta
)
342 if (tid
< IWL_MAX_TID_COUNT
)
343 rs_tl_turn_on_agg_for_tid(mvm
, lq_data
, tid
, sta
);
345 IWL_ERR(mvm
, "tid exceeds max TID count: %d/%d\n",
346 tid
, IWL_MAX_TID_COUNT
);
349 static inline int get_num_of_ant_from_rate(u32 rate_n_flags
)
351 return !!(rate_n_flags
& RATE_MCS_ANT_A_MSK
) +
352 !!(rate_n_flags
& RATE_MCS_ANT_B_MSK
) +
353 !!(rate_n_flags
& RATE_MCS_ANT_C_MSK
);
357 * Static function to get the expected throughput from an iwl_scale_tbl_info
358 * that wraps a NULL pointer check
360 static s32
get_expected_tpt(struct iwl_scale_tbl_info
*tbl
, int rs_index
)
362 if (tbl
->expected_tpt
)
363 return tbl
->expected_tpt
[rs_index
];
368 * rs_collect_tx_data - Update the success/failure sliding window
370 * We keep a sliding window of the last 62 packets transmitted
371 * at this rate. window->data contains the bitmask of successful
374 static int rs_collect_tx_data(struct iwl_scale_tbl_info
*tbl
,
375 int scale_index
, int attempts
, int successes
)
377 struct iwl_rate_scale_data
*window
= NULL
;
378 static const u64 mask
= (((u64
)1) << (IWL_RATE_MAX_WINDOW
- 1));
381 if (scale_index
< 0 || scale_index
>= IWL_RATE_COUNT
)
384 /* Select window for current tx bit rate */
385 window
= &(tbl
->win
[scale_index
]);
387 /* Get expected throughput */
388 tpt
= get_expected_tpt(tbl
, scale_index
);
391 * Keep track of only the latest 62 tx frame attempts in this rate's
392 * history window; anything older isn't really relevant any more.
393 * If we have filled up the sliding window, drop the oldest attempt;
394 * if the oldest attempt (highest bit in bitmap) shows "success",
395 * subtract "1" from the success counter (this is the main reason
396 * we keep these bitmaps!).
398 while (attempts
> 0) {
399 if (window
->counter
>= IWL_RATE_MAX_WINDOW
) {
400 /* remove earliest */
401 window
->counter
= IWL_RATE_MAX_WINDOW
- 1;
403 if (window
->data
& mask
) {
404 window
->data
&= ~mask
;
405 window
->success_counter
--;
409 /* Increment frames-attempted counter */
412 /* Shift bitmap by one frame to throw away oldest history */
415 /* Mark the most recent #successes attempts as successful */
417 window
->success_counter
++;
425 /* Calculate current success ratio, avoid divide-by-0! */
426 if (window
->counter
> 0)
427 window
->success_ratio
= 128 * (100 * window
->success_counter
)
430 window
->success_ratio
= IWL_INVALID_VALUE
;
432 fail_count
= window
->counter
- window
->success_counter
;
434 /* Calculate average throughput, if we have enough history. */
435 if ((fail_count
>= IWL_RATE_MIN_FAILURE_TH
) ||
436 (window
->success_counter
>= IWL_RATE_MIN_SUCCESS_TH
))
437 window
->average_tpt
= (window
->success_ratio
* tpt
+ 64) / 128;
439 window
->average_tpt
= IWL_INVALID_VALUE
;
441 /* Tag this window as having been updated */
442 window
->stamp
= jiffies
;
448 * Fill uCode API rate_n_flags field, based on "search" or "active" table.
450 /* FIXME:RS:remove this function and put the flags statically in the table */
451 static u32
rate_n_flags_from_tbl(struct iwl_mvm
*mvm
,
452 struct iwl_scale_tbl_info
*tbl
,
453 int index
, u8 use_green
)
455 u32 rate_n_flags
= 0;
457 rate_n_flags
|= ((tbl
->ant_type
<< RATE_MCS_ANT_POS
) &
458 RATE_MCS_ANT_ABC_MSK
);
460 if (is_legacy(tbl
->lq_type
)) {
461 rate_n_flags
|= iwl_rates
[index
].plcp
;
462 if (index
>= IWL_FIRST_CCK_RATE
&& index
<= IWL_LAST_CCK_RATE
)
463 rate_n_flags
|= RATE_MCS_CCK_MSK
;
467 if (is_ht(tbl
->lq_type
)) {
468 if (index
< IWL_FIRST_HT_RATE
|| index
> IWL_LAST_HT_RATE
) {
469 IWL_ERR(mvm
, "Invalid HT rate index %d\n", index
);
470 index
= IWL_LAST_HT_RATE
;
472 rate_n_flags
|= RATE_MCS_HT_MSK
;
474 if (is_ht_siso(tbl
->lq_type
))
475 rate_n_flags
|= iwl_rates
[index
].plcp_ht_siso
;
476 else if (is_ht_mimo2(tbl
->lq_type
))
477 rate_n_flags
|= iwl_rates
[index
].plcp_ht_mimo2
;
480 } else if (is_vht(tbl
->lq_type
)) {
481 if (index
< IWL_FIRST_VHT_RATE
|| index
> IWL_LAST_VHT_RATE
) {
482 IWL_ERR(mvm
, "Invalid VHT rate index %d\n", index
);
483 index
= IWL_LAST_VHT_RATE
;
485 rate_n_flags
|= RATE_MCS_VHT_MSK
;
486 if (is_vht_siso(tbl
->lq_type
))
487 rate_n_flags
|= iwl_rates
[index
].plcp_vht_siso
;
488 else if (is_vht_mimo2(tbl
->lq_type
))
489 rate_n_flags
|= iwl_rates
[index
].plcp_vht_mimo2
;
494 IWL_ERR(mvm
, "Invalid tbl->lq_type %d\n", tbl
->lq_type
);
497 rate_n_flags
|= tbl
->bw
;
499 rate_n_flags
|= RATE_MCS_SGI_MSK
;
501 /* TODO: remove GF completely ? */
503 rate_n_flags
|= RATE_HT_MCS_GF_MSK
;
504 if (is_ht_siso(tbl
->lq_type
) && tbl
->is_SGI
) {
505 rate_n_flags
&= ~RATE_MCS_SGI_MSK
;
506 IWL_ERR(mvm
, "GF was set with SGI:SISO\n");
513 * Interpret uCode API's rate_n_flags format,
514 * fill "search" or "active" tx mode table.
516 static int rs_get_tbl_info_from_mcs(const u32 rate_n_flags
,
517 enum ieee80211_band band
,
518 struct iwl_scale_tbl_info
*tbl
,
521 u32 ant_msk
= (rate_n_flags
& RATE_MCS_ANT_ABC_MSK
);
522 u8 num_of_ant
= get_num_of_ant_from_rate(rate_n_flags
);
525 memset(tbl
, 0, offsetof(struct iwl_scale_tbl_info
, win
));
526 *rate_idx
= iwl_hwrate_to_plcp_idx(rate_n_flags
);
528 if (*rate_idx
== IWL_RATE_INVALID
) {
532 tbl
->is_SGI
= 0; /* default legacy setup */
534 tbl
->ant_type
= (ant_msk
>> RATE_MCS_ANT_POS
);
535 tbl
->lq_type
= LQ_NONE
;
536 tbl
->max_search
= IWL_MAX_SEARCH
;
539 if (!(rate_n_flags
& RATE_MCS_HT_MSK
) &&
540 !(rate_n_flags
& RATE_MCS_VHT_MSK
)) {
541 if (num_of_ant
== 1) {
542 if (band
== IEEE80211_BAND_5GHZ
)
543 tbl
->lq_type
= LQ_LEGACY_A
;
545 tbl
->lq_type
= LQ_LEGACY_G
;
552 if (rate_n_flags
& RATE_MCS_SGI_MSK
)
555 tbl
->bw
= rate_n_flags
& RATE_MCS_CHAN_WIDTH_MSK
;
557 if (rate_n_flags
& RATE_MCS_HT_MSK
) {
558 nss
= ((rate_n_flags
& RATE_HT_MCS_NSS_MSK
) >>
559 RATE_HT_MCS_NSS_POS
) + 1;
562 tbl
->lq_type
= LQ_HT_SISO
;
563 WARN_ON_ONCE(num_of_ant
!= 1);
564 } else if (nss
== 2) {
565 tbl
->lq_type
= LQ_HT_MIMO2
;
566 WARN_ON_ONCE(num_of_ant
!= 2);
570 } else if (rate_n_flags
& RATE_MCS_VHT_MSK
) {
571 nss
= ((rate_n_flags
& RATE_VHT_MCS_NSS_MSK
) >>
572 RATE_VHT_MCS_NSS_POS
) + 1;
575 tbl
->lq_type
= LQ_VHT_SISO
;
576 WARN_ON_ONCE(num_of_ant
!= 1);
577 } else if (nss
== 2) {
578 tbl
->lq_type
= LQ_VHT_MIMO2
;
579 WARN_ON_ONCE(num_of_ant
!= 2);
585 WARN_ON_ONCE(tbl
->bw
== RATE_MCS_CHAN_WIDTH_160
);
586 WARN_ON_ONCE(tbl
->bw
== RATE_MCS_CHAN_WIDTH_80
&&
587 !is_vht(tbl
->lq_type
));
592 /* switch to another antenna/antennas and return 1 */
593 /* if no other valid antenna found, return 0 */
594 static int rs_toggle_antenna(u32 valid_ant
, u32
*rate_n_flags
,
595 struct iwl_scale_tbl_info
*tbl
)
599 if (!tbl
->ant_type
|| tbl
->ant_type
> ANT_ABC
)
602 if (!rs_is_valid_ant(valid_ant
, tbl
->ant_type
))
605 new_ant_type
= ant_toggle_lookup
[tbl
->ant_type
];
607 while ((new_ant_type
!= tbl
->ant_type
) &&
608 !rs_is_valid_ant(valid_ant
, new_ant_type
))
609 new_ant_type
= ant_toggle_lookup
[new_ant_type
];
611 if (new_ant_type
== tbl
->ant_type
)
614 tbl
->ant_type
= new_ant_type
;
615 *rate_n_flags
&= ~RATE_MCS_ANT_ABC_MSK
;
616 *rate_n_flags
|= new_ant_type
<< RATE_MCS_ANT_POS
;
621 * Green-field mode is valid if the station supports it and
622 * there are no non-GF stations present in the BSS.
624 static bool rs_use_green(struct ieee80211_sta
*sta
)
627 * There's a bug somewhere in this code that causes the
628 * scaling to get stuck because GF+SGI can't be combined
629 * in SISO rates. Until we find that bug, disable GF, it
630 * has only limited benefit and we still interoperate with
631 * GF APs since we can always receive GF transmissions.
637 * rs_get_supported_rates - get the available rates
639 * if management frame or broadcast frame only return
640 * basic available rates.
643 static u16
rs_get_supported_rates(struct iwl_lq_sta
*lq_sta
,
644 struct ieee80211_hdr
*hdr
,
645 enum iwl_table_type rate_type
)
647 if (is_legacy(rate_type
))
648 return lq_sta
->active_legacy_rate
;
649 else if (is_siso(rate_type
))
650 return lq_sta
->active_siso_rate
;
651 else if (is_mimo2(rate_type
))
652 return lq_sta
->active_mimo2_rate
;
658 static u16
rs_get_adjacent_rate(struct iwl_mvm
*mvm
, u8 index
, u16 rate_mask
,
661 u8 high
= IWL_RATE_INVALID
;
662 u8 low
= IWL_RATE_INVALID
;
664 /* 802.11A or ht walks to the next literal adjacent rate in
666 if (is_a_band(rate_type
) || !is_legacy(rate_type
)) {
670 /* Find the previous rate that is in the rate mask */
672 for (mask
= (1 << i
); i
>= 0; i
--, mask
>>= 1) {
673 if (rate_mask
& mask
) {
679 /* Find the next rate that is in the rate mask */
681 for (mask
= (1 << i
); i
< IWL_RATE_COUNT
; i
++, mask
<<= 1) {
682 if (rate_mask
& mask
) {
688 return (high
<< 8) | low
;
692 while (low
!= IWL_RATE_INVALID
) {
693 low
= iwl_rates
[low
].prev_rs
;
694 if (low
== IWL_RATE_INVALID
)
696 if (rate_mask
& (1 << low
))
698 IWL_DEBUG_RATE(mvm
, "Skipping masked lower rate: %d\n", low
);
702 while (high
!= IWL_RATE_INVALID
) {
703 high
= iwl_rates
[high
].next_rs
;
704 if (high
== IWL_RATE_INVALID
)
706 if (rate_mask
& (1 << high
))
708 IWL_DEBUG_RATE(mvm
, "Skipping masked higher rate: %d\n", high
);
711 return (high
<< 8) | low
;
714 static u32
rs_get_lower_rate(struct iwl_lq_sta
*lq_sta
,
715 struct iwl_scale_tbl_info
*tbl
,
716 u8 scale_index
, u8 ht_possible
)
721 u8 switch_to_legacy
= 0;
722 u8 is_green
= lq_sta
->is_green
;
723 struct iwl_mvm
*mvm
= lq_sta
->drv
;
725 /* check if we need to switch from HT to legacy rates.
726 * assumption is that mandatory rates (1Mbps or 6Mbps)
727 * are always supported (spec demand) */
728 if (!is_legacy(tbl
->lq_type
) && (!ht_possible
|| !scale_index
)) {
729 switch_to_legacy
= 1;
730 scale_index
= rs_ht_to_legacy
[scale_index
];
731 if (lq_sta
->band
== IEEE80211_BAND_5GHZ
)
732 tbl
->lq_type
= LQ_LEGACY_A
;
734 tbl
->lq_type
= LQ_LEGACY_G
;
736 if (num_of_ant(tbl
->ant_type
) > 1)
738 first_antenna(iwl_fw_valid_tx_ant(mvm
->fw
));
742 tbl
->max_search
= IWL_MAX_SEARCH
;
745 rate_mask
= rs_get_supported_rates(lq_sta
, NULL
, tbl
->lq_type
);
747 /* Mask with station rate restriction */
748 if (is_legacy(tbl
->lq_type
)) {
749 /* supp_rates has no CCK bits in A mode */
750 if (lq_sta
->band
== IEEE80211_BAND_5GHZ
)
751 rate_mask
= (u16
)(rate_mask
&
752 (lq_sta
->supp_rates
<< IWL_FIRST_OFDM_RATE
));
754 rate_mask
= (u16
)(rate_mask
& lq_sta
->supp_rates
);
757 /* If we switched from HT to legacy, check current rate */
758 if (switch_to_legacy
&& (rate_mask
& (1 << scale_index
))) {
763 high_low
= rs_get_adjacent_rate(lq_sta
->drv
, scale_index
, rate_mask
,
765 low
= high_low
& 0xff;
767 if (low
== IWL_RATE_INVALID
)
771 return rate_n_flags_from_tbl(lq_sta
->drv
, tbl
, low
, is_green
);
775 * Simple function to compare two rate scale table types
777 static bool table_type_matches(struct iwl_scale_tbl_info
*a
,
778 struct iwl_scale_tbl_info
*b
)
780 return (a
->lq_type
== b
->lq_type
) && (a
->ant_type
== b
->ant_type
) &&
781 (a
->is_SGI
== b
->is_SGI
);
784 static u32
rs_ch_width_from_mac_flags(enum mac80211_rate_control_flags flags
)
786 if (flags
& IEEE80211_TX_RC_40_MHZ_WIDTH
)
787 return RATE_MCS_CHAN_WIDTH_40
;
788 else if (flags
& IEEE80211_TX_RC_80_MHZ_WIDTH
)
789 return RATE_MCS_CHAN_WIDTH_80
;
790 else if (flags
& IEEE80211_TX_RC_160_MHZ_WIDTH
)
791 return RATE_MCS_CHAN_WIDTH_160
;
793 return RATE_MCS_CHAN_WIDTH_20
;
797 * mac80211 sends us Tx status
799 static void rs_tx_status(void *mvm_r
, struct ieee80211_supported_band
*sband
,
800 struct ieee80211_sta
*sta
, void *priv_sta
,
805 int rs_index
, mac_index
, i
;
806 struct iwl_lq_sta
*lq_sta
= priv_sta
;
807 struct iwl_lq_cmd
*table
;
808 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*)skb
->data
;
809 struct iwl_op_mode
*op_mode
= (struct iwl_op_mode
*)mvm_r
;
810 struct iwl_mvm
*mvm
= IWL_OP_MODE_GET_MVM(op_mode
);
811 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
812 enum mac80211_rate_control_flags mac_flags
;
814 struct iwl_scale_tbl_info tbl_type
;
815 struct iwl_scale_tbl_info
*curr_tbl
, *other_tbl
, *tmp_tbl
;
817 IWL_DEBUG_RATE_LIMIT(mvm
,
818 "get frame ack response, update rate scale window\n");
820 /* Treat uninitialized rate scaling data same as non-existing. */
822 IWL_DEBUG_RATE(mvm
, "Station rate scaling not created yet.\n");
824 } else if (!lq_sta
->drv
) {
825 IWL_DEBUG_RATE(mvm
, "Rate scaling not initialized yet.\n");
829 if (!ieee80211_is_data(hdr
->frame_control
) ||
830 info
->flags
& IEEE80211_TX_CTL_NO_ACK
)
833 /* This packet was aggregated but doesn't carry status info */
834 if ((info
->flags
& IEEE80211_TX_CTL_AMPDU
) &&
835 !(info
->flags
& IEEE80211_TX_STAT_AMPDU
))
839 * Ignore this Tx frame response if its initial rate doesn't match
840 * that of latest Link Quality command. There may be stragglers
841 * from a previous Link Quality command, but we're no longer interested
842 * in those; they're either from the "active" mode while we're trying
843 * to check "search" mode, or a prior "search" mode after we've moved
844 * to a new "search" mode (which might become the new "active" mode).
847 tx_rate
= le32_to_cpu(table
->rs_table
[0]);
848 rs_get_tbl_info_from_mcs(tx_rate
, info
->band
, &tbl_type
, &rs_index
);
849 if (info
->band
== IEEE80211_BAND_5GHZ
)
850 rs_index
-= IWL_FIRST_OFDM_RATE
;
851 mac_flags
= info
->status
.rates
[0].flags
;
852 mac_index
= info
->status
.rates
[0].idx
;
853 /* For HT packets, map MCS to PLCP */
854 if (mac_flags
& IEEE80211_TX_RC_MCS
) {
855 /* Remove # of streams */
856 mac_index
&= RATE_HT_MCS_RATE_CODE_MSK
;
857 if (mac_index
>= (IWL_RATE_9M_INDEX
- IWL_FIRST_OFDM_RATE
))
860 * mac80211 HT index is always zero-indexed; we need to move
861 * HT OFDM rates after CCK rates in 2.4 GHz band
863 if (info
->band
== IEEE80211_BAND_2GHZ
)
864 mac_index
+= IWL_FIRST_OFDM_RATE
;
865 } else if (mac_flags
& IEEE80211_TX_RC_VHT_MCS
) {
866 mac_index
&= RATE_VHT_MCS_RATE_CODE_MSK
;
867 if (mac_index
>= (IWL_RATE_9M_INDEX
- IWL_FIRST_OFDM_RATE
))
871 /* Here we actually compare this rate to the latest LQ command */
872 if ((mac_index
< 0) ||
873 (tbl_type
.is_SGI
!= !!(mac_flags
& IEEE80211_TX_RC_SHORT_GI
)) ||
874 (tbl_type
.bw
!= rs_ch_width_from_mac_flags(mac_flags
)) ||
875 (tbl_type
.ant_type
!= info
->status
.antenna
) ||
876 (!!(tx_rate
& RATE_MCS_HT_MSK
) !=
877 !!(mac_flags
& IEEE80211_TX_RC_MCS
)) ||
878 (!!(tx_rate
& RATE_MCS_VHT_MSK
) !=
879 !!(mac_flags
& IEEE80211_TX_RC_VHT_MCS
)) ||
880 (!!(tx_rate
& RATE_HT_MCS_GF_MSK
) !=
881 !!(mac_flags
& IEEE80211_TX_RC_GREEN_FIELD
)) ||
882 (rs_index
!= mac_index
)) {
884 "initial rate %d does not match %d (0x%x)\n",
885 mac_index
, rs_index
, tx_rate
);
887 * Since rates mis-match, the last LQ command may have failed.
888 * After IWL_MISSED_RATE_MAX mis-matches, resync the uCode with
891 lq_sta
->missed_rate_counter
++;
892 if (lq_sta
->missed_rate_counter
> IWL_MISSED_RATE_MAX
) {
893 lq_sta
->missed_rate_counter
= 0;
894 iwl_mvm_send_lq_cmd(mvm
, &lq_sta
->lq
, CMD_ASYNC
, false);
896 /* Regardless, ignore this status info for outdated rate */
899 /* Rate did match, so reset the missed_rate_counter */
900 lq_sta
->missed_rate_counter
= 0;
902 /* Figure out if rate scale algorithm is in active or search table */
903 if (table_type_matches(&tbl_type
,
904 &(lq_sta
->lq_info
[lq_sta
->active_tbl
]))) {
905 curr_tbl
= &(lq_sta
->lq_info
[lq_sta
->active_tbl
]);
906 other_tbl
= &(lq_sta
->lq_info
[1 - lq_sta
->active_tbl
]);
907 } else if (table_type_matches(
908 &tbl_type
, &lq_sta
->lq_info
[1 - lq_sta
->active_tbl
])) {
909 curr_tbl
= &(lq_sta
->lq_info
[1 - lq_sta
->active_tbl
]);
910 other_tbl
= &(lq_sta
->lq_info
[lq_sta
->active_tbl
]);
913 "Neither active nor search matches tx rate\n");
914 tmp_tbl
= &(lq_sta
->lq_info
[lq_sta
->active_tbl
]);
915 IWL_DEBUG_RATE(mvm
, "active- lq:%x, ant:%x, SGI:%d\n",
916 tmp_tbl
->lq_type
, tmp_tbl
->ant_type
,
918 tmp_tbl
= &(lq_sta
->lq_info
[1 - lq_sta
->active_tbl
]);
919 IWL_DEBUG_RATE(mvm
, "search- lq:%x, ant:%x, SGI:%d\n",
920 tmp_tbl
->lq_type
, tmp_tbl
->ant_type
,
922 IWL_DEBUG_RATE(mvm
, "actual- lq:%x, ant:%x, SGI:%d\n",
923 tbl_type
.lq_type
, tbl_type
.ant_type
,
926 * no matching table found, let's by-pass the data collection
927 * and continue to perform rate scale to find the rate table
929 rs_stay_in_table(lq_sta
, true);
934 * Updating the frame history depends on whether packets were
937 * For aggregation, all packets were transmitted at the same rate, the
938 * first index into rate scale table.
940 if (info
->flags
& IEEE80211_TX_STAT_AMPDU
) {
941 tx_rate
= le32_to_cpu(table
->rs_table
[0]);
942 rs_get_tbl_info_from_mcs(tx_rate
, info
->band
, &tbl_type
,
944 rs_collect_tx_data(curr_tbl
, rs_index
,
945 info
->status
.ampdu_len
,
946 info
->status
.ampdu_ack_len
);
948 /* Update success/fail counts if not searching for new mode */
949 if (lq_sta
->stay_in_tbl
) {
950 lq_sta
->total_success
+= info
->status
.ampdu_ack_len
;
951 lq_sta
->total_failed
+= (info
->status
.ampdu_len
-
952 info
->status
.ampdu_ack_len
);
956 * For legacy, update frame history with for each Tx retry.
958 retries
= info
->status
.rates
[0].count
- 1;
959 /* HW doesn't send more than 15 retries */
960 retries
= min(retries
, 15);
962 /* The last transmission may have been successful */
963 legacy_success
= !!(info
->flags
& IEEE80211_TX_STAT_ACK
);
964 /* Collect data for each rate used during failed TX attempts */
965 for (i
= 0; i
<= retries
; ++i
) {
966 tx_rate
= le32_to_cpu(table
->rs_table
[i
]);
967 rs_get_tbl_info_from_mcs(tx_rate
, info
->band
,
968 &tbl_type
, &rs_index
);
970 * Only collect stats if retried rate is in the same RS
971 * table as active/search.
973 if (table_type_matches(&tbl_type
, curr_tbl
))
975 else if (table_type_matches(&tbl_type
, other_tbl
))
979 rs_collect_tx_data(tmp_tbl
, rs_index
, 1,
980 i
< retries
? 0 : legacy_success
);
983 /* Update success/fail counts if not searching for new mode */
984 if (lq_sta
->stay_in_tbl
) {
985 lq_sta
->total_success
+= legacy_success
;
986 lq_sta
->total_failed
+= retries
+ (1 - legacy_success
);
989 /* The last TX rate is cached in lq_sta; it's set in if/else above */
990 lq_sta
->last_rate_n_flags
= tx_rate
;
992 /* See if there's a better rate or modulation mode to try. */
993 if (sta
&& sta
->supp_rates
[sband
->band
])
994 rs_rate_scale_perform(mvm
, skb
, sta
, lq_sta
);
998 * Begin a period of staying with a selected modulation mode.
999 * Set "stay_in_tbl" flag to prevent any mode switches.
1000 * Set frame tx success limits according to legacy vs. high-throughput,
1001 * and reset overall (spanning all rates) tx success history statistics.
1002 * These control how long we stay using same modulation mode before
1003 * searching for a new mode.
1005 static void rs_set_stay_in_table(struct iwl_mvm
*mvm
, u8 is_legacy
,
1006 struct iwl_lq_sta
*lq_sta
)
1008 IWL_DEBUG_RATE(mvm
, "we are staying in the same table\n");
1009 lq_sta
->stay_in_tbl
= 1; /* only place this gets set */
1011 lq_sta
->table_count_limit
= IWL_LEGACY_TABLE_COUNT
;
1012 lq_sta
->max_failure_limit
= IWL_LEGACY_FAILURE_LIMIT
;
1013 lq_sta
->max_success_limit
= IWL_LEGACY_SUCCESS_LIMIT
;
1015 lq_sta
->table_count_limit
= IWL_NONE_LEGACY_TABLE_COUNT
;
1016 lq_sta
->max_failure_limit
= IWL_NONE_LEGACY_FAILURE_LIMIT
;
1017 lq_sta
->max_success_limit
= IWL_NONE_LEGACY_SUCCESS_LIMIT
;
1019 lq_sta
->table_count
= 0;
1020 lq_sta
->total_failed
= 0;
1021 lq_sta
->total_success
= 0;
1022 lq_sta
->flush_timer
= jiffies
;
1023 lq_sta
->action_counter
= 0;
1027 * Find correct throughput table for given mode of modulation
1029 static void rs_set_expected_tpt_table(struct iwl_lq_sta
*lq_sta
,
1030 struct iwl_scale_tbl_info
*tbl
)
1032 /* Used to choose among HT tables */
1033 s32 (*ht_tbl_pointer
)[IWL_RATE_COUNT
];
1035 /* Check for invalid LQ type */
1036 if (WARN_ON_ONCE(!is_legacy(tbl
->lq_type
) && !is_ht(tbl
->lq_type
) &&
1037 !(is_vht(tbl
->lq_type
)))) {
1038 tbl
->expected_tpt
= expected_tpt_legacy
;
1042 /* Legacy rates have only one table */
1043 if (is_legacy(tbl
->lq_type
)) {
1044 tbl
->expected_tpt
= expected_tpt_legacy
;
1048 ht_tbl_pointer
= expected_tpt_mimo2_20MHz
;
1049 /* Choose among many HT tables depending on number of streams
1050 * (SISO/MIMO2), channel width (20/40/80), SGI, and aggregation
1052 if (is_siso(tbl
->lq_type
)) {
1054 case RATE_MCS_CHAN_WIDTH_20
:
1055 ht_tbl_pointer
= expected_tpt_siso_20MHz
;
1057 case RATE_MCS_CHAN_WIDTH_40
:
1058 ht_tbl_pointer
= expected_tpt_siso_40MHz
;
1060 case RATE_MCS_CHAN_WIDTH_80
:
1061 ht_tbl_pointer
= expected_tpt_siso_80MHz
;
1066 } else if (is_mimo2(tbl
->lq_type
)) {
1068 case RATE_MCS_CHAN_WIDTH_20
:
1069 ht_tbl_pointer
= expected_tpt_mimo2_20MHz
;
1071 case RATE_MCS_CHAN_WIDTH_40
:
1072 ht_tbl_pointer
= expected_tpt_mimo2_40MHz
;
1074 case RATE_MCS_CHAN_WIDTH_80
:
1075 ht_tbl_pointer
= expected_tpt_mimo2_80MHz
;
1084 if (!tbl
->is_SGI
&& !lq_sta
->is_agg
) /* Normal */
1085 tbl
->expected_tpt
= ht_tbl_pointer
[0];
1086 else if (tbl
->is_SGI
&& !lq_sta
->is_agg
) /* SGI */
1087 tbl
->expected_tpt
= ht_tbl_pointer
[1];
1088 else if (!tbl
->is_SGI
&& lq_sta
->is_agg
) /* AGG */
1089 tbl
->expected_tpt
= ht_tbl_pointer
[2];
1091 tbl
->expected_tpt
= ht_tbl_pointer
[3];
1095 * Find starting rate for new "search" high-throughput mode of modulation.
1096 * Goal is to find lowest expected rate (under perfect conditions) that is
1097 * above the current measured throughput of "active" mode, to give new mode
1098 * a fair chance to prove itself without too many challenges.
1100 * This gets called when transitioning to more aggressive modulation
1101 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
1102 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1103 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1104 * bit rate will typically need to increase, but not if performance was bad.
1106 static s32
rs_get_best_rate(struct iwl_mvm
*mvm
,
1107 struct iwl_lq_sta
*lq_sta
,
1108 struct iwl_scale_tbl_info
*tbl
, /* "search" */
1109 u16 rate_mask
, s8 index
)
1111 /* "active" values */
1112 struct iwl_scale_tbl_info
*active_tbl
=
1113 &(lq_sta
->lq_info
[lq_sta
->active_tbl
]);
1114 s32 active_sr
= active_tbl
->win
[index
].success_ratio
;
1115 s32 active_tpt
= active_tbl
->expected_tpt
[index
];
1117 /* expected "search" throughput */
1118 s32
*tpt_tbl
= tbl
->expected_tpt
;
1120 s32 new_rate
, high
, low
, start_hi
;
1124 new_rate
= high
= low
= start_hi
= IWL_RATE_INVALID
;
1127 high_low
= rs_get_adjacent_rate(mvm
, rate
, rate_mask
,
1130 low
= high_low
& 0xff;
1131 high
= (high_low
>> 8) & 0xff;
1134 * Lower the "search" bit rate, to give new "search" mode
1135 * approximately the same throughput as "active" if:
1137 * 1) "Active" mode has been working modestly well (but not
1138 * great), and expected "search" throughput (under perfect
1139 * conditions) at candidate rate is above the actual
1140 * measured "active" throughput (but less than expected
1141 * "active" throughput under perfect conditions).
1143 * 2) "Active" mode has been working perfectly or very well
1144 * and expected "search" throughput (under perfect
1145 * conditions) at candidate rate is above expected
1146 * "active" throughput (under perfect conditions).
1148 if ((((100 * tpt_tbl
[rate
]) > lq_sta
->last_tpt
) &&
1149 ((active_sr
> IWL_RATE_DECREASE_TH
) &&
1150 (active_sr
<= IWL_RATE_HIGH_TH
) &&
1151 (tpt_tbl
[rate
] <= active_tpt
))) ||
1152 ((active_sr
>= IWL_RATE_SCALE_SWITCH
) &&
1153 (tpt_tbl
[rate
] > active_tpt
))) {
1154 /* (2nd or later pass)
1155 * If we've already tried to raise the rate, and are
1156 * now trying to lower it, use the higher rate. */
1157 if (start_hi
!= IWL_RATE_INVALID
) {
1158 new_rate
= start_hi
;
1164 /* Loop again with lower rate */
1165 if (low
!= IWL_RATE_INVALID
)
1168 /* Lower rate not available, use the original */
1172 /* Else try to raise the "search" rate to match "active" */
1174 /* (2nd or later pass)
1175 * If we've already tried to lower the rate, and are
1176 * now trying to raise it, use the lower rate. */
1177 if (new_rate
!= IWL_RATE_INVALID
)
1180 /* Loop again with higher rate */
1181 else if (high
!= IWL_RATE_INVALID
) {
1185 /* Higher rate not available, use the original */
1196 /* Move to the next action and wrap around to the first action in case
1197 * we're at the last action. Assumes actions start at 0.
1199 static inline void rs_move_next_action(struct iwl_scale_tbl_info
*tbl
,
1202 BUILD_BUG_ON(IWL_LEGACY_FIRST_ACTION
!= 0);
1203 BUILD_BUG_ON(IWL_SISO_FIRST_ACTION
!= 0);
1204 BUILD_BUG_ON(IWL_MIMO2_FIRST_ACTION
!= 0);
1206 tbl
->action
= (tbl
->action
+ 1) % (last_action
+ 1);
1209 static void rs_set_bw_from_sta(struct iwl_scale_tbl_info
*tbl
,
1210 struct ieee80211_sta
*sta
)
1212 if (sta
->bandwidth
>= IEEE80211_STA_RX_BW_80
)
1213 tbl
->bw
= RATE_MCS_CHAN_WIDTH_80
;
1214 else if (sta
->bandwidth
>= IEEE80211_STA_RX_BW_40
)
1215 tbl
->bw
= RATE_MCS_CHAN_WIDTH_40
;
1217 tbl
->bw
= RATE_MCS_CHAN_WIDTH_20
;
1220 static bool rs_sgi_allowed(struct iwl_scale_tbl_info
*tbl
,
1221 struct ieee80211_sta
*sta
)
1223 struct ieee80211_sta_ht_cap
*ht_cap
= &sta
->ht_cap
;
1224 struct ieee80211_sta_vht_cap
*vht_cap
= &sta
->vht_cap
;
1226 if (is_ht20(tbl
) && (ht_cap
->cap
&
1227 IEEE80211_HT_CAP_SGI_20
))
1229 if (is_ht40(tbl
) && (ht_cap
->cap
&
1230 IEEE80211_HT_CAP_SGI_40
))
1232 if (is_ht80(tbl
) && (vht_cap
->cap
&
1233 IEEE80211_VHT_CAP_SHORT_GI_80
))
1240 * Set up search table for MIMO2
1242 static int rs_switch_to_mimo2(struct iwl_mvm
*mvm
,
1243 struct iwl_lq_sta
*lq_sta
,
1244 struct ieee80211_sta
*sta
,
1245 struct iwl_scale_tbl_info
*tbl
, int index
)
1249 s8 is_green
= lq_sta
->is_green
;
1251 if (!sta
->ht_cap
.ht_supported
)
1254 if (sta
->smps_mode
== IEEE80211_SMPS_STATIC
)
1257 /* Need both Tx chains/antennas to support MIMO */
1258 if (num_of_ant(iwl_fw_valid_tx_ant(mvm
->fw
)) < 2)
1261 IWL_DEBUG_RATE(mvm
, "LQ: try to switch to MIMO2\n");
1263 tbl
->lq_type
= lq_sta
->is_vht
? LQ_VHT_MIMO2
: LQ_HT_MIMO2
;
1265 tbl
->max_search
= IWL_MAX_SEARCH
;
1266 rate_mask
= lq_sta
->active_mimo2_rate
;
1268 rs_set_bw_from_sta(tbl
, sta
);
1269 rs_set_expected_tpt_table(lq_sta
, tbl
);
1271 rate
= rs_get_best_rate(mvm
, lq_sta
, tbl
, rate_mask
, index
);
1273 IWL_DEBUG_RATE(mvm
, "LQ: MIMO2 best rate %d mask %X\n",
1275 if ((rate
== IWL_RATE_INVALID
) || !((1 << rate
) & rate_mask
)) {
1276 IWL_DEBUG_RATE(mvm
, "Can't switch with index %d rate mask %x\n",
1280 tbl
->current_rate
= rate_n_flags_from_tbl(mvm
, tbl
, rate
, is_green
);
1282 IWL_DEBUG_RATE(mvm
, "LQ: Switch to new mcs %X index is green %X\n",
1283 tbl
->current_rate
, is_green
);
1288 * Set up search table for SISO
1290 static int rs_switch_to_siso(struct iwl_mvm
*mvm
,
1291 struct iwl_lq_sta
*lq_sta
,
1292 struct ieee80211_sta
*sta
,
1293 struct iwl_scale_tbl_info
*tbl
, int index
)
1296 u8 is_green
= lq_sta
->is_green
;
1299 if (!sta
->ht_cap
.ht_supported
)
1302 IWL_DEBUG_RATE(mvm
, "LQ: try to switch to SISO\n");
1304 tbl
->lq_type
= lq_sta
->is_vht
? LQ_VHT_SISO
: LQ_HT_SISO
;
1306 tbl
->max_search
= IWL_MAX_SEARCH
;
1307 rate_mask
= lq_sta
->active_siso_rate
;
1310 tbl
->is_SGI
= 0; /*11n spec: no SGI in SISO+Greenfield*/
1312 rs_set_bw_from_sta(tbl
, sta
);
1313 rs_set_expected_tpt_table(lq_sta
, tbl
);
1314 rate
= rs_get_best_rate(mvm
, lq_sta
, tbl
, rate_mask
, index
);
1316 IWL_DEBUG_RATE(mvm
, "LQ: get best rate %d mask %X\n", rate
, rate_mask
);
1317 if ((rate
== IWL_RATE_INVALID
) || !((1 << rate
) & rate_mask
)) {
1319 "can not switch with index %d rate mask %x\n",
1323 tbl
->current_rate
= rate_n_flags_from_tbl(mvm
, tbl
, rate
, is_green
);
1324 IWL_DEBUG_RATE(mvm
, "LQ: Switch to new mcs %X index is green %X\n",
1325 tbl
->current_rate
, is_green
);
1330 * Try to switch to new modulation mode from legacy
1332 static int rs_move_legacy_other(struct iwl_mvm
*mvm
,
1333 struct iwl_lq_sta
*lq_sta
,
1334 struct ieee80211_sta
*sta
,
1337 struct iwl_scale_tbl_info
*tbl
= &(lq_sta
->lq_info
[lq_sta
->active_tbl
]);
1338 struct iwl_scale_tbl_info
*search_tbl
=
1339 &(lq_sta
->lq_info
[(1 - lq_sta
->active_tbl
)]);
1340 struct iwl_rate_scale_data
*window
= &(tbl
->win
[index
]);
1341 u32 sz
= (sizeof(struct iwl_scale_tbl_info
) -
1342 (sizeof(struct iwl_rate_scale_data
) * IWL_RATE_COUNT
));
1344 u8 valid_tx_ant
= iwl_fw_valid_tx_ant(mvm
->fw
);
1345 u8 tx_chains_num
= num_of_ant(valid_tx_ant
);
1347 u8 update_search_tbl_counter
= 0;
1349 start_action
= tbl
->action
;
1351 lq_sta
->action_counter
++;
1352 switch (tbl
->action
) {
1353 case IWL_LEGACY_SWITCH_ANTENNA
:
1354 IWL_DEBUG_RATE(mvm
, "LQ: Legacy toggle Antenna\n");
1356 if (tx_chains_num
<= 1)
1359 /* Don't change antenna if success has been great */
1360 if (window
->success_ratio
>= IWL_RS_GOOD_RATIO
)
1363 /* Set up search table to try other antenna */
1364 memcpy(search_tbl
, tbl
, sz
);
1366 if (rs_toggle_antenna(valid_tx_ant
,
1367 &search_tbl
->current_rate
,
1369 update_search_tbl_counter
= 1;
1370 rs_set_expected_tpt_table(lq_sta
, search_tbl
);
1374 case IWL_LEGACY_SWITCH_SISO
:
1375 IWL_DEBUG_RATE(mvm
, "LQ: Legacy switch to SISO\n");
1377 /* Set up search table to try SISO */
1378 memcpy(search_tbl
, tbl
, sz
);
1379 search_tbl
->is_SGI
= 0;
1380 ret
= rs_switch_to_siso(mvm
, lq_sta
, sta
,
1383 lq_sta
->action_counter
= 0;
1388 case IWL_LEGACY_SWITCH_MIMO2
:
1389 IWL_DEBUG_RATE(mvm
, "LQ: Legacy switch to MIMO2\n");
1391 /* Set up search table to try MIMO */
1392 memcpy(search_tbl
, tbl
, sz
);
1393 search_tbl
->is_SGI
= 0;
1395 search_tbl
->ant_type
= ANT_AB
;
1397 if (!rs_is_valid_ant(valid_tx_ant
,
1398 search_tbl
->ant_type
))
1401 ret
= rs_switch_to_mimo2(mvm
, lq_sta
, sta
,
1404 lq_sta
->action_counter
= 0;
1411 rs_move_next_action(tbl
, IWL_LEGACY_LAST_ACTION
);
1413 if (tbl
->action
== start_action
)
1416 search_tbl
->lq_type
= LQ_NONE
;
1420 lq_sta
->search_better_tbl
= 1;
1421 rs_move_next_action(tbl
, IWL_LEGACY_LAST_ACTION
);
1422 if (update_search_tbl_counter
)
1423 search_tbl
->action
= tbl
->action
;
1428 * Try to switch to new modulation mode from SISO
1430 static int rs_move_siso_to_other(struct iwl_mvm
*mvm
,
1431 struct iwl_lq_sta
*lq_sta
,
1432 struct ieee80211_sta
*sta
, int index
)
1434 u8 is_green
= lq_sta
->is_green
;
1435 struct iwl_scale_tbl_info
*tbl
= &(lq_sta
->lq_info
[lq_sta
->active_tbl
]);
1436 struct iwl_scale_tbl_info
*search_tbl
=
1437 &(lq_sta
->lq_info
[(1 - lq_sta
->active_tbl
)]);
1438 struct iwl_rate_scale_data
*window
= &(tbl
->win
[index
]);
1439 u32 sz
= (sizeof(struct iwl_scale_tbl_info
) -
1440 (sizeof(struct iwl_rate_scale_data
) * IWL_RATE_COUNT
));
1442 u8 valid_tx_ant
= iwl_fw_valid_tx_ant(mvm
->fw
);
1443 u8 tx_chains_num
= num_of_ant(valid_tx_ant
);
1444 u8 update_search_tbl_counter
= 0;
1447 switch (le32_to_cpu(mvm
->last_bt_notif
.bt_activity_grading
)) {
1448 case IWL_BT_COEX_TRAFFIC_LOAD_NONE
:
1451 case IWL_BT_COEX_TRAFFIC_LOAD_LOW
:
1452 /* avoid switching to antenna B but allow MIMO */
1453 if (tbl
->action
== IWL_SISO_SWITCH_ANTENNA
&&
1454 tbl
->ant_type
== ANT_A
)
1455 tbl
->action
= IWL_SISO_SWITCH_MIMO2
;
1457 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH
:
1458 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS
:
1459 /* A - avoid antenna B and MIMO. B - switch to A */
1460 if (tbl
->ant_type
== ANT_A
)
1462 first_antenna(iwl_fw_valid_tx_ant(mvm
->fw
));
1463 tbl
->action
= IWL_SISO_SWITCH_ANTENNA
;
1466 IWL_ERR(mvm
, "Invalid BT load %d",
1467 le32_to_cpu(mvm
->last_bt_notif
.bt_activity_grading
));
1471 start_action
= tbl
->action
;
1473 lq_sta
->action_counter
++;
1474 switch (tbl
->action
) {
1475 case IWL_SISO_SWITCH_ANTENNA
:
1476 IWL_DEBUG_RATE(mvm
, "LQ: SISO toggle Antenna\n");
1477 if (tx_chains_num
<= 1)
1480 if (window
->success_ratio
>= IWL_RS_GOOD_RATIO
&&
1481 BT_MBOX_MSG(&mvm
->last_bt_notif
, 3,
1485 memcpy(search_tbl
, tbl
, sz
);
1486 if (rs_toggle_antenna(valid_tx_ant
,
1487 &search_tbl
->current_rate
,
1489 update_search_tbl_counter
= 1;
1493 case IWL_SISO_SWITCH_MIMO2
:
1494 IWL_DEBUG_RATE(mvm
, "LQ: SISO switch to MIMO2\n");
1495 memcpy(search_tbl
, tbl
, sz
);
1496 search_tbl
->is_SGI
= 0;
1498 search_tbl
->ant_type
= ANT_AB
;
1500 if (!rs_is_valid_ant(valid_tx_ant
,
1501 search_tbl
->ant_type
))
1504 ret
= rs_switch_to_mimo2(mvm
, lq_sta
, sta
,
1509 case IWL_SISO_SWITCH_GI
:
1510 if (!rs_sgi_allowed(tbl
, sta
))
1513 IWL_DEBUG_RATE(mvm
, "LQ: SISO toggle SGI/NGI\n");
1515 memcpy(search_tbl
, tbl
, sz
);
1521 "SGI was set in GF+SISO\n");
1523 search_tbl
->is_SGI
= !tbl
->is_SGI
;
1524 rs_set_expected_tpt_table(lq_sta
, search_tbl
);
1526 s32 tpt
= lq_sta
->last_tpt
/ 100;
1527 if (tpt
>= search_tbl
->expected_tpt
[index
])
1530 search_tbl
->current_rate
=
1531 rate_n_flags_from_tbl(mvm
, search_tbl
,
1533 update_search_tbl_counter
= 1;
1538 rs_move_next_action(tbl
, IWL_SISO_LAST_ACTION
);
1540 if (tbl
->action
== start_action
)
1543 search_tbl
->lq_type
= LQ_NONE
;
1547 lq_sta
->search_better_tbl
= 1;
1548 rs_move_next_action(tbl
, IWL_SISO_LAST_ACTION
);
1549 if (update_search_tbl_counter
)
1550 search_tbl
->action
= tbl
->action
;
1556 * Try to switch to new modulation mode from MIMO2
1558 static int rs_move_mimo2_to_other(struct iwl_mvm
*mvm
,
1559 struct iwl_lq_sta
*lq_sta
,
1560 struct ieee80211_sta
*sta
, int index
)
1562 s8 is_green
= lq_sta
->is_green
;
1563 struct iwl_scale_tbl_info
*tbl
= &(lq_sta
->lq_info
[lq_sta
->active_tbl
]);
1564 struct iwl_scale_tbl_info
*search_tbl
=
1565 &(lq_sta
->lq_info
[(1 - lq_sta
->active_tbl
)]);
1566 u32 sz
= (sizeof(struct iwl_scale_tbl_info
) -
1567 (sizeof(struct iwl_rate_scale_data
) * IWL_RATE_COUNT
));
1569 u8 valid_tx_ant
= iwl_fw_valid_tx_ant(mvm
->fw
);
1570 u8 update_search_tbl_counter
= 0;
1573 switch (le32_to_cpu(mvm
->last_bt_notif
.bt_activity_grading
)) {
1574 case IWL_BT_COEX_TRAFFIC_LOAD_NONE
:
1577 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH
:
1578 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS
:
1579 /* avoid antenna B and MIMO */
1580 if (tbl
->action
!= IWL_MIMO2_SWITCH_SISO_A
)
1581 tbl
->action
= IWL_MIMO2_SWITCH_SISO_A
;
1583 case IWL_BT_COEX_TRAFFIC_LOAD_LOW
:
1584 /* avoid antenna B unless MIMO */
1585 if (tbl
->action
== IWL_MIMO2_SWITCH_SISO_B
)
1586 tbl
->action
= IWL_MIMO2_SWITCH_SISO_A
;
1589 IWL_ERR(mvm
, "Invalid BT load %d",
1590 le32_to_cpu(mvm
->last_bt_notif
.bt_activity_grading
));
1594 start_action
= tbl
->action
;
1596 lq_sta
->action_counter
++;
1597 switch (tbl
->action
) {
1598 case IWL_MIMO2_SWITCH_SISO_A
:
1599 case IWL_MIMO2_SWITCH_SISO_B
:
1600 IWL_DEBUG_RATE(mvm
, "LQ: MIMO2 switch to SISO\n");
1602 /* Set up new search table for SISO */
1603 memcpy(search_tbl
, tbl
, sz
);
1605 if (tbl
->action
== IWL_MIMO2_SWITCH_SISO_A
)
1606 search_tbl
->ant_type
= ANT_A
;
1607 else /* tbl->action == IWL_MIMO2_SWITCH_SISO_B */
1608 search_tbl
->ant_type
= ANT_B
;
1610 if (!rs_is_valid_ant(valid_tx_ant
,
1611 search_tbl
->ant_type
))
1614 ret
= rs_switch_to_siso(mvm
, lq_sta
, sta
,
1621 case IWL_MIMO2_SWITCH_GI
:
1622 if (!rs_sgi_allowed(tbl
, sta
))
1625 IWL_DEBUG_RATE(mvm
, "LQ: MIMO2 toggle SGI/NGI\n");
1627 /* Set up new search table for MIMO2 */
1628 memcpy(search_tbl
, tbl
, sz
);
1629 search_tbl
->is_SGI
= !tbl
->is_SGI
;
1630 rs_set_expected_tpt_table(lq_sta
, search_tbl
);
1632 * If active table already uses the fastest possible
1633 * modulation (dual stream with short guard interval),
1634 * and it's working well, there's no need to look
1635 * for a better type of modulation!
1638 s32 tpt
= lq_sta
->last_tpt
/ 100;
1639 if (tpt
>= search_tbl
->expected_tpt
[index
])
1642 search_tbl
->current_rate
=
1643 rate_n_flags_from_tbl(mvm
, search_tbl
,
1645 update_search_tbl_counter
= 1;
1650 rs_move_next_action(tbl
, IWL_MIMO2_LAST_ACTION
);
1652 if (tbl
->action
== start_action
)
1655 search_tbl
->lq_type
= LQ_NONE
;
1658 lq_sta
->search_better_tbl
= 1;
1659 rs_move_next_action(tbl
, IWL_MIMO2_LAST_ACTION
);
1660 if (update_search_tbl_counter
)
1661 search_tbl
->action
= tbl
->action
;
1667 * Check whether we should continue using same modulation mode, or
1668 * begin search for a new mode, based on:
1669 * 1) # tx successes or failures while using this mode
1670 * 2) # times calling this function
1671 * 3) elapsed time in this mode (not used, for now)
1673 static void rs_stay_in_table(struct iwl_lq_sta
*lq_sta
, bool force_search
)
1675 struct iwl_scale_tbl_info
*tbl
;
1678 int flush_interval_passed
= 0;
1679 struct iwl_mvm
*mvm
;
1682 active_tbl
= lq_sta
->active_tbl
;
1684 tbl
= &(lq_sta
->lq_info
[active_tbl
]);
1686 /* If we've been disallowing search, see if we should now allow it */
1687 if (lq_sta
->stay_in_tbl
) {
1688 /* Elapsed time using current modulation mode */
1689 if (lq_sta
->flush_timer
)
1690 flush_interval_passed
=
1692 (unsigned long)(lq_sta
->flush_timer
+
1693 IWL_RATE_SCALE_FLUSH_INTVL
));
1696 * Check if we should allow search for new modulation mode.
1697 * If many frames have failed or succeeded, or we've used
1698 * this same modulation for a long time, allow search, and
1699 * reset history stats that keep track of whether we should
1700 * allow a new search. Also (below) reset all bitmaps and
1701 * stats in active history.
1704 (lq_sta
->total_failed
> lq_sta
->max_failure_limit
) ||
1705 (lq_sta
->total_success
> lq_sta
->max_success_limit
) ||
1706 ((!lq_sta
->search_better_tbl
) &&
1707 (lq_sta
->flush_timer
) && (flush_interval_passed
))) {
1709 "LQ: stay is expired %d %d %d\n",
1710 lq_sta
->total_failed
,
1711 lq_sta
->total_success
,
1712 flush_interval_passed
);
1714 /* Allow search for new mode */
1715 lq_sta
->stay_in_tbl
= 0; /* only place reset */
1716 lq_sta
->total_failed
= 0;
1717 lq_sta
->total_success
= 0;
1718 lq_sta
->flush_timer
= 0;
1720 * Else if we've used this modulation mode enough repetitions
1721 * (regardless of elapsed time or success/failure), reset
1722 * history bitmaps and rate-specific stats for all rates in
1726 lq_sta
->table_count
++;
1727 if (lq_sta
->table_count
>=
1728 lq_sta
->table_count_limit
) {
1729 lq_sta
->table_count
= 0;
1732 "LQ: stay in table clear win\n");
1733 for (i
= 0; i
< IWL_RATE_COUNT
; i
++)
1734 rs_rate_scale_clear_window(
1739 /* If transitioning to allow "search", reset all history
1740 * bitmaps and stats in active table (this will become the new
1741 * "search" table). */
1742 if (!lq_sta
->stay_in_tbl
) {
1743 for (i
= 0; i
< IWL_RATE_COUNT
; i
++)
1744 rs_rate_scale_clear_window(&(tbl
->win
[i
]));
1750 * setup rate table in uCode
1752 static void rs_update_rate_tbl(struct iwl_mvm
*mvm
,
1753 struct iwl_lq_sta
*lq_sta
,
1754 struct iwl_scale_tbl_info
*tbl
,
1755 int index
, u8 is_green
)
1759 /* Update uCode's rate table. */
1760 rate
= rate_n_flags_from_tbl(mvm
, tbl
, index
, is_green
);
1761 rs_fill_link_cmd(mvm
, lq_sta
, rate
);
1762 iwl_mvm_send_lq_cmd(mvm
, &lq_sta
->lq
, CMD_ASYNC
, false);
1765 static u8
rs_get_tid(struct iwl_lq_sta
*lq_data
,
1766 struct ieee80211_hdr
*hdr
)
1768 u8 tid
= IWL_MAX_TID_COUNT
;
1770 if (ieee80211_is_data_qos(hdr
->frame_control
)) {
1771 u8
*qc
= ieee80211_get_qos_ctl(hdr
);
1775 if (unlikely(tid
> IWL_MAX_TID_COUNT
))
1776 tid
= IWL_MAX_TID_COUNT
;
1782 * Do rate scaling and search for new modulation mode.
1784 static void rs_rate_scale_perform(struct iwl_mvm
*mvm
,
1785 struct sk_buff
*skb
,
1786 struct ieee80211_sta
*sta
,
1787 struct iwl_lq_sta
*lq_sta
)
1789 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
1790 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*)skb
->data
;
1791 int low
= IWL_RATE_INVALID
;
1792 int high
= IWL_RATE_INVALID
;
1795 struct iwl_rate_scale_data
*window
= NULL
;
1796 int current_tpt
= IWL_INVALID_VALUE
;
1797 int low_tpt
= IWL_INVALID_VALUE
;
1798 int high_tpt
= IWL_INVALID_VALUE
;
1800 s8 scale_action
= 0;
1803 struct iwl_scale_tbl_info
*tbl
, *tbl1
;
1804 u16 rate_scale_index_msk
= 0;
1810 u8 tid
= IWL_MAX_TID_COUNT
;
1811 struct iwl_mvm_sta
*sta_priv
= (void *)sta
->drv_priv
;
1812 struct iwl_mvm_tid_data
*tid_data
;
1814 IWL_DEBUG_RATE(mvm
, "rate scale calculate new rate for skb\n");
1816 /* Send management frames and NO_ACK data using lowest rate. */
1817 /* TODO: this could probably be improved.. */
1818 if (!ieee80211_is_data(hdr
->frame_control
) ||
1819 info
->flags
& IEEE80211_TX_CTL_NO_ACK
)
1822 lq_sta
->supp_rates
= sta
->supp_rates
[lq_sta
->band
];
1824 tid
= rs_get_tid(lq_sta
, hdr
);
1825 if ((tid
!= IWL_MAX_TID_COUNT
) &&
1826 (lq_sta
->tx_agg_tid_en
& (1 << tid
))) {
1827 tid_data
= &sta_priv
->tid_data
[tid
];
1828 if (tid_data
->state
== IWL_AGG_OFF
)
1837 * Select rate-scale / modulation-mode table to work with in
1838 * the rest of this function: "search" if searching for better
1839 * modulation mode, or "active" if doing rate scaling within a mode.
1841 if (!lq_sta
->search_better_tbl
)
1842 active_tbl
= lq_sta
->active_tbl
;
1844 active_tbl
= 1 - lq_sta
->active_tbl
;
1846 tbl
= &(lq_sta
->lq_info
[active_tbl
]);
1847 if (is_legacy(tbl
->lq_type
))
1848 lq_sta
->is_green
= 0;
1850 lq_sta
->is_green
= rs_use_green(sta
);
1851 is_green
= lq_sta
->is_green
;
1853 /* current tx rate */
1854 index
= lq_sta
->last_txrate_idx
;
1856 IWL_DEBUG_RATE(mvm
, "Rate scale index %d for type %d\n", index
,
1859 /* rates available for this association, and for modulation mode */
1860 rate_mask
= rs_get_supported_rates(lq_sta
, hdr
, tbl
->lq_type
);
1862 IWL_DEBUG_RATE(mvm
, "mask 0x%04X\n", rate_mask
);
1864 /* mask with station rate restriction */
1865 if (is_legacy(tbl
->lq_type
)) {
1866 if (lq_sta
->band
== IEEE80211_BAND_5GHZ
)
1867 /* supp_rates has no CCK bits in A mode */
1868 rate_scale_index_msk
= (u16
) (rate_mask
&
1869 (lq_sta
->supp_rates
<< IWL_FIRST_OFDM_RATE
));
1871 rate_scale_index_msk
= (u16
) (rate_mask
&
1872 lq_sta
->supp_rates
);
1875 rate_scale_index_msk
= rate_mask
;
1878 if (!rate_scale_index_msk
)
1879 rate_scale_index_msk
= rate_mask
;
1881 if (!((1 << index
) & rate_scale_index_msk
)) {
1882 IWL_ERR(mvm
, "Current Rate is not valid\n");
1883 if (lq_sta
->search_better_tbl
) {
1884 /* revert to active table if search table is not valid*/
1885 tbl
->lq_type
= LQ_NONE
;
1886 lq_sta
->search_better_tbl
= 0;
1887 tbl
= &(lq_sta
->lq_info
[lq_sta
->active_tbl
]);
1888 /* get "active" rate info */
1889 index
= iwl_hwrate_to_plcp_idx(tbl
->current_rate
);
1890 rs_update_rate_tbl(mvm
, lq_sta
, tbl
, index
, is_green
);
1895 /* Get expected throughput table and history window for current rate */
1896 if (!tbl
->expected_tpt
) {
1897 IWL_ERR(mvm
, "tbl->expected_tpt is NULL\n");
1901 /* force user max rate if set by user */
1902 if ((lq_sta
->max_rate_idx
!= -1) &&
1903 (lq_sta
->max_rate_idx
< index
)) {
1904 index
= lq_sta
->max_rate_idx
;
1906 window
= &(tbl
->win
[index
]);
1910 window
= &(tbl
->win
[index
]);
1913 * If there is not enough history to calculate actual average
1914 * throughput, keep analyzing results of more tx frames, without
1915 * changing rate or mode (bypass most of the rest of this function).
1916 * Set up new rate table in uCode only if old rate is not supported
1917 * in current association (use new rate found above).
1919 fail_count
= window
->counter
- window
->success_counter
;
1920 if ((fail_count
< IWL_RATE_MIN_FAILURE_TH
) &&
1921 (window
->success_counter
< IWL_RATE_MIN_SUCCESS_TH
)) {
1923 "LQ: still below TH. succ=%d total=%d for index %d\n",
1924 window
->success_counter
, window
->counter
, index
);
1926 /* Can't calculate this yet; not enough history */
1927 window
->average_tpt
= IWL_INVALID_VALUE
;
1929 /* Should we stay with this modulation mode,
1930 * or search for a new one? */
1931 rs_stay_in_table(lq_sta
, false);
1935 /* Else we have enough samples; calculate estimate of
1936 * actual average throughput */
1937 if (window
->average_tpt
!= ((window
->success_ratio
*
1938 tbl
->expected_tpt
[index
] + 64) / 128)) {
1940 "expected_tpt should have been calculated by now\n");
1941 window
->average_tpt
= ((window
->success_ratio
*
1942 tbl
->expected_tpt
[index
] + 64) / 128);
1945 /* If we are searching for better modulation mode, check success. */
1946 if (lq_sta
->search_better_tbl
) {
1947 /* If good success, continue using the "search" mode;
1948 * no need to send new link quality command, since we're
1949 * continuing to use the setup that we've been trying. */
1950 if (window
->average_tpt
> lq_sta
->last_tpt
) {
1952 "LQ: SWITCHING TO NEW TABLE suc=%d cur-tpt=%d old-tpt=%d\n",
1953 window
->success_ratio
,
1954 window
->average_tpt
,
1957 if (!is_legacy(tbl
->lq_type
))
1958 lq_sta
->enable_counter
= 1;
1960 /* Swap tables; "search" becomes "active" */
1961 lq_sta
->active_tbl
= active_tbl
;
1962 current_tpt
= window
->average_tpt
;
1963 /* Else poor success; go back to mode in "active" table */
1966 "LQ: GOING BACK TO THE OLD TABLE suc=%d cur-tpt=%d old-tpt=%d\n",
1967 window
->success_ratio
,
1968 window
->average_tpt
,
1971 /* Nullify "search" table */
1972 tbl
->lq_type
= LQ_NONE
;
1974 /* Revert to "active" table */
1975 active_tbl
= lq_sta
->active_tbl
;
1976 tbl
= &(lq_sta
->lq_info
[active_tbl
]);
1978 /* Revert to "active" rate and throughput info */
1979 index
= iwl_hwrate_to_plcp_idx(tbl
->current_rate
);
1980 current_tpt
= lq_sta
->last_tpt
;
1982 /* Need to set up a new rate table in uCode */
1986 /* Either way, we've made a decision; modulation mode
1987 * search is done, allow rate adjustment next time. */
1988 lq_sta
->search_better_tbl
= 0;
1989 done_search
= 1; /* Don't switch modes below! */
1993 /* (Else) not in search of better modulation mode, try for better
1994 * starting rate, while staying in this mode. */
1995 high_low
= rs_get_adjacent_rate(mvm
, index
, rate_scale_index_msk
,
1997 low
= high_low
& 0xff;
1998 high
= (high_low
>> 8) & 0xff;
2000 /* If user set max rate, dont allow higher than user constrain */
2001 if ((lq_sta
->max_rate_idx
!= -1) &&
2002 (lq_sta
->max_rate_idx
< high
))
2003 high
= IWL_RATE_INVALID
;
2005 sr
= window
->success_ratio
;
2007 /* Collect measured throughputs for current and adjacent rates */
2008 current_tpt
= window
->average_tpt
;
2009 if (low
!= IWL_RATE_INVALID
)
2010 low_tpt
= tbl
->win
[low
].average_tpt
;
2011 if (high
!= IWL_RATE_INVALID
)
2012 high_tpt
= tbl
->win
[high
].average_tpt
;
2016 /* Too many failures, decrease rate */
2017 if ((sr
<= IWL_RATE_DECREASE_TH
) || (current_tpt
== 0)) {
2019 "decrease rate because of low success_ratio\n");
2021 /* No throughput measured yet for adjacent rates; try increase. */
2022 } else if ((low_tpt
== IWL_INVALID_VALUE
) &&
2023 (high_tpt
== IWL_INVALID_VALUE
)) {
2024 if (high
!= IWL_RATE_INVALID
&& sr
>= IWL_RATE_INCREASE_TH
)
2026 else if (low
!= IWL_RATE_INVALID
)
2030 /* Both adjacent throughputs are measured, but neither one has better
2031 * throughput; we're using the best rate, don't change it! */
2032 else if ((low_tpt
!= IWL_INVALID_VALUE
) &&
2033 (high_tpt
!= IWL_INVALID_VALUE
) &&
2034 (low_tpt
< current_tpt
) &&
2035 (high_tpt
< current_tpt
))
2038 /* At least one adjacent rate's throughput is measured,
2039 * and may have better performance. */
2041 /* Higher adjacent rate's throughput is measured */
2042 if (high_tpt
!= IWL_INVALID_VALUE
) {
2043 /* Higher rate has better throughput */
2044 if (high_tpt
> current_tpt
&&
2045 sr
>= IWL_RATE_INCREASE_TH
) {
2051 /* Lower adjacent rate's throughput is measured */
2052 } else if (low_tpt
!= IWL_INVALID_VALUE
) {
2053 /* Lower rate has better throughput */
2054 if (low_tpt
> current_tpt
) {
2056 "decrease rate because of low tpt\n");
2058 } else if (sr
>= IWL_RATE_INCREASE_TH
) {
2064 /* Sanity check; asked for decrease, but success rate or throughput
2065 * has been good at old rate. Don't change it. */
2066 if ((scale_action
== -1) && (low
!= IWL_RATE_INVALID
) &&
2067 ((sr
> IWL_RATE_HIGH_TH
) ||
2068 (current_tpt
> (100 * tbl
->expected_tpt
[low
]))))
2071 if ((le32_to_cpu(mvm
->last_bt_notif
.bt_activity_grading
) >=
2072 IWL_BT_COEX_TRAFFIC_LOAD_HIGH
) && (is_mimo(tbl
->lq_type
))) {
2073 if (lq_sta
->last_bt_traffic
>
2074 le32_to_cpu(mvm
->last_bt_notif
.bt_activity_grading
)) {
2076 * don't set scale_action, don't want to scale up if
2077 * the rate scale doesn't otherwise think that is a
2080 } else if (lq_sta
->last_bt_traffic
<=
2081 le32_to_cpu(mvm
->last_bt_notif
.bt_activity_grading
)) {
2085 lq_sta
->last_bt_traffic
=
2086 le32_to_cpu(mvm
->last_bt_notif
.bt_activity_grading
);
2088 if ((le32_to_cpu(mvm
->last_bt_notif
.bt_activity_grading
) >=
2089 IWL_BT_COEX_TRAFFIC_LOAD_HIGH
) && is_mimo(tbl
->lq_type
)) {
2090 /* search for a new modulation */
2091 rs_stay_in_table(lq_sta
, true);
2095 switch (scale_action
) {
2097 /* Decrease starting rate, update uCode's rate table */
2098 if (low
!= IWL_RATE_INVALID
) {
2105 /* Increase starting rate, update uCode's rate table */
2106 if (high
!= IWL_RATE_INVALID
) {
2119 "choose rate scale index %d action %d low %d high %d type %d\n",
2120 index
, scale_action
, low
, high
, tbl
->lq_type
);
2123 /* Replace uCode's rate table for the destination station. */
2125 rs_update_rate_tbl(mvm
, lq_sta
, tbl
, index
, is_green
);
2127 rs_stay_in_table(lq_sta
, false);
2130 * Search for new modulation mode if we're:
2131 * 1) Not changing rates right now
2132 * 2) Not just finishing up a search
2133 * 3) Allowing a new search
2135 if (!update_lq
&& !done_search
&&
2136 !lq_sta
->stay_in_tbl
&& window
->counter
) {
2137 /* Save current throughput to compare with "search" throughput*/
2138 lq_sta
->last_tpt
= current_tpt
;
2140 /* Select a new "search" modulation mode to try.
2141 * If one is found, set up the new "search" table. */
2142 if (is_legacy(tbl
->lq_type
))
2143 rs_move_legacy_other(mvm
, lq_sta
, sta
, index
);
2144 else if (is_siso(tbl
->lq_type
))
2145 rs_move_siso_to_other(mvm
, lq_sta
, sta
, index
);
2146 else if (is_mimo2(tbl
->lq_type
))
2147 rs_move_mimo2_to_other(mvm
, lq_sta
, sta
, index
);
2151 /* If new "search" mode was selected, set up in uCode table */
2152 if (lq_sta
->search_better_tbl
) {
2153 /* Access the "search" table, clear its history. */
2154 tbl
= &(lq_sta
->lq_info
[(1 - lq_sta
->active_tbl
)]);
2155 for (i
= 0; i
< IWL_RATE_COUNT
; i
++)
2156 rs_rate_scale_clear_window(&(tbl
->win
[i
]));
2158 /* Use new "search" start rate */
2159 index
= iwl_hwrate_to_plcp_idx(tbl
->current_rate
);
2162 "Switch current mcs: %X index: %d\n",
2163 tbl
->current_rate
, index
);
2164 rs_fill_link_cmd(mvm
, lq_sta
, tbl
->current_rate
);
2165 iwl_mvm_send_lq_cmd(mvm
, &lq_sta
->lq
, CMD_ASYNC
, false);
2171 if (done_search
&& !lq_sta
->stay_in_tbl
) {
2172 /* If the "active" (non-search) mode was legacy,
2173 * and we've tried switching antennas,
2174 * but we haven't been able to try HT modes (not available),
2175 * stay with best antenna legacy modulation for a while
2176 * before next round of mode comparisons. */
2177 tbl1
= &(lq_sta
->lq_info
[lq_sta
->active_tbl
]);
2178 if (is_legacy(tbl1
->lq_type
) && !sta
->ht_cap
.ht_supported
&&
2179 lq_sta
->action_counter
> tbl1
->max_search
) {
2180 IWL_DEBUG_RATE(mvm
, "LQ: STAY in legacy table\n");
2181 rs_set_stay_in_table(mvm
, 1, lq_sta
);
2184 /* If we're in an HT mode, and all 3 mode switch actions
2185 * have been tried and compared, stay in this best modulation
2186 * mode for a while before next round of mode comparisons. */
2187 if (lq_sta
->enable_counter
&&
2188 (lq_sta
->action_counter
>= tbl1
->max_search
)) {
2189 if ((lq_sta
->last_tpt
> IWL_AGG_TPT_THREHOLD
) &&
2190 (lq_sta
->tx_agg_tid_en
& (1 << tid
)) &&
2191 (tid
!= IWL_MAX_TID_COUNT
)) {
2192 tid_data
= &sta_priv
->tid_data
[tid
];
2193 if (tid_data
->state
== IWL_AGG_OFF
) {
2195 "try to aggregate tid %d\n",
2197 rs_tl_turn_on_agg(mvm
, tid
,
2201 rs_set_stay_in_table(mvm
, 0, lq_sta
);
2206 tbl
->current_rate
= rate_n_flags_from_tbl(mvm
, tbl
, index
, is_green
);
2207 lq_sta
->last_txrate_idx
= index
;
2211 * rs_initialize_lq - Initialize a station's hardware rate table
2213 * The uCode's station table contains a table of fallback rates
2214 * for automatic fallback during transmission.
2216 * NOTE: This sets up a default set of values. These will be replaced later
2217 * if the driver's iwl-agn-rs rate scaling algorithm is used, instead of
2220 * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
2221 * calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
2222 * which requires station table entry to exist).
2224 static void rs_initialize_lq(struct iwl_mvm
*mvm
,
2225 struct ieee80211_sta
*sta
,
2226 struct iwl_lq_sta
*lq_sta
,
2227 enum ieee80211_band band
)
2229 struct iwl_scale_tbl_info
*tbl
;
2233 u8 use_green
= rs_use_green(sta
);
2237 if (!sta
|| !lq_sta
)
2240 i
= lq_sta
->last_txrate_idx
;
2242 valid_tx_ant
= iwl_fw_valid_tx_ant(mvm
->fw
);
2244 if (!lq_sta
->search_better_tbl
)
2245 active_tbl
= lq_sta
->active_tbl
;
2247 active_tbl
= 1 - lq_sta
->active_tbl
;
2249 tbl
= &(lq_sta
->lq_info
[active_tbl
]);
2251 if ((i
< 0) || (i
>= IWL_RATE_COUNT
))
2254 rate
= iwl_rates
[i
].plcp
;
2255 tbl
->ant_type
= first_antenna(valid_tx_ant
);
2256 rate
|= tbl
->ant_type
<< RATE_MCS_ANT_POS
;
2258 if (i
>= IWL_FIRST_CCK_RATE
&& i
<= IWL_LAST_CCK_RATE
)
2259 rate
|= RATE_MCS_CCK_MSK
;
2261 rs_get_tbl_info_from_mcs(rate
, band
, tbl
, &rate_idx
);
2262 if (!rs_is_valid_ant(valid_tx_ant
, tbl
->ant_type
))
2263 rs_toggle_antenna(valid_tx_ant
, &rate
, tbl
);
2265 rate
= rate_n_flags_from_tbl(mvm
, tbl
, rate_idx
, use_green
);
2266 tbl
->current_rate
= rate
;
2267 rs_set_expected_tpt_table(lq_sta
, tbl
);
2268 rs_fill_link_cmd(NULL
, lq_sta
, rate
);
2269 /* TODO restore station should remember the lq cmd */
2270 iwl_mvm_send_lq_cmd(mvm
, &lq_sta
->lq
, CMD_SYNC
, true);
2273 static void rs_get_rate(void *mvm_r
, struct ieee80211_sta
*sta
, void *mvm_sta
,
2274 struct ieee80211_tx_rate_control
*txrc
)
2276 struct sk_buff
*skb
= txrc
->skb
;
2277 struct ieee80211_supported_band
*sband
= txrc
->sband
;
2278 struct iwl_op_mode
*op_mode __maybe_unused
=
2279 (struct iwl_op_mode
*)mvm_r
;
2280 struct iwl_mvm
*mvm __maybe_unused
= IWL_OP_MODE_GET_MVM(op_mode
);
2281 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
2282 struct iwl_lq_sta
*lq_sta
= mvm_sta
;
2284 IWL_DEBUG_RATE_LIMIT(mvm
, "rate scale calculate new rate for skb\n");
2286 /* Get max rate if user set max rate */
2288 lq_sta
->max_rate_idx
= txrc
->max_rate_idx
;
2289 if ((sband
->band
== IEEE80211_BAND_5GHZ
) &&
2290 (lq_sta
->max_rate_idx
!= -1))
2291 lq_sta
->max_rate_idx
+= IWL_FIRST_OFDM_RATE
;
2292 if ((lq_sta
->max_rate_idx
< 0) ||
2293 (lq_sta
->max_rate_idx
>= IWL_RATE_COUNT
))
2294 lq_sta
->max_rate_idx
= -1;
2297 /* Treat uninitialized rate scaling data same as non-existing. */
2298 if (lq_sta
&& !lq_sta
->drv
) {
2299 IWL_DEBUG_RATE(mvm
, "Rate scaling not initialized yet.\n");
2303 /* Send management frames and NO_ACK data using lowest rate. */
2304 if (rate_control_send_low(sta
, mvm_sta
, txrc
))
2307 iwl_mvm_hwrate_to_tx_rate(lq_sta
->last_rate_n_flags
,
2308 info
->band
, &info
->control
.rates
[0]);
2310 info
->control
.rates
[0].count
= 1;
2313 static void *rs_alloc_sta(void *mvm_rate
, struct ieee80211_sta
*sta
,
2316 struct iwl_mvm_sta
*sta_priv
= (struct iwl_mvm_sta
*)sta
->drv_priv
;
2317 struct iwl_op_mode
*op_mode __maybe_unused
=
2318 (struct iwl_op_mode
*)mvm_rate
;
2319 struct iwl_mvm
*mvm __maybe_unused
= IWL_OP_MODE_GET_MVM(op_mode
);
2321 IWL_DEBUG_RATE(mvm
, "create station rate scale window\n");
2323 return &sta_priv
->lq_sta
;
2326 static int rs_vht_highest_rx_mcs_index(struct ieee80211_sta_vht_cap
*vht_cap
,
2329 u16 rx_mcs
= le16_to_cpu(vht_cap
->vht_mcs
.rx_mcs_map
) &
2330 (0x3 << (2 * (nss
- 1)));
2331 rx_mcs
>>= (2 * (nss
- 1));
2333 if (rx_mcs
== IEEE80211_VHT_MCS_SUPPORT_0_7
)
2334 return IWL_RATE_MCS_7_INDEX
;
2335 else if (rx_mcs
== IEEE80211_VHT_MCS_SUPPORT_0_8
)
2336 return IWL_RATE_MCS_8_INDEX
;
2337 else if (rx_mcs
== IEEE80211_VHT_MCS_SUPPORT_0_9
)
2338 return IWL_RATE_MCS_9_INDEX
;
2340 WARN_ON_ONCE(rx_mcs
!= IEEE80211_VHT_MCS_NOT_SUPPORTED
);
2345 * Called after adding a new station to initialize rate scaling
2347 void iwl_mvm_rs_rate_init(struct iwl_mvm
*mvm
, struct ieee80211_sta
*sta
,
2348 enum ieee80211_band band
)
2351 struct ieee80211_hw
*hw
= mvm
->hw
;
2352 struct ieee80211_sta_ht_cap
*ht_cap
= &sta
->ht_cap
;
2353 struct ieee80211_sta_vht_cap
*vht_cap
= &sta
->vht_cap
;
2354 struct iwl_mvm_sta
*sta_priv
;
2355 struct iwl_lq_sta
*lq_sta
;
2356 struct ieee80211_supported_band
*sband
;
2357 unsigned long supp
; /* must be unsigned long for for_each_set_bit */
2359 sta_priv
= (struct iwl_mvm_sta
*)sta
->drv_priv
;
2360 lq_sta
= &sta_priv
->lq_sta
;
2361 sband
= hw
->wiphy
->bands
[band
];
2363 lq_sta
->lq
.sta_id
= sta_priv
->sta_id
;
2365 for (j
= 0; j
< LQ_SIZE
; j
++)
2366 for (i
= 0; i
< IWL_RATE_COUNT
; i
++)
2367 rs_rate_scale_clear_window(&lq_sta
->lq_info
[j
].win
[i
]);
2369 lq_sta
->flush_timer
= 0;
2370 lq_sta
->supp_rates
= sta
->supp_rates
[sband
->band
];
2373 "LQ: *** rate scale station global init for station %d ***\n",
2375 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2376 * the lowest or the highest rate.. Could consider using RSSI from
2377 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2380 lq_sta
->max_rate_idx
= -1;
2381 lq_sta
->missed_rate_counter
= IWL_MISSED_RATE_MAX
;
2382 lq_sta
->is_green
= rs_use_green(sta
);
2383 lq_sta
->band
= sband
->band
;
2385 * active legacy rates as per supported rates bitmap
2387 supp
= sta
->supp_rates
[sband
->band
];
2388 lq_sta
->active_legacy_rate
= 0;
2389 for_each_set_bit(i
, &supp
, BITS_PER_LONG
)
2390 lq_sta
->active_legacy_rate
|= BIT(sband
->bitrates
[i
].hw_value
);
2392 /* TODO: should probably account for rx_highest for both HT/VHT */
2393 if (!vht_cap
|| !vht_cap
->vht_supported
) {
2394 /* active_siso_rate mask includes 9 MBits (bit 5),
2395 * and CCK (bits 0-3), supp_rates[] does not;
2396 * shift to convert format, force 9 MBits off.
2398 lq_sta
->active_siso_rate
= ht_cap
->mcs
.rx_mask
[0] << 1;
2399 lq_sta
->active_siso_rate
|= ht_cap
->mcs
.rx_mask
[0] & 0x1;
2400 lq_sta
->active_siso_rate
&= ~((u16
)0x2);
2401 lq_sta
->active_siso_rate
<<= IWL_FIRST_OFDM_RATE
;
2404 lq_sta
->active_mimo2_rate
= ht_cap
->mcs
.rx_mask
[1] << 1;
2405 lq_sta
->active_mimo2_rate
|= ht_cap
->mcs
.rx_mask
[1] & 0x1;
2406 lq_sta
->active_mimo2_rate
&= ~((u16
)0x2);
2407 lq_sta
->active_mimo2_rate
<<= IWL_FIRST_OFDM_RATE
;
2409 lq_sta
->is_vht
= false;
2411 int highest_mcs
= rs_vht_highest_rx_mcs_index(vht_cap
, 1);
2412 if (highest_mcs
>= IWL_RATE_MCS_0_INDEX
) {
2413 for (i
= IWL_RATE_MCS_0_INDEX
; i
<= highest_mcs
; i
++) {
2414 if (i
== IWL_RATE_9M_INDEX
)
2417 lq_sta
->active_siso_rate
|= BIT(i
);
2421 highest_mcs
= rs_vht_highest_rx_mcs_index(vht_cap
, 2);
2422 if (highest_mcs
>= IWL_RATE_MCS_0_INDEX
) {
2423 for (i
= IWL_RATE_MCS_0_INDEX
; i
<= highest_mcs
; i
++) {
2424 if (i
== IWL_RATE_9M_INDEX
)
2427 lq_sta
->active_mimo2_rate
|= BIT(i
);
2431 /* TODO: avoid MCS9 in 20Mhz which isn't valid for 11ac */
2432 lq_sta
->is_vht
= true;
2436 "SISO-RATE=%X MIMO2-RATE=%X VHT=%d\n",
2437 lq_sta
->active_siso_rate
,
2438 lq_sta
->active_mimo2_rate
,
2441 /* These values will be overridden later */
2442 lq_sta
->lq
.single_stream_ant_msk
=
2443 first_antenna(iwl_fw_valid_tx_ant(mvm
->fw
));
2444 lq_sta
->lq
.dual_stream_ant_msk
=
2445 iwl_fw_valid_tx_ant(mvm
->fw
) &
2446 ~first_antenna(iwl_fw_valid_tx_ant(mvm
->fw
));
2447 if (!lq_sta
->lq
.dual_stream_ant_msk
) {
2448 lq_sta
->lq
.dual_stream_ant_msk
= ANT_AB
;
2449 } else if (num_of_ant(iwl_fw_valid_tx_ant(mvm
->fw
)) == 2) {
2450 lq_sta
->lq
.dual_stream_ant_msk
=
2451 iwl_fw_valid_tx_ant(mvm
->fw
);
2454 /* as default allow aggregation for all tids */
2455 lq_sta
->tx_agg_tid_en
= IWL_AGG_ALL_TID
;
2458 /* Set last_txrate_idx to lowest rate */
2459 lq_sta
->last_txrate_idx
= rate_lowest_index(sband
, sta
);
2460 if (sband
->band
== IEEE80211_BAND_5GHZ
)
2461 lq_sta
->last_txrate_idx
+= IWL_FIRST_OFDM_RATE
;
2463 #ifdef CONFIG_MAC80211_DEBUGFS
2464 lq_sta
->dbg_fixed_rate
= 0;
2467 rs_initialize_lq(mvm
, sta
, lq_sta
, band
);
2470 static void rs_fill_link_cmd(struct iwl_mvm
*mvm
,
2471 struct iwl_lq_sta
*lq_sta
, u32 new_rate
)
2473 struct iwl_scale_tbl_info tbl_type
;
2476 int repeat_rate
= 0;
2477 u8 ant_toggle_cnt
= 0;
2478 u8 use_ht_possible
= 1;
2479 u8 valid_tx_ant
= 0;
2480 struct iwl_lq_cmd
*lq_cmd
= &lq_sta
->lq
;
2482 /* Override starting rate (index 0) if needed for debug purposes */
2483 rs_dbgfs_set_mcs(lq_sta
, &new_rate
);
2485 /* Interpret new_rate (rate_n_flags) */
2486 rs_get_tbl_info_from_mcs(new_rate
, lq_sta
->band
,
2487 &tbl_type
, &rate_idx
);
2489 /* How many times should we repeat the initial rate? */
2490 if (is_legacy(tbl_type
.lq_type
)) {
2492 repeat_rate
= IWL_NUMBER_TRY
;
2494 repeat_rate
= min(IWL_HT_NUMBER_TRY
,
2495 LINK_QUAL_AGG_DISABLE_START_DEF
- 1);
2498 lq_cmd
->mimo_delim
= is_mimo(tbl_type
.lq_type
) ? 1 : 0;
2500 /* Fill 1st table entry (index 0) */
2501 lq_cmd
->rs_table
[index
] = cpu_to_le32(new_rate
);
2503 if (num_of_ant(tbl_type
.ant_type
) == 1)
2504 lq_cmd
->single_stream_ant_msk
= tbl_type
.ant_type
;
2505 else if (num_of_ant(tbl_type
.ant_type
) == 2)
2506 lq_cmd
->dual_stream_ant_msk
= tbl_type
.ant_type
;
2507 /* otherwise we don't modify the existing value */
2512 valid_tx_ant
= iwl_fw_valid_tx_ant(mvm
->fw
);
2514 /* Fill rest of rate table */
2515 while (index
< LINK_QUAL_MAX_RETRY_NUM
) {
2516 /* Repeat initial/next rate.
2517 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2518 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
2519 while (repeat_rate
> 0 && (index
< LINK_QUAL_MAX_RETRY_NUM
)) {
2520 if (is_legacy(tbl_type
.lq_type
)) {
2521 if (ant_toggle_cnt
< NUM_TRY_BEFORE_ANT_TOGGLE
)
2524 rs_toggle_antenna(valid_tx_ant
,
2525 &new_rate
, &tbl_type
))
2529 /* Override next rate if needed for debug purposes */
2530 rs_dbgfs_set_mcs(lq_sta
, &new_rate
);
2532 /* Fill next table entry */
2533 lq_cmd
->rs_table
[index
] =
2534 cpu_to_le32(new_rate
);
2539 rs_get_tbl_info_from_mcs(new_rate
, lq_sta
->band
, &tbl_type
,
2542 /* Indicate to uCode which entries might be MIMO.
2543 * If initial rate was MIMO, this will finally end up
2544 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
2545 if (is_mimo(tbl_type
.lq_type
))
2546 lq_cmd
->mimo_delim
= index
;
2549 new_rate
= rs_get_lower_rate(lq_sta
, &tbl_type
, rate_idx
,
2552 /* How many times should we repeat the next rate? */
2553 if (is_legacy(tbl_type
.lq_type
)) {
2554 if (ant_toggle_cnt
< NUM_TRY_BEFORE_ANT_TOGGLE
)
2557 rs_toggle_antenna(valid_tx_ant
,
2558 &new_rate
, &tbl_type
))
2561 repeat_rate
= IWL_NUMBER_TRY
;
2563 repeat_rate
= IWL_HT_NUMBER_TRY
;
2566 /* Don't allow HT rates after next pass.
2567 * rs_get_lower_rate() will change type to LQ_LEGACY_A
2570 use_ht_possible
= 0;
2572 /* Override next rate if needed for debug purposes */
2573 rs_dbgfs_set_mcs(lq_sta
, &new_rate
);
2575 /* Fill next table entry */
2576 lq_cmd
->rs_table
[index
] = cpu_to_le32(new_rate
);
2582 lq_cmd
->agg_frame_cnt_limit
= LINK_QUAL_AGG_FRAME_LIMIT_DEF
;
2583 lq_cmd
->agg_disable_start_th
= LINK_QUAL_AGG_DISABLE_START_DEF
;
2585 lq_cmd
->agg_time_limit
=
2586 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF
);
2589 * overwrite if needed, pass aggregation time limit
2590 * to uCode in uSec - This is racy - but heh, at least it helps...
2592 if (mvm
&& le32_to_cpu(mvm
->last_bt_notif
.bt_activity_grading
) >= 2)
2593 lq_cmd
->agg_time_limit
= cpu_to_le16(1200);
2596 static void *rs_alloc(struct ieee80211_hw
*hw
, struct dentry
*debugfsdir
)
2600 /* rate scale requires free function to be implemented */
2601 static void rs_free(void *mvm_rate
)
2606 static void rs_free_sta(void *mvm_r
, struct ieee80211_sta
*sta
,
2609 struct iwl_op_mode
*op_mode __maybe_unused
= mvm_r
;
2610 struct iwl_mvm
*mvm __maybe_unused
= IWL_OP_MODE_GET_MVM(op_mode
);
2612 IWL_DEBUG_RATE(mvm
, "enter\n");
2613 IWL_DEBUG_RATE(mvm
, "leave\n");
2616 #ifdef CONFIG_MAC80211_DEBUGFS
2617 static void rs_dbgfs_set_mcs(struct iwl_lq_sta
*lq_sta
,
2620 struct iwl_mvm
*mvm
;
2625 valid_tx_ant
= iwl_fw_valid_tx_ant(mvm
->fw
);
2626 if (lq_sta
->dbg_fixed_rate
) {
2628 ((lq_sta
->dbg_fixed_rate
& RATE_MCS_ANT_ABC_MSK
)
2629 >> RATE_MCS_ANT_POS
);
2630 if ((valid_tx_ant
& ant_sel_tx
) == ant_sel_tx
) {
2631 *rate_n_flags
= lq_sta
->dbg_fixed_rate
;
2632 IWL_DEBUG_RATE(mvm
, "Fixed rate ON\n");
2634 lq_sta
->dbg_fixed_rate
= 0;
2636 "Invalid antenna selection 0x%X, Valid is 0x%X\n",
2637 ant_sel_tx
, valid_tx_ant
);
2638 IWL_DEBUG_RATE(mvm
, "Fixed rate OFF\n");
2641 IWL_DEBUG_RATE(mvm
, "Fixed rate OFF\n");
2645 static ssize_t
rs_sta_dbgfs_scale_table_write(struct file
*file
,
2646 const char __user
*user_buf
, size_t count
, loff_t
*ppos
)
2648 struct iwl_lq_sta
*lq_sta
= file
->private_data
;
2649 struct iwl_mvm
*mvm
;
2656 memset(buf
, 0, sizeof(buf
));
2657 buf_size
= min(count
, sizeof(buf
) - 1);
2658 if (copy_from_user(buf
, user_buf
, buf_size
))
2661 if (sscanf(buf
, "%x", &parsed_rate
) == 1)
2662 lq_sta
->dbg_fixed_rate
= parsed_rate
;
2664 lq_sta
->dbg_fixed_rate
= 0;
2666 rs_program_fix_rate(mvm
, lq_sta
);
2671 static ssize_t
rs_sta_dbgfs_scale_table_read(struct file
*file
,
2672 char __user
*user_buf
, size_t count
, loff_t
*ppos
)
2680 struct iwl_lq_sta
*lq_sta
= file
->private_data
;
2681 struct iwl_mvm
*mvm
;
2682 struct iwl_scale_tbl_info
*tbl
= &(lq_sta
->lq_info
[lq_sta
->active_tbl
]);
2685 buff
= kmalloc(1024, GFP_KERNEL
);
2689 desc
+= sprintf(buff
+desc
, "sta_id %d\n", lq_sta
->lq
.sta_id
);
2690 desc
+= sprintf(buff
+desc
, "failed=%d success=%d rate=0%X\n",
2691 lq_sta
->total_failed
, lq_sta
->total_success
,
2692 lq_sta
->active_legacy_rate
);
2693 desc
+= sprintf(buff
+desc
, "fixed rate 0x%X\n",
2694 lq_sta
->dbg_fixed_rate
);
2695 desc
+= sprintf(buff
+desc
, "valid_tx_ant %s%s%s\n",
2696 (iwl_fw_valid_tx_ant(mvm
->fw
) & ANT_A
) ? "ANT_A," : "",
2697 (iwl_fw_valid_tx_ant(mvm
->fw
) & ANT_B
) ? "ANT_B," : "",
2698 (iwl_fw_valid_tx_ant(mvm
->fw
) & ANT_C
) ? "ANT_C" : "");
2699 desc
+= sprintf(buff
+desc
, "lq type %s\n",
2700 (is_legacy(tbl
->lq_type
)) ? "legacy" :
2701 is_vht(tbl
->lq_type
) ? "VHT" : "HT");
2702 if (is_ht(tbl
->lq_type
)) {
2703 desc
+= sprintf(buff
+desc
, " %s",
2704 (is_siso(tbl
->lq_type
)) ? "SISO" : "MIMO2");
2705 desc
+= sprintf(buff
+desc
, " %s",
2706 (is_ht20(tbl
)) ? "20MHz" :
2707 (is_ht40(tbl
)) ? "40MHz" :
2708 (is_ht80(tbl
)) ? "80Mhz" : "BAD BW");
2709 desc
+= sprintf(buff
+desc
, " %s %s %s\n",
2710 (tbl
->is_SGI
) ? "SGI" : "",
2711 (lq_sta
->is_green
) ? "GF enabled" : "",
2712 (lq_sta
->is_agg
) ? "AGG on" : "");
2714 desc
+= sprintf(buff
+desc
, "last tx rate=0x%X\n",
2715 lq_sta
->last_rate_n_flags
);
2716 desc
+= sprintf(buff
+desc
,
2717 "general: flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
2719 lq_sta
->lq
.mimo_delim
,
2720 lq_sta
->lq
.single_stream_ant_msk
,
2721 lq_sta
->lq
.dual_stream_ant_msk
);
2723 desc
+= sprintf(buff
+desc
,
2724 "agg: time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
2725 le16_to_cpu(lq_sta
->lq
.agg_time_limit
),
2726 lq_sta
->lq
.agg_disable_start_th
,
2727 lq_sta
->lq
.agg_frame_cnt_limit
);
2729 desc
+= sprintf(buff
+desc
,
2730 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
2731 lq_sta
->lq
.initial_rate_index
[0],
2732 lq_sta
->lq
.initial_rate_index
[1],
2733 lq_sta
->lq
.initial_rate_index
[2],
2734 lq_sta
->lq
.initial_rate_index
[3]);
2736 for (i
= 0; i
< LINK_QUAL_MAX_RETRY_NUM
; i
++) {
2737 index
= iwl_hwrate_to_plcp_idx(
2738 le32_to_cpu(lq_sta
->lq
.rs_table
[i
]));
2739 if (is_legacy(tbl
->lq_type
)) {
2740 desc
+= sprintf(buff
+desc
, " rate[%d] 0x%X %smbps\n",
2741 i
, le32_to_cpu(lq_sta
->lq
.rs_table
[i
]),
2742 iwl_rate_mcs
[index
].mbps
);
2744 desc
+= sprintf(buff
+desc
,
2745 " rate[%d] 0x%X %smbps (%s)\n",
2746 i
, le32_to_cpu(lq_sta
->lq
.rs_table
[i
]),
2747 iwl_rate_mcs
[index
].mbps
,
2748 iwl_rate_mcs
[index
].mcs
);
2752 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buff
, desc
);
2757 static const struct file_operations rs_sta_dbgfs_scale_table_ops
= {
2758 .write
= rs_sta_dbgfs_scale_table_write
,
2759 .read
= rs_sta_dbgfs_scale_table_read
,
2760 .open
= simple_open
,
2761 .llseek
= default_llseek
,
2763 static ssize_t
rs_sta_dbgfs_stats_table_read(struct file
*file
,
2764 char __user
*user_buf
, size_t count
, loff_t
*ppos
)
2770 struct iwl_scale_tbl_info
*tbl
;
2771 struct iwl_lq_sta
*lq_sta
= file
->private_data
;
2773 buff
= kmalloc(1024, GFP_KERNEL
);
2777 for (i
= 0; i
< LQ_SIZE
; i
++) {
2778 tbl
= &(lq_sta
->lq_info
[i
]);
2779 desc
+= sprintf(buff
+desc
,
2780 "%s type=%d SGI=%d BW=%s DUP=0 GF=%d\n"
2782 lq_sta
->active_tbl
== i
? "*" : "x",
2785 is_ht20(tbl
) ? "20Mhz" :
2786 is_ht40(tbl
) ? "40Mhz" :
2787 is_ht80(tbl
) ? "80Mhz" : "ERR",
2790 for (j
= 0; j
< IWL_RATE_COUNT
; j
++) {
2791 desc
+= sprintf(buff
+desc
,
2792 "counter=%d success=%d %%=%d\n",
2793 tbl
->win
[j
].counter
,
2794 tbl
->win
[j
].success_counter
,
2795 tbl
->win
[j
].success_ratio
);
2798 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buff
, desc
);
2803 static const struct file_operations rs_sta_dbgfs_stats_table_ops
= {
2804 .read
= rs_sta_dbgfs_stats_table_read
,
2805 .open
= simple_open
,
2806 .llseek
= default_llseek
,
2809 static void rs_add_debugfs(void *mvm
, void *mvm_sta
, struct dentry
*dir
)
2811 struct iwl_lq_sta
*lq_sta
= mvm_sta
;
2812 lq_sta
->rs_sta_dbgfs_scale_table_file
=
2813 debugfs_create_file("rate_scale_table", S_IRUSR
| S_IWUSR
, dir
,
2814 lq_sta
, &rs_sta_dbgfs_scale_table_ops
);
2815 lq_sta
->rs_sta_dbgfs_stats_table_file
=
2816 debugfs_create_file("rate_stats_table", S_IRUSR
, dir
,
2817 lq_sta
, &rs_sta_dbgfs_stats_table_ops
);
2818 lq_sta
->rs_sta_dbgfs_tx_agg_tid_en_file
=
2819 debugfs_create_u8("tx_agg_tid_enable", S_IRUSR
| S_IWUSR
, dir
,
2820 &lq_sta
->tx_agg_tid_en
);
2823 static void rs_remove_debugfs(void *mvm
, void *mvm_sta
)
2825 struct iwl_lq_sta
*lq_sta
= mvm_sta
;
2826 debugfs_remove(lq_sta
->rs_sta_dbgfs_scale_table_file
);
2827 debugfs_remove(lq_sta
->rs_sta_dbgfs_stats_table_file
);
2828 debugfs_remove(lq_sta
->rs_sta_dbgfs_tx_agg_tid_en_file
);
2833 * Initialization of rate scaling information is done by driver after
2834 * the station is added. Since mac80211 calls this function before a
2835 * station is added we ignore it.
2837 static void rs_rate_init_stub(void *mvm_r
,
2838 struct ieee80211_supported_band
*sband
,
2839 struct cfg80211_chan_def
*chandef
,
2840 struct ieee80211_sta
*sta
, void *mvm_sta
)
2843 static struct rate_control_ops rs_mvm_ops
= {
2846 .tx_status
= rs_tx_status
,
2847 .get_rate
= rs_get_rate
,
2848 .rate_init
= rs_rate_init_stub
,
2851 .alloc_sta
= rs_alloc_sta
,
2852 .free_sta
= rs_free_sta
,
2853 #ifdef CONFIG_MAC80211_DEBUGFS
2854 .add_sta_debugfs
= rs_add_debugfs
,
2855 .remove_sta_debugfs
= rs_remove_debugfs
,
2859 int iwl_mvm_rate_control_register(void)
2861 return ieee80211_rate_control_register(&rs_mvm_ops
);
2864 void iwl_mvm_rate_control_unregister(void)
2866 ieee80211_rate_control_unregister(&rs_mvm_ops
);
2870 * iwl_mvm_tx_protection - Gets LQ command, change it to enable/disable
2871 * Tx protection, according to this rquest and previous requests,
2872 * and send the LQ command.
2873 * @mvmsta: The station
2874 * @enable: Enable Tx protection?
2876 int iwl_mvm_tx_protection(struct iwl_mvm
*mvm
, struct iwl_mvm_sta
*mvmsta
,
2879 struct iwl_lq_cmd
*lq
= &mvmsta
->lq_sta
.lq
;
2881 lockdep_assert_held(&mvm
->mutex
);
2884 if (mvmsta
->tx_protection
== 0)
2885 lq
->flags
|= LQ_FLAG_SET_STA_TLC_RTS_MSK
;
2886 mvmsta
->tx_protection
++;
2888 mvmsta
->tx_protection
--;
2889 if (mvmsta
->tx_protection
== 0)
2890 lq
->flags
&= ~LQ_FLAG_SET_STA_TLC_RTS_MSK
;
2893 return iwl_mvm_send_lq_cmd(mvm
, lq
, CMD_ASYNC
, false);