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
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>
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,
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,
71 /*ANT_AB -> */ ANT_BC,
73 /*ANT_AC -> */ ANT_AB,
74 /*ANT_BC -> */ ANT_AC,
75 /*ANT_ABC -> */ ANT_ABC,
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 */
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;
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
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.
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 */
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 */
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 */
144 enum ieee80211_band band;
147 /* The following are bitmaps of rates; IWL_RATE_6M_MASK, etc. */
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];
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;
168 struct iwl_priv *drv;
170 /* used to be in sta_info */
172 /* last tx rate_n_flags */
173 u32 last_rate_n_flags;
176 static void rs_rate_scale_perform(struct iwl_priv *priv,
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);
188 static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
189 u32 *rate_n_flags, int index)
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
257 const static struct iwl_rate_mcs_info iwl_rate_mcs[IWL_RATE_COUNT] = {
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)
283 window->success_counter = 0;
284 window->success_ratio = IWL_INVALID_VALUE;
286 window->average_tpt = IWL_INVALID_VALUE;
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;
311 if (tl->head >= TID_QUEUE_MAX_SIZE)
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);
326 struct iwl_traffic_load *tl = NULL;
329 if (ieee80211_is_data_qos(hdr->frame_control)) {
330 u8 *qc = ieee80211_get_qos_ctl(hdr);
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)) {
345 tl->time_stamp = curr_time;
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;
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);
378 struct iwl_traffic_load *tl = NULL;
380 if (tid >= TID_MAX_LOAD_COUNT)
383 tl = &(lq_data->load[tid]);
385 curr_time -= curr_time % TID_ROUND_VALUE;
387 if (!(tl->queue_count))
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);
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 if (rs_tl_get_load(lq_data, tid) > IWL_AGG_LOAD_THRESHOLD) {
406 IWL_DEBUG_HT(priv, "Starting Tx agg: STA: %pM tid: %d\n",
408 ieee80211_start_tx_ba_session(priv->hw, sta->addr, tid);
412 static void rs_tl_turn_on_agg(struct iwl_priv *priv, u8 tid,
413 struct iwl_lq_sta *lq_data,
414 struct ieee80211_sta *sta)
416 if ((tid < TID_MAX_LOAD_COUNT))
417 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
418 else if (tid == IWL_AGG_ALL_TID)
419 for (tid = 0; tid < TID_MAX_LOAD_COUNT; tid++)
420 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
423 static inline int get_num_of_ant_from_rate(u32 rate_n_flags)
425 return !!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
426 !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
427 !!(rate_n_flags & RATE_MCS_ANT_C_MSK);
431 * rs_collect_tx_data - Update the success/failure sliding window
433 * We keep a sliding window of the last 62 packets transmitted
434 * at this rate. window->data contains the bitmask of successful
437 static int rs_collect_tx_data(struct iwl_rate_scale_data *windows,
438 int scale_index, s32 tpt, int retries,
441 struct iwl_rate_scale_data *window = NULL;
442 static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
445 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
448 /* Select data for current tx bit rate */
449 window = &(windows[scale_index]);
452 * Keep track of only the latest 62 tx frame attempts in this rate's
453 * history window; anything older isn't really relevant any more.
454 * If we have filled up the sliding window, drop the oldest attempt;
455 * if the oldest attempt (highest bit in bitmap) shows "success",
456 * subtract "1" from the success counter (this is the main reason
457 * we keep these bitmaps!).
459 while (retries > 0) {
460 if (window->counter >= IWL_RATE_MAX_WINDOW) {
462 /* remove earliest */
463 window->counter = IWL_RATE_MAX_WINDOW - 1;
465 if (window->data & mask) {
466 window->data &= ~mask;
467 window->success_counter--;
471 /* Increment frames-attempted counter */
474 /* Shift bitmap by one frame (throw away oldest history),
475 * OR in "1", and increment "success" if this
476 * frame was successful. */
479 window->success_counter++;
487 /* Calculate current success ratio, avoid divide-by-0! */
488 if (window->counter > 0)
489 window->success_ratio = 128 * (100 * window->success_counter)
492 window->success_ratio = IWL_INVALID_VALUE;
494 fail_count = window->counter - window->success_counter;
496 /* Calculate average throughput, if we have enough history. */
497 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
498 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
499 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
501 window->average_tpt = IWL_INVALID_VALUE;
503 /* Tag this window as having been updated */
504 window->stamp = jiffies;
510 * Fill uCode API rate_n_flags field, based on "search" or "active" table.
512 /* FIXME:RS:remove this function and put the flags statically in the table */
513 static u32 rate_n_flags_from_tbl(struct iwl_priv *priv,
514 struct iwl_scale_tbl_info *tbl,
515 int index, u8 use_green)
517 u32 rate_n_flags = 0;
519 if (is_legacy(tbl->lq_type)) {
520 rate_n_flags = iwl_rates[index].plcp;
521 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
522 rate_n_flags |= RATE_MCS_CCK_MSK;
524 } else if (is_Ht(tbl->lq_type)) {
525 if (index > IWL_LAST_OFDM_RATE) {
526 IWL_ERR(priv, "Invalid HT rate index %d\n", index);
527 index = IWL_LAST_OFDM_RATE;
529 rate_n_flags = RATE_MCS_HT_MSK;
531 if (is_siso(tbl->lq_type))
532 rate_n_flags |= iwl_rates[index].plcp_siso;
533 else if (is_mimo2(tbl->lq_type))
534 rate_n_flags |= iwl_rates[index].plcp_mimo2;
536 rate_n_flags |= iwl_rates[index].plcp_mimo3;
538 IWL_ERR(priv, "Invalid tbl->lq_type %d\n", tbl->lq_type);
541 rate_n_flags |= ((tbl->ant_type << RATE_MCS_ANT_POS) &
542 RATE_MCS_ANT_ABC_MSK);
544 if (is_Ht(tbl->lq_type)) {
547 rate_n_flags |= RATE_MCS_DUP_MSK;
549 rate_n_flags |= RATE_MCS_HT40_MSK;
552 rate_n_flags |= RATE_MCS_SGI_MSK;
555 rate_n_flags |= RATE_MCS_GF_MSK;
556 if (is_siso(tbl->lq_type) && tbl->is_SGI) {
557 rate_n_flags &= ~RATE_MCS_SGI_MSK;
558 IWL_ERR(priv, "GF was set with SGI:SISO\n");
566 * Interpret uCode API's rate_n_flags format,
567 * fill "search" or "active" tx mode table.
569 static int rs_get_tbl_info_from_mcs(const u32 rate_n_flags,
570 enum ieee80211_band band,
571 struct iwl_scale_tbl_info *tbl,
574 u32 ant_msk = (rate_n_flags & RATE_MCS_ANT_ABC_MSK);
575 u8 num_of_ant = get_num_of_ant_from_rate(rate_n_flags);
578 *rate_idx = iwl_hwrate_to_plcp_idx(rate_n_flags);
580 if (*rate_idx == IWL_RATE_INVALID) {
584 tbl->is_SGI = 0; /* default legacy setup */
587 tbl->ant_type = (ant_msk >> RATE_MCS_ANT_POS);
588 tbl->lq_type = LQ_NONE;
589 tbl->max_search = IWL_MAX_SEARCH;
591 /* legacy rate format */
592 if (!(rate_n_flags & RATE_MCS_HT_MSK)) {
593 if (num_of_ant == 1) {
594 if (band == IEEE80211_BAND_5GHZ)
601 if (rate_n_flags & RATE_MCS_SGI_MSK)
604 if ((rate_n_flags & RATE_MCS_HT40_MSK) ||
605 (rate_n_flags & RATE_MCS_DUP_MSK))
608 if (rate_n_flags & RATE_MCS_DUP_MSK)
611 mcs = rs_extract_rate(rate_n_flags);
614 if (mcs <= IWL_RATE_SISO_60M_PLCP) {
616 tbl->lq_type = LQ_SISO; /*else NONE*/
618 } else if (mcs <= IWL_RATE_MIMO2_60M_PLCP) {
620 tbl->lq_type = LQ_MIMO2;
623 if (num_of_ant == 3) {
624 tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
625 tbl->lq_type = LQ_MIMO3;
632 /* switch to another antenna/antennas and return 1 */
633 /* if no other valid antenna found, return 0 */
634 static int rs_toggle_antenna(u32 valid_ant, u32 *rate_n_flags,
635 struct iwl_scale_tbl_info *tbl)
639 if (!tbl->ant_type || tbl->ant_type > ANT_ABC)
642 if (!rs_is_valid_ant(valid_ant, tbl->ant_type))
645 new_ant_type = ant_toggle_lookup[tbl->ant_type];
647 while ((new_ant_type != tbl->ant_type) &&
648 !rs_is_valid_ant(valid_ant, new_ant_type))
649 new_ant_type = ant_toggle_lookup[new_ant_type];
651 if (new_ant_type == tbl->ant_type)
654 tbl->ant_type = new_ant_type;
655 *rate_n_flags &= ~RATE_MCS_ANT_ABC_MSK;
656 *rate_n_flags |= new_ant_type << RATE_MCS_ANT_POS;
660 /* in 4965 we don't use greenfield at all */
661 static inline u8 rs_use_green(struct iwl_priv *priv,
662 struct ieee80211_conf *conf)
666 if ((priv->hw_rev & CSR_HW_REV_TYPE_MSK) == CSR_HW_REV_TYPE_4965)
669 is_green = (conf_is_ht(conf) &&
670 priv->current_ht_config.is_green_field &&
671 !priv->current_ht_config.non_GF_STA_present);
676 * rs_get_supported_rates - get the available rates
678 * if management frame or broadcast frame only return
679 * basic available rates.
682 static u16 rs_get_supported_rates(struct iwl_lq_sta *lq_sta,
683 struct ieee80211_hdr *hdr,
684 enum iwl_table_type rate_type)
686 if (hdr && is_multicast_ether_addr(hdr->addr1) &&
687 lq_sta->active_rate_basic)
688 return lq_sta->active_rate_basic;
690 if (is_legacy(rate_type)) {
691 return lq_sta->active_legacy_rate;
693 if (is_siso(rate_type))
694 return lq_sta->active_siso_rate;
695 else if (is_mimo2(rate_type))
696 return lq_sta->active_mimo2_rate;
698 return lq_sta->active_mimo3_rate;
702 static u16 rs_get_adjacent_rate(struct iwl_priv *priv, u8 index, u16 rate_mask,
705 u8 high = IWL_RATE_INVALID;
706 u8 low = IWL_RATE_INVALID;
708 /* 802.11A or ht walks to the next literal adjacent rate in
710 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
714 /* Find the previous rate that is in the rate mask */
716 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
717 if (rate_mask & mask) {
723 /* Find the next rate that is in the rate mask */
725 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
726 if (rate_mask & mask) {
732 return (high << 8) | low;
736 while (low != IWL_RATE_INVALID) {
737 low = iwl_rates[low].prev_rs;
738 if (low == IWL_RATE_INVALID)
740 if (rate_mask & (1 << low))
742 IWL_DEBUG_RATE(priv, "Skipping masked lower rate: %d\n", low);
746 while (high != IWL_RATE_INVALID) {
747 high = iwl_rates[high].next_rs;
748 if (high == IWL_RATE_INVALID)
750 if (rate_mask & (1 << high))
752 IWL_DEBUG_RATE(priv, "Skipping masked higher rate: %d\n", high);
755 return (high << 8) | low;
758 static u32 rs_get_lower_rate(struct iwl_lq_sta *lq_sta,
759 struct iwl_scale_tbl_info *tbl,
760 u8 scale_index, u8 ht_possible)
765 u8 switch_to_legacy = 0;
766 u8 is_green = lq_sta->is_green;
768 /* check if we need to switch from HT to legacy rates.
769 * assumption is that mandatory rates (1Mbps or 6Mbps)
770 * are always supported (spec demand) */
771 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
772 switch_to_legacy = 1;
773 scale_index = rs_ht_to_legacy[scale_index];
774 if (lq_sta->band == IEEE80211_BAND_5GHZ)
779 if (num_of_ant(tbl->ant_type) > 1)
780 tbl->ant_type = ANT_A;/*FIXME:RS*/
784 tbl->max_search = IWL_MAX_SEARCH;
787 rate_mask = rs_get_supported_rates(lq_sta, NULL, tbl->lq_type);
789 /* Mask with station rate restriction */
790 if (is_legacy(tbl->lq_type)) {
791 /* supp_rates has no CCK bits in A mode */
792 if (lq_sta->band == IEEE80211_BAND_5GHZ)
793 rate_mask = (u16)(rate_mask &
794 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
796 rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
799 /* If we switched from HT to legacy, check current rate */
800 if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
805 high_low = rs_get_adjacent_rate(lq_sta->drv, scale_index, rate_mask,
807 low = high_low & 0xff;
809 if (low == IWL_RATE_INVALID)
813 return rate_n_flags_from_tbl(lq_sta->drv, tbl, low, is_green);
817 * mac80211 sends us Tx status
819 static void rs_tx_status(void *priv_r, struct ieee80211_supported_band *sband,
820 struct ieee80211_sta *sta, void *priv_sta,
825 int rs_index, index = 0;
826 struct iwl_lq_sta *lq_sta = priv_sta;
827 struct iwl_link_quality_cmd *table;
828 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
829 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
830 struct ieee80211_hw *hw = priv->hw;
831 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
832 struct iwl_rate_scale_data *window = NULL;
833 struct iwl_rate_scale_data *search_win = NULL;
835 struct iwl_scale_tbl_info tbl_type;
836 struct iwl_scale_tbl_info *curr_tbl, *search_tbl;
840 IWL_DEBUG_RATE_LIMIT(priv, "get frame ack response, update rate scale window\n");
842 if (!ieee80211_is_data(hdr->frame_control) ||
843 info->flags & IEEE80211_TX_CTL_NO_ACK)
846 /* This packet was aggregated but doesn't carry rate scale info */
847 if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
848 !(info->flags & IEEE80211_TX_STAT_AMPDU))
851 if (info->flags & IEEE80211_TX_STAT_AMPDU)
854 retries = info->status.rates[0].count - 1;
859 if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) &&
860 !lq_sta->ibss_sta_added)
864 active_index = lq_sta->active_tbl;
866 curr_tbl = &(lq_sta->lq_info[active_index]);
867 search_tbl = &(lq_sta->lq_info[(1 - active_index)]);
868 window = (struct iwl_rate_scale_data *)&(curr_tbl->win[0]);
869 search_win = (struct iwl_rate_scale_data *)&(search_tbl->win[0]);
872 * Ignore this Tx frame response if its initial rate doesn't match
873 * that of latest Link Quality command. There may be stragglers
874 * from a previous Link Quality command, but we're no longer interested
875 * in those; they're either from the "active" mode while we're trying
876 * to check "search" mode, or a prior "search" mode after we've moved
877 * to a new "search" mode (which might become the new "active" mode).
879 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
880 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
881 if (priv->band == IEEE80211_BAND_5GHZ)
882 rs_index -= IWL_FIRST_OFDM_RATE;
884 if ((info->status.rates[0].idx < 0) ||
885 (tbl_type.is_SGI != !!(info->status.rates[0].flags & IEEE80211_TX_RC_SHORT_GI)) ||
886 (tbl_type.is_ht40 != !!(info->status.rates[0].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)) ||
887 (tbl_type.is_dup != !!(info->status.rates[0].flags & IEEE80211_TX_RC_DUP_DATA)) ||
888 (tbl_type.ant_type != info->antenna_sel_tx) ||
889 (!!(tx_rate & RATE_MCS_HT_MSK) != !!(info->status.rates[0].flags & IEEE80211_TX_RC_MCS)) ||
890 (!!(tx_rate & RATE_MCS_GF_MSK) != !!(info->status.rates[0].flags & IEEE80211_TX_RC_GREEN_FIELD)) ||
891 (hw->wiphy->bands[priv->band]->bitrates[rs_index].bitrate !=
892 hw->wiphy->bands[info->band]->bitrates[info->status.rates[0].idx].bitrate)) {
893 IWL_DEBUG_RATE(priv, "initial rate does not match 0x%x\n", tx_rate);
894 /* the last LQ command could failed so the LQ in ucode not
895 * the same in driver sync up
897 lq_sta->missed_rate_counter++;
898 if (lq_sta->missed_rate_counter > IWL_MISSED_RATE_MAX) {
899 lq_sta->missed_rate_counter = 0;
900 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
905 lq_sta->missed_rate_counter = 0;
906 /* Update frame history window with "failure" for each Tx retry. */
908 /* Look up the rate and other info used for each tx attempt.
909 * Each tx attempt steps one entry deeper in the rate table. */
910 tx_rate = le32_to_cpu(table->rs_table[index].rate_n_flags);
911 rs_get_tbl_info_from_mcs(tx_rate, priv->band,
912 &tbl_type, &rs_index);
914 /* If type matches "search" table,
915 * add failure to "search" history */
916 if ((tbl_type.lq_type == search_tbl->lq_type) &&
917 (tbl_type.ant_type == search_tbl->ant_type) &&
918 (tbl_type.is_SGI == search_tbl->is_SGI)) {
919 if (search_tbl->expected_tpt)
920 tpt = search_tbl->expected_tpt[rs_index];
923 rs_collect_tx_data(search_win, rs_index, tpt, 1, 0);
925 /* Else if type matches "current/active" table,
926 * add failure to "current/active" history */
927 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
928 (tbl_type.ant_type == curr_tbl->ant_type) &&
929 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
930 if (curr_tbl->expected_tpt)
931 tpt = curr_tbl->expected_tpt[rs_index];
934 rs_collect_tx_data(window, rs_index, tpt, 1, 0);
937 /* If not searching for a new mode, increment failed counter
938 * ... this helps determine when to start searching again */
939 if (lq_sta->stay_in_tbl)
940 lq_sta->total_failed++;
947 * Find (by rate) the history window to update with final Tx attempt;
948 * if Tx was successful first try, use original rate,
949 * else look up the rate that was, finally, successful.
951 tx_rate = le32_to_cpu(table->rs_table[index].rate_n_flags);
952 lq_sta->last_rate_n_flags = tx_rate;
953 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
955 /* Update frame history window with "success" if Tx got ACKed ... */
956 status = !!(info->flags & IEEE80211_TX_STAT_ACK);
958 /* If type matches "search" table,
959 * add final tx status to "search" history */
960 if ((tbl_type.lq_type == search_tbl->lq_type) &&
961 (tbl_type.ant_type == search_tbl->ant_type) &&
962 (tbl_type.is_SGI == search_tbl->is_SGI)) {
963 if (search_tbl->expected_tpt)
964 tpt = search_tbl->expected_tpt[rs_index];
967 if (info->flags & IEEE80211_TX_STAT_AMPDU)
968 rs_collect_tx_data(search_win, rs_index, tpt,
969 info->status.ampdu_ack_len,
970 info->status.ampdu_ack_map);
972 rs_collect_tx_data(search_win, rs_index, tpt,
974 /* Else if type matches "current/active" table,
975 * add final tx status to "current/active" history */
976 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
977 (tbl_type.ant_type == curr_tbl->ant_type) &&
978 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
979 if (curr_tbl->expected_tpt)
980 tpt = curr_tbl->expected_tpt[rs_index];
983 if (info->flags & IEEE80211_TX_STAT_AMPDU)
984 rs_collect_tx_data(window, rs_index, tpt,
985 info->status.ampdu_ack_len,
986 info->status.ampdu_ack_map);
988 rs_collect_tx_data(window, rs_index, tpt,
992 /* If not searching for new mode, increment success/failed counter
993 * ... these help determine when to start searching again */
994 if (lq_sta->stay_in_tbl) {
995 if (info->flags & IEEE80211_TX_STAT_AMPDU) {
996 lq_sta->total_success += info->status.ampdu_ack_map;
997 lq_sta->total_failed +=
998 (info->status.ampdu_ack_len - info->status.ampdu_ack_map);
1001 lq_sta->total_success++;
1003 lq_sta->total_failed++;
1007 /* See if there's a better rate or modulation mode to try. */
1008 if (sta && sta->supp_rates[sband->band])
1009 rs_rate_scale_perform(priv, skb, sta, lq_sta);
1015 * Begin a period of staying with a selected modulation mode.
1016 * Set "stay_in_tbl" flag to prevent any mode switches.
1017 * Set frame tx success limits according to legacy vs. high-throughput,
1018 * and reset overall (spanning all rates) tx success history statistics.
1019 * These control how long we stay using same modulation mode before
1020 * searching for a new mode.
1022 static void rs_set_stay_in_table(struct iwl_priv *priv, u8 is_legacy,
1023 struct iwl_lq_sta *lq_sta)
1025 IWL_DEBUG_RATE(priv, "we are staying in the same table\n");
1026 lq_sta->stay_in_tbl = 1; /* only place this gets set */
1028 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
1029 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
1030 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
1032 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
1033 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
1034 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
1036 lq_sta->table_count = 0;
1037 lq_sta->total_failed = 0;
1038 lq_sta->total_success = 0;
1039 lq_sta->flush_timer = jiffies;
1040 lq_sta->action_counter = 0;
1044 * Find correct throughput table for given mode of modulation
1046 static void rs_set_expected_tpt_table(struct iwl_lq_sta *lq_sta,
1047 struct iwl_scale_tbl_info *tbl)
1049 if (is_legacy(tbl->lq_type)) {
1050 if (!is_a_band(tbl->lq_type))
1051 tbl->expected_tpt = expected_tpt_G;
1053 tbl->expected_tpt = expected_tpt_A;
1054 } else if (is_siso(tbl->lq_type)) {
1055 if (tbl->is_ht40 && !lq_sta->is_dup)
1057 tbl->expected_tpt = expected_tpt_siso40MHzSGI;
1059 tbl->expected_tpt = expected_tpt_siso40MHz;
1060 else if (tbl->is_SGI)
1061 tbl->expected_tpt = expected_tpt_siso20MHzSGI;
1063 tbl->expected_tpt = expected_tpt_siso20MHz;
1064 } else if (is_mimo2(tbl->lq_type)) {
1065 if (tbl->is_ht40 && !lq_sta->is_dup)
1067 tbl->expected_tpt = expected_tpt_mimo2_40MHzSGI;
1069 tbl->expected_tpt = expected_tpt_mimo2_40MHz;
1070 else if (tbl->is_SGI)
1071 tbl->expected_tpt = expected_tpt_mimo2_20MHzSGI;
1073 tbl->expected_tpt = expected_tpt_mimo2_20MHz;
1074 } else if (is_mimo3(tbl->lq_type)) {
1075 if (tbl->is_ht40 && !lq_sta->is_dup)
1077 tbl->expected_tpt = expected_tpt_mimo3_40MHzSGI;
1079 tbl->expected_tpt = expected_tpt_mimo3_40MHz;
1080 else if (tbl->is_SGI)
1081 tbl->expected_tpt = expected_tpt_mimo3_20MHzSGI;
1083 tbl->expected_tpt = expected_tpt_mimo3_20MHz;
1085 tbl->expected_tpt = expected_tpt_G;
1089 * Find starting rate for new "search" high-throughput mode of modulation.
1090 * Goal is to find lowest expected rate (under perfect conditions) that is
1091 * above the current measured throughput of "active" mode, to give new mode
1092 * a fair chance to prove itself without too many challenges.
1094 * This gets called when transitioning to more aggressive modulation
1095 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
1096 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1097 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1098 * bit rate will typically need to increase, but not if performance was bad.
1100 static s32 rs_get_best_rate(struct iwl_priv *priv,
1101 struct iwl_lq_sta *lq_sta,
1102 struct iwl_scale_tbl_info *tbl, /* "search" */
1103 u16 rate_mask, s8 index)
1105 /* "active" values */
1106 struct iwl_scale_tbl_info *active_tbl =
1107 &(lq_sta->lq_info[lq_sta->active_tbl]);
1108 s32 active_sr = active_tbl->win[index].success_ratio;
1109 s32 active_tpt = active_tbl->expected_tpt[index];
1111 /* expected "search" throughput */
1112 s32 *tpt_tbl = tbl->expected_tpt;
1114 s32 new_rate, high, low, start_hi;
1118 new_rate = high = low = start_hi = IWL_RATE_INVALID;
1121 high_low = rs_get_adjacent_rate(priv, rate, rate_mask,
1124 low = high_low & 0xff;
1125 high = (high_low >> 8) & 0xff;
1128 * Lower the "search" bit rate, to give new "search" mode
1129 * approximately the same throughput as "active" if:
1131 * 1) "Active" mode has been working modestly well (but not
1132 * great), and expected "search" throughput (under perfect
1133 * conditions) at candidate rate is above the actual
1134 * measured "active" throughput (but less than expected
1135 * "active" throughput under perfect conditions).
1137 * 2) "Active" mode has been working perfectly or very well
1138 * and expected "search" throughput (under perfect
1139 * conditions) at candidate rate is above expected
1140 * "active" throughput (under perfect conditions).
1142 if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
1143 ((active_sr > IWL_RATE_DECREASE_TH) &&
1144 (active_sr <= IWL_RATE_HIGH_TH) &&
1145 (tpt_tbl[rate] <= active_tpt))) ||
1146 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
1147 (tpt_tbl[rate] > active_tpt))) {
1149 /* (2nd or later pass)
1150 * If we've already tried to raise the rate, and are
1151 * now trying to lower it, use the higher rate. */
1152 if (start_hi != IWL_RATE_INVALID) {
1153 new_rate = start_hi;
1159 /* Loop again with lower rate */
1160 if (low != IWL_RATE_INVALID)
1163 /* Lower rate not available, use the original */
1167 /* Else try to raise the "search" rate to match "active" */
1169 /* (2nd or later pass)
1170 * If we've already tried to lower the rate, and are
1171 * now trying to raise it, use the lower rate. */
1172 if (new_rate != IWL_RATE_INVALID)
1175 /* Loop again with higher rate */
1176 else if (high != IWL_RATE_INVALID) {
1180 /* Higher rate not available, use the original */
1192 * Set up search table for MIMO2
1194 static int rs_switch_to_mimo2(struct iwl_priv *priv,
1195 struct iwl_lq_sta *lq_sta,
1196 struct ieee80211_conf *conf,
1197 struct ieee80211_sta *sta,
1198 struct iwl_scale_tbl_info *tbl, int index)
1202 s8 is_green = lq_sta->is_green;
1204 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1207 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
1208 == WLAN_HT_CAP_SM_PS_STATIC)
1211 /* Need both Tx chains/antennas to support MIMO */
1212 if (priv->hw_params.tx_chains_num < 2)
1215 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO2\n");
1217 tbl->lq_type = LQ_MIMO2;
1218 tbl->is_dup = lq_sta->is_dup;
1220 tbl->max_search = IWL_MAX_SEARCH;
1221 rate_mask = lq_sta->active_mimo2_rate;
1223 if (iwl_is_ht40_tx_allowed(priv, &sta->ht_cap))
1228 /* FIXME: - don't toggle SGI here
1230 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
1234 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
1240 rs_set_expected_tpt_table(lq_sta, tbl);
1242 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1244 IWL_DEBUG_RATE(priv, "LQ: MIMO2 best rate %d mask %X\n", rate, rate_mask);
1245 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1246 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
1250 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
1252 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
1253 tbl->current_rate, is_green);
1258 * Set up search table for MIMO3
1260 static int rs_switch_to_mimo3(struct iwl_priv *priv,
1261 struct iwl_lq_sta *lq_sta,
1262 struct ieee80211_conf *conf,
1263 struct ieee80211_sta *sta,
1264 struct iwl_scale_tbl_info *tbl, int index)
1268 s8 is_green = lq_sta->is_green;
1270 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1273 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
1274 == WLAN_HT_CAP_SM_PS_STATIC)
1277 /* Need both Tx chains/antennas to support MIMO */
1278 if (priv->hw_params.tx_chains_num < 3)
1281 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO3\n");
1283 tbl->lq_type = LQ_MIMO3;
1284 tbl->is_dup = lq_sta->is_dup;
1286 tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
1287 rate_mask = lq_sta->active_mimo3_rate;
1289 if (iwl_is_ht40_tx_allowed(priv, &sta->ht_cap))
1294 /* FIXME: - don't toggle SGI here
1296 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
1300 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
1306 rs_set_expected_tpt_table(lq_sta, tbl);
1308 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1310 IWL_DEBUG_RATE(priv, "LQ: MIMO3 best rate %d mask %X\n",
1312 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1313 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
1317 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
1319 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
1320 tbl->current_rate, is_green);
1325 * Set up search table for SISO
1327 static int rs_switch_to_siso(struct iwl_priv *priv,
1328 struct iwl_lq_sta *lq_sta,
1329 struct ieee80211_conf *conf,
1330 struct ieee80211_sta *sta,
1331 struct iwl_scale_tbl_info *tbl, int index)
1334 u8 is_green = lq_sta->is_green;
1337 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1340 IWL_DEBUG_RATE(priv, "LQ: try to switch to SISO\n");
1342 tbl->is_dup = lq_sta->is_dup;
1343 tbl->lq_type = LQ_SISO;
1345 tbl->max_search = IWL_MAX_SEARCH;
1346 rate_mask = lq_sta->active_siso_rate;
1348 if (iwl_is_ht40_tx_allowed(priv, &sta->ht_cap))
1353 /* FIXME: - don't toggle SGI here
1355 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
1359 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
1366 tbl->is_SGI = 0; /*11n spec: no SGI in SISO+Greenfield*/
1368 rs_set_expected_tpt_table(lq_sta, tbl);
1369 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1371 IWL_DEBUG_RATE(priv, "LQ: get best rate %d mask %X\n", rate, rate_mask);
1372 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1373 IWL_DEBUG_RATE(priv, "can not switch with index %d rate mask %x\n",
1377 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
1378 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
1379 tbl->current_rate, is_green);
1384 * Try to switch to new modulation mode from legacy
1386 static int rs_move_legacy_other(struct iwl_priv *priv,
1387 struct iwl_lq_sta *lq_sta,
1388 struct ieee80211_conf *conf,
1389 struct ieee80211_sta *sta,
1392 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1393 struct iwl_scale_tbl_info *search_tbl =
1394 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1395 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1396 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1397 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1398 u8 start_action = tbl->action;
1399 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1400 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1402 u8 update_search_tbl_counter = 0;
1404 if (!iwl_ht_enabled(priv))
1405 /* stay in Legacy */
1406 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
1407 else if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE &&
1408 tbl->action > IWL_LEGACY_SWITCH_SISO)
1409 tbl->action = IWL_LEGACY_SWITCH_SISO;
1411 lq_sta->action_counter++;
1412 switch (tbl->action) {
1413 case IWL_LEGACY_SWITCH_ANTENNA1:
1414 case IWL_LEGACY_SWITCH_ANTENNA2:
1415 IWL_DEBUG_RATE(priv, "LQ: Legacy toggle Antenna\n");
1417 if ((tbl->action == IWL_LEGACY_SWITCH_ANTENNA1 &&
1418 tx_chains_num <= 1) ||
1419 (tbl->action == IWL_LEGACY_SWITCH_ANTENNA2 &&
1420 tx_chains_num <= 2))
1423 /* Don't change antenna if success has been great */
1424 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1427 /* Set up search table to try other antenna */
1428 memcpy(search_tbl, tbl, sz);
1430 if (rs_toggle_antenna(valid_tx_ant,
1431 &search_tbl->current_rate, search_tbl)) {
1432 update_search_tbl_counter = 1;
1433 rs_set_expected_tpt_table(lq_sta, search_tbl);
1437 case IWL_LEGACY_SWITCH_SISO:
1438 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to SISO\n");
1440 /* Set up search table to try SISO */
1441 memcpy(search_tbl, tbl, sz);
1442 search_tbl->is_SGI = 0;
1443 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
1446 lq_sta->action_counter = 0;
1451 case IWL_LEGACY_SWITCH_MIMO2_AB:
1452 case IWL_LEGACY_SWITCH_MIMO2_AC:
1453 case IWL_LEGACY_SWITCH_MIMO2_BC:
1454 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO2\n");
1456 /* Set up search table to try MIMO */
1457 memcpy(search_tbl, tbl, sz);
1458 search_tbl->is_SGI = 0;
1460 if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AB)
1461 search_tbl->ant_type = ANT_AB;
1462 else if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AC)
1463 search_tbl->ant_type = ANT_AC;
1465 search_tbl->ant_type = ANT_BC;
1467 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1470 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
1473 lq_sta->action_counter = 0;
1478 case IWL_LEGACY_SWITCH_MIMO3_ABC:
1479 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO3\n");
1481 /* Set up search table to try MIMO3 */
1482 memcpy(search_tbl, tbl, sz);
1483 search_tbl->is_SGI = 0;
1485 search_tbl->ant_type = ANT_ABC;
1487 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1490 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1493 lq_sta->action_counter = 0;
1499 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
1500 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
1502 if (tbl->action == start_action)
1506 search_tbl->lq_type = LQ_NONE;
1510 lq_sta->search_better_tbl = 1;
1512 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
1513 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
1514 if (update_search_tbl_counter)
1515 search_tbl->action = tbl->action;
1521 * Try to switch to new modulation mode from SISO
1523 static int rs_move_siso_to_other(struct iwl_priv *priv,
1524 struct iwl_lq_sta *lq_sta,
1525 struct ieee80211_conf *conf,
1526 struct ieee80211_sta *sta, int index)
1528 u8 is_green = lq_sta->is_green;
1529 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1530 struct iwl_scale_tbl_info *search_tbl =
1531 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1532 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1533 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1534 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1535 u8 start_action = tbl->action;
1536 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1537 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1538 u8 update_search_tbl_counter = 0;
1541 if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE &&
1542 tbl->action > IWL_SISO_SWITCH_ANTENNA2) {
1544 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1547 lq_sta->action_counter++;
1548 switch (tbl->action) {
1549 case IWL_SISO_SWITCH_ANTENNA1:
1550 case IWL_SISO_SWITCH_ANTENNA2:
1551 IWL_DEBUG_RATE(priv, "LQ: SISO toggle Antenna\n");
1553 if ((tbl->action == IWL_SISO_SWITCH_ANTENNA1 &&
1554 tx_chains_num <= 1) ||
1555 (tbl->action == IWL_SISO_SWITCH_ANTENNA2 &&
1556 tx_chains_num <= 2))
1559 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1562 memcpy(search_tbl, tbl, sz);
1563 if (rs_toggle_antenna(valid_tx_ant,
1564 &search_tbl->current_rate, search_tbl)) {
1565 update_search_tbl_counter = 1;
1569 case IWL_SISO_SWITCH_MIMO2_AB:
1570 case IWL_SISO_SWITCH_MIMO2_AC:
1571 case IWL_SISO_SWITCH_MIMO2_BC:
1572 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO2\n");
1573 memcpy(search_tbl, tbl, sz);
1574 search_tbl->is_SGI = 0;
1576 if (tbl->action == IWL_SISO_SWITCH_MIMO2_AB)
1577 search_tbl->ant_type = ANT_AB;
1578 else if (tbl->action == IWL_SISO_SWITCH_MIMO2_AC)
1579 search_tbl->ant_type = ANT_AC;
1581 search_tbl->ant_type = ANT_BC;
1583 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1586 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
1591 case IWL_SISO_SWITCH_GI:
1592 if (!tbl->is_ht40 &&
1593 !(priv->current_ht_config.sgf &
1597 !(priv->current_ht_config.sgf &
1601 IWL_DEBUG_RATE(priv, "LQ: SISO toggle SGI/NGI\n");
1603 memcpy(search_tbl, tbl, sz);
1609 "SGI was set in GF+SISO\n");
1611 search_tbl->is_SGI = !tbl->is_SGI;
1612 rs_set_expected_tpt_table(lq_sta, search_tbl);
1614 s32 tpt = lq_sta->last_tpt / 100;
1615 if (tpt >= search_tbl->expected_tpt[index])
1618 search_tbl->current_rate =
1619 rate_n_flags_from_tbl(priv, search_tbl,
1621 update_search_tbl_counter = 1;
1623 case IWL_SISO_SWITCH_MIMO3_ABC:
1624 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO3\n");
1625 memcpy(search_tbl, tbl, sz);
1626 search_tbl->is_SGI = 0;
1627 search_tbl->ant_type = ANT_ABC;
1629 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1632 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1639 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
1640 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1642 if (tbl->action == start_action)
1645 search_tbl->lq_type = LQ_NONE;
1649 lq_sta->search_better_tbl = 1;
1651 if (tbl->action > IWL_SISO_SWITCH_MIMO3_ABC)
1652 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1653 if (update_search_tbl_counter)
1654 search_tbl->action = tbl->action;
1660 * Try to switch to new modulation mode from MIMO2
1662 static int rs_move_mimo2_to_other(struct iwl_priv *priv,
1663 struct iwl_lq_sta *lq_sta,
1664 struct ieee80211_conf *conf,
1665 struct ieee80211_sta *sta, int index)
1667 s8 is_green = lq_sta->is_green;
1668 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1669 struct iwl_scale_tbl_info *search_tbl =
1670 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1671 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1672 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1673 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1674 u8 start_action = tbl->action;
1675 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1676 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1677 u8 update_search_tbl_counter = 0;
1680 if ((iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE) &&
1681 (tbl->action < IWL_MIMO2_SWITCH_SISO_A ||
1682 tbl->action > IWL_MIMO2_SWITCH_SISO_C)) {
1683 /* switch in SISO */
1684 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1687 lq_sta->action_counter++;
1688 switch (tbl->action) {
1689 case IWL_MIMO2_SWITCH_ANTENNA1:
1690 case IWL_MIMO2_SWITCH_ANTENNA2:
1691 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle Antennas\n");
1693 if (tx_chains_num <= 2)
1696 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1699 memcpy(search_tbl, tbl, sz);
1700 if (rs_toggle_antenna(valid_tx_ant,
1701 &search_tbl->current_rate, search_tbl)) {
1702 update_search_tbl_counter = 1;
1706 case IWL_MIMO2_SWITCH_SISO_A:
1707 case IWL_MIMO2_SWITCH_SISO_B:
1708 case IWL_MIMO2_SWITCH_SISO_C:
1709 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to SISO\n");
1711 /* Set up new search table for SISO */
1712 memcpy(search_tbl, tbl, sz);
1714 if (tbl->action == IWL_MIMO2_SWITCH_SISO_A)
1715 search_tbl->ant_type = ANT_A;
1716 else if (tbl->action == IWL_MIMO2_SWITCH_SISO_B)
1717 search_tbl->ant_type = ANT_B;
1719 search_tbl->ant_type = ANT_C;
1721 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1724 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
1731 case IWL_MIMO2_SWITCH_GI:
1732 if (!tbl->is_ht40 &&
1733 !(priv->current_ht_config.sgf &
1737 !(priv->current_ht_config.sgf &
1741 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle SGI/NGI\n");
1743 /* Set up new search table for MIMO2 */
1744 memcpy(search_tbl, tbl, sz);
1745 search_tbl->is_SGI = !tbl->is_SGI;
1746 rs_set_expected_tpt_table(lq_sta, search_tbl);
1748 * If active table already uses the fastest possible
1749 * modulation (dual stream with short guard interval),
1750 * and it's working well, there's no need to look
1751 * for a better type of modulation!
1754 s32 tpt = lq_sta->last_tpt / 100;
1755 if (tpt >= search_tbl->expected_tpt[index])
1758 search_tbl->current_rate =
1759 rate_n_flags_from_tbl(priv, search_tbl,
1761 update_search_tbl_counter = 1;
1764 case IWL_MIMO2_SWITCH_MIMO3_ABC:
1765 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to MIMO3\n");
1766 memcpy(search_tbl, tbl, sz);
1767 search_tbl->is_SGI = 0;
1768 search_tbl->ant_type = ANT_ABC;
1770 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1773 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1781 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1782 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
1784 if (tbl->action == start_action)
1787 search_tbl->lq_type = LQ_NONE;
1790 lq_sta->search_better_tbl = 1;
1792 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1793 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
1794 if (update_search_tbl_counter)
1795 search_tbl->action = tbl->action;
1802 * Try to switch to new modulation mode from MIMO3
1804 static int rs_move_mimo3_to_other(struct iwl_priv *priv,
1805 struct iwl_lq_sta *lq_sta,
1806 struct ieee80211_conf *conf,
1807 struct ieee80211_sta *sta, int index)
1809 s8 is_green = lq_sta->is_green;
1810 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1811 struct iwl_scale_tbl_info *search_tbl =
1812 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1813 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1814 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1815 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1816 u8 start_action = tbl->action;
1817 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1818 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1820 u8 update_search_tbl_counter = 0;
1822 if ((iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE) &&
1823 (tbl->action < IWL_MIMO3_SWITCH_SISO_A ||
1824 tbl->action > IWL_MIMO3_SWITCH_SISO_C)) {
1825 /* switch in SISO */
1826 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
1829 lq_sta->action_counter++;
1830 switch (tbl->action) {
1831 case IWL_MIMO3_SWITCH_ANTENNA1:
1832 case IWL_MIMO3_SWITCH_ANTENNA2:
1833 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle Antennas\n");
1835 if (tx_chains_num <= 3)
1838 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1841 memcpy(search_tbl, tbl, sz);
1842 if (rs_toggle_antenna(valid_tx_ant,
1843 &search_tbl->current_rate, search_tbl))
1846 case IWL_MIMO3_SWITCH_SISO_A:
1847 case IWL_MIMO3_SWITCH_SISO_B:
1848 case IWL_MIMO3_SWITCH_SISO_C:
1849 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to SISO\n");
1851 /* Set up new search table for SISO */
1852 memcpy(search_tbl, tbl, sz);
1854 if (tbl->action == IWL_MIMO3_SWITCH_SISO_A)
1855 search_tbl->ant_type = ANT_A;
1856 else if (tbl->action == IWL_MIMO3_SWITCH_SISO_B)
1857 search_tbl->ant_type = ANT_B;
1859 search_tbl->ant_type = ANT_C;
1861 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1864 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
1871 case IWL_MIMO3_SWITCH_MIMO2_AB:
1872 case IWL_MIMO3_SWITCH_MIMO2_AC:
1873 case IWL_MIMO3_SWITCH_MIMO2_BC:
1874 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to MIMO2\n");
1876 memcpy(search_tbl, tbl, sz);
1877 search_tbl->is_SGI = 0;
1878 if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AB)
1879 search_tbl->ant_type = ANT_AB;
1880 else if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AC)
1881 search_tbl->ant_type = ANT_AC;
1883 search_tbl->ant_type = ANT_BC;
1885 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1888 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
1895 case IWL_MIMO3_SWITCH_GI:
1896 if (!tbl->is_ht40 &&
1897 !(priv->current_ht_config.sgf &
1901 !(priv->current_ht_config.sgf &
1905 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle SGI/NGI\n");
1907 /* Set up new search table for MIMO */
1908 memcpy(search_tbl, tbl, sz);
1909 search_tbl->is_SGI = !tbl->is_SGI;
1910 rs_set_expected_tpt_table(lq_sta, search_tbl);
1912 * If active table already uses the fastest possible
1913 * modulation (dual stream with short guard interval),
1914 * and it's working well, there's no need to look
1915 * for a better type of modulation!
1918 s32 tpt = lq_sta->last_tpt / 100;
1919 if (tpt >= search_tbl->expected_tpt[index])
1922 search_tbl->current_rate =
1923 rate_n_flags_from_tbl(priv, search_tbl,
1925 update_search_tbl_counter = 1;
1929 if (tbl->action > IWL_MIMO3_SWITCH_GI)
1930 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
1932 if (tbl->action == start_action)
1935 search_tbl->lq_type = LQ_NONE;
1938 lq_sta->search_better_tbl = 1;
1940 if (tbl->action > IWL_MIMO3_SWITCH_GI)
1941 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
1942 if (update_search_tbl_counter)
1943 search_tbl->action = tbl->action;
1950 * Check whether we should continue using same modulation mode, or
1951 * begin search for a new mode, based on:
1952 * 1) # tx successes or failures while using this mode
1953 * 2) # times calling this function
1954 * 3) elapsed time in this mode (not used, for now)
1956 static void rs_stay_in_table(struct iwl_lq_sta *lq_sta)
1958 struct iwl_scale_tbl_info *tbl;
1961 int flush_interval_passed = 0;
1962 struct iwl_priv *priv;
1965 active_tbl = lq_sta->active_tbl;
1967 tbl = &(lq_sta->lq_info[active_tbl]);
1969 /* If we've been disallowing search, see if we should now allow it */
1970 if (lq_sta->stay_in_tbl) {
1972 /* Elapsed time using current modulation mode */
1973 if (lq_sta->flush_timer)
1974 flush_interval_passed =
1976 (unsigned long)(lq_sta->flush_timer +
1977 IWL_RATE_SCALE_FLUSH_INTVL));
1980 * Check if we should allow search for new modulation mode.
1981 * If many frames have failed or succeeded, or we've used
1982 * this same modulation for a long time, allow search, and
1983 * reset history stats that keep track of whether we should
1984 * allow a new search. Also (below) reset all bitmaps and
1985 * stats in active history.
1987 if ((lq_sta->total_failed > lq_sta->max_failure_limit) ||
1988 (lq_sta->total_success > lq_sta->max_success_limit) ||
1989 ((!lq_sta->search_better_tbl) && (lq_sta->flush_timer)
1990 && (flush_interval_passed))) {
1991 IWL_DEBUG_RATE(priv, "LQ: stay is expired %d %d %d\n:",
1992 lq_sta->total_failed,
1993 lq_sta->total_success,
1994 flush_interval_passed);
1996 /* Allow search for new mode */
1997 lq_sta->stay_in_tbl = 0; /* only place reset */
1998 lq_sta->total_failed = 0;
1999 lq_sta->total_success = 0;
2000 lq_sta->flush_timer = 0;
2003 * Else if we've used this modulation mode enough repetitions
2004 * (regardless of elapsed time or success/failure), reset
2005 * history bitmaps and rate-specific stats for all rates in
2009 lq_sta->table_count++;
2010 if (lq_sta->table_count >=
2011 lq_sta->table_count_limit) {
2012 lq_sta->table_count = 0;
2014 IWL_DEBUG_RATE(priv, "LQ: stay in table clear win\n");
2015 for (i = 0; i < IWL_RATE_COUNT; i++)
2016 rs_rate_scale_clear_window(
2021 /* If transitioning to allow "search", reset all history
2022 * bitmaps and stats in active table (this will become the new
2023 * "search" table). */
2024 if (!lq_sta->stay_in_tbl) {
2025 for (i = 0; i < IWL_RATE_COUNT; i++)
2026 rs_rate_scale_clear_window(&(tbl->win[i]));
2032 * setup rate table in uCode
2033 * return rate_n_flags as used in the table
2035 static u32 rs_update_rate_tbl(struct iwl_priv *priv,
2036 struct iwl_lq_sta *lq_sta,
2037 struct iwl_scale_tbl_info *tbl,
2038 int index, u8 is_green)
2042 /* Update uCode's rate table. */
2043 rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
2044 rs_fill_link_cmd(priv, lq_sta, rate);
2045 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
2051 * Do rate scaling and search for new modulation mode.
2053 static void rs_rate_scale_perform(struct iwl_priv *priv,
2054 struct sk_buff *skb,
2055 struct ieee80211_sta *sta,
2056 struct iwl_lq_sta *lq_sta)
2058 struct ieee80211_hw *hw = priv->hw;
2059 struct ieee80211_conf *conf = &hw->conf;
2060 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2061 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2062 int low = IWL_RATE_INVALID;
2063 int high = IWL_RATE_INVALID;
2066 struct iwl_rate_scale_data *window = NULL;
2067 int current_tpt = IWL_INVALID_VALUE;
2068 int low_tpt = IWL_INVALID_VALUE;
2069 int high_tpt = IWL_INVALID_VALUE;
2071 s8 scale_action = 0;
2074 struct iwl_scale_tbl_info *tbl, *tbl1;
2075 u16 rate_scale_index_msk = 0;
2082 u8 tid = MAX_TID_COUNT;
2083 struct iwl_tid_data *tid_data;
2085 IWL_DEBUG_RATE(priv, "rate scale calculate new rate for skb\n");
2087 /* Send management frames and NO_ACK data using lowest rate. */
2088 /* TODO: this could probably be improved.. */
2089 if (!ieee80211_is_data(hdr->frame_control) ||
2090 info->flags & IEEE80211_TX_CTL_NO_ACK)
2093 if (!sta || !lq_sta)
2096 lq_sta->supp_rates = sta->supp_rates[lq_sta->band];
2098 tid = rs_tl_add_packet(lq_sta, hdr);
2101 * Select rate-scale / modulation-mode table to work with in
2102 * the rest of this function: "search" if searching for better
2103 * modulation mode, or "active" if doing rate scaling within a mode.
2105 if (!lq_sta->search_better_tbl)
2106 active_tbl = lq_sta->active_tbl;
2108 active_tbl = 1 - lq_sta->active_tbl;
2110 tbl = &(lq_sta->lq_info[active_tbl]);
2111 if (is_legacy(tbl->lq_type))
2112 lq_sta->is_green = 0;
2114 lq_sta->is_green = rs_use_green(priv, conf);
2115 is_green = lq_sta->is_green;
2117 /* current tx rate */
2118 index = lq_sta->last_txrate_idx;
2120 IWL_DEBUG_RATE(priv, "Rate scale index %d for type %d\n", index,
2123 /* rates available for this association, and for modulation mode */
2124 rate_mask = rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
2126 IWL_DEBUG_RATE(priv, "mask 0x%04X \n", rate_mask);
2128 /* mask with station rate restriction */
2129 if (is_legacy(tbl->lq_type)) {
2130 if (lq_sta->band == IEEE80211_BAND_5GHZ)
2131 /* supp_rates has no CCK bits in A mode */
2132 rate_scale_index_msk = (u16) (rate_mask &
2133 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
2135 rate_scale_index_msk = (u16) (rate_mask &
2136 lq_sta->supp_rates);
2139 rate_scale_index_msk = rate_mask;
2141 if (!rate_scale_index_msk)
2142 rate_scale_index_msk = rate_mask;
2144 if (!((1 << index) & rate_scale_index_msk)) {
2145 IWL_ERR(priv, "Current Rate is not valid\n");
2146 if (lq_sta->search_better_tbl) {
2147 /* revert to active table if search table is not valid*/
2148 tbl->lq_type = LQ_NONE;
2149 lq_sta->search_better_tbl = 0;
2150 tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
2151 /* get "active" rate info */
2152 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
2153 rate = rs_update_rate_tbl(priv, lq_sta,
2154 tbl, index, is_green);
2159 /* Get expected throughput table and history window for current rate */
2160 if (!tbl->expected_tpt) {
2161 IWL_ERR(priv, "tbl->expected_tpt is NULL\n");
2165 /* force user max rate if set by user */
2166 if ((lq_sta->max_rate_idx != -1) &&
2167 (lq_sta->max_rate_idx < index)) {
2168 index = lq_sta->max_rate_idx;
2170 window = &(tbl->win[index]);
2174 window = &(tbl->win[index]);
2177 * If there is not enough history to calculate actual average
2178 * throughput, keep analyzing results of more tx frames, without
2179 * changing rate or mode (bypass most of the rest of this function).
2180 * Set up new rate table in uCode only if old rate is not supported
2181 * in current association (use new rate found above).
2183 fail_count = window->counter - window->success_counter;
2184 if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
2185 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
2186 IWL_DEBUG_RATE(priv, "LQ: still below TH. succ=%d total=%d "
2188 window->success_counter, window->counter, index);
2190 /* Can't calculate this yet; not enough history */
2191 window->average_tpt = IWL_INVALID_VALUE;
2193 /* Should we stay with this modulation mode,
2194 * or search for a new one? */
2195 rs_stay_in_table(lq_sta);
2200 /* Else we have enough samples; calculate estimate of
2201 * actual average throughput */
2203 BUG_ON(window->average_tpt != ((window->success_ratio *
2204 tbl->expected_tpt[index] + 64) / 128));
2206 /* If we are searching for better modulation mode, check success. */
2207 if (lq_sta->search_better_tbl &&
2208 (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_MULTI)) {
2209 /* If good success, continue using the "search" mode;
2210 * no need to send new link quality command, since we're
2211 * continuing to use the setup that we've been trying. */
2212 if (window->average_tpt > lq_sta->last_tpt) {
2214 IWL_DEBUG_RATE(priv, "LQ: SWITCHING TO NEW TABLE "
2215 "suc=%d cur-tpt=%d old-tpt=%d\n",
2216 window->success_ratio,
2217 window->average_tpt,
2220 if (!is_legacy(tbl->lq_type))
2221 lq_sta->enable_counter = 1;
2223 /* Swap tables; "search" becomes "active" */
2224 lq_sta->active_tbl = active_tbl;
2225 current_tpt = window->average_tpt;
2227 /* Else poor success; go back to mode in "active" table */
2230 IWL_DEBUG_RATE(priv, "LQ: GOING BACK TO THE OLD TABLE "
2231 "suc=%d cur-tpt=%d old-tpt=%d\n",
2232 window->success_ratio,
2233 window->average_tpt,
2236 /* Nullify "search" table */
2237 tbl->lq_type = LQ_NONE;
2239 /* Revert to "active" table */
2240 active_tbl = lq_sta->active_tbl;
2241 tbl = &(lq_sta->lq_info[active_tbl]);
2243 /* Revert to "active" rate and throughput info */
2244 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
2245 current_tpt = lq_sta->last_tpt;
2247 /* Need to set up a new rate table in uCode */
2251 /* Either way, we've made a decision; modulation mode
2252 * search is done, allow rate adjustment next time. */
2253 lq_sta->search_better_tbl = 0;
2254 done_search = 1; /* Don't switch modes below! */
2258 /* (Else) not in search of better modulation mode, try for better
2259 * starting rate, while staying in this mode. */
2260 high_low = rs_get_adjacent_rate(priv, index, rate_scale_index_msk,
2262 low = high_low & 0xff;
2263 high = (high_low >> 8) & 0xff;
2265 /* If user set max rate, dont allow higher than user constrain */
2266 if ((lq_sta->max_rate_idx != -1) &&
2267 (lq_sta->max_rate_idx < high))
2268 high = IWL_RATE_INVALID;
2270 sr = window->success_ratio;
2272 /* Collect measured throughputs for current and adjacent rates */
2273 current_tpt = window->average_tpt;
2274 if (low != IWL_RATE_INVALID)
2275 low_tpt = tbl->win[low].average_tpt;
2276 if (high != IWL_RATE_INVALID)
2277 high_tpt = tbl->win[high].average_tpt;
2281 /* Too many failures, decrease rate */
2282 if ((sr <= IWL_RATE_DECREASE_TH) || (current_tpt == 0)) {
2283 IWL_DEBUG_RATE(priv, "decrease rate because of low success_ratio\n");
2286 /* No throughput measured yet for adjacent rates; try increase. */
2287 } else if ((low_tpt == IWL_INVALID_VALUE) &&
2288 (high_tpt == IWL_INVALID_VALUE)) {
2290 if (high != IWL_RATE_INVALID && sr >= IWL_RATE_INCREASE_TH)
2292 else if (low != IWL_RATE_INVALID)
2296 /* Both adjacent throughputs are measured, but neither one has better
2297 * throughput; we're using the best rate, don't change it! */
2298 else if ((low_tpt != IWL_INVALID_VALUE) &&
2299 (high_tpt != IWL_INVALID_VALUE) &&
2300 (low_tpt < current_tpt) &&
2301 (high_tpt < current_tpt))
2304 /* At least one adjacent rate's throughput is measured,
2305 * and may have better performance. */
2307 /* Higher adjacent rate's throughput is measured */
2308 if (high_tpt != IWL_INVALID_VALUE) {
2309 /* Higher rate has better throughput */
2310 if (high_tpt > current_tpt &&
2311 sr >= IWL_RATE_INCREASE_TH) {
2317 /* Lower adjacent rate's throughput is measured */
2318 } else if (low_tpt != IWL_INVALID_VALUE) {
2319 /* Lower rate has better throughput */
2320 if (low_tpt > current_tpt) {
2321 IWL_DEBUG_RATE(priv,
2322 "decrease rate because of low tpt\n");
2324 } else if (sr >= IWL_RATE_INCREASE_TH) {
2330 /* Sanity check; asked for decrease, but success rate or throughput
2331 * has been good at old rate. Don't change it. */
2332 if ((scale_action == -1) && (low != IWL_RATE_INVALID) &&
2333 ((sr > IWL_RATE_HIGH_TH) ||
2334 (current_tpt > (100 * tbl->expected_tpt[low]))))
2336 if (!iwl_ht_enabled(priv) && !is_legacy(tbl->lq_type))
2338 if (iwl_tx_ant_restriction(priv) != IWL_ANT_OK_MULTI &&
2339 (is_mimo2(tbl->lq_type) || is_mimo3(tbl->lq_type)))
2341 switch (scale_action) {
2343 /* Decrease starting rate, update uCode's rate table */
2344 if (low != IWL_RATE_INVALID) {
2351 /* Increase starting rate, update uCode's rate table */
2352 if (high != IWL_RATE_INVALID) {
2364 IWL_DEBUG_RATE(priv, "choose rate scale index %d action %d low %d "
2365 "high %d type %d\n",
2366 index, scale_action, low, high, tbl->lq_type);
2369 /* Replace uCode's rate table for the destination station. */
2371 rate = rs_update_rate_tbl(priv, lq_sta,
2372 tbl, index, is_green);
2374 if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_MULTI) {
2375 /* Should we stay with this modulation mode,
2376 * or search for a new one? */
2377 rs_stay_in_table(lq_sta);
2380 * Search for new modulation mode if we're:
2381 * 1) Not changing rates right now
2382 * 2) Not just finishing up a search
2383 * 3) Allowing a new search
2385 if (!update_lq && !done_search && !lq_sta->stay_in_tbl && window->counter) {
2386 /* Save current throughput to compare with "search" throughput*/
2387 lq_sta->last_tpt = current_tpt;
2389 /* Select a new "search" modulation mode to try.
2390 * If one is found, set up the new "search" table. */
2391 if (is_legacy(tbl->lq_type))
2392 rs_move_legacy_other(priv, lq_sta, conf, sta, index);
2393 else if (is_siso(tbl->lq_type))
2394 rs_move_siso_to_other(priv, lq_sta, conf, sta, index);
2395 else if (is_mimo2(tbl->lq_type))
2396 rs_move_mimo2_to_other(priv, lq_sta, conf, sta, index);
2398 rs_move_mimo3_to_other(priv, lq_sta, conf, sta, index);
2400 /* If new "search" mode was selected, set up in uCode table */
2401 if (lq_sta->search_better_tbl) {
2402 /* Access the "search" table, clear its history. */
2403 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
2404 for (i = 0; i < IWL_RATE_COUNT; i++)
2405 rs_rate_scale_clear_window(&(tbl->win[i]));
2407 /* Use new "search" start rate */
2408 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
2410 IWL_DEBUG_RATE(priv, "Switch current mcs: %X index: %d\n",
2411 tbl->current_rate, index);
2412 rs_fill_link_cmd(priv, lq_sta, tbl->current_rate);
2413 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
2418 if (done_search && !lq_sta->stay_in_tbl) {
2419 /* If the "active" (non-search) mode was legacy,
2420 * and we've tried switching antennas,
2421 * but we haven't been able to try HT modes (not available),
2422 * stay with best antenna legacy modulation for a while
2423 * before next round of mode comparisons. */
2424 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
2425 if (is_legacy(tbl1->lq_type) && !conf_is_ht(conf) &&
2426 lq_sta->action_counter > tbl1->max_search) {
2427 IWL_DEBUG_RATE(priv, "LQ: STAY in legacy table\n");
2428 rs_set_stay_in_table(priv, 1, lq_sta);
2431 /* If we're in an HT mode, and all 3 mode switch actions
2432 * have been tried and compared, stay in this best modulation
2433 * mode for a while before next round of mode comparisons. */
2434 if (lq_sta->enable_counter &&
2435 (lq_sta->action_counter >= tbl1->max_search) &&
2436 iwl_ht_enabled(priv)) {
2437 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2438 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
2439 (tid != MAX_TID_COUNT)) {
2441 &priv->stations[lq_sta->lq.sta_id].tid[tid];
2442 if (tid_data->agg.state == IWL_AGG_OFF) {
2443 IWL_DEBUG_RATE(priv,
2444 "try to aggregate tid %d\n",
2446 rs_tl_turn_on_agg(priv, tid,
2450 rs_set_stay_in_table(priv, 0, lq_sta);
2455 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
2457 lq_sta->last_txrate_idx = i;
2463 static void rs_initialize_lq(struct iwl_priv *priv,
2464 struct ieee80211_conf *conf,
2465 struct ieee80211_sta *sta,
2466 struct iwl_lq_sta *lq_sta)
2468 struct iwl_scale_tbl_info *tbl;
2472 u8 use_green = rs_use_green(priv, conf);
2476 if (!sta || !lq_sta)
2479 i = lq_sta->last_txrate_idx;
2481 if ((lq_sta->lq.sta_id == 0xff) &&
2482 (priv->iw_mode == NL80211_IFTYPE_ADHOC))
2485 valid_tx_ant = priv->hw_params.valid_tx_ant;
2487 if (!lq_sta->search_better_tbl)
2488 active_tbl = lq_sta->active_tbl;
2490 active_tbl = 1 - lq_sta->active_tbl;
2492 tbl = &(lq_sta->lq_info[active_tbl]);
2494 if ((i < 0) || (i >= IWL_RATE_COUNT))
2497 rate = iwl_rates[i].plcp;
2498 tbl->ant_type = first_antenna(valid_tx_ant);
2499 rate |= tbl->ant_type << RATE_MCS_ANT_POS;
2501 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
2502 rate |= RATE_MCS_CCK_MSK;
2504 rs_get_tbl_info_from_mcs(rate, priv->band, tbl, &rate_idx);
2505 if (!rs_is_valid_ant(valid_tx_ant, tbl->ant_type))
2506 rs_toggle_antenna(valid_tx_ant, &rate, tbl);
2508 rate = rate_n_flags_from_tbl(priv, tbl, rate_idx, use_green);
2509 tbl->current_rate = rate;
2510 rs_set_expected_tpt_table(lq_sta, tbl);
2511 rs_fill_link_cmd(NULL, lq_sta, rate);
2512 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
2517 static void rs_get_rate(void *priv_r, struct ieee80211_sta *sta, void *priv_sta,
2518 struct ieee80211_tx_rate_control *txrc)
2521 struct sk_buff *skb = txrc->skb;
2522 struct ieee80211_supported_band *sband = txrc->sband;
2523 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
2524 struct ieee80211_conf *conf = &priv->hw->conf;
2525 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
2526 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2527 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2528 struct iwl_lq_sta *lq_sta = priv_sta;
2531 IWL_DEBUG_RATE_LIMIT(priv, "rate scale calculate new rate for skb\n");
2533 /* Get max rate if user set max rate */
2535 lq_sta->max_rate_idx = txrc->max_rate_idx;
2536 if ((sband->band == IEEE80211_BAND_5GHZ) &&
2537 (lq_sta->max_rate_idx != -1))
2538 lq_sta->max_rate_idx += IWL_FIRST_OFDM_RATE;
2539 if ((lq_sta->max_rate_idx < 0) ||
2540 (lq_sta->max_rate_idx >= IWL_RATE_COUNT))
2541 lq_sta->max_rate_idx = -1;
2544 /* Send management frames and NO_ACK data using lowest rate. */
2545 if (rate_control_send_low(sta, priv_sta, txrc))
2548 rate_idx = lq_sta->last_txrate_idx;
2550 if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) &&
2551 !lq_sta->ibss_sta_added) {
2552 u8 sta_id = iwl_find_station(priv, hdr->addr1);
2554 if (sta_id == IWL_INVALID_STATION) {
2555 IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n",
2557 sta_id = iwl_add_station(priv, hdr->addr1,
2558 false, CMD_ASYNC, ht_cap);
2560 if ((sta_id != IWL_INVALID_STATION)) {
2561 lq_sta->lq.sta_id = sta_id;
2562 lq_sta->lq.rs_table[0].rate_n_flags = 0;
2563 lq_sta->ibss_sta_added = 1;
2564 rs_initialize_lq(priv, conf, sta, lq_sta);
2568 if (lq_sta->last_rate_n_flags & RATE_MCS_HT_MSK) {
2569 rate_idx -= IWL_FIRST_OFDM_RATE;
2570 /* 6M and 9M shared same MCS index */
2571 rate_idx = (rate_idx > 0) ? (rate_idx - 1) : 0;
2572 if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
2573 IWL_RATE_MIMO3_6M_PLCP)
2574 rate_idx = rate_idx + (2 * MCS_INDEX_PER_STREAM);
2575 else if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
2576 IWL_RATE_MIMO2_6M_PLCP)
2577 rate_idx = rate_idx + MCS_INDEX_PER_STREAM;
2578 info->control.rates[0].flags = IEEE80211_TX_RC_MCS;
2579 if (lq_sta->last_rate_n_flags & RATE_MCS_SGI_MSK)
2580 info->control.rates[0].flags |= IEEE80211_TX_RC_SHORT_GI;
2581 if (lq_sta->last_rate_n_flags & RATE_MCS_DUP_MSK)
2582 info->control.rates[0].flags |= IEEE80211_TX_RC_DUP_DATA;
2583 if (lq_sta->last_rate_n_flags & RATE_MCS_HT40_MSK)
2584 info->control.rates[0].flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
2585 if (lq_sta->last_rate_n_flags & RATE_MCS_GF_MSK)
2586 info->control.rates[0].flags |= IEEE80211_TX_RC_GREEN_FIELD;
2588 if (rate_idx < 0 || rate_idx > IWL_RATE_COUNT)
2589 rate_idx = rate_lowest_index(sband, sta);
2590 else if (sband->band == IEEE80211_BAND_5GHZ)
2591 rate_idx -= IWL_FIRST_OFDM_RATE;
2593 info->control.rates[0].idx = rate_idx;
2597 static void *rs_alloc_sta(void *priv_rate, struct ieee80211_sta *sta,
2600 struct iwl_lq_sta *lq_sta;
2601 struct iwl_priv *priv;
2604 priv = (struct iwl_priv *)priv_rate;
2605 IWL_DEBUG_RATE(priv, "create station rate scale window\n");
2607 lq_sta = kzalloc(sizeof(struct iwl_lq_sta), gfp);
2611 lq_sta->lq.sta_id = 0xff;
2614 for (j = 0; j < LQ_SIZE; j++)
2615 for (i = 0; i < IWL_RATE_COUNT; i++)
2616 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
2621 static void rs_rate_init(void *priv_r, struct ieee80211_supported_band *sband,
2622 struct ieee80211_sta *sta, void *priv_sta)
2625 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
2626 struct ieee80211_conf *conf = &priv->hw->conf;
2627 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
2628 struct iwl_lq_sta *lq_sta = priv_sta;
2633 lq_sta->flush_timer = 0;
2634 lq_sta->supp_rates = sta->supp_rates[sband->band];
2635 for (j = 0; j < LQ_SIZE; j++)
2636 for (i = 0; i < IWL_RATE_COUNT; i++)
2637 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
2639 IWL_DEBUG_RATE(priv, "LQ: *** rate scale station global init ***\n");
2640 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2641 * the lowest or the highest rate.. Could consider using RSSI from
2642 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2645 lq_sta->ibss_sta_added = 0;
2646 if (priv->iw_mode == NL80211_IFTYPE_AP) {
2647 u8 sta_id = iwl_find_station(priv,
2650 /* for IBSS the call are from tasklet */
2651 IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n", sta->addr);
2653 if (sta_id == IWL_INVALID_STATION) {
2654 IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n", sta->addr);
2655 sta_id = iwl_add_station(priv, sta->addr, false,
2658 if ((sta_id != IWL_INVALID_STATION)) {
2659 lq_sta->lq.sta_id = sta_id;
2660 lq_sta->lq.rs_table[0].rate_n_flags = 0;
2662 /* FIXME: this is w/a remove it later */
2663 priv->assoc_station_added = 1;
2667 lq_sta->max_rate_idx = -1;
2668 lq_sta->missed_rate_counter = IWL_MISSED_RATE_MAX;
2669 lq_sta->is_green = rs_use_green(priv, conf);
2670 lq_sta->active_legacy_rate = priv->active_rate & ~(0x1000);
2671 lq_sta->active_rate_basic = priv->active_rate_basic;
2672 lq_sta->band = priv->band;
2674 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2675 * supp_rates[] does not; shift to convert format, force 9 MBits off.
2677 lq_sta->active_siso_rate = ht_cap->mcs.rx_mask[0] << 1;
2678 lq_sta->active_siso_rate |= ht_cap->mcs.rx_mask[0] & 0x1;
2679 lq_sta->active_siso_rate &= ~((u16)0x2);
2680 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
2683 lq_sta->active_mimo2_rate = ht_cap->mcs.rx_mask[1] << 1;
2684 lq_sta->active_mimo2_rate |= ht_cap->mcs.rx_mask[1] & 0x1;
2685 lq_sta->active_mimo2_rate &= ~((u16)0x2);
2686 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2688 lq_sta->active_mimo3_rate = ht_cap->mcs.rx_mask[2] << 1;
2689 lq_sta->active_mimo3_rate |= ht_cap->mcs.rx_mask[2] & 0x1;
2690 lq_sta->active_mimo3_rate &= ~((u16)0x2);
2691 lq_sta->active_mimo3_rate <<= IWL_FIRST_OFDM_RATE;
2693 IWL_DEBUG_RATE(priv, "SISO-RATE=%X MIMO2-RATE=%X MIMO3-RATE=%X\n",
2694 lq_sta->active_siso_rate,
2695 lq_sta->active_mimo2_rate,
2696 lq_sta->active_mimo3_rate);
2698 /* These values will be overridden later */
2699 lq_sta->lq.general_params.single_stream_ant_msk = ANT_A;
2700 lq_sta->lq.general_params.dual_stream_ant_msk = ANT_AB;
2702 /* as default allow aggregation for all tids */
2703 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
2706 /* Find highest tx rate supported by hardware and destination station */
2707 mask_bit = sta->supp_rates[sband->band];
2708 count = sband->n_bitrates;
2709 if (sband->band == IEEE80211_BAND_5GHZ) {
2710 count += IWL_FIRST_OFDM_RATE;
2711 start_rate = IWL_FIRST_OFDM_RATE;
2712 mask_bit <<= IWL_FIRST_OFDM_RATE;
2715 mask_bit = mask_bit & lq_sta->active_legacy_rate;
2716 lq_sta->last_txrate_idx = 4;
2717 for (i = start_rate; i < count; i++)
2718 if (mask_bit & BIT(i))
2719 lq_sta->last_txrate_idx = i;
2721 rs_initialize_lq(priv, conf, sta, lq_sta);
2724 static void rs_fill_link_cmd(struct iwl_priv *priv,
2725 struct iwl_lq_sta *lq_sta, u32 new_rate)
2727 struct iwl_scale_tbl_info tbl_type;
2730 int repeat_rate = 0;
2731 u8 ant_toggle_cnt = 0;
2732 u8 use_ht_possible = 1;
2733 u8 valid_tx_ant = 0;
2734 struct iwl_link_quality_cmd *lq_cmd = &lq_sta->lq;
2736 /* Override starting rate (index 0) if needed for debug purposes */
2737 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
2739 /* Interpret new_rate (rate_n_flags) */
2740 memset(&tbl_type, 0, sizeof(tbl_type));
2741 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
2742 &tbl_type, &rate_idx);
2744 /* How many times should we repeat the initial rate? */
2745 if (is_legacy(tbl_type.lq_type)) {
2747 repeat_rate = IWL_NUMBER_TRY;
2749 repeat_rate = IWL_HT_NUMBER_TRY;
2752 lq_cmd->general_params.mimo_delimiter =
2753 is_mimo(tbl_type.lq_type) ? 1 : 0;
2755 /* Fill 1st table entry (index 0) */
2756 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
2758 if (num_of_ant(tbl_type.ant_type) == 1) {
2759 lq_cmd->general_params.single_stream_ant_msk =
2761 } else if (num_of_ant(tbl_type.ant_type) == 2) {
2762 lq_cmd->general_params.dual_stream_ant_msk =
2764 } /* otherwise we don't modify the existing value */
2770 valid_tx_ant = priv->hw_params.valid_tx_ant;
2772 /* Fill rest of rate table */
2773 while (index < LINK_QUAL_MAX_RETRY_NUM) {
2774 /* Repeat initial/next rate.
2775 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2776 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
2777 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
2778 if (is_legacy(tbl_type.lq_type)) {
2779 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2782 rs_toggle_antenna(valid_tx_ant,
2783 &new_rate, &tbl_type))
2787 /* Override next rate if needed for debug purposes */
2788 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
2790 /* Fill next table entry */
2791 lq_cmd->rs_table[index].rate_n_flags =
2792 cpu_to_le32(new_rate);
2797 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band, &tbl_type,
2800 /* Indicate to uCode which entries might be MIMO.
2801 * If initial rate was MIMO, this will finally end up
2802 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
2803 if (is_mimo(tbl_type.lq_type))
2804 lq_cmd->general_params.mimo_delimiter = index;
2807 new_rate = rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
2810 /* How many times should we repeat the next rate? */
2811 if (is_legacy(tbl_type.lq_type)) {
2812 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2815 rs_toggle_antenna(valid_tx_ant,
2816 &new_rate, &tbl_type))
2819 repeat_rate = IWL_NUMBER_TRY;
2821 repeat_rate = IWL_HT_NUMBER_TRY;
2824 /* Don't allow HT rates after next pass.
2825 * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
2826 use_ht_possible = 0;
2828 /* Override next rate if needed for debug purposes */
2829 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
2831 /* Fill next table entry */
2832 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
2838 lq_cmd->agg_params.agg_frame_cnt_limit = LINK_QUAL_AGG_FRAME_LIMIT_MAX;
2839 lq_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
2840 lq_cmd->agg_params.agg_time_limit =
2841 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
2844 static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
2848 /* rate scale requires free function to be implemented */
2849 static void rs_free(void *priv_rate)
2854 static void rs_free_sta(void *priv_r, struct ieee80211_sta *sta,
2857 struct iwl_lq_sta *lq_sta = priv_sta;
2858 struct iwl_priv *priv __maybe_unused = priv_r;
2860 IWL_DEBUG_RATE(priv, "enter\n");
2862 IWL_DEBUG_RATE(priv, "leave\n");
2866 #ifdef CONFIG_MAC80211_DEBUGFS
2867 static int open_file_generic(struct inode *inode, struct file *file)
2869 file->private_data = inode->i_private;
2872 static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
2873 u32 *rate_n_flags, int index)
2875 struct iwl_priv *priv;
2880 valid_tx_ant = priv->hw_params.valid_tx_ant;
2881 if (lq_sta->dbg_fixed_rate) {
2883 ((lq_sta->dbg_fixed_rate & RATE_MCS_ANT_ABC_MSK)
2884 >> RATE_MCS_ANT_POS);
2885 if ((valid_tx_ant & ant_sel_tx) == ant_sel_tx) {
2886 *rate_n_flags = lq_sta->dbg_fixed_rate;
2887 IWL_DEBUG_RATE(priv, "Fixed rate ON\n");
2889 lq_sta->dbg_fixed_rate = 0;
2891 "Invalid antenna selection 0x%X, Valid is 0x%X\n",
2892 ant_sel_tx, valid_tx_ant);
2893 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
2896 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
2900 static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
2901 const char __user *user_buf, size_t count, loff_t *ppos)
2903 struct iwl_lq_sta *lq_sta = file->private_data;
2904 struct iwl_priv *priv;
2910 memset(buf, 0, sizeof(buf));
2911 buf_size = min(count, sizeof(buf) - 1);
2912 if (copy_from_user(buf, user_buf, buf_size))
2915 if (sscanf(buf, "%x", &parsed_rate) == 1)
2916 lq_sta->dbg_fixed_rate = parsed_rate;
2918 lq_sta->dbg_fixed_rate = 0;
2920 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
2921 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2922 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2923 lq_sta->active_mimo3_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2925 IWL_DEBUG_RATE(priv, "sta_id %d rate 0x%X\n",
2926 lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
2928 if (lq_sta->dbg_fixed_rate) {
2929 rs_fill_link_cmd(NULL, lq_sta, lq_sta->dbg_fixed_rate);
2930 iwl_send_lq_cmd(lq_sta->drv, &lq_sta->lq, CMD_ASYNC);
2936 static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
2937 char __user *user_buf, size_t count, loff_t *ppos)
2945 struct iwl_lq_sta *lq_sta = file->private_data;
2946 struct iwl_priv *priv;
2947 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
2950 buff = kmalloc(1024, GFP_KERNEL);
2954 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
2955 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
2956 lq_sta->total_failed, lq_sta->total_success,
2957 lq_sta->active_legacy_rate);
2958 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
2959 lq_sta->dbg_fixed_rate);
2960 desc += sprintf(buff+desc, "valid_tx_ant %s%s%s\n",
2961 (priv->hw_params.valid_tx_ant & ANT_A) ? "ANT_A," : "",
2962 (priv->hw_params.valid_tx_ant & ANT_B) ? "ANT_B," : "",
2963 (priv->hw_params.valid_tx_ant & ANT_C) ? "ANT_C" : "");
2964 desc += sprintf(buff+desc, "lq type %s\n",
2965 (is_legacy(tbl->lq_type)) ? "legacy" : "HT");
2966 if (is_Ht(tbl->lq_type)) {
2967 desc += sprintf(buff+desc, " %s",
2968 (is_siso(tbl->lq_type)) ? "SISO" :
2969 ((is_mimo2(tbl->lq_type)) ? "MIMO2" : "MIMO3"));
2970 desc += sprintf(buff+desc, " %s",
2971 (tbl->is_ht40) ? "40MHz" : "20MHz");
2972 desc += sprintf(buff+desc, " %s %s\n", (tbl->is_SGI) ? "SGI" : "",
2973 (lq_sta->is_green) ? "GF enabled" : "");
2975 desc += sprintf(buff+desc, "last tx rate=0x%X\n",
2976 lq_sta->last_rate_n_flags);
2977 desc += sprintf(buff+desc, "general:"
2978 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
2979 lq_sta->lq.general_params.flags,
2980 lq_sta->lq.general_params.mimo_delimiter,
2981 lq_sta->lq.general_params.single_stream_ant_msk,
2982 lq_sta->lq.general_params.dual_stream_ant_msk);
2984 desc += sprintf(buff+desc, "agg:"
2985 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
2986 le16_to_cpu(lq_sta->lq.agg_params.agg_time_limit),
2987 lq_sta->lq.agg_params.agg_dis_start_th,
2988 lq_sta->lq.agg_params.agg_frame_cnt_limit);
2990 desc += sprintf(buff+desc,
2991 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
2992 lq_sta->lq.general_params.start_rate_index[0],
2993 lq_sta->lq.general_params.start_rate_index[1],
2994 lq_sta->lq.general_params.start_rate_index[2],
2995 lq_sta->lq.general_params.start_rate_index[3]);
2997 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
2998 index = iwl_hwrate_to_plcp_idx(
2999 le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags));
3000 if (is_legacy(tbl->lq_type)) {
3001 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps\n",
3002 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
3003 iwl_rate_mcs[index].mbps);
3005 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps (%s)\n",
3006 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
3007 iwl_rate_mcs[index].mbps, iwl_rate_mcs[index].mcs);
3011 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3016 static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
3017 .write = rs_sta_dbgfs_scale_table_write,
3018 .read = rs_sta_dbgfs_scale_table_read,
3019 .open = open_file_generic,
3021 static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
3022 char __user *user_buf, size_t count, loff_t *ppos)
3029 struct iwl_lq_sta *lq_sta = file->private_data;
3031 buff = kmalloc(1024, GFP_KERNEL);
3035 for (i = 0; i < LQ_SIZE; i++) {
3036 desc += sprintf(buff+desc,
3037 "%s type=%d SGI=%d HT40=%d DUP=%d GF=%d\n"
3039 lq_sta->active_tbl == i ? "*" : "x",
3040 lq_sta->lq_info[i].lq_type,
3041 lq_sta->lq_info[i].is_SGI,
3042 lq_sta->lq_info[i].is_ht40,
3043 lq_sta->lq_info[i].is_dup,
3045 lq_sta->lq_info[i].current_rate);
3046 for (j = 0; j < IWL_RATE_COUNT; j++) {
3047 desc += sprintf(buff+desc,
3048 "counter=%d success=%d %%=%d\n",
3049 lq_sta->lq_info[i].win[j].counter,
3050 lq_sta->lq_info[i].win[j].success_counter,
3051 lq_sta->lq_info[i].win[j].success_ratio);
3054 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3059 static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
3060 .read = rs_sta_dbgfs_stats_table_read,
3061 .open = open_file_generic,
3064 static ssize_t rs_sta_dbgfs_rate_scale_data_read(struct file *file,
3065 char __user *user_buf, size_t count, loff_t *ppos)
3071 struct iwl_lq_sta *lq_sta = file->private_data;
3072 struct iwl_priv *priv;
3073 struct iwl_scale_tbl_info *tbl = &lq_sta->lq_info[lq_sta->active_tbl];
3077 if (is_Ht(tbl->lq_type))
3078 desc += sprintf(buff+desc,
3079 "Bit Rate= %d Mb/s\n",
3080 tbl->expected_tpt[lq_sta->last_txrate_idx]);
3082 desc += sprintf(buff+desc,
3083 "Bit Rate= %d Mb/s\n",
3084 iwl_rates[lq_sta->last_txrate_idx].ieee >> 1);
3085 desc += sprintf(buff+desc,
3086 "Signal Level= %d dBm\tNoise Level= %d dBm\n",
3087 priv->last_rx_rssi, priv->last_rx_noise);
3088 desc += sprintf(buff+desc,
3089 "Tsf= 0x%llx\tBeacon time= 0x%08X\n",
3090 priv->last_tsf, priv->last_beacon_time);
3092 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3096 static const struct file_operations rs_sta_dbgfs_rate_scale_data_ops = {
3097 .read = rs_sta_dbgfs_rate_scale_data_read,
3098 .open = open_file_generic,
3101 static void rs_add_debugfs(void *priv, void *priv_sta,
3104 struct iwl_lq_sta *lq_sta = priv_sta;
3105 lq_sta->rs_sta_dbgfs_scale_table_file =
3106 debugfs_create_file("rate_scale_table", 0600, dir,
3107 lq_sta, &rs_sta_dbgfs_scale_table_ops);
3108 lq_sta->rs_sta_dbgfs_stats_table_file =
3109 debugfs_create_file("rate_stats_table", 0600, dir,
3110 lq_sta, &rs_sta_dbgfs_stats_table_ops);
3111 lq_sta->rs_sta_dbgfs_rate_scale_data_file =
3112 debugfs_create_file("rate_scale_data", 0600, dir,
3113 lq_sta, &rs_sta_dbgfs_rate_scale_data_ops);
3114 lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
3115 debugfs_create_u8("tx_agg_tid_enable", 0600, dir,
3116 &lq_sta->tx_agg_tid_en);
3120 static void rs_remove_debugfs(void *priv, void *priv_sta)
3122 struct iwl_lq_sta *lq_sta = priv_sta;
3123 debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
3124 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
3125 debugfs_remove(lq_sta->rs_sta_dbgfs_rate_scale_data_file);
3126 debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
3130 static struct rate_control_ops rs_ops = {
3133 .tx_status = rs_tx_status,
3134 .get_rate = rs_get_rate,
3135 .rate_init = rs_rate_init,
3138 .alloc_sta = rs_alloc_sta,
3139 .free_sta = rs_free_sta,
3140 #ifdef CONFIG_MAC80211_DEBUGFS
3141 .add_sta_debugfs = rs_add_debugfs,
3142 .remove_sta_debugfs = rs_remove_debugfs,
3146 int iwlagn_rate_control_register(void)
3148 return ieee80211_rate_control_register(&rs_ops);
3151 void iwlagn_rate_control_unregister(void)
3153 ieee80211_rate_control_unregister(&rs_ops);