ieee80211_ht.c revision 205513
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 205513 2010-03-23 12:03:09Z rpaulo $");
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>
50195377Ssam#include <net80211/ieee80211_action.h>
51178354Ssam#include <net80211/ieee80211_input.h>
52170530Ssam
53170530Ssam/* define here, used throughout file */
54170530Ssam#define	MS(_v, _f)	(((_v) & _f) >> _f##_S)
55170530Ssam#define	SM(_v, _f)	(((_v) << _f##_S) & _f)
56170530Ssam
57178354Ssamconst struct ieee80211_mcs_rates ieee80211_htrates[16] = {
58205277Srpaulo    /*  20Mhz  SGI 40Mhz  SGI */
59178354Ssam	{  13,  14,  27,  30 },	/* MCS 0 */
60178354Ssam	{  26,  29,  54,  60 },	/* MCS 1 */
61178354Ssam	{  39,  43,  81,  90 },	/* MCS 2 */
62178354Ssam	{  52,  58, 108, 120 },	/* MCS 3 */
63178354Ssam	{  78,  87, 162, 180 },	/* MCS 4 */
64178354Ssam	{ 104, 116, 216, 240 },	/* MCS 5 */
65178354Ssam	{ 117, 130, 243, 270 },	/* MCS 6 */
66178354Ssam	{ 130, 144, 270, 300 },	/* MCS 7 */
67178354Ssam	{  26,  29,  54,  60 },	/* MCS 8 */
68178354Ssam	{  52,  58, 108, 120 },	/* MCS 9 */
69178354Ssam	{  78,  87, 162, 180 },	/* MCS 10 */
70178354Ssam	{ 104, 116, 216, 240 },	/* MCS 11 */
71178354Ssam	{ 156, 173, 324, 360 },	/* MCS 12 */
72178354Ssam	{ 208, 231, 432, 480 },	/* MCS 13 */
73178354Ssam	{ 234, 260, 486, 540 },	/* MCS 14 */
74178354Ssam	{ 260, 289, 540, 600 }	/* MCS 15 */
75170530Ssam};
76170530Ssam
77170530Ssamstatic const struct ieee80211_htrateset ieee80211_rateset_11n =
78170530Ssam	{ 16, {
79170530Ssam	          0,   1,   2,   3,   4,  5,   6,  7,  8,  9,
80170530Ssam		 10,  11,  12,  13,  14,  15 }
81170530Ssam	};
82170530Ssam
83173273Ssam#ifdef IEEE80211_AMPDU_AGE
84193115Ssamstatic	int ieee80211_ampdu_age = -1;	/* threshold for ampdu reorder q (ms) */
85193115SsamSYSCTL_PROC(_net_wlan, OID_AUTO, ampdu_age, CTLTYPE_INT | CTLFLAG_RW,
86193115Ssam	&ieee80211_ampdu_age, 0, ieee80211_sysctl_msecs_ticks, "I",
87193115Ssam	"AMPDU max reorder age (ms)");
88173273Ssam#endif
89173273Ssam
90193115Ssamstatic	int ieee80211_recv_bar_ena = 1;
91193115SsamSYSCTL_INT(_net_wlan, OID_AUTO, recv_bar, CTLFLAG_RW, &ieee80211_recv_bar_ena,
92193115Ssam	    0, "BAR frame processing (ena/dis)");
93193115Ssam
94193115Ssamstatic	int ieee80211_addba_timeout = -1;/* timeout for ADDBA response */
95193115SsamSYSCTL_PROC(_net_wlan, OID_AUTO, addba_timeout, CTLTYPE_INT | CTLFLAG_RW,
96193115Ssam	&ieee80211_addba_timeout, 0, ieee80211_sysctl_msecs_ticks, "I",
97193115Ssam	"ADDBA request timeout (ms)");
98193115Ssamstatic	int ieee80211_addba_backoff = -1;/* backoff after max ADDBA requests */
99193115SsamSYSCTL_PROC(_net_wlan, OID_AUTO, addba_backoff, CTLTYPE_INT | CTLFLAG_RW,
100193115Ssam	&ieee80211_addba_backoff, 0, ieee80211_sysctl_msecs_ticks, "I",
101193115Ssam	"ADDBA request backoff (ms)");
102193115Ssamstatic	int ieee80211_addba_maxtries = 3;/* max ADDBA requests before backoff */
103193115SsamSYSCTL_INT(_net_wlan, OID_AUTO, addba_maxtries, CTLTYPE_INT | CTLFLAG_RW,
104193115Ssam	&ieee80211_addba_maxtries, 0, "max ADDBA requests sent before backoff");
105193115Ssam
106193115Ssamstatic	int ieee80211_bar_timeout = -1;	/* timeout waiting for BAR response */
107193115Ssamstatic	int ieee80211_bar_maxtries = 50;/* max BAR requests before DELBA */
108193115Ssam
109195377Ssamstatic	ieee80211_recv_action_func ht_recv_action_ba_addba_request;
110195377Ssamstatic	ieee80211_recv_action_func ht_recv_action_ba_addba_response;
111195377Ssamstatic	ieee80211_recv_action_func ht_recv_action_ba_delba;
112195377Ssamstatic	ieee80211_recv_action_func ht_recv_action_ht_mimopwrsave;
113195377Ssamstatic	ieee80211_recv_action_func ht_recv_action_ht_txchwidth;
114195377Ssam
115195377Ssamstatic	ieee80211_send_action_func ht_send_action_ba_addba;
116195377Ssamstatic	ieee80211_send_action_func ht_send_action_ba_delba;
117195377Ssamstatic	ieee80211_send_action_func ht_send_action_ht_txchwidth;
118195377Ssam
119178354Ssamstatic void
120195377Ssamieee80211_ht_init(void)
121178354Ssam{
122195377Ssam	/*
123195377Ssam	 * Setup HT parameters that depends on the clock frequency.
124195377Ssam	 */
125178354Ssam#ifdef IEEE80211_AMPDU_AGE
126178354Ssam	ieee80211_ampdu_age = msecs_to_ticks(500);
127178354Ssam#endif
128178354Ssam	ieee80211_addba_timeout = msecs_to_ticks(250);
129178354Ssam	ieee80211_addba_backoff = msecs_to_ticks(10*1000);
130184280Ssam	ieee80211_bar_timeout = msecs_to_ticks(250);
131195377Ssam	/*
132195377Ssam	 * Register action frame handlers.
133195377Ssam	 */
134195377Ssam	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_BA,
135195377Ssam	    IEEE80211_ACTION_BA_ADDBA_REQUEST, ht_recv_action_ba_addba_request);
136195377Ssam	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_BA,
137195377Ssam	    IEEE80211_ACTION_BA_ADDBA_RESPONSE, ht_recv_action_ba_addba_response);
138195377Ssam	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_BA,
139195377Ssam	    IEEE80211_ACTION_BA_DELBA, ht_recv_action_ba_delba);
140195377Ssam	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_HT,
141195377Ssam	    IEEE80211_ACTION_HT_MIMOPWRSAVE, ht_recv_action_ht_mimopwrsave);
142195377Ssam	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_HT,
143195377Ssam	    IEEE80211_ACTION_HT_TXCHWIDTH, ht_recv_action_ht_txchwidth);
144195377Ssam
145195377Ssam	ieee80211_send_action_register(IEEE80211_ACTION_CAT_BA,
146195377Ssam	    IEEE80211_ACTION_BA_ADDBA_REQUEST, ht_send_action_ba_addba);
147195377Ssam	ieee80211_send_action_register(IEEE80211_ACTION_CAT_BA,
148195377Ssam	    IEEE80211_ACTION_BA_ADDBA_RESPONSE, ht_send_action_ba_addba);
149195377Ssam	ieee80211_send_action_register(IEEE80211_ACTION_CAT_BA,
150195377Ssam	    IEEE80211_ACTION_BA_DELBA, ht_send_action_ba_delba);
151195377Ssam	ieee80211_send_action_register(IEEE80211_ACTION_CAT_HT,
152195377Ssam	    IEEE80211_ACTION_HT_TXCHWIDTH, ht_send_action_ht_txchwidth);
153178354Ssam}
154195377SsamSYSINIT(wlan_ht, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_ht_init, NULL);
155170530Ssam
156178354Ssamstatic int ieee80211_ampdu_enable(struct ieee80211_node *ni,
157178354Ssam	struct ieee80211_tx_ampdu *tap);
158170530Ssamstatic int ieee80211_addba_request(struct ieee80211_node *ni,
159170530Ssam	struct ieee80211_tx_ampdu *tap,
160170530Ssam	int dialogtoken, int baparamset, int batimeout);
161170530Ssamstatic int ieee80211_addba_response(struct ieee80211_node *ni,
162170530Ssam	struct ieee80211_tx_ampdu *tap,
163170530Ssam	int code, int baparamset, int batimeout);
164170530Ssamstatic void ieee80211_addba_stop(struct ieee80211_node *ni,
165170530Ssam	struct ieee80211_tx_ampdu *tap);
166184280Ssamstatic void ieee80211_bar_response(struct ieee80211_node *ni,
167184280Ssam	struct ieee80211_tx_ampdu *tap, int status);
168184280Ssamstatic void ampdu_tx_stop(struct ieee80211_tx_ampdu *tap);
169184280Ssamstatic void bar_stop_timer(struct ieee80211_tx_ampdu *tap);
170191552Ssamstatic int ampdu_rx_start(struct ieee80211_node *, struct ieee80211_rx_ampdu *,
171191552Ssam	int baparamset, int batimeout, int baseqctl);
172191552Ssamstatic void ampdu_rx_stop(struct ieee80211_node *, struct ieee80211_rx_ampdu *);
173170530Ssam
174170530Ssamvoid
175170530Ssamieee80211_ht_attach(struct ieee80211com *ic)
176170530Ssam{
177170530Ssam	/* setup default aggregation policy */
178195377Ssam	ic->ic_recv_action = ieee80211_recv_action;
179170530Ssam	ic->ic_send_action = ieee80211_send_action;
180178354Ssam	ic->ic_ampdu_enable = ieee80211_ampdu_enable;
181170530Ssam	ic->ic_addba_request = ieee80211_addba_request;
182170530Ssam	ic->ic_addba_response = ieee80211_addba_response;
183170530Ssam	ic->ic_addba_stop = ieee80211_addba_stop;
184184280Ssam	ic->ic_bar_response = ieee80211_bar_response;
185191552Ssam	ic->ic_ampdu_rx_start = ampdu_rx_start;
186191552Ssam	ic->ic_ampdu_rx_stop = ampdu_rx_stop;
187170530Ssam
188173273Ssam	ic->ic_htprotmode = IEEE80211_PROT_RTSCTS;
189173273Ssam	ic->ic_curhtprotmode = IEEE80211_HTINFO_OPMODE_PURE;
190178354Ssam}
191173273Ssam
192178354Ssamvoid
193178354Ssamieee80211_ht_detach(struct ieee80211com *ic)
194178354Ssam{
195178354Ssam}
196173273Ssam
197178354Ssamvoid
198178354Ssamieee80211_ht_vattach(struct ieee80211vap *vap)
199178354Ssam{
200178354Ssam
201178354Ssam	/* driver can override defaults */
202178354Ssam	vap->iv_ampdu_rxmax = IEEE80211_HTCAP_MAXRXAMPDU_8K;
203178354Ssam	vap->iv_ampdu_density = IEEE80211_HTCAP_MPDUDENSITY_NA;
204178354Ssam	vap->iv_ampdu_limit = vap->iv_ampdu_rxmax;
205178354Ssam	vap->iv_amsdu_limit = vap->iv_htcaps & IEEE80211_HTCAP_MAXAMSDU;
206178354Ssam	/* tx aggregation traffic thresholds */
207178354Ssam	vap->iv_ampdu_mintraffic[WME_AC_BK] = 128;
208178354Ssam	vap->iv_ampdu_mintraffic[WME_AC_BE] = 64;
209178354Ssam	vap->iv_ampdu_mintraffic[WME_AC_VO] = 32;
210178354Ssam	vap->iv_ampdu_mintraffic[WME_AC_VI] = 32;
211178354Ssam
212178354Ssam	if (vap->iv_htcaps & IEEE80211_HTC_HT) {
213170530Ssam		/*
214173273Ssam		 * Device is HT capable; enable all HT-related
215173273Ssam		 * facilities by default.
216170530Ssam		 * XXX these choices may be too aggressive.
217170530Ssam		 */
218193655Ssam		vap->iv_flags_ht |= IEEE80211_FHT_HT
219193655Ssam				 |  IEEE80211_FHT_HTCOMPAT
220193655Ssam				 ;
221178354Ssam		if (vap->iv_htcaps & IEEE80211_HTCAP_SHORTGI20)
222193655Ssam			vap->iv_flags_ht |= IEEE80211_FHT_SHORTGI20;
223173273Ssam		/* XXX infer from channel list? */
224178354Ssam		if (vap->iv_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
225193655Ssam			vap->iv_flags_ht |= IEEE80211_FHT_USEHT40;
226178354Ssam			if (vap->iv_htcaps & IEEE80211_HTCAP_SHORTGI40)
227193655Ssam				vap->iv_flags_ht |= IEEE80211_FHT_SHORTGI40;
228170530Ssam		}
229183256Ssam		/* enable RIFS if capable */
230183256Ssam		if (vap->iv_htcaps & IEEE80211_HTC_RIFS)
231193655Ssam			vap->iv_flags_ht |= IEEE80211_FHT_RIFS;
232183256Ssam
233170530Ssam		/* NB: A-MPDU and A-MSDU rx are mandated, these are tx only */
234193655Ssam		vap->iv_flags_ht |= IEEE80211_FHT_AMPDU_RX;
235178354Ssam		if (vap->iv_htcaps & IEEE80211_HTC_AMPDU)
236193655Ssam			vap->iv_flags_ht |= IEEE80211_FHT_AMPDU_TX;
237193655Ssam		vap->iv_flags_ht |= IEEE80211_FHT_AMSDU_RX;
238178354Ssam		if (vap->iv_htcaps & IEEE80211_HTC_AMSDU)
239193655Ssam			vap->iv_flags_ht |= IEEE80211_FHT_AMSDU_TX;
240170530Ssam	}
241178354Ssam	/* NB: disable default legacy WDS, too many issues right now */
242178354Ssam	if (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY)
243193655Ssam		vap->iv_flags_ht &= ~IEEE80211_FHT_HT;
244170530Ssam}
245170530Ssam
246170530Ssamvoid
247178354Ssamieee80211_ht_vdetach(struct ieee80211vap *vap)
248170530Ssam{
249170530Ssam}
250170530Ssam
251170530Ssamstatic void
252205277Srpauloht_rateprint(struct ieee80211com *ic, int mode,
253205277Srpaulo	const struct ieee80211_htrateset *rs, int maxmcs, int ratetype)
254170530Ssam{
255170530Ssam	int i, rate, mword;
256170530Ssam
257205277Srpaulo	for (i = 0; i < rs->rs_nrates && i < maxmcs; i++) {
258172226Ssam		mword = ieee80211_rate2media(ic,
259172226Ssam		    rs->rs_rates[i] | IEEE80211_RATE_MCS, mode);
260170530Ssam		if (IFM_SUBTYPE(mword) != IFM_IEEE80211_MCS)
261170530Ssam			continue;
262205277Srpaulo		switch (ratetype) {
263205277Srpaulo		case 0:
264205277Srpaulo			rate = ieee80211_htrates[
265205277Srpaulo			    rs->rs_rates[i]].ht20_rate_800ns;
266205277Srpaulo			break;
267205277Srpaulo		case 1:
268205277Srpaulo			rate = ieee80211_htrates[
269205277Srpaulo			    rs->rs_rates[i]].ht20_rate_400ns;
270205277Srpaulo			break;
271205277Srpaulo		case 2:
272205277Srpaulo			rate = ieee80211_htrates[
273205277Srpaulo			    rs->rs_rates[i]].ht40_rate_800ns;
274205277Srpaulo			break;
275205277Srpaulo		default:
276205277Srpaulo			rate = ieee80211_htrates[
277205277Srpaulo			    rs->rs_rates[i]].ht40_rate_400ns;
278205277Srpaulo			break;
279205277Srpaulo		}
280170530Ssam		printf("%s%d%sMbps", (i != 0 ? " " : ""),
281170530Ssam		    rate / 2, ((rate & 0x1) != 0 ? ".5" : ""));
282170530Ssam	}
283170530Ssam	printf("\n");
284170530Ssam}
285170530Ssam
286205277Srpaulostatic void
287205277Srpauloht_announce(struct ieee80211com *ic, int mode,
288205277Srpaulo	const struct ieee80211_htrateset *rs)
289205277Srpaulo{
290205277Srpaulo	struct ifnet *ifp = ic->ic_ifp;
291205281Srpaulo	int maxmcs = 2 * 8;
292205277Srpaulo	const char *modestr = ieee80211_phymode_name[mode];
293205277Srpaulo
294205277Srpaulo	KASSERT(maxmcs <= 16, ("maxmcs > 16"));
295205277Srpaulo	if_printf(ifp, "%d MCS rates\n", maxmcs);
296205277Srpaulo	if_printf(ifp, "%s MCS 20Mhz: ", modestr);
297205277Srpaulo	ht_rateprint(ic, mode, rs, maxmcs, 0);
298205277Srpaulo	if_printf(ifp, "%s MCS 20Mhz SGI: ", modestr);
299205277Srpaulo	ht_rateprint(ic, mode, rs, maxmcs, 1);
300205277Srpaulo	if_printf(ifp, "%s MCS 40Mhz: ", modestr);
301205277Srpaulo	ht_rateprint(ic, mode, rs, maxmcs, 2);
302205277Srpaulo	if_printf(ifp, "%s MCS 40Mhz SGI: ", modestr);
303205277Srpaulo	ht_rateprint(ic, mode, rs, maxmcs, 3);
304205277Srpaulo}
305205277Srpaulo
306170530Ssamvoid
307170530Ssamieee80211_ht_announce(struct ieee80211com *ic)
308170530Ssam{
309170530Ssam	if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA))
310170530Ssam		ht_announce(ic, IEEE80211_MODE_11NA, &ieee80211_rateset_11n);
311170530Ssam	if (isset(ic->ic_modecaps, IEEE80211_MODE_11NG))
312170530Ssam		ht_announce(ic, IEEE80211_MODE_11NG, &ieee80211_rateset_11n);
313170530Ssam}
314170530Ssam
315170530Ssamconst struct ieee80211_htrateset *
316170530Ssamieee80211_get_suphtrates(struct ieee80211com *ic,
317170530Ssam	const struct ieee80211_channel *c)
318170530Ssam{
319173273Ssam	return &ieee80211_rateset_11n;
320170530Ssam}
321170530Ssam
322170530Ssam/*
323170530Ssam * Receive processing.
324170530Ssam */
325170530Ssam
326170530Ssam/*
327170530Ssam * Decap the encapsulated A-MSDU frames and dispatch all but
328170530Ssam * the last for delivery.  The last frame is returned for
329170530Ssam * delivery via the normal path.
330170530Ssam */
331170530Ssamstruct mbuf *
332170530Ssamieee80211_decap_amsdu(struct ieee80211_node *ni, struct mbuf *m)
333170530Ssam{
334178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
335173462Ssam	int framelen;
336170530Ssam	struct mbuf *n;
337170530Ssam
338170530Ssam	/* discard 802.3 header inserted by ieee80211_decap */
339170530Ssam	m_adj(m, sizeof(struct ether_header));
340170530Ssam
341178354Ssam	vap->iv_stats.is_amsdu_decap++;
342170530Ssam
343170530Ssam	for (;;) {
344170530Ssam		/*
345170530Ssam		 * Decap the first frame, bust it apart from the
346170530Ssam		 * remainder and deliver.  We leave the last frame
347170530Ssam		 * delivery to the caller (for consistency with other
348170530Ssam		 * code paths, could also do it here).
349170530Ssam		 */
350170530Ssam		m = ieee80211_decap1(m, &framelen);
351170530Ssam		if (m == NULL) {
352178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
353173462Ssam			    ni->ni_macaddr, "a-msdu", "%s", "decap failed");
354178354Ssam			vap->iv_stats.is_amsdu_tooshort++;
355170530Ssam			return NULL;
356170530Ssam		}
357173462Ssam		if (m->m_pkthdr.len == framelen)
358170530Ssam			break;
359170530Ssam		n = m_split(m, framelen, M_NOWAIT);
360170530Ssam		if (n == NULL) {
361178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
362170530Ssam			    ni->ni_macaddr, "a-msdu",
363170530Ssam			    "%s", "unable to split encapsulated frames");
364178354Ssam			vap->iv_stats.is_amsdu_split++;
365170530Ssam			m_freem(m);			/* NB: must reclaim */
366170530Ssam			return NULL;
367170530Ssam		}
368178354Ssam		vap->iv_deliver_data(vap, ni, m);
369170530Ssam
370170530Ssam		/*
371170530Ssam		 * Remove frame contents; each intermediate frame
372170530Ssam		 * is required to be aligned to a 4-byte boundary.
373170530Ssam		 */
374170530Ssam		m = n;
375170530Ssam		m_adj(m, roundup2(framelen, 4) - framelen);	/* padding */
376170530Ssam	}
377170530Ssam	return m;				/* last delivered by caller */
378170530Ssam}
379170530Ssam
380170530Ssam/*
381170530Ssam * Purge all frames in the A-MPDU re-order queue.
382170530Ssam */
383170530Ssamstatic void
384170530Ssamampdu_rx_purge(struct ieee80211_rx_ampdu *rap)
385170530Ssam{
386170530Ssam	struct mbuf *m;
387170530Ssam	int i;
388170530Ssam
389170530Ssam	for (i = 0; i < rap->rxa_wnd; i++) {
390170530Ssam		m = rap->rxa_m[i];
391170530Ssam		if (m != NULL) {
392170530Ssam			rap->rxa_m[i] = NULL;
393170530Ssam			rap->rxa_qbytes -= m->m_pkthdr.len;
394170530Ssam			m_freem(m);
395170530Ssam			if (--rap->rxa_qframes == 0)
396170530Ssam				break;
397170530Ssam		}
398170530Ssam	}
399170530Ssam	KASSERT(rap->rxa_qbytes == 0 && rap->rxa_qframes == 0,
400170530Ssam	    ("lost %u data, %u frames on ampdu rx q",
401170530Ssam	    rap->rxa_qbytes, rap->rxa_qframes));
402170530Ssam}
403170530Ssam
404170530Ssam/*
405178354Ssam * Start A-MPDU rx/re-order processing for the specified TID.
406178354Ssam */
407191552Ssamstatic int
408191552Ssamampdu_rx_start(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap,
409191552Ssam	int baparamset, int batimeout, int baseqctl)
410178354Ssam{
411191552Ssam	int bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
412191552Ssam
413178354Ssam	if (rap->rxa_flags & IEEE80211_AGGR_RUNNING) {
414178354Ssam		/*
415178354Ssam		 * AMPDU previously setup and not terminated with a DELBA,
416178354Ssam		 * flush the reorder q's in case anything remains.
417178354Ssam		 */
418178354Ssam		ampdu_rx_purge(rap);
419178354Ssam	}
420178354Ssam	memset(rap, 0, sizeof(*rap));
421178354Ssam	rap->rxa_wnd = (bufsiz == 0) ?
422178354Ssam	    IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
423191552Ssam	rap->rxa_start = MS(baseqctl, IEEE80211_BASEQ_START);
424178354Ssam	rap->rxa_flags |=  IEEE80211_AGGR_RUNNING | IEEE80211_AGGR_XCHGPEND;
425191552Ssam
426191552Ssam	return 0;
427178354Ssam}
428178354Ssam
429178354Ssam/*
430170530Ssam * Stop A-MPDU rx processing for the specified TID.
431170530Ssam */
432170530Ssamstatic void
433191552Ssamampdu_rx_stop(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
434170530Ssam{
435205277Srpaulo
436170530Ssam	ampdu_rx_purge(rap);
437178354Ssam	rap->rxa_flags &= ~(IEEE80211_AGGR_RUNNING | IEEE80211_AGGR_XCHGPEND);
438170530Ssam}
439170530Ssam
440170530Ssam/*
441170530Ssam * Dispatch a frame from the A-MPDU reorder queue.  The
442170530Ssam * frame is fed back into ieee80211_input marked with an
443183247Ssam * M_AMPDU_MPDU flag so it doesn't come back to us (it also
444170530Ssam * permits ieee80211_input to optimize re-processing).
445170530Ssam */
446170530Ssamstatic __inline void
447170530Ssamampdu_dispatch(struct ieee80211_node *ni, struct mbuf *m)
448170530Ssam{
449183247Ssam	m->m_flags |= M_AMPDU_MPDU;	/* bypass normal processing */
450192468Ssam	/* NB: rssi and noise are ignored w/ M_AMPDU_MPDU set */
451192468Ssam	(void) ieee80211_input(ni, m, 0, 0);
452170530Ssam}
453170530Ssam
454170530Ssam/*
455170530Ssam * Dispatch as many frames as possible from the re-order queue.
456170530Ssam * Frames will always be "at the front"; we process all frames
457170530Ssam * up to the first empty slot in the window.  On completion we
458170530Ssam * cleanup state if there are still pending frames in the current
459170530Ssam * BA window.  We assume the frame at slot 0 is already handled
460170530Ssam * by the caller; we always start at slot 1.
461170530Ssam */
462170530Ssamstatic void
463170530Ssamampdu_rx_dispatch(struct ieee80211_rx_ampdu *rap, struct ieee80211_node *ni)
464170530Ssam{
465178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
466170530Ssam	struct mbuf *m;
467170530Ssam	int i;
468170530Ssam
469170530Ssam	/* flush run of frames */
470170530Ssam	for (i = 1; i < rap->rxa_wnd; i++) {
471170530Ssam		m = rap->rxa_m[i];
472170530Ssam		if (m == NULL)
473170530Ssam			break;
474170530Ssam		rap->rxa_m[i] = NULL;
475170530Ssam		rap->rxa_qbytes -= m->m_pkthdr.len;
476170530Ssam		rap->rxa_qframes--;
477170530Ssam
478170530Ssam		ampdu_dispatch(ni, m);
479170530Ssam	}
480170530Ssam	/*
481170530Ssam	 * If frames remain, copy the mbuf pointers down so
482170530Ssam	 * they correspond to the offsets in the new window.
483170530Ssam	 */
484170530Ssam	if (rap->rxa_qframes != 0) {
485170530Ssam		int n = rap->rxa_qframes, j;
486170530Ssam		for (j = i+1; j < rap->rxa_wnd; j++) {
487170530Ssam			if (rap->rxa_m[j] != NULL) {
488170530Ssam				rap->rxa_m[j-i] = rap->rxa_m[j];
489170530Ssam				rap->rxa_m[j] = NULL;
490170530Ssam				if (--n == 0)
491170530Ssam					break;
492170530Ssam			}
493170530Ssam		}
494170530Ssam		KASSERT(n == 0, ("lost %d frames", n));
495178354Ssam		vap->iv_stats.is_ampdu_rx_copy += rap->rxa_qframes;
496170530Ssam	}
497173273Ssam	/*
498173273Ssam	 * Adjust the start of the BA window to
499173273Ssam	 * reflect the frames just dispatched.
500173273Ssam	 */
501173273Ssam	rap->rxa_start = IEEE80211_SEQ_ADD(rap->rxa_start, i);
502178354Ssam	vap->iv_stats.is_ampdu_rx_oor += i;
503170530Ssam}
504170530Ssam
505173273Ssam#ifdef IEEE80211_AMPDU_AGE
506170530Ssam/*
507173273Ssam * Dispatch all frames in the A-MPDU re-order queue.
508170530Ssam */
509170530Ssamstatic void
510173273Ssamampdu_rx_flush(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
511170530Ssam{
512178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
513170530Ssam	struct mbuf *m;
514170530Ssam	int i;
515170530Ssam
516173273Ssam	for (i = 0; i < rap->rxa_wnd; i++) {
517170530Ssam		m = rap->rxa_m[i];
518170530Ssam		if (m == NULL)
519170530Ssam			continue;
520170530Ssam		rap->rxa_m[i] = NULL;
521170530Ssam		rap->rxa_qbytes -= m->m_pkthdr.len;
522173273Ssam		rap->rxa_qframes--;
523178354Ssam		vap->iv_stats.is_ampdu_rx_oor++;
524173273Ssam
525170530Ssam		ampdu_dispatch(ni, m);
526173273Ssam		if (rap->rxa_qframes == 0)
527170530Ssam			break;
528170530Ssam	}
529170530Ssam}
530173273Ssam#endif /* IEEE80211_AMPDU_AGE */
531170530Ssam
532170530Ssam/*
533173273Ssam * Dispatch all frames in the A-MPDU re-order queue
534173273Ssam * preceding the specified sequence number.  This logic
535173273Ssam * handles window moves due to a received MSDU or BAR.
536173273Ssam */
537173273Ssamstatic void
538173273Ssamampdu_rx_flush_upto(struct ieee80211_node *ni,
539173273Ssam	struct ieee80211_rx_ampdu *rap, ieee80211_seq winstart)
540173273Ssam{
541178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
542173273Ssam	struct mbuf *m;
543173273Ssam	ieee80211_seq seqno;
544173273Ssam	int i;
545173273Ssam
546173273Ssam	/*
547173273Ssam	 * Flush any complete MSDU's with a sequence number lower
548173273Ssam	 * than winstart.  Gaps may exist.  Note that we may actually
549173273Ssam	 * dispatch frames past winstart if a run continues; this is
550173273Ssam	 * an optimization that avoids having to do a separate pass
551173273Ssam	 * to dispatch frames after moving the BA window start.
552173273Ssam	 */
553173273Ssam	seqno = rap->rxa_start;
554173273Ssam	for (i = 0; i < rap->rxa_wnd; i++) {
555173273Ssam		m = rap->rxa_m[i];
556173273Ssam		if (m != NULL) {
557173273Ssam			rap->rxa_m[i] = NULL;
558173273Ssam			rap->rxa_qbytes -= m->m_pkthdr.len;
559173273Ssam			rap->rxa_qframes--;
560178354Ssam			vap->iv_stats.is_ampdu_rx_oor++;
561173273Ssam
562173273Ssam			ampdu_dispatch(ni, m);
563173273Ssam		} else {
564173273Ssam			if (!IEEE80211_SEQ_BA_BEFORE(seqno, winstart))
565173273Ssam				break;
566173273Ssam		}
567173273Ssam		seqno = IEEE80211_SEQ_INC(seqno);
568173273Ssam	}
569173273Ssam	/*
570173273Ssam	 * If frames remain, copy the mbuf pointers down so
571173273Ssam	 * they correspond to the offsets in the new window.
572173273Ssam	 */
573173273Ssam	if (rap->rxa_qframes != 0) {
574173273Ssam		int n = rap->rxa_qframes, j;
575178354Ssam
576178354Ssam		/* NB: this loop assumes i > 0 and/or rxa_m[0] is NULL */
577178354Ssam		KASSERT(rap->rxa_m[0] == NULL,
578178354Ssam		    ("%s: BA window slot 0 occupied", __func__));
579173273Ssam		for (j = i+1; j < rap->rxa_wnd; j++) {
580173273Ssam			if (rap->rxa_m[j] != NULL) {
581173273Ssam				rap->rxa_m[j-i] = rap->rxa_m[j];
582173273Ssam				rap->rxa_m[j] = NULL;
583173273Ssam				if (--n == 0)
584173273Ssam					break;
585173273Ssam			}
586173273Ssam		}
587173273Ssam		KASSERT(n == 0, ("%s: lost %d frames, qframes %d off %d "
588173273Ssam		    "BA win <%d:%d> winstart %d",
589173273Ssam		    __func__, n, rap->rxa_qframes, i, rap->rxa_start,
590173273Ssam		    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
591173273Ssam		    winstart));
592178354Ssam		vap->iv_stats.is_ampdu_rx_copy += rap->rxa_qframes;
593173273Ssam	}
594173273Ssam	/*
595173273Ssam	 * Move the start of the BA window; we use the
596173273Ssam	 * sequence number of the last MSDU that was
597173273Ssam	 * passed up the stack+1 or winstart if stopped on
598173273Ssam	 * a gap in the reorder buffer.
599173273Ssam	 */
600173273Ssam	rap->rxa_start = seqno;
601173273Ssam}
602173273Ssam
603173273Ssam/*
604170530Ssam * Process a received QoS data frame for an HT station.  Handle
605170530Ssam * A-MPDU reordering: if this frame is received out of order
606170530Ssam * and falls within the BA window hold onto it.  Otherwise if
607173273Ssam * this frame completes a run, flush any pending frames.  We
608170530Ssam * return 1 if the frame is consumed.  A 0 is returned if
609170530Ssam * the frame should be processed normally by the caller.
610170530Ssam */
611170530Ssamint
612170530Ssamieee80211_ampdu_reorder(struct ieee80211_node *ni, struct mbuf *m)
613170530Ssam{
614170530Ssam#define	IEEE80211_FC0_QOSDATA \
615170530Ssam	(IEEE80211_FC0_TYPE_DATA|IEEE80211_FC0_SUBTYPE_QOS|IEEE80211_FC0_VERSION_0)
616173273Ssam#define	PROCESS		0	/* caller should process frame */
617173273Ssam#define	CONSUMED	1	/* frame consumed, caller does nothing */
618178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
619170530Ssam	struct ieee80211_qosframe *wh;
620170530Ssam	struct ieee80211_rx_ampdu *rap;
621170530Ssam	ieee80211_seq rxseq;
622170530Ssam	uint8_t tid;
623170530Ssam	int off;
624170530Ssam
625183247Ssam	KASSERT((m->m_flags & (M_AMPDU | M_AMPDU_MPDU)) == M_AMPDU,
626183247Ssam	    ("!a-mpdu or already re-ordered, flags 0x%x", m->m_flags));
627170530Ssam	KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT sta"));
628170530Ssam
629170530Ssam	/* NB: m_len known to be sufficient */
630170530Ssam	wh = mtod(m, struct ieee80211_qosframe *);
631183247Ssam	if (wh->i_fc[0] != IEEE80211_FC0_QOSDATA) {
632183247Ssam		/*
633183247Ssam		 * Not QoS data, shouldn't get here but just
634183247Ssam		 * return it to the caller for processing.
635183247Ssam		 */
636183247Ssam		return PROCESS;
637183247Ssam	}
638193840Ssam	if (IEEE80211_IS_DSTODS(wh))
639173273Ssam		tid = ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0];
640173273Ssam	else
641173273Ssam		tid = wh->i_qos[0];
642173273Ssam	tid &= IEEE80211_QOS_TID;
643170530Ssam	rap = &ni->ni_rx_ampdu[tid];
644170530Ssam	if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
645170530Ssam		/*
646170530Ssam		 * No ADDBA request yet, don't touch.
647170530Ssam		 */
648173273Ssam		return PROCESS;
649170530Ssam	}
650182827Ssam	rxseq = le16toh(*(uint16_t *)wh->i_seq);
651182827Ssam	if ((rxseq & IEEE80211_SEQ_FRAG_MASK) != 0) {
652182827Ssam		/*
653182827Ssam		 * Fragments are not allowed; toss.
654182827Ssam		 */
655182827Ssam		IEEE80211_DISCARD_MAC(vap,
656182827Ssam		    IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
657182827Ssam		    "A-MPDU", "fragment, rxseq 0x%x tid %u%s", rxseq, tid,
658182827Ssam		    wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
659182827Ssam		vap->iv_stats.is_ampdu_rx_drop++;
660182827Ssam		IEEE80211_NODE_STAT(ni, rx_drop);
661182827Ssam		m_freem(m);
662182827Ssam		return CONSUMED;
663182827Ssam	}
664182827Ssam	rxseq >>= IEEE80211_SEQ_SEQ_SHIFT;
665173273Ssam	rap->rxa_nframes++;
666173273Ssamagain:
667170530Ssam	if (rxseq == rap->rxa_start) {
668170530Ssam		/*
669170530Ssam		 * First frame in window.
670170530Ssam		 */
671170530Ssam		if (rap->rxa_qframes != 0) {
672170530Ssam			/*
673170530Ssam			 * Dispatch as many packets as we can.
674170530Ssam			 */
675170530Ssam			KASSERT(rap->rxa_m[0] == NULL, ("unexpected dup"));
676170530Ssam			ampdu_dispatch(ni, m);
677170530Ssam			ampdu_rx_dispatch(rap, ni);
678173273Ssam			return CONSUMED;
679170530Ssam		} else {
680170530Ssam			/*
681170530Ssam			 * In order; advance window and notify
682170530Ssam			 * caller to dispatch directly.
683170530Ssam			 */
684170530Ssam			rap->rxa_start = IEEE80211_SEQ_INC(rxseq);
685173273Ssam			return PROCESS;
686170530Ssam		}
687170530Ssam	}
688170530Ssam	/*
689173273Ssam	 * Frame is out of order; store if in the BA window.
690170530Ssam	 */
691170530Ssam	/* calculate offset in BA window */
692170530Ssam	off = IEEE80211_SEQ_SUB(rxseq, rap->rxa_start);
693173273Ssam	if (off < rap->rxa_wnd) {
694170530Ssam		/*
695173273Ssam		 * Common case (hopefully): in the BA window.
696205277Srpaulo		 * Sec 9.10.7.6.2 a) (p.137)
697173273Ssam		 */
698173273Ssam#ifdef IEEE80211_AMPDU_AGE
699173273Ssam		/*
700173273Ssam		 * Check for frames sitting too long in the reorder queue.
701173273Ssam		 * This should only ever happen if frames are not delivered
702173273Ssam		 * without the sender otherwise notifying us (e.g. with a
703173273Ssam		 * BAR to move the window).  Typically this happens because
704173273Ssam		 * of vendor bugs that cause the sequence number to jump.
705173273Ssam		 * When this happens we get a gap in the reorder queue that
706173273Ssam		 * leaves frame sitting on the queue until they get pushed
707173273Ssam		 * out due to window moves.  When the vendor does not send
708173273Ssam		 * BAR this move only happens due to explicit packet sends
709170530Ssam		 *
710173273Ssam		 * NB: we only track the time of the oldest frame in the
711173273Ssam		 * reorder q; this means that if we flush we might push
712173273Ssam		 * frames that still "new"; if this happens then subsequent
713173273Ssam		 * frames will result in BA window moves which cost something
714173273Ssam		 * but is still better than a big throughput dip.
715170530Ssam		 */
716173273Ssam		if (rap->rxa_qframes != 0) {
717173273Ssam			/* XXX honor batimeout? */
718173273Ssam			if (ticks - rap->rxa_age > ieee80211_ampdu_age) {
719173273Ssam				/*
720173273Ssam				 * Too long since we received the first
721173273Ssam				 * frame; flush the reorder buffer.
722173273Ssam				 */
723173273Ssam				if (rap->rxa_qframes != 0) {
724178354Ssam					vap->iv_stats.is_ampdu_rx_age +=
725173273Ssam					    rap->rxa_qframes;
726173273Ssam					ampdu_rx_flush(ni, rap);
727173273Ssam				}
728173273Ssam				rap->rxa_start = IEEE80211_SEQ_INC(rxseq);
729173273Ssam				return PROCESS;
730173273Ssam			}
731173273Ssam		} else {
732173273Ssam			/*
733173273Ssam			 * First frame, start aging timer.
734173273Ssam			 */
735173273Ssam			rap->rxa_age = ticks;
736173273Ssam		}
737173273Ssam#endif /* IEEE80211_AMPDU_AGE */
738173273Ssam		/* save packet */
739173273Ssam		if (rap->rxa_m[off] == NULL) {
740173273Ssam			rap->rxa_m[off] = m;
741173273Ssam			rap->rxa_qframes++;
742173273Ssam			rap->rxa_qbytes += m->m_pkthdr.len;
743178354Ssam			vap->iv_stats.is_ampdu_rx_reorder++;
744173273Ssam		} else {
745178354Ssam			IEEE80211_DISCARD_MAC(vap,
746173273Ssam			    IEEE80211_MSG_INPUT | IEEE80211_MSG_11N,
747173273Ssam			    ni->ni_macaddr, "a-mpdu duplicate",
748173273Ssam			    "seqno %u tid %u BA win <%u:%u>",
749173273Ssam			    rxseq, tid, rap->rxa_start,
750173273Ssam			    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1));
751178354Ssam			vap->iv_stats.is_rx_dup++;
752173273Ssam			IEEE80211_NODE_STAT(ni, rx_dup);
753173273Ssam			m_freem(m);
754173273Ssam		}
755173273Ssam		return CONSUMED;
756173273Ssam	}
757173273Ssam	if (off < IEEE80211_SEQ_BA_RANGE) {
758173273Ssam		/*
759173273Ssam		 * Outside the BA window, but within range;
760173273Ssam		 * flush the reorder q and move the window.
761205277Srpaulo		 * Sec 9.10.7.6.2 b) (p.138)
762173273Ssam		 */
763178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
764173273Ssam		    "move BA win <%u:%u> (%u frames) rxseq %u tid %u",
765170530Ssam		    rap->rxa_start,
766173273Ssam		    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
767170530Ssam		    rap->rxa_qframes, rxseq, tid);
768178354Ssam		vap->iv_stats.is_ampdu_rx_move++;
769170530Ssam
770173273Ssam		/*
771173273Ssam		 * The spec says to flush frames up to but not including:
772173273Ssam		 * 	WinStart_B = rxseq - rap->rxa_wnd + 1
773173273Ssam		 * Then insert the frame or notify the caller to process
774173273Ssam		 * it immediately.  We can safely do this by just starting
775173273Ssam		 * over again because we know the frame will now be within
776173273Ssam		 * the BA window.
777173273Ssam		 */
778173273Ssam		/* NB: rxa_wnd known to be >0 */
779173273Ssam		ampdu_rx_flush_upto(ni, rap,
780173273Ssam		    IEEE80211_SEQ_SUB(rxseq, rap->rxa_wnd-1));
781173273Ssam		goto again;
782170530Ssam	} else {
783170530Ssam		/*
784173273Ssam		 * Outside the BA window and out of range; toss.
785205277Srpaulo		 * Sec 9.10.7.6.2 c) (p.138)
786170530Ssam		 */
787178354Ssam		IEEE80211_DISCARD_MAC(vap,
788173273Ssam		    IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
789178354Ssam		    "MPDU", "BA win <%u:%u> (%u frames) rxseq %u tid %u%s",
790173273Ssam		    rap->rxa_start,
791173273Ssam		    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
792173273Ssam		    rap->rxa_qframes, rxseq, tid,
793173273Ssam		    wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
794178354Ssam		vap->iv_stats.is_ampdu_rx_drop++;
795173273Ssam		IEEE80211_NODE_STAT(ni, rx_drop);
796170530Ssam		m_freem(m);
797173273Ssam		return CONSUMED;
798170530Ssam	}
799173273Ssam#undef CONSUMED
800173273Ssam#undef PROCESS
801170530Ssam#undef IEEE80211_FC0_QOSDATA
802170530Ssam}
803170530Ssam
804170530Ssam/*
805170530Ssam * Process a BAR ctl frame.  Dispatch all frames up to
806170530Ssam * the sequence number of the frame.  If this frame is
807173273Ssam * out of range it's discarded.
808170530Ssam */
809170530Ssamvoid
810170530Ssamieee80211_recv_bar(struct ieee80211_node *ni, struct mbuf *m0)
811170530Ssam{
812178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
813170530Ssam	struct ieee80211_frame_bar *wh;
814170530Ssam	struct ieee80211_rx_ampdu *rap;
815170530Ssam	ieee80211_seq rxseq;
816170530Ssam	int tid, off;
817170530Ssam
818173273Ssam	if (!ieee80211_recv_bar_ena) {
819173273Ssam#if 0
820178354Ssam		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_11N,
821173273Ssam		    ni->ni_macaddr, "BAR", "%s", "processing disabled");
822173273Ssam#endif
823178354Ssam		vap->iv_stats.is_ampdu_bar_bad++;
824173273Ssam		return;
825173273Ssam	}
826170530Ssam	wh = mtod(m0, struct ieee80211_frame_bar *);
827170530Ssam	/* XXX check basic BAR */
828170530Ssam	tid = MS(le16toh(wh->i_ctl), IEEE80211_BAR_TID);
829170530Ssam	rap = &ni->ni_rx_ampdu[tid];
830170530Ssam	if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
831170530Ssam		/*
832170530Ssam		 * No ADDBA request yet, don't touch.
833170530Ssam		 */
834178354Ssam		IEEE80211_DISCARD_MAC(vap,
835170530Ssam		    IEEE80211_MSG_INPUT | IEEE80211_MSG_11N,
836170530Ssam		    ni->ni_macaddr, "BAR", "no BA stream, tid %u", tid);
837178354Ssam		vap->iv_stats.is_ampdu_bar_bad++;
838170530Ssam		return;
839170530Ssam	}
840178354Ssam	vap->iv_stats.is_ampdu_bar_rx++;
841170530Ssam	rxseq = le16toh(wh->i_seq) >> IEEE80211_SEQ_SEQ_SHIFT;
842173273Ssam	if (rxseq == rap->rxa_start)
843173273Ssam		return;
844170530Ssam	/* calculate offset in BA window */
845170530Ssam	off = IEEE80211_SEQ_SUB(rxseq, rap->rxa_start);
846173273Ssam	if (off < IEEE80211_SEQ_BA_RANGE) {
847170530Ssam		/*
848173273Ssam		 * Flush the reorder q up to rxseq and move the window.
849205277Srpaulo		 * Sec 9.10.7.6.3 a) (p.138)
850170530Ssam		 */
851178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
852173273Ssam		    "BAR moves BA win <%u:%u> (%u frames) rxseq %u tid %u",
853170530Ssam		    rap->rxa_start,
854173273Ssam		    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
855173273Ssam		    rap->rxa_qframes, rxseq, tid);
856178354Ssam		vap->iv_stats.is_ampdu_bar_move++;
857173273Ssam
858173273Ssam		ampdu_rx_flush_upto(ni, rap, rxseq);
859173273Ssam		if (off >= rap->rxa_wnd) {
860173273Ssam			/*
861173273Ssam			 * BAR specifies a window start to the right of BA
862173273Ssam			 * window; we must move it explicitly since
863173273Ssam			 * ampdu_rx_flush_upto will not.
864173273Ssam			 */
865173273Ssam			rap->rxa_start = rxseq;
866170530Ssam		}
867173273Ssam	} else {
868170530Ssam		/*
869173273Ssam		 * Out of range; toss.
870205277Srpaulo		 * Sec 9.10.7.6.3 b) (p.138)
871170530Ssam		 */
872178354Ssam		IEEE80211_DISCARD_MAC(vap,
873173273Ssam		    IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
874173273Ssam		    "BAR", "BA win <%u:%u> (%u frames) rxseq %u tid %u%s",
875173273Ssam		    rap->rxa_start,
876173273Ssam		    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
877173273Ssam		    rap->rxa_qframes, rxseq, tid,
878173273Ssam		    wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
879178354Ssam		vap->iv_stats.is_ampdu_bar_oow++;
880173273Ssam		IEEE80211_NODE_STAT(ni, rx_drop);
881170530Ssam	}
882170530Ssam}
883170530Ssam
884170530Ssam/*
885170530Ssam * Setup HT-specific state in a node.  Called only
886170530Ssam * when HT use is negotiated so we don't do extra
887170530Ssam * work for temporary and/or legacy sta's.
888170530Ssam */
889170530Ssamvoid
890183254Ssamieee80211_ht_node_init(struct ieee80211_node *ni)
891170530Ssam{
892170530Ssam	struct ieee80211_tx_ampdu *tap;
893170530Ssam	int ac;
894170530Ssam
895173273Ssam	if (ni->ni_flags & IEEE80211_NODE_HT) {
896173273Ssam		/*
897173273Ssam		 * Clean AMPDU state on re-associate.  This handles the case
898173273Ssam		 * where a station leaves w/o notifying us and then returns
899173273Ssam		 * before node is reaped for inactivity.
900173273Ssam		 */
901173273Ssam		ieee80211_ht_node_cleanup(ni);
902173273Ssam	}
903170530Ssam	for (ac = 0; ac < WME_NUM_AC; ac++) {
904170530Ssam		tap = &ni->ni_tx_ampdu[ac];
905170530Ssam		tap->txa_ac = ac;
906184280Ssam		tap->txa_ni = ni;
907173273Ssam		/* NB: further initialization deferred */
908170530Ssam	}
909173273Ssam	ni->ni_flags |= IEEE80211_NODE_HT | IEEE80211_NODE_AMPDU;
910170530Ssam}
911170530Ssam
912170530Ssam/*
913170530Ssam * Cleanup HT-specific state in a node.  Called only
914170530Ssam * when HT use has been marked.
915170530Ssam */
916170530Ssamvoid
917170530Ssamieee80211_ht_node_cleanup(struct ieee80211_node *ni)
918170530Ssam{
919191552Ssam	struct ieee80211com *ic = ni->ni_ic;
920170530Ssam	int i;
921170530Ssam
922170530Ssam	KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT node"));
923170530Ssam
924170530Ssam	/* XXX optimize this */
925170530Ssam	for (i = 0; i < WME_NUM_AC; i++) {
926170530Ssam		struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[i];
927184280Ssam		if (tap->txa_flags & IEEE80211_AGGR_SETUP)
928184280Ssam			ampdu_tx_stop(tap);
929170530Ssam	}
930170530Ssam	for (i = 0; i < WME_NUM_TID; i++)
931191552Ssam		ic->ic_ampdu_rx_stop(ni, &ni->ni_rx_ampdu[i]);
932170530Ssam
933170530Ssam	ni->ni_htcap = 0;
934182828Ssam	ni->ni_flags &= ~IEEE80211_NODE_HT_ALL;
935170530Ssam}
936170530Ssam
937178354Ssam/*
938178354Ssam * Age out HT resources for a station.
939178354Ssam */
940178354Ssamvoid
941178354Ssamieee80211_ht_node_age(struct ieee80211_node *ni)
942178354Ssam{
943178354Ssam#ifdef IEEE80211_AMPDU_AGE
944178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
945178354Ssam	uint8_t tid;
946178354Ssam#endif
947178354Ssam
948178354Ssam	KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT sta"));
949178354Ssam
950178354Ssam#ifdef IEEE80211_AMPDU_AGE
951178354Ssam	for (tid = 0; tid < WME_NUM_TID; tid++) {
952178354Ssam		struct ieee80211_rx_ampdu *rap;
953178354Ssam
954178354Ssam		rap = &ni->ni_rx_ampdu[tid];
955178354Ssam		if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0)
956178354Ssam			continue;
957178354Ssam		if (rap->rxa_qframes == 0)
958178354Ssam			continue;
959178354Ssam		/*
960178354Ssam		 * Check for frames sitting too long in the reorder queue.
961178354Ssam		 * See above for more details on what's happening here.
962178354Ssam		 */
963178354Ssam		/* XXX honor batimeout? */
964178354Ssam		if (ticks - rap->rxa_age > ieee80211_ampdu_age) {
965178354Ssam			/*
966178354Ssam			 * Too long since we received the first
967178354Ssam			 * frame; flush the reorder buffer.
968178354Ssam			 */
969178354Ssam			vap->iv_stats.is_ampdu_rx_age += rap->rxa_qframes;
970178354Ssam			ampdu_rx_flush(ni, rap);
971178354Ssam		}
972178354Ssam	}
973178354Ssam#endif /* IEEE80211_AMPDU_AGE */
974178354Ssam}
975178354Ssam
976173273Ssamstatic struct ieee80211_channel *
977173273Ssamfindhtchan(struct ieee80211com *ic, struct ieee80211_channel *c, int htflags)
978173273Ssam{
979173273Ssam	return ieee80211_find_channel(ic, c->ic_freq,
980173273Ssam	    (c->ic_flags &~ IEEE80211_CHAN_HT) | htflags);
981173273Ssam}
982173273Ssam
983173273Ssam/*
984173273Ssam * Adjust a channel to be HT/non-HT according to the vap's configuration.
985173273Ssam */
986173273Ssamstruct ieee80211_channel *
987173273Ssamieee80211_ht_adjust_channel(struct ieee80211com *ic,
988173273Ssam	struct ieee80211_channel *chan, int flags)
989173273Ssam{
990173273Ssam	struct ieee80211_channel *c;
991173273Ssam
992193655Ssam	if (flags & IEEE80211_FHT_HT) {
993173273Ssam		/* promote to HT if possible */
994193655Ssam		if (flags & IEEE80211_FHT_USEHT40) {
995173273Ssam			if (!IEEE80211_IS_CHAN_HT40(chan)) {
996173273Ssam				/* NB: arbitrarily pick ht40+ over ht40- */
997173273Ssam				c = findhtchan(ic, chan, IEEE80211_CHAN_HT40U);
998173273Ssam				if (c == NULL)
999173273Ssam					c = findhtchan(ic, chan,
1000173273Ssam						IEEE80211_CHAN_HT40D);
1001173273Ssam				if (c == NULL)
1002173273Ssam					c = findhtchan(ic, chan,
1003173273Ssam						IEEE80211_CHAN_HT20);
1004173273Ssam				if (c != NULL)
1005173273Ssam					chan = c;
1006173273Ssam			}
1007173273Ssam		} else if (!IEEE80211_IS_CHAN_HT20(chan)) {
1008173273Ssam			c = findhtchan(ic, chan, IEEE80211_CHAN_HT20);
1009173273Ssam			if (c != NULL)
1010173273Ssam				chan = c;
1011173273Ssam		}
1012173273Ssam	} else if (IEEE80211_IS_CHAN_HT(chan)) {
1013173273Ssam		/* demote to legacy, HT use is disabled */
1014173273Ssam		c = ieee80211_find_channel(ic, chan->ic_freq,
1015173273Ssam		    chan->ic_flags &~ IEEE80211_CHAN_HT);
1016173273Ssam		if (c != NULL)
1017173273Ssam			chan = c;
1018173273Ssam	}
1019173273Ssam	return chan;
1020173273Ssam}
1021173273Ssam
1022173273Ssam/*
1023173273Ssam * Setup HT-specific state for a legacy WDS peer.
1024173273Ssam */
1025173273Ssamvoid
1026173273Ssamieee80211_ht_wds_init(struct ieee80211_node *ni)
1027173273Ssam{
1028178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1029173273Ssam	struct ieee80211_tx_ampdu *tap;
1030173273Ssam	int ac;
1031173273Ssam
1032193655Ssam	KASSERT(vap->iv_flags_ht & IEEE80211_FHT_HT, ("no HT requested"));
1033173273Ssam
1034173273Ssam	/* XXX check scan cache in case peer has an ap and we have info */
1035173273Ssam	/*
1036173273Ssam	 * If setup with a legacy channel; locate an HT channel.
1037173273Ssam	 * Otherwise if the inherited channel (from a companion
1038173273Ssam	 * AP) is suitable use it so we use the same location
1039173273Ssam	 * for the extension channel).
1040173273Ssam	 */
1041178354Ssam	ni->ni_chan = ieee80211_ht_adjust_channel(ni->ni_ic,
1042178354Ssam	    ni->ni_chan, ieee80211_htchanflags(ni->ni_chan));
1043173273Ssam
1044173273Ssam	ni->ni_htcap = 0;
1045193655Ssam	if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20)
1046173273Ssam		ni->ni_htcap |= IEEE80211_HTCAP_SHORTGI20;
1047173273Ssam	if (IEEE80211_IS_CHAN_HT40(ni->ni_chan)) {
1048173273Ssam		ni->ni_htcap |= IEEE80211_HTCAP_CHWIDTH40;
1049173273Ssam		ni->ni_chw = 40;
1050173273Ssam		if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan))
1051173273Ssam			ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_ABOVE;
1052173273Ssam		else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan))
1053173273Ssam			ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_BELOW;
1054193655Ssam		if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40)
1055173273Ssam			ni->ni_htcap |= IEEE80211_HTCAP_SHORTGI40;
1056173273Ssam	} else {
1057173273Ssam		ni->ni_chw = 20;
1058173273Ssam		ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_NONE;
1059173273Ssam	}
1060173273Ssam	ni->ni_htctlchan = ni->ni_chan->ic_ieee;
1061193655Ssam	if (vap->iv_flags_ht & IEEE80211_FHT_RIFS)
1062183256Ssam		ni->ni_flags |= IEEE80211_NODE_RIFS;
1063183256Ssam	/* XXX does it make sense to enable SMPS? */
1064173273Ssam
1065173273Ssam	ni->ni_htopmode = 0;		/* XXX need protection state */
1066173273Ssam	ni->ni_htstbc = 0;		/* XXX need info */
1067173273Ssam
1068173273Ssam	for (ac = 0; ac < WME_NUM_AC; ac++) {
1069173273Ssam		tap = &ni->ni_tx_ampdu[ac];
1070173273Ssam		tap->txa_ac = ac;
1071173273Ssam	}
1072193655Ssam	/* NB: AMPDU tx/rx governed by IEEE80211_FHT_AMPDU_{TX,RX} */
1073173273Ssam	ni->ni_flags |= IEEE80211_NODE_HT | IEEE80211_NODE_AMPDU;
1074173273Ssam}
1075173273Ssam
1076173273Ssam/*
1077173273Ssam * Notify hostap vaps of a change in the HTINFO ie.
1078173273Ssam */
1079173273Ssamstatic void
1080173273Ssamhtinfo_notify(struct ieee80211com *ic)
1081173273Ssam{
1082178354Ssam	struct ieee80211vap *vap;
1083178354Ssam	int first = 1;
1084178354Ssam
1085178354Ssam	IEEE80211_LOCK_ASSERT(ic);
1086178354Ssam
1087178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1088178354Ssam		if (vap->iv_opmode != IEEE80211_M_HOSTAP)
1089178354Ssam			continue;
1090183253Ssam		if (vap->iv_state != IEEE80211_S_RUN ||
1091183253Ssam		    !IEEE80211_IS_CHAN_HT(vap->iv_bss->ni_chan))
1092183253Ssam			continue;
1093178354Ssam		if (first) {
1094178354Ssam			IEEE80211_NOTE(vap,
1095178354Ssam			    IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N,
1096178354Ssam			    vap->iv_bss,
1097178354Ssam			    "HT bss occupancy change: %d sta, %d ht, "
1098178354Ssam			    "%d ht40%s, HT protmode now 0x%x"
1099178354Ssam			    , ic->ic_sta_assoc
1100178354Ssam			    , ic->ic_ht_sta_assoc
1101178354Ssam			    , ic->ic_ht40_sta_assoc
1102193655Ssam			    , (ic->ic_flags_ht & IEEE80211_FHT_NONHT_PR) ?
1103178354Ssam				 ", non-HT sta present" : ""
1104178354Ssam			    , ic->ic_curhtprotmode);
1105178354Ssam			first = 0;
1106178354Ssam		}
1107178354Ssam		ieee80211_beacon_notify(vap, IEEE80211_BEACON_HTINFO);
1108178354Ssam	}
1109173273Ssam}
1110173273Ssam
1111173273Ssam/*
1112173273Ssam * Calculate HT protection mode from current
1113173273Ssam * state and handle updates.
1114173273Ssam */
1115173273Ssamstatic void
1116173273Ssamhtinfo_update(struct ieee80211com *ic)
1117173273Ssam{
1118173273Ssam	uint8_t protmode;
1119173273Ssam
1120178354Ssam	if (ic->ic_sta_assoc != ic->ic_ht_sta_assoc) {
1121178354Ssam		protmode = IEEE80211_HTINFO_OPMODE_MIXED
1122178354Ssam			 | IEEE80211_HTINFO_NONHT_PRESENT;
1123193655Ssam	} else if (ic->ic_flags_ht & IEEE80211_FHT_NONHT_PR) {
1124173273Ssam		protmode = IEEE80211_HTINFO_OPMODE_PROTOPT
1125178354Ssam			 | IEEE80211_HTINFO_NONHT_PRESENT;
1126178354Ssam	} else if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
1127178354Ssam	    IEEE80211_IS_CHAN_HT40(ic->ic_bsschan) &&
1128173273Ssam	    ic->ic_sta_assoc != ic->ic_ht40_sta_assoc) {
1129173273Ssam		protmode = IEEE80211_HTINFO_OPMODE_HT20PR;
1130173273Ssam	} else {
1131173273Ssam		protmode = IEEE80211_HTINFO_OPMODE_PURE;
1132173273Ssam	}
1133173273Ssam	if (protmode != ic->ic_curhtprotmode) {
1134173273Ssam		ic->ic_curhtprotmode = protmode;
1135173273Ssam		htinfo_notify(ic);
1136173273Ssam	}
1137173273Ssam}
1138173273Ssam
1139173273Ssam/*
1140173273Ssam * Handle an HT station joining a BSS.
1141173273Ssam */
1142173273Ssamvoid
1143173273Ssamieee80211_ht_node_join(struct ieee80211_node *ni)
1144173273Ssam{
1145173273Ssam	struct ieee80211com *ic = ni->ni_ic;
1146173273Ssam
1147173273Ssam	IEEE80211_LOCK_ASSERT(ic);
1148173273Ssam
1149173273Ssam	if (ni->ni_flags & IEEE80211_NODE_HT) {
1150173273Ssam		ic->ic_ht_sta_assoc++;
1151173273Ssam		if (ni->ni_chw == 40)
1152173273Ssam			ic->ic_ht40_sta_assoc++;
1153173273Ssam	}
1154173273Ssam	htinfo_update(ic);
1155173273Ssam}
1156173273Ssam
1157173273Ssam/*
1158173273Ssam * Handle an HT station leaving a BSS.
1159173273Ssam */
1160173273Ssamvoid
1161173273Ssamieee80211_ht_node_leave(struct ieee80211_node *ni)
1162173273Ssam{
1163173273Ssam	struct ieee80211com *ic = ni->ni_ic;
1164173273Ssam
1165173273Ssam	IEEE80211_LOCK_ASSERT(ic);
1166173273Ssam
1167173273Ssam	if (ni->ni_flags & IEEE80211_NODE_HT) {
1168173273Ssam		ic->ic_ht_sta_assoc--;
1169173273Ssam		if (ni->ni_chw == 40)
1170173273Ssam			ic->ic_ht40_sta_assoc--;
1171173273Ssam	}
1172173273Ssam	htinfo_update(ic);
1173173273Ssam}
1174173273Ssam
1175173273Ssam/*
1176173273Ssam * Public version of htinfo_update; used for processing
1177178354Ssam * beacon frames from overlapping bss.
1178178354Ssam *
1179178354Ssam * Caller can specify either IEEE80211_HTINFO_OPMODE_MIXED
1180178354Ssam * (on receipt of a beacon that advertises MIXED) or
1181178354Ssam * IEEE80211_HTINFO_OPMODE_PROTOPT (on receipt of a beacon
1182178354Ssam * from an overlapping legacy bss).  We treat MIXED with
1183178354Ssam * a higher precedence than PROTOPT (i.e. we will not change
1184178354Ssam * change PROTOPT -> MIXED; only MIXED -> PROTOPT).  This
1185178354Ssam * corresponds to how we handle things in htinfo_update.
1186173273Ssam */
1187173273Ssamvoid
1188178354Ssamieee80211_htprot_update(struct ieee80211com *ic, int protmode)
1189173273Ssam{
1190178354Ssam#define	OPMODE(x)	SM(x, IEEE80211_HTINFO_OPMODE)
1191183246Ssam	IEEE80211_LOCK(ic);
1192178354Ssam
1193178354Ssam	/* track non-HT station presence */
1194178354Ssam	KASSERT(protmode & IEEE80211_HTINFO_NONHT_PRESENT,
1195183246Ssam	    ("protmode 0x%x", protmode));
1196193655Ssam	ic->ic_flags_ht |= IEEE80211_FHT_NONHT_PR;
1197178354Ssam	ic->ic_lastnonht = ticks;
1198178354Ssam
1199183246Ssam	if (protmode != ic->ic_curhtprotmode &&
1200183246Ssam	    (OPMODE(ic->ic_curhtprotmode) != IEEE80211_HTINFO_OPMODE_MIXED ||
1201183246Ssam	     OPMODE(protmode) == IEEE80211_HTINFO_OPMODE_PROTOPT)) {
1202183246Ssam		/* push beacon update */
1203183246Ssam		ic->ic_curhtprotmode = protmode;
1204183246Ssam		htinfo_notify(ic);
1205183246Ssam	}
1206181197Ssam	IEEE80211_UNLOCK(ic);
1207178354Ssam#undef OPMODE
1208173273Ssam}
1209173273Ssam
1210173273Ssam/*
1211173273Ssam * Time out presence of an overlapping bss with non-HT
1212173273Ssam * stations.  When operating in hostap mode we listen for
1213173273Ssam * beacons from other stations and if we identify a non-HT
1214173273Ssam * station is present we update the opmode field of the
1215173273Ssam * HTINFO ie.  To identify when all non-HT stations are
1216173273Ssam * gone we time out this condition.
1217173273Ssam */
1218173273Ssamvoid
1219173273Ssamieee80211_ht_timeout(struct ieee80211com *ic)
1220173273Ssam{
1221173273Ssam	IEEE80211_LOCK_ASSERT(ic);
1222173273Ssam
1223193655Ssam	if ((ic->ic_flags_ht & IEEE80211_FHT_NONHT_PR) &&
1224173273Ssam	    time_after(ticks, ic->ic_lastnonht + IEEE80211_NONHT_PRESENT_AGE)) {
1225173273Ssam#if 0
1226178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
1227173273Ssam		    "%s", "time out non-HT STA present on channel");
1228173273Ssam#endif
1229193655Ssam		ic->ic_flags_ht &= ~IEEE80211_FHT_NONHT_PR;
1230173273Ssam		htinfo_update(ic);
1231173273Ssam	}
1232173273Ssam}
1233173273Ssam
1234170530Ssam/* unalligned little endian access */
1235170530Ssam#define LE_READ_2(p)					\
1236170530Ssam	((uint16_t)					\
1237170530Ssam	 ((((const uint8_t *)(p))[0]      ) |		\
1238170530Ssam	  (((const uint8_t *)(p))[1] <<  8)))
1239170530Ssam
1240170530Ssam/*
1241170530Ssam * Process an 802.11n HT capabilities ie.
1242170530Ssam */
1243170530Ssamvoid
1244170530Ssamieee80211_parse_htcap(struct ieee80211_node *ni, const uint8_t *ie)
1245170530Ssam{
1246170530Ssam	if (ie[0] == IEEE80211_ELEMID_VENDOR) {
1247170530Ssam		/*
1248170530Ssam		 * Station used Vendor OUI ie to associate;
1249170530Ssam		 * mark the node so when we respond we'll use
1250170530Ssam		 * the Vendor OUI's and not the standard ie's.
1251170530Ssam		 */
1252170530Ssam		ni->ni_flags |= IEEE80211_NODE_HTCOMPAT;
1253170530Ssam		ie += 4;
1254170530Ssam	} else
1255170530Ssam		ni->ni_flags &= ~IEEE80211_NODE_HTCOMPAT;
1256170530Ssam
1257170530Ssam	ni->ni_htcap = LE_READ_2(ie +
1258170530Ssam		__offsetof(struct ieee80211_ie_htcap, hc_cap));
1259170530Ssam	ni->ni_htparam = ie[__offsetof(struct ieee80211_ie_htcap, hc_param)];
1260170530Ssam}
1261170530Ssam
1262183254Ssamstatic void
1263183254Ssamhtinfo_parse(struct ieee80211_node *ni,
1264183254Ssam	const struct ieee80211_ie_htinfo *htinfo)
1265170530Ssam{
1266170530Ssam	uint16_t w;
1267170530Ssam
1268170530Ssam	ni->ni_htctlchan = htinfo->hi_ctrlchannel;
1269170530Ssam	ni->ni_ht2ndchan = SM(htinfo->hi_byte1, IEEE80211_HTINFO_2NDCHAN);
1270172055Ssam	w = LE_READ_2(&htinfo->hi_byte2);
1271170530Ssam	ni->ni_htopmode = SM(w, IEEE80211_HTINFO_OPMODE);
1272170530Ssam	w = LE_READ_2(&htinfo->hi_byte45);
1273170530Ssam	ni->ni_htstbc = SM(w, IEEE80211_HTINFO_BASIC_STBCMCS);
1274183254Ssam}
1275183254Ssam
1276183254Ssam/*
1277183254Ssam * Parse an 802.11n HT info ie and save useful information
1278183254Ssam * to the node state.  Note this does not effect any state
1279183254Ssam * changes such as for channel width change.
1280183254Ssam */
1281183254Ssamvoid
1282183254Ssamieee80211_parse_htinfo(struct ieee80211_node *ni, const uint8_t *ie)
1283183254Ssam{
1284183254Ssam	if (ie[0] == IEEE80211_ELEMID_VENDOR)
1285183254Ssam		ie += 4;
1286183254Ssam	htinfo_parse(ni, (const struct ieee80211_ie_htinfo *) ie);
1287183254Ssam}
1288183254Ssam
1289183254Ssam/*
1290183254Ssam * Handle 11n channel switch.  Use the received HT ie's to
1291183254Ssam * identify the right channel to use.  If we cannot locate it
1292183254Ssam * in the channel table then fallback to legacy operation.
1293183254Ssam * Note that we use this information to identify the node's
1294183254Ssam * channel only; the caller is responsible for insuring any
1295183254Ssam * required channel change is done (e.g. in sta mode when
1296183254Ssam * parsing the contents of a beacon frame).
1297183254Ssam */
1298183254Ssamstatic void
1299183254Ssamhtinfo_update_chw(struct ieee80211_node *ni, int htflags)
1300183254Ssam{
1301183254Ssam	struct ieee80211com *ic = ni->ni_ic;
1302183254Ssam	struct ieee80211_channel *c;
1303183254Ssam	int chanflags;
1304183254Ssam
1305173273Ssam	chanflags = (ni->ni_chan->ic_flags &~ IEEE80211_CHAN_HT) | htflags;
1306173273Ssam	if (chanflags != ni->ni_chan->ic_flags) {
1307183254Ssam		/* XXX not right for ht40- */
1308173273Ssam		c = ieee80211_find_channel(ic, ni->ni_chan->ic_freq, chanflags);
1309178354Ssam		if (c == NULL && (htflags & IEEE80211_CHAN_HT40)) {
1310173273Ssam			/*
1311173273Ssam			 * No HT40 channel entry in our table; fall back
1312173273Ssam			 * to HT20 operation.  This should not happen.
1313173273Ssam			 */
1314173273Ssam			c = findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT20);
1315183254Ssam#if 0
1316183254Ssam			IEEE80211_NOTE(ni->ni_vap,
1317173273Ssam			    IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
1318173273Ssam			    "no HT40 channel (freq %u), falling back to HT20",
1319173273Ssam			    ni->ni_chan->ic_freq);
1320183254Ssam#endif
1321173273Ssam			/* XXX stat */
1322173273Ssam		}
1323173273Ssam		if (c != NULL && c != ni->ni_chan) {
1324183254Ssam			IEEE80211_NOTE(ni->ni_vap,
1325173273Ssam			    IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
1326173273Ssam			    "switch station to HT%d channel %u/0x%x",
1327173273Ssam			    IEEE80211_IS_CHAN_HT40(c) ? 40 : 20,
1328173273Ssam			    c->ic_freq, c->ic_flags);
1329173273Ssam			ni->ni_chan = c;
1330173273Ssam		}
1331173273Ssam		/* NB: caller responsible for forcing any channel change */
1332173273Ssam	}
1333173273Ssam	/* update node's tx channel width */
1334173273Ssam	ni->ni_chw = IEEE80211_IS_CHAN_HT40(ni->ni_chan)? 40 : 20;
1335170530Ssam}
1336170530Ssam
1337170530Ssam/*
1338183255Ssam * Update 11n MIMO PS state according to received htcap.
1339183255Ssam */
1340183255Ssamstatic __inline int
1341183255Ssamhtcap_update_mimo_ps(struct ieee80211_node *ni)
1342183255Ssam{
1343183255Ssam	uint16_t oflags = ni->ni_flags;
1344183255Ssam
1345183255Ssam	switch (ni->ni_htcap & IEEE80211_HTCAP_SMPS) {
1346183255Ssam	case IEEE80211_HTCAP_SMPS_DYNAMIC:
1347183255Ssam		ni->ni_flags |= IEEE80211_NODE_MIMO_PS;
1348183255Ssam		ni->ni_flags |= IEEE80211_NODE_MIMO_RTS;
1349183255Ssam		break;
1350183255Ssam	case IEEE80211_HTCAP_SMPS_ENA:
1351183255Ssam		ni->ni_flags |= IEEE80211_NODE_MIMO_PS;
1352183255Ssam		ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS;
1353183255Ssam		break;
1354183255Ssam	case IEEE80211_HTCAP_SMPS_OFF:
1355183255Ssam	default:		/* disable on rx of reserved value */
1356183255Ssam		ni->ni_flags &= ~IEEE80211_NODE_MIMO_PS;
1357183255Ssam		ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS;
1358183255Ssam		break;
1359183255Ssam	}
1360183255Ssam	return (oflags ^ ni->ni_flags);
1361183255Ssam}
1362183255Ssam
1363183255Ssam/*
1364183257Ssam * Update short GI state according to received htcap
1365183257Ssam * and local settings.
1366183257Ssam */
1367183257Ssamstatic __inline void
1368183257Ssamhtcap_update_shortgi(struct ieee80211_node *ni)
1369183257Ssam{
1370183257Ssam	struct ieee80211vap *vap = ni->ni_vap;
1371183257Ssam
1372183257Ssam	ni->ni_flags &= ~(IEEE80211_NODE_SGI20|IEEE80211_NODE_SGI40);
1373183257Ssam	if ((ni->ni_htcap & IEEE80211_HTCAP_SHORTGI20) &&
1374193655Ssam	    (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20))
1375183257Ssam		ni->ni_flags |= IEEE80211_NODE_SGI20;
1376183257Ssam	if ((ni->ni_htcap & IEEE80211_HTCAP_SHORTGI40) &&
1377193655Ssam	    (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40))
1378183257Ssam		ni->ni_flags |= IEEE80211_NODE_SGI40;
1379183257Ssam}
1380183257Ssam
1381183257Ssam/*
1382183254Ssam * Parse and update HT-related state extracted from
1383183254Ssam * the HT cap and info ie's.
1384183254Ssam */
1385183254Ssamvoid
1386183254Ssamieee80211_ht_updateparams(struct ieee80211_node *ni,
1387183254Ssam	const uint8_t *htcapie, const uint8_t *htinfoie)
1388183254Ssam{
1389183254Ssam	struct ieee80211vap *vap = ni->ni_vap;
1390183254Ssam	const struct ieee80211_ie_htinfo *htinfo;
1391183254Ssam	int htflags;
1392183254Ssam
1393183254Ssam	ieee80211_parse_htcap(ni, htcapie);
1394183255Ssam	if (vap->iv_htcaps & IEEE80211_HTCAP_SMPS)
1395183255Ssam		htcap_update_mimo_ps(ni);
1396183257Ssam	htcap_update_shortgi(ni);
1397183254Ssam
1398183254Ssam	if (htinfoie[0] == IEEE80211_ELEMID_VENDOR)
1399183254Ssam		htinfoie += 4;
1400183254Ssam	htinfo = (const struct ieee80211_ie_htinfo *) htinfoie;
1401183254Ssam	htinfo_parse(ni, htinfo);
1402183254Ssam
1403193655Ssam	htflags = (vap->iv_flags_ht & IEEE80211_FHT_HT) ?
1404183254Ssam	    IEEE80211_CHAN_HT20 : 0;
1405183254Ssam	/* NB: honor operating mode constraint */
1406183254Ssam	if ((htinfo->hi_byte1 & IEEE80211_HTINFO_TXWIDTH_2040) &&
1407193655Ssam	    (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)) {
1408183254Ssam		if (ni->ni_ht2ndchan == IEEE80211_HTINFO_2NDCHAN_ABOVE)
1409183254Ssam			htflags = IEEE80211_CHAN_HT40U;
1410183254Ssam		else if (ni->ni_ht2ndchan == IEEE80211_HTINFO_2NDCHAN_BELOW)
1411183254Ssam			htflags = IEEE80211_CHAN_HT40D;
1412183254Ssam	}
1413183254Ssam	htinfo_update_chw(ni, htflags);
1414183256Ssam
1415183256Ssam	if ((htinfo->hi_byte1 & IEEE80211_HTINFO_RIFSMODE_PERM) &&
1416193655Ssam	    (vap->iv_flags_ht & IEEE80211_FHT_RIFS))
1417183256Ssam		ni->ni_flags |= IEEE80211_NODE_RIFS;
1418183256Ssam	else
1419183256Ssam		ni->ni_flags &= ~IEEE80211_NODE_RIFS;
1420183254Ssam}
1421183254Ssam
1422183254Ssam/*
1423183254Ssam * Parse and update HT-related state extracted from the HT cap ie
1424183254Ssam * for a station joining an HT BSS.
1425183254Ssam */
1426183254Ssamvoid
1427183254Ssamieee80211_ht_updatehtcap(struct ieee80211_node *ni, const uint8_t *htcapie)
1428183254Ssam{
1429183254Ssam	struct ieee80211vap *vap = ni->ni_vap;
1430183254Ssam	int htflags;
1431183254Ssam
1432183254Ssam	ieee80211_parse_htcap(ni, htcapie);
1433183255Ssam	if (vap->iv_htcaps & IEEE80211_HTCAP_SMPS)
1434183255Ssam		htcap_update_mimo_ps(ni);
1435183257Ssam	htcap_update_shortgi(ni);
1436183254Ssam
1437183254Ssam	/* NB: honor operating mode constraint */
1438183254Ssam	/* XXX 40 MHZ intolerant */
1439193655Ssam	htflags = (vap->iv_flags_ht & IEEE80211_FHT_HT) ?
1440183254Ssam	    IEEE80211_CHAN_HT20 : 0;
1441183254Ssam	if ((ni->ni_htcap & IEEE80211_HTCAP_CHWIDTH40) &&
1442193655Ssam	    (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)) {
1443183254Ssam		if (IEEE80211_IS_CHAN_HT40U(vap->iv_bss->ni_chan))
1444183254Ssam			htflags = IEEE80211_CHAN_HT40U;
1445183254Ssam		else if (IEEE80211_IS_CHAN_HT40D(vap->iv_bss->ni_chan))
1446183254Ssam			htflags = IEEE80211_CHAN_HT40D;
1447183254Ssam	}
1448183254Ssam	htinfo_update_chw(ni, htflags);
1449183254Ssam}
1450183254Ssam
1451183254Ssam/*
1452170530Ssam * Install received HT rate set by parsing the HT cap ie.
1453170530Ssam */
1454170530Ssamint
1455170530Ssamieee80211_setup_htrates(struct ieee80211_node *ni, const uint8_t *ie, int flags)
1456170530Ssam{
1457178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1458170530Ssam	const struct ieee80211_ie_htcap *htcap;
1459170530Ssam	struct ieee80211_htrateset *rs;
1460170530Ssam	int i;
1461170530Ssam
1462170530Ssam	rs = &ni->ni_htrates;
1463170530Ssam	memset(rs, 0, sizeof(*rs));
1464170530Ssam	if (ie != NULL) {
1465170530Ssam		if (ie[0] == IEEE80211_ELEMID_VENDOR)
1466170530Ssam			ie += 4;
1467170530Ssam		htcap = (const struct ieee80211_ie_htcap *) ie;
1468170530Ssam		for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++) {
1469170530Ssam			if (isclr(htcap->hc_mcsset, i))
1470170530Ssam				continue;
1471170530Ssam			if (rs->rs_nrates == IEEE80211_HTRATE_MAXSIZE) {
1472178354Ssam				IEEE80211_NOTE(vap,
1473170530Ssam				    IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
1474170530Ssam				    "WARNING, HT rate set too large; only "
1475170530Ssam				    "using %u rates", IEEE80211_HTRATE_MAXSIZE);
1476178354Ssam				vap->iv_stats.is_rx_rstoobig++;
1477170530Ssam				break;
1478170530Ssam			}
1479170530Ssam			rs->rs_rates[rs->rs_nrates++] = i;
1480170530Ssam		}
1481170530Ssam	}
1482170530Ssam	return ieee80211_fix_rate(ni, (struct ieee80211_rateset *) rs, flags);
1483170530Ssam}
1484170530Ssam
1485170530Ssam/*
1486170530Ssam * Mark rates in a node's HT rate set as basic according
1487170530Ssam * to the information in the supplied HT info ie.
1488170530Ssam */
1489170530Ssamvoid
1490170530Ssamieee80211_setup_basic_htrates(struct ieee80211_node *ni, const uint8_t *ie)
1491170530Ssam{
1492170530Ssam	const struct ieee80211_ie_htinfo *htinfo;
1493170530Ssam	struct ieee80211_htrateset *rs;
1494170530Ssam	int i, j;
1495170530Ssam
1496170530Ssam	if (ie[0] == IEEE80211_ELEMID_VENDOR)
1497170530Ssam		ie += 4;
1498170530Ssam	htinfo = (const struct ieee80211_ie_htinfo *) ie;
1499170530Ssam	rs = &ni->ni_htrates;
1500170530Ssam	if (rs->rs_nrates == 0) {
1501178354Ssam		IEEE80211_NOTE(ni->ni_vap,
1502170530Ssam		    IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
1503170530Ssam		    "%s", "WARNING, empty HT rate set");
1504170530Ssam		return;
1505170530Ssam	}
1506170530Ssam	for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++) {
1507170530Ssam		if (isclr(htinfo->hi_basicmcsset, i))
1508170530Ssam			continue;
1509170530Ssam		for (j = 0; j < rs->rs_nrates; j++)
1510170530Ssam			if ((rs->rs_rates[j] & IEEE80211_RATE_VAL) == i)
1511170530Ssam				rs->rs_rates[j] |= IEEE80211_RATE_BASIC;
1512170530Ssam	}
1513170530Ssam}
1514170530Ssam
1515170530Ssamstatic void
1516184280Ssamampdu_tx_setup(struct ieee80211_tx_ampdu *tap)
1517184280Ssam{
1518184280Ssam	callout_init(&tap->txa_timer, CALLOUT_MPSAFE);
1519184280Ssam	tap->txa_flags |= IEEE80211_AGGR_SETUP;
1520184280Ssam}
1521184280Ssam
1522184280Ssamstatic void
1523184280Ssamampdu_tx_stop(struct ieee80211_tx_ampdu *tap)
1524184280Ssam{
1525184280Ssam	struct ieee80211_node *ni = tap->txa_ni;
1526184280Ssam	struct ieee80211com *ic = ni->ni_ic;
1527184280Ssam
1528184280Ssam	KASSERT(tap->txa_flags & IEEE80211_AGGR_SETUP,
1529184280Ssam	    ("txa_flags 0x%x ac %d", tap->txa_flags, tap->txa_ac));
1530184280Ssam
1531184280Ssam	/*
1532184280Ssam	 * Stop BA stream if setup so driver has a chance
1533184280Ssam	 * to reclaim any resources it might have allocated.
1534184280Ssam	 */
1535184280Ssam	ic->ic_addba_stop(ni, tap);
1536184280Ssam	/*
1537184280Ssam	 * Stop any pending BAR transmit.
1538184280Ssam	 */
1539184280Ssam	bar_stop_timer(tap);
1540184280Ssam
1541184280Ssam	tap->txa_lastsample = 0;
1542184280Ssam	tap->txa_avgpps = 0;
1543184280Ssam	/* NB: clearing NAK means we may re-send ADDBA */
1544184280Ssam	tap->txa_flags &= ~(IEEE80211_AGGR_SETUP | IEEE80211_AGGR_NAK);
1545184280Ssam}
1546184280Ssam
1547184280Ssamstatic void
1548170530Ssamaddba_timeout(void *arg)
1549170530Ssam{
1550170530Ssam	struct ieee80211_tx_ampdu *tap = arg;
1551170530Ssam
1552170530Ssam	/* XXX ? */
1553170530Ssam	tap->txa_flags &= ~IEEE80211_AGGR_XCHGPEND;
1554170530Ssam	tap->txa_attempts++;
1555170530Ssam}
1556170530Ssam
1557170530Ssamstatic void
1558170530Ssamaddba_start_timeout(struct ieee80211_tx_ampdu *tap)
1559170530Ssam{
1560170530Ssam	/* XXX use CALLOUT_PENDING instead? */
1561178354Ssam	callout_reset(&tap->txa_timer, ieee80211_addba_timeout,
1562170530Ssam	    addba_timeout, tap);
1563170530Ssam	tap->txa_flags |= IEEE80211_AGGR_XCHGPEND;
1564178354Ssam	tap->txa_nextrequest = ticks + ieee80211_addba_timeout;
1565170530Ssam}
1566170530Ssam
1567170530Ssamstatic void
1568170530Ssamaddba_stop_timeout(struct ieee80211_tx_ampdu *tap)
1569170530Ssam{
1570170530Ssam	/* XXX use CALLOUT_PENDING instead? */
1571170530Ssam	if (tap->txa_flags & IEEE80211_AGGR_XCHGPEND) {
1572170530Ssam		callout_stop(&tap->txa_timer);
1573170530Ssam		tap->txa_flags &= ~IEEE80211_AGGR_XCHGPEND;
1574170530Ssam	}
1575170530Ssam}
1576170530Ssam
1577170530Ssam/*
1578170530Ssam * Default method for requesting A-MPDU tx aggregation.
1579170530Ssam * We setup the specified state block and start a timer
1580170530Ssam * to wait for an ADDBA response frame.
1581170530Ssam */
1582170530Ssamstatic int
1583170530Ssamieee80211_addba_request(struct ieee80211_node *ni,
1584170530Ssam	struct ieee80211_tx_ampdu *tap,
1585170530Ssam	int dialogtoken, int baparamset, int batimeout)
1586170530Ssam{
1587170530Ssam	int bufsiz;
1588170530Ssam
1589170530Ssam	/* XXX locking */
1590170530Ssam	tap->txa_token = dialogtoken;
1591170530Ssam	tap->txa_flags |= IEEE80211_AGGR_IMMEDIATE;
1592170530Ssam	bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
1593170530Ssam	tap->txa_wnd = (bufsiz == 0) ?
1594170530Ssam	    IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
1595170530Ssam	addba_start_timeout(tap);
1596170530Ssam	return 1;
1597170530Ssam}
1598170530Ssam
1599170530Ssam/*
1600170530Ssam * Default method for processing an A-MPDU tx aggregation
1601170530Ssam * response.  We shutdown any pending timer and update the
1602170530Ssam * state block according to the reply.
1603170530Ssam */
1604170530Ssamstatic int
1605170530Ssamieee80211_addba_response(struct ieee80211_node *ni,
1606170530Ssam	struct ieee80211_tx_ampdu *tap,
1607170530Ssam	int status, int baparamset, int batimeout)
1608170530Ssam{
1609184280Ssam	int bufsiz, tid;
1610170530Ssam
1611170530Ssam	/* XXX locking */
1612170530Ssam	addba_stop_timeout(tap);
1613170530Ssam	if (status == IEEE80211_STATUS_SUCCESS) {
1614170530Ssam		bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
1615170530Ssam		/* XXX override our request? */
1616170530Ssam		tap->txa_wnd = (bufsiz == 0) ?
1617170530Ssam		    IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
1618184280Ssam		/* XXX AC/TID */
1619184280Ssam		tid = MS(baparamset, IEEE80211_BAPS_TID);
1620170530Ssam		tap->txa_flags |= IEEE80211_AGGR_RUNNING;
1621184280Ssam		tap->txa_attempts = 0;
1622173273Ssam	} else {
1623173273Ssam		/* mark tid so we don't try again */
1624173273Ssam		tap->txa_flags |= IEEE80211_AGGR_NAK;
1625170530Ssam	}
1626170530Ssam	return 1;
1627170530Ssam}
1628170530Ssam
1629170530Ssam/*
1630170530Ssam * Default method for stopping A-MPDU tx aggregation.
1631170530Ssam * Any timer is cleared and we drain any pending frames.
1632170530Ssam */
1633170530Ssamstatic void
1634170530Ssamieee80211_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
1635170530Ssam{
1636170530Ssam	/* XXX locking */
1637170530Ssam	addba_stop_timeout(tap);
1638170530Ssam	if (tap->txa_flags & IEEE80211_AGGR_RUNNING) {
1639182830Ssam		/* XXX clear aggregation queue */
1640170530Ssam		tap->txa_flags &= ~IEEE80211_AGGR_RUNNING;
1641170530Ssam	}
1642170530Ssam	tap->txa_attempts = 0;
1643170530Ssam}
1644170530Ssam
1645170530Ssam/*
1646170530Ssam * Process a received action frame using the default aggregation
1647170530Ssam * policy.  We intercept ADDBA-related frames and use them to
1648170530Ssam * update our aggregation state.  All other frames are passed up
1649170530Ssam * for processing by ieee80211_recv_action.
1650170530Ssam */
1651195377Ssamstatic int
1652195377Ssamht_recv_action_ba_addba_request(struct ieee80211_node *ni,
1653195377Ssam	const struct ieee80211_frame *wh,
1654170530Ssam	const uint8_t *frm, const uint8_t *efrm)
1655170530Ssam{
1656170530Ssam	struct ieee80211com *ic = ni->ni_ic;
1657178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1658170530Ssam	struct ieee80211_rx_ampdu *rap;
1659195377Ssam	uint8_t dialogtoken;
1660195377Ssam	uint16_t baparamset, batimeout, baseqctl;
1661205277Srpaulo	uint16_t args[5];
1662195377Ssam	int tid;
1663170530Ssam
1664195377Ssam	dialogtoken = frm[2];
1665195377Ssam	baparamset = LE_READ_2(frm+3);
1666195377Ssam	batimeout = LE_READ_2(frm+5);
1667195377Ssam	baseqctl = LE_READ_2(frm+7);
1668170530Ssam
1669195377Ssam	tid = MS(baparamset, IEEE80211_BAPS_TID);
1670170530Ssam
1671195377Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1672195377Ssam	    "recv ADDBA request: dialogtoken %u baparamset 0x%x "
1673195377Ssam	    "(tid %d bufsiz %d) batimeout %d baseqctl %d:%d",
1674195377Ssam	    dialogtoken, baparamset,
1675195377Ssam	    tid, MS(baparamset, IEEE80211_BAPS_BUFSIZ),
1676195377Ssam	    batimeout,
1677195377Ssam	    MS(baseqctl, IEEE80211_BASEQ_START),
1678195377Ssam	    MS(baseqctl, IEEE80211_BASEQ_FRAG));
1679170530Ssam
1680195377Ssam	rap = &ni->ni_rx_ampdu[tid];
1681170530Ssam
1682195377Ssam	/* Send ADDBA response */
1683195377Ssam	args[0] = dialogtoken;
1684195377Ssam	/*
1685195377Ssam	 * NB: We ack only if the sta associated with HT and
1686195377Ssam	 * the ap is configured to do AMPDU rx (the latter
1687195377Ssam	 * violates the 11n spec and is mostly for testing).
1688195377Ssam	 */
1689195377Ssam	if ((ni->ni_flags & IEEE80211_NODE_AMPDU_RX) &&
1690195377Ssam	    (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_RX)) {
1691195377Ssam		/* XXX handle ampdu_rx_start failure */
1692195377Ssam		ic->ic_ampdu_rx_start(ni, rap,
1693195377Ssam		    baparamset, batimeout, baseqctl);
1694170530Ssam
1695195377Ssam		args[1] = IEEE80211_STATUS_SUCCESS;
1696195377Ssam	} else {
1697195377Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1698195377Ssam		    ni, "reject ADDBA request: %s",
1699195377Ssam		    ni->ni_flags & IEEE80211_NODE_AMPDU_RX ?
1700195377Ssam		       "administratively disabled" :
1701195377Ssam		       "not negotiated for station");
1702195377Ssam		vap->iv_stats.is_addba_reject++;
1703195377Ssam		args[1] = IEEE80211_STATUS_UNSPECIFIED;
1704195377Ssam	}
1705195377Ssam	/* XXX honor rap flags? */
1706195377Ssam	args[2] = IEEE80211_BAPS_POLICY_IMMEDIATE
1707195377Ssam		| SM(tid, IEEE80211_BAPS_TID)
1708195377Ssam		| SM(rap->rxa_wnd, IEEE80211_BAPS_BUFSIZ)
1709195377Ssam		;
1710195377Ssam	args[3] = 0;
1711205277Srpaulo	args[4] = 0;
1712195377Ssam	ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
1713195377Ssam		IEEE80211_ACTION_BA_ADDBA_RESPONSE, args);
1714195377Ssam	return 0;
1715195377Ssam}
1716170530Ssam
1717195377Ssamstatic int
1718195377Ssamht_recv_action_ba_addba_response(struct ieee80211_node *ni,
1719195377Ssam	const struct ieee80211_frame *wh,
1720195377Ssam	const uint8_t *frm, const uint8_t *efrm)
1721195377Ssam{
1722195377Ssam	struct ieee80211com *ic = ni->ni_ic;
1723195377Ssam	struct ieee80211vap *vap = ni->ni_vap;
1724195377Ssam	struct ieee80211_tx_ampdu *tap;
1725195377Ssam	uint8_t dialogtoken, policy;
1726195377Ssam	uint16_t baparamset, batimeout, code;
1727195377Ssam	int tid, ac, bufsiz;
1728170530Ssam
1729195377Ssam	dialogtoken = frm[2];
1730195377Ssam	code = LE_READ_2(frm+3);
1731195377Ssam	baparamset = LE_READ_2(frm+5);
1732195377Ssam	tid = MS(baparamset, IEEE80211_BAPS_TID);
1733195377Ssam	bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
1734195377Ssam	policy = MS(baparamset, IEEE80211_BAPS_POLICY);
1735195377Ssam	batimeout = LE_READ_2(frm+7);
1736195377Ssam
1737195377Ssam	ac = TID_TO_WME_AC(tid);
1738195377Ssam	tap = &ni->ni_tx_ampdu[ac];
1739195377Ssam	if ((tap->txa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
1740195377Ssam		IEEE80211_DISCARD_MAC(vap,
1741195377Ssam		    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1742195377Ssam		    ni->ni_macaddr, "ADDBA response",
1743195377Ssam		    "no pending ADDBA, tid %d dialogtoken %u "
1744195377Ssam		    "code %d", tid, dialogtoken, code);
1745195377Ssam		vap->iv_stats.is_addba_norequest++;
1746195377Ssam		return 0;
1747195377Ssam	}
1748195377Ssam	if (dialogtoken != tap->txa_token) {
1749195377Ssam		IEEE80211_DISCARD_MAC(vap,
1750195377Ssam		    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1751195377Ssam		    ni->ni_macaddr, "ADDBA response",
1752195377Ssam		    "dialogtoken mismatch: waiting for %d, "
1753195377Ssam		    "received %d, tid %d code %d",
1754195377Ssam		    tap->txa_token, dialogtoken, tid, code);
1755195377Ssam		vap->iv_stats.is_addba_badtoken++;
1756195377Ssam		return 0;
1757195377Ssam	}
1758195377Ssam	/* NB: assumes IEEE80211_AGGR_IMMEDIATE is 1 */
1759195377Ssam	if (policy != (tap->txa_flags & IEEE80211_AGGR_IMMEDIATE)) {
1760195377Ssam		IEEE80211_DISCARD_MAC(vap,
1761195377Ssam		    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1762195377Ssam		    ni->ni_macaddr, "ADDBA response",
1763195377Ssam		    "policy mismatch: expecting %s, "
1764195377Ssam		    "received %s, tid %d code %d",
1765195377Ssam		    tap->txa_flags & IEEE80211_AGGR_IMMEDIATE,
1766195377Ssam		    policy, tid, code);
1767195377Ssam		vap->iv_stats.is_addba_badpolicy++;
1768195377Ssam		return 0;
1769195377Ssam	}
1770182829Ssam#if 0
1771195377Ssam	/* XXX we take MIN in ieee80211_addba_response */
1772195377Ssam	if (bufsiz > IEEE80211_AGGR_BAWMAX) {
1773195377Ssam		IEEE80211_DISCARD_MAC(vap,
1774195377Ssam		    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1775195377Ssam		    ni->ni_macaddr, "ADDBA response",
1776195377Ssam		    "BA window too large: max %d, "
1777195377Ssam		    "received %d, tid %d code %d",
1778195377Ssam		    bufsiz, IEEE80211_AGGR_BAWMAX, tid, code);
1779195377Ssam		vap->iv_stats.is_addba_badbawinsize++;
1780195377Ssam		return 0;
1781195377Ssam	}
1782182829Ssam#endif
1783195377Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1784195377Ssam	    "recv ADDBA response: dialogtoken %u code %d "
1785195377Ssam	    "baparamset 0x%x (tid %d bufsiz %d) batimeout %d",
1786195377Ssam	    dialogtoken, code, baparamset, tid, bufsiz,
1787195377Ssam	    batimeout);
1788195377Ssam	ic->ic_addba_response(ni, tap, code, baparamset, batimeout);
1789195377Ssam	return 0;
1790195377Ssam}
1791170530Ssam
1792195377Ssamstatic int
1793195377Ssamht_recv_action_ba_delba(struct ieee80211_node *ni,
1794195377Ssam	const struct ieee80211_frame *wh,
1795195377Ssam	const uint8_t *frm, const uint8_t *efrm)
1796195377Ssam{
1797195377Ssam	struct ieee80211com *ic = ni->ni_ic;
1798195377Ssam	struct ieee80211_rx_ampdu *rap;
1799195377Ssam	struct ieee80211_tx_ampdu *tap;
1800195377Ssam	uint16_t baparamset, code;
1801195377Ssam	int tid, ac;
1802170530Ssam
1803195377Ssam	baparamset = LE_READ_2(frm+2);
1804195377Ssam	code = LE_READ_2(frm+4);
1805170530Ssam
1806195377Ssam	tid = MS(baparamset, IEEE80211_DELBAPS_TID);
1807170530Ssam
1808195377Ssam	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1809195377Ssam	    "recv DELBA: baparamset 0x%x (tid %d initiator %d) "
1810195377Ssam	    "code %d", baparamset, tid,
1811195377Ssam	    MS(baparamset, IEEE80211_DELBAPS_INIT), code);
1812195377Ssam
1813195377Ssam	if ((baparamset & IEEE80211_DELBAPS_INIT) == 0) {
1814195377Ssam		ac = TID_TO_WME_AC(tid);
1815195377Ssam		tap = &ni->ni_tx_ampdu[ac];
1816195377Ssam		ic->ic_addba_stop(ni, tap);
1817195377Ssam	} else {
1818195377Ssam		rap = &ni->ni_rx_ampdu[tid];
1819195377Ssam		ic->ic_ampdu_rx_stop(ni, rap);
1820170530Ssam	}
1821195377Ssam	return 0;
1822170530Ssam}
1823170530Ssam
1824195377Ssamstatic int
1825195377Ssamht_recv_action_ht_txchwidth(struct ieee80211_node *ni,
1826195377Ssam	const struct ieee80211_frame *wh,
1827170530Ssam	const uint8_t *frm, const uint8_t *efrm)
1828170530Ssam{
1829170530Ssam	int chw;
1830170530Ssam
1831195377Ssam	chw = (frm[2] == IEEE80211_A_HT_TXCHWIDTH_2040) ? 40 : 20;
1832195377Ssam
1833195377Ssam	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1834195377Ssam	    "%s: HT txchwidth, width %d%s",
1835195377Ssam	    __func__, chw, ni->ni_chw != chw ? "*" : "");
1836195377Ssam	if (chw != ni->ni_chw) {
1837195377Ssam		ni->ni_chw = chw;
1838195377Ssam		/* XXX notify on change */
1839170530Ssam	}
1840195377Ssam	return 0;
1841170530Ssam}
1842170530Ssam
1843195377Ssamstatic int
1844195377Ssamht_recv_action_ht_mimopwrsave(struct ieee80211_node *ni,
1845195377Ssam	const struct ieee80211_frame *wh,
1846195377Ssam	const uint8_t *frm, const uint8_t *efrm)
1847195377Ssam{
1848195377Ssam	const struct ieee80211_action_ht_mimopowersave *mps =
1849195377Ssam	    (const struct ieee80211_action_ht_mimopowersave *) frm;
1850195377Ssam
1851195377Ssam	/* XXX check iv_htcaps */
1852195377Ssam	if (mps->am_control & IEEE80211_A_HT_MIMOPWRSAVE_ENA)
1853195377Ssam		ni->ni_flags |= IEEE80211_NODE_MIMO_PS;
1854195377Ssam	else
1855195377Ssam		ni->ni_flags &= ~IEEE80211_NODE_MIMO_PS;
1856195377Ssam	if (mps->am_control & IEEE80211_A_HT_MIMOPWRSAVE_MODE)
1857195377Ssam		ni->ni_flags |= IEEE80211_NODE_MIMO_RTS;
1858195377Ssam	else
1859195377Ssam		ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS;
1860195377Ssam	/* XXX notify on change */
1861195377Ssam	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1862195377Ssam	    "%s: HT MIMO PS (%s%s)", __func__,
1863195377Ssam	    (ni->ni_flags & IEEE80211_NODE_MIMO_PS) ?  "on" : "off",
1864195377Ssam	    (ni->ni_flags & IEEE80211_NODE_MIMO_RTS) ?  "+rts" : ""
1865195377Ssam	);
1866195377Ssam	return 0;
1867195377Ssam}
1868195377Ssam
1869170530Ssam/*
1870170530Ssam * Transmit processing.
1871170530Ssam */
1872170530Ssam
1873170530Ssam/*
1874178354Ssam * Check if A-MPDU should be requested/enabled for a stream.
1875178354Ssam * We require a traffic rate above a per-AC threshold and we
1876178354Ssam * also handle backoff from previous failed attempts.
1877178354Ssam *
1878178354Ssam * Drivers may override this method to bring in information
1879178354Ssam * such as link state conditions in making the decision.
1880178354Ssam */
1881178354Ssamstatic int
1882178354Ssamieee80211_ampdu_enable(struct ieee80211_node *ni,
1883178354Ssam	struct ieee80211_tx_ampdu *tap)
1884178354Ssam{
1885178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1886178354Ssam
1887178354Ssam	if (tap->txa_avgpps < vap->iv_ampdu_mintraffic[tap->txa_ac])
1888178354Ssam		return 0;
1889178354Ssam	/* XXX check rssi? */
1890178354Ssam	if (tap->txa_attempts >= ieee80211_addba_maxtries &&
1891178354Ssam	    ticks < tap->txa_nextrequest) {
1892178354Ssam		/*
1893178354Ssam		 * Don't retry too often; txa_nextrequest is set
1894178354Ssam		 * to the minimum interval we'll retry after
1895178354Ssam		 * ieee80211_addba_maxtries failed attempts are made.
1896178354Ssam		 */
1897178354Ssam		return 0;
1898178354Ssam	}
1899178354Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
1900183249Ssam	    "enable AMPDU on %s, avgpps %d pkts %d",
1901183249Ssam	    ieee80211_wme_acnames[tap->txa_ac], tap->txa_avgpps, tap->txa_pkts);
1902178354Ssam	return 1;
1903178354Ssam}
1904178354Ssam
1905178354Ssam/*
1906170530Ssam * Request A-MPDU tx aggregation.  Setup local state and
1907170530Ssam * issue an ADDBA request.  BA use will only happen after
1908170530Ssam * the other end replies with ADDBA response.
1909170530Ssam */
1910170530Ssamint
1911170530Ssamieee80211_ampdu_request(struct ieee80211_node *ni,
1912170530Ssam	struct ieee80211_tx_ampdu *tap)
1913170530Ssam{
1914170530Ssam	struct ieee80211com *ic = ni->ni_ic;
1915205277Srpaulo	uint16_t args[5];
1916170530Ssam	int tid, dialogtoken;
1917170530Ssam	static int tokens = 0;	/* XXX */
1918170530Ssam
1919170530Ssam	/* XXX locking */
1920170530Ssam	if ((tap->txa_flags & IEEE80211_AGGR_SETUP) == 0) {
1921170530Ssam		/* do deferred setup of state */
1922184280Ssam		ampdu_tx_setup(tap);
1923170530Ssam	}
1924173273Ssam	/* XXX hack for not doing proper locking */
1925173273Ssam	tap->txa_flags &= ~IEEE80211_AGGR_NAK;
1926173273Ssam
1927170530Ssam	dialogtoken = (tokens+1) % 63;		/* XXX */
1928183245Ssam	tid = WME_AC_TO_TID(tap->txa_ac);
1929183245Ssam	tap->txa_start = ni->ni_txseqs[tid];
1930170530Ssam
1931170530Ssam	args[0] = dialogtoken;
1932205277Srpaulo	args[1] = 0;	/* NB: status code not used */
1933205277Srpaulo	args[2]	= IEEE80211_BAPS_POLICY_IMMEDIATE
1934170530Ssam		| SM(tid, IEEE80211_BAPS_TID)
1935170530Ssam		| SM(IEEE80211_AGGR_BAWMAX, IEEE80211_BAPS_BUFSIZ)
1936170530Ssam		;
1937205277Srpaulo	args[3] = 0;	/* batimeout */
1938170530Ssam	/* NB: do first so there's no race against reply */
1939205277Srpaulo	if (!ic->ic_addba_request(ni, tap, dialogtoken, args[2], args[3])) {
1940170530Ssam		/* unable to setup state, don't make request */
1941178354Ssam		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
1942173273Ssam		    ni, "%s: could not setup BA stream for AC %d",
1943173273Ssam		    __func__, tap->txa_ac);
1944173273Ssam		/* defer next try so we don't slam the driver with requests */
1945178354Ssam		tap->txa_attempts = ieee80211_addba_maxtries;
1946178354Ssam		/* NB: check in case driver wants to override */
1947178354Ssam		if (tap->txa_nextrequest <= ticks)
1948178354Ssam			tap->txa_nextrequest = ticks + ieee80211_addba_backoff;
1949170530Ssam		return 0;
1950170530Ssam	}
1951170530Ssam	tokens = dialogtoken;			/* allocate token */
1952183245Ssam	/* NB: after calling ic_addba_request so driver can set txa_start */
1953205277Srpaulo	args[4] = SM(tap->txa_start, IEEE80211_BASEQ_START)
1954178953Ssam		| SM(0, IEEE80211_BASEQ_FRAG)
1955178953Ssam		;
1956170530Ssam	return ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
1957170530Ssam		IEEE80211_ACTION_BA_ADDBA_REQUEST, args);
1958170530Ssam}
1959170530Ssam
1960170530Ssam/*
1961173273Ssam * Terminate an AMPDU tx stream.  State is reclaimed
1962173273Ssam * and the peer notified with a DelBA Action frame.
1963173273Ssam */
1964173273Ssamvoid
1965183250Ssamieee80211_ampdu_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
1966183250Ssam	int reason)
1967173273Ssam{
1968173273Ssam	struct ieee80211com *ic = ni->ni_ic;
1969178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1970173273Ssam	uint16_t args[4];
1971173273Ssam
1972173273Ssam	/* XXX locking */
1973184280Ssam	tap->txa_flags &= ~IEEE80211_AGGR_BARPEND;
1974173273Ssam	if (IEEE80211_AMPDU_RUNNING(tap)) {
1975178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1976183250Ssam		    ni, "%s: stop BA stream for AC %d (reason %d)",
1977183250Ssam		    __func__, tap->txa_ac, reason);
1978178354Ssam		vap->iv_stats.is_ampdu_stop++;
1979173273Ssam
1980173273Ssam		ic->ic_addba_stop(ni, tap);
1981173273Ssam		args[0] = WME_AC_TO_TID(tap->txa_ac);
1982173273Ssam		args[1] = IEEE80211_DELBAPS_INIT;
1983183250Ssam		args[2] = reason;			/* XXX reason code */
1984195377Ssam		ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
1985173273Ssam			IEEE80211_ACTION_BA_DELBA, args);
1986173273Ssam	} else {
1987178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1988183250Ssam		    ni, "%s: BA stream for AC %d not running (reason %d)",
1989183250Ssam		    __func__, tap->txa_ac, reason);
1990178354Ssam		vap->iv_stats.is_ampdu_stop_failed++;
1991173273Ssam	}
1992173273Ssam}
1993173273Ssam
1994184280Ssamstatic void
1995184280Ssambar_timeout(void *arg)
1996184280Ssam{
1997184280Ssam	struct ieee80211_tx_ampdu *tap = arg;
1998184280Ssam	struct ieee80211_node *ni = tap->txa_ni;
1999184280Ssam
2000184280Ssam	KASSERT((tap->txa_flags & IEEE80211_AGGR_XCHGPEND) == 0,
2001184280Ssam	    ("bar/addba collision, flags 0x%x", tap->txa_flags));
2002184280Ssam
2003184280Ssam	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2004184280Ssam	    ni, "%s: tid %u flags 0x%x attempts %d", __func__,
2005184280Ssam	    tap->txa_ac, tap->txa_flags, tap->txa_attempts);
2006184280Ssam
2007184280Ssam	/* guard against race with bar_tx_complete */
2008184280Ssam	if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) == 0)
2009184280Ssam		return;
2010184280Ssam	/* XXX ? */
2011184280Ssam	if (tap->txa_attempts >= ieee80211_bar_maxtries)
2012184280Ssam		ieee80211_ampdu_stop(ni, tap, IEEE80211_REASON_TIMEOUT);
2013184280Ssam	else
2014184280Ssam		ieee80211_send_bar(ni, tap, tap->txa_seqpending);
2015184280Ssam}
2016184280Ssam
2017184280Ssamstatic void
2018184280Ssambar_start_timer(struct ieee80211_tx_ampdu *tap)
2019184280Ssam{
2020184280Ssam	callout_reset(&tap->txa_timer, ieee80211_bar_timeout, bar_timeout, tap);
2021184280Ssam}
2022184280Ssam
2023184280Ssamstatic void
2024184280Ssambar_stop_timer(struct ieee80211_tx_ampdu *tap)
2025184280Ssam{
2026184280Ssam	callout_stop(&tap->txa_timer);
2027184280Ssam}
2028184280Ssam
2029184280Ssamstatic void
2030184280Ssambar_tx_complete(struct ieee80211_node *ni, void *arg, int status)
2031184280Ssam{
2032184280Ssam	struct ieee80211_tx_ampdu *tap = arg;
2033184280Ssam
2034184280Ssam	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2035184280Ssam	    ni, "%s: tid %u flags 0x%x pending %d status %d",
2036184280Ssam	    __func__, tap->txa_ac, tap->txa_flags,
2037184280Ssam	    callout_pending(&tap->txa_timer), status);
2038184280Ssam
2039184280Ssam	/* XXX locking */
2040184280Ssam	if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) &&
2041184280Ssam	    callout_pending(&tap->txa_timer)) {
2042184280Ssam		struct ieee80211com *ic = ni->ni_ic;
2043184280Ssam
2044184280Ssam		if (status)		/* ACK'd */
2045184280Ssam			bar_stop_timer(tap);
2046184280Ssam		ic->ic_bar_response(ni, tap, status);
2047184280Ssam		/* NB: just let timer expire so we pace requests */
2048184280Ssam	}
2049184280Ssam}
2050184280Ssam
2051184280Ssamstatic void
2052184280Ssamieee80211_bar_response(struct ieee80211_node *ni,
2053184280Ssam	struct ieee80211_tx_ampdu *tap, int status)
2054184280Ssam{
2055184280Ssam
2056184280Ssam	if (status != 0) {		/* got ACK */
2057184280Ssam		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2058184280Ssam		    ni, "BAR moves BA win <%u:%u> (%u frames) txseq %u tid %u",
2059184280Ssam		    tap->txa_start,
2060184280Ssam		    IEEE80211_SEQ_ADD(tap->txa_start, tap->txa_wnd-1),
2061184280Ssam		    tap->txa_qframes, tap->txa_seqpending,
2062184280Ssam		    WME_AC_TO_TID(tap->txa_ac));
2063184280Ssam
2064184280Ssam		/* NB: timer already stopped in bar_tx_complete */
2065184280Ssam		tap->txa_start = tap->txa_seqpending;
2066184280Ssam		tap->txa_flags &= ~IEEE80211_AGGR_BARPEND;
2067184280Ssam	}
2068184280Ssam}
2069184280Ssam
2070173273Ssam/*
2071170530Ssam * Transmit a BAR frame to the specified node.  The
2072170530Ssam * BAR contents are drawn from the supplied aggregation
2073170530Ssam * state associated with the node.
2074184280Ssam *
2075184280Ssam * NB: we only handle immediate ACK w/ compressed bitmap.
2076170530Ssam */
2077170530Ssamint
2078170530Ssamieee80211_send_bar(struct ieee80211_node *ni,
2079184280Ssam	struct ieee80211_tx_ampdu *tap, ieee80211_seq seq)
2080170530Ssam{
2081178354Ssam#define	senderr(_x, _v)	do { vap->iv_stats._v++; ret = _x; goto bad; } while (0)
2082178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2083170530Ssam	struct ieee80211com *ic = ni->ni_ic;
2084184280Ssam	struct ieee80211_frame_bar *bar;
2085170530Ssam	struct mbuf *m;
2086184280Ssam	uint16_t barctl, barseqctl;
2087170530Ssam	uint8_t *frm;
2088170530Ssam	int tid, ret;
2089170530Ssam
2090184280Ssam	if ((tap->txa_flags & IEEE80211_AGGR_RUNNING) == 0) {
2091184280Ssam		/* no ADDBA response, should not happen */
2092184280Ssam		/* XXX stat+msg */
2093184280Ssam		return EINVAL;
2094184280Ssam	}
2095184280Ssam	/* XXX locking */
2096184280Ssam	bar_stop_timer(tap);
2097184280Ssam
2098170530Ssam	ieee80211_ref_node(ni);
2099170530Ssam
2100184280Ssam	m = ieee80211_getmgtframe(&frm, ic->ic_headroom, sizeof(*bar));
2101170530Ssam	if (m == NULL)
2102170530Ssam		senderr(ENOMEM, is_tx_nobuf);
2103170530Ssam
2104184280Ssam	if (!ieee80211_add_callback(m, bar_tx_complete, tap)) {
2105184280Ssam		m_freem(m);
2106184280Ssam		senderr(ENOMEM, is_tx_nobuf);	/* XXX */
2107184280Ssam		/* NOTREACHED */
2108184280Ssam	}
2109184280Ssam
2110184280Ssam	bar = mtod(m, struct ieee80211_frame_bar *);
2111184280Ssam	bar->i_fc[0] = IEEE80211_FC0_VERSION_0 |
2112170530Ssam		IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_BAR;
2113184280Ssam	bar->i_fc[1] = 0;
2114184280Ssam	IEEE80211_ADDR_COPY(bar->i_ra, ni->ni_macaddr);
2115184280Ssam	IEEE80211_ADDR_COPY(bar->i_ta, vap->iv_myaddr);
2116170530Ssam
2117170530Ssam	tid = WME_AC_TO_TID(tap->txa_ac);
2118170530Ssam	barctl 	= (tap->txa_flags & IEEE80211_AGGR_IMMEDIATE ?
2119184280Ssam			0 : IEEE80211_BAR_NOACK)
2120184280Ssam		| IEEE80211_BAR_COMP
2121184280Ssam		| SM(tid, IEEE80211_BAR_TID)
2122170530Ssam		;
2123184280Ssam	barseqctl = SM(seq, IEEE80211_BAR_SEQ_START);
2124184280Ssam	/* NB: known to have proper alignment */
2125184280Ssam	bar->i_ctl = htole16(barctl);
2126184280Ssam	bar->i_seq = htole16(barseqctl);
2127184280Ssam	m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_bar);
2128170530Ssam
2129178354Ssam	M_WME_SETAC(m, WME_AC_VO);
2130178354Ssam
2131170530Ssam	IEEE80211_NODE_STAT(ni, tx_mgmt);	/* XXX tx_ctl? */
2132170530Ssam
2133184280Ssam	/* XXX locking */
2134184280Ssam	/* init/bump attempts counter */
2135184280Ssam	if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) == 0)
2136184280Ssam		tap->txa_attempts = 1;
2137184280Ssam	else
2138184280Ssam		tap->txa_attempts++;
2139184280Ssam	tap->txa_seqpending = seq;
2140184280Ssam	tap->txa_flags |= IEEE80211_AGGR_BARPEND;
2141170530Ssam
2142184280Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_11N,
2143184280Ssam	    ni, "send BAR: tid %u ctl 0x%x start %u (attempt %d)",
2144184280Ssam	    tid, barctl, seq, tap->txa_attempts);
2145184280Ssam
2146184280Ssam	ret = ic->ic_raw_xmit(ni, m, NULL);
2147184280Ssam	if (ret != 0) {
2148184280Ssam		/* xmit failed, clear state flag */
2149184280Ssam		tap->txa_flags &= ~IEEE80211_AGGR_BARPEND;
2150184280Ssam		goto bad;
2151184280Ssam	}
2152184280Ssam	/* XXX hack against tx complete happening before timer is started */
2153184280Ssam	if (tap->txa_flags & IEEE80211_AGGR_BARPEND)
2154184280Ssam		bar_start_timer(tap);
2155184280Ssam	return 0;
2156170530Ssambad:
2157170530Ssam	ieee80211_free_node(ni);
2158170530Ssam	return ret;
2159170530Ssam#undef senderr
2160170530Ssam}
2161170530Ssam
2162195377Ssamstatic int
2163195377Ssamht_action_output(struct ieee80211_node *ni, struct mbuf *m)
2164195377Ssam{
2165195377Ssam	struct ieee80211_bpf_params params;
2166195377Ssam
2167195377Ssam	memset(&params, 0, sizeof(params));
2168195377Ssam	params.ibp_pri = WME_AC_VO;
2169195377Ssam	params.ibp_rate0 = ni->ni_txparms->mgmtrate;
2170195377Ssam	/* NB: we know all frames are unicast */
2171195377Ssam	params.ibp_try0 = ni->ni_txparms->maxretry;
2172195377Ssam	params.ibp_power = ni->ni_txpower;
2173195377Ssam	return ieee80211_mgmt_output(ni, m, IEEE80211_FC0_SUBTYPE_ACTION,
2174195377Ssam	     &params);
2175195377Ssam}
2176195377Ssam
2177195377Ssam#define	ADDSHORT(frm, v) do {			\
2178195377Ssam	frm[0] = (v) & 0xff;			\
2179195377Ssam	frm[1] = (v) >> 8;			\
2180195377Ssam	frm += 2;				\
2181195377Ssam} while (0)
2182195377Ssam
2183170530Ssam/*
2184170530Ssam * Send an action management frame.  The arguments are stuff
2185170530Ssam * into a frame without inspection; the caller is assumed to
2186170530Ssam * prepare them carefully (e.g. based on the aggregation state).
2187170530Ssam */
2188195377Ssamstatic int
2189195377Ssamht_send_action_ba_addba(struct ieee80211_node *ni,
2190195377Ssam	int category, int action, void *arg0)
2191170530Ssam{
2192178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2193170530Ssam	struct ieee80211com *ic = ni->ni_ic;
2194195377Ssam	uint16_t *args = arg0;
2195170530Ssam	struct mbuf *m;
2196170530Ssam	uint8_t *frm;
2197195377Ssam
2198195377Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2199205277Srpaulo	    "send ADDBA %s: dialogtoken %d status %d "
2200195377Ssam	    "baparamset 0x%x (tid %d) batimeout 0x%x baseqctl 0x%x",
2201195377Ssam	    (action == IEEE80211_ACTION_BA_ADDBA_REQUEST) ?
2202195377Ssam		"request" : "response",
2203205277Srpaulo	    args[0], args[1], args[2], MS(args[2], IEEE80211_BAPS_TID),
2204205277Srpaulo	    args[3], args[4]);
2205195377Ssam
2206195377Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2207195377Ssam	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2208195377Ssam	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2209195377Ssam	ieee80211_ref_node(ni);
2210195377Ssam
2211195377Ssam	m = ieee80211_getmgtframe(&frm,
2212195377Ssam	    ic->ic_headroom + sizeof(struct ieee80211_frame),
2213195377Ssam	    sizeof(uint16_t)	/* action+category */
2214195377Ssam	    /* XXX may action payload */
2215195377Ssam	    + sizeof(struct ieee80211_action_ba_addbaresponse)
2216195377Ssam	);
2217195377Ssam	if (m != NULL) {
2218195377Ssam		*frm++ = category;
2219195377Ssam		*frm++ = action;
2220195377Ssam		*frm++ = args[0];		/* dialog token */
2221205277Srpaulo		if (action == IEEE80211_ACTION_BA_ADDBA_RESPONSE)
2222205277Srpaulo			ADDSHORT(frm, args[1]);	/* status code */
2223205277Srpaulo		ADDSHORT(frm, args[2]);		/* baparamset */
2224205277Srpaulo		ADDSHORT(frm, args[3]);		/* batimeout */
2225195377Ssam		if (action == IEEE80211_ACTION_BA_ADDBA_REQUEST)
2226205277Srpaulo			ADDSHORT(frm, args[4]);	/* baseqctl */
2227195377Ssam		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2228195377Ssam		return ht_action_output(ni, m);
2229195377Ssam	} else {
2230195377Ssam		vap->iv_stats.is_tx_nobuf++;
2231195377Ssam		ieee80211_free_node(ni);
2232195377Ssam		return ENOMEM;
2233195377Ssam	}
2234195377Ssam}
2235195377Ssam
2236195377Ssamstatic int
2237195377Ssamht_send_action_ba_delba(struct ieee80211_node *ni,
2238195377Ssam	int category, int action, void *arg0)
2239195377Ssam{
2240195377Ssam	struct ieee80211vap *vap = ni->ni_vap;
2241195377Ssam	struct ieee80211com *ic = ni->ni_ic;
2242195377Ssam	uint16_t *args = arg0;
2243195377Ssam	struct mbuf *m;
2244170530Ssam	uint16_t baparamset;
2245195377Ssam	uint8_t *frm;
2246170530Ssam
2247195377Ssam	baparamset = SM(args[0], IEEE80211_DELBAPS_TID)
2248195377Ssam		   | args[1]
2249195377Ssam		   ;
2250195377Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2251195377Ssam	    "send DELBA action: tid %d, initiator %d reason %d",
2252195377Ssam	    args[0], args[1], args[2]);
2253170530Ssam
2254178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2255195377Ssam	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2256195377Ssam	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2257170530Ssam	ieee80211_ref_node(ni);
2258170530Ssam
2259170530Ssam	m = ieee80211_getmgtframe(&frm,
2260195377Ssam	    ic->ic_headroom + sizeof(struct ieee80211_frame),
2261195377Ssam	    sizeof(uint16_t)	/* action+category */
2262195377Ssam	    /* XXX may action payload */
2263195377Ssam	    + sizeof(struct ieee80211_action_ba_addbaresponse)
2264170530Ssam	);
2265195377Ssam	if (m != NULL) {
2266195377Ssam		*frm++ = category;
2267195377Ssam		*frm++ = action;
2268195377Ssam		ADDSHORT(frm, baparamset);
2269195377Ssam		ADDSHORT(frm, args[2]);		/* reason code */
2270195377Ssam		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2271195377Ssam		return ht_action_output(ni, m);
2272195377Ssam	} else {
2273195377Ssam		vap->iv_stats.is_tx_nobuf++;
2274195377Ssam		ieee80211_free_node(ni);
2275195377Ssam		return ENOMEM;
2276195377Ssam	}
2277195377Ssam}
2278170530Ssam
2279195377Ssamstatic int
2280195377Ssamht_send_action_ht_txchwidth(struct ieee80211_node *ni,
2281195377Ssam	int category, int action, void *arg0)
2282195377Ssam{
2283195377Ssam	struct ieee80211vap *vap = ni->ni_vap;
2284195377Ssam	struct ieee80211com *ic = ni->ni_ic;
2285195377Ssam	struct mbuf *m;
2286195377Ssam	uint8_t *frm;
2287170530Ssam
2288195377Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2289195377Ssam	    "send HT txchwidth: width %d",
2290195377Ssam	    IEEE80211_IS_CHAN_HT40(ni->ni_chan) ? 40 : 20);
2291170530Ssam
2292195377Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2293195377Ssam	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2294195377Ssam	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2295195377Ssam	ieee80211_ref_node(ni);
2296170530Ssam
2297195377Ssam	m = ieee80211_getmgtframe(&frm,
2298195377Ssam	    ic->ic_headroom + sizeof(struct ieee80211_frame),
2299195377Ssam	    sizeof(uint16_t)	/* action+category */
2300195377Ssam	    /* XXX may action payload */
2301195377Ssam	    + sizeof(struct ieee80211_action_ba_addbaresponse)
2302195377Ssam	);
2303195377Ssam	if (m != NULL) {
2304195377Ssam		*frm++ = category;
2305195377Ssam		*frm++ = action;
2306195377Ssam		*frm++ = IEEE80211_IS_CHAN_HT40(ni->ni_chan) ?
2307195377Ssam			IEEE80211_A_HT_TXCHWIDTH_2040 :
2308195377Ssam			IEEE80211_A_HT_TXCHWIDTH_20;
2309195377Ssam		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2310195377Ssam		return ht_action_output(ni, m);
2311195377Ssam	} else {
2312195377Ssam		vap->iv_stats.is_tx_nobuf++;
2313195377Ssam		ieee80211_free_node(ni);
2314195377Ssam		return ENOMEM;
2315170530Ssam	}
2316195377Ssam}
2317170530Ssam#undef ADDSHORT
2318170530Ssam
2319170530Ssam/*
2320170530Ssam * Construct the MCS bit mask for inclusion
2321170530Ssam * in an HT information element.
2322170530Ssam */
2323170530Ssamstatic void
2324170530Ssamieee80211_set_htrates(uint8_t *frm, const struct ieee80211_htrateset *rs)
2325170530Ssam{
2326170530Ssam	int i;
2327170530Ssam
2328170530Ssam	for (i = 0; i < rs->rs_nrates; i++) {
2329170530Ssam		int r = rs->rs_rates[i] & IEEE80211_RATE_VAL;
2330170530Ssam		if (r < IEEE80211_HTRATE_MAXSIZE) {	/* XXX? */
2331170530Ssam			/* NB: this assumes a particular implementation */
2332170530Ssam			setbit(frm, r);
2333170530Ssam		}
2334170530Ssam	}
2335170530Ssam}
2336170530Ssam
2337170530Ssam/*
2338170530Ssam * Add body of an HTCAP information element.
2339170530Ssam */
2340170530Ssamstatic uint8_t *
2341170530Ssamieee80211_add_htcap_body(uint8_t *frm, struct ieee80211_node *ni)
2342170530Ssam{
2343170530Ssam#define	ADDSHORT(frm, v) do {			\
2344170530Ssam	frm[0] = (v) & 0xff;			\
2345170530Ssam	frm[1] = (v) >> 8;			\
2346170530Ssam	frm += 2;				\
2347170530Ssam} while (0)
2348178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2349205513Srpaulo	uint16_t caps, extcaps;
2350173865Ssam	int rxmax, density;
2351170530Ssam
2352170530Ssam	/* HT capabilities */
2353178354Ssam	caps = vap->iv_htcaps & 0xffff;
2354173273Ssam	/*
2355173273Ssam	 * Note channel width depends on whether we are operating as
2356173273Ssam	 * a sta or not.  When operating as a sta we are generating
2357173273Ssam	 * a request based on our desired configuration.  Otherwise
2358173273Ssam	 * we are operational and the channel attributes identify
2359173273Ssam	 * how we've been setup (which might be different if a fixed
2360173273Ssam	 * channel is specified).
2361173273Ssam	 */
2362178354Ssam	if (vap->iv_opmode == IEEE80211_M_STA) {
2363173273Ssam		/* override 20/40 use based on config */
2364193655Ssam		if (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)
2365173273Ssam			caps |= IEEE80211_HTCAP_CHWIDTH40;
2366173273Ssam		else
2367173273Ssam			caps &= ~IEEE80211_HTCAP_CHWIDTH40;
2368173865Ssam		/* use advertised setting (XXX locally constraint) */
2369173865Ssam		rxmax = MS(ni->ni_htparam, IEEE80211_HTCAP_MAXRXAMPDU);
2370173865Ssam		density = MS(ni->ni_htparam, IEEE80211_HTCAP_MPDUDENSITY);
2371173273Ssam	} else {
2372173273Ssam		/* override 20/40 use based on current channel */
2373178354Ssam		if (IEEE80211_IS_CHAN_HT40(ni->ni_chan))
2374173273Ssam			caps |= IEEE80211_HTCAP_CHWIDTH40;
2375173273Ssam		else
2376173273Ssam			caps &= ~IEEE80211_HTCAP_CHWIDTH40;
2377178354Ssam		rxmax = vap->iv_ampdu_rxmax;
2378178354Ssam		density = vap->iv_ampdu_density;
2379173273Ssam	}
2380170530Ssam	/* adjust short GI based on channel and config */
2381193655Ssam	if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20) == 0)
2382170530Ssam		caps &= ~IEEE80211_HTCAP_SHORTGI20;
2383193655Ssam	if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40) == 0 ||
2384170530Ssam	    (caps & IEEE80211_HTCAP_CHWIDTH40) == 0)
2385170530Ssam		caps &= ~IEEE80211_HTCAP_SHORTGI40;
2386170530Ssam	ADDSHORT(frm, caps);
2387170530Ssam
2388170530Ssam	/* HT parameters */
2389173865Ssam	*frm = SM(rxmax, IEEE80211_HTCAP_MAXRXAMPDU)
2390173865Ssam	     | SM(density, IEEE80211_HTCAP_MPDUDENSITY)
2391173273Ssam	     ;
2392170530Ssam	frm++;
2393170530Ssam
2394170530Ssam	/* pre-zero remainder of ie */
2395170530Ssam	memset(frm, 0, sizeof(struct ieee80211_ie_htcap) -
2396170530Ssam		__offsetof(struct ieee80211_ie_htcap, hc_mcsset));
2397170530Ssam
2398170530Ssam	/* supported MCS set */
2399173273Ssam	/*
2400173273Ssam	 * XXX it would better to get the rate set from ni_htrates
2401173273Ssam	 * so we can restrict it but for sta mode ni_htrates isn't
2402173273Ssam	 * setup when we're called to form an AssocReq frame so for
2403173273Ssam	 * now we're restricted to the default HT rate set.
2404173273Ssam	 */
2405173273Ssam	ieee80211_set_htrates(frm, &ieee80211_rateset_11n);
2406170530Ssam
2407205513Srpaulo	frm += __offsetof(struct ieee80211_ie_htcap, hc_extcap) -
2408205513Srpaulo		__offsetof(struct ieee80211_ie_htcap, hc_mcsset);
2409205513Srpaulo
2410205513Srpaulo	/* HT extended capabilities */
2411205513Srpaulo	extcaps = vap->iv_htextcaps & 0xffff;
2412205513Srpaulo
2413205513Srpaulo	ADDSHORT(frm, extcaps);
2414205513Srpaulo
2415170530Ssam	frm += sizeof(struct ieee80211_ie_htcap) -
2416205513Srpaulo		__offsetof(struct ieee80211_ie_htcap, hc_txbf);
2417205513Srpaulo
2418170530Ssam	return frm;
2419170530Ssam#undef ADDSHORT
2420170530Ssam}
2421170530Ssam
2422170530Ssam/*
2423170530Ssam * Add 802.11n HT capabilities information element
2424170530Ssam */
2425170530Ssamuint8_t *
2426170530Ssamieee80211_add_htcap(uint8_t *frm, struct ieee80211_node *ni)
2427170530Ssam{
2428170530Ssam	frm[0] = IEEE80211_ELEMID_HTCAP;
2429170530Ssam	frm[1] = sizeof(struct ieee80211_ie_htcap) - 2;
2430170530Ssam	return ieee80211_add_htcap_body(frm + 2, ni);
2431170530Ssam}
2432170530Ssam
2433170530Ssam/*
2434170530Ssam * Add Broadcom OUI wrapped standard HTCAP ie; this is
2435170530Ssam * used for compatibility w/ pre-draft implementations.
2436170530Ssam */
2437170530Ssamuint8_t *
2438170530Ssamieee80211_add_htcap_vendor(uint8_t *frm, struct ieee80211_node *ni)
2439170530Ssam{
2440170530Ssam	frm[0] = IEEE80211_ELEMID_VENDOR;
2441170530Ssam	frm[1] = 4 + sizeof(struct ieee80211_ie_htcap) - 2;
2442170530Ssam	frm[2] = (BCM_OUI >> 0) & 0xff;
2443170530Ssam	frm[3] = (BCM_OUI >> 8) & 0xff;
2444170530Ssam	frm[4] = (BCM_OUI >> 16) & 0xff;
2445170530Ssam	frm[5] = BCM_OUI_HTCAP;
2446170530Ssam	return ieee80211_add_htcap_body(frm + 6, ni);
2447170530Ssam}
2448170530Ssam
2449170530Ssam/*
2450170530Ssam * Construct the MCS bit mask of basic rates
2451170530Ssam * for inclusion in an HT information element.
2452170530Ssam */
2453170530Ssamstatic void
2454170530Ssamieee80211_set_basic_htrates(uint8_t *frm, const struct ieee80211_htrateset *rs)
2455170530Ssam{
2456170530Ssam	int i;
2457170530Ssam
2458170530Ssam	for (i = 0; i < rs->rs_nrates; i++) {
2459170530Ssam		int r = rs->rs_rates[i] & IEEE80211_RATE_VAL;
2460170530Ssam		if ((rs->rs_rates[i] & IEEE80211_RATE_BASIC) &&
2461170530Ssam		    r < IEEE80211_HTRATE_MAXSIZE) {
2462170530Ssam			/* NB: this assumes a particular implementation */
2463170530Ssam			setbit(frm, r);
2464170530Ssam		}
2465170530Ssam	}
2466170530Ssam}
2467170530Ssam
2468170530Ssam/*
2469172211Ssam * Update the HTINFO ie for a beacon frame.
2470172211Ssam */
2471172211Ssamvoid
2472178354Ssamieee80211_ht_update_beacon(struct ieee80211vap *vap,
2473172211Ssam	struct ieee80211_beacon_offsets *bo)
2474172211Ssam{
2475172211Ssam#define	PROTMODE	(IEEE80211_HTINFO_OPMODE|IEEE80211_HTINFO_NONHT_PRESENT)
2476178354Ssam	const struct ieee80211_channel *bsschan = vap->iv_bss->ni_chan;
2477178354Ssam	struct ieee80211com *ic = vap->iv_ic;
2478172211Ssam	struct ieee80211_ie_htinfo *ht =
2479172211Ssam	   (struct ieee80211_ie_htinfo *) bo->bo_htinfo;
2480172211Ssam
2481172211Ssam	/* XXX only update on channel change */
2482178354Ssam	ht->hi_ctrlchannel = ieee80211_chan2ieee(ic, bsschan);
2483193655Ssam	if (vap->iv_flags_ht & IEEE80211_FHT_RIFS)
2484183256Ssam		ht->hi_byte1 = IEEE80211_HTINFO_RIFSMODE_PERM;
2485183256Ssam	else
2486183256Ssam		ht->hi_byte1 = IEEE80211_HTINFO_RIFSMODE_PROH;
2487178354Ssam	if (IEEE80211_IS_CHAN_HT40U(bsschan))
2488172211Ssam		ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_ABOVE;
2489178354Ssam	else if (IEEE80211_IS_CHAN_HT40D(bsschan))
2490172211Ssam		ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_BELOW;
2491172211Ssam	else
2492172211Ssam		ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_NONE;
2493178354Ssam	if (IEEE80211_IS_CHAN_HT40(bsschan))
2494172211Ssam		ht->hi_byte1 |= IEEE80211_HTINFO_TXWIDTH_2040;
2495172211Ssam
2496172211Ssam	/* protection mode */
2497172211Ssam	ht->hi_byte2 = (ht->hi_byte2 &~ PROTMODE) | ic->ic_curhtprotmode;
2498172211Ssam
2499172211Ssam	/* XXX propagate to vendor ie's */
2500172211Ssam#undef PROTMODE
2501172211Ssam}
2502172211Ssam
2503172211Ssam/*
2504170530Ssam * Add body of an HTINFO information element.
2505173273Ssam *
2506173273Ssam * NB: We don't use struct ieee80211_ie_htinfo because we can
2507173273Ssam * be called to fillin both a standard ie and a compat ie that
2508173273Ssam * has a vendor OUI at the front.
2509170530Ssam */
2510170530Ssamstatic uint8_t *
2511170530Ssamieee80211_add_htinfo_body(uint8_t *frm, struct ieee80211_node *ni)
2512170530Ssam{
2513183256Ssam	struct ieee80211vap *vap = ni->ni_vap;
2514170530Ssam	struct ieee80211com *ic = ni->ni_ic;
2515170530Ssam
2516170530Ssam	/* pre-zero remainder of ie */
2517170530Ssam	memset(frm, 0, sizeof(struct ieee80211_ie_htinfo) - 2);
2518170530Ssam
2519170530Ssam	/* primary/control channel center */
2520178354Ssam	*frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
2521170530Ssam
2522193655Ssam	if (vap->iv_flags_ht & IEEE80211_FHT_RIFS)
2523183256Ssam		frm[0] = IEEE80211_HTINFO_RIFSMODE_PERM;
2524183256Ssam	else
2525183256Ssam		frm[0] = IEEE80211_HTINFO_RIFSMODE_PROH;
2526178354Ssam	if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan))
2527170530Ssam		frm[0] |= IEEE80211_HTINFO_2NDCHAN_ABOVE;
2528178354Ssam	else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan))
2529170530Ssam		frm[0] |= IEEE80211_HTINFO_2NDCHAN_BELOW;
2530170530Ssam	else
2531170530Ssam		frm[0] |= IEEE80211_HTINFO_2NDCHAN_NONE;
2532178354Ssam	if (IEEE80211_IS_CHAN_HT40(ni->ni_chan))
2533170530Ssam		frm[0] |= IEEE80211_HTINFO_TXWIDTH_2040;
2534170530Ssam
2535172211Ssam	frm[1] = ic->ic_curhtprotmode;
2536170530Ssam
2537170530Ssam	frm += 5;
2538170530Ssam
2539170530Ssam	/* basic MCS set */
2540170530Ssam	ieee80211_set_basic_htrates(frm, &ni->ni_htrates);
2541170530Ssam	frm += sizeof(struct ieee80211_ie_htinfo) -
2542170530Ssam		__offsetof(struct ieee80211_ie_htinfo, hi_basicmcsset);
2543170530Ssam	return frm;
2544170530Ssam}
2545170530Ssam
2546170530Ssam/*
2547170530Ssam * Add 802.11n HT information information element.
2548170530Ssam */
2549170530Ssamuint8_t *
2550170530Ssamieee80211_add_htinfo(uint8_t *frm, struct ieee80211_node *ni)
2551170530Ssam{
2552170530Ssam	frm[0] = IEEE80211_ELEMID_HTINFO;
2553170530Ssam	frm[1] = sizeof(struct ieee80211_ie_htinfo) - 2;
2554170530Ssam	return ieee80211_add_htinfo_body(frm + 2, ni);
2555170530Ssam}
2556170530Ssam
2557170530Ssam/*
2558170530Ssam * Add Broadcom OUI wrapped standard HTINFO ie; this is
2559170530Ssam * used for compatibility w/ pre-draft implementations.
2560170530Ssam */
2561170530Ssamuint8_t *
2562170530Ssamieee80211_add_htinfo_vendor(uint8_t *frm, struct ieee80211_node *ni)
2563170530Ssam{
2564170530Ssam	frm[0] = IEEE80211_ELEMID_VENDOR;
2565170530Ssam	frm[1] = 4 + sizeof(struct ieee80211_ie_htinfo) - 2;
2566170530Ssam	frm[2] = (BCM_OUI >> 0) & 0xff;
2567170530Ssam	frm[3] = (BCM_OUI >> 8) & 0xff;
2568170530Ssam	frm[4] = (BCM_OUI >> 16) & 0xff;
2569170530Ssam	frm[5] = BCM_OUI_HTINFO;
2570170530Ssam	return ieee80211_add_htinfo_body(frm + 6, ni);
2571170530Ssam}
2572