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