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_mimo_allow(struct iwl_mvm
*mvm
, struct ieee80211_sta
*sta
,
162 struct iwl_scale_tbl_info
*tbl
)
164 if (!sta
->ht_cap
.ht_supported
)
167 if (sta
->smps_mode
== IEEE80211_SMPS_STATIC
)
170 if (num_of_ant(mvm
->fw
->valid_tx_ant
) < 2)
173 if (!iwl_mvm_bt_coex_is_mimo_allowed(mvm
, sta
))
179 static bool rs_siso_allow(struct iwl_mvm
*mvm
, struct ieee80211_sta
*sta
,
180 struct iwl_scale_tbl_info
*tbl
)
182 if (!sta
->ht_cap
.ht_supported
)
188 static bool rs_sgi_allow(struct iwl_mvm
*mvm
, struct ieee80211_sta
*sta
,
189 struct iwl_scale_tbl_info
*tbl
)
191 struct rs_rate
*rate
= &tbl
->rate
;
192 struct ieee80211_sta_ht_cap
*ht_cap
= &sta
->ht_cap
;
193 struct ieee80211_sta_vht_cap
*vht_cap
= &sta
->vht_cap
;
195 if (is_ht20(rate
) && (ht_cap
->cap
&
196 IEEE80211_HT_CAP_SGI_20
))
198 if (is_ht40(rate
) && (ht_cap
->cap
&
199 IEEE80211_HT_CAP_SGI_40
))
201 if (is_ht80(rate
) && (vht_cap
->cap
&
202 IEEE80211_VHT_CAP_SHORT_GI_80
))
208 static const struct rs_tx_column rs_tx_columns
[] = {
209 [RS_COLUMN_LEGACY_ANT_A
] = {
213 RS_COLUMN_LEGACY_ANT_B
,
214 RS_COLUMN_SISO_ANT_A
,
222 [RS_COLUMN_LEGACY_ANT_B
] = {
226 RS_COLUMN_LEGACY_ANT_A
,
227 RS_COLUMN_SISO_ANT_B
,
235 [RS_COLUMN_SISO_ANT_A
] = {
239 RS_COLUMN_SISO_ANT_B
,
241 RS_COLUMN_SISO_ANT_A_SGI
,
242 RS_COLUMN_LEGACY_ANT_A
,
243 RS_COLUMN_LEGACY_ANT_B
,
251 [RS_COLUMN_SISO_ANT_B
] = {
255 RS_COLUMN_SISO_ANT_A
,
257 RS_COLUMN_SISO_ANT_B_SGI
,
258 RS_COLUMN_LEGACY_ANT_A
,
259 RS_COLUMN_LEGACY_ANT_B
,
267 [RS_COLUMN_SISO_ANT_A_SGI
] = {
272 RS_COLUMN_SISO_ANT_B_SGI
,
274 RS_COLUMN_SISO_ANT_A
,
275 RS_COLUMN_LEGACY_ANT_A
,
276 RS_COLUMN_LEGACY_ANT_B
,
285 [RS_COLUMN_SISO_ANT_B_SGI
] = {
290 RS_COLUMN_SISO_ANT_A_SGI
,
292 RS_COLUMN_SISO_ANT_B
,
293 RS_COLUMN_LEGACY_ANT_A
,
294 RS_COLUMN_LEGACY_ANT_B
,
303 [RS_COLUMN_MIMO2
] = {
307 RS_COLUMN_SISO_ANT_A
,
309 RS_COLUMN_LEGACY_ANT_A
,
310 RS_COLUMN_LEGACY_ANT_B
,
319 [RS_COLUMN_MIMO2_SGI
] = {
324 RS_COLUMN_SISO_ANT_A_SGI
,
326 RS_COLUMN_LEGACY_ANT_A
,
327 RS_COLUMN_LEGACY_ANT_B
,
339 static inline u8
rs_extract_rate(u32 rate_n_flags
)
341 /* also works for HT because bits 7:6 are zero there */
342 return (u8
)(rate_n_flags
& RATE_LEGACY_RATE_MSK
);
345 static int iwl_hwrate_to_plcp_idx(u32 rate_n_flags
)
349 if (rate_n_flags
& RATE_MCS_HT_MSK
) {
350 idx
= rate_n_flags
& RATE_HT_MCS_RATE_CODE_MSK
;
351 idx
+= IWL_RATE_MCS_0_INDEX
;
353 /* skip 9M not supported in HT*/
354 if (idx
>= IWL_RATE_9M_INDEX
)
356 if ((idx
>= IWL_FIRST_HT_RATE
) && (idx
<= IWL_LAST_HT_RATE
))
358 } else if (rate_n_flags
& RATE_MCS_VHT_MSK
) {
359 idx
= rate_n_flags
& RATE_VHT_MCS_RATE_CODE_MSK
;
360 idx
+= IWL_RATE_MCS_0_INDEX
;
362 /* skip 9M not supported in VHT*/
363 if (idx
>= IWL_RATE_9M_INDEX
)
365 if ((idx
>= IWL_FIRST_VHT_RATE
) && (idx
<= IWL_LAST_VHT_RATE
))
368 /* legacy rate format, search for match in table */
370 u8 legacy_rate
= rs_extract_rate(rate_n_flags
);
371 for (idx
= 0; idx
< ARRAY_SIZE(iwl_rates
); idx
++)
372 if (iwl_rates
[idx
].plcp
== legacy_rate
)
376 return IWL_RATE_INVALID
;
379 static void rs_rate_scale_perform(struct iwl_mvm
*mvm
,
381 struct ieee80211_sta
*sta
,
382 struct iwl_lq_sta
*lq_sta
);
383 static void rs_fill_lq_cmd(struct iwl_mvm
*mvm
,
384 struct ieee80211_sta
*sta
,
385 struct iwl_lq_sta
*lq_sta
,
386 const struct rs_rate
*initial_rate
);
387 static void rs_stay_in_table(struct iwl_lq_sta
*lq_sta
, bool force_search
);
390 * The following tables contain the expected throughput metrics for all rates
392 * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
394 * where invalid entries are zeros.
396 * CCK rates are only valid in legacy table and will only be used in G
400 static const u16 expected_tpt_legacy
[IWL_RATE_COUNT
] = {
401 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 0, 0, 0
404 /* Expected TpT tables. 4 indexes:
405 * 0 - NGI, 1 - SGI, 2 - AGG+NGI, 3 - AGG+SGI
407 static const u16 expected_tpt_siso_20MHz
[4][IWL_RATE_COUNT
] = {
408 {0, 0, 0, 0, 42, 0, 76, 102, 124, 159, 183, 193, 202, 216, 0},
409 {0, 0, 0, 0, 46, 0, 82, 110, 132, 168, 192, 202, 210, 225, 0},
410 {0, 0, 0, 0, 49, 0, 97, 145, 192, 285, 375, 420, 464, 551, 0},
411 {0, 0, 0, 0, 54, 0, 108, 160, 213, 315, 415, 465, 513, 608, 0},
414 static const u16 expected_tpt_siso_40MHz
[4][IWL_RATE_COUNT
] = {
415 {0, 0, 0, 0, 77, 0, 127, 160, 184, 220, 242, 250, 257, 269, 275},
416 {0, 0, 0, 0, 83, 0, 135, 169, 193, 229, 250, 257, 264, 275, 280},
417 {0, 0, 0, 0, 101, 0, 199, 295, 389, 570, 744, 828, 911, 1070, 1173},
418 {0, 0, 0, 0, 112, 0, 220, 326, 429, 629, 819, 912, 1000, 1173, 1284},
421 static const u16 expected_tpt_siso_80MHz
[4][IWL_RATE_COUNT
] = {
422 {0, 0, 0, 0, 130, 0, 191, 223, 244, 273, 288, 294, 298, 305, 308},
423 {0, 0, 0, 0, 138, 0, 200, 231, 251, 279, 293, 298, 302, 308, 312},
424 {0, 0, 0, 0, 217, 0, 429, 634, 834, 1220, 1585, 1760, 1931, 2258, 2466},
425 {0, 0, 0, 0, 241, 0, 475, 701, 921, 1343, 1741, 1931, 2117, 2468, 2691},
428 static const u16 expected_tpt_mimo2_20MHz
[4][IWL_RATE_COUNT
] = {
429 {0, 0, 0, 0, 74, 0, 123, 155, 179, 213, 235, 243, 250, 261, 0},
430 {0, 0, 0, 0, 81, 0, 131, 164, 187, 221, 242, 250, 256, 267, 0},
431 {0, 0, 0, 0, 98, 0, 193, 286, 375, 550, 718, 799, 878, 1032, 0},
432 {0, 0, 0, 0, 109, 0, 214, 316, 414, 607, 790, 879, 965, 1132, 0},
435 static const u16 expected_tpt_mimo2_40MHz
[4][IWL_RATE_COUNT
] = {
436 {0, 0, 0, 0, 123, 0, 182, 214, 235, 264, 279, 285, 289, 296, 300},
437 {0, 0, 0, 0, 131, 0, 191, 222, 242, 270, 284, 289, 293, 300, 303},
438 {0, 0, 0, 0, 200, 0, 390, 571, 741, 1067, 1365, 1505, 1640, 1894, 2053},
439 {0, 0, 0, 0, 221, 0, 430, 630, 816, 1169, 1490, 1641, 1784, 2053, 2221},
442 static const u16 expected_tpt_mimo2_80MHz
[4][IWL_RATE_COUNT
] = {
443 {0, 0, 0, 0, 182, 0, 240, 264, 278, 299, 308, 311, 313, 317, 319},
444 {0, 0, 0, 0, 190, 0, 247, 269, 282, 302, 310, 313, 315, 319, 320},
445 {0, 0, 0, 0, 428, 0, 833, 1215, 1577, 2254, 2863, 3147, 3418, 3913, 4219},
446 {0, 0, 0, 0, 474, 0, 920, 1338, 1732, 2464, 3116, 3418, 3705, 4225, 4545},
450 static const struct iwl_rate_mcs_info iwl_rate_mcs
[IWL_RATE_COUNT
] = {
459 { "24", "16QAM 1/2"},
460 { "36", "16QAM 3/4"},
461 { "48", "64QAM 2/3"},
462 { "54", "64QAM 3/4"},
463 { "60", "64QAM 5/6"},
466 #define MCS_INDEX_PER_STREAM (8)
468 static const char *rs_pretty_ant(u8 ant
)
470 static const char * const ant_name
[] = {
484 return ant_name
[ant
];
487 static const char *rs_pretty_lq_type(enum iwl_table_type type
)
489 static const char * const lq_types
[] = {
491 [LQ_LEGACY_A
] = "LEGACY_A",
492 [LQ_LEGACY_G
] = "LEGACY_G",
493 [LQ_HT_SISO
] = "HT SISO",
494 [LQ_HT_MIMO2
] = "HT MIMO",
495 [LQ_VHT_SISO
] = "VHT SISO",
496 [LQ_VHT_MIMO2
] = "VHT MIMO",
499 if (type
< LQ_NONE
|| type
>= LQ_MAX
)
502 return lq_types
[type
];
505 static inline void rs_dump_rate(struct iwl_mvm
*mvm
, const struct rs_rate
*rate
,
508 IWL_DEBUG_RATE(mvm
, "%s: (%s: %d) ANT: %s BW: %d SGI: %d LDPC: %d\n",
509 prefix
, rs_pretty_lq_type(rate
->type
),
510 rate
->index
, rs_pretty_ant(rate
->ant
),
511 rate
->bw
, rate
->sgi
, rate
->ldpc
);
514 static void rs_rate_scale_clear_window(struct iwl_rate_scale_data
*window
)
517 window
->success_counter
= 0;
518 window
->success_ratio
= IWL_INVALID_VALUE
;
520 window
->average_tpt
= IWL_INVALID_VALUE
;
523 static void rs_rate_scale_clear_tbl_windows(struct iwl_mvm
*mvm
,
524 struct iwl_scale_tbl_info
*tbl
)
528 IWL_DEBUG_RATE(mvm
, "Clearing up window stats\n");
529 for (i
= 0; i
< IWL_RATE_COUNT
; i
++)
530 rs_rate_scale_clear_window(&tbl
->win
[i
]);
532 for (i
= 0; i
< ARRAY_SIZE(tbl
->tpc_win
); i
++)
533 rs_rate_scale_clear_window(&tbl
->tpc_win
[i
]);
536 static inline u8
rs_is_valid_ant(u8 valid_antenna
, u8 ant_type
)
538 return (ant_type
& valid_antenna
) == ant_type
;
541 static int rs_tl_turn_on_agg_for_tid(struct iwl_mvm
*mvm
,
542 struct iwl_lq_sta
*lq_data
, u8 tid
,
543 struct ieee80211_sta
*sta
)
547 IWL_DEBUG_HT(mvm
, "Starting Tx agg: STA: %pM tid: %d\n",
549 ret
= ieee80211_start_tx_ba_session(sta
, tid
, 5000);
550 if (ret
== -EAGAIN
) {
552 * driver and mac80211 is out of sync
553 * this might be cause by reloading firmware
554 * stop the tx ba session here
556 IWL_ERR(mvm
, "Fail start Tx agg on tid: %d\n",
558 ieee80211_stop_tx_ba_session(sta
, tid
);
563 static void rs_tl_turn_on_agg(struct iwl_mvm
*mvm
, u8 tid
,
564 struct iwl_lq_sta
*lq_data
,
565 struct ieee80211_sta
*sta
)
567 if (tid
< IWL_MAX_TID_COUNT
)
568 rs_tl_turn_on_agg_for_tid(mvm
, lq_data
, tid
, sta
);
570 IWL_ERR(mvm
, "tid exceeds max TID count: %d/%d\n",
571 tid
, IWL_MAX_TID_COUNT
);
574 static inline int get_num_of_ant_from_rate(u32 rate_n_flags
)
576 return !!(rate_n_flags
& RATE_MCS_ANT_A_MSK
) +
577 !!(rate_n_flags
& RATE_MCS_ANT_B_MSK
) +
578 !!(rate_n_flags
& RATE_MCS_ANT_C_MSK
);
582 * Static function to get the expected throughput from an iwl_scale_tbl_info
583 * that wraps a NULL pointer check
585 static s32
get_expected_tpt(struct iwl_scale_tbl_info
*tbl
, int rs_index
)
587 if (tbl
->expected_tpt
)
588 return tbl
->expected_tpt
[rs_index
];
593 * rs_collect_tx_data - Update the success/failure sliding window
595 * We keep a sliding window of the last 62 packets transmitted
596 * at this rate. window->data contains the bitmask of successful
599 static int _rs_collect_tx_data(struct iwl_scale_tbl_info
*tbl
,
600 int scale_index
, int attempts
, int successes
,
601 struct iwl_rate_scale_data
*window
)
603 static const u64 mask
= (((u64
)1) << (IWL_RATE_MAX_WINDOW
- 1));
606 /* Get expected throughput */
607 tpt
= get_expected_tpt(tbl
, scale_index
);
610 * Keep track of only the latest 62 tx frame attempts in this rate's
611 * history window; anything older isn't really relevant any more.
612 * If we have filled up the sliding window, drop the oldest attempt;
613 * if the oldest attempt (highest bit in bitmap) shows "success",
614 * subtract "1" from the success counter (this is the main reason
615 * we keep these bitmaps!).
617 while (attempts
> 0) {
618 if (window
->counter
>= IWL_RATE_MAX_WINDOW
) {
619 /* remove earliest */
620 window
->counter
= IWL_RATE_MAX_WINDOW
- 1;
622 if (window
->data
& mask
) {
623 window
->data
&= ~mask
;
624 window
->success_counter
--;
628 /* Increment frames-attempted counter */
631 /* Shift bitmap by one frame to throw away oldest history */
634 /* Mark the most recent #successes attempts as successful */
636 window
->success_counter
++;
644 /* Calculate current success ratio, avoid divide-by-0! */
645 if (window
->counter
> 0)
646 window
->success_ratio
= 128 * (100 * window
->success_counter
)
649 window
->success_ratio
= IWL_INVALID_VALUE
;
651 fail_count
= window
->counter
- window
->success_counter
;
653 /* Calculate average throughput, if we have enough history. */
654 if ((fail_count
>= IWL_RATE_MIN_FAILURE_TH
) ||
655 (window
->success_counter
>= IWL_RATE_MIN_SUCCESS_TH
))
656 window
->average_tpt
= (window
->success_ratio
* tpt
+ 64) / 128;
658 window
->average_tpt
= IWL_INVALID_VALUE
;
663 static int rs_collect_tx_data(struct iwl_lq_sta
*lq_sta
,
664 struct iwl_scale_tbl_info
*tbl
,
665 int scale_index
, int attempts
, int successes
,
668 struct iwl_rate_scale_data
*window
= NULL
;
671 if (scale_index
< 0 || scale_index
>= IWL_RATE_COUNT
)
674 if (tbl
->column
!= RS_COLUMN_INVALID
) {
675 lq_sta
->tx_stats
[tbl
->column
][scale_index
].total
+= attempts
;
676 lq_sta
->tx_stats
[tbl
->column
][scale_index
].success
+= successes
;
679 /* Select window for current tx bit rate */
680 window
= &(tbl
->win
[scale_index
]);
682 ret
= _rs_collect_tx_data(tbl
, scale_index
, attempts
, successes
,
687 if (WARN_ON_ONCE(reduced_txp
> TPC_MAX_REDUCTION
))
690 window
= &tbl
->tpc_win
[reduced_txp
];
691 return _rs_collect_tx_data(tbl
, scale_index
, attempts
, successes
,
695 /* Convert rs_rate object into ucode rate bitmask */
696 static u32
ucode_rate_from_rs_rate(struct iwl_mvm
*mvm
,
697 struct rs_rate
*rate
)
700 int index
= rate
->index
;
702 ucode_rate
|= ((rate
->ant
<< RATE_MCS_ANT_POS
) &
703 RATE_MCS_ANT_ABC_MSK
);
705 if (is_legacy(rate
)) {
706 ucode_rate
|= iwl_rates
[index
].plcp
;
707 if (index
>= IWL_FIRST_CCK_RATE
&& index
<= IWL_LAST_CCK_RATE
)
708 ucode_rate
|= RATE_MCS_CCK_MSK
;
713 if (index
< IWL_FIRST_HT_RATE
|| index
> IWL_LAST_HT_RATE
) {
714 IWL_ERR(mvm
, "Invalid HT rate index %d\n", index
);
715 index
= IWL_LAST_HT_RATE
;
717 ucode_rate
|= RATE_MCS_HT_MSK
;
719 if (is_ht_siso(rate
))
720 ucode_rate
|= iwl_rates
[index
].plcp_ht_siso
;
721 else if (is_ht_mimo2(rate
))
722 ucode_rate
|= iwl_rates
[index
].plcp_ht_mimo2
;
725 } else if (is_vht(rate
)) {
726 if (index
< IWL_FIRST_VHT_RATE
|| index
> IWL_LAST_VHT_RATE
) {
727 IWL_ERR(mvm
, "Invalid VHT rate index %d\n", index
);
728 index
= IWL_LAST_VHT_RATE
;
730 ucode_rate
|= RATE_MCS_VHT_MSK
;
731 if (is_vht_siso(rate
))
732 ucode_rate
|= iwl_rates
[index
].plcp_vht_siso
;
733 else if (is_vht_mimo2(rate
))
734 ucode_rate
|= iwl_rates
[index
].plcp_vht_mimo2
;
739 IWL_ERR(mvm
, "Invalid rate->type %d\n", rate
->type
);
742 ucode_rate
|= rate
->bw
;
744 ucode_rate
|= RATE_MCS_SGI_MSK
;
746 ucode_rate
|= RATE_MCS_LDPC_MSK
;
751 /* Convert a ucode rate into an rs_rate object */
752 static int rs_rate_from_ucode_rate(const u32 ucode_rate
,
753 enum ieee80211_band band
,
754 struct rs_rate
*rate
)
756 u32 ant_msk
= ucode_rate
& RATE_MCS_ANT_ABC_MSK
;
757 u8 num_of_ant
= get_num_of_ant_from_rate(ucode_rate
);
760 memset(rate
, 0, sizeof(*rate
));
761 rate
->index
= iwl_hwrate_to_plcp_idx(ucode_rate
);
763 if (rate
->index
== IWL_RATE_INVALID
)
766 rate
->ant
= (ant_msk
>> RATE_MCS_ANT_POS
);
769 if (!(ucode_rate
& RATE_MCS_HT_MSK
) &&
770 !(ucode_rate
& RATE_MCS_VHT_MSK
)) {
771 if (num_of_ant
== 1) {
772 if (band
== IEEE80211_BAND_5GHZ
)
773 rate
->type
= LQ_LEGACY_A
;
775 rate
->type
= LQ_LEGACY_G
;
782 if (ucode_rate
& RATE_MCS_SGI_MSK
)
784 if (ucode_rate
& RATE_MCS_LDPC_MSK
)
787 rate
->bw
= ucode_rate
& RATE_MCS_CHAN_WIDTH_MSK
;
789 if (ucode_rate
& RATE_MCS_HT_MSK
) {
790 nss
= ((ucode_rate
& RATE_HT_MCS_NSS_MSK
) >>
791 RATE_HT_MCS_NSS_POS
) + 1;
794 rate
->type
= LQ_HT_SISO
;
795 WARN_ON_ONCE(num_of_ant
!= 1);
796 } else if (nss
== 2) {
797 rate
->type
= LQ_HT_MIMO2
;
798 WARN_ON_ONCE(num_of_ant
!= 2);
802 } else if (ucode_rate
& RATE_MCS_VHT_MSK
) {
803 nss
= ((ucode_rate
& RATE_VHT_MCS_NSS_MSK
) >>
804 RATE_VHT_MCS_NSS_POS
) + 1;
807 rate
->type
= LQ_VHT_SISO
;
808 WARN_ON_ONCE(num_of_ant
!= 1);
809 } else if (nss
== 2) {
810 rate
->type
= LQ_VHT_MIMO2
;
811 WARN_ON_ONCE(num_of_ant
!= 2);
817 WARN_ON_ONCE(rate
->bw
== RATE_MCS_CHAN_WIDTH_160
);
818 WARN_ON_ONCE(rate
->bw
== RATE_MCS_CHAN_WIDTH_80
&&
824 /* switch to another antenna/antennas and return 1 */
825 /* if no other valid antenna found, return 0 */
826 static int rs_toggle_antenna(u32 valid_ant
, struct rs_rate
*rate
)
830 if (!rate
->ant
|| rate
->ant
> ANT_ABC
)
833 if (!rs_is_valid_ant(valid_ant
, rate
->ant
))
836 new_ant_type
= ant_toggle_lookup
[rate
->ant
];
838 while ((new_ant_type
!= rate
->ant
) &&
839 !rs_is_valid_ant(valid_ant
, new_ant_type
))
840 new_ant_type
= ant_toggle_lookup
[new_ant_type
];
842 if (new_ant_type
== rate
->ant
)
845 rate
->ant
= new_ant_type
;
850 static u16
rs_get_supported_rates(struct iwl_lq_sta
*lq_sta
,
851 struct rs_rate
*rate
)
854 return lq_sta
->active_legacy_rate
;
855 else if (is_siso(rate
))
856 return lq_sta
->active_siso_rate
;
857 else if (is_mimo2(rate
))
858 return lq_sta
->active_mimo2_rate
;
864 static u16
rs_get_adjacent_rate(struct iwl_mvm
*mvm
, u8 index
, u16 rate_mask
,
867 u8 high
= IWL_RATE_INVALID
;
868 u8 low
= IWL_RATE_INVALID
;
870 /* 802.11A or ht walks to the next literal adjacent rate in
872 if (is_type_a_band(rate_type
) || !is_type_legacy(rate_type
)) {
876 /* Find the previous rate that is in the rate mask */
878 for (mask
= (1 << i
); i
>= 0; i
--, mask
>>= 1) {
879 if (rate_mask
& mask
) {
885 /* Find the next rate that is in the rate mask */
887 for (mask
= (1 << i
); i
< IWL_RATE_COUNT
; i
++, mask
<<= 1) {
888 if (rate_mask
& mask
) {
894 return (high
<< 8) | low
;
898 while (low
!= IWL_RATE_INVALID
) {
899 low
= iwl_rates
[low
].prev_rs
;
900 if (low
== IWL_RATE_INVALID
)
902 if (rate_mask
& (1 << low
))
904 IWL_DEBUG_RATE(mvm
, "Skipping masked lower rate: %d\n", low
);
908 while (high
!= IWL_RATE_INVALID
) {
909 high
= iwl_rates
[high
].next_rs
;
910 if (high
== IWL_RATE_INVALID
)
912 if (rate_mask
& (1 << high
))
914 IWL_DEBUG_RATE(mvm
, "Skipping masked higher rate: %d\n", high
);
917 return (high
<< 8) | low
;
920 static inline bool rs_rate_supported(struct iwl_lq_sta
*lq_sta
,
921 struct rs_rate
*rate
)
923 return BIT(rate
->index
) & rs_get_supported_rates(lq_sta
, rate
);
926 /* Get the next supported lower rate in the current column.
927 * Return true if bottom rate in the current column was reached
929 static bool rs_get_lower_rate_in_column(struct iwl_lq_sta
*lq_sta
,
930 struct rs_rate
*rate
)
935 struct iwl_mvm
*mvm
= lq_sta
->pers
.drv
;
937 rate_mask
= rs_get_supported_rates(lq_sta
, rate
);
938 high_low
= rs_get_adjacent_rate(mvm
, rate
->index
, rate_mask
,
940 low
= high_low
& 0xff;
942 /* Bottom rate of column reached */
943 if (low
== IWL_RATE_INVALID
)
950 /* Get the next rate to use following a column downgrade */
951 static void rs_get_lower_rate_down_column(struct iwl_lq_sta
*lq_sta
,
952 struct rs_rate
*rate
)
954 struct iwl_mvm
*mvm
= lq_sta
->pers
.drv
;
956 if (is_legacy(rate
)) {
957 /* No column to downgrade from Legacy */
959 } else if (is_siso(rate
)) {
960 /* Downgrade to Legacy if we were in SISO */
961 if (lq_sta
->band
== IEEE80211_BAND_5GHZ
)
962 rate
->type
= LQ_LEGACY_A
;
964 rate
->type
= LQ_LEGACY_G
;
966 rate
->bw
= RATE_MCS_CHAN_WIDTH_20
;
968 WARN_ON_ONCE(rate
->index
< IWL_RATE_MCS_0_INDEX
||
969 rate
->index
> IWL_RATE_MCS_9_INDEX
);
971 rate
->index
= rs_ht_to_legacy
[rate
->index
];
974 /* Downgrade to SISO with same MCS if in MIMO */
975 rate
->type
= is_vht_mimo2(rate
) ?
976 LQ_VHT_SISO
: LQ_HT_SISO
;
979 if (num_of_ant(rate
->ant
) > 1)
980 rate
->ant
= first_antenna(mvm
->fw
->valid_tx_ant
);
982 /* Relevant in both switching to SISO or Legacy */
985 if (!rs_rate_supported(lq_sta
, rate
))
986 rs_get_lower_rate_in_column(lq_sta
, rate
);
989 /* Simple function to compare two rate scale table types */
990 static inline bool rs_rate_match(struct rs_rate
*a
,
993 return (a
->type
== b
->type
) && (a
->ant
== b
->ant
) && (a
->sgi
== b
->sgi
);
996 static u32
rs_ch_width_from_mac_flags(enum mac80211_rate_control_flags flags
)
998 if (flags
& IEEE80211_TX_RC_40_MHZ_WIDTH
)
999 return RATE_MCS_CHAN_WIDTH_40
;
1000 else if (flags
& IEEE80211_TX_RC_80_MHZ_WIDTH
)
1001 return RATE_MCS_CHAN_WIDTH_80
;
1002 else if (flags
& IEEE80211_TX_RC_160_MHZ_WIDTH
)
1003 return RATE_MCS_CHAN_WIDTH_160
;
1005 return RATE_MCS_CHAN_WIDTH_20
;
1009 * mac80211 sends us Tx status
1011 static void rs_tx_status(void *mvm_r
, struct ieee80211_supported_band
*sband
,
1012 struct ieee80211_sta
*sta
, void *priv_sta
,
1013 struct sk_buff
*skb
)
1018 struct iwl_lq_sta
*lq_sta
= priv_sta
;
1019 struct iwl_lq_cmd
*table
;
1020 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*)skb
->data
;
1021 struct iwl_op_mode
*op_mode
= (struct iwl_op_mode
*)mvm_r
;
1022 struct iwl_mvm
*mvm
= IWL_OP_MODE_GET_MVM(op_mode
);
1023 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
1024 enum mac80211_rate_control_flags mac_flags
;
1026 struct rs_rate rate
;
1027 struct iwl_scale_tbl_info
*curr_tbl
, *other_tbl
, *tmp_tbl
;
1028 u8 reduced_txp
= (uintptr_t)info
->status
.status_driver_data
[0];
1030 /* Treat uninitialized rate scaling data same as non-existing. */
1032 IWL_DEBUG_RATE(mvm
, "Station rate scaling not created yet.\n");
1034 } else if (!lq_sta
->pers
.drv
) {
1035 IWL_DEBUG_RATE(mvm
, "Rate scaling not initialized yet.\n");
1039 #ifdef CONFIG_MAC80211_DEBUGFS
1040 /* Disable last tx check if we are debugging with fixed rate */
1041 if (lq_sta
->pers
.dbg_fixed_rate
) {
1042 IWL_DEBUG_RATE(mvm
, "Fixed rate. avoid rate scaling\n");
1046 if (!ieee80211_is_data(hdr
->frame_control
) ||
1047 info
->flags
& IEEE80211_TX_CTL_NO_ACK
)
1050 /* This packet was aggregated but doesn't carry status info */
1051 if ((info
->flags
& IEEE80211_TX_CTL_AMPDU
) &&
1052 !(info
->flags
& IEEE80211_TX_STAT_AMPDU
))
1056 * Ignore this Tx frame response if its initial rate doesn't match
1057 * that of latest Link Quality command. There may be stragglers
1058 * from a previous Link Quality command, but we're no longer interested
1059 * in those; they're either from the "active" mode while we're trying
1060 * to check "search" mode, or a prior "search" mode after we've moved
1061 * to a new "search" mode (which might become the new "active" mode).
1063 table
= &lq_sta
->lq
;
1064 ucode_rate
= le32_to_cpu(table
->rs_table
[0]);
1065 rs_rate_from_ucode_rate(ucode_rate
, info
->band
, &rate
);
1066 if (info
->band
== IEEE80211_BAND_5GHZ
)
1067 rate
.index
-= IWL_FIRST_OFDM_RATE
;
1068 mac_flags
= info
->status
.rates
[0].flags
;
1069 mac_index
= info
->status
.rates
[0].idx
;
1070 /* For HT packets, map MCS to PLCP */
1071 if (mac_flags
& IEEE80211_TX_RC_MCS
) {
1072 /* Remove # of streams */
1073 mac_index
&= RATE_HT_MCS_RATE_CODE_MSK
;
1074 if (mac_index
>= (IWL_RATE_9M_INDEX
- IWL_FIRST_OFDM_RATE
))
1077 * mac80211 HT index is always zero-indexed; we need to move
1078 * HT OFDM rates after CCK rates in 2.4 GHz band
1080 if (info
->band
== IEEE80211_BAND_2GHZ
)
1081 mac_index
+= IWL_FIRST_OFDM_RATE
;
1082 } else if (mac_flags
& IEEE80211_TX_RC_VHT_MCS
) {
1083 mac_index
&= RATE_VHT_MCS_RATE_CODE_MSK
;
1084 if (mac_index
>= (IWL_RATE_9M_INDEX
- IWL_FIRST_OFDM_RATE
))
1088 if (time_after(jiffies
,
1089 (unsigned long)(lq_sta
->last_tx
+ RS_IDLE_TIMEOUT
))) {
1091 IWL_DEBUG_RATE(mvm
, "Tx idle for too long. reinit rs\n");
1092 for (tid
= 0; tid
< IWL_MAX_TID_COUNT
; tid
++)
1093 ieee80211_stop_tx_ba_session(sta
, tid
);
1095 iwl_mvm_rs_rate_init(mvm
, sta
, sband
->band
, false);
1098 lq_sta
->last_tx
= jiffies
;
1100 /* Here we actually compare this rate to the latest LQ command */
1101 if ((mac_index
< 0) ||
1102 (rate
.sgi
!= !!(mac_flags
& IEEE80211_TX_RC_SHORT_GI
)) ||
1103 (rate
.bw
!= rs_ch_width_from_mac_flags(mac_flags
)) ||
1104 (rate
.ant
!= info
->status
.antenna
) ||
1105 (!!(ucode_rate
& RATE_MCS_HT_MSK
) !=
1106 !!(mac_flags
& IEEE80211_TX_RC_MCS
)) ||
1107 (!!(ucode_rate
& RATE_MCS_VHT_MSK
) !=
1108 !!(mac_flags
& IEEE80211_TX_RC_VHT_MCS
)) ||
1109 (!!(ucode_rate
& RATE_HT_MCS_GF_MSK
) !=
1110 !!(mac_flags
& IEEE80211_TX_RC_GREEN_FIELD
)) ||
1111 (rate
.index
!= mac_index
)) {
1113 "initial rate %d does not match %d (0x%x)\n",
1114 mac_index
, rate
.index
, ucode_rate
);
1116 * Since rates mis-match, the last LQ command may have failed.
1117 * After IWL_MISSED_RATE_MAX mis-matches, resync the uCode with
1120 lq_sta
->missed_rate_counter
++;
1121 if (lq_sta
->missed_rate_counter
> IWL_MISSED_RATE_MAX
) {
1122 lq_sta
->missed_rate_counter
= 0;
1124 "Too many rates mismatch. Send sync LQ. rs_state %d\n",
1126 iwl_mvm_send_lq_cmd(mvm
, &lq_sta
->lq
, false);
1128 /* Regardless, ignore this status info for outdated rate */
1131 /* Rate did match, so reset the missed_rate_counter */
1132 lq_sta
->missed_rate_counter
= 0;
1134 /* Figure out if rate scale algorithm is in active or search table */
1135 if (rs_rate_match(&rate
,
1136 &(lq_sta
->lq_info
[lq_sta
->active_tbl
].rate
))) {
1137 curr_tbl
= &(lq_sta
->lq_info
[lq_sta
->active_tbl
]);
1138 other_tbl
= &(lq_sta
->lq_info
[1 - lq_sta
->active_tbl
]);
1139 } else if (rs_rate_match(&rate
,
1140 &lq_sta
->lq_info
[1 - lq_sta
->active_tbl
].rate
)) {
1141 curr_tbl
= &(lq_sta
->lq_info
[1 - lq_sta
->active_tbl
]);
1142 other_tbl
= &(lq_sta
->lq_info
[lq_sta
->active_tbl
]);
1145 "Neither active nor search matches tx rate\n");
1146 tmp_tbl
= &(lq_sta
->lq_info
[lq_sta
->active_tbl
]);
1147 rs_dump_rate(mvm
, &tmp_tbl
->rate
, "ACTIVE");
1148 tmp_tbl
= &(lq_sta
->lq_info
[1 - lq_sta
->active_tbl
]);
1149 rs_dump_rate(mvm
, &tmp_tbl
->rate
, "SEARCH");
1150 rs_dump_rate(mvm
, &rate
, "ACTUAL");
1153 * no matching table found, let's by-pass the data collection
1154 * and continue to perform rate scale to find the rate table
1156 rs_stay_in_table(lq_sta
, true);
1161 * Updating the frame history depends on whether packets were
1164 * For aggregation, all packets were transmitted at the same rate, the
1165 * first index into rate scale table.
1167 if (info
->flags
& IEEE80211_TX_STAT_AMPDU
) {
1168 ucode_rate
= le32_to_cpu(table
->rs_table
[0]);
1169 rs_rate_from_ucode_rate(ucode_rate
, info
->band
, &rate
);
1170 rs_collect_tx_data(lq_sta
, curr_tbl
, rate
.index
,
1171 info
->status
.ampdu_len
,
1172 info
->status
.ampdu_ack_len
,
1175 /* Update success/fail counts if not searching for new mode */
1176 if (lq_sta
->rs_state
== RS_STATE_STAY_IN_COLUMN
) {
1177 lq_sta
->total_success
+= info
->status
.ampdu_ack_len
;
1178 lq_sta
->total_failed
+= (info
->status
.ampdu_len
-
1179 info
->status
.ampdu_ack_len
);
1183 * For legacy, update frame history with for each Tx retry.
1185 retries
= info
->status
.rates
[0].count
- 1;
1186 /* HW doesn't send more than 15 retries */
1187 retries
= min(retries
, 15);
1189 /* The last transmission may have been successful */
1190 legacy_success
= !!(info
->flags
& IEEE80211_TX_STAT_ACK
);
1191 /* Collect data for each rate used during failed TX attempts */
1192 for (i
= 0; i
<= retries
; ++i
) {
1193 ucode_rate
= le32_to_cpu(table
->rs_table
[i
]);
1194 rs_rate_from_ucode_rate(ucode_rate
, info
->band
, &rate
);
1196 * Only collect stats if retried rate is in the same RS
1197 * table as active/search.
1199 if (rs_rate_match(&rate
, &curr_tbl
->rate
))
1201 else if (rs_rate_match(&rate
, &other_tbl
->rate
))
1202 tmp_tbl
= other_tbl
;
1206 rs_collect_tx_data(lq_sta
, tmp_tbl
, rate
.index
, 1,
1207 i
< retries
? 0 : legacy_success
,
1211 /* Update success/fail counts if not searching for new mode */
1212 if (lq_sta
->rs_state
== RS_STATE_STAY_IN_COLUMN
) {
1213 lq_sta
->total_success
+= legacy_success
;
1214 lq_sta
->total_failed
+= retries
+ (1 - legacy_success
);
1217 /* The last TX rate is cached in lq_sta; it's set in if/else above */
1218 lq_sta
->last_rate_n_flags
= ucode_rate
;
1219 IWL_DEBUG_RATE(mvm
, "reduced txpower: %d\n", reduced_txp
);
1221 /* See if there's a better rate or modulation mode to try. */
1222 if (sta
&& sta
->supp_rates
[sband
->band
])
1223 rs_rate_scale_perform(mvm
, skb
, sta
, lq_sta
);
1227 * Begin a period of staying with a selected modulation mode.
1228 * Set "stay_in_tbl" flag to prevent any mode switches.
1229 * Set frame tx success limits according to legacy vs. high-throughput,
1230 * and reset overall (spanning all rates) tx success history statistics.
1231 * These control how long we stay using same modulation mode before
1232 * searching for a new mode.
1234 static void rs_set_stay_in_table(struct iwl_mvm
*mvm
, u8 is_legacy
,
1235 struct iwl_lq_sta
*lq_sta
)
1237 IWL_DEBUG_RATE(mvm
, "Moving to RS_STATE_STAY_IN_COLUMN\n");
1238 lq_sta
->rs_state
= RS_STATE_STAY_IN_COLUMN
;
1240 lq_sta
->table_count_limit
= IWL_LEGACY_TABLE_COUNT
;
1241 lq_sta
->max_failure_limit
= IWL_LEGACY_FAILURE_LIMIT
;
1242 lq_sta
->max_success_limit
= IWL_LEGACY_SUCCESS_LIMIT
;
1244 lq_sta
->table_count_limit
= IWL_NONE_LEGACY_TABLE_COUNT
;
1245 lq_sta
->max_failure_limit
= IWL_NONE_LEGACY_FAILURE_LIMIT
;
1246 lq_sta
->max_success_limit
= IWL_NONE_LEGACY_SUCCESS_LIMIT
;
1248 lq_sta
->table_count
= 0;
1249 lq_sta
->total_failed
= 0;
1250 lq_sta
->total_success
= 0;
1251 lq_sta
->flush_timer
= jiffies
;
1252 lq_sta
->visited_columns
= 0;
1255 static int rs_get_max_allowed_rate(struct iwl_lq_sta
*lq_sta
,
1256 const struct rs_tx_column
*column
)
1258 switch (column
->mode
) {
1260 return lq_sta
->max_legacy_rate_idx
;
1262 return lq_sta
->max_siso_rate_idx
;
1264 return lq_sta
->max_mimo2_rate_idx
;
1269 return lq_sta
->max_legacy_rate_idx
;
1272 static const u16
*rs_get_expected_tpt_table(struct iwl_lq_sta
*lq_sta
,
1273 const struct rs_tx_column
*column
,
1276 /* Used to choose among HT tables */
1277 const u16 (*ht_tbl_pointer
)[IWL_RATE_COUNT
];
1279 if (WARN_ON_ONCE(column
->mode
!= RS_LEGACY
&&
1280 column
->mode
!= RS_SISO
&&
1281 column
->mode
!= RS_MIMO2
))
1282 return expected_tpt_legacy
;
1284 /* Legacy rates have only one table */
1285 if (column
->mode
== RS_LEGACY
)
1286 return expected_tpt_legacy
;
1288 ht_tbl_pointer
= expected_tpt_mimo2_20MHz
;
1289 /* Choose among many HT tables depending on number of streams
1290 * (SISO/MIMO2), channel width (20/40/80), SGI, and aggregation
1292 if (column
->mode
== RS_SISO
) {
1294 case RATE_MCS_CHAN_WIDTH_20
:
1295 ht_tbl_pointer
= expected_tpt_siso_20MHz
;
1297 case RATE_MCS_CHAN_WIDTH_40
:
1298 ht_tbl_pointer
= expected_tpt_siso_40MHz
;
1300 case RATE_MCS_CHAN_WIDTH_80
:
1301 ht_tbl_pointer
= expected_tpt_siso_80MHz
;
1306 } else if (column
->mode
== RS_MIMO2
) {
1308 case RATE_MCS_CHAN_WIDTH_20
:
1309 ht_tbl_pointer
= expected_tpt_mimo2_20MHz
;
1311 case RATE_MCS_CHAN_WIDTH_40
:
1312 ht_tbl_pointer
= expected_tpt_mimo2_40MHz
;
1314 case RATE_MCS_CHAN_WIDTH_80
:
1315 ht_tbl_pointer
= expected_tpt_mimo2_80MHz
;
1324 if (!column
->sgi
&& !lq_sta
->is_agg
) /* Normal */
1325 return ht_tbl_pointer
[0];
1326 else if (column
->sgi
&& !lq_sta
->is_agg
) /* SGI */
1327 return ht_tbl_pointer
[1];
1328 else if (!column
->sgi
&& lq_sta
->is_agg
) /* AGG */
1329 return ht_tbl_pointer
[2];
1331 return ht_tbl_pointer
[3];
1334 static void rs_set_expected_tpt_table(struct iwl_lq_sta
*lq_sta
,
1335 struct iwl_scale_tbl_info
*tbl
)
1337 struct rs_rate
*rate
= &tbl
->rate
;
1338 const struct rs_tx_column
*column
= &rs_tx_columns
[tbl
->column
];
1340 tbl
->expected_tpt
= rs_get_expected_tpt_table(lq_sta
, column
, rate
->bw
);
1343 static s32
rs_get_best_rate(struct iwl_mvm
*mvm
,
1344 struct iwl_lq_sta
*lq_sta
,
1345 struct iwl_scale_tbl_info
*tbl
, /* "search" */
1346 unsigned long rate_mask
, s8 index
)
1348 struct iwl_scale_tbl_info
*active_tbl
=
1349 &(lq_sta
->lq_info
[lq_sta
->active_tbl
]);
1350 s32 success_ratio
= active_tbl
->win
[index
].success_ratio
;
1351 u16 expected_current_tpt
= active_tbl
->expected_tpt
[index
];
1352 const u16
*tpt_tbl
= tbl
->expected_tpt
;
1357 if (success_ratio
> RS_SR_NO_DECREASE
) {
1358 target_tpt
= 100 * expected_current_tpt
;
1360 "SR %d high. Find rate exceeding EXPECTED_CURRENT %d\n",
1361 success_ratio
, target_tpt
);
1363 target_tpt
= lq_sta
->last_tpt
;
1365 "SR %d not thag good. Find rate exceeding ACTUAL_TPT %d\n",
1366 success_ratio
, target_tpt
);
1369 rate_idx
= find_first_bit(&rate_mask
, BITS_PER_LONG
);
1371 while (rate_idx
!= IWL_RATE_INVALID
) {
1372 if (target_tpt
< (100 * tpt_tbl
[rate_idx
]))
1375 high_low
= rs_get_adjacent_rate(mvm
, rate_idx
, rate_mask
,
1378 rate_idx
= (high_low
>> 8) & 0xff;
1381 IWL_DEBUG_RATE(mvm
, "Best rate found %d target_tp %d expected_new %d\n",
1382 rate_idx
, target_tpt
,
1383 rate_idx
!= IWL_RATE_INVALID
?
1384 100 * tpt_tbl
[rate_idx
] : IWL_INVALID_VALUE
);
1389 static u32
rs_bw_from_sta_bw(struct ieee80211_sta
*sta
)
1391 if (sta
->bandwidth
>= IEEE80211_STA_RX_BW_80
)
1392 return RATE_MCS_CHAN_WIDTH_80
;
1393 else if (sta
->bandwidth
>= IEEE80211_STA_RX_BW_40
)
1394 return RATE_MCS_CHAN_WIDTH_40
;
1396 return RATE_MCS_CHAN_WIDTH_20
;
1400 * Check whether we should continue using same modulation mode, or
1401 * begin search for a new mode, based on:
1402 * 1) # tx successes or failures while using this mode
1403 * 2) # times calling this function
1404 * 3) elapsed time in this mode (not used, for now)
1406 static void rs_stay_in_table(struct iwl_lq_sta
*lq_sta
, bool force_search
)
1408 struct iwl_scale_tbl_info
*tbl
;
1410 int flush_interval_passed
= 0;
1411 struct iwl_mvm
*mvm
;
1413 mvm
= lq_sta
->pers
.drv
;
1414 active_tbl
= lq_sta
->active_tbl
;
1416 tbl
= &(lq_sta
->lq_info
[active_tbl
]);
1418 /* If we've been disallowing search, see if we should now allow it */
1419 if (lq_sta
->rs_state
== RS_STATE_STAY_IN_COLUMN
) {
1420 /* Elapsed time using current modulation mode */
1421 if (lq_sta
->flush_timer
)
1422 flush_interval_passed
=
1424 (unsigned long)(lq_sta
->flush_timer
+
1425 RS_STAY_IN_COLUMN_TIMEOUT
));
1428 * Check if we should allow search for new modulation mode.
1429 * If many frames have failed or succeeded, or we've used
1430 * this same modulation for a long time, allow search, and
1431 * reset history stats that keep track of whether we should
1432 * allow a new search. Also (below) reset all bitmaps and
1433 * stats in active history.
1436 (lq_sta
->total_failed
> lq_sta
->max_failure_limit
) ||
1437 (lq_sta
->total_success
> lq_sta
->max_success_limit
) ||
1438 ((!lq_sta
->search_better_tbl
) &&
1439 (lq_sta
->flush_timer
) && (flush_interval_passed
))) {
1441 "LQ: stay is expired %d %d %d\n",
1442 lq_sta
->total_failed
,
1443 lq_sta
->total_success
,
1444 flush_interval_passed
);
1446 /* Allow search for new mode */
1447 lq_sta
->rs_state
= RS_STATE_SEARCH_CYCLE_STARTED
;
1449 "Moving to RS_STATE_SEARCH_CYCLE_STARTED\n");
1450 lq_sta
->total_failed
= 0;
1451 lq_sta
->total_success
= 0;
1452 lq_sta
->flush_timer
= 0;
1453 /* mark the current column as visited */
1454 lq_sta
->visited_columns
= BIT(tbl
->column
);
1456 * Else if we've used this modulation mode enough repetitions
1457 * (regardless of elapsed time or success/failure), reset
1458 * history bitmaps and rate-specific stats for all rates in
1462 lq_sta
->table_count
++;
1463 if (lq_sta
->table_count
>=
1464 lq_sta
->table_count_limit
) {
1465 lq_sta
->table_count
= 0;
1468 "LQ: stay in table clear win\n");
1469 rs_rate_scale_clear_tbl_windows(mvm
, tbl
);
1473 /* If transitioning to allow "search", reset all history
1474 * bitmaps and stats in active table (this will become the new
1475 * "search" table). */
1476 if (lq_sta
->rs_state
== RS_STATE_SEARCH_CYCLE_STARTED
) {
1477 rs_rate_scale_clear_tbl_windows(mvm
, tbl
);
1483 * setup rate table in uCode
1485 static void rs_update_rate_tbl(struct iwl_mvm
*mvm
,
1486 struct ieee80211_sta
*sta
,
1487 struct iwl_lq_sta
*lq_sta
,
1488 struct rs_rate
*rate
)
1490 rs_fill_lq_cmd(mvm
, sta
, lq_sta
, rate
);
1491 iwl_mvm_send_lq_cmd(mvm
, &lq_sta
->lq
, false);
1494 static u8
rs_get_tid(struct iwl_lq_sta
*lq_data
,
1495 struct ieee80211_hdr
*hdr
)
1497 u8 tid
= IWL_MAX_TID_COUNT
;
1499 if (ieee80211_is_data_qos(hdr
->frame_control
)) {
1500 u8
*qc
= ieee80211_get_qos_ctl(hdr
);
1504 if (unlikely(tid
> IWL_MAX_TID_COUNT
))
1505 tid
= IWL_MAX_TID_COUNT
;
1510 static enum rs_column
rs_get_next_column(struct iwl_mvm
*mvm
,
1511 struct iwl_lq_sta
*lq_sta
,
1512 struct ieee80211_sta
*sta
,
1513 struct iwl_scale_tbl_info
*tbl
)
1516 enum rs_column next_col_id
;
1517 const struct rs_tx_column
*curr_col
= &rs_tx_columns
[tbl
->column
];
1518 const struct rs_tx_column
*next_col
;
1519 allow_column_func_t allow_func
;
1520 u8 valid_ants
= mvm
->fw
->valid_tx_ant
;
1521 const u16
*expected_tpt_tbl
;
1522 u16 tpt
, max_expected_tpt
;
1524 for (i
= 0; i
< MAX_NEXT_COLUMNS
; i
++) {
1525 next_col_id
= curr_col
->next_columns
[i
];
1527 if (next_col_id
== RS_COLUMN_INVALID
)
1530 if (lq_sta
->visited_columns
& BIT(next_col_id
)) {
1531 IWL_DEBUG_RATE(mvm
, "Skip already visited column %d\n",
1536 next_col
= &rs_tx_columns
[next_col_id
];
1538 if (!rs_is_valid_ant(valid_ants
, next_col
->ant
)) {
1540 "Skip column %d as ANT config isn't supported by chip. valid_ants 0x%x column ant 0x%x\n",
1541 next_col_id
, valid_ants
, next_col
->ant
);
1545 for (j
= 0; j
< MAX_COLUMN_CHECKS
; j
++) {
1546 allow_func
= next_col
->checks
[j
];
1547 if (allow_func
&& !allow_func(mvm
, sta
, tbl
))
1551 if (j
!= MAX_COLUMN_CHECKS
) {
1553 "Skip column %d: not allowed (check %d failed)\n",
1559 tpt
= lq_sta
->last_tpt
/ 100;
1560 expected_tpt_tbl
= rs_get_expected_tpt_table(lq_sta
, next_col
,
1561 rs_bw_from_sta_bw(sta
));
1562 if (WARN_ON_ONCE(!expected_tpt_tbl
))
1565 max_rate
= rs_get_max_allowed_rate(lq_sta
, next_col
);
1566 if (WARN_ON_ONCE(max_rate
== IWL_RATE_INVALID
))
1569 max_expected_tpt
= expected_tpt_tbl
[max_rate
];
1570 if (tpt
>= max_expected_tpt
) {
1572 "Skip column %d: can't beat current TPT. Max expected %d current %d\n",
1573 next_col_id
, max_expected_tpt
, tpt
);
1578 "Found potential column %d. Max expected %d current %d\n",
1579 next_col_id
, max_expected_tpt
, tpt
);
1583 if (i
== MAX_NEXT_COLUMNS
)
1584 return RS_COLUMN_INVALID
;
1589 static int rs_switch_to_column(struct iwl_mvm
*mvm
,
1590 struct iwl_lq_sta
*lq_sta
,
1591 struct ieee80211_sta
*sta
,
1592 enum rs_column col_id
)
1594 struct iwl_scale_tbl_info
*tbl
= &(lq_sta
->lq_info
[lq_sta
->active_tbl
]);
1595 struct iwl_scale_tbl_info
*search_tbl
=
1596 &(lq_sta
->lq_info
[(1 - lq_sta
->active_tbl
)]);
1597 struct rs_rate
*rate
= &search_tbl
->rate
;
1598 const struct rs_tx_column
*column
= &rs_tx_columns
[col_id
];
1599 const struct rs_tx_column
*curr_column
= &rs_tx_columns
[tbl
->column
];
1600 u32 sz
= (sizeof(struct iwl_scale_tbl_info
) -
1601 (sizeof(struct iwl_rate_scale_data
) * IWL_RATE_COUNT
));
1602 unsigned long rate_mask
= 0;
1605 memcpy(search_tbl
, tbl
, sz
);
1607 rate
->sgi
= column
->sgi
;
1608 rate
->ant
= column
->ant
;
1610 if (column
->mode
== RS_LEGACY
) {
1611 if (lq_sta
->band
== IEEE80211_BAND_5GHZ
)
1612 rate
->type
= LQ_LEGACY_A
;
1614 rate
->type
= LQ_LEGACY_G
;
1616 rate_mask
= lq_sta
->active_legacy_rate
;
1617 } else if (column
->mode
== RS_SISO
) {
1618 rate
->type
= lq_sta
->is_vht
? LQ_VHT_SISO
: LQ_HT_SISO
;
1619 rate_mask
= lq_sta
->active_siso_rate
;
1620 } else if (column
->mode
== RS_MIMO2
) {
1621 rate
->type
= lq_sta
->is_vht
? LQ_VHT_MIMO2
: LQ_HT_MIMO2
;
1622 rate_mask
= lq_sta
->active_mimo2_rate
;
1624 WARN_ON_ONCE("Bad column mode");
1627 rate
->bw
= rs_bw_from_sta_bw(sta
);
1628 rate
->ldpc
= lq_sta
->ldpc
;
1629 search_tbl
->column
= col_id
;
1630 rs_set_expected_tpt_table(lq_sta
, search_tbl
);
1632 lq_sta
->visited_columns
|= BIT(col_id
);
1634 /* Get the best matching rate if we're changing modes. e.g.
1635 * SISO->MIMO, LEGACY->SISO, MIMO->SISO
1637 if (curr_column
->mode
!= column
->mode
) {
1638 rate_idx
= rs_get_best_rate(mvm
, lq_sta
, search_tbl
,
1639 rate_mask
, rate
->index
);
1641 if ((rate_idx
== IWL_RATE_INVALID
) ||
1642 !(BIT(rate_idx
) & rate_mask
)) {
1644 "can not switch with index %d"
1646 rate_idx
, rate_mask
);
1651 rate
->index
= rate_idx
;
1654 IWL_DEBUG_RATE(mvm
, "Switched to column %d: Index %d\n",
1655 col_id
, rate
->index
);
1660 rate
->type
= LQ_NONE
;
1664 static enum rs_action
rs_get_rate_action(struct iwl_mvm
*mvm
,
1665 struct iwl_scale_tbl_info
*tbl
,
1666 s32 sr
, int low
, int high
,
1668 int low_tpt
, int high_tpt
)
1670 enum rs_action action
= RS_ACTION_STAY
;
1672 if ((sr
<= RS_SR_FORCE_DECREASE
) || (current_tpt
== 0)) {
1674 "Decrease rate because of low SR\n");
1675 return RS_ACTION_DOWNSCALE
;
1678 if ((low_tpt
== IWL_INVALID_VALUE
) &&
1679 (high_tpt
== IWL_INVALID_VALUE
) &&
1680 (high
!= IWL_RATE_INVALID
)) {
1682 "No data about high/low rates. Increase rate\n");
1683 return RS_ACTION_UPSCALE
;
1686 if ((high_tpt
== IWL_INVALID_VALUE
) &&
1687 (high
!= IWL_RATE_INVALID
) &&
1688 (low_tpt
!= IWL_INVALID_VALUE
) &&
1689 (low_tpt
< current_tpt
)) {
1691 "No data about high rate and low rate is worse. Increase rate\n");
1692 return RS_ACTION_UPSCALE
;
1695 if ((high_tpt
!= IWL_INVALID_VALUE
) &&
1696 (high_tpt
> current_tpt
)) {
1698 "Higher rate is better. Increate rate\n");
1699 return RS_ACTION_UPSCALE
;
1702 if ((low_tpt
!= IWL_INVALID_VALUE
) &&
1703 (high_tpt
!= IWL_INVALID_VALUE
) &&
1704 (low_tpt
< current_tpt
) &&
1705 (high_tpt
< current_tpt
)) {
1707 "Both high and low are worse. Maintain rate\n");
1708 return RS_ACTION_STAY
;
1711 if ((low_tpt
!= IWL_INVALID_VALUE
) &&
1712 (low_tpt
> current_tpt
)) {
1714 "Lower rate is better\n");
1715 action
= RS_ACTION_DOWNSCALE
;
1719 if ((low_tpt
== IWL_INVALID_VALUE
) &&
1720 (low
!= IWL_RATE_INVALID
)) {
1722 "No data about lower rate\n");
1723 action
= RS_ACTION_DOWNSCALE
;
1727 IWL_DEBUG_RATE(mvm
, "Maintain rate\n");
1730 if ((action
== RS_ACTION_DOWNSCALE
) && (low
!= IWL_RATE_INVALID
)) {
1731 if (sr
>= RS_SR_NO_DECREASE
) {
1733 "SR is above NO DECREASE. Avoid downscale\n");
1734 action
= RS_ACTION_STAY
;
1735 } else if (current_tpt
> (100 * tbl
->expected_tpt
[low
])) {
1737 "Current TPT is higher than max expected in low rate. Avoid downscale\n");
1738 action
= RS_ACTION_STAY
;
1740 IWL_DEBUG_RATE(mvm
, "Decrease rate\n");
1747 static void rs_get_adjacent_txp(struct iwl_mvm
*mvm
, int index
,
1748 int *weaker
, int *stronger
)
1750 *weaker
= index
+ TPC_TX_POWER_STEP
;
1751 if (*weaker
> TPC_MAX_REDUCTION
)
1752 *weaker
= TPC_INVALID
;
1754 *stronger
= index
- TPC_TX_POWER_STEP
;
1756 *stronger
= TPC_INVALID
;
1759 static bool rs_tpc_allowed(struct iwl_mvm
*mvm
, struct ieee80211_vif
*vif
,
1760 struct rs_rate
*rate
, enum ieee80211_band band
)
1762 int index
= rate
->index
;
1763 bool cam
= (iwlmvm_mod_params
.power_scheme
== IWL_POWER_SCHEME_CAM
);
1764 bool sta_ps_disabled
= (vif
->type
== NL80211_IFTYPE_STATION
&&
1767 IWL_DEBUG_RATE(mvm
, "cam: %d sta_ps_disabled %d\n",
1768 cam
, sta_ps_disabled
);
1770 * allow tpc only if power management is enabled, or bt coex
1771 * activity grade allows it and we are on 2.4Ghz.
1773 if ((cam
|| sta_ps_disabled
) &&
1774 !iwl_mvm_bt_coex_is_tpc_allowed(mvm
, band
))
1777 IWL_DEBUG_RATE(mvm
, "check rate, table type: %d\n", rate
->type
);
1778 if (is_legacy(rate
))
1779 return index
== IWL_RATE_54M_INDEX
;
1781 return index
== IWL_RATE_MCS_7_INDEX
;
1783 return index
== IWL_RATE_MCS_7_INDEX
||
1784 index
== IWL_RATE_MCS_8_INDEX
||
1785 index
== IWL_RATE_MCS_9_INDEX
;
1793 TPC_ACTION_DECREASE
,
1794 TPC_ACTION_INCREASE
,
1795 TPC_ACTION_NO_RESTIRCTION
,
1798 static enum tpc_action
rs_get_tpc_action(struct iwl_mvm
*mvm
,
1799 s32 sr
, int weak
, int strong
,
1801 int weak_tpt
, int strong_tpt
)
1803 /* stay until we have valid tpt */
1804 if (current_tpt
== IWL_INVALID_VALUE
) {
1805 IWL_DEBUG_RATE(mvm
, "no current tpt. stay.\n");
1806 return TPC_ACTION_STAY
;
1809 /* Too many failures, increase txp */
1810 if (sr
<= TPC_SR_FORCE_INCREASE
|| current_tpt
== 0) {
1811 IWL_DEBUG_RATE(mvm
, "increase txp because of weak SR\n");
1812 return TPC_ACTION_NO_RESTIRCTION
;
1815 /* try decreasing first if applicable */
1816 if (weak
!= TPC_INVALID
) {
1817 if (weak_tpt
== IWL_INVALID_VALUE
&&
1818 (strong_tpt
== IWL_INVALID_VALUE
||
1819 current_tpt
>= strong_tpt
)) {
1821 "no weak txp measurement. decrease txp\n");
1822 return TPC_ACTION_DECREASE
;
1825 if (weak_tpt
> current_tpt
) {
1827 "lower txp has better tpt. decrease txp\n");
1828 return TPC_ACTION_DECREASE
;
1832 /* next, increase if needed */
1833 if (sr
< TPC_SR_NO_INCREASE
&& strong
!= TPC_INVALID
) {
1834 if (weak_tpt
== IWL_INVALID_VALUE
&&
1835 strong_tpt
!= IWL_INVALID_VALUE
&&
1836 current_tpt
< strong_tpt
) {
1838 "higher txp has better tpt. increase txp\n");
1839 return TPC_ACTION_INCREASE
;
1842 if (weak_tpt
< current_tpt
&&
1843 (strong_tpt
== IWL_INVALID_VALUE
||
1844 strong_tpt
> current_tpt
)) {
1846 "lower txp has worse tpt. increase txp\n");
1847 return TPC_ACTION_INCREASE
;
1851 IWL_DEBUG_RATE(mvm
, "no need to increase or decrease txp - stay\n");
1852 return TPC_ACTION_STAY
;
1855 static bool rs_tpc_perform(struct iwl_mvm
*mvm
,
1856 struct ieee80211_sta
*sta
,
1857 struct iwl_lq_sta
*lq_sta
,
1858 struct iwl_scale_tbl_info
*tbl
)
1860 struct iwl_mvm_sta
*mvm_sta
= (void *)sta
->drv_priv
;
1861 struct ieee80211_vif
*vif
= mvm_sta
->vif
;
1862 struct ieee80211_chanctx_conf
*chanctx_conf
;
1863 enum ieee80211_band band
;
1864 struct iwl_rate_scale_data
*window
;
1865 struct rs_rate
*rate
= &tbl
->rate
;
1866 enum tpc_action action
;
1868 u8 cur
= lq_sta
->lq
.reduced_tpc
;
1871 int weak_tpt
= IWL_INVALID_VALUE
, strong_tpt
= IWL_INVALID_VALUE
;
1873 #ifdef CONFIG_MAC80211_DEBUGFS
1874 if (lq_sta
->pers
.dbg_fixed_txp_reduction
<= TPC_MAX_REDUCTION
) {
1875 IWL_DEBUG_RATE(mvm
, "fixed tpc: %d\n",
1876 lq_sta
->pers
.dbg_fixed_txp_reduction
);
1877 lq_sta
->lq
.reduced_tpc
= lq_sta
->pers
.dbg_fixed_txp_reduction
;
1878 return cur
!= lq_sta
->pers
.dbg_fixed_txp_reduction
;
1883 chanctx_conf
= rcu_dereference(vif
->chanctx_conf
);
1884 if (WARN_ON(!chanctx_conf
))
1885 band
= IEEE80211_NUM_BANDS
;
1887 band
= chanctx_conf
->def
.chan
->band
;
1890 if (!rs_tpc_allowed(mvm
, vif
, rate
, band
)) {
1892 "tpc is not allowed. remove txp restrictions\n");
1893 lq_sta
->lq
.reduced_tpc
= TPC_NO_REDUCTION
;
1894 return cur
!= TPC_NO_REDUCTION
;
1897 rs_get_adjacent_txp(mvm
, cur
, &weak
, &strong
);
1899 /* Collect measured throughputs for current and adjacent rates */
1900 window
= tbl
->tpc_win
;
1901 sr
= window
[cur
].success_ratio
;
1902 current_tpt
= window
[cur
].average_tpt
;
1903 if (weak
!= TPC_INVALID
)
1904 weak_tpt
= window
[weak
].average_tpt
;
1905 if (strong
!= TPC_INVALID
)
1906 strong_tpt
= window
[strong
].average_tpt
;
1909 "(TPC: %d): cur_tpt %d SR %d weak %d strong %d weak_tpt %d strong_tpt %d\n",
1910 cur
, current_tpt
, sr
, weak
, strong
,
1911 weak_tpt
, strong_tpt
);
1913 action
= rs_get_tpc_action(mvm
, sr
, weak
, strong
,
1914 current_tpt
, weak_tpt
, strong_tpt
);
1916 /* override actions if we are on the edge */
1917 if (weak
== TPC_INVALID
&& action
== TPC_ACTION_DECREASE
) {
1918 IWL_DEBUG_RATE(mvm
, "already in lowest txp, stay\n");
1919 action
= TPC_ACTION_STAY
;
1920 } else if (strong
== TPC_INVALID
&&
1921 (action
== TPC_ACTION_INCREASE
||
1922 action
== TPC_ACTION_NO_RESTIRCTION
)) {
1923 IWL_DEBUG_RATE(mvm
, "already in highest txp, stay\n");
1924 action
= TPC_ACTION_STAY
;
1928 case TPC_ACTION_DECREASE
:
1929 lq_sta
->lq
.reduced_tpc
= weak
;
1931 case TPC_ACTION_INCREASE
:
1932 lq_sta
->lq
.reduced_tpc
= strong
;
1934 case TPC_ACTION_NO_RESTIRCTION
:
1935 lq_sta
->lq
.reduced_tpc
= TPC_NO_REDUCTION
;
1937 case TPC_ACTION_STAY
:
1945 * Do rate scaling and search for new modulation mode.
1947 static void rs_rate_scale_perform(struct iwl_mvm
*mvm
,
1948 struct sk_buff
*skb
,
1949 struct ieee80211_sta
*sta
,
1950 struct iwl_lq_sta
*lq_sta
)
1952 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
1953 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*)skb
->data
;
1954 int low
= IWL_RATE_INVALID
;
1955 int high
= IWL_RATE_INVALID
;
1957 struct iwl_rate_scale_data
*window
= NULL
;
1958 int current_tpt
= IWL_INVALID_VALUE
;
1959 int low_tpt
= IWL_INVALID_VALUE
;
1960 int high_tpt
= IWL_INVALID_VALUE
;
1962 enum rs_action scale_action
= RS_ACTION_STAY
;
1965 struct iwl_scale_tbl_info
*tbl
, *tbl1
;
1970 u8 tid
= IWL_MAX_TID_COUNT
;
1971 u8 prev_agg
= lq_sta
->is_agg
;
1972 struct iwl_mvm_sta
*sta_priv
= (void *)sta
->drv_priv
;
1973 struct iwl_mvm_tid_data
*tid_data
;
1974 struct rs_rate
*rate
;
1976 /* Send management frames and NO_ACK data using lowest rate. */
1977 /* TODO: this could probably be improved.. */
1978 if (!ieee80211_is_data(hdr
->frame_control
) ||
1979 info
->flags
& IEEE80211_TX_CTL_NO_ACK
)
1982 tid
= rs_get_tid(lq_sta
, hdr
);
1983 if ((tid
!= IWL_MAX_TID_COUNT
) &&
1984 (lq_sta
->tx_agg_tid_en
& (1 << tid
))) {
1985 tid_data
= &sta_priv
->tid_data
[tid
];
1986 if (tid_data
->state
== IWL_AGG_OFF
)
1995 * Select rate-scale / modulation-mode table to work with in
1996 * the rest of this function: "search" if searching for better
1997 * modulation mode, or "active" if doing rate scaling within a mode.
1999 if (!lq_sta
->search_better_tbl
)
2000 active_tbl
= lq_sta
->active_tbl
;
2002 active_tbl
= 1 - lq_sta
->active_tbl
;
2004 tbl
= &(lq_sta
->lq_info
[active_tbl
]);
2007 if (prev_agg
!= lq_sta
->is_agg
) {
2009 "Aggregation changed: prev %d current %d. Update expected TPT table\n",
2010 prev_agg
, lq_sta
->is_agg
);
2011 rs_set_expected_tpt_table(lq_sta
, tbl
);
2012 rs_rate_scale_clear_tbl_windows(mvm
, tbl
);
2015 /* current tx rate */
2016 index
= lq_sta
->last_txrate_idx
;
2018 /* rates available for this association, and for modulation mode */
2019 rate_mask
= rs_get_supported_rates(lq_sta
, rate
);
2021 if (!(BIT(index
) & rate_mask
)) {
2022 IWL_ERR(mvm
, "Current Rate is not valid\n");
2023 if (lq_sta
->search_better_tbl
) {
2024 /* revert to active table if search table is not valid*/
2025 rate
->type
= LQ_NONE
;
2026 lq_sta
->search_better_tbl
= 0;
2027 tbl
= &(lq_sta
->lq_info
[lq_sta
->active_tbl
]);
2028 rs_update_rate_tbl(mvm
, sta
, lq_sta
, &tbl
->rate
);
2033 /* Get expected throughput table and history window for current rate */
2034 if (!tbl
->expected_tpt
) {
2035 IWL_ERR(mvm
, "tbl->expected_tpt is NULL\n");
2039 /* force user max rate if set by user */
2040 if ((lq_sta
->max_rate_idx
!= -1) &&
2041 (lq_sta
->max_rate_idx
< index
)) {
2042 index
= lq_sta
->max_rate_idx
;
2044 window
= &(tbl
->win
[index
]);
2046 "Forcing user max rate %d\n",
2051 window
= &(tbl
->win
[index
]);
2054 * If there is not enough history to calculate actual average
2055 * throughput, keep analyzing results of more tx frames, without
2056 * changing rate or mode (bypass most of the rest of this function).
2057 * Set up new rate table in uCode only if old rate is not supported
2058 * in current association (use new rate found above).
2060 fail_count
= window
->counter
- window
->success_counter
;
2061 if ((fail_count
< IWL_RATE_MIN_FAILURE_TH
) &&
2062 (window
->success_counter
< IWL_RATE_MIN_SUCCESS_TH
)) {
2064 "(%s: %d): Test Window: succ %d total %d\n",
2065 rs_pretty_lq_type(rate
->type
),
2066 index
, window
->success_counter
, window
->counter
);
2068 /* Can't calculate this yet; not enough history */
2069 window
->average_tpt
= IWL_INVALID_VALUE
;
2071 /* Should we stay with this modulation mode,
2072 * or search for a new one? */
2073 rs_stay_in_table(lq_sta
, false);
2077 /* Else we have enough samples; calculate estimate of
2078 * actual average throughput */
2079 if (window
->average_tpt
!= ((window
->success_ratio
*
2080 tbl
->expected_tpt
[index
] + 64) / 128)) {
2081 window
->average_tpt
= ((window
->success_ratio
*
2082 tbl
->expected_tpt
[index
] + 64) / 128);
2085 /* If we are searching for better modulation mode, check success. */
2086 if (lq_sta
->search_better_tbl
) {
2087 /* If good success, continue using the "search" mode;
2088 * no need to send new link quality command, since we're
2089 * continuing to use the setup that we've been trying. */
2090 if (window
->average_tpt
> lq_sta
->last_tpt
) {
2092 "SWITCHING TO NEW TABLE SR: %d "
2093 "cur-tpt %d old-tpt %d\n",
2094 window
->success_ratio
,
2095 window
->average_tpt
,
2098 /* Swap tables; "search" becomes "active" */
2099 lq_sta
->active_tbl
= active_tbl
;
2100 current_tpt
= window
->average_tpt
;
2101 /* Else poor success; go back to mode in "active" table */
2104 "GOING BACK TO THE OLD TABLE: SR %d "
2105 "cur-tpt %d old-tpt %d\n",
2106 window
->success_ratio
,
2107 window
->average_tpt
,
2110 /* Nullify "search" table */
2111 rate
->type
= LQ_NONE
;
2113 /* Revert to "active" table */
2114 active_tbl
= lq_sta
->active_tbl
;
2115 tbl
= &(lq_sta
->lq_info
[active_tbl
]);
2117 /* Revert to "active" rate and throughput info */
2118 index
= tbl
->rate
.index
;
2119 current_tpt
= lq_sta
->last_tpt
;
2121 /* Need to set up a new rate table in uCode */
2125 /* Either way, we've made a decision; modulation mode
2126 * search is done, allow rate adjustment next time. */
2127 lq_sta
->search_better_tbl
= 0;
2128 done_search
= 1; /* Don't switch modes below! */
2132 /* (Else) not in search of better modulation mode, try for better
2133 * starting rate, while staying in this mode. */
2134 high_low
= rs_get_adjacent_rate(mvm
, index
, rate_mask
, rate
->type
);
2135 low
= high_low
& 0xff;
2136 high
= (high_low
>> 8) & 0xff;
2138 /* If user set max rate, dont allow higher than user constrain */
2139 if ((lq_sta
->max_rate_idx
!= -1) &&
2140 (lq_sta
->max_rate_idx
< high
))
2141 high
= IWL_RATE_INVALID
;
2143 sr
= window
->success_ratio
;
2145 /* Collect measured throughputs for current and adjacent rates */
2146 current_tpt
= window
->average_tpt
;
2147 if (low
!= IWL_RATE_INVALID
)
2148 low_tpt
= tbl
->win
[low
].average_tpt
;
2149 if (high
!= IWL_RATE_INVALID
)
2150 high_tpt
= tbl
->win
[high
].average_tpt
;
2153 "(%s: %d): cur_tpt %d SR %d low %d high %d low_tpt %d high_tpt %d\n",
2154 rs_pretty_lq_type(rate
->type
), index
, current_tpt
, sr
,
2155 low
, high
, low_tpt
, high_tpt
);
2157 scale_action
= rs_get_rate_action(mvm
, tbl
, sr
, low
, high
,
2158 current_tpt
, low_tpt
, high_tpt
);
2160 /* Force a search in case BT doesn't like us being in MIMO */
2161 if (is_mimo(rate
) &&
2162 !iwl_mvm_bt_coex_is_mimo_allowed(mvm
, sta
)) {
2164 "BT Coex forbids MIMO. Search for new config\n");
2165 rs_stay_in_table(lq_sta
, true);
2169 switch (scale_action
) {
2170 case RS_ACTION_DOWNSCALE
:
2171 /* Decrease starting rate, update uCode's rate table */
2172 if (low
!= IWL_RATE_INVALID
) {
2177 "At the bottom rate. Can't decrease\n");
2181 case RS_ACTION_UPSCALE
:
2182 /* Increase starting rate, update uCode's rate table */
2183 if (high
!= IWL_RATE_INVALID
) {
2188 "At the top rate. Can't increase\n");
2192 case RS_ACTION_STAY
:
2194 if (lq_sta
->rs_state
== RS_STATE_STAY_IN_COLUMN
)
2195 update_lq
= rs_tpc_perform(mvm
, sta
, lq_sta
, tbl
);
2202 /* Replace uCode's rate table for the destination station. */
2204 tbl
->rate
.index
= index
;
2205 rs_update_rate_tbl(mvm
, sta
, lq_sta
, &tbl
->rate
);
2208 rs_stay_in_table(lq_sta
, false);
2211 * Search for new modulation mode if we're:
2212 * 1) Not changing rates right now
2213 * 2) Not just finishing up a search
2214 * 3) Allowing a new search
2216 if (!update_lq
&& !done_search
&&
2217 lq_sta
->rs_state
== RS_STATE_SEARCH_CYCLE_STARTED
2218 && window
->counter
) {
2219 enum rs_column next_column
;
2221 /* Save current throughput to compare with "search" throughput*/
2222 lq_sta
->last_tpt
= current_tpt
;
2225 "Start Search: update_lq %d done_search %d rs_state %d win->counter %d\n",
2226 update_lq
, done_search
, lq_sta
->rs_state
,
2229 next_column
= rs_get_next_column(mvm
, lq_sta
, sta
, tbl
);
2230 if (next_column
!= RS_COLUMN_INVALID
) {
2231 int ret
= rs_switch_to_column(mvm
, lq_sta
, sta
,
2234 lq_sta
->search_better_tbl
= 1;
2237 "No more columns to explore in search cycle. Go to RS_STATE_SEARCH_CYCLE_ENDED\n");
2238 lq_sta
->rs_state
= RS_STATE_SEARCH_CYCLE_ENDED
;
2241 /* If new "search" mode was selected, set up in uCode table */
2242 if (lq_sta
->search_better_tbl
) {
2243 /* Access the "search" table, clear its history. */
2244 tbl
= &(lq_sta
->lq_info
[(1 - lq_sta
->active_tbl
)]);
2245 rs_rate_scale_clear_tbl_windows(mvm
, tbl
);
2247 /* Use new "search" start rate */
2248 index
= tbl
->rate
.index
;
2250 rs_dump_rate(mvm
, &tbl
->rate
,
2251 "Switch to SEARCH TABLE:");
2252 rs_fill_lq_cmd(mvm
, sta
, lq_sta
, &tbl
->rate
);
2253 iwl_mvm_send_lq_cmd(mvm
, &lq_sta
->lq
, false);
2259 if (done_search
&& lq_sta
->rs_state
== RS_STATE_SEARCH_CYCLE_ENDED
) {
2260 /* If the "active" (non-search) mode was legacy,
2261 * and we've tried switching antennas,
2262 * but we haven't been able to try HT modes (not available),
2263 * stay with best antenna legacy modulation for a while
2264 * before next round of mode comparisons. */
2265 tbl1
= &(lq_sta
->lq_info
[lq_sta
->active_tbl
]);
2266 if (is_legacy(&tbl1
->rate
)) {
2267 IWL_DEBUG_RATE(mvm
, "LQ: STAY in legacy table\n");
2269 if (tid
!= IWL_MAX_TID_COUNT
) {
2270 tid_data
= &sta_priv
->tid_data
[tid
];
2271 if (tid_data
->state
!= IWL_AGG_OFF
) {
2273 "Stop aggregation on tid %d\n",
2275 ieee80211_stop_tx_ba_session(sta
, tid
);
2278 rs_set_stay_in_table(mvm
, 1, lq_sta
);
2280 /* If we're in an HT mode, and all 3 mode switch actions
2281 * have been tried and compared, stay in this best modulation
2282 * mode for a while before next round of mode comparisons. */
2283 if ((lq_sta
->last_tpt
> IWL_AGG_TPT_THREHOLD
) &&
2284 (lq_sta
->tx_agg_tid_en
& (1 << tid
)) &&
2285 (tid
!= IWL_MAX_TID_COUNT
)) {
2286 tid_data
= &sta_priv
->tid_data
[tid
];
2287 if (tid_data
->state
== IWL_AGG_OFF
) {
2289 "try to aggregate tid %d\n",
2291 rs_tl_turn_on_agg(mvm
, tid
,
2295 rs_set_stay_in_table(mvm
, 0, lq_sta
);
2300 lq_sta
->last_txrate_idx
= index
;
2304 * rs_initialize_lq - Initialize a station's hardware rate table
2306 * The uCode's station table contains a table of fallback rates
2307 * for automatic fallback during transmission.
2309 * NOTE: This sets up a default set of values. These will be replaced later
2310 * if the driver's iwl-agn-rs rate scaling algorithm is used, instead of
2313 * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
2314 * calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
2315 * which requires station table entry to exist).
2317 static void rs_initialize_lq(struct iwl_mvm
*mvm
,
2318 struct ieee80211_sta
*sta
,
2319 struct iwl_lq_sta
*lq_sta
,
2320 enum ieee80211_band band
,
2323 struct iwl_scale_tbl_info
*tbl
;
2324 struct rs_rate
*rate
;
2329 if (!sta
|| !lq_sta
)
2332 i
= lq_sta
->last_txrate_idx
;
2334 valid_tx_ant
= mvm
->fw
->valid_tx_ant
;
2336 if (!lq_sta
->search_better_tbl
)
2337 active_tbl
= lq_sta
->active_tbl
;
2339 active_tbl
= 1 - lq_sta
->active_tbl
;
2341 tbl
= &(lq_sta
->lq_info
[active_tbl
]);
2344 if ((i
< 0) || (i
>= IWL_RATE_COUNT
))
2348 rate
->ant
= first_antenna(valid_tx_ant
);
2351 rate
->bw
= RATE_MCS_CHAN_WIDTH_20
;
2352 if (band
== IEEE80211_BAND_5GHZ
)
2353 rate
->type
= LQ_LEGACY_A
;
2355 rate
->type
= LQ_LEGACY_G
;
2357 WARN_ON_ONCE(rate
->ant
!= ANT_A
&& rate
->ant
!= ANT_B
);
2358 if (rate
->ant
== ANT_A
)
2359 tbl
->column
= RS_COLUMN_LEGACY_ANT_A
;
2361 tbl
->column
= RS_COLUMN_LEGACY_ANT_B
;
2363 rs_set_expected_tpt_table(lq_sta
, tbl
);
2364 rs_fill_lq_cmd(mvm
, sta
, lq_sta
, rate
);
2365 /* TODO restore station should remember the lq cmd */
2366 iwl_mvm_send_lq_cmd(mvm
, &lq_sta
->lq
, init
);
2369 static void rs_get_rate(void *mvm_r
, struct ieee80211_sta
*sta
, void *mvm_sta
,
2370 struct ieee80211_tx_rate_control
*txrc
)
2372 struct sk_buff
*skb
= txrc
->skb
;
2373 struct ieee80211_supported_band
*sband
= txrc
->sband
;
2374 struct iwl_op_mode
*op_mode __maybe_unused
=
2375 (struct iwl_op_mode
*)mvm_r
;
2376 struct iwl_mvm
*mvm __maybe_unused
= IWL_OP_MODE_GET_MVM(op_mode
);
2377 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
2378 struct iwl_lq_sta
*lq_sta
= mvm_sta
;
2380 /* Get max rate if user set max rate */
2382 lq_sta
->max_rate_idx
= txrc
->max_rate_idx
;
2383 if ((sband
->band
== IEEE80211_BAND_5GHZ
) &&
2384 (lq_sta
->max_rate_idx
!= -1))
2385 lq_sta
->max_rate_idx
+= IWL_FIRST_OFDM_RATE
;
2386 if ((lq_sta
->max_rate_idx
< 0) ||
2387 (lq_sta
->max_rate_idx
>= IWL_RATE_COUNT
))
2388 lq_sta
->max_rate_idx
= -1;
2391 /* Treat uninitialized rate scaling data same as non-existing. */
2392 if (lq_sta
&& !lq_sta
->pers
.drv
) {
2393 IWL_DEBUG_RATE(mvm
, "Rate scaling not initialized yet.\n");
2397 /* Send management frames and NO_ACK data using lowest rate. */
2398 if (rate_control_send_low(sta
, mvm_sta
, txrc
))
2401 iwl_mvm_hwrate_to_tx_rate(lq_sta
->last_rate_n_flags
,
2402 info
->band
, &info
->control
.rates
[0]);
2404 info
->control
.rates
[0].count
= 1;
2407 static void *rs_alloc_sta(void *mvm_rate
, struct ieee80211_sta
*sta
,
2410 struct iwl_mvm_sta
*sta_priv
= (struct iwl_mvm_sta
*)sta
->drv_priv
;
2411 struct iwl_op_mode
*op_mode
= (struct iwl_op_mode
*)mvm_rate
;
2412 struct iwl_mvm
*mvm
= IWL_OP_MODE_GET_MVM(op_mode
);
2413 struct iwl_lq_sta
*lq_sta
= &sta_priv
->lq_sta
;
2415 IWL_DEBUG_RATE(mvm
, "create station rate scale window\n");
2417 lq_sta
->pers
.drv
= mvm
;
2418 #ifdef CONFIG_MAC80211_DEBUGFS
2419 lq_sta
->pers
.dbg_fixed_rate
= 0;
2420 lq_sta
->pers
.dbg_fixed_txp_reduction
= TPC_INVALID
;
2423 return &sta_priv
->lq_sta
;
2426 static int rs_vht_highest_rx_mcs_index(struct ieee80211_sta_vht_cap
*vht_cap
,
2429 u16 rx_mcs
= le16_to_cpu(vht_cap
->vht_mcs
.rx_mcs_map
) &
2430 (0x3 << (2 * (nss
- 1)));
2431 rx_mcs
>>= (2 * (nss
- 1));
2433 if (rx_mcs
== IEEE80211_VHT_MCS_SUPPORT_0_7
)
2434 return IWL_RATE_MCS_7_INDEX
;
2435 else if (rx_mcs
== IEEE80211_VHT_MCS_SUPPORT_0_8
)
2436 return IWL_RATE_MCS_8_INDEX
;
2437 else if (rx_mcs
== IEEE80211_VHT_MCS_SUPPORT_0_9
)
2438 return IWL_RATE_MCS_9_INDEX
;
2440 WARN_ON_ONCE(rx_mcs
!= IEEE80211_VHT_MCS_NOT_SUPPORTED
);
2444 static void rs_vht_set_enabled_rates(struct ieee80211_sta
*sta
,
2445 struct ieee80211_sta_vht_cap
*vht_cap
,
2446 struct iwl_lq_sta
*lq_sta
)
2449 int highest_mcs
= rs_vht_highest_rx_mcs_index(vht_cap
, 1);
2451 if (highest_mcs
>= IWL_RATE_MCS_0_INDEX
) {
2452 for (i
= IWL_RATE_MCS_0_INDEX
; i
<= highest_mcs
; i
++) {
2453 if (i
== IWL_RATE_9M_INDEX
)
2456 /* VHT MCS9 isn't valid for 20Mhz for NSS=1,2 */
2457 if (i
== IWL_RATE_MCS_9_INDEX
&&
2458 sta
->bandwidth
== IEEE80211_STA_RX_BW_20
)
2461 lq_sta
->active_siso_rate
|= BIT(i
);
2465 if (sta
->rx_nss
< 2)
2468 highest_mcs
= rs_vht_highest_rx_mcs_index(vht_cap
, 2);
2469 if (highest_mcs
>= IWL_RATE_MCS_0_INDEX
) {
2470 for (i
= IWL_RATE_MCS_0_INDEX
; i
<= highest_mcs
; i
++) {
2471 if (i
== IWL_RATE_9M_INDEX
)
2474 /* VHT MCS9 isn't valid for 20Mhz for NSS=1,2 */
2475 if (i
== IWL_RATE_MCS_9_INDEX
&&
2476 sta
->bandwidth
== IEEE80211_STA_RX_BW_20
)
2479 lq_sta
->active_mimo2_rate
|= BIT(i
);
2484 #ifdef CONFIG_IWLWIFI_DEBUGFS
2485 static void iwl_mvm_reset_frame_stats(struct iwl_mvm
*mvm
,
2486 struct iwl_mvm_frame_stats
*stats
)
2488 spin_lock_bh(&mvm
->drv_stats_lock
);
2489 memset(stats
, 0, sizeof(*stats
));
2490 spin_unlock_bh(&mvm
->drv_stats_lock
);
2493 void iwl_mvm_update_frame_stats(struct iwl_mvm
*mvm
,
2494 struct iwl_mvm_frame_stats
*stats
,
2497 u8 nss
= 0, mcs
= 0;
2499 spin_lock(&mvm
->drv_stats_lock
);
2502 stats
->agg_frames
++;
2504 stats
->success_frames
++;
2506 switch (rate
& RATE_MCS_CHAN_WIDTH_MSK
) {
2507 case RATE_MCS_CHAN_WIDTH_20
:
2508 stats
->bw_20_frames
++;
2510 case RATE_MCS_CHAN_WIDTH_40
:
2511 stats
->bw_40_frames
++;
2513 case RATE_MCS_CHAN_WIDTH_80
:
2514 stats
->bw_80_frames
++;
2517 WARN_ONCE(1, "bad BW. rate 0x%x", rate
);
2520 if (rate
& RATE_MCS_HT_MSK
) {
2522 mcs
= rate
& RATE_HT_MCS_RATE_CODE_MSK
;
2523 nss
= ((rate
& RATE_HT_MCS_NSS_MSK
) >> RATE_HT_MCS_NSS_POS
) + 1;
2524 } else if (rate
& RATE_MCS_VHT_MSK
) {
2525 stats
->vht_frames
++;
2526 mcs
= rate
& RATE_VHT_MCS_RATE_CODE_MSK
;
2527 nss
= ((rate
& RATE_VHT_MCS_NSS_MSK
) >>
2528 RATE_VHT_MCS_NSS_POS
) + 1;
2530 stats
->legacy_frames
++;
2534 stats
->siso_frames
++;
2536 stats
->mimo2_frames
++;
2538 if (rate
& RATE_MCS_SGI_MSK
)
2539 stats
->sgi_frames
++;
2541 stats
->ngi_frames
++;
2543 stats
->last_rates
[stats
->last_frame_idx
] = rate
;
2544 stats
->last_frame_idx
= (stats
->last_frame_idx
+ 1) %
2545 ARRAY_SIZE(stats
->last_rates
);
2547 spin_unlock(&mvm
->drv_stats_lock
);
2552 * Called after adding a new station to initialize rate scaling
2554 void iwl_mvm_rs_rate_init(struct iwl_mvm
*mvm
, struct ieee80211_sta
*sta
,
2555 enum ieee80211_band band
, bool init
)
2558 struct ieee80211_hw
*hw
= mvm
->hw
;
2559 struct ieee80211_sta_ht_cap
*ht_cap
= &sta
->ht_cap
;
2560 struct ieee80211_sta_vht_cap
*vht_cap
= &sta
->vht_cap
;
2561 struct iwl_mvm_sta
*sta_priv
;
2562 struct iwl_lq_sta
*lq_sta
;
2563 struct ieee80211_supported_band
*sband
;
2564 unsigned long supp
; /* must be unsigned long for for_each_set_bit */
2566 sta_priv
= (struct iwl_mvm_sta
*)sta
->drv_priv
;
2567 lq_sta
= &sta_priv
->lq_sta
;
2569 /* clear all non-persistent lq data */
2570 memset(lq_sta
, 0, offsetof(typeof(*lq_sta
), pers
));
2572 sband
= hw
->wiphy
->bands
[band
];
2574 lq_sta
->lq
.sta_id
= sta_priv
->sta_id
;
2576 for (j
= 0; j
< LQ_SIZE
; j
++)
2577 rs_rate_scale_clear_tbl_windows(mvm
, &lq_sta
->lq_info
[j
]);
2579 lq_sta
->flush_timer
= 0;
2580 lq_sta
->last_tx
= jiffies
;
2583 "LQ: *** rate scale station global init for station %d ***\n",
2585 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2586 * the lowest or the highest rate.. Could consider using RSSI from
2587 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2590 lq_sta
->max_rate_idx
= -1;
2591 lq_sta
->missed_rate_counter
= IWL_MISSED_RATE_MAX
;
2592 lq_sta
->band
= sband
->band
;
2594 * active legacy rates as per supported rates bitmap
2596 supp
= sta
->supp_rates
[sband
->band
];
2597 lq_sta
->active_legacy_rate
= 0;
2598 for_each_set_bit(i
, &supp
, BITS_PER_LONG
)
2599 lq_sta
->active_legacy_rate
|= BIT(sband
->bitrates
[i
].hw_value
);
2601 /* TODO: should probably account for rx_highest for both HT/VHT */
2602 if (!vht_cap
|| !vht_cap
->vht_supported
) {
2603 /* active_siso_rate mask includes 9 MBits (bit 5),
2604 * and CCK (bits 0-3), supp_rates[] does not;
2605 * shift to convert format, force 9 MBits off.
2607 lq_sta
->active_siso_rate
= ht_cap
->mcs
.rx_mask
[0] << 1;
2608 lq_sta
->active_siso_rate
|= ht_cap
->mcs
.rx_mask
[0] & 0x1;
2609 lq_sta
->active_siso_rate
&= ~((u16
)0x2);
2610 lq_sta
->active_siso_rate
<<= IWL_FIRST_OFDM_RATE
;
2613 lq_sta
->active_mimo2_rate
= ht_cap
->mcs
.rx_mask
[1] << 1;
2614 lq_sta
->active_mimo2_rate
|= ht_cap
->mcs
.rx_mask
[1] & 0x1;
2615 lq_sta
->active_mimo2_rate
&= ~((u16
)0x2);
2616 lq_sta
->active_mimo2_rate
<<= IWL_FIRST_OFDM_RATE
;
2618 lq_sta
->is_vht
= false;
2619 if (mvm
->cfg
->ht_params
->ldpc
&&
2620 (ht_cap
->cap
& IEEE80211_HT_CAP_LDPC_CODING
))
2621 lq_sta
->ldpc
= true;
2623 rs_vht_set_enabled_rates(sta
, vht_cap
, lq_sta
);
2624 lq_sta
->is_vht
= true;
2626 if (mvm
->cfg
->ht_params
->ldpc
&&
2627 (vht_cap
->cap
& IEEE80211_VHT_CAP_RXLDPC
))
2628 lq_sta
->ldpc
= true;
2631 lq_sta
->max_legacy_rate_idx
= find_last_bit(&lq_sta
->active_legacy_rate
,
2633 lq_sta
->max_siso_rate_idx
= find_last_bit(&lq_sta
->active_siso_rate
,
2635 lq_sta
->max_mimo2_rate_idx
= find_last_bit(&lq_sta
->active_mimo2_rate
,
2639 "RATE MASK: LEGACY=%lX SISO=%lX MIMO2=%lX VHT=%d LDPC=%d\n",
2640 lq_sta
->active_legacy_rate
,
2641 lq_sta
->active_siso_rate
,
2642 lq_sta
->active_mimo2_rate
,
2643 lq_sta
->is_vht
, lq_sta
->ldpc
);
2644 IWL_DEBUG_RATE(mvm
, "MAX RATE: LEGACY=%d SISO=%d MIMO2=%d\n",
2645 lq_sta
->max_legacy_rate_idx
,
2646 lq_sta
->max_siso_rate_idx
,
2647 lq_sta
->max_mimo2_rate_idx
);
2649 /* These values will be overridden later */
2650 lq_sta
->lq
.single_stream_ant_msk
=
2651 first_antenna(mvm
->fw
->valid_tx_ant
);
2652 lq_sta
->lq
.dual_stream_ant_msk
= ANT_AB
;
2654 /* as default allow aggregation for all tids */
2655 lq_sta
->tx_agg_tid_en
= IWL_AGG_ALL_TID
;
2657 /* Set last_txrate_idx to lowest rate */
2658 lq_sta
->last_txrate_idx
= rate_lowest_index(sband
, sta
);
2659 if (sband
->band
== IEEE80211_BAND_5GHZ
)
2660 lq_sta
->last_txrate_idx
+= IWL_FIRST_OFDM_RATE
;
2662 #ifdef CONFIG_IWLWIFI_DEBUGFS
2663 iwl_mvm_reset_frame_stats(mvm
, &mvm
->drv_rx_stats
);
2665 rs_initialize_lq(mvm
, sta
, lq_sta
, band
, init
);
2668 static void rs_rate_update(void *mvm_r
,
2669 struct ieee80211_supported_band
*sband
,
2670 struct cfg80211_chan_def
*chandef
,
2671 struct ieee80211_sta
*sta
, void *priv_sta
,
2675 struct iwl_op_mode
*op_mode
=
2676 (struct iwl_op_mode
*)mvm_r
;
2677 struct iwl_mvm
*mvm
= IWL_OP_MODE_GET_MVM(op_mode
);
2679 /* Stop any ongoing aggregations as rs starts off assuming no agg */
2680 for (tid
= 0; tid
< IWL_MAX_TID_COUNT
; tid
++)
2681 ieee80211_stop_tx_ba_session(sta
, tid
);
2683 iwl_mvm_rs_rate_init(mvm
, sta
, sband
->band
, false);
2686 #ifdef CONFIG_MAC80211_DEBUGFS
2687 static void rs_build_rates_table_from_fixed(struct iwl_mvm
*mvm
,
2688 struct iwl_lq_cmd
*lq_cmd
,
2689 enum ieee80211_band band
,
2692 struct rs_rate rate
;
2694 int num_rates
= ARRAY_SIZE(lq_cmd
->rs_table
);
2695 __le32 ucode_rate_le32
= cpu_to_le32(ucode_rate
);
2696 u8 ant
= (ucode_rate
& RATE_MCS_ANT_ABC_MSK
) >> RATE_MCS_ANT_POS
;
2698 for (i
= 0; i
< num_rates
; i
++)
2699 lq_cmd
->rs_table
[i
] = ucode_rate_le32
;
2701 rs_rate_from_ucode_rate(ucode_rate
, band
, &rate
);
2704 lq_cmd
->mimo_delim
= num_rates
- 1;
2706 lq_cmd
->mimo_delim
= 0;
2708 lq_cmd
->reduced_tpc
= 0;
2710 if (num_of_ant(ant
) == 1)
2711 lq_cmd
->single_stream_ant_msk
= ant
;
2713 lq_cmd
->agg_frame_cnt_limit
= LINK_QUAL_AGG_FRAME_LIMIT_DEF
;
2715 #endif /* CONFIG_MAC80211_DEBUGFS */
2717 static void rs_fill_rates_for_column(struct iwl_mvm
*mvm
,
2718 struct iwl_lq_sta
*lq_sta
,
2719 struct rs_rate
*rate
,
2720 __le32
*rs_table
, int *rs_table_index
,
2721 int num_rates
, int num_retries
,
2722 u8 valid_tx_ant
, bool toggle_ant
)
2726 bool bottom_reached
= false;
2727 int prev_rate_idx
= rate
->index
;
2728 int end
= LINK_QUAL_MAX_RETRY_NUM
;
2729 int index
= *rs_table_index
;
2731 for (i
= 0; i
< num_rates
&& index
< end
; i
++) {
2732 ucode_rate
= cpu_to_le32(ucode_rate_from_rs_rate(mvm
, rate
));
2733 for (j
= 0; j
< num_retries
&& index
< end
; j
++, index
++)
2734 rs_table
[index
] = ucode_rate
;
2737 rs_toggle_antenna(valid_tx_ant
, rate
);
2739 prev_rate_idx
= rate
->index
;
2740 bottom_reached
= rs_get_lower_rate_in_column(lq_sta
, rate
);
2741 if (bottom_reached
&& !is_legacy(rate
))
2745 if (!bottom_reached
)
2746 rate
->index
= prev_rate_idx
;
2748 *rs_table_index
= index
;
2751 /* Building the rate table is non trivial. When we're in MIMO2/VHT/80Mhz/SGI
2752 * column the rate table should look like this:
2754 * rate[0] 0x400D019 VHT | ANT: AB BW: 80Mhz MCS: 9 NSS: 2 SGI
2755 * rate[1] 0x400D019 VHT | ANT: AB BW: 80Mhz MCS: 9 NSS: 2 SGI
2756 * rate[2] 0x400D018 VHT | ANT: AB BW: 80Mhz MCS: 8 NSS: 2 SGI
2757 * rate[3] 0x400D018 VHT | ANT: AB BW: 80Mhz MCS: 8 NSS: 2 SGI
2758 * rate[4] 0x400D017 VHT | ANT: AB BW: 80Mhz MCS: 7 NSS: 2 SGI
2759 * rate[5] 0x400D017 VHT | ANT: AB BW: 80Mhz MCS: 7 NSS: 2 SGI
2760 * rate[6] 0x4005007 VHT | ANT: A BW: 80Mhz MCS: 7 NSS: 1 NGI
2761 * rate[7] 0x4009006 VHT | ANT: B BW: 80Mhz MCS: 6 NSS: 1 NGI
2762 * rate[8] 0x4005005 VHT | ANT: A BW: 80Mhz MCS: 5 NSS: 1 NGI
2763 * rate[9] 0x800B Legacy | ANT: B Rate: 36 Mbps
2764 * rate[10] 0x4009 Legacy | ANT: A Rate: 24 Mbps
2765 * rate[11] 0x8007 Legacy | ANT: B Rate: 18 Mbps
2766 * rate[12] 0x4005 Legacy | ANT: A Rate: 12 Mbps
2767 * rate[13] 0x800F Legacy | ANT: B Rate: 9 Mbps
2768 * rate[14] 0x400D Legacy | ANT: A Rate: 6 Mbps
2769 * rate[15] 0x800D Legacy | ANT: B Rate: 6 Mbps
2771 static void rs_build_rates_table(struct iwl_mvm
*mvm
,
2772 struct iwl_lq_sta
*lq_sta
,
2773 const struct rs_rate
*initial_rate
)
2775 struct rs_rate rate
;
2776 int num_rates
, num_retries
, index
= 0;
2777 u8 valid_tx_ant
= 0;
2778 struct iwl_lq_cmd
*lq_cmd
= &lq_sta
->lq
;
2779 bool toggle_ant
= false;
2781 memcpy(&rate
, initial_rate
, sizeof(rate
));
2783 valid_tx_ant
= mvm
->fw
->valid_tx_ant
;
2785 if (is_siso(&rate
)) {
2786 num_rates
= RS_INITIAL_SISO_NUM_RATES
;
2787 num_retries
= RS_HT_VHT_RETRIES_PER_RATE
;
2788 } else if (is_mimo(&rate
)) {
2789 num_rates
= RS_INITIAL_MIMO_NUM_RATES
;
2790 num_retries
= RS_HT_VHT_RETRIES_PER_RATE
;
2792 num_rates
= RS_INITIAL_LEGACY_NUM_RATES
;
2793 num_retries
= RS_LEGACY_RETRIES_PER_RATE
;
2797 rs_fill_rates_for_column(mvm
, lq_sta
, &rate
, lq_cmd
->rs_table
, &index
,
2798 num_rates
, num_retries
, valid_tx_ant
,
2801 rs_get_lower_rate_down_column(lq_sta
, &rate
);
2803 if (is_siso(&rate
)) {
2804 num_rates
= RS_SECONDARY_SISO_NUM_RATES
;
2805 num_retries
= RS_SECONDARY_SISO_RETRIES
;
2806 lq_cmd
->mimo_delim
= index
;
2807 } else if (is_legacy(&rate
)) {
2808 num_rates
= RS_SECONDARY_LEGACY_NUM_RATES
;
2809 num_retries
= RS_LEGACY_RETRIES_PER_RATE
;
2816 rs_fill_rates_for_column(mvm
, lq_sta
, &rate
, lq_cmd
->rs_table
, &index
,
2817 num_rates
, num_retries
, valid_tx_ant
,
2820 rs_get_lower_rate_down_column(lq_sta
, &rate
);
2822 num_rates
= RS_SECONDARY_LEGACY_NUM_RATES
;
2823 num_retries
= RS_LEGACY_RETRIES_PER_RATE
;
2825 rs_fill_rates_for_column(mvm
, lq_sta
, &rate
, lq_cmd
->rs_table
, &index
,
2826 num_rates
, num_retries
, valid_tx_ant
,
2831 static void rs_fill_lq_cmd(struct iwl_mvm
*mvm
,
2832 struct ieee80211_sta
*sta
,
2833 struct iwl_lq_sta
*lq_sta
,
2834 const struct rs_rate
*initial_rate
)
2836 struct iwl_lq_cmd
*lq_cmd
= &lq_sta
->lq
;
2837 struct iwl_mvm_sta
*mvmsta
;
2838 struct iwl_mvm_vif
*mvmvif
;
2840 lq_cmd
->agg_disable_start_th
= LINK_QUAL_AGG_DISABLE_START_DEF
;
2841 lq_cmd
->agg_time_limit
=
2842 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF
);
2844 #ifdef CONFIG_MAC80211_DEBUGFS
2845 if (lq_sta
->pers
.dbg_fixed_rate
) {
2846 rs_build_rates_table_from_fixed(mvm
, lq_cmd
,
2848 lq_sta
->pers
.dbg_fixed_rate
);
2852 if (WARN_ON_ONCE(!sta
|| !initial_rate
))
2855 rs_build_rates_table(mvm
, lq_sta
, initial_rate
);
2857 if (num_of_ant(initial_rate
->ant
) == 1)
2858 lq_cmd
->single_stream_ant_msk
= initial_rate
->ant
;
2860 mvmsta
= iwl_mvm_sta_from_mac80211(sta
);
2861 mvmvif
= iwl_mvm_vif_from_mac80211(mvmsta
->vif
);
2863 if (num_of_ant(initial_rate
->ant
) == 1)
2864 lq_cmd
->single_stream_ant_msk
= initial_rate
->ant
;
2866 lq_cmd
->agg_frame_cnt_limit
= mvmsta
->max_agg_bufsize
;
2869 * In case of low latency, tell the firwmare to leave a frame in the
2870 * Tx Fifo so that it can start a transaction in the same TxOP. This
2871 * basically allows the firmware to send bursts.
2873 if (iwl_mvm_vif_low_latency(mvmvif
)) {
2874 lq_cmd
->agg_frame_cnt_limit
--;
2876 if (mvm
->low_latency_agg_frame_limit
)
2877 lq_cmd
->agg_frame_cnt_limit
=
2878 min(lq_cmd
->agg_frame_cnt_limit
,
2879 mvm
->low_latency_agg_frame_limit
);
2882 if (mvmsta
->vif
->p2p
)
2883 lq_cmd
->flags
|= LQ_FLAG_USE_RTS_MSK
;
2885 lq_cmd
->agg_time_limit
=
2886 cpu_to_le16(iwl_mvm_coex_agg_time_limit(mvm
, sta
));
2889 static void *rs_alloc(struct ieee80211_hw
*hw
, struct dentry
*debugfsdir
)
2893 /* rate scale requires free function to be implemented */
2894 static void rs_free(void *mvm_rate
)
2899 static void rs_free_sta(void *mvm_r
, struct ieee80211_sta
*sta
,
2902 struct iwl_op_mode
*op_mode __maybe_unused
= mvm_r
;
2903 struct iwl_mvm
*mvm __maybe_unused
= IWL_OP_MODE_GET_MVM(op_mode
);
2905 IWL_DEBUG_RATE(mvm
, "enter\n");
2906 IWL_DEBUG_RATE(mvm
, "leave\n");
2909 #ifdef CONFIG_MAC80211_DEBUGFS
2910 int rs_pretty_print_rate(char *buf
, const u32 rate
)
2914 u8 mcs
= 0, nss
= 0;
2915 u8 ant
= (rate
& RATE_MCS_ANT_ABC_MSK
) >> RATE_MCS_ANT_POS
;
2917 if (!(rate
& RATE_MCS_HT_MSK
) &&
2918 !(rate
& RATE_MCS_VHT_MSK
)) {
2919 int index
= iwl_hwrate_to_plcp_idx(rate
);
2921 return sprintf(buf
, "Legacy | ANT: %s Rate: %s Mbps\n",
2923 index
== IWL_RATE_INVALID
? "BAD" :
2924 iwl_rate_mcs
[index
].mbps
);
2927 if (rate
& RATE_MCS_VHT_MSK
) {
2929 mcs
= rate
& RATE_VHT_MCS_RATE_CODE_MSK
;
2930 nss
= ((rate
& RATE_VHT_MCS_NSS_MSK
)
2931 >> RATE_VHT_MCS_NSS_POS
) + 1;
2932 } else if (rate
& RATE_MCS_HT_MSK
) {
2934 mcs
= rate
& RATE_HT_MCS_INDEX_MSK
;
2936 type
= "Unknown"; /* shouldn't happen */
2939 switch (rate
& RATE_MCS_CHAN_WIDTH_MSK
) {
2940 case RATE_MCS_CHAN_WIDTH_20
:
2943 case RATE_MCS_CHAN_WIDTH_40
:
2946 case RATE_MCS_CHAN_WIDTH_80
:
2949 case RATE_MCS_CHAN_WIDTH_160
:
2956 return sprintf(buf
, "%s | ANT: %s BW: %s MCS: %d NSS: %d %s%s%s%s%s\n",
2957 type
, rs_pretty_ant(ant
), bw
, mcs
, nss
,
2958 (rate
& RATE_MCS_SGI_MSK
) ? "SGI " : "NGI ",
2959 (rate
& RATE_MCS_HT_STBC_MSK
) ? "STBC " : "",
2960 (rate
& RATE_MCS_LDPC_MSK
) ? "LDPC " : "",
2961 (rate
& RATE_MCS_BF_MSK
) ? "BF " : "",
2962 (rate
& RATE_MCS_ZLF_MSK
) ? "ZLF " : "");
2966 * Program the device to use fixed rate for frame transmit
2967 * This is for debugging/testing only
2968 * once the device start use fixed rate, we need to reload the module
2969 * to being back the normal operation.
2971 static void rs_program_fix_rate(struct iwl_mvm
*mvm
,
2972 struct iwl_lq_sta
*lq_sta
)
2974 lq_sta
->active_legacy_rate
= 0x0FFF; /* 1 - 54 MBits, includes CCK */
2975 lq_sta
->active_siso_rate
= 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2976 lq_sta
->active_mimo2_rate
= 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2978 IWL_DEBUG_RATE(mvm
, "sta_id %d rate 0x%X\n",
2979 lq_sta
->lq
.sta_id
, lq_sta
->pers
.dbg_fixed_rate
);
2981 if (lq_sta
->pers
.dbg_fixed_rate
) {
2982 rs_fill_lq_cmd(mvm
, NULL
, lq_sta
, NULL
);
2983 iwl_mvm_send_lq_cmd(lq_sta
->pers
.drv
, &lq_sta
->lq
, false);
2987 static ssize_t
rs_sta_dbgfs_scale_table_write(struct file
*file
,
2988 const char __user
*user_buf
, size_t count
, loff_t
*ppos
)
2990 struct iwl_lq_sta
*lq_sta
= file
->private_data
;
2991 struct iwl_mvm
*mvm
;
2996 mvm
= lq_sta
->pers
.drv
;
2997 memset(buf
, 0, sizeof(buf
));
2998 buf_size
= min(count
, sizeof(buf
) - 1);
2999 if (copy_from_user(buf
, user_buf
, buf_size
))
3002 if (sscanf(buf
, "%x", &parsed_rate
) == 1)
3003 lq_sta
->pers
.dbg_fixed_rate
= parsed_rate
;
3005 lq_sta
->pers
.dbg_fixed_rate
= 0;
3007 rs_program_fix_rate(mvm
, lq_sta
);
3012 static ssize_t
rs_sta_dbgfs_scale_table_read(struct file
*file
,
3013 char __user
*user_buf
, size_t count
, loff_t
*ppos
)
3020 struct iwl_lq_sta
*lq_sta
= file
->private_data
;
3021 struct iwl_mvm
*mvm
;
3022 struct iwl_scale_tbl_info
*tbl
= &(lq_sta
->lq_info
[lq_sta
->active_tbl
]);
3023 struct rs_rate
*rate
= &tbl
->rate
;
3024 mvm
= lq_sta
->pers
.drv
;
3025 buff
= kmalloc(2048, GFP_KERNEL
);
3029 desc
+= sprintf(buff
+desc
, "sta_id %d\n", lq_sta
->lq
.sta_id
);
3030 desc
+= sprintf(buff
+desc
, "failed=%d success=%d rate=0%lX\n",
3031 lq_sta
->total_failed
, lq_sta
->total_success
,
3032 lq_sta
->active_legacy_rate
);
3033 desc
+= sprintf(buff
+desc
, "fixed rate 0x%X\n",
3034 lq_sta
->pers
.dbg_fixed_rate
);
3035 desc
+= sprintf(buff
+desc
, "valid_tx_ant %s%s%s\n",
3036 (mvm
->fw
->valid_tx_ant
& ANT_A
) ? "ANT_A," : "",
3037 (mvm
->fw
->valid_tx_ant
& ANT_B
) ? "ANT_B," : "",
3038 (mvm
->fw
->valid_tx_ant
& ANT_C
) ? "ANT_C" : "");
3039 desc
+= sprintf(buff
+desc
, "lq type %s\n",
3040 (is_legacy(rate
)) ? "legacy" :
3041 is_vht(rate
) ? "VHT" : "HT");
3042 if (!is_legacy(rate
)) {
3043 desc
+= sprintf(buff
+desc
, " %s",
3044 (is_siso(rate
)) ? "SISO" : "MIMO2");
3045 desc
+= sprintf(buff
+desc
, " %s",
3046 (is_ht20(rate
)) ? "20MHz" :
3047 (is_ht40(rate
)) ? "40MHz" :
3048 (is_ht80(rate
)) ? "80Mhz" : "BAD BW");
3049 desc
+= sprintf(buff
+desc
, " %s %s %s\n",
3050 (rate
->sgi
) ? "SGI" : "NGI",
3051 (rate
->ldpc
) ? "LDPC" : "BCC",
3052 (lq_sta
->is_agg
) ? "AGG on" : "");
3054 desc
+= sprintf(buff
+desc
, "last tx rate=0x%X\n",
3055 lq_sta
->last_rate_n_flags
);
3056 desc
+= sprintf(buff
+desc
,
3057 "general: flags=0x%X mimo-d=%d s-ant=0x%x d-ant=0x%x\n",
3059 lq_sta
->lq
.mimo_delim
,
3060 lq_sta
->lq
.single_stream_ant_msk
,
3061 lq_sta
->lq
.dual_stream_ant_msk
);
3063 desc
+= sprintf(buff
+desc
,
3064 "agg: time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
3065 le16_to_cpu(lq_sta
->lq
.agg_time_limit
),
3066 lq_sta
->lq
.agg_disable_start_th
,
3067 lq_sta
->lq
.agg_frame_cnt_limit
);
3069 desc
+= sprintf(buff
+desc
, "reduced tpc=%d\n", lq_sta
->lq
.reduced_tpc
);
3070 desc
+= sprintf(buff
+desc
,
3071 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
3072 lq_sta
->lq
.initial_rate_index
[0],
3073 lq_sta
->lq
.initial_rate_index
[1],
3074 lq_sta
->lq
.initial_rate_index
[2],
3075 lq_sta
->lq
.initial_rate_index
[3]);
3077 for (i
= 0; i
< LINK_QUAL_MAX_RETRY_NUM
; i
++) {
3078 u32 r
= le32_to_cpu(lq_sta
->lq
.rs_table
[i
]);
3080 desc
+= sprintf(buff
+desc
, " rate[%d] 0x%X ", i
, r
);
3081 desc
+= rs_pretty_print_rate(buff
+desc
, r
);
3084 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buff
, desc
);
3089 static const struct file_operations rs_sta_dbgfs_scale_table_ops
= {
3090 .write
= rs_sta_dbgfs_scale_table_write
,
3091 .read
= rs_sta_dbgfs_scale_table_read
,
3092 .open
= simple_open
,
3093 .llseek
= default_llseek
,
3095 static ssize_t
rs_sta_dbgfs_stats_table_read(struct file
*file
,
3096 char __user
*user_buf
, size_t count
, loff_t
*ppos
)
3102 struct iwl_scale_tbl_info
*tbl
;
3103 struct rs_rate
*rate
;
3104 struct iwl_lq_sta
*lq_sta
= file
->private_data
;
3106 buff
= kmalloc(1024, GFP_KERNEL
);
3110 for (i
= 0; i
< LQ_SIZE
; i
++) {
3111 tbl
= &(lq_sta
->lq_info
[i
]);
3113 desc
+= sprintf(buff
+desc
,
3114 "%s type=%d SGI=%d BW=%s DUP=0\n"
3116 lq_sta
->active_tbl
== i
? "*" : "x",
3119 is_ht20(rate
) ? "20Mhz" :
3120 is_ht40(rate
) ? "40Mhz" :
3121 is_ht80(rate
) ? "80Mhz" : "ERR",
3123 for (j
= 0; j
< IWL_RATE_COUNT
; j
++) {
3124 desc
+= sprintf(buff
+desc
,
3125 "counter=%d success=%d %%=%d\n",
3126 tbl
->win
[j
].counter
,
3127 tbl
->win
[j
].success_counter
,
3128 tbl
->win
[j
].success_ratio
);
3131 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buff
, desc
);
3136 static const struct file_operations rs_sta_dbgfs_stats_table_ops
= {
3137 .read
= rs_sta_dbgfs_stats_table_read
,
3138 .open
= simple_open
,
3139 .llseek
= default_llseek
,
3142 static ssize_t
rs_sta_dbgfs_drv_tx_stats_read(struct file
*file
,
3143 char __user
*user_buf
,
3144 size_t count
, loff_t
*ppos
)
3146 static const char * const column_name
[] = {
3147 [RS_COLUMN_LEGACY_ANT_A
] = "LEGACY_ANT_A",
3148 [RS_COLUMN_LEGACY_ANT_B
] = "LEGACY_ANT_B",
3149 [RS_COLUMN_SISO_ANT_A
] = "SISO_ANT_A",
3150 [RS_COLUMN_SISO_ANT_B
] = "SISO_ANT_B",
3151 [RS_COLUMN_SISO_ANT_A_SGI
] = "SISO_ANT_A_SGI",
3152 [RS_COLUMN_SISO_ANT_B_SGI
] = "SISO_ANT_B_SGI",
3153 [RS_COLUMN_MIMO2
] = "MIMO2",
3154 [RS_COLUMN_MIMO2_SGI
] = "MIMO2_SGI",
3157 static const char * const rate_name
[] = {
3158 [IWL_RATE_1M_INDEX
] = "1M",
3159 [IWL_RATE_2M_INDEX
] = "2M",
3160 [IWL_RATE_5M_INDEX
] = "5.5M",
3161 [IWL_RATE_11M_INDEX
] = "11M",
3162 [IWL_RATE_6M_INDEX
] = "6M|MCS0",
3163 [IWL_RATE_9M_INDEX
] = "9M",
3164 [IWL_RATE_12M_INDEX
] = "12M|MCS1",
3165 [IWL_RATE_18M_INDEX
] = "18M|MCS2",
3166 [IWL_RATE_24M_INDEX
] = "24M|MCS3",
3167 [IWL_RATE_36M_INDEX
] = "36M|MCS4",
3168 [IWL_RATE_48M_INDEX
] = "48M|MCS5",
3169 [IWL_RATE_54M_INDEX
] = "54M|MCS6",
3170 [IWL_RATE_MCS_7_INDEX
] = "MCS7",
3171 [IWL_RATE_MCS_8_INDEX
] = "MCS8",
3172 [IWL_RATE_MCS_9_INDEX
] = "MCS9",
3175 char *buff
, *pos
, *endpos
;
3178 struct iwl_lq_sta
*lq_sta
= file
->private_data
;
3179 struct rs_rate_stats
*stats
;
3180 static const size_t bufsz
= 1024;
3182 buff
= kmalloc(bufsz
, GFP_KERNEL
);
3187 endpos
= pos
+ bufsz
;
3189 pos
+= scnprintf(pos
, endpos
- pos
, "COLUMN,");
3190 for (rate
= 0; rate
< IWL_RATE_COUNT
; rate
++)
3191 pos
+= scnprintf(pos
, endpos
- pos
, "%s,", rate_name
[rate
]);
3192 pos
+= scnprintf(pos
, endpos
- pos
, "\n");
3194 for (col
= 0; col
< RS_COLUMN_COUNT
; col
++) {
3195 pos
+= scnprintf(pos
, endpos
- pos
,
3196 "%s,", column_name
[col
]);
3198 for (rate
= 0; rate
< IWL_RATE_COUNT
; rate
++) {
3199 stats
= &(lq_sta
->tx_stats
[col
][rate
]);
3200 pos
+= scnprintf(pos
, endpos
- pos
,
3205 pos
+= scnprintf(pos
, endpos
- pos
, "\n");
3208 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buff
, pos
- buff
);
3213 static ssize_t
rs_sta_dbgfs_drv_tx_stats_write(struct file
*file
,
3214 const char __user
*user_buf
,
3215 size_t count
, loff_t
*ppos
)
3217 struct iwl_lq_sta
*lq_sta
= file
->private_data
;
3218 memset(lq_sta
->tx_stats
, 0, sizeof(lq_sta
->tx_stats
));
3223 static const struct file_operations rs_sta_dbgfs_drv_tx_stats_ops
= {
3224 .read
= rs_sta_dbgfs_drv_tx_stats_read
,
3225 .write
= rs_sta_dbgfs_drv_tx_stats_write
,
3226 .open
= simple_open
,
3227 .llseek
= default_llseek
,
3230 static void rs_add_debugfs(void *mvm
, void *mvm_sta
, struct dentry
*dir
)
3232 struct iwl_lq_sta
*lq_sta
= mvm_sta
;
3233 debugfs_create_file("rate_scale_table", S_IRUSR
| S_IWUSR
, dir
,
3234 lq_sta
, &rs_sta_dbgfs_scale_table_ops
);
3235 debugfs_create_file("rate_stats_table", S_IRUSR
, dir
,
3236 lq_sta
, &rs_sta_dbgfs_stats_table_ops
);
3237 debugfs_create_file("drv_tx_stats", S_IRUSR
| S_IWUSR
, dir
,
3238 lq_sta
, &rs_sta_dbgfs_drv_tx_stats_ops
);
3239 debugfs_create_u8("tx_agg_tid_enable", S_IRUSR
| S_IWUSR
, dir
,
3240 &lq_sta
->tx_agg_tid_en
);
3241 debugfs_create_u8("reduced_tpc", S_IRUSR
| S_IWUSR
, dir
,
3242 &lq_sta
->pers
.dbg_fixed_txp_reduction
);
3245 static void rs_remove_debugfs(void *mvm
, void *mvm_sta
)
3251 * Initialization of rate scaling information is done by driver after
3252 * the station is added. Since mac80211 calls this function before a
3253 * station is added we ignore it.
3255 static void rs_rate_init_stub(void *mvm_r
,
3256 struct ieee80211_supported_band
*sband
,
3257 struct cfg80211_chan_def
*chandef
,
3258 struct ieee80211_sta
*sta
, void *mvm_sta
)
3262 static const struct rate_control_ops rs_mvm_ops
= {
3264 .tx_status
= rs_tx_status
,
3265 .get_rate
= rs_get_rate
,
3266 .rate_init
= rs_rate_init_stub
,
3269 .alloc_sta
= rs_alloc_sta
,
3270 .free_sta
= rs_free_sta
,
3271 .rate_update
= rs_rate_update
,
3272 #ifdef CONFIG_MAC80211_DEBUGFS
3273 .add_sta_debugfs
= rs_add_debugfs
,
3274 .remove_sta_debugfs
= rs_remove_debugfs
,
3278 int iwl_mvm_rate_control_register(void)
3280 return ieee80211_rate_control_register(&rs_mvm_ops
);
3283 void iwl_mvm_rate_control_unregister(void)
3285 ieee80211_rate_control_unregister(&rs_mvm_ops
);
3289 * iwl_mvm_tx_protection - Gets LQ command, change it to enable/disable
3290 * Tx protection, according to this rquest and previous requests,
3291 * and send the LQ command.
3292 * @mvmsta: The station
3293 * @enable: Enable Tx protection?
3295 int iwl_mvm_tx_protection(struct iwl_mvm
*mvm
, struct iwl_mvm_sta
*mvmsta
,
3298 struct iwl_lq_cmd
*lq
= &mvmsta
->lq_sta
.lq
;
3300 lockdep_assert_held(&mvm
->mutex
);
3303 if (mvmsta
->tx_protection
== 0)
3304 lq
->flags
|= LQ_FLAG_USE_RTS_MSK
;
3305 mvmsta
->tx_protection
++;
3307 mvmsta
->tx_protection
--;
3308 if (mvmsta
->tx_protection
== 0)
3309 lq
->flags
&= ~LQ_FLAG_USE_RTS_MSK
;
3312 return iwl_mvm_send_lq_cmd(mvm
, lq
, false);