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