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