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