xref: /freebsd/sys/net80211/ieee80211_vht.c (revision 2e3507c25e42292b45a5482e116d278f5515d04d)
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
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
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
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
126 ieee80211_vht_attach(struct ieee80211com *ic)
127 {
128 }
129 
130 void
131 ieee80211_vht_detach(struct ieee80211com *ic)
132 {
133 }
134 
135 void
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
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
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
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
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
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 void
242 ieee80211_parse_vhtopmode(struct ieee80211_node *ni, const uint8_t *ie)
243 {
244 	/* vht operation */
245 	ni->ni_vht_chanwidth = ie[2];
246 	ni->ni_vht_chan1 = ie[3];
247 	ni->ni_vht_chan2 = ie[4];
248 	ni->ni_vht_basicmcs = le16dec(ie + 5);
249 
250 #if 0
251 	printf("%s: chan1=%d, chan2=%d, chanwidth=%d, basicmcs=0x%04x\n",
252 	    __func__, ni->ni_vht_chan1, ni->ni_vht_chan2, ni->ni_vht_chanwidth,
253 	    ni->ni_vht_basicmcs);
254 #endif
255 }
256 
257 /*
258  * Parse an 802.11ac VHT capability IE.
259  */
260 void
261 ieee80211_parse_vhtcap(struct ieee80211_node *ni, const uint8_t *ie)
262 {
263 
264 	/* vht capability */
265 	ni->ni_vhtcap = le32dec(ie + 2);
266 
267 	/* suppmcs */
268 	ni->ni_vht_mcsinfo.rx_mcs_map = le16dec(ie + 6);
269 	ni->ni_vht_mcsinfo.rx_highest = le16dec(ie + 8);
270 	ni->ni_vht_mcsinfo.tx_mcs_map = le16dec(ie + 10);
271 	ni->ni_vht_mcsinfo.tx_highest = le16dec(ie + 12);
272 }
273 
274 int
275 ieee80211_vht_updateparams(struct ieee80211_node *ni,
276     const uint8_t *vhtcap_ie,
277     const uint8_t *vhtop_ie)
278 {
279 
280 	//printf("%s: called\n", __func__);
281 
282 	ieee80211_parse_vhtcap(ni, vhtcap_ie);
283 	ieee80211_parse_vhtopmode(ni, vhtop_ie);
284 	return (0);
285 }
286 
287 void
288 ieee80211_setup_vht_rates(struct ieee80211_node *ni,
289     const uint8_t *vhtcap_ie,
290     const uint8_t *vhtop_ie)
291 {
292 
293 	//printf("%s: called\n", __func__);
294 	/* XXX TODO */
295 }
296 
297 void
298 ieee80211_vht_timeout(struct ieee80211vap *vap)
299 {
300 }
301 
302 void
303 ieee80211_vht_node_join(struct ieee80211_node *ni)
304 {
305 
306 	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, ni,
307 	    "%s: called", __func__);
308 }
309 
310 void
311 ieee80211_vht_node_leave(struct ieee80211_node *ni)
312 {
313 
314 	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, ni,
315 	    "%s: called", __func__);
316 }
317 
318 /*
319  * Calculate the VHTCAP IE for a given node.
320  *
321  * This includes calculating the capability intersection based on the
322  * current operating mode and intersection of the TX/RX MCS maps.
323  *
324  * The standard only makes it clear about MCS rate negotiation
325  * and MCS basic rates (which must be a subset of the general
326  * negotiated rates).  It doesn't make it clear that the AP should
327  * figure out the minimum functional overlap with the STA and
328  * support that.
329  *
330  * Note: this is in host order, not in 802.11 endian order.
331  *
332  * TODO: ensure I re-read 9.7.11 Rate Selection for VHT STAs.
333  *
334  * TODO: investigate what we should negotiate for MU-MIMO beamforming
335  *       options.
336  *
337  * opmode is '1' for "vhtcap as if I'm a STA", 0 otherwise.
338  */
339 void
340 ieee80211_vht_get_vhtcap_ie(struct ieee80211_node *ni,
341     struct ieee80211_vht_cap *vhtcap, int opmode)
342 {
343 	struct ieee80211vap *vap = ni->ni_vap;
344 //	struct ieee80211com *ic = vap->iv_ic;
345 	uint32_t val, val1, val2;
346 	uint32_t new_vhtcap;
347 	int i;
348 
349 	/*
350 	 * Capabilities - it depends on whether we are a station
351 	 * or not.
352 	 */
353 	new_vhtcap = 0;
354 
355 	/*
356 	 * Station - use our desired configuration based on
357 	 * local config, local device bits and the already-learnt
358 	 * vhtcap/vhtinfo IE in the node.
359 	 */
360 
361 	/* Limit MPDU size to the smaller of the two */
362 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
363 	    IEEE80211_VHTCAP_MAX_MPDU_MASK);
364 	if (opmode == 1) {
365 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
366 		    IEEE80211_VHTCAP_MAX_MPDU_MASK);
367 	}
368 	val = MIN(val1, val2);
369 	new_vhtcap |= _IEEE80211_SHIFTMASK(val, IEEE80211_VHTCAP_MAX_MPDU_MASK);
370 
371 	/* Limit supp channel config */
372 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
373 	    IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_MASK);
374 	if (opmode == 1) {
375 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
376 		    IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_MASK);
377 	}
378 	if ((val2 == 2) &&
379 	    ((vap->iv_vht_flags & IEEE80211_FVHT_USEVHT80P80) == 0))
380 		val2 = 1;
381 	if ((val2 == 1) &&
382 	    ((vap->iv_vht_flags & IEEE80211_FVHT_USEVHT160) == 0))
383 		val2 = 0;
384 	val = MIN(val1, val2);
385 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
386 	     IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_MASK);
387 
388 	/* RX LDPC */
389 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
390 	    IEEE80211_VHTCAP_RXLDPC);
391 	if (opmode == 1) {
392 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
393 		    IEEE80211_VHTCAP_RXLDPC);
394 	}
395 	val = MIN(val1, val2);
396 	new_vhtcap |= _IEEE80211_SHIFTMASK(val, IEEE80211_VHTCAP_RXLDPC);
397 
398 	/* Short-GI 80 */
399 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
400 	    IEEE80211_VHTCAP_SHORT_GI_80);
401 	if (opmode == 1) {
402 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
403 		    IEEE80211_VHTCAP_SHORT_GI_80);
404 	}
405 	val = MIN(val1, val2);
406 	new_vhtcap |= _IEEE80211_SHIFTMASK(val, IEEE80211_VHTCAP_SHORT_GI_80);
407 
408 	/* Short-GI 160 */
409 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
410 	    IEEE80211_VHTCAP_SHORT_GI_160);
411 	if (opmode == 1) {
412 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
413 		    IEEE80211_VHTCAP_SHORT_GI_160);
414 	}
415 	val = MIN(val1, val2);
416 	new_vhtcap |= _IEEE80211_SHIFTMASK(val, IEEE80211_VHTCAP_SHORT_GI_160);
417 
418 	/*
419 	 * STBC is slightly more complicated.
420 	 *
421 	 * In non-STA mode, we just announce our capabilities and that
422 	 * is that.
423 	 *
424 	 * In STA mode, we should calculate our capabilities based on
425 	 * local capabilities /and/ what the remote says. So:
426 	 *
427 	 * + Only TX STBC if we support it and the remote supports RX STBC;
428 	 * + Only announce RX STBC if we support it and the remote supports
429 	 *   TX STBC;
430 	 * + RX STBC should be the minimum of local and remote RX STBC;
431 	 */
432 
433 	/* TX STBC */
434 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
435 	    IEEE80211_VHTCAP_TXSTBC);
436 	if (opmode == 1) {
437 		/* STA mode - enable it only if node RXSTBC is non-zero */
438 		val2 = !! _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
439 		    IEEE80211_VHTCAP_RXSTBC_MASK);
440 	}
441 	val = MIN(val1, val2);
442 	/* XXX For now, use the 11n config flag */
443 	if ((vap->iv_flags_ht & IEEE80211_FHT_STBC_TX) == 0)
444 		val = 0;
445 	new_vhtcap |= _IEEE80211_SHIFTMASK(val, IEEE80211_VHTCAP_TXSTBC);
446 
447 	/* RX STBC1..4 */
448 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
449 	    IEEE80211_VHTCAP_RXSTBC_MASK);
450 	if (opmode == 1) {
451 		/* STA mode - enable it only if node TXSTBC is non-zero */
452 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
453 		   IEEE80211_VHTCAP_TXSTBC);
454 	}
455 	val = MIN(val1, val2);
456 	/* XXX For now, use the 11n config flag */
457 	if ((vap->iv_flags_ht & IEEE80211_FHT_STBC_RX) == 0)
458 		val = 0;
459 	new_vhtcap |= _IEEE80211_SHIFTMASK(val, IEEE80211_VHTCAP_RXSTBC_MASK);
460 
461 	/*
462 	 * Finally - if RXSTBC is 0, then don't enable TXSTBC.
463 	 * Strictly speaking a device can TXSTBC and not RXSTBC, but
464 	 * it would be silly.
465 	 */
466 	if (val == 0)
467 		new_vhtcap &= ~IEEE80211_VHTCAP_TXSTBC;
468 
469 	/*
470 	 * Some of these fields require other fields to exist.
471 	 * So before using it, the parent field needs to be checked
472 	 * otherwise the overridden value may be wrong.
473 	 *
474 	 * For example, if SU beamformee is set to 0, then BF STS
475 	 * needs to be 0.
476 	 */
477 
478 	/* SU Beamformer capable */
479 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
480 	    IEEE80211_VHTCAP_SU_BEAMFORMER_CAPABLE);
481 	if (opmode == 1) {
482 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
483 		    IEEE80211_VHTCAP_SU_BEAMFORMER_CAPABLE);
484 	}
485 	val = MIN(val1, val2);
486 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
487 	    IEEE80211_VHTCAP_SU_BEAMFORMER_CAPABLE);
488 
489 	/* SU Beamformee capable */
490 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
491 	    IEEE80211_VHTCAP_SU_BEAMFORMEE_CAPABLE);
492 	if (opmode == 1) {
493 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
494 		    IEEE80211_VHTCAP_SU_BEAMFORMEE_CAPABLE);
495 	}
496 	val = MIN(val1, val2);
497 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
498 	    IEEE80211_VHTCAP_SU_BEAMFORMEE_CAPABLE);
499 
500 	/* Beamformee STS capability - only if SU beamformee capable */
501 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
502 	    IEEE80211_VHTCAP_BEAMFORMEE_STS_MASK);
503 	if (opmode == 1) {
504 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
505 		    IEEE80211_VHTCAP_BEAMFORMEE_STS_MASK);
506 	}
507 	val = MIN(val1, val2);
508 	if ((new_vhtcap & IEEE80211_VHTCAP_SU_BEAMFORMEE_CAPABLE) == 0)
509 		val = 0;
510 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
511 	    IEEE80211_VHTCAP_BEAMFORMEE_STS_MASK);
512 
513 	/* Sounding dimensions - only if SU beamformer capable */
514 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
515 	    IEEE80211_VHTCAP_SOUNDING_DIMENSIONS_MASK);
516 	if (opmode == 1)
517 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
518 		    IEEE80211_VHTCAP_SOUNDING_DIMENSIONS_MASK);
519 	val = MIN(val1, val2);
520 	if ((new_vhtcap & IEEE80211_VHTCAP_SU_BEAMFORMER_CAPABLE) == 0)
521 		val = 0;
522 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
523 	    IEEE80211_VHTCAP_SOUNDING_DIMENSIONS_MASK);
524 
525 	/*
526 	 * MU Beamformer capable - only if SU BFF capable, MU BFF capable
527 	 * and STA (not AP)
528 	 */
529 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
530 	    IEEE80211_VHTCAP_MU_BEAMFORMER_CAPABLE);
531 	if (opmode == 1)
532 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
533 		    IEEE80211_VHTCAP_MU_BEAMFORMER_CAPABLE);
534 	val = MIN(val1, val2);
535 	if ((new_vhtcap & IEEE80211_VHTCAP_SU_BEAMFORMER_CAPABLE) == 0)
536 		val = 0;
537 	if (opmode != 1)	/* Only enable for STA mode */
538 		val = 0;
539 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
540 	   IEEE80211_VHTCAP_SU_BEAMFORMER_CAPABLE);
541 
542 	/*
543 	 * MU Beamformee capable - only if SU BFE capable, MU BFE capable
544 	 * and AP (not STA)
545 	 */
546 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
547 	    IEEE80211_VHTCAP_MU_BEAMFORMEE_CAPABLE);
548 	if (opmode == 1)
549 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
550 		    IEEE80211_VHTCAP_MU_BEAMFORMEE_CAPABLE);
551 	val = MIN(val1, val2);
552 	if ((new_vhtcap & IEEE80211_VHTCAP_SU_BEAMFORMEE_CAPABLE) == 0)
553 		val = 0;
554 	if (opmode != 0)	/* Only enable for AP mode */
555 		val = 0;
556 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
557 	   IEEE80211_VHTCAP_SU_BEAMFORMEE_CAPABLE);
558 
559 	/* VHT TXOP PS */
560 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
561 	    IEEE80211_VHTCAP_VHT_TXOP_PS);
562 	if (opmode == 1)
563 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
564 		    IEEE80211_VHTCAP_VHT_TXOP_PS);
565 	val = MIN(val1, val2);
566 	new_vhtcap |= _IEEE80211_SHIFTMASK(val, IEEE80211_VHTCAP_VHT_TXOP_PS);
567 
568 	/* HTC_VHT */
569 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
570 	    IEEE80211_VHTCAP_HTC_VHT);
571 	if (opmode == 1)
572 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
573 		    IEEE80211_VHTCAP_HTC_VHT);
574 	val = MIN(val1, val2);
575 	new_vhtcap |= _IEEE80211_SHIFTMASK(val, IEEE80211_VHTCAP_HTC_VHT);
576 
577 	/* A-MPDU length max */
578 	/* XXX TODO: we need a userland config knob for this */
579 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
580 	    IEEE80211_VHTCAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK);
581 	if (opmode == 1)
582 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
583 		    IEEE80211_VHTCAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK);
584 	val = MIN(val1, val2);
585 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
586 	    IEEE80211_VHTCAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK);
587 
588 	/*
589 	 * Link adaptation is only valid if HTC-VHT capable is 1.
590 	 * Otherwise, always set it to 0.
591 	 */
592 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
593 	    IEEE80211_VHTCAP_VHT_LINK_ADAPTATION_VHT_MASK);
594 	if (opmode == 1)
595 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
596 		    IEEE80211_VHTCAP_VHT_LINK_ADAPTATION_VHT_MASK);
597 	val = MIN(val1, val2);
598 	if ((new_vhtcap & IEEE80211_VHTCAP_HTC_VHT) == 0)
599 		val = 0;
600 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
601 	    IEEE80211_VHTCAP_VHT_LINK_ADAPTATION_VHT_MASK);
602 
603 	/*
604 	 * The following two options are 0 if the pattern may change, 1 if it
605 	 * does not change.  So, downgrade to the higher value.
606 	 */
607 
608 	/* RX antenna pattern */
609 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
610 	    IEEE80211_VHTCAP_RX_ANTENNA_PATTERN);
611 	if (opmode == 1)
612 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
613 		    IEEE80211_VHTCAP_RX_ANTENNA_PATTERN);
614 	val = MAX(val1, val2);
615 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
616 	    IEEE80211_VHTCAP_RX_ANTENNA_PATTERN);
617 
618 	/* TX antenna pattern */
619 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
620 	    IEEE80211_VHTCAP_TX_ANTENNA_PATTERN);
621 	if (opmode == 1)
622 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
623 		    IEEE80211_VHTCAP_TX_ANTENNA_PATTERN);
624 	val = MAX(val1, val2);
625 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
626 	    IEEE80211_VHTCAP_TX_ANTENNA_PATTERN);
627 
628 	/*
629 	 * MCS set - again, we announce what we want to use
630 	 * based on configuration, device capabilities and
631 	 * already-learnt vhtcap/vhtinfo IE information.
632 	 */
633 
634 	/* MCS set - start with whatever the device supports */
635 	vhtcap->supp_mcs.rx_mcs_map = vap->iv_vht_cap.supp_mcs.rx_mcs_map;
636 	vhtcap->supp_mcs.rx_highest = 0;
637 	vhtcap->supp_mcs.tx_mcs_map = vap->iv_vht_cap.supp_mcs.tx_mcs_map;
638 	vhtcap->supp_mcs.tx_highest = 0;
639 
640 	vhtcap->vht_cap_info = new_vhtcap;
641 
642 	/*
643 	 * Now, if we're a STA, mask off whatever the AP doesn't support.
644 	 * Ie, we continue to state we can receive whatever we can do,
645 	 * but we only announce that we will transmit rates that meet
646 	 * the AP requirement.
647 	 *
648 	 * Note: 0 - MCS0..7; 1 - MCS0..8; 2 - MCS0..9; 3 = not supported.
649 	 * We can't just use MIN() because '3' means "no", so special case it.
650 	 */
651 	if (opmode) {
652 		for (i = 0; i < 8; i++) {
653 			val1 = (vhtcap->supp_mcs.tx_mcs_map >> (i*2)) & 0x3;
654 			val2 = (ni->ni_vht_mcsinfo.tx_mcs_map >> (i*2)) & 0x3;
655 			val = MIN(val1, val2);
656 			if (val1 == 3 || val2 == 3)
657 				val = 3;
658 			vhtcap->supp_mcs.tx_mcs_map &= ~(0x3 << (i*2));
659 			vhtcap->supp_mcs.tx_mcs_map |= (val << (i*2));
660 		}
661 	}
662 }
663 
664 /*
665  * Add a VHTCAP field.
666  *
667  * If in station mode, we announce what we would like our
668  * desired configuration to be.
669  *
670  * Else, we announce our capabilities based on our current
671  * configuration.
672  */
673 uint8_t *
674 ieee80211_add_vhtcap(uint8_t *frm, struct ieee80211_node *ni)
675 {
676 	struct ieee80211_vht_cap vhtcap;
677 
678 	ieee80211_vht_get_vhtcap_ie(ni, &vhtcap, 1);
679 
680 	frm[0] = IEEE80211_ELEMID_VHT_CAP;
681 	frm[1] = sizeof(vhtcap);
682 	frm += 2;
683 
684 	/* 32-bit VHT capability */
685 	ADDWORD(frm, vhtcap.vht_cap_info);
686 
687 	/* suppmcs */
688 	ADDSHORT(frm, vhtcap.supp_mcs.rx_mcs_map);
689 	ADDSHORT(frm, vhtcap.supp_mcs.rx_highest);
690 	ADDSHORT(frm, vhtcap.supp_mcs.tx_mcs_map);
691 	ADDSHORT(frm, vhtcap.supp_mcs.tx_highest);
692 
693 	return (frm);
694 }
695 
696 /*
697  * Non-associated probe requests.  Add VHT capabilities based on
698  * the current channel configuration.  No BSS yet.
699  */
700 uint8_t *
701 ieee80211_add_vhtcap_ch(uint8_t *frm, struct ieee80211vap *vap,
702     struct ieee80211_channel *c)
703 {
704 	struct ieee80211_vht_cap *vhtcap;
705 
706 	memset(frm, 0, 2 + sizeof(*vhtcap));
707 	frm[0] = IEEE80211_ELEMID_VHT_CAP;
708 	frm[1] = sizeof(*vhtcap);
709 	frm += 2;
710 
711 	/* 32-bit VHT capability */
712 	ADDWORD(frm, vap->iv_vht_cap.vht_cap_info);
713 
714 	/* supp_mcs */
715 	ADDSHORT(frm, vap->iv_vht_cap.supp_mcs.rx_mcs_map);
716 	ADDSHORT(frm, vap->iv_vht_cap.supp_mcs.rx_highest);
717 	ADDSHORT(frm, vap->iv_vht_cap.supp_mcs.tx_mcs_map);
718 	ADDSHORT(frm, vap->iv_vht_cap.supp_mcs.tx_highest);
719 
720 	return (frm);
721 }
722 
723 static uint8_t
724 ieee80211_vht_get_chwidth_ie(struct ieee80211_channel *c)
725 {
726 
727 	/*
728 	 * XXX TODO: look at the node configuration as
729 	 * well?
730 	 */
731 
732 	if (IEEE80211_IS_CHAN_VHT80P80(c))
733 		return IEEE80211_VHT_CHANWIDTH_80P80MHZ;
734 	if (IEEE80211_IS_CHAN_VHT160(c))
735 		return IEEE80211_VHT_CHANWIDTH_160MHZ;
736 	if (IEEE80211_IS_CHAN_VHT80(c))
737 		return IEEE80211_VHT_CHANWIDTH_80MHZ;
738 	if (IEEE80211_IS_CHAN_VHT40(c))
739 		return IEEE80211_VHT_CHANWIDTH_USE_HT;
740 	if (IEEE80211_IS_CHAN_VHT20(c))
741 		return IEEE80211_VHT_CHANWIDTH_USE_HT;
742 
743 	/* We shouldn't get here */
744 	printf("%s: called on a non-VHT channel (freq=%d, flags=0x%08x\n",
745 	    __func__, (int) c->ic_freq, c->ic_flags);
746 	return IEEE80211_VHT_CHANWIDTH_USE_HT;
747 }
748 
749 /*
750  * Note: this just uses the current channel information;
751  * it doesn't use the node info after parsing.
752  *
753  * XXX TODO: need to make the basic MCS set configurable.
754  * XXX TODO: read 802.11-2013 to determine what to set
755  *           chwidth to when scanning.  I have a feeling
756  *           it isn't involved in scanning and we shouldn't
757  *           be sending it; and I don't yet know what to set
758  *           it to for IBSS or hostap where the peer may be
759  *           a completely different channel width to us.
760  */
761 uint8_t *
762 ieee80211_add_vhtinfo(uint8_t *frm, struct ieee80211_node *ni)
763 {
764 
765 	frm[0] = IEEE80211_ELEMID_VHT_OPMODE;
766 	frm[1] = sizeof(struct ieee80211_vht_operation);
767 	frm += 2;
768 
769 	/* 8-bit chanwidth */
770 	*frm++ = ieee80211_vht_get_chwidth_ie(ni->ni_chan);
771 
772 	/* 8-bit freq1 */
773 	*frm++ = ni->ni_chan->ic_vht_ch_freq1;
774 
775 	/* 8-bit freq2 */
776 	*frm++ = ni->ni_chan->ic_vht_ch_freq2;
777 
778 	/* 16-bit basic MCS set - just MCS0..7 for NSS=1 for now */
779 	ADDSHORT(frm, 0xfffc);
780 
781 	return (frm);
782 }
783 
784 void
785 ieee80211_vht_update_cap(struct ieee80211_node *ni, const uint8_t *vhtcap_ie,
786     const uint8_t *vhtop_ie)
787 {
788 
789 	ieee80211_parse_vhtcap(ni, vhtcap_ie);
790 	ieee80211_parse_vhtopmode(ni, vhtop_ie);
791 }
792 
793 static struct ieee80211_channel *
794 findvhtchan(struct ieee80211com *ic, struct ieee80211_channel *c, int vhtflags)
795 {
796 
797 	return (ieee80211_find_channel(ic, c->ic_freq,
798 	    (c->ic_flags & ~IEEE80211_CHAN_VHT) | vhtflags));
799 }
800 
801 /*
802  * Handle channel promotion to VHT, similar to ieee80211_ht_adjust_channel().
803  */
804 struct ieee80211_channel *
805 ieee80211_vht_adjust_channel(struct ieee80211com *ic,
806     struct ieee80211_channel *chan, int flags)
807 {
808 	struct ieee80211_channel *c;
809 
810 	/* First case - handle channel demotion - if VHT isn't set */
811 	if ((flags & IEEE80211_FVHT_MASK) == 0) {
812 #if 0
813 		printf("%s: demoting channel %d/0x%08x\n", __func__,
814 		    chan->ic_ieee, chan->ic_flags);
815 #endif
816 		c = ieee80211_find_channel(ic, chan->ic_freq,
817 		    chan->ic_flags & ~IEEE80211_CHAN_VHT);
818 		if (c == NULL)
819 			c = chan;
820 #if 0
821 		printf("%s: .. to %d/0x%08x\n", __func__,
822 		    c->ic_ieee, c->ic_flags);
823 #endif
824 		return (c);
825 	}
826 
827 	/*
828 	 * We can upgrade to VHT - attempt to do so
829 	 *
830 	 * Note: we don't clear the HT flags, these are the hints
831 	 * for HT40U/HT40D when selecting VHT40 or larger channels.
832 	 */
833 	c = NULL;
834 	if ((c == NULL) && (flags & IEEE80211_FVHT_USEVHT160))
835 		c = findvhtchan(ic, chan, IEEE80211_CHAN_VHT160);
836 
837 	if ((c == NULL) && (flags & IEEE80211_FVHT_USEVHT80P80))
838 		c = findvhtchan(ic, chan, IEEE80211_CHAN_VHT80P80);
839 
840 	if ((c == NULL) && (flags & IEEE80211_FVHT_USEVHT80))
841 		c = findvhtchan(ic, chan, IEEE80211_CHAN_VHT80);
842 
843 	if ((c == NULL) && (flags & IEEE80211_FVHT_USEVHT40))
844 		c = findvhtchan(ic, chan, IEEE80211_CHAN_VHT40U);
845 	if ((c == NULL) && (flags & IEEE80211_FVHT_USEVHT40))
846 		c = findvhtchan(ic, chan, IEEE80211_CHAN_VHT40D);
847 	/*
848 	 * If we get here, VHT20 is always possible because we checked
849 	 * for IEEE80211_FVHT_VHT above.
850 	 */
851 	if (c == NULL)
852 		c = findvhtchan(ic, chan, IEEE80211_CHAN_VHT20);
853 
854 	if (c != NULL)
855 		chan = c;
856 
857 #if 0
858 	printf("%s: selected %d/0x%08x\n", __func__, c->ic_ieee, c->ic_flags);
859 #endif
860 	return (chan);
861 }
862 
863 /*
864  * Calculate the VHT operation IE for a given node.
865  *
866  * This includes calculating the suitable channel width/parameters
867  * and basic MCS set.
868  *
869  * TODO: ensure I read 9.7.11 Rate Selection for VHT STAs.
870  * TODO: ensure I read 10.39.7 - BSS Basic VHT-MCS and NSS set operation.
871  */
872 void
873 ieee80211_vht_get_vhtinfo_ie(struct ieee80211_node *ni,
874     struct ieee80211_vht_operation *vhtop, int opmode)
875 {
876 	printf("%s: called; TODO!\n", __func__);
877 }
878