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