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