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