xref: /linux/net/mac80211/vht.c (revision 91a4855d6c03e770e42f17c798a36a3c46e63de2)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * VHT handling
4  *
5  * Portions of this file
6  * Copyright(c) 2015 - 2016 Intel Deutschland GmbH
7  * Copyright (C) 2018 - 2026 Intel Corporation
8  */
9 
10 #include <linux/ieee80211.h>
11 #include <linux/export.h>
12 #include <net/mac80211.h>
13 #include "ieee80211_i.h"
14 #include "rate.h"
15 
16 
17 static void __check_vhtcap_disable(struct ieee80211_sub_if_data *sdata,
18 				   struct ieee80211_sta_vht_cap *vht_cap,
19 				   u32 flag)
20 {
21 	__le32 le_flag = cpu_to_le32(flag);
22 
23 	if (sdata->u.mgd.vht_capa_mask.vht_cap_info & le_flag &&
24 	    !(sdata->u.mgd.vht_capa.vht_cap_info & le_flag))
25 		vht_cap->cap &= ~flag;
26 }
27 
28 void ieee80211_apply_vhtcap_overrides(struct ieee80211_sub_if_data *sdata,
29 				      struct ieee80211_sta_vht_cap *vht_cap)
30 {
31 	int i;
32 	u16 rxmcs_mask, rxmcs_cap, rxmcs_n, txmcs_mask, txmcs_cap, txmcs_n;
33 
34 	if (!vht_cap->vht_supported)
35 		return;
36 
37 	if (sdata->vif.type != NL80211_IFTYPE_STATION)
38 		return;
39 
40 	__check_vhtcap_disable(sdata, vht_cap,
41 			       IEEE80211_VHT_CAP_RXLDPC);
42 	__check_vhtcap_disable(sdata, vht_cap,
43 			       IEEE80211_VHT_CAP_SHORT_GI_80);
44 	__check_vhtcap_disable(sdata, vht_cap,
45 			       IEEE80211_VHT_CAP_SHORT_GI_160);
46 	__check_vhtcap_disable(sdata, vht_cap,
47 			       IEEE80211_VHT_CAP_TXSTBC);
48 	__check_vhtcap_disable(sdata, vht_cap,
49 			       IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE);
50 	__check_vhtcap_disable(sdata, vht_cap,
51 			       IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE);
52 	__check_vhtcap_disable(sdata, vht_cap,
53 			       IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN);
54 	__check_vhtcap_disable(sdata, vht_cap,
55 			       IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN);
56 
57 	/* Allow user to decrease AMPDU length exponent */
58 	if (sdata->u.mgd.vht_capa_mask.vht_cap_info &
59 	    cpu_to_le32(IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK)) {
60 		u32 cap, n;
61 
62 		n = le32_to_cpu(sdata->u.mgd.vht_capa.vht_cap_info) &
63 			IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
64 		n >>= IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
65 		cap = vht_cap->cap & IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
66 		cap >>= IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
67 
68 		if (n < cap) {
69 			vht_cap->cap &=
70 				~IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
71 			vht_cap->cap |=
72 				n << IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
73 		}
74 	}
75 
76 	/* Allow the user to decrease MCSes */
77 	rxmcs_mask =
78 		le16_to_cpu(sdata->u.mgd.vht_capa_mask.supp_mcs.rx_mcs_map);
79 	rxmcs_n = le16_to_cpu(sdata->u.mgd.vht_capa.supp_mcs.rx_mcs_map);
80 	rxmcs_n &= rxmcs_mask;
81 	rxmcs_cap = le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
82 
83 	txmcs_mask =
84 		le16_to_cpu(sdata->u.mgd.vht_capa_mask.supp_mcs.tx_mcs_map);
85 	txmcs_n = le16_to_cpu(sdata->u.mgd.vht_capa.supp_mcs.tx_mcs_map);
86 	txmcs_n &= txmcs_mask;
87 	txmcs_cap = le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map);
88 	for (i = 0; i < 8; i++) {
89 		u8 m, n, c;
90 
91 		m = (rxmcs_mask >> 2*i) & IEEE80211_VHT_MCS_NOT_SUPPORTED;
92 		n = (rxmcs_n >> 2*i) & IEEE80211_VHT_MCS_NOT_SUPPORTED;
93 		c = (rxmcs_cap >> 2*i) & IEEE80211_VHT_MCS_NOT_SUPPORTED;
94 
95 		if (m && ((c != IEEE80211_VHT_MCS_NOT_SUPPORTED && n < c) ||
96 			  n == IEEE80211_VHT_MCS_NOT_SUPPORTED)) {
97 			rxmcs_cap &= ~(3 << 2*i);
98 			rxmcs_cap |= (rxmcs_n & (3 << 2*i));
99 		}
100 
101 		m = (txmcs_mask >> 2*i) & IEEE80211_VHT_MCS_NOT_SUPPORTED;
102 		n = (txmcs_n >> 2*i) & IEEE80211_VHT_MCS_NOT_SUPPORTED;
103 		c = (txmcs_cap >> 2*i) & IEEE80211_VHT_MCS_NOT_SUPPORTED;
104 
105 		if (m && ((c != IEEE80211_VHT_MCS_NOT_SUPPORTED && n < c) ||
106 			  n == IEEE80211_VHT_MCS_NOT_SUPPORTED)) {
107 			txmcs_cap &= ~(3 << 2*i);
108 			txmcs_cap |= (txmcs_n & (3 << 2*i));
109 		}
110 	}
111 	vht_cap->vht_mcs.rx_mcs_map = cpu_to_le16(rxmcs_cap);
112 	vht_cap->vht_mcs.tx_mcs_map = cpu_to_le16(txmcs_cap);
113 }
114 
115 void
116 ieee80211_vht_cap_ie_to_sta_vht_cap(struct ieee80211_sub_if_data *sdata,
117 				    struct ieee80211_supported_band *sband,
118 				    const struct ieee80211_sta_vht_cap *own_vht_cap,
119 				    const struct ieee80211_vht_cap *vht_cap_ie,
120 				    const struct ieee80211_vht_cap *vht_cap_ie2,
121 				    struct link_sta_info *link_sta)
122 {
123 	struct ieee80211_sta_vht_cap *vht_cap = &link_sta->pub->vht_cap;
124 	struct ieee80211_sta_vht_cap own_cap;
125 	u32 cap_info, i;
126 	u32 mpdu_len;
127 
128 	memset(vht_cap, 0, sizeof(*vht_cap));
129 
130 	if (!link_sta->pub->ht_cap.ht_supported)
131 		return;
132 
133 	if (!vht_cap_ie || !own_vht_cap->vht_supported)
134 		return;
135 
136 	/* NDI station are using the capabilities from the NMI station */
137 	if (WARN_ON_ONCE(sdata->vif.type == NL80211_IFTYPE_NAN_DATA))
138 		return;
139 
140 	if (sband) {
141 		/* Allow VHT if at least one channel on the sband supports 80 MHz */
142 		bool have_80mhz = false;
143 
144 		for (i = 0; i < sband->n_channels; i++) {
145 			if (sband->channels[i].flags & (IEEE80211_CHAN_DISABLED |
146 							IEEE80211_CHAN_NO_80MHZ))
147 				continue;
148 
149 			have_80mhz = true;
150 			break;
151 		}
152 
153 		if (!have_80mhz)
154 			return;
155 	}
156 
157 	/*
158 	 * A VHT STA must support 40 MHz, but if we verify that here
159 	 * then we break a few things - some APs (e.g. Netgear R6300v2
160 	 * and others based on the BCM4360 chipset) will unset this
161 	 * capability bit when operating in 20 MHz.
162 	 */
163 
164 	vht_cap->vht_supported = true;
165 
166 	own_cap = *own_vht_cap;
167 	/*
168 	 * If user has specified capability overrides, take care
169 	 * of that if the station we're setting up is the AP that
170 	 * we advertised a restricted capability set to. Override
171 	 * our own capabilities and then use those below.
172 	 */
173 	if (sdata->vif.type == NL80211_IFTYPE_STATION &&
174 	    !test_sta_flag(link_sta->sta, WLAN_STA_TDLS_PEER))
175 		ieee80211_apply_vhtcap_overrides(sdata, &own_cap);
176 
177 	/* take some capabilities as-is */
178 	cap_info = le32_to_cpu(vht_cap_ie->vht_cap_info);
179 	vht_cap->cap = cap_info;
180 	vht_cap->cap &= IEEE80211_VHT_CAP_RXLDPC |
181 			IEEE80211_VHT_CAP_VHT_TXOP_PS |
182 			IEEE80211_VHT_CAP_HTC_VHT |
183 			IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK |
184 			IEEE80211_VHT_CAP_VHT_LINK_ADAPTATION_VHT_UNSOL_MFB |
185 			IEEE80211_VHT_CAP_VHT_LINK_ADAPTATION_VHT_MRQ_MFB |
186 			IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN |
187 			IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN;
188 
189 	vht_cap->cap |= min_t(u32, cap_info & IEEE80211_VHT_CAP_MAX_MPDU_MASK,
190 			      own_cap.cap & IEEE80211_VHT_CAP_MAX_MPDU_MASK);
191 
192 	/* and some based on our own capabilities */
193 	switch (own_cap.cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK) {
194 	case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ:
195 		vht_cap->cap |= cap_info &
196 				IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
197 		break;
198 	case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ:
199 		vht_cap->cap |= cap_info &
200 				IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
201 		break;
202 	default:
203 		/* nothing */
204 		break;
205 	}
206 
207 	/* symmetric capabilities */
208 	vht_cap->cap |= cap_info & own_cap.cap &
209 			(IEEE80211_VHT_CAP_SHORT_GI_80 |
210 			 IEEE80211_VHT_CAP_SHORT_GI_160);
211 
212 	/* remaining ones */
213 	if (own_cap.cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)
214 		vht_cap->cap |= cap_info &
215 				(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
216 				 IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
217 
218 	if (own_cap.cap & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)
219 		vht_cap->cap |= cap_info &
220 				(IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
221 				 IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK);
222 
223 	if (own_cap.cap & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)
224 		vht_cap->cap |= cap_info &
225 				IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
226 
227 	if (own_cap.cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)
228 		vht_cap->cap |= cap_info &
229 				IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE;
230 
231 	if (own_cap.cap & IEEE80211_VHT_CAP_TXSTBC)
232 		vht_cap->cap |= cap_info & IEEE80211_VHT_CAP_RXSTBC_MASK;
233 
234 	if (own_cap.cap & IEEE80211_VHT_CAP_RXSTBC_MASK)
235 		vht_cap->cap |= cap_info & IEEE80211_VHT_CAP_TXSTBC;
236 
237 	/* Copy peer MCS info, the driver might need them. */
238 	memcpy(&vht_cap->vht_mcs, &vht_cap_ie->supp_mcs,
239 	       sizeof(struct ieee80211_vht_mcs_info));
240 
241 	/* copy EXT_NSS_BW Support value or remove the capability */
242 	if (ieee80211_hw_check(&sdata->local->hw, SUPPORTS_VHT_EXT_NSS_BW))
243 		vht_cap->cap |= (cap_info & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK);
244 	else
245 		vht_cap->vht_mcs.tx_highest &=
246 			~cpu_to_le16(IEEE80211_VHT_EXT_NSS_BW_CAPABLE);
247 
248 	/* but also restrict MCSes */
249 	for (i = 0; i < 8; i++) {
250 		u16 own_rx, own_tx, peer_rx, peer_tx;
251 
252 		own_rx = le16_to_cpu(own_cap.vht_mcs.rx_mcs_map);
253 		own_rx = (own_rx >> i * 2) & IEEE80211_VHT_MCS_NOT_SUPPORTED;
254 
255 		own_tx = le16_to_cpu(own_cap.vht_mcs.tx_mcs_map);
256 		own_tx = (own_tx >> i * 2) & IEEE80211_VHT_MCS_NOT_SUPPORTED;
257 
258 		peer_rx = le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
259 		peer_rx = (peer_rx >> i * 2) & IEEE80211_VHT_MCS_NOT_SUPPORTED;
260 
261 		peer_tx = le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map);
262 		peer_tx = (peer_tx >> i * 2) & IEEE80211_VHT_MCS_NOT_SUPPORTED;
263 
264 		if (peer_tx != IEEE80211_VHT_MCS_NOT_SUPPORTED) {
265 			if (own_rx == IEEE80211_VHT_MCS_NOT_SUPPORTED)
266 				peer_tx = IEEE80211_VHT_MCS_NOT_SUPPORTED;
267 			else if (own_rx < peer_tx)
268 				peer_tx = own_rx;
269 		}
270 
271 		if (peer_rx != IEEE80211_VHT_MCS_NOT_SUPPORTED) {
272 			if (own_tx == IEEE80211_VHT_MCS_NOT_SUPPORTED)
273 				peer_rx = IEEE80211_VHT_MCS_NOT_SUPPORTED;
274 			else if (own_tx < peer_rx)
275 				peer_rx = own_tx;
276 		}
277 
278 		vht_cap->vht_mcs.rx_mcs_map &=
279 			~cpu_to_le16(IEEE80211_VHT_MCS_NOT_SUPPORTED << i * 2);
280 		vht_cap->vht_mcs.rx_mcs_map |= cpu_to_le16(peer_rx << i * 2);
281 
282 		vht_cap->vht_mcs.tx_mcs_map &=
283 			~cpu_to_le16(IEEE80211_VHT_MCS_NOT_SUPPORTED << i * 2);
284 		vht_cap->vht_mcs.tx_mcs_map |= cpu_to_le16(peer_tx << i * 2);
285 	}
286 
287 	/*
288 	 * This is a workaround for VHT-enabled STAs which break the spec
289 	 * and have the VHT-MCS Rx map filled in with value 3 for all eight
290 	 * spatial streams, an example is AR9462.
291 	 *
292 	 * As per spec, in section 22.1.1 Introduction to the VHT PHY
293 	 * A VHT STA shall support at least single spatial stream VHT-MCSs
294 	 * 0 to 7 (transmit and receive) in all supported channel widths.
295 	 */
296 	if (vht_cap->vht_mcs.rx_mcs_map == cpu_to_le16(0xFFFF)) {
297 		vht_cap->vht_supported = false;
298 		sdata_info(sdata,
299 			   "Ignoring VHT IE from %pM (link:%pM) due to invalid rx_mcs_map\n",
300 			   link_sta->sta->addr, link_sta->addr);
301 		return;
302 	}
303 
304 	/* finally set up the bandwidth */
305 	switch (vht_cap->cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK) {
306 	case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ:
307 	case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ:
308 		link_sta->cur_max_bandwidth = IEEE80211_STA_RX_BW_160;
309 		break;
310 	default:
311 		link_sta->cur_max_bandwidth = IEEE80211_STA_RX_BW_80;
312 
313 		if (!(vht_cap->vht_mcs.tx_highest &
314 				cpu_to_le16(IEEE80211_VHT_EXT_NSS_BW_CAPABLE)))
315 			break;
316 
317 		/*
318 		 * If this is non-zero, then it does support 160 MHz after all,
319 		 * in one form or the other. We don't distinguish here (or even
320 		 * above) between 160 and 80+80 yet.
321 		 */
322 		if (cap_info & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK)
323 			link_sta->cur_max_bandwidth =
324 				IEEE80211_STA_RX_BW_160;
325 	}
326 
327 	if (sdata->vif.type != NL80211_IFTYPE_NAN)
328 		link_sta->pub->bandwidth = ieee80211_sta_cur_vht_bw(link_sta);
329 
330 	/*
331 	 * Work around the Cisco 9115 FW 17.3 bug by taking the min of
332 	 * both reported MPDU lengths.
333 	 */
334 	mpdu_len = vht_cap->cap & IEEE80211_VHT_CAP_MAX_MPDU_MASK;
335 	if (vht_cap_ie2)
336 		mpdu_len = min_t(u32, mpdu_len,
337 				 le32_get_bits(vht_cap_ie2->vht_cap_info,
338 					       IEEE80211_VHT_CAP_MAX_MPDU_MASK));
339 
340 	/*
341 	 * FIXME - should the amsdu len be per link? store per link
342 	 * and maintain a minimum?
343 	 */
344 	switch (mpdu_len) {
345 	case IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454:
346 		link_sta->pub->agg.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_VHT_11454;
347 		break;
348 	case IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_7991:
349 		link_sta->pub->agg.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_VHT_7991;
350 		break;
351 	case IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_3895:
352 	default:
353 		link_sta->pub->agg.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_VHT_3895;
354 		break;
355 	}
356 
357 	ieee80211_sta_recalc_aggregates(&link_sta->sta->sta);
358 }
359 
360 /* FIXME: move this to some better location - parses HE/EHT now */
361 static enum ieee80211_sta_rx_bandwidth
362 __ieee80211_sta_cap_rx_bw(struct link_sta_info *link_sta,
363 			  struct cfg80211_chan_def *chandef)
364 {
365 	unsigned int link_id = link_sta->link_id;
366 	struct ieee80211_sub_if_data *sdata = link_sta->sta->sdata;
367 	struct ieee80211_sta_vht_cap *vht_cap = &link_sta->pub->vht_cap;
368 	struct ieee80211_sta_he_cap *he_cap = &link_sta->pub->he_cap;
369 	struct ieee80211_sta_eht_cap *eht_cap = &link_sta->pub->eht_cap;
370 	u32 cap_width;
371 
372 	if (he_cap->has_he) {
373 		enum nl80211_band band;
374 		u8 info;
375 
376 		if (chandef) {
377 			band = chandef->chan->band;
378 		} else {
379 			struct ieee80211_bss_conf *link_conf;
380 
381 			if (WARN_ON_ONCE(sdata->vif.type == NL80211_IFTYPE_NAN_DATA ||
382 					 sdata->vif.type == NL80211_IFTYPE_NAN))
383 				return IEEE80211_STA_RX_BW_20;
384 
385 			rcu_read_lock();
386 			link_conf = rcu_dereference(sdata->vif.link_conf[link_id]);
387 			band = link_conf->chanreq.oper.chan->band;
388 			rcu_read_unlock();
389 		}
390 
391 		if (eht_cap->has_eht && band == NL80211_BAND_6GHZ) {
392 			info = eht_cap->eht_cap_elem.phy_cap_info[0];
393 
394 			if (info & IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ)
395 				return IEEE80211_STA_RX_BW_320;
396 		}
397 
398 		info = he_cap->he_cap_elem.phy_cap_info[0];
399 
400 		if (band == NL80211_BAND_2GHZ) {
401 			if (info & IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G)
402 				return IEEE80211_STA_RX_BW_40;
403 			return IEEE80211_STA_RX_BW_20;
404 		}
405 
406 		if (info & IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G ||
407 		    info & IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)
408 			return IEEE80211_STA_RX_BW_160;
409 
410 		if (info & IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G)
411 			return IEEE80211_STA_RX_BW_80;
412 
413 		return IEEE80211_STA_RX_BW_20;
414 	}
415 
416 	if (!vht_cap->vht_supported)
417 		return link_sta->pub->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 ?
418 				IEEE80211_STA_RX_BW_40 :
419 				IEEE80211_STA_RX_BW_20;
420 
421 	cap_width = vht_cap->cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
422 
423 	if (cap_width == IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ ||
424 	    cap_width == IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ)
425 		return IEEE80211_STA_RX_BW_160;
426 
427 	/*
428 	 * If this is non-zero, then it does support 160 MHz after all,
429 	 * in one form or the other. We don't distinguish here (or even
430 	 * above) between 160 and 80+80 yet.
431 	 */
432 	if (vht_cap->cap & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK)
433 		return IEEE80211_STA_RX_BW_160;
434 
435 	return IEEE80211_STA_RX_BW_80;
436 }
437 
438 enum ieee80211_sta_rx_bandwidth
439 _ieee80211_sta_cap_rx_bw(struct link_sta_info *link_sta,
440 			 struct cfg80211_chan_def *chandef)
441 {
442 	/*
443 	 * With RX OMI, also pretend that the STA's capability changed.
444 	 * Of course this isn't really true, it didn't change, only our
445 	 * RX capability was changed by notifying RX OMI to the STA.
446 	 * The purpose, however, is to save power, and that requires
447 	 * changing also transmissions to the AP and the chanctx. The
448 	 * transmissions depend on link_sta->bandwidth which is set in
449 	 * _ieee80211_sta_cur_vht_bw() below, but the chanctx depends
450 	 * on the result of this function which is also called by
451 	 * _ieee80211_sta_cur_vht_bw(), so we need to do that here as
452 	 * well. This is sufficient for the steady state, but during
453 	 * the transition we already need to change TX/RX separately,
454 	 * so _ieee80211_sta_cur_vht_bw() below applies the _tx one.
455 	 */
456 	return min(__ieee80211_sta_cap_rx_bw(link_sta, chandef),
457 		   link_sta->rx_omi_bw_rx);
458 }
459 
460 enum nl80211_chan_width
461 ieee80211_sta_cap_chan_bw(struct link_sta_info *link_sta)
462 {
463 	struct ieee80211_sta_vht_cap *vht_cap = &link_sta->pub->vht_cap;
464 	u32 cap_width;
465 
466 	if (!vht_cap->vht_supported) {
467 		if (!link_sta->pub->ht_cap.ht_supported)
468 			return NL80211_CHAN_WIDTH_20_NOHT;
469 
470 		return link_sta->pub->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 ?
471 				NL80211_CHAN_WIDTH_40 : NL80211_CHAN_WIDTH_20;
472 	}
473 
474 	cap_width = vht_cap->cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
475 
476 	if (cap_width == IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ)
477 		return NL80211_CHAN_WIDTH_160;
478 	else if (cap_width == IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ)
479 		return NL80211_CHAN_WIDTH_80P80;
480 
481 	return NL80211_CHAN_WIDTH_80;
482 }
483 
484 enum nl80211_chan_width
485 ieee80211_sta_rx_bw_to_chan_width(struct link_sta_info *link_sta)
486 {
487 	enum ieee80211_sta_rx_bandwidth cur_bw =
488 		link_sta->pub->bandwidth;
489 	struct ieee80211_sta_vht_cap *vht_cap =
490 		&link_sta->pub->vht_cap;
491 	u32 cap_width;
492 
493 	switch (cur_bw) {
494 	case IEEE80211_STA_RX_BW_20:
495 		if (!link_sta->pub->ht_cap.ht_supported)
496 			return NL80211_CHAN_WIDTH_20_NOHT;
497 		else
498 			return NL80211_CHAN_WIDTH_20;
499 	case IEEE80211_STA_RX_BW_40:
500 		return NL80211_CHAN_WIDTH_40;
501 	case IEEE80211_STA_RX_BW_80:
502 		return NL80211_CHAN_WIDTH_80;
503 	case IEEE80211_STA_RX_BW_160:
504 		cap_width =
505 			vht_cap->cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
506 
507 		if (cap_width == IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ)
508 			return NL80211_CHAN_WIDTH_160;
509 
510 		return NL80211_CHAN_WIDTH_80P80;
511 	default:
512 		return NL80211_CHAN_WIDTH_20;
513 	}
514 }
515 
516 /* FIXME: rename/move - this deals with everything not just VHT */
517 enum ieee80211_sta_rx_bandwidth
518 _ieee80211_sta_cur_vht_bw(struct link_sta_info *link_sta,
519 			  struct cfg80211_chan_def *chandef)
520 {
521 	struct sta_info *sta = link_sta->sta;
522 	enum nl80211_chan_width bss_width;
523 	enum ieee80211_sta_rx_bandwidth bw;
524 
525 	if (chandef) {
526 		bss_width = chandef->width;
527 	} else {
528 		struct ieee80211_bss_conf *link_conf;
529 
530 		/* NAN operates on multiple channels so a chandef must be given */
531 		if (WARN_ON_ONCE(sta->sdata->vif.type == NL80211_IFTYPE_NAN ||
532 				 sta->sdata->vif.type == NL80211_IFTYPE_NAN_DATA))
533 			return IEEE80211_STA_RX_BW_20;
534 
535 		rcu_read_lock();
536 		link_conf = rcu_dereference(sta->sdata->vif.link_conf[link_sta->link_id]);
537 		if (WARN_ON_ONCE(!link_conf)) {
538 			rcu_read_unlock();
539 			return IEEE80211_STA_RX_BW_20;
540 		}
541 		bss_width = link_conf->chanreq.oper.width;
542 		rcu_read_unlock();
543 	}
544 
545 	/* intentionally do not take rx_bw_omi_rx into account */
546 	bw = __ieee80211_sta_cap_rx_bw(link_sta, chandef);
547 	bw = min(bw, link_sta->cur_max_bandwidth);
548 	/* but do apply rx_omi_bw_tx */
549 	bw = min(bw, link_sta->rx_omi_bw_tx);
550 
551 	/* Don't consider AP's bandwidth for TDLS peers, section 11.23.1 of
552 	 * IEEE80211-2016 specification makes higher bandwidth operation
553 	 * possible on the TDLS link if the peers have wider bandwidth
554 	 * capability.
555 	 *
556 	 * However, in this case, and only if the TDLS peer is authorized,
557 	 * limit to the tdls_chandef so that the configuration here isn't
558 	 * wider than what's actually requested on the channel context.
559 	 */
560 	if (test_sta_flag(sta, WLAN_STA_TDLS_PEER) &&
561 	    test_sta_flag(sta, WLAN_STA_TDLS_WIDER_BW) &&
562 	    test_sta_flag(sta, WLAN_STA_AUTHORIZED) &&
563 	    sta->tdls_chandef.chan)
564 		bw = min(bw, ieee80211_chan_width_to_rx_bw(sta->tdls_chandef.width));
565 	else
566 		bw = min(bw, ieee80211_chan_width_to_rx_bw(bss_width));
567 
568 	return bw;
569 }
570 
571 void ieee80211_sta_init_nss(struct link_sta_info *link_sta)
572 {
573 	u8 ht_rx_nss = 0, vht_rx_nss = 0, he_rx_nss = 0, eht_rx_nss = 0, rx_nss;
574 	bool support_160;
575 
576 	if (link_sta->pub->eht_cap.has_eht) {
577 		int i;
578 		const u8 *rx_nss_mcs = (void *)&link_sta->pub->eht_cap.eht_mcs_nss_supp;
579 
580 		/* get the max nss for EHT over all possible bandwidths and mcs */
581 		for (i = 0; i < sizeof(struct ieee80211_eht_mcs_nss_supp); i++)
582 			eht_rx_nss = max_t(u8, eht_rx_nss,
583 					   u8_get_bits(rx_nss_mcs[i],
584 						       IEEE80211_EHT_MCS_NSS_RX));
585 	}
586 
587 	if (link_sta->pub->he_cap.has_he) {
588 		int i;
589 		u8 rx_mcs_80 = 0, rx_mcs_160 = 0;
590 		const struct ieee80211_sta_he_cap *he_cap = &link_sta->pub->he_cap;
591 		u16 mcs_160_map =
592 			le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_160);
593 		u16 mcs_80_map = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_80);
594 
595 		for (i = 7; i >= 0; i--) {
596 			u8 mcs_160 = (mcs_160_map >> (2 * i)) & 3;
597 
598 			if (mcs_160 != IEEE80211_HE_MCS_NOT_SUPPORTED) {
599 				rx_mcs_160 = i + 1;
600 				break;
601 			}
602 		}
603 		for (i = 7; i >= 0; i--) {
604 			u8 mcs_80 = (mcs_80_map >> (2 * i)) & 3;
605 
606 			if (mcs_80 != IEEE80211_HE_MCS_NOT_SUPPORTED) {
607 				rx_mcs_80 = i + 1;
608 				break;
609 			}
610 		}
611 
612 		support_160 = he_cap->he_cap_elem.phy_cap_info[0] &
613 			      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G;
614 
615 		if (support_160)
616 			he_rx_nss = min(rx_mcs_80, rx_mcs_160);
617 		else
618 			he_rx_nss = rx_mcs_80;
619 	}
620 
621 	if (link_sta->pub->ht_cap.ht_supported) {
622 		if (link_sta->pub->ht_cap.mcs.rx_mask[0])
623 			ht_rx_nss++;
624 		if (link_sta->pub->ht_cap.mcs.rx_mask[1])
625 			ht_rx_nss++;
626 		if (link_sta->pub->ht_cap.mcs.rx_mask[2])
627 			ht_rx_nss++;
628 		if (link_sta->pub->ht_cap.mcs.rx_mask[3])
629 			ht_rx_nss++;
630 		/* FIXME: consider rx_highest? */
631 	}
632 
633 	if (link_sta->pub->vht_cap.vht_supported) {
634 		int i;
635 		u16 rx_mcs_map;
636 
637 		rx_mcs_map = le16_to_cpu(link_sta->pub->vht_cap.vht_mcs.rx_mcs_map);
638 
639 		for (i = 7; i >= 0; i--) {
640 			u8 mcs = (rx_mcs_map >> (2 * i)) & 3;
641 
642 			if (mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED) {
643 				vht_rx_nss = i + 1;
644 				break;
645 			}
646 		}
647 		/* FIXME: consider rx_highest? */
648 	}
649 
650 	rx_nss = max(vht_rx_nss, ht_rx_nss);
651 	rx_nss = max(he_rx_nss, rx_nss);
652 	rx_nss = max(eht_rx_nss, rx_nss);
653 	rx_nss = max_t(u8, 1, rx_nss);
654 	link_sta->capa_nss = rx_nss;
655 
656 	/* that shouldn't be set yet, but we can handle it anyway */
657 	if (link_sta->op_mode_nss)
658 		link_sta->pub->rx_nss =
659 			min_t(u8, rx_nss, link_sta->op_mode_nss);
660 	else
661 		link_sta->pub->rx_nss = rx_nss;
662 }
663 
664 u32 __ieee80211_vht_handle_opmode(struct ieee80211_sub_if_data *sdata,
665 				  struct link_sta_info *link_sta,
666 				  u8 opmode, enum nl80211_band band)
667 {
668 	enum ieee80211_sta_rx_bandwidth new_bw;
669 	struct sta_opmode_info sta_opmode = {};
670 	u32 changed = 0;
671 	u8 nss;
672 
673 	/* ignore - no support for BF yet */
674 	if (opmode & IEEE80211_OPMODE_NOTIF_RX_NSS_TYPE_BF)
675 		return 0;
676 
677 	nss = opmode & IEEE80211_OPMODE_NOTIF_RX_NSS_MASK;
678 	nss >>= IEEE80211_OPMODE_NOTIF_RX_NSS_SHIFT;
679 	nss += 1;
680 
681 	if (link_sta->op_mode_nss != nss) {
682 		if (nss <= link_sta->capa_nss) {
683 			link_sta->op_mode_nss = nss;
684 
685 			if (nss != link_sta->pub->rx_nss) {
686 				link_sta->pub->rx_nss = nss;
687 				changed |= IEEE80211_RC_NSS_CHANGED;
688 				sta_opmode.rx_nss = link_sta->pub->rx_nss;
689 				sta_opmode.changed |= STA_OPMODE_N_SS_CHANGED;
690 			}
691 		} else {
692 			sdata_dbg(sdata,
693 				  "Ignore NSS change to invalid %d in VHT opmode notif from %pM",
694 				  nss, link_sta->pub->addr);
695 		}
696 	}
697 
698 	switch (opmode & IEEE80211_OPMODE_NOTIF_CHANWIDTH_MASK) {
699 	case IEEE80211_OPMODE_NOTIF_CHANWIDTH_20MHZ:
700 		/* ignore IEEE80211_OPMODE_NOTIF_BW_160_80P80 must not be set */
701 		link_sta->cur_max_bandwidth = IEEE80211_STA_RX_BW_20;
702 		break;
703 	case IEEE80211_OPMODE_NOTIF_CHANWIDTH_40MHZ:
704 		/* ignore IEEE80211_OPMODE_NOTIF_BW_160_80P80 must not be set */
705 		link_sta->cur_max_bandwidth = IEEE80211_STA_RX_BW_40;
706 		break;
707 	case IEEE80211_OPMODE_NOTIF_CHANWIDTH_80MHZ:
708 		if (opmode & IEEE80211_OPMODE_NOTIF_BW_160_80P80)
709 			link_sta->cur_max_bandwidth = IEEE80211_STA_RX_BW_160;
710 		else
711 			link_sta->cur_max_bandwidth = IEEE80211_STA_RX_BW_80;
712 		break;
713 	case IEEE80211_OPMODE_NOTIF_CHANWIDTH_160MHZ:
714 		/* legacy only, no longer used by newer spec */
715 		link_sta->cur_max_bandwidth = IEEE80211_STA_RX_BW_160;
716 		break;
717 	}
718 
719 	new_bw = ieee80211_sta_cur_vht_bw(link_sta);
720 	if (new_bw != link_sta->pub->bandwidth) {
721 		link_sta->pub->bandwidth = new_bw;
722 		sta_opmode.bw = ieee80211_sta_rx_bw_to_chan_width(link_sta);
723 		changed |= IEEE80211_RC_BW_CHANGED;
724 		sta_opmode.changed |= STA_OPMODE_MAX_BW_CHANGED;
725 	}
726 
727 	if (sta_opmode.changed)
728 		cfg80211_sta_opmode_change_notify(sdata->dev, link_sta->addr,
729 						  &sta_opmode, GFP_KERNEL);
730 
731 	return changed;
732 }
733 
734 void ieee80211_process_mu_groups(struct ieee80211_sub_if_data *sdata,
735 				 struct ieee80211_link_data *link,
736 				 struct ieee80211_mgmt *mgmt)
737 {
738 	struct ieee80211_bss_conf *link_conf = link->conf;
739 
740 	if (!link_conf->mu_mimo_owner)
741 		return;
742 
743 	if (!memcmp(mgmt->u.action.vht_group_notif.position,
744 		    link_conf->mu_group.position, WLAN_USER_POSITION_LEN) &&
745 	    !memcmp(mgmt->u.action.vht_group_notif.membership,
746 		    link_conf->mu_group.membership, WLAN_MEMBERSHIP_LEN))
747 		return;
748 
749 	memcpy(link_conf->mu_group.membership,
750 	       mgmt->u.action.vht_group_notif.membership,
751 	       WLAN_MEMBERSHIP_LEN);
752 	memcpy(link_conf->mu_group.position,
753 	       mgmt->u.action.vht_group_notif.position,
754 	       WLAN_USER_POSITION_LEN);
755 
756 	ieee80211_link_info_change_notify(sdata, link,
757 					  BSS_CHANGED_MU_GROUPS);
758 }
759 
760 void ieee80211_update_mu_groups(struct ieee80211_vif *vif, unsigned int link_id,
761 				const u8 *membership, const u8 *position)
762 {
763 	struct ieee80211_bss_conf *link_conf;
764 
765 	rcu_read_lock();
766 	link_conf = rcu_dereference(vif->link_conf[link_id]);
767 
768 	if (!WARN_ON_ONCE(!link_conf || !link_conf->mu_mimo_owner)) {
769 		memcpy(link_conf->mu_group.membership, membership,
770 		       WLAN_MEMBERSHIP_LEN);
771 		memcpy(link_conf->mu_group.position, position,
772 		       WLAN_USER_POSITION_LEN);
773 	}
774 	rcu_read_unlock();
775 }
776 EXPORT_SYMBOL_GPL(ieee80211_update_mu_groups);
777 
778 void ieee80211_vht_handle_opmode(struct ieee80211_sub_if_data *sdata,
779 				 struct link_sta_info *link_sta,
780 				 u8 opmode, enum nl80211_band band)
781 {
782 	struct ieee80211_local *local = sdata->local;
783 	struct ieee80211_supported_band *sband = local->hw.wiphy->bands[band];
784 
785 	u32 changed = __ieee80211_vht_handle_opmode(sdata, link_sta,
786 						    opmode, band);
787 
788 	if (changed > 0) {
789 		ieee80211_recalc_min_chandef(sdata, link_sta->link_id);
790 		rate_control_rate_update(local, sband, link_sta, changed);
791 	}
792 }
793 
794 void ieee80211_get_vht_mask_from_cap(__le16 vht_cap,
795 				     u16 vht_mask[NL80211_VHT_NSS_MAX])
796 {
797 	int i;
798 	u16 mask, cap = le16_to_cpu(vht_cap);
799 
800 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
801 		mask = (cap >> i * 2) & IEEE80211_VHT_MCS_NOT_SUPPORTED;
802 		switch (mask) {
803 		case IEEE80211_VHT_MCS_SUPPORT_0_7:
804 			vht_mask[i] = 0x00FF;
805 			break;
806 		case IEEE80211_VHT_MCS_SUPPORT_0_8:
807 			vht_mask[i] = 0x01FF;
808 			break;
809 		case IEEE80211_VHT_MCS_SUPPORT_0_9:
810 			vht_mask[i] = 0x03FF;
811 			break;
812 		case IEEE80211_VHT_MCS_NOT_SUPPORTED:
813 		default:
814 			vht_mask[i] = 0;
815 			break;
816 		}
817 	}
818 }
819