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