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