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