Merge tag 'hda-switcheroo' of git://git.kernel.org/pub/scm/linux/kernel/git/tiwai...
[GitHub/mt8127/android_kernel_alcatel_ttab.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"
a30e3112 39#include "iwl-agn.h"
d0f76d68 40#include "iwl-op-mode.h"
65de7e84 41#include "iwl-modparams.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
65de7e84 423 if ((iwlwifi_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 =
9e295116 822 first_antenna(priv->hw_params.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 873
66e863a5
WYG
874 if (priv->bt_ant_couple_ok) {
875 /*
876 * Is there a need to switch between
877 * full concurrency and 3-wire?
878 */
66e863a5
WYG
879 if (priv->bt_ci_compliance && priv->bt_ant_couple_ok)
880 full_concurrent = true;
881 else
882 full_concurrent = false;
66e863a5
WYG
883 }
884 if ((priv->bt_traffic_load != priv->last_bt_traffic_load) ||
885 (priv->bt_full_concurrent != full_concurrent)) {
bee008b7
WYG
886 priv->bt_full_concurrent = full_concurrent;
887
888 /* Update uCode's rate table. */
889 tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
890 rs_fill_link_cmd(priv, lq_sta, tbl->current_rate);
7e6a5886 891 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_ASYNC, false);
bee008b7 892
1ee158d8 893 queue_work(priv->workqueue, &priv->bt_full_concurrency);
bee008b7
WYG
894 }
895}
896
77626355
BC
897/*
898 * mac80211 sends us Tx status
899 */
4b7679a5
JB
900static void rs_tx_status(void *priv_r, struct ieee80211_supported_band *sband,
901 struct ieee80211_sta *sta, void *priv_sta,
e039fa4a 902 struct sk_buff *skb)
b481de9c 903{
95407aa4
DH
904 int legacy_success;
905 int retries;
906 int rs_index, mac_index, i;
417f114b 907 struct iwl_lq_sta *lq_sta = priv_sta;
66c73db7 908 struct iwl_link_quality_cmd *table;
b481de9c 909 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
d0f76d68
EG
910 struct iwl_op_mode *op_mode = (struct iwl_op_mode *)priv_r;
911 struct iwl_priv *priv = IWL_OP_MODE_GET_DVM(op_mode);
e039fa4a 912 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
5027309b 913 enum mac80211_rate_control_flags mac_flags;
39e88504 914 u32 tx_rate;
e227ceac 915 struct iwl_scale_tbl_info tbl_type;
04f2dec1 916 struct iwl_scale_tbl_info *curr_tbl, *other_tbl, *tmp_tbl;
7e6a5886 917 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
c6892906 918 struct iwl_rxon_context *ctx = sta_priv->ctx;
b481de9c 919
e1623446 920 IWL_DEBUG_RATE_LIMIT(priv, "get frame ack response, update rate scale window\n");
b481de9c 921
fe6b23dd
RC
922 /* Treat uninitialized rate scaling data same as non-existing. */
923 if (!lq_sta) {
924 IWL_DEBUG_RATE(priv, "Station rate scaling not created yet.\n");
925 return;
926 } else if (!lq_sta->drv) {
927 IWL_DEBUG_RATE(priv, "Rate scaling not initialized yet.\n");
928 return;
929 }
930
417f114b 931 if (!ieee80211_is_data(hdr->frame_control) ||
514d65c1 932 info->flags & IEEE80211_TX_CTL_NO_ACK)
b481de9c
ZY
933 return;
934
e3949d62 935 /* This packet was aggregated but doesn't carry status info */
e039fa4a
JB
936 if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
937 !(info->flags & IEEE80211_TX_STAT_AMPDU))
99556438
RR
938 return;
939
77626355
BC
940 /*
941 * Ignore this Tx frame response if its initial rate doesn't match
942 * that of latest Link Quality command. There may be stragglers
943 * from a previous Link Quality command, but we're no longer interested
944 * in those; they're either from the "active" mode while we're trying
945 * to check "search" mode, or a prior "search" mode after we've moved
946 * to a new "search" mode (which might become the new "active" mode).
947 */
95407aa4 948 table = &lq_sta->lq;
39e88504
GC
949 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
950 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
4c424e4c
RR
951 if (priv->band == IEEE80211_BAND_5GHZ)
952 rs_index -= IWL_FIRST_OFDM_RATE;
5027309b
DH
953 mac_flags = info->status.rates[0].flags;
954 mac_index = info->status.rates[0].idx;
31513be8
DH
955 /* For HT packets, map MCS to PLCP */
956 if (mac_flags & IEEE80211_TX_RC_MCS) {
957 mac_index &= RATE_MCS_CODE_MSK; /* Remove # of streams */
958 if (mac_index >= (IWL_RATE_9M_INDEX - IWL_FIRST_OFDM_RATE))
959 mac_index++;
392a0baf
DH
960 /*
961 * mac80211 HT index is always zero-indexed; we need to move
962 * HT OFDM rates after CCK rates in 2.4 GHz band
963 */
964 if (priv->band == IEEE80211_BAND_2GHZ)
965 mac_index += IWL_FIRST_OFDM_RATE;
31513be8 966 }
95407aa4 967 /* Here we actually compare this rate to the latest LQ command */
5027309b
DH
968 if ((mac_index < 0) ||
969 (tbl_type.is_SGI != !!(mac_flags & IEEE80211_TX_RC_SHORT_GI)) ||
970 (tbl_type.is_ht40 != !!(mac_flags & IEEE80211_TX_RC_40_MHZ_WIDTH)) ||
971 (tbl_type.is_dup != !!(mac_flags & IEEE80211_TX_RC_DUP_DATA)) ||
d748b464 972 (tbl_type.ant_type != info->status.antenna) ||
5027309b
DH
973 (!!(tx_rate & RATE_MCS_HT_MSK) != !!(mac_flags & IEEE80211_TX_RC_MCS)) ||
974 (!!(tx_rate & RATE_MCS_GF_MSK) != !!(mac_flags & IEEE80211_TX_RC_GREEN_FIELD)) ||
31513be8
DH
975 (rs_index != mac_index)) {
976 IWL_DEBUG_RATE(priv, "initial rate %d does not match %d (0x%x)\n", mac_index, rs_index, tx_rate);
95407aa4
DH
977 /*
978 * Since rates mis-match, the last LQ command may have failed.
979 * After IWL_MISSED_RATE_MAX mis-matches, resync the uCode with
980 * ... driver.
d5e49036 981 */
7d049e5a
AM
982 lq_sta->missed_rate_counter++;
983 if (lq_sta->missed_rate_counter > IWL_MISSED_RATE_MAX) {
984 lq_sta->missed_rate_counter = 0;
7e6a5886 985 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_ASYNC, false);
7d049e5a 986 }
95407aa4
DH
987 /* Regardless, ignore this status info for outdated rate */
988 return;
989 } else
990 /* Rate did match, so reset the missed_rate_counter */
991 lq_sta->missed_rate_counter = 0;
992
993 /* Figure out if rate scale algorithm is in active or search table */
994 if (table_type_matches(&tbl_type,
995 &(lq_sta->lq_info[lq_sta->active_tbl]))) {
996 curr_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
997 other_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
998 } else if (table_type_matches(&tbl_type,
999 &lq_sta->lq_info[1 - lq_sta->active_tbl])) {
1000 curr_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
1001 other_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1002 } else {
1003 IWL_DEBUG_RATE(priv, "Neither active nor search matches tx rate\n");
80e9158c
WYG
1004 tmp_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1005 IWL_DEBUG_RATE(priv, "active- lq:%x, ant:%x, SGI:%d\n",
1006 tmp_tbl->lq_type, tmp_tbl->ant_type, tmp_tbl->is_SGI);
1007 tmp_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
1008 IWL_DEBUG_RATE(priv, "search- lq:%x, ant:%x, SGI:%d\n",
1009 tmp_tbl->lq_type, tmp_tbl->ant_type, tmp_tbl->is_SGI);
1010 IWL_DEBUG_RATE(priv, "actual- lq:%x, ant:%x, SGI:%d\n",
1011 tbl_type.lq_type, tbl_type.ant_type, tbl_type.is_SGI);
1012 /*
1013 * no matching table found, let's by-pass the data collection
1014 * and continue to perform rate scale to find the rate table
1015 */
da5dbb97 1016 rs_stay_in_table(lq_sta, true);
80e9158c 1017 goto done;
b481de9c
ZY
1018 }
1019
95407aa4
DH
1020 /*
1021 * Updating the frame history depends on whether packets were
1022 * aggregated.
1023 *
1024 * For aggregation, all packets were transmitted at the same rate, the
1025 * first index into rate scale table.
1026 */
1027 if (info->flags & IEEE80211_TX_STAT_AMPDU) {
1028 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
1029 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type,
1030 &rs_index);
04f2dec1 1031 rs_collect_tx_data(curr_tbl, rs_index,
e3a3cd87
DH
1032 info->status.ampdu_len,
1033 info->status.ampdu_ack_len);
95407aa4
DH
1034
1035 /* Update success/fail counts if not searching for new mode */
1036 if (lq_sta->stay_in_tbl) {
e3a3cd87
DH
1037 lq_sta->total_success += info->status.ampdu_ack_len;
1038 lq_sta->total_failed += (info->status.ampdu_len -
1039 info->status.ampdu_ack_len);
b481de9c 1040 }
95407aa4 1041 } else {
77626355 1042 /*
95407aa4 1043 * For legacy, update frame history with for each Tx retry.
77626355 1044 */
95407aa4
DH
1045 retries = info->status.rates[0].count - 1;
1046 /* HW doesn't send more than 15 retries */
1047 retries = min(retries, 15);
1048
1049 /* The last transmission may have been successful */
1050 legacy_success = !!(info->flags & IEEE80211_TX_STAT_ACK);
1051 /* Collect data for each rate used during failed TX attempts */
1052 for (i = 0; i <= retries; ++i) {
1053 tx_rate = le32_to_cpu(table->rs_table[i].rate_n_flags);
1054 rs_get_tbl_info_from_mcs(tx_rate, priv->band,
1055 &tbl_type, &rs_index);
1056 /*
1057 * Only collect stats if retried rate is in the same RS
1058 * table as active/search.
1059 */
1060 if (table_type_matches(&tbl_type, curr_tbl))
04f2dec1 1061 tmp_tbl = curr_tbl;
95407aa4 1062 else if (table_type_matches(&tbl_type, other_tbl))
04f2dec1 1063 tmp_tbl = other_tbl;
95407aa4
DH
1064 else
1065 continue;
04f2dec1
SZ
1066 rs_collect_tx_data(tmp_tbl, rs_index, 1,
1067 i < retries ? 0 : legacy_success);
95407aa4
DH
1068 }
1069
1070 /* Update success/fail counts if not searching for new mode */
1071 if (lq_sta->stay_in_tbl) {
1072 lq_sta->total_success += legacy_success;
1073 lq_sta->total_failed += retries + (1 - legacy_success);
99556438 1074 }
b481de9c 1075 }
95407aa4
DH
1076 /* The last TX rate is cached in lq_sta; it's set in if/else above */
1077 lq_sta->last_rate_n_flags = tx_rate;
80e9158c 1078done:
77626355 1079 /* See if there's a better rate or modulation mode to try. */
c338ba3c 1080 if (sta && sta->supp_rates[sband->band])
514d65c1 1081 rs_rate_scale_perform(priv, skb, sta, lq_sta);
c10e2c10 1082
5ef15ccc 1083#if defined(CONFIG_MAC80211_DEBUGFS) && defined(CONFIG_IWLWIFI_DEVICE_TESTMODE)
4e308119
WYG
1084 if ((priv->tm_fixed_rate) &&
1085 (priv->tm_fixed_rate != lq_sta->dbg_fixed_rate))
6489854b
WYG
1086 rs_program_fix_rate(priv, lq_sta);
1087#endif
2152268f 1088 if (priv->cfg->bt_params && priv->cfg->bt_params->advanced_bt_coexist)
7e6a5886 1089 rs_bt_update_lq(priv, ctx, lq_sta);
b481de9c
ZY
1090}
1091
77626355
BC
1092/*
1093 * Begin a period of staying with a selected modulation mode.
1094 * Set "stay_in_tbl" flag to prevent any mode switches.
1095 * Set frame tx success limits according to legacy vs. high-throughput,
1096 * and reset overall (spanning all rates) tx success history statistics.
1097 * These control how long we stay using same modulation mode before
1098 * searching for a new mode.
1099 */
bf403db8 1100static void rs_set_stay_in_table(struct iwl_priv *priv, u8 is_legacy,
e227ceac 1101 struct iwl_lq_sta *lq_sta)
b481de9c 1102{
e1623446 1103 IWL_DEBUG_RATE(priv, "we are staying in the same table\n");
c33104f0 1104 lq_sta->stay_in_tbl = 1; /* only place this gets set */
b481de9c 1105 if (is_legacy) {
c33104f0
TW
1106 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
1107 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
1108 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
b481de9c 1109 } else {
c33104f0
TW
1110 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
1111 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
1112 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
b481de9c 1113 }
c33104f0
TW
1114 lq_sta->table_count = 0;
1115 lq_sta->total_failed = 0;
1116 lq_sta->total_success = 0;
447fee70 1117 lq_sta->flush_timer = jiffies;
d6e93399 1118 lq_sta->action_counter = 0;
b481de9c
ZY
1119}
1120
77626355
BC
1121/*
1122 * Find correct throughput table for given mode of modulation
1123 */
e227ceac
TW
1124static void rs_set_expected_tpt_table(struct iwl_lq_sta *lq_sta,
1125 struct iwl_scale_tbl_info *tbl)
b481de9c 1126{
e3949d62
DH
1127 /* Used to choose among HT tables */
1128 s32 (*ht_tbl_pointer)[IWL_RATE_COUNT];
1129
1130 /* Check for invalid LQ type */
1131 if (WARN_ON_ONCE(!is_legacy(tbl->lq_type) && !is_Ht(tbl->lq_type))) {
1132 tbl->expected_tpt = expected_tpt_legacy;
1133 return;
1134 }
1135
1136 /* Legacy rates have only one table */
b481de9c 1137 if (is_legacy(tbl->lq_type)) {
e3949d62
DH
1138 tbl->expected_tpt = expected_tpt_legacy;
1139 return;
1140 }
1141
1142 /* Choose among many HT tables depending on number of streams
1143 * (SISO/MIMO2/MIMO3), channel width (20/40), SGI, and aggregation
1144 * status */
1145 if (is_siso(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
1146 ht_tbl_pointer = expected_tpt_siso20MHz;
1147 else if (is_siso(tbl->lq_type))
1148 ht_tbl_pointer = expected_tpt_siso40MHz;
1149 else if (is_mimo2(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
1150 ht_tbl_pointer = expected_tpt_mimo2_20MHz;
1151 else if (is_mimo2(tbl->lq_type))
1152 ht_tbl_pointer = expected_tpt_mimo2_40MHz;
1153 else if (is_mimo3(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
1154 ht_tbl_pointer = expected_tpt_mimo3_20MHz;
1155 else /* if (is_mimo3(tbl->lq_type)) <-- must be true */
1156 ht_tbl_pointer = expected_tpt_mimo3_40MHz;
1157
1158 if (!tbl->is_SGI && !lq_sta->is_agg) /* Normal */
1159 tbl->expected_tpt = ht_tbl_pointer[0];
1160 else if (tbl->is_SGI && !lq_sta->is_agg) /* SGI */
1161 tbl->expected_tpt = ht_tbl_pointer[1];
1162 else if (!tbl->is_SGI && lq_sta->is_agg) /* AGG */
1163 tbl->expected_tpt = ht_tbl_pointer[2];
1164 else /* AGG+SGI */
1165 tbl->expected_tpt = ht_tbl_pointer[3];
b481de9c
ZY
1166}
1167
77626355
BC
1168/*
1169 * Find starting rate for new "search" high-throughput mode of modulation.
1170 * Goal is to find lowest expected rate (under perfect conditions) that is
1171 * above the current measured throughput of "active" mode, to give new mode
1172 * a fair chance to prove itself without too many challenges.
1173 *
1174 * This gets called when transitioning to more aggressive modulation
1175 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
1176 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1177 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1178 * bit rate will typically need to increase, but not if performance was bad.
1179 */
c79dd5b5 1180static s32 rs_get_best_rate(struct iwl_priv *priv,
e227ceac
TW
1181 struct iwl_lq_sta *lq_sta,
1182 struct iwl_scale_tbl_info *tbl, /* "search" */
f935a6da 1183 u16 rate_mask, s8 index)
b481de9c 1184{
77626355 1185 /* "active" values */
e227ceac 1186 struct iwl_scale_tbl_info *active_tbl =
c33104f0 1187 &(lq_sta->lq_info[lq_sta->active_tbl]);
b481de9c 1188 s32 active_sr = active_tbl->win[index].success_ratio;
b481de9c 1189 s32 active_tpt = active_tbl->expected_tpt[index];
77626355
BC
1190
1191 /* expected "search" throughput */
1192 s32 *tpt_tbl = tbl->expected_tpt;
1193
1194 s32 new_rate, high, low, start_hi;
b481de9c 1195 u16 high_low;
f935a6da 1196 s8 rate = index;
b481de9c
ZY
1197
1198 new_rate = high = low = start_hi = IWL_RATE_INVALID;
1199
1200 for (; ;) {
bf403db8
EK
1201 high_low = rs_get_adjacent_rate(priv, rate, rate_mask,
1202 tbl->lq_type);
b481de9c
ZY
1203
1204 low = high_low & 0xff;
1205 high = (high_low >> 8) & 0xff;
1206
77626355
BC
1207 /*
1208 * Lower the "search" bit rate, to give new "search" mode
1209 * approximately the same throughput as "active" if:
1210 *
1211 * 1) "Active" mode has been working modestly well (but not
1212 * great), and expected "search" throughput (under perfect
1213 * conditions) at candidate rate is above the actual
1214 * measured "active" throughput (but less than expected
1215 * "active" throughput under perfect conditions).
1216 * OR
1217 * 2) "Active" mode has been working perfectly or very well
1218 * and expected "search" throughput (under perfect
1219 * conditions) at candidate rate is above expected
1220 * "active" throughput (under perfect conditions).
1221 */
c33104f0 1222 if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
b481de9c
ZY
1223 ((active_sr > IWL_RATE_DECREASE_TH) &&
1224 (active_sr <= IWL_RATE_HIGH_TH) &&
1225 (tpt_tbl[rate] <= active_tpt))) ||
1226 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
1227 (tpt_tbl[rate] > active_tpt))) {
1228
77626355
BC
1229 /* (2nd or later pass)
1230 * If we've already tried to raise the rate, and are
1231 * now trying to lower it, use the higher rate. */
b481de9c
ZY
1232 if (start_hi != IWL_RATE_INVALID) {
1233 new_rate = start_hi;
1234 break;
1235 }
77626355 1236
b481de9c 1237 new_rate = rate;
77626355
BC
1238
1239 /* Loop again with lower rate */
b481de9c
ZY
1240 if (low != IWL_RATE_INVALID)
1241 rate = low;
77626355
BC
1242
1243 /* Lower rate not available, use the original */
b481de9c
ZY
1244 else
1245 break;
77626355
BC
1246
1247 /* Else try to raise the "search" rate to match "active" */
b481de9c 1248 } else {
77626355
BC
1249 /* (2nd or later pass)
1250 * If we've already tried to lower the rate, and are
1251 * now trying to raise it, use the lower rate. */
b481de9c
ZY
1252 if (new_rate != IWL_RATE_INVALID)
1253 break;
77626355
BC
1254
1255 /* Loop again with higher rate */
b481de9c
ZY
1256 else if (high != IWL_RATE_INVALID) {
1257 start_hi = high;
1258 rate = high;
77626355
BC
1259
1260 /* Higher rate not available, use the original */
b481de9c 1261 } else {
90d7795e 1262 new_rate = rate;
b481de9c
ZY
1263 break;
1264 }
1265 }
1266 }
1267
1268 return new_rate;
1269}
b481de9c 1270
77626355 1271/*
584a0f00 1272 * Set up search table for MIMO2
77626355 1273 */
fde0db31 1274static int rs_switch_to_mimo2(struct iwl_priv *priv,
e227ceac 1275 struct iwl_lq_sta *lq_sta,
270243a5 1276 struct ieee80211_conf *conf,
4b7679a5 1277 struct ieee80211_sta *sta,
e227ceac 1278 struct iwl_scale_tbl_info *tbl, int index)
b481de9c 1279{
b481de9c
ZY
1280 u16 rate_mask;
1281 s32 rate;
c33104f0 1282 s8 is_green = lq_sta->is_green;
7e6a5886 1283 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
c6892906 1284 struct iwl_rxon_context *ctx = sta_priv->ctx;
b481de9c 1285
de27e64e 1286 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
b481de9c
ZY
1287 return -1;
1288
d9fe60de 1289 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
00c5ae2f 1290 == WLAN_HT_CAP_SM_PS_STATIC)
b481de9c
ZY
1291 return -1;
1292
77626355 1293 /* Need both Tx chains/antennas to support MIMO */
9e295116 1294 if (priv->hw_params.tx_chains_num < 2)
b481de9c
ZY
1295 return -1;
1296
e1623446 1297 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO2\n");
39e88504
GC
1298
1299 tbl->lq_type = LQ_MIMO2;
c33104f0 1300 tbl->is_dup = lq_sta->is_dup;
b481de9c 1301 tbl->action = 0;
d6e93399 1302 tbl->max_search = IWL_MAX_SEARCH;
39e88504
GC
1303 rate_mask = lq_sta->active_mimo2_rate;
1304
7e6a5886 1305 if (iwl_is_ht40_tx_allowed(priv, ctx, &sta->ht_cap))
7aafef1c 1306 tbl->is_ht40 = 1;
b481de9c 1307 else
7aafef1c 1308 tbl->is_ht40 = 0;
b481de9c 1309
eecd6e57 1310 rs_set_expected_tpt_table(lq_sta, tbl);
b481de9c 1311
f935a6da 1312 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
b481de9c 1313
e1623446 1314 IWL_DEBUG_RATE(priv, "LQ: MIMO2 best rate %d mask %X\n", rate, rate_mask);
584a0f00
WYG
1315 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1316 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
1317 rate, rate_mask);
1318 return -1;
1319 }
1320 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
1321
1322 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
1323 tbl->current_rate, is_green);
1324 return 0;
1325}
1326
1327/*
1328 * Set up search table for MIMO3
1329 */
1330static int rs_switch_to_mimo3(struct iwl_priv *priv,
1331 struct iwl_lq_sta *lq_sta,
1332 struct ieee80211_conf *conf,
1333 struct ieee80211_sta *sta,
1334 struct iwl_scale_tbl_info *tbl, int index)
1335{
1336 u16 rate_mask;
1337 s32 rate;
1338 s8 is_green = lq_sta->is_green;
7e6a5886 1339 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
c6892906 1340 struct iwl_rxon_context *ctx = sta_priv->ctx;
584a0f00
WYG
1341
1342 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1343 return -1;
1344
1345 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
1346 == WLAN_HT_CAP_SM_PS_STATIC)
1347 return -1;
1348
1349 /* Need both Tx chains/antennas to support MIMO */
9e295116 1350 if (priv->hw_params.tx_chains_num < 3)
584a0f00
WYG
1351 return -1;
1352
1353 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO3\n");
1354
1355 tbl->lq_type = LQ_MIMO3;
1356 tbl->is_dup = lq_sta->is_dup;
1357 tbl->action = 0;
d6e93399 1358 tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
584a0f00
WYG
1359 rate_mask = lq_sta->active_mimo3_rate;
1360
7e6a5886 1361 if (iwl_is_ht40_tx_allowed(priv, ctx, &sta->ht_cap))
7aafef1c 1362 tbl->is_ht40 = 1;
584a0f00 1363 else
7aafef1c 1364 tbl->is_ht40 = 0;
39e88504 1365
584a0f00
WYG
1366 rs_set_expected_tpt_table(lq_sta, tbl);
1367
1368 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1369
1370 IWL_DEBUG_RATE(priv, "LQ: MIMO3 best rate %d mask %X\n",
1371 rate, rate_mask);
39e88504 1372 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
e1623446 1373 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
39e88504 1374 rate, rate_mask);
b481de9c 1375 return -1;
39e88504 1376 }
978785a3 1377 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
b481de9c 1378
e1623446 1379 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
39e88504 1380 tbl->current_rate, is_green);
dc2453ae 1381 return 0;
fde0db31 1382}
b481de9c 1383
77626355
BC
1384/*
1385 * Set up search table for SISO
1386 */
c79dd5b5 1387static int rs_switch_to_siso(struct iwl_priv *priv,
e227ceac 1388 struct iwl_lq_sta *lq_sta,
270243a5 1389 struct ieee80211_conf *conf,
4b7679a5 1390 struct ieee80211_sta *sta,
e227ceac 1391 struct iwl_scale_tbl_info *tbl, int index)
b481de9c 1392{
b481de9c 1393 u16 rate_mask;
c33104f0 1394 u8 is_green = lq_sta->is_green;
b481de9c 1395 s32 rate;
7e6a5886 1396 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
c6892906 1397 struct iwl_rxon_context *ctx = sta_priv->ctx;
b481de9c 1398
de27e64e 1399 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
b481de9c
ZY
1400 return -1;
1401
e1623446 1402 IWL_DEBUG_RATE(priv, "LQ: try to switch to SISO\n");
39e88504 1403
c33104f0 1404 tbl->is_dup = lq_sta->is_dup;
b481de9c
ZY
1405 tbl->lq_type = LQ_SISO;
1406 tbl->action = 0;
d6e93399 1407 tbl->max_search = IWL_MAX_SEARCH;
39e88504 1408 rate_mask = lq_sta->active_siso_rate;
b481de9c 1409
7e6a5886 1410 if (iwl_is_ht40_tx_allowed(priv, ctx, &sta->ht_cap))
7aafef1c 1411 tbl->is_ht40 = 1;
b481de9c 1412 else
7aafef1c 1413 tbl->is_ht40 = 0;
b481de9c 1414
b481de9c 1415 if (is_green)
39e88504 1416 tbl->is_SGI = 0; /*11n spec: no SGI in SISO+Greenfield*/
b481de9c 1417
eecd6e57 1418 rs_set_expected_tpt_table(lq_sta, tbl);
f935a6da 1419 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
b481de9c 1420
e1623446 1421 IWL_DEBUG_RATE(priv, "LQ: get best rate %d mask %X\n", rate, rate_mask);
b481de9c 1422 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
e1623446 1423 IWL_DEBUG_RATE(priv, "can not switch with index %d rate mask %x\n",
b481de9c
ZY
1424 rate, rate_mask);
1425 return -1;
1426 }
978785a3 1427 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
e1623446 1428 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
39e88504 1429 tbl->current_rate, is_green);
403ab56b 1430 return 0;
b481de9c
ZY
1431}
1432
77626355
BC
1433/*
1434 * Try to switch to new modulation mode from legacy
1435 */
c79dd5b5 1436static int rs_move_legacy_other(struct iwl_priv *priv,
e227ceac 1437 struct iwl_lq_sta *lq_sta,
270243a5 1438 struct ieee80211_conf *conf,
4b7679a5 1439 struct ieee80211_sta *sta,
b481de9c
ZY
1440 int index)
1441{
e227ceac
TW
1442 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1443 struct iwl_scale_tbl_info *search_tbl =
1444 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1445 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1446 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1447 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
b1a78858 1448 u8 start_action;
9e295116
JB
1449 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1450 u8 tx_chains_num = priv->hw_params.tx_chains_num;
fde0db31 1451 int ret = 0;
d6e93399 1452 u8 update_search_tbl_counter = 0;
b481de9c 1453
290f599c
JB
1454 switch (priv->bt_traffic_load) {
1455 case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
1456 /* nothing */
1457 break;
1458 case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
1459 /* avoid antenna B unless MIMO */
1460 if (tbl->action == IWL_LEGACY_SWITCH_ANTENNA2)
383b0874 1461 tbl->action = IWL_LEGACY_SWITCH_SISO;
290f599c
JB
1462 break;
1463 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
1464 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
1465 /* avoid antenna B and MIMO */
d6189124 1466 valid_tx_ant =
9e295116 1467 first_antenna(priv->hw_params.valid_tx_ant);
290f599c
JB
1468 if (tbl->action >= IWL_LEGACY_SWITCH_ANTENNA2 &&
1469 tbl->action != IWL_LEGACY_SWITCH_SISO)
da5dbb97 1470 tbl->action = IWL_LEGACY_SWITCH_SISO;
290f599c
JB
1471 break;
1472 default:
1473 IWL_ERR(priv, "Invalid BT load %d", priv->bt_traffic_load);
1474 break;
1475 }
1476
46f9381a
WYG
1477 if (!iwl_ht_enabled(priv))
1478 /* stay in Legacy */
1479 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
3ad3b92a 1480 else if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE &&
46f9381a
WYG
1481 tbl->action > IWL_LEGACY_SWITCH_SISO)
1482 tbl->action = IWL_LEGACY_SWITCH_SISO;
bee008b7
WYG
1483
1484 /* configure as 1x1 if bt full concurrency */
1485 if (priv->bt_full_concurrent) {
1486 if (!iwl_ht_enabled(priv))
1487 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
1488 else if (tbl->action >= IWL_LEGACY_SWITCH_ANTENNA2)
1489 tbl->action = IWL_LEGACY_SWITCH_SISO;
d6189124 1490 valid_tx_ant =
9e295116 1491 first_antenna(priv->hw_params.valid_tx_ant);
bee008b7
WYG
1492 }
1493
b1a78858 1494 start_action = tbl->action;
b481de9c 1495 for (; ;) {
d6e93399 1496 lq_sta->action_counter++;
b481de9c 1497 switch (tbl->action) {
3110bef7
GC
1498 case IWL_LEGACY_SWITCH_ANTENNA1:
1499 case IWL_LEGACY_SWITCH_ANTENNA2:
e1623446 1500 IWL_DEBUG_RATE(priv, "LQ: Legacy toggle Antenna\n");
b481de9c 1501
3110bef7
GC
1502 if ((tbl->action == IWL_LEGACY_SWITCH_ANTENNA1 &&
1503 tx_chains_num <= 1) ||
1504 (tbl->action == IWL_LEGACY_SWITCH_ANTENNA2 &&
1505 tx_chains_num <= 2))
1506 break;
1507
77626355 1508 /* Don't change antenna if success has been great */
da5dbb97
WYG
1509 if (window->success_ratio >= IWL_RS_GOOD_RATIO &&
1510 !priv->bt_full_concurrent &&
1511 priv->bt_traffic_load ==
1512 IWL_BT_COEX_TRAFFIC_LOAD_NONE)
b481de9c 1513 break;
77626355 1514
77626355 1515 /* Set up search table to try other antenna */
b481de9c
ZY
1516 memcpy(search_tbl, tbl, sz);
1517
aade00ce
GC
1518 if (rs_toggle_antenna(valid_tx_ant,
1519 &search_tbl->current_rate, search_tbl)) {
d6e93399 1520 update_search_tbl_counter = 1;
3110bef7 1521 rs_set_expected_tpt_table(lq_sta, search_tbl);
aade00ce
GC
1522 goto out;
1523 }
a5e8b505 1524 break;
b481de9c 1525 case IWL_LEGACY_SWITCH_SISO:
e1623446 1526 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to SISO\n");
77626355
BC
1527
1528 /* Set up search table to try SISO */
b481de9c 1529 memcpy(search_tbl, tbl, sz);
b481de9c 1530 search_tbl->is_SGI = 0;
c33104f0 1531 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
270243a5 1532 search_tbl, index);
dc2453ae 1533 if (!ret) {
c33104f0 1534 lq_sta->action_counter = 0;
b481de9c 1535 goto out;
dc2453ae 1536 }
b481de9c
ZY
1537
1538 break;
3110bef7
GC
1539 case IWL_LEGACY_SWITCH_MIMO2_AB:
1540 case IWL_LEGACY_SWITCH_MIMO2_AC:
1541 case IWL_LEGACY_SWITCH_MIMO2_BC:
e1623446 1542 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO2\n");
77626355
BC
1543
1544 /* Set up search table to try MIMO */
b481de9c 1545 memcpy(search_tbl, tbl, sz);
b481de9c 1546 search_tbl->is_SGI = 0;
3110bef7
GC
1547
1548 if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AB)
1549 search_tbl->ant_type = ANT_AB;
1550 else if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AC)
1551 search_tbl->ant_type = ANT_AC;
1552 else
1553 search_tbl->ant_type = ANT_BC;
1554
1555 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1556 break;
1557
fde0db31 1558 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
270243a5 1559 search_tbl, index);
dc2453ae 1560 if (!ret) {
c33104f0 1561 lq_sta->action_counter = 0;
b481de9c 1562 goto out;
dc2453ae 1563 }
b481de9c 1564 break;
584a0f00
WYG
1565
1566 case IWL_LEGACY_SWITCH_MIMO3_ABC:
1567 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO3\n");
1568
1569 /* Set up search table to try MIMO3 */
1570 memcpy(search_tbl, tbl, sz);
1571 search_tbl->is_SGI = 0;
1572
1573 search_tbl->ant_type = ANT_ABC;
1574
1575 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1576 break;
1577
1578 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1579 search_tbl, index);
1580 if (!ret) {
1581 lq_sta->action_counter = 0;
1582 goto out;
1583 }
1584 break;
b481de9c
ZY
1585 }
1586 tbl->action++;
584a0f00 1587 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
3110bef7 1588 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
b481de9c
ZY
1589
1590 if (tbl->action == start_action)
1591 break;
1592
1593 }
3110bef7 1594 search_tbl->lq_type = LQ_NONE;
b481de9c
ZY
1595 return 0;
1596
3110bef7
GC
1597out:
1598 lq_sta->search_better_tbl = 1;
b481de9c 1599 tbl->action++;
584a0f00 1600 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
3110bef7 1601 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
d6e93399
MA
1602 if (update_search_tbl_counter)
1603 search_tbl->action = tbl->action;
b481de9c
ZY
1604 return 0;
1605
1606}
1607
77626355
BC
1608/*
1609 * Try to switch to new modulation mode from SISO
1610 */
c79dd5b5 1611static int rs_move_siso_to_other(struct iwl_priv *priv,
e227ceac 1612 struct iwl_lq_sta *lq_sta,
270243a5 1613 struct ieee80211_conf *conf,
4b7679a5 1614 struct ieee80211_sta *sta, int index)
b481de9c 1615{
c33104f0 1616 u8 is_green = lq_sta->is_green;
e227ceac
TW
1617 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1618 struct iwl_scale_tbl_info *search_tbl =
1619 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1620 struct iwl_rate_scale_data *window = &(tbl->win[index]);
28e6f489 1621 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
e227ceac
TW
1622 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1623 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
b1a78858 1624 u8 start_action;
9e295116
JB
1625 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1626 u8 tx_chains_num = priv->hw_params.tx_chains_num;
d6e93399 1627 u8 update_search_tbl_counter = 0;
fde0db31 1628 int ret;
b481de9c 1629
290f599c
JB
1630 switch (priv->bt_traffic_load) {
1631 case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
1632 /* nothing */
1633 break;
1634 case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
1635 /* avoid antenna B unless MIMO */
1636 if (tbl->action == IWL_SISO_SWITCH_ANTENNA2)
383b0874 1637 tbl->action = IWL_SISO_SWITCH_MIMO2_AB;
290f599c
JB
1638 break;
1639 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
1640 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
1641 /* avoid antenna B and MIMO */
d6189124 1642 valid_tx_ant =
9e295116 1643 first_antenna(priv->hw_params.valid_tx_ant);
da5dbb97 1644 if (tbl->action != IWL_SISO_SWITCH_ANTENNA1)
290f599c
JB
1645 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1646 break;
1647 default:
1648 IWL_ERR(priv, "Invalid BT load %d", priv->bt_traffic_load);
1649 break;
1650 }
1651
3ad3b92a 1652 if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE &&
46f9381a
WYG
1653 tbl->action > IWL_SISO_SWITCH_ANTENNA2) {
1654 /* stay in SISO */
1655 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1656 }
bee008b7
WYG
1657
1658 /* configure as 1x1 if bt full concurrency */
da5dbb97 1659 if (priv->bt_full_concurrent) {
d6189124 1660 valid_tx_ant =
9e295116 1661 first_antenna(priv->hw_params.valid_tx_ant);
da5dbb97
WYG
1662 if (tbl->action >= IWL_LEGACY_SWITCH_ANTENNA2)
1663 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1664 }
bee008b7 1665
b1a78858 1666 start_action = tbl->action;
b481de9c 1667 for (;;) {
c33104f0 1668 lq_sta->action_counter++;
b481de9c 1669 switch (tbl->action) {
3110bef7
GC
1670 case IWL_SISO_SWITCH_ANTENNA1:
1671 case IWL_SISO_SWITCH_ANTENNA2:
e1623446 1672 IWL_DEBUG_RATE(priv, "LQ: SISO toggle Antenna\n");
3110bef7 1673 if ((tbl->action == IWL_SISO_SWITCH_ANTENNA1 &&
da5dbb97 1674 tx_chains_num <= 1) ||
3110bef7 1675 (tbl->action == IWL_SISO_SWITCH_ANTENNA2 &&
da5dbb97 1676 tx_chains_num <= 2))
3110bef7
GC
1677 break;
1678
da5dbb97
WYG
1679 if (window->success_ratio >= IWL_RS_GOOD_RATIO &&
1680 !priv->bt_full_concurrent &&
1681 priv->bt_traffic_load ==
1682 IWL_BT_COEX_TRAFFIC_LOAD_NONE)
b481de9c 1683 break;
b481de9c
ZY
1684
1685 memcpy(search_tbl, tbl, sz);
aade00ce 1686 if (rs_toggle_antenna(valid_tx_ant,
d6e93399
MA
1687 &search_tbl->current_rate, search_tbl)) {
1688 update_search_tbl_counter = 1;
aade00ce 1689 goto out;
d6e93399 1690 }
a5e8b505 1691 break;
3110bef7
GC
1692 case IWL_SISO_SWITCH_MIMO2_AB:
1693 case IWL_SISO_SWITCH_MIMO2_AC:
1694 case IWL_SISO_SWITCH_MIMO2_BC:
e1623446 1695 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO2\n");
b481de9c 1696 memcpy(search_tbl, tbl, sz);
b481de9c 1697 search_tbl->is_SGI = 0;
3110bef7
GC
1698
1699 if (tbl->action == IWL_SISO_SWITCH_MIMO2_AB)
1700 search_tbl->ant_type = ANT_AB;
1701 else if (tbl->action == IWL_SISO_SWITCH_MIMO2_AC)
1702 search_tbl->ant_type = ANT_AC;
1703 else
1704 search_tbl->ant_type = ANT_BC;
1705
1706 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1707 break;
1708
fde0db31 1709 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
270243a5 1710 search_tbl, index);
3110bef7 1711 if (!ret)
b481de9c
ZY
1712 goto out;
1713 break;
1714 case IWL_SISO_SWITCH_GI:
28e6f489
DH
1715 if (!tbl->is_ht40 && !(ht_cap->cap &
1716 IEEE80211_HT_CAP_SGI_20))
a9841013 1717 break;
28e6f489
DH
1718 if (tbl->is_ht40 && !(ht_cap->cap &
1719 IEEE80211_HT_CAP_SGI_40))
a9841013
EG
1720 break;
1721
e1623446 1722 IWL_DEBUG_RATE(priv, "LQ: SISO toggle SGI/NGI\n");
0c11b4de 1723
b481de9c 1724 memcpy(search_tbl, tbl, sz);
39e88504
GC
1725 if (is_green) {
1726 if (!tbl->is_SGI)
1727 break;
1728 else
15b1687c
WT
1729 IWL_ERR(priv,
1730 "SGI was set in GF+SISO\n");
b481de9c 1731 }
39e88504 1732 search_tbl->is_SGI = !tbl->is_SGI;
eecd6e57 1733 rs_set_expected_tpt_table(lq_sta, search_tbl);
39e88504
GC
1734 if (tbl->is_SGI) {
1735 s32 tpt = lq_sta->last_tpt / 100;
1736 if (tpt >= search_tbl->expected_tpt[index])
1737 break;
1738 }
978785a3
TW
1739 search_tbl->current_rate =
1740 rate_n_flags_from_tbl(priv, search_tbl,
1741 index, is_green);
d6e93399 1742 update_search_tbl_counter = 1;
b481de9c 1743 goto out;
584a0f00
WYG
1744 case IWL_SISO_SWITCH_MIMO3_ABC:
1745 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO3\n");
1746 memcpy(search_tbl, tbl, sz);
1747 search_tbl->is_SGI = 0;
1748 search_tbl->ant_type = ANT_ABC;
1749
1750 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1751 break;
1752
1753 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1754 search_tbl, index);
1755 if (!ret)
1756 goto out;
1757 break;
b481de9c
ZY
1758 }
1759 tbl->action++;
584a0f00 1760 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
3110bef7 1761 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
b481de9c
ZY
1762
1763 if (tbl->action == start_action)
1764 break;
1765 }
3110bef7 1766 search_tbl->lq_type = LQ_NONE;
b481de9c
ZY
1767 return 0;
1768
1769 out:
3110bef7 1770 lq_sta->search_better_tbl = 1;
b481de9c 1771 tbl->action++;
584a0f00 1772 if (tbl->action > IWL_SISO_SWITCH_MIMO3_ABC)
3110bef7 1773 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
d6e93399
MA
1774 if (update_search_tbl_counter)
1775 search_tbl->action = tbl->action;
1776
b481de9c
ZY
1777 return 0;
1778}
1779
77626355 1780/*
584a0f00 1781 * Try to switch to new modulation mode from MIMO2
77626355 1782 */
584a0f00 1783static int rs_move_mimo2_to_other(struct iwl_priv *priv,
e227ceac 1784 struct iwl_lq_sta *lq_sta,
270243a5 1785 struct ieee80211_conf *conf,
4b7679a5 1786 struct ieee80211_sta *sta, int index)
b481de9c 1787{
c33104f0 1788 s8 is_green = lq_sta->is_green;
e227ceac
TW
1789 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1790 struct iwl_scale_tbl_info *search_tbl =
1791 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
3110bef7 1792 struct iwl_rate_scale_data *window = &(tbl->win[index]);
28e6f489 1793 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
e227ceac
TW
1794 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1795 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
b1a78858 1796 u8 start_action;
9e295116
JB
1797 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1798 u8 tx_chains_num = priv->hw_params.tx_chains_num;
d6e93399 1799 u8 update_search_tbl_counter = 0;
aade00ce 1800 int ret;
b481de9c 1801
290f599c
JB
1802 switch (priv->bt_traffic_load) {
1803 case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
1804 /* nothing */
1805 break;
1806 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
1807 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
1808 /* avoid antenna B and MIMO */
da5dbb97
WYG
1809 if (tbl->action != IWL_MIMO2_SWITCH_SISO_A)
1810 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1811 break;
290f599c
JB
1812 case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
1813 /* avoid antenna B unless MIMO */
da5dbb97
WYG
1814 if (tbl->action == IWL_MIMO2_SWITCH_SISO_B ||
1815 tbl->action == IWL_MIMO2_SWITCH_SISO_C)
290f599c
JB
1816 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1817 break;
1818 default:
1819 IWL_ERR(priv, "Invalid BT load %d", priv->bt_traffic_load);
1820 break;
1821 }
1822
3ad3b92a 1823 if ((iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE) &&
46f9381a
WYG
1824 (tbl->action < IWL_MIMO2_SWITCH_SISO_A ||
1825 tbl->action > IWL_MIMO2_SWITCH_SISO_C)) {
1826 /* switch in SISO */
1827 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1828 }
bee008b7
WYG
1829
1830 /* configure as 1x1 if bt full concurrency */
1831 if (priv->bt_full_concurrent &&
1832 (tbl->action < IWL_MIMO2_SWITCH_SISO_A ||
1833 tbl->action > IWL_MIMO2_SWITCH_SISO_C))
1834 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1835
b1a78858 1836 start_action = tbl->action;
b481de9c 1837 for (;;) {
c33104f0 1838 lq_sta->action_counter++;
b481de9c 1839 switch (tbl->action) {
3110bef7
GC
1840 case IWL_MIMO2_SWITCH_ANTENNA1:
1841 case IWL_MIMO2_SWITCH_ANTENNA2:
584a0f00 1842 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle Antennas\n");
3110bef7
GC
1843
1844 if (tx_chains_num <= 2)
1845 break;
1846
1847 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1848 break;
1849
1850 memcpy(search_tbl, tbl, sz);
1851 if (rs_toggle_antenna(valid_tx_ant,
d6e93399
MA
1852 &search_tbl->current_rate, search_tbl)) {
1853 update_search_tbl_counter = 1;
3110bef7 1854 goto out;
d6e93399 1855 }
3110bef7
GC
1856 break;
1857 case IWL_MIMO2_SWITCH_SISO_A:
1858 case IWL_MIMO2_SWITCH_SISO_B:
1859 case IWL_MIMO2_SWITCH_SISO_C:
e1623446 1860 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to SISO\n");
0c11b4de 1861
77626355 1862 /* Set up new search table for SISO */
b481de9c 1863 memcpy(search_tbl, tbl, sz);
39e88504 1864
3110bef7 1865 if (tbl->action == IWL_MIMO2_SWITCH_SISO_A)
fde0db31 1866 search_tbl->ant_type = ANT_A;
3110bef7 1867 else if (tbl->action == IWL_MIMO2_SWITCH_SISO_B)
fde0db31 1868 search_tbl->ant_type = ANT_B;
3110bef7
GC
1869 else
1870 search_tbl->ant_type = ANT_C;
1871
1872 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1873 break;
b481de9c 1874
c33104f0 1875 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
270243a5 1876 search_tbl, index);
3110bef7 1877 if (!ret)
b481de9c 1878 goto out;
3110bef7 1879
b481de9c
ZY
1880 break;
1881
3110bef7 1882 case IWL_MIMO2_SWITCH_GI:
28e6f489
DH
1883 if (!tbl->is_ht40 && !(ht_cap->cap &
1884 IEEE80211_HT_CAP_SGI_20))
a9841013 1885 break;
28e6f489
DH
1886 if (tbl->is_ht40 && !(ht_cap->cap &
1887 IEEE80211_HT_CAP_SGI_40))
a9841013
EG
1888 break;
1889
584a0f00 1890 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle SGI/NGI\n");
77626355 1891
584a0f00 1892 /* Set up new search table for MIMO2 */
b481de9c 1893 memcpy(search_tbl, tbl, sz);
39e88504 1894 search_tbl->is_SGI = !tbl->is_SGI;
eecd6e57 1895 rs_set_expected_tpt_table(lq_sta, search_tbl);
77626355
BC
1896 /*
1897 * If active table already uses the fastest possible
1898 * modulation (dual stream with short guard interval),
1899 * and it's working well, there's no need to look
1900 * for a better type of modulation!
1901 */
39e88504 1902 if (tbl->is_SGI) {
c33104f0 1903 s32 tpt = lq_sta->last_tpt / 100;
39e88504
GC
1904 if (tpt >= search_tbl->expected_tpt[index])
1905 break;
b481de9c 1906 }
978785a3
TW
1907 search_tbl->current_rate =
1908 rate_n_flags_from_tbl(priv, search_tbl,
1909 index, is_green);
d6e93399 1910 update_search_tbl_counter = 1;
b481de9c
ZY
1911 goto out;
1912
584a0f00
WYG
1913 case IWL_MIMO2_SWITCH_MIMO3_ABC:
1914 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to MIMO3\n");
1915 memcpy(search_tbl, tbl, sz);
1916 search_tbl->is_SGI = 0;
1917 search_tbl->ant_type = ANT_ABC;
1918
1919 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1920 break;
1921
1922 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1923 search_tbl, index);
1924 if (!ret)
1925 goto out;
1926
1927 break;
b481de9c
ZY
1928 }
1929 tbl->action++;
584a0f00 1930 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
3110bef7 1931 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
b481de9c
ZY
1932
1933 if (tbl->action == start_action)
1934 break;
1935 }
3110bef7 1936 search_tbl->lq_type = LQ_NONE;
b481de9c
ZY
1937 return 0;
1938 out:
3110bef7 1939 lq_sta->search_better_tbl = 1;
b481de9c 1940 tbl->action++;
584a0f00 1941 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
3110bef7 1942 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
d6e93399
MA
1943 if (update_search_tbl_counter)
1944 search_tbl->action = tbl->action;
1945
b481de9c
ZY
1946 return 0;
1947
1948}
1949
584a0f00
WYG
1950/*
1951 * Try to switch to new modulation mode from MIMO3
1952 */
1953static int rs_move_mimo3_to_other(struct iwl_priv *priv,
1954 struct iwl_lq_sta *lq_sta,
1955 struct ieee80211_conf *conf,
1956 struct ieee80211_sta *sta, int index)
1957{
1958 s8 is_green = lq_sta->is_green;
1959 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1960 struct iwl_scale_tbl_info *search_tbl =
1961 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1962 struct iwl_rate_scale_data *window = &(tbl->win[index]);
28e6f489 1963 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
584a0f00
WYG
1964 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1965 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
b1a78858 1966 u8 start_action;
9e295116
JB
1967 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1968 u8 tx_chains_num = priv->hw_params.tx_chains_num;
584a0f00 1969 int ret;
d6e93399 1970 u8 update_search_tbl_counter = 0;
584a0f00 1971
290f599c
JB
1972 switch (priv->bt_traffic_load) {
1973 case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
1974 /* nothing */
1975 break;
1976 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
1977 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
1978 /* avoid antenna B and MIMO */
da5dbb97 1979 if (tbl->action != IWL_MIMO3_SWITCH_SISO_A)
290f599c 1980 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
da5dbb97 1981 break;
290f599c
JB
1982 case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
1983 /* avoid antenna B unless MIMO */
da5dbb97
WYG
1984 if (tbl->action == IWL_MIMO3_SWITCH_SISO_B ||
1985 tbl->action == IWL_MIMO3_SWITCH_SISO_C)
290f599c 1986 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
290f599c
JB
1987 break;
1988 default:
1989 IWL_ERR(priv, "Invalid BT load %d", priv->bt_traffic_load);
1990 break;
1991 }
1992
3ad3b92a 1993 if ((iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE) &&
46f9381a
WYG
1994 (tbl->action < IWL_MIMO3_SWITCH_SISO_A ||
1995 tbl->action > IWL_MIMO3_SWITCH_SISO_C)) {
1996 /* switch in SISO */
1997 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
1998 }
bee008b7
WYG
1999
2000 /* configure as 1x1 if bt full concurrency */
2001 if (priv->bt_full_concurrent &&
2002 (tbl->action < IWL_MIMO3_SWITCH_SISO_A ||
2003 tbl->action > IWL_MIMO3_SWITCH_SISO_C))
2004 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
2005
b1a78858 2006 start_action = tbl->action;
584a0f00
WYG
2007 for (;;) {
2008 lq_sta->action_counter++;
2009 switch (tbl->action) {
2010 case IWL_MIMO3_SWITCH_ANTENNA1:
2011 case IWL_MIMO3_SWITCH_ANTENNA2:
2012 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle Antennas\n");
2013
2014 if (tx_chains_num <= 3)
2015 break;
2016
2017 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
2018 break;
2019
2020 memcpy(search_tbl, tbl, sz);
2021 if (rs_toggle_antenna(valid_tx_ant,
2022 &search_tbl->current_rate, search_tbl))
2023 goto out;
2024 break;
2025 case IWL_MIMO3_SWITCH_SISO_A:
2026 case IWL_MIMO3_SWITCH_SISO_B:
2027 case IWL_MIMO3_SWITCH_SISO_C:
2028 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to SISO\n");
2029
2030 /* Set up new search table for SISO */
2031 memcpy(search_tbl, tbl, sz);
2032
2033 if (tbl->action == IWL_MIMO3_SWITCH_SISO_A)
2034 search_tbl->ant_type = ANT_A;
2035 else if (tbl->action == IWL_MIMO3_SWITCH_SISO_B)
2036 search_tbl->ant_type = ANT_B;
2037 else
2038 search_tbl->ant_type = ANT_C;
2039
2040 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
2041 break;
2042
2043 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
2044 search_tbl, index);
2045 if (!ret)
2046 goto out;
2047
2048 break;
2049
2050 case IWL_MIMO3_SWITCH_MIMO2_AB:
2051 case IWL_MIMO3_SWITCH_MIMO2_AC:
2052 case IWL_MIMO3_SWITCH_MIMO2_BC:
2053 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to MIMO2\n");
2054
2055 memcpy(search_tbl, tbl, sz);
2056 search_tbl->is_SGI = 0;
2057 if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AB)
2058 search_tbl->ant_type = ANT_AB;
2059 else if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AC)
2060 search_tbl->ant_type = ANT_AC;
2061 else
2062 search_tbl->ant_type = ANT_BC;
2063
2064 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
2065 break;
2066
2067 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
2068 search_tbl, index);
2069 if (!ret)
2070 goto out;
2071
2072 break;
2073
2074 case IWL_MIMO3_SWITCH_GI:
28e6f489
DH
2075 if (!tbl->is_ht40 && !(ht_cap->cap &
2076 IEEE80211_HT_CAP_SGI_20))
584a0f00 2077 break;
28e6f489
DH
2078 if (tbl->is_ht40 && !(ht_cap->cap &
2079 IEEE80211_HT_CAP_SGI_40))
584a0f00
WYG
2080 break;
2081
2082 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle SGI/NGI\n");
2083
2084 /* Set up new search table for MIMO */
2085 memcpy(search_tbl, tbl, sz);
2086 search_tbl->is_SGI = !tbl->is_SGI;
2087 rs_set_expected_tpt_table(lq_sta, search_tbl);
2088 /*
2089 * If active table already uses the fastest possible
2090 * modulation (dual stream with short guard interval),
2091 * and it's working well, there's no need to look
2092 * for a better type of modulation!
2093 */
2094 if (tbl->is_SGI) {
2095 s32 tpt = lq_sta->last_tpt / 100;
2096 if (tpt >= search_tbl->expected_tpt[index])
2097 break;
2098 }
2099 search_tbl->current_rate =
2100 rate_n_flags_from_tbl(priv, search_tbl,
2101 index, is_green);
d6e93399 2102 update_search_tbl_counter = 1;
584a0f00
WYG
2103 goto out;
2104 }
2105 tbl->action++;
2106 if (tbl->action > IWL_MIMO3_SWITCH_GI)
2107 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
2108
2109 if (tbl->action == start_action)
2110 break;
2111 }
2112 search_tbl->lq_type = LQ_NONE;
2113 return 0;
2114 out:
2115 lq_sta->search_better_tbl = 1;
2116 tbl->action++;
2117 if (tbl->action > IWL_MIMO3_SWITCH_GI)
2118 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
d6e93399
MA
2119 if (update_search_tbl_counter)
2120 search_tbl->action = tbl->action;
2121
584a0f00
WYG
2122 return 0;
2123
2124}
2125
77626355
BC
2126/*
2127 * Check whether we should continue using same modulation mode, or
2128 * begin search for a new mode, based on:
2129 * 1) # tx successes or failures while using this mode
2130 * 2) # times calling this function
2131 * 3) elapsed time in this mode (not used, for now)
2132 */
da5dbb97 2133static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search)
b481de9c 2134{
e227ceac 2135 struct iwl_scale_tbl_info *tbl;
b481de9c
ZY
2136 int i;
2137 int active_tbl;
2138 int flush_interval_passed = 0;
bf403db8 2139 struct iwl_priv *priv;
b481de9c 2140
bf403db8 2141 priv = lq_sta->drv;
c33104f0 2142 active_tbl = lq_sta->active_tbl;
b481de9c 2143
c33104f0 2144 tbl = &(lq_sta->lq_info[active_tbl]);
b481de9c 2145
77626355 2146 /* If we've been disallowing search, see if we should now allow it */
c33104f0 2147 if (lq_sta->stay_in_tbl) {
b481de9c 2148
77626355 2149 /* Elapsed time using current modulation mode */
c33104f0 2150 if (lq_sta->flush_timer)
b481de9c 2151 flush_interval_passed =
447fee70
MA
2152 time_after(jiffies,
2153 (unsigned long)(lq_sta->flush_timer +
b481de9c
ZY
2154 IWL_RATE_SCALE_FLUSH_INTVL));
2155
77626355
BC
2156 /*
2157 * Check if we should allow search for new modulation mode.
2158 * If many frames have failed or succeeded, or we've used
2159 * this same modulation for a long time, allow search, and
2160 * reset history stats that keep track of whether we should
2161 * allow a new search. Also (below) reset all bitmaps and
2162 * stats in active history.
2163 */
da5dbb97
WYG
2164 if (force_search ||
2165 (lq_sta->total_failed > lq_sta->max_failure_limit) ||
c33104f0
TW
2166 (lq_sta->total_success > lq_sta->max_success_limit) ||
2167 ((!lq_sta->search_better_tbl) && (lq_sta->flush_timer)
b481de9c 2168 && (flush_interval_passed))) {
0ca24daf 2169 IWL_DEBUG_RATE(priv, "LQ: stay is expired %d %d %d\n",
c33104f0
TW
2170 lq_sta->total_failed,
2171 lq_sta->total_success,
b481de9c 2172 flush_interval_passed);
77626355
BC
2173
2174 /* Allow search for new mode */
c33104f0
TW
2175 lq_sta->stay_in_tbl = 0; /* only place reset */
2176 lq_sta->total_failed = 0;
2177 lq_sta->total_success = 0;
2178 lq_sta->flush_timer = 0;
77626355
BC
2179
2180 /*
2181 * Else if we've used this modulation mode enough repetitions
2182 * (regardless of elapsed time or success/failure), reset
2183 * history bitmaps and rate-specific stats for all rates in
2184 * active table.
2185 */
403ab56b 2186 } else {
c33104f0
TW
2187 lq_sta->table_count++;
2188 if (lq_sta->table_count >=
2189 lq_sta->table_count_limit) {
2190 lq_sta->table_count = 0;
b481de9c 2191
e1623446 2192 IWL_DEBUG_RATE(priv, "LQ: stay in table clear win\n");
b481de9c
ZY
2193 for (i = 0; i < IWL_RATE_COUNT; i++)
2194 rs_rate_scale_clear_window(
2195 &(tbl->win[i]));
2196 }
2197 }
2198
77626355
BC
2199 /* If transitioning to allow "search", reset all history
2200 * bitmaps and stats in active table (this will become the new
2201 * "search" table). */
c33104f0 2202 if (!lq_sta->stay_in_tbl) {
b481de9c
ZY
2203 for (i = 0; i < IWL_RATE_COUNT; i++)
2204 rs_rate_scale_clear_window(&(tbl->win[i]));
2205 }
2206 }
2207}
2208
e3139fe7
WYG
2209/*
2210 * setup rate table in uCode
e3139fe7 2211 */
70817b5e
JB
2212static void rs_update_rate_tbl(struct iwl_priv *priv,
2213 struct iwl_rxon_context *ctx,
2214 struct iwl_lq_sta *lq_sta,
2215 struct iwl_scale_tbl_info *tbl,
2216 int index, u8 is_green)
e3139fe7
WYG
2217{
2218 u32 rate;
2219
2220 /* Update uCode's rate table. */
2221 rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
2222 rs_fill_link_cmd(priv, lq_sta, rate);
7e6a5886 2223 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_ASYNC, false);
e3139fe7
WYG
2224}
2225
77626355
BC
2226/*
2227 * Do rate scaling and search for new modulation mode.
2228 */
c79dd5b5 2229static void rs_rate_scale_perform(struct iwl_priv *priv,
514d65c1 2230 struct sk_buff *skb,
4b7679a5
JB
2231 struct ieee80211_sta *sta,
2232 struct iwl_lq_sta *lq_sta)
b481de9c 2233{
4b7679a5 2234 struct ieee80211_hw *hw = priv->hw;
270243a5 2235 struct ieee80211_conf *conf = &hw->conf;
514d65c1
GS
2236 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2237 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
b481de9c
ZY
2238 int low = IWL_RATE_INVALID;
2239 int high = IWL_RATE_INVALID;
2240 int index;
2241 int i;
e227ceac 2242 struct iwl_rate_scale_data *window = NULL;
b481de9c
ZY
2243 int current_tpt = IWL_INVALID_VALUE;
2244 int low_tpt = IWL_INVALID_VALUE;
2245 int high_tpt = IWL_INVALID_VALUE;
2246 u32 fail_count;
2247 s8 scale_action = 0;
fd7c8a40 2248 u16 rate_mask;
b481de9c 2249 u8 update_lq = 0;
e227ceac 2250 struct iwl_scale_tbl_info *tbl, *tbl1;
b481de9c 2251 u16 rate_scale_index_msk = 0;
b481de9c
ZY
2252 u8 is_green = 0;
2253 u8 active_tbl = 0;
2254 u8 done_search = 0;
2255 u16 high_low;
a5e8b505 2256 s32 sr;
5f85a789 2257 u8 tid = IWL_MAX_TID_COUNT;
86b4766b 2258 struct iwl_tid_data *tid_data;
7e6a5886 2259 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
c6892906 2260 struct iwl_rxon_context *ctx = sta_priv->ctx;
b481de9c 2261
e1623446 2262 IWL_DEBUG_RATE(priv, "rate scale calculate new rate for skb\n");
b481de9c 2263
514d65c1 2264 /* Send management frames and NO_ACK data using lowest rate. */
417f114b
TW
2265 /* TODO: this could probably be improved.. */
2266 if (!ieee80211_is_data(hdr->frame_control) ||
514d65c1 2267 info->flags & IEEE80211_TX_CTL_NO_ACK)
b481de9c 2268 return;
b481de9c 2269
4b7679a5 2270 lq_sta->supp_rates = sta->supp_rates[lq_sta->band];
b481de9c 2271
faa29718 2272 tid = rs_tl_add_packet(lq_sta, hdr);
5f85a789
EG
2273 if ((tid != IWL_MAX_TID_COUNT) &&
2274 (lq_sta->tx_agg_tid_en & (1 << tid))) {
04cf6824 2275 tid_data = &priv->tid_data[lq_sta->lq.sta_id][tid];
e3949d62
DH
2276 if (tid_data->agg.state == IWL_AGG_OFF)
2277 lq_sta->is_agg = 0;
2278 else
2279 lq_sta->is_agg = 1;
2280 } else
2281 lq_sta->is_agg = 0;
faa29718 2282
77626355
BC
2283 /*
2284 * Select rate-scale / modulation-mode table to work with in
2285 * the rest of this function: "search" if searching for better
2286 * modulation mode, or "active" if doing rate scaling within a mode.
2287 */
c33104f0
TW
2288 if (!lq_sta->search_better_tbl)
2289 active_tbl = lq_sta->active_tbl;
b481de9c 2290 else
c33104f0 2291 active_tbl = 1 - lq_sta->active_tbl;
b481de9c 2292
c33104f0 2293 tbl = &(lq_sta->lq_info[active_tbl]);
2681b20b
WYG
2294 if (is_legacy(tbl->lq_type))
2295 lq_sta->is_green = 0;
2296 else
7e6a5886 2297 lq_sta->is_green = rs_use_green(sta);
c33104f0 2298 is_green = lq_sta->is_green;
b481de9c 2299
77626355 2300 /* current tx rate */
b7e35008 2301 index = lq_sta->last_txrate_idx;
b481de9c 2302
e1623446 2303 IWL_DEBUG_RATE(priv, "Rate scale index %d for type %d\n", index,
b481de9c
ZY
2304 tbl->lq_type);
2305
77626355 2306 /* rates available for this association, and for modulation mode */
eecd6e57 2307 rate_mask = rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
b481de9c 2308
91dd6c27 2309 IWL_DEBUG_RATE(priv, "mask 0x%04X\n", rate_mask);
b481de9c
ZY
2310
2311 /* mask with station rate restriction */
2312 if (is_legacy(tbl->lq_type)) {
8318d78a 2313 if (lq_sta->band == IEEE80211_BAND_5GHZ)
77626355 2314 /* supp_rates has no CCK bits in A mode */
b481de9c 2315 rate_scale_index_msk = (u16) (rate_mask &
c33104f0 2316 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
b481de9c
ZY
2317 else
2318 rate_scale_index_msk = (u16) (rate_mask &
c33104f0 2319 lq_sta->supp_rates);
b481de9c
ZY
2320
2321 } else
2322 rate_scale_index_msk = rate_mask;
2323
2324 if (!rate_scale_index_msk)
2325 rate_scale_index_msk = rate_mask;
2326
07bc28ed 2327 if (!((1 << index) & rate_scale_index_msk)) {
15b1687c 2328 IWL_ERR(priv, "Current Rate is not valid\n");
e3139fe7
WYG
2329 if (lq_sta->search_better_tbl) {
2330 /* revert to active table if search table is not valid*/
2331 tbl->lq_type = LQ_NONE;
2332 lq_sta->search_better_tbl = 0;
2333 tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
2334 /* get "active" rate info */
2335 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
70817b5e
JB
2336 rs_update_rate_tbl(priv, ctx, lq_sta, tbl,
2337 index, is_green);
e3139fe7 2338 }
07bc28ed 2339 return;
b481de9c
ZY
2340 }
2341
77626355 2342 /* Get expected throughput table and history window for current rate */
07bc28ed 2343 if (!tbl->expected_tpt) {
15b1687c 2344 IWL_ERR(priv, "tbl->expected_tpt is NULL\n");
07bc28ed
GC
2345 return;
2346 }
b481de9c 2347
c6ec7a9b
AM
2348 /* force user max rate if set by user */
2349 if ((lq_sta->max_rate_idx != -1) &&
2350 (lq_sta->max_rate_idx < index)) {
2351 index = lq_sta->max_rate_idx;
2352 update_lq = 1;
2353 window = &(tbl->win[index]);
2354 goto lq_update;
2355 }
2356
b481de9c
ZY
2357 window = &(tbl->win[index]);
2358
77626355
BC
2359 /*
2360 * If there is not enough history to calculate actual average
2361 * throughput, keep analyzing results of more tx frames, without
2362 * changing rate or mode (bypass most of the rest of this function).
2363 * Set up new rate table in uCode only if old rate is not supported
2364 * in current association (use new rate found above).
2365 */
b481de9c 2366 fail_count = window->counter - window->success_counter;
07bc28ed
GC
2367 if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
2368 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
e1623446 2369 IWL_DEBUG_RATE(priv, "LQ: still below TH. succ=%d total=%d "
b481de9c
ZY
2370 "for index %d\n",
2371 window->success_counter, window->counter, index);
77626355
BC
2372
2373 /* Can't calculate this yet; not enough history */
b481de9c 2374 window->average_tpt = IWL_INVALID_VALUE;
77626355
BC
2375
2376 /* Should we stay with this modulation mode,
2377 * or search for a new one? */
da5dbb97 2378 rs_stay_in_table(lq_sta, false);
77626355 2379
b481de9c 2380 goto out;
3110bef7 2381 }
77626355
BC
2382 /* Else we have enough samples; calculate estimate of
2383 * actual average throughput */
3d79b2a9
RC
2384 if (window->average_tpt != ((window->success_ratio *
2385 tbl->expected_tpt[index] + 64) / 128)) {
2386 IWL_ERR(priv, "expected_tpt should have been calculated by now\n");
2387 window->average_tpt = ((window->success_ratio *
2388 tbl->expected_tpt[index] + 64) / 128);
2389 }
b481de9c 2390
77626355 2391 /* If we are searching for better modulation mode, check success. */
46f9381a 2392 if (lq_sta->search_better_tbl &&
3ad3b92a 2393 (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_MULTI)) {
77626355
BC
2394 /* If good success, continue using the "search" mode;
2395 * no need to send new link quality command, since we're
2396 * continuing to use the setup that we've been trying. */
07bc28ed
GC
2397 if (window->average_tpt > lq_sta->last_tpt) {
2398
e1623446 2399 IWL_DEBUG_RATE(priv, "LQ: SWITCHING TO NEW TABLE "
07bc28ed
GC
2400 "suc=%d cur-tpt=%d old-tpt=%d\n",
2401 window->success_ratio,
2402 window->average_tpt,
2403 lq_sta->last_tpt);
2404
2405 if (!is_legacy(tbl->lq_type))
c33104f0 2406 lq_sta->enable_counter = 1;
07bc28ed 2407
77626355 2408 /* Swap tables; "search" becomes "active" */
c33104f0 2409 lq_sta->active_tbl = active_tbl;
b481de9c 2410 current_tpt = window->average_tpt;
77626355
BC
2411
2412 /* Else poor success; go back to mode in "active" table */
b481de9c 2413 } else {
07bc28ed 2414
e1623446 2415 IWL_DEBUG_RATE(priv, "LQ: GOING BACK TO THE OLD TABLE "
07bc28ed
GC
2416 "suc=%d cur-tpt=%d old-tpt=%d\n",
2417 window->success_ratio,
2418 window->average_tpt,
2419 lq_sta->last_tpt);
2420
77626355 2421 /* Nullify "search" table */
b481de9c 2422 tbl->lq_type = LQ_NONE;
77626355
BC
2423
2424 /* Revert to "active" table */
c33104f0
TW
2425 active_tbl = lq_sta->active_tbl;
2426 tbl = &(lq_sta->lq_info[active_tbl]);
b481de9c 2427
77626355 2428 /* Revert to "active" rate and throughput info */
e7d326ac 2429 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
c33104f0 2430 current_tpt = lq_sta->last_tpt;
b481de9c 2431
77626355 2432 /* Need to set up a new rate table in uCode */
b481de9c 2433 update_lq = 1;
b481de9c 2434 }
77626355
BC
2435
2436 /* Either way, we've made a decision; modulation mode
2437 * search is done, allow rate adjustment next time. */
c33104f0 2438 lq_sta->search_better_tbl = 0;
77626355 2439 done_search = 1; /* Don't switch modes below! */
b481de9c
ZY
2440 goto lq_update;
2441 }
2442
77626355
BC
2443 /* (Else) not in search of better modulation mode, try for better
2444 * starting rate, while staying in this mode. */
bf403db8 2445 high_low = rs_get_adjacent_rate(priv, index, rate_scale_index_msk,
b481de9c
ZY
2446 tbl->lq_type);
2447 low = high_low & 0xff;
2448 high = (high_low >> 8) & 0xff;
2449
c6ec7a9b
AM
2450 /* If user set max rate, dont allow higher than user constrain */
2451 if ((lq_sta->max_rate_idx != -1) &&
2452 (lq_sta->max_rate_idx < high))
2453 high = IWL_RATE_INVALID;
2454
a5e8b505
GC
2455 sr = window->success_ratio;
2456
77626355 2457 /* Collect measured throughputs for current and adjacent rates */
b481de9c 2458 current_tpt = window->average_tpt;
b481de9c
ZY
2459 if (low != IWL_RATE_INVALID)
2460 low_tpt = tbl->win[low].average_tpt;
b481de9c
ZY
2461 if (high != IWL_RATE_INVALID)
2462 high_tpt = tbl->win[high].average_tpt;
2463
a5e8b505 2464 scale_action = 0;
b481de9c 2465
77626355 2466 /* Too many failures, decrease rate */
a5e8b505 2467 if ((sr <= IWL_RATE_DECREASE_TH) || (current_tpt == 0)) {
e1623446 2468 IWL_DEBUG_RATE(priv, "decrease rate because of low success_ratio\n");
b481de9c 2469 scale_action = -1;
77626355
BC
2470
2471 /* No throughput measured yet for adjacent rates; try increase. */
b481de9c 2472 } else if ((low_tpt == IWL_INVALID_VALUE) &&
a5e8b505
GC
2473 (high_tpt == IWL_INVALID_VALUE)) {
2474
2475 if (high != IWL_RATE_INVALID && sr >= IWL_RATE_INCREASE_TH)
2476 scale_action = 1;
2477 else if (low != IWL_RATE_INVALID)
8fe72311 2478 scale_action = 0;
a5e8b505 2479 }
77626355
BC
2480
2481 /* Both adjacent throughputs are measured, but neither one has better
2482 * throughput; we're using the best rate, don't change it! */
b481de9c
ZY
2483 else if ((low_tpt != IWL_INVALID_VALUE) &&
2484 (high_tpt != IWL_INVALID_VALUE) &&
2485 (low_tpt < current_tpt) &&
2486 (high_tpt < current_tpt))
2487 scale_action = 0;
77626355
BC
2488
2489 /* At least one adjacent rate's throughput is measured,
2490 * and may have better performance. */
b481de9c 2491 else {
77626355 2492 /* Higher adjacent rate's throughput is measured */
b481de9c 2493 if (high_tpt != IWL_INVALID_VALUE) {
77626355 2494 /* Higher rate has better throughput */
a5e8b505
GC
2495 if (high_tpt > current_tpt &&
2496 sr >= IWL_RATE_INCREASE_TH) {
b481de9c 2497 scale_action = 1;
a5e8b505 2498 } else {
8fe72311 2499 scale_action = 0;
b481de9c 2500 }
77626355
BC
2501
2502 /* Lower adjacent rate's throughput is measured */
b481de9c 2503 } else if (low_tpt != IWL_INVALID_VALUE) {
77626355 2504 /* Lower rate has better throughput */
b481de9c 2505 if (low_tpt > current_tpt) {
e1623446
TW
2506 IWL_DEBUG_RATE(priv,
2507 "decrease rate because of low tpt\n");
b481de9c 2508 scale_action = -1;
a5e8b505 2509 } else if (sr >= IWL_RATE_INCREASE_TH) {
b481de9c 2510 scale_action = 1;
a5e8b505 2511 }
b481de9c
ZY
2512 }
2513 }
2514
77626355
BC
2515 /* Sanity check; asked for decrease, but success rate or throughput
2516 * has been good at old rate. Don't change it. */
a5e8b505
GC
2517 if ((scale_action == -1) && (low != IWL_RATE_INVALID) &&
2518 ((sr > IWL_RATE_HIGH_TH) ||
b481de9c 2519 (current_tpt > (100 * tbl->expected_tpt[low]))))
b481de9c 2520 scale_action = 0;
46f9381a
WYG
2521 if (!iwl_ht_enabled(priv) && !is_legacy(tbl->lq_type))
2522 scale_action = -1;
3ad3b92a 2523 if (iwl_tx_ant_restriction(priv) != IWL_ANT_OK_MULTI &&
46f9381a
WYG
2524 (is_mimo2(tbl->lq_type) || is_mimo3(tbl->lq_type)))
2525 scale_action = -1;
290f599c 2526
da5dbb97
WYG
2527 if ((priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH) &&
2528 (is_mimo2(tbl->lq_type) || is_mimo3(tbl->lq_type))) {
2529 if (lq_sta->last_bt_traffic > priv->bt_traffic_load) {
2530 /*
2531 * don't set scale_action, don't want to scale up if
2532 * the rate scale doesn't otherwise think that is a
2533 * good idea.
2534 */
2535 } else if (lq_sta->last_bt_traffic <= priv->bt_traffic_load) {
2536 scale_action = -1;
2537 }
2538 }
2539 lq_sta->last_bt_traffic = priv->bt_traffic_load;
2540
2541 if ((priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH) &&
2542 (is_mimo2(tbl->lq_type) || is_mimo3(tbl->lq_type))) {
2543 /* search for a new modulation */
2544 rs_stay_in_table(lq_sta, true);
2545 goto lq_update;
290f599c
JB
2546 }
2547
b481de9c
ZY
2548 switch (scale_action) {
2549 case -1:
77626355 2550 /* Decrease starting rate, update uCode's rate table */
b481de9c
ZY
2551 if (low != IWL_RATE_INVALID) {
2552 update_lq = 1;
2553 index = low;
2554 }
447fee70 2555
b481de9c
ZY
2556 break;
2557 case 1:
77626355 2558 /* Increase starting rate, update uCode's rate table */
b481de9c
ZY
2559 if (high != IWL_RATE_INVALID) {
2560 update_lq = 1;
2561 index = high;
2562 }
2563
2564 break;
2565 case 0:
77626355 2566 /* No change */
b481de9c
ZY
2567 default:
2568 break;
2569 }
2570
e1623446 2571 IWL_DEBUG_RATE(priv, "choose rate scale index %d action %d low %d "
b481de9c
ZY
2572 "high %d type %d\n",
2573 index, scale_action, low, high, tbl->lq_type);
2574
a5e8b505 2575lq_update:
77626355 2576 /* Replace uCode's rate table for the destination station. */
e3139fe7 2577 if (update_lq)
70817b5e 2578 rs_update_rate_tbl(priv, ctx, lq_sta, tbl, index, is_green);
77626355 2579
3ad3b92a 2580 if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_MULTI) {
46f9381a
WYG
2581 /* Should we stay with this modulation mode,
2582 * or search for a new one? */
da5dbb97 2583 rs_stay_in_table(lq_sta, false);
46f9381a 2584 }
77626355
BC
2585 /*
2586 * Search for new modulation mode if we're:
2587 * 1) Not changing rates right now
2588 * 2) Not just finishing up a search
2589 * 3) Allowing a new search
2590 */
47cfd463 2591 if (!update_lq && !done_search && !lq_sta->stay_in_tbl && window->counter) {
77626355 2592 /* Save current throughput to compare with "search" throughput*/
c33104f0 2593 lq_sta->last_tpt = current_tpt;
b481de9c 2594
77626355
BC
2595 /* Select a new "search" modulation mode to try.
2596 * If one is found, set up the new "search" table. */
b481de9c 2597 if (is_legacy(tbl->lq_type))
c33104f0 2598 rs_move_legacy_other(priv, lq_sta, conf, sta, index);
b481de9c 2599 else if (is_siso(tbl->lq_type))
c33104f0 2600 rs_move_siso_to_other(priv, lq_sta, conf, sta, index);
584a0f00
WYG
2601 else if (is_mimo2(tbl->lq_type))
2602 rs_move_mimo2_to_other(priv, lq_sta, conf, sta, index);
b481de9c 2603 else
584a0f00 2604 rs_move_mimo3_to_other(priv, lq_sta, conf, sta, index);
b481de9c 2605
77626355 2606 /* If new "search" mode was selected, set up in uCode table */
c33104f0 2607 if (lq_sta->search_better_tbl) {
77626355 2608 /* Access the "search" table, clear its history. */
c33104f0 2609 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
b481de9c
ZY
2610 for (i = 0; i < IWL_RATE_COUNT; i++)
2611 rs_rate_scale_clear_window(&(tbl->win[i]));
2612
77626355 2613 /* Use new "search" start rate */
e7d326ac 2614 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
b481de9c 2615
e1623446 2616 IWL_DEBUG_RATE(priv, "Switch current mcs: %X index: %d\n",
39e88504 2617 tbl->current_rate, index);
07bc28ed 2618 rs_fill_link_cmd(priv, lq_sta, tbl->current_rate);
7e6a5886 2619 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_ASYNC, false);
447fee70
MA
2620 } else
2621 done_search = 1;
2622 }
b481de9c 2623
447fee70 2624 if (done_search && !lq_sta->stay_in_tbl) {
77626355
BC
2625 /* If the "active" (non-search) mode was legacy,
2626 * and we've tried switching antennas,
2627 * but we haven't been able to try HT modes (not available),
2628 * stay with best antenna legacy modulation for a while
2629 * before next round of mode comparisons. */
c33104f0 2630 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
de27e64e 2631 if (is_legacy(tbl1->lq_type) && !conf_is_ht(conf) &&
d6e93399 2632 lq_sta->action_counter > tbl1->max_search) {
e1623446 2633 IWL_DEBUG_RATE(priv, "LQ: STAY in legacy table\n");
bf403db8 2634 rs_set_stay_in_table(priv, 1, lq_sta);
b481de9c
ZY
2635 }
2636
77626355
BC
2637 /* If we're in an HT mode, and all 3 mode switch actions
2638 * have been tried and compared, stay in this best modulation
2639 * mode for a while before next round of mode comparisons. */
c33104f0 2640 if (lq_sta->enable_counter &&
46f9381a
WYG
2641 (lq_sta->action_counter >= tbl1->max_search) &&
2642 iwl_ht_enabled(priv)) {
0c11b4de
RR
2643 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2644 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
5f85a789
EG
2645 (tid != IWL_MAX_TID_COUNT)) {
2646 u8 sta_id = lq_sta->lq.sta_id;
04cf6824 2647 tid_data = &priv->tid_data[sta_id][tid];
86b4766b
WYG
2648 if (tid_data->agg.state == IWL_AGG_OFF) {
2649 IWL_DEBUG_RATE(priv,
2650 "try to aggregate tid %d\n",
2651 tid);
2652 rs_tl_turn_on_agg(priv, tid,
2653 lq_sta, sta);
2654 }
b481de9c 2655 }
bf403db8 2656 rs_set_stay_in_table(priv, 0, lq_sta);
b481de9c 2657 }
b481de9c
ZY
2658 }
2659
2660out:
978785a3 2661 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
c3322b30 2662 lq_sta->last_txrate_idx = index;
b481de9c
ZY
2663}
2664
fe6b23dd
RC
2665/**
2666 * rs_initialize_lq - Initialize a station's hardware rate table
2667 *
2668 * The uCode's station table contains a table of fallback rates
2669 * for automatic fallback during transmission.
2670 *
2671 * NOTE: This sets up a default set of values. These will be replaced later
2672 * if the driver's iwl-agn-rs rate scaling algorithm is used, instead of
2673 * rc80211_simple.
2674 *
2675 * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
2676 * calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
2677 * which requires station table entry to exist).
2678 */
c79dd5b5 2679static void rs_initialize_lq(struct iwl_priv *priv,
4b7679a5
JB
2680 struct ieee80211_sta *sta,
2681 struct iwl_lq_sta *lq_sta)
b481de9c 2682{
e227ceac 2683 struct iwl_scale_tbl_info *tbl;
b481de9c 2684 int rate_idx;
aade00ce 2685 int i;
39e88504 2686 u32 rate;
7e6a5886 2687 u8 use_green = rs_use_green(sta);
aade00ce
GC
2688 u8 active_tbl = 0;
2689 u8 valid_tx_ant;
7e6a5886
JB
2690 struct iwl_station_priv *sta_priv;
2691 struct iwl_rxon_context *ctx;
b481de9c 2692
4b7679a5 2693 if (!sta || !lq_sta)
7e6a5886
JB
2694 return;
2695
2696 sta_priv = (void *)sta->drv_priv;
c6892906 2697 ctx = sta_priv->ctx;
b481de9c 2698
b7e35008 2699 i = lq_sta->last_txrate_idx;
b481de9c 2700
9e295116 2701 valid_tx_ant = priv->hw_params.valid_tx_ant;
aade00ce 2702
c33104f0
TW
2703 if (!lq_sta->search_better_tbl)
2704 active_tbl = lq_sta->active_tbl;
b481de9c 2705 else
c33104f0 2706 active_tbl = 1 - lq_sta->active_tbl;
b481de9c 2707
c33104f0 2708 tbl = &(lq_sta->lq_info[active_tbl]);
b481de9c
ZY
2709
2710 if ((i < 0) || (i >= IWL_RATE_COUNT))
2711 i = 0;
2712
1826dcc0 2713 rate = iwl_rates[i].plcp;
eb48dcaf
WT
2714 tbl->ant_type = first_antenna(valid_tx_ant);
2715 rate |= tbl->ant_type << RATE_MCS_ANT_POS;
b481de9c
ZY
2716
2717 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
39e88504 2718 rate |= RATE_MCS_CCK_MSK;
b481de9c 2719
39e88504 2720 rs_get_tbl_info_from_mcs(rate, priv->band, tbl, &rate_idx);
aade00ce
GC
2721 if (!rs_is_valid_ant(valid_tx_ant, tbl->ant_type))
2722 rs_toggle_antenna(valid_tx_ant, &rate, tbl);
b481de9c 2723
978785a3 2724 rate = rate_n_flags_from_tbl(priv, tbl, rate_idx, use_green);
39e88504 2725 tbl->current_rate = rate;
eecd6e57 2726 rs_set_expected_tpt_table(lq_sta, tbl);
07bc28ed 2727 rs_fill_link_cmd(NULL, lq_sta, rate);
fe6b23dd 2728 priv->stations[lq_sta->lq.sta_id].lq = &lq_sta->lq;
7e6a5886 2729 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_SYNC, true);
b481de9c
ZY
2730}
2731
e6a9854b
JB
2732static void rs_get_rate(void *priv_r, struct ieee80211_sta *sta, void *priv_sta,
2733 struct ieee80211_tx_rate_control *txrc)
b481de9c
ZY
2734{
2735
e6a9854b
JB
2736 struct sk_buff *skb = txrc->skb;
2737 struct ieee80211_supported_band *sband = txrc->sband;
d0f76d68
EG
2738 struct iwl_op_mode *op_mode __maybe_unused =
2739 (struct iwl_op_mode *)priv_r;
2740 struct iwl_priv *priv __maybe_unused = IWL_OP_MODE_GET_DVM(op_mode);
e6a9854b 2741 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
417f114b
TW
2742 struct iwl_lq_sta *lq_sta = priv_sta;
2743 int rate_idx;
b481de9c 2744
e1623446 2745 IWL_DEBUG_RATE_LIMIT(priv, "rate scale calculate new rate for skb\n");
b481de9c 2746
c6ec7a9b
AM
2747 /* Get max rate if user set max rate */
2748 if (lq_sta) {
2749 lq_sta->max_rate_idx = txrc->max_rate_idx;
2750 if ((sband->band == IEEE80211_BAND_5GHZ) &&
2751 (lq_sta->max_rate_idx != -1))
2752 lq_sta->max_rate_idx += IWL_FIRST_OFDM_RATE;
2753 if ((lq_sta->max_rate_idx < 0) ||
2754 (lq_sta->max_rate_idx >= IWL_RATE_COUNT))
2755 lq_sta->max_rate_idx = -1;
2756 }
2757
fe6b23dd
RC
2758 /* Treat uninitialized rate scaling data same as non-existing. */
2759 if (lq_sta && !lq_sta->drv) {
2760 IWL_DEBUG_RATE(priv, "Rate scaling not initialized yet.\n");
2761 priv_sta = NULL;
2762 }
2763
514d65c1 2764 /* Send management frames and NO_ACK data using lowest rate. */
4c6d4f5c 2765 if (rate_control_send_low(sta, priv_sta, txrc))
4b7679a5 2766 return;
b481de9c 2767
417f114b 2768 rate_idx = lq_sta->last_txrate_idx;
b481de9c 2769
a9c146b3 2770 if (lq_sta->last_rate_n_flags & RATE_MCS_HT_MSK) {
417f114b 2771 rate_idx -= IWL_FIRST_OFDM_RATE;
a9c146b3
WYG
2772 /* 6M and 9M shared same MCS index */
2773 rate_idx = (rate_idx > 0) ? (rate_idx - 1) : 0;
2774 if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
2775 IWL_RATE_MIMO3_6M_PLCP)
2776 rate_idx = rate_idx + (2 * MCS_INDEX_PER_STREAM);
2777 else if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
2778 IWL_RATE_MIMO2_6M_PLCP)
2779 rate_idx = rate_idx + MCS_INDEX_PER_STREAM;
2780 info->control.rates[0].flags = IEEE80211_TX_RC_MCS;
2781 if (lq_sta->last_rate_n_flags & RATE_MCS_SGI_MSK)
2782 info->control.rates[0].flags |= IEEE80211_TX_RC_SHORT_GI;
2783 if (lq_sta->last_rate_n_flags & RATE_MCS_DUP_MSK)
2784 info->control.rates[0].flags |= IEEE80211_TX_RC_DUP_DATA;
7aafef1c 2785 if (lq_sta->last_rate_n_flags & RATE_MCS_HT40_MSK)
a9c146b3
WYG
2786 info->control.rates[0].flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
2787 if (lq_sta->last_rate_n_flags & RATE_MCS_GF_MSK)
2788 info->control.rates[0].flags |= IEEE80211_TX_RC_GREEN_FIELD;
2789 } else {
5027309b
DH
2790 /* Check for invalid rates */
2791 if ((rate_idx < 0) || (rate_idx >= IWL_RATE_COUNT_LEGACY) ||
2792 ((sband->band == IEEE80211_BAND_5GHZ) &&
2793 (rate_idx < IWL_FIRST_OFDM_RATE)))
a9c146b3 2794 rate_idx = rate_lowest_index(sband, sta);
5027309b 2795 /* On valid 5 GHz rate, adjust index */
a9c146b3
WYG
2796 else if (sband->band == IEEE80211_BAND_5GHZ)
2797 rate_idx -= IWL_FIRST_OFDM_RATE;
7ebaeff8 2798 info->control.rates[0].flags = 0;
a9c146b3 2799 }
417f114b 2800 info->control.rates[0].idx = rate_idx;
a9c146b3 2801
b481de9c
ZY
2802}
2803
4b7679a5
JB
2804static void *rs_alloc_sta(void *priv_rate, struct ieee80211_sta *sta,
2805 gfp_t gfp)
b481de9c 2806{
8d9698b3 2807 struct iwl_station_priv *sta_priv = (struct iwl_station_priv *) sta->drv_priv;
d0f76d68
EG
2808 struct iwl_op_mode *op_mode __maybe_unused =
2809 (struct iwl_op_mode *)priv_rate;
2810 struct iwl_priv *priv __maybe_unused = IWL_OP_MODE_GET_DVM(op_mode);
b481de9c 2811
e1623446 2812 IWL_DEBUG_RATE(priv, "create station rate scale window\n");
b481de9c 2813
08960dea 2814 return &sta_priv->lq_sta;
b481de9c
ZY
2815}
2816
fe6b23dd
RC
2817/*
2818 * Called after adding a new station to initialize rate scaling
2819 */
2820void iwl_rs_rate_init(struct iwl_priv *priv, struct ieee80211_sta *sta, u8 sta_id)
b481de9c
ZY
2821{
2822 int i, j;
fe6b23dd 2823 struct ieee80211_hw *hw = priv->hw;
4b7679a5 2824 struct ieee80211_conf *conf = &priv->hw->conf;
7869b0ea 2825 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
fe6b23dd
RC
2826 struct iwl_station_priv *sta_priv;
2827 struct iwl_lq_sta *lq_sta;
2828 struct ieee80211_supported_band *sband;
3ac40eda 2829 unsigned long supp; /* must be unsigned long for for_each_set_bit */
fe6b23dd
RC
2830
2831 sta_priv = (struct iwl_station_priv *) sta->drv_priv;
2832 lq_sta = &sta_priv->lq_sta;
2833 sband = hw->wiphy->bands[conf->channel->band];
b481de9c 2834
fe6b23dd
RC
2835
2836 lq_sta->lq.sta_id = sta_id;
5ad13f8c
RC
2837
2838 for (j = 0; j < LQ_SIZE; j++)
2839 for (i = 0; i < IWL_RATE_COUNT; i++)
2840 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
2841
c33104f0 2842 lq_sta->flush_timer = 0;
4b7679a5 2843 lq_sta->supp_rates = sta->supp_rates[sband->band];
b481de9c
ZY
2844 for (j = 0; j < LQ_SIZE; j++)
2845 for (i = 0; i < IWL_RATE_COUNT; i++)
3ac7f146 2846 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
b481de9c 2847
fe6b23dd
RC
2848 IWL_DEBUG_RATE(priv, "LQ: *** rate scale station global init for station %d ***\n",
2849 sta_id);
b481de9c
ZY
2850 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2851 * the lowest or the highest rate.. Could consider using RSSI from
2852 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2853 * after assoc.. */
2854
c33104f0 2855 lq_sta->is_dup = 0;
c6ec7a9b 2856 lq_sta->max_rate_idx = -1;
7d049e5a 2857 lq_sta->missed_rate_counter = IWL_MISSED_RATE_MAX;
7e6a5886 2858 lq_sta->is_green = rs_use_green(sta);
3ac40eda
JB
2859 lq_sta->band = sband->band;
2860 /*
2861 * active legacy rates as per supported rates bitmap
2862 */
2863 supp = sta->supp_rates[sband->band];
2864 lq_sta->active_legacy_rate = 0;
2865 for_each_set_bit(i, &supp, BITS_PER_LONG)
2866 lq_sta->active_legacy_rate |= BIT(sband->bitrates[i].hw_value);
2867
77626355
BC
2868 /*
2869 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2870 * supp_rates[] does not; shift to convert format, force 9 MBits off.
2871 */
7869b0ea
DH
2872 lq_sta->active_siso_rate = ht_cap->mcs.rx_mask[0] << 1;
2873 lq_sta->active_siso_rate |= ht_cap->mcs.rx_mask[0] & 0x1;
c33104f0 2874 lq_sta->active_siso_rate &= ~((u16)0x2);
fde0db31 2875 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
b481de9c 2876
77626355 2877 /* Same here */
7869b0ea
DH
2878 lq_sta->active_mimo2_rate = ht_cap->mcs.rx_mask[1] << 1;
2879 lq_sta->active_mimo2_rate |= ht_cap->mcs.rx_mask[1] & 0x1;
fde0db31
GC
2880 lq_sta->active_mimo2_rate &= ~((u16)0x2);
2881 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2882
7869b0ea
DH
2883 lq_sta->active_mimo3_rate = ht_cap->mcs.rx_mask[2] << 1;
2884 lq_sta->active_mimo3_rate |= ht_cap->mcs.rx_mask[2] & 0x1;
fde0db31
GC
2885 lq_sta->active_mimo3_rate &= ~((u16)0x2);
2886 lq_sta->active_mimo3_rate <<= IWL_FIRST_OFDM_RATE;
2887
e1623446 2888 IWL_DEBUG_RATE(priv, "SISO-RATE=%X MIMO2-RATE=%X MIMO3-RATE=%X\n",
c33104f0 2889 lq_sta->active_siso_rate,
fde0db31
GC
2890 lq_sta->active_mimo2_rate,
2891 lq_sta->active_mimo3_rate);
2892
a96a27f9 2893 /* These values will be overridden later */
3a23d695 2894 lq_sta->lq.general_params.single_stream_ant_msk =
9e295116 2895 first_antenna(priv->hw_params.valid_tx_ant);
3a23d695 2896 lq_sta->lq.general_params.dual_stream_ant_msk =
9e295116
JB
2897 priv->hw_params.valid_tx_ant &
2898 ~first_antenna(priv->hw_params.valid_tx_ant);
3a23d695
WYG
2899 if (!lq_sta->lq.general_params.dual_stream_ant_msk) {
2900 lq_sta->lq.general_params.dual_stream_ant_msk = ANT_AB;
9e295116 2901 } else if (num_of_ant(priv->hw_params.valid_tx_ant) == 2) {
3a23d695 2902 lq_sta->lq.general_params.dual_stream_ant_msk =
9e295116 2903 priv->hw_params.valid_tx_ant;
3a23d695 2904 }
07bc28ed 2905
0c11b4de
RR
2906 /* as default allow aggregation for all tids */
2907 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
c33104f0 2908 lq_sta->drv = priv;
b481de9c 2909
5027309b
DH
2910 /* Set last_txrate_idx to lowest rate */
2911 lq_sta->last_txrate_idx = rate_lowest_index(sband, sta);
2912 if (sband->band == IEEE80211_BAND_5GHZ)
2913 lq_sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
e3949d62 2914 lq_sta->is_agg = 0;
5ef15ccc 2915#ifdef CONFIG_IWLWIFI_DEVICE_TESTMODE
4e308119 2916 priv->tm_fixed_rate = 0;
c10e2c10 2917#endif
7d5f01ad 2918#ifdef CONFIG_MAC80211_DEBUGFS
a1da077b 2919 lq_sta->dbg_fixed_rate = 0;
7d5f01ad 2920#endif
a1da077b 2921
9b6ca448 2922 rs_initialize_lq(priv, sta, lq_sta);
b481de9c
ZY
2923}
2924
46f9381a 2925static void rs_fill_link_cmd(struct iwl_priv *priv,
e227ceac 2926 struct iwl_lq_sta *lq_sta, u32 new_rate)
b481de9c 2927{
e227ceac 2928 struct iwl_scale_tbl_info tbl_type;
b481de9c 2929 int index = 0;
b481de9c 2930 int rate_idx;
1b696de2 2931 int repeat_rate = 0;
aade00ce 2932 u8 ant_toggle_cnt = 0;
b481de9c 2933 u8 use_ht_possible = 1;
aade00ce 2934 u8 valid_tx_ant = 0;
7b090687
JB
2935 struct iwl_station_priv *sta_priv =
2936 container_of(lq_sta, struct iwl_station_priv, lq_sta);
07bc28ed 2937 struct iwl_link_quality_cmd *lq_cmd = &lq_sta->lq;
b481de9c 2938
77626355 2939 /* Override starting rate (index 0) if needed for debug purposes */
39e88504 2940 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
98d7e09a 2941
39e88504
GC
2942 /* Interpret new_rate (rate_n_flags) */
2943 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
b481de9c
ZY
2944 &tbl_type, &rate_idx);
2945
da5dbb97
WYG
2946 if (priv && priv->bt_full_concurrent) {
2947 /* 1x1 only */
2948 tbl_type.ant_type =
9e295116 2949 first_antenna(priv->hw_params.valid_tx_ant);
da5dbb97
WYG
2950 }
2951
77626355 2952 /* How many times should we repeat the initial rate? */
b481de9c 2953 if (is_legacy(tbl_type.lq_type)) {
aade00ce 2954 ant_toggle_cnt = 1;
1b696de2 2955 repeat_rate = IWL_NUMBER_TRY;
aade00ce 2956 } else {
374920cb
DH
2957 repeat_rate = min(IWL_HT_NUMBER_TRY,
2958 LINK_QUAL_AGG_DISABLE_START_DEF - 1);
aade00ce 2959 }
b481de9c
ZY
2960
2961 lq_cmd->general_params.mimo_delimiter =
2962 is_mimo(tbl_type.lq_type) ? 1 : 0;
77626355
BC
2963
2964 /* Fill 1st table entry (index 0) */
39e88504 2965 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
b481de9c 2966
07bc28ed
GC
2967 if (num_of_ant(tbl_type.ant_type) == 1) {
2968 lq_cmd->general_params.single_stream_ant_msk =
2969 tbl_type.ant_type;
2970 } else if (num_of_ant(tbl_type.ant_type) == 2) {
2971 lq_cmd->general_params.dual_stream_ant_msk =
2972 tbl_type.ant_type;
2973 } /* otherwise we don't modify the existing value */
b481de9c
ZY
2974
2975 index++;
1b696de2 2976 repeat_rate--;
bee008b7
WYG
2977 if (priv) {
2978 if (priv->bt_full_concurrent)
2979 valid_tx_ant = ANT_A;
2980 else
9e295116 2981 valid_tx_ant = priv->hw_params.valid_tx_ant;
bee008b7 2982 }
aade00ce 2983
77626355 2984 /* Fill rest of rate table */
b481de9c 2985 while (index < LINK_QUAL_MAX_RETRY_NUM) {
77626355
BC
2986 /* Repeat initial/next rate.
2987 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2988 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
1b696de2 2989 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
b481de9c 2990 if (is_legacy(tbl_type.lq_type)) {
aade00ce
GC
2991 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2992 ant_toggle_cnt++;
2993 else if (priv &&
2994 rs_toggle_antenna(valid_tx_ant,
2995 &new_rate, &tbl_type))
2996 ant_toggle_cnt = 1;
da5dbb97 2997 }
98d7e09a 2998
77626355 2999 /* Override next rate if needed for debug purposes */
c33104f0 3000 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
77626355
BC
3001
3002 /* Fill next table entry */
b481de9c 3003 lq_cmd->rs_table[index].rate_n_flags =
39e88504 3004 cpu_to_le32(new_rate);
1b696de2 3005 repeat_rate--;
b481de9c
ZY
3006 index++;
3007 }
3008
39e88504 3009 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band, &tbl_type,
b481de9c
ZY
3010 &rate_idx);
3011
da5dbb97
WYG
3012 if (priv && priv->bt_full_concurrent) {
3013 /* 1x1 only */
3014 tbl_type.ant_type =
9e295116 3015 first_antenna(priv->hw_params.valid_tx_ant);
da5dbb97
WYG
3016 }
3017
77626355
BC
3018 /* Indicate to uCode which entries might be MIMO.
3019 * If initial rate was MIMO, this will finally end up
3020 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
b481de9c
ZY
3021 if (is_mimo(tbl_type.lq_type))
3022 lq_cmd->general_params.mimo_delimiter = index;
3023
77626355 3024 /* Get next rate */
39e88504
GC
3025 new_rate = rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
3026 use_ht_possible);
b481de9c 3027
77626355 3028 /* How many times should we repeat the next rate? */
b481de9c 3029 if (is_legacy(tbl_type.lq_type)) {
aade00ce
GC
3030 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
3031 ant_toggle_cnt++;
3032 else if (priv &&
3033 rs_toggle_antenna(valid_tx_ant,
3034 &new_rate, &tbl_type))
3035 ant_toggle_cnt = 1;
3036
1b696de2 3037 repeat_rate = IWL_NUMBER_TRY;
aade00ce 3038 } else {
1b696de2 3039 repeat_rate = IWL_HT_NUMBER_TRY;
aade00ce 3040 }
b481de9c 3041
77626355
BC
3042 /* Don't allow HT rates after next pass.
3043 * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
b481de9c
ZY
3044 use_ht_possible = 0;
3045
77626355 3046 /* Override next rate if needed for debug purposes */
c33104f0 3047 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
77626355
BC
3048
3049 /* Fill next table entry */
39e88504 3050 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
b481de9c
ZY
3051
3052 index++;
1b696de2 3053 repeat_rate--;
b481de9c
ZY
3054 }
3055
7b090687
JB
3056 lq_cmd->agg_params.agg_frame_cnt_limit =
3057 sta_priv->max_agg_bufsize ?: LINK_QUAL_AGG_FRAME_LIMIT_DEF;
13c33a09 3058 lq_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
7469701e 3059
13c33a09
WYG
3060 lq_cmd->agg_params.agg_time_limit =
3061 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
7469701e
WYG
3062 /*
3063 * overwrite if needed, pass aggregation time limit
3064 * to uCode in uSec
3065 */
2152268f
EG
3066 if (priv && priv->cfg->bt_params &&
3067 priv->cfg->bt_params->agg_time_limit &&
66e863a5 3068 priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH)
7469701e 3069 lq_cmd->agg_params.agg_time_limit =
2152268f 3070 cpu_to_le16(priv->cfg->bt_params->agg_time_limit);
b481de9c
ZY
3071}
3072
4b7679a5 3073static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
b481de9c 3074{
4b7679a5 3075 return hw->priv;
b481de9c
ZY
3076}
3077/* rate scale requires free function to be implemented */
3078static void rs_free(void *priv_rate)
3079{
3080 return;
3081}
3082
4b7679a5
JB
3083static void rs_free_sta(void *priv_r, struct ieee80211_sta *sta,
3084 void *priv_sta)
b481de9c 3085{
d0f76d68
EG
3086 struct iwl_op_mode *op_mode __maybe_unused = priv_r;
3087 struct iwl_priv *priv __maybe_unused = IWL_OP_MODE_GET_DVM(op_mode);
b481de9c 3088
e1623446 3089 IWL_DEBUG_RATE(priv, "enter\n");
e1623446 3090 IWL_DEBUG_RATE(priv, "leave\n");
b481de9c
ZY
3091}
3092
93dc646a 3093#ifdef CONFIG_MAC80211_DEBUGFS
e227ceac
TW
3094static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
3095 u32 *rate_n_flags, int index)
98d7e09a 3096{
bf403db8 3097 struct iwl_priv *priv;
3c4955f8
WYG
3098 u8 valid_tx_ant;
3099 u8 ant_sel_tx;
bf403db8
EK
3100
3101 priv = lq_sta->drv;
9e295116 3102 valid_tx_ant = priv->hw_params.valid_tx_ant;
39e88504 3103 if (lq_sta->dbg_fixed_rate) {
3c4955f8
WYG
3104 ant_sel_tx =
3105 ((lq_sta->dbg_fixed_rate & RATE_MCS_ANT_ABC_MSK)
3106 >> RATE_MCS_ANT_POS);
3107 if ((valid_tx_ant & ant_sel_tx) == ant_sel_tx) {
39e88504 3108 *rate_n_flags = lq_sta->dbg_fixed_rate;
3c4955f8 3109 IWL_DEBUG_RATE(priv, "Fixed rate ON\n");
39e88504 3110 } else {
3c4955f8
WYG
3111 lq_sta->dbg_fixed_rate = 0;
3112 IWL_ERR(priv,
3113 "Invalid antenna selection 0x%X, Valid is 0x%X\n",
3114 ant_sel_tx, valid_tx_ant);
3115 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
39e88504 3116 }
39e88504 3117 } else {
e1623446 3118 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
98d7e09a 3119 }
98d7e09a 3120}
5ae212c9 3121
98d7e09a
ZY
3122static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
3123 const char __user *user_buf, size_t count, loff_t *ppos)
3124{
e227ceac 3125 struct iwl_lq_sta *lq_sta = file->private_data;
bf403db8 3126 struct iwl_priv *priv;
98d7e09a 3127 char buf[64];
805d7d23 3128 size_t buf_size;
98d7e09a 3129 u32 parsed_rate;
6489854b 3130
98d7e09a 3131
bf403db8 3132 priv = lq_sta->drv;
98d7e09a
ZY
3133 memset(buf, 0, sizeof(buf));
3134 buf_size = min(count, sizeof(buf) - 1);
3135 if (copy_from_user(buf, user_buf, buf_size))
3136 return -EFAULT;
3137
3138 if (sscanf(buf, "%x", &parsed_rate) == 1)
4e308119 3139 lq_sta->dbg_fixed_rate = parsed_rate;
98d7e09a 3140 else
4e308119 3141 lq_sta->dbg_fixed_rate = 0;
98d7e09a 3142
6489854b 3143 rs_program_fix_rate(priv, lq_sta);
98d7e09a
ZY
3144
3145 return count;
3146}
0209dc11 3147
5ae212c9
ZY
3148static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
3149 char __user *user_buf, size_t count, loff_t *ppos)
3150{
a412c804 3151 char *buff;
5ae212c9
ZY
3152 int desc = 0;
3153 int i = 0;
12b96817 3154 int index = 0;
a412c804 3155 ssize_t ret;
5ae212c9 3156
e227ceac 3157 struct iwl_lq_sta *lq_sta = file->private_data;
d8ae4f52 3158 struct iwl_priv *priv;
adb7b5e6 3159 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
5ae212c9 3160
d8ae4f52 3161 priv = lq_sta->drv;
a412c804
FS
3162 buff = kmalloc(1024, GFP_KERNEL);
3163 if (!buff)
3164 return -ENOMEM;
3165
c33104f0 3166 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
98d7e09a 3167 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
c33104f0 3168 lq_sta->total_failed, lq_sta->total_success,
39e88504 3169 lq_sta->active_legacy_rate);
98d7e09a 3170 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
4e308119 3171 lq_sta->dbg_fixed_rate);
d8ae4f52 3172 desc += sprintf(buff+desc, "valid_tx_ant %s%s%s\n",
9e295116
JB
3173 (priv->hw_params.valid_tx_ant & ANT_A) ? "ANT_A," : "",
3174 (priv->hw_params.valid_tx_ant & ANT_B) ? "ANT_B," : "",
3175 (priv->hw_params.valid_tx_ant & ANT_C) ? "ANT_C" : "");
adb7b5e6
WYG
3176 desc += sprintf(buff+desc, "lq type %s\n",
3177 (is_legacy(tbl->lq_type)) ? "legacy" : "HT");
3178 if (is_Ht(tbl->lq_type)) {
3179 desc += sprintf(buff+desc, " %s",
3180 (is_siso(tbl->lq_type)) ? "SISO" :
3181 ((is_mimo2(tbl->lq_type)) ? "MIMO2" : "MIMO3"));
3182 desc += sprintf(buff+desc, " %s",
7aafef1c 3183 (tbl->is_ht40) ? "40MHz" : "20MHz");
e3949d62
DH
3184 desc += sprintf(buff+desc, " %s %s %s\n", (tbl->is_SGI) ? "SGI" : "",
3185 (lq_sta->is_green) ? "GF enabled" : "",
3186 (lq_sta->is_agg) ? "AGG on" : "");
adb7b5e6 3187 }
12b96817
WYG
3188 desc += sprintf(buff+desc, "last tx rate=0x%X\n",
3189 lq_sta->last_rate_n_flags);
5ae212c9
ZY
3190 desc += sprintf(buff+desc, "general:"
3191 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
c33104f0
TW
3192 lq_sta->lq.general_params.flags,
3193 lq_sta->lq.general_params.mimo_delimiter,
3194 lq_sta->lq.general_params.single_stream_ant_msk,
3195 lq_sta->lq.general_params.dual_stream_ant_msk);
5ae212c9
ZY
3196
3197 desc += sprintf(buff+desc, "agg:"
3198 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
c33104f0
TW
3199 le16_to_cpu(lq_sta->lq.agg_params.agg_time_limit),
3200 lq_sta->lq.agg_params.agg_dis_start_th,
3201 lq_sta->lq.agg_params.agg_frame_cnt_limit);
5ae212c9
ZY
3202
3203 desc += sprintf(buff+desc,
3204 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
c33104f0
TW
3205 lq_sta->lq.general_params.start_rate_index[0],
3206 lq_sta->lq.general_params.start_rate_index[1],
3207 lq_sta->lq.general_params.start_rate_index[2],
3208 lq_sta->lq.general_params.start_rate_index[3]);
5ae212c9 3209
12b96817
WYG
3210 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
3211 index = iwl_hwrate_to_plcp_idx(
3212 le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags));
3213 if (is_legacy(tbl->lq_type)) {
3214 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps\n",
3215 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
3216 iwl_rate_mcs[index].mbps);
3217 } else {
3218 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps (%s)\n",
3219 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
3220 iwl_rate_mcs[index].mbps, iwl_rate_mcs[index].mcs);
3221 }
3222 }
5ae212c9 3223
a412c804
FS
3224 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3225 kfree(buff);
3226 return ret;
5ae212c9
ZY
3227}
3228
3229static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
98d7e09a 3230 .write = rs_sta_dbgfs_scale_table_write,
5ae212c9 3231 .read = rs_sta_dbgfs_scale_table_read,
234e3405 3232 .open = simple_open,
6038f373 3233 .llseek = default_llseek,
5ae212c9 3234};
0209dc11
ZY
3235static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
3236 char __user *user_buf, size_t count, loff_t *ppos)
3237{
a412c804 3238 char *buff;
0209dc11
ZY
3239 int desc = 0;
3240 int i, j;
a412c804 3241 ssize_t ret;
0209dc11 3242
e227ceac 3243 struct iwl_lq_sta *lq_sta = file->private_data;
a412c804
FS
3244
3245 buff = kmalloc(1024, GFP_KERNEL);
3246 if (!buff)
3247 return -ENOMEM;
3248
0209dc11 3249 for (i = 0; i < LQ_SIZE; i++) {
7aafef1c
WYG
3250 desc += sprintf(buff+desc,
3251 "%s type=%d SGI=%d HT40=%d DUP=%d GF=%d\n"
0209dc11 3252 "rate=0x%X\n",
c3056065 3253 lq_sta->active_tbl == i ? "*" : "x",
c33104f0
TW
3254 lq_sta->lq_info[i].lq_type,
3255 lq_sta->lq_info[i].is_SGI,
7aafef1c 3256 lq_sta->lq_info[i].is_ht40,
c33104f0 3257 lq_sta->lq_info[i].is_dup,
2681b20b 3258 lq_sta->is_green,
39e88504 3259 lq_sta->lq_info[i].current_rate);
0209dc11
ZY
3260 for (j = 0; j < IWL_RATE_COUNT; j++) {
3261 desc += sprintf(buff+desc,
c33104f0
TW
3262 "counter=%d success=%d %%=%d\n",
3263 lq_sta->lq_info[i].win[j].counter,
3264 lq_sta->lq_info[i].win[j].success_counter,
3265 lq_sta->lq_info[i].win[j].success_ratio);
0209dc11
ZY
3266 }
3267 }
a412c804
FS
3268 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3269 kfree(buff);
3270 return ret;
0209dc11
ZY
3271}
3272
3273static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
3274 .read = rs_sta_dbgfs_stats_table_read,
234e3405 3275 .open = simple_open,
6038f373 3276 .llseek = default_llseek,
0209dc11 3277};
5ae212c9 3278
38167459
WYG
3279static ssize_t rs_sta_dbgfs_rate_scale_data_read(struct file *file,
3280 char __user *user_buf, size_t count, loff_t *ppos)
3281{
38167459 3282 struct iwl_lq_sta *lq_sta = file->private_data;
38167459 3283 struct iwl_scale_tbl_info *tbl = &lq_sta->lq_info[lq_sta->active_tbl];
70817b5e
JB
3284 char buff[120];
3285 int desc = 0;
38167459
WYG
3286
3287 if (is_Ht(tbl->lq_type))
3288 desc += sprintf(buff+desc,
3289 "Bit Rate= %d Mb/s\n",
3290 tbl->expected_tpt[lq_sta->last_txrate_idx]);
3291 else
3292 desc += sprintf(buff+desc,
3293 "Bit Rate= %d Mb/s\n",
3294 iwl_rates[lq_sta->last_txrate_idx].ieee >> 1);
38167459 3295
08960dea 3296 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
38167459
WYG
3297}
3298
3299static const struct file_operations rs_sta_dbgfs_rate_scale_data_ops = {
3300 .read = rs_sta_dbgfs_rate_scale_data_read,
234e3405 3301 .open = simple_open,
6038f373 3302 .llseek = default_llseek,
38167459
WYG
3303};
3304
93dc646a
ZY
3305static void rs_add_debugfs(void *priv, void *priv_sta,
3306 struct dentry *dir)
3307{
e227ceac 3308 struct iwl_lq_sta *lq_sta = priv_sta;
c33104f0 3309 lq_sta->rs_sta_dbgfs_scale_table_file =
43e85115 3310 debugfs_create_file("rate_scale_table", S_IRUSR | S_IWUSR, dir,
c33104f0
TW
3311 lq_sta, &rs_sta_dbgfs_scale_table_ops);
3312 lq_sta->rs_sta_dbgfs_stats_table_file =
43e85115 3313 debugfs_create_file("rate_stats_table", S_IRUSR, dir,
c33104f0 3314 lq_sta, &rs_sta_dbgfs_stats_table_ops);
38167459 3315 lq_sta->rs_sta_dbgfs_rate_scale_data_file =
43e85115 3316 debugfs_create_file("rate_scale_data", S_IRUSR, dir,
38167459 3317 lq_sta, &rs_sta_dbgfs_rate_scale_data_ops);
0c11b4de 3318 lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
43e85115 3319 debugfs_create_u8("tx_agg_tid_enable", S_IRUSR | S_IWUSR, dir,
0c11b4de 3320 &lq_sta->tx_agg_tid_en);
0c11b4de 3321
93dc646a
ZY
3322}
3323
3324static void rs_remove_debugfs(void *priv, void *priv_sta)
3325{
e227ceac 3326 struct iwl_lq_sta *lq_sta = priv_sta;
c33104f0
TW
3327 debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
3328 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
38167459 3329 debugfs_remove(lq_sta->rs_sta_dbgfs_rate_scale_data_file);
0c11b4de 3330 debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
93dc646a
ZY
3331}
3332#endif
3333
fe6b23dd
RC
3334/*
3335 * Initialization of rate scaling information is done by driver after
3336 * the station is added. Since mac80211 calls this function before a
3337 * station is added we ignore it.
3338 */
3339static void rs_rate_init_stub(void *priv_r, struct ieee80211_supported_band *sband,
3340 struct ieee80211_sta *sta, void *priv_sta)
3341{
3342}
b481de9c
ZY
3343static struct rate_control_ops rs_ops = {
3344 .module = NULL,
3345 .name = RS_NAME,
3346 .tx_status = rs_tx_status,
3347 .get_rate = rs_get_rate,
fe6b23dd 3348 .rate_init = rs_rate_init_stub,
b481de9c
ZY
3349 .alloc = rs_alloc,
3350 .free = rs_free,
3351 .alloc_sta = rs_alloc_sta,
3352 .free_sta = rs_free_sta,
93dc646a
ZY
3353#ifdef CONFIG_MAC80211_DEBUGFS
3354 .add_sta_debugfs = rs_add_debugfs,
3355 .remove_sta_debugfs = rs_remove_debugfs,
3356#endif
b481de9c
ZY
3357};
3358
e227ceac 3359int iwlagn_rate_control_register(void)
b481de9c 3360{
897e1cf2 3361 return ieee80211_rate_control_register(&rs_ops);
b481de9c
ZY
3362}
3363
e227ceac 3364void iwlagn_rate_control_unregister(void)
b481de9c
ZY
3365{
3366 ieee80211_rate_control_unregister(&rs_ops);
3367}
3368