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;
661 * Green-field mode is valid if the station supports it and
662 * there are no non-GF stations present in the BSS.
664 static inline u8 rs_use_green(struct ieee80211_sta *sta,
665 struct iwl_ht_info *ht_conf)
667 return (sta->ht_cap.cap & IEEE80211_HT_CAP_GRN_FLD) &&
668 !(ht_conf->non_GF_STA_present);
672 * rs_get_supported_rates - get the available rates
674 * if management frame or broadcast frame only return
675 * basic available rates.
678 static u16 rs_get_supported_rates(struct iwl_lq_sta *lq_sta,
679 struct ieee80211_hdr *hdr,
680 enum iwl_table_type rate_type)
682 if (hdr && is_multicast_ether_addr(hdr->addr1) &&
683 lq_sta->active_rate_basic)
684 return lq_sta->active_rate_basic;
686 if (is_legacy(rate_type)) {
687 return lq_sta->active_legacy_rate;
689 if (is_siso(rate_type))
690 return lq_sta->active_siso_rate;
691 else if (is_mimo2(rate_type))
692 return lq_sta->active_mimo2_rate;
694 return lq_sta->active_mimo3_rate;
698 static u16 rs_get_adjacent_rate(struct iwl_priv *priv, u8 index, u16 rate_mask,
701 u8 high = IWL_RATE_INVALID;
702 u8 low = IWL_RATE_INVALID;
704 /* 802.11A or ht walks to the next literal adjacent rate in
706 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
710 /* Find the previous rate that is in the rate mask */
712 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
713 if (rate_mask & mask) {
719 /* Find the next rate that is in the rate mask */
721 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
722 if (rate_mask & mask) {
728 return (high << 8) | low;
732 while (low != IWL_RATE_INVALID) {
733 low = iwl_rates[low].prev_rs;
734 if (low == IWL_RATE_INVALID)
736 if (rate_mask & (1 << low))
738 IWL_DEBUG_RATE(priv, "Skipping masked lower rate: %d\n", low);
742 while (high != IWL_RATE_INVALID) {
743 high = iwl_rates[high].next_rs;
744 if (high == IWL_RATE_INVALID)
746 if (rate_mask & (1 << high))
748 IWL_DEBUG_RATE(priv, "Skipping masked higher rate: %d\n", high);
751 return (high << 8) | low;
754 static u32 rs_get_lower_rate(struct iwl_lq_sta *lq_sta,
755 struct iwl_scale_tbl_info *tbl,
756 u8 scale_index, u8 ht_possible)
761 u8 switch_to_legacy = 0;
762 u8 is_green = lq_sta->is_green;
764 /* check if we need to switch from HT to legacy rates.
765 * assumption is that mandatory rates (1Mbps or 6Mbps)
766 * are always supported (spec demand) */
767 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
768 switch_to_legacy = 1;
769 scale_index = rs_ht_to_legacy[scale_index];
770 if (lq_sta->band == IEEE80211_BAND_5GHZ)
775 if (num_of_ant(tbl->ant_type) > 1)
776 tbl->ant_type = ANT_A;/*FIXME:RS*/
780 tbl->max_search = IWL_MAX_SEARCH;
783 rate_mask = rs_get_supported_rates(lq_sta, NULL, tbl->lq_type);
785 /* Mask with station rate restriction */
786 if (is_legacy(tbl->lq_type)) {
787 /* supp_rates has no CCK bits in A mode */
788 if (lq_sta->band == IEEE80211_BAND_5GHZ)
789 rate_mask = (u16)(rate_mask &
790 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
792 rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
795 /* If we switched from HT to legacy, check current rate */
796 if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
801 high_low = rs_get_adjacent_rate(lq_sta->drv, scale_index, rate_mask,
803 low = high_low & 0xff;
805 if (low == IWL_RATE_INVALID)
809 return rate_n_flags_from_tbl(lq_sta->drv, tbl, low, is_green);
813 * mac80211 sends us Tx status
815 static void rs_tx_status(void *priv_r, struct ieee80211_supported_band *sband,
816 struct ieee80211_sta *sta, void *priv_sta,
821 int rs_index, mac_index, index = 0;
822 struct iwl_lq_sta *lq_sta = priv_sta;
823 struct iwl_link_quality_cmd *table;
824 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
825 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
826 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
827 struct iwl_rate_scale_data *window = NULL;
828 struct iwl_rate_scale_data *search_win = NULL;
829 enum mac80211_rate_control_flags mac_flags;
831 struct iwl_scale_tbl_info tbl_type;
832 struct iwl_scale_tbl_info *curr_tbl, *search_tbl;
836 IWL_DEBUG_RATE_LIMIT(priv, "get frame ack response, update rate scale window\n");
838 if (!ieee80211_is_data(hdr->frame_control) ||
839 info->flags & IEEE80211_TX_CTL_NO_ACK)
842 /* This packet was aggregated but doesn't carry rate scale info */
843 if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
844 !(info->flags & IEEE80211_TX_STAT_AMPDU))
847 if (info->flags & IEEE80211_TX_STAT_AMPDU)
850 retries = info->status.rates[0].count - 1;
855 if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) &&
856 !lq_sta->ibss_sta_added)
860 active_index = lq_sta->active_tbl;
862 curr_tbl = &(lq_sta->lq_info[active_index]);
863 search_tbl = &(lq_sta->lq_info[(1 - active_index)]);
864 window = (struct iwl_rate_scale_data *)&(curr_tbl->win[0]);
865 search_win = (struct iwl_rate_scale_data *)&(search_tbl->win[0]);
868 * Ignore this Tx frame response if its initial rate doesn't match
869 * that of latest Link Quality command. There may be stragglers
870 * from a previous Link Quality command, but we're no longer interested
871 * in those; they're either from the "active" mode while we're trying
872 * to check "search" mode, or a prior "search" mode after we've moved
873 * to a new "search" mode (which might become the new "active" mode).
875 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
876 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
877 if (priv->band == IEEE80211_BAND_5GHZ)
878 rs_index -= IWL_FIRST_OFDM_RATE;
879 mac_flags = info->status.rates[0].flags;
880 mac_index = info->status.rates[0].idx;
881 /* For HT packets, map MCS to PLCP */
882 if (mac_flags & IEEE80211_TX_RC_MCS) {
883 mac_index &= RATE_MCS_CODE_MSK; /* Remove # of streams */
884 if (mac_index >= (IWL_RATE_9M_INDEX - IWL_FIRST_OFDM_RATE))
887 * mac80211 HT index is always zero-indexed; we need to move
888 * HT OFDM rates after CCK rates in 2.4 GHz band
890 if (priv->band == IEEE80211_BAND_2GHZ)
891 mac_index += IWL_FIRST_OFDM_RATE;
894 if ((mac_index < 0) ||
895 (tbl_type.is_SGI != !!(mac_flags & IEEE80211_TX_RC_SHORT_GI)) ||
896 (tbl_type.is_ht40 != !!(mac_flags & IEEE80211_TX_RC_40_MHZ_WIDTH)) ||
897 (tbl_type.is_dup != !!(mac_flags & IEEE80211_TX_RC_DUP_DATA)) ||
898 (tbl_type.ant_type != info->antenna_sel_tx) ||
899 (!!(tx_rate & RATE_MCS_HT_MSK) != !!(mac_flags & IEEE80211_TX_RC_MCS)) ||
900 (!!(tx_rate & RATE_MCS_GF_MSK) != !!(mac_flags & IEEE80211_TX_RC_GREEN_FIELD)) ||
901 (rs_index != mac_index)) {
902 IWL_DEBUG_RATE(priv, "initial rate %d does not match %d (0x%x)\n", mac_index, rs_index, tx_rate);
903 /* the last LQ command could failed so the LQ in ucode not
904 * the same in driver sync up
906 lq_sta->missed_rate_counter++;
907 if (lq_sta->missed_rate_counter > IWL_MISSED_RATE_MAX) {
908 lq_sta->missed_rate_counter = 0;
909 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
914 lq_sta->missed_rate_counter = 0;
915 /* Update frame history window with "failure" for each Tx retry. */
917 /* Look up the rate and other info used for each tx attempt.
918 * Each tx attempt steps one entry deeper in the rate table. */
919 tx_rate = le32_to_cpu(table->rs_table[index].rate_n_flags);
920 rs_get_tbl_info_from_mcs(tx_rate, priv->band,
921 &tbl_type, &rs_index);
923 /* If type matches "search" table,
924 * add failure to "search" history */
925 if ((tbl_type.lq_type == search_tbl->lq_type) &&
926 (tbl_type.ant_type == search_tbl->ant_type) &&
927 (tbl_type.is_SGI == search_tbl->is_SGI)) {
928 if (search_tbl->expected_tpt)
929 tpt = search_tbl->expected_tpt[rs_index];
932 rs_collect_tx_data(search_win, rs_index, tpt, 1, 0);
934 /* Else if type matches "current/active" table,
935 * add failure to "current/active" history */
936 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
937 (tbl_type.ant_type == curr_tbl->ant_type) &&
938 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
939 if (curr_tbl->expected_tpt)
940 tpt = curr_tbl->expected_tpt[rs_index];
943 rs_collect_tx_data(window, rs_index, tpt, 1, 0);
946 /* If not searching for a new mode, increment failed counter
947 * ... this helps determine when to start searching again */
948 if (lq_sta->stay_in_tbl)
949 lq_sta->total_failed++;
956 * Find (by rate) the history window to update with final Tx attempt;
957 * if Tx was successful first try, use original rate,
958 * else look up the rate that was, finally, successful.
960 tx_rate = le32_to_cpu(table->rs_table[index].rate_n_flags);
961 lq_sta->last_rate_n_flags = tx_rate;
962 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
964 /* Update frame history window with "success" if Tx got ACKed ... */
965 status = !!(info->flags & IEEE80211_TX_STAT_ACK);
967 /* If type matches "search" table,
968 * add final tx status to "search" history */
969 if ((tbl_type.lq_type == search_tbl->lq_type) &&
970 (tbl_type.ant_type == search_tbl->ant_type) &&
971 (tbl_type.is_SGI == search_tbl->is_SGI)) {
972 if (search_tbl->expected_tpt)
973 tpt = search_tbl->expected_tpt[rs_index];
976 if (info->flags & IEEE80211_TX_STAT_AMPDU)
977 rs_collect_tx_data(search_win, rs_index, tpt,
978 info->status.ampdu_ack_len,
979 info->status.ampdu_ack_map);
981 rs_collect_tx_data(search_win, rs_index, tpt,
983 /* Else if type matches "current/active" table,
984 * add final tx status to "current/active" history */
985 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
986 (tbl_type.ant_type == curr_tbl->ant_type) &&
987 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
988 if (curr_tbl->expected_tpt)
989 tpt = curr_tbl->expected_tpt[rs_index];
992 if (info->flags & IEEE80211_TX_STAT_AMPDU)
993 rs_collect_tx_data(window, rs_index, tpt,
994 info->status.ampdu_ack_len,
995 info->status.ampdu_ack_map);
997 rs_collect_tx_data(window, rs_index, tpt,
1001 /* If not searching for new mode, increment success/failed counter
1002 * ... these help determine when to start searching again */
1003 if (lq_sta->stay_in_tbl) {
1004 if (info->flags & IEEE80211_TX_STAT_AMPDU) {
1005 lq_sta->total_success += info->status.ampdu_ack_map;
1006 lq_sta->total_failed +=
1007 (info->status.ampdu_ack_len - info->status.ampdu_ack_map);
1010 lq_sta->total_success++;
1012 lq_sta->total_failed++;
1016 /* See if there's a better rate or modulation mode to try. */
1017 if (sta && sta->supp_rates[sband->band])
1018 rs_rate_scale_perform(priv, skb, sta, lq_sta);
1024 * Begin a period of staying with a selected modulation mode.
1025 * Set "stay_in_tbl" flag to prevent any mode switches.
1026 * Set frame tx success limits according to legacy vs. high-throughput,
1027 * and reset overall (spanning all rates) tx success history statistics.
1028 * These control how long we stay using same modulation mode before
1029 * searching for a new mode.
1031 static void rs_set_stay_in_table(struct iwl_priv *priv, u8 is_legacy,
1032 struct iwl_lq_sta *lq_sta)
1034 IWL_DEBUG_RATE(priv, "we are staying in the same table\n");
1035 lq_sta->stay_in_tbl = 1; /* only place this gets set */
1037 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
1038 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
1039 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
1041 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
1042 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
1043 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
1045 lq_sta->table_count = 0;
1046 lq_sta->total_failed = 0;
1047 lq_sta->total_success = 0;
1048 lq_sta->flush_timer = jiffies;
1049 lq_sta->action_counter = 0;
1053 * Find correct throughput table for given mode of modulation
1055 static void rs_set_expected_tpt_table(struct iwl_lq_sta *lq_sta,
1056 struct iwl_scale_tbl_info *tbl)
1058 if (is_legacy(tbl->lq_type)) {
1059 if (!is_a_band(tbl->lq_type))
1060 tbl->expected_tpt = expected_tpt_G;
1062 tbl->expected_tpt = expected_tpt_A;
1063 } else if (is_siso(tbl->lq_type)) {
1064 if (tbl->is_ht40 && !lq_sta->is_dup)
1066 tbl->expected_tpt = expected_tpt_siso40MHzSGI;
1068 tbl->expected_tpt = expected_tpt_siso40MHz;
1069 else if (tbl->is_SGI)
1070 tbl->expected_tpt = expected_tpt_siso20MHzSGI;
1072 tbl->expected_tpt = expected_tpt_siso20MHz;
1073 } else if (is_mimo2(tbl->lq_type)) {
1074 if (tbl->is_ht40 && !lq_sta->is_dup)
1076 tbl->expected_tpt = expected_tpt_mimo2_40MHzSGI;
1078 tbl->expected_tpt = expected_tpt_mimo2_40MHz;
1079 else if (tbl->is_SGI)
1080 tbl->expected_tpt = expected_tpt_mimo2_20MHzSGI;
1082 tbl->expected_tpt = expected_tpt_mimo2_20MHz;
1083 } else if (is_mimo3(tbl->lq_type)) {
1084 if (tbl->is_ht40 && !lq_sta->is_dup)
1086 tbl->expected_tpt = expected_tpt_mimo3_40MHzSGI;
1088 tbl->expected_tpt = expected_tpt_mimo3_40MHz;
1089 else if (tbl->is_SGI)
1090 tbl->expected_tpt = expected_tpt_mimo3_20MHzSGI;
1092 tbl->expected_tpt = expected_tpt_mimo3_20MHz;
1094 tbl->expected_tpt = expected_tpt_G;
1098 * Find starting rate for new "search" high-throughput mode of modulation.
1099 * Goal is to find lowest expected rate (under perfect conditions) that is
1100 * above the current measured throughput of "active" mode, to give new mode
1101 * a fair chance to prove itself without too many challenges.
1103 * This gets called when transitioning to more aggressive modulation
1104 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
1105 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1106 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1107 * bit rate will typically need to increase, but not if performance was bad.
1109 static s32 rs_get_best_rate(struct iwl_priv *priv,
1110 struct iwl_lq_sta *lq_sta,
1111 struct iwl_scale_tbl_info *tbl, /* "search" */
1112 u16 rate_mask, s8 index)
1114 /* "active" values */
1115 struct iwl_scale_tbl_info *active_tbl =
1116 &(lq_sta->lq_info[lq_sta->active_tbl]);
1117 s32 active_sr = active_tbl->win[index].success_ratio;
1118 s32 active_tpt = active_tbl->expected_tpt[index];
1120 /* expected "search" throughput */
1121 s32 *tpt_tbl = tbl->expected_tpt;
1123 s32 new_rate, high, low, start_hi;
1127 new_rate = high = low = start_hi = IWL_RATE_INVALID;
1130 high_low = rs_get_adjacent_rate(priv, rate, rate_mask,
1133 low = high_low & 0xff;
1134 high = (high_low >> 8) & 0xff;
1137 * Lower the "search" bit rate, to give new "search" mode
1138 * approximately the same throughput as "active" if:
1140 * 1) "Active" mode has been working modestly well (but not
1141 * great), and expected "search" throughput (under perfect
1142 * conditions) at candidate rate is above the actual
1143 * measured "active" throughput (but less than expected
1144 * "active" throughput under perfect conditions).
1146 * 2) "Active" mode has been working perfectly or very well
1147 * and expected "search" throughput (under perfect
1148 * conditions) at candidate rate is above expected
1149 * "active" throughput (under perfect conditions).
1151 if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
1152 ((active_sr > IWL_RATE_DECREASE_TH) &&
1153 (active_sr <= IWL_RATE_HIGH_TH) &&
1154 (tpt_tbl[rate] <= active_tpt))) ||
1155 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
1156 (tpt_tbl[rate] > active_tpt))) {
1158 /* (2nd or later pass)
1159 * If we've already tried to raise the rate, and are
1160 * now trying to lower it, use the higher rate. */
1161 if (start_hi != IWL_RATE_INVALID) {
1162 new_rate = start_hi;
1168 /* Loop again with lower rate */
1169 if (low != IWL_RATE_INVALID)
1172 /* Lower rate not available, use the original */
1176 /* Else try to raise the "search" rate to match "active" */
1178 /* (2nd or later pass)
1179 * If we've already tried to lower the rate, and are
1180 * now trying to raise it, use the lower rate. */
1181 if (new_rate != IWL_RATE_INVALID)
1184 /* Loop again with higher rate */
1185 else if (high != IWL_RATE_INVALID) {
1189 /* Higher rate not available, use the original */
1201 * Set up search table for MIMO2
1203 static int rs_switch_to_mimo2(struct iwl_priv *priv,
1204 struct iwl_lq_sta *lq_sta,
1205 struct ieee80211_conf *conf,
1206 struct ieee80211_sta *sta,
1207 struct iwl_scale_tbl_info *tbl, int index)
1211 s8 is_green = lq_sta->is_green;
1213 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1216 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
1217 == WLAN_HT_CAP_SM_PS_STATIC)
1220 /* Need both Tx chains/antennas to support MIMO */
1221 if (priv->hw_params.tx_chains_num < 2)
1224 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO2\n");
1226 tbl->lq_type = LQ_MIMO2;
1227 tbl->is_dup = lq_sta->is_dup;
1229 tbl->max_search = IWL_MAX_SEARCH;
1230 rate_mask = lq_sta->active_mimo2_rate;
1232 if (iwl_is_ht40_tx_allowed(priv, &sta->ht_cap))
1237 rs_set_expected_tpt_table(lq_sta, tbl);
1239 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1241 IWL_DEBUG_RATE(priv, "LQ: MIMO2 best rate %d mask %X\n", rate, rate_mask);
1242 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1243 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
1247 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
1249 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
1250 tbl->current_rate, is_green);
1255 * Set up search table for MIMO3
1257 static int rs_switch_to_mimo3(struct iwl_priv *priv,
1258 struct iwl_lq_sta *lq_sta,
1259 struct ieee80211_conf *conf,
1260 struct ieee80211_sta *sta,
1261 struct iwl_scale_tbl_info *tbl, int index)
1265 s8 is_green = lq_sta->is_green;
1267 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1270 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
1271 == WLAN_HT_CAP_SM_PS_STATIC)
1274 /* Need both Tx chains/antennas to support MIMO */
1275 if (priv->hw_params.tx_chains_num < 3)
1278 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO3\n");
1280 tbl->lq_type = LQ_MIMO3;
1281 tbl->is_dup = lq_sta->is_dup;
1283 tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
1284 rate_mask = lq_sta->active_mimo3_rate;
1286 if (iwl_is_ht40_tx_allowed(priv, &sta->ht_cap))
1291 rs_set_expected_tpt_table(lq_sta, tbl);
1293 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1295 IWL_DEBUG_RATE(priv, "LQ: MIMO3 best rate %d mask %X\n",
1297 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1298 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
1302 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
1304 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
1305 tbl->current_rate, is_green);
1310 * Set up search table for SISO
1312 static int rs_switch_to_siso(struct iwl_priv *priv,
1313 struct iwl_lq_sta *lq_sta,
1314 struct ieee80211_conf *conf,
1315 struct ieee80211_sta *sta,
1316 struct iwl_scale_tbl_info *tbl, int index)
1319 u8 is_green = lq_sta->is_green;
1322 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1325 IWL_DEBUG_RATE(priv, "LQ: try to switch to SISO\n");
1327 tbl->is_dup = lq_sta->is_dup;
1328 tbl->lq_type = LQ_SISO;
1330 tbl->max_search = IWL_MAX_SEARCH;
1331 rate_mask = lq_sta->active_siso_rate;
1333 if (iwl_is_ht40_tx_allowed(priv, &sta->ht_cap))
1339 tbl->is_SGI = 0; /*11n spec: no SGI in SISO+Greenfield*/
1341 rs_set_expected_tpt_table(lq_sta, tbl);
1342 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1344 IWL_DEBUG_RATE(priv, "LQ: get best rate %d mask %X\n", rate, rate_mask);
1345 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1346 IWL_DEBUG_RATE(priv, "can not switch with index %d rate mask %x\n",
1350 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
1351 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
1352 tbl->current_rate, is_green);
1357 * Try to switch to new modulation mode from legacy
1359 static int rs_move_legacy_other(struct iwl_priv *priv,
1360 struct iwl_lq_sta *lq_sta,
1361 struct ieee80211_conf *conf,
1362 struct ieee80211_sta *sta,
1365 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1366 struct iwl_scale_tbl_info *search_tbl =
1367 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1368 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1369 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1370 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1371 u8 start_action = tbl->action;
1372 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1373 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1375 u8 update_search_tbl_counter = 0;
1377 if (!iwl_ht_enabled(priv))
1378 /* stay in Legacy */
1379 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
1380 else if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE &&
1381 tbl->action > IWL_LEGACY_SWITCH_SISO)
1382 tbl->action = IWL_LEGACY_SWITCH_SISO;
1384 lq_sta->action_counter++;
1385 switch (tbl->action) {
1386 case IWL_LEGACY_SWITCH_ANTENNA1:
1387 case IWL_LEGACY_SWITCH_ANTENNA2:
1388 IWL_DEBUG_RATE(priv, "LQ: Legacy toggle Antenna\n");
1390 if ((tbl->action == IWL_LEGACY_SWITCH_ANTENNA1 &&
1391 tx_chains_num <= 1) ||
1392 (tbl->action == IWL_LEGACY_SWITCH_ANTENNA2 &&
1393 tx_chains_num <= 2))
1396 /* Don't change antenna if success has been great */
1397 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1400 /* Set up search table to try other antenna */
1401 memcpy(search_tbl, tbl, sz);
1403 if (rs_toggle_antenna(valid_tx_ant,
1404 &search_tbl->current_rate, search_tbl)) {
1405 update_search_tbl_counter = 1;
1406 rs_set_expected_tpt_table(lq_sta, search_tbl);
1410 case IWL_LEGACY_SWITCH_SISO:
1411 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to SISO\n");
1413 /* Set up search table to try SISO */
1414 memcpy(search_tbl, tbl, sz);
1415 search_tbl->is_SGI = 0;
1416 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
1419 lq_sta->action_counter = 0;
1424 case IWL_LEGACY_SWITCH_MIMO2_AB:
1425 case IWL_LEGACY_SWITCH_MIMO2_AC:
1426 case IWL_LEGACY_SWITCH_MIMO2_BC:
1427 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO2\n");
1429 /* Set up search table to try MIMO */
1430 memcpy(search_tbl, tbl, sz);
1431 search_tbl->is_SGI = 0;
1433 if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AB)
1434 search_tbl->ant_type = ANT_AB;
1435 else if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AC)
1436 search_tbl->ant_type = ANT_AC;
1438 search_tbl->ant_type = ANT_BC;
1440 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1443 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
1446 lq_sta->action_counter = 0;
1451 case IWL_LEGACY_SWITCH_MIMO3_ABC:
1452 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO3\n");
1454 /* Set up search table to try MIMO3 */
1455 memcpy(search_tbl, tbl, sz);
1456 search_tbl->is_SGI = 0;
1458 search_tbl->ant_type = ANT_ABC;
1460 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1463 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1466 lq_sta->action_counter = 0;
1472 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
1473 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
1475 if (tbl->action == start_action)
1479 search_tbl->lq_type = LQ_NONE;
1483 lq_sta->search_better_tbl = 1;
1485 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
1486 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
1487 if (update_search_tbl_counter)
1488 search_tbl->action = tbl->action;
1494 * Try to switch to new modulation mode from SISO
1496 static int rs_move_siso_to_other(struct iwl_priv *priv,
1497 struct iwl_lq_sta *lq_sta,
1498 struct ieee80211_conf *conf,
1499 struct ieee80211_sta *sta, int index)
1501 u8 is_green = lq_sta->is_green;
1502 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1503 struct iwl_scale_tbl_info *search_tbl =
1504 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1505 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1506 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
1507 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1508 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1509 u8 start_action = tbl->action;
1510 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1511 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1512 u8 update_search_tbl_counter = 0;
1515 if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE &&
1516 tbl->action > IWL_SISO_SWITCH_ANTENNA2) {
1518 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1521 lq_sta->action_counter++;
1522 switch (tbl->action) {
1523 case IWL_SISO_SWITCH_ANTENNA1:
1524 case IWL_SISO_SWITCH_ANTENNA2:
1525 IWL_DEBUG_RATE(priv, "LQ: SISO toggle Antenna\n");
1527 if ((tbl->action == IWL_SISO_SWITCH_ANTENNA1 &&
1528 tx_chains_num <= 1) ||
1529 (tbl->action == IWL_SISO_SWITCH_ANTENNA2 &&
1530 tx_chains_num <= 2))
1533 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1536 memcpy(search_tbl, tbl, sz);
1537 if (rs_toggle_antenna(valid_tx_ant,
1538 &search_tbl->current_rate, search_tbl)) {
1539 update_search_tbl_counter = 1;
1543 case IWL_SISO_SWITCH_MIMO2_AB:
1544 case IWL_SISO_SWITCH_MIMO2_AC:
1545 case IWL_SISO_SWITCH_MIMO2_BC:
1546 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO2\n");
1547 memcpy(search_tbl, tbl, sz);
1548 search_tbl->is_SGI = 0;
1550 if (tbl->action == IWL_SISO_SWITCH_MIMO2_AB)
1551 search_tbl->ant_type = ANT_AB;
1552 else if (tbl->action == IWL_SISO_SWITCH_MIMO2_AC)
1553 search_tbl->ant_type = ANT_AC;
1555 search_tbl->ant_type = ANT_BC;
1557 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1560 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
1565 case IWL_SISO_SWITCH_GI:
1566 if (!tbl->is_ht40 && !(ht_cap->cap &
1567 IEEE80211_HT_CAP_SGI_20))
1569 if (tbl->is_ht40 && !(ht_cap->cap &
1570 IEEE80211_HT_CAP_SGI_40))
1573 IWL_DEBUG_RATE(priv, "LQ: SISO toggle SGI/NGI\n");
1575 memcpy(search_tbl, tbl, sz);
1581 "SGI was set in GF+SISO\n");
1583 search_tbl->is_SGI = !tbl->is_SGI;
1584 rs_set_expected_tpt_table(lq_sta, search_tbl);
1586 s32 tpt = lq_sta->last_tpt / 100;
1587 if (tpt >= search_tbl->expected_tpt[index])
1590 search_tbl->current_rate =
1591 rate_n_flags_from_tbl(priv, search_tbl,
1593 update_search_tbl_counter = 1;
1595 case IWL_SISO_SWITCH_MIMO3_ABC:
1596 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO3\n");
1597 memcpy(search_tbl, tbl, sz);
1598 search_tbl->is_SGI = 0;
1599 search_tbl->ant_type = ANT_ABC;
1601 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1604 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1611 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
1612 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1614 if (tbl->action == start_action)
1617 search_tbl->lq_type = LQ_NONE;
1621 lq_sta->search_better_tbl = 1;
1623 if (tbl->action > IWL_SISO_SWITCH_MIMO3_ABC)
1624 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1625 if (update_search_tbl_counter)
1626 search_tbl->action = tbl->action;
1632 * Try to switch to new modulation mode from MIMO2
1634 static int rs_move_mimo2_to_other(struct iwl_priv *priv,
1635 struct iwl_lq_sta *lq_sta,
1636 struct ieee80211_conf *conf,
1637 struct ieee80211_sta *sta, int index)
1639 s8 is_green = lq_sta->is_green;
1640 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1641 struct iwl_scale_tbl_info *search_tbl =
1642 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1643 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1644 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
1645 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1646 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1647 u8 start_action = tbl->action;
1648 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1649 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1650 u8 update_search_tbl_counter = 0;
1653 if ((iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE) &&
1654 (tbl->action < IWL_MIMO2_SWITCH_SISO_A ||
1655 tbl->action > IWL_MIMO2_SWITCH_SISO_C)) {
1656 /* switch in SISO */
1657 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1660 lq_sta->action_counter++;
1661 switch (tbl->action) {
1662 case IWL_MIMO2_SWITCH_ANTENNA1:
1663 case IWL_MIMO2_SWITCH_ANTENNA2:
1664 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle Antennas\n");
1666 if (tx_chains_num <= 2)
1669 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1672 memcpy(search_tbl, tbl, sz);
1673 if (rs_toggle_antenna(valid_tx_ant,
1674 &search_tbl->current_rate, search_tbl)) {
1675 update_search_tbl_counter = 1;
1679 case IWL_MIMO2_SWITCH_SISO_A:
1680 case IWL_MIMO2_SWITCH_SISO_B:
1681 case IWL_MIMO2_SWITCH_SISO_C:
1682 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to SISO\n");
1684 /* Set up new search table for SISO */
1685 memcpy(search_tbl, tbl, sz);
1687 if (tbl->action == IWL_MIMO2_SWITCH_SISO_A)
1688 search_tbl->ant_type = ANT_A;
1689 else if (tbl->action == IWL_MIMO2_SWITCH_SISO_B)
1690 search_tbl->ant_type = ANT_B;
1692 search_tbl->ant_type = ANT_C;
1694 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1697 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
1704 case IWL_MIMO2_SWITCH_GI:
1705 if (!tbl->is_ht40 && !(ht_cap->cap &
1706 IEEE80211_HT_CAP_SGI_20))
1708 if (tbl->is_ht40 && !(ht_cap->cap &
1709 IEEE80211_HT_CAP_SGI_40))
1712 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle SGI/NGI\n");
1714 /* Set up new search table for MIMO2 */
1715 memcpy(search_tbl, tbl, sz);
1716 search_tbl->is_SGI = !tbl->is_SGI;
1717 rs_set_expected_tpt_table(lq_sta, search_tbl);
1719 * If active table already uses the fastest possible
1720 * modulation (dual stream with short guard interval),
1721 * and it's working well, there's no need to look
1722 * for a better type of modulation!
1725 s32 tpt = lq_sta->last_tpt / 100;
1726 if (tpt >= search_tbl->expected_tpt[index])
1729 search_tbl->current_rate =
1730 rate_n_flags_from_tbl(priv, search_tbl,
1732 update_search_tbl_counter = 1;
1735 case IWL_MIMO2_SWITCH_MIMO3_ABC:
1736 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to MIMO3\n");
1737 memcpy(search_tbl, tbl, sz);
1738 search_tbl->is_SGI = 0;
1739 search_tbl->ant_type = ANT_ABC;
1741 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1744 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1752 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1753 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
1755 if (tbl->action == start_action)
1758 search_tbl->lq_type = LQ_NONE;
1761 lq_sta->search_better_tbl = 1;
1763 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1764 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
1765 if (update_search_tbl_counter)
1766 search_tbl->action = tbl->action;
1773 * Try to switch to new modulation mode from MIMO3
1775 static int rs_move_mimo3_to_other(struct iwl_priv *priv,
1776 struct iwl_lq_sta *lq_sta,
1777 struct ieee80211_conf *conf,
1778 struct ieee80211_sta *sta, int index)
1780 s8 is_green = lq_sta->is_green;
1781 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1782 struct iwl_scale_tbl_info *search_tbl =
1783 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1784 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1785 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
1786 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1787 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1788 u8 start_action = tbl->action;
1789 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1790 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1792 u8 update_search_tbl_counter = 0;
1794 if ((iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE) &&
1795 (tbl->action < IWL_MIMO3_SWITCH_SISO_A ||
1796 tbl->action > IWL_MIMO3_SWITCH_SISO_C)) {
1797 /* switch in SISO */
1798 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
1801 lq_sta->action_counter++;
1802 switch (tbl->action) {
1803 case IWL_MIMO3_SWITCH_ANTENNA1:
1804 case IWL_MIMO3_SWITCH_ANTENNA2:
1805 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle Antennas\n");
1807 if (tx_chains_num <= 3)
1810 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1813 memcpy(search_tbl, tbl, sz);
1814 if (rs_toggle_antenna(valid_tx_ant,
1815 &search_tbl->current_rate, search_tbl))
1818 case IWL_MIMO3_SWITCH_SISO_A:
1819 case IWL_MIMO3_SWITCH_SISO_B:
1820 case IWL_MIMO3_SWITCH_SISO_C:
1821 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to SISO\n");
1823 /* Set up new search table for SISO */
1824 memcpy(search_tbl, tbl, sz);
1826 if (tbl->action == IWL_MIMO3_SWITCH_SISO_A)
1827 search_tbl->ant_type = ANT_A;
1828 else if (tbl->action == IWL_MIMO3_SWITCH_SISO_B)
1829 search_tbl->ant_type = ANT_B;
1831 search_tbl->ant_type = ANT_C;
1833 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1836 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
1843 case IWL_MIMO3_SWITCH_MIMO2_AB:
1844 case IWL_MIMO3_SWITCH_MIMO2_AC:
1845 case IWL_MIMO3_SWITCH_MIMO2_BC:
1846 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to MIMO2\n");
1848 memcpy(search_tbl, tbl, sz);
1849 search_tbl->is_SGI = 0;
1850 if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AB)
1851 search_tbl->ant_type = ANT_AB;
1852 else if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AC)
1853 search_tbl->ant_type = ANT_AC;
1855 search_tbl->ant_type = ANT_BC;
1857 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1860 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
1867 case IWL_MIMO3_SWITCH_GI:
1868 if (!tbl->is_ht40 && !(ht_cap->cap &
1869 IEEE80211_HT_CAP_SGI_20))
1871 if (tbl->is_ht40 && !(ht_cap->cap &
1872 IEEE80211_HT_CAP_SGI_40))
1875 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle SGI/NGI\n");
1877 /* Set up new search table for MIMO */
1878 memcpy(search_tbl, tbl, sz);
1879 search_tbl->is_SGI = !tbl->is_SGI;
1880 rs_set_expected_tpt_table(lq_sta, search_tbl);
1882 * If active table already uses the fastest possible
1883 * modulation (dual stream with short guard interval),
1884 * and it's working well, there's no need to look
1885 * for a better type of modulation!
1888 s32 tpt = lq_sta->last_tpt / 100;
1889 if (tpt >= search_tbl->expected_tpt[index])
1892 search_tbl->current_rate =
1893 rate_n_flags_from_tbl(priv, search_tbl,
1895 update_search_tbl_counter = 1;
1899 if (tbl->action > IWL_MIMO3_SWITCH_GI)
1900 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
1902 if (tbl->action == start_action)
1905 search_tbl->lq_type = LQ_NONE;
1908 lq_sta->search_better_tbl = 1;
1910 if (tbl->action > IWL_MIMO3_SWITCH_GI)
1911 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
1912 if (update_search_tbl_counter)
1913 search_tbl->action = tbl->action;
1920 * Check whether we should continue using same modulation mode, or
1921 * begin search for a new mode, based on:
1922 * 1) # tx successes or failures while using this mode
1923 * 2) # times calling this function
1924 * 3) elapsed time in this mode (not used, for now)
1926 static void rs_stay_in_table(struct iwl_lq_sta *lq_sta)
1928 struct iwl_scale_tbl_info *tbl;
1931 int flush_interval_passed = 0;
1932 struct iwl_priv *priv;
1935 active_tbl = lq_sta->active_tbl;
1937 tbl = &(lq_sta->lq_info[active_tbl]);
1939 /* If we've been disallowing search, see if we should now allow it */
1940 if (lq_sta->stay_in_tbl) {
1942 /* Elapsed time using current modulation mode */
1943 if (lq_sta->flush_timer)
1944 flush_interval_passed =
1946 (unsigned long)(lq_sta->flush_timer +
1947 IWL_RATE_SCALE_FLUSH_INTVL));
1950 * Check if we should allow search for new modulation mode.
1951 * If many frames have failed or succeeded, or we've used
1952 * this same modulation for a long time, allow search, and
1953 * reset history stats that keep track of whether we should
1954 * allow a new search. Also (below) reset all bitmaps and
1955 * stats in active history.
1957 if ((lq_sta->total_failed > lq_sta->max_failure_limit) ||
1958 (lq_sta->total_success > lq_sta->max_success_limit) ||
1959 ((!lq_sta->search_better_tbl) && (lq_sta->flush_timer)
1960 && (flush_interval_passed))) {
1961 IWL_DEBUG_RATE(priv, "LQ: stay is expired %d %d %d\n:",
1962 lq_sta->total_failed,
1963 lq_sta->total_success,
1964 flush_interval_passed);
1966 /* Allow search for new mode */
1967 lq_sta->stay_in_tbl = 0; /* only place reset */
1968 lq_sta->total_failed = 0;
1969 lq_sta->total_success = 0;
1970 lq_sta->flush_timer = 0;
1973 * Else if we've used this modulation mode enough repetitions
1974 * (regardless of elapsed time or success/failure), reset
1975 * history bitmaps and rate-specific stats for all rates in
1979 lq_sta->table_count++;
1980 if (lq_sta->table_count >=
1981 lq_sta->table_count_limit) {
1982 lq_sta->table_count = 0;
1984 IWL_DEBUG_RATE(priv, "LQ: stay in table clear win\n");
1985 for (i = 0; i < IWL_RATE_COUNT; i++)
1986 rs_rate_scale_clear_window(
1991 /* If transitioning to allow "search", reset all history
1992 * bitmaps and stats in active table (this will become the new
1993 * "search" table). */
1994 if (!lq_sta->stay_in_tbl) {
1995 for (i = 0; i < IWL_RATE_COUNT; i++)
1996 rs_rate_scale_clear_window(&(tbl->win[i]));
2002 * setup rate table in uCode
2003 * return rate_n_flags as used in the table
2005 static u32 rs_update_rate_tbl(struct iwl_priv *priv,
2006 struct iwl_lq_sta *lq_sta,
2007 struct iwl_scale_tbl_info *tbl,
2008 int index, u8 is_green)
2012 /* Update uCode's rate table. */
2013 rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
2014 rs_fill_link_cmd(priv, lq_sta, rate);
2015 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
2021 * Do rate scaling and search for new modulation mode.
2023 static void rs_rate_scale_perform(struct iwl_priv *priv,
2024 struct sk_buff *skb,
2025 struct ieee80211_sta *sta,
2026 struct iwl_lq_sta *lq_sta)
2028 struct ieee80211_hw *hw = priv->hw;
2029 struct ieee80211_conf *conf = &hw->conf;
2030 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2031 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2032 int low = IWL_RATE_INVALID;
2033 int high = IWL_RATE_INVALID;
2036 struct iwl_rate_scale_data *window = NULL;
2037 int current_tpt = IWL_INVALID_VALUE;
2038 int low_tpt = IWL_INVALID_VALUE;
2039 int high_tpt = IWL_INVALID_VALUE;
2041 s8 scale_action = 0;
2044 struct iwl_scale_tbl_info *tbl, *tbl1;
2045 u16 rate_scale_index_msk = 0;
2052 u8 tid = MAX_TID_COUNT;
2053 struct iwl_tid_data *tid_data;
2055 IWL_DEBUG_RATE(priv, "rate scale calculate new rate for skb\n");
2057 /* Send management frames and NO_ACK data using lowest rate. */
2058 /* TODO: this could probably be improved.. */
2059 if (!ieee80211_is_data(hdr->frame_control) ||
2060 info->flags & IEEE80211_TX_CTL_NO_ACK)
2063 if (!sta || !lq_sta)
2066 lq_sta->supp_rates = sta->supp_rates[lq_sta->band];
2068 tid = rs_tl_add_packet(lq_sta, hdr);
2071 * Select rate-scale / modulation-mode table to work with in
2072 * the rest of this function: "search" if searching for better
2073 * modulation mode, or "active" if doing rate scaling within a mode.
2075 if (!lq_sta->search_better_tbl)
2076 active_tbl = lq_sta->active_tbl;
2078 active_tbl = 1 - lq_sta->active_tbl;
2080 tbl = &(lq_sta->lq_info[active_tbl]);
2081 if (is_legacy(tbl->lq_type))
2082 lq_sta->is_green = 0;
2084 lq_sta->is_green = rs_use_green(sta, &priv->current_ht_config);
2085 is_green = lq_sta->is_green;
2087 /* current tx rate */
2088 index = lq_sta->last_txrate_idx;
2090 IWL_DEBUG_RATE(priv, "Rate scale index %d for type %d\n", index,
2093 /* rates available for this association, and for modulation mode */
2094 rate_mask = rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
2096 IWL_DEBUG_RATE(priv, "mask 0x%04X \n", rate_mask);
2098 /* mask with station rate restriction */
2099 if (is_legacy(tbl->lq_type)) {
2100 if (lq_sta->band == IEEE80211_BAND_5GHZ)
2101 /* supp_rates has no CCK bits in A mode */
2102 rate_scale_index_msk = (u16) (rate_mask &
2103 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
2105 rate_scale_index_msk = (u16) (rate_mask &
2106 lq_sta->supp_rates);
2109 rate_scale_index_msk = rate_mask;
2111 if (!rate_scale_index_msk)
2112 rate_scale_index_msk = rate_mask;
2114 if (!((1 << index) & rate_scale_index_msk)) {
2115 IWL_ERR(priv, "Current Rate is not valid\n");
2116 if (lq_sta->search_better_tbl) {
2117 /* revert to active table if search table is not valid*/
2118 tbl->lq_type = LQ_NONE;
2119 lq_sta->search_better_tbl = 0;
2120 tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
2121 /* get "active" rate info */
2122 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
2123 rate = rs_update_rate_tbl(priv, lq_sta,
2124 tbl, index, is_green);
2129 /* Get expected throughput table and history window for current rate */
2130 if (!tbl->expected_tpt) {
2131 IWL_ERR(priv, "tbl->expected_tpt is NULL\n");
2135 /* force user max rate if set by user */
2136 if ((lq_sta->max_rate_idx != -1) &&
2137 (lq_sta->max_rate_idx < index)) {
2138 index = lq_sta->max_rate_idx;
2140 window = &(tbl->win[index]);
2144 window = &(tbl->win[index]);
2147 * If there is not enough history to calculate actual average
2148 * throughput, keep analyzing results of more tx frames, without
2149 * changing rate or mode (bypass most of the rest of this function).
2150 * Set up new rate table in uCode only if old rate is not supported
2151 * in current association (use new rate found above).
2153 fail_count = window->counter - window->success_counter;
2154 if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
2155 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
2156 IWL_DEBUG_RATE(priv, "LQ: still below TH. succ=%d total=%d "
2158 window->success_counter, window->counter, index);
2160 /* Can't calculate this yet; not enough history */
2161 window->average_tpt = IWL_INVALID_VALUE;
2163 /* Should we stay with this modulation mode,
2164 * or search for a new one? */
2165 rs_stay_in_table(lq_sta);
2170 /* Else we have enough samples; calculate estimate of
2171 * actual average throughput */
2173 BUG_ON(window->average_tpt != ((window->success_ratio *
2174 tbl->expected_tpt[index] + 64) / 128));
2176 /* If we are searching for better modulation mode, check success. */
2177 if (lq_sta->search_better_tbl &&
2178 (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_MULTI)) {
2179 /* If good success, continue using the "search" mode;
2180 * no need to send new link quality command, since we're
2181 * continuing to use the setup that we've been trying. */
2182 if (window->average_tpt > lq_sta->last_tpt) {
2184 IWL_DEBUG_RATE(priv, "LQ: SWITCHING TO NEW TABLE "
2185 "suc=%d cur-tpt=%d old-tpt=%d\n",
2186 window->success_ratio,
2187 window->average_tpt,
2190 if (!is_legacy(tbl->lq_type))
2191 lq_sta->enable_counter = 1;
2193 /* Swap tables; "search" becomes "active" */
2194 lq_sta->active_tbl = active_tbl;
2195 current_tpt = window->average_tpt;
2197 /* Else poor success; go back to mode in "active" table */
2200 IWL_DEBUG_RATE(priv, "LQ: GOING BACK TO THE OLD TABLE "
2201 "suc=%d cur-tpt=%d old-tpt=%d\n",
2202 window->success_ratio,
2203 window->average_tpt,
2206 /* Nullify "search" table */
2207 tbl->lq_type = LQ_NONE;
2209 /* Revert to "active" table */
2210 active_tbl = lq_sta->active_tbl;
2211 tbl = &(lq_sta->lq_info[active_tbl]);
2213 /* Revert to "active" rate and throughput info */
2214 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
2215 current_tpt = lq_sta->last_tpt;
2217 /* Need to set up a new rate table in uCode */
2221 /* Either way, we've made a decision; modulation mode
2222 * search is done, allow rate adjustment next time. */
2223 lq_sta->search_better_tbl = 0;
2224 done_search = 1; /* Don't switch modes below! */
2228 /* (Else) not in search of better modulation mode, try for better
2229 * starting rate, while staying in this mode. */
2230 high_low = rs_get_adjacent_rate(priv, index, rate_scale_index_msk,
2232 low = high_low & 0xff;
2233 high = (high_low >> 8) & 0xff;
2235 /* If user set max rate, dont allow higher than user constrain */
2236 if ((lq_sta->max_rate_idx != -1) &&
2237 (lq_sta->max_rate_idx < high))
2238 high = IWL_RATE_INVALID;
2240 sr = window->success_ratio;
2242 /* Collect measured throughputs for current and adjacent rates */
2243 current_tpt = window->average_tpt;
2244 if (low != IWL_RATE_INVALID)
2245 low_tpt = tbl->win[low].average_tpt;
2246 if (high != IWL_RATE_INVALID)
2247 high_tpt = tbl->win[high].average_tpt;
2251 /* Too many failures, decrease rate */
2252 if ((sr <= IWL_RATE_DECREASE_TH) || (current_tpt == 0)) {
2253 IWL_DEBUG_RATE(priv, "decrease rate because of low success_ratio\n");
2256 /* No throughput measured yet for adjacent rates; try increase. */
2257 } else if ((low_tpt == IWL_INVALID_VALUE) &&
2258 (high_tpt == IWL_INVALID_VALUE)) {
2260 if (high != IWL_RATE_INVALID && sr >= IWL_RATE_INCREASE_TH)
2262 else if (low != IWL_RATE_INVALID)
2266 /* Both adjacent throughputs are measured, but neither one has better
2267 * throughput; we're using the best rate, don't change it! */
2268 else if ((low_tpt != IWL_INVALID_VALUE) &&
2269 (high_tpt != IWL_INVALID_VALUE) &&
2270 (low_tpt < current_tpt) &&
2271 (high_tpt < current_tpt))
2274 /* At least one adjacent rate's throughput is measured,
2275 * and may have better performance. */
2277 /* Higher adjacent rate's throughput is measured */
2278 if (high_tpt != IWL_INVALID_VALUE) {
2279 /* Higher rate has better throughput */
2280 if (high_tpt > current_tpt &&
2281 sr >= IWL_RATE_INCREASE_TH) {
2287 /* Lower adjacent rate's throughput is measured */
2288 } else if (low_tpt != IWL_INVALID_VALUE) {
2289 /* Lower rate has better throughput */
2290 if (low_tpt > current_tpt) {
2291 IWL_DEBUG_RATE(priv,
2292 "decrease rate because of low tpt\n");
2294 } else if (sr >= IWL_RATE_INCREASE_TH) {
2300 /* Sanity check; asked for decrease, but success rate or throughput
2301 * has been good at old rate. Don't change it. */
2302 if ((scale_action == -1) && (low != IWL_RATE_INVALID) &&
2303 ((sr > IWL_RATE_HIGH_TH) ||
2304 (current_tpt > (100 * tbl->expected_tpt[low]))))
2306 if (!iwl_ht_enabled(priv) && !is_legacy(tbl->lq_type))
2308 if (iwl_tx_ant_restriction(priv) != IWL_ANT_OK_MULTI &&
2309 (is_mimo2(tbl->lq_type) || is_mimo3(tbl->lq_type)))
2311 switch (scale_action) {
2313 /* Decrease starting rate, update uCode's rate table */
2314 if (low != IWL_RATE_INVALID) {
2321 /* Increase starting rate, update uCode's rate table */
2322 if (high != IWL_RATE_INVALID) {
2334 IWL_DEBUG_RATE(priv, "choose rate scale index %d action %d low %d "
2335 "high %d type %d\n",
2336 index, scale_action, low, high, tbl->lq_type);
2339 /* Replace uCode's rate table for the destination station. */
2341 rate = rs_update_rate_tbl(priv, lq_sta,
2342 tbl, index, is_green);
2344 if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_MULTI) {
2345 /* Should we stay with this modulation mode,
2346 * or search for a new one? */
2347 rs_stay_in_table(lq_sta);
2350 * Search for new modulation mode if we're:
2351 * 1) Not changing rates right now
2352 * 2) Not just finishing up a search
2353 * 3) Allowing a new search
2355 if (!update_lq && !done_search && !lq_sta->stay_in_tbl && window->counter) {
2356 /* Save current throughput to compare with "search" throughput*/
2357 lq_sta->last_tpt = current_tpt;
2359 /* Select a new "search" modulation mode to try.
2360 * If one is found, set up the new "search" table. */
2361 if (is_legacy(tbl->lq_type))
2362 rs_move_legacy_other(priv, lq_sta, conf, sta, index);
2363 else if (is_siso(tbl->lq_type))
2364 rs_move_siso_to_other(priv, lq_sta, conf, sta, index);
2365 else if (is_mimo2(tbl->lq_type))
2366 rs_move_mimo2_to_other(priv, lq_sta, conf, sta, index);
2368 rs_move_mimo3_to_other(priv, lq_sta, conf, sta, index);
2370 /* If new "search" mode was selected, set up in uCode table */
2371 if (lq_sta->search_better_tbl) {
2372 /* Access the "search" table, clear its history. */
2373 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
2374 for (i = 0; i < IWL_RATE_COUNT; i++)
2375 rs_rate_scale_clear_window(&(tbl->win[i]));
2377 /* Use new "search" start rate */
2378 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
2380 IWL_DEBUG_RATE(priv, "Switch current mcs: %X index: %d\n",
2381 tbl->current_rate, index);
2382 rs_fill_link_cmd(priv, lq_sta, tbl->current_rate);
2383 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
2388 if (done_search && !lq_sta->stay_in_tbl) {
2389 /* If the "active" (non-search) mode was legacy,
2390 * and we've tried switching antennas,
2391 * but we haven't been able to try HT modes (not available),
2392 * stay with best antenna legacy modulation for a while
2393 * before next round of mode comparisons. */
2394 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
2395 if (is_legacy(tbl1->lq_type) && !conf_is_ht(conf) &&
2396 lq_sta->action_counter > tbl1->max_search) {
2397 IWL_DEBUG_RATE(priv, "LQ: STAY in legacy table\n");
2398 rs_set_stay_in_table(priv, 1, lq_sta);
2401 /* If we're in an HT mode, and all 3 mode switch actions
2402 * have been tried and compared, stay in this best modulation
2403 * mode for a while before next round of mode comparisons. */
2404 if (lq_sta->enable_counter &&
2405 (lq_sta->action_counter >= tbl1->max_search) &&
2406 iwl_ht_enabled(priv)) {
2407 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2408 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
2409 (tid != MAX_TID_COUNT)) {
2411 &priv->stations[lq_sta->lq.sta_id].tid[tid];
2412 if (tid_data->agg.state == IWL_AGG_OFF) {
2413 IWL_DEBUG_RATE(priv,
2414 "try to aggregate tid %d\n",
2416 rs_tl_turn_on_agg(priv, tid,
2420 rs_set_stay_in_table(priv, 0, lq_sta);
2425 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
2427 lq_sta->last_txrate_idx = i;
2433 static void rs_initialize_lq(struct iwl_priv *priv,
2434 struct ieee80211_conf *conf,
2435 struct ieee80211_sta *sta,
2436 struct iwl_lq_sta *lq_sta)
2438 struct iwl_scale_tbl_info *tbl;
2442 u8 use_green = rs_use_green(sta, &priv->current_ht_config);
2446 if (!sta || !lq_sta)
2449 i = lq_sta->last_txrate_idx;
2451 if ((lq_sta->lq.sta_id == 0xff) &&
2452 (priv->iw_mode == NL80211_IFTYPE_ADHOC))
2455 valid_tx_ant = priv->hw_params.valid_tx_ant;
2457 if (!lq_sta->search_better_tbl)
2458 active_tbl = lq_sta->active_tbl;
2460 active_tbl = 1 - lq_sta->active_tbl;
2462 tbl = &(lq_sta->lq_info[active_tbl]);
2464 if ((i < 0) || (i >= IWL_RATE_COUNT))
2467 rate = iwl_rates[i].plcp;
2468 tbl->ant_type = first_antenna(valid_tx_ant);
2469 rate |= tbl->ant_type << RATE_MCS_ANT_POS;
2471 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
2472 rate |= RATE_MCS_CCK_MSK;
2474 rs_get_tbl_info_from_mcs(rate, priv->band, tbl, &rate_idx);
2475 if (!rs_is_valid_ant(valid_tx_ant, tbl->ant_type))
2476 rs_toggle_antenna(valid_tx_ant, &rate, tbl);
2478 rate = rate_n_flags_from_tbl(priv, tbl, rate_idx, use_green);
2479 tbl->current_rate = rate;
2480 rs_set_expected_tpt_table(lq_sta, tbl);
2481 rs_fill_link_cmd(NULL, lq_sta, rate);
2482 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
2487 static void rs_get_rate(void *priv_r, struct ieee80211_sta *sta, void *priv_sta,
2488 struct ieee80211_tx_rate_control *txrc)
2491 struct sk_buff *skb = txrc->skb;
2492 struct ieee80211_supported_band *sband = txrc->sband;
2493 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
2494 struct ieee80211_conf *conf = &priv->hw->conf;
2495 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
2496 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2497 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2498 struct iwl_lq_sta *lq_sta = priv_sta;
2501 IWL_DEBUG_RATE_LIMIT(priv, "rate scale calculate new rate for skb\n");
2503 /* Get max rate if user set max rate */
2505 lq_sta->max_rate_idx = txrc->max_rate_idx;
2506 if ((sband->band == IEEE80211_BAND_5GHZ) &&
2507 (lq_sta->max_rate_idx != -1))
2508 lq_sta->max_rate_idx += IWL_FIRST_OFDM_RATE;
2509 if ((lq_sta->max_rate_idx < 0) ||
2510 (lq_sta->max_rate_idx >= IWL_RATE_COUNT))
2511 lq_sta->max_rate_idx = -1;
2514 /* Send management frames and NO_ACK data using lowest rate. */
2515 if (rate_control_send_low(sta, priv_sta, txrc))
2518 rate_idx = lq_sta->last_txrate_idx;
2520 if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) &&
2521 !lq_sta->ibss_sta_added) {
2522 u8 sta_id = iwl_find_station(priv, hdr->addr1);
2524 if (sta_id == IWL_INVALID_STATION) {
2525 IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n",
2527 sta_id = iwl_add_station(priv, hdr->addr1,
2528 false, CMD_ASYNC, ht_cap);
2530 if ((sta_id != IWL_INVALID_STATION)) {
2531 lq_sta->lq.sta_id = sta_id;
2532 lq_sta->lq.rs_table[0].rate_n_flags = 0;
2533 lq_sta->ibss_sta_added = 1;
2534 rs_initialize_lq(priv, conf, sta, lq_sta);
2538 if (lq_sta->last_rate_n_flags & RATE_MCS_HT_MSK) {
2539 rate_idx -= IWL_FIRST_OFDM_RATE;
2540 /* 6M and 9M shared same MCS index */
2541 rate_idx = (rate_idx > 0) ? (rate_idx - 1) : 0;
2542 if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
2543 IWL_RATE_MIMO3_6M_PLCP)
2544 rate_idx = rate_idx + (2 * MCS_INDEX_PER_STREAM);
2545 else if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
2546 IWL_RATE_MIMO2_6M_PLCP)
2547 rate_idx = rate_idx + MCS_INDEX_PER_STREAM;
2548 info->control.rates[0].flags = IEEE80211_TX_RC_MCS;
2549 if (lq_sta->last_rate_n_flags & RATE_MCS_SGI_MSK)
2550 info->control.rates[0].flags |= IEEE80211_TX_RC_SHORT_GI;
2551 if (lq_sta->last_rate_n_flags & RATE_MCS_DUP_MSK)
2552 info->control.rates[0].flags |= IEEE80211_TX_RC_DUP_DATA;
2553 if (lq_sta->last_rate_n_flags & RATE_MCS_HT40_MSK)
2554 info->control.rates[0].flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
2555 if (lq_sta->last_rate_n_flags & RATE_MCS_GF_MSK)
2556 info->control.rates[0].flags |= IEEE80211_TX_RC_GREEN_FIELD;
2558 /* Check for invalid rates */
2559 if ((rate_idx < 0) || (rate_idx >= IWL_RATE_COUNT_LEGACY) ||
2560 ((sband->band == IEEE80211_BAND_5GHZ) &&
2561 (rate_idx < IWL_FIRST_OFDM_RATE)))
2562 rate_idx = rate_lowest_index(sband, sta);
2563 /* On valid 5 GHz rate, adjust index */
2564 else if (sband->band == IEEE80211_BAND_5GHZ)
2565 rate_idx -= IWL_FIRST_OFDM_RATE;
2566 info->control.rates[0].flags = 0;
2568 info->control.rates[0].idx = rate_idx;
2572 static void *rs_alloc_sta(void *priv_rate, struct ieee80211_sta *sta,
2575 struct iwl_lq_sta *lq_sta;
2576 struct iwl_priv *priv;
2579 priv = (struct iwl_priv *)priv_rate;
2580 IWL_DEBUG_RATE(priv, "create station rate scale window\n");
2582 lq_sta = kzalloc(sizeof(struct iwl_lq_sta), gfp);
2586 lq_sta->lq.sta_id = 0xff;
2589 for (j = 0; j < LQ_SIZE; j++)
2590 for (i = 0; i < IWL_RATE_COUNT; i++)
2591 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
2596 static void rs_rate_init(void *priv_r, struct ieee80211_supported_band *sband,
2597 struct ieee80211_sta *sta, void *priv_sta)
2600 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
2601 struct ieee80211_conf *conf = &priv->hw->conf;
2602 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
2603 struct iwl_lq_sta *lq_sta = priv_sta;
2605 lq_sta->flush_timer = 0;
2606 lq_sta->supp_rates = sta->supp_rates[sband->band];
2607 for (j = 0; j < LQ_SIZE; j++)
2608 for (i = 0; i < IWL_RATE_COUNT; i++)
2609 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
2611 IWL_DEBUG_RATE(priv, "LQ: *** rate scale station global init ***\n");
2612 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2613 * the lowest or the highest rate.. Could consider using RSSI from
2614 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2617 lq_sta->ibss_sta_added = 0;
2618 if (priv->iw_mode == NL80211_IFTYPE_AP) {
2619 u8 sta_id = iwl_find_station(priv,
2622 /* for IBSS the call are from tasklet */
2623 IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n", sta->addr);
2625 if (sta_id == IWL_INVALID_STATION) {
2626 IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n", sta->addr);
2627 sta_id = iwl_add_station(priv, sta->addr, false,
2630 if ((sta_id != IWL_INVALID_STATION)) {
2631 lq_sta->lq.sta_id = sta_id;
2632 lq_sta->lq.rs_table[0].rate_n_flags = 0;
2634 /* FIXME: this is w/a remove it later */
2635 priv->assoc_station_added = 1;
2639 lq_sta->max_rate_idx = -1;
2640 lq_sta->missed_rate_counter = IWL_MISSED_RATE_MAX;
2641 lq_sta->is_green = rs_use_green(sta, &priv->current_ht_config);
2642 lq_sta->active_legacy_rate = priv->active_rate & ~(0x1000);
2643 lq_sta->active_rate_basic = priv->active_rate_basic;
2644 lq_sta->band = priv->band;
2646 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2647 * supp_rates[] does not; shift to convert format, force 9 MBits off.
2649 lq_sta->active_siso_rate = ht_cap->mcs.rx_mask[0] << 1;
2650 lq_sta->active_siso_rate |= ht_cap->mcs.rx_mask[0] & 0x1;
2651 lq_sta->active_siso_rate &= ~((u16)0x2);
2652 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
2655 lq_sta->active_mimo2_rate = ht_cap->mcs.rx_mask[1] << 1;
2656 lq_sta->active_mimo2_rate |= ht_cap->mcs.rx_mask[1] & 0x1;
2657 lq_sta->active_mimo2_rate &= ~((u16)0x2);
2658 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2660 lq_sta->active_mimo3_rate = ht_cap->mcs.rx_mask[2] << 1;
2661 lq_sta->active_mimo3_rate |= ht_cap->mcs.rx_mask[2] & 0x1;
2662 lq_sta->active_mimo3_rate &= ~((u16)0x2);
2663 lq_sta->active_mimo3_rate <<= IWL_FIRST_OFDM_RATE;
2665 IWL_DEBUG_RATE(priv, "SISO-RATE=%X MIMO2-RATE=%X MIMO3-RATE=%X\n",
2666 lq_sta->active_siso_rate,
2667 lq_sta->active_mimo2_rate,
2668 lq_sta->active_mimo3_rate);
2670 /* These values will be overridden later */
2671 lq_sta->lq.general_params.single_stream_ant_msk = ANT_A;
2672 lq_sta->lq.general_params.dual_stream_ant_msk = ANT_AB;
2674 /* as default allow aggregation for all tids */
2675 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
2678 /* Set last_txrate_idx to lowest rate */
2679 lq_sta->last_txrate_idx = rate_lowest_index(sband, sta);
2680 if (sband->band == IEEE80211_BAND_5GHZ)
2681 lq_sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
2683 rs_initialize_lq(priv, conf, sta, lq_sta);
2686 static void rs_fill_link_cmd(struct iwl_priv *priv,
2687 struct iwl_lq_sta *lq_sta, u32 new_rate)
2689 struct iwl_scale_tbl_info tbl_type;
2692 int repeat_rate = 0;
2693 u8 ant_toggle_cnt = 0;
2694 u8 use_ht_possible = 1;
2695 u8 valid_tx_ant = 0;
2696 struct iwl_link_quality_cmd *lq_cmd = &lq_sta->lq;
2698 /* Override starting rate (index 0) if needed for debug purposes */
2699 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
2701 /* Interpret new_rate (rate_n_flags) */
2702 memset(&tbl_type, 0, sizeof(tbl_type));
2703 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
2704 &tbl_type, &rate_idx);
2706 /* How many times should we repeat the initial rate? */
2707 if (is_legacy(tbl_type.lq_type)) {
2709 repeat_rate = IWL_NUMBER_TRY;
2711 repeat_rate = IWL_HT_NUMBER_TRY;
2714 lq_cmd->general_params.mimo_delimiter =
2715 is_mimo(tbl_type.lq_type) ? 1 : 0;
2717 /* Fill 1st table entry (index 0) */
2718 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
2720 if (num_of_ant(tbl_type.ant_type) == 1) {
2721 lq_cmd->general_params.single_stream_ant_msk =
2723 } else if (num_of_ant(tbl_type.ant_type) == 2) {
2724 lq_cmd->general_params.dual_stream_ant_msk =
2726 } /* otherwise we don't modify the existing value */
2732 valid_tx_ant = priv->hw_params.valid_tx_ant;
2734 /* Fill rest of rate table */
2735 while (index < LINK_QUAL_MAX_RETRY_NUM) {
2736 /* Repeat initial/next rate.
2737 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2738 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
2739 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
2740 if (is_legacy(tbl_type.lq_type)) {
2741 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2744 rs_toggle_antenna(valid_tx_ant,
2745 &new_rate, &tbl_type))
2749 /* Override next rate if needed for debug purposes */
2750 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
2752 /* Fill next table entry */
2753 lq_cmd->rs_table[index].rate_n_flags =
2754 cpu_to_le32(new_rate);
2759 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band, &tbl_type,
2762 /* Indicate to uCode which entries might be MIMO.
2763 * If initial rate was MIMO, this will finally end up
2764 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
2765 if (is_mimo(tbl_type.lq_type))
2766 lq_cmd->general_params.mimo_delimiter = index;
2769 new_rate = rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
2772 /* How many times should we repeat the next rate? */
2773 if (is_legacy(tbl_type.lq_type)) {
2774 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2777 rs_toggle_antenna(valid_tx_ant,
2778 &new_rate, &tbl_type))
2781 repeat_rate = IWL_NUMBER_TRY;
2783 repeat_rate = IWL_HT_NUMBER_TRY;
2786 /* Don't allow HT rates after next pass.
2787 * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
2788 use_ht_possible = 0;
2790 /* Override next rate if needed for debug purposes */
2791 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
2793 /* Fill next table entry */
2794 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
2800 lq_cmd->agg_params.agg_frame_cnt_limit = LINK_QUAL_AGG_FRAME_LIMIT_MAX;
2801 lq_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
2802 lq_cmd->agg_params.agg_time_limit =
2803 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
2806 static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
2810 /* rate scale requires free function to be implemented */
2811 static void rs_free(void *priv_rate)
2816 static void rs_free_sta(void *priv_r, struct ieee80211_sta *sta,
2819 struct iwl_lq_sta *lq_sta = priv_sta;
2820 struct iwl_priv *priv __maybe_unused = priv_r;
2822 IWL_DEBUG_RATE(priv, "enter\n");
2824 IWL_DEBUG_RATE(priv, "leave\n");
2828 #ifdef CONFIG_MAC80211_DEBUGFS
2829 static int open_file_generic(struct inode *inode, struct file *file)
2831 file->private_data = inode->i_private;
2834 static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
2835 u32 *rate_n_flags, int index)
2837 struct iwl_priv *priv;
2842 valid_tx_ant = priv->hw_params.valid_tx_ant;
2843 if (lq_sta->dbg_fixed_rate) {
2845 ((lq_sta->dbg_fixed_rate & RATE_MCS_ANT_ABC_MSK)
2846 >> RATE_MCS_ANT_POS);
2847 if ((valid_tx_ant & ant_sel_tx) == ant_sel_tx) {
2848 *rate_n_flags = lq_sta->dbg_fixed_rate;
2849 IWL_DEBUG_RATE(priv, "Fixed rate ON\n");
2851 lq_sta->dbg_fixed_rate = 0;
2853 "Invalid antenna selection 0x%X, Valid is 0x%X\n",
2854 ant_sel_tx, valid_tx_ant);
2855 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
2858 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
2862 static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
2863 const char __user *user_buf, size_t count, loff_t *ppos)
2865 struct iwl_lq_sta *lq_sta = file->private_data;
2866 struct iwl_priv *priv;
2872 memset(buf, 0, sizeof(buf));
2873 buf_size = min(count, sizeof(buf) - 1);
2874 if (copy_from_user(buf, user_buf, buf_size))
2877 if (sscanf(buf, "%x", &parsed_rate) == 1)
2878 lq_sta->dbg_fixed_rate = parsed_rate;
2880 lq_sta->dbg_fixed_rate = 0;
2882 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
2883 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2884 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2885 lq_sta->active_mimo3_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2887 IWL_DEBUG_RATE(priv, "sta_id %d rate 0x%X\n",
2888 lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
2890 if (lq_sta->dbg_fixed_rate) {
2891 rs_fill_link_cmd(NULL, lq_sta, lq_sta->dbg_fixed_rate);
2892 iwl_send_lq_cmd(lq_sta->drv, &lq_sta->lq, CMD_ASYNC);
2898 static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
2899 char __user *user_buf, size_t count, loff_t *ppos)
2907 struct iwl_lq_sta *lq_sta = file->private_data;
2908 struct iwl_priv *priv;
2909 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
2912 buff = kmalloc(1024, GFP_KERNEL);
2916 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
2917 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
2918 lq_sta->total_failed, lq_sta->total_success,
2919 lq_sta->active_legacy_rate);
2920 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
2921 lq_sta->dbg_fixed_rate);
2922 desc += sprintf(buff+desc, "valid_tx_ant %s%s%s\n",
2923 (priv->hw_params.valid_tx_ant & ANT_A) ? "ANT_A," : "",
2924 (priv->hw_params.valid_tx_ant & ANT_B) ? "ANT_B," : "",
2925 (priv->hw_params.valid_tx_ant & ANT_C) ? "ANT_C" : "");
2926 desc += sprintf(buff+desc, "lq type %s\n",
2927 (is_legacy(tbl->lq_type)) ? "legacy" : "HT");
2928 if (is_Ht(tbl->lq_type)) {
2929 desc += sprintf(buff+desc, " %s",
2930 (is_siso(tbl->lq_type)) ? "SISO" :
2931 ((is_mimo2(tbl->lq_type)) ? "MIMO2" : "MIMO3"));
2932 desc += sprintf(buff+desc, " %s",
2933 (tbl->is_ht40) ? "40MHz" : "20MHz");
2934 desc += sprintf(buff+desc, " %s %s\n", (tbl->is_SGI) ? "SGI" : "",
2935 (lq_sta->is_green) ? "GF enabled" : "");
2937 desc += sprintf(buff+desc, "last tx rate=0x%X\n",
2938 lq_sta->last_rate_n_flags);
2939 desc += sprintf(buff+desc, "general:"
2940 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
2941 lq_sta->lq.general_params.flags,
2942 lq_sta->lq.general_params.mimo_delimiter,
2943 lq_sta->lq.general_params.single_stream_ant_msk,
2944 lq_sta->lq.general_params.dual_stream_ant_msk);
2946 desc += sprintf(buff+desc, "agg:"
2947 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
2948 le16_to_cpu(lq_sta->lq.agg_params.agg_time_limit),
2949 lq_sta->lq.agg_params.agg_dis_start_th,
2950 lq_sta->lq.agg_params.agg_frame_cnt_limit);
2952 desc += sprintf(buff+desc,
2953 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
2954 lq_sta->lq.general_params.start_rate_index[0],
2955 lq_sta->lq.general_params.start_rate_index[1],
2956 lq_sta->lq.general_params.start_rate_index[2],
2957 lq_sta->lq.general_params.start_rate_index[3]);
2959 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
2960 index = iwl_hwrate_to_plcp_idx(
2961 le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags));
2962 if (is_legacy(tbl->lq_type)) {
2963 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps\n",
2964 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
2965 iwl_rate_mcs[index].mbps);
2967 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps (%s)\n",
2968 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
2969 iwl_rate_mcs[index].mbps, iwl_rate_mcs[index].mcs);
2973 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2978 static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
2979 .write = rs_sta_dbgfs_scale_table_write,
2980 .read = rs_sta_dbgfs_scale_table_read,
2981 .open = open_file_generic,
2983 static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
2984 char __user *user_buf, size_t count, loff_t *ppos)
2991 struct iwl_lq_sta *lq_sta = file->private_data;
2993 buff = kmalloc(1024, GFP_KERNEL);
2997 for (i = 0; i < LQ_SIZE; i++) {
2998 desc += sprintf(buff+desc,
2999 "%s type=%d SGI=%d HT40=%d DUP=%d GF=%d\n"
3001 lq_sta->active_tbl == i ? "*" : "x",
3002 lq_sta->lq_info[i].lq_type,
3003 lq_sta->lq_info[i].is_SGI,
3004 lq_sta->lq_info[i].is_ht40,
3005 lq_sta->lq_info[i].is_dup,
3007 lq_sta->lq_info[i].current_rate);
3008 for (j = 0; j < IWL_RATE_COUNT; j++) {
3009 desc += sprintf(buff+desc,
3010 "counter=%d success=%d %%=%d\n",
3011 lq_sta->lq_info[i].win[j].counter,
3012 lq_sta->lq_info[i].win[j].success_counter,
3013 lq_sta->lq_info[i].win[j].success_ratio);
3016 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3021 static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
3022 .read = rs_sta_dbgfs_stats_table_read,
3023 .open = open_file_generic,
3026 static ssize_t rs_sta_dbgfs_rate_scale_data_read(struct file *file,
3027 char __user *user_buf, size_t count, loff_t *ppos)
3033 struct iwl_lq_sta *lq_sta = file->private_data;
3034 struct iwl_priv *priv;
3035 struct iwl_scale_tbl_info *tbl = &lq_sta->lq_info[lq_sta->active_tbl];
3039 if (is_Ht(tbl->lq_type))
3040 desc += sprintf(buff+desc,
3041 "Bit Rate= %d Mb/s\n",
3042 tbl->expected_tpt[lq_sta->last_txrate_idx]);
3044 desc += sprintf(buff+desc,
3045 "Bit Rate= %d Mb/s\n",
3046 iwl_rates[lq_sta->last_txrate_idx].ieee >> 1);
3047 desc += sprintf(buff+desc,
3048 "Signal Level= %d dBm\tNoise Level= %d dBm\n",
3049 priv->last_rx_rssi, priv->last_rx_noise);
3050 desc += sprintf(buff+desc,
3051 "Tsf= 0x%llx\tBeacon time= 0x%08X\n",
3052 priv->last_tsf, priv->last_beacon_time);
3054 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3058 static const struct file_operations rs_sta_dbgfs_rate_scale_data_ops = {
3059 .read = rs_sta_dbgfs_rate_scale_data_read,
3060 .open = open_file_generic,
3063 static void rs_add_debugfs(void *priv, void *priv_sta,
3066 struct iwl_lq_sta *lq_sta = priv_sta;
3067 lq_sta->rs_sta_dbgfs_scale_table_file =
3068 debugfs_create_file("rate_scale_table", 0600, dir,
3069 lq_sta, &rs_sta_dbgfs_scale_table_ops);
3070 lq_sta->rs_sta_dbgfs_stats_table_file =
3071 debugfs_create_file("rate_stats_table", 0600, dir,
3072 lq_sta, &rs_sta_dbgfs_stats_table_ops);
3073 lq_sta->rs_sta_dbgfs_rate_scale_data_file =
3074 debugfs_create_file("rate_scale_data", 0600, dir,
3075 lq_sta, &rs_sta_dbgfs_rate_scale_data_ops);
3076 lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
3077 debugfs_create_u8("tx_agg_tid_enable", 0600, dir,
3078 &lq_sta->tx_agg_tid_en);
3082 static void rs_remove_debugfs(void *priv, void *priv_sta)
3084 struct iwl_lq_sta *lq_sta = priv_sta;
3085 debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
3086 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
3087 debugfs_remove(lq_sta->rs_sta_dbgfs_rate_scale_data_file);
3088 debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
3092 static struct rate_control_ops rs_ops = {
3095 .tx_status = rs_tx_status,
3096 .get_rate = rs_get_rate,
3097 .rate_init = rs_rate_init,
3100 .alloc_sta = rs_alloc_sta,
3101 .free_sta = rs_free_sta,
3102 #ifdef CONFIG_MAC80211_DEBUGFS
3103 .add_sta_debugfs = rs_add_debugfs,
3104 .remove_sta_debugfs = rs_remove_debugfs,
3108 int iwlagn_rate_control_register(void)
3110 return ieee80211_rate_control_register(&rs_ops);
3113 void iwlagn_rate_control_unregister(void)
3115 ieee80211_rate_control_unregister(&rs_ops);