ieee80211_ht.c revision 183245
150276Speter/*-
262449Speter * Copyright (c) 2007-2008 Sam Leffler, Errno Consulting
350276Speter * All rights reserved.
450276Speter *
550276Speter * Redistribution and use in source and binary forms, with or without
650276Speter * modification, are permitted provided that the following conditions
750276Speter * are met:
850276Speter * 1. Redistributions of source code must retain the above copyright
950276Speter *    notice, this list of conditions and the following disclaimer.
1050276Speter * 2. Redistributions in binary form must reproduce the above copyright
1150276Speter *    notice, this list of conditions and the following disclaimer in the
1250276Speter *    documentation and/or other materials provided with the distribution.
1350276Speter *
1450276Speter * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1550276Speter * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1650276Speter * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1750276Speter * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1850276Speter * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1950276Speter * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2050276Speter * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2150276Speter * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2250276Speter * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2350276Speter * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2450276Speter */
2550276Speter
2650276Speter#include <sys/cdefs.h>
2750276Speter#ifdef __FreeBSD__
2850276Speter__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_ht.c 183245 2008-09-21 22:22:28Z sam $");
2950276Speter#endif
3050276Speter
3150276Speter/*
3250276Speter * IEEE 802.11n protocol support.
3350276Speter */
3462449Speter
3550276Speter#include "opt_inet.h"
3650276Speter#include "opt_wlan.h"
3750276Speter
3850276Speter#include <sys/param.h>
3950276Speter#include <sys/kernel.h>
4050276Speter#include <sys/systm.h>
4150276Speter#include <sys/endian.h>
4250276Speter
4350276Speter#include <sys/socket.h>
4450276Speter
4550276Speter#include <net/if.h>
4650276Speter#include <net/if_media.h>
4750276Speter#include <net/ethernet.h>
4850276Speter
4950276Speter#include <net80211/ieee80211_var.h>
5062449Speter#include <net80211/ieee80211_input.h>
5150276Speter
5250276Speter/* define here, used throughout file */
5350276Speter#define	MS(_v, _f)	(((_v) & _f) >> _f##_S)
5450276Speter#define	SM(_v, _f)	(((_v) << _f##_S) & _f)
5550276Speter
5650276Speterconst struct ieee80211_mcs_rates ieee80211_htrates[16] = {
5762449Speter	{  13,  14,  27,  30 },	/* MCS 0 */
5862449Speter	{  26,  29,  54,  60 },	/* MCS 1 */
5950276Speter	{  39,  43,  81,  90 },	/* MCS 2 */
6050276Speter	{  52,  58, 108, 120 },	/* MCS 3 */
6150276Speter	{  78,  87, 162, 180 },	/* MCS 4 */
6262449Speter	{ 104, 116, 216, 240 },	/* MCS 5 */
6362449Speter	{ 117, 130, 243, 270 },	/* MCS 6 */
6450276Speter	{ 130, 144, 270, 300 },	/* MCS 7 */
6550276Speter	{  26,  29,  54,  60 },	/* MCS 8 */
6650276Speter	{  52,  58, 108, 120 },	/* MCS 9 */
6750276Speter	{  78,  87, 162, 180 },	/* MCS 10 */
6850276Speter	{ 104, 116, 216, 240 },	/* MCS 11 */
6950276Speter	{ 156, 173, 324, 360 },	/* MCS 12 */
7050276Speter	{ 208, 231, 432, 480 },	/* MCS 13 */
7150276Speter	{ 234, 260, 486, 540 },	/* MCS 14 */
7250276Speter	{ 260, 289, 540, 600 }	/* MCS 15 */
7350276Speter};
7450276Speter
7550276Speterstatic const struct ieee80211_htrateset ieee80211_rateset_11n =
7650276Speter	{ 16, {
7750276Speter	          0,   1,   2,   3,   4,  5,   6,  7,  8,  9,
7850276Speter		 10,  11,  12,  13,  14,  15 }
7950276Speter	};
8050276Speter
8150276Speter#ifdef IEEE80211_AMPDU_AGE
8250276Speter/* XXX public for sysctl hookup */
8350276Speterint	ieee80211_ampdu_age = -1;	/* threshold for ampdu reorder q (ms) */
8450276Speter#endif
8550276Speterint	ieee80211_recv_bar_ena = 1;
8650276Speterint	ieee80211_addba_timeout = -1;	/* timeout waiting for ADDBA response */
8750276Speterint	ieee80211_addba_backoff = -1;	/* backoff after max ADDBA requests */
8850276Speterint	ieee80211_addba_maxtries = 3;	/* max ADDBA requests before backoff */
8950276Speter
9050276Speter/*
9150276Speter * Setup HT parameters that depends on the clock frequency.
9250276Speter */
9350276Speterstatic void
9450276Speterieee80211_ht_setup(void)
9550276Speter{
9650276Speter#ifdef IEEE80211_AMPDU_AGE
9750276Speter	ieee80211_ampdu_age = msecs_to_ticks(500);
9850276Speter#endif
9950276Speter	ieee80211_addba_timeout = msecs_to_ticks(250);
10050276Speter	ieee80211_addba_backoff = msecs_to_ticks(10*1000);
10150276Speter}
10250276SpeterSYSINIT(wlan_ht, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_ht_setup, NULL);
10350276Speter
10450276Speterstatic int ieee80211_ampdu_enable(struct ieee80211_node *ni,
10550276Speter	struct ieee80211_tx_ampdu *tap);
10650276Speterstatic int ieee80211_addba_request(struct ieee80211_node *ni,
10750276Speter	struct ieee80211_tx_ampdu *tap,
10850276Speter	int dialogtoken, int baparamset, int batimeout);
10950276Speterstatic int ieee80211_addba_response(struct ieee80211_node *ni,
11050276Speter	struct ieee80211_tx_ampdu *tap,
11150276Speter	int code, int baparamset, int batimeout);
11250276Speterstatic void ieee80211_addba_stop(struct ieee80211_node *ni,
11350276Speter	struct ieee80211_tx_ampdu *tap);
11450276Speterstatic void ieee80211_aggr_recv_action(struct ieee80211_node *ni,
11550276Speter	const uint8_t *frm, const uint8_t *efrm);
11650276Speter
11750276Spetervoid
11850276Speterieee80211_ht_attach(struct ieee80211com *ic)
11950276Speter{
12050276Speter	/* setup default aggregation policy */
12150276Speter	ic->ic_recv_action = ieee80211_aggr_recv_action;
12250276Speter	ic->ic_send_action = ieee80211_send_action;
12350276Speter	ic->ic_ampdu_enable = ieee80211_ampdu_enable;
12450276Speter	ic->ic_addba_request = ieee80211_addba_request;
12550276Speter	ic->ic_addba_response = ieee80211_addba_response;
12650276Speter	ic->ic_addba_stop = ieee80211_addba_stop;
12750276Speter
12850276Speter	ic->ic_htprotmode = IEEE80211_PROT_RTSCTS;
12950276Speter	ic->ic_curhtprotmode = IEEE80211_HTINFO_OPMODE_PURE;
13050276Speter}
13150276Speter
13250276Spetervoid
13350276Speterieee80211_ht_detach(struct ieee80211com *ic)
13450276Speter{
13550276Speter}
13650276Speter
13750276Spetervoid
13862449Speterieee80211_ht_vattach(struct ieee80211vap *vap)
13950276Speter{
14050276Speter
14150276Speter	/* driver can override defaults */
14250276Speter	vap->iv_ampdu_rxmax = IEEE80211_HTCAP_MAXRXAMPDU_8K;
14350276Speter	vap->iv_ampdu_density = IEEE80211_HTCAP_MPDUDENSITY_NA;
14450276Speter	vap->iv_ampdu_limit = vap->iv_ampdu_rxmax;
14550276Speter	vap->iv_amsdu_limit = vap->iv_htcaps & IEEE80211_HTCAP_MAXAMSDU;
14650276Speter	/* tx aggregation traffic thresholds */
14750276Speter	vap->iv_ampdu_mintraffic[WME_AC_BK] = 128;
14850276Speter	vap->iv_ampdu_mintraffic[WME_AC_BE] = 64;
14950276Speter	vap->iv_ampdu_mintraffic[WME_AC_VO] = 32;
15062449Speter	vap->iv_ampdu_mintraffic[WME_AC_VI] = 32;
15150276Speter
15250276Speter	if (vap->iv_htcaps & IEEE80211_HTC_HT) {
15350276Speter		/*
15450276Speter		 * Device is HT capable; enable all HT-related
15550276Speter		 * facilities by default.
15650276Speter		 * XXX these choices may be too aggressive.
15750276Speter		 */
15850276Speter		vap->iv_flags_ext |= IEEE80211_FEXT_HT
15950276Speter				  |  IEEE80211_FEXT_HTCOMPAT
16050276Speter				  ;
16150276Speter		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 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;	/* bypass normal processing */
345	/* NB: rssi, noise, and rstamp are ignored w/ M_AMPDU 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(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT sta"));
521
522	/* NB: m_len known to be sufficient */
523	wh = mtod(m, struct ieee80211_qosframe *);
524	KASSERT(wh->i_fc[0] == IEEE80211_FC0_QOSDATA, ("not QoS data"));
525
526	if ((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) == IEEE80211_FC1_DIR_DSTODS)
527		tid = ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0];
528	else
529		tid = wh->i_qos[0];
530	tid &= IEEE80211_QOS_TID;
531	rap = &ni->ni_rx_ampdu[tid];
532	if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
533		/*
534		 * No ADDBA request yet, don't touch.
535		 */
536		return PROCESS;
537	}
538	rxseq = le16toh(*(uint16_t *)wh->i_seq);
539	if ((rxseq & IEEE80211_SEQ_FRAG_MASK) != 0) {
540		/*
541		 * Fragments are not allowed; toss.
542		 */
543		IEEE80211_DISCARD_MAC(vap,
544		    IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
545		    "A-MPDU", "fragment, rxseq 0x%x tid %u%s", rxseq, tid,
546		    wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
547		vap->iv_stats.is_ampdu_rx_drop++;
548		IEEE80211_NODE_STAT(ni, rx_drop);
549		m_freem(m);
550		return CONSUMED;
551	}
552	rxseq >>= IEEE80211_SEQ_SEQ_SHIFT;
553	rap->rxa_nframes++;
554again:
555	if (rxseq == rap->rxa_start) {
556		/*
557		 * First frame in window.
558		 */
559		if (rap->rxa_qframes != 0) {
560			/*
561			 * Dispatch as many packets as we can.
562			 */
563			KASSERT(rap->rxa_m[0] == NULL, ("unexpected dup"));
564			ampdu_dispatch(ni, m);
565			ampdu_rx_dispatch(rap, ni);
566			return CONSUMED;
567		} else {
568			/*
569			 * In order; advance window and notify
570			 * caller to dispatch directly.
571			 */
572			rap->rxa_start = IEEE80211_SEQ_INC(rxseq);
573			return PROCESS;
574		}
575	}
576	/*
577	 * Frame is out of order; store if in the BA window.
578	 */
579	/* calculate offset in BA window */
580	off = IEEE80211_SEQ_SUB(rxseq, rap->rxa_start);
581	if (off < rap->rxa_wnd) {
582		/*
583		 * Common case (hopefully): in the BA window.
584		 * Sec 9.10.7.6 a) (D2.04 p.118 line 47)
585		 */
586#ifdef IEEE80211_AMPDU_AGE
587		/*
588		 * Check for frames sitting too long in the reorder queue.
589		 * This should only ever happen if frames are not delivered
590		 * without the sender otherwise notifying us (e.g. with a
591		 * BAR to move the window).  Typically this happens because
592		 * of vendor bugs that cause the sequence number to jump.
593		 * When this happens we get a gap in the reorder queue that
594		 * leaves frame sitting on the queue until they get pushed
595		 * out due to window moves.  When the vendor does not send
596		 * BAR this move only happens due to explicit packet sends
597		 *
598		 * NB: we only track the time of the oldest frame in the
599		 * reorder q; this means that if we flush we might push
600		 * frames that still "new"; if this happens then subsequent
601		 * frames will result in BA window moves which cost something
602		 * but is still better than a big throughput dip.
603		 */
604		if (rap->rxa_qframes != 0) {
605			/* XXX honor batimeout? */
606			if (ticks - rap->rxa_age > ieee80211_ampdu_age) {
607				/*
608				 * Too long since we received the first
609				 * frame; flush the reorder buffer.
610				 */
611				if (rap->rxa_qframes != 0) {
612					vap->iv_stats.is_ampdu_rx_age +=
613					    rap->rxa_qframes;
614					ampdu_rx_flush(ni, rap);
615				}
616				rap->rxa_start = IEEE80211_SEQ_INC(rxseq);
617				return PROCESS;
618			}
619		} else {
620			/*
621			 * First frame, start aging timer.
622			 */
623			rap->rxa_age = ticks;
624		}
625#endif /* IEEE80211_AMPDU_AGE */
626		/* save packet */
627		if (rap->rxa_m[off] == NULL) {
628			rap->rxa_m[off] = m;
629			rap->rxa_qframes++;
630			rap->rxa_qbytes += m->m_pkthdr.len;
631			vap->iv_stats.is_ampdu_rx_reorder++;
632		} else {
633			IEEE80211_DISCARD_MAC(vap,
634			    IEEE80211_MSG_INPUT | IEEE80211_MSG_11N,
635			    ni->ni_macaddr, "a-mpdu duplicate",
636			    "seqno %u tid %u BA win <%u:%u>",
637			    rxseq, tid, rap->rxa_start,
638			    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1));
639			vap->iv_stats.is_rx_dup++;
640			IEEE80211_NODE_STAT(ni, rx_dup);
641			m_freem(m);
642		}
643		return CONSUMED;
644	}
645	if (off < IEEE80211_SEQ_BA_RANGE) {
646		/*
647		 * Outside the BA window, but within range;
648		 * flush the reorder q and move the window.
649		 * Sec 9.10.7.6 b) (D2.04 p.118 line 60)
650		 */
651		IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
652		    "move BA win <%u:%u> (%u frames) rxseq %u tid %u",
653		    rap->rxa_start,
654		    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
655		    rap->rxa_qframes, rxseq, tid);
656		vap->iv_stats.is_ampdu_rx_move++;
657
658		/*
659		 * The spec says to flush frames up to but not including:
660		 * 	WinStart_B = rxseq - rap->rxa_wnd + 1
661		 * Then insert the frame or notify the caller to process
662		 * it immediately.  We can safely do this by just starting
663		 * over again because we know the frame will now be within
664		 * the BA window.
665		 */
666		/* NB: rxa_wnd known to be >0 */
667		ampdu_rx_flush_upto(ni, rap,
668		    IEEE80211_SEQ_SUB(rxseq, rap->rxa_wnd-1));
669		goto again;
670	} else {
671		/*
672		 * Outside the BA window and out of range; toss.
673		 * Sec 9.10.7.6 c) (D2.04 p.119 line 16)
674		 */
675		IEEE80211_DISCARD_MAC(vap,
676		    IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
677		    "MPDU", "BA win <%u:%u> (%u frames) rxseq %u tid %u%s",
678		    rap->rxa_start,
679		    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
680		    rap->rxa_qframes, rxseq, tid,
681		    wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
682		vap->iv_stats.is_ampdu_rx_drop++;
683		IEEE80211_NODE_STAT(ni, rx_drop);
684		m_freem(m);
685		return CONSUMED;
686	}
687#undef CONSUMED
688#undef PROCESS
689#undef IEEE80211_FC0_QOSDATA
690}
691
692/*
693 * Process a BAR ctl frame.  Dispatch all frames up to
694 * the sequence number of the frame.  If this frame is
695 * out of range it's discarded.
696 */
697void
698ieee80211_recv_bar(struct ieee80211_node *ni, struct mbuf *m0)
699{
700	struct ieee80211vap *vap = ni->ni_vap;
701	struct ieee80211_frame_bar *wh;
702	struct ieee80211_rx_ampdu *rap;
703	ieee80211_seq rxseq;
704	int tid, off;
705
706	if (!ieee80211_recv_bar_ena) {
707#if 0
708		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_11N,
709		    ni->ni_macaddr, "BAR", "%s", "processing disabled");
710#endif
711		vap->iv_stats.is_ampdu_bar_bad++;
712		return;
713	}
714	wh = mtod(m0, struct ieee80211_frame_bar *);
715	/* XXX check basic BAR */
716	tid = MS(le16toh(wh->i_ctl), IEEE80211_BAR_TID);
717	rap = &ni->ni_rx_ampdu[tid];
718	if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
719		/*
720		 * No ADDBA request yet, don't touch.
721		 */
722		IEEE80211_DISCARD_MAC(vap,
723		    IEEE80211_MSG_INPUT | IEEE80211_MSG_11N,
724		    ni->ni_macaddr, "BAR", "no BA stream, tid %u", tid);
725		vap->iv_stats.is_ampdu_bar_bad++;
726		return;
727	}
728	vap->iv_stats.is_ampdu_bar_rx++;
729	rxseq = le16toh(wh->i_seq) >> IEEE80211_SEQ_SEQ_SHIFT;
730	if (rxseq == rap->rxa_start)
731		return;
732	/* calculate offset in BA window */
733	off = IEEE80211_SEQ_SUB(rxseq, rap->rxa_start);
734	if (off < IEEE80211_SEQ_BA_RANGE) {
735		/*
736		 * Flush the reorder q up to rxseq and move the window.
737		 * Sec 9.10.7.6 a) (D2.04 p.119 line 22)
738		 */
739		IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
740		    "BAR moves BA win <%u:%u> (%u frames) rxseq %u tid %u",
741		    rap->rxa_start,
742		    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
743		    rap->rxa_qframes, rxseq, tid);
744		vap->iv_stats.is_ampdu_bar_move++;
745
746		ampdu_rx_flush_upto(ni, rap, rxseq);
747		if (off >= rap->rxa_wnd) {
748			/*
749			 * BAR specifies a window start to the right of BA
750			 * window; we must move it explicitly since
751			 * ampdu_rx_flush_upto will not.
752			 */
753			rap->rxa_start = rxseq;
754		}
755	} else {
756		/*
757		 * Out of range; toss.
758		 * Sec 9.10.7.6 b) (D2.04 p.119 line 41)
759		 */
760		IEEE80211_DISCARD_MAC(vap,
761		    IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
762		    "BAR", "BA win <%u:%u> (%u frames) rxseq %u tid %u%s",
763		    rap->rxa_start,
764		    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
765		    rap->rxa_qframes, rxseq, tid,
766		    wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
767		vap->iv_stats.is_ampdu_bar_oow++;
768		IEEE80211_NODE_STAT(ni, rx_drop);
769	}
770}
771
772/*
773 * Setup HT-specific state in a node.  Called only
774 * when HT use is negotiated so we don't do extra
775 * work for temporary and/or legacy sta's.
776 */
777void
778ieee80211_ht_node_init(struct ieee80211_node *ni, const uint8_t *htcap)
779{
780	struct ieee80211_tx_ampdu *tap;
781	int ac;
782
783	if (ni->ni_flags & IEEE80211_NODE_HT) {
784		/*
785		 * Clean AMPDU state on re-associate.  This handles the case
786		 * where a station leaves w/o notifying us and then returns
787		 * before node is reaped for inactivity.
788		 */
789		ieee80211_ht_node_cleanup(ni);
790	}
791	ieee80211_parse_htcap(ni, htcap);
792	for (ac = 0; ac < WME_NUM_AC; ac++) {
793		tap = &ni->ni_tx_ampdu[ac];
794		tap->txa_ac = ac;
795		/* NB: further initialization deferred */
796	}
797	ni->ni_flags |= IEEE80211_NODE_HT | IEEE80211_NODE_AMPDU;
798}
799
800/*
801 * Cleanup HT-specific state in a node.  Called only
802 * when HT use has been marked.
803 */
804void
805ieee80211_ht_node_cleanup(struct ieee80211_node *ni)
806{
807	struct ieee80211com *ic = ni->ni_ic;
808	int i;
809
810	KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT node"));
811
812	/* XXX optimize this */
813	for (i = 0; i < WME_NUM_AC; i++) {
814		struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[i];
815		if (tap->txa_flags & IEEE80211_AGGR_SETUP) {
816			/*
817			 * Stop BA stream if setup so driver has a chance
818			 * to reclaim any resources it might have allocated.
819			 */
820			ic->ic_addba_stop(ni, &ni->ni_tx_ampdu[i]);
821			tap->txa_lastsample = 0;
822			tap->txa_avgpps = 0;
823			/* NB: clearing NAK means we may re-send ADDBA */
824			tap->txa_flags &=
825			    ~(IEEE80211_AGGR_SETUP | IEEE80211_AGGR_NAK);
826		}
827	}
828	for (i = 0; i < WME_NUM_TID; i++)
829		ampdu_rx_stop(&ni->ni_rx_ampdu[i]);
830
831	ni->ni_htcap = 0;
832	ni->ni_flags &= ~IEEE80211_NODE_HT_ALL;
833}
834
835/*
836 * Age out HT resources for a station.
837 */
838void
839ieee80211_ht_node_age(struct ieee80211_node *ni)
840{
841#ifdef IEEE80211_AMPDU_AGE
842	struct ieee80211vap *vap = ni->ni_vap;
843	uint8_t tid;
844#endif
845
846	KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT sta"));
847
848#ifdef IEEE80211_AMPDU_AGE
849	for (tid = 0; tid < WME_NUM_TID; tid++) {
850		struct ieee80211_rx_ampdu *rap;
851
852		rap = &ni->ni_rx_ampdu[tid];
853		if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0)
854			continue;
855		if (rap->rxa_qframes == 0)
856			continue;
857		/*
858		 * Check for frames sitting too long in the reorder queue.
859		 * See above for more details on what's happening here.
860		 */
861		/* XXX honor batimeout? */
862		if (ticks - rap->rxa_age > ieee80211_ampdu_age) {
863			/*
864			 * Too long since we received the first
865			 * frame; flush the reorder buffer.
866			 */
867			vap->iv_stats.is_ampdu_rx_age += rap->rxa_qframes;
868			ampdu_rx_flush(ni, rap);
869		}
870	}
871#endif /* IEEE80211_AMPDU_AGE */
872}
873
874static struct ieee80211_channel *
875findhtchan(struct ieee80211com *ic, struct ieee80211_channel *c, int htflags)
876{
877	return ieee80211_find_channel(ic, c->ic_freq,
878	    (c->ic_flags &~ IEEE80211_CHAN_HT) | htflags);
879}
880
881/*
882 * Adjust a channel to be HT/non-HT according to the vap's configuration.
883 */
884struct ieee80211_channel *
885ieee80211_ht_adjust_channel(struct ieee80211com *ic,
886	struct ieee80211_channel *chan, int flags)
887{
888	struct ieee80211_channel *c;
889
890	if (flags & IEEE80211_FEXT_HT) {
891		/* promote to HT if possible */
892		if (flags & IEEE80211_FEXT_USEHT40) {
893			if (!IEEE80211_IS_CHAN_HT40(chan)) {
894				/* NB: arbitrarily pick ht40+ over ht40- */
895				c = findhtchan(ic, chan, IEEE80211_CHAN_HT40U);
896				if (c == NULL)
897					c = findhtchan(ic, chan,
898						IEEE80211_CHAN_HT40D);
899				if (c == NULL)
900					c = findhtchan(ic, chan,
901						IEEE80211_CHAN_HT20);
902				if (c != NULL)
903					chan = c;
904			}
905		} else if (!IEEE80211_IS_CHAN_HT20(chan)) {
906			c = findhtchan(ic, chan, IEEE80211_CHAN_HT20);
907			if (c != NULL)
908				chan = c;
909		}
910	} else if (IEEE80211_IS_CHAN_HT(chan)) {
911		/* demote to legacy, HT use is disabled */
912		c = ieee80211_find_channel(ic, chan->ic_freq,
913		    chan->ic_flags &~ IEEE80211_CHAN_HT);
914		if (c != NULL)
915			chan = c;
916	}
917	return chan;
918}
919
920/*
921 * Setup HT-specific state for a legacy WDS peer.
922 */
923void
924ieee80211_ht_wds_init(struct ieee80211_node *ni)
925{
926	struct ieee80211vap *vap = ni->ni_vap;
927	struct ieee80211_tx_ampdu *tap;
928	int ac;
929
930	KASSERT(vap->iv_flags_ext & IEEE80211_FEXT_HT, ("no HT requested"));
931
932	/* XXX check scan cache in case peer has an ap and we have info */
933	/*
934	 * If setup with a legacy channel; locate an HT channel.
935	 * Otherwise if the inherited channel (from a companion
936	 * AP) is suitable use it so we use the same location
937	 * for the extension channel).
938	 */
939	ni->ni_chan = ieee80211_ht_adjust_channel(ni->ni_ic,
940	    ni->ni_chan, ieee80211_htchanflags(ni->ni_chan));
941
942	ni->ni_htcap = 0;
943	if (vap->iv_flags_ext & IEEE80211_FEXT_SHORTGI20)
944		ni->ni_htcap |= IEEE80211_HTCAP_SHORTGI20;
945	if (IEEE80211_IS_CHAN_HT40(ni->ni_chan)) {
946		ni->ni_htcap |= IEEE80211_HTCAP_CHWIDTH40;
947		ni->ni_chw = 40;
948		if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan))
949			ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_ABOVE;
950		else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan))
951			ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_BELOW;
952		if (vap->iv_flags_ext & IEEE80211_FEXT_SHORTGI40)
953			ni->ni_htcap |= IEEE80211_HTCAP_SHORTGI40;
954	} else {
955		ni->ni_chw = 20;
956		ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_NONE;
957	}
958	ni->ni_htctlchan = ni->ni_chan->ic_ieee;
959
960	ni->ni_htopmode = 0;		/* XXX need protection state */
961	ni->ni_htstbc = 0;		/* XXX need info */
962
963	for (ac = 0; ac < WME_NUM_AC; ac++) {
964		tap = &ni->ni_tx_ampdu[ac];
965		tap->txa_ac = ac;
966	}
967	/* NB: AMPDU tx/rx governed by IEEE80211_FEXT_AMPDU_{TX,RX} */
968	ni->ni_flags |= IEEE80211_NODE_HT | IEEE80211_NODE_AMPDU;
969}
970
971/*
972 * Notify hostap vaps of a change in the HTINFO ie.
973 */
974static void
975htinfo_notify(struct ieee80211com *ic)
976{
977	struct ieee80211vap *vap;
978	int first = 1;
979
980	IEEE80211_LOCK_ASSERT(ic);
981
982	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
983		if (vap->iv_opmode != IEEE80211_M_HOSTAP)
984			continue;
985		if (first) {
986			IEEE80211_NOTE(vap,
987			    IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N,
988			    vap->iv_bss,
989			    "HT bss occupancy change: %d sta, %d ht, "
990			    "%d ht40%s, HT protmode now 0x%x"
991			    , ic->ic_sta_assoc
992			    , ic->ic_ht_sta_assoc
993			    , ic->ic_ht40_sta_assoc
994			    , (ic->ic_flags_ext & IEEE80211_FEXT_NONHT_PR) ?
995				 ", non-HT sta present" : ""
996			    , ic->ic_curhtprotmode);
997			first = 0;
998		}
999		ieee80211_beacon_notify(vap, IEEE80211_BEACON_HTINFO);
1000	}
1001}
1002
1003/*
1004 * Calculate HT protection mode from current
1005 * state and handle updates.
1006 */
1007static void
1008htinfo_update(struct ieee80211com *ic)
1009{
1010	uint8_t protmode;
1011
1012	if (ic->ic_sta_assoc != ic->ic_ht_sta_assoc) {
1013		protmode = IEEE80211_HTINFO_OPMODE_MIXED
1014			 | IEEE80211_HTINFO_NONHT_PRESENT;
1015	} else if (ic->ic_flags_ext & IEEE80211_FEXT_NONHT_PR) {
1016		protmode = IEEE80211_HTINFO_OPMODE_PROTOPT
1017			 | IEEE80211_HTINFO_NONHT_PRESENT;
1018	} else if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
1019	    IEEE80211_IS_CHAN_HT40(ic->ic_bsschan) &&
1020	    ic->ic_sta_assoc != ic->ic_ht40_sta_assoc) {
1021		protmode = IEEE80211_HTINFO_OPMODE_HT20PR;
1022	} else {
1023		protmode = IEEE80211_HTINFO_OPMODE_PURE;
1024	}
1025	if (protmode != ic->ic_curhtprotmode) {
1026		ic->ic_curhtprotmode = protmode;
1027		htinfo_notify(ic);
1028	}
1029}
1030
1031/*
1032 * Handle an HT station joining a BSS.
1033 */
1034void
1035ieee80211_ht_node_join(struct ieee80211_node *ni)
1036{
1037	struct ieee80211com *ic = ni->ni_ic;
1038
1039	IEEE80211_LOCK_ASSERT(ic);
1040
1041	if (ni->ni_flags & IEEE80211_NODE_HT) {
1042		ic->ic_ht_sta_assoc++;
1043		if (ni->ni_chw == 40)
1044			ic->ic_ht40_sta_assoc++;
1045	}
1046	htinfo_update(ic);
1047}
1048
1049/*
1050 * Handle an HT station leaving a BSS.
1051 */
1052void
1053ieee80211_ht_node_leave(struct ieee80211_node *ni)
1054{
1055	struct ieee80211com *ic = ni->ni_ic;
1056
1057	IEEE80211_LOCK_ASSERT(ic);
1058
1059	if (ni->ni_flags & IEEE80211_NODE_HT) {
1060		ic->ic_ht_sta_assoc--;
1061		if (ni->ni_chw == 40)
1062			ic->ic_ht40_sta_assoc--;
1063	}
1064	htinfo_update(ic);
1065}
1066
1067/*
1068 * Public version of htinfo_update; used for processing
1069 * beacon frames from overlapping bss.
1070 *
1071 * Caller can specify either IEEE80211_HTINFO_OPMODE_MIXED
1072 * (on receipt of a beacon that advertises MIXED) or
1073 * IEEE80211_HTINFO_OPMODE_PROTOPT (on receipt of a beacon
1074 * from an overlapping legacy bss).  We treat MIXED with
1075 * a higher precedence than PROTOPT (i.e. we will not change
1076 * change PROTOPT -> MIXED; only MIXED -> PROTOPT).  This
1077 * corresponds to how we handle things in htinfo_update.
1078 */
1079void
1080ieee80211_htprot_update(struct ieee80211com *ic, int protmode)
1081{
1082#define	OPMODE(x)	SM(x, IEEE80211_HTINFO_OPMODE)
1083	if (protmode == ic->ic_curhtprotmode)
1084		return;
1085	if (OPMODE(ic->ic_curhtprotmode) == IEEE80211_HTINFO_OPMODE_MIXED &&
1086	    OPMODE(protmode) == IEEE80211_HTINFO_OPMODE_PROTOPT)
1087		return;
1088
1089	IEEE80211_LOCK(ic);
1090	/* track non-HT station presence */
1091	KASSERT(protmode & IEEE80211_HTINFO_NONHT_PRESENT,
1092	    ("missing NONHT_PRESENT"));
1093	ic->ic_flags_ext |= IEEE80211_FEXT_NONHT_PR;
1094	ic->ic_lastnonht = ticks;
1095
1096	/* push beacon update */
1097	ic->ic_curhtprotmode = protmode;
1098	htinfo_notify(ic);
1099	IEEE80211_UNLOCK(ic);
1100#undef OPMODE
1101}
1102
1103/*
1104 * Time out presence of an overlapping bss with non-HT
1105 * stations.  When operating in hostap mode we listen for
1106 * beacons from other stations and if we identify a non-HT
1107 * station is present we update the opmode field of the
1108 * HTINFO ie.  To identify when all non-HT stations are
1109 * gone we time out this condition.
1110 */
1111void
1112ieee80211_ht_timeout(struct ieee80211com *ic)
1113{
1114	IEEE80211_LOCK_ASSERT(ic);
1115
1116	if ((ic->ic_flags_ext & IEEE80211_FEXT_NONHT_PR) &&
1117	    time_after(ticks, ic->ic_lastnonht + IEEE80211_NONHT_PRESENT_AGE)) {
1118#if 0
1119		IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
1120		    "%s", "time out non-HT STA present on channel");
1121#endif
1122		ic->ic_flags_ext &= ~IEEE80211_FEXT_NONHT_PR;
1123		htinfo_update(ic);
1124	}
1125}
1126
1127/* unalligned little endian access */
1128#define LE_READ_2(p)					\
1129	((uint16_t)					\
1130	 ((((const uint8_t *)(p))[0]      ) |		\
1131	  (((const uint8_t *)(p))[1] <<  8)))
1132
1133/*
1134 * Process an 802.11n HT capabilities ie.
1135 */
1136void
1137ieee80211_parse_htcap(struct ieee80211_node *ni, const uint8_t *ie)
1138{
1139	struct ieee80211vap *vap = ni->ni_vap;
1140
1141	if (ie[0] == IEEE80211_ELEMID_VENDOR) {
1142		/*
1143		 * Station used Vendor OUI ie to associate;
1144		 * mark the node so when we respond we'll use
1145		 * the Vendor OUI's and not the standard ie's.
1146		 */
1147		ni->ni_flags |= IEEE80211_NODE_HTCOMPAT;
1148		ie += 4;
1149	} else
1150		ni->ni_flags &= ~IEEE80211_NODE_HTCOMPAT;
1151
1152	ni->ni_htcap = LE_READ_2(ie +
1153		__offsetof(struct ieee80211_ie_htcap, hc_cap));
1154	ni->ni_htparam = ie[__offsetof(struct ieee80211_ie_htcap, hc_param)];
1155	/* XXX needed or will ieee80211_parse_htinfo always be called? */
1156	ni->ni_chw = (ni->ni_htcap & IEEE80211_HTCAP_CHWIDTH40) &&
1157		     (vap->iv_flags_ext & IEEE80211_FEXT_USEHT40) ? 40 : 20;
1158}
1159
1160/*
1161 * Process an 802.11n HT info ie and update the node state.
1162 * Note that we handle use this information to identify the
1163 * correct channel (HT20, HT40+, HT40-, legacy).  The caller
1164 * is responsible for insuring any required channel change is
1165 * done (e.g. in sta mode when parsing the contents of a
1166 * beacon frame).
1167 */
1168void
1169ieee80211_parse_htinfo(struct ieee80211_node *ni, const uint8_t *ie)
1170{
1171	struct ieee80211com *ic = ni->ni_ic;
1172	struct ieee80211vap *vap = ni->ni_vap;
1173 	const struct ieee80211_ie_htinfo *htinfo;
1174	struct ieee80211_channel *c;
1175	uint16_t w;
1176	int htflags, chanflags;
1177
1178	if (ie[0] == IEEE80211_ELEMID_VENDOR)
1179		ie += 4;
1180 	htinfo = (const struct ieee80211_ie_htinfo *) ie;
1181	ni->ni_htctlchan = htinfo->hi_ctrlchannel;
1182	ni->ni_ht2ndchan = SM(htinfo->hi_byte1, IEEE80211_HTINFO_2NDCHAN);
1183	w = LE_READ_2(&htinfo->hi_byte2);
1184	ni->ni_htopmode = SM(w, IEEE80211_HTINFO_OPMODE);
1185	w = LE_READ_2(&htinfo->hi_byte45);
1186	ni->ni_htstbc = SM(w, IEEE80211_HTINFO_BASIC_STBCMCS);
1187	/*
1188	 * Handle 11n channel switch.  Use the received HT ie's to
1189	 * identify the right channel to use.  If we cannot locate it
1190	 * in the channel table then fallback to legacy operation.
1191	 */
1192	/* NB: honor operating mode constraint */
1193	htflags = (vap->iv_flags_ext & IEEE80211_FEXT_HT) ?
1194	    IEEE80211_CHAN_HT20 : 0;
1195	if ((htinfo->hi_byte1 & IEEE80211_HTINFO_TXWIDTH_2040) &&
1196	    (vap->iv_flags_ext & IEEE80211_FEXT_USEHT40)) {
1197		if (ni->ni_ht2ndchan == IEEE80211_HTINFO_2NDCHAN_ABOVE)
1198			htflags = IEEE80211_CHAN_HT40U;
1199		else if (ni->ni_ht2ndchan == IEEE80211_HTINFO_2NDCHAN_BELOW)
1200			htflags = IEEE80211_CHAN_HT40D;
1201	}
1202	chanflags = (ni->ni_chan->ic_flags &~ IEEE80211_CHAN_HT) | htflags;
1203	if (chanflags != ni->ni_chan->ic_flags) {
1204		c = ieee80211_find_channel(ic, ni->ni_chan->ic_freq, chanflags);
1205		if (c == NULL && (htflags & IEEE80211_CHAN_HT40)) {
1206			/*
1207			 * No HT40 channel entry in our table; fall back
1208			 * to HT20 operation.  This should not happen.
1209			 */
1210			c = findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT20);
1211			IEEE80211_NOTE(vap,
1212			    IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
1213			    "no HT40 channel (freq %u), falling back to HT20",
1214			    ni->ni_chan->ic_freq);
1215			/* XXX stat */
1216		}
1217		if (c != NULL && c != ni->ni_chan) {
1218			IEEE80211_NOTE(vap,
1219			    IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
1220			    "switch station to HT%d channel %u/0x%x",
1221			    IEEE80211_IS_CHAN_HT40(c) ? 40 : 20,
1222			    c->ic_freq, c->ic_flags);
1223			ni->ni_chan = c;
1224		}
1225		/* NB: caller responsible for forcing any channel change */
1226	}
1227	/* update node's tx channel width */
1228	ni->ni_chw = IEEE80211_IS_CHAN_HT40(ni->ni_chan)? 40 : 20;
1229}
1230
1231/*
1232 * Install received HT rate set by parsing the HT cap ie.
1233 */
1234int
1235ieee80211_setup_htrates(struct ieee80211_node *ni, const uint8_t *ie, int flags)
1236{
1237	struct ieee80211vap *vap = ni->ni_vap;
1238	const struct ieee80211_ie_htcap *htcap;
1239	struct ieee80211_htrateset *rs;
1240	int i;
1241
1242	rs = &ni->ni_htrates;
1243	memset(rs, 0, sizeof(*rs));
1244	if (ie != NULL) {
1245		if (ie[0] == IEEE80211_ELEMID_VENDOR)
1246			ie += 4;
1247		htcap = (const struct ieee80211_ie_htcap *) ie;
1248		for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++) {
1249			if (isclr(htcap->hc_mcsset, i))
1250				continue;
1251			if (rs->rs_nrates == IEEE80211_HTRATE_MAXSIZE) {
1252				IEEE80211_NOTE(vap,
1253				    IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
1254				    "WARNING, HT rate set too large; only "
1255				    "using %u rates", IEEE80211_HTRATE_MAXSIZE);
1256				vap->iv_stats.is_rx_rstoobig++;
1257				break;
1258			}
1259			rs->rs_rates[rs->rs_nrates++] = i;
1260		}
1261	}
1262	return ieee80211_fix_rate(ni, (struct ieee80211_rateset *) rs, flags);
1263}
1264
1265/*
1266 * Mark rates in a node's HT rate set as basic according
1267 * to the information in the supplied HT info ie.
1268 */
1269void
1270ieee80211_setup_basic_htrates(struct ieee80211_node *ni, const uint8_t *ie)
1271{
1272	const struct ieee80211_ie_htinfo *htinfo;
1273	struct ieee80211_htrateset *rs;
1274	int i, j;
1275
1276	if (ie[0] == IEEE80211_ELEMID_VENDOR)
1277		ie += 4;
1278	htinfo = (const struct ieee80211_ie_htinfo *) ie;
1279	rs = &ni->ni_htrates;
1280	if (rs->rs_nrates == 0) {
1281		IEEE80211_NOTE(ni->ni_vap,
1282		    IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
1283		    "%s", "WARNING, empty HT rate set");
1284		return;
1285	}
1286	for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++) {
1287		if (isclr(htinfo->hi_basicmcsset, i))
1288			continue;
1289		for (j = 0; j < rs->rs_nrates; j++)
1290			if ((rs->rs_rates[j] & IEEE80211_RATE_VAL) == i)
1291				rs->rs_rates[j] |= IEEE80211_RATE_BASIC;
1292	}
1293}
1294
1295static void
1296addba_timeout(void *arg)
1297{
1298	struct ieee80211_tx_ampdu *tap = arg;
1299
1300	/* XXX ? */
1301	tap->txa_flags &= ~IEEE80211_AGGR_XCHGPEND;
1302	tap->txa_attempts++;
1303}
1304
1305static void
1306addba_start_timeout(struct ieee80211_tx_ampdu *tap)
1307{
1308	/* XXX use CALLOUT_PENDING instead? */
1309	callout_reset(&tap->txa_timer, ieee80211_addba_timeout,
1310	    addba_timeout, tap);
1311	tap->txa_flags |= IEEE80211_AGGR_XCHGPEND;
1312	tap->txa_nextrequest = ticks + ieee80211_addba_timeout;
1313}
1314
1315static void
1316addba_stop_timeout(struct ieee80211_tx_ampdu *tap)
1317{
1318	/* XXX use CALLOUT_PENDING instead? */
1319	if (tap->txa_flags & IEEE80211_AGGR_XCHGPEND) {
1320		callout_stop(&tap->txa_timer);
1321		tap->txa_flags &= ~IEEE80211_AGGR_XCHGPEND;
1322	}
1323}
1324
1325/*
1326 * Default method for requesting A-MPDU tx aggregation.
1327 * We setup the specified state block and start a timer
1328 * to wait for an ADDBA response frame.
1329 */
1330static int
1331ieee80211_addba_request(struct ieee80211_node *ni,
1332	struct ieee80211_tx_ampdu *tap,
1333	int dialogtoken, int baparamset, int batimeout)
1334{
1335	int bufsiz;
1336
1337	/* XXX locking */
1338	tap->txa_token = dialogtoken;
1339	tap->txa_flags |= IEEE80211_AGGR_IMMEDIATE;
1340	tap->txa_start = 0;
1341	bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
1342	tap->txa_wnd = (bufsiz == 0) ?
1343	    IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
1344	addba_start_timeout(tap);
1345	return 1;
1346}
1347
1348/*
1349 * Default method for processing an A-MPDU tx aggregation
1350 * response.  We shutdown any pending timer and update the
1351 * state block according to the reply.
1352 */
1353static int
1354ieee80211_addba_response(struct ieee80211_node *ni,
1355	struct ieee80211_tx_ampdu *tap,
1356	int status, int baparamset, int batimeout)
1357{
1358	int bufsiz;
1359
1360	/* XXX locking */
1361	addba_stop_timeout(tap);
1362	if (status == IEEE80211_STATUS_SUCCESS) {
1363		bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
1364		/* XXX override our request? */
1365		tap->txa_wnd = (bufsiz == 0) ?
1366		    IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
1367		tap->txa_flags |= IEEE80211_AGGR_RUNNING;
1368	} else {
1369		/* mark tid so we don't try again */
1370		tap->txa_flags |= IEEE80211_AGGR_NAK;
1371	}
1372	return 1;
1373}
1374
1375/*
1376 * Default method for stopping A-MPDU tx aggregation.
1377 * Any timer is cleared and we drain any pending frames.
1378 */
1379static void
1380ieee80211_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
1381{
1382	/* XXX locking */
1383	addba_stop_timeout(tap);
1384	if (tap->txa_flags & IEEE80211_AGGR_RUNNING) {
1385		/* XXX clear aggregation queue */
1386		tap->txa_flags &= ~IEEE80211_AGGR_RUNNING;
1387	}
1388	tap->txa_attempts = 0;
1389}
1390
1391/*
1392 * Process a received action frame using the default aggregation
1393 * policy.  We intercept ADDBA-related frames and use them to
1394 * update our aggregation state.  All other frames are passed up
1395 * for processing by ieee80211_recv_action.
1396 */
1397static void
1398ieee80211_aggr_recv_action(struct ieee80211_node *ni,
1399	const uint8_t *frm, const uint8_t *efrm)
1400{
1401	struct ieee80211com *ic = ni->ni_ic;
1402	struct ieee80211vap *vap = ni->ni_vap;
1403	const struct ieee80211_action *ia;
1404	struct ieee80211_rx_ampdu *rap;
1405	struct ieee80211_tx_ampdu *tap;
1406	uint8_t dialogtoken, policy;
1407	uint16_t baparamset, batimeout, baseqctl, code;
1408	uint16_t args[4];
1409	int tid, ac, bufsiz;
1410
1411	ia = (const struct ieee80211_action *) frm;
1412	switch (ia->ia_category) {
1413	case IEEE80211_ACTION_CAT_BA:
1414		switch (ia->ia_action) {
1415		case IEEE80211_ACTION_BA_ADDBA_REQUEST:
1416			dialogtoken = frm[2];
1417			baparamset = LE_READ_2(frm+3);
1418			batimeout = LE_READ_2(frm+5);
1419			baseqctl = LE_READ_2(frm+7);
1420
1421			tid = MS(baparamset, IEEE80211_BAPS_TID);
1422			bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
1423
1424			IEEE80211_NOTE(vap,
1425			    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1426			    "recv ADDBA request: dialogtoken %u "
1427			    "baparamset 0x%x (tid %d bufsiz %d) batimeout %d "
1428			    "baseqctl %d:%d",
1429			    dialogtoken, baparamset, tid, bufsiz, batimeout,
1430			    MS(baseqctl, IEEE80211_BASEQ_START),
1431			    MS(baseqctl, IEEE80211_BASEQ_FRAG));
1432
1433			rap = &ni->ni_rx_ampdu[tid];
1434
1435			/* Send ADDBA response */
1436			args[0] = dialogtoken;
1437			/*
1438			 * NB: We ack only if the sta associated with HT and
1439			 * the ap is configured to do AMPDU rx (the latter
1440			 * violates the 11n spec and is mostly for testing).
1441			 */
1442			if ((ni->ni_flags & IEEE80211_NODE_AMPDU_RX) &&
1443			    (vap->iv_flags_ext & IEEE80211_FEXT_AMPDU_RX)) {
1444				ampdu_rx_start(rap, bufsiz,
1445				    MS(baseqctl, IEEE80211_BASEQ_START));
1446
1447				args[1] = IEEE80211_STATUS_SUCCESS;
1448			} else {
1449				IEEE80211_NOTE(vap,
1450				    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1451				    ni, "reject ADDBA request: %s",
1452				    ni->ni_flags & IEEE80211_NODE_AMPDU_RX ?
1453				       "administratively disabled" :
1454				       "not negotiated for station");
1455				vap->iv_stats.is_addba_reject++;
1456				args[1] = IEEE80211_STATUS_UNSPECIFIED;
1457			}
1458			/* XXX honor rap flags? */
1459			args[2] = IEEE80211_BAPS_POLICY_IMMEDIATE
1460				| SM(tid, IEEE80211_BAPS_TID)
1461				| SM(rap->rxa_wnd, IEEE80211_BAPS_BUFSIZ)
1462				;
1463			args[3] = 0;
1464			ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
1465				IEEE80211_ACTION_BA_ADDBA_RESPONSE, args);
1466			return;
1467
1468		case IEEE80211_ACTION_BA_ADDBA_RESPONSE:
1469			dialogtoken = frm[2];
1470			code = LE_READ_2(frm+3);
1471			baparamset = LE_READ_2(frm+5);
1472			tid = MS(baparamset, IEEE80211_BAPS_TID);
1473			bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
1474			policy = MS(baparamset, IEEE80211_BAPS_POLICY);
1475			batimeout = LE_READ_2(frm+7);
1476
1477			ac = TID_TO_WME_AC(tid);
1478			tap = &ni->ni_tx_ampdu[ac];
1479			if ((tap->txa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
1480				IEEE80211_DISCARD_MAC(vap,
1481				    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1482				    ni->ni_macaddr, "ADDBA response",
1483				    "no pending ADDBA, tid %d dialogtoken %u "
1484				    "code %d", tid, dialogtoken, code);
1485				vap->iv_stats.is_addba_norequest++;
1486				return;
1487			}
1488			if (dialogtoken != tap->txa_token) {
1489				IEEE80211_DISCARD_MAC(vap,
1490				    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1491				    ni->ni_macaddr, "ADDBA response",
1492				    "dialogtoken mismatch: waiting for %d, "
1493				    "received %d, tid %d code %d",
1494				    tap->txa_token, dialogtoken, tid, code);
1495				vap->iv_stats.is_addba_badtoken++;
1496				return;
1497			}
1498			/* NB: assumes IEEE80211_AGGR_IMMEDIATE is 1 */
1499			if (policy != (tap->txa_flags & IEEE80211_AGGR_IMMEDIATE)) {
1500				IEEE80211_DISCARD_MAC(vap,
1501				    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1502				    ni->ni_macaddr, "ADDBA response",
1503				    "policy mismatch: expecting %s, "
1504				    "received %s, tid %d code %d",
1505				    tap->txa_flags & IEEE80211_AGGR_IMMEDIATE,
1506				    policy, tid, code);
1507				vap->iv_stats.is_addba_badpolicy++;
1508				return;
1509			}
1510#if 0
1511			/* XXX we take MIN in ieee80211_addba_response */
1512			if (bufsiz > IEEE80211_AGGR_BAWMAX) {
1513				IEEE80211_DISCARD_MAC(vap,
1514				    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1515				    ni->ni_macaddr, "ADDBA response",
1516				    "BA window too large: max %d, "
1517				    "received %d, tid %d code %d",
1518				    bufsiz, IEEE80211_AGGR_BAWMAX, tid, code);
1519				vap->iv_stats.is_addba_badbawinsize++;
1520				return;
1521			}
1522#endif
1523
1524			IEEE80211_NOTE(vap,
1525			    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1526			    "recv ADDBA response: dialogtoken %u code %d "
1527			    "baparamset 0x%x (tid %d bufsiz %d) batimeout %d",
1528			    dialogtoken, code, baparamset, tid, bufsiz,
1529			    batimeout);
1530			ic->ic_addba_response(ni, tap,
1531				code, baparamset, batimeout);
1532			return;
1533
1534		case IEEE80211_ACTION_BA_DELBA:
1535			baparamset = LE_READ_2(frm+2);
1536			code = LE_READ_2(frm+4);
1537
1538			tid = MS(baparamset, IEEE80211_DELBAPS_TID);
1539
1540			IEEE80211_NOTE(vap,
1541			    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1542			    "recv DELBA: baparamset 0x%x (tid %d initiator %d) "
1543			    "code %d", baparamset, tid,
1544			    MS(baparamset, IEEE80211_DELBAPS_INIT), code);
1545
1546			if ((baparamset & IEEE80211_DELBAPS_INIT) == 0) {
1547				ac = TID_TO_WME_AC(tid);
1548				tap = &ni->ni_tx_ampdu[ac];
1549				ic->ic_addba_stop(ni, tap);
1550			} else {
1551				rap = &ni->ni_rx_ampdu[tid];
1552				ampdu_rx_stop(rap);
1553			}
1554			return;
1555		}
1556		break;
1557	}
1558	ieee80211_recv_action(ni, frm, efrm);
1559}
1560
1561/*
1562 * Process a received 802.11n action frame.
1563 * Aggregation-related frames are assumed to be handled
1564 * already; we handle any other frames we can, otherwise
1565 * complain about being unsupported (with debugging).
1566 */
1567void
1568ieee80211_recv_action(struct ieee80211_node *ni,
1569	const uint8_t *frm, const uint8_t *efrm)
1570{
1571	struct ieee80211vap *vap = ni->ni_vap;
1572	const struct ieee80211_action *ia;
1573	int chw;
1574
1575	ia = (const struct ieee80211_action *) frm;
1576	switch (ia->ia_category) {
1577	case IEEE80211_ACTION_CAT_BA:
1578		IEEE80211_NOTE(vap,
1579		    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1580		    "%s: BA action %d not implemented", __func__,
1581		    ia->ia_action);
1582		vap->iv_stats.is_rx_mgtdiscard++;
1583		break;
1584	case IEEE80211_ACTION_CAT_HT:
1585		switch (ia->ia_action) {
1586		case IEEE80211_ACTION_HT_TXCHWIDTH:
1587			chw = frm[2] == IEEE80211_A_HT_TXCHWIDTH_2040 ? 40 : 20;
1588			IEEE80211_NOTE(vap,
1589			    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1590		            "%s: HT txchwidth, width %d%s",
1591			    __func__, chw, ni->ni_chw != chw ? "*" : "");
1592			if (chw != ni->ni_chw) {
1593				ni->ni_chw = chw;
1594				/* XXX notify on change */
1595			}
1596			break;
1597		case IEEE80211_ACTION_HT_MIMOPWRSAVE:
1598			IEEE80211_NOTE(vap,
1599			    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1600		            "%s: HT MIMO PS", __func__);
1601			break;
1602		default:
1603			IEEE80211_NOTE(vap,
1604			   IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1605		           "%s: HT action %d not implemented", __func__,
1606			   ia->ia_action);
1607			vap->iv_stats.is_rx_mgtdiscard++;
1608			break;
1609		}
1610		break;
1611	default:
1612		IEEE80211_NOTE(vap,
1613		    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1614		    "%s: category %d not implemented", __func__,
1615		    ia->ia_category);
1616		vap->iv_stats.is_rx_mgtdiscard++;
1617		break;
1618	}
1619}
1620
1621/*
1622 * Transmit processing.
1623 */
1624
1625/*
1626 * Check if A-MPDU should be requested/enabled for a stream.
1627 * We require a traffic rate above a per-AC threshold and we
1628 * also handle backoff from previous failed attempts.
1629 *
1630 * Drivers may override this method to bring in information
1631 * such as link state conditions in making the decision.
1632 */
1633static int
1634ieee80211_ampdu_enable(struct ieee80211_node *ni,
1635	struct ieee80211_tx_ampdu *tap)
1636{
1637	struct ieee80211vap *vap = ni->ni_vap;
1638
1639	if (tap->txa_avgpps < vap->iv_ampdu_mintraffic[tap->txa_ac])
1640		return 0;
1641	/* XXX check rssi? */
1642	if (tap->txa_attempts >= ieee80211_addba_maxtries &&
1643	    ticks < tap->txa_nextrequest) {
1644		/*
1645		 * Don't retry too often; txa_nextrequest is set
1646		 * to the minimum interval we'll retry after
1647		 * ieee80211_addba_maxtries failed attempts are made.
1648		 */
1649		return 0;
1650	}
1651	IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
1652	    "%s: enable AMPDU on %s, avgpps %d pkts %d",
1653	    __func__, ieee80211_wme_acnames[tap->txa_ac],
1654	    tap->txa_avgpps, tap->txa_pkts);
1655	return 1;
1656}
1657
1658/*
1659 * Request A-MPDU tx aggregation.  Setup local state and
1660 * issue an ADDBA request.  BA use will only happen after
1661 * the other end replies with ADDBA response.
1662 */
1663int
1664ieee80211_ampdu_request(struct ieee80211_node *ni,
1665	struct ieee80211_tx_ampdu *tap)
1666{
1667	struct ieee80211com *ic = ni->ni_ic;
1668	uint16_t args[4];
1669	int tid, dialogtoken;
1670	static int tokens = 0;	/* XXX */
1671
1672	/* XXX locking */
1673	if ((tap->txa_flags & IEEE80211_AGGR_SETUP) == 0) {
1674		/* do deferred setup of state */
1675		callout_init(&tap->txa_timer, CALLOUT_MPSAFE);
1676		tap->txa_flags |= IEEE80211_AGGR_SETUP;
1677	}
1678	/* XXX hack for not doing proper locking */
1679	tap->txa_flags &= ~IEEE80211_AGGR_NAK;
1680
1681	dialogtoken = (tokens+1) % 63;		/* XXX */
1682	tid = WME_AC_TO_TID(tap->txa_ac);
1683	tap->txa_start = ni->ni_txseqs[tid];
1684
1685	tid = WME_AC_TO_TID(tap->txa_ac);
1686	args[0] = dialogtoken;
1687	args[1]	= IEEE80211_BAPS_POLICY_IMMEDIATE
1688		| SM(tid, IEEE80211_BAPS_TID)
1689		| SM(IEEE80211_AGGR_BAWMAX, IEEE80211_BAPS_BUFSIZ)
1690		;
1691	args[2] = 0;	/* batimeout */
1692	/* NB: do first so there's no race against reply */
1693	if (!ic->ic_addba_request(ni, tap, dialogtoken, args[1], args[2])) {
1694		/* unable to setup state, don't make request */
1695		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
1696		    ni, "%s: could not setup BA stream for AC %d",
1697		    __func__, tap->txa_ac);
1698		/* defer next try so we don't slam the driver with requests */
1699		tap->txa_attempts = ieee80211_addba_maxtries;
1700		/* NB: check in case driver wants to override */
1701		if (tap->txa_nextrequest <= ticks)
1702			tap->txa_nextrequest = ticks + ieee80211_addba_backoff;
1703		return 0;
1704	}
1705	tokens = dialogtoken;			/* allocate token */
1706	/* NB: after calling ic_addba_request so driver can set txa_start */
1707	args[3] = SM(tap->txa_start, IEEE80211_BASEQ_START)
1708		| SM(0, IEEE80211_BASEQ_FRAG)
1709		;
1710	return ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
1711		IEEE80211_ACTION_BA_ADDBA_REQUEST, args);
1712}
1713
1714/*
1715 * Terminate an AMPDU tx stream.  State is reclaimed
1716 * and the peer notified with a DelBA Action frame.
1717 */
1718void
1719ieee80211_ampdu_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
1720{
1721	struct ieee80211com *ic = ni->ni_ic;
1722	struct ieee80211vap *vap = ni->ni_vap;
1723	uint16_t args[4];
1724
1725	/* XXX locking */
1726	if (IEEE80211_AMPDU_RUNNING(tap)) {
1727		IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1728		    ni, "%s: stop BA stream for AC %d", __func__, tap->txa_ac);
1729		vap->iv_stats.is_ampdu_stop++;
1730
1731		ic->ic_addba_stop(ni, tap);
1732		args[0] = WME_AC_TO_TID(tap->txa_ac);
1733		args[1] = IEEE80211_DELBAPS_INIT;
1734		args[2] = 1;				/* XXX reason code */
1735		ieee80211_send_action(ni, IEEE80211_ACTION_CAT_BA,
1736			IEEE80211_ACTION_BA_DELBA, args);
1737	} else {
1738		IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1739		    ni, "%s: BA stream for AC %d not running",
1740		    __func__, tap->txa_ac);
1741		vap->iv_stats.is_ampdu_stop_failed++;
1742	}
1743}
1744
1745/*
1746 * Transmit a BAR frame to the specified node.  The
1747 * BAR contents are drawn from the supplied aggregation
1748 * state associated with the node.
1749 */
1750int
1751ieee80211_send_bar(struct ieee80211_node *ni,
1752	const struct ieee80211_tx_ampdu *tap)
1753{
1754#define	senderr(_x, _v)	do { vap->iv_stats._v++; ret = _x; goto bad; } while (0)
1755#define	ADDSHORT(frm, v) do {			\
1756	frm[0] = (v) & 0xff;			\
1757	frm[1] = (v) >> 8;			\
1758	frm += 2;				\
1759} while (0)
1760	struct ieee80211vap *vap = ni->ni_vap;
1761	struct ieee80211com *ic = ni->ni_ic;
1762	struct ieee80211_frame_min *wh;
1763	struct mbuf *m;
1764	uint8_t *frm;
1765	uint16_t barctl, barseqctl;
1766	int tid, ret;
1767
1768	ieee80211_ref_node(ni);
1769
1770	m = ieee80211_getmgtframe(&frm,
1771		ic->ic_headroom + sizeof(struct ieee80211_frame_min),
1772		sizeof(struct ieee80211_ba_request)
1773	);
1774	if (m == NULL)
1775		senderr(ENOMEM, is_tx_nobuf);
1776
1777	wh = mtod(m, struct ieee80211_frame_min *);
1778	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 |
1779		IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_BAR;
1780	wh->i_fc[1] = 0;
1781	IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
1782	IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1783
1784	tid = WME_AC_TO_TID(tap->txa_ac);
1785	barctl 	= (tap->txa_flags & IEEE80211_AGGR_IMMEDIATE ?
1786			IEEE80211_BAPS_POLICY_IMMEDIATE :
1787			IEEE80211_BAPS_POLICY_DELAYED)
1788		| SM(tid, IEEE80211_BAPS_TID)
1789		| SM(tap->txa_wnd, IEEE80211_BAPS_BUFSIZ)
1790		;
1791	barseqctl = SM(tap->txa_start, IEEE80211_BASEQ_START)
1792		| SM(0, IEEE80211_BASEQ_FRAG)
1793		;
1794	ADDSHORT(frm, barctl);
1795	ADDSHORT(frm, barseqctl);
1796	m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
1797
1798	M_WME_SETAC(m, WME_AC_VO);
1799
1800	IEEE80211_NODE_STAT(ni, tx_mgmt);	/* XXX tx_ctl? */
1801
1802	IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
1803	    ni, "send bar frame (tid %u start %u) on channel %u",
1804	    tid, tap->txa_start, ieee80211_chan2ieee(ic, ic->ic_curchan));
1805
1806	return ic->ic_raw_xmit(ni, m, NULL);
1807bad:
1808	ieee80211_free_node(ni);
1809	return ret;
1810#undef ADDSHORT
1811#undef senderr
1812}
1813
1814/*
1815 * Send an action management frame.  The arguments are stuff
1816 * into a frame without inspection; the caller is assumed to
1817 * prepare them carefully (e.g. based on the aggregation state).
1818 */
1819int
1820ieee80211_send_action(struct ieee80211_node *ni,
1821	int category, int action, uint16_t args[4])
1822{
1823#define	senderr(_x, _v)	do { vap->iv_stats._v++; ret = _x; goto bad; } while (0)
1824#define	ADDSHORT(frm, v) do {			\
1825	frm[0] = (v) & 0xff;			\
1826	frm[1] = (v) >> 8;			\
1827	frm += 2;				\
1828} while (0)
1829	struct ieee80211vap *vap = ni->ni_vap;
1830	struct ieee80211com *ic = ni->ni_ic;
1831	struct mbuf *m;
1832	uint8_t *frm;
1833	uint16_t baparamset;
1834	int ret;
1835
1836	KASSERT(ni != NULL, ("null node"));
1837
1838	/*
1839	 * Hold a reference on the node so it doesn't go away until after
1840	 * the xmit is complete all the way in the driver.  On error we
1841	 * will remove our reference.
1842	 */
1843	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1844		"ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
1845		__func__, __LINE__,
1846		ni, ether_sprintf(ni->ni_macaddr),
1847		ieee80211_node_refcnt(ni)+1);
1848	ieee80211_ref_node(ni);
1849
1850	m = ieee80211_getmgtframe(&frm,
1851		ic->ic_headroom + sizeof(struct ieee80211_frame),
1852		  sizeof(uint16_t)	/* action+category */
1853		/* XXX may action payload */
1854		+ sizeof(struct ieee80211_action_ba_addbaresponse)
1855	);
1856	if (m == NULL)
1857		senderr(ENOMEM, is_tx_nobuf);
1858
1859	*frm++ = category;
1860	*frm++ = action;
1861	switch (category) {
1862	case IEEE80211_ACTION_CAT_BA:
1863		switch (action) {
1864		case IEEE80211_ACTION_BA_ADDBA_REQUEST:
1865			IEEE80211_NOTE(vap,
1866			    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1867			    "send ADDBA request: dialogtoken %d "
1868			    "baparamset 0x%x (tid %d) batimeout 0x%x baseqctl 0x%x",
1869			    args[0], args[1], MS(args[1], IEEE80211_BAPS_TID),
1870			    args[2], args[3]);
1871
1872			*frm++ = args[0];	/* dialog token */
1873			ADDSHORT(frm, args[1]);	/* baparamset */
1874			ADDSHORT(frm, args[2]);	/* batimeout */
1875			ADDSHORT(frm, args[3]);	/* baseqctl */
1876			break;
1877		case IEEE80211_ACTION_BA_ADDBA_RESPONSE:
1878			IEEE80211_NOTE(vap,
1879			    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1880			    "send ADDBA response: dialogtoken %d status %d "
1881			    "baparamset 0x%x (tid %d) batimeout %d",
1882			    args[0], args[1], args[2],
1883			    MS(args[2], IEEE80211_BAPS_TID), args[3]);
1884
1885			*frm++ = args[0];	/* dialog token */
1886			ADDSHORT(frm, args[1]);	/* statuscode */
1887			ADDSHORT(frm, args[2]);	/* baparamset */
1888			ADDSHORT(frm, args[3]);	/* batimeout */
1889			break;
1890		case IEEE80211_ACTION_BA_DELBA:
1891			/* XXX */
1892			baparamset = SM(args[0], IEEE80211_DELBAPS_TID)
1893				   | args[1]
1894				   ;
1895			ADDSHORT(frm, baparamset);
1896			ADDSHORT(frm, args[2]);	/* reason code */
1897
1898			IEEE80211_NOTE(vap,
1899			    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1900			    "send DELBA action: tid %d, initiator %d reason %d",
1901			    args[0], args[1], args[2]);
1902			break;
1903		default:
1904			goto badaction;
1905		}
1906		break;
1907	case IEEE80211_ACTION_CAT_HT:
1908		switch (action) {
1909		case IEEE80211_ACTION_HT_TXCHWIDTH:
1910			IEEE80211_NOTE(vap,
1911			    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1912			    ni, "send HT txchwidth: width %d",
1913			    IEEE80211_IS_CHAN_HT40(ni->ni_chan) ? 40 : 20
1914			);
1915			*frm++ = IEEE80211_IS_CHAN_HT40(ni->ni_chan) ?
1916				IEEE80211_A_HT_TXCHWIDTH_2040 :
1917				IEEE80211_A_HT_TXCHWIDTH_20;
1918			break;
1919		default:
1920			goto badaction;
1921		}
1922		break;
1923	default:
1924	badaction:
1925		IEEE80211_NOTE(vap,
1926		    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1927		    "%s: unsupported category %d action %d", __func__,
1928		    category, action);
1929		senderr(EINVAL, is_tx_unknownmgt);
1930		/* NOTREACHED */
1931	}
1932	m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
1933
1934	return ieee80211_mgmt_output(ni, m, IEEE80211_FC0_SUBTYPE_ACTION);
1935bad:
1936	ieee80211_free_node(ni);
1937	if (m != NULL)
1938		m_freem(m);
1939	return ret;
1940#undef ADDSHORT
1941#undef senderr
1942}
1943
1944/*
1945 * Construct the MCS bit mask for inclusion
1946 * in an HT information element.
1947 */
1948static void
1949ieee80211_set_htrates(uint8_t *frm, const struct ieee80211_htrateset *rs)
1950{
1951	int i;
1952
1953	for (i = 0; i < rs->rs_nrates; i++) {
1954		int r = rs->rs_rates[i] & IEEE80211_RATE_VAL;
1955		if (r < IEEE80211_HTRATE_MAXSIZE) {	/* XXX? */
1956			/* NB: this assumes a particular implementation */
1957			setbit(frm, r);
1958		}
1959	}
1960}
1961
1962/*
1963 * Add body of an HTCAP information element.
1964 */
1965static uint8_t *
1966ieee80211_add_htcap_body(uint8_t *frm, struct ieee80211_node *ni)
1967{
1968#define	ADDSHORT(frm, v) do {			\
1969	frm[0] = (v) & 0xff;			\
1970	frm[1] = (v) >> 8;			\
1971	frm += 2;				\
1972} while (0)
1973	struct ieee80211vap *vap = ni->ni_vap;
1974	uint16_t caps;
1975	int rxmax, density;
1976
1977	/* HT capabilities */
1978	caps = vap->iv_htcaps & 0xffff;
1979	/*
1980	 * Note channel width depends on whether we are operating as
1981	 * a sta or not.  When operating as a sta we are generating
1982	 * a request based on our desired configuration.  Otherwise
1983	 * we are operational and the channel attributes identify
1984	 * how we've been setup (which might be different if a fixed
1985	 * channel is specified).
1986	 */
1987	if (vap->iv_opmode == IEEE80211_M_STA) {
1988		/* override 20/40 use based on config */
1989		if (vap->iv_flags_ext & IEEE80211_FEXT_USEHT40)
1990			caps |= IEEE80211_HTCAP_CHWIDTH40;
1991		else
1992			caps &= ~IEEE80211_HTCAP_CHWIDTH40;
1993		/* use advertised setting (XXX locally constraint) */
1994		rxmax = MS(ni->ni_htparam, IEEE80211_HTCAP_MAXRXAMPDU);
1995		density = MS(ni->ni_htparam, IEEE80211_HTCAP_MPDUDENSITY);
1996	} else {
1997		/* override 20/40 use based on current channel */
1998		if (IEEE80211_IS_CHAN_HT40(ni->ni_chan))
1999			caps |= IEEE80211_HTCAP_CHWIDTH40;
2000		else
2001			caps &= ~IEEE80211_HTCAP_CHWIDTH40;
2002		rxmax = vap->iv_ampdu_rxmax;
2003		density = vap->iv_ampdu_density;
2004	}
2005	/* adjust short GI based on channel and config */
2006	if ((vap->iv_flags_ext & IEEE80211_FEXT_SHORTGI20) == 0)
2007		caps &= ~IEEE80211_HTCAP_SHORTGI20;
2008	if ((vap->iv_flags_ext & IEEE80211_FEXT_SHORTGI40) == 0 ||
2009	    (caps & IEEE80211_HTCAP_CHWIDTH40) == 0)
2010		caps &= ~IEEE80211_HTCAP_SHORTGI40;
2011	ADDSHORT(frm, caps);
2012
2013	/* HT parameters */
2014	*frm = SM(rxmax, IEEE80211_HTCAP_MAXRXAMPDU)
2015	     | SM(density, IEEE80211_HTCAP_MPDUDENSITY)
2016	     ;
2017	frm++;
2018
2019	/* pre-zero remainder of ie */
2020	memset(frm, 0, sizeof(struct ieee80211_ie_htcap) -
2021		__offsetof(struct ieee80211_ie_htcap, hc_mcsset));
2022
2023	/* supported MCS set */
2024	/*
2025	 * XXX it would better to get the rate set from ni_htrates
2026	 * so we can restrict it but for sta mode ni_htrates isn't
2027	 * setup when we're called to form an AssocReq frame so for
2028	 * now we're restricted to the default HT rate set.
2029	 */
2030	ieee80211_set_htrates(frm, &ieee80211_rateset_11n);
2031
2032	frm += sizeof(struct ieee80211_ie_htcap) -
2033		__offsetof(struct ieee80211_ie_htcap, hc_mcsset);
2034	return frm;
2035#undef ADDSHORT
2036}
2037
2038/*
2039 * Add 802.11n HT capabilities information element
2040 */
2041uint8_t *
2042ieee80211_add_htcap(uint8_t *frm, struct ieee80211_node *ni)
2043{
2044	frm[0] = IEEE80211_ELEMID_HTCAP;
2045	frm[1] = sizeof(struct ieee80211_ie_htcap) - 2;
2046	return ieee80211_add_htcap_body(frm + 2, ni);
2047}
2048
2049/*
2050 * Add Broadcom OUI wrapped standard HTCAP ie; this is
2051 * used for compatibility w/ pre-draft implementations.
2052 */
2053uint8_t *
2054ieee80211_add_htcap_vendor(uint8_t *frm, struct ieee80211_node *ni)
2055{
2056	frm[0] = IEEE80211_ELEMID_VENDOR;
2057	frm[1] = 4 + sizeof(struct ieee80211_ie_htcap) - 2;
2058	frm[2] = (BCM_OUI >> 0) & 0xff;
2059	frm[3] = (BCM_OUI >> 8) & 0xff;
2060	frm[4] = (BCM_OUI >> 16) & 0xff;
2061	frm[5] = BCM_OUI_HTCAP;
2062	return ieee80211_add_htcap_body(frm + 6, ni);
2063}
2064
2065/*
2066 * Construct the MCS bit mask of basic rates
2067 * for inclusion in an HT information element.
2068 */
2069static void
2070ieee80211_set_basic_htrates(uint8_t *frm, const struct ieee80211_htrateset *rs)
2071{
2072	int i;
2073
2074	for (i = 0; i < rs->rs_nrates; i++) {
2075		int r = rs->rs_rates[i] & IEEE80211_RATE_VAL;
2076		if ((rs->rs_rates[i] & IEEE80211_RATE_BASIC) &&
2077		    r < IEEE80211_HTRATE_MAXSIZE) {
2078			/* NB: this assumes a particular implementation */
2079			setbit(frm, r);
2080		}
2081	}
2082}
2083
2084/*
2085 * Update the HTINFO ie for a beacon frame.
2086 */
2087void
2088ieee80211_ht_update_beacon(struct ieee80211vap *vap,
2089	struct ieee80211_beacon_offsets *bo)
2090{
2091#define	PROTMODE	(IEEE80211_HTINFO_OPMODE|IEEE80211_HTINFO_NONHT_PRESENT)
2092	const struct ieee80211_channel *bsschan = vap->iv_bss->ni_chan;
2093	struct ieee80211com *ic = vap->iv_ic;
2094	struct ieee80211_ie_htinfo *ht =
2095	   (struct ieee80211_ie_htinfo *) bo->bo_htinfo;
2096
2097	/* XXX only update on channel change */
2098	ht->hi_ctrlchannel = ieee80211_chan2ieee(ic, bsschan);
2099	ht->hi_byte1 = IEEE80211_HTINFO_RIFSMODE_PROH;
2100	if (IEEE80211_IS_CHAN_HT40U(bsschan))
2101		ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_ABOVE;
2102	else if (IEEE80211_IS_CHAN_HT40D(bsschan))
2103		ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_BELOW;
2104	else
2105		ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_NONE;
2106	if (IEEE80211_IS_CHAN_HT40(bsschan))
2107		ht->hi_byte1 |= IEEE80211_HTINFO_TXWIDTH_2040;
2108
2109	/* protection mode */
2110	ht->hi_byte2 = (ht->hi_byte2 &~ PROTMODE) | ic->ic_curhtprotmode;
2111
2112	/* XXX propagate to vendor ie's */
2113#undef PROTMODE
2114}
2115
2116/*
2117 * Add body of an HTINFO information element.
2118 *
2119 * NB: We don't use struct ieee80211_ie_htinfo because we can
2120 * be called to fillin both a standard ie and a compat ie that
2121 * has a vendor OUI at the front.
2122 */
2123static uint8_t *
2124ieee80211_add_htinfo_body(uint8_t *frm, struct ieee80211_node *ni)
2125{
2126	struct ieee80211com *ic = ni->ni_ic;
2127
2128	/* pre-zero remainder of ie */
2129	memset(frm, 0, sizeof(struct ieee80211_ie_htinfo) - 2);
2130
2131	/* primary/control channel center */
2132	*frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
2133
2134	frm[0] = IEEE80211_HTINFO_RIFSMODE_PROH;
2135	if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan))
2136		frm[0] |= IEEE80211_HTINFO_2NDCHAN_ABOVE;
2137	else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan))
2138		frm[0] |= IEEE80211_HTINFO_2NDCHAN_BELOW;
2139	else
2140		frm[0] |= IEEE80211_HTINFO_2NDCHAN_NONE;
2141	if (IEEE80211_IS_CHAN_HT40(ni->ni_chan))
2142		frm[0] |= IEEE80211_HTINFO_TXWIDTH_2040;
2143
2144	frm[1] = ic->ic_curhtprotmode;
2145
2146	frm += 5;
2147
2148	/* basic MCS set */
2149	ieee80211_set_basic_htrates(frm, &ni->ni_htrates);
2150	frm += sizeof(struct ieee80211_ie_htinfo) -
2151		__offsetof(struct ieee80211_ie_htinfo, hi_basicmcsset);
2152	return frm;
2153}
2154
2155/*
2156 * Add 802.11n HT information information element.
2157 */
2158uint8_t *
2159ieee80211_add_htinfo(uint8_t *frm, struct ieee80211_node *ni)
2160{
2161	frm[0] = IEEE80211_ELEMID_HTINFO;
2162	frm[1] = sizeof(struct ieee80211_ie_htinfo) - 2;
2163	return ieee80211_add_htinfo_body(frm + 2, ni);
2164}
2165
2166/*
2167 * Add Broadcom OUI wrapped standard HTINFO ie; this is
2168 * used for compatibility w/ pre-draft implementations.
2169 */
2170uint8_t *
2171ieee80211_add_htinfo_vendor(uint8_t *frm, struct ieee80211_node *ni)
2172{
2173	frm[0] = IEEE80211_ELEMID_VENDOR;
2174	frm[1] = 4 + sizeof(struct ieee80211_ie_htinfo) - 2;
2175	frm[2] = (BCM_OUI >> 0) & 0xff;
2176	frm[3] = (BCM_OUI >> 8) & 0xff;
2177	frm[4] = (BCM_OUI >> 16) & 0xff;
2178	frm[5] = BCM_OUI_HTINFO;
2179	return ieee80211_add_htinfo_body(frm + 6, ni);
2180}
2181