]> git.ipfire.org Git - thirdparty/linux.git/blame - drivers/net/wireless/intel/iwlwifi/dvm/rs.c
Merge tag 'drm/tegra/for-5.7-fixes' of git://anongit.freedesktop.org/tegra/linux...
[thirdparty/linux.git] / drivers / net / wireless / intel / iwlwifi / dvm / rs.c
CommitLineData
4273a380 1// SPDX-License-Identifier: GPL-2.0-only
b481de9c
ZY
2/******************************************************************************
3 *
51368bf7 4 * Copyright(c) 2005 - 2014 Intel Corporation. All rights reserved.
b481de9c 5 *
b481de9c 6 * Contact Information:
cb2f8277 7 * Intel Linux Wireless <linuxwifi@intel.com>
b481de9c
ZY
8 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
9 *
10 *****************************************************************************/
11#include <linux/kernel.h>
b481de9c 12#include <linux/skbuff.h>
5a0e3ad6 13#include <linux/slab.h>
b481de9c 14#include <net/mac80211.h>
b481de9c
ZY
15
16#include <linux/netdevice.h>
17#include <linux/etherdevice.h>
18#include <linux/delay.h>
19
20#include <linux/workqueue.h>
21
1023fdc4
JB
22#include "dev.h"
23#include "agn.h"
b481de9c 24
e227ceac 25#define RS_NAME "iwl-agn-rs"
b481de9c 26
aade00ce 27#define NUM_TRY_BEFORE_ANT_TOGGLE 1
b481de9c
ZY
28#define IWL_NUMBER_TRY 1
29#define IWL_HT_NUMBER_TRY 3
30
77626355
BC
31#define IWL_RATE_MAX_WINDOW 62 /* # tx in history window */
32#define IWL_RATE_MIN_FAILURE_TH 6 /* min failures to calc tpt */
33#define IWL_RATE_MIN_SUCCESS_TH 8 /* min successes to calc tpt */
34
7d049e5a
AM
35/* max allowed rate miss before sync LQ cmd */
36#define IWL_MISSED_RATE_MAX 15
77626355 37/* max time to accum history 2 seconds */
447fee70 38#define IWL_RATE_SCALE_FLUSH_INTVL (3*HZ)
b481de9c
ZY
39
40static u8 rs_ht_to_legacy[] = {
41 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
42 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
43 IWL_RATE_6M_INDEX,
44 IWL_RATE_6M_INDEX, IWL_RATE_9M_INDEX,
45 IWL_RATE_12M_INDEX, IWL_RATE_18M_INDEX,
46 IWL_RATE_24M_INDEX, IWL_RATE_36M_INDEX,
47 IWL_RATE_48M_INDEX, IWL_RATE_54M_INDEX
48};
49
aade00ce
GC
50static const u8 ant_toggle_lookup[] = {
51 /*ANT_NONE -> */ ANT_NONE,
52 /*ANT_A -> */ ANT_B,
53 /*ANT_B -> */ ANT_C,
54 /*ANT_AB -> */ ANT_BC,
55 /*ANT_C -> */ ANT_A,
56 /*ANT_AC -> */ ANT_AB,
57 /*ANT_BC -> */ ANT_AC,
58 /*ANT_ABC -> */ ANT_ABC,
59};
60
635b85b4
JB
61#define IWL_DECLARE_RATE_INFO(r, s, ip, in, rp, rn, pp, np) \
62 [IWL_RATE_##r##M_INDEX] = { IWL_RATE_##r##M_PLCP, \
63 IWL_RATE_SISO_##s##M_PLCP, \
64 IWL_RATE_MIMO2_##s##M_PLCP,\
65 IWL_RATE_MIMO3_##s##M_PLCP,\
66 IWL_RATE_##r##M_IEEE, \
67 IWL_RATE_##ip##M_INDEX, \
68 IWL_RATE_##in##M_INDEX, \
69 IWL_RATE_##rp##M_INDEX, \
70 IWL_RATE_##rn##M_INDEX, \
71 IWL_RATE_##pp##M_INDEX, \
72 IWL_RATE_##np##M_INDEX }
73
74/*
75 * Parameter order:
76 * rate, ht rate, prev rate, next rate, prev tgg rate, next tgg rate
77 *
78 * If there isn't a valid next or previous rate then INV is used which
79 * maps to IWL_RATE_INVALID
80 *
81 */
82const struct iwl_rate_info iwl_rates[IWL_RATE_COUNT] = {
83 IWL_DECLARE_RATE_INFO(1, INV, INV, 2, INV, 2, INV, 2), /* 1mbps */
84 IWL_DECLARE_RATE_INFO(2, INV, 1, 5, 1, 5, 1, 5), /* 2mbps */
85 IWL_DECLARE_RATE_INFO(5, INV, 2, 6, 2, 11, 2, 11), /*5.5mbps */
86 IWL_DECLARE_RATE_INFO(11, INV, 9, 12, 9, 12, 5, 18), /* 11mbps */
87 IWL_DECLARE_RATE_INFO(6, 6, 5, 9, 5, 11, 5, 11), /* 6mbps */
88 IWL_DECLARE_RATE_INFO(9, 6, 6, 11, 6, 11, 5, 11), /* 9mbps */
89 IWL_DECLARE_RATE_INFO(12, 12, 11, 18, 11, 18, 11, 18), /* 12mbps */
90 IWL_DECLARE_RATE_INFO(18, 18, 12, 24, 12, 24, 11, 24), /* 18mbps */
91 IWL_DECLARE_RATE_INFO(24, 24, 18, 36, 18, 36, 18, 36), /* 24mbps */
92 IWL_DECLARE_RATE_INFO(36, 36, 24, 48, 24, 48, 24, 48), /* 36mbps */
93 IWL_DECLARE_RATE_INFO(48, 48, 36, 54, 36, 54, 36, 54), /* 48mbps */
94 IWL_DECLARE_RATE_INFO(54, 54, 48, INV, 48, INV, 48, INV),/* 54mbps */
95 IWL_DECLARE_RATE_INFO(60, 60, 48, INV, 48, INV, 48, INV),/* 60mbps */
96 /* FIXME:RS: ^^ should be INV (legacy) */
97};
98
2520546a
DH
99static inline u8 rs_extract_rate(u32 rate_n_flags)
100{
101 return (u8)(rate_n_flags & RATE_MCS_RATE_MSK);
102}
103
635b85b4
JB
104static int iwl_hwrate_to_plcp_idx(u32 rate_n_flags)
105{
106 int idx = 0;
107
108 /* HT rate format */
109 if (rate_n_flags & RATE_MCS_HT_MSK) {
2520546a 110 idx = rs_extract_rate(rate_n_flags);
635b85b4
JB
111
112 if (idx >= IWL_RATE_MIMO3_6M_PLCP)
113 idx = idx - IWL_RATE_MIMO3_6M_PLCP;
114 else if (idx >= IWL_RATE_MIMO2_6M_PLCP)
115 idx = idx - IWL_RATE_MIMO2_6M_PLCP;
116
117 idx += IWL_FIRST_OFDM_RATE;
118 /* skip 9M not supported in ht*/
119 if (idx >= IWL_RATE_9M_INDEX)
120 idx += 1;
121 if ((idx >= IWL_FIRST_OFDM_RATE) && (idx <= IWL_LAST_OFDM_RATE))
122 return idx;
123
124 /* legacy rate format, search for match in table */
125 } else {
126 for (idx = 0; idx < ARRAY_SIZE(iwl_rates); idx++)
2520546a
DH
127 if (iwl_rates[idx].plcp ==
128 rs_extract_rate(rate_n_flags))
635b85b4
JB
129 return idx;
130 }
131
132 return -1;
133}
134
c79dd5b5 135static void rs_rate_scale_perform(struct iwl_priv *priv,
514d65c1 136 struct sk_buff *skb,
4b7679a5
JB
137 struct ieee80211_sta *sta,
138 struct iwl_lq_sta *lq_sta);
46f9381a 139static void rs_fill_link_cmd(struct iwl_priv *priv,
e227ceac 140 struct iwl_lq_sta *lq_sta, u32 rate_n_flags);
da5dbb97 141static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search);
b481de9c
ZY
142
143
98d7e09a 144#ifdef CONFIG_MAC80211_DEBUGFS
e227ceac
TW
145static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
146 u32 *rate_n_flags, int index);
98d7e09a 147#else
e227ceac
TW
148static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
149 u32 *rate_n_flags, int index)
98d7e09a
ZY
150{}
151#endif
77626355 152
e3949d62
DH
153/**
154 * The following tables contain the expected throughput metrics for all rates
155 *
156 * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
157 *
158 * where invalid entries are zeros.
159 *
160 * CCK rates are only valid in legacy table and will only be used in G
161 * (2.4 GHz) band.
77626355 162 */
584a0f00 163
a34529e8 164static const u16 expected_tpt_legacy[IWL_RATE_COUNT] = {
e3949d62 165 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 0
b481de9c
ZY
166};
167
a34529e8 168static const u16 expected_tpt_siso20MHz[4][IWL_RATE_COUNT] = {
7fc11e9b
WYG
169 {0, 0, 0, 0, 42, 0, 76, 102, 124, 159, 183, 193, 202}, /* Norm */
170 {0, 0, 0, 0, 46, 0, 82, 110, 132, 168, 192, 202, 210}, /* SGI */
171 {0, 0, 0, 0, 47, 0, 91, 133, 171, 242, 305, 334, 362}, /* AGG */
172 {0, 0, 0, 0, 52, 0, 101, 145, 187, 264, 330, 361, 390}, /* AGG+SGI */
b481de9c
ZY
173};
174
a34529e8 175static const u16 expected_tpt_siso40MHz[4][IWL_RATE_COUNT] = {
e3949d62
DH
176 {0, 0, 0, 0, 77, 0, 127, 160, 184, 220, 242, 250, 257}, /* Norm */
177 {0, 0, 0, 0, 83, 0, 135, 169, 193, 229, 250, 257, 264}, /* SGI */
7fc11e9b
WYG
178 {0, 0, 0, 0, 94, 0, 177, 249, 313, 423, 512, 550, 586}, /* AGG */
179 {0, 0, 0, 0, 104, 0, 193, 270, 338, 454, 545, 584, 620}, /* AGG+SGI */
b481de9c
ZY
180};
181
a34529e8 182static const u16 expected_tpt_mimo2_20MHz[4][IWL_RATE_COUNT] = {
7fc11e9b
WYG
183 {0, 0, 0, 0, 74, 0, 123, 155, 179, 214, 236, 244, 251}, /* Norm */
184 {0, 0, 0, 0, 81, 0, 131, 164, 188, 223, 243, 251, 257}, /* SGI */
185 {0, 0, 0, 0, 89, 0, 167, 235, 296, 402, 488, 526, 560}, /* AGG */
186 {0, 0, 0, 0, 97, 0, 182, 255, 320, 431, 520, 558, 593}, /* AGG+SGI*/
b481de9c
ZY
187};
188
a34529e8 189static const u16 expected_tpt_mimo2_40MHz[4][IWL_RATE_COUNT] = {
e3949d62
DH
190 {0, 0, 0, 0, 123, 0, 182, 214, 235, 264, 279, 285, 289}, /* Norm */
191 {0, 0, 0, 0, 131, 0, 191, 222, 242, 270, 284, 289, 293}, /* SGI */
7fc11e9b
WYG
192 {0, 0, 0, 0, 171, 0, 305, 410, 496, 634, 731, 771, 805}, /* AGG */
193 {0, 0, 0, 0, 186, 0, 329, 439, 527, 667, 764, 803, 838}, /* AGG+SGI */
b481de9c
ZY
194};
195
a34529e8 196static const u16 expected_tpt_mimo3_20MHz[4][IWL_RATE_COUNT] = {
e3949d62
DH
197 {0, 0, 0, 0, 99, 0, 153, 186, 208, 239, 256, 263, 268}, /* Norm */
198 {0, 0, 0, 0, 106, 0, 162, 194, 215, 246, 262, 268, 273}, /* SGI */
199 {0, 0, 0, 0, 134, 0, 249, 346, 431, 574, 685, 732, 775}, /* AGG */
200 {0, 0, 0, 0, 148, 0, 272, 376, 465, 614, 727, 775, 818}, /* AGG+SGI */
584a0f00
WYG
201};
202
a34529e8 203static const u16 expected_tpt_mimo3_40MHz[4][IWL_RATE_COUNT] = {
e3949d62
DH
204 {0, 0, 0, 0, 152, 0, 211, 239, 255, 279, 290, 294, 297}, /* Norm */
205 {0, 0, 0, 0, 160, 0, 219, 245, 261, 284, 294, 297, 300}, /* SGI */
206 {0, 0, 0, 0, 254, 0, 443, 584, 695, 868, 984, 1030, 1070}, /* AGG */
207 {0, 0, 0, 0, 277, 0, 478, 624, 737, 911, 1026, 1070, 1109}, /* AGG+SGI */
584a0f00
WYG
208};
209
12b96817 210/* mbps, mcs */
79496738 211static const struct iwl_rate_mcs_info iwl_rate_mcs[IWL_RATE_COUNT] = {
e3949d62
DH
212 { "1", "BPSK DSSS"},
213 { "2", "QPSK DSSS"},
214 {"5.5", "BPSK CCK"},
215 { "11", "QPSK CCK"},
216 { "6", "BPSK 1/2"},
217 { "9", "BPSK 1/2"},
218 { "12", "QPSK 1/2"},
219 { "18", "QPSK 3/4"},
220 { "24", "16QAM 1/2"},
221 { "36", "16QAM 3/4"},
222 { "48", "64QAM 2/3"},
223 { "54", "64QAM 3/4"},
224 { "60", "64QAM 5/6"},
12b96817
WYG
225};
226
a9c146b3
WYG
227#define MCS_INDEX_PER_STREAM (8)
228
e227ceac 229static void rs_rate_scale_clear_window(struct iwl_rate_scale_data *window)
b481de9c
ZY
230{
231 window->data = 0;
232 window->success_counter = 0;
233 window->success_ratio = IWL_INVALID_VALUE;
234 window->counter = 0;
235 window->average_tpt = IWL_INVALID_VALUE;
236 window->stamp = 0;
b481de9c
ZY
237}
238
aade00ce
GC
239static inline u8 rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
240{
3ac7f146 241 return (ant_type & valid_antenna) == ant_type;
aade00ce
GC
242}
243
0c11b4de
RR
244/*
245 * removes the old data from the statistics. All data that is older than
246 * TID_MAX_TIME_DIFF, will be deleted.
247 */
e227ceac 248static void rs_tl_rm_old_stats(struct iwl_traffic_load *tl, u32 curr_time)
0c11b4de
RR
249{
250 /* The oldest age we want to keep */
251 u32 oldest_time = curr_time - TID_MAX_TIME_DIFF;
252
253 while (tl->queue_count &&
254 (tl->time_stamp < oldest_time)) {
255 tl->total -= tl->packet_count[tl->head];
256 tl->packet_count[tl->head] = 0;
257 tl->time_stamp += TID_QUEUE_CELL_SPACING;
258 tl->queue_count--;
259 tl->head++;
260 if (tl->head >= TID_QUEUE_MAX_SIZE)
261 tl->head = 0;
262 }
263}
264
265/*
266 * increment traffic load value for tid and also remove
267 * any old values if passed the certain time period
268 */
e227ceac 269static u8 rs_tl_add_packet(struct iwl_lq_sta *lq_data,
faa29718 270 struct ieee80211_hdr *hdr)
0c11b4de
RR
271{
272 u32 curr_time = jiffies_to_msecs(jiffies);
273 u32 time_diff;
274 s32 index;
e227ceac 275 struct iwl_traffic_load *tl = NULL;
54dbb525 276 u8 tid;
0c11b4de 277
417f114b 278 if (ieee80211_is_data_qos(hdr->frame_control)) {
fd7c8a40 279 u8 *qc = ieee80211_get_qos_ctl(hdr);
54dbb525
TW
280 tid = qc[0] & 0xf;
281 } else
5f85a789 282 return IWL_MAX_TID_COUNT;
0c11b4de 283
a8448553 284 if (unlikely(tid >= IWL_MAX_TID_COUNT))
5f85a789 285 return IWL_MAX_TID_COUNT;
e6a6cf4c 286
0c11b4de
RR
287 tl = &lq_data->load[tid];
288
289 curr_time -= curr_time % TID_ROUND_VALUE;
290
291 /* Happens only for the first packet. Initialize the data */
292 if (!(tl->queue_count)) {
293 tl->total = 1;
294 tl->time_stamp = curr_time;
295 tl->queue_count = 1;
296 tl->head = 0;
297 tl->packet_count[0] = 1;
5f85a789 298 return IWL_MAX_TID_COUNT;
0c11b4de
RR
299 }
300
301 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
302 index = time_diff / TID_QUEUE_CELL_SPACING;
303
304 /* The history is too long: remove data that is older than */
305 /* TID_MAX_TIME_DIFF */
306 if (index >= TID_QUEUE_MAX_SIZE)
307 rs_tl_rm_old_stats(tl, curr_time);
308
309 index = (tl->head + index) % TID_QUEUE_MAX_SIZE;
310 tl->packet_count[index] = tl->packet_count[index] + 1;
311 tl->total = tl->total + 1;
312
313 if ((index + 1) > tl->queue_count)
314 tl->queue_count = index + 1;
faa29718
RR
315
316 return tid;
0c11b4de
RR
317}
318
54a430c0 319#ifdef CONFIG_MAC80211_DEBUGFS
4e308119
WYG
320/**
321 * Program the device to use fixed rate for frame transmit
322 * This is for debugging/testing only
323 * once the device start use fixed rate, we need to reload the module
324 * to being back the normal operation.
325 */
6489854b
WYG
326static void rs_program_fix_rate(struct iwl_priv *priv,
327 struct iwl_lq_sta *lq_sta)
328{
329 struct iwl_station_priv *sta_priv =
330 container_of(lq_sta, struct iwl_station_priv, lq_sta);
c6892906 331 struct iwl_rxon_context *ctx = sta_priv->ctx;
6489854b
WYG
332
333 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
334 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
335 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
336 lq_sta->active_mimo3_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
337
6489854b 338 IWL_DEBUG_RATE(priv, "sta_id %d rate 0x%X\n",
4e308119 339 lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
6489854b 340
4e308119
WYG
341 if (lq_sta->dbg_fixed_rate) {
342 rs_fill_link_cmd(NULL, lq_sta, lq_sta->dbg_fixed_rate);
6489854b
WYG
343 iwl_send_lq_cmd(lq_sta->drv, ctx, &lq_sta->lq, CMD_ASYNC,
344 false);
345 }
346}
347#endif
348
0c11b4de
RR
349/*
350 get the traffic load value for tid
351*/
ad04e088 352static void rs_tl_get_load(struct iwl_lq_sta *lq_data, u8 tid)
0c11b4de
RR
353{
354 u32 curr_time = jiffies_to_msecs(jiffies);
355 u32 time_diff;
356 s32 index;
e227ceac 357 struct iwl_traffic_load *tl = NULL;
0c11b4de 358
a8448553 359 if (tid >= IWL_MAX_TID_COUNT)
ad04e088 360 return;
0c11b4de
RR
361
362 tl = &(lq_data->load[tid]);
363
364 curr_time -= curr_time % TID_ROUND_VALUE;
365
366 if (!(tl->queue_count))
ad04e088 367 return;
0c11b4de
RR
368
369 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
370 index = time_diff / TID_QUEUE_CELL_SPACING;
371
372 /* The history is too long: remove data that is older than */
373 /* TID_MAX_TIME_DIFF */
374 if (index >= TID_QUEUE_MAX_SIZE)
375 rs_tl_rm_old_stats(tl, curr_time);
0c11b4de
RR
376}
377
82ca9341 378static int rs_tl_turn_on_agg_for_tid(struct iwl_priv *priv,
e227ceac 379 struct iwl_lq_sta *lq_data, u8 tid,
4b7679a5 380 struct ieee80211_sta *sta)
0c11b4de 381{
82ca9341 382 int ret = -EAGAIN;
290f599c
JB
383
384 /*
385 * Don't create TX aggregation sessions when in high
386 * BT traffic, as they would just be disrupted by BT.
387 */
388 if (priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH) {
6690c01d
JB
389 IWL_DEBUG_COEX(priv,
390 "BT traffic (%d), no aggregation allowed\n",
391 priv->bt_traffic_load);
290f599c
JB
392 return ret;
393 }
394
ad04e088 395 rs_tl_get_load(lq_data, tid);
45d42700 396
44cc429c
EG
397 IWL_DEBUG_HT(priv, "Starting Tx agg: STA: %pM tid: %d\n",
398 sta->addr, tid);
399 ret = ieee80211_start_tx_ba_session(sta, tid, 5000);
400 if (ret == -EAGAIN) {
401 /*
402 * driver and mac80211 is out of sync
403 * this might be cause by reloading firmware
404 * stop the tx ba session here
405 */
406 IWL_ERR(priv, "Fail start Tx agg on tid: %d\n",
407 tid);
408 ieee80211_stop_tx_ba_session(sta, tid);
2411054a 409 }
82ca9341 410 return ret;
0c11b4de
RR
411}
412
c79dd5b5 413static void rs_tl_turn_on_agg(struct iwl_priv *priv, u8 tid,
e227ceac 414 struct iwl_lq_sta *lq_data,
4b7679a5 415 struct ieee80211_sta *sta)
0c11b4de 416{
a8448553 417 if (tid < IWL_MAX_TID_COUNT)
cfecc6b4
WYG
418 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
419 else
a8448553
JB
420 IWL_ERR(priv, "tid exceeds max TID count: %d/%d\n",
421 tid, IWL_MAX_TID_COUNT);
0c11b4de
RR
422}
423
39e88504 424static inline int get_num_of_ant_from_rate(u32 rate_n_flags)
fde0db31 425{
3ac7f146
TW
426 return !!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
427 !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
428 !!(rate_n_flags & RATE_MCS_ANT_C_MSK);
fde0db31
GC
429}
430
04f2dec1
SZ
431/*
432 * Static function to get the expected throughput from an iwl_scale_tbl_info
433 * that wraps a NULL pointer check
434 */
435static s32 get_expected_tpt(struct iwl_scale_tbl_info *tbl, int rs_index)
436{
437 if (tbl->expected_tpt)
438 return tbl->expected_tpt[rs_index];
439 return 0;
440}
441
77626355
BC
442/**
443 * rs_collect_tx_data - Update the success/failure sliding window
444 *
445 * We keep a sliding window of the last 62 packets transmitted
446 * at this rate. window->data contains the bitmask of successful
447 * packets.
448 */
04f2dec1
SZ
449static int rs_collect_tx_data(struct iwl_scale_tbl_info *tbl,
450 int scale_index, int attempts, int successes)
b481de9c 451{
e227ceac 452 struct iwl_rate_scale_data *window = NULL;
39e88504 453 static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
04f2dec1 454 s32 fail_count, tpt;
b481de9c 455
dc2453ae
TW
456 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
457 return -EINVAL;
b481de9c 458
e3949d62 459 /* Select window for current tx bit rate */
04f2dec1
SZ
460 window = &(tbl->win[scale_index]);
461
462 /* Get expected throughput */
463 tpt = get_expected_tpt(tbl, scale_index);
b481de9c 464
77626355
BC
465 /*
466 * Keep track of only the latest 62 tx frame attempts in this rate's
467 * history window; anything older isn't really relevant any more.
468 * If we have filled up the sliding window, drop the oldest attempt;
469 * if the oldest attempt (highest bit in bitmap) shows "success",
470 * subtract "1" from the success counter (this is the main reason
471 * we keep these bitmaps!).
472 */
e3949d62 473 while (attempts > 0) {
39e88504
GC
474 if (window->counter >= IWL_RATE_MAX_WINDOW) {
475
476 /* remove earliest */
477 window->counter = IWL_RATE_MAX_WINDOW - 1;
478
99556438
RR
479 if (window->data & mask) {
480 window->data &= ~mask;
39e88504 481 window->success_counter--;
99556438 482 }
b481de9c 483 }
b481de9c 484
99556438
RR
485 /* Increment frames-attempted counter */
486 window->counter++;
487
e3949d62 488 /* Shift bitmap by one frame to throw away oldest history */
90d7795e 489 window->data <<= 1;
e3949d62
DH
490
491 /* Mark the most recent #successes attempts as successful */
99556438 492 if (successes > 0) {
39e88504 493 window->success_counter++;
99556438
RR
494 window->data |= 0x1;
495 successes--;
496 }
77626355 497
e3949d62 498 attempts--;
b481de9c
ZY
499 }
500
77626355 501 /* Calculate current success ratio, avoid divide-by-0! */
b481de9c
ZY
502 if (window->counter > 0)
503 window->success_ratio = 128 * (100 * window->success_counter)
504 / window->counter;
505 else
506 window->success_ratio = IWL_INVALID_VALUE;
507
508 fail_count = window->counter - window->success_counter;
509
77626355 510 /* Calculate average throughput, if we have enough history. */
b481de9c
ZY
511 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
512 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
513 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
514 else
515 window->average_tpt = IWL_INVALID_VALUE;
516
77626355 517 /* Tag this window as having been updated */
b481de9c
ZY
518 window->stamp = jiffies;
519
dc2453ae 520 return 0;
b481de9c
ZY
521}
522
77626355
BC
523/*
524 * Fill uCode API rate_n_flags field, based on "search" or "active" table.
525 */
39e88504 526/* FIXME:RS:remove this function and put the flags statically in the table */
978785a3
TW
527static u32 rate_n_flags_from_tbl(struct iwl_priv *priv,
528 struct iwl_scale_tbl_info *tbl,
529 int index, u8 use_green)
b481de9c 530{
39e88504
GC
531 u32 rate_n_flags = 0;
532
b481de9c 533 if (is_legacy(tbl->lq_type)) {
1826dcc0 534 rate_n_flags = iwl_rates[index].plcp;
b481de9c 535 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
39e88504 536 rate_n_flags |= RATE_MCS_CCK_MSK;
b481de9c 537
fde0db31
GC
538 } else if (is_Ht(tbl->lq_type)) {
539 if (index > IWL_LAST_OFDM_RATE) {
978785a3 540 IWL_ERR(priv, "Invalid HT rate index %d\n", index);
b481de9c 541 index = IWL_LAST_OFDM_RATE;
fde0db31 542 }
39e88504 543 rate_n_flags = RATE_MCS_HT_MSK;
fde0db31
GC
544
545 if (is_siso(tbl->lq_type))
1826dcc0 546 rate_n_flags |= iwl_rates[index].plcp_siso;
fde0db31 547 else if (is_mimo2(tbl->lq_type))
1826dcc0 548 rate_n_flags |= iwl_rates[index].plcp_mimo2;
fde0db31 549 else
1826dcc0 550 rate_n_flags |= iwl_rates[index].plcp_mimo3;
b481de9c 551 } else {
978785a3 552 IWL_ERR(priv, "Invalid tbl->lq_type %d\n", tbl->lq_type);
b481de9c
ZY
553 }
554
39e88504 555 rate_n_flags |= ((tbl->ant_type << RATE_MCS_ANT_POS) &
fde0db31 556 RATE_MCS_ANT_ABC_MSK);
b481de9c 557
39e88504 558 if (is_Ht(tbl->lq_type)) {
7aafef1c 559 if (tbl->is_ht40) {
39e88504
GC
560 if (tbl->is_dup)
561 rate_n_flags |= RATE_MCS_DUP_MSK;
562 else
7aafef1c 563 rate_n_flags |= RATE_MCS_HT40_MSK;
39e88504
GC
564 }
565 if (tbl->is_SGI)
566 rate_n_flags |= RATE_MCS_SGI_MSK;
567
568 if (use_green) {
569 rate_n_flags |= RATE_MCS_GF_MSK;
570 if (is_siso(tbl->lq_type) && tbl->is_SGI) {
571 rate_n_flags &= ~RATE_MCS_SGI_MSK;
978785a3 572 IWL_ERR(priv, "GF was set with SGI:SISO\n");
39e88504
GC
573 }
574 }
b481de9c 575 }
39e88504 576 return rate_n_flags;
b481de9c
ZY
577}
578
77626355
BC
579/*
580 * Interpret uCode API's rate_n_flags format,
581 * fill "search" or "active" tx mode table.
582 */
39e88504 583static int rs_get_tbl_info_from_mcs(const u32 rate_n_flags,
57fbcce3 584 enum nl80211_band band,
e227ceac 585 struct iwl_scale_tbl_info *tbl,
b481de9c
ZY
586 int *rate_idx)
587{
39e88504
GC
588 u32 ant_msk = (rate_n_flags & RATE_MCS_ANT_ABC_MSK);
589 u8 num_of_ant = get_num_of_ant_from_rate(rate_n_flags);
590 u8 mcs;
b481de9c 591
80e9158c 592 memset(tbl, 0, sizeof(struct iwl_scale_tbl_info));
e7d326ac 593 *rate_idx = iwl_hwrate_to_plcp_idx(rate_n_flags);
b481de9c 594
fde0db31 595 if (*rate_idx == IWL_RATE_INVALID) {
b481de9c 596 *rate_idx = -1;
dc2453ae 597 return -EINVAL;
b481de9c 598 }
77626355 599 tbl->is_SGI = 0; /* default legacy setup */
7aafef1c 600 tbl->is_ht40 = 0;
b481de9c 601 tbl->is_dup = 0;
fde0db31
GC
602 tbl->ant_type = (ant_msk >> RATE_MCS_ANT_POS);
603 tbl->lq_type = LQ_NONE;
d6e93399 604 tbl->max_search = IWL_MAX_SEARCH;
b481de9c 605
77626355 606 /* legacy rate format */
39e88504 607 if (!(rate_n_flags & RATE_MCS_HT_MSK)) {
fde0db31 608 if (num_of_ant == 1) {
57fbcce3 609 if (band == NL80211_BAND_5GHZ)
b481de9c
ZY
610 tbl->lq_type = LQ_A;
611 else
612 tbl->lq_type = LQ_G;
b481de9c 613 }
fde0db31 614 /* HT rate format */
b481de9c 615 } else {
39e88504 616 if (rate_n_flags & RATE_MCS_SGI_MSK)
b481de9c
ZY
617 tbl->is_SGI = 1;
618
7aafef1c 619 if ((rate_n_flags & RATE_MCS_HT40_MSK) ||
39e88504 620 (rate_n_flags & RATE_MCS_DUP_MSK))
7aafef1c 621 tbl->is_ht40 = 1;
b481de9c 622
39e88504 623 if (rate_n_flags & RATE_MCS_DUP_MSK)
b481de9c 624 tbl->is_dup = 1;
fde0db31 625
39e88504 626 mcs = rs_extract_rate(rate_n_flags);
fde0db31 627
39e88504
GC
628 /* SISO */
629 if (mcs <= IWL_RATE_SISO_60M_PLCP) {
fde0db31
GC
630 if (num_of_ant == 1)
631 tbl->lq_type = LQ_SISO; /*else NONE*/
632 /* MIMO2 */
39e88504 633 } else if (mcs <= IWL_RATE_MIMO2_60M_PLCP) {
fde0db31
GC
634 if (num_of_ant == 2)
635 tbl->lq_type = LQ_MIMO2;
636 /* MIMO3 */
637 } else {
d6e93399
MA
638 if (num_of_ant == 3) {
639 tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
fde0db31 640 tbl->lq_type = LQ_MIMO3;
d6e93399 641 }
fde0db31 642 }
b481de9c
ZY
643 }
644 return 0;
645}
aade00ce
GC
646
647/* switch to another antenna/antennas and return 1 */
648/* if no other valid antenna found, return 0 */
649static int rs_toggle_antenna(u32 valid_ant, u32 *rate_n_flags,
e227ceac 650 struct iwl_scale_tbl_info *tbl)
b481de9c 651{
aade00ce
GC
652 u8 new_ant_type;
653
654 if (!tbl->ant_type || tbl->ant_type > ANT_ABC)
655 return 0;
656
657 if (!rs_is_valid_ant(valid_ant, tbl->ant_type))
658 return 0;
659
660 new_ant_type = ant_toggle_lookup[tbl->ant_type];
661
662 while ((new_ant_type != tbl->ant_type) &&
663 !rs_is_valid_ant(valid_ant, new_ant_type))
664 new_ant_type = ant_toggle_lookup[new_ant_type];
665
666 if (new_ant_type == tbl->ant_type)
667 return 0;
668
669 tbl->ant_type = new_ant_type;
670 *rate_n_flags &= ~RATE_MCS_ANT_ABC_MSK;
671 *rate_n_flags |= new_ant_type << RATE_MCS_ANT_POS;
672 return 1;
b481de9c
ZY
673}
674
b261793d
DH
675/**
676 * Green-field mode is valid if the station supports it and
677 * there are no non-GF stations present in the BSS.
678 */
7e6a5886 679static bool rs_use_green(struct ieee80211_sta *sta)
b481de9c 680{
50e2a30c
JB
681 /*
682 * There's a bug somewhere in this code that causes the
683 * scaling to get stuck because GF+SGI can't be combined
684 * in SISO rates. Until we find that bug, disable GF, it
685 * has only limited benefit and we still interoperate with
686 * GF APs since we can always receive GF transmissions.
687 */
688 return false;
b481de9c
ZY
689}
690
691/**
692 * rs_get_supported_rates - get the available rates
693 *
694 * if management frame or broadcast frame only return
695 * basic available rates.
696 *
697 */
e227ceac
TW
698static u16 rs_get_supported_rates(struct iwl_lq_sta *lq_sta,
699 struct ieee80211_hdr *hdr,
700 enum iwl_table_type rate_type)
b481de9c 701{
eecd6e57
GC
702 if (is_legacy(rate_type)) {
703 return lq_sta->active_legacy_rate;
704 } else {
b481de9c 705 if (is_siso(rate_type))
eecd6e57 706 return lq_sta->active_siso_rate;
fde0db31 707 else if (is_mimo2(rate_type))
eecd6e57 708 return lq_sta->active_mimo2_rate;
b481de9c 709 else
eecd6e57 710 return lq_sta->active_mimo3_rate;
c33104f0 711 }
b481de9c
ZY
712}
713
bf403db8
EK
714static u16 rs_get_adjacent_rate(struct iwl_priv *priv, u8 index, u16 rate_mask,
715 int rate_type)
b481de9c
ZY
716{
717 u8 high = IWL_RATE_INVALID;
718 u8 low = IWL_RATE_INVALID;
719
01ebd063 720 /* 802.11A or ht walks to the next literal adjacent rate in
b481de9c
ZY
721 * the rate table */
722 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
723 int i;
724 u32 mask;
725
726 /* Find the previous rate that is in the rate mask */
727 i = index - 1;
1aa0ec5c
EG
728 if (i >= 0)
729 mask = BIT(i);
730
731 for (; i >= 0; i--, mask >>= 1) {
b481de9c
ZY
732 if (rate_mask & mask) {
733 low = i;
734 break;
735 }
736 }
737
738 /* Find the next rate that is in the rate mask */
739 i = index + 1;
740 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
741 if (rate_mask & mask) {
742 high = i;
743 break;
744 }
745 }
746
747 return (high << 8) | low;
748 }
749
750 low = index;
751 while (low != IWL_RATE_INVALID) {
1826dcc0 752 low = iwl_rates[low].prev_rs;
b481de9c
ZY
753 if (low == IWL_RATE_INVALID)
754 break;
755 if (rate_mask & (1 << low))
756 break;
e1623446 757 IWL_DEBUG_RATE(priv, "Skipping masked lower rate: %d\n", low);
b481de9c
ZY
758 }
759
760 high = index;
761 while (high != IWL_RATE_INVALID) {
1826dcc0 762 high = iwl_rates[high].next_rs;
b481de9c
ZY
763 if (high == IWL_RATE_INVALID)
764 break;
765 if (rate_mask & (1 << high))
766 break;
e1623446 767 IWL_DEBUG_RATE(priv, "Skipping masked higher rate: %d\n", high);
b481de9c
ZY
768 }
769
770 return (high << 8) | low;
771}
772
e227ceac
TW
773static u32 rs_get_lower_rate(struct iwl_lq_sta *lq_sta,
774 struct iwl_scale_tbl_info *tbl,
775 u8 scale_index, u8 ht_possible)
b481de9c 776{
b481de9c
ZY
777 s32 low;
778 u16 rate_mask;
779 u16 high_low;
780 u8 switch_to_legacy = 0;
c33104f0 781 u8 is_green = lq_sta->is_green;
2ff6578b 782 struct iwl_priv *priv = lq_sta->drv;
b481de9c
ZY
783
784 /* check if we need to switch from HT to legacy rates.
785 * assumption is that mandatory rates (1Mbps or 6Mbps)
786 * are always supported (spec demand) */
787 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
788 switch_to_legacy = 1;
789 scale_index = rs_ht_to_legacy[scale_index];
57fbcce3 790 if (lq_sta->band == NL80211_BAND_5GHZ)
b481de9c
ZY
791 tbl->lq_type = LQ_A;
792 else
793 tbl->lq_type = LQ_G;
794
69fdb309 795 if (num_of_ant(tbl->ant_type) > 1)
2ff6578b 796 tbl->ant_type =
b7998c8b 797 first_antenna(priv->nvm_data->valid_tx_ant);
b481de9c 798
7aafef1c 799 tbl->is_ht40 = 0;
b481de9c 800 tbl->is_SGI = 0;
d6e93399 801 tbl->max_search = IWL_MAX_SEARCH;
b481de9c
ZY
802 }
803
eecd6e57 804 rate_mask = rs_get_supported_rates(lq_sta, NULL, tbl->lq_type);
b481de9c 805
77626355 806 /* Mask with station rate restriction */
b481de9c 807 if (is_legacy(tbl->lq_type)) {
77626355 808 /* supp_rates has no CCK bits in A mode */
57fbcce3 809 if (lq_sta->band == NL80211_BAND_5GHZ)
b481de9c 810 rate_mask = (u16)(rate_mask &
c33104f0 811 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
b481de9c 812 else
c33104f0 813 rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
b481de9c
ZY
814 }
815
77626355 816 /* If we switched from HT to legacy, check current rate */
dc2453ae 817 if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
39e88504
GC
818 low = scale_index;
819 goto out;
b481de9c
ZY
820 }
821
bf403db8
EK
822 high_low = rs_get_adjacent_rate(lq_sta->drv, scale_index, rate_mask,
823 tbl->lq_type);
b481de9c
ZY
824 low = high_low & 0xff;
825
39e88504
GC
826 if (low == IWL_RATE_INVALID)
827 low = scale_index;
828
829out:
978785a3 830 return rate_n_flags_from_tbl(lq_sta->drv, tbl, low, is_green);
b481de9c
ZY
831}
832
95407aa4
DH
833/*
834 * Simple function to compare two rate scale table types
835 */
836static bool table_type_matches(struct iwl_scale_tbl_info *a,
837 struct iwl_scale_tbl_info *b)
838{
839 return (a->lq_type == b->lq_type) && (a->ant_type == b->ant_type) &&
840 (a->is_SGI == b->is_SGI);
841}
95407aa4 842
7e6a5886
JB
843static void rs_bt_update_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
844 struct iwl_lq_sta *lq_sta)
bee008b7
WYG
845{
846 struct iwl_scale_tbl_info *tbl;
66e863a5 847 bool full_concurrent = priv->bt_full_concurrent;
bee008b7 848
66e863a5
WYG
849 if (priv->bt_ant_couple_ok) {
850 /*
851 * Is there a need to switch between
852 * full concurrency and 3-wire?
853 */
e7babbe3 854 if (priv->bt_ci_compliance)
66e863a5
WYG
855 full_concurrent = true;
856 else
857 full_concurrent = false;
66e863a5
WYG
858 }
859 if ((priv->bt_traffic_load != priv->last_bt_traffic_load) ||
860 (priv->bt_full_concurrent != full_concurrent)) {
bee008b7 861 priv->bt_full_concurrent = full_concurrent;
882dde8e 862 priv->last_bt_traffic_load = priv->bt_traffic_load;
bee008b7
WYG
863
864 /* Update uCode's rate table. */
865 tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
866 rs_fill_link_cmd(priv, lq_sta, tbl->current_rate);
7e6a5886 867 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_ASYNC, false);
bee008b7 868
1ee158d8 869 queue_work(priv->workqueue, &priv->bt_full_concurrency);
bee008b7
WYG
870 }
871}
872
77626355
BC
873/*
874 * mac80211 sends us Tx status
875 */
4b7679a5
JB
876static void rs_tx_status(void *priv_r, struct ieee80211_supported_band *sband,
877 struct ieee80211_sta *sta, void *priv_sta,
e039fa4a 878 struct sk_buff *skb)
b481de9c 879{
95407aa4
DH
880 int legacy_success;
881 int retries;
882 int rs_index, mac_index, i;
417f114b 883 struct iwl_lq_sta *lq_sta = priv_sta;
66c73db7 884 struct iwl_link_quality_cmd *table;
b481de9c 885 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
d0f76d68
EG
886 struct iwl_op_mode *op_mode = (struct iwl_op_mode *)priv_r;
887 struct iwl_priv *priv = IWL_OP_MODE_GET_DVM(op_mode);
e039fa4a 888 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
5027309b 889 enum mac80211_rate_control_flags mac_flags;
39e88504 890 u32 tx_rate;
e227ceac 891 struct iwl_scale_tbl_info tbl_type;
04f2dec1 892 struct iwl_scale_tbl_info *curr_tbl, *other_tbl, *tmp_tbl;
7e6a5886 893 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
c6892906 894 struct iwl_rxon_context *ctx = sta_priv->ctx;
b481de9c 895
e1623446 896 IWL_DEBUG_RATE_LIMIT(priv, "get frame ack response, update rate scale window\n");
b481de9c 897
fe6b23dd
RC
898 /* Treat uninitialized rate scaling data same as non-existing. */
899 if (!lq_sta) {
900 IWL_DEBUG_RATE(priv, "Station rate scaling not created yet.\n");
901 return;
902 } else if (!lq_sta->drv) {
903 IWL_DEBUG_RATE(priv, "Rate scaling not initialized yet.\n");
904 return;
905 }
906
417f114b 907 if (!ieee80211_is_data(hdr->frame_control) ||
514d65c1 908 info->flags & IEEE80211_TX_CTL_NO_ACK)
b481de9c
ZY
909 return;
910
e3949d62 911 /* This packet was aggregated but doesn't carry status info */
e039fa4a
JB
912 if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
913 !(info->flags & IEEE80211_TX_STAT_AMPDU))
99556438
RR
914 return;
915
77626355
BC
916 /*
917 * Ignore this Tx frame response if its initial rate doesn't match
918 * that of latest Link Quality command. There may be stragglers
919 * from a previous Link Quality command, but we're no longer interested
920 * in those; they're either from the "active" mode while we're trying
921 * to check "search" mode, or a prior "search" mode after we've moved
922 * to a new "search" mode (which might become the new "active" mode).
923 */
95407aa4 924 table = &lq_sta->lq;
39e88504
GC
925 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
926 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
57fbcce3 927 if (priv->band == NL80211_BAND_5GHZ)
4c424e4c 928 rs_index -= IWL_FIRST_OFDM_RATE;
5027309b
DH
929 mac_flags = info->status.rates[0].flags;
930 mac_index = info->status.rates[0].idx;
31513be8
DH
931 /* For HT packets, map MCS to PLCP */
932 if (mac_flags & IEEE80211_TX_RC_MCS) {
933 mac_index &= RATE_MCS_CODE_MSK; /* Remove # of streams */
934 if (mac_index >= (IWL_RATE_9M_INDEX - IWL_FIRST_OFDM_RATE))
935 mac_index++;
392a0baf
DH
936 /*
937 * mac80211 HT index is always zero-indexed; we need to move
938 * HT OFDM rates after CCK rates in 2.4 GHz band
939 */
57fbcce3 940 if (priv->band == NL80211_BAND_2GHZ)
392a0baf 941 mac_index += IWL_FIRST_OFDM_RATE;
31513be8 942 }
95407aa4 943 /* Here we actually compare this rate to the latest LQ command */
5027309b
DH
944 if ((mac_index < 0) ||
945 (tbl_type.is_SGI != !!(mac_flags & IEEE80211_TX_RC_SHORT_GI)) ||
946 (tbl_type.is_ht40 != !!(mac_flags & IEEE80211_TX_RC_40_MHZ_WIDTH)) ||
947 (tbl_type.is_dup != !!(mac_flags & IEEE80211_TX_RC_DUP_DATA)) ||
d748b464 948 (tbl_type.ant_type != info->status.antenna) ||
5027309b
DH
949 (!!(tx_rate & RATE_MCS_HT_MSK) != !!(mac_flags & IEEE80211_TX_RC_MCS)) ||
950 (!!(tx_rate & RATE_MCS_GF_MSK) != !!(mac_flags & IEEE80211_TX_RC_GREEN_FIELD)) ||
31513be8
DH
951 (rs_index != mac_index)) {
952 IWL_DEBUG_RATE(priv, "initial rate %d does not match %d (0x%x)\n", mac_index, rs_index, tx_rate);
95407aa4
DH
953 /*
954 * Since rates mis-match, the last LQ command may have failed.
955 * After IWL_MISSED_RATE_MAX mis-matches, resync the uCode with
956 * ... driver.
d5e49036 957 */
7d049e5a
AM
958 lq_sta->missed_rate_counter++;
959 if (lq_sta->missed_rate_counter > IWL_MISSED_RATE_MAX) {
960 lq_sta->missed_rate_counter = 0;
7e6a5886 961 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_ASYNC, false);
7d049e5a 962 }
95407aa4
DH
963 /* Regardless, ignore this status info for outdated rate */
964 return;
965 } else
966 /* Rate did match, so reset the missed_rate_counter */
967 lq_sta->missed_rate_counter = 0;
968
969 /* Figure out if rate scale algorithm is in active or search table */
970 if (table_type_matches(&tbl_type,
971 &(lq_sta->lq_info[lq_sta->active_tbl]))) {
972 curr_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
973 other_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
974 } else if (table_type_matches(&tbl_type,
975 &lq_sta->lq_info[1 - lq_sta->active_tbl])) {
976 curr_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
977 other_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
978 } else {
979 IWL_DEBUG_RATE(priv, "Neither active nor search matches tx rate\n");
80e9158c
WYG
980 tmp_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
981 IWL_DEBUG_RATE(priv, "active- lq:%x, ant:%x, SGI:%d\n",
982 tmp_tbl->lq_type, tmp_tbl->ant_type, tmp_tbl->is_SGI);
983 tmp_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
984 IWL_DEBUG_RATE(priv, "search- lq:%x, ant:%x, SGI:%d\n",
985 tmp_tbl->lq_type, tmp_tbl->ant_type, tmp_tbl->is_SGI);
986 IWL_DEBUG_RATE(priv, "actual- lq:%x, ant:%x, SGI:%d\n",
987 tbl_type.lq_type, tbl_type.ant_type, tbl_type.is_SGI);
988 /*
989 * no matching table found, let's by-pass the data collection
990 * and continue to perform rate scale to find the rate table
991 */
da5dbb97 992 rs_stay_in_table(lq_sta, true);
80e9158c 993 goto done;
b481de9c
ZY
994 }
995
95407aa4
DH
996 /*
997 * Updating the frame history depends on whether packets were
998 * aggregated.
999 *
1000 * For aggregation, all packets were transmitted at the same rate, the
1001 * first index into rate scale table.
1002 */
1003 if (info->flags & IEEE80211_TX_STAT_AMPDU) {
1004 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
1005 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type,
1006 &rs_index);
04f2dec1 1007 rs_collect_tx_data(curr_tbl, rs_index,
e3a3cd87
DH
1008 info->status.ampdu_len,
1009 info->status.ampdu_ack_len);
95407aa4
DH
1010
1011 /* Update success/fail counts if not searching for new mode */
1012 if (lq_sta->stay_in_tbl) {
e3a3cd87
DH
1013 lq_sta->total_success += info->status.ampdu_ack_len;
1014 lq_sta->total_failed += (info->status.ampdu_len -
1015 info->status.ampdu_ack_len);
b481de9c 1016 }
95407aa4 1017 } else {
77626355 1018 /*
95407aa4 1019 * For legacy, update frame history with for each Tx retry.
77626355 1020 */
95407aa4
DH
1021 retries = info->status.rates[0].count - 1;
1022 /* HW doesn't send more than 15 retries */
1023 retries = min(retries, 15);
1024
1025 /* The last transmission may have been successful */
1026 legacy_success = !!(info->flags & IEEE80211_TX_STAT_ACK);
1027 /* Collect data for each rate used during failed TX attempts */
1028 for (i = 0; i <= retries; ++i) {
1029 tx_rate = le32_to_cpu(table->rs_table[i].rate_n_flags);
1030 rs_get_tbl_info_from_mcs(tx_rate, priv->band,
1031 &tbl_type, &rs_index);
1032 /*
1033 * Only collect stats if retried rate is in the same RS
1034 * table as active/search.
1035 */
1036 if (table_type_matches(&tbl_type, curr_tbl))
04f2dec1 1037 tmp_tbl = curr_tbl;
95407aa4 1038 else if (table_type_matches(&tbl_type, other_tbl))
04f2dec1 1039 tmp_tbl = other_tbl;
95407aa4
DH
1040 else
1041 continue;
04f2dec1
SZ
1042 rs_collect_tx_data(tmp_tbl, rs_index, 1,
1043 i < retries ? 0 : legacy_success);
95407aa4
DH
1044 }
1045
1046 /* Update success/fail counts if not searching for new mode */
1047 if (lq_sta->stay_in_tbl) {
1048 lq_sta->total_success += legacy_success;
1049 lq_sta->total_failed += retries + (1 - legacy_success);
99556438 1050 }
b481de9c 1051 }
95407aa4
DH
1052 /* The last TX rate is cached in lq_sta; it's set in if/else above */
1053 lq_sta->last_rate_n_flags = tx_rate;
80e9158c 1054done:
77626355 1055 /* See if there's a better rate or modulation mode to try. */
c338ba3c 1056 if (sta && sta->supp_rates[sband->band])
514d65c1 1057 rs_rate_scale_perform(priv, skb, sta, lq_sta);
c10e2c10 1058
0d8877a1 1059 if (priv->lib->bt_params && priv->lib->bt_params->advanced_bt_coexist)
7e6a5886 1060 rs_bt_update_lq(priv, ctx, lq_sta);
b481de9c
ZY
1061}
1062
77626355
BC
1063/*
1064 * Begin a period of staying with a selected modulation mode.
1065 * Set "stay_in_tbl" flag to prevent any mode switches.
1066 * Set frame tx success limits according to legacy vs. high-throughput,
1067 * and reset overall (spanning all rates) tx success history statistics.
1068 * These control how long we stay using same modulation mode before
1069 * searching for a new mode.
1070 */
bf403db8 1071static void rs_set_stay_in_table(struct iwl_priv *priv, u8 is_legacy,
e227ceac 1072 struct iwl_lq_sta *lq_sta)
b481de9c 1073{
e1623446 1074 IWL_DEBUG_RATE(priv, "we are staying in the same table\n");
c33104f0 1075 lq_sta->stay_in_tbl = 1; /* only place this gets set */
b481de9c 1076 if (is_legacy) {
c33104f0
TW
1077 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
1078 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
1079 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
b481de9c 1080 } else {
c33104f0
TW
1081 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
1082 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
1083 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
b481de9c 1084 }
c33104f0
TW
1085 lq_sta->table_count = 0;
1086 lq_sta->total_failed = 0;
1087 lq_sta->total_success = 0;
447fee70 1088 lq_sta->flush_timer = jiffies;
d6e93399 1089 lq_sta->action_counter = 0;
b481de9c
ZY
1090}
1091
77626355
BC
1092/*
1093 * Find correct throughput table for given mode of modulation
1094 */
e227ceac
TW
1095static void rs_set_expected_tpt_table(struct iwl_lq_sta *lq_sta,
1096 struct iwl_scale_tbl_info *tbl)
b481de9c 1097{
e3949d62 1098 /* Used to choose among HT tables */
a34529e8 1099 const u16 (*ht_tbl_pointer)[IWL_RATE_COUNT];
e3949d62
DH
1100
1101 /* Check for invalid LQ type */
1102 if (WARN_ON_ONCE(!is_legacy(tbl->lq_type) && !is_Ht(tbl->lq_type))) {
1103 tbl->expected_tpt = expected_tpt_legacy;
1104 return;
1105 }
1106
1107 /* Legacy rates have only one table */
b481de9c 1108 if (is_legacy(tbl->lq_type)) {
e3949d62
DH
1109 tbl->expected_tpt = expected_tpt_legacy;
1110 return;
1111 }
1112
1113 /* Choose among many HT tables depending on number of streams
1114 * (SISO/MIMO2/MIMO3), channel width (20/40), SGI, and aggregation
1115 * status */
1116 if (is_siso(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
1117 ht_tbl_pointer = expected_tpt_siso20MHz;
1118 else if (is_siso(tbl->lq_type))
1119 ht_tbl_pointer = expected_tpt_siso40MHz;
1120 else if (is_mimo2(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
1121 ht_tbl_pointer = expected_tpt_mimo2_20MHz;
1122 else if (is_mimo2(tbl->lq_type))
1123 ht_tbl_pointer = expected_tpt_mimo2_40MHz;
1124 else if (is_mimo3(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
1125 ht_tbl_pointer = expected_tpt_mimo3_20MHz;
1126 else /* if (is_mimo3(tbl->lq_type)) <-- must be true */
1127 ht_tbl_pointer = expected_tpt_mimo3_40MHz;
1128
1129 if (!tbl->is_SGI && !lq_sta->is_agg) /* Normal */
1130 tbl->expected_tpt = ht_tbl_pointer[0];
1131 else if (tbl->is_SGI && !lq_sta->is_agg) /* SGI */
1132 tbl->expected_tpt = ht_tbl_pointer[1];
1133 else if (!tbl->is_SGI && lq_sta->is_agg) /* AGG */
1134 tbl->expected_tpt = ht_tbl_pointer[2];
1135 else /* AGG+SGI */
1136 tbl->expected_tpt = ht_tbl_pointer[3];
b481de9c
ZY
1137}
1138
77626355
BC
1139/*
1140 * Find starting rate for new "search" high-throughput mode of modulation.
1141 * Goal is to find lowest expected rate (under perfect conditions) that is
1142 * above the current measured throughput of "active" mode, to give new mode
1143 * a fair chance to prove itself without too many challenges.
1144 *
1145 * This gets called when transitioning to more aggressive modulation
1146 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
1147 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1148 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1149 * bit rate will typically need to increase, but not if performance was bad.
1150 */
c79dd5b5 1151static s32 rs_get_best_rate(struct iwl_priv *priv,
e227ceac
TW
1152 struct iwl_lq_sta *lq_sta,
1153 struct iwl_scale_tbl_info *tbl, /* "search" */
f935a6da 1154 u16 rate_mask, s8 index)
b481de9c 1155{
77626355 1156 /* "active" values */
e227ceac 1157 struct iwl_scale_tbl_info *active_tbl =
c33104f0 1158 &(lq_sta->lq_info[lq_sta->active_tbl]);
b481de9c 1159 s32 active_sr = active_tbl->win[index].success_ratio;
b481de9c 1160 s32 active_tpt = active_tbl->expected_tpt[index];
77626355 1161 /* expected "search" throughput */
a34529e8 1162 const u16 *tpt_tbl = tbl->expected_tpt;
77626355
BC
1163
1164 s32 new_rate, high, low, start_hi;
b481de9c 1165 u16 high_low;
f935a6da 1166 s8 rate = index;
b481de9c
ZY
1167
1168 new_rate = high = low = start_hi = IWL_RATE_INVALID;
1169
1170 for (; ;) {
bf403db8
EK
1171 high_low = rs_get_adjacent_rate(priv, rate, rate_mask,
1172 tbl->lq_type);
b481de9c
ZY
1173
1174 low = high_low & 0xff;
1175 high = (high_low >> 8) & 0xff;
1176
77626355
BC
1177 /*
1178 * Lower the "search" bit rate, to give new "search" mode
1179 * approximately the same throughput as "active" if:
1180 *
1181 * 1) "Active" mode has been working modestly well (but not
1182 * great), and expected "search" throughput (under perfect
1183 * conditions) at candidate rate is above the actual
1184 * measured "active" throughput (but less than expected
1185 * "active" throughput under perfect conditions).
1186 * OR
1187 * 2) "Active" mode has been working perfectly or very well
1188 * and expected "search" throughput (under perfect
1189 * conditions) at candidate rate is above expected
1190 * "active" throughput (under perfect conditions).
1191 */
c33104f0 1192 if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
b481de9c
ZY
1193 ((active_sr > IWL_RATE_DECREASE_TH) &&
1194 (active_sr <= IWL_RATE_HIGH_TH) &&
1195 (tpt_tbl[rate] <= active_tpt))) ||
1196 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
1197 (tpt_tbl[rate] > active_tpt))) {
1198
77626355
BC
1199 /* (2nd or later pass)
1200 * If we've already tried to raise the rate, and are
1201 * now trying to lower it, use the higher rate. */
b481de9c
ZY
1202 if (start_hi != IWL_RATE_INVALID) {
1203 new_rate = start_hi;
1204 break;
1205 }
77626355 1206
b481de9c 1207 new_rate = rate;
77626355
BC
1208
1209 /* Loop again with lower rate */
b481de9c
ZY
1210 if (low != IWL_RATE_INVALID)
1211 rate = low;
77626355
BC
1212
1213 /* Lower rate not available, use the original */
b481de9c
ZY
1214 else
1215 break;
77626355
BC
1216
1217 /* Else try to raise the "search" rate to match "active" */
b481de9c 1218 } else {
77626355
BC
1219 /* (2nd or later pass)
1220 * If we've already tried to lower the rate, and are
1221 * now trying to raise it, use the lower rate. */
b481de9c
ZY
1222 if (new_rate != IWL_RATE_INVALID)
1223 break;
77626355
BC
1224
1225 /* Loop again with higher rate */
b481de9c
ZY
1226 else if (high != IWL_RATE_INVALID) {
1227 start_hi = high;
1228 rate = high;
77626355
BC
1229
1230 /* Higher rate not available, use the original */
b481de9c 1231 } else {
90d7795e 1232 new_rate = rate;
b481de9c
ZY
1233 break;
1234 }
1235 }
1236 }
1237
1238 return new_rate;
1239}
b481de9c 1240
77626355 1241/*
584a0f00 1242 * Set up search table for MIMO2
77626355 1243 */
fde0db31 1244static int rs_switch_to_mimo2(struct iwl_priv *priv,
e227ceac 1245 struct iwl_lq_sta *lq_sta,
270243a5 1246 struct ieee80211_conf *conf,
4b7679a5 1247 struct ieee80211_sta *sta,
e227ceac 1248 struct iwl_scale_tbl_info *tbl, int index)
b481de9c 1249{
b481de9c
ZY
1250 u16 rate_mask;
1251 s32 rate;
c33104f0 1252 s8 is_green = lq_sta->is_green;
7e6a5886 1253 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
c6892906 1254 struct iwl_rxon_context *ctx = sta_priv->ctx;
b481de9c 1255
de27e64e 1256 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
b481de9c
ZY
1257 return -1;
1258
af0ed69b 1259 if (sta->smps_mode == IEEE80211_SMPS_STATIC)
b481de9c
ZY
1260 return -1;
1261
77626355 1262 /* Need both Tx chains/antennas to support MIMO */
9e295116 1263 if (priv->hw_params.tx_chains_num < 2)
b481de9c
ZY
1264 return -1;
1265
e1623446 1266 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO2\n");
39e88504
GC
1267
1268 tbl->lq_type = LQ_MIMO2;
c33104f0 1269 tbl->is_dup = lq_sta->is_dup;
b481de9c 1270 tbl->action = 0;
d6e93399 1271 tbl->max_search = IWL_MAX_SEARCH;
39e88504
GC
1272 rate_mask = lq_sta->active_mimo2_rate;
1273
e1a0c6b3 1274 if (iwl_is_ht40_tx_allowed(priv, ctx, sta))
7aafef1c 1275 tbl->is_ht40 = 1;
b481de9c 1276 else
7aafef1c 1277 tbl->is_ht40 = 0;
b481de9c 1278
eecd6e57 1279 rs_set_expected_tpt_table(lq_sta, tbl);
b481de9c 1280
f935a6da 1281 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
b481de9c 1282
e1623446 1283 IWL_DEBUG_RATE(priv, "LQ: MIMO2 best rate %d mask %X\n", rate, rate_mask);
584a0f00
WYG
1284 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1285 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
1286 rate, rate_mask);
1287 return -1;
1288 }
1289 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
1290
1291 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
1292 tbl->current_rate, is_green);
1293 return 0;
1294}
1295
1296/*
1297 * Set up search table for MIMO3
1298 */
1299static int rs_switch_to_mimo3(struct iwl_priv *priv,
1300 struct iwl_lq_sta *lq_sta,
1301 struct ieee80211_conf *conf,
1302 struct ieee80211_sta *sta,
1303 struct iwl_scale_tbl_info *tbl, int index)
1304{
1305 u16 rate_mask;
1306 s32 rate;
1307 s8 is_green = lq_sta->is_green;
7e6a5886 1308 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
c6892906 1309 struct iwl_rxon_context *ctx = sta_priv->ctx;
584a0f00
WYG
1310
1311 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1312 return -1;
1313
af0ed69b 1314 if (sta->smps_mode == IEEE80211_SMPS_STATIC)
584a0f00
WYG
1315 return -1;
1316
1317 /* Need both Tx chains/antennas to support MIMO */
9e295116 1318 if (priv->hw_params.tx_chains_num < 3)
584a0f00
WYG
1319 return -1;
1320
1321 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO3\n");
1322
1323 tbl->lq_type = LQ_MIMO3;
1324 tbl->is_dup = lq_sta->is_dup;
1325 tbl->action = 0;
d6e93399 1326 tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
584a0f00
WYG
1327 rate_mask = lq_sta->active_mimo3_rate;
1328
e1a0c6b3 1329 if (iwl_is_ht40_tx_allowed(priv, ctx, sta))
7aafef1c 1330 tbl->is_ht40 = 1;
584a0f00 1331 else
7aafef1c 1332 tbl->is_ht40 = 0;
39e88504 1333
584a0f00
WYG
1334 rs_set_expected_tpt_table(lq_sta, tbl);
1335
1336 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1337
1338 IWL_DEBUG_RATE(priv, "LQ: MIMO3 best rate %d mask %X\n",
1339 rate, rate_mask);
39e88504 1340 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
e1623446 1341 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
39e88504 1342 rate, rate_mask);
b481de9c 1343 return -1;
39e88504 1344 }
978785a3 1345 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
b481de9c 1346
e1623446 1347 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
39e88504 1348 tbl->current_rate, is_green);
dc2453ae 1349 return 0;
fde0db31 1350}
b481de9c 1351
77626355
BC
1352/*
1353 * Set up search table for SISO
1354 */
c79dd5b5 1355static int rs_switch_to_siso(struct iwl_priv *priv,
e227ceac 1356 struct iwl_lq_sta *lq_sta,
270243a5 1357 struct ieee80211_conf *conf,
4b7679a5 1358 struct ieee80211_sta *sta,
e227ceac 1359 struct iwl_scale_tbl_info *tbl, int index)
b481de9c 1360{
b481de9c 1361 u16 rate_mask;
c33104f0 1362 u8 is_green = lq_sta->is_green;
b481de9c 1363 s32 rate;
7e6a5886 1364 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
c6892906 1365 struct iwl_rxon_context *ctx = sta_priv->ctx;
b481de9c 1366
de27e64e 1367 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
b481de9c
ZY
1368 return -1;
1369
e1623446 1370 IWL_DEBUG_RATE(priv, "LQ: try to switch to SISO\n");
39e88504 1371
c33104f0 1372 tbl->is_dup = lq_sta->is_dup;
b481de9c
ZY
1373 tbl->lq_type = LQ_SISO;
1374 tbl->action = 0;
d6e93399 1375 tbl->max_search = IWL_MAX_SEARCH;
39e88504 1376 rate_mask = lq_sta->active_siso_rate;
b481de9c 1377
e1a0c6b3 1378 if (iwl_is_ht40_tx_allowed(priv, ctx, sta))
7aafef1c 1379 tbl->is_ht40 = 1;
b481de9c 1380 else
7aafef1c 1381 tbl->is_ht40 = 0;
b481de9c 1382
b481de9c 1383 if (is_green)
39e88504 1384 tbl->is_SGI = 0; /*11n spec: no SGI in SISO+Greenfield*/
b481de9c 1385
eecd6e57 1386 rs_set_expected_tpt_table(lq_sta, tbl);
f935a6da 1387 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
b481de9c 1388
e1623446 1389 IWL_DEBUG_RATE(priv, "LQ: get best rate %d mask %X\n", rate, rate_mask);
b481de9c 1390 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
e1623446 1391 IWL_DEBUG_RATE(priv, "can not switch with index %d rate mask %x\n",
b481de9c
ZY
1392 rate, rate_mask);
1393 return -1;
1394 }
978785a3 1395 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
e1623446 1396 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
39e88504 1397 tbl->current_rate, is_green);
403ab56b 1398 return 0;
b481de9c
ZY
1399}
1400
77626355
BC
1401/*
1402 * Try to switch to new modulation mode from legacy
1403 */
48ed7040
NK
1404static void rs_move_legacy_other(struct iwl_priv *priv,
1405 struct iwl_lq_sta *lq_sta,
1406 struct ieee80211_conf *conf,
1407 struct ieee80211_sta *sta,
1408 int index)
b481de9c 1409{
e227ceac
TW
1410 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1411 struct iwl_scale_tbl_info *search_tbl =
1412 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1413 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1414 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1415 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
b1a78858 1416 u8 start_action;
b7998c8b 1417 u8 valid_tx_ant = priv->nvm_data->valid_tx_ant;
9e295116 1418 u8 tx_chains_num = priv->hw_params.tx_chains_num;
fde0db31 1419 int ret = 0;
d6e93399 1420 u8 update_search_tbl_counter = 0;
b481de9c 1421
290f599c
JB
1422 switch (priv->bt_traffic_load) {
1423 case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
1424 /* nothing */
1425 break;
1426 case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
1427 /* avoid antenna B unless MIMO */
1428 if (tbl->action == IWL_LEGACY_SWITCH_ANTENNA2)
383b0874 1429 tbl->action = IWL_LEGACY_SWITCH_SISO;
290f599c
JB
1430 break;
1431 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
1432 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
1433 /* avoid antenna B and MIMO */
d6189124 1434 valid_tx_ant =
b7998c8b 1435 first_antenna(priv->nvm_data->valid_tx_ant);
290f599c
JB
1436 if (tbl->action >= IWL_LEGACY_SWITCH_ANTENNA2 &&
1437 tbl->action != IWL_LEGACY_SWITCH_SISO)
da5dbb97 1438 tbl->action = IWL_LEGACY_SWITCH_SISO;
290f599c
JB
1439 break;
1440 default:
3c6acb61 1441 IWL_ERR(priv, "Invalid BT load %d\n", priv->bt_traffic_load);
290f599c
JB
1442 break;
1443 }
1444
46f9381a
WYG
1445 if (!iwl_ht_enabled(priv))
1446 /* stay in Legacy */
1447 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
3ad3b92a 1448 else if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE &&
46f9381a
WYG
1449 tbl->action > IWL_LEGACY_SWITCH_SISO)
1450 tbl->action = IWL_LEGACY_SWITCH_SISO;
bee008b7
WYG
1451
1452 /* configure as 1x1 if bt full concurrency */
1453 if (priv->bt_full_concurrent) {
1454 if (!iwl_ht_enabled(priv))
1455 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
1456 else if (tbl->action >= IWL_LEGACY_SWITCH_ANTENNA2)
1457 tbl->action = IWL_LEGACY_SWITCH_SISO;
d6189124 1458 valid_tx_ant =
b7998c8b 1459 first_antenna(priv->nvm_data->valid_tx_ant);
bee008b7
WYG
1460 }
1461
b1a78858 1462 start_action = tbl->action;
b481de9c 1463 for (; ;) {
d6e93399 1464 lq_sta->action_counter++;
b481de9c 1465 switch (tbl->action) {
3110bef7
GC
1466 case IWL_LEGACY_SWITCH_ANTENNA1:
1467 case IWL_LEGACY_SWITCH_ANTENNA2:
e1623446 1468 IWL_DEBUG_RATE(priv, "LQ: Legacy toggle Antenna\n");
b481de9c 1469
3110bef7
GC
1470 if ((tbl->action == IWL_LEGACY_SWITCH_ANTENNA1 &&
1471 tx_chains_num <= 1) ||
1472 (tbl->action == IWL_LEGACY_SWITCH_ANTENNA2 &&
1473 tx_chains_num <= 2))
1474 break;
1475
77626355 1476 /* Don't change antenna if success has been great */
da5dbb97
WYG
1477 if (window->success_ratio >= IWL_RS_GOOD_RATIO &&
1478 !priv->bt_full_concurrent &&
1479 priv->bt_traffic_load ==
1480 IWL_BT_COEX_TRAFFIC_LOAD_NONE)
b481de9c 1481 break;
77626355 1482
77626355 1483 /* Set up search table to try other antenna */
b481de9c
ZY
1484 memcpy(search_tbl, tbl, sz);
1485
aade00ce
GC
1486 if (rs_toggle_antenna(valid_tx_ant,
1487 &search_tbl->current_rate, search_tbl)) {
d6e93399 1488 update_search_tbl_counter = 1;
3110bef7 1489 rs_set_expected_tpt_table(lq_sta, search_tbl);
aade00ce
GC
1490 goto out;
1491 }
a5e8b505 1492 break;
b481de9c 1493 case IWL_LEGACY_SWITCH_SISO:
e1623446 1494 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to SISO\n");
77626355
BC
1495
1496 /* Set up search table to try SISO */
b481de9c 1497 memcpy(search_tbl, tbl, sz);
b481de9c 1498 search_tbl->is_SGI = 0;
c33104f0 1499 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
270243a5 1500 search_tbl, index);
dc2453ae 1501 if (!ret) {
c33104f0 1502 lq_sta->action_counter = 0;
b481de9c 1503 goto out;
dc2453ae 1504 }
b481de9c
ZY
1505
1506 break;
3110bef7
GC
1507 case IWL_LEGACY_SWITCH_MIMO2_AB:
1508 case IWL_LEGACY_SWITCH_MIMO2_AC:
1509 case IWL_LEGACY_SWITCH_MIMO2_BC:
e1623446 1510 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO2\n");
77626355
BC
1511
1512 /* Set up search table to try MIMO */
b481de9c 1513 memcpy(search_tbl, tbl, sz);
b481de9c 1514 search_tbl->is_SGI = 0;
3110bef7
GC
1515
1516 if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AB)
1517 search_tbl->ant_type = ANT_AB;
1518 else if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AC)
1519 search_tbl->ant_type = ANT_AC;
1520 else
1521 search_tbl->ant_type = ANT_BC;
1522
1523 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1524 break;
1525
fde0db31 1526 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
270243a5 1527 search_tbl, index);
dc2453ae 1528 if (!ret) {
c33104f0 1529 lq_sta->action_counter = 0;
b481de9c 1530 goto out;
dc2453ae 1531 }
b481de9c 1532 break;
584a0f00
WYG
1533
1534 case IWL_LEGACY_SWITCH_MIMO3_ABC:
1535 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO3\n");
1536
1537 /* Set up search table to try MIMO3 */
1538 memcpy(search_tbl, tbl, sz);
1539 search_tbl->is_SGI = 0;
1540
1541 search_tbl->ant_type = ANT_ABC;
1542
1543 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1544 break;
1545
1546 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1547 search_tbl, index);
1548 if (!ret) {
1549 lq_sta->action_counter = 0;
1550 goto out;
1551 }
1552 break;
b481de9c
ZY
1553 }
1554 tbl->action++;
584a0f00 1555 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
3110bef7 1556 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
b481de9c
ZY
1557
1558 if (tbl->action == start_action)
1559 break;
1560
1561 }
3110bef7 1562 search_tbl->lq_type = LQ_NONE;
48ed7040 1563 return;
b481de9c 1564
3110bef7
GC
1565out:
1566 lq_sta->search_better_tbl = 1;
b481de9c 1567 tbl->action++;
584a0f00 1568 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
3110bef7 1569 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
d6e93399
MA
1570 if (update_search_tbl_counter)
1571 search_tbl->action = tbl->action;
b481de9c
ZY
1572}
1573
77626355
BC
1574/*
1575 * Try to switch to new modulation mode from SISO
1576 */
48ed7040
NK
1577static void rs_move_siso_to_other(struct iwl_priv *priv,
1578 struct iwl_lq_sta *lq_sta,
1579 struct ieee80211_conf *conf,
1580 struct ieee80211_sta *sta, int index)
b481de9c 1581{
c33104f0 1582 u8 is_green = lq_sta->is_green;
e227ceac
TW
1583 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1584 struct iwl_scale_tbl_info *search_tbl =
1585 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1586 struct iwl_rate_scale_data *window = &(tbl->win[index]);
28e6f489 1587 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
e227ceac
TW
1588 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1589 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
b1a78858 1590 u8 start_action;
b7998c8b 1591 u8 valid_tx_ant = priv->nvm_data->valid_tx_ant;
9e295116 1592 u8 tx_chains_num = priv->hw_params.tx_chains_num;
d6e93399 1593 u8 update_search_tbl_counter = 0;
fde0db31 1594 int ret;
b481de9c 1595
290f599c
JB
1596 switch (priv->bt_traffic_load) {
1597 case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
1598 /* nothing */
1599 break;
1600 case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
1601 /* avoid antenna B unless MIMO */
1602 if (tbl->action == IWL_SISO_SWITCH_ANTENNA2)
383b0874 1603 tbl->action = IWL_SISO_SWITCH_MIMO2_AB;
290f599c
JB
1604 break;
1605 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
1606 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
1607 /* avoid antenna B and MIMO */
d6189124 1608 valid_tx_ant =
b7998c8b 1609 first_antenna(priv->nvm_data->valid_tx_ant);
da5dbb97 1610 if (tbl->action != IWL_SISO_SWITCH_ANTENNA1)
290f599c
JB
1611 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1612 break;
1613 default:
3c6acb61 1614 IWL_ERR(priv, "Invalid BT load %d\n", priv->bt_traffic_load);
290f599c
JB
1615 break;
1616 }
1617
3ad3b92a 1618 if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE &&
46f9381a
WYG
1619 tbl->action > IWL_SISO_SWITCH_ANTENNA2) {
1620 /* stay in SISO */
1621 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1622 }
bee008b7
WYG
1623
1624 /* configure as 1x1 if bt full concurrency */
da5dbb97 1625 if (priv->bt_full_concurrent) {
d6189124 1626 valid_tx_ant =
b7998c8b 1627 first_antenna(priv->nvm_data->valid_tx_ant);
da5dbb97
WYG
1628 if (tbl->action >= IWL_LEGACY_SWITCH_ANTENNA2)
1629 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1630 }
bee008b7 1631
b1a78858 1632 start_action = tbl->action;
b481de9c 1633 for (;;) {
c33104f0 1634 lq_sta->action_counter++;
b481de9c 1635 switch (tbl->action) {
3110bef7
GC
1636 case IWL_SISO_SWITCH_ANTENNA1:
1637 case IWL_SISO_SWITCH_ANTENNA2:
e1623446 1638 IWL_DEBUG_RATE(priv, "LQ: SISO toggle Antenna\n");
3110bef7 1639 if ((tbl->action == IWL_SISO_SWITCH_ANTENNA1 &&
da5dbb97 1640 tx_chains_num <= 1) ||
3110bef7 1641 (tbl->action == IWL_SISO_SWITCH_ANTENNA2 &&
da5dbb97 1642 tx_chains_num <= 2))
3110bef7
GC
1643 break;
1644
da5dbb97
WYG
1645 if (window->success_ratio >= IWL_RS_GOOD_RATIO &&
1646 !priv->bt_full_concurrent &&
1647 priv->bt_traffic_load ==
1648 IWL_BT_COEX_TRAFFIC_LOAD_NONE)
b481de9c 1649 break;
b481de9c
ZY
1650
1651 memcpy(search_tbl, tbl, sz);
aade00ce 1652 if (rs_toggle_antenna(valid_tx_ant,
d6e93399
MA
1653 &search_tbl->current_rate, search_tbl)) {
1654 update_search_tbl_counter = 1;
aade00ce 1655 goto out;
d6e93399 1656 }
a5e8b505 1657 break;
3110bef7
GC
1658 case IWL_SISO_SWITCH_MIMO2_AB:
1659 case IWL_SISO_SWITCH_MIMO2_AC:
1660 case IWL_SISO_SWITCH_MIMO2_BC:
e1623446 1661 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO2\n");
b481de9c 1662 memcpy(search_tbl, tbl, sz);
b481de9c 1663 search_tbl->is_SGI = 0;
3110bef7
GC
1664
1665 if (tbl->action == IWL_SISO_SWITCH_MIMO2_AB)
1666 search_tbl->ant_type = ANT_AB;
1667 else if (tbl->action == IWL_SISO_SWITCH_MIMO2_AC)
1668 search_tbl->ant_type = ANT_AC;
1669 else
1670 search_tbl->ant_type = ANT_BC;
1671
1672 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1673 break;
1674
fde0db31 1675 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
270243a5 1676 search_tbl, index);
3110bef7 1677 if (!ret)
b481de9c
ZY
1678 goto out;
1679 break;
1680 case IWL_SISO_SWITCH_GI:
28e6f489
DH
1681 if (!tbl->is_ht40 && !(ht_cap->cap &
1682 IEEE80211_HT_CAP_SGI_20))
a9841013 1683 break;
28e6f489
DH
1684 if (tbl->is_ht40 && !(ht_cap->cap &
1685 IEEE80211_HT_CAP_SGI_40))
a9841013
EG
1686 break;
1687
e1623446 1688 IWL_DEBUG_RATE(priv, "LQ: SISO toggle SGI/NGI\n");
0c11b4de 1689
b481de9c 1690 memcpy(search_tbl, tbl, sz);
39e88504
GC
1691 if (is_green) {
1692 if (!tbl->is_SGI)
1693 break;
1694 else
15b1687c
WT
1695 IWL_ERR(priv,
1696 "SGI was set in GF+SISO\n");
b481de9c 1697 }
39e88504 1698 search_tbl->is_SGI = !tbl->is_SGI;
eecd6e57 1699 rs_set_expected_tpt_table(lq_sta, search_tbl);
39e88504
GC
1700 if (tbl->is_SGI) {
1701 s32 tpt = lq_sta->last_tpt / 100;
1702 if (tpt >= search_tbl->expected_tpt[index])
1703 break;
1704 }
978785a3
TW
1705 search_tbl->current_rate =
1706 rate_n_flags_from_tbl(priv, search_tbl,
1707 index, is_green);
d6e93399 1708 update_search_tbl_counter = 1;
b481de9c 1709 goto out;
584a0f00
WYG
1710 case IWL_SISO_SWITCH_MIMO3_ABC:
1711 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO3\n");
1712 memcpy(search_tbl, tbl, sz);
1713 search_tbl->is_SGI = 0;
1714 search_tbl->ant_type = ANT_ABC;
1715
1716 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1717 break;
1718
1719 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1720 search_tbl, index);
1721 if (!ret)
1722 goto out;
1723 break;
b481de9c
ZY
1724 }
1725 tbl->action++;
584a0f00 1726 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
3110bef7 1727 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
b481de9c
ZY
1728
1729 if (tbl->action == start_action)
1730 break;
1731 }
3110bef7 1732 search_tbl->lq_type = LQ_NONE;
48ed7040 1733 return;
b481de9c
ZY
1734
1735 out:
3110bef7 1736 lq_sta->search_better_tbl = 1;
b481de9c 1737 tbl->action++;
584a0f00 1738 if (tbl->action > IWL_SISO_SWITCH_MIMO3_ABC)
3110bef7 1739 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
d6e93399
MA
1740 if (update_search_tbl_counter)
1741 search_tbl->action = tbl->action;
b481de9c
ZY
1742}
1743
77626355 1744/*
584a0f00 1745 * Try to switch to new modulation mode from MIMO2
77626355 1746 */
48ed7040
NK
1747static void rs_move_mimo2_to_other(struct iwl_priv *priv,
1748 struct iwl_lq_sta *lq_sta,
1749 struct ieee80211_conf *conf,
1750 struct ieee80211_sta *sta, int index)
b481de9c 1751{
c33104f0 1752 s8 is_green = lq_sta->is_green;
e227ceac
TW
1753 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1754 struct iwl_scale_tbl_info *search_tbl =
1755 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
3110bef7 1756 struct iwl_rate_scale_data *window = &(tbl->win[index]);
28e6f489 1757 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
e227ceac
TW
1758 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1759 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
b1a78858 1760 u8 start_action;
b7998c8b 1761 u8 valid_tx_ant = priv->nvm_data->valid_tx_ant;
9e295116 1762 u8 tx_chains_num = priv->hw_params.tx_chains_num;
d6e93399 1763 u8 update_search_tbl_counter = 0;
aade00ce 1764 int ret;
b481de9c 1765
290f599c
JB
1766 switch (priv->bt_traffic_load) {
1767 case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
1768 /* nothing */
1769 break;
1770 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
1771 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
1772 /* avoid antenna B and MIMO */
da5dbb97
WYG
1773 if (tbl->action != IWL_MIMO2_SWITCH_SISO_A)
1774 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1775 break;
290f599c
JB
1776 case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
1777 /* avoid antenna B unless MIMO */
da5dbb97
WYG
1778 if (tbl->action == IWL_MIMO2_SWITCH_SISO_B ||
1779 tbl->action == IWL_MIMO2_SWITCH_SISO_C)
290f599c
JB
1780 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1781 break;
1782 default:
3c6acb61 1783 IWL_ERR(priv, "Invalid BT load %d\n", priv->bt_traffic_load);
290f599c
JB
1784 break;
1785 }
1786
3ad3b92a 1787 if ((iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE) &&
46f9381a
WYG
1788 (tbl->action < IWL_MIMO2_SWITCH_SISO_A ||
1789 tbl->action > IWL_MIMO2_SWITCH_SISO_C)) {
1790 /* switch in SISO */
1791 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1792 }
bee008b7
WYG
1793
1794 /* configure as 1x1 if bt full concurrency */
1795 if (priv->bt_full_concurrent &&
1796 (tbl->action < IWL_MIMO2_SWITCH_SISO_A ||
1797 tbl->action > IWL_MIMO2_SWITCH_SISO_C))
1798 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1799
b1a78858 1800 start_action = tbl->action;
b481de9c 1801 for (;;) {
c33104f0 1802 lq_sta->action_counter++;
b481de9c 1803 switch (tbl->action) {
3110bef7
GC
1804 case IWL_MIMO2_SWITCH_ANTENNA1:
1805 case IWL_MIMO2_SWITCH_ANTENNA2:
584a0f00 1806 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle Antennas\n");
3110bef7
GC
1807
1808 if (tx_chains_num <= 2)
1809 break;
1810
1811 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1812 break;
1813
1814 memcpy(search_tbl, tbl, sz);
1815 if (rs_toggle_antenna(valid_tx_ant,
d6e93399
MA
1816 &search_tbl->current_rate, search_tbl)) {
1817 update_search_tbl_counter = 1;
3110bef7 1818 goto out;
d6e93399 1819 }
3110bef7
GC
1820 break;
1821 case IWL_MIMO2_SWITCH_SISO_A:
1822 case IWL_MIMO2_SWITCH_SISO_B:
1823 case IWL_MIMO2_SWITCH_SISO_C:
e1623446 1824 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to SISO\n");
0c11b4de 1825
77626355 1826 /* Set up new search table for SISO */
b481de9c 1827 memcpy(search_tbl, tbl, sz);
39e88504 1828
3110bef7 1829 if (tbl->action == IWL_MIMO2_SWITCH_SISO_A)
fde0db31 1830 search_tbl->ant_type = ANT_A;
3110bef7 1831 else if (tbl->action == IWL_MIMO2_SWITCH_SISO_B)
fde0db31 1832 search_tbl->ant_type = ANT_B;
3110bef7
GC
1833 else
1834 search_tbl->ant_type = ANT_C;
1835
1836 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1837 break;
b481de9c 1838
c33104f0 1839 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
270243a5 1840 search_tbl, index);
3110bef7 1841 if (!ret)
b481de9c 1842 goto out;
3110bef7 1843
b481de9c
ZY
1844 break;
1845
3110bef7 1846 case IWL_MIMO2_SWITCH_GI:
28e6f489
DH
1847 if (!tbl->is_ht40 && !(ht_cap->cap &
1848 IEEE80211_HT_CAP_SGI_20))
a9841013 1849 break;
28e6f489
DH
1850 if (tbl->is_ht40 && !(ht_cap->cap &
1851 IEEE80211_HT_CAP_SGI_40))
a9841013
EG
1852 break;
1853
584a0f00 1854 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle SGI/NGI\n");
77626355 1855
584a0f00 1856 /* Set up new search table for MIMO2 */
b481de9c 1857 memcpy(search_tbl, tbl, sz);
39e88504 1858 search_tbl->is_SGI = !tbl->is_SGI;
eecd6e57 1859 rs_set_expected_tpt_table(lq_sta, search_tbl);
77626355
BC
1860 /*
1861 * If active table already uses the fastest possible
1862 * modulation (dual stream with short guard interval),
1863 * and it's working well, there's no need to look
1864 * for a better type of modulation!
1865 */
39e88504 1866 if (tbl->is_SGI) {
c33104f0 1867 s32 tpt = lq_sta->last_tpt / 100;
39e88504
GC
1868 if (tpt >= search_tbl->expected_tpt[index])
1869 break;
b481de9c 1870 }
978785a3
TW
1871 search_tbl->current_rate =
1872 rate_n_flags_from_tbl(priv, search_tbl,
1873 index, is_green);
d6e93399 1874 update_search_tbl_counter = 1;
b481de9c
ZY
1875 goto out;
1876
584a0f00
WYG
1877 case IWL_MIMO2_SWITCH_MIMO3_ABC:
1878 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to MIMO3\n");
1879 memcpy(search_tbl, tbl, sz);
1880 search_tbl->is_SGI = 0;
1881 search_tbl->ant_type = ANT_ABC;
1882
1883 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1884 break;
1885
1886 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1887 search_tbl, index);
1888 if (!ret)
1889 goto out;
1890
1891 break;
b481de9c
ZY
1892 }
1893 tbl->action++;
584a0f00 1894 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
3110bef7 1895 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
b481de9c
ZY
1896
1897 if (tbl->action == start_action)
1898 break;
1899 }
3110bef7 1900 search_tbl->lq_type = LQ_NONE;
48ed7040 1901 return;
b481de9c 1902 out:
3110bef7 1903 lq_sta->search_better_tbl = 1;
b481de9c 1904 tbl->action++;
584a0f00 1905 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
3110bef7 1906 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
d6e93399
MA
1907 if (update_search_tbl_counter)
1908 search_tbl->action = tbl->action;
1909
b481de9c
ZY
1910}
1911
584a0f00
WYG
1912/*
1913 * Try to switch to new modulation mode from MIMO3
1914 */
48ed7040
NK
1915static void rs_move_mimo3_to_other(struct iwl_priv *priv,
1916 struct iwl_lq_sta *lq_sta,
1917 struct ieee80211_conf *conf,
1918 struct ieee80211_sta *sta, int index)
584a0f00
WYG
1919{
1920 s8 is_green = lq_sta->is_green;
1921 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1922 struct iwl_scale_tbl_info *search_tbl =
1923 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1924 struct iwl_rate_scale_data *window = &(tbl->win[index]);
28e6f489 1925 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
584a0f00
WYG
1926 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1927 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
b1a78858 1928 u8 start_action;
b7998c8b 1929 u8 valid_tx_ant = priv->nvm_data->valid_tx_ant;
9e295116 1930 u8 tx_chains_num = priv->hw_params.tx_chains_num;
584a0f00 1931 int ret;
d6e93399 1932 u8 update_search_tbl_counter = 0;
584a0f00 1933
290f599c
JB
1934 switch (priv->bt_traffic_load) {
1935 case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
1936 /* nothing */
1937 break;
1938 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
1939 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
1940 /* avoid antenna B and MIMO */
da5dbb97 1941 if (tbl->action != IWL_MIMO3_SWITCH_SISO_A)
290f599c 1942 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
da5dbb97 1943 break;
290f599c
JB
1944 case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
1945 /* avoid antenna B unless MIMO */
da5dbb97
WYG
1946 if (tbl->action == IWL_MIMO3_SWITCH_SISO_B ||
1947 tbl->action == IWL_MIMO3_SWITCH_SISO_C)
290f599c 1948 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
290f599c
JB
1949 break;
1950 default:
3c6acb61 1951 IWL_ERR(priv, "Invalid BT load %d\n", priv->bt_traffic_load);
290f599c
JB
1952 break;
1953 }
1954
3ad3b92a 1955 if ((iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE) &&
46f9381a
WYG
1956 (tbl->action < IWL_MIMO3_SWITCH_SISO_A ||
1957 tbl->action > IWL_MIMO3_SWITCH_SISO_C)) {
1958 /* switch in SISO */
1959 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
1960 }
bee008b7
WYG
1961
1962 /* configure as 1x1 if bt full concurrency */
1963 if (priv->bt_full_concurrent &&
1964 (tbl->action < IWL_MIMO3_SWITCH_SISO_A ||
1965 tbl->action > IWL_MIMO3_SWITCH_SISO_C))
1966 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
1967
b1a78858 1968 start_action = tbl->action;
584a0f00
WYG
1969 for (;;) {
1970 lq_sta->action_counter++;
1971 switch (tbl->action) {
1972 case IWL_MIMO3_SWITCH_ANTENNA1:
1973 case IWL_MIMO3_SWITCH_ANTENNA2:
1974 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle Antennas\n");
1975
1976 if (tx_chains_num <= 3)
1977 break;
1978
1979 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1980 break;
1981
1982 memcpy(search_tbl, tbl, sz);
1983 if (rs_toggle_antenna(valid_tx_ant,
1984 &search_tbl->current_rate, search_tbl))
1985 goto out;
1986 break;
1987 case IWL_MIMO3_SWITCH_SISO_A:
1988 case IWL_MIMO3_SWITCH_SISO_B:
1989 case IWL_MIMO3_SWITCH_SISO_C:
1990 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to SISO\n");
1991
1992 /* Set up new search table for SISO */
1993 memcpy(search_tbl, tbl, sz);
1994
1995 if (tbl->action == IWL_MIMO3_SWITCH_SISO_A)
1996 search_tbl->ant_type = ANT_A;
1997 else if (tbl->action == IWL_MIMO3_SWITCH_SISO_B)
1998 search_tbl->ant_type = ANT_B;
1999 else
2000 search_tbl->ant_type = ANT_C;
2001
2002 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
2003 break;
2004
2005 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
2006 search_tbl, index);
2007 if (!ret)
2008 goto out;
2009
2010 break;
2011
2012 case IWL_MIMO3_SWITCH_MIMO2_AB:
2013 case IWL_MIMO3_SWITCH_MIMO2_AC:
2014 case IWL_MIMO3_SWITCH_MIMO2_BC:
2015 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to MIMO2\n");
2016
2017 memcpy(search_tbl, tbl, sz);
2018 search_tbl->is_SGI = 0;
2019 if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AB)
2020 search_tbl->ant_type = ANT_AB;
2021 else if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AC)
2022 search_tbl->ant_type = ANT_AC;
2023 else
2024 search_tbl->ant_type = ANT_BC;
2025
2026 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
2027 break;
2028
2029 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
2030 search_tbl, index);
2031 if (!ret)
2032 goto out;
2033
2034 break;
2035
2036 case IWL_MIMO3_SWITCH_GI:
28e6f489
DH
2037 if (!tbl->is_ht40 && !(ht_cap->cap &
2038 IEEE80211_HT_CAP_SGI_20))
584a0f00 2039 break;
28e6f489
DH
2040 if (tbl->is_ht40 && !(ht_cap->cap &
2041 IEEE80211_HT_CAP_SGI_40))
584a0f00
WYG
2042 break;
2043
2044 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle SGI/NGI\n");
2045
2046 /* Set up new search table for MIMO */
2047 memcpy(search_tbl, tbl, sz);
2048 search_tbl->is_SGI = !tbl->is_SGI;
2049 rs_set_expected_tpt_table(lq_sta, search_tbl);
2050 /*
2051 * If active table already uses the fastest possible
2052 * modulation (dual stream with short guard interval),
2053 * and it's working well, there's no need to look
2054 * for a better type of modulation!
2055 */
2056 if (tbl->is_SGI) {
2057 s32 tpt = lq_sta->last_tpt / 100;
2058 if (tpt >= search_tbl->expected_tpt[index])
2059 break;
2060 }
2061 search_tbl->current_rate =
2062 rate_n_flags_from_tbl(priv, search_tbl,
2063 index, is_green);
d6e93399 2064 update_search_tbl_counter = 1;
584a0f00
WYG
2065 goto out;
2066 }
2067 tbl->action++;
2068 if (tbl->action > IWL_MIMO3_SWITCH_GI)
2069 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
2070
2071 if (tbl->action == start_action)
2072 break;
2073 }
2074 search_tbl->lq_type = LQ_NONE;
48ed7040 2075 return;
584a0f00
WYG
2076 out:
2077 lq_sta->search_better_tbl = 1;
2078 tbl->action++;
2079 if (tbl->action > IWL_MIMO3_SWITCH_GI)
2080 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
d6e93399
MA
2081 if (update_search_tbl_counter)
2082 search_tbl->action = tbl->action;
584a0f00
WYG
2083}
2084
77626355
BC
2085/*
2086 * Check whether we should continue using same modulation mode, or
2087 * begin search for a new mode, based on:
2088 * 1) # tx successes or failures while using this mode
2089 * 2) # times calling this function
2090 * 3) elapsed time in this mode (not used, for now)
2091 */
da5dbb97 2092static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search)
b481de9c 2093{
e227ceac 2094 struct iwl_scale_tbl_info *tbl;
b481de9c
ZY
2095 int i;
2096 int active_tbl;
2097 int flush_interval_passed = 0;
bf403db8 2098 struct iwl_priv *priv;
b481de9c 2099
bf403db8 2100 priv = lq_sta->drv;
c33104f0 2101 active_tbl = lq_sta->active_tbl;
b481de9c 2102
c33104f0 2103 tbl = &(lq_sta->lq_info[active_tbl]);
b481de9c 2104
77626355 2105 /* If we've been disallowing search, see if we should now allow it */
c33104f0 2106 if (lq_sta->stay_in_tbl) {
b481de9c 2107
77626355 2108 /* Elapsed time using current modulation mode */
c33104f0 2109 if (lq_sta->flush_timer)
b481de9c 2110 flush_interval_passed =
447fee70
MA
2111 time_after(jiffies,
2112 (unsigned long)(lq_sta->flush_timer +
b481de9c
ZY
2113 IWL_RATE_SCALE_FLUSH_INTVL));
2114
77626355
BC
2115 /*
2116 * Check if we should allow search for new modulation mode.
2117 * If many frames have failed or succeeded, or we've used
2118 * this same modulation for a long time, allow search, and
2119 * reset history stats that keep track of whether we should
2120 * allow a new search. Also (below) reset all bitmaps and
2121 * stats in active history.
2122 */
da5dbb97
WYG
2123 if (force_search ||
2124 (lq_sta->total_failed > lq_sta->max_failure_limit) ||
c33104f0
TW
2125 (lq_sta->total_success > lq_sta->max_success_limit) ||
2126 ((!lq_sta->search_better_tbl) && (lq_sta->flush_timer)
b481de9c 2127 && (flush_interval_passed))) {
0ca24daf 2128 IWL_DEBUG_RATE(priv, "LQ: stay is expired %d %d %d\n",
c33104f0
TW
2129 lq_sta->total_failed,
2130 lq_sta->total_success,
b481de9c 2131 flush_interval_passed);
77626355
BC
2132
2133 /* Allow search for new mode */
c33104f0
TW
2134 lq_sta->stay_in_tbl = 0; /* only place reset */
2135 lq_sta->total_failed = 0;
2136 lq_sta->total_success = 0;
2137 lq_sta->flush_timer = 0;
77626355
BC
2138
2139 /*
2140 * Else if we've used this modulation mode enough repetitions
2141 * (regardless of elapsed time or success/failure), reset
2142 * history bitmaps and rate-specific stats for all rates in
2143 * active table.
2144 */
403ab56b 2145 } else {
c33104f0
TW
2146 lq_sta->table_count++;
2147 if (lq_sta->table_count >=
2148 lq_sta->table_count_limit) {
2149 lq_sta->table_count = 0;
b481de9c 2150
e1623446 2151 IWL_DEBUG_RATE(priv, "LQ: stay in table clear win\n");
b481de9c
ZY
2152 for (i = 0; i < IWL_RATE_COUNT; i++)
2153 rs_rate_scale_clear_window(
2154 &(tbl->win[i]));
2155 }
2156 }
2157
77626355
BC
2158 /* If transitioning to allow "search", reset all history
2159 * bitmaps and stats in active table (this will become the new
2160 * "search" table). */
c33104f0 2161 if (!lq_sta->stay_in_tbl) {
b481de9c
ZY
2162 for (i = 0; i < IWL_RATE_COUNT; i++)
2163 rs_rate_scale_clear_window(&(tbl->win[i]));
2164 }
2165 }
2166}
2167
e3139fe7
WYG
2168/*
2169 * setup rate table in uCode
e3139fe7 2170 */
70817b5e
JB
2171static void rs_update_rate_tbl(struct iwl_priv *priv,
2172 struct iwl_rxon_context *ctx,
2173 struct iwl_lq_sta *lq_sta,
2174 struct iwl_scale_tbl_info *tbl,
2175 int index, u8 is_green)
e3139fe7
WYG
2176{
2177 u32 rate;
2178
2179 /* Update uCode's rate table. */
2180 rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
2181 rs_fill_link_cmd(priv, lq_sta, rate);
7e6a5886 2182 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_ASYNC, false);
e3139fe7
WYG
2183}
2184
77626355
BC
2185/*
2186 * Do rate scaling and search for new modulation mode.
2187 */
c79dd5b5 2188static void rs_rate_scale_perform(struct iwl_priv *priv,
514d65c1 2189 struct sk_buff *skb,
4b7679a5
JB
2190 struct ieee80211_sta *sta,
2191 struct iwl_lq_sta *lq_sta)
b481de9c 2192{
4b7679a5 2193 struct ieee80211_hw *hw = priv->hw;
270243a5 2194 struct ieee80211_conf *conf = &hw->conf;
514d65c1
GS
2195 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2196 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
b481de9c
ZY
2197 int low = IWL_RATE_INVALID;
2198 int high = IWL_RATE_INVALID;
2199 int index;
2200 int i;
e227ceac 2201 struct iwl_rate_scale_data *window = NULL;
b481de9c
ZY
2202 int current_tpt = IWL_INVALID_VALUE;
2203 int low_tpt = IWL_INVALID_VALUE;
2204 int high_tpt = IWL_INVALID_VALUE;
2205 u32 fail_count;
2206 s8 scale_action = 0;
fd7c8a40 2207 u16 rate_mask;
b481de9c 2208 u8 update_lq = 0;
e227ceac 2209 struct iwl_scale_tbl_info *tbl, *tbl1;
b481de9c 2210 u16 rate_scale_index_msk = 0;
b481de9c
ZY
2211 u8 is_green = 0;
2212 u8 active_tbl = 0;
2213 u8 done_search = 0;
2214 u16 high_low;
a5e8b505 2215 s32 sr;
5f85a789 2216 u8 tid = IWL_MAX_TID_COUNT;
86b4766b 2217 struct iwl_tid_data *tid_data;
7e6a5886 2218 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
c6892906 2219 struct iwl_rxon_context *ctx = sta_priv->ctx;
b481de9c 2220
e1623446 2221 IWL_DEBUG_RATE(priv, "rate scale calculate new rate for skb\n");
b481de9c 2222
514d65c1 2223 /* Send management frames and NO_ACK data using lowest rate. */
417f114b
TW
2224 /* TODO: this could probably be improved.. */
2225 if (!ieee80211_is_data(hdr->frame_control) ||
514d65c1 2226 info->flags & IEEE80211_TX_CTL_NO_ACK)
b481de9c 2227 return;
b481de9c 2228
4b7679a5 2229 lq_sta->supp_rates = sta->supp_rates[lq_sta->band];
b481de9c 2230
faa29718 2231 tid = rs_tl_add_packet(lq_sta, hdr);
5f85a789
EG
2232 if ((tid != IWL_MAX_TID_COUNT) &&
2233 (lq_sta->tx_agg_tid_en & (1 << tid))) {
04cf6824 2234 tid_data = &priv->tid_data[lq_sta->lq.sta_id][tid];
e3949d62
DH
2235 if (tid_data->agg.state == IWL_AGG_OFF)
2236 lq_sta->is_agg = 0;
2237 else
2238 lq_sta->is_agg = 1;
2239 } else
2240 lq_sta->is_agg = 0;
faa29718 2241
77626355
BC
2242 /*
2243 * Select rate-scale / modulation-mode table to work with in
2244 * the rest of this function: "search" if searching for better
2245 * modulation mode, or "active" if doing rate scaling within a mode.
2246 */
c33104f0
TW
2247 if (!lq_sta->search_better_tbl)
2248 active_tbl = lq_sta->active_tbl;
b481de9c 2249 else
c33104f0 2250 active_tbl = 1 - lq_sta->active_tbl;
b481de9c 2251
c33104f0 2252 tbl = &(lq_sta->lq_info[active_tbl]);
2681b20b
WYG
2253 if (is_legacy(tbl->lq_type))
2254 lq_sta->is_green = 0;
2255 else
7e6a5886 2256 lq_sta->is_green = rs_use_green(sta);
c33104f0 2257 is_green = lq_sta->is_green;
b481de9c 2258
77626355 2259 /* current tx rate */
b7e35008 2260 index = lq_sta->last_txrate_idx;
b481de9c 2261
e1623446 2262 IWL_DEBUG_RATE(priv, "Rate scale index %d for type %d\n", index,
b481de9c
ZY
2263 tbl->lq_type);
2264
77626355 2265 /* rates available for this association, and for modulation mode */
eecd6e57 2266 rate_mask = rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
b481de9c 2267
91dd6c27 2268 IWL_DEBUG_RATE(priv, "mask 0x%04X\n", rate_mask);
b481de9c
ZY
2269
2270 /* mask with station rate restriction */
2271 if (is_legacy(tbl->lq_type)) {
57fbcce3 2272 if (lq_sta->band == NL80211_BAND_5GHZ)
77626355 2273 /* supp_rates has no CCK bits in A mode */
b481de9c 2274 rate_scale_index_msk = (u16) (rate_mask &
c33104f0 2275 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
b481de9c
ZY
2276 else
2277 rate_scale_index_msk = (u16) (rate_mask &
c33104f0 2278 lq_sta->supp_rates);
b481de9c
ZY
2279
2280 } else
2281 rate_scale_index_msk = rate_mask;
2282
2283 if (!rate_scale_index_msk)
2284 rate_scale_index_msk = rate_mask;
2285
07bc28ed 2286 if (!((1 << index) & rate_scale_index_msk)) {
15b1687c 2287 IWL_ERR(priv, "Current Rate is not valid\n");
e3139fe7
WYG
2288 if (lq_sta->search_better_tbl) {
2289 /* revert to active table if search table is not valid*/
2290 tbl->lq_type = LQ_NONE;
2291 lq_sta->search_better_tbl = 0;
2292 tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
2293 /* get "active" rate info */
2294 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
70817b5e
JB
2295 rs_update_rate_tbl(priv, ctx, lq_sta, tbl,
2296 index, is_green);
e3139fe7 2297 }
07bc28ed 2298 return;
b481de9c
ZY
2299 }
2300
77626355 2301 /* Get expected throughput table and history window for current rate */
07bc28ed 2302 if (!tbl->expected_tpt) {
15b1687c 2303 IWL_ERR(priv, "tbl->expected_tpt is NULL\n");
07bc28ed
GC
2304 return;
2305 }
b481de9c 2306
c6ec7a9b
AM
2307 /* force user max rate if set by user */
2308 if ((lq_sta->max_rate_idx != -1) &&
2309 (lq_sta->max_rate_idx < index)) {
2310 index = lq_sta->max_rate_idx;
2311 update_lq = 1;
2312 window = &(tbl->win[index]);
2313 goto lq_update;
2314 }
2315
b481de9c
ZY
2316 window = &(tbl->win[index]);
2317
77626355
BC
2318 /*
2319 * If there is not enough history to calculate actual average
2320 * throughput, keep analyzing results of more tx frames, without
2321 * changing rate or mode (bypass most of the rest of this function).
2322 * Set up new rate table in uCode only if old rate is not supported
2323 * in current association (use new rate found above).
2324 */
b481de9c 2325 fail_count = window->counter - window->success_counter;
07bc28ed
GC
2326 if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
2327 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
e1623446 2328 IWL_DEBUG_RATE(priv, "LQ: still below TH. succ=%d total=%d "
b481de9c
ZY
2329 "for index %d\n",
2330 window->success_counter, window->counter, index);
77626355
BC
2331
2332 /* Can't calculate this yet; not enough history */
b481de9c 2333 window->average_tpt = IWL_INVALID_VALUE;
77626355
BC
2334
2335 /* Should we stay with this modulation mode,
2336 * or search for a new one? */
da5dbb97 2337 rs_stay_in_table(lq_sta, false);
77626355 2338
b481de9c 2339 goto out;
3110bef7 2340 }
77626355
BC
2341 /* Else we have enough samples; calculate estimate of
2342 * actual average throughput */
3d79b2a9
RC
2343 if (window->average_tpt != ((window->success_ratio *
2344 tbl->expected_tpt[index] + 64) / 128)) {
2345 IWL_ERR(priv, "expected_tpt should have been calculated by now\n");
2346 window->average_tpt = ((window->success_ratio *
2347 tbl->expected_tpt[index] + 64) / 128);
2348 }
b481de9c 2349
77626355 2350 /* If we are searching for better modulation mode, check success. */
46f9381a 2351 if (lq_sta->search_better_tbl &&
3ad3b92a 2352 (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_MULTI)) {
77626355
BC
2353 /* If good success, continue using the "search" mode;
2354 * no need to send new link quality command, since we're
2355 * continuing to use the setup that we've been trying. */
07bc28ed
GC
2356 if (window->average_tpt > lq_sta->last_tpt) {
2357
e1623446 2358 IWL_DEBUG_RATE(priv, "LQ: SWITCHING TO NEW TABLE "
07bc28ed
GC
2359 "suc=%d cur-tpt=%d old-tpt=%d\n",
2360 window->success_ratio,
2361 window->average_tpt,
2362 lq_sta->last_tpt);
2363
2364 if (!is_legacy(tbl->lq_type))
c33104f0 2365 lq_sta->enable_counter = 1;
07bc28ed 2366
77626355 2367 /* Swap tables; "search" becomes "active" */
c33104f0 2368 lq_sta->active_tbl = active_tbl;
b481de9c 2369 current_tpt = window->average_tpt;
77626355
BC
2370
2371 /* Else poor success; go back to mode in "active" table */
b481de9c 2372 } else {
07bc28ed 2373
e1623446 2374 IWL_DEBUG_RATE(priv, "LQ: GOING BACK TO THE OLD TABLE "
07bc28ed
GC
2375 "suc=%d cur-tpt=%d old-tpt=%d\n",
2376 window->success_ratio,
2377 window->average_tpt,
2378 lq_sta->last_tpt);
2379
77626355 2380 /* Nullify "search" table */
b481de9c 2381 tbl->lq_type = LQ_NONE;
77626355
BC
2382
2383 /* Revert to "active" table */
c33104f0
TW
2384 active_tbl = lq_sta->active_tbl;
2385 tbl = &(lq_sta->lq_info[active_tbl]);
b481de9c 2386
77626355 2387 /* Revert to "active" rate and throughput info */
e7d326ac 2388 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
c33104f0 2389 current_tpt = lq_sta->last_tpt;
b481de9c 2390
77626355 2391 /* Need to set up a new rate table in uCode */
b481de9c 2392 update_lq = 1;
b481de9c 2393 }
77626355
BC
2394
2395 /* Either way, we've made a decision; modulation mode
2396 * search is done, allow rate adjustment next time. */
c33104f0 2397 lq_sta->search_better_tbl = 0;
77626355 2398 done_search = 1; /* Don't switch modes below! */
b481de9c
ZY
2399 goto lq_update;
2400 }
2401
77626355
BC
2402 /* (Else) not in search of better modulation mode, try for better
2403 * starting rate, while staying in this mode. */
bf403db8 2404 high_low = rs_get_adjacent_rate(priv, index, rate_scale_index_msk,
b481de9c
ZY
2405 tbl->lq_type);
2406 low = high_low & 0xff;
2407 high = (high_low >> 8) & 0xff;
2408
c6ec7a9b
AM
2409 /* If user set max rate, dont allow higher than user constrain */
2410 if ((lq_sta->max_rate_idx != -1) &&
2411 (lq_sta->max_rate_idx < high))
2412 high = IWL_RATE_INVALID;
2413
a5e8b505
GC
2414 sr = window->success_ratio;
2415
77626355 2416 /* Collect measured throughputs for current and adjacent rates */
b481de9c 2417 current_tpt = window->average_tpt;
b481de9c
ZY
2418 if (low != IWL_RATE_INVALID)
2419 low_tpt = tbl->win[low].average_tpt;
b481de9c
ZY
2420 if (high != IWL_RATE_INVALID)
2421 high_tpt = tbl->win[high].average_tpt;
2422
a5e8b505 2423 scale_action = 0;
b481de9c 2424
77626355 2425 /* Too many failures, decrease rate */
a5e8b505 2426 if ((sr <= IWL_RATE_DECREASE_TH) || (current_tpt == 0)) {
e1623446 2427 IWL_DEBUG_RATE(priv, "decrease rate because of low success_ratio\n");
b481de9c 2428 scale_action = -1;
77626355
BC
2429
2430 /* No throughput measured yet for adjacent rates; try increase. */
b481de9c 2431 } else if ((low_tpt == IWL_INVALID_VALUE) &&
a5e8b505
GC
2432 (high_tpt == IWL_INVALID_VALUE)) {
2433
2434 if (high != IWL_RATE_INVALID && sr >= IWL_RATE_INCREASE_TH)
2435 scale_action = 1;
2436 else if (low != IWL_RATE_INVALID)
8fe72311 2437 scale_action = 0;
a5e8b505 2438 }
77626355
BC
2439
2440 /* Both adjacent throughputs are measured, but neither one has better
2441 * throughput; we're using the best rate, don't change it! */
b481de9c
ZY
2442 else if ((low_tpt != IWL_INVALID_VALUE) &&
2443 (high_tpt != IWL_INVALID_VALUE) &&
2444 (low_tpt < current_tpt) &&
2445 (high_tpt < current_tpt))
2446 scale_action = 0;
77626355
BC
2447
2448 /* At least one adjacent rate's throughput is measured,
2449 * and may have better performance. */
b481de9c 2450 else {
77626355 2451 /* Higher adjacent rate's throughput is measured */
b481de9c 2452 if (high_tpt != IWL_INVALID_VALUE) {
77626355 2453 /* Higher rate has better throughput */
a5e8b505
GC
2454 if (high_tpt > current_tpt &&
2455 sr >= IWL_RATE_INCREASE_TH) {
b481de9c 2456 scale_action = 1;
a5e8b505 2457 } else {
8fe72311 2458 scale_action = 0;
b481de9c 2459 }
77626355
BC
2460
2461 /* Lower adjacent rate's throughput is measured */
b481de9c 2462 } else if (low_tpt != IWL_INVALID_VALUE) {
77626355 2463 /* Lower rate has better throughput */
b481de9c 2464 if (low_tpt > current_tpt) {
e1623446
TW
2465 IWL_DEBUG_RATE(priv,
2466 "decrease rate because of low tpt\n");
b481de9c 2467 scale_action = -1;
a5e8b505 2468 } else if (sr >= IWL_RATE_INCREASE_TH) {
b481de9c 2469 scale_action = 1;
a5e8b505 2470 }
b481de9c
ZY
2471 }
2472 }
2473
77626355
BC
2474 /* Sanity check; asked for decrease, but success rate or throughput
2475 * has been good at old rate. Don't change it. */
a5e8b505
GC
2476 if ((scale_action == -1) && (low != IWL_RATE_INVALID) &&
2477 ((sr > IWL_RATE_HIGH_TH) ||
b481de9c 2478 (current_tpt > (100 * tbl->expected_tpt[low]))))
b481de9c 2479 scale_action = 0;
46f9381a
WYG
2480 if (!iwl_ht_enabled(priv) && !is_legacy(tbl->lq_type))
2481 scale_action = -1;
3ad3b92a 2482 if (iwl_tx_ant_restriction(priv) != IWL_ANT_OK_MULTI &&
46f9381a
WYG
2483 (is_mimo2(tbl->lq_type) || is_mimo3(tbl->lq_type)))
2484 scale_action = -1;
290f599c 2485
da5dbb97
WYG
2486 if ((priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH) &&
2487 (is_mimo2(tbl->lq_type) || is_mimo3(tbl->lq_type))) {
2488 if (lq_sta->last_bt_traffic > priv->bt_traffic_load) {
2489 /*
2490 * don't set scale_action, don't want to scale up if
2491 * the rate scale doesn't otherwise think that is a
2492 * good idea.
2493 */
2494 } else if (lq_sta->last_bt_traffic <= priv->bt_traffic_load) {
2495 scale_action = -1;
2496 }
2497 }
2498 lq_sta->last_bt_traffic = priv->bt_traffic_load;
2499
2500 if ((priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH) &&
2501 (is_mimo2(tbl->lq_type) || is_mimo3(tbl->lq_type))) {
2502 /* search for a new modulation */
2503 rs_stay_in_table(lq_sta, true);
2504 goto lq_update;
290f599c
JB
2505 }
2506
b481de9c
ZY
2507 switch (scale_action) {
2508 case -1:
77626355 2509 /* Decrease starting rate, update uCode's rate table */
b481de9c
ZY
2510 if (low != IWL_RATE_INVALID) {
2511 update_lq = 1;
2512 index = low;
2513 }
447fee70 2514
b481de9c
ZY
2515 break;
2516 case 1:
77626355 2517 /* Increase starting rate, update uCode's rate table */
b481de9c
ZY
2518 if (high != IWL_RATE_INVALID) {
2519 update_lq = 1;
2520 index = high;
2521 }
2522
2523 break;
2524 case 0:
77626355 2525 /* No change */
b481de9c
ZY
2526 default:
2527 break;
2528 }
2529
e1623446 2530 IWL_DEBUG_RATE(priv, "choose rate scale index %d action %d low %d "
b481de9c
ZY
2531 "high %d type %d\n",
2532 index, scale_action, low, high, tbl->lq_type);
2533
a5e8b505 2534lq_update:
77626355 2535 /* Replace uCode's rate table for the destination station. */
e3139fe7 2536 if (update_lq)
70817b5e 2537 rs_update_rate_tbl(priv, ctx, lq_sta, tbl, index, is_green);
77626355 2538
3ad3b92a 2539 if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_MULTI) {
46f9381a
WYG
2540 /* Should we stay with this modulation mode,
2541 * or search for a new one? */
da5dbb97 2542 rs_stay_in_table(lq_sta, false);
46f9381a 2543 }
77626355
BC
2544 /*
2545 * Search for new modulation mode if we're:
2546 * 1) Not changing rates right now
2547 * 2) Not just finishing up a search
2548 * 3) Allowing a new search
2549 */
47cfd463 2550 if (!update_lq && !done_search && !lq_sta->stay_in_tbl && window->counter) {
77626355 2551 /* Save current throughput to compare with "search" throughput*/
c33104f0 2552 lq_sta->last_tpt = current_tpt;
b481de9c 2553
77626355
BC
2554 /* Select a new "search" modulation mode to try.
2555 * If one is found, set up the new "search" table. */
b481de9c 2556 if (is_legacy(tbl->lq_type))
c33104f0 2557 rs_move_legacy_other(priv, lq_sta, conf, sta, index);
b481de9c 2558 else if (is_siso(tbl->lq_type))
c33104f0 2559 rs_move_siso_to_other(priv, lq_sta, conf, sta, index);
584a0f00
WYG
2560 else if (is_mimo2(tbl->lq_type))
2561 rs_move_mimo2_to_other(priv, lq_sta, conf, sta, index);
b481de9c 2562 else
584a0f00 2563 rs_move_mimo3_to_other(priv, lq_sta, conf, sta, index);
b481de9c 2564
77626355 2565 /* If new "search" mode was selected, set up in uCode table */
c33104f0 2566 if (lq_sta->search_better_tbl) {
77626355 2567 /* Access the "search" table, clear its history. */
c33104f0 2568 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
b481de9c
ZY
2569 for (i = 0; i < IWL_RATE_COUNT; i++)
2570 rs_rate_scale_clear_window(&(tbl->win[i]));
2571
77626355 2572 /* Use new "search" start rate */
e7d326ac 2573 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
b481de9c 2574
e1623446 2575 IWL_DEBUG_RATE(priv, "Switch current mcs: %X index: %d\n",
39e88504 2576 tbl->current_rate, index);
07bc28ed 2577 rs_fill_link_cmd(priv, lq_sta, tbl->current_rate);
7e6a5886 2578 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_ASYNC, false);
447fee70
MA
2579 } else
2580 done_search = 1;
2581 }
b481de9c 2582
447fee70 2583 if (done_search && !lq_sta->stay_in_tbl) {
77626355
BC
2584 /* If the "active" (non-search) mode was legacy,
2585 * and we've tried switching antennas,
2586 * but we haven't been able to try HT modes (not available),
2587 * stay with best antenna legacy modulation for a while
2588 * before next round of mode comparisons. */
c33104f0 2589 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
de27e64e 2590 if (is_legacy(tbl1->lq_type) && !conf_is_ht(conf) &&
d6e93399 2591 lq_sta->action_counter > tbl1->max_search) {
e1623446 2592 IWL_DEBUG_RATE(priv, "LQ: STAY in legacy table\n");
bf403db8 2593 rs_set_stay_in_table(priv, 1, lq_sta);
b481de9c
ZY
2594 }
2595
77626355
BC
2596 /* If we're in an HT mode, and all 3 mode switch actions
2597 * have been tried and compared, stay in this best modulation
2598 * mode for a while before next round of mode comparisons. */
c33104f0 2599 if (lq_sta->enable_counter &&
46f9381a
WYG
2600 (lq_sta->action_counter >= tbl1->max_search) &&
2601 iwl_ht_enabled(priv)) {
0c11b4de
RR
2602 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2603 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
5f85a789
EG
2604 (tid != IWL_MAX_TID_COUNT)) {
2605 u8 sta_id = lq_sta->lq.sta_id;
04cf6824 2606 tid_data = &priv->tid_data[sta_id][tid];
86b4766b
WYG
2607 if (tid_data->agg.state == IWL_AGG_OFF) {
2608 IWL_DEBUG_RATE(priv,
2609 "try to aggregate tid %d\n",
2610 tid);
2611 rs_tl_turn_on_agg(priv, tid,
2612 lq_sta, sta);
2613 }
b481de9c 2614 }
bf403db8 2615 rs_set_stay_in_table(priv, 0, lq_sta);
b481de9c 2616 }
b481de9c
ZY
2617 }
2618
2619out:
978785a3 2620 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
c3322b30 2621 lq_sta->last_txrate_idx = index;
b481de9c
ZY
2622}
2623
fe6b23dd
RC
2624/**
2625 * rs_initialize_lq - Initialize a station's hardware rate table
2626 *
2627 * The uCode's station table contains a table of fallback rates
2628 * for automatic fallback during transmission.
2629 *
2630 * NOTE: This sets up a default set of values. These will be replaced later
2631 * if the driver's iwl-agn-rs rate scaling algorithm is used, instead of
2632 * rc80211_simple.
2633 *
2634 * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
2635 * calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
2636 * which requires station table entry to exist).
2637 */
c79dd5b5 2638static void rs_initialize_lq(struct iwl_priv *priv,
4b7679a5
JB
2639 struct ieee80211_sta *sta,
2640 struct iwl_lq_sta *lq_sta)
b481de9c 2641{
e227ceac 2642 struct iwl_scale_tbl_info *tbl;
b481de9c 2643 int rate_idx;
aade00ce 2644 int i;
39e88504 2645 u32 rate;
7e6a5886 2646 u8 use_green = rs_use_green(sta);
aade00ce
GC
2647 u8 active_tbl = 0;
2648 u8 valid_tx_ant;
7e6a5886
JB
2649 struct iwl_station_priv *sta_priv;
2650 struct iwl_rxon_context *ctx;
b481de9c 2651
4b7679a5 2652 if (!sta || !lq_sta)
7e6a5886
JB
2653 return;
2654
2655 sta_priv = (void *)sta->drv_priv;
c6892906 2656 ctx = sta_priv->ctx;
b481de9c 2657
b7e35008 2658 i = lq_sta->last_txrate_idx;
b481de9c 2659
b7998c8b 2660 valid_tx_ant = priv->nvm_data->valid_tx_ant;
aade00ce 2661
c33104f0
TW
2662 if (!lq_sta->search_better_tbl)
2663 active_tbl = lq_sta->active_tbl;
b481de9c 2664 else
c33104f0 2665 active_tbl = 1 - lq_sta->active_tbl;
b481de9c 2666
c33104f0 2667 tbl = &(lq_sta->lq_info[active_tbl]);
b481de9c
ZY
2668
2669 if ((i < 0) || (i >= IWL_RATE_COUNT))
2670 i = 0;
2671
1826dcc0 2672 rate = iwl_rates[i].plcp;
eb48dcaf
WT
2673 tbl->ant_type = first_antenna(valid_tx_ant);
2674 rate |= tbl->ant_type << RATE_MCS_ANT_POS;
b481de9c
ZY
2675
2676 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
39e88504 2677 rate |= RATE_MCS_CCK_MSK;
b481de9c 2678
39e88504 2679 rs_get_tbl_info_from_mcs(rate, priv->band, tbl, &rate_idx);
aade00ce
GC
2680 if (!rs_is_valid_ant(valid_tx_ant, tbl->ant_type))
2681 rs_toggle_antenna(valid_tx_ant, &rate, tbl);
b481de9c 2682
978785a3 2683 rate = rate_n_flags_from_tbl(priv, tbl, rate_idx, use_green);
39e88504 2684 tbl->current_rate = rate;
eecd6e57 2685 rs_set_expected_tpt_table(lq_sta, tbl);
07bc28ed 2686 rs_fill_link_cmd(NULL, lq_sta, rate);
fe6b23dd 2687 priv->stations[lq_sta->lq.sta_id].lq = &lq_sta->lq;
a1022927 2688 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, 0, true);
b481de9c
ZY
2689}
2690
e6a9854b
JB
2691static void rs_get_rate(void *priv_r, struct ieee80211_sta *sta, void *priv_sta,
2692 struct ieee80211_tx_rate_control *txrc)
b481de9c
ZY
2693{
2694
e6a9854b
JB
2695 struct sk_buff *skb = txrc->skb;
2696 struct ieee80211_supported_band *sband = txrc->sband;
d0f76d68
EG
2697 struct iwl_op_mode *op_mode __maybe_unused =
2698 (struct iwl_op_mode *)priv_r;
2699 struct iwl_priv *priv __maybe_unused = IWL_OP_MODE_GET_DVM(op_mode);
e6a9854b 2700 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
417f114b
TW
2701 struct iwl_lq_sta *lq_sta = priv_sta;
2702 int rate_idx;
b481de9c 2703
e1623446 2704 IWL_DEBUG_RATE_LIMIT(priv, "rate scale calculate new rate for skb\n");
b481de9c 2705
c6ec7a9b
AM
2706 /* Get max rate if user set max rate */
2707 if (lq_sta) {
ce1d834f 2708 lq_sta->max_rate_idx = fls(txrc->rate_idx_mask) - 1;
57fbcce3 2709 if ((sband->band == NL80211_BAND_5GHZ) &&
c6ec7a9b
AM
2710 (lq_sta->max_rate_idx != -1))
2711 lq_sta->max_rate_idx += IWL_FIRST_OFDM_RATE;
2712 if ((lq_sta->max_rate_idx < 0) ||
2713 (lq_sta->max_rate_idx >= IWL_RATE_COUNT))
2714 lq_sta->max_rate_idx = -1;
2715 }
2716
fe6b23dd
RC
2717 /* Treat uninitialized rate scaling data same as non-existing. */
2718 if (lq_sta && !lq_sta->drv) {
2719 IWL_DEBUG_RATE(priv, "Rate scaling not initialized yet.\n");
2720 priv_sta = NULL;
2721 }
2722
417f114b 2723 rate_idx = lq_sta->last_txrate_idx;
b481de9c 2724
a9c146b3 2725 if (lq_sta->last_rate_n_flags & RATE_MCS_HT_MSK) {
417f114b 2726 rate_idx -= IWL_FIRST_OFDM_RATE;
a9c146b3
WYG
2727 /* 6M and 9M shared same MCS index */
2728 rate_idx = (rate_idx > 0) ? (rate_idx - 1) : 0;
2729 if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
2730 IWL_RATE_MIMO3_6M_PLCP)
2731 rate_idx = rate_idx + (2 * MCS_INDEX_PER_STREAM);
2732 else if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
2733 IWL_RATE_MIMO2_6M_PLCP)
2734 rate_idx = rate_idx + MCS_INDEX_PER_STREAM;
2735 info->control.rates[0].flags = IEEE80211_TX_RC_MCS;
2736 if (lq_sta->last_rate_n_flags & RATE_MCS_SGI_MSK)
2737 info->control.rates[0].flags |= IEEE80211_TX_RC_SHORT_GI;
2738 if (lq_sta->last_rate_n_flags & RATE_MCS_DUP_MSK)
2739 info->control.rates[0].flags |= IEEE80211_TX_RC_DUP_DATA;
7aafef1c 2740 if (lq_sta->last_rate_n_flags & RATE_MCS_HT40_MSK)
a9c146b3
WYG
2741 info->control.rates[0].flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
2742 if (lq_sta->last_rate_n_flags & RATE_MCS_GF_MSK)
2743 info->control.rates[0].flags |= IEEE80211_TX_RC_GREEN_FIELD;
2744 } else {
5027309b
DH
2745 /* Check for invalid rates */
2746 if ((rate_idx < 0) || (rate_idx >= IWL_RATE_COUNT_LEGACY) ||
57fbcce3 2747 ((sband->band == NL80211_BAND_5GHZ) &&
5027309b 2748 (rate_idx < IWL_FIRST_OFDM_RATE)))
a9c146b3 2749 rate_idx = rate_lowest_index(sband, sta);
5027309b 2750 /* On valid 5 GHz rate, adjust index */
57fbcce3 2751 else if (sband->band == NL80211_BAND_5GHZ)
a9c146b3 2752 rate_idx -= IWL_FIRST_OFDM_RATE;
7ebaeff8 2753 info->control.rates[0].flags = 0;
a9c146b3 2754 }
417f114b 2755 info->control.rates[0].idx = rate_idx;
622ebe99 2756 info->control.rates[0].count = 1;
b481de9c
ZY
2757}
2758
4b7679a5
JB
2759static void *rs_alloc_sta(void *priv_rate, struct ieee80211_sta *sta,
2760 gfp_t gfp)
b481de9c 2761{
8d9698b3 2762 struct iwl_station_priv *sta_priv = (struct iwl_station_priv *) sta->drv_priv;
d0f76d68
EG
2763 struct iwl_op_mode *op_mode __maybe_unused =
2764 (struct iwl_op_mode *)priv_rate;
2765 struct iwl_priv *priv __maybe_unused = IWL_OP_MODE_GET_DVM(op_mode);
b481de9c 2766
e1623446 2767 IWL_DEBUG_RATE(priv, "create station rate scale window\n");
b481de9c 2768
08960dea 2769 return &sta_priv->lq_sta;
b481de9c
ZY
2770}
2771
fe6b23dd
RC
2772/*
2773 * Called after adding a new station to initialize rate scaling
2774 */
2775void iwl_rs_rate_init(struct iwl_priv *priv, struct ieee80211_sta *sta, u8 sta_id)
b481de9c
ZY
2776{
2777 int i, j;
fe6b23dd 2778 struct ieee80211_hw *hw = priv->hw;
4b7679a5 2779 struct ieee80211_conf *conf = &priv->hw->conf;
7869b0ea 2780 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
fe6b23dd
RC
2781 struct iwl_station_priv *sta_priv;
2782 struct iwl_lq_sta *lq_sta;
2783 struct ieee80211_supported_band *sband;
3ac40eda 2784 unsigned long supp; /* must be unsigned long for for_each_set_bit */
fe6b23dd
RC
2785
2786 sta_priv = (struct iwl_station_priv *) sta->drv_priv;
2787 lq_sta = &sta_priv->lq_sta;
675a0b04 2788 sband = hw->wiphy->bands[conf->chandef.chan->band];
b481de9c 2789
fe6b23dd
RC
2790
2791 lq_sta->lq.sta_id = sta_id;
5ad13f8c
RC
2792
2793 for (j = 0; j < LQ_SIZE; j++)
2794 for (i = 0; i < IWL_RATE_COUNT; i++)
2795 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
2796
c33104f0 2797 lq_sta->flush_timer = 0;
4b7679a5 2798 lq_sta->supp_rates = sta->supp_rates[sband->band];
b481de9c 2799
fe6b23dd
RC
2800 IWL_DEBUG_RATE(priv, "LQ: *** rate scale station global init for station %d ***\n",
2801 sta_id);
b481de9c
ZY
2802 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2803 * the lowest or the highest rate.. Could consider using RSSI from
2804 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2805 * after assoc.. */
2806
c33104f0 2807 lq_sta->is_dup = 0;
c6ec7a9b 2808 lq_sta->max_rate_idx = -1;
7d049e5a 2809 lq_sta->missed_rate_counter = IWL_MISSED_RATE_MAX;
7e6a5886 2810 lq_sta->is_green = rs_use_green(sta);
3ac40eda
JB
2811 lq_sta->band = sband->band;
2812 /*
2813 * active legacy rates as per supported rates bitmap
2814 */
2815 supp = sta->supp_rates[sband->band];
2816 lq_sta->active_legacy_rate = 0;
2817 for_each_set_bit(i, &supp, BITS_PER_LONG)
2818 lq_sta->active_legacy_rate |= BIT(sband->bitrates[i].hw_value);
2819
77626355
BC
2820 /*
2821 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2822 * supp_rates[] does not; shift to convert format, force 9 MBits off.
2823 */
7869b0ea
DH
2824 lq_sta->active_siso_rate = ht_cap->mcs.rx_mask[0] << 1;
2825 lq_sta->active_siso_rate |= ht_cap->mcs.rx_mask[0] & 0x1;
c33104f0 2826 lq_sta->active_siso_rate &= ~((u16)0x2);
fde0db31 2827 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
b481de9c 2828
77626355 2829 /* Same here */
7869b0ea
DH
2830 lq_sta->active_mimo2_rate = ht_cap->mcs.rx_mask[1] << 1;
2831 lq_sta->active_mimo2_rate |= ht_cap->mcs.rx_mask[1] & 0x1;
fde0db31
GC
2832 lq_sta->active_mimo2_rate &= ~((u16)0x2);
2833 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2834
7869b0ea
DH
2835 lq_sta->active_mimo3_rate = ht_cap->mcs.rx_mask[2] << 1;
2836 lq_sta->active_mimo3_rate |= ht_cap->mcs.rx_mask[2] & 0x1;
fde0db31
GC
2837 lq_sta->active_mimo3_rate &= ~((u16)0x2);
2838 lq_sta->active_mimo3_rate <<= IWL_FIRST_OFDM_RATE;
2839
e1623446 2840 IWL_DEBUG_RATE(priv, "SISO-RATE=%X MIMO2-RATE=%X MIMO3-RATE=%X\n",
c33104f0 2841 lq_sta->active_siso_rate,
fde0db31
GC
2842 lq_sta->active_mimo2_rate,
2843 lq_sta->active_mimo3_rate);
2844
a96a27f9 2845 /* These values will be overridden later */
3a23d695 2846 lq_sta->lq.general_params.single_stream_ant_msk =
b7998c8b 2847 first_antenna(priv->nvm_data->valid_tx_ant);
3a23d695 2848 lq_sta->lq.general_params.dual_stream_ant_msk =
b7998c8b
EL
2849 priv->nvm_data->valid_tx_ant &
2850 ~first_antenna(priv->nvm_data->valid_tx_ant);
3a23d695
WYG
2851 if (!lq_sta->lq.general_params.dual_stream_ant_msk) {
2852 lq_sta->lq.general_params.dual_stream_ant_msk = ANT_AB;
b7998c8b 2853 } else if (num_of_ant(priv->nvm_data->valid_tx_ant) == 2) {
3a23d695 2854 lq_sta->lq.general_params.dual_stream_ant_msk =
b7998c8b 2855 priv->nvm_data->valid_tx_ant;
3a23d695 2856 }
07bc28ed 2857
0c11b4de
RR
2858 /* as default allow aggregation for all tids */
2859 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
c33104f0 2860 lq_sta->drv = priv;
b481de9c 2861
5027309b
DH
2862 /* Set last_txrate_idx to lowest rate */
2863 lq_sta->last_txrate_idx = rate_lowest_index(sband, sta);
57fbcce3 2864 if (sband->band == NL80211_BAND_5GHZ)
5027309b 2865 lq_sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
e3949d62 2866 lq_sta->is_agg = 0;
7d5f01ad 2867#ifdef CONFIG_MAC80211_DEBUGFS
a1da077b 2868 lq_sta->dbg_fixed_rate = 0;
7d5f01ad 2869#endif
a1da077b 2870
9b6ca448 2871 rs_initialize_lq(priv, sta, lq_sta);
b481de9c
ZY
2872}
2873
46f9381a 2874static void rs_fill_link_cmd(struct iwl_priv *priv,
e227ceac 2875 struct iwl_lq_sta *lq_sta, u32 new_rate)
b481de9c 2876{
e227ceac 2877 struct iwl_scale_tbl_info tbl_type;
b481de9c 2878 int index = 0;
b481de9c 2879 int rate_idx;
1b696de2 2880 int repeat_rate = 0;
aade00ce 2881 u8 ant_toggle_cnt = 0;
b481de9c 2882 u8 use_ht_possible = 1;
aade00ce 2883 u8 valid_tx_ant = 0;
7b090687
JB
2884 struct iwl_station_priv *sta_priv =
2885 container_of(lq_sta, struct iwl_station_priv, lq_sta);
07bc28ed 2886 struct iwl_link_quality_cmd *lq_cmd = &lq_sta->lq;
b481de9c 2887
77626355 2888 /* Override starting rate (index 0) if needed for debug purposes */
39e88504 2889 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
98d7e09a 2890
39e88504
GC
2891 /* Interpret new_rate (rate_n_flags) */
2892 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
b481de9c
ZY
2893 &tbl_type, &rate_idx);
2894
da5dbb97
WYG
2895 if (priv && priv->bt_full_concurrent) {
2896 /* 1x1 only */
2897 tbl_type.ant_type =
b7998c8b 2898 first_antenna(priv->nvm_data->valid_tx_ant);
da5dbb97
WYG
2899 }
2900
77626355 2901 /* How many times should we repeat the initial rate? */
b481de9c 2902 if (is_legacy(tbl_type.lq_type)) {
aade00ce 2903 ant_toggle_cnt = 1;
1b696de2 2904 repeat_rate = IWL_NUMBER_TRY;
aade00ce 2905 } else {
374920cb
DH
2906 repeat_rate = min(IWL_HT_NUMBER_TRY,
2907 LINK_QUAL_AGG_DISABLE_START_DEF - 1);
aade00ce 2908 }
b481de9c
ZY
2909
2910 lq_cmd->general_params.mimo_delimiter =
2911 is_mimo(tbl_type.lq_type) ? 1 : 0;
77626355
BC
2912
2913 /* Fill 1st table entry (index 0) */
39e88504 2914 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
b481de9c 2915
07bc28ed
GC
2916 if (num_of_ant(tbl_type.ant_type) == 1) {
2917 lq_cmd->general_params.single_stream_ant_msk =
2918 tbl_type.ant_type;
2919 } else if (num_of_ant(tbl_type.ant_type) == 2) {
2920 lq_cmd->general_params.dual_stream_ant_msk =
2921 tbl_type.ant_type;
2922 } /* otherwise we don't modify the existing value */
b481de9c
ZY
2923
2924 index++;
1b696de2 2925 repeat_rate--;
bee008b7
WYG
2926 if (priv) {
2927 if (priv->bt_full_concurrent)
2928 valid_tx_ant = ANT_A;
2929 else
b7998c8b 2930 valid_tx_ant = priv->nvm_data->valid_tx_ant;
bee008b7 2931 }
aade00ce 2932
77626355 2933 /* Fill rest of rate table */
b481de9c 2934 while (index < LINK_QUAL_MAX_RETRY_NUM) {
77626355
BC
2935 /* Repeat initial/next rate.
2936 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2937 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
1b696de2 2938 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
b481de9c 2939 if (is_legacy(tbl_type.lq_type)) {
aade00ce
GC
2940 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2941 ant_toggle_cnt++;
2942 else if (priv &&
2943 rs_toggle_antenna(valid_tx_ant,
2944 &new_rate, &tbl_type))
2945 ant_toggle_cnt = 1;
da5dbb97 2946 }
98d7e09a 2947
77626355 2948 /* Override next rate if needed for debug purposes */
c33104f0 2949 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
77626355
BC
2950
2951 /* Fill next table entry */
b481de9c 2952 lq_cmd->rs_table[index].rate_n_flags =
39e88504 2953 cpu_to_le32(new_rate);
1b696de2 2954 repeat_rate--;
b481de9c
ZY
2955 index++;
2956 }
2957
39e88504 2958 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band, &tbl_type,
b481de9c
ZY
2959 &rate_idx);
2960
da5dbb97
WYG
2961 if (priv && priv->bt_full_concurrent) {
2962 /* 1x1 only */
2963 tbl_type.ant_type =
b7998c8b 2964 first_antenna(priv->nvm_data->valid_tx_ant);
da5dbb97
WYG
2965 }
2966
77626355
BC
2967 /* Indicate to uCode which entries might be MIMO.
2968 * If initial rate was MIMO, this will finally end up
2969 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
b481de9c
ZY
2970 if (is_mimo(tbl_type.lq_type))
2971 lq_cmd->general_params.mimo_delimiter = index;
2972
77626355 2973 /* Get next rate */
39e88504
GC
2974 new_rate = rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
2975 use_ht_possible);
b481de9c 2976
77626355 2977 /* How many times should we repeat the next rate? */
b481de9c 2978 if (is_legacy(tbl_type.lq_type)) {
aade00ce
GC
2979 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2980 ant_toggle_cnt++;
2981 else if (priv &&
2982 rs_toggle_antenna(valid_tx_ant,
2983 &new_rate, &tbl_type))
2984 ant_toggle_cnt = 1;
2985
1b696de2 2986 repeat_rate = IWL_NUMBER_TRY;
aade00ce 2987 } else {
1b696de2 2988 repeat_rate = IWL_HT_NUMBER_TRY;
aade00ce 2989 }
b481de9c 2990
77626355
BC
2991 /* Don't allow HT rates after next pass.
2992 * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
b481de9c
ZY
2993 use_ht_possible = 0;
2994
77626355 2995 /* Override next rate if needed for debug purposes */
c33104f0 2996 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
77626355
BC
2997
2998 /* Fill next table entry */
39e88504 2999 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
b481de9c
ZY
3000
3001 index++;
1b696de2 3002 repeat_rate--;
b481de9c
ZY
3003 }
3004
7b090687
JB
3005 lq_cmd->agg_params.agg_frame_cnt_limit =
3006 sta_priv->max_agg_bufsize ?: LINK_QUAL_AGG_FRAME_LIMIT_DEF;
13c33a09 3007 lq_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
7469701e 3008
13c33a09
WYG
3009 lq_cmd->agg_params.agg_time_limit =
3010 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
7469701e
WYG
3011 /*
3012 * overwrite if needed, pass aggregation time limit
3013 * to uCode in uSec
3014 */
0d8877a1
JB
3015 if (priv && priv->lib->bt_params &&
3016 priv->lib->bt_params->agg_time_limit &&
66e863a5 3017 priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH)
7469701e 3018 lq_cmd->agg_params.agg_time_limit =
0d8877a1 3019 cpu_to_le16(priv->lib->bt_params->agg_time_limit);
b481de9c
ZY
3020}
3021
6cb5f3ea 3022static void *rs_alloc(struct ieee80211_hw *hw)
b481de9c 3023{
4b7679a5 3024 return hw->priv;
b481de9c
ZY
3025}
3026/* rate scale requires free function to be implemented */
3027static void rs_free(void *priv_rate)
3028{
3029 return;
3030}
3031
4b7679a5
JB
3032static void rs_free_sta(void *priv_r, struct ieee80211_sta *sta,
3033 void *priv_sta)
b481de9c 3034{
d0f76d68
EG
3035 struct iwl_op_mode *op_mode __maybe_unused = priv_r;
3036 struct iwl_priv *priv __maybe_unused = IWL_OP_MODE_GET_DVM(op_mode);
b481de9c 3037
e1623446 3038 IWL_DEBUG_RATE(priv, "enter\n");
e1623446 3039 IWL_DEBUG_RATE(priv, "leave\n");
b481de9c
ZY
3040}
3041
93dc646a 3042#ifdef CONFIG_MAC80211_DEBUGFS
e227ceac
TW
3043static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
3044 u32 *rate_n_flags, int index)
98d7e09a 3045{
bf403db8 3046 struct iwl_priv *priv;
3c4955f8
WYG
3047 u8 valid_tx_ant;
3048 u8 ant_sel_tx;
bf403db8
EK
3049
3050 priv = lq_sta->drv;
b7998c8b 3051 valid_tx_ant = priv->nvm_data->valid_tx_ant;
39e88504 3052 if (lq_sta->dbg_fixed_rate) {
3c4955f8
WYG
3053 ant_sel_tx =
3054 ((lq_sta->dbg_fixed_rate & RATE_MCS_ANT_ABC_MSK)
3055 >> RATE_MCS_ANT_POS);
3056 if ((valid_tx_ant & ant_sel_tx) == ant_sel_tx) {
39e88504 3057 *rate_n_flags = lq_sta->dbg_fixed_rate;
3c4955f8 3058 IWL_DEBUG_RATE(priv, "Fixed rate ON\n");
39e88504 3059 } else {
3c4955f8
WYG
3060 lq_sta->dbg_fixed_rate = 0;
3061 IWL_ERR(priv,
3062 "Invalid antenna selection 0x%X, Valid is 0x%X\n",
3063 ant_sel_tx, valid_tx_ant);
3064 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
39e88504 3065 }
39e88504 3066 } else {
e1623446 3067 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
98d7e09a 3068 }
98d7e09a 3069}
5ae212c9 3070
98d7e09a
ZY
3071static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
3072 const char __user *user_buf, size_t count, loff_t *ppos)
3073{
e227ceac 3074 struct iwl_lq_sta *lq_sta = file->private_data;
bf403db8 3075 struct iwl_priv *priv;
98d7e09a 3076 char buf[64];
805d7d23 3077 size_t buf_size;
98d7e09a 3078 u32 parsed_rate;
6489854b 3079
98d7e09a 3080
bf403db8 3081 priv = lq_sta->drv;
98d7e09a
ZY
3082 memset(buf, 0, sizeof(buf));
3083 buf_size = min(count, sizeof(buf) - 1);
3084 if (copy_from_user(buf, user_buf, buf_size))
3085 return -EFAULT;
3086
3087 if (sscanf(buf, "%x", &parsed_rate) == 1)
4e308119 3088 lq_sta->dbg_fixed_rate = parsed_rate;
98d7e09a 3089 else
4e308119 3090 lq_sta->dbg_fixed_rate = 0;
98d7e09a 3091
6489854b 3092 rs_program_fix_rate(priv, lq_sta);
98d7e09a
ZY
3093
3094 return count;
3095}
0209dc11 3096
5ae212c9
ZY
3097static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
3098 char __user *user_buf, size_t count, loff_t *ppos)
3099{
a412c804 3100 char *buff;
5ae212c9
ZY
3101 int desc = 0;
3102 int i = 0;
12b96817 3103 int index = 0;
a412c804 3104 ssize_t ret;
5ae212c9 3105
e227ceac 3106 struct iwl_lq_sta *lq_sta = file->private_data;
d8ae4f52 3107 struct iwl_priv *priv;
adb7b5e6 3108 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
5ae212c9 3109
d8ae4f52 3110 priv = lq_sta->drv;
a412c804
FS
3111 buff = kmalloc(1024, GFP_KERNEL);
3112 if (!buff)
3113 return -ENOMEM;
3114
c33104f0 3115 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
98d7e09a 3116 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
c33104f0 3117 lq_sta->total_failed, lq_sta->total_success,
39e88504 3118 lq_sta->active_legacy_rate);
98d7e09a 3119 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
4e308119 3120 lq_sta->dbg_fixed_rate);
d8ae4f52 3121 desc += sprintf(buff+desc, "valid_tx_ant %s%s%s\n",
b7998c8b
EL
3122 (priv->nvm_data->valid_tx_ant & ANT_A) ? "ANT_A," : "",
3123 (priv->nvm_data->valid_tx_ant & ANT_B) ? "ANT_B," : "",
3124 (priv->nvm_data->valid_tx_ant & ANT_C) ? "ANT_C" : "");
adb7b5e6
WYG
3125 desc += sprintf(buff+desc, "lq type %s\n",
3126 (is_legacy(tbl->lq_type)) ? "legacy" : "HT");
3127 if (is_Ht(tbl->lq_type)) {
e70fe7eb 3128 desc += sprintf(buff + desc, " %s",
adb7b5e6
WYG
3129 (is_siso(tbl->lq_type)) ? "SISO" :
3130 ((is_mimo2(tbl->lq_type)) ? "MIMO2" : "MIMO3"));
e70fe7eb 3131 desc += sprintf(buff + desc, " %s",
7aafef1c 3132 (tbl->is_ht40) ? "40MHz" : "20MHz");
e70fe7eb
EG
3133 desc += sprintf(buff + desc, " %s %s %s\n",
3134 (tbl->is_SGI) ? "SGI" : "",
e3949d62
DH
3135 (lq_sta->is_green) ? "GF enabled" : "",
3136 (lq_sta->is_agg) ? "AGG on" : "");
adb7b5e6 3137 }
12b96817
WYG
3138 desc += sprintf(buff+desc, "last tx rate=0x%X\n",
3139 lq_sta->last_rate_n_flags);
5ae212c9
ZY
3140 desc += sprintf(buff+desc, "general:"
3141 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
c33104f0
TW
3142 lq_sta->lq.general_params.flags,
3143 lq_sta->lq.general_params.mimo_delimiter,
3144 lq_sta->lq.general_params.single_stream_ant_msk,
3145 lq_sta->lq.general_params.dual_stream_ant_msk);
5ae212c9
ZY
3146
3147 desc += sprintf(buff+desc, "agg:"
3148 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
c33104f0
TW
3149 le16_to_cpu(lq_sta->lq.agg_params.agg_time_limit),
3150 lq_sta->lq.agg_params.agg_dis_start_th,
3151 lq_sta->lq.agg_params.agg_frame_cnt_limit);
5ae212c9
ZY
3152
3153 desc += sprintf(buff+desc,
3154 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
c33104f0
TW
3155 lq_sta->lq.general_params.start_rate_index[0],
3156 lq_sta->lq.general_params.start_rate_index[1],
3157 lq_sta->lq.general_params.start_rate_index[2],
3158 lq_sta->lq.general_params.start_rate_index[3]);
5ae212c9 3159
12b96817
WYG
3160 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
3161 index = iwl_hwrate_to_plcp_idx(
3162 le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags));
3163 if (is_legacy(tbl->lq_type)) {
3164 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps\n",
3165 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
3166 iwl_rate_mcs[index].mbps);
3167 } else {
3168 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps (%s)\n",
3169 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
3170 iwl_rate_mcs[index].mbps, iwl_rate_mcs[index].mcs);
3171 }
3172 }
5ae212c9 3173
a412c804
FS
3174 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3175 kfree(buff);
3176 return ret;
5ae212c9
ZY
3177}
3178
3179static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
98d7e09a 3180 .write = rs_sta_dbgfs_scale_table_write,
5ae212c9 3181 .read = rs_sta_dbgfs_scale_table_read,
234e3405 3182 .open = simple_open,
6038f373 3183 .llseek = default_llseek,
5ae212c9 3184};
0209dc11
ZY
3185static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
3186 char __user *user_buf, size_t count, loff_t *ppos)
3187{
a412c804 3188 char *buff;
0209dc11
ZY
3189 int desc = 0;
3190 int i, j;
a412c804 3191 ssize_t ret;
0209dc11 3192
e227ceac 3193 struct iwl_lq_sta *lq_sta = file->private_data;
a412c804
FS
3194
3195 buff = kmalloc(1024, GFP_KERNEL);
3196 if (!buff)
3197 return -ENOMEM;
3198
0209dc11 3199 for (i = 0; i < LQ_SIZE; i++) {
7aafef1c
WYG
3200 desc += sprintf(buff+desc,
3201 "%s type=%d SGI=%d HT40=%d DUP=%d GF=%d\n"
0209dc11 3202 "rate=0x%X\n",
c3056065 3203 lq_sta->active_tbl == i ? "*" : "x",
c33104f0
TW
3204 lq_sta->lq_info[i].lq_type,
3205 lq_sta->lq_info[i].is_SGI,
7aafef1c 3206 lq_sta->lq_info[i].is_ht40,
c33104f0 3207 lq_sta->lq_info[i].is_dup,
2681b20b 3208 lq_sta->is_green,
39e88504 3209 lq_sta->lq_info[i].current_rate);
0209dc11
ZY
3210 for (j = 0; j < IWL_RATE_COUNT; j++) {
3211 desc += sprintf(buff+desc,
c33104f0
TW
3212 "counter=%d success=%d %%=%d\n",
3213 lq_sta->lq_info[i].win[j].counter,
3214 lq_sta->lq_info[i].win[j].success_counter,
3215 lq_sta->lq_info[i].win[j].success_ratio);
0209dc11
ZY
3216 }
3217 }
a412c804
FS
3218 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3219 kfree(buff);
3220 return ret;
0209dc11
ZY
3221}
3222
3223static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
3224 .read = rs_sta_dbgfs_stats_table_read,
234e3405 3225 .open = simple_open,
6038f373 3226 .llseek = default_llseek,
0209dc11 3227};
5ae212c9 3228
38167459
WYG
3229static ssize_t rs_sta_dbgfs_rate_scale_data_read(struct file *file,
3230 char __user *user_buf, size_t count, loff_t *ppos)
3231{
38167459 3232 struct iwl_lq_sta *lq_sta = file->private_data;
38167459 3233 struct iwl_scale_tbl_info *tbl = &lq_sta->lq_info[lq_sta->active_tbl];
70817b5e
JB
3234 char buff[120];
3235 int desc = 0;
38167459
WYG
3236
3237 if (is_Ht(tbl->lq_type))
3238 desc += sprintf(buff+desc,
3239 "Bit Rate= %d Mb/s\n",
3240 tbl->expected_tpt[lq_sta->last_txrate_idx]);
3241 else
3242 desc += sprintf(buff+desc,
3243 "Bit Rate= %d Mb/s\n",
3244 iwl_rates[lq_sta->last_txrate_idx].ieee >> 1);
38167459 3245
08960dea 3246 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
38167459
WYG
3247}
3248
3249static const struct file_operations rs_sta_dbgfs_rate_scale_data_ops = {
3250 .read = rs_sta_dbgfs_rate_scale_data_read,
234e3405 3251 .open = simple_open,
6038f373 3252 .llseek = default_llseek,
38167459
WYG
3253};
3254
93dc646a
ZY
3255static void rs_add_debugfs(void *priv, void *priv_sta,
3256 struct dentry *dir)
3257{
e227ceac 3258 struct iwl_lq_sta *lq_sta = priv_sta;
0c11b4de 3259
49175fe6
GKH
3260 debugfs_create_file("rate_scale_table", 0600, dir, lq_sta,
3261 &rs_sta_dbgfs_scale_table_ops);
3262 debugfs_create_file("rate_stats_table", 0400, dir, lq_sta,
3263 &rs_sta_dbgfs_stats_table_ops);
3264 debugfs_create_file("rate_scale_data", 0400, dir, lq_sta,
3265 &rs_sta_dbgfs_rate_scale_data_ops);
3266 debugfs_create_u8("tx_agg_tid_enable", 0600, dir,
3267 &lq_sta->tx_agg_tid_en);
93dc646a 3268
93dc646a
ZY
3269}
3270#endif
3271
fe6b23dd
RC
3272/*
3273 * Initialization of rate scaling information is done by driver after
3274 * the station is added. Since mac80211 calls this function before a
3275 * station is added we ignore it.
3276 */
3277static void rs_rate_init_stub(void *priv_r, struct ieee80211_supported_band *sband,
3de805cf
SW
3278 struct cfg80211_chan_def *chandef,
3279 struct ieee80211_sta *sta, void *priv_sta)
fe6b23dd
RC
3280{
3281}
631ad703
JB
3282
3283static const struct rate_control_ops rs_ops = {
b481de9c
ZY
3284 .name = RS_NAME,
3285 .tx_status = rs_tx_status,
3286 .get_rate = rs_get_rate,
fe6b23dd 3287 .rate_init = rs_rate_init_stub,
b481de9c
ZY
3288 .alloc = rs_alloc,
3289 .free = rs_free,
3290 .alloc_sta = rs_alloc_sta,
3291 .free_sta = rs_free_sta,
93dc646a
ZY
3292#ifdef CONFIG_MAC80211_DEBUGFS
3293 .add_sta_debugfs = rs_add_debugfs,
93dc646a 3294#endif
b481de9c
ZY
3295};
3296
e227ceac 3297int iwlagn_rate_control_register(void)
b481de9c 3298{
897e1cf2 3299 return ieee80211_rate_control_register(&rs_ops);
b481de9c
ZY
3300}
3301
e227ceac 3302void iwlagn_rate_control_unregister(void)
b481de9c
ZY
3303{
3304 ieee80211_rate_control_unregister(&rs_ops);
3305}
3306