1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3  * Copyright (C) 2017 Intel Deutschland GmbH
4  * Copyright (C) 2018-2022 Intel Corporation
5  */
6 #include "rs.h"
7 #include "fw-api.h"
8 #include "sta.h"
9 #include "iwl-op-mode.h"
10 #include "mvm.h"
11 
12 static u8 rs_fw_bw_from_sta_bw(const struct ieee80211_sta *sta)
13 {
14 	switch (sta->deflink.bandwidth) {
15 	case IEEE80211_STA_RX_BW_320:
16 		return IWL_TLC_MNG_CH_WIDTH_320MHZ;
17 	case IEEE80211_STA_RX_BW_160:
18 		return IWL_TLC_MNG_CH_WIDTH_160MHZ;
19 	case IEEE80211_STA_RX_BW_80:
20 		return IWL_TLC_MNG_CH_WIDTH_80MHZ;
21 	case IEEE80211_STA_RX_BW_40:
22 		return IWL_TLC_MNG_CH_WIDTH_40MHZ;
23 	case IEEE80211_STA_RX_BW_20:
24 	default:
25 		return IWL_TLC_MNG_CH_WIDTH_20MHZ;
26 	}
27 }
28 
29 static u8 rs_fw_set_active_chains(u8 chains)
30 {
31 	u8 fw_chains = 0;
32 
33 	if (chains & ANT_A)
34 		fw_chains |= IWL_TLC_MNG_CHAIN_A_MSK;
35 	if (chains & ANT_B)
36 		fw_chains |= IWL_TLC_MNG_CHAIN_B_MSK;
37 
38 	return fw_chains;
39 }
40 
41 static u8 rs_fw_sgi_cw_support(struct ieee80211_sta *sta)
42 {
43 	struct ieee80211_sta_ht_cap *ht_cap = &sta->deflink.ht_cap;
44 	struct ieee80211_sta_vht_cap *vht_cap = &sta->deflink.vht_cap;
45 	struct ieee80211_sta_he_cap *he_cap = &sta->deflink.he_cap;
46 	u8 supp = 0;
47 
48 	if (he_cap->has_he)
49 		return 0;
50 
51 	if (ht_cap->cap & IEEE80211_HT_CAP_SGI_20)
52 		supp |= BIT(IWL_TLC_MNG_CH_WIDTH_20MHZ);
53 	if (ht_cap->cap & IEEE80211_HT_CAP_SGI_40)
54 		supp |= BIT(IWL_TLC_MNG_CH_WIDTH_40MHZ);
55 	if (vht_cap->cap & IEEE80211_VHT_CAP_SHORT_GI_80)
56 		supp |= BIT(IWL_TLC_MNG_CH_WIDTH_80MHZ);
57 	if (vht_cap->cap & IEEE80211_VHT_CAP_SHORT_GI_160)
58 		supp |= BIT(IWL_TLC_MNG_CH_WIDTH_160MHZ);
59 
60 	return supp;
61 }
62 
63 static u16 rs_fw_get_config_flags(struct iwl_mvm *mvm,
64 				  struct ieee80211_sta *sta,
65 				  struct ieee80211_supported_band *sband)
66 {
67 	struct ieee80211_sta_ht_cap *ht_cap = &sta->deflink.ht_cap;
68 	struct ieee80211_sta_vht_cap *vht_cap = &sta->deflink.vht_cap;
69 	struct ieee80211_sta_he_cap *he_cap = &sta->deflink.he_cap;
70 	bool vht_ena = vht_cap->vht_supported;
71 	u16 flags = 0;
72 
73 	/* get STBC flags */
74 	if (mvm->cfg->ht_params->stbc &&
75 	    (num_of_ant(iwl_mvm_get_valid_tx_ant(mvm)) > 1)) {
76 		if (he_cap->has_he && he_cap->he_cap_elem.phy_cap_info[2] &
77 				      IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ)
78 			flags |= IWL_TLC_MNG_CFG_FLAGS_STBC_MSK;
79 		else if (vht_cap->cap & IEEE80211_VHT_CAP_RXSTBC_MASK)
80 			flags |= IWL_TLC_MNG_CFG_FLAGS_STBC_MSK;
81 		else if (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC)
82 			flags |= IWL_TLC_MNG_CFG_FLAGS_STBC_MSK;
83 	}
84 
85 	if (mvm->cfg->ht_params->ldpc &&
86 	    ((ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING) ||
87 	     (vht_ena && (vht_cap->cap & IEEE80211_VHT_CAP_RXLDPC))))
88 		flags |= IWL_TLC_MNG_CFG_FLAGS_LDPC_MSK;
89 
90 	/* consider LDPC support in case of HE */
91 	if (he_cap->has_he && (he_cap->he_cap_elem.phy_cap_info[1] &
92 	    IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD))
93 		flags |= IWL_TLC_MNG_CFG_FLAGS_LDPC_MSK;
94 
95 	if (sband->iftype_data && sband->iftype_data->he_cap.has_he &&
96 	    !(sband->iftype_data->he_cap.he_cap_elem.phy_cap_info[1] &
97 	     IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD))
98 		flags &= ~IWL_TLC_MNG_CFG_FLAGS_LDPC_MSK;
99 
100 	if (he_cap->has_he &&
101 	    (he_cap->he_cap_elem.phy_cap_info[3] &
102 	     IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_MASK &&
103 	     sband->iftype_data &&
104 	     sband->iftype_data->he_cap.he_cap_elem.phy_cap_info[3] &
105 	     IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK))
106 		flags |= IWL_TLC_MNG_CFG_FLAGS_HE_DCM_NSS_1_MSK;
107 
108 	return flags;
109 }
110 
111 static
112 int rs_fw_vht_highest_rx_mcs_index(const struct ieee80211_sta_vht_cap *vht_cap,
113 				   int nss)
114 {
115 	u16 rx_mcs = le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map) &
116 		(0x3 << (2 * (nss - 1)));
117 	rx_mcs >>= (2 * (nss - 1));
118 
119 	switch (rx_mcs) {
120 	case IEEE80211_VHT_MCS_SUPPORT_0_7:
121 		return IWL_TLC_MNG_HT_RATE_MCS7;
122 	case IEEE80211_VHT_MCS_SUPPORT_0_8:
123 		return IWL_TLC_MNG_HT_RATE_MCS8;
124 	case IEEE80211_VHT_MCS_SUPPORT_0_9:
125 		return IWL_TLC_MNG_HT_RATE_MCS9;
126 	default:
127 		WARN_ON_ONCE(1);
128 		break;
129 	}
130 
131 	return 0;
132 }
133 
134 static void
135 rs_fw_vht_set_enabled_rates(const struct ieee80211_sta *sta,
136 			    const struct ieee80211_sta_vht_cap *vht_cap,
137 			    struct iwl_tlc_config_cmd_v4 *cmd)
138 {
139 	u16 supp;
140 	int i, highest_mcs;
141 	u8 max_nss = sta->deflink.rx_nss;
142 	struct ieee80211_vht_cap ieee_vht_cap = {
143 		.vht_cap_info = cpu_to_le32(vht_cap->cap),
144 		.supp_mcs = vht_cap->vht_mcs,
145 	};
146 
147 	/* the station support only a single receive chain */
148 	if (sta->deflink.smps_mode == IEEE80211_SMPS_STATIC)
149 		max_nss = 1;
150 
151 	for (i = 0; i < max_nss && i < IWL_TLC_NSS_MAX; i++) {
152 		int nss = i + 1;
153 
154 		highest_mcs = rs_fw_vht_highest_rx_mcs_index(vht_cap, nss);
155 		if (!highest_mcs)
156 			continue;
157 
158 		supp = BIT(highest_mcs + 1) - 1;
159 		if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_20)
160 			supp &= ~BIT(IWL_TLC_MNG_HT_RATE_MCS9);
161 
162 		cmd->ht_rates[i][IWL_TLC_MCS_PER_BW_80] = cpu_to_le16(supp);
163 		/*
164 		 * Check if VHT extended NSS indicates that the bandwidth/NSS
165 		 * configuration is supported - only for MCS 0 since we already
166 		 * decoded the MCS bits anyway ourselves.
167 		 */
168 		if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_160 &&
169 		    ieee80211_get_vht_max_nss(&ieee_vht_cap,
170 					      IEEE80211_VHT_CHANWIDTH_160MHZ,
171 					      0, true, nss) >= nss)
172 			cmd->ht_rates[i][IWL_TLC_MCS_PER_BW_160] =
173 				cmd->ht_rates[i][IWL_TLC_MCS_PER_BW_80];
174 	}
175 }
176 
177 static u16 rs_fw_he_ieee80211_mcs_to_rs_mcs(u16 mcs)
178 {
179 	switch (mcs) {
180 	case IEEE80211_HE_MCS_SUPPORT_0_7:
181 		return BIT(IWL_TLC_MNG_HT_RATE_MCS7 + 1) - 1;
182 	case IEEE80211_HE_MCS_SUPPORT_0_9:
183 		return BIT(IWL_TLC_MNG_HT_RATE_MCS9 + 1) - 1;
184 	case IEEE80211_HE_MCS_SUPPORT_0_11:
185 		return BIT(IWL_TLC_MNG_HT_RATE_MCS11 + 1) - 1;
186 	case IEEE80211_HE_MCS_NOT_SUPPORTED:
187 		return 0;
188 	}
189 
190 	WARN(1, "invalid HE MCS %d\n", mcs);
191 	return 0;
192 }
193 
194 static void
195 rs_fw_he_set_enabled_rates(const struct ieee80211_sta *sta,
196 			   struct ieee80211_supported_band *sband,
197 			   struct iwl_tlc_config_cmd_v4 *cmd)
198 {
199 	const struct ieee80211_sta_he_cap *he_cap = &sta->deflink.he_cap;
200 	u16 mcs_160 = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_160);
201 	u16 mcs_80 = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_80);
202 	u16 tx_mcs_80 =
203 		le16_to_cpu(sband->iftype_data->he_cap.he_mcs_nss_supp.tx_mcs_80);
204 	u16 tx_mcs_160 =
205 		le16_to_cpu(sband->iftype_data->he_cap.he_mcs_nss_supp.tx_mcs_160);
206 	int i;
207 	u8 nss = sta->deflink.rx_nss;
208 
209 	/* the station support only a single receive chain */
210 	if (sta->deflink.smps_mode == IEEE80211_SMPS_STATIC)
211 		nss = 1;
212 
213 	for (i = 0; i < nss && i < IWL_TLC_NSS_MAX; i++) {
214 		u16 _mcs_160 = (mcs_160 >> (2 * i)) & 0x3;
215 		u16 _mcs_80 = (mcs_80 >> (2 * i)) & 0x3;
216 		u16 _tx_mcs_160 = (tx_mcs_160 >> (2 * i)) & 0x3;
217 		u16 _tx_mcs_80 = (tx_mcs_80 >> (2 * i)) & 0x3;
218 
219 		/* If one side doesn't support - mark both as not supporting */
220 		if (_mcs_80 == IEEE80211_HE_MCS_NOT_SUPPORTED ||
221 		    _tx_mcs_80 == IEEE80211_HE_MCS_NOT_SUPPORTED) {
222 			_mcs_80 = IEEE80211_HE_MCS_NOT_SUPPORTED;
223 			_tx_mcs_80 = IEEE80211_HE_MCS_NOT_SUPPORTED;
224 		}
225 		if (_mcs_80 > _tx_mcs_80)
226 			_mcs_80 = _tx_mcs_80;
227 		cmd->ht_rates[i][IWL_TLC_MCS_PER_BW_80] =
228 			cpu_to_le16(rs_fw_he_ieee80211_mcs_to_rs_mcs(_mcs_80));
229 
230 		/* If one side doesn't support - mark both as not supporting */
231 		if (_mcs_160 == IEEE80211_HE_MCS_NOT_SUPPORTED ||
232 		    _tx_mcs_160 == IEEE80211_HE_MCS_NOT_SUPPORTED) {
233 			_mcs_160 = IEEE80211_HE_MCS_NOT_SUPPORTED;
234 			_tx_mcs_160 = IEEE80211_HE_MCS_NOT_SUPPORTED;
235 		}
236 		if (_mcs_160 > _tx_mcs_160)
237 			_mcs_160 = _tx_mcs_160;
238 		cmd->ht_rates[i][IWL_TLC_MCS_PER_BW_160] =
239 			cpu_to_le16(rs_fw_he_ieee80211_mcs_to_rs_mcs(_mcs_160));
240 	}
241 }
242 
243 static u8 rs_fw_eht_max_nss(u8 rx_nss, u8 tx_nss)
244 {
245 	u8 tx = u8_get_bits(tx_nss, IEEE80211_EHT_MCS_NSS_TX);
246 	u8 rx = u8_get_bits(rx_nss, IEEE80211_EHT_MCS_NSS_RX);
247 	/* the max nss that can be used,
248 	 * is the min with our tx capa and the peer rx capa.
249 	 */
250 	return min(tx, rx);
251 }
252 
253 #define MAX_NSS_MCS(mcs_num, rx, tx) \
254 	rs_fw_eht_max_nss((rx)->rx_tx_mcs ##mcs_num## _max_nss, \
255 			  (tx)->rx_tx_mcs ##mcs_num## _max_nss)
256 
257 static void rs_fw_set_eht_mcs_nss(__le16 ht_rates[][3],
258 				  enum IWL_TLC_MCS_PER_BW bw,
259 				  u8 max_nss, u16 mcs_msk)
260 {
261 	if (max_nss >= 2)
262 		ht_rates[IWL_TLC_NSS_2][bw] |= cpu_to_le16(mcs_msk);
263 
264 	if (max_nss >= 1)
265 		ht_rates[IWL_TLC_NSS_1][bw] |= cpu_to_le16(mcs_msk);
266 }
267 
268 static const
269 struct ieee80211_eht_mcs_nss_supp_bw *
270 rs_fw_rs_mcs2eht_mcs(enum IWL_TLC_MCS_PER_BW bw,
271 		     const struct ieee80211_eht_mcs_nss_supp *eht_mcs)
272 {
273 	switch (bw) {
274 	case IWL_TLC_MCS_PER_BW_80:
275 		return &eht_mcs->bw._80;
276 	case IWL_TLC_MCS_PER_BW_160:
277 		return &eht_mcs->bw._160;
278 	case IWL_TLC_MCS_PER_BW_320:
279 		return &eht_mcs->bw._320;
280 	default:
281 		return NULL;
282 	}
283 }
284 
285 static void rs_fw_eht_set_enabled_rates(const struct ieee80211_sta *sta,
286 					struct ieee80211_supported_band *sband,
287 					struct iwl_tlc_config_cmd_v4 *cmd)
288 {
289 	/* peer RX mcs capa */
290 	const struct ieee80211_eht_mcs_nss_supp *eht_rx_mcs =
291 		&sta->deflink.eht_cap.eht_mcs_nss_supp;
292 	/* our TX mcs capa */
293 	const struct ieee80211_eht_mcs_nss_supp *eht_tx_mcs =
294 		&sband->iftype_data->eht_cap.eht_mcs_nss_supp;
295 
296 	enum IWL_TLC_MCS_PER_BW bw;
297 	struct ieee80211_eht_mcs_nss_supp_20mhz_only mcs_rx_20;
298 	struct ieee80211_eht_mcs_nss_supp_20mhz_only mcs_tx_20;
299 
300 	/* peer is 20Mhz only */
301 	if (!(sta->deflink.he_cap.he_cap_elem.phy_cap_info[0] &
302 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_MASK_ALL)) {
303 		mcs_rx_20 = eht_rx_mcs->only_20mhz;
304 	} else {
305 		mcs_rx_20.rx_tx_mcs7_max_nss = eht_rx_mcs->bw._80.rx_tx_mcs9_max_nss;
306 		mcs_rx_20.rx_tx_mcs9_max_nss = eht_rx_mcs->bw._80.rx_tx_mcs9_max_nss;
307 		mcs_rx_20.rx_tx_mcs11_max_nss = eht_rx_mcs->bw._80.rx_tx_mcs11_max_nss;
308 		mcs_rx_20.rx_tx_mcs13_max_nss = eht_rx_mcs->bw._80.rx_tx_mcs13_max_nss;
309 	}
310 
311 	/* nic is 20Mhz only */
312 	if (!(sband->iftype_data->he_cap.he_cap_elem.phy_cap_info[0] &
313 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_MASK_ALL)) {
314 		mcs_tx_20 = eht_tx_mcs->only_20mhz;
315 	} else {
316 		mcs_tx_20.rx_tx_mcs7_max_nss = eht_tx_mcs->bw._80.rx_tx_mcs9_max_nss;
317 		mcs_tx_20.rx_tx_mcs9_max_nss = eht_tx_mcs->bw._80.rx_tx_mcs9_max_nss;
318 		mcs_tx_20.rx_tx_mcs11_max_nss = eht_tx_mcs->bw._80.rx_tx_mcs11_max_nss;
319 		mcs_tx_20.rx_tx_mcs13_max_nss = eht_tx_mcs->bw._80.rx_tx_mcs13_max_nss;
320 	}
321 
322 	/* rates for 20/40/80 bw */
323 	bw = IWL_TLC_MCS_PER_BW_80;
324 	rs_fw_set_eht_mcs_nss(cmd->ht_rates, bw,
325 			      MAX_NSS_MCS(7, &mcs_rx_20, &mcs_tx_20), GENMASK(7, 0));
326 	rs_fw_set_eht_mcs_nss(cmd->ht_rates, bw,
327 			      MAX_NSS_MCS(9, &mcs_rx_20, &mcs_tx_20), GENMASK(9, 8));
328 	rs_fw_set_eht_mcs_nss(cmd->ht_rates, bw,
329 			      MAX_NSS_MCS(11, &mcs_rx_20, &mcs_tx_20), GENMASK(11, 10));
330 	rs_fw_set_eht_mcs_nss(cmd->ht_rates, bw,
331 			      MAX_NSS_MCS(13, &mcs_rx_20, &mcs_tx_20), GENMASK(13, 12));
332 
333 	/* rate for 160/320 bw */
334 	for (bw = IWL_TLC_MCS_PER_BW_160; bw <= IWL_TLC_MCS_PER_BW_320; bw++) {
335 		const struct ieee80211_eht_mcs_nss_supp_bw *mcs_rx =
336 			rs_fw_rs_mcs2eht_mcs(bw, eht_rx_mcs);
337 		const struct ieee80211_eht_mcs_nss_supp_bw *mcs_tx =
338 			rs_fw_rs_mcs2eht_mcs(bw, eht_tx_mcs);
339 
340 		/* got unsuppored index for bw */
341 		if (!mcs_rx || !mcs_tx)
342 			continue;
343 
344 		rs_fw_set_eht_mcs_nss(cmd->ht_rates, bw,
345 				      MAX_NSS_MCS(9, mcs_rx, mcs_tx), GENMASK(9, 0));
346 		rs_fw_set_eht_mcs_nss(cmd->ht_rates, bw,
347 				      MAX_NSS_MCS(11, mcs_rx, mcs_tx), GENMASK(11, 10));
348 		rs_fw_set_eht_mcs_nss(cmd->ht_rates, bw,
349 				      MAX_NSS_MCS(13, mcs_rx, mcs_tx), GENMASK(13, 12));
350 	}
351 
352 	/* the station support only a single receive chain */
353 	if (sta->deflink.smps_mode == IEEE80211_SMPS_STATIC ||
354 	    sta->deflink.rx_nss < 2)
355 		memset(cmd->ht_rates[IWL_TLC_NSS_2], 0,
356 		       sizeof(cmd->ht_rates[IWL_TLC_NSS_2]));
357 }
358 
359 static void rs_fw_set_supp_rates(struct ieee80211_sta *sta,
360 				 struct ieee80211_supported_band *sband,
361 				 struct iwl_tlc_config_cmd_v4 *cmd)
362 {
363 	int i;
364 	u16 supp = 0;
365 	unsigned long tmp; /* must be unsigned long for for_each_set_bit */
366 	const struct ieee80211_sta_ht_cap *ht_cap = &sta->deflink.ht_cap;
367 	const struct ieee80211_sta_vht_cap *vht_cap = &sta->deflink.vht_cap;
368 	const struct ieee80211_sta_he_cap *he_cap = &sta->deflink.he_cap;
369 
370 	/* non HT rates */
371 	tmp = sta->deflink.supp_rates[sband->band];
372 	for_each_set_bit(i, &tmp, BITS_PER_LONG)
373 		supp |= BIT(sband->bitrates[i].hw_value);
374 
375 	cmd->non_ht_rates = cpu_to_le16(supp);
376 	cmd->mode = IWL_TLC_MNG_MODE_NON_HT;
377 
378 	/* HT/VHT rates */
379 	if (sta->deflink.eht_cap.has_eht) {
380 		cmd->mode = IWL_TLC_MNG_MODE_EHT;
381 		rs_fw_eht_set_enabled_rates(sta, sband, cmd);
382 	} else if (he_cap->has_he) {
383 		cmd->mode = IWL_TLC_MNG_MODE_HE;
384 		rs_fw_he_set_enabled_rates(sta, sband, cmd);
385 	} else if (vht_cap->vht_supported) {
386 		cmd->mode = IWL_TLC_MNG_MODE_VHT;
387 		rs_fw_vht_set_enabled_rates(sta, vht_cap, cmd);
388 	} else if (ht_cap->ht_supported) {
389 		cmd->mode = IWL_TLC_MNG_MODE_HT;
390 		cmd->ht_rates[IWL_TLC_NSS_1][IWL_TLC_MCS_PER_BW_80] =
391 			cpu_to_le16(ht_cap->mcs.rx_mask[0]);
392 
393 		/* the station support only a single receive chain */
394 		if (sta->deflink.smps_mode == IEEE80211_SMPS_STATIC)
395 			cmd->ht_rates[IWL_TLC_NSS_2][IWL_TLC_MCS_PER_BW_80] =
396 				0;
397 		else
398 			cmd->ht_rates[IWL_TLC_NSS_2][IWL_TLC_MCS_PER_BW_80] =
399 				cpu_to_le16(ht_cap->mcs.rx_mask[1]);
400 	}
401 }
402 
403 void iwl_mvm_tlc_update_notif(struct iwl_mvm *mvm,
404 			      struct iwl_rx_cmd_buffer *rxb)
405 {
406 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
407 	struct iwl_tlc_update_notif *notif;
408 	struct ieee80211_sta *sta;
409 	struct iwl_mvm_sta *mvmsta;
410 	struct iwl_lq_sta_rs_fw *lq_sta;
411 	u32 flags;
412 
413 	rcu_read_lock();
414 
415 	notif = (void *)pkt->data;
416 	sta = rcu_dereference(mvm->fw_id_to_mac_id[notif->sta_id]);
417 	if (IS_ERR_OR_NULL(sta)) {
418 		/* can happen in remove station flow where mvm removed internally
419 		 * the station before removing from FW
420 		 */
421 		IWL_DEBUG_RATE(mvm,
422 			       "Invalid mvm RCU pointer for sta id (%d) in TLC notification\n",
423 			       notif->sta_id);
424 		goto out;
425 	}
426 
427 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
428 
429 	if (!mvmsta) {
430 		IWL_ERR(mvm, "Invalid sta id (%d) in FW TLC notification\n",
431 			notif->sta_id);
432 		goto out;
433 	}
434 
435 	flags = le32_to_cpu(notif->flags);
436 
437 	lq_sta = &mvmsta->lq_sta.rs_fw;
438 
439 	if (flags & IWL_TLC_NOTIF_FLAG_RATE) {
440 		char pretty_rate[100];
441 
442 		if (iwl_fw_lookup_notif_ver(mvm->fw, DATA_PATH_GROUP,
443 					    TLC_MNG_UPDATE_NOTIF, 0) < 3) {
444 			rs_pretty_print_rate_v1(pretty_rate,
445 						sizeof(pretty_rate),
446 						le32_to_cpu(notif->rate));
447 			IWL_DEBUG_RATE(mvm,
448 				       "Got rate in old format. Rate: %s. Converting.\n",
449 				       pretty_rate);
450 			lq_sta->last_rate_n_flags =
451 				iwl_new_rate_from_v1(le32_to_cpu(notif->rate));
452 		} else {
453 			lq_sta->last_rate_n_flags = le32_to_cpu(notif->rate);
454 		}
455 		rs_pretty_print_rate(pretty_rate, sizeof(pretty_rate),
456 				     lq_sta->last_rate_n_flags);
457 		IWL_DEBUG_RATE(mvm, "new rate: %s\n", pretty_rate);
458 	}
459 
460 	if (flags & IWL_TLC_NOTIF_FLAG_AMSDU && !mvmsta->orig_amsdu_len) {
461 		u16 size = le32_to_cpu(notif->amsdu_size);
462 		int i;
463 
464 		if (sta->deflink.agg.max_amsdu_len < size) {
465 			/*
466 			 * In debug sta->deflink.agg.max_amsdu_len < size
467 			 * so also check with orig_amsdu_len which holds the
468 			 * original data before debugfs changed the value
469 			 */
470 			WARN_ON(mvmsta->orig_amsdu_len < size);
471 			goto out;
472 		}
473 
474 		mvmsta->amsdu_enabled = le32_to_cpu(notif->amsdu_enabled);
475 		mvmsta->max_amsdu_len = size;
476 		sta->deflink.agg.max_rc_amsdu_len = mvmsta->max_amsdu_len;
477 
478 		for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
479 			if (mvmsta->amsdu_enabled & BIT(i))
480 				sta->deflink.agg.max_tid_amsdu_len[i] =
481 					iwl_mvm_max_amsdu_size(mvm, sta, i);
482 			else
483 				/*
484 				 * Not so elegant, but this will effectively
485 				 * prevent AMSDU on this TID
486 				 */
487 				sta->deflink.agg.max_tid_amsdu_len[i] = 1;
488 		}
489 
490 		IWL_DEBUG_RATE(mvm,
491 			       "AMSDU update. AMSDU size: %d, AMSDU selected size: %d, AMSDU TID bitmap 0x%X\n",
492 			       le32_to_cpu(notif->amsdu_size), size,
493 			       mvmsta->amsdu_enabled);
494 	}
495 out:
496 	rcu_read_unlock();
497 }
498 
499 u16 rs_fw_get_max_amsdu_len(struct ieee80211_sta *sta)
500 {
501 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
502 	const struct ieee80211_sta_vht_cap *vht_cap = &sta->deflink.vht_cap;
503 	const struct ieee80211_sta_ht_cap *ht_cap = &sta->deflink.ht_cap;
504 
505 	if (mvmsta->vif->bss_conf.chandef.chan->band == NL80211_BAND_6GHZ) {
506 		switch (le16_get_bits(sta->deflink.he_6ghz_capa.capa,
507 				      IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN)) {
508 		case IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454:
509 			return IEEE80211_MAX_MPDU_LEN_VHT_11454;
510 		case IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_7991:
511 			return IEEE80211_MAX_MPDU_LEN_VHT_7991;
512 		default:
513 			return IEEE80211_MAX_MPDU_LEN_VHT_3895;
514 		}
515 	} else
516 	if (vht_cap->vht_supported) {
517 		switch (vht_cap->cap & IEEE80211_VHT_CAP_MAX_MPDU_MASK) {
518 		case IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454:
519 			return IEEE80211_MAX_MPDU_LEN_VHT_11454;
520 		case IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_7991:
521 			return IEEE80211_MAX_MPDU_LEN_VHT_7991;
522 		default:
523 			return IEEE80211_MAX_MPDU_LEN_VHT_3895;
524 		}
525 	} else if (ht_cap->ht_supported) {
526 		if (ht_cap->cap & IEEE80211_HT_CAP_MAX_AMSDU)
527 			/*
528 			 * agg is offloaded so we need to assume that agg
529 			 * are enabled and max mpdu in ampdu is 4095
530 			 * (spec 802.11-2016 9.3.2.1)
531 			 */
532 			return IEEE80211_MAX_MPDU_LEN_HT_BA;
533 		else
534 			return IEEE80211_MAX_MPDU_LEN_HT_3839;
535 	}
536 
537 	/* in legacy mode no amsdu is enabled so return zero */
538 	return 0;
539 }
540 
541 void rs_fw_rate_init(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
542 		     enum nl80211_band band, bool update)
543 {
544 	struct ieee80211_hw *hw = mvm->hw;
545 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
546 	struct iwl_lq_sta_rs_fw *lq_sta = &mvmsta->lq_sta.rs_fw;
547 	u32 cmd_id = WIDE_ID(DATA_PATH_GROUP, TLC_MNG_CONFIG_CMD);
548 	struct ieee80211_supported_band *sband = hw->wiphy->bands[band];
549 	u16 max_amsdu_len = rs_fw_get_max_amsdu_len(sta);
550 	struct iwl_tlc_config_cmd_v4 cfg_cmd = {
551 		.sta_id = mvmsta->sta_id,
552 		.max_ch_width = update ?
553 			rs_fw_bw_from_sta_bw(sta) : RATE_MCS_CHAN_WIDTH_20,
554 		.flags = cpu_to_le16(rs_fw_get_config_flags(mvm, sta, sband)),
555 		.chains = rs_fw_set_active_chains(iwl_mvm_get_valid_tx_ant(mvm)),
556 		.sgi_ch_width_supp = rs_fw_sgi_cw_support(sta),
557 		.max_mpdu_len = iwl_mvm_is_csum_supported(mvm) ?
558 				cpu_to_le16(max_amsdu_len) : 0,
559 	};
560 	int ret;
561 	int cmd_ver;
562 
563 	memset(lq_sta, 0, offsetof(typeof(*lq_sta), pers));
564 
565 #ifdef CONFIG_IWLWIFI_DEBUGFS
566 	iwl_mvm_reset_frame_stats(mvm);
567 #endif
568 	rs_fw_set_supp_rates(sta, sband, &cfg_cmd);
569 
570 	/*
571 	 * since TLC offload works with one mode we can assume
572 	 * that only vht/ht is used and also set it as station max amsdu
573 	 */
574 	sta->deflink.agg.max_amsdu_len = max_amsdu_len;
575 
576 	cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw,
577 					WIDE_ID(DATA_PATH_GROUP,
578 						TLC_MNG_CONFIG_CMD),
579 					0);
580 	IWL_DEBUG_RATE(mvm, "TLC CONFIG CMD, sta_id=%d, max_ch_width=%d, mode=%d\n",
581 		       cfg_cmd.sta_id, cfg_cmd.max_ch_width, cfg_cmd.mode);
582 	IWL_DEBUG_RATE(mvm, "TLC CONFIG CMD, chains=0x%X, ch_wid_supp=%d, flags=0x%X\n",
583 		       cfg_cmd.chains, cfg_cmd.sgi_ch_width_supp, cfg_cmd.flags);
584 	IWL_DEBUG_RATE(mvm, "TLC CONFIG CMD, mpdu_len=%d, no_ht_rate=0x%X, tx_op=%d\n",
585 		       cfg_cmd.max_mpdu_len, cfg_cmd.non_ht_rates, cfg_cmd.max_tx_op);
586 	IWL_DEBUG_RATE(mvm, "TLC CONFIG CMD, ht_rate[0][0]=0x%X, ht_rate[1][0]=0x%X\n",
587 		       cfg_cmd.ht_rates[0][0], cfg_cmd.ht_rates[1][0]);
588 	IWL_DEBUG_RATE(mvm, "TLC CONFIG CMD, ht_rate[0][1]=0x%X, ht_rate[1][1]=0x%X\n",
589 		       cfg_cmd.ht_rates[0][1], cfg_cmd.ht_rates[1][1]);
590 	IWL_DEBUG_RATE(mvm, "TLC CONFIG CMD, ht_rate[0][2]=0x%X, ht_rate[1][2]=0x%X\n",
591 		       cfg_cmd.ht_rates[0][2], cfg_cmd.ht_rates[1][2]);
592 	if (cmd_ver == 4) {
593 		ret = iwl_mvm_send_cmd_pdu(mvm, cmd_id, CMD_ASYNC,
594 					   sizeof(cfg_cmd), &cfg_cmd);
595 	} else if (cmd_ver < 4) {
596 		struct iwl_tlc_config_cmd_v3 cfg_cmd_v3 = {
597 			.sta_id = cfg_cmd.sta_id,
598 			.max_ch_width = cfg_cmd.max_ch_width,
599 			.mode = cfg_cmd.mode,
600 			.chains = cfg_cmd.chains,
601 			.amsdu = !!cfg_cmd.max_mpdu_len,
602 			.flags = cfg_cmd.flags,
603 			.non_ht_rates = cfg_cmd.non_ht_rates,
604 			.ht_rates[0][0] = cfg_cmd.ht_rates[0][0],
605 			.ht_rates[0][1] = cfg_cmd.ht_rates[0][1],
606 			.ht_rates[1][0] = cfg_cmd.ht_rates[1][0],
607 			.ht_rates[1][1] = cfg_cmd.ht_rates[1][1],
608 			.sgi_ch_width_supp = cfg_cmd.sgi_ch_width_supp,
609 			.max_mpdu_len = cfg_cmd.max_mpdu_len,
610 		};
611 
612 		u16 cmd_size = sizeof(cfg_cmd_v3);
613 
614 		/* In old versions of the API the struct is 4 bytes smaller */
615 		if (iwl_fw_lookup_cmd_ver(mvm->fw,
616 					  WIDE_ID(DATA_PATH_GROUP,
617 						  TLC_MNG_CONFIG_CMD), 0) < 3)
618 			cmd_size -= 4;
619 
620 		ret = iwl_mvm_send_cmd_pdu(mvm, cmd_id, CMD_ASYNC, cmd_size,
621 					   &cfg_cmd_v3);
622 	} else {
623 		ret = -EINVAL;
624 	}
625 
626 	if (ret)
627 		IWL_ERR(mvm, "Failed to send rate scale config (%d)\n", ret);
628 }
629 
630 int rs_fw_tx_protection(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
631 			bool enable)
632 {
633 	/* TODO: need to introduce a new FW cmd since LQ cmd is not relevant */
634 	IWL_DEBUG_RATE(mvm, "tx protection - not implemented yet.\n");
635 	return 0;
636 }
637 
638 void iwl_mvm_rs_add_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta)
639 {
640 	struct iwl_lq_sta_rs_fw *lq_sta = &mvmsta->lq_sta.rs_fw;
641 
642 	IWL_DEBUG_RATE(mvm, "create station rate scale window\n");
643 
644 	lq_sta->pers.drv = mvm;
645 	lq_sta->pers.sta_id = mvmsta->sta_id;
646 	lq_sta->pers.chains = 0;
647 	memset(lq_sta->pers.chain_signal, 0, sizeof(lq_sta->pers.chain_signal));
648 	lq_sta->pers.last_rssi = S8_MIN;
649 	lq_sta->last_rate_n_flags = 0;
650 
651 #ifdef CONFIG_MAC80211_DEBUGFS
652 	lq_sta->pers.dbg_fixed_rate = 0;
653 #endif
654 }
655