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