]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/net80211/ieee80211_ht.c
MFV r362143:
[FreeBSD/FreeBSD.git] / sys / net80211 / ieee80211_ht.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2007-2008 Sam Leffler, Errno Consulting
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #include <sys/cdefs.h>
29 #ifdef __FreeBSD__
30 __FBSDID("$FreeBSD$");
31 #endif
32
33 /*
34  * IEEE 802.11n protocol support.
35  */
36
37 #include "opt_inet.h"
38 #include "opt_wlan.h"
39
40 #include <sys/param.h>
41 #include <sys/kernel.h>
42 #include <sys/malloc.h>
43 #include <sys/systm.h> 
44 #include <sys/endian.h>
45  
46 #include <sys/socket.h>
47
48 #include <net/if.h>
49 #include <net/if_var.h>
50 #include <net/if_media.h>
51 #include <net/ethernet.h>
52
53 #include <net80211/ieee80211_var.h>
54 #include <net80211/ieee80211_action.h>
55 #include <net80211/ieee80211_input.h>
56
57 /* define here, used throughout file */
58 #define MS(_v, _f)      (((_v) & _f) >> _f##_S)
59 #define SM(_v, _f)      (((_v) << _f##_S) & _f)
60
61 const struct ieee80211_mcs_rates ieee80211_htrates[IEEE80211_HTRATE_MAXSIZE] = {
62         {  13,  14,   27,   30 },       /* MCS 0 */
63         {  26,  29,   54,   60 },       /* MCS 1 */
64         {  39,  43,   81,   90 },       /* MCS 2 */
65         {  52,  58,  108,  120 },       /* MCS 3 */
66         {  78,  87,  162,  180 },       /* MCS 4 */
67         { 104, 116,  216,  240 },       /* MCS 5 */
68         { 117, 130,  243,  270 },       /* MCS 6 */
69         { 130, 144,  270,  300 },       /* MCS 7 */
70         {  26,  29,   54,   60 },       /* MCS 8 */
71         {  52,  58,  108,  120 },       /* MCS 9 */
72         {  78,  87,  162,  180 },       /* MCS 10 */
73         { 104, 116,  216,  240 },       /* MCS 11 */
74         { 156, 173,  324,  360 },       /* MCS 12 */
75         { 208, 231,  432,  480 },       /* MCS 13 */
76         { 234, 260,  486,  540 },       /* MCS 14 */
77         { 260, 289,  540,  600 },       /* MCS 15 */
78         {  39,  43,   81,   90 },       /* MCS 16 */
79         {  78,  87,  162,  180 },       /* MCS 17 */
80         { 117, 130,  243,  270 },       /* MCS 18 */
81         { 156, 173,  324,  360 },       /* MCS 19 */
82         { 234, 260,  486,  540 },       /* MCS 20 */
83         { 312, 347,  648,  720 },       /* MCS 21 */
84         { 351, 390,  729,  810 },       /* MCS 22 */
85         { 390, 433,  810,  900 },       /* MCS 23 */
86         {  52,  58,  108,  120 },       /* MCS 24 */
87         { 104, 116,  216,  240 },       /* MCS 25 */
88         { 156, 173,  324,  360 },       /* MCS 26 */
89         { 208, 231,  432,  480 },       /* MCS 27 */
90         { 312, 347,  648,  720 },       /* MCS 28 */
91         { 416, 462,  864,  960 },       /* MCS 29 */
92         { 468, 520,  972, 1080 },       /* MCS 30 */
93         { 520, 578, 1080, 1200 },       /* MCS 31 */
94         {   0,   0,   12,   13 },       /* MCS 32 */
95         {  78,  87,  162,  180 },       /* MCS 33 */
96         { 104, 116,  216,  240 },       /* MCS 34 */
97         { 130, 144,  270,  300 },       /* MCS 35 */
98         { 117, 130,  243,  270 },       /* MCS 36 */
99         { 156, 173,  324,  360 },       /* MCS 37 */
100         { 195, 217,  405,  450 },       /* MCS 38 */
101         { 104, 116,  216,  240 },       /* MCS 39 */
102         { 130, 144,  270,  300 },       /* MCS 40 */
103         { 130, 144,  270,  300 },       /* MCS 41 */
104         { 156, 173,  324,  360 },       /* MCS 42 */
105         { 182, 202,  378,  420 },       /* MCS 43 */
106         { 182, 202,  378,  420 },       /* MCS 44 */
107         { 208, 231,  432,  480 },       /* MCS 45 */
108         { 156, 173,  324,  360 },       /* MCS 46 */
109         { 195, 217,  405,  450 },       /* MCS 47 */
110         { 195, 217,  405,  450 },       /* MCS 48 */
111         { 234, 260,  486,  540 },       /* MCS 49 */
112         { 273, 303,  567,  630 },       /* MCS 50 */
113         { 273, 303,  567,  630 },       /* MCS 51 */
114         { 312, 347,  648,  720 },       /* MCS 52 */
115         { 130, 144,  270,  300 },       /* MCS 53 */
116         { 156, 173,  324,  360 },       /* MCS 54 */
117         { 182, 202,  378,  420 },       /* MCS 55 */
118         { 156, 173,  324,  360 },       /* MCS 56 */
119         { 182, 202,  378,  420 },       /* MCS 57 */
120         { 208, 231,  432,  480 },       /* MCS 58 */
121         { 234, 260,  486,  540 },       /* MCS 59 */
122         { 208, 231,  432,  480 },       /* MCS 60 */
123         { 234, 260,  486,  540 },       /* MCS 61 */
124         { 260, 289,  540,  600 },       /* MCS 62 */
125         { 260, 289,  540,  600 },       /* MCS 63 */
126         { 286, 318,  594,  660 },       /* MCS 64 */
127         { 195, 217,  405,  450 },       /* MCS 65 */
128         { 234, 260,  486,  540 },       /* MCS 66 */
129         { 273, 303,  567,  630 },       /* MCS 67 */
130         { 234, 260,  486,  540 },       /* MCS 68 */
131         { 273, 303,  567,  630 },       /* MCS 69 */
132         { 312, 347,  648,  720 },       /* MCS 70 */
133         { 351, 390,  729,  810 },       /* MCS 71 */
134         { 312, 347,  648,  720 },       /* MCS 72 */
135         { 351, 390,  729,  810 },       /* MCS 73 */
136         { 390, 433,  810,  900 },       /* MCS 74 */
137         { 390, 433,  810,  900 },       /* MCS 75 */
138         { 429, 477,  891,  990 },       /* MCS 76 */
139 };
140
141 static  int ieee80211_ampdu_age = -1;   /* threshold for ampdu reorder q (ms) */
142 SYSCTL_PROC(_net_wlan, OID_AUTO, ampdu_age,
143     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
144     &ieee80211_ampdu_age, 0, ieee80211_sysctl_msecs_ticks, "I",
145     "AMPDU max reorder age (ms)");
146
147 static  int ieee80211_recv_bar_ena = 1;
148 SYSCTL_INT(_net_wlan, OID_AUTO, recv_bar, CTLFLAG_RW, &ieee80211_recv_bar_ena,
149             0, "BAR frame processing (ena/dis)");
150
151 static  int ieee80211_addba_timeout = -1;/* timeout for ADDBA response */
152 SYSCTL_PROC(_net_wlan, OID_AUTO, addba_timeout,
153     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
154     &ieee80211_addba_timeout, 0, ieee80211_sysctl_msecs_ticks, "I",
155     "ADDBA request timeout (ms)");
156 static  int ieee80211_addba_backoff = -1;/* backoff after max ADDBA requests */
157 SYSCTL_PROC(_net_wlan, OID_AUTO, addba_backoff,
158     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
159     &ieee80211_addba_backoff, 0, ieee80211_sysctl_msecs_ticks, "I",
160     "ADDBA request backoff (ms)");
161 static  int ieee80211_addba_maxtries = 3;/* max ADDBA requests before backoff */
162 SYSCTL_INT(_net_wlan, OID_AUTO, addba_maxtries, CTLFLAG_RW,
163         &ieee80211_addba_maxtries, 0, "max ADDBA requests sent before backoff");
164
165 static  int ieee80211_bar_timeout = -1; /* timeout waiting for BAR response */
166 static  int ieee80211_bar_maxtries = 50;/* max BAR requests before DELBA */
167
168 static  ieee80211_recv_action_func ht_recv_action_ba_addba_request;
169 static  ieee80211_recv_action_func ht_recv_action_ba_addba_response;
170 static  ieee80211_recv_action_func ht_recv_action_ba_delba;
171 static  ieee80211_recv_action_func ht_recv_action_ht_mimopwrsave;
172 static  ieee80211_recv_action_func ht_recv_action_ht_txchwidth;
173
174 static  ieee80211_send_action_func ht_send_action_ba_addba;
175 static  ieee80211_send_action_func ht_send_action_ba_delba;
176 static  ieee80211_send_action_func ht_send_action_ht_txchwidth;
177
178 static void
179 ieee80211_ht_init(void)
180 {
181         /*
182          * Setup HT parameters that depends on the clock frequency.
183          */
184         ieee80211_ampdu_age = msecs_to_ticks(500);
185         ieee80211_addba_timeout = msecs_to_ticks(250);
186         ieee80211_addba_backoff = msecs_to_ticks(10*1000);
187         ieee80211_bar_timeout = msecs_to_ticks(250);
188         /*
189          * Register action frame handlers.
190          */
191         ieee80211_recv_action_register(IEEE80211_ACTION_CAT_BA, 
192             IEEE80211_ACTION_BA_ADDBA_REQUEST, ht_recv_action_ba_addba_request);
193         ieee80211_recv_action_register(IEEE80211_ACTION_CAT_BA, 
194             IEEE80211_ACTION_BA_ADDBA_RESPONSE, ht_recv_action_ba_addba_response);
195         ieee80211_recv_action_register(IEEE80211_ACTION_CAT_BA, 
196             IEEE80211_ACTION_BA_DELBA, ht_recv_action_ba_delba);
197         ieee80211_recv_action_register(IEEE80211_ACTION_CAT_HT, 
198             IEEE80211_ACTION_HT_MIMOPWRSAVE, ht_recv_action_ht_mimopwrsave);
199         ieee80211_recv_action_register(IEEE80211_ACTION_CAT_HT, 
200             IEEE80211_ACTION_HT_TXCHWIDTH, ht_recv_action_ht_txchwidth);
201
202         ieee80211_send_action_register(IEEE80211_ACTION_CAT_BA, 
203             IEEE80211_ACTION_BA_ADDBA_REQUEST, ht_send_action_ba_addba);
204         ieee80211_send_action_register(IEEE80211_ACTION_CAT_BA, 
205             IEEE80211_ACTION_BA_ADDBA_RESPONSE, ht_send_action_ba_addba);
206         ieee80211_send_action_register(IEEE80211_ACTION_CAT_BA, 
207             IEEE80211_ACTION_BA_DELBA, ht_send_action_ba_delba);
208         ieee80211_send_action_register(IEEE80211_ACTION_CAT_HT, 
209             IEEE80211_ACTION_HT_TXCHWIDTH, ht_send_action_ht_txchwidth);
210 }
211 SYSINIT(wlan_ht, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_ht_init, NULL);
212
213 static int ieee80211_ampdu_enable(struct ieee80211_node *ni,
214         struct ieee80211_tx_ampdu *tap);
215 static int ieee80211_addba_request(struct ieee80211_node *ni,
216         struct ieee80211_tx_ampdu *tap,
217         int dialogtoken, int baparamset, int batimeout);
218 static int ieee80211_addba_response(struct ieee80211_node *ni,
219         struct ieee80211_tx_ampdu *tap,
220         int code, int baparamset, int batimeout);
221 static void ieee80211_addba_stop(struct ieee80211_node *ni,
222         struct ieee80211_tx_ampdu *tap);
223 static void null_addba_response_timeout(struct ieee80211_node *ni,
224         struct ieee80211_tx_ampdu *tap);
225
226 static void ieee80211_bar_response(struct ieee80211_node *ni,
227         struct ieee80211_tx_ampdu *tap, int status);
228 static void ampdu_tx_stop(struct ieee80211_tx_ampdu *tap);
229 static void bar_stop_timer(struct ieee80211_tx_ampdu *tap);
230 static int ampdu_rx_start(struct ieee80211_node *, struct ieee80211_rx_ampdu *,
231         int baparamset, int batimeout, int baseqctl);
232 static void ampdu_rx_stop(struct ieee80211_node *, struct ieee80211_rx_ampdu *);
233
234 void
235 ieee80211_ht_attach(struct ieee80211com *ic)
236 {
237         /* setup default aggregation policy */
238         ic->ic_recv_action = ieee80211_recv_action;
239         ic->ic_send_action = ieee80211_send_action;
240         ic->ic_ampdu_enable = ieee80211_ampdu_enable;
241         ic->ic_addba_request = ieee80211_addba_request;
242         ic->ic_addba_response = ieee80211_addba_response;
243         ic->ic_addba_response_timeout = null_addba_response_timeout;
244         ic->ic_addba_stop = ieee80211_addba_stop;
245         ic->ic_bar_response = ieee80211_bar_response;
246         ic->ic_ampdu_rx_start = ampdu_rx_start;
247         ic->ic_ampdu_rx_stop = ampdu_rx_stop;
248
249         ic->ic_htprotmode = IEEE80211_PROT_RTSCTS;
250         ic->ic_curhtprotmode = IEEE80211_HTINFO_OPMODE_PURE;
251 }
252
253 void
254 ieee80211_ht_detach(struct ieee80211com *ic)
255 {
256 }
257
258 void
259 ieee80211_ht_vattach(struct ieee80211vap *vap)
260 {
261
262         /* driver can override defaults */
263         vap->iv_ampdu_rxmax = IEEE80211_HTCAP_MAXRXAMPDU_8K;
264         vap->iv_ampdu_density = IEEE80211_HTCAP_MPDUDENSITY_NA;
265         vap->iv_ampdu_limit = vap->iv_ampdu_rxmax;
266         vap->iv_amsdu_limit = vap->iv_htcaps & IEEE80211_HTCAP_MAXAMSDU;
267         /* tx aggregation traffic thresholds */
268         vap->iv_ampdu_mintraffic[WME_AC_BK] = 128;
269         vap->iv_ampdu_mintraffic[WME_AC_BE] = 64;
270         vap->iv_ampdu_mintraffic[WME_AC_VO] = 32;
271         vap->iv_ampdu_mintraffic[WME_AC_VI] = 32;
272
273         if (vap->iv_htcaps & IEEE80211_HTC_HT) {
274                 /*
275                  * Device is HT capable; enable all HT-related
276                  * facilities by default.
277                  * XXX these choices may be too aggressive.
278                  */
279                 vap->iv_flags_ht |= IEEE80211_FHT_HT
280                                  |  IEEE80211_FHT_HTCOMPAT
281                                  ;
282                 if (vap->iv_htcaps & IEEE80211_HTCAP_SHORTGI20)
283                         vap->iv_flags_ht |= IEEE80211_FHT_SHORTGI20;
284                 /* XXX infer from channel list? */
285                 if (vap->iv_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
286                         vap->iv_flags_ht |= IEEE80211_FHT_USEHT40;
287                         if (vap->iv_htcaps & IEEE80211_HTCAP_SHORTGI40)
288                                 vap->iv_flags_ht |= IEEE80211_FHT_SHORTGI40;
289                 }
290                 /* enable RIFS if capable */
291                 if (vap->iv_htcaps & IEEE80211_HTC_RIFS)
292                         vap->iv_flags_ht |= IEEE80211_FHT_RIFS;
293
294                 /* NB: A-MPDU and A-MSDU rx are mandated, these are tx only */
295                 vap->iv_flags_ht |= IEEE80211_FHT_AMPDU_RX;
296                 if (vap->iv_htcaps & IEEE80211_HTC_AMPDU)
297                         vap->iv_flags_ht |= IEEE80211_FHT_AMPDU_TX;
298                 vap->iv_flags_ht |= IEEE80211_FHT_AMSDU_RX;
299                 if (vap->iv_htcaps & IEEE80211_HTC_AMSDU)
300                         vap->iv_flags_ht |= IEEE80211_FHT_AMSDU_TX;
301
302                 if (vap->iv_htcaps & IEEE80211_HTCAP_TXSTBC)
303                         vap->iv_flags_ht |= IEEE80211_FHT_STBC_TX;
304                 if (vap->iv_htcaps & IEEE80211_HTCAP_RXSTBC)
305                         vap->iv_flags_ht |= IEEE80211_FHT_STBC_RX;
306
307                 if (vap->iv_htcaps & IEEE80211_HTCAP_LDPC)
308                         vap->iv_flags_ht |= IEEE80211_FHT_LDPC_RX;
309                 if (vap->iv_htcaps & IEEE80211_HTC_TXLDPC)
310                         vap->iv_flags_ht |= IEEE80211_FHT_LDPC_TX;
311         }
312         /* NB: disable default legacy WDS, too many issues right now */
313         if (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY)
314                 vap->iv_flags_ht &= ~IEEE80211_FHT_HT;
315 }
316
317 void
318 ieee80211_ht_vdetach(struct ieee80211vap *vap)
319 {
320 }
321
322 static int
323 ht_getrate(struct ieee80211com *ic, int index, enum ieee80211_phymode mode,
324     int ratetype)
325 {
326         int mword, rate;
327
328         mword = ieee80211_rate2media(ic, index | IEEE80211_RATE_MCS, mode);
329         if (IFM_SUBTYPE(mword) != IFM_IEEE80211_MCS)
330                 return (0);
331         switch (ratetype) {
332         case 0:
333                 rate = ieee80211_htrates[index].ht20_rate_800ns;
334                 break;
335         case 1:
336                 rate = ieee80211_htrates[index].ht20_rate_400ns;
337                 break;
338         case 2:
339                 rate = ieee80211_htrates[index].ht40_rate_800ns;
340                 break;
341         default:
342                 rate = ieee80211_htrates[index].ht40_rate_400ns;
343                 break;
344         }
345         return (rate);
346 }
347
348 static struct printranges {
349         int     minmcs;
350         int     maxmcs;
351         int     txstream;
352         int     ratetype;
353         int     htcapflags;
354 } ranges[] = {
355         {  0,  7, 1, 0, 0 },
356         {  8, 15, 2, 0, 0 },
357         { 16, 23, 3, 0, 0 },
358         { 24, 31, 4, 0, 0 },
359         { 32,  0, 1, 2, IEEE80211_HTC_TXMCS32 },
360         { 33, 38, 2, 0, IEEE80211_HTC_TXUNEQUAL },
361         { 39, 52, 3, 0, IEEE80211_HTC_TXUNEQUAL },
362         { 53, 76, 4, 0, IEEE80211_HTC_TXUNEQUAL },
363         {  0,  0, 0, 0, 0 },
364 };
365
366 static void
367 ht_rateprint(struct ieee80211com *ic, enum ieee80211_phymode mode, int ratetype)
368 {
369         int minrate, maxrate;
370         struct printranges *range;
371
372         for (range = ranges; range->txstream != 0; range++) {
373                 if (ic->ic_txstream < range->txstream)
374                         continue;
375                 if (range->htcapflags &&
376                     (ic->ic_htcaps & range->htcapflags) == 0)
377                         continue;
378                 if (ratetype < range->ratetype)
379                         continue;
380                 minrate = ht_getrate(ic, range->minmcs, mode, ratetype);
381                 maxrate = ht_getrate(ic, range->maxmcs, mode, ratetype);
382                 if (range->maxmcs) {
383                         ic_printf(ic, "MCS %d-%d: %d%sMbps - %d%sMbps\n",
384                             range->minmcs, range->maxmcs,
385                             minrate/2, ((minrate & 0x1) != 0 ? ".5" : ""),
386                             maxrate/2, ((maxrate & 0x1) != 0 ? ".5" : ""));
387                 } else {
388                         ic_printf(ic, "MCS %d: %d%sMbps\n", range->minmcs,
389                             minrate/2, ((minrate & 0x1) != 0 ? ".5" : ""));
390                 }
391         }
392 }
393
394 static void
395 ht_announce(struct ieee80211com *ic, enum ieee80211_phymode mode)
396 {
397         const char *modestr = ieee80211_phymode_name[mode];
398
399         ic_printf(ic, "%s MCS 20MHz\n", modestr);
400         ht_rateprint(ic, mode, 0);
401         if (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20) {
402                 ic_printf(ic, "%s MCS 20MHz SGI\n", modestr);
403                 ht_rateprint(ic, mode, 1);
404         }
405         if (ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
406                 ic_printf(ic, "%s MCS 40MHz:\n", modestr);
407                 ht_rateprint(ic, mode, 2);
408         }
409         if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
410             (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40)) {
411                 ic_printf(ic, "%s MCS 40MHz SGI:\n", modestr);
412                 ht_rateprint(ic, mode, 3);
413         }
414 }
415
416 void
417 ieee80211_ht_announce(struct ieee80211com *ic)
418 {
419
420         if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) ||
421             isset(ic->ic_modecaps, IEEE80211_MODE_11NG))
422                 ic_printf(ic, "%dT%dR\n", ic->ic_txstream, ic->ic_rxstream);
423         if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA))
424                 ht_announce(ic, IEEE80211_MODE_11NA);
425         if (isset(ic->ic_modecaps, IEEE80211_MODE_11NG))
426                 ht_announce(ic, IEEE80211_MODE_11NG);
427 }
428
429 void
430 ieee80211_init_suphtrates(struct ieee80211com *ic)
431 {
432 #define ADDRATE(x)      do {                                            \
433         htrateset->rs_rates[htrateset->rs_nrates] = x;                  \
434         htrateset->rs_nrates++;                                         \
435 } while (0)
436         struct ieee80211_htrateset *htrateset = &ic->ic_sup_htrates;
437         int i;
438
439         memset(htrateset, 0, sizeof(struct ieee80211_htrateset));
440         for (i = 0; i < ic->ic_txstream * 8; i++)
441                 ADDRATE(i);
442         if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
443             (ic->ic_htcaps & IEEE80211_HTC_TXMCS32))
444                 ADDRATE(32);
445         if (ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL) {
446                 if (ic->ic_txstream >= 2) {
447                          for (i = 33; i <= 38; i++)
448                                 ADDRATE(i);
449                 }
450                 if (ic->ic_txstream >= 3) {
451                         for (i = 39; i <= 52; i++)
452                                 ADDRATE(i);
453                 }
454                 if (ic->ic_txstream == 4) {
455                         for (i = 53; i <= 76; i++)
456                                 ADDRATE(i);
457                 }
458         }
459 #undef  ADDRATE
460 }
461
462 /*
463  * Receive processing.
464  */
465
466 /*
467  * Decap the encapsulated A-MSDU frames and dispatch all but
468  * the last for delivery.  The last frame is returned for 
469  * delivery via the normal path.
470  */
471 struct mbuf *
472 ieee80211_decap_amsdu(struct ieee80211_node *ni, struct mbuf *m)
473 {
474         struct ieee80211vap *vap = ni->ni_vap;
475         int framelen;
476         struct mbuf *n;
477
478         /* discard 802.3 header inserted by ieee80211_decap */
479         m_adj(m, sizeof(struct ether_header));
480
481         vap->iv_stats.is_amsdu_decap++;
482
483         for (;;) {
484                 /*
485                  * Decap the first frame, bust it apart from the
486                  * remainder and deliver.  We leave the last frame
487                  * delivery to the caller (for consistency with other
488                  * code paths, could also do it here).
489                  */
490                 m = ieee80211_decap1(m, &framelen);
491                 if (m == NULL) {
492                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
493                             ni->ni_macaddr, "a-msdu", "%s", "decap failed");
494                         vap->iv_stats.is_amsdu_tooshort++;
495                         return NULL;
496                 }
497                 if (m->m_pkthdr.len == framelen)
498                         break;
499                 n = m_split(m, framelen, M_NOWAIT);
500                 if (n == NULL) {
501                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
502                             ni->ni_macaddr, "a-msdu",
503                             "%s", "unable to split encapsulated frames");
504                         vap->iv_stats.is_amsdu_split++;
505                         m_freem(m);                     /* NB: must reclaim */
506                         return NULL;
507                 }
508                 vap->iv_deliver_data(vap, ni, m);
509
510                 /*
511                  * Remove frame contents; each intermediate frame
512                  * is required to be aligned to a 4-byte boundary.
513                  */
514                 m = n;
515                 m_adj(m, roundup2(framelen, 4) - framelen);     /* padding */
516         }
517         return m;                               /* last delivered by caller */
518 }
519
520 /*
521  * Add the given frame to the current RX reorder slot.
522  *
523  * For future offloaded A-MSDU handling where multiple frames with
524  * the same sequence number show up here, this routine will append
525  * those frames as long as they're appropriately tagged.
526  */
527 static int
528 ampdu_rx_add_slot(struct ieee80211_rx_ampdu *rap, int off, int tid,
529     ieee80211_seq rxseq,
530     struct ieee80211_node *ni,
531     struct mbuf *m)
532 {
533         struct ieee80211vap *vap = ni->ni_vap;
534
535         if (rap->rxa_m[off] == NULL) {
536                 rap->rxa_m[off] = m;
537                 rap->rxa_qframes++;
538                 rap->rxa_qbytes += m->m_pkthdr.len;
539                 vap->iv_stats.is_ampdu_rx_reorder++;
540                 return (0);
541         } else {
542                 IEEE80211_DISCARD_MAC(vap,
543                     IEEE80211_MSG_INPUT | IEEE80211_MSG_11N,
544                     ni->ni_macaddr, "a-mpdu duplicate",
545                     "seqno %u tid %u BA win <%u:%u>",
546                     rxseq, tid, rap->rxa_start,
547                     IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1));
548                 vap->iv_stats.is_rx_dup++;
549                 IEEE80211_NODE_STAT(ni, rx_dup);
550                 m_freem(m);
551                 return (-1);
552         }
553 }
554
555 static void
556 ampdu_rx_purge_slot(struct ieee80211_rx_ampdu *rap, int i)
557 {
558         struct mbuf *m;
559
560         m = rap->rxa_m[i];
561         if (m == NULL)
562                 return;
563
564         rap->rxa_m[i] = NULL;
565         rap->rxa_qbytes -= m->m_pkthdr.len;
566         rap->rxa_qframes--;
567         m_freem(m);
568 }
569
570 /*
571  * Purge all frames in the A-MPDU re-order queue.
572  */
573 static void
574 ampdu_rx_purge(struct ieee80211_rx_ampdu *rap)
575 {
576         int i;
577
578         for (i = 0; i < rap->rxa_wnd; i++) {
579                 ampdu_rx_purge_slot(rap, i);
580                 if (rap->rxa_qframes == 0)
581                         break;
582         }
583         KASSERT(rap->rxa_qbytes == 0 && rap->rxa_qframes == 0,
584             ("lost %u data, %u frames on ampdu rx q",
585             rap->rxa_qbytes, rap->rxa_qframes));
586 }
587
588 /*
589  * Start A-MPDU rx/re-order processing for the specified TID.
590  */
591 static int
592 ampdu_rx_start(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap,
593         int baparamset, int batimeout, int baseqctl)
594 {
595         struct ieee80211vap *vap = ni->ni_vap;
596         int bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
597
598         if (rap->rxa_flags & IEEE80211_AGGR_RUNNING) {
599                 /*
600                  * AMPDU previously setup and not terminated with a DELBA,
601                  * flush the reorder q's in case anything remains.
602                  */
603                 ampdu_rx_purge(rap);
604         }
605         memset(rap, 0, sizeof(*rap));
606         rap->rxa_wnd = (bufsiz == 0) ?
607             IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
608         rap->rxa_start = MS(baseqctl, IEEE80211_BASEQ_START);
609         rap->rxa_flags |=  IEEE80211_AGGR_RUNNING | IEEE80211_AGGR_XCHGPEND;
610
611         /* XXX this should be a configuration flag */
612         if ((vap->iv_htcaps & IEEE80211_HTC_RX_AMSDU_AMPDU) &&
613             (MS(baparamset, IEEE80211_BAPS_AMSDU)))
614                 rap->rxa_flags |= IEEE80211_AGGR_AMSDU;
615         else
616                 rap->rxa_flags &= ~IEEE80211_AGGR_AMSDU;
617
618         return 0;
619 }
620
621 /*
622  * Public function; manually setup the RX ampdu state.
623  */
624 int
625 ieee80211_ampdu_rx_start_ext(struct ieee80211_node *ni, int tid, int seq, int baw)
626 {
627         struct ieee80211_rx_ampdu *rap;
628
629         /* XXX TODO: sanity check tid, seq, baw */
630
631         rap = &ni->ni_rx_ampdu[tid];
632
633         if (rap->rxa_flags & IEEE80211_AGGR_RUNNING) {
634                 /*
635                  * AMPDU previously setup and not terminated with a DELBA,
636                  * flush the reorder q's in case anything remains.
637                  */
638                 ampdu_rx_purge(rap);
639         }
640
641         memset(rap, 0, sizeof(*rap));
642         rap->rxa_wnd = (baw== 0) ?
643             IEEE80211_AGGR_BAWMAX : min(baw, IEEE80211_AGGR_BAWMAX);
644         if (seq == -1) {
645                 /* Wait for the first RX frame, use that as BAW */
646                 rap->rxa_start = 0;
647                 rap->rxa_flags |= IEEE80211_AGGR_WAITRX;
648         } else {
649                 rap->rxa_start = seq;
650         }
651         rap->rxa_flags |=  IEEE80211_AGGR_RUNNING | IEEE80211_AGGR_XCHGPEND;
652
653         /* XXX TODO: no amsdu flag */
654
655         IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, ni,
656             "%s: tid=%d, start=%d, wnd=%d, flags=0x%08x",
657             __func__,
658             tid,
659             seq,
660             rap->rxa_wnd,
661             rap->rxa_flags);
662
663         return 0;
664 }
665
666 /*
667  * Public function; manually stop the RX AMPDU state.
668  */
669 void
670 ieee80211_ampdu_rx_stop_ext(struct ieee80211_node *ni, int tid)
671 {
672         struct ieee80211_rx_ampdu *rap;
673
674         /* XXX TODO: sanity check tid, seq, baw */
675         rap = &ni->ni_rx_ampdu[tid];
676         ampdu_rx_stop(ni, rap);
677 }
678
679 /*
680  * Stop A-MPDU rx processing for the specified TID.
681  */
682 static void
683 ampdu_rx_stop(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
684 {
685
686         ampdu_rx_purge(rap);
687         rap->rxa_flags &= ~(IEEE80211_AGGR_RUNNING
688             | IEEE80211_AGGR_XCHGPEND
689             | IEEE80211_AGGR_WAITRX);
690 }
691
692 /*
693  * Dispatch a frame from the A-MPDU reorder queue.  The
694  * frame is fed back into ieee80211_input marked with an
695  * M_AMPDU_MPDU flag so it doesn't come back to us (it also
696  * permits ieee80211_input to optimize re-processing).
697  */
698 static __inline void
699 ampdu_dispatch(struct ieee80211_node *ni, struct mbuf *m)
700 {
701         m->m_flags |= M_AMPDU_MPDU;     /* bypass normal processing */
702         /* NB: rssi and noise are ignored w/ M_AMPDU_MPDU set */
703         (void) ieee80211_input(ni, m, 0, 0);
704 }
705
706 static int
707 ampdu_dispatch_slot(struct ieee80211_rx_ampdu *rap, struct ieee80211_node *ni,
708     int i)
709 {
710         struct mbuf *m;
711
712         if (rap->rxa_m[i] == NULL)
713                 return (0);
714
715         m = rap->rxa_m[i];
716         rap->rxa_m[i] = NULL;
717         rap->rxa_qbytes -= m->m_pkthdr.len;
718         rap->rxa_qframes--;
719
720         ampdu_dispatch(ni, m);
721
722         return (1);
723 }
724
725 static void
726 ampdu_rx_moveup(struct ieee80211_rx_ampdu *rap, struct ieee80211_node *ni,
727     int i, int winstart)
728 {
729         struct ieee80211vap *vap = ni->ni_vap;
730
731         if (rap->rxa_qframes != 0) {
732                 int n = rap->rxa_qframes, j;
733
734                 if (winstart != -1) {
735                         /*
736                          * NB: in window-sliding mode, loop assumes i > 0
737                          * and/or rxa_m[0] is NULL
738                          */
739                         KASSERT(rap->rxa_m[0] == NULL,
740                             ("%s: BA window slot 0 occupied", __func__));
741                 }
742                 for (j = i+1; j < rap->rxa_wnd; j++) {
743                         if (rap->rxa_m[j] != NULL) {
744                                 rap->rxa_m[j-i] = rap->rxa_m[j];
745                                 rap->rxa_m[j] = NULL;
746                                 if (--n == 0)
747                                         break;
748                         }
749                 }
750                 KASSERT(n == 0, ("%s: lost %d frames, qframes %d off %d "
751                     "BA win <%d:%d> winstart %d",
752                     __func__, n, rap->rxa_qframes, i, rap->rxa_start,
753                     IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
754                     winstart));
755                 vap->iv_stats.is_ampdu_rx_copy += rap->rxa_qframes;
756         }
757 }
758
759 /*
760  * Dispatch as many frames as possible from the re-order queue.
761  * Frames will always be "at the front"; we process all frames
762  * up to the first empty slot in the window.  On completion we
763  * cleanup state if there are still pending frames in the current
764  * BA window.  We assume the frame at slot 0 is already handled
765  * by the caller; we always start at slot 1.
766  */
767 static void
768 ampdu_rx_dispatch(struct ieee80211_rx_ampdu *rap, struct ieee80211_node *ni)
769 {
770         struct ieee80211vap *vap = ni->ni_vap;
771         int i;
772
773         /* flush run of frames */
774         for (i = 1; i < rap->rxa_wnd; i++) {
775                 if (ampdu_dispatch_slot(rap, ni, i) == 0)
776                         break;
777         }
778
779         /*
780          * If frames remain, copy the mbuf pointers down so
781          * they correspond to the offsets in the new window.
782          */
783         ampdu_rx_moveup(rap, ni, i, -1);
784
785         /*
786          * Adjust the start of the BA window to
787          * reflect the frames just dispatched.
788          */
789         rap->rxa_start = IEEE80211_SEQ_ADD(rap->rxa_start, i);
790         vap->iv_stats.is_ampdu_rx_oor += i;
791 }
792
793 /*
794  * Dispatch all frames in the A-MPDU re-order queue.
795  */
796 static void
797 ampdu_rx_flush(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
798 {
799         struct ieee80211vap *vap = ni->ni_vap;
800         int i, r;
801
802         for (i = 0; i < rap->rxa_wnd; i++) {
803                 r = ampdu_dispatch_slot(rap, ni, i);
804                 if (r == 0)
805                         continue;
806                 vap->iv_stats.is_ampdu_rx_oor += r;
807
808                 if (rap->rxa_qframes == 0)
809                         break;
810         }
811 }
812
813 /*
814  * Dispatch all frames in the A-MPDU re-order queue
815  * preceding the specified sequence number.  This logic
816  * handles window moves due to a received MSDU or BAR.
817  */
818 static void
819 ampdu_rx_flush_upto(struct ieee80211_node *ni,
820         struct ieee80211_rx_ampdu *rap, ieee80211_seq winstart)
821 {
822         struct ieee80211vap *vap = ni->ni_vap;
823         ieee80211_seq seqno;
824         int i, r;
825
826         /*
827          * Flush any complete MSDU's with a sequence number lower
828          * than winstart.  Gaps may exist.  Note that we may actually
829          * dispatch frames past winstart if a run continues; this is
830          * an optimization that avoids having to do a separate pass
831          * to dispatch frames after moving the BA window start.
832          */
833         seqno = rap->rxa_start;
834         for (i = 0; i < rap->rxa_wnd; i++) {
835                 r = ampdu_dispatch_slot(rap, ni, i);
836                 if (r == 0) {
837                         if (!IEEE80211_SEQ_BA_BEFORE(seqno, winstart))
838                                 break;
839                 }
840                 vap->iv_stats.is_ampdu_rx_oor += r;
841                 seqno = IEEE80211_SEQ_INC(seqno);
842         }
843         /*
844          * If frames remain, copy the mbuf pointers down so
845          * they correspond to the offsets in the new window.
846          */
847         ampdu_rx_moveup(rap, ni, i, winstart);
848
849         /*
850          * Move the start of the BA window; we use the
851          * sequence number of the last MSDU that was
852          * passed up the stack+1 or winstart if stopped on
853          * a gap in the reorder buffer.
854          */
855         rap->rxa_start = seqno;
856 }
857
858 /*
859  * Process a received QoS data frame for an HT station.  Handle
860  * A-MPDU reordering: if this frame is received out of order
861  * and falls within the BA window hold onto it.  Otherwise if
862  * this frame completes a run, flush any pending frames.  We
863  * return 1 if the frame is consumed.  A 0 is returned if
864  * the frame should be processed normally by the caller.
865  */
866 int
867 ieee80211_ampdu_reorder(struct ieee80211_node *ni, struct mbuf *m,
868     const struct ieee80211_rx_stats *rxs)
869 {
870 #define PROCESS         0       /* caller should process frame */
871 #define CONSUMED        1       /* frame consumed, caller does nothing */
872         struct ieee80211vap *vap = ni->ni_vap;
873         struct ieee80211_qosframe *wh;
874         struct ieee80211_rx_ampdu *rap;
875         ieee80211_seq rxseq;
876         uint8_t tid;
877         int off;
878         int amsdu_more = ieee80211_check_rxseq_amsdu_more(rxs);
879
880         KASSERT((m->m_flags & (M_AMPDU | M_AMPDU_MPDU)) == M_AMPDU,
881             ("!a-mpdu or already re-ordered, flags 0x%x", m->m_flags));
882         KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT sta"));
883
884         /* NB: m_len known to be sufficient */
885         wh = mtod(m, struct ieee80211_qosframe *);
886         if (wh->i_fc[0] != IEEE80211_FC0_QOSDATA) {
887                 /*
888                  * Not QoS data, shouldn't get here but just
889                  * return it to the caller for processing.
890                  */
891                 return PROCESS;
892         }
893
894         /*
895          * 802.11-2012 9.3.2.10 - Duplicate detection and recovery.
896          *
897          * Multicast QoS data frames are checked against a different
898          * counter, not the per-TID counter.
899          */
900         if (IEEE80211_IS_MULTICAST(wh->i_addr1))
901                 return PROCESS;
902
903         tid = ieee80211_getqos(wh)[0];
904         tid &= IEEE80211_QOS_TID;
905         rap = &ni->ni_rx_ampdu[tid];
906         if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
907                 /*
908                  * No ADDBA request yet, don't touch.
909                  */
910                 return PROCESS;
911         }
912         rxseq = le16toh(*(uint16_t *)wh->i_seq);
913         if ((rxseq & IEEE80211_SEQ_FRAG_MASK) != 0) {
914                 /*
915                  * Fragments are not allowed; toss.
916                  */
917                 IEEE80211_DISCARD_MAC(vap,
918                     IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
919                     "A-MPDU", "fragment, rxseq 0x%x tid %u%s", rxseq, tid,
920                     wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
921                 vap->iv_stats.is_ampdu_rx_drop++;
922                 IEEE80211_NODE_STAT(ni, rx_drop);
923                 m_freem(m);
924                 return CONSUMED;
925         }
926         rxseq >>= IEEE80211_SEQ_SEQ_SHIFT;
927         rap->rxa_nframes++;
928
929         /*
930          * Handle waiting for the first frame to define the BAW.
931          * Some firmware doesn't provide the RX of the starting point
932          * of the BAW and we have to cope.
933          */
934         if (rap->rxa_flags & IEEE80211_AGGR_WAITRX) {
935                 rap->rxa_flags &= ~IEEE80211_AGGR_WAITRX;
936                 rap->rxa_start = rxseq;
937         }
938 again:
939         if (rxseq == rap->rxa_start) {
940                 /*
941                  * First frame in window.
942                  */
943                 if (rap->rxa_qframes != 0) {
944                         /*
945                          * Dispatch as many packets as we can.
946                          */
947                         KASSERT(rap->rxa_m[0] == NULL, ("unexpected dup"));
948                         ampdu_dispatch(ni, m);
949                         ampdu_rx_dispatch(rap, ni);
950                         return CONSUMED;
951                 } else {
952                         /*
953                          * In order; advance window if needed and notify
954                          * caller to dispatch directly.
955                          */
956                         if (amsdu_more)
957                                 rap->rxa_start = IEEE80211_SEQ_INC(rxseq);
958                         return PROCESS;
959                 }
960         }
961         /*
962          * Frame is out of order; store if in the BA window.
963          */
964         /* calculate offset in BA window */
965         off = IEEE80211_SEQ_SUB(rxseq, rap->rxa_start);
966         if (off < rap->rxa_wnd) {
967                 /*
968                  * Common case (hopefully): in the BA window.
969                  * Sec 9.10.7.6.2 a) (p.137)
970                  */
971
972                 /* 
973                  * Check for frames sitting too long in the reorder queue.
974                  * This should only ever happen if frames are not delivered
975                  * without the sender otherwise notifying us (e.g. with a
976                  * BAR to move the window).  Typically this happens because
977                  * of vendor bugs that cause the sequence number to jump.
978                  * When this happens we get a gap in the reorder queue that
979                  * leaves frame sitting on the queue until they get pushed
980                  * out due to window moves.  When the vendor does not send
981                  * BAR this move only happens due to explicit packet sends
982                  *
983                  * NB: we only track the time of the oldest frame in the
984                  * reorder q; this means that if we flush we might push
985                  * frames that still "new"; if this happens then subsequent
986                  * frames will result in BA window moves which cost something
987                  * but is still better than a big throughput dip.
988                  */
989                 if (rap->rxa_qframes != 0) {
990                         /* XXX honor batimeout? */
991                         if (ticks - rap->rxa_age > ieee80211_ampdu_age) {
992                                 /*
993                                  * Too long since we received the first
994                                  * frame; flush the reorder buffer.
995                                  */
996                                 if (rap->rxa_qframes != 0) {
997                                         vap->iv_stats.is_ampdu_rx_age +=
998                                             rap->rxa_qframes;
999                                         ampdu_rx_flush(ni, rap);
1000                                 }
1001                                 if (amsdu_more)
1002                                         rap->rxa_start = IEEE80211_SEQ_INC(rxseq);
1003                                 return PROCESS;
1004                         }
1005                 } else {
1006                         /*
1007                          * First frame, start aging timer.
1008                          */
1009                         rap->rxa_age = ticks;
1010                 }
1011
1012                 /* save packet - this consumes, no matter what */
1013                 ampdu_rx_add_slot(rap, off, tid, rxseq, ni, m);
1014
1015                 return CONSUMED;
1016         }
1017         if (off < IEEE80211_SEQ_BA_RANGE) {
1018                 /*
1019                  * Outside the BA window, but within range;
1020                  * flush the reorder q and move the window.
1021                  * Sec 9.10.7.6.2 b) (p.138)
1022                  */
1023                 IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
1024                     "move BA win <%u:%u> (%u frames) rxseq %u tid %u",
1025                     rap->rxa_start,
1026                     IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
1027                     rap->rxa_qframes, rxseq, tid);
1028                 vap->iv_stats.is_ampdu_rx_move++;
1029
1030                 /*
1031                  * The spec says to flush frames up to but not including:
1032                  *      WinStart_B = rxseq - rap->rxa_wnd + 1
1033                  * Then insert the frame or notify the caller to process
1034                  * it immediately.  We can safely do this by just starting
1035                  * over again because we know the frame will now be within
1036                  * the BA window.
1037                  */
1038                 /* NB: rxa_wnd known to be >0 */
1039                 ampdu_rx_flush_upto(ni, rap,
1040                     IEEE80211_SEQ_SUB(rxseq, rap->rxa_wnd-1));
1041                 goto again;
1042         } else {
1043                 /*
1044                  * Outside the BA window and out of range; toss.
1045                  * Sec 9.10.7.6.2 c) (p.138)
1046                  */
1047                 IEEE80211_DISCARD_MAC(vap,
1048                     IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
1049                     "MPDU", "BA win <%u:%u> (%u frames) rxseq %u tid %u%s",
1050                     rap->rxa_start,
1051                     IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
1052                     rap->rxa_qframes, rxseq, tid,
1053                     wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
1054                 vap->iv_stats.is_ampdu_rx_drop++;
1055                 IEEE80211_NODE_STAT(ni, rx_drop);
1056                 m_freem(m);
1057                 return CONSUMED;
1058         }
1059 #undef CONSUMED
1060 #undef PROCESS
1061 }
1062
1063 /*
1064  * Process a BAR ctl frame.  Dispatch all frames up to
1065  * the sequence number of the frame.  If this frame is
1066  * out of range it's discarded.
1067  */
1068 void
1069 ieee80211_recv_bar(struct ieee80211_node *ni, struct mbuf *m0)
1070 {
1071         struct ieee80211vap *vap = ni->ni_vap;
1072         struct ieee80211_frame_bar *wh;
1073         struct ieee80211_rx_ampdu *rap;
1074         ieee80211_seq rxseq;
1075         int tid, off;
1076
1077         if (!ieee80211_recv_bar_ena) {
1078 #if 0
1079                 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_11N,
1080                     ni->ni_macaddr, "BAR", "%s", "processing disabled");
1081 #endif
1082                 vap->iv_stats.is_ampdu_bar_bad++;
1083                 return;
1084         }
1085         wh = mtod(m0, struct ieee80211_frame_bar *);
1086         /* XXX check basic BAR */
1087         tid = MS(le16toh(wh->i_ctl), IEEE80211_BAR_TID);
1088         rap = &ni->ni_rx_ampdu[tid];
1089         if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
1090                 /*
1091                  * No ADDBA request yet, don't touch.
1092                  */
1093                 IEEE80211_DISCARD_MAC(vap,
1094                     IEEE80211_MSG_INPUT | IEEE80211_MSG_11N,
1095                     ni->ni_macaddr, "BAR", "no BA stream, tid %u", tid);
1096                 vap->iv_stats.is_ampdu_bar_bad++;
1097                 return;
1098         }
1099         vap->iv_stats.is_ampdu_bar_rx++;
1100         rxseq = le16toh(wh->i_seq) >> IEEE80211_SEQ_SEQ_SHIFT;
1101         if (rxseq == rap->rxa_start)
1102                 return;
1103         /* calculate offset in BA window */
1104         off = IEEE80211_SEQ_SUB(rxseq, rap->rxa_start);
1105         if (off < IEEE80211_SEQ_BA_RANGE) {
1106                 /*
1107                  * Flush the reorder q up to rxseq and move the window.
1108                  * Sec 9.10.7.6.3 a) (p.138)
1109                  */
1110                 IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
1111                     "BAR moves BA win <%u:%u> (%u frames) rxseq %u tid %u",
1112                     rap->rxa_start,
1113                     IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
1114                     rap->rxa_qframes, rxseq, tid);
1115                 vap->iv_stats.is_ampdu_bar_move++;
1116
1117                 ampdu_rx_flush_upto(ni, rap, rxseq);
1118                 if (off >= rap->rxa_wnd) {
1119                         /*
1120                          * BAR specifies a window start to the right of BA
1121                          * window; we must move it explicitly since
1122                          * ampdu_rx_flush_upto will not.
1123                          */
1124                         rap->rxa_start = rxseq;
1125                 }
1126         } else {
1127                 /*
1128                  * Out of range; toss.
1129                  * Sec 9.10.7.6.3 b) (p.138)
1130                  */
1131                 IEEE80211_DISCARD_MAC(vap,
1132                     IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
1133                     "BAR", "BA win <%u:%u> (%u frames) rxseq %u tid %u%s",
1134                     rap->rxa_start,
1135                     IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
1136                     rap->rxa_qframes, rxseq, tid,
1137                     wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
1138                 vap->iv_stats.is_ampdu_bar_oow++;
1139                 IEEE80211_NODE_STAT(ni, rx_drop);
1140         }
1141 }
1142
1143 /*
1144  * Setup HT-specific state in a node.  Called only
1145  * when HT use is negotiated so we don't do extra
1146  * work for temporary and/or legacy sta's.
1147  */
1148 void
1149 ieee80211_ht_node_init(struct ieee80211_node *ni)
1150 {
1151         struct ieee80211_tx_ampdu *tap;
1152         int tid;
1153
1154         IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
1155             ni,
1156             "%s: called (%p)",
1157             __func__,
1158             ni);
1159
1160         if (ni->ni_flags & IEEE80211_NODE_HT) {
1161                 /*
1162                  * Clean AMPDU state on re-associate.  This handles the case
1163                  * where a station leaves w/o notifying us and then returns
1164                  * before node is reaped for inactivity.
1165                  */
1166                 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
1167                     ni,
1168                     "%s: calling cleanup (%p)",
1169                     __func__, ni);
1170                 ieee80211_ht_node_cleanup(ni);
1171         }
1172         for (tid = 0; tid < WME_NUM_TID; tid++) {
1173                 tap = &ni->ni_tx_ampdu[tid];
1174                 tap->txa_tid = tid;
1175                 tap->txa_ni = ni;
1176                 ieee80211_txampdu_init_pps(tap);
1177                 /* NB: further initialization deferred */
1178         }
1179         ni->ni_flags |= IEEE80211_NODE_HT | IEEE80211_NODE_AMPDU |
1180             IEEE80211_NODE_AMSDU;
1181 }
1182
1183 /*
1184  * Cleanup HT-specific state in a node.  Called only
1185  * when HT use has been marked.
1186  */
1187 void
1188 ieee80211_ht_node_cleanup(struct ieee80211_node *ni)
1189 {
1190         struct ieee80211com *ic = ni->ni_ic;
1191         int i;
1192
1193         IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
1194             ni,
1195             "%s: called (%p)",
1196             __func__, ni);
1197
1198         KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT node"));
1199
1200         /* XXX optimize this */
1201         for (i = 0; i < WME_NUM_TID; i++) {
1202                 struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[i];
1203                 if (tap->txa_flags & IEEE80211_AGGR_SETUP)
1204                         ampdu_tx_stop(tap);
1205         }
1206         for (i = 0; i < WME_NUM_TID; i++)
1207                 ic->ic_ampdu_rx_stop(ni, &ni->ni_rx_ampdu[i]);
1208
1209         ni->ni_htcap = 0;
1210         ni->ni_flags &= ~IEEE80211_NODE_HT_ALL;
1211 }
1212
1213 /*
1214  * Age out HT resources for a station.
1215  */
1216 void
1217 ieee80211_ht_node_age(struct ieee80211_node *ni)
1218 {
1219         struct ieee80211vap *vap = ni->ni_vap;
1220         uint8_t tid;
1221
1222         KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT sta"));
1223
1224         for (tid = 0; tid < WME_NUM_TID; tid++) {
1225                 struct ieee80211_rx_ampdu *rap;
1226
1227                 rap = &ni->ni_rx_ampdu[tid];
1228                 if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0)
1229                         continue;
1230                 if (rap->rxa_qframes == 0)
1231                         continue;
1232                 /* 
1233                  * Check for frames sitting too long in the reorder queue.
1234                  * See above for more details on what's happening here.
1235                  */
1236                 /* XXX honor batimeout? */
1237                 if (ticks - rap->rxa_age > ieee80211_ampdu_age) {
1238                         /*
1239                          * Too long since we received the first
1240                          * frame; flush the reorder buffer.
1241                          */
1242                         vap->iv_stats.is_ampdu_rx_age += rap->rxa_qframes;
1243                         ampdu_rx_flush(ni, rap);
1244                 }
1245         }
1246 }
1247
1248 static struct ieee80211_channel *
1249 findhtchan(struct ieee80211com *ic, struct ieee80211_channel *c, int htflags)
1250 {
1251         return ieee80211_find_channel(ic, c->ic_freq,
1252             (c->ic_flags &~ IEEE80211_CHAN_HT) | htflags);
1253 }
1254
1255 /*
1256  * Adjust a channel to be HT/non-HT according to the vap's configuration.
1257  */
1258 struct ieee80211_channel *
1259 ieee80211_ht_adjust_channel(struct ieee80211com *ic,
1260         struct ieee80211_channel *chan, int flags)
1261 {
1262         struct ieee80211_channel *c;
1263
1264         if (flags & IEEE80211_FHT_HT) {
1265                 /* promote to HT if possible */
1266                 if (flags & IEEE80211_FHT_USEHT40) {
1267                         if (!IEEE80211_IS_CHAN_HT40(chan)) {
1268                                 /* NB: arbitrarily pick ht40+ over ht40- */
1269                                 c = findhtchan(ic, chan, IEEE80211_CHAN_HT40U);
1270                                 if (c == NULL)
1271                                         c = findhtchan(ic, chan,
1272                                                 IEEE80211_CHAN_HT40D);
1273                                 if (c == NULL)
1274                                         c = findhtchan(ic, chan,
1275                                                 IEEE80211_CHAN_HT20);
1276                                 if (c != NULL)
1277                                         chan = c;
1278                         }
1279                 } else if (!IEEE80211_IS_CHAN_HT20(chan)) {
1280                         c = findhtchan(ic, chan, IEEE80211_CHAN_HT20);
1281                         if (c != NULL)
1282                                 chan = c;
1283                 }
1284         } else if (IEEE80211_IS_CHAN_HT(chan)) {
1285                 /* demote to legacy, HT use is disabled */
1286                 c = ieee80211_find_channel(ic, chan->ic_freq,
1287                     chan->ic_flags &~ IEEE80211_CHAN_HT);
1288                 if (c != NULL)
1289                         chan = c;
1290         }
1291         return chan;
1292 }
1293
1294 /*
1295  * Setup HT-specific state for a legacy WDS peer.
1296  */
1297 void
1298 ieee80211_ht_wds_init(struct ieee80211_node *ni)
1299 {
1300         struct ieee80211vap *vap = ni->ni_vap;
1301         struct ieee80211_tx_ampdu *tap;
1302         int tid;
1303
1304         KASSERT(vap->iv_flags_ht & IEEE80211_FHT_HT, ("no HT requested"));
1305
1306         /* XXX check scan cache in case peer has an ap and we have info */
1307         /*
1308          * If setup with a legacy channel; locate an HT channel.
1309          * Otherwise if the inherited channel (from a companion
1310          * AP) is suitable use it so we use the same location
1311          * for the extension channel).
1312          */
1313         ni->ni_chan = ieee80211_ht_adjust_channel(ni->ni_ic,
1314             ni->ni_chan, ieee80211_htchanflags(ni->ni_chan));
1315
1316         ni->ni_htcap = 0;
1317         if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20)
1318                 ni->ni_htcap |= IEEE80211_HTCAP_SHORTGI20;
1319         if (IEEE80211_IS_CHAN_HT40(ni->ni_chan)) {
1320                 ni->ni_htcap |= IEEE80211_HTCAP_CHWIDTH40;
1321                 ni->ni_chw = 40;
1322                 if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan))
1323                         ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_ABOVE;
1324                 else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan))
1325                         ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_BELOW;
1326                 if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40)
1327                         ni->ni_htcap |= IEEE80211_HTCAP_SHORTGI40;
1328         } else {
1329                 ni->ni_chw = 20;
1330                 ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_NONE;
1331         }
1332         ni->ni_htctlchan = ni->ni_chan->ic_ieee;
1333         if (vap->iv_flags_ht & IEEE80211_FHT_RIFS)
1334                 ni->ni_flags |= IEEE80211_NODE_RIFS;
1335         /* XXX does it make sense to enable SMPS? */
1336
1337         ni->ni_htopmode = 0;            /* XXX need protection state */
1338         ni->ni_htstbc = 0;              /* XXX need info */
1339
1340         for (tid = 0; tid < WME_NUM_TID; tid++) {
1341                 tap = &ni->ni_tx_ampdu[tid];
1342                 tap->txa_tid = tid;
1343                 ieee80211_txampdu_init_pps(tap);
1344         }
1345         /* NB: AMPDU tx/rx governed by IEEE80211_FHT_AMPDU_{TX,RX} */
1346         ni->ni_flags |= IEEE80211_NODE_HT | IEEE80211_NODE_AMPDU |
1347             IEEE80211_NODE_AMSDU;
1348 }
1349
1350 /*
1351  * Notify hostap vaps of a change in the HTINFO ie.
1352  */
1353 static void
1354 htinfo_notify(struct ieee80211com *ic)
1355 {
1356         struct ieee80211vap *vap;
1357         int first = 1;
1358
1359         IEEE80211_LOCK_ASSERT(ic);
1360
1361         TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1362                 if (vap->iv_opmode != IEEE80211_M_HOSTAP)
1363                         continue;
1364                 if (vap->iv_state != IEEE80211_S_RUN ||
1365                     !IEEE80211_IS_CHAN_HT(vap->iv_bss->ni_chan))
1366                         continue;
1367                 if (first) {
1368                         IEEE80211_NOTE(vap,
1369                             IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N,
1370                             vap->iv_bss,
1371                             "HT bss occupancy change: %d sta, %d ht, "
1372                             "%d ht40%s, HT protmode now 0x%x"
1373                             , ic->ic_sta_assoc
1374                             , ic->ic_ht_sta_assoc
1375                             , ic->ic_ht40_sta_assoc
1376                             , (ic->ic_flags_ht & IEEE80211_FHT_NONHT_PR) ?
1377                                  ", non-HT sta present" : ""
1378                             , ic->ic_curhtprotmode);
1379                         first = 0;
1380                 }
1381                 ieee80211_beacon_notify(vap, IEEE80211_BEACON_HTINFO);
1382         }
1383 }
1384
1385 /*
1386  * Calculate HT protection mode from current
1387  * state and handle updates.
1388  */
1389 static void
1390 htinfo_update(struct ieee80211com *ic)
1391 {
1392         uint8_t protmode;
1393
1394         if (ic->ic_sta_assoc != ic->ic_ht_sta_assoc) {
1395                 protmode = IEEE80211_HTINFO_OPMODE_MIXED
1396                          | IEEE80211_HTINFO_NONHT_PRESENT;
1397         } else if (ic->ic_flags_ht & IEEE80211_FHT_NONHT_PR) {
1398                 protmode = IEEE80211_HTINFO_OPMODE_PROTOPT
1399                          | IEEE80211_HTINFO_NONHT_PRESENT;
1400         } else if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
1401             IEEE80211_IS_CHAN_HT40(ic->ic_bsschan) && 
1402             ic->ic_sta_assoc != ic->ic_ht40_sta_assoc) {
1403                 protmode = IEEE80211_HTINFO_OPMODE_HT20PR;
1404         } else {
1405                 protmode = IEEE80211_HTINFO_OPMODE_PURE;
1406         }
1407         if (protmode != ic->ic_curhtprotmode) {
1408                 ic->ic_curhtprotmode = protmode;
1409                 htinfo_notify(ic);
1410         }
1411 }
1412
1413 /*
1414  * Handle an HT station joining a BSS.
1415  */
1416 void
1417 ieee80211_ht_node_join(struct ieee80211_node *ni)
1418 {
1419         struct ieee80211com *ic = ni->ni_ic;
1420
1421         IEEE80211_LOCK_ASSERT(ic);
1422
1423         if (ni->ni_flags & IEEE80211_NODE_HT) {
1424                 ic->ic_ht_sta_assoc++;
1425                 if (ni->ni_chw == 40)
1426                         ic->ic_ht40_sta_assoc++;
1427         }
1428         htinfo_update(ic);
1429 }
1430
1431 /*
1432  * Handle an HT station leaving a BSS.
1433  */
1434 void
1435 ieee80211_ht_node_leave(struct ieee80211_node *ni)
1436 {
1437         struct ieee80211com *ic = ni->ni_ic;
1438
1439         IEEE80211_LOCK_ASSERT(ic);
1440
1441         if (ni->ni_flags & IEEE80211_NODE_HT) {
1442                 ic->ic_ht_sta_assoc--;
1443                 if (ni->ni_chw == 40)
1444                         ic->ic_ht40_sta_assoc--;
1445         }
1446         htinfo_update(ic);
1447 }
1448
1449 /*
1450  * Public version of htinfo_update; used for processing
1451  * beacon frames from overlapping bss.
1452  *
1453  * Caller can specify either IEEE80211_HTINFO_OPMODE_MIXED
1454  * (on receipt of a beacon that advertises MIXED) or
1455  * IEEE80211_HTINFO_OPMODE_PROTOPT (on receipt of a beacon
1456  * from an overlapping legacy bss).  We treat MIXED with
1457  * a higher precedence than PROTOPT (i.e. we will not change
1458  * change PROTOPT -> MIXED; only MIXED -> PROTOPT).  This
1459  * corresponds to how we handle things in htinfo_update.
1460  */
1461 void
1462 ieee80211_htprot_update(struct ieee80211com *ic, int protmode)
1463 {
1464 #define OPMODE(x)       SM(x, IEEE80211_HTINFO_OPMODE)
1465         IEEE80211_LOCK(ic);
1466
1467         /* track non-HT station presence */
1468         KASSERT(protmode & IEEE80211_HTINFO_NONHT_PRESENT,
1469             ("protmode 0x%x", protmode));
1470         ic->ic_flags_ht |= IEEE80211_FHT_NONHT_PR;
1471         ic->ic_lastnonht = ticks;
1472
1473         if (protmode != ic->ic_curhtprotmode &&
1474             (OPMODE(ic->ic_curhtprotmode) != IEEE80211_HTINFO_OPMODE_MIXED ||
1475              OPMODE(protmode) == IEEE80211_HTINFO_OPMODE_PROTOPT)) {
1476                 /* push beacon update */
1477                 ic->ic_curhtprotmode = protmode;
1478                 htinfo_notify(ic);
1479         }
1480         IEEE80211_UNLOCK(ic);
1481 #undef OPMODE
1482 }
1483
1484 /*
1485  * Time out presence of an overlapping bss with non-HT
1486  * stations.  When operating in hostap mode we listen for
1487  * beacons from other stations and if we identify a non-HT
1488  * station is present we update the opmode field of the
1489  * HTINFO ie.  To identify when all non-HT stations are
1490  * gone we time out this condition.
1491  */
1492 void
1493 ieee80211_ht_timeout(struct ieee80211com *ic)
1494 {
1495         IEEE80211_LOCK_ASSERT(ic);
1496
1497         if ((ic->ic_flags_ht & IEEE80211_FHT_NONHT_PR) &&
1498             ieee80211_time_after(ticks, ic->ic_lastnonht + IEEE80211_NONHT_PRESENT_AGE)) {
1499 #if 0
1500                 IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
1501                     "%s", "time out non-HT STA present on channel");
1502 #endif
1503                 ic->ic_flags_ht &= ~IEEE80211_FHT_NONHT_PR;
1504                 htinfo_update(ic);
1505         }
1506 }
1507
1508 /*
1509  * Process an 802.11n HT capabilities ie.
1510  */
1511 void
1512 ieee80211_parse_htcap(struct ieee80211_node *ni, const uint8_t *ie)
1513 {
1514         if (ie[0] == IEEE80211_ELEMID_VENDOR) {
1515                 /*
1516                  * Station used Vendor OUI ie to associate;
1517                  * mark the node so when we respond we'll use
1518                  * the Vendor OUI's and not the standard ie's.
1519                  */
1520                 ni->ni_flags |= IEEE80211_NODE_HTCOMPAT;
1521                 ie += 4;
1522         } else
1523                 ni->ni_flags &= ~IEEE80211_NODE_HTCOMPAT;
1524
1525         ni->ni_htcap = le16dec(ie +
1526                 __offsetof(struct ieee80211_ie_htcap, hc_cap));
1527         ni->ni_htparam = ie[__offsetof(struct ieee80211_ie_htcap, hc_param)];
1528 }
1529
1530 static void
1531 htinfo_parse(struct ieee80211_node *ni,
1532         const struct ieee80211_ie_htinfo *htinfo)
1533 {
1534         uint16_t w;
1535
1536         ni->ni_htctlchan = htinfo->hi_ctrlchannel;
1537         ni->ni_ht2ndchan = SM(htinfo->hi_byte1, IEEE80211_HTINFO_2NDCHAN);
1538         w = le16dec(&htinfo->hi_byte2);
1539         ni->ni_htopmode = SM(w, IEEE80211_HTINFO_OPMODE);
1540         w = le16dec(&htinfo->hi_byte45);
1541         ni->ni_htstbc = SM(w, IEEE80211_HTINFO_BASIC_STBCMCS);
1542 }
1543
1544 /*
1545  * Parse an 802.11n HT info ie and save useful information
1546  * to the node state.  Note this does not effect any state
1547  * changes such as for channel width change.
1548  */
1549 void
1550 ieee80211_parse_htinfo(struct ieee80211_node *ni, const uint8_t *ie)
1551 {
1552         if (ie[0] == IEEE80211_ELEMID_VENDOR)
1553                 ie += 4;
1554         htinfo_parse(ni, (const struct ieee80211_ie_htinfo *) ie);
1555 }
1556
1557 /*
1558  * Handle 11n/11ac channel switch.
1559  *
1560  * Use the received HT/VHT ie's to identify the right channel to use.
1561  * If we cannot locate it in the channel table then fallback to
1562  * legacy operation.
1563  *
1564  * Note that we use this information to identify the node's
1565  * channel only; the caller is responsible for insuring any
1566  * required channel change is done (e.g. in sta mode when
1567  * parsing the contents of a beacon frame).
1568  */
1569 static int
1570 htinfo_update_chw(struct ieee80211_node *ni, int htflags, int vhtflags)
1571 {
1572         struct ieee80211com *ic = ni->ni_ic;
1573         struct ieee80211_channel *c;
1574         int chanflags;
1575         int ret = 0;
1576
1577         /*
1578          * First step - do HT/VHT only channel lookup based on operating mode
1579          * flags.  This involves masking out the VHT flags as well.
1580          * Otherwise we end up doing the full channel walk each time
1581          * we trigger this, which is expensive.
1582          */
1583         chanflags = (ni->ni_chan->ic_flags &~
1584             (IEEE80211_CHAN_HT | IEEE80211_CHAN_VHT)) | htflags | vhtflags;
1585
1586         if (chanflags == ni->ni_chan->ic_flags)
1587                 goto done;
1588
1589         /*
1590          * If HT /or/ VHT flags have changed then check both.
1591          * We need to start by picking a HT channel anyway.
1592          */
1593
1594         c = NULL;
1595         chanflags = (ni->ni_chan->ic_flags &~
1596             (IEEE80211_CHAN_HT | IEEE80211_CHAN_VHT)) | htflags;
1597         /* XXX not right for ht40- */
1598         c = ieee80211_find_channel(ic, ni->ni_chan->ic_freq, chanflags);
1599         if (c == NULL && (htflags & IEEE80211_CHAN_HT40)) {
1600                 /*
1601                  * No HT40 channel entry in our table; fall back
1602                  * to HT20 operation.  This should not happen.
1603                  */
1604                 c = findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT20);
1605 #if 0
1606                 IEEE80211_NOTE(ni->ni_vap,
1607                     IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
1608                     "no HT40 channel (freq %u), falling back to HT20",
1609                     ni->ni_chan->ic_freq);
1610 #endif
1611                 /* XXX stat */
1612         }
1613
1614         /* Nothing found - leave it alone; move onto VHT */
1615         if (c == NULL)
1616                 c = ni->ni_chan;
1617
1618         /*
1619          * If it's non-HT, then bail out now.
1620          */
1621         if (! IEEE80211_IS_CHAN_HT(c)) {
1622                 IEEE80211_NOTE(ni->ni_vap,
1623                     IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
1624                     "not HT; skipping VHT check (%u/0x%x)",
1625                     c->ic_freq, c->ic_flags);
1626                 goto done;
1627         }
1628
1629         /*
1630          * Next step - look at the current VHT flags and determine
1631          * if we need to upgrade.  Mask out the VHT and HT flags since
1632          * the vhtflags field will already have the correct HT
1633          * flags to use.
1634          */
1635         if (IEEE80211_CONF_VHT(ic) && ni->ni_vhtcap != 0 && vhtflags != 0) {
1636                 chanflags = (c->ic_flags
1637                     &~ (IEEE80211_CHAN_HT | IEEE80211_CHAN_VHT))
1638                     | vhtflags;
1639                 IEEE80211_NOTE(ni->ni_vap,
1640                     IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N,
1641                     ni,
1642                     "%s: VHT; chanwidth=0x%02x; vhtflags=0x%08x",
1643                     __func__, ni->ni_vht_chanwidth, vhtflags);
1644
1645                 IEEE80211_NOTE(ni->ni_vap,
1646                     IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N,
1647                     ni,
1648                     "%s: VHT; trying lookup for %d/0x%08x",
1649                     __func__, c->ic_freq, chanflags);
1650                 c = ieee80211_find_channel(ic, c->ic_freq, chanflags);
1651         }
1652
1653         /* Finally, if it's changed */
1654         if (c != NULL && c != ni->ni_chan) {
1655                 IEEE80211_NOTE(ni->ni_vap,
1656                     IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
1657                     "switch station to %s%d channel %u/0x%x",
1658                     IEEE80211_IS_CHAN_VHT(c) ? "VHT" : "HT",
1659                     IEEE80211_IS_CHAN_VHT80(c) ? 80 :
1660                       (IEEE80211_IS_CHAN_HT40(c) ? 40 : 20),
1661                     c->ic_freq, c->ic_flags);
1662                 ni->ni_chan = c;
1663                 ret = 1;
1664         }
1665         /* NB: caller responsible for forcing any channel change */
1666
1667 done:
1668         /* update node's (11n) tx channel width */
1669         ni->ni_chw = IEEE80211_IS_CHAN_HT40(ni->ni_chan)? 40 : 20;
1670         return (ret);
1671 }
1672
1673 /*
1674  * Update 11n MIMO PS state according to received htcap.
1675  */
1676 static __inline int
1677 htcap_update_mimo_ps(struct ieee80211_node *ni)
1678 {
1679         uint16_t oflags = ni->ni_flags;
1680
1681         switch (ni->ni_htcap & IEEE80211_HTCAP_SMPS) {
1682         case IEEE80211_HTCAP_SMPS_DYNAMIC:
1683                 ni->ni_flags |= IEEE80211_NODE_MIMO_PS;
1684                 ni->ni_flags |= IEEE80211_NODE_MIMO_RTS;
1685                 break;
1686         case IEEE80211_HTCAP_SMPS_ENA:
1687                 ni->ni_flags |= IEEE80211_NODE_MIMO_PS;
1688                 ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS;
1689                 break;
1690         case IEEE80211_HTCAP_SMPS_OFF:
1691         default:                /* disable on rx of reserved value */
1692                 ni->ni_flags &= ~IEEE80211_NODE_MIMO_PS;
1693                 ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS;
1694                 break;
1695         }
1696         return (oflags ^ ni->ni_flags);
1697 }
1698
1699 /*
1700  * Update short GI state according to received htcap
1701  * and local settings.
1702  */
1703 static __inline void
1704 htcap_update_shortgi(struct ieee80211_node *ni)
1705 {
1706         struct ieee80211vap *vap = ni->ni_vap;
1707
1708         ni->ni_flags &= ~(IEEE80211_NODE_SGI20|IEEE80211_NODE_SGI40);
1709         if ((ni->ni_htcap & IEEE80211_HTCAP_SHORTGI20) &&
1710             (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20))
1711                 ni->ni_flags |= IEEE80211_NODE_SGI20;
1712         if ((ni->ni_htcap & IEEE80211_HTCAP_SHORTGI40) &&
1713             (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40))
1714                 ni->ni_flags |= IEEE80211_NODE_SGI40;
1715 }
1716
1717 /*
1718  * Update LDPC state according to received htcap
1719  * and local settings.
1720  */
1721 static __inline void
1722 htcap_update_ldpc(struct ieee80211_node *ni)
1723 {
1724         struct ieee80211vap *vap = ni->ni_vap;
1725
1726         if ((ni->ni_htcap & IEEE80211_HTCAP_LDPC) &&
1727             (vap->iv_flags_ht & IEEE80211_FHT_LDPC_TX))
1728                 ni->ni_flags |= IEEE80211_NODE_LDPC;
1729 }
1730
1731 /*
1732  * Parse and update HT-related state extracted from
1733  * the HT cap and info ie's.
1734  *
1735  * This is called from the STA management path and
1736  * the ieee80211_node_join() path.  It will take into
1737  * account the IEs discovered during scanning and
1738  * adjust things accordingly.
1739  */
1740 void
1741 ieee80211_ht_updateparams(struct ieee80211_node *ni,
1742         const uint8_t *htcapie, const uint8_t *htinfoie)
1743 {
1744         struct ieee80211vap *vap = ni->ni_vap;
1745         const struct ieee80211_ie_htinfo *htinfo;
1746
1747         ieee80211_parse_htcap(ni, htcapie);
1748         if (vap->iv_htcaps & IEEE80211_HTC_SMPS)
1749                 htcap_update_mimo_ps(ni);
1750         htcap_update_shortgi(ni);
1751         htcap_update_ldpc(ni);
1752
1753         if (htinfoie[0] == IEEE80211_ELEMID_VENDOR)
1754                 htinfoie += 4;
1755         htinfo = (const struct ieee80211_ie_htinfo *) htinfoie;
1756         htinfo_parse(ni, htinfo);
1757
1758         /*
1759          * Defer the node channel change; we need to now
1760          * update VHT parameters before we do it.
1761          */
1762
1763         if ((htinfo->hi_byte1 & IEEE80211_HTINFO_RIFSMODE_PERM) &&
1764             (vap->iv_flags_ht & IEEE80211_FHT_RIFS))
1765                 ni->ni_flags |= IEEE80211_NODE_RIFS;
1766         else
1767                 ni->ni_flags &= ~IEEE80211_NODE_RIFS;
1768 }
1769
1770 static uint32_t
1771 ieee80211_vht_get_vhtflags(struct ieee80211_node *ni, uint32_t htflags)
1772 {
1773         struct ieee80211vap *vap = ni->ni_vap;
1774         uint32_t vhtflags = 0;
1775
1776         vhtflags = 0;
1777         if (ni->ni_flags & IEEE80211_NODE_VHT && vap->iv_flags_vht & IEEE80211_FVHT_VHT) {
1778                 if ((ni->ni_vht_chanwidth == IEEE80211_VHT_CHANWIDTH_160MHZ) &&
1779                     /* XXX 2 means "160MHz and 80+80MHz", 1 means "160MHz" */
1780                     (MS(vap->iv_vhtcaps,
1781                      IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_MASK) >= 1) &&
1782                     (vap->iv_flags_vht & IEEE80211_FVHT_USEVHT160)) {
1783                         vhtflags = IEEE80211_CHAN_VHT160;
1784                         /* Mirror the HT40 flags */
1785                         if (htflags == IEEE80211_CHAN_HT40U) {
1786                                 vhtflags |= IEEE80211_CHAN_HT40U;
1787                         } else if (htflags == IEEE80211_CHAN_HT40D) {
1788                                 vhtflags |= IEEE80211_CHAN_HT40D;
1789                         }
1790                 } else if ((ni->ni_vht_chanwidth == IEEE80211_VHT_CHANWIDTH_80P80MHZ) &&
1791                     /* XXX 2 means "160MHz and 80+80MHz" */
1792                     (MS(vap->iv_vhtcaps,
1793                      IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_MASK) == 2) &&
1794                     (vap->iv_flags_vht & IEEE80211_FVHT_USEVHT80P80)) {
1795                         vhtflags = IEEE80211_CHAN_VHT80_80;
1796                         /* Mirror the HT40 flags */
1797                         if (htflags == IEEE80211_CHAN_HT40U) {
1798                                 vhtflags |= IEEE80211_CHAN_HT40U;
1799                         } else if (htflags == IEEE80211_CHAN_HT40D) {
1800                                 vhtflags |= IEEE80211_CHAN_HT40D;
1801                         }
1802                 } else if ((ni->ni_vht_chanwidth == IEEE80211_VHT_CHANWIDTH_80MHZ) &&
1803                     (vap->iv_flags_vht & IEEE80211_FVHT_USEVHT80)) {
1804                         vhtflags = IEEE80211_CHAN_VHT80;
1805                         /* Mirror the HT40 flags */
1806                         if (htflags == IEEE80211_CHAN_HT40U) {
1807                                 vhtflags |= IEEE80211_CHAN_HT40U;
1808                         } else if (htflags == IEEE80211_CHAN_HT40D) {
1809                                 vhtflags |= IEEE80211_CHAN_HT40D;
1810                         }
1811                 } else if (ni->ni_vht_chanwidth == IEEE80211_VHT_CHANWIDTH_USE_HT) {
1812                         /* Mirror the HT40 flags */
1813                         /*
1814                          * XXX TODO: if ht40 is disabled, but vht40 isn't
1815                          * disabled then this logic will get very, very sad.
1816                          * It's quite possible the only sane thing to do is
1817                          * to not have vht40 as an option, and just obey
1818                          * 'ht40' as that flag.
1819                          */
1820                         if ((htflags == IEEE80211_CHAN_HT40U) &&
1821                             (vap->iv_flags_vht & IEEE80211_FVHT_USEVHT40)) {
1822                                 vhtflags = IEEE80211_CHAN_VHT40U
1823                                     | IEEE80211_CHAN_HT40U;
1824                         } else if (htflags == IEEE80211_CHAN_HT40D &&
1825                             (vap->iv_flags_vht & IEEE80211_FVHT_USEVHT40)) {
1826                                 vhtflags = IEEE80211_CHAN_VHT40D
1827                                     | IEEE80211_CHAN_HT40D;
1828                         } else if (htflags == IEEE80211_CHAN_HT20) {
1829                                 vhtflags = IEEE80211_CHAN_VHT20
1830                                     | IEEE80211_CHAN_HT20;
1831                         }
1832                 } else {
1833                         vhtflags = IEEE80211_CHAN_VHT20;
1834                 }
1835         }
1836         return (vhtflags);
1837 }
1838
1839 /*
1840  * Final part of updating the HT parameters.
1841  *
1842  * This is called from the STA management path and
1843  * the ieee80211_node_join() path.  It will take into
1844  * account the IEs discovered during scanning and
1845  * adjust things accordingly.
1846  *
1847  * This is done after a call to ieee80211_ht_updateparams()
1848  * because it (and the upcoming VHT version of updateparams)
1849  * needs to ensure everything is parsed before htinfo_update_chw()
1850  * is called - which will change the channel config for the
1851  * node for us.
1852  */
1853 int
1854 ieee80211_ht_updateparams_final(struct ieee80211_node *ni,
1855         const uint8_t *htcapie, const uint8_t *htinfoie)
1856 {
1857         struct ieee80211vap *vap = ni->ni_vap;
1858         const struct ieee80211_ie_htinfo *htinfo;
1859         int htflags, vhtflags;
1860         int ret = 0;
1861
1862         htinfo = (const struct ieee80211_ie_htinfo *) htinfoie;
1863
1864         htflags = (vap->iv_flags_ht & IEEE80211_FHT_HT) ?
1865             IEEE80211_CHAN_HT20 : 0;
1866
1867         /* NB: honor operating mode constraint */
1868         if ((htinfo->hi_byte1 & IEEE80211_HTINFO_TXWIDTH_2040) &&
1869             (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)) {
1870                 if (ni->ni_ht2ndchan == IEEE80211_HTINFO_2NDCHAN_ABOVE)
1871                         htflags = IEEE80211_CHAN_HT40U;
1872                 else if (ni->ni_ht2ndchan == IEEE80211_HTINFO_2NDCHAN_BELOW)
1873                         htflags = IEEE80211_CHAN_HT40D;
1874         }
1875
1876         /*
1877          * VHT flags - do much the same; check whether VHT is available
1878          * and if so, what our ideal channel use would be based on our
1879          * capabilities and the (pre-parsed) VHT info IE.
1880          */
1881         vhtflags = ieee80211_vht_get_vhtflags(ni, htflags);
1882
1883         if (htinfo_update_chw(ni, htflags, vhtflags))
1884                 ret = 1;
1885
1886         return (ret);
1887 }
1888
1889 /*
1890  * Parse and update HT-related state extracted from the HT cap ie
1891  * for a station joining an HT BSS.
1892  *
1893  * This is called from the hostap path for each station.
1894  */
1895 void
1896 ieee80211_ht_updatehtcap(struct ieee80211_node *ni, const uint8_t *htcapie)
1897 {
1898         struct ieee80211vap *vap = ni->ni_vap;
1899
1900         ieee80211_parse_htcap(ni, htcapie);
1901         if (vap->iv_htcaps & IEEE80211_HTC_SMPS)
1902                 htcap_update_mimo_ps(ni);
1903         htcap_update_shortgi(ni);
1904         htcap_update_ldpc(ni);
1905 }
1906
1907 /*
1908  * Called once HT and VHT capabilities are parsed in hostap mode -
1909  * this will adjust the channel configuration of the given node
1910  * based on the configuration and capabilities.
1911  */
1912 void
1913 ieee80211_ht_updatehtcap_final(struct ieee80211_node *ni)
1914 {
1915         struct ieee80211vap *vap = ni->ni_vap;
1916         int htflags;
1917         int vhtflags;
1918
1919         /* NB: honor operating mode constraint */
1920         /* XXX 40 MHz intolerant */
1921         htflags = (vap->iv_flags_ht & IEEE80211_FHT_HT) ?
1922             IEEE80211_CHAN_HT20 : 0;
1923         if ((ni->ni_htcap & IEEE80211_HTCAP_CHWIDTH40) &&
1924             (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)) {
1925                 if (IEEE80211_IS_CHAN_HT40U(vap->iv_bss->ni_chan))
1926                         htflags = IEEE80211_CHAN_HT40U;
1927                 else if (IEEE80211_IS_CHAN_HT40D(vap->iv_bss->ni_chan))
1928                         htflags = IEEE80211_CHAN_HT40D;
1929         }
1930         /*
1931          * VHT flags - do much the same; check whether VHT is available
1932          * and if so, what our ideal channel use would be based on our
1933          * capabilities and the (pre-parsed) VHT info IE.
1934          */
1935         vhtflags = ieee80211_vht_get_vhtflags(ni, htflags);
1936
1937         (void) htinfo_update_chw(ni, htflags, vhtflags);
1938 }
1939
1940 /*
1941  * Install received HT rate set by parsing the HT cap ie.
1942  */
1943 int
1944 ieee80211_setup_htrates(struct ieee80211_node *ni, const uint8_t *ie, int flags)
1945 {
1946         struct ieee80211com *ic = ni->ni_ic;
1947         struct ieee80211vap *vap = ni->ni_vap;
1948         const struct ieee80211_ie_htcap *htcap;
1949         struct ieee80211_htrateset *rs;
1950         int i, maxequalmcs, maxunequalmcs;
1951
1952         maxequalmcs = ic->ic_txstream * 8 - 1;
1953         maxunequalmcs = 0;
1954         if (ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL) {
1955                 if (ic->ic_txstream >= 2)
1956                         maxunequalmcs = 38;
1957                 if (ic->ic_txstream >= 3)
1958                         maxunequalmcs = 52;
1959                 if (ic->ic_txstream >= 4)
1960                         maxunequalmcs = 76;
1961         }
1962
1963         rs = &ni->ni_htrates;
1964         memset(rs, 0, sizeof(*rs));
1965         if (ie != NULL) {
1966                 if (ie[0] == IEEE80211_ELEMID_VENDOR)
1967                         ie += 4;
1968                 htcap = (const struct ieee80211_ie_htcap *) ie;
1969                 for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++) {
1970                         if (isclr(htcap->hc_mcsset, i))
1971                                 continue;
1972                         if (rs->rs_nrates == IEEE80211_HTRATE_MAXSIZE) {
1973                                 IEEE80211_NOTE(vap,
1974                                     IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
1975                                     "WARNING, HT rate set too large; only "
1976                                     "using %u rates", IEEE80211_HTRATE_MAXSIZE);
1977                                 vap->iv_stats.is_rx_rstoobig++;
1978                                 break;
1979                         }
1980                         if (i <= 31 && i > maxequalmcs)
1981                                 continue;
1982                         if (i == 32 &&
1983                             (ic->ic_htcaps & IEEE80211_HTC_TXMCS32) == 0)
1984                                 continue;
1985                         if (i > 32 && i > maxunequalmcs)
1986                                 continue;
1987                         rs->rs_rates[rs->rs_nrates++] = i;
1988                 }
1989         }
1990         return ieee80211_fix_rate(ni, (struct ieee80211_rateset *) rs, flags);
1991 }
1992
1993 /*
1994  * Mark rates in a node's HT rate set as basic according
1995  * to the information in the supplied HT info ie.
1996  */
1997 void
1998 ieee80211_setup_basic_htrates(struct ieee80211_node *ni, const uint8_t *ie)
1999 {
2000         const struct ieee80211_ie_htinfo *htinfo;
2001         struct ieee80211_htrateset *rs;
2002         int i, j;
2003
2004         if (ie[0] == IEEE80211_ELEMID_VENDOR)
2005                 ie += 4;
2006         htinfo = (const struct ieee80211_ie_htinfo *) ie;
2007         rs = &ni->ni_htrates;
2008         if (rs->rs_nrates == 0) {
2009                 IEEE80211_NOTE(ni->ni_vap,
2010                     IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
2011                     "%s", "WARNING, empty HT rate set");
2012                 return;
2013         }
2014         for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++) {
2015                 if (isclr(htinfo->hi_basicmcsset, i))
2016                         continue;
2017                 for (j = 0; j < rs->rs_nrates; j++)
2018                         if ((rs->rs_rates[j] & IEEE80211_RATE_VAL) == i)
2019                                 rs->rs_rates[j] |= IEEE80211_RATE_BASIC;
2020         }
2021 }
2022
2023 static void
2024 ampdu_tx_setup(struct ieee80211_tx_ampdu *tap)
2025 {
2026         callout_init(&tap->txa_timer, 1);
2027         tap->txa_flags |= IEEE80211_AGGR_SETUP;
2028         tap->txa_lastsample = ticks;
2029 }
2030
2031 static void
2032 ampdu_tx_stop(struct ieee80211_tx_ampdu *tap)
2033 {
2034         struct ieee80211_node *ni = tap->txa_ni;
2035         struct ieee80211com *ic = ni->ni_ic;
2036
2037         IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2038             tap->txa_ni,
2039             "%s: called",
2040             __func__);
2041
2042         KASSERT(tap->txa_flags & IEEE80211_AGGR_SETUP,
2043             ("txa_flags 0x%x tid %d ac %d", tap->txa_flags, tap->txa_tid,
2044             TID_TO_WME_AC(tap->txa_tid)));
2045
2046         /*
2047          * Stop BA stream if setup so driver has a chance
2048          * to reclaim any resources it might have allocated.
2049          */
2050         ic->ic_addba_stop(ni, tap);
2051         /*
2052          * Stop any pending BAR transmit.
2053          */
2054         bar_stop_timer(tap);
2055
2056         /*
2057          * Reset packet estimate.
2058          */
2059         ieee80211_txampdu_init_pps(tap);
2060
2061         /* NB: clearing NAK means we may re-send ADDBA */ 
2062         tap->txa_flags &= ~(IEEE80211_AGGR_SETUP | IEEE80211_AGGR_NAK);
2063 }
2064
2065 /*
2066  * ADDBA response timeout.
2067  *
2068  * If software aggregation and per-TID queue management was done here,
2069  * that queue would be unpaused after the ADDBA timeout occurs.
2070  */
2071 static void
2072 addba_timeout(void *arg)
2073 {
2074         struct ieee80211_tx_ampdu *tap = arg;
2075         struct ieee80211_node *ni = tap->txa_ni;
2076         struct ieee80211com *ic = ni->ni_ic;
2077
2078         /* XXX ? */
2079         tap->txa_flags &= ~IEEE80211_AGGR_XCHGPEND;
2080         tap->txa_attempts++;
2081         ic->ic_addba_response_timeout(ni, tap);
2082 }
2083
2084 static void
2085 addba_start_timeout(struct ieee80211_tx_ampdu *tap)
2086 {
2087         /* XXX use CALLOUT_PENDING instead? */
2088         callout_reset(&tap->txa_timer, ieee80211_addba_timeout,
2089             addba_timeout, tap);
2090         tap->txa_flags |= IEEE80211_AGGR_XCHGPEND;
2091         tap->txa_nextrequest = ticks + ieee80211_addba_timeout;
2092 }
2093
2094 static void
2095 addba_stop_timeout(struct ieee80211_tx_ampdu *tap)
2096 {
2097         /* XXX use CALLOUT_PENDING instead? */
2098         if (tap->txa_flags & IEEE80211_AGGR_XCHGPEND) {
2099                 callout_stop(&tap->txa_timer);
2100                 tap->txa_flags &= ~IEEE80211_AGGR_XCHGPEND;
2101         }
2102 }
2103
2104 static void
2105 null_addba_response_timeout(struct ieee80211_node *ni,
2106     struct ieee80211_tx_ampdu *tap)
2107 {
2108 }
2109
2110 /*
2111  * Default method for requesting A-MPDU tx aggregation.
2112  * We setup the specified state block and start a timer
2113  * to wait for an ADDBA response frame.
2114  */
2115 static int
2116 ieee80211_addba_request(struct ieee80211_node *ni,
2117         struct ieee80211_tx_ampdu *tap,
2118         int dialogtoken, int baparamset, int batimeout)
2119 {
2120         int bufsiz;
2121
2122         /* XXX locking */
2123         tap->txa_token = dialogtoken;
2124         tap->txa_flags |= IEEE80211_AGGR_IMMEDIATE;
2125         bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
2126         tap->txa_wnd = (bufsiz == 0) ?
2127             IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
2128         addba_start_timeout(tap);
2129         return 1;
2130 }
2131
2132 /*
2133  * Called by drivers that wish to request an ADDBA session be
2134  * setup.  This brings it up and starts the request timer.
2135  */
2136 int
2137 ieee80211_ampdu_tx_request_ext(struct ieee80211_node *ni, int tid)
2138 {
2139         struct ieee80211_tx_ampdu *tap;
2140
2141         if (tid < 0 || tid > 15)
2142                 return (0);
2143         tap = &ni->ni_tx_ampdu[tid];
2144
2145         /* XXX locking */
2146         if ((tap->txa_flags & IEEE80211_AGGR_SETUP) == 0) {
2147                 /* do deferred setup of state */
2148                 ampdu_tx_setup(tap);
2149         }
2150         /* XXX hack for not doing proper locking */
2151         tap->txa_flags &= ~IEEE80211_AGGR_NAK;
2152         addba_start_timeout(tap);
2153         return (1);
2154 }
2155
2156 /*
2157  * Called by drivers that have marked a session as active.
2158  */
2159 int
2160 ieee80211_ampdu_tx_request_active_ext(struct ieee80211_node *ni, int tid,
2161     int status)
2162 {
2163         struct ieee80211_tx_ampdu *tap;
2164
2165         if (tid < 0 || tid > 15)
2166                 return (0);
2167         tap = &ni->ni_tx_ampdu[tid];
2168
2169         /* XXX locking */
2170         addba_stop_timeout(tap);
2171         if (status == 1) {
2172                 tap->txa_flags |= IEEE80211_AGGR_RUNNING;
2173                 tap->txa_attempts = 0;
2174         } else {
2175                 /* mark tid so we don't try again */
2176                 tap->txa_flags |= IEEE80211_AGGR_NAK;
2177         }
2178         return (1);
2179 }
2180
2181 /*
2182  * Default method for processing an A-MPDU tx aggregation
2183  * response.  We shutdown any pending timer and update the
2184  * state block according to the reply.
2185  */
2186 static int
2187 ieee80211_addba_response(struct ieee80211_node *ni,
2188         struct ieee80211_tx_ampdu *tap,
2189         int status, int baparamset, int batimeout)
2190 {
2191         struct ieee80211vap *vap = ni->ni_vap;
2192         int bufsiz, tid;
2193
2194         /* XXX locking */
2195         addba_stop_timeout(tap);
2196         if (status == IEEE80211_STATUS_SUCCESS) {
2197                 bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
2198                 /* XXX override our request? */
2199                 tap->txa_wnd = (bufsiz == 0) ?
2200                     IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
2201                 tid = MS(baparamset, IEEE80211_BAPS_TID);
2202                 tap->txa_flags |= IEEE80211_AGGR_RUNNING;
2203                 tap->txa_attempts = 0;
2204                 /* TODO: this should be a vap flag */
2205                 if ((vap->iv_htcaps & IEEE80211_HTC_TX_AMSDU_AMPDU) &&
2206                     (ni->ni_flags & IEEE80211_NODE_AMSDU_TX) &&
2207                     (MS(baparamset, IEEE80211_BAPS_AMSDU)))
2208                         tap->txa_flags |= IEEE80211_AGGR_AMSDU;
2209                 else
2210                         tap->txa_flags &= ~IEEE80211_AGGR_AMSDU;
2211         } else {
2212                 /* mark tid so we don't try again */
2213                 tap->txa_flags |= IEEE80211_AGGR_NAK;
2214         }
2215         return 1;
2216 }
2217
2218 /*
2219  * Default method for stopping A-MPDU tx aggregation.
2220  * Any timer is cleared and we drain any pending frames.
2221  */
2222 static void
2223 ieee80211_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
2224 {
2225         /* XXX locking */
2226         addba_stop_timeout(tap);
2227         if (tap->txa_flags & IEEE80211_AGGR_RUNNING) {
2228                 /* XXX clear aggregation queue */
2229                 tap->txa_flags &= ~(IEEE80211_AGGR_RUNNING | IEEE80211_AGGR_AMSDU);
2230         }
2231         tap->txa_attempts = 0;
2232 }
2233
2234 /*
2235  * Process a received action frame using the default aggregation
2236  * policy.  We intercept ADDBA-related frames and use them to
2237  * update our aggregation state.  All other frames are passed up
2238  * for processing by ieee80211_recv_action.
2239  */
2240 static int
2241 ht_recv_action_ba_addba_request(struct ieee80211_node *ni,
2242         const struct ieee80211_frame *wh,
2243         const uint8_t *frm, const uint8_t *efrm)
2244 {
2245         struct ieee80211com *ic = ni->ni_ic;
2246         struct ieee80211vap *vap = ni->ni_vap;
2247         struct ieee80211_rx_ampdu *rap;
2248         uint8_t dialogtoken;
2249         uint16_t baparamset, batimeout, baseqctl;
2250         uint16_t args[5];
2251         int tid;
2252
2253         dialogtoken = frm[2];
2254         baparamset = le16dec(frm+3);
2255         batimeout = le16dec(frm+5);
2256         baseqctl = le16dec(frm+7);
2257
2258         tid = MS(baparamset, IEEE80211_BAPS_TID);
2259
2260         IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2261             "recv ADDBA request: dialogtoken %u baparamset 0x%x "
2262             "(tid %d bufsiz %d) batimeout %d baseqctl %d:%d amsdu %d",
2263             dialogtoken, baparamset,
2264             tid, MS(baparamset, IEEE80211_BAPS_BUFSIZ),
2265             batimeout,
2266             MS(baseqctl, IEEE80211_BASEQ_START),
2267             MS(baseqctl, IEEE80211_BASEQ_FRAG),
2268             MS(baparamset, IEEE80211_BAPS_AMSDU));
2269
2270         rap = &ni->ni_rx_ampdu[tid];
2271
2272         /* Send ADDBA response */
2273         args[0] = dialogtoken;
2274         /*
2275          * NB: We ack only if the sta associated with HT and
2276          * the ap is configured to do AMPDU rx (the latter
2277          * violates the 11n spec and is mostly for testing).
2278          */
2279         if ((ni->ni_flags & IEEE80211_NODE_AMPDU_RX) &&
2280             (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_RX)) {
2281                 /* XXX TODO: handle ampdu_rx_start failure */
2282                 ic->ic_ampdu_rx_start(ni, rap,
2283                     baparamset, batimeout, baseqctl);
2284
2285                 args[1] = IEEE80211_STATUS_SUCCESS;
2286         } else {
2287                 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2288                     ni, "reject ADDBA request: %s",
2289                     ni->ni_flags & IEEE80211_NODE_AMPDU_RX ?
2290                        "administratively disabled" :
2291                        "not negotiated for station");
2292                 vap->iv_stats.is_addba_reject++;
2293                 args[1] = IEEE80211_STATUS_UNSPECIFIED;
2294         }
2295         /* XXX honor rap flags? */
2296         args[2] = IEEE80211_BAPS_POLICY_IMMEDIATE
2297                 | SM(tid, IEEE80211_BAPS_TID)
2298                 | SM(rap->rxa_wnd, IEEE80211_BAPS_BUFSIZ)
2299                 ;
2300
2301         /*
2302          * TODO: we're out of iv_flags_ht fields; once
2303          * this is extended we should make this configurable.
2304          */
2305         if ((baparamset & IEEE80211_BAPS_AMSDU) &&
2306             (ni->ni_flags & IEEE80211_NODE_AMSDU_RX) &&
2307             (vap->iv_htcaps & IEEE80211_HTC_RX_AMSDU_AMPDU))
2308                 args[2] |= IEEE80211_BAPS_AMSDU;
2309
2310         args[3] = 0;
2311         args[4] = 0;
2312         ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
2313                 IEEE80211_ACTION_BA_ADDBA_RESPONSE, args);
2314         return 0;
2315 }
2316
2317 static int
2318 ht_recv_action_ba_addba_response(struct ieee80211_node *ni,
2319         const struct ieee80211_frame *wh,
2320         const uint8_t *frm, const uint8_t *efrm)
2321 {
2322         struct ieee80211com *ic = ni->ni_ic;
2323         struct ieee80211vap *vap = ni->ni_vap;
2324         struct ieee80211_tx_ampdu *tap;
2325         uint8_t dialogtoken, policy;
2326         uint16_t baparamset, batimeout, code;
2327         int tid, bufsiz;
2328         int amsdu;
2329
2330         dialogtoken = frm[2];
2331         code = le16dec(frm+3);
2332         baparamset = le16dec(frm+5);
2333         tid = MS(baparamset, IEEE80211_BAPS_TID);
2334         bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
2335         policy = MS(baparamset, IEEE80211_BAPS_POLICY);
2336         amsdu = !! MS(baparamset, IEEE80211_BAPS_AMSDU);
2337         batimeout = le16dec(frm+7);
2338
2339         tap = &ni->ni_tx_ampdu[tid];
2340         if ((tap->txa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
2341                 IEEE80211_DISCARD_MAC(vap,
2342                     IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2343                     ni->ni_macaddr, "ADDBA response",
2344                     "no pending ADDBA, tid %d dialogtoken %u "
2345                     "code %d", tid, dialogtoken, code);
2346                 vap->iv_stats.is_addba_norequest++;
2347                 return 0;
2348         }
2349         if (dialogtoken != tap->txa_token) {
2350                 IEEE80211_DISCARD_MAC(vap,
2351                     IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2352                     ni->ni_macaddr, "ADDBA response",
2353                     "dialogtoken mismatch: waiting for %d, "
2354                     "received %d, tid %d code %d",
2355                     tap->txa_token, dialogtoken, tid, code);
2356                 vap->iv_stats.is_addba_badtoken++;
2357                 return 0;
2358         }
2359         /* NB: assumes IEEE80211_AGGR_IMMEDIATE is 1 */
2360         if (policy != (tap->txa_flags & IEEE80211_AGGR_IMMEDIATE)) {
2361                 IEEE80211_DISCARD_MAC(vap,
2362                     IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2363                     ni->ni_macaddr, "ADDBA response",
2364                     "policy mismatch: expecting %s, "
2365                     "received %s, tid %d code %d",
2366                     tap->txa_flags & IEEE80211_AGGR_IMMEDIATE,
2367                     policy, tid, code);
2368                 vap->iv_stats.is_addba_badpolicy++;
2369                 return 0;
2370         }
2371 #if 0
2372         /* XXX we take MIN in ieee80211_addba_response */
2373         if (bufsiz > IEEE80211_AGGR_BAWMAX) {
2374                 IEEE80211_DISCARD_MAC(vap,
2375                     IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2376                     ni->ni_macaddr, "ADDBA response",
2377                     "BA window too large: max %d, "
2378                     "received %d, tid %d code %d",
2379                     bufsiz, IEEE80211_AGGR_BAWMAX, tid, code);
2380                 vap->iv_stats.is_addba_badbawinsize++;
2381                 return 0;
2382         }
2383 #endif
2384
2385         IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2386             "recv ADDBA response: dialogtoken %u code %d "
2387             "baparamset 0x%x (tid %d bufsiz %d amsdu %d) batimeout %d",
2388             dialogtoken, code, baparamset, tid,
2389             bufsiz,
2390             amsdu,
2391             batimeout);
2392         ic->ic_addba_response(ni, tap, code, baparamset, batimeout);
2393         return 0;
2394 }
2395
2396 static int
2397 ht_recv_action_ba_delba(struct ieee80211_node *ni,
2398         const struct ieee80211_frame *wh,
2399         const uint8_t *frm, const uint8_t *efrm)
2400 {
2401         struct ieee80211com *ic = ni->ni_ic;
2402         struct ieee80211_rx_ampdu *rap;
2403         struct ieee80211_tx_ampdu *tap;
2404         uint16_t baparamset, code;
2405         int tid;
2406
2407         baparamset = le16dec(frm+2);
2408         code = le16dec(frm+4);
2409
2410         tid = MS(baparamset, IEEE80211_DELBAPS_TID);
2411
2412         IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2413             "recv DELBA: baparamset 0x%x (tid %d initiator %d) "
2414             "code %d", baparamset, tid,
2415             MS(baparamset, IEEE80211_DELBAPS_INIT), code);
2416
2417         if ((baparamset & IEEE80211_DELBAPS_INIT) == 0) {
2418                 tap = &ni->ni_tx_ampdu[tid];
2419                 ic->ic_addba_stop(ni, tap);
2420         } else {
2421                 rap = &ni->ni_rx_ampdu[tid];
2422                 ic->ic_ampdu_rx_stop(ni, rap);
2423         }
2424         return 0;
2425 }
2426
2427 static int
2428 ht_recv_action_ht_txchwidth(struct ieee80211_node *ni,
2429         const struct ieee80211_frame *wh,
2430         const uint8_t *frm, const uint8_t *efrm)
2431 {
2432         int chw;
2433
2434         chw = (frm[2] == IEEE80211_A_HT_TXCHWIDTH_2040) ? 40 : 20;
2435
2436         IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2437             "%s: HT txchwidth, width %d%s",
2438             __func__, chw, ni->ni_chw != chw ? "*" : "");
2439         if (chw != ni->ni_chw) {
2440                 /* XXX does this need to change the ht40 station count? */
2441                 ni->ni_chw = chw;
2442                 /* XXX notify on change */
2443         }
2444         return 0;
2445 }
2446
2447 static int
2448 ht_recv_action_ht_mimopwrsave(struct ieee80211_node *ni,
2449         const struct ieee80211_frame *wh,
2450         const uint8_t *frm, const uint8_t *efrm)
2451 {
2452         const struct ieee80211_action_ht_mimopowersave *mps =
2453             (const struct ieee80211_action_ht_mimopowersave *) frm;
2454
2455         /* XXX check iv_htcaps */
2456         if (mps->am_control & IEEE80211_A_HT_MIMOPWRSAVE_ENA)
2457                 ni->ni_flags |= IEEE80211_NODE_MIMO_PS;
2458         else
2459                 ni->ni_flags &= ~IEEE80211_NODE_MIMO_PS;
2460         if (mps->am_control & IEEE80211_A_HT_MIMOPWRSAVE_MODE)
2461                 ni->ni_flags |= IEEE80211_NODE_MIMO_RTS;
2462         else
2463                 ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS;
2464         /* XXX notify on change */
2465         IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2466             "%s: HT MIMO PS (%s%s)", __func__,
2467             (ni->ni_flags & IEEE80211_NODE_MIMO_PS) ?  "on" : "off",
2468             (ni->ni_flags & IEEE80211_NODE_MIMO_RTS) ?  "+rts" : ""
2469         );
2470         return 0;
2471 }
2472
2473 /*
2474  * Transmit processing.
2475  */
2476
2477 /*
2478  * Check if A-MPDU should be requested/enabled for a stream.
2479  * We require a traffic rate above a per-AC threshold and we
2480  * also handle backoff from previous failed attempts.
2481  *
2482  * Drivers may override this method to bring in information
2483  * such as link state conditions in making the decision.
2484  */
2485 static int
2486 ieee80211_ampdu_enable(struct ieee80211_node *ni,
2487         struct ieee80211_tx_ampdu *tap)
2488 {
2489         struct ieee80211vap *vap = ni->ni_vap;
2490
2491         if (tap->txa_avgpps <
2492             vap->iv_ampdu_mintraffic[TID_TO_WME_AC(tap->txa_tid)])
2493                 return 0;
2494         /* XXX check rssi? */
2495         if (tap->txa_attempts >= ieee80211_addba_maxtries &&
2496             ieee80211_time_after(ticks, tap->txa_nextrequest)) {
2497                 /*
2498                  * Don't retry too often; txa_nextrequest is set
2499                  * to the minimum interval we'll retry after
2500                  * ieee80211_addba_maxtries failed attempts are made.
2501                  */
2502                 return 0;
2503         }
2504         IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
2505             "enable AMPDU on tid %d (%s), avgpps %d pkts %d attempt %d",
2506             tap->txa_tid, ieee80211_wme_acnames[TID_TO_WME_AC(tap->txa_tid)],
2507             tap->txa_avgpps, tap->txa_pkts, tap->txa_attempts);
2508         return 1;
2509 }
2510
2511 /*
2512  * Request A-MPDU tx aggregation.  Setup local state and
2513  * issue an ADDBA request.  BA use will only happen after
2514  * the other end replies with ADDBA response.
2515  */
2516 int
2517 ieee80211_ampdu_request(struct ieee80211_node *ni,
2518         struct ieee80211_tx_ampdu *tap)
2519 {
2520         struct ieee80211com *ic = ni->ni_ic;
2521         uint16_t args[5];
2522         int tid, dialogtoken;
2523         static int tokens = 0;  /* XXX */
2524
2525         /* XXX locking */
2526         if ((tap->txa_flags & IEEE80211_AGGR_SETUP) == 0) {
2527                 /* do deferred setup of state */
2528                 ampdu_tx_setup(tap);
2529         }
2530         /* XXX hack for not doing proper locking */
2531         tap->txa_flags &= ~IEEE80211_AGGR_NAK;
2532
2533         dialogtoken = (tokens+1) % 63;          /* XXX */
2534         tid = tap->txa_tid;
2535
2536         /*
2537          * XXX TODO: This is racy with any other parallel TX going on. :(
2538          */
2539         tap->txa_start = ni->ni_txseqs[tid];
2540
2541         args[0] = dialogtoken;
2542         args[1] = 0;    /* NB: status code not used */
2543         args[2] = IEEE80211_BAPS_POLICY_IMMEDIATE
2544                 | SM(tid, IEEE80211_BAPS_TID)
2545                 | SM(IEEE80211_AGGR_BAWMAX, IEEE80211_BAPS_BUFSIZ)
2546                 ;
2547
2548         /* XXX TODO: this should be a flag, not iv_htcaps */
2549         if ((ni->ni_flags & IEEE80211_NODE_AMSDU_TX) &&
2550             (ni->ni_vap->iv_htcaps & IEEE80211_HTC_TX_AMSDU_AMPDU))
2551                 args[2] |= IEEE80211_BAPS_AMSDU;
2552
2553         args[3] = 0;    /* batimeout */
2554         /* NB: do first so there's no race against reply */
2555         if (!ic->ic_addba_request(ni, tap, dialogtoken, args[2], args[3])) {
2556                 /* unable to setup state, don't make request */
2557                 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2558                     ni, "%s: could not setup BA stream for TID %d AC %d",
2559                     __func__, tap->txa_tid, TID_TO_WME_AC(tap->txa_tid));
2560                 /* defer next try so we don't slam the driver with requests */
2561                 tap->txa_attempts = ieee80211_addba_maxtries;
2562                 /* NB: check in case driver wants to override */
2563                 if (tap->txa_nextrequest <= ticks)
2564                         tap->txa_nextrequest = ticks + ieee80211_addba_backoff;
2565                 return 0;
2566         }
2567         tokens = dialogtoken;                   /* allocate token */
2568         /* NB: after calling ic_addba_request so driver can set txa_start */
2569         args[4] = SM(tap->txa_start, IEEE80211_BASEQ_START)
2570                 | SM(0, IEEE80211_BASEQ_FRAG)
2571                 ;
2572         return ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
2573                 IEEE80211_ACTION_BA_ADDBA_REQUEST, args);
2574 }
2575
2576 /*
2577  * Terminate an AMPDU tx stream.  State is reclaimed
2578  * and the peer notified with a DelBA Action frame.
2579  */
2580 void
2581 ieee80211_ampdu_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
2582         int reason)
2583 {
2584         struct ieee80211com *ic = ni->ni_ic;
2585         struct ieee80211vap *vap = ni->ni_vap;
2586         uint16_t args[4];
2587
2588         /* XXX locking */
2589         tap->txa_flags &= ~IEEE80211_AGGR_BARPEND;
2590         if (IEEE80211_AMPDU_RUNNING(tap)) {
2591                 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2592                     ni, "%s: stop BA stream for TID %d (reason: %d (%s))",
2593                     __func__, tap->txa_tid, reason,
2594                     ieee80211_reason_to_string(reason));
2595                 vap->iv_stats.is_ampdu_stop++;
2596
2597                 ic->ic_addba_stop(ni, tap);
2598                 args[0] = tap->txa_tid;
2599                 args[1] = IEEE80211_DELBAPS_INIT;
2600                 args[2] = reason;                       /* XXX reason code */
2601                 ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
2602                         IEEE80211_ACTION_BA_DELBA, args);
2603         } else {
2604                 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2605                     ni, "%s: BA stream for TID %d not running "
2606                     "(reason: %d (%s))", __func__, tap->txa_tid, reason,
2607                     ieee80211_reason_to_string(reason));
2608                 vap->iv_stats.is_ampdu_stop_failed++;
2609         }
2610 }
2611
2612 /* XXX */
2613 static void bar_start_timer(struct ieee80211_tx_ampdu *tap);
2614
2615 static void
2616 bar_timeout(void *arg)
2617 {
2618         struct ieee80211_tx_ampdu *tap = arg;
2619         struct ieee80211_node *ni = tap->txa_ni;
2620
2621         KASSERT((tap->txa_flags & IEEE80211_AGGR_XCHGPEND) == 0,
2622             ("bar/addba collision, flags 0x%x", tap->txa_flags));
2623
2624         IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2625             ni, "%s: tid %u flags 0x%x attempts %d", __func__,
2626             tap->txa_tid, tap->txa_flags, tap->txa_attempts);
2627
2628         /* guard against race with bar_tx_complete */
2629         if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) == 0)
2630                 return;
2631         /* XXX ? */
2632         if (tap->txa_attempts >= ieee80211_bar_maxtries) {
2633                 struct ieee80211com *ic = ni->ni_ic;
2634
2635                 ni->ni_vap->iv_stats.is_ampdu_bar_tx_fail++;
2636                 /*
2637                  * If (at least) the last BAR TX timeout was due to
2638                  * an ieee80211_send_bar() failures, then we need
2639                  * to make sure we notify the driver that a BAR
2640                  * TX did occur and fail.  This gives the driver
2641                  * a chance to undo any queue pause that may
2642                  * have occurred.
2643                  */
2644                 ic->ic_bar_response(ni, tap, 1);
2645                 ieee80211_ampdu_stop(ni, tap, IEEE80211_REASON_TIMEOUT);
2646         } else {
2647                 ni->ni_vap->iv_stats.is_ampdu_bar_tx_retry++;
2648                 if (ieee80211_send_bar(ni, tap, tap->txa_seqpending) != 0) {
2649                         IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2650                             ni, "%s: failed to TX, starting timer\n",
2651                             __func__);
2652                         /*
2653                          * If ieee80211_send_bar() fails here, the
2654                          * timer may have stopped and/or the pending
2655                          * flag may be clear.  Because of this,
2656                          * fake the BARPEND and reset the timer.
2657                          * A retransmission attempt will then occur
2658                          * during the next timeout.
2659                          */
2660                         /* XXX locking */
2661                         tap->txa_flags |= IEEE80211_AGGR_BARPEND;
2662                         bar_start_timer(tap);
2663                 }
2664         }
2665 }
2666
2667 static void
2668 bar_start_timer(struct ieee80211_tx_ampdu *tap)
2669 {
2670         IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2671             tap->txa_ni,
2672             "%s: called",
2673             __func__);
2674         callout_reset(&tap->txa_timer, ieee80211_bar_timeout, bar_timeout, tap);
2675 }
2676
2677 static void
2678 bar_stop_timer(struct ieee80211_tx_ampdu *tap)
2679 {
2680         IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2681             tap->txa_ni,
2682             "%s: called",
2683             __func__);
2684         callout_stop(&tap->txa_timer);
2685 }
2686
2687 static void
2688 bar_tx_complete(struct ieee80211_node *ni, void *arg, int status)
2689 {
2690         struct ieee80211_tx_ampdu *tap = arg;
2691
2692         IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2693             ni, "%s: tid %u flags 0x%x pending %d status %d",
2694             __func__, tap->txa_tid, tap->txa_flags,
2695             callout_pending(&tap->txa_timer), status);
2696
2697         ni->ni_vap->iv_stats.is_ampdu_bar_tx++;
2698         /* XXX locking */
2699         if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) &&
2700             callout_pending(&tap->txa_timer)) {
2701                 struct ieee80211com *ic = ni->ni_ic;
2702
2703                 if (status == 0)                /* ACK'd */
2704                         bar_stop_timer(tap);
2705                 ic->ic_bar_response(ni, tap, status);
2706                 /* NB: just let timer expire so we pace requests */
2707         }
2708 }
2709
2710 static void
2711 ieee80211_bar_response(struct ieee80211_node *ni,
2712         struct ieee80211_tx_ampdu *tap, int status)
2713 {
2714
2715         IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2716             tap->txa_ni,
2717             "%s: called",
2718             __func__);
2719         if (status == 0) {              /* got ACK */
2720                 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2721                     ni, "BAR moves BA win <%u:%u> (%u frames) txseq %u tid %u",
2722                     tap->txa_start,
2723                     IEEE80211_SEQ_ADD(tap->txa_start, tap->txa_wnd-1),
2724                     tap->txa_qframes, tap->txa_seqpending,
2725                     tap->txa_tid);
2726
2727                 /* NB: timer already stopped in bar_tx_complete */
2728                 tap->txa_start = tap->txa_seqpending;
2729                 tap->txa_flags &= ~IEEE80211_AGGR_BARPEND;
2730         }
2731 }
2732
2733 /*
2734  * Transmit a BAR frame to the specified node.  The
2735  * BAR contents are drawn from the supplied aggregation
2736  * state associated with the node.
2737  *
2738  * NB: we only handle immediate ACK w/ compressed bitmap.
2739  */
2740 int
2741 ieee80211_send_bar(struct ieee80211_node *ni,
2742         struct ieee80211_tx_ampdu *tap, ieee80211_seq seq)
2743 {
2744 #define senderr(_x, _v) do { vap->iv_stats._v++; ret = _x; goto bad; } while (0)
2745         struct ieee80211vap *vap = ni->ni_vap;
2746         struct ieee80211com *ic = ni->ni_ic;
2747         struct ieee80211_frame_bar *bar;
2748         struct mbuf *m;
2749         uint16_t barctl, barseqctl;
2750         uint8_t *frm;
2751         int tid, ret;
2752
2753
2754         IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2755             tap->txa_ni,
2756             "%s: called",
2757             __func__);
2758
2759         if ((tap->txa_flags & IEEE80211_AGGR_RUNNING) == 0) {
2760                 /* no ADDBA response, should not happen */
2761                 /* XXX stat+msg */
2762                 return EINVAL;
2763         }
2764         /* XXX locking */
2765         bar_stop_timer(tap);
2766
2767         ieee80211_ref_node(ni);
2768
2769         m = ieee80211_getmgtframe(&frm, ic->ic_headroom, sizeof(*bar));
2770         if (m == NULL)
2771                 senderr(ENOMEM, is_tx_nobuf);
2772
2773         if (!ieee80211_add_callback(m, bar_tx_complete, tap)) {
2774                 m_freem(m);
2775                 senderr(ENOMEM, is_tx_nobuf);   /* XXX */
2776                 /* NOTREACHED */
2777         }
2778
2779         bar = mtod(m, struct ieee80211_frame_bar *);
2780         bar->i_fc[0] = IEEE80211_FC0_VERSION_0 |
2781                 IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_BAR;
2782         bar->i_fc[1] = 0;
2783         IEEE80211_ADDR_COPY(bar->i_ra, ni->ni_macaddr);
2784         IEEE80211_ADDR_COPY(bar->i_ta, vap->iv_myaddr);
2785
2786         tid = tap->txa_tid;
2787         barctl  = (tap->txa_flags & IEEE80211_AGGR_IMMEDIATE ?
2788                         0 : IEEE80211_BAR_NOACK)
2789                 | IEEE80211_BAR_COMP
2790                 | SM(tid, IEEE80211_BAR_TID)
2791                 ;
2792         barseqctl = SM(seq, IEEE80211_BAR_SEQ_START);
2793         /* NB: known to have proper alignment */
2794         bar->i_ctl = htole16(barctl);
2795         bar->i_seq = htole16(barseqctl);
2796         m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_bar);
2797
2798         M_WME_SETAC(m, WME_AC_VO);
2799
2800         IEEE80211_NODE_STAT(ni, tx_mgmt);       /* XXX tx_ctl? */
2801
2802         /* XXX locking */
2803         /* init/bump attempts counter */
2804         if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) == 0)
2805                 tap->txa_attempts = 1;
2806         else
2807                 tap->txa_attempts++;
2808         tap->txa_seqpending = seq;
2809         tap->txa_flags |= IEEE80211_AGGR_BARPEND;
2810
2811         IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_11N,
2812             ni, "send BAR: tid %u ctl 0x%x start %u (attempt %d)",
2813             tid, barctl, seq, tap->txa_attempts);
2814
2815         /*
2816          * ic_raw_xmit will free the node reference
2817          * regardless of queue/TX success or failure.
2818          */
2819         IEEE80211_TX_LOCK(ic);
2820         ret = ieee80211_raw_output(vap, ni, m, NULL);
2821         IEEE80211_TX_UNLOCK(ic);
2822         if (ret != 0) {
2823                 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_11N,
2824                     ni, "send BAR: failed: (ret = %d)\n",
2825                     ret);
2826                 /* xmit failed, clear state flag */
2827                 tap->txa_flags &= ~IEEE80211_AGGR_BARPEND;
2828                 vap->iv_stats.is_ampdu_bar_tx_fail++;
2829                 return ret;
2830         }
2831         /* XXX hack against tx complete happening before timer is started */
2832         if (tap->txa_flags & IEEE80211_AGGR_BARPEND)
2833                 bar_start_timer(tap);
2834         return 0;
2835 bad:
2836         IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2837             tap->txa_ni,
2838             "%s: bad! ret=%d",
2839             __func__, ret);
2840         vap->iv_stats.is_ampdu_bar_tx_fail++;
2841         ieee80211_free_node(ni);
2842         return ret;
2843 #undef senderr
2844 }
2845
2846 static int
2847 ht_action_output(struct ieee80211_node *ni, struct mbuf *m)
2848 {
2849         struct ieee80211_bpf_params params;
2850
2851         memset(&params, 0, sizeof(params));
2852         params.ibp_pri = WME_AC_VO;
2853         params.ibp_rate0 = ni->ni_txparms->mgmtrate;
2854         /* NB: we know all frames are unicast */
2855         params.ibp_try0 = ni->ni_txparms->maxretry;
2856         params.ibp_power = ni->ni_txpower;
2857         return ieee80211_mgmt_output(ni, m, IEEE80211_FC0_SUBTYPE_ACTION,
2858              &params);
2859 }
2860
2861 #define ADDSHORT(frm, v) do {                   \
2862         frm[0] = (v) & 0xff;                    \
2863         frm[1] = (v) >> 8;                      \
2864         frm += 2;                               \
2865 } while (0)
2866
2867 /*
2868  * Send an action management frame.  The arguments are stuff
2869  * into a frame without inspection; the caller is assumed to
2870  * prepare them carefully (e.g. based on the aggregation state).
2871  */
2872 static int
2873 ht_send_action_ba_addba(struct ieee80211_node *ni,
2874         int category, int action, void *arg0)
2875 {
2876         struct ieee80211vap *vap = ni->ni_vap;
2877         struct ieee80211com *ic = ni->ni_ic;
2878         uint16_t *args = arg0;
2879         struct mbuf *m;
2880         uint8_t *frm;
2881
2882         IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2883             "send ADDBA %s: dialogtoken %d status %d "
2884             "baparamset 0x%x (tid %d amsdu %d) batimeout 0x%x baseqctl 0x%x",
2885             (action == IEEE80211_ACTION_BA_ADDBA_REQUEST) ?
2886                 "request" : "response",
2887             args[0], args[1], args[2], MS(args[2], IEEE80211_BAPS_TID),
2888             MS(args[2], IEEE80211_BAPS_AMSDU), args[3], args[4]);
2889
2890         IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2891             "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2892             ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2893         ieee80211_ref_node(ni);
2894
2895         m = ieee80211_getmgtframe(&frm,
2896             ic->ic_headroom + sizeof(struct ieee80211_frame),
2897             sizeof(uint16_t)    /* action+category */
2898             /* XXX may action payload */
2899             + sizeof(struct ieee80211_action_ba_addbaresponse)
2900         );
2901         if (m != NULL) {
2902                 *frm++ = category;
2903                 *frm++ = action;
2904                 *frm++ = args[0];               /* dialog token */
2905                 if (action == IEEE80211_ACTION_BA_ADDBA_RESPONSE)
2906                         ADDSHORT(frm, args[1]); /* status code */
2907                 ADDSHORT(frm, args[2]);         /* baparamset */
2908                 ADDSHORT(frm, args[3]);         /* batimeout */
2909                 if (action == IEEE80211_ACTION_BA_ADDBA_REQUEST)
2910                         ADDSHORT(frm, args[4]); /* baseqctl */
2911                 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2912                 return ht_action_output(ni, m);
2913         } else {
2914                 vap->iv_stats.is_tx_nobuf++;
2915                 ieee80211_free_node(ni);
2916                 return ENOMEM;
2917         }
2918 }
2919
2920 static int
2921 ht_send_action_ba_delba(struct ieee80211_node *ni,
2922         int category, int action, void *arg0)
2923 {
2924         struct ieee80211vap *vap = ni->ni_vap;
2925         struct ieee80211com *ic = ni->ni_ic;
2926         uint16_t *args = arg0;
2927         struct mbuf *m;
2928         uint16_t baparamset;
2929         uint8_t *frm;
2930
2931         baparamset = SM(args[0], IEEE80211_DELBAPS_TID)
2932                    | args[1]
2933                    ;
2934         IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2935             "send DELBA action: tid %d, initiator %d reason %d (%s)",
2936             args[0], args[1], args[2], ieee80211_reason_to_string(args[2]));
2937
2938         IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2939             "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2940             ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2941         ieee80211_ref_node(ni);
2942
2943         m = ieee80211_getmgtframe(&frm,
2944             ic->ic_headroom + sizeof(struct ieee80211_frame),
2945             sizeof(uint16_t)    /* action+category */
2946             /* XXX may action payload */
2947             + sizeof(struct ieee80211_action_ba_addbaresponse)
2948         );
2949         if (m != NULL) {
2950                 *frm++ = category;
2951                 *frm++ = action;
2952                 ADDSHORT(frm, baparamset);
2953                 ADDSHORT(frm, args[2]);         /* reason code */
2954                 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2955                 return ht_action_output(ni, m);
2956         } else {
2957                 vap->iv_stats.is_tx_nobuf++;
2958                 ieee80211_free_node(ni);
2959                 return ENOMEM;
2960         }
2961 }
2962
2963 static int
2964 ht_send_action_ht_txchwidth(struct ieee80211_node *ni,
2965         int category, int action, void *arg0)
2966 {
2967         struct ieee80211vap *vap = ni->ni_vap;
2968         struct ieee80211com *ic = ni->ni_ic;
2969         struct mbuf *m;
2970         uint8_t *frm;
2971
2972         IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2973             "send HT txchwidth: width %d",
2974             IEEE80211_IS_CHAN_HT40(ni->ni_chan) ? 40 : 20);
2975
2976         IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2977             "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2978             ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2979         ieee80211_ref_node(ni);
2980
2981         m = ieee80211_getmgtframe(&frm,
2982             ic->ic_headroom + sizeof(struct ieee80211_frame),
2983             sizeof(uint16_t)    /* action+category */
2984             /* XXX may action payload */
2985             + sizeof(struct ieee80211_action_ba_addbaresponse)
2986         );
2987         if (m != NULL) {
2988                 *frm++ = category;
2989                 *frm++ = action;
2990                 *frm++ = IEEE80211_IS_CHAN_HT40(ni->ni_chan) ? 
2991                         IEEE80211_A_HT_TXCHWIDTH_2040 :
2992                         IEEE80211_A_HT_TXCHWIDTH_20;
2993                 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2994                 return ht_action_output(ni, m);
2995         } else {
2996                 vap->iv_stats.is_tx_nobuf++;
2997                 ieee80211_free_node(ni);
2998                 return ENOMEM;
2999         }
3000 }
3001 #undef ADDSHORT
3002
3003 /*
3004  * Construct the MCS bit mask for inclusion in an HT capabilities
3005  * information element.
3006  */
3007 static void
3008 ieee80211_set_mcsset(struct ieee80211com *ic, uint8_t *frm)
3009 {
3010         int i;
3011         uint8_t txparams;
3012
3013         KASSERT((ic->ic_rxstream > 0 && ic->ic_rxstream <= 4),
3014             ("ic_rxstream %d out of range", ic->ic_rxstream));
3015         KASSERT((ic->ic_txstream > 0 && ic->ic_txstream <= 4),
3016             ("ic_txstream %d out of range", ic->ic_txstream));
3017
3018         for (i = 0; i < ic->ic_rxstream * 8; i++)
3019                 setbit(frm, i);
3020         if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
3021             (ic->ic_htcaps & IEEE80211_HTC_RXMCS32))
3022                 setbit(frm, 32);
3023         if (ic->ic_htcaps & IEEE80211_HTC_RXUNEQUAL) {
3024                 if (ic->ic_rxstream >= 2) {
3025                         for (i = 33; i <= 38; i++)
3026                                 setbit(frm, i);
3027                 }
3028                 if (ic->ic_rxstream >= 3) {
3029                         for (i = 39; i <= 52; i++)
3030                                 setbit(frm, i);
3031                 }
3032                 if (ic->ic_txstream >= 4) {
3033                         for (i = 53; i <= 76; i++)
3034                                 setbit(frm, i);
3035                 }
3036         }
3037
3038         if (ic->ic_rxstream != ic->ic_txstream) {
3039                 txparams = 0x1;                 /* TX MCS set defined */
3040                 txparams |= 0x2;                /* TX RX MCS not equal */
3041                 txparams |= (ic->ic_txstream - 1) << 2; /* num TX streams */
3042                 if (ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL)
3043                         txparams |= 0x16;       /* TX unequal modulation sup */
3044         } else
3045                 txparams = 0;
3046         frm[12] = txparams;
3047 }
3048
3049 /*
3050  * Add body of an HTCAP information element.
3051  */
3052 static uint8_t *
3053 ieee80211_add_htcap_body(uint8_t *frm, struct ieee80211_node *ni)
3054 {
3055 #define ADDSHORT(frm, v) do {                   \
3056         frm[0] = (v) & 0xff;                    \
3057         frm[1] = (v) >> 8;                      \
3058         frm += 2;                               \
3059 } while (0)
3060         struct ieee80211com *ic = ni->ni_ic;
3061         struct ieee80211vap *vap = ni->ni_vap;
3062         uint16_t caps, extcaps;
3063         int rxmax, density;
3064
3065         /* HT capabilities */
3066         caps = vap->iv_htcaps & 0xffff;
3067         /*
3068          * Note channel width depends on whether we are operating as
3069          * a sta or not.  When operating as a sta we are generating
3070          * a request based on our desired configuration.  Otherwise
3071          * we are operational and the channel attributes identify
3072          * how we've been setup (which might be different if a fixed
3073          * channel is specified).
3074          */
3075         if (vap->iv_opmode == IEEE80211_M_STA) {
3076                 /* override 20/40 use based on config */
3077                 if (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)
3078                         caps |= IEEE80211_HTCAP_CHWIDTH40;
3079                 else
3080                         caps &= ~IEEE80211_HTCAP_CHWIDTH40;
3081
3082                 /* Start by using the advertised settings */
3083                 rxmax = MS(ni->ni_htparam, IEEE80211_HTCAP_MAXRXAMPDU);
3084                 density = MS(ni->ni_htparam, IEEE80211_HTCAP_MPDUDENSITY);
3085
3086                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_11N,
3087                     "%s: advertised rxmax=%d, density=%d, vap rxmax=%d, density=%d\n",
3088                     __func__,
3089                     rxmax,
3090                     density,
3091                     vap->iv_ampdu_rxmax,
3092                     vap->iv_ampdu_density);
3093
3094                 /* Cap at VAP rxmax */
3095                 if (rxmax > vap->iv_ampdu_rxmax)
3096                         rxmax = vap->iv_ampdu_rxmax;
3097
3098                 /*
3099                  * If the VAP ampdu density value greater, use that.
3100                  *
3101                  * (Larger density value == larger minimum gap between A-MPDU
3102                  * subframes.)
3103                  */
3104                 if (vap->iv_ampdu_density > density)
3105                         density = vap->iv_ampdu_density;
3106
3107                 /*
3108                  * NB: Hardware might support HT40 on some but not all
3109                  * channels. We can't determine this earlier because only
3110                  * after association the channel is upgraded to HT based
3111                  * on the negotiated capabilities.
3112                  */
3113                 if (ni->ni_chan != IEEE80211_CHAN_ANYC &&
3114                     findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT40U) == NULL &&
3115                     findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT40D) == NULL)
3116                         caps &= ~IEEE80211_HTCAP_CHWIDTH40;
3117         } else {
3118                 /* override 20/40 use based on current channel */
3119                 if (IEEE80211_IS_CHAN_HT40(ni->ni_chan))
3120                         caps |= IEEE80211_HTCAP_CHWIDTH40;
3121                 else
3122                         caps &= ~IEEE80211_HTCAP_CHWIDTH40;
3123
3124                 /* XXX TODO should it start by using advertised settings? */
3125                 rxmax = vap->iv_ampdu_rxmax;
3126                 density = vap->iv_ampdu_density;
3127         }
3128
3129         /* adjust short GI based on channel and config */
3130         if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20) == 0)
3131                 caps &= ~IEEE80211_HTCAP_SHORTGI20;
3132         if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40) == 0 ||
3133             (caps & IEEE80211_HTCAP_CHWIDTH40) == 0)
3134                 caps &= ~IEEE80211_HTCAP_SHORTGI40;
3135
3136         /* adjust STBC based on receive capabilities */
3137         if ((vap->iv_flags_ht & IEEE80211_FHT_STBC_RX) == 0)
3138                 caps &= ~IEEE80211_HTCAP_RXSTBC;
3139
3140         /* adjust LDPC based on receive capabilites */
3141         if ((vap->iv_flags_ht & IEEE80211_FHT_LDPC_RX) == 0)
3142                 caps &= ~IEEE80211_HTCAP_LDPC;
3143
3144         ADDSHORT(frm, caps);
3145
3146         /* HT parameters */
3147         *frm = SM(rxmax, IEEE80211_HTCAP_MAXRXAMPDU)
3148              | SM(density, IEEE80211_HTCAP_MPDUDENSITY)
3149              ;
3150         frm++;
3151
3152         /* pre-zero remainder of ie */
3153         memset(frm, 0, sizeof(struct ieee80211_ie_htcap) - 
3154                 __offsetof(struct ieee80211_ie_htcap, hc_mcsset));
3155
3156         /* supported MCS set */
3157         /*
3158          * XXX: For sta mode the rate set should be restricted based
3159          * on the AP's capabilities, but ni_htrates isn't setup when
3160          * we're called to form an AssocReq frame so for now we're
3161          * restricted to the device capabilities.
3162          */
3163         ieee80211_set_mcsset(ni->ni_ic, frm);
3164
3165         frm += __offsetof(struct ieee80211_ie_htcap, hc_extcap) -
3166                 __offsetof(struct ieee80211_ie_htcap, hc_mcsset);
3167
3168         /* HT extended capabilities */
3169         extcaps = vap->iv_htextcaps & 0xffff;
3170
3171         ADDSHORT(frm, extcaps);
3172
3173         frm += sizeof(struct ieee80211_ie_htcap) -
3174                 __offsetof(struct ieee80211_ie_htcap, hc_txbf);
3175
3176         return frm;
3177 #undef ADDSHORT
3178 }
3179
3180 /*
3181  * Add 802.11n HT capabilities information element
3182  */
3183 uint8_t *
3184 ieee80211_add_htcap(uint8_t *frm, struct ieee80211_node *ni)
3185 {
3186         frm[0] = IEEE80211_ELEMID_HTCAP;
3187         frm[1] = sizeof(struct ieee80211_ie_htcap) - 2;
3188         return ieee80211_add_htcap_body(frm + 2, ni);
3189 }
3190
3191 /*
3192  * Non-associated probe request - add HT capabilities based on
3193  * the current channel configuration.
3194  */
3195 static uint8_t *
3196 ieee80211_add_htcap_body_ch(uint8_t *frm, struct ieee80211vap *vap,
3197     struct ieee80211_channel *c)
3198 {
3199 #define ADDSHORT(frm, v) do {                   \
3200         frm[0] = (v) & 0xff;                    \
3201         frm[1] = (v) >> 8;                      \
3202         frm += 2;                               \
3203 } while (0)
3204         struct ieee80211com *ic = vap->iv_ic;
3205         uint16_t caps, extcaps;
3206         int rxmax, density;
3207
3208         /* HT capabilities */
3209         caps = vap->iv_htcaps & 0xffff;
3210
3211         /*
3212          * We don't use this in STA mode; only in IBSS mode.
3213          * So in IBSS mode we base our HTCAP flags on the
3214          * given channel.
3215          */
3216
3217         /* override 20/40 use based on current channel */
3218         if (IEEE80211_IS_CHAN_HT40(c))
3219                 caps |= IEEE80211_HTCAP_CHWIDTH40;
3220         else
3221                 caps &= ~IEEE80211_HTCAP_CHWIDTH40;
3222
3223         /* Use the currently configured values */
3224         rxmax = vap->iv_ampdu_rxmax;
3225         density = vap->iv_ampdu_density;
3226
3227         /* adjust short GI based on channel and config */
3228         if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20) == 0)
3229                 caps &= ~IEEE80211_HTCAP_SHORTGI20;
3230         if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40) == 0 ||
3231             (caps & IEEE80211_HTCAP_CHWIDTH40) == 0)
3232                 caps &= ~IEEE80211_HTCAP_SHORTGI40;
3233         ADDSHORT(frm, caps);
3234
3235         /* HT parameters */
3236         *frm = SM(rxmax, IEEE80211_HTCAP_MAXRXAMPDU)
3237              | SM(density, IEEE80211_HTCAP_MPDUDENSITY)
3238              ;
3239         frm++;
3240
3241         /* pre-zero remainder of ie */
3242         memset(frm, 0, sizeof(struct ieee80211_ie_htcap) - 
3243                 __offsetof(struct ieee80211_ie_htcap, hc_mcsset));
3244
3245         /* supported MCS set */
3246         /*
3247          * XXX: For sta mode the rate set should be restricted based
3248          * on the AP's capabilities, but ni_htrates isn't setup when
3249          * we're called to form an AssocReq frame so for now we're
3250          * restricted to the device capabilities.
3251          */
3252         ieee80211_set_mcsset(ic, frm);
3253
3254         frm += __offsetof(struct ieee80211_ie_htcap, hc_extcap) -
3255                 __offsetof(struct ieee80211_ie_htcap, hc_mcsset);
3256
3257         /* HT extended capabilities */
3258         extcaps = vap->iv_htextcaps & 0xffff;
3259
3260         ADDSHORT(frm, extcaps);
3261
3262         frm += sizeof(struct ieee80211_ie_htcap) -
3263                 __offsetof(struct ieee80211_ie_htcap, hc_txbf);
3264
3265         return frm;
3266 #undef ADDSHORT
3267 }
3268
3269 /*
3270  * Add 802.11n HT capabilities information element
3271  */
3272 uint8_t *
3273 ieee80211_add_htcap_ch(uint8_t *frm, struct ieee80211vap *vap,
3274     struct ieee80211_channel *c)
3275 {
3276         frm[0] = IEEE80211_ELEMID_HTCAP;
3277         frm[1] = sizeof(struct ieee80211_ie_htcap) - 2;
3278         return ieee80211_add_htcap_body_ch(frm + 2, vap, c);
3279 }
3280
3281 /*
3282  * Add Broadcom OUI wrapped standard HTCAP ie; this is
3283  * used for compatibility w/ pre-draft implementations.
3284  */
3285 uint8_t *
3286 ieee80211_add_htcap_vendor(uint8_t *frm, struct ieee80211_node *ni)
3287 {
3288         frm[0] = IEEE80211_ELEMID_VENDOR;
3289         frm[1] = 4 + sizeof(struct ieee80211_ie_htcap) - 2;
3290         frm[2] = (BCM_OUI >> 0) & 0xff;
3291         frm[3] = (BCM_OUI >> 8) & 0xff;
3292         frm[4] = (BCM_OUI >> 16) & 0xff;
3293         frm[5] = BCM_OUI_HTCAP;
3294         return ieee80211_add_htcap_body(frm + 6, ni);
3295 }
3296
3297 /*
3298  * Construct the MCS bit mask of basic rates
3299  * for inclusion in an HT information element.
3300  */
3301 static void
3302 ieee80211_set_basic_htrates(uint8_t *frm, const struct ieee80211_htrateset *rs)
3303 {
3304         int i;
3305
3306         for (i = 0; i < rs->rs_nrates; i++) {
3307                 int r = rs->rs_rates[i] & IEEE80211_RATE_VAL;
3308                 if ((rs->rs_rates[i] & IEEE80211_RATE_BASIC) &&
3309                     r < IEEE80211_HTRATE_MAXSIZE) {
3310                         /* NB: this assumes a particular implementation */
3311                         setbit(frm, r);
3312                 }
3313         }
3314 }
3315
3316 /*
3317  * Update the HTINFO ie for a beacon frame.
3318  */
3319 void
3320 ieee80211_ht_update_beacon(struct ieee80211vap *vap,
3321         struct ieee80211_beacon_offsets *bo)
3322 {
3323 #define PROTMODE        (IEEE80211_HTINFO_OPMODE|IEEE80211_HTINFO_NONHT_PRESENT)
3324         struct ieee80211_node *ni;
3325         const struct ieee80211_channel *bsschan;
3326         struct ieee80211com *ic = vap->iv_ic;
3327         struct ieee80211_ie_htinfo *ht =
3328            (struct ieee80211_ie_htinfo *) bo->bo_htinfo;
3329
3330         ni = ieee80211_ref_node(vap->iv_bss);
3331         bsschan = ni->ni_chan;
3332
3333         /* XXX only update on channel change */
3334         ht->hi_ctrlchannel = ieee80211_chan2ieee(ic, bsschan);
3335         if (vap->iv_flags_ht & IEEE80211_FHT_RIFS)
3336                 ht->hi_byte1 = IEEE80211_HTINFO_RIFSMODE_PERM;
3337         else
3338                 ht->hi_byte1 = IEEE80211_HTINFO_RIFSMODE_PROH;
3339         if (IEEE80211_IS_CHAN_HT40U(bsschan))
3340                 ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_ABOVE;
3341         else if (IEEE80211_IS_CHAN_HT40D(bsschan))
3342                 ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_BELOW;
3343         else
3344                 ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_NONE;
3345         if (IEEE80211_IS_CHAN_HT40(bsschan))
3346                 ht->hi_byte1 |= IEEE80211_HTINFO_TXWIDTH_2040;
3347
3348         /* protection mode */
3349         ht->hi_byte2 = (ht->hi_byte2 &~ PROTMODE) | ic->ic_curhtprotmode;
3350
3351         ieee80211_free_node(ni);
3352
3353         /* XXX propagate to vendor ie's */
3354 #undef PROTMODE
3355 }
3356
3357 /*
3358  * Add body of an HTINFO information element.
3359  *
3360  * NB: We don't use struct ieee80211_ie_htinfo because we can
3361  * be called to fillin both a standard ie and a compat ie that
3362  * has a vendor OUI at the front.
3363  */
3364 static uint8_t *
3365 ieee80211_add_htinfo_body(uint8_t *frm, struct ieee80211_node *ni)
3366 {
3367         struct ieee80211vap *vap = ni->ni_vap;
3368         struct ieee80211com *ic = ni->ni_ic;
3369
3370         /* pre-zero remainder of ie */
3371         memset(frm, 0, sizeof(struct ieee80211_ie_htinfo) - 2);
3372
3373         /* primary/control channel center */
3374         *frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
3375
3376         if (vap->iv_flags_ht & IEEE80211_FHT_RIFS)
3377                 frm[0] = IEEE80211_HTINFO_RIFSMODE_PERM;
3378         else
3379                 frm[0] = IEEE80211_HTINFO_RIFSMODE_PROH;
3380         if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan))
3381                 frm[0] |= IEEE80211_HTINFO_2NDCHAN_ABOVE;
3382         else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan))
3383                 frm[0] |= IEEE80211_HTINFO_2NDCHAN_BELOW;
3384         else
3385                 frm[0] |= IEEE80211_HTINFO_2NDCHAN_NONE;
3386         if (IEEE80211_IS_CHAN_HT40(ni->ni_chan))
3387                 frm[0] |= IEEE80211_HTINFO_TXWIDTH_2040;
3388
3389         frm[1] = ic->ic_curhtprotmode;
3390
3391         frm += 5;
3392
3393         /* basic MCS set */
3394         ieee80211_set_basic_htrates(frm, &ni->ni_htrates);
3395         frm += sizeof(struct ieee80211_ie_htinfo) -
3396                 __offsetof(struct ieee80211_ie_htinfo, hi_basicmcsset);
3397         return frm;
3398 }
3399
3400 /*
3401  * Add 802.11n HT information element.
3402  */
3403 uint8_t *
3404 ieee80211_add_htinfo(uint8_t *frm, struct ieee80211_node *ni)
3405 {
3406         frm[0] = IEEE80211_ELEMID_HTINFO;
3407         frm[1] = sizeof(struct ieee80211_ie_htinfo) - 2;
3408         return ieee80211_add_htinfo_body(frm + 2, ni);
3409 }
3410
3411 /*
3412  * Add Broadcom OUI wrapped standard HTINFO ie; this is
3413  * used for compatibility w/ pre-draft implementations.
3414  */
3415 uint8_t *
3416 ieee80211_add_htinfo_vendor(uint8_t *frm, struct ieee80211_node *ni)
3417 {
3418         frm[0] = IEEE80211_ELEMID_VENDOR;
3419         frm[1] = 4 + sizeof(struct ieee80211_ie_htinfo) - 2;
3420         frm[2] = (BCM_OUI >> 0) & 0xff;
3421         frm[3] = (BCM_OUI >> 8) & 0xff;
3422         frm[4] = (BCM_OUI >> 16) & 0xff;
3423         frm[5] = BCM_OUI_HTINFO;
3424         return ieee80211_add_htinfo_body(frm + 6, ni);
3425 }