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