1 /******************************************************************************
3 * Copyright(c) 2005 - 2010 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/slab.h>
30 #include <linux/wireless.h>
31 #include <net/mac80211.h>
33 #include <linux/netdevice.h>
34 #include <linux/etherdevice.h>
35 #include <linux/delay.h>
37 #include <linux/workqueue.h>
43 #define RS_NAME "iwl-agn-rs"
45 #define NUM_TRY_BEFORE_ANT_TOGGLE 1
46 #define IWL_NUMBER_TRY 1
47 #define IWL_HT_NUMBER_TRY 3
49 #define IWL_RATE_MAX_WINDOW 62 /* # tx in history window */
50 #define IWL_RATE_MIN_FAILURE_TH 6 /* min failures to calc tpt */
51 #define IWL_RATE_MIN_SUCCESS_TH 8 /* min successes to calc tpt */
53 /* max allowed rate miss before sync LQ cmd */
54 #define IWL_MISSED_RATE_MAX 15
55 /* max time to accum history 2 seconds */
56 #define IWL_RATE_SCALE_FLUSH_INTVL (3*HZ)
58 static u8 rs_ht_to_legacy[] = {
59 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
60 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
62 IWL_RATE_6M_INDEX, IWL_RATE_9M_INDEX,
63 IWL_RATE_12M_INDEX, IWL_RATE_18M_INDEX,
64 IWL_RATE_24M_INDEX, IWL_RATE_36M_INDEX,
65 IWL_RATE_48M_INDEX, IWL_RATE_54M_INDEX
68 static const u8 ant_toggle_lookup[] = {
69 /*ANT_NONE -> */ ANT_NONE,
72 /*ANT_AB -> */ ANT_BC,
74 /*ANT_AC -> */ ANT_AB,
75 /*ANT_BC -> */ ANT_AC,
76 /*ANT_ABC -> */ ANT_ABC,
79 static void rs_rate_scale_perform(struct iwl_priv *priv,
81 struct ieee80211_sta *sta,
82 struct iwl_lq_sta *lq_sta);
83 static void rs_fill_link_cmd(struct iwl_priv *priv,
84 struct iwl_lq_sta *lq_sta, u32 rate_n_flags);
87 #ifdef CONFIG_MAC80211_DEBUGFS
88 static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
89 u32 *rate_n_flags, int index);
91 static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
92 u32 *rate_n_flags, int index)
97 * The following tables contain the expected throughput metrics for all rates
99 * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
101 * where invalid entries are zeros.
103 * CCK rates are only valid in legacy table and will only be used in G
107 static s32 expected_tpt_legacy[IWL_RATE_COUNT] = {
108 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 0
111 static s32 expected_tpt_siso20MHz[4][IWL_RATE_COUNT] = {
112 {0, 0, 0, 0, 42, 0, 76, 102, 124, 158, 183, 193, 202}, /* Norm */
113 {0, 0, 0, 0, 46, 0, 82, 110, 132, 167, 192, 202, 210}, /* SGI */
114 {0, 0, 0, 0, 48, 0, 93, 135, 176, 251, 319, 351, 381}, /* AGG */
115 {0, 0, 0, 0, 53, 0, 102, 149, 193, 275, 348, 381, 413}, /* AGG+SGI */
118 static s32 expected_tpt_siso40MHz[4][IWL_RATE_COUNT] = {
119 {0, 0, 0, 0, 77, 0, 127, 160, 184, 220, 242, 250, 257}, /* Norm */
120 {0, 0, 0, 0, 83, 0, 135, 169, 193, 229, 250, 257, 264}, /* SGI */
121 {0, 0, 0, 0, 96, 0, 182, 259, 328, 451, 553, 598, 640}, /* AGG */
122 {0, 0, 0, 0, 106, 0, 199, 282, 357, 487, 593, 640, 683}, /* AGG+SGI */
125 static s32 expected_tpt_mimo2_20MHz[4][IWL_RATE_COUNT] = {
126 {0, 0, 0, 0, 74, 0, 123, 155, 179, 213, 235, 243, 250}, /* Norm */
127 {0, 0, 0, 0, 81, 0, 131, 164, 187, 221, 242, 250, 256}, /* SGI */
128 {0, 0, 0, 0, 92, 0, 175, 250, 317, 436, 534, 578, 619}, /* AGG */
129 {0, 0, 0, 0, 102, 0, 192, 273, 344, 470, 573, 619, 660}, /* AGG+SGI*/
132 static s32 expected_tpt_mimo2_40MHz[4][IWL_RATE_COUNT] = {
133 {0, 0, 0, 0, 123, 0, 182, 214, 235, 264, 279, 285, 289}, /* Norm */
134 {0, 0, 0, 0, 131, 0, 191, 222, 242, 270, 284, 289, 293}, /* SGI */
135 {0, 0, 0, 0, 180, 0, 327, 446, 545, 708, 828, 878, 922}, /* AGG */
136 {0, 0, 0, 0, 197, 0, 355, 481, 584, 752, 872, 922, 966}, /* AGG+SGI */
139 static s32 expected_tpt_mimo3_20MHz[4][IWL_RATE_COUNT] = {
140 {0, 0, 0, 0, 99, 0, 153, 186, 208, 239, 256, 263, 268}, /* Norm */
141 {0, 0, 0, 0, 106, 0, 162, 194, 215, 246, 262, 268, 273}, /* SGI */
142 {0, 0, 0, 0, 134, 0, 249, 346, 431, 574, 685, 732, 775}, /* AGG */
143 {0, 0, 0, 0, 148, 0, 272, 376, 465, 614, 727, 775, 818}, /* AGG+SGI */
146 static s32 expected_tpt_mimo3_40MHz[4][IWL_RATE_COUNT] = {
147 {0, 0, 0, 0, 152, 0, 211, 239, 255, 279, 290, 294, 297}, /* Norm */
148 {0, 0, 0, 0, 160, 0, 219, 245, 261, 284, 294, 297, 300}, /* SGI */
149 {0, 0, 0, 0, 254, 0, 443, 584, 695, 868, 984, 1030, 1070}, /* AGG */
150 {0, 0, 0, 0, 277, 0, 478, 624, 737, 911, 1026, 1070, 1109}, /* AGG+SGI */
154 static const struct iwl_rate_mcs_info iwl_rate_mcs[IWL_RATE_COUNT] = {
163 { "24", "16QAM 1/2"},
164 { "36", "16QAM 3/4"},
165 { "48", "64QAM 2/3"},
166 { "54", "64QAM 3/4"},
167 { "60", "64QAM 5/6"},
170 #define MCS_INDEX_PER_STREAM (8)
172 static inline u8 rs_extract_rate(u32 rate_n_flags)
174 return (u8)(rate_n_flags & 0xFF);
177 static void rs_rate_scale_clear_window(struct iwl_rate_scale_data *window)
180 window->success_counter = 0;
181 window->success_ratio = IWL_INVALID_VALUE;
183 window->average_tpt = IWL_INVALID_VALUE;
187 static inline u8 rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
189 return (ant_type & valid_antenna) == ant_type;
193 * removes the old data from the statistics. All data that is older than
194 * TID_MAX_TIME_DIFF, will be deleted.
196 static void rs_tl_rm_old_stats(struct iwl_traffic_load *tl, u32 curr_time)
198 /* The oldest age we want to keep */
199 u32 oldest_time = curr_time - TID_MAX_TIME_DIFF;
201 while (tl->queue_count &&
202 (tl->time_stamp < oldest_time)) {
203 tl->total -= tl->packet_count[tl->head];
204 tl->packet_count[tl->head] = 0;
205 tl->time_stamp += TID_QUEUE_CELL_SPACING;
208 if (tl->head >= TID_QUEUE_MAX_SIZE)
214 * increment traffic load value for tid and also remove
215 * any old values if passed the certain time period
217 static u8 rs_tl_add_packet(struct iwl_lq_sta *lq_data,
218 struct ieee80211_hdr *hdr)
220 u32 curr_time = jiffies_to_msecs(jiffies);
223 struct iwl_traffic_load *tl = NULL;
226 if (ieee80211_is_data_qos(hdr->frame_control)) {
227 u8 *qc = ieee80211_get_qos_ctl(hdr);
230 return MAX_TID_COUNT;
232 if (unlikely(tid >= TID_MAX_LOAD_COUNT))
233 return MAX_TID_COUNT;
235 tl = &lq_data->load[tid];
237 curr_time -= curr_time % TID_ROUND_VALUE;
239 /* Happens only for the first packet. Initialize the data */
240 if (!(tl->queue_count)) {
242 tl->time_stamp = curr_time;
245 tl->packet_count[0] = 1;
246 return MAX_TID_COUNT;
249 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
250 index = time_diff / TID_QUEUE_CELL_SPACING;
252 /* The history is too long: remove data that is older than */
253 /* TID_MAX_TIME_DIFF */
254 if (index >= TID_QUEUE_MAX_SIZE)
255 rs_tl_rm_old_stats(tl, curr_time);
257 index = (tl->head + index) % TID_QUEUE_MAX_SIZE;
258 tl->packet_count[index] = tl->packet_count[index] + 1;
259 tl->total = tl->total + 1;
261 if ((index + 1) > tl->queue_count)
262 tl->queue_count = index + 1;
268 get the traffic load value for tid
270 static u32 rs_tl_get_load(struct iwl_lq_sta *lq_data, u8 tid)
272 u32 curr_time = jiffies_to_msecs(jiffies);
275 struct iwl_traffic_load *tl = NULL;
277 if (tid >= TID_MAX_LOAD_COUNT)
280 tl = &(lq_data->load[tid]);
282 curr_time -= curr_time % TID_ROUND_VALUE;
284 if (!(tl->queue_count))
287 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
288 index = time_diff / TID_QUEUE_CELL_SPACING;
290 /* The history is too long: remove data that is older than */
291 /* TID_MAX_TIME_DIFF */
292 if (index >= TID_QUEUE_MAX_SIZE)
293 rs_tl_rm_old_stats(tl, curr_time);
298 static int rs_tl_turn_on_agg_for_tid(struct iwl_priv *priv,
299 struct iwl_lq_sta *lq_data, u8 tid,
300 struct ieee80211_sta *sta)
304 if (rs_tl_get_load(lq_data, tid) > IWL_AGG_LOAD_THRESHOLD) {
305 IWL_DEBUG_HT(priv, "Starting Tx agg: STA: %pM tid: %d\n",
307 ret = ieee80211_start_tx_ba_session(sta, tid);
308 if (ret == -EAGAIN) {
310 * driver and mac80211 is out of sync
311 * this might be cause by reloading firmware
312 * stop the tx ba session here
314 IWL_DEBUG_HT(priv, "Fail start Tx agg on tid: %d\n",
316 ieee80211_stop_tx_ba_session(sta, tid);
319 IWL_ERR(priv, "Fail finding valid aggregation tid: %d\n", tid);
323 static void rs_tl_turn_on_agg(struct iwl_priv *priv, u8 tid,
324 struct iwl_lq_sta *lq_data,
325 struct ieee80211_sta *sta)
327 if (tid < TID_MAX_LOAD_COUNT)
328 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
330 IWL_ERR(priv, "tid exceeds max load count: %d/%d\n",
331 tid, TID_MAX_LOAD_COUNT);
334 static inline int get_num_of_ant_from_rate(u32 rate_n_flags)
336 return !!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
337 !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
338 !!(rate_n_flags & RATE_MCS_ANT_C_MSK);
342 * Static function to get the expected throughput from an iwl_scale_tbl_info
343 * that wraps a NULL pointer check
345 static s32 get_expected_tpt(struct iwl_scale_tbl_info *tbl, int rs_index)
347 if (tbl->expected_tpt)
348 return tbl->expected_tpt[rs_index];
353 * rs_collect_tx_data - Update the success/failure sliding window
355 * We keep a sliding window of the last 62 packets transmitted
356 * at this rate. window->data contains the bitmask of successful
359 static int rs_collect_tx_data(struct iwl_scale_tbl_info *tbl,
360 int scale_index, int attempts, int successes)
362 struct iwl_rate_scale_data *window = NULL;
363 static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
366 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
369 /* Select window for current tx bit rate */
370 window = &(tbl->win[scale_index]);
372 /* Get expected throughput */
373 tpt = get_expected_tpt(tbl, scale_index);
376 * Keep track of only the latest 62 tx frame attempts in this rate's
377 * history window; anything older isn't really relevant any more.
378 * If we have filled up the sliding window, drop the oldest attempt;
379 * if the oldest attempt (highest bit in bitmap) shows "success",
380 * subtract "1" from the success counter (this is the main reason
381 * we keep these bitmaps!).
383 while (attempts > 0) {
384 if (window->counter >= IWL_RATE_MAX_WINDOW) {
386 /* remove earliest */
387 window->counter = IWL_RATE_MAX_WINDOW - 1;
389 if (window->data & mask) {
390 window->data &= ~mask;
391 window->success_counter--;
395 /* Increment frames-attempted counter */
398 /* Shift bitmap by one frame to throw away oldest history */
401 /* Mark the most recent #successes attempts as successful */
403 window->success_counter++;
411 /* Calculate current success ratio, avoid divide-by-0! */
412 if (window->counter > 0)
413 window->success_ratio = 128 * (100 * window->success_counter)
416 window->success_ratio = IWL_INVALID_VALUE;
418 fail_count = window->counter - window->success_counter;
420 /* Calculate average throughput, if we have enough history. */
421 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
422 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
423 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
425 window->average_tpt = IWL_INVALID_VALUE;
427 /* Tag this window as having been updated */
428 window->stamp = jiffies;
434 * Fill uCode API rate_n_flags field, based on "search" or "active" table.
436 /* FIXME:RS:remove this function and put the flags statically in the table */
437 static u32 rate_n_flags_from_tbl(struct iwl_priv *priv,
438 struct iwl_scale_tbl_info *tbl,
439 int index, u8 use_green)
441 u32 rate_n_flags = 0;
443 if (is_legacy(tbl->lq_type)) {
444 rate_n_flags = iwl_rates[index].plcp;
445 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
446 rate_n_flags |= RATE_MCS_CCK_MSK;
448 } else if (is_Ht(tbl->lq_type)) {
449 if (index > IWL_LAST_OFDM_RATE) {
450 IWL_ERR(priv, "Invalid HT rate index %d\n", index);
451 index = IWL_LAST_OFDM_RATE;
453 rate_n_flags = RATE_MCS_HT_MSK;
455 if (is_siso(tbl->lq_type))
456 rate_n_flags |= iwl_rates[index].plcp_siso;
457 else if (is_mimo2(tbl->lq_type))
458 rate_n_flags |= iwl_rates[index].plcp_mimo2;
460 rate_n_flags |= iwl_rates[index].plcp_mimo3;
462 IWL_ERR(priv, "Invalid tbl->lq_type %d\n", tbl->lq_type);
465 rate_n_flags |= ((tbl->ant_type << RATE_MCS_ANT_POS) &
466 RATE_MCS_ANT_ABC_MSK);
468 if (is_Ht(tbl->lq_type)) {
471 rate_n_flags |= RATE_MCS_DUP_MSK;
473 rate_n_flags |= RATE_MCS_HT40_MSK;
476 rate_n_flags |= RATE_MCS_SGI_MSK;
479 rate_n_flags |= RATE_MCS_GF_MSK;
480 if (is_siso(tbl->lq_type) && tbl->is_SGI) {
481 rate_n_flags &= ~RATE_MCS_SGI_MSK;
482 IWL_ERR(priv, "GF was set with SGI:SISO\n");
490 * Interpret uCode API's rate_n_flags format,
491 * fill "search" or "active" tx mode table.
493 static int rs_get_tbl_info_from_mcs(const u32 rate_n_flags,
494 enum ieee80211_band band,
495 struct iwl_scale_tbl_info *tbl,
498 u32 ant_msk = (rate_n_flags & RATE_MCS_ANT_ABC_MSK);
499 u8 num_of_ant = get_num_of_ant_from_rate(rate_n_flags);
502 *rate_idx = iwl_hwrate_to_plcp_idx(rate_n_flags);
504 if (*rate_idx == IWL_RATE_INVALID) {
508 tbl->is_SGI = 0; /* default legacy setup */
511 tbl->ant_type = (ant_msk >> RATE_MCS_ANT_POS);
512 tbl->lq_type = LQ_NONE;
513 tbl->max_search = IWL_MAX_SEARCH;
515 /* legacy rate format */
516 if (!(rate_n_flags & RATE_MCS_HT_MSK)) {
517 if (num_of_ant == 1) {
518 if (band == IEEE80211_BAND_5GHZ)
525 if (rate_n_flags & RATE_MCS_SGI_MSK)
528 if ((rate_n_flags & RATE_MCS_HT40_MSK) ||
529 (rate_n_flags & RATE_MCS_DUP_MSK))
532 if (rate_n_flags & RATE_MCS_DUP_MSK)
535 mcs = rs_extract_rate(rate_n_flags);
538 if (mcs <= IWL_RATE_SISO_60M_PLCP) {
540 tbl->lq_type = LQ_SISO; /*else NONE*/
542 } else if (mcs <= IWL_RATE_MIMO2_60M_PLCP) {
544 tbl->lq_type = LQ_MIMO2;
547 if (num_of_ant == 3) {
548 tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
549 tbl->lq_type = LQ_MIMO3;
556 /* switch to another antenna/antennas and return 1 */
557 /* if no other valid antenna found, return 0 */
558 static int rs_toggle_antenna(u32 valid_ant, u32 *rate_n_flags,
559 struct iwl_scale_tbl_info *tbl)
563 if (!tbl->ant_type || tbl->ant_type > ANT_ABC)
566 if (!rs_is_valid_ant(valid_ant, tbl->ant_type))
569 new_ant_type = ant_toggle_lookup[tbl->ant_type];
571 while ((new_ant_type != tbl->ant_type) &&
572 !rs_is_valid_ant(valid_ant, new_ant_type))
573 new_ant_type = ant_toggle_lookup[new_ant_type];
575 if (new_ant_type == tbl->ant_type)
578 tbl->ant_type = new_ant_type;
579 *rate_n_flags &= ~RATE_MCS_ANT_ABC_MSK;
580 *rate_n_flags |= new_ant_type << RATE_MCS_ANT_POS;
585 * Green-field mode is valid if the station supports it and
586 * there are no non-GF stations present in the BSS.
588 static inline u8 rs_use_green(struct ieee80211_sta *sta,
589 struct iwl_ht_config *ht_conf)
591 return (sta->ht_cap.cap & IEEE80211_HT_CAP_GRN_FLD) &&
592 !(ht_conf->non_GF_STA_present);
596 * rs_get_supported_rates - get the available rates
598 * if management frame or broadcast frame only return
599 * basic available rates.
602 static u16 rs_get_supported_rates(struct iwl_lq_sta *lq_sta,
603 struct ieee80211_hdr *hdr,
604 enum iwl_table_type rate_type)
606 if (is_legacy(rate_type)) {
607 return lq_sta->active_legacy_rate;
609 if (is_siso(rate_type))
610 return lq_sta->active_siso_rate;
611 else if (is_mimo2(rate_type))
612 return lq_sta->active_mimo2_rate;
614 return lq_sta->active_mimo3_rate;
618 static u16 rs_get_adjacent_rate(struct iwl_priv *priv, u8 index, u16 rate_mask,
621 u8 high = IWL_RATE_INVALID;
622 u8 low = IWL_RATE_INVALID;
624 /* 802.11A or ht walks to the next literal adjacent rate in
626 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
630 /* Find the previous rate that is in the rate mask */
632 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
633 if (rate_mask & mask) {
639 /* Find the next rate that is in the rate mask */
641 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
642 if (rate_mask & mask) {
648 return (high << 8) | low;
652 while (low != IWL_RATE_INVALID) {
653 low = iwl_rates[low].prev_rs;
654 if (low == IWL_RATE_INVALID)
656 if (rate_mask & (1 << low))
658 IWL_DEBUG_RATE(priv, "Skipping masked lower rate: %d\n", low);
662 while (high != IWL_RATE_INVALID) {
663 high = iwl_rates[high].next_rs;
664 if (high == IWL_RATE_INVALID)
666 if (rate_mask & (1 << high))
668 IWL_DEBUG_RATE(priv, "Skipping masked higher rate: %d\n", high);
671 return (high << 8) | low;
674 static u32 rs_get_lower_rate(struct iwl_lq_sta *lq_sta,
675 struct iwl_scale_tbl_info *tbl,
676 u8 scale_index, u8 ht_possible)
681 u8 switch_to_legacy = 0;
682 u8 is_green = lq_sta->is_green;
683 struct iwl_priv *priv = lq_sta->drv;
685 /* check if we need to switch from HT to legacy rates.
686 * assumption is that mandatory rates (1Mbps or 6Mbps)
687 * are always supported (spec demand) */
688 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
689 switch_to_legacy = 1;
690 scale_index = rs_ht_to_legacy[scale_index];
691 if (lq_sta->band == IEEE80211_BAND_5GHZ)
696 if (num_of_ant(tbl->ant_type) > 1)
698 first_antenna(priv->hw_params.valid_tx_ant);
702 tbl->max_search = IWL_MAX_SEARCH;
705 rate_mask = rs_get_supported_rates(lq_sta, NULL, tbl->lq_type);
707 /* Mask with station rate restriction */
708 if (is_legacy(tbl->lq_type)) {
709 /* supp_rates has no CCK bits in A mode */
710 if (lq_sta->band == IEEE80211_BAND_5GHZ)
711 rate_mask = (u16)(rate_mask &
712 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
714 rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
717 /* If we switched from HT to legacy, check current rate */
718 if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
723 high_low = rs_get_adjacent_rate(lq_sta->drv, scale_index, rate_mask,
725 low = high_low & 0xff;
727 if (low == IWL_RATE_INVALID)
731 return rate_n_flags_from_tbl(lq_sta->drv, tbl, low, is_green);
735 * Simple function to compare two rate scale table types
737 static bool table_type_matches(struct iwl_scale_tbl_info *a,
738 struct iwl_scale_tbl_info *b)
740 return (a->lq_type == b->lq_type) && (a->ant_type == b->ant_type) &&
741 (a->is_SGI == b->is_SGI);
745 * mac80211 sends us Tx status
747 static void rs_tx_status(void *priv_r, struct ieee80211_supported_band *sband,
748 struct ieee80211_sta *sta, void *priv_sta,
753 int rs_index, mac_index, i;
754 struct iwl_lq_sta *lq_sta = priv_sta;
755 struct iwl_link_quality_cmd *table;
756 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
757 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
758 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
759 enum mac80211_rate_control_flags mac_flags;
761 struct iwl_scale_tbl_info tbl_type;
762 struct iwl_scale_tbl_info *curr_tbl, *other_tbl, *tmp_tbl;
764 IWL_DEBUG_RATE_LIMIT(priv, "get frame ack response, update rate scale window\n");
766 /* Treat uninitialized rate scaling data same as non-existing. */
768 IWL_DEBUG_RATE(priv, "Station rate scaling not created yet.\n");
770 } else if (!lq_sta->drv) {
771 IWL_DEBUG_RATE(priv, "Rate scaling not initialized yet.\n");
775 if (!ieee80211_is_data(hdr->frame_control) ||
776 info->flags & IEEE80211_TX_CTL_NO_ACK)
779 /* This packet was aggregated but doesn't carry status info */
780 if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
781 !(info->flags & IEEE80211_TX_STAT_AMPDU))
785 * Ignore this Tx frame response if its initial rate doesn't match
786 * that of latest Link Quality command. There may be stragglers
787 * from a previous Link Quality command, but we're no longer interested
788 * in those; they're either from the "active" mode while we're trying
789 * to check "search" mode, or a prior "search" mode after we've moved
790 * to a new "search" mode (which might become the new "active" mode).
793 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
794 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
795 if (priv->band == IEEE80211_BAND_5GHZ)
796 rs_index -= IWL_FIRST_OFDM_RATE;
797 mac_flags = info->status.rates[0].flags;
798 mac_index = info->status.rates[0].idx;
799 /* For HT packets, map MCS to PLCP */
800 if (mac_flags & IEEE80211_TX_RC_MCS) {
801 mac_index &= RATE_MCS_CODE_MSK; /* Remove # of streams */
802 if (mac_index >= (IWL_RATE_9M_INDEX - IWL_FIRST_OFDM_RATE))
805 * mac80211 HT index is always zero-indexed; we need to move
806 * HT OFDM rates after CCK rates in 2.4 GHz band
808 if (priv->band == IEEE80211_BAND_2GHZ)
809 mac_index += IWL_FIRST_OFDM_RATE;
811 /* Here we actually compare this rate to the latest LQ command */
812 if ((mac_index < 0) ||
813 (tbl_type.is_SGI != !!(mac_flags & IEEE80211_TX_RC_SHORT_GI)) ||
814 (tbl_type.is_ht40 != !!(mac_flags & IEEE80211_TX_RC_40_MHZ_WIDTH)) ||
815 (tbl_type.is_dup != !!(mac_flags & IEEE80211_TX_RC_DUP_DATA)) ||
816 (tbl_type.ant_type != info->antenna_sel_tx) ||
817 (!!(tx_rate & RATE_MCS_HT_MSK) != !!(mac_flags & IEEE80211_TX_RC_MCS)) ||
818 (!!(tx_rate & RATE_MCS_GF_MSK) != !!(mac_flags & IEEE80211_TX_RC_GREEN_FIELD)) ||
819 (rs_index != mac_index)) {
820 IWL_DEBUG_RATE(priv, "initial rate %d does not match %d (0x%x)\n", mac_index, rs_index, tx_rate);
822 * Since rates mis-match, the last LQ command may have failed.
823 * After IWL_MISSED_RATE_MAX mis-matches, resync the uCode with
826 lq_sta->missed_rate_counter++;
827 if (lq_sta->missed_rate_counter > IWL_MISSED_RATE_MAX) {
828 lq_sta->missed_rate_counter = 0;
829 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC, false);
831 /* Regardless, ignore this status info for outdated rate */
834 /* Rate did match, so reset the missed_rate_counter */
835 lq_sta->missed_rate_counter = 0;
837 /* Figure out if rate scale algorithm is in active or search table */
838 if (table_type_matches(&tbl_type,
839 &(lq_sta->lq_info[lq_sta->active_tbl]))) {
840 curr_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
841 other_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
842 } else if (table_type_matches(&tbl_type,
843 &lq_sta->lq_info[1 - lq_sta->active_tbl])) {
844 curr_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
845 other_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
847 IWL_DEBUG_RATE(priv, "Neither active nor search matches tx rate\n");
852 * Updating the frame history depends on whether packets were
855 * For aggregation, all packets were transmitted at the same rate, the
856 * first index into rate scale table.
858 if (info->flags & IEEE80211_TX_STAT_AMPDU) {
859 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
860 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type,
862 rs_collect_tx_data(curr_tbl, rs_index,
863 info->status.ampdu_len,
864 info->status.ampdu_ack_len);
866 /* Update success/fail counts if not searching for new mode */
867 if (lq_sta->stay_in_tbl) {
868 lq_sta->total_success += info->status.ampdu_ack_len;
869 lq_sta->total_failed += (info->status.ampdu_len -
870 info->status.ampdu_ack_len);
874 * For legacy, update frame history with for each Tx retry.
876 retries = info->status.rates[0].count - 1;
877 /* HW doesn't send more than 15 retries */
878 retries = min(retries, 15);
880 /* The last transmission may have been successful */
881 legacy_success = !!(info->flags & IEEE80211_TX_STAT_ACK);
882 /* Collect data for each rate used during failed TX attempts */
883 for (i = 0; i <= retries; ++i) {
884 tx_rate = le32_to_cpu(table->rs_table[i].rate_n_flags);
885 rs_get_tbl_info_from_mcs(tx_rate, priv->band,
886 &tbl_type, &rs_index);
888 * Only collect stats if retried rate is in the same RS
889 * table as active/search.
891 if (table_type_matches(&tbl_type, curr_tbl))
893 else if (table_type_matches(&tbl_type, other_tbl))
897 rs_collect_tx_data(tmp_tbl, rs_index, 1,
898 i < retries ? 0 : legacy_success);
901 /* Update success/fail counts if not searching for new mode */
902 if (lq_sta->stay_in_tbl) {
903 lq_sta->total_success += legacy_success;
904 lq_sta->total_failed += retries + (1 - legacy_success);
907 /* The last TX rate is cached in lq_sta; it's set in if/else above */
908 lq_sta->last_rate_n_flags = tx_rate;
910 /* See if there's a better rate or modulation mode to try. */
911 if (sta && sta->supp_rates[sband->band])
912 rs_rate_scale_perform(priv, skb, sta, lq_sta);
916 * Begin a period of staying with a selected modulation mode.
917 * Set "stay_in_tbl" flag to prevent any mode switches.
918 * Set frame tx success limits according to legacy vs. high-throughput,
919 * and reset overall (spanning all rates) tx success history statistics.
920 * These control how long we stay using same modulation mode before
921 * searching for a new mode.
923 static void rs_set_stay_in_table(struct iwl_priv *priv, u8 is_legacy,
924 struct iwl_lq_sta *lq_sta)
926 IWL_DEBUG_RATE(priv, "we are staying in the same table\n");
927 lq_sta->stay_in_tbl = 1; /* only place this gets set */
929 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
930 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
931 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
933 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
934 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
935 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
937 lq_sta->table_count = 0;
938 lq_sta->total_failed = 0;
939 lq_sta->total_success = 0;
940 lq_sta->flush_timer = jiffies;
941 lq_sta->action_counter = 0;
945 * Find correct throughput table for given mode of modulation
947 static void rs_set_expected_tpt_table(struct iwl_lq_sta *lq_sta,
948 struct iwl_scale_tbl_info *tbl)
950 /* Used to choose among HT tables */
951 s32 (*ht_tbl_pointer)[IWL_RATE_COUNT];
953 /* Check for invalid LQ type */
954 if (WARN_ON_ONCE(!is_legacy(tbl->lq_type) && !is_Ht(tbl->lq_type))) {
955 tbl->expected_tpt = expected_tpt_legacy;
959 /* Legacy rates have only one table */
960 if (is_legacy(tbl->lq_type)) {
961 tbl->expected_tpt = expected_tpt_legacy;
965 /* Choose among many HT tables depending on number of streams
966 * (SISO/MIMO2/MIMO3), channel width (20/40), SGI, and aggregation
968 if (is_siso(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
969 ht_tbl_pointer = expected_tpt_siso20MHz;
970 else if (is_siso(tbl->lq_type))
971 ht_tbl_pointer = expected_tpt_siso40MHz;
972 else if (is_mimo2(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
973 ht_tbl_pointer = expected_tpt_mimo2_20MHz;
974 else if (is_mimo2(tbl->lq_type))
975 ht_tbl_pointer = expected_tpt_mimo2_40MHz;
976 else if (is_mimo3(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
977 ht_tbl_pointer = expected_tpt_mimo3_20MHz;
978 else /* if (is_mimo3(tbl->lq_type)) <-- must be true */
979 ht_tbl_pointer = expected_tpt_mimo3_40MHz;
981 if (!tbl->is_SGI && !lq_sta->is_agg) /* Normal */
982 tbl->expected_tpt = ht_tbl_pointer[0];
983 else if (tbl->is_SGI && !lq_sta->is_agg) /* SGI */
984 tbl->expected_tpt = ht_tbl_pointer[1];
985 else if (!tbl->is_SGI && lq_sta->is_agg) /* AGG */
986 tbl->expected_tpt = ht_tbl_pointer[2];
988 tbl->expected_tpt = ht_tbl_pointer[3];
992 * Find starting rate for new "search" high-throughput mode of modulation.
993 * Goal is to find lowest expected rate (under perfect conditions) that is
994 * above the current measured throughput of "active" mode, to give new mode
995 * a fair chance to prove itself without too many challenges.
997 * This gets called when transitioning to more aggressive modulation
998 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
999 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1000 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1001 * bit rate will typically need to increase, but not if performance was bad.
1003 static s32 rs_get_best_rate(struct iwl_priv *priv,
1004 struct iwl_lq_sta *lq_sta,
1005 struct iwl_scale_tbl_info *tbl, /* "search" */
1006 u16 rate_mask, s8 index)
1008 /* "active" values */
1009 struct iwl_scale_tbl_info *active_tbl =
1010 &(lq_sta->lq_info[lq_sta->active_tbl]);
1011 s32 active_sr = active_tbl->win[index].success_ratio;
1012 s32 active_tpt = active_tbl->expected_tpt[index];
1014 /* expected "search" throughput */
1015 s32 *tpt_tbl = tbl->expected_tpt;
1017 s32 new_rate, high, low, start_hi;
1021 new_rate = high = low = start_hi = IWL_RATE_INVALID;
1024 high_low = rs_get_adjacent_rate(priv, rate, rate_mask,
1027 low = high_low & 0xff;
1028 high = (high_low >> 8) & 0xff;
1031 * Lower the "search" bit rate, to give new "search" mode
1032 * approximately the same throughput as "active" if:
1034 * 1) "Active" mode has been working modestly well (but not
1035 * great), and expected "search" throughput (under perfect
1036 * conditions) at candidate rate is above the actual
1037 * measured "active" throughput (but less than expected
1038 * "active" throughput under perfect conditions).
1040 * 2) "Active" mode has been working perfectly or very well
1041 * and expected "search" throughput (under perfect
1042 * conditions) at candidate rate is above expected
1043 * "active" throughput (under perfect conditions).
1045 if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
1046 ((active_sr > IWL_RATE_DECREASE_TH) &&
1047 (active_sr <= IWL_RATE_HIGH_TH) &&
1048 (tpt_tbl[rate] <= active_tpt))) ||
1049 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
1050 (tpt_tbl[rate] > active_tpt))) {
1052 /* (2nd or later pass)
1053 * If we've already tried to raise the rate, and are
1054 * now trying to lower it, use the higher rate. */
1055 if (start_hi != IWL_RATE_INVALID) {
1056 new_rate = start_hi;
1062 /* Loop again with lower rate */
1063 if (low != IWL_RATE_INVALID)
1066 /* Lower rate not available, use the original */
1070 /* Else try to raise the "search" rate to match "active" */
1072 /* (2nd or later pass)
1073 * If we've already tried to lower the rate, and are
1074 * now trying to raise it, use the lower rate. */
1075 if (new_rate != IWL_RATE_INVALID)
1078 /* Loop again with higher rate */
1079 else if (high != IWL_RATE_INVALID) {
1083 /* Higher rate not available, use the original */
1095 * Set up search table for MIMO2
1097 static int rs_switch_to_mimo2(struct iwl_priv *priv,
1098 struct iwl_lq_sta *lq_sta,
1099 struct ieee80211_conf *conf,
1100 struct ieee80211_sta *sta,
1101 struct iwl_scale_tbl_info *tbl, int index)
1105 s8 is_green = lq_sta->is_green;
1107 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1110 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
1111 == WLAN_HT_CAP_SM_PS_STATIC)
1114 /* Need both Tx chains/antennas to support MIMO */
1115 if (priv->hw_params.tx_chains_num < 2)
1118 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO2\n");
1120 tbl->lq_type = LQ_MIMO2;
1121 tbl->is_dup = lq_sta->is_dup;
1123 tbl->max_search = IWL_MAX_SEARCH;
1124 rate_mask = lq_sta->active_mimo2_rate;
1126 if (iwl_is_ht40_tx_allowed(priv, &sta->ht_cap))
1131 rs_set_expected_tpt_table(lq_sta, tbl);
1133 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1135 IWL_DEBUG_RATE(priv, "LQ: MIMO2 best rate %d mask %X\n", rate, rate_mask);
1136 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1137 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
1141 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
1143 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
1144 tbl->current_rate, is_green);
1149 * Set up search table for MIMO3
1151 static int rs_switch_to_mimo3(struct iwl_priv *priv,
1152 struct iwl_lq_sta *lq_sta,
1153 struct ieee80211_conf *conf,
1154 struct ieee80211_sta *sta,
1155 struct iwl_scale_tbl_info *tbl, int index)
1159 s8 is_green = lq_sta->is_green;
1161 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1164 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
1165 == WLAN_HT_CAP_SM_PS_STATIC)
1168 /* Need both Tx chains/antennas to support MIMO */
1169 if (priv->hw_params.tx_chains_num < 3)
1172 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO3\n");
1174 tbl->lq_type = LQ_MIMO3;
1175 tbl->is_dup = lq_sta->is_dup;
1177 tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
1178 rate_mask = lq_sta->active_mimo3_rate;
1180 if (iwl_is_ht40_tx_allowed(priv, &sta->ht_cap))
1185 rs_set_expected_tpt_table(lq_sta, tbl);
1187 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1189 IWL_DEBUG_RATE(priv, "LQ: MIMO3 best rate %d mask %X\n",
1191 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1192 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
1196 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
1198 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
1199 tbl->current_rate, is_green);
1204 * Set up search table for SISO
1206 static int rs_switch_to_siso(struct iwl_priv *priv,
1207 struct iwl_lq_sta *lq_sta,
1208 struct ieee80211_conf *conf,
1209 struct ieee80211_sta *sta,
1210 struct iwl_scale_tbl_info *tbl, int index)
1213 u8 is_green = lq_sta->is_green;
1216 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1219 IWL_DEBUG_RATE(priv, "LQ: try to switch to SISO\n");
1221 tbl->is_dup = lq_sta->is_dup;
1222 tbl->lq_type = LQ_SISO;
1224 tbl->max_search = IWL_MAX_SEARCH;
1225 rate_mask = lq_sta->active_siso_rate;
1227 if (iwl_is_ht40_tx_allowed(priv, &sta->ht_cap))
1233 tbl->is_SGI = 0; /*11n spec: no SGI in SISO+Greenfield*/
1235 rs_set_expected_tpt_table(lq_sta, tbl);
1236 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1238 IWL_DEBUG_RATE(priv, "LQ: get best rate %d mask %X\n", rate, rate_mask);
1239 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1240 IWL_DEBUG_RATE(priv, "can not switch with index %d rate mask %x\n",
1244 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
1245 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
1246 tbl->current_rate, is_green);
1251 * Try to switch to new modulation mode from legacy
1253 static int rs_move_legacy_other(struct iwl_priv *priv,
1254 struct iwl_lq_sta *lq_sta,
1255 struct ieee80211_conf *conf,
1256 struct ieee80211_sta *sta,
1259 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1260 struct iwl_scale_tbl_info *search_tbl =
1261 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1262 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1263 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1264 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1265 u8 start_action = tbl->action;
1266 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1267 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1269 u8 update_search_tbl_counter = 0;
1271 if (!iwl_ht_enabled(priv))
1272 /* stay in Legacy */
1273 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
1274 else if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE &&
1275 tbl->action > IWL_LEGACY_SWITCH_SISO)
1276 tbl->action = IWL_LEGACY_SWITCH_SISO;
1278 lq_sta->action_counter++;
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 if ((tbl->action == IWL_LEGACY_SWITCH_ANTENNA1 &&
1285 tx_chains_num <= 1) ||
1286 (tbl->action == IWL_LEGACY_SWITCH_ANTENNA2 &&
1287 tx_chains_num <= 2))
1290 /* Don't change antenna if success has been great */
1291 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1294 /* Set up search table to try other antenna */
1295 memcpy(search_tbl, tbl, sz);
1297 if (rs_toggle_antenna(valid_tx_ant,
1298 &search_tbl->current_rate, search_tbl)) {
1299 update_search_tbl_counter = 1;
1300 rs_set_expected_tpt_table(lq_sta, search_tbl);
1304 case IWL_LEGACY_SWITCH_SISO:
1305 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to SISO\n");
1307 /* Set up search table to try SISO */
1308 memcpy(search_tbl, tbl, sz);
1309 search_tbl->is_SGI = 0;
1310 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
1313 lq_sta->action_counter = 0;
1318 case IWL_LEGACY_SWITCH_MIMO2_AB:
1319 case IWL_LEGACY_SWITCH_MIMO2_AC:
1320 case IWL_LEGACY_SWITCH_MIMO2_BC:
1321 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO2\n");
1323 /* Set up search table to try MIMO */
1324 memcpy(search_tbl, tbl, sz);
1325 search_tbl->is_SGI = 0;
1327 if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AB)
1328 search_tbl->ant_type = ANT_AB;
1329 else if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AC)
1330 search_tbl->ant_type = ANT_AC;
1332 search_tbl->ant_type = ANT_BC;
1334 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1337 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
1340 lq_sta->action_counter = 0;
1345 case IWL_LEGACY_SWITCH_MIMO3_ABC:
1346 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO3\n");
1348 /* Set up search table to try MIMO3 */
1349 memcpy(search_tbl, tbl, sz);
1350 search_tbl->is_SGI = 0;
1352 search_tbl->ant_type = ANT_ABC;
1354 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1357 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1360 lq_sta->action_counter = 0;
1366 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
1367 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
1369 if (tbl->action == start_action)
1373 search_tbl->lq_type = LQ_NONE;
1377 lq_sta->search_better_tbl = 1;
1379 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
1380 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
1381 if (update_search_tbl_counter)
1382 search_tbl->action = tbl->action;
1388 * Try to switch to new modulation mode from SISO
1390 static int rs_move_siso_to_other(struct iwl_priv *priv,
1391 struct iwl_lq_sta *lq_sta,
1392 struct ieee80211_conf *conf,
1393 struct ieee80211_sta *sta, int index)
1395 u8 is_green = lq_sta->is_green;
1396 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1397 struct iwl_scale_tbl_info *search_tbl =
1398 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1399 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1400 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
1401 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1402 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1403 u8 start_action = tbl->action;
1404 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1405 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1406 u8 update_search_tbl_counter = 0;
1409 if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE &&
1410 tbl->action > IWL_SISO_SWITCH_ANTENNA2) {
1412 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1415 lq_sta->action_counter++;
1416 switch (tbl->action) {
1417 case IWL_SISO_SWITCH_ANTENNA1:
1418 case IWL_SISO_SWITCH_ANTENNA2:
1419 IWL_DEBUG_RATE(priv, "LQ: SISO toggle Antenna\n");
1421 if ((tbl->action == IWL_SISO_SWITCH_ANTENNA1 &&
1422 tx_chains_num <= 1) ||
1423 (tbl->action == IWL_SISO_SWITCH_ANTENNA2 &&
1424 tx_chains_num <= 2))
1427 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1430 memcpy(search_tbl, tbl, sz);
1431 if (rs_toggle_antenna(valid_tx_ant,
1432 &search_tbl->current_rate, search_tbl)) {
1433 update_search_tbl_counter = 1;
1437 case IWL_SISO_SWITCH_MIMO2_AB:
1438 case IWL_SISO_SWITCH_MIMO2_AC:
1439 case IWL_SISO_SWITCH_MIMO2_BC:
1440 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO2\n");
1441 memcpy(search_tbl, tbl, sz);
1442 search_tbl->is_SGI = 0;
1444 if (tbl->action == IWL_SISO_SWITCH_MIMO2_AB)
1445 search_tbl->ant_type = ANT_AB;
1446 else if (tbl->action == IWL_SISO_SWITCH_MIMO2_AC)
1447 search_tbl->ant_type = ANT_AC;
1449 search_tbl->ant_type = ANT_BC;
1451 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1454 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
1459 case IWL_SISO_SWITCH_GI:
1460 if (!tbl->is_ht40 && !(ht_cap->cap &
1461 IEEE80211_HT_CAP_SGI_20))
1463 if (tbl->is_ht40 && !(ht_cap->cap &
1464 IEEE80211_HT_CAP_SGI_40))
1467 IWL_DEBUG_RATE(priv, "LQ: SISO toggle SGI/NGI\n");
1469 memcpy(search_tbl, tbl, sz);
1475 "SGI was set in GF+SISO\n");
1477 search_tbl->is_SGI = !tbl->is_SGI;
1478 rs_set_expected_tpt_table(lq_sta, search_tbl);
1480 s32 tpt = lq_sta->last_tpt / 100;
1481 if (tpt >= search_tbl->expected_tpt[index])
1484 search_tbl->current_rate =
1485 rate_n_flags_from_tbl(priv, search_tbl,
1487 update_search_tbl_counter = 1;
1489 case IWL_SISO_SWITCH_MIMO3_ABC:
1490 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO3\n");
1491 memcpy(search_tbl, tbl, sz);
1492 search_tbl->is_SGI = 0;
1493 search_tbl->ant_type = ANT_ABC;
1495 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1498 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1505 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
1506 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1508 if (tbl->action == start_action)
1511 search_tbl->lq_type = LQ_NONE;
1515 lq_sta->search_better_tbl = 1;
1517 if (tbl->action > IWL_SISO_SWITCH_MIMO3_ABC)
1518 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1519 if (update_search_tbl_counter)
1520 search_tbl->action = tbl->action;
1526 * Try to switch to new modulation mode from MIMO2
1528 static int rs_move_mimo2_to_other(struct iwl_priv *priv,
1529 struct iwl_lq_sta *lq_sta,
1530 struct ieee80211_conf *conf,
1531 struct ieee80211_sta *sta, int index)
1533 s8 is_green = lq_sta->is_green;
1534 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1535 struct iwl_scale_tbl_info *search_tbl =
1536 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1537 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1538 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
1539 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1540 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1541 u8 start_action = tbl->action;
1542 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1543 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1544 u8 update_search_tbl_counter = 0;
1547 if ((iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE) &&
1548 (tbl->action < IWL_MIMO2_SWITCH_SISO_A ||
1549 tbl->action > IWL_MIMO2_SWITCH_SISO_C)) {
1550 /* switch in SISO */
1551 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1554 lq_sta->action_counter++;
1555 switch (tbl->action) {
1556 case IWL_MIMO2_SWITCH_ANTENNA1:
1557 case IWL_MIMO2_SWITCH_ANTENNA2:
1558 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle Antennas\n");
1560 if (tx_chains_num <= 2)
1563 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1566 memcpy(search_tbl, tbl, sz);
1567 if (rs_toggle_antenna(valid_tx_ant,
1568 &search_tbl->current_rate, search_tbl)) {
1569 update_search_tbl_counter = 1;
1573 case IWL_MIMO2_SWITCH_SISO_A:
1574 case IWL_MIMO2_SWITCH_SISO_B:
1575 case IWL_MIMO2_SWITCH_SISO_C:
1576 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to SISO\n");
1578 /* Set up new search table for SISO */
1579 memcpy(search_tbl, tbl, sz);
1581 if (tbl->action == IWL_MIMO2_SWITCH_SISO_A)
1582 search_tbl->ant_type = ANT_A;
1583 else if (tbl->action == IWL_MIMO2_SWITCH_SISO_B)
1584 search_tbl->ant_type = ANT_B;
1586 search_tbl->ant_type = ANT_C;
1588 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1591 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
1598 case IWL_MIMO2_SWITCH_GI:
1599 if (!tbl->is_ht40 && !(ht_cap->cap &
1600 IEEE80211_HT_CAP_SGI_20))
1602 if (tbl->is_ht40 && !(ht_cap->cap &
1603 IEEE80211_HT_CAP_SGI_40))
1606 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle SGI/NGI\n");
1608 /* Set up new search table for MIMO2 */
1609 memcpy(search_tbl, tbl, sz);
1610 search_tbl->is_SGI = !tbl->is_SGI;
1611 rs_set_expected_tpt_table(lq_sta, search_tbl);
1613 * If active table already uses the fastest possible
1614 * modulation (dual stream with short guard interval),
1615 * and it's working well, there's no need to look
1616 * for a better type of modulation!
1619 s32 tpt = lq_sta->last_tpt / 100;
1620 if (tpt >= search_tbl->expected_tpt[index])
1623 search_tbl->current_rate =
1624 rate_n_flags_from_tbl(priv, search_tbl,
1626 update_search_tbl_counter = 1;
1629 case IWL_MIMO2_SWITCH_MIMO3_ABC:
1630 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to MIMO3\n");
1631 memcpy(search_tbl, tbl, sz);
1632 search_tbl->is_SGI = 0;
1633 search_tbl->ant_type = ANT_ABC;
1635 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1638 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1646 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1647 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
1649 if (tbl->action == start_action)
1652 search_tbl->lq_type = LQ_NONE;
1655 lq_sta->search_better_tbl = 1;
1657 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1658 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
1659 if (update_search_tbl_counter)
1660 search_tbl->action = tbl->action;
1667 * Try to switch to new modulation mode from MIMO3
1669 static int rs_move_mimo3_to_other(struct iwl_priv *priv,
1670 struct iwl_lq_sta *lq_sta,
1671 struct ieee80211_conf *conf,
1672 struct ieee80211_sta *sta, int index)
1674 s8 is_green = lq_sta->is_green;
1675 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1676 struct iwl_scale_tbl_info *search_tbl =
1677 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1678 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1679 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
1680 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1681 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1682 u8 start_action = tbl->action;
1683 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1684 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1686 u8 update_search_tbl_counter = 0;
1688 if ((iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE) &&
1689 (tbl->action < IWL_MIMO3_SWITCH_SISO_A ||
1690 tbl->action > IWL_MIMO3_SWITCH_SISO_C)) {
1691 /* switch in SISO */
1692 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
1695 lq_sta->action_counter++;
1696 switch (tbl->action) {
1697 case IWL_MIMO3_SWITCH_ANTENNA1:
1698 case IWL_MIMO3_SWITCH_ANTENNA2:
1699 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle Antennas\n");
1701 if (tx_chains_num <= 3)
1704 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1707 memcpy(search_tbl, tbl, sz);
1708 if (rs_toggle_antenna(valid_tx_ant,
1709 &search_tbl->current_rate, search_tbl))
1712 case IWL_MIMO3_SWITCH_SISO_A:
1713 case IWL_MIMO3_SWITCH_SISO_B:
1714 case IWL_MIMO3_SWITCH_SISO_C:
1715 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to SISO\n");
1717 /* Set up new search table for SISO */
1718 memcpy(search_tbl, tbl, sz);
1720 if (tbl->action == IWL_MIMO3_SWITCH_SISO_A)
1721 search_tbl->ant_type = ANT_A;
1722 else if (tbl->action == IWL_MIMO3_SWITCH_SISO_B)
1723 search_tbl->ant_type = ANT_B;
1725 search_tbl->ant_type = ANT_C;
1727 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1730 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
1737 case IWL_MIMO3_SWITCH_MIMO2_AB:
1738 case IWL_MIMO3_SWITCH_MIMO2_AC:
1739 case IWL_MIMO3_SWITCH_MIMO2_BC:
1740 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to MIMO2\n");
1742 memcpy(search_tbl, tbl, sz);
1743 search_tbl->is_SGI = 0;
1744 if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AB)
1745 search_tbl->ant_type = ANT_AB;
1746 else if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AC)
1747 search_tbl->ant_type = ANT_AC;
1749 search_tbl->ant_type = ANT_BC;
1751 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1754 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
1761 case IWL_MIMO3_SWITCH_GI:
1762 if (!tbl->is_ht40 && !(ht_cap->cap &
1763 IEEE80211_HT_CAP_SGI_20))
1765 if (tbl->is_ht40 && !(ht_cap->cap &
1766 IEEE80211_HT_CAP_SGI_40))
1769 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle SGI/NGI\n");
1771 /* Set up new search table for MIMO */
1772 memcpy(search_tbl, tbl, sz);
1773 search_tbl->is_SGI = !tbl->is_SGI;
1774 rs_set_expected_tpt_table(lq_sta, search_tbl);
1776 * If active table already uses the fastest possible
1777 * modulation (dual stream with short guard interval),
1778 * and it's working well, there's no need to look
1779 * for a better type of modulation!
1782 s32 tpt = lq_sta->last_tpt / 100;
1783 if (tpt >= search_tbl->expected_tpt[index])
1786 search_tbl->current_rate =
1787 rate_n_flags_from_tbl(priv, search_tbl,
1789 update_search_tbl_counter = 1;
1793 if (tbl->action > IWL_MIMO3_SWITCH_GI)
1794 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
1796 if (tbl->action == start_action)
1799 search_tbl->lq_type = LQ_NONE;
1802 lq_sta->search_better_tbl = 1;
1804 if (tbl->action > IWL_MIMO3_SWITCH_GI)
1805 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
1806 if (update_search_tbl_counter)
1807 search_tbl->action = tbl->action;
1814 * Check whether we should continue using same modulation mode, or
1815 * begin search for a new mode, based on:
1816 * 1) # tx successes or failures while using this mode
1817 * 2) # times calling this function
1818 * 3) elapsed time in this mode (not used, for now)
1820 static void rs_stay_in_table(struct iwl_lq_sta *lq_sta)
1822 struct iwl_scale_tbl_info *tbl;
1825 int flush_interval_passed = 0;
1826 struct iwl_priv *priv;
1829 active_tbl = lq_sta->active_tbl;
1831 tbl = &(lq_sta->lq_info[active_tbl]);
1833 /* If we've been disallowing search, see if we should now allow it */
1834 if (lq_sta->stay_in_tbl) {
1836 /* Elapsed time using current modulation mode */
1837 if (lq_sta->flush_timer)
1838 flush_interval_passed =
1840 (unsigned long)(lq_sta->flush_timer +
1841 IWL_RATE_SCALE_FLUSH_INTVL));
1844 * Check if we should allow search for new modulation mode.
1845 * If many frames have failed or succeeded, or we've used
1846 * this same modulation for a long time, allow search, and
1847 * reset history stats that keep track of whether we should
1848 * allow a new search. Also (below) reset all bitmaps and
1849 * stats in active history.
1851 if ((lq_sta->total_failed > lq_sta->max_failure_limit) ||
1852 (lq_sta->total_success > lq_sta->max_success_limit) ||
1853 ((!lq_sta->search_better_tbl) && (lq_sta->flush_timer)
1854 && (flush_interval_passed))) {
1855 IWL_DEBUG_RATE(priv, "LQ: stay is expired %d %d %d\n:",
1856 lq_sta->total_failed,
1857 lq_sta->total_success,
1858 flush_interval_passed);
1860 /* Allow search for new mode */
1861 lq_sta->stay_in_tbl = 0; /* only place reset */
1862 lq_sta->total_failed = 0;
1863 lq_sta->total_success = 0;
1864 lq_sta->flush_timer = 0;
1867 * Else if we've used this modulation mode enough repetitions
1868 * (regardless of elapsed time or success/failure), reset
1869 * history bitmaps and rate-specific stats for all rates in
1873 lq_sta->table_count++;
1874 if (lq_sta->table_count >=
1875 lq_sta->table_count_limit) {
1876 lq_sta->table_count = 0;
1878 IWL_DEBUG_RATE(priv, "LQ: stay in table clear win\n");
1879 for (i = 0; i < IWL_RATE_COUNT; i++)
1880 rs_rate_scale_clear_window(
1885 /* If transitioning to allow "search", reset all history
1886 * bitmaps and stats in active table (this will become the new
1887 * "search" table). */
1888 if (!lq_sta->stay_in_tbl) {
1889 for (i = 0; i < IWL_RATE_COUNT; i++)
1890 rs_rate_scale_clear_window(&(tbl->win[i]));
1896 * setup rate table in uCode
1897 * return rate_n_flags as used in the table
1899 static u32 rs_update_rate_tbl(struct iwl_priv *priv,
1900 struct iwl_lq_sta *lq_sta,
1901 struct iwl_scale_tbl_info *tbl,
1902 int index, u8 is_green)
1906 /* Update uCode's rate table. */
1907 rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
1908 rs_fill_link_cmd(priv, lq_sta, rate);
1909 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC, false);
1915 * Do rate scaling and search for new modulation mode.
1917 static void rs_rate_scale_perform(struct iwl_priv *priv,
1918 struct sk_buff *skb,
1919 struct ieee80211_sta *sta,
1920 struct iwl_lq_sta *lq_sta)
1922 struct ieee80211_hw *hw = priv->hw;
1923 struct ieee80211_conf *conf = &hw->conf;
1924 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1925 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1926 int low = IWL_RATE_INVALID;
1927 int high = IWL_RATE_INVALID;
1930 struct iwl_rate_scale_data *window = NULL;
1931 int current_tpt = IWL_INVALID_VALUE;
1932 int low_tpt = IWL_INVALID_VALUE;
1933 int high_tpt = IWL_INVALID_VALUE;
1935 s8 scale_action = 0;
1938 struct iwl_scale_tbl_info *tbl, *tbl1;
1939 u16 rate_scale_index_msk = 0;
1946 u8 tid = MAX_TID_COUNT;
1947 struct iwl_tid_data *tid_data;
1949 IWL_DEBUG_RATE(priv, "rate scale calculate new rate for skb\n");
1951 /* Send management frames and NO_ACK data using lowest rate. */
1952 /* TODO: this could probably be improved.. */
1953 if (!ieee80211_is_data(hdr->frame_control) ||
1954 info->flags & IEEE80211_TX_CTL_NO_ACK)
1957 if (!sta || !lq_sta)
1960 lq_sta->supp_rates = sta->supp_rates[lq_sta->band];
1962 tid = rs_tl_add_packet(lq_sta, hdr);
1963 if ((tid != MAX_TID_COUNT) && (lq_sta->tx_agg_tid_en & (1 << tid))) {
1964 tid_data = &priv->stations[lq_sta->lq.sta_id].tid[tid];
1965 if (tid_data->agg.state == IWL_AGG_OFF)
1973 * Select rate-scale / modulation-mode table to work with in
1974 * the rest of this function: "search" if searching for better
1975 * modulation mode, or "active" if doing rate scaling within a mode.
1977 if (!lq_sta->search_better_tbl)
1978 active_tbl = lq_sta->active_tbl;
1980 active_tbl = 1 - lq_sta->active_tbl;
1982 tbl = &(lq_sta->lq_info[active_tbl]);
1983 if (is_legacy(tbl->lq_type))
1984 lq_sta->is_green = 0;
1986 lq_sta->is_green = rs_use_green(sta, &priv->current_ht_config);
1987 is_green = lq_sta->is_green;
1989 /* current tx rate */
1990 index = lq_sta->last_txrate_idx;
1992 IWL_DEBUG_RATE(priv, "Rate scale index %d for type %d\n", index,
1995 /* rates available for this association, and for modulation mode */
1996 rate_mask = rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
1998 IWL_DEBUG_RATE(priv, "mask 0x%04X\n", rate_mask);
2000 /* mask with station rate restriction */
2001 if (is_legacy(tbl->lq_type)) {
2002 if (lq_sta->band == IEEE80211_BAND_5GHZ)
2003 /* supp_rates has no CCK bits in A mode */
2004 rate_scale_index_msk = (u16) (rate_mask &
2005 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
2007 rate_scale_index_msk = (u16) (rate_mask &
2008 lq_sta->supp_rates);
2011 rate_scale_index_msk = rate_mask;
2013 if (!rate_scale_index_msk)
2014 rate_scale_index_msk = rate_mask;
2016 if (!((1 << index) & rate_scale_index_msk)) {
2017 IWL_ERR(priv, "Current Rate is not valid\n");
2018 if (lq_sta->search_better_tbl) {
2019 /* revert to active table if search table is not valid*/
2020 tbl->lq_type = LQ_NONE;
2021 lq_sta->search_better_tbl = 0;
2022 tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
2023 /* get "active" rate info */
2024 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
2025 rate = rs_update_rate_tbl(priv, lq_sta,
2026 tbl, index, is_green);
2031 /* Get expected throughput table and history window for current rate */
2032 if (!tbl->expected_tpt) {
2033 IWL_ERR(priv, "tbl->expected_tpt is NULL\n");
2037 /* force user max rate if set by user */
2038 if ((lq_sta->max_rate_idx != -1) &&
2039 (lq_sta->max_rate_idx < index)) {
2040 index = lq_sta->max_rate_idx;
2042 window = &(tbl->win[index]);
2046 window = &(tbl->win[index]);
2049 * If there is not enough history to calculate actual average
2050 * throughput, keep analyzing results of more tx frames, without
2051 * changing rate or mode (bypass most of the rest of this function).
2052 * Set up new rate table in uCode only if old rate is not supported
2053 * in current association (use new rate found above).
2055 fail_count = window->counter - window->success_counter;
2056 if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
2057 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
2058 IWL_DEBUG_RATE(priv, "LQ: still below TH. succ=%d total=%d "
2060 window->success_counter, window->counter, index);
2062 /* Can't calculate this yet; not enough history */
2063 window->average_tpt = IWL_INVALID_VALUE;
2065 /* Should we stay with this modulation mode,
2066 * or search for a new one? */
2067 rs_stay_in_table(lq_sta);
2071 /* Else we have enough samples; calculate estimate of
2072 * actual average throughput */
2073 if (window->average_tpt != ((window->success_ratio *
2074 tbl->expected_tpt[index] + 64) / 128)) {
2075 IWL_ERR(priv, "expected_tpt should have been calculated by now\n");
2076 window->average_tpt = ((window->success_ratio *
2077 tbl->expected_tpt[index] + 64) / 128);
2080 /* If we are searching for better modulation mode, check success. */
2081 if (lq_sta->search_better_tbl &&
2082 (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_MULTI)) {
2083 /* If good success, continue using the "search" mode;
2084 * no need to send new link quality command, since we're
2085 * continuing to use the setup that we've been trying. */
2086 if (window->average_tpt > lq_sta->last_tpt) {
2088 IWL_DEBUG_RATE(priv, "LQ: SWITCHING TO NEW TABLE "
2089 "suc=%d cur-tpt=%d old-tpt=%d\n",
2090 window->success_ratio,
2091 window->average_tpt,
2094 if (!is_legacy(tbl->lq_type))
2095 lq_sta->enable_counter = 1;
2097 /* Swap tables; "search" becomes "active" */
2098 lq_sta->active_tbl = active_tbl;
2099 current_tpt = window->average_tpt;
2101 /* Else poor success; go back to mode in "active" table */
2104 IWL_DEBUG_RATE(priv, "LQ: GOING BACK TO THE OLD TABLE "
2105 "suc=%d cur-tpt=%d old-tpt=%d\n",
2106 window->success_ratio,
2107 window->average_tpt,
2110 /* Nullify "search" table */
2111 tbl->lq_type = LQ_NONE;
2113 /* Revert to "active" table */
2114 active_tbl = lq_sta->active_tbl;
2115 tbl = &(lq_sta->lq_info[active_tbl]);
2117 /* Revert to "active" rate and throughput info */
2118 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
2119 current_tpt = lq_sta->last_tpt;
2121 /* Need to set up a new rate table in uCode */
2125 /* Either way, we've made a decision; modulation mode
2126 * search is done, allow rate adjustment next time. */
2127 lq_sta->search_better_tbl = 0;
2128 done_search = 1; /* Don't switch modes below! */
2132 /* (Else) not in search of better modulation mode, try for better
2133 * starting rate, while staying in this mode. */
2134 high_low = rs_get_adjacent_rate(priv, index, rate_scale_index_msk,
2136 low = high_low & 0xff;
2137 high = (high_low >> 8) & 0xff;
2139 /* If user set max rate, dont allow higher than user constrain */
2140 if ((lq_sta->max_rate_idx != -1) &&
2141 (lq_sta->max_rate_idx < high))
2142 high = IWL_RATE_INVALID;
2144 sr = window->success_ratio;
2146 /* Collect measured throughputs for current and adjacent rates */
2147 current_tpt = window->average_tpt;
2148 if (low != IWL_RATE_INVALID)
2149 low_tpt = tbl->win[low].average_tpt;
2150 if (high != IWL_RATE_INVALID)
2151 high_tpt = tbl->win[high].average_tpt;
2155 /* Too many failures, decrease rate */
2156 if ((sr <= IWL_RATE_DECREASE_TH) || (current_tpt == 0)) {
2157 IWL_DEBUG_RATE(priv, "decrease rate because of low success_ratio\n");
2160 /* No throughput measured yet for adjacent rates; try increase. */
2161 } else if ((low_tpt == IWL_INVALID_VALUE) &&
2162 (high_tpt == IWL_INVALID_VALUE)) {
2164 if (high != IWL_RATE_INVALID && sr >= IWL_RATE_INCREASE_TH)
2166 else if (low != IWL_RATE_INVALID)
2170 /* Both adjacent throughputs are measured, but neither one has better
2171 * throughput; we're using the best rate, don't change it! */
2172 else if ((low_tpt != IWL_INVALID_VALUE) &&
2173 (high_tpt != IWL_INVALID_VALUE) &&
2174 (low_tpt < current_tpt) &&
2175 (high_tpt < current_tpt))
2178 /* At least one adjacent rate's throughput is measured,
2179 * and may have better performance. */
2181 /* Higher adjacent rate's throughput is measured */
2182 if (high_tpt != IWL_INVALID_VALUE) {
2183 /* Higher rate has better throughput */
2184 if (high_tpt > current_tpt &&
2185 sr >= IWL_RATE_INCREASE_TH) {
2191 /* Lower adjacent rate's throughput is measured */
2192 } else if (low_tpt != IWL_INVALID_VALUE) {
2193 /* Lower rate has better throughput */
2194 if (low_tpt > current_tpt) {
2195 IWL_DEBUG_RATE(priv,
2196 "decrease rate because of low tpt\n");
2198 } else if (sr >= IWL_RATE_INCREASE_TH) {
2204 /* Sanity check; asked for decrease, but success rate or throughput
2205 * has been good at old rate. Don't change it. */
2206 if ((scale_action == -1) && (low != IWL_RATE_INVALID) &&
2207 ((sr > IWL_RATE_HIGH_TH) ||
2208 (current_tpt > (100 * tbl->expected_tpt[low]))))
2210 if (!iwl_ht_enabled(priv) && !is_legacy(tbl->lq_type))
2212 if (iwl_tx_ant_restriction(priv) != IWL_ANT_OK_MULTI &&
2213 (is_mimo2(tbl->lq_type) || is_mimo3(tbl->lq_type)))
2215 switch (scale_action) {
2217 /* Decrease starting rate, update uCode's rate table */
2218 if (low != IWL_RATE_INVALID) {
2225 /* Increase starting rate, update uCode's rate table */
2226 if (high != IWL_RATE_INVALID) {
2238 IWL_DEBUG_RATE(priv, "choose rate scale index %d action %d low %d "
2239 "high %d type %d\n",
2240 index, scale_action, low, high, tbl->lq_type);
2243 /* Replace uCode's rate table for the destination station. */
2245 rate = rs_update_rate_tbl(priv, lq_sta,
2246 tbl, index, is_green);
2248 if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_MULTI) {
2249 /* Should we stay with this modulation mode,
2250 * or search for a new one? */
2251 rs_stay_in_table(lq_sta);
2254 * Search for new modulation mode if we're:
2255 * 1) Not changing rates right now
2256 * 2) Not just finishing up a search
2257 * 3) Allowing a new search
2259 if (!update_lq && !done_search && !lq_sta->stay_in_tbl && window->counter) {
2260 /* Save current throughput to compare with "search" throughput*/
2261 lq_sta->last_tpt = current_tpt;
2263 /* Select a new "search" modulation mode to try.
2264 * If one is found, set up the new "search" table. */
2265 if (is_legacy(tbl->lq_type))
2266 rs_move_legacy_other(priv, lq_sta, conf, sta, index);
2267 else if (is_siso(tbl->lq_type))
2268 rs_move_siso_to_other(priv, lq_sta, conf, sta, index);
2269 else if (is_mimo2(tbl->lq_type))
2270 rs_move_mimo2_to_other(priv, lq_sta, conf, sta, index);
2272 rs_move_mimo3_to_other(priv, lq_sta, conf, sta, index);
2274 /* If new "search" mode was selected, set up in uCode table */
2275 if (lq_sta->search_better_tbl) {
2276 /* Access the "search" table, clear its history. */
2277 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
2278 for (i = 0; i < IWL_RATE_COUNT; i++)
2279 rs_rate_scale_clear_window(&(tbl->win[i]));
2281 /* Use new "search" start rate */
2282 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
2284 IWL_DEBUG_RATE(priv, "Switch current mcs: %X index: %d\n",
2285 tbl->current_rate, index);
2286 rs_fill_link_cmd(priv, lq_sta, tbl->current_rate);
2287 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC, false);
2292 if (done_search && !lq_sta->stay_in_tbl) {
2293 /* If the "active" (non-search) mode was legacy,
2294 * and we've tried switching antennas,
2295 * but we haven't been able to try HT modes (not available),
2296 * stay with best antenna legacy modulation for a while
2297 * before next round of mode comparisons. */
2298 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
2299 if (is_legacy(tbl1->lq_type) && !conf_is_ht(conf) &&
2300 lq_sta->action_counter > tbl1->max_search) {
2301 IWL_DEBUG_RATE(priv, "LQ: STAY in legacy table\n");
2302 rs_set_stay_in_table(priv, 1, lq_sta);
2305 /* If we're in an HT mode, and all 3 mode switch actions
2306 * have been tried and compared, stay in this best modulation
2307 * mode for a while before next round of mode comparisons. */
2308 if (lq_sta->enable_counter &&
2309 (lq_sta->action_counter >= tbl1->max_search) &&
2310 iwl_ht_enabled(priv)) {
2311 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2312 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
2313 (tid != MAX_TID_COUNT)) {
2315 &priv->stations[lq_sta->lq.sta_id].tid[tid];
2316 if (tid_data->agg.state == IWL_AGG_OFF) {
2317 IWL_DEBUG_RATE(priv,
2318 "try to aggregate tid %d\n",
2320 rs_tl_turn_on_agg(priv, tid,
2324 rs_set_stay_in_table(priv, 0, lq_sta);
2329 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
2331 lq_sta->last_txrate_idx = i;
2335 * rs_initialize_lq - Initialize a station's hardware rate table
2337 * The uCode's station table contains a table of fallback rates
2338 * for automatic fallback during transmission.
2340 * NOTE: This sets up a default set of values. These will be replaced later
2341 * if the driver's iwl-agn-rs rate scaling algorithm is used, instead of
2344 * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
2345 * calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
2346 * which requires station table entry to exist).
2348 static void rs_initialize_lq(struct iwl_priv *priv,
2349 struct ieee80211_conf *conf,
2350 struct ieee80211_sta *sta,
2351 struct iwl_lq_sta *lq_sta)
2353 struct iwl_scale_tbl_info *tbl;
2357 u8 use_green = rs_use_green(sta, &priv->current_ht_config);
2361 if (!sta || !lq_sta)
2364 i = lq_sta->last_txrate_idx;
2366 valid_tx_ant = priv->hw_params.valid_tx_ant;
2368 if (!lq_sta->search_better_tbl)
2369 active_tbl = lq_sta->active_tbl;
2371 active_tbl = 1 - lq_sta->active_tbl;
2373 tbl = &(lq_sta->lq_info[active_tbl]);
2375 if ((i < 0) || (i >= IWL_RATE_COUNT))
2378 rate = iwl_rates[i].plcp;
2379 tbl->ant_type = first_antenna(valid_tx_ant);
2380 rate |= tbl->ant_type << RATE_MCS_ANT_POS;
2382 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
2383 rate |= RATE_MCS_CCK_MSK;
2385 rs_get_tbl_info_from_mcs(rate, priv->band, tbl, &rate_idx);
2386 if (!rs_is_valid_ant(valid_tx_ant, tbl->ant_type))
2387 rs_toggle_antenna(valid_tx_ant, &rate, tbl);
2389 rate = rate_n_flags_from_tbl(priv, tbl, rate_idx, use_green);
2390 tbl->current_rate = rate;
2391 rs_set_expected_tpt_table(lq_sta, tbl);
2392 rs_fill_link_cmd(NULL, lq_sta, rate);
2393 priv->stations[lq_sta->lq.sta_id].lq = &lq_sta->lq;
2394 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_SYNC, true);
2399 static void rs_get_rate(void *priv_r, struct ieee80211_sta *sta, void *priv_sta,
2400 struct ieee80211_tx_rate_control *txrc)
2403 struct sk_buff *skb = txrc->skb;
2404 struct ieee80211_supported_band *sband = txrc->sband;
2405 struct iwl_priv *priv __maybe_unused = (struct iwl_priv *)priv_r;
2406 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2407 struct iwl_lq_sta *lq_sta = priv_sta;
2410 IWL_DEBUG_RATE_LIMIT(priv, "rate scale calculate new rate for skb\n");
2412 /* Get max rate if user set max rate */
2414 lq_sta->max_rate_idx = txrc->max_rate_idx;
2415 if ((sband->band == IEEE80211_BAND_5GHZ) &&
2416 (lq_sta->max_rate_idx != -1))
2417 lq_sta->max_rate_idx += IWL_FIRST_OFDM_RATE;
2418 if ((lq_sta->max_rate_idx < 0) ||
2419 (lq_sta->max_rate_idx >= IWL_RATE_COUNT))
2420 lq_sta->max_rate_idx = -1;
2423 /* Treat uninitialized rate scaling data same as non-existing. */
2424 if (lq_sta && !lq_sta->drv) {
2425 IWL_DEBUG_RATE(priv, "Rate scaling not initialized yet.\n");
2429 /* Send management frames and NO_ACK data using lowest rate. */
2430 if (rate_control_send_low(sta, priv_sta, txrc))
2433 rate_idx = lq_sta->last_txrate_idx;
2435 if (lq_sta->last_rate_n_flags & RATE_MCS_HT_MSK) {
2436 rate_idx -= IWL_FIRST_OFDM_RATE;
2437 /* 6M and 9M shared same MCS index */
2438 rate_idx = (rate_idx > 0) ? (rate_idx - 1) : 0;
2439 if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
2440 IWL_RATE_MIMO3_6M_PLCP)
2441 rate_idx = rate_idx + (2 * MCS_INDEX_PER_STREAM);
2442 else if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
2443 IWL_RATE_MIMO2_6M_PLCP)
2444 rate_idx = rate_idx + MCS_INDEX_PER_STREAM;
2445 info->control.rates[0].flags = IEEE80211_TX_RC_MCS;
2446 if (lq_sta->last_rate_n_flags & RATE_MCS_SGI_MSK)
2447 info->control.rates[0].flags |= IEEE80211_TX_RC_SHORT_GI;
2448 if (lq_sta->last_rate_n_flags & RATE_MCS_DUP_MSK)
2449 info->control.rates[0].flags |= IEEE80211_TX_RC_DUP_DATA;
2450 if (lq_sta->last_rate_n_flags & RATE_MCS_HT40_MSK)
2451 info->control.rates[0].flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
2452 if (lq_sta->last_rate_n_flags & RATE_MCS_GF_MSK)
2453 info->control.rates[0].flags |= IEEE80211_TX_RC_GREEN_FIELD;
2455 /* Check for invalid rates */
2456 if ((rate_idx < 0) || (rate_idx >= IWL_RATE_COUNT_LEGACY) ||
2457 ((sband->band == IEEE80211_BAND_5GHZ) &&
2458 (rate_idx < IWL_FIRST_OFDM_RATE)))
2459 rate_idx = rate_lowest_index(sband, sta);
2460 /* On valid 5 GHz rate, adjust index */
2461 else if (sband->band == IEEE80211_BAND_5GHZ)
2462 rate_idx -= IWL_FIRST_OFDM_RATE;
2463 info->control.rates[0].flags = 0;
2465 info->control.rates[0].idx = rate_idx;
2469 static void *rs_alloc_sta(void *priv_rate, struct ieee80211_sta *sta,
2472 struct iwl_lq_sta *lq_sta;
2473 struct iwl_station_priv *sta_priv = (struct iwl_station_priv *) sta->drv_priv;
2474 struct iwl_priv *priv;
2476 priv = (struct iwl_priv *)priv_rate;
2477 IWL_DEBUG_RATE(priv, "create station rate scale window\n");
2479 lq_sta = &sta_priv->lq_sta;
2485 * Called after adding a new station to initialize rate scaling
2487 void iwl_rs_rate_init(struct iwl_priv *priv, struct ieee80211_sta *sta, u8 sta_id)
2490 struct ieee80211_hw *hw = priv->hw;
2491 struct ieee80211_conf *conf = &priv->hw->conf;
2492 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
2493 struct iwl_station_priv *sta_priv;
2494 struct iwl_lq_sta *lq_sta;
2495 struct ieee80211_supported_band *sband;
2497 sta_priv = (struct iwl_station_priv *) sta->drv_priv;
2498 lq_sta = &sta_priv->lq_sta;
2499 sband = hw->wiphy->bands[conf->channel->band];
2502 lq_sta->lq.sta_id = sta_id;
2504 for (j = 0; j < LQ_SIZE; j++)
2505 for (i = 0; i < IWL_RATE_COUNT; i++)
2506 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
2508 lq_sta->flush_timer = 0;
2509 lq_sta->supp_rates = sta->supp_rates[sband->band];
2510 for (j = 0; j < LQ_SIZE; j++)
2511 for (i = 0; i < IWL_RATE_COUNT; i++)
2512 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
2514 IWL_DEBUG_RATE(priv, "LQ: *** rate scale station global init for station %d ***\n",
2516 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2517 * the lowest or the highest rate.. Could consider using RSSI from
2518 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2522 lq_sta->max_rate_idx = -1;
2523 lq_sta->missed_rate_counter = IWL_MISSED_RATE_MAX;
2524 lq_sta->is_green = rs_use_green(sta, &priv->current_ht_config);
2525 lq_sta->active_legacy_rate = priv->active_rate & ~(0x1000);
2526 lq_sta->band = priv->band;
2528 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2529 * supp_rates[] does not; shift to convert format, force 9 MBits off.
2531 lq_sta->active_siso_rate = ht_cap->mcs.rx_mask[0] << 1;
2532 lq_sta->active_siso_rate |= ht_cap->mcs.rx_mask[0] & 0x1;
2533 lq_sta->active_siso_rate &= ~((u16)0x2);
2534 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
2537 lq_sta->active_mimo2_rate = ht_cap->mcs.rx_mask[1] << 1;
2538 lq_sta->active_mimo2_rate |= ht_cap->mcs.rx_mask[1] & 0x1;
2539 lq_sta->active_mimo2_rate &= ~((u16)0x2);
2540 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2542 lq_sta->active_mimo3_rate = ht_cap->mcs.rx_mask[2] << 1;
2543 lq_sta->active_mimo3_rate |= ht_cap->mcs.rx_mask[2] & 0x1;
2544 lq_sta->active_mimo3_rate &= ~((u16)0x2);
2545 lq_sta->active_mimo3_rate <<= IWL_FIRST_OFDM_RATE;
2547 IWL_DEBUG_RATE(priv, "SISO-RATE=%X MIMO2-RATE=%X MIMO3-RATE=%X\n",
2548 lq_sta->active_siso_rate,
2549 lq_sta->active_mimo2_rate,
2550 lq_sta->active_mimo3_rate);
2552 /* These values will be overridden later */
2553 lq_sta->lq.general_params.single_stream_ant_msk =
2554 first_antenna(priv->hw_params.valid_tx_ant);
2555 lq_sta->lq.general_params.dual_stream_ant_msk =
2556 priv->hw_params.valid_tx_ant &
2557 ~first_antenna(priv->hw_params.valid_tx_ant);
2558 if (!lq_sta->lq.general_params.dual_stream_ant_msk) {
2559 lq_sta->lq.general_params.dual_stream_ant_msk = ANT_AB;
2560 } else if (num_of_ant(priv->hw_params.valid_tx_ant) == 2) {
2561 lq_sta->lq.general_params.dual_stream_ant_msk =
2562 priv->hw_params.valid_tx_ant;
2565 /* as default allow aggregation for all tids */
2566 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
2569 /* Set last_txrate_idx to lowest rate */
2570 lq_sta->last_txrate_idx = rate_lowest_index(sband, sta);
2571 if (sband->band == IEEE80211_BAND_5GHZ)
2572 lq_sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
2575 rs_initialize_lq(priv, conf, sta, lq_sta);
2578 static void rs_fill_link_cmd(struct iwl_priv *priv,
2579 struct iwl_lq_sta *lq_sta, u32 new_rate)
2581 struct iwl_scale_tbl_info tbl_type;
2584 int repeat_rate = 0;
2585 u8 ant_toggle_cnt = 0;
2586 u8 use_ht_possible = 1;
2587 u8 valid_tx_ant = 0;
2588 struct iwl_link_quality_cmd *lq_cmd = &lq_sta->lq;
2590 /* Override starting rate (index 0) if needed for debug purposes */
2591 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
2593 /* Interpret new_rate (rate_n_flags) */
2594 memset(&tbl_type, 0, sizeof(tbl_type));
2595 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
2596 &tbl_type, &rate_idx);
2598 /* How many times should we repeat the initial rate? */
2599 if (is_legacy(tbl_type.lq_type)) {
2601 repeat_rate = IWL_NUMBER_TRY;
2603 repeat_rate = IWL_HT_NUMBER_TRY;
2606 lq_cmd->general_params.mimo_delimiter =
2607 is_mimo(tbl_type.lq_type) ? 1 : 0;
2609 /* Fill 1st table entry (index 0) */
2610 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
2612 if (num_of_ant(tbl_type.ant_type) == 1) {
2613 lq_cmd->general_params.single_stream_ant_msk =
2615 } else if (num_of_ant(tbl_type.ant_type) == 2) {
2616 lq_cmd->general_params.dual_stream_ant_msk =
2618 } /* otherwise we don't modify the existing value */
2624 valid_tx_ant = priv->hw_params.valid_tx_ant;
2626 /* Fill rest of rate table */
2627 while (index < LINK_QUAL_MAX_RETRY_NUM) {
2628 /* Repeat initial/next rate.
2629 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2630 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
2631 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
2632 if (is_legacy(tbl_type.lq_type)) {
2633 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2636 rs_toggle_antenna(valid_tx_ant,
2637 &new_rate, &tbl_type))
2641 /* Override next rate if needed for debug purposes */
2642 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
2644 /* Fill next table entry */
2645 lq_cmd->rs_table[index].rate_n_flags =
2646 cpu_to_le32(new_rate);
2651 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band, &tbl_type,
2654 /* Indicate to uCode which entries might be MIMO.
2655 * If initial rate was MIMO, this will finally end up
2656 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
2657 if (is_mimo(tbl_type.lq_type))
2658 lq_cmd->general_params.mimo_delimiter = index;
2661 new_rate = rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
2664 /* How many times should we repeat the next rate? */
2665 if (is_legacy(tbl_type.lq_type)) {
2666 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2669 rs_toggle_antenna(valid_tx_ant,
2670 &new_rate, &tbl_type))
2673 repeat_rate = IWL_NUMBER_TRY;
2675 repeat_rate = IWL_HT_NUMBER_TRY;
2678 /* Don't allow HT rates after next pass.
2679 * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
2680 use_ht_possible = 0;
2682 /* Override next rate if needed for debug purposes */
2683 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
2685 /* Fill next table entry */
2686 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
2692 lq_cmd->agg_params.agg_frame_cnt_limit = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
2693 lq_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
2694 lq_cmd->agg_params.agg_time_limit =
2695 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
2698 static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
2702 /* rate scale requires free function to be implemented */
2703 static void rs_free(void *priv_rate)
2708 static void rs_free_sta(void *priv_r, struct ieee80211_sta *sta,
2711 struct iwl_priv *priv __maybe_unused = priv_r;
2713 IWL_DEBUG_RATE(priv, "enter\n");
2714 IWL_DEBUG_RATE(priv, "leave\n");
2718 #ifdef CONFIG_MAC80211_DEBUGFS
2719 static int open_file_generic(struct inode *inode, struct file *file)
2721 file->private_data = inode->i_private;
2724 static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
2725 u32 *rate_n_flags, int index)
2727 struct iwl_priv *priv;
2732 valid_tx_ant = priv->hw_params.valid_tx_ant;
2733 if (lq_sta->dbg_fixed_rate) {
2735 ((lq_sta->dbg_fixed_rate & RATE_MCS_ANT_ABC_MSK)
2736 >> RATE_MCS_ANT_POS);
2737 if ((valid_tx_ant & ant_sel_tx) == ant_sel_tx) {
2738 *rate_n_flags = lq_sta->dbg_fixed_rate;
2739 IWL_DEBUG_RATE(priv, "Fixed rate ON\n");
2741 lq_sta->dbg_fixed_rate = 0;
2743 "Invalid antenna selection 0x%X, Valid is 0x%X\n",
2744 ant_sel_tx, valid_tx_ant);
2745 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
2748 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
2752 static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
2753 const char __user *user_buf, size_t count, loff_t *ppos)
2755 struct iwl_lq_sta *lq_sta = file->private_data;
2756 struct iwl_priv *priv;
2762 memset(buf, 0, sizeof(buf));
2763 buf_size = min(count, sizeof(buf) - 1);
2764 if (copy_from_user(buf, user_buf, buf_size))
2767 if (sscanf(buf, "%x", &parsed_rate) == 1)
2768 lq_sta->dbg_fixed_rate = parsed_rate;
2770 lq_sta->dbg_fixed_rate = 0;
2772 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
2773 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2774 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2775 lq_sta->active_mimo3_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2777 IWL_DEBUG_RATE(priv, "sta_id %d rate 0x%X\n",
2778 lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
2780 if (lq_sta->dbg_fixed_rate) {
2781 rs_fill_link_cmd(NULL, lq_sta, lq_sta->dbg_fixed_rate);
2782 iwl_send_lq_cmd(lq_sta->drv, &lq_sta->lq, CMD_ASYNC, false);
2788 static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
2789 char __user *user_buf, size_t count, loff_t *ppos)
2797 struct iwl_lq_sta *lq_sta = file->private_data;
2798 struct iwl_priv *priv;
2799 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
2802 buff = kmalloc(1024, GFP_KERNEL);
2806 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
2807 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
2808 lq_sta->total_failed, lq_sta->total_success,
2809 lq_sta->active_legacy_rate);
2810 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
2811 lq_sta->dbg_fixed_rate);
2812 desc += sprintf(buff+desc, "valid_tx_ant %s%s%s\n",
2813 (priv->hw_params.valid_tx_ant & ANT_A) ? "ANT_A," : "",
2814 (priv->hw_params.valid_tx_ant & ANT_B) ? "ANT_B," : "",
2815 (priv->hw_params.valid_tx_ant & ANT_C) ? "ANT_C" : "");
2816 desc += sprintf(buff+desc, "lq type %s\n",
2817 (is_legacy(tbl->lq_type)) ? "legacy" : "HT");
2818 if (is_Ht(tbl->lq_type)) {
2819 desc += sprintf(buff+desc, " %s",
2820 (is_siso(tbl->lq_type)) ? "SISO" :
2821 ((is_mimo2(tbl->lq_type)) ? "MIMO2" : "MIMO3"));
2822 desc += sprintf(buff+desc, " %s",
2823 (tbl->is_ht40) ? "40MHz" : "20MHz");
2824 desc += sprintf(buff+desc, " %s %s %s\n", (tbl->is_SGI) ? "SGI" : "",
2825 (lq_sta->is_green) ? "GF enabled" : "",
2826 (lq_sta->is_agg) ? "AGG on" : "");
2828 desc += sprintf(buff+desc, "last tx rate=0x%X\n",
2829 lq_sta->last_rate_n_flags);
2830 desc += sprintf(buff+desc, "general:"
2831 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
2832 lq_sta->lq.general_params.flags,
2833 lq_sta->lq.general_params.mimo_delimiter,
2834 lq_sta->lq.general_params.single_stream_ant_msk,
2835 lq_sta->lq.general_params.dual_stream_ant_msk);
2837 desc += sprintf(buff+desc, "agg:"
2838 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
2839 le16_to_cpu(lq_sta->lq.agg_params.agg_time_limit),
2840 lq_sta->lq.agg_params.agg_dis_start_th,
2841 lq_sta->lq.agg_params.agg_frame_cnt_limit);
2843 desc += sprintf(buff+desc,
2844 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
2845 lq_sta->lq.general_params.start_rate_index[0],
2846 lq_sta->lq.general_params.start_rate_index[1],
2847 lq_sta->lq.general_params.start_rate_index[2],
2848 lq_sta->lq.general_params.start_rate_index[3]);
2850 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
2851 index = iwl_hwrate_to_plcp_idx(
2852 le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags));
2853 if (is_legacy(tbl->lq_type)) {
2854 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps\n",
2855 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
2856 iwl_rate_mcs[index].mbps);
2858 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps (%s)\n",
2859 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
2860 iwl_rate_mcs[index].mbps, iwl_rate_mcs[index].mcs);
2864 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2869 static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
2870 .write = rs_sta_dbgfs_scale_table_write,
2871 .read = rs_sta_dbgfs_scale_table_read,
2872 .open = open_file_generic,
2874 static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
2875 char __user *user_buf, size_t count, loff_t *ppos)
2882 struct iwl_lq_sta *lq_sta = file->private_data;
2884 buff = kmalloc(1024, GFP_KERNEL);
2888 for (i = 0; i < LQ_SIZE; i++) {
2889 desc += sprintf(buff+desc,
2890 "%s type=%d SGI=%d HT40=%d DUP=%d GF=%d\n"
2892 lq_sta->active_tbl == i ? "*" : "x",
2893 lq_sta->lq_info[i].lq_type,
2894 lq_sta->lq_info[i].is_SGI,
2895 lq_sta->lq_info[i].is_ht40,
2896 lq_sta->lq_info[i].is_dup,
2898 lq_sta->lq_info[i].current_rate);
2899 for (j = 0; j < IWL_RATE_COUNT; j++) {
2900 desc += sprintf(buff+desc,
2901 "counter=%d success=%d %%=%d\n",
2902 lq_sta->lq_info[i].win[j].counter,
2903 lq_sta->lq_info[i].win[j].success_counter,
2904 lq_sta->lq_info[i].win[j].success_ratio);
2907 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2912 static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
2913 .read = rs_sta_dbgfs_stats_table_read,
2914 .open = open_file_generic,
2917 static ssize_t rs_sta_dbgfs_rate_scale_data_read(struct file *file,
2918 char __user *user_buf, size_t count, loff_t *ppos)
2924 struct iwl_lq_sta *lq_sta = file->private_data;
2925 struct iwl_priv *priv;
2926 struct iwl_scale_tbl_info *tbl = &lq_sta->lq_info[lq_sta->active_tbl];
2930 if (is_Ht(tbl->lq_type))
2931 desc += sprintf(buff+desc,
2932 "Bit Rate= %d Mb/s\n",
2933 tbl->expected_tpt[lq_sta->last_txrate_idx]);
2935 desc += sprintf(buff+desc,
2936 "Bit Rate= %d Mb/s\n",
2937 iwl_rates[lq_sta->last_txrate_idx].ieee >> 1);
2939 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2943 static const struct file_operations rs_sta_dbgfs_rate_scale_data_ops = {
2944 .read = rs_sta_dbgfs_rate_scale_data_read,
2945 .open = open_file_generic,
2948 static void rs_add_debugfs(void *priv, void *priv_sta,
2951 struct iwl_lq_sta *lq_sta = priv_sta;
2952 lq_sta->rs_sta_dbgfs_scale_table_file =
2953 debugfs_create_file("rate_scale_table", S_IRUSR | S_IWUSR, dir,
2954 lq_sta, &rs_sta_dbgfs_scale_table_ops);
2955 lq_sta->rs_sta_dbgfs_stats_table_file =
2956 debugfs_create_file("rate_stats_table", S_IRUSR, dir,
2957 lq_sta, &rs_sta_dbgfs_stats_table_ops);
2958 lq_sta->rs_sta_dbgfs_rate_scale_data_file =
2959 debugfs_create_file("rate_scale_data", S_IRUSR, dir,
2960 lq_sta, &rs_sta_dbgfs_rate_scale_data_ops);
2961 lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
2962 debugfs_create_u8("tx_agg_tid_enable", S_IRUSR | S_IWUSR, dir,
2963 &lq_sta->tx_agg_tid_en);
2967 static void rs_remove_debugfs(void *priv, void *priv_sta)
2969 struct iwl_lq_sta *lq_sta = priv_sta;
2970 debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
2971 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
2972 debugfs_remove(lq_sta->rs_sta_dbgfs_rate_scale_data_file);
2973 debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
2978 * Initialization of rate scaling information is done by driver after
2979 * the station is added. Since mac80211 calls this function before a
2980 * station is added we ignore it.
2982 static void rs_rate_init_stub(void *priv_r, struct ieee80211_supported_band *sband,
2983 struct ieee80211_sta *sta, void *priv_sta)
2986 static struct rate_control_ops rs_ops = {
2989 .tx_status = rs_tx_status,
2990 .get_rate = rs_get_rate,
2991 .rate_init = rs_rate_init_stub,
2994 .alloc_sta = rs_alloc_sta,
2995 .free_sta = rs_free_sta,
2996 #ifdef CONFIG_MAC80211_DEBUGFS
2997 .add_sta_debugfs = rs_add_debugfs,
2998 .remove_sta_debugfs = rs_remove_debugfs,
3002 int iwlagn_rate_control_register(void)
3004 return ieee80211_rate_control_register(&rs_ops);
3007 void iwlagn_rate_control_unregister(void)
3009 ieee80211_rate_control_unregister(&rs_ops);