xref: /openbmc/linux/net/mac80211/vht.c (revision 9cfc5c90)
1 /*
2  * VHT handling
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  */
8 
9 #include <linux/ieee80211.h>
10 #include <linux/export.h>
11 #include <net/mac80211.h>
12 #include "ieee80211_i.h"
13 #include "rate.h"
14 
15 
16 static void __check_vhtcap_disable(struct ieee80211_sub_if_data *sdata,
17 				   struct ieee80211_sta_vht_cap *vht_cap,
18 				   u32 flag)
19 {
20 	__le32 le_flag = cpu_to_le32(flag);
21 
22 	if (sdata->u.mgd.vht_capa_mask.vht_cap_info & le_flag &&
23 	    !(sdata->u.mgd.vht_capa.vht_cap_info & le_flag))
24 		vht_cap->cap &= ~flag;
25 }
26 
27 void ieee80211_apply_vhtcap_overrides(struct ieee80211_sub_if_data *sdata,
28 				      struct ieee80211_sta_vht_cap *vht_cap)
29 {
30 	int i;
31 	u16 rxmcs_mask, rxmcs_cap, rxmcs_n, txmcs_mask, txmcs_cap, txmcs_n;
32 
33 	if (!vht_cap->vht_supported)
34 		return;
35 
36 	if (sdata->vif.type != NL80211_IFTYPE_STATION)
37 		return;
38 
39 	__check_vhtcap_disable(sdata, vht_cap,
40 			       IEEE80211_VHT_CAP_RXLDPC);
41 	__check_vhtcap_disable(sdata, vht_cap,
42 			       IEEE80211_VHT_CAP_SHORT_GI_80);
43 	__check_vhtcap_disable(sdata, vht_cap,
44 			       IEEE80211_VHT_CAP_SHORT_GI_160);
45 	__check_vhtcap_disable(sdata, vht_cap,
46 			       IEEE80211_VHT_CAP_TXSTBC);
47 	__check_vhtcap_disable(sdata, vht_cap,
48 			       IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE);
49 	__check_vhtcap_disable(sdata, vht_cap,
50 			       IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE);
51 	__check_vhtcap_disable(sdata, vht_cap,
52 			       IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN);
53 	__check_vhtcap_disable(sdata, vht_cap,
54 			       IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN);
55 
56 	/* Allow user to decrease AMPDU length exponent */
57 	if (sdata->u.mgd.vht_capa_mask.vht_cap_info &
58 	    cpu_to_le32(IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK)) {
59 		u32 cap, n;
60 
61 		n = le32_to_cpu(sdata->u.mgd.vht_capa.vht_cap_info) &
62 			IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
63 		n >>= IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
64 		cap = vht_cap->cap & IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
65 		cap >>= IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
66 
67 		if (n < cap) {
68 			vht_cap->cap &=
69 				~IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
70 			vht_cap->cap |=
71 				n << IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
72 		}
73 	}
74 
75 	/* Allow the user to decrease MCSes */
76 	rxmcs_mask =
77 		le16_to_cpu(sdata->u.mgd.vht_capa_mask.supp_mcs.rx_mcs_map);
78 	rxmcs_n = le16_to_cpu(sdata->u.mgd.vht_capa.supp_mcs.rx_mcs_map);
79 	rxmcs_n &= rxmcs_mask;
80 	rxmcs_cap = le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
81 
82 	txmcs_mask =
83 		le16_to_cpu(sdata->u.mgd.vht_capa_mask.supp_mcs.tx_mcs_map);
84 	txmcs_n = le16_to_cpu(sdata->u.mgd.vht_capa.supp_mcs.tx_mcs_map);
85 	txmcs_n &= txmcs_mask;
86 	txmcs_cap = le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map);
87 	for (i = 0; i < 8; i++) {
88 		u8 m, n, c;
89 
90 		m = (rxmcs_mask >> 2*i) & IEEE80211_VHT_MCS_NOT_SUPPORTED;
91 		n = (rxmcs_n >> 2*i) & IEEE80211_VHT_MCS_NOT_SUPPORTED;
92 		c = (rxmcs_cap >> 2*i) & IEEE80211_VHT_MCS_NOT_SUPPORTED;
93 
94 		if (m && ((c != IEEE80211_VHT_MCS_NOT_SUPPORTED && n < c) ||
95 			  n == IEEE80211_VHT_MCS_NOT_SUPPORTED)) {
96 			rxmcs_cap &= ~(3 << 2*i);
97 			rxmcs_cap |= (rxmcs_n & (3 << 2*i));
98 		}
99 
100 		m = (txmcs_mask >> 2*i) & IEEE80211_VHT_MCS_NOT_SUPPORTED;
101 		n = (txmcs_n >> 2*i) & IEEE80211_VHT_MCS_NOT_SUPPORTED;
102 		c = (txmcs_cap >> 2*i) & IEEE80211_VHT_MCS_NOT_SUPPORTED;
103 
104 		if (m && ((c != IEEE80211_VHT_MCS_NOT_SUPPORTED && n < c) ||
105 			  n == IEEE80211_VHT_MCS_NOT_SUPPORTED)) {
106 			txmcs_cap &= ~(3 << 2*i);
107 			txmcs_cap |= (txmcs_n & (3 << 2*i));
108 		}
109 	}
110 	vht_cap->vht_mcs.rx_mcs_map = cpu_to_le16(rxmcs_cap);
111 	vht_cap->vht_mcs.tx_mcs_map = cpu_to_le16(txmcs_cap);
112 }
113 
114 void
115 ieee80211_vht_cap_ie_to_sta_vht_cap(struct ieee80211_sub_if_data *sdata,
116 				    struct ieee80211_supported_band *sband,
117 				    const struct ieee80211_vht_cap *vht_cap_ie,
118 				    struct sta_info *sta)
119 {
120 	struct ieee80211_sta_vht_cap *vht_cap = &sta->sta.vht_cap;
121 	struct ieee80211_sta_vht_cap own_cap;
122 	u32 cap_info, i;
123 	bool have_80mhz;
124 
125 	memset(vht_cap, 0, sizeof(*vht_cap));
126 
127 	if (!sta->sta.ht_cap.ht_supported)
128 		return;
129 
130 	if (!vht_cap_ie || !sband->vht_cap.vht_supported)
131 		return;
132 
133 	/* Allow VHT if at least one channel on the sband supports 80 MHz */
134 	have_80mhz = false;
135 	for (i = 0; i < sband->n_channels; i++) {
136 		if (sband->channels[i].flags & (IEEE80211_CHAN_DISABLED |
137 						IEEE80211_CHAN_NO_80MHZ))
138 			continue;
139 
140 		have_80mhz = true;
141 		break;
142 	}
143 
144 	if (!have_80mhz)
145 		return;
146 
147 	/*
148 	 * A VHT STA must support 40 MHz, but if we verify that here
149 	 * then we break a few things - some APs (e.g. Netgear R6300v2
150 	 * and others based on the BCM4360 chipset) will unset this
151 	 * capability bit when operating in 20 MHz.
152 	 */
153 
154 	vht_cap->vht_supported = true;
155 
156 	own_cap = sband->vht_cap;
157 	/*
158 	 * If user has specified capability overrides, take care
159 	 * of that if the station we're setting up is the AP that
160 	 * we advertised a restricted capability set to. Override
161 	 * our own capabilities and then use those below.
162 	 */
163 	if (sdata->vif.type == NL80211_IFTYPE_STATION &&
164 	    !test_sta_flag(sta, WLAN_STA_TDLS_PEER))
165 		ieee80211_apply_vhtcap_overrides(sdata, &own_cap);
166 
167 	/* take some capabilities as-is */
168 	cap_info = le32_to_cpu(vht_cap_ie->vht_cap_info);
169 	vht_cap->cap = cap_info;
170 	vht_cap->cap &= IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_3895 |
171 			IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_7991 |
172 			IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
173 			IEEE80211_VHT_CAP_RXLDPC |
174 			IEEE80211_VHT_CAP_VHT_TXOP_PS |
175 			IEEE80211_VHT_CAP_HTC_VHT |
176 			IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK |
177 			IEEE80211_VHT_CAP_VHT_LINK_ADAPTATION_VHT_UNSOL_MFB |
178 			IEEE80211_VHT_CAP_VHT_LINK_ADAPTATION_VHT_MRQ_MFB |
179 			IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN |
180 			IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN;
181 
182 	/* and some based on our own capabilities */
183 	switch (own_cap.cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK) {
184 	case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ:
185 		vht_cap->cap |= cap_info &
186 				IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
187 		break;
188 	case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ:
189 		vht_cap->cap |= cap_info &
190 				IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
191 		break;
192 	default:
193 		/* nothing */
194 		break;
195 	}
196 
197 	/* symmetric capabilities */
198 	vht_cap->cap |= cap_info & own_cap.cap &
199 			(IEEE80211_VHT_CAP_SHORT_GI_80 |
200 			 IEEE80211_VHT_CAP_SHORT_GI_160);
201 
202 	/* remaining ones */
203 	if (own_cap.cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)
204 		vht_cap->cap |= cap_info &
205 				(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
206 				 IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
207 
208 	if (own_cap.cap & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)
209 		vht_cap->cap |= cap_info &
210 				(IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
211 				 IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK);
212 
213 	if (own_cap.cap & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)
214 		vht_cap->cap |= cap_info &
215 				IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
216 
217 	if (own_cap.cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)
218 		vht_cap->cap |= cap_info &
219 				IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE;
220 
221 	if (own_cap.cap & IEEE80211_VHT_CAP_TXSTBC)
222 		vht_cap->cap |= cap_info & IEEE80211_VHT_CAP_RXSTBC_MASK;
223 
224 	if (own_cap.cap & IEEE80211_VHT_CAP_RXSTBC_MASK)
225 		vht_cap->cap |= cap_info & IEEE80211_VHT_CAP_TXSTBC;
226 
227 	/* Copy peer MCS info, the driver might need them. */
228 	memcpy(&vht_cap->vht_mcs, &vht_cap_ie->supp_mcs,
229 	       sizeof(struct ieee80211_vht_mcs_info));
230 
231 	/* but also restrict MCSes */
232 	for (i = 0; i < 8; i++) {
233 		u16 own_rx, own_tx, peer_rx, peer_tx;
234 
235 		own_rx = le16_to_cpu(own_cap.vht_mcs.rx_mcs_map);
236 		own_rx = (own_rx >> i * 2) & IEEE80211_VHT_MCS_NOT_SUPPORTED;
237 
238 		own_tx = le16_to_cpu(own_cap.vht_mcs.tx_mcs_map);
239 		own_tx = (own_tx >> i * 2) & IEEE80211_VHT_MCS_NOT_SUPPORTED;
240 
241 		peer_rx = le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
242 		peer_rx = (peer_rx >> i * 2) & IEEE80211_VHT_MCS_NOT_SUPPORTED;
243 
244 		peer_tx = le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map);
245 		peer_tx = (peer_tx >> i * 2) & IEEE80211_VHT_MCS_NOT_SUPPORTED;
246 
247 		if (peer_tx != IEEE80211_VHT_MCS_NOT_SUPPORTED) {
248 			if (own_rx == IEEE80211_VHT_MCS_NOT_SUPPORTED)
249 				peer_tx = IEEE80211_VHT_MCS_NOT_SUPPORTED;
250 			else if (own_rx < peer_tx)
251 				peer_tx = own_rx;
252 		}
253 
254 		if (peer_rx != IEEE80211_VHT_MCS_NOT_SUPPORTED) {
255 			if (own_tx == IEEE80211_VHT_MCS_NOT_SUPPORTED)
256 				peer_rx = IEEE80211_VHT_MCS_NOT_SUPPORTED;
257 			else if (own_tx < peer_rx)
258 				peer_rx = own_tx;
259 		}
260 
261 		vht_cap->vht_mcs.rx_mcs_map &=
262 			~cpu_to_le16(IEEE80211_VHT_MCS_NOT_SUPPORTED << i * 2);
263 		vht_cap->vht_mcs.rx_mcs_map |= cpu_to_le16(peer_rx << i * 2);
264 
265 		vht_cap->vht_mcs.tx_mcs_map &=
266 			~cpu_to_le16(IEEE80211_VHT_MCS_NOT_SUPPORTED << i * 2);
267 		vht_cap->vht_mcs.tx_mcs_map |= cpu_to_le16(peer_tx << i * 2);
268 	}
269 
270 	/* finally set up the bandwidth */
271 	switch (vht_cap->cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK) {
272 	case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ:
273 	case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ:
274 		sta->cur_max_bandwidth = IEEE80211_STA_RX_BW_160;
275 		break;
276 	default:
277 		sta->cur_max_bandwidth = IEEE80211_STA_RX_BW_80;
278 	}
279 
280 	sta->sta.bandwidth = ieee80211_sta_cur_vht_bw(sta);
281 }
282 
283 enum ieee80211_sta_rx_bandwidth ieee80211_sta_cap_rx_bw(struct sta_info *sta)
284 {
285 	struct ieee80211_sta_vht_cap *vht_cap = &sta->sta.vht_cap;
286 	u32 cap_width;
287 
288 	if (!vht_cap->vht_supported)
289 		return sta->sta.ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 ?
290 				IEEE80211_STA_RX_BW_40 :
291 				IEEE80211_STA_RX_BW_20;
292 
293 	cap_width = vht_cap->cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
294 
295 	if (cap_width == IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ ||
296 	    cap_width == IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ)
297 		return IEEE80211_STA_RX_BW_160;
298 
299 	return IEEE80211_STA_RX_BW_80;
300 }
301 
302 static enum ieee80211_sta_rx_bandwidth
303 ieee80211_chan_width_to_rx_bw(enum nl80211_chan_width width)
304 {
305 	switch (width) {
306 	case NL80211_CHAN_WIDTH_20_NOHT:
307 	case NL80211_CHAN_WIDTH_20:
308 		return IEEE80211_STA_RX_BW_20;
309 	case NL80211_CHAN_WIDTH_40:
310 		return IEEE80211_STA_RX_BW_40;
311 	case NL80211_CHAN_WIDTH_80:
312 		return IEEE80211_STA_RX_BW_80;
313 	case NL80211_CHAN_WIDTH_160:
314 	case NL80211_CHAN_WIDTH_80P80:
315 		return IEEE80211_STA_RX_BW_160;
316 	default:
317 		WARN_ON_ONCE(1);
318 		return IEEE80211_STA_RX_BW_20;
319 	}
320 }
321 
322 enum ieee80211_sta_rx_bandwidth ieee80211_sta_cur_vht_bw(struct sta_info *sta)
323 {
324 	struct ieee80211_sub_if_data *sdata = sta->sdata;
325 	enum ieee80211_sta_rx_bandwidth bw;
326 	enum nl80211_chan_width bss_width = sdata->vif.bss_conf.chandef.width;
327 
328 	bw = ieee80211_sta_cap_rx_bw(sta);
329 	bw = min(bw, sta->cur_max_bandwidth);
330 
331 	/* do not cap the BW of TDLS WIDER_BW peers by the bss */
332 	if (!test_sta_flag(sta, WLAN_STA_TDLS_WIDER_BW))
333 		bw = min(bw, ieee80211_chan_width_to_rx_bw(bss_width));
334 
335 	return bw;
336 }
337 
338 void ieee80211_sta_set_rx_nss(struct sta_info *sta)
339 {
340 	u8 ht_rx_nss = 0, vht_rx_nss = 0;
341 
342 	/* if we received a notification already don't overwrite it */
343 	if (sta->sta.rx_nss)
344 		return;
345 
346 	if (sta->sta.ht_cap.ht_supported) {
347 		if (sta->sta.ht_cap.mcs.rx_mask[0])
348 			ht_rx_nss++;
349 		if (sta->sta.ht_cap.mcs.rx_mask[1])
350 			ht_rx_nss++;
351 		if (sta->sta.ht_cap.mcs.rx_mask[2])
352 			ht_rx_nss++;
353 		if (sta->sta.ht_cap.mcs.rx_mask[3])
354 			ht_rx_nss++;
355 		/* FIXME: consider rx_highest? */
356 	}
357 
358 	if (sta->sta.vht_cap.vht_supported) {
359 		int i;
360 		u16 rx_mcs_map;
361 
362 		rx_mcs_map = le16_to_cpu(sta->sta.vht_cap.vht_mcs.rx_mcs_map);
363 
364 		for (i = 7; i >= 0; i--) {
365 			u8 mcs = (rx_mcs_map >> (2 * i)) & 3;
366 
367 			if (mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED) {
368 				vht_rx_nss = i + 1;
369 				break;
370 			}
371 		}
372 		/* FIXME: consider rx_highest? */
373 	}
374 
375 	ht_rx_nss = max(ht_rx_nss, vht_rx_nss);
376 	sta->sta.rx_nss = max_t(u8, 1, ht_rx_nss);
377 }
378 
379 u32 __ieee80211_vht_handle_opmode(struct ieee80211_sub_if_data *sdata,
380 				  struct sta_info *sta, u8 opmode,
381 				  enum ieee80211_band band, bool nss_only)
382 {
383 	struct ieee80211_local *local = sdata->local;
384 	struct ieee80211_supported_band *sband;
385 	enum ieee80211_sta_rx_bandwidth new_bw;
386 	u32 changed = 0;
387 	u8 nss;
388 
389 	sband = local->hw.wiphy->bands[band];
390 
391 	/* ignore - no support for BF yet */
392 	if (opmode & IEEE80211_OPMODE_NOTIF_RX_NSS_TYPE_BF)
393 		return 0;
394 
395 	nss = opmode & IEEE80211_OPMODE_NOTIF_RX_NSS_MASK;
396 	nss >>= IEEE80211_OPMODE_NOTIF_RX_NSS_SHIFT;
397 	nss += 1;
398 
399 	if (sta->sta.rx_nss != nss) {
400 		sta->sta.rx_nss = nss;
401 		changed |= IEEE80211_RC_NSS_CHANGED;
402 	}
403 
404 	if (nss_only)
405 		return changed;
406 
407 	switch (opmode & IEEE80211_OPMODE_NOTIF_CHANWIDTH_MASK) {
408 	case IEEE80211_OPMODE_NOTIF_CHANWIDTH_20MHZ:
409 		sta->cur_max_bandwidth = IEEE80211_STA_RX_BW_20;
410 		break;
411 	case IEEE80211_OPMODE_NOTIF_CHANWIDTH_40MHZ:
412 		sta->cur_max_bandwidth = IEEE80211_STA_RX_BW_40;
413 		break;
414 	case IEEE80211_OPMODE_NOTIF_CHANWIDTH_80MHZ:
415 		sta->cur_max_bandwidth = IEEE80211_STA_RX_BW_80;
416 		break;
417 	case IEEE80211_OPMODE_NOTIF_CHANWIDTH_160MHZ:
418 		sta->cur_max_bandwidth = IEEE80211_STA_RX_BW_160;
419 		break;
420 	}
421 
422 	new_bw = ieee80211_sta_cur_vht_bw(sta);
423 	if (new_bw != sta->sta.bandwidth) {
424 		sta->sta.bandwidth = new_bw;
425 		changed |= IEEE80211_RC_BW_CHANGED;
426 	}
427 
428 	return changed;
429 }
430 
431 void ieee80211_vht_handle_opmode(struct ieee80211_sub_if_data *sdata,
432 				 struct sta_info *sta, u8 opmode,
433 				 enum ieee80211_band band, bool nss_only)
434 {
435 	struct ieee80211_local *local = sdata->local;
436 	struct ieee80211_supported_band *sband = local->hw.wiphy->bands[band];
437 
438 	u32 changed = __ieee80211_vht_handle_opmode(sdata, sta, opmode,
439 						    band, nss_only);
440 
441 	if (changed > 0)
442 		rate_control_rate_update(local, sband, sta, changed);
443 }
444 
445 void ieee80211_get_vht_mask_from_cap(__le16 vht_cap,
446 				     u16 vht_mask[NL80211_VHT_NSS_MAX])
447 {
448 	int i;
449 	u16 mask, cap = le16_to_cpu(vht_cap);
450 
451 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
452 		mask = (cap >> i * 2) & IEEE80211_VHT_MCS_NOT_SUPPORTED;
453 		switch (mask) {
454 		case IEEE80211_VHT_MCS_SUPPORT_0_7:
455 			vht_mask[i] = 0x00FF;
456 			break;
457 		case IEEE80211_VHT_MCS_SUPPORT_0_8:
458 			vht_mask[i] = 0x01FF;
459 			break;
460 		case IEEE80211_VHT_MCS_SUPPORT_0_9:
461 			vht_mask[i] = 0x03FF;
462 			break;
463 		case IEEE80211_VHT_MCS_NOT_SUPPORTED:
464 		default:
465 			vht_mask[i] = 0;
466 			break;
467 		}
468 	}
469 }
470