xref: /freebsd/sys/net80211/ieee80211_vht.c (revision eaeb601bd6a77b1f1c0889df45693d8c602e4863)
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 #include <sys/cdefs.h>
27 #ifdef __FreeBSD__
28 __FBSDID("$FreeBSD$");
29 #endif
30 
31 /*
32  * IEEE 802.11ac-2013 protocol support.
33  */
34 
35 #include "opt_inet.h"
36 #include "opt_wlan.h"
37 
38 #include <sys/param.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/systm.h>
42 #include <sys/endian.h>
43 
44 #include <sys/socket.h>
45 
46 #include <net/if.h>
47 #include <net/if_var.h>
48 #include <net/if_media.h>
49 #include <net/ethernet.h>
50 
51 #include <net80211/ieee80211_var.h>
52 #include <net80211/ieee80211_action.h>
53 #include <net80211/ieee80211_input.h>
54 #include <net80211/ieee80211_vht.h>
55 
56 #define	ADDSHORT(frm, v) do {			\
57 	frm[0] = (v) & 0xff;			\
58 	frm[1] = (v) >> 8;			\
59 	frm += 2;				\
60 } while (0)
61 #define	ADDWORD(frm, v) do {			\
62 	frm[0] = (v) & 0xff;			\
63 	frm[1] = ((v) >> 8) & 0xff;		\
64 	frm[2] = ((v) >> 16) & 0xff;		\
65 	frm[3] = ((v) >> 24) & 0xff;		\
66 	frm += 4;				\
67 } while (0)
68 
69 /*
70  * Immediate TODO:
71  *
72  * + handle WLAN_ACTION_VHT_OPMODE_NOTIF and other VHT action frames
73  * + ensure vhtinfo/vhtcap parameters correctly use the negotiated
74  *   capabilities and ratesets
75  * + group ID management operation
76  */
77 
78 /*
79  * XXX TODO: handle WLAN_ACTION_VHT_OPMODE_NOTIF
80  *
81  * Look at mac80211/vht.c:ieee80211_vht_handle_opmode() for further details.
82  */
83 
84 static int
85 vht_recv_action_placeholder(struct ieee80211_node *ni,
86     const struct ieee80211_frame *wh,
87     const uint8_t *frm, const uint8_t *efrm)
88 {
89 
90 #ifdef IEEE80211_DEBUG
91 	ieee80211_note(ni->ni_vap, "%s: called; fc=0x%.2x/0x%.2x",
92 	    __func__, wh->i_fc[0], wh->i_fc[1]);
93 #endif
94 	return (0);
95 }
96 
97 static int
98 vht_send_action_placeholder(struct ieee80211_node *ni,
99     int category, int action, void *arg0)
100 {
101 
102 #ifdef IEEE80211_DEBUG
103 	ieee80211_note(ni->ni_vap, "%s: called; category=%d, action=%d",
104 	    __func__, category, action);
105 #endif
106 	return (EINVAL);
107 }
108 
109 static void
110 ieee80211_vht_init(void)
111 {
112 
113 	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_VHT,
114 	    WLAN_ACTION_VHT_COMPRESSED_BF, vht_recv_action_placeholder);
115 	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_VHT,
116 	    WLAN_ACTION_VHT_GROUPID_MGMT, vht_recv_action_placeholder);
117 	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_VHT,
118 	    WLAN_ACTION_VHT_OPMODE_NOTIF, vht_recv_action_placeholder);
119 
120 	ieee80211_send_action_register(IEEE80211_ACTION_CAT_VHT,
121 	    WLAN_ACTION_VHT_COMPRESSED_BF, vht_send_action_placeholder);
122 	ieee80211_send_action_register(IEEE80211_ACTION_CAT_VHT,
123 	    WLAN_ACTION_VHT_GROUPID_MGMT, vht_send_action_placeholder);
124 	ieee80211_send_action_register(IEEE80211_ACTION_CAT_VHT,
125 	    WLAN_ACTION_VHT_OPMODE_NOTIF, vht_send_action_placeholder);
126 }
127 
128 SYSINIT(wlan_vht, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_vht_init, NULL);
129 
130 void
131 ieee80211_vht_attach(struct ieee80211com *ic)
132 {
133 }
134 
135 void
136 ieee80211_vht_detach(struct ieee80211com *ic)
137 {
138 }
139 
140 void
141 ieee80211_vht_vattach(struct ieee80211vap *vap)
142 {
143 	struct ieee80211com *ic = vap->iv_ic;
144 
145 	if (! IEEE80211_CONF_VHT(ic))
146 		return;
147 
148 	vap->iv_vhtcaps = ic->ic_vhtcaps;
149 	vap->iv_vhtextcaps = ic->ic_vhtextcaps;
150 
151 	/* XXX assume VHT80 support; should really check vhtcaps */
152 	vap->iv_flags_vht =
153 	    IEEE80211_FVHT_VHT
154 	    | IEEE80211_FVHT_USEVHT40
155 	    | IEEE80211_FVHT_USEVHT80;
156 	if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160MHZ(vap->iv_vhtcaps))
157 		vap->iv_flags_vht |= IEEE80211_FVHT_USEVHT160;
158 	if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160_80P80MHZ(vap->iv_vhtcaps))
159 		vap->iv_flags_vht |= IEEE80211_FVHT_USEVHT80P80;
160 
161 	memcpy(&vap->iv_vht_mcsinfo, &ic->ic_vht_mcsinfo,
162 	    sizeof(struct ieee80211_vht_mcs_info));
163 }
164 
165 void
166 ieee80211_vht_vdetach(struct ieee80211vap *vap)
167 {
168 }
169 
170 #if 0
171 static void
172 vht_announce(struct ieee80211com *ic, enum ieee80211_phymode mode)
173 {
174 }
175 #endif
176 
177 static int
178 vht_mcs_to_num(int m)
179 {
180 
181 	switch (m) {
182 	case IEEE80211_VHT_MCS_SUPPORT_0_7:
183 		return (7);
184 	case IEEE80211_VHT_MCS_SUPPORT_0_8:
185 		return (8);
186 	case IEEE80211_VHT_MCS_SUPPORT_0_9:
187 		return (9);
188 	default:
189 		return (0);
190 	}
191 }
192 
193 void
194 ieee80211_vht_announce(struct ieee80211com *ic)
195 {
196 	int i, tx, rx;
197 
198 	if (! IEEE80211_CONF_VHT(ic))
199 		return;
200 
201 	/* Channel width */
202 	ic_printf(ic, "[VHT] Channel Widths: 20MHz, 40MHz, 80MHz%s%s\n",
203 	    (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160MHZ(ic->ic_vhtcaps)) ?
204 		", 160MHz" : "",
205 	    (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160_80P80MHZ(ic->ic_vhtcaps)) ?
206 		 ", 80+80MHz" : "");
207 	/* Features */
208 	ic_printf(ic, "[VHT] Features: %b\n", ic->ic_vhtcaps,
209 	    IEEE80211_VHTCAP_BITS);
210 
211 	/* For now, just 5GHz VHT.  Worry about 2GHz VHT later */
212 	for (i = 0; i < 8; i++) {
213 		/* Each stream is 2 bits */
214 		tx = (ic->ic_vht_mcsinfo.tx_mcs_map >> (2*i)) & 0x3;
215 		rx = (ic->ic_vht_mcsinfo.rx_mcs_map >> (2*i)) & 0x3;
216 		if (tx == 3 && rx == 3)
217 			continue;
218 		ic_printf(ic, "[VHT] NSS %d: TX MCS 0..%d, RX MCS 0..%d\n",
219 		    i + 1, vht_mcs_to_num(tx), vht_mcs_to_num(rx));
220 	}
221 }
222 
223 void
224 ieee80211_vht_node_init(struct ieee80211_node *ni)
225 {
226 
227 	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, ni,
228 	    "%s: called", __func__);
229 	ni->ni_flags |= IEEE80211_NODE_VHT;
230 }
231 
232 void
233 ieee80211_vht_node_cleanup(struct ieee80211_node *ni)
234 {
235 
236 	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, ni,
237 	    "%s: called", __func__);
238 	ni->ni_flags &= ~IEEE80211_NODE_VHT;
239 	ni->ni_vhtcap = 0;
240 	bzero(&ni->ni_vht_mcsinfo, sizeof(struct ieee80211_vht_mcs_info));
241 }
242 
243 /*
244  * Parse an 802.11ac VHT operation IE.
245  */
246 void
247 ieee80211_parse_vhtopmode(struct ieee80211_node *ni, const uint8_t *ie)
248 {
249 	/* vht operation */
250 	ni->ni_vht_chanwidth = ie[2];
251 	ni->ni_vht_chan1 = ie[3];
252 	ni->ni_vht_chan2 = ie[4];
253 	ni->ni_vht_basicmcs = le16dec(ie + 5);
254 
255 #if 0
256 	printf("%s: chan1=%d, chan2=%d, chanwidth=%d, basicmcs=0x%04x\n",
257 	    __func__, ni->ni_vht_chan1, ni->ni_vht_chan2, ni->ni_vht_chanwidth,
258 	    ni->ni_vht_basicmcs);
259 #endif
260 }
261 
262 /*
263  * Parse an 802.11ac VHT capability IE.
264  */
265 void
266 ieee80211_parse_vhtcap(struct ieee80211_node *ni, const uint8_t *ie)
267 {
268 
269 	/* vht capability */
270 	ni->ni_vhtcap = le32dec(ie + 2);
271 
272 	/* suppmcs */
273 	ni->ni_vht_mcsinfo.rx_mcs_map = le16dec(ie + 6);
274 	ni->ni_vht_mcsinfo.rx_highest = le16dec(ie + 8);
275 	ni->ni_vht_mcsinfo.tx_mcs_map = le16dec(ie + 10);
276 	ni->ni_vht_mcsinfo.tx_highest = le16dec(ie + 12);
277 }
278 
279 int
280 ieee80211_vht_updateparams(struct ieee80211_node *ni,
281     const uint8_t *vhtcap_ie,
282     const uint8_t *vhtop_ie)
283 {
284 
285 	//printf("%s: called\n", __func__);
286 
287 	ieee80211_parse_vhtcap(ni, vhtcap_ie);
288 	ieee80211_parse_vhtopmode(ni, vhtop_ie);
289 	return (0);
290 }
291 
292 void
293 ieee80211_setup_vht_rates(struct ieee80211_node *ni,
294     const uint8_t *vhtcap_ie,
295     const uint8_t *vhtop_ie)
296 {
297 
298 	//printf("%s: called\n", __func__);
299 	/* XXX TODO */
300 }
301 
302 void
303 ieee80211_vht_timeout(struct ieee80211vap *vap)
304 {
305 }
306 
307 void
308 ieee80211_vht_node_join(struct ieee80211_node *ni)
309 {
310 
311 	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, ni,
312 	    "%s: called", __func__);
313 }
314 
315 void
316 ieee80211_vht_node_leave(struct ieee80211_node *ni)
317 {
318 
319 	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, ni,
320 	    "%s: called", __func__);
321 }
322 
323 /*
324  * Calculate the VHTCAP IE for a given node.
325  *
326  * This includes calculating the capability intersection based on the
327  * current operating mode and intersection of the TX/RX MCS maps.
328  *
329  * The standard only makes it clear about MCS rate negotiation
330  * and MCS basic rates (which must be a subset of the general
331  * negotiated rates).  It doesn't make it clear that the AP should
332  * figure out the minimum functional overlap with the STA and
333  * support that.
334  *
335  * Note: this is in host order, not in 802.11 endian order.
336  *
337  * TODO: ensure I re-read 9.7.11 Rate Selection for VHT STAs.
338  *
339  * TODO: investigate what we should negotiate for MU-MIMO beamforming
340  *       options.
341  *
342  * opmode is '1' for "vhtcap as if I'm a STA", 0 otherwise.
343  */
344 void
345 ieee80211_vht_get_vhtcap_ie(struct ieee80211_node *ni,
346     struct ieee80211_ie_vhtcap *vhtcap, int opmode)
347 {
348 	struct ieee80211vap *vap = ni->ni_vap;
349 //	struct ieee80211com *ic = vap->iv_ic;
350 	uint32_t val, val1, val2;
351 	uint32_t new_vhtcap;
352 	int i;
353 
354 	vhtcap->ie = IEEE80211_ELEMID_VHT_CAP;
355 	vhtcap->len = sizeof(struct ieee80211_ie_vhtcap) - 2;
356 
357 	/*
358 	 * Capabilities - it depends on whether we are a station
359 	 * or not.
360 	 */
361 	new_vhtcap = 0;
362 
363 	/*
364 	 * Station - use our desired configuration based on
365 	 * local config, local device bits and the already-learnt
366 	 * vhtcap/vhtinfo IE in the node.
367 	 */
368 
369 	/* Limit MPDU size to the smaller of the two */
370 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vhtcaps,
371 	    IEEE80211_VHTCAP_MAX_MPDU_MASK);
372 	if (opmode == 1) {
373 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
374 		    IEEE80211_VHTCAP_MAX_MPDU_MASK);
375 	}
376 	val = MIN(val1, val2);
377 	new_vhtcap |= _IEEE80211_SHIFTMASK(val, IEEE80211_VHTCAP_MAX_MPDU_MASK);
378 
379 	/* Limit supp channel config */
380 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vhtcaps,
381 	    IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_MASK);
382 	if (opmode == 1) {
383 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
384 		    IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_MASK);
385 	}
386 	if ((val2 == 2) &&
387 	    ((vap->iv_flags_vht & IEEE80211_FVHT_USEVHT80P80) == 0))
388 		val2 = 1;
389 	if ((val2 == 1) &&
390 	    ((vap->iv_flags_vht & IEEE80211_FVHT_USEVHT160) == 0))
391 		val2 = 0;
392 	val = MIN(val1, val2);
393 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
394 	     IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_MASK);
395 
396 	/* RX LDPC */
397 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vhtcaps,
398 	    IEEE80211_VHTCAP_RXLDPC);
399 	if (opmode == 1) {
400 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
401 		    IEEE80211_VHTCAP_RXLDPC);
402 	}
403 	val = MIN(val1, val2);
404 	new_vhtcap |= _IEEE80211_SHIFTMASK(val, IEEE80211_VHTCAP_RXLDPC);
405 
406 	/* Short-GI 80 */
407 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vhtcaps,
408 	    IEEE80211_VHTCAP_SHORT_GI_80);
409 	if (opmode == 1) {
410 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
411 		    IEEE80211_VHTCAP_SHORT_GI_80);
412 	}
413 	val = MIN(val1, val2);
414 	new_vhtcap |= _IEEE80211_SHIFTMASK(val, IEEE80211_VHTCAP_SHORT_GI_80);
415 
416 	/* Short-GI 160 */
417 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vhtcaps,
418 	    IEEE80211_VHTCAP_SHORT_GI_160);
419 	if (opmode == 1) {
420 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
421 		    IEEE80211_VHTCAP_SHORT_GI_160);
422 	}
423 	val = MIN(val1, val2);
424 	new_vhtcap |= _IEEE80211_SHIFTMASK(val, IEEE80211_VHTCAP_SHORT_GI_160);
425 
426 	/*
427 	 * STBC is slightly more complicated.
428 	 *
429 	 * In non-STA mode, we just announce our capabilities and that
430 	 * is that.
431 	 *
432 	 * In STA mode, we should calculate our capabilities based on
433 	 * local capabilities /and/ what the remote says. So:
434 	 *
435 	 * + Only TX STBC if we support it and the remote supports RX STBC;
436 	 * + Only announce RX STBC if we support it and the remote supports
437 	 *   TX STBC;
438 	 * + RX STBC should be the minimum of local and remote RX STBC;
439 	 */
440 
441 	/* TX STBC */
442 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vhtcaps,
443 	    IEEE80211_VHTCAP_TXSTBC);
444 	if (opmode == 1) {
445 		/* STA mode - enable it only if node RXSTBC is non-zero */
446 		val2 = !! _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
447 		    IEEE80211_VHTCAP_RXSTBC_MASK);
448 	}
449 	val = MIN(val1, val2);
450 	/* XXX For now, use the 11n config flag */
451 	if ((vap->iv_flags_ht & IEEE80211_FHT_STBC_TX) == 0)
452 		val = 0;
453 	new_vhtcap |= _IEEE80211_SHIFTMASK(val, IEEE80211_VHTCAP_TXSTBC);
454 
455 	/* RX STBC1..4 */
456 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vhtcaps,
457 	    IEEE80211_VHTCAP_RXSTBC_MASK);
458 	if (opmode == 1) {
459 		/* STA mode - enable it only if node TXSTBC is non-zero */
460 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
461 		   IEEE80211_VHTCAP_TXSTBC);
462 	}
463 	val = MIN(val1, val2);
464 	/* XXX For now, use the 11n config flag */
465 	if ((vap->iv_flags_ht & IEEE80211_FHT_STBC_RX) == 0)
466 		val = 0;
467 	new_vhtcap |= _IEEE80211_SHIFTMASK(val, IEEE80211_VHTCAP_RXSTBC_MASK);
468 
469 	/*
470 	 * Finally - if RXSTBC is 0, then don't enable TXSTBC.
471 	 * Strictly speaking a device can TXSTBC and not RXSTBC, but
472 	 * it would be silly.
473 	 */
474 	if (val == 0)
475 		new_vhtcap &= ~IEEE80211_VHTCAP_TXSTBC;
476 
477 	/*
478 	 * Some of these fields require other fields to exist.
479 	 * So before using it, the parent field needs to be checked
480 	 * otherwise the overridden value may be wrong.
481 	 *
482 	 * For example, if SU beamformee is set to 0, then BF STS
483 	 * needs to be 0.
484 	 */
485 
486 	/* SU Beamformer capable */
487 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vhtcaps,
488 	    IEEE80211_VHTCAP_SU_BEAMFORMER_CAPABLE);
489 	if (opmode == 1) {
490 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
491 		    IEEE80211_VHTCAP_SU_BEAMFORMER_CAPABLE);
492 	}
493 	val = MIN(val1, val2);
494 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
495 	    IEEE80211_VHTCAP_SU_BEAMFORMER_CAPABLE);
496 
497 	/* SU Beamformee capable */
498 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vhtcaps,
499 	    IEEE80211_VHTCAP_SU_BEAMFORMEE_CAPABLE);
500 	if (opmode == 1) {
501 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
502 		    IEEE80211_VHTCAP_SU_BEAMFORMEE_CAPABLE);
503 	}
504 	val = MIN(val1, val2);
505 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
506 	    IEEE80211_VHTCAP_SU_BEAMFORMEE_CAPABLE);
507 
508 	/* Beamformee STS capability - only if SU beamformee capable */
509 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vhtcaps,
510 	    IEEE80211_VHTCAP_BEAMFORMEE_STS_MASK);
511 	if (opmode == 1) {
512 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
513 		    IEEE80211_VHTCAP_BEAMFORMEE_STS_MASK);
514 	}
515 	val = MIN(val1, val2);
516 	if ((new_vhtcap & IEEE80211_VHTCAP_SU_BEAMFORMEE_CAPABLE) == 0)
517 		val = 0;
518 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
519 	    IEEE80211_VHTCAP_BEAMFORMEE_STS_MASK);
520 
521 	/* Sounding dimensions - only if SU beamformer capable */
522 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vhtcaps,
523 	    IEEE80211_VHTCAP_SOUNDING_DIMENSIONS_MASK);
524 	if (opmode == 1)
525 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
526 		    IEEE80211_VHTCAP_SOUNDING_DIMENSIONS_MASK);
527 	val = MIN(val1, val2);
528 	if ((new_vhtcap & IEEE80211_VHTCAP_SU_BEAMFORMER_CAPABLE) == 0)
529 		val = 0;
530 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
531 	    IEEE80211_VHTCAP_SOUNDING_DIMENSIONS_MASK);
532 
533 	/*
534 	 * MU Beamformer capable - only if SU BFF capable, MU BFF capable
535 	 * and STA (not AP)
536 	 */
537 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vhtcaps,
538 	    IEEE80211_VHTCAP_MU_BEAMFORMER_CAPABLE);
539 	if (opmode == 1)
540 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
541 		    IEEE80211_VHTCAP_MU_BEAMFORMER_CAPABLE);
542 	val = MIN(val1, val2);
543 	if ((new_vhtcap & IEEE80211_VHTCAP_SU_BEAMFORMER_CAPABLE) == 0)
544 		val = 0;
545 	if (opmode != 1)	/* Only enable for STA mode */
546 		val = 0;
547 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
548 	   IEEE80211_VHTCAP_SU_BEAMFORMER_CAPABLE);
549 
550 	/*
551 	 * MU Beamformee capable - only if SU BFE capable, MU BFE capable
552 	 * and AP (not STA)
553 	 */
554 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vhtcaps,
555 	    IEEE80211_VHTCAP_MU_BEAMFORMEE_CAPABLE);
556 	if (opmode == 1)
557 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
558 		    IEEE80211_VHTCAP_MU_BEAMFORMEE_CAPABLE);
559 	val = MIN(val1, val2);
560 	if ((new_vhtcap & IEEE80211_VHTCAP_SU_BEAMFORMEE_CAPABLE) == 0)
561 		val = 0;
562 	if (opmode != 0)	/* Only enable for AP mode */
563 		val = 0;
564 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
565 	   IEEE80211_VHTCAP_SU_BEAMFORMEE_CAPABLE);
566 
567 	/* VHT TXOP PS */
568 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vhtcaps,
569 	    IEEE80211_VHTCAP_VHT_TXOP_PS);
570 	if (opmode == 1)
571 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
572 		    IEEE80211_VHTCAP_VHT_TXOP_PS);
573 	val = MIN(val1, val2);
574 	new_vhtcap |= _IEEE80211_SHIFTMASK(val, IEEE80211_VHTCAP_VHT_TXOP_PS);
575 
576 	/* HTC_VHT */
577 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vhtcaps,
578 	    IEEE80211_VHTCAP_HTC_VHT);
579 	if (opmode == 1)
580 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
581 		    IEEE80211_VHTCAP_HTC_VHT);
582 	val = MIN(val1, val2);
583 	new_vhtcap |= _IEEE80211_SHIFTMASK(val, IEEE80211_VHTCAP_HTC_VHT);
584 
585 	/* A-MPDU length max */
586 	/* XXX TODO: we need a userland config knob for this */
587 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vhtcaps,
588 	    IEEE80211_VHTCAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK);
589 	if (opmode == 1)
590 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
591 		    IEEE80211_VHTCAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK);
592 	val = MIN(val1, val2);
593 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
594 	    IEEE80211_VHTCAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK);
595 
596 	/*
597 	 * Link adaptation is only valid if HTC-VHT capable is 1.
598 	 * Otherwise, always set it to 0.
599 	 */
600 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vhtcaps,
601 	    IEEE80211_VHTCAP_VHT_LINK_ADAPTATION_VHT_MASK);
602 	if (opmode == 1)
603 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
604 		    IEEE80211_VHTCAP_VHT_LINK_ADAPTATION_VHT_MASK);
605 	val = MIN(val1, val2);
606 	if ((new_vhtcap & IEEE80211_VHTCAP_HTC_VHT) == 0)
607 		val = 0;
608 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
609 	    IEEE80211_VHTCAP_VHT_LINK_ADAPTATION_VHT_MASK);
610 
611 	/*
612 	 * The following two options are 0 if the pattern may change, 1 if it
613 	 * does not change.  So, downgrade to the higher value.
614 	 */
615 
616 	/* RX antenna pattern */
617 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vhtcaps,
618 	    IEEE80211_VHTCAP_RX_ANTENNA_PATTERN);
619 	if (opmode == 1)
620 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
621 		    IEEE80211_VHTCAP_RX_ANTENNA_PATTERN);
622 	val = MAX(val1, val2);
623 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
624 	    IEEE80211_VHTCAP_RX_ANTENNA_PATTERN);
625 
626 	/* TX antenna pattern */
627 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vhtcaps,
628 	    IEEE80211_VHTCAP_TX_ANTENNA_PATTERN);
629 	if (opmode == 1)
630 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
631 		    IEEE80211_VHTCAP_TX_ANTENNA_PATTERN);
632 	val = MAX(val1, val2);
633 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
634 	    IEEE80211_VHTCAP_TX_ANTENNA_PATTERN);
635 
636 	/*
637 	 * MCS set - again, we announce what we want to use
638 	 * based on configuration, device capabilities and
639 	 * already-learnt vhtcap/vhtinfo IE information.
640 	 */
641 
642 	/* MCS set - start with whatever the device supports */
643 	vhtcap->supp_mcs.rx_mcs_map = vap->iv_vht_mcsinfo.rx_mcs_map;
644 	vhtcap->supp_mcs.rx_highest = 0;
645 	vhtcap->supp_mcs.tx_mcs_map = vap->iv_vht_mcsinfo.tx_mcs_map;
646 	vhtcap->supp_mcs.tx_highest = 0;
647 
648 	vhtcap->vht_cap_info = new_vhtcap;
649 
650 	/*
651 	 * Now, if we're a STA, mask off whatever the AP doesn't support.
652 	 * Ie, we continue to state we can receive whatever we can do,
653 	 * but we only announce that we will transmit rates that meet
654 	 * the AP requirement.
655 	 *
656 	 * Note: 0 - MCS0..7; 1 - MCS0..8; 2 - MCS0..9; 3 = not supported.
657 	 * We can't just use MIN() because '3' means "no", so special case it.
658 	 */
659 	if (opmode) {
660 		for (i = 0; i < 8; i++) {
661 			val1 = (vhtcap->supp_mcs.tx_mcs_map >> (i*2)) & 0x3;
662 			val2 = (ni->ni_vht_mcsinfo.tx_mcs_map >> (i*2)) & 0x3;
663 			val = MIN(val1, val2);
664 			if (val1 == 3 || val2 == 3)
665 				val = 3;
666 			vhtcap->supp_mcs.tx_mcs_map &= ~(0x3 << (i*2));
667 			vhtcap->supp_mcs.tx_mcs_map |= (val << (i*2));
668 		}
669 	}
670 }
671 
672 /*
673  * Add a VHTCAP field.
674  *
675  * If in station mode, we announce what we would like our
676  * desired configuration to be.
677  *
678  * Else, we announce our capabilities based on our current
679  * configuration.
680  */
681 uint8_t *
682 ieee80211_add_vhtcap(uint8_t *frm, struct ieee80211_node *ni)
683 {
684 	struct ieee80211_ie_vhtcap vhtcap;
685 	int opmode;
686 
687 	opmode = 0;
688 	if (ni->ni_vap->iv_opmode == IEEE80211_M_STA)
689 		opmode = 1;
690 
691 	ieee80211_vht_get_vhtcap_ie(ni, &vhtcap, opmode);
692 
693 	memset(frm, '\0', sizeof(struct ieee80211_ie_vhtcap));
694 
695 	frm[0] = IEEE80211_ELEMID_VHT_CAP;
696 	frm[1] = sizeof(struct ieee80211_ie_vhtcap) - 2;
697 	frm += 2;
698 
699 	/* 32-bit VHT capability */
700 	ADDWORD(frm, vhtcap.vht_cap_info);
701 
702 	/* suppmcs */
703 	ADDSHORT(frm, vhtcap.supp_mcs.rx_mcs_map);
704 	ADDSHORT(frm, vhtcap.supp_mcs.rx_highest);
705 	ADDSHORT(frm, vhtcap.supp_mcs.tx_mcs_map);
706 	ADDSHORT(frm, vhtcap.supp_mcs.tx_highest);
707 
708 	return (frm);
709 }
710 
711 static uint8_t
712 ieee80211_vht_get_chwidth_ie(struct ieee80211_channel *c)
713 {
714 
715 	/*
716 	 * XXX TODO: look at the node configuration as
717 	 * well?
718 	 */
719 
720 	if (IEEE80211_IS_CHAN_VHT80P80(c))
721 		return IEEE80211_VHT_CHANWIDTH_80P80MHZ;
722 	if (IEEE80211_IS_CHAN_VHT160(c))
723 		return IEEE80211_VHT_CHANWIDTH_160MHZ;
724 	if (IEEE80211_IS_CHAN_VHT80(c))
725 		return IEEE80211_VHT_CHANWIDTH_80MHZ;
726 	if (IEEE80211_IS_CHAN_VHT40(c))
727 		return IEEE80211_VHT_CHANWIDTH_USE_HT;
728 	if (IEEE80211_IS_CHAN_VHT20(c))
729 		return IEEE80211_VHT_CHANWIDTH_USE_HT;
730 
731 	/* We shouldn't get here */
732 	printf("%s: called on a non-VHT channel (freq=%d, flags=0x%08x\n",
733 	    __func__, (int) c->ic_freq, c->ic_flags);
734 	return IEEE80211_VHT_CHANWIDTH_USE_HT;
735 }
736 
737 /*
738  * Note: this just uses the current channel information;
739  * it doesn't use the node info after parsing.
740  *
741  * XXX TODO: need to make the basic MCS set configurable.
742  * XXX TODO: read 802.11-2013 to determine what to set
743  *           chwidth to when scanning.  I have a feeling
744  *           it isn't involved in scanning and we shouldn't
745  *           be sending it; and I don't yet know what to set
746  *           it to for IBSS or hostap where the peer may be
747  *           a completely different channel width to us.
748  */
749 uint8_t *
750 ieee80211_add_vhtinfo(uint8_t *frm, struct ieee80211_node *ni)
751 {
752 	memset(frm, '\0', sizeof(struct ieee80211_ie_vht_operation));
753 
754 	frm[0] = IEEE80211_ELEMID_VHT_OPMODE;
755 	frm[1] = sizeof(struct ieee80211_ie_vht_operation) - 2;
756 	frm += 2;
757 
758 	/* 8-bit chanwidth */
759 	*frm++ = ieee80211_vht_get_chwidth_ie(ni->ni_chan);
760 
761 	/* 8-bit freq1 */
762 	*frm++ = ni->ni_chan->ic_vht_ch_freq1;
763 
764 	/* 8-bit freq2 */
765 	*frm++ = ni->ni_chan->ic_vht_ch_freq2;
766 
767 	/* 16-bit basic MCS set - just MCS0..7 for NSS=1 for now */
768 	ADDSHORT(frm, 0xfffc);
769 
770 	return (frm);
771 }
772 
773 void
774 ieee80211_vht_update_cap(struct ieee80211_node *ni, const uint8_t *vhtcap_ie,
775     const uint8_t *vhtop_ie)
776 {
777 
778 	ieee80211_parse_vhtcap(ni, vhtcap_ie);
779 	ieee80211_parse_vhtopmode(ni, vhtop_ie);
780 }
781 
782 static struct ieee80211_channel *
783 findvhtchan(struct ieee80211com *ic, struct ieee80211_channel *c, int vhtflags)
784 {
785 
786 	return (ieee80211_find_channel(ic, c->ic_freq,
787 	    (c->ic_flags & ~IEEE80211_CHAN_VHT) | vhtflags));
788 }
789 
790 /*
791  * Handle channel promotion to VHT, similar to ieee80211_ht_adjust_channel().
792  */
793 struct ieee80211_channel *
794 ieee80211_vht_adjust_channel(struct ieee80211com *ic,
795     struct ieee80211_channel *chan, int flags)
796 {
797 	struct ieee80211_channel *c;
798 
799 	/* First case - handle channel demotion - if VHT isn't set */
800 	if ((flags & IEEE80211_FVHT_VHT) == 0) {
801 #if 0
802 		printf("%s: demoting channel %d/0x%08x\n", __func__,
803 		    chan->ic_ieee, chan->ic_flags);
804 #endif
805 		c = ieee80211_find_channel(ic, chan->ic_freq,
806 		    chan->ic_flags & ~IEEE80211_CHAN_VHT);
807 		if (c == NULL)
808 			c = chan;
809 #if 0
810 		printf("%s: .. to %d/0x%08x\n", __func__,
811 		    c->ic_ieee, c->ic_flags);
812 #endif
813 		return (c);
814 	}
815 
816 	/*
817 	 * We can upgrade to VHT - attempt to do so
818 	 *
819 	 * Note: we don't clear the HT flags, these are the hints
820 	 * for HT40U/HT40D when selecting VHT40 or larger channels.
821 	 */
822 	/* Start with VHT80 */
823 	c = NULL;
824 	if ((c == NULL) && (flags & IEEE80211_FVHT_USEVHT160))
825 		c = findvhtchan(ic, chan, IEEE80211_CHAN_VHT80);
826 
827 	if ((c == NULL) && (flags & IEEE80211_FVHT_USEVHT80P80))
828 		c = findvhtchan(ic, chan, IEEE80211_CHAN_VHT80P80);
829 
830 	if ((c == NULL) && (flags & IEEE80211_FVHT_USEVHT80))
831 		c = findvhtchan(ic, chan, IEEE80211_CHAN_VHT80);
832 
833 	if ((c == NULL) && (flags & IEEE80211_FVHT_USEVHT40))
834 		c = findvhtchan(ic, chan, IEEE80211_CHAN_VHT40U);
835 	if ((c == NULL) && (flags & IEEE80211_FVHT_USEVHT40))
836 		c = findvhtchan(ic, chan, IEEE80211_CHAN_VHT40D);
837 	/*
838 	 * If we get here, VHT20 is always possible because we checked
839 	 * for IEEE80211_FVHT_VHT above.
840 	 */
841 	if (c == NULL)
842 		c = findvhtchan(ic, chan, IEEE80211_CHAN_VHT20);
843 
844 	if (c != NULL)
845 		chan = c;
846 
847 #if 0
848 	printf("%s: selected %d/0x%08x\n", __func__, c->ic_ieee, c->ic_flags);
849 #endif
850 	return (chan);
851 }
852 
853 /*
854  * Calculate the VHT operation IE for a given node.
855  *
856  * This includes calculating the suitable channel width/parameters
857  * and basic MCS set.
858  *
859  * TODO: ensure I read 9.7.11 Rate Selection for VHT STAs.
860  * TODO: ensure I read 10.39.7 - BSS Basic VHT-MCS and NSS set operation.
861  */
862 void
863 ieee80211_vht_get_vhtinfo_ie(struct ieee80211_node *ni,
864     struct ieee80211_ie_vht_operation *vhtop, int opmode)
865 {
866 	printf("%s: called; TODO!\n", __func__);
867 }
868