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