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