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