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