1 /******************************************************************************
3 * Copyright(c) 2005 - 2014 Intel Corporation. All rights reserved.
4 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of version 2 of the GNU General Public License as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
19 * The full GNU General Public License is included in this distribution in the
20 * file called LICENSE.
22 * Contact Information:
23 * Intel Linux Wireless <ilw@linux.intel.com>
24 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
26 *****************************************************************************/
27 #include <linux/kernel.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>
40 #include "iwl-op-mode.h"
43 #define RS_NAME "iwl-mvm-rs"
45 #define NUM_TRY_BEFORE_ANT_TOGGLE 1
46 #define RS_LEGACY_RETRIES_PER_RATE 1
47 #define RS_HT_VHT_RETRIES_PER_RATE 2
48 #define RS_HT_VHT_RETRIES_PER_RATE_TW 1
49 #define RS_INITIAL_MIMO_NUM_RATES 3
50 #define RS_INITIAL_SISO_NUM_RATES 3
51 #define RS_INITIAL_LEGACY_NUM_RATES LINK_QUAL_MAX_RETRY_NUM
52 #define RS_SECONDARY_LEGACY_NUM_RATES LINK_QUAL_MAX_RETRY_NUM
53 #define RS_SECONDARY_SISO_NUM_RATES 3
54 #define RS_SECONDARY_SISO_RETRIES 1
56 #define IWL_RATE_MAX_WINDOW 62 /* # tx in history window */
57 #define IWL_RATE_MIN_FAILURE_TH 3 /* min failures to calc tpt */
58 #define IWL_RATE_MIN_SUCCESS_TH 8 /* min successes to calc tpt */
60 /* max allowed rate miss before sync LQ cmd */
61 #define IWL_MISSED_RATE_MAX 15
62 #define RS_STAY_IN_COLUMN_TIMEOUT (5*HZ)
63 #define RS_IDLE_TIMEOUT (5*HZ)
65 static u8 rs_ht_to_legacy[] = {
66 [IWL_RATE_MCS_0_INDEX] = IWL_RATE_6M_INDEX,
67 [IWL_RATE_MCS_1_INDEX] = IWL_RATE_9M_INDEX,
68 [IWL_RATE_MCS_2_INDEX] = IWL_RATE_12M_INDEX,
69 [IWL_RATE_MCS_3_INDEX] = IWL_RATE_18M_INDEX,
70 [IWL_RATE_MCS_4_INDEX] = IWL_RATE_24M_INDEX,
71 [IWL_RATE_MCS_5_INDEX] = IWL_RATE_36M_INDEX,
72 [IWL_RATE_MCS_6_INDEX] = IWL_RATE_48M_INDEX,
73 [IWL_RATE_MCS_7_INDEX] = IWL_RATE_54M_INDEX,
74 [IWL_RATE_MCS_8_INDEX] = IWL_RATE_54M_INDEX,
75 [IWL_RATE_MCS_9_INDEX] = IWL_RATE_54M_INDEX,
78 static const u8 ant_toggle_lookup[] = {
79 [ANT_NONE] = ANT_NONE,
89 #define IWL_DECLARE_RATE_INFO(r, s, rp, rn) \
90 [IWL_RATE_##r##M_INDEX] = { IWL_RATE_##r##M_PLCP, \
91 IWL_RATE_HT_SISO_MCS_##s##_PLCP, \
92 IWL_RATE_HT_MIMO2_MCS_##s##_PLCP, \
93 IWL_RATE_VHT_SISO_MCS_##s##_PLCP, \
94 IWL_RATE_VHT_MIMO2_MCS_##s##_PLCP,\
95 IWL_RATE_##rp##M_INDEX, \
96 IWL_RATE_##rn##M_INDEX }
98 #define IWL_DECLARE_MCS_RATE(s) \
99 [IWL_RATE_MCS_##s##_INDEX] = { IWL_RATE_INVM_PLCP, \
100 IWL_RATE_HT_SISO_MCS_##s##_PLCP, \
101 IWL_RATE_HT_MIMO2_MCS_##s##_PLCP, \
102 IWL_RATE_VHT_SISO_MCS_##s##_PLCP, \
103 IWL_RATE_VHT_MIMO2_MCS_##s##_PLCP, \
104 IWL_RATE_INVM_INDEX, \
105 IWL_RATE_INVM_INDEX }
109 * rate, ht rate, prev rate, next rate
111 * If there isn't a valid next or previous rate then INV is used which
112 * maps to IWL_RATE_INVALID
115 static const struct iwl_rs_rate_info iwl_rates[IWL_RATE_COUNT] = {
116 IWL_DECLARE_RATE_INFO(1, INV, INV, 2), /* 1mbps */
117 IWL_DECLARE_RATE_INFO(2, INV, 1, 5), /* 2mbps */
118 IWL_DECLARE_RATE_INFO(5, INV, 2, 11), /*5.5mbps */
119 IWL_DECLARE_RATE_INFO(11, INV, 9, 12), /* 11mbps */
120 IWL_DECLARE_RATE_INFO(6, 0, 5, 11), /* 6mbps ; MCS 0 */
121 IWL_DECLARE_RATE_INFO(9, INV, 6, 11), /* 9mbps */
122 IWL_DECLARE_RATE_INFO(12, 1, 11, 18), /* 12mbps ; MCS 1 */
123 IWL_DECLARE_RATE_INFO(18, 2, 12, 24), /* 18mbps ; MCS 2 */
124 IWL_DECLARE_RATE_INFO(24, 3, 18, 36), /* 24mbps ; MCS 3 */
125 IWL_DECLARE_RATE_INFO(36, 4, 24, 48), /* 36mbps ; MCS 4 */
126 IWL_DECLARE_RATE_INFO(48, 5, 36, 54), /* 48mbps ; MCS 5 */
127 IWL_DECLARE_RATE_INFO(54, 6, 48, INV), /* 54mbps ; MCS 6 */
128 IWL_DECLARE_MCS_RATE(7), /* MCS 7 */
129 IWL_DECLARE_MCS_RATE(8), /* MCS 8 */
130 IWL_DECLARE_MCS_RATE(9), /* MCS 9 */
135 RS_ACTION_DOWNSCALE = -1,
136 RS_ACTION_UPSCALE = 1,
139 enum rs_column_mode {
146 #define MAX_NEXT_COLUMNS 7
147 #define MAX_COLUMN_CHECKS 3
149 typedef bool (*allow_column_func_t) (struct iwl_mvm *mvm,
150 struct ieee80211_sta *sta,
151 struct iwl_scale_tbl_info *tbl);
153 struct rs_tx_column {
154 enum rs_column_mode mode;
157 enum rs_column next_columns[MAX_NEXT_COLUMNS];
158 allow_column_func_t checks[MAX_COLUMN_CHECKS];
161 static bool rs_ant_allow(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
162 struct iwl_scale_tbl_info *tbl)
164 return iwl_mvm_bt_coex_is_ant_avail(mvm, tbl->rate.ant);
167 static bool rs_mimo_allow(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
168 struct iwl_scale_tbl_info *tbl)
170 if (!sta->ht_cap.ht_supported)
173 if (sta->smps_mode == IEEE80211_SMPS_STATIC)
176 if (num_of_ant(iwl_mvm_get_valid_tx_ant(mvm)) < 2)
179 if (!iwl_mvm_bt_coex_is_mimo_allowed(mvm, sta))
185 static bool rs_siso_allow(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
186 struct iwl_scale_tbl_info *tbl)
188 if (!sta->ht_cap.ht_supported)
194 static bool rs_sgi_allow(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
195 struct iwl_scale_tbl_info *tbl)
197 struct rs_rate *rate = &tbl->rate;
198 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
199 struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
201 if (is_ht20(rate) && (ht_cap->cap &
202 IEEE80211_HT_CAP_SGI_20))
204 if (is_ht40(rate) && (ht_cap->cap &
205 IEEE80211_HT_CAP_SGI_40))
207 if (is_ht80(rate) && (vht_cap->cap &
208 IEEE80211_VHT_CAP_SHORT_GI_80))
214 static const struct rs_tx_column rs_tx_columns[] = {
215 [RS_COLUMN_LEGACY_ANT_A] = {
219 RS_COLUMN_LEGACY_ANT_B,
220 RS_COLUMN_SISO_ANT_A,
231 [RS_COLUMN_LEGACY_ANT_B] = {
235 RS_COLUMN_LEGACY_ANT_A,
236 RS_COLUMN_SISO_ANT_B,
247 [RS_COLUMN_SISO_ANT_A] = {
251 RS_COLUMN_SISO_ANT_B,
253 RS_COLUMN_SISO_ANT_A_SGI,
254 RS_COLUMN_LEGACY_ANT_A,
255 RS_COLUMN_LEGACY_ANT_B,
264 [RS_COLUMN_SISO_ANT_B] = {
268 RS_COLUMN_SISO_ANT_A,
270 RS_COLUMN_SISO_ANT_B_SGI,
271 RS_COLUMN_LEGACY_ANT_A,
272 RS_COLUMN_LEGACY_ANT_B,
281 [RS_COLUMN_SISO_ANT_A_SGI] = {
286 RS_COLUMN_SISO_ANT_B_SGI,
288 RS_COLUMN_SISO_ANT_A,
289 RS_COLUMN_LEGACY_ANT_A,
290 RS_COLUMN_LEGACY_ANT_B,
300 [RS_COLUMN_SISO_ANT_B_SGI] = {
305 RS_COLUMN_SISO_ANT_A_SGI,
307 RS_COLUMN_SISO_ANT_B,
308 RS_COLUMN_LEGACY_ANT_A,
309 RS_COLUMN_LEGACY_ANT_B,
319 [RS_COLUMN_MIMO2] = {
323 RS_COLUMN_SISO_ANT_A,
325 RS_COLUMN_LEGACY_ANT_A,
326 RS_COLUMN_LEGACY_ANT_B,
335 [RS_COLUMN_MIMO2_SGI] = {
340 RS_COLUMN_SISO_ANT_A_SGI,
342 RS_COLUMN_LEGACY_ANT_A,
343 RS_COLUMN_LEGACY_ANT_B,
355 static inline u8 rs_extract_rate(u32 rate_n_flags)
357 /* also works for HT because bits 7:6 are zero there */
358 return (u8)(rate_n_flags & RATE_LEGACY_RATE_MSK);
361 static int iwl_hwrate_to_plcp_idx(u32 rate_n_flags)
365 if (rate_n_flags & RATE_MCS_HT_MSK) {
366 idx = rate_n_flags & RATE_HT_MCS_RATE_CODE_MSK;
367 idx += IWL_RATE_MCS_0_INDEX;
369 /* skip 9M not supported in HT*/
370 if (idx >= IWL_RATE_9M_INDEX)
372 if ((idx >= IWL_FIRST_HT_RATE) && (idx <= IWL_LAST_HT_RATE))
374 } else if (rate_n_flags & RATE_MCS_VHT_MSK) {
375 idx = rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK;
376 idx += IWL_RATE_MCS_0_INDEX;
378 /* skip 9M not supported in VHT*/
379 if (idx >= IWL_RATE_9M_INDEX)
381 if ((idx >= IWL_FIRST_VHT_RATE) && (idx <= IWL_LAST_VHT_RATE))
384 /* legacy rate format, search for match in table */
386 u8 legacy_rate = rs_extract_rate(rate_n_flags);
387 for (idx = 0; idx < ARRAY_SIZE(iwl_rates); idx++)
388 if (iwl_rates[idx].plcp == legacy_rate)
392 return IWL_RATE_INVALID;
395 static void rs_rate_scale_perform(struct iwl_mvm *mvm,
396 struct ieee80211_sta *sta,
397 struct iwl_lq_sta *lq_sta,
399 static void rs_fill_lq_cmd(struct iwl_mvm *mvm,
400 struct ieee80211_sta *sta,
401 struct iwl_lq_sta *lq_sta,
402 const struct rs_rate *initial_rate);
403 static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search);
406 * The following tables contain the expected throughput metrics for all rates
408 * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
410 * where invalid entries are zeros.
412 * CCK rates are only valid in legacy table and will only be used in G
416 static const u16 expected_tpt_legacy[IWL_RATE_COUNT] = {
417 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 0, 0, 0
420 /* Expected TpT tables. 4 indexes:
421 * 0 - NGI, 1 - SGI, 2 - AGG+NGI, 3 - AGG+SGI
423 static const u16 expected_tpt_siso_20MHz[4][IWL_RATE_COUNT] = {
424 {0, 0, 0, 0, 42, 0, 76, 102, 124, 159, 183, 193, 202, 216, 0},
425 {0, 0, 0, 0, 46, 0, 82, 110, 132, 168, 192, 202, 210, 225, 0},
426 {0, 0, 0, 0, 49, 0, 97, 145, 192, 285, 375, 420, 464, 551, 0},
427 {0, 0, 0, 0, 54, 0, 108, 160, 213, 315, 415, 465, 513, 608, 0},
430 static const u16 expected_tpt_siso_40MHz[4][IWL_RATE_COUNT] = {
431 {0, 0, 0, 0, 77, 0, 127, 160, 184, 220, 242, 250, 257, 269, 275},
432 {0, 0, 0, 0, 83, 0, 135, 169, 193, 229, 250, 257, 264, 275, 280},
433 {0, 0, 0, 0, 101, 0, 199, 295, 389, 570, 744, 828, 911, 1070, 1173},
434 {0, 0, 0, 0, 112, 0, 220, 326, 429, 629, 819, 912, 1000, 1173, 1284},
437 static const u16 expected_tpt_siso_80MHz[4][IWL_RATE_COUNT] = {
438 {0, 0, 0, 0, 130, 0, 191, 223, 244, 273, 288, 294, 298, 305, 308},
439 {0, 0, 0, 0, 138, 0, 200, 231, 251, 279, 293, 298, 302, 308, 312},
440 {0, 0, 0, 0, 217, 0, 429, 634, 834, 1220, 1585, 1760, 1931, 2258, 2466},
441 {0, 0, 0, 0, 241, 0, 475, 701, 921, 1343, 1741, 1931, 2117, 2468, 2691},
444 static const u16 expected_tpt_mimo2_20MHz[4][IWL_RATE_COUNT] = {
445 {0, 0, 0, 0, 74, 0, 123, 155, 179, 213, 235, 243, 250, 261, 0},
446 {0, 0, 0, 0, 81, 0, 131, 164, 187, 221, 242, 250, 256, 267, 0},
447 {0, 0, 0, 0, 98, 0, 193, 286, 375, 550, 718, 799, 878, 1032, 0},
448 {0, 0, 0, 0, 109, 0, 214, 316, 414, 607, 790, 879, 965, 1132, 0},
451 static const u16 expected_tpt_mimo2_40MHz[4][IWL_RATE_COUNT] = {
452 {0, 0, 0, 0, 123, 0, 182, 214, 235, 264, 279, 285, 289, 296, 300},
453 {0, 0, 0, 0, 131, 0, 191, 222, 242, 270, 284, 289, 293, 300, 303},
454 {0, 0, 0, 0, 200, 0, 390, 571, 741, 1067, 1365, 1505, 1640, 1894, 2053},
455 {0, 0, 0, 0, 221, 0, 430, 630, 816, 1169, 1490, 1641, 1784, 2053, 2221},
458 static const u16 expected_tpt_mimo2_80MHz[4][IWL_RATE_COUNT] = {
459 {0, 0, 0, 0, 182, 0, 240, 264, 278, 299, 308, 311, 313, 317, 319},
460 {0, 0, 0, 0, 190, 0, 247, 269, 282, 302, 310, 313, 315, 319, 320},
461 {0, 0, 0, 0, 428, 0, 833, 1215, 1577, 2254, 2863, 3147, 3418, 3913, 4219},
462 {0, 0, 0, 0, 474, 0, 920, 1338, 1732, 2464, 3116, 3418, 3705, 4225, 4545},
466 static const struct iwl_rate_mcs_info iwl_rate_mcs[IWL_RATE_COUNT] = {
475 { "24", "16QAM 1/2"},
476 { "36", "16QAM 3/4"},
477 { "48", "64QAM 2/3"},
478 { "54", "64QAM 3/4"},
479 { "60", "64QAM 5/6"},
482 #define MCS_INDEX_PER_STREAM (8)
484 static const char *rs_pretty_ant(u8 ant)
486 static const char * const ant_name[] = {
500 return ant_name[ant];
503 static const char *rs_pretty_lq_type(enum iwl_table_type type)
505 static const char * const lq_types[] = {
507 [LQ_LEGACY_A] = "LEGACY_A",
508 [LQ_LEGACY_G] = "LEGACY_G",
509 [LQ_HT_SISO] = "HT SISO",
510 [LQ_HT_MIMO2] = "HT MIMO",
511 [LQ_VHT_SISO] = "VHT SISO",
512 [LQ_VHT_MIMO2] = "VHT MIMO",
515 if (type < LQ_NONE || type >= LQ_MAX)
518 return lq_types[type];
521 static inline void rs_dump_rate(struct iwl_mvm *mvm, const struct rs_rate *rate,
525 "%s: (%s: %d) ANT: %s BW: %d SGI: %d LDPC: %d STBC: %d\n",
526 prefix, rs_pretty_lq_type(rate->type),
527 rate->index, rs_pretty_ant(rate->ant),
528 rate->bw, rate->sgi, rate->ldpc, rate->stbc);
531 static void rs_rate_scale_clear_window(struct iwl_rate_scale_data *window)
534 window->success_counter = 0;
535 window->success_ratio = IWL_INVALID_VALUE;
537 window->average_tpt = IWL_INVALID_VALUE;
540 static void rs_rate_scale_clear_tbl_windows(struct iwl_mvm *mvm,
541 struct iwl_scale_tbl_info *tbl)
545 IWL_DEBUG_RATE(mvm, "Clearing up window stats\n");
546 for (i = 0; i < IWL_RATE_COUNT; i++)
547 rs_rate_scale_clear_window(&tbl->win[i]);
549 for (i = 0; i < ARRAY_SIZE(tbl->tpc_win); i++)
550 rs_rate_scale_clear_window(&tbl->tpc_win[i]);
553 static inline u8 rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
555 return (ant_type & valid_antenna) == ant_type;
558 static int rs_tl_turn_on_agg_for_tid(struct iwl_mvm *mvm,
559 struct iwl_lq_sta *lq_data, u8 tid,
560 struct ieee80211_sta *sta)
564 IWL_DEBUG_HT(mvm, "Starting Tx agg: STA: %pM tid: %d\n",
566 ret = ieee80211_start_tx_ba_session(sta, tid, 5000);
567 if (ret == -EAGAIN) {
569 * driver and mac80211 is out of sync
570 * this might be cause by reloading firmware
571 * stop the tx ba session here
573 IWL_ERR(mvm, "Fail start Tx agg on tid: %d\n",
575 ieee80211_stop_tx_ba_session(sta, tid);
580 static void rs_tl_turn_on_agg(struct iwl_mvm *mvm, u8 tid,
581 struct iwl_lq_sta *lq_data,
582 struct ieee80211_sta *sta)
584 if (tid < IWL_MAX_TID_COUNT)
585 rs_tl_turn_on_agg_for_tid(mvm, lq_data, tid, sta);
587 IWL_ERR(mvm, "tid exceeds max TID count: %d/%d\n",
588 tid, IWL_MAX_TID_COUNT);
591 static inline int get_num_of_ant_from_rate(u32 rate_n_flags)
593 return !!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
594 !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
595 !!(rate_n_flags & RATE_MCS_ANT_C_MSK);
599 * Static function to get the expected throughput from an iwl_scale_tbl_info
600 * that wraps a NULL pointer check
602 static s32 get_expected_tpt(struct iwl_scale_tbl_info *tbl, int rs_index)
604 if (tbl->expected_tpt)
605 return tbl->expected_tpt[rs_index];
610 * rs_collect_tx_data - Update the success/failure sliding window
612 * We keep a sliding window of the last 62 packets transmitted
613 * at this rate. window->data contains the bitmask of successful
616 static int _rs_collect_tx_data(struct iwl_scale_tbl_info *tbl,
617 int scale_index, int attempts, int successes,
618 struct iwl_rate_scale_data *window)
620 static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
623 /* Get expected throughput */
624 tpt = get_expected_tpt(tbl, scale_index);
627 * Keep track of only the latest 62 tx frame attempts in this rate's
628 * history window; anything older isn't really relevant any more.
629 * If we have filled up the sliding window, drop the oldest attempt;
630 * if the oldest attempt (highest bit in bitmap) shows "success",
631 * subtract "1" from the success counter (this is the main reason
632 * we keep these bitmaps!).
634 while (attempts > 0) {
635 if (window->counter >= IWL_RATE_MAX_WINDOW) {
636 /* remove earliest */
637 window->counter = IWL_RATE_MAX_WINDOW - 1;
639 if (window->data & mask) {
640 window->data &= ~mask;
641 window->success_counter--;
645 /* Increment frames-attempted counter */
648 /* Shift bitmap by one frame to throw away oldest history */
651 /* Mark the most recent #successes attempts as successful */
653 window->success_counter++;
661 /* Calculate current success ratio, avoid divide-by-0! */
662 if (window->counter > 0)
663 window->success_ratio = 128 * (100 * window->success_counter)
666 window->success_ratio = IWL_INVALID_VALUE;
668 fail_count = window->counter - window->success_counter;
670 /* Calculate average throughput, if we have enough history. */
671 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
672 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
673 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
675 window->average_tpt = IWL_INVALID_VALUE;
680 static int rs_collect_tx_data(struct iwl_lq_sta *lq_sta,
681 struct iwl_scale_tbl_info *tbl,
682 int scale_index, int attempts, int successes,
685 struct iwl_rate_scale_data *window = NULL;
688 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
691 if (tbl->column != RS_COLUMN_INVALID) {
692 struct lq_sta_pers *pers = &lq_sta->pers;
694 pers->tx_stats[tbl->column][scale_index].total += attempts;
695 pers->tx_stats[tbl->column][scale_index].success += successes;
698 /* Select window for current tx bit rate */
699 window = &(tbl->win[scale_index]);
701 ret = _rs_collect_tx_data(tbl, scale_index, attempts, successes,
706 if (WARN_ON_ONCE(reduced_txp > TPC_MAX_REDUCTION))
709 window = &tbl->tpc_win[reduced_txp];
710 return _rs_collect_tx_data(tbl, scale_index, attempts, successes,
714 /* Convert rs_rate object into ucode rate bitmask */
715 static u32 ucode_rate_from_rs_rate(struct iwl_mvm *mvm,
716 struct rs_rate *rate)
719 int index = rate->index;
721 ucode_rate |= ((rate->ant << RATE_MCS_ANT_POS) &
722 RATE_MCS_ANT_ABC_MSK);
724 if (is_legacy(rate)) {
725 ucode_rate |= iwl_rates[index].plcp;
726 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
727 ucode_rate |= RATE_MCS_CCK_MSK;
732 if (index < IWL_FIRST_HT_RATE || index > IWL_LAST_HT_RATE) {
733 IWL_ERR(mvm, "Invalid HT rate index %d\n", index);
734 index = IWL_LAST_HT_RATE;
736 ucode_rate |= RATE_MCS_HT_MSK;
738 if (is_ht_siso(rate))
739 ucode_rate |= iwl_rates[index].plcp_ht_siso;
740 else if (is_ht_mimo2(rate))
741 ucode_rate |= iwl_rates[index].plcp_ht_mimo2;
744 } else if (is_vht(rate)) {
745 if (index < IWL_FIRST_VHT_RATE || index > IWL_LAST_VHT_RATE) {
746 IWL_ERR(mvm, "Invalid VHT rate index %d\n", index);
747 index = IWL_LAST_VHT_RATE;
749 ucode_rate |= RATE_MCS_VHT_MSK;
750 if (is_vht_siso(rate))
751 ucode_rate |= iwl_rates[index].plcp_vht_siso;
752 else if (is_vht_mimo2(rate))
753 ucode_rate |= iwl_rates[index].plcp_vht_mimo2;
758 IWL_ERR(mvm, "Invalid rate->type %d\n", rate->type);
761 if (is_siso(rate) && rate->stbc) {
762 /* To enable STBC we need to set both a flag and ANT_AB */
763 ucode_rate |= RATE_MCS_ANT_AB_MSK;
764 ucode_rate |= RATE_MCS_VHT_STBC_MSK;
767 ucode_rate |= rate->bw;
769 ucode_rate |= RATE_MCS_SGI_MSK;
771 ucode_rate |= RATE_MCS_LDPC_MSK;
776 /* Convert a ucode rate into an rs_rate object */
777 static int rs_rate_from_ucode_rate(const u32 ucode_rate,
778 enum ieee80211_band band,
779 struct rs_rate *rate)
781 u32 ant_msk = ucode_rate & RATE_MCS_ANT_ABC_MSK;
782 u8 num_of_ant = get_num_of_ant_from_rate(ucode_rate);
785 memset(rate, 0, sizeof(*rate));
786 rate->index = iwl_hwrate_to_plcp_idx(ucode_rate);
788 if (rate->index == IWL_RATE_INVALID)
791 rate->ant = (ant_msk >> RATE_MCS_ANT_POS);
794 if (!(ucode_rate & RATE_MCS_HT_MSK) &&
795 !(ucode_rate & RATE_MCS_VHT_MSK)) {
796 if (num_of_ant == 1) {
797 if (band == IEEE80211_BAND_5GHZ)
798 rate->type = LQ_LEGACY_A;
800 rate->type = LQ_LEGACY_G;
807 if (ucode_rate & RATE_MCS_SGI_MSK)
809 if (ucode_rate & RATE_MCS_LDPC_MSK)
811 if (ucode_rate & RATE_MCS_VHT_STBC_MSK)
814 rate->bw = ucode_rate & RATE_MCS_CHAN_WIDTH_MSK;
816 if (ucode_rate & RATE_MCS_HT_MSK) {
817 nss = ((ucode_rate & RATE_HT_MCS_NSS_MSK) >>
818 RATE_HT_MCS_NSS_POS) + 1;
821 rate->type = LQ_HT_SISO;
822 WARN_ON_ONCE(!rate->stbc && num_of_ant != 1);
823 } else if (nss == 2) {
824 rate->type = LQ_HT_MIMO2;
825 WARN_ON_ONCE(num_of_ant != 2);
829 } else if (ucode_rate & RATE_MCS_VHT_MSK) {
830 nss = ((ucode_rate & RATE_VHT_MCS_NSS_MSK) >>
831 RATE_VHT_MCS_NSS_POS) + 1;
834 rate->type = LQ_VHT_SISO;
835 WARN_ON_ONCE(!rate->stbc && num_of_ant != 1);
836 } else if (nss == 2) {
837 rate->type = LQ_VHT_MIMO2;
838 WARN_ON_ONCE(num_of_ant != 2);
844 WARN_ON_ONCE(rate->bw == RATE_MCS_CHAN_WIDTH_160);
845 WARN_ON_ONCE(rate->bw == RATE_MCS_CHAN_WIDTH_80 &&
851 /* switch to another antenna/antennas and return 1 */
852 /* if no other valid antenna found, return 0 */
853 static int rs_toggle_antenna(u32 valid_ant, struct rs_rate *rate)
857 if (!rate->ant || rate->ant > ANT_ABC)
860 if (!rs_is_valid_ant(valid_ant, rate->ant))
863 new_ant_type = ant_toggle_lookup[rate->ant];
865 while ((new_ant_type != rate->ant) &&
866 !rs_is_valid_ant(valid_ant, new_ant_type))
867 new_ant_type = ant_toggle_lookup[new_ant_type];
869 if (new_ant_type == rate->ant)
872 rate->ant = new_ant_type;
877 static u16 rs_get_supported_rates(struct iwl_lq_sta *lq_sta,
878 struct rs_rate *rate)
881 return lq_sta->active_legacy_rate;
882 else if (is_siso(rate))
883 return lq_sta->active_siso_rate;
884 else if (is_mimo2(rate))
885 return lq_sta->active_mimo2_rate;
891 static u16 rs_get_adjacent_rate(struct iwl_mvm *mvm, u8 index, u16 rate_mask,
894 u8 high = IWL_RATE_INVALID;
895 u8 low = IWL_RATE_INVALID;
897 /* 802.11A or ht walks to the next literal adjacent rate in
899 if (is_type_a_band(rate_type) || !is_type_legacy(rate_type)) {
903 /* Find the previous rate that is in the rate mask */
905 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
906 if (rate_mask & mask) {
912 /* Find the next rate that is in the rate mask */
914 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
915 if (rate_mask & mask) {
921 return (high << 8) | low;
925 while (low != IWL_RATE_INVALID) {
926 low = iwl_rates[low].prev_rs;
927 if (low == IWL_RATE_INVALID)
929 if (rate_mask & (1 << low))
931 IWL_DEBUG_RATE(mvm, "Skipping masked lower rate: %d\n", low);
935 while (high != IWL_RATE_INVALID) {
936 high = iwl_rates[high].next_rs;
937 if (high == IWL_RATE_INVALID)
939 if (rate_mask & (1 << high))
941 IWL_DEBUG_RATE(mvm, "Skipping masked higher rate: %d\n", high);
944 return (high << 8) | low;
947 static inline bool rs_rate_supported(struct iwl_lq_sta *lq_sta,
948 struct rs_rate *rate)
950 return BIT(rate->index) & rs_get_supported_rates(lq_sta, rate);
953 /* Get the next supported lower rate in the current column.
954 * Return true if bottom rate in the current column was reached
956 static bool rs_get_lower_rate_in_column(struct iwl_lq_sta *lq_sta,
957 struct rs_rate *rate)
962 struct iwl_mvm *mvm = lq_sta->pers.drv;
964 rate_mask = rs_get_supported_rates(lq_sta, rate);
965 high_low = rs_get_adjacent_rate(mvm, rate->index, rate_mask,
967 low = high_low & 0xff;
969 /* Bottom rate of column reached */
970 if (low == IWL_RATE_INVALID)
977 /* Get the next rate to use following a column downgrade */
978 static void rs_get_lower_rate_down_column(struct iwl_lq_sta *lq_sta,
979 struct rs_rate *rate)
981 struct iwl_mvm *mvm = lq_sta->pers.drv;
983 if (is_legacy(rate)) {
984 /* No column to downgrade from Legacy */
986 } else if (is_siso(rate)) {
987 /* Downgrade to Legacy if we were in SISO */
988 if (lq_sta->band == IEEE80211_BAND_5GHZ)
989 rate->type = LQ_LEGACY_A;
991 rate->type = LQ_LEGACY_G;
993 rate->bw = RATE_MCS_CHAN_WIDTH_20;
995 WARN_ON_ONCE(rate->index < IWL_RATE_MCS_0_INDEX ||
996 rate->index > IWL_RATE_MCS_9_INDEX);
998 rate->index = rs_ht_to_legacy[rate->index];
1001 /* Downgrade to SISO with same MCS if in MIMO */
1002 rate->type = is_vht_mimo2(rate) ?
1003 LQ_VHT_SISO : LQ_HT_SISO;
1006 if (num_of_ant(rate->ant) > 1)
1007 rate->ant = first_antenna(iwl_mvm_get_valid_tx_ant(mvm));
1009 /* Relevant in both switching to SISO or Legacy */
1012 if (!rs_rate_supported(lq_sta, rate))
1013 rs_get_lower_rate_in_column(lq_sta, rate);
1016 /* Simple function to compare two rate scale table types */
1017 static inline bool rs_rate_match(struct rs_rate *a,
1023 ant_match = (b->ant == ANT_A || b->ant == ANT_B);
1025 ant_match = (a->ant == b->ant);
1027 return (a->type == b->type) && (a->bw == b->bw) && (a->sgi == b->sgi)
1031 static u32 rs_ch_width_from_mac_flags(enum mac80211_rate_control_flags flags)
1033 if (flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
1034 return RATE_MCS_CHAN_WIDTH_40;
1035 else if (flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
1036 return RATE_MCS_CHAN_WIDTH_80;
1037 else if (flags & IEEE80211_TX_RC_160_MHZ_WIDTH)
1038 return RATE_MCS_CHAN_WIDTH_160;
1040 return RATE_MCS_CHAN_WIDTH_20;
1043 static u8 rs_get_tid(struct ieee80211_hdr *hdr)
1045 u8 tid = IWL_MAX_TID_COUNT;
1047 if (ieee80211_is_data_qos(hdr->frame_control)) {
1048 u8 *qc = ieee80211_get_qos_ctl(hdr);
1052 if (unlikely(tid > IWL_MAX_TID_COUNT))
1053 tid = IWL_MAX_TID_COUNT;
1058 void iwl_mvm_rs_tx_status(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
1059 int tid, struct ieee80211_tx_info *info)
1064 struct iwl_lq_cmd *table;
1065 enum mac80211_rate_control_flags mac_flags;
1067 struct rs_rate rate;
1068 struct iwl_scale_tbl_info *curr_tbl, *other_tbl, *tmp_tbl;
1069 u8 reduced_txp = (uintptr_t)info->status.status_driver_data[0];
1070 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1071 struct iwl_lq_sta *lq_sta = &mvmsta->lq_sta;
1073 /* Treat uninitialized rate scaling data same as non-existing. */
1075 IWL_DEBUG_RATE(mvm, "Station rate scaling not created yet.\n");
1077 } else if (!lq_sta->pers.drv) {
1078 IWL_DEBUG_RATE(mvm, "Rate scaling not initialized yet.\n");
1082 #ifdef CONFIG_MAC80211_DEBUGFS
1083 /* Disable last tx check if we are debugging with fixed rate */
1084 if (lq_sta->pers.dbg_fixed_rate) {
1085 IWL_DEBUG_RATE(mvm, "Fixed rate. avoid rate scaling\n");
1089 /* This packet was aggregated but doesn't carry status info */
1090 if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
1091 !(info->flags & IEEE80211_TX_STAT_AMPDU))
1095 * Ignore this Tx frame response if its initial rate doesn't match
1096 * that of latest Link Quality command. There may be stragglers
1097 * from a previous Link Quality command, but we're no longer interested
1098 * in those; they're either from the "active" mode while we're trying
1099 * to check "search" mode, or a prior "search" mode after we've moved
1100 * to a new "search" mode (which might become the new "active" mode).
1102 table = &lq_sta->lq;
1103 ucode_rate = le32_to_cpu(table->rs_table[0]);
1104 rs_rate_from_ucode_rate(ucode_rate, info->band, &rate);
1105 if (info->band == IEEE80211_BAND_5GHZ)
1106 rate.index -= IWL_FIRST_OFDM_RATE;
1107 mac_flags = info->status.rates[0].flags;
1108 mac_index = info->status.rates[0].idx;
1109 /* For HT packets, map MCS to PLCP */
1110 if (mac_flags & IEEE80211_TX_RC_MCS) {
1111 /* Remove # of streams */
1112 mac_index &= RATE_HT_MCS_RATE_CODE_MSK;
1113 if (mac_index >= (IWL_RATE_9M_INDEX - IWL_FIRST_OFDM_RATE))
1116 * mac80211 HT index is always zero-indexed; we need to move
1117 * HT OFDM rates after CCK rates in 2.4 GHz band
1119 if (info->band == IEEE80211_BAND_2GHZ)
1120 mac_index += IWL_FIRST_OFDM_RATE;
1121 } else if (mac_flags & IEEE80211_TX_RC_VHT_MCS) {
1122 mac_index &= RATE_VHT_MCS_RATE_CODE_MSK;
1123 if (mac_index >= (IWL_RATE_9M_INDEX - IWL_FIRST_OFDM_RATE))
1127 if (time_after(jiffies,
1128 (unsigned long)(lq_sta->last_tx + RS_IDLE_TIMEOUT))) {
1131 IWL_DEBUG_RATE(mvm, "Tx idle for too long. reinit rs\n");
1132 for (t = 0; t < IWL_MAX_TID_COUNT; t++)
1133 ieee80211_stop_tx_ba_session(sta, t);
1135 iwl_mvm_rs_rate_init(mvm, sta, info->band, false);
1138 lq_sta->last_tx = jiffies;
1140 /* Here we actually compare this rate to the latest LQ command */
1141 if ((mac_index < 0) ||
1142 (rate.sgi != !!(mac_flags & IEEE80211_TX_RC_SHORT_GI)) ||
1143 (rate.bw != rs_ch_width_from_mac_flags(mac_flags)) ||
1144 (rate.ant != info->status.antenna) ||
1145 (!!(ucode_rate & RATE_MCS_HT_MSK) !=
1146 !!(mac_flags & IEEE80211_TX_RC_MCS)) ||
1147 (!!(ucode_rate & RATE_MCS_VHT_MSK) !=
1148 !!(mac_flags & IEEE80211_TX_RC_VHT_MCS)) ||
1149 (!!(ucode_rate & RATE_HT_MCS_GF_MSK) !=
1150 !!(mac_flags & IEEE80211_TX_RC_GREEN_FIELD)) ||
1151 (rate.index != mac_index)) {
1153 "initial rate %d does not match %d (0x%x)\n",
1154 mac_index, rate.index, ucode_rate);
1156 * Since rates mis-match, the last LQ command may have failed.
1157 * After IWL_MISSED_RATE_MAX mis-matches, resync the uCode with
1160 lq_sta->missed_rate_counter++;
1161 if (lq_sta->missed_rate_counter > IWL_MISSED_RATE_MAX) {
1162 lq_sta->missed_rate_counter = 0;
1164 "Too many rates mismatch. Send sync LQ. rs_state %d\n",
1166 iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, false);
1168 /* Regardless, ignore this status info for outdated rate */
1171 /* Rate did match, so reset the missed_rate_counter */
1172 lq_sta->missed_rate_counter = 0;
1174 if (!lq_sta->search_better_tbl) {
1175 curr_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1176 other_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
1178 curr_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
1179 other_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1182 if (WARN_ON_ONCE(!rs_rate_match(&rate, &curr_tbl->rate))) {
1184 "Neither active nor search matches tx rate\n");
1185 tmp_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1186 rs_dump_rate(mvm, &tmp_tbl->rate, "ACTIVE");
1187 tmp_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
1188 rs_dump_rate(mvm, &tmp_tbl->rate, "SEARCH");
1189 rs_dump_rate(mvm, &rate, "ACTUAL");
1192 * no matching table found, let's by-pass the data collection
1193 * and continue to perform rate scale to find the rate table
1195 rs_stay_in_table(lq_sta, true);
1200 * Updating the frame history depends on whether packets were
1203 * For aggregation, all packets were transmitted at the same rate, the
1204 * first index into rate scale table.
1206 if (info->flags & IEEE80211_TX_STAT_AMPDU) {
1207 /* ampdu_ack_len = 0 marks no BA was received. In this case
1208 * treat it as a single frame loss as we don't want the success
1209 * ratio to dip too quickly because a BA wasn't received
1211 if (info->status.ampdu_ack_len == 0)
1212 info->status.ampdu_len = 1;
1214 ucode_rate = le32_to_cpu(table->rs_table[0]);
1215 rs_rate_from_ucode_rate(ucode_rate, info->band, &rate);
1216 rs_collect_tx_data(lq_sta, curr_tbl, rate.index,
1217 info->status.ampdu_len,
1218 info->status.ampdu_ack_len,
1221 /* Update success/fail counts if not searching for new mode */
1222 if (lq_sta->rs_state == RS_STATE_STAY_IN_COLUMN) {
1223 lq_sta->total_success += info->status.ampdu_ack_len;
1224 lq_sta->total_failed += (info->status.ampdu_len -
1225 info->status.ampdu_ack_len);
1229 * For legacy, update frame history with for each Tx retry.
1231 retries = info->status.rates[0].count - 1;
1232 /* HW doesn't send more than 15 retries */
1233 retries = min(retries, 15);
1235 /* The last transmission may have been successful */
1236 legacy_success = !!(info->flags & IEEE80211_TX_STAT_ACK);
1237 /* Collect data for each rate used during failed TX attempts */
1238 for (i = 0; i <= retries; ++i) {
1239 ucode_rate = le32_to_cpu(table->rs_table[i]);
1240 rs_rate_from_ucode_rate(ucode_rate, info->band, &rate);
1242 * Only collect stats if retried rate is in the same RS
1243 * table as active/search.
1245 if (rs_rate_match(&rate, &curr_tbl->rate))
1247 else if (rs_rate_match(&rate, &other_tbl->rate))
1248 tmp_tbl = other_tbl;
1252 rs_collect_tx_data(lq_sta, tmp_tbl, rate.index, 1,
1253 i < retries ? 0 : legacy_success,
1257 /* Update success/fail counts if not searching for new mode */
1258 if (lq_sta->rs_state == RS_STATE_STAY_IN_COLUMN) {
1259 lq_sta->total_success += legacy_success;
1260 lq_sta->total_failed += retries + (1 - legacy_success);
1263 /* The last TX rate is cached in lq_sta; it's set in if/else above */
1264 lq_sta->last_rate_n_flags = ucode_rate;
1265 IWL_DEBUG_RATE(mvm, "reduced txpower: %d\n", reduced_txp);
1267 /* See if there's a better rate or modulation mode to try. */
1268 if (sta->supp_rates[info->band])
1269 rs_rate_scale_perform(mvm, sta, lq_sta, tid);
1273 * mac80211 sends us Tx status
1275 static void rs_mac80211_tx_status(void *mvm_r,
1276 struct ieee80211_supported_band *sband,
1277 struct ieee80211_sta *sta, void *priv_sta,
1278 struct sk_buff *skb)
1280 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1281 struct iwl_op_mode *op_mode = (struct iwl_op_mode *)mvm_r;
1282 struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1283 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1285 if (!ieee80211_is_data(hdr->frame_control) ||
1286 info->flags & IEEE80211_TX_CTL_NO_ACK)
1289 iwl_mvm_rs_tx_status(mvm, sta, rs_get_tid(hdr), info);
1293 * Begin a period of staying with a selected modulation mode.
1294 * Set "stay_in_tbl" flag to prevent any mode switches.
1295 * Set frame tx success limits according to legacy vs. high-throughput,
1296 * and reset overall (spanning all rates) tx success history statistics.
1297 * These control how long we stay using same modulation mode before
1298 * searching for a new mode.
1300 static void rs_set_stay_in_table(struct iwl_mvm *mvm, u8 is_legacy,
1301 struct iwl_lq_sta *lq_sta)
1303 IWL_DEBUG_RATE(mvm, "Moving to RS_STATE_STAY_IN_COLUMN\n");
1304 lq_sta->rs_state = RS_STATE_STAY_IN_COLUMN;
1306 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
1307 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
1308 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
1310 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
1311 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
1312 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
1314 lq_sta->table_count = 0;
1315 lq_sta->total_failed = 0;
1316 lq_sta->total_success = 0;
1317 lq_sta->flush_timer = jiffies;
1318 lq_sta->visited_columns = 0;
1321 static inline int rs_get_max_rate_from_mask(unsigned long rate_mask)
1324 return find_last_bit(&rate_mask, BITS_PER_LONG);
1325 return IWL_RATE_INVALID;
1328 static int rs_get_max_allowed_rate(struct iwl_lq_sta *lq_sta,
1329 const struct rs_tx_column *column)
1331 switch (column->mode) {
1333 return lq_sta->max_legacy_rate_idx;
1335 return lq_sta->max_siso_rate_idx;
1337 return lq_sta->max_mimo2_rate_idx;
1342 return lq_sta->max_legacy_rate_idx;
1345 static const u16 *rs_get_expected_tpt_table(struct iwl_lq_sta *lq_sta,
1346 const struct rs_tx_column *column,
1349 /* Used to choose among HT tables */
1350 const u16 (*ht_tbl_pointer)[IWL_RATE_COUNT];
1352 if (WARN_ON_ONCE(column->mode != RS_LEGACY &&
1353 column->mode != RS_SISO &&
1354 column->mode != RS_MIMO2))
1355 return expected_tpt_legacy;
1357 /* Legacy rates have only one table */
1358 if (column->mode == RS_LEGACY)
1359 return expected_tpt_legacy;
1361 ht_tbl_pointer = expected_tpt_mimo2_20MHz;
1362 /* Choose among many HT tables depending on number of streams
1363 * (SISO/MIMO2), channel width (20/40/80), SGI, and aggregation
1365 if (column->mode == RS_SISO) {
1367 case RATE_MCS_CHAN_WIDTH_20:
1368 ht_tbl_pointer = expected_tpt_siso_20MHz;
1370 case RATE_MCS_CHAN_WIDTH_40:
1371 ht_tbl_pointer = expected_tpt_siso_40MHz;
1373 case RATE_MCS_CHAN_WIDTH_80:
1374 ht_tbl_pointer = expected_tpt_siso_80MHz;
1379 } else if (column->mode == RS_MIMO2) {
1381 case RATE_MCS_CHAN_WIDTH_20:
1382 ht_tbl_pointer = expected_tpt_mimo2_20MHz;
1384 case RATE_MCS_CHAN_WIDTH_40:
1385 ht_tbl_pointer = expected_tpt_mimo2_40MHz;
1387 case RATE_MCS_CHAN_WIDTH_80:
1388 ht_tbl_pointer = expected_tpt_mimo2_80MHz;
1397 if (!column->sgi && !lq_sta->is_agg) /* Normal */
1398 return ht_tbl_pointer[0];
1399 else if (column->sgi && !lq_sta->is_agg) /* SGI */
1400 return ht_tbl_pointer[1];
1401 else if (!column->sgi && lq_sta->is_agg) /* AGG */
1402 return ht_tbl_pointer[2];
1404 return ht_tbl_pointer[3];
1407 static void rs_set_expected_tpt_table(struct iwl_lq_sta *lq_sta,
1408 struct iwl_scale_tbl_info *tbl)
1410 struct rs_rate *rate = &tbl->rate;
1411 const struct rs_tx_column *column = &rs_tx_columns[tbl->column];
1413 tbl->expected_tpt = rs_get_expected_tpt_table(lq_sta, column, rate->bw);
1416 static s32 rs_get_best_rate(struct iwl_mvm *mvm,
1417 struct iwl_lq_sta *lq_sta,
1418 struct iwl_scale_tbl_info *tbl, /* "search" */
1419 unsigned long rate_mask, s8 index)
1421 struct iwl_scale_tbl_info *active_tbl =
1422 &(lq_sta->lq_info[lq_sta->active_tbl]);
1423 s32 success_ratio = active_tbl->win[index].success_ratio;
1424 u16 expected_current_tpt = active_tbl->expected_tpt[index];
1425 const u16 *tpt_tbl = tbl->expected_tpt;
1430 if (success_ratio > RS_SR_NO_DECREASE) {
1431 target_tpt = 100 * expected_current_tpt;
1433 "SR %d high. Find rate exceeding EXPECTED_CURRENT %d\n",
1434 success_ratio, target_tpt);
1436 target_tpt = lq_sta->last_tpt;
1438 "SR %d not thag good. Find rate exceeding ACTUAL_TPT %d\n",
1439 success_ratio, target_tpt);
1442 rate_idx = find_first_bit(&rate_mask, BITS_PER_LONG);
1444 while (rate_idx != IWL_RATE_INVALID) {
1445 if (target_tpt < (100 * tpt_tbl[rate_idx]))
1448 high_low = rs_get_adjacent_rate(mvm, rate_idx, rate_mask,
1451 rate_idx = (high_low >> 8) & 0xff;
1454 IWL_DEBUG_RATE(mvm, "Best rate found %d target_tp %d expected_new %d\n",
1455 rate_idx, target_tpt,
1456 rate_idx != IWL_RATE_INVALID ?
1457 100 * tpt_tbl[rate_idx] : IWL_INVALID_VALUE);
1462 static u32 rs_bw_from_sta_bw(struct ieee80211_sta *sta)
1464 if (sta->bandwidth >= IEEE80211_STA_RX_BW_80)
1465 return RATE_MCS_CHAN_WIDTH_80;
1466 else if (sta->bandwidth >= IEEE80211_STA_RX_BW_40)
1467 return RATE_MCS_CHAN_WIDTH_40;
1469 return RATE_MCS_CHAN_WIDTH_20;
1473 * Check whether we should continue using same modulation mode, or
1474 * begin search for a new mode, based on:
1475 * 1) # tx successes or failures while using this mode
1476 * 2) # times calling this function
1477 * 3) elapsed time in this mode (not used, for now)
1479 static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search)
1481 struct iwl_scale_tbl_info *tbl;
1483 int flush_interval_passed = 0;
1484 struct iwl_mvm *mvm;
1486 mvm = lq_sta->pers.drv;
1487 active_tbl = lq_sta->active_tbl;
1489 tbl = &(lq_sta->lq_info[active_tbl]);
1491 /* If we've been disallowing search, see if we should now allow it */
1492 if (lq_sta->rs_state == RS_STATE_STAY_IN_COLUMN) {
1493 /* Elapsed time using current modulation mode */
1494 if (lq_sta->flush_timer)
1495 flush_interval_passed =
1497 (unsigned long)(lq_sta->flush_timer +
1498 RS_STAY_IN_COLUMN_TIMEOUT));
1501 * Check if we should allow search for new modulation mode.
1502 * If many frames have failed or succeeded, or we've used
1503 * this same modulation for a long time, allow search, and
1504 * reset history stats that keep track of whether we should
1505 * allow a new search. Also (below) reset all bitmaps and
1506 * stats in active history.
1509 (lq_sta->total_failed > lq_sta->max_failure_limit) ||
1510 (lq_sta->total_success > lq_sta->max_success_limit) ||
1511 ((!lq_sta->search_better_tbl) &&
1512 (lq_sta->flush_timer) && (flush_interval_passed))) {
1514 "LQ: stay is expired %d %d %d\n",
1515 lq_sta->total_failed,
1516 lq_sta->total_success,
1517 flush_interval_passed);
1519 /* Allow search for new mode */
1520 lq_sta->rs_state = RS_STATE_SEARCH_CYCLE_STARTED;
1522 "Moving to RS_STATE_SEARCH_CYCLE_STARTED\n");
1523 lq_sta->total_failed = 0;
1524 lq_sta->total_success = 0;
1525 lq_sta->flush_timer = 0;
1526 /* mark the current column as visited */
1527 lq_sta->visited_columns = BIT(tbl->column);
1529 * Else if we've used this modulation mode enough repetitions
1530 * (regardless of elapsed time or success/failure), reset
1531 * history bitmaps and rate-specific stats for all rates in
1535 lq_sta->table_count++;
1536 if (lq_sta->table_count >=
1537 lq_sta->table_count_limit) {
1538 lq_sta->table_count = 0;
1541 "LQ: stay in table clear win\n");
1542 rs_rate_scale_clear_tbl_windows(mvm, tbl);
1546 /* If transitioning to allow "search", reset all history
1547 * bitmaps and stats in active table (this will become the new
1548 * "search" table). */
1549 if (lq_sta->rs_state == RS_STATE_SEARCH_CYCLE_STARTED) {
1550 rs_rate_scale_clear_tbl_windows(mvm, tbl);
1556 * setup rate table in uCode
1558 static void rs_update_rate_tbl(struct iwl_mvm *mvm,
1559 struct ieee80211_sta *sta,
1560 struct iwl_lq_sta *lq_sta,
1561 struct rs_rate *rate)
1563 rs_fill_lq_cmd(mvm, sta, lq_sta, rate);
1564 iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, false);
1567 static enum rs_column rs_get_next_column(struct iwl_mvm *mvm,
1568 struct iwl_lq_sta *lq_sta,
1569 struct ieee80211_sta *sta,
1570 struct iwl_scale_tbl_info *tbl)
1573 enum rs_column next_col_id;
1574 const struct rs_tx_column *curr_col = &rs_tx_columns[tbl->column];
1575 const struct rs_tx_column *next_col;
1576 allow_column_func_t allow_func;
1577 u8 valid_ants = iwl_mvm_get_valid_tx_ant(mvm);
1578 const u16 *expected_tpt_tbl;
1579 u16 tpt, max_expected_tpt;
1581 for (i = 0; i < MAX_NEXT_COLUMNS; i++) {
1582 next_col_id = curr_col->next_columns[i];
1584 if (next_col_id == RS_COLUMN_INVALID)
1587 if (lq_sta->visited_columns & BIT(next_col_id)) {
1588 IWL_DEBUG_RATE(mvm, "Skip already visited column %d\n",
1593 next_col = &rs_tx_columns[next_col_id];
1595 if (!rs_is_valid_ant(valid_ants, next_col->ant)) {
1597 "Skip column %d as ANT config isn't supported by chip. valid_ants 0x%x column ant 0x%x\n",
1598 next_col_id, valid_ants, next_col->ant);
1602 for (j = 0; j < MAX_COLUMN_CHECKS; j++) {
1603 allow_func = next_col->checks[j];
1604 if (allow_func && !allow_func(mvm, sta, tbl))
1608 if (j != MAX_COLUMN_CHECKS) {
1610 "Skip column %d: not allowed (check %d failed)\n",
1616 tpt = lq_sta->last_tpt / 100;
1617 expected_tpt_tbl = rs_get_expected_tpt_table(lq_sta, next_col,
1618 rs_bw_from_sta_bw(sta));
1619 if (WARN_ON_ONCE(!expected_tpt_tbl))
1622 max_rate = rs_get_max_allowed_rate(lq_sta, next_col);
1623 if (max_rate == IWL_RATE_INVALID) {
1625 "Skip column %d: no rate is allowed in this column\n",
1630 max_expected_tpt = expected_tpt_tbl[max_rate];
1631 if (tpt >= max_expected_tpt) {
1633 "Skip column %d: can't beat current TPT. Max expected %d current %d\n",
1634 next_col_id, max_expected_tpt, tpt);
1639 "Found potential column %d. Max expected %d current %d\n",
1640 next_col_id, max_expected_tpt, tpt);
1644 if (i == MAX_NEXT_COLUMNS)
1645 return RS_COLUMN_INVALID;
1650 static int rs_switch_to_column(struct iwl_mvm *mvm,
1651 struct iwl_lq_sta *lq_sta,
1652 struct ieee80211_sta *sta,
1653 enum rs_column col_id)
1655 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1656 struct iwl_scale_tbl_info *search_tbl =
1657 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1658 struct rs_rate *rate = &search_tbl->rate;
1659 const struct rs_tx_column *column = &rs_tx_columns[col_id];
1660 const struct rs_tx_column *curr_column = &rs_tx_columns[tbl->column];
1661 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1662 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1663 unsigned long rate_mask = 0;
1666 memcpy(search_tbl, tbl, sz);
1668 rate->sgi = column->sgi;
1669 rate->ant = column->ant;
1671 if (column->mode == RS_LEGACY) {
1672 if (lq_sta->band == IEEE80211_BAND_5GHZ)
1673 rate->type = LQ_LEGACY_A;
1675 rate->type = LQ_LEGACY_G;
1677 rate->bw = RATE_MCS_CHAN_WIDTH_20;
1679 rate_mask = lq_sta->active_legacy_rate;
1680 } else if (column->mode == RS_SISO) {
1681 rate->type = lq_sta->is_vht ? LQ_VHT_SISO : LQ_HT_SISO;
1682 rate_mask = lq_sta->active_siso_rate;
1683 } else if (column->mode == RS_MIMO2) {
1684 rate->type = lq_sta->is_vht ? LQ_VHT_MIMO2 : LQ_HT_MIMO2;
1685 rate_mask = lq_sta->active_mimo2_rate;
1687 WARN_ON_ONCE("Bad column mode");
1690 if (column->mode != RS_LEGACY) {
1691 rate->bw = rs_bw_from_sta_bw(sta);
1692 rate->ldpc = lq_sta->ldpc;
1695 search_tbl->column = col_id;
1696 rs_set_expected_tpt_table(lq_sta, search_tbl);
1698 lq_sta->visited_columns |= BIT(col_id);
1700 /* Get the best matching rate if we're changing modes. e.g.
1701 * SISO->MIMO, LEGACY->SISO, MIMO->SISO
1703 if (curr_column->mode != column->mode) {
1704 rate_idx = rs_get_best_rate(mvm, lq_sta, search_tbl,
1705 rate_mask, rate->index);
1707 if ((rate_idx == IWL_RATE_INVALID) ||
1708 !(BIT(rate_idx) & rate_mask)) {
1710 "can not switch with index %d"
1712 rate_idx, rate_mask);
1717 rate->index = rate_idx;
1720 IWL_DEBUG_RATE(mvm, "Switched to column %d: Index %d\n",
1721 col_id, rate->index);
1726 rate->type = LQ_NONE;
1730 static enum rs_action rs_get_rate_action(struct iwl_mvm *mvm,
1731 struct iwl_scale_tbl_info *tbl,
1732 s32 sr, int low, int high,
1734 int low_tpt, int high_tpt)
1736 enum rs_action action = RS_ACTION_STAY;
1738 if ((sr <= RS_SR_FORCE_DECREASE) || (current_tpt == 0)) {
1740 "Decrease rate because of low SR\n");
1741 return RS_ACTION_DOWNSCALE;
1744 if ((low_tpt == IWL_INVALID_VALUE) &&
1745 (high_tpt == IWL_INVALID_VALUE) &&
1746 (high != IWL_RATE_INVALID)) {
1748 "No data about high/low rates. Increase rate\n");
1749 return RS_ACTION_UPSCALE;
1752 if ((high_tpt == IWL_INVALID_VALUE) &&
1753 (high != IWL_RATE_INVALID) &&
1754 (low_tpt != IWL_INVALID_VALUE) &&
1755 (low_tpt < current_tpt)) {
1757 "No data about high rate and low rate is worse. Increase rate\n");
1758 return RS_ACTION_UPSCALE;
1761 if ((high_tpt != IWL_INVALID_VALUE) &&
1762 (high_tpt > current_tpt)) {
1764 "Higher rate is better. Increate rate\n");
1765 return RS_ACTION_UPSCALE;
1768 if ((low_tpt != IWL_INVALID_VALUE) &&
1769 (high_tpt != IWL_INVALID_VALUE) &&
1770 (low_tpt < current_tpt) &&
1771 (high_tpt < current_tpt)) {
1773 "Both high and low are worse. Maintain rate\n");
1774 return RS_ACTION_STAY;
1777 if ((low_tpt != IWL_INVALID_VALUE) &&
1778 (low_tpt > current_tpt)) {
1780 "Lower rate is better\n");
1781 action = RS_ACTION_DOWNSCALE;
1785 if ((low_tpt == IWL_INVALID_VALUE) &&
1786 (low != IWL_RATE_INVALID)) {
1788 "No data about lower rate\n");
1789 action = RS_ACTION_DOWNSCALE;
1793 IWL_DEBUG_RATE(mvm, "Maintain rate\n");
1796 if ((action == RS_ACTION_DOWNSCALE) && (low != IWL_RATE_INVALID)) {
1797 if (sr >= RS_SR_NO_DECREASE) {
1799 "SR is above NO DECREASE. Avoid downscale\n");
1800 action = RS_ACTION_STAY;
1801 } else if (current_tpt > (100 * tbl->expected_tpt[low])) {
1803 "Current TPT is higher than max expected in low rate. Avoid downscale\n");
1804 action = RS_ACTION_STAY;
1806 IWL_DEBUG_RATE(mvm, "Decrease rate\n");
1813 static bool rs_stbc_allow(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
1814 struct iwl_lq_sta *lq_sta)
1816 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1817 struct ieee80211_vif *vif = mvmsta->vif;
1818 bool sta_ps_disabled = (vif->type == NL80211_IFTYPE_STATION &&
1821 /* Our chip supports Tx STBC and the peer is an HT/VHT STA which
1822 * supports STBC of at least 1*SS
1827 if (!mvm->ps_disabled && !sta_ps_disabled)
1830 if (!iwl_mvm_bt_coex_is_mimo_allowed(mvm, sta))
1836 static void rs_get_adjacent_txp(struct iwl_mvm *mvm, int index,
1837 int *weaker, int *stronger)
1839 *weaker = index + TPC_TX_POWER_STEP;
1840 if (*weaker > TPC_MAX_REDUCTION)
1841 *weaker = TPC_INVALID;
1843 *stronger = index - TPC_TX_POWER_STEP;
1845 *stronger = TPC_INVALID;
1848 static bool rs_tpc_allowed(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1849 struct rs_rate *rate, enum ieee80211_band band)
1851 int index = rate->index;
1852 bool cam = (iwlmvm_mod_params.power_scheme == IWL_POWER_SCHEME_CAM);
1853 bool sta_ps_disabled = (vif->type == NL80211_IFTYPE_STATION &&
1856 IWL_DEBUG_RATE(mvm, "cam: %d sta_ps_disabled %d\n",
1857 cam, sta_ps_disabled);
1859 * allow tpc only if power management is enabled, or bt coex
1860 * activity grade allows it and we are on 2.4Ghz.
1862 if ((cam || sta_ps_disabled) &&
1863 !iwl_mvm_bt_coex_is_tpc_allowed(mvm, band))
1866 IWL_DEBUG_RATE(mvm, "check rate, table type: %d\n", rate->type);
1867 if (is_legacy(rate))
1868 return index == IWL_RATE_54M_INDEX;
1870 return index == IWL_RATE_MCS_7_INDEX;
1872 return index == IWL_RATE_MCS_7_INDEX ||
1873 index == IWL_RATE_MCS_8_INDEX ||
1874 index == IWL_RATE_MCS_9_INDEX;
1882 TPC_ACTION_DECREASE,
1883 TPC_ACTION_INCREASE,
1884 TPC_ACTION_NO_RESTIRCTION,
1887 static enum tpc_action rs_get_tpc_action(struct iwl_mvm *mvm,
1888 s32 sr, int weak, int strong,
1890 int weak_tpt, int strong_tpt)
1892 /* stay until we have valid tpt */
1893 if (current_tpt == IWL_INVALID_VALUE) {
1894 IWL_DEBUG_RATE(mvm, "no current tpt. stay.\n");
1895 return TPC_ACTION_STAY;
1898 /* Too many failures, increase txp */
1899 if (sr <= TPC_SR_FORCE_INCREASE || current_tpt == 0) {
1900 IWL_DEBUG_RATE(mvm, "increase txp because of weak SR\n");
1901 return TPC_ACTION_NO_RESTIRCTION;
1904 /* try decreasing first if applicable */
1905 if (weak != TPC_INVALID) {
1906 if (weak_tpt == IWL_INVALID_VALUE &&
1907 (strong_tpt == IWL_INVALID_VALUE ||
1908 current_tpt >= strong_tpt)) {
1910 "no weak txp measurement. decrease txp\n");
1911 return TPC_ACTION_DECREASE;
1914 if (weak_tpt > current_tpt) {
1916 "lower txp has better tpt. decrease txp\n");
1917 return TPC_ACTION_DECREASE;
1921 /* next, increase if needed */
1922 if (sr < TPC_SR_NO_INCREASE && strong != TPC_INVALID) {
1923 if (weak_tpt == IWL_INVALID_VALUE &&
1924 strong_tpt != IWL_INVALID_VALUE &&
1925 current_tpt < strong_tpt) {
1927 "higher txp has better tpt. increase txp\n");
1928 return TPC_ACTION_INCREASE;
1931 if (weak_tpt < current_tpt &&
1932 (strong_tpt == IWL_INVALID_VALUE ||
1933 strong_tpt > current_tpt)) {
1935 "lower txp has worse tpt. increase txp\n");
1936 return TPC_ACTION_INCREASE;
1940 IWL_DEBUG_RATE(mvm, "no need to increase or decrease txp - stay\n");
1941 return TPC_ACTION_STAY;
1944 static bool rs_tpc_perform(struct iwl_mvm *mvm,
1945 struct ieee80211_sta *sta,
1946 struct iwl_lq_sta *lq_sta,
1947 struct iwl_scale_tbl_info *tbl)
1949 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
1950 struct ieee80211_vif *vif = mvm_sta->vif;
1951 struct ieee80211_chanctx_conf *chanctx_conf;
1952 enum ieee80211_band band;
1953 struct iwl_rate_scale_data *window;
1954 struct rs_rate *rate = &tbl->rate;
1955 enum tpc_action action;
1957 u8 cur = lq_sta->lq.reduced_tpc;
1960 int weak_tpt = IWL_INVALID_VALUE, strong_tpt = IWL_INVALID_VALUE;
1962 #ifdef CONFIG_MAC80211_DEBUGFS
1963 if (lq_sta->pers.dbg_fixed_txp_reduction <= TPC_MAX_REDUCTION) {
1964 IWL_DEBUG_RATE(mvm, "fixed tpc: %d\n",
1965 lq_sta->pers.dbg_fixed_txp_reduction);
1966 lq_sta->lq.reduced_tpc = lq_sta->pers.dbg_fixed_txp_reduction;
1967 return cur != lq_sta->pers.dbg_fixed_txp_reduction;
1972 chanctx_conf = rcu_dereference(vif->chanctx_conf);
1973 if (WARN_ON(!chanctx_conf))
1974 band = IEEE80211_NUM_BANDS;
1976 band = chanctx_conf->def.chan->band;
1979 if (!rs_tpc_allowed(mvm, vif, rate, band)) {
1981 "tpc is not allowed. remove txp restrictions\n");
1982 lq_sta->lq.reduced_tpc = TPC_NO_REDUCTION;
1983 return cur != TPC_NO_REDUCTION;
1986 rs_get_adjacent_txp(mvm, cur, &weak, &strong);
1988 /* Collect measured throughputs for current and adjacent rates */
1989 window = tbl->tpc_win;
1990 sr = window[cur].success_ratio;
1991 current_tpt = window[cur].average_tpt;
1992 if (weak != TPC_INVALID)
1993 weak_tpt = window[weak].average_tpt;
1994 if (strong != TPC_INVALID)
1995 strong_tpt = window[strong].average_tpt;
1998 "(TPC: %d): cur_tpt %d SR %d weak %d strong %d weak_tpt %d strong_tpt %d\n",
1999 cur, current_tpt, sr, weak, strong,
2000 weak_tpt, strong_tpt);
2002 action = rs_get_tpc_action(mvm, sr, weak, strong,
2003 current_tpt, weak_tpt, strong_tpt);
2005 /* override actions if we are on the edge */
2006 if (weak == TPC_INVALID && action == TPC_ACTION_DECREASE) {
2007 IWL_DEBUG_RATE(mvm, "already in lowest txp, stay\n");
2008 action = TPC_ACTION_STAY;
2009 } else if (strong == TPC_INVALID &&
2010 (action == TPC_ACTION_INCREASE ||
2011 action == TPC_ACTION_NO_RESTIRCTION)) {
2012 IWL_DEBUG_RATE(mvm, "already in highest txp, stay\n");
2013 action = TPC_ACTION_STAY;
2017 case TPC_ACTION_DECREASE:
2018 lq_sta->lq.reduced_tpc = weak;
2020 case TPC_ACTION_INCREASE:
2021 lq_sta->lq.reduced_tpc = strong;
2023 case TPC_ACTION_NO_RESTIRCTION:
2024 lq_sta->lq.reduced_tpc = TPC_NO_REDUCTION;
2026 case TPC_ACTION_STAY:
2034 * Do rate scaling and search for new modulation mode.
2036 static void rs_rate_scale_perform(struct iwl_mvm *mvm,
2037 struct ieee80211_sta *sta,
2038 struct iwl_lq_sta *lq_sta,
2041 int low = IWL_RATE_INVALID;
2042 int high = IWL_RATE_INVALID;
2044 struct iwl_rate_scale_data *window = NULL;
2045 int current_tpt = IWL_INVALID_VALUE;
2046 int low_tpt = IWL_INVALID_VALUE;
2047 int high_tpt = IWL_INVALID_VALUE;
2049 enum rs_action scale_action = RS_ACTION_STAY;
2052 struct iwl_scale_tbl_info *tbl, *tbl1;
2057 u8 prev_agg = lq_sta->is_agg;
2058 struct iwl_mvm_sta *sta_priv = (void *)sta->drv_priv;
2059 struct iwl_mvm_tid_data *tid_data;
2060 struct rs_rate *rate;
2062 lq_sta->is_agg = !!sta_priv->agg_tids;
2065 * Select rate-scale / modulation-mode table to work with in
2066 * the rest of this function: "search" if searching for better
2067 * modulation mode, or "active" if doing rate scaling within a mode.
2069 if (!lq_sta->search_better_tbl)
2070 active_tbl = lq_sta->active_tbl;
2072 active_tbl = 1 - lq_sta->active_tbl;
2074 tbl = &(lq_sta->lq_info[active_tbl]);
2077 if (prev_agg != lq_sta->is_agg) {
2079 "Aggregation changed: prev %d current %d. Update expected TPT table\n",
2080 prev_agg, lq_sta->is_agg);
2081 rs_set_expected_tpt_table(lq_sta, tbl);
2082 rs_rate_scale_clear_tbl_windows(mvm, tbl);
2085 /* current tx rate */
2086 index = lq_sta->last_txrate_idx;
2088 /* rates available for this association, and for modulation mode */
2089 rate_mask = rs_get_supported_rates(lq_sta, rate);
2091 if (!(BIT(index) & rate_mask)) {
2092 IWL_ERR(mvm, "Current Rate is not valid\n");
2093 if (lq_sta->search_better_tbl) {
2094 /* revert to active table if search table is not valid*/
2095 rate->type = LQ_NONE;
2096 lq_sta->search_better_tbl = 0;
2097 tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
2098 rs_update_rate_tbl(mvm, sta, lq_sta, &tbl->rate);
2103 /* Get expected throughput table and history window for current rate */
2104 if (!tbl->expected_tpt) {
2105 IWL_ERR(mvm, "tbl->expected_tpt is NULL\n");
2109 /* TODO: handle rate_idx_mask and rate_idx_mcs_mask */
2110 window = &(tbl->win[index]);
2113 * If there is not enough history to calculate actual average
2114 * throughput, keep analyzing results of more tx frames, without
2115 * changing rate or mode (bypass most of the rest of this function).
2116 * Set up new rate table in uCode only if old rate is not supported
2117 * in current association (use new rate found above).
2119 fail_count = window->counter - window->success_counter;
2120 if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
2121 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
2123 "(%s: %d): Test Window: succ %d total %d\n",
2124 rs_pretty_lq_type(rate->type),
2125 index, window->success_counter, window->counter);
2127 /* Can't calculate this yet; not enough history */
2128 window->average_tpt = IWL_INVALID_VALUE;
2130 /* Should we stay with this modulation mode,
2131 * or search for a new one? */
2132 rs_stay_in_table(lq_sta, false);
2136 /* Else we have enough samples; calculate estimate of
2137 * actual average throughput */
2138 if (window->average_tpt != ((window->success_ratio *
2139 tbl->expected_tpt[index] + 64) / 128)) {
2140 window->average_tpt = ((window->success_ratio *
2141 tbl->expected_tpt[index] + 64) / 128);
2144 /* If we are searching for better modulation mode, check success. */
2145 if (lq_sta->search_better_tbl) {
2146 /* If good success, continue using the "search" mode;
2147 * no need to send new link quality command, since we're
2148 * continuing to use the setup that we've been trying. */
2149 if (window->average_tpt > lq_sta->last_tpt) {
2151 "SWITCHING TO NEW TABLE SR: %d "
2152 "cur-tpt %d old-tpt %d\n",
2153 window->success_ratio,
2154 window->average_tpt,
2157 /* Swap tables; "search" becomes "active" */
2158 lq_sta->active_tbl = active_tbl;
2159 current_tpt = window->average_tpt;
2160 /* Else poor success; go back to mode in "active" table */
2163 "GOING BACK TO THE OLD TABLE: SR %d "
2164 "cur-tpt %d old-tpt %d\n",
2165 window->success_ratio,
2166 window->average_tpt,
2169 /* Nullify "search" table */
2170 rate->type = LQ_NONE;
2172 /* Revert to "active" table */
2173 active_tbl = lq_sta->active_tbl;
2174 tbl = &(lq_sta->lq_info[active_tbl]);
2176 /* Revert to "active" rate and throughput info */
2177 index = tbl->rate.index;
2178 current_tpt = lq_sta->last_tpt;
2180 /* Need to set up a new rate table in uCode */
2184 /* Either way, we've made a decision; modulation mode
2185 * search is done, allow rate adjustment next time. */
2186 lq_sta->search_better_tbl = 0;
2187 done_search = 1; /* Don't switch modes below! */
2191 /* (Else) not in search of better modulation mode, try for better
2192 * starting rate, while staying in this mode. */
2193 high_low = rs_get_adjacent_rate(mvm, index, rate_mask, rate->type);
2194 low = high_low & 0xff;
2195 high = (high_low >> 8) & 0xff;
2197 /* TODO: handle rate_idx_mask and rate_idx_mcs_mask */
2199 sr = window->success_ratio;
2201 /* Collect measured throughputs for current and adjacent rates */
2202 current_tpt = window->average_tpt;
2203 if (low != IWL_RATE_INVALID)
2204 low_tpt = tbl->win[low].average_tpt;
2205 if (high != IWL_RATE_INVALID)
2206 high_tpt = tbl->win[high].average_tpt;
2209 "(%s: %d): cur_tpt %d SR %d low %d high %d low_tpt %d high_tpt %d\n",
2210 rs_pretty_lq_type(rate->type), index, current_tpt, sr,
2211 low, high, low_tpt, high_tpt);
2213 scale_action = rs_get_rate_action(mvm, tbl, sr, low, high,
2214 current_tpt, low_tpt, high_tpt);
2216 /* Force a search in case BT doesn't like us being in MIMO */
2217 if (is_mimo(rate) &&
2218 !iwl_mvm_bt_coex_is_mimo_allowed(mvm, sta)) {
2220 "BT Coex forbids MIMO. Search for new config\n");
2221 rs_stay_in_table(lq_sta, true);
2225 switch (scale_action) {
2226 case RS_ACTION_DOWNSCALE:
2227 /* Decrease starting rate, update uCode's rate table */
2228 if (low != IWL_RATE_INVALID) {
2233 "At the bottom rate. Can't decrease\n");
2237 case RS_ACTION_UPSCALE:
2238 /* Increase starting rate, update uCode's rate table */
2239 if (high != IWL_RATE_INVALID) {
2244 "At the top rate. Can't increase\n");
2248 case RS_ACTION_STAY:
2250 if (lq_sta->rs_state == RS_STATE_STAY_IN_COLUMN)
2251 update_lq = rs_tpc_perform(mvm, sta, lq_sta, tbl);
2258 /* Replace uCode's rate table for the destination station. */
2260 tbl->rate.index = index;
2261 rs_update_rate_tbl(mvm, sta, lq_sta, &tbl->rate);
2264 rs_stay_in_table(lq_sta, false);
2267 * Search for new modulation mode if we're:
2268 * 1) Not changing rates right now
2269 * 2) Not just finishing up a search
2270 * 3) Allowing a new search
2272 if (!update_lq && !done_search &&
2273 lq_sta->rs_state == RS_STATE_SEARCH_CYCLE_STARTED
2274 && window->counter) {
2275 enum rs_column next_column;
2277 /* Save current throughput to compare with "search" throughput*/
2278 lq_sta->last_tpt = current_tpt;
2281 "Start Search: update_lq %d done_search %d rs_state %d win->counter %d\n",
2282 update_lq, done_search, lq_sta->rs_state,
2285 next_column = rs_get_next_column(mvm, lq_sta, sta, tbl);
2286 if (next_column != RS_COLUMN_INVALID) {
2287 int ret = rs_switch_to_column(mvm, lq_sta, sta,
2290 lq_sta->search_better_tbl = 1;
2293 "No more columns to explore in search cycle. Go to RS_STATE_SEARCH_CYCLE_ENDED\n");
2294 lq_sta->rs_state = RS_STATE_SEARCH_CYCLE_ENDED;
2297 /* If new "search" mode was selected, set up in uCode table */
2298 if (lq_sta->search_better_tbl) {
2299 /* Access the "search" table, clear its history. */
2300 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
2301 rs_rate_scale_clear_tbl_windows(mvm, tbl);
2303 /* Use new "search" start rate */
2304 index = tbl->rate.index;
2306 rs_dump_rate(mvm, &tbl->rate,
2307 "Switch to SEARCH TABLE:");
2308 rs_fill_lq_cmd(mvm, sta, lq_sta, &tbl->rate);
2309 iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, false);
2315 if (done_search && lq_sta->rs_state == RS_STATE_SEARCH_CYCLE_ENDED) {
2316 /* If the "active" (non-search) mode was legacy,
2317 * and we've tried switching antennas,
2318 * but we haven't been able to try HT modes (not available),
2319 * stay with best antenna legacy modulation for a while
2320 * before next round of mode comparisons. */
2321 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
2322 if (is_legacy(&tbl1->rate)) {
2323 IWL_DEBUG_RATE(mvm, "LQ: STAY in legacy table\n");
2325 if (tid != IWL_MAX_TID_COUNT) {
2326 tid_data = &sta_priv->tid_data[tid];
2327 if (tid_data->state != IWL_AGG_OFF) {
2329 "Stop aggregation on tid %d\n",
2331 ieee80211_stop_tx_ba_session(sta, tid);
2334 rs_set_stay_in_table(mvm, 1, lq_sta);
2336 /* If we're in an HT mode, and all 3 mode switch actions
2337 * have been tried and compared, stay in this best modulation
2338 * mode for a while before next round of mode comparisons. */
2339 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2340 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
2341 (tid != IWL_MAX_TID_COUNT)) {
2342 tid_data = &sta_priv->tid_data[tid];
2343 if (tid_data->state == IWL_AGG_OFF) {
2345 "try to aggregate tid %d\n",
2347 rs_tl_turn_on_agg(mvm, tid,
2351 rs_set_stay_in_table(mvm, 0, lq_sta);
2356 lq_sta->last_txrate_idx = index;
2359 struct rs_init_rate_info {
2364 static const struct rs_init_rate_info rs_init_rates_24ghz[] = {
2365 { -60, IWL_RATE_54M_INDEX },
2366 { -64, IWL_RATE_48M_INDEX },
2367 { -68, IWL_RATE_36M_INDEX },
2368 { -80, IWL_RATE_24M_INDEX },
2369 { -84, IWL_RATE_18M_INDEX },
2370 { -85, IWL_RATE_12M_INDEX },
2371 { -86, IWL_RATE_11M_INDEX },
2372 { -88, IWL_RATE_5M_INDEX },
2373 { -90, IWL_RATE_2M_INDEX },
2374 { S8_MIN, IWL_RATE_1M_INDEX },
2377 static const struct rs_init_rate_info rs_init_rates_5ghz[] = {
2378 { -60, IWL_RATE_54M_INDEX },
2379 { -64, IWL_RATE_48M_INDEX },
2380 { -72, IWL_RATE_36M_INDEX },
2381 { -80, IWL_RATE_24M_INDEX },
2382 { -84, IWL_RATE_18M_INDEX },
2383 { -85, IWL_RATE_12M_INDEX },
2384 { -87, IWL_RATE_9M_INDEX },
2385 { S8_MIN, IWL_RATE_6M_INDEX },
2388 /* Choose an initial legacy rate and antenna to use based on the RSSI
2391 static void rs_get_initial_rate(struct iwl_mvm *mvm,
2392 struct iwl_lq_sta *lq_sta,
2393 enum ieee80211_band band,
2394 struct rs_rate *rate)
2397 s8 best_rssi = S8_MIN;
2398 u8 best_ant = ANT_NONE;
2399 u8 valid_tx_ant = iwl_mvm_get_valid_tx_ant(mvm);
2400 const struct rs_init_rate_info *initial_rates;
2402 for (i = 0; i < ARRAY_SIZE(lq_sta->pers.chain_signal); i++) {
2403 if (!(lq_sta->pers.chains & BIT(i)))
2406 if (lq_sta->pers.chain_signal[i] > best_rssi) {
2407 best_rssi = lq_sta->pers.chain_signal[i];
2412 IWL_DEBUG_RATE(mvm, "Best ANT: %s Best RSSI: %d\n",
2413 rs_pretty_ant(best_ant), best_rssi);
2415 if (best_ant != ANT_A && best_ant != ANT_B)
2416 rate->ant = first_antenna(valid_tx_ant);
2418 rate->ant = best_ant;
2422 rate->bw = RATE_MCS_CHAN_WIDTH_20;
2424 rate->index = find_first_bit(&lq_sta->active_legacy_rate,
2427 if (band == IEEE80211_BAND_5GHZ) {
2428 rate->type = LQ_LEGACY_A;
2429 initial_rates = rs_init_rates_5ghz;
2430 nentries = ARRAY_SIZE(rs_init_rates_5ghz);
2432 rate->type = LQ_LEGACY_G;
2433 initial_rates = rs_init_rates_24ghz;
2434 nentries = ARRAY_SIZE(rs_init_rates_24ghz);
2437 if (IWL_MVM_RS_RSSI_BASED_INIT_RATE) {
2438 for (i = 0; i < nentries; i++) {
2439 int rate_idx = initial_rates[i].rate_idx;
2440 if ((best_rssi >= initial_rates[i].rssi) &&
2441 (BIT(rate_idx) & lq_sta->active_legacy_rate)) {
2442 rate->index = rate_idx;
2448 IWL_DEBUG_RATE(mvm, "rate_idx %d ANT %s\n", rate->index,
2449 rs_pretty_ant(rate->ant));
2452 /* Save info about RSSI of last Rx */
2453 void rs_update_last_rssi(struct iwl_mvm *mvm,
2454 struct iwl_lq_sta *lq_sta,
2455 struct ieee80211_rx_status *rx_status)
2457 lq_sta->pers.chains = rx_status->chains;
2458 lq_sta->pers.chain_signal[0] = rx_status->chain_signal[0];
2459 lq_sta->pers.chain_signal[1] = rx_status->chain_signal[1];
2460 lq_sta->pers.chain_signal[2] = rx_status->chain_signal[2];
2464 * rs_initialize_lq - Initialize a station's hardware rate table
2466 * The uCode's station table contains a table of fallback rates
2467 * for automatic fallback during transmission.
2469 * NOTE: This sets up a default set of values. These will be replaced later
2470 * if the driver's iwl-agn-rs rate scaling algorithm is used, instead of
2473 * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
2474 * calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
2475 * which requires station table entry to exist).
2477 static void rs_initialize_lq(struct iwl_mvm *mvm,
2478 struct ieee80211_sta *sta,
2479 struct iwl_lq_sta *lq_sta,
2480 enum ieee80211_band band,
2483 struct iwl_scale_tbl_info *tbl;
2484 struct rs_rate *rate;
2487 if (!sta || !lq_sta)
2490 if (!lq_sta->search_better_tbl)
2491 active_tbl = lq_sta->active_tbl;
2493 active_tbl = 1 - lq_sta->active_tbl;
2495 tbl = &(lq_sta->lq_info[active_tbl]);
2498 rs_get_initial_rate(mvm, lq_sta, band, rate);
2499 lq_sta->last_txrate_idx = rate->index;
2501 WARN_ON_ONCE(rate->ant != ANT_A && rate->ant != ANT_B);
2502 if (rate->ant == ANT_A)
2503 tbl->column = RS_COLUMN_LEGACY_ANT_A;
2505 tbl->column = RS_COLUMN_LEGACY_ANT_B;
2507 rs_set_expected_tpt_table(lq_sta, tbl);
2508 rs_fill_lq_cmd(mvm, sta, lq_sta, rate);
2509 /* TODO restore station should remember the lq cmd */
2510 iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, init);
2513 static void rs_get_rate(void *mvm_r, struct ieee80211_sta *sta, void *mvm_sta,
2514 struct ieee80211_tx_rate_control *txrc)
2516 struct sk_buff *skb = txrc->skb;
2517 struct iwl_op_mode *op_mode __maybe_unused =
2518 (struct iwl_op_mode *)mvm_r;
2519 struct iwl_mvm *mvm __maybe_unused = IWL_OP_MODE_GET_MVM(op_mode);
2520 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2521 struct iwl_lq_sta *lq_sta = mvm_sta;
2523 /* TODO: handle rate_idx_mask and rate_idx_mcs_mask */
2525 /* Treat uninitialized rate scaling data same as non-existing. */
2526 if (lq_sta && !lq_sta->pers.drv) {
2527 IWL_DEBUG_RATE(mvm, "Rate scaling not initialized yet.\n");
2531 /* Send management frames and NO_ACK data using lowest rate. */
2532 if (rate_control_send_low(sta, mvm_sta, txrc))
2535 iwl_mvm_hwrate_to_tx_rate(lq_sta->last_rate_n_flags,
2536 info->band, &info->control.rates[0]);
2538 info->control.rates[0].count = 1;
2541 static void *rs_alloc_sta(void *mvm_rate, struct ieee80211_sta *sta,
2544 struct iwl_mvm_sta *sta_priv = (struct iwl_mvm_sta *)sta->drv_priv;
2545 struct iwl_op_mode *op_mode = (struct iwl_op_mode *)mvm_rate;
2546 struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
2547 struct iwl_lq_sta *lq_sta = &sta_priv->lq_sta;
2549 IWL_DEBUG_RATE(mvm, "create station rate scale window\n");
2551 lq_sta->pers.drv = mvm;
2552 #ifdef CONFIG_MAC80211_DEBUGFS
2553 lq_sta->pers.dbg_fixed_rate = 0;
2554 lq_sta->pers.dbg_fixed_txp_reduction = TPC_INVALID;
2556 lq_sta->pers.chains = 0;
2557 memset(lq_sta->pers.chain_signal, 0, sizeof(lq_sta->pers.chain_signal));
2559 return &sta_priv->lq_sta;
2562 static int rs_vht_highest_rx_mcs_index(struct ieee80211_sta_vht_cap *vht_cap,
2565 u16 rx_mcs = le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map) &
2566 (0x3 << (2 * (nss - 1)));
2567 rx_mcs >>= (2 * (nss - 1));
2569 if (rx_mcs == IEEE80211_VHT_MCS_SUPPORT_0_7)
2570 return IWL_RATE_MCS_7_INDEX;
2571 else if (rx_mcs == IEEE80211_VHT_MCS_SUPPORT_0_8)
2572 return IWL_RATE_MCS_8_INDEX;
2573 else if (rx_mcs == IEEE80211_VHT_MCS_SUPPORT_0_9)
2574 return IWL_RATE_MCS_9_INDEX;
2576 WARN_ON_ONCE(rx_mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED);
2580 static void rs_vht_set_enabled_rates(struct ieee80211_sta *sta,
2581 struct ieee80211_sta_vht_cap *vht_cap,
2582 struct iwl_lq_sta *lq_sta)
2585 int highest_mcs = rs_vht_highest_rx_mcs_index(vht_cap, 1);
2587 if (highest_mcs >= IWL_RATE_MCS_0_INDEX) {
2588 for (i = IWL_RATE_MCS_0_INDEX; i <= highest_mcs; i++) {
2589 if (i == IWL_RATE_9M_INDEX)
2592 /* VHT MCS9 isn't valid for 20Mhz for NSS=1,2 */
2593 if (i == IWL_RATE_MCS_9_INDEX &&
2594 sta->bandwidth == IEEE80211_STA_RX_BW_20)
2597 lq_sta->active_siso_rate |= BIT(i);
2601 if (sta->rx_nss < 2)
2604 highest_mcs = rs_vht_highest_rx_mcs_index(vht_cap, 2);
2605 if (highest_mcs >= IWL_RATE_MCS_0_INDEX) {
2606 for (i = IWL_RATE_MCS_0_INDEX; i <= highest_mcs; i++) {
2607 if (i == IWL_RATE_9M_INDEX)
2610 /* VHT MCS9 isn't valid for 20Mhz for NSS=1,2 */
2611 if (i == IWL_RATE_MCS_9_INDEX &&
2612 sta->bandwidth == IEEE80211_STA_RX_BW_20)
2615 lq_sta->active_mimo2_rate |= BIT(i);
2620 #ifdef CONFIG_IWLWIFI_DEBUGFS
2621 static void iwl_mvm_reset_frame_stats(struct iwl_mvm *mvm,
2622 struct iwl_mvm_frame_stats *stats)
2624 spin_lock_bh(&mvm->drv_stats_lock);
2625 memset(stats, 0, sizeof(*stats));
2626 spin_unlock_bh(&mvm->drv_stats_lock);
2629 void iwl_mvm_update_frame_stats(struct iwl_mvm *mvm,
2630 struct iwl_mvm_frame_stats *stats,
2633 u8 nss = 0, mcs = 0;
2635 spin_lock(&mvm->drv_stats_lock);
2638 stats->agg_frames++;
2640 stats->success_frames++;
2642 switch (rate & RATE_MCS_CHAN_WIDTH_MSK) {
2643 case RATE_MCS_CHAN_WIDTH_20:
2644 stats->bw_20_frames++;
2646 case RATE_MCS_CHAN_WIDTH_40:
2647 stats->bw_40_frames++;
2649 case RATE_MCS_CHAN_WIDTH_80:
2650 stats->bw_80_frames++;
2653 WARN_ONCE(1, "bad BW. rate 0x%x", rate);
2656 if (rate & RATE_MCS_HT_MSK) {
2658 mcs = rate & RATE_HT_MCS_RATE_CODE_MSK;
2659 nss = ((rate & RATE_HT_MCS_NSS_MSK) >> RATE_HT_MCS_NSS_POS) + 1;
2660 } else if (rate & RATE_MCS_VHT_MSK) {
2661 stats->vht_frames++;
2662 mcs = rate & RATE_VHT_MCS_RATE_CODE_MSK;
2663 nss = ((rate & RATE_VHT_MCS_NSS_MSK) >>
2664 RATE_VHT_MCS_NSS_POS) + 1;
2666 stats->legacy_frames++;
2670 stats->siso_frames++;
2672 stats->mimo2_frames++;
2674 if (rate & RATE_MCS_SGI_MSK)
2675 stats->sgi_frames++;
2677 stats->ngi_frames++;
2679 stats->last_rates[stats->last_frame_idx] = rate;
2680 stats->last_frame_idx = (stats->last_frame_idx + 1) %
2681 ARRAY_SIZE(stats->last_rates);
2683 spin_unlock(&mvm->drv_stats_lock);
2688 * Called after adding a new station to initialize rate scaling
2690 void iwl_mvm_rs_rate_init(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2691 enum ieee80211_band band, bool init)
2694 struct ieee80211_hw *hw = mvm->hw;
2695 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
2696 struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
2697 struct iwl_mvm_sta *sta_priv;
2698 struct iwl_lq_sta *lq_sta;
2699 struct ieee80211_supported_band *sband;
2700 unsigned long supp; /* must be unsigned long for for_each_set_bit */
2702 sta_priv = (struct iwl_mvm_sta *)sta->drv_priv;
2703 lq_sta = &sta_priv->lq_sta;
2705 /* clear all non-persistent lq data */
2706 memset(lq_sta, 0, offsetof(typeof(*lq_sta), pers));
2708 sband = hw->wiphy->bands[band];
2710 lq_sta->lq.sta_id = sta_priv->sta_id;
2712 for (j = 0; j < LQ_SIZE; j++)
2713 rs_rate_scale_clear_tbl_windows(mvm, &lq_sta->lq_info[j]);
2715 lq_sta->flush_timer = 0;
2716 lq_sta->last_tx = jiffies;
2719 "LQ: *** rate scale station global init for station %d ***\n",
2721 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2722 * the lowest or the highest rate.. Could consider using RSSI from
2723 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2726 lq_sta->missed_rate_counter = IWL_MISSED_RATE_MAX;
2727 lq_sta->band = sband->band;
2729 * active legacy rates as per supported rates bitmap
2731 supp = sta->supp_rates[sband->band];
2732 lq_sta->active_legacy_rate = 0;
2733 for_each_set_bit(i, &supp, BITS_PER_LONG)
2734 lq_sta->active_legacy_rate |= BIT(sband->bitrates[i].hw_value);
2736 /* TODO: should probably account for rx_highest for both HT/VHT */
2737 if (!vht_cap || !vht_cap->vht_supported) {
2738 /* active_siso_rate mask includes 9 MBits (bit 5),
2739 * and CCK (bits 0-3), supp_rates[] does not;
2740 * shift to convert format, force 9 MBits off.
2742 lq_sta->active_siso_rate = ht_cap->mcs.rx_mask[0] << 1;
2743 lq_sta->active_siso_rate |= ht_cap->mcs.rx_mask[0] & 0x1;
2744 lq_sta->active_siso_rate &= ~((u16)0x2);
2745 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
2748 lq_sta->active_mimo2_rate = ht_cap->mcs.rx_mask[1] << 1;
2749 lq_sta->active_mimo2_rate |= ht_cap->mcs.rx_mask[1] & 0x1;
2750 lq_sta->active_mimo2_rate &= ~((u16)0x2);
2751 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2753 lq_sta->is_vht = false;
2754 if (mvm->cfg->ht_params->ldpc &&
2755 (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING))
2756 lq_sta->ldpc = true;
2758 if (mvm->cfg->ht_params->stbc &&
2759 (num_of_ant(iwl_mvm_get_valid_tx_ant(mvm)) > 1) &&
2760 (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC))
2761 lq_sta->stbc = true;
2763 rs_vht_set_enabled_rates(sta, vht_cap, lq_sta);
2764 lq_sta->is_vht = true;
2766 if (mvm->cfg->ht_params->ldpc &&
2767 (vht_cap->cap & IEEE80211_VHT_CAP_RXLDPC))
2768 lq_sta->ldpc = true;
2770 if (mvm->cfg->ht_params->stbc &&
2771 (num_of_ant(iwl_mvm_get_valid_tx_ant(mvm)) > 1) &&
2772 (vht_cap->cap & IEEE80211_VHT_CAP_RXSTBC_MASK))
2773 lq_sta->stbc = true;
2776 if (IWL_MVM_RS_DISABLE_MIMO)
2777 lq_sta->active_mimo2_rate = 0;
2779 lq_sta->max_legacy_rate_idx =
2780 rs_get_max_rate_from_mask(lq_sta->active_legacy_rate);
2781 lq_sta->max_siso_rate_idx =
2782 rs_get_max_rate_from_mask(lq_sta->active_siso_rate);
2783 lq_sta->max_mimo2_rate_idx =
2784 rs_get_max_rate_from_mask(lq_sta->active_mimo2_rate);
2787 "RATE MASK: LEGACY=%lX SISO=%lX MIMO2=%lX VHT=%d LDPC=%d STBC%d\n",
2788 lq_sta->active_legacy_rate,
2789 lq_sta->active_siso_rate,
2790 lq_sta->active_mimo2_rate,
2791 lq_sta->is_vht, lq_sta->ldpc, lq_sta->stbc);
2792 IWL_DEBUG_RATE(mvm, "MAX RATE: LEGACY=%d SISO=%d MIMO2=%d\n",
2793 lq_sta->max_legacy_rate_idx,
2794 lq_sta->max_siso_rate_idx,
2795 lq_sta->max_mimo2_rate_idx);
2797 /* These values will be overridden later */
2798 lq_sta->lq.single_stream_ant_msk =
2799 first_antenna(iwl_mvm_get_valid_tx_ant(mvm));
2800 lq_sta->lq.dual_stream_ant_msk = ANT_AB;
2802 /* as default allow aggregation for all tids */
2803 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
2805 #ifdef CONFIG_IWLWIFI_DEBUGFS
2806 iwl_mvm_reset_frame_stats(mvm, &mvm->drv_rx_stats);
2808 rs_initialize_lq(mvm, sta, lq_sta, band, init);
2811 static void rs_rate_update(void *mvm_r,
2812 struct ieee80211_supported_band *sband,
2813 struct cfg80211_chan_def *chandef,
2814 struct ieee80211_sta *sta, void *priv_sta,
2818 struct iwl_op_mode *op_mode =
2819 (struct iwl_op_mode *)mvm_r;
2820 struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
2822 /* Stop any ongoing aggregations as rs starts off assuming no agg */
2823 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++)
2824 ieee80211_stop_tx_ba_session(sta, tid);
2826 iwl_mvm_rs_rate_init(mvm, sta, sband->band, false);
2829 #ifdef CONFIG_MAC80211_DEBUGFS
2830 static void rs_build_rates_table_from_fixed(struct iwl_mvm *mvm,
2831 struct iwl_lq_cmd *lq_cmd,
2832 enum ieee80211_band band,
2835 struct rs_rate rate;
2837 int num_rates = ARRAY_SIZE(lq_cmd->rs_table);
2838 __le32 ucode_rate_le32 = cpu_to_le32(ucode_rate);
2839 u8 ant = (ucode_rate & RATE_MCS_ANT_ABC_MSK) >> RATE_MCS_ANT_POS;
2841 for (i = 0; i < num_rates; i++)
2842 lq_cmd->rs_table[i] = ucode_rate_le32;
2844 rs_rate_from_ucode_rate(ucode_rate, band, &rate);
2847 lq_cmd->mimo_delim = num_rates - 1;
2849 lq_cmd->mimo_delim = 0;
2851 lq_cmd->reduced_tpc = 0;
2853 if (num_of_ant(ant) == 1)
2854 lq_cmd->single_stream_ant_msk = ant;
2856 lq_cmd->agg_frame_cnt_limit = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
2858 #endif /* CONFIG_MAC80211_DEBUGFS */
2860 static void rs_fill_rates_for_column(struct iwl_mvm *mvm,
2861 struct iwl_lq_sta *lq_sta,
2862 struct rs_rate *rate,
2863 __le32 *rs_table, int *rs_table_index,
2864 int num_rates, int num_retries,
2865 u8 valid_tx_ant, bool toggle_ant)
2869 bool bottom_reached = false;
2870 int prev_rate_idx = rate->index;
2871 int end = LINK_QUAL_MAX_RETRY_NUM;
2872 int index = *rs_table_index;
2874 for (i = 0; i < num_rates && index < end; i++) {
2875 ucode_rate = cpu_to_le32(ucode_rate_from_rs_rate(mvm, rate));
2876 for (j = 0; j < num_retries && index < end; j++, index++)
2877 rs_table[index] = ucode_rate;
2880 rs_toggle_antenna(valid_tx_ant, rate);
2882 prev_rate_idx = rate->index;
2883 bottom_reached = rs_get_lower_rate_in_column(lq_sta, rate);
2884 if (bottom_reached && !is_legacy(rate))
2888 if (!bottom_reached)
2889 rate->index = prev_rate_idx;
2891 *rs_table_index = index;
2894 /* Building the rate table is non trivial. When we're in MIMO2/VHT/80Mhz/SGI
2895 * column the rate table should look like this:
2897 * rate[0] 0x400D019 VHT | ANT: AB BW: 80Mhz MCS: 9 NSS: 2 SGI
2898 * rate[1] 0x400D019 VHT | ANT: AB BW: 80Mhz MCS: 9 NSS: 2 SGI
2899 * rate[2] 0x400D018 VHT | ANT: AB BW: 80Mhz MCS: 8 NSS: 2 SGI
2900 * rate[3] 0x400D018 VHT | ANT: AB BW: 80Mhz MCS: 8 NSS: 2 SGI
2901 * rate[4] 0x400D017 VHT | ANT: AB BW: 80Mhz MCS: 7 NSS: 2 SGI
2902 * rate[5] 0x400D017 VHT | ANT: AB BW: 80Mhz MCS: 7 NSS: 2 SGI
2903 * rate[6] 0x4005007 VHT | ANT: A BW: 80Mhz MCS: 7 NSS: 1 NGI
2904 * rate[7] 0x4009006 VHT | ANT: B BW: 80Mhz MCS: 6 NSS: 1 NGI
2905 * rate[8] 0x4005005 VHT | ANT: A BW: 80Mhz MCS: 5 NSS: 1 NGI
2906 * rate[9] 0x800B Legacy | ANT: B Rate: 36 Mbps
2907 * rate[10] 0x4009 Legacy | ANT: A Rate: 24 Mbps
2908 * rate[11] 0x8007 Legacy | ANT: B Rate: 18 Mbps
2909 * rate[12] 0x4005 Legacy | ANT: A Rate: 12 Mbps
2910 * rate[13] 0x800F Legacy | ANT: B Rate: 9 Mbps
2911 * rate[14] 0x400D Legacy | ANT: A Rate: 6 Mbps
2912 * rate[15] 0x800D Legacy | ANT: B Rate: 6 Mbps
2914 static void rs_build_rates_table(struct iwl_mvm *mvm,
2915 struct ieee80211_sta *sta,
2916 struct iwl_lq_sta *lq_sta,
2917 const struct rs_rate *initial_rate)
2919 struct rs_rate rate;
2920 int num_rates, num_retries, index = 0;
2921 u8 valid_tx_ant = 0;
2922 struct iwl_lq_cmd *lq_cmd = &lq_sta->lq;
2923 bool toggle_ant = false;
2925 memcpy(&rate, initial_rate, sizeof(rate));
2927 valid_tx_ant = iwl_mvm_get_valid_tx_ant(mvm);
2928 rate.stbc = rs_stbc_allow(mvm, sta, lq_sta);
2930 if (is_siso(&rate)) {
2931 num_rates = RS_INITIAL_SISO_NUM_RATES;
2932 num_retries = RS_HT_VHT_RETRIES_PER_RATE;
2933 } else if (is_mimo(&rate)) {
2934 num_rates = RS_INITIAL_MIMO_NUM_RATES;
2935 num_retries = RS_HT_VHT_RETRIES_PER_RATE;
2937 num_rates = RS_INITIAL_LEGACY_NUM_RATES;
2938 num_retries = RS_LEGACY_RETRIES_PER_RATE;
2942 rs_fill_rates_for_column(mvm, lq_sta, &rate, lq_cmd->rs_table, &index,
2943 num_rates, num_retries, valid_tx_ant,
2946 rs_get_lower_rate_down_column(lq_sta, &rate);
2948 if (is_siso(&rate)) {
2949 num_rates = RS_SECONDARY_SISO_NUM_RATES;
2950 num_retries = RS_SECONDARY_SISO_RETRIES;
2951 lq_cmd->mimo_delim = index;
2952 } else if (is_legacy(&rate)) {
2953 num_rates = RS_SECONDARY_LEGACY_NUM_RATES;
2954 num_retries = RS_LEGACY_RETRIES_PER_RATE;
2961 rs_fill_rates_for_column(mvm, lq_sta, &rate, lq_cmd->rs_table, &index,
2962 num_rates, num_retries, valid_tx_ant,
2965 rs_get_lower_rate_down_column(lq_sta, &rate);
2967 num_rates = RS_SECONDARY_LEGACY_NUM_RATES;
2968 num_retries = RS_LEGACY_RETRIES_PER_RATE;
2970 rs_fill_rates_for_column(mvm, lq_sta, &rate, lq_cmd->rs_table, &index,
2971 num_rates, num_retries, valid_tx_ant,
2976 static void rs_fill_lq_cmd(struct iwl_mvm *mvm,
2977 struct ieee80211_sta *sta,
2978 struct iwl_lq_sta *lq_sta,
2979 const struct rs_rate *initial_rate)
2981 struct iwl_lq_cmd *lq_cmd = &lq_sta->lq;
2982 struct iwl_mvm_sta *mvmsta;
2983 struct iwl_mvm_vif *mvmvif;
2985 lq_cmd->agg_disable_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
2986 lq_cmd->agg_time_limit =
2987 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
2989 #ifdef CONFIG_MAC80211_DEBUGFS
2990 if (lq_sta->pers.dbg_fixed_rate) {
2991 rs_build_rates_table_from_fixed(mvm, lq_cmd,
2993 lq_sta->pers.dbg_fixed_rate);
2997 if (WARN_ON_ONCE(!sta || !initial_rate))
3000 rs_build_rates_table(mvm, sta, lq_sta, initial_rate);
3002 if (num_of_ant(initial_rate->ant) == 1)
3003 lq_cmd->single_stream_ant_msk = initial_rate->ant;
3005 mvmsta = iwl_mvm_sta_from_mac80211(sta);
3006 mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif);
3008 if (num_of_ant(initial_rate->ant) == 1)
3009 lq_cmd->single_stream_ant_msk = initial_rate->ant;
3011 lq_cmd->agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
3014 * In case of low latency, tell the firwmare to leave a frame in the
3015 * Tx Fifo so that it can start a transaction in the same TxOP. This
3016 * basically allows the firmware to send bursts.
3018 if (iwl_mvm_vif_low_latency(mvmvif)) {
3019 lq_cmd->agg_frame_cnt_limit--;
3021 if (mvm->low_latency_agg_frame_limit)
3022 lq_cmd->agg_frame_cnt_limit =
3023 min(lq_cmd->agg_frame_cnt_limit,
3024 mvm->low_latency_agg_frame_limit);
3027 if (mvmsta->vif->p2p)
3028 lq_cmd->flags |= LQ_FLAG_USE_RTS_MSK;
3030 lq_cmd->agg_time_limit =
3031 cpu_to_le16(iwl_mvm_coex_agg_time_limit(mvm, sta));
3034 static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
3038 /* rate scale requires free function to be implemented */
3039 static void rs_free(void *mvm_rate)
3044 static void rs_free_sta(void *mvm_r, struct ieee80211_sta *sta,
3047 struct iwl_op_mode *op_mode __maybe_unused = mvm_r;
3048 struct iwl_mvm *mvm __maybe_unused = IWL_OP_MODE_GET_MVM(op_mode);
3050 IWL_DEBUG_RATE(mvm, "enter\n");
3051 IWL_DEBUG_RATE(mvm, "leave\n");
3054 #ifdef CONFIG_MAC80211_DEBUGFS
3055 int rs_pretty_print_rate(char *buf, const u32 rate)
3059 u8 mcs = 0, nss = 0;
3060 u8 ant = (rate & RATE_MCS_ANT_ABC_MSK) >> RATE_MCS_ANT_POS;
3062 if (!(rate & RATE_MCS_HT_MSK) &&
3063 !(rate & RATE_MCS_VHT_MSK)) {
3064 int index = iwl_hwrate_to_plcp_idx(rate);
3066 return sprintf(buf, "Legacy | ANT: %s Rate: %s Mbps\n",
3068 index == IWL_RATE_INVALID ? "BAD" :
3069 iwl_rate_mcs[index].mbps);
3072 if (rate & RATE_MCS_VHT_MSK) {
3074 mcs = rate & RATE_VHT_MCS_RATE_CODE_MSK;
3075 nss = ((rate & RATE_VHT_MCS_NSS_MSK)
3076 >> RATE_VHT_MCS_NSS_POS) + 1;
3077 } else if (rate & RATE_MCS_HT_MSK) {
3079 mcs = rate & RATE_HT_MCS_INDEX_MSK;
3081 type = "Unknown"; /* shouldn't happen */
3084 switch (rate & RATE_MCS_CHAN_WIDTH_MSK) {
3085 case RATE_MCS_CHAN_WIDTH_20:
3088 case RATE_MCS_CHAN_WIDTH_40:
3091 case RATE_MCS_CHAN_WIDTH_80:
3094 case RATE_MCS_CHAN_WIDTH_160:
3101 return sprintf(buf, "%s | ANT: %s BW: %s MCS: %d NSS: %d %s%s%s%s%s\n",
3102 type, rs_pretty_ant(ant), bw, mcs, nss,
3103 (rate & RATE_MCS_SGI_MSK) ? "SGI " : "NGI ",
3104 (rate & RATE_MCS_HT_STBC_MSK) ? "STBC " : "",
3105 (rate & RATE_MCS_LDPC_MSK) ? "LDPC " : "",
3106 (rate & RATE_MCS_BF_MSK) ? "BF " : "",
3107 (rate & RATE_MCS_ZLF_MSK) ? "ZLF " : "");
3111 * Program the device to use fixed rate for frame transmit
3112 * This is for debugging/testing only
3113 * once the device start use fixed rate, we need to reload the module
3114 * to being back the normal operation.
3116 static void rs_program_fix_rate(struct iwl_mvm *mvm,
3117 struct iwl_lq_sta *lq_sta)
3119 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
3120 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
3121 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
3123 IWL_DEBUG_RATE(mvm, "sta_id %d rate 0x%X\n",
3124 lq_sta->lq.sta_id, lq_sta->pers.dbg_fixed_rate);
3126 if (lq_sta->pers.dbg_fixed_rate) {
3127 rs_fill_lq_cmd(mvm, NULL, lq_sta, NULL);
3128 iwl_mvm_send_lq_cmd(lq_sta->pers.drv, &lq_sta->lq, false);
3132 static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
3133 const char __user *user_buf, size_t count, loff_t *ppos)
3135 struct iwl_lq_sta *lq_sta = file->private_data;
3136 struct iwl_mvm *mvm;
3141 mvm = lq_sta->pers.drv;
3142 memset(buf, 0, sizeof(buf));
3143 buf_size = min(count, sizeof(buf) - 1);
3144 if (copy_from_user(buf, user_buf, buf_size))
3147 if (sscanf(buf, "%x", &parsed_rate) == 1)
3148 lq_sta->pers.dbg_fixed_rate = parsed_rate;
3150 lq_sta->pers.dbg_fixed_rate = 0;
3152 rs_program_fix_rate(mvm, lq_sta);
3157 static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
3158 char __user *user_buf, size_t count, loff_t *ppos)
3165 struct iwl_lq_sta *lq_sta = file->private_data;
3166 struct iwl_mvm *mvm;
3167 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
3168 struct rs_rate *rate = &tbl->rate;
3169 mvm = lq_sta->pers.drv;
3170 buff = kmalloc(2048, GFP_KERNEL);
3174 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
3175 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%lX\n",
3176 lq_sta->total_failed, lq_sta->total_success,
3177 lq_sta->active_legacy_rate);
3178 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
3179 lq_sta->pers.dbg_fixed_rate);
3180 desc += sprintf(buff+desc, "valid_tx_ant %s%s%s\n",
3181 (iwl_mvm_get_valid_tx_ant(mvm) & ANT_A) ? "ANT_A," : "",
3182 (iwl_mvm_get_valid_tx_ant(mvm) & ANT_B) ? "ANT_B," : "",
3183 (iwl_mvm_get_valid_tx_ant(mvm) & ANT_C) ? "ANT_C" : "");
3184 desc += sprintf(buff+desc, "lq type %s\n",
3185 (is_legacy(rate)) ? "legacy" :
3186 is_vht(rate) ? "VHT" : "HT");
3187 if (!is_legacy(rate)) {
3188 desc += sprintf(buff+desc, " %s",
3189 (is_siso(rate)) ? "SISO" : "MIMO2");
3190 desc += sprintf(buff+desc, " %s",
3191 (is_ht20(rate)) ? "20MHz" :
3192 (is_ht40(rate)) ? "40MHz" :
3193 (is_ht80(rate)) ? "80Mhz" : "BAD BW");
3194 desc += sprintf(buff+desc, " %s %s %s\n",
3195 (rate->sgi) ? "SGI" : "NGI",
3196 (rate->ldpc) ? "LDPC" : "BCC",
3197 (lq_sta->is_agg) ? "AGG on" : "");
3199 desc += sprintf(buff+desc, "last tx rate=0x%X\n",
3200 lq_sta->last_rate_n_flags);
3201 desc += sprintf(buff+desc,
3202 "general: flags=0x%X mimo-d=%d s-ant=0x%x d-ant=0x%x\n",
3204 lq_sta->lq.mimo_delim,
3205 lq_sta->lq.single_stream_ant_msk,
3206 lq_sta->lq.dual_stream_ant_msk);
3208 desc += sprintf(buff+desc,
3209 "agg: time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
3210 le16_to_cpu(lq_sta->lq.agg_time_limit),
3211 lq_sta->lq.agg_disable_start_th,
3212 lq_sta->lq.agg_frame_cnt_limit);
3214 desc += sprintf(buff+desc, "reduced tpc=%d\n", lq_sta->lq.reduced_tpc);
3215 desc += sprintf(buff+desc,
3216 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
3217 lq_sta->lq.initial_rate_index[0],
3218 lq_sta->lq.initial_rate_index[1],
3219 lq_sta->lq.initial_rate_index[2],
3220 lq_sta->lq.initial_rate_index[3]);
3222 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
3223 u32 r = le32_to_cpu(lq_sta->lq.rs_table[i]);
3225 desc += sprintf(buff+desc, " rate[%d] 0x%X ", i, r);
3226 desc += rs_pretty_print_rate(buff+desc, r);
3229 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3234 static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
3235 .write = rs_sta_dbgfs_scale_table_write,
3236 .read = rs_sta_dbgfs_scale_table_read,
3237 .open = simple_open,
3238 .llseek = default_llseek,
3240 static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
3241 char __user *user_buf, size_t count, loff_t *ppos)
3247 struct iwl_scale_tbl_info *tbl;
3248 struct rs_rate *rate;
3249 struct iwl_lq_sta *lq_sta = file->private_data;
3251 buff = kmalloc(1024, GFP_KERNEL);
3255 for (i = 0; i < LQ_SIZE; i++) {
3256 tbl = &(lq_sta->lq_info[i]);
3258 desc += sprintf(buff+desc,
3259 "%s type=%d SGI=%d BW=%s DUP=0\n"
3261 lq_sta->active_tbl == i ? "*" : "x",
3264 is_ht20(rate) ? "20Mhz" :
3265 is_ht40(rate) ? "40Mhz" :
3266 is_ht80(rate) ? "80Mhz" : "ERR",
3268 for (j = 0; j < IWL_RATE_COUNT; j++) {
3269 desc += sprintf(buff+desc,
3270 "counter=%d success=%d %%=%d\n",
3271 tbl->win[j].counter,
3272 tbl->win[j].success_counter,
3273 tbl->win[j].success_ratio);
3276 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3281 static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
3282 .read = rs_sta_dbgfs_stats_table_read,
3283 .open = simple_open,
3284 .llseek = default_llseek,
3287 static ssize_t rs_sta_dbgfs_drv_tx_stats_read(struct file *file,
3288 char __user *user_buf,
3289 size_t count, loff_t *ppos)
3291 static const char * const column_name[] = {
3292 [RS_COLUMN_LEGACY_ANT_A] = "LEGACY_ANT_A",
3293 [RS_COLUMN_LEGACY_ANT_B] = "LEGACY_ANT_B",
3294 [RS_COLUMN_SISO_ANT_A] = "SISO_ANT_A",
3295 [RS_COLUMN_SISO_ANT_B] = "SISO_ANT_B",
3296 [RS_COLUMN_SISO_ANT_A_SGI] = "SISO_ANT_A_SGI",
3297 [RS_COLUMN_SISO_ANT_B_SGI] = "SISO_ANT_B_SGI",
3298 [RS_COLUMN_MIMO2] = "MIMO2",
3299 [RS_COLUMN_MIMO2_SGI] = "MIMO2_SGI",
3302 static const char * const rate_name[] = {
3303 [IWL_RATE_1M_INDEX] = "1M",
3304 [IWL_RATE_2M_INDEX] = "2M",
3305 [IWL_RATE_5M_INDEX] = "5.5M",
3306 [IWL_RATE_11M_INDEX] = "11M",
3307 [IWL_RATE_6M_INDEX] = "6M|MCS0",
3308 [IWL_RATE_9M_INDEX] = "9M",
3309 [IWL_RATE_12M_INDEX] = "12M|MCS1",
3310 [IWL_RATE_18M_INDEX] = "18M|MCS2",
3311 [IWL_RATE_24M_INDEX] = "24M|MCS3",
3312 [IWL_RATE_36M_INDEX] = "36M|MCS4",
3313 [IWL_RATE_48M_INDEX] = "48M|MCS5",
3314 [IWL_RATE_54M_INDEX] = "54M|MCS6",
3315 [IWL_RATE_MCS_7_INDEX] = "MCS7",
3316 [IWL_RATE_MCS_8_INDEX] = "MCS8",
3317 [IWL_RATE_MCS_9_INDEX] = "MCS9",
3320 char *buff, *pos, *endpos;
3323 struct iwl_lq_sta *lq_sta = file->private_data;
3324 struct rs_rate_stats *stats;
3325 static const size_t bufsz = 1024;
3327 buff = kmalloc(bufsz, GFP_KERNEL);
3332 endpos = pos + bufsz;
3334 pos += scnprintf(pos, endpos - pos, "COLUMN,");
3335 for (rate = 0; rate < IWL_RATE_COUNT; rate++)
3336 pos += scnprintf(pos, endpos - pos, "%s,", rate_name[rate]);
3337 pos += scnprintf(pos, endpos - pos, "\n");
3339 for (col = 0; col < RS_COLUMN_COUNT; col++) {
3340 pos += scnprintf(pos, endpos - pos,
3341 "%s,", column_name[col]);
3343 for (rate = 0; rate < IWL_RATE_COUNT; rate++) {
3344 stats = &(lq_sta->pers.tx_stats[col][rate]);
3345 pos += scnprintf(pos, endpos - pos,
3350 pos += scnprintf(pos, endpos - pos, "\n");
3353 ret = simple_read_from_buffer(user_buf, count, ppos, buff, pos - buff);
3358 static ssize_t rs_sta_dbgfs_drv_tx_stats_write(struct file *file,
3359 const char __user *user_buf,
3360 size_t count, loff_t *ppos)
3362 struct iwl_lq_sta *lq_sta = file->private_data;
3363 memset(lq_sta->pers.tx_stats, 0, sizeof(lq_sta->pers.tx_stats));
3368 static const struct file_operations rs_sta_dbgfs_drv_tx_stats_ops = {
3369 .read = rs_sta_dbgfs_drv_tx_stats_read,
3370 .write = rs_sta_dbgfs_drv_tx_stats_write,
3371 .open = simple_open,
3372 .llseek = default_llseek,
3375 static void rs_add_debugfs(void *mvm, void *mvm_sta, struct dentry *dir)
3377 struct iwl_lq_sta *lq_sta = mvm_sta;
3378 debugfs_create_file("rate_scale_table", S_IRUSR | S_IWUSR, dir,
3379 lq_sta, &rs_sta_dbgfs_scale_table_ops);
3380 debugfs_create_file("rate_stats_table", S_IRUSR, dir,
3381 lq_sta, &rs_sta_dbgfs_stats_table_ops);
3382 debugfs_create_file("drv_tx_stats", S_IRUSR | S_IWUSR, dir,
3383 lq_sta, &rs_sta_dbgfs_drv_tx_stats_ops);
3384 debugfs_create_u8("tx_agg_tid_enable", S_IRUSR | S_IWUSR, dir,
3385 &lq_sta->tx_agg_tid_en);
3386 debugfs_create_u8("reduced_tpc", S_IRUSR | S_IWUSR, dir,
3387 &lq_sta->pers.dbg_fixed_txp_reduction);
3390 static void rs_remove_debugfs(void *mvm, void *mvm_sta)
3396 * Initialization of rate scaling information is done by driver after
3397 * the station is added. Since mac80211 calls this function before a
3398 * station is added we ignore it.
3400 static void rs_rate_init_stub(void *mvm_r,
3401 struct ieee80211_supported_band *sband,
3402 struct cfg80211_chan_def *chandef,
3403 struct ieee80211_sta *sta, void *mvm_sta)
3407 static const struct rate_control_ops rs_mvm_ops = {
3409 .tx_status = rs_mac80211_tx_status,
3410 .get_rate = rs_get_rate,
3411 .rate_init = rs_rate_init_stub,
3414 .alloc_sta = rs_alloc_sta,
3415 .free_sta = rs_free_sta,
3416 .rate_update = rs_rate_update,
3417 #ifdef CONFIG_MAC80211_DEBUGFS
3418 .add_sta_debugfs = rs_add_debugfs,
3419 .remove_sta_debugfs = rs_remove_debugfs,
3423 int iwl_mvm_rate_control_register(void)
3425 return ieee80211_rate_control_register(&rs_mvm_ops);
3428 void iwl_mvm_rate_control_unregister(void)
3430 ieee80211_rate_control_unregister(&rs_mvm_ops);
3434 * iwl_mvm_tx_protection - Gets LQ command, change it to enable/disable
3435 * Tx protection, according to this rquest and previous requests,
3436 * and send the LQ command.
3437 * @mvmsta: The station
3438 * @enable: Enable Tx protection?
3440 int iwl_mvm_tx_protection(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
3443 struct iwl_lq_cmd *lq = &mvmsta->lq_sta.lq;
3445 lockdep_assert_held(&mvm->mutex);
3448 if (mvmsta->tx_protection == 0)
3449 lq->flags |= LQ_FLAG_USE_RTS_MSK;
3450 mvmsta->tx_protection++;
3452 mvmsta->tx_protection--;
3453 if (mvmsta->tx_protection == 0)
3454 lq->flags &= ~LQ_FLAG_USE_RTS_MSK;
3457 return iwl_mvm_send_lq_cmd(mvm, lq, false);