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