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