Linux Audio

Check our new training course

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