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