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