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