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 (2*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_fat; /* 1 = 40 MHz channel width */
101 u8 is_dup; /* 1 = duplicated data streams */
102 u8 action; /* change modulation; IWL_[LEGACY/SISO/MIMO]_SWITCH_* */
103 s32 *expected_tpt; /* throughput metrics; expected_tpt_G, etc. */
104 u32 current_rate; /* rate_n_flags, uCode API format */
105 struct iwl_rate_scale_data win[IWL_RATE_COUNT]; /* rate histories */
108 struct iwl_traffic_load {
109 unsigned long time_stamp; /* age of the oldest statistics */
110 u32 packet_count[TID_QUEUE_MAX_SIZE]; /* packet count in this time
112 u32 total; /* total num of packets during the
113 * last TID_MAX_TIME_DIFF */
114 u8 queue_count; /* number of queues that has
115 * been used since the last cleanup */
116 u8 head; /* start of the circular buffer */
120 * struct iwl_lq_sta -- driver's rate scaling private structure
122 * Pointer to this gets passed back and forth between driver and mac80211.
125 u8 active_tbl; /* index of active table, range 0-1 */
126 u8 enable_counter; /* indicates HT mode */
127 u8 stay_in_tbl; /* 1: disallow, 0: allow search for new mode */
128 u8 search_better_tbl; /* 1: currently trying alternate mode */
131 /* The following determine when to search for a new mode */
132 u32 table_count_limit;
133 u32 max_failure_limit; /* # failed frames before new search */
134 u32 max_success_limit; /* # successful frames before new search */
136 u32 total_failed; /* total failed frames, any/all rates */
137 u32 total_success; /* total successful frames, any/all rates */
138 u32 flush_timer; /* time staying in mode before new search */
140 u8 action_counter; /* # mode-switch actions tried */
143 enum ieee80211_band band;
146 /* The following are bitmaps of rates; IWL_RATE_6M_MASK, etc. */
148 u16 active_legacy_rate;
149 u16 active_siso_rate;
150 u16 active_mimo2_rate;
151 u16 active_mimo3_rate;
152 u16 active_rate_basic;
153 s8 max_rate_idx; /* Max rate set by user */
154 u8 missed_rate_counter;
156 struct iwl_link_quality_cmd lq;
157 struct iwl_scale_tbl_info lq_info[LQ_SIZE]; /* "active", "search" */
158 struct iwl_traffic_load load[TID_MAX_LOAD_COUNT];
160 #ifdef CONFIG_MAC80211_DEBUGFS
161 struct dentry *rs_sta_dbgfs_scale_table_file;
162 struct dentry *rs_sta_dbgfs_stats_table_file;
163 struct dentry *rs_sta_dbgfs_tx_agg_tid_en_file;
166 struct iwl_priv *drv;
168 /* used to be in sta_info */
172 static void rs_rate_scale_perform(struct iwl_priv *priv,
173 struct ieee80211_hdr *hdr,
174 struct ieee80211_sta *sta,
175 struct iwl_lq_sta *lq_sta);
176 static void rs_fill_link_cmd(const struct iwl_priv *priv,
177 struct iwl_lq_sta *lq_sta, u32 rate_n_flags);
180 #ifdef CONFIG_MAC80211_DEBUGFS
181 static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
182 u32 *rate_n_flags, int index);
184 static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
185 u32 *rate_n_flags, int index)
190 * Expected throughput metrics for following rates:
191 * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
192 * "G" is the only table that supports CCK (the first 4 rates).
195 static s32 expected_tpt_A[IWL_RATE_COUNT] = {
196 0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186, 186
199 static s32 expected_tpt_G[IWL_RATE_COUNT] = {
200 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 186
203 static s32 expected_tpt_siso20MHz[IWL_RATE_COUNT] = {
204 0, 0, 0, 0, 42, 42, 76, 102, 124, 159, 183, 193, 202
207 static s32 expected_tpt_siso20MHzSGI[IWL_RATE_COUNT] = {
208 0, 0, 0, 0, 46, 46, 82, 110, 132, 168, 192, 202, 211
211 static s32 expected_tpt_mimo2_20MHz[IWL_RATE_COUNT] = {
212 0, 0, 0, 0, 74, 74, 123, 155, 179, 214, 236, 244, 251
215 static s32 expected_tpt_mimo2_20MHzSGI[IWL_RATE_COUNT] = {
216 0, 0, 0, 0, 81, 81, 131, 164, 188, 222, 243, 251, 257
219 static s32 expected_tpt_siso40MHz[IWL_RATE_COUNT] = {
220 0, 0, 0, 0, 77, 77, 127, 160, 184, 220, 242, 250, 257
223 static s32 expected_tpt_siso40MHzSGI[IWL_RATE_COUNT] = {
224 0, 0, 0, 0, 83, 83, 135, 169, 193, 229, 250, 257, 264
227 static s32 expected_tpt_mimo2_40MHz[IWL_RATE_COUNT] = {
228 0, 0, 0, 0, 123, 123, 182, 214, 235, 264, 279, 285, 289
231 static s32 expected_tpt_mimo2_40MHzSGI[IWL_RATE_COUNT] = {
232 0, 0, 0, 0, 131, 131, 191, 222, 242, 270, 284, 289, 293
235 /* Expected throughput metric MIMO3 */
236 static s32 expected_tpt_mimo3_20MHz[IWL_RATE_COUNT] = {
237 0, 0, 0, 0, 99, 99, 153, 186, 208, 239, 256, 263, 268
240 static s32 expected_tpt_mimo3_20MHzSGI[IWL_RATE_COUNT] = {
241 0, 0, 0, 0, 106, 106, 162, 194, 215, 246, 262, 268, 273
244 static s32 expected_tpt_mimo3_40MHz[IWL_RATE_COUNT] = {
245 0, 0, 0, 0, 152, 152, 211, 239, 255, 279, 290, 294, 297
248 static s32 expected_tpt_mimo3_40MHzSGI[IWL_RATE_COUNT] = {
249 0, 0, 0, 0, 160, 160, 219, 245, 261, 284, 294, 297, 300
252 static inline u8 rs_extract_rate(u32 rate_n_flags)
254 return (u8)(rate_n_flags & 0xFF);
257 static void rs_rate_scale_clear_window(struct iwl_rate_scale_data *window)
260 window->success_counter = 0;
261 window->success_ratio = IWL_INVALID_VALUE;
263 window->average_tpt = IWL_INVALID_VALUE;
267 static inline u8 rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
269 return (ant_type & valid_antenna) == ant_type;
273 * removes the old data from the statistics. All data that is older than
274 * TID_MAX_TIME_DIFF, will be deleted.
276 static void rs_tl_rm_old_stats(struct iwl_traffic_load *tl, u32 curr_time)
278 /* The oldest age we want to keep */
279 u32 oldest_time = curr_time - TID_MAX_TIME_DIFF;
281 while (tl->queue_count &&
282 (tl->time_stamp < oldest_time)) {
283 tl->total -= tl->packet_count[tl->head];
284 tl->packet_count[tl->head] = 0;
285 tl->time_stamp += TID_QUEUE_CELL_SPACING;
288 if (tl->head >= TID_QUEUE_MAX_SIZE)
294 * increment traffic load value for tid and also remove
295 * any old values if passed the certain time period
297 static u8 rs_tl_add_packet(struct iwl_lq_sta *lq_data,
298 struct ieee80211_hdr *hdr)
300 u32 curr_time = jiffies_to_msecs(jiffies);
303 struct iwl_traffic_load *tl = NULL;
306 if (ieee80211_is_data_qos(hdr->frame_control)) {
307 u8 *qc = ieee80211_get_qos_ctl(hdr);
310 return MAX_TID_COUNT;
312 tl = &lq_data->load[tid];
314 curr_time -= curr_time % TID_ROUND_VALUE;
316 /* Happens only for the first packet. Initialize the data */
317 if (!(tl->queue_count)) {
319 tl->time_stamp = curr_time;
322 tl->packet_count[0] = 1;
323 return MAX_TID_COUNT;
326 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
327 index = time_diff / TID_QUEUE_CELL_SPACING;
329 /* The history is too long: remove data that is older than */
330 /* TID_MAX_TIME_DIFF */
331 if (index >= TID_QUEUE_MAX_SIZE)
332 rs_tl_rm_old_stats(tl, curr_time);
334 index = (tl->head + index) % TID_QUEUE_MAX_SIZE;
335 tl->packet_count[index] = tl->packet_count[index] + 1;
336 tl->total = tl->total + 1;
338 if ((index + 1) > tl->queue_count)
339 tl->queue_count = index + 1;
345 get the traffic load value for tid
347 static u32 rs_tl_get_load(struct iwl_lq_sta *lq_data, u8 tid)
349 u32 curr_time = jiffies_to_msecs(jiffies);
352 struct iwl_traffic_load *tl = NULL;
354 if (tid >= TID_MAX_LOAD_COUNT)
357 tl = &(lq_data->load[tid]);
359 curr_time -= curr_time % TID_ROUND_VALUE;
361 if (!(tl->queue_count))
364 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
365 index = time_diff / TID_QUEUE_CELL_SPACING;
367 /* The history is too long: remove data that is older than */
368 /* TID_MAX_TIME_DIFF */
369 if (index >= TID_QUEUE_MAX_SIZE)
370 rs_tl_rm_old_stats(tl, curr_time);
375 static void rs_tl_turn_on_agg_for_tid(struct iwl_priv *priv,
376 struct iwl_lq_sta *lq_data, u8 tid,
377 struct ieee80211_sta *sta)
379 if (rs_tl_get_load(lq_data, tid) > IWL_AGG_LOAD_THRESHOLD) {
380 IWL_DEBUG_HT(priv, "Starting Tx agg: STA: %pM tid: %d\n",
382 ieee80211_start_tx_ba_session(priv->hw, sta->addr, tid);
386 static void rs_tl_turn_on_agg(struct iwl_priv *priv, u8 tid,
387 struct iwl_lq_sta *lq_data,
388 struct ieee80211_sta *sta)
390 if ((tid < TID_MAX_LOAD_COUNT))
391 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
392 else if (tid == IWL_AGG_ALL_TID)
393 for (tid = 0; tid < TID_MAX_LOAD_COUNT; tid++)
394 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
397 static inline int get_num_of_ant_from_rate(u32 rate_n_flags)
399 return !!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
400 !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
401 !!(rate_n_flags & RATE_MCS_ANT_C_MSK);
405 * rs_collect_tx_data - Update the success/failure sliding window
407 * We keep a sliding window of the last 62 packets transmitted
408 * at this rate. window->data contains the bitmask of successful
411 static int rs_collect_tx_data(struct iwl_rate_scale_data *windows,
412 int scale_index, s32 tpt, int retries,
415 struct iwl_rate_scale_data *window = NULL;
416 static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
419 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
422 /* Select data for current tx bit rate */
423 window = &(windows[scale_index]);
426 * Keep track of only the latest 62 tx frame attempts in this rate's
427 * history window; anything older isn't really relevant any more.
428 * If we have filled up the sliding window, drop the oldest attempt;
429 * if the oldest attempt (highest bit in bitmap) shows "success",
430 * subtract "1" from the success counter (this is the main reason
431 * we keep these bitmaps!).
433 while (retries > 0) {
434 if (window->counter >= IWL_RATE_MAX_WINDOW) {
436 /* remove earliest */
437 window->counter = IWL_RATE_MAX_WINDOW - 1;
439 if (window->data & mask) {
440 window->data &= ~mask;
441 window->success_counter--;
445 /* Increment frames-attempted counter */
448 /* Shift bitmap by one frame (throw away oldest history),
449 * OR in "1", and increment "success" if this
450 * frame was successful. */
453 window->success_counter++;
461 /* Calculate current success ratio, avoid divide-by-0! */
462 if (window->counter > 0)
463 window->success_ratio = 128 * (100 * window->success_counter)
466 window->success_ratio = IWL_INVALID_VALUE;
468 fail_count = window->counter - window->success_counter;
470 /* Calculate average throughput, if we have enough history. */
471 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
472 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
473 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
475 window->average_tpt = IWL_INVALID_VALUE;
477 /* Tag this window as having been updated */
478 window->stamp = jiffies;
484 * Fill uCode API rate_n_flags field, based on "search" or "active" table.
486 /* FIXME:RS:remove this function and put the flags statically in the table */
487 static u32 rate_n_flags_from_tbl(struct iwl_priv *priv,
488 struct iwl_scale_tbl_info *tbl,
489 int index, u8 use_green)
491 u32 rate_n_flags = 0;
493 if (is_legacy(tbl->lq_type)) {
494 rate_n_flags = iwl_rates[index].plcp;
495 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
496 rate_n_flags |= RATE_MCS_CCK_MSK;
498 } else if (is_Ht(tbl->lq_type)) {
499 if (index > IWL_LAST_OFDM_RATE) {
500 IWL_ERR(priv, "Invalid HT rate index %d\n", index);
501 index = IWL_LAST_OFDM_RATE;
503 rate_n_flags = RATE_MCS_HT_MSK;
505 if (is_siso(tbl->lq_type))
506 rate_n_flags |= iwl_rates[index].plcp_siso;
507 else if (is_mimo2(tbl->lq_type))
508 rate_n_flags |= iwl_rates[index].plcp_mimo2;
510 rate_n_flags |= iwl_rates[index].plcp_mimo3;
512 IWL_ERR(priv, "Invalid tbl->lq_type %d\n", tbl->lq_type);
515 rate_n_flags |= ((tbl->ant_type << RATE_MCS_ANT_POS) &
516 RATE_MCS_ANT_ABC_MSK);
518 if (is_Ht(tbl->lq_type)) {
521 rate_n_flags |= RATE_MCS_DUP_MSK;
523 rate_n_flags |= RATE_MCS_FAT_MSK;
526 rate_n_flags |= RATE_MCS_SGI_MSK;
529 rate_n_flags |= RATE_MCS_GF_MSK;
530 if (is_siso(tbl->lq_type) && tbl->is_SGI) {
531 rate_n_flags &= ~RATE_MCS_SGI_MSK;
532 IWL_ERR(priv, "GF was set with SGI:SISO\n");
540 * Interpret uCode API's rate_n_flags format,
541 * fill "search" or "active" tx mode table.
543 static int rs_get_tbl_info_from_mcs(const u32 rate_n_flags,
544 enum ieee80211_band band,
545 struct iwl_scale_tbl_info *tbl,
548 u32 ant_msk = (rate_n_flags & RATE_MCS_ANT_ABC_MSK);
549 u8 num_of_ant = get_num_of_ant_from_rate(rate_n_flags);
552 *rate_idx = iwl_hwrate_to_plcp_idx(rate_n_flags);
554 if (*rate_idx == IWL_RATE_INVALID) {
558 tbl->is_SGI = 0; /* default legacy setup */
561 tbl->ant_type = (ant_msk >> RATE_MCS_ANT_POS);
562 tbl->lq_type = LQ_NONE;
564 /* legacy rate format */
565 if (!(rate_n_flags & RATE_MCS_HT_MSK)) {
566 if (num_of_ant == 1) {
567 if (band == IEEE80211_BAND_5GHZ)
574 if (rate_n_flags & RATE_MCS_SGI_MSK)
577 if ((rate_n_flags & RATE_MCS_FAT_MSK) ||
578 (rate_n_flags & RATE_MCS_DUP_MSK))
581 if (rate_n_flags & RATE_MCS_DUP_MSK)
584 mcs = rs_extract_rate(rate_n_flags);
587 if (mcs <= IWL_RATE_SISO_60M_PLCP) {
589 tbl->lq_type = LQ_SISO; /*else NONE*/
591 } else if (mcs <= IWL_RATE_MIMO2_60M_PLCP) {
593 tbl->lq_type = LQ_MIMO2;
597 tbl->lq_type = LQ_MIMO3;
603 /* switch to another antenna/antennas and return 1 */
604 /* if no other valid antenna found, return 0 */
605 static int rs_toggle_antenna(u32 valid_ant, u32 *rate_n_flags,
606 struct iwl_scale_tbl_info *tbl)
610 if (!tbl->ant_type || tbl->ant_type > ANT_ABC)
613 if (!rs_is_valid_ant(valid_ant, tbl->ant_type))
616 new_ant_type = ant_toggle_lookup[tbl->ant_type];
618 while ((new_ant_type != tbl->ant_type) &&
619 !rs_is_valid_ant(valid_ant, new_ant_type))
620 new_ant_type = ant_toggle_lookup[new_ant_type];
622 if (new_ant_type == tbl->ant_type)
625 tbl->ant_type = new_ant_type;
626 *rate_n_flags &= ~RATE_MCS_ANT_ABC_MSK;
627 *rate_n_flags |= new_ant_type << RATE_MCS_ANT_POS;
631 /* FIXME:RS: in 4965 we don't use greenfield at all */
632 /* FIXME:RS: don't use greenfield for now in TX */
634 static inline u8 rs_use_green(struct iwl_priv *priv, struct ieee80211_conf *conf)
636 return (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) &&
637 priv->current_ht_config.is_green_field &&
638 !priv->current_ht_config.non_GF_STA_present;
641 static inline u8 rs_use_green(struct iwl_priv *priv, struct ieee80211_conf *conf)
647 * rs_get_supported_rates - get the available rates
649 * if management frame or broadcast frame only return
650 * basic available rates.
653 static u16 rs_get_supported_rates(struct iwl_lq_sta *lq_sta,
654 struct ieee80211_hdr *hdr,
655 enum iwl_table_type rate_type)
657 if (hdr && is_multicast_ether_addr(hdr->addr1) &&
658 lq_sta->active_rate_basic)
659 return lq_sta->active_rate_basic;
661 if (is_legacy(rate_type)) {
662 return lq_sta->active_legacy_rate;
664 if (is_siso(rate_type))
665 return lq_sta->active_siso_rate;
666 else if (is_mimo2(rate_type))
667 return lq_sta->active_mimo2_rate;
669 return lq_sta->active_mimo3_rate;
673 static u16 rs_get_adjacent_rate(struct iwl_priv *priv, u8 index, u16 rate_mask,
676 u8 high = IWL_RATE_INVALID;
677 u8 low = IWL_RATE_INVALID;
679 /* 802.11A or ht walks to the next literal adjacent rate in
681 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
685 /* Find the previous rate that is in the rate mask */
687 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
688 if (rate_mask & mask) {
694 /* Find the next rate that is in the rate mask */
696 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
697 if (rate_mask & mask) {
703 return (high << 8) | low;
707 while (low != IWL_RATE_INVALID) {
708 low = iwl_rates[low].prev_rs;
709 if (low == IWL_RATE_INVALID)
711 if (rate_mask & (1 << low))
713 IWL_DEBUG_RATE(priv, "Skipping masked lower rate: %d\n", low);
717 while (high != IWL_RATE_INVALID) {
718 high = iwl_rates[high].next_rs;
719 if (high == IWL_RATE_INVALID)
721 if (rate_mask & (1 << high))
723 IWL_DEBUG_RATE(priv, "Skipping masked higher rate: %d\n", high);
726 return (high << 8) | low;
729 static u32 rs_get_lower_rate(struct iwl_lq_sta *lq_sta,
730 struct iwl_scale_tbl_info *tbl,
731 u8 scale_index, u8 ht_possible)
736 u8 switch_to_legacy = 0;
737 u8 is_green = lq_sta->is_green;
739 /* check if we need to switch from HT to legacy rates.
740 * assumption is that mandatory rates (1Mbps or 6Mbps)
741 * are always supported (spec demand) */
742 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
743 switch_to_legacy = 1;
744 scale_index = rs_ht_to_legacy[scale_index];
745 if (lq_sta->band == IEEE80211_BAND_5GHZ)
750 if (num_of_ant(tbl->ant_type) > 1)
751 tbl->ant_type = ANT_A;/*FIXME:RS*/
757 rate_mask = rs_get_supported_rates(lq_sta, NULL, tbl->lq_type);
759 /* Mask with station rate restriction */
760 if (is_legacy(tbl->lq_type)) {
761 /* supp_rates has no CCK bits in A mode */
762 if (lq_sta->band == IEEE80211_BAND_5GHZ)
763 rate_mask = (u16)(rate_mask &
764 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
766 rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
769 /* If we switched from HT to legacy, check current rate */
770 if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
775 high_low = rs_get_adjacent_rate(lq_sta->drv, scale_index, rate_mask,
777 low = high_low & 0xff;
779 if (low == IWL_RATE_INVALID)
783 return rate_n_flags_from_tbl(lq_sta->drv, tbl, low, is_green);
787 * mac80211 sends us Tx status
789 static void rs_tx_status(void *priv_r, struct ieee80211_supported_band *sband,
790 struct ieee80211_sta *sta, void *priv_sta,
795 int rs_index, index = 0;
796 struct iwl_lq_sta *lq_sta = priv_sta;
797 struct iwl_link_quality_cmd *table;
798 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
799 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
800 struct ieee80211_hw *hw = priv->hw;
801 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
802 struct iwl_rate_scale_data *window = NULL;
803 struct iwl_rate_scale_data *search_win = NULL;
805 struct iwl_scale_tbl_info tbl_type;
806 struct iwl_scale_tbl_info *curr_tbl, *search_tbl;
810 IWL_DEBUG_RATE_LIMIT(priv, "get frame ack response, update rate scale window\n");
812 if (!ieee80211_is_data(hdr->frame_control) ||
813 is_multicast_ether_addr(hdr->addr1))
816 /* This packet was aggregated but doesn't carry rate scale info */
817 if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
818 !(info->flags & IEEE80211_TX_STAT_AMPDU))
821 if (info->flags & IEEE80211_TX_STAT_AMPDU)
824 retries = info->status.rates[0].count - 1;
829 if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) &&
830 !lq_sta->ibss_sta_added)
834 active_index = lq_sta->active_tbl;
836 curr_tbl = &(lq_sta->lq_info[active_index]);
837 search_tbl = &(lq_sta->lq_info[(1 - active_index)]);
838 window = (struct iwl_rate_scale_data *)&(curr_tbl->win[0]);
839 search_win = (struct iwl_rate_scale_data *)&(search_tbl->win[0]);
842 * Ignore this Tx frame response if its initial rate doesn't match
843 * that of latest Link Quality command. There may be stragglers
844 * from a previous Link Quality command, but we're no longer interested
845 * in those; they're either from the "active" mode while we're trying
846 * to check "search" mode, or a prior "search" mode after we've moved
847 * to a new "search" mode (which might become the new "active" mode).
849 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
850 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
851 if (priv->band == IEEE80211_BAND_5GHZ)
852 rs_index -= IWL_FIRST_OFDM_RATE;
854 if ((info->status.rates[0].idx < 0) ||
855 (tbl_type.is_SGI != !!(info->status.rates[0].flags & IEEE80211_TX_RC_SHORT_GI)) ||
856 (tbl_type.is_fat != !!(info->status.rates[0].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)) ||
857 (tbl_type.is_dup != !!(info->status.rates[0].flags & IEEE80211_TX_RC_DUP_DATA)) ||
858 (tbl_type.ant_type != info->antenna_sel_tx) ||
859 (!!(tx_rate & RATE_MCS_HT_MSK) != !!(info->status.rates[0].flags & IEEE80211_TX_RC_MCS)) ||
860 (!!(tx_rate & RATE_MCS_GF_MSK) != !!(info->status.rates[0].flags & IEEE80211_TX_RC_GREEN_FIELD)) ||
861 (hw->wiphy->bands[priv->band]->bitrates[rs_index].bitrate !=
862 hw->wiphy->bands[info->band]->bitrates[info->status.rates[0].idx].bitrate)) {
863 IWL_DEBUG_RATE(priv, "initial rate does not match 0x%x\n", tx_rate);
864 /* the last LQ command could failed so the LQ in ucode not
865 * the same in driver sync up
867 lq_sta->missed_rate_counter++;
868 if (lq_sta->missed_rate_counter > IWL_MISSED_RATE_MAX) {
869 lq_sta->missed_rate_counter = 0;
870 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
875 lq_sta->missed_rate_counter = 0;
876 /* Update frame history window with "failure" for each Tx retry. */
878 /* Look up the rate and other info used for each tx attempt.
879 * Each tx attempt steps one entry deeper in the rate table. */
880 tx_rate = le32_to_cpu(table->rs_table[index].rate_n_flags);
881 rs_get_tbl_info_from_mcs(tx_rate, priv->band,
882 &tbl_type, &rs_index);
884 /* If type matches "search" table,
885 * add failure to "search" history */
886 if ((tbl_type.lq_type == search_tbl->lq_type) &&
887 (tbl_type.ant_type == search_tbl->ant_type) &&
888 (tbl_type.is_SGI == search_tbl->is_SGI)) {
889 if (search_tbl->expected_tpt)
890 tpt = search_tbl->expected_tpt[rs_index];
893 rs_collect_tx_data(search_win, rs_index, tpt, 1, 0);
895 /* Else if type matches "current/active" table,
896 * add failure to "current/active" history */
897 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
898 (tbl_type.ant_type == curr_tbl->ant_type) &&
899 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
900 if (curr_tbl->expected_tpt)
901 tpt = curr_tbl->expected_tpt[rs_index];
904 rs_collect_tx_data(window, rs_index, tpt, 1, 0);
907 /* If not searching for a new mode, increment failed counter
908 * ... this helps determine when to start searching again */
909 if (lq_sta->stay_in_tbl)
910 lq_sta->total_failed++;
917 * Find (by rate) the history window to update with final Tx attempt;
918 * if Tx was successful first try, use original rate,
919 * else look up the rate that was, finally, successful.
921 tx_rate = le32_to_cpu(table->rs_table[index].rate_n_flags);
922 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
924 /* Update frame history window with "success" if Tx got ACKed ... */
925 status = !!(info->flags & IEEE80211_TX_STAT_ACK);
927 /* If type matches "search" table,
928 * add final tx status to "search" history */
929 if ((tbl_type.lq_type == search_tbl->lq_type) &&
930 (tbl_type.ant_type == search_tbl->ant_type) &&
931 (tbl_type.is_SGI == search_tbl->is_SGI)) {
932 if (search_tbl->expected_tpt)
933 tpt = search_tbl->expected_tpt[rs_index];
936 if (info->flags & IEEE80211_TX_STAT_AMPDU)
937 rs_collect_tx_data(search_win, rs_index, tpt,
938 info->status.ampdu_ack_len,
939 info->status.ampdu_ack_map);
941 rs_collect_tx_data(search_win, rs_index, tpt,
943 /* Else if type matches "current/active" table,
944 * add final tx status to "current/active" history */
945 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
946 (tbl_type.ant_type == curr_tbl->ant_type) &&
947 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
948 if (curr_tbl->expected_tpt)
949 tpt = curr_tbl->expected_tpt[rs_index];
952 if (info->flags & IEEE80211_TX_STAT_AMPDU)
953 rs_collect_tx_data(window, rs_index, tpt,
954 info->status.ampdu_ack_len,
955 info->status.ampdu_ack_map);
957 rs_collect_tx_data(window, rs_index, tpt,
961 /* If not searching for new mode, increment success/failed counter
962 * ... these help determine when to start searching again */
963 if (lq_sta->stay_in_tbl) {
964 if (info->flags & IEEE80211_TX_STAT_AMPDU) {
965 lq_sta->total_success += info->status.ampdu_ack_map;
966 lq_sta->total_failed +=
967 (info->status.ampdu_ack_len - info->status.ampdu_ack_map);
970 lq_sta->total_success++;
972 lq_sta->total_failed++;
976 /* See if there's a better rate or modulation mode to try. */
977 if (sta && sta->supp_rates[sband->band])
978 rs_rate_scale_perform(priv, hdr, sta, lq_sta);
984 * Begin a period of staying with a selected modulation mode.
985 * Set "stay_in_tbl" flag to prevent any mode switches.
986 * Set frame tx success limits according to legacy vs. high-throughput,
987 * and reset overall (spanning all rates) tx success history statistics.
988 * These control how long we stay using same modulation mode before
989 * searching for a new mode.
991 static void rs_set_stay_in_table(struct iwl_priv *priv, u8 is_legacy,
992 struct iwl_lq_sta *lq_sta)
994 IWL_DEBUG_RATE(priv, "we are staying in the same table\n");
995 lq_sta->stay_in_tbl = 1; /* only place this gets set */
997 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
998 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
999 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
1001 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
1002 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
1003 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
1005 lq_sta->table_count = 0;
1006 lq_sta->total_failed = 0;
1007 lq_sta->total_success = 0;
1011 * Find correct throughput table for given mode of modulation
1013 static void rs_set_expected_tpt_table(struct iwl_lq_sta *lq_sta,
1014 struct iwl_scale_tbl_info *tbl)
1016 if (is_legacy(tbl->lq_type)) {
1017 if (!is_a_band(tbl->lq_type))
1018 tbl->expected_tpt = expected_tpt_G;
1020 tbl->expected_tpt = expected_tpt_A;
1021 } else if (is_siso(tbl->lq_type)) {
1022 if (tbl->is_fat && !lq_sta->is_dup)
1024 tbl->expected_tpt = expected_tpt_siso40MHzSGI;
1026 tbl->expected_tpt = expected_tpt_siso40MHz;
1027 else if (tbl->is_SGI)
1028 tbl->expected_tpt = expected_tpt_siso20MHzSGI;
1030 tbl->expected_tpt = expected_tpt_siso20MHz;
1031 } else if (is_mimo2(tbl->lq_type)) {
1032 if (tbl->is_fat && !lq_sta->is_dup)
1034 tbl->expected_tpt = expected_tpt_mimo2_40MHzSGI;
1036 tbl->expected_tpt = expected_tpt_mimo2_40MHz;
1037 else if (tbl->is_SGI)
1038 tbl->expected_tpt = expected_tpt_mimo2_20MHzSGI;
1040 tbl->expected_tpt = expected_tpt_mimo2_20MHz;
1041 } else if (is_mimo3(tbl->lq_type)) {
1042 if (tbl->is_fat && !lq_sta->is_dup)
1044 tbl->expected_tpt = expected_tpt_mimo3_40MHzSGI;
1046 tbl->expected_tpt = expected_tpt_mimo3_40MHz;
1047 else if (tbl->is_SGI)
1048 tbl->expected_tpt = expected_tpt_mimo3_20MHzSGI;
1050 tbl->expected_tpt = expected_tpt_mimo3_20MHz;
1052 tbl->expected_tpt = expected_tpt_G;
1056 * Find starting rate for new "search" high-throughput mode of modulation.
1057 * Goal is to find lowest expected rate (under perfect conditions) that is
1058 * above the current measured throughput of "active" mode, to give new mode
1059 * a fair chance to prove itself without too many challenges.
1061 * This gets called when transitioning to more aggressive modulation
1062 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
1063 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1064 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1065 * bit rate will typically need to increase, but not if performance was bad.
1067 static s32 rs_get_best_rate(struct iwl_priv *priv,
1068 struct iwl_lq_sta *lq_sta,
1069 struct iwl_scale_tbl_info *tbl, /* "search" */
1070 u16 rate_mask, s8 index)
1072 /* "active" values */
1073 struct iwl_scale_tbl_info *active_tbl =
1074 &(lq_sta->lq_info[lq_sta->active_tbl]);
1075 s32 active_sr = active_tbl->win[index].success_ratio;
1076 s32 active_tpt = active_tbl->expected_tpt[index];
1078 /* expected "search" throughput */
1079 s32 *tpt_tbl = tbl->expected_tpt;
1081 s32 new_rate, high, low, start_hi;
1085 new_rate = high = low = start_hi = IWL_RATE_INVALID;
1088 high_low = rs_get_adjacent_rate(priv, rate, rate_mask,
1091 low = high_low & 0xff;
1092 high = (high_low >> 8) & 0xff;
1095 * Lower the "search" bit rate, to give new "search" mode
1096 * approximately the same throughput as "active" if:
1098 * 1) "Active" mode has been working modestly well (but not
1099 * great), and expected "search" throughput (under perfect
1100 * conditions) at candidate rate is above the actual
1101 * measured "active" throughput (but less than expected
1102 * "active" throughput under perfect conditions).
1104 * 2) "Active" mode has been working perfectly or very well
1105 * and expected "search" throughput (under perfect
1106 * conditions) at candidate rate is above expected
1107 * "active" throughput (under perfect conditions).
1109 if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
1110 ((active_sr > IWL_RATE_DECREASE_TH) &&
1111 (active_sr <= IWL_RATE_HIGH_TH) &&
1112 (tpt_tbl[rate] <= active_tpt))) ||
1113 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
1114 (tpt_tbl[rate] > active_tpt))) {
1116 /* (2nd or later pass)
1117 * If we've already tried to raise the rate, and are
1118 * now trying to lower it, use the higher rate. */
1119 if (start_hi != IWL_RATE_INVALID) {
1120 new_rate = start_hi;
1126 /* Loop again with lower rate */
1127 if (low != IWL_RATE_INVALID)
1130 /* Lower rate not available, use the original */
1134 /* Else try to raise the "search" rate to match "active" */
1136 /* (2nd or later pass)
1137 * If we've already tried to lower the rate, and are
1138 * now trying to raise it, use the lower rate. */
1139 if (new_rate != IWL_RATE_INVALID)
1142 /* Loop again with higher rate */
1143 else if (high != IWL_RATE_INVALID) {
1147 /* Higher rate not available, use the original */
1159 * Set up search table for MIMO2
1161 static int rs_switch_to_mimo2(struct iwl_priv *priv,
1162 struct iwl_lq_sta *lq_sta,
1163 struct ieee80211_conf *conf,
1164 struct ieee80211_sta *sta,
1165 struct iwl_scale_tbl_info *tbl, int index)
1169 s8 is_green = lq_sta->is_green;
1171 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1174 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
1175 == WLAN_HT_CAP_SM_PS_STATIC)
1178 /* Need both Tx chains/antennas to support MIMO */
1179 if (priv->hw_params.tx_chains_num < 2)
1182 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO2\n");
1184 tbl->lq_type = LQ_MIMO2;
1185 tbl->is_dup = lq_sta->is_dup;
1187 rate_mask = lq_sta->active_mimo2_rate;
1189 if (priv->current_ht_config.supported_chan_width
1190 == IWL_CHANNEL_WIDTH_40MHZ)
1195 /* FIXME: - don't toggle SGI here
1197 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
1201 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
1207 rs_set_expected_tpt_table(lq_sta, tbl);
1209 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1211 IWL_DEBUG_RATE(priv, "LQ: MIMO2 best rate %d mask %X\n", rate, rate_mask);
1212 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1213 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
1217 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
1219 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
1220 tbl->current_rate, is_green);
1225 * Set up search table for MIMO3
1227 static int rs_switch_to_mimo3(struct iwl_priv *priv,
1228 struct iwl_lq_sta *lq_sta,
1229 struct ieee80211_conf *conf,
1230 struct ieee80211_sta *sta,
1231 struct iwl_scale_tbl_info *tbl, int index)
1235 s8 is_green = lq_sta->is_green;
1237 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1240 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
1241 == WLAN_HT_CAP_SM_PS_STATIC)
1244 /* Need both Tx chains/antennas to support MIMO */
1245 if (priv->hw_params.tx_chains_num < 3)
1248 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO3\n");
1250 tbl->lq_type = LQ_MIMO3;
1251 tbl->is_dup = lq_sta->is_dup;
1253 rate_mask = lq_sta->active_mimo3_rate;
1255 if (priv->current_ht_config.supported_chan_width
1256 == IWL_CHANNEL_WIDTH_40MHZ)
1261 /* FIXME: - don't toggle SGI here
1263 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
1267 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
1273 rs_set_expected_tpt_table(lq_sta, tbl);
1275 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1277 IWL_DEBUG_RATE(priv, "LQ: MIMO3 best rate %d mask %X\n",
1279 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1280 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
1284 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
1286 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
1287 tbl->current_rate, is_green);
1292 * Set up search table for SISO
1294 static int rs_switch_to_siso(struct iwl_priv *priv,
1295 struct iwl_lq_sta *lq_sta,
1296 struct ieee80211_conf *conf,
1297 struct ieee80211_sta *sta,
1298 struct iwl_scale_tbl_info *tbl, int index)
1301 u8 is_green = lq_sta->is_green;
1304 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1307 IWL_DEBUG_RATE(priv, "LQ: try to switch to SISO\n");
1309 tbl->is_dup = lq_sta->is_dup;
1310 tbl->lq_type = LQ_SISO;
1312 rate_mask = lq_sta->active_siso_rate;
1314 if (priv->current_ht_config.supported_chan_width
1315 == IWL_CHANNEL_WIDTH_40MHZ)
1320 /* FIXME: - don't toggle SGI here
1322 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
1326 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
1333 tbl->is_SGI = 0; /*11n spec: no SGI in SISO+Greenfield*/
1335 rs_set_expected_tpt_table(lq_sta, tbl);
1336 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1338 IWL_DEBUG_RATE(priv, "LQ: get best rate %d mask %X\n", rate, rate_mask);
1339 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1340 IWL_DEBUG_RATE(priv, "can not switch with index %d rate mask %x\n",
1344 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
1345 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
1346 tbl->current_rate, is_green);
1351 * Try to switch to new modulation mode from legacy
1353 static int rs_move_legacy_other(struct iwl_priv *priv,
1354 struct iwl_lq_sta *lq_sta,
1355 struct ieee80211_conf *conf,
1356 struct ieee80211_sta *sta,
1359 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1360 struct iwl_scale_tbl_info *search_tbl =
1361 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1362 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1363 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1364 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1365 u8 start_action = tbl->action;
1366 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1367 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1371 switch (tbl->action) {
1372 case IWL_LEGACY_SWITCH_ANTENNA1:
1373 case IWL_LEGACY_SWITCH_ANTENNA2:
1374 IWL_DEBUG_RATE(priv, "LQ: Legacy toggle Antenna\n");
1376 lq_sta->action_counter++;
1378 if ((tbl->action == IWL_LEGACY_SWITCH_ANTENNA1 &&
1379 tx_chains_num <= 1) ||
1380 (tbl->action == IWL_LEGACY_SWITCH_ANTENNA2 &&
1381 tx_chains_num <= 2))
1384 /* Don't change antenna if success has been great */
1385 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1388 /* Set up search table to try other antenna */
1389 memcpy(search_tbl, tbl, sz);
1391 if (rs_toggle_antenna(valid_tx_ant,
1392 &search_tbl->current_rate, search_tbl)) {
1393 rs_set_expected_tpt_table(lq_sta, search_tbl);
1397 case IWL_LEGACY_SWITCH_SISO:
1398 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to SISO\n");
1400 /* Set up search table to try SISO */
1401 memcpy(search_tbl, tbl, sz);
1402 search_tbl->is_SGI = 0;
1403 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
1406 lq_sta->action_counter = 0;
1411 case IWL_LEGACY_SWITCH_MIMO2_AB:
1412 case IWL_LEGACY_SWITCH_MIMO2_AC:
1413 case IWL_LEGACY_SWITCH_MIMO2_BC:
1414 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO2\n");
1416 /* Set up search table to try MIMO */
1417 memcpy(search_tbl, tbl, sz);
1418 search_tbl->is_SGI = 0;
1420 if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AB)
1421 search_tbl->ant_type = ANT_AB;
1422 else if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AC)
1423 search_tbl->ant_type = ANT_AC;
1425 search_tbl->ant_type = ANT_BC;
1427 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1430 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
1433 lq_sta->action_counter = 0;
1438 case IWL_LEGACY_SWITCH_MIMO3_ABC:
1439 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO3\n");
1441 /* Set up search table to try MIMO3 */
1442 memcpy(search_tbl, tbl, sz);
1443 search_tbl->is_SGI = 0;
1445 search_tbl->ant_type = ANT_ABC;
1447 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1450 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1453 lq_sta->action_counter = 0;
1459 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
1460 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
1462 if (tbl->action == start_action)
1466 search_tbl->lq_type = LQ_NONE;
1470 lq_sta->search_better_tbl = 1;
1472 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
1473 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
1479 * Try to switch to new modulation mode from SISO
1481 static int rs_move_siso_to_other(struct iwl_priv *priv,
1482 struct iwl_lq_sta *lq_sta,
1483 struct ieee80211_conf *conf,
1484 struct ieee80211_sta *sta, int index)
1486 u8 is_green = lq_sta->is_green;
1487 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1488 struct iwl_scale_tbl_info *search_tbl =
1489 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1490 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1491 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1492 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1493 u8 start_action = tbl->action;
1494 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1495 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1499 lq_sta->action_counter++;
1500 switch (tbl->action) {
1501 case IWL_SISO_SWITCH_ANTENNA1:
1502 case IWL_SISO_SWITCH_ANTENNA2:
1503 IWL_DEBUG_RATE(priv, "LQ: SISO toggle Antenna\n");
1505 if ((tbl->action == IWL_SISO_SWITCH_ANTENNA1 &&
1506 tx_chains_num <= 1) ||
1507 (tbl->action == IWL_SISO_SWITCH_ANTENNA2 &&
1508 tx_chains_num <= 2))
1511 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1514 memcpy(search_tbl, tbl, sz);
1515 if (rs_toggle_antenna(valid_tx_ant,
1516 &search_tbl->current_rate, search_tbl))
1519 case IWL_SISO_SWITCH_MIMO2_AB:
1520 case IWL_SISO_SWITCH_MIMO2_AC:
1521 case IWL_SISO_SWITCH_MIMO2_BC:
1522 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO2\n");
1523 memcpy(search_tbl, tbl, sz);
1524 search_tbl->is_SGI = 0;
1526 if (tbl->action == IWL_SISO_SWITCH_MIMO2_AB)
1527 search_tbl->ant_type = ANT_AB;
1528 else if (tbl->action == IWL_SISO_SWITCH_MIMO2_AC)
1529 search_tbl->ant_type = ANT_AC;
1531 search_tbl->ant_type = ANT_BC;
1533 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1536 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
1541 case IWL_SISO_SWITCH_GI:
1543 !(priv->current_ht_config.sgf &
1547 !(priv->current_ht_config.sgf &
1551 IWL_DEBUG_RATE(priv, "LQ: SISO toggle SGI/NGI\n");
1553 memcpy(search_tbl, tbl, sz);
1559 "SGI was set in GF+SISO\n");
1561 search_tbl->is_SGI = !tbl->is_SGI;
1562 rs_set_expected_tpt_table(lq_sta, search_tbl);
1564 s32 tpt = lq_sta->last_tpt / 100;
1565 if (tpt >= search_tbl->expected_tpt[index])
1568 search_tbl->current_rate =
1569 rate_n_flags_from_tbl(priv, search_tbl,
1572 case IWL_SISO_SWITCH_MIMO3_ABC:
1573 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO3\n");
1574 memcpy(search_tbl, tbl, sz);
1575 search_tbl->is_SGI = 0;
1576 search_tbl->ant_type = ANT_ABC;
1578 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1581 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1588 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
1589 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1591 if (tbl->action == start_action)
1594 search_tbl->lq_type = LQ_NONE;
1598 lq_sta->search_better_tbl = 1;
1600 if (tbl->action > IWL_SISO_SWITCH_MIMO3_ABC)
1601 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1606 * Try to switch to new modulation mode from MIMO2
1608 static int rs_move_mimo2_to_other(struct iwl_priv *priv,
1609 struct iwl_lq_sta *lq_sta,
1610 struct ieee80211_conf *conf,
1611 struct ieee80211_sta *sta, int index)
1613 s8 is_green = lq_sta->is_green;
1614 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1615 struct iwl_scale_tbl_info *search_tbl =
1616 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1617 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1618 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1619 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1620 u8 start_action = tbl->action;
1621 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1622 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1626 lq_sta->action_counter++;
1627 switch (tbl->action) {
1628 case IWL_MIMO2_SWITCH_ANTENNA1:
1629 case IWL_MIMO2_SWITCH_ANTENNA2:
1630 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle Antennas\n");
1632 if (tx_chains_num <= 2)
1635 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1638 memcpy(search_tbl, tbl, sz);
1639 if (rs_toggle_antenna(valid_tx_ant,
1640 &search_tbl->current_rate, search_tbl))
1643 case IWL_MIMO2_SWITCH_SISO_A:
1644 case IWL_MIMO2_SWITCH_SISO_B:
1645 case IWL_MIMO2_SWITCH_SISO_C:
1646 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to SISO\n");
1648 /* Set up new search table for SISO */
1649 memcpy(search_tbl, tbl, sz);
1651 if (tbl->action == IWL_MIMO2_SWITCH_SISO_A)
1652 search_tbl->ant_type = ANT_A;
1653 else if (tbl->action == IWL_MIMO2_SWITCH_SISO_B)
1654 search_tbl->ant_type = ANT_B;
1656 search_tbl->ant_type = ANT_C;
1658 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1661 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
1668 case IWL_MIMO2_SWITCH_GI:
1670 !(priv->current_ht_config.sgf &
1674 !(priv->current_ht_config.sgf &
1678 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle SGI/NGI\n");
1680 /* Set up new search table for MIMO2 */
1681 memcpy(search_tbl, tbl, sz);
1682 search_tbl->is_SGI = !tbl->is_SGI;
1683 rs_set_expected_tpt_table(lq_sta, search_tbl);
1685 * If active table already uses the fastest possible
1686 * modulation (dual stream with short guard interval),
1687 * and it's working well, there's no need to look
1688 * for a better type of modulation!
1691 s32 tpt = lq_sta->last_tpt / 100;
1692 if (tpt >= search_tbl->expected_tpt[index])
1695 search_tbl->current_rate =
1696 rate_n_flags_from_tbl(priv, search_tbl,
1700 case IWL_MIMO2_SWITCH_MIMO3_ABC:
1701 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to MIMO3\n");
1702 memcpy(search_tbl, tbl, sz);
1703 search_tbl->is_SGI = 0;
1704 search_tbl->ant_type = ANT_ABC;
1706 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1709 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1717 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1718 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
1720 if (tbl->action == start_action)
1723 search_tbl->lq_type = LQ_NONE;
1726 lq_sta->search_better_tbl = 1;
1728 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1729 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
1735 * Try to switch to new modulation mode from MIMO3
1737 static int rs_move_mimo3_to_other(struct iwl_priv *priv,
1738 struct iwl_lq_sta *lq_sta,
1739 struct ieee80211_conf *conf,
1740 struct ieee80211_sta *sta, int index)
1742 s8 is_green = lq_sta->is_green;
1743 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1744 struct iwl_scale_tbl_info *search_tbl =
1745 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1746 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1747 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1748 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1749 u8 start_action = tbl->action;
1750 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1751 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1755 lq_sta->action_counter++;
1756 switch (tbl->action) {
1757 case IWL_MIMO3_SWITCH_ANTENNA1:
1758 case IWL_MIMO3_SWITCH_ANTENNA2:
1759 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle Antennas\n");
1761 if (tx_chains_num <= 3)
1764 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1767 memcpy(search_tbl, tbl, sz);
1768 if (rs_toggle_antenna(valid_tx_ant,
1769 &search_tbl->current_rate, search_tbl))
1772 case IWL_MIMO3_SWITCH_SISO_A:
1773 case IWL_MIMO3_SWITCH_SISO_B:
1774 case IWL_MIMO3_SWITCH_SISO_C:
1775 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to SISO\n");
1777 /* Set up new search table for SISO */
1778 memcpy(search_tbl, tbl, sz);
1780 if (tbl->action == IWL_MIMO3_SWITCH_SISO_A)
1781 search_tbl->ant_type = ANT_A;
1782 else if (tbl->action == IWL_MIMO3_SWITCH_SISO_B)
1783 search_tbl->ant_type = ANT_B;
1785 search_tbl->ant_type = ANT_C;
1787 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1790 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
1797 case IWL_MIMO3_SWITCH_MIMO2_AB:
1798 case IWL_MIMO3_SWITCH_MIMO2_AC:
1799 case IWL_MIMO3_SWITCH_MIMO2_BC:
1800 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to MIMO2\n");
1802 memcpy(search_tbl, tbl, sz);
1803 search_tbl->is_SGI = 0;
1804 if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AB)
1805 search_tbl->ant_type = ANT_AB;
1806 else if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AC)
1807 search_tbl->ant_type = ANT_AC;
1809 search_tbl->ant_type = ANT_BC;
1811 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1814 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
1821 case IWL_MIMO3_SWITCH_GI:
1823 !(priv->current_ht_config.sgf &
1827 !(priv->current_ht_config.sgf &
1831 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle SGI/NGI\n");
1833 /* Set up new search table for MIMO */
1834 memcpy(search_tbl, tbl, sz);
1835 search_tbl->is_SGI = !tbl->is_SGI;
1836 rs_set_expected_tpt_table(lq_sta, search_tbl);
1838 * If active table already uses the fastest possible
1839 * modulation (dual stream with short guard interval),
1840 * and it's working well, there's no need to look
1841 * for a better type of modulation!
1844 s32 tpt = lq_sta->last_tpt / 100;
1845 if (tpt >= search_tbl->expected_tpt[index])
1848 search_tbl->current_rate =
1849 rate_n_flags_from_tbl(priv, search_tbl,
1854 if (tbl->action > IWL_MIMO3_SWITCH_GI)
1855 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
1857 if (tbl->action == start_action)
1860 search_tbl->lq_type = LQ_NONE;
1863 lq_sta->search_better_tbl = 1;
1865 if (tbl->action > IWL_MIMO3_SWITCH_GI)
1866 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
1872 * Check whether we should continue using same modulation mode, or
1873 * begin search for a new mode, based on:
1874 * 1) # tx successes or failures while using this mode
1875 * 2) # times calling this function
1876 * 3) elapsed time in this mode (not used, for now)
1878 static void rs_stay_in_table(struct iwl_lq_sta *lq_sta)
1880 struct iwl_scale_tbl_info *tbl;
1883 int flush_interval_passed = 0;
1884 struct iwl_priv *priv;
1887 active_tbl = lq_sta->active_tbl;
1889 tbl = &(lq_sta->lq_info[active_tbl]);
1891 /* If we've been disallowing search, see if we should now allow it */
1892 if (lq_sta->stay_in_tbl) {
1894 /* Elapsed time using current modulation mode */
1895 if (lq_sta->flush_timer)
1896 flush_interval_passed =
1898 (unsigned long)(lq_sta->flush_timer +
1899 IWL_RATE_SCALE_FLUSH_INTVL));
1902 * Check if we should allow search for new modulation mode.
1903 * If many frames have failed or succeeded, or we've used
1904 * this same modulation for a long time, allow search, and
1905 * reset history stats that keep track of whether we should
1906 * allow a new search. Also (below) reset all bitmaps and
1907 * stats in active history.
1909 if ((lq_sta->total_failed > lq_sta->max_failure_limit) ||
1910 (lq_sta->total_success > lq_sta->max_success_limit) ||
1911 ((!lq_sta->search_better_tbl) && (lq_sta->flush_timer)
1912 && (flush_interval_passed))) {
1913 IWL_DEBUG_RATE(priv, "LQ: stay is expired %d %d %d\n:",
1914 lq_sta->total_failed,
1915 lq_sta->total_success,
1916 flush_interval_passed);
1918 /* Allow search for new mode */
1919 lq_sta->stay_in_tbl = 0; /* only place reset */
1920 lq_sta->total_failed = 0;
1921 lq_sta->total_success = 0;
1922 lq_sta->flush_timer = 0;
1925 * Else if we've used this modulation mode enough repetitions
1926 * (regardless of elapsed time or success/failure), reset
1927 * history bitmaps and rate-specific stats for all rates in
1931 lq_sta->table_count++;
1932 if (lq_sta->table_count >=
1933 lq_sta->table_count_limit) {
1934 lq_sta->table_count = 0;
1936 IWL_DEBUG_RATE(priv, "LQ: stay in table clear win\n");
1937 for (i = 0; i < IWL_RATE_COUNT; i++)
1938 rs_rate_scale_clear_window(
1943 /* If transitioning to allow "search", reset all history
1944 * bitmaps and stats in active table (this will become the new
1945 * "search" table). */
1946 if (!lq_sta->stay_in_tbl) {
1947 for (i = 0; i < IWL_RATE_COUNT; i++)
1948 rs_rate_scale_clear_window(&(tbl->win[i]));
1954 * Do rate scaling and search for new modulation mode.
1956 static void rs_rate_scale_perform(struct iwl_priv *priv,
1957 struct ieee80211_hdr *hdr,
1958 struct ieee80211_sta *sta,
1959 struct iwl_lq_sta *lq_sta)
1961 struct ieee80211_hw *hw = priv->hw;
1962 struct ieee80211_conf *conf = &hw->conf;
1963 int low = IWL_RATE_INVALID;
1964 int high = IWL_RATE_INVALID;
1967 struct iwl_rate_scale_data *window = NULL;
1968 int current_tpt = IWL_INVALID_VALUE;
1969 int low_tpt = IWL_INVALID_VALUE;
1970 int high_tpt = IWL_INVALID_VALUE;
1972 s8 scale_action = 0;
1975 struct iwl_scale_tbl_info *tbl, *tbl1;
1976 u16 rate_scale_index_msk = 0;
1983 u8 tid = MAX_TID_COUNT;
1984 struct iwl_tid_data *tid_data;
1986 IWL_DEBUG_RATE(priv, "rate scale calculate new rate for skb\n");
1988 /* Send management frames and broadcast/multicast data using
1990 /* TODO: this could probably be improved.. */
1991 if (!ieee80211_is_data(hdr->frame_control) ||
1992 is_multicast_ether_addr(hdr->addr1))
1995 if (!sta || !lq_sta)
1998 lq_sta->supp_rates = sta->supp_rates[lq_sta->band];
2000 tid = rs_tl_add_packet(lq_sta, hdr);
2003 * Select rate-scale / modulation-mode table to work with in
2004 * the rest of this function: "search" if searching for better
2005 * modulation mode, or "active" if doing rate scaling within a mode.
2007 if (!lq_sta->search_better_tbl)
2008 active_tbl = lq_sta->active_tbl;
2010 active_tbl = 1 - lq_sta->active_tbl;
2012 tbl = &(lq_sta->lq_info[active_tbl]);
2013 is_green = lq_sta->is_green;
2015 /* current tx rate */
2016 index = lq_sta->last_txrate_idx;
2018 IWL_DEBUG_RATE(priv, "Rate scale index %d for type %d\n", index,
2021 /* rates available for this association, and for modulation mode */
2022 rate_mask = rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
2024 IWL_DEBUG_RATE(priv, "mask 0x%04X \n", rate_mask);
2026 /* mask with station rate restriction */
2027 if (is_legacy(tbl->lq_type)) {
2028 if (lq_sta->band == IEEE80211_BAND_5GHZ)
2029 /* supp_rates has no CCK bits in A mode */
2030 rate_scale_index_msk = (u16) (rate_mask &
2031 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
2033 rate_scale_index_msk = (u16) (rate_mask &
2034 lq_sta->supp_rates);
2037 rate_scale_index_msk = rate_mask;
2039 if (!rate_scale_index_msk)
2040 rate_scale_index_msk = rate_mask;
2042 if (!((1 << index) & rate_scale_index_msk)) {
2043 IWL_ERR(priv, "Current Rate is not valid\n");
2047 /* Get expected throughput table and history window for current rate */
2048 if (!tbl->expected_tpt) {
2049 IWL_ERR(priv, "tbl->expected_tpt is NULL\n");
2053 /* force user max rate if set by user */
2054 if ((lq_sta->max_rate_idx != -1) &&
2055 (lq_sta->max_rate_idx < index)) {
2056 index = lq_sta->max_rate_idx;
2058 window = &(tbl->win[index]);
2062 window = &(tbl->win[index]);
2065 * If there is not enough history to calculate actual average
2066 * throughput, keep analyzing results of more tx frames, without
2067 * changing rate or mode (bypass most of the rest of this function).
2068 * Set up new rate table in uCode only if old rate is not supported
2069 * in current association (use new rate found above).
2071 fail_count = window->counter - window->success_counter;
2072 if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
2073 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
2074 IWL_DEBUG_RATE(priv, "LQ: still below TH. succ=%d total=%d "
2076 window->success_counter, window->counter, index);
2078 /* Can't calculate this yet; not enough history */
2079 window->average_tpt = IWL_INVALID_VALUE;
2081 /* Should we stay with this modulation mode,
2082 * or search for a new one? */
2083 rs_stay_in_table(lq_sta);
2088 /* Else we have enough samples; calculate estimate of
2089 * actual average throughput */
2091 BUG_ON(window->average_tpt != ((window->success_ratio *
2092 tbl->expected_tpt[index] + 64) / 128));
2094 /* If we are searching for better modulation mode, check success. */
2095 if (lq_sta->search_better_tbl) {
2097 /* If good success, continue using the "search" mode;
2098 * no need to send new link quality command, since we're
2099 * continuing to use the setup that we've been trying. */
2100 if (window->average_tpt > lq_sta->last_tpt) {
2102 IWL_DEBUG_RATE(priv, "LQ: SWITCHING TO NEW TABLE "
2103 "suc=%d cur-tpt=%d old-tpt=%d\n",
2104 window->success_ratio,
2105 window->average_tpt,
2108 if (!is_legacy(tbl->lq_type))
2109 lq_sta->enable_counter = 1;
2111 /* Swap tables; "search" becomes "active" */
2112 lq_sta->active_tbl = active_tbl;
2113 current_tpt = window->average_tpt;
2115 /* Else poor success; go back to mode in "active" table */
2118 IWL_DEBUG_RATE(priv, "LQ: GOING BACK TO THE OLD TABLE "
2119 "suc=%d cur-tpt=%d old-tpt=%d\n",
2120 window->success_ratio,
2121 window->average_tpt,
2124 /* Nullify "search" table */
2125 tbl->lq_type = LQ_NONE;
2127 /* Revert to "active" table */
2128 active_tbl = lq_sta->active_tbl;
2129 tbl = &(lq_sta->lq_info[active_tbl]);
2131 /* Revert to "active" rate and throughput info */
2132 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
2133 current_tpt = lq_sta->last_tpt;
2135 /* Need to set up a new rate table in uCode */
2139 /* Either way, we've made a decision; modulation mode
2140 * search is done, allow rate adjustment next time. */
2141 lq_sta->search_better_tbl = 0;
2142 done_search = 1; /* Don't switch modes below! */
2146 /* (Else) not in search of better modulation mode, try for better
2147 * starting rate, while staying in this mode. */
2148 high_low = rs_get_adjacent_rate(priv, index, rate_scale_index_msk,
2150 low = high_low & 0xff;
2151 high = (high_low >> 8) & 0xff;
2153 /* If user set max rate, dont allow higher than user constrain */
2154 if ((lq_sta->max_rate_idx != -1) &&
2155 (lq_sta->max_rate_idx < high))
2156 high = IWL_RATE_INVALID;
2158 sr = window->success_ratio;
2160 /* Collect measured throughputs for current and adjacent rates */
2161 current_tpt = window->average_tpt;
2162 if (low != IWL_RATE_INVALID)
2163 low_tpt = tbl->win[low].average_tpt;
2164 if (high != IWL_RATE_INVALID)
2165 high_tpt = tbl->win[high].average_tpt;
2169 /* Too many failures, decrease rate */
2170 if ((sr <= IWL_RATE_DECREASE_TH) || (current_tpt == 0)) {
2171 IWL_DEBUG_RATE(priv, "decrease rate because of low success_ratio\n");
2174 /* No throughput measured yet for adjacent rates; try increase. */
2175 } else if ((low_tpt == IWL_INVALID_VALUE) &&
2176 (high_tpt == IWL_INVALID_VALUE)) {
2178 if (high != IWL_RATE_INVALID && sr >= IWL_RATE_INCREASE_TH)
2180 else if (low != IWL_RATE_INVALID)
2184 /* Both adjacent throughputs are measured, but neither one has better
2185 * throughput; we're using the best rate, don't change it! */
2186 else if ((low_tpt != IWL_INVALID_VALUE) &&
2187 (high_tpt != IWL_INVALID_VALUE) &&
2188 (low_tpt < current_tpt) &&
2189 (high_tpt < current_tpt))
2192 /* At least one adjacent rate's throughput is measured,
2193 * and may have better performance. */
2195 /* Higher adjacent rate's throughput is measured */
2196 if (high_tpt != IWL_INVALID_VALUE) {
2197 /* Higher rate has better throughput */
2198 if (high_tpt > current_tpt &&
2199 sr >= IWL_RATE_INCREASE_TH) {
2205 /* Lower adjacent rate's throughput is measured */
2206 } else if (low_tpt != IWL_INVALID_VALUE) {
2207 /* Lower rate has better throughput */
2208 if (low_tpt > current_tpt) {
2209 IWL_DEBUG_RATE(priv,
2210 "decrease rate because of low tpt\n");
2212 } else if (sr >= IWL_RATE_INCREASE_TH) {
2218 /* Sanity check; asked for decrease, but success rate or throughput
2219 * has been good at old rate. Don't change it. */
2220 if ((scale_action == -1) && (low != IWL_RATE_INVALID) &&
2221 ((sr > IWL_RATE_HIGH_TH) ||
2222 (current_tpt > (100 * tbl->expected_tpt[low]))))
2225 switch (scale_action) {
2227 /* Decrease starting rate, update uCode's rate table */
2228 if (low != IWL_RATE_INVALID) {
2234 /* Increase starting rate, update uCode's rate table */
2235 if (high != IWL_RATE_INVALID) {
2247 IWL_DEBUG_RATE(priv, "choose rate scale index %d action %d low %d "
2248 "high %d type %d\n",
2249 index, scale_action, low, high, tbl->lq_type);
2252 /* Replace uCode's rate table for the destination station. */
2254 rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
2255 rs_fill_link_cmd(priv, lq_sta, rate);
2256 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
2259 /* Should we stay with this modulation mode, or search for a new one? */
2260 rs_stay_in_table(lq_sta);
2263 * Search for new modulation mode if we're:
2264 * 1) Not changing rates right now
2265 * 2) Not just finishing up a search
2266 * 3) Allowing a new search
2268 if (!update_lq && !done_search && !lq_sta->stay_in_tbl && window->counter) {
2269 /* Save current throughput to compare with "search" throughput*/
2270 lq_sta->last_tpt = current_tpt;
2272 /* Select a new "search" modulation mode to try.
2273 * If one is found, set up the new "search" table. */
2274 if (is_legacy(tbl->lq_type))
2275 rs_move_legacy_other(priv, lq_sta, conf, sta, index);
2276 else if (is_siso(tbl->lq_type))
2277 rs_move_siso_to_other(priv, lq_sta, conf, sta, index);
2278 else if (is_mimo2(tbl->lq_type))
2279 rs_move_mimo2_to_other(priv, lq_sta, conf, sta, index);
2281 rs_move_mimo3_to_other(priv, lq_sta, conf, sta, index);
2283 /* If new "search" mode was selected, set up in uCode table */
2284 if (lq_sta->search_better_tbl) {
2285 /* Access the "search" table, clear its history. */
2286 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
2287 for (i = 0; i < IWL_RATE_COUNT; i++)
2288 rs_rate_scale_clear_window(&(tbl->win[i]));
2290 /* Use new "search" start rate */
2291 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
2293 IWL_DEBUG_RATE(priv, "Switch current mcs: %X index: %d\n",
2294 tbl->current_rate, index);
2295 rs_fill_link_cmd(priv, lq_sta, tbl->current_rate);
2296 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
2299 /* If the "active" (non-search) mode was legacy,
2300 * and we've tried switching antennas,
2301 * but we haven't been able to try HT modes (not available),
2302 * stay with best antenna legacy modulation for a while
2303 * before next round of mode comparisons. */
2304 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
2305 if (is_legacy(tbl1->lq_type) && !conf_is_ht(conf) &&
2306 lq_sta->action_counter >= 1) {
2307 lq_sta->action_counter = 0;
2308 IWL_DEBUG_RATE(priv, "LQ: STAY in legacy table\n");
2309 rs_set_stay_in_table(priv, 1, lq_sta);
2312 /* If we're in an HT mode, and all 3 mode switch actions
2313 * have been tried and compared, stay in this best modulation
2314 * mode for a while before next round of mode comparisons. */
2315 if (lq_sta->enable_counter &&
2316 (lq_sta->action_counter >= IWL_ACTION_LIMIT)) {
2317 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2318 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
2319 (tid != MAX_TID_COUNT)) {
2321 &priv->stations[lq_sta->lq.sta_id].tid[tid];
2322 if (tid_data->agg.state == IWL_AGG_OFF) {
2323 IWL_DEBUG_RATE(priv,
2324 "try to aggregate tid %d\n",
2326 rs_tl_turn_on_agg(priv, tid,
2330 lq_sta->action_counter = 0;
2331 rs_set_stay_in_table(priv, 0, lq_sta);
2335 * Else, don't search for a new modulation mode.
2336 * Put new timestamp in stay-in-modulation-mode flush timer if:
2337 * 1) Not changing rates right now
2338 * 2) Not just finishing up a search
2339 * 3) flush timer is empty
2342 if ((!update_lq) && (!done_search) && (!lq_sta->flush_timer))
2343 lq_sta->flush_timer = jiffies;
2347 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
2349 lq_sta->last_txrate_idx = i;
2355 static void rs_initialize_lq(struct iwl_priv *priv,
2356 struct ieee80211_conf *conf,
2357 struct ieee80211_sta *sta,
2358 struct iwl_lq_sta *lq_sta)
2360 struct iwl_scale_tbl_info *tbl;
2364 u8 use_green = rs_use_green(priv, conf);
2368 if (!sta || !lq_sta)
2371 i = lq_sta->last_txrate_idx;
2373 if ((lq_sta->lq.sta_id == 0xff) &&
2374 (priv->iw_mode == NL80211_IFTYPE_ADHOC))
2377 valid_tx_ant = priv->hw_params.valid_tx_ant;
2379 if (!lq_sta->search_better_tbl)
2380 active_tbl = lq_sta->active_tbl;
2382 active_tbl = 1 - lq_sta->active_tbl;
2384 tbl = &(lq_sta->lq_info[active_tbl]);
2386 if ((i < 0) || (i >= IWL_RATE_COUNT))
2389 rate = iwl_rates[i].plcp;
2390 tbl->ant_type = first_antenna(valid_tx_ant);
2391 rate |= tbl->ant_type << RATE_MCS_ANT_POS;
2393 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
2394 rate |= RATE_MCS_CCK_MSK;
2396 rs_get_tbl_info_from_mcs(rate, priv->band, tbl, &rate_idx);
2397 if (!rs_is_valid_ant(valid_tx_ant, tbl->ant_type))
2398 rs_toggle_antenna(valid_tx_ant, &rate, tbl);
2400 rate = rate_n_flags_from_tbl(priv, tbl, rate_idx, use_green);
2401 tbl->current_rate = rate;
2402 rs_set_expected_tpt_table(lq_sta, tbl);
2403 rs_fill_link_cmd(NULL, lq_sta, rate);
2404 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
2409 static void rs_get_rate(void *priv_r, struct ieee80211_sta *sta, void *priv_sta,
2410 struct ieee80211_tx_rate_control *txrc)
2413 struct sk_buff *skb = txrc->skb;
2414 struct ieee80211_supported_band *sband = txrc->sband;
2415 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
2416 struct ieee80211_conf *conf = &priv->hw->conf;
2417 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2418 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2419 struct iwl_lq_sta *lq_sta = priv_sta;
2423 IWL_DEBUG_RATE_LIMIT(priv, "rate scale calculate new rate for skb\n");
2425 /* Get max rate if user set max rate */
2427 lq_sta->max_rate_idx = txrc->max_rate_idx;
2428 if ((sband->band == IEEE80211_BAND_5GHZ) &&
2429 (lq_sta->max_rate_idx != -1))
2430 lq_sta->max_rate_idx += IWL_FIRST_OFDM_RATE;
2431 if ((lq_sta->max_rate_idx < 0) ||
2432 (lq_sta->max_rate_idx >= IWL_RATE_COUNT))
2433 lq_sta->max_rate_idx = -1;
2437 mask_bit = sta->supp_rates[sband->band];
2439 /* Send management frames and broadcast/multicast data using lowest
2441 if (!ieee80211_is_data(hdr->frame_control) ||
2442 is_multicast_ether_addr(hdr->addr1) || !sta || !lq_sta) {
2444 info->control.rates[0].idx =
2445 rate_lowest_index(sband, NULL);
2447 info->control.rates[0].idx =
2448 rate_lowest_index(sband, sta);
2452 rate_idx = lq_sta->last_txrate_idx;
2454 if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) &&
2455 !lq_sta->ibss_sta_added) {
2456 u8 sta_id = priv->cfg->ops->smgmt->find_station(priv,
2459 if (sta_id == IWL_INVALID_STATION) {
2460 IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n",
2462 sta_id = priv->cfg->ops->smgmt->add_station(priv,
2466 if ((sta_id != IWL_INVALID_STATION)) {
2467 lq_sta->lq.sta_id = sta_id;
2468 lq_sta->lq.rs_table[0].rate_n_flags = 0;
2469 lq_sta->ibss_sta_added = 1;
2470 rs_initialize_lq(priv, conf, sta, lq_sta);
2474 if (rate_idx < 0 || rate_idx > IWL_RATE_COUNT)
2475 rate_idx = rate_lowest_index(sband, sta);
2476 else if (sband->band == IEEE80211_BAND_5GHZ)
2477 rate_idx -= IWL_FIRST_OFDM_RATE;
2479 info->control.rates[0].idx = rate_idx;
2482 static void *rs_alloc_sta(void *priv_rate, struct ieee80211_sta *sta,
2485 struct iwl_lq_sta *lq_sta;
2486 struct iwl_priv *priv;
2489 priv = (struct iwl_priv *)priv_rate;
2490 IWL_DEBUG_RATE(priv, "create station rate scale window\n");
2492 lq_sta = kzalloc(sizeof(struct iwl_lq_sta), gfp);
2496 lq_sta->lq.sta_id = 0xff;
2499 for (j = 0; j < LQ_SIZE; j++)
2500 for (i = 0; i < IWL_RATE_COUNT; i++)
2501 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
2506 static void rs_rate_init(void *priv_r, struct ieee80211_supported_band *sband,
2507 struct ieee80211_sta *sta, void *priv_sta)
2510 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
2511 struct ieee80211_conf *conf = &priv->hw->conf;
2512 struct iwl_lq_sta *lq_sta = priv_sta;
2517 lq_sta->flush_timer = 0;
2518 lq_sta->supp_rates = sta->supp_rates[sband->band];
2519 for (j = 0; j < LQ_SIZE; j++)
2520 for (i = 0; i < IWL_RATE_COUNT; i++)
2521 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
2523 IWL_DEBUG_RATE(priv, "LQ: *** rate scale station global init ***\n");
2524 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2525 * the lowest or the highest rate.. Could consider using RSSI from
2526 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2529 lq_sta->ibss_sta_added = 0;
2530 if (priv->iw_mode == NL80211_IFTYPE_AP) {
2531 u8 sta_id = priv->cfg->ops->smgmt->find_station(priv,
2534 /* for IBSS the call are from tasklet */
2535 IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n", sta->addr);
2537 if (sta_id == IWL_INVALID_STATION) {
2538 IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n", sta->addr);
2539 sta_id = priv->cfg->ops->smgmt->add_station(priv,
2543 if ((sta_id != IWL_INVALID_STATION)) {
2544 lq_sta->lq.sta_id = sta_id;
2545 lq_sta->lq.rs_table[0].rate_n_flags = 0;
2547 /* FIXME: this is w/a remove it later */
2548 priv->assoc_station_added = 1;
2552 lq_sta->max_rate_idx = -1;
2553 lq_sta->missed_rate_counter = IWL_MISSED_RATE_MAX;
2554 lq_sta->is_green = rs_use_green(priv, conf);
2555 lq_sta->active_legacy_rate = priv->active_rate & ~(0x1000);
2556 lq_sta->active_rate_basic = priv->active_rate_basic;
2557 lq_sta->band = priv->band;
2559 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2560 * supp_rates[] does not; shift to convert format, force 9 MBits off.
2562 lq_sta->active_siso_rate = sta->ht_cap.mcs.rx_mask[0] << 1;
2563 lq_sta->active_siso_rate |= sta->ht_cap.mcs.rx_mask[0] & 0x1;
2564 lq_sta->active_siso_rate &= ~((u16)0x2);
2565 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
2568 lq_sta->active_mimo2_rate = sta->ht_cap.mcs.rx_mask[1] << 1;
2569 lq_sta->active_mimo2_rate |= sta->ht_cap.mcs.rx_mask[1] & 0x1;
2570 lq_sta->active_mimo2_rate &= ~((u16)0x2);
2571 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2573 lq_sta->active_mimo3_rate = sta->ht_cap.mcs.rx_mask[2] << 1;
2574 lq_sta->active_mimo3_rate |= sta->ht_cap.mcs.rx_mask[2] & 0x1;
2575 lq_sta->active_mimo3_rate &= ~((u16)0x2);
2576 lq_sta->active_mimo3_rate <<= IWL_FIRST_OFDM_RATE;
2578 IWL_DEBUG_RATE(priv, "SISO-RATE=%X MIMO2-RATE=%X MIMO3-RATE=%X\n",
2579 lq_sta->active_siso_rate,
2580 lq_sta->active_mimo2_rate,
2581 lq_sta->active_mimo3_rate);
2583 /* These values will be overridden later */
2584 lq_sta->lq.general_params.single_stream_ant_msk = ANT_A;
2585 lq_sta->lq.general_params.dual_stream_ant_msk = ANT_AB;
2587 /* as default allow aggregation for all tids */
2588 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
2591 /* Find highest tx rate supported by hardware and destination station */
2592 mask_bit = sta->supp_rates[sband->band];
2593 count = sband->n_bitrates;
2594 if (sband->band == IEEE80211_BAND_5GHZ) {
2595 count += IWL_FIRST_OFDM_RATE;
2596 start_rate = IWL_FIRST_OFDM_RATE;
2597 mask_bit <<= IWL_FIRST_OFDM_RATE;
2600 mask_bit = mask_bit & lq_sta->active_legacy_rate;
2601 lq_sta->last_txrate_idx = 4;
2602 for (i = start_rate; i < count; i++)
2603 if (mask_bit & BIT(i))
2604 lq_sta->last_txrate_idx = i;
2606 rs_initialize_lq(priv, conf, sta, lq_sta);
2609 static void rs_fill_link_cmd(const struct iwl_priv *priv,
2610 struct iwl_lq_sta *lq_sta, u32 new_rate)
2612 struct iwl_scale_tbl_info tbl_type;
2615 int repeat_rate = 0;
2616 u8 ant_toggle_cnt = 0;
2617 u8 use_ht_possible = 1;
2618 u8 valid_tx_ant = 0;
2619 struct iwl_link_quality_cmd *lq_cmd = &lq_sta->lq;
2621 /* Override starting rate (index 0) if needed for debug purposes */
2622 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
2624 /* Interpret new_rate (rate_n_flags) */
2625 memset(&tbl_type, 0, sizeof(tbl_type));
2626 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
2627 &tbl_type, &rate_idx);
2629 /* How many times should we repeat the initial rate? */
2630 if (is_legacy(tbl_type.lq_type)) {
2632 repeat_rate = IWL_NUMBER_TRY;
2634 repeat_rate = IWL_HT_NUMBER_TRY;
2637 lq_cmd->general_params.mimo_delimiter =
2638 is_mimo(tbl_type.lq_type) ? 1 : 0;
2640 /* Fill 1st table entry (index 0) */
2641 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
2643 if (num_of_ant(tbl_type.ant_type) == 1) {
2644 lq_cmd->general_params.single_stream_ant_msk =
2646 } else if (num_of_ant(tbl_type.ant_type) == 2) {
2647 lq_cmd->general_params.dual_stream_ant_msk =
2649 } else if (num_of_ant(tbl_type.ant_type) == 3) {
2650 lq_cmd->general_params.dual_stream_ant_msk =
2652 } /* otherwise we don't modify the existing value */
2658 valid_tx_ant = priv->hw_params.valid_tx_ant;
2660 /* Fill rest of rate table */
2661 while (index < LINK_QUAL_MAX_RETRY_NUM) {
2662 /* Repeat initial/next rate.
2663 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2664 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
2665 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
2666 if (is_legacy(tbl_type.lq_type)) {
2667 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2670 rs_toggle_antenna(valid_tx_ant,
2671 &new_rate, &tbl_type))
2675 /* Override next rate if needed for debug purposes */
2676 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
2678 /* Fill next table entry */
2679 lq_cmd->rs_table[index].rate_n_flags =
2680 cpu_to_le32(new_rate);
2685 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band, &tbl_type,
2688 /* Indicate to uCode which entries might be MIMO.
2689 * If initial rate was MIMO, this will finally end up
2690 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
2691 if (is_mimo(tbl_type.lq_type))
2692 lq_cmd->general_params.mimo_delimiter = index;
2695 new_rate = rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
2698 /* How many times should we repeat the next rate? */
2699 if (is_legacy(tbl_type.lq_type)) {
2700 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2703 rs_toggle_antenna(valid_tx_ant,
2704 &new_rate, &tbl_type))
2707 repeat_rate = IWL_NUMBER_TRY;
2709 repeat_rate = IWL_HT_NUMBER_TRY;
2712 /* Don't allow HT rates after next pass.
2713 * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
2714 use_ht_possible = 0;
2716 /* Override next rate if needed for debug purposes */
2717 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
2719 /* Fill next table entry */
2720 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
2726 lq_cmd->agg_params.agg_frame_cnt_limit = 64;
2727 lq_cmd->agg_params.agg_dis_start_th = 3;
2728 lq_cmd->agg_params.agg_time_limit = cpu_to_le16(4000);
2731 static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
2735 /* rate scale requires free function to be implemented */
2736 static void rs_free(void *priv_rate)
2741 static void rs_free_sta(void *priv_r, struct ieee80211_sta *sta,
2744 struct iwl_lq_sta *lq_sta = priv_sta;
2745 struct iwl_priv *priv __maybe_unused = priv_r;
2747 IWL_DEBUG_RATE(priv, "enter\n");
2749 IWL_DEBUG_RATE(priv, "leave\n");
2753 #ifdef CONFIG_MAC80211_DEBUGFS
2754 static int open_file_generic(struct inode *inode, struct file *file)
2756 file->private_data = inode->i_private;
2759 static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
2760 u32 *rate_n_flags, int index)
2762 struct iwl_priv *priv;
2767 valid_tx_ant = priv->hw_params.valid_tx_ant;
2768 if (lq_sta->dbg_fixed_rate) {
2770 ((lq_sta->dbg_fixed_rate & RATE_MCS_ANT_ABC_MSK)
2771 >> RATE_MCS_ANT_POS);
2772 if ((valid_tx_ant & ant_sel_tx) == ant_sel_tx) {
2773 *rate_n_flags = lq_sta->dbg_fixed_rate;
2774 IWL_DEBUG_RATE(priv, "Fixed rate ON\n");
2776 lq_sta->dbg_fixed_rate = 0;
2778 "Invalid antenna selection 0x%X, Valid is 0x%X\n",
2779 ant_sel_tx, valid_tx_ant);
2780 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
2783 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
2787 static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
2788 const char __user *user_buf, size_t count, loff_t *ppos)
2790 struct iwl_lq_sta *lq_sta = file->private_data;
2791 struct iwl_priv *priv;
2797 memset(buf, 0, sizeof(buf));
2798 buf_size = min(count, sizeof(buf) - 1);
2799 if (copy_from_user(buf, user_buf, buf_size))
2802 if (sscanf(buf, "%x", &parsed_rate) == 1)
2803 lq_sta->dbg_fixed_rate = parsed_rate;
2805 lq_sta->dbg_fixed_rate = 0;
2807 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
2808 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2809 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2810 lq_sta->active_mimo3_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2812 IWL_DEBUG_RATE(priv, "sta_id %d rate 0x%X\n",
2813 lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
2815 if (lq_sta->dbg_fixed_rate) {
2816 rs_fill_link_cmd(NULL, lq_sta, lq_sta->dbg_fixed_rate);
2817 iwl_send_lq_cmd(lq_sta->drv, &lq_sta->lq, CMD_ASYNC);
2823 static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
2824 char __user *user_buf, size_t count, loff_t *ppos)
2831 struct iwl_lq_sta *lq_sta = file->private_data;
2832 struct iwl_priv *priv;
2833 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
2836 buff = kmalloc(1024, GFP_KERNEL);
2840 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
2841 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
2842 lq_sta->total_failed, lq_sta->total_success,
2843 lq_sta->active_legacy_rate);
2844 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
2845 lq_sta->dbg_fixed_rate);
2846 desc += sprintf(buff+desc, "valid_tx_ant %s%s%s\n",
2847 (priv->hw_params.valid_tx_ant & ANT_A) ? "ANT_A," : "",
2848 (priv->hw_params.valid_tx_ant & ANT_B) ? "ANT_B," : "",
2849 (priv->hw_params.valid_tx_ant & ANT_C) ? "ANT_C" : "");
2850 desc += sprintf(buff+desc, "lq type %s\n",
2851 (is_legacy(tbl->lq_type)) ? "legacy" : "HT");
2852 if (is_Ht(tbl->lq_type)) {
2853 desc += sprintf(buff+desc, " %s",
2854 (is_siso(tbl->lq_type)) ? "SISO" :
2855 ((is_mimo2(tbl->lq_type)) ? "MIMO2" : "MIMO3"));
2856 desc += sprintf(buff+desc, " %s",
2857 (tbl->is_fat) ? "40MHz" : "20MHz");
2858 desc += sprintf(buff+desc, " %s\n", (tbl->is_SGI) ? "SGI" : "");
2860 desc += sprintf(buff+desc, "general:"
2861 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
2862 lq_sta->lq.general_params.flags,
2863 lq_sta->lq.general_params.mimo_delimiter,
2864 lq_sta->lq.general_params.single_stream_ant_msk,
2865 lq_sta->lq.general_params.dual_stream_ant_msk);
2867 desc += sprintf(buff+desc, "agg:"
2868 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
2869 le16_to_cpu(lq_sta->lq.agg_params.agg_time_limit),
2870 lq_sta->lq.agg_params.agg_dis_start_th,
2871 lq_sta->lq.agg_params.agg_frame_cnt_limit);
2873 desc += sprintf(buff+desc,
2874 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
2875 lq_sta->lq.general_params.start_rate_index[0],
2876 lq_sta->lq.general_params.start_rate_index[1],
2877 lq_sta->lq.general_params.start_rate_index[2],
2878 lq_sta->lq.general_params.start_rate_index[3]);
2881 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
2882 desc += sprintf(buff+desc, " rate[%d] 0x%X\n",
2883 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags));
2885 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2890 static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
2891 .write = rs_sta_dbgfs_scale_table_write,
2892 .read = rs_sta_dbgfs_scale_table_read,
2893 .open = open_file_generic,
2895 static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
2896 char __user *user_buf, size_t count, loff_t *ppos)
2903 struct iwl_lq_sta *lq_sta = file->private_data;
2905 buff = kmalloc(1024, GFP_KERNEL);
2909 for (i = 0; i < LQ_SIZE; i++) {
2910 desc += sprintf(buff+desc, "%s type=%d SGI=%d FAT=%d DUP=%d\n"
2912 lq_sta->active_tbl == i ? "*" : "x",
2913 lq_sta->lq_info[i].lq_type,
2914 lq_sta->lq_info[i].is_SGI,
2915 lq_sta->lq_info[i].is_fat,
2916 lq_sta->lq_info[i].is_dup,
2917 lq_sta->lq_info[i].current_rate);
2918 for (j = 0; j < IWL_RATE_COUNT; j++) {
2919 desc += sprintf(buff+desc,
2920 "counter=%d success=%d %%=%d\n",
2921 lq_sta->lq_info[i].win[j].counter,
2922 lq_sta->lq_info[i].win[j].success_counter,
2923 lq_sta->lq_info[i].win[j].success_ratio);
2926 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2931 static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
2932 .read = rs_sta_dbgfs_stats_table_read,
2933 .open = open_file_generic,
2936 static void rs_add_debugfs(void *priv, void *priv_sta,
2939 struct iwl_lq_sta *lq_sta = priv_sta;
2940 lq_sta->rs_sta_dbgfs_scale_table_file =
2941 debugfs_create_file("rate_scale_table", 0600, dir,
2942 lq_sta, &rs_sta_dbgfs_scale_table_ops);
2943 lq_sta->rs_sta_dbgfs_stats_table_file =
2944 debugfs_create_file("rate_stats_table", 0600, dir,
2945 lq_sta, &rs_sta_dbgfs_stats_table_ops);
2946 lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
2947 debugfs_create_u8("tx_agg_tid_enable", 0600, dir,
2948 &lq_sta->tx_agg_tid_en);
2952 static void rs_remove_debugfs(void *priv, void *priv_sta)
2954 struct iwl_lq_sta *lq_sta = priv_sta;
2955 debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
2956 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
2957 debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
2961 static struct rate_control_ops rs_ops = {
2964 .tx_status = rs_tx_status,
2965 .get_rate = rs_get_rate,
2966 .rate_init = rs_rate_init,
2969 .alloc_sta = rs_alloc_sta,
2970 .free_sta = rs_free_sta,
2971 #ifdef CONFIG_MAC80211_DEBUGFS
2972 .add_sta_debugfs = rs_add_debugfs,
2973 .remove_sta_debugfs = rs_remove_debugfs,
2977 int iwlagn_rate_control_register(void)
2979 return ieee80211_rate_control_register(&rs_ops);
2982 void iwlagn_rate_control_unregister(void)
2984 ieee80211_rate_control_unregister(&rs_ops);