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).
194 /*FIXME:RS:need to separate tables for MIMO2/MIMO3*/
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_mimo20MHz[IWL_RATE_COUNT] = {
212 0, 0, 0, 0, 74, 74, 123, 155, 179, 214, 236, 244, 251
215 static s32 expected_tpt_mimo20MHzSGI[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_mimo40MHz[IWL_RATE_COUNT] = {
228 0, 0, 0, 0, 123, 123, 182, 214, 235, 264, 279, 285, 289
231 static s32 expected_tpt_mimo40MHzSGI[IWL_RATE_COUNT] = {
232 0, 0, 0, 0, 131, 131, 191, 222, 242, 270, 284, 289, 293
235 static inline u8 rs_extract_rate(u32 rate_n_flags)
237 return (u8)(rate_n_flags & 0xFF);
240 static void rs_rate_scale_clear_window(struct iwl_rate_scale_data *window)
243 window->success_counter = 0;
244 window->success_ratio = IWL_INVALID_VALUE;
246 window->average_tpt = IWL_INVALID_VALUE;
250 static inline u8 rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
252 return (ant_type & valid_antenna) == ant_type;
256 * removes the old data from the statistics. All data that is older than
257 * TID_MAX_TIME_DIFF, will be deleted.
259 static void rs_tl_rm_old_stats(struct iwl_traffic_load *tl, u32 curr_time)
261 /* The oldest age we want to keep */
262 u32 oldest_time = curr_time - TID_MAX_TIME_DIFF;
264 while (tl->queue_count &&
265 (tl->time_stamp < oldest_time)) {
266 tl->total -= tl->packet_count[tl->head];
267 tl->packet_count[tl->head] = 0;
268 tl->time_stamp += TID_QUEUE_CELL_SPACING;
271 if (tl->head >= TID_QUEUE_MAX_SIZE)
277 * increment traffic load value for tid and also remove
278 * any old values if passed the certain time period
280 static u8 rs_tl_add_packet(struct iwl_lq_sta *lq_data,
281 struct ieee80211_hdr *hdr)
283 u32 curr_time = jiffies_to_msecs(jiffies);
286 struct iwl_traffic_load *tl = NULL;
289 if (ieee80211_is_data_qos(hdr->frame_control)) {
290 u8 *qc = ieee80211_get_qos_ctl(hdr);
293 return MAX_TID_COUNT;
295 tl = &lq_data->load[tid];
297 curr_time -= curr_time % TID_ROUND_VALUE;
299 /* Happens only for the first packet. Initialize the data */
300 if (!(tl->queue_count)) {
302 tl->time_stamp = curr_time;
305 tl->packet_count[0] = 1;
306 return MAX_TID_COUNT;
309 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
310 index = time_diff / TID_QUEUE_CELL_SPACING;
312 /* The history is too long: remove data that is older than */
313 /* TID_MAX_TIME_DIFF */
314 if (index >= TID_QUEUE_MAX_SIZE)
315 rs_tl_rm_old_stats(tl, curr_time);
317 index = (tl->head + index) % TID_QUEUE_MAX_SIZE;
318 tl->packet_count[index] = tl->packet_count[index] + 1;
319 tl->total = tl->total + 1;
321 if ((index + 1) > tl->queue_count)
322 tl->queue_count = index + 1;
328 get the traffic load value for tid
330 static u32 rs_tl_get_load(struct iwl_lq_sta *lq_data, u8 tid)
332 u32 curr_time = jiffies_to_msecs(jiffies);
335 struct iwl_traffic_load *tl = NULL;
337 if (tid >= TID_MAX_LOAD_COUNT)
340 tl = &(lq_data->load[tid]);
342 curr_time -= curr_time % TID_ROUND_VALUE;
344 if (!(tl->queue_count))
347 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
348 index = time_diff / TID_QUEUE_CELL_SPACING;
350 /* The history is too long: remove data that is older than */
351 /* TID_MAX_TIME_DIFF */
352 if (index >= TID_QUEUE_MAX_SIZE)
353 rs_tl_rm_old_stats(tl, curr_time);
358 static void rs_tl_turn_on_agg_for_tid(struct iwl_priv *priv,
359 struct iwl_lq_sta *lq_data, u8 tid,
360 struct ieee80211_sta *sta)
362 if (rs_tl_get_load(lq_data, tid) > IWL_AGG_LOAD_THRESHOLD) {
363 IWL_DEBUG_HT(priv, "Starting Tx agg: STA: %pM tid: %d\n",
365 ieee80211_start_tx_ba_session(priv->hw, sta->addr, tid);
369 static void rs_tl_turn_on_agg(struct iwl_priv *priv, u8 tid,
370 struct iwl_lq_sta *lq_data,
371 struct ieee80211_sta *sta)
373 if ((tid < TID_MAX_LOAD_COUNT))
374 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
375 else if (tid == IWL_AGG_ALL_TID)
376 for (tid = 0; tid < TID_MAX_LOAD_COUNT; tid++)
377 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
380 static inline int get_num_of_ant_from_rate(u32 rate_n_flags)
382 return !!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
383 !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
384 !!(rate_n_flags & RATE_MCS_ANT_C_MSK);
388 * rs_collect_tx_data - Update the success/failure sliding window
390 * We keep a sliding window of the last 62 packets transmitted
391 * at this rate. window->data contains the bitmask of successful
394 static int rs_collect_tx_data(struct iwl_rate_scale_data *windows,
395 int scale_index, s32 tpt, int retries,
398 struct iwl_rate_scale_data *window = NULL;
399 static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
402 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
405 /* Select data for current tx bit rate */
406 window = &(windows[scale_index]);
409 * Keep track of only the latest 62 tx frame attempts in this rate's
410 * history window; anything older isn't really relevant any more.
411 * If we have filled up the sliding window, drop the oldest attempt;
412 * if the oldest attempt (highest bit in bitmap) shows "success",
413 * subtract "1" from the success counter (this is the main reason
414 * we keep these bitmaps!).
416 while (retries > 0) {
417 if (window->counter >= IWL_RATE_MAX_WINDOW) {
419 /* remove earliest */
420 window->counter = IWL_RATE_MAX_WINDOW - 1;
422 if (window->data & mask) {
423 window->data &= ~mask;
424 window->success_counter--;
428 /* Increment frames-attempted counter */
431 /* Shift bitmap by one frame (throw away oldest history),
432 * OR in "1", and increment "success" if this
433 * frame was successful. */
436 window->success_counter++;
444 /* Calculate current success ratio, avoid divide-by-0! */
445 if (window->counter > 0)
446 window->success_ratio = 128 * (100 * window->success_counter)
449 window->success_ratio = IWL_INVALID_VALUE;
451 fail_count = window->counter - window->success_counter;
453 /* Calculate average throughput, if we have enough history. */
454 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
455 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
456 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
458 window->average_tpt = IWL_INVALID_VALUE;
460 /* Tag this window as having been updated */
461 window->stamp = jiffies;
467 * Fill uCode API rate_n_flags field, based on "search" or "active" table.
469 /* FIXME:RS:remove this function and put the flags statically in the table */
470 static u32 rate_n_flags_from_tbl(struct iwl_priv *priv,
471 struct iwl_scale_tbl_info *tbl,
472 int index, u8 use_green)
474 u32 rate_n_flags = 0;
476 if (is_legacy(tbl->lq_type)) {
477 rate_n_flags = iwl_rates[index].plcp;
478 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
479 rate_n_flags |= RATE_MCS_CCK_MSK;
481 } else if (is_Ht(tbl->lq_type)) {
482 if (index > IWL_LAST_OFDM_RATE) {
483 IWL_ERR(priv, "Invalid HT rate index %d\n", index);
484 index = IWL_LAST_OFDM_RATE;
486 rate_n_flags = RATE_MCS_HT_MSK;
488 if (is_siso(tbl->lq_type))
489 rate_n_flags |= iwl_rates[index].plcp_siso;
490 else if (is_mimo2(tbl->lq_type))
491 rate_n_flags |= iwl_rates[index].plcp_mimo2;
493 rate_n_flags |= iwl_rates[index].plcp_mimo3;
495 IWL_ERR(priv, "Invalid tbl->lq_type %d\n", tbl->lq_type);
498 rate_n_flags |= ((tbl->ant_type << RATE_MCS_ANT_POS) &
499 RATE_MCS_ANT_ABC_MSK);
501 if (is_Ht(tbl->lq_type)) {
504 rate_n_flags |= RATE_MCS_DUP_MSK;
506 rate_n_flags |= RATE_MCS_FAT_MSK;
509 rate_n_flags |= RATE_MCS_SGI_MSK;
512 rate_n_flags |= RATE_MCS_GF_MSK;
513 if (is_siso(tbl->lq_type) && tbl->is_SGI) {
514 rate_n_flags &= ~RATE_MCS_SGI_MSK;
515 IWL_ERR(priv, "GF was set with SGI:SISO\n");
523 * Interpret uCode API's rate_n_flags format,
524 * fill "search" or "active" tx mode table.
526 static int rs_get_tbl_info_from_mcs(const u32 rate_n_flags,
527 enum ieee80211_band band,
528 struct iwl_scale_tbl_info *tbl,
531 u32 ant_msk = (rate_n_flags & RATE_MCS_ANT_ABC_MSK);
532 u8 num_of_ant = get_num_of_ant_from_rate(rate_n_flags);
535 *rate_idx = iwl_hwrate_to_plcp_idx(rate_n_flags);
537 if (*rate_idx == IWL_RATE_INVALID) {
541 tbl->is_SGI = 0; /* default legacy setup */
544 tbl->ant_type = (ant_msk >> RATE_MCS_ANT_POS);
545 tbl->lq_type = LQ_NONE;
547 /* legacy rate format */
548 if (!(rate_n_flags & RATE_MCS_HT_MSK)) {
549 if (num_of_ant == 1) {
550 if (band == IEEE80211_BAND_5GHZ)
557 if (rate_n_flags & RATE_MCS_SGI_MSK)
560 if ((rate_n_flags & RATE_MCS_FAT_MSK) ||
561 (rate_n_flags & RATE_MCS_DUP_MSK))
564 if (rate_n_flags & RATE_MCS_DUP_MSK)
567 mcs = rs_extract_rate(rate_n_flags);
570 if (mcs <= IWL_RATE_SISO_60M_PLCP) {
572 tbl->lq_type = LQ_SISO; /*else NONE*/
574 } else if (mcs <= IWL_RATE_MIMO2_60M_PLCP) {
576 tbl->lq_type = LQ_MIMO2;
580 tbl->lq_type = LQ_MIMO3;
586 /* switch to another antenna/antennas and return 1 */
587 /* if no other valid antenna found, return 0 */
588 static int rs_toggle_antenna(u32 valid_ant, u32 *rate_n_flags,
589 struct iwl_scale_tbl_info *tbl)
593 if (!tbl->ant_type || tbl->ant_type > ANT_ABC)
596 if (!rs_is_valid_ant(valid_ant, tbl->ant_type))
599 new_ant_type = ant_toggle_lookup[tbl->ant_type];
601 while ((new_ant_type != tbl->ant_type) &&
602 !rs_is_valid_ant(valid_ant, new_ant_type))
603 new_ant_type = ant_toggle_lookup[new_ant_type];
605 if (new_ant_type == tbl->ant_type)
608 tbl->ant_type = new_ant_type;
609 *rate_n_flags &= ~RATE_MCS_ANT_ABC_MSK;
610 *rate_n_flags |= new_ant_type << RATE_MCS_ANT_POS;
614 /* FIXME:RS: in 4965 we don't use greenfield at all */
615 /* FIXME:RS: don't use greenfield for now in TX */
617 static inline u8 rs_use_green(struct iwl_priv *priv, struct ieee80211_conf *conf)
619 return (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) &&
620 priv->current_ht_config.is_green_field &&
621 !priv->current_ht_config.non_GF_STA_present;
624 static inline u8 rs_use_green(struct iwl_priv *priv, struct ieee80211_conf *conf)
630 * rs_get_supported_rates - get the available rates
632 * if management frame or broadcast frame only return
633 * basic available rates.
636 static u16 rs_get_supported_rates(struct iwl_lq_sta *lq_sta,
637 struct ieee80211_hdr *hdr,
638 enum iwl_table_type rate_type)
640 if (hdr && is_multicast_ether_addr(hdr->addr1) &&
641 lq_sta->active_rate_basic)
642 return lq_sta->active_rate_basic;
644 if (is_legacy(rate_type)) {
645 return lq_sta->active_legacy_rate;
647 if (is_siso(rate_type))
648 return lq_sta->active_siso_rate;
649 else if (is_mimo2(rate_type))
650 return lq_sta->active_mimo2_rate;
652 return lq_sta->active_mimo3_rate;
656 static u16 rs_get_adjacent_rate(struct iwl_priv *priv, u8 index, u16 rate_mask,
659 u8 high = IWL_RATE_INVALID;
660 u8 low = IWL_RATE_INVALID;
662 /* 802.11A or ht walks to the next literal adjacent rate in
664 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
668 /* Find the previous rate that is in the rate mask */
670 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
671 if (rate_mask & mask) {
677 /* Find the next rate that is in the rate mask */
679 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
680 if (rate_mask & mask) {
686 return (high << 8) | low;
690 while (low != IWL_RATE_INVALID) {
691 low = iwl_rates[low].prev_rs;
692 if (low == IWL_RATE_INVALID)
694 if (rate_mask & (1 << low))
696 IWL_DEBUG_RATE(priv, "Skipping masked lower rate: %d\n", low);
700 while (high != IWL_RATE_INVALID) {
701 high = iwl_rates[high].next_rs;
702 if (high == IWL_RATE_INVALID)
704 if (rate_mask & (1 << high))
706 IWL_DEBUG_RATE(priv, "Skipping masked higher rate: %d\n", high);
709 return (high << 8) | low;
712 static u32 rs_get_lower_rate(struct iwl_lq_sta *lq_sta,
713 struct iwl_scale_tbl_info *tbl,
714 u8 scale_index, u8 ht_possible)
719 u8 switch_to_legacy = 0;
720 u8 is_green = lq_sta->is_green;
722 /* check if we need to switch from HT to legacy rates.
723 * assumption is that mandatory rates (1Mbps or 6Mbps)
724 * are always supported (spec demand) */
725 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
726 switch_to_legacy = 1;
727 scale_index = rs_ht_to_legacy[scale_index];
728 if (lq_sta->band == IEEE80211_BAND_5GHZ)
733 if (num_of_ant(tbl->ant_type) > 1)
734 tbl->ant_type = ANT_A;/*FIXME:RS*/
740 rate_mask = rs_get_supported_rates(lq_sta, NULL, tbl->lq_type);
742 /* Mask with station rate restriction */
743 if (is_legacy(tbl->lq_type)) {
744 /* supp_rates has no CCK bits in A mode */
745 if (lq_sta->band == IEEE80211_BAND_5GHZ)
746 rate_mask = (u16)(rate_mask &
747 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
749 rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
752 /* If we switched from HT to legacy, check current rate */
753 if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
758 high_low = rs_get_adjacent_rate(lq_sta->drv, scale_index, rate_mask,
760 low = high_low & 0xff;
762 if (low == IWL_RATE_INVALID)
766 return rate_n_flags_from_tbl(lq_sta->drv, tbl, low, is_green);
770 * mac80211 sends us Tx status
772 static void rs_tx_status(void *priv_r, struct ieee80211_supported_band *sband,
773 struct ieee80211_sta *sta, void *priv_sta,
778 int rs_index, index = 0;
779 struct iwl_lq_sta *lq_sta = priv_sta;
780 struct iwl_link_quality_cmd *table;
781 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
782 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
783 struct ieee80211_hw *hw = priv->hw;
784 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
785 struct iwl_rate_scale_data *window = NULL;
786 struct iwl_rate_scale_data *search_win = NULL;
788 struct iwl_scale_tbl_info tbl_type;
789 struct iwl_scale_tbl_info *curr_tbl, *search_tbl;
793 IWL_DEBUG_RATE_LIMIT(priv, "get frame ack response, update rate scale window\n");
795 if (!ieee80211_is_data(hdr->frame_control) ||
796 is_multicast_ether_addr(hdr->addr1))
799 /* This packet was aggregated but doesn't carry rate scale info */
800 if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
801 !(info->flags & IEEE80211_TX_STAT_AMPDU))
804 if (info->flags & IEEE80211_TX_STAT_AMPDU)
807 retries = info->status.rates[0].count - 1;
812 if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) &&
813 !lq_sta->ibss_sta_added)
817 active_index = lq_sta->active_tbl;
819 curr_tbl = &(lq_sta->lq_info[active_index]);
820 search_tbl = &(lq_sta->lq_info[(1 - active_index)]);
821 window = (struct iwl_rate_scale_data *)&(curr_tbl->win[0]);
822 search_win = (struct iwl_rate_scale_data *)&(search_tbl->win[0]);
825 * Ignore this Tx frame response if its initial rate doesn't match
826 * that of latest Link Quality command. There may be stragglers
827 * from a previous Link Quality command, but we're no longer interested
828 * in those; they're either from the "active" mode while we're trying
829 * to check "search" mode, or a prior "search" mode after we've moved
830 * to a new "search" mode (which might become the new "active" mode).
832 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
833 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
834 if (priv->band == IEEE80211_BAND_5GHZ)
835 rs_index -= IWL_FIRST_OFDM_RATE;
837 if ((info->status.rates[0].idx < 0) ||
838 (tbl_type.is_SGI != !!(info->status.rates[0].flags & IEEE80211_TX_RC_SHORT_GI)) ||
839 (tbl_type.is_fat != !!(info->status.rates[0].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)) ||
840 (tbl_type.is_dup != !!(info->status.rates[0].flags & IEEE80211_TX_RC_DUP_DATA)) ||
841 (tbl_type.ant_type != info->antenna_sel_tx) ||
842 (!!(tx_rate & RATE_MCS_HT_MSK) != !!(info->status.rates[0].flags & IEEE80211_TX_RC_MCS)) ||
843 (!!(tx_rate & RATE_MCS_GF_MSK) != !!(info->status.rates[0].flags & IEEE80211_TX_RC_GREEN_FIELD)) ||
844 (hw->wiphy->bands[priv->band]->bitrates[rs_index].bitrate !=
845 hw->wiphy->bands[info->band]->bitrates[info->status.rates[0].idx].bitrate)) {
846 IWL_DEBUG_RATE(priv, "initial rate does not match 0x%x\n", tx_rate);
847 /* the last LQ command could failed so the LQ in ucode not
848 * the same in driver sync up
850 lq_sta->missed_rate_counter++;
851 if (lq_sta->missed_rate_counter > IWL_MISSED_RATE_MAX) {
852 lq_sta->missed_rate_counter = 0;
853 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
858 lq_sta->missed_rate_counter = 0;
859 /* Update frame history window with "failure" for each Tx retry. */
861 /* Look up the rate and other info used for each tx attempt.
862 * Each tx attempt steps one entry deeper in the rate table. */
863 tx_rate = le32_to_cpu(table->rs_table[index].rate_n_flags);
864 rs_get_tbl_info_from_mcs(tx_rate, priv->band,
865 &tbl_type, &rs_index);
867 /* If type matches "search" table,
868 * add failure to "search" history */
869 if ((tbl_type.lq_type == search_tbl->lq_type) &&
870 (tbl_type.ant_type == search_tbl->ant_type) &&
871 (tbl_type.is_SGI == search_tbl->is_SGI)) {
872 if (search_tbl->expected_tpt)
873 tpt = search_tbl->expected_tpt[rs_index];
876 rs_collect_tx_data(search_win, rs_index, tpt, 1, 0);
878 /* Else if type matches "current/active" table,
879 * add failure to "current/active" history */
880 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
881 (tbl_type.ant_type == curr_tbl->ant_type) &&
882 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
883 if (curr_tbl->expected_tpt)
884 tpt = curr_tbl->expected_tpt[rs_index];
887 rs_collect_tx_data(window, rs_index, tpt, 1, 0);
890 /* If not searching for a new mode, increment failed counter
891 * ... this helps determine when to start searching again */
892 if (lq_sta->stay_in_tbl)
893 lq_sta->total_failed++;
900 * Find (by rate) the history window to update with final Tx attempt;
901 * if Tx was successful first try, use original rate,
902 * else look up the rate that was, finally, successful.
904 tx_rate = le32_to_cpu(table->rs_table[index].rate_n_flags);
905 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
907 /* Update frame history window with "success" if Tx got ACKed ... */
908 status = !!(info->flags & IEEE80211_TX_STAT_ACK);
910 /* If type matches "search" table,
911 * add final tx status to "search" history */
912 if ((tbl_type.lq_type == search_tbl->lq_type) &&
913 (tbl_type.ant_type == search_tbl->ant_type) &&
914 (tbl_type.is_SGI == search_tbl->is_SGI)) {
915 if (search_tbl->expected_tpt)
916 tpt = search_tbl->expected_tpt[rs_index];
919 if (info->flags & IEEE80211_TX_STAT_AMPDU)
920 rs_collect_tx_data(search_win, rs_index, tpt,
921 info->status.ampdu_ack_len,
922 info->status.ampdu_ack_map);
924 rs_collect_tx_data(search_win, rs_index, tpt,
926 /* Else if type matches "current/active" table,
927 * add final tx status to "current/active" history */
928 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
929 (tbl_type.ant_type == curr_tbl->ant_type) &&
930 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
931 if (curr_tbl->expected_tpt)
932 tpt = curr_tbl->expected_tpt[rs_index];
935 if (info->flags & IEEE80211_TX_STAT_AMPDU)
936 rs_collect_tx_data(window, rs_index, tpt,
937 info->status.ampdu_ack_len,
938 info->status.ampdu_ack_map);
940 rs_collect_tx_data(window, rs_index, tpt,
944 /* If not searching for new mode, increment success/failed counter
945 * ... these help determine when to start searching again */
946 if (lq_sta->stay_in_tbl) {
947 if (info->flags & IEEE80211_TX_STAT_AMPDU) {
948 lq_sta->total_success += info->status.ampdu_ack_map;
949 lq_sta->total_failed +=
950 (info->status.ampdu_ack_len - info->status.ampdu_ack_map);
953 lq_sta->total_success++;
955 lq_sta->total_failed++;
959 /* See if there's a better rate or modulation mode to try. */
960 if (sta && sta->supp_rates[sband->band])
961 rs_rate_scale_perform(priv, hdr, sta, lq_sta);
967 * Begin a period of staying with a selected modulation mode.
968 * Set "stay_in_tbl" flag to prevent any mode switches.
969 * Set frame tx success limits according to legacy vs. high-throughput,
970 * and reset overall (spanning all rates) tx success history statistics.
971 * These control how long we stay using same modulation mode before
972 * searching for a new mode.
974 static void rs_set_stay_in_table(struct iwl_priv *priv, u8 is_legacy,
975 struct iwl_lq_sta *lq_sta)
977 IWL_DEBUG_RATE(priv, "we are staying in the same table\n");
978 lq_sta->stay_in_tbl = 1; /* only place this gets set */
980 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
981 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
982 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
984 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
985 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
986 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
988 lq_sta->table_count = 0;
989 lq_sta->total_failed = 0;
990 lq_sta->total_success = 0;
994 * Find correct throughput table for given mode of modulation
996 static void rs_set_expected_tpt_table(struct iwl_lq_sta *lq_sta,
997 struct iwl_scale_tbl_info *tbl)
999 if (is_legacy(tbl->lq_type)) {
1000 if (!is_a_band(tbl->lq_type))
1001 tbl->expected_tpt = expected_tpt_G;
1003 tbl->expected_tpt = expected_tpt_A;
1004 } else if (is_siso(tbl->lq_type)) {
1005 if (tbl->is_fat && !lq_sta->is_dup)
1007 tbl->expected_tpt = expected_tpt_siso40MHzSGI;
1009 tbl->expected_tpt = expected_tpt_siso40MHz;
1010 else if (tbl->is_SGI)
1011 tbl->expected_tpt = expected_tpt_siso20MHzSGI;
1013 tbl->expected_tpt = expected_tpt_siso20MHz;
1015 } else if (is_mimo(tbl->lq_type)) { /* FIXME:need to separate mimo2/3 */
1016 if (tbl->is_fat && !lq_sta->is_dup)
1018 tbl->expected_tpt = expected_tpt_mimo40MHzSGI;
1020 tbl->expected_tpt = expected_tpt_mimo40MHz;
1021 else if (tbl->is_SGI)
1022 tbl->expected_tpt = expected_tpt_mimo20MHzSGI;
1024 tbl->expected_tpt = expected_tpt_mimo20MHz;
1026 tbl->expected_tpt = expected_tpt_G;
1030 * Find starting rate for new "search" high-throughput mode of modulation.
1031 * Goal is to find lowest expected rate (under perfect conditions) that is
1032 * above the current measured throughput of "active" mode, to give new mode
1033 * a fair chance to prove itself without too many challenges.
1035 * This gets called when transitioning to more aggressive modulation
1036 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
1037 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1038 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1039 * bit rate will typically need to increase, but not if performance was bad.
1041 static s32 rs_get_best_rate(struct iwl_priv *priv,
1042 struct iwl_lq_sta *lq_sta,
1043 struct iwl_scale_tbl_info *tbl, /* "search" */
1044 u16 rate_mask, s8 index)
1046 /* "active" values */
1047 struct iwl_scale_tbl_info *active_tbl =
1048 &(lq_sta->lq_info[lq_sta->active_tbl]);
1049 s32 active_sr = active_tbl->win[index].success_ratio;
1050 s32 active_tpt = active_tbl->expected_tpt[index];
1052 /* expected "search" throughput */
1053 s32 *tpt_tbl = tbl->expected_tpt;
1055 s32 new_rate, high, low, start_hi;
1059 new_rate = high = low = start_hi = IWL_RATE_INVALID;
1062 high_low = rs_get_adjacent_rate(priv, rate, rate_mask,
1065 low = high_low & 0xff;
1066 high = (high_low >> 8) & 0xff;
1069 * Lower the "search" bit rate, to give new "search" mode
1070 * approximately the same throughput as "active" if:
1072 * 1) "Active" mode has been working modestly well (but not
1073 * great), and expected "search" throughput (under perfect
1074 * conditions) at candidate rate is above the actual
1075 * measured "active" throughput (but less than expected
1076 * "active" throughput under perfect conditions).
1078 * 2) "Active" mode has been working perfectly or very well
1079 * and expected "search" throughput (under perfect
1080 * conditions) at candidate rate is above expected
1081 * "active" throughput (under perfect conditions).
1083 if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
1084 ((active_sr > IWL_RATE_DECREASE_TH) &&
1085 (active_sr <= IWL_RATE_HIGH_TH) &&
1086 (tpt_tbl[rate] <= active_tpt))) ||
1087 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
1088 (tpt_tbl[rate] > active_tpt))) {
1090 /* (2nd or later pass)
1091 * If we've already tried to raise the rate, and are
1092 * now trying to lower it, use the higher rate. */
1093 if (start_hi != IWL_RATE_INVALID) {
1094 new_rate = start_hi;
1100 /* Loop again with lower rate */
1101 if (low != IWL_RATE_INVALID)
1104 /* Lower rate not available, use the original */
1108 /* Else try to raise the "search" rate to match "active" */
1110 /* (2nd or later pass)
1111 * If we've already tried to lower the rate, and are
1112 * now trying to raise it, use the lower rate. */
1113 if (new_rate != IWL_RATE_INVALID)
1116 /* Loop again with higher rate */
1117 else if (high != IWL_RATE_INVALID) {
1121 /* Higher rate not available, use the original */
1133 * Set up search table for MIMO
1135 static int rs_switch_to_mimo2(struct iwl_priv *priv,
1136 struct iwl_lq_sta *lq_sta,
1137 struct ieee80211_conf *conf,
1138 struct ieee80211_sta *sta,
1139 struct iwl_scale_tbl_info *tbl, int index)
1143 s8 is_green = lq_sta->is_green;
1145 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1148 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
1149 == WLAN_HT_CAP_SM_PS_STATIC)
1152 /* Need both Tx chains/antennas to support MIMO */
1153 if (priv->hw_params.tx_chains_num < 2)
1156 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO2\n");
1158 tbl->lq_type = LQ_MIMO2;
1159 tbl->is_dup = lq_sta->is_dup;
1161 rate_mask = lq_sta->active_mimo2_rate;
1163 if (priv->current_ht_config.supported_chan_width
1164 == IWL_CHANNEL_WIDTH_40MHZ)
1169 /* FIXME: - don't toggle SGI here
1171 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
1175 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
1181 rs_set_expected_tpt_table(lq_sta, tbl);
1183 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1185 IWL_DEBUG_RATE(priv, "LQ: MIMO2 best rate %d mask %X\n", rate, rate_mask);
1187 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1188 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
1192 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
1194 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
1195 tbl->current_rate, is_green);
1200 * Set up search table for SISO
1202 static int rs_switch_to_siso(struct iwl_priv *priv,
1203 struct iwl_lq_sta *lq_sta,
1204 struct ieee80211_conf *conf,
1205 struct ieee80211_sta *sta,
1206 struct iwl_scale_tbl_info *tbl, int index)
1209 u8 is_green = lq_sta->is_green;
1212 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1215 IWL_DEBUG_RATE(priv, "LQ: try to switch to SISO\n");
1217 tbl->is_dup = lq_sta->is_dup;
1218 tbl->lq_type = LQ_SISO;
1220 rate_mask = lq_sta->active_siso_rate;
1222 if (priv->current_ht_config.supported_chan_width
1223 == IWL_CHANNEL_WIDTH_40MHZ)
1228 /* FIXME: - don't toggle SGI here
1230 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
1234 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
1241 tbl->is_SGI = 0; /*11n spec: no SGI in SISO+Greenfield*/
1243 rs_set_expected_tpt_table(lq_sta, tbl);
1244 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1246 IWL_DEBUG_RATE(priv, "LQ: get best rate %d mask %X\n", rate, rate_mask);
1247 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1248 IWL_DEBUG_RATE(priv, "can not switch with index %d rate mask %x\n",
1252 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
1253 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
1254 tbl->current_rate, is_green);
1259 * Try to switch to new modulation mode from legacy
1261 static int rs_move_legacy_other(struct iwl_priv *priv,
1262 struct iwl_lq_sta *lq_sta,
1263 struct ieee80211_conf *conf,
1264 struct ieee80211_sta *sta,
1267 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1268 struct iwl_scale_tbl_info *search_tbl =
1269 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1270 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1271 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1272 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1273 u8 start_action = tbl->action;
1274 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1275 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1279 switch (tbl->action) {
1280 case IWL_LEGACY_SWITCH_ANTENNA1:
1281 case IWL_LEGACY_SWITCH_ANTENNA2:
1282 IWL_DEBUG_RATE(priv, "LQ: Legacy toggle Antenna\n");
1284 lq_sta->action_counter++;
1286 if ((tbl->action == IWL_LEGACY_SWITCH_ANTENNA1 &&
1287 tx_chains_num <= 1) ||
1288 (tbl->action == IWL_LEGACY_SWITCH_ANTENNA2 &&
1289 tx_chains_num <= 2))
1292 /* Don't change antenna if success has been great */
1293 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1296 /* Set up search table to try other antenna */
1297 memcpy(search_tbl, tbl, sz);
1299 if (rs_toggle_antenna(valid_tx_ant,
1300 &search_tbl->current_rate, search_tbl)) {
1301 rs_set_expected_tpt_table(lq_sta, search_tbl);
1305 case IWL_LEGACY_SWITCH_SISO:
1306 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to SISO\n");
1308 /* Set up search table to try SISO */
1309 memcpy(search_tbl, tbl, sz);
1310 search_tbl->is_SGI = 0;
1311 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
1314 lq_sta->action_counter = 0;
1319 case IWL_LEGACY_SWITCH_MIMO2_AB:
1320 case IWL_LEGACY_SWITCH_MIMO2_AC:
1321 case IWL_LEGACY_SWITCH_MIMO2_BC:
1322 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO2\n");
1324 /* Set up search table to try MIMO */
1325 memcpy(search_tbl, tbl, sz);
1326 search_tbl->is_SGI = 0;
1328 if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AB)
1329 search_tbl->ant_type = ANT_AB;
1330 else if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AC)
1331 search_tbl->ant_type = ANT_AC;
1333 search_tbl->ant_type = ANT_BC;
1335 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1338 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
1341 lq_sta->action_counter = 0;
1347 if (tbl->action > IWL_LEGACY_SWITCH_MIMO2_BC)
1348 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
1350 if (tbl->action == start_action)
1354 search_tbl->lq_type = LQ_NONE;
1358 lq_sta->search_better_tbl = 1;
1360 if (tbl->action > IWL_LEGACY_SWITCH_MIMO2_BC)
1361 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
1367 * Try to switch to new modulation mode from SISO
1369 static int rs_move_siso_to_other(struct iwl_priv *priv,
1370 struct iwl_lq_sta *lq_sta,
1371 struct ieee80211_conf *conf,
1372 struct ieee80211_sta *sta, int index)
1374 u8 is_green = lq_sta->is_green;
1375 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1376 struct iwl_scale_tbl_info *search_tbl =
1377 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1378 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1379 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1380 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1381 u8 start_action = tbl->action;
1382 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1383 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1387 lq_sta->action_counter++;
1388 switch (tbl->action) {
1389 case IWL_SISO_SWITCH_ANTENNA1:
1390 case IWL_SISO_SWITCH_ANTENNA2:
1391 IWL_DEBUG_RATE(priv, "LQ: SISO toggle Antenna\n");
1393 if ((tbl->action == IWL_SISO_SWITCH_ANTENNA1 &&
1394 tx_chains_num <= 1) ||
1395 (tbl->action == IWL_SISO_SWITCH_ANTENNA2 &&
1396 tx_chains_num <= 2))
1399 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1402 memcpy(search_tbl, tbl, sz);
1403 if (rs_toggle_antenna(valid_tx_ant,
1404 &search_tbl->current_rate, search_tbl))
1407 case IWL_SISO_SWITCH_MIMO2_AB:
1408 case IWL_SISO_SWITCH_MIMO2_AC:
1409 case IWL_SISO_SWITCH_MIMO2_BC:
1410 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO2\n");
1411 memcpy(search_tbl, tbl, sz);
1412 search_tbl->is_SGI = 0;
1414 if (tbl->action == IWL_SISO_SWITCH_MIMO2_AB)
1415 search_tbl->ant_type = ANT_AB;
1416 else if (tbl->action == IWL_SISO_SWITCH_MIMO2_AC)
1417 search_tbl->ant_type = ANT_AC;
1419 search_tbl->ant_type = ANT_BC;
1421 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1424 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
1429 case IWL_SISO_SWITCH_GI:
1431 !(priv->current_ht_config.sgf &
1435 !(priv->current_ht_config.sgf &
1439 IWL_DEBUG_RATE(priv, "LQ: SISO toggle SGI/NGI\n");
1441 memcpy(search_tbl, tbl, sz);
1447 "SGI was set in GF+SISO\n");
1449 search_tbl->is_SGI = !tbl->is_SGI;
1450 rs_set_expected_tpt_table(lq_sta, search_tbl);
1452 s32 tpt = lq_sta->last_tpt / 100;
1453 if (tpt >= search_tbl->expected_tpt[index])
1456 search_tbl->current_rate =
1457 rate_n_flags_from_tbl(priv, search_tbl,
1462 if (tbl->action > IWL_SISO_SWITCH_GI)
1463 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1465 if (tbl->action == start_action)
1468 search_tbl->lq_type = LQ_NONE;
1472 lq_sta->search_better_tbl = 1;
1474 if (tbl->action > IWL_SISO_SWITCH_GI)
1475 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1480 * Try to switch to new modulation mode from MIMO
1482 static int rs_move_mimo_to_other(struct iwl_priv *priv,
1483 struct iwl_lq_sta *lq_sta,
1484 struct ieee80211_conf *conf,
1485 struct ieee80211_sta *sta, int index)
1487 s8 is_green = lq_sta->is_green;
1488 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1489 struct iwl_scale_tbl_info *search_tbl =
1490 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1491 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1492 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1493 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1494 u8 start_action = tbl->action;
1495 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1496 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1500 lq_sta->action_counter++;
1501 switch (tbl->action) {
1502 case IWL_MIMO2_SWITCH_ANTENNA1:
1503 case IWL_MIMO2_SWITCH_ANTENNA2:
1504 IWL_DEBUG_RATE(priv, "LQ: MIMO toggle Antennas\n");
1506 if (tx_chains_num <= 2)
1509 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1512 memcpy(search_tbl, tbl, sz);
1513 if (rs_toggle_antenna(valid_tx_ant,
1514 &search_tbl->current_rate, search_tbl))
1517 case IWL_MIMO2_SWITCH_SISO_A:
1518 case IWL_MIMO2_SWITCH_SISO_B:
1519 case IWL_MIMO2_SWITCH_SISO_C:
1520 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to SISO\n");
1522 /* Set up new search table for SISO */
1523 memcpy(search_tbl, tbl, sz);
1525 if (tbl->action == IWL_MIMO2_SWITCH_SISO_A)
1526 search_tbl->ant_type = ANT_A;
1527 else if (tbl->action == IWL_MIMO2_SWITCH_SISO_B)
1528 search_tbl->ant_type = ANT_B;
1530 search_tbl->ant_type = ANT_C;
1532 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1535 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
1542 case IWL_MIMO2_SWITCH_GI:
1544 !(priv->current_ht_config.sgf &
1548 !(priv->current_ht_config.sgf &
1552 IWL_DEBUG_RATE(priv, "LQ: MIMO toggle SGI/NGI\n");
1554 /* Set up new search table for MIMO */
1555 memcpy(search_tbl, tbl, sz);
1556 search_tbl->is_SGI = !tbl->is_SGI;
1557 rs_set_expected_tpt_table(lq_sta, search_tbl);
1559 * If active table already uses the fastest possible
1560 * modulation (dual stream with short guard interval),
1561 * and it's working well, there's no need to look
1562 * for a better type of modulation!
1565 s32 tpt = lq_sta->last_tpt / 100;
1566 if (tpt >= search_tbl->expected_tpt[index])
1569 search_tbl->current_rate =
1570 rate_n_flags_from_tbl(priv, search_tbl,
1576 if (tbl->action > IWL_MIMO2_SWITCH_GI)
1577 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
1579 if (tbl->action == start_action)
1582 search_tbl->lq_type = LQ_NONE;
1585 lq_sta->search_better_tbl = 1;
1587 if (tbl->action > IWL_MIMO2_SWITCH_GI)
1588 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
1594 * Check whether we should continue using same modulation mode, or
1595 * begin search for a new mode, based on:
1596 * 1) # tx successes or failures while using this mode
1597 * 2) # times calling this function
1598 * 3) elapsed time in this mode (not used, for now)
1600 static void rs_stay_in_table(struct iwl_lq_sta *lq_sta)
1602 struct iwl_scale_tbl_info *tbl;
1605 int flush_interval_passed = 0;
1606 struct iwl_priv *priv;
1609 active_tbl = lq_sta->active_tbl;
1611 tbl = &(lq_sta->lq_info[active_tbl]);
1613 /* If we've been disallowing search, see if we should now allow it */
1614 if (lq_sta->stay_in_tbl) {
1616 /* Elapsed time using current modulation mode */
1617 if (lq_sta->flush_timer)
1618 flush_interval_passed =
1620 (unsigned long)(lq_sta->flush_timer +
1621 IWL_RATE_SCALE_FLUSH_INTVL));
1624 * Check if we should allow search for new modulation mode.
1625 * If many frames have failed or succeeded, or we've used
1626 * this same modulation for a long time, allow search, and
1627 * reset history stats that keep track of whether we should
1628 * allow a new search. Also (below) reset all bitmaps and
1629 * stats in active history.
1631 if ((lq_sta->total_failed > lq_sta->max_failure_limit) ||
1632 (lq_sta->total_success > lq_sta->max_success_limit) ||
1633 ((!lq_sta->search_better_tbl) && (lq_sta->flush_timer)
1634 && (flush_interval_passed))) {
1635 IWL_DEBUG_RATE(priv, "LQ: stay is expired %d %d %d\n:",
1636 lq_sta->total_failed,
1637 lq_sta->total_success,
1638 flush_interval_passed);
1640 /* Allow search for new mode */
1641 lq_sta->stay_in_tbl = 0; /* only place reset */
1642 lq_sta->total_failed = 0;
1643 lq_sta->total_success = 0;
1644 lq_sta->flush_timer = 0;
1647 * Else if we've used this modulation mode enough repetitions
1648 * (regardless of elapsed time or success/failure), reset
1649 * history bitmaps and rate-specific stats for all rates in
1653 lq_sta->table_count++;
1654 if (lq_sta->table_count >=
1655 lq_sta->table_count_limit) {
1656 lq_sta->table_count = 0;
1658 IWL_DEBUG_RATE(priv, "LQ: stay in table clear win\n");
1659 for (i = 0; i < IWL_RATE_COUNT; i++)
1660 rs_rate_scale_clear_window(
1665 /* If transitioning to allow "search", reset all history
1666 * bitmaps and stats in active table (this will become the new
1667 * "search" table). */
1668 if (!lq_sta->stay_in_tbl) {
1669 for (i = 0; i < IWL_RATE_COUNT; i++)
1670 rs_rate_scale_clear_window(&(tbl->win[i]));
1676 * Do rate scaling and search for new modulation mode.
1678 static void rs_rate_scale_perform(struct iwl_priv *priv,
1679 struct ieee80211_hdr *hdr,
1680 struct ieee80211_sta *sta,
1681 struct iwl_lq_sta *lq_sta)
1683 struct ieee80211_hw *hw = priv->hw;
1684 struct ieee80211_conf *conf = &hw->conf;
1685 int low = IWL_RATE_INVALID;
1686 int high = IWL_RATE_INVALID;
1689 struct iwl_rate_scale_data *window = NULL;
1690 int current_tpt = IWL_INVALID_VALUE;
1691 int low_tpt = IWL_INVALID_VALUE;
1692 int high_tpt = IWL_INVALID_VALUE;
1694 s8 scale_action = 0;
1697 struct iwl_scale_tbl_info *tbl, *tbl1;
1698 u16 rate_scale_index_msk = 0;
1705 u8 tid = MAX_TID_COUNT;
1706 struct iwl_tid_data *tid_data;
1708 IWL_DEBUG_RATE(priv, "rate scale calculate new rate for skb\n");
1710 /* Send management frames and broadcast/multicast data using
1712 /* TODO: this could probably be improved.. */
1713 if (!ieee80211_is_data(hdr->frame_control) ||
1714 is_multicast_ether_addr(hdr->addr1))
1717 if (!sta || !lq_sta)
1720 lq_sta->supp_rates = sta->supp_rates[lq_sta->band];
1722 tid = rs_tl_add_packet(lq_sta, hdr);
1725 * Select rate-scale / modulation-mode table to work with in
1726 * the rest of this function: "search" if searching for better
1727 * modulation mode, or "active" if doing rate scaling within a mode.
1729 if (!lq_sta->search_better_tbl)
1730 active_tbl = lq_sta->active_tbl;
1732 active_tbl = 1 - lq_sta->active_tbl;
1734 tbl = &(lq_sta->lq_info[active_tbl]);
1735 is_green = lq_sta->is_green;
1737 /* current tx rate */
1738 index = lq_sta->last_txrate_idx;
1740 IWL_DEBUG_RATE(priv, "Rate scale index %d for type %d\n", index,
1743 /* rates available for this association, and for modulation mode */
1744 rate_mask = rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
1746 IWL_DEBUG_RATE(priv, "mask 0x%04X \n", rate_mask);
1748 /* mask with station rate restriction */
1749 if (is_legacy(tbl->lq_type)) {
1750 if (lq_sta->band == IEEE80211_BAND_5GHZ)
1751 /* supp_rates has no CCK bits in A mode */
1752 rate_scale_index_msk = (u16) (rate_mask &
1753 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
1755 rate_scale_index_msk = (u16) (rate_mask &
1756 lq_sta->supp_rates);
1759 rate_scale_index_msk = rate_mask;
1761 if (!rate_scale_index_msk)
1762 rate_scale_index_msk = rate_mask;
1764 if (!((1 << index) & rate_scale_index_msk)) {
1765 IWL_ERR(priv, "Current Rate is not valid\n");
1769 /* Get expected throughput table and history window for current rate */
1770 if (!tbl->expected_tpt) {
1771 IWL_ERR(priv, "tbl->expected_tpt is NULL\n");
1775 /* force user max rate if set by user */
1776 if ((lq_sta->max_rate_idx != -1) &&
1777 (lq_sta->max_rate_idx < index)) {
1778 index = lq_sta->max_rate_idx;
1780 window = &(tbl->win[index]);
1784 window = &(tbl->win[index]);
1787 * If there is not enough history to calculate actual average
1788 * throughput, keep analyzing results of more tx frames, without
1789 * changing rate or mode (bypass most of the rest of this function).
1790 * Set up new rate table in uCode only if old rate is not supported
1791 * in current association (use new rate found above).
1793 fail_count = window->counter - window->success_counter;
1794 if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
1795 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
1796 IWL_DEBUG_RATE(priv, "LQ: still below TH. succ=%d total=%d "
1798 window->success_counter, window->counter, index);
1800 /* Can't calculate this yet; not enough history */
1801 window->average_tpt = IWL_INVALID_VALUE;
1803 /* Should we stay with this modulation mode,
1804 * or search for a new one? */
1805 rs_stay_in_table(lq_sta);
1810 /* Else we have enough samples; calculate estimate of
1811 * actual average throughput */
1813 BUG_ON(window->average_tpt != ((window->success_ratio *
1814 tbl->expected_tpt[index] + 64) / 128));
1816 /* If we are searching for better modulation mode, check success. */
1817 if (lq_sta->search_better_tbl) {
1819 /* If good success, continue using the "search" mode;
1820 * no need to send new link quality command, since we're
1821 * continuing to use the setup that we've been trying. */
1822 if (window->average_tpt > lq_sta->last_tpt) {
1824 IWL_DEBUG_RATE(priv, "LQ: SWITCHING TO NEW TABLE "
1825 "suc=%d cur-tpt=%d old-tpt=%d\n",
1826 window->success_ratio,
1827 window->average_tpt,
1830 if (!is_legacy(tbl->lq_type))
1831 lq_sta->enable_counter = 1;
1833 /* Swap tables; "search" becomes "active" */
1834 lq_sta->active_tbl = active_tbl;
1835 current_tpt = window->average_tpt;
1837 /* Else poor success; go back to mode in "active" table */
1840 IWL_DEBUG_RATE(priv, "LQ: GOING BACK TO THE OLD TABLE "
1841 "suc=%d cur-tpt=%d old-tpt=%d\n",
1842 window->success_ratio,
1843 window->average_tpt,
1846 /* Nullify "search" table */
1847 tbl->lq_type = LQ_NONE;
1849 /* Revert to "active" table */
1850 active_tbl = lq_sta->active_tbl;
1851 tbl = &(lq_sta->lq_info[active_tbl]);
1853 /* Revert to "active" rate and throughput info */
1854 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
1855 current_tpt = lq_sta->last_tpt;
1857 /* Need to set up a new rate table in uCode */
1861 /* Either way, we've made a decision; modulation mode
1862 * search is done, allow rate adjustment next time. */
1863 lq_sta->search_better_tbl = 0;
1864 done_search = 1; /* Don't switch modes below! */
1868 /* (Else) not in search of better modulation mode, try for better
1869 * starting rate, while staying in this mode. */
1870 high_low = rs_get_adjacent_rate(priv, index, rate_scale_index_msk,
1872 low = high_low & 0xff;
1873 high = (high_low >> 8) & 0xff;
1875 /* If user set max rate, dont allow higher than user constrain */
1876 if ((lq_sta->max_rate_idx != -1) &&
1877 (lq_sta->max_rate_idx < high))
1878 high = IWL_RATE_INVALID;
1880 sr = window->success_ratio;
1882 /* Collect measured throughputs for current and adjacent rates */
1883 current_tpt = window->average_tpt;
1884 if (low != IWL_RATE_INVALID)
1885 low_tpt = tbl->win[low].average_tpt;
1886 if (high != IWL_RATE_INVALID)
1887 high_tpt = tbl->win[high].average_tpt;
1891 /* Too many failures, decrease rate */
1892 if ((sr <= IWL_RATE_DECREASE_TH) || (current_tpt == 0)) {
1893 IWL_DEBUG_RATE(priv, "decrease rate because of low success_ratio\n");
1896 /* No throughput measured yet for adjacent rates; try increase. */
1897 } else if ((low_tpt == IWL_INVALID_VALUE) &&
1898 (high_tpt == IWL_INVALID_VALUE)) {
1900 if (high != IWL_RATE_INVALID && sr >= IWL_RATE_INCREASE_TH)
1902 else if (low != IWL_RATE_INVALID)
1906 /* Both adjacent throughputs are measured, but neither one has better
1907 * throughput; we're using the best rate, don't change it! */
1908 else if ((low_tpt != IWL_INVALID_VALUE) &&
1909 (high_tpt != IWL_INVALID_VALUE) &&
1910 (low_tpt < current_tpt) &&
1911 (high_tpt < current_tpt))
1914 /* At least one adjacent rate's throughput is measured,
1915 * and may have better performance. */
1917 /* Higher adjacent rate's throughput is measured */
1918 if (high_tpt != IWL_INVALID_VALUE) {
1919 /* Higher rate has better throughput */
1920 if (high_tpt > current_tpt &&
1921 sr >= IWL_RATE_INCREASE_TH) {
1927 /* Lower adjacent rate's throughput is measured */
1928 } else if (low_tpt != IWL_INVALID_VALUE) {
1929 /* Lower rate has better throughput */
1930 if (low_tpt > current_tpt) {
1931 IWL_DEBUG_RATE(priv,
1932 "decrease rate because of low tpt\n");
1934 } else if (sr >= IWL_RATE_INCREASE_TH) {
1940 /* Sanity check; asked for decrease, but success rate or throughput
1941 * has been good at old rate. Don't change it. */
1942 if ((scale_action == -1) && (low != IWL_RATE_INVALID) &&
1943 ((sr > IWL_RATE_HIGH_TH) ||
1944 (current_tpt > (100 * tbl->expected_tpt[low]))))
1947 switch (scale_action) {
1949 /* Decrease starting rate, update uCode's rate table */
1950 if (low != IWL_RATE_INVALID) {
1956 /* Increase starting rate, update uCode's rate table */
1957 if (high != IWL_RATE_INVALID) {
1969 IWL_DEBUG_RATE(priv, "choose rate scale index %d action %d low %d "
1970 "high %d type %d\n",
1971 index, scale_action, low, high, tbl->lq_type);
1974 /* Replace uCode's rate table for the destination station. */
1976 rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
1977 rs_fill_link_cmd(priv, lq_sta, rate);
1978 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
1981 /* Should we stay with this modulation mode, or search for a new one? */
1982 rs_stay_in_table(lq_sta);
1985 * Search for new modulation mode if we're:
1986 * 1) Not changing rates right now
1987 * 2) Not just finishing up a search
1988 * 3) Allowing a new search
1990 if (!update_lq && !done_search && !lq_sta->stay_in_tbl && window->counter) {
1991 /* Save current throughput to compare with "search" throughput*/
1992 lq_sta->last_tpt = current_tpt;
1994 /* Select a new "search" modulation mode to try.
1995 * If one is found, set up the new "search" table. */
1996 if (is_legacy(tbl->lq_type))
1997 rs_move_legacy_other(priv, lq_sta, conf, sta, index);
1998 else if (is_siso(tbl->lq_type))
1999 rs_move_siso_to_other(priv, lq_sta, conf, sta, index);
2001 rs_move_mimo_to_other(priv, lq_sta, conf, sta, index);
2003 /* If new "search" mode was selected, set up in uCode table */
2004 if (lq_sta->search_better_tbl) {
2005 /* Access the "search" table, clear its history. */
2006 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
2007 for (i = 0; i < IWL_RATE_COUNT; i++)
2008 rs_rate_scale_clear_window(&(tbl->win[i]));
2010 /* Use new "search" start rate */
2011 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
2013 IWL_DEBUG_RATE(priv, "Switch current mcs: %X index: %d\n",
2014 tbl->current_rate, index);
2015 rs_fill_link_cmd(priv, lq_sta, tbl->current_rate);
2016 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
2019 /* If the "active" (non-search) mode was legacy,
2020 * and we've tried switching antennas,
2021 * but we haven't been able to try HT modes (not available),
2022 * stay with best antenna legacy modulation for a while
2023 * before next round of mode comparisons. */
2024 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
2025 if (is_legacy(tbl1->lq_type) && !conf_is_ht(conf) &&
2026 lq_sta->action_counter >= 1) {
2027 lq_sta->action_counter = 0;
2028 IWL_DEBUG_RATE(priv, "LQ: STAY in legacy table\n");
2029 rs_set_stay_in_table(priv, 1, lq_sta);
2032 /* If we're in an HT mode, and all 3 mode switch actions
2033 * have been tried and compared, stay in this best modulation
2034 * mode for a while before next round of mode comparisons. */
2035 if (lq_sta->enable_counter &&
2036 (lq_sta->action_counter >= IWL_ACTION_LIMIT)) {
2037 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2038 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
2039 (tid != MAX_TID_COUNT)) {
2041 &priv->stations[lq_sta->lq.sta_id].tid[tid];
2042 if (tid_data->agg.state == IWL_AGG_OFF) {
2043 IWL_DEBUG_RATE(priv,
2044 "try to aggregate tid %d\n",
2046 rs_tl_turn_on_agg(priv, tid,
2050 lq_sta->action_counter = 0;
2051 rs_set_stay_in_table(priv, 0, lq_sta);
2055 * Else, don't search for a new modulation mode.
2056 * Put new timestamp in stay-in-modulation-mode flush timer if:
2057 * 1) Not changing rates right now
2058 * 2) Not just finishing up a search
2059 * 3) flush timer is empty
2062 if ((!update_lq) && (!done_search) && (!lq_sta->flush_timer))
2063 lq_sta->flush_timer = jiffies;
2067 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
2069 lq_sta->last_txrate_idx = i;
2075 static void rs_initialize_lq(struct iwl_priv *priv,
2076 struct ieee80211_conf *conf,
2077 struct ieee80211_sta *sta,
2078 struct iwl_lq_sta *lq_sta)
2080 struct iwl_scale_tbl_info *tbl;
2084 u8 use_green = rs_use_green(priv, conf);
2088 if (!sta || !lq_sta)
2091 i = lq_sta->last_txrate_idx;
2093 if ((lq_sta->lq.sta_id == 0xff) &&
2094 (priv->iw_mode == NL80211_IFTYPE_ADHOC))
2097 valid_tx_ant = priv->hw_params.valid_tx_ant;
2099 if (!lq_sta->search_better_tbl)
2100 active_tbl = lq_sta->active_tbl;
2102 active_tbl = 1 - lq_sta->active_tbl;
2104 tbl = &(lq_sta->lq_info[active_tbl]);
2106 if ((i < 0) || (i >= IWL_RATE_COUNT))
2109 rate = iwl_rates[i].plcp;
2110 tbl->ant_type = first_antenna(valid_tx_ant);
2111 rate |= tbl->ant_type << RATE_MCS_ANT_POS;
2113 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
2114 rate |= RATE_MCS_CCK_MSK;
2116 rs_get_tbl_info_from_mcs(rate, priv->band, tbl, &rate_idx);
2117 if (!rs_is_valid_ant(valid_tx_ant, tbl->ant_type))
2118 rs_toggle_antenna(valid_tx_ant, &rate, tbl);
2120 rate = rate_n_flags_from_tbl(priv, tbl, rate_idx, use_green);
2121 tbl->current_rate = rate;
2122 rs_set_expected_tpt_table(lq_sta, tbl);
2123 rs_fill_link_cmd(NULL, lq_sta, rate);
2124 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
2129 static void rs_get_rate(void *priv_r, struct ieee80211_sta *sta, void *priv_sta,
2130 struct ieee80211_tx_rate_control *txrc)
2133 struct sk_buff *skb = txrc->skb;
2134 struct ieee80211_supported_band *sband = txrc->sband;
2135 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
2136 struct ieee80211_conf *conf = &priv->hw->conf;
2137 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2138 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2139 struct iwl_lq_sta *lq_sta = priv_sta;
2143 IWL_DEBUG_RATE_LIMIT(priv, "rate scale calculate new rate for skb\n");
2145 /* Get max rate if user set max rate */
2147 lq_sta->max_rate_idx = txrc->max_rate_idx;
2148 if ((sband->band == IEEE80211_BAND_5GHZ) &&
2149 (lq_sta->max_rate_idx != -1))
2150 lq_sta->max_rate_idx += IWL_FIRST_OFDM_RATE;
2151 if ((lq_sta->max_rate_idx < 0) ||
2152 (lq_sta->max_rate_idx >= IWL_RATE_COUNT))
2153 lq_sta->max_rate_idx = -1;
2157 mask_bit = sta->supp_rates[sband->band];
2159 /* Send management frames and broadcast/multicast data using lowest
2161 if (!ieee80211_is_data(hdr->frame_control) ||
2162 is_multicast_ether_addr(hdr->addr1) || !sta || !lq_sta) {
2164 info->control.rates[0].idx =
2165 rate_lowest_index(sband, NULL);
2167 info->control.rates[0].idx =
2168 rate_lowest_index(sband, sta);
2172 rate_idx = lq_sta->last_txrate_idx;
2174 if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) &&
2175 !lq_sta->ibss_sta_added) {
2176 u8 sta_id = iwl_find_station(priv, hdr->addr1);
2178 if (sta_id == IWL_INVALID_STATION) {
2179 IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n",
2181 sta_id = iwl_add_station_flags(priv, hdr->addr1,
2182 0, CMD_ASYNC, NULL);
2184 if ((sta_id != IWL_INVALID_STATION)) {
2185 lq_sta->lq.sta_id = sta_id;
2186 lq_sta->lq.rs_table[0].rate_n_flags = 0;
2187 lq_sta->ibss_sta_added = 1;
2188 rs_initialize_lq(priv, conf, sta, lq_sta);
2192 if (rate_idx < 0 || rate_idx > IWL_RATE_COUNT)
2193 rate_idx = rate_lowest_index(sband, sta);
2194 else if (sband->band == IEEE80211_BAND_5GHZ)
2195 rate_idx -= IWL_FIRST_OFDM_RATE;
2197 info->control.rates[0].idx = rate_idx;
2200 static void *rs_alloc_sta(void *priv_rate, struct ieee80211_sta *sta,
2203 struct iwl_lq_sta *lq_sta;
2204 struct iwl_priv *priv;
2207 priv = (struct iwl_priv *)priv_rate;
2208 IWL_DEBUG_RATE(priv, "create station rate scale window\n");
2210 lq_sta = kzalloc(sizeof(struct iwl_lq_sta), gfp);
2214 lq_sta->lq.sta_id = 0xff;
2217 for (j = 0; j < LQ_SIZE; j++)
2218 for (i = 0; i < IWL_RATE_COUNT; i++)
2219 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
2224 static void rs_rate_init(void *priv_r, struct ieee80211_supported_band *sband,
2225 struct ieee80211_sta *sta, void *priv_sta)
2228 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
2229 struct ieee80211_conf *conf = &priv->hw->conf;
2230 struct iwl_lq_sta *lq_sta = priv_sta;
2235 lq_sta->flush_timer = 0;
2236 lq_sta->supp_rates = sta->supp_rates[sband->band];
2237 for (j = 0; j < LQ_SIZE; j++)
2238 for (i = 0; i < IWL_RATE_COUNT; i++)
2239 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
2241 IWL_DEBUG_RATE(priv, "LQ: *** rate scale station global init ***\n");
2242 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2243 * the lowest or the highest rate.. Could consider using RSSI from
2244 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2247 lq_sta->ibss_sta_added = 0;
2248 if (priv->iw_mode == NL80211_IFTYPE_AP) {
2249 u8 sta_id = iwl_find_station(priv, sta->addr);
2251 /* for IBSS the call are from tasklet */
2252 IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n", sta->addr);
2254 if (sta_id == IWL_INVALID_STATION) {
2255 IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n", sta->addr);
2256 sta_id = iwl_add_station_flags(priv, sta->addr,
2257 0, CMD_ASYNC, NULL);
2259 if ((sta_id != IWL_INVALID_STATION)) {
2260 lq_sta->lq.sta_id = sta_id;
2261 lq_sta->lq.rs_table[0].rate_n_flags = 0;
2263 /* FIXME: this is w/a remove it later */
2264 priv->assoc_station_added = 1;
2268 lq_sta->max_rate_idx = -1;
2269 lq_sta->missed_rate_counter = IWL_MISSED_RATE_MAX;
2270 lq_sta->is_green = rs_use_green(priv, conf);
2271 lq_sta->active_legacy_rate = priv->active_rate & ~(0x1000);
2272 lq_sta->active_rate_basic = priv->active_rate_basic;
2273 lq_sta->band = priv->band;
2275 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2276 * supp_rates[] does not; shift to convert format, force 9 MBits off.
2278 lq_sta->active_siso_rate = sta->ht_cap.mcs.rx_mask[0] << 1;
2279 lq_sta->active_siso_rate |= sta->ht_cap.mcs.rx_mask[0] & 0x1;
2280 lq_sta->active_siso_rate &= ~((u16)0x2);
2281 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
2284 lq_sta->active_mimo2_rate = sta->ht_cap.mcs.rx_mask[1] << 1;
2285 lq_sta->active_mimo2_rate |= sta->ht_cap.mcs.rx_mask[1] & 0x1;
2286 lq_sta->active_mimo2_rate &= ~((u16)0x2);
2287 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2289 lq_sta->active_mimo3_rate = sta->ht_cap.mcs.rx_mask[2] << 1;
2290 lq_sta->active_mimo3_rate |= sta->ht_cap.mcs.rx_mask[2] & 0x1;
2291 lq_sta->active_mimo3_rate &= ~((u16)0x2);
2292 lq_sta->active_mimo3_rate <<= IWL_FIRST_OFDM_RATE;
2294 IWL_DEBUG_RATE(priv, "SISO-RATE=%X MIMO2-RATE=%X MIMO3-RATE=%X\n",
2295 lq_sta->active_siso_rate,
2296 lq_sta->active_mimo2_rate,
2297 lq_sta->active_mimo3_rate);
2299 /* These values will be overridden later */
2300 lq_sta->lq.general_params.single_stream_ant_msk = ANT_A;
2301 lq_sta->lq.general_params.dual_stream_ant_msk = ANT_AB;
2303 /* as default allow aggregation for all tids */
2304 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
2307 /* Find highest tx rate supported by hardware and destination station */
2308 mask_bit = sta->supp_rates[sband->band];
2309 count = sband->n_bitrates;
2310 if (sband->band == IEEE80211_BAND_5GHZ) {
2311 count += IWL_FIRST_OFDM_RATE;
2312 start_rate = IWL_FIRST_OFDM_RATE;
2313 mask_bit <<= IWL_FIRST_OFDM_RATE;
2316 mask_bit = mask_bit & lq_sta->active_legacy_rate;
2317 lq_sta->last_txrate_idx = 4;
2318 for (i = start_rate; i < count; i++)
2319 if (mask_bit & BIT(i))
2320 lq_sta->last_txrate_idx = i;
2322 rs_initialize_lq(priv, conf, sta, lq_sta);
2325 static void rs_fill_link_cmd(const struct iwl_priv *priv,
2326 struct iwl_lq_sta *lq_sta, u32 new_rate)
2328 struct iwl_scale_tbl_info tbl_type;
2331 int repeat_rate = 0;
2332 u8 ant_toggle_cnt = 0;
2333 u8 use_ht_possible = 1;
2334 u8 valid_tx_ant = 0;
2335 struct iwl_link_quality_cmd *lq_cmd = &lq_sta->lq;
2337 /* Override starting rate (index 0) if needed for debug purposes */
2338 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
2340 /* Interpret new_rate (rate_n_flags) */
2341 memset(&tbl_type, 0, sizeof(tbl_type));
2342 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
2343 &tbl_type, &rate_idx);
2345 /* How many times should we repeat the initial rate? */
2346 if (is_legacy(tbl_type.lq_type)) {
2348 repeat_rate = IWL_NUMBER_TRY;
2350 repeat_rate = IWL_HT_NUMBER_TRY;
2353 lq_cmd->general_params.mimo_delimiter =
2354 is_mimo(tbl_type.lq_type) ? 1 : 0;
2356 /* Fill 1st table entry (index 0) */
2357 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
2359 if (num_of_ant(tbl_type.ant_type) == 1) {
2360 lq_cmd->general_params.single_stream_ant_msk =
2362 } else if (num_of_ant(tbl_type.ant_type) == 2) {
2363 lq_cmd->general_params.dual_stream_ant_msk =
2365 } /* otherwise we don't modify the existing value */
2371 valid_tx_ant = priv->hw_params.valid_tx_ant;
2373 /* Fill rest of rate table */
2374 while (index < LINK_QUAL_MAX_RETRY_NUM) {
2375 /* Repeat initial/next rate.
2376 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2377 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
2378 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
2379 if (is_legacy(tbl_type.lq_type)) {
2380 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2383 rs_toggle_antenna(valid_tx_ant,
2384 &new_rate, &tbl_type))
2388 /* Override next rate if needed for debug purposes */
2389 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
2391 /* Fill next table entry */
2392 lq_cmd->rs_table[index].rate_n_flags =
2393 cpu_to_le32(new_rate);
2398 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band, &tbl_type,
2401 /* Indicate to uCode which entries might be MIMO.
2402 * If initial rate was MIMO, this will finally end up
2403 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
2404 if (is_mimo(tbl_type.lq_type))
2405 lq_cmd->general_params.mimo_delimiter = index;
2408 new_rate = rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
2411 /* How many times should we repeat the next rate? */
2412 if (is_legacy(tbl_type.lq_type)) {
2413 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2416 rs_toggle_antenna(valid_tx_ant,
2417 &new_rate, &tbl_type))
2420 repeat_rate = IWL_NUMBER_TRY;
2422 repeat_rate = IWL_HT_NUMBER_TRY;
2425 /* Don't allow HT rates after next pass.
2426 * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
2427 use_ht_possible = 0;
2429 /* Override next rate if needed for debug purposes */
2430 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
2432 /* Fill next table entry */
2433 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
2439 lq_cmd->agg_params.agg_frame_cnt_limit = 64;
2440 lq_cmd->agg_params.agg_dis_start_th = 3;
2441 lq_cmd->agg_params.agg_time_limit = cpu_to_le16(4000);
2444 static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
2448 /* rate scale requires free function to be implemented */
2449 static void rs_free(void *priv_rate)
2454 static void rs_free_sta(void *priv_r, struct ieee80211_sta *sta,
2457 struct iwl_lq_sta *lq_sta = priv_sta;
2458 struct iwl_priv *priv __maybe_unused = priv_r;
2460 IWL_DEBUG_RATE(priv, "enter\n");
2462 IWL_DEBUG_RATE(priv, "leave\n");
2466 #ifdef CONFIG_MAC80211_DEBUGFS
2467 static int open_file_generic(struct inode *inode, struct file *file)
2469 file->private_data = inode->i_private;
2472 static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
2473 u32 *rate_n_flags, int index)
2475 struct iwl_priv *priv;
2480 valid_tx_ant = priv->hw_params.valid_tx_ant;
2481 if (lq_sta->dbg_fixed_rate) {
2483 ((lq_sta->dbg_fixed_rate & RATE_MCS_ANT_ABC_MSK)
2484 >> RATE_MCS_ANT_POS);
2485 if ((valid_tx_ant & ant_sel_tx) == ant_sel_tx) {
2486 *rate_n_flags = lq_sta->dbg_fixed_rate;
2487 IWL_DEBUG_RATE(priv, "Fixed rate ON\n");
2489 lq_sta->dbg_fixed_rate = 0;
2491 "Invalid antenna selection 0x%X, Valid is 0x%X\n",
2492 ant_sel_tx, valid_tx_ant);
2493 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
2496 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
2500 static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
2501 const char __user *user_buf, size_t count, loff_t *ppos)
2503 struct iwl_lq_sta *lq_sta = file->private_data;
2504 struct iwl_priv *priv;
2510 memset(buf, 0, sizeof(buf));
2511 buf_size = min(count, sizeof(buf) - 1);
2512 if (copy_from_user(buf, user_buf, buf_size))
2515 if (sscanf(buf, "%x", &parsed_rate) == 1)
2516 lq_sta->dbg_fixed_rate = parsed_rate;
2518 lq_sta->dbg_fixed_rate = 0;
2520 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
2521 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2522 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2523 lq_sta->active_mimo3_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2525 IWL_DEBUG_RATE(priv, "sta_id %d rate 0x%X\n",
2526 lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
2528 if (lq_sta->dbg_fixed_rate) {
2529 rs_fill_link_cmd(NULL, lq_sta, lq_sta->dbg_fixed_rate);
2530 iwl_send_lq_cmd(lq_sta->drv, &lq_sta->lq, CMD_ASYNC);
2536 static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
2537 char __user *user_buf, size_t count, loff_t *ppos)
2544 struct iwl_lq_sta *lq_sta = file->private_data;
2545 struct iwl_priv *priv;
2546 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
2549 buff = kmalloc(1024, GFP_KERNEL);
2553 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
2554 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
2555 lq_sta->total_failed, lq_sta->total_success,
2556 lq_sta->active_legacy_rate);
2557 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
2558 lq_sta->dbg_fixed_rate);
2559 desc += sprintf(buff+desc, "valid_tx_ant %s%s%s\n",
2560 (priv->hw_params.valid_tx_ant & ANT_A) ? "ANT_A," : "",
2561 (priv->hw_params.valid_tx_ant & ANT_B) ? "ANT_B," : "",
2562 (priv->hw_params.valid_tx_ant & ANT_C) ? "ANT_C" : "");
2563 desc += sprintf(buff+desc, "lq type %s\n",
2564 (is_legacy(tbl->lq_type)) ? "legacy" : "HT");
2565 if (is_Ht(tbl->lq_type)) {
2566 desc += sprintf(buff+desc, " %s",
2567 (is_siso(tbl->lq_type)) ? "SISO" :
2568 ((is_mimo2(tbl->lq_type)) ? "MIMO2" : "MIMO3"));
2569 desc += sprintf(buff+desc, " %s",
2570 (tbl->is_fat) ? "40MHz" : "20MHz");
2571 desc += sprintf(buff+desc, " %s\n", (tbl->is_SGI) ? "SGI" : "");
2573 desc += sprintf(buff+desc, "general:"
2574 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
2575 lq_sta->lq.general_params.flags,
2576 lq_sta->lq.general_params.mimo_delimiter,
2577 lq_sta->lq.general_params.single_stream_ant_msk,
2578 lq_sta->lq.general_params.dual_stream_ant_msk);
2580 desc += sprintf(buff+desc, "agg:"
2581 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
2582 le16_to_cpu(lq_sta->lq.agg_params.agg_time_limit),
2583 lq_sta->lq.agg_params.agg_dis_start_th,
2584 lq_sta->lq.agg_params.agg_frame_cnt_limit);
2586 desc += sprintf(buff+desc,
2587 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
2588 lq_sta->lq.general_params.start_rate_index[0],
2589 lq_sta->lq.general_params.start_rate_index[1],
2590 lq_sta->lq.general_params.start_rate_index[2],
2591 lq_sta->lq.general_params.start_rate_index[3]);
2594 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
2595 desc += sprintf(buff+desc, " rate[%d] 0x%X\n",
2596 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags));
2598 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2603 static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
2604 .write = rs_sta_dbgfs_scale_table_write,
2605 .read = rs_sta_dbgfs_scale_table_read,
2606 .open = open_file_generic,
2608 static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
2609 char __user *user_buf, size_t count, loff_t *ppos)
2616 struct iwl_lq_sta *lq_sta = file->private_data;
2618 buff = kmalloc(1024, GFP_KERNEL);
2622 for (i = 0; i < LQ_SIZE; i++) {
2623 desc += sprintf(buff+desc, "%s type=%d SGI=%d FAT=%d DUP=%d\n"
2625 lq_sta->active_tbl == i ? "*" : "x",
2626 lq_sta->lq_info[i].lq_type,
2627 lq_sta->lq_info[i].is_SGI,
2628 lq_sta->lq_info[i].is_fat,
2629 lq_sta->lq_info[i].is_dup,
2630 lq_sta->lq_info[i].current_rate);
2631 for (j = 0; j < IWL_RATE_COUNT; j++) {
2632 desc += sprintf(buff+desc,
2633 "counter=%d success=%d %%=%d\n",
2634 lq_sta->lq_info[i].win[j].counter,
2635 lq_sta->lq_info[i].win[j].success_counter,
2636 lq_sta->lq_info[i].win[j].success_ratio);
2639 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2644 static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
2645 .read = rs_sta_dbgfs_stats_table_read,
2646 .open = open_file_generic,
2649 static void rs_add_debugfs(void *priv, void *priv_sta,
2652 struct iwl_lq_sta *lq_sta = priv_sta;
2653 lq_sta->rs_sta_dbgfs_scale_table_file =
2654 debugfs_create_file("rate_scale_table", 0600, dir,
2655 lq_sta, &rs_sta_dbgfs_scale_table_ops);
2656 lq_sta->rs_sta_dbgfs_stats_table_file =
2657 debugfs_create_file("rate_stats_table", 0600, dir,
2658 lq_sta, &rs_sta_dbgfs_stats_table_ops);
2659 lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
2660 debugfs_create_u8("tx_agg_tid_enable", 0600, dir,
2661 &lq_sta->tx_agg_tid_en);
2665 static void rs_remove_debugfs(void *priv, void *priv_sta)
2667 struct iwl_lq_sta *lq_sta = priv_sta;
2668 debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
2669 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
2670 debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
2674 static struct rate_control_ops rs_ops = {
2677 .tx_status = rs_tx_status,
2678 .get_rate = rs_get_rate,
2679 .rate_init = rs_rate_init,
2682 .alloc_sta = rs_alloc_sta,
2683 .free_sta = rs_free_sta,
2684 #ifdef CONFIG_MAC80211_DEBUGFS
2685 .add_sta_debugfs = rs_add_debugfs,
2686 .remove_sta_debugfs = rs_remove_debugfs,
2690 int iwlagn_rate_control_register(void)
2692 return ieee80211_rate_control_register(&rs_ops);
2695 void iwlagn_rate_control_unregister(void)
2697 ieee80211_rate_control_unregister(&rs_ops);