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