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