xref: /freebsd/sys/net80211/ieee80211_vht.c (revision 0588444700f4facf9d0ce2b6f125a9bd172dde91)
1 /*-
2  * Copyright (c) 2017 Adrian Chadd <adrian@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 /*
27  * IEEE 802.11ac-2013 protocol support.
28  */
29 
30 #include "opt_inet.h"
31 #include "opt_wlan.h"
32 
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/systm.h>
37 #include <sys/endian.h>
38 
39 #include <sys/socket.h>
40 
41 #include <net/if.h>
42 #include <net/if_var.h>
43 #include <net/if_media.h>
44 #include <net/ethernet.h>
45 
46 #include <net80211/ieee80211_var.h>
47 #include <net80211/ieee80211_action.h>
48 #include <net80211/ieee80211_input.h>
49 #include <net80211/ieee80211_vht.h>
50 
51 #define	ADDSHORT(frm, v) do {			\
52 	frm[0] = (v) & 0xff;			\
53 	frm[1] = (v) >> 8;			\
54 	frm += 2;				\
55 } while (0)
56 #define	ADDWORD(frm, v) do {			\
57 	frm[0] = (v) & 0xff;			\
58 	frm[1] = ((v) >> 8) & 0xff;		\
59 	frm[2] = ((v) >> 16) & 0xff;		\
60 	frm[3] = ((v) >> 24) & 0xff;		\
61 	frm += 4;				\
62 } while (0)
63 
64 /*
65  * Immediate TODO:
66  *
67  * + handle WLAN_ACTION_VHT_OPMODE_NOTIF and other VHT action frames
68  * + ensure vhtinfo/vhtcap parameters correctly use the negotiated
69  *   capabilities and ratesets
70  * + group ID management operation
71  */
72 
73 /*
74  * XXX TODO: handle WLAN_ACTION_VHT_OPMODE_NOTIF
75  *
76  * Look at mac80211/vht.c:ieee80211_vht_handle_opmode() for further details.
77  */
78 
79 static int
vht_recv_action_placeholder(struct ieee80211_node * ni,const struct ieee80211_frame * wh,const uint8_t * frm,const uint8_t * efrm)80 vht_recv_action_placeholder(struct ieee80211_node *ni,
81     const struct ieee80211_frame *wh,
82     const uint8_t *frm, const uint8_t *efrm)
83 {
84 
85 #ifdef IEEE80211_DEBUG
86 	ieee80211_note(ni->ni_vap, "%s: called; fc=0x%.2x/0x%.2x",
87 	    __func__, wh->i_fc[0], wh->i_fc[1]);
88 #endif
89 	return (0);
90 }
91 
92 static int
vht_send_action_placeholder(struct ieee80211_node * ni,int category,int action,void * arg0)93 vht_send_action_placeholder(struct ieee80211_node *ni,
94     int category, int action, void *arg0)
95 {
96 
97 #ifdef IEEE80211_DEBUG
98 	ieee80211_note(ni->ni_vap, "%s: called; category=%d, action=%d",
99 	    __func__, category, action);
100 #endif
101 	return (EINVAL);
102 }
103 
104 static void
ieee80211_vht_init(void)105 ieee80211_vht_init(void)
106 {
107 
108 	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_VHT,
109 	    WLAN_ACTION_VHT_COMPRESSED_BF, vht_recv_action_placeholder);
110 	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_VHT,
111 	    WLAN_ACTION_VHT_GROUPID_MGMT, vht_recv_action_placeholder);
112 	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_VHT,
113 	    WLAN_ACTION_VHT_OPMODE_NOTIF, vht_recv_action_placeholder);
114 
115 	ieee80211_send_action_register(IEEE80211_ACTION_CAT_VHT,
116 	    WLAN_ACTION_VHT_COMPRESSED_BF, vht_send_action_placeholder);
117 	ieee80211_send_action_register(IEEE80211_ACTION_CAT_VHT,
118 	    WLAN_ACTION_VHT_GROUPID_MGMT, vht_send_action_placeholder);
119 	ieee80211_send_action_register(IEEE80211_ACTION_CAT_VHT,
120 	    WLAN_ACTION_VHT_OPMODE_NOTIF, vht_send_action_placeholder);
121 }
122 
123 SYSINIT(wlan_vht, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_vht_init, NULL);
124 
125 void
ieee80211_vht_attach(struct ieee80211com * ic)126 ieee80211_vht_attach(struct ieee80211com *ic)
127 {
128 }
129 
130 void
ieee80211_vht_detach(struct ieee80211com * ic)131 ieee80211_vht_detach(struct ieee80211com *ic)
132 {
133 }
134 
135 void
ieee80211_vht_vattach(struct ieee80211vap * vap)136 ieee80211_vht_vattach(struct ieee80211vap *vap)
137 {
138 	struct ieee80211com *ic = vap->iv_ic;
139 
140 	if (! IEEE80211_CONF_VHT(ic))
141 		return;
142 
143 	vap->iv_vht_cap.vht_cap_info = ic->ic_vht_cap.vht_cap_info;
144 	vap->iv_vhtextcaps = ic->ic_vhtextcaps;
145 
146 	/* XXX assume VHT80 support; should really check vhtcaps */
147 	vap->iv_vht_flags =
148 	    IEEE80211_FVHT_VHT
149 	    | IEEE80211_FVHT_USEVHT40
150 	    | IEEE80211_FVHT_USEVHT80;
151 	if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160MHZ(vap->iv_vht_cap.vht_cap_info))
152 		vap->iv_vht_flags |= IEEE80211_FVHT_USEVHT160;
153 	if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160_80P80MHZ(vap->iv_vht_cap.vht_cap_info))
154 		vap->iv_vht_flags |= IEEE80211_FVHT_USEVHT80P80;
155 
156 	memcpy(&vap->iv_vht_cap.supp_mcs, &ic->ic_vht_cap.supp_mcs,
157 	    sizeof(struct ieee80211_vht_mcs_info));
158 }
159 
160 void
ieee80211_vht_vdetach(struct ieee80211vap * vap)161 ieee80211_vht_vdetach(struct ieee80211vap *vap)
162 {
163 }
164 
165 #if 0
166 static void
167 vht_announce(struct ieee80211com *ic, enum ieee80211_phymode mode)
168 {
169 }
170 #endif
171 
172 static int
vht_mcs_to_num(int m)173 vht_mcs_to_num(int m)
174 {
175 
176 	switch (m) {
177 	case IEEE80211_VHT_MCS_SUPPORT_0_7:
178 		return (7);
179 	case IEEE80211_VHT_MCS_SUPPORT_0_8:
180 		return (8);
181 	case IEEE80211_VHT_MCS_SUPPORT_0_9:
182 		return (9);
183 	default:
184 		return (0);
185 	}
186 }
187 
188 void
ieee80211_vht_announce(struct ieee80211com * ic)189 ieee80211_vht_announce(struct ieee80211com *ic)
190 {
191 	int i, tx, rx;
192 
193 	if (! IEEE80211_CONF_VHT(ic))
194 		return;
195 
196 	/* Channel width */
197 	ic_printf(ic, "[VHT] Channel Widths: 20MHz, 40MHz, 80MHz%s%s\n",
198 	    (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160MHZ(ic->ic_vht_cap.vht_cap_info)) ?
199 		", 160MHz" : "",
200 	    (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160_80P80MHZ(ic->ic_vht_cap.vht_cap_info)) ?
201 		 ", 80+80MHz" : "");
202 	/* Features */
203 	ic_printf(ic, "[VHT] Features: %b\n", ic->ic_vht_cap.vht_cap_info,
204 	    IEEE80211_VHTCAP_BITS);
205 
206 	/* For now, just 5GHz VHT.  Worry about 2GHz VHT later */
207 	for (i = 0; i < 8; i++) {
208 		/* Each stream is 2 bits */
209 		tx = (ic->ic_vht_cap.supp_mcs.tx_mcs_map >> (2*i)) & 0x3;
210 		rx = (ic->ic_vht_cap.supp_mcs.rx_mcs_map >> (2*i)) & 0x3;
211 		if (tx == 3 && rx == 3)
212 			continue;
213 		ic_printf(ic, "[VHT] NSS %d: TX MCS 0..%d, RX MCS 0..%d\n",
214 		    i + 1, vht_mcs_to_num(tx), vht_mcs_to_num(rx));
215 	}
216 }
217 
218 void
ieee80211_vht_node_init(struct ieee80211_node * ni)219 ieee80211_vht_node_init(struct ieee80211_node *ni)
220 {
221 
222 	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, ni,
223 	    "%s: called", __func__);
224 	ni->ni_flags |= IEEE80211_NODE_VHT;
225 }
226 
227 void
ieee80211_vht_node_cleanup(struct ieee80211_node * ni)228 ieee80211_vht_node_cleanup(struct ieee80211_node *ni)
229 {
230 
231 	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, ni,
232 	    "%s: called", __func__);
233 	ni->ni_flags &= ~IEEE80211_NODE_VHT;
234 	ni->ni_vhtcap = 0;
235 	bzero(&ni->ni_vht_mcsinfo, sizeof(struct ieee80211_vht_mcs_info));
236 }
237 
238 /*
239  * Parse an 802.11ac VHT operation IE.
240  *
241  * 802.11-2020 9.4.2.158 (VHT Operation element)
242  */
243 void
ieee80211_parse_vhtopmode(struct ieee80211_node * ni,const uint8_t * ie)244 ieee80211_parse_vhtopmode(struct ieee80211_node *ni, const uint8_t *ie)
245 {
246 	/* vht operation */
247 	ni->ni_vht_chanwidth = ie[2];
248 	ni->ni_vht_chan1 = ie[3];
249 	ni->ni_vht_chan2 = ie[4];
250 	ni->ni_vht_basicmcs = le16dec(ie + 5);
251 
252 #if 0
253 	printf("%s: chan1=%d, chan2=%d, chanwidth=%d, basicmcs=0x%04x\n",
254 	    __func__, ni->ni_vht_chan1, ni->ni_vht_chan2, ni->ni_vht_chanwidth,
255 	    ni->ni_vht_basicmcs);
256 #endif
257 }
258 
259 /*
260  * Parse an 802.11ac VHT capability IE.
261  *
262  * 802.11-2020 9.4.2.157 (VHT Capabilities element)
263  */
264 void
ieee80211_parse_vhtcap(struct ieee80211_node * ni,const uint8_t * ie)265 ieee80211_parse_vhtcap(struct ieee80211_node *ni, const uint8_t *ie)
266 {
267 
268 	/* vht capability */
269 	ni->ni_vhtcap = le32dec(ie + 2);
270 
271 	/* suppmcs */
272 	ni->ni_vht_mcsinfo.rx_mcs_map = le16dec(ie + 6);
273 	ni->ni_vht_mcsinfo.rx_highest = le16dec(ie + 8);
274 	ni->ni_vht_mcsinfo.tx_mcs_map = le16dec(ie + 10);
275 	ni->ni_vht_mcsinfo.tx_highest = le16dec(ie + 12);
276 }
277 
278 int
ieee80211_vht_updateparams(struct ieee80211_node * ni,const uint8_t * vhtcap_ie,const uint8_t * vhtop_ie)279 ieee80211_vht_updateparams(struct ieee80211_node *ni,
280     const uint8_t *vhtcap_ie,
281     const uint8_t *vhtop_ie)
282 {
283 
284 	//printf("%s: called\n", __func__);
285 
286 	ieee80211_parse_vhtcap(ni, vhtcap_ie);
287 	ieee80211_parse_vhtopmode(ni, vhtop_ie);
288 	return (0);
289 }
290 
291 /**
292  * @brief calculate the supported MCS rates for this node
293  *
294  * This is called once a node has finished association /
295  * joined a BSS.  The vhtcap / vhtop IEs are from the
296  * peer.  The transmit rate tables need to be combined
297  * together to setup the list of available rates.
298  *
299  * This must be called after the ieee80211_node VHT fields
300  * have been parsed / populated by either ieee80211_vht_updateparams() or
301  * ieee80211_parse_vhtcap(),
302  *
303  * This does not take into account the channel bandwidth,
304  * which (a) may change during operation, and (b) depends
305  * upon packet to packet rate transmission selection.
306  * There are various rate combinations which are not
307  * available in various channel widths and those will
308  * need to be masked off separately.
309  *
310  * (See 802.11-2020 21.5 Parameters for VHT-MCSs for the
311  * tables and supported rates.)
312  *
313  * ALSO: i need to do some filtering based on the HT set too.
314  * (That should be done here too, and in the negotiation, sigh.)
315  * (See 802.11-2016 10.7.12.3 Additional rate selection constraints
316  * for VHT PPDUs)
317  *
318  * @param ni	struct ieee80211_node to configure
319  */
320 void
ieee80211_setup_vht_rates(struct ieee80211_node * ni)321 ieee80211_setup_vht_rates(struct ieee80211_node *ni)
322 {
323 	struct ieee80211vap *vap = ni->ni_vap;
324 	uint32_t val, val1, val2;
325 	uint16_t tx_mcs_map = 0;
326 	int i;
327 
328 	/*
329 	 * Merge our tx_mcs_map with the peer rx_mcs_map to determine what
330 	 * can be actually transmitted to the peer.
331 	 */
332 
333 	for (i = 0; i < 8; i++) {
334 		/*
335 		 * Merge the two together; remember that 0..2 is in order
336 		 * of increasing MCS support, but 3 equals
337 		 * IEEE80211_VHT_MCS_NOT_SUPPORTED so must "win".
338 		 */
339 		val1 = (vap->iv_vht_cap.supp_mcs.tx_mcs_map >> (i*2)) & 0x3;
340 		val2 = (ni->ni_vht_mcsinfo.rx_mcs_map >> (i*2)) & 0x3;
341 		val = MIN(val1, val2);
342 		if (val1 == IEEE80211_VHT_MCS_NOT_SUPPORTED ||
343 		    val2 == IEEE80211_VHT_MCS_NOT_SUPPORTED)
344 			val = IEEE80211_VHT_MCS_NOT_SUPPORTED;
345 		tx_mcs_map |= (val << (i*2));
346 	}
347 
348 	/* Store the TX MCS map somewhere in the node that can be used */
349 	ni->ni_vht_tx_map = tx_mcs_map;
350 }
351 
352 void
ieee80211_vht_timeout(struct ieee80211vap * vap)353 ieee80211_vht_timeout(struct ieee80211vap *vap)
354 {
355 }
356 
357 void
ieee80211_vht_node_join(struct ieee80211_node * ni)358 ieee80211_vht_node_join(struct ieee80211_node *ni)
359 {
360 
361 	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, ni,
362 	    "%s: called", __func__);
363 }
364 
365 void
ieee80211_vht_node_leave(struct ieee80211_node * ni)366 ieee80211_vht_node_leave(struct ieee80211_node *ni)
367 {
368 
369 	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, ni,
370 	    "%s: called", __func__);
371 }
372 
373 /*
374  * Calculate the VHTCAP IE for a given node.
375  *
376  * This includes calculating the capability intersection based on the
377  * current operating mode and intersection of the TX/RX MCS maps.
378  *
379  * The standard only makes it clear about MCS rate negotiation
380  * and MCS basic rates (which must be a subset of the general
381  * negotiated rates).  It doesn't make it clear that the AP should
382  * figure out the minimum functional overlap with the STA and
383  * support that.
384  *
385  * Note: this is in host order, not in 802.11 endian order.
386  *
387  * TODO: ensure I re-read 9.7.11 Rate Selection for VHT STAs.
388  *
389  * TODO: investigate what we should negotiate for MU-MIMO beamforming
390  *       options.
391  *
392  * opmode is '1' for "vhtcap as if I'm a STA", 0 otherwise.
393  */
394 void
ieee80211_vht_get_vhtcap_ie(struct ieee80211_node * ni,struct ieee80211_vht_cap * vhtcap,int opmode)395 ieee80211_vht_get_vhtcap_ie(struct ieee80211_node *ni,
396     struct ieee80211_vht_cap *vhtcap, int opmode)
397 {
398 	struct ieee80211vap *vap = ni->ni_vap;
399 //	struct ieee80211com *ic = vap->iv_ic;
400 	uint32_t val, val1, val2;
401 	uint32_t new_vhtcap;
402 	int i;
403 
404 	/*
405 	 * Capabilities - it depends on whether we are a station
406 	 * or not.
407 	 */
408 	new_vhtcap = 0;
409 
410 	/*
411 	 * Station - use our desired configuration based on
412 	 * local config, local device bits and the already-learnt
413 	 * vhtcap/vhtinfo IE in the node.
414 	 */
415 
416 	/* Limit MPDU size to the smaller of the two */
417 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
418 	    IEEE80211_VHTCAP_MAX_MPDU_MASK);
419 	if (opmode == 1) {
420 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
421 		    IEEE80211_VHTCAP_MAX_MPDU_MASK);
422 	}
423 	val = MIN(val1, val2);
424 	new_vhtcap |= _IEEE80211_SHIFTMASK(val, IEEE80211_VHTCAP_MAX_MPDU_MASK);
425 
426 	/* Limit supp channel config */
427 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
428 	    IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_MASK);
429 	if (opmode == 1) {
430 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
431 		    IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_MASK);
432 	}
433 	if ((val2 == 2) &&
434 	    ((vap->iv_vht_flags & IEEE80211_FVHT_USEVHT80P80) == 0))
435 		val2 = 1;
436 	if ((val2 == 1) &&
437 	    ((vap->iv_vht_flags & IEEE80211_FVHT_USEVHT160) == 0))
438 		val2 = 0;
439 	val = MIN(val1, val2);
440 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
441 	     IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_MASK);
442 
443 	/* RX LDPC */
444 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
445 	    IEEE80211_VHTCAP_RXLDPC);
446 	if (opmode == 1) {
447 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
448 		    IEEE80211_VHTCAP_RXLDPC);
449 	}
450 	val = MIN(val1, val2);
451 	new_vhtcap |= _IEEE80211_SHIFTMASK(val, IEEE80211_VHTCAP_RXLDPC);
452 
453 	/* Short-GI 80 */
454 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
455 	    IEEE80211_VHTCAP_SHORT_GI_80);
456 	if (opmode == 1) {
457 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
458 		    IEEE80211_VHTCAP_SHORT_GI_80);
459 	}
460 	val = MIN(val1, val2);
461 	new_vhtcap |= _IEEE80211_SHIFTMASK(val, IEEE80211_VHTCAP_SHORT_GI_80);
462 
463 	/* Short-GI 160 */
464 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
465 	    IEEE80211_VHTCAP_SHORT_GI_160);
466 	if (opmode == 1) {
467 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
468 		    IEEE80211_VHTCAP_SHORT_GI_160);
469 	}
470 	val = MIN(val1, val2);
471 	new_vhtcap |= _IEEE80211_SHIFTMASK(val, IEEE80211_VHTCAP_SHORT_GI_160);
472 
473 	/*
474 	 * STBC is slightly more complicated.
475 	 *
476 	 * In non-STA mode, we just announce our capabilities and that
477 	 * is that.
478 	 *
479 	 * In STA mode, we should calculate our capabilities based on
480 	 * local capabilities /and/ what the remote says. So:
481 	 *
482 	 * + Only TX STBC if we support it and the remote supports RX STBC;
483 	 * + Only announce RX STBC if we support it and the remote supports
484 	 *   TX STBC;
485 	 * + RX STBC should be the minimum of local and remote RX STBC;
486 	 */
487 
488 	/* TX STBC */
489 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
490 	    IEEE80211_VHTCAP_TXSTBC);
491 	if (opmode == 1) {
492 		/* STA mode - enable it only if node RXSTBC is non-zero */
493 		val2 = !! _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
494 		    IEEE80211_VHTCAP_RXSTBC_MASK);
495 	}
496 	val = MIN(val1, val2);
497 	if ((vap->iv_vht_flags & IEEE80211_FVHT_STBC_TX) == 0)
498 		val = 0;
499 	new_vhtcap |= _IEEE80211_SHIFTMASK(val, IEEE80211_VHTCAP_TXSTBC);
500 
501 	/* RX STBC1..4 */
502 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
503 	    IEEE80211_VHTCAP_RXSTBC_MASK);
504 	if (opmode == 1) {
505 		/* STA mode - enable it only if node TXSTBC is non-zero */
506 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
507 		   IEEE80211_VHTCAP_TXSTBC);
508 	}
509 	val = MIN(val1, val2);
510 	if ((vap->iv_vht_flags & IEEE80211_FVHT_STBC_RX) == 0)
511 		val = 0;
512 	new_vhtcap |= _IEEE80211_SHIFTMASK(val, IEEE80211_VHTCAP_RXSTBC_MASK);
513 
514 	/*
515 	 * Finally - if RXSTBC is 0, then don't enable TXSTBC.
516 	 * Strictly speaking a device can TXSTBC and not RXSTBC, but
517 	 * it would be silly.
518 	 */
519 	if (val == 0)
520 		new_vhtcap &= ~IEEE80211_VHTCAP_TXSTBC;
521 
522 	/*
523 	 * Some of these fields require other fields to exist.
524 	 * So before using it, the parent field needs to be checked
525 	 * otherwise the overridden value may be wrong.
526 	 *
527 	 * For example, if SU beamformee is set to 0, then BF STS
528 	 * needs to be 0.
529 	 */
530 
531 	/* SU Beamformer capable */
532 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
533 	    IEEE80211_VHTCAP_SU_BEAMFORMER_CAPABLE);
534 	if (opmode == 1) {
535 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
536 		    IEEE80211_VHTCAP_SU_BEAMFORMER_CAPABLE);
537 	}
538 	val = MIN(val1, val2);
539 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
540 	    IEEE80211_VHTCAP_SU_BEAMFORMER_CAPABLE);
541 
542 	/* SU Beamformee capable */
543 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
544 	    IEEE80211_VHTCAP_SU_BEAMFORMEE_CAPABLE);
545 	if (opmode == 1) {
546 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
547 		    IEEE80211_VHTCAP_SU_BEAMFORMEE_CAPABLE);
548 	}
549 	val = MIN(val1, val2);
550 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
551 	    IEEE80211_VHTCAP_SU_BEAMFORMEE_CAPABLE);
552 
553 	/* Beamformee STS capability - only if SU beamformee capable */
554 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
555 	    IEEE80211_VHTCAP_BEAMFORMEE_STS_MASK);
556 	if (opmode == 1) {
557 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
558 		    IEEE80211_VHTCAP_BEAMFORMEE_STS_MASK);
559 	}
560 	val = MIN(val1, val2);
561 	if ((new_vhtcap & IEEE80211_VHTCAP_SU_BEAMFORMEE_CAPABLE) == 0)
562 		val = 0;
563 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
564 	    IEEE80211_VHTCAP_BEAMFORMEE_STS_MASK);
565 
566 	/* Sounding dimensions - only if SU beamformer capable */
567 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
568 	    IEEE80211_VHTCAP_SOUNDING_DIMENSIONS_MASK);
569 	if (opmode == 1)
570 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
571 		    IEEE80211_VHTCAP_SOUNDING_DIMENSIONS_MASK);
572 	val = MIN(val1, val2);
573 	if ((new_vhtcap & IEEE80211_VHTCAP_SU_BEAMFORMER_CAPABLE) == 0)
574 		val = 0;
575 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
576 	    IEEE80211_VHTCAP_SOUNDING_DIMENSIONS_MASK);
577 
578 	/*
579 	 * MU Beamformer capable - only if SU BFF capable, MU BFF capable
580 	 * and STA (not AP)
581 	 */
582 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
583 	    IEEE80211_VHTCAP_MU_BEAMFORMER_CAPABLE);
584 	if (opmode == 1)
585 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
586 		    IEEE80211_VHTCAP_MU_BEAMFORMER_CAPABLE);
587 	val = MIN(val1, val2);
588 	if ((new_vhtcap & IEEE80211_VHTCAP_SU_BEAMFORMER_CAPABLE) == 0)
589 		val = 0;
590 	if (opmode != 1)	/* Only enable for STA mode */
591 		val = 0;
592 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
593 	   IEEE80211_VHTCAP_SU_BEAMFORMER_CAPABLE);
594 
595 	/*
596 	 * MU Beamformee capable - only if SU BFE capable, MU BFE capable
597 	 * and AP (not STA)
598 	 */
599 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
600 	    IEEE80211_VHTCAP_MU_BEAMFORMEE_CAPABLE);
601 	if (opmode == 1)
602 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
603 		    IEEE80211_VHTCAP_MU_BEAMFORMEE_CAPABLE);
604 	val = MIN(val1, val2);
605 	if ((new_vhtcap & IEEE80211_VHTCAP_SU_BEAMFORMEE_CAPABLE) == 0)
606 		val = 0;
607 	if (opmode != 0)	/* Only enable for AP mode */
608 		val = 0;
609 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
610 	   IEEE80211_VHTCAP_SU_BEAMFORMEE_CAPABLE);
611 
612 	/* VHT TXOP PS */
613 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
614 	    IEEE80211_VHTCAP_VHT_TXOP_PS);
615 	if (opmode == 1)
616 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
617 		    IEEE80211_VHTCAP_VHT_TXOP_PS);
618 	val = MIN(val1, val2);
619 	new_vhtcap |= _IEEE80211_SHIFTMASK(val, IEEE80211_VHTCAP_VHT_TXOP_PS);
620 
621 	/* HTC_VHT */
622 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
623 	    IEEE80211_VHTCAP_HTC_VHT);
624 	if (opmode == 1)
625 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
626 		    IEEE80211_VHTCAP_HTC_VHT);
627 	val = MIN(val1, val2);
628 	new_vhtcap |= _IEEE80211_SHIFTMASK(val, IEEE80211_VHTCAP_HTC_VHT);
629 
630 	/* A-MPDU length max */
631 	/* XXX TODO: we need a userland config knob for this */
632 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
633 	    IEEE80211_VHTCAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK);
634 	if (opmode == 1)
635 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
636 		    IEEE80211_VHTCAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK);
637 	val = MIN(val1, val2);
638 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
639 	    IEEE80211_VHTCAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK);
640 
641 	/*
642 	 * Link adaptation is only valid if HTC-VHT capable is 1.
643 	 * Otherwise, always set it to 0.
644 	 */
645 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
646 	    IEEE80211_VHTCAP_VHT_LINK_ADAPTATION_VHT_MASK);
647 	if (opmode == 1)
648 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
649 		    IEEE80211_VHTCAP_VHT_LINK_ADAPTATION_VHT_MASK);
650 	val = MIN(val1, val2);
651 	if ((new_vhtcap & IEEE80211_VHTCAP_HTC_VHT) == 0)
652 		val = 0;
653 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
654 	    IEEE80211_VHTCAP_VHT_LINK_ADAPTATION_VHT_MASK);
655 
656 	/*
657 	 * The following two options are 0 if the pattern may change, 1 if it
658 	 * does not change.  So, downgrade to the higher value.
659 	 */
660 
661 	/* RX antenna pattern */
662 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
663 	    IEEE80211_VHTCAP_RX_ANTENNA_PATTERN);
664 	if (opmode == 1)
665 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
666 		    IEEE80211_VHTCAP_RX_ANTENNA_PATTERN);
667 	val = MAX(val1, val2);
668 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
669 	    IEEE80211_VHTCAP_RX_ANTENNA_PATTERN);
670 
671 	/* TX antenna pattern */
672 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
673 	    IEEE80211_VHTCAP_TX_ANTENNA_PATTERN);
674 	if (opmode == 1)
675 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
676 		    IEEE80211_VHTCAP_TX_ANTENNA_PATTERN);
677 	val = MAX(val1, val2);
678 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
679 	    IEEE80211_VHTCAP_TX_ANTENNA_PATTERN);
680 
681 	/*
682 	 * MCS set - again, we announce what we want to use
683 	 * based on configuration, device capabilities and
684 	 * already-learnt vhtcap/vhtinfo IE information.
685 	 */
686 
687 	/* MCS set - start with whatever the device supports */
688 	vhtcap->supp_mcs.rx_mcs_map = vap->iv_vht_cap.supp_mcs.rx_mcs_map;
689 	vhtcap->supp_mcs.rx_highest = 0;
690 	vhtcap->supp_mcs.tx_mcs_map = vap->iv_vht_cap.supp_mcs.tx_mcs_map;
691 	vhtcap->supp_mcs.tx_highest = 0;
692 
693 	vhtcap->vht_cap_info = new_vhtcap;
694 
695 	/*
696 	 * Now, if we're a STA, mask off whatever the AP doesn't support.
697 	 * Ie, we continue to state we can receive whatever we can do,
698 	 * but we only announce that we will transmit rates that meet
699 	 * the AP requirement.
700 	 *
701 	 * Note: 0 - MCS0..7; 1 - MCS0..8; 2 - MCS0..9; 3 = not supported.
702 	 * We can't just use MIN() because '3' means "no", so special case it.
703 	 */
704 	if (opmode) {
705 		for (i = 0; i < 8; i++) {
706 			val1 = (vhtcap->supp_mcs.tx_mcs_map >> (i*2)) & 0x3;
707 			val2 = (ni->ni_vht_mcsinfo.tx_mcs_map >> (i*2)) & 0x3;
708 			val = MIN(val1, val2);
709 			if (val1 == 3 || val2 == 3)
710 				val = 3;
711 			vhtcap->supp_mcs.tx_mcs_map &= ~(0x3 << (i*2));
712 			vhtcap->supp_mcs.tx_mcs_map |= (val << (i*2));
713 		}
714 	}
715 }
716 
717 /*
718  * Add a VHTCAP field.
719  *
720  * If in station mode, we announce what we would like our
721  * desired configuration to be.
722  *
723  * Else, we announce our capabilities based on our current
724  * configuration.
725  */
726 uint8_t *
ieee80211_add_vhtcap(uint8_t * frm,struct ieee80211_node * ni)727 ieee80211_add_vhtcap(uint8_t *frm, struct ieee80211_node *ni)
728 {
729 	struct ieee80211_vht_cap vhtcap;
730 
731 	ieee80211_vht_get_vhtcap_ie(ni, &vhtcap, 1);
732 
733 	frm[0] = IEEE80211_ELEMID_VHT_CAP;
734 	frm[1] = sizeof(vhtcap);
735 	frm += 2;
736 
737 	/* 32-bit VHT capability */
738 	ADDWORD(frm, vhtcap.vht_cap_info);
739 
740 	/* suppmcs */
741 	ADDSHORT(frm, vhtcap.supp_mcs.rx_mcs_map);
742 	ADDSHORT(frm, vhtcap.supp_mcs.rx_highest);
743 	ADDSHORT(frm, vhtcap.supp_mcs.tx_mcs_map);
744 	ADDSHORT(frm, vhtcap.supp_mcs.tx_highest);
745 
746 	return (frm);
747 }
748 
749 /*
750  * Non-associated probe requests.  Add VHT capabilities based on
751  * the current channel configuration.  No BSS yet.
752  */
753 uint8_t *
ieee80211_add_vhtcap_ch(uint8_t * frm,struct ieee80211vap * vap,struct ieee80211_channel * c)754 ieee80211_add_vhtcap_ch(uint8_t *frm, struct ieee80211vap *vap,
755     struct ieee80211_channel *c)
756 {
757 	struct ieee80211_vht_cap *vhtcap;
758 
759 	memset(frm, 0, 2 + sizeof(*vhtcap));
760 	frm[0] = IEEE80211_ELEMID_VHT_CAP;
761 	frm[1] = sizeof(*vhtcap);
762 	frm += 2;
763 
764 	/* 32-bit VHT capability */
765 	ADDWORD(frm, vap->iv_vht_cap.vht_cap_info);
766 
767 	/* supp_mcs */
768 	ADDSHORT(frm, vap->iv_vht_cap.supp_mcs.rx_mcs_map);
769 	ADDSHORT(frm, vap->iv_vht_cap.supp_mcs.rx_highest);
770 	ADDSHORT(frm, vap->iv_vht_cap.supp_mcs.tx_mcs_map);
771 	ADDSHORT(frm, vap->iv_vht_cap.supp_mcs.tx_highest);
772 
773 	return (frm);
774 }
775 
776 static uint8_t
ieee80211_vht_get_chwidth_ie(struct ieee80211_channel * c)777 ieee80211_vht_get_chwidth_ie(struct ieee80211_channel *c)
778 {
779 
780 	/*
781 	 * XXX TODO: look at the node configuration as
782 	 * well?
783 	 */
784 
785 	if (IEEE80211_IS_CHAN_VHT80P80(c))
786 		return IEEE80211_VHT_CHANWIDTH_80P80MHZ;
787 	if (IEEE80211_IS_CHAN_VHT160(c))
788 		return IEEE80211_VHT_CHANWIDTH_160MHZ;
789 	if (IEEE80211_IS_CHAN_VHT80(c))
790 		return IEEE80211_VHT_CHANWIDTH_80MHZ;
791 	if (IEEE80211_IS_CHAN_VHT40(c))
792 		return IEEE80211_VHT_CHANWIDTH_USE_HT;
793 	if (IEEE80211_IS_CHAN_VHT20(c))
794 		return IEEE80211_VHT_CHANWIDTH_USE_HT;
795 
796 	/* We shouldn't get here */
797 	printf("%s: called on a non-VHT channel (freq=%d, flags=0x%08x\n",
798 	    __func__, (int) c->ic_freq, c->ic_flags);
799 	return IEEE80211_VHT_CHANWIDTH_USE_HT;
800 }
801 
802 /*
803  * Note: this just uses the current channel information;
804  * it doesn't use the node info after parsing.
805  *
806  * XXX TODO: need to make the basic MCS set configurable.
807  * XXX TODO: read 802.11-2013 to determine what to set
808  *           chwidth to when scanning.  I have a feeling
809  *           it isn't involved in scanning and we shouldn't
810  *           be sending it; and I don't yet know what to set
811  *           it to for IBSS or hostap where the peer may be
812  *           a completely different channel width to us.
813  */
814 uint8_t *
ieee80211_add_vhtinfo(uint8_t * frm,struct ieee80211_node * ni)815 ieee80211_add_vhtinfo(uint8_t *frm, struct ieee80211_node *ni)
816 {
817 
818 	frm[0] = IEEE80211_ELEMID_VHT_OPMODE;
819 	frm[1] = sizeof(struct ieee80211_vht_operation);
820 	frm += 2;
821 
822 	/* 8-bit chanwidth */
823 	*frm++ = ieee80211_vht_get_chwidth_ie(ni->ni_chan);
824 
825 	/* 8-bit freq1 */
826 	*frm++ = ni->ni_chan->ic_vht_ch_freq1;
827 
828 	/* 8-bit freq2 */
829 	*frm++ = ni->ni_chan->ic_vht_ch_freq2;
830 
831 	/* 16-bit basic MCS set - just MCS0..7 for NSS=1 for now */
832 	ADDSHORT(frm, 0xfffc);
833 
834 	return (frm);
835 }
836 
837 void
ieee80211_vht_update_cap(struct ieee80211_node * ni,const uint8_t * vhtcap_ie,const uint8_t * vhtop_ie)838 ieee80211_vht_update_cap(struct ieee80211_node *ni, const uint8_t *vhtcap_ie,
839     const uint8_t *vhtop_ie)
840 {
841 
842 	ieee80211_parse_vhtcap(ni, vhtcap_ie);
843 	ieee80211_parse_vhtopmode(ni, vhtop_ie);
844 }
845 
846 static struct ieee80211_channel *
findvhtchan(struct ieee80211com * ic,struct ieee80211_channel * c,int vhtflags)847 findvhtchan(struct ieee80211com *ic, struct ieee80211_channel *c, int vhtflags)
848 {
849 
850 	return (ieee80211_find_channel(ic, c->ic_freq,
851 	    (c->ic_flags & ~IEEE80211_CHAN_VHT) | vhtflags));
852 }
853 
854 /*
855  * Handle channel promotion to VHT, similar to ieee80211_ht_adjust_channel().
856  */
857 struct ieee80211_channel *
ieee80211_vht_adjust_channel(struct ieee80211com * ic,struct ieee80211_channel * chan,int flags)858 ieee80211_vht_adjust_channel(struct ieee80211com *ic,
859     struct ieee80211_channel *chan, int flags)
860 {
861 	struct ieee80211_channel *c;
862 
863 	/* First case - handle channel demotion - if VHT isn't set */
864 	if ((flags & IEEE80211_FVHT_MASK) == 0) {
865 #if 0
866 		printf("%s: demoting channel %d/0x%08x\n", __func__,
867 		    chan->ic_ieee, chan->ic_flags);
868 #endif
869 		c = ieee80211_find_channel(ic, chan->ic_freq,
870 		    chan->ic_flags & ~IEEE80211_CHAN_VHT);
871 		if (c == NULL)
872 			c = chan;
873 #if 0
874 		printf("%s: .. to %d/0x%08x\n", __func__,
875 		    c->ic_ieee, c->ic_flags);
876 #endif
877 		return (c);
878 	}
879 
880 	/*
881 	 * We can upgrade to VHT - attempt to do so
882 	 *
883 	 * Note: we don't clear the HT flags, these are the hints
884 	 * for HT40U/HT40D when selecting VHT40 or larger channels.
885 	 */
886 	c = NULL;
887 	if ((c == NULL) && (flags & IEEE80211_FVHT_USEVHT160))
888 		c = findvhtchan(ic, chan, IEEE80211_CHAN_VHT160);
889 
890 	if ((c == NULL) && (flags & IEEE80211_FVHT_USEVHT80P80))
891 		c = findvhtchan(ic, chan, IEEE80211_CHAN_VHT80P80);
892 
893 	if ((c == NULL) && (flags & IEEE80211_FVHT_USEVHT80))
894 		c = findvhtchan(ic, chan, IEEE80211_CHAN_VHT80);
895 
896 	if ((c == NULL) && (flags & IEEE80211_FVHT_USEVHT40))
897 		c = findvhtchan(ic, chan, IEEE80211_CHAN_VHT40U);
898 	if ((c == NULL) && (flags & IEEE80211_FVHT_USEVHT40))
899 		c = findvhtchan(ic, chan, IEEE80211_CHAN_VHT40D);
900 	/*
901 	 * If we get here, VHT20 is always possible because we checked
902 	 * for IEEE80211_FVHT_VHT above.
903 	 */
904 	if (c == NULL)
905 		c = findvhtchan(ic, chan, IEEE80211_CHAN_VHT20);
906 
907 	if (c != NULL)
908 		chan = c;
909 
910 #if 0
911 	printf("%s: selected %d/0x%08x\n", __func__, c->ic_ieee, c->ic_flags);
912 #endif
913 	return (chan);
914 }
915 
916 /*
917  * Calculate the VHT operation IE for a given node.
918  *
919  * This includes calculating the suitable channel width/parameters
920  * and basic MCS set.
921  *
922  * TODO: ensure I read 9.7.11 Rate Selection for VHT STAs.
923  * TODO: ensure I read 10.39.7 - BSS Basic VHT-MCS and NSS set operation.
924  */
925 void
ieee80211_vht_get_vhtinfo_ie(struct ieee80211_node * ni,struct ieee80211_vht_operation * vhtop,int opmode)926 ieee80211_vht_get_vhtinfo_ie(struct ieee80211_node *ni,
927     struct ieee80211_vht_operation *vhtop, int opmode)
928 {
929 	printf("%s: called; TODO!\n", __func__);
930 }
931 
932 /*
933  * Return true if VHT rates can be used for the given node.
934  */
935 bool
ieee80211_vht_check_tx_vht(const struct ieee80211_node * ni)936 ieee80211_vht_check_tx_vht(const struct ieee80211_node *ni)
937 {
938 	const struct ieee80211vap *vap;
939 	const struct ieee80211_channel *bss_chan;
940 
941 	if (ni == NULL || ni->ni_chan == IEEE80211_CHAN_ANYC ||
942 	    ni->ni_vap == NULL || ni->ni_vap->iv_bss == NULL)
943 		return (false);
944 
945 	vap = ni->ni_vap;
946 	bss_chan = vap->iv_bss->ni_chan;
947 
948 	if (bss_chan == IEEE80211_CHAN_ANYC)
949 		return (false);
950 
951 	return (IEEE80211_IS_CHAN_VHT(ni->ni_chan));
952 }
953 
954 /*
955  * Return true if VHT40 rates can be transmitted to the given node.
956  *
957  * This verifies that the BSS is VHT40 capable and the current
958  * node channel width is 40MHz.
959  */
960 static bool
ieee80211_vht_check_tx_vht40(const struct ieee80211_node * ni)961 ieee80211_vht_check_tx_vht40(const struct ieee80211_node *ni)
962 {
963 	struct ieee80211vap *vap;
964 	struct ieee80211_channel *bss_chan;
965 
966 	if (!ieee80211_vht_check_tx_vht(ni))
967 		return (false);
968 
969 	vap = ni->ni_vap;
970 	bss_chan = vap->iv_bss->ni_chan;
971 
972 	return (IEEE80211_IS_CHAN_VHT40(bss_chan) &&
973 	    IEEE80211_IS_CHAN_VHT40(ni->ni_chan) &&
974 	    (ni->ni_chw == IEEE80211_STA_RX_BW_40));
975 }
976 
977 /*
978  * Return true if VHT80 rates can be transmitted to the given node.
979  *
980  * This verifies that the BSS is VHT80 capable and the current
981  * node channel width is 80MHz.
982  */
983 static bool
ieee80211_vht_check_tx_vht80(const struct ieee80211_node * ni)984 ieee80211_vht_check_tx_vht80(const struct ieee80211_node *ni)
985 {
986 	struct ieee80211vap *vap;
987 	struct ieee80211_channel *bss_chan;
988 
989 	if (!ieee80211_vht_check_tx_vht(ni))
990 		return (false);
991 
992 	vap = ni->ni_vap;
993 	bss_chan = vap->iv_bss->ni_chan;
994 
995 	return (IEEE80211_IS_CHAN_VHT80(bss_chan) &&
996 	    IEEE80211_IS_CHAN_VHT80(ni->ni_chan) &&
997 	    (ni->ni_chw == IEEE80211_STA_RX_BW_80));
998 }
999 
1000 /*
1001  * Return true if VHT 160 rates can be transmitted to the given node.
1002  *
1003  * This verifies that the BSS is VHT80+80 or VHT160 capable and the current
1004  * node channel width is 80+80MHz or 160MHz.
1005  */
1006 static bool
ieee80211_vht_check_tx_vht160(const struct ieee80211_node * ni)1007 ieee80211_vht_check_tx_vht160(const struct ieee80211_node *ni)
1008 {
1009 	struct ieee80211vap *vap;
1010 	struct ieee80211_channel *bss_chan;
1011 
1012 	if (!ieee80211_vht_check_tx_vht(ni))
1013 		return (false);
1014 
1015 	vap = ni->ni_vap;
1016 	bss_chan = vap->iv_bss->ni_chan;
1017 
1018 	if (ni->ni_chw != IEEE80211_STA_RX_BW_160)
1019 		return (false);
1020 
1021 	if (IEEE80211_IS_CHAN_VHT160(bss_chan) &&
1022 	    IEEE80211_IS_CHAN_VHT160(ni->ni_chan))
1023 		return (true);
1024 
1025 	if (IEEE80211_IS_CHAN_VHT80P80(bss_chan) &&
1026 	    IEEE80211_IS_CHAN_VHT80P80(ni->ni_chan))
1027 		return (true);
1028 
1029 	return (false);
1030 }
1031 
1032 /**
1033  * @brief Check if the given transmit bandwidth is available to the given node
1034  *
1035  * This checks that the node and BSS both allow the given bandwidth,
1036  * and that the current node bandwidth (which can dynamically change)
1037  * also allows said bandwidth.
1038  *
1039  * This relies on the channels having the flags for the narrower
1040  * channels as well - eg a VHT160 channel will have the CHAN_VHT80,
1041  * CHAN_VHT40, CHAN_VHT flags also set.
1042  *
1043  * @param ni		the ieee80211_node to check
1044  * @param bw		the required bandwidth to check
1045  *
1046  * @returns true if it is allowed, false otherwise
1047  */
1048 bool
ieee80211_vht_check_tx_bw(const struct ieee80211_node * ni,enum ieee80211_sta_rx_bw bw)1049 ieee80211_vht_check_tx_bw(const struct ieee80211_node *ni,
1050     enum ieee80211_sta_rx_bw bw)
1051 {
1052 
1053 	switch (bw) {
1054 	case IEEE80211_STA_RX_BW_20:
1055 		return (ieee80211_vht_check_tx_vht(ni));
1056 	case IEEE80211_STA_RX_BW_40:
1057 		return (ieee80211_vht_check_tx_vht40(ni));
1058 	case IEEE80211_STA_RX_BW_80:
1059 		return (ieee80211_vht_check_tx_vht80(ni));
1060 	case IEEE80211_STA_RX_BW_160:
1061 		return (ieee80211_vht_check_tx_vht160(ni));
1062 	case IEEE80211_STA_RX_BW_320:
1063 		return (false);
1064 	default:
1065 		return (false);
1066 	}
1067 }
1068 
1069 /**
1070  * @brief Check if the given VHT bw/nss/mcs combination is valid
1071  *        for the give node.
1072  *
1073  * This checks whether the given VHT bw/nss/mcs is valid based on
1074  * the negotiated rate mask in the node.
1075  *
1076  * @param ni	struct ieee80211_node node to check
1077  * @param bw	channel bandwidth to check
1078  * @param nss	NSS
1079  * @param mcs	MCS
1080  * @returns True if this combination is available, false otherwise.
1081  */
1082 bool
ieee80211_vht_node_check_tx_valid_mcs(const struct ieee80211_node * ni,enum ieee80211_sta_rx_bw bw,uint8_t nss,uint8_t mcs)1083 ieee80211_vht_node_check_tx_valid_mcs(const struct ieee80211_node *ni,
1084     enum ieee80211_sta_rx_bw bw, uint8_t nss, uint8_t mcs)
1085 {
1086 	uint8_t mc;
1087 
1088 	/* Validate arguments */
1089 	if (nss < 1 || nss > 8)
1090 		return (false);
1091 	if (mcs > 9)
1092 		return (false);
1093 
1094 	/* Check our choice of rate is actually valid */
1095 	if (!ieee80211_phy_vht_validate_mcs(bw, nss, mcs))
1096 		return (false);
1097 
1098 	/*
1099 	 * Next, check if the MCS rate is available for the
1100 	 * given NSS.
1101 	 */
1102 	mc = ni->ni_vht_tx_map >> (2*(nss-1)) & 0x3;
1103 	switch (mc) {
1104 	case IEEE80211_VHT_MCS_NOT_SUPPORTED:
1105 		/* Not supported at this NSS */
1106 		return (false);
1107 	case IEEE80211_VHT_MCS_SUPPORT_0_9:
1108 		return (mcs <= 9);
1109 	case IEEE80211_VHT_MCS_SUPPORT_0_8:
1110 		return (mcs <= 8);
1111 	case IEEE80211_VHT_MCS_SUPPORT_0_7:
1112 		return (mcs <= 7);
1113 	default:
1114 		return (false);
1115 	}
1116 }
1117