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