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