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