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