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