ieee80211_ht.c revision 234324
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 234324 2012-04-15 20:29:39Z adrian $");
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
57219456Sbschmidtconst struct ieee80211_mcs_rates ieee80211_htrates[IEEE80211_HTRATE_MAXSIZE] = {
58219456Sbschmidt	{  13,  14,   27,   30 },	/* MCS 0 */
59219456Sbschmidt	{  26,  29,   54,   60 },	/* MCS 1 */
60219456Sbschmidt	{  39,  43,   81,   90 },	/* MCS 2 */
61219456Sbschmidt	{  52,  58,  108,  120 },	/* MCS 3 */
62219456Sbschmidt	{  78,  87,  162,  180 },	/* MCS 4 */
63219456Sbschmidt	{ 104, 116,  216,  240 },	/* MCS 5 */
64219456Sbschmidt	{ 117, 130,  243,  270 },	/* MCS 6 */
65219456Sbschmidt	{ 130, 144,  270,  300 },	/* MCS 7 */
66219456Sbschmidt	{  26,  29,   54,   60 },	/* MCS 8 */
67219456Sbschmidt	{  52,  58,  108,  120 },	/* MCS 9 */
68219456Sbschmidt	{  78,  87,  162,  180 },	/* MCS 10 */
69219456Sbschmidt	{ 104, 116,  216,  240 },	/* MCS 11 */
70219456Sbschmidt	{ 156, 173,  324,  360 },	/* MCS 12 */
71219456Sbschmidt	{ 208, 231,  432,  480 },	/* MCS 13 */
72219456Sbschmidt	{ 234, 260,  486,  540 },	/* MCS 14 */
73219456Sbschmidt	{ 260, 289,  540,  600 },	/* MCS 15 */
74219456Sbschmidt	{  39,  43,   81,   90 },	/* MCS 16 */
75219456Sbschmidt	{  78,  87,  162,  180 },	/* MCS 17 */
76219456Sbschmidt	{ 117, 130,  243,  270 },	/* MCS 18 */
77219456Sbschmidt	{ 156, 173,  324,  360 },	/* MCS 19 */
78219456Sbschmidt	{ 234, 260,  486,  540 },	/* MCS 20 */
79219456Sbschmidt	{ 312, 347,  648,  720 },	/* MCS 21 */
80219456Sbschmidt	{ 351, 390,  729,  810 },	/* MCS 22 */
81219456Sbschmidt	{ 390, 433,  810,  900 },	/* MCS 23 */
82219456Sbschmidt	{  52,  58,  108,  120 },	/* MCS 24 */
83219456Sbschmidt	{ 104, 116,  216,  240 },	/* MCS 25 */
84219456Sbschmidt	{ 156, 173,  324,  360 },	/* MCS 26 */
85219456Sbschmidt	{ 208, 231,  432,  480 },	/* MCS 27 */
86219456Sbschmidt	{ 312, 347,  648,  720 },	/* MCS 28 */
87219456Sbschmidt	{ 416, 462,  864,  960 },	/* MCS 29 */
88219456Sbschmidt	{ 468, 520,  972, 1080 },	/* MCS 30 */
89219456Sbschmidt	{ 520, 578, 1080, 1200 },	/* MCS 31 */
90219456Sbschmidt	{   0,   0,   12,   13 },	/* MCS 32 */
91219456Sbschmidt	{  78,  87,  162,  180 },	/* MCS 33 */
92219456Sbschmidt	{ 104, 116,  216,  240 },	/* MCS 34 */
93219456Sbschmidt	{ 130, 144,  270,  300 },	/* MCS 35 */
94219456Sbschmidt	{ 117, 130,  243,  270 },	/* MCS 36 */
95219456Sbschmidt	{ 156, 173,  324,  360 },	/* MCS 37 */
96219456Sbschmidt	{ 195, 217,  405,  450 },	/* MCS 38 */
97219456Sbschmidt	{ 104, 116,  216,  240 },	/* MCS 39 */
98219456Sbschmidt	{ 130, 144,  270,  300 },	/* MCS 40 */
99219456Sbschmidt	{ 130, 144,  270,  300 },	/* MCS 41 */
100219456Sbschmidt	{ 156, 173,  324,  360 },	/* MCS 42 */
101219456Sbschmidt	{ 182, 202,  378,  420 },	/* MCS 43 */
102219456Sbschmidt	{ 182, 202,  378,  420 },	/* MCS 44 */
103219456Sbschmidt	{ 208, 231,  432,  480 },	/* MCS 45 */
104219456Sbschmidt	{ 156, 173,  324,  360 },	/* MCS 46 */
105219456Sbschmidt	{ 195, 217,  405,  450 },	/* MCS 47 */
106219456Sbschmidt	{ 195, 217,  405,  450 },	/* MCS 48 */
107219456Sbschmidt	{ 234, 260,  486,  540 },	/* MCS 49 */
108219456Sbschmidt	{ 273, 303,  567,  630 },	/* MCS 50 */
109219456Sbschmidt	{ 273, 303,  567,  630 },	/* MCS 51 */
110219456Sbschmidt	{ 312, 347,  648,  720 },	/* MCS 52 */
111219456Sbschmidt	{ 130, 144,  270,  300 },	/* MCS 53 */
112219456Sbschmidt	{ 156, 173,  324,  360 },	/* MCS 54 */
113219456Sbschmidt	{ 182, 202,  378,  420 },	/* MCS 55 */
114219456Sbschmidt	{ 156, 173,  324,  360 },	/* MCS 56 */
115219456Sbschmidt	{ 182, 202,  378,  420 },	/* MCS 57 */
116219456Sbschmidt	{ 208, 231,  432,  480 },	/* MCS 58 */
117219456Sbschmidt	{ 234, 260,  486,  540 },	/* MCS 59 */
118219456Sbschmidt	{ 208, 231,  432,  480 },	/* MCS 60 */
119219456Sbschmidt	{ 234, 260,  486,  540 },	/* MCS 61 */
120219456Sbschmidt	{ 260, 289,  540,  600 },	/* MCS 62 */
121219456Sbschmidt	{ 260, 289,  540,  600 },	/* MCS 63 */
122219456Sbschmidt	{ 286, 318,  594,  660 },	/* MCS 64 */
123219456Sbschmidt	{ 195, 217,  405,  450 },	/* MCS 65 */
124219456Sbschmidt	{ 234, 260,  486,  540 },	/* MCS 66 */
125219456Sbschmidt	{ 273, 303,  567,  630 },	/* MCS 67 */
126219456Sbschmidt	{ 234, 260,  486,  540 },	/* MCS 68 */
127219456Sbschmidt	{ 273, 303,  567,  630 },	/* MCS 69 */
128219456Sbschmidt	{ 312, 347,  648,  720 },	/* MCS 70 */
129219456Sbschmidt	{ 351, 390,  729,  810 },	/* MCS 71 */
130219456Sbschmidt	{ 312, 347,  648,  720 },	/* MCS 72 */
131219456Sbschmidt	{ 351, 390,  729,  810 },	/* MCS 73 */
132219456Sbschmidt	{ 390, 433,  810,  900 },	/* MCS 74 */
133219456Sbschmidt	{ 390, 433,  810,  900 },	/* MCS 75 */
134219456Sbschmidt	{ 429, 477,  891,  990 },	/* MCS 76 */
135170530Ssam};
136170530Ssam
137173273Ssam#ifdef IEEE80211_AMPDU_AGE
138193115Ssamstatic	int ieee80211_ampdu_age = -1;	/* threshold for ampdu reorder q (ms) */
139193115SsamSYSCTL_PROC(_net_wlan, OID_AUTO, ampdu_age, CTLTYPE_INT | CTLFLAG_RW,
140193115Ssam	&ieee80211_ampdu_age, 0, ieee80211_sysctl_msecs_ticks, "I",
141193115Ssam	"AMPDU max reorder age (ms)");
142173273Ssam#endif
143173273Ssam
144193115Ssamstatic	int ieee80211_recv_bar_ena = 1;
145193115SsamSYSCTL_INT(_net_wlan, OID_AUTO, recv_bar, CTLFLAG_RW, &ieee80211_recv_bar_ena,
146193115Ssam	    0, "BAR frame processing (ena/dis)");
147193115Ssam
148193115Ssamstatic	int ieee80211_addba_timeout = -1;/* timeout for ADDBA response */
149193115SsamSYSCTL_PROC(_net_wlan, OID_AUTO, addba_timeout, CTLTYPE_INT | CTLFLAG_RW,
150193115Ssam	&ieee80211_addba_timeout, 0, ieee80211_sysctl_msecs_ticks, "I",
151193115Ssam	"ADDBA request timeout (ms)");
152193115Ssamstatic	int ieee80211_addba_backoff = -1;/* backoff after max ADDBA requests */
153193115SsamSYSCTL_PROC(_net_wlan, OID_AUTO, addba_backoff, CTLTYPE_INT | CTLFLAG_RW,
154193115Ssam	&ieee80211_addba_backoff, 0, ieee80211_sysctl_msecs_ticks, "I",
155193115Ssam	"ADDBA request backoff (ms)");
156193115Ssamstatic	int ieee80211_addba_maxtries = 3;/* max ADDBA requests before backoff */
157193115SsamSYSCTL_INT(_net_wlan, OID_AUTO, addba_maxtries, CTLTYPE_INT | CTLFLAG_RW,
158193115Ssam	&ieee80211_addba_maxtries, 0, "max ADDBA requests sent before backoff");
159193115Ssam
160193115Ssamstatic	int ieee80211_bar_timeout = -1;	/* timeout waiting for BAR response */
161193115Ssamstatic	int ieee80211_bar_maxtries = 50;/* max BAR requests before DELBA */
162193115Ssam
163195377Ssamstatic	ieee80211_recv_action_func ht_recv_action_ba_addba_request;
164195377Ssamstatic	ieee80211_recv_action_func ht_recv_action_ba_addba_response;
165195377Ssamstatic	ieee80211_recv_action_func ht_recv_action_ba_delba;
166195377Ssamstatic	ieee80211_recv_action_func ht_recv_action_ht_mimopwrsave;
167195377Ssamstatic	ieee80211_recv_action_func ht_recv_action_ht_txchwidth;
168195377Ssam
169195377Ssamstatic	ieee80211_send_action_func ht_send_action_ba_addba;
170195377Ssamstatic	ieee80211_send_action_func ht_send_action_ba_delba;
171195377Ssamstatic	ieee80211_send_action_func ht_send_action_ht_txchwidth;
172195377Ssam
173178354Ssamstatic void
174195377Ssamieee80211_ht_init(void)
175178354Ssam{
176195377Ssam	/*
177195377Ssam	 * Setup HT parameters that depends on the clock frequency.
178195377Ssam	 */
179178354Ssam#ifdef IEEE80211_AMPDU_AGE
180178354Ssam	ieee80211_ampdu_age = msecs_to_ticks(500);
181178354Ssam#endif
182178354Ssam	ieee80211_addba_timeout = msecs_to_ticks(250);
183178354Ssam	ieee80211_addba_backoff = msecs_to_ticks(10*1000);
184184280Ssam	ieee80211_bar_timeout = msecs_to_ticks(250);
185195377Ssam	/*
186195377Ssam	 * Register action frame handlers.
187195377Ssam	 */
188195377Ssam	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_BA,
189195377Ssam	    IEEE80211_ACTION_BA_ADDBA_REQUEST, ht_recv_action_ba_addba_request);
190195377Ssam	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_BA,
191195377Ssam	    IEEE80211_ACTION_BA_ADDBA_RESPONSE, ht_recv_action_ba_addba_response);
192195377Ssam	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_BA,
193195377Ssam	    IEEE80211_ACTION_BA_DELBA, ht_recv_action_ba_delba);
194195377Ssam	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_HT,
195195377Ssam	    IEEE80211_ACTION_HT_MIMOPWRSAVE, ht_recv_action_ht_mimopwrsave);
196195377Ssam	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_HT,
197195377Ssam	    IEEE80211_ACTION_HT_TXCHWIDTH, ht_recv_action_ht_txchwidth);
198195377Ssam
199195377Ssam	ieee80211_send_action_register(IEEE80211_ACTION_CAT_BA,
200195377Ssam	    IEEE80211_ACTION_BA_ADDBA_REQUEST, ht_send_action_ba_addba);
201195377Ssam	ieee80211_send_action_register(IEEE80211_ACTION_CAT_BA,
202195377Ssam	    IEEE80211_ACTION_BA_ADDBA_RESPONSE, ht_send_action_ba_addba);
203195377Ssam	ieee80211_send_action_register(IEEE80211_ACTION_CAT_BA,
204195377Ssam	    IEEE80211_ACTION_BA_DELBA, ht_send_action_ba_delba);
205195377Ssam	ieee80211_send_action_register(IEEE80211_ACTION_CAT_HT,
206195377Ssam	    IEEE80211_ACTION_HT_TXCHWIDTH, ht_send_action_ht_txchwidth);
207178354Ssam}
208195377SsamSYSINIT(wlan_ht, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_ht_init, NULL);
209170530Ssam
210178354Ssamstatic int ieee80211_ampdu_enable(struct ieee80211_node *ni,
211178354Ssam	struct ieee80211_tx_ampdu *tap);
212170530Ssamstatic int ieee80211_addba_request(struct ieee80211_node *ni,
213170530Ssam	struct ieee80211_tx_ampdu *tap,
214170530Ssam	int dialogtoken, int baparamset, int batimeout);
215170530Ssamstatic int ieee80211_addba_response(struct ieee80211_node *ni,
216170530Ssam	struct ieee80211_tx_ampdu *tap,
217170530Ssam	int code, int baparamset, int batimeout);
218170530Ssamstatic void ieee80211_addba_stop(struct ieee80211_node *ni,
219170530Ssam	struct ieee80211_tx_ampdu *tap);
220223331Sadrianstatic void null_addba_response_timeout(struct ieee80211_node *ni,
221223331Sadrian	struct ieee80211_tx_ampdu *tap);
222223331Sadrian
223184280Ssamstatic void ieee80211_bar_response(struct ieee80211_node *ni,
224184280Ssam	struct ieee80211_tx_ampdu *tap, int status);
225184280Ssamstatic void ampdu_tx_stop(struct ieee80211_tx_ampdu *tap);
226184280Ssamstatic void bar_stop_timer(struct ieee80211_tx_ampdu *tap);
227191552Ssamstatic int ampdu_rx_start(struct ieee80211_node *, struct ieee80211_rx_ampdu *,
228191552Ssam	int baparamset, int batimeout, int baseqctl);
229191552Ssamstatic void ampdu_rx_stop(struct ieee80211_node *, struct ieee80211_rx_ampdu *);
230170530Ssam
231170530Ssamvoid
232170530Ssamieee80211_ht_attach(struct ieee80211com *ic)
233170530Ssam{
234170530Ssam	/* setup default aggregation policy */
235195377Ssam	ic->ic_recv_action = ieee80211_recv_action;
236170530Ssam	ic->ic_send_action = ieee80211_send_action;
237178354Ssam	ic->ic_ampdu_enable = ieee80211_ampdu_enable;
238170530Ssam	ic->ic_addba_request = ieee80211_addba_request;
239170530Ssam	ic->ic_addba_response = ieee80211_addba_response;
240223331Sadrian	ic->ic_addba_response_timeout = null_addba_response_timeout;
241170530Ssam	ic->ic_addba_stop = ieee80211_addba_stop;
242184280Ssam	ic->ic_bar_response = ieee80211_bar_response;
243191552Ssam	ic->ic_ampdu_rx_start = ampdu_rx_start;
244191552Ssam	ic->ic_ampdu_rx_stop = ampdu_rx_stop;
245170530Ssam
246173273Ssam	ic->ic_htprotmode = IEEE80211_PROT_RTSCTS;
247173273Ssam	ic->ic_curhtprotmode = IEEE80211_HTINFO_OPMODE_PURE;
248178354Ssam}
249173273Ssam
250178354Ssamvoid
251178354Ssamieee80211_ht_detach(struct ieee80211com *ic)
252178354Ssam{
253178354Ssam}
254173273Ssam
255178354Ssamvoid
256178354Ssamieee80211_ht_vattach(struct ieee80211vap *vap)
257178354Ssam{
258178354Ssam
259178354Ssam	/* driver can override defaults */
260178354Ssam	vap->iv_ampdu_rxmax = IEEE80211_HTCAP_MAXRXAMPDU_8K;
261178354Ssam	vap->iv_ampdu_density = IEEE80211_HTCAP_MPDUDENSITY_NA;
262178354Ssam	vap->iv_ampdu_limit = vap->iv_ampdu_rxmax;
263178354Ssam	vap->iv_amsdu_limit = vap->iv_htcaps & IEEE80211_HTCAP_MAXAMSDU;
264178354Ssam	/* tx aggregation traffic thresholds */
265178354Ssam	vap->iv_ampdu_mintraffic[WME_AC_BK] = 128;
266178354Ssam	vap->iv_ampdu_mintraffic[WME_AC_BE] = 64;
267178354Ssam	vap->iv_ampdu_mintraffic[WME_AC_VO] = 32;
268178354Ssam	vap->iv_ampdu_mintraffic[WME_AC_VI] = 32;
269178354Ssam
270178354Ssam	if (vap->iv_htcaps & IEEE80211_HTC_HT) {
271170530Ssam		/*
272173273Ssam		 * Device is HT capable; enable all HT-related
273173273Ssam		 * facilities by default.
274170530Ssam		 * XXX these choices may be too aggressive.
275170530Ssam		 */
276193655Ssam		vap->iv_flags_ht |= IEEE80211_FHT_HT
277193655Ssam				 |  IEEE80211_FHT_HTCOMPAT
278193655Ssam				 ;
279178354Ssam		if (vap->iv_htcaps & IEEE80211_HTCAP_SHORTGI20)
280193655Ssam			vap->iv_flags_ht |= IEEE80211_FHT_SHORTGI20;
281173273Ssam		/* XXX infer from channel list? */
282178354Ssam		if (vap->iv_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
283193655Ssam			vap->iv_flags_ht |= IEEE80211_FHT_USEHT40;
284178354Ssam			if (vap->iv_htcaps & IEEE80211_HTCAP_SHORTGI40)
285193655Ssam				vap->iv_flags_ht |= IEEE80211_FHT_SHORTGI40;
286170530Ssam		}
287183256Ssam		/* enable RIFS if capable */
288183256Ssam		if (vap->iv_htcaps & IEEE80211_HTC_RIFS)
289193655Ssam			vap->iv_flags_ht |= IEEE80211_FHT_RIFS;
290183256Ssam
291170530Ssam		/* NB: A-MPDU and A-MSDU rx are mandated, these are tx only */
292193655Ssam		vap->iv_flags_ht |= IEEE80211_FHT_AMPDU_RX;
293178354Ssam		if (vap->iv_htcaps & IEEE80211_HTC_AMPDU)
294193655Ssam			vap->iv_flags_ht |= IEEE80211_FHT_AMPDU_TX;
295193655Ssam		vap->iv_flags_ht |= IEEE80211_FHT_AMSDU_RX;
296178354Ssam		if (vap->iv_htcaps & IEEE80211_HTC_AMSDU)
297193655Ssam			vap->iv_flags_ht |= IEEE80211_FHT_AMSDU_TX;
298170530Ssam	}
299178354Ssam	/* NB: disable default legacy WDS, too many issues right now */
300178354Ssam	if (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY)
301193655Ssam		vap->iv_flags_ht &= ~IEEE80211_FHT_HT;
302170530Ssam}
303170530Ssam
304170530Ssamvoid
305178354Ssamieee80211_ht_vdetach(struct ieee80211vap *vap)
306170530Ssam{
307170530Ssam}
308170530Ssam
309219598Sbschmidtstatic int
310228621Sbschmidtht_getrate(struct ieee80211com *ic, int index, enum ieee80211_phymode mode,
311228621Sbschmidt    int ratetype)
312219598Sbschmidt{
313219598Sbschmidt	int mword, rate;
314219598Sbschmidt
315219598Sbschmidt	mword = ieee80211_rate2media(ic, index | IEEE80211_RATE_MCS, mode);
316219598Sbschmidt	if (IFM_SUBTYPE(mword) != IFM_IEEE80211_MCS)
317219598Sbschmidt		return (0);
318219598Sbschmidt	switch (ratetype) {
319219598Sbschmidt	case 0:
320219598Sbschmidt		rate = ieee80211_htrates[index].ht20_rate_800ns;
321219598Sbschmidt		break;
322219598Sbschmidt	case 1:
323219598Sbschmidt		rate = ieee80211_htrates[index].ht20_rate_400ns;
324219598Sbschmidt		break;
325219598Sbschmidt	case 2:
326219598Sbschmidt		rate = ieee80211_htrates[index].ht40_rate_800ns;
327219598Sbschmidt		break;
328219598Sbschmidt	default:
329219598Sbschmidt		rate = ieee80211_htrates[index].ht40_rate_400ns;
330219598Sbschmidt		break;
331219598Sbschmidt	}
332219598Sbschmidt	return (rate);
333219598Sbschmidt}
334219598Sbschmidt
335219598Sbschmidtstatic struct printranges {
336219598Sbschmidt	int	minmcs;
337219598Sbschmidt	int	maxmcs;
338219598Sbschmidt	int	txstream;
339219598Sbschmidt	int	ratetype;
340219598Sbschmidt	int	htcapflags;
341219598Sbschmidt} ranges[] = {
342219598Sbschmidt	{  0,  7, 1, 0, 0 },
343219598Sbschmidt	{  8, 15, 2, 0, 0 },
344219598Sbschmidt	{ 16, 23, 3, 0, 0 },
345219598Sbschmidt	{ 24, 31, 4, 0, 0 },
346219598Sbschmidt	{ 32,  0, 1, 2, IEEE80211_HTC_TXMCS32 },
347219598Sbschmidt	{ 33, 38, 2, 0, IEEE80211_HTC_TXUNEQUAL },
348219598Sbschmidt	{ 39, 52, 3, 0, IEEE80211_HTC_TXUNEQUAL },
349219598Sbschmidt	{ 53, 76, 4, 0, IEEE80211_HTC_TXUNEQUAL },
350219598Sbschmidt	{  0,  0, 0, 0, 0 },
351219598Sbschmidt};
352219598Sbschmidt
353170530Ssamstatic void
354228621Sbschmidtht_rateprint(struct ieee80211com *ic, enum ieee80211_phymode mode, int ratetype)
355170530Ssam{
356219598Sbschmidt	struct ifnet *ifp = ic->ic_ifp;
357219598Sbschmidt	int minrate, maxrate;
358219598Sbschmidt	struct printranges *range;
359170530Ssam
360219598Sbschmidt	for (range = ranges; range->txstream != 0; range++) {
361219598Sbschmidt		if (ic->ic_txstream < range->txstream)
362170530Ssam			continue;
363219598Sbschmidt		if (range->htcapflags &&
364219598Sbschmidt		    (ic->ic_htcaps & range->htcapflags) == 0)
365219598Sbschmidt			continue;
366219598Sbschmidt		if (ratetype < range->ratetype)
367219598Sbschmidt			continue;
368219598Sbschmidt		minrate = ht_getrate(ic, range->minmcs, mode, ratetype);
369219598Sbschmidt		maxrate = ht_getrate(ic, range->maxmcs, mode, ratetype);
370219598Sbschmidt		if (range->maxmcs) {
371219598Sbschmidt			if_printf(ifp, "MCS %d-%d: %d%sMbps - %d%sMbps\n",
372219598Sbschmidt			    range->minmcs, range->maxmcs,
373219598Sbschmidt			    minrate/2, ((minrate & 0x1) != 0 ? ".5" : ""),
374219598Sbschmidt			    maxrate/2, ((maxrate & 0x1) != 0 ? ".5" : ""));
375219598Sbschmidt		} else {
376219598Sbschmidt			if_printf(ifp, "MCS %d: %d%sMbps\n", range->minmcs,
377219598Sbschmidt			    minrate/2, ((minrate & 0x1) != 0 ? ".5" : ""));
378205277Srpaulo		}
379170530Ssam	}
380170530Ssam}
381170530Ssam
382205277Srpaulostatic void
383228621Sbschmidtht_announce(struct ieee80211com *ic, enum ieee80211_phymode mode)
384205277Srpaulo{
385205277Srpaulo	struct ifnet *ifp = ic->ic_ifp;
386205277Srpaulo	const char *modestr = ieee80211_phymode_name[mode];
387219598Sbschmidt
388219606Sbschmidt	if_printf(ifp, "%s MCS 20MHz\n", modestr);
389219598Sbschmidt	ht_rateprint(ic, mode, 0);
390219598Sbschmidt	if (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20) {
391219606Sbschmidt		if_printf(ifp, "%s MCS 20MHz SGI\n", modestr);
392219598Sbschmidt		ht_rateprint(ic, mode, 1);
393219598Sbschmidt	}
394219598Sbschmidt	if (ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
395219606Sbschmidt		if_printf(ifp, "%s MCS 40MHz:\n", modestr);
396219598Sbschmidt		ht_rateprint(ic, mode, 2);
397219598Sbschmidt	}
398219598Sbschmidt	if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
399219598Sbschmidt	    (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40)) {
400219606Sbschmidt		if_printf(ifp, "%s MCS 40MHz SGI:\n", modestr);
401219598Sbschmidt		ht_rateprint(ic, mode, 3);
402219598Sbschmidt	}
403205277Srpaulo}
404205277Srpaulo
405170530Ssamvoid
406170530Ssamieee80211_ht_announce(struct ieee80211com *ic)
407170530Ssam{
408219598Sbschmidt	struct ifnet *ifp = ic->ic_ifp;
409219598Sbschmidt
410219598Sbschmidt	if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) ||
411219598Sbschmidt	    isset(ic->ic_modecaps, IEEE80211_MODE_11NG))
412219598Sbschmidt		if_printf(ifp, "%dT%dR\n", ic->ic_txstream, ic->ic_rxstream);
413170530Ssam	if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA))
414219598Sbschmidt		ht_announce(ic, IEEE80211_MODE_11NA);
415170530Ssam	if (isset(ic->ic_modecaps, IEEE80211_MODE_11NG))
416219598Sbschmidt		ht_announce(ic, IEEE80211_MODE_11NG);
417170530Ssam}
418170530Ssam
419219602Sbschmidtstatic struct ieee80211_htrateset htrateset;
420219602Sbschmidt
421170530Ssamconst struct ieee80211_htrateset *
422170530Ssamieee80211_get_suphtrates(struct ieee80211com *ic,
423219602Sbschmidt    const struct ieee80211_channel *c)
424170530Ssam{
425219602Sbschmidt#define	ADDRATE(x)	do {						\
426219602Sbschmidt	htrateset.rs_rates[htrateset.rs_nrates] = x;			\
427219602Sbschmidt	htrateset.rs_nrates++;						\
428219602Sbschmidt} while (0)
429219602Sbschmidt	int i;
430219602Sbschmidt
431219602Sbschmidt	memset(&htrateset, 0, sizeof(struct ieee80211_htrateset));
432219602Sbschmidt	for (i = 0; i < ic->ic_txstream * 8; i++)
433219602Sbschmidt		ADDRATE(i);
434219602Sbschmidt	if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
435219602Sbschmidt	    (ic->ic_htcaps & IEEE80211_HTC_TXMCS32))
436229284Sbschmidt		ADDRATE(32);
437219602Sbschmidt	if (ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL) {
438219602Sbschmidt		if (ic->ic_txstream >= 2) {
439219602Sbschmidt			 for (i = 33; i <= 38; i++)
440219602Sbschmidt				ADDRATE(i);
441219602Sbschmidt		}
442219602Sbschmidt		if (ic->ic_txstream >= 3) {
443219602Sbschmidt			for (i = 39; i <= 52; i++)
444219602Sbschmidt				ADDRATE(i);
445219602Sbschmidt		}
446219602Sbschmidt		if (ic->ic_txstream == 4) {
447219602Sbschmidt			for (i = 53; i <= 76; i++)
448219602Sbschmidt				ADDRATE(i);
449219602Sbschmidt		}
450219602Sbschmidt	}
451219602Sbschmidt	return &htrateset;
452219602Sbschmidt#undef	ADDRATE
453170530Ssam}
454170530Ssam
455170530Ssam/*
456170530Ssam * Receive processing.
457170530Ssam */
458170530Ssam
459170530Ssam/*
460170530Ssam * Decap the encapsulated A-MSDU frames and dispatch all but
461170530Ssam * the last for delivery.  The last frame is returned for
462170530Ssam * delivery via the normal path.
463170530Ssam */
464170530Ssamstruct mbuf *
465170530Ssamieee80211_decap_amsdu(struct ieee80211_node *ni, struct mbuf *m)
466170530Ssam{
467178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
468173462Ssam	int framelen;
469170530Ssam	struct mbuf *n;
470170530Ssam
471170530Ssam	/* discard 802.3 header inserted by ieee80211_decap */
472170530Ssam	m_adj(m, sizeof(struct ether_header));
473170530Ssam
474178354Ssam	vap->iv_stats.is_amsdu_decap++;
475170530Ssam
476170530Ssam	for (;;) {
477170530Ssam		/*
478170530Ssam		 * Decap the first frame, bust it apart from the
479170530Ssam		 * remainder and deliver.  We leave the last frame
480170530Ssam		 * delivery to the caller (for consistency with other
481170530Ssam		 * code paths, could also do it here).
482170530Ssam		 */
483170530Ssam		m = ieee80211_decap1(m, &framelen);
484170530Ssam		if (m == NULL) {
485178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
486173462Ssam			    ni->ni_macaddr, "a-msdu", "%s", "decap failed");
487178354Ssam			vap->iv_stats.is_amsdu_tooshort++;
488170530Ssam			return NULL;
489170530Ssam		}
490173462Ssam		if (m->m_pkthdr.len == framelen)
491170530Ssam			break;
492170530Ssam		n = m_split(m, framelen, M_NOWAIT);
493170530Ssam		if (n == NULL) {
494178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
495170530Ssam			    ni->ni_macaddr, "a-msdu",
496170530Ssam			    "%s", "unable to split encapsulated frames");
497178354Ssam			vap->iv_stats.is_amsdu_split++;
498170530Ssam			m_freem(m);			/* NB: must reclaim */
499170530Ssam			return NULL;
500170530Ssam		}
501178354Ssam		vap->iv_deliver_data(vap, ni, m);
502170530Ssam
503170530Ssam		/*
504170530Ssam		 * Remove frame contents; each intermediate frame
505170530Ssam		 * is required to be aligned to a 4-byte boundary.
506170530Ssam		 */
507170530Ssam		m = n;
508170530Ssam		m_adj(m, roundup2(framelen, 4) - framelen);	/* padding */
509170530Ssam	}
510170530Ssam	return m;				/* last delivered by caller */
511170530Ssam}
512170530Ssam
513170530Ssam/*
514170530Ssam * Purge all frames in the A-MPDU re-order queue.
515170530Ssam */
516170530Ssamstatic void
517170530Ssamampdu_rx_purge(struct ieee80211_rx_ampdu *rap)
518170530Ssam{
519170530Ssam	struct mbuf *m;
520170530Ssam	int i;
521170530Ssam
522170530Ssam	for (i = 0; i < rap->rxa_wnd; i++) {
523170530Ssam		m = rap->rxa_m[i];
524170530Ssam		if (m != NULL) {
525170530Ssam			rap->rxa_m[i] = NULL;
526170530Ssam			rap->rxa_qbytes -= m->m_pkthdr.len;
527170530Ssam			m_freem(m);
528170530Ssam			if (--rap->rxa_qframes == 0)
529170530Ssam				break;
530170530Ssam		}
531170530Ssam	}
532170530Ssam	KASSERT(rap->rxa_qbytes == 0 && rap->rxa_qframes == 0,
533170530Ssam	    ("lost %u data, %u frames on ampdu rx q",
534170530Ssam	    rap->rxa_qbytes, rap->rxa_qframes));
535170530Ssam}
536170530Ssam
537170530Ssam/*
538178354Ssam * Start A-MPDU rx/re-order processing for the specified TID.
539178354Ssam */
540191552Ssamstatic int
541191552Ssamampdu_rx_start(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap,
542191552Ssam	int baparamset, int batimeout, int baseqctl)
543178354Ssam{
544191552Ssam	int bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
545191552Ssam
546178354Ssam	if (rap->rxa_flags & IEEE80211_AGGR_RUNNING) {
547178354Ssam		/*
548178354Ssam		 * AMPDU previously setup and not terminated with a DELBA,
549178354Ssam		 * flush the reorder q's in case anything remains.
550178354Ssam		 */
551178354Ssam		ampdu_rx_purge(rap);
552178354Ssam	}
553178354Ssam	memset(rap, 0, sizeof(*rap));
554178354Ssam	rap->rxa_wnd = (bufsiz == 0) ?
555178354Ssam	    IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
556191552Ssam	rap->rxa_start = MS(baseqctl, IEEE80211_BASEQ_START);
557178354Ssam	rap->rxa_flags |=  IEEE80211_AGGR_RUNNING | IEEE80211_AGGR_XCHGPEND;
558191552Ssam
559191552Ssam	return 0;
560178354Ssam}
561178354Ssam
562178354Ssam/*
563170530Ssam * Stop A-MPDU rx processing for the specified TID.
564170530Ssam */
565170530Ssamstatic void
566191552Ssamampdu_rx_stop(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
567170530Ssam{
568205277Srpaulo
569170530Ssam	ampdu_rx_purge(rap);
570178354Ssam	rap->rxa_flags &= ~(IEEE80211_AGGR_RUNNING | IEEE80211_AGGR_XCHGPEND);
571170530Ssam}
572170530Ssam
573170530Ssam/*
574170530Ssam * Dispatch a frame from the A-MPDU reorder queue.  The
575170530Ssam * frame is fed back into ieee80211_input marked with an
576183247Ssam * M_AMPDU_MPDU flag so it doesn't come back to us (it also
577170530Ssam * permits ieee80211_input to optimize re-processing).
578170530Ssam */
579170530Ssamstatic __inline void
580170530Ssamampdu_dispatch(struct ieee80211_node *ni, struct mbuf *m)
581170530Ssam{
582183247Ssam	m->m_flags |= M_AMPDU_MPDU;	/* bypass normal processing */
583192468Ssam	/* NB: rssi and noise are ignored w/ M_AMPDU_MPDU set */
584192468Ssam	(void) ieee80211_input(ni, m, 0, 0);
585170530Ssam}
586170530Ssam
587170530Ssam/*
588170530Ssam * Dispatch as many frames as possible from the re-order queue.
589170530Ssam * Frames will always be "at the front"; we process all frames
590170530Ssam * up to the first empty slot in the window.  On completion we
591170530Ssam * cleanup state if there are still pending frames in the current
592170530Ssam * BA window.  We assume the frame at slot 0 is already handled
593170530Ssam * by the caller; we always start at slot 1.
594170530Ssam */
595170530Ssamstatic void
596170530Ssamampdu_rx_dispatch(struct ieee80211_rx_ampdu *rap, struct ieee80211_node *ni)
597170530Ssam{
598178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
599170530Ssam	struct mbuf *m;
600170530Ssam	int i;
601170530Ssam
602170530Ssam	/* flush run of frames */
603170530Ssam	for (i = 1; i < rap->rxa_wnd; i++) {
604170530Ssam		m = rap->rxa_m[i];
605170530Ssam		if (m == NULL)
606170530Ssam			break;
607170530Ssam		rap->rxa_m[i] = NULL;
608170530Ssam		rap->rxa_qbytes -= m->m_pkthdr.len;
609170530Ssam		rap->rxa_qframes--;
610170530Ssam
611170530Ssam		ampdu_dispatch(ni, m);
612170530Ssam	}
613170530Ssam	/*
614170530Ssam	 * If frames remain, copy the mbuf pointers down so
615170530Ssam	 * they correspond to the offsets in the new window.
616170530Ssam	 */
617170530Ssam	if (rap->rxa_qframes != 0) {
618170530Ssam		int n = rap->rxa_qframes, j;
619170530Ssam		for (j = i+1; j < rap->rxa_wnd; j++) {
620170530Ssam			if (rap->rxa_m[j] != NULL) {
621170530Ssam				rap->rxa_m[j-i] = rap->rxa_m[j];
622170530Ssam				rap->rxa_m[j] = NULL;
623170530Ssam				if (--n == 0)
624170530Ssam					break;
625170530Ssam			}
626170530Ssam		}
627170530Ssam		KASSERT(n == 0, ("lost %d frames", n));
628178354Ssam		vap->iv_stats.is_ampdu_rx_copy += rap->rxa_qframes;
629170530Ssam	}
630173273Ssam	/*
631173273Ssam	 * Adjust the start of the BA window to
632173273Ssam	 * reflect the frames just dispatched.
633173273Ssam	 */
634173273Ssam	rap->rxa_start = IEEE80211_SEQ_ADD(rap->rxa_start, i);
635178354Ssam	vap->iv_stats.is_ampdu_rx_oor += i;
636170530Ssam}
637170530Ssam
638173273Ssam#ifdef IEEE80211_AMPDU_AGE
639170530Ssam/*
640173273Ssam * Dispatch all frames in the A-MPDU re-order queue.
641170530Ssam */
642170530Ssamstatic void
643173273Ssamampdu_rx_flush(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
644170530Ssam{
645178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
646170530Ssam	struct mbuf *m;
647170530Ssam	int i;
648170530Ssam
649173273Ssam	for (i = 0; i < rap->rxa_wnd; i++) {
650170530Ssam		m = rap->rxa_m[i];
651170530Ssam		if (m == NULL)
652170530Ssam			continue;
653170530Ssam		rap->rxa_m[i] = NULL;
654170530Ssam		rap->rxa_qbytes -= m->m_pkthdr.len;
655173273Ssam		rap->rxa_qframes--;
656178354Ssam		vap->iv_stats.is_ampdu_rx_oor++;
657173273Ssam
658170530Ssam		ampdu_dispatch(ni, m);
659173273Ssam		if (rap->rxa_qframes == 0)
660170530Ssam			break;
661170530Ssam	}
662170530Ssam}
663173273Ssam#endif /* IEEE80211_AMPDU_AGE */
664170530Ssam
665170530Ssam/*
666173273Ssam * Dispatch all frames in the A-MPDU re-order queue
667173273Ssam * preceding the specified sequence number.  This logic
668173273Ssam * handles window moves due to a received MSDU or BAR.
669173273Ssam */
670173273Ssamstatic void
671173273Ssamampdu_rx_flush_upto(struct ieee80211_node *ni,
672173273Ssam	struct ieee80211_rx_ampdu *rap, ieee80211_seq winstart)
673173273Ssam{
674178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
675173273Ssam	struct mbuf *m;
676173273Ssam	ieee80211_seq seqno;
677173273Ssam	int i;
678173273Ssam
679173273Ssam	/*
680173273Ssam	 * Flush any complete MSDU's with a sequence number lower
681173273Ssam	 * than winstart.  Gaps may exist.  Note that we may actually
682173273Ssam	 * dispatch frames past winstart if a run continues; this is
683173273Ssam	 * an optimization that avoids having to do a separate pass
684173273Ssam	 * to dispatch frames after moving the BA window start.
685173273Ssam	 */
686173273Ssam	seqno = rap->rxa_start;
687173273Ssam	for (i = 0; i < rap->rxa_wnd; i++) {
688173273Ssam		m = rap->rxa_m[i];
689173273Ssam		if (m != NULL) {
690173273Ssam			rap->rxa_m[i] = NULL;
691173273Ssam			rap->rxa_qbytes -= m->m_pkthdr.len;
692173273Ssam			rap->rxa_qframes--;
693178354Ssam			vap->iv_stats.is_ampdu_rx_oor++;
694173273Ssam
695173273Ssam			ampdu_dispatch(ni, m);
696173273Ssam		} else {
697173273Ssam			if (!IEEE80211_SEQ_BA_BEFORE(seqno, winstart))
698173273Ssam				break;
699173273Ssam		}
700173273Ssam		seqno = IEEE80211_SEQ_INC(seqno);
701173273Ssam	}
702173273Ssam	/*
703173273Ssam	 * If frames remain, copy the mbuf pointers down so
704173273Ssam	 * they correspond to the offsets in the new window.
705173273Ssam	 */
706173273Ssam	if (rap->rxa_qframes != 0) {
707173273Ssam		int n = rap->rxa_qframes, j;
708178354Ssam
709178354Ssam		/* NB: this loop assumes i > 0 and/or rxa_m[0] is NULL */
710178354Ssam		KASSERT(rap->rxa_m[0] == NULL,
711178354Ssam		    ("%s: BA window slot 0 occupied", __func__));
712173273Ssam		for (j = i+1; j < rap->rxa_wnd; j++) {
713173273Ssam			if (rap->rxa_m[j] != NULL) {
714173273Ssam				rap->rxa_m[j-i] = rap->rxa_m[j];
715173273Ssam				rap->rxa_m[j] = NULL;
716173273Ssam				if (--n == 0)
717173273Ssam					break;
718173273Ssam			}
719173273Ssam		}
720173273Ssam		KASSERT(n == 0, ("%s: lost %d frames, qframes %d off %d "
721173273Ssam		    "BA win <%d:%d> winstart %d",
722173273Ssam		    __func__, n, rap->rxa_qframes, i, rap->rxa_start,
723173273Ssam		    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
724173273Ssam		    winstart));
725178354Ssam		vap->iv_stats.is_ampdu_rx_copy += rap->rxa_qframes;
726173273Ssam	}
727173273Ssam	/*
728173273Ssam	 * Move the start of the BA window; we use the
729173273Ssam	 * sequence number of the last MSDU that was
730173273Ssam	 * passed up the stack+1 or winstart if stopped on
731173273Ssam	 * a gap in the reorder buffer.
732173273Ssam	 */
733173273Ssam	rap->rxa_start = seqno;
734173273Ssam}
735173273Ssam
736173273Ssam/*
737170530Ssam * Process a received QoS data frame for an HT station.  Handle
738170530Ssam * A-MPDU reordering: if this frame is received out of order
739170530Ssam * and falls within the BA window hold onto it.  Otherwise if
740173273Ssam * this frame completes a run, flush any pending frames.  We
741170530Ssam * return 1 if the frame is consumed.  A 0 is returned if
742170530Ssam * the frame should be processed normally by the caller.
743170530Ssam */
744170530Ssamint
745170530Ssamieee80211_ampdu_reorder(struct ieee80211_node *ni, struct mbuf *m)
746170530Ssam{
747170530Ssam#define	IEEE80211_FC0_QOSDATA \
748170530Ssam	(IEEE80211_FC0_TYPE_DATA|IEEE80211_FC0_SUBTYPE_QOS|IEEE80211_FC0_VERSION_0)
749173273Ssam#define	PROCESS		0	/* caller should process frame */
750173273Ssam#define	CONSUMED	1	/* frame consumed, caller does nothing */
751178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
752170530Ssam	struct ieee80211_qosframe *wh;
753170530Ssam	struct ieee80211_rx_ampdu *rap;
754170530Ssam	ieee80211_seq rxseq;
755170530Ssam	uint8_t tid;
756170530Ssam	int off;
757170530Ssam
758183247Ssam	KASSERT((m->m_flags & (M_AMPDU | M_AMPDU_MPDU)) == M_AMPDU,
759183247Ssam	    ("!a-mpdu or already re-ordered, flags 0x%x", m->m_flags));
760170530Ssam	KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT sta"));
761170530Ssam
762170530Ssam	/* NB: m_len known to be sufficient */
763170530Ssam	wh = mtod(m, struct ieee80211_qosframe *);
764183247Ssam	if (wh->i_fc[0] != IEEE80211_FC0_QOSDATA) {
765183247Ssam		/*
766183247Ssam		 * Not QoS data, shouldn't get here but just
767183247Ssam		 * return it to the caller for processing.
768183247Ssam		 */
769183247Ssam		return PROCESS;
770183247Ssam	}
771193840Ssam	if (IEEE80211_IS_DSTODS(wh))
772173273Ssam		tid = ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0];
773173273Ssam	else
774173273Ssam		tid = wh->i_qos[0];
775173273Ssam	tid &= IEEE80211_QOS_TID;
776170530Ssam	rap = &ni->ni_rx_ampdu[tid];
777170530Ssam	if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
778170530Ssam		/*
779170530Ssam		 * No ADDBA request yet, don't touch.
780170530Ssam		 */
781173273Ssam		return PROCESS;
782170530Ssam	}
783182827Ssam	rxseq = le16toh(*(uint16_t *)wh->i_seq);
784182827Ssam	if ((rxseq & IEEE80211_SEQ_FRAG_MASK) != 0) {
785182827Ssam		/*
786182827Ssam		 * Fragments are not allowed; toss.
787182827Ssam		 */
788182827Ssam		IEEE80211_DISCARD_MAC(vap,
789182827Ssam		    IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
790182827Ssam		    "A-MPDU", "fragment, rxseq 0x%x tid %u%s", rxseq, tid,
791182827Ssam		    wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
792182827Ssam		vap->iv_stats.is_ampdu_rx_drop++;
793182827Ssam		IEEE80211_NODE_STAT(ni, rx_drop);
794182827Ssam		m_freem(m);
795182827Ssam		return CONSUMED;
796182827Ssam	}
797182827Ssam	rxseq >>= IEEE80211_SEQ_SEQ_SHIFT;
798173273Ssam	rap->rxa_nframes++;
799173273Ssamagain:
800170530Ssam	if (rxseq == rap->rxa_start) {
801170530Ssam		/*
802170530Ssam		 * First frame in window.
803170530Ssam		 */
804170530Ssam		if (rap->rxa_qframes != 0) {
805170530Ssam			/*
806170530Ssam			 * Dispatch as many packets as we can.
807170530Ssam			 */
808170530Ssam			KASSERT(rap->rxa_m[0] == NULL, ("unexpected dup"));
809170530Ssam			ampdu_dispatch(ni, m);
810170530Ssam			ampdu_rx_dispatch(rap, ni);
811173273Ssam			return CONSUMED;
812170530Ssam		} else {
813170530Ssam			/*
814170530Ssam			 * In order; advance window and notify
815170530Ssam			 * caller to dispatch directly.
816170530Ssam			 */
817170530Ssam			rap->rxa_start = IEEE80211_SEQ_INC(rxseq);
818173273Ssam			return PROCESS;
819170530Ssam		}
820170530Ssam	}
821170530Ssam	/*
822173273Ssam	 * Frame is out of order; store if in the BA window.
823170530Ssam	 */
824170530Ssam	/* calculate offset in BA window */
825170530Ssam	off = IEEE80211_SEQ_SUB(rxseq, rap->rxa_start);
826173273Ssam	if (off < rap->rxa_wnd) {
827170530Ssam		/*
828173273Ssam		 * Common case (hopefully): in the BA window.
829205277Srpaulo		 * Sec 9.10.7.6.2 a) (p.137)
830173273Ssam		 */
831173273Ssam#ifdef IEEE80211_AMPDU_AGE
832173273Ssam		/*
833173273Ssam		 * Check for frames sitting too long in the reorder queue.
834173273Ssam		 * This should only ever happen if frames are not delivered
835173273Ssam		 * without the sender otherwise notifying us (e.g. with a
836173273Ssam		 * BAR to move the window).  Typically this happens because
837173273Ssam		 * of vendor bugs that cause the sequence number to jump.
838173273Ssam		 * When this happens we get a gap in the reorder queue that
839173273Ssam		 * leaves frame sitting on the queue until they get pushed
840173273Ssam		 * out due to window moves.  When the vendor does not send
841173273Ssam		 * BAR this move only happens due to explicit packet sends
842170530Ssam		 *
843173273Ssam		 * NB: we only track the time of the oldest frame in the
844173273Ssam		 * reorder q; this means that if we flush we might push
845173273Ssam		 * frames that still "new"; if this happens then subsequent
846173273Ssam		 * frames will result in BA window moves which cost something
847173273Ssam		 * but is still better than a big throughput dip.
848170530Ssam		 */
849173273Ssam		if (rap->rxa_qframes != 0) {
850173273Ssam			/* XXX honor batimeout? */
851173273Ssam			if (ticks - rap->rxa_age > ieee80211_ampdu_age) {
852173273Ssam				/*
853173273Ssam				 * Too long since we received the first
854173273Ssam				 * frame; flush the reorder buffer.
855173273Ssam				 */
856173273Ssam				if (rap->rxa_qframes != 0) {
857178354Ssam					vap->iv_stats.is_ampdu_rx_age +=
858173273Ssam					    rap->rxa_qframes;
859173273Ssam					ampdu_rx_flush(ni, rap);
860173273Ssam				}
861173273Ssam				rap->rxa_start = IEEE80211_SEQ_INC(rxseq);
862173273Ssam				return PROCESS;
863173273Ssam			}
864173273Ssam		} else {
865173273Ssam			/*
866173273Ssam			 * First frame, start aging timer.
867173273Ssam			 */
868173273Ssam			rap->rxa_age = ticks;
869173273Ssam		}
870173273Ssam#endif /* IEEE80211_AMPDU_AGE */
871173273Ssam		/* save packet */
872173273Ssam		if (rap->rxa_m[off] == NULL) {
873173273Ssam			rap->rxa_m[off] = m;
874173273Ssam			rap->rxa_qframes++;
875173273Ssam			rap->rxa_qbytes += m->m_pkthdr.len;
876178354Ssam			vap->iv_stats.is_ampdu_rx_reorder++;
877173273Ssam		} else {
878178354Ssam			IEEE80211_DISCARD_MAC(vap,
879173273Ssam			    IEEE80211_MSG_INPUT | IEEE80211_MSG_11N,
880173273Ssam			    ni->ni_macaddr, "a-mpdu duplicate",
881173273Ssam			    "seqno %u tid %u BA win <%u:%u>",
882173273Ssam			    rxseq, tid, rap->rxa_start,
883173273Ssam			    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1));
884178354Ssam			vap->iv_stats.is_rx_dup++;
885173273Ssam			IEEE80211_NODE_STAT(ni, rx_dup);
886173273Ssam			m_freem(m);
887173273Ssam		}
888173273Ssam		return CONSUMED;
889173273Ssam	}
890173273Ssam	if (off < IEEE80211_SEQ_BA_RANGE) {
891173273Ssam		/*
892173273Ssam		 * Outside the BA window, but within range;
893173273Ssam		 * flush the reorder q and move the window.
894205277Srpaulo		 * Sec 9.10.7.6.2 b) (p.138)
895173273Ssam		 */
896178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
897173273Ssam		    "move BA win <%u:%u> (%u frames) rxseq %u tid %u",
898170530Ssam		    rap->rxa_start,
899173273Ssam		    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
900170530Ssam		    rap->rxa_qframes, rxseq, tid);
901178354Ssam		vap->iv_stats.is_ampdu_rx_move++;
902170530Ssam
903173273Ssam		/*
904173273Ssam		 * The spec says to flush frames up to but not including:
905173273Ssam		 * 	WinStart_B = rxseq - rap->rxa_wnd + 1
906173273Ssam		 * Then insert the frame or notify the caller to process
907173273Ssam		 * it immediately.  We can safely do this by just starting
908173273Ssam		 * over again because we know the frame will now be within
909173273Ssam		 * the BA window.
910173273Ssam		 */
911173273Ssam		/* NB: rxa_wnd known to be >0 */
912173273Ssam		ampdu_rx_flush_upto(ni, rap,
913173273Ssam		    IEEE80211_SEQ_SUB(rxseq, rap->rxa_wnd-1));
914173273Ssam		goto again;
915170530Ssam	} else {
916170530Ssam		/*
917173273Ssam		 * Outside the BA window and out of range; toss.
918205277Srpaulo		 * Sec 9.10.7.6.2 c) (p.138)
919170530Ssam		 */
920178354Ssam		IEEE80211_DISCARD_MAC(vap,
921173273Ssam		    IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
922178354Ssam		    "MPDU", "BA win <%u:%u> (%u frames) rxseq %u tid %u%s",
923173273Ssam		    rap->rxa_start,
924173273Ssam		    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
925173273Ssam		    rap->rxa_qframes, rxseq, tid,
926173273Ssam		    wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
927178354Ssam		vap->iv_stats.is_ampdu_rx_drop++;
928173273Ssam		IEEE80211_NODE_STAT(ni, rx_drop);
929170530Ssam		m_freem(m);
930173273Ssam		return CONSUMED;
931170530Ssam	}
932173273Ssam#undef CONSUMED
933173273Ssam#undef PROCESS
934170530Ssam#undef IEEE80211_FC0_QOSDATA
935170530Ssam}
936170530Ssam
937170530Ssam/*
938170530Ssam * Process a BAR ctl frame.  Dispatch all frames up to
939170530Ssam * the sequence number of the frame.  If this frame is
940173273Ssam * out of range it's discarded.
941170530Ssam */
942170530Ssamvoid
943170530Ssamieee80211_recv_bar(struct ieee80211_node *ni, struct mbuf *m0)
944170530Ssam{
945178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
946170530Ssam	struct ieee80211_frame_bar *wh;
947170530Ssam	struct ieee80211_rx_ampdu *rap;
948170530Ssam	ieee80211_seq rxseq;
949170530Ssam	int tid, off;
950170530Ssam
951173273Ssam	if (!ieee80211_recv_bar_ena) {
952173273Ssam#if 0
953178354Ssam		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_11N,
954173273Ssam		    ni->ni_macaddr, "BAR", "%s", "processing disabled");
955173273Ssam#endif
956178354Ssam		vap->iv_stats.is_ampdu_bar_bad++;
957173273Ssam		return;
958173273Ssam	}
959170530Ssam	wh = mtod(m0, struct ieee80211_frame_bar *);
960170530Ssam	/* XXX check basic BAR */
961170530Ssam	tid = MS(le16toh(wh->i_ctl), IEEE80211_BAR_TID);
962170530Ssam	rap = &ni->ni_rx_ampdu[tid];
963170530Ssam	if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
964170530Ssam		/*
965170530Ssam		 * No ADDBA request yet, don't touch.
966170530Ssam		 */
967178354Ssam		IEEE80211_DISCARD_MAC(vap,
968170530Ssam		    IEEE80211_MSG_INPUT | IEEE80211_MSG_11N,
969170530Ssam		    ni->ni_macaddr, "BAR", "no BA stream, tid %u", tid);
970178354Ssam		vap->iv_stats.is_ampdu_bar_bad++;
971170530Ssam		return;
972170530Ssam	}
973178354Ssam	vap->iv_stats.is_ampdu_bar_rx++;
974170530Ssam	rxseq = le16toh(wh->i_seq) >> IEEE80211_SEQ_SEQ_SHIFT;
975173273Ssam	if (rxseq == rap->rxa_start)
976173273Ssam		return;
977170530Ssam	/* calculate offset in BA window */
978170530Ssam	off = IEEE80211_SEQ_SUB(rxseq, rap->rxa_start);
979173273Ssam	if (off < IEEE80211_SEQ_BA_RANGE) {
980170530Ssam		/*
981173273Ssam		 * Flush the reorder q up to rxseq and move the window.
982205277Srpaulo		 * Sec 9.10.7.6.3 a) (p.138)
983170530Ssam		 */
984178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
985173273Ssam		    "BAR moves BA win <%u:%u> (%u frames) rxseq %u tid %u",
986170530Ssam		    rap->rxa_start,
987173273Ssam		    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
988173273Ssam		    rap->rxa_qframes, rxseq, tid);
989178354Ssam		vap->iv_stats.is_ampdu_bar_move++;
990173273Ssam
991173273Ssam		ampdu_rx_flush_upto(ni, rap, rxseq);
992173273Ssam		if (off >= rap->rxa_wnd) {
993173273Ssam			/*
994173273Ssam			 * BAR specifies a window start to the right of BA
995173273Ssam			 * window; we must move it explicitly since
996173273Ssam			 * ampdu_rx_flush_upto will not.
997173273Ssam			 */
998173273Ssam			rap->rxa_start = rxseq;
999170530Ssam		}
1000173273Ssam	} else {
1001170530Ssam		/*
1002173273Ssam		 * Out of range; toss.
1003205277Srpaulo		 * Sec 9.10.7.6.3 b) (p.138)
1004170530Ssam		 */
1005178354Ssam		IEEE80211_DISCARD_MAC(vap,
1006173273Ssam		    IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
1007173273Ssam		    "BAR", "BA win <%u:%u> (%u frames) rxseq %u tid %u%s",
1008173273Ssam		    rap->rxa_start,
1009173273Ssam		    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
1010173273Ssam		    rap->rxa_qframes, rxseq, tid,
1011173273Ssam		    wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
1012178354Ssam		vap->iv_stats.is_ampdu_bar_oow++;
1013173273Ssam		IEEE80211_NODE_STAT(ni, rx_drop);
1014170530Ssam	}
1015170530Ssam}
1016170530Ssam
1017170530Ssam/*
1018170530Ssam * Setup HT-specific state in a node.  Called only
1019170530Ssam * when HT use is negotiated so we don't do extra
1020170530Ssam * work for temporary and/or legacy sta's.
1021170530Ssam */
1022170530Ssamvoid
1023183254Ssamieee80211_ht_node_init(struct ieee80211_node *ni)
1024170530Ssam{
1025170530Ssam	struct ieee80211_tx_ampdu *tap;
1026234324Sadrian	int tid;
1027170530Ssam
1028173273Ssam	if (ni->ni_flags & IEEE80211_NODE_HT) {
1029173273Ssam		/*
1030173273Ssam		 * Clean AMPDU state on re-associate.  This handles the case
1031173273Ssam		 * where a station leaves w/o notifying us and then returns
1032173273Ssam		 * before node is reaped for inactivity.
1033173273Ssam		 */
1034173273Ssam		ieee80211_ht_node_cleanup(ni);
1035173273Ssam	}
1036234324Sadrian	for (tid = 0; tid < WME_NUM_TID; tid++) {
1037234324Sadrian		tap = &ni->ni_tx_ampdu[tid];
1038234324Sadrian		tap->txa_tid = tid;
1039184280Ssam		tap->txa_ni = ni;
1040173273Ssam		/* NB: further initialization deferred */
1041170530Ssam	}
1042173273Ssam	ni->ni_flags |= IEEE80211_NODE_HT | IEEE80211_NODE_AMPDU;
1043170530Ssam}
1044170530Ssam
1045170530Ssam/*
1046170530Ssam * Cleanup HT-specific state in a node.  Called only
1047170530Ssam * when HT use has been marked.
1048170530Ssam */
1049170530Ssamvoid
1050170530Ssamieee80211_ht_node_cleanup(struct ieee80211_node *ni)
1051170530Ssam{
1052191552Ssam	struct ieee80211com *ic = ni->ni_ic;
1053170530Ssam	int i;
1054170530Ssam
1055170530Ssam	KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT node"));
1056170530Ssam
1057170530Ssam	/* XXX optimize this */
1058234324Sadrian	for (i = 0; i < WME_NUM_TID; i++) {
1059170530Ssam		struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[i];
1060184280Ssam		if (tap->txa_flags & IEEE80211_AGGR_SETUP)
1061184280Ssam			ampdu_tx_stop(tap);
1062170530Ssam	}
1063170530Ssam	for (i = 0; i < WME_NUM_TID; i++)
1064191552Ssam		ic->ic_ampdu_rx_stop(ni, &ni->ni_rx_ampdu[i]);
1065170530Ssam
1066170530Ssam	ni->ni_htcap = 0;
1067182828Ssam	ni->ni_flags &= ~IEEE80211_NODE_HT_ALL;
1068170530Ssam}
1069170530Ssam
1070178354Ssam/*
1071178354Ssam * Age out HT resources for a station.
1072178354Ssam */
1073178354Ssamvoid
1074178354Ssamieee80211_ht_node_age(struct ieee80211_node *ni)
1075178354Ssam{
1076178354Ssam#ifdef IEEE80211_AMPDU_AGE
1077178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1078178354Ssam	uint8_t tid;
1079178354Ssam#endif
1080178354Ssam
1081178354Ssam	KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT sta"));
1082178354Ssam
1083178354Ssam#ifdef IEEE80211_AMPDU_AGE
1084178354Ssam	for (tid = 0; tid < WME_NUM_TID; tid++) {
1085178354Ssam		struct ieee80211_rx_ampdu *rap;
1086178354Ssam
1087178354Ssam		rap = &ni->ni_rx_ampdu[tid];
1088178354Ssam		if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0)
1089178354Ssam			continue;
1090178354Ssam		if (rap->rxa_qframes == 0)
1091178354Ssam			continue;
1092178354Ssam		/*
1093178354Ssam		 * Check for frames sitting too long in the reorder queue.
1094178354Ssam		 * See above for more details on what's happening here.
1095178354Ssam		 */
1096178354Ssam		/* XXX honor batimeout? */
1097178354Ssam		if (ticks - rap->rxa_age > ieee80211_ampdu_age) {
1098178354Ssam			/*
1099178354Ssam			 * Too long since we received the first
1100178354Ssam			 * frame; flush the reorder buffer.
1101178354Ssam			 */
1102178354Ssam			vap->iv_stats.is_ampdu_rx_age += rap->rxa_qframes;
1103178354Ssam			ampdu_rx_flush(ni, rap);
1104178354Ssam		}
1105178354Ssam	}
1106178354Ssam#endif /* IEEE80211_AMPDU_AGE */
1107178354Ssam}
1108178354Ssam
1109173273Ssamstatic struct ieee80211_channel *
1110173273Ssamfindhtchan(struct ieee80211com *ic, struct ieee80211_channel *c, int htflags)
1111173273Ssam{
1112173273Ssam	return ieee80211_find_channel(ic, c->ic_freq,
1113173273Ssam	    (c->ic_flags &~ IEEE80211_CHAN_HT) | htflags);
1114173273Ssam}
1115173273Ssam
1116173273Ssam/*
1117173273Ssam * Adjust a channel to be HT/non-HT according to the vap's configuration.
1118173273Ssam */
1119173273Ssamstruct ieee80211_channel *
1120173273Ssamieee80211_ht_adjust_channel(struct ieee80211com *ic,
1121173273Ssam	struct ieee80211_channel *chan, int flags)
1122173273Ssam{
1123173273Ssam	struct ieee80211_channel *c;
1124173273Ssam
1125193655Ssam	if (flags & IEEE80211_FHT_HT) {
1126173273Ssam		/* promote to HT if possible */
1127193655Ssam		if (flags & IEEE80211_FHT_USEHT40) {
1128173273Ssam			if (!IEEE80211_IS_CHAN_HT40(chan)) {
1129173273Ssam				/* NB: arbitrarily pick ht40+ over ht40- */
1130173273Ssam				c = findhtchan(ic, chan, IEEE80211_CHAN_HT40U);
1131173273Ssam				if (c == NULL)
1132173273Ssam					c = findhtchan(ic, chan,
1133173273Ssam						IEEE80211_CHAN_HT40D);
1134173273Ssam				if (c == NULL)
1135173273Ssam					c = findhtchan(ic, chan,
1136173273Ssam						IEEE80211_CHAN_HT20);
1137173273Ssam				if (c != NULL)
1138173273Ssam					chan = c;
1139173273Ssam			}
1140173273Ssam		} else if (!IEEE80211_IS_CHAN_HT20(chan)) {
1141173273Ssam			c = findhtchan(ic, chan, IEEE80211_CHAN_HT20);
1142173273Ssam			if (c != NULL)
1143173273Ssam				chan = c;
1144173273Ssam		}
1145173273Ssam	} else if (IEEE80211_IS_CHAN_HT(chan)) {
1146173273Ssam		/* demote to legacy, HT use is disabled */
1147173273Ssam		c = ieee80211_find_channel(ic, chan->ic_freq,
1148173273Ssam		    chan->ic_flags &~ IEEE80211_CHAN_HT);
1149173273Ssam		if (c != NULL)
1150173273Ssam			chan = c;
1151173273Ssam	}
1152173273Ssam	return chan;
1153173273Ssam}
1154173273Ssam
1155173273Ssam/*
1156173273Ssam * Setup HT-specific state for a legacy WDS peer.
1157173273Ssam */
1158173273Ssamvoid
1159173273Ssamieee80211_ht_wds_init(struct ieee80211_node *ni)
1160173273Ssam{
1161178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1162173273Ssam	struct ieee80211_tx_ampdu *tap;
1163234324Sadrian	int tid;
1164173273Ssam
1165193655Ssam	KASSERT(vap->iv_flags_ht & IEEE80211_FHT_HT, ("no HT requested"));
1166173273Ssam
1167173273Ssam	/* XXX check scan cache in case peer has an ap and we have info */
1168173273Ssam	/*
1169173273Ssam	 * If setup with a legacy channel; locate an HT channel.
1170173273Ssam	 * Otherwise if the inherited channel (from a companion
1171173273Ssam	 * AP) is suitable use it so we use the same location
1172173273Ssam	 * for the extension channel).
1173173273Ssam	 */
1174178354Ssam	ni->ni_chan = ieee80211_ht_adjust_channel(ni->ni_ic,
1175178354Ssam	    ni->ni_chan, ieee80211_htchanflags(ni->ni_chan));
1176173273Ssam
1177173273Ssam	ni->ni_htcap = 0;
1178193655Ssam	if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20)
1179173273Ssam		ni->ni_htcap |= IEEE80211_HTCAP_SHORTGI20;
1180173273Ssam	if (IEEE80211_IS_CHAN_HT40(ni->ni_chan)) {
1181173273Ssam		ni->ni_htcap |= IEEE80211_HTCAP_CHWIDTH40;
1182173273Ssam		ni->ni_chw = 40;
1183173273Ssam		if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan))
1184173273Ssam			ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_ABOVE;
1185173273Ssam		else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan))
1186173273Ssam			ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_BELOW;
1187193655Ssam		if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40)
1188173273Ssam			ni->ni_htcap |= IEEE80211_HTCAP_SHORTGI40;
1189173273Ssam	} else {
1190173273Ssam		ni->ni_chw = 20;
1191173273Ssam		ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_NONE;
1192173273Ssam	}
1193173273Ssam	ni->ni_htctlchan = ni->ni_chan->ic_ieee;
1194193655Ssam	if (vap->iv_flags_ht & IEEE80211_FHT_RIFS)
1195183256Ssam		ni->ni_flags |= IEEE80211_NODE_RIFS;
1196183256Ssam	/* XXX does it make sense to enable SMPS? */
1197173273Ssam
1198173273Ssam	ni->ni_htopmode = 0;		/* XXX need protection state */
1199173273Ssam	ni->ni_htstbc = 0;		/* XXX need info */
1200173273Ssam
1201234324Sadrian	for (tid = 0; tid < WME_NUM_TID; tid++) {
1202234324Sadrian		tap = &ni->ni_tx_ampdu[tid];
1203234324Sadrian		tap->txa_tid = tid;
1204173273Ssam	}
1205193655Ssam	/* NB: AMPDU tx/rx governed by IEEE80211_FHT_AMPDU_{TX,RX} */
1206173273Ssam	ni->ni_flags |= IEEE80211_NODE_HT | IEEE80211_NODE_AMPDU;
1207173273Ssam}
1208173273Ssam
1209173273Ssam/*
1210173273Ssam * Notify hostap vaps of a change in the HTINFO ie.
1211173273Ssam */
1212173273Ssamstatic void
1213173273Ssamhtinfo_notify(struct ieee80211com *ic)
1214173273Ssam{
1215178354Ssam	struct ieee80211vap *vap;
1216178354Ssam	int first = 1;
1217178354Ssam
1218178354Ssam	IEEE80211_LOCK_ASSERT(ic);
1219178354Ssam
1220178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1221178354Ssam		if (vap->iv_opmode != IEEE80211_M_HOSTAP)
1222178354Ssam			continue;
1223183253Ssam		if (vap->iv_state != IEEE80211_S_RUN ||
1224183253Ssam		    !IEEE80211_IS_CHAN_HT(vap->iv_bss->ni_chan))
1225183253Ssam			continue;
1226178354Ssam		if (first) {
1227178354Ssam			IEEE80211_NOTE(vap,
1228178354Ssam			    IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N,
1229178354Ssam			    vap->iv_bss,
1230178354Ssam			    "HT bss occupancy change: %d sta, %d ht, "
1231178354Ssam			    "%d ht40%s, HT protmode now 0x%x"
1232178354Ssam			    , ic->ic_sta_assoc
1233178354Ssam			    , ic->ic_ht_sta_assoc
1234178354Ssam			    , ic->ic_ht40_sta_assoc
1235193655Ssam			    , (ic->ic_flags_ht & IEEE80211_FHT_NONHT_PR) ?
1236178354Ssam				 ", non-HT sta present" : ""
1237178354Ssam			    , ic->ic_curhtprotmode);
1238178354Ssam			first = 0;
1239178354Ssam		}
1240178354Ssam		ieee80211_beacon_notify(vap, IEEE80211_BEACON_HTINFO);
1241178354Ssam	}
1242173273Ssam}
1243173273Ssam
1244173273Ssam/*
1245173273Ssam * Calculate HT protection mode from current
1246173273Ssam * state and handle updates.
1247173273Ssam */
1248173273Ssamstatic void
1249173273Ssamhtinfo_update(struct ieee80211com *ic)
1250173273Ssam{
1251173273Ssam	uint8_t protmode;
1252173273Ssam
1253178354Ssam	if (ic->ic_sta_assoc != ic->ic_ht_sta_assoc) {
1254178354Ssam		protmode = IEEE80211_HTINFO_OPMODE_MIXED
1255178354Ssam			 | IEEE80211_HTINFO_NONHT_PRESENT;
1256193655Ssam	} else if (ic->ic_flags_ht & IEEE80211_FHT_NONHT_PR) {
1257173273Ssam		protmode = IEEE80211_HTINFO_OPMODE_PROTOPT
1258178354Ssam			 | IEEE80211_HTINFO_NONHT_PRESENT;
1259178354Ssam	} else if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
1260178354Ssam	    IEEE80211_IS_CHAN_HT40(ic->ic_bsschan) &&
1261173273Ssam	    ic->ic_sta_assoc != ic->ic_ht40_sta_assoc) {
1262173273Ssam		protmode = IEEE80211_HTINFO_OPMODE_HT20PR;
1263173273Ssam	} else {
1264173273Ssam		protmode = IEEE80211_HTINFO_OPMODE_PURE;
1265173273Ssam	}
1266173273Ssam	if (protmode != ic->ic_curhtprotmode) {
1267173273Ssam		ic->ic_curhtprotmode = protmode;
1268173273Ssam		htinfo_notify(ic);
1269173273Ssam	}
1270173273Ssam}
1271173273Ssam
1272173273Ssam/*
1273173273Ssam * Handle an HT station joining a BSS.
1274173273Ssam */
1275173273Ssamvoid
1276173273Ssamieee80211_ht_node_join(struct ieee80211_node *ni)
1277173273Ssam{
1278173273Ssam	struct ieee80211com *ic = ni->ni_ic;
1279173273Ssam
1280173273Ssam	IEEE80211_LOCK_ASSERT(ic);
1281173273Ssam
1282173273Ssam	if (ni->ni_flags & IEEE80211_NODE_HT) {
1283173273Ssam		ic->ic_ht_sta_assoc++;
1284173273Ssam		if (ni->ni_chw == 40)
1285173273Ssam			ic->ic_ht40_sta_assoc++;
1286173273Ssam	}
1287173273Ssam	htinfo_update(ic);
1288173273Ssam}
1289173273Ssam
1290173273Ssam/*
1291173273Ssam * Handle an HT station leaving a BSS.
1292173273Ssam */
1293173273Ssamvoid
1294173273Ssamieee80211_ht_node_leave(struct ieee80211_node *ni)
1295173273Ssam{
1296173273Ssam	struct ieee80211com *ic = ni->ni_ic;
1297173273Ssam
1298173273Ssam	IEEE80211_LOCK_ASSERT(ic);
1299173273Ssam
1300173273Ssam	if (ni->ni_flags & IEEE80211_NODE_HT) {
1301173273Ssam		ic->ic_ht_sta_assoc--;
1302173273Ssam		if (ni->ni_chw == 40)
1303173273Ssam			ic->ic_ht40_sta_assoc--;
1304173273Ssam	}
1305173273Ssam	htinfo_update(ic);
1306173273Ssam}
1307173273Ssam
1308173273Ssam/*
1309173273Ssam * Public version of htinfo_update; used for processing
1310178354Ssam * beacon frames from overlapping bss.
1311178354Ssam *
1312178354Ssam * Caller can specify either IEEE80211_HTINFO_OPMODE_MIXED
1313178354Ssam * (on receipt of a beacon that advertises MIXED) or
1314178354Ssam * IEEE80211_HTINFO_OPMODE_PROTOPT (on receipt of a beacon
1315178354Ssam * from an overlapping legacy bss).  We treat MIXED with
1316178354Ssam * a higher precedence than PROTOPT (i.e. we will not change
1317178354Ssam * change PROTOPT -> MIXED; only MIXED -> PROTOPT).  This
1318178354Ssam * corresponds to how we handle things in htinfo_update.
1319173273Ssam */
1320173273Ssamvoid
1321178354Ssamieee80211_htprot_update(struct ieee80211com *ic, int protmode)
1322173273Ssam{
1323178354Ssam#define	OPMODE(x)	SM(x, IEEE80211_HTINFO_OPMODE)
1324183246Ssam	IEEE80211_LOCK(ic);
1325178354Ssam
1326178354Ssam	/* track non-HT station presence */
1327178354Ssam	KASSERT(protmode & IEEE80211_HTINFO_NONHT_PRESENT,
1328183246Ssam	    ("protmode 0x%x", protmode));
1329193655Ssam	ic->ic_flags_ht |= IEEE80211_FHT_NONHT_PR;
1330178354Ssam	ic->ic_lastnonht = ticks;
1331178354Ssam
1332183246Ssam	if (protmode != ic->ic_curhtprotmode &&
1333183246Ssam	    (OPMODE(ic->ic_curhtprotmode) != IEEE80211_HTINFO_OPMODE_MIXED ||
1334183246Ssam	     OPMODE(protmode) == IEEE80211_HTINFO_OPMODE_PROTOPT)) {
1335183246Ssam		/* push beacon update */
1336183246Ssam		ic->ic_curhtprotmode = protmode;
1337183246Ssam		htinfo_notify(ic);
1338183246Ssam	}
1339181197Ssam	IEEE80211_UNLOCK(ic);
1340178354Ssam#undef OPMODE
1341173273Ssam}
1342173273Ssam
1343173273Ssam/*
1344173273Ssam * Time out presence of an overlapping bss with non-HT
1345173273Ssam * stations.  When operating in hostap mode we listen for
1346173273Ssam * beacons from other stations and if we identify a non-HT
1347173273Ssam * station is present we update the opmode field of the
1348173273Ssam * HTINFO ie.  To identify when all non-HT stations are
1349173273Ssam * gone we time out this condition.
1350173273Ssam */
1351173273Ssamvoid
1352173273Ssamieee80211_ht_timeout(struct ieee80211com *ic)
1353173273Ssam{
1354173273Ssam	IEEE80211_LOCK_ASSERT(ic);
1355173273Ssam
1356193655Ssam	if ((ic->ic_flags_ht & IEEE80211_FHT_NONHT_PR) &&
1357173273Ssam	    time_after(ticks, ic->ic_lastnonht + IEEE80211_NONHT_PRESENT_AGE)) {
1358173273Ssam#if 0
1359178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
1360173273Ssam		    "%s", "time out non-HT STA present on channel");
1361173273Ssam#endif
1362193655Ssam		ic->ic_flags_ht &= ~IEEE80211_FHT_NONHT_PR;
1363173273Ssam		htinfo_update(ic);
1364173273Ssam	}
1365173273Ssam}
1366173273Ssam
1367170530Ssam/* unalligned little endian access */
1368170530Ssam#define LE_READ_2(p)					\
1369170530Ssam	((uint16_t)					\
1370170530Ssam	 ((((const uint8_t *)(p))[0]      ) |		\
1371170530Ssam	  (((const uint8_t *)(p))[1] <<  8)))
1372170530Ssam
1373170530Ssam/*
1374170530Ssam * Process an 802.11n HT capabilities ie.
1375170530Ssam */
1376170530Ssamvoid
1377170530Ssamieee80211_parse_htcap(struct ieee80211_node *ni, const uint8_t *ie)
1378170530Ssam{
1379170530Ssam	if (ie[0] == IEEE80211_ELEMID_VENDOR) {
1380170530Ssam		/*
1381170530Ssam		 * Station used Vendor OUI ie to associate;
1382170530Ssam		 * mark the node so when we respond we'll use
1383170530Ssam		 * the Vendor OUI's and not the standard ie's.
1384170530Ssam		 */
1385170530Ssam		ni->ni_flags |= IEEE80211_NODE_HTCOMPAT;
1386170530Ssam		ie += 4;
1387170530Ssam	} else
1388170530Ssam		ni->ni_flags &= ~IEEE80211_NODE_HTCOMPAT;
1389170530Ssam
1390170530Ssam	ni->ni_htcap = LE_READ_2(ie +
1391170530Ssam		__offsetof(struct ieee80211_ie_htcap, hc_cap));
1392170530Ssam	ni->ni_htparam = ie[__offsetof(struct ieee80211_ie_htcap, hc_param)];
1393170530Ssam}
1394170530Ssam
1395183254Ssamstatic void
1396183254Ssamhtinfo_parse(struct ieee80211_node *ni,
1397183254Ssam	const struct ieee80211_ie_htinfo *htinfo)
1398170530Ssam{
1399170530Ssam	uint16_t w;
1400170530Ssam
1401170530Ssam	ni->ni_htctlchan = htinfo->hi_ctrlchannel;
1402170530Ssam	ni->ni_ht2ndchan = SM(htinfo->hi_byte1, IEEE80211_HTINFO_2NDCHAN);
1403172055Ssam	w = LE_READ_2(&htinfo->hi_byte2);
1404170530Ssam	ni->ni_htopmode = SM(w, IEEE80211_HTINFO_OPMODE);
1405170530Ssam	w = LE_READ_2(&htinfo->hi_byte45);
1406170530Ssam	ni->ni_htstbc = SM(w, IEEE80211_HTINFO_BASIC_STBCMCS);
1407183254Ssam}
1408183254Ssam
1409183254Ssam/*
1410183254Ssam * Parse an 802.11n HT info ie and save useful information
1411183254Ssam * to the node state.  Note this does not effect any state
1412183254Ssam * changes such as for channel width change.
1413183254Ssam */
1414183254Ssamvoid
1415183254Ssamieee80211_parse_htinfo(struct ieee80211_node *ni, const uint8_t *ie)
1416183254Ssam{
1417183254Ssam	if (ie[0] == IEEE80211_ELEMID_VENDOR)
1418183254Ssam		ie += 4;
1419183254Ssam	htinfo_parse(ni, (const struct ieee80211_ie_htinfo *) ie);
1420183254Ssam}
1421183254Ssam
1422183254Ssam/*
1423183254Ssam * Handle 11n channel switch.  Use the received HT ie's to
1424183254Ssam * identify the right channel to use.  If we cannot locate it
1425183254Ssam * in the channel table then fallback to legacy operation.
1426183254Ssam * Note that we use this information to identify the node's
1427183254Ssam * channel only; the caller is responsible for insuring any
1428183254Ssam * required channel change is done (e.g. in sta mode when
1429183254Ssam * parsing the contents of a beacon frame).
1430183254Ssam */
1431233452Sadrianstatic int
1432183254Ssamhtinfo_update_chw(struct ieee80211_node *ni, int htflags)
1433183254Ssam{
1434183254Ssam	struct ieee80211com *ic = ni->ni_ic;
1435183254Ssam	struct ieee80211_channel *c;
1436183254Ssam	int chanflags;
1437233452Sadrian	int ret = 0;
1438183254Ssam
1439173273Ssam	chanflags = (ni->ni_chan->ic_flags &~ IEEE80211_CHAN_HT) | htflags;
1440173273Ssam	if (chanflags != ni->ni_chan->ic_flags) {
1441183254Ssam		/* XXX not right for ht40- */
1442173273Ssam		c = ieee80211_find_channel(ic, ni->ni_chan->ic_freq, chanflags);
1443178354Ssam		if (c == NULL && (htflags & IEEE80211_CHAN_HT40)) {
1444173273Ssam			/*
1445173273Ssam			 * No HT40 channel entry in our table; fall back
1446173273Ssam			 * to HT20 operation.  This should not happen.
1447173273Ssam			 */
1448173273Ssam			c = findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT20);
1449183254Ssam#if 0
1450183254Ssam			IEEE80211_NOTE(ni->ni_vap,
1451173273Ssam			    IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
1452173273Ssam			    "no HT40 channel (freq %u), falling back to HT20",
1453173273Ssam			    ni->ni_chan->ic_freq);
1454183254Ssam#endif
1455173273Ssam			/* XXX stat */
1456173273Ssam		}
1457173273Ssam		if (c != NULL && c != ni->ni_chan) {
1458183254Ssam			IEEE80211_NOTE(ni->ni_vap,
1459173273Ssam			    IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
1460173273Ssam			    "switch station to HT%d channel %u/0x%x",
1461173273Ssam			    IEEE80211_IS_CHAN_HT40(c) ? 40 : 20,
1462173273Ssam			    c->ic_freq, c->ic_flags);
1463173273Ssam			ni->ni_chan = c;
1464233452Sadrian			ret = 1;
1465173273Ssam		}
1466173273Ssam		/* NB: caller responsible for forcing any channel change */
1467173273Ssam	}
1468173273Ssam	/* update node's tx channel width */
1469173273Ssam	ni->ni_chw = IEEE80211_IS_CHAN_HT40(ni->ni_chan)? 40 : 20;
1470233452Sadrian	return (ret);
1471170530Ssam}
1472170530Ssam
1473170530Ssam/*
1474183255Ssam * Update 11n MIMO PS state according to received htcap.
1475183255Ssam */
1476183255Ssamstatic __inline int
1477183255Ssamhtcap_update_mimo_ps(struct ieee80211_node *ni)
1478183255Ssam{
1479183255Ssam	uint16_t oflags = ni->ni_flags;
1480183255Ssam
1481183255Ssam	switch (ni->ni_htcap & IEEE80211_HTCAP_SMPS) {
1482183255Ssam	case IEEE80211_HTCAP_SMPS_DYNAMIC:
1483183255Ssam		ni->ni_flags |= IEEE80211_NODE_MIMO_PS;
1484183255Ssam		ni->ni_flags |= IEEE80211_NODE_MIMO_RTS;
1485183255Ssam		break;
1486183255Ssam	case IEEE80211_HTCAP_SMPS_ENA:
1487183255Ssam		ni->ni_flags |= IEEE80211_NODE_MIMO_PS;
1488183255Ssam		ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS;
1489183255Ssam		break;
1490183255Ssam	case IEEE80211_HTCAP_SMPS_OFF:
1491183255Ssam	default:		/* disable on rx of reserved value */
1492183255Ssam		ni->ni_flags &= ~IEEE80211_NODE_MIMO_PS;
1493183255Ssam		ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS;
1494183255Ssam		break;
1495183255Ssam	}
1496183255Ssam	return (oflags ^ ni->ni_flags);
1497183255Ssam}
1498183255Ssam
1499183255Ssam/*
1500183257Ssam * Update short GI state according to received htcap
1501183257Ssam * and local settings.
1502183257Ssam */
1503183257Ssamstatic __inline void
1504183257Ssamhtcap_update_shortgi(struct ieee80211_node *ni)
1505183257Ssam{
1506183257Ssam	struct ieee80211vap *vap = ni->ni_vap;
1507183257Ssam
1508183257Ssam	ni->ni_flags &= ~(IEEE80211_NODE_SGI20|IEEE80211_NODE_SGI40);
1509183257Ssam	if ((ni->ni_htcap & IEEE80211_HTCAP_SHORTGI20) &&
1510193655Ssam	    (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20))
1511183257Ssam		ni->ni_flags |= IEEE80211_NODE_SGI20;
1512183257Ssam	if ((ni->ni_htcap & IEEE80211_HTCAP_SHORTGI40) &&
1513193655Ssam	    (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40))
1514183257Ssam		ni->ni_flags |= IEEE80211_NODE_SGI40;
1515183257Ssam}
1516183257Ssam
1517183257Ssam/*
1518183254Ssam * Parse and update HT-related state extracted from
1519183254Ssam * the HT cap and info ie's.
1520183254Ssam */
1521233452Sadrianint
1522183254Ssamieee80211_ht_updateparams(struct ieee80211_node *ni,
1523183254Ssam	const uint8_t *htcapie, const uint8_t *htinfoie)
1524183254Ssam{
1525183254Ssam	struct ieee80211vap *vap = ni->ni_vap;
1526183254Ssam	const struct ieee80211_ie_htinfo *htinfo;
1527183254Ssam	int htflags;
1528233452Sadrian	int ret = 0;
1529183254Ssam
1530183254Ssam	ieee80211_parse_htcap(ni, htcapie);
1531183255Ssam	if (vap->iv_htcaps & IEEE80211_HTCAP_SMPS)
1532183255Ssam		htcap_update_mimo_ps(ni);
1533183257Ssam	htcap_update_shortgi(ni);
1534183254Ssam
1535183254Ssam	if (htinfoie[0] == IEEE80211_ELEMID_VENDOR)
1536183254Ssam		htinfoie += 4;
1537183254Ssam	htinfo = (const struct ieee80211_ie_htinfo *) htinfoie;
1538183254Ssam	htinfo_parse(ni, htinfo);
1539183254Ssam
1540193655Ssam	htflags = (vap->iv_flags_ht & IEEE80211_FHT_HT) ?
1541183254Ssam	    IEEE80211_CHAN_HT20 : 0;
1542183254Ssam	/* NB: honor operating mode constraint */
1543183254Ssam	if ((htinfo->hi_byte1 & IEEE80211_HTINFO_TXWIDTH_2040) &&
1544193655Ssam	    (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)) {
1545183254Ssam		if (ni->ni_ht2ndchan == IEEE80211_HTINFO_2NDCHAN_ABOVE)
1546183254Ssam			htflags = IEEE80211_CHAN_HT40U;
1547183254Ssam		else if (ni->ni_ht2ndchan == IEEE80211_HTINFO_2NDCHAN_BELOW)
1548183254Ssam			htflags = IEEE80211_CHAN_HT40D;
1549183254Ssam	}
1550233452Sadrian	if (htinfo_update_chw(ni, htflags))
1551233452Sadrian		ret = 1;
1552183256Ssam
1553183256Ssam	if ((htinfo->hi_byte1 & IEEE80211_HTINFO_RIFSMODE_PERM) &&
1554193655Ssam	    (vap->iv_flags_ht & IEEE80211_FHT_RIFS))
1555183256Ssam		ni->ni_flags |= IEEE80211_NODE_RIFS;
1556183256Ssam	else
1557183256Ssam		ni->ni_flags &= ~IEEE80211_NODE_RIFS;
1558233452Sadrian
1559233452Sadrian	return (ret);
1560183254Ssam}
1561183254Ssam
1562183254Ssam/*
1563183254Ssam * Parse and update HT-related state extracted from the HT cap ie
1564183254Ssam * for a station joining an HT BSS.
1565183254Ssam */
1566183254Ssamvoid
1567183254Ssamieee80211_ht_updatehtcap(struct ieee80211_node *ni, const uint8_t *htcapie)
1568183254Ssam{
1569183254Ssam	struct ieee80211vap *vap = ni->ni_vap;
1570183254Ssam	int htflags;
1571183254Ssam
1572183254Ssam	ieee80211_parse_htcap(ni, htcapie);
1573183255Ssam	if (vap->iv_htcaps & IEEE80211_HTCAP_SMPS)
1574183255Ssam		htcap_update_mimo_ps(ni);
1575183257Ssam	htcap_update_shortgi(ni);
1576183254Ssam
1577183254Ssam	/* NB: honor operating mode constraint */
1578219606Sbschmidt	/* XXX 40 MHz intolerant */
1579193655Ssam	htflags = (vap->iv_flags_ht & IEEE80211_FHT_HT) ?
1580183254Ssam	    IEEE80211_CHAN_HT20 : 0;
1581183254Ssam	if ((ni->ni_htcap & IEEE80211_HTCAP_CHWIDTH40) &&
1582193655Ssam	    (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)) {
1583183254Ssam		if (IEEE80211_IS_CHAN_HT40U(vap->iv_bss->ni_chan))
1584183254Ssam			htflags = IEEE80211_CHAN_HT40U;
1585183254Ssam		else if (IEEE80211_IS_CHAN_HT40D(vap->iv_bss->ni_chan))
1586183254Ssam			htflags = IEEE80211_CHAN_HT40D;
1587183254Ssam	}
1588233452Sadrian	(void) htinfo_update_chw(ni, htflags);
1589183254Ssam}
1590183254Ssam
1591183254Ssam/*
1592170530Ssam * Install received HT rate set by parsing the HT cap ie.
1593170530Ssam */
1594170530Ssamint
1595170530Ssamieee80211_setup_htrates(struct ieee80211_node *ni, const uint8_t *ie, int flags)
1596170530Ssam{
1597219602Sbschmidt	struct ieee80211com *ic = ni->ni_ic;
1598178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1599170530Ssam	const struct ieee80211_ie_htcap *htcap;
1600170530Ssam	struct ieee80211_htrateset *rs;
1601219602Sbschmidt	int i, maxequalmcs, maxunequalmcs;
1602170530Ssam
1603219602Sbschmidt	maxequalmcs = ic->ic_txstream * 8 - 1;
1604219602Sbschmidt	if (ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL) {
1605219602Sbschmidt		if (ic->ic_txstream >= 2)
1606219602Sbschmidt			maxunequalmcs = 38;
1607219602Sbschmidt		if (ic->ic_txstream >= 3)
1608219602Sbschmidt			maxunequalmcs = 52;
1609219602Sbschmidt		if (ic->ic_txstream >= 4)
1610219602Sbschmidt			maxunequalmcs = 76;
1611219602Sbschmidt	} else
1612219602Sbschmidt		maxunequalmcs = 0;
1613219602Sbschmidt
1614170530Ssam	rs = &ni->ni_htrates;
1615170530Ssam	memset(rs, 0, sizeof(*rs));
1616170530Ssam	if (ie != NULL) {
1617170530Ssam		if (ie[0] == IEEE80211_ELEMID_VENDOR)
1618170530Ssam			ie += 4;
1619170530Ssam		htcap = (const struct ieee80211_ie_htcap *) ie;
1620170530Ssam		for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++) {
1621170530Ssam			if (isclr(htcap->hc_mcsset, i))
1622170530Ssam				continue;
1623170530Ssam			if (rs->rs_nrates == IEEE80211_HTRATE_MAXSIZE) {
1624178354Ssam				IEEE80211_NOTE(vap,
1625170530Ssam				    IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
1626170530Ssam				    "WARNING, HT rate set too large; only "
1627170530Ssam				    "using %u rates", IEEE80211_HTRATE_MAXSIZE);
1628178354Ssam				vap->iv_stats.is_rx_rstoobig++;
1629170530Ssam				break;
1630170530Ssam			}
1631219602Sbschmidt			if (i <= 31 && i > maxequalmcs)
1632219602Sbschmidt				continue;
1633219602Sbschmidt			if (i == 32 &&
1634219602Sbschmidt			    (ic->ic_htcaps & IEEE80211_HTC_TXMCS32) == 0)
1635219602Sbschmidt				continue;
1636219602Sbschmidt			if (i > 32 && i > maxunequalmcs)
1637219602Sbschmidt				continue;
1638170530Ssam			rs->rs_rates[rs->rs_nrates++] = i;
1639170530Ssam		}
1640170530Ssam	}
1641170530Ssam	return ieee80211_fix_rate(ni, (struct ieee80211_rateset *) rs, flags);
1642170530Ssam}
1643170530Ssam
1644170530Ssam/*
1645170530Ssam * Mark rates in a node's HT rate set as basic according
1646170530Ssam * to the information in the supplied HT info ie.
1647170530Ssam */
1648170530Ssamvoid
1649170530Ssamieee80211_setup_basic_htrates(struct ieee80211_node *ni, const uint8_t *ie)
1650170530Ssam{
1651170530Ssam	const struct ieee80211_ie_htinfo *htinfo;
1652170530Ssam	struct ieee80211_htrateset *rs;
1653170530Ssam	int i, j;
1654170530Ssam
1655170530Ssam	if (ie[0] == IEEE80211_ELEMID_VENDOR)
1656170530Ssam		ie += 4;
1657170530Ssam	htinfo = (const struct ieee80211_ie_htinfo *) ie;
1658170530Ssam	rs = &ni->ni_htrates;
1659170530Ssam	if (rs->rs_nrates == 0) {
1660178354Ssam		IEEE80211_NOTE(ni->ni_vap,
1661170530Ssam		    IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
1662170530Ssam		    "%s", "WARNING, empty HT rate set");
1663170530Ssam		return;
1664170530Ssam	}
1665170530Ssam	for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++) {
1666170530Ssam		if (isclr(htinfo->hi_basicmcsset, i))
1667170530Ssam			continue;
1668170530Ssam		for (j = 0; j < rs->rs_nrates; j++)
1669170530Ssam			if ((rs->rs_rates[j] & IEEE80211_RATE_VAL) == i)
1670170530Ssam				rs->rs_rates[j] |= IEEE80211_RATE_BASIC;
1671170530Ssam	}
1672170530Ssam}
1673170530Ssam
1674170530Ssamstatic void
1675184280Ssamampdu_tx_setup(struct ieee80211_tx_ampdu *tap)
1676184280Ssam{
1677184280Ssam	callout_init(&tap->txa_timer, CALLOUT_MPSAFE);
1678184280Ssam	tap->txa_flags |= IEEE80211_AGGR_SETUP;
1679184280Ssam}
1680184280Ssam
1681184280Ssamstatic void
1682184280Ssamampdu_tx_stop(struct ieee80211_tx_ampdu *tap)
1683184280Ssam{
1684184280Ssam	struct ieee80211_node *ni = tap->txa_ni;
1685184280Ssam	struct ieee80211com *ic = ni->ni_ic;
1686184280Ssam
1687184280Ssam	KASSERT(tap->txa_flags & IEEE80211_AGGR_SETUP,
1688234324Sadrian	    ("txa_flags 0x%x tid %d ac %d", tap->txa_flags, tap->txa_tid,
1689234324Sadrian	    TID_TO_WME_AC(tap->txa_tid)));
1690184280Ssam
1691184280Ssam	/*
1692184280Ssam	 * Stop BA stream if setup so driver has a chance
1693184280Ssam	 * to reclaim any resources it might have allocated.
1694184280Ssam	 */
1695184280Ssam	ic->ic_addba_stop(ni, tap);
1696184280Ssam	/*
1697184280Ssam	 * Stop any pending BAR transmit.
1698184280Ssam	 */
1699184280Ssam	bar_stop_timer(tap);
1700184280Ssam
1701184280Ssam	tap->txa_lastsample = 0;
1702184280Ssam	tap->txa_avgpps = 0;
1703184280Ssam	/* NB: clearing NAK means we may re-send ADDBA */
1704184280Ssam	tap->txa_flags &= ~(IEEE80211_AGGR_SETUP | IEEE80211_AGGR_NAK);
1705184280Ssam}
1706184280Ssam
1707223331Sadrian/*
1708223331Sadrian * ADDBA response timeout.
1709223331Sadrian *
1710223331Sadrian * If software aggregation and per-TID queue management was done here,
1711223331Sadrian * that queue would be unpaused after the ADDBA timeout occurs.
1712223331Sadrian */
1713184280Ssamstatic void
1714170530Ssamaddba_timeout(void *arg)
1715170530Ssam{
1716170530Ssam	struct ieee80211_tx_ampdu *tap = arg;
1717223331Sadrian	struct ieee80211_node *ni = tap->txa_ni;
1718223331Sadrian	struct ieee80211com *ic = ni->ni_ic;
1719170530Ssam
1720170530Ssam	/* XXX ? */
1721170530Ssam	tap->txa_flags &= ~IEEE80211_AGGR_XCHGPEND;
1722170530Ssam	tap->txa_attempts++;
1723223331Sadrian	ic->ic_addba_response_timeout(ni, tap);
1724170530Ssam}
1725170530Ssam
1726170530Ssamstatic void
1727170530Ssamaddba_start_timeout(struct ieee80211_tx_ampdu *tap)
1728170530Ssam{
1729170530Ssam	/* XXX use CALLOUT_PENDING instead? */
1730178354Ssam	callout_reset(&tap->txa_timer, ieee80211_addba_timeout,
1731170530Ssam	    addba_timeout, tap);
1732170530Ssam	tap->txa_flags |= IEEE80211_AGGR_XCHGPEND;
1733178354Ssam	tap->txa_nextrequest = ticks + ieee80211_addba_timeout;
1734170530Ssam}
1735170530Ssam
1736170530Ssamstatic void
1737170530Ssamaddba_stop_timeout(struct ieee80211_tx_ampdu *tap)
1738170530Ssam{
1739170530Ssam	/* XXX use CALLOUT_PENDING instead? */
1740170530Ssam	if (tap->txa_flags & IEEE80211_AGGR_XCHGPEND) {
1741170530Ssam		callout_stop(&tap->txa_timer);
1742170530Ssam		tap->txa_flags &= ~IEEE80211_AGGR_XCHGPEND;
1743170530Ssam	}
1744170530Ssam}
1745170530Ssam
1746223331Sadrianstatic void
1747223331Sadriannull_addba_response_timeout(struct ieee80211_node *ni,
1748223331Sadrian    struct ieee80211_tx_ampdu *tap)
1749223331Sadrian{
1750223331Sadrian}
1751223331Sadrian
1752170530Ssam/*
1753170530Ssam * Default method for requesting A-MPDU tx aggregation.
1754170530Ssam * We setup the specified state block and start a timer
1755170530Ssam * to wait for an ADDBA response frame.
1756170530Ssam */
1757170530Ssamstatic int
1758170530Ssamieee80211_addba_request(struct ieee80211_node *ni,
1759170530Ssam	struct ieee80211_tx_ampdu *tap,
1760170530Ssam	int dialogtoken, int baparamset, int batimeout)
1761170530Ssam{
1762170530Ssam	int bufsiz;
1763170530Ssam
1764170530Ssam	/* XXX locking */
1765170530Ssam	tap->txa_token = dialogtoken;
1766170530Ssam	tap->txa_flags |= IEEE80211_AGGR_IMMEDIATE;
1767170530Ssam	bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
1768170530Ssam	tap->txa_wnd = (bufsiz == 0) ?
1769170530Ssam	    IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
1770170530Ssam	addba_start_timeout(tap);
1771170530Ssam	return 1;
1772170530Ssam}
1773170530Ssam
1774170530Ssam/*
1775170530Ssam * Default method for processing an A-MPDU tx aggregation
1776170530Ssam * response.  We shutdown any pending timer and update the
1777170530Ssam * state block according to the reply.
1778170530Ssam */
1779170530Ssamstatic int
1780170530Ssamieee80211_addba_response(struct ieee80211_node *ni,
1781170530Ssam	struct ieee80211_tx_ampdu *tap,
1782170530Ssam	int status, int baparamset, int batimeout)
1783170530Ssam{
1784184280Ssam	int bufsiz, tid;
1785170530Ssam
1786170530Ssam	/* XXX locking */
1787170530Ssam	addba_stop_timeout(tap);
1788170530Ssam	if (status == IEEE80211_STATUS_SUCCESS) {
1789170530Ssam		bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
1790170530Ssam		/* XXX override our request? */
1791170530Ssam		tap->txa_wnd = (bufsiz == 0) ?
1792170530Ssam		    IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
1793184280Ssam		/* XXX AC/TID */
1794184280Ssam		tid = MS(baparamset, IEEE80211_BAPS_TID);
1795170530Ssam		tap->txa_flags |= IEEE80211_AGGR_RUNNING;
1796184280Ssam		tap->txa_attempts = 0;
1797173273Ssam	} else {
1798173273Ssam		/* mark tid so we don't try again */
1799173273Ssam		tap->txa_flags |= IEEE80211_AGGR_NAK;
1800170530Ssam	}
1801170530Ssam	return 1;
1802170530Ssam}
1803170530Ssam
1804170530Ssam/*
1805170530Ssam * Default method for stopping A-MPDU tx aggregation.
1806170530Ssam * Any timer is cleared and we drain any pending frames.
1807170530Ssam */
1808170530Ssamstatic void
1809170530Ssamieee80211_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
1810170530Ssam{
1811170530Ssam	/* XXX locking */
1812170530Ssam	addba_stop_timeout(tap);
1813170530Ssam	if (tap->txa_flags & IEEE80211_AGGR_RUNNING) {
1814182830Ssam		/* XXX clear aggregation queue */
1815170530Ssam		tap->txa_flags &= ~IEEE80211_AGGR_RUNNING;
1816170530Ssam	}
1817170530Ssam	tap->txa_attempts = 0;
1818170530Ssam}
1819170530Ssam
1820170530Ssam/*
1821170530Ssam * Process a received action frame using the default aggregation
1822170530Ssam * policy.  We intercept ADDBA-related frames and use them to
1823170530Ssam * update our aggregation state.  All other frames are passed up
1824170530Ssam * for processing by ieee80211_recv_action.
1825170530Ssam */
1826195377Ssamstatic int
1827195377Ssamht_recv_action_ba_addba_request(struct ieee80211_node *ni,
1828195377Ssam	const struct ieee80211_frame *wh,
1829170530Ssam	const uint8_t *frm, const uint8_t *efrm)
1830170530Ssam{
1831170530Ssam	struct ieee80211com *ic = ni->ni_ic;
1832178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1833170530Ssam	struct ieee80211_rx_ampdu *rap;
1834195377Ssam	uint8_t dialogtoken;
1835195377Ssam	uint16_t baparamset, batimeout, baseqctl;
1836205277Srpaulo	uint16_t args[5];
1837195377Ssam	int tid;
1838170530Ssam
1839195377Ssam	dialogtoken = frm[2];
1840195377Ssam	baparamset = LE_READ_2(frm+3);
1841195377Ssam	batimeout = LE_READ_2(frm+5);
1842195377Ssam	baseqctl = LE_READ_2(frm+7);
1843170530Ssam
1844195377Ssam	tid = MS(baparamset, IEEE80211_BAPS_TID);
1845170530Ssam
1846195377Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1847195377Ssam	    "recv ADDBA request: dialogtoken %u baparamset 0x%x "
1848195377Ssam	    "(tid %d bufsiz %d) batimeout %d baseqctl %d:%d",
1849195377Ssam	    dialogtoken, baparamset,
1850195377Ssam	    tid, MS(baparamset, IEEE80211_BAPS_BUFSIZ),
1851195377Ssam	    batimeout,
1852195377Ssam	    MS(baseqctl, IEEE80211_BASEQ_START),
1853195377Ssam	    MS(baseqctl, IEEE80211_BASEQ_FRAG));
1854170530Ssam
1855195377Ssam	rap = &ni->ni_rx_ampdu[tid];
1856170530Ssam
1857195377Ssam	/* Send ADDBA response */
1858195377Ssam	args[0] = dialogtoken;
1859195377Ssam	/*
1860195377Ssam	 * NB: We ack only if the sta associated with HT and
1861195377Ssam	 * the ap is configured to do AMPDU rx (the latter
1862195377Ssam	 * violates the 11n spec and is mostly for testing).
1863195377Ssam	 */
1864195377Ssam	if ((ni->ni_flags & IEEE80211_NODE_AMPDU_RX) &&
1865195377Ssam	    (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_RX)) {
1866195377Ssam		/* XXX handle ampdu_rx_start failure */
1867195377Ssam		ic->ic_ampdu_rx_start(ni, rap,
1868195377Ssam		    baparamset, batimeout, baseqctl);
1869170530Ssam
1870195377Ssam		args[1] = IEEE80211_STATUS_SUCCESS;
1871195377Ssam	} else {
1872195377Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1873195377Ssam		    ni, "reject ADDBA request: %s",
1874195377Ssam		    ni->ni_flags & IEEE80211_NODE_AMPDU_RX ?
1875195377Ssam		       "administratively disabled" :
1876195377Ssam		       "not negotiated for station");
1877195377Ssam		vap->iv_stats.is_addba_reject++;
1878195377Ssam		args[1] = IEEE80211_STATUS_UNSPECIFIED;
1879195377Ssam	}
1880195377Ssam	/* XXX honor rap flags? */
1881195377Ssam	args[2] = IEEE80211_BAPS_POLICY_IMMEDIATE
1882195377Ssam		| SM(tid, IEEE80211_BAPS_TID)
1883195377Ssam		| SM(rap->rxa_wnd, IEEE80211_BAPS_BUFSIZ)
1884195377Ssam		;
1885195377Ssam	args[3] = 0;
1886205277Srpaulo	args[4] = 0;
1887195377Ssam	ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
1888195377Ssam		IEEE80211_ACTION_BA_ADDBA_RESPONSE, args);
1889195377Ssam	return 0;
1890195377Ssam}
1891170530Ssam
1892195377Ssamstatic int
1893195377Ssamht_recv_action_ba_addba_response(struct ieee80211_node *ni,
1894195377Ssam	const struct ieee80211_frame *wh,
1895195377Ssam	const uint8_t *frm, const uint8_t *efrm)
1896195377Ssam{
1897195377Ssam	struct ieee80211com *ic = ni->ni_ic;
1898195377Ssam	struct ieee80211vap *vap = ni->ni_vap;
1899195377Ssam	struct ieee80211_tx_ampdu *tap;
1900195377Ssam	uint8_t dialogtoken, policy;
1901195377Ssam	uint16_t baparamset, batimeout, code;
1902234324Sadrian	int tid, bufsiz;
1903170530Ssam
1904195377Ssam	dialogtoken = frm[2];
1905195377Ssam	code = LE_READ_2(frm+3);
1906195377Ssam	baparamset = LE_READ_2(frm+5);
1907195377Ssam	tid = MS(baparamset, IEEE80211_BAPS_TID);
1908195377Ssam	bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
1909195377Ssam	policy = MS(baparamset, IEEE80211_BAPS_POLICY);
1910195377Ssam	batimeout = LE_READ_2(frm+7);
1911195377Ssam
1912234324Sadrian	tap = &ni->ni_tx_ampdu[tid];
1913195377Ssam	if ((tap->txa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
1914195377Ssam		IEEE80211_DISCARD_MAC(vap,
1915195377Ssam		    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1916195377Ssam		    ni->ni_macaddr, "ADDBA response",
1917195377Ssam		    "no pending ADDBA, tid %d dialogtoken %u "
1918195377Ssam		    "code %d", tid, dialogtoken, code);
1919195377Ssam		vap->iv_stats.is_addba_norequest++;
1920195377Ssam		return 0;
1921195377Ssam	}
1922195377Ssam	if (dialogtoken != tap->txa_token) {
1923195377Ssam		IEEE80211_DISCARD_MAC(vap,
1924195377Ssam		    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1925195377Ssam		    ni->ni_macaddr, "ADDBA response",
1926195377Ssam		    "dialogtoken mismatch: waiting for %d, "
1927195377Ssam		    "received %d, tid %d code %d",
1928195377Ssam		    tap->txa_token, dialogtoken, tid, code);
1929195377Ssam		vap->iv_stats.is_addba_badtoken++;
1930195377Ssam		return 0;
1931195377Ssam	}
1932195377Ssam	/* NB: assumes IEEE80211_AGGR_IMMEDIATE is 1 */
1933195377Ssam	if (policy != (tap->txa_flags & IEEE80211_AGGR_IMMEDIATE)) {
1934195377Ssam		IEEE80211_DISCARD_MAC(vap,
1935195377Ssam		    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1936195377Ssam		    ni->ni_macaddr, "ADDBA response",
1937195377Ssam		    "policy mismatch: expecting %s, "
1938195377Ssam		    "received %s, tid %d code %d",
1939195377Ssam		    tap->txa_flags & IEEE80211_AGGR_IMMEDIATE,
1940195377Ssam		    policy, tid, code);
1941195377Ssam		vap->iv_stats.is_addba_badpolicy++;
1942195377Ssam		return 0;
1943195377Ssam	}
1944182829Ssam#if 0
1945195377Ssam	/* XXX we take MIN in ieee80211_addba_response */
1946195377Ssam	if (bufsiz > IEEE80211_AGGR_BAWMAX) {
1947195377Ssam		IEEE80211_DISCARD_MAC(vap,
1948195377Ssam		    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1949195377Ssam		    ni->ni_macaddr, "ADDBA response",
1950195377Ssam		    "BA window too large: max %d, "
1951195377Ssam		    "received %d, tid %d code %d",
1952195377Ssam		    bufsiz, IEEE80211_AGGR_BAWMAX, tid, code);
1953195377Ssam		vap->iv_stats.is_addba_badbawinsize++;
1954195377Ssam		return 0;
1955195377Ssam	}
1956182829Ssam#endif
1957195377Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1958195377Ssam	    "recv ADDBA response: dialogtoken %u code %d "
1959195377Ssam	    "baparamset 0x%x (tid %d bufsiz %d) batimeout %d",
1960195377Ssam	    dialogtoken, code, baparamset, tid, bufsiz,
1961195377Ssam	    batimeout);
1962195377Ssam	ic->ic_addba_response(ni, tap, code, baparamset, batimeout);
1963195377Ssam	return 0;
1964195377Ssam}
1965170530Ssam
1966195377Ssamstatic int
1967195377Ssamht_recv_action_ba_delba(struct ieee80211_node *ni,
1968195377Ssam	const struct ieee80211_frame *wh,
1969195377Ssam	const uint8_t *frm, const uint8_t *efrm)
1970195377Ssam{
1971195377Ssam	struct ieee80211com *ic = ni->ni_ic;
1972195377Ssam	struct ieee80211_rx_ampdu *rap;
1973195377Ssam	struct ieee80211_tx_ampdu *tap;
1974195377Ssam	uint16_t baparamset, code;
1975234324Sadrian	int tid;
1976170530Ssam
1977195377Ssam	baparamset = LE_READ_2(frm+2);
1978195377Ssam	code = LE_READ_2(frm+4);
1979170530Ssam
1980195377Ssam	tid = MS(baparamset, IEEE80211_DELBAPS_TID);
1981170530Ssam
1982195377Ssam	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1983195377Ssam	    "recv DELBA: baparamset 0x%x (tid %d initiator %d) "
1984195377Ssam	    "code %d", baparamset, tid,
1985195377Ssam	    MS(baparamset, IEEE80211_DELBAPS_INIT), code);
1986195377Ssam
1987195377Ssam	if ((baparamset & IEEE80211_DELBAPS_INIT) == 0) {
1988234324Sadrian		tap = &ni->ni_tx_ampdu[tid];
1989195377Ssam		ic->ic_addba_stop(ni, tap);
1990195377Ssam	} else {
1991195377Ssam		rap = &ni->ni_rx_ampdu[tid];
1992195377Ssam		ic->ic_ampdu_rx_stop(ni, rap);
1993170530Ssam	}
1994195377Ssam	return 0;
1995170530Ssam}
1996170530Ssam
1997195377Ssamstatic int
1998195377Ssamht_recv_action_ht_txchwidth(struct ieee80211_node *ni,
1999195377Ssam	const struct ieee80211_frame *wh,
2000170530Ssam	const uint8_t *frm, const uint8_t *efrm)
2001170530Ssam{
2002170530Ssam	int chw;
2003170530Ssam
2004195377Ssam	chw = (frm[2] == IEEE80211_A_HT_TXCHWIDTH_2040) ? 40 : 20;
2005195377Ssam
2006195377Ssam	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2007195377Ssam	    "%s: HT txchwidth, width %d%s",
2008195377Ssam	    __func__, chw, ni->ni_chw != chw ? "*" : "");
2009195377Ssam	if (chw != ni->ni_chw) {
2010195377Ssam		ni->ni_chw = chw;
2011195377Ssam		/* XXX notify on change */
2012170530Ssam	}
2013195377Ssam	return 0;
2014170530Ssam}
2015170530Ssam
2016195377Ssamstatic int
2017195377Ssamht_recv_action_ht_mimopwrsave(struct ieee80211_node *ni,
2018195377Ssam	const struct ieee80211_frame *wh,
2019195377Ssam	const uint8_t *frm, const uint8_t *efrm)
2020195377Ssam{
2021195377Ssam	const struct ieee80211_action_ht_mimopowersave *mps =
2022195377Ssam	    (const struct ieee80211_action_ht_mimopowersave *) frm;
2023195377Ssam
2024195377Ssam	/* XXX check iv_htcaps */
2025195377Ssam	if (mps->am_control & IEEE80211_A_HT_MIMOPWRSAVE_ENA)
2026195377Ssam		ni->ni_flags |= IEEE80211_NODE_MIMO_PS;
2027195377Ssam	else
2028195377Ssam		ni->ni_flags &= ~IEEE80211_NODE_MIMO_PS;
2029195377Ssam	if (mps->am_control & IEEE80211_A_HT_MIMOPWRSAVE_MODE)
2030195377Ssam		ni->ni_flags |= IEEE80211_NODE_MIMO_RTS;
2031195377Ssam	else
2032195377Ssam		ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS;
2033195377Ssam	/* XXX notify on change */
2034195377Ssam	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2035195377Ssam	    "%s: HT MIMO PS (%s%s)", __func__,
2036195377Ssam	    (ni->ni_flags & IEEE80211_NODE_MIMO_PS) ?  "on" : "off",
2037195377Ssam	    (ni->ni_flags & IEEE80211_NODE_MIMO_RTS) ?  "+rts" : ""
2038195377Ssam	);
2039195377Ssam	return 0;
2040195377Ssam}
2041195377Ssam
2042170530Ssam/*
2043170530Ssam * Transmit processing.
2044170530Ssam */
2045170530Ssam
2046170530Ssam/*
2047178354Ssam * Check if A-MPDU should be requested/enabled for a stream.
2048178354Ssam * We require a traffic rate above a per-AC threshold and we
2049178354Ssam * also handle backoff from previous failed attempts.
2050178354Ssam *
2051178354Ssam * Drivers may override this method to bring in information
2052178354Ssam * such as link state conditions in making the decision.
2053178354Ssam */
2054178354Ssamstatic int
2055178354Ssamieee80211_ampdu_enable(struct ieee80211_node *ni,
2056178354Ssam	struct ieee80211_tx_ampdu *tap)
2057178354Ssam{
2058178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2059178354Ssam
2060234324Sadrian	if (tap->txa_avgpps <
2061234324Sadrian	    vap->iv_ampdu_mintraffic[TID_TO_WME_AC(tap->txa_tid)])
2062178354Ssam		return 0;
2063178354Ssam	/* XXX check rssi? */
2064178354Ssam	if (tap->txa_attempts >= ieee80211_addba_maxtries &&
2065178354Ssam	    ticks < tap->txa_nextrequest) {
2066178354Ssam		/*
2067178354Ssam		 * Don't retry too often; txa_nextrequest is set
2068178354Ssam		 * to the minimum interval we'll retry after
2069178354Ssam		 * ieee80211_addba_maxtries failed attempts are made.
2070178354Ssam		 */
2071178354Ssam		return 0;
2072178354Ssam	}
2073178354Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
2074234324Sadrian	    "enable AMPDU on tid %d (%s), avgpps %d pkts %d",
2075234324Sadrian	    tap->txa_tid, ieee80211_wme_acnames[TID_TO_WME_AC(tap->txa_tid)],
2076234324Sadrian	    tap->txa_avgpps, tap->txa_pkts);
2077178354Ssam	return 1;
2078178354Ssam}
2079178354Ssam
2080178354Ssam/*
2081170530Ssam * Request A-MPDU tx aggregation.  Setup local state and
2082170530Ssam * issue an ADDBA request.  BA use will only happen after
2083170530Ssam * the other end replies with ADDBA response.
2084170530Ssam */
2085170530Ssamint
2086170530Ssamieee80211_ampdu_request(struct ieee80211_node *ni,
2087170530Ssam	struct ieee80211_tx_ampdu *tap)
2088170530Ssam{
2089170530Ssam	struct ieee80211com *ic = ni->ni_ic;
2090205277Srpaulo	uint16_t args[5];
2091170530Ssam	int tid, dialogtoken;
2092170530Ssam	static int tokens = 0;	/* XXX */
2093170530Ssam
2094170530Ssam	/* XXX locking */
2095170530Ssam	if ((tap->txa_flags & IEEE80211_AGGR_SETUP) == 0) {
2096170530Ssam		/* do deferred setup of state */
2097184280Ssam		ampdu_tx_setup(tap);
2098170530Ssam	}
2099173273Ssam	/* XXX hack for not doing proper locking */
2100173273Ssam	tap->txa_flags &= ~IEEE80211_AGGR_NAK;
2101173273Ssam
2102170530Ssam	dialogtoken = (tokens+1) % 63;		/* XXX */
2103234324Sadrian	tid = tap->txa_tid;
2104183245Ssam	tap->txa_start = ni->ni_txseqs[tid];
2105170530Ssam
2106170530Ssam	args[0] = dialogtoken;
2107205277Srpaulo	args[1] = 0;	/* NB: status code not used */
2108205277Srpaulo	args[2]	= IEEE80211_BAPS_POLICY_IMMEDIATE
2109170530Ssam		| SM(tid, IEEE80211_BAPS_TID)
2110170530Ssam		| SM(IEEE80211_AGGR_BAWMAX, IEEE80211_BAPS_BUFSIZ)
2111170530Ssam		;
2112205277Srpaulo	args[3] = 0;	/* batimeout */
2113170530Ssam	/* NB: do first so there's no race against reply */
2114205277Srpaulo	if (!ic->ic_addba_request(ni, tap, dialogtoken, args[2], args[3])) {
2115170530Ssam		/* unable to setup state, don't make request */
2116178354Ssam		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2117234324Sadrian		    ni, "%s: could not setup BA stream for TID %d AC %d",
2118234324Sadrian		    __func__, tap->txa_tid, TID_TO_WME_AC(tap->txa_tid));
2119173273Ssam		/* defer next try so we don't slam the driver with requests */
2120178354Ssam		tap->txa_attempts = ieee80211_addba_maxtries;
2121178354Ssam		/* NB: check in case driver wants to override */
2122178354Ssam		if (tap->txa_nextrequest <= ticks)
2123178354Ssam			tap->txa_nextrequest = ticks + ieee80211_addba_backoff;
2124170530Ssam		return 0;
2125170530Ssam	}
2126170530Ssam	tokens = dialogtoken;			/* allocate token */
2127183245Ssam	/* NB: after calling ic_addba_request so driver can set txa_start */
2128205277Srpaulo	args[4] = SM(tap->txa_start, IEEE80211_BASEQ_START)
2129178953Ssam		| SM(0, IEEE80211_BASEQ_FRAG)
2130178953Ssam		;
2131170530Ssam	return ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
2132170530Ssam		IEEE80211_ACTION_BA_ADDBA_REQUEST, args);
2133170530Ssam}
2134170530Ssam
2135170530Ssam/*
2136173273Ssam * Terminate an AMPDU tx stream.  State is reclaimed
2137173273Ssam * and the peer notified with a DelBA Action frame.
2138173273Ssam */
2139173273Ssamvoid
2140183250Ssamieee80211_ampdu_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
2141183250Ssam	int reason)
2142173273Ssam{
2143173273Ssam	struct ieee80211com *ic = ni->ni_ic;
2144178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2145173273Ssam	uint16_t args[4];
2146173273Ssam
2147173273Ssam	/* XXX locking */
2148184280Ssam	tap->txa_flags &= ~IEEE80211_AGGR_BARPEND;
2149173273Ssam	if (IEEE80211_AMPDU_RUNNING(tap)) {
2150178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2151234324Sadrian		    ni, "%s: stop BA stream for TID %d (reason %d)",
2152234324Sadrian		    __func__, tap->txa_tid, reason);
2153178354Ssam		vap->iv_stats.is_ampdu_stop++;
2154173273Ssam
2155173273Ssam		ic->ic_addba_stop(ni, tap);
2156234324Sadrian		args[0] = tap->txa_tid;
2157173273Ssam		args[1] = IEEE80211_DELBAPS_INIT;
2158183250Ssam		args[2] = reason;			/* XXX reason code */
2159195377Ssam		ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
2160173273Ssam			IEEE80211_ACTION_BA_DELBA, args);
2161173273Ssam	} else {
2162178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2163234324Sadrian		    ni, "%s: BA stream for TID %d not running (reason %d)",
2164234324Sadrian		    __func__, tap->txa_tid, reason);
2165178354Ssam		vap->iv_stats.is_ampdu_stop_failed++;
2166173273Ssam	}
2167173273Ssam}
2168173273Ssam
2169184280Ssamstatic void
2170184280Ssambar_timeout(void *arg)
2171184280Ssam{
2172184280Ssam	struct ieee80211_tx_ampdu *tap = arg;
2173184280Ssam	struct ieee80211_node *ni = tap->txa_ni;
2174184280Ssam
2175184280Ssam	KASSERT((tap->txa_flags & IEEE80211_AGGR_XCHGPEND) == 0,
2176184280Ssam	    ("bar/addba collision, flags 0x%x", tap->txa_flags));
2177184280Ssam
2178184280Ssam	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2179184280Ssam	    ni, "%s: tid %u flags 0x%x attempts %d", __func__,
2180234324Sadrian	    tap->txa_tid, tap->txa_flags, tap->txa_attempts);
2181184280Ssam
2182184280Ssam	/* guard against race with bar_tx_complete */
2183184280Ssam	if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) == 0)
2184184280Ssam		return;
2185184280Ssam	/* XXX ? */
2186234018Sadrian	if (tap->txa_attempts >= ieee80211_bar_maxtries) {
2187234018Sadrian		ni->ni_vap->iv_stats.is_ampdu_bar_tx_fail++;
2188184280Ssam		ieee80211_ampdu_stop(ni, tap, IEEE80211_REASON_TIMEOUT);
2189234018Sadrian	} else {
2190234018Sadrian		ni->ni_vap->iv_stats.is_ampdu_bar_tx_retry++;
2191184280Ssam		ieee80211_send_bar(ni, tap, tap->txa_seqpending);
2192234018Sadrian	}
2193184280Ssam}
2194184280Ssam
2195184280Ssamstatic void
2196184280Ssambar_start_timer(struct ieee80211_tx_ampdu *tap)
2197184280Ssam{
2198184280Ssam	callout_reset(&tap->txa_timer, ieee80211_bar_timeout, bar_timeout, tap);
2199184280Ssam}
2200184280Ssam
2201184280Ssamstatic void
2202184280Ssambar_stop_timer(struct ieee80211_tx_ampdu *tap)
2203184280Ssam{
2204184280Ssam	callout_stop(&tap->txa_timer);
2205184280Ssam}
2206184280Ssam
2207184280Ssamstatic void
2208184280Ssambar_tx_complete(struct ieee80211_node *ni, void *arg, int status)
2209184280Ssam{
2210184280Ssam	struct ieee80211_tx_ampdu *tap = arg;
2211184280Ssam
2212184280Ssam	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2213184280Ssam	    ni, "%s: tid %u flags 0x%x pending %d status %d",
2214234324Sadrian	    __func__, tap->txa_tid, tap->txa_flags,
2215184280Ssam	    callout_pending(&tap->txa_timer), status);
2216184280Ssam
2217234018Sadrian	ni->ni_vap->iv_stats.is_ampdu_bar_tx++;
2218184280Ssam	/* XXX locking */
2219184280Ssam	if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) &&
2220184280Ssam	    callout_pending(&tap->txa_timer)) {
2221184280Ssam		struct ieee80211com *ic = ni->ni_ic;
2222184280Ssam
2223224907Sadrian		if (status == 0)		/* ACK'd */
2224184280Ssam			bar_stop_timer(tap);
2225184280Ssam		ic->ic_bar_response(ni, tap, status);
2226184280Ssam		/* NB: just let timer expire so we pace requests */
2227184280Ssam	}
2228184280Ssam}
2229184280Ssam
2230184280Ssamstatic void
2231184280Ssamieee80211_bar_response(struct ieee80211_node *ni,
2232184280Ssam	struct ieee80211_tx_ampdu *tap, int status)
2233184280Ssam{
2234184280Ssam
2235224907Sadrian	if (status == 0) {		/* got ACK */
2236184280Ssam		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2237184280Ssam		    ni, "BAR moves BA win <%u:%u> (%u frames) txseq %u tid %u",
2238184280Ssam		    tap->txa_start,
2239184280Ssam		    IEEE80211_SEQ_ADD(tap->txa_start, tap->txa_wnd-1),
2240184280Ssam		    tap->txa_qframes, tap->txa_seqpending,
2241234324Sadrian		    tap->txa_tid);
2242184280Ssam
2243184280Ssam		/* NB: timer already stopped in bar_tx_complete */
2244184280Ssam		tap->txa_start = tap->txa_seqpending;
2245184280Ssam		tap->txa_flags &= ~IEEE80211_AGGR_BARPEND;
2246184280Ssam	}
2247184280Ssam}
2248184280Ssam
2249173273Ssam/*
2250170530Ssam * Transmit a BAR frame to the specified node.  The
2251170530Ssam * BAR contents are drawn from the supplied aggregation
2252170530Ssam * state associated with the node.
2253184280Ssam *
2254184280Ssam * NB: we only handle immediate ACK w/ compressed bitmap.
2255170530Ssam */
2256170530Ssamint
2257170530Ssamieee80211_send_bar(struct ieee80211_node *ni,
2258184280Ssam	struct ieee80211_tx_ampdu *tap, ieee80211_seq seq)
2259170530Ssam{
2260178354Ssam#define	senderr(_x, _v)	do { vap->iv_stats._v++; ret = _x; goto bad; } while (0)
2261178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2262170530Ssam	struct ieee80211com *ic = ni->ni_ic;
2263184280Ssam	struct ieee80211_frame_bar *bar;
2264170530Ssam	struct mbuf *m;
2265184280Ssam	uint16_t barctl, barseqctl;
2266170530Ssam	uint8_t *frm;
2267170530Ssam	int tid, ret;
2268170530Ssam
2269184280Ssam	if ((tap->txa_flags & IEEE80211_AGGR_RUNNING) == 0) {
2270184280Ssam		/* no ADDBA response, should not happen */
2271184280Ssam		/* XXX stat+msg */
2272184280Ssam		return EINVAL;
2273184280Ssam	}
2274184280Ssam	/* XXX locking */
2275184280Ssam	bar_stop_timer(tap);
2276184280Ssam
2277170530Ssam	ieee80211_ref_node(ni);
2278170530Ssam
2279184280Ssam	m = ieee80211_getmgtframe(&frm, ic->ic_headroom, sizeof(*bar));
2280170530Ssam	if (m == NULL)
2281170530Ssam		senderr(ENOMEM, is_tx_nobuf);
2282170530Ssam
2283184280Ssam	if (!ieee80211_add_callback(m, bar_tx_complete, tap)) {
2284184280Ssam		m_freem(m);
2285184280Ssam		senderr(ENOMEM, is_tx_nobuf);	/* XXX */
2286184280Ssam		/* NOTREACHED */
2287184280Ssam	}
2288184280Ssam
2289184280Ssam	bar = mtod(m, struct ieee80211_frame_bar *);
2290184280Ssam	bar->i_fc[0] = IEEE80211_FC0_VERSION_0 |
2291170530Ssam		IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_BAR;
2292184280Ssam	bar->i_fc[1] = 0;
2293184280Ssam	IEEE80211_ADDR_COPY(bar->i_ra, ni->ni_macaddr);
2294184280Ssam	IEEE80211_ADDR_COPY(bar->i_ta, vap->iv_myaddr);
2295170530Ssam
2296234324Sadrian	tid = tap->txa_tid;
2297170530Ssam	barctl 	= (tap->txa_flags & IEEE80211_AGGR_IMMEDIATE ?
2298184280Ssam			0 : IEEE80211_BAR_NOACK)
2299184280Ssam		| IEEE80211_BAR_COMP
2300184280Ssam		| SM(tid, IEEE80211_BAR_TID)
2301170530Ssam		;
2302184280Ssam	barseqctl = SM(seq, IEEE80211_BAR_SEQ_START);
2303184280Ssam	/* NB: known to have proper alignment */
2304184280Ssam	bar->i_ctl = htole16(barctl);
2305184280Ssam	bar->i_seq = htole16(barseqctl);
2306184280Ssam	m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_bar);
2307170530Ssam
2308178354Ssam	M_WME_SETAC(m, WME_AC_VO);
2309178354Ssam
2310170530Ssam	IEEE80211_NODE_STAT(ni, tx_mgmt);	/* XXX tx_ctl? */
2311170530Ssam
2312184280Ssam	/* XXX locking */
2313184280Ssam	/* init/bump attempts counter */
2314184280Ssam	if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) == 0)
2315184280Ssam		tap->txa_attempts = 1;
2316184280Ssam	else
2317184280Ssam		tap->txa_attempts++;
2318184280Ssam	tap->txa_seqpending = seq;
2319184280Ssam	tap->txa_flags |= IEEE80211_AGGR_BARPEND;
2320170530Ssam
2321184280Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_11N,
2322184280Ssam	    ni, "send BAR: tid %u ctl 0x%x start %u (attempt %d)",
2323184280Ssam	    tid, barctl, seq, tap->txa_attempts);
2324184280Ssam
2325225013Sadrian	/*
2326225013Sadrian	 * ic_raw_xmit will free the node reference
2327225013Sadrian	 * regardless of queue/TX success or failure.
2328225013Sadrian	 */
2329184280Ssam	ret = ic->ic_raw_xmit(ni, m, NULL);
2330184280Ssam	if (ret != 0) {
2331184280Ssam		/* xmit failed, clear state flag */
2332184280Ssam		tap->txa_flags &= ~IEEE80211_AGGR_BARPEND;
2333234018Sadrian		vap->iv_stats.is_ampdu_bar_tx_fail++;
2334225013Sadrian		return ret;
2335184280Ssam	}
2336184280Ssam	/* XXX hack against tx complete happening before timer is started */
2337184280Ssam	if (tap->txa_flags & IEEE80211_AGGR_BARPEND)
2338184280Ssam		bar_start_timer(tap);
2339184280Ssam	return 0;
2340170530Ssambad:
2341234018Sadrian	vap->iv_stats.is_ampdu_bar_tx_fail++;
2342170530Ssam	ieee80211_free_node(ni);
2343170530Ssam	return ret;
2344170530Ssam#undef senderr
2345170530Ssam}
2346170530Ssam
2347195377Ssamstatic int
2348195377Ssamht_action_output(struct ieee80211_node *ni, struct mbuf *m)
2349195377Ssam{
2350195377Ssam	struct ieee80211_bpf_params params;
2351195377Ssam
2352195377Ssam	memset(&params, 0, sizeof(params));
2353195377Ssam	params.ibp_pri = WME_AC_VO;
2354195377Ssam	params.ibp_rate0 = ni->ni_txparms->mgmtrate;
2355195377Ssam	/* NB: we know all frames are unicast */
2356195377Ssam	params.ibp_try0 = ni->ni_txparms->maxretry;
2357195377Ssam	params.ibp_power = ni->ni_txpower;
2358195377Ssam	return ieee80211_mgmt_output(ni, m, IEEE80211_FC0_SUBTYPE_ACTION,
2359195377Ssam	     &params);
2360195377Ssam}
2361195377Ssam
2362195377Ssam#define	ADDSHORT(frm, v) do {			\
2363195377Ssam	frm[0] = (v) & 0xff;			\
2364195377Ssam	frm[1] = (v) >> 8;			\
2365195377Ssam	frm += 2;				\
2366195377Ssam} while (0)
2367195377Ssam
2368170530Ssam/*
2369170530Ssam * Send an action management frame.  The arguments are stuff
2370170530Ssam * into a frame without inspection; the caller is assumed to
2371170530Ssam * prepare them carefully (e.g. based on the aggregation state).
2372170530Ssam */
2373195377Ssamstatic int
2374195377Ssamht_send_action_ba_addba(struct ieee80211_node *ni,
2375195377Ssam	int category, int action, void *arg0)
2376170530Ssam{
2377178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2378170530Ssam	struct ieee80211com *ic = ni->ni_ic;
2379195377Ssam	uint16_t *args = arg0;
2380170530Ssam	struct mbuf *m;
2381170530Ssam	uint8_t *frm;
2382195377Ssam
2383195377Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2384205277Srpaulo	    "send ADDBA %s: dialogtoken %d status %d "
2385195377Ssam	    "baparamset 0x%x (tid %d) batimeout 0x%x baseqctl 0x%x",
2386195377Ssam	    (action == IEEE80211_ACTION_BA_ADDBA_REQUEST) ?
2387195377Ssam		"request" : "response",
2388205277Srpaulo	    args[0], args[1], args[2], MS(args[2], IEEE80211_BAPS_TID),
2389205277Srpaulo	    args[3], args[4]);
2390195377Ssam
2391195377Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2392195377Ssam	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2393195377Ssam	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2394195377Ssam	ieee80211_ref_node(ni);
2395195377Ssam
2396195377Ssam	m = ieee80211_getmgtframe(&frm,
2397195377Ssam	    ic->ic_headroom + sizeof(struct ieee80211_frame),
2398195377Ssam	    sizeof(uint16_t)	/* action+category */
2399195377Ssam	    /* XXX may action payload */
2400195377Ssam	    + sizeof(struct ieee80211_action_ba_addbaresponse)
2401195377Ssam	);
2402195377Ssam	if (m != NULL) {
2403195377Ssam		*frm++ = category;
2404195377Ssam		*frm++ = action;
2405195377Ssam		*frm++ = args[0];		/* dialog token */
2406205277Srpaulo		if (action == IEEE80211_ACTION_BA_ADDBA_RESPONSE)
2407205277Srpaulo			ADDSHORT(frm, args[1]);	/* status code */
2408205277Srpaulo		ADDSHORT(frm, args[2]);		/* baparamset */
2409205277Srpaulo		ADDSHORT(frm, args[3]);		/* batimeout */
2410195377Ssam		if (action == IEEE80211_ACTION_BA_ADDBA_REQUEST)
2411205277Srpaulo			ADDSHORT(frm, args[4]);	/* baseqctl */
2412195377Ssam		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2413195377Ssam		return ht_action_output(ni, m);
2414195377Ssam	} else {
2415195377Ssam		vap->iv_stats.is_tx_nobuf++;
2416195377Ssam		ieee80211_free_node(ni);
2417195377Ssam		return ENOMEM;
2418195377Ssam	}
2419195377Ssam}
2420195377Ssam
2421195377Ssamstatic int
2422195377Ssamht_send_action_ba_delba(struct ieee80211_node *ni,
2423195377Ssam	int category, int action, void *arg0)
2424195377Ssam{
2425195377Ssam	struct ieee80211vap *vap = ni->ni_vap;
2426195377Ssam	struct ieee80211com *ic = ni->ni_ic;
2427195377Ssam	uint16_t *args = arg0;
2428195377Ssam	struct mbuf *m;
2429170530Ssam	uint16_t baparamset;
2430195377Ssam	uint8_t *frm;
2431170530Ssam
2432195377Ssam	baparamset = SM(args[0], IEEE80211_DELBAPS_TID)
2433195377Ssam		   | args[1]
2434195377Ssam		   ;
2435195377Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2436195377Ssam	    "send DELBA action: tid %d, initiator %d reason %d",
2437195377Ssam	    args[0], args[1], args[2]);
2438170530Ssam
2439178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2440195377Ssam	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2441195377Ssam	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2442170530Ssam	ieee80211_ref_node(ni);
2443170530Ssam
2444170530Ssam	m = ieee80211_getmgtframe(&frm,
2445195377Ssam	    ic->ic_headroom + sizeof(struct ieee80211_frame),
2446195377Ssam	    sizeof(uint16_t)	/* action+category */
2447195377Ssam	    /* XXX may action payload */
2448195377Ssam	    + sizeof(struct ieee80211_action_ba_addbaresponse)
2449170530Ssam	);
2450195377Ssam	if (m != NULL) {
2451195377Ssam		*frm++ = category;
2452195377Ssam		*frm++ = action;
2453195377Ssam		ADDSHORT(frm, baparamset);
2454195377Ssam		ADDSHORT(frm, args[2]);		/* reason code */
2455195377Ssam		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2456195377Ssam		return ht_action_output(ni, m);
2457195377Ssam	} else {
2458195377Ssam		vap->iv_stats.is_tx_nobuf++;
2459195377Ssam		ieee80211_free_node(ni);
2460195377Ssam		return ENOMEM;
2461195377Ssam	}
2462195377Ssam}
2463170530Ssam
2464195377Ssamstatic int
2465195377Ssamht_send_action_ht_txchwidth(struct ieee80211_node *ni,
2466195377Ssam	int category, int action, void *arg0)
2467195377Ssam{
2468195377Ssam	struct ieee80211vap *vap = ni->ni_vap;
2469195377Ssam	struct ieee80211com *ic = ni->ni_ic;
2470195377Ssam	struct mbuf *m;
2471195377Ssam	uint8_t *frm;
2472170530Ssam
2473195377Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2474195377Ssam	    "send HT txchwidth: width %d",
2475195377Ssam	    IEEE80211_IS_CHAN_HT40(ni->ni_chan) ? 40 : 20);
2476170530Ssam
2477195377Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2478195377Ssam	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2479195377Ssam	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2480195377Ssam	ieee80211_ref_node(ni);
2481170530Ssam
2482195377Ssam	m = ieee80211_getmgtframe(&frm,
2483195377Ssam	    ic->ic_headroom + sizeof(struct ieee80211_frame),
2484195377Ssam	    sizeof(uint16_t)	/* action+category */
2485195377Ssam	    /* XXX may action payload */
2486195377Ssam	    + sizeof(struct ieee80211_action_ba_addbaresponse)
2487195377Ssam	);
2488195377Ssam	if (m != NULL) {
2489195377Ssam		*frm++ = category;
2490195377Ssam		*frm++ = action;
2491195377Ssam		*frm++ = IEEE80211_IS_CHAN_HT40(ni->ni_chan) ?
2492195377Ssam			IEEE80211_A_HT_TXCHWIDTH_2040 :
2493195377Ssam			IEEE80211_A_HT_TXCHWIDTH_20;
2494195377Ssam		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2495195377Ssam		return ht_action_output(ni, m);
2496195377Ssam	} else {
2497195377Ssam		vap->iv_stats.is_tx_nobuf++;
2498195377Ssam		ieee80211_free_node(ni);
2499195377Ssam		return ENOMEM;
2500170530Ssam	}
2501195377Ssam}
2502170530Ssam#undef ADDSHORT
2503170530Ssam
2504170530Ssam/*
2505219600Sbschmidt * Construct the MCS bit mask for inclusion in an HT capabilities
2506219600Sbschmidt * information element.
2507170530Ssam */
2508219600Sbschmidtstatic void
2509219600Sbschmidtieee80211_set_mcsset(struct ieee80211com *ic, uint8_t *frm)
2510170530Ssam{
2511170530Ssam	int i;
2512219600Sbschmidt	uint8_t txparams;
2513170530Ssam
2514219600Sbschmidt	KASSERT((ic->ic_rxstream > 0 && ic->ic_rxstream <= 4),
2515219600Sbschmidt	    ("ic_rxstream %d out of range", ic->ic_rxstream));
2516219600Sbschmidt	KASSERT((ic->ic_txstream > 0 && ic->ic_txstream <= 4),
2517219600Sbschmidt	    ("ic_txstream %d out of range", ic->ic_txstream));
2518219600Sbschmidt
2519219600Sbschmidt	for (i = 0; i < ic->ic_rxstream * 8; i++)
2520219600Sbschmidt		setbit(frm, i);
2521219600Sbschmidt	if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
2522219600Sbschmidt	    (ic->ic_htcaps & IEEE80211_HTC_RXMCS32))
2523219600Sbschmidt		setbit(frm, 32);
2524219600Sbschmidt	if (ic->ic_htcaps & IEEE80211_HTC_RXUNEQUAL) {
2525219600Sbschmidt		if (ic->ic_rxstream >= 2) {
2526219600Sbschmidt			for (i = 33; i <= 38; i++)
2527219600Sbschmidt				setbit(frm, i);
2528170530Ssam		}
2529219600Sbschmidt		if (ic->ic_rxstream >= 3) {
2530219600Sbschmidt			for (i = 39; i <= 52; i++)
2531219600Sbschmidt				setbit(frm, i);
2532219600Sbschmidt		}
2533219600Sbschmidt		if (ic->ic_txstream >= 4) {
2534219600Sbschmidt			for (i = 53; i <= 76; i++)
2535219600Sbschmidt				setbit(frm, i);
2536219600Sbschmidt		}
2537170530Ssam	}
2538219600Sbschmidt
2539219600Sbschmidt	if (ic->ic_rxstream != ic->ic_txstream) {
2540219600Sbschmidt		txparams = 0x1;			/* TX MCS set defined */
2541219600Sbschmidt		txparams |= 0x2;		/* TX RX MCS not equal */
2542219600Sbschmidt		txparams |= (ic->ic_txstream - 1) << 2;	/* num TX streams */
2543219600Sbschmidt		if (ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL)
2544219600Sbschmidt			txparams |= 0x16;	/* TX unequal modulation sup */
2545219600Sbschmidt	} else
2546219600Sbschmidt		txparams = 0;
2547219600Sbschmidt	frm[12] = txparams;
2548170530Ssam}
2549170530Ssam
2550170530Ssam/*
2551170530Ssam * Add body of an HTCAP information element.
2552170530Ssam */
2553170530Ssamstatic uint8_t *
2554170530Ssamieee80211_add_htcap_body(uint8_t *frm, struct ieee80211_node *ni)
2555170530Ssam{
2556170530Ssam#define	ADDSHORT(frm, v) do {			\
2557170530Ssam	frm[0] = (v) & 0xff;			\
2558170530Ssam	frm[1] = (v) >> 8;			\
2559170530Ssam	frm += 2;				\
2560170530Ssam} while (0)
2561222683Sbschmidt	struct ieee80211com *ic = ni->ni_ic;
2562178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2563205513Srpaulo	uint16_t caps, extcaps;
2564173865Ssam	int rxmax, density;
2565170530Ssam
2566170530Ssam	/* HT capabilities */
2567178354Ssam	caps = vap->iv_htcaps & 0xffff;
2568173273Ssam	/*
2569173273Ssam	 * Note channel width depends on whether we are operating as
2570173273Ssam	 * a sta or not.  When operating as a sta we are generating
2571173273Ssam	 * a request based on our desired configuration.  Otherwise
2572173273Ssam	 * we are operational and the channel attributes identify
2573173273Ssam	 * how we've been setup (which might be different if a fixed
2574173273Ssam	 * channel is specified).
2575173273Ssam	 */
2576178354Ssam	if (vap->iv_opmode == IEEE80211_M_STA) {
2577173273Ssam		/* override 20/40 use based on config */
2578193655Ssam		if (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)
2579173273Ssam			caps |= IEEE80211_HTCAP_CHWIDTH40;
2580173273Ssam		else
2581173273Ssam			caps &= ~IEEE80211_HTCAP_CHWIDTH40;
2582173865Ssam		/* use advertised setting (XXX locally constraint) */
2583173865Ssam		rxmax = MS(ni->ni_htparam, IEEE80211_HTCAP_MAXRXAMPDU);
2584173865Ssam		density = MS(ni->ni_htparam, IEEE80211_HTCAP_MPDUDENSITY);
2585222683Sbschmidt
2586222683Sbschmidt		/*
2587222683Sbschmidt		 * NB: Hardware might support HT40 on some but not all
2588222683Sbschmidt		 * channels. We can't determine this earlier because only
2589222683Sbschmidt		 * after association the channel is upgraded to HT based
2590222683Sbschmidt		 * on the negotiated capabilities.
2591222683Sbschmidt		 */
2592222683Sbschmidt		if (ni->ni_chan != IEEE80211_CHAN_ANYC &&
2593222683Sbschmidt		    findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT40U) == NULL &&
2594222683Sbschmidt		    findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT40D) == NULL)
2595222683Sbschmidt			caps &= ~IEEE80211_HTCAP_CHWIDTH40;
2596173273Ssam	} else {
2597173273Ssam		/* override 20/40 use based on current channel */
2598178354Ssam		if (IEEE80211_IS_CHAN_HT40(ni->ni_chan))
2599173273Ssam			caps |= IEEE80211_HTCAP_CHWIDTH40;
2600173273Ssam		else
2601173273Ssam			caps &= ~IEEE80211_HTCAP_CHWIDTH40;
2602178354Ssam		rxmax = vap->iv_ampdu_rxmax;
2603178354Ssam		density = vap->iv_ampdu_density;
2604173273Ssam	}
2605170530Ssam	/* adjust short GI based on channel and config */
2606193655Ssam	if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20) == 0)
2607170530Ssam		caps &= ~IEEE80211_HTCAP_SHORTGI20;
2608193655Ssam	if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40) == 0 ||
2609170530Ssam	    (caps & IEEE80211_HTCAP_CHWIDTH40) == 0)
2610170530Ssam		caps &= ~IEEE80211_HTCAP_SHORTGI40;
2611170530Ssam	ADDSHORT(frm, caps);
2612170530Ssam
2613170530Ssam	/* HT parameters */
2614173865Ssam	*frm = SM(rxmax, IEEE80211_HTCAP_MAXRXAMPDU)
2615173865Ssam	     | SM(density, IEEE80211_HTCAP_MPDUDENSITY)
2616173273Ssam	     ;
2617170530Ssam	frm++;
2618170530Ssam
2619170530Ssam	/* pre-zero remainder of ie */
2620170530Ssam	memset(frm, 0, sizeof(struct ieee80211_ie_htcap) -
2621170530Ssam		__offsetof(struct ieee80211_ie_htcap, hc_mcsset));
2622170530Ssam
2623170530Ssam	/* supported MCS set */
2624173273Ssam	/*
2625219600Sbschmidt	 * XXX: For sta mode the rate set should be restricted based
2626219600Sbschmidt	 * on the AP's capabilities, but ni_htrates isn't setup when
2627219600Sbschmidt	 * we're called to form an AssocReq frame so for now we're
2628219600Sbschmidt	 * restricted to the device capabilities.
2629173273Ssam	 */
2630219600Sbschmidt	ieee80211_set_mcsset(ni->ni_ic, frm);
2631170530Ssam
2632205513Srpaulo	frm += __offsetof(struct ieee80211_ie_htcap, hc_extcap) -
2633205513Srpaulo		__offsetof(struct ieee80211_ie_htcap, hc_mcsset);
2634205513Srpaulo
2635205513Srpaulo	/* HT extended capabilities */
2636205513Srpaulo	extcaps = vap->iv_htextcaps & 0xffff;
2637205513Srpaulo
2638205513Srpaulo	ADDSHORT(frm, extcaps);
2639205513Srpaulo
2640170530Ssam	frm += sizeof(struct ieee80211_ie_htcap) -
2641205513Srpaulo		__offsetof(struct ieee80211_ie_htcap, hc_txbf);
2642205513Srpaulo
2643170530Ssam	return frm;
2644170530Ssam#undef ADDSHORT
2645170530Ssam}
2646170530Ssam
2647170530Ssam/*
2648170530Ssam * Add 802.11n HT capabilities information element
2649170530Ssam */
2650170530Ssamuint8_t *
2651170530Ssamieee80211_add_htcap(uint8_t *frm, struct ieee80211_node *ni)
2652170530Ssam{
2653170530Ssam	frm[0] = IEEE80211_ELEMID_HTCAP;
2654170530Ssam	frm[1] = sizeof(struct ieee80211_ie_htcap) - 2;
2655170530Ssam	return ieee80211_add_htcap_body(frm + 2, ni);
2656170530Ssam}
2657170530Ssam
2658170530Ssam/*
2659170530Ssam * Add Broadcom OUI wrapped standard HTCAP ie; this is
2660170530Ssam * used for compatibility w/ pre-draft implementations.
2661170530Ssam */
2662170530Ssamuint8_t *
2663170530Ssamieee80211_add_htcap_vendor(uint8_t *frm, struct ieee80211_node *ni)
2664170530Ssam{
2665170530Ssam	frm[0] = IEEE80211_ELEMID_VENDOR;
2666170530Ssam	frm[1] = 4 + sizeof(struct ieee80211_ie_htcap) - 2;
2667170530Ssam	frm[2] = (BCM_OUI >> 0) & 0xff;
2668170530Ssam	frm[3] = (BCM_OUI >> 8) & 0xff;
2669170530Ssam	frm[4] = (BCM_OUI >> 16) & 0xff;
2670170530Ssam	frm[5] = BCM_OUI_HTCAP;
2671170530Ssam	return ieee80211_add_htcap_body(frm + 6, ni);
2672170530Ssam}
2673170530Ssam
2674170530Ssam/*
2675170530Ssam * Construct the MCS bit mask of basic rates
2676170530Ssam * for inclusion in an HT information element.
2677170530Ssam */
2678170530Ssamstatic void
2679170530Ssamieee80211_set_basic_htrates(uint8_t *frm, const struct ieee80211_htrateset *rs)
2680170530Ssam{
2681170530Ssam	int i;
2682170530Ssam
2683170530Ssam	for (i = 0; i < rs->rs_nrates; i++) {
2684170530Ssam		int r = rs->rs_rates[i] & IEEE80211_RATE_VAL;
2685170530Ssam		if ((rs->rs_rates[i] & IEEE80211_RATE_BASIC) &&
2686170530Ssam		    r < IEEE80211_HTRATE_MAXSIZE) {
2687170530Ssam			/* NB: this assumes a particular implementation */
2688170530Ssam			setbit(frm, r);
2689170530Ssam		}
2690170530Ssam	}
2691170530Ssam}
2692170530Ssam
2693170530Ssam/*
2694172211Ssam * Update the HTINFO ie for a beacon frame.
2695172211Ssam */
2696172211Ssamvoid
2697178354Ssamieee80211_ht_update_beacon(struct ieee80211vap *vap,
2698172211Ssam	struct ieee80211_beacon_offsets *bo)
2699172211Ssam{
2700172211Ssam#define	PROTMODE	(IEEE80211_HTINFO_OPMODE|IEEE80211_HTINFO_NONHT_PRESENT)
2701178354Ssam	const struct ieee80211_channel *bsschan = vap->iv_bss->ni_chan;
2702178354Ssam	struct ieee80211com *ic = vap->iv_ic;
2703172211Ssam	struct ieee80211_ie_htinfo *ht =
2704172211Ssam	   (struct ieee80211_ie_htinfo *) bo->bo_htinfo;
2705172211Ssam
2706172211Ssam	/* XXX only update on channel change */
2707178354Ssam	ht->hi_ctrlchannel = ieee80211_chan2ieee(ic, bsschan);
2708193655Ssam	if (vap->iv_flags_ht & IEEE80211_FHT_RIFS)
2709183256Ssam		ht->hi_byte1 = IEEE80211_HTINFO_RIFSMODE_PERM;
2710183256Ssam	else
2711183256Ssam		ht->hi_byte1 = IEEE80211_HTINFO_RIFSMODE_PROH;
2712178354Ssam	if (IEEE80211_IS_CHAN_HT40U(bsschan))
2713172211Ssam		ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_ABOVE;
2714178354Ssam	else if (IEEE80211_IS_CHAN_HT40D(bsschan))
2715172211Ssam		ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_BELOW;
2716172211Ssam	else
2717172211Ssam		ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_NONE;
2718178354Ssam	if (IEEE80211_IS_CHAN_HT40(bsschan))
2719172211Ssam		ht->hi_byte1 |= IEEE80211_HTINFO_TXWIDTH_2040;
2720172211Ssam
2721172211Ssam	/* protection mode */
2722172211Ssam	ht->hi_byte2 = (ht->hi_byte2 &~ PROTMODE) | ic->ic_curhtprotmode;
2723172211Ssam
2724172211Ssam	/* XXX propagate to vendor ie's */
2725172211Ssam#undef PROTMODE
2726172211Ssam}
2727172211Ssam
2728172211Ssam/*
2729170530Ssam * Add body of an HTINFO information element.
2730173273Ssam *
2731173273Ssam * NB: We don't use struct ieee80211_ie_htinfo because we can
2732173273Ssam * be called to fillin both a standard ie and a compat ie that
2733173273Ssam * has a vendor OUI at the front.
2734170530Ssam */
2735170530Ssamstatic uint8_t *
2736170530Ssamieee80211_add_htinfo_body(uint8_t *frm, struct ieee80211_node *ni)
2737170530Ssam{
2738183256Ssam	struct ieee80211vap *vap = ni->ni_vap;
2739170530Ssam	struct ieee80211com *ic = ni->ni_ic;
2740170530Ssam
2741170530Ssam	/* pre-zero remainder of ie */
2742170530Ssam	memset(frm, 0, sizeof(struct ieee80211_ie_htinfo) - 2);
2743170530Ssam
2744170530Ssam	/* primary/control channel center */
2745178354Ssam	*frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
2746170530Ssam
2747193655Ssam	if (vap->iv_flags_ht & IEEE80211_FHT_RIFS)
2748183256Ssam		frm[0] = IEEE80211_HTINFO_RIFSMODE_PERM;
2749183256Ssam	else
2750183256Ssam		frm[0] = IEEE80211_HTINFO_RIFSMODE_PROH;
2751178354Ssam	if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan))
2752170530Ssam		frm[0] |= IEEE80211_HTINFO_2NDCHAN_ABOVE;
2753178354Ssam	else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan))
2754170530Ssam		frm[0] |= IEEE80211_HTINFO_2NDCHAN_BELOW;
2755170530Ssam	else
2756170530Ssam		frm[0] |= IEEE80211_HTINFO_2NDCHAN_NONE;
2757178354Ssam	if (IEEE80211_IS_CHAN_HT40(ni->ni_chan))
2758170530Ssam		frm[0] |= IEEE80211_HTINFO_TXWIDTH_2040;
2759170530Ssam
2760172211Ssam	frm[1] = ic->ic_curhtprotmode;
2761170530Ssam
2762170530Ssam	frm += 5;
2763170530Ssam
2764170530Ssam	/* basic MCS set */
2765170530Ssam	ieee80211_set_basic_htrates(frm, &ni->ni_htrates);
2766170530Ssam	frm += sizeof(struct ieee80211_ie_htinfo) -
2767170530Ssam		__offsetof(struct ieee80211_ie_htinfo, hi_basicmcsset);
2768170530Ssam	return frm;
2769170530Ssam}
2770170530Ssam
2771170530Ssam/*
2772170530Ssam * Add 802.11n HT information information element.
2773170530Ssam */
2774170530Ssamuint8_t *
2775170530Ssamieee80211_add_htinfo(uint8_t *frm, struct ieee80211_node *ni)
2776170530Ssam{
2777170530Ssam	frm[0] = IEEE80211_ELEMID_HTINFO;
2778170530Ssam	frm[1] = sizeof(struct ieee80211_ie_htinfo) - 2;
2779170530Ssam	return ieee80211_add_htinfo_body(frm + 2, ni);
2780170530Ssam}
2781170530Ssam
2782170530Ssam/*
2783170530Ssam * Add Broadcom OUI wrapped standard HTINFO ie; this is
2784170530Ssam * used for compatibility w/ pre-draft implementations.
2785170530Ssam */
2786170530Ssamuint8_t *
2787170530Ssamieee80211_add_htinfo_vendor(uint8_t *frm, struct ieee80211_node *ni)
2788170530Ssam{
2789170530Ssam	frm[0] = IEEE80211_ELEMID_VENDOR;
2790170530Ssam	frm[1] = 4 + sizeof(struct ieee80211_ie_htinfo) - 2;
2791170530Ssam	frm[2] = (BCM_OUI >> 0) & 0xff;
2792170530Ssam	frm[3] = (BCM_OUI >> 8) & 0xff;
2793170530Ssam	frm[4] = (BCM_OUI >> 16) & 0xff;
2794170530Ssam	frm[5] = BCM_OUI_HTINFO;
2795170530Ssam	return ieee80211_add_htinfo_body(frm + 6, ni);
2796170530Ssam}
2797