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