ieee80211_ht.c revision 178354
1170530Ssam/*-
2178354Ssam * Copyright (c) 2007-2008 Sam Leffler, Errno Consulting
3170530Ssam * All rights reserved.
4170530Ssam *
5170530Ssam * Redistribution and use in source and binary forms, with or without
6170530Ssam * modification, are permitted provided that the following conditions
7170530Ssam * are met:
8170530Ssam * 1. Redistributions of source code must retain the above copyright
9170530Ssam *    notice, this list of conditions and the following disclaimer.
10170530Ssam * 2. Redistributions in binary form must reproduce the above copyright
11170530Ssam *    notice, this list of conditions and the following disclaimer in the
12170530Ssam *    documentation and/or other materials provided with the distribution.
13170530Ssam *
14170530Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15170530Ssam * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16170530Ssam * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17170530Ssam * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18170530Ssam * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19170530Ssam * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20170530Ssam * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21170530Ssam * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22170530Ssam * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23170530Ssam * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24170530Ssam */
25170530Ssam
26170530Ssam#include <sys/cdefs.h>
27170530Ssam#ifdef __FreeBSD__
28170530Ssam__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_ht.c 178354 2008-04-20 20:35:46Z sam $");
29170530Ssam#endif
30170530Ssam
31170530Ssam/*
32170530Ssam * IEEE 802.11n protocol support.
33170530Ssam */
34170530Ssam
35170530Ssam#include "opt_inet.h"
36178354Ssam#include "opt_wlan.h"
37170530Ssam
38170530Ssam#include <sys/param.h>
39170530Ssam#include <sys/kernel.h>
40170530Ssam#include <sys/systm.h>
41170530Ssam#include <sys/endian.h>
42170530Ssam
43170530Ssam#include <sys/socket.h>
44170530Ssam
45170530Ssam#include <net/if.h>
46170530Ssam#include <net/if_media.h>
47170530Ssam#include <net/ethernet.h>
48170530Ssam
49170530Ssam#include <net80211/ieee80211_var.h>
50178354Ssam#include <net80211/ieee80211_input.h>
51170530Ssam
52170530Ssam/* define here, used throughout file */
53170530Ssam#define	MS(_v, _f)	(((_v) & _f) >> _f##_S)
54170530Ssam#define	SM(_v, _f)	(((_v) << _f##_S) & _f)
55170530Ssam
56178354Ssamconst struct ieee80211_mcs_rates ieee80211_htrates[16] = {
57178354Ssam	{  13,  14,  27,  30 },	/* MCS 0 */
58178354Ssam	{  26,  29,  54,  60 },	/* MCS 1 */
59178354Ssam	{  39,  43,  81,  90 },	/* MCS 2 */
60178354Ssam	{  52,  58, 108, 120 },	/* MCS 3 */
61178354Ssam	{  78,  87, 162, 180 },	/* MCS 4 */
62178354Ssam	{ 104, 116, 216, 240 },	/* MCS 5 */
63178354Ssam	{ 117, 130, 243, 270 },	/* MCS 6 */
64178354Ssam	{ 130, 144, 270, 300 },	/* MCS 7 */
65178354Ssam	{  26,  29,  54,  60 },	/* MCS 8 */
66178354Ssam	{  52,  58, 108, 120 },	/* MCS 9 */
67178354Ssam	{  78,  87, 162, 180 },	/* MCS 10 */
68178354Ssam	{ 104, 116, 216, 240 },	/* MCS 11 */
69178354Ssam	{ 156, 173, 324, 360 },	/* MCS 12 */
70178354Ssam	{ 208, 231, 432, 480 },	/* MCS 13 */
71178354Ssam	{ 234, 260, 486, 540 },	/* MCS 14 */
72178354Ssam	{ 260, 289, 540, 600 }	/* MCS 15 */
73170530Ssam};
74170530Ssam
75170530Ssamstatic const struct ieee80211_htrateset ieee80211_rateset_11n =
76170530Ssam	{ 16, {
77170530Ssam	          0,   1,   2,   3,   4,  5,   6,  7,  8,  9,
78170530Ssam		 10,  11,  12,  13,  14,  15 }
79170530Ssam	};
80170530Ssam
81173273Ssam#ifdef IEEE80211_AMPDU_AGE
82173273Ssam/* XXX public for sysctl hookup */
83173273Ssamint	ieee80211_ampdu_age = -1;	/* threshold for ampdu reorder q (ms) */
84173273Ssam#endif
85173273Ssamint	ieee80211_recv_bar_ena = 1;
86178354Ssamint	ieee80211_addba_timeout = -1;	/* timeout waiting for ADDBA response */
87178354Ssamint	ieee80211_addba_backoff = -1;	/* backoff after max ADDBA requests */
88178354Ssamint	ieee80211_addba_maxtries = 3;	/* max ADDBA requests before backoff */
89173273Ssam
90178354Ssam/*
91178354Ssam * Setup HT parameters that depends on the clock frequency.
92178354Ssam */
93178354Ssamstatic void
94178354Ssamieee80211_ht_setup(void)
95178354Ssam{
96178354Ssam#ifdef IEEE80211_AMPDU_AGE
97178354Ssam	ieee80211_ampdu_age = msecs_to_ticks(500);
98178354Ssam#endif
99178354Ssam	ieee80211_addba_timeout = msecs_to_ticks(250);
100178354Ssam	ieee80211_addba_backoff = msecs_to_ticks(10*1000);
101178354Ssam}
102178354SsamSYSINIT(wlan_ht, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_ht_setup, NULL);
103170530Ssam
104178354Ssamstatic int ieee80211_ampdu_enable(struct ieee80211_node *ni,
105178354Ssam	struct ieee80211_tx_ampdu *tap);
106170530Ssamstatic int ieee80211_addba_request(struct ieee80211_node *ni,
107170530Ssam	struct ieee80211_tx_ampdu *tap,
108170530Ssam	int dialogtoken, int baparamset, int batimeout);
109170530Ssamstatic int ieee80211_addba_response(struct ieee80211_node *ni,
110170530Ssam	struct ieee80211_tx_ampdu *tap,
111170530Ssam	int code, int baparamset, int batimeout);
112170530Ssamstatic void ieee80211_addba_stop(struct ieee80211_node *ni,
113170530Ssam	struct ieee80211_tx_ampdu *tap);
114170530Ssamstatic void ieee80211_aggr_recv_action(struct ieee80211_node *ni,
115170530Ssam	const uint8_t *frm, const uint8_t *efrm);
116170530Ssam
117170530Ssamvoid
118170530Ssamieee80211_ht_attach(struct ieee80211com *ic)
119170530Ssam{
120170530Ssam	/* setup default aggregation policy */
121170530Ssam	ic->ic_recv_action = ieee80211_aggr_recv_action;
122170530Ssam	ic->ic_send_action = ieee80211_send_action;
123178354Ssam	ic->ic_ampdu_enable = ieee80211_ampdu_enable;
124170530Ssam	ic->ic_addba_request = ieee80211_addba_request;
125170530Ssam	ic->ic_addba_response = ieee80211_addba_response;
126170530Ssam	ic->ic_addba_stop = ieee80211_addba_stop;
127170530Ssam
128173273Ssam	ic->ic_htprotmode = IEEE80211_PROT_RTSCTS;
129173273Ssam	ic->ic_curhtprotmode = IEEE80211_HTINFO_OPMODE_PURE;
130178354Ssam}
131173273Ssam
132178354Ssamvoid
133178354Ssamieee80211_ht_detach(struct ieee80211com *ic)
134178354Ssam{
135178354Ssam}
136173273Ssam
137178354Ssamvoid
138178354Ssamieee80211_ht_vattach(struct ieee80211vap *vap)
139178354Ssam{
140178354Ssam
141178354Ssam	/* driver can override defaults */
142178354Ssam	vap->iv_ampdu_rxmax = IEEE80211_HTCAP_MAXRXAMPDU_8K;
143178354Ssam	vap->iv_ampdu_density = IEEE80211_HTCAP_MPDUDENSITY_NA;
144178354Ssam	vap->iv_ampdu_limit = vap->iv_ampdu_rxmax;
145178354Ssam	vap->iv_amsdu_limit = vap->iv_htcaps & IEEE80211_HTCAP_MAXAMSDU;
146178354Ssam	/* tx aggregation traffic thresholds */
147178354Ssam	vap->iv_ampdu_mintraffic[WME_AC_BK] = 128;
148178354Ssam	vap->iv_ampdu_mintraffic[WME_AC_BE] = 64;
149178354Ssam	vap->iv_ampdu_mintraffic[WME_AC_VO] = 32;
150178354Ssam	vap->iv_ampdu_mintraffic[WME_AC_VI] = 32;
151178354Ssam
152178354Ssam	if (vap->iv_htcaps & IEEE80211_HTC_HT) {
153170530Ssam		/*
154173273Ssam		 * Device is HT capable; enable all HT-related
155173273Ssam		 * facilities by default.
156170530Ssam		 * XXX these choices may be too aggressive.
157170530Ssam		 */
158178354Ssam		vap->iv_flags_ext |= IEEE80211_FEXT_HT
159178354Ssam				  |  IEEE80211_FEXT_HTCOMPAT
160178354Ssam				  ;
161178354Ssam		if (vap->iv_htcaps & IEEE80211_HTCAP_SHORTGI20)
162178354Ssam			vap->iv_flags_ext |= IEEE80211_FEXT_SHORTGI20;
163173273Ssam		/* XXX infer from channel list? */
164178354Ssam		if (vap->iv_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
165178354Ssam			vap->iv_flags_ext |= IEEE80211_FEXT_USEHT40;
166178354Ssam			if (vap->iv_htcaps & IEEE80211_HTCAP_SHORTGI40)
167178354Ssam				vap->iv_flags_ext |= IEEE80211_FEXT_SHORTGI40;
168170530Ssam		}
169170530Ssam		/* NB: A-MPDU and A-MSDU rx are mandated, these are tx only */
170178354Ssam		vap->iv_flags_ext |= IEEE80211_FEXT_AMPDU_RX;
171178354Ssam		if (vap->iv_htcaps & IEEE80211_HTC_AMPDU)
172178354Ssam			vap->iv_flags_ext |= IEEE80211_FEXT_AMPDU_TX;
173178354Ssam		vap->iv_flags_ext |= IEEE80211_FEXT_AMSDU_RX;
174178354Ssam		if (vap->iv_htcaps & IEEE80211_HTC_AMSDU)
175178354Ssam			vap->iv_flags_ext |= IEEE80211_FEXT_AMSDU_TX;
176170530Ssam	}
177178354Ssam	/* NB: disable default legacy WDS, too many issues right now */
178178354Ssam	if (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY)
179178354Ssam		vap->iv_flags_ext &= ~IEEE80211_FEXT_HT;
180170530Ssam}
181170530Ssam
182170530Ssamvoid
183178354Ssamieee80211_ht_vdetach(struct ieee80211vap *vap)
184170530Ssam{
185170530Ssam}
186170530Ssam
187170530Ssamstatic void
188170530Ssamht_announce(struct ieee80211com *ic, int mode,
189170530Ssam	const struct ieee80211_htrateset *rs)
190170530Ssam{
191170530Ssam	struct ifnet *ifp = ic->ic_ifp;
192170530Ssam	int i, rate, mword;
193170530Ssam
194170530Ssam	if_printf(ifp, "%s MCS: ", ieee80211_phymode_name[mode]);
195170530Ssam	for (i = 0; i < rs->rs_nrates; i++) {
196172226Ssam		mword = ieee80211_rate2media(ic,
197172226Ssam		    rs->rs_rates[i] | IEEE80211_RATE_MCS, mode);
198170530Ssam		if (IFM_SUBTYPE(mword) != IFM_IEEE80211_MCS)
199170530Ssam			continue;
200178354Ssam		rate = ieee80211_htrates[rs->rs_rates[i]].ht40_rate_400ns;
201170530Ssam		printf("%s%d%sMbps", (i != 0 ? " " : ""),
202170530Ssam		    rate / 2, ((rate & 0x1) != 0 ? ".5" : ""));
203170530Ssam	}
204170530Ssam	printf("\n");
205170530Ssam}
206170530Ssam
207170530Ssamvoid
208170530Ssamieee80211_ht_announce(struct ieee80211com *ic)
209170530Ssam{
210170530Ssam	if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA))
211170530Ssam		ht_announce(ic, IEEE80211_MODE_11NA, &ieee80211_rateset_11n);
212170530Ssam	if (isset(ic->ic_modecaps, IEEE80211_MODE_11NG))
213170530Ssam		ht_announce(ic, IEEE80211_MODE_11NG, &ieee80211_rateset_11n);
214170530Ssam}
215170530Ssam
216170530Ssamconst struct ieee80211_htrateset *
217170530Ssamieee80211_get_suphtrates(struct ieee80211com *ic,
218170530Ssam	const struct ieee80211_channel *c)
219170530Ssam{
220173273Ssam	return &ieee80211_rateset_11n;
221170530Ssam}
222170530Ssam
223170530Ssam/*
224170530Ssam * Receive processing.
225170530Ssam */
226170530Ssam
227170530Ssam/*
228170530Ssam * Decap the encapsulated A-MSDU frames and dispatch all but
229170530Ssam * the last for delivery.  The last frame is returned for
230170530Ssam * delivery via the normal path.
231170530Ssam */
232170530Ssamstruct mbuf *
233170530Ssamieee80211_decap_amsdu(struct ieee80211_node *ni, struct mbuf *m)
234170530Ssam{
235178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
236173462Ssam	int framelen;
237170530Ssam	struct mbuf *n;
238170530Ssam
239170530Ssam	/* discard 802.3 header inserted by ieee80211_decap */
240170530Ssam	m_adj(m, sizeof(struct ether_header));
241170530Ssam
242178354Ssam	vap->iv_stats.is_amsdu_decap++;
243170530Ssam
244170530Ssam	for (;;) {
245170530Ssam		/*
246170530Ssam		 * Decap the first frame, bust it apart from the
247170530Ssam		 * remainder and deliver.  We leave the last frame
248170530Ssam		 * delivery to the caller (for consistency with other
249170530Ssam		 * code paths, could also do it here).
250170530Ssam		 */
251170530Ssam		m = ieee80211_decap1(m, &framelen);
252170530Ssam		if (m == NULL) {
253178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
254173462Ssam			    ni->ni_macaddr, "a-msdu", "%s", "decap failed");
255178354Ssam			vap->iv_stats.is_amsdu_tooshort++;
256170530Ssam			return NULL;
257170530Ssam		}
258173462Ssam		if (m->m_pkthdr.len == framelen)
259170530Ssam			break;
260170530Ssam		n = m_split(m, framelen, M_NOWAIT);
261170530Ssam		if (n == NULL) {
262178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
263170530Ssam			    ni->ni_macaddr, "a-msdu",
264170530Ssam			    "%s", "unable to split encapsulated frames");
265178354Ssam			vap->iv_stats.is_amsdu_split++;
266170530Ssam			m_freem(m);			/* NB: must reclaim */
267170530Ssam			return NULL;
268170530Ssam		}
269178354Ssam		vap->iv_deliver_data(vap, ni, m);
270170530Ssam
271170530Ssam		/*
272170530Ssam		 * Remove frame contents; each intermediate frame
273170530Ssam		 * is required to be aligned to a 4-byte boundary.
274170530Ssam		 */
275170530Ssam		m = n;
276170530Ssam		m_adj(m, roundup2(framelen, 4) - framelen);	/* padding */
277170530Ssam	}
278170530Ssam	return m;				/* last delivered by caller */
279170530Ssam}
280170530Ssam
281170530Ssam/*
282170530Ssam * Purge all frames in the A-MPDU re-order queue.
283170530Ssam */
284170530Ssamstatic void
285170530Ssamampdu_rx_purge(struct ieee80211_rx_ampdu *rap)
286170530Ssam{
287170530Ssam	struct mbuf *m;
288170530Ssam	int i;
289170530Ssam
290170530Ssam	for (i = 0; i < rap->rxa_wnd; i++) {
291170530Ssam		m = rap->rxa_m[i];
292170530Ssam		if (m != NULL) {
293170530Ssam			rap->rxa_m[i] = NULL;
294170530Ssam			rap->rxa_qbytes -= m->m_pkthdr.len;
295170530Ssam			m_freem(m);
296170530Ssam			if (--rap->rxa_qframes == 0)
297170530Ssam				break;
298170530Ssam		}
299170530Ssam	}
300170530Ssam	KASSERT(rap->rxa_qbytes == 0 && rap->rxa_qframes == 0,
301170530Ssam	    ("lost %u data, %u frames on ampdu rx q",
302170530Ssam	    rap->rxa_qbytes, rap->rxa_qframes));
303170530Ssam}
304170530Ssam
305170530Ssam/*
306178354Ssam * Start A-MPDU rx/re-order processing for the specified TID.
307178354Ssam */
308178354Ssamstatic void
309178354Ssamampdu_rx_start(struct ieee80211_rx_ampdu *rap, int bufsiz, int start)
310178354Ssam{
311178354Ssam	if (rap->rxa_flags & IEEE80211_AGGR_RUNNING) {
312178354Ssam		/*
313178354Ssam		 * AMPDU previously setup and not terminated with a DELBA,
314178354Ssam		 * flush the reorder q's in case anything remains.
315178354Ssam		 */
316178354Ssam		ampdu_rx_purge(rap);
317178354Ssam	}
318178354Ssam	memset(rap, 0, sizeof(*rap));
319178354Ssam	rap->rxa_wnd = (bufsiz == 0) ?
320178354Ssam	    IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
321178354Ssam	rap->rxa_start = start;
322178354Ssam	rap->rxa_flags |=  IEEE80211_AGGR_RUNNING | IEEE80211_AGGR_XCHGPEND;
323178354Ssam}
324178354Ssam
325178354Ssam/*
326170530Ssam * Stop A-MPDU rx processing for the specified TID.
327170530Ssam */
328170530Ssamstatic void
329170530Ssamampdu_rx_stop(struct ieee80211_rx_ampdu *rap)
330170530Ssam{
331170530Ssam	ampdu_rx_purge(rap);
332178354Ssam	rap->rxa_flags &= ~(IEEE80211_AGGR_RUNNING | IEEE80211_AGGR_XCHGPEND);
333170530Ssam}
334170530Ssam
335170530Ssam/*
336170530Ssam * Dispatch a frame from the A-MPDU reorder queue.  The
337170530Ssam * frame is fed back into ieee80211_input marked with an
338170530Ssam * M_AMPDU flag so it doesn't come back to us (it also
339170530Ssam * permits ieee80211_input to optimize re-processing).
340170530Ssam */
341170530Ssamstatic __inline void
342170530Ssamampdu_dispatch(struct ieee80211_node *ni, struct mbuf *m)
343170530Ssam{
344170530Ssam	m->m_flags |= M_AMPDU;	/* bypass normal processing */
345170530Ssam	/* NB: rssi, noise, and rstamp are ignored w/ M_AMPDU set */
346178354Ssam	(void) ieee80211_input(ni, m, 0, 0, 0);
347170530Ssam}
348170530Ssam
349170530Ssam/*
350170530Ssam * Dispatch as many frames as possible from the re-order queue.
351170530Ssam * Frames will always be "at the front"; we process all frames
352170530Ssam * up to the first empty slot in the window.  On completion we
353170530Ssam * cleanup state if there are still pending frames in the current
354170530Ssam * BA window.  We assume the frame at slot 0 is already handled
355170530Ssam * by the caller; we always start at slot 1.
356170530Ssam */
357170530Ssamstatic void
358170530Ssamampdu_rx_dispatch(struct ieee80211_rx_ampdu *rap, struct ieee80211_node *ni)
359170530Ssam{
360178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
361170530Ssam	struct mbuf *m;
362170530Ssam	int i;
363170530Ssam
364170530Ssam	/* flush run of frames */
365170530Ssam	for (i = 1; i < rap->rxa_wnd; i++) {
366170530Ssam		m = rap->rxa_m[i];
367170530Ssam		if (m == NULL)
368170530Ssam			break;
369170530Ssam		rap->rxa_m[i] = NULL;
370170530Ssam		rap->rxa_qbytes -= m->m_pkthdr.len;
371170530Ssam		rap->rxa_qframes--;
372170530Ssam
373170530Ssam		ampdu_dispatch(ni, m);
374170530Ssam	}
375170530Ssam	/*
376170530Ssam	 * If frames remain, copy the mbuf pointers down so
377170530Ssam	 * they correspond to the offsets in the new window.
378170530Ssam	 */
379170530Ssam	if (rap->rxa_qframes != 0) {
380170530Ssam		int n = rap->rxa_qframes, j;
381170530Ssam		for (j = i+1; j < rap->rxa_wnd; j++) {
382170530Ssam			if (rap->rxa_m[j] != NULL) {
383170530Ssam				rap->rxa_m[j-i] = rap->rxa_m[j];
384170530Ssam				rap->rxa_m[j] = NULL;
385170530Ssam				if (--n == 0)
386170530Ssam					break;
387170530Ssam			}
388170530Ssam		}
389170530Ssam		KASSERT(n == 0, ("lost %d frames", n));
390178354Ssam		vap->iv_stats.is_ampdu_rx_copy += rap->rxa_qframes;
391170530Ssam	}
392173273Ssam	/*
393173273Ssam	 * Adjust the start of the BA window to
394173273Ssam	 * reflect the frames just dispatched.
395173273Ssam	 */
396173273Ssam	rap->rxa_start = IEEE80211_SEQ_ADD(rap->rxa_start, i);
397178354Ssam	vap->iv_stats.is_ampdu_rx_oor += i;
398170530Ssam}
399170530Ssam
400173273Ssam#ifdef IEEE80211_AMPDU_AGE
401170530Ssam/*
402173273Ssam * Dispatch all frames in the A-MPDU re-order queue.
403170530Ssam */
404170530Ssamstatic void
405173273Ssamampdu_rx_flush(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
406170530Ssam{
407178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
408170530Ssam	struct mbuf *m;
409170530Ssam	int i;
410170530Ssam
411173273Ssam	for (i = 0; i < rap->rxa_wnd; i++) {
412170530Ssam		m = rap->rxa_m[i];
413170530Ssam		if (m == NULL)
414170530Ssam			continue;
415170530Ssam		rap->rxa_m[i] = NULL;
416170530Ssam		rap->rxa_qbytes -= m->m_pkthdr.len;
417173273Ssam		rap->rxa_qframes--;
418178354Ssam		vap->iv_stats.is_ampdu_rx_oor++;
419173273Ssam
420170530Ssam		ampdu_dispatch(ni, m);
421173273Ssam		if (rap->rxa_qframes == 0)
422170530Ssam			break;
423170530Ssam	}
424170530Ssam}
425173273Ssam#endif /* IEEE80211_AMPDU_AGE */
426170530Ssam
427170530Ssam/*
428173273Ssam * Dispatch all frames in the A-MPDU re-order queue
429173273Ssam * preceding the specified sequence number.  This logic
430173273Ssam * handles window moves due to a received MSDU or BAR.
431173273Ssam */
432173273Ssamstatic void
433173273Ssamampdu_rx_flush_upto(struct ieee80211_node *ni,
434173273Ssam	struct ieee80211_rx_ampdu *rap, ieee80211_seq winstart)
435173273Ssam{
436178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
437173273Ssam	struct mbuf *m;
438173273Ssam	ieee80211_seq seqno;
439173273Ssam	int i;
440173273Ssam
441173273Ssam	/*
442173273Ssam	 * Flush any complete MSDU's with a sequence number lower
443173273Ssam	 * than winstart.  Gaps may exist.  Note that we may actually
444173273Ssam	 * dispatch frames past winstart if a run continues; this is
445173273Ssam	 * an optimization that avoids having to do a separate pass
446173273Ssam	 * to dispatch frames after moving the BA window start.
447173273Ssam	 */
448173273Ssam	seqno = rap->rxa_start;
449173273Ssam	for (i = 0; i < rap->rxa_wnd; i++) {
450173273Ssam		m = rap->rxa_m[i];
451173273Ssam		if (m != NULL) {
452173273Ssam			rap->rxa_m[i] = NULL;
453173273Ssam			rap->rxa_qbytes -= m->m_pkthdr.len;
454173273Ssam			rap->rxa_qframes--;
455178354Ssam			vap->iv_stats.is_ampdu_rx_oor++;
456173273Ssam
457173273Ssam			ampdu_dispatch(ni, m);
458173273Ssam		} else {
459173273Ssam			if (!IEEE80211_SEQ_BA_BEFORE(seqno, winstart))
460173273Ssam				break;
461173273Ssam		}
462173273Ssam		seqno = IEEE80211_SEQ_INC(seqno);
463173273Ssam	}
464173273Ssam	/*
465173273Ssam	 * If frames remain, copy the mbuf pointers down so
466173273Ssam	 * they correspond to the offsets in the new window.
467173273Ssam	 */
468173273Ssam	if (rap->rxa_qframes != 0) {
469173273Ssam		int n = rap->rxa_qframes, j;
470178354Ssam
471178354Ssam		/* NB: this loop assumes i > 0 and/or rxa_m[0] is NULL */
472178354Ssam		KASSERT(rap->rxa_m[0] == NULL,
473178354Ssam		    ("%s: BA window slot 0 occupied", __func__));
474173273Ssam		for (j = i+1; j < rap->rxa_wnd; j++) {
475173273Ssam			if (rap->rxa_m[j] != NULL) {
476173273Ssam				rap->rxa_m[j-i] = rap->rxa_m[j];
477173273Ssam				rap->rxa_m[j] = NULL;
478173273Ssam				if (--n == 0)
479173273Ssam					break;
480173273Ssam			}
481173273Ssam		}
482173273Ssam		KASSERT(n == 0, ("%s: lost %d frames, qframes %d off %d "
483173273Ssam		    "BA win <%d:%d> winstart %d",
484173273Ssam		    __func__, n, rap->rxa_qframes, i, rap->rxa_start,
485173273Ssam		    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
486173273Ssam		    winstart));
487178354Ssam		vap->iv_stats.is_ampdu_rx_copy += rap->rxa_qframes;
488173273Ssam	}
489173273Ssam	/*
490173273Ssam	 * Move the start of the BA window; we use the
491173273Ssam	 * sequence number of the last MSDU that was
492173273Ssam	 * passed up the stack+1 or winstart if stopped on
493173273Ssam	 * a gap in the reorder buffer.
494173273Ssam	 */
495173273Ssam	rap->rxa_start = seqno;
496173273Ssam}
497173273Ssam
498173273Ssam/*
499170530Ssam * Process a received QoS data frame for an HT station.  Handle
500170530Ssam * A-MPDU reordering: if this frame is received out of order
501170530Ssam * and falls within the BA window hold onto it.  Otherwise if
502173273Ssam * this frame completes a run, flush any pending frames.  We
503170530Ssam * return 1 if the frame is consumed.  A 0 is returned if
504170530Ssam * the frame should be processed normally by the caller.
505170530Ssam */
506170530Ssamint
507170530Ssamieee80211_ampdu_reorder(struct ieee80211_node *ni, struct mbuf *m)
508170530Ssam{
509170530Ssam#define	IEEE80211_FC0_QOSDATA \
510170530Ssam	(IEEE80211_FC0_TYPE_DATA|IEEE80211_FC0_SUBTYPE_QOS|IEEE80211_FC0_VERSION_0)
511173273Ssam#define	PROCESS		0	/* caller should process frame */
512173273Ssam#define	CONSUMED	1	/* frame consumed, caller does nothing */
513178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
514170530Ssam	struct ieee80211_qosframe *wh;
515170530Ssam	struct ieee80211_rx_ampdu *rap;
516170530Ssam	ieee80211_seq rxseq;
517170530Ssam	uint8_t tid;
518170530Ssam	int off;
519170530Ssam
520170530Ssam	KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT sta"));
521170530Ssam
522170530Ssam	/* NB: m_len known to be sufficient */
523170530Ssam	wh = mtod(m, struct ieee80211_qosframe *);
524170530Ssam	KASSERT(wh->i_fc[0] == IEEE80211_FC0_QOSDATA, ("not QoS data"));
525170530Ssam
526173273Ssam	if ((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) == IEEE80211_FC1_DIR_DSTODS)
527173273Ssam		tid = ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0];
528173273Ssam	else
529173273Ssam		tid = wh->i_qos[0];
530173273Ssam	tid &= IEEE80211_QOS_TID;
531170530Ssam	rap = &ni->ni_rx_ampdu[tid];
532170530Ssam	if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
533170530Ssam		/*
534170530Ssam		 * No ADDBA request yet, don't touch.
535170530Ssam		 */
536173273Ssam		return PROCESS;
537170530Ssam	}
538170530Ssam	rxseq = le16toh(*(uint16_t *)wh->i_seq) >> IEEE80211_SEQ_SEQ_SHIFT;
539173273Ssam	rap->rxa_nframes++;
540173273Ssamagain:
541170530Ssam	if (rxseq == rap->rxa_start) {
542170530Ssam		/*
543170530Ssam		 * First frame in window.
544170530Ssam		 */
545170530Ssam		if (rap->rxa_qframes != 0) {
546170530Ssam			/*
547170530Ssam			 * Dispatch as many packets as we can.
548170530Ssam			 */
549170530Ssam			KASSERT(rap->rxa_m[0] == NULL, ("unexpected dup"));
550170530Ssam			ampdu_dispatch(ni, m);
551170530Ssam			ampdu_rx_dispatch(rap, ni);
552173273Ssam			return CONSUMED;
553170530Ssam		} else {
554170530Ssam			/*
555170530Ssam			 * In order; advance window and notify
556170530Ssam			 * caller to dispatch directly.
557170530Ssam			 */
558170530Ssam			rap->rxa_start = IEEE80211_SEQ_INC(rxseq);
559173273Ssam			return PROCESS;
560170530Ssam		}
561170530Ssam	}
562170530Ssam	/*
563173273Ssam	 * Frame is out of order; store if in the BA window.
564170530Ssam	 */
565170530Ssam	/* calculate offset in BA window */
566170530Ssam	off = IEEE80211_SEQ_SUB(rxseq, rap->rxa_start);
567173273Ssam	if (off < rap->rxa_wnd) {
568170530Ssam		/*
569173273Ssam		 * Common case (hopefully): in the BA window.
570173273Ssam		 * Sec 9.10.7.6 a) (D2.04 p.118 line 47)
571173273Ssam		 */
572173273Ssam#ifdef IEEE80211_AMPDU_AGE
573173273Ssam		/*
574173273Ssam		 * Check for frames sitting too long in the reorder queue.
575173273Ssam		 * This should only ever happen if frames are not delivered
576173273Ssam		 * without the sender otherwise notifying us (e.g. with a
577173273Ssam		 * BAR to move the window).  Typically this happens because
578173273Ssam		 * of vendor bugs that cause the sequence number to jump.
579173273Ssam		 * When this happens we get a gap in the reorder queue that
580173273Ssam		 * leaves frame sitting on the queue until they get pushed
581173273Ssam		 * out due to window moves.  When the vendor does not send
582173273Ssam		 * BAR this move only happens due to explicit packet sends
583170530Ssam		 *
584173273Ssam		 * NB: we only track the time of the oldest frame in the
585173273Ssam		 * reorder q; this means that if we flush we might push
586173273Ssam		 * frames that still "new"; if this happens then subsequent
587173273Ssam		 * frames will result in BA window moves which cost something
588173273Ssam		 * but is still better than a big throughput dip.
589170530Ssam		 */
590173273Ssam		if (rap->rxa_qframes != 0) {
591173273Ssam			/* XXX honor batimeout? */
592173273Ssam			if (ticks - rap->rxa_age > ieee80211_ampdu_age) {
593173273Ssam				/*
594173273Ssam				 * Too long since we received the first
595173273Ssam				 * frame; flush the reorder buffer.
596173273Ssam				 */
597173273Ssam				if (rap->rxa_qframes != 0) {
598178354Ssam					vap->iv_stats.is_ampdu_rx_age +=
599173273Ssam					    rap->rxa_qframes;
600173273Ssam					ampdu_rx_flush(ni, rap);
601173273Ssam				}
602173273Ssam				rap->rxa_start = IEEE80211_SEQ_INC(rxseq);
603173273Ssam				return PROCESS;
604173273Ssam			}
605173273Ssam		} else {
606173273Ssam			/*
607173273Ssam			 * First frame, start aging timer.
608173273Ssam			 */
609173273Ssam			rap->rxa_age = ticks;
610173273Ssam		}
611173273Ssam#endif /* IEEE80211_AMPDU_AGE */
612173273Ssam		/* save packet */
613173273Ssam		if (rap->rxa_m[off] == NULL) {
614173273Ssam			rap->rxa_m[off] = m;
615173273Ssam			rap->rxa_qframes++;
616173273Ssam			rap->rxa_qbytes += m->m_pkthdr.len;
617178354Ssam			vap->iv_stats.is_ampdu_rx_reorder++;
618173273Ssam		} else {
619178354Ssam			IEEE80211_DISCARD_MAC(vap,
620173273Ssam			    IEEE80211_MSG_INPUT | IEEE80211_MSG_11N,
621173273Ssam			    ni->ni_macaddr, "a-mpdu duplicate",
622173273Ssam			    "seqno %u tid %u BA win <%u:%u>",
623173273Ssam			    rxseq, tid, rap->rxa_start,
624173273Ssam			    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1));
625178354Ssam			vap->iv_stats.is_rx_dup++;
626173273Ssam			IEEE80211_NODE_STAT(ni, rx_dup);
627173273Ssam			m_freem(m);
628173273Ssam		}
629173273Ssam		return CONSUMED;
630173273Ssam	}
631173273Ssam	if (off < IEEE80211_SEQ_BA_RANGE) {
632173273Ssam		/*
633173273Ssam		 * Outside the BA window, but within range;
634173273Ssam		 * flush the reorder q and move the window.
635173273Ssam		 * Sec 9.10.7.6 b) (D2.04 p.118 line 60)
636173273Ssam		 */
637178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
638173273Ssam		    "move BA win <%u:%u> (%u frames) rxseq %u tid %u",
639170530Ssam		    rap->rxa_start,
640173273Ssam		    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
641170530Ssam		    rap->rxa_qframes, rxseq, tid);
642178354Ssam		vap->iv_stats.is_ampdu_rx_move++;
643170530Ssam
644173273Ssam		/*
645173273Ssam		 * The spec says to flush frames up to but not including:
646173273Ssam		 * 	WinStart_B = rxseq - rap->rxa_wnd + 1
647173273Ssam		 * Then insert the frame or notify the caller to process
648173273Ssam		 * it immediately.  We can safely do this by just starting
649173273Ssam		 * over again because we know the frame will now be within
650173273Ssam		 * the BA window.
651173273Ssam		 */
652173273Ssam		/* NB: rxa_wnd known to be >0 */
653173273Ssam		ampdu_rx_flush_upto(ni, rap,
654173273Ssam		    IEEE80211_SEQ_SUB(rxseq, rap->rxa_wnd-1));
655173273Ssam		goto again;
656170530Ssam	} else {
657170530Ssam		/*
658173273Ssam		 * Outside the BA window and out of range; toss.
659173273Ssam		 * Sec 9.10.7.6 c) (D2.04 p.119 line 16)
660170530Ssam		 */
661178354Ssam		IEEE80211_DISCARD_MAC(vap,
662173273Ssam		    IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
663178354Ssam		    "MPDU", "BA win <%u:%u> (%u frames) rxseq %u tid %u%s",
664173273Ssam		    rap->rxa_start,
665173273Ssam		    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
666173273Ssam		    rap->rxa_qframes, rxseq, tid,
667173273Ssam		    wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
668178354Ssam		vap->iv_stats.is_ampdu_rx_drop++;
669173273Ssam		IEEE80211_NODE_STAT(ni, rx_drop);
670170530Ssam		m_freem(m);
671173273Ssam		return CONSUMED;
672170530Ssam	}
673173273Ssam#undef CONSUMED
674173273Ssam#undef PROCESS
675170530Ssam#undef IEEE80211_FC0_QOSDATA
676170530Ssam}
677170530Ssam
678170530Ssam/*
679170530Ssam * Process a BAR ctl frame.  Dispatch all frames up to
680170530Ssam * the sequence number of the frame.  If this frame is
681173273Ssam * out of range it's discarded.
682170530Ssam */
683170530Ssamvoid
684170530Ssamieee80211_recv_bar(struct ieee80211_node *ni, struct mbuf *m0)
685170530Ssam{
686178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
687170530Ssam	struct ieee80211_frame_bar *wh;
688170530Ssam	struct ieee80211_rx_ampdu *rap;
689170530Ssam	ieee80211_seq rxseq;
690170530Ssam	int tid, off;
691170530Ssam
692173273Ssam	if (!ieee80211_recv_bar_ena) {
693173273Ssam#if 0
694178354Ssam		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_11N,
695173273Ssam		    ni->ni_macaddr, "BAR", "%s", "processing disabled");
696173273Ssam#endif
697178354Ssam		vap->iv_stats.is_ampdu_bar_bad++;
698173273Ssam		return;
699173273Ssam	}
700170530Ssam	wh = mtod(m0, struct ieee80211_frame_bar *);
701170530Ssam	/* XXX check basic BAR */
702170530Ssam	tid = MS(le16toh(wh->i_ctl), IEEE80211_BAR_TID);
703170530Ssam	rap = &ni->ni_rx_ampdu[tid];
704170530Ssam	if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
705170530Ssam		/*
706170530Ssam		 * No ADDBA request yet, don't touch.
707170530Ssam		 */
708178354Ssam		IEEE80211_DISCARD_MAC(vap,
709170530Ssam		    IEEE80211_MSG_INPUT | IEEE80211_MSG_11N,
710170530Ssam		    ni->ni_macaddr, "BAR", "no BA stream, tid %u", tid);
711178354Ssam		vap->iv_stats.is_ampdu_bar_bad++;
712170530Ssam		return;
713170530Ssam	}
714178354Ssam	vap->iv_stats.is_ampdu_bar_rx++;
715170530Ssam	rxseq = le16toh(wh->i_seq) >> IEEE80211_SEQ_SEQ_SHIFT;
716173273Ssam	if (rxseq == rap->rxa_start)
717173273Ssam		return;
718170530Ssam	/* calculate offset in BA window */
719170530Ssam	off = IEEE80211_SEQ_SUB(rxseq, rap->rxa_start);
720173273Ssam	if (off < IEEE80211_SEQ_BA_RANGE) {
721170530Ssam		/*
722173273Ssam		 * Flush the reorder q up to rxseq and move the window.
723173273Ssam		 * Sec 9.10.7.6 a) (D2.04 p.119 line 22)
724170530Ssam		 */
725178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
726173273Ssam		    "BAR moves BA win <%u:%u> (%u frames) rxseq %u tid %u",
727170530Ssam		    rap->rxa_start,
728173273Ssam		    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
729173273Ssam		    rap->rxa_qframes, rxseq, tid);
730178354Ssam		vap->iv_stats.is_ampdu_bar_move++;
731173273Ssam
732173273Ssam		ampdu_rx_flush_upto(ni, rap, rxseq);
733173273Ssam		if (off >= rap->rxa_wnd) {
734173273Ssam			/*
735173273Ssam			 * BAR specifies a window start to the right of BA
736173273Ssam			 * window; we must move it explicitly since
737173273Ssam			 * ampdu_rx_flush_upto will not.
738173273Ssam			 */
739173273Ssam			rap->rxa_start = rxseq;
740170530Ssam		}
741173273Ssam	} else {
742170530Ssam		/*
743173273Ssam		 * Out of range; toss.
744173273Ssam		 * Sec 9.10.7.6 b) (D2.04 p.119 line 41)
745170530Ssam		 */
746178354Ssam		IEEE80211_DISCARD_MAC(vap,
747173273Ssam		    IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
748173273Ssam		    "BAR", "BA win <%u:%u> (%u frames) rxseq %u tid %u%s",
749173273Ssam		    rap->rxa_start,
750173273Ssam		    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
751173273Ssam		    rap->rxa_qframes, rxseq, tid,
752173273Ssam		    wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
753178354Ssam		vap->iv_stats.is_ampdu_bar_oow++;
754173273Ssam		IEEE80211_NODE_STAT(ni, rx_drop);
755170530Ssam	}
756170530Ssam}
757170530Ssam
758170530Ssam/*
759170530Ssam * Setup HT-specific state in a node.  Called only
760170530Ssam * when HT use is negotiated so we don't do extra
761170530Ssam * work for temporary and/or legacy sta's.
762170530Ssam */
763170530Ssamvoid
764170530Ssamieee80211_ht_node_init(struct ieee80211_node *ni, const uint8_t *htcap)
765170530Ssam{
766170530Ssam	struct ieee80211_tx_ampdu *tap;
767170530Ssam	int ac;
768170530Ssam
769173273Ssam	if (ni->ni_flags & IEEE80211_NODE_HT) {
770173273Ssam		/*
771173273Ssam		 * Clean AMPDU state on re-associate.  This handles the case
772173273Ssam		 * where a station leaves w/o notifying us and then returns
773173273Ssam		 * before node is reaped for inactivity.
774173273Ssam		 */
775173273Ssam		ieee80211_ht_node_cleanup(ni);
776173273Ssam	}
777170530Ssam	ieee80211_parse_htcap(ni, htcap);
778170530Ssam	for (ac = 0; ac < WME_NUM_AC; ac++) {
779170530Ssam		tap = &ni->ni_tx_ampdu[ac];
780170530Ssam		tap->txa_ac = ac;
781173273Ssam		/* NB: further initialization deferred */
782170530Ssam	}
783173273Ssam	ni->ni_flags |= IEEE80211_NODE_HT | IEEE80211_NODE_AMPDU;
784170530Ssam}
785170530Ssam
786170530Ssam/*
787170530Ssam * Cleanup HT-specific state in a node.  Called only
788170530Ssam * when HT use has been marked.
789170530Ssam */
790170530Ssamvoid
791170530Ssamieee80211_ht_node_cleanup(struct ieee80211_node *ni)
792170530Ssam{
793170530Ssam	struct ieee80211com *ic = ni->ni_ic;
794170530Ssam	int i;
795170530Ssam
796170530Ssam	KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT node"));
797170530Ssam
798170530Ssam	/* XXX optimize this */
799170530Ssam	for (i = 0; i < WME_NUM_AC; i++) {
800170530Ssam		struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[i];
801173273Ssam		if (tap->txa_flags & IEEE80211_AGGR_SETUP) {
802173273Ssam			/*
803173273Ssam			 * Stop BA stream if setup so driver has a chance
804173273Ssam			 * to reclaim any resources it might have allocated.
805173273Ssam			 */
806170530Ssam			ic->ic_addba_stop(ni, &ni->ni_tx_ampdu[i]);
807173273Ssam			IEEE80211_TAPQ_DESTROY(tap);
808178354Ssam			tap->txa_lastsample = 0;
809178354Ssam			tap->txa_avgpps = 0;
810173273Ssam			/* NB: clearing NAK means we may re-send ADDBA */
811173273Ssam			tap->txa_flags &=
812173273Ssam			    ~(IEEE80211_AGGR_SETUP | IEEE80211_AGGR_NAK);
813173273Ssam		}
814170530Ssam	}
815170530Ssam	for (i = 0; i < WME_NUM_TID; i++)
816170530Ssam		ampdu_rx_stop(&ni->ni_rx_ampdu[i]);
817170530Ssam
818170530Ssam	ni->ni_htcap = 0;
819173273Ssam	ni->ni_flags &= ~(IEEE80211_NODE_HT | IEEE80211_NODE_HTCOMPAT |
820173273Ssam		IEEE80211_NODE_AMPDU);
821170530Ssam}
822170530Ssam
823178354Ssam/*
824178354Ssam * Age out HT resources for a station.
825178354Ssam */
826178354Ssamvoid
827178354Ssamieee80211_ht_node_age(struct ieee80211_node *ni)
828178354Ssam{
829178354Ssam#ifdef IEEE80211_AMPDU_AGE
830178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
831178354Ssam	uint8_t tid;
832178354Ssam#endif
833178354Ssam
834178354Ssam	KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT sta"));
835178354Ssam
836178354Ssam#ifdef IEEE80211_AMPDU_AGE
837178354Ssam	for (tid = 0; tid < WME_NUM_TID; tid++) {
838178354Ssam		struct ieee80211_rx_ampdu *rap;
839178354Ssam
840178354Ssam		rap = &ni->ni_rx_ampdu[tid];
841178354Ssam		if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0)
842178354Ssam			continue;
843178354Ssam		if (rap->rxa_qframes == 0)
844178354Ssam			continue;
845178354Ssam		/*
846178354Ssam		 * Check for frames sitting too long in the reorder queue.
847178354Ssam		 * See above for more details on what's happening here.
848178354Ssam		 */
849178354Ssam		/* XXX honor batimeout? */
850178354Ssam		if (ticks - rap->rxa_age > ieee80211_ampdu_age) {
851178354Ssam			/*
852178354Ssam			 * Too long since we received the first
853178354Ssam			 * frame; flush the reorder buffer.
854178354Ssam			 */
855178354Ssam			vap->iv_stats.is_ampdu_rx_age += rap->rxa_qframes;
856178354Ssam			ampdu_rx_flush(ni, rap);
857178354Ssam		}
858178354Ssam	}
859178354Ssam#endif /* IEEE80211_AMPDU_AGE */
860178354Ssam}
861178354Ssam
862173273Ssamstatic struct ieee80211_channel *
863173273Ssamfindhtchan(struct ieee80211com *ic, struct ieee80211_channel *c, int htflags)
864173273Ssam{
865173273Ssam	return ieee80211_find_channel(ic, c->ic_freq,
866173273Ssam	    (c->ic_flags &~ IEEE80211_CHAN_HT) | htflags);
867173273Ssam}
868173273Ssam
869173273Ssam/*
870173273Ssam * Adjust a channel to be HT/non-HT according to the vap's configuration.
871173273Ssam */
872173273Ssamstruct ieee80211_channel *
873173273Ssamieee80211_ht_adjust_channel(struct ieee80211com *ic,
874173273Ssam	struct ieee80211_channel *chan, int flags)
875173273Ssam{
876173273Ssam	struct ieee80211_channel *c;
877173273Ssam
878173273Ssam	if (flags & IEEE80211_FEXT_HT) {
879173273Ssam		/* promote to HT if possible */
880173273Ssam		if (flags & IEEE80211_FEXT_USEHT40) {
881173273Ssam			if (!IEEE80211_IS_CHAN_HT40(chan)) {
882173273Ssam				/* NB: arbitrarily pick ht40+ over ht40- */
883173273Ssam				c = findhtchan(ic, chan, IEEE80211_CHAN_HT40U);
884173273Ssam				if (c == NULL)
885173273Ssam					c = findhtchan(ic, chan,
886173273Ssam						IEEE80211_CHAN_HT40D);
887173273Ssam				if (c == NULL)
888173273Ssam					c = findhtchan(ic, chan,
889173273Ssam						IEEE80211_CHAN_HT20);
890173273Ssam				if (c != NULL)
891173273Ssam					chan = c;
892173273Ssam			}
893173273Ssam		} else if (!IEEE80211_IS_CHAN_HT20(chan)) {
894173273Ssam			c = findhtchan(ic, chan, IEEE80211_CHAN_HT20);
895173273Ssam			if (c != NULL)
896173273Ssam				chan = c;
897173273Ssam		}
898173273Ssam	} else if (IEEE80211_IS_CHAN_HT(chan)) {
899173273Ssam		/* demote to legacy, HT use is disabled */
900173273Ssam		c = ieee80211_find_channel(ic, chan->ic_freq,
901173273Ssam		    chan->ic_flags &~ IEEE80211_CHAN_HT);
902173273Ssam		if (c != NULL)
903173273Ssam			chan = c;
904173273Ssam	}
905173273Ssam	return chan;
906173273Ssam}
907173273Ssam
908173273Ssam/*
909173273Ssam * Setup HT-specific state for a legacy WDS peer.
910173273Ssam */
911173273Ssamvoid
912173273Ssamieee80211_ht_wds_init(struct ieee80211_node *ni)
913173273Ssam{
914178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
915173273Ssam	struct ieee80211_tx_ampdu *tap;
916173273Ssam	int ac;
917173273Ssam
918178354Ssam	KASSERT(vap->iv_flags_ext & IEEE80211_FEXT_HT, ("no HT requested"));
919173273Ssam
920173273Ssam	/* XXX check scan cache in case peer has an ap and we have info */
921173273Ssam	/*
922173273Ssam	 * If setup with a legacy channel; locate an HT channel.
923173273Ssam	 * Otherwise if the inherited channel (from a companion
924173273Ssam	 * AP) is suitable use it so we use the same location
925173273Ssam	 * for the extension channel).
926173273Ssam	 */
927178354Ssam	ni->ni_chan = ieee80211_ht_adjust_channel(ni->ni_ic,
928178354Ssam	    ni->ni_chan, ieee80211_htchanflags(ni->ni_chan));
929173273Ssam
930173273Ssam	ni->ni_htcap = 0;
931178354Ssam	if (vap->iv_flags_ext & IEEE80211_FEXT_SHORTGI20)
932173273Ssam		ni->ni_htcap |= IEEE80211_HTCAP_SHORTGI20;
933173273Ssam	if (IEEE80211_IS_CHAN_HT40(ni->ni_chan)) {
934173273Ssam		ni->ni_htcap |= IEEE80211_HTCAP_CHWIDTH40;
935173273Ssam		ni->ni_chw = 40;
936173273Ssam		if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan))
937173273Ssam			ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_ABOVE;
938173273Ssam		else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan))
939173273Ssam			ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_BELOW;
940178354Ssam		if (vap->iv_flags_ext & IEEE80211_FEXT_SHORTGI40)
941173273Ssam			ni->ni_htcap |= IEEE80211_HTCAP_SHORTGI40;
942173273Ssam	} else {
943173273Ssam		ni->ni_chw = 20;
944173273Ssam		ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_NONE;
945173273Ssam	}
946173273Ssam	ni->ni_htctlchan = ni->ni_chan->ic_ieee;
947173273Ssam
948173273Ssam	ni->ni_htopmode = 0;		/* XXX need protection state */
949173273Ssam	ni->ni_htstbc = 0;		/* XXX need info */
950173273Ssam
951173273Ssam	for (ac = 0; ac < WME_NUM_AC; ac++) {
952173273Ssam		tap = &ni->ni_tx_ampdu[ac];
953173273Ssam		tap->txa_ac = ac;
954173273Ssam	}
955173273Ssam	/* NB: AMPDU tx/rx governed by IEEE80211_FEXT_AMPDU_{TX,RX} */
956173273Ssam	ni->ni_flags |= IEEE80211_NODE_HT | IEEE80211_NODE_AMPDU;
957173273Ssam}
958173273Ssam
959173273Ssam/*
960173273Ssam * Notify hostap vaps of a change in the HTINFO ie.
961173273Ssam */
962173273Ssamstatic void
963173273Ssamhtinfo_notify(struct ieee80211com *ic)
964173273Ssam{
965178354Ssam	struct ieee80211vap *vap;
966178354Ssam	int first = 1;
967178354Ssam
968178354Ssam	IEEE80211_LOCK_ASSERT(ic);
969178354Ssam
970178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
971178354Ssam		if (vap->iv_opmode != IEEE80211_M_HOSTAP)
972178354Ssam			continue;
973178354Ssam		if (first) {
974178354Ssam			IEEE80211_NOTE(vap,
975178354Ssam			    IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N,
976178354Ssam			    vap->iv_bss,
977178354Ssam			    "HT bss occupancy change: %d sta, %d ht, "
978178354Ssam			    "%d ht40%s, HT protmode now 0x%x"
979178354Ssam			    , ic->ic_sta_assoc
980178354Ssam			    , ic->ic_ht_sta_assoc
981178354Ssam			    , ic->ic_ht40_sta_assoc
982178354Ssam			    , (ic->ic_flags_ext & IEEE80211_FEXT_NONHT_PR) ?
983178354Ssam				 ", non-HT sta present" : ""
984178354Ssam			    , ic->ic_curhtprotmode);
985178354Ssam			first = 0;
986178354Ssam		}
987178354Ssam		ieee80211_beacon_notify(vap, IEEE80211_BEACON_HTINFO);
988178354Ssam	}
989173273Ssam}
990173273Ssam
991173273Ssam/*
992173273Ssam * Calculate HT protection mode from current
993173273Ssam * state and handle updates.
994173273Ssam */
995173273Ssamstatic void
996173273Ssamhtinfo_update(struct ieee80211com *ic)
997173273Ssam{
998173273Ssam	uint8_t protmode;
999173273Ssam
1000178354Ssam	if (ic->ic_sta_assoc != ic->ic_ht_sta_assoc) {
1001178354Ssam		protmode = IEEE80211_HTINFO_OPMODE_MIXED
1002178354Ssam			 | IEEE80211_HTINFO_NONHT_PRESENT;
1003178354Ssam	} else if (ic->ic_flags_ext & IEEE80211_FEXT_NONHT_PR) {
1004173273Ssam		protmode = IEEE80211_HTINFO_OPMODE_PROTOPT
1005178354Ssam			 | IEEE80211_HTINFO_NONHT_PRESENT;
1006178354Ssam	} else if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
1007178354Ssam	    IEEE80211_IS_CHAN_HT40(ic->ic_bsschan) &&
1008173273Ssam	    ic->ic_sta_assoc != ic->ic_ht40_sta_assoc) {
1009173273Ssam		protmode = IEEE80211_HTINFO_OPMODE_HT20PR;
1010173273Ssam	} else {
1011173273Ssam		protmode = IEEE80211_HTINFO_OPMODE_PURE;
1012173273Ssam	}
1013173273Ssam	if (protmode != ic->ic_curhtprotmode) {
1014173273Ssam		ic->ic_curhtprotmode = protmode;
1015173273Ssam		htinfo_notify(ic);
1016173273Ssam	}
1017173273Ssam}
1018173273Ssam
1019173273Ssam/*
1020173273Ssam * Handle an HT station joining a BSS.
1021173273Ssam */
1022173273Ssamvoid
1023173273Ssamieee80211_ht_node_join(struct ieee80211_node *ni)
1024173273Ssam{
1025173273Ssam	struct ieee80211com *ic = ni->ni_ic;
1026173273Ssam
1027173273Ssam	IEEE80211_LOCK_ASSERT(ic);
1028173273Ssam
1029173273Ssam	if (ni->ni_flags & IEEE80211_NODE_HT) {
1030173273Ssam		ic->ic_ht_sta_assoc++;
1031173273Ssam		if (ni->ni_chw == 40)
1032173273Ssam			ic->ic_ht40_sta_assoc++;
1033173273Ssam	}
1034173273Ssam	htinfo_update(ic);
1035173273Ssam}
1036173273Ssam
1037173273Ssam/*
1038173273Ssam * Handle an HT station leaving a BSS.
1039173273Ssam */
1040173273Ssamvoid
1041173273Ssamieee80211_ht_node_leave(struct ieee80211_node *ni)
1042173273Ssam{
1043173273Ssam	struct ieee80211com *ic = ni->ni_ic;
1044173273Ssam
1045173273Ssam	IEEE80211_LOCK_ASSERT(ic);
1046173273Ssam
1047173273Ssam	if (ni->ni_flags & IEEE80211_NODE_HT) {
1048173273Ssam		ic->ic_ht_sta_assoc--;
1049173273Ssam		if (ni->ni_chw == 40)
1050173273Ssam			ic->ic_ht40_sta_assoc--;
1051173273Ssam	}
1052173273Ssam	htinfo_update(ic);
1053173273Ssam}
1054173273Ssam
1055173273Ssam/*
1056173273Ssam * Public version of htinfo_update; used for processing
1057178354Ssam * beacon frames from overlapping bss.
1058178354Ssam *
1059178354Ssam * Caller can specify either IEEE80211_HTINFO_OPMODE_MIXED
1060178354Ssam * (on receipt of a beacon that advertises MIXED) or
1061178354Ssam * IEEE80211_HTINFO_OPMODE_PROTOPT (on receipt of a beacon
1062178354Ssam * from an overlapping legacy bss).  We treat MIXED with
1063178354Ssam * a higher precedence than PROTOPT (i.e. we will not change
1064178354Ssam * change PROTOPT -> MIXED; only MIXED -> PROTOPT).  This
1065178354Ssam * corresponds to how we handle things in htinfo_update.
1066173273Ssam */
1067173273Ssamvoid
1068178354Ssamieee80211_htprot_update(struct ieee80211com *ic, int protmode)
1069173273Ssam{
1070178354Ssam#define	OPMODE(x)	SM(x, IEEE80211_HTINFO_OPMODE)
1071178354Ssam	if (protmode == ic->ic_curhtprotmode)
1072178354Ssam		return;
1073178354Ssam	if (OPMODE(ic->ic_curhtprotmode) == IEEE80211_HTINFO_OPMODE_MIXED &&
1074178354Ssam	    OPMODE(protmode) == IEEE80211_HTINFO_OPMODE_PROTOPT)
1075178354Ssam		return;
1076178354Ssam
1077178354Ssam	/* track non-HT station presence */
1078178354Ssam	KASSERT(protmode & IEEE80211_HTINFO_NONHT_PRESENT,
1079178354Ssam	    ("missing NONHT_PRESENT"));
1080178354Ssam	ic->ic_flags_ext |= IEEE80211_FEXT_NONHT_PR;
1081178354Ssam	ic->ic_lastnonht = ticks;
1082178354Ssam
1083178354Ssam	/* push beacon update */
1084178354Ssam	ic->ic_curhtprotmode = protmode;
1085178354Ssam	htinfo_notify(ic);
1086178354Ssam#undef OPMODE
1087173273Ssam}
1088173273Ssam
1089173273Ssam/*
1090173273Ssam * Time out presence of an overlapping bss with non-HT
1091173273Ssam * stations.  When operating in hostap mode we listen for
1092173273Ssam * beacons from other stations and if we identify a non-HT
1093173273Ssam * station is present we update the opmode field of the
1094173273Ssam * HTINFO ie.  To identify when all non-HT stations are
1095173273Ssam * gone we time out this condition.
1096173273Ssam */
1097173273Ssamvoid
1098173273Ssamieee80211_ht_timeout(struct ieee80211com *ic)
1099173273Ssam{
1100173273Ssam	IEEE80211_LOCK_ASSERT(ic);
1101173273Ssam
1102173273Ssam	if ((ic->ic_flags_ext & IEEE80211_FEXT_NONHT_PR) &&
1103173273Ssam	    time_after(ticks, ic->ic_lastnonht + IEEE80211_NONHT_PRESENT_AGE)) {
1104173273Ssam#if 0
1105178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
1106173273Ssam		    "%s", "time out non-HT STA present on channel");
1107173273Ssam#endif
1108173273Ssam		ic->ic_flags_ext &= ~IEEE80211_FEXT_NONHT_PR;
1109173273Ssam		htinfo_update(ic);
1110173273Ssam	}
1111173273Ssam}
1112173273Ssam
1113170530Ssam/* unalligned little endian access */
1114170530Ssam#define LE_READ_2(p)					\
1115170530Ssam	((uint16_t)					\
1116170530Ssam	 ((((const uint8_t *)(p))[0]      ) |		\
1117170530Ssam	  (((const uint8_t *)(p))[1] <<  8)))
1118170530Ssam
1119170530Ssam/*
1120170530Ssam * Process an 802.11n HT capabilities ie.
1121170530Ssam */
1122170530Ssamvoid
1123170530Ssamieee80211_parse_htcap(struct ieee80211_node *ni, const uint8_t *ie)
1124170530Ssam{
1125178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1126170530Ssam
1127170530Ssam	if (ie[0] == IEEE80211_ELEMID_VENDOR) {
1128170530Ssam		/*
1129170530Ssam		 * Station used Vendor OUI ie to associate;
1130170530Ssam		 * mark the node so when we respond we'll use
1131170530Ssam		 * the Vendor OUI's and not the standard ie's.
1132170530Ssam		 */
1133170530Ssam		ni->ni_flags |= IEEE80211_NODE_HTCOMPAT;
1134170530Ssam		ie += 4;
1135170530Ssam	} else
1136170530Ssam		ni->ni_flags &= ~IEEE80211_NODE_HTCOMPAT;
1137170530Ssam
1138170530Ssam	ni->ni_htcap = LE_READ_2(ie +
1139170530Ssam		__offsetof(struct ieee80211_ie_htcap, hc_cap));
1140170530Ssam	ni->ni_htparam = ie[__offsetof(struct ieee80211_ie_htcap, hc_param)];
1141173273Ssam	/* XXX needed or will ieee80211_parse_htinfo always be called? */
1142173273Ssam	ni->ni_chw = (ni->ni_htcap & IEEE80211_HTCAP_CHWIDTH40) &&
1143178354Ssam		     (vap->iv_flags_ext & IEEE80211_FEXT_USEHT40) ? 40 : 20;
1144170530Ssam}
1145170530Ssam
1146170530Ssam/*
1147173273Ssam * Process an 802.11n HT info ie and update the node state.
1148173273Ssam * Note that we handle use this information to identify the
1149173273Ssam * correct channel (HT20, HT40+, HT40-, legacy).  The caller
1150173273Ssam * is responsible for insuring any required channel change is
1151173273Ssam * done (e.g. in sta mode when parsing the contents of a
1152173273Ssam * beacon frame).
1153170530Ssam */
1154170530Ssamvoid
1155170530Ssamieee80211_parse_htinfo(struct ieee80211_node *ni, const uint8_t *ie)
1156170530Ssam{
1157173273Ssam	struct ieee80211com *ic = ni->ni_ic;
1158178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1159170530Ssam 	const struct ieee80211_ie_htinfo *htinfo;
1160173273Ssam	struct ieee80211_channel *c;
1161170530Ssam	uint16_t w;
1162173273Ssam	int htflags, chanflags;
1163170530Ssam
1164170530Ssam	if (ie[0] == IEEE80211_ELEMID_VENDOR)
1165170530Ssam		ie += 4;
1166170530Ssam 	htinfo = (const struct ieee80211_ie_htinfo *) ie;
1167170530Ssam	ni->ni_htctlchan = htinfo->hi_ctrlchannel;
1168170530Ssam	ni->ni_ht2ndchan = SM(htinfo->hi_byte1, IEEE80211_HTINFO_2NDCHAN);
1169172055Ssam	w = LE_READ_2(&htinfo->hi_byte2);
1170170530Ssam	ni->ni_htopmode = SM(w, IEEE80211_HTINFO_OPMODE);
1171170530Ssam	w = LE_READ_2(&htinfo->hi_byte45);
1172170530Ssam	ni->ni_htstbc = SM(w, IEEE80211_HTINFO_BASIC_STBCMCS);
1173173273Ssam	/*
1174173273Ssam	 * Handle 11n channel switch.  Use the received HT ie's to
1175173273Ssam	 * identify the right channel to use.  If we cannot locate it
1176173273Ssam	 * in the channel table then fallback to legacy operation.
1177173273Ssam	 */
1178178354Ssam	/* NB: honor operating mode constraint */
1179178354Ssam	htflags = (vap->iv_flags_ext & IEEE80211_FEXT_HT) ?
1180173273Ssam	    IEEE80211_CHAN_HT20 : 0;
1181173273Ssam	if ((htinfo->hi_byte1 & IEEE80211_HTINFO_TXWIDTH_2040) &&
1182178354Ssam	    (vap->iv_flags_ext & IEEE80211_FEXT_USEHT40)) {
1183173273Ssam		if (ni->ni_ht2ndchan == IEEE80211_HTINFO_2NDCHAN_ABOVE)
1184173273Ssam			htflags = IEEE80211_CHAN_HT40U;
1185173273Ssam		else if (ni->ni_ht2ndchan == IEEE80211_HTINFO_2NDCHAN_BELOW)
1186173273Ssam			htflags = IEEE80211_CHAN_HT40D;
1187170530Ssam	}
1188173273Ssam	chanflags = (ni->ni_chan->ic_flags &~ IEEE80211_CHAN_HT) | htflags;
1189173273Ssam	if (chanflags != ni->ni_chan->ic_flags) {
1190173273Ssam		c = ieee80211_find_channel(ic, ni->ni_chan->ic_freq, chanflags);
1191178354Ssam		if (c == NULL && (htflags & IEEE80211_CHAN_HT40)) {
1192173273Ssam			/*
1193173273Ssam			 * No HT40 channel entry in our table; fall back
1194173273Ssam			 * to HT20 operation.  This should not happen.
1195173273Ssam			 */
1196173273Ssam			c = findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT20);
1197178354Ssam			IEEE80211_NOTE(vap,
1198173273Ssam			    IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
1199173273Ssam			    "no HT40 channel (freq %u), falling back to HT20",
1200173273Ssam			    ni->ni_chan->ic_freq);
1201173273Ssam			/* XXX stat */
1202173273Ssam		}
1203173273Ssam		if (c != NULL && c != ni->ni_chan) {
1204178354Ssam			IEEE80211_NOTE(vap,
1205173273Ssam			    IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
1206173273Ssam			    "switch station to HT%d channel %u/0x%x",
1207173273Ssam			    IEEE80211_IS_CHAN_HT40(c) ? 40 : 20,
1208173273Ssam			    c->ic_freq, c->ic_flags);
1209173273Ssam			ni->ni_chan = c;
1210173273Ssam		}
1211173273Ssam		/* NB: caller responsible for forcing any channel change */
1212173273Ssam	}
1213173273Ssam	/* update node's tx channel width */
1214173273Ssam	ni->ni_chw = IEEE80211_IS_CHAN_HT40(ni->ni_chan)? 40 : 20;
1215170530Ssam}
1216170530Ssam
1217170530Ssam/*
1218170530Ssam * Install received HT rate set by parsing the HT cap ie.
1219170530Ssam */
1220170530Ssamint
1221170530Ssamieee80211_setup_htrates(struct ieee80211_node *ni, const uint8_t *ie, int flags)
1222170530Ssam{
1223178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1224170530Ssam	const struct ieee80211_ie_htcap *htcap;
1225170530Ssam	struct ieee80211_htrateset *rs;
1226170530Ssam	int i;
1227170530Ssam
1228170530Ssam	rs = &ni->ni_htrates;
1229170530Ssam	memset(rs, 0, sizeof(*rs));
1230170530Ssam	if (ie != NULL) {
1231170530Ssam		if (ie[0] == IEEE80211_ELEMID_VENDOR)
1232170530Ssam			ie += 4;
1233170530Ssam		htcap = (const struct ieee80211_ie_htcap *) ie;
1234170530Ssam		for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++) {
1235170530Ssam			if (isclr(htcap->hc_mcsset, i))
1236170530Ssam				continue;
1237170530Ssam			if (rs->rs_nrates == IEEE80211_HTRATE_MAXSIZE) {
1238178354Ssam				IEEE80211_NOTE(vap,
1239170530Ssam				    IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
1240170530Ssam				    "WARNING, HT rate set too large; only "
1241170530Ssam				    "using %u rates", IEEE80211_HTRATE_MAXSIZE);
1242178354Ssam				vap->iv_stats.is_rx_rstoobig++;
1243170530Ssam				break;
1244170530Ssam			}
1245170530Ssam			rs->rs_rates[rs->rs_nrates++] = i;
1246170530Ssam		}
1247170530Ssam	}
1248170530Ssam	return ieee80211_fix_rate(ni, (struct ieee80211_rateset *) rs, flags);
1249170530Ssam}
1250170530Ssam
1251170530Ssam/*
1252170530Ssam * Mark rates in a node's HT rate set as basic according
1253170530Ssam * to the information in the supplied HT info ie.
1254170530Ssam */
1255170530Ssamvoid
1256170530Ssamieee80211_setup_basic_htrates(struct ieee80211_node *ni, const uint8_t *ie)
1257170530Ssam{
1258170530Ssam	const struct ieee80211_ie_htinfo *htinfo;
1259170530Ssam	struct ieee80211_htrateset *rs;
1260170530Ssam	int i, j;
1261170530Ssam
1262170530Ssam	if (ie[0] == IEEE80211_ELEMID_VENDOR)
1263170530Ssam		ie += 4;
1264170530Ssam	htinfo = (const struct ieee80211_ie_htinfo *) ie;
1265170530Ssam	rs = &ni->ni_htrates;
1266170530Ssam	if (rs->rs_nrates == 0) {
1267178354Ssam		IEEE80211_NOTE(ni->ni_vap,
1268170530Ssam		    IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
1269170530Ssam		    "%s", "WARNING, empty HT rate set");
1270170530Ssam		return;
1271170530Ssam	}
1272170530Ssam	for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++) {
1273170530Ssam		if (isclr(htinfo->hi_basicmcsset, i))
1274170530Ssam			continue;
1275170530Ssam		for (j = 0; j < rs->rs_nrates; j++)
1276170530Ssam			if ((rs->rs_rates[j] & IEEE80211_RATE_VAL) == i)
1277170530Ssam				rs->rs_rates[j] |= IEEE80211_RATE_BASIC;
1278170530Ssam	}
1279170530Ssam}
1280170530Ssam
1281170530Ssamstatic void
1282170530Ssamaddba_timeout(void *arg)
1283170530Ssam{
1284170530Ssam	struct ieee80211_tx_ampdu *tap = arg;
1285170530Ssam
1286170530Ssam	/* XXX ? */
1287170530Ssam	tap->txa_flags &= ~IEEE80211_AGGR_XCHGPEND;
1288170530Ssam	tap->txa_attempts++;
1289170530Ssam}
1290170530Ssam
1291170530Ssamstatic void
1292170530Ssamaddba_start_timeout(struct ieee80211_tx_ampdu *tap)
1293170530Ssam{
1294170530Ssam	/* XXX use CALLOUT_PENDING instead? */
1295178354Ssam	callout_reset(&tap->txa_timer, ieee80211_addba_timeout,
1296170530Ssam	    addba_timeout, tap);
1297170530Ssam	tap->txa_flags |= IEEE80211_AGGR_XCHGPEND;
1298178354Ssam	tap->txa_nextrequest = ticks + ieee80211_addba_timeout;
1299170530Ssam}
1300170530Ssam
1301170530Ssamstatic void
1302170530Ssamaddba_stop_timeout(struct ieee80211_tx_ampdu *tap)
1303170530Ssam{
1304170530Ssam	/* XXX use CALLOUT_PENDING instead? */
1305170530Ssam	if (tap->txa_flags & IEEE80211_AGGR_XCHGPEND) {
1306170530Ssam		callout_stop(&tap->txa_timer);
1307170530Ssam		tap->txa_flags &= ~IEEE80211_AGGR_XCHGPEND;
1308170530Ssam	}
1309170530Ssam}
1310170530Ssam
1311170530Ssam/*
1312170530Ssam * Default method for requesting A-MPDU tx aggregation.
1313170530Ssam * We setup the specified state block and start a timer
1314170530Ssam * to wait for an ADDBA response frame.
1315170530Ssam */
1316170530Ssamstatic int
1317170530Ssamieee80211_addba_request(struct ieee80211_node *ni,
1318170530Ssam	struct ieee80211_tx_ampdu *tap,
1319170530Ssam	int dialogtoken, int baparamset, int batimeout)
1320170530Ssam{
1321170530Ssam	int bufsiz;
1322170530Ssam
1323170530Ssam	/* XXX locking */
1324170530Ssam	tap->txa_token = dialogtoken;
1325170530Ssam	tap->txa_flags |= IEEE80211_AGGR_IMMEDIATE;
1326170530Ssam	tap->txa_start = tap->txa_seqstart = 0;
1327170530Ssam	bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
1328170530Ssam	tap->txa_wnd = (bufsiz == 0) ?
1329170530Ssam	    IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
1330170530Ssam	addba_start_timeout(tap);
1331170530Ssam	return 1;
1332170530Ssam}
1333170530Ssam
1334170530Ssam/*
1335170530Ssam * Default method for processing an A-MPDU tx aggregation
1336170530Ssam * response.  We shutdown any pending timer and update the
1337170530Ssam * state block according to the reply.
1338170530Ssam */
1339170530Ssamstatic int
1340170530Ssamieee80211_addba_response(struct ieee80211_node *ni,
1341170530Ssam	struct ieee80211_tx_ampdu *tap,
1342170530Ssam	int status, int baparamset, int batimeout)
1343170530Ssam{
1344170530Ssam	int bufsiz;
1345170530Ssam
1346170530Ssam	/* XXX locking */
1347170530Ssam	addba_stop_timeout(tap);
1348170530Ssam	if (status == IEEE80211_STATUS_SUCCESS) {
1349170530Ssam		bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
1350170530Ssam		/* XXX override our request? */
1351170530Ssam		tap->txa_wnd = (bufsiz == 0) ?
1352170530Ssam		    IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
1353170530Ssam		tap->txa_flags |= IEEE80211_AGGR_RUNNING;
1354173273Ssam	} else {
1355173273Ssam		/* mark tid so we don't try again */
1356173273Ssam		tap->txa_flags |= IEEE80211_AGGR_NAK;
1357170530Ssam	}
1358170530Ssam	return 1;
1359170530Ssam}
1360170530Ssam
1361170530Ssam/*
1362170530Ssam * Default method for stopping A-MPDU tx aggregation.
1363170530Ssam * Any timer is cleared and we drain any pending frames.
1364170530Ssam */
1365170530Ssamstatic void
1366170530Ssamieee80211_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
1367170530Ssam{
1368170530Ssam	/* XXX locking */
1369170530Ssam	addba_stop_timeout(tap);
1370170530Ssam	if (tap->txa_flags & IEEE80211_AGGR_RUNNING) {
1371170530Ssam		/* clear aggregation queue */
1372170530Ssam		ieee80211_drain_ifq(&tap->txa_q);
1373170530Ssam		tap->txa_flags &= ~IEEE80211_AGGR_RUNNING;
1374170530Ssam	}
1375170530Ssam	tap->txa_attempts = 0;
1376170530Ssam}
1377170530Ssam
1378170530Ssam/*
1379170530Ssam * Process a received action frame using the default aggregation
1380170530Ssam * policy.  We intercept ADDBA-related frames and use them to
1381170530Ssam * update our aggregation state.  All other frames are passed up
1382170530Ssam * for processing by ieee80211_recv_action.
1383170530Ssam */
1384170530Ssamstatic void
1385170530Ssamieee80211_aggr_recv_action(struct ieee80211_node *ni,
1386170530Ssam	const uint8_t *frm, const uint8_t *efrm)
1387170530Ssam{
1388170530Ssam	struct ieee80211com *ic = ni->ni_ic;
1389178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1390170530Ssam	const struct ieee80211_action *ia;
1391170530Ssam	struct ieee80211_rx_ampdu *rap;
1392170530Ssam	struct ieee80211_tx_ampdu *tap;
1393170530Ssam	uint8_t dialogtoken;
1394170530Ssam	uint16_t baparamset, batimeout, baseqctl, code;
1395170530Ssam	uint16_t args[4];
1396170530Ssam	int tid, ac, bufsiz;
1397170530Ssam
1398170530Ssam	ia = (const struct ieee80211_action *) frm;
1399170530Ssam	switch (ia->ia_category) {
1400170530Ssam	case IEEE80211_ACTION_CAT_BA:
1401170530Ssam		switch (ia->ia_action) {
1402170530Ssam		case IEEE80211_ACTION_BA_ADDBA_REQUEST:
1403170530Ssam			dialogtoken = frm[2];
1404170530Ssam			baparamset = LE_READ_2(frm+3);
1405170530Ssam			batimeout = LE_READ_2(frm+5);
1406170530Ssam			baseqctl = LE_READ_2(frm+7);
1407170530Ssam
1408170530Ssam			tid = MS(baparamset, IEEE80211_BAPS_TID);
1409170530Ssam			bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
1410170530Ssam
1411178354Ssam			IEEE80211_NOTE(vap,
1412170530Ssam			    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1413170530Ssam			    "recv ADDBA request: dialogtoken %u "
1414170530Ssam			    "baparamset 0x%x (tid %d bufsiz %d) batimeout %d "
1415173273Ssam			    "baseqctl %d:%d",
1416173273Ssam			    dialogtoken, baparamset, tid, bufsiz, batimeout,
1417173273Ssam			    MS(baseqctl, IEEE80211_BASEQ_START),
1418173273Ssam			    MS(baseqctl, IEEE80211_BASEQ_FRAG));
1419170530Ssam
1420170530Ssam			rap = &ni->ni_rx_ampdu[tid];
1421170530Ssam
1422170530Ssam			/* Send ADDBA response */
1423170530Ssam			args[0] = dialogtoken;
1424173273Ssam			/*
1425173273Ssam			 * NB: We ack only if the sta associated with HT and
1426173273Ssam			 * the ap is configured to do AMPDU rx (the latter
1427173273Ssam			 * violates the 11n spec and is mostly for testing).
1428173273Ssam			 */
1429173273Ssam			if ((ni->ni_flags & IEEE80211_NODE_AMPDU_RX) &&
1430178354Ssam			    (vap->iv_flags_ext & IEEE80211_FEXT_AMPDU_RX)) {
1431170530Ssam				ampdu_rx_start(rap, bufsiz,
1432170530Ssam				    MS(baseqctl, IEEE80211_BASEQ_START));
1433170530Ssam
1434170530Ssam				args[1] = IEEE80211_STATUS_SUCCESS;
1435173273Ssam			} else {
1436178354Ssam				IEEE80211_NOTE(vap,
1437173273Ssam				    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1438173273Ssam				    ni, "reject ADDBA request: %s",
1439173273Ssam				    ni->ni_flags & IEEE80211_NODE_AMPDU_RX ?
1440173273Ssam				       "administratively disabled" :
1441173273Ssam				       "not negotiated for station");
1442178354Ssam				vap->iv_stats.is_addba_reject++;
1443170530Ssam				args[1] = IEEE80211_STATUS_UNSPECIFIED;
1444173273Ssam			}
1445170530Ssam			/* XXX honor rap flags? */
1446170530Ssam			args[2] = IEEE80211_BAPS_POLICY_IMMEDIATE
1447170530Ssam				| SM(tid, IEEE80211_BAPS_TID)
1448170530Ssam				| SM(rap->rxa_wnd, IEEE80211_BAPS_BUFSIZ)
1449170530Ssam				;
1450170530Ssam			args[3] = 0;
1451170530Ssam			ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
1452170530Ssam				IEEE80211_ACTION_BA_ADDBA_RESPONSE, args);
1453170530Ssam			return;
1454170530Ssam
1455170530Ssam		case IEEE80211_ACTION_BA_ADDBA_RESPONSE:
1456170530Ssam			dialogtoken = frm[2];
1457170530Ssam			code = LE_READ_2(frm+3);
1458170530Ssam			baparamset = LE_READ_2(frm+5);
1459170530Ssam			tid = MS(baparamset, IEEE80211_BAPS_TID);
1460170530Ssam			bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
1461170530Ssam			batimeout = LE_READ_2(frm+7);
1462170530Ssam
1463173273Ssam			ac = TID_TO_WME_AC(tid);
1464173273Ssam			tap = &ni->ni_tx_ampdu[ac];
1465175877Ssam			if ((tap->txa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
1466178354Ssam				IEEE80211_DISCARD_MAC(vap,
1467173273Ssam				    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1468173273Ssam				    ni->ni_macaddr, "ADDBA response",
1469173273Ssam				    "no pending ADDBA, tid %d dialogtoken %u "
1470173273Ssam				    "code %d", tid, dialogtoken, code);
1471178354Ssam				vap->iv_stats.is_addba_norequest++;
1472173273Ssam				return;
1473173273Ssam			}
1474173273Ssam			if (dialogtoken != tap->txa_token) {
1475178354Ssam				IEEE80211_DISCARD_MAC(vap,
1476173273Ssam				    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1477173273Ssam				    ni->ni_macaddr, "ADDBA response",
1478173273Ssam				    "dialogtoken mismatch: waiting for %d, "
1479173273Ssam				    "received %d, tid %d code %d",
1480173273Ssam				    tap->txa_token, dialogtoken, tid, code);
1481178354Ssam				vap->iv_stats.is_addba_badtoken++;
1482173273Ssam				return;
1483173273Ssam			}
1484173273Ssam
1485178354Ssam			IEEE80211_NOTE(vap,
1486170530Ssam			    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1487170530Ssam			    "recv ADDBA response: dialogtoken %u code %d "
1488170530Ssam			    "baparamset 0x%x (tid %d bufsiz %d) batimeout %d",
1489170530Ssam			    dialogtoken, code, baparamset, tid, bufsiz,
1490170530Ssam			    batimeout);
1491170530Ssam			ic->ic_addba_response(ni, tap,
1492170530Ssam				code, baparamset, batimeout);
1493170530Ssam			return;
1494170530Ssam
1495170530Ssam		case IEEE80211_ACTION_BA_DELBA:
1496170530Ssam			baparamset = LE_READ_2(frm+2);
1497170530Ssam			code = LE_READ_2(frm+4);
1498170530Ssam
1499170530Ssam			tid = MS(baparamset, IEEE80211_DELBAPS_TID);
1500170530Ssam
1501178354Ssam			IEEE80211_NOTE(vap,
1502170530Ssam			    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1503170530Ssam			    "recv DELBA: baparamset 0x%x (tid %d initiator %d) "
1504170530Ssam			    "code %d", baparamset, tid,
1505170530Ssam			    MS(baparamset, IEEE80211_DELBAPS_INIT), code);
1506170530Ssam
1507170530Ssam			if ((baparamset & IEEE80211_DELBAPS_INIT) == 0) {
1508170530Ssam				ac = TID_TO_WME_AC(tid);
1509170530Ssam				tap = &ni->ni_tx_ampdu[ac];
1510170530Ssam				ic->ic_addba_stop(ni, tap);
1511170530Ssam			} else {
1512170530Ssam				rap = &ni->ni_rx_ampdu[tid];
1513170530Ssam				ampdu_rx_stop(rap);
1514170530Ssam			}
1515170530Ssam			return;
1516170530Ssam		}
1517170530Ssam		break;
1518170530Ssam	}
1519173273Ssam	ieee80211_recv_action(ni, frm, efrm);
1520170530Ssam}
1521170530Ssam
1522170530Ssam/*
1523170530Ssam * Process a received 802.11n action frame.
1524170530Ssam * Aggregation-related frames are assumed to be handled
1525170530Ssam * already; we handle any other frames we can, otherwise
1526170530Ssam * complain about being unsupported (with debugging).
1527170530Ssam */
1528170530Ssamvoid
1529170530Ssamieee80211_recv_action(struct ieee80211_node *ni,
1530170530Ssam	const uint8_t *frm, const uint8_t *efrm)
1531170530Ssam{
1532178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1533170530Ssam	const struct ieee80211_action *ia;
1534170530Ssam	int chw;
1535170530Ssam
1536170530Ssam	ia = (const struct ieee80211_action *) frm;
1537170530Ssam	switch (ia->ia_category) {
1538170530Ssam	case IEEE80211_ACTION_CAT_BA:
1539178354Ssam		IEEE80211_NOTE(vap,
1540170530Ssam		    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1541170530Ssam		    "%s: BA action %d not implemented", __func__,
1542170530Ssam		    ia->ia_action);
1543178354Ssam		vap->iv_stats.is_rx_mgtdiscard++;
1544170530Ssam		break;
1545170530Ssam	case IEEE80211_ACTION_CAT_HT:
1546170530Ssam		switch (ia->ia_action) {
1547170530Ssam		case IEEE80211_ACTION_HT_TXCHWIDTH:
1548170530Ssam			chw = frm[2] == IEEE80211_A_HT_TXCHWIDTH_2040 ? 40 : 20;
1549170530Ssam			if (chw != ni->ni_chw) {
1550170530Ssam				ni->ni_chw = chw;
1551170530Ssam				ni->ni_flags |= IEEE80211_NODE_CHWUPDATE;
1552170530Ssam			}
1553178354Ssam			IEEE80211_NOTE(vap,
1554170530Ssam			    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1555173273Ssam		            "%s: HT txchwidth, width %d (%s)",
1556170530Ssam			    __func__, chw,
1557170530Ssam			    ni->ni_flags & IEEE80211_NODE_CHWUPDATE ?
1558170530Ssam				"new" : "no change");
1559170530Ssam			break;
1560173273Ssam		case IEEE80211_ACTION_HT_MIMOPWRSAVE:
1561178354Ssam			IEEE80211_NOTE(vap,
1562173273Ssam			    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1563173273Ssam		            "%s: HT MIMO PS", __func__);
1564173273Ssam			break;
1565170530Ssam		default:
1566178354Ssam			IEEE80211_NOTE(vap,
1567170530Ssam			   IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1568170530Ssam		           "%s: HT action %d not implemented", __func__,
1569170530Ssam			   ia->ia_action);
1570178354Ssam			vap->iv_stats.is_rx_mgtdiscard++;
1571170530Ssam			break;
1572170530Ssam		}
1573170530Ssam		break;
1574170530Ssam	default:
1575178354Ssam		IEEE80211_NOTE(vap,
1576170530Ssam		    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1577170530Ssam		    "%s: category %d not implemented", __func__,
1578170530Ssam		    ia->ia_category);
1579178354Ssam		vap->iv_stats.is_rx_mgtdiscard++;
1580170530Ssam		break;
1581170530Ssam	}
1582170530Ssam}
1583170530Ssam
1584170530Ssam/*
1585170530Ssam * Transmit processing.
1586170530Ssam */
1587170530Ssam
1588170530Ssam/*
1589178354Ssam * Check if A-MPDU should be requested/enabled for a stream.
1590178354Ssam * We require a traffic rate above a per-AC threshold and we
1591178354Ssam * also handle backoff from previous failed attempts.
1592178354Ssam *
1593178354Ssam * Drivers may override this method to bring in information
1594178354Ssam * such as link state conditions in making the decision.
1595178354Ssam */
1596178354Ssamstatic int
1597178354Ssamieee80211_ampdu_enable(struct ieee80211_node *ni,
1598178354Ssam	struct ieee80211_tx_ampdu *tap)
1599178354Ssam{
1600178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1601178354Ssam
1602178354Ssam	if (tap->txa_avgpps < vap->iv_ampdu_mintraffic[tap->txa_ac])
1603178354Ssam		return 0;
1604178354Ssam	/* XXX check rssi? */
1605178354Ssam	if (tap->txa_attempts >= ieee80211_addba_maxtries &&
1606178354Ssam	    ticks < tap->txa_nextrequest) {
1607178354Ssam		/*
1608178354Ssam		 * Don't retry too often; txa_nextrequest is set
1609178354Ssam		 * to the minimum interval we'll retry after
1610178354Ssam		 * ieee80211_addba_maxtries failed attempts are made.
1611178354Ssam		 */
1612178354Ssam		return 0;
1613178354Ssam	}
1614178354Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
1615178354Ssam	    "%s: enable AMPDU on %s, avgpps %d pkts %d",
1616178354Ssam	    __func__, ieee80211_wme_acnames[tap->txa_ac],
1617178354Ssam	    tap->txa_avgpps, tap->txa_pkts);
1618178354Ssam	return 1;
1619178354Ssam}
1620178354Ssam
1621178354Ssam/*
1622170530Ssam * Request A-MPDU tx aggregation.  Setup local state and
1623170530Ssam * issue an ADDBA request.  BA use will only happen after
1624170530Ssam * the other end replies with ADDBA response.
1625170530Ssam */
1626170530Ssamint
1627170530Ssamieee80211_ampdu_request(struct ieee80211_node *ni,
1628170530Ssam	struct ieee80211_tx_ampdu *tap)
1629170530Ssam{
1630170530Ssam	struct ieee80211com *ic = ni->ni_ic;
1631170530Ssam	uint16_t args[4];
1632170530Ssam	int tid, dialogtoken;
1633170530Ssam	static int tokens = 0;	/* XXX */
1634170530Ssam
1635170530Ssam	/* XXX locking */
1636170530Ssam	if ((tap->txa_flags & IEEE80211_AGGR_SETUP) == 0) {
1637170530Ssam		/* do deferred setup of state */
1638173273Ssam		IEEE80211_TAPQ_INIT(tap);
1639170530Ssam		callout_init(&tap->txa_timer, CALLOUT_MPSAFE);
1640170530Ssam		tap->txa_flags |= IEEE80211_AGGR_SETUP;
1641170530Ssam	}
1642173273Ssam	/* XXX hack for not doing proper locking */
1643173273Ssam	tap->txa_flags &= ~IEEE80211_AGGR_NAK;
1644173273Ssam
1645170530Ssam	dialogtoken = (tokens+1) % 63;		/* XXX */
1646170530Ssam
1647170530Ssam	tid = WME_AC_TO_TID(tap->txa_ac);
1648170530Ssam	args[0] = dialogtoken;
1649170530Ssam	args[1]	= IEEE80211_BAPS_POLICY_IMMEDIATE
1650170530Ssam		| SM(tid, IEEE80211_BAPS_TID)
1651170530Ssam		| SM(IEEE80211_AGGR_BAWMAX, IEEE80211_BAPS_BUFSIZ)
1652170530Ssam		;
1653170530Ssam	args[2] = 0;	/* batimeout */
1654170530Ssam	args[3] = SM(0, IEEE80211_BASEQ_START)
1655170530Ssam		| SM(0, IEEE80211_BASEQ_FRAG)
1656170530Ssam		;
1657170530Ssam	/* NB: do first so there's no race against reply */
1658170530Ssam	if (!ic->ic_addba_request(ni, tap, dialogtoken, args[1], args[2])) {
1659170530Ssam		/* unable to setup state, don't make request */
1660178354Ssam		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
1661173273Ssam		    ni, "%s: could not setup BA stream for AC %d",
1662173273Ssam		    __func__, tap->txa_ac);
1663173273Ssam		/* defer next try so we don't slam the driver with requests */
1664178354Ssam		tap->txa_attempts = ieee80211_addba_maxtries;
1665178354Ssam		/* NB: check in case driver wants to override */
1666178354Ssam		if (tap->txa_nextrequest <= ticks)
1667178354Ssam			tap->txa_nextrequest = ticks + ieee80211_addba_backoff;
1668170530Ssam		return 0;
1669170530Ssam	}
1670170530Ssam	tokens = dialogtoken;			/* allocate token */
1671170530Ssam	return ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
1672170530Ssam		IEEE80211_ACTION_BA_ADDBA_REQUEST, args);
1673170530Ssam}
1674170530Ssam
1675170530Ssam/*
1676173273Ssam * Terminate an AMPDU tx stream.  State is reclaimed
1677173273Ssam * and the peer notified with a DelBA Action frame.
1678173273Ssam */
1679173273Ssamvoid
1680173273Ssamieee80211_ampdu_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
1681173273Ssam{
1682173273Ssam	struct ieee80211com *ic = ni->ni_ic;
1683178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1684173273Ssam	uint16_t args[4];
1685173273Ssam
1686173273Ssam	/* XXX locking */
1687173273Ssam	if (IEEE80211_AMPDU_RUNNING(tap)) {
1688178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1689173273Ssam		    ni, "%s: stop BA stream for AC %d", __func__, tap->txa_ac);
1690178354Ssam		vap->iv_stats.is_ampdu_stop++;
1691173273Ssam
1692173273Ssam		ic->ic_addba_stop(ni, tap);
1693173273Ssam		args[0] = WME_AC_TO_TID(tap->txa_ac);
1694173273Ssam		args[1] = IEEE80211_DELBAPS_INIT;
1695173273Ssam		args[2] = 1;				/* XXX reason code */
1696173273Ssam		ieee80211_send_action(ni, IEEE80211_ACTION_CAT_BA,
1697173273Ssam			IEEE80211_ACTION_BA_DELBA, args);
1698173273Ssam	} else {
1699178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1700173273Ssam		    ni, "%s: BA stream for AC %d not running",
1701173273Ssam		    __func__, tap->txa_ac);
1702178354Ssam		vap->iv_stats.is_ampdu_stop_failed++;
1703173273Ssam	}
1704173273Ssam}
1705173273Ssam
1706173273Ssam/*
1707170530Ssam * Transmit a BAR frame to the specified node.  The
1708170530Ssam * BAR contents are drawn from the supplied aggregation
1709170530Ssam * state associated with the node.
1710170530Ssam */
1711170530Ssamint
1712170530Ssamieee80211_send_bar(struct ieee80211_node *ni,
1713170530Ssam	const struct ieee80211_tx_ampdu *tap)
1714170530Ssam{
1715178354Ssam#define	senderr(_x, _v)	do { vap->iv_stats._v++; ret = _x; goto bad; } while (0)
1716170530Ssam#define	ADDSHORT(frm, v) do {			\
1717170530Ssam	frm[0] = (v) & 0xff;			\
1718170530Ssam	frm[1] = (v) >> 8;			\
1719170530Ssam	frm += 2;				\
1720170530Ssam} while (0)
1721178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1722170530Ssam	struct ieee80211com *ic = ni->ni_ic;
1723170530Ssam	struct ieee80211_frame_min *wh;
1724170530Ssam	struct mbuf *m;
1725170530Ssam	uint8_t *frm;
1726170530Ssam	uint16_t barctl, barseqctl;
1727170530Ssam	int tid, ret;
1728170530Ssam
1729170530Ssam	ieee80211_ref_node(ni);
1730170530Ssam
1731170530Ssam	m = ieee80211_getmgtframe(&frm,
1732170530Ssam		ic->ic_headroom + sizeof(struct ieee80211_frame_min),
1733170530Ssam		sizeof(struct ieee80211_ba_request)
1734170530Ssam	);
1735170530Ssam	if (m == NULL)
1736170530Ssam		senderr(ENOMEM, is_tx_nobuf);
1737170530Ssam
1738170530Ssam	wh = mtod(m, struct ieee80211_frame_min *);
1739170530Ssam	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 |
1740170530Ssam		IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_BAR;
1741170530Ssam	wh->i_fc[1] = 0;
1742170530Ssam	IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
1743178354Ssam	IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1744170530Ssam
1745170530Ssam	tid = WME_AC_TO_TID(tap->txa_ac);
1746170530Ssam	barctl 	= (tap->txa_flags & IEEE80211_AGGR_IMMEDIATE ?
1747170530Ssam			IEEE80211_BAPS_POLICY_IMMEDIATE :
1748170530Ssam			IEEE80211_BAPS_POLICY_DELAYED)
1749170530Ssam		| SM(tid, IEEE80211_BAPS_TID)
1750170530Ssam		| SM(tap->txa_wnd, IEEE80211_BAPS_BUFSIZ)
1751170530Ssam		;
1752170530Ssam	barseqctl = SM(tap->txa_start, IEEE80211_BASEQ_START)
1753170530Ssam		| SM(0, IEEE80211_BASEQ_FRAG)
1754170530Ssam		;
1755170530Ssam	ADDSHORT(frm, barctl);
1756170530Ssam	ADDSHORT(frm, barseqctl);
1757170530Ssam	m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
1758170530Ssam
1759178354Ssam	M_WME_SETAC(m, WME_AC_VO);
1760178354Ssam
1761170530Ssam	IEEE80211_NODE_STAT(ni, tx_mgmt);	/* XXX tx_ctl? */
1762170530Ssam
1763178354Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
1764173273Ssam	    ni, "send bar frame (tid %u start %u) on channel %u",
1765173273Ssam	    tid, tap->txa_start, ieee80211_chan2ieee(ic, ic->ic_curchan));
1766170530Ssam
1767178354Ssam	return ic->ic_raw_xmit(ni, m, NULL);
1768170530Ssambad:
1769170530Ssam	ieee80211_free_node(ni);
1770170530Ssam	return ret;
1771170530Ssam#undef ADDSHORT
1772170530Ssam#undef senderr
1773170530Ssam}
1774170530Ssam
1775170530Ssam/*
1776170530Ssam * Send an action management frame.  The arguments are stuff
1777170530Ssam * into a frame without inspection; the caller is assumed to
1778170530Ssam * prepare them carefully (e.g. based on the aggregation state).
1779170530Ssam */
1780170530Ssamint
1781170530Ssamieee80211_send_action(struct ieee80211_node *ni,
1782170530Ssam	int category, int action, uint16_t args[4])
1783170530Ssam{
1784178354Ssam#define	senderr(_x, _v)	do { vap->iv_stats._v++; ret = _x; goto bad; } while (0)
1785170530Ssam#define	ADDSHORT(frm, v) do {			\
1786170530Ssam	frm[0] = (v) & 0xff;			\
1787170530Ssam	frm[1] = (v) >> 8;			\
1788170530Ssam	frm += 2;				\
1789170530Ssam} while (0)
1790178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1791170530Ssam	struct ieee80211com *ic = ni->ni_ic;
1792170530Ssam	struct mbuf *m;
1793170530Ssam	uint8_t *frm;
1794170530Ssam	uint16_t baparamset;
1795170530Ssam	int ret;
1796170530Ssam
1797170530Ssam	KASSERT(ni != NULL, ("null node"));
1798170530Ssam
1799170530Ssam	/*
1800170530Ssam	 * Hold a reference on the node so it doesn't go away until after
1801170530Ssam	 * the xmit is complete all the way in the driver.  On error we
1802170530Ssam	 * will remove our reference.
1803170530Ssam	 */
1804178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1805170530Ssam		"ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
1806170530Ssam		__func__, __LINE__,
1807170530Ssam		ni, ether_sprintf(ni->ni_macaddr),
1808170530Ssam		ieee80211_node_refcnt(ni)+1);
1809170530Ssam	ieee80211_ref_node(ni);
1810170530Ssam
1811170530Ssam	m = ieee80211_getmgtframe(&frm,
1812170530Ssam		ic->ic_headroom + sizeof(struct ieee80211_frame),
1813170530Ssam		  sizeof(uint16_t)	/* action+category */
1814170530Ssam		/* XXX may action payload */
1815170530Ssam		+ sizeof(struct ieee80211_action_ba_addbaresponse)
1816170530Ssam	);
1817170530Ssam	if (m == NULL)
1818170530Ssam		senderr(ENOMEM, is_tx_nobuf);
1819170530Ssam
1820170530Ssam	*frm++ = category;
1821170530Ssam	*frm++ = action;
1822170530Ssam	switch (category) {
1823170530Ssam	case IEEE80211_ACTION_CAT_BA:
1824170530Ssam		switch (action) {
1825170530Ssam		case IEEE80211_ACTION_BA_ADDBA_REQUEST:
1826178354Ssam			IEEE80211_NOTE(vap,
1827170530Ssam			    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1828173273Ssam			    "send ADDBA request: dialogtoken %d "
1829173273Ssam			    "baparamset 0x%x (tid %d) batimeout 0x%x baseqctl 0x%x",
1830173273Ssam			    args[0], args[1], MS(args[1], IEEE80211_BAPS_TID),
1831173273Ssam			    args[2], args[3]);
1832170530Ssam
1833170530Ssam			*frm++ = args[0];	/* dialog token */
1834170530Ssam			ADDSHORT(frm, args[1]);	/* baparamset */
1835170530Ssam			ADDSHORT(frm, args[2]);	/* batimeout */
1836170530Ssam			ADDSHORT(frm, args[3]);	/* baseqctl */
1837170530Ssam			break;
1838170530Ssam		case IEEE80211_ACTION_BA_ADDBA_RESPONSE:
1839178354Ssam			IEEE80211_NOTE(vap,
1840170530Ssam			    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1841170530Ssam			    "send ADDBA response: dialogtoken %d status %d "
1842170530Ssam			    "baparamset 0x%x (tid %d) batimeout %d",
1843170530Ssam			    args[0], args[1], args[2],
1844170530Ssam			    MS(args[2], IEEE80211_BAPS_TID), args[3]);
1845170530Ssam
1846170530Ssam			*frm++ = args[0];	/* dialog token */
1847170530Ssam			ADDSHORT(frm, args[1]);	/* statuscode */
1848170530Ssam			ADDSHORT(frm, args[2]);	/* baparamset */
1849170530Ssam			ADDSHORT(frm, args[3]);	/* batimeout */
1850170530Ssam			break;
1851170530Ssam		case IEEE80211_ACTION_BA_DELBA:
1852170530Ssam			/* XXX */
1853170530Ssam			baparamset = SM(args[0], IEEE80211_DELBAPS_TID)
1854170530Ssam				   | SM(args[1], IEEE80211_DELBAPS_INIT)
1855170530Ssam				   ;
1856170530Ssam			ADDSHORT(frm, baparamset);
1857170530Ssam			ADDSHORT(frm, args[2]);	/* reason code */
1858170530Ssam
1859178354Ssam			IEEE80211_NOTE(vap,
1860170530Ssam			    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1861170530Ssam			    "send DELBA action: tid %d, initiator %d reason %d",
1862170530Ssam			    args[0], args[1], args[2]);
1863170530Ssam			break;
1864170530Ssam		default:
1865170530Ssam			goto badaction;
1866170530Ssam		}
1867170530Ssam		break;
1868170530Ssam	case IEEE80211_ACTION_CAT_HT:
1869170530Ssam		switch (action) {
1870170530Ssam		case IEEE80211_ACTION_HT_TXCHWIDTH:
1871178354Ssam			IEEE80211_NOTE(vap,
1872170530Ssam			    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1873170530Ssam			    ni, "send HT txchwidth: width %d",
1874178354Ssam			    IEEE80211_IS_CHAN_HT40(ni->ni_chan) ? 40 : 20
1875170530Ssam			);
1876178354Ssam			*frm++ = IEEE80211_IS_CHAN_HT40(ni->ni_chan) ?
1877170530Ssam				IEEE80211_A_HT_TXCHWIDTH_2040 :
1878170530Ssam				IEEE80211_A_HT_TXCHWIDTH_20;
1879170530Ssam			break;
1880170530Ssam		default:
1881170530Ssam			goto badaction;
1882170530Ssam		}
1883170530Ssam		break;
1884170530Ssam	default:
1885170530Ssam	badaction:
1886178354Ssam		IEEE80211_NOTE(vap,
1887170530Ssam		    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1888170530Ssam		    "%s: unsupported category %d action %d", __func__,
1889170530Ssam		    category, action);
1890170530Ssam		senderr(EINVAL, is_tx_unknownmgt);
1891170530Ssam		/* NOTREACHED */
1892170530Ssam	}
1893170530Ssam	m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
1894170530Ssam
1895178354Ssam	return ieee80211_mgmt_output(ni, m, IEEE80211_FC0_SUBTYPE_ACTION);
1896170530Ssambad:
1897170530Ssam	ieee80211_free_node(ni);
1898178354Ssam	if (m != NULL)
1899178354Ssam		m_freem(m);
1900170530Ssam	return ret;
1901170530Ssam#undef ADDSHORT
1902170530Ssam#undef senderr
1903170530Ssam}
1904170530Ssam
1905170530Ssam/*
1906170530Ssam * Construct the MCS bit mask for inclusion
1907170530Ssam * in an HT information element.
1908170530Ssam */
1909170530Ssamstatic void
1910170530Ssamieee80211_set_htrates(uint8_t *frm, const struct ieee80211_htrateset *rs)
1911170530Ssam{
1912170530Ssam	int i;
1913170530Ssam
1914170530Ssam	for (i = 0; i < rs->rs_nrates; i++) {
1915170530Ssam		int r = rs->rs_rates[i] & IEEE80211_RATE_VAL;
1916170530Ssam		if (r < IEEE80211_HTRATE_MAXSIZE) {	/* XXX? */
1917170530Ssam			/* NB: this assumes a particular implementation */
1918170530Ssam			setbit(frm, r);
1919170530Ssam		}
1920170530Ssam	}
1921170530Ssam}
1922170530Ssam
1923170530Ssam/*
1924170530Ssam * Add body of an HTCAP information element.
1925170530Ssam */
1926170530Ssamstatic uint8_t *
1927170530Ssamieee80211_add_htcap_body(uint8_t *frm, struct ieee80211_node *ni)
1928170530Ssam{
1929170530Ssam#define	ADDSHORT(frm, v) do {			\
1930170530Ssam	frm[0] = (v) & 0xff;			\
1931170530Ssam	frm[1] = (v) >> 8;			\
1932170530Ssam	frm += 2;				\
1933170530Ssam} while (0)
1934178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1935170530Ssam	uint16_t caps;
1936173865Ssam	int rxmax, density;
1937170530Ssam
1938170530Ssam	/* HT capabilities */
1939178354Ssam	caps = vap->iv_htcaps & 0xffff;
1940173273Ssam	/*
1941173273Ssam	 * Note channel width depends on whether we are operating as
1942173273Ssam	 * a sta or not.  When operating as a sta we are generating
1943173273Ssam	 * a request based on our desired configuration.  Otherwise
1944173273Ssam	 * we are operational and the channel attributes identify
1945173273Ssam	 * how we've been setup (which might be different if a fixed
1946173273Ssam	 * channel is specified).
1947173273Ssam	 */
1948178354Ssam	if (vap->iv_opmode == IEEE80211_M_STA) {
1949173273Ssam		/* override 20/40 use based on config */
1950178354Ssam		if (vap->iv_flags_ext & IEEE80211_FEXT_USEHT40)
1951173273Ssam			caps |= IEEE80211_HTCAP_CHWIDTH40;
1952173273Ssam		else
1953173273Ssam			caps &= ~IEEE80211_HTCAP_CHWIDTH40;
1954173865Ssam		/* use advertised setting (XXX locally constraint) */
1955173865Ssam		rxmax = MS(ni->ni_htparam, IEEE80211_HTCAP_MAXRXAMPDU);
1956173865Ssam		density = MS(ni->ni_htparam, IEEE80211_HTCAP_MPDUDENSITY);
1957173273Ssam	} else {
1958173273Ssam		/* override 20/40 use based on current channel */
1959178354Ssam		if (IEEE80211_IS_CHAN_HT40(ni->ni_chan))
1960173273Ssam			caps |= IEEE80211_HTCAP_CHWIDTH40;
1961173273Ssam		else
1962173273Ssam			caps &= ~IEEE80211_HTCAP_CHWIDTH40;
1963178354Ssam		rxmax = vap->iv_ampdu_rxmax;
1964178354Ssam		density = vap->iv_ampdu_density;
1965173273Ssam	}
1966170530Ssam	/* adjust short GI based on channel and config */
1967178354Ssam	if ((vap->iv_flags_ext & IEEE80211_FEXT_SHORTGI20) == 0)
1968170530Ssam		caps &= ~IEEE80211_HTCAP_SHORTGI20;
1969178354Ssam	if ((vap->iv_flags_ext & IEEE80211_FEXT_SHORTGI40) == 0 ||
1970170530Ssam	    (caps & IEEE80211_HTCAP_CHWIDTH40) == 0)
1971170530Ssam		caps &= ~IEEE80211_HTCAP_SHORTGI40;
1972170530Ssam	ADDSHORT(frm, caps);
1973170530Ssam
1974170530Ssam	/* HT parameters */
1975173865Ssam	*frm = SM(rxmax, IEEE80211_HTCAP_MAXRXAMPDU)
1976173865Ssam	     | SM(density, IEEE80211_HTCAP_MPDUDENSITY)
1977173273Ssam	     ;
1978170530Ssam	frm++;
1979170530Ssam
1980170530Ssam	/* pre-zero remainder of ie */
1981170530Ssam	memset(frm, 0, sizeof(struct ieee80211_ie_htcap) -
1982170530Ssam		__offsetof(struct ieee80211_ie_htcap, hc_mcsset));
1983170530Ssam
1984170530Ssam	/* supported MCS set */
1985173273Ssam	/*
1986173273Ssam	 * XXX it would better to get the rate set from ni_htrates
1987173273Ssam	 * so we can restrict it but for sta mode ni_htrates isn't
1988173273Ssam	 * setup when we're called to form an AssocReq frame so for
1989173273Ssam	 * now we're restricted to the default HT rate set.
1990173273Ssam	 */
1991173273Ssam	ieee80211_set_htrates(frm, &ieee80211_rateset_11n);
1992170530Ssam
1993170530Ssam	frm += sizeof(struct ieee80211_ie_htcap) -
1994170530Ssam		__offsetof(struct ieee80211_ie_htcap, hc_mcsset);
1995170530Ssam	return frm;
1996170530Ssam#undef ADDSHORT
1997170530Ssam}
1998170530Ssam
1999170530Ssam/*
2000170530Ssam * Add 802.11n HT capabilities information element
2001170530Ssam */
2002170530Ssamuint8_t *
2003170530Ssamieee80211_add_htcap(uint8_t *frm, struct ieee80211_node *ni)
2004170530Ssam{
2005170530Ssam	frm[0] = IEEE80211_ELEMID_HTCAP;
2006170530Ssam	frm[1] = sizeof(struct ieee80211_ie_htcap) - 2;
2007170530Ssam	return ieee80211_add_htcap_body(frm + 2, ni);
2008170530Ssam}
2009170530Ssam
2010170530Ssam/*
2011170530Ssam * Add Broadcom OUI wrapped standard HTCAP ie; this is
2012170530Ssam * used for compatibility w/ pre-draft implementations.
2013170530Ssam */
2014170530Ssamuint8_t *
2015170530Ssamieee80211_add_htcap_vendor(uint8_t *frm, struct ieee80211_node *ni)
2016170530Ssam{
2017170530Ssam	frm[0] = IEEE80211_ELEMID_VENDOR;
2018170530Ssam	frm[1] = 4 + sizeof(struct ieee80211_ie_htcap) - 2;
2019170530Ssam	frm[2] = (BCM_OUI >> 0) & 0xff;
2020170530Ssam	frm[3] = (BCM_OUI >> 8) & 0xff;
2021170530Ssam	frm[4] = (BCM_OUI >> 16) & 0xff;
2022170530Ssam	frm[5] = BCM_OUI_HTCAP;
2023170530Ssam	return ieee80211_add_htcap_body(frm + 6, ni);
2024170530Ssam}
2025170530Ssam
2026170530Ssam/*
2027170530Ssam * Construct the MCS bit mask of basic rates
2028170530Ssam * for inclusion in an HT information element.
2029170530Ssam */
2030170530Ssamstatic void
2031170530Ssamieee80211_set_basic_htrates(uint8_t *frm, const struct ieee80211_htrateset *rs)
2032170530Ssam{
2033170530Ssam	int i;
2034170530Ssam
2035170530Ssam	for (i = 0; i < rs->rs_nrates; i++) {
2036170530Ssam		int r = rs->rs_rates[i] & IEEE80211_RATE_VAL;
2037170530Ssam		if ((rs->rs_rates[i] & IEEE80211_RATE_BASIC) &&
2038170530Ssam		    r < IEEE80211_HTRATE_MAXSIZE) {
2039170530Ssam			/* NB: this assumes a particular implementation */
2040170530Ssam			setbit(frm, r);
2041170530Ssam		}
2042170530Ssam	}
2043170530Ssam}
2044170530Ssam
2045170530Ssam/*
2046172211Ssam * Update the HTINFO ie for a beacon frame.
2047172211Ssam */
2048172211Ssamvoid
2049178354Ssamieee80211_ht_update_beacon(struct ieee80211vap *vap,
2050172211Ssam	struct ieee80211_beacon_offsets *bo)
2051172211Ssam{
2052172211Ssam#define	PROTMODE	(IEEE80211_HTINFO_OPMODE|IEEE80211_HTINFO_NONHT_PRESENT)
2053178354Ssam	const struct ieee80211_channel *bsschan = vap->iv_bss->ni_chan;
2054178354Ssam	struct ieee80211com *ic = vap->iv_ic;
2055172211Ssam	struct ieee80211_ie_htinfo *ht =
2056172211Ssam	   (struct ieee80211_ie_htinfo *) bo->bo_htinfo;
2057172211Ssam
2058172211Ssam	/* XXX only update on channel change */
2059178354Ssam	ht->hi_ctrlchannel = ieee80211_chan2ieee(ic, bsschan);
2060172211Ssam	ht->hi_byte1 = IEEE80211_HTINFO_RIFSMODE_PROH;
2061178354Ssam	if (IEEE80211_IS_CHAN_HT40U(bsschan))
2062172211Ssam		ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_ABOVE;
2063178354Ssam	else if (IEEE80211_IS_CHAN_HT40D(bsschan))
2064172211Ssam		ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_BELOW;
2065172211Ssam	else
2066172211Ssam		ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_NONE;
2067178354Ssam	if (IEEE80211_IS_CHAN_HT40(bsschan))
2068172211Ssam		ht->hi_byte1 |= IEEE80211_HTINFO_TXWIDTH_2040;
2069172211Ssam
2070172211Ssam	/* protection mode */
2071172211Ssam	ht->hi_byte2 = (ht->hi_byte2 &~ PROTMODE) | ic->ic_curhtprotmode;
2072172211Ssam
2073172211Ssam	/* XXX propagate to vendor ie's */
2074172211Ssam#undef PROTMODE
2075172211Ssam}
2076172211Ssam
2077172211Ssam/*
2078170530Ssam * Add body of an HTINFO information element.
2079173273Ssam *
2080173273Ssam * NB: We don't use struct ieee80211_ie_htinfo because we can
2081173273Ssam * be called to fillin both a standard ie and a compat ie that
2082173273Ssam * has a vendor OUI at the front.
2083170530Ssam */
2084170530Ssamstatic uint8_t *
2085170530Ssamieee80211_add_htinfo_body(uint8_t *frm, struct ieee80211_node *ni)
2086170530Ssam{
2087170530Ssam	struct ieee80211com *ic = ni->ni_ic;
2088170530Ssam
2089170530Ssam	/* pre-zero remainder of ie */
2090170530Ssam	memset(frm, 0, sizeof(struct ieee80211_ie_htinfo) - 2);
2091170530Ssam
2092170530Ssam	/* primary/control channel center */
2093178354Ssam	*frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
2094170530Ssam
2095170530Ssam	frm[0] = IEEE80211_HTINFO_RIFSMODE_PROH;
2096178354Ssam	if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan))
2097170530Ssam		frm[0] |= IEEE80211_HTINFO_2NDCHAN_ABOVE;
2098178354Ssam	else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan))
2099170530Ssam		frm[0] |= IEEE80211_HTINFO_2NDCHAN_BELOW;
2100170530Ssam	else
2101170530Ssam		frm[0] |= IEEE80211_HTINFO_2NDCHAN_NONE;
2102178354Ssam	if (IEEE80211_IS_CHAN_HT40(ni->ni_chan))
2103170530Ssam		frm[0] |= IEEE80211_HTINFO_TXWIDTH_2040;
2104170530Ssam
2105172211Ssam	frm[1] = ic->ic_curhtprotmode;
2106170530Ssam
2107170530Ssam	frm += 5;
2108170530Ssam
2109170530Ssam	/* basic MCS set */
2110170530Ssam	ieee80211_set_basic_htrates(frm, &ni->ni_htrates);
2111170530Ssam	frm += sizeof(struct ieee80211_ie_htinfo) -
2112170530Ssam		__offsetof(struct ieee80211_ie_htinfo, hi_basicmcsset);
2113170530Ssam	return frm;
2114170530Ssam}
2115170530Ssam
2116170530Ssam/*
2117170530Ssam * Add 802.11n HT information information element.
2118170530Ssam */
2119170530Ssamuint8_t *
2120170530Ssamieee80211_add_htinfo(uint8_t *frm, struct ieee80211_node *ni)
2121170530Ssam{
2122170530Ssam	frm[0] = IEEE80211_ELEMID_HTINFO;
2123170530Ssam	frm[1] = sizeof(struct ieee80211_ie_htinfo) - 2;
2124170530Ssam	return ieee80211_add_htinfo_body(frm + 2, ni);
2125170530Ssam}
2126170530Ssam
2127170530Ssam/*
2128170530Ssam * Add Broadcom OUI wrapped standard HTINFO ie; this is
2129170530Ssam * used for compatibility w/ pre-draft implementations.
2130170530Ssam */
2131170530Ssamuint8_t *
2132170530Ssamieee80211_add_htinfo_vendor(uint8_t *frm, struct ieee80211_node *ni)
2133170530Ssam{
2134170530Ssam	frm[0] = IEEE80211_ELEMID_VENDOR;
2135170530Ssam	frm[1] = 4 + sizeof(struct ieee80211_ie_htinfo) - 2;
2136170530Ssam	frm[2] = (BCM_OUI >> 0) & 0xff;
2137170530Ssam	frm[3] = (BCM_OUI >> 8) & 0xff;
2138170530Ssam	frm[4] = (BCM_OUI >> 16) & 0xff;
2139170530Ssam	frm[5] = BCM_OUI_HTINFO;
2140170530Ssam	return ieee80211_add_htinfo_body(frm + 6, ni);
2141170530Ssam}
2142