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