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