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