xref: /linux/drivers/net/wireless/intel/iwlwifi/mvm/rs.c (revision ab93e0dd72c37d378dd936f031ffb83ff2bd87ce)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /******************************************************************************
3  *
4  * Copyright(c) 2005 - 2014, 2018 - 2023 Intel Corporation. All rights reserved.
5  * Copyright(c) 2025 Intel Corporation
6  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
7  * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
8  *****************************************************************************/
9 #include <linux/kernel.h>
10 #include <linux/skbuff.h>
11 #include <linux/slab.h>
12 #include <net/mac80211.h>
13 
14 #include <linux/netdevice.h>
15 #include <linux/etherdevice.h>
16 #include <linux/delay.h>
17 
18 #include <linux/workqueue.h>
19 #include "rs.h"
20 #include "fw-api.h"
21 #include "sta.h"
22 #include "iwl-op-mode.h"
23 #include "mvm.h"
24 #include "debugfs.h"
25 
26 #define IWL_RATE_MAX_WINDOW		62	/* # tx in history window */
27 
28 /* Calculations of success ratio are done in fixed point where 12800 is 100%.
29  * Use this macro when dealing with thresholds consts set as a percentage
30  */
31 #define RS_PERCENT(x) (128 * x)
32 
33 static u8 rs_ht_to_legacy[] = {
34 	[IWL_RATE_MCS_0_INDEX] = IWL_RATE_6M_INDEX,
35 	[IWL_RATE_MCS_1_INDEX] = IWL_RATE_9M_INDEX,
36 	[IWL_RATE_MCS_2_INDEX] = IWL_RATE_12M_INDEX,
37 	[IWL_RATE_MCS_3_INDEX] = IWL_RATE_18M_INDEX,
38 	[IWL_RATE_MCS_4_INDEX] = IWL_RATE_24M_INDEX,
39 	[IWL_RATE_MCS_5_INDEX] = IWL_RATE_36M_INDEX,
40 	[IWL_RATE_MCS_6_INDEX] = IWL_RATE_48M_INDEX,
41 	[IWL_RATE_MCS_7_INDEX] = IWL_RATE_54M_INDEX,
42 	[IWL_RATE_MCS_8_INDEX] = IWL_RATE_54M_INDEX,
43 	[IWL_RATE_MCS_9_INDEX] = IWL_RATE_54M_INDEX,
44 };
45 
46 static const u8 ant_toggle_lookup[] = {
47 	[ANT_NONE] = ANT_NONE,
48 	[ANT_A] = ANT_B,
49 	[ANT_B] = ANT_A,
50 	[ANT_AB] = ANT_AB,
51 };
52 
53 #define IWL_DECLARE_RATE_INFO(r, s, rp, rn)			      \
54 	[IWL_RATE_##r##M_INDEX] = { IWL_RATE_##r##M_PLCP,	      \
55 				    IWL_RATE_HT_SISO_MCS_##s##_PLCP,  \
56 				    IWL_RATE_HT_MIMO2_MCS_##s##_PLCP, \
57 				    IWL_RATE_VHT_SISO_MCS_##s##_PLCP, \
58 				    IWL_RATE_VHT_MIMO2_MCS_##s##_PLCP,\
59 				    IWL_RATE_##rp##M_INDEX,	      \
60 				    IWL_RATE_##rn##M_INDEX }
61 
62 #define IWL_DECLARE_MCS_RATE(s)						  \
63 	[IWL_RATE_MCS_##s##_INDEX] = { IWL_RATE_INVM_PLCP,		  \
64 				       IWL_RATE_HT_SISO_MCS_##s##_PLCP,	  \
65 				       IWL_RATE_HT_MIMO2_MCS_##s##_PLCP,  \
66 				       IWL_RATE_VHT_SISO_MCS_##s##_PLCP,  \
67 				       IWL_RATE_VHT_MIMO2_MCS_##s##_PLCP, \
68 				       IWL_RATE_INVM_INDEX,	          \
69 				       IWL_RATE_INVM_INDEX }
70 
71 /*
72  * Parameter order:
73  *   rate, ht rate, prev rate, next rate
74  *
75  * If there isn't a valid next or previous rate then INV is used which
76  * maps to IWL_RATE_INVALID
77  *
78  */
79 static const struct iwl_rs_rate_info iwl_rates[IWL_RATE_COUNT] = {
80 	IWL_DECLARE_RATE_INFO(1, INV, INV, 2),   /*  1mbps */
81 	IWL_DECLARE_RATE_INFO(2, INV, 1, 5),     /*  2mbps */
82 	IWL_DECLARE_RATE_INFO(5, INV, 2, 11),    /*5.5mbps */
83 	IWL_DECLARE_RATE_INFO(11, INV, 9, 12),   /* 11mbps */
84 	IWL_DECLARE_RATE_INFO(6, 0, 5, 11),      /*  6mbps ; MCS 0 */
85 	IWL_DECLARE_RATE_INFO(9, INV, 6, 11),    /*  9mbps */
86 	IWL_DECLARE_RATE_INFO(12, 1, 11, 18),    /* 12mbps ; MCS 1 */
87 	IWL_DECLARE_RATE_INFO(18, 2, 12, 24),    /* 18mbps ; MCS 2 */
88 	IWL_DECLARE_RATE_INFO(24, 3, 18, 36),    /* 24mbps ; MCS 3 */
89 	IWL_DECLARE_RATE_INFO(36, 4, 24, 48),    /* 36mbps ; MCS 4 */
90 	IWL_DECLARE_RATE_INFO(48, 5, 36, 54),    /* 48mbps ; MCS 5 */
91 	IWL_DECLARE_RATE_INFO(54, 6, 48, INV),   /* 54mbps ; MCS 6 */
92 	IWL_DECLARE_MCS_RATE(7),                 /* MCS 7 */
93 	IWL_DECLARE_MCS_RATE(8),                 /* MCS 8 */
94 	IWL_DECLARE_MCS_RATE(9),                 /* MCS 9 */
95 };
96 
97 enum rs_action {
98 	RS_ACTION_STAY = 0,
99 	RS_ACTION_DOWNSCALE = -1,
100 	RS_ACTION_UPSCALE = 1,
101 };
102 
103 enum rs_column_mode {
104 	RS_INVALID = 0,
105 	RS_LEGACY,
106 	RS_SISO,
107 	RS_MIMO2,
108 };
109 
110 #define MAX_NEXT_COLUMNS 7
111 #define MAX_COLUMN_CHECKS 3
112 
113 struct rs_tx_column;
114 
115 typedef bool (*allow_column_func_t) (struct iwl_mvm *mvm,
116 				     struct ieee80211_sta *sta,
117 				     struct rs_rate *rate,
118 				     const struct rs_tx_column *next_col);
119 
120 struct rs_tx_column {
121 	enum rs_column_mode mode;
122 	u8 ant;
123 	bool sgi;
124 	enum rs_column next_columns[MAX_NEXT_COLUMNS];
125 	allow_column_func_t checks[MAX_COLUMN_CHECKS];
126 };
127 
rs_ant_allow(struct iwl_mvm * mvm,struct ieee80211_sta * sta,struct rs_rate * rate,const struct rs_tx_column * next_col)128 static bool rs_ant_allow(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
129 			 struct rs_rate *rate,
130 			 const struct rs_tx_column *next_col)
131 {
132 	return iwl_mvm_bt_coex_is_ant_avail(mvm, next_col->ant);
133 }
134 
rs_mimo_allow(struct iwl_mvm * mvm,struct ieee80211_sta * sta,struct rs_rate * rate,const struct rs_tx_column * next_col)135 static bool rs_mimo_allow(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
136 			  struct rs_rate *rate,
137 			  const struct rs_tx_column *next_col)
138 {
139 	if (!sta->deflink.ht_cap.ht_supported)
140 		return false;
141 
142 	if (sta->deflink.smps_mode == IEEE80211_SMPS_STATIC)
143 		return false;
144 
145 	if (num_of_ant(iwl_mvm_get_valid_tx_ant(mvm)) < 2)
146 		return false;
147 
148 	if (!iwl_mvm_bt_coex_is_mimo_allowed(mvm, sta))
149 		return false;
150 
151 	if (mvm->nvm_data->sku_cap_mimo_disabled)
152 		return false;
153 
154 	return true;
155 }
156 
rs_siso_allow(struct iwl_mvm * mvm,struct ieee80211_sta * sta,struct rs_rate * rate,const struct rs_tx_column * next_col)157 static bool rs_siso_allow(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
158 			  struct rs_rate *rate,
159 			  const struct rs_tx_column *next_col)
160 {
161 	if (!sta->deflink.ht_cap.ht_supported)
162 		return false;
163 
164 	return true;
165 }
166 
rs_sgi_allow(struct iwl_mvm * mvm,struct ieee80211_sta * sta,struct rs_rate * rate,const struct rs_tx_column * next_col)167 static bool rs_sgi_allow(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
168 			 struct rs_rate *rate,
169 			 const struct rs_tx_column *next_col)
170 {
171 	struct ieee80211_sta_ht_cap *ht_cap = &sta->deflink.ht_cap;
172 	struct ieee80211_sta_vht_cap *vht_cap = &sta->deflink.vht_cap;
173 
174 	if (is_ht20(rate) && (ht_cap->cap &
175 			     IEEE80211_HT_CAP_SGI_20))
176 		return true;
177 	if (is_ht40(rate) && (ht_cap->cap &
178 			     IEEE80211_HT_CAP_SGI_40))
179 		return true;
180 	if (is_ht80(rate) && (vht_cap->cap &
181 			     IEEE80211_VHT_CAP_SHORT_GI_80))
182 		return true;
183 	if (is_ht160(rate) && (vht_cap->cap &
184 			     IEEE80211_VHT_CAP_SHORT_GI_160))
185 		return true;
186 
187 	return false;
188 }
189 
190 static const struct rs_tx_column rs_tx_columns[] = {
191 	[RS_COLUMN_LEGACY_ANT_A] = {
192 		.mode = RS_LEGACY,
193 		.ant = ANT_A,
194 		.next_columns = {
195 			RS_COLUMN_LEGACY_ANT_B,
196 			RS_COLUMN_SISO_ANT_A,
197 			RS_COLUMN_MIMO2,
198 			RS_COLUMN_INVALID,
199 			RS_COLUMN_INVALID,
200 			RS_COLUMN_INVALID,
201 			RS_COLUMN_INVALID,
202 		},
203 		.checks = {
204 			rs_ant_allow,
205 		},
206 	},
207 	[RS_COLUMN_LEGACY_ANT_B] = {
208 		.mode = RS_LEGACY,
209 		.ant = ANT_B,
210 		.next_columns = {
211 			RS_COLUMN_LEGACY_ANT_A,
212 			RS_COLUMN_SISO_ANT_B,
213 			RS_COLUMN_MIMO2,
214 			RS_COLUMN_INVALID,
215 			RS_COLUMN_INVALID,
216 			RS_COLUMN_INVALID,
217 			RS_COLUMN_INVALID,
218 		},
219 		.checks = {
220 			rs_ant_allow,
221 		},
222 	},
223 	[RS_COLUMN_SISO_ANT_A] = {
224 		.mode = RS_SISO,
225 		.ant = ANT_A,
226 		.next_columns = {
227 			RS_COLUMN_SISO_ANT_B,
228 			RS_COLUMN_MIMO2,
229 			RS_COLUMN_SISO_ANT_A_SGI,
230 			RS_COLUMN_LEGACY_ANT_A,
231 			RS_COLUMN_LEGACY_ANT_B,
232 			RS_COLUMN_INVALID,
233 			RS_COLUMN_INVALID,
234 		},
235 		.checks = {
236 			rs_siso_allow,
237 			rs_ant_allow,
238 		},
239 	},
240 	[RS_COLUMN_SISO_ANT_B] = {
241 		.mode = RS_SISO,
242 		.ant = ANT_B,
243 		.next_columns = {
244 			RS_COLUMN_SISO_ANT_A,
245 			RS_COLUMN_MIMO2,
246 			RS_COLUMN_SISO_ANT_B_SGI,
247 			RS_COLUMN_LEGACY_ANT_A,
248 			RS_COLUMN_LEGACY_ANT_B,
249 			RS_COLUMN_INVALID,
250 			RS_COLUMN_INVALID,
251 		},
252 		.checks = {
253 			rs_siso_allow,
254 			rs_ant_allow,
255 		},
256 	},
257 	[RS_COLUMN_SISO_ANT_A_SGI] = {
258 		.mode = RS_SISO,
259 		.ant = ANT_A,
260 		.sgi = true,
261 		.next_columns = {
262 			RS_COLUMN_SISO_ANT_B_SGI,
263 			RS_COLUMN_MIMO2_SGI,
264 			RS_COLUMN_SISO_ANT_A,
265 			RS_COLUMN_LEGACY_ANT_A,
266 			RS_COLUMN_LEGACY_ANT_B,
267 			RS_COLUMN_INVALID,
268 			RS_COLUMN_INVALID,
269 		},
270 		.checks = {
271 			rs_siso_allow,
272 			rs_ant_allow,
273 			rs_sgi_allow,
274 		},
275 	},
276 	[RS_COLUMN_SISO_ANT_B_SGI] = {
277 		.mode = RS_SISO,
278 		.ant = ANT_B,
279 		.sgi = true,
280 		.next_columns = {
281 			RS_COLUMN_SISO_ANT_A_SGI,
282 			RS_COLUMN_MIMO2_SGI,
283 			RS_COLUMN_SISO_ANT_B,
284 			RS_COLUMN_LEGACY_ANT_A,
285 			RS_COLUMN_LEGACY_ANT_B,
286 			RS_COLUMN_INVALID,
287 			RS_COLUMN_INVALID,
288 		},
289 		.checks = {
290 			rs_siso_allow,
291 			rs_ant_allow,
292 			rs_sgi_allow,
293 		},
294 	},
295 	[RS_COLUMN_MIMO2] = {
296 		.mode = RS_MIMO2,
297 		.ant = ANT_AB,
298 		.next_columns = {
299 			RS_COLUMN_SISO_ANT_A,
300 			RS_COLUMN_MIMO2_SGI,
301 			RS_COLUMN_LEGACY_ANT_A,
302 			RS_COLUMN_LEGACY_ANT_B,
303 			RS_COLUMN_INVALID,
304 			RS_COLUMN_INVALID,
305 			RS_COLUMN_INVALID,
306 		},
307 		.checks = {
308 			rs_mimo_allow,
309 		},
310 	},
311 	[RS_COLUMN_MIMO2_SGI] = {
312 		.mode = RS_MIMO2,
313 		.ant = ANT_AB,
314 		.sgi = true,
315 		.next_columns = {
316 			RS_COLUMN_SISO_ANT_A_SGI,
317 			RS_COLUMN_MIMO2,
318 			RS_COLUMN_LEGACY_ANT_A,
319 			RS_COLUMN_LEGACY_ANT_B,
320 			RS_COLUMN_INVALID,
321 			RS_COLUMN_INVALID,
322 			RS_COLUMN_INVALID,
323 		},
324 		.checks = {
325 			rs_mimo_allow,
326 			rs_sgi_allow,
327 		},
328 	},
329 };
330 
rs_extract_rate(u32 rate_n_flags)331 static inline u8 rs_extract_rate(u32 rate_n_flags)
332 {
333 	/* also works for HT because bits 7:6 are zero there */
334 	return (u8)(rate_n_flags & RATE_LEGACY_RATE_MSK_V1);
335 }
336 
iwl_hwrate_to_plcp_idx(u32 rate_n_flags)337 static int iwl_hwrate_to_plcp_idx(u32 rate_n_flags)
338 {
339 	int idx = 0;
340 
341 	if (rate_n_flags & RATE_MCS_HT_MSK_V1) {
342 		idx = rate_n_flags & RATE_HT_MCS_RATE_CODE_MSK_V1;
343 		idx += IWL_RATE_MCS_0_INDEX;
344 
345 		/* skip 9M not supported in HT*/
346 		if (idx >= IWL_RATE_9M_INDEX)
347 			idx += 1;
348 		if ((idx >= IWL_FIRST_HT_RATE) && (idx <= IWL_LAST_HT_RATE))
349 			return idx;
350 	} else if (rate_n_flags & RATE_MCS_VHT_MSK_V1 ||
351 		   rate_n_flags & RATE_MCS_HE_MSK_V1) {
352 		idx = rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK;
353 		idx += IWL_RATE_MCS_0_INDEX;
354 
355 		/* skip 9M not supported in VHT*/
356 		if (idx >= IWL_RATE_9M_INDEX)
357 			idx++;
358 		if ((idx >= IWL_FIRST_VHT_RATE) && (idx <= IWL_LAST_VHT_RATE))
359 			return idx;
360 		if ((rate_n_flags & RATE_MCS_HE_MSK_V1) &&
361 		    idx <= IWL_LAST_HE_RATE)
362 			return idx;
363 	} else {
364 		/* legacy rate format, search for match in table */
365 
366 		u8 legacy_rate = rs_extract_rate(rate_n_flags);
367 		for (idx = 0; idx < ARRAY_SIZE(iwl_rates); idx++)
368 			if (iwl_rates[idx].plcp == legacy_rate)
369 				return idx;
370 	}
371 
372 	return IWL_RATE_INVALID;
373 }
374 
375 static void rs_rate_scale_perform(struct iwl_mvm *mvm,
376 				  struct ieee80211_sta *sta,
377 				  struct iwl_lq_sta *lq_sta,
378 				  int tid, bool ndp);
379 static void rs_fill_lq_cmd(struct iwl_mvm *mvm,
380 			   struct ieee80211_sta *sta,
381 			   struct iwl_lq_sta *lq_sta,
382 			   const struct rs_rate *initial_rate);
383 static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search);
384 
385 /*
386  * The following tables contain the expected throughput metrics for all rates
387  *
388  *	1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
389  *
390  * where invalid entries are zeros.
391  *
392  * CCK rates are only valid in legacy table and will only be used in G
393  * (2.4 GHz) band.
394  */
395 static const u16 expected_tpt_legacy[IWL_RATE_COUNT] = {
396 	7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 0, 0, 0
397 };
398 
399 /* Expected TpT tables. 4 indexes:
400  * 0 - NGI, 1 - SGI, 2 - AGG+NGI, 3 - AGG+SGI
401  */
402 static const u16 expected_tpt_siso_20MHz[4][IWL_RATE_COUNT] = {
403 	{0, 0, 0, 0, 42, 0,  76, 102, 124, 159, 183, 193, 202, 216, 0},
404 	{0, 0, 0, 0, 46, 0,  82, 110, 132, 168, 192, 202, 210, 225, 0},
405 	{0, 0, 0, 0, 49, 0,  97, 145, 192, 285, 375, 420, 464, 551, 0},
406 	{0, 0, 0, 0, 54, 0, 108, 160, 213, 315, 415, 465, 513, 608, 0},
407 };
408 
409 static const u16 expected_tpt_siso_40MHz[4][IWL_RATE_COUNT] = {
410 	{0, 0, 0, 0,  77, 0, 127, 160, 184, 220, 242, 250,  257,  269,  275},
411 	{0, 0, 0, 0,  83, 0, 135, 169, 193, 229, 250, 257,  264,  275,  280},
412 	{0, 0, 0, 0, 101, 0, 199, 295, 389, 570, 744, 828,  911, 1070, 1173},
413 	{0, 0, 0, 0, 112, 0, 220, 326, 429, 629, 819, 912, 1000, 1173, 1284},
414 };
415 
416 static const u16 expected_tpt_siso_80MHz[4][IWL_RATE_COUNT] = {
417 	{0, 0, 0, 0, 130, 0, 191, 223, 244,  273,  288,  294,  298,  305,  308},
418 	{0, 0, 0, 0, 138, 0, 200, 231, 251,  279,  293,  298,  302,  308,  312},
419 	{0, 0, 0, 0, 217, 0, 429, 634, 834, 1220, 1585, 1760, 1931, 2258, 2466},
420 	{0, 0, 0, 0, 241, 0, 475, 701, 921, 1343, 1741, 1931, 2117, 2468, 2691},
421 };
422 
423 static const u16 expected_tpt_siso_160MHz[4][IWL_RATE_COUNT] = {
424 	{0, 0, 0, 0, 191, 0, 244, 288,  298,  308,  313,  318,  323,  328,  330},
425 	{0, 0, 0, 0, 200, 0, 251, 293,  302,  312,  317,  322,  327,  332,  334},
426 	{0, 0, 0, 0, 439, 0, 875, 1307, 1736, 2584, 3419, 3831, 4240, 5049, 5581},
427 	{0, 0, 0, 0, 488, 0, 972, 1451, 1925, 2864, 3785, 4240, 4691, 5581, 6165},
428 };
429 
430 static const u16 expected_tpt_mimo2_20MHz[4][IWL_RATE_COUNT] = {
431 	{0, 0, 0, 0,  74, 0, 123, 155, 179, 213, 235, 243, 250,  261, 0},
432 	{0, 0, 0, 0,  81, 0, 131, 164, 187, 221, 242, 250, 256,  267, 0},
433 	{0, 0, 0, 0,  98, 0, 193, 286, 375, 550, 718, 799, 878, 1032, 0},
434 	{0, 0, 0, 0, 109, 0, 214, 316, 414, 607, 790, 879, 965, 1132, 0},
435 };
436 
437 static const u16 expected_tpt_mimo2_40MHz[4][IWL_RATE_COUNT] = {
438 	{0, 0, 0, 0, 123, 0, 182, 214, 235,  264,  279,  285,  289,  296,  300},
439 	{0, 0, 0, 0, 131, 0, 191, 222, 242,  270,  284,  289,  293,  300,  303},
440 	{0, 0, 0, 0, 200, 0, 390, 571, 741, 1067, 1365, 1505, 1640, 1894, 2053},
441 	{0, 0, 0, 0, 221, 0, 430, 630, 816, 1169, 1490, 1641, 1784, 2053, 2221},
442 };
443 
444 static const u16 expected_tpt_mimo2_80MHz[4][IWL_RATE_COUNT] = {
445 	{0, 0, 0, 0, 182, 0, 240,  264,  278,  299,  308,  311,  313,  317,  319},
446 	{0, 0, 0, 0, 190, 0, 247,  269,  282,  302,  310,  313,  315,  319,  320},
447 	{0, 0, 0, 0, 428, 0, 833, 1215, 1577, 2254, 2863, 3147, 3418, 3913, 4219},
448 	{0, 0, 0, 0, 474, 0, 920, 1338, 1732, 2464, 3116, 3418, 3705, 4225, 4545},
449 };
450 
451 static const u16 expected_tpt_mimo2_160MHz[4][IWL_RATE_COUNT] = {
452 	{0, 0, 0, 0, 240, 0, 278,  308,  313,  319,  322,  324,  328,  330,   334},
453 	{0, 0, 0, 0, 247, 0, 282,  310,  315,  320,  323,  325,  329,  332,   338},
454 	{0, 0, 0, 0, 875, 0, 1735, 2582, 3414, 5043, 6619, 7389, 8147, 9629,  10592},
455 	{0, 0, 0, 0, 971, 0, 1925, 2861, 3779, 5574, 7304, 8147, 8976, 10592, 11640},
456 };
457 
rs_pretty_lq_type(enum iwl_table_type type)458 static const char *rs_pretty_lq_type(enum iwl_table_type type)
459 {
460 	static const char * const lq_types[] = {
461 		[LQ_NONE] = "NONE",
462 		[LQ_LEGACY_A] = "LEGACY_A",
463 		[LQ_LEGACY_G] = "LEGACY_G",
464 		[LQ_HT_SISO] = "HT SISO",
465 		[LQ_HT_MIMO2] = "HT MIMO",
466 		[LQ_VHT_SISO] = "VHT SISO",
467 		[LQ_VHT_MIMO2] = "VHT MIMO",
468 		[LQ_HE_SISO] = "HE SISO",
469 		[LQ_HE_MIMO2] = "HE MIMO",
470 	};
471 
472 	if (type < LQ_NONE || type >= LQ_MAX)
473 		return "UNKNOWN";
474 
475 	return lq_types[type];
476 }
477 
rs_pretty_rate(const struct rs_rate * rate)478 static char *rs_pretty_rate(const struct rs_rate *rate)
479 {
480 	static char buf[40];
481 	static const char * const legacy_rates[] = {
482 		[IWL_RATE_1M_INDEX] = "1M",
483 		[IWL_RATE_2M_INDEX] = "2M",
484 		[IWL_RATE_5M_INDEX] = "5.5M",
485 		[IWL_RATE_11M_INDEX] = "11M",
486 		[IWL_RATE_6M_INDEX] = "6M",
487 		[IWL_RATE_9M_INDEX] = "9M",
488 		[IWL_RATE_12M_INDEX] = "12M",
489 		[IWL_RATE_18M_INDEX] = "18M",
490 		[IWL_RATE_24M_INDEX] = "24M",
491 		[IWL_RATE_36M_INDEX] = "36M",
492 		[IWL_RATE_48M_INDEX] = "48M",
493 		[IWL_RATE_54M_INDEX] = "54M",
494 	};
495 	static const char *const ht_vht_rates[] = {
496 		[IWL_RATE_MCS_0_INDEX] = "MCS0",
497 		[IWL_RATE_MCS_1_INDEX] = "MCS1",
498 		[IWL_RATE_MCS_2_INDEX] = "MCS2",
499 		[IWL_RATE_MCS_3_INDEX] = "MCS3",
500 		[IWL_RATE_MCS_4_INDEX] = "MCS4",
501 		[IWL_RATE_MCS_5_INDEX] = "MCS5",
502 		[IWL_RATE_MCS_6_INDEX] = "MCS6",
503 		[IWL_RATE_MCS_7_INDEX] = "MCS7",
504 		[IWL_RATE_MCS_8_INDEX] = "MCS8",
505 		[IWL_RATE_MCS_9_INDEX] = "MCS9",
506 	};
507 	const char *rate_str;
508 
509 	if (is_type_legacy(rate->type) && (rate->index <= IWL_RATE_54M_INDEX))
510 		rate_str = legacy_rates[rate->index];
511 	else if ((is_type_ht(rate->type) || is_type_vht(rate->type)) &&
512 		 (rate->index >= IWL_RATE_MCS_0_INDEX) &&
513 		 (rate->index <= IWL_RATE_MCS_9_INDEX))
514 		rate_str = ht_vht_rates[rate->index];
515 	else
516 		rate_str = NULL;
517 
518 	sprintf(buf, "(%s|%s|%s)", rs_pretty_lq_type(rate->type),
519 		iwl_rs_pretty_ant(rate->ant), rate_str ?: "BAD_RATE");
520 	return buf;
521 }
522 
rs_dump_rate(struct iwl_mvm * mvm,const struct rs_rate * rate,const char * prefix)523 static inline void rs_dump_rate(struct iwl_mvm *mvm, const struct rs_rate *rate,
524 				const char *prefix)
525 {
526 	IWL_DEBUG_RATE(mvm,
527 		       "%s: %s BW: %d SGI: %d LDPC: %d STBC: %d\n",
528 		       prefix, rs_pretty_rate(rate), rate->bw,
529 		       rate->sgi, rate->ldpc, rate->stbc);
530 }
531 
rs_rate_scale_clear_window(struct iwl_rate_scale_data * window)532 static void rs_rate_scale_clear_window(struct iwl_rate_scale_data *window)
533 {
534 	window->data = 0;
535 	window->success_counter = 0;
536 	window->success_ratio = IWL_INVALID_VALUE;
537 	window->counter = 0;
538 	window->average_tpt = IWL_INVALID_VALUE;
539 }
540 
rs_rate_scale_clear_tbl_windows(struct iwl_mvm * mvm,struct iwl_scale_tbl_info * tbl)541 static void rs_rate_scale_clear_tbl_windows(struct iwl_mvm *mvm,
542 					    struct iwl_scale_tbl_info *tbl)
543 {
544 	int i;
545 
546 	IWL_DEBUG_RATE(mvm, "Clearing up window stats\n");
547 	for (i = 0; i < IWL_RATE_COUNT; i++)
548 		rs_rate_scale_clear_window(&tbl->win[i]);
549 
550 	for (i = 0; i < ARRAY_SIZE(tbl->tpc_win); i++)
551 		rs_rate_scale_clear_window(&tbl->tpc_win[i]);
552 }
553 
rs_is_valid_ant(u8 valid_antenna,u8 ant_type)554 static inline u8 rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
555 {
556 	return (ant_type & valid_antenna) == ant_type;
557 }
558 
rs_tl_turn_on_agg_for_tid(struct iwl_mvm * mvm,struct iwl_lq_sta * lq_data,u8 tid,struct ieee80211_sta * sta)559 static int rs_tl_turn_on_agg_for_tid(struct iwl_mvm *mvm,
560 				     struct iwl_lq_sta *lq_data, u8 tid,
561 				     struct ieee80211_sta *sta)
562 {
563 	int ret;
564 
565 	IWL_DEBUG_HT(mvm, "Starting Tx agg: STA: %pM tid: %d\n",
566 		     sta->addr, tid);
567 
568 	/* start BA session until the peer sends del BA */
569 	ret = ieee80211_start_tx_ba_session(sta, tid, 0);
570 	if (ret == -EAGAIN) {
571 		/*
572 		 * driver and mac80211 is out of sync
573 		 * this might be cause by reloading firmware
574 		 * stop the tx ba session here
575 		 */
576 		IWL_ERR(mvm, "Fail start Tx agg on tid: %d\n",
577 			tid);
578 		ieee80211_stop_tx_ba_session(sta, tid);
579 	}
580 	return ret;
581 }
582 
rs_tl_turn_on_agg(struct iwl_mvm * mvm,struct iwl_mvm_sta * mvmsta,u8 tid,struct iwl_lq_sta * lq_sta,struct ieee80211_sta * sta)583 static void rs_tl_turn_on_agg(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
584 			      u8 tid, struct iwl_lq_sta *lq_sta,
585 			      struct ieee80211_sta *sta)
586 {
587 	struct iwl_mvm_tid_data *tid_data;
588 
589 	/*
590 	 * In AP mode, tid can be equal to IWL_MAX_TID_COUNT
591 	 * when the frame is not QoS
592 	 */
593 	if (WARN_ON_ONCE(tid > IWL_MAX_TID_COUNT)) {
594 		IWL_ERR(mvm, "tid exceeds max TID count: %d/%d\n",
595 			tid, IWL_MAX_TID_COUNT);
596 		return;
597 	} else if (tid == IWL_MAX_TID_COUNT) {
598 		return;
599 	}
600 
601 	tid_data = &mvmsta->tid_data[tid];
602 	if (mvmsta->sta_state >= IEEE80211_STA_AUTHORIZED &&
603 	    tid_data->state == IWL_AGG_OFF &&
604 	    (lq_sta->tx_agg_tid_en & BIT(tid)) &&
605 	    tid_data->tx_count_last >= IWL_MVM_RS_AGG_START_THRESHOLD) {
606 		IWL_DEBUG_RATE(mvm, "try to aggregate tid %d\n", tid);
607 		if (rs_tl_turn_on_agg_for_tid(mvm, lq_sta, tid, sta) == 0)
608 			tid_data->state = IWL_AGG_QUEUED;
609 	}
610 }
611 
get_num_of_ant_from_rate(u32 rate_n_flags)612 static inline int get_num_of_ant_from_rate(u32 rate_n_flags)
613 {
614 	return !!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
615 	       !!(rate_n_flags & RATE_MCS_ANT_B_MSK);
616 }
617 
618 /*
619  * Static function to get the expected throughput from an iwl_scale_tbl_info
620  * that wraps a NULL pointer check
621  */
get_expected_tpt(struct iwl_scale_tbl_info * tbl,int rs_index)622 static s32 get_expected_tpt(struct iwl_scale_tbl_info *tbl, int rs_index)
623 {
624 	if (tbl->expected_tpt)
625 		return tbl->expected_tpt[rs_index];
626 	return 0;
627 }
628 
629 /*
630  * rs_collect_tx_data - Update the success/failure sliding window
631  *
632  * We keep a sliding window of the last 62 packets transmitted
633  * at this rate.  window->data contains the bitmask of successful
634  * packets.
635  */
_rs_collect_tx_data(struct iwl_mvm * mvm,struct iwl_scale_tbl_info * tbl,int scale_index,int attempts,int successes,struct iwl_rate_scale_data * window)636 static int _rs_collect_tx_data(struct iwl_mvm *mvm,
637 			       struct iwl_scale_tbl_info *tbl,
638 			       int scale_index, int attempts, int successes,
639 			       struct iwl_rate_scale_data *window)
640 {
641 	static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
642 	s32 fail_count, tpt;
643 
644 	/* Get expected throughput */
645 	tpt = get_expected_tpt(tbl, scale_index);
646 
647 	/*
648 	 * Keep track of only the latest 62 tx frame attempts in this rate's
649 	 * history window; anything older isn't really relevant any more.
650 	 * If we have filled up the sliding window, drop the oldest attempt;
651 	 * if the oldest attempt (highest bit in bitmap) shows "success",
652 	 * subtract "1" from the success counter (this is the main reason
653 	 * we keep these bitmaps!).
654 	 */
655 	while (attempts > 0) {
656 		if (window->counter >= IWL_RATE_MAX_WINDOW) {
657 			/* remove earliest */
658 			window->counter = IWL_RATE_MAX_WINDOW - 1;
659 
660 			if (window->data & mask) {
661 				window->data &= ~mask;
662 				window->success_counter--;
663 			}
664 		}
665 
666 		/* Increment frames-attempted counter */
667 		window->counter++;
668 
669 		/* Shift bitmap by one frame to throw away oldest history */
670 		window->data <<= 1;
671 
672 		/* Mark the most recent #successes attempts as successful */
673 		if (successes > 0) {
674 			window->success_counter++;
675 			window->data |= 0x1;
676 			successes--;
677 		}
678 
679 		attempts--;
680 	}
681 
682 	/* Calculate current success ratio, avoid divide-by-0! */
683 	if (window->counter > 0)
684 		window->success_ratio = 128 * (100 * window->success_counter)
685 					/ window->counter;
686 	else
687 		window->success_ratio = IWL_INVALID_VALUE;
688 
689 	fail_count = window->counter - window->success_counter;
690 
691 	/* Calculate average throughput, if we have enough history. */
692 	if ((fail_count >= IWL_MVM_RS_RATE_MIN_FAILURE_TH) ||
693 	    (window->success_counter >= IWL_MVM_RS_RATE_MIN_SUCCESS_TH))
694 		window->average_tpt = (window->success_ratio * tpt + 64) / 128;
695 	else
696 		window->average_tpt = IWL_INVALID_VALUE;
697 
698 	return 0;
699 }
700 
rs_collect_tpc_data(struct iwl_mvm * mvm,struct iwl_lq_sta * lq_sta,struct iwl_scale_tbl_info * tbl,int scale_index,int attempts,int successes,u8 reduced_txp)701 static int rs_collect_tpc_data(struct iwl_mvm *mvm,
702 			       struct iwl_lq_sta *lq_sta,
703 			       struct iwl_scale_tbl_info *tbl,
704 			       int scale_index, int attempts, int successes,
705 			       u8 reduced_txp)
706 {
707 	struct iwl_rate_scale_data *window = NULL;
708 
709 	if (WARN_ON_ONCE(reduced_txp > TPC_MAX_REDUCTION))
710 		return -EINVAL;
711 
712 	window = &tbl->tpc_win[reduced_txp];
713 	return  _rs_collect_tx_data(mvm, tbl, scale_index, attempts, successes,
714 				    window);
715 }
716 
rs_update_tid_tpt_stats(struct iwl_mvm * mvm,struct iwl_mvm_sta * mvmsta,u8 tid,int successes)717 static void rs_update_tid_tpt_stats(struct iwl_mvm *mvm,
718 				    struct iwl_mvm_sta *mvmsta,
719 				    u8 tid, int successes)
720 {
721 	struct iwl_mvm_tid_data *tid_data;
722 
723 	if (tid >= IWL_MAX_TID_COUNT)
724 		return;
725 
726 	tid_data = &mvmsta->tid_data[tid];
727 
728 	/*
729 	 * Measure if there're enough successful transmits per second.
730 	 * These statistics are used only to decide if we can start a
731 	 * BA session, so it should be updated only when A-MPDU is
732 	 * off.
733 	 */
734 	if (tid_data->state != IWL_AGG_OFF)
735 		return;
736 
737 	if (time_is_before_jiffies(tid_data->tpt_meas_start + HZ) ||
738 	    (tid_data->tx_count >= IWL_MVM_RS_AGG_START_THRESHOLD)) {
739 		tid_data->tx_count_last = tid_data->tx_count;
740 		tid_data->tx_count = 0;
741 		tid_data->tpt_meas_start = jiffies;
742 	} else {
743 		tid_data->tx_count += successes;
744 	}
745 }
746 
rs_collect_tlc_data(struct iwl_mvm * mvm,struct iwl_mvm_sta * mvmsta,u8 tid,struct iwl_scale_tbl_info * tbl,int scale_index,int attempts,int successes)747 static int rs_collect_tlc_data(struct iwl_mvm *mvm,
748 			       struct iwl_mvm_sta *mvmsta, u8 tid,
749 			       struct iwl_scale_tbl_info *tbl,
750 			       int scale_index, int attempts, int successes)
751 {
752 	struct iwl_rate_scale_data *window = NULL;
753 
754 	if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
755 		return -EINVAL;
756 
757 	if (tbl->column != RS_COLUMN_INVALID) {
758 		struct lq_sta_pers *pers = &mvmsta->deflink.lq_sta.rs_drv.pers;
759 
760 		pers->tx_stats[tbl->column][scale_index].total += attempts;
761 		pers->tx_stats[tbl->column][scale_index].success += successes;
762 	}
763 
764 	rs_update_tid_tpt_stats(mvm, mvmsta, tid, successes);
765 
766 	/* Select window for current tx bit rate */
767 	window = &(tbl->win[scale_index]);
768 	return _rs_collect_tx_data(mvm, tbl, scale_index, attempts, successes,
769 				   window);
770 }
771 
772 /* Convert rs_rate object into ucode rate bitmask */
ucode_rate_from_rs_rate(struct iwl_mvm * mvm,struct rs_rate * rate)773 static u32 ucode_rate_from_rs_rate(struct iwl_mvm *mvm,
774 				  struct rs_rate *rate)
775 {
776 	u32 ucode_rate = 0;
777 	int index = rate->index;
778 
779 	ucode_rate |= ((rate->ant << RATE_MCS_ANT_POS) &
780 			 RATE_MCS_ANT_AB_MSK);
781 
782 	if (is_legacy(rate)) {
783 		ucode_rate |= iwl_rates[index].plcp;
784 		if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
785 			ucode_rate |= RATE_MCS_CCK_MSK_V1;
786 		return ucode_rate;
787 	}
788 
789 	/* set RTS protection for all non legacy rates
790 	 * This helps with congested environments reducing the conflict cost to
791 	 * RTS retries only, instead of the entire BA packet.
792 	 */
793 	ucode_rate |= RATE_MCS_RTS_REQUIRED_MSK;
794 
795 	if (is_ht(rate)) {
796 		if (index < IWL_FIRST_HT_RATE || index > IWL_LAST_HT_RATE) {
797 			IWL_ERR(mvm, "Invalid HT rate index %d\n", index);
798 			index = IWL_LAST_HT_RATE;
799 		}
800 		ucode_rate |= RATE_MCS_HT_MSK_V1;
801 
802 		if (is_ht_siso(rate))
803 			ucode_rate |= iwl_rates[index].plcp_ht_siso;
804 		else if (is_ht_mimo2(rate))
805 			ucode_rate |= iwl_rates[index].plcp_ht_mimo2;
806 		else
807 			WARN_ON_ONCE(1);
808 	} else if (is_vht(rate)) {
809 		if (index < IWL_FIRST_VHT_RATE || index > IWL_LAST_VHT_RATE) {
810 			IWL_ERR(mvm, "Invalid VHT rate index %d\n", index);
811 			index = IWL_LAST_VHT_RATE;
812 		}
813 		ucode_rate |= RATE_MCS_VHT_MSK_V1;
814 		if (is_vht_siso(rate))
815 			ucode_rate |= iwl_rates[index].plcp_vht_siso;
816 		else if (is_vht_mimo2(rate))
817 			ucode_rate |= iwl_rates[index].plcp_vht_mimo2;
818 		else
819 			WARN_ON_ONCE(1);
820 
821 	} else {
822 		IWL_ERR(mvm, "Invalid rate->type %d\n", rate->type);
823 	}
824 
825 	if (is_siso(rate) && rate->stbc) {
826 		/* To enable STBC we need to set both a flag and ANT_AB */
827 		ucode_rate |= RATE_MCS_ANT_AB_MSK;
828 		ucode_rate |= RATE_MCS_STBC_MSK;
829 	}
830 
831 	ucode_rate |= rate->bw;
832 	if (rate->sgi)
833 		ucode_rate |= RATE_MCS_SGI_MSK_V1;
834 	if (rate->ldpc)
835 		ucode_rate |= RATE_MCS_LDPC_MSK_V1;
836 
837 	return ucode_rate;
838 }
839 
840 /* Convert a ucode rate into an rs_rate object */
rs_rate_from_ucode_rate(const u32 ucode_rate,enum nl80211_band band,struct rs_rate * rate)841 static int rs_rate_from_ucode_rate(const u32 ucode_rate,
842 				   enum nl80211_band band,
843 				   struct rs_rate *rate)
844 {
845 	u32 ant_msk = ucode_rate & RATE_MCS_ANT_AB_MSK;
846 	u8 num_of_ant = get_num_of_ant_from_rate(ucode_rate);
847 	u8 nss;
848 
849 	memset(rate, 0, sizeof(*rate));
850 	rate->index = iwl_hwrate_to_plcp_idx(ucode_rate);
851 
852 	if (rate->index == IWL_RATE_INVALID)
853 		return -EINVAL;
854 
855 	rate->ant = (ant_msk >> RATE_MCS_ANT_POS);
856 
857 	/* Legacy */
858 	if (!(ucode_rate & RATE_MCS_HT_MSK_V1) &&
859 	    !(ucode_rate & RATE_MCS_VHT_MSK_V1) &&
860 	    !(ucode_rate & RATE_MCS_HE_MSK_V1)) {
861 		if (num_of_ant == 1) {
862 			if (band == NL80211_BAND_5GHZ)
863 				rate->type = LQ_LEGACY_A;
864 			else
865 				rate->type = LQ_LEGACY_G;
866 		}
867 
868 		return 0;
869 	}
870 
871 	/* HT, VHT or HE */
872 	if (ucode_rate & RATE_MCS_SGI_MSK_V1)
873 		rate->sgi = true;
874 	if (ucode_rate & RATE_MCS_LDPC_MSK_V1)
875 		rate->ldpc = true;
876 	if (ucode_rate & RATE_MCS_STBC_MSK)
877 		rate->stbc = true;
878 	if (ucode_rate & RATE_MCS_BF_MSK)
879 		rate->bfer = true;
880 
881 	rate->bw = ucode_rate & RATE_MCS_CHAN_WIDTH_MSK_V1;
882 
883 	if (ucode_rate & RATE_MCS_HT_MSK_V1) {
884 		nss = ((ucode_rate & RATE_HT_MCS_NSS_MSK_V1) >>
885 		       RATE_HT_MCS_NSS_POS_V1) + 1;
886 
887 		if (nss == 1) {
888 			rate->type = LQ_HT_SISO;
889 			WARN_ONCE(!rate->stbc && !rate->bfer && num_of_ant != 1,
890 				  "stbc %d bfer %d",
891 				  rate->stbc, rate->bfer);
892 		} else if (nss == 2) {
893 			rate->type = LQ_HT_MIMO2;
894 			WARN_ON_ONCE(num_of_ant != 2);
895 		} else {
896 			WARN_ON_ONCE(1);
897 		}
898 	} else if (ucode_rate & RATE_MCS_VHT_MSK_V1) {
899 		nss = FIELD_GET(RATE_VHT_MCS_NSS_MSK, ucode_rate) + 1;
900 
901 		if (nss == 1) {
902 			rate->type = LQ_VHT_SISO;
903 			WARN_ONCE(!rate->stbc && !rate->bfer && num_of_ant != 1,
904 				  "stbc %d bfer %d",
905 				  rate->stbc, rate->bfer);
906 		} else if (nss == 2) {
907 			rate->type = LQ_VHT_MIMO2;
908 			WARN_ON_ONCE(num_of_ant != 2);
909 		} else {
910 			WARN_ON_ONCE(1);
911 		}
912 	} else if (ucode_rate & RATE_MCS_HE_MSK_V1) {
913 		nss = FIELD_GET(RATE_VHT_MCS_NSS_MSK, ucode_rate) + 1;
914 
915 		if (nss == 1) {
916 			rate->type = LQ_HE_SISO;
917 			WARN_ONCE(!rate->stbc && !rate->bfer && num_of_ant != 1,
918 				  "stbc %d bfer %d", rate->stbc, rate->bfer);
919 		} else if (nss == 2) {
920 			rate->type = LQ_HE_MIMO2;
921 			WARN_ON_ONCE(num_of_ant != 2);
922 		} else {
923 			WARN_ON_ONCE(1);
924 		}
925 	}
926 
927 	WARN_ON_ONCE(rate->bw == RATE_MCS_CHAN_WIDTH_80 &&
928 		     !is_he(rate) && !is_vht(rate));
929 
930 	return 0;
931 }
932 
933 /* switch to another antenna/antennas and return 1 */
934 /* if no other valid antenna found, return 0 */
rs_toggle_antenna(u32 valid_ant,struct rs_rate * rate)935 static int rs_toggle_antenna(u32 valid_ant, struct rs_rate *rate)
936 {
937 	u8 new_ant_type;
938 
939 	if (!rs_is_valid_ant(valid_ant, rate->ant))
940 		return 0;
941 
942 	new_ant_type = ant_toggle_lookup[rate->ant];
943 
944 	while ((new_ant_type != rate->ant) &&
945 	       !rs_is_valid_ant(valid_ant, new_ant_type))
946 		new_ant_type = ant_toggle_lookup[new_ant_type];
947 
948 	if (new_ant_type == rate->ant)
949 		return 0;
950 
951 	rate->ant = new_ant_type;
952 
953 	return 1;
954 }
955 
rs_get_supported_rates(struct iwl_lq_sta * lq_sta,struct rs_rate * rate)956 static u16 rs_get_supported_rates(struct iwl_lq_sta *lq_sta,
957 				  struct rs_rate *rate)
958 {
959 	if (is_legacy(rate))
960 		return lq_sta->active_legacy_rate;
961 	else if (is_siso(rate))
962 		return lq_sta->active_siso_rate;
963 	else if (is_mimo2(rate))
964 		return lq_sta->active_mimo2_rate;
965 
966 	WARN_ON_ONCE(1);
967 	return 0;
968 }
969 
rs_get_adjacent_rate(struct iwl_mvm * mvm,u8 index,u16 rate_mask,int rate_type)970 static u16 rs_get_adjacent_rate(struct iwl_mvm *mvm, u8 index, u16 rate_mask,
971 				int rate_type)
972 {
973 	u8 high = IWL_RATE_INVALID;
974 	u8 low = IWL_RATE_INVALID;
975 
976 	/* 802.11A or ht walks to the next literal adjacent rate in
977 	 * the rate table */
978 	if (is_type_a_band(rate_type) || !is_type_legacy(rate_type)) {
979 		int i;
980 		u32 mask;
981 
982 		/* Find the previous rate that is in the rate mask */
983 		i = index - 1;
984 		if (i >= 0)
985 			mask = BIT(i);
986 		for (; i >= 0; i--, mask >>= 1) {
987 			if (rate_mask & mask) {
988 				low = i;
989 				break;
990 			}
991 		}
992 
993 		/* Find the next rate that is in the rate mask */
994 		i = index + 1;
995 		for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
996 			if (rate_mask & mask) {
997 				high = i;
998 				break;
999 			}
1000 		}
1001 
1002 		return (high << 8) | low;
1003 	}
1004 
1005 	low = index;
1006 	while (low != IWL_RATE_INVALID) {
1007 		low = iwl_rates[low].prev_rs;
1008 		if (low == IWL_RATE_INVALID)
1009 			break;
1010 		if (rate_mask & (1 << low))
1011 			break;
1012 	}
1013 
1014 	high = index;
1015 	while (high != IWL_RATE_INVALID) {
1016 		high = iwl_rates[high].next_rs;
1017 		if (high == IWL_RATE_INVALID)
1018 			break;
1019 		if (rate_mask & (1 << high))
1020 			break;
1021 	}
1022 
1023 	return (high << 8) | low;
1024 }
1025 
rs_rate_supported(struct iwl_lq_sta * lq_sta,struct rs_rate * rate)1026 static inline bool rs_rate_supported(struct iwl_lq_sta *lq_sta,
1027 				     struct rs_rate *rate)
1028 {
1029 	return BIT(rate->index) & rs_get_supported_rates(lq_sta, rate);
1030 }
1031 
1032 /* Get the next supported lower rate in the current column.
1033  * Return true if bottom rate in the current column was reached
1034  */
rs_get_lower_rate_in_column(struct iwl_lq_sta * lq_sta,struct rs_rate * rate)1035 static bool rs_get_lower_rate_in_column(struct iwl_lq_sta *lq_sta,
1036 					struct rs_rate *rate)
1037 {
1038 	u8 low;
1039 	u16 high_low;
1040 	u16 rate_mask;
1041 	struct iwl_mvm *mvm = lq_sta->pers.drv;
1042 
1043 	rate_mask = rs_get_supported_rates(lq_sta, rate);
1044 	high_low = rs_get_adjacent_rate(mvm, rate->index, rate_mask,
1045 					rate->type);
1046 	low = high_low & 0xff;
1047 
1048 	/* Bottom rate of column reached */
1049 	if (low == IWL_RATE_INVALID)
1050 		return true;
1051 
1052 	rate->index = low;
1053 	return false;
1054 }
1055 
1056 /* Get the next rate to use following a column downgrade */
rs_get_lower_rate_down_column(struct iwl_lq_sta * lq_sta,struct rs_rate * rate)1057 static void rs_get_lower_rate_down_column(struct iwl_lq_sta *lq_sta,
1058 					  struct rs_rate *rate)
1059 {
1060 	struct iwl_mvm *mvm = lq_sta->pers.drv;
1061 
1062 	if (is_legacy(rate)) {
1063 		/* No column to downgrade from Legacy */
1064 		return;
1065 	} else if (is_siso(rate)) {
1066 		/* Downgrade to Legacy if we were in SISO */
1067 		if (lq_sta->band == NL80211_BAND_5GHZ)
1068 			rate->type = LQ_LEGACY_A;
1069 		else
1070 			rate->type = LQ_LEGACY_G;
1071 
1072 		rate->bw = RATE_MCS_CHAN_WIDTH_20;
1073 
1074 		if (WARN_ON_ONCE(rate->index < IWL_RATE_MCS_0_INDEX))
1075 			rate->index = rs_ht_to_legacy[IWL_RATE_MCS_0_INDEX];
1076 		else if (WARN_ON_ONCE(rate->index > IWL_RATE_MCS_9_INDEX))
1077 			rate->index = rs_ht_to_legacy[IWL_RATE_MCS_9_INDEX];
1078 		else
1079 			rate->index = rs_ht_to_legacy[rate->index];
1080 
1081 		rate->ldpc = false;
1082 	} else {
1083 		/* Downgrade to SISO with same MCS if in MIMO  */
1084 		rate->type = is_vht_mimo2(rate) ?
1085 			LQ_VHT_SISO : LQ_HT_SISO;
1086 	}
1087 
1088 	if (num_of_ant(rate->ant) > 1)
1089 		rate->ant = first_antenna(iwl_mvm_get_valid_tx_ant(mvm));
1090 
1091 	/* Relevant in both switching to SISO or Legacy */
1092 	rate->sgi = false;
1093 
1094 	if (!rs_rate_supported(lq_sta, rate))
1095 		rs_get_lower_rate_in_column(lq_sta, rate);
1096 }
1097 
1098 /* Check if both rates share the same column */
rs_rate_column_match(struct rs_rate * a,struct rs_rate * b)1099 static inline bool rs_rate_column_match(struct rs_rate *a,
1100 					struct rs_rate *b)
1101 {
1102 	bool ant_match;
1103 
1104 	if (a->stbc || a->bfer)
1105 		ant_match = (b->ant == ANT_A || b->ant == ANT_B);
1106 	else
1107 		ant_match = (a->ant == b->ant);
1108 
1109 	return (a->type == b->type) && (a->bw == b->bw) && (a->sgi == b->sgi)
1110 		&& ant_match;
1111 }
1112 
rs_get_column_from_rate(struct rs_rate * rate)1113 static inline enum rs_column rs_get_column_from_rate(struct rs_rate *rate)
1114 {
1115 	if (is_legacy(rate)) {
1116 		if (rate->ant == ANT_A)
1117 			return RS_COLUMN_LEGACY_ANT_A;
1118 
1119 		if (rate->ant == ANT_B)
1120 			return RS_COLUMN_LEGACY_ANT_B;
1121 
1122 		goto err;
1123 	}
1124 
1125 	if (is_siso(rate)) {
1126 		if (rate->ant == ANT_A || rate->stbc || rate->bfer)
1127 			return rate->sgi ? RS_COLUMN_SISO_ANT_A_SGI :
1128 				RS_COLUMN_SISO_ANT_A;
1129 
1130 		if (rate->ant == ANT_B)
1131 			return rate->sgi ? RS_COLUMN_SISO_ANT_B_SGI :
1132 				RS_COLUMN_SISO_ANT_B;
1133 
1134 		goto err;
1135 	}
1136 
1137 	if (is_mimo(rate))
1138 		return rate->sgi ? RS_COLUMN_MIMO2_SGI : RS_COLUMN_MIMO2;
1139 
1140 err:
1141 	return RS_COLUMN_INVALID;
1142 }
1143 
rs_get_tid(struct ieee80211_hdr * hdr)1144 static u8 rs_get_tid(struct ieee80211_hdr *hdr)
1145 {
1146 	u8 tid = IWL_MAX_TID_COUNT;
1147 
1148 	if (ieee80211_is_data_qos(hdr->frame_control)) {
1149 		u8 *qc = ieee80211_get_qos_ctl(hdr);
1150 		tid = qc[0] & 0xf;
1151 	}
1152 
1153 	if (unlikely(tid > IWL_MAX_TID_COUNT))
1154 		tid = IWL_MAX_TID_COUNT;
1155 
1156 	return tid;
1157 }
1158 
1159 /*
1160  * mac80211 sends us Tx status
1161  */
rs_drv_mac80211_tx_status(void * mvm_r,struct ieee80211_supported_band * sband,struct ieee80211_sta * sta,void * priv_sta,struct sk_buff * skb)1162 static void rs_drv_mac80211_tx_status(void *mvm_r,
1163 				      struct ieee80211_supported_band *sband,
1164 				      struct ieee80211_sta *sta, void *priv_sta,
1165 				      struct sk_buff *skb)
1166 {
1167 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1168 	struct iwl_op_mode *op_mode = mvm_r;
1169 	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1170 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1171 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1172 
1173 	if (!mvmsta->vif)
1174 		return;
1175 
1176 	if (!ieee80211_is_data(hdr->frame_control) ||
1177 	    info->flags & IEEE80211_TX_CTL_NO_ACK)
1178 		return;
1179 
1180 	iwl_mvm_rs_tx_status(mvm, sta, rs_get_tid(hdr), info,
1181 			     ieee80211_is_qos_nullfunc(hdr->frame_control));
1182 }
1183 
1184 /*
1185  * Begin a period of staying with a selected modulation mode.
1186  * Set "stay_in_tbl" flag to prevent any mode switches.
1187  * Set frame tx success limits according to legacy vs. high-throughput,
1188  * and reset overall (spanning all rates) tx success history statistics.
1189  * These control how long we stay using same modulation mode before
1190  * searching for a new mode.
1191  */
rs_set_stay_in_table(struct iwl_mvm * mvm,u8 is_legacy,struct iwl_lq_sta * lq_sta)1192 static void rs_set_stay_in_table(struct iwl_mvm *mvm, u8 is_legacy,
1193 				 struct iwl_lq_sta *lq_sta)
1194 {
1195 	IWL_DEBUG_RATE(mvm, "Moving to RS_STATE_STAY_IN_COLUMN\n");
1196 	lq_sta->rs_state = RS_STATE_STAY_IN_COLUMN;
1197 	if (is_legacy) {
1198 		lq_sta->table_count_limit = IWL_MVM_RS_LEGACY_TABLE_COUNT;
1199 		lq_sta->max_failure_limit = IWL_MVM_RS_LEGACY_FAILURE_LIMIT;
1200 		lq_sta->max_success_limit = IWL_MVM_RS_LEGACY_SUCCESS_LIMIT;
1201 	} else {
1202 		lq_sta->table_count_limit = IWL_MVM_RS_NON_LEGACY_TABLE_COUNT;
1203 		lq_sta->max_failure_limit = IWL_MVM_RS_NON_LEGACY_FAILURE_LIMIT;
1204 		lq_sta->max_success_limit = IWL_MVM_RS_NON_LEGACY_SUCCESS_LIMIT;
1205 	}
1206 	lq_sta->table_count = 0;
1207 	lq_sta->total_failed = 0;
1208 	lq_sta->total_success = 0;
1209 	lq_sta->flush_timer = jiffies;
1210 	lq_sta->visited_columns = 0;
1211 }
1212 
rs_get_max_rate_from_mask(unsigned long rate_mask)1213 static inline int rs_get_max_rate_from_mask(unsigned long rate_mask)
1214 {
1215 	if (rate_mask)
1216 		return find_last_bit(&rate_mask, BITS_PER_LONG);
1217 	return IWL_RATE_INVALID;
1218 }
1219 
rs_get_max_allowed_rate(struct iwl_lq_sta * lq_sta,const struct rs_tx_column * column)1220 static int rs_get_max_allowed_rate(struct iwl_lq_sta *lq_sta,
1221 				   const struct rs_tx_column *column)
1222 {
1223 	switch (column->mode) {
1224 	case RS_LEGACY:
1225 		return lq_sta->max_legacy_rate_idx;
1226 	case RS_SISO:
1227 		return lq_sta->max_siso_rate_idx;
1228 	case RS_MIMO2:
1229 		return lq_sta->max_mimo2_rate_idx;
1230 	default:
1231 		WARN_ON_ONCE(1);
1232 	}
1233 
1234 	return lq_sta->max_legacy_rate_idx;
1235 }
1236 
rs_get_expected_tpt_table(struct iwl_lq_sta * lq_sta,const struct rs_tx_column * column,u32 bw)1237 static const u16 *rs_get_expected_tpt_table(struct iwl_lq_sta *lq_sta,
1238 					    const struct rs_tx_column *column,
1239 					    u32 bw)
1240 {
1241 	/* Used to choose among HT tables */
1242 	const u16 (*ht_tbl_pointer)[IWL_RATE_COUNT];
1243 
1244 	if (WARN_ON_ONCE(column->mode != RS_LEGACY &&
1245 			 column->mode != RS_SISO &&
1246 			 column->mode != RS_MIMO2))
1247 		return expected_tpt_legacy;
1248 
1249 	/* Legacy rates have only one table */
1250 	if (column->mode == RS_LEGACY)
1251 		return expected_tpt_legacy;
1252 
1253 	ht_tbl_pointer = expected_tpt_mimo2_20MHz;
1254 	/* Choose among many HT tables depending on number of streams
1255 	 * (SISO/MIMO2), channel width (20/40/80), SGI, and aggregation
1256 	 * status */
1257 	if (column->mode == RS_SISO) {
1258 		switch (bw) {
1259 		case RATE_MCS_CHAN_WIDTH_20:
1260 			ht_tbl_pointer = expected_tpt_siso_20MHz;
1261 			break;
1262 		case RATE_MCS_CHAN_WIDTH_40:
1263 			ht_tbl_pointer = expected_tpt_siso_40MHz;
1264 			break;
1265 		case RATE_MCS_CHAN_WIDTH_80:
1266 			ht_tbl_pointer = expected_tpt_siso_80MHz;
1267 			break;
1268 		case RATE_MCS_CHAN_WIDTH_160:
1269 			ht_tbl_pointer = expected_tpt_siso_160MHz;
1270 			break;
1271 		default:
1272 			WARN_ON_ONCE(1);
1273 		}
1274 	} else if (column->mode == RS_MIMO2) {
1275 		switch (bw) {
1276 		case RATE_MCS_CHAN_WIDTH_20:
1277 			ht_tbl_pointer = expected_tpt_mimo2_20MHz;
1278 			break;
1279 		case RATE_MCS_CHAN_WIDTH_40:
1280 			ht_tbl_pointer = expected_tpt_mimo2_40MHz;
1281 			break;
1282 		case RATE_MCS_CHAN_WIDTH_80:
1283 			ht_tbl_pointer = expected_tpt_mimo2_80MHz;
1284 			break;
1285 		case RATE_MCS_CHAN_WIDTH_160:
1286 			ht_tbl_pointer = expected_tpt_mimo2_160MHz;
1287 			break;
1288 		default:
1289 			WARN_ON_ONCE(1);
1290 		}
1291 	} else {
1292 		WARN_ON_ONCE(1);
1293 	}
1294 
1295 	if (!column->sgi && !lq_sta->is_agg)		/* Normal */
1296 		return ht_tbl_pointer[0];
1297 	else if (column->sgi && !lq_sta->is_agg)        /* SGI */
1298 		return ht_tbl_pointer[1];
1299 	else if (!column->sgi && lq_sta->is_agg)        /* AGG */
1300 		return ht_tbl_pointer[2];
1301 	else						/* AGG+SGI */
1302 		return ht_tbl_pointer[3];
1303 }
1304 
rs_set_expected_tpt_table(struct iwl_lq_sta * lq_sta,struct iwl_scale_tbl_info * tbl)1305 static void rs_set_expected_tpt_table(struct iwl_lq_sta *lq_sta,
1306 				      struct iwl_scale_tbl_info *tbl)
1307 {
1308 	struct rs_rate *rate = &tbl->rate;
1309 	const struct rs_tx_column *column = &rs_tx_columns[tbl->column];
1310 
1311 	tbl->expected_tpt = rs_get_expected_tpt_table(lq_sta, column, rate->bw);
1312 }
1313 
1314 /* rs uses two tables, one is active and the second is for searching better
1315  * configuration. This function, according to the index of the currently
1316  * active table returns the search table, which is located at the
1317  * index complementary to 1 according to the active table (active = 1,
1318  * search = 0 or active = 0, search = 1).
1319  * Since lq_info is an arary of size 2, make sure index cannot be out of bounds.
1320  */
rs_search_tbl(u8 active_tbl)1321 static inline u8 rs_search_tbl(u8 active_tbl)
1322 {
1323 	return (active_tbl ^ 1) & 1;
1324 }
1325 
rs_get_best_rate(struct iwl_mvm * mvm,struct iwl_lq_sta * lq_sta,struct iwl_scale_tbl_info * tbl,unsigned long rate_mask,s8 index)1326 static s32 rs_get_best_rate(struct iwl_mvm *mvm,
1327 			    struct iwl_lq_sta *lq_sta,
1328 			    struct iwl_scale_tbl_info *tbl,	/* "search" */
1329 			    unsigned long rate_mask, s8 index)
1330 {
1331 	struct iwl_scale_tbl_info *active_tbl =
1332 	    &(lq_sta->lq_info[lq_sta->active_tbl]);
1333 	s32 success_ratio = active_tbl->win[index].success_ratio;
1334 	u16 expected_current_tpt = active_tbl->expected_tpt[index];
1335 	const u16 *tpt_tbl = tbl->expected_tpt;
1336 	u16 high_low;
1337 	u32 target_tpt;
1338 	int rate_idx;
1339 
1340 	if (success_ratio >= RS_PERCENT(IWL_MVM_RS_SR_NO_DECREASE)) {
1341 		target_tpt = 100 * expected_current_tpt;
1342 		IWL_DEBUG_RATE(mvm,
1343 			       "SR %d high. Find rate exceeding EXPECTED_CURRENT %d\n",
1344 			       success_ratio, target_tpt);
1345 	} else {
1346 		target_tpt = lq_sta->last_tpt;
1347 		IWL_DEBUG_RATE(mvm,
1348 			       "SR %d not that good. Find rate exceeding ACTUAL_TPT %d\n",
1349 			       success_ratio, target_tpt);
1350 	}
1351 
1352 	rate_idx = find_first_bit(&rate_mask, BITS_PER_LONG);
1353 
1354 	while (rate_idx != IWL_RATE_INVALID) {
1355 		if (target_tpt < (100 * tpt_tbl[rate_idx]))
1356 			break;
1357 
1358 		high_low = rs_get_adjacent_rate(mvm, rate_idx, rate_mask,
1359 						tbl->rate.type);
1360 
1361 		rate_idx = (high_low >> 8) & 0xff;
1362 	}
1363 
1364 	IWL_DEBUG_RATE(mvm, "Best rate found %d target_tp %d expected_new %d\n",
1365 		       rate_idx, target_tpt,
1366 		       rate_idx != IWL_RATE_INVALID ?
1367 		       100 * tpt_tbl[rate_idx] : IWL_INVALID_VALUE);
1368 
1369 	return rate_idx;
1370 }
1371 
rs_bw_from_sta_bw(struct ieee80211_sta * sta)1372 static u32 rs_bw_from_sta_bw(struct ieee80211_sta *sta)
1373 {
1374 	struct ieee80211_sta_vht_cap *sta_vht_cap = &sta->deflink.vht_cap;
1375 	struct ieee80211_vht_cap vht_cap = {
1376 		.vht_cap_info = cpu_to_le32(sta_vht_cap->cap),
1377 		.supp_mcs = sta_vht_cap->vht_mcs,
1378 	};
1379 
1380 	switch (sta->deflink.bandwidth) {
1381 	case IEEE80211_STA_RX_BW_160:
1382 		/*
1383 		 * Don't use 160 MHz if VHT extended NSS support
1384 		 * says we cannot use 2 streams, we don't want to
1385 		 * deal with this.
1386 		 * We only check MCS 0 - they will support that if
1387 		 * we got here at all and we don't care which MCS,
1388 		 * we want to determine a more global state.
1389 		 */
1390 		if (ieee80211_get_vht_max_nss(&vht_cap,
1391 					      IEEE80211_VHT_CHANWIDTH_160MHZ,
1392 					      0, true,
1393 					      sta->deflink.rx_nss) < sta->deflink.rx_nss)
1394 			return RATE_MCS_CHAN_WIDTH_80;
1395 		return RATE_MCS_CHAN_WIDTH_160;
1396 	case IEEE80211_STA_RX_BW_80:
1397 		return RATE_MCS_CHAN_WIDTH_80;
1398 	case IEEE80211_STA_RX_BW_40:
1399 		return RATE_MCS_CHAN_WIDTH_40;
1400 	case IEEE80211_STA_RX_BW_20:
1401 	default:
1402 		return RATE_MCS_CHAN_WIDTH_20;
1403 	}
1404 }
1405 
1406 /*
1407  * Check whether we should continue using same modulation mode, or
1408  * begin search for a new mode, based on:
1409  * 1) # tx successes or failures while using this mode
1410  * 2) # times calling this function
1411  * 3) elapsed time in this mode (not used, for now)
1412  */
rs_stay_in_table(struct iwl_lq_sta * lq_sta,bool force_search)1413 static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search)
1414 {
1415 	struct iwl_scale_tbl_info *tbl;
1416 	int active_tbl;
1417 	int flush_interval_passed = 0;
1418 	struct iwl_mvm *mvm;
1419 
1420 	mvm = lq_sta->pers.drv;
1421 	active_tbl = lq_sta->active_tbl;
1422 
1423 	tbl = &(lq_sta->lq_info[active_tbl]);
1424 
1425 	/* If we've been disallowing search, see if we should now allow it */
1426 	if (lq_sta->rs_state == RS_STATE_STAY_IN_COLUMN) {
1427 		/* Elapsed time using current modulation mode */
1428 		if (lq_sta->flush_timer)
1429 			flush_interval_passed =
1430 				time_after(jiffies,
1431 					   (unsigned long)(lq_sta->flush_timer +
1432 							   (IWL_MVM_RS_STAY_IN_COLUMN_TIMEOUT * HZ)));
1433 
1434 		/*
1435 		 * Check if we should allow search for new modulation mode.
1436 		 * If many frames have failed or succeeded, or we've used
1437 		 * this same modulation for a long time, allow search, and
1438 		 * reset history stats that keep track of whether we should
1439 		 * allow a new search.  Also (below) reset all bitmaps and
1440 		 * stats in active history.
1441 		 */
1442 		if (force_search ||
1443 		    (lq_sta->total_failed > lq_sta->max_failure_limit) ||
1444 		    (lq_sta->total_success > lq_sta->max_success_limit) ||
1445 		    ((!lq_sta->search_better_tbl) &&
1446 		     (lq_sta->flush_timer) && (flush_interval_passed))) {
1447 			IWL_DEBUG_RATE(mvm,
1448 				       "LQ: stay is expired %d %d %d\n",
1449 				     lq_sta->total_failed,
1450 				     lq_sta->total_success,
1451 				     flush_interval_passed);
1452 
1453 			/* Allow search for new mode */
1454 			lq_sta->rs_state = RS_STATE_SEARCH_CYCLE_STARTED;
1455 			IWL_DEBUG_RATE(mvm,
1456 				       "Moving to RS_STATE_SEARCH_CYCLE_STARTED\n");
1457 			lq_sta->total_failed = 0;
1458 			lq_sta->total_success = 0;
1459 			lq_sta->flush_timer = 0;
1460 			/* mark the current column as visited */
1461 			lq_sta->visited_columns = BIT(tbl->column);
1462 		/*
1463 		 * Else if we've used this modulation mode enough repetitions
1464 		 * (regardless of elapsed time or success/failure), reset
1465 		 * history bitmaps and rate-specific stats for all rates in
1466 		 * active table.
1467 		 */
1468 		} else {
1469 			lq_sta->table_count++;
1470 			if (lq_sta->table_count >=
1471 			    lq_sta->table_count_limit) {
1472 				lq_sta->table_count = 0;
1473 
1474 				IWL_DEBUG_RATE(mvm,
1475 					       "LQ: stay in table clear win\n");
1476 				rs_rate_scale_clear_tbl_windows(mvm, tbl);
1477 			}
1478 		}
1479 
1480 		/* If transitioning to allow "search", reset all history
1481 		 * bitmaps and stats in active table (this will become the new
1482 		 * "search" table). */
1483 		if (lq_sta->rs_state == RS_STATE_SEARCH_CYCLE_STARTED) {
1484 			rs_rate_scale_clear_tbl_windows(mvm, tbl);
1485 		}
1486 	}
1487 }
1488 
rs_set_amsdu_len(struct iwl_mvm * mvm,struct ieee80211_sta * sta,struct iwl_scale_tbl_info * tbl,enum rs_action scale_action)1489 static void rs_set_amsdu_len(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
1490 			     struct iwl_scale_tbl_info *tbl,
1491 			     enum rs_action scale_action)
1492 {
1493 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1494 	struct ieee80211_bss_conf *bss_conf = &mvmsta->vif->bss_conf;
1495 	int i;
1496 
1497 	sta->deflink.agg.max_amsdu_len =
1498 		rs_fw_get_max_amsdu_len(sta, bss_conf, &sta->deflink);
1499 
1500 	/*
1501 	 * In case TLC offload is not active amsdu_enabled is either 0xFFFF
1502 	 * or 0, since there is no per-TID alg.
1503 	 */
1504 	if ((!is_vht(&tbl->rate) && !is_ht(&tbl->rate)) ||
1505 	    tbl->rate.index < IWL_RATE_MCS_5_INDEX ||
1506 	    scale_action == RS_ACTION_DOWNSCALE)
1507 		mvmsta->amsdu_enabled = 0;
1508 	else
1509 		mvmsta->amsdu_enabled = 0xFFFF;
1510 
1511 	if (bss_conf->he_support &&
1512 	    !iwlwifi_mod_params.disable_11ax)
1513 		mvmsta->max_amsdu_len = sta->deflink.agg.max_amsdu_len;
1514 	else
1515 		mvmsta->max_amsdu_len =
1516 			min_t(int, sta->deflink.agg.max_amsdu_len, 8500);
1517 
1518 	sta->deflink.agg.max_rc_amsdu_len = mvmsta->max_amsdu_len;
1519 
1520 	for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
1521 		if (mvmsta->amsdu_enabled)
1522 			sta->deflink.agg.max_tid_amsdu_len[i] =
1523 				iwl_mvm_max_amsdu_size(mvm, sta, i);
1524 		else
1525 			/*
1526 			 * Not so elegant, but this will effectively
1527 			 * prevent AMSDU on this TID
1528 			 */
1529 			sta->deflink.agg.max_tid_amsdu_len[i] = 1;
1530 	}
1531 }
1532 
1533 /*
1534  * setup rate table in uCode
1535  */
rs_update_rate_tbl(struct iwl_mvm * mvm,struct ieee80211_sta * sta,struct iwl_lq_sta * lq_sta,struct iwl_scale_tbl_info * tbl)1536 static void rs_update_rate_tbl(struct iwl_mvm *mvm,
1537 			       struct ieee80211_sta *sta,
1538 			       struct iwl_lq_sta *lq_sta,
1539 			       struct iwl_scale_tbl_info *tbl)
1540 {
1541 	rs_fill_lq_cmd(mvm, sta, lq_sta, &tbl->rate);
1542 	iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq);
1543 }
1544 
rs_tweak_rate_tbl(struct iwl_mvm * mvm,struct ieee80211_sta * sta,struct iwl_lq_sta * lq_sta,struct iwl_scale_tbl_info * tbl,enum rs_action scale_action)1545 static bool rs_tweak_rate_tbl(struct iwl_mvm *mvm,
1546 			      struct ieee80211_sta *sta,
1547 			      struct iwl_lq_sta *lq_sta,
1548 			      struct iwl_scale_tbl_info *tbl,
1549 			      enum rs_action scale_action)
1550 {
1551 	if (rs_bw_from_sta_bw(sta) != RATE_MCS_CHAN_WIDTH_80)
1552 		return false;
1553 
1554 	if (!is_vht_siso(&tbl->rate))
1555 		return false;
1556 
1557 	if ((tbl->rate.bw == RATE_MCS_CHAN_WIDTH_80) &&
1558 	    (tbl->rate.index == IWL_RATE_MCS_0_INDEX) &&
1559 	    (scale_action == RS_ACTION_DOWNSCALE)) {
1560 		tbl->rate.bw = RATE_MCS_CHAN_WIDTH_20;
1561 		tbl->rate.index = IWL_RATE_MCS_4_INDEX;
1562 		IWL_DEBUG_RATE(mvm, "Switch 80Mhz SISO MCS0 -> 20Mhz MCS4\n");
1563 		goto tweaked;
1564 	}
1565 
1566 	/* Go back to 80Mhz MCS1 only if we've established that 20Mhz MCS5 is
1567 	 * sustainable, i.e. we're past the test window. We can't go back
1568 	 * if MCS5 is just tested as this will happen always after switching
1569 	 * to 20Mhz MCS4 because the rate stats are cleared.
1570 	 */
1571 	if ((tbl->rate.bw == RATE_MCS_CHAN_WIDTH_20) &&
1572 	    (((tbl->rate.index == IWL_RATE_MCS_5_INDEX) &&
1573 	     (scale_action == RS_ACTION_STAY)) ||
1574 	     ((tbl->rate.index > IWL_RATE_MCS_5_INDEX) &&
1575 	      (scale_action == RS_ACTION_UPSCALE)))) {
1576 		tbl->rate.bw = RATE_MCS_CHAN_WIDTH_80;
1577 		tbl->rate.index = IWL_RATE_MCS_1_INDEX;
1578 		IWL_DEBUG_RATE(mvm, "Switch 20Mhz SISO MCS5 -> 80Mhz MCS1\n");
1579 		goto tweaked;
1580 	}
1581 
1582 	return false;
1583 
1584 tweaked:
1585 	rs_set_expected_tpt_table(lq_sta, tbl);
1586 	rs_rate_scale_clear_tbl_windows(mvm, tbl);
1587 	return true;
1588 }
1589 
rs_get_next_column(struct iwl_mvm * mvm,struct iwl_lq_sta * lq_sta,struct ieee80211_sta * sta,struct iwl_scale_tbl_info * tbl)1590 static enum rs_column rs_get_next_column(struct iwl_mvm *mvm,
1591 					 struct iwl_lq_sta *lq_sta,
1592 					 struct ieee80211_sta *sta,
1593 					 struct iwl_scale_tbl_info *tbl)
1594 {
1595 	int i, j, max_rate;
1596 	enum rs_column next_col_id;
1597 	const struct rs_tx_column *curr_col = &rs_tx_columns[tbl->column];
1598 	const struct rs_tx_column *next_col;
1599 	allow_column_func_t allow_func;
1600 	u8 valid_ants = iwl_mvm_get_valid_tx_ant(mvm);
1601 	const u16 *expected_tpt_tbl;
1602 	u16 tpt, max_expected_tpt;
1603 
1604 	for (i = 0; i < MAX_NEXT_COLUMNS; i++) {
1605 		next_col_id = curr_col->next_columns[i];
1606 
1607 		if (next_col_id == RS_COLUMN_INVALID)
1608 			continue;
1609 
1610 		if (lq_sta->visited_columns & BIT(next_col_id)) {
1611 			IWL_DEBUG_RATE(mvm, "Skip already visited column %d\n",
1612 				       next_col_id);
1613 			continue;
1614 		}
1615 
1616 		next_col = &rs_tx_columns[next_col_id];
1617 
1618 		if (!rs_is_valid_ant(valid_ants, next_col->ant)) {
1619 			IWL_DEBUG_RATE(mvm,
1620 				       "Skip column %d as ANT config isn't supported by chip. valid_ants 0x%x column ant 0x%x\n",
1621 				       next_col_id, valid_ants, next_col->ant);
1622 			continue;
1623 		}
1624 
1625 		for (j = 0; j < MAX_COLUMN_CHECKS; j++) {
1626 			allow_func = next_col->checks[j];
1627 			if (allow_func && !allow_func(mvm, sta, &tbl->rate,
1628 						      next_col))
1629 				break;
1630 		}
1631 
1632 		if (j != MAX_COLUMN_CHECKS) {
1633 			IWL_DEBUG_RATE(mvm,
1634 				       "Skip column %d: not allowed (check %d failed)\n",
1635 				       next_col_id, j);
1636 
1637 			continue;
1638 		}
1639 
1640 		tpt = lq_sta->last_tpt / 100;
1641 		expected_tpt_tbl = rs_get_expected_tpt_table(lq_sta, next_col,
1642 						     rs_bw_from_sta_bw(sta));
1643 		if (WARN_ON_ONCE(!expected_tpt_tbl))
1644 			continue;
1645 
1646 		max_rate = rs_get_max_allowed_rate(lq_sta, next_col);
1647 		if (max_rate == IWL_RATE_INVALID) {
1648 			IWL_DEBUG_RATE(mvm,
1649 				       "Skip column %d: no rate is allowed in this column\n",
1650 				       next_col_id);
1651 			continue;
1652 		}
1653 
1654 		max_expected_tpt = expected_tpt_tbl[max_rate];
1655 		if (tpt >= max_expected_tpt) {
1656 			IWL_DEBUG_RATE(mvm,
1657 				       "Skip column %d: can't beat current TPT. Max expected %d current %d\n",
1658 				       next_col_id, max_expected_tpt, tpt);
1659 			continue;
1660 		}
1661 
1662 		IWL_DEBUG_RATE(mvm,
1663 			       "Found potential column %d. Max expected %d current %d\n",
1664 			       next_col_id, max_expected_tpt, tpt);
1665 		break;
1666 	}
1667 
1668 	if (i == MAX_NEXT_COLUMNS)
1669 		return RS_COLUMN_INVALID;
1670 
1671 	return next_col_id;
1672 }
1673 
rs_switch_to_column(struct iwl_mvm * mvm,struct iwl_lq_sta * lq_sta,struct ieee80211_sta * sta,enum rs_column col_id)1674 static int rs_switch_to_column(struct iwl_mvm *mvm,
1675 			       struct iwl_lq_sta *lq_sta,
1676 			       struct ieee80211_sta *sta,
1677 			       enum rs_column col_id)
1678 {
1679 	struct iwl_scale_tbl_info *tbl = &lq_sta->lq_info[lq_sta->active_tbl];
1680 	struct iwl_scale_tbl_info *search_tbl =
1681 		&lq_sta->lq_info[rs_search_tbl(lq_sta->active_tbl)];
1682 	struct rs_rate *rate = &search_tbl->rate;
1683 	const struct rs_tx_column *column = &rs_tx_columns[col_id];
1684 	const struct rs_tx_column *curr_column = &rs_tx_columns[tbl->column];
1685 	unsigned long rate_mask = 0;
1686 	u32 rate_idx = 0;
1687 
1688 	memcpy(search_tbl, tbl, offsetof(struct iwl_scale_tbl_info, win));
1689 
1690 	rate->sgi = column->sgi;
1691 	rate->ant = column->ant;
1692 
1693 	if (column->mode == RS_LEGACY) {
1694 		if (lq_sta->band == NL80211_BAND_5GHZ)
1695 			rate->type = LQ_LEGACY_A;
1696 		else
1697 			rate->type = LQ_LEGACY_G;
1698 
1699 		rate->bw = RATE_MCS_CHAN_WIDTH_20;
1700 		rate->ldpc = false;
1701 		rate_mask = lq_sta->active_legacy_rate;
1702 	} else if (column->mode == RS_SISO) {
1703 		rate->type = lq_sta->is_vht ? LQ_VHT_SISO : LQ_HT_SISO;
1704 		rate_mask = lq_sta->active_siso_rate;
1705 	} else if (column->mode == RS_MIMO2) {
1706 		rate->type = lq_sta->is_vht ? LQ_VHT_MIMO2 : LQ_HT_MIMO2;
1707 		rate_mask = lq_sta->active_mimo2_rate;
1708 	} else {
1709 		WARN_ONCE(1, "Bad column mode");
1710 	}
1711 
1712 	if (column->mode != RS_LEGACY) {
1713 		rate->bw = rs_bw_from_sta_bw(sta);
1714 		rate->ldpc = lq_sta->ldpc;
1715 	}
1716 
1717 	search_tbl->column = col_id;
1718 	rs_set_expected_tpt_table(lq_sta, search_tbl);
1719 
1720 	lq_sta->visited_columns |= BIT(col_id);
1721 
1722 	/* Get the best matching rate if we're changing modes. e.g.
1723 	 * SISO->MIMO, LEGACY->SISO, MIMO->SISO
1724 	 */
1725 	if (curr_column->mode != column->mode) {
1726 		rate_idx = rs_get_best_rate(mvm, lq_sta, search_tbl,
1727 					    rate_mask, rate->index);
1728 
1729 		if ((rate_idx == IWL_RATE_INVALID) ||
1730 		    !(BIT(rate_idx) & rate_mask)) {
1731 			IWL_DEBUG_RATE(mvm,
1732 				       "can not switch with index %d"
1733 				       " rate mask %lx\n",
1734 				       rate_idx, rate_mask);
1735 
1736 			goto err;
1737 		}
1738 
1739 		rate->index = rate_idx;
1740 	}
1741 
1742 	IWL_DEBUG_RATE(mvm, "Switched to column %d: Index %d\n",
1743 		       col_id, rate->index);
1744 
1745 	return 0;
1746 
1747 err:
1748 	rate->type = LQ_NONE;
1749 	return -1;
1750 }
1751 
rs_get_rate_action(struct iwl_mvm * mvm,struct iwl_scale_tbl_info * tbl,s32 sr,int low,int high,int current_tpt,int low_tpt,int high_tpt)1752 static enum rs_action rs_get_rate_action(struct iwl_mvm *mvm,
1753 					 struct iwl_scale_tbl_info *tbl,
1754 					 s32 sr, int low, int high,
1755 					 int current_tpt,
1756 					 int low_tpt, int high_tpt)
1757 {
1758 	enum rs_action action = RS_ACTION_STAY;
1759 
1760 	if ((sr <= RS_PERCENT(IWL_MVM_RS_SR_FORCE_DECREASE)) ||
1761 	    (current_tpt == 0)) {
1762 		IWL_DEBUG_RATE(mvm,
1763 			       "Decrease rate because of low SR\n");
1764 		return RS_ACTION_DOWNSCALE;
1765 	}
1766 
1767 	if ((low_tpt == IWL_INVALID_VALUE) &&
1768 	    (high_tpt == IWL_INVALID_VALUE) &&
1769 	    (high != IWL_RATE_INVALID)) {
1770 		IWL_DEBUG_RATE(mvm,
1771 			       "No data about high/low rates. Increase rate\n");
1772 		return RS_ACTION_UPSCALE;
1773 	}
1774 
1775 	if ((high_tpt == IWL_INVALID_VALUE) &&
1776 	    (high != IWL_RATE_INVALID) &&
1777 	    (low_tpt != IWL_INVALID_VALUE) &&
1778 	    (low_tpt < current_tpt)) {
1779 		IWL_DEBUG_RATE(mvm,
1780 			       "No data about high rate and low rate is worse. Increase rate\n");
1781 		return RS_ACTION_UPSCALE;
1782 	}
1783 
1784 	if ((high_tpt != IWL_INVALID_VALUE) &&
1785 	    (high_tpt > current_tpt)) {
1786 		IWL_DEBUG_RATE(mvm,
1787 			       "Higher rate is better. Increase rate\n");
1788 		return RS_ACTION_UPSCALE;
1789 	}
1790 
1791 	if ((low_tpt != IWL_INVALID_VALUE) &&
1792 	    (high_tpt != IWL_INVALID_VALUE) &&
1793 	    (low_tpt < current_tpt) &&
1794 	    (high_tpt < current_tpt)) {
1795 		IWL_DEBUG_RATE(mvm,
1796 			       "Both high and low are worse. Maintain rate\n");
1797 		return RS_ACTION_STAY;
1798 	}
1799 
1800 	if ((low_tpt != IWL_INVALID_VALUE) &&
1801 	    (low_tpt > current_tpt)) {
1802 		IWL_DEBUG_RATE(mvm,
1803 			       "Lower rate is better\n");
1804 		action = RS_ACTION_DOWNSCALE;
1805 		goto out;
1806 	}
1807 
1808 	if ((low_tpt == IWL_INVALID_VALUE) &&
1809 	    (low != IWL_RATE_INVALID)) {
1810 		IWL_DEBUG_RATE(mvm,
1811 			       "No data about lower rate\n");
1812 		action = RS_ACTION_DOWNSCALE;
1813 		goto out;
1814 	}
1815 
1816 	IWL_DEBUG_RATE(mvm, "Maintain rate\n");
1817 
1818 out:
1819 	if ((action == RS_ACTION_DOWNSCALE) && (low != IWL_RATE_INVALID)) {
1820 		if (sr >= RS_PERCENT(IWL_MVM_RS_SR_NO_DECREASE)) {
1821 			IWL_DEBUG_RATE(mvm,
1822 				       "SR is above NO DECREASE. Avoid downscale\n");
1823 			action = RS_ACTION_STAY;
1824 		} else if (current_tpt > (100 * tbl->expected_tpt[low])) {
1825 			IWL_DEBUG_RATE(mvm,
1826 				       "Current TPT is higher than max expected in low rate. Avoid downscale\n");
1827 			action = RS_ACTION_STAY;
1828 		} else {
1829 			IWL_DEBUG_RATE(mvm, "Decrease rate\n");
1830 		}
1831 	}
1832 
1833 	return action;
1834 }
1835 
rs_stbc_allow(struct iwl_mvm * mvm,struct ieee80211_sta * sta,struct iwl_lq_sta * lq_sta)1836 static bool rs_stbc_allow(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
1837 			  struct iwl_lq_sta *lq_sta)
1838 {
1839 	/* Our chip supports Tx STBC and the peer is an HT/VHT STA which
1840 	 * supports STBC of at least 1*SS
1841 	 */
1842 	if (!lq_sta->stbc_capable)
1843 		return false;
1844 
1845 	if (!iwl_mvm_bt_coex_is_mimo_allowed(mvm, sta))
1846 		return false;
1847 
1848 	return true;
1849 }
1850 
rs_get_adjacent_txp(struct iwl_mvm * mvm,int index,int * weaker,int * stronger)1851 static void rs_get_adjacent_txp(struct iwl_mvm *mvm, int index,
1852 				int *weaker, int *stronger)
1853 {
1854 	*weaker = index + IWL_MVM_RS_TPC_TX_POWER_STEP;
1855 	if (*weaker > TPC_MAX_REDUCTION)
1856 		*weaker = TPC_INVALID;
1857 
1858 	*stronger = index - IWL_MVM_RS_TPC_TX_POWER_STEP;
1859 	if (*stronger < 0)
1860 		*stronger = TPC_INVALID;
1861 }
1862 
rs_tpc_allowed(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct rs_rate * rate,enum nl80211_band band)1863 static bool rs_tpc_allowed(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1864 			   struct rs_rate *rate, enum nl80211_band band)
1865 {
1866 	int index = rate->index;
1867 	bool cam = (iwlmvm_mod_params.power_scheme == IWL_POWER_SCHEME_CAM);
1868 	bool sta_ps_disabled = (vif->type == NL80211_IFTYPE_STATION &&
1869 				!vif->cfg.ps);
1870 
1871 	IWL_DEBUG_RATE(mvm, "cam: %d sta_ps_disabled %d\n",
1872 		       cam, sta_ps_disabled);
1873 	/*
1874 	 * allow tpc only if power management is enabled, or bt coex
1875 	 * activity grade allows it and we are on 2.4Ghz.
1876 	 */
1877 	if ((cam || sta_ps_disabled) &&
1878 	    !iwl_mvm_bt_coex_is_tpc_allowed(mvm, band))
1879 		return false;
1880 
1881 	IWL_DEBUG_RATE(mvm, "check rate, table type: %d\n", rate->type);
1882 	if (is_legacy(rate))
1883 		return index == IWL_RATE_54M_INDEX;
1884 	if (is_ht(rate))
1885 		return index == IWL_RATE_MCS_7_INDEX;
1886 	if (is_vht(rate))
1887 		return index == IWL_RATE_MCS_9_INDEX;
1888 
1889 	WARN_ON_ONCE(1);
1890 	return false;
1891 }
1892 
1893 enum tpc_action {
1894 	TPC_ACTION_STAY,
1895 	TPC_ACTION_DECREASE,
1896 	TPC_ACTION_INCREASE,
1897 	TPC_ACTION_NO_RESTIRCTION,
1898 };
1899 
rs_get_tpc_action(struct iwl_mvm * mvm,s32 sr,int weak,int strong,int current_tpt,int weak_tpt,int strong_tpt)1900 static enum tpc_action rs_get_tpc_action(struct iwl_mvm *mvm,
1901 					 s32 sr, int weak, int strong,
1902 					 int current_tpt,
1903 					 int weak_tpt, int strong_tpt)
1904 {
1905 	/* stay until we have valid tpt */
1906 	if (current_tpt == IWL_INVALID_VALUE) {
1907 		IWL_DEBUG_RATE(mvm, "no current tpt. stay.\n");
1908 		return TPC_ACTION_STAY;
1909 	}
1910 
1911 	/* Too many failures, increase txp */
1912 	if (sr <= RS_PERCENT(IWL_MVM_RS_TPC_SR_FORCE_INCREASE) ||
1913 	    current_tpt == 0) {
1914 		IWL_DEBUG_RATE(mvm, "increase txp because of weak SR\n");
1915 		return TPC_ACTION_NO_RESTIRCTION;
1916 	}
1917 
1918 	/* try decreasing first if applicable */
1919 	if (sr >= RS_PERCENT(IWL_MVM_RS_TPC_SR_NO_INCREASE) &&
1920 	    weak != TPC_INVALID) {
1921 		if (weak_tpt == IWL_INVALID_VALUE &&
1922 		    (strong_tpt == IWL_INVALID_VALUE ||
1923 		     current_tpt >= strong_tpt)) {
1924 			IWL_DEBUG_RATE(mvm,
1925 				       "no weak txp measurement. decrease txp\n");
1926 			return TPC_ACTION_DECREASE;
1927 		}
1928 
1929 		if (weak_tpt > current_tpt) {
1930 			IWL_DEBUG_RATE(mvm,
1931 				       "lower txp has better tpt. decrease txp\n");
1932 			return TPC_ACTION_DECREASE;
1933 		}
1934 	}
1935 
1936 	/* next, increase if needed */
1937 	if (sr < RS_PERCENT(IWL_MVM_RS_TPC_SR_NO_INCREASE) &&
1938 	    strong != TPC_INVALID) {
1939 		if (weak_tpt == IWL_INVALID_VALUE &&
1940 		    strong_tpt != IWL_INVALID_VALUE &&
1941 		    current_tpt < strong_tpt) {
1942 			IWL_DEBUG_RATE(mvm,
1943 				       "higher txp has better tpt. increase txp\n");
1944 			return TPC_ACTION_INCREASE;
1945 		}
1946 
1947 		if (weak_tpt < current_tpt &&
1948 		    (strong_tpt == IWL_INVALID_VALUE ||
1949 		     strong_tpt > current_tpt)) {
1950 			IWL_DEBUG_RATE(mvm,
1951 				       "lower txp has worse tpt. increase txp\n");
1952 			return TPC_ACTION_INCREASE;
1953 		}
1954 	}
1955 
1956 	IWL_DEBUG_RATE(mvm, "no need to increase or decrease txp - stay\n");
1957 	return TPC_ACTION_STAY;
1958 }
1959 
rs_tpc_perform(struct iwl_mvm * mvm,struct ieee80211_sta * sta,struct iwl_lq_sta * lq_sta,struct iwl_scale_tbl_info * tbl)1960 static bool rs_tpc_perform(struct iwl_mvm *mvm,
1961 			   struct ieee80211_sta *sta,
1962 			   struct iwl_lq_sta *lq_sta,
1963 			   struct iwl_scale_tbl_info *tbl)
1964 {
1965 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1966 	struct ieee80211_vif *vif = mvm_sta->vif;
1967 	struct ieee80211_chanctx_conf *chanctx_conf;
1968 	enum nl80211_band band;
1969 	struct iwl_rate_scale_data *window;
1970 	struct rs_rate *rate = &tbl->rate;
1971 	enum tpc_action action;
1972 	s32 sr;
1973 	u8 cur = lq_sta->lq.reduced_tpc;
1974 	int current_tpt;
1975 	int weak, strong;
1976 	int weak_tpt = IWL_INVALID_VALUE, strong_tpt = IWL_INVALID_VALUE;
1977 
1978 #ifdef CONFIG_MAC80211_DEBUGFS
1979 	if (lq_sta->pers.dbg_fixed_txp_reduction <= TPC_MAX_REDUCTION) {
1980 		IWL_DEBUG_RATE(mvm, "fixed tpc: %d\n",
1981 			       lq_sta->pers.dbg_fixed_txp_reduction);
1982 		lq_sta->lq.reduced_tpc = lq_sta->pers.dbg_fixed_txp_reduction;
1983 		return cur != lq_sta->pers.dbg_fixed_txp_reduction;
1984 	}
1985 #endif
1986 
1987 	rcu_read_lock();
1988 	chanctx_conf = rcu_dereference(vif->bss_conf.chanctx_conf);
1989 	if (WARN_ON(!chanctx_conf))
1990 		band = NUM_NL80211_BANDS;
1991 	else
1992 		band = chanctx_conf->def.chan->band;
1993 	rcu_read_unlock();
1994 
1995 	if (!rs_tpc_allowed(mvm, vif, rate, band)) {
1996 		IWL_DEBUG_RATE(mvm,
1997 			       "tpc is not allowed. remove txp restrictions\n");
1998 		lq_sta->lq.reduced_tpc = TPC_NO_REDUCTION;
1999 		return cur != TPC_NO_REDUCTION;
2000 	}
2001 
2002 	rs_get_adjacent_txp(mvm, cur, &weak, &strong);
2003 
2004 	/* Collect measured throughputs for current and adjacent rates */
2005 	window = tbl->tpc_win;
2006 	sr = window[cur].success_ratio;
2007 	current_tpt = window[cur].average_tpt;
2008 	if (weak != TPC_INVALID)
2009 		weak_tpt = window[weak].average_tpt;
2010 	if (strong != TPC_INVALID)
2011 		strong_tpt = window[strong].average_tpt;
2012 
2013 	IWL_DEBUG_RATE(mvm,
2014 		       "(TPC: %d): cur_tpt %d SR %d weak %d strong %d weak_tpt %d strong_tpt %d\n",
2015 		       cur, current_tpt, sr, weak, strong,
2016 		       weak_tpt, strong_tpt);
2017 
2018 	action = rs_get_tpc_action(mvm, sr, weak, strong,
2019 				   current_tpt, weak_tpt, strong_tpt);
2020 
2021 	/* override actions if we are on the edge */
2022 	if (weak == TPC_INVALID && action == TPC_ACTION_DECREASE) {
2023 		IWL_DEBUG_RATE(mvm, "already in lowest txp, stay\n");
2024 		action = TPC_ACTION_STAY;
2025 	} else if (strong == TPC_INVALID &&
2026 		   (action == TPC_ACTION_INCREASE ||
2027 		    action == TPC_ACTION_NO_RESTIRCTION)) {
2028 		IWL_DEBUG_RATE(mvm, "already in highest txp, stay\n");
2029 		action = TPC_ACTION_STAY;
2030 	}
2031 
2032 	switch (action) {
2033 	case TPC_ACTION_DECREASE:
2034 		lq_sta->lq.reduced_tpc = weak;
2035 		return true;
2036 	case TPC_ACTION_INCREASE:
2037 		lq_sta->lq.reduced_tpc = strong;
2038 		return true;
2039 	case TPC_ACTION_NO_RESTIRCTION:
2040 		lq_sta->lq.reduced_tpc = TPC_NO_REDUCTION;
2041 		return true;
2042 	case TPC_ACTION_STAY:
2043 		/* do nothing */
2044 		break;
2045 	}
2046 	return false;
2047 }
2048 
2049 /*
2050  * Do rate scaling and search for new modulation mode.
2051  */
rs_rate_scale_perform(struct iwl_mvm * mvm,struct ieee80211_sta * sta,struct iwl_lq_sta * lq_sta,int tid,bool ndp)2052 static void rs_rate_scale_perform(struct iwl_mvm *mvm,
2053 				  struct ieee80211_sta *sta,
2054 				  struct iwl_lq_sta *lq_sta,
2055 				  int tid, bool ndp)
2056 {
2057 	int low = IWL_RATE_INVALID;
2058 	int high = IWL_RATE_INVALID;
2059 	int index;
2060 	struct iwl_rate_scale_data *window = NULL;
2061 	int current_tpt = IWL_INVALID_VALUE;
2062 	int low_tpt = IWL_INVALID_VALUE;
2063 	int high_tpt = IWL_INVALID_VALUE;
2064 	u32 fail_count;
2065 	enum rs_action scale_action = RS_ACTION_STAY;
2066 	u16 rate_mask;
2067 	u8 update_lq = 0;
2068 	struct iwl_scale_tbl_info *tbl, *tbl1;
2069 	u8 active_tbl = 0;
2070 	u8 done_search = 0;
2071 	u16 high_low;
2072 	s32 sr;
2073 	u8 prev_agg = lq_sta->is_agg;
2074 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2075 	struct rs_rate *rate;
2076 
2077 	lq_sta->is_agg = !!mvmsta->agg_tids;
2078 
2079 	/*
2080 	 * Select rate-scale / modulation-mode table to work with in
2081 	 * the rest of this function:  "search" if searching for better
2082 	 * modulation mode, or "active" if doing rate scaling within a mode.
2083 	 */
2084 	if (!lq_sta->search_better_tbl)
2085 		active_tbl = lq_sta->active_tbl;
2086 	else
2087 		active_tbl = rs_search_tbl(lq_sta->active_tbl);
2088 
2089 	tbl = &(lq_sta->lq_info[active_tbl]);
2090 	rate = &tbl->rate;
2091 
2092 	if (prev_agg != lq_sta->is_agg) {
2093 		IWL_DEBUG_RATE(mvm,
2094 			       "Aggregation changed: prev %d current %d. Update expected TPT table\n",
2095 			       prev_agg, lq_sta->is_agg);
2096 		rs_set_expected_tpt_table(lq_sta, tbl);
2097 		rs_rate_scale_clear_tbl_windows(mvm, tbl);
2098 	}
2099 
2100 	/* current tx rate */
2101 	index = rate->index;
2102 
2103 	/* rates available for this association, and for modulation mode */
2104 	rate_mask = rs_get_supported_rates(lq_sta, rate);
2105 
2106 	if (!(BIT(index) & rate_mask)) {
2107 		IWL_ERR(mvm, "Current Rate is not valid\n");
2108 		if (lq_sta->search_better_tbl) {
2109 			/* revert to active table if search table is not valid*/
2110 			rate->type = LQ_NONE;
2111 			lq_sta->search_better_tbl = 0;
2112 			tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
2113 			rs_update_rate_tbl(mvm, sta, lq_sta, tbl);
2114 		}
2115 		return;
2116 	}
2117 
2118 	/* Get expected throughput table and history window for current rate */
2119 	if (!tbl->expected_tpt) {
2120 		IWL_ERR(mvm, "tbl->expected_tpt is NULL\n");
2121 		return;
2122 	}
2123 
2124 	/* TODO: handle rate_idx_mask and rate_idx_mcs_mask */
2125 	window = &(tbl->win[index]);
2126 
2127 	/*
2128 	 * If there is not enough history to calculate actual average
2129 	 * throughput, keep analyzing results of more tx frames, without
2130 	 * changing rate or mode (bypass most of the rest of this function).
2131 	 * Set up new rate table in uCode only if old rate is not supported
2132 	 * in current association (use new rate found above).
2133 	 */
2134 	fail_count = window->counter - window->success_counter;
2135 	if ((fail_count < IWL_MVM_RS_RATE_MIN_FAILURE_TH) &&
2136 	    (window->success_counter < IWL_MVM_RS_RATE_MIN_SUCCESS_TH)) {
2137 		IWL_DEBUG_RATE(mvm,
2138 			       "%s: Test Window: succ %d total %d\n",
2139 			       rs_pretty_rate(rate),
2140 			       window->success_counter, window->counter);
2141 
2142 		/* Can't calculate this yet; not enough history */
2143 		window->average_tpt = IWL_INVALID_VALUE;
2144 
2145 		/* Should we stay with this modulation mode,
2146 		 * or search for a new one? */
2147 		rs_stay_in_table(lq_sta, false);
2148 
2149 		return;
2150 	}
2151 
2152 	/* If we are searching for better modulation mode, check success. */
2153 	if (lq_sta->search_better_tbl) {
2154 		/* If good success, continue using the "search" mode;
2155 		 * no need to send new link quality command, since we're
2156 		 * continuing to use the setup that we've been trying. */
2157 		if (window->average_tpt > lq_sta->last_tpt) {
2158 			IWL_DEBUG_RATE(mvm,
2159 				       "SWITCHING TO NEW TABLE SR: %d "
2160 				       "cur-tpt %d old-tpt %d\n",
2161 				       window->success_ratio,
2162 				       window->average_tpt,
2163 				       lq_sta->last_tpt);
2164 
2165 			/* Swap tables; "search" becomes "active" */
2166 			lq_sta->active_tbl = active_tbl;
2167 			current_tpt = window->average_tpt;
2168 		/* Else poor success; go back to mode in "active" table */
2169 		} else {
2170 			IWL_DEBUG_RATE(mvm,
2171 				       "GOING BACK TO THE OLD TABLE: SR %d "
2172 				       "cur-tpt %d old-tpt %d\n",
2173 				       window->success_ratio,
2174 				       window->average_tpt,
2175 				       lq_sta->last_tpt);
2176 
2177 			/* Nullify "search" table */
2178 			rate->type = LQ_NONE;
2179 
2180 			/* Revert to "active" table */
2181 			active_tbl = lq_sta->active_tbl;
2182 			tbl = &(lq_sta->lq_info[active_tbl]);
2183 
2184 			/* Revert to "active" rate and throughput info */
2185 			index = tbl->rate.index;
2186 			current_tpt = lq_sta->last_tpt;
2187 
2188 			/* Need to set up a new rate table in uCode */
2189 			update_lq = 1;
2190 		}
2191 
2192 		/* Either way, we've made a decision; modulation mode
2193 		 * search is done, allow rate adjustment next time. */
2194 		lq_sta->search_better_tbl = 0;
2195 		done_search = 1;	/* Don't switch modes below! */
2196 		goto lq_update;
2197 	}
2198 
2199 	/* (Else) not in search of better modulation mode, try for better
2200 	 * starting rate, while staying in this mode. */
2201 	high_low = rs_get_adjacent_rate(mvm, index, rate_mask, rate->type);
2202 	low = high_low & 0xff;
2203 	high = (high_low >> 8) & 0xff;
2204 
2205 	/* TODO: handle rate_idx_mask and rate_idx_mcs_mask */
2206 
2207 	sr = window->success_ratio;
2208 
2209 	/* Collect measured throughputs for current and adjacent rates */
2210 	current_tpt = window->average_tpt;
2211 	if (low != IWL_RATE_INVALID)
2212 		low_tpt = tbl->win[low].average_tpt;
2213 	if (high != IWL_RATE_INVALID)
2214 		high_tpt = tbl->win[high].average_tpt;
2215 
2216 	IWL_DEBUG_RATE(mvm,
2217 		       "%s: cur_tpt %d SR %d low %d high %d low_tpt %d high_tpt %d\n",
2218 		       rs_pretty_rate(rate), current_tpt, sr,
2219 		       low, high, low_tpt, high_tpt);
2220 
2221 	scale_action = rs_get_rate_action(mvm, tbl, sr, low, high,
2222 					  current_tpt, low_tpt, high_tpt);
2223 
2224 	/* Force a search in case BT doesn't like us being in MIMO */
2225 	if (is_mimo(rate) &&
2226 	    !iwl_mvm_bt_coex_is_mimo_allowed(mvm, sta)) {
2227 		IWL_DEBUG_RATE(mvm,
2228 			       "BT Coex forbids MIMO. Search for new config\n");
2229 		rs_stay_in_table(lq_sta, true);
2230 		goto lq_update;
2231 	}
2232 
2233 	switch (scale_action) {
2234 	case RS_ACTION_DOWNSCALE:
2235 		/* Decrease starting rate, update uCode's rate table */
2236 		if (low != IWL_RATE_INVALID) {
2237 			update_lq = 1;
2238 			index = low;
2239 		} else {
2240 			IWL_DEBUG_RATE(mvm,
2241 				       "At the bottom rate. Can't decrease\n");
2242 		}
2243 
2244 		break;
2245 	case RS_ACTION_UPSCALE:
2246 		/* Increase starting rate, update uCode's rate table */
2247 		if (high != IWL_RATE_INVALID) {
2248 			update_lq = 1;
2249 			index = high;
2250 		} else {
2251 			IWL_DEBUG_RATE(mvm,
2252 				       "At the top rate. Can't increase\n");
2253 		}
2254 
2255 		break;
2256 	case RS_ACTION_STAY:
2257 		/* No change */
2258 		if (lq_sta->rs_state == RS_STATE_STAY_IN_COLUMN)
2259 			update_lq = rs_tpc_perform(mvm, sta, lq_sta, tbl);
2260 		break;
2261 	default:
2262 		break;
2263 	}
2264 
2265 lq_update:
2266 	/* Replace uCode's rate table for the destination station. */
2267 	if (update_lq) {
2268 		tbl->rate.index = index;
2269 		if (IWL_MVM_RS_80_20_FAR_RANGE_TWEAK)
2270 			rs_tweak_rate_tbl(mvm, sta, lq_sta, tbl, scale_action);
2271 		rs_set_amsdu_len(mvm, sta, tbl, scale_action);
2272 		rs_update_rate_tbl(mvm, sta, lq_sta, tbl);
2273 	}
2274 
2275 	rs_stay_in_table(lq_sta, false);
2276 
2277 	/*
2278 	 * Search for new modulation mode if we're:
2279 	 * 1)  Not changing rates right now
2280 	 * 2)  Not just finishing up a search
2281 	 * 3)  Allowing a new search
2282 	 */
2283 	if (!update_lq && !done_search &&
2284 	    lq_sta->rs_state == RS_STATE_SEARCH_CYCLE_STARTED
2285 	    && window->counter) {
2286 		enum rs_column next_column;
2287 
2288 		/* Save current throughput to compare with "search" throughput*/
2289 		lq_sta->last_tpt = current_tpt;
2290 
2291 		IWL_DEBUG_RATE(mvm,
2292 			       "Start Search: update_lq %d done_search %d rs_state %d win->counter %d\n",
2293 			       update_lq, done_search, lq_sta->rs_state,
2294 			       window->counter);
2295 
2296 		next_column = rs_get_next_column(mvm, lq_sta, sta, tbl);
2297 		if (next_column != RS_COLUMN_INVALID) {
2298 			int ret = rs_switch_to_column(mvm, lq_sta, sta,
2299 						      next_column);
2300 			if (!ret)
2301 				lq_sta->search_better_tbl = 1;
2302 		} else {
2303 			IWL_DEBUG_RATE(mvm,
2304 				       "No more columns to explore in search cycle. Go to RS_STATE_SEARCH_CYCLE_ENDED\n");
2305 			lq_sta->rs_state = RS_STATE_SEARCH_CYCLE_ENDED;
2306 		}
2307 
2308 		/* If new "search" mode was selected, set up in uCode table */
2309 		if (lq_sta->search_better_tbl) {
2310 			/* Access the "search" table, clear its history. */
2311 			tbl = &lq_sta->lq_info[rs_search_tbl(lq_sta->active_tbl)];
2312 			rs_rate_scale_clear_tbl_windows(mvm, tbl);
2313 
2314 			/* Use new "search" start rate */
2315 			index = tbl->rate.index;
2316 
2317 			rs_dump_rate(mvm, &tbl->rate,
2318 				     "Switch to SEARCH TABLE:");
2319 			rs_update_rate_tbl(mvm, sta, lq_sta, tbl);
2320 		} else {
2321 			done_search = 1;
2322 		}
2323 	}
2324 
2325 	if (!ndp)
2326 		rs_tl_turn_on_agg(mvm, mvmsta, tid, lq_sta, sta);
2327 
2328 	if (done_search && lq_sta->rs_state == RS_STATE_SEARCH_CYCLE_ENDED) {
2329 		tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
2330 		rs_set_stay_in_table(mvm, is_legacy(&tbl1->rate), lq_sta);
2331 	}
2332 }
2333 
2334 struct rs_init_rate_info {
2335 	s8 rssi;
2336 	u8 rate_idx;
2337 };
2338 
2339 static const struct rs_init_rate_info rs_optimal_rates_24ghz_legacy[] = {
2340 	{ -60, IWL_RATE_54M_INDEX },
2341 	{ -64, IWL_RATE_48M_INDEX },
2342 	{ -68, IWL_RATE_36M_INDEX },
2343 	{ -80, IWL_RATE_24M_INDEX },
2344 	{ -84, IWL_RATE_18M_INDEX },
2345 	{ -85, IWL_RATE_12M_INDEX },
2346 	{ -86, IWL_RATE_11M_INDEX },
2347 	{ -88, IWL_RATE_5M_INDEX  },
2348 	{ -90, IWL_RATE_2M_INDEX  },
2349 	{ S8_MIN, IWL_RATE_1M_INDEX },
2350 };
2351 
2352 static const struct rs_init_rate_info rs_optimal_rates_5ghz_legacy[] = {
2353 	{ -60, IWL_RATE_54M_INDEX },
2354 	{ -64, IWL_RATE_48M_INDEX },
2355 	{ -72, IWL_RATE_36M_INDEX },
2356 	{ -80, IWL_RATE_24M_INDEX },
2357 	{ -84, IWL_RATE_18M_INDEX },
2358 	{ -85, IWL_RATE_12M_INDEX },
2359 	{ -87, IWL_RATE_9M_INDEX  },
2360 	{ S8_MIN, IWL_RATE_6M_INDEX },
2361 };
2362 
2363 static const struct rs_init_rate_info rs_optimal_rates_ht[] = {
2364 	{ -60, IWL_RATE_MCS_7_INDEX },
2365 	{ -64, IWL_RATE_MCS_6_INDEX },
2366 	{ -68, IWL_RATE_MCS_5_INDEX },
2367 	{ -72, IWL_RATE_MCS_4_INDEX },
2368 	{ -80, IWL_RATE_MCS_3_INDEX },
2369 	{ -84, IWL_RATE_MCS_2_INDEX },
2370 	{ -85, IWL_RATE_MCS_1_INDEX },
2371 	{ S8_MIN, IWL_RATE_MCS_0_INDEX},
2372 };
2373 
2374 /* MCS index 9 is not valid for 20MHz VHT channel width,
2375  * but is ok for 40, 80 and 160MHz channels.
2376  */
2377 static const struct rs_init_rate_info rs_optimal_rates_vht_20mhz[] = {
2378 	{ -60, IWL_RATE_MCS_8_INDEX },
2379 	{ -64, IWL_RATE_MCS_7_INDEX },
2380 	{ -68, IWL_RATE_MCS_6_INDEX },
2381 	{ -72, IWL_RATE_MCS_5_INDEX },
2382 	{ -80, IWL_RATE_MCS_4_INDEX },
2383 	{ -84, IWL_RATE_MCS_3_INDEX },
2384 	{ -85, IWL_RATE_MCS_2_INDEX },
2385 	{ -87, IWL_RATE_MCS_1_INDEX },
2386 	{ S8_MIN, IWL_RATE_MCS_0_INDEX},
2387 };
2388 
2389 static const struct rs_init_rate_info rs_optimal_rates_vht[] = {
2390 	{ -60, IWL_RATE_MCS_9_INDEX },
2391 	{ -64, IWL_RATE_MCS_8_INDEX },
2392 	{ -68, IWL_RATE_MCS_7_INDEX },
2393 	{ -72, IWL_RATE_MCS_6_INDEX },
2394 	{ -80, IWL_RATE_MCS_5_INDEX },
2395 	{ -84, IWL_RATE_MCS_4_INDEX },
2396 	{ -85, IWL_RATE_MCS_3_INDEX },
2397 	{ -87, IWL_RATE_MCS_2_INDEX },
2398 	{ -88, IWL_RATE_MCS_1_INDEX },
2399 	{ S8_MIN, IWL_RATE_MCS_0_INDEX },
2400 };
2401 
2402 #define IWL_RS_LOW_RSSI_THRESHOLD (-76) /* dBm */
2403 
2404 /* Init the optimal rate based on STA caps
2405  * This combined with rssi is used to report the last tx rate
2406  * to userspace when we haven't transmitted enough frames.
2407  */
rs_init_optimal_rate(struct iwl_mvm * mvm,struct ieee80211_sta * sta,struct iwl_lq_sta * lq_sta)2408 static void rs_init_optimal_rate(struct iwl_mvm *mvm,
2409 				 struct ieee80211_sta *sta,
2410 				 struct iwl_lq_sta *lq_sta)
2411 {
2412 	struct rs_rate *rate = &lq_sta->optimal_rate;
2413 
2414 	if (lq_sta->max_mimo2_rate_idx != IWL_RATE_INVALID)
2415 		rate->type = lq_sta->is_vht ? LQ_VHT_MIMO2 : LQ_HT_MIMO2;
2416 	else if (lq_sta->max_siso_rate_idx != IWL_RATE_INVALID)
2417 		rate->type = lq_sta->is_vht ? LQ_VHT_SISO : LQ_HT_SISO;
2418 	else if (lq_sta->band == NL80211_BAND_5GHZ)
2419 		rate->type = LQ_LEGACY_A;
2420 	else
2421 		rate->type = LQ_LEGACY_G;
2422 
2423 	rate->bw = rs_bw_from_sta_bw(sta);
2424 	rate->sgi = rs_sgi_allow(mvm, sta, rate, NULL);
2425 
2426 	/* ANT/LDPC/STBC aren't relevant for the rate reported to userspace */
2427 
2428 	if (is_mimo(rate)) {
2429 		lq_sta->optimal_rate_mask = lq_sta->active_mimo2_rate;
2430 	} else if (is_siso(rate)) {
2431 		lq_sta->optimal_rate_mask = lq_sta->active_siso_rate;
2432 	} else {
2433 		lq_sta->optimal_rate_mask = lq_sta->active_legacy_rate;
2434 
2435 		if (lq_sta->band == NL80211_BAND_5GHZ) {
2436 			lq_sta->optimal_rates = rs_optimal_rates_5ghz_legacy;
2437 			lq_sta->optimal_nentries =
2438 				ARRAY_SIZE(rs_optimal_rates_5ghz_legacy);
2439 		} else {
2440 			lq_sta->optimal_rates = rs_optimal_rates_24ghz_legacy;
2441 			lq_sta->optimal_nentries =
2442 				ARRAY_SIZE(rs_optimal_rates_24ghz_legacy);
2443 		}
2444 	}
2445 
2446 	if (is_vht(rate)) {
2447 		if (rate->bw == RATE_MCS_CHAN_WIDTH_20) {
2448 			lq_sta->optimal_rates = rs_optimal_rates_vht_20mhz;
2449 			lq_sta->optimal_nentries =
2450 				ARRAY_SIZE(rs_optimal_rates_vht_20mhz);
2451 		} else {
2452 			lq_sta->optimal_rates = rs_optimal_rates_vht;
2453 			lq_sta->optimal_nentries =
2454 				ARRAY_SIZE(rs_optimal_rates_vht);
2455 		}
2456 	} else if (is_ht(rate)) {
2457 		lq_sta->optimal_rates = rs_optimal_rates_ht;
2458 		lq_sta->optimal_nentries = ARRAY_SIZE(rs_optimal_rates_ht);
2459 	}
2460 }
2461 
2462 /* Compute the optimal rate index based on RSSI */
rs_get_optimal_rate(struct iwl_mvm * mvm,struct iwl_lq_sta * lq_sta)2463 static struct rs_rate *rs_get_optimal_rate(struct iwl_mvm *mvm,
2464 					   struct iwl_lq_sta *lq_sta)
2465 {
2466 	struct rs_rate *rate = &lq_sta->optimal_rate;
2467 	int i;
2468 
2469 	rate->index = find_first_bit(&lq_sta->optimal_rate_mask,
2470 				     BITS_PER_LONG);
2471 
2472 	for (i = 0; i < lq_sta->optimal_nentries; i++) {
2473 		int rate_idx = lq_sta->optimal_rates[i].rate_idx;
2474 
2475 		if ((lq_sta->pers.last_rssi >= lq_sta->optimal_rates[i].rssi) &&
2476 		    (BIT(rate_idx) & lq_sta->optimal_rate_mask)) {
2477 			rate->index = rate_idx;
2478 			break;
2479 		}
2480 	}
2481 
2482 	return rate;
2483 }
2484 
2485 /* Choose an initial legacy rate and antenna to use based on the RSSI
2486  * of last Rx
2487  */
rs_get_initial_rate(struct iwl_mvm * mvm,struct ieee80211_sta * sta,struct iwl_lq_sta * lq_sta,enum nl80211_band band,struct rs_rate * rate)2488 static void rs_get_initial_rate(struct iwl_mvm *mvm,
2489 				struct ieee80211_sta *sta,
2490 				struct iwl_lq_sta *lq_sta,
2491 				enum nl80211_band band,
2492 				struct rs_rate *rate)
2493 {
2494 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2495 	int i, nentries;
2496 	unsigned long active_rate;
2497 	s8 best_rssi = S8_MIN;
2498 	u8 best_ant = ANT_NONE;
2499 	u8 valid_tx_ant = iwl_mvm_get_valid_tx_ant(mvm);
2500 	const struct rs_init_rate_info *initial_rates;
2501 
2502 	for (i = 0; i < ARRAY_SIZE(lq_sta->pers.chain_signal); i++) {
2503 		if (!(lq_sta->pers.chains & BIT(i)))
2504 			continue;
2505 
2506 		if (lq_sta->pers.chain_signal[i] > best_rssi) {
2507 			best_rssi = lq_sta->pers.chain_signal[i];
2508 			best_ant = BIT(i);
2509 		}
2510 	}
2511 
2512 	IWL_DEBUG_RATE(mvm, "Best ANT: %s Best RSSI: %d\n",
2513 		       iwl_rs_pretty_ant(best_ant), best_rssi);
2514 
2515 	if (best_ant != ANT_A && best_ant != ANT_B)
2516 		rate->ant = first_antenna(valid_tx_ant);
2517 	else
2518 		rate->ant = best_ant;
2519 
2520 	rate->sgi = false;
2521 	rate->ldpc = false;
2522 	rate->bw = RATE_MCS_CHAN_WIDTH_20;
2523 
2524 	rate->index = find_first_bit(&lq_sta->active_legacy_rate,
2525 				     BITS_PER_LONG);
2526 
2527 	if (band == NL80211_BAND_5GHZ) {
2528 		rate->type = LQ_LEGACY_A;
2529 		initial_rates = rs_optimal_rates_5ghz_legacy;
2530 		nentries = ARRAY_SIZE(rs_optimal_rates_5ghz_legacy);
2531 	} else {
2532 		rate->type = LQ_LEGACY_G;
2533 		initial_rates = rs_optimal_rates_24ghz_legacy;
2534 		nentries = ARRAY_SIZE(rs_optimal_rates_24ghz_legacy);
2535 	}
2536 
2537 	if (!IWL_MVM_RS_RSSI_BASED_INIT_RATE)
2538 		goto out;
2539 
2540 	/* Start from a higher rate if the corresponding debug capability
2541 	 * is enabled. The rate is chosen according to AP capabilities.
2542 	 * In case of VHT/HT when the rssi is low fallback to the case of
2543 	 * legacy rates.
2544 	 */
2545 	if (sta->deflink.vht_cap.vht_supported &&
2546 	    best_rssi > IWL_RS_LOW_RSSI_THRESHOLD) {
2547 		/*
2548 		 * In AP mode, when a new station associates, rs is initialized
2549 		 * immediately upon association completion, before the phy
2550 		 * context is updated with the association parameters, so the
2551 		 * sta bandwidth might be wider than the phy context allows.
2552 		 * To avoid this issue, always initialize rs with 20mhz
2553 		 * bandwidth rate, and after authorization, when the phy context
2554 		 * is already up-to-date, re-init rs with the correct bw.
2555 		 */
2556 		u32 bw = mvmsta->sta_state < IEEE80211_STA_AUTHORIZED ?
2557 				RATE_MCS_CHAN_WIDTH_20 : rs_bw_from_sta_bw(sta);
2558 
2559 		switch (bw) {
2560 		case RATE_MCS_CHAN_WIDTH_40:
2561 		case RATE_MCS_CHAN_WIDTH_80:
2562 		case RATE_MCS_CHAN_WIDTH_160:
2563 			initial_rates = rs_optimal_rates_vht;
2564 			nentries = ARRAY_SIZE(rs_optimal_rates_vht);
2565 			break;
2566 		case RATE_MCS_CHAN_WIDTH_20:
2567 			initial_rates = rs_optimal_rates_vht_20mhz;
2568 			nentries = ARRAY_SIZE(rs_optimal_rates_vht_20mhz);
2569 			break;
2570 		default:
2571 			IWL_ERR(mvm, "Invalid BW %d\n",
2572 				sta->deflink.bandwidth);
2573 			goto out;
2574 		}
2575 
2576 		active_rate = lq_sta->active_siso_rate;
2577 		rate->type = LQ_VHT_SISO;
2578 		rate->bw = bw;
2579 	} else if (sta->deflink.ht_cap.ht_supported &&
2580 		   best_rssi > IWL_RS_LOW_RSSI_THRESHOLD) {
2581 		initial_rates = rs_optimal_rates_ht;
2582 		nentries = ARRAY_SIZE(rs_optimal_rates_ht);
2583 		active_rate = lq_sta->active_siso_rate;
2584 		rate->type = LQ_HT_SISO;
2585 	} else {
2586 		active_rate = lq_sta->active_legacy_rate;
2587 	}
2588 
2589 	for (i = 0; i < nentries; i++) {
2590 		int rate_idx = initial_rates[i].rate_idx;
2591 
2592 		if ((best_rssi >= initial_rates[i].rssi) &&
2593 		    (BIT(rate_idx) & active_rate)) {
2594 			rate->index = rate_idx;
2595 			break;
2596 		}
2597 	}
2598 
2599 out:
2600 	rs_dump_rate(mvm, rate, "INITIAL");
2601 }
2602 
2603 /* Save info about RSSI of last Rx */
rs_update_last_rssi(struct iwl_mvm * mvm,struct iwl_mvm_sta * mvmsta,struct ieee80211_rx_status * rx_status)2604 void rs_update_last_rssi(struct iwl_mvm *mvm,
2605 			 struct iwl_mvm_sta *mvmsta,
2606 			 struct ieee80211_rx_status *rx_status)
2607 {
2608 	struct iwl_lq_sta *lq_sta = &mvmsta->deflink.lq_sta.rs_drv;
2609 	int i;
2610 
2611 	lq_sta->pers.chains = rx_status->chains;
2612 	lq_sta->pers.chain_signal[0] = rx_status->chain_signal[0];
2613 	lq_sta->pers.chain_signal[1] = rx_status->chain_signal[1];
2614 	lq_sta->pers.last_rssi = S8_MIN;
2615 
2616 	for (i = 0; i < ARRAY_SIZE(lq_sta->pers.chain_signal); i++) {
2617 		if (!(lq_sta->pers.chains & BIT(i)))
2618 			continue;
2619 
2620 		if (lq_sta->pers.chain_signal[i] > lq_sta->pers.last_rssi)
2621 			lq_sta->pers.last_rssi = lq_sta->pers.chain_signal[i];
2622 	}
2623 }
2624 
2625 /*
2626  * rs_initialize_lq - Initialize a station's hardware rate table
2627  *
2628  * The uCode's station table contains a table of fallback rates
2629  * for automatic fallback during transmission.
2630  *
2631  * NOTE: This sets up a default set of values.  These will be replaced later
2632  *       if the driver's iwl-agn-rs rate scaling algorithm is used, instead of
2633  *       rc80211_simple.
2634  *
2635  * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
2636  *       calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
2637  *       which requires station table entry to exist).
2638  */
rs_initialize_lq(struct iwl_mvm * mvm,struct ieee80211_sta * sta,struct iwl_lq_sta * lq_sta,enum nl80211_band band)2639 static void rs_initialize_lq(struct iwl_mvm *mvm,
2640 			     struct ieee80211_sta *sta,
2641 			     struct iwl_lq_sta *lq_sta,
2642 			     enum nl80211_band band)
2643 {
2644 	struct iwl_scale_tbl_info *tbl;
2645 	struct rs_rate *rate;
2646 	u8 active_tbl = 0;
2647 
2648 	if (!sta || !lq_sta)
2649 		return;
2650 
2651 	if (!lq_sta->search_better_tbl)
2652 		active_tbl = lq_sta->active_tbl;
2653 	else
2654 		active_tbl = rs_search_tbl(lq_sta->active_tbl);
2655 
2656 	tbl = &(lq_sta->lq_info[active_tbl]);
2657 	rate = &tbl->rate;
2658 
2659 	rs_get_initial_rate(mvm, sta, lq_sta, band, rate);
2660 	rs_init_optimal_rate(mvm, sta, lq_sta);
2661 
2662 	WARN_ONCE(rate->ant != ANT_A && rate->ant != ANT_B,
2663 		  "ant: 0x%x, chains 0x%x, fw tx ant: 0x%x, nvm tx ant: 0x%x\n",
2664 		  rate->ant, lq_sta->pers.chains, mvm->fw->valid_tx_ant,
2665 		  mvm->nvm_data ? mvm->nvm_data->valid_tx_ant : ANT_INVALID);
2666 
2667 	tbl->column = rs_get_column_from_rate(rate);
2668 
2669 	rs_set_expected_tpt_table(lq_sta, tbl);
2670 	rs_fill_lq_cmd(mvm, sta, lq_sta, rate);
2671 	/* TODO restore station should remember the lq cmd */
2672 	iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq);
2673 }
2674 
rs_drv_get_rate(void * mvm_r,struct ieee80211_sta * sta,void * mvm_sta,struct ieee80211_tx_rate_control * txrc)2675 static void rs_drv_get_rate(void *mvm_r, struct ieee80211_sta *sta,
2676 			    void *mvm_sta,
2677 			    struct ieee80211_tx_rate_control *txrc)
2678 {
2679 	struct iwl_op_mode *op_mode = mvm_r;
2680 	struct iwl_mvm *mvm __maybe_unused = IWL_OP_MODE_GET_MVM(op_mode);
2681 	struct sk_buff *skb = txrc->skb;
2682 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2683 	struct iwl_lq_sta *lq_sta;
2684 	struct rs_rate *optimal_rate;
2685 	u32 last_ucode_rate;
2686 
2687 	if (sta && !iwl_mvm_sta_from_mac80211(sta)->vif) {
2688 		/* if vif isn't initialized mvm doesn't know about
2689 		 * this station, so don't do anything with the it
2690 		 */
2691 		mvm_sta = NULL;
2692 	}
2693 
2694 	if (!mvm_sta)
2695 		return;
2696 
2697 	lq_sta = mvm_sta;
2698 
2699 	spin_lock_bh(&lq_sta->pers.lock);
2700 	iwl_mvm_hwrate_to_tx_rate(iwl_mvm_v3_rate_from_fw(
2701 					cpu_to_le32(lq_sta->last_rate_n_flags),
2702 					1),
2703 				  info->band, &info->control.rates[0]);
2704 	info->control.rates[0].count = 1;
2705 
2706 	/* Report the optimal rate based on rssi and STA caps if we haven't
2707 	 * converged yet (too little traffic) or exploring other modulations
2708 	 */
2709 	if (lq_sta->rs_state != RS_STATE_STAY_IN_COLUMN) {
2710 		optimal_rate = rs_get_optimal_rate(mvm, lq_sta);
2711 		last_ucode_rate = ucode_rate_from_rs_rate(mvm,
2712 							  optimal_rate);
2713 		last_ucode_rate =
2714 			iwl_mvm_v3_rate_from_fw(cpu_to_le32(last_ucode_rate),
2715 						1);
2716 		iwl_mvm_hwrate_to_tx_rate(last_ucode_rate, info->band,
2717 					  &txrc->reported_rate);
2718 		txrc->reported_rate.count = 1;
2719 	}
2720 	spin_unlock_bh(&lq_sta->pers.lock);
2721 }
2722 
rs_drv_alloc_sta(void * mvm_rate,struct ieee80211_sta * sta,gfp_t gfp)2723 static void *rs_drv_alloc_sta(void *mvm_rate, struct ieee80211_sta *sta,
2724 			      gfp_t gfp)
2725 {
2726 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2727 	struct iwl_op_mode *op_mode = (struct iwl_op_mode *)mvm_rate;
2728 	struct iwl_mvm *mvm  = IWL_OP_MODE_GET_MVM(op_mode);
2729 	struct iwl_lq_sta *lq_sta = &mvmsta->deflink.lq_sta.rs_drv;
2730 
2731 	IWL_DEBUG_RATE(mvm, "create station rate scale window\n");
2732 
2733 	lq_sta->pers.drv = mvm;
2734 #ifdef CONFIG_MAC80211_DEBUGFS
2735 	lq_sta->pers.dbg_fixed_rate = 0;
2736 	lq_sta->pers.dbg_fixed_txp_reduction = TPC_INVALID;
2737 	lq_sta->pers.ss_force = RS_SS_FORCE_NONE;
2738 #endif
2739 	lq_sta->pers.chains = 0;
2740 	memset(lq_sta->pers.chain_signal, 0, sizeof(lq_sta->pers.chain_signal));
2741 	lq_sta->pers.last_rssi = S8_MIN;
2742 
2743 	return lq_sta;
2744 }
2745 
rs_vht_highest_rx_mcs_index(struct ieee80211_sta_vht_cap * vht_cap,int nss)2746 static int rs_vht_highest_rx_mcs_index(struct ieee80211_sta_vht_cap *vht_cap,
2747 				       int nss)
2748 {
2749 	u16 rx_mcs = le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map) &
2750 		(0x3 << (2 * (nss - 1)));
2751 	rx_mcs >>= (2 * (nss - 1));
2752 
2753 	if (rx_mcs == IEEE80211_VHT_MCS_SUPPORT_0_7)
2754 		return IWL_RATE_MCS_7_INDEX;
2755 	else if (rx_mcs == IEEE80211_VHT_MCS_SUPPORT_0_8)
2756 		return IWL_RATE_MCS_8_INDEX;
2757 	else if (rx_mcs == IEEE80211_VHT_MCS_SUPPORT_0_9)
2758 		return IWL_RATE_MCS_9_INDEX;
2759 
2760 	WARN_ON_ONCE(rx_mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED);
2761 	return -1;
2762 }
2763 
rs_vht_set_enabled_rates(struct ieee80211_sta * sta,struct ieee80211_sta_vht_cap * vht_cap,struct iwl_lq_sta * lq_sta)2764 static void rs_vht_set_enabled_rates(struct ieee80211_sta *sta,
2765 				     struct ieee80211_sta_vht_cap *vht_cap,
2766 				     struct iwl_lq_sta *lq_sta)
2767 {
2768 	int i;
2769 	int highest_mcs = rs_vht_highest_rx_mcs_index(vht_cap, 1);
2770 
2771 	if (highest_mcs >= IWL_RATE_MCS_0_INDEX) {
2772 		for (i = IWL_RATE_MCS_0_INDEX; i <= highest_mcs; i++) {
2773 			if (i == IWL_RATE_9M_INDEX)
2774 				continue;
2775 
2776 			/* VHT MCS9 isn't valid for 20Mhz for NSS=1,2 */
2777 			if (i == IWL_RATE_MCS_9_INDEX &&
2778 			    sta->deflink.bandwidth == IEEE80211_STA_RX_BW_20)
2779 				continue;
2780 
2781 			lq_sta->active_siso_rate |= BIT(i);
2782 		}
2783 	}
2784 
2785 	if (sta->deflink.rx_nss < 2)
2786 		return;
2787 
2788 	highest_mcs = rs_vht_highest_rx_mcs_index(vht_cap, 2);
2789 	if (highest_mcs >= IWL_RATE_MCS_0_INDEX) {
2790 		for (i = IWL_RATE_MCS_0_INDEX; i <= highest_mcs; i++) {
2791 			if (i == IWL_RATE_9M_INDEX)
2792 				continue;
2793 
2794 			/* VHT MCS9 isn't valid for 20Mhz for NSS=1,2 */
2795 			if (i == IWL_RATE_MCS_9_INDEX &&
2796 			    sta->deflink.bandwidth == IEEE80211_STA_RX_BW_20)
2797 				continue;
2798 
2799 			lq_sta->active_mimo2_rate |= BIT(i);
2800 		}
2801 	}
2802 }
2803 
rs_ht_init(struct iwl_mvm * mvm,struct ieee80211_sta * sta,struct iwl_lq_sta * lq_sta,struct ieee80211_sta_ht_cap * ht_cap)2804 static void rs_ht_init(struct iwl_mvm *mvm,
2805 		       struct ieee80211_sta *sta,
2806 		       struct iwl_lq_sta *lq_sta,
2807 		       struct ieee80211_sta_ht_cap *ht_cap)
2808 {
2809 	/* active_siso_rate mask includes 9 MBits (bit 5),
2810 	 * and CCK (bits 0-3), supp_rates[] does not;
2811 	 * shift to convert format, force 9 MBits off.
2812 	 */
2813 	lq_sta->active_siso_rate = ht_cap->mcs.rx_mask[0] << 1;
2814 	lq_sta->active_siso_rate |= ht_cap->mcs.rx_mask[0] & 0x1;
2815 	lq_sta->active_siso_rate &= ~((u16)0x2);
2816 	lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
2817 
2818 	lq_sta->active_mimo2_rate = ht_cap->mcs.rx_mask[1] << 1;
2819 	lq_sta->active_mimo2_rate |= ht_cap->mcs.rx_mask[1] & 0x1;
2820 	lq_sta->active_mimo2_rate &= ~((u16)0x2);
2821 	lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2822 
2823 	if (mvm->cfg->ht_params.ldpc &&
2824 	    (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING))
2825 		lq_sta->ldpc = true;
2826 
2827 	if (mvm->cfg->ht_params.stbc &&
2828 	    (num_of_ant(iwl_mvm_get_valid_tx_ant(mvm)) > 1) &&
2829 	    (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC))
2830 		lq_sta->stbc_capable = true;
2831 
2832 	lq_sta->is_vht = false;
2833 }
2834 
rs_vht_init(struct iwl_mvm * mvm,struct ieee80211_sta * sta,struct iwl_lq_sta * lq_sta,struct ieee80211_sta_vht_cap * vht_cap)2835 static void rs_vht_init(struct iwl_mvm *mvm,
2836 			struct ieee80211_sta *sta,
2837 			struct iwl_lq_sta *lq_sta,
2838 			struct ieee80211_sta_vht_cap *vht_cap)
2839 {
2840 	rs_vht_set_enabled_rates(sta, vht_cap, lq_sta);
2841 
2842 	if (mvm->cfg->ht_params.ldpc &&
2843 	    (vht_cap->cap & IEEE80211_VHT_CAP_RXLDPC))
2844 		lq_sta->ldpc = true;
2845 
2846 	if (mvm->cfg->ht_params.stbc &&
2847 	    (num_of_ant(iwl_mvm_get_valid_tx_ant(mvm)) > 1) &&
2848 	    (vht_cap->cap & IEEE80211_VHT_CAP_RXSTBC_MASK))
2849 		lq_sta->stbc_capable = true;
2850 
2851 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_BEAMFORMER) &&
2852 	    (num_of_ant(iwl_mvm_get_valid_tx_ant(mvm)) > 1) &&
2853 	    (vht_cap->cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE))
2854 		lq_sta->bfer_capable = true;
2855 
2856 	lq_sta->is_vht = true;
2857 }
2858 
2859 #ifdef CONFIG_IWLWIFI_DEBUGFS
iwl_mvm_reset_frame_stats(struct iwl_mvm * mvm)2860 void iwl_mvm_reset_frame_stats(struct iwl_mvm *mvm)
2861 {
2862 	spin_lock_bh(&mvm->drv_stats_lock);
2863 	memset(&mvm->drv_rx_stats, 0, sizeof(mvm->drv_rx_stats));
2864 	spin_unlock_bh(&mvm->drv_stats_lock);
2865 }
2866 
iwl_mvm_update_frame_stats(struct iwl_mvm * mvm,u32 rate,bool agg)2867 void iwl_mvm_update_frame_stats(struct iwl_mvm *mvm, u32 rate, bool agg)
2868 {
2869 	u8 nss = 0;
2870 
2871 	spin_lock(&mvm->drv_stats_lock);
2872 
2873 	if (agg)
2874 		mvm->drv_rx_stats.agg_frames++;
2875 
2876 	mvm->drv_rx_stats.success_frames++;
2877 
2878 	switch (rate & RATE_MCS_CHAN_WIDTH_MSK_V1) {
2879 	case RATE_MCS_CHAN_WIDTH_20:
2880 		mvm->drv_rx_stats.bw_20_frames++;
2881 		break;
2882 	case RATE_MCS_CHAN_WIDTH_40:
2883 		mvm->drv_rx_stats.bw_40_frames++;
2884 		break;
2885 	case RATE_MCS_CHAN_WIDTH_80:
2886 		mvm->drv_rx_stats.bw_80_frames++;
2887 		break;
2888 	case RATE_MCS_CHAN_WIDTH_160:
2889 		mvm->drv_rx_stats.bw_160_frames++;
2890 		break;
2891 	default:
2892 		WARN_ONCE(1, "bad BW. rate 0x%x", rate);
2893 	}
2894 
2895 	if (rate & RATE_MCS_HT_MSK_V1) {
2896 		mvm->drv_rx_stats.ht_frames++;
2897 		nss = FIELD_GET(RATE_HT_MCS_MIMO2_MSK, rate) + 1;
2898 	} else if (rate & RATE_MCS_VHT_MSK_V1) {
2899 		mvm->drv_rx_stats.vht_frames++;
2900 		nss = FIELD_GET(RATE_VHT_MCS_NSS_MSK, rate) + 1;
2901 	} else {
2902 		mvm->drv_rx_stats.legacy_frames++;
2903 	}
2904 
2905 	if (nss == 1)
2906 		mvm->drv_rx_stats.siso_frames++;
2907 	else if (nss == 2)
2908 		mvm->drv_rx_stats.mimo2_frames++;
2909 
2910 	if (rate & RATE_MCS_SGI_MSK_V1)
2911 		mvm->drv_rx_stats.sgi_frames++;
2912 	else
2913 		mvm->drv_rx_stats.ngi_frames++;
2914 
2915 	mvm->drv_rx_stats.last_rates[mvm->drv_rx_stats.last_frame_idx] = rate;
2916 	mvm->drv_rx_stats.last_frame_idx =
2917 		(mvm->drv_rx_stats.last_frame_idx + 1) %
2918 			ARRAY_SIZE(mvm->drv_rx_stats.last_rates);
2919 
2920 	spin_unlock(&mvm->drv_stats_lock);
2921 }
2922 #endif
2923 
2924 /*
2925  * Called after adding a new station to initialize rate scaling
2926  */
rs_drv_rate_init(struct iwl_mvm * mvm,struct ieee80211_sta * sta,enum nl80211_band band)2927 static void rs_drv_rate_init(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2928 			     enum nl80211_band band)
2929 {
2930 	int i, j;
2931 	struct ieee80211_hw *hw = mvm->hw;
2932 	struct ieee80211_sta_ht_cap *ht_cap = &sta->deflink.ht_cap;
2933 	struct ieee80211_sta_vht_cap *vht_cap = &sta->deflink.vht_cap;
2934 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2935 	struct iwl_lq_sta *lq_sta = &mvmsta->deflink.lq_sta.rs_drv;
2936 	struct ieee80211_supported_band *sband;
2937 	unsigned long supp; /* must be unsigned long for for_each_set_bit */
2938 
2939 	lockdep_assert_held(&mvmsta->deflink.lq_sta.rs_drv.pers.lock);
2940 
2941 	/* clear all non-persistent lq data */
2942 	memset(lq_sta, 0, offsetof(typeof(*lq_sta), pers));
2943 
2944 	sband = hw->wiphy->bands[band];
2945 
2946 	lq_sta->lq.sta_id = mvmsta->deflink.sta_id;
2947 	mvmsta->amsdu_enabled = 0;
2948 	mvmsta->max_amsdu_len = sta->cur->max_amsdu_len;
2949 
2950 	for (j = 0; j < LQ_SIZE; j++)
2951 		rs_rate_scale_clear_tbl_windows(mvm, &lq_sta->lq_info[j]);
2952 
2953 	lq_sta->flush_timer = 0;
2954 	lq_sta->last_tx = jiffies;
2955 
2956 	IWL_DEBUG_RATE(mvm,
2957 		       "LQ: *** rate scale station global init for station %d ***\n",
2958 		       mvmsta->deflink.sta_id);
2959 	/* TODO: what is a good starting rate for STA? About middle? Maybe not
2960 	 * the lowest or the highest rate.. Could consider using RSSI from
2961 	 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2962 	 * after assoc.. */
2963 
2964 	lq_sta->missed_rate_counter = IWL_MVM_RS_MISSED_RATE_MAX;
2965 	lq_sta->band = sband->band;
2966 	/*
2967 	 * active legacy rates as per supported rates bitmap
2968 	 */
2969 	supp = sta->deflink.supp_rates[sband->band];
2970 	lq_sta->active_legacy_rate = 0;
2971 	for_each_set_bit(i, &supp, BITS_PER_LONG)
2972 		lq_sta->active_legacy_rate |= BIT(sband->bitrates[i].hw_value);
2973 
2974 	/* TODO: should probably account for rx_highest for both HT/VHT */
2975 	if (!vht_cap || !vht_cap->vht_supported)
2976 		rs_ht_init(mvm, sta, lq_sta, ht_cap);
2977 	else
2978 		rs_vht_init(mvm, sta, lq_sta, vht_cap);
2979 
2980 	lq_sta->max_legacy_rate_idx =
2981 		rs_get_max_rate_from_mask(lq_sta->active_legacy_rate);
2982 	lq_sta->max_siso_rate_idx =
2983 		rs_get_max_rate_from_mask(lq_sta->active_siso_rate);
2984 	lq_sta->max_mimo2_rate_idx =
2985 		rs_get_max_rate_from_mask(lq_sta->active_mimo2_rate);
2986 
2987 	IWL_DEBUG_RATE(mvm,
2988 		       "LEGACY=%lX SISO=%lX MIMO2=%lX VHT=%d LDPC=%d STBC=%d BFER=%d\n",
2989 		       lq_sta->active_legacy_rate,
2990 		       lq_sta->active_siso_rate,
2991 		       lq_sta->active_mimo2_rate,
2992 		       lq_sta->is_vht, lq_sta->ldpc, lq_sta->stbc_capable,
2993 		       lq_sta->bfer_capable);
2994 	IWL_DEBUG_RATE(mvm, "MAX RATE: LEGACY=%d SISO=%d MIMO2=%d\n",
2995 		       lq_sta->max_legacy_rate_idx,
2996 		       lq_sta->max_siso_rate_idx,
2997 		       lq_sta->max_mimo2_rate_idx);
2998 
2999 	/* These values will be overridden later */
3000 	lq_sta->lq.single_stream_ant_msk =
3001 		iwl_mvm_bt_coex_get_single_ant_msk(mvm, iwl_mvm_get_valid_tx_ant(mvm));
3002 	lq_sta->lq.dual_stream_ant_msk = ANT_AB;
3003 
3004 	/* as default allow aggregation for all tids */
3005 	lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
3006 	lq_sta->is_agg = 0;
3007 #ifdef CONFIG_IWLWIFI_DEBUGFS
3008 	iwl_mvm_reset_frame_stats(mvm);
3009 #endif
3010 	rs_initialize_lq(mvm, sta, lq_sta, band);
3011 }
3012 
rs_drv_rate_update(void * mvm_r,struct ieee80211_supported_band * sband,struct cfg80211_chan_def * chandef,struct ieee80211_sta * sta,void * priv_sta,u32 changed)3013 static void rs_drv_rate_update(void *mvm_r,
3014 			       struct ieee80211_supported_band *sband,
3015 			       struct cfg80211_chan_def *chandef,
3016 			       struct ieee80211_sta *sta,
3017 			       void *priv_sta, u32 changed)
3018 {
3019 	struct iwl_op_mode *op_mode = mvm_r;
3020 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3021 	struct iwl_mvm *mvm __maybe_unused = IWL_OP_MODE_GET_MVM(op_mode);
3022 	u8 tid;
3023 
3024 	if (!mvmsta->vif)
3025 		return;
3026 
3027 	/* Stop any ongoing aggregations as rs starts off assuming no agg */
3028 	for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++)
3029 		ieee80211_stop_tx_ba_session(sta, tid);
3030 
3031 	iwl_mvm_rs_rate_init(mvm, mvmsta->vif, sta,
3032 			     &mvmsta->vif->bss_conf, &sta->deflink,
3033 			     sband->band);
3034 }
3035 
__iwl_mvm_rs_tx_status(struct iwl_mvm * mvm,struct ieee80211_sta * sta,int tid,struct ieee80211_tx_info * info,bool ndp)3036 static void __iwl_mvm_rs_tx_status(struct iwl_mvm *mvm,
3037 				   struct ieee80211_sta *sta,
3038 				   int tid, struct ieee80211_tx_info *info,
3039 				   bool ndp)
3040 {
3041 	int legacy_success;
3042 	int retries;
3043 	int i;
3044 	struct iwl_lq_cmd *table;
3045 	u32 lq_hwrate;
3046 	struct rs_rate lq_rate, tx_resp_rate;
3047 	struct iwl_scale_tbl_info *curr_tbl, *other_tbl, *tmp_tbl;
3048 	u32 tlc_info = (uintptr_t)info->status.status_driver_data[0];
3049 	u8 reduced_txp = tlc_info & RS_DRV_DATA_TXP_MSK;
3050 	u8 lq_color = RS_DRV_DATA_LQ_COLOR_GET(tlc_info);
3051 	u32 tx_resp_hwrate = (uintptr_t)info->status.status_driver_data[1];
3052 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3053 	struct iwl_lq_sta *lq_sta = &mvmsta->deflink.lq_sta.rs_drv;
3054 
3055 	if (!lq_sta->pers.drv) {
3056 		IWL_DEBUG_RATE(mvm, "Rate scaling not initialized yet.\n");
3057 		return;
3058 	}
3059 
3060 	/* This packet was aggregated but doesn't carry status info */
3061 	if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
3062 	    !(info->flags & IEEE80211_TX_STAT_AMPDU))
3063 		return;
3064 
3065 	if (rs_rate_from_ucode_rate(tx_resp_hwrate, info->band,
3066 				    &tx_resp_rate)) {
3067 		WARN_ON_ONCE(1);
3068 		return;
3069 	}
3070 
3071 #ifdef CONFIG_MAC80211_DEBUGFS
3072 	/* Disable last tx check if we are debugging with fixed rate but
3073 	 * update tx stats
3074 	 */
3075 	if (lq_sta->pers.dbg_fixed_rate) {
3076 		int index = tx_resp_rate.index;
3077 		enum rs_column column;
3078 		int attempts, success;
3079 
3080 		column = rs_get_column_from_rate(&tx_resp_rate);
3081 		if (WARN_ONCE(column == RS_COLUMN_INVALID,
3082 			      "Can't map rate 0x%x to column",
3083 			      tx_resp_hwrate))
3084 			return;
3085 
3086 		if (info->flags & IEEE80211_TX_STAT_AMPDU) {
3087 			attempts = info->status.ampdu_len;
3088 			success = info->status.ampdu_ack_len;
3089 		} else {
3090 			attempts = info->status.rates[0].count;
3091 			success = !!(info->flags & IEEE80211_TX_STAT_ACK);
3092 		}
3093 
3094 		lq_sta->pers.tx_stats[column][index].total += attempts;
3095 		lq_sta->pers.tx_stats[column][index].success += success;
3096 
3097 		IWL_DEBUG_RATE(mvm, "Fixed rate 0x%x success %d attempts %d\n",
3098 			       tx_resp_hwrate, success, attempts);
3099 		return;
3100 	}
3101 #endif
3102 
3103 	if (time_after(jiffies,
3104 		       (unsigned long)(lq_sta->last_tx +
3105 				       (IWL_MVM_RS_IDLE_TIMEOUT * HZ)))) {
3106 		IWL_DEBUG_RATE(mvm, "Tx idle for too long. reinit rs\n");
3107 		/* reach here only in case of driver RS, call directly
3108 		 * the unlocked version
3109 		 */
3110 		rs_drv_rate_init(mvm, sta, info->band);
3111 		return;
3112 	}
3113 	lq_sta->last_tx = jiffies;
3114 
3115 	/* Ignore this Tx frame response if its initial rate doesn't match
3116 	 * that of latest Link Quality command.  There may be stragglers
3117 	 * from a previous Link Quality command, but we're no longer interested
3118 	 * in those; they're either from the "active" mode while we're trying
3119 	 * to check "search" mode, or a prior "search" mode after we've moved
3120 	 * to a new "search" mode (which might become the new "active" mode).
3121 	 */
3122 	table = &lq_sta->lq;
3123 	lq_hwrate = le32_to_cpu(table->rs_table[0]);
3124 	if (rs_rate_from_ucode_rate(lq_hwrate, info->band, &lq_rate)) {
3125 		WARN_ON_ONCE(1);
3126 		return;
3127 	}
3128 
3129 	/* Here we actually compare this rate to the latest LQ command */
3130 	if (lq_color != LQ_FLAG_COLOR_GET(table->flags)) {
3131 		IWL_DEBUG_RATE(mvm,
3132 			       "tx resp color 0x%x does not match 0x%x\n",
3133 			       lq_color, LQ_FLAG_COLOR_GET(table->flags));
3134 
3135 		/* Since rates mis-match, the last LQ command may have failed.
3136 		 * After IWL_MISSED_RATE_MAX mis-matches, resync the uCode with
3137 		 * ... driver.
3138 		 */
3139 		lq_sta->missed_rate_counter++;
3140 		if (lq_sta->missed_rate_counter > IWL_MVM_RS_MISSED_RATE_MAX) {
3141 			lq_sta->missed_rate_counter = 0;
3142 			IWL_DEBUG_RATE(mvm,
3143 				       "Too many rates mismatch. Send sync LQ. rs_state %d\n",
3144 				       lq_sta->rs_state);
3145 			iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq);
3146 		}
3147 		/* Regardless, ignore this status info for outdated rate */
3148 		return;
3149 	}
3150 
3151 	/* Rate did match, so reset the missed_rate_counter */
3152 	lq_sta->missed_rate_counter = 0;
3153 
3154 	if (!lq_sta->search_better_tbl) {
3155 		curr_tbl = &lq_sta->lq_info[lq_sta->active_tbl];
3156 		other_tbl = &lq_sta->lq_info[rs_search_tbl(lq_sta->active_tbl)];
3157 	} else {
3158 		curr_tbl = &lq_sta->lq_info[rs_search_tbl(lq_sta->active_tbl)];
3159 		other_tbl = &lq_sta->lq_info[lq_sta->active_tbl];
3160 	}
3161 
3162 	if (WARN_ON_ONCE(!rs_rate_column_match(&lq_rate, &curr_tbl->rate))) {
3163 		IWL_DEBUG_RATE(mvm,
3164 			       "Neither active nor search matches tx rate\n");
3165 		tmp_tbl = &lq_sta->lq_info[lq_sta->active_tbl];
3166 		rs_dump_rate(mvm, &tmp_tbl->rate, "ACTIVE");
3167 		tmp_tbl = &lq_sta->lq_info[rs_search_tbl(lq_sta->active_tbl)];
3168 		rs_dump_rate(mvm, &tmp_tbl->rate, "SEARCH");
3169 		rs_dump_rate(mvm, &lq_rate, "ACTUAL");
3170 
3171 		/* no matching table found, let's by-pass the data collection
3172 		 * and continue to perform rate scale to find the rate table
3173 		 */
3174 		rs_stay_in_table(lq_sta, true);
3175 		goto done;
3176 	}
3177 
3178 	/* Updating the frame history depends on whether packets were
3179 	 * aggregated.
3180 	 *
3181 	 * For aggregation, all packets were transmitted at the same rate, the
3182 	 * first index into rate scale table.
3183 	 */
3184 	if (info->flags & IEEE80211_TX_STAT_AMPDU) {
3185 		rs_collect_tpc_data(mvm, lq_sta, curr_tbl, tx_resp_rate.index,
3186 				    info->status.ampdu_len,
3187 				    info->status.ampdu_ack_len,
3188 				    reduced_txp);
3189 
3190 		/* ampdu_ack_len = 0 marks no BA was received. For TLC, treat
3191 		 * it as a single frame loss as we don't want the success ratio
3192 		 * to dip too quickly because a BA wasn't received.
3193 		 * For TPC, there's no need for this optimisation since we want
3194 		 * to recover very quickly from a bad power reduction and,
3195 		 * therefore we'd like the success ratio to get an immediate hit
3196 		 * when failing to get a BA, so we'd switch back to a lower or
3197 		 * zero power reduction. When FW transmits agg with a rate
3198 		 * different from the initial rate, it will not use reduced txp
3199 		 * and will send BA notification twice (one empty with reduced
3200 		 * txp equal to the value from LQ and one with reduced txp 0).
3201 		 * We need to update counters for each txp level accordingly.
3202 		 */
3203 		if (info->status.ampdu_ack_len == 0)
3204 			info->status.ampdu_len = 1;
3205 
3206 		rs_collect_tlc_data(mvm, mvmsta, tid, curr_tbl,
3207 				    tx_resp_rate.index,
3208 				    info->status.ampdu_len,
3209 				    info->status.ampdu_ack_len);
3210 
3211 		/* Update success/fail counts if not searching for new mode */
3212 		if (lq_sta->rs_state == RS_STATE_STAY_IN_COLUMN) {
3213 			lq_sta->total_success += info->status.ampdu_ack_len;
3214 			lq_sta->total_failed += (info->status.ampdu_len -
3215 					info->status.ampdu_ack_len);
3216 		}
3217 	} else {
3218 		/* For legacy, update frame history with for each Tx retry. */
3219 		retries = info->status.rates[0].count - 1;
3220 		/* HW doesn't send more than 15 retries */
3221 		retries = min(retries, 15);
3222 
3223 		/* The last transmission may have been successful */
3224 		legacy_success = !!(info->flags & IEEE80211_TX_STAT_ACK);
3225 		/* Collect data for each rate used during failed TX attempts */
3226 		for (i = 0; i <= retries; ++i) {
3227 			lq_hwrate = le32_to_cpu(table->rs_table[i]);
3228 			if (rs_rate_from_ucode_rate(lq_hwrate, info->band,
3229 						    &lq_rate)) {
3230 				WARN_ON_ONCE(1);
3231 				return;
3232 			}
3233 
3234 			/* Only collect stats if retried rate is in the same RS
3235 			 * table as active/search.
3236 			 */
3237 			if (rs_rate_column_match(&lq_rate, &curr_tbl->rate))
3238 				tmp_tbl = curr_tbl;
3239 			else if (rs_rate_column_match(&lq_rate,
3240 						      &other_tbl->rate))
3241 				tmp_tbl = other_tbl;
3242 			else
3243 				continue;
3244 
3245 			rs_collect_tpc_data(mvm, lq_sta, tmp_tbl,
3246 					    tx_resp_rate.index, 1,
3247 					    i < retries ? 0 : legacy_success,
3248 					    reduced_txp);
3249 			rs_collect_tlc_data(mvm, mvmsta, tid, tmp_tbl,
3250 					    tx_resp_rate.index, 1,
3251 					    i < retries ? 0 : legacy_success);
3252 		}
3253 
3254 		/* Update success/fail counts if not searching for new mode */
3255 		if (lq_sta->rs_state == RS_STATE_STAY_IN_COLUMN) {
3256 			lq_sta->total_success += legacy_success;
3257 			lq_sta->total_failed += retries + (1 - legacy_success);
3258 		}
3259 	}
3260 	/* The last TX rate is cached in lq_sta; it's set in if/else above */
3261 	lq_sta->last_rate_n_flags = lq_hwrate;
3262 	IWL_DEBUG_RATE(mvm, "reduced txpower: %d\n", reduced_txp);
3263 done:
3264 	/* See if there's a better rate or modulation mode to try. */
3265 	if (sta->deflink.supp_rates[info->band])
3266 		rs_rate_scale_perform(mvm, sta, lq_sta, tid, ndp);
3267 }
3268 
iwl_mvm_rs_tx_status(struct iwl_mvm * mvm,struct ieee80211_sta * sta,int tid,struct ieee80211_tx_info * info,bool ndp)3269 void iwl_mvm_rs_tx_status(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
3270 			  int tid, struct ieee80211_tx_info *info, bool ndp)
3271 {
3272 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3273 
3274 	/* If it's locked we are in middle of init flow
3275 	 * just wait for next tx status to update the lq_sta data
3276 	 */
3277 	if (!spin_trylock_bh(&mvmsta->deflink.lq_sta.rs_drv.pers.lock))
3278 		return;
3279 
3280 	__iwl_mvm_rs_tx_status(mvm, sta, tid, info, ndp);
3281 	spin_unlock_bh(&mvmsta->deflink.lq_sta.rs_drv.pers.lock);
3282 }
3283 
3284 #ifdef CONFIG_MAC80211_DEBUGFS
rs_build_rates_table_from_fixed(struct iwl_mvm * mvm,struct iwl_lq_cmd * lq_cmd,enum nl80211_band band,u32 ucode_rate)3285 static void rs_build_rates_table_from_fixed(struct iwl_mvm *mvm,
3286 					    struct iwl_lq_cmd *lq_cmd,
3287 					    enum nl80211_band band,
3288 					    u32 ucode_rate)
3289 {
3290 	struct rs_rate rate;
3291 	int i;
3292 	int num_rates = ARRAY_SIZE(lq_cmd->rs_table);
3293 	__le32 ucode_rate_le32 = cpu_to_le32(ucode_rate);
3294 	u8 ant = (ucode_rate & RATE_MCS_ANT_AB_MSK) >> RATE_MCS_ANT_POS;
3295 
3296 	for (i = 0; i < num_rates; i++)
3297 		lq_cmd->rs_table[i] = ucode_rate_le32;
3298 
3299 	if (rs_rate_from_ucode_rate(ucode_rate, band, &rate)) {
3300 		WARN_ON_ONCE(1);
3301 		return;
3302 	}
3303 
3304 	if (is_mimo(&rate))
3305 		lq_cmd->mimo_delim = num_rates - 1;
3306 	else
3307 		lq_cmd->mimo_delim = 0;
3308 
3309 	lq_cmd->reduced_tpc = 0;
3310 
3311 	if (num_of_ant(ant) == 1)
3312 		lq_cmd->single_stream_ant_msk = ant;
3313 
3314 	if (!mvm->trans->mac_cfg->gen2)
3315 		lq_cmd->agg_frame_cnt_limit = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
3316 	else
3317 		lq_cmd->agg_frame_cnt_limit =
3318 			LINK_QUAL_AGG_FRAME_LIMIT_GEN2_DEF;
3319 }
3320 #endif /* CONFIG_MAC80211_DEBUGFS */
3321 
rs_fill_rates_for_column(struct iwl_mvm * mvm,struct iwl_lq_sta * lq_sta,struct rs_rate * rate,__le32 * rs_table,int * rs_table_index,int num_rates,int num_retries,u8 valid_tx_ant,bool toggle_ant)3322 static void rs_fill_rates_for_column(struct iwl_mvm *mvm,
3323 				     struct iwl_lq_sta *lq_sta,
3324 				     struct rs_rate *rate,
3325 				     __le32 *rs_table, int *rs_table_index,
3326 				     int num_rates, int num_retries,
3327 				     u8 valid_tx_ant, bool toggle_ant)
3328 {
3329 	int i, j;
3330 	__le32 ucode_rate;
3331 	bool bottom_reached = false;
3332 	int prev_rate_idx = rate->index;
3333 	int end = LINK_QUAL_MAX_RETRY_NUM;
3334 	int index = *rs_table_index;
3335 
3336 	for (i = 0; i < num_rates && index < end; i++) {
3337 		for (j = 0; j < num_retries && index < end; j++, index++) {
3338 			ucode_rate = cpu_to_le32(ucode_rate_from_rs_rate(mvm,
3339 									 rate));
3340 			rs_table[index] = ucode_rate;
3341 			if (toggle_ant)
3342 				rs_toggle_antenna(valid_tx_ant, rate);
3343 		}
3344 
3345 		prev_rate_idx = rate->index;
3346 		bottom_reached = rs_get_lower_rate_in_column(lq_sta, rate);
3347 		if (bottom_reached && !is_legacy(rate))
3348 			break;
3349 	}
3350 
3351 	if (!bottom_reached && !is_legacy(rate))
3352 		rate->index = prev_rate_idx;
3353 
3354 	*rs_table_index = index;
3355 }
3356 
3357 /* Building the rate table is non trivial. When we're in MIMO2/VHT/80Mhz/SGI
3358  * column the rate table should look like this:
3359  *
3360  * rate[0] 0x400F019 VHT | ANT: AB BW: 80Mhz MCS: 9 NSS: 2 SGI
3361  * rate[1] 0x400F019 VHT | ANT: AB BW: 80Mhz MCS: 9 NSS: 2 SGI
3362  * rate[2] 0x400F018 VHT | ANT: AB BW: 80Mhz MCS: 8 NSS: 2 SGI
3363  * rate[3] 0x400F018 VHT | ANT: AB BW: 80Mhz MCS: 8 NSS: 2 SGI
3364  * rate[4] 0x400F017 VHT | ANT: AB BW: 80Mhz MCS: 7 NSS: 2 SGI
3365  * rate[5] 0x400F017 VHT | ANT: AB BW: 80Mhz MCS: 7 NSS: 2 SGI
3366  * rate[6] 0x4005007 VHT | ANT: A BW: 80Mhz MCS: 7 NSS: 1 NGI
3367  * rate[7] 0x4009006 VHT | ANT: B BW: 80Mhz MCS: 6 NSS: 1 NGI
3368  * rate[8] 0x4005005 VHT | ANT: A BW: 80Mhz MCS: 5 NSS: 1 NGI
3369  * rate[9] 0x800B Legacy | ANT: B Rate: 36 Mbps
3370  * rate[10] 0x4009 Legacy | ANT: A Rate: 24 Mbps
3371  * rate[11] 0x8007 Legacy | ANT: B Rate: 18 Mbps
3372  * rate[12] 0x4005 Legacy | ANT: A Rate: 12 Mbps
3373  * rate[13] 0x800F Legacy | ANT: B Rate: 9 Mbps
3374  * rate[14] 0x400D Legacy | ANT: A Rate: 6 Mbps
3375  * rate[15] 0x800D Legacy | ANT: B Rate: 6 Mbps
3376  */
rs_build_rates_table(struct iwl_mvm * mvm,struct ieee80211_sta * sta,struct iwl_lq_sta * lq_sta,const struct rs_rate * initial_rate)3377 static void rs_build_rates_table(struct iwl_mvm *mvm,
3378 				 struct ieee80211_sta *sta,
3379 				 struct iwl_lq_sta *lq_sta,
3380 				 const struct rs_rate *initial_rate)
3381 {
3382 	struct rs_rate rate;
3383 	int num_rates, num_retries, index = 0;
3384 	u8 valid_tx_ant = 0;
3385 	struct iwl_lq_cmd *lq_cmd = &lq_sta->lq;
3386 	bool toggle_ant = false;
3387 	u32 color;
3388 
3389 	memcpy(&rate, initial_rate, sizeof(rate));
3390 
3391 	valid_tx_ant = iwl_mvm_get_valid_tx_ant(mvm);
3392 
3393 	/* TODO: remove old API when min FW API hits 14 */
3394 	if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_LQ_SS_PARAMS) &&
3395 	    rs_stbc_allow(mvm, sta, lq_sta))
3396 		rate.stbc = true;
3397 
3398 	if (is_siso(&rate)) {
3399 		num_rates = IWL_MVM_RS_INITIAL_SISO_NUM_RATES;
3400 		num_retries = IWL_MVM_RS_HT_VHT_RETRIES_PER_RATE;
3401 	} else if (is_mimo(&rate)) {
3402 		num_rates = IWL_MVM_RS_INITIAL_MIMO_NUM_RATES;
3403 		num_retries = IWL_MVM_RS_HT_VHT_RETRIES_PER_RATE;
3404 	} else {
3405 		num_rates = IWL_MVM_RS_INITIAL_LEGACY_NUM_RATES;
3406 		num_retries = IWL_MVM_RS_INITIAL_LEGACY_RETRIES;
3407 		toggle_ant = true;
3408 	}
3409 
3410 	rs_fill_rates_for_column(mvm, lq_sta, &rate, lq_cmd->rs_table, &index,
3411 				 num_rates, num_retries, valid_tx_ant,
3412 				 toggle_ant);
3413 
3414 	rs_get_lower_rate_down_column(lq_sta, &rate);
3415 
3416 	if (is_siso(&rate)) {
3417 		num_rates = IWL_MVM_RS_SECONDARY_SISO_NUM_RATES;
3418 		num_retries = IWL_MVM_RS_SECONDARY_SISO_RETRIES;
3419 		lq_cmd->mimo_delim = index;
3420 	} else if (is_legacy(&rate)) {
3421 		num_rates = IWL_MVM_RS_SECONDARY_LEGACY_NUM_RATES;
3422 		num_retries = IWL_MVM_RS_SECONDARY_LEGACY_RETRIES;
3423 	} else {
3424 		WARN_ON_ONCE(1);
3425 	}
3426 
3427 	toggle_ant = true;
3428 
3429 	rs_fill_rates_for_column(mvm, lq_sta, &rate, lq_cmd->rs_table, &index,
3430 				 num_rates, num_retries, valid_tx_ant,
3431 				 toggle_ant);
3432 
3433 	rs_get_lower_rate_down_column(lq_sta, &rate);
3434 
3435 	num_rates = IWL_MVM_RS_SECONDARY_LEGACY_NUM_RATES;
3436 	num_retries = IWL_MVM_RS_SECONDARY_LEGACY_RETRIES;
3437 
3438 	rs_fill_rates_for_column(mvm, lq_sta, &rate, lq_cmd->rs_table, &index,
3439 				 num_rates, num_retries, valid_tx_ant,
3440 				 toggle_ant);
3441 
3442 	/* update the color of the LQ command (as a counter at bits 1-3) */
3443 	color = LQ_FLAGS_COLOR_INC(LQ_FLAG_COLOR_GET(lq_cmd->flags));
3444 	lq_cmd->flags = LQ_FLAG_COLOR_SET(lq_cmd->flags, color);
3445 }
3446 
3447 struct rs_bfer_active_iter_data {
3448 	struct ieee80211_sta *exclude_sta;
3449 	struct iwl_mvm_sta *bfer_mvmsta;
3450 };
3451 
rs_bfer_active_iter(void * _data,struct ieee80211_sta * sta)3452 static void rs_bfer_active_iter(void *_data,
3453 				struct ieee80211_sta *sta)
3454 {
3455 	struct rs_bfer_active_iter_data *data = _data;
3456 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3457 	struct iwl_lq_cmd *lq_cmd = &mvmsta->deflink.lq_sta.rs_drv.lq;
3458 	u32 ss_params = le32_to_cpu(lq_cmd->ss_params);
3459 
3460 	if (sta == data->exclude_sta)
3461 		return;
3462 
3463 	/* The current sta has BFER allowed */
3464 	if (ss_params & LQ_SS_BFER_ALLOWED) {
3465 		WARN_ON_ONCE(data->bfer_mvmsta != NULL);
3466 
3467 		data->bfer_mvmsta = mvmsta;
3468 	}
3469 }
3470 
rs_bfer_priority(struct iwl_mvm_sta * sta)3471 static int rs_bfer_priority(struct iwl_mvm_sta *sta)
3472 {
3473 	int prio = -1;
3474 	enum nl80211_iftype viftype = ieee80211_vif_type_p2p(sta->vif);
3475 
3476 	switch (viftype) {
3477 	case NL80211_IFTYPE_AP:
3478 	case NL80211_IFTYPE_P2P_GO:
3479 		prio = 3;
3480 		break;
3481 	case NL80211_IFTYPE_P2P_CLIENT:
3482 		prio = 2;
3483 		break;
3484 	case NL80211_IFTYPE_STATION:
3485 		prio = 1;
3486 		break;
3487 	default:
3488 		WARN_ONCE(true, "viftype %d sta_id %d", viftype,
3489 			  sta->deflink.sta_id);
3490 		prio = -1;
3491 	}
3492 
3493 	return prio;
3494 }
3495 
3496 /* Returns >0 if sta1 has a higher BFER priority compared to sta2 */
rs_bfer_priority_cmp(struct iwl_mvm_sta * sta1,struct iwl_mvm_sta * sta2)3497 static int rs_bfer_priority_cmp(struct iwl_mvm_sta *sta1,
3498 				struct iwl_mvm_sta *sta2)
3499 {
3500 	int prio1 = rs_bfer_priority(sta1);
3501 	int prio2 = rs_bfer_priority(sta2);
3502 
3503 	if (prio1 > prio2)
3504 		return 1;
3505 	if (prio1 < prio2)
3506 		return -1;
3507 	return 0;
3508 }
3509 
rs_set_lq_ss_params(struct iwl_mvm * mvm,struct ieee80211_sta * sta,struct iwl_lq_sta * lq_sta,const struct rs_rate * initial_rate)3510 static void rs_set_lq_ss_params(struct iwl_mvm *mvm,
3511 				struct ieee80211_sta *sta,
3512 				struct iwl_lq_sta *lq_sta,
3513 				const struct rs_rate *initial_rate)
3514 {
3515 	struct iwl_lq_cmd *lq_cmd = &lq_sta->lq;
3516 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3517 	struct rs_bfer_active_iter_data data = {
3518 		.exclude_sta = sta,
3519 		.bfer_mvmsta = NULL,
3520 	};
3521 	struct iwl_mvm_sta *bfer_mvmsta = NULL;
3522 	u32 ss_params = LQ_SS_PARAMS_VALID;
3523 
3524 	if (!iwl_mvm_bt_coex_is_mimo_allowed(mvm, sta))
3525 		goto out;
3526 
3527 #ifdef CONFIG_MAC80211_DEBUGFS
3528 	/* Check if forcing the decision is configured.
3529 	 * Note that SISO is forced by not allowing STBC or BFER
3530 	 */
3531 	if (lq_sta->pers.ss_force == RS_SS_FORCE_STBC)
3532 		ss_params |= (LQ_SS_STBC_1SS_ALLOWED | LQ_SS_FORCE);
3533 	else if (lq_sta->pers.ss_force == RS_SS_FORCE_BFER)
3534 		ss_params |= (LQ_SS_BFER_ALLOWED | LQ_SS_FORCE);
3535 
3536 	if (lq_sta->pers.ss_force != RS_SS_FORCE_NONE) {
3537 		IWL_DEBUG_RATE(mvm, "Forcing single stream Tx decision %d\n",
3538 			       lq_sta->pers.ss_force);
3539 		goto out;
3540 	}
3541 #endif
3542 
3543 	if (lq_sta->stbc_capable)
3544 		ss_params |= LQ_SS_STBC_1SS_ALLOWED;
3545 
3546 	if (!lq_sta->bfer_capable)
3547 		goto out;
3548 
3549 	ieee80211_iterate_stations_atomic(mvm->hw,
3550 					  rs_bfer_active_iter,
3551 					  &data);
3552 	bfer_mvmsta = data.bfer_mvmsta;
3553 
3554 	/* This code is safe as it doesn't run concurrently for different
3555 	 * stations. This is guaranteed by the fact that calls to
3556 	 * ieee80211_tx_status wouldn't run concurrently for a single HW.
3557 	 */
3558 	if (!bfer_mvmsta) {
3559 		IWL_DEBUG_RATE(mvm, "No sta with BFER allowed found. Allow\n");
3560 
3561 		ss_params |= LQ_SS_BFER_ALLOWED;
3562 		goto out;
3563 	}
3564 
3565 	IWL_DEBUG_RATE(mvm, "Found existing sta %d with BFER activated\n",
3566 		       bfer_mvmsta->deflink.sta_id);
3567 
3568 	/* Disallow BFER on another STA if active and we're a higher priority */
3569 	if (rs_bfer_priority_cmp(mvmsta, bfer_mvmsta) > 0) {
3570 		struct iwl_lq_cmd *bfersta_lq_cmd =
3571 			&bfer_mvmsta->deflink.lq_sta.rs_drv.lq;
3572 		u32 bfersta_ss_params = le32_to_cpu(bfersta_lq_cmd->ss_params);
3573 
3574 		bfersta_ss_params &= ~LQ_SS_BFER_ALLOWED;
3575 		bfersta_lq_cmd->ss_params = cpu_to_le32(bfersta_ss_params);
3576 		iwl_mvm_send_lq_cmd(mvm, bfersta_lq_cmd);
3577 
3578 		ss_params |= LQ_SS_BFER_ALLOWED;
3579 		IWL_DEBUG_RATE(mvm,
3580 			       "Lower priority BFER sta found (%d). Switch BFER\n",
3581 			       bfer_mvmsta->deflink.sta_id);
3582 	}
3583 out:
3584 	lq_cmd->ss_params = cpu_to_le32(ss_params);
3585 }
3586 
rs_fill_lq_cmd(struct iwl_mvm * mvm,struct ieee80211_sta * sta,struct iwl_lq_sta * lq_sta,const struct rs_rate * initial_rate)3587 static void rs_fill_lq_cmd(struct iwl_mvm *mvm,
3588 			   struct ieee80211_sta *sta,
3589 			   struct iwl_lq_sta *lq_sta,
3590 			   const struct rs_rate *initial_rate)
3591 {
3592 	struct iwl_lq_cmd *lq_cmd = &lq_sta->lq;
3593 	struct iwl_mvm_sta *mvmsta;
3594 	struct iwl_mvm_vif *mvmvif;
3595 
3596 	lq_cmd->agg_disable_start_th = IWL_MVM_RS_AGG_DISABLE_START;
3597 	lq_cmd->agg_time_limit =
3598 		cpu_to_le16(IWL_MVM_RS_AGG_TIME_LIMIT);
3599 
3600 #ifdef CONFIG_MAC80211_DEBUGFS
3601 	if (lq_sta->pers.dbg_fixed_rate) {
3602 		rs_build_rates_table_from_fixed(mvm, lq_cmd,
3603 						lq_sta->band,
3604 						lq_sta->pers.dbg_fixed_rate);
3605 		return;
3606 	}
3607 #endif
3608 	if (WARN_ON_ONCE(!sta || !initial_rate))
3609 		return;
3610 
3611 	rs_build_rates_table(mvm, sta, lq_sta, initial_rate);
3612 
3613 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_LQ_SS_PARAMS))
3614 		rs_set_lq_ss_params(mvm, sta, lq_sta, initial_rate);
3615 
3616 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
3617 	mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif);
3618 
3619 	if (!fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_COEX_SCHEMA_2) &&
3620 	    num_of_ant(initial_rate->ant) == 1)
3621 		lq_cmd->single_stream_ant_msk = initial_rate->ant;
3622 
3623 	lq_cmd->agg_frame_cnt_limit = lq_sta->pers.max_agg_bufsize;
3624 
3625 	/*
3626 	 * In case of low latency, tell the firmware to leave a frame in the
3627 	 * Tx Fifo so that it can start a transaction in the same TxOP. This
3628 	 * basically allows the firmware to send bursts.
3629 	 */
3630 	if (iwl_mvm_vif_low_latency(mvmvif))
3631 		lq_cmd->agg_frame_cnt_limit--;
3632 
3633 	if (mvmsta->vif->p2p)
3634 		lq_cmd->flags |= LQ_FLAG_USE_RTS_MSK;
3635 
3636 	lq_cmd->agg_time_limit =
3637 			cpu_to_le16(iwl_mvm_coex_agg_time_limit(mvm, sta));
3638 }
3639 
rs_alloc(struct ieee80211_hw * hw)3640 static void *rs_alloc(struct ieee80211_hw *hw)
3641 {
3642 	return hw->priv;
3643 }
3644 
3645 /* rate scale requires free function to be implemented */
rs_free(void * mvm_rate)3646 static void rs_free(void *mvm_rate)
3647 {
3648 	return;
3649 }
3650 
rs_free_sta(void * mvm_r,struct ieee80211_sta * sta,void * mvm_sta)3651 static void rs_free_sta(void *mvm_r, struct ieee80211_sta *sta, void *mvm_sta)
3652 {
3653 	struct iwl_op_mode *op_mode __maybe_unused = mvm_r;
3654 	struct iwl_mvm *mvm __maybe_unused = IWL_OP_MODE_GET_MVM(op_mode);
3655 
3656 	IWL_DEBUG_RATE(mvm, "enter\n");
3657 	IWL_DEBUG_RATE(mvm, "leave\n");
3658 }
3659 
rs_pretty_print_rate_v1(char * buf,int bufsz,const u32 rate)3660 int rs_pretty_print_rate_v1(char *buf, int bufsz, const u32 rate)
3661 {
3662 
3663 	char *type;
3664 	u8 mcs = 0, nss = 0;
3665 	u8 ant = (rate & RATE_MCS_ANT_AB_MSK) >> RATE_MCS_ANT_POS;
3666 	u32 bw = (rate & RATE_MCS_CHAN_WIDTH_MSK_V1) >>
3667 		RATE_MCS_CHAN_WIDTH_POS;
3668 
3669 	if (!(rate & RATE_MCS_HT_MSK_V1) &&
3670 	    !(rate & RATE_MCS_VHT_MSK_V1) &&
3671 	    !(rate & RATE_MCS_HE_MSK_V1)) {
3672 		int index = iwl_hwrate_to_plcp_idx(rate);
3673 
3674 		return scnprintf(buf, bufsz, "Legacy | ANT: %s Rate: %s Mbps",
3675 				 iwl_rs_pretty_ant(ant),
3676 				 index == IWL_RATE_INVALID ? "BAD" :
3677 				 iwl_rate_mcs(index)->mbps);
3678 	}
3679 
3680 	if (rate & RATE_MCS_VHT_MSK_V1) {
3681 		type = "VHT";
3682 		mcs = rate & RATE_VHT_MCS_RATE_CODE_MSK;
3683 		nss = FIELD_GET(RATE_VHT_MCS_NSS_MSK, rate) + 1;
3684 	} else if (rate & RATE_MCS_HT_MSK_V1) {
3685 		type = "HT";
3686 		mcs = rate & RATE_HT_MCS_INDEX_MSK_V1;
3687 		nss = FIELD_GET(RATE_HT_MCS_MIMO2_MSK, rate) + 1;
3688 	} else if (rate & RATE_MCS_HE_MSK_V1) {
3689 		type = "HE";
3690 		mcs = rate & RATE_VHT_MCS_RATE_CODE_MSK;
3691 		nss = FIELD_GET(RATE_VHT_MCS_NSS_MSK, rate) + 1;
3692 	} else {
3693 		type = "Unknown"; /* shouldn't happen */
3694 	}
3695 
3696 	return scnprintf(buf, bufsz,
3697 			 "0x%x: %s | ANT: %s BW: %s MCS: %d NSS: %d %s%s%s%s%s",
3698 			 rate, type, iwl_rs_pretty_ant(ant), iwl_rs_pretty_bw(bw), mcs, nss,
3699 			 (rate & RATE_MCS_SGI_MSK_V1) ? "SGI " : "NGI ",
3700 			 (rate & RATE_MCS_STBC_MSK) ? "STBC " : "",
3701 			 (rate & RATE_MCS_LDPC_MSK_V1) ? "LDPC " : "",
3702 			 (rate & RATE_HE_DUAL_CARRIER_MODE_MSK) ? "DCM " : "",
3703 			 (rate & RATE_MCS_BF_MSK) ? "BF " : "");
3704 }
3705 
3706 #ifdef CONFIG_MAC80211_DEBUGFS
3707 /*
3708  * Program the device to use fixed rate for frame transmit
3709  * This is for debugging/testing only
3710  * once the device start use fixed rate, we need to reload the module
3711  * to being back the normal operation.
3712  */
rs_program_fix_rate(struct iwl_mvm * mvm,struct iwl_lq_sta * lq_sta)3713 static void rs_program_fix_rate(struct iwl_mvm *mvm,
3714 				struct iwl_lq_sta *lq_sta)
3715 {
3716 	lq_sta->active_legacy_rate = 0x0FFF;	/* 1 - 54 MBits, includes CCK */
3717 	lq_sta->active_siso_rate   = 0x1FD0;	/* 6 - 60 MBits, no 9, no CCK */
3718 	lq_sta->active_mimo2_rate  = 0x1FD0;	/* 6 - 60 MBits, no 9, no CCK */
3719 
3720 	IWL_DEBUG_RATE(mvm, "sta_id %d rate 0x%X\n",
3721 		       lq_sta->lq.sta_id, lq_sta->pers.dbg_fixed_rate);
3722 
3723 	if (lq_sta->pers.dbg_fixed_rate) {
3724 		rs_fill_lq_cmd(mvm, NULL, lq_sta, NULL);
3725 		iwl_mvm_send_lq_cmd(lq_sta->pers.drv, &lq_sta->lq);
3726 	}
3727 }
3728 
rs_sta_dbgfs_scale_table_write(struct file * file,const char __user * user_buf,size_t count,loff_t * ppos)3729 static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
3730 			const char __user *user_buf, size_t count, loff_t *ppos)
3731 {
3732 	struct iwl_lq_sta *lq_sta = file->private_data;
3733 	struct iwl_mvm *mvm;
3734 	char buf[64];
3735 	size_t buf_size;
3736 	u32 parsed_rate;
3737 
3738 	mvm = lq_sta->pers.drv;
3739 	memset(buf, 0, sizeof(buf));
3740 	buf_size = min(count, sizeof(buf) -  1);
3741 	if (copy_from_user(buf, user_buf, buf_size))
3742 		return -EFAULT;
3743 
3744 	if (sscanf(buf, "%x", &parsed_rate) == 1)
3745 		lq_sta->pers.dbg_fixed_rate = parsed_rate;
3746 	else
3747 		lq_sta->pers.dbg_fixed_rate = 0;
3748 
3749 	rs_program_fix_rate(mvm, lq_sta);
3750 
3751 	return count;
3752 }
3753 
rs_sta_dbgfs_scale_table_read(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)3754 static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
3755 			char __user *user_buf, size_t count, loff_t *ppos)
3756 {
3757 	char *buff;
3758 	int desc = 0;
3759 	int i = 0;
3760 	ssize_t ret;
3761 	static const size_t bufsz = 2048;
3762 
3763 	struct iwl_lq_sta *lq_sta = file->private_data;
3764 	struct iwl_mvm_sta *mvmsta =
3765 		container_of(lq_sta, struct iwl_mvm_sta, deflink.lq_sta.rs_drv);
3766 	struct iwl_mvm *mvm;
3767 	struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
3768 	struct rs_rate *rate = &tbl->rate;
3769 	u32 ss_params;
3770 
3771 	mvm = lq_sta->pers.drv;
3772 	buff = kmalloc(bufsz, GFP_KERNEL);
3773 	if (!buff)
3774 		return -ENOMEM;
3775 
3776 	desc += scnprintf(buff + desc, bufsz - desc,
3777 			  "sta_id %d\n", lq_sta->lq.sta_id);
3778 	desc += scnprintf(buff + desc, bufsz - desc,
3779 			  "failed=%d success=%d rate=0%lX\n",
3780 			  lq_sta->total_failed, lq_sta->total_success,
3781 			  lq_sta->active_legacy_rate);
3782 	desc += scnprintf(buff + desc, bufsz - desc, "fixed rate 0x%X\n",
3783 			  lq_sta->pers.dbg_fixed_rate);
3784 	desc += scnprintf(buff + desc, bufsz - desc, "valid_tx_ant %s%s\n",
3785 	    (iwl_mvm_get_valid_tx_ant(mvm) & ANT_A) ? "ANT_A," : "",
3786 	    (iwl_mvm_get_valid_tx_ant(mvm) & ANT_B) ? "ANT_B," : "");
3787 	desc += scnprintf(buff + desc, bufsz - desc, "lq type %s\n",
3788 			  (is_legacy(rate)) ? "legacy" :
3789 			  is_vht(rate) ? "VHT" : "HT");
3790 	if (!is_legacy(rate)) {
3791 		desc += scnprintf(buff + desc, bufsz - desc, " %s",
3792 		   (is_siso(rate)) ? "SISO" : "MIMO2");
3793 		desc += scnprintf(buff + desc, bufsz - desc, " %s",
3794 				(is_ht20(rate)) ? "20MHz" :
3795 				(is_ht40(rate)) ? "40MHz" :
3796 				(is_ht80(rate)) ? "80MHz" :
3797 				(is_ht160(rate)) ? "160MHz" : "BAD BW");
3798 		desc += scnprintf(buff + desc, bufsz - desc, " %s %s %s %s\n",
3799 				(rate->sgi) ? "SGI" : "NGI",
3800 				(rate->ldpc) ? "LDPC" : "BCC",
3801 				(lq_sta->is_agg) ? "AGG on" : "",
3802 				(mvmsta->amsdu_enabled) ? "AMSDU on" : "");
3803 	}
3804 	desc += scnprintf(buff + desc, bufsz - desc, "last tx rate=0x%X\n",
3805 			lq_sta->last_rate_n_flags);
3806 	desc += scnprintf(buff + desc, bufsz - desc,
3807 			"general: flags=0x%X mimo-d=%d s-ant=0x%x d-ant=0x%x\n",
3808 			lq_sta->lq.flags,
3809 			lq_sta->lq.mimo_delim,
3810 			lq_sta->lq.single_stream_ant_msk,
3811 			lq_sta->lq.dual_stream_ant_msk);
3812 
3813 	desc += scnprintf(buff + desc, bufsz - desc,
3814 			"agg: time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
3815 			le16_to_cpu(lq_sta->lq.agg_time_limit),
3816 			lq_sta->lq.agg_disable_start_th,
3817 			lq_sta->lq.agg_frame_cnt_limit);
3818 
3819 	desc += scnprintf(buff + desc, bufsz - desc, "reduced tpc=%d\n",
3820 			  lq_sta->lq.reduced_tpc);
3821 	ss_params = le32_to_cpu(lq_sta->lq.ss_params);
3822 	desc += scnprintf(buff + desc, bufsz - desc,
3823 			"single stream params: %s%s%s%s\n",
3824 			(ss_params & LQ_SS_PARAMS_VALID) ?
3825 			"VALID" : "INVALID",
3826 			(ss_params & LQ_SS_BFER_ALLOWED) ?
3827 			", BFER" : "",
3828 			(ss_params & LQ_SS_STBC_1SS_ALLOWED) ?
3829 			", STBC" : "",
3830 			(ss_params & LQ_SS_FORCE) ?
3831 			", FORCE" : "");
3832 	desc += scnprintf(buff + desc, bufsz - desc,
3833 			"Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
3834 			lq_sta->lq.initial_rate_index[0],
3835 			lq_sta->lq.initial_rate_index[1],
3836 			lq_sta->lq.initial_rate_index[2],
3837 			lq_sta->lq.initial_rate_index[3]);
3838 
3839 	for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
3840 		u32 r = le32_to_cpu(lq_sta->lq.rs_table[i]);
3841 
3842 		desc += scnprintf(buff + desc, bufsz - desc,
3843 				  " rate[%d] 0x%X ", i, r);
3844 		desc += rs_pretty_print_rate_v1(buff + desc, bufsz - desc, r);
3845 		if (desc < bufsz - 1)
3846 			buff[desc++] = '\n';
3847 	}
3848 
3849 	ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3850 	kfree(buff);
3851 	return ret;
3852 }
3853 
3854 static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
3855 	.write = rs_sta_dbgfs_scale_table_write,
3856 	.read = rs_sta_dbgfs_scale_table_read,
3857 	.open = simple_open,
3858 	.llseek = default_llseek,
3859 };
rs_sta_dbgfs_stats_table_read(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)3860 static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
3861 			char __user *user_buf, size_t count, loff_t *ppos)
3862 {
3863 	char *buff;
3864 	int desc = 0;
3865 	int i, j;
3866 	ssize_t ret;
3867 	struct iwl_scale_tbl_info *tbl;
3868 	struct rs_rate *rate;
3869 	struct iwl_lq_sta *lq_sta = file->private_data;
3870 
3871 	buff = kmalloc(1024, GFP_KERNEL);
3872 	if (!buff)
3873 		return -ENOMEM;
3874 
3875 	for (i = 0; i < LQ_SIZE; i++) {
3876 		tbl = &(lq_sta->lq_info[i]);
3877 		rate = &tbl->rate;
3878 		desc += sprintf(buff+desc,
3879 				"%s type=%d SGI=%d BW=%s DUP=0\n"
3880 				"index=%d\n",
3881 				lq_sta->active_tbl == i ? "*" : "x",
3882 				rate->type,
3883 				rate->sgi,
3884 				is_ht20(rate) ? "20MHz" :
3885 				is_ht40(rate) ? "40MHz" :
3886 				is_ht80(rate) ? "80MHz" :
3887 				is_ht160(rate) ? "160MHz" : "ERR",
3888 				rate->index);
3889 		for (j = 0; j < IWL_RATE_COUNT; j++) {
3890 			desc += sprintf(buff+desc,
3891 				"counter=%d success=%d %%=%d\n",
3892 				tbl->win[j].counter,
3893 				tbl->win[j].success_counter,
3894 				tbl->win[j].success_ratio);
3895 		}
3896 	}
3897 	ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3898 	kfree(buff);
3899 	return ret;
3900 }
3901 
3902 static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
3903 	.read = rs_sta_dbgfs_stats_table_read,
3904 	.open = simple_open,
3905 	.llseek = default_llseek,
3906 };
3907 
rs_sta_dbgfs_drv_tx_stats_read(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)3908 static ssize_t rs_sta_dbgfs_drv_tx_stats_read(struct file *file,
3909 					      char __user *user_buf,
3910 					      size_t count, loff_t *ppos)
3911 {
3912 	static const char * const column_name[] = {
3913 		[RS_COLUMN_LEGACY_ANT_A] = "LEGACY_ANT_A",
3914 		[RS_COLUMN_LEGACY_ANT_B] = "LEGACY_ANT_B",
3915 		[RS_COLUMN_SISO_ANT_A] = "SISO_ANT_A",
3916 		[RS_COLUMN_SISO_ANT_B] = "SISO_ANT_B",
3917 		[RS_COLUMN_SISO_ANT_A_SGI] = "SISO_ANT_A_SGI",
3918 		[RS_COLUMN_SISO_ANT_B_SGI] = "SISO_ANT_B_SGI",
3919 		[RS_COLUMN_MIMO2] = "MIMO2",
3920 		[RS_COLUMN_MIMO2_SGI] = "MIMO2_SGI",
3921 	};
3922 
3923 	static const char * const rate_name[] = {
3924 		[IWL_RATE_1M_INDEX] = "1M",
3925 		[IWL_RATE_2M_INDEX] = "2M",
3926 		[IWL_RATE_5M_INDEX] = "5.5M",
3927 		[IWL_RATE_11M_INDEX] = "11M",
3928 		[IWL_RATE_6M_INDEX] = "6M|MCS0",
3929 		[IWL_RATE_9M_INDEX] = "9M",
3930 		[IWL_RATE_12M_INDEX] = "12M|MCS1",
3931 		[IWL_RATE_18M_INDEX] = "18M|MCS2",
3932 		[IWL_RATE_24M_INDEX] = "24M|MCS3",
3933 		[IWL_RATE_36M_INDEX] = "36M|MCS4",
3934 		[IWL_RATE_48M_INDEX] = "48M|MCS5",
3935 		[IWL_RATE_54M_INDEX] = "54M|MCS6",
3936 		[IWL_RATE_MCS_7_INDEX] = "MCS7",
3937 		[IWL_RATE_MCS_8_INDEX] = "MCS8",
3938 		[IWL_RATE_MCS_9_INDEX] = "MCS9",
3939 		[IWL_RATE_MCS_10_INDEX] = "MCS10",
3940 		[IWL_RATE_MCS_11_INDEX] = "MCS11",
3941 	};
3942 
3943 	char *buff, *pos, *endpos;
3944 	int col, rate;
3945 	ssize_t ret;
3946 	struct iwl_lq_sta *lq_sta = file->private_data;
3947 	struct rs_rate_stats *stats;
3948 	static const size_t bufsz = 1024;
3949 
3950 	buff = kmalloc(bufsz, GFP_KERNEL);
3951 	if (!buff)
3952 		return -ENOMEM;
3953 
3954 	pos = buff;
3955 	endpos = pos + bufsz;
3956 
3957 	pos += scnprintf(pos, endpos - pos, "COLUMN,");
3958 	for (rate = 0; rate < IWL_RATE_COUNT; rate++)
3959 		pos += scnprintf(pos, endpos - pos, "%s,", rate_name[rate]);
3960 	pos += scnprintf(pos, endpos - pos, "\n");
3961 
3962 	for (col = 0; col < RS_COLUMN_COUNT; col++) {
3963 		pos += scnprintf(pos, endpos - pos,
3964 				 "%s,", column_name[col]);
3965 
3966 		for (rate = 0; rate < IWL_RATE_COUNT; rate++) {
3967 			stats = &(lq_sta->pers.tx_stats[col][rate]);
3968 			pos += scnprintf(pos, endpos - pos,
3969 					 "%llu/%llu,",
3970 					 stats->success,
3971 					 stats->total);
3972 		}
3973 		pos += scnprintf(pos, endpos - pos, "\n");
3974 	}
3975 
3976 	ret = simple_read_from_buffer(user_buf, count, ppos, buff, pos - buff);
3977 	kfree(buff);
3978 	return ret;
3979 }
3980 
rs_sta_dbgfs_drv_tx_stats_write(struct file * file,const char __user * user_buf,size_t count,loff_t * ppos)3981 static ssize_t rs_sta_dbgfs_drv_tx_stats_write(struct file *file,
3982 					       const char __user *user_buf,
3983 					       size_t count, loff_t *ppos)
3984 {
3985 	struct iwl_lq_sta *lq_sta = file->private_data;
3986 	memset(lq_sta->pers.tx_stats, 0, sizeof(lq_sta->pers.tx_stats));
3987 
3988 	return count;
3989 }
3990 
3991 static const struct file_operations rs_sta_dbgfs_drv_tx_stats_ops = {
3992 	.read = rs_sta_dbgfs_drv_tx_stats_read,
3993 	.write = rs_sta_dbgfs_drv_tx_stats_write,
3994 	.open = simple_open,
3995 	.llseek = default_llseek,
3996 };
3997 
iwl_dbgfs_ss_force_read(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)3998 static ssize_t iwl_dbgfs_ss_force_read(struct file *file,
3999 				       char __user *user_buf,
4000 				       size_t count, loff_t *ppos)
4001 {
4002 	struct iwl_lq_sta *lq_sta = file->private_data;
4003 	char buf[12];
4004 	int bufsz = sizeof(buf);
4005 	int pos = 0;
4006 	static const char * const ss_force_name[] = {
4007 		[RS_SS_FORCE_NONE] = "none",
4008 		[RS_SS_FORCE_STBC] = "stbc",
4009 		[RS_SS_FORCE_BFER] = "bfer",
4010 		[RS_SS_FORCE_SISO] = "siso",
4011 	};
4012 
4013 	pos += scnprintf(buf+pos, bufsz-pos, "%s\n",
4014 			 ss_force_name[lq_sta->pers.ss_force]);
4015 	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
4016 }
4017 
iwl_dbgfs_ss_force_write(struct iwl_lq_sta * lq_sta,char * buf,size_t count,loff_t * ppos)4018 static ssize_t iwl_dbgfs_ss_force_write(struct iwl_lq_sta *lq_sta, char *buf,
4019 					size_t count, loff_t *ppos)
4020 {
4021 	struct iwl_mvm *mvm = lq_sta->pers.drv;
4022 	int ret = 0;
4023 
4024 	if (!strncmp("none", buf, 4)) {
4025 		lq_sta->pers.ss_force = RS_SS_FORCE_NONE;
4026 	} else if (!strncmp("siso", buf, 4)) {
4027 		lq_sta->pers.ss_force = RS_SS_FORCE_SISO;
4028 	} else if (!strncmp("stbc", buf, 4)) {
4029 		if (lq_sta->stbc_capable) {
4030 			lq_sta->pers.ss_force = RS_SS_FORCE_STBC;
4031 		} else {
4032 			IWL_ERR(mvm,
4033 				"can't force STBC. peer doesn't support\n");
4034 			ret = -EINVAL;
4035 		}
4036 	} else if (!strncmp("bfer", buf, 4)) {
4037 		if (lq_sta->bfer_capable) {
4038 			lq_sta->pers.ss_force = RS_SS_FORCE_BFER;
4039 		} else {
4040 			IWL_ERR(mvm,
4041 				"can't force BFER. peer doesn't support\n");
4042 			ret = -EINVAL;
4043 		}
4044 	} else {
4045 		IWL_ERR(mvm, "valid values none|siso|stbc|bfer\n");
4046 		ret = -EINVAL;
4047 	}
4048 	return ret ?: count;
4049 }
4050 
4051 #define MVM_DEBUGFS_READ_WRITE_FILE_OPS(name, bufsz) \
4052 	_MVM_DEBUGFS_READ_WRITE_FILE_OPS(name, bufsz, struct iwl_lq_sta)
4053 #define MVM_DEBUGFS_ADD_FILE_RS(name, parent, mode) do {		\
4054 		debugfs_create_file(#name, mode, parent, lq_sta,	\
4055 				    &iwl_dbgfs_##name##_ops);		\
4056 	} while (0)
4057 
4058 MVM_DEBUGFS_READ_WRITE_FILE_OPS(ss_force, 32);
4059 
rs_drv_add_sta_debugfs(void * mvm,void * priv_sta,struct dentry * dir)4060 static void rs_drv_add_sta_debugfs(void *mvm, void *priv_sta,
4061 				   struct dentry *dir)
4062 {
4063 	struct iwl_lq_sta *lq_sta = priv_sta;
4064 	struct iwl_mvm_sta *mvmsta;
4065 
4066 	mvmsta = container_of(lq_sta, struct iwl_mvm_sta,
4067 			      deflink.lq_sta.rs_drv);
4068 
4069 	if (!mvmsta->vif)
4070 		return;
4071 
4072 	debugfs_create_file("rate_scale_table", 0600, dir,
4073 			    lq_sta, &rs_sta_dbgfs_scale_table_ops);
4074 	debugfs_create_file("rate_stats_table", 0400, dir,
4075 			    lq_sta, &rs_sta_dbgfs_stats_table_ops);
4076 	debugfs_create_file("drv_tx_stats", 0600, dir,
4077 			    lq_sta, &rs_sta_dbgfs_drv_tx_stats_ops);
4078 	debugfs_create_u8("tx_agg_tid_enable", 0600, dir,
4079 			  &lq_sta->tx_agg_tid_en);
4080 	debugfs_create_u8("reduced_tpc", 0600, dir,
4081 			  &lq_sta->pers.dbg_fixed_txp_reduction);
4082 
4083 	MVM_DEBUGFS_ADD_FILE_RS(ss_force, dir, 0600);
4084 }
4085 #endif
4086 
4087 /*
4088  * Initialization of rate scaling information is done by driver after
4089  * the station is added. Since mac80211 calls this function before a
4090  * station is added we ignore it.
4091  */
rs_rate_init_ops(void * mvm_r,struct ieee80211_supported_band * sband,struct cfg80211_chan_def * chandef,struct ieee80211_sta * sta,void * mvm_sta)4092 static void rs_rate_init_ops(void *mvm_r,
4093 			     struct ieee80211_supported_band *sband,
4094 			     struct cfg80211_chan_def *chandef,
4095 			     struct ieee80211_sta *sta, void *mvm_sta)
4096 {
4097 }
4098 
4099 /* ops for rate scaling implemented in the driver */
4100 static const struct rate_control_ops rs_mvm_ops_drv = {
4101 	.name = RS_NAME,
4102 	.tx_status = rs_drv_mac80211_tx_status,
4103 	.get_rate = rs_drv_get_rate,
4104 	.rate_init = rs_rate_init_ops,
4105 	.alloc = rs_alloc,
4106 	.free = rs_free,
4107 	.alloc_sta = rs_drv_alloc_sta,
4108 	.free_sta = rs_free_sta,
4109 	.rate_update = rs_drv_rate_update,
4110 #ifdef CONFIG_MAC80211_DEBUGFS
4111 	.add_sta_debugfs = rs_drv_add_sta_debugfs,
4112 #endif
4113 	.capa = RATE_CTRL_CAPA_VHT_EXT_NSS_BW,
4114 };
4115 
iwl_mvm_rs_rate_init(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_bss_conf * link_conf,struct ieee80211_link_sta * link_sta,enum nl80211_band band)4116 void iwl_mvm_rs_rate_init(struct iwl_mvm *mvm,
4117 			  struct ieee80211_vif *vif,
4118 			  struct ieee80211_sta *sta,
4119 			  struct ieee80211_bss_conf *link_conf,
4120 			  struct ieee80211_link_sta *link_sta,
4121 			  enum nl80211_band band)
4122 {
4123 	if (iwl_mvm_has_tlc_offload(mvm)) {
4124 		iwl_mvm_rs_fw_rate_init(mvm, vif, sta, link_conf,
4125 					link_sta, band);
4126 	} else {
4127 		struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
4128 
4129 		spin_lock_bh(&mvmsta->deflink.lq_sta.rs_drv.pers.lock);
4130 		rs_drv_rate_init(mvm, sta, band);
4131 		spin_unlock_bh(&mvmsta->deflink.lq_sta.rs_drv.pers.lock);
4132 	}
4133 }
4134 
iwl_mvm_rate_control_register(void)4135 int iwl_mvm_rate_control_register(void)
4136 {
4137 	return ieee80211_rate_control_register(&rs_mvm_ops_drv);
4138 }
4139 
iwl_mvm_rate_control_unregister(void)4140 void iwl_mvm_rate_control_unregister(void)
4141 {
4142 	ieee80211_rate_control_unregister(&rs_mvm_ops_drv);
4143 }
4144 
rs_drv_tx_protection(struct iwl_mvm * mvm,struct iwl_mvm_sta * mvmsta,bool enable)4145 static int rs_drv_tx_protection(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
4146 				bool enable)
4147 {
4148 	struct iwl_lq_cmd *lq = &mvmsta->deflink.lq_sta.rs_drv.lq;
4149 
4150 	lockdep_assert_held(&mvm->mutex);
4151 
4152 	if (enable) {
4153 		if (mvmsta->tx_protection == 0)
4154 			lq->flags |= LQ_FLAG_USE_RTS_MSK;
4155 		mvmsta->tx_protection++;
4156 	} else {
4157 		mvmsta->tx_protection--;
4158 		if (mvmsta->tx_protection == 0)
4159 			lq->flags &= ~LQ_FLAG_USE_RTS_MSK;
4160 	}
4161 
4162 	return iwl_mvm_send_lq_cmd(mvm, lq);
4163 }
4164 
4165 /**
4166  * iwl_mvm_tx_protection - ask FW to enable RTS/CTS protection
4167  * @mvm: The mvm component
4168  * @mvmsta: The station
4169  * @enable: Enable Tx protection?
4170  *
4171  * Returns: an error code
4172  */
iwl_mvm_tx_protection(struct iwl_mvm * mvm,struct iwl_mvm_sta * mvmsta,bool enable)4173 int iwl_mvm_tx_protection(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
4174 			  bool enable)
4175 {
4176 	if (iwl_mvm_has_tlc_offload(mvm))
4177 		return rs_fw_tx_protection(mvm, mvmsta, enable);
4178 	else
4179 		return rs_drv_tx_protection(mvm, mvmsta, enable);
4180 }
4181 
iwl_legacy_rate_to_fw_idx(u32 rate_n_flags)4182 static u32 iwl_legacy_rate_to_fw_idx(u32 rate_n_flags)
4183 {
4184 	int rate = rate_n_flags & RATE_LEGACY_RATE_MSK_V1;
4185 	int idx;
4186 	bool ofdm = !(rate_n_flags & RATE_MCS_CCK_MSK_V1);
4187 	int offset = ofdm ? IWL_FIRST_OFDM_RATE : 0;
4188 	int last = ofdm ? IWL_RATE_COUNT_LEGACY : IWL_FIRST_OFDM_RATE;
4189 
4190 	for (idx = offset; idx < last; idx++)
4191 		if (iwl_fw_rate_idx_to_plcp(idx) == rate)
4192 			return idx - offset;
4193 	return IWL_RATE_INVALID;
4194 }
4195 
iwl_mvm_v3_rate_from_fw(__le32 rate,u8 rate_ver)4196 u32 iwl_mvm_v3_rate_from_fw(__le32 rate, u8 rate_ver)
4197 {
4198 	u32 rate_v3 = 0, rate_v1;
4199 	u32 dup = 0;
4200 
4201 	if (rate_ver > 1)
4202 		return iwl_v3_rate_from_v2_v3(rate, rate_ver >= 3);
4203 
4204 	rate_v1 = le32_to_cpu(rate);
4205 	if (rate_v1 == 0)
4206 		return rate_v1;
4207 	/* convert rate */
4208 	if (rate_v1 & RATE_MCS_HT_MSK_V1) {
4209 		u32 nss;
4210 
4211 		rate_v3 |= RATE_MCS_MOD_TYPE_HT;
4212 		rate_v3 |=
4213 			rate_v1 & RATE_HT_MCS_RATE_CODE_MSK_V1;
4214 		nss = u32_get_bits(rate_v1, RATE_HT_MCS_MIMO2_MSK);
4215 		rate_v3 |= u32_encode_bits(nss, RATE_MCS_NSS_MSK);
4216 	} else if (rate_v1 & RATE_MCS_VHT_MSK_V1 ||
4217 		   rate_v1 & RATE_MCS_HE_MSK_V1) {
4218 		u32 nss = u32_get_bits(rate_v1, RATE_VHT_MCS_NSS_MSK);
4219 
4220 		rate_v3 |= rate_v1 & RATE_VHT_MCS_RATE_CODE_MSK;
4221 
4222 		rate_v3 |= u32_encode_bits(nss, RATE_MCS_NSS_MSK);
4223 
4224 		if (rate_v1 & RATE_MCS_HE_MSK_V1) {
4225 			u32 he_type_bits = rate_v1 & RATE_MCS_HE_TYPE_MSK_V1;
4226 			u32 he_type = he_type_bits >> RATE_MCS_HE_TYPE_POS_V1;
4227 			u32 he_106t = (rate_v1 & RATE_MCS_HE_106T_MSK_V1) >>
4228 				RATE_MCS_HE_106T_POS_V1;
4229 			u32 he_gi_ltf = (rate_v1 & RATE_MCS_HE_GI_LTF_MSK_V1) >>
4230 				RATE_MCS_HE_GI_LTF_POS;
4231 
4232 			if ((he_type_bits == RATE_MCS_HE_TYPE_SU ||
4233 			     he_type_bits == RATE_MCS_HE_TYPE_EXT_SU) &&
4234 			    he_gi_ltf == RATE_MCS_HE_SU_4_LTF)
4235 				/* the new rate have an additional bit to
4236 				 * represent the value 4 rather then using SGI
4237 				 * bit for this purpose - as it was done in the
4238 				 * old rate
4239 				 */
4240 				he_gi_ltf += (rate_v1 & RATE_MCS_SGI_MSK_V1) >>
4241 					RATE_MCS_SGI_POS_V1;
4242 
4243 			rate_v3 |= he_gi_ltf << RATE_MCS_HE_GI_LTF_POS;
4244 			rate_v3 |= he_type << RATE_MCS_HE_TYPE_POS;
4245 			rate_v3 |= he_106t << RATE_MCS_HE_106T_POS;
4246 			rate_v3 |= rate_v1 & RATE_HE_DUAL_CARRIER_MODE_MSK;
4247 			rate_v3 |= RATE_MCS_MOD_TYPE_HE;
4248 		} else {
4249 			rate_v3 |= RATE_MCS_MOD_TYPE_VHT;
4250 		}
4251 	/* if legacy format */
4252 	} else {
4253 		u32 legacy_rate = iwl_legacy_rate_to_fw_idx(rate_v1);
4254 
4255 		if (WARN_ON_ONCE(legacy_rate == IWL_RATE_INVALID))
4256 			legacy_rate = (rate_v1 & RATE_MCS_CCK_MSK_V1) ?
4257 				IWL_FIRST_CCK_RATE : IWL_FIRST_OFDM_RATE;
4258 
4259 		rate_v3 |= legacy_rate;
4260 		if (!(rate_v1 & RATE_MCS_CCK_MSK_V1))
4261 			rate_v3 |= RATE_MCS_MOD_TYPE_LEGACY_OFDM;
4262 	}
4263 
4264 	/* convert flags */
4265 	if (rate_v1 & RATE_MCS_LDPC_MSK_V1)
4266 		rate_v3 |= RATE_MCS_LDPC_MSK;
4267 	rate_v3 |= (rate_v1 & RATE_MCS_CHAN_WIDTH_MSK_V1) |
4268 		(rate_v1 & RATE_MCS_ANT_AB_MSK) |
4269 		(rate_v1 & RATE_MCS_STBC_MSK) |
4270 		(rate_v1 & RATE_MCS_BF_MSK);
4271 
4272 	dup = (rate_v1 & RATE_MCS_DUP_MSK_V1) >> RATE_MCS_DUP_POS_V1;
4273 	if (dup) {
4274 		rate_v3 |= RATE_MCS_DUP_MSK;
4275 		rate_v3 |= dup << RATE_MCS_CHAN_WIDTH_POS;
4276 	}
4277 
4278 	if ((!(rate_v1 & RATE_MCS_HE_MSK_V1)) &&
4279 	    (rate_v1 & RATE_MCS_SGI_MSK_V1))
4280 		rate_v3 |= RATE_MCS_SGI_MSK;
4281 
4282 	return rate_v3;
4283 }
4284 
iwl_mvm_v3_rate_to_fw(u32 rate,u8 rate_ver)4285 __le32 iwl_mvm_v3_rate_to_fw(u32 rate, u8 rate_ver)
4286 {
4287 	u32 result = 0;
4288 	int rate_idx;
4289 
4290 	if (rate_ver > 1)
4291 		return iwl_v3_rate_to_v2_v3(rate, rate_ver > 2);
4292 
4293 	switch (rate & RATE_MCS_MOD_TYPE_MSK) {
4294 	case RATE_MCS_MOD_TYPE_CCK:
4295 		result = RATE_MCS_CCK_MSK_V1;
4296 		fallthrough;
4297 	case RATE_MCS_MOD_TYPE_LEGACY_OFDM:
4298 		rate_idx = u32_get_bits(rate, RATE_LEGACY_RATE_MSK);
4299 		if (!(result & RATE_MCS_CCK_MSK_V1))
4300 			rate_idx += IWL_FIRST_OFDM_RATE;
4301 		result |= u32_encode_bits(iwl_fw_rate_idx_to_plcp(rate_idx),
4302 					  RATE_LEGACY_RATE_MSK_V1);
4303 		break;
4304 	case RATE_MCS_MOD_TYPE_HT:
4305 		result = RATE_MCS_HT_MSK_V1;
4306 		result |= u32_encode_bits(u32_get_bits(rate,
4307 						       RATE_HT_MCS_CODE_MSK),
4308 					  RATE_HT_MCS_RATE_CODE_MSK_V1);
4309 		result |= u32_encode_bits(u32_get_bits(rate,
4310 						       RATE_MCS_NSS_MSK),
4311 					  RATE_HT_MCS_MIMO2_MSK);
4312 		break;
4313 	case RATE_MCS_MOD_TYPE_VHT:
4314 		result = RATE_MCS_VHT_MSK_V1;
4315 		result |= u32_encode_bits(u32_get_bits(rate,
4316 						       RATE_VHT_MCS_NSS_MSK),
4317 					  RATE_MCS_CODE_MSK);
4318 		result |= u32_encode_bits(u32_get_bits(rate, RATE_MCS_NSS_MSK),
4319 					  RATE_VHT_MCS_NSS_MSK);
4320 		break;
4321 	case RATE_MCS_MOD_TYPE_HE: /* not generated */
4322 	default:
4323 		WARN_ONCE(1, "bad modulation type %d\n",
4324 			  u32_get_bits(rate, RATE_MCS_MOD_TYPE_MSK));
4325 		return 0;
4326 	}
4327 
4328 	if (rate & RATE_MCS_LDPC_MSK)
4329 		result |= RATE_MCS_LDPC_MSK_V1;
4330 	WARN_ON_ONCE(u32_get_bits(rate, RATE_MCS_CHAN_WIDTH_MSK) >
4331 			RATE_MCS_CHAN_WIDTH_160_VAL);
4332 	result |= (rate & RATE_MCS_CHAN_WIDTH_MSK_V1) |
4333 		  (rate & RATE_MCS_ANT_AB_MSK) |
4334 		  (rate & RATE_MCS_STBC_MSK) |
4335 		  (rate & RATE_MCS_BF_MSK);
4336 
4337 	/* not handling DUP since we don't use it */
4338 	WARN_ON_ONCE(rate & RATE_MCS_DUP_MSK);
4339 
4340 	if (rate & RATE_MCS_SGI_MSK)
4341 		result |= RATE_MCS_SGI_MSK_V1;
4342 
4343 	return cpu_to_le32(result);
4344 }
4345