mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / drivers / net / wireless / iwlwifi / mvm / rs.c
blob4ffaa3fa153f78e7d14f2ad90223b01146562c50
1 /******************************************************************************
3 * Copyright(c) 2005 - 2013 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
12 * more details.
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 <net/mac80211.h>
32 #include <linux/netdevice.h>
33 #include <linux/etherdevice.h>
34 #include <linux/delay.h>
36 #include <linux/workqueue.h>
37 #include "rs.h"
38 #include "fw-api.h"
39 #include "sta.h"
40 #include "iwl-op-mode.h"
41 #include "mvm.h"
43 #define RS_NAME "iwl-mvm-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_1M_INDEX] = IWL_RATE_6M_INDEX,
60 [IWL_RATE_2M_INDEX] = IWL_RATE_6M_INDEX,
61 [IWL_RATE_5M_INDEX] = IWL_RATE_6M_INDEX,
62 [IWL_RATE_11M_INDEX] = IWL_RATE_6M_INDEX,
63 [IWL_RATE_6M_INDEX] = IWL_RATE_6M_INDEX,
64 [IWL_RATE_9M_INDEX] = IWL_RATE_6M_INDEX,
65 [IWL_RATE_12M_INDEX] = IWL_RATE_9M_INDEX,
66 [IWL_RATE_18M_INDEX] = IWL_RATE_12M_INDEX,
67 [IWL_RATE_24M_INDEX] = IWL_RATE_18M_INDEX,
68 [IWL_RATE_36M_INDEX] = IWL_RATE_24M_INDEX,
69 [IWL_RATE_48M_INDEX] = IWL_RATE_36M_INDEX,
70 [IWL_RATE_54M_INDEX] = IWL_RATE_48M_INDEX,
71 [IWL_RATE_60M_INDEX] = IWL_RATE_54M_INDEX,
74 static const u8 ant_toggle_lookup[] = {
75 [ANT_NONE] = ANT_NONE,
76 [ANT_A] = ANT_B,
77 [ANT_B] = ANT_C,
78 [ANT_AB] = ANT_BC,
79 [ANT_C] = ANT_A,
80 [ANT_AC] = ANT_AB,
81 [ANT_BC] = ANT_AC,
82 [ANT_ABC] = ANT_ABC,
85 #define IWL_DECLARE_RATE_INFO(r, s, rp, rn) \
86 [IWL_RATE_##r##M_INDEX] = { IWL_RATE_##r##M_PLCP, \
87 IWL_RATE_SISO_##s##M_PLCP, \
88 IWL_RATE_MIMO2_##s##M_PLCP,\
89 IWL_RATE_##rp##M_INDEX, \
90 IWL_RATE_##rn##M_INDEX }
93 * Parameter order:
94 * rate, ht rate, prev rate, next rate
96 * If there isn't a valid next or previous rate then INV is used which
97 * maps to IWL_RATE_INVALID
100 static const struct iwl_rs_rate_info iwl_rates[IWL_RATE_COUNT] = {
101 IWL_DECLARE_RATE_INFO(1, INV, INV, 2), /* 1mbps */
102 IWL_DECLARE_RATE_INFO(2, INV, 1, 5), /* 2mbps */
103 IWL_DECLARE_RATE_INFO(5, INV, 2, 11), /*5.5mbps */
104 IWL_DECLARE_RATE_INFO(11, INV, 9, 12), /* 11mbps */
105 IWL_DECLARE_RATE_INFO(6, 6, 5, 11), /* 6mbps */
106 IWL_DECLARE_RATE_INFO(9, 6, 6, 11), /* 9mbps */
107 IWL_DECLARE_RATE_INFO(12, 12, 11, 18), /* 12mbps */
108 IWL_DECLARE_RATE_INFO(18, 18, 12, 24), /* 18mbps */
109 IWL_DECLARE_RATE_INFO(24, 24, 18, 36), /* 24mbps */
110 IWL_DECLARE_RATE_INFO(36, 36, 24, 48), /* 36mbps */
111 IWL_DECLARE_RATE_INFO(48, 48, 36, 54), /* 48mbps */
112 IWL_DECLARE_RATE_INFO(54, 54, 48, INV), /* 54mbps */
113 IWL_DECLARE_RATE_INFO(60, 60, 48, INV), /* 60mbps */
114 /* FIXME:RS: ^^ should be INV (legacy) */
117 static inline u8 rs_extract_rate(u32 rate_n_flags)
119 /* also works for HT because bits 7:6 are zero there */
120 return (u8)(rate_n_flags & RATE_LEGACY_RATE_MSK);
123 static int iwl_hwrate_to_plcp_idx(u32 rate_n_flags)
125 int idx = 0;
127 /* HT rate format */
128 if (rate_n_flags & RATE_MCS_HT_MSK) {
129 idx = rs_extract_rate(rate_n_flags);
131 WARN_ON_ONCE(idx >= IWL_RATE_MIMO3_6M_PLCP);
132 if (idx >= IWL_RATE_MIMO2_6M_PLCP)
133 idx = idx - IWL_RATE_MIMO2_6M_PLCP;
135 idx += IWL_FIRST_OFDM_RATE;
136 /* skip 9M not supported in ht*/
137 if (idx >= IWL_RATE_9M_INDEX)
138 idx += 1;
139 if ((idx >= IWL_FIRST_OFDM_RATE) && (idx <= IWL_LAST_OFDM_RATE))
140 return idx;
142 /* legacy rate format, search for match in table */
143 } else {
144 for (idx = 0; idx < ARRAY_SIZE(iwl_rates); idx++)
145 if (iwl_rates[idx].plcp ==
146 rs_extract_rate(rate_n_flags))
147 return idx;
150 return -1;
153 static void rs_rate_scale_perform(struct iwl_mvm *mvm,
154 struct sk_buff *skb,
155 struct ieee80211_sta *sta,
156 struct iwl_lq_sta *lq_sta);
157 static void rs_fill_link_cmd(struct iwl_mvm *mvm,
158 struct iwl_lq_sta *lq_sta, u32 rate_n_flags);
159 static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search);
162 #ifdef CONFIG_MAC80211_DEBUGFS
163 static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
164 u32 *rate_n_flags);
165 #else
166 static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
167 u32 *rate_n_flags)
169 #endif
172 * The following tables contain the expected throughput metrics for all rates
174 * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
176 * where invalid entries are zeros.
178 * CCK rates are only valid in legacy table and will only be used in G
179 * (2.4 GHz) band.
182 static s32 expected_tpt_legacy[IWL_RATE_COUNT] = {
183 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 0
186 static s32 expected_tpt_siso20MHz[4][IWL_RATE_COUNT] = {
187 {0, 0, 0, 0, 42, 0, 76, 102, 124, 159, 183, 193, 202}, /* Norm */
188 {0, 0, 0, 0, 46, 0, 82, 110, 132, 168, 192, 202, 210}, /* SGI */
189 {0, 0, 0, 0, 47, 0, 91, 133, 171, 242, 305, 334, 362}, /* AGG */
190 {0, 0, 0, 0, 52, 0, 101, 145, 187, 264, 330, 361, 390}, /* AGG+SGI */
193 static s32 expected_tpt_siso40MHz[4][IWL_RATE_COUNT] = {
194 {0, 0, 0, 0, 77, 0, 127, 160, 184, 220, 242, 250, 257}, /* Norm */
195 {0, 0, 0, 0, 83, 0, 135, 169, 193, 229, 250, 257, 264}, /* SGI */
196 {0, 0, 0, 0, 94, 0, 177, 249, 313, 423, 512, 550, 586}, /* AGG */
197 {0, 0, 0, 0, 104, 0, 193, 270, 338, 454, 545, 584, 620}, /* AGG+SGI */
200 static s32 expected_tpt_mimo2_20MHz[4][IWL_RATE_COUNT] = {
201 {0, 0, 0, 0, 74, 0, 123, 155, 179, 214, 236, 244, 251}, /* Norm */
202 {0, 0, 0, 0, 81, 0, 131, 164, 188, 223, 243, 251, 257}, /* SGI */
203 {0, 0, 0, 0, 89, 0, 167, 235, 296, 402, 488, 526, 560}, /* AGG */
204 {0, 0, 0, 0, 97, 0, 182, 255, 320, 431, 520, 558, 593}, /* AGG+SGI*/
207 static s32 expected_tpt_mimo2_40MHz[4][IWL_RATE_COUNT] = {
208 {0, 0, 0, 0, 123, 0, 182, 214, 235, 264, 279, 285, 289}, /* Norm */
209 {0, 0, 0, 0, 131, 0, 191, 222, 242, 270, 284, 289, 293}, /* SGI */
210 {0, 0, 0, 0, 171, 0, 305, 410, 496, 634, 731, 771, 805}, /* AGG */
211 {0, 0, 0, 0, 186, 0, 329, 439, 527, 667, 764, 803, 838}, /* AGG+SGI */
214 /* mbps, mcs */
215 static const struct iwl_rate_mcs_info iwl_rate_mcs[IWL_RATE_COUNT] = {
216 { "1", "BPSK DSSS"},
217 { "2", "QPSK DSSS"},
218 {"5.5", "BPSK CCK"},
219 { "11", "QPSK CCK"},
220 { "6", "BPSK 1/2"},
221 { "9", "BPSK 1/2"},
222 { "12", "QPSK 1/2"},
223 { "18", "QPSK 3/4"},
224 { "24", "16QAM 1/2"},
225 { "36", "16QAM 3/4"},
226 { "48", "64QAM 2/3"},
227 { "54", "64QAM 3/4"},
228 { "60", "64QAM 5/6"},
231 #define MCS_INDEX_PER_STREAM (8)
233 static void rs_rate_scale_clear_window(struct iwl_rate_scale_data *window)
235 window->data = 0;
236 window->success_counter = 0;
237 window->success_ratio = IWL_INVALID_VALUE;
238 window->counter = 0;
239 window->average_tpt = IWL_INVALID_VALUE;
240 window->stamp = 0;
243 static inline u8 rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
245 return (ant_type & valid_antenna) == ant_type;
248 #ifdef CONFIG_MAC80211_DEBUGFS
250 * Program the device to use fixed rate for frame transmit
251 * This is for debugging/testing only
252 * once the device start use fixed rate, we need to reload the module
253 * to being back the normal operation.
255 static void rs_program_fix_rate(struct iwl_mvm *mvm,
256 struct iwl_lq_sta *lq_sta)
258 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
259 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
260 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
262 IWL_DEBUG_RATE(mvm, "sta_id %d rate 0x%X\n",
263 lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
265 if (lq_sta->dbg_fixed_rate) {
266 rs_fill_link_cmd(NULL, lq_sta, lq_sta->dbg_fixed_rate);
267 iwl_mvm_send_lq_cmd(lq_sta->drv, &lq_sta->lq, CMD_ASYNC, false);
270 #endif
272 static int rs_tl_turn_on_agg_for_tid(struct iwl_mvm *mvm,
273 struct iwl_lq_sta *lq_data, u8 tid,
274 struct ieee80211_sta *sta)
276 int ret = -EAGAIN;
279 * Don't create TX aggregation sessions when in high
280 * BT traffic, as they would just be disrupted by BT.
282 if (BT_MBOX_MSG(&mvm->last_bt_notif, 3, TRAFFIC_LOAD) >= 2) {
283 IWL_DEBUG_COEX(mvm, "BT traffic (%d), no aggregation allowed\n",
284 BT_MBOX_MSG(&mvm->last_bt_notif,
285 3, TRAFFIC_LOAD));
286 return ret;
289 IWL_DEBUG_HT(mvm, "Starting Tx agg: STA: %pM tid: %d\n",
290 sta->addr, tid);
291 ret = ieee80211_start_tx_ba_session(sta, tid, 5000);
292 if (ret == -EAGAIN) {
294 * driver and mac80211 is out of sync
295 * this might be cause by reloading firmware
296 * stop the tx ba session here
298 IWL_ERR(mvm, "Fail start Tx agg on tid: %d\n",
299 tid);
300 ieee80211_stop_tx_ba_session(sta, tid);
302 return ret;
305 static void rs_tl_turn_on_agg(struct iwl_mvm *mvm, u8 tid,
306 struct iwl_lq_sta *lq_data,
307 struct ieee80211_sta *sta)
309 if (tid < IWL_MAX_TID_COUNT)
310 rs_tl_turn_on_agg_for_tid(mvm, lq_data, tid, sta);
311 else
312 IWL_ERR(mvm, "tid exceeds max TID count: %d/%d\n",
313 tid, IWL_MAX_TID_COUNT);
316 static inline int get_num_of_ant_from_rate(u32 rate_n_flags)
318 return !!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
319 !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
320 !!(rate_n_flags & RATE_MCS_ANT_C_MSK);
324 * Static function to get the expected throughput from an iwl_scale_tbl_info
325 * that wraps a NULL pointer check
327 static s32 get_expected_tpt(struct iwl_scale_tbl_info *tbl, int rs_index)
329 if (tbl->expected_tpt)
330 return tbl->expected_tpt[rs_index];
331 return 0;
335 * rs_collect_tx_data - Update the success/failure sliding window
337 * We keep a sliding window of the last 62 packets transmitted
338 * at this rate. window->data contains the bitmask of successful
339 * packets.
341 static int rs_collect_tx_data(struct iwl_scale_tbl_info *tbl,
342 int scale_index, int attempts, int successes)
344 struct iwl_rate_scale_data *window = NULL;
345 static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
346 s32 fail_count, tpt;
348 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
349 return -EINVAL;
351 /* Select window for current tx bit rate */
352 window = &(tbl->win[scale_index]);
354 /* Get expected throughput */
355 tpt = get_expected_tpt(tbl, scale_index);
358 * Keep track of only the latest 62 tx frame attempts in this rate's
359 * history window; anything older isn't really relevant any more.
360 * If we have filled up the sliding window, drop the oldest attempt;
361 * if the oldest attempt (highest bit in bitmap) shows "success",
362 * subtract "1" from the success counter (this is the main reason
363 * we keep these bitmaps!).
365 while (attempts > 0) {
366 if (window->counter >= IWL_RATE_MAX_WINDOW) {
367 /* remove earliest */
368 window->counter = IWL_RATE_MAX_WINDOW - 1;
370 if (window->data & mask) {
371 window->data &= ~mask;
372 window->success_counter--;
376 /* Increment frames-attempted counter */
377 window->counter++;
379 /* Shift bitmap by one frame to throw away oldest history */
380 window->data <<= 1;
382 /* Mark the most recent #successes attempts as successful */
383 if (successes > 0) {
384 window->success_counter++;
385 window->data |= 0x1;
386 successes--;
389 attempts--;
392 /* Calculate current success ratio, avoid divide-by-0! */
393 if (window->counter > 0)
394 window->success_ratio = 128 * (100 * window->success_counter)
395 / window->counter;
396 else
397 window->success_ratio = IWL_INVALID_VALUE;
399 fail_count = window->counter - window->success_counter;
401 /* Calculate average throughput, if we have enough history. */
402 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
403 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
404 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
405 else
406 window->average_tpt = IWL_INVALID_VALUE;
408 /* Tag this window as having been updated */
409 window->stamp = jiffies;
411 return 0;
415 * Fill uCode API rate_n_flags field, based on "search" or "active" table.
417 /* FIXME:RS:remove this function and put the flags statically in the table */
418 static u32 rate_n_flags_from_tbl(struct iwl_mvm *mvm,
419 struct iwl_scale_tbl_info *tbl,
420 int index, u8 use_green)
422 u32 rate_n_flags = 0;
424 if (is_legacy(tbl->lq_type)) {
425 rate_n_flags = iwl_rates[index].plcp;
426 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
427 rate_n_flags |= RATE_MCS_CCK_MSK;
428 } else if (is_Ht(tbl->lq_type)) {
429 if (index > IWL_LAST_OFDM_RATE) {
430 IWL_ERR(mvm, "Invalid HT rate index %d\n", index);
431 index = IWL_LAST_OFDM_RATE;
433 rate_n_flags = RATE_MCS_HT_MSK;
435 if (is_siso(tbl->lq_type))
436 rate_n_flags |= iwl_rates[index].plcp_siso;
437 else if (is_mimo2(tbl->lq_type))
438 rate_n_flags |= iwl_rates[index].plcp_mimo2;
439 else
440 WARN_ON_ONCE(1);
441 } else {
442 IWL_ERR(mvm, "Invalid tbl->lq_type %d\n", tbl->lq_type);
445 rate_n_flags |= ((tbl->ant_type << RATE_MCS_ANT_POS) &
446 RATE_MCS_ANT_ABC_MSK);
448 if (is_Ht(tbl->lq_type)) {
449 if (tbl->is_ht40)
450 rate_n_flags |= RATE_MCS_CHAN_WIDTH_40;
451 if (tbl->is_SGI)
452 rate_n_flags |= RATE_MCS_SGI_MSK;
454 if (use_green) {
455 rate_n_flags |= RATE_HT_MCS_GF_MSK;
456 if (is_siso(tbl->lq_type) && tbl->is_SGI) {
457 rate_n_flags &= ~RATE_MCS_SGI_MSK;
458 IWL_ERR(mvm, "GF was set with SGI:SISO\n");
462 return rate_n_flags;
466 * Interpret uCode API's rate_n_flags format,
467 * fill "search" or "active" tx mode table.
469 static int rs_get_tbl_info_from_mcs(const u32 rate_n_flags,
470 enum ieee80211_band band,
471 struct iwl_scale_tbl_info *tbl,
472 int *rate_idx)
474 u32 ant_msk = (rate_n_flags & RATE_MCS_ANT_ABC_MSK);
475 u8 num_of_ant = get_num_of_ant_from_rate(rate_n_flags);
476 u8 mcs;
478 memset(tbl, 0, offsetof(struct iwl_scale_tbl_info, win));
479 *rate_idx = iwl_hwrate_to_plcp_idx(rate_n_flags);
481 if (*rate_idx == IWL_RATE_INVALID) {
482 *rate_idx = -1;
483 return -EINVAL;
485 tbl->is_SGI = 0; /* default legacy setup */
486 tbl->is_ht40 = 0;
487 tbl->ant_type = (ant_msk >> RATE_MCS_ANT_POS);
488 tbl->lq_type = LQ_NONE;
489 tbl->max_search = IWL_MAX_SEARCH;
491 /* legacy rate format */
492 if (!(rate_n_flags & RATE_MCS_HT_MSK)) {
493 if (num_of_ant == 1) {
494 if (band == IEEE80211_BAND_5GHZ)
495 tbl->lq_type = LQ_A;
496 else
497 tbl->lq_type = LQ_G;
499 /* HT rate format */
500 } else {
501 if (rate_n_flags & RATE_MCS_SGI_MSK)
502 tbl->is_SGI = 1;
504 if (rate_n_flags & RATE_MCS_CHAN_WIDTH_40) /* TODO */
505 tbl->is_ht40 = 1;
507 mcs = rs_extract_rate(rate_n_flags);
509 /* SISO */
510 if (mcs <= IWL_RATE_SISO_60M_PLCP) {
511 if (num_of_ant == 1)
512 tbl->lq_type = LQ_SISO; /*else NONE*/
513 /* MIMO2 */
514 } else if (mcs <= IWL_RATE_MIMO2_60M_PLCP) {
515 if (num_of_ant == 2)
516 tbl->lq_type = LQ_MIMO2;
517 } else {
518 WARN_ON_ONCE(num_of_ant == 3);
521 return 0;
524 /* switch to another antenna/antennas and return 1 */
525 /* if no other valid antenna found, return 0 */
526 static int rs_toggle_antenna(u32 valid_ant, u32 *rate_n_flags,
527 struct iwl_scale_tbl_info *tbl)
529 u8 new_ant_type;
531 if (!tbl->ant_type || tbl->ant_type > ANT_ABC)
532 return 0;
534 if (!rs_is_valid_ant(valid_ant, tbl->ant_type))
535 return 0;
537 new_ant_type = ant_toggle_lookup[tbl->ant_type];
539 while ((new_ant_type != tbl->ant_type) &&
540 !rs_is_valid_ant(valid_ant, new_ant_type))
541 new_ant_type = ant_toggle_lookup[new_ant_type];
543 if (new_ant_type == tbl->ant_type)
544 return 0;
546 tbl->ant_type = new_ant_type;
547 *rate_n_flags &= ~RATE_MCS_ANT_ABC_MSK;
548 *rate_n_flags |= new_ant_type << RATE_MCS_ANT_POS;
549 return 1;
553 * Green-field mode is valid if the station supports it and
554 * there are no non-GF stations present in the BSS.
556 static bool rs_use_green(struct ieee80211_sta *sta)
559 * There's a bug somewhere in this code that causes the
560 * scaling to get stuck because GF+SGI can't be combined
561 * in SISO rates. Until we find that bug, disable GF, it
562 * has only limited benefit and we still interoperate with
563 * GF APs since we can always receive GF transmissions.
565 return false;
569 * rs_get_supported_rates - get the available rates
571 * if management frame or broadcast frame only return
572 * basic available rates.
575 static u16 rs_get_supported_rates(struct iwl_lq_sta *lq_sta,
576 struct ieee80211_hdr *hdr,
577 enum iwl_table_type rate_type)
579 if (is_legacy(rate_type)) {
580 return lq_sta->active_legacy_rate;
581 } else {
582 if (is_siso(rate_type))
583 return lq_sta->active_siso_rate;
584 else {
585 WARN_ON_ONCE(!is_mimo2(rate_type));
586 return lq_sta->active_mimo2_rate;
591 static u16 rs_get_adjacent_rate(struct iwl_mvm *mvm, u8 index, u16 rate_mask,
592 int rate_type)
594 u8 high = IWL_RATE_INVALID;
595 u8 low = IWL_RATE_INVALID;
597 /* 802.11A or ht walks to the next literal adjacent rate in
598 * the rate table */
599 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
600 int i;
601 u32 mask;
603 /* Find the previous rate that is in the rate mask */
604 i = index - 1;
605 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
606 if (rate_mask & mask) {
607 low = i;
608 break;
612 /* Find the next rate that is in the rate mask */
613 i = index + 1;
614 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
615 if (rate_mask & mask) {
616 high = i;
617 break;
621 return (high << 8) | low;
624 low = index;
625 while (low != IWL_RATE_INVALID) {
626 low = iwl_rates[low].prev_rs;
627 if (low == IWL_RATE_INVALID)
628 break;
629 if (rate_mask & (1 << low))
630 break;
631 IWL_DEBUG_RATE(mvm, "Skipping masked lower rate: %d\n", low);
634 high = index;
635 while (high != IWL_RATE_INVALID) {
636 high = iwl_rates[high].next_rs;
637 if (high == IWL_RATE_INVALID)
638 break;
639 if (rate_mask & (1 << high))
640 break;
641 IWL_DEBUG_RATE(mvm, "Skipping masked higher rate: %d\n", high);
644 return (high << 8) | low;
647 static u32 rs_get_lower_rate(struct iwl_lq_sta *lq_sta,
648 struct iwl_scale_tbl_info *tbl,
649 u8 scale_index, u8 ht_possible)
651 s32 low;
652 u16 rate_mask;
653 u16 high_low;
654 u8 switch_to_legacy = 0;
655 u8 is_green = lq_sta->is_green;
656 struct iwl_mvm *mvm = lq_sta->drv;
658 /* check if we need to switch from HT to legacy rates.
659 * assumption is that mandatory rates (1Mbps or 6Mbps)
660 * are always supported (spec demand) */
661 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
662 switch_to_legacy = 1;
663 scale_index = rs_ht_to_legacy[scale_index];
664 if (lq_sta->band == IEEE80211_BAND_5GHZ)
665 tbl->lq_type = LQ_A;
666 else
667 tbl->lq_type = LQ_G;
669 if (num_of_ant(tbl->ant_type) > 1)
670 tbl->ant_type =
671 first_antenna(iwl_fw_valid_tx_ant(mvm->fw));
673 tbl->is_ht40 = 0;
674 tbl->is_SGI = 0;
675 tbl->max_search = IWL_MAX_SEARCH;
678 rate_mask = rs_get_supported_rates(lq_sta, NULL, tbl->lq_type);
680 /* Mask with station rate restriction */
681 if (is_legacy(tbl->lq_type)) {
682 /* supp_rates has no CCK bits in A mode */
683 if (lq_sta->band == IEEE80211_BAND_5GHZ)
684 rate_mask = (u16)(rate_mask &
685 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
686 else
687 rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
690 /* If we switched from HT to legacy, check current rate */
691 if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
692 low = scale_index;
693 goto out;
696 high_low = rs_get_adjacent_rate(lq_sta->drv, scale_index, rate_mask,
697 tbl->lq_type);
698 low = high_low & 0xff;
700 if (low == IWL_RATE_INVALID)
701 low = scale_index;
703 out:
704 return rate_n_flags_from_tbl(lq_sta->drv, tbl, low, is_green);
708 * Simple function to compare two rate scale table types
710 static bool table_type_matches(struct iwl_scale_tbl_info *a,
711 struct iwl_scale_tbl_info *b)
713 return (a->lq_type == b->lq_type) && (a->ant_type == b->ant_type) &&
714 (a->is_SGI == b->is_SGI);
718 * mac80211 sends us Tx status
720 static void rs_tx_status(void *mvm_r, struct ieee80211_supported_band *sband,
721 struct ieee80211_sta *sta, void *priv_sta,
722 struct sk_buff *skb)
724 int legacy_success;
725 int retries;
726 int rs_index, mac_index, i;
727 struct iwl_lq_sta *lq_sta = priv_sta;
728 struct iwl_lq_cmd *table;
729 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
730 struct iwl_op_mode *op_mode = (struct iwl_op_mode *)mvm_r;
731 struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
732 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
733 enum mac80211_rate_control_flags mac_flags;
734 u32 tx_rate;
735 struct iwl_scale_tbl_info tbl_type;
736 struct iwl_scale_tbl_info *curr_tbl, *other_tbl, *tmp_tbl;
738 IWL_DEBUG_RATE_LIMIT(mvm,
739 "get frame ack response, update rate scale window\n");
741 /* Treat uninitialized rate scaling data same as non-existing. */
742 if (!lq_sta) {
743 IWL_DEBUG_RATE(mvm, "Station rate scaling not created yet.\n");
744 return;
745 } else if (!lq_sta->drv) {
746 IWL_DEBUG_RATE(mvm, "Rate scaling not initialized yet.\n");
747 return;
750 if (!ieee80211_is_data(hdr->frame_control) ||
751 info->flags & IEEE80211_TX_CTL_NO_ACK)
752 return;
754 /* This packet was aggregated but doesn't carry status info */
755 if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
756 !(info->flags & IEEE80211_TX_STAT_AMPDU))
757 return;
760 * Ignore this Tx frame response if its initial rate doesn't match
761 * that of latest Link Quality command. There may be stragglers
762 * from a previous Link Quality command, but we're no longer interested
763 * in those; they're either from the "active" mode while we're trying
764 * to check "search" mode, or a prior "search" mode after we've moved
765 * to a new "search" mode (which might become the new "active" mode).
767 table = &lq_sta->lq;
768 tx_rate = le32_to_cpu(table->rs_table[0]);
769 rs_get_tbl_info_from_mcs(tx_rate, info->band, &tbl_type, &rs_index);
770 if (info->band == IEEE80211_BAND_5GHZ)
771 rs_index -= IWL_FIRST_OFDM_RATE;
772 mac_flags = info->status.rates[0].flags;
773 mac_index = info->status.rates[0].idx;
774 /* For HT packets, map MCS to PLCP */
775 if (mac_flags & IEEE80211_TX_RC_MCS) {
776 /* Remove # of streams */
777 mac_index &= RATE_HT_MCS_RATE_CODE_MSK;
778 if (mac_index >= (IWL_RATE_9M_INDEX - IWL_FIRST_OFDM_RATE))
779 mac_index++;
781 * mac80211 HT index is always zero-indexed; we need to move
782 * HT OFDM rates after CCK rates in 2.4 GHz band
784 if (info->band == IEEE80211_BAND_2GHZ)
785 mac_index += IWL_FIRST_OFDM_RATE;
787 /* Here we actually compare this rate to the latest LQ command */
788 if ((mac_index < 0) ||
789 (tbl_type.is_SGI != !!(mac_flags & IEEE80211_TX_RC_SHORT_GI)) ||
790 (tbl_type.is_ht40 != !!(mac_flags & IEEE80211_TX_RC_40_MHZ_WIDTH)) ||
791 (tbl_type.ant_type != info->status.antenna) ||
792 (!!(tx_rate & RATE_MCS_HT_MSK) !=
793 !!(mac_flags & IEEE80211_TX_RC_MCS)) ||
794 (!!(tx_rate & RATE_HT_MCS_GF_MSK) !=
795 !!(mac_flags & IEEE80211_TX_RC_GREEN_FIELD)) ||
796 (rs_index != mac_index)) {
797 IWL_DEBUG_RATE(mvm,
798 "initial rate %d does not match %d (0x%x)\n",
799 mac_index, rs_index, tx_rate);
801 * Since rates mis-match, the last LQ command may have failed.
802 * After IWL_MISSED_RATE_MAX mis-matches, resync the uCode with
803 * ... driver.
805 lq_sta->missed_rate_counter++;
806 if (lq_sta->missed_rate_counter > IWL_MISSED_RATE_MAX) {
807 lq_sta->missed_rate_counter = 0;
808 iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, CMD_ASYNC, false);
810 /* Regardless, ignore this status info for outdated rate */
811 return;
812 } else
813 /* Rate did match, so reset the missed_rate_counter */
814 lq_sta->missed_rate_counter = 0;
816 /* Figure out if rate scale algorithm is in active or search table */
817 if (table_type_matches(&tbl_type,
818 &(lq_sta->lq_info[lq_sta->active_tbl]))) {
819 curr_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
820 other_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
821 } else if (table_type_matches(
822 &tbl_type, &lq_sta->lq_info[1 - lq_sta->active_tbl])) {
823 curr_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
824 other_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
825 } else {
826 IWL_DEBUG_RATE(mvm,
827 "Neither active nor search matches tx rate\n");
828 tmp_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
829 IWL_DEBUG_RATE(mvm, "active- lq:%x, ant:%x, SGI:%d\n",
830 tmp_tbl->lq_type, tmp_tbl->ant_type,
831 tmp_tbl->is_SGI);
832 tmp_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
833 IWL_DEBUG_RATE(mvm, "search- lq:%x, ant:%x, SGI:%d\n",
834 tmp_tbl->lq_type, tmp_tbl->ant_type,
835 tmp_tbl->is_SGI);
836 IWL_DEBUG_RATE(mvm, "actual- lq:%x, ant:%x, SGI:%d\n",
837 tbl_type.lq_type, tbl_type.ant_type,
838 tbl_type.is_SGI);
840 * no matching table found, let's by-pass the data collection
841 * and continue to perform rate scale to find the rate table
843 rs_stay_in_table(lq_sta, true);
844 goto done;
848 * Updating the frame history depends on whether packets were
849 * aggregated.
851 * For aggregation, all packets were transmitted at the same rate, the
852 * first index into rate scale table.
854 if (info->flags & IEEE80211_TX_STAT_AMPDU) {
855 tx_rate = le32_to_cpu(table->rs_table[0]);
856 rs_get_tbl_info_from_mcs(tx_rate, info->band, &tbl_type,
857 &rs_index);
858 rs_collect_tx_data(curr_tbl, rs_index,
859 info->status.ampdu_len,
860 info->status.ampdu_ack_len);
862 /* Update success/fail counts if not searching for new mode */
863 if (lq_sta->stay_in_tbl) {
864 lq_sta->total_success += info->status.ampdu_ack_len;
865 lq_sta->total_failed += (info->status.ampdu_len -
866 info->status.ampdu_ack_len);
868 } else {
870 * For legacy, update frame history with for each Tx retry.
872 retries = info->status.rates[0].count - 1;
873 /* HW doesn't send more than 15 retries */
874 retries = min(retries, 15);
876 /* The last transmission may have been successful */
877 legacy_success = !!(info->flags & IEEE80211_TX_STAT_ACK);
878 /* Collect data for each rate used during failed TX attempts */
879 for (i = 0; i <= retries; ++i) {
880 tx_rate = le32_to_cpu(table->rs_table[i]);
881 rs_get_tbl_info_from_mcs(tx_rate, info->band,
882 &tbl_type, &rs_index);
884 * Only collect stats if retried rate is in the same RS
885 * table as active/search.
887 if (table_type_matches(&tbl_type, curr_tbl))
888 tmp_tbl = curr_tbl;
889 else if (table_type_matches(&tbl_type, other_tbl))
890 tmp_tbl = other_tbl;
891 else
892 continue;
893 rs_collect_tx_data(tmp_tbl, rs_index, 1,
894 i < retries ? 0 : legacy_success);
897 /* Update success/fail counts if not searching for new mode */
898 if (lq_sta->stay_in_tbl) {
899 lq_sta->total_success += legacy_success;
900 lq_sta->total_failed += retries + (1 - legacy_success);
903 /* The last TX rate is cached in lq_sta; it's set in if/else above */
904 lq_sta->last_rate_n_flags = tx_rate;
905 done:
906 /* See if there's a better rate or modulation mode to try. */
907 if (sta && sta->supp_rates[sband->band])
908 rs_rate_scale_perform(mvm, skb, sta, lq_sta);
912 * Begin a period of staying with a selected modulation mode.
913 * Set "stay_in_tbl" flag to prevent any mode switches.
914 * Set frame tx success limits according to legacy vs. high-throughput,
915 * and reset overall (spanning all rates) tx success history statistics.
916 * These control how long we stay using same modulation mode before
917 * searching for a new mode.
919 static void rs_set_stay_in_table(struct iwl_mvm *mvm, u8 is_legacy,
920 struct iwl_lq_sta *lq_sta)
922 IWL_DEBUG_RATE(mvm, "we are staying in the same table\n");
923 lq_sta->stay_in_tbl = 1; /* only place this gets set */
924 if (is_legacy) {
925 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
926 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
927 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
928 } else {
929 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
930 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
931 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
933 lq_sta->table_count = 0;
934 lq_sta->total_failed = 0;
935 lq_sta->total_success = 0;
936 lq_sta->flush_timer = jiffies;
937 lq_sta->action_counter = 0;
941 * Find correct throughput table for given mode of modulation
943 static void rs_set_expected_tpt_table(struct iwl_lq_sta *lq_sta,
944 struct iwl_scale_tbl_info *tbl)
946 /* Used to choose among HT tables */
947 s32 (*ht_tbl_pointer)[IWL_RATE_COUNT];
949 /* Check for invalid LQ type */
950 if (WARN_ON_ONCE(!is_legacy(tbl->lq_type) && !is_Ht(tbl->lq_type))) {
951 tbl->expected_tpt = expected_tpt_legacy;
952 return;
955 /* Legacy rates have only one table */
956 if (is_legacy(tbl->lq_type)) {
957 tbl->expected_tpt = expected_tpt_legacy;
958 return;
961 /* Choose among many HT tables depending on number of streams
962 * (SISO/MIMO2), channel width (20/40), SGI, and aggregation
963 * status */
964 if (is_siso(tbl->lq_type) && !tbl->is_ht40)
965 ht_tbl_pointer = expected_tpt_siso20MHz;
966 else if (is_siso(tbl->lq_type))
967 ht_tbl_pointer = expected_tpt_siso40MHz;
968 else if (is_mimo2(tbl->lq_type) && !tbl->is_ht40)
969 ht_tbl_pointer = expected_tpt_mimo2_20MHz;
970 else {
971 WARN_ON_ONCE(!is_mimo2(tbl->lq_type));
972 ht_tbl_pointer = expected_tpt_mimo2_40MHz;
975 if (!tbl->is_SGI && !lq_sta->is_agg) /* Normal */
976 tbl->expected_tpt = ht_tbl_pointer[0];
977 else if (tbl->is_SGI && !lq_sta->is_agg) /* SGI */
978 tbl->expected_tpt = ht_tbl_pointer[1];
979 else if (!tbl->is_SGI && lq_sta->is_agg) /* AGG */
980 tbl->expected_tpt = ht_tbl_pointer[2];
981 else /* AGG+SGI */
982 tbl->expected_tpt = ht_tbl_pointer[3];
986 * Find starting rate for new "search" high-throughput mode of modulation.
987 * Goal is to find lowest expected rate (under perfect conditions) that is
988 * above the current measured throughput of "active" mode, to give new mode
989 * a fair chance to prove itself without too many challenges.
991 * This gets called when transitioning to more aggressive modulation
992 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
993 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
994 * to decrease to match "active" throughput. When moving from MIMO to SISO,
995 * bit rate will typically need to increase, but not if performance was bad.
997 static s32 rs_get_best_rate(struct iwl_mvm *mvm,
998 struct iwl_lq_sta *lq_sta,
999 struct iwl_scale_tbl_info *tbl, /* "search" */
1000 u16 rate_mask, s8 index)
1002 /* "active" values */
1003 struct iwl_scale_tbl_info *active_tbl =
1004 &(lq_sta->lq_info[lq_sta->active_tbl]);
1005 s32 active_sr = active_tbl->win[index].success_ratio;
1006 s32 active_tpt = active_tbl->expected_tpt[index];
1008 /* expected "search" throughput */
1009 s32 *tpt_tbl = tbl->expected_tpt;
1011 s32 new_rate, high, low, start_hi;
1012 u16 high_low;
1013 s8 rate = index;
1015 new_rate = high = low = start_hi = IWL_RATE_INVALID;
1017 while (1) {
1018 high_low = rs_get_adjacent_rate(mvm, rate, rate_mask,
1019 tbl->lq_type);
1021 low = high_low & 0xff;
1022 high = (high_low >> 8) & 0xff;
1025 * Lower the "search" bit rate, to give new "search" mode
1026 * approximately the same throughput as "active" if:
1028 * 1) "Active" mode has been working modestly well (but not
1029 * great), and expected "search" throughput (under perfect
1030 * conditions) at candidate rate is above the actual
1031 * measured "active" throughput (but less than expected
1032 * "active" throughput under perfect conditions).
1033 * OR
1034 * 2) "Active" mode has been working perfectly or very well
1035 * and expected "search" throughput (under perfect
1036 * conditions) at candidate rate is above expected
1037 * "active" throughput (under perfect conditions).
1039 if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
1040 ((active_sr > IWL_RATE_DECREASE_TH) &&
1041 (active_sr <= IWL_RATE_HIGH_TH) &&
1042 (tpt_tbl[rate] <= active_tpt))) ||
1043 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
1044 (tpt_tbl[rate] > active_tpt))) {
1045 /* (2nd or later pass)
1046 * If we've already tried to raise the rate, and are
1047 * now trying to lower it, use the higher rate. */
1048 if (start_hi != IWL_RATE_INVALID) {
1049 new_rate = start_hi;
1050 break;
1053 new_rate = rate;
1055 /* Loop again with lower rate */
1056 if (low != IWL_RATE_INVALID)
1057 rate = low;
1059 /* Lower rate not available, use the original */
1060 else
1061 break;
1063 /* Else try to raise the "search" rate to match "active" */
1064 } else {
1065 /* (2nd or later pass)
1066 * If we've already tried to lower the rate, and are
1067 * now trying to raise it, use the lower rate. */
1068 if (new_rate != IWL_RATE_INVALID)
1069 break;
1071 /* Loop again with higher rate */
1072 else if (high != IWL_RATE_INVALID) {
1073 start_hi = high;
1074 rate = high;
1076 /* Higher rate not available, use the original */
1077 } else {
1078 new_rate = rate;
1079 break;
1084 return new_rate;
1087 static bool iwl_is_ht40_tx_allowed(struct ieee80211_sta *sta)
1089 return sta->bandwidth >= IEEE80211_STA_RX_BW_40;
1093 * Set up search table for MIMO2
1095 static int rs_switch_to_mimo2(struct iwl_mvm *mvm,
1096 struct iwl_lq_sta *lq_sta,
1097 struct ieee80211_sta *sta,
1098 struct iwl_scale_tbl_info *tbl, int index)
1100 u16 rate_mask;
1101 s32 rate;
1102 s8 is_green = lq_sta->is_green;
1104 if (!sta->ht_cap.ht_supported)
1105 return -1;
1107 if (sta->smps_mode == IEEE80211_SMPS_STATIC)
1108 return -1;
1110 /* Need both Tx chains/antennas to support MIMO */
1111 if (num_of_ant(iwl_fw_valid_tx_ant(mvm->fw)) < 2)
1112 return -1;
1114 IWL_DEBUG_RATE(mvm, "LQ: try to switch to MIMO2\n");
1116 tbl->lq_type = LQ_MIMO2;
1117 tbl->action = 0;
1118 tbl->max_search = IWL_MAX_SEARCH;
1119 rate_mask = lq_sta->active_mimo2_rate;
1121 if (iwl_is_ht40_tx_allowed(sta))
1122 tbl->is_ht40 = 1;
1123 else
1124 tbl->is_ht40 = 0;
1126 rs_set_expected_tpt_table(lq_sta, tbl);
1128 rate = rs_get_best_rate(mvm, lq_sta, tbl, rate_mask, index);
1130 IWL_DEBUG_RATE(mvm, "LQ: MIMO2 best rate %d mask %X\n",
1131 rate, rate_mask);
1132 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1133 IWL_DEBUG_RATE(mvm, "Can't switch with index %d rate mask %x\n",
1134 rate, rate_mask);
1135 return -1;
1137 tbl->current_rate = rate_n_flags_from_tbl(mvm, tbl, rate, is_green);
1139 IWL_DEBUG_RATE(mvm, "LQ: Switch to new mcs %X index is green %X\n",
1140 tbl->current_rate, is_green);
1141 return 0;
1145 * Set up search table for SISO
1147 static int rs_switch_to_siso(struct iwl_mvm *mvm,
1148 struct iwl_lq_sta *lq_sta,
1149 struct ieee80211_sta *sta,
1150 struct iwl_scale_tbl_info *tbl, int index)
1152 u16 rate_mask;
1153 u8 is_green = lq_sta->is_green;
1154 s32 rate;
1156 if (!sta->ht_cap.ht_supported)
1157 return -1;
1159 IWL_DEBUG_RATE(mvm, "LQ: try to switch to SISO\n");
1161 tbl->lq_type = LQ_SISO;
1162 tbl->action = 0;
1163 tbl->max_search = IWL_MAX_SEARCH;
1164 rate_mask = lq_sta->active_siso_rate;
1166 if (iwl_is_ht40_tx_allowed(sta))
1167 tbl->is_ht40 = 1;
1168 else
1169 tbl->is_ht40 = 0;
1171 if (is_green)
1172 tbl->is_SGI = 0; /*11n spec: no SGI in SISO+Greenfield*/
1174 rs_set_expected_tpt_table(lq_sta, tbl);
1175 rate = rs_get_best_rate(mvm, lq_sta, tbl, rate_mask, index);
1177 IWL_DEBUG_RATE(mvm, "LQ: get best rate %d mask %X\n", rate, rate_mask);
1178 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1179 IWL_DEBUG_RATE(mvm,
1180 "can not switch with index %d rate mask %x\n",
1181 rate, rate_mask);
1182 return -1;
1184 tbl->current_rate = rate_n_flags_from_tbl(mvm, tbl, rate, is_green);
1185 IWL_DEBUG_RATE(mvm, "LQ: Switch to new mcs %X index is green %X\n",
1186 tbl->current_rate, is_green);
1187 return 0;
1191 * Try to switch to new modulation mode from legacy
1193 static int rs_move_legacy_other(struct iwl_mvm *mvm,
1194 struct iwl_lq_sta *lq_sta,
1195 struct ieee80211_sta *sta,
1196 int index)
1198 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1199 struct iwl_scale_tbl_info *search_tbl =
1200 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1201 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1202 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1203 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1204 u8 start_action;
1205 u8 valid_tx_ant = iwl_fw_valid_tx_ant(mvm->fw);
1206 u8 tx_chains_num = num_of_ant(valid_tx_ant);
1207 int ret;
1208 u8 update_search_tbl_counter = 0;
1210 start_action = tbl->action;
1211 while (1) {
1212 lq_sta->action_counter++;
1213 switch (tbl->action) {
1214 case IWL_LEGACY_SWITCH_ANTENNA1:
1215 case IWL_LEGACY_SWITCH_ANTENNA2:
1216 IWL_DEBUG_RATE(mvm, "LQ: Legacy toggle Antenna\n");
1218 if ((tbl->action == IWL_LEGACY_SWITCH_ANTENNA1 &&
1219 tx_chains_num <= 1) ||
1220 (tbl->action == IWL_LEGACY_SWITCH_ANTENNA2 &&
1221 tx_chains_num <= 2))
1222 break;
1224 /* Don't change antenna if success has been great */
1225 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1226 break;
1228 /* Set up search table to try other antenna */
1229 memcpy(search_tbl, tbl, sz);
1231 if (rs_toggle_antenna(valid_tx_ant,
1232 &search_tbl->current_rate,
1233 search_tbl)) {
1234 update_search_tbl_counter = 1;
1235 rs_set_expected_tpt_table(lq_sta, search_tbl);
1236 goto out;
1238 break;
1239 case IWL_LEGACY_SWITCH_SISO:
1240 IWL_DEBUG_RATE(mvm, "LQ: Legacy switch to SISO\n");
1242 /* Set up search table to try SISO */
1243 memcpy(search_tbl, tbl, sz);
1244 search_tbl->is_SGI = 0;
1245 ret = rs_switch_to_siso(mvm, lq_sta, sta,
1246 search_tbl, index);
1247 if (!ret) {
1248 lq_sta->action_counter = 0;
1249 goto out;
1252 break;
1253 case IWL_LEGACY_SWITCH_MIMO2:
1254 IWL_DEBUG_RATE(mvm, "LQ: Legacy switch to MIMO2\n");
1256 /* Set up search table to try MIMO */
1257 memcpy(search_tbl, tbl, sz);
1258 search_tbl->is_SGI = 0;
1260 search_tbl->ant_type = ANT_AB;
1262 if (!rs_is_valid_ant(valid_tx_ant,
1263 search_tbl->ant_type))
1264 break;
1266 ret = rs_switch_to_mimo2(mvm, lq_sta, sta,
1267 search_tbl, index);
1268 if (!ret) {
1269 lq_sta->action_counter = 0;
1270 goto out;
1272 break;
1273 default:
1274 WARN_ON_ONCE(1);
1276 tbl->action++;
1277 if (tbl->action > IWL_LEGACY_SWITCH_MIMO2)
1278 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
1280 if (tbl->action == start_action)
1281 break;
1283 search_tbl->lq_type = LQ_NONE;
1284 return 0;
1286 out:
1287 lq_sta->search_better_tbl = 1;
1288 tbl->action++;
1289 if (tbl->action > IWL_LEGACY_SWITCH_MIMO2)
1290 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
1291 if (update_search_tbl_counter)
1292 search_tbl->action = tbl->action;
1293 return 0;
1297 * Try to switch to new modulation mode from SISO
1299 static int rs_move_siso_to_other(struct iwl_mvm *mvm,
1300 struct iwl_lq_sta *lq_sta,
1301 struct ieee80211_sta *sta, int index)
1303 u8 is_green = lq_sta->is_green;
1304 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1305 struct iwl_scale_tbl_info *search_tbl =
1306 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1307 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1308 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
1309 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1310 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1311 u8 start_action;
1312 u8 valid_tx_ant = iwl_fw_valid_tx_ant(mvm->fw);
1313 u8 tx_chains_num = num_of_ant(valid_tx_ant);
1314 u8 update_search_tbl_counter = 0;
1315 int ret;
1317 switch (BT_MBOX_MSG(&mvm->last_bt_notif, 3, TRAFFIC_LOAD)) {
1318 case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
1319 /* nothing */
1320 break;
1321 case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
1322 /* avoid antenna B unless MIMO */
1323 if (tbl->action == IWL_SISO_SWITCH_ANTENNA2)
1324 tbl->action = IWL_SISO_SWITCH_MIMO2;
1325 break;
1326 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
1327 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
1328 /* avoid antenna B and MIMO */
1329 valid_tx_ant =
1330 first_antenna(iwl_fw_valid_tx_ant(mvm->fw));
1331 if (tbl->action != IWL_SISO_SWITCH_ANTENNA1)
1332 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1333 break;
1334 default:
1335 IWL_ERR(mvm, "Invalid BT load %d",
1336 BT_MBOX_MSG(&mvm->last_bt_notif, 3, TRAFFIC_LOAD));
1337 break;
1340 start_action = tbl->action;
1341 while (1) {
1342 lq_sta->action_counter++;
1343 switch (tbl->action) {
1344 case IWL_SISO_SWITCH_ANTENNA1:
1345 case IWL_SISO_SWITCH_ANTENNA2:
1346 IWL_DEBUG_RATE(mvm, "LQ: SISO toggle Antenna\n");
1347 if ((tbl->action == IWL_SISO_SWITCH_ANTENNA1 &&
1348 tx_chains_num <= 1) ||
1349 (tbl->action == IWL_SISO_SWITCH_ANTENNA2 &&
1350 tx_chains_num <= 2))
1351 break;
1353 if (window->success_ratio >= IWL_RS_GOOD_RATIO &&
1354 BT_MBOX_MSG(&mvm->last_bt_notif, 3,
1355 TRAFFIC_LOAD) == 0)
1356 break;
1358 memcpy(search_tbl, tbl, sz);
1359 if (rs_toggle_antenna(valid_tx_ant,
1360 &search_tbl->current_rate,
1361 search_tbl)) {
1362 update_search_tbl_counter = 1;
1363 goto out;
1365 break;
1366 case IWL_SISO_SWITCH_MIMO2:
1367 IWL_DEBUG_RATE(mvm, "LQ: SISO switch to MIMO2\n");
1368 memcpy(search_tbl, tbl, sz);
1369 search_tbl->is_SGI = 0;
1371 search_tbl->ant_type = ANT_AB;
1373 if (!rs_is_valid_ant(valid_tx_ant,
1374 search_tbl->ant_type))
1375 break;
1377 ret = rs_switch_to_mimo2(mvm, lq_sta, sta,
1378 search_tbl, index);
1379 if (!ret)
1380 goto out;
1381 break;
1382 case IWL_SISO_SWITCH_GI:
1383 if (!tbl->is_ht40 && !(ht_cap->cap &
1384 IEEE80211_HT_CAP_SGI_20))
1385 break;
1386 if (tbl->is_ht40 && !(ht_cap->cap &
1387 IEEE80211_HT_CAP_SGI_40))
1388 break;
1390 IWL_DEBUG_RATE(mvm, "LQ: SISO toggle SGI/NGI\n");
1392 memcpy(search_tbl, tbl, sz);
1393 if (is_green) {
1394 if (!tbl->is_SGI)
1395 break;
1396 else
1397 IWL_ERR(mvm,
1398 "SGI was set in GF+SISO\n");
1400 search_tbl->is_SGI = !tbl->is_SGI;
1401 rs_set_expected_tpt_table(lq_sta, search_tbl);
1402 if (tbl->is_SGI) {
1403 s32 tpt = lq_sta->last_tpt / 100;
1404 if (tpt >= search_tbl->expected_tpt[index])
1405 break;
1407 search_tbl->current_rate =
1408 rate_n_flags_from_tbl(mvm, search_tbl,
1409 index, is_green);
1410 update_search_tbl_counter = 1;
1411 goto out;
1412 default:
1413 WARN_ON_ONCE(1);
1415 tbl->action++;
1416 if (tbl->action > IWL_SISO_SWITCH_GI)
1417 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1419 if (tbl->action == start_action)
1420 break;
1422 search_tbl->lq_type = LQ_NONE;
1423 return 0;
1425 out:
1426 lq_sta->search_better_tbl = 1;
1427 tbl->action++;
1428 if (tbl->action > IWL_SISO_SWITCH_GI)
1429 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1430 if (update_search_tbl_counter)
1431 search_tbl->action = tbl->action;
1433 return 0;
1437 * Try to switch to new modulation mode from MIMO2
1439 static int rs_move_mimo2_to_other(struct iwl_mvm *mvm,
1440 struct iwl_lq_sta *lq_sta,
1441 struct ieee80211_sta *sta, int index)
1443 s8 is_green = lq_sta->is_green;
1444 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1445 struct iwl_scale_tbl_info *search_tbl =
1446 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1447 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1448 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
1449 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1450 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1451 u8 start_action;
1452 u8 valid_tx_ant = iwl_fw_valid_tx_ant(mvm->fw);
1453 u8 tx_chains_num = num_of_ant(valid_tx_ant);
1454 u8 update_search_tbl_counter = 0;
1455 int ret;
1457 switch (BT_MBOX_MSG(&mvm->last_bt_notif, 3, TRAFFIC_LOAD)) {
1458 case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
1459 /* nothing */
1460 break;
1461 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
1462 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
1463 /* avoid antenna B and MIMO */
1464 if (tbl->action != IWL_MIMO2_SWITCH_SISO_A)
1465 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1466 break;
1467 case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
1468 /* avoid antenna B unless MIMO */
1469 if (tbl->action == IWL_MIMO2_SWITCH_SISO_B)
1470 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1471 break;
1472 default:
1473 IWL_ERR(mvm, "Invalid BT load %d",
1474 BT_MBOX_MSG(&mvm->last_bt_notif, 3, TRAFFIC_LOAD));
1475 break;
1478 start_action = tbl->action;
1479 while (1) {
1480 lq_sta->action_counter++;
1481 switch (tbl->action) {
1482 case IWL_MIMO2_SWITCH_ANTENNA1:
1483 case IWL_MIMO2_SWITCH_ANTENNA2:
1484 IWL_DEBUG_RATE(mvm, "LQ: MIMO2 toggle Antennas\n");
1486 if (tx_chains_num <= 2)
1487 break;
1489 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1490 break;
1492 memcpy(search_tbl, tbl, sz);
1493 if (rs_toggle_antenna(valid_tx_ant,
1494 &search_tbl->current_rate,
1495 search_tbl)) {
1496 update_search_tbl_counter = 1;
1497 goto out;
1499 break;
1500 case IWL_MIMO2_SWITCH_SISO_A:
1501 case IWL_MIMO2_SWITCH_SISO_B:
1502 IWL_DEBUG_RATE(mvm, "LQ: MIMO2 switch to SISO\n");
1504 /* Set up new search table for SISO */
1505 memcpy(search_tbl, tbl, sz);
1507 if (tbl->action == IWL_MIMO2_SWITCH_SISO_A)
1508 search_tbl->ant_type = ANT_A;
1509 else /* tbl->action == IWL_MIMO2_SWITCH_SISO_B */
1510 search_tbl->ant_type = ANT_B;
1512 if (!rs_is_valid_ant(valid_tx_ant,
1513 search_tbl->ant_type))
1514 break;
1516 ret = rs_switch_to_siso(mvm, lq_sta, sta,
1517 search_tbl, index);
1518 if (!ret)
1519 goto out;
1521 break;
1523 case IWL_MIMO2_SWITCH_GI:
1524 if (!tbl->is_ht40 && !(ht_cap->cap &
1525 IEEE80211_HT_CAP_SGI_20))
1526 break;
1527 if (tbl->is_ht40 && !(ht_cap->cap &
1528 IEEE80211_HT_CAP_SGI_40))
1529 break;
1531 IWL_DEBUG_RATE(mvm, "LQ: MIMO2 toggle SGI/NGI\n");
1533 /* Set up new search table for MIMO2 */
1534 memcpy(search_tbl, tbl, sz);
1535 search_tbl->is_SGI = !tbl->is_SGI;
1536 rs_set_expected_tpt_table(lq_sta, search_tbl);
1538 * If active table already uses the fastest possible
1539 * modulation (dual stream with short guard interval),
1540 * and it's working well, there's no need to look
1541 * for a better type of modulation!
1543 if (tbl->is_SGI) {
1544 s32 tpt = lq_sta->last_tpt / 100;
1545 if (tpt >= search_tbl->expected_tpt[index])
1546 break;
1548 search_tbl->current_rate =
1549 rate_n_flags_from_tbl(mvm, search_tbl,
1550 index, is_green);
1551 update_search_tbl_counter = 1;
1552 goto out;
1553 default:
1554 WARN_ON_ONCE(1);
1556 tbl->action++;
1557 if (tbl->action > IWL_MIMO2_SWITCH_GI)
1558 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
1560 if (tbl->action == start_action)
1561 break;
1563 search_tbl->lq_type = LQ_NONE;
1564 return 0;
1565 out:
1566 lq_sta->search_better_tbl = 1;
1567 tbl->action++;
1568 if (tbl->action > IWL_MIMO2_SWITCH_GI)
1569 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
1570 if (update_search_tbl_counter)
1571 search_tbl->action = tbl->action;
1573 return 0;
1577 * Check whether we should continue using same modulation mode, or
1578 * begin search for a new mode, based on:
1579 * 1) # tx successes or failures while using this mode
1580 * 2) # times calling this function
1581 * 3) elapsed time in this mode (not used, for now)
1583 static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search)
1585 struct iwl_scale_tbl_info *tbl;
1586 int i;
1587 int active_tbl;
1588 int flush_interval_passed = 0;
1589 struct iwl_mvm *mvm;
1591 mvm = lq_sta->drv;
1592 active_tbl = lq_sta->active_tbl;
1594 tbl = &(lq_sta->lq_info[active_tbl]);
1596 /* If we've been disallowing search, see if we should now allow it */
1597 if (lq_sta->stay_in_tbl) {
1598 /* Elapsed time using current modulation mode */
1599 if (lq_sta->flush_timer)
1600 flush_interval_passed =
1601 time_after(jiffies,
1602 (unsigned long)(lq_sta->flush_timer +
1603 IWL_RATE_SCALE_FLUSH_INTVL));
1606 * Check if we should allow search for new modulation mode.
1607 * If many frames have failed or succeeded, or we've used
1608 * this same modulation for a long time, allow search, and
1609 * reset history stats that keep track of whether we should
1610 * allow a new search. Also (below) reset all bitmaps and
1611 * stats in active history.
1613 if (force_search ||
1614 (lq_sta->total_failed > lq_sta->max_failure_limit) ||
1615 (lq_sta->total_success > lq_sta->max_success_limit) ||
1616 ((!lq_sta->search_better_tbl) &&
1617 (lq_sta->flush_timer) && (flush_interval_passed))) {
1618 IWL_DEBUG_RATE(mvm,
1619 "LQ: stay is expired %d %d %d\n",
1620 lq_sta->total_failed,
1621 lq_sta->total_success,
1622 flush_interval_passed);
1624 /* Allow search for new mode */
1625 lq_sta->stay_in_tbl = 0; /* only place reset */
1626 lq_sta->total_failed = 0;
1627 lq_sta->total_success = 0;
1628 lq_sta->flush_timer = 0;
1630 * Else if we've used this modulation mode enough repetitions
1631 * (regardless of elapsed time or success/failure), reset
1632 * history bitmaps and rate-specific stats for all rates in
1633 * active table.
1635 } else {
1636 lq_sta->table_count++;
1637 if (lq_sta->table_count >=
1638 lq_sta->table_count_limit) {
1639 lq_sta->table_count = 0;
1641 IWL_DEBUG_RATE(mvm,
1642 "LQ: stay in table clear win\n");
1643 for (i = 0; i < IWL_RATE_COUNT; i++)
1644 rs_rate_scale_clear_window(
1645 &(tbl->win[i]));
1649 /* If transitioning to allow "search", reset all history
1650 * bitmaps and stats in active table (this will become the new
1651 * "search" table). */
1652 if (!lq_sta->stay_in_tbl) {
1653 for (i = 0; i < IWL_RATE_COUNT; i++)
1654 rs_rate_scale_clear_window(&(tbl->win[i]));
1660 * setup rate table in uCode
1662 static void rs_update_rate_tbl(struct iwl_mvm *mvm,
1663 struct iwl_lq_sta *lq_sta,
1664 struct iwl_scale_tbl_info *tbl,
1665 int index, u8 is_green)
1667 u32 rate;
1669 /* Update uCode's rate table. */
1670 rate = rate_n_flags_from_tbl(mvm, tbl, index, is_green);
1671 rs_fill_link_cmd(mvm, lq_sta, rate);
1672 iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, CMD_ASYNC, false);
1675 static u8 rs_get_tid(struct iwl_lq_sta *lq_data,
1676 struct ieee80211_hdr *hdr)
1678 u8 tid = IWL_MAX_TID_COUNT;
1680 if (ieee80211_is_data_qos(hdr->frame_control)) {
1681 u8 *qc = ieee80211_get_qos_ctl(hdr);
1682 tid = qc[0] & 0xf;
1685 if (unlikely(tid > IWL_MAX_TID_COUNT))
1686 tid = IWL_MAX_TID_COUNT;
1688 return tid;
1692 * Do rate scaling and search for new modulation mode.
1694 static void rs_rate_scale_perform(struct iwl_mvm *mvm,
1695 struct sk_buff *skb,
1696 struct ieee80211_sta *sta,
1697 struct iwl_lq_sta *lq_sta)
1699 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1700 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1701 int low = IWL_RATE_INVALID;
1702 int high = IWL_RATE_INVALID;
1703 int index;
1704 int i;
1705 struct iwl_rate_scale_data *window = NULL;
1706 int current_tpt = IWL_INVALID_VALUE;
1707 int low_tpt = IWL_INVALID_VALUE;
1708 int high_tpt = IWL_INVALID_VALUE;
1709 u32 fail_count;
1710 s8 scale_action = 0;
1711 u16 rate_mask;
1712 u8 update_lq = 0;
1713 struct iwl_scale_tbl_info *tbl, *tbl1;
1714 u16 rate_scale_index_msk = 0;
1715 u8 is_green = 0;
1716 u8 active_tbl = 0;
1717 u8 done_search = 0;
1718 u16 high_low;
1719 s32 sr;
1720 u8 tid = IWL_MAX_TID_COUNT;
1721 struct iwl_mvm_sta *sta_priv = (void *)sta->drv_priv;
1722 struct iwl_mvm_tid_data *tid_data;
1724 IWL_DEBUG_RATE(mvm, "rate scale calculate new rate for skb\n");
1726 /* Send management frames and NO_ACK data using lowest rate. */
1727 /* TODO: this could probably be improved.. */
1728 if (!ieee80211_is_data(hdr->frame_control) ||
1729 info->flags & IEEE80211_TX_CTL_NO_ACK)
1730 return;
1732 lq_sta->supp_rates = sta->supp_rates[lq_sta->band];
1734 tid = rs_get_tid(lq_sta, hdr);
1735 if ((tid != IWL_MAX_TID_COUNT) &&
1736 (lq_sta->tx_agg_tid_en & (1 << tid))) {
1737 tid_data = &sta_priv->tid_data[tid];
1738 if (tid_data->state == IWL_AGG_OFF)
1739 lq_sta->is_agg = 0;
1740 else
1741 lq_sta->is_agg = 1;
1742 } else {
1743 lq_sta->is_agg = 0;
1747 * Select rate-scale / modulation-mode table to work with in
1748 * the rest of this function: "search" if searching for better
1749 * modulation mode, or "active" if doing rate scaling within a mode.
1751 if (!lq_sta->search_better_tbl)
1752 active_tbl = lq_sta->active_tbl;
1753 else
1754 active_tbl = 1 - lq_sta->active_tbl;
1756 tbl = &(lq_sta->lq_info[active_tbl]);
1757 if (is_legacy(tbl->lq_type))
1758 lq_sta->is_green = 0;
1759 else
1760 lq_sta->is_green = rs_use_green(sta);
1761 is_green = lq_sta->is_green;
1763 /* current tx rate */
1764 index = lq_sta->last_txrate_idx;
1766 IWL_DEBUG_RATE(mvm, "Rate scale index %d for type %d\n", index,
1767 tbl->lq_type);
1769 /* rates available for this association, and for modulation mode */
1770 rate_mask = rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
1772 IWL_DEBUG_RATE(mvm, "mask 0x%04X\n", rate_mask);
1774 /* mask with station rate restriction */
1775 if (is_legacy(tbl->lq_type)) {
1776 if (lq_sta->band == IEEE80211_BAND_5GHZ)
1777 /* supp_rates has no CCK bits in A mode */
1778 rate_scale_index_msk = (u16) (rate_mask &
1779 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
1780 else
1781 rate_scale_index_msk = (u16) (rate_mask &
1782 lq_sta->supp_rates);
1784 } else {
1785 rate_scale_index_msk = rate_mask;
1788 if (!rate_scale_index_msk)
1789 rate_scale_index_msk = rate_mask;
1791 if (!((1 << index) & rate_scale_index_msk)) {
1792 IWL_ERR(mvm, "Current Rate is not valid\n");
1793 if (lq_sta->search_better_tbl) {
1794 /* revert to active table if search table is not valid*/
1795 tbl->lq_type = LQ_NONE;
1796 lq_sta->search_better_tbl = 0;
1797 tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1798 /* get "active" rate info */
1799 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
1800 rs_update_rate_tbl(mvm, lq_sta, tbl, index, is_green);
1802 return;
1805 /* Get expected throughput table and history window for current rate */
1806 if (!tbl->expected_tpt) {
1807 IWL_ERR(mvm, "tbl->expected_tpt is NULL\n");
1808 return;
1811 /* force user max rate if set by user */
1812 if ((lq_sta->max_rate_idx != -1) &&
1813 (lq_sta->max_rate_idx < index)) {
1814 index = lq_sta->max_rate_idx;
1815 update_lq = 1;
1816 window = &(tbl->win[index]);
1817 goto lq_update;
1820 window = &(tbl->win[index]);
1823 * If there is not enough history to calculate actual average
1824 * throughput, keep analyzing results of more tx frames, without
1825 * changing rate or mode (bypass most of the rest of this function).
1826 * Set up new rate table in uCode only if old rate is not supported
1827 * in current association (use new rate found above).
1829 fail_count = window->counter - window->success_counter;
1830 if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
1831 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
1832 IWL_DEBUG_RATE(mvm,
1833 "LQ: still below TH. succ=%d total=%d for index %d\n",
1834 window->success_counter, window->counter, index);
1836 /* Can't calculate this yet; not enough history */
1837 window->average_tpt = IWL_INVALID_VALUE;
1839 /* Should we stay with this modulation mode,
1840 * or search for a new one? */
1841 rs_stay_in_table(lq_sta, false);
1843 goto out;
1845 /* Else we have enough samples; calculate estimate of
1846 * actual average throughput */
1847 if (window->average_tpt != ((window->success_ratio *
1848 tbl->expected_tpt[index] + 64) / 128)) {
1849 IWL_ERR(mvm,
1850 "expected_tpt should have been calculated by now\n");
1851 window->average_tpt = ((window->success_ratio *
1852 tbl->expected_tpt[index] + 64) / 128);
1855 /* If we are searching for better modulation mode, check success. */
1856 if (lq_sta->search_better_tbl) {
1857 /* If good success, continue using the "search" mode;
1858 * no need to send new link quality command, since we're
1859 * continuing to use the setup that we've been trying. */
1860 if (window->average_tpt > lq_sta->last_tpt) {
1861 IWL_DEBUG_RATE(mvm,
1862 "LQ: SWITCHING TO NEW TABLE suc=%d cur-tpt=%d old-tpt=%d\n",
1863 window->success_ratio,
1864 window->average_tpt,
1865 lq_sta->last_tpt);
1867 if (!is_legacy(tbl->lq_type))
1868 lq_sta->enable_counter = 1;
1870 /* Swap tables; "search" becomes "active" */
1871 lq_sta->active_tbl = active_tbl;
1872 current_tpt = window->average_tpt;
1873 /* Else poor success; go back to mode in "active" table */
1874 } else {
1875 IWL_DEBUG_RATE(mvm,
1876 "LQ: GOING BACK TO THE OLD TABLE suc=%d cur-tpt=%d old-tpt=%d\n",
1877 window->success_ratio,
1878 window->average_tpt,
1879 lq_sta->last_tpt);
1881 /* Nullify "search" table */
1882 tbl->lq_type = LQ_NONE;
1884 /* Revert to "active" table */
1885 active_tbl = lq_sta->active_tbl;
1886 tbl = &(lq_sta->lq_info[active_tbl]);
1888 /* Revert to "active" rate and throughput info */
1889 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
1890 current_tpt = lq_sta->last_tpt;
1892 /* Need to set up a new rate table in uCode */
1893 update_lq = 1;
1896 /* Either way, we've made a decision; modulation mode
1897 * search is done, allow rate adjustment next time. */
1898 lq_sta->search_better_tbl = 0;
1899 done_search = 1; /* Don't switch modes below! */
1900 goto lq_update;
1903 /* (Else) not in search of better modulation mode, try for better
1904 * starting rate, while staying in this mode. */
1905 high_low = rs_get_adjacent_rate(mvm, index, rate_scale_index_msk,
1906 tbl->lq_type);
1907 low = high_low & 0xff;
1908 high = (high_low >> 8) & 0xff;
1910 /* If user set max rate, dont allow higher than user constrain */
1911 if ((lq_sta->max_rate_idx != -1) &&
1912 (lq_sta->max_rate_idx < high))
1913 high = IWL_RATE_INVALID;
1915 sr = window->success_ratio;
1917 /* Collect measured throughputs for current and adjacent rates */
1918 current_tpt = window->average_tpt;
1919 if (low != IWL_RATE_INVALID)
1920 low_tpt = tbl->win[low].average_tpt;
1921 if (high != IWL_RATE_INVALID)
1922 high_tpt = tbl->win[high].average_tpt;
1924 scale_action = 0;
1926 /* Too many failures, decrease rate */
1927 if ((sr <= IWL_RATE_DECREASE_TH) || (current_tpt == 0)) {
1928 IWL_DEBUG_RATE(mvm,
1929 "decrease rate because of low success_ratio\n");
1930 scale_action = -1;
1931 /* No throughput measured yet for adjacent rates; try increase. */
1932 } else if ((low_tpt == IWL_INVALID_VALUE) &&
1933 (high_tpt == IWL_INVALID_VALUE)) {
1934 if (high != IWL_RATE_INVALID && sr >= IWL_RATE_INCREASE_TH)
1935 scale_action = 1;
1936 else if (low != IWL_RATE_INVALID)
1937 scale_action = 0;
1940 /* Both adjacent throughputs are measured, but neither one has better
1941 * throughput; we're using the best rate, don't change it! */
1942 else if ((low_tpt != IWL_INVALID_VALUE) &&
1943 (high_tpt != IWL_INVALID_VALUE) &&
1944 (low_tpt < current_tpt) &&
1945 (high_tpt < current_tpt))
1946 scale_action = 0;
1948 /* At least one adjacent rate's throughput is measured,
1949 * and may have better performance. */
1950 else {
1951 /* Higher adjacent rate's throughput is measured */
1952 if (high_tpt != IWL_INVALID_VALUE) {
1953 /* Higher rate has better throughput */
1954 if (high_tpt > current_tpt &&
1955 sr >= IWL_RATE_INCREASE_TH) {
1956 scale_action = 1;
1957 } else {
1958 scale_action = 0;
1961 /* Lower adjacent rate's throughput is measured */
1962 } else if (low_tpt != IWL_INVALID_VALUE) {
1963 /* Lower rate has better throughput */
1964 if (low_tpt > current_tpt) {
1965 IWL_DEBUG_RATE(mvm,
1966 "decrease rate because of low tpt\n");
1967 scale_action = -1;
1968 } else if (sr >= IWL_RATE_INCREASE_TH) {
1969 scale_action = 1;
1974 /* Sanity check; asked for decrease, but success rate or throughput
1975 * has been good at old rate. Don't change it. */
1976 if ((scale_action == -1) && (low != IWL_RATE_INVALID) &&
1977 ((sr > IWL_RATE_HIGH_TH) ||
1978 (current_tpt > (100 * tbl->expected_tpt[low]))))
1979 scale_action = 0;
1981 if ((BT_MBOX_MSG(&mvm->last_bt_notif, 3, TRAFFIC_LOAD) >=
1982 IWL_BT_COEX_TRAFFIC_LOAD_HIGH) && (is_mimo(tbl->lq_type))) {
1983 if (lq_sta->last_bt_traffic >
1984 BT_MBOX_MSG(&mvm->last_bt_notif, 3, TRAFFIC_LOAD)) {
1986 * don't set scale_action, don't want to scale up if
1987 * the rate scale doesn't otherwise think that is a
1988 * good idea.
1990 } else if (lq_sta->last_bt_traffic <=
1991 BT_MBOX_MSG(&mvm->last_bt_notif, 3, TRAFFIC_LOAD)) {
1992 scale_action = -1;
1995 lq_sta->last_bt_traffic =
1996 BT_MBOX_MSG(&mvm->last_bt_notif, 3, TRAFFIC_LOAD);
1998 if ((BT_MBOX_MSG(&mvm->last_bt_notif, 3, TRAFFIC_LOAD) >=
1999 IWL_BT_COEX_TRAFFIC_LOAD_HIGH) && is_mimo(tbl->lq_type)) {
2000 /* search for a new modulation */
2001 rs_stay_in_table(lq_sta, true);
2002 goto lq_update;
2005 switch (scale_action) {
2006 case -1:
2007 /* Decrease starting rate, update uCode's rate table */
2008 if (low != IWL_RATE_INVALID) {
2009 update_lq = 1;
2010 index = low;
2013 break;
2014 case 1:
2015 /* Increase starting rate, update uCode's rate table */
2016 if (high != IWL_RATE_INVALID) {
2017 update_lq = 1;
2018 index = high;
2021 break;
2022 case 0:
2023 /* No change */
2024 default:
2025 break;
2028 IWL_DEBUG_RATE(mvm,
2029 "choose rate scale index %d action %d low %d high %d type %d\n",
2030 index, scale_action, low, high, tbl->lq_type);
2032 lq_update:
2033 /* Replace uCode's rate table for the destination station. */
2034 if (update_lq)
2035 rs_update_rate_tbl(mvm, lq_sta, tbl, index, is_green);
2037 rs_stay_in_table(lq_sta, false);
2040 * Search for new modulation mode if we're:
2041 * 1) Not changing rates right now
2042 * 2) Not just finishing up a search
2043 * 3) Allowing a new search
2045 if (!update_lq && !done_search &&
2046 !lq_sta->stay_in_tbl && window->counter) {
2047 /* Save current throughput to compare with "search" throughput*/
2048 lq_sta->last_tpt = current_tpt;
2050 /* Select a new "search" modulation mode to try.
2051 * If one is found, set up the new "search" table. */
2052 if (is_legacy(tbl->lq_type))
2053 rs_move_legacy_other(mvm, lq_sta, sta, index);
2054 else if (is_siso(tbl->lq_type))
2055 rs_move_siso_to_other(mvm, lq_sta, sta, index);
2056 else if (is_mimo2(tbl->lq_type))
2057 rs_move_mimo2_to_other(mvm, lq_sta, sta, index);
2058 else
2059 WARN_ON_ONCE(1);
2061 /* If new "search" mode was selected, set up in uCode table */
2062 if (lq_sta->search_better_tbl) {
2063 /* Access the "search" table, clear its history. */
2064 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
2065 for (i = 0; i < IWL_RATE_COUNT; i++)
2066 rs_rate_scale_clear_window(&(tbl->win[i]));
2068 /* Use new "search" start rate */
2069 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
2071 IWL_DEBUG_RATE(mvm,
2072 "Switch current mcs: %X index: %d\n",
2073 tbl->current_rate, index);
2074 rs_fill_link_cmd(mvm, lq_sta, tbl->current_rate);
2075 iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, CMD_ASYNC, false);
2076 } else {
2077 done_search = 1;
2081 if (done_search && !lq_sta->stay_in_tbl) {
2082 /* If the "active" (non-search) mode was legacy,
2083 * and we've tried switching antennas,
2084 * but we haven't been able to try HT modes (not available),
2085 * stay with best antenna legacy modulation for a while
2086 * before next round of mode comparisons. */
2087 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
2088 if (is_legacy(tbl1->lq_type) && !sta->ht_cap.ht_supported &&
2089 lq_sta->action_counter > tbl1->max_search) {
2090 IWL_DEBUG_RATE(mvm, "LQ: STAY in legacy table\n");
2091 rs_set_stay_in_table(mvm, 1, lq_sta);
2094 /* If we're in an HT mode, and all 3 mode switch actions
2095 * have been tried and compared, stay in this best modulation
2096 * mode for a while before next round of mode comparisons. */
2097 if (lq_sta->enable_counter &&
2098 (lq_sta->action_counter >= tbl1->max_search)) {
2099 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2100 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
2101 (tid != IWL_MAX_TID_COUNT)) {
2102 tid_data = &sta_priv->tid_data[tid];
2103 if (tid_data->state == IWL_AGG_OFF) {
2104 IWL_DEBUG_RATE(mvm,
2105 "try to aggregate tid %d\n",
2106 tid);
2107 rs_tl_turn_on_agg(mvm, tid,
2108 lq_sta, sta);
2111 rs_set_stay_in_table(mvm, 0, lq_sta);
2115 out:
2116 tbl->current_rate = rate_n_flags_from_tbl(mvm, tbl, index, is_green);
2117 lq_sta->last_txrate_idx = index;
2121 * rs_initialize_lq - Initialize a station's hardware rate table
2123 * The uCode's station table contains a table of fallback rates
2124 * for automatic fallback during transmission.
2126 * NOTE: This sets up a default set of values. These will be replaced later
2127 * if the driver's iwl-agn-rs rate scaling algorithm is used, instead of
2128 * rc80211_simple.
2130 * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
2131 * calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
2132 * which requires station table entry to exist).
2134 static void rs_initialize_lq(struct iwl_mvm *mvm,
2135 struct ieee80211_sta *sta,
2136 struct iwl_lq_sta *lq_sta,
2137 enum ieee80211_band band)
2139 struct iwl_scale_tbl_info *tbl;
2140 int rate_idx;
2141 int i;
2142 u32 rate;
2143 u8 use_green = rs_use_green(sta);
2144 u8 active_tbl = 0;
2145 u8 valid_tx_ant;
2147 if (!sta || !lq_sta)
2148 return;
2150 i = lq_sta->last_txrate_idx;
2152 valid_tx_ant = iwl_fw_valid_tx_ant(mvm->fw);
2154 if (!lq_sta->search_better_tbl)
2155 active_tbl = lq_sta->active_tbl;
2156 else
2157 active_tbl = 1 - lq_sta->active_tbl;
2159 tbl = &(lq_sta->lq_info[active_tbl]);
2161 if ((i < 0) || (i >= IWL_RATE_COUNT))
2162 i = 0;
2164 rate = iwl_rates[i].plcp;
2165 tbl->ant_type = first_antenna(valid_tx_ant);
2166 rate |= tbl->ant_type << RATE_MCS_ANT_POS;
2168 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
2169 rate |= RATE_MCS_CCK_MSK;
2171 rs_get_tbl_info_from_mcs(rate, band, tbl, &rate_idx);
2172 if (!rs_is_valid_ant(valid_tx_ant, tbl->ant_type))
2173 rs_toggle_antenna(valid_tx_ant, &rate, tbl);
2175 rate = rate_n_flags_from_tbl(mvm, tbl, rate_idx, use_green);
2176 tbl->current_rate = rate;
2177 rs_set_expected_tpt_table(lq_sta, tbl);
2178 rs_fill_link_cmd(NULL, lq_sta, rate);
2179 /* TODO restore station should remember the lq cmd */
2180 iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, CMD_SYNC, true);
2183 static void rs_get_rate(void *mvm_r, struct ieee80211_sta *sta, void *mvm_sta,
2184 struct ieee80211_tx_rate_control *txrc)
2186 struct sk_buff *skb = txrc->skb;
2187 struct ieee80211_supported_band *sband = txrc->sband;
2188 struct iwl_op_mode *op_mode __maybe_unused =
2189 (struct iwl_op_mode *)mvm_r;
2190 struct iwl_mvm *mvm __maybe_unused = IWL_OP_MODE_GET_MVM(op_mode);
2191 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2192 struct iwl_lq_sta *lq_sta = mvm_sta;
2193 int rate_idx;
2195 IWL_DEBUG_RATE_LIMIT(mvm, "rate scale calculate new rate for skb\n");
2197 /* Get max rate if user set max rate */
2198 if (lq_sta) {
2199 lq_sta->max_rate_idx = txrc->max_rate_idx;
2200 if ((sband->band == IEEE80211_BAND_5GHZ) &&
2201 (lq_sta->max_rate_idx != -1))
2202 lq_sta->max_rate_idx += IWL_FIRST_OFDM_RATE;
2203 if ((lq_sta->max_rate_idx < 0) ||
2204 (lq_sta->max_rate_idx >= IWL_RATE_COUNT))
2205 lq_sta->max_rate_idx = -1;
2208 /* Treat uninitialized rate scaling data same as non-existing. */
2209 if (lq_sta && !lq_sta->drv) {
2210 IWL_DEBUG_RATE(mvm, "Rate scaling not initialized yet.\n");
2211 mvm_sta = NULL;
2214 /* Send management frames and NO_ACK data using lowest rate. */
2215 if (rate_control_send_low(sta, mvm_sta, txrc))
2216 return;
2218 rate_idx = lq_sta->last_txrate_idx;
2220 if (lq_sta->last_rate_n_flags & RATE_MCS_HT_MSK) {
2221 rate_idx -= IWL_FIRST_OFDM_RATE;
2222 /* 6M and 9M shared same MCS index */
2223 rate_idx = (rate_idx > 0) ? (rate_idx - 1) : 0;
2224 WARN_ON_ONCE(rs_extract_rate(lq_sta->last_rate_n_flags) >=
2225 IWL_RATE_MIMO3_6M_PLCP);
2226 if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
2227 IWL_RATE_MIMO2_6M_PLCP)
2228 rate_idx = rate_idx + MCS_INDEX_PER_STREAM;
2229 info->control.rates[0].flags = IEEE80211_TX_RC_MCS;
2230 if (lq_sta->last_rate_n_flags & RATE_MCS_SGI_MSK)
2231 info->control.rates[0].flags |= IEEE80211_TX_RC_SHORT_GI;
2232 if (lq_sta->last_rate_n_flags & RATE_MCS_CHAN_WIDTH_40) /* TODO */
2233 info->control.rates[0].flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
2234 if (lq_sta->last_rate_n_flags & RATE_HT_MCS_GF_MSK)
2235 info->control.rates[0].flags |= IEEE80211_TX_RC_GREEN_FIELD;
2236 } else {
2237 /* Check for invalid rates */
2238 if ((rate_idx < 0) || (rate_idx >= IWL_RATE_COUNT_LEGACY) ||
2239 ((sband->band == IEEE80211_BAND_5GHZ) &&
2240 (rate_idx < IWL_FIRST_OFDM_RATE)))
2241 rate_idx = rate_lowest_index(sband, sta);
2242 /* On valid 5 GHz rate, adjust index */
2243 else if (sband->band == IEEE80211_BAND_5GHZ)
2244 rate_idx -= IWL_FIRST_OFDM_RATE;
2245 info->control.rates[0].flags = 0;
2247 info->control.rates[0].idx = rate_idx;
2248 info->control.rates[0].count = 1;
2251 static void *rs_alloc_sta(void *mvm_rate, struct ieee80211_sta *sta,
2252 gfp_t gfp)
2254 struct iwl_mvm_sta *sta_priv = (struct iwl_mvm_sta *)sta->drv_priv;
2255 struct iwl_op_mode *op_mode __maybe_unused =
2256 (struct iwl_op_mode *)mvm_rate;
2257 struct iwl_mvm *mvm __maybe_unused = IWL_OP_MODE_GET_MVM(op_mode);
2259 IWL_DEBUG_RATE(mvm, "create station rate scale window\n");
2261 return &sta_priv->lq_sta;
2265 * Called after adding a new station to initialize rate scaling
2267 void iwl_mvm_rs_rate_init(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2268 enum ieee80211_band band)
2270 int i, j;
2271 struct ieee80211_hw *hw = mvm->hw;
2272 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
2273 struct iwl_mvm_sta *sta_priv;
2274 struct iwl_lq_sta *lq_sta;
2275 struct ieee80211_supported_band *sband;
2276 unsigned long supp; /* must be unsigned long for for_each_set_bit */
2278 sta_priv = (struct iwl_mvm_sta *)sta->drv_priv;
2279 lq_sta = &sta_priv->lq_sta;
2280 sband = hw->wiphy->bands[band];
2282 lq_sta->lq.sta_id = sta_priv->sta_id;
2284 for (j = 0; j < LQ_SIZE; j++)
2285 for (i = 0; i < IWL_RATE_COUNT; i++)
2286 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
2288 lq_sta->flush_timer = 0;
2289 lq_sta->supp_rates = sta->supp_rates[sband->band];
2291 IWL_DEBUG_RATE(mvm,
2292 "LQ: *** rate scale station global init for station %d ***\n",
2293 sta_priv->sta_id);
2294 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2295 * the lowest or the highest rate.. Could consider using RSSI from
2296 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2297 * after assoc.. */
2299 lq_sta->max_rate_idx = -1;
2300 lq_sta->missed_rate_counter = IWL_MISSED_RATE_MAX;
2301 lq_sta->is_green = rs_use_green(sta);
2302 lq_sta->band = sband->band;
2304 * active legacy rates as per supported rates bitmap
2306 supp = sta->supp_rates[sband->band];
2307 lq_sta->active_legacy_rate = 0;
2308 for_each_set_bit(i, &supp, BITS_PER_LONG)
2309 lq_sta->active_legacy_rate |= BIT(sband->bitrates[i].hw_value);
2312 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2313 * supp_rates[] does not; shift to convert format, force 9 MBits off.
2315 lq_sta->active_siso_rate = ht_cap->mcs.rx_mask[0] << 1;
2316 lq_sta->active_siso_rate |= ht_cap->mcs.rx_mask[0] & 0x1;
2317 lq_sta->active_siso_rate &= ~((u16)0x2);
2318 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
2320 /* Same here */
2321 lq_sta->active_mimo2_rate = ht_cap->mcs.rx_mask[1] << 1;
2322 lq_sta->active_mimo2_rate |= ht_cap->mcs.rx_mask[1] & 0x1;
2323 lq_sta->active_mimo2_rate &= ~((u16)0x2);
2324 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2326 IWL_DEBUG_RATE(mvm,
2327 "SISO-RATE=%X MIMO2-RATE=%X\n",
2328 lq_sta->active_siso_rate,
2329 lq_sta->active_mimo2_rate);
2331 /* These values will be overridden later */
2332 lq_sta->lq.single_stream_ant_msk =
2333 first_antenna(iwl_fw_valid_tx_ant(mvm->fw));
2334 lq_sta->lq.dual_stream_ant_msk =
2335 iwl_fw_valid_tx_ant(mvm->fw) &
2336 ~first_antenna(iwl_fw_valid_tx_ant(mvm->fw));
2337 if (!lq_sta->lq.dual_stream_ant_msk) {
2338 lq_sta->lq.dual_stream_ant_msk = ANT_AB;
2339 } else if (num_of_ant(iwl_fw_valid_tx_ant(mvm->fw)) == 2) {
2340 lq_sta->lq.dual_stream_ant_msk =
2341 iwl_fw_valid_tx_ant(mvm->fw);
2344 /* as default allow aggregation for all tids */
2345 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
2346 lq_sta->drv = mvm;
2348 /* Set last_txrate_idx to lowest rate */
2349 lq_sta->last_txrate_idx = rate_lowest_index(sband, sta);
2350 if (sband->band == IEEE80211_BAND_5GHZ)
2351 lq_sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
2352 lq_sta->is_agg = 0;
2353 #ifdef CONFIG_MAC80211_DEBUGFS
2354 lq_sta->dbg_fixed_rate = 0;
2355 #endif
2357 rs_initialize_lq(mvm, sta, lq_sta, band);
2360 static void rs_fill_link_cmd(struct iwl_mvm *mvm,
2361 struct iwl_lq_sta *lq_sta, u32 new_rate)
2363 struct iwl_scale_tbl_info tbl_type;
2364 int index = 0;
2365 int rate_idx;
2366 int repeat_rate = 0;
2367 u8 ant_toggle_cnt = 0;
2368 u8 use_ht_possible = 1;
2369 u8 valid_tx_ant = 0;
2370 struct iwl_lq_cmd *lq_cmd = &lq_sta->lq;
2372 /* Override starting rate (index 0) if needed for debug purposes */
2373 rs_dbgfs_set_mcs(lq_sta, &new_rate);
2375 /* Interpret new_rate (rate_n_flags) */
2376 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
2377 &tbl_type, &rate_idx);
2379 /* How many times should we repeat the initial rate? */
2380 if (is_legacy(tbl_type.lq_type)) {
2381 ant_toggle_cnt = 1;
2382 repeat_rate = IWL_NUMBER_TRY;
2383 } else {
2384 repeat_rate = min(IWL_HT_NUMBER_TRY,
2385 LINK_QUAL_AGG_DISABLE_START_DEF - 1);
2388 lq_cmd->mimo_delim = is_mimo(tbl_type.lq_type) ? 1 : 0;
2390 /* Fill 1st table entry (index 0) */
2391 lq_cmd->rs_table[index] = cpu_to_le32(new_rate);
2393 if (num_of_ant(tbl_type.ant_type) == 1)
2394 lq_cmd->single_stream_ant_msk = tbl_type.ant_type;
2395 else if (num_of_ant(tbl_type.ant_type) == 2)
2396 lq_cmd->dual_stream_ant_msk = tbl_type.ant_type;
2397 /* otherwise we don't modify the existing value */
2399 index++;
2400 repeat_rate--;
2401 if (mvm)
2402 valid_tx_ant = iwl_fw_valid_tx_ant(mvm->fw);
2404 /* Fill rest of rate table */
2405 while (index < LINK_QUAL_MAX_RETRY_NUM) {
2406 /* Repeat initial/next rate.
2407 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2408 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
2409 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
2410 if (is_legacy(tbl_type.lq_type)) {
2411 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2412 ant_toggle_cnt++;
2413 else if (mvm &&
2414 rs_toggle_antenna(valid_tx_ant,
2415 &new_rate, &tbl_type))
2416 ant_toggle_cnt = 1;
2419 /* Override next rate if needed for debug purposes */
2420 rs_dbgfs_set_mcs(lq_sta, &new_rate);
2422 /* Fill next table entry */
2423 lq_cmd->rs_table[index] =
2424 cpu_to_le32(new_rate);
2425 repeat_rate--;
2426 index++;
2429 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band, &tbl_type,
2430 &rate_idx);
2433 /* Indicate to uCode which entries might be MIMO.
2434 * If initial rate was MIMO, this will finally end up
2435 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
2436 if (is_mimo(tbl_type.lq_type))
2437 lq_cmd->mimo_delim = index;
2439 /* Get next rate */
2440 new_rate = rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
2441 use_ht_possible);
2443 /* How many times should we repeat the next rate? */
2444 if (is_legacy(tbl_type.lq_type)) {
2445 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2446 ant_toggle_cnt++;
2447 else if (mvm &&
2448 rs_toggle_antenna(valid_tx_ant,
2449 &new_rate, &tbl_type))
2450 ant_toggle_cnt = 1;
2452 repeat_rate = IWL_NUMBER_TRY;
2453 } else {
2454 repeat_rate = IWL_HT_NUMBER_TRY;
2457 /* Don't allow HT rates after next pass.
2458 * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
2459 use_ht_possible = 0;
2461 /* Override next rate if needed for debug purposes */
2462 rs_dbgfs_set_mcs(lq_sta, &new_rate);
2464 /* Fill next table entry */
2465 lq_cmd->rs_table[index] = cpu_to_le32(new_rate);
2467 index++;
2468 repeat_rate--;
2471 lq_cmd->agg_frame_cnt_limit = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
2472 lq_cmd->agg_disable_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
2474 lq_cmd->agg_time_limit =
2475 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
2478 * overwrite if needed, pass aggregation time limit
2479 * to uCode in uSec - This is racy - but heh, at least it helps...
2481 if (mvm && BT_MBOX_MSG(&mvm->last_bt_notif, 3, TRAFFIC_LOAD) >= 2)
2482 lq_cmd->agg_time_limit = cpu_to_le16(1200);
2485 static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
2487 return hw->priv;
2489 /* rate scale requires free function to be implemented */
2490 static void rs_free(void *mvm_rate)
2492 return;
2495 static void rs_free_sta(void *mvm_r, struct ieee80211_sta *sta,
2496 void *mvm_sta)
2498 struct iwl_op_mode *op_mode __maybe_unused = mvm_r;
2499 struct iwl_mvm *mvm __maybe_unused = IWL_OP_MODE_GET_MVM(op_mode);
2501 IWL_DEBUG_RATE(mvm, "enter\n");
2502 IWL_DEBUG_RATE(mvm, "leave\n");
2505 #ifdef CONFIG_MAC80211_DEBUGFS
2506 static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
2507 u32 *rate_n_flags)
2509 struct iwl_mvm *mvm;
2510 u8 valid_tx_ant;
2511 u8 ant_sel_tx;
2513 mvm = lq_sta->drv;
2514 valid_tx_ant = iwl_fw_valid_tx_ant(mvm->fw);
2515 if (lq_sta->dbg_fixed_rate) {
2516 ant_sel_tx =
2517 ((lq_sta->dbg_fixed_rate & RATE_MCS_ANT_ABC_MSK)
2518 >> RATE_MCS_ANT_POS);
2519 if ((valid_tx_ant & ant_sel_tx) == ant_sel_tx) {
2520 *rate_n_flags = lq_sta->dbg_fixed_rate;
2521 IWL_DEBUG_RATE(mvm, "Fixed rate ON\n");
2522 } else {
2523 lq_sta->dbg_fixed_rate = 0;
2524 IWL_ERR(mvm,
2525 "Invalid antenna selection 0x%X, Valid is 0x%X\n",
2526 ant_sel_tx, valid_tx_ant);
2527 IWL_DEBUG_RATE(mvm, "Fixed rate OFF\n");
2529 } else {
2530 IWL_DEBUG_RATE(mvm, "Fixed rate OFF\n");
2534 static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
2535 const char __user *user_buf, size_t count, loff_t *ppos)
2537 struct iwl_lq_sta *lq_sta = file->private_data;
2538 struct iwl_mvm *mvm;
2539 char buf[64];
2540 size_t buf_size;
2541 u32 parsed_rate;
2544 mvm = lq_sta->drv;
2545 memset(buf, 0, sizeof(buf));
2546 buf_size = min(count, sizeof(buf) - 1);
2547 if (copy_from_user(buf, user_buf, buf_size))
2548 return -EFAULT;
2550 if (sscanf(buf, "%x", &parsed_rate) == 1)
2551 lq_sta->dbg_fixed_rate = parsed_rate;
2552 else
2553 lq_sta->dbg_fixed_rate = 0;
2555 rs_program_fix_rate(mvm, lq_sta);
2557 return count;
2560 static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
2561 char __user *user_buf, size_t count, loff_t *ppos)
2563 char *buff;
2564 int desc = 0;
2565 int i = 0;
2566 int index = 0;
2567 ssize_t ret;
2569 struct iwl_lq_sta *lq_sta = file->private_data;
2570 struct iwl_mvm *mvm;
2571 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
2573 mvm = lq_sta->drv;
2574 buff = kmalloc(1024, GFP_KERNEL);
2575 if (!buff)
2576 return -ENOMEM;
2578 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
2579 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
2580 lq_sta->total_failed, lq_sta->total_success,
2581 lq_sta->active_legacy_rate);
2582 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
2583 lq_sta->dbg_fixed_rate);
2584 desc += sprintf(buff+desc, "valid_tx_ant %s%s%s\n",
2585 (iwl_fw_valid_tx_ant(mvm->fw) & ANT_A) ? "ANT_A," : "",
2586 (iwl_fw_valid_tx_ant(mvm->fw) & ANT_B) ? "ANT_B," : "",
2587 (iwl_fw_valid_tx_ant(mvm->fw) & ANT_C) ? "ANT_C" : "");
2588 desc += sprintf(buff+desc, "lq type %s\n",
2589 (is_legacy(tbl->lq_type)) ? "legacy" : "HT");
2590 if (is_Ht(tbl->lq_type)) {
2591 desc += sprintf(buff+desc, " %s",
2592 (is_siso(tbl->lq_type)) ? "SISO" : "MIMO2");
2593 desc += sprintf(buff+desc, " %s",
2594 (tbl->is_ht40) ? "40MHz" : "20MHz");
2595 desc += sprintf(buff+desc, " %s %s %s\n",
2596 (tbl->is_SGI) ? "SGI" : "",
2597 (lq_sta->is_green) ? "GF enabled" : "",
2598 (lq_sta->is_agg) ? "AGG on" : "");
2600 desc += sprintf(buff+desc, "last tx rate=0x%X\n",
2601 lq_sta->last_rate_n_flags);
2602 desc += sprintf(buff+desc,
2603 "general: flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
2604 lq_sta->lq.flags,
2605 lq_sta->lq.mimo_delim,
2606 lq_sta->lq.single_stream_ant_msk,
2607 lq_sta->lq.dual_stream_ant_msk);
2609 desc += sprintf(buff+desc,
2610 "agg: time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
2611 le16_to_cpu(lq_sta->lq.agg_time_limit),
2612 lq_sta->lq.agg_disable_start_th,
2613 lq_sta->lq.agg_frame_cnt_limit);
2615 desc += sprintf(buff+desc,
2616 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
2617 lq_sta->lq.initial_rate_index[0],
2618 lq_sta->lq.initial_rate_index[1],
2619 lq_sta->lq.initial_rate_index[2],
2620 lq_sta->lq.initial_rate_index[3]);
2622 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
2623 index = iwl_hwrate_to_plcp_idx(
2624 le32_to_cpu(lq_sta->lq.rs_table[i]));
2625 if (is_legacy(tbl->lq_type)) {
2626 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps\n",
2627 i, le32_to_cpu(lq_sta->lq.rs_table[i]),
2628 iwl_rate_mcs[index].mbps);
2629 } else {
2630 desc += sprintf(buff+desc,
2631 " rate[%d] 0x%X %smbps (%s)\n",
2632 i, le32_to_cpu(lq_sta->lq.rs_table[i]),
2633 iwl_rate_mcs[index].mbps,
2634 iwl_rate_mcs[index].mcs);
2638 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2639 kfree(buff);
2640 return ret;
2643 static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
2644 .write = rs_sta_dbgfs_scale_table_write,
2645 .read = rs_sta_dbgfs_scale_table_read,
2646 .open = simple_open,
2647 .llseek = default_llseek,
2649 static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
2650 char __user *user_buf, size_t count, loff_t *ppos)
2652 char *buff;
2653 int desc = 0;
2654 int i, j;
2655 ssize_t ret;
2657 struct iwl_lq_sta *lq_sta = file->private_data;
2659 buff = kmalloc(1024, GFP_KERNEL);
2660 if (!buff)
2661 return -ENOMEM;
2663 for (i = 0; i < LQ_SIZE; i++) {
2664 desc += sprintf(buff+desc,
2665 "%s type=%d SGI=%d HT40=%d DUP=0 GF=%d\n"
2666 "rate=0x%X\n",
2667 lq_sta->active_tbl == i ? "*" : "x",
2668 lq_sta->lq_info[i].lq_type,
2669 lq_sta->lq_info[i].is_SGI,
2670 lq_sta->lq_info[i].is_ht40,
2671 lq_sta->is_green,
2672 lq_sta->lq_info[i].current_rate);
2673 for (j = 0; j < IWL_RATE_COUNT; j++) {
2674 desc += sprintf(buff+desc,
2675 "counter=%d success=%d %%=%d\n",
2676 lq_sta->lq_info[i].win[j].counter,
2677 lq_sta->lq_info[i].win[j].success_counter,
2678 lq_sta->lq_info[i].win[j].success_ratio);
2681 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2682 kfree(buff);
2683 return ret;
2686 static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
2687 .read = rs_sta_dbgfs_stats_table_read,
2688 .open = simple_open,
2689 .llseek = default_llseek,
2692 static void rs_add_debugfs(void *mvm, void *mvm_sta, struct dentry *dir)
2694 struct iwl_lq_sta *lq_sta = mvm_sta;
2695 lq_sta->rs_sta_dbgfs_scale_table_file =
2696 debugfs_create_file("rate_scale_table", S_IRUSR | S_IWUSR, dir,
2697 lq_sta, &rs_sta_dbgfs_scale_table_ops);
2698 lq_sta->rs_sta_dbgfs_stats_table_file =
2699 debugfs_create_file("rate_stats_table", S_IRUSR, dir,
2700 lq_sta, &rs_sta_dbgfs_stats_table_ops);
2701 lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
2702 debugfs_create_u8("tx_agg_tid_enable", S_IRUSR | S_IWUSR, dir,
2703 &lq_sta->tx_agg_tid_en);
2706 static void rs_remove_debugfs(void *mvm, void *mvm_sta)
2708 struct iwl_lq_sta *lq_sta = mvm_sta;
2709 debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
2710 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
2711 debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
2713 #endif
2716 * Initialization of rate scaling information is done by driver after
2717 * the station is added. Since mac80211 calls this function before a
2718 * station is added we ignore it.
2720 static void rs_rate_init_stub(void *mvm_r,
2721 struct ieee80211_supported_band *sband,
2722 struct cfg80211_chan_def *chandef,
2723 struct ieee80211_sta *sta, void *mvm_sta)
2726 static struct rate_control_ops rs_mvm_ops = {
2727 .module = NULL,
2728 .name = RS_NAME,
2729 .tx_status = rs_tx_status,
2730 .get_rate = rs_get_rate,
2731 .rate_init = rs_rate_init_stub,
2732 .alloc = rs_alloc,
2733 .free = rs_free,
2734 .alloc_sta = rs_alloc_sta,
2735 .free_sta = rs_free_sta,
2736 #ifdef CONFIG_MAC80211_DEBUGFS
2737 .add_sta_debugfs = rs_add_debugfs,
2738 .remove_sta_debugfs = rs_remove_debugfs,
2739 #endif
2742 int iwl_mvm_rate_control_register(void)
2744 return ieee80211_rate_control_register(&rs_mvm_ops);
2747 void iwl_mvm_rate_control_unregister(void)
2749 ieee80211_rate_control_unregister(&rs_mvm_ops);
2753 * iwl_mvm_tx_protection - Gets LQ command, change it to enable/disable
2754 * Tx protection, according to this rquest and previous requests,
2755 * and send the LQ command.
2756 * @mvmsta: The station
2757 * @enable: Enable Tx protection?
2759 int iwl_mvm_tx_protection(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
2760 bool enable)
2762 struct iwl_lq_cmd *lq = &mvmsta->lq_sta.lq;
2764 lockdep_assert_held(&mvm->mutex);
2766 if (enable) {
2767 if (mvmsta->tx_protection == 0)
2768 lq->flags |= LQ_FLAG_SET_STA_TLC_RTS_MSK;
2769 mvmsta->tx_protection++;
2770 } else {
2771 mvmsta->tx_protection--;
2772 if (mvmsta->tx_protection == 0)
2773 lq->flags &= ~LQ_FLAG_SET_STA_TLC_RTS_MSK;
2776 return iwl_mvm_send_lq_cmd(mvm, lq, CMD_ASYNC, false);