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