]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/net/wireless/iwlwifi/mvm/rs.c
iwlwifi: mvm/trans: abort d0i3_enter in case of held ref
[mirror_ubuntu-artful-kernel.git] / drivers / net / wireless / iwlwifi / mvm / rs.c
CommitLineData
8ca151b5
JB
1/******************************************************************************
2 *
51368bf7 3 * Copyright(c) 2005 - 2014 Intel Corporation. All rights reserved.
8b4139dc 4 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
8ca151b5
JB
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of version 2 of the GNU General Public License as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
18 *
19 * The full GNU General Public License is included in this distribution in the
20 * file called LICENSE.
21 *
22 * Contact Information:
23 * Intel Linux Wireless <ilw@linux.intel.com>
24 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
25 *
26 *****************************************************************************/
27#include <linux/kernel.h>
8ca151b5
JB
28#include <linux/skbuff.h>
29#include <linux/slab.h>
30#include <net/mac80211.h>
31
32#include <linux/netdevice.h>
33#include <linux/etherdevice.h>
34#include <linux/delay.h>
35
36#include <linux/workqueue.h>
37#include "rs.h"
38#include "fw-api.h"
39#include "sta.h"
40#include "iwl-op-mode.h"
41#include "mvm.h"
42
43#define RS_NAME "iwl-mvm-rs"
44
e07be6d3
ES
45#define NUM_TRY_BEFORE_ANT_TOGGLE 1
46#define RS_LEGACY_RETRIES_PER_RATE 1
47#define RS_HT_VHT_RETRIES_PER_RATE 2
48#define RS_HT_VHT_RETRIES_PER_RATE_TW 1
49#define RS_INITIAL_MIMO_NUM_RATES 3
50#define RS_INITIAL_SISO_NUM_RATES 3
51#define RS_INITIAL_LEGACY_NUM_RATES LINK_QUAL_MAX_RETRY_NUM
52#define RS_SECONDARY_LEGACY_NUM_RATES LINK_QUAL_MAX_RETRY_NUM
53#define RS_SECONDARY_SISO_NUM_RATES 3
54#define RS_SECONDARY_SISO_RETRIES 1
8ca151b5
JB
55
56#define IWL_RATE_MAX_WINDOW 62 /* # tx in history window */
d17334c4 57#define IWL_RATE_MIN_FAILURE_TH 3 /* min failures to calc tpt */
8ca151b5
JB
58#define IWL_RATE_MIN_SUCCESS_TH 8 /* min successes to calc tpt */
59
60/* max allowed rate miss before sync LQ cmd */
61#define IWL_MISSED_RATE_MAX 15
32b01726 62#define RS_STAY_IN_COLUMN_TIMEOUT (5*HZ)
87d5e415 63#define RS_IDLE_TIMEOUT (5*HZ)
8ca151b5
JB
64
65static u8 rs_ht_to_legacy[] = {
da87d7d5
ES
66 [IWL_RATE_MCS_0_INDEX] = IWL_RATE_6M_INDEX,
67 [IWL_RATE_MCS_1_INDEX] = IWL_RATE_9M_INDEX,
68 [IWL_RATE_MCS_2_INDEX] = IWL_RATE_12M_INDEX,
69 [IWL_RATE_MCS_3_INDEX] = IWL_RATE_18M_INDEX,
70 [IWL_RATE_MCS_4_INDEX] = IWL_RATE_24M_INDEX,
71 [IWL_RATE_MCS_5_INDEX] = IWL_RATE_36M_INDEX,
72 [IWL_RATE_MCS_6_INDEX] = IWL_RATE_48M_INDEX,
73 [IWL_RATE_MCS_7_INDEX] = IWL_RATE_54M_INDEX,
74 [IWL_RATE_MCS_8_INDEX] = IWL_RATE_54M_INDEX,
75 [IWL_RATE_MCS_9_INDEX] = IWL_RATE_54M_INDEX,
8ca151b5
JB
76};
77
78static const u8 ant_toggle_lookup[] = {
ef4394b9
EP
79 [ANT_NONE] = ANT_NONE,
80 [ANT_A] = ANT_B,
81 [ANT_B] = ANT_C,
82 [ANT_AB] = ANT_BC,
83 [ANT_C] = ANT_A,
84 [ANT_AC] = ANT_AB,
85 [ANT_BC] = ANT_AC,
86 [ANT_ABC] = ANT_ABC,
8ca151b5
JB
87};
88
d310e405
ES
89#define IWL_DECLARE_RATE_INFO(r, s, rp, rn) \
90 [IWL_RATE_##r##M_INDEX] = { IWL_RATE_##r##M_PLCP, \
91 IWL_RATE_HT_SISO_MCS_##s##_PLCP, \
92 IWL_RATE_HT_MIMO2_MCS_##s##_PLCP, \
93 IWL_RATE_VHT_SISO_MCS_##s##_PLCP, \
94 IWL_RATE_VHT_MIMO2_MCS_##s##_PLCP,\
95 IWL_RATE_##rp##M_INDEX, \
ab055ce9 96 IWL_RATE_##rn##M_INDEX }
8ca151b5 97
d310e405
ES
98#define IWL_DECLARE_MCS_RATE(s) \
99 [IWL_RATE_MCS_##s##_INDEX] = { IWL_RATE_INVM_PLCP, \
100 IWL_RATE_HT_SISO_MCS_##s##_PLCP, \
101 IWL_RATE_HT_MIMO2_MCS_##s##_PLCP, \
102 IWL_RATE_VHT_SISO_MCS_##s##_PLCP, \
103 IWL_RATE_VHT_MIMO2_MCS_##s##_PLCP, \
104 IWL_RATE_INVM_INDEX, \
105 IWL_RATE_INVM_INDEX }
106
8ca151b5
JB
107/*
108 * Parameter order:
ab055ce9 109 * rate, ht rate, prev rate, next rate
8ca151b5
JB
110 *
111 * If there isn't a valid next or previous rate then INV is used which
112 * maps to IWL_RATE_INVALID
113 *
114 */
115static const struct iwl_rs_rate_info iwl_rates[IWL_RATE_COUNT] = {
ab055ce9
ES
116 IWL_DECLARE_RATE_INFO(1, INV, INV, 2), /* 1mbps */
117 IWL_DECLARE_RATE_INFO(2, INV, 1, 5), /* 2mbps */
118 IWL_DECLARE_RATE_INFO(5, INV, 2, 11), /*5.5mbps */
119 IWL_DECLARE_RATE_INFO(11, INV, 9, 12), /* 11mbps */
d310e405
ES
120 IWL_DECLARE_RATE_INFO(6, 0, 5, 11), /* 6mbps ; MCS 0 */
121 IWL_DECLARE_RATE_INFO(9, INV, 6, 11), /* 9mbps */
122 IWL_DECLARE_RATE_INFO(12, 1, 11, 18), /* 12mbps ; MCS 1 */
123 IWL_DECLARE_RATE_INFO(18, 2, 12, 24), /* 18mbps ; MCS 2 */
124 IWL_DECLARE_RATE_INFO(24, 3, 18, 36), /* 24mbps ; MCS 3 */
125 IWL_DECLARE_RATE_INFO(36, 4, 24, 48), /* 36mbps ; MCS 4 */
126 IWL_DECLARE_RATE_INFO(48, 5, 36, 54), /* 48mbps ; MCS 5 */
127 IWL_DECLARE_RATE_INFO(54, 6, 48, INV), /* 54mbps ; MCS 6 */
128 IWL_DECLARE_MCS_RATE(7), /* MCS 7 */
129 IWL_DECLARE_MCS_RATE(8), /* MCS 8 */
130 IWL_DECLARE_MCS_RATE(9), /* MCS 9 */
8ca151b5
JB
131};
132
4e4b815c
ES
133enum rs_action {
134 RS_ACTION_STAY = 0,
135 RS_ACTION_DOWNSCALE = -1,
136 RS_ACTION_UPSCALE = 1,
137};
138
b3b06a32
ES
139enum rs_column_mode {
140 RS_INVALID = 0,
141 RS_LEGACY,
142 RS_SISO,
143 RS_MIMO2,
144};
145
fd7dbee5 146#define MAX_NEXT_COLUMNS 7
b3b06a32
ES
147#define MAX_COLUMN_CHECKS 3
148
149typedef bool (*allow_column_func_t) (struct iwl_mvm *mvm,
150 struct ieee80211_sta *sta,
151 struct iwl_scale_tbl_info *tbl);
152
153struct rs_tx_column {
154 enum rs_column_mode mode;
155 u8 ant;
156 bool sgi;
157 enum rs_column next_columns[MAX_NEXT_COLUMNS];
158 allow_column_func_t checks[MAX_COLUMN_CHECKS];
159};
160
161static bool rs_mimo_allow(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
162 struct iwl_scale_tbl_info *tbl)
163{
164 if (!sta->ht_cap.ht_supported)
165 return false;
166
167 if (sta->smps_mode == IEEE80211_SMPS_STATIC)
168 return false;
169
4ed735e7 170 if (num_of_ant(mvm->fw->valid_tx_ant) < 2)
b3b06a32
ES
171 return false;
172
173 if (!iwl_mvm_bt_coex_is_mimo_allowed(mvm, sta))
174 return false;
175
176 return true;
177}
178
179static bool rs_siso_allow(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
180 struct iwl_scale_tbl_info *tbl)
181{
182 if (!sta->ht_cap.ht_supported)
183 return false;
184
185 return true;
186}
187
188static bool rs_sgi_allow(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
189 struct iwl_scale_tbl_info *tbl)
190{
191 struct rs_rate *rate = &tbl->rate;
192 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
193 struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
194
195 if (is_ht20(rate) && (ht_cap->cap &
196 IEEE80211_HT_CAP_SGI_20))
197 return true;
198 if (is_ht40(rate) && (ht_cap->cap &
199 IEEE80211_HT_CAP_SGI_40))
200 return true;
201 if (is_ht80(rate) && (vht_cap->cap &
202 IEEE80211_VHT_CAP_SHORT_GI_80))
203 return true;
204
205 return false;
206}
207
208static const struct rs_tx_column rs_tx_columns[] = {
209 [RS_COLUMN_LEGACY_ANT_A] = {
210 .mode = RS_LEGACY,
211 .ant = ANT_A,
212 .next_columns = {
213 RS_COLUMN_LEGACY_ANT_B,
214 RS_COLUMN_SISO_ANT_A,
31b20452 215 RS_COLUMN_MIMO2,
d8fff919
ES
216 RS_COLUMN_INVALID,
217 RS_COLUMN_INVALID,
fd7dbee5
ES
218 RS_COLUMN_INVALID,
219 RS_COLUMN_INVALID,
b3b06a32
ES
220 },
221 },
222 [RS_COLUMN_LEGACY_ANT_B] = {
223 .mode = RS_LEGACY,
224 .ant = ANT_B,
225 .next_columns = {
226 RS_COLUMN_LEGACY_ANT_A,
227 RS_COLUMN_SISO_ANT_B,
31b20452 228 RS_COLUMN_MIMO2,
d8fff919
ES
229 RS_COLUMN_INVALID,
230 RS_COLUMN_INVALID,
fd7dbee5
ES
231 RS_COLUMN_INVALID,
232 RS_COLUMN_INVALID,
b3b06a32
ES
233 },
234 },
235 [RS_COLUMN_SISO_ANT_A] = {
236 .mode = RS_SISO,
237 .ant = ANT_A,
238 .next_columns = {
239 RS_COLUMN_SISO_ANT_B,
240 RS_COLUMN_MIMO2,
241 RS_COLUMN_SISO_ANT_A_SGI,
fd7dbee5
ES
242 RS_COLUMN_LEGACY_ANT_A,
243 RS_COLUMN_LEGACY_ANT_B,
d8fff919 244 RS_COLUMN_INVALID,
31b20452 245 RS_COLUMN_INVALID,
b3b06a32
ES
246 },
247 .checks = {
248 rs_siso_allow,
249 },
250 },
251 [RS_COLUMN_SISO_ANT_B] = {
252 .mode = RS_SISO,
253 .ant = ANT_B,
254 .next_columns = {
255 RS_COLUMN_SISO_ANT_A,
256 RS_COLUMN_MIMO2,
257 RS_COLUMN_SISO_ANT_B_SGI,
fd7dbee5
ES
258 RS_COLUMN_LEGACY_ANT_A,
259 RS_COLUMN_LEGACY_ANT_B,
d8fff919 260 RS_COLUMN_INVALID,
31b20452 261 RS_COLUMN_INVALID,
b3b06a32
ES
262 },
263 .checks = {
264 rs_siso_allow,
265 },
266 },
267 [RS_COLUMN_SISO_ANT_A_SGI] = {
268 .mode = RS_SISO,
269 .ant = ANT_A,
270 .sgi = true,
271 .next_columns = {
272 RS_COLUMN_SISO_ANT_B_SGI,
273 RS_COLUMN_MIMO2_SGI,
274 RS_COLUMN_SISO_ANT_A,
fd7dbee5
ES
275 RS_COLUMN_LEGACY_ANT_A,
276 RS_COLUMN_LEGACY_ANT_B,
31b20452
ES
277 RS_COLUMN_INVALID,
278 RS_COLUMN_INVALID,
b3b06a32
ES
279 },
280 .checks = {
281 rs_siso_allow,
282 rs_sgi_allow,
283 },
284 },
285 [RS_COLUMN_SISO_ANT_B_SGI] = {
286 .mode = RS_SISO,
287 .ant = ANT_B,
288 .sgi = true,
289 .next_columns = {
290 RS_COLUMN_SISO_ANT_A_SGI,
291 RS_COLUMN_MIMO2_SGI,
292 RS_COLUMN_SISO_ANT_B,
fd7dbee5
ES
293 RS_COLUMN_LEGACY_ANT_A,
294 RS_COLUMN_LEGACY_ANT_B,
31b20452
ES
295 RS_COLUMN_INVALID,
296 RS_COLUMN_INVALID,
b3b06a32
ES
297 },
298 .checks = {
299 rs_siso_allow,
300 rs_sgi_allow,
301 },
302 },
303 [RS_COLUMN_MIMO2] = {
304 .mode = RS_MIMO2,
305 .ant = ANT_AB,
306 .next_columns = {
307 RS_COLUMN_SISO_ANT_A,
308 RS_COLUMN_MIMO2_SGI,
fd7dbee5
ES
309 RS_COLUMN_LEGACY_ANT_A,
310 RS_COLUMN_LEGACY_ANT_B,
31b20452
ES
311 RS_COLUMN_INVALID,
312 RS_COLUMN_INVALID,
313 RS_COLUMN_INVALID,
b3b06a32
ES
314 },
315 .checks = {
316 rs_mimo_allow,
317 },
318 },
319 [RS_COLUMN_MIMO2_SGI] = {
320 .mode = RS_MIMO2,
321 .ant = ANT_AB,
322 .sgi = true,
323 .next_columns = {
324 RS_COLUMN_SISO_ANT_A_SGI,
325 RS_COLUMN_MIMO2,
fd7dbee5
ES
326 RS_COLUMN_LEGACY_ANT_A,
327 RS_COLUMN_LEGACY_ANT_B,
31b20452
ES
328 RS_COLUMN_INVALID,
329 RS_COLUMN_INVALID,
330 RS_COLUMN_INVALID,
b3b06a32
ES
331 },
332 .checks = {
333 rs_mimo_allow,
334 rs_sgi_allow,
335 },
336 },
337};
338
8ca151b5
JB
339static inline u8 rs_extract_rate(u32 rate_n_flags)
340{
341 /* also works for HT because bits 7:6 are zero there */
342 return (u8)(rate_n_flags & RATE_LEGACY_RATE_MSK);
343}
344
345static int iwl_hwrate_to_plcp_idx(u32 rate_n_flags)
346{
347 int idx = 0;
348
8ca151b5 349 if (rate_n_flags & RATE_MCS_HT_MSK) {
d310e405
ES
350 idx = rate_n_flags & RATE_HT_MCS_RATE_CODE_MSK;
351 idx += IWL_RATE_MCS_0_INDEX;
8ca151b5 352
d310e405 353 /* skip 9M not supported in HT*/
8ca151b5
JB
354 if (idx >= IWL_RATE_9M_INDEX)
355 idx += 1;
4e82dd3a 356 if ((idx >= IWL_FIRST_HT_RATE) && (idx <= IWL_LAST_HT_RATE))
8ca151b5 357 return idx;
d310e405
ES
358 } else if (rate_n_flags & RATE_MCS_VHT_MSK) {
359 idx = rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK;
360 idx += IWL_RATE_MCS_0_INDEX;
8ca151b5 361
d310e405
ES
362 /* skip 9M not supported in VHT*/
363 if (idx >= IWL_RATE_9M_INDEX)
364 idx++;
365 if ((idx >= IWL_FIRST_VHT_RATE) && (idx <= IWL_LAST_VHT_RATE))
366 return idx;
8ca151b5 367 } else {
d310e405
ES
368 /* legacy rate format, search for match in table */
369
370 u8 legacy_rate = rs_extract_rate(rate_n_flags);
8ca151b5 371 for (idx = 0; idx < ARRAY_SIZE(iwl_rates); idx++)
d310e405 372 if (iwl_rates[idx].plcp == legacy_rate)
8ca151b5
JB
373 return idx;
374 }
375
560843f4 376 return IWL_RATE_INVALID;
8ca151b5
JB
377}
378
379static void rs_rate_scale_perform(struct iwl_mvm *mvm,
1ebc8f2e
ES
380 struct ieee80211_sta *sta,
381 struct iwl_lq_sta *lq_sta,
382 int tid);
8fc7c58c
ES
383static void rs_fill_lq_cmd(struct iwl_mvm *mvm,
384 struct ieee80211_sta *sta,
385 struct iwl_lq_sta *lq_sta,
386 const struct rs_rate *initial_rate);
8ca151b5 387static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search);
8ca151b5 388
8ca151b5
JB
389/**
390 * The following tables contain the expected throughput metrics for all rates
391 *
392 * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
393 *
394 * where invalid entries are zeros.
395 *
396 * CCK rates are only valid in legacy table and will only be used in G
397 * (2.4 GHz) band.
398 */
399
a34529e8 400static const u16 expected_tpt_legacy[IWL_RATE_COUNT] = {
4e82dd3a 401 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 0, 0, 0
8ca151b5
JB
402};
403
4e82dd3a
ES
404/* Expected TpT tables. 4 indexes:
405 * 0 - NGI, 1 - SGI, 2 - AGG+NGI, 3 - AGG+SGI
406 */
a34529e8 407static const u16 expected_tpt_siso_20MHz[4][IWL_RATE_COUNT] = {
4e82dd3a
ES
408 {0, 0, 0, 0, 42, 0, 76, 102, 124, 159, 183, 193, 202, 216, 0},
409 {0, 0, 0, 0, 46, 0, 82, 110, 132, 168, 192, 202, 210, 225, 0},
410 {0, 0, 0, 0, 49, 0, 97, 145, 192, 285, 375, 420, 464, 551, 0},
411 {0, 0, 0, 0, 54, 0, 108, 160, 213, 315, 415, 465, 513, 608, 0},
8ca151b5
JB
412};
413
a34529e8 414static const u16 expected_tpt_siso_40MHz[4][IWL_RATE_COUNT] = {
4e82dd3a
ES
415 {0, 0, 0, 0, 77, 0, 127, 160, 184, 220, 242, 250, 257, 269, 275},
416 {0, 0, 0, 0, 83, 0, 135, 169, 193, 229, 250, 257, 264, 275, 280},
417 {0, 0, 0, 0, 101, 0, 199, 295, 389, 570, 744, 828, 911, 1070, 1173},
418 {0, 0, 0, 0, 112, 0, 220, 326, 429, 629, 819, 912, 1000, 1173, 1284},
8ca151b5
JB
419};
420
a34529e8 421static const u16 expected_tpt_siso_80MHz[4][IWL_RATE_COUNT] = {
d310e405
ES
422 {0, 0, 0, 0, 130, 0, 191, 223, 244, 273, 288, 294, 298, 305, 308},
423 {0, 0, 0, 0, 138, 0, 200, 231, 251, 279, 293, 298, 302, 308, 312},
424 {0, 0, 0, 0, 217, 0, 429, 634, 834, 1220, 1585, 1760, 1931, 2258, 2466},
425 {0, 0, 0, 0, 241, 0, 475, 701, 921, 1343, 1741, 1931, 2117, 2468, 2691},
426};
427
a34529e8 428static const u16 expected_tpt_mimo2_20MHz[4][IWL_RATE_COUNT] = {
4e82dd3a
ES
429 {0, 0, 0, 0, 74, 0, 123, 155, 179, 213, 235, 243, 250, 261, 0},
430 {0, 0, 0, 0, 81, 0, 131, 164, 187, 221, 242, 250, 256, 267, 0},
431 {0, 0, 0, 0, 98, 0, 193, 286, 375, 550, 718, 799, 878, 1032, 0},
432 {0, 0, 0, 0, 109, 0, 214, 316, 414, 607, 790, 879, 965, 1132, 0},
8ca151b5
JB
433};
434
a34529e8 435static const u16 expected_tpt_mimo2_40MHz[4][IWL_RATE_COUNT] = {
4e82dd3a
ES
436 {0, 0, 0, 0, 123, 0, 182, 214, 235, 264, 279, 285, 289, 296, 300},
437 {0, 0, 0, 0, 131, 0, 191, 222, 242, 270, 284, 289, 293, 300, 303},
438 {0, 0, 0, 0, 200, 0, 390, 571, 741, 1067, 1365, 1505, 1640, 1894, 2053},
439 {0, 0, 0, 0, 221, 0, 430, 630, 816, 1169, 1490, 1641, 1784, 2053, 2221},
8ca151b5
JB
440};
441
a34529e8 442static const u16 expected_tpt_mimo2_80MHz[4][IWL_RATE_COUNT] = {
d310e405
ES
443 {0, 0, 0, 0, 182, 0, 240, 264, 278, 299, 308, 311, 313, 317, 319},
444 {0, 0, 0, 0, 190, 0, 247, 269, 282, 302, 310, 313, 315, 319, 320},
445 {0, 0, 0, 0, 428, 0, 833, 1215, 1577, 2254, 2863, 3147, 3418, 3913, 4219},
446 {0, 0, 0, 0, 474, 0, 920, 1338, 1732, 2464, 3116, 3418, 3705, 4225, 4545},
447};
448
8ca151b5
JB
449/* mbps, mcs */
450static const struct iwl_rate_mcs_info iwl_rate_mcs[IWL_RATE_COUNT] = {
451 { "1", "BPSK DSSS"},
452 { "2", "QPSK DSSS"},
453 {"5.5", "BPSK CCK"},
454 { "11", "QPSK CCK"},
455 { "6", "BPSK 1/2"},
456 { "9", "BPSK 1/2"},
457 { "12", "QPSK 1/2"},
458 { "18", "QPSK 3/4"},
459 { "24", "16QAM 1/2"},
460 { "36", "16QAM 3/4"},
461 { "48", "64QAM 2/3"},
462 { "54", "64QAM 3/4"},
463 { "60", "64QAM 5/6"},
464};
465
466#define MCS_INDEX_PER_STREAM (8)
467
9d10849e
EG
468static const char *rs_pretty_ant(u8 ant)
469{
470 static const char * const ant_name[] = {
471 [ANT_NONE] = "None",
472 [ANT_A] = "A",
473 [ANT_B] = "B",
474 [ANT_AB] = "AB",
475 [ANT_C] = "C",
476 [ANT_AC] = "AC",
477 [ANT_BC] = "BC",
478 [ANT_ABC] = "ABC",
479 };
480
481 if (ant > ANT_ABC)
482 return "UNKNOWN";
483
484 return ant_name[ant];
485}
486
487static const char *rs_pretty_lq_type(enum iwl_table_type type)
488{
489 static const char * const lq_types[] = {
490 [LQ_NONE] = "NONE",
491 [LQ_LEGACY_A] = "LEGACY_A",
492 [LQ_LEGACY_G] = "LEGACY_G",
493 [LQ_HT_SISO] = "HT SISO",
494 [LQ_HT_MIMO2] = "HT MIMO",
495 [LQ_VHT_SISO] = "VHT SISO",
496 [LQ_VHT_MIMO2] = "VHT MIMO",
497 };
498
499 if (type < LQ_NONE || type >= LQ_MAX)
500 return "UNKNOWN";
501
502 return lq_types[type];
503}
504
5aa33553
ES
505static inline void rs_dump_rate(struct iwl_mvm *mvm, const struct rs_rate *rate,
506 const char *prefix)
507{
d8bafeaf
ES
508 IWL_DEBUG_RATE(mvm,
509 "%s: (%s: %d) ANT: %s BW: %d SGI: %d LDPC: %d STBC %d\n",
5aa33553
ES
510 prefix, rs_pretty_lq_type(rate->type),
511 rate->index, rs_pretty_ant(rate->ant),
d8bafeaf 512 rate->bw, rate->sgi, rate->ldpc, rate->stbc);
5aa33553
ES
513}
514
8ca151b5
JB
515static void rs_rate_scale_clear_window(struct iwl_rate_scale_data *window)
516{
517 window->data = 0;
518 window->success_counter = 0;
519 window->success_ratio = IWL_INVALID_VALUE;
520 window->counter = 0;
521 window->average_tpt = IWL_INVALID_VALUE;
8ca151b5
JB
522}
523
b804eeb6
ES
524static void rs_rate_scale_clear_tbl_windows(struct iwl_mvm *mvm,
525 struct iwl_scale_tbl_info *tbl)
3ca71f60
EP
526{
527 int i;
528
b804eeb6 529 IWL_DEBUG_RATE(mvm, "Clearing up window stats\n");
3ca71f60
EP
530 for (i = 0; i < IWL_RATE_COUNT; i++)
531 rs_rate_scale_clear_window(&tbl->win[i]);
2fd647f8
EP
532
533 for (i = 0; i < ARRAY_SIZE(tbl->tpc_win); i++)
534 rs_rate_scale_clear_window(&tbl->tpc_win[i]);
3ca71f60
EP
535}
536
8ca151b5
JB
537static inline u8 rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
538{
539 return (ant_type & valid_antenna) == ant_type;
540}
541
8ca151b5
JB
542static int rs_tl_turn_on_agg_for_tid(struct iwl_mvm *mvm,
543 struct iwl_lq_sta *lq_data, u8 tid,
544 struct ieee80211_sta *sta)
545{
546 int ret = -EAGAIN;
8ca151b5 547
44cc429c
EG
548 IWL_DEBUG_HT(mvm, "Starting Tx agg: STA: %pM tid: %d\n",
549 sta->addr, tid);
550 ret = ieee80211_start_tx_ba_session(sta, tid, 5000);
551 if (ret == -EAGAIN) {
552 /*
553 * driver and mac80211 is out of sync
554 * this might be cause by reloading firmware
555 * stop the tx ba session here
556 */
557 IWL_ERR(mvm, "Fail start Tx agg on tid: %d\n",
558 tid);
559 ieee80211_stop_tx_ba_session(sta, tid);
8ca151b5
JB
560 }
561 return ret;
562}
563
564static void rs_tl_turn_on_agg(struct iwl_mvm *mvm, u8 tid,
565 struct iwl_lq_sta *lq_data,
566 struct ieee80211_sta *sta)
567{
568 if (tid < IWL_MAX_TID_COUNT)
569 rs_tl_turn_on_agg_for_tid(mvm, lq_data, tid, sta);
570 else
571 IWL_ERR(mvm, "tid exceeds max TID count: %d/%d\n",
572 tid, IWL_MAX_TID_COUNT);
573}
574
575static inline int get_num_of_ant_from_rate(u32 rate_n_flags)
576{
577 return !!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
578 !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
579 !!(rate_n_flags & RATE_MCS_ANT_C_MSK);
580}
581
582/*
583 * Static function to get the expected throughput from an iwl_scale_tbl_info
584 * that wraps a NULL pointer check
585 */
586static s32 get_expected_tpt(struct iwl_scale_tbl_info *tbl, int rs_index)
587{
588 if (tbl->expected_tpt)
589 return tbl->expected_tpt[rs_index];
590 return 0;
591}
592
593/**
594 * rs_collect_tx_data - Update the success/failure sliding window
595 *
596 * We keep a sliding window of the last 62 packets transmitted
597 * at this rate. window->data contains the bitmask of successful
598 * packets.
599 */
0bd3c5a7
EP
600static int _rs_collect_tx_data(struct iwl_scale_tbl_info *tbl,
601 int scale_index, int attempts, int successes,
602 struct iwl_rate_scale_data *window)
8ca151b5 603{
8ca151b5
JB
604 static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
605 s32 fail_count, tpt;
606
8ca151b5
JB
607 /* Get expected throughput */
608 tpt = get_expected_tpt(tbl, scale_index);
609
610 /*
611 * Keep track of only the latest 62 tx frame attempts in this rate's
612 * history window; anything older isn't really relevant any more.
613 * If we have filled up the sliding window, drop the oldest attempt;
614 * if the oldest attempt (highest bit in bitmap) shows "success",
615 * subtract "1" from the success counter (this is the main reason
616 * we keep these bitmaps!).
617 */
618 while (attempts > 0) {
619 if (window->counter >= IWL_RATE_MAX_WINDOW) {
620 /* remove earliest */
621 window->counter = IWL_RATE_MAX_WINDOW - 1;
622
623 if (window->data & mask) {
624 window->data &= ~mask;
625 window->success_counter--;
626 }
627 }
628
629 /* Increment frames-attempted counter */
630 window->counter++;
631
632 /* Shift bitmap by one frame to throw away oldest history */
633 window->data <<= 1;
634
635 /* Mark the most recent #successes attempts as successful */
636 if (successes > 0) {
637 window->success_counter++;
638 window->data |= 0x1;
639 successes--;
640 }
641
642 attempts--;
643 }
644
645 /* Calculate current success ratio, avoid divide-by-0! */
646 if (window->counter > 0)
647 window->success_ratio = 128 * (100 * window->success_counter)
648 / window->counter;
649 else
650 window->success_ratio = IWL_INVALID_VALUE;
651
652 fail_count = window->counter - window->success_counter;
653
654 /* Calculate average throughput, if we have enough history. */
655 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
656 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
657 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
658 else
659 window->average_tpt = IWL_INVALID_VALUE;
660
8ca151b5
JB
661 return 0;
662}
663
757cf23b
ES
664static int rs_collect_tx_data(struct iwl_lq_sta *lq_sta,
665 struct iwl_scale_tbl_info *tbl,
2fd647f8
EP
666 int scale_index, int attempts, int successes,
667 u8 reduced_txp)
0bd3c5a7
EP
668{
669 struct iwl_rate_scale_data *window = NULL;
2fd647f8 670 int ret;
0bd3c5a7
EP
671
672 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
673 return -EINVAL;
674
757cf23b 675 if (tbl->column != RS_COLUMN_INVALID) {
f58220f6
ES
676 struct lq_sta_pers *pers = &lq_sta->pers;
677
678 pers->tx_stats[tbl->column][scale_index].total += attempts;
679 pers->tx_stats[tbl->column][scale_index].success += successes;
757cf23b
ES
680 }
681
0bd3c5a7
EP
682 /* Select window for current tx bit rate */
683 window = &(tbl->win[scale_index]);
684
2fd647f8
EP
685 ret = _rs_collect_tx_data(tbl, scale_index, attempts, successes,
686 window);
687 if (ret)
688 return ret;
689
690 if (WARN_ON_ONCE(reduced_txp > TPC_MAX_REDUCTION))
691 return -EINVAL;
692
693 window = &tbl->tpc_win[reduced_txp];
0bd3c5a7
EP
694 return _rs_collect_tx_data(tbl, scale_index, attempts, successes,
695 window);
696}
697
5aa33553
ES
698/* Convert rs_rate object into ucode rate bitmask */
699static u32 ucode_rate_from_rs_rate(struct iwl_mvm *mvm,
8fc7c58c 700 struct rs_rate *rate)
8ca151b5 701{
5aa33553
ES
702 u32 ucode_rate = 0;
703 int index = rate->index;
8ca151b5 704
5aa33553 705 ucode_rate |= ((rate->ant << RATE_MCS_ANT_POS) &
d310e405
ES
706 RATE_MCS_ANT_ABC_MSK);
707
5aa33553
ES
708 if (is_legacy(rate)) {
709 ucode_rate |= iwl_rates[index].plcp;
8ca151b5 710 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
5aa33553
ES
711 ucode_rate |= RATE_MCS_CCK_MSK;
712 return ucode_rate;
d310e405
ES
713 }
714
5aa33553 715 if (is_ht(rate)) {
d310e405 716 if (index < IWL_FIRST_HT_RATE || index > IWL_LAST_HT_RATE) {
8ca151b5 717 IWL_ERR(mvm, "Invalid HT rate index %d\n", index);
4e82dd3a 718 index = IWL_LAST_HT_RATE;
8ca151b5 719 }
5aa33553 720 ucode_rate |= RATE_MCS_HT_MSK;
8ca151b5 721
5aa33553
ES
722 if (is_ht_siso(rate))
723 ucode_rate |= iwl_rates[index].plcp_ht_siso;
724 else if (is_ht_mimo2(rate))
725 ucode_rate |= iwl_rates[index].plcp_ht_mimo2;
8ca151b5 726 else
d972ab31 727 WARN_ON_ONCE(1);
5aa33553 728 } else if (is_vht(rate)) {
d310e405
ES
729 if (index < IWL_FIRST_VHT_RATE || index > IWL_LAST_VHT_RATE) {
730 IWL_ERR(mvm, "Invalid VHT rate index %d\n", index);
731 index = IWL_LAST_VHT_RATE;
732 }
5aa33553
ES
733 ucode_rate |= RATE_MCS_VHT_MSK;
734 if (is_vht_siso(rate))
735 ucode_rate |= iwl_rates[index].plcp_vht_siso;
736 else if (is_vht_mimo2(rate))
737 ucode_rate |= iwl_rates[index].plcp_vht_mimo2;
d310e405
ES
738 else
739 WARN_ON_ONCE(1);
740
8ca151b5 741 } else {
5aa33553 742 IWL_ERR(mvm, "Invalid rate->type %d\n", rate->type);
8ca151b5
JB
743 }
744
d8bafeaf
ES
745 if (is_siso(rate) && rate->stbc) {
746 /* To enable STBC we need to set both a flag and ANT_AB */
747 ucode_rate |= RATE_MCS_ANT_AB_MSK;
748 ucode_rate |= RATE_MCS_VHT_STBC_MSK;
749 }
750
5aa33553
ES
751 ucode_rate |= rate->bw;
752 if (rate->sgi)
753 ucode_rate |= RATE_MCS_SGI_MSK;
a3576ff2
ES
754 if (rate->ldpc)
755 ucode_rate |= RATE_MCS_LDPC_MSK;
d310e405 756
5aa33553 757 return ucode_rate;
8ca151b5
JB
758}
759
5aa33553
ES
760/* Convert a ucode rate into an rs_rate object */
761static int rs_rate_from_ucode_rate(const u32 ucode_rate,
762 enum ieee80211_band band,
763 struct rs_rate *rate)
8ca151b5 764{
5aa33553
ES
765 u32 ant_msk = ucode_rate & RATE_MCS_ANT_ABC_MSK;
766 u8 num_of_ant = get_num_of_ant_from_rate(ucode_rate);
d310e405 767 u8 nss;
8ca151b5 768
9e898f1b 769 memset(rate, 0, sizeof(*rate));
5aa33553 770 rate->index = iwl_hwrate_to_plcp_idx(ucode_rate);
8ca151b5 771
560843f4 772 if (rate->index == IWL_RATE_INVALID)
8ca151b5 773 return -EINVAL;
5aa33553
ES
774
775 rate->ant = (ant_msk >> RATE_MCS_ANT_POS);
8ca151b5 776
d310e405 777 /* Legacy */
5aa33553
ES
778 if (!(ucode_rate & RATE_MCS_HT_MSK) &&
779 !(ucode_rate & RATE_MCS_VHT_MSK)) {
8ca151b5
JB
780 if (num_of_ant == 1) {
781 if (band == IEEE80211_BAND_5GHZ)
5aa33553 782 rate->type = LQ_LEGACY_A;
8ca151b5 783 else
5aa33553 784 rate->type = LQ_LEGACY_G;
8ca151b5 785 }
d310e405
ES
786
787 return 0;
788 }
789
790 /* HT or VHT */
5aa33553
ES
791 if (ucode_rate & RATE_MCS_SGI_MSK)
792 rate->sgi = true;
a3576ff2
ES
793 if (ucode_rate & RATE_MCS_LDPC_MSK)
794 rate->ldpc = true;
d8bafeaf
ES
795 if (ucode_rate & RATE_MCS_VHT_STBC_MSK)
796 rate->stbc = true;
d310e405 797
5aa33553 798 rate->bw = ucode_rate & RATE_MCS_CHAN_WIDTH_MSK;
d310e405 799
5aa33553
ES
800 if (ucode_rate & RATE_MCS_HT_MSK) {
801 nss = ((ucode_rate & RATE_HT_MCS_NSS_MSK) >>
d310e405
ES
802 RATE_HT_MCS_NSS_POS) + 1;
803
804 if (nss == 1) {
5aa33553 805 rate->type = LQ_HT_SISO;
d8bafeaf 806 WARN_ON_ONCE(!rate->stbc && num_of_ant != 1);
d310e405 807 } else if (nss == 2) {
5aa33553 808 rate->type = LQ_HT_MIMO2;
d310e405 809 WARN_ON_ONCE(num_of_ant != 2);
8ca151b5 810 } else {
d310e405
ES
811 WARN_ON_ONCE(1);
812 }
5aa33553
ES
813 } else if (ucode_rate & RATE_MCS_VHT_MSK) {
814 nss = ((ucode_rate & RATE_VHT_MCS_NSS_MSK) >>
d310e405
ES
815 RATE_VHT_MCS_NSS_POS) + 1;
816
817 if (nss == 1) {
5aa33553 818 rate->type = LQ_VHT_SISO;
1507fb75 819 WARN_ON_ONCE(!rate->stbc && num_of_ant != 1);
d310e405 820 } else if (nss == 2) {
5aa33553 821 rate->type = LQ_VHT_MIMO2;
d310e405
ES
822 WARN_ON_ONCE(num_of_ant != 2);
823 } else {
824 WARN_ON_ONCE(1);
8ca151b5
JB
825 }
826 }
d310e405 827
5aa33553
ES
828 WARN_ON_ONCE(rate->bw == RATE_MCS_CHAN_WIDTH_160);
829 WARN_ON_ONCE(rate->bw == RATE_MCS_CHAN_WIDTH_80 &&
830 !is_vht(rate));
d310e405 831
8ca151b5
JB
832 return 0;
833}
834
835/* switch to another antenna/antennas and return 1 */
836/* if no other valid antenna found, return 0 */
8fc7c58c 837static int rs_toggle_antenna(u32 valid_ant, struct rs_rate *rate)
8ca151b5
JB
838{
839 u8 new_ant_type;
840
5aa33553 841 if (!rate->ant || rate->ant > ANT_ABC)
8ca151b5
JB
842 return 0;
843
5aa33553 844 if (!rs_is_valid_ant(valid_ant, rate->ant))
8ca151b5
JB
845 return 0;
846
5aa33553 847 new_ant_type = ant_toggle_lookup[rate->ant];
8ca151b5 848
5aa33553 849 while ((new_ant_type != rate->ant) &&
8ca151b5
JB
850 !rs_is_valid_ant(valid_ant, new_ant_type))
851 new_ant_type = ant_toggle_lookup[new_ant_type];
852
5aa33553 853 if (new_ant_type == rate->ant)
8ca151b5
JB
854 return 0;
855
5aa33553
ES
856 rate->ant = new_ant_type;
857
8ca151b5
JB
858 return 1;
859}
860
8ca151b5 861static u16 rs_get_supported_rates(struct iwl_lq_sta *lq_sta,
5aa33553 862 struct rs_rate *rate)
8ca151b5 863{
5aa33553 864 if (is_legacy(rate))
8ca151b5 865 return lq_sta->active_legacy_rate;
5aa33553 866 else if (is_siso(rate))
d310e405 867 return lq_sta->active_siso_rate;
5aa33553 868 else if (is_mimo2(rate))
d310e405
ES
869 return lq_sta->active_mimo2_rate;
870
871 WARN_ON_ONCE(1);
872 return 0;
8ca151b5
JB
873}
874
875static u16 rs_get_adjacent_rate(struct iwl_mvm *mvm, u8 index, u16 rate_mask,
876 int rate_type)
877{
878 u8 high = IWL_RATE_INVALID;
879 u8 low = IWL_RATE_INVALID;
880
881 /* 802.11A or ht walks to the next literal adjacent rate in
882 * the rate table */
5aa33553 883 if (is_type_a_band(rate_type) || !is_type_legacy(rate_type)) {
8ca151b5
JB
884 int i;
885 u32 mask;
886
887 /* Find the previous rate that is in the rate mask */
888 i = index - 1;
889 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
890 if (rate_mask & mask) {
891 low = i;
892 break;
893 }
894 }
895
896 /* Find the next rate that is in the rate mask */
897 i = index + 1;
898 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
899 if (rate_mask & mask) {
900 high = i;
901 break;
902 }
903 }
904
905 return (high << 8) | low;
906 }
907
908 low = index;
909 while (low != IWL_RATE_INVALID) {
910 low = iwl_rates[low].prev_rs;
911 if (low == IWL_RATE_INVALID)
912 break;
913 if (rate_mask & (1 << low))
914 break;
915 IWL_DEBUG_RATE(mvm, "Skipping masked lower rate: %d\n", low);
916 }
917
918 high = index;
919 while (high != IWL_RATE_INVALID) {
920 high = iwl_rates[high].next_rs;
921 if (high == IWL_RATE_INVALID)
922 break;
923 if (rate_mask & (1 << high))
924 break;
925 IWL_DEBUG_RATE(mvm, "Skipping masked higher rate: %d\n", high);
926 }
927
928 return (high << 8) | low;
929}
930
e07be6d3
ES
931static inline bool rs_rate_supported(struct iwl_lq_sta *lq_sta,
932 struct rs_rate *rate)
8ca151b5 933{
e07be6d3
ES
934 return BIT(rate->index) & rs_get_supported_rates(lq_sta, rate);
935}
936
937/* Get the next supported lower rate in the current column.
938 * Return true if bottom rate in the current column was reached
939 */
940static bool rs_get_lower_rate_in_column(struct iwl_lq_sta *lq_sta,
941 struct rs_rate *rate)
942{
943 u8 low;
8ca151b5 944 u16 high_low;
e07be6d3 945 u16 rate_mask;
ae969afe 946 struct iwl_mvm *mvm = lq_sta->pers.drv;
e07be6d3
ES
947
948 rate_mask = rs_get_supported_rates(lq_sta, rate);
949 high_low = rs_get_adjacent_rate(mvm, rate->index, rate_mask,
950 rate->type);
951 low = high_low & 0xff;
952
953 /* Bottom rate of column reached */
954 if (low == IWL_RATE_INVALID)
955 return true;
956
957 rate->index = low;
958 return false;
959}
960
961/* Get the next rate to use following a column downgrade */
962static void rs_get_lower_rate_down_column(struct iwl_lq_sta *lq_sta,
963 struct rs_rate *rate)
964{
ae969afe 965 struct iwl_mvm *mvm = lq_sta->pers.drv;
8ca151b5 966
e07be6d3
ES
967 if (is_legacy(rate)) {
968 /* No column to downgrade from Legacy */
969 return;
970 } else if (is_siso(rate)) {
971 /* Downgrade to Legacy if we were in SISO */
8ca151b5 972 if (lq_sta->band == IEEE80211_BAND_5GHZ)
5aa33553 973 rate->type = LQ_LEGACY_A;
8ca151b5 974 else
5aa33553 975 rate->type = LQ_LEGACY_G;
8ca151b5 976
a56db7d1 977 rate->bw = RATE_MCS_CHAN_WIDTH_20;
8ca151b5 978
b800040d 979 WARN_ON_ONCE(rate->index < IWL_RATE_MCS_0_INDEX ||
e07be6d3 980 rate->index > IWL_RATE_MCS_9_INDEX);
8ca151b5 981
e07be6d3 982 rate->index = rs_ht_to_legacy[rate->index];
a3576ff2 983 rate->ldpc = false;
e07be6d3
ES
984 } else {
985 /* Downgrade to SISO with same MCS if in MIMO */
986 rate->type = is_vht_mimo2(rate) ?
987 LQ_VHT_SISO : LQ_HT_SISO;
8ca151b5
JB
988 }
989
e07be6d3 990 if (num_of_ant(rate->ant) > 1)
4ed735e7 991 rate->ant = first_antenna(mvm->fw->valid_tx_ant);
8ca151b5 992
e07be6d3
ES
993 /* Relevant in both switching to SISO or Legacy */
994 rate->sgi = false;
995
996 if (!rs_rate_supported(lq_sta, rate))
997 rs_get_lower_rate_in_column(lq_sta, rate);
8ca151b5
JB
998}
999
5aa33553
ES
1000/* Simple function to compare two rate scale table types */
1001static inline bool rs_rate_match(struct rs_rate *a,
1002 struct rs_rate *b)
8ca151b5 1003{
d8bafeaf
ES
1004 bool ant_match;
1005
1006 if (a->stbc)
1007 ant_match = (b->ant == ANT_A || b->ant == ANT_B);
1008 else
1009 ant_match = (a->ant == b->ant);
1010
1011 return (a->type == b->type) && (a->bw == b->bw) && (a->sgi == b->sgi)
1012 && ant_match;
8ca151b5
JB
1013}
1014
d310e405
ES
1015static u32 rs_ch_width_from_mac_flags(enum mac80211_rate_control_flags flags)
1016{
1017 if (flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
1018 return RATE_MCS_CHAN_WIDTH_40;
1019 else if (flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
1020 return RATE_MCS_CHAN_WIDTH_80;
1021 else if (flags & IEEE80211_TX_RC_160_MHZ_WIDTH)
1022 return RATE_MCS_CHAN_WIDTH_160;
1023
1024 return RATE_MCS_CHAN_WIDTH_20;
1025}
1026
1ebc8f2e
ES
1027static u8 rs_get_tid(struct ieee80211_hdr *hdr)
1028{
1029 u8 tid = IWL_MAX_TID_COUNT;
1030
1031 if (ieee80211_is_data_qos(hdr->frame_control)) {
1032 u8 *qc = ieee80211_get_qos_ctl(hdr);
1033 tid = qc[0] & 0xf;
1034 }
1035
1036 if (unlikely(tid > IWL_MAX_TID_COUNT))
1037 tid = IWL_MAX_TID_COUNT;
1038
1039 return tid;
1040}
1041
1042void iwl_mvm_rs_tx_status(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
1043 int tid, struct ieee80211_tx_info *info)
8ca151b5
JB
1044{
1045 int legacy_success;
1046 int retries;
5aa33553 1047 int mac_index, i;
8ca151b5 1048 struct iwl_lq_cmd *table;
8ca151b5 1049 enum mac80211_rate_control_flags mac_flags;
5aa33553
ES
1050 u32 ucode_rate;
1051 struct rs_rate rate;
8ca151b5 1052 struct iwl_scale_tbl_info *curr_tbl, *other_tbl, *tmp_tbl;
2fd647f8 1053 u8 reduced_txp = (uintptr_t)info->status.status_driver_data[0];
1ebc8f2e
ES
1054 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1055 struct iwl_lq_sta *lq_sta = &mvmsta->lq_sta;
8ca151b5 1056
8ca151b5
JB
1057 /* Treat uninitialized rate scaling data same as non-existing. */
1058 if (!lq_sta) {
1059 IWL_DEBUG_RATE(mvm, "Station rate scaling not created yet.\n");
1060 return;
ae969afe 1061 } else if (!lq_sta->pers.drv) {
8ca151b5
JB
1062 IWL_DEBUG_RATE(mvm, "Rate scaling not initialized yet.\n");
1063 return;
1064 }
1065
8e96440e 1066#ifdef CONFIG_MAC80211_DEBUGFS
87d5e415 1067 /* Disable last tx check if we are debugging with fixed rate */
ae969afe 1068 if (lq_sta->pers.dbg_fixed_rate) {
87d5e415
ES
1069 IWL_DEBUG_RATE(mvm, "Fixed rate. avoid rate scaling\n");
1070 return;
1071 }
1072#endif
8ca151b5
JB
1073 /* This packet was aggregated but doesn't carry status info */
1074 if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
1075 !(info->flags & IEEE80211_TX_STAT_AMPDU))
1076 return;
1077
1078 /*
1079 * Ignore this Tx frame response if its initial rate doesn't match
1080 * that of latest Link Quality command. There may be stragglers
1081 * from a previous Link Quality command, but we're no longer interested
1082 * in those; they're either from the "active" mode while we're trying
1083 * to check "search" mode, or a prior "search" mode after we've moved
1084 * to a new "search" mode (which might become the new "active" mode).
1085 */
1086 table = &lq_sta->lq;
5aa33553
ES
1087 ucode_rate = le32_to_cpu(table->rs_table[0]);
1088 rs_rate_from_ucode_rate(ucode_rate, info->band, &rate);
8ca151b5 1089 if (info->band == IEEE80211_BAND_5GHZ)
5aa33553 1090 rate.index -= IWL_FIRST_OFDM_RATE;
8ca151b5
JB
1091 mac_flags = info->status.rates[0].flags;
1092 mac_index = info->status.rates[0].idx;
1093 /* For HT packets, map MCS to PLCP */
1094 if (mac_flags & IEEE80211_TX_RC_MCS) {
1095 /* Remove # of streams */
1096 mac_index &= RATE_HT_MCS_RATE_CODE_MSK;
1097 if (mac_index >= (IWL_RATE_9M_INDEX - IWL_FIRST_OFDM_RATE))
1098 mac_index++;
1099 /*
1100 * mac80211 HT index is always zero-indexed; we need to move
1101 * HT OFDM rates after CCK rates in 2.4 GHz band
1102 */
1103 if (info->band == IEEE80211_BAND_2GHZ)
1104 mac_index += IWL_FIRST_OFDM_RATE;
d310e405
ES
1105 } else if (mac_flags & IEEE80211_TX_RC_VHT_MCS) {
1106 mac_index &= RATE_VHT_MCS_RATE_CODE_MSK;
1107 if (mac_index >= (IWL_RATE_9M_INDEX - IWL_FIRST_OFDM_RATE))
1108 mac_index++;
8ca151b5 1109 }
d310e405 1110
87d5e415
ES
1111 if (time_after(jiffies,
1112 (unsigned long)(lq_sta->last_tx + RS_IDLE_TIMEOUT))) {
efad21d2
JB
1113 int t;
1114
87d5e415 1115 IWL_DEBUG_RATE(mvm, "Tx idle for too long. reinit rs\n");
efad21d2
JB
1116 for (t = 0; t < IWL_MAX_TID_COUNT; t++)
1117 ieee80211_stop_tx_ba_session(sta, t);
87d5e415 1118
1ebc8f2e 1119 iwl_mvm_rs_rate_init(mvm, sta, info->band, false);
87d5e415
ES
1120 return;
1121 }
1122 lq_sta->last_tx = jiffies;
1123
8ca151b5
JB
1124 /* Here we actually compare this rate to the latest LQ command */
1125 if ((mac_index < 0) ||
5aa33553
ES
1126 (rate.sgi != !!(mac_flags & IEEE80211_TX_RC_SHORT_GI)) ||
1127 (rate.bw != rs_ch_width_from_mac_flags(mac_flags)) ||
1128 (rate.ant != info->status.antenna) ||
1129 (!!(ucode_rate & RATE_MCS_HT_MSK) !=
d310e405 1130 !!(mac_flags & IEEE80211_TX_RC_MCS)) ||
5aa33553 1131 (!!(ucode_rate & RATE_MCS_VHT_MSK) !=
d310e405 1132 !!(mac_flags & IEEE80211_TX_RC_VHT_MCS)) ||
5aa33553 1133 (!!(ucode_rate & RATE_HT_MCS_GF_MSK) !=
d310e405 1134 !!(mac_flags & IEEE80211_TX_RC_GREEN_FIELD)) ||
5aa33553 1135 (rate.index != mac_index)) {
8ca151b5
JB
1136 IWL_DEBUG_RATE(mvm,
1137 "initial rate %d does not match %d (0x%x)\n",
5aa33553 1138 mac_index, rate.index, ucode_rate);
8ca151b5
JB
1139 /*
1140 * Since rates mis-match, the last LQ command may have failed.
1141 * After IWL_MISSED_RATE_MAX mis-matches, resync the uCode with
1142 * ... driver.
1143 */
1144 lq_sta->missed_rate_counter++;
1145 if (lq_sta->missed_rate_counter > IWL_MISSED_RATE_MAX) {
1146 lq_sta->missed_rate_counter = 0;
b3b06a32
ES
1147 IWL_DEBUG_RATE(mvm,
1148 "Too many rates mismatch. Send sync LQ. rs_state %d\n",
1149 lq_sta->rs_state);
9e680946 1150 iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, false);
8ca151b5
JB
1151 }
1152 /* Regardless, ignore this status info for outdated rate */
1153 return;
1154 } else
1155 /* Rate did match, so reset the missed_rate_counter */
1156 lq_sta->missed_rate_counter = 0;
1157
1158 /* Figure out if rate scale algorithm is in active or search table */
5aa33553
ES
1159 if (rs_rate_match(&rate,
1160 &(lq_sta->lq_info[lq_sta->active_tbl].rate))) {
8ca151b5
JB
1161 curr_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1162 other_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
5aa33553
ES
1163 } else if (rs_rate_match(&rate,
1164 &lq_sta->lq_info[1 - lq_sta->active_tbl].rate)) {
8ca151b5
JB
1165 curr_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
1166 other_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1167 } else {
1168 IWL_DEBUG_RATE(mvm,
1169 "Neither active nor search matches tx rate\n");
1170 tmp_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
5aa33553 1171 rs_dump_rate(mvm, &tmp_tbl->rate, "ACTIVE");
8ca151b5 1172 tmp_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
5aa33553
ES
1173 rs_dump_rate(mvm, &tmp_tbl->rate, "SEARCH");
1174 rs_dump_rate(mvm, &rate, "ACTUAL");
1175
8ca151b5
JB
1176 /*
1177 * no matching table found, let's by-pass the data collection
1178 * and continue to perform rate scale to find the rate table
1179 */
1180 rs_stay_in_table(lq_sta, true);
1181 goto done;
1182 }
1183
1184 /*
1185 * Updating the frame history depends on whether packets were
1186 * aggregated.
1187 *
1188 * For aggregation, all packets were transmitted at the same rate, the
1189 * first index into rate scale table.
1190 */
1191 if (info->flags & IEEE80211_TX_STAT_AMPDU) {
5aa33553
ES
1192 ucode_rate = le32_to_cpu(table->rs_table[0]);
1193 rs_rate_from_ucode_rate(ucode_rate, info->band, &rate);
757cf23b 1194 rs_collect_tx_data(lq_sta, curr_tbl, rate.index,
8ca151b5 1195 info->status.ampdu_len,
2fd647f8
EP
1196 info->status.ampdu_ack_len,
1197 reduced_txp);
8ca151b5
JB
1198
1199 /* Update success/fail counts if not searching for new mode */
b3b06a32 1200 if (lq_sta->rs_state == RS_STATE_STAY_IN_COLUMN) {
8ca151b5
JB
1201 lq_sta->total_success += info->status.ampdu_ack_len;
1202 lq_sta->total_failed += (info->status.ampdu_len -
1203 info->status.ampdu_ack_len);
1204 }
1205 } else {
1206 /*
1207 * For legacy, update frame history with for each Tx retry.
1208 */
1209 retries = info->status.rates[0].count - 1;
1210 /* HW doesn't send more than 15 retries */
1211 retries = min(retries, 15);
1212
1213 /* The last transmission may have been successful */
1214 legacy_success = !!(info->flags & IEEE80211_TX_STAT_ACK);
1215 /* Collect data for each rate used during failed TX attempts */
1216 for (i = 0; i <= retries; ++i) {
5aa33553
ES
1217 ucode_rate = le32_to_cpu(table->rs_table[i]);
1218 rs_rate_from_ucode_rate(ucode_rate, info->band, &rate);
8ca151b5
JB
1219 /*
1220 * Only collect stats if retried rate is in the same RS
1221 * table as active/search.
1222 */
5aa33553 1223 if (rs_rate_match(&rate, &curr_tbl->rate))
8ca151b5 1224 tmp_tbl = curr_tbl;
5aa33553 1225 else if (rs_rate_match(&rate, &other_tbl->rate))
8ca151b5 1226 tmp_tbl = other_tbl;
4107dbd2 1227 else
8ca151b5 1228 continue;
4107dbd2 1229
757cf23b 1230 rs_collect_tx_data(lq_sta, tmp_tbl, rate.index, 1,
2fd647f8
EP
1231 i < retries ? 0 : legacy_success,
1232 reduced_txp);
8ca151b5
JB
1233 }
1234
1235 /* Update success/fail counts if not searching for new mode */
b3b06a32 1236 if (lq_sta->rs_state == RS_STATE_STAY_IN_COLUMN) {
8ca151b5
JB
1237 lq_sta->total_success += legacy_success;
1238 lq_sta->total_failed += retries + (1 - legacy_success);
1239 }
1240 }
1241 /* The last TX rate is cached in lq_sta; it's set in if/else above */
5aa33553 1242 lq_sta->last_rate_n_flags = ucode_rate;
2fd647f8 1243 IWL_DEBUG_RATE(mvm, "reduced txpower: %d\n", reduced_txp);
8ca151b5
JB
1244done:
1245 /* See if there's a better rate or modulation mode to try. */
8dfc2751 1246 if (sta->supp_rates[info->band])
1ebc8f2e
ES
1247 rs_rate_scale_perform(mvm, sta, lq_sta, tid);
1248}
1249
1250/*
1251 * mac80211 sends us Tx status
1252 */
1253static void rs_mac80211_tx_status(void *mvm_r,
1254 struct ieee80211_supported_band *sband,
1255 struct ieee80211_sta *sta, void *priv_sta,
1256 struct sk_buff *skb)
1257{
1258 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1259 struct iwl_op_mode *op_mode = (struct iwl_op_mode *)mvm_r;
1260 struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1261 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1262
1263 if (!ieee80211_is_data(hdr->frame_control) ||
1264 info->flags & IEEE80211_TX_CTL_NO_ACK)
1265 return;
1266
1267 iwl_mvm_rs_tx_status(mvm, sta, rs_get_tid(hdr), info);
8ca151b5
JB
1268}
1269
1270/*
1271 * Begin a period of staying with a selected modulation mode.
1272 * Set "stay_in_tbl" flag to prevent any mode switches.
1273 * Set frame tx success limits according to legacy vs. high-throughput,
1274 * and reset overall (spanning all rates) tx success history statistics.
1275 * These control how long we stay using same modulation mode before
1276 * searching for a new mode.
1277 */
1278static void rs_set_stay_in_table(struct iwl_mvm *mvm, u8 is_legacy,
1279 struct iwl_lq_sta *lq_sta)
1280{
b3b06a32
ES
1281 IWL_DEBUG_RATE(mvm, "Moving to RS_STATE_STAY_IN_COLUMN\n");
1282 lq_sta->rs_state = RS_STATE_STAY_IN_COLUMN;
8ca151b5
JB
1283 if (is_legacy) {
1284 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
1285 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
1286 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
1287 } else {
1288 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
1289 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
1290 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
1291 }
1292 lq_sta->table_count = 0;
1293 lq_sta->total_failed = 0;
1294 lq_sta->total_success = 0;
1295 lq_sta->flush_timer = jiffies;
b3b06a32 1296 lq_sta->visited_columns = 0;
8ca151b5
JB
1297}
1298
198266a3
ES
1299static int rs_get_max_allowed_rate(struct iwl_lq_sta *lq_sta,
1300 const struct rs_tx_column *column)
1301{
1302 switch (column->mode) {
1303 case RS_LEGACY:
1304 return lq_sta->max_legacy_rate_idx;
1305 case RS_SISO:
1306 return lq_sta->max_siso_rate_idx;
1307 case RS_MIMO2:
1308 return lq_sta->max_mimo2_rate_idx;
1309 default:
1310 WARN_ON_ONCE(1);
1311 }
1312
1313 return lq_sta->max_legacy_rate_idx;
1314}
1315
a34529e8 1316static const u16 *rs_get_expected_tpt_table(struct iwl_lq_sta *lq_sta,
198266a3
ES
1317 const struct rs_tx_column *column,
1318 u32 bw)
8ca151b5
JB
1319{
1320 /* Used to choose among HT tables */
a34529e8 1321 const u16 (*ht_tbl_pointer)[IWL_RATE_COUNT];
8ca151b5 1322
b3b06a32
ES
1323 if (WARN_ON_ONCE(column->mode != RS_LEGACY &&
1324 column->mode != RS_SISO &&
1325 column->mode != RS_MIMO2))
1326 return expected_tpt_legacy;
8ca151b5
JB
1327
1328 /* Legacy rates have only one table */
b3b06a32
ES
1329 if (column->mode == RS_LEGACY)
1330 return expected_tpt_legacy;
8ca151b5 1331
d310e405 1332 ht_tbl_pointer = expected_tpt_mimo2_20MHz;
8ca151b5 1333 /* Choose among many HT tables depending on number of streams
d310e405 1334 * (SISO/MIMO2), channel width (20/40/80), SGI, and aggregation
8ca151b5 1335 * status */
b3b06a32
ES
1336 if (column->mode == RS_SISO) {
1337 switch (bw) {
d310e405
ES
1338 case RATE_MCS_CHAN_WIDTH_20:
1339 ht_tbl_pointer = expected_tpt_siso_20MHz;
1340 break;
1341 case RATE_MCS_CHAN_WIDTH_40:
1342 ht_tbl_pointer = expected_tpt_siso_40MHz;
1343 break;
1344 case RATE_MCS_CHAN_WIDTH_80:
1345 ht_tbl_pointer = expected_tpt_siso_80MHz;
1346 break;
1347 default:
1348 WARN_ON_ONCE(1);
1349 }
b3b06a32
ES
1350 } else if (column->mode == RS_MIMO2) {
1351 switch (bw) {
d310e405
ES
1352 case RATE_MCS_CHAN_WIDTH_20:
1353 ht_tbl_pointer = expected_tpt_mimo2_20MHz;
1354 break;
1355 case RATE_MCS_CHAN_WIDTH_40:
1356 ht_tbl_pointer = expected_tpt_mimo2_40MHz;
1357 break;
1358 case RATE_MCS_CHAN_WIDTH_80:
1359 ht_tbl_pointer = expected_tpt_mimo2_80MHz;
1360 break;
1361 default:
1362 WARN_ON_ONCE(1);
1363 }
1364 } else {
1365 WARN_ON_ONCE(1);
d972ab31 1366 }
8ca151b5 1367
b3b06a32
ES
1368 if (!column->sgi && !lq_sta->is_agg) /* Normal */
1369 return ht_tbl_pointer[0];
1370 else if (column->sgi && !lq_sta->is_agg) /* SGI */
1371 return ht_tbl_pointer[1];
1372 else if (!column->sgi && lq_sta->is_agg) /* AGG */
1373 return ht_tbl_pointer[2];
8ca151b5 1374 else /* AGG+SGI */
b3b06a32
ES
1375 return ht_tbl_pointer[3];
1376}
1377
1378static void rs_set_expected_tpt_table(struct iwl_lq_sta *lq_sta,
1379 struct iwl_scale_tbl_info *tbl)
1380{
1381 struct rs_rate *rate = &tbl->rate;
1382 const struct rs_tx_column *column = &rs_tx_columns[tbl->column];
1383
1384 tbl->expected_tpt = rs_get_expected_tpt_table(lq_sta, column, rate->bw);
8ca151b5
JB
1385}
1386
8ca151b5
JB
1387static s32 rs_get_best_rate(struct iwl_mvm *mvm,
1388 struct iwl_lq_sta *lq_sta,
1389 struct iwl_scale_tbl_info *tbl, /* "search" */
80763515 1390 unsigned long rate_mask, s8 index)
8ca151b5 1391{
8ca151b5
JB
1392 struct iwl_scale_tbl_info *active_tbl =
1393 &(lq_sta->lq_info[lq_sta->active_tbl]);
80763515
ES
1394 s32 success_ratio = active_tbl->win[index].success_ratio;
1395 u16 expected_current_tpt = active_tbl->expected_tpt[index];
a34529e8 1396 const u16 *tpt_tbl = tbl->expected_tpt;
8ca151b5 1397 u16 high_low;
80763515
ES
1398 u32 target_tpt;
1399 int rate_idx;
8ca151b5 1400
80763515
ES
1401 if (success_ratio > RS_SR_NO_DECREASE) {
1402 target_tpt = 100 * expected_current_tpt;
1403 IWL_DEBUG_RATE(mvm,
1404 "SR %d high. Find rate exceeding EXPECTED_CURRENT %d\n",
1405 success_ratio, target_tpt);
1406 } else {
1407 target_tpt = lq_sta->last_tpt;
1408 IWL_DEBUG_RATE(mvm,
1409 "SR %d not thag good. Find rate exceeding ACTUAL_TPT %d\n",
1410 success_ratio, target_tpt);
1411 }
8ca151b5 1412
80763515 1413 rate_idx = find_first_bit(&rate_mask, BITS_PER_LONG);
8ca151b5 1414
80763515
ES
1415 while (rate_idx != IWL_RATE_INVALID) {
1416 if (target_tpt < (100 * tpt_tbl[rate_idx]))
1417 break;
8ca151b5 1418
80763515
ES
1419 high_low = rs_get_adjacent_rate(mvm, rate_idx, rate_mask,
1420 tbl->rate.type);
8ca151b5 1421
80763515 1422 rate_idx = (high_low >> 8) & 0xff;
8ca151b5
JB
1423 }
1424
80763515
ES
1425 IWL_DEBUG_RATE(mvm, "Best rate found %d target_tp %d expected_new %d\n",
1426 rate_idx, target_tpt,
1427 rate_idx != IWL_RATE_INVALID ?
1428 100 * tpt_tbl[rate_idx] : IWL_INVALID_VALUE);
1429
1430 return rate_idx;
8ca151b5
JB
1431}
1432
5aa33553 1433static u32 rs_bw_from_sta_bw(struct ieee80211_sta *sta)
d310e405
ES
1434{
1435 if (sta->bandwidth >= IEEE80211_STA_RX_BW_80)
5aa33553 1436 return RATE_MCS_CHAN_WIDTH_80;
d310e405 1437 else if (sta->bandwidth >= IEEE80211_STA_RX_BW_40)
5aa33553
ES
1438 return RATE_MCS_CHAN_WIDTH_40;
1439
1440 return RATE_MCS_CHAN_WIDTH_20;
d310e405
ES
1441}
1442
8ca151b5
JB
1443/*
1444 * Check whether we should continue using same modulation mode, or
1445 * begin search for a new mode, based on:
1446 * 1) # tx successes or failures while using this mode
1447 * 2) # times calling this function
1448 * 3) elapsed time in this mode (not used, for now)
1449 */
1450static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search)
1451{
1452 struct iwl_scale_tbl_info *tbl;
8ca151b5
JB
1453 int active_tbl;
1454 int flush_interval_passed = 0;
1455 struct iwl_mvm *mvm;
1456
ae969afe 1457 mvm = lq_sta->pers.drv;
8ca151b5
JB
1458 active_tbl = lq_sta->active_tbl;
1459
1460 tbl = &(lq_sta->lq_info[active_tbl]);
1461
1462 /* If we've been disallowing search, see if we should now allow it */
b3b06a32 1463 if (lq_sta->rs_state == RS_STATE_STAY_IN_COLUMN) {
8ca151b5
JB
1464 /* Elapsed time using current modulation mode */
1465 if (lq_sta->flush_timer)
1466 flush_interval_passed =
1467 time_after(jiffies,
1468 (unsigned long)(lq_sta->flush_timer +
32b01726 1469 RS_STAY_IN_COLUMN_TIMEOUT));
8ca151b5
JB
1470
1471 /*
1472 * Check if we should allow search for new modulation mode.
1473 * If many frames have failed or succeeded, or we've used
1474 * this same modulation for a long time, allow search, and
1475 * reset history stats that keep track of whether we should
1476 * allow a new search. Also (below) reset all bitmaps and
1477 * stats in active history.
1478 */
1479 if (force_search ||
1480 (lq_sta->total_failed > lq_sta->max_failure_limit) ||
1481 (lq_sta->total_success > lq_sta->max_success_limit) ||
1482 ((!lq_sta->search_better_tbl) &&
1483 (lq_sta->flush_timer) && (flush_interval_passed))) {
1484 IWL_DEBUG_RATE(mvm,
1485 "LQ: stay is expired %d %d %d\n",
1486 lq_sta->total_failed,
1487 lq_sta->total_success,
1488 flush_interval_passed);
1489
1490 /* Allow search for new mode */
b3b06a32
ES
1491 lq_sta->rs_state = RS_STATE_SEARCH_CYCLE_STARTED;
1492 IWL_DEBUG_RATE(mvm,
1493 "Moving to RS_STATE_SEARCH_CYCLE_STARTED\n");
8ca151b5
JB
1494 lq_sta->total_failed = 0;
1495 lq_sta->total_success = 0;
1496 lq_sta->flush_timer = 0;
b3b06a32
ES
1497 /* mark the current column as visited */
1498 lq_sta->visited_columns = BIT(tbl->column);
8ca151b5
JB
1499 /*
1500 * Else if we've used this modulation mode enough repetitions
1501 * (regardless of elapsed time or success/failure), reset
1502 * history bitmaps and rate-specific stats for all rates in
1503 * active table.
1504 */
1505 } else {
1506 lq_sta->table_count++;
1507 if (lq_sta->table_count >=
1508 lq_sta->table_count_limit) {
1509 lq_sta->table_count = 0;
1510
1511 IWL_DEBUG_RATE(mvm,
1512 "LQ: stay in table clear win\n");
b804eeb6 1513 rs_rate_scale_clear_tbl_windows(mvm, tbl);
8ca151b5
JB
1514 }
1515 }
1516
1517 /* If transitioning to allow "search", reset all history
1518 * bitmaps and stats in active table (this will become the new
1519 * "search" table). */
b3b06a32 1520 if (lq_sta->rs_state == RS_STATE_SEARCH_CYCLE_STARTED) {
b804eeb6 1521 rs_rate_scale_clear_tbl_windows(mvm, tbl);
8ca151b5
JB
1522 }
1523 }
1524}
1525
1526/*
1527 * setup rate table in uCode
1528 */
1529static void rs_update_rate_tbl(struct iwl_mvm *mvm,
9145d151 1530 struct ieee80211_sta *sta,
8ca151b5 1531 struct iwl_lq_sta *lq_sta,
5aa33553 1532 struct rs_rate *rate)
8ca151b5 1533{
8fc7c58c 1534 rs_fill_lq_cmd(mvm, sta, lq_sta, rate);
9e680946 1535 iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, false);
8ca151b5
JB
1536}
1537
b3b06a32
ES
1538static enum rs_column rs_get_next_column(struct iwl_mvm *mvm,
1539 struct iwl_lq_sta *lq_sta,
1540 struct ieee80211_sta *sta,
1541 struct iwl_scale_tbl_info *tbl)
1542{
198266a3 1543 int i, j, max_rate;
b3b06a32
ES
1544 enum rs_column next_col_id;
1545 const struct rs_tx_column *curr_col = &rs_tx_columns[tbl->column];
1546 const struct rs_tx_column *next_col;
1547 allow_column_func_t allow_func;
4ed735e7 1548 u8 valid_ants = mvm->fw->valid_tx_ant;
a34529e8 1549 const u16 *expected_tpt_tbl;
198266a3 1550 u16 tpt, max_expected_tpt;
b3b06a32
ES
1551
1552 for (i = 0; i < MAX_NEXT_COLUMNS; i++) {
1553 next_col_id = curr_col->next_columns[i];
1554
1555 if (next_col_id == RS_COLUMN_INVALID)
1556 continue;
1557
1558 if (lq_sta->visited_columns & BIT(next_col_id)) {
1559 IWL_DEBUG_RATE(mvm, "Skip already visited column %d\n",
1560 next_col_id);
1561 continue;
1562 }
1563
1564 next_col = &rs_tx_columns[next_col_id];
1565
1566 if (!rs_is_valid_ant(valid_ants, next_col->ant)) {
1567 IWL_DEBUG_RATE(mvm,
1568 "Skip column %d as ANT config isn't supported by chip. valid_ants 0x%x column ant 0x%x\n",
1569 next_col_id, valid_ants, next_col->ant);
1570 continue;
1571 }
1572
1573 for (j = 0; j < MAX_COLUMN_CHECKS; j++) {
1574 allow_func = next_col->checks[j];
1575 if (allow_func && !allow_func(mvm, sta, tbl))
1576 break;
1577 }
1578
1579 if (j != MAX_COLUMN_CHECKS) {
1580 IWL_DEBUG_RATE(mvm,
1581 "Skip column %d: not allowed (check %d failed)\n",
1582 next_col_id, j);
1583
1584 continue;
1585 }
1586
1587 tpt = lq_sta->last_tpt / 100;
1588 expected_tpt_tbl = rs_get_expected_tpt_table(lq_sta, next_col,
049e0c33 1589 rs_bw_from_sta_bw(sta));
b3b06a32
ES
1590 if (WARN_ON_ONCE(!expected_tpt_tbl))
1591 continue;
1592
198266a3
ES
1593 max_rate = rs_get_max_allowed_rate(lq_sta, next_col);
1594 if (WARN_ON_ONCE(max_rate == IWL_RATE_INVALID))
1595 continue;
b3b06a32 1596
198266a3 1597 max_expected_tpt = expected_tpt_tbl[max_rate];
b3b06a32
ES
1598 if (tpt >= max_expected_tpt) {
1599 IWL_DEBUG_RATE(mvm,
1600 "Skip column %d: can't beat current TPT. Max expected %d current %d\n",
1601 next_col_id, max_expected_tpt, tpt);
1602 continue;
1603 }
1604
198266a3
ES
1605 IWL_DEBUG_RATE(mvm,
1606 "Found potential column %d. Max expected %d current %d\n",
1607 next_col_id, max_expected_tpt, tpt);
b3b06a32
ES
1608 break;
1609 }
1610
1611 if (i == MAX_NEXT_COLUMNS)
1612 return RS_COLUMN_INVALID;
1613
b3b06a32
ES
1614 return next_col_id;
1615}
1616
1617static int rs_switch_to_column(struct iwl_mvm *mvm,
1618 struct iwl_lq_sta *lq_sta,
1619 struct ieee80211_sta *sta,
1620 enum rs_column col_id)
1621{
1622 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1623 struct iwl_scale_tbl_info *search_tbl =
1624 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1625 struct rs_rate *rate = &search_tbl->rate;
1626 const struct rs_tx_column *column = &rs_tx_columns[col_id];
1627 const struct rs_tx_column *curr_column = &rs_tx_columns[tbl->column];
1628 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1629 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
80763515 1630 unsigned long rate_mask = 0;
b3b06a32
ES
1631 u32 rate_idx = 0;
1632
1633 memcpy(search_tbl, tbl, sz);
1634
1635 rate->sgi = column->sgi;
1636 rate->ant = column->ant;
1637
1638 if (column->mode == RS_LEGACY) {
1639 if (lq_sta->band == IEEE80211_BAND_5GHZ)
1640 rate->type = LQ_LEGACY_A;
1641 else
1642 rate->type = LQ_LEGACY_G;
1643
d8bafeaf
ES
1644 rate->bw = RATE_MCS_CHAN_WIDTH_20;
1645 rate->ldpc = false;
b3b06a32
ES
1646 rate_mask = lq_sta->active_legacy_rate;
1647 } else if (column->mode == RS_SISO) {
1648 rate->type = lq_sta->is_vht ? LQ_VHT_SISO : LQ_HT_SISO;
1649 rate_mask = lq_sta->active_siso_rate;
1650 } else if (column->mode == RS_MIMO2) {
1651 rate->type = lq_sta->is_vht ? LQ_VHT_MIMO2 : LQ_HT_MIMO2;
1652 rate_mask = lq_sta->active_mimo2_rate;
1653 } else {
1654 WARN_ON_ONCE("Bad column mode");
1655 }
1656
d8bafeaf
ES
1657 if (column->mode != RS_LEGACY) {
1658 rate->bw = rs_bw_from_sta_bw(sta);
1659 rate->ldpc = lq_sta->ldpc;
1660 }
1661
b3b06a32
ES
1662 search_tbl->column = col_id;
1663 rs_set_expected_tpt_table(lq_sta, search_tbl);
1664
75d3e2d6
ES
1665 lq_sta->visited_columns |= BIT(col_id);
1666
b3b06a32
ES
1667 /* Get the best matching rate if we're changing modes. e.g.
1668 * SISO->MIMO, LEGACY->SISO, MIMO->SISO
1669 */
1670 if (curr_column->mode != column->mode) {
1671 rate_idx = rs_get_best_rate(mvm, lq_sta, search_tbl,
1672 rate_mask, rate->index);
1673
1674 if ((rate_idx == IWL_RATE_INVALID) ||
1675 !(BIT(rate_idx) & rate_mask)) {
1676 IWL_DEBUG_RATE(mvm,
1677 "can not switch with index %d"
80763515 1678 " rate mask %lx\n",
b3b06a32
ES
1679 rate_idx, rate_mask);
1680
1681 goto err;
1682 }
1683
1684 rate->index = rate_idx;
1685 }
1686
b3b06a32
ES
1687 IWL_DEBUG_RATE(mvm, "Switched to column %d: Index %d\n",
1688 col_id, rate->index);
1689
b3b06a32
ES
1690 return 0;
1691
1692err:
1693 rate->type = LQ_NONE;
1694 return -1;
1695}
1696
4e4b815c
ES
1697static enum rs_action rs_get_rate_action(struct iwl_mvm *mvm,
1698 struct iwl_scale_tbl_info *tbl,
1699 s32 sr, int low, int high,
1700 int current_tpt,
1701 int low_tpt, int high_tpt)
1702{
1703 enum rs_action action = RS_ACTION_STAY;
1704
4e4b815c
ES
1705 if ((sr <= RS_SR_FORCE_DECREASE) || (current_tpt == 0)) {
1706 IWL_DEBUG_RATE(mvm,
e53839eb
ES
1707 "Decrease rate because of low SR\n");
1708 return RS_ACTION_DOWNSCALE;
4e4b815c
ES
1709 }
1710
e53839eb
ES
1711 if ((low_tpt == IWL_INVALID_VALUE) &&
1712 (high_tpt == IWL_INVALID_VALUE) &&
1713 (high != IWL_RATE_INVALID)) {
4e4b815c 1714 IWL_DEBUG_RATE(mvm,
e53839eb
ES
1715 "No data about high/low rates. Increase rate\n");
1716 return RS_ACTION_UPSCALE;
4e4b815c
ES
1717 }
1718
e53839eb
ES
1719 if ((high_tpt == IWL_INVALID_VALUE) &&
1720 (high != IWL_RATE_INVALID) &&
1721 (low_tpt != IWL_INVALID_VALUE) &&
1722 (low_tpt < current_tpt)) {
1723 IWL_DEBUG_RATE(mvm,
1724 "No data about high rate and low rate is worse. Increase rate\n");
1725 return RS_ACTION_UPSCALE;
1726 }
4e4b815c 1727
e53839eb
ES
1728 if ((high_tpt != IWL_INVALID_VALUE) &&
1729 (high_tpt > current_tpt)) {
1730 IWL_DEBUG_RATE(mvm,
1731 "Higher rate is better. Increate rate\n");
1732 return RS_ACTION_UPSCALE;
4e4b815c
ES
1733 }
1734
e53839eb
ES
1735 if ((low_tpt != IWL_INVALID_VALUE) &&
1736 (high_tpt != IWL_INVALID_VALUE) &&
1737 (low_tpt < current_tpt) &&
1738 (high_tpt < current_tpt)) {
4e4b815c 1739 IWL_DEBUG_RATE(mvm,
e53839eb
ES
1740 "Both high and low are worse. Maintain rate\n");
1741 return RS_ACTION_STAY;
1742 }
1743
1744 if ((low_tpt != IWL_INVALID_VALUE) &&
1745 (low_tpt > current_tpt)) {
1746 IWL_DEBUG_RATE(mvm,
1747 "Lower rate is better\n");
1748 action = RS_ACTION_DOWNSCALE;
1749 goto out;
1750 }
1751
1752 if ((low_tpt == IWL_INVALID_VALUE) &&
1753 (low != IWL_RATE_INVALID)) {
4e4b815c 1754 IWL_DEBUG_RATE(mvm,
e53839eb
ES
1755 "No data about lower rate\n");
1756 action = RS_ACTION_DOWNSCALE;
1757 goto out;
1758 }
1759
1760 IWL_DEBUG_RATE(mvm, "Maintain rate\n");
1761
1762out:
1763 if ((action == RS_ACTION_DOWNSCALE) && (low != IWL_RATE_INVALID)) {
1764 if (sr >= RS_SR_NO_DECREASE) {
1765 IWL_DEBUG_RATE(mvm,
1766 "SR is above NO DECREASE. Avoid downscale\n");
1767 action = RS_ACTION_STAY;
1768 } else if (current_tpt > (100 * tbl->expected_tpt[low])) {
1769 IWL_DEBUG_RATE(mvm,
1770 "Current TPT is higher than max expected in low rate. Avoid downscale\n");
1771 action = RS_ACTION_STAY;
1772 } else {
1773 IWL_DEBUG_RATE(mvm, "Decrease rate\n");
1774 }
4e4b815c
ES
1775 }
1776
1777 return action;
1778}
b3b06a32 1779
d8bafeaf
ES
1780static bool rs_stbc_allow(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
1781 struct iwl_lq_sta *lq_sta)
1782{
1783 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1784 struct ieee80211_vif *vif = mvmsta->vif;
1785 bool sta_ps_disabled = (vif->type == NL80211_IFTYPE_STATION &&
1786 !vif->bss_conf.ps);
1787
1788 /* Our chip supports Tx STBC and the peer is an HT/VHT STA which
1789 * supports STBC of at least 1*SS
1790 */
1791 if (!lq_sta->stbc)
1792 return false;
1793
1794 if (!mvm->ps_disabled && !sta_ps_disabled)
1795 return false;
1796
1797 if (!iwl_mvm_bt_coex_is_mimo_allowed(mvm, sta))
1798 return false;
1799
1800 return true;
1801}
1802
2fd647f8
EP
1803static void rs_get_adjacent_txp(struct iwl_mvm *mvm, int index,
1804 int *weaker, int *stronger)
1805{
1806 *weaker = index + TPC_TX_POWER_STEP;
1807 if (*weaker > TPC_MAX_REDUCTION)
1808 *weaker = TPC_INVALID;
1809
1810 *stronger = index - TPC_TX_POWER_STEP;
1811 if (*stronger < 0)
1812 *stronger = TPC_INVALID;
1813}
1814
1e9551de
ES
1815static bool rs_tpc_allowed(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1816 struct rs_rate *rate, enum ieee80211_band band)
2fd647f8
EP
1817{
1818 int index = rate->index;
1e9551de
ES
1819 bool cam = (iwlmvm_mod_params.power_scheme == IWL_POWER_SCHEME_CAM);
1820 bool sta_ps_disabled = (vif->type == NL80211_IFTYPE_STATION &&
1821 !vif->bss_conf.ps);
2fd647f8 1822
1e9551de
ES
1823 IWL_DEBUG_RATE(mvm, "cam: %d sta_ps_disabled %d\n",
1824 cam, sta_ps_disabled);
2fd647f8
EP
1825 /*
1826 * allow tpc only if power management is enabled, or bt coex
1827 * activity grade allows it and we are on 2.4Ghz.
1828 */
1e9551de 1829 if ((cam || sta_ps_disabled) &&
2fd647f8
EP
1830 !iwl_mvm_bt_coex_is_tpc_allowed(mvm, band))
1831 return false;
1832
1833 IWL_DEBUG_RATE(mvm, "check rate, table type: %d\n", rate->type);
1834 if (is_legacy(rate))
1835 return index == IWL_RATE_54M_INDEX;
1836 if (is_ht(rate))
1837 return index == IWL_RATE_MCS_7_INDEX;
1838 if (is_vht(rate))
1839 return index == IWL_RATE_MCS_7_INDEX ||
1840 index == IWL_RATE_MCS_8_INDEX ||
1841 index == IWL_RATE_MCS_9_INDEX;
1842
1843 WARN_ON_ONCE(1);
1844 return false;
1845}
1846
1847enum tpc_action {
1848 TPC_ACTION_STAY,
1849 TPC_ACTION_DECREASE,
1850 TPC_ACTION_INCREASE,
1851 TPC_ACTION_NO_RESTIRCTION,
1852};
1853
1854static enum tpc_action rs_get_tpc_action(struct iwl_mvm *mvm,
1855 s32 sr, int weak, int strong,
1856 int current_tpt,
1857 int weak_tpt, int strong_tpt)
1858{
1859 /* stay until we have valid tpt */
1860 if (current_tpt == IWL_INVALID_VALUE) {
1861 IWL_DEBUG_RATE(mvm, "no current tpt. stay.\n");
1862 return TPC_ACTION_STAY;
1863 }
1864
1865 /* Too many failures, increase txp */
1866 if (sr <= TPC_SR_FORCE_INCREASE || current_tpt == 0) {
1867 IWL_DEBUG_RATE(mvm, "increase txp because of weak SR\n");
1868 return TPC_ACTION_NO_RESTIRCTION;
1869 }
1870
1871 /* try decreasing first if applicable */
1872 if (weak != TPC_INVALID) {
1873 if (weak_tpt == IWL_INVALID_VALUE &&
1874 (strong_tpt == IWL_INVALID_VALUE ||
1875 current_tpt >= strong_tpt)) {
1876 IWL_DEBUG_RATE(mvm,
1877 "no weak txp measurement. decrease txp\n");
1878 return TPC_ACTION_DECREASE;
1879 }
1880
1881 if (weak_tpt > current_tpt) {
1882 IWL_DEBUG_RATE(mvm,
1883 "lower txp has better tpt. decrease txp\n");
1884 return TPC_ACTION_DECREASE;
1885 }
1886 }
1887
1888 /* next, increase if needed */
1889 if (sr < TPC_SR_NO_INCREASE && strong != TPC_INVALID) {
1890 if (weak_tpt == IWL_INVALID_VALUE &&
1891 strong_tpt != IWL_INVALID_VALUE &&
1892 current_tpt < strong_tpt) {
1893 IWL_DEBUG_RATE(mvm,
1894 "higher txp has better tpt. increase txp\n");
1895 return TPC_ACTION_INCREASE;
1896 }
1897
1898 if (weak_tpt < current_tpt &&
1899 (strong_tpt == IWL_INVALID_VALUE ||
1900 strong_tpt > current_tpt)) {
1901 IWL_DEBUG_RATE(mvm,
1902 "lower txp has worse tpt. increase txp\n");
1903 return TPC_ACTION_INCREASE;
1904 }
1905 }
1906
1907 IWL_DEBUG_RATE(mvm, "no need to increase or decrease txp - stay\n");
1908 return TPC_ACTION_STAY;
1909}
1910
1911static bool rs_tpc_perform(struct iwl_mvm *mvm,
1912 struct ieee80211_sta *sta,
1913 struct iwl_lq_sta *lq_sta,
1914 struct iwl_scale_tbl_info *tbl)
1915{
1916 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
1917 struct ieee80211_vif *vif = mvm_sta->vif;
1918 struct ieee80211_chanctx_conf *chanctx_conf;
1919 enum ieee80211_band band;
1920 struct iwl_rate_scale_data *window;
1921 struct rs_rate *rate = &tbl->rate;
1922 enum tpc_action action;
1923 s32 sr;
1924 u8 cur = lq_sta->lq.reduced_tpc;
1925 int current_tpt;
1926 int weak, strong;
1927 int weak_tpt = IWL_INVALID_VALUE, strong_tpt = IWL_INVALID_VALUE;
1928
9a75b3df 1929#ifdef CONFIG_MAC80211_DEBUGFS
ae969afe 1930 if (lq_sta->pers.dbg_fixed_txp_reduction <= TPC_MAX_REDUCTION) {
3c6acb61 1931 IWL_DEBUG_RATE(mvm, "fixed tpc: %d\n",
ae969afe
EP
1932 lq_sta->pers.dbg_fixed_txp_reduction);
1933 lq_sta->lq.reduced_tpc = lq_sta->pers.dbg_fixed_txp_reduction;
1934 return cur != lq_sta->pers.dbg_fixed_txp_reduction;
9a75b3df
EP
1935 }
1936#endif
1937
2fd647f8
EP
1938 rcu_read_lock();
1939 chanctx_conf = rcu_dereference(vif->chanctx_conf);
1940 if (WARN_ON(!chanctx_conf))
1941 band = IEEE80211_NUM_BANDS;
1942 else
1943 band = chanctx_conf->def.chan->band;
1944 rcu_read_unlock();
1945
1e9551de 1946 if (!rs_tpc_allowed(mvm, vif, rate, band)) {
2fd647f8 1947 IWL_DEBUG_RATE(mvm,
3c6acb61 1948 "tpc is not allowed. remove txp restrictions\n");
2fd647f8
EP
1949 lq_sta->lq.reduced_tpc = TPC_NO_REDUCTION;
1950 return cur != TPC_NO_REDUCTION;
1951 }
1952
1953 rs_get_adjacent_txp(mvm, cur, &weak, &strong);
1954
1955 /* Collect measured throughputs for current and adjacent rates */
1956 window = tbl->tpc_win;
1957 sr = window[cur].success_ratio;
1958 current_tpt = window[cur].average_tpt;
1959 if (weak != TPC_INVALID)
1960 weak_tpt = window[weak].average_tpt;
1961 if (strong != TPC_INVALID)
1962 strong_tpt = window[strong].average_tpt;
1963
1964 IWL_DEBUG_RATE(mvm,
1965 "(TPC: %d): cur_tpt %d SR %d weak %d strong %d weak_tpt %d strong_tpt %d\n",
1966 cur, current_tpt, sr, weak, strong,
1967 weak_tpt, strong_tpt);
1968
1969 action = rs_get_tpc_action(mvm, sr, weak, strong,
1970 current_tpt, weak_tpt, strong_tpt);
1971
1972 /* override actions if we are on the edge */
1973 if (weak == TPC_INVALID && action == TPC_ACTION_DECREASE) {
3c6acb61 1974 IWL_DEBUG_RATE(mvm, "already in lowest txp, stay\n");
2fd647f8
EP
1975 action = TPC_ACTION_STAY;
1976 } else if (strong == TPC_INVALID &&
1977 (action == TPC_ACTION_INCREASE ||
1978 action == TPC_ACTION_NO_RESTIRCTION)) {
3c6acb61 1979 IWL_DEBUG_RATE(mvm, "already in highest txp, stay\n");
2fd647f8
EP
1980 action = TPC_ACTION_STAY;
1981 }
1982
1983 switch (action) {
1984 case TPC_ACTION_DECREASE:
1985 lq_sta->lq.reduced_tpc = weak;
1986 return true;
1987 case TPC_ACTION_INCREASE:
1988 lq_sta->lq.reduced_tpc = strong;
1989 return true;
1990 case TPC_ACTION_NO_RESTIRCTION:
1991 lq_sta->lq.reduced_tpc = TPC_NO_REDUCTION;
1992 return true;
1993 case TPC_ACTION_STAY:
1994 /* do nothing */
1995 break;
1996 }
1997 return false;
1998}
1999
8ca151b5
JB
2000/*
2001 * Do rate scaling and search for new modulation mode.
2002 */
2003static void rs_rate_scale_perform(struct iwl_mvm *mvm,
8ca151b5 2004 struct ieee80211_sta *sta,
1ebc8f2e
ES
2005 struct iwl_lq_sta *lq_sta,
2006 int tid)
8ca151b5 2007{
8ca151b5
JB
2008 int low = IWL_RATE_INVALID;
2009 int high = IWL_RATE_INVALID;
2010 int index;
8ca151b5
JB
2011 struct iwl_rate_scale_data *window = NULL;
2012 int current_tpt = IWL_INVALID_VALUE;
2013 int low_tpt = IWL_INVALID_VALUE;
2014 int high_tpt = IWL_INVALID_VALUE;
2015 u32 fail_count;
4e4b815c 2016 enum rs_action scale_action = RS_ACTION_STAY;
8ca151b5
JB
2017 u16 rate_mask;
2018 u8 update_lq = 0;
2019 struct iwl_scale_tbl_info *tbl, *tbl1;
8ca151b5
JB
2020 u8 active_tbl = 0;
2021 u8 done_search = 0;
2022 u16 high_low;
2023 s32 sr;
9d015a03 2024 u8 prev_agg = lq_sta->is_agg;
8ca151b5
JB
2025 struct iwl_mvm_sta *sta_priv = (void *)sta->drv_priv;
2026 struct iwl_mvm_tid_data *tid_data;
5aa33553 2027 struct rs_rate *rate;
8ca151b5 2028
efed6640 2029 lq_sta->is_agg = !!sta_priv->agg_tids;
8ca151b5
JB
2030
2031 /*
2032 * Select rate-scale / modulation-mode table to work with in
2033 * the rest of this function: "search" if searching for better
2034 * modulation mode, or "active" if doing rate scaling within a mode.
2035 */
2036 if (!lq_sta->search_better_tbl)
2037 active_tbl = lq_sta->active_tbl;
2038 else
2039 active_tbl = 1 - lq_sta->active_tbl;
2040
2041 tbl = &(lq_sta->lq_info[active_tbl]);
5aa33553 2042 rate = &tbl->rate;
8ca151b5 2043
9d015a03
ES
2044 if (prev_agg != lq_sta->is_agg) {
2045 IWL_DEBUG_RATE(mvm,
2046 "Aggregation changed: prev %d current %d. Update expected TPT table\n",
2047 prev_agg, lq_sta->is_agg);
2048 rs_set_expected_tpt_table(lq_sta, tbl);
b804eeb6 2049 rs_rate_scale_clear_tbl_windows(mvm, tbl);
9d015a03
ES
2050 }
2051
8ca151b5
JB
2052 /* current tx rate */
2053 index = lq_sta->last_txrate_idx;
2054
8ca151b5 2055 /* rates available for this association, and for modulation mode */
a8ff14f5 2056 rate_mask = rs_get_supported_rates(lq_sta, rate);
8ca151b5 2057
ca6f38ff 2058 if (!(BIT(index) & rate_mask)) {
8ca151b5
JB
2059 IWL_ERR(mvm, "Current Rate is not valid\n");
2060 if (lq_sta->search_better_tbl) {
2061 /* revert to active table if search table is not valid*/
5aa33553 2062 rate->type = LQ_NONE;
8ca151b5
JB
2063 lq_sta->search_better_tbl = 0;
2064 tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
5aa33553 2065 rs_update_rate_tbl(mvm, sta, lq_sta, &tbl->rate);
8ca151b5
JB
2066 }
2067 return;
2068 }
2069
2070 /* Get expected throughput table and history window for current rate */
2071 if (!tbl->expected_tpt) {
2072 IWL_ERR(mvm, "tbl->expected_tpt is NULL\n");
2073 return;
2074 }
2075
2536e7c3 2076 /* TODO: handle rate_idx_mask and rate_idx_mcs_mask */
8ca151b5
JB
2077 window = &(tbl->win[index]);
2078
2079 /*
2080 * If there is not enough history to calculate actual average
2081 * throughput, keep analyzing results of more tx frames, without
2082 * changing rate or mode (bypass most of the rest of this function).
2083 * Set up new rate table in uCode only if old rate is not supported
2084 * in current association (use new rate found above).
2085 */
2086 fail_count = window->counter - window->success_counter;
2087 if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
2088 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
2089 IWL_DEBUG_RATE(mvm,
4d30ee82 2090 "(%s: %d): Test Window: succ %d total %d\n",
5aa33553 2091 rs_pretty_lq_type(rate->type),
4d30ee82 2092 index, window->success_counter, window->counter);
8ca151b5
JB
2093
2094 /* Can't calculate this yet; not enough history */
2095 window->average_tpt = IWL_INVALID_VALUE;
2096
2097 /* Should we stay with this modulation mode,
2098 * or search for a new one? */
2099 rs_stay_in_table(lq_sta, false);
2100
2101 goto out;
2102 }
2103 /* Else we have enough samples; calculate estimate of
2104 * actual average throughput */
2105 if (window->average_tpt != ((window->success_ratio *
2106 tbl->expected_tpt[index] + 64) / 128)) {
8ca151b5
JB
2107 window->average_tpt = ((window->success_ratio *
2108 tbl->expected_tpt[index] + 64) / 128);
2109 }
2110
2111 /* If we are searching for better modulation mode, check success. */
2112 if (lq_sta->search_better_tbl) {
2113 /* If good success, continue using the "search" mode;
2114 * no need to send new link quality command, since we're
2115 * continuing to use the setup that we've been trying. */
2116 if (window->average_tpt > lq_sta->last_tpt) {
2117 IWL_DEBUG_RATE(mvm,
4d30ee82
ES
2118 "SWITCHING TO NEW TABLE SR: %d "
2119 "cur-tpt %d old-tpt %d\n",
8ca151b5
JB
2120 window->success_ratio,
2121 window->average_tpt,
2122 lq_sta->last_tpt);
2123
8ca151b5
JB
2124 /* Swap tables; "search" becomes "active" */
2125 lq_sta->active_tbl = active_tbl;
2126 current_tpt = window->average_tpt;
2127 /* Else poor success; go back to mode in "active" table */
2128 } else {
2129 IWL_DEBUG_RATE(mvm,
4d30ee82
ES
2130 "GOING BACK TO THE OLD TABLE: SR %d "
2131 "cur-tpt %d old-tpt %d\n",
8ca151b5
JB
2132 window->success_ratio,
2133 window->average_tpt,
2134 lq_sta->last_tpt);
2135
2136 /* Nullify "search" table */
5aa33553 2137 rate->type = LQ_NONE;
8ca151b5
JB
2138
2139 /* Revert to "active" table */
2140 active_tbl = lq_sta->active_tbl;
2141 tbl = &(lq_sta->lq_info[active_tbl]);
2142
2143 /* Revert to "active" rate and throughput info */
8fc7c58c 2144 index = tbl->rate.index;
8ca151b5
JB
2145 current_tpt = lq_sta->last_tpt;
2146
2147 /* Need to set up a new rate table in uCode */
2148 update_lq = 1;
2149 }
2150
2151 /* Either way, we've made a decision; modulation mode
2152 * search is done, allow rate adjustment next time. */
2153 lq_sta->search_better_tbl = 0;
2154 done_search = 1; /* Don't switch modes below! */
2155 goto lq_update;
2156 }
2157
2158 /* (Else) not in search of better modulation mode, try for better
2159 * starting rate, while staying in this mode. */
ca6f38ff 2160 high_low = rs_get_adjacent_rate(mvm, index, rate_mask, rate->type);
8ca151b5
JB
2161 low = high_low & 0xff;
2162 high = (high_low >> 8) & 0xff;
2163
2536e7c3 2164 /* TODO: handle rate_idx_mask and rate_idx_mcs_mask */
8ca151b5
JB
2165
2166 sr = window->success_ratio;
2167
2168 /* Collect measured throughputs for current and adjacent rates */
2169 current_tpt = window->average_tpt;
2170 if (low != IWL_RATE_INVALID)
2171 low_tpt = tbl->win[low].average_tpt;
2172 if (high != IWL_RATE_INVALID)
2173 high_tpt = tbl->win[high].average_tpt;
2174
4d30ee82
ES
2175 IWL_DEBUG_RATE(mvm,
2176 "(%s: %d): cur_tpt %d SR %d low %d high %d low_tpt %d high_tpt %d\n",
5aa33553
ES
2177 rs_pretty_lq_type(rate->type), index, current_tpt, sr,
2178 low, high, low_tpt, high_tpt);
4d30ee82 2179
4e4b815c
ES
2180 scale_action = rs_get_rate_action(mvm, tbl, sr, low, high,
2181 current_tpt, low_tpt, high_tpt);
8ca151b5 2182
393b9e5b 2183 /* Force a search in case BT doesn't like us being in MIMO */
5aa33553 2184 if (is_mimo(rate) &&
393b9e5b 2185 !iwl_mvm_bt_coex_is_mimo_allowed(mvm, sta)) {
4d30ee82
ES
2186 IWL_DEBUG_RATE(mvm,
2187 "BT Coex forbids MIMO. Search for new config\n");
36946ce6
EG
2188 rs_stay_in_table(lq_sta, true);
2189 goto lq_update;
2190 }
2191
8ca151b5 2192 switch (scale_action) {
4e4b815c 2193 case RS_ACTION_DOWNSCALE:
8ca151b5
JB
2194 /* Decrease starting rate, update uCode's rate table */
2195 if (low != IWL_RATE_INVALID) {
2196 update_lq = 1;
2197 index = low;
4d30ee82
ES
2198 } else {
2199 IWL_DEBUG_RATE(mvm,
2200 "At the bottom rate. Can't decrease\n");
8ca151b5
JB
2201 }
2202
2203 break;
4e4b815c 2204 case RS_ACTION_UPSCALE:
8ca151b5
JB
2205 /* Increase starting rate, update uCode's rate table */
2206 if (high != IWL_RATE_INVALID) {
2207 update_lq = 1;
2208 index = high;
4d30ee82
ES
2209 } else {
2210 IWL_DEBUG_RATE(mvm,
2211 "At the top rate. Can't increase\n");
8ca151b5
JB
2212 }
2213
2214 break;
4e4b815c 2215 case RS_ACTION_STAY:
8ca151b5 2216 /* No change */
e6c8d602
ES
2217 if (lq_sta->rs_state == RS_STATE_STAY_IN_COLUMN)
2218 update_lq = rs_tpc_perform(mvm, sta, lq_sta, tbl);
2fd647f8 2219 break;
8ca151b5
JB
2220 default:
2221 break;
2222 }
2223
8ca151b5
JB
2224lq_update:
2225 /* Replace uCode's rate table for the destination station. */
5aa33553
ES
2226 if (update_lq) {
2227 tbl->rate.index = index;
2228 rs_update_rate_tbl(mvm, sta, lq_sta, &tbl->rate);
2229 }
8ca151b5
JB
2230
2231 rs_stay_in_table(lq_sta, false);
2232
2233 /*
2234 * Search for new modulation mode if we're:
2235 * 1) Not changing rates right now
2236 * 2) Not just finishing up a search
2237 * 3) Allowing a new search
2238 */
2239 if (!update_lq && !done_search &&
b3b06a32
ES
2240 lq_sta->rs_state == RS_STATE_SEARCH_CYCLE_STARTED
2241 && window->counter) {
2242 enum rs_column next_column;
2243
8ca151b5
JB
2244 /* Save current throughput to compare with "search" throughput*/
2245 lq_sta->last_tpt = current_tpt;
2246
4d30ee82 2247 IWL_DEBUG_RATE(mvm,
b3b06a32
ES
2248 "Start Search: update_lq %d done_search %d rs_state %d win->counter %d\n",
2249 update_lq, done_search, lq_sta->rs_state,
4d30ee82 2250 window->counter);
b3b06a32
ES
2251
2252 next_column = rs_get_next_column(mvm, lq_sta, sta, tbl);
2253 if (next_column != RS_COLUMN_INVALID) {
2254 int ret = rs_switch_to_column(mvm, lq_sta, sta,
2255 next_column);
2256 if (!ret)
2257 lq_sta->search_better_tbl = 1;
2258 } else {
2259 IWL_DEBUG_RATE(mvm,
2260 "No more columns to explore in search cycle. Go to RS_STATE_SEARCH_CYCLE_ENDED\n");
2261 lq_sta->rs_state = RS_STATE_SEARCH_CYCLE_ENDED;
2262 }
8ca151b5
JB
2263
2264 /* If new "search" mode was selected, set up in uCode table */
2265 if (lq_sta->search_better_tbl) {
2266 /* Access the "search" table, clear its history. */
2267 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
b804eeb6 2268 rs_rate_scale_clear_tbl_windows(mvm, tbl);
8ca151b5
JB
2269
2270 /* Use new "search" start rate */
8fc7c58c 2271 index = tbl->rate.index;
8ca151b5 2272
b3b06a32
ES
2273 rs_dump_rate(mvm, &tbl->rate,
2274 "Switch to SEARCH TABLE:");
8fc7c58c 2275 rs_fill_lq_cmd(mvm, sta, lq_sta, &tbl->rate);
9e680946 2276 iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, false);
8ca151b5
JB
2277 } else {
2278 done_search = 1;
2279 }
2280 }
2281
b3b06a32 2282 if (done_search && lq_sta->rs_state == RS_STATE_SEARCH_CYCLE_ENDED) {
8ca151b5
JB
2283 /* If the "active" (non-search) mode was legacy,
2284 * and we've tried switching antennas,
2285 * but we haven't been able to try HT modes (not available),
2286 * stay with best antenna legacy modulation for a while
2287 * before next round of mode comparisons. */
2288 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
fd7dbee5 2289 if (is_legacy(&tbl1->rate)) {
8ca151b5 2290 IWL_DEBUG_RATE(mvm, "LQ: STAY in legacy table\n");
fd7dbee5
ES
2291
2292 if (tid != IWL_MAX_TID_COUNT) {
2293 tid_data = &sta_priv->tid_data[tid];
2294 if (tid_data->state != IWL_AGG_OFF) {
2295 IWL_DEBUG_RATE(mvm,
2296 "Stop aggregation on tid %d\n",
2297 tid);
2298 ieee80211_stop_tx_ba_session(sta, tid);
2299 }
2300 }
8ca151b5 2301 rs_set_stay_in_table(mvm, 1, lq_sta);
b3b06a32 2302 } else {
8ca151b5
JB
2303 /* If we're in an HT mode, and all 3 mode switch actions
2304 * have been tried and compared, stay in this best modulation
2305 * mode for a while before next round of mode comparisons. */
8ca151b5
JB
2306 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2307 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
2308 (tid != IWL_MAX_TID_COUNT)) {
2309 tid_data = &sta_priv->tid_data[tid];
2310 if (tid_data->state == IWL_AGG_OFF) {
2311 IWL_DEBUG_RATE(mvm,
2312 "try to aggregate tid %d\n",
2313 tid);
2314 rs_tl_turn_on_agg(mvm, tid,
2315 lq_sta, sta);
2316 }
2317 }
2318 rs_set_stay_in_table(mvm, 0, lq_sta);
2319 }
2320 }
2321
2322out:
8ca151b5
JB
2323 lq_sta->last_txrate_idx = index;
2324}
2325
361dbec8
ES
2326struct rs_init_rate_info {
2327 s8 rssi;
2328 u8 rate_idx;
2329};
2330
2331static const struct rs_init_rate_info rs_init_rates_24ghz[] = {
2332 { -60, IWL_RATE_54M_INDEX },
2333 { -64, IWL_RATE_48M_INDEX },
2334 { -68, IWL_RATE_36M_INDEX },
2335 { -80, IWL_RATE_24M_INDEX },
2336 { -84, IWL_RATE_18M_INDEX },
2337 { -85, IWL_RATE_12M_INDEX },
2338 { -86, IWL_RATE_11M_INDEX },
2339 { -88, IWL_RATE_5M_INDEX },
2340 { -90, IWL_RATE_2M_INDEX },
2341 { S8_MIN, IWL_RATE_1M_INDEX },
2342};
2343
2344static const struct rs_init_rate_info rs_init_rates_5ghz[] = {
2345 { -60, IWL_RATE_54M_INDEX },
2346 { -64, IWL_RATE_48M_INDEX },
2347 { -72, IWL_RATE_36M_INDEX },
2348 { -80, IWL_RATE_24M_INDEX },
2349 { -84, IWL_RATE_18M_INDEX },
2350 { -85, IWL_RATE_12M_INDEX },
2351 { -87, IWL_RATE_9M_INDEX },
2352 { S8_MIN, IWL_RATE_6M_INDEX },
2353};
2354
2355/* Choose an initial legacy rate and antenna to use based on the RSSI
2356 * of last Rx
2357 */
2358static void rs_get_initial_rate(struct iwl_mvm *mvm,
2359 struct iwl_lq_sta *lq_sta,
2360 enum ieee80211_band band,
2361 struct rs_rate *rate)
2362{
2363 int i, nentries;
2364 s8 best_rssi = S8_MIN;
2365 u8 best_ant = ANT_NONE;
2366 u8 valid_tx_ant = mvm->fw->valid_tx_ant;
2367 const struct rs_init_rate_info *initial_rates;
2368
2369 for (i = 0; i < ARRAY_SIZE(lq_sta->pers.chain_signal); i++) {
2370 if (!(lq_sta->pers.chains & BIT(i)))
2371 continue;
2372
2373 if (lq_sta->pers.chain_signal[i] > best_rssi) {
2374 best_rssi = lq_sta->pers.chain_signal[i];
2375 best_ant = BIT(i);
2376 }
2377 }
2378
2379 IWL_DEBUG_RATE(mvm, "Best ANT: %s Best RSSI: %d\n",
2380 rs_pretty_ant(best_ant), best_rssi);
2381
2382 if (best_ant != ANT_A && best_ant != ANT_B)
2383 rate->ant = first_antenna(valid_tx_ant);
2384 else
2385 rate->ant = best_ant;
2386
2387 rate->sgi = false;
2388 rate->ldpc = false;
2389 rate->bw = RATE_MCS_CHAN_WIDTH_20;
2390
2391 rate->index = find_first_bit(&lq_sta->active_legacy_rate,
2392 BITS_PER_LONG);
2393
2394 if (band == IEEE80211_BAND_5GHZ) {
2395 rate->type = LQ_LEGACY_A;
2396 initial_rates = rs_init_rates_5ghz;
2397 nentries = ARRAY_SIZE(rs_init_rates_5ghz);
2398 } else {
2399 rate->type = LQ_LEGACY_G;
2400 initial_rates = rs_init_rates_24ghz;
2401 nentries = ARRAY_SIZE(rs_init_rates_24ghz);
2402 }
2403
2404 if (IWL_MVM_RS_RSSI_BASED_INIT_RATE) {
2405 for (i = 0; i < nentries; i++) {
2406 int rate_idx = initial_rates[i].rate_idx;
2407 if ((best_rssi >= initial_rates[i].rssi) &&
2408 (BIT(rate_idx) & lq_sta->active_legacy_rate)) {
2409 rate->index = rate_idx;
2410 break;
2411 }
2412 }
2413 }
2414
2415 IWL_DEBUG_RATE(mvm, "rate_idx %d ANT %s\n", rate->index,
2416 rs_pretty_ant(rate->ant));
2417}
2418
2419/* Save info about RSSI of last Rx */
2420void rs_update_last_rssi(struct iwl_mvm *mvm,
2421 struct iwl_lq_sta *lq_sta,
2422 struct ieee80211_rx_status *rx_status)
2423{
2424 lq_sta->pers.chains = rx_status->chains;
2425 lq_sta->pers.chain_signal[0] = rx_status->chain_signal[0];
2426 lq_sta->pers.chain_signal[1] = rx_status->chain_signal[1];
2427 lq_sta->pers.chain_signal[2] = rx_status->chain_signal[2];
2428}
2429
8ca151b5
JB
2430/**
2431 * rs_initialize_lq - Initialize a station's hardware rate table
2432 *
2433 * The uCode's station table contains a table of fallback rates
2434 * for automatic fallback during transmission.
2435 *
2436 * NOTE: This sets up a default set of values. These will be replaced later
2437 * if the driver's iwl-agn-rs rate scaling algorithm is used, instead of
2438 * rc80211_simple.
2439 *
2440 * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
2441 * calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
2442 * which requires station table entry to exist).
2443 */
2444static void rs_initialize_lq(struct iwl_mvm *mvm,
2445 struct ieee80211_sta *sta,
2446 struct iwl_lq_sta *lq_sta,
b87c2179
ES
2447 enum ieee80211_band band,
2448 bool init)
8ca151b5
JB
2449{
2450 struct iwl_scale_tbl_info *tbl;
5aa33553 2451 struct rs_rate *rate;
8ca151b5 2452 u8 active_tbl = 0;
8ca151b5
JB
2453
2454 if (!sta || !lq_sta)
2455 return;
2456
8ca151b5
JB
2457 if (!lq_sta->search_better_tbl)
2458 active_tbl = lq_sta->active_tbl;
2459 else
2460 active_tbl = 1 - lq_sta->active_tbl;
2461
2462 tbl = &(lq_sta->lq_info[active_tbl]);
5aa33553 2463 rate = &tbl->rate;
8ca151b5 2464
361dbec8
ES
2465 rs_get_initial_rate(mvm, lq_sta, band, rate);
2466 lq_sta->last_txrate_idx = rate->index;
8ca151b5 2467
b3b06a32
ES
2468 WARN_ON_ONCE(rate->ant != ANT_A && rate->ant != ANT_B);
2469 if (rate->ant == ANT_A)
2470 tbl->column = RS_COLUMN_LEGACY_ANT_A;
2471 else
2472 tbl->column = RS_COLUMN_LEGACY_ANT_B;
2473
8ca151b5 2474 rs_set_expected_tpt_table(lq_sta, tbl);
80e9e5cd 2475 rs_fill_lq_cmd(mvm, sta, lq_sta, rate);
8ca151b5 2476 /* TODO restore station should remember the lq cmd */
b87c2179 2477 iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, init);
8ca151b5
JB
2478}
2479
2480static void rs_get_rate(void *mvm_r, struct ieee80211_sta *sta, void *mvm_sta,
2481 struct ieee80211_tx_rate_control *txrc)
2482{
2483 struct sk_buff *skb = txrc->skb;
8ca151b5
JB
2484 struct iwl_op_mode *op_mode __maybe_unused =
2485 (struct iwl_op_mode *)mvm_r;
2486 struct iwl_mvm *mvm __maybe_unused = IWL_OP_MODE_GET_MVM(op_mode);
2487 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2488 struct iwl_lq_sta *lq_sta = mvm_sta;
8ca151b5 2489
2536e7c3 2490 /* TODO: handle rate_idx_mask and rate_idx_mcs_mask */
8ca151b5
JB
2491
2492 /* Treat uninitialized rate scaling data same as non-existing. */
ae969afe 2493 if (lq_sta && !lq_sta->pers.drv) {
8ca151b5
JB
2494 IWL_DEBUG_RATE(mvm, "Rate scaling not initialized yet.\n");
2495 mvm_sta = NULL;
2496 }
2497
2498 /* Send management frames and NO_ACK data using lowest rate. */
2499 if (rate_control_send_low(sta, mvm_sta, txrc))
2500 return;
2501
d310e405
ES
2502 iwl_mvm_hwrate_to_tx_rate(lq_sta->last_rate_n_flags,
2503 info->band, &info->control.rates[0]);
2504
622ebe99 2505 info->control.rates[0].count = 1;
8ca151b5
JB
2506}
2507
2508static void *rs_alloc_sta(void *mvm_rate, struct ieee80211_sta *sta,
2509 gfp_t gfp)
2510{
2511 struct iwl_mvm_sta *sta_priv = (struct iwl_mvm_sta *)sta->drv_priv;
ae969afe
EP
2512 struct iwl_op_mode *op_mode = (struct iwl_op_mode *)mvm_rate;
2513 struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
2514 struct iwl_lq_sta *lq_sta = &sta_priv->lq_sta;
8ca151b5
JB
2515
2516 IWL_DEBUG_RATE(mvm, "create station rate scale window\n");
2517
ae969afe
EP
2518 lq_sta->pers.drv = mvm;
2519#ifdef CONFIG_MAC80211_DEBUGFS
2520 lq_sta->pers.dbg_fixed_rate = 0;
2521 lq_sta->pers.dbg_fixed_txp_reduction = TPC_INVALID;
2522#endif
361dbec8
ES
2523 lq_sta->pers.chains = 0;
2524 memset(lq_sta->pers.chain_signal, 0, sizeof(lq_sta->pers.chain_signal));
ae969afe 2525
8ca151b5
JB
2526 return &sta_priv->lq_sta;
2527}
2528
d310e405
ES
2529static int rs_vht_highest_rx_mcs_index(struct ieee80211_sta_vht_cap *vht_cap,
2530 int nss)
2531{
2532 u16 rx_mcs = le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map) &
2533 (0x3 << (2 * (nss - 1)));
2534 rx_mcs >>= (2 * (nss - 1));
2535
2536 if (rx_mcs == IEEE80211_VHT_MCS_SUPPORT_0_7)
2537 return IWL_RATE_MCS_7_INDEX;
2538 else if (rx_mcs == IEEE80211_VHT_MCS_SUPPORT_0_8)
2539 return IWL_RATE_MCS_8_INDEX;
2540 else if (rx_mcs == IEEE80211_VHT_MCS_SUPPORT_0_9)
2541 return IWL_RATE_MCS_9_INDEX;
2542
2543 WARN_ON_ONCE(rx_mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED);
2544 return -1;
2545}
2546
750a1b45
ES
2547static void rs_vht_set_enabled_rates(struct ieee80211_sta *sta,
2548 struct ieee80211_sta_vht_cap *vht_cap,
2549 struct iwl_lq_sta *lq_sta)
2550{
2551 int i;
2552 int highest_mcs = rs_vht_highest_rx_mcs_index(vht_cap, 1);
2553
2554 if (highest_mcs >= IWL_RATE_MCS_0_INDEX) {
2555 for (i = IWL_RATE_MCS_0_INDEX; i <= highest_mcs; i++) {
2556 if (i == IWL_RATE_9M_INDEX)
2557 continue;
2558
271518ad
ES
2559 /* VHT MCS9 isn't valid for 20Mhz for NSS=1,2 */
2560 if (i == IWL_RATE_MCS_9_INDEX &&
2561 sta->bandwidth == IEEE80211_STA_RX_BW_20)
2562 continue;
2563
750a1b45
ES
2564 lq_sta->active_siso_rate |= BIT(i);
2565 }
2566 }
2567
ecc90e79
ES
2568 if (sta->rx_nss < 2)
2569 return;
2570
750a1b45
ES
2571 highest_mcs = rs_vht_highest_rx_mcs_index(vht_cap, 2);
2572 if (highest_mcs >= IWL_RATE_MCS_0_INDEX) {
2573 for (i = IWL_RATE_MCS_0_INDEX; i <= highest_mcs; i++) {
2574 if (i == IWL_RATE_9M_INDEX)
2575 continue;
2576
271518ad
ES
2577 /* VHT MCS9 isn't valid for 20Mhz for NSS=1,2 */
2578 if (i == IWL_RATE_MCS_9_INDEX &&
2579 sta->bandwidth == IEEE80211_STA_RX_BW_20)
2580 continue;
2581
750a1b45
ES
2582 lq_sta->active_mimo2_rate |= BIT(i);
2583 }
2584 }
2585}
2586
5fc0f76c
ES
2587#ifdef CONFIG_IWLWIFI_DEBUGFS
2588static void iwl_mvm_reset_frame_stats(struct iwl_mvm *mvm,
2589 struct iwl_mvm_frame_stats *stats)
2590{
2591 spin_lock_bh(&mvm->drv_stats_lock);
2592 memset(stats, 0, sizeof(*stats));
2593 spin_unlock_bh(&mvm->drv_stats_lock);
2594}
2595
2596void iwl_mvm_update_frame_stats(struct iwl_mvm *mvm,
2597 struct iwl_mvm_frame_stats *stats,
2598 u32 rate, bool agg)
2599{
2600 u8 nss = 0, mcs = 0;
2601
2602 spin_lock(&mvm->drv_stats_lock);
2603
2604 if (agg)
2605 stats->agg_frames++;
2606
2607 stats->success_frames++;
2608
2609 switch (rate & RATE_MCS_CHAN_WIDTH_MSK) {
2610 case RATE_MCS_CHAN_WIDTH_20:
2611 stats->bw_20_frames++;
2612 break;
2613 case RATE_MCS_CHAN_WIDTH_40:
2614 stats->bw_40_frames++;
2615 break;
2616 case RATE_MCS_CHAN_WIDTH_80:
2617 stats->bw_80_frames++;
2618 break;
2619 default:
2620 WARN_ONCE(1, "bad BW. rate 0x%x", rate);
2621 }
2622
2623 if (rate & RATE_MCS_HT_MSK) {
2624 stats->ht_frames++;
2625 mcs = rate & RATE_HT_MCS_RATE_CODE_MSK;
2626 nss = ((rate & RATE_HT_MCS_NSS_MSK) >> RATE_HT_MCS_NSS_POS) + 1;
2627 } else if (rate & RATE_MCS_VHT_MSK) {
2628 stats->vht_frames++;
2629 mcs = rate & RATE_VHT_MCS_RATE_CODE_MSK;
2630 nss = ((rate & RATE_VHT_MCS_NSS_MSK) >>
2631 RATE_VHT_MCS_NSS_POS) + 1;
2632 } else {
2633 stats->legacy_frames++;
2634 }
2635
2636 if (nss == 1)
2637 stats->siso_frames++;
2638 else if (nss == 2)
2639 stats->mimo2_frames++;
2640
2641 if (rate & RATE_MCS_SGI_MSK)
2642 stats->sgi_frames++;
2643 else
2644 stats->ngi_frames++;
2645
2646 stats->last_rates[stats->last_frame_idx] = rate;
2647 stats->last_frame_idx = (stats->last_frame_idx + 1) %
2648 ARRAY_SIZE(stats->last_rates);
2649
2650 spin_unlock(&mvm->drv_stats_lock);
2651}
2652#endif
2653
8ca151b5
JB
2654/*
2655 * Called after adding a new station to initialize rate scaling
2656 */
2657void iwl_mvm_rs_rate_init(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
b87c2179 2658 enum ieee80211_band band, bool init)
8ca151b5
JB
2659{
2660 int i, j;
2661 struct ieee80211_hw *hw = mvm->hw;
2662 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
d310e405 2663 struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
8ca151b5
JB
2664 struct iwl_mvm_sta *sta_priv;
2665 struct iwl_lq_sta *lq_sta;
2666 struct ieee80211_supported_band *sband;
2667 unsigned long supp; /* must be unsigned long for for_each_set_bit */
2668
2669 sta_priv = (struct iwl_mvm_sta *)sta->drv_priv;
2670 lq_sta = &sta_priv->lq_sta;
ae969afe
EP
2671
2672 /* clear all non-persistent lq data */
2673 memset(lq_sta, 0, offsetof(typeof(*lq_sta), pers));
b87c2179 2674
8ca151b5
JB
2675 sband = hw->wiphy->bands[band];
2676
2677 lq_sta->lq.sta_id = sta_priv->sta_id;
2678
2679 for (j = 0; j < LQ_SIZE; j++)
b804eeb6 2680 rs_rate_scale_clear_tbl_windows(mvm, &lq_sta->lq_info[j]);
8ca151b5
JB
2681
2682 lq_sta->flush_timer = 0;
87d5e415 2683 lq_sta->last_tx = jiffies;
8ca151b5
JB
2684
2685 IWL_DEBUG_RATE(mvm,
2686 "LQ: *** rate scale station global init for station %d ***\n",
2687 sta_priv->sta_id);
2688 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2689 * the lowest or the highest rate.. Could consider using RSSI from
2690 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2691 * after assoc.. */
2692
8ca151b5 2693 lq_sta->missed_rate_counter = IWL_MISSED_RATE_MAX;
8ca151b5
JB
2694 lq_sta->band = sband->band;
2695 /*
2696 * active legacy rates as per supported rates bitmap
2697 */
2698 supp = sta->supp_rates[sband->band];
2699 lq_sta->active_legacy_rate = 0;
2700 for_each_set_bit(i, &supp, BITS_PER_LONG)
2701 lq_sta->active_legacy_rate |= BIT(sband->bitrates[i].hw_value);
2702
d310e405
ES
2703 /* TODO: should probably account for rx_highest for both HT/VHT */
2704 if (!vht_cap || !vht_cap->vht_supported) {
2705 /* active_siso_rate mask includes 9 MBits (bit 5),
2706 * and CCK (bits 0-3), supp_rates[] does not;
2707 * shift to convert format, force 9 MBits off.
2708 */
2709 lq_sta->active_siso_rate = ht_cap->mcs.rx_mask[0] << 1;
2710 lq_sta->active_siso_rate |= ht_cap->mcs.rx_mask[0] & 0x1;
2711 lq_sta->active_siso_rate &= ~((u16)0x2);
2712 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
2713
2714 /* Same here */
2715 lq_sta->active_mimo2_rate = ht_cap->mcs.rx_mask[1] << 1;
2716 lq_sta->active_mimo2_rate |= ht_cap->mcs.rx_mask[1] & 0x1;
2717 lq_sta->active_mimo2_rate &= ~((u16)0x2);
2718 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2719
2720 lq_sta->is_vht = false;
a3576ff2
ES
2721 if (mvm->cfg->ht_params->ldpc &&
2722 (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING))
2723 lq_sta->ldpc = true;
d8bafeaf
ES
2724
2725 if (mvm->cfg->ht_params->stbc &&
2726 (num_of_ant(mvm->fw->valid_tx_ant) > 1) &&
2727 (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC))
2728 lq_sta->stbc = true;
d310e405 2729 } else {
750a1b45 2730 rs_vht_set_enabled_rates(sta, vht_cap, lq_sta);
d310e405 2731 lq_sta->is_vht = true;
a3576ff2
ES
2732
2733 if (mvm->cfg->ht_params->ldpc &&
2734 (vht_cap->cap & IEEE80211_VHT_CAP_RXLDPC))
2735 lq_sta->ldpc = true;
d8bafeaf
ES
2736
2737 if (mvm->cfg->ht_params->stbc &&
2738 (num_of_ant(mvm->fw->valid_tx_ant) > 1) &&
2739 (vht_cap->cap & IEEE80211_VHT_CAP_RXSTBC_MASK))
2740 lq_sta->stbc = true;
d310e405 2741 }
8ca151b5 2742
dc574e52
ES
2743 if (IWL_MVM_RS_DISABLE_MIMO)
2744 lq_sta->active_mimo2_rate = 0;
2745
198266a3
ES
2746 lq_sta->max_legacy_rate_idx = find_last_bit(&lq_sta->active_legacy_rate,
2747 BITS_PER_LONG);
2748 lq_sta->max_siso_rate_idx = find_last_bit(&lq_sta->active_siso_rate,
2749 BITS_PER_LONG);
2750 lq_sta->max_mimo2_rate_idx = find_last_bit(&lq_sta->active_mimo2_rate,
2751 BITS_PER_LONG);
2752
a3576ff2 2753 IWL_DEBUG_RATE(mvm,
d8bafeaf 2754 "RATE MASK: LEGACY=%lX SISO=%lX MIMO2=%lX VHT=%d LDPC=%d STBC%d\n",
198266a3 2755 lq_sta->active_legacy_rate,
8ca151b5 2756 lq_sta->active_siso_rate,
d310e405 2757 lq_sta->active_mimo2_rate,
d8bafeaf 2758 lq_sta->is_vht, lq_sta->ldpc, lq_sta->stbc);
198266a3
ES
2759 IWL_DEBUG_RATE(mvm, "MAX RATE: LEGACY=%d SISO=%d MIMO2=%d\n",
2760 lq_sta->max_legacy_rate_idx,
2761 lq_sta->max_siso_rate_idx,
2762 lq_sta->max_mimo2_rate_idx);
8ca151b5
JB
2763
2764 /* These values will be overridden later */
2765 lq_sta->lq.single_stream_ant_msk =
4ed735e7 2766 first_antenna(mvm->fw->valid_tx_ant);
809bccfa 2767 lq_sta->lq.dual_stream_ant_msk = ANT_AB;
8ca151b5
JB
2768
2769 /* as default allow aggregation for all tids */
2770 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
8ca151b5 2771 lq_sta->is_agg = 0;
5fc0f76c
ES
2772#ifdef CONFIG_IWLWIFI_DEBUGFS
2773 iwl_mvm_reset_frame_stats(mvm, &mvm->drv_rx_stats);
2774#endif
b87c2179
ES
2775 rs_initialize_lq(mvm, sta, lq_sta, band, init);
2776}
2777
2778static void rs_rate_update(void *mvm_r,
2779 struct ieee80211_supported_band *sband,
2780 struct cfg80211_chan_def *chandef,
2781 struct ieee80211_sta *sta, void *priv_sta,
2782 u32 changed)
2783{
2784 u8 tid;
2785 struct iwl_op_mode *op_mode =
2786 (struct iwl_op_mode *)mvm_r;
2787 struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
2788
2789 /* Stop any ongoing aggregations as rs starts off assuming no agg */
2790 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++)
2791 ieee80211_stop_tx_ba_session(sta, tid);
2792
2793 iwl_mvm_rs_rate_init(mvm, sta, sband->band, false);
8ca151b5
JB
2794}
2795
8fc7c58c
ES
2796#ifdef CONFIG_MAC80211_DEBUGFS
2797static void rs_build_rates_table_from_fixed(struct iwl_mvm *mvm,
2798 struct iwl_lq_cmd *lq_cmd,
2799 enum ieee80211_band band,
2800 u32 ucode_rate)
2801{
2802 struct rs_rate rate;
2803 int i;
2804 int num_rates = ARRAY_SIZE(lq_cmd->rs_table);
2805 __le32 ucode_rate_le32 = cpu_to_le32(ucode_rate);
7ceb98a3 2806 u8 ant = (ucode_rate & RATE_MCS_ANT_ABC_MSK) >> RATE_MCS_ANT_POS;
8fc7c58c
ES
2807
2808 for (i = 0; i < num_rates; i++)
2809 lq_cmd->rs_table[i] = ucode_rate_le32;
2810
2811 rs_rate_from_ucode_rate(ucode_rate, band, &rate);
2812
2813 if (is_mimo(&rate))
2814 lq_cmd->mimo_delim = num_rates - 1;
2815 else
2816 lq_cmd->mimo_delim = 0;
7ceb98a3
EG
2817
2818 lq_cmd->reduced_tpc = 0;
2819
2820 if (num_of_ant(ant) == 1)
2821 lq_cmd->single_stream_ant_msk = ant;
2822
2823 lq_cmd->agg_frame_cnt_limit = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
8fc7c58c
ES
2824}
2825#endif /* CONFIG_MAC80211_DEBUGFS */
2826
e07be6d3
ES
2827static void rs_fill_rates_for_column(struct iwl_mvm *mvm,
2828 struct iwl_lq_sta *lq_sta,
2829 struct rs_rate *rate,
2830 __le32 *rs_table, int *rs_table_index,
2831 int num_rates, int num_retries,
2832 u8 valid_tx_ant, bool toggle_ant)
2833{
2834 int i, j;
2835 __le32 ucode_rate;
2836 bool bottom_reached = false;
2837 int prev_rate_idx = rate->index;
2838 int end = LINK_QUAL_MAX_RETRY_NUM;
2839 int index = *rs_table_index;
2840
2841 for (i = 0; i < num_rates && index < end; i++) {
2842 ucode_rate = cpu_to_le32(ucode_rate_from_rs_rate(mvm, rate));
2843 for (j = 0; j < num_retries && index < end; j++, index++)
2844 rs_table[index] = ucode_rate;
2845
2846 if (toggle_ant)
2847 rs_toggle_antenna(valid_tx_ant, rate);
2848
2849 prev_rate_idx = rate->index;
2850 bottom_reached = rs_get_lower_rate_in_column(lq_sta, rate);
2851 if (bottom_reached && !is_legacy(rate))
2852 break;
2853 }
2854
2855 if (!bottom_reached)
2856 rate->index = prev_rate_idx;
2857
2858 *rs_table_index = index;
2859}
2860
2861/* Building the rate table is non trivial. When we're in MIMO2/VHT/80Mhz/SGI
2862 * column the rate table should look like this:
2863 *
2864 * rate[0] 0x400D019 VHT | ANT: AB BW: 80Mhz MCS: 9 NSS: 2 SGI
2865 * rate[1] 0x400D019 VHT | ANT: AB BW: 80Mhz MCS: 9 NSS: 2 SGI
2866 * rate[2] 0x400D018 VHT | ANT: AB BW: 80Mhz MCS: 8 NSS: 2 SGI
2867 * rate[3] 0x400D018 VHT | ANT: AB BW: 80Mhz MCS: 8 NSS: 2 SGI
2868 * rate[4] 0x400D017 VHT | ANT: AB BW: 80Mhz MCS: 7 NSS: 2 SGI
2869 * rate[5] 0x400D017 VHT | ANT: AB BW: 80Mhz MCS: 7 NSS: 2 SGI
2870 * rate[6] 0x4005007 VHT | ANT: A BW: 80Mhz MCS: 7 NSS: 1 NGI
2871 * rate[7] 0x4009006 VHT | ANT: B BW: 80Mhz MCS: 6 NSS: 1 NGI
2872 * rate[8] 0x4005005 VHT | ANT: A BW: 80Mhz MCS: 5 NSS: 1 NGI
2873 * rate[9] 0x800B Legacy | ANT: B Rate: 36 Mbps
2874 * rate[10] 0x4009 Legacy | ANT: A Rate: 24 Mbps
2875 * rate[11] 0x8007 Legacy | ANT: B Rate: 18 Mbps
2876 * rate[12] 0x4005 Legacy | ANT: A Rate: 12 Mbps
2877 * rate[13] 0x800F Legacy | ANT: B Rate: 9 Mbps
2878 * rate[14] 0x400D Legacy | ANT: A Rate: 6 Mbps
2879 * rate[15] 0x800D Legacy | ANT: B Rate: 6 Mbps
2880 */
8fc7c58c 2881static void rs_build_rates_table(struct iwl_mvm *mvm,
d8bafeaf 2882 struct ieee80211_sta *sta,
8fc7c58c
ES
2883 struct iwl_lq_sta *lq_sta,
2884 const struct rs_rate *initial_rate)
8ca151b5 2885{
5aa33553 2886 struct rs_rate rate;
e07be6d3 2887 int num_rates, num_retries, index = 0;
8ca151b5
JB
2888 u8 valid_tx_ant = 0;
2889 struct iwl_lq_cmd *lq_cmd = &lq_sta->lq;
e07be6d3 2890 bool toggle_ant = false;
8ca151b5 2891
9e898f1b 2892 memcpy(&rate, initial_rate, sizeof(rate));
8fc7c58c 2893
4ed735e7 2894 valid_tx_ant = mvm->fw->valid_tx_ant;
d8bafeaf 2895 rate.stbc = rs_stbc_allow(mvm, sta, lq_sta);
8ca151b5 2896
e07be6d3
ES
2897 if (is_siso(&rate)) {
2898 num_rates = RS_INITIAL_SISO_NUM_RATES;
2899 num_retries = RS_HT_VHT_RETRIES_PER_RATE;
2900 } else if (is_mimo(&rate)) {
2901 num_rates = RS_INITIAL_MIMO_NUM_RATES;
2902 num_retries = RS_HT_VHT_RETRIES_PER_RATE;
8ca151b5 2903 } else {
e07be6d3
ES
2904 num_rates = RS_INITIAL_LEGACY_NUM_RATES;
2905 num_retries = RS_LEGACY_RETRIES_PER_RATE;
2906 toggle_ant = true;
8ca151b5
JB
2907 }
2908
e07be6d3
ES
2909 rs_fill_rates_for_column(mvm, lq_sta, &rate, lq_cmd->rs_table, &index,
2910 num_rates, num_retries, valid_tx_ant,
2911 toggle_ant);
8ca151b5 2912
e07be6d3 2913 rs_get_lower_rate_down_column(lq_sta, &rate);
8ca151b5 2914
e07be6d3
ES
2915 if (is_siso(&rate)) {
2916 num_rates = RS_SECONDARY_SISO_NUM_RATES;
2917 num_retries = RS_SECONDARY_SISO_RETRIES;
d9088f60 2918 lq_cmd->mimo_delim = index;
e07be6d3
ES
2919 } else if (is_legacy(&rate)) {
2920 num_rates = RS_SECONDARY_LEGACY_NUM_RATES;
2921 num_retries = RS_LEGACY_RETRIES_PER_RATE;
2922 } else {
2923 WARN_ON_ONCE(1);
2924 }
8ca151b5 2925
e07be6d3 2926 toggle_ant = true;
8ca151b5 2927
e07be6d3
ES
2928 rs_fill_rates_for_column(mvm, lq_sta, &rate, lq_cmd->rs_table, &index,
2929 num_rates, num_retries, valid_tx_ant,
2930 toggle_ant);
8ca151b5 2931
e07be6d3 2932 rs_get_lower_rate_down_column(lq_sta, &rate);
8ca151b5 2933
e07be6d3
ES
2934 num_rates = RS_SECONDARY_LEGACY_NUM_RATES;
2935 num_retries = RS_LEGACY_RETRIES_PER_RATE;
8ca151b5 2936
e07be6d3
ES
2937 rs_fill_rates_for_column(mvm, lq_sta, &rate, lq_cmd->rs_table, &index,
2938 num_rates, num_retries, valid_tx_ant,
2939 toggle_ant);
8ca151b5 2940
8fc7c58c
ES
2941}
2942
2943static void rs_fill_lq_cmd(struct iwl_mvm *mvm,
2944 struct ieee80211_sta *sta,
2945 struct iwl_lq_sta *lq_sta,
2946 const struct rs_rate *initial_rate)
2947{
2948 struct iwl_lq_cmd *lq_cmd = &lq_sta->lq;
7ceb98a3
EG
2949 struct iwl_mvm_sta *mvmsta;
2950 struct iwl_mvm_vif *mvmvif;
2951
2952 lq_cmd->agg_disable_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
2953 lq_cmd->agg_time_limit =
2954 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
8fc7c58c
ES
2955
2956#ifdef CONFIG_MAC80211_DEBUGFS
ae969afe 2957 if (lq_sta->pers.dbg_fixed_rate) {
8fc7c58c
ES
2958 rs_build_rates_table_from_fixed(mvm, lq_cmd,
2959 lq_sta->band,
ae969afe 2960 lq_sta->pers.dbg_fixed_rate);
7ceb98a3
EG
2961 return;
2962 }
8fc7c58c 2963#endif
7ceb98a3
EG
2964 if (WARN_ON_ONCE(!sta || !initial_rate))
2965 return;
8fc7c58c 2966
d8bafeaf 2967 rs_build_rates_table(mvm, sta, lq_sta, initial_rate);
8ca151b5 2968
7ceb98a3
EG
2969 if (num_of_ant(initial_rate->ant) == 1)
2970 lq_cmd->single_stream_ant_msk = initial_rate->ant;
8ca151b5 2971
7ceb98a3
EG
2972 mvmsta = iwl_mvm_sta_from_mac80211(sta);
2973 mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif);
e96d551f 2974
7ceb98a3
EG
2975 if (num_of_ant(initial_rate->ant) == 1)
2976 lq_cmd->single_stream_ant_msk = initial_rate->ant;
2977
2978 lq_cmd->agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
2979
2980 /*
2981 * In case of low latency, tell the firwmare to leave a frame in the
2982 * Tx Fifo so that it can start a transaction in the same TxOP. This
2983 * basically allows the firmware to send bursts.
2984 */
9ecd051e 2985 if (iwl_mvm_vif_low_latency(mvmvif)) {
7ceb98a3
EG
2986 lq_cmd->agg_frame_cnt_limit--;
2987
9ecd051e
ES
2988 if (mvm->low_latency_agg_frame_limit)
2989 lq_cmd->agg_frame_cnt_limit =
2990 min(lq_cmd->agg_frame_cnt_limit,
2991 mvm->low_latency_agg_frame_limit);
2992 }
2993
a4db848f
EG
2994 if (mvmsta->vif->p2p)
2995 lq_cmd->flags |= LQ_FLAG_USE_RTS_MSK;
2996
7ceb98a3 2997 lq_cmd->agg_time_limit =
5b7ff615 2998 cpu_to_le16(iwl_mvm_coex_agg_time_limit(mvm, sta));
8ca151b5
JB
2999}
3000
3001static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
3002{
3003 return hw->priv;
3004}
3005/* rate scale requires free function to be implemented */
3006static void rs_free(void *mvm_rate)
3007{
3008 return;
3009}
3010
3011static void rs_free_sta(void *mvm_r, struct ieee80211_sta *sta,
3012 void *mvm_sta)
3013{
3014 struct iwl_op_mode *op_mode __maybe_unused = mvm_r;
3015 struct iwl_mvm *mvm __maybe_unused = IWL_OP_MODE_GET_MVM(op_mode);
3016
3017 IWL_DEBUG_RATE(mvm, "enter\n");
3018 IWL_DEBUG_RATE(mvm, "leave\n");
3019}
3020
3021#ifdef CONFIG_MAC80211_DEBUGFS
5fc0f76c 3022int rs_pretty_print_rate(char *buf, const u32 rate)
5aa33553
ES
3023{
3024
1a61b347
ES
3025 char *type, *bw;
3026 u8 mcs = 0, nss = 0;
3027 u8 ant = (rate & RATE_MCS_ANT_ABC_MSK) >> RATE_MCS_ANT_POS;
3028
3029 if (!(rate & RATE_MCS_HT_MSK) &&
3030 !(rate & RATE_MCS_VHT_MSK)) {
3031 int index = iwl_hwrate_to_plcp_idx(rate);
3032
3033 return sprintf(buf, "Legacy | ANT: %s Rate: %s Mbps\n",
560843f4
ES
3034 rs_pretty_ant(ant),
3035 index == IWL_RATE_INVALID ? "BAD" :
3036 iwl_rate_mcs[index].mbps);
1a61b347
ES
3037 }
3038
3039 if (rate & RATE_MCS_VHT_MSK) {
3040 type = "VHT";
3041 mcs = rate & RATE_VHT_MCS_RATE_CODE_MSK;
3042 nss = ((rate & RATE_VHT_MCS_NSS_MSK)
3043 >> RATE_VHT_MCS_NSS_POS) + 1;
3044 } else if (rate & RATE_MCS_HT_MSK) {
3045 type = "HT";
3046 mcs = rate & RATE_HT_MCS_INDEX_MSK;
3047 } else {
3048 type = "Unknown"; /* shouldn't happen */
3049 }
3050
3051 switch (rate & RATE_MCS_CHAN_WIDTH_MSK) {
3052 case RATE_MCS_CHAN_WIDTH_20:
3053 bw = "20Mhz";
3054 break;
3055 case RATE_MCS_CHAN_WIDTH_40:
3056 bw = "40Mhz";
3057 break;
3058 case RATE_MCS_CHAN_WIDTH_80:
3059 bw = "80Mhz";
3060 break;
3061 case RATE_MCS_CHAN_WIDTH_160:
3062 bw = "160Mhz";
3063 break;
3064 default:
3065 bw = "BAD BW";
3066 }
3067
3068 return sprintf(buf, "%s | ANT: %s BW: %s MCS: %d NSS: %d %s%s%s%s%s\n",
5aa33553 3069 type, rs_pretty_ant(ant), bw, mcs, nss,
1a61b347 3070 (rate & RATE_MCS_SGI_MSK) ? "SGI " : "NGI ",
7b1dd048 3071 (rate & RATE_MCS_HT_STBC_MSK) ? "STBC " : "",
1a61b347
ES
3072 (rate & RATE_MCS_LDPC_MSK) ? "LDPC " : "",
3073 (rate & RATE_MCS_BF_MSK) ? "BF " : "",
3074 (rate & RATE_MCS_ZLF_MSK) ? "ZLF " : "");
3075}
3076
a0c38b21
ES
3077/**
3078 * Program the device to use fixed rate for frame transmit
3079 * This is for debugging/testing only
3080 * once the device start use fixed rate, we need to reload the module
3081 * to being back the normal operation.
3082 */
3083static void rs_program_fix_rate(struct iwl_mvm *mvm,
3084 struct iwl_lq_sta *lq_sta)
3085{
3086 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
3087 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
3088 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
3089
3090 IWL_DEBUG_RATE(mvm, "sta_id %d rate 0x%X\n",
ae969afe 3091 lq_sta->lq.sta_id, lq_sta->pers.dbg_fixed_rate);
a0c38b21 3092
ae969afe 3093 if (lq_sta->pers.dbg_fixed_rate) {
7ceb98a3 3094 rs_fill_lq_cmd(mvm, NULL, lq_sta, NULL);
ae969afe 3095 iwl_mvm_send_lq_cmd(lq_sta->pers.drv, &lq_sta->lq, false);
a0c38b21
ES
3096 }
3097}
3098
9d10849e
EG
3099static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
3100 const char __user *user_buf, size_t count, loff_t *ppos)
3101{
3102 struct iwl_lq_sta *lq_sta = file->private_data;
3103 struct iwl_mvm *mvm;
3104 char buf[64];
3105 size_t buf_size;
3106 u32 parsed_rate;
3107
ae969afe 3108 mvm = lq_sta->pers.drv;
9d10849e
EG
3109 memset(buf, 0, sizeof(buf));
3110 buf_size = min(count, sizeof(buf) - 1);
3111 if (copy_from_user(buf, user_buf, buf_size))
3112 return -EFAULT;
3113
3114 if (sscanf(buf, "%x", &parsed_rate) == 1)
ae969afe 3115 lq_sta->pers.dbg_fixed_rate = parsed_rate;
9d10849e 3116 else
ae969afe 3117 lq_sta->pers.dbg_fixed_rate = 0;
9d10849e
EG
3118
3119 rs_program_fix_rate(mvm, lq_sta);
3120
3121 return count;
3122}
3123
8ca151b5
JB
3124static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
3125 char __user *user_buf, size_t count, loff_t *ppos)
3126{
3127 char *buff;
3128 int desc = 0;
3129 int i = 0;
8ca151b5
JB
3130 ssize_t ret;
3131
3132 struct iwl_lq_sta *lq_sta = file->private_data;
3133 struct iwl_mvm *mvm;
3134 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
5aa33553 3135 struct rs_rate *rate = &tbl->rate;
ae969afe 3136 mvm = lq_sta->pers.drv;
1a61b347 3137 buff = kmalloc(2048, GFP_KERNEL);
8ca151b5
JB
3138 if (!buff)
3139 return -ENOMEM;
3140
3141 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
198266a3 3142 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%lX\n",
8ca151b5
JB
3143 lq_sta->total_failed, lq_sta->total_success,
3144 lq_sta->active_legacy_rate);
3145 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
ae969afe 3146 lq_sta->pers.dbg_fixed_rate);
8ca151b5 3147 desc += sprintf(buff+desc, "valid_tx_ant %s%s%s\n",
4ed735e7
JB
3148 (mvm->fw->valid_tx_ant & ANT_A) ? "ANT_A," : "",
3149 (mvm->fw->valid_tx_ant & ANT_B) ? "ANT_B," : "",
3150 (mvm->fw->valid_tx_ant & ANT_C) ? "ANT_C" : "");
8ca151b5 3151 desc += sprintf(buff+desc, "lq type %s\n",
5aa33553
ES
3152 (is_legacy(rate)) ? "legacy" :
3153 is_vht(rate) ? "VHT" : "HT");
3154 if (!is_legacy(rate)) {
8ca151b5 3155 desc += sprintf(buff+desc, " %s",
5aa33553 3156 (is_siso(rate)) ? "SISO" : "MIMO2");
8ca151b5 3157 desc += sprintf(buff+desc, " %s",
5aa33553
ES
3158 (is_ht20(rate)) ? "20MHz" :
3159 (is_ht40(rate)) ? "40MHz" :
3160 (is_ht80(rate)) ? "80Mhz" : "BAD BW");
a3576ff2 3161 desc += sprintf(buff+desc, " %s %s %s\n",
5aa33553 3162 (rate->sgi) ? "SGI" : "NGI",
a3576ff2 3163 (rate->ldpc) ? "LDPC" : "BCC",
6a524f48 3164 (lq_sta->is_agg) ? "AGG on" : "");
8ca151b5
JB
3165 }
3166 desc += sprintf(buff+desc, "last tx rate=0x%X\n",
3167 lq_sta->last_rate_n_flags);
3168 desc += sprintf(buff+desc,
1a61b347 3169 "general: flags=0x%X mimo-d=%d s-ant=0x%x d-ant=0x%x\n",
8ca151b5
JB
3170 lq_sta->lq.flags,
3171 lq_sta->lq.mimo_delim,
3172 lq_sta->lq.single_stream_ant_msk,
3173 lq_sta->lq.dual_stream_ant_msk);
3174
3175 desc += sprintf(buff+desc,
3176 "agg: time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
3177 le16_to_cpu(lq_sta->lq.agg_time_limit),
3178 lq_sta->lq.agg_disable_start_th,
3179 lq_sta->lq.agg_frame_cnt_limit);
3180
2fd647f8 3181 desc += sprintf(buff+desc, "reduced tpc=%d\n", lq_sta->lq.reduced_tpc);
8ca151b5
JB
3182 desc += sprintf(buff+desc,
3183 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
3184 lq_sta->lq.initial_rate_index[0],
3185 lq_sta->lq.initial_rate_index[1],
3186 lq_sta->lq.initial_rate_index[2],
3187 lq_sta->lq.initial_rate_index[3]);
3188
3189 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
cf4ef654 3190 u32 r = le32_to_cpu(lq_sta->lq.rs_table[i]);
1a61b347 3191
cf4ef654
JB
3192 desc += sprintf(buff+desc, " rate[%d] 0x%X ", i, r);
3193 desc += rs_pretty_print_rate(buff+desc, r);
8ca151b5
JB
3194 }
3195
3196 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3197 kfree(buff);
3198 return ret;
3199}
3200
3201static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
3202 .write = rs_sta_dbgfs_scale_table_write,
3203 .read = rs_sta_dbgfs_scale_table_read,
3204 .open = simple_open,
3205 .llseek = default_llseek,
3206};
3207static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
3208 char __user *user_buf, size_t count, loff_t *ppos)
3209{
3210 char *buff;
3211 int desc = 0;
3212 int i, j;
3213 ssize_t ret;
d310e405 3214 struct iwl_scale_tbl_info *tbl;
5aa33553 3215 struct rs_rate *rate;
8ca151b5
JB
3216 struct iwl_lq_sta *lq_sta = file->private_data;
3217
3218 buff = kmalloc(1024, GFP_KERNEL);
3219 if (!buff)
3220 return -ENOMEM;
3221
3222 for (i = 0; i < LQ_SIZE; i++) {
d310e405 3223 tbl = &(lq_sta->lq_info[i]);
5aa33553 3224 rate = &tbl->rate;
8ca151b5 3225 desc += sprintf(buff+desc,
6a524f48 3226 "%s type=%d SGI=%d BW=%s DUP=0\n"
8fc7c58c 3227 "index=%d\n",
8ca151b5 3228 lq_sta->active_tbl == i ? "*" : "x",
5aa33553
ES
3229 rate->type,
3230 rate->sgi,
3231 is_ht20(rate) ? "20Mhz" :
3232 is_ht40(rate) ? "40Mhz" :
3233 is_ht80(rate) ? "80Mhz" : "ERR",
8fc7c58c 3234 rate->index);
8ca151b5
JB
3235 for (j = 0; j < IWL_RATE_COUNT; j++) {
3236 desc += sprintf(buff+desc,
3237 "counter=%d success=%d %%=%d\n",
d310e405
ES
3238 tbl->win[j].counter,
3239 tbl->win[j].success_counter,
3240 tbl->win[j].success_ratio);
8ca151b5
JB
3241 }
3242 }
3243 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3244 kfree(buff);
3245 return ret;
3246}
3247
3248static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
3249 .read = rs_sta_dbgfs_stats_table_read,
3250 .open = simple_open,
3251 .llseek = default_llseek,
3252};
3253
757cf23b
ES
3254static ssize_t rs_sta_dbgfs_drv_tx_stats_read(struct file *file,
3255 char __user *user_buf,
3256 size_t count, loff_t *ppos)
3257{
3258 static const char * const column_name[] = {
3259 [RS_COLUMN_LEGACY_ANT_A] = "LEGACY_ANT_A",
3260 [RS_COLUMN_LEGACY_ANT_B] = "LEGACY_ANT_B",
3261 [RS_COLUMN_SISO_ANT_A] = "SISO_ANT_A",
3262 [RS_COLUMN_SISO_ANT_B] = "SISO_ANT_B",
3263 [RS_COLUMN_SISO_ANT_A_SGI] = "SISO_ANT_A_SGI",
3264 [RS_COLUMN_SISO_ANT_B_SGI] = "SISO_ANT_B_SGI",
3265 [RS_COLUMN_MIMO2] = "MIMO2",
3266 [RS_COLUMN_MIMO2_SGI] = "MIMO2_SGI",
3267 };
3268
3269 static const char * const rate_name[] = {
3270 [IWL_RATE_1M_INDEX] = "1M",
3271 [IWL_RATE_2M_INDEX] = "2M",
3272 [IWL_RATE_5M_INDEX] = "5.5M",
3273 [IWL_RATE_11M_INDEX] = "11M",
3274 [IWL_RATE_6M_INDEX] = "6M|MCS0",
3275 [IWL_RATE_9M_INDEX] = "9M",
3276 [IWL_RATE_12M_INDEX] = "12M|MCS1",
3277 [IWL_RATE_18M_INDEX] = "18M|MCS2",
3278 [IWL_RATE_24M_INDEX] = "24M|MCS3",
3279 [IWL_RATE_36M_INDEX] = "36M|MCS4",
3280 [IWL_RATE_48M_INDEX] = "48M|MCS5",
3281 [IWL_RATE_54M_INDEX] = "54M|MCS6",
3282 [IWL_RATE_MCS_7_INDEX] = "MCS7",
3283 [IWL_RATE_MCS_8_INDEX] = "MCS8",
3284 [IWL_RATE_MCS_9_INDEX] = "MCS9",
3285 };
3286
3287 char *buff, *pos, *endpos;
3288 int col, rate;
3289 ssize_t ret;
3290 struct iwl_lq_sta *lq_sta = file->private_data;
3291 struct rs_rate_stats *stats;
3292 static const size_t bufsz = 1024;
3293
3294 buff = kmalloc(bufsz, GFP_KERNEL);
3295 if (!buff)
3296 return -ENOMEM;
3297
3298 pos = buff;
3299 endpos = pos + bufsz;
3300
3301 pos += scnprintf(pos, endpos - pos, "COLUMN,");
3302 for (rate = 0; rate < IWL_RATE_COUNT; rate++)
3303 pos += scnprintf(pos, endpos - pos, "%s,", rate_name[rate]);
3304 pos += scnprintf(pos, endpos - pos, "\n");
3305
3306 for (col = 0; col < RS_COLUMN_COUNT; col++) {
3307 pos += scnprintf(pos, endpos - pos,
3308 "%s,", column_name[col]);
3309
3310 for (rate = 0; rate < IWL_RATE_COUNT; rate++) {
f58220f6 3311 stats = &(lq_sta->pers.tx_stats[col][rate]);
757cf23b
ES
3312 pos += scnprintf(pos, endpos - pos,
3313 "%llu/%llu,",
3314 stats->success,
3315 stats->total);
3316 }
3317 pos += scnprintf(pos, endpos - pos, "\n");
3318 }
3319
3320 ret = simple_read_from_buffer(user_buf, count, ppos, buff, pos - buff);
3321 kfree(buff);
3322 return ret;
3323}
3324
3325static ssize_t rs_sta_dbgfs_drv_tx_stats_write(struct file *file,
3326 const char __user *user_buf,
3327 size_t count, loff_t *ppos)
3328{
3329 struct iwl_lq_sta *lq_sta = file->private_data;
f58220f6 3330 memset(lq_sta->pers.tx_stats, 0, sizeof(lq_sta->pers.tx_stats));
757cf23b
ES
3331
3332 return count;
3333}
3334
3335static const struct file_operations rs_sta_dbgfs_drv_tx_stats_ops = {
3336 .read = rs_sta_dbgfs_drv_tx_stats_read,
3337 .write = rs_sta_dbgfs_drv_tx_stats_write,
3338 .open = simple_open,
3339 .llseek = default_llseek,
3340};
3341
8ca151b5
JB
3342static void rs_add_debugfs(void *mvm, void *mvm_sta, struct dentry *dir)
3343{
3344 struct iwl_lq_sta *lq_sta = mvm_sta;
c6e1faad
EP
3345 debugfs_create_file("rate_scale_table", S_IRUSR | S_IWUSR, dir,
3346 lq_sta, &rs_sta_dbgfs_scale_table_ops);
3347 debugfs_create_file("rate_stats_table", S_IRUSR, dir,
3348 lq_sta, &rs_sta_dbgfs_stats_table_ops);
3349 debugfs_create_file("drv_tx_stats", S_IRUSR | S_IWUSR, dir,
3350 lq_sta, &rs_sta_dbgfs_drv_tx_stats_ops);
3351 debugfs_create_u8("tx_agg_tid_enable", S_IRUSR | S_IWUSR, dir,
3352 &lq_sta->tx_agg_tid_en);
3353 debugfs_create_u8("reduced_tpc", S_IRUSR | S_IWUSR, dir,
3354 &lq_sta->pers.dbg_fixed_txp_reduction);
8ca151b5
JB
3355}
3356
3357static void rs_remove_debugfs(void *mvm, void *mvm_sta)
3358{
8ca151b5
JB
3359}
3360#endif
3361
3362/*
3363 * Initialization of rate scaling information is done by driver after
3364 * the station is added. Since mac80211 calls this function before a
3365 * station is added we ignore it.
3366 */
3367static void rs_rate_init_stub(void *mvm_r,
3de805cf
SW
3368 struct ieee80211_supported_band *sband,
3369 struct cfg80211_chan_def *chandef,
3370 struct ieee80211_sta *sta, void *mvm_sta)
8ca151b5
JB
3371{
3372}
631ad703
JB
3373
3374static const struct rate_control_ops rs_mvm_ops = {
8ca151b5 3375 .name = RS_NAME,
1ebc8f2e 3376 .tx_status = rs_mac80211_tx_status,
8ca151b5
JB
3377 .get_rate = rs_get_rate,
3378 .rate_init = rs_rate_init_stub,
3379 .alloc = rs_alloc,
3380 .free = rs_free,
3381 .alloc_sta = rs_alloc_sta,
3382 .free_sta = rs_free_sta,
b87c2179 3383 .rate_update = rs_rate_update,
8ca151b5
JB
3384#ifdef CONFIG_MAC80211_DEBUGFS
3385 .add_sta_debugfs = rs_add_debugfs,
3386 .remove_sta_debugfs = rs_remove_debugfs,
3387#endif
3388};
3389
3390int iwl_mvm_rate_control_register(void)
3391{
3392 return ieee80211_rate_control_register(&rs_mvm_ops);
3393}
3394
3395void iwl_mvm_rate_control_unregister(void)
3396{
3397 ieee80211_rate_control_unregister(&rs_mvm_ops);
3398}
9ee718aa
EL
3399
3400/**
3401 * iwl_mvm_tx_protection - Gets LQ command, change it to enable/disable
3402 * Tx protection, according to this rquest and previous requests,
3403 * and send the LQ command.
9ee718aa
EL
3404 * @mvmsta: The station
3405 * @enable: Enable Tx protection?
3406 */
e126b5d9
JB
3407int iwl_mvm_tx_protection(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
3408 bool enable)
9ee718aa 3409{
e126b5d9
JB
3410 struct iwl_lq_cmd *lq = &mvmsta->lq_sta.lq;
3411
9ee718aa
EL
3412 lockdep_assert_held(&mvm->mutex);
3413
3414 if (enable) {
3415 if (mvmsta->tx_protection == 0)
d307ec8d 3416 lq->flags |= LQ_FLAG_USE_RTS_MSK;
9ee718aa
EL
3417 mvmsta->tx_protection++;
3418 } else {
3419 mvmsta->tx_protection--;
3420 if (mvmsta->tx_protection == 0)
d307ec8d 3421 lq->flags &= ~LQ_FLAG_USE_RTS_MSK;
9ee718aa
EL
3422 }
3423
9e680946 3424 return iwl_mvm_send_lq_cmd(mvm, lq, false);
9ee718aa 3425}