initial commit with v2.6.32.60
[linux-2.6.32.60-moxart.git] / drivers / net / wireless / iwlwifi / iwl-agn-rs.c
blob1a3dfa2b1ef020f684dbd80c3c4529bf1116f009
1 /******************************************************************************
3 * Copyright(c) 2005 - 2009 Intel Corporation. All rights reserved.
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.
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.
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
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
21 * Contact Information:
22 * Intel Linux Wireless <ilw@linux.intel.com>
23 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
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>
32 #include <linux/netdevice.h>
33 #include <linux/etherdevice.h>
34 #include <linux/delay.h>
36 #include <linux/workqueue.h>
38 #include "iwl-dev.h"
39 #include "iwl-sta.h"
40 #include "iwl-core.h"
42 #define RS_NAME "iwl-agn-rs"
44 #define NUM_TRY_BEFORE_ANT_TOGGLE 1
45 #define IWL_NUMBER_TRY 1
46 #define IWL_HT_NUMBER_TRY 3
48 #define IWL_RATE_MAX_WINDOW 62 /* # tx in history window */
49 #define IWL_RATE_MIN_FAILURE_TH 6 /* min failures to calc tpt */
50 #define IWL_RATE_MIN_SUCCESS_TH 8 /* min successes to calc tpt */
52 /* max allowed rate miss before sync LQ cmd */
53 #define IWL_MISSED_RATE_MAX 15
54 /* max time to accum history 2 seconds */
55 #define IWL_RATE_SCALE_FLUSH_INTVL (3*HZ)
57 static u8 rs_ht_to_legacy[] = {
58 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
59 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
60 IWL_RATE_6M_INDEX,
61 IWL_RATE_6M_INDEX, IWL_RATE_9M_INDEX,
62 IWL_RATE_12M_INDEX, IWL_RATE_18M_INDEX,
63 IWL_RATE_24M_INDEX, IWL_RATE_36M_INDEX,
64 IWL_RATE_48M_INDEX, IWL_RATE_54M_INDEX
67 static const u8 ant_toggle_lookup[] = {
68 /*ANT_NONE -> */ ANT_NONE,
69 /*ANT_A -> */ ANT_B,
70 /*ANT_B -> */ ANT_C,
71 /*ANT_AB -> */ ANT_BC,
72 /*ANT_C -> */ ANT_A,
73 /*ANT_AC -> */ ANT_AB,
74 /*ANT_BC -> */ ANT_AC,
75 /*ANT_ABC -> */ ANT_ABC,
78 /**
79 * struct iwl_rate_scale_data -- tx success history for one rate
81 struct iwl_rate_scale_data {
82 u64 data; /* bitmap of successful frames */
83 s32 success_counter; /* number of frames successful */
84 s32 success_ratio; /* per-cent * 128 */
85 s32 counter; /* number of frames attempted */
86 s32 average_tpt; /* success ratio * expected throughput */
87 unsigned long stamp;
90 /**
91 * struct iwl_scale_tbl_info -- tx params and success history for all rates
93 * There are two of these in struct iwl_lq_sta,
94 * one for "active", and one for "search".
96 struct iwl_scale_tbl_info {
97 enum iwl_table_type lq_type;
98 u8 ant_type;
99 u8 is_SGI; /* 1 = short guard interval */
100 u8 is_ht40; /* 1 = 40 MHz channel width */
101 u8 is_dup; /* 1 = duplicated data streams */
102 u8 action; /* change modulation; IWL_[LEGACY/SISO/MIMO]_SWITCH_* */
103 u8 max_search; /* maximun number of tables we can search */
104 s32 *expected_tpt; /* throughput metrics; expected_tpt_G, etc. */
105 u32 current_rate; /* rate_n_flags, uCode API format */
106 struct iwl_rate_scale_data win[IWL_RATE_COUNT]; /* rate histories */
109 struct iwl_traffic_load {
110 unsigned long time_stamp; /* age of the oldest statistics */
111 u32 packet_count[TID_QUEUE_MAX_SIZE]; /* packet count in this time
112 * slice */
113 u32 total; /* total num of packets during the
114 * last TID_MAX_TIME_DIFF */
115 u8 queue_count; /* number of queues that has
116 * been used since the last cleanup */
117 u8 head; /* start of the circular buffer */
121 * struct iwl_lq_sta -- driver's rate scaling private structure
123 * Pointer to this gets passed back and forth between driver and mac80211.
125 struct iwl_lq_sta {
126 u8 active_tbl; /* index of active table, range 0-1 */
127 u8 enable_counter; /* indicates HT mode */
128 u8 stay_in_tbl; /* 1: disallow, 0: allow search for new mode */
129 u8 search_better_tbl; /* 1: currently trying alternate mode */
130 s32 last_tpt;
132 /* The following determine when to search for a new mode */
133 u32 table_count_limit;
134 u32 max_failure_limit; /* # failed frames before new search */
135 u32 max_success_limit; /* # successful frames before new search */
136 u32 table_count;
137 u32 total_failed; /* total failed frames, any/all rates */
138 u32 total_success; /* total successful frames, any/all rates */
139 u64 flush_timer; /* time staying in mode before new search */
141 u8 action_counter; /* # mode-switch actions tried */
142 u8 is_green;
143 u8 is_dup;
144 enum ieee80211_band band;
145 u8 ibss_sta_added;
147 /* The following are bitmaps of rates; IWL_RATE_6M_MASK, etc. */
148 u32 supp_rates;
149 u16 active_legacy_rate;
150 u16 active_siso_rate;
151 u16 active_mimo2_rate;
152 u16 active_mimo3_rate;
153 u16 active_rate_basic;
154 s8 max_rate_idx; /* Max rate set by user */
155 u8 missed_rate_counter;
157 struct iwl_link_quality_cmd lq;
158 struct iwl_scale_tbl_info lq_info[LQ_SIZE]; /* "active", "search" */
159 struct iwl_traffic_load load[TID_MAX_LOAD_COUNT];
160 u8 tx_agg_tid_en;
161 #ifdef CONFIG_MAC80211_DEBUGFS
162 struct dentry *rs_sta_dbgfs_scale_table_file;
163 struct dentry *rs_sta_dbgfs_stats_table_file;
164 struct dentry *rs_sta_dbgfs_rate_scale_data_file;
165 struct dentry *rs_sta_dbgfs_tx_agg_tid_en_file;
166 u32 dbg_fixed_rate;
167 #endif
168 struct iwl_priv *drv;
170 /* used to be in sta_info */
171 int last_txrate_idx;
172 /* last tx rate_n_flags */
173 u32 last_rate_n_flags;
176 static void rs_rate_scale_perform(struct iwl_priv *priv,
177 struct sk_buff *skb,
178 struct ieee80211_sta *sta,
179 struct iwl_lq_sta *lq_sta);
180 static void rs_fill_link_cmd(struct iwl_priv *priv,
181 struct iwl_lq_sta *lq_sta, u32 rate_n_flags);
184 #ifdef CONFIG_MAC80211_DEBUGFS
185 static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
186 u32 *rate_n_flags, int index);
187 #else
188 static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
189 u32 *rate_n_flags, int index)
191 #endif
194 * Expected throughput metrics for following rates:
195 * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
196 * "G" is the only table that supports CCK (the first 4 rates).
199 static s32 expected_tpt_A[IWL_RATE_COUNT] = {
200 0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186, 186
203 static s32 expected_tpt_G[IWL_RATE_COUNT] = {
204 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 186
207 static s32 expected_tpt_siso20MHz[IWL_RATE_COUNT] = {
208 0, 0, 0, 0, 42, 42, 76, 102, 124, 159, 183, 193, 202
211 static s32 expected_tpt_siso20MHzSGI[IWL_RATE_COUNT] = {
212 0, 0, 0, 0, 46, 46, 82, 110, 132, 168, 192, 202, 211
215 static s32 expected_tpt_mimo2_20MHz[IWL_RATE_COUNT] = {
216 0, 0, 0, 0, 74, 74, 123, 155, 179, 214, 236, 244, 251
219 static s32 expected_tpt_mimo2_20MHzSGI[IWL_RATE_COUNT] = {
220 0, 0, 0, 0, 81, 81, 131, 164, 188, 222, 243, 251, 257
223 static s32 expected_tpt_siso40MHz[IWL_RATE_COUNT] = {
224 0, 0, 0, 0, 77, 77, 127, 160, 184, 220, 242, 250, 257
227 static s32 expected_tpt_siso40MHzSGI[IWL_RATE_COUNT] = {
228 0, 0, 0, 0, 83, 83, 135, 169, 193, 229, 250, 257, 264
231 static s32 expected_tpt_mimo2_40MHz[IWL_RATE_COUNT] = {
232 0, 0, 0, 0, 123, 123, 182, 214, 235, 264, 279, 285, 289
235 static s32 expected_tpt_mimo2_40MHzSGI[IWL_RATE_COUNT] = {
236 0, 0, 0, 0, 131, 131, 191, 222, 242, 270, 284, 289, 293
239 /* Expected throughput metric MIMO3 */
240 static s32 expected_tpt_mimo3_20MHz[IWL_RATE_COUNT] = {
241 0, 0, 0, 0, 99, 99, 153, 186, 208, 239, 256, 263, 268
244 static s32 expected_tpt_mimo3_20MHzSGI[IWL_RATE_COUNT] = {
245 0, 0, 0, 0, 106, 106, 162, 194, 215, 246, 262, 268, 273
248 static s32 expected_tpt_mimo3_40MHz[IWL_RATE_COUNT] = {
249 0, 0, 0, 0, 152, 152, 211, 239, 255, 279, 290, 294, 297
252 static s32 expected_tpt_mimo3_40MHzSGI[IWL_RATE_COUNT] = {
253 0, 0, 0, 0, 160, 160, 219, 245, 261, 284, 294, 297, 300
256 /* mbps, mcs */
257 const static struct iwl_rate_mcs_info iwl_rate_mcs[IWL_RATE_COUNT] = {
258 {"1", ""},
259 {"2", ""},
260 {"5.5", ""},
261 {"11", ""},
262 {"6", "BPSK 1/2"},
263 {"9", "BPSK 1/2"},
264 {"12", "QPSK 1/2"},
265 {"18", "QPSK 3/4"},
266 {"24", "16QAM 1/2"},
267 {"36", "16QAM 3/4"},
268 {"48", "64QAM 2/3"},
269 {"54", "64QAM 3/4"},
270 {"60", "64QAM 5/6"}
273 #define MCS_INDEX_PER_STREAM (8)
275 static inline u8 rs_extract_rate(u32 rate_n_flags)
277 return (u8)(rate_n_flags & 0xFF);
280 static void rs_rate_scale_clear_window(struct iwl_rate_scale_data *window)
282 window->data = 0;
283 window->success_counter = 0;
284 window->success_ratio = IWL_INVALID_VALUE;
285 window->counter = 0;
286 window->average_tpt = IWL_INVALID_VALUE;
287 window->stamp = 0;
290 static inline u8 rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
292 return (ant_type & valid_antenna) == ant_type;
296 * removes the old data from the statistics. All data that is older than
297 * TID_MAX_TIME_DIFF, will be deleted.
299 static void rs_tl_rm_old_stats(struct iwl_traffic_load *tl, u32 curr_time)
301 /* The oldest age we want to keep */
302 u32 oldest_time = curr_time - TID_MAX_TIME_DIFF;
304 while (tl->queue_count &&
305 (tl->time_stamp < oldest_time)) {
306 tl->total -= tl->packet_count[tl->head];
307 tl->packet_count[tl->head] = 0;
308 tl->time_stamp += TID_QUEUE_CELL_SPACING;
309 tl->queue_count--;
310 tl->head++;
311 if (tl->head >= TID_QUEUE_MAX_SIZE)
312 tl->head = 0;
317 * increment traffic load value for tid and also remove
318 * any old values if passed the certain time period
320 static u8 rs_tl_add_packet(struct iwl_lq_sta *lq_data,
321 struct ieee80211_hdr *hdr)
323 u32 curr_time = jiffies_to_msecs(jiffies);
324 u32 time_diff;
325 s32 index;
326 struct iwl_traffic_load *tl = NULL;
327 u8 tid;
329 if (ieee80211_is_data_qos(hdr->frame_control)) {
330 u8 *qc = ieee80211_get_qos_ctl(hdr);
331 tid = qc[0] & 0xf;
332 } else
333 return MAX_TID_COUNT;
335 if (unlikely(tid >= TID_MAX_LOAD_COUNT))
336 return MAX_TID_COUNT;
338 tl = &lq_data->load[tid];
340 curr_time -= curr_time % TID_ROUND_VALUE;
342 /* Happens only for the first packet. Initialize the data */
343 if (!(tl->queue_count)) {
344 tl->total = 1;
345 tl->time_stamp = curr_time;
346 tl->queue_count = 1;
347 tl->head = 0;
348 tl->packet_count[0] = 1;
349 return MAX_TID_COUNT;
352 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
353 index = time_diff / TID_QUEUE_CELL_SPACING;
355 /* The history is too long: remove data that is older than */
356 /* TID_MAX_TIME_DIFF */
357 if (index >= TID_QUEUE_MAX_SIZE)
358 rs_tl_rm_old_stats(tl, curr_time);
360 index = (tl->head + index) % TID_QUEUE_MAX_SIZE;
361 tl->packet_count[index] = tl->packet_count[index] + 1;
362 tl->total = tl->total + 1;
364 if ((index + 1) > tl->queue_count)
365 tl->queue_count = index + 1;
367 return tid;
371 get the traffic load value for tid
373 static u32 rs_tl_get_load(struct iwl_lq_sta *lq_data, u8 tid)
375 u32 curr_time = jiffies_to_msecs(jiffies);
376 u32 time_diff;
377 s32 index;
378 struct iwl_traffic_load *tl = NULL;
380 if (tid >= TID_MAX_LOAD_COUNT)
381 return 0;
383 tl = &(lq_data->load[tid]);
385 curr_time -= curr_time % TID_ROUND_VALUE;
387 if (!(tl->queue_count))
388 return 0;
390 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
391 index = time_diff / TID_QUEUE_CELL_SPACING;
393 /* The history is too long: remove data that is older than */
394 /* TID_MAX_TIME_DIFF */
395 if (index >= TID_QUEUE_MAX_SIZE)
396 rs_tl_rm_old_stats(tl, curr_time);
398 return tl->total;
401 static void rs_tl_turn_on_agg_for_tid(struct iwl_priv *priv,
402 struct iwl_lq_sta *lq_data, u8 tid,
403 struct ieee80211_sta *sta)
405 int ret;
407 if (rs_tl_get_load(lq_data, tid) > IWL_AGG_LOAD_THRESHOLD) {
408 IWL_DEBUG_HT(priv, "Starting Tx agg: STA: %pM tid: %d\n",
409 sta->addr, tid);
410 ret = ieee80211_start_tx_ba_session(priv->hw, sta->addr, tid);
411 if (ret == -EAGAIN) {
413 * driver and mac80211 is out of sync
414 * this might be cause by reloading firmware
415 * stop the tx ba session here
417 IWL_DEBUG_HT(priv, "Fail start Tx agg on tid: %d\n",
418 tid);
419 ret = ieee80211_stop_tx_ba_session(priv->hw, sta->addr, tid,
420 WLAN_BACK_INITIATOR);
425 static void rs_tl_turn_on_agg(struct iwl_priv *priv, u8 tid,
426 struct iwl_lq_sta *lq_data,
427 struct ieee80211_sta *sta)
429 if ((tid < TID_MAX_LOAD_COUNT))
430 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
431 else if (tid == IWL_AGG_ALL_TID)
432 for (tid = 0; tid < TID_MAX_LOAD_COUNT; tid++)
433 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
434 if (priv->cfg->use_rts_for_ht) {
436 * switch to RTS/CTS if it is the prefer protection method
437 * for HT traffic
439 IWL_DEBUG_HT(priv, "use RTS/CTS protection for HT\n");
440 priv->staging_rxon.flags &= ~RXON_FLG_SELF_CTS_EN;
441 iwlcore_commit_rxon(priv);
445 static inline int get_num_of_ant_from_rate(u32 rate_n_flags)
447 return !!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
448 !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
449 !!(rate_n_flags & RATE_MCS_ANT_C_MSK);
453 * rs_collect_tx_data - Update the success/failure sliding window
455 * We keep a sliding window of the last 62 packets transmitted
456 * at this rate. window->data contains the bitmask of successful
457 * packets.
459 static int rs_collect_tx_data(struct iwl_rate_scale_data *windows,
460 int scale_index, s32 tpt, int retries,
461 int successes)
463 struct iwl_rate_scale_data *window = NULL;
464 static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
465 s32 fail_count;
467 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
468 return -EINVAL;
470 /* Select data for current tx bit rate */
471 window = &(windows[scale_index]);
474 * Keep track of only the latest 62 tx frame attempts in this rate's
475 * history window; anything older isn't really relevant any more.
476 * If we have filled up the sliding window, drop the oldest attempt;
477 * if the oldest attempt (highest bit in bitmap) shows "success",
478 * subtract "1" from the success counter (this is the main reason
479 * we keep these bitmaps!).
481 while (retries > 0) {
482 if (window->counter >= IWL_RATE_MAX_WINDOW) {
484 /* remove earliest */
485 window->counter = IWL_RATE_MAX_WINDOW - 1;
487 if (window->data & mask) {
488 window->data &= ~mask;
489 window->success_counter--;
493 /* Increment frames-attempted counter */
494 window->counter++;
496 /* Shift bitmap by one frame (throw away oldest history),
497 * OR in "1", and increment "success" if this
498 * frame was successful. */
499 window->data <<= 1;
500 if (successes > 0) {
501 window->success_counter++;
502 window->data |= 0x1;
503 successes--;
506 retries--;
509 /* Calculate current success ratio, avoid divide-by-0! */
510 if (window->counter > 0)
511 window->success_ratio = 128 * (100 * window->success_counter)
512 / window->counter;
513 else
514 window->success_ratio = IWL_INVALID_VALUE;
516 fail_count = window->counter - window->success_counter;
518 /* Calculate average throughput, if we have enough history. */
519 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
520 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
521 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
522 else
523 window->average_tpt = IWL_INVALID_VALUE;
525 /* Tag this window as having been updated */
526 window->stamp = jiffies;
528 return 0;
532 * Fill uCode API rate_n_flags field, based on "search" or "active" table.
534 /* FIXME:RS:remove this function and put the flags statically in the table */
535 static u32 rate_n_flags_from_tbl(struct iwl_priv *priv,
536 struct iwl_scale_tbl_info *tbl,
537 int index, u8 use_green)
539 u32 rate_n_flags = 0;
541 if (is_legacy(tbl->lq_type)) {
542 rate_n_flags = iwl_rates[index].plcp;
543 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
544 rate_n_flags |= RATE_MCS_CCK_MSK;
546 } else if (is_Ht(tbl->lq_type)) {
547 if (index > IWL_LAST_OFDM_RATE) {
548 IWL_ERR(priv, "Invalid HT rate index %d\n", index);
549 index = IWL_LAST_OFDM_RATE;
551 rate_n_flags = RATE_MCS_HT_MSK;
553 if (is_siso(tbl->lq_type))
554 rate_n_flags |= iwl_rates[index].plcp_siso;
555 else if (is_mimo2(tbl->lq_type))
556 rate_n_flags |= iwl_rates[index].plcp_mimo2;
557 else
558 rate_n_flags |= iwl_rates[index].plcp_mimo3;
559 } else {
560 IWL_ERR(priv, "Invalid tbl->lq_type %d\n", tbl->lq_type);
563 rate_n_flags |= ((tbl->ant_type << RATE_MCS_ANT_POS) &
564 RATE_MCS_ANT_ABC_MSK);
566 if (is_Ht(tbl->lq_type)) {
567 if (tbl->is_ht40) {
568 if (tbl->is_dup)
569 rate_n_flags |= RATE_MCS_DUP_MSK;
570 else
571 rate_n_flags |= RATE_MCS_HT40_MSK;
573 if (tbl->is_SGI)
574 rate_n_flags |= RATE_MCS_SGI_MSK;
576 if (use_green) {
577 rate_n_flags |= RATE_MCS_GF_MSK;
578 if (is_siso(tbl->lq_type) && tbl->is_SGI) {
579 rate_n_flags &= ~RATE_MCS_SGI_MSK;
580 IWL_ERR(priv, "GF was set with SGI:SISO\n");
584 return rate_n_flags;
588 * Interpret uCode API's rate_n_flags format,
589 * fill "search" or "active" tx mode table.
591 static int rs_get_tbl_info_from_mcs(const u32 rate_n_flags,
592 enum ieee80211_band band,
593 struct iwl_scale_tbl_info *tbl,
594 int *rate_idx)
596 u32 ant_msk = (rate_n_flags & RATE_MCS_ANT_ABC_MSK);
597 u8 num_of_ant = get_num_of_ant_from_rate(rate_n_flags);
598 u8 mcs;
600 *rate_idx = iwl_hwrate_to_plcp_idx(rate_n_flags);
602 if (*rate_idx == IWL_RATE_INVALID) {
603 *rate_idx = -1;
604 return -EINVAL;
606 tbl->is_SGI = 0; /* default legacy setup */
607 tbl->is_ht40 = 0;
608 tbl->is_dup = 0;
609 tbl->ant_type = (ant_msk >> RATE_MCS_ANT_POS);
610 tbl->lq_type = LQ_NONE;
611 tbl->max_search = IWL_MAX_SEARCH;
613 /* legacy rate format */
614 if (!(rate_n_flags & RATE_MCS_HT_MSK)) {
615 if (num_of_ant == 1) {
616 if (band == IEEE80211_BAND_5GHZ)
617 tbl->lq_type = LQ_A;
618 else
619 tbl->lq_type = LQ_G;
621 /* HT rate format */
622 } else {
623 if (rate_n_flags & RATE_MCS_SGI_MSK)
624 tbl->is_SGI = 1;
626 if ((rate_n_flags & RATE_MCS_HT40_MSK) ||
627 (rate_n_flags & RATE_MCS_DUP_MSK))
628 tbl->is_ht40 = 1;
630 if (rate_n_flags & RATE_MCS_DUP_MSK)
631 tbl->is_dup = 1;
633 mcs = rs_extract_rate(rate_n_flags);
635 /* SISO */
636 if (mcs <= IWL_RATE_SISO_60M_PLCP) {
637 if (num_of_ant == 1)
638 tbl->lq_type = LQ_SISO; /*else NONE*/
639 /* MIMO2 */
640 } else if (mcs <= IWL_RATE_MIMO2_60M_PLCP) {
641 if (num_of_ant == 2)
642 tbl->lq_type = LQ_MIMO2;
643 /* MIMO3 */
644 } else {
645 if (num_of_ant == 3) {
646 tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
647 tbl->lq_type = LQ_MIMO3;
651 return 0;
654 /* switch to another antenna/antennas and return 1 */
655 /* if no other valid antenna found, return 0 */
656 static int rs_toggle_antenna(u32 valid_ant, u32 *rate_n_flags,
657 struct iwl_scale_tbl_info *tbl)
659 u8 new_ant_type;
661 if (!tbl->ant_type || tbl->ant_type > ANT_ABC)
662 return 0;
664 if (!rs_is_valid_ant(valid_ant, tbl->ant_type))
665 return 0;
667 new_ant_type = ant_toggle_lookup[tbl->ant_type];
669 while ((new_ant_type != tbl->ant_type) &&
670 !rs_is_valid_ant(valid_ant, new_ant_type))
671 new_ant_type = ant_toggle_lookup[new_ant_type];
673 if (new_ant_type == tbl->ant_type)
674 return 0;
676 tbl->ant_type = new_ant_type;
677 *rate_n_flags &= ~RATE_MCS_ANT_ABC_MSK;
678 *rate_n_flags |= new_ant_type << RATE_MCS_ANT_POS;
679 return 1;
683 * Green-field mode is valid if the station supports it and
684 * there are no non-GF stations present in the BSS.
686 static inline u8 rs_use_green(struct ieee80211_sta *sta,
687 struct iwl_ht_info *ht_conf)
689 return (sta->ht_cap.cap & IEEE80211_HT_CAP_GRN_FLD) &&
690 !(ht_conf->non_GF_STA_present);
694 * rs_get_supported_rates - get the available rates
696 * if management frame or broadcast frame only return
697 * basic available rates.
700 static u16 rs_get_supported_rates(struct iwl_lq_sta *lq_sta,
701 struct ieee80211_hdr *hdr,
702 enum iwl_table_type rate_type)
704 if (hdr && is_multicast_ether_addr(hdr->addr1) &&
705 lq_sta->active_rate_basic)
706 return lq_sta->active_rate_basic;
708 if (is_legacy(rate_type)) {
709 return lq_sta->active_legacy_rate;
710 } else {
711 if (is_siso(rate_type))
712 return lq_sta->active_siso_rate;
713 else if (is_mimo2(rate_type))
714 return lq_sta->active_mimo2_rate;
715 else
716 return lq_sta->active_mimo3_rate;
720 static u16 rs_get_adjacent_rate(struct iwl_priv *priv, u8 index, u16 rate_mask,
721 int rate_type)
723 u8 high = IWL_RATE_INVALID;
724 u8 low = IWL_RATE_INVALID;
726 /* 802.11A or ht walks to the next literal adjacent rate in
727 * the rate table */
728 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
729 int i;
730 u32 mask;
732 /* Find the previous rate that is in the rate mask */
733 i = index - 1;
734 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
735 if (rate_mask & mask) {
736 low = i;
737 break;
741 /* Find the next rate that is in the rate mask */
742 i = index + 1;
743 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
744 if (rate_mask & mask) {
745 high = i;
746 break;
750 return (high << 8) | low;
753 low = index;
754 while (low != IWL_RATE_INVALID) {
755 low = iwl_rates[low].prev_rs;
756 if (low == IWL_RATE_INVALID)
757 break;
758 if (rate_mask & (1 << low))
759 break;
760 IWL_DEBUG_RATE(priv, "Skipping masked lower rate: %d\n", low);
763 high = index;
764 while (high != IWL_RATE_INVALID) {
765 high = iwl_rates[high].next_rs;
766 if (high == IWL_RATE_INVALID)
767 break;
768 if (rate_mask & (1 << high))
769 break;
770 IWL_DEBUG_RATE(priv, "Skipping masked higher rate: %d\n", high);
773 return (high << 8) | low;
776 static u32 rs_get_lower_rate(struct iwl_lq_sta *lq_sta,
777 struct iwl_scale_tbl_info *tbl,
778 u8 scale_index, u8 ht_possible)
780 s32 low;
781 u16 rate_mask;
782 u16 high_low;
783 u8 switch_to_legacy = 0;
784 u8 is_green = lq_sta->is_green;
785 struct iwl_priv *priv = lq_sta->drv;
787 /* check if we need to switch from HT to legacy rates.
788 * assumption is that mandatory rates (1Mbps or 6Mbps)
789 * are always supported (spec demand) */
790 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
791 switch_to_legacy = 1;
792 scale_index = rs_ht_to_legacy[scale_index];
793 if (lq_sta->band == IEEE80211_BAND_5GHZ)
794 tbl->lq_type = LQ_A;
795 else
796 tbl->lq_type = LQ_G;
798 if (num_of_ant(tbl->ant_type) > 1)
799 tbl->ant_type =
800 first_antenna(priv->hw_params.valid_tx_ant);
802 tbl->is_ht40 = 0;
803 tbl->is_SGI = 0;
804 tbl->max_search = IWL_MAX_SEARCH;
807 rate_mask = rs_get_supported_rates(lq_sta, NULL, tbl->lq_type);
809 /* Mask with station rate restriction */
810 if (is_legacy(tbl->lq_type)) {
811 /* supp_rates has no CCK bits in A mode */
812 if (lq_sta->band == IEEE80211_BAND_5GHZ)
813 rate_mask = (u16)(rate_mask &
814 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
815 else
816 rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
819 /* If we switched from HT to legacy, check current rate */
820 if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
821 low = scale_index;
822 goto out;
825 high_low = rs_get_adjacent_rate(lq_sta->drv, scale_index, rate_mask,
826 tbl->lq_type);
827 low = high_low & 0xff;
829 if (low == IWL_RATE_INVALID)
830 low = scale_index;
832 out:
833 return rate_n_flags_from_tbl(lq_sta->drv, tbl, low, is_green);
837 * mac80211 sends us Tx status
839 static void rs_tx_status(void *priv_r, struct ieee80211_supported_band *sband,
840 struct ieee80211_sta *sta, void *priv_sta,
841 struct sk_buff *skb)
843 int status;
844 u8 retries;
845 int rs_index, mac_index, index = 0;
846 struct iwl_lq_sta *lq_sta = priv_sta;
847 struct iwl_link_quality_cmd *table;
848 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
849 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
850 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
851 struct iwl_rate_scale_data *window = NULL;
852 struct iwl_rate_scale_data *search_win = NULL;
853 enum mac80211_rate_control_flags mac_flags;
854 u32 tx_rate;
855 struct iwl_scale_tbl_info tbl_type;
856 struct iwl_scale_tbl_info *curr_tbl, *search_tbl;
857 u8 active_index = 0;
858 s32 tpt = 0;
860 IWL_DEBUG_RATE_LIMIT(priv, "get frame ack response, update rate scale window\n");
862 if (!ieee80211_is_data(hdr->frame_control) ||
863 info->flags & IEEE80211_TX_CTL_NO_ACK)
864 return;
866 /* This packet was aggregated but doesn't carry rate scale info */
867 if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
868 !(info->flags & IEEE80211_TX_STAT_AMPDU))
869 return;
871 if (info->flags & IEEE80211_TX_STAT_AMPDU)
872 retries = 0;
873 else
874 retries = info->status.rates[0].count - 1;
876 if (retries > 15)
877 retries = 15;
879 if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) &&
880 !lq_sta->ibss_sta_added)
881 goto out;
883 table = &lq_sta->lq;
884 active_index = lq_sta->active_tbl;
886 curr_tbl = &(lq_sta->lq_info[active_index]);
887 search_tbl = &(lq_sta->lq_info[(1 - active_index)]);
888 window = (struct iwl_rate_scale_data *)&(curr_tbl->win[0]);
889 search_win = (struct iwl_rate_scale_data *)&(search_tbl->win[0]);
892 * Ignore this Tx frame response if its initial rate doesn't match
893 * that of latest Link Quality command. There may be stragglers
894 * from a previous Link Quality command, but we're no longer interested
895 * in those; they're either from the "active" mode while we're trying
896 * to check "search" mode, or a prior "search" mode after we've moved
897 * to a new "search" mode (which might become the new "active" mode).
899 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
900 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
901 if (priv->band == IEEE80211_BAND_5GHZ)
902 rs_index -= IWL_FIRST_OFDM_RATE;
903 mac_flags = info->status.rates[0].flags;
904 mac_index = info->status.rates[0].idx;
905 /* For HT packets, map MCS to PLCP */
906 if (mac_flags & IEEE80211_TX_RC_MCS) {
907 mac_index &= RATE_MCS_CODE_MSK; /* Remove # of streams */
908 if (mac_index >= (IWL_RATE_9M_INDEX - IWL_FIRST_OFDM_RATE))
909 mac_index++;
911 * mac80211 HT index is always zero-indexed; we need to move
912 * HT OFDM rates after CCK rates in 2.4 GHz band
914 if (priv->band == IEEE80211_BAND_2GHZ)
915 mac_index += IWL_FIRST_OFDM_RATE;
918 if ((mac_index < 0) ||
919 (tbl_type.is_SGI != !!(mac_flags & IEEE80211_TX_RC_SHORT_GI)) ||
920 (tbl_type.is_ht40 != !!(mac_flags & IEEE80211_TX_RC_40_MHZ_WIDTH)) ||
921 (tbl_type.is_dup != !!(mac_flags & IEEE80211_TX_RC_DUP_DATA)) ||
922 (tbl_type.ant_type != info->antenna_sel_tx) ||
923 (!!(tx_rate & RATE_MCS_HT_MSK) != !!(mac_flags & IEEE80211_TX_RC_MCS)) ||
924 (!!(tx_rate & RATE_MCS_GF_MSK) != !!(mac_flags & IEEE80211_TX_RC_GREEN_FIELD)) ||
925 (rs_index != mac_index)) {
926 IWL_DEBUG_RATE(priv, "initial rate %d does not match %d (0x%x)\n", mac_index, rs_index, tx_rate);
927 /* the last LQ command could failed so the LQ in ucode not
928 * the same in driver sync up
930 lq_sta->missed_rate_counter++;
931 if (lq_sta->missed_rate_counter > IWL_MISSED_RATE_MAX) {
932 lq_sta->missed_rate_counter = 0;
933 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
935 goto out;
938 lq_sta->missed_rate_counter = 0;
939 /* Update frame history window with "failure" for each Tx retry. */
940 while (retries) {
941 /* Look up the rate and other info used for each tx attempt.
942 * Each tx attempt steps one entry deeper in the rate table. */
943 tx_rate = le32_to_cpu(table->rs_table[index].rate_n_flags);
944 rs_get_tbl_info_from_mcs(tx_rate, priv->band,
945 &tbl_type, &rs_index);
947 /* If type matches "search" table,
948 * add failure to "search" history */
949 if ((tbl_type.lq_type == search_tbl->lq_type) &&
950 (tbl_type.ant_type == search_tbl->ant_type) &&
951 (tbl_type.is_SGI == search_tbl->is_SGI)) {
952 if (search_tbl->expected_tpt)
953 tpt = search_tbl->expected_tpt[rs_index];
954 else
955 tpt = 0;
956 rs_collect_tx_data(search_win, rs_index, tpt, 1, 0);
958 /* Else if type matches "current/active" table,
959 * add failure to "current/active" history */
960 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
961 (tbl_type.ant_type == curr_tbl->ant_type) &&
962 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
963 if (curr_tbl->expected_tpt)
964 tpt = curr_tbl->expected_tpt[rs_index];
965 else
966 tpt = 0;
967 rs_collect_tx_data(window, rs_index, tpt, 1, 0);
970 /* If not searching for a new mode, increment failed counter
971 * ... this helps determine when to start searching again */
972 if (lq_sta->stay_in_tbl)
973 lq_sta->total_failed++;
974 --retries;
975 index++;
980 * Find (by rate) the history window to update with final Tx attempt;
981 * if Tx was successful first try, use original rate,
982 * else look up the rate that was, finally, successful.
984 tx_rate = le32_to_cpu(table->rs_table[index].rate_n_flags);
985 lq_sta->last_rate_n_flags = tx_rate;
986 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
988 /* Update frame history window with "success" if Tx got ACKed ... */
989 status = !!(info->flags & IEEE80211_TX_STAT_ACK);
991 /* If type matches "search" table,
992 * add final tx status to "search" history */
993 if ((tbl_type.lq_type == search_tbl->lq_type) &&
994 (tbl_type.ant_type == search_tbl->ant_type) &&
995 (tbl_type.is_SGI == search_tbl->is_SGI)) {
996 if (search_tbl->expected_tpt)
997 tpt = search_tbl->expected_tpt[rs_index];
998 else
999 tpt = 0;
1000 if (info->flags & IEEE80211_TX_STAT_AMPDU)
1001 rs_collect_tx_data(search_win, rs_index, tpt,
1002 info->status.ampdu_ack_len,
1003 info->status.ampdu_ack_map);
1004 else
1005 rs_collect_tx_data(search_win, rs_index, tpt,
1006 1, status);
1007 /* Else if type matches "current/active" table,
1008 * add final tx status to "current/active" history */
1009 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
1010 (tbl_type.ant_type == curr_tbl->ant_type) &&
1011 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
1012 if (curr_tbl->expected_tpt)
1013 tpt = curr_tbl->expected_tpt[rs_index];
1014 else
1015 tpt = 0;
1016 if (info->flags & IEEE80211_TX_STAT_AMPDU)
1017 rs_collect_tx_data(window, rs_index, tpt,
1018 info->status.ampdu_ack_len,
1019 info->status.ampdu_ack_map);
1020 else
1021 rs_collect_tx_data(window, rs_index, tpt,
1022 1, status);
1025 /* If not searching for new mode, increment success/failed counter
1026 * ... these help determine when to start searching again */
1027 if (lq_sta->stay_in_tbl) {
1028 if (info->flags & IEEE80211_TX_STAT_AMPDU) {
1029 lq_sta->total_success += info->status.ampdu_ack_map;
1030 lq_sta->total_failed +=
1031 (info->status.ampdu_ack_len - info->status.ampdu_ack_map);
1032 } else {
1033 if (status)
1034 lq_sta->total_success++;
1035 else
1036 lq_sta->total_failed++;
1040 /* See if there's a better rate or modulation mode to try. */
1041 if (sta && sta->supp_rates[sband->band])
1042 rs_rate_scale_perform(priv, skb, sta, lq_sta);
1043 out:
1044 return;
1048 * Begin a period of staying with a selected modulation mode.
1049 * Set "stay_in_tbl" flag to prevent any mode switches.
1050 * Set frame tx success limits according to legacy vs. high-throughput,
1051 * and reset overall (spanning all rates) tx success history statistics.
1052 * These control how long we stay using same modulation mode before
1053 * searching for a new mode.
1055 static void rs_set_stay_in_table(struct iwl_priv *priv, u8 is_legacy,
1056 struct iwl_lq_sta *lq_sta)
1058 IWL_DEBUG_RATE(priv, "we are staying in the same table\n");
1059 lq_sta->stay_in_tbl = 1; /* only place this gets set */
1060 if (is_legacy) {
1061 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
1062 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
1063 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
1064 } else {
1065 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
1066 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
1067 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
1069 lq_sta->table_count = 0;
1070 lq_sta->total_failed = 0;
1071 lq_sta->total_success = 0;
1072 lq_sta->flush_timer = jiffies;
1073 lq_sta->action_counter = 0;
1077 * Find correct throughput table for given mode of modulation
1079 static void rs_set_expected_tpt_table(struct iwl_lq_sta *lq_sta,
1080 struct iwl_scale_tbl_info *tbl)
1082 if (is_legacy(tbl->lq_type)) {
1083 if (!is_a_band(tbl->lq_type))
1084 tbl->expected_tpt = expected_tpt_G;
1085 else
1086 tbl->expected_tpt = expected_tpt_A;
1087 } else if (is_siso(tbl->lq_type)) {
1088 if (tbl->is_ht40 && !lq_sta->is_dup)
1089 if (tbl->is_SGI)
1090 tbl->expected_tpt = expected_tpt_siso40MHzSGI;
1091 else
1092 tbl->expected_tpt = expected_tpt_siso40MHz;
1093 else if (tbl->is_SGI)
1094 tbl->expected_tpt = expected_tpt_siso20MHzSGI;
1095 else
1096 tbl->expected_tpt = expected_tpt_siso20MHz;
1097 } else if (is_mimo2(tbl->lq_type)) {
1098 if (tbl->is_ht40 && !lq_sta->is_dup)
1099 if (tbl->is_SGI)
1100 tbl->expected_tpt = expected_tpt_mimo2_40MHzSGI;
1101 else
1102 tbl->expected_tpt = expected_tpt_mimo2_40MHz;
1103 else if (tbl->is_SGI)
1104 tbl->expected_tpt = expected_tpt_mimo2_20MHzSGI;
1105 else
1106 tbl->expected_tpt = expected_tpt_mimo2_20MHz;
1107 } else if (is_mimo3(tbl->lq_type)) {
1108 if (tbl->is_ht40 && !lq_sta->is_dup)
1109 if (tbl->is_SGI)
1110 tbl->expected_tpt = expected_tpt_mimo3_40MHzSGI;
1111 else
1112 tbl->expected_tpt = expected_tpt_mimo3_40MHz;
1113 else if (tbl->is_SGI)
1114 tbl->expected_tpt = expected_tpt_mimo3_20MHzSGI;
1115 else
1116 tbl->expected_tpt = expected_tpt_mimo3_20MHz;
1117 } else
1118 tbl->expected_tpt = expected_tpt_G;
1122 * Find starting rate for new "search" high-throughput mode of modulation.
1123 * Goal is to find lowest expected rate (under perfect conditions) that is
1124 * above the current measured throughput of "active" mode, to give new mode
1125 * a fair chance to prove itself without too many challenges.
1127 * This gets called when transitioning to more aggressive modulation
1128 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
1129 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1130 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1131 * bit rate will typically need to increase, but not if performance was bad.
1133 static s32 rs_get_best_rate(struct iwl_priv *priv,
1134 struct iwl_lq_sta *lq_sta,
1135 struct iwl_scale_tbl_info *tbl, /* "search" */
1136 u16 rate_mask, s8 index)
1138 /* "active" values */
1139 struct iwl_scale_tbl_info *active_tbl =
1140 &(lq_sta->lq_info[lq_sta->active_tbl]);
1141 s32 active_sr = active_tbl->win[index].success_ratio;
1142 s32 active_tpt = active_tbl->expected_tpt[index];
1144 /* expected "search" throughput */
1145 s32 *tpt_tbl = tbl->expected_tpt;
1147 s32 new_rate, high, low, start_hi;
1148 u16 high_low;
1149 s8 rate = index;
1151 new_rate = high = low = start_hi = IWL_RATE_INVALID;
1153 for (; ;) {
1154 high_low = rs_get_adjacent_rate(priv, rate, rate_mask,
1155 tbl->lq_type);
1157 low = high_low & 0xff;
1158 high = (high_low >> 8) & 0xff;
1161 * Lower the "search" bit rate, to give new "search" mode
1162 * approximately the same throughput as "active" if:
1164 * 1) "Active" mode has been working modestly well (but not
1165 * great), and expected "search" throughput (under perfect
1166 * conditions) at candidate rate is above the actual
1167 * measured "active" throughput (but less than expected
1168 * "active" throughput under perfect conditions).
1169 * OR
1170 * 2) "Active" mode has been working perfectly or very well
1171 * and expected "search" throughput (under perfect
1172 * conditions) at candidate rate is above expected
1173 * "active" throughput (under perfect conditions).
1175 if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
1176 ((active_sr > IWL_RATE_DECREASE_TH) &&
1177 (active_sr <= IWL_RATE_HIGH_TH) &&
1178 (tpt_tbl[rate] <= active_tpt))) ||
1179 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
1180 (tpt_tbl[rate] > active_tpt))) {
1182 /* (2nd or later pass)
1183 * If we've already tried to raise the rate, and are
1184 * now trying to lower it, use the higher rate. */
1185 if (start_hi != IWL_RATE_INVALID) {
1186 new_rate = start_hi;
1187 break;
1190 new_rate = rate;
1192 /* Loop again with lower rate */
1193 if (low != IWL_RATE_INVALID)
1194 rate = low;
1196 /* Lower rate not available, use the original */
1197 else
1198 break;
1200 /* Else try to raise the "search" rate to match "active" */
1201 } else {
1202 /* (2nd or later pass)
1203 * If we've already tried to lower the rate, and are
1204 * now trying to raise it, use the lower rate. */
1205 if (new_rate != IWL_RATE_INVALID)
1206 break;
1208 /* Loop again with higher rate */
1209 else if (high != IWL_RATE_INVALID) {
1210 start_hi = high;
1211 rate = high;
1213 /* Higher rate not available, use the original */
1214 } else {
1215 new_rate = rate;
1216 break;
1221 return new_rate;
1225 * Set up search table for MIMO2
1227 static int rs_switch_to_mimo2(struct iwl_priv *priv,
1228 struct iwl_lq_sta *lq_sta,
1229 struct ieee80211_conf *conf,
1230 struct ieee80211_sta *sta,
1231 struct iwl_scale_tbl_info *tbl, int index)
1233 u16 rate_mask;
1234 s32 rate;
1235 s8 is_green = lq_sta->is_green;
1237 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1238 return -1;
1240 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
1241 == WLAN_HT_CAP_SM_PS_STATIC)
1242 return -1;
1244 /* Need both Tx chains/antennas to support MIMO */
1245 if (priv->hw_params.tx_chains_num < 2)
1246 return -1;
1248 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO2\n");
1250 tbl->lq_type = LQ_MIMO2;
1251 tbl->is_dup = lq_sta->is_dup;
1252 tbl->action = 0;
1253 tbl->max_search = IWL_MAX_SEARCH;
1254 rate_mask = lq_sta->active_mimo2_rate;
1256 if (iwl_is_ht40_tx_allowed(priv, &sta->ht_cap))
1257 tbl->is_ht40 = 1;
1258 else
1259 tbl->is_ht40 = 0;
1261 rs_set_expected_tpt_table(lq_sta, tbl);
1263 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1265 IWL_DEBUG_RATE(priv, "LQ: MIMO2 best rate %d mask %X\n", rate, rate_mask);
1266 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1267 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
1268 rate, rate_mask);
1269 return -1;
1271 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
1273 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
1274 tbl->current_rate, is_green);
1275 return 0;
1279 * Set up search table for MIMO3
1281 static int rs_switch_to_mimo3(struct iwl_priv *priv,
1282 struct iwl_lq_sta *lq_sta,
1283 struct ieee80211_conf *conf,
1284 struct ieee80211_sta *sta,
1285 struct iwl_scale_tbl_info *tbl, int index)
1287 u16 rate_mask;
1288 s32 rate;
1289 s8 is_green = lq_sta->is_green;
1291 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1292 return -1;
1294 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
1295 == WLAN_HT_CAP_SM_PS_STATIC)
1296 return -1;
1298 /* Need both Tx chains/antennas to support MIMO */
1299 if (priv->hw_params.tx_chains_num < 3)
1300 return -1;
1302 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO3\n");
1304 tbl->lq_type = LQ_MIMO3;
1305 tbl->is_dup = lq_sta->is_dup;
1306 tbl->action = 0;
1307 tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
1308 rate_mask = lq_sta->active_mimo3_rate;
1310 if (iwl_is_ht40_tx_allowed(priv, &sta->ht_cap))
1311 tbl->is_ht40 = 1;
1312 else
1313 tbl->is_ht40 = 0;
1315 rs_set_expected_tpt_table(lq_sta, tbl);
1317 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1319 IWL_DEBUG_RATE(priv, "LQ: MIMO3 best rate %d mask %X\n",
1320 rate, rate_mask);
1321 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1322 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
1323 rate, rate_mask);
1324 return -1;
1326 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
1328 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
1329 tbl->current_rate, is_green);
1330 return 0;
1334 * Set up search table for SISO
1336 static int rs_switch_to_siso(struct iwl_priv *priv,
1337 struct iwl_lq_sta *lq_sta,
1338 struct ieee80211_conf *conf,
1339 struct ieee80211_sta *sta,
1340 struct iwl_scale_tbl_info *tbl, int index)
1342 u16 rate_mask;
1343 u8 is_green = lq_sta->is_green;
1344 s32 rate;
1346 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1347 return -1;
1349 IWL_DEBUG_RATE(priv, "LQ: try to switch to SISO\n");
1351 tbl->is_dup = lq_sta->is_dup;
1352 tbl->lq_type = LQ_SISO;
1353 tbl->action = 0;
1354 tbl->max_search = IWL_MAX_SEARCH;
1355 rate_mask = lq_sta->active_siso_rate;
1357 if (iwl_is_ht40_tx_allowed(priv, &sta->ht_cap))
1358 tbl->is_ht40 = 1;
1359 else
1360 tbl->is_ht40 = 0;
1362 if (is_green)
1363 tbl->is_SGI = 0; /*11n spec: no SGI in SISO+Greenfield*/
1365 rs_set_expected_tpt_table(lq_sta, tbl);
1366 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1368 IWL_DEBUG_RATE(priv, "LQ: get best rate %d mask %X\n", rate, rate_mask);
1369 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1370 IWL_DEBUG_RATE(priv, "can not switch with index %d rate mask %x\n",
1371 rate, rate_mask);
1372 return -1;
1374 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
1375 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
1376 tbl->current_rate, is_green);
1377 return 0;
1381 * Try to switch to new modulation mode from legacy
1383 static int rs_move_legacy_other(struct iwl_priv *priv,
1384 struct iwl_lq_sta *lq_sta,
1385 struct ieee80211_conf *conf,
1386 struct ieee80211_sta *sta,
1387 int index)
1389 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1390 struct iwl_scale_tbl_info *search_tbl =
1391 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1392 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1393 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1394 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1395 u8 start_action = tbl->action;
1396 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1397 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1398 int ret = 0;
1399 u8 update_search_tbl_counter = 0;
1401 if (!iwl_ht_enabled(priv))
1402 /* stay in Legacy */
1403 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
1404 else if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE &&
1405 tbl->action > IWL_LEGACY_SWITCH_SISO)
1406 tbl->action = IWL_LEGACY_SWITCH_SISO;
1407 for (; ;) {
1408 lq_sta->action_counter++;
1409 switch (tbl->action) {
1410 case IWL_LEGACY_SWITCH_ANTENNA1:
1411 case IWL_LEGACY_SWITCH_ANTENNA2:
1412 IWL_DEBUG_RATE(priv, "LQ: Legacy toggle Antenna\n");
1414 if ((tbl->action == IWL_LEGACY_SWITCH_ANTENNA1 &&
1415 tx_chains_num <= 1) ||
1416 (tbl->action == IWL_LEGACY_SWITCH_ANTENNA2 &&
1417 tx_chains_num <= 2))
1418 break;
1420 /* Don't change antenna if success has been great */
1421 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1422 break;
1424 /* Set up search table to try other antenna */
1425 memcpy(search_tbl, tbl, sz);
1427 if (rs_toggle_antenna(valid_tx_ant,
1428 &search_tbl->current_rate, search_tbl)) {
1429 update_search_tbl_counter = 1;
1430 rs_set_expected_tpt_table(lq_sta, search_tbl);
1431 goto out;
1433 break;
1434 case IWL_LEGACY_SWITCH_SISO:
1435 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to SISO\n");
1437 /* Set up search table to try SISO */
1438 memcpy(search_tbl, tbl, sz);
1439 search_tbl->is_SGI = 0;
1440 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
1441 search_tbl, index);
1442 if (!ret) {
1443 lq_sta->action_counter = 0;
1444 goto out;
1447 break;
1448 case IWL_LEGACY_SWITCH_MIMO2_AB:
1449 case IWL_LEGACY_SWITCH_MIMO2_AC:
1450 case IWL_LEGACY_SWITCH_MIMO2_BC:
1451 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO2\n");
1453 /* Set up search table to try MIMO */
1454 memcpy(search_tbl, tbl, sz);
1455 search_tbl->is_SGI = 0;
1457 if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AB)
1458 search_tbl->ant_type = ANT_AB;
1459 else if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AC)
1460 search_tbl->ant_type = ANT_AC;
1461 else
1462 search_tbl->ant_type = ANT_BC;
1464 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1465 break;
1467 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
1468 search_tbl, index);
1469 if (!ret) {
1470 lq_sta->action_counter = 0;
1471 goto out;
1473 break;
1475 case IWL_LEGACY_SWITCH_MIMO3_ABC:
1476 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO3\n");
1478 /* Set up search table to try MIMO3 */
1479 memcpy(search_tbl, tbl, sz);
1480 search_tbl->is_SGI = 0;
1482 search_tbl->ant_type = ANT_ABC;
1484 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1485 break;
1487 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1488 search_tbl, index);
1489 if (!ret) {
1490 lq_sta->action_counter = 0;
1491 goto out;
1493 break;
1495 tbl->action++;
1496 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
1497 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
1499 if (tbl->action == start_action)
1500 break;
1503 search_tbl->lq_type = LQ_NONE;
1504 return 0;
1506 out:
1507 lq_sta->search_better_tbl = 1;
1508 tbl->action++;
1509 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
1510 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
1511 if (update_search_tbl_counter)
1512 search_tbl->action = tbl->action;
1513 return 0;
1518 * Try to switch to new modulation mode from SISO
1520 static int rs_move_siso_to_other(struct iwl_priv *priv,
1521 struct iwl_lq_sta *lq_sta,
1522 struct ieee80211_conf *conf,
1523 struct ieee80211_sta *sta, int index)
1525 u8 is_green = lq_sta->is_green;
1526 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1527 struct iwl_scale_tbl_info *search_tbl =
1528 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1529 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1530 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
1531 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1532 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1533 u8 start_action = tbl->action;
1534 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1535 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1536 u8 update_search_tbl_counter = 0;
1537 int ret;
1539 if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE &&
1540 tbl->action > IWL_SISO_SWITCH_ANTENNA2) {
1541 /* stay in SISO */
1542 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1544 for (;;) {
1545 lq_sta->action_counter++;
1546 switch (tbl->action) {
1547 case IWL_SISO_SWITCH_ANTENNA1:
1548 case IWL_SISO_SWITCH_ANTENNA2:
1549 IWL_DEBUG_RATE(priv, "LQ: SISO toggle Antenna\n");
1551 if ((tbl->action == IWL_SISO_SWITCH_ANTENNA1 &&
1552 tx_chains_num <= 1) ||
1553 (tbl->action == IWL_SISO_SWITCH_ANTENNA2 &&
1554 tx_chains_num <= 2))
1555 break;
1557 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1558 break;
1560 memcpy(search_tbl, tbl, sz);
1561 if (rs_toggle_antenna(valid_tx_ant,
1562 &search_tbl->current_rate, search_tbl)) {
1563 update_search_tbl_counter = 1;
1564 goto out;
1566 break;
1567 case IWL_SISO_SWITCH_MIMO2_AB:
1568 case IWL_SISO_SWITCH_MIMO2_AC:
1569 case IWL_SISO_SWITCH_MIMO2_BC:
1570 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO2\n");
1571 memcpy(search_tbl, tbl, sz);
1572 search_tbl->is_SGI = 0;
1574 if (tbl->action == IWL_SISO_SWITCH_MIMO2_AB)
1575 search_tbl->ant_type = ANT_AB;
1576 else if (tbl->action == IWL_SISO_SWITCH_MIMO2_AC)
1577 search_tbl->ant_type = ANT_AC;
1578 else
1579 search_tbl->ant_type = ANT_BC;
1581 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1582 break;
1584 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
1585 search_tbl, index);
1586 if (!ret)
1587 goto out;
1588 break;
1589 case IWL_SISO_SWITCH_GI:
1590 if (!tbl->is_ht40 && !(ht_cap->cap &
1591 IEEE80211_HT_CAP_SGI_20))
1592 break;
1593 if (tbl->is_ht40 && !(ht_cap->cap &
1594 IEEE80211_HT_CAP_SGI_40))
1595 break;
1597 IWL_DEBUG_RATE(priv, "LQ: SISO toggle SGI/NGI\n");
1599 memcpy(search_tbl, tbl, sz);
1600 if (is_green) {
1601 if (!tbl->is_SGI)
1602 break;
1603 else
1604 IWL_ERR(priv,
1605 "SGI was set in GF+SISO\n");
1607 search_tbl->is_SGI = !tbl->is_SGI;
1608 rs_set_expected_tpt_table(lq_sta, search_tbl);
1609 if (tbl->is_SGI) {
1610 s32 tpt = lq_sta->last_tpt / 100;
1611 if (tpt >= search_tbl->expected_tpt[index])
1612 break;
1614 search_tbl->current_rate =
1615 rate_n_flags_from_tbl(priv, search_tbl,
1616 index, is_green);
1617 update_search_tbl_counter = 1;
1618 goto out;
1619 case IWL_SISO_SWITCH_MIMO3_ABC:
1620 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO3\n");
1621 memcpy(search_tbl, tbl, sz);
1622 search_tbl->is_SGI = 0;
1623 search_tbl->ant_type = ANT_ABC;
1625 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1626 break;
1628 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1629 search_tbl, index);
1630 if (!ret)
1631 goto out;
1632 break;
1634 tbl->action++;
1635 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
1636 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1638 if (tbl->action == start_action)
1639 break;
1641 search_tbl->lq_type = LQ_NONE;
1642 return 0;
1644 out:
1645 lq_sta->search_better_tbl = 1;
1646 tbl->action++;
1647 if (tbl->action > IWL_SISO_SWITCH_MIMO3_ABC)
1648 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1649 if (update_search_tbl_counter)
1650 search_tbl->action = tbl->action;
1652 return 0;
1656 * Try to switch to new modulation mode from MIMO2
1658 static int rs_move_mimo2_to_other(struct iwl_priv *priv,
1659 struct iwl_lq_sta *lq_sta,
1660 struct ieee80211_conf *conf,
1661 struct ieee80211_sta *sta, int index)
1663 s8 is_green = lq_sta->is_green;
1664 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1665 struct iwl_scale_tbl_info *search_tbl =
1666 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1667 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1668 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
1669 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1670 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1671 u8 start_action = tbl->action;
1672 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1673 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1674 u8 update_search_tbl_counter = 0;
1675 int ret;
1677 if ((iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE) &&
1678 (tbl->action < IWL_MIMO2_SWITCH_SISO_A ||
1679 tbl->action > IWL_MIMO2_SWITCH_SISO_C)) {
1680 /* switch in SISO */
1681 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1683 for (;;) {
1684 lq_sta->action_counter++;
1685 switch (tbl->action) {
1686 case IWL_MIMO2_SWITCH_ANTENNA1:
1687 case IWL_MIMO2_SWITCH_ANTENNA2:
1688 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle Antennas\n");
1690 if (tx_chains_num <= 2)
1691 break;
1693 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1694 break;
1696 memcpy(search_tbl, tbl, sz);
1697 if (rs_toggle_antenna(valid_tx_ant,
1698 &search_tbl->current_rate, search_tbl)) {
1699 update_search_tbl_counter = 1;
1700 goto out;
1702 break;
1703 case IWL_MIMO2_SWITCH_SISO_A:
1704 case IWL_MIMO2_SWITCH_SISO_B:
1705 case IWL_MIMO2_SWITCH_SISO_C:
1706 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to SISO\n");
1708 /* Set up new search table for SISO */
1709 memcpy(search_tbl, tbl, sz);
1711 if (tbl->action == IWL_MIMO2_SWITCH_SISO_A)
1712 search_tbl->ant_type = ANT_A;
1713 else if (tbl->action == IWL_MIMO2_SWITCH_SISO_B)
1714 search_tbl->ant_type = ANT_B;
1715 else
1716 search_tbl->ant_type = ANT_C;
1718 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1719 break;
1721 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
1722 search_tbl, index);
1723 if (!ret)
1724 goto out;
1726 break;
1728 case IWL_MIMO2_SWITCH_GI:
1729 if (!tbl->is_ht40 && !(ht_cap->cap &
1730 IEEE80211_HT_CAP_SGI_20))
1731 break;
1732 if (tbl->is_ht40 && !(ht_cap->cap &
1733 IEEE80211_HT_CAP_SGI_40))
1734 break;
1736 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle SGI/NGI\n");
1738 /* Set up new search table for MIMO2 */
1739 memcpy(search_tbl, tbl, sz);
1740 search_tbl->is_SGI = !tbl->is_SGI;
1741 rs_set_expected_tpt_table(lq_sta, search_tbl);
1743 * If active table already uses the fastest possible
1744 * modulation (dual stream with short guard interval),
1745 * and it's working well, there's no need to look
1746 * for a better type of modulation!
1748 if (tbl->is_SGI) {
1749 s32 tpt = lq_sta->last_tpt / 100;
1750 if (tpt >= search_tbl->expected_tpt[index])
1751 break;
1753 search_tbl->current_rate =
1754 rate_n_flags_from_tbl(priv, search_tbl,
1755 index, is_green);
1756 update_search_tbl_counter = 1;
1757 goto out;
1759 case IWL_MIMO2_SWITCH_MIMO3_ABC:
1760 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to MIMO3\n");
1761 memcpy(search_tbl, tbl, sz);
1762 search_tbl->is_SGI = 0;
1763 search_tbl->ant_type = ANT_ABC;
1765 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1766 break;
1768 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1769 search_tbl, index);
1770 if (!ret)
1771 goto out;
1773 break;
1775 tbl->action++;
1776 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1777 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
1779 if (tbl->action == start_action)
1780 break;
1782 search_tbl->lq_type = LQ_NONE;
1783 return 0;
1784 out:
1785 lq_sta->search_better_tbl = 1;
1786 tbl->action++;
1787 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1788 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
1789 if (update_search_tbl_counter)
1790 search_tbl->action = tbl->action;
1792 return 0;
1797 * Try to switch to new modulation mode from MIMO3
1799 static int rs_move_mimo3_to_other(struct iwl_priv *priv,
1800 struct iwl_lq_sta *lq_sta,
1801 struct ieee80211_conf *conf,
1802 struct ieee80211_sta *sta, int index)
1804 s8 is_green = lq_sta->is_green;
1805 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1806 struct iwl_scale_tbl_info *search_tbl =
1807 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1808 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1809 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
1810 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1811 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1812 u8 start_action = tbl->action;
1813 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1814 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1815 int ret;
1816 u8 update_search_tbl_counter = 0;
1818 if ((iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE) &&
1819 (tbl->action < IWL_MIMO3_SWITCH_SISO_A ||
1820 tbl->action > IWL_MIMO3_SWITCH_SISO_C)) {
1821 /* switch in SISO */
1822 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
1824 for (;;) {
1825 lq_sta->action_counter++;
1826 switch (tbl->action) {
1827 case IWL_MIMO3_SWITCH_ANTENNA1:
1828 case IWL_MIMO3_SWITCH_ANTENNA2:
1829 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle Antennas\n");
1831 if (tx_chains_num <= 3)
1832 break;
1834 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1835 break;
1837 memcpy(search_tbl, tbl, sz);
1838 if (rs_toggle_antenna(valid_tx_ant,
1839 &search_tbl->current_rate, search_tbl))
1840 goto out;
1841 break;
1842 case IWL_MIMO3_SWITCH_SISO_A:
1843 case IWL_MIMO3_SWITCH_SISO_B:
1844 case IWL_MIMO3_SWITCH_SISO_C:
1845 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to SISO\n");
1847 /* Set up new search table for SISO */
1848 memcpy(search_tbl, tbl, sz);
1850 if (tbl->action == IWL_MIMO3_SWITCH_SISO_A)
1851 search_tbl->ant_type = ANT_A;
1852 else if (tbl->action == IWL_MIMO3_SWITCH_SISO_B)
1853 search_tbl->ant_type = ANT_B;
1854 else
1855 search_tbl->ant_type = ANT_C;
1857 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1858 break;
1860 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
1861 search_tbl, index);
1862 if (!ret)
1863 goto out;
1865 break;
1867 case IWL_MIMO3_SWITCH_MIMO2_AB:
1868 case IWL_MIMO3_SWITCH_MIMO2_AC:
1869 case IWL_MIMO3_SWITCH_MIMO2_BC:
1870 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to MIMO2\n");
1872 memcpy(search_tbl, tbl, sz);
1873 search_tbl->is_SGI = 0;
1874 if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AB)
1875 search_tbl->ant_type = ANT_AB;
1876 else if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AC)
1877 search_tbl->ant_type = ANT_AC;
1878 else
1879 search_tbl->ant_type = ANT_BC;
1881 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1882 break;
1884 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
1885 search_tbl, index);
1886 if (!ret)
1887 goto out;
1889 break;
1891 case IWL_MIMO3_SWITCH_GI:
1892 if (!tbl->is_ht40 && !(ht_cap->cap &
1893 IEEE80211_HT_CAP_SGI_20))
1894 break;
1895 if (tbl->is_ht40 && !(ht_cap->cap &
1896 IEEE80211_HT_CAP_SGI_40))
1897 break;
1899 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle SGI/NGI\n");
1901 /* Set up new search table for MIMO */
1902 memcpy(search_tbl, tbl, sz);
1903 search_tbl->is_SGI = !tbl->is_SGI;
1904 rs_set_expected_tpt_table(lq_sta, search_tbl);
1906 * If active table already uses the fastest possible
1907 * modulation (dual stream with short guard interval),
1908 * and it's working well, there's no need to look
1909 * for a better type of modulation!
1911 if (tbl->is_SGI) {
1912 s32 tpt = lq_sta->last_tpt / 100;
1913 if (tpt >= search_tbl->expected_tpt[index])
1914 break;
1916 search_tbl->current_rate =
1917 rate_n_flags_from_tbl(priv, search_tbl,
1918 index, is_green);
1919 update_search_tbl_counter = 1;
1920 goto out;
1922 tbl->action++;
1923 if (tbl->action > IWL_MIMO3_SWITCH_GI)
1924 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
1926 if (tbl->action == start_action)
1927 break;
1929 search_tbl->lq_type = LQ_NONE;
1930 return 0;
1931 out:
1932 lq_sta->search_better_tbl = 1;
1933 tbl->action++;
1934 if (tbl->action > IWL_MIMO3_SWITCH_GI)
1935 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
1936 if (update_search_tbl_counter)
1937 search_tbl->action = tbl->action;
1939 return 0;
1944 * Check whether we should continue using same modulation mode, or
1945 * begin search for a new mode, based on:
1946 * 1) # tx successes or failures while using this mode
1947 * 2) # times calling this function
1948 * 3) elapsed time in this mode (not used, for now)
1950 static void rs_stay_in_table(struct iwl_lq_sta *lq_sta)
1952 struct iwl_scale_tbl_info *tbl;
1953 int i;
1954 int active_tbl;
1955 int flush_interval_passed = 0;
1956 struct iwl_priv *priv;
1958 priv = lq_sta->drv;
1959 active_tbl = lq_sta->active_tbl;
1961 tbl = &(lq_sta->lq_info[active_tbl]);
1963 /* If we've been disallowing search, see if we should now allow it */
1964 if (lq_sta->stay_in_tbl) {
1966 /* Elapsed time using current modulation mode */
1967 if (lq_sta->flush_timer)
1968 flush_interval_passed =
1969 time_after(jiffies,
1970 (unsigned long)(lq_sta->flush_timer +
1971 IWL_RATE_SCALE_FLUSH_INTVL));
1974 * Check if we should allow search for new modulation mode.
1975 * If many frames have failed or succeeded, or we've used
1976 * this same modulation for a long time, allow search, and
1977 * reset history stats that keep track of whether we should
1978 * allow a new search. Also (below) reset all bitmaps and
1979 * stats in active history.
1981 if ((lq_sta->total_failed > lq_sta->max_failure_limit) ||
1982 (lq_sta->total_success > lq_sta->max_success_limit) ||
1983 ((!lq_sta->search_better_tbl) && (lq_sta->flush_timer)
1984 && (flush_interval_passed))) {
1985 IWL_DEBUG_RATE(priv, "LQ: stay is expired %d %d %d\n:",
1986 lq_sta->total_failed,
1987 lq_sta->total_success,
1988 flush_interval_passed);
1990 /* Allow search for new mode */
1991 lq_sta->stay_in_tbl = 0; /* only place reset */
1992 lq_sta->total_failed = 0;
1993 lq_sta->total_success = 0;
1994 lq_sta->flush_timer = 0;
1997 * Else if we've used this modulation mode enough repetitions
1998 * (regardless of elapsed time or success/failure), reset
1999 * history bitmaps and rate-specific stats for all rates in
2000 * active table.
2002 } else {
2003 lq_sta->table_count++;
2004 if (lq_sta->table_count >=
2005 lq_sta->table_count_limit) {
2006 lq_sta->table_count = 0;
2008 IWL_DEBUG_RATE(priv, "LQ: stay in table clear win\n");
2009 for (i = 0; i < IWL_RATE_COUNT; i++)
2010 rs_rate_scale_clear_window(
2011 &(tbl->win[i]));
2015 /* If transitioning to allow "search", reset all history
2016 * bitmaps and stats in active table (this will become the new
2017 * "search" table). */
2018 if (!lq_sta->stay_in_tbl) {
2019 for (i = 0; i < IWL_RATE_COUNT; i++)
2020 rs_rate_scale_clear_window(&(tbl->win[i]));
2026 * setup rate table in uCode
2027 * return rate_n_flags as used in the table
2029 static u32 rs_update_rate_tbl(struct iwl_priv *priv,
2030 struct iwl_lq_sta *lq_sta,
2031 struct iwl_scale_tbl_info *tbl,
2032 int index, u8 is_green)
2034 u32 rate;
2036 /* Update uCode's rate table. */
2037 rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
2038 rs_fill_link_cmd(priv, lq_sta, rate);
2039 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
2041 return rate;
2045 * Do rate scaling and search for new modulation mode.
2047 static void rs_rate_scale_perform(struct iwl_priv *priv,
2048 struct sk_buff *skb,
2049 struct ieee80211_sta *sta,
2050 struct iwl_lq_sta *lq_sta)
2052 struct ieee80211_hw *hw = priv->hw;
2053 struct ieee80211_conf *conf = &hw->conf;
2054 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2055 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2056 int low = IWL_RATE_INVALID;
2057 int high = IWL_RATE_INVALID;
2058 int index;
2059 int i;
2060 struct iwl_rate_scale_data *window = NULL;
2061 int current_tpt = IWL_INVALID_VALUE;
2062 int low_tpt = IWL_INVALID_VALUE;
2063 int high_tpt = IWL_INVALID_VALUE;
2064 u32 fail_count;
2065 s8 scale_action = 0;
2066 u16 rate_mask;
2067 u8 update_lq = 0;
2068 struct iwl_scale_tbl_info *tbl, *tbl1;
2069 u16 rate_scale_index_msk = 0;
2070 u32 rate;
2071 u8 is_green = 0;
2072 u8 active_tbl = 0;
2073 u8 done_search = 0;
2074 u16 high_low;
2075 s32 sr;
2076 u8 tid = MAX_TID_COUNT;
2077 struct iwl_tid_data *tid_data;
2079 IWL_DEBUG_RATE(priv, "rate scale calculate new rate for skb\n");
2081 /* Send management frames and NO_ACK data using lowest rate. */
2082 /* TODO: this could probably be improved.. */
2083 if (!ieee80211_is_data(hdr->frame_control) ||
2084 info->flags & IEEE80211_TX_CTL_NO_ACK)
2085 return;
2087 if (!sta || !lq_sta)
2088 return;
2090 lq_sta->supp_rates = sta->supp_rates[lq_sta->band];
2092 tid = rs_tl_add_packet(lq_sta, hdr);
2095 * Select rate-scale / modulation-mode table to work with in
2096 * the rest of this function: "search" if searching for better
2097 * modulation mode, or "active" if doing rate scaling within a mode.
2099 if (!lq_sta->search_better_tbl)
2100 active_tbl = lq_sta->active_tbl;
2101 else
2102 active_tbl = 1 - lq_sta->active_tbl;
2104 tbl = &(lq_sta->lq_info[active_tbl]);
2105 if (is_legacy(tbl->lq_type))
2106 lq_sta->is_green = 0;
2107 else
2108 lq_sta->is_green = rs_use_green(sta, &priv->current_ht_config);
2109 is_green = lq_sta->is_green;
2111 /* current tx rate */
2112 index = lq_sta->last_txrate_idx;
2114 IWL_DEBUG_RATE(priv, "Rate scale index %d for type %d\n", index,
2115 tbl->lq_type);
2117 /* rates available for this association, and for modulation mode */
2118 rate_mask = rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
2120 IWL_DEBUG_RATE(priv, "mask 0x%04X \n", rate_mask);
2122 /* mask with station rate restriction */
2123 if (is_legacy(tbl->lq_type)) {
2124 if (lq_sta->band == IEEE80211_BAND_5GHZ)
2125 /* supp_rates has no CCK bits in A mode */
2126 rate_scale_index_msk = (u16) (rate_mask &
2127 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
2128 else
2129 rate_scale_index_msk = (u16) (rate_mask &
2130 lq_sta->supp_rates);
2132 } else
2133 rate_scale_index_msk = rate_mask;
2135 if (!rate_scale_index_msk)
2136 rate_scale_index_msk = rate_mask;
2138 if (!((1 << index) & rate_scale_index_msk)) {
2139 IWL_ERR(priv, "Current Rate is not valid\n");
2140 if (lq_sta->search_better_tbl) {
2141 /* revert to active table if search table is not valid*/
2142 tbl->lq_type = LQ_NONE;
2143 lq_sta->search_better_tbl = 0;
2144 tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
2145 /* get "active" rate info */
2146 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
2147 rate = rs_update_rate_tbl(priv, lq_sta,
2148 tbl, index, is_green);
2150 return;
2153 /* Get expected throughput table and history window for current rate */
2154 if (!tbl->expected_tpt) {
2155 IWL_ERR(priv, "tbl->expected_tpt is NULL\n");
2156 return;
2159 /* force user max rate if set by user */
2160 if ((lq_sta->max_rate_idx != -1) &&
2161 (lq_sta->max_rate_idx < index)) {
2162 index = lq_sta->max_rate_idx;
2163 update_lq = 1;
2164 window = &(tbl->win[index]);
2165 goto lq_update;
2168 window = &(tbl->win[index]);
2171 * If there is not enough history to calculate actual average
2172 * throughput, keep analyzing results of more tx frames, without
2173 * changing rate or mode (bypass most of the rest of this function).
2174 * Set up new rate table in uCode only if old rate is not supported
2175 * in current association (use new rate found above).
2177 fail_count = window->counter - window->success_counter;
2178 if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
2179 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
2180 IWL_DEBUG_RATE(priv, "LQ: still below TH. succ=%d total=%d "
2181 "for index %d\n",
2182 window->success_counter, window->counter, index);
2184 /* Can't calculate this yet; not enough history */
2185 window->average_tpt = IWL_INVALID_VALUE;
2187 /* Should we stay with this modulation mode,
2188 * or search for a new one? */
2189 rs_stay_in_table(lq_sta);
2191 goto out;
2194 /* Else we have enough samples; calculate estimate of
2195 * actual average throughput */
2196 if (window->average_tpt != ((window->success_ratio *
2197 tbl->expected_tpt[index] + 64) / 128)) {
2198 IWL_ERR(priv, "expected_tpt should have been calculated by now\n");
2199 window->average_tpt = ((window->success_ratio *
2200 tbl->expected_tpt[index] + 64) / 128);
2203 /* If we are searching for better modulation mode, check success. */
2204 if (lq_sta->search_better_tbl &&
2205 (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_MULTI)) {
2206 /* If good success, continue using the "search" mode;
2207 * no need to send new link quality command, since we're
2208 * continuing to use the setup that we've been trying. */
2209 if (window->average_tpt > lq_sta->last_tpt) {
2211 IWL_DEBUG_RATE(priv, "LQ: SWITCHING TO NEW TABLE "
2212 "suc=%d cur-tpt=%d old-tpt=%d\n",
2213 window->success_ratio,
2214 window->average_tpt,
2215 lq_sta->last_tpt);
2217 if (!is_legacy(tbl->lq_type))
2218 lq_sta->enable_counter = 1;
2220 /* Swap tables; "search" becomes "active" */
2221 lq_sta->active_tbl = active_tbl;
2222 current_tpt = window->average_tpt;
2224 /* Else poor success; go back to mode in "active" table */
2225 } else {
2227 IWL_DEBUG_RATE(priv, "LQ: GOING BACK TO THE OLD TABLE "
2228 "suc=%d cur-tpt=%d old-tpt=%d\n",
2229 window->success_ratio,
2230 window->average_tpt,
2231 lq_sta->last_tpt);
2233 /* Nullify "search" table */
2234 tbl->lq_type = LQ_NONE;
2236 /* Revert to "active" table */
2237 active_tbl = lq_sta->active_tbl;
2238 tbl = &(lq_sta->lq_info[active_tbl]);
2240 /* Revert to "active" rate and throughput info */
2241 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
2242 current_tpt = lq_sta->last_tpt;
2244 /* Need to set up a new rate table in uCode */
2245 update_lq = 1;
2248 /* Either way, we've made a decision; modulation mode
2249 * search is done, allow rate adjustment next time. */
2250 lq_sta->search_better_tbl = 0;
2251 done_search = 1; /* Don't switch modes below! */
2252 goto lq_update;
2255 /* (Else) not in search of better modulation mode, try for better
2256 * starting rate, while staying in this mode. */
2257 high_low = rs_get_adjacent_rate(priv, index, rate_scale_index_msk,
2258 tbl->lq_type);
2259 low = high_low & 0xff;
2260 high = (high_low >> 8) & 0xff;
2262 /* If user set max rate, dont allow higher than user constrain */
2263 if ((lq_sta->max_rate_idx != -1) &&
2264 (lq_sta->max_rate_idx < high))
2265 high = IWL_RATE_INVALID;
2267 sr = window->success_ratio;
2269 /* Collect measured throughputs for current and adjacent rates */
2270 current_tpt = window->average_tpt;
2271 if (low != IWL_RATE_INVALID)
2272 low_tpt = tbl->win[low].average_tpt;
2273 if (high != IWL_RATE_INVALID)
2274 high_tpt = tbl->win[high].average_tpt;
2276 scale_action = 0;
2278 /* Too many failures, decrease rate */
2279 if ((sr <= IWL_RATE_DECREASE_TH) || (current_tpt == 0)) {
2280 IWL_DEBUG_RATE(priv, "decrease rate because of low success_ratio\n");
2281 scale_action = -1;
2283 /* No throughput measured yet for adjacent rates; try increase. */
2284 } else if ((low_tpt == IWL_INVALID_VALUE) &&
2285 (high_tpt == IWL_INVALID_VALUE)) {
2287 if (high != IWL_RATE_INVALID && sr >= IWL_RATE_INCREASE_TH)
2288 scale_action = 1;
2289 else if (low != IWL_RATE_INVALID)
2290 scale_action = 0;
2293 /* Both adjacent throughputs are measured, but neither one has better
2294 * throughput; we're using the best rate, don't change it! */
2295 else if ((low_tpt != IWL_INVALID_VALUE) &&
2296 (high_tpt != IWL_INVALID_VALUE) &&
2297 (low_tpt < current_tpt) &&
2298 (high_tpt < current_tpt))
2299 scale_action = 0;
2301 /* At least one adjacent rate's throughput is measured,
2302 * and may have better performance. */
2303 else {
2304 /* Higher adjacent rate's throughput is measured */
2305 if (high_tpt != IWL_INVALID_VALUE) {
2306 /* Higher rate has better throughput */
2307 if (high_tpt > current_tpt &&
2308 sr >= IWL_RATE_INCREASE_TH) {
2309 scale_action = 1;
2310 } else {
2311 scale_action = 0;
2314 /* Lower adjacent rate's throughput is measured */
2315 } else if (low_tpt != IWL_INVALID_VALUE) {
2316 /* Lower rate has better throughput */
2317 if (low_tpt > current_tpt) {
2318 IWL_DEBUG_RATE(priv,
2319 "decrease rate because of low tpt\n");
2320 scale_action = -1;
2321 } else if (sr >= IWL_RATE_INCREASE_TH) {
2322 scale_action = 1;
2327 /* Sanity check; asked for decrease, but success rate or throughput
2328 * has been good at old rate. Don't change it. */
2329 if ((scale_action == -1) && (low != IWL_RATE_INVALID) &&
2330 ((sr > IWL_RATE_HIGH_TH) ||
2331 (current_tpt > (100 * tbl->expected_tpt[low]))))
2332 scale_action = 0;
2333 if (!iwl_ht_enabled(priv) && !is_legacy(tbl->lq_type))
2334 scale_action = -1;
2335 if (iwl_tx_ant_restriction(priv) != IWL_ANT_OK_MULTI &&
2336 (is_mimo2(tbl->lq_type) || is_mimo3(tbl->lq_type)))
2337 scale_action = -1;
2338 switch (scale_action) {
2339 case -1:
2340 /* Decrease starting rate, update uCode's rate table */
2341 if (low != IWL_RATE_INVALID) {
2342 update_lq = 1;
2343 index = low;
2346 break;
2347 case 1:
2348 /* Increase starting rate, update uCode's rate table */
2349 if (high != IWL_RATE_INVALID) {
2350 update_lq = 1;
2351 index = high;
2354 break;
2355 case 0:
2356 /* No change */
2357 default:
2358 break;
2361 IWL_DEBUG_RATE(priv, "choose rate scale index %d action %d low %d "
2362 "high %d type %d\n",
2363 index, scale_action, low, high, tbl->lq_type);
2365 lq_update:
2366 /* Replace uCode's rate table for the destination station. */
2367 if (update_lq)
2368 rate = rs_update_rate_tbl(priv, lq_sta,
2369 tbl, index, is_green);
2371 if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_MULTI) {
2372 /* Should we stay with this modulation mode,
2373 * or search for a new one? */
2374 rs_stay_in_table(lq_sta);
2377 * Search for new modulation mode if we're:
2378 * 1) Not changing rates right now
2379 * 2) Not just finishing up a search
2380 * 3) Allowing a new search
2382 if (!update_lq && !done_search && !lq_sta->stay_in_tbl && window->counter) {
2383 /* Save current throughput to compare with "search" throughput*/
2384 lq_sta->last_tpt = current_tpt;
2386 /* Select a new "search" modulation mode to try.
2387 * If one is found, set up the new "search" table. */
2388 if (is_legacy(tbl->lq_type))
2389 rs_move_legacy_other(priv, lq_sta, conf, sta, index);
2390 else if (is_siso(tbl->lq_type))
2391 rs_move_siso_to_other(priv, lq_sta, conf, sta, index);
2392 else if (is_mimo2(tbl->lq_type))
2393 rs_move_mimo2_to_other(priv, lq_sta, conf, sta, index);
2394 else
2395 rs_move_mimo3_to_other(priv, lq_sta, conf, sta, index);
2397 /* If new "search" mode was selected, set up in uCode table */
2398 if (lq_sta->search_better_tbl) {
2399 /* Access the "search" table, clear its history. */
2400 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
2401 for (i = 0; i < IWL_RATE_COUNT; i++)
2402 rs_rate_scale_clear_window(&(tbl->win[i]));
2404 /* Use new "search" start rate */
2405 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
2407 IWL_DEBUG_RATE(priv, "Switch current mcs: %X index: %d\n",
2408 tbl->current_rate, index);
2409 rs_fill_link_cmd(priv, lq_sta, tbl->current_rate);
2410 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
2411 } else
2412 done_search = 1;
2415 if (done_search && !lq_sta->stay_in_tbl) {
2416 /* If the "active" (non-search) mode was legacy,
2417 * and we've tried switching antennas,
2418 * but we haven't been able to try HT modes (not available),
2419 * stay with best antenna legacy modulation for a while
2420 * before next round of mode comparisons. */
2421 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
2422 if (is_legacy(tbl1->lq_type) && !conf_is_ht(conf) &&
2423 lq_sta->action_counter > tbl1->max_search) {
2424 IWL_DEBUG_RATE(priv, "LQ: STAY in legacy table\n");
2425 rs_set_stay_in_table(priv, 1, lq_sta);
2428 /* If we're in an HT mode, and all 3 mode switch actions
2429 * have been tried and compared, stay in this best modulation
2430 * mode for a while before next round of mode comparisons. */
2431 if (lq_sta->enable_counter &&
2432 (lq_sta->action_counter >= tbl1->max_search) &&
2433 iwl_ht_enabled(priv)) {
2434 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2435 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
2436 (tid != MAX_TID_COUNT)) {
2437 tid_data =
2438 &priv->stations[lq_sta->lq.sta_id].tid[tid];
2439 if (tid_data->agg.state == IWL_AGG_OFF) {
2440 IWL_DEBUG_RATE(priv,
2441 "try to aggregate tid %d\n",
2442 tid);
2443 rs_tl_turn_on_agg(priv, tid,
2444 lq_sta, sta);
2447 rs_set_stay_in_table(priv, 0, lq_sta);
2451 out:
2452 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
2453 i = index;
2454 lq_sta->last_txrate_idx = i;
2456 return;
2460 static void rs_initialize_lq(struct iwl_priv *priv,
2461 struct ieee80211_conf *conf,
2462 struct ieee80211_sta *sta,
2463 struct iwl_lq_sta *lq_sta)
2465 struct iwl_scale_tbl_info *tbl;
2466 int rate_idx;
2467 int i;
2468 u32 rate;
2469 u8 use_green = rs_use_green(sta, &priv->current_ht_config);
2470 u8 active_tbl = 0;
2471 u8 valid_tx_ant;
2473 if (!sta || !lq_sta)
2474 goto out;
2476 i = lq_sta->last_txrate_idx;
2478 if ((lq_sta->lq.sta_id == 0xff) &&
2479 (priv->iw_mode == NL80211_IFTYPE_ADHOC))
2480 goto out;
2482 valid_tx_ant = priv->hw_params.valid_tx_ant;
2484 if (!lq_sta->search_better_tbl)
2485 active_tbl = lq_sta->active_tbl;
2486 else
2487 active_tbl = 1 - lq_sta->active_tbl;
2489 tbl = &(lq_sta->lq_info[active_tbl]);
2491 if ((i < 0) || (i >= IWL_RATE_COUNT))
2492 i = 0;
2494 rate = iwl_rates[i].plcp;
2495 tbl->ant_type = first_antenna(valid_tx_ant);
2496 rate |= tbl->ant_type << RATE_MCS_ANT_POS;
2498 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
2499 rate |= RATE_MCS_CCK_MSK;
2501 rs_get_tbl_info_from_mcs(rate, priv->band, tbl, &rate_idx);
2502 if (!rs_is_valid_ant(valid_tx_ant, tbl->ant_type))
2503 rs_toggle_antenna(valid_tx_ant, &rate, tbl);
2505 rate = rate_n_flags_from_tbl(priv, tbl, rate_idx, use_green);
2506 tbl->current_rate = rate;
2507 rs_set_expected_tpt_table(lq_sta, tbl);
2508 rs_fill_link_cmd(NULL, lq_sta, rate);
2509 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
2510 out:
2511 return;
2514 static void rs_get_rate(void *priv_r, struct ieee80211_sta *sta, void *priv_sta,
2515 struct ieee80211_tx_rate_control *txrc)
2518 struct sk_buff *skb = txrc->skb;
2519 struct ieee80211_supported_band *sband = txrc->sband;
2520 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
2521 struct ieee80211_conf *conf = &priv->hw->conf;
2522 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
2523 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2524 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2525 struct iwl_lq_sta *lq_sta = priv_sta;
2526 int rate_idx;
2528 IWL_DEBUG_RATE_LIMIT(priv, "rate scale calculate new rate for skb\n");
2530 /* Get max rate if user set max rate */
2531 if (lq_sta) {
2532 lq_sta->max_rate_idx = txrc->max_rate_idx;
2533 if ((sband->band == IEEE80211_BAND_5GHZ) &&
2534 (lq_sta->max_rate_idx != -1))
2535 lq_sta->max_rate_idx += IWL_FIRST_OFDM_RATE;
2536 if ((lq_sta->max_rate_idx < 0) ||
2537 (lq_sta->max_rate_idx >= IWL_RATE_COUNT))
2538 lq_sta->max_rate_idx = -1;
2541 /* Send management frames and NO_ACK data using lowest rate. */
2542 if (rate_control_send_low(sta, priv_sta, txrc))
2543 return;
2545 rate_idx = lq_sta->last_txrate_idx;
2547 if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) &&
2548 !lq_sta->ibss_sta_added) {
2549 u8 sta_id = iwl_find_station(priv, hdr->addr1);
2551 if (sta_id == IWL_INVALID_STATION) {
2552 IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n",
2553 hdr->addr1);
2554 sta_id = iwl_add_station(priv, hdr->addr1,
2555 false, CMD_ASYNC, ht_cap);
2557 if ((sta_id != IWL_INVALID_STATION)) {
2558 lq_sta->lq.sta_id = sta_id;
2559 lq_sta->lq.rs_table[0].rate_n_flags = 0;
2560 lq_sta->ibss_sta_added = 1;
2561 rs_initialize_lq(priv, conf, sta, lq_sta);
2565 if (lq_sta->last_rate_n_flags & RATE_MCS_HT_MSK) {
2566 rate_idx -= IWL_FIRST_OFDM_RATE;
2567 /* 6M and 9M shared same MCS index */
2568 rate_idx = (rate_idx > 0) ? (rate_idx - 1) : 0;
2569 if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
2570 IWL_RATE_MIMO3_6M_PLCP)
2571 rate_idx = rate_idx + (2 * MCS_INDEX_PER_STREAM);
2572 else if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
2573 IWL_RATE_MIMO2_6M_PLCP)
2574 rate_idx = rate_idx + MCS_INDEX_PER_STREAM;
2575 info->control.rates[0].flags = IEEE80211_TX_RC_MCS;
2576 if (lq_sta->last_rate_n_flags & RATE_MCS_SGI_MSK)
2577 info->control.rates[0].flags |= IEEE80211_TX_RC_SHORT_GI;
2578 if (lq_sta->last_rate_n_flags & RATE_MCS_DUP_MSK)
2579 info->control.rates[0].flags |= IEEE80211_TX_RC_DUP_DATA;
2580 if (lq_sta->last_rate_n_flags & RATE_MCS_HT40_MSK)
2581 info->control.rates[0].flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
2582 if (lq_sta->last_rate_n_flags & RATE_MCS_GF_MSK)
2583 info->control.rates[0].flags |= IEEE80211_TX_RC_GREEN_FIELD;
2584 } else {
2585 /* Check for invalid rates */
2586 if ((rate_idx < 0) || (rate_idx >= IWL_RATE_COUNT_LEGACY) ||
2587 ((sband->band == IEEE80211_BAND_5GHZ) &&
2588 (rate_idx < IWL_FIRST_OFDM_RATE)))
2589 rate_idx = rate_lowest_index(sband, sta);
2590 /* On valid 5 GHz rate, adjust index */
2591 else if (sband->band == IEEE80211_BAND_5GHZ)
2592 rate_idx -= IWL_FIRST_OFDM_RATE;
2593 info->control.rates[0].flags = 0;
2595 info->control.rates[0].idx = rate_idx;
2599 static void *rs_alloc_sta(void *priv_rate, struct ieee80211_sta *sta,
2600 gfp_t gfp)
2602 struct iwl_lq_sta *lq_sta;
2603 struct iwl_priv *priv;
2604 int i, j;
2606 priv = (struct iwl_priv *)priv_rate;
2607 IWL_DEBUG_RATE(priv, "create station rate scale window\n");
2609 lq_sta = kzalloc(sizeof(struct iwl_lq_sta), gfp);
2611 if (lq_sta == NULL)
2612 return NULL;
2613 lq_sta->lq.sta_id = 0xff;
2616 for (j = 0; j < LQ_SIZE; j++)
2617 for (i = 0; i < IWL_RATE_COUNT; i++)
2618 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
2620 return lq_sta;
2623 static void rs_rate_init(void *priv_r, struct ieee80211_supported_band *sband,
2624 struct ieee80211_sta *sta, void *priv_sta)
2626 int i, j;
2627 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
2628 struct ieee80211_conf *conf = &priv->hw->conf;
2629 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
2630 struct iwl_lq_sta *lq_sta = priv_sta;
2632 lq_sta->flush_timer = 0;
2633 lq_sta->supp_rates = sta->supp_rates[sband->band];
2634 for (j = 0; j < LQ_SIZE; j++)
2635 for (i = 0; i < IWL_RATE_COUNT; i++)
2636 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
2638 IWL_DEBUG_RATE(priv, "LQ: *** rate scale station global init ***\n");
2639 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2640 * the lowest or the highest rate.. Could consider using RSSI from
2641 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2642 * after assoc.. */
2644 lq_sta->ibss_sta_added = 0;
2645 if (priv->iw_mode == NL80211_IFTYPE_AP) {
2646 u8 sta_id = iwl_find_station(priv,
2647 sta->addr);
2649 /* for IBSS the call are from tasklet */
2650 IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n", sta->addr);
2652 if (sta_id == IWL_INVALID_STATION) {
2653 IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n", sta->addr);
2654 sta_id = iwl_add_station(priv, sta->addr, false,
2655 CMD_ASYNC, ht_cap);
2657 if ((sta_id != IWL_INVALID_STATION)) {
2658 lq_sta->lq.sta_id = sta_id;
2659 lq_sta->lq.rs_table[0].rate_n_flags = 0;
2661 /* FIXME: this is w/a remove it later */
2662 priv->assoc_station_added = 1;
2665 lq_sta->is_dup = 0;
2666 lq_sta->max_rate_idx = -1;
2667 lq_sta->missed_rate_counter = IWL_MISSED_RATE_MAX;
2668 lq_sta->is_green = rs_use_green(sta, &priv->current_ht_config);
2669 lq_sta->active_legacy_rate = priv->active_rate & ~(0x1000);
2670 lq_sta->active_rate_basic = priv->active_rate_basic;
2671 lq_sta->band = priv->band;
2673 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2674 * supp_rates[] does not; shift to convert format, force 9 MBits off.
2676 lq_sta->active_siso_rate = ht_cap->mcs.rx_mask[0] << 1;
2677 lq_sta->active_siso_rate |= ht_cap->mcs.rx_mask[0] & 0x1;
2678 lq_sta->active_siso_rate &= ~((u16)0x2);
2679 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
2681 /* Same here */
2682 lq_sta->active_mimo2_rate = ht_cap->mcs.rx_mask[1] << 1;
2683 lq_sta->active_mimo2_rate |= ht_cap->mcs.rx_mask[1] & 0x1;
2684 lq_sta->active_mimo2_rate &= ~((u16)0x2);
2685 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2687 lq_sta->active_mimo3_rate = ht_cap->mcs.rx_mask[2] << 1;
2688 lq_sta->active_mimo3_rate |= ht_cap->mcs.rx_mask[2] & 0x1;
2689 lq_sta->active_mimo3_rate &= ~((u16)0x2);
2690 lq_sta->active_mimo3_rate <<= IWL_FIRST_OFDM_RATE;
2692 IWL_DEBUG_RATE(priv, "SISO-RATE=%X MIMO2-RATE=%X MIMO3-RATE=%X\n",
2693 lq_sta->active_siso_rate,
2694 lq_sta->active_mimo2_rate,
2695 lq_sta->active_mimo3_rate);
2697 /* These values will be overridden later */
2698 lq_sta->lq.general_params.single_stream_ant_msk = ANT_A;
2699 lq_sta->lq.general_params.dual_stream_ant_msk = ANT_AB;
2701 /* as default allow aggregation for all tids */
2702 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
2703 lq_sta->drv = priv;
2705 /* Set last_txrate_idx to lowest rate */
2706 lq_sta->last_txrate_idx = rate_lowest_index(sband, sta);
2707 if (sband->band == IEEE80211_BAND_5GHZ)
2708 lq_sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
2710 rs_initialize_lq(priv, conf, sta, lq_sta);
2713 static void rs_fill_link_cmd(struct iwl_priv *priv,
2714 struct iwl_lq_sta *lq_sta, u32 new_rate)
2716 struct iwl_scale_tbl_info tbl_type;
2717 int index = 0;
2718 int rate_idx;
2719 int repeat_rate = 0;
2720 u8 ant_toggle_cnt = 0;
2721 u8 use_ht_possible = 1;
2722 u8 valid_tx_ant = 0;
2723 struct iwl_link_quality_cmd *lq_cmd = &lq_sta->lq;
2725 /* Override starting rate (index 0) if needed for debug purposes */
2726 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
2728 /* Interpret new_rate (rate_n_flags) */
2729 memset(&tbl_type, 0, sizeof(tbl_type));
2730 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
2731 &tbl_type, &rate_idx);
2733 /* How many times should we repeat the initial rate? */
2734 if (is_legacy(tbl_type.lq_type)) {
2735 ant_toggle_cnt = 1;
2736 repeat_rate = IWL_NUMBER_TRY;
2737 } else {
2738 repeat_rate = IWL_HT_NUMBER_TRY;
2741 lq_cmd->general_params.mimo_delimiter =
2742 is_mimo(tbl_type.lq_type) ? 1 : 0;
2744 /* Fill 1st table entry (index 0) */
2745 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
2747 if (num_of_ant(tbl_type.ant_type) == 1) {
2748 lq_cmd->general_params.single_stream_ant_msk =
2749 tbl_type.ant_type;
2750 } else if (num_of_ant(tbl_type.ant_type) == 2) {
2751 lq_cmd->general_params.dual_stream_ant_msk =
2752 tbl_type.ant_type;
2753 } /* otherwise we don't modify the existing value */
2755 index++;
2756 repeat_rate--;
2758 if (priv)
2759 valid_tx_ant = priv->hw_params.valid_tx_ant;
2761 /* Fill rest of rate table */
2762 while (index < LINK_QUAL_MAX_RETRY_NUM) {
2763 /* Repeat initial/next rate.
2764 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2765 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
2766 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
2767 if (is_legacy(tbl_type.lq_type)) {
2768 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2769 ant_toggle_cnt++;
2770 else if (priv &&
2771 rs_toggle_antenna(valid_tx_ant,
2772 &new_rate, &tbl_type))
2773 ant_toggle_cnt = 1;
2776 /* Override next rate if needed for debug purposes */
2777 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
2779 /* Fill next table entry */
2780 lq_cmd->rs_table[index].rate_n_flags =
2781 cpu_to_le32(new_rate);
2782 repeat_rate--;
2783 index++;
2786 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band, &tbl_type,
2787 &rate_idx);
2789 /* Indicate to uCode which entries might be MIMO.
2790 * If initial rate was MIMO, this will finally end up
2791 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
2792 if (is_mimo(tbl_type.lq_type))
2793 lq_cmd->general_params.mimo_delimiter = index;
2795 /* Get next rate */
2796 new_rate = rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
2797 use_ht_possible);
2799 /* How many times should we repeat the next rate? */
2800 if (is_legacy(tbl_type.lq_type)) {
2801 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2802 ant_toggle_cnt++;
2803 else if (priv &&
2804 rs_toggle_antenna(valid_tx_ant,
2805 &new_rate, &tbl_type))
2806 ant_toggle_cnt = 1;
2808 repeat_rate = IWL_NUMBER_TRY;
2809 } else {
2810 repeat_rate = IWL_HT_NUMBER_TRY;
2813 /* Don't allow HT rates after next pass.
2814 * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
2815 use_ht_possible = 0;
2817 /* Override next rate if needed for debug purposes */
2818 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
2820 /* Fill next table entry */
2821 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
2823 index++;
2824 repeat_rate--;
2827 lq_cmd->agg_params.agg_frame_cnt_limit = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
2828 lq_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
2829 lq_cmd->agg_params.agg_time_limit =
2830 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
2833 static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
2835 return hw->priv;
2837 /* rate scale requires free function to be implemented */
2838 static void rs_free(void *priv_rate)
2840 return;
2843 static void rs_free_sta(void *priv_r, struct ieee80211_sta *sta,
2844 void *priv_sta)
2846 struct iwl_lq_sta *lq_sta = priv_sta;
2847 struct iwl_priv *priv __maybe_unused = priv_r;
2849 IWL_DEBUG_RATE(priv, "enter\n");
2850 kfree(lq_sta);
2851 IWL_DEBUG_RATE(priv, "leave\n");
2855 #ifdef CONFIG_MAC80211_DEBUGFS
2856 static int open_file_generic(struct inode *inode, struct file *file)
2858 file->private_data = inode->i_private;
2859 return 0;
2861 static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
2862 u32 *rate_n_flags, int index)
2864 struct iwl_priv *priv;
2865 u8 valid_tx_ant;
2866 u8 ant_sel_tx;
2868 priv = lq_sta->drv;
2869 valid_tx_ant = priv->hw_params.valid_tx_ant;
2870 if (lq_sta->dbg_fixed_rate) {
2871 ant_sel_tx =
2872 ((lq_sta->dbg_fixed_rate & RATE_MCS_ANT_ABC_MSK)
2873 >> RATE_MCS_ANT_POS);
2874 if ((valid_tx_ant & ant_sel_tx) == ant_sel_tx) {
2875 *rate_n_flags = lq_sta->dbg_fixed_rate;
2876 IWL_DEBUG_RATE(priv, "Fixed rate ON\n");
2877 } else {
2878 lq_sta->dbg_fixed_rate = 0;
2879 IWL_ERR(priv,
2880 "Invalid antenna selection 0x%X, Valid is 0x%X\n",
2881 ant_sel_tx, valid_tx_ant);
2882 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
2884 } else {
2885 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
2889 static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
2890 const char __user *user_buf, size_t count, loff_t *ppos)
2892 struct iwl_lq_sta *lq_sta = file->private_data;
2893 struct iwl_priv *priv;
2894 char buf[64];
2895 int buf_size;
2896 u32 parsed_rate;
2898 priv = lq_sta->drv;
2899 memset(buf, 0, sizeof(buf));
2900 buf_size = min(count, sizeof(buf) - 1);
2901 if (copy_from_user(buf, user_buf, buf_size))
2902 return -EFAULT;
2904 if (sscanf(buf, "%x", &parsed_rate) == 1)
2905 lq_sta->dbg_fixed_rate = parsed_rate;
2906 else
2907 lq_sta->dbg_fixed_rate = 0;
2909 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
2910 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2911 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2912 lq_sta->active_mimo3_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2914 IWL_DEBUG_RATE(priv, "sta_id %d rate 0x%X\n",
2915 lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
2917 if (lq_sta->dbg_fixed_rate) {
2918 rs_fill_link_cmd(NULL, lq_sta, lq_sta->dbg_fixed_rate);
2919 iwl_send_lq_cmd(lq_sta->drv, &lq_sta->lq, CMD_ASYNC);
2922 return count;
2925 static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
2926 char __user *user_buf, size_t count, loff_t *ppos)
2928 char *buff;
2929 int desc = 0;
2930 int i = 0;
2931 int index = 0;
2932 ssize_t ret;
2934 struct iwl_lq_sta *lq_sta = file->private_data;
2935 struct iwl_priv *priv;
2936 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
2938 priv = lq_sta->drv;
2939 buff = kmalloc(1024, GFP_KERNEL);
2940 if (!buff)
2941 return -ENOMEM;
2943 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
2944 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
2945 lq_sta->total_failed, lq_sta->total_success,
2946 lq_sta->active_legacy_rate);
2947 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
2948 lq_sta->dbg_fixed_rate);
2949 desc += sprintf(buff+desc, "valid_tx_ant %s%s%s\n",
2950 (priv->hw_params.valid_tx_ant & ANT_A) ? "ANT_A," : "",
2951 (priv->hw_params.valid_tx_ant & ANT_B) ? "ANT_B," : "",
2952 (priv->hw_params.valid_tx_ant & ANT_C) ? "ANT_C" : "");
2953 desc += sprintf(buff+desc, "lq type %s\n",
2954 (is_legacy(tbl->lq_type)) ? "legacy" : "HT");
2955 if (is_Ht(tbl->lq_type)) {
2956 desc += sprintf(buff+desc, " %s",
2957 (is_siso(tbl->lq_type)) ? "SISO" :
2958 ((is_mimo2(tbl->lq_type)) ? "MIMO2" : "MIMO3"));
2959 desc += sprintf(buff+desc, " %s",
2960 (tbl->is_ht40) ? "40MHz" : "20MHz");
2961 desc += sprintf(buff+desc, " %s %s\n", (tbl->is_SGI) ? "SGI" : "",
2962 (lq_sta->is_green) ? "GF enabled" : "");
2964 desc += sprintf(buff+desc, "last tx rate=0x%X\n",
2965 lq_sta->last_rate_n_flags);
2966 desc += sprintf(buff+desc, "general:"
2967 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
2968 lq_sta->lq.general_params.flags,
2969 lq_sta->lq.general_params.mimo_delimiter,
2970 lq_sta->lq.general_params.single_stream_ant_msk,
2971 lq_sta->lq.general_params.dual_stream_ant_msk);
2973 desc += sprintf(buff+desc, "agg:"
2974 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
2975 le16_to_cpu(lq_sta->lq.agg_params.agg_time_limit),
2976 lq_sta->lq.agg_params.agg_dis_start_th,
2977 lq_sta->lq.agg_params.agg_frame_cnt_limit);
2979 desc += sprintf(buff+desc,
2980 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
2981 lq_sta->lq.general_params.start_rate_index[0],
2982 lq_sta->lq.general_params.start_rate_index[1],
2983 lq_sta->lq.general_params.start_rate_index[2],
2984 lq_sta->lq.general_params.start_rate_index[3]);
2986 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
2987 index = iwl_hwrate_to_plcp_idx(
2988 le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags));
2989 if (is_legacy(tbl->lq_type)) {
2990 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps\n",
2991 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
2992 iwl_rate_mcs[index].mbps);
2993 } else {
2994 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps (%s)\n",
2995 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
2996 iwl_rate_mcs[index].mbps, iwl_rate_mcs[index].mcs);
3000 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3001 kfree(buff);
3002 return ret;
3005 static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
3006 .write = rs_sta_dbgfs_scale_table_write,
3007 .read = rs_sta_dbgfs_scale_table_read,
3008 .open = open_file_generic,
3010 static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
3011 char __user *user_buf, size_t count, loff_t *ppos)
3013 char *buff;
3014 int desc = 0;
3015 int i, j;
3016 ssize_t ret;
3018 struct iwl_lq_sta *lq_sta = file->private_data;
3020 buff = kmalloc(1024, GFP_KERNEL);
3021 if (!buff)
3022 return -ENOMEM;
3024 for (i = 0; i < LQ_SIZE; i++) {
3025 desc += sprintf(buff+desc,
3026 "%s type=%d SGI=%d HT40=%d DUP=%d GF=%d\n"
3027 "rate=0x%X\n",
3028 lq_sta->active_tbl == i ? "*" : "x",
3029 lq_sta->lq_info[i].lq_type,
3030 lq_sta->lq_info[i].is_SGI,
3031 lq_sta->lq_info[i].is_ht40,
3032 lq_sta->lq_info[i].is_dup,
3033 lq_sta->is_green,
3034 lq_sta->lq_info[i].current_rate);
3035 for (j = 0; j < IWL_RATE_COUNT; j++) {
3036 desc += sprintf(buff+desc,
3037 "counter=%d success=%d %%=%d\n",
3038 lq_sta->lq_info[i].win[j].counter,
3039 lq_sta->lq_info[i].win[j].success_counter,
3040 lq_sta->lq_info[i].win[j].success_ratio);
3043 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3044 kfree(buff);
3045 return ret;
3048 static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
3049 .read = rs_sta_dbgfs_stats_table_read,
3050 .open = open_file_generic,
3053 static ssize_t rs_sta_dbgfs_rate_scale_data_read(struct file *file,
3054 char __user *user_buf, size_t count, loff_t *ppos)
3056 char buff[120];
3057 int desc = 0;
3058 ssize_t ret;
3060 struct iwl_lq_sta *lq_sta = file->private_data;
3061 struct iwl_priv *priv;
3062 struct iwl_scale_tbl_info *tbl = &lq_sta->lq_info[lq_sta->active_tbl];
3064 priv = lq_sta->drv;
3066 if (is_Ht(tbl->lq_type))
3067 desc += sprintf(buff+desc,
3068 "Bit Rate= %d Mb/s\n",
3069 tbl->expected_tpt[lq_sta->last_txrate_idx]);
3070 else
3071 desc += sprintf(buff+desc,
3072 "Bit Rate= %d Mb/s\n",
3073 iwl_rates[lq_sta->last_txrate_idx].ieee >> 1);
3074 desc += sprintf(buff+desc,
3075 "Signal Level= %d dBm\tNoise Level= %d dBm\n",
3076 priv->last_rx_rssi, priv->last_rx_noise);
3077 desc += sprintf(buff+desc,
3078 "Tsf= 0x%llx\tBeacon time= 0x%08X\n",
3079 priv->last_tsf, priv->last_beacon_time);
3081 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3082 return ret;
3085 static const struct file_operations rs_sta_dbgfs_rate_scale_data_ops = {
3086 .read = rs_sta_dbgfs_rate_scale_data_read,
3087 .open = open_file_generic,
3090 static void rs_add_debugfs(void *priv, void *priv_sta,
3091 struct dentry *dir)
3093 struct iwl_lq_sta *lq_sta = priv_sta;
3094 lq_sta->rs_sta_dbgfs_scale_table_file =
3095 debugfs_create_file("rate_scale_table", 0600, dir,
3096 lq_sta, &rs_sta_dbgfs_scale_table_ops);
3097 lq_sta->rs_sta_dbgfs_stats_table_file =
3098 debugfs_create_file("rate_stats_table", 0600, dir,
3099 lq_sta, &rs_sta_dbgfs_stats_table_ops);
3100 lq_sta->rs_sta_dbgfs_rate_scale_data_file =
3101 debugfs_create_file("rate_scale_data", 0600, dir,
3102 lq_sta, &rs_sta_dbgfs_rate_scale_data_ops);
3103 lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
3104 debugfs_create_u8("tx_agg_tid_enable", 0600, dir,
3105 &lq_sta->tx_agg_tid_en);
3109 static void rs_remove_debugfs(void *priv, void *priv_sta)
3111 struct iwl_lq_sta *lq_sta = priv_sta;
3112 debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
3113 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
3114 debugfs_remove(lq_sta->rs_sta_dbgfs_rate_scale_data_file);
3115 debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
3117 #endif
3119 static struct rate_control_ops rs_ops = {
3120 .module = NULL,
3121 .name = RS_NAME,
3122 .tx_status = rs_tx_status,
3123 .get_rate = rs_get_rate,
3124 .rate_init = rs_rate_init,
3125 .alloc = rs_alloc,
3126 .free = rs_free,
3127 .alloc_sta = rs_alloc_sta,
3128 .free_sta = rs_free_sta,
3129 #ifdef CONFIG_MAC80211_DEBUGFS
3130 .add_sta_debugfs = rs_add_debugfs,
3131 .remove_sta_debugfs = rs_remove_debugfs,
3132 #endif
3135 int iwlagn_rate_control_register(void)
3137 return ieee80211_rate_control_register(&rs_ops);
3140 void iwlagn_rate_control_unregister(void)
3142 ieee80211_rate_control_unregister(&rs_ops);