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