]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/net80211/ieee80211_vht.c
vfs: group vnode-related sysctls under vfs.vnode
[FreeBSD/FreeBSD.git] / sys / net80211 / ieee80211_vht.c
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_vhtcaps = ic->ic_vhtcaps;
148         vap->iv_vhtextcaps = ic->ic_vhtextcaps;
149
150         /* XXX assume VHT80 support; should really check vhtcaps */
151         vap->iv_flags_vht =
152             IEEE80211_FVHT_VHT
153             | IEEE80211_FVHT_USEVHT40
154             | IEEE80211_FVHT_USEVHT80;
155         if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160MHZ(vap->iv_vhtcaps))
156                 vap->iv_flags_vht |= IEEE80211_FVHT_USEVHT160;
157         if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160_80P80MHZ(vap->iv_vhtcaps))
158                 vap->iv_flags_vht |= IEEE80211_FVHT_USEVHT80P80;
159
160         memcpy(&vap->iv_vht_mcsinfo, &ic->ic_vht_mcsinfo,
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_vhtcaps)) ?
203                 ", 160MHz" : "",
204             (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160_80P80MHZ(ic->ic_vhtcaps)) ?
205                  ", 80+80MHz" : "");
206         /* Features */
207         ic_printf(ic, "[VHT] Features: %b\n", ic->ic_vhtcaps,
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_mcsinfo.tx_mcs_map >> (2*i)) & 0x3;
214                 rx = (ic->ic_vht_mcsinfo.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_vhtcaps,
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_vhtcaps,
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_flags_vht & IEEE80211_FVHT_USEVHT80P80) == 0))
387                 val2 = 1;
388         if ((val2 == 1) &&
389             ((vap->iv_flags_vht & 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_vhtcaps,
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_vhtcaps,
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_vhtcaps,
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_vhtcaps,
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_vhtcaps,
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_vhtcaps,
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_vhtcaps,
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_vhtcaps,
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_vhtcaps,
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_vhtcaps,
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_vhtcaps,
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_vhtcaps,
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_vhtcaps,
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_vhtcaps,
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_vhtcaps,
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_vhtcaps,
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_vhtcaps,
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_mcsinfo.rx_mcs_map;
643         vhtcap->supp_mcs.rx_highest = 0;
644         vhtcap->supp_mcs.tx_mcs_map = vap->iv_vht_mcsinfo.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 static uint8_t
711 ieee80211_vht_get_chwidth_ie(struct ieee80211_channel *c)
712 {
713
714         /*
715          * XXX TODO: look at the node configuration as
716          * well?
717          */
718
719         if (IEEE80211_IS_CHAN_VHT80P80(c))
720                 return IEEE80211_VHT_CHANWIDTH_80P80MHZ;
721         if (IEEE80211_IS_CHAN_VHT160(c))
722                 return IEEE80211_VHT_CHANWIDTH_160MHZ;
723         if (IEEE80211_IS_CHAN_VHT80(c))
724                 return IEEE80211_VHT_CHANWIDTH_80MHZ;
725         if (IEEE80211_IS_CHAN_VHT40(c))
726                 return IEEE80211_VHT_CHANWIDTH_USE_HT;
727         if (IEEE80211_IS_CHAN_VHT20(c))
728                 return IEEE80211_VHT_CHANWIDTH_USE_HT;
729
730         /* We shouldn't get here */
731         printf("%s: called on a non-VHT channel (freq=%d, flags=0x%08x\n",
732             __func__, (int) c->ic_freq, c->ic_flags);
733         return IEEE80211_VHT_CHANWIDTH_USE_HT;
734 }
735
736 /*
737  * Note: this just uses the current channel information;
738  * it doesn't use the node info after parsing.
739  *
740  * XXX TODO: need to make the basic MCS set configurable.
741  * XXX TODO: read 802.11-2013 to determine what to set
742  *           chwidth to when scanning.  I have a feeling
743  *           it isn't involved in scanning and we shouldn't
744  *           be sending it; and I don't yet know what to set
745  *           it to for IBSS or hostap where the peer may be
746  *           a completely different channel width to us.
747  */
748 uint8_t *
749 ieee80211_add_vhtinfo(uint8_t *frm, struct ieee80211_node *ni)
750 {
751         memset(frm, '\0', sizeof(struct ieee80211_ie_vht_operation));
752
753         frm[0] = IEEE80211_ELEMID_VHT_OPMODE;
754         frm[1] = sizeof(struct ieee80211_ie_vht_operation) - 2;
755         frm += 2;
756
757         /* 8-bit chanwidth */
758         *frm++ = ieee80211_vht_get_chwidth_ie(ni->ni_chan);
759
760         /* 8-bit freq1 */
761         *frm++ = ni->ni_chan->ic_vht_ch_freq1;
762
763         /* 8-bit freq2 */
764         *frm++ = ni->ni_chan->ic_vht_ch_freq2;
765
766         /* 16-bit basic MCS set - just MCS0..7 for NSS=1 for now */
767         ADDSHORT(frm, 0xfffc);
768
769         return (frm);
770 }
771
772 void
773 ieee80211_vht_update_cap(struct ieee80211_node *ni, const uint8_t *vhtcap_ie,
774     const uint8_t *vhtop_ie)
775 {
776
777         ieee80211_parse_vhtcap(ni, vhtcap_ie);
778         ieee80211_parse_vhtopmode(ni, vhtop_ie);
779 }
780
781 static struct ieee80211_channel *
782 findvhtchan(struct ieee80211com *ic, struct ieee80211_channel *c, int vhtflags)
783 {
784
785         return (ieee80211_find_channel(ic, c->ic_freq,
786             (c->ic_flags & ~IEEE80211_CHAN_VHT) | vhtflags));
787 }
788
789 /*
790  * Handle channel promotion to VHT, similar to ieee80211_ht_adjust_channel().
791  */
792 struct ieee80211_channel *
793 ieee80211_vht_adjust_channel(struct ieee80211com *ic,
794     struct ieee80211_channel *chan, int flags)
795 {
796         struct ieee80211_channel *c;
797
798         /* First case - handle channel demotion - if VHT isn't set */
799         if ((flags & IEEE80211_FVHT_MASK) == 0) {
800 #if 0
801                 printf("%s: demoting channel %d/0x%08x\n", __func__,
802                     chan->ic_ieee, chan->ic_flags);
803 #endif
804                 c = ieee80211_find_channel(ic, chan->ic_freq,
805                     chan->ic_flags & ~IEEE80211_CHAN_VHT);
806                 if (c == NULL)
807                         c = chan;
808 #if 0
809                 printf("%s: .. to %d/0x%08x\n", __func__,
810                     c->ic_ieee, c->ic_flags);
811 #endif
812                 return (c);
813         }
814
815         /*
816          * We can upgrade to VHT - attempt to do so
817          *
818          * Note: we don't clear the HT flags, these are the hints
819          * for HT40U/HT40D when selecting VHT40 or larger channels.
820          */
821         c = NULL;
822         if ((c == NULL) && (flags & IEEE80211_FVHT_USEVHT160))
823                 c = findvhtchan(ic, chan, IEEE80211_CHAN_VHT160);
824
825         if ((c == NULL) && (flags & IEEE80211_FVHT_USEVHT80P80))
826                 c = findvhtchan(ic, chan, IEEE80211_CHAN_VHT80P80);
827
828         if ((c == NULL) && (flags & IEEE80211_FVHT_USEVHT80))
829                 c = findvhtchan(ic, chan, IEEE80211_CHAN_VHT80);
830
831         if ((c == NULL) && (flags & IEEE80211_FVHT_USEVHT40))
832                 c = findvhtchan(ic, chan, IEEE80211_CHAN_VHT40U);
833         if ((c == NULL) && (flags & IEEE80211_FVHT_USEVHT40))
834                 c = findvhtchan(ic, chan, IEEE80211_CHAN_VHT40D);
835         /*
836          * If we get here, VHT20 is always possible because we checked
837          * for IEEE80211_FVHT_VHT above.
838          */
839         if (c == NULL)
840                 c = findvhtchan(ic, chan, IEEE80211_CHAN_VHT20);
841
842         if (c != NULL)
843                 chan = c;
844
845 #if 0
846         printf("%s: selected %d/0x%08x\n", __func__, c->ic_ieee, c->ic_flags);
847 #endif
848         return (chan);
849 }
850
851 /*
852  * Calculate the VHT operation IE for a given node.
853  *
854  * This includes calculating the suitable channel width/parameters
855  * and basic MCS set.
856  *
857  * TODO: ensure I read 9.7.11 Rate Selection for VHT STAs.
858  * TODO: ensure I read 10.39.7 - BSS Basic VHT-MCS and NSS set operation.
859  */
860 void
861 ieee80211_vht_get_vhtinfo_ie(struct ieee80211_node *ni,
862     struct ieee80211_ie_vht_operation *vhtop, int opmode)
863 {
864         printf("%s: called; TODO!\n", __func__);
865 }