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