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