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