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$");
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
1028242205Sadrian	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
1029242205Sadrian	    ni,
1030242205Sadrian	    "%s: called",
1031242205Sadrian	    __func__);
1032242205Sadrian
1033173273Ssam	if (ni->ni_flags & IEEE80211_NODE_HT) {
1034173273Ssam		/*
1035173273Ssam		 * Clean AMPDU state on re-associate.  This handles the case
1036173273Ssam		 * where a station leaves w/o notifying us and then returns
1037173273Ssam		 * before node is reaped for inactivity.
1038173273Ssam		 */
1039242205Sadrian		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
1040242205Sadrian		    ni,
1041242205Sadrian		    "%s: calling cleanup",
1042242205Sadrian		    __func__);
1043173273Ssam		ieee80211_ht_node_cleanup(ni);
1044173273Ssam	}
1045234324Sadrian	for (tid = 0; tid < WME_NUM_TID; tid++) {
1046234324Sadrian		tap = &ni->ni_tx_ampdu[tid];
1047234324Sadrian		tap->txa_tid = tid;
1048184280Ssam		tap->txa_ni = ni;
1049173273Ssam		/* NB: further initialization deferred */
1050170530Ssam	}
1051173273Ssam	ni->ni_flags |= IEEE80211_NODE_HT | IEEE80211_NODE_AMPDU;
1052170530Ssam}
1053170530Ssam
1054170530Ssam/*
1055170530Ssam * Cleanup HT-specific state in a node.  Called only
1056170530Ssam * when HT use has been marked.
1057170530Ssam */
1058170530Ssamvoid
1059170530Ssamieee80211_ht_node_cleanup(struct ieee80211_node *ni)
1060170530Ssam{
1061191552Ssam	struct ieee80211com *ic = ni->ni_ic;
1062170530Ssam	int i;
1063170530Ssam
1064242205Sadrian	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
1065242205Sadrian	    ni,
1066242205Sadrian	    "%s: called",
1067242205Sadrian	    __func__);
1068242205Sadrian
1069170530Ssam	KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT node"));
1070170530Ssam
1071170530Ssam	/* XXX optimize this */
1072234324Sadrian	for (i = 0; i < WME_NUM_TID; i++) {
1073170530Ssam		struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[i];
1074184280Ssam		if (tap->txa_flags & IEEE80211_AGGR_SETUP)
1075184280Ssam			ampdu_tx_stop(tap);
1076170530Ssam	}
1077170530Ssam	for (i = 0; i < WME_NUM_TID; i++)
1078191552Ssam		ic->ic_ampdu_rx_stop(ni, &ni->ni_rx_ampdu[i]);
1079170530Ssam
1080170530Ssam	ni->ni_htcap = 0;
1081182828Ssam	ni->ni_flags &= ~IEEE80211_NODE_HT_ALL;
1082170530Ssam}
1083170530Ssam
1084178354Ssam/*
1085178354Ssam * Age out HT resources for a station.
1086178354Ssam */
1087178354Ssamvoid
1088178354Ssamieee80211_ht_node_age(struct ieee80211_node *ni)
1089178354Ssam{
1090178354Ssam#ifdef IEEE80211_AMPDU_AGE
1091178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1092178354Ssam	uint8_t tid;
1093178354Ssam#endif
1094178354Ssam
1095178354Ssam	KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT sta"));
1096178354Ssam
1097178354Ssam#ifdef IEEE80211_AMPDU_AGE
1098178354Ssam	for (tid = 0; tid < WME_NUM_TID; tid++) {
1099178354Ssam		struct ieee80211_rx_ampdu *rap;
1100178354Ssam
1101178354Ssam		rap = &ni->ni_rx_ampdu[tid];
1102178354Ssam		if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0)
1103178354Ssam			continue;
1104178354Ssam		if (rap->rxa_qframes == 0)
1105178354Ssam			continue;
1106178354Ssam		/*
1107178354Ssam		 * Check for frames sitting too long in the reorder queue.
1108178354Ssam		 * See above for more details on what's happening here.
1109178354Ssam		 */
1110178354Ssam		/* XXX honor batimeout? */
1111178354Ssam		if (ticks - rap->rxa_age > ieee80211_ampdu_age) {
1112178354Ssam			/*
1113178354Ssam			 * Too long since we received the first
1114178354Ssam			 * frame; flush the reorder buffer.
1115178354Ssam			 */
1116178354Ssam			vap->iv_stats.is_ampdu_rx_age += rap->rxa_qframes;
1117178354Ssam			ampdu_rx_flush(ni, rap);
1118178354Ssam		}
1119178354Ssam	}
1120178354Ssam#endif /* IEEE80211_AMPDU_AGE */
1121178354Ssam}
1122178354Ssam
1123173273Ssamstatic struct ieee80211_channel *
1124173273Ssamfindhtchan(struct ieee80211com *ic, struct ieee80211_channel *c, int htflags)
1125173273Ssam{
1126173273Ssam	return ieee80211_find_channel(ic, c->ic_freq,
1127173273Ssam	    (c->ic_flags &~ IEEE80211_CHAN_HT) | htflags);
1128173273Ssam}
1129173273Ssam
1130173273Ssam/*
1131173273Ssam * Adjust a channel to be HT/non-HT according to the vap's configuration.
1132173273Ssam */
1133173273Ssamstruct ieee80211_channel *
1134173273Ssamieee80211_ht_adjust_channel(struct ieee80211com *ic,
1135173273Ssam	struct ieee80211_channel *chan, int flags)
1136173273Ssam{
1137173273Ssam	struct ieee80211_channel *c;
1138173273Ssam
1139193655Ssam	if (flags & IEEE80211_FHT_HT) {
1140173273Ssam		/* promote to HT if possible */
1141193655Ssam		if (flags & IEEE80211_FHT_USEHT40) {
1142173273Ssam			if (!IEEE80211_IS_CHAN_HT40(chan)) {
1143173273Ssam				/* NB: arbitrarily pick ht40+ over ht40- */
1144173273Ssam				c = findhtchan(ic, chan, IEEE80211_CHAN_HT40U);
1145173273Ssam				if (c == NULL)
1146173273Ssam					c = findhtchan(ic, chan,
1147173273Ssam						IEEE80211_CHAN_HT40D);
1148173273Ssam				if (c == NULL)
1149173273Ssam					c = findhtchan(ic, chan,
1150173273Ssam						IEEE80211_CHAN_HT20);
1151173273Ssam				if (c != NULL)
1152173273Ssam					chan = c;
1153173273Ssam			}
1154173273Ssam		} else if (!IEEE80211_IS_CHAN_HT20(chan)) {
1155173273Ssam			c = findhtchan(ic, chan, IEEE80211_CHAN_HT20);
1156173273Ssam			if (c != NULL)
1157173273Ssam				chan = c;
1158173273Ssam		}
1159173273Ssam	} else if (IEEE80211_IS_CHAN_HT(chan)) {
1160173273Ssam		/* demote to legacy, HT use is disabled */
1161173273Ssam		c = ieee80211_find_channel(ic, chan->ic_freq,
1162173273Ssam		    chan->ic_flags &~ IEEE80211_CHAN_HT);
1163173273Ssam		if (c != NULL)
1164173273Ssam			chan = c;
1165173273Ssam	}
1166173273Ssam	return chan;
1167173273Ssam}
1168173273Ssam
1169173273Ssam/*
1170173273Ssam * Setup HT-specific state for a legacy WDS peer.
1171173273Ssam */
1172173273Ssamvoid
1173173273Ssamieee80211_ht_wds_init(struct ieee80211_node *ni)
1174173273Ssam{
1175178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1176173273Ssam	struct ieee80211_tx_ampdu *tap;
1177234324Sadrian	int tid;
1178173273Ssam
1179193655Ssam	KASSERT(vap->iv_flags_ht & IEEE80211_FHT_HT, ("no HT requested"));
1180173273Ssam
1181173273Ssam	/* XXX check scan cache in case peer has an ap and we have info */
1182173273Ssam	/*
1183173273Ssam	 * If setup with a legacy channel; locate an HT channel.
1184173273Ssam	 * Otherwise if the inherited channel (from a companion
1185173273Ssam	 * AP) is suitable use it so we use the same location
1186173273Ssam	 * for the extension channel).
1187173273Ssam	 */
1188178354Ssam	ni->ni_chan = ieee80211_ht_adjust_channel(ni->ni_ic,
1189178354Ssam	    ni->ni_chan, ieee80211_htchanflags(ni->ni_chan));
1190173273Ssam
1191173273Ssam	ni->ni_htcap = 0;
1192193655Ssam	if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20)
1193173273Ssam		ni->ni_htcap |= IEEE80211_HTCAP_SHORTGI20;
1194173273Ssam	if (IEEE80211_IS_CHAN_HT40(ni->ni_chan)) {
1195173273Ssam		ni->ni_htcap |= IEEE80211_HTCAP_CHWIDTH40;
1196173273Ssam		ni->ni_chw = 40;
1197173273Ssam		if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan))
1198173273Ssam			ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_ABOVE;
1199173273Ssam		else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan))
1200173273Ssam			ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_BELOW;
1201193655Ssam		if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40)
1202173273Ssam			ni->ni_htcap |= IEEE80211_HTCAP_SHORTGI40;
1203173273Ssam	} else {
1204173273Ssam		ni->ni_chw = 20;
1205173273Ssam		ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_NONE;
1206173273Ssam	}
1207173273Ssam	ni->ni_htctlchan = ni->ni_chan->ic_ieee;
1208193655Ssam	if (vap->iv_flags_ht & IEEE80211_FHT_RIFS)
1209183256Ssam		ni->ni_flags |= IEEE80211_NODE_RIFS;
1210183256Ssam	/* XXX does it make sense to enable SMPS? */
1211173273Ssam
1212173273Ssam	ni->ni_htopmode = 0;		/* XXX need protection state */
1213173273Ssam	ni->ni_htstbc = 0;		/* XXX need info */
1214173273Ssam
1215234324Sadrian	for (tid = 0; tid < WME_NUM_TID; tid++) {
1216234324Sadrian		tap = &ni->ni_tx_ampdu[tid];
1217234324Sadrian		tap->txa_tid = tid;
1218173273Ssam	}
1219193655Ssam	/* NB: AMPDU tx/rx governed by IEEE80211_FHT_AMPDU_{TX,RX} */
1220173273Ssam	ni->ni_flags |= IEEE80211_NODE_HT | IEEE80211_NODE_AMPDU;
1221173273Ssam}
1222173273Ssam
1223173273Ssam/*
1224173273Ssam * Notify hostap vaps of a change in the HTINFO ie.
1225173273Ssam */
1226173273Ssamstatic void
1227173273Ssamhtinfo_notify(struct ieee80211com *ic)
1228173273Ssam{
1229178354Ssam	struct ieee80211vap *vap;
1230178354Ssam	int first = 1;
1231178354Ssam
1232178354Ssam	IEEE80211_LOCK_ASSERT(ic);
1233178354Ssam
1234178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1235178354Ssam		if (vap->iv_opmode != IEEE80211_M_HOSTAP)
1236178354Ssam			continue;
1237183253Ssam		if (vap->iv_state != IEEE80211_S_RUN ||
1238183253Ssam		    !IEEE80211_IS_CHAN_HT(vap->iv_bss->ni_chan))
1239183253Ssam			continue;
1240178354Ssam		if (first) {
1241178354Ssam			IEEE80211_NOTE(vap,
1242178354Ssam			    IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N,
1243178354Ssam			    vap->iv_bss,
1244178354Ssam			    "HT bss occupancy change: %d sta, %d ht, "
1245178354Ssam			    "%d ht40%s, HT protmode now 0x%x"
1246178354Ssam			    , ic->ic_sta_assoc
1247178354Ssam			    , ic->ic_ht_sta_assoc
1248178354Ssam			    , ic->ic_ht40_sta_assoc
1249193655Ssam			    , (ic->ic_flags_ht & IEEE80211_FHT_NONHT_PR) ?
1250178354Ssam				 ", non-HT sta present" : ""
1251178354Ssam			    , ic->ic_curhtprotmode);
1252178354Ssam			first = 0;
1253178354Ssam		}
1254178354Ssam		ieee80211_beacon_notify(vap, IEEE80211_BEACON_HTINFO);
1255178354Ssam	}
1256173273Ssam}
1257173273Ssam
1258173273Ssam/*
1259173273Ssam * Calculate HT protection mode from current
1260173273Ssam * state and handle updates.
1261173273Ssam */
1262173273Ssamstatic void
1263173273Ssamhtinfo_update(struct ieee80211com *ic)
1264173273Ssam{
1265173273Ssam	uint8_t protmode;
1266173273Ssam
1267178354Ssam	if (ic->ic_sta_assoc != ic->ic_ht_sta_assoc) {
1268178354Ssam		protmode = IEEE80211_HTINFO_OPMODE_MIXED
1269178354Ssam			 | IEEE80211_HTINFO_NONHT_PRESENT;
1270193655Ssam	} else if (ic->ic_flags_ht & IEEE80211_FHT_NONHT_PR) {
1271173273Ssam		protmode = IEEE80211_HTINFO_OPMODE_PROTOPT
1272178354Ssam			 | IEEE80211_HTINFO_NONHT_PRESENT;
1273178354Ssam	} else if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
1274178354Ssam	    IEEE80211_IS_CHAN_HT40(ic->ic_bsschan) &&
1275173273Ssam	    ic->ic_sta_assoc != ic->ic_ht40_sta_assoc) {
1276173273Ssam		protmode = IEEE80211_HTINFO_OPMODE_HT20PR;
1277173273Ssam	} else {
1278173273Ssam		protmode = IEEE80211_HTINFO_OPMODE_PURE;
1279173273Ssam	}
1280173273Ssam	if (protmode != ic->ic_curhtprotmode) {
1281173273Ssam		ic->ic_curhtprotmode = protmode;
1282173273Ssam		htinfo_notify(ic);
1283173273Ssam	}
1284173273Ssam}
1285173273Ssam
1286173273Ssam/*
1287173273Ssam * Handle an HT station joining a BSS.
1288173273Ssam */
1289173273Ssamvoid
1290173273Ssamieee80211_ht_node_join(struct ieee80211_node *ni)
1291173273Ssam{
1292173273Ssam	struct ieee80211com *ic = ni->ni_ic;
1293173273Ssam
1294173273Ssam	IEEE80211_LOCK_ASSERT(ic);
1295173273Ssam
1296173273Ssam	if (ni->ni_flags & IEEE80211_NODE_HT) {
1297173273Ssam		ic->ic_ht_sta_assoc++;
1298173273Ssam		if (ni->ni_chw == 40)
1299173273Ssam			ic->ic_ht40_sta_assoc++;
1300173273Ssam	}
1301173273Ssam	htinfo_update(ic);
1302173273Ssam}
1303173273Ssam
1304173273Ssam/*
1305173273Ssam * Handle an HT station leaving a BSS.
1306173273Ssam */
1307173273Ssamvoid
1308173273Ssamieee80211_ht_node_leave(struct ieee80211_node *ni)
1309173273Ssam{
1310173273Ssam	struct ieee80211com *ic = ni->ni_ic;
1311173273Ssam
1312173273Ssam	IEEE80211_LOCK_ASSERT(ic);
1313173273Ssam
1314173273Ssam	if (ni->ni_flags & IEEE80211_NODE_HT) {
1315173273Ssam		ic->ic_ht_sta_assoc--;
1316173273Ssam		if (ni->ni_chw == 40)
1317173273Ssam			ic->ic_ht40_sta_assoc--;
1318173273Ssam	}
1319173273Ssam	htinfo_update(ic);
1320173273Ssam}
1321173273Ssam
1322173273Ssam/*
1323173273Ssam * Public version of htinfo_update; used for processing
1324178354Ssam * beacon frames from overlapping bss.
1325178354Ssam *
1326178354Ssam * Caller can specify either IEEE80211_HTINFO_OPMODE_MIXED
1327178354Ssam * (on receipt of a beacon that advertises MIXED) or
1328178354Ssam * IEEE80211_HTINFO_OPMODE_PROTOPT (on receipt of a beacon
1329178354Ssam * from an overlapping legacy bss).  We treat MIXED with
1330178354Ssam * a higher precedence than PROTOPT (i.e. we will not change
1331178354Ssam * change PROTOPT -> MIXED; only MIXED -> PROTOPT).  This
1332178354Ssam * corresponds to how we handle things in htinfo_update.
1333173273Ssam */
1334173273Ssamvoid
1335178354Ssamieee80211_htprot_update(struct ieee80211com *ic, int protmode)
1336173273Ssam{
1337178354Ssam#define	OPMODE(x)	SM(x, IEEE80211_HTINFO_OPMODE)
1338183246Ssam	IEEE80211_LOCK(ic);
1339178354Ssam
1340178354Ssam	/* track non-HT station presence */
1341178354Ssam	KASSERT(protmode & IEEE80211_HTINFO_NONHT_PRESENT,
1342183246Ssam	    ("protmode 0x%x", protmode));
1343193655Ssam	ic->ic_flags_ht |= IEEE80211_FHT_NONHT_PR;
1344178354Ssam	ic->ic_lastnonht = ticks;
1345178354Ssam
1346183246Ssam	if (protmode != ic->ic_curhtprotmode &&
1347183246Ssam	    (OPMODE(ic->ic_curhtprotmode) != IEEE80211_HTINFO_OPMODE_MIXED ||
1348183246Ssam	     OPMODE(protmode) == IEEE80211_HTINFO_OPMODE_PROTOPT)) {
1349183246Ssam		/* push beacon update */
1350183246Ssam		ic->ic_curhtprotmode = protmode;
1351183246Ssam		htinfo_notify(ic);
1352183246Ssam	}
1353181197Ssam	IEEE80211_UNLOCK(ic);
1354178354Ssam#undef OPMODE
1355173273Ssam}
1356173273Ssam
1357173273Ssam/*
1358173273Ssam * Time out presence of an overlapping bss with non-HT
1359173273Ssam * stations.  When operating in hostap mode we listen for
1360173273Ssam * beacons from other stations and if we identify a non-HT
1361173273Ssam * station is present we update the opmode field of the
1362173273Ssam * HTINFO ie.  To identify when all non-HT stations are
1363173273Ssam * gone we time out this condition.
1364173273Ssam */
1365173273Ssamvoid
1366173273Ssamieee80211_ht_timeout(struct ieee80211com *ic)
1367173273Ssam{
1368173273Ssam	IEEE80211_LOCK_ASSERT(ic);
1369173273Ssam
1370193655Ssam	if ((ic->ic_flags_ht & IEEE80211_FHT_NONHT_PR) &&
1371173273Ssam	    time_after(ticks, ic->ic_lastnonht + IEEE80211_NONHT_PRESENT_AGE)) {
1372173273Ssam#if 0
1373178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
1374173273Ssam		    "%s", "time out non-HT STA present on channel");
1375173273Ssam#endif
1376193655Ssam		ic->ic_flags_ht &= ~IEEE80211_FHT_NONHT_PR;
1377173273Ssam		htinfo_update(ic);
1378173273Ssam	}
1379173273Ssam}
1380173273Ssam
1381170530Ssam/* unalligned little endian access */
1382170530Ssam#define LE_READ_2(p)					\
1383170530Ssam	((uint16_t)					\
1384170530Ssam	 ((((const uint8_t *)(p))[0]      ) |		\
1385170530Ssam	  (((const uint8_t *)(p))[1] <<  8)))
1386170530Ssam
1387170530Ssam/*
1388170530Ssam * Process an 802.11n HT capabilities ie.
1389170530Ssam */
1390170530Ssamvoid
1391170530Ssamieee80211_parse_htcap(struct ieee80211_node *ni, const uint8_t *ie)
1392170530Ssam{
1393170530Ssam	if (ie[0] == IEEE80211_ELEMID_VENDOR) {
1394170530Ssam		/*
1395170530Ssam		 * Station used Vendor OUI ie to associate;
1396170530Ssam		 * mark the node so when we respond we'll use
1397170530Ssam		 * the Vendor OUI's and not the standard ie's.
1398170530Ssam		 */
1399170530Ssam		ni->ni_flags |= IEEE80211_NODE_HTCOMPAT;
1400170530Ssam		ie += 4;
1401170530Ssam	} else
1402170530Ssam		ni->ni_flags &= ~IEEE80211_NODE_HTCOMPAT;
1403170530Ssam
1404170530Ssam	ni->ni_htcap = LE_READ_2(ie +
1405170530Ssam		__offsetof(struct ieee80211_ie_htcap, hc_cap));
1406170530Ssam	ni->ni_htparam = ie[__offsetof(struct ieee80211_ie_htcap, hc_param)];
1407170530Ssam}
1408170530Ssam
1409183254Ssamstatic void
1410183254Ssamhtinfo_parse(struct ieee80211_node *ni,
1411183254Ssam	const struct ieee80211_ie_htinfo *htinfo)
1412170530Ssam{
1413170530Ssam	uint16_t w;
1414170530Ssam
1415170530Ssam	ni->ni_htctlchan = htinfo->hi_ctrlchannel;
1416170530Ssam	ni->ni_ht2ndchan = SM(htinfo->hi_byte1, IEEE80211_HTINFO_2NDCHAN);
1417172055Ssam	w = LE_READ_2(&htinfo->hi_byte2);
1418170530Ssam	ni->ni_htopmode = SM(w, IEEE80211_HTINFO_OPMODE);
1419170530Ssam	w = LE_READ_2(&htinfo->hi_byte45);
1420170530Ssam	ni->ni_htstbc = SM(w, IEEE80211_HTINFO_BASIC_STBCMCS);
1421183254Ssam}
1422183254Ssam
1423183254Ssam/*
1424183254Ssam * Parse an 802.11n HT info ie and save useful information
1425183254Ssam * to the node state.  Note this does not effect any state
1426183254Ssam * changes such as for channel width change.
1427183254Ssam */
1428183254Ssamvoid
1429183254Ssamieee80211_parse_htinfo(struct ieee80211_node *ni, const uint8_t *ie)
1430183254Ssam{
1431183254Ssam	if (ie[0] == IEEE80211_ELEMID_VENDOR)
1432183254Ssam		ie += 4;
1433183254Ssam	htinfo_parse(ni, (const struct ieee80211_ie_htinfo *) ie);
1434183254Ssam}
1435183254Ssam
1436183254Ssam/*
1437183254Ssam * Handle 11n channel switch.  Use the received HT ie's to
1438183254Ssam * identify the right channel to use.  If we cannot locate it
1439183254Ssam * in the channel table then fallback to legacy operation.
1440183254Ssam * Note that we use this information to identify the node's
1441183254Ssam * channel only; the caller is responsible for insuring any
1442183254Ssam * required channel change is done (e.g. in sta mode when
1443183254Ssam * parsing the contents of a beacon frame).
1444183254Ssam */
1445233452Sadrianstatic int
1446183254Ssamhtinfo_update_chw(struct ieee80211_node *ni, int htflags)
1447183254Ssam{
1448183254Ssam	struct ieee80211com *ic = ni->ni_ic;
1449183254Ssam	struct ieee80211_channel *c;
1450183254Ssam	int chanflags;
1451233452Sadrian	int ret = 0;
1452183254Ssam
1453173273Ssam	chanflags = (ni->ni_chan->ic_flags &~ IEEE80211_CHAN_HT) | htflags;
1454173273Ssam	if (chanflags != ni->ni_chan->ic_flags) {
1455183254Ssam		/* XXX not right for ht40- */
1456173273Ssam		c = ieee80211_find_channel(ic, ni->ni_chan->ic_freq, chanflags);
1457178354Ssam		if (c == NULL && (htflags & IEEE80211_CHAN_HT40)) {
1458173273Ssam			/*
1459173273Ssam			 * No HT40 channel entry in our table; fall back
1460173273Ssam			 * to HT20 operation.  This should not happen.
1461173273Ssam			 */
1462173273Ssam			c = findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT20);
1463183254Ssam#if 0
1464183254Ssam			IEEE80211_NOTE(ni->ni_vap,
1465173273Ssam			    IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
1466173273Ssam			    "no HT40 channel (freq %u), falling back to HT20",
1467173273Ssam			    ni->ni_chan->ic_freq);
1468183254Ssam#endif
1469173273Ssam			/* XXX stat */
1470173273Ssam		}
1471173273Ssam		if (c != NULL && c != ni->ni_chan) {
1472183254Ssam			IEEE80211_NOTE(ni->ni_vap,
1473173273Ssam			    IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
1474173273Ssam			    "switch station to HT%d channel %u/0x%x",
1475173273Ssam			    IEEE80211_IS_CHAN_HT40(c) ? 40 : 20,
1476173273Ssam			    c->ic_freq, c->ic_flags);
1477173273Ssam			ni->ni_chan = c;
1478233452Sadrian			ret = 1;
1479173273Ssam		}
1480173273Ssam		/* NB: caller responsible for forcing any channel change */
1481173273Ssam	}
1482173273Ssam	/* update node's tx channel width */
1483173273Ssam	ni->ni_chw = IEEE80211_IS_CHAN_HT40(ni->ni_chan)? 40 : 20;
1484233452Sadrian	return (ret);
1485170530Ssam}
1486170530Ssam
1487170530Ssam/*
1488183255Ssam * Update 11n MIMO PS state according to received htcap.
1489183255Ssam */
1490183255Ssamstatic __inline int
1491183255Ssamhtcap_update_mimo_ps(struct ieee80211_node *ni)
1492183255Ssam{
1493183255Ssam	uint16_t oflags = ni->ni_flags;
1494183255Ssam
1495183255Ssam	switch (ni->ni_htcap & IEEE80211_HTCAP_SMPS) {
1496183255Ssam	case IEEE80211_HTCAP_SMPS_DYNAMIC:
1497183255Ssam		ni->ni_flags |= IEEE80211_NODE_MIMO_PS;
1498183255Ssam		ni->ni_flags |= IEEE80211_NODE_MIMO_RTS;
1499183255Ssam		break;
1500183255Ssam	case IEEE80211_HTCAP_SMPS_ENA:
1501183255Ssam		ni->ni_flags |= IEEE80211_NODE_MIMO_PS;
1502183255Ssam		ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS;
1503183255Ssam		break;
1504183255Ssam	case IEEE80211_HTCAP_SMPS_OFF:
1505183255Ssam	default:		/* disable on rx of reserved value */
1506183255Ssam		ni->ni_flags &= ~IEEE80211_NODE_MIMO_PS;
1507183255Ssam		ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS;
1508183255Ssam		break;
1509183255Ssam	}
1510183255Ssam	return (oflags ^ ni->ni_flags);
1511183255Ssam}
1512183255Ssam
1513183255Ssam/*
1514183257Ssam * Update short GI state according to received htcap
1515183257Ssam * and local settings.
1516183257Ssam */
1517183257Ssamstatic __inline void
1518183257Ssamhtcap_update_shortgi(struct ieee80211_node *ni)
1519183257Ssam{
1520183257Ssam	struct ieee80211vap *vap = ni->ni_vap;
1521183257Ssam
1522183257Ssam	ni->ni_flags &= ~(IEEE80211_NODE_SGI20|IEEE80211_NODE_SGI40);
1523183257Ssam	if ((ni->ni_htcap & IEEE80211_HTCAP_SHORTGI20) &&
1524193655Ssam	    (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20))
1525183257Ssam		ni->ni_flags |= IEEE80211_NODE_SGI20;
1526183257Ssam	if ((ni->ni_htcap & IEEE80211_HTCAP_SHORTGI40) &&
1527193655Ssam	    (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40))
1528183257Ssam		ni->ni_flags |= IEEE80211_NODE_SGI40;
1529183257Ssam}
1530183257Ssam
1531183257Ssam/*
1532183254Ssam * Parse and update HT-related state extracted from
1533183254Ssam * the HT cap and info ie's.
1534183254Ssam */
1535233452Sadrianint
1536183254Ssamieee80211_ht_updateparams(struct ieee80211_node *ni,
1537183254Ssam	const uint8_t *htcapie, const uint8_t *htinfoie)
1538183254Ssam{
1539183254Ssam	struct ieee80211vap *vap = ni->ni_vap;
1540183254Ssam	const struct ieee80211_ie_htinfo *htinfo;
1541183254Ssam	int htflags;
1542233452Sadrian	int ret = 0;
1543183254Ssam
1544183254Ssam	ieee80211_parse_htcap(ni, htcapie);
1545183255Ssam	if (vap->iv_htcaps & IEEE80211_HTCAP_SMPS)
1546183255Ssam		htcap_update_mimo_ps(ni);
1547183257Ssam	htcap_update_shortgi(ni);
1548183254Ssam
1549183254Ssam	if (htinfoie[0] == IEEE80211_ELEMID_VENDOR)
1550183254Ssam		htinfoie += 4;
1551183254Ssam	htinfo = (const struct ieee80211_ie_htinfo *) htinfoie;
1552183254Ssam	htinfo_parse(ni, htinfo);
1553183254Ssam
1554193655Ssam	htflags = (vap->iv_flags_ht & IEEE80211_FHT_HT) ?
1555183254Ssam	    IEEE80211_CHAN_HT20 : 0;
1556183254Ssam	/* NB: honor operating mode constraint */
1557183254Ssam	if ((htinfo->hi_byte1 & IEEE80211_HTINFO_TXWIDTH_2040) &&
1558193655Ssam	    (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)) {
1559183254Ssam		if (ni->ni_ht2ndchan == IEEE80211_HTINFO_2NDCHAN_ABOVE)
1560183254Ssam			htflags = IEEE80211_CHAN_HT40U;
1561183254Ssam		else if (ni->ni_ht2ndchan == IEEE80211_HTINFO_2NDCHAN_BELOW)
1562183254Ssam			htflags = IEEE80211_CHAN_HT40D;
1563183254Ssam	}
1564233452Sadrian	if (htinfo_update_chw(ni, htflags))
1565233452Sadrian		ret = 1;
1566183256Ssam
1567183256Ssam	if ((htinfo->hi_byte1 & IEEE80211_HTINFO_RIFSMODE_PERM) &&
1568193655Ssam	    (vap->iv_flags_ht & IEEE80211_FHT_RIFS))
1569183256Ssam		ni->ni_flags |= IEEE80211_NODE_RIFS;
1570183256Ssam	else
1571183256Ssam		ni->ni_flags &= ~IEEE80211_NODE_RIFS;
1572233452Sadrian
1573233452Sadrian	return (ret);
1574183254Ssam}
1575183254Ssam
1576183254Ssam/*
1577183254Ssam * Parse and update HT-related state extracted from the HT cap ie
1578183254Ssam * for a station joining an HT BSS.
1579183254Ssam */
1580183254Ssamvoid
1581183254Ssamieee80211_ht_updatehtcap(struct ieee80211_node *ni, const uint8_t *htcapie)
1582183254Ssam{
1583183254Ssam	struct ieee80211vap *vap = ni->ni_vap;
1584183254Ssam	int htflags;
1585183254Ssam
1586183254Ssam	ieee80211_parse_htcap(ni, htcapie);
1587183255Ssam	if (vap->iv_htcaps & IEEE80211_HTCAP_SMPS)
1588183255Ssam		htcap_update_mimo_ps(ni);
1589183257Ssam	htcap_update_shortgi(ni);
1590183254Ssam
1591183254Ssam	/* NB: honor operating mode constraint */
1592219606Sbschmidt	/* XXX 40 MHz intolerant */
1593193655Ssam	htflags = (vap->iv_flags_ht & IEEE80211_FHT_HT) ?
1594183254Ssam	    IEEE80211_CHAN_HT20 : 0;
1595183254Ssam	if ((ni->ni_htcap & IEEE80211_HTCAP_CHWIDTH40) &&
1596193655Ssam	    (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)) {
1597183254Ssam		if (IEEE80211_IS_CHAN_HT40U(vap->iv_bss->ni_chan))
1598183254Ssam			htflags = IEEE80211_CHAN_HT40U;
1599183254Ssam		else if (IEEE80211_IS_CHAN_HT40D(vap->iv_bss->ni_chan))
1600183254Ssam			htflags = IEEE80211_CHAN_HT40D;
1601183254Ssam	}
1602233452Sadrian	(void) htinfo_update_chw(ni, htflags);
1603183254Ssam}
1604183254Ssam
1605183254Ssam/*
1606170530Ssam * Install received HT rate set by parsing the HT cap ie.
1607170530Ssam */
1608170530Ssamint
1609170530Ssamieee80211_setup_htrates(struct ieee80211_node *ni, const uint8_t *ie, int flags)
1610170530Ssam{
1611219602Sbschmidt	struct ieee80211com *ic = ni->ni_ic;
1612178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1613170530Ssam	const struct ieee80211_ie_htcap *htcap;
1614170530Ssam	struct ieee80211_htrateset *rs;
1615219602Sbschmidt	int i, maxequalmcs, maxunequalmcs;
1616170530Ssam
1617219602Sbschmidt	maxequalmcs = ic->ic_txstream * 8 - 1;
1618219602Sbschmidt	if (ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL) {
1619219602Sbschmidt		if (ic->ic_txstream >= 2)
1620219602Sbschmidt			maxunequalmcs = 38;
1621219602Sbschmidt		if (ic->ic_txstream >= 3)
1622219602Sbschmidt			maxunequalmcs = 52;
1623219602Sbschmidt		if (ic->ic_txstream >= 4)
1624219602Sbschmidt			maxunequalmcs = 76;
1625219602Sbschmidt	} else
1626219602Sbschmidt		maxunequalmcs = 0;
1627219602Sbschmidt
1628170530Ssam	rs = &ni->ni_htrates;
1629170530Ssam	memset(rs, 0, sizeof(*rs));
1630170530Ssam	if (ie != NULL) {
1631170530Ssam		if (ie[0] == IEEE80211_ELEMID_VENDOR)
1632170530Ssam			ie += 4;
1633170530Ssam		htcap = (const struct ieee80211_ie_htcap *) ie;
1634170530Ssam		for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++) {
1635170530Ssam			if (isclr(htcap->hc_mcsset, i))
1636170530Ssam				continue;
1637170530Ssam			if (rs->rs_nrates == IEEE80211_HTRATE_MAXSIZE) {
1638178354Ssam				IEEE80211_NOTE(vap,
1639170530Ssam				    IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
1640170530Ssam				    "WARNING, HT rate set too large; only "
1641170530Ssam				    "using %u rates", IEEE80211_HTRATE_MAXSIZE);
1642178354Ssam				vap->iv_stats.is_rx_rstoobig++;
1643170530Ssam				break;
1644170530Ssam			}
1645219602Sbschmidt			if (i <= 31 && i > maxequalmcs)
1646219602Sbschmidt				continue;
1647219602Sbschmidt			if (i == 32 &&
1648219602Sbschmidt			    (ic->ic_htcaps & IEEE80211_HTC_TXMCS32) == 0)
1649219602Sbschmidt				continue;
1650219602Sbschmidt			if (i > 32 && i > maxunequalmcs)
1651219602Sbschmidt				continue;
1652170530Ssam			rs->rs_rates[rs->rs_nrates++] = i;
1653170530Ssam		}
1654170530Ssam	}
1655170530Ssam	return ieee80211_fix_rate(ni, (struct ieee80211_rateset *) rs, flags);
1656170530Ssam}
1657170530Ssam
1658170530Ssam/*
1659170530Ssam * Mark rates in a node's HT rate set as basic according
1660170530Ssam * to the information in the supplied HT info ie.
1661170530Ssam */
1662170530Ssamvoid
1663170530Ssamieee80211_setup_basic_htrates(struct ieee80211_node *ni, const uint8_t *ie)
1664170530Ssam{
1665170530Ssam	const struct ieee80211_ie_htinfo *htinfo;
1666170530Ssam	struct ieee80211_htrateset *rs;
1667170530Ssam	int i, j;
1668170530Ssam
1669170530Ssam	if (ie[0] == IEEE80211_ELEMID_VENDOR)
1670170530Ssam		ie += 4;
1671170530Ssam	htinfo = (const struct ieee80211_ie_htinfo *) ie;
1672170530Ssam	rs = &ni->ni_htrates;
1673170530Ssam	if (rs->rs_nrates == 0) {
1674178354Ssam		IEEE80211_NOTE(ni->ni_vap,
1675170530Ssam		    IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
1676170530Ssam		    "%s", "WARNING, empty HT rate set");
1677170530Ssam		return;
1678170530Ssam	}
1679170530Ssam	for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++) {
1680170530Ssam		if (isclr(htinfo->hi_basicmcsset, i))
1681170530Ssam			continue;
1682170530Ssam		for (j = 0; j < rs->rs_nrates; j++)
1683170530Ssam			if ((rs->rs_rates[j] & IEEE80211_RATE_VAL) == i)
1684170530Ssam				rs->rs_rates[j] |= IEEE80211_RATE_BASIC;
1685170530Ssam	}
1686170530Ssam}
1687170530Ssam
1688170530Ssamstatic void
1689184280Ssamampdu_tx_setup(struct ieee80211_tx_ampdu *tap)
1690184280Ssam{
1691184280Ssam	callout_init(&tap->txa_timer, CALLOUT_MPSAFE);
1692184280Ssam	tap->txa_flags |= IEEE80211_AGGR_SETUP;
1693184280Ssam}
1694184280Ssam
1695184280Ssamstatic void
1696184280Ssamampdu_tx_stop(struct ieee80211_tx_ampdu *tap)
1697184280Ssam{
1698184280Ssam	struct ieee80211_node *ni = tap->txa_ni;
1699184280Ssam	struct ieee80211com *ic = ni->ni_ic;
1700184280Ssam
1701242205Sadrian	IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
1702242205Sadrian	    tap->txa_ni,
1703242205Sadrian	    "%s: called",
1704242205Sadrian	    __func__);
1705242205Sadrian
1706184280Ssam	KASSERT(tap->txa_flags & IEEE80211_AGGR_SETUP,
1707234324Sadrian	    ("txa_flags 0x%x tid %d ac %d", tap->txa_flags, tap->txa_tid,
1708234324Sadrian	    TID_TO_WME_AC(tap->txa_tid)));
1709184280Ssam
1710184280Ssam	/*
1711184280Ssam	 * Stop BA stream if setup so driver has a chance
1712184280Ssam	 * to reclaim any resources it might have allocated.
1713184280Ssam	 */
1714184280Ssam	ic->ic_addba_stop(ni, tap);
1715184280Ssam	/*
1716184280Ssam	 * Stop any pending BAR transmit.
1717184280Ssam	 */
1718184280Ssam	bar_stop_timer(tap);
1719184280Ssam
1720184280Ssam	tap->txa_lastsample = 0;
1721184280Ssam	tap->txa_avgpps = 0;
1722184280Ssam	/* NB: clearing NAK means we may re-send ADDBA */
1723184280Ssam	tap->txa_flags &= ~(IEEE80211_AGGR_SETUP | IEEE80211_AGGR_NAK);
1724184280Ssam}
1725184280Ssam
1726223331Sadrian/*
1727223331Sadrian * ADDBA response timeout.
1728223331Sadrian *
1729223331Sadrian * If software aggregation and per-TID queue management was done here,
1730223331Sadrian * that queue would be unpaused after the ADDBA timeout occurs.
1731223331Sadrian */
1732184280Ssamstatic void
1733170530Ssamaddba_timeout(void *arg)
1734170530Ssam{
1735170530Ssam	struct ieee80211_tx_ampdu *tap = arg;
1736223331Sadrian	struct ieee80211_node *ni = tap->txa_ni;
1737223331Sadrian	struct ieee80211com *ic = ni->ni_ic;
1738170530Ssam
1739170530Ssam	/* XXX ? */
1740170530Ssam	tap->txa_flags &= ~IEEE80211_AGGR_XCHGPEND;
1741170530Ssam	tap->txa_attempts++;
1742223331Sadrian	ic->ic_addba_response_timeout(ni, tap);
1743170530Ssam}
1744170530Ssam
1745170530Ssamstatic void
1746170530Ssamaddba_start_timeout(struct ieee80211_tx_ampdu *tap)
1747170530Ssam{
1748170530Ssam	/* XXX use CALLOUT_PENDING instead? */
1749178354Ssam	callout_reset(&tap->txa_timer, ieee80211_addba_timeout,
1750170530Ssam	    addba_timeout, tap);
1751170530Ssam	tap->txa_flags |= IEEE80211_AGGR_XCHGPEND;
1752178354Ssam	tap->txa_nextrequest = ticks + ieee80211_addba_timeout;
1753170530Ssam}
1754170530Ssam
1755170530Ssamstatic void
1756170530Ssamaddba_stop_timeout(struct ieee80211_tx_ampdu *tap)
1757170530Ssam{
1758170530Ssam	/* XXX use CALLOUT_PENDING instead? */
1759170530Ssam	if (tap->txa_flags & IEEE80211_AGGR_XCHGPEND) {
1760170530Ssam		callout_stop(&tap->txa_timer);
1761170530Ssam		tap->txa_flags &= ~IEEE80211_AGGR_XCHGPEND;
1762170530Ssam	}
1763170530Ssam}
1764170530Ssam
1765223331Sadrianstatic void
1766223331Sadriannull_addba_response_timeout(struct ieee80211_node *ni,
1767223331Sadrian    struct ieee80211_tx_ampdu *tap)
1768223331Sadrian{
1769223331Sadrian}
1770223331Sadrian
1771170530Ssam/*
1772170530Ssam * Default method for requesting A-MPDU tx aggregation.
1773170530Ssam * We setup the specified state block and start a timer
1774170530Ssam * to wait for an ADDBA response frame.
1775170530Ssam */
1776170530Ssamstatic int
1777170530Ssamieee80211_addba_request(struct ieee80211_node *ni,
1778170530Ssam	struct ieee80211_tx_ampdu *tap,
1779170530Ssam	int dialogtoken, int baparamset, int batimeout)
1780170530Ssam{
1781170530Ssam	int bufsiz;
1782170530Ssam
1783170530Ssam	/* XXX locking */
1784170530Ssam	tap->txa_token = dialogtoken;
1785170530Ssam	tap->txa_flags |= IEEE80211_AGGR_IMMEDIATE;
1786170530Ssam	bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
1787170530Ssam	tap->txa_wnd = (bufsiz == 0) ?
1788170530Ssam	    IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
1789170530Ssam	addba_start_timeout(tap);
1790170530Ssam	return 1;
1791170530Ssam}
1792170530Ssam
1793170530Ssam/*
1794170530Ssam * Default method for processing an A-MPDU tx aggregation
1795170530Ssam * response.  We shutdown any pending timer and update the
1796170530Ssam * state block according to the reply.
1797170530Ssam */
1798170530Ssamstatic int
1799170530Ssamieee80211_addba_response(struct ieee80211_node *ni,
1800170530Ssam	struct ieee80211_tx_ampdu *tap,
1801170530Ssam	int status, int baparamset, int batimeout)
1802170530Ssam{
1803184280Ssam	int bufsiz, tid;
1804170530Ssam
1805170530Ssam	/* XXX locking */
1806170530Ssam	addba_stop_timeout(tap);
1807170530Ssam	if (status == IEEE80211_STATUS_SUCCESS) {
1808170530Ssam		bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
1809170530Ssam		/* XXX override our request? */
1810170530Ssam		tap->txa_wnd = (bufsiz == 0) ?
1811170530Ssam		    IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
1812184280Ssam		/* XXX AC/TID */
1813184280Ssam		tid = MS(baparamset, IEEE80211_BAPS_TID);
1814170530Ssam		tap->txa_flags |= IEEE80211_AGGR_RUNNING;
1815184280Ssam		tap->txa_attempts = 0;
1816173273Ssam	} else {
1817173273Ssam		/* mark tid so we don't try again */
1818173273Ssam		tap->txa_flags |= IEEE80211_AGGR_NAK;
1819170530Ssam	}
1820170530Ssam	return 1;
1821170530Ssam}
1822170530Ssam
1823170530Ssam/*
1824170530Ssam * Default method for stopping A-MPDU tx aggregation.
1825170530Ssam * Any timer is cleared and we drain any pending frames.
1826170530Ssam */
1827170530Ssamstatic void
1828170530Ssamieee80211_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
1829170530Ssam{
1830170530Ssam	/* XXX locking */
1831170530Ssam	addba_stop_timeout(tap);
1832170530Ssam	if (tap->txa_flags & IEEE80211_AGGR_RUNNING) {
1833182830Ssam		/* XXX clear aggregation queue */
1834170530Ssam		tap->txa_flags &= ~IEEE80211_AGGR_RUNNING;
1835170530Ssam	}
1836170530Ssam	tap->txa_attempts = 0;
1837170530Ssam}
1838170530Ssam
1839170530Ssam/*
1840170530Ssam * Process a received action frame using the default aggregation
1841170530Ssam * policy.  We intercept ADDBA-related frames and use them to
1842170530Ssam * update our aggregation state.  All other frames are passed up
1843170530Ssam * for processing by ieee80211_recv_action.
1844170530Ssam */
1845195377Ssamstatic int
1846195377Ssamht_recv_action_ba_addba_request(struct ieee80211_node *ni,
1847195377Ssam	const struct ieee80211_frame *wh,
1848170530Ssam	const uint8_t *frm, const uint8_t *efrm)
1849170530Ssam{
1850170530Ssam	struct ieee80211com *ic = ni->ni_ic;
1851178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1852170530Ssam	struct ieee80211_rx_ampdu *rap;
1853195377Ssam	uint8_t dialogtoken;
1854195377Ssam	uint16_t baparamset, batimeout, baseqctl;
1855205277Srpaulo	uint16_t args[5];
1856195377Ssam	int tid;
1857170530Ssam
1858195377Ssam	dialogtoken = frm[2];
1859195377Ssam	baparamset = LE_READ_2(frm+3);
1860195377Ssam	batimeout = LE_READ_2(frm+5);
1861195377Ssam	baseqctl = LE_READ_2(frm+7);
1862170530Ssam
1863195377Ssam	tid = MS(baparamset, IEEE80211_BAPS_TID);
1864170530Ssam
1865195377Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1866195377Ssam	    "recv ADDBA request: dialogtoken %u baparamset 0x%x "
1867195377Ssam	    "(tid %d bufsiz %d) batimeout %d baseqctl %d:%d",
1868195377Ssam	    dialogtoken, baparamset,
1869195377Ssam	    tid, MS(baparamset, IEEE80211_BAPS_BUFSIZ),
1870195377Ssam	    batimeout,
1871195377Ssam	    MS(baseqctl, IEEE80211_BASEQ_START),
1872195377Ssam	    MS(baseqctl, IEEE80211_BASEQ_FRAG));
1873170530Ssam
1874195377Ssam	rap = &ni->ni_rx_ampdu[tid];
1875170530Ssam
1876195377Ssam	/* Send ADDBA response */
1877195377Ssam	args[0] = dialogtoken;
1878195377Ssam	/*
1879195377Ssam	 * NB: We ack only if the sta associated with HT and
1880195377Ssam	 * the ap is configured to do AMPDU rx (the latter
1881195377Ssam	 * violates the 11n spec and is mostly for testing).
1882195377Ssam	 */
1883195377Ssam	if ((ni->ni_flags & IEEE80211_NODE_AMPDU_RX) &&
1884195377Ssam	    (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_RX)) {
1885195377Ssam		/* XXX handle ampdu_rx_start failure */
1886195377Ssam		ic->ic_ampdu_rx_start(ni, rap,
1887195377Ssam		    baparamset, batimeout, baseqctl);
1888170530Ssam
1889195377Ssam		args[1] = IEEE80211_STATUS_SUCCESS;
1890195377Ssam	} else {
1891195377Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1892195377Ssam		    ni, "reject ADDBA request: %s",
1893195377Ssam		    ni->ni_flags & IEEE80211_NODE_AMPDU_RX ?
1894195377Ssam		       "administratively disabled" :
1895195377Ssam		       "not negotiated for station");
1896195377Ssam		vap->iv_stats.is_addba_reject++;
1897195377Ssam		args[1] = IEEE80211_STATUS_UNSPECIFIED;
1898195377Ssam	}
1899195377Ssam	/* XXX honor rap flags? */
1900195377Ssam	args[2] = IEEE80211_BAPS_POLICY_IMMEDIATE
1901195377Ssam		| SM(tid, IEEE80211_BAPS_TID)
1902195377Ssam		| SM(rap->rxa_wnd, IEEE80211_BAPS_BUFSIZ)
1903195377Ssam		;
1904195377Ssam	args[3] = 0;
1905205277Srpaulo	args[4] = 0;
1906195377Ssam	ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
1907195377Ssam		IEEE80211_ACTION_BA_ADDBA_RESPONSE, args);
1908195377Ssam	return 0;
1909195377Ssam}
1910170530Ssam
1911195377Ssamstatic int
1912195377Ssamht_recv_action_ba_addba_response(struct ieee80211_node *ni,
1913195377Ssam	const struct ieee80211_frame *wh,
1914195377Ssam	const uint8_t *frm, const uint8_t *efrm)
1915195377Ssam{
1916195377Ssam	struct ieee80211com *ic = ni->ni_ic;
1917195377Ssam	struct ieee80211vap *vap = ni->ni_vap;
1918195377Ssam	struct ieee80211_tx_ampdu *tap;
1919195377Ssam	uint8_t dialogtoken, policy;
1920195377Ssam	uint16_t baparamset, batimeout, code;
1921234324Sadrian	int tid, bufsiz;
1922170530Ssam
1923195377Ssam	dialogtoken = frm[2];
1924195377Ssam	code = LE_READ_2(frm+3);
1925195377Ssam	baparamset = LE_READ_2(frm+5);
1926195377Ssam	tid = MS(baparamset, IEEE80211_BAPS_TID);
1927195377Ssam	bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
1928195377Ssam	policy = MS(baparamset, IEEE80211_BAPS_POLICY);
1929195377Ssam	batimeout = LE_READ_2(frm+7);
1930195377Ssam
1931234324Sadrian	tap = &ni->ni_tx_ampdu[tid];
1932195377Ssam	if ((tap->txa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
1933195377Ssam		IEEE80211_DISCARD_MAC(vap,
1934195377Ssam		    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1935195377Ssam		    ni->ni_macaddr, "ADDBA response",
1936195377Ssam		    "no pending ADDBA, tid %d dialogtoken %u "
1937195377Ssam		    "code %d", tid, dialogtoken, code);
1938195377Ssam		vap->iv_stats.is_addba_norequest++;
1939195377Ssam		return 0;
1940195377Ssam	}
1941195377Ssam	if (dialogtoken != tap->txa_token) {
1942195377Ssam		IEEE80211_DISCARD_MAC(vap,
1943195377Ssam		    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1944195377Ssam		    ni->ni_macaddr, "ADDBA response",
1945195377Ssam		    "dialogtoken mismatch: waiting for %d, "
1946195377Ssam		    "received %d, tid %d code %d",
1947195377Ssam		    tap->txa_token, dialogtoken, tid, code);
1948195377Ssam		vap->iv_stats.is_addba_badtoken++;
1949195377Ssam		return 0;
1950195377Ssam	}
1951195377Ssam	/* NB: assumes IEEE80211_AGGR_IMMEDIATE is 1 */
1952195377Ssam	if (policy != (tap->txa_flags & IEEE80211_AGGR_IMMEDIATE)) {
1953195377Ssam		IEEE80211_DISCARD_MAC(vap,
1954195377Ssam		    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1955195377Ssam		    ni->ni_macaddr, "ADDBA response",
1956195377Ssam		    "policy mismatch: expecting %s, "
1957195377Ssam		    "received %s, tid %d code %d",
1958195377Ssam		    tap->txa_flags & IEEE80211_AGGR_IMMEDIATE,
1959195377Ssam		    policy, tid, code);
1960195377Ssam		vap->iv_stats.is_addba_badpolicy++;
1961195377Ssam		return 0;
1962195377Ssam	}
1963182829Ssam#if 0
1964195377Ssam	/* XXX we take MIN in ieee80211_addba_response */
1965195377Ssam	if (bufsiz > IEEE80211_AGGR_BAWMAX) {
1966195377Ssam		IEEE80211_DISCARD_MAC(vap,
1967195377Ssam		    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1968195377Ssam		    ni->ni_macaddr, "ADDBA response",
1969195377Ssam		    "BA window too large: max %d, "
1970195377Ssam		    "received %d, tid %d code %d",
1971195377Ssam		    bufsiz, IEEE80211_AGGR_BAWMAX, tid, code);
1972195377Ssam		vap->iv_stats.is_addba_badbawinsize++;
1973195377Ssam		return 0;
1974195377Ssam	}
1975182829Ssam#endif
1976195377Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1977195377Ssam	    "recv ADDBA response: dialogtoken %u code %d "
1978195377Ssam	    "baparamset 0x%x (tid %d bufsiz %d) batimeout %d",
1979195377Ssam	    dialogtoken, code, baparamset, tid, bufsiz,
1980195377Ssam	    batimeout);
1981195377Ssam	ic->ic_addba_response(ni, tap, code, baparamset, batimeout);
1982195377Ssam	return 0;
1983195377Ssam}
1984170530Ssam
1985195377Ssamstatic int
1986195377Ssamht_recv_action_ba_delba(struct ieee80211_node *ni,
1987195377Ssam	const struct ieee80211_frame *wh,
1988195377Ssam	const uint8_t *frm, const uint8_t *efrm)
1989195377Ssam{
1990195377Ssam	struct ieee80211com *ic = ni->ni_ic;
1991195377Ssam	struct ieee80211_rx_ampdu *rap;
1992195377Ssam	struct ieee80211_tx_ampdu *tap;
1993195377Ssam	uint16_t baparamset, code;
1994234324Sadrian	int tid;
1995170530Ssam
1996195377Ssam	baparamset = LE_READ_2(frm+2);
1997195377Ssam	code = LE_READ_2(frm+4);
1998170530Ssam
1999195377Ssam	tid = MS(baparamset, IEEE80211_DELBAPS_TID);
2000170530Ssam
2001195377Ssam	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2002195377Ssam	    "recv DELBA: baparamset 0x%x (tid %d initiator %d) "
2003195377Ssam	    "code %d", baparamset, tid,
2004195377Ssam	    MS(baparamset, IEEE80211_DELBAPS_INIT), code);
2005195377Ssam
2006195377Ssam	if ((baparamset & IEEE80211_DELBAPS_INIT) == 0) {
2007234324Sadrian		tap = &ni->ni_tx_ampdu[tid];
2008195377Ssam		ic->ic_addba_stop(ni, tap);
2009195377Ssam	} else {
2010195377Ssam		rap = &ni->ni_rx_ampdu[tid];
2011195377Ssam		ic->ic_ampdu_rx_stop(ni, rap);
2012170530Ssam	}
2013195377Ssam	return 0;
2014170530Ssam}
2015170530Ssam
2016195377Ssamstatic int
2017195377Ssamht_recv_action_ht_txchwidth(struct ieee80211_node *ni,
2018195377Ssam	const struct ieee80211_frame *wh,
2019170530Ssam	const uint8_t *frm, const uint8_t *efrm)
2020170530Ssam{
2021170530Ssam	int chw;
2022170530Ssam
2023195377Ssam	chw = (frm[2] == IEEE80211_A_HT_TXCHWIDTH_2040) ? 40 : 20;
2024195377Ssam
2025195377Ssam	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2026195377Ssam	    "%s: HT txchwidth, width %d%s",
2027195377Ssam	    __func__, chw, ni->ni_chw != chw ? "*" : "");
2028195377Ssam	if (chw != ni->ni_chw) {
2029195377Ssam		ni->ni_chw = chw;
2030195377Ssam		/* XXX notify on change */
2031170530Ssam	}
2032195377Ssam	return 0;
2033170530Ssam}
2034170530Ssam
2035195377Ssamstatic int
2036195377Ssamht_recv_action_ht_mimopwrsave(struct ieee80211_node *ni,
2037195377Ssam	const struct ieee80211_frame *wh,
2038195377Ssam	const uint8_t *frm, const uint8_t *efrm)
2039195377Ssam{
2040195377Ssam	const struct ieee80211_action_ht_mimopowersave *mps =
2041195377Ssam	    (const struct ieee80211_action_ht_mimopowersave *) frm;
2042195377Ssam
2043195377Ssam	/* XXX check iv_htcaps */
2044195377Ssam	if (mps->am_control & IEEE80211_A_HT_MIMOPWRSAVE_ENA)
2045195377Ssam		ni->ni_flags |= IEEE80211_NODE_MIMO_PS;
2046195377Ssam	else
2047195377Ssam		ni->ni_flags &= ~IEEE80211_NODE_MIMO_PS;
2048195377Ssam	if (mps->am_control & IEEE80211_A_HT_MIMOPWRSAVE_MODE)
2049195377Ssam		ni->ni_flags |= IEEE80211_NODE_MIMO_RTS;
2050195377Ssam	else
2051195377Ssam		ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS;
2052195377Ssam	/* XXX notify on change */
2053195377Ssam	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2054195377Ssam	    "%s: HT MIMO PS (%s%s)", __func__,
2055195377Ssam	    (ni->ni_flags & IEEE80211_NODE_MIMO_PS) ?  "on" : "off",
2056195377Ssam	    (ni->ni_flags & IEEE80211_NODE_MIMO_RTS) ?  "+rts" : ""
2057195377Ssam	);
2058195377Ssam	return 0;
2059195377Ssam}
2060195377Ssam
2061170530Ssam/*
2062170530Ssam * Transmit processing.
2063170530Ssam */
2064170530Ssam
2065170530Ssam/*
2066178354Ssam * Check if A-MPDU should be requested/enabled for a stream.
2067178354Ssam * We require a traffic rate above a per-AC threshold and we
2068178354Ssam * also handle backoff from previous failed attempts.
2069178354Ssam *
2070178354Ssam * Drivers may override this method to bring in information
2071178354Ssam * such as link state conditions in making the decision.
2072178354Ssam */
2073178354Ssamstatic int
2074178354Ssamieee80211_ampdu_enable(struct ieee80211_node *ni,
2075178354Ssam	struct ieee80211_tx_ampdu *tap)
2076178354Ssam{
2077178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2078178354Ssam
2079234324Sadrian	if (tap->txa_avgpps <
2080234324Sadrian	    vap->iv_ampdu_mintraffic[TID_TO_WME_AC(tap->txa_tid)])
2081178354Ssam		return 0;
2082178354Ssam	/* XXX check rssi? */
2083178354Ssam	if (tap->txa_attempts >= ieee80211_addba_maxtries &&
2084178354Ssam	    ticks < tap->txa_nextrequest) {
2085178354Ssam		/*
2086178354Ssam		 * Don't retry too often; txa_nextrequest is set
2087178354Ssam		 * to the minimum interval we'll retry after
2088178354Ssam		 * ieee80211_addba_maxtries failed attempts are made.
2089178354Ssam		 */
2090178354Ssam		return 0;
2091178354Ssam	}
2092178354Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
2093234324Sadrian	    "enable AMPDU on tid %d (%s), avgpps %d pkts %d",
2094234324Sadrian	    tap->txa_tid, ieee80211_wme_acnames[TID_TO_WME_AC(tap->txa_tid)],
2095234324Sadrian	    tap->txa_avgpps, tap->txa_pkts);
2096178354Ssam	return 1;
2097178354Ssam}
2098178354Ssam
2099178354Ssam/*
2100170530Ssam * Request A-MPDU tx aggregation.  Setup local state and
2101170530Ssam * issue an ADDBA request.  BA use will only happen after
2102170530Ssam * the other end replies with ADDBA response.
2103170530Ssam */
2104170530Ssamint
2105170530Ssamieee80211_ampdu_request(struct ieee80211_node *ni,
2106170530Ssam	struct ieee80211_tx_ampdu *tap)
2107170530Ssam{
2108170530Ssam	struct ieee80211com *ic = ni->ni_ic;
2109205277Srpaulo	uint16_t args[5];
2110170530Ssam	int tid, dialogtoken;
2111170530Ssam	static int tokens = 0;	/* XXX */
2112170530Ssam
2113170530Ssam	/* XXX locking */
2114170530Ssam	if ((tap->txa_flags & IEEE80211_AGGR_SETUP) == 0) {
2115170530Ssam		/* do deferred setup of state */
2116184280Ssam		ampdu_tx_setup(tap);
2117170530Ssam	}
2118173273Ssam	/* XXX hack for not doing proper locking */
2119173273Ssam	tap->txa_flags &= ~IEEE80211_AGGR_NAK;
2120173273Ssam
2121170530Ssam	dialogtoken = (tokens+1) % 63;		/* XXX */
2122234324Sadrian	tid = tap->txa_tid;
2123183245Ssam	tap->txa_start = ni->ni_txseqs[tid];
2124170530Ssam
2125170530Ssam	args[0] = dialogtoken;
2126205277Srpaulo	args[1] = 0;	/* NB: status code not used */
2127205277Srpaulo	args[2]	= IEEE80211_BAPS_POLICY_IMMEDIATE
2128170530Ssam		| SM(tid, IEEE80211_BAPS_TID)
2129170530Ssam		| SM(IEEE80211_AGGR_BAWMAX, IEEE80211_BAPS_BUFSIZ)
2130170530Ssam		;
2131205277Srpaulo	args[3] = 0;	/* batimeout */
2132170530Ssam	/* NB: do first so there's no race against reply */
2133205277Srpaulo	if (!ic->ic_addba_request(ni, tap, dialogtoken, args[2], args[3])) {
2134170530Ssam		/* unable to setup state, don't make request */
2135178354Ssam		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2136234324Sadrian		    ni, "%s: could not setup BA stream for TID %d AC %d",
2137234324Sadrian		    __func__, tap->txa_tid, TID_TO_WME_AC(tap->txa_tid));
2138173273Ssam		/* defer next try so we don't slam the driver with requests */
2139178354Ssam		tap->txa_attempts = ieee80211_addba_maxtries;
2140178354Ssam		/* NB: check in case driver wants to override */
2141178354Ssam		if (tap->txa_nextrequest <= ticks)
2142178354Ssam			tap->txa_nextrequest = ticks + ieee80211_addba_backoff;
2143170530Ssam		return 0;
2144170530Ssam	}
2145170530Ssam	tokens = dialogtoken;			/* allocate token */
2146183245Ssam	/* NB: after calling ic_addba_request so driver can set txa_start */
2147205277Srpaulo	args[4] = SM(tap->txa_start, IEEE80211_BASEQ_START)
2148178953Ssam		| SM(0, IEEE80211_BASEQ_FRAG)
2149178953Ssam		;
2150170530Ssam	return ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
2151170530Ssam		IEEE80211_ACTION_BA_ADDBA_REQUEST, args);
2152170530Ssam}
2153170530Ssam
2154170530Ssam/*
2155173273Ssam * Terminate an AMPDU tx stream.  State is reclaimed
2156173273Ssam * and the peer notified with a DelBA Action frame.
2157173273Ssam */
2158173273Ssamvoid
2159183250Ssamieee80211_ampdu_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
2160183250Ssam	int reason)
2161173273Ssam{
2162173273Ssam	struct ieee80211com *ic = ni->ni_ic;
2163178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2164173273Ssam	uint16_t args[4];
2165173273Ssam
2166173273Ssam	/* XXX locking */
2167184280Ssam	tap->txa_flags &= ~IEEE80211_AGGR_BARPEND;
2168173273Ssam	if (IEEE80211_AMPDU_RUNNING(tap)) {
2169178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2170234324Sadrian		    ni, "%s: stop BA stream for TID %d (reason %d)",
2171234324Sadrian		    __func__, tap->txa_tid, reason);
2172178354Ssam		vap->iv_stats.is_ampdu_stop++;
2173173273Ssam
2174173273Ssam		ic->ic_addba_stop(ni, tap);
2175234324Sadrian		args[0] = tap->txa_tid;
2176173273Ssam		args[1] = IEEE80211_DELBAPS_INIT;
2177183250Ssam		args[2] = reason;			/* XXX reason code */
2178195377Ssam		ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
2179173273Ssam			IEEE80211_ACTION_BA_DELBA, args);
2180173273Ssam	} else {
2181178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2182234324Sadrian		    ni, "%s: BA stream for TID %d not running (reason %d)",
2183234324Sadrian		    __func__, tap->txa_tid, reason);
2184178354Ssam		vap->iv_stats.is_ampdu_stop_failed++;
2185173273Ssam	}
2186173273Ssam}
2187173273Ssam
2188235801Sadrian/* XXX */
2189235801Sadrianstatic void bar_start_timer(struct ieee80211_tx_ampdu *tap);
2190235801Sadrian
2191184280Ssamstatic void
2192184280Ssambar_timeout(void *arg)
2193184280Ssam{
2194184280Ssam	struct ieee80211_tx_ampdu *tap = arg;
2195184280Ssam	struct ieee80211_node *ni = tap->txa_ni;
2196184280Ssam
2197184280Ssam	KASSERT((tap->txa_flags & IEEE80211_AGGR_XCHGPEND) == 0,
2198184280Ssam	    ("bar/addba collision, flags 0x%x", tap->txa_flags));
2199184280Ssam
2200184280Ssam	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2201184280Ssam	    ni, "%s: tid %u flags 0x%x attempts %d", __func__,
2202234324Sadrian	    tap->txa_tid, tap->txa_flags, tap->txa_attempts);
2203184280Ssam
2204184280Ssam	/* guard against race with bar_tx_complete */
2205184280Ssam	if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) == 0)
2206184280Ssam		return;
2207184280Ssam	/* XXX ? */
2208234018Sadrian	if (tap->txa_attempts >= ieee80211_bar_maxtries) {
2209235801Sadrian		struct ieee80211com *ic = ni->ni_ic;
2210235801Sadrian
2211234018Sadrian		ni->ni_vap->iv_stats.is_ampdu_bar_tx_fail++;
2212235801Sadrian		/*
2213235801Sadrian		 * If (at least) the last BAR TX timeout was due to
2214235801Sadrian		 * an ieee80211_send_bar() failures, then we need
2215235801Sadrian		 * to make sure we notify the driver that a BAR
2216235801Sadrian		 * TX did occur and fail.  This gives the driver
2217235801Sadrian		 * a chance to undo any queue pause that may
2218235801Sadrian		 * have occured.
2219235801Sadrian		 */
2220235801Sadrian		ic->ic_bar_response(ni, tap, 1);
2221184280Ssam		ieee80211_ampdu_stop(ni, tap, IEEE80211_REASON_TIMEOUT);
2222234018Sadrian	} else {
2223234018Sadrian		ni->ni_vap->iv_stats.is_ampdu_bar_tx_retry++;
2224235801Sadrian		if (ieee80211_send_bar(ni, tap, tap->txa_seqpending) != 0) {
2225242205Sadrian			IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2226242205Sadrian			    ni, "%s: failed to TX, starting timer\n",
2227242205Sadrian			    __func__);
2228235801Sadrian			/*
2229235801Sadrian			 * If ieee80211_send_bar() fails here, the
2230235801Sadrian			 * timer may have stopped and/or the pending
2231235801Sadrian			 * flag may be clear.  Because of this,
2232235801Sadrian			 * fake the BARPEND and reset the timer.
2233235801Sadrian			 * A retransmission attempt will then occur
2234235801Sadrian			 * during the next timeout.
2235235801Sadrian			 */
2236235801Sadrian			/* XXX locking */
2237235801Sadrian			tap->txa_flags |= IEEE80211_AGGR_BARPEND;
2238235801Sadrian			bar_start_timer(tap);
2239235801Sadrian		}
2240234018Sadrian	}
2241184280Ssam}
2242184280Ssam
2243184280Ssamstatic void
2244184280Ssambar_start_timer(struct ieee80211_tx_ampdu *tap)
2245184280Ssam{
2246242205Sadrian	IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2247242205Sadrian	    tap->txa_ni,
2248242205Sadrian	    "%s: called",
2249242205Sadrian	    __func__);
2250184280Ssam	callout_reset(&tap->txa_timer, ieee80211_bar_timeout, bar_timeout, tap);
2251184280Ssam}
2252184280Ssam
2253184280Ssamstatic void
2254184280Ssambar_stop_timer(struct ieee80211_tx_ampdu *tap)
2255184280Ssam{
2256242205Sadrian	IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2257242205Sadrian	    tap->txa_ni,
2258242205Sadrian	    "%s: called",
2259242205Sadrian	    __func__);
2260184280Ssam	callout_stop(&tap->txa_timer);
2261184280Ssam}
2262184280Ssam
2263184280Ssamstatic void
2264184280Ssambar_tx_complete(struct ieee80211_node *ni, void *arg, int status)
2265184280Ssam{
2266184280Ssam	struct ieee80211_tx_ampdu *tap = arg;
2267184280Ssam
2268184280Ssam	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2269184280Ssam	    ni, "%s: tid %u flags 0x%x pending %d status %d",
2270234324Sadrian	    __func__, tap->txa_tid, tap->txa_flags,
2271184280Ssam	    callout_pending(&tap->txa_timer), status);
2272184280Ssam
2273234018Sadrian	ni->ni_vap->iv_stats.is_ampdu_bar_tx++;
2274184280Ssam	/* XXX locking */
2275184280Ssam	if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) &&
2276184280Ssam	    callout_pending(&tap->txa_timer)) {
2277184280Ssam		struct ieee80211com *ic = ni->ni_ic;
2278184280Ssam
2279224907Sadrian		if (status == 0)		/* ACK'd */
2280184280Ssam			bar_stop_timer(tap);
2281184280Ssam		ic->ic_bar_response(ni, tap, status);
2282184280Ssam		/* NB: just let timer expire so we pace requests */
2283184280Ssam	}
2284184280Ssam}
2285184280Ssam
2286184280Ssamstatic void
2287184280Ssamieee80211_bar_response(struct ieee80211_node *ni,
2288184280Ssam	struct ieee80211_tx_ampdu *tap, int status)
2289184280Ssam{
2290184280Ssam
2291242205Sadrian	IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2292242205Sadrian	    tap->txa_ni,
2293242205Sadrian	    "%s: called",
2294242205Sadrian	    __func__);
2295224907Sadrian	if (status == 0) {		/* got ACK */
2296184280Ssam		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2297184280Ssam		    ni, "BAR moves BA win <%u:%u> (%u frames) txseq %u tid %u",
2298184280Ssam		    tap->txa_start,
2299184280Ssam		    IEEE80211_SEQ_ADD(tap->txa_start, tap->txa_wnd-1),
2300184280Ssam		    tap->txa_qframes, tap->txa_seqpending,
2301234324Sadrian		    tap->txa_tid);
2302184280Ssam
2303184280Ssam		/* NB: timer already stopped in bar_tx_complete */
2304184280Ssam		tap->txa_start = tap->txa_seqpending;
2305184280Ssam		tap->txa_flags &= ~IEEE80211_AGGR_BARPEND;
2306184280Ssam	}
2307184280Ssam}
2308184280Ssam
2309173273Ssam/*
2310170530Ssam * Transmit a BAR frame to the specified node.  The
2311170530Ssam * BAR contents are drawn from the supplied aggregation
2312170530Ssam * state associated with the node.
2313184280Ssam *
2314184280Ssam * NB: we only handle immediate ACK w/ compressed bitmap.
2315170530Ssam */
2316170530Ssamint
2317170530Ssamieee80211_send_bar(struct ieee80211_node *ni,
2318184280Ssam	struct ieee80211_tx_ampdu *tap, ieee80211_seq seq)
2319170530Ssam{
2320178354Ssam#define	senderr(_x, _v)	do { vap->iv_stats._v++; ret = _x; goto bad; } while (0)
2321178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2322170530Ssam	struct ieee80211com *ic = ni->ni_ic;
2323184280Ssam	struct ieee80211_frame_bar *bar;
2324170530Ssam	struct mbuf *m;
2325184280Ssam	uint16_t barctl, barseqctl;
2326170530Ssam	uint8_t *frm;
2327170530Ssam	int tid, ret;
2328170530Ssam
2329242205Sadrian
2330242205Sadrian	IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2331242205Sadrian	    tap->txa_ni,
2332242205Sadrian	    "%s: called",
2333242205Sadrian	    __func__);
2334242205Sadrian
2335184280Ssam	if ((tap->txa_flags & IEEE80211_AGGR_RUNNING) == 0) {
2336184280Ssam		/* no ADDBA response, should not happen */
2337184280Ssam		/* XXX stat+msg */
2338184280Ssam		return EINVAL;
2339184280Ssam	}
2340184280Ssam	/* XXX locking */
2341184280Ssam	bar_stop_timer(tap);
2342184280Ssam
2343170530Ssam	ieee80211_ref_node(ni);
2344170530Ssam
2345184280Ssam	m = ieee80211_getmgtframe(&frm, ic->ic_headroom, sizeof(*bar));
2346170530Ssam	if (m == NULL)
2347170530Ssam		senderr(ENOMEM, is_tx_nobuf);
2348170530Ssam
2349184280Ssam	if (!ieee80211_add_callback(m, bar_tx_complete, tap)) {
2350184280Ssam		m_freem(m);
2351184280Ssam		senderr(ENOMEM, is_tx_nobuf);	/* XXX */
2352184280Ssam		/* NOTREACHED */
2353184280Ssam	}
2354184280Ssam
2355184280Ssam	bar = mtod(m, struct ieee80211_frame_bar *);
2356184280Ssam	bar->i_fc[0] = IEEE80211_FC0_VERSION_0 |
2357170530Ssam		IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_BAR;
2358184280Ssam	bar->i_fc[1] = 0;
2359184280Ssam	IEEE80211_ADDR_COPY(bar->i_ra, ni->ni_macaddr);
2360184280Ssam	IEEE80211_ADDR_COPY(bar->i_ta, vap->iv_myaddr);
2361170530Ssam
2362234324Sadrian	tid = tap->txa_tid;
2363170530Ssam	barctl 	= (tap->txa_flags & IEEE80211_AGGR_IMMEDIATE ?
2364184280Ssam			0 : IEEE80211_BAR_NOACK)
2365184280Ssam		| IEEE80211_BAR_COMP
2366184280Ssam		| SM(tid, IEEE80211_BAR_TID)
2367170530Ssam		;
2368184280Ssam	barseqctl = SM(seq, IEEE80211_BAR_SEQ_START);
2369184280Ssam	/* NB: known to have proper alignment */
2370184280Ssam	bar->i_ctl = htole16(barctl);
2371184280Ssam	bar->i_seq = htole16(barseqctl);
2372184280Ssam	m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_bar);
2373170530Ssam
2374178354Ssam	M_WME_SETAC(m, WME_AC_VO);
2375178354Ssam
2376170530Ssam	IEEE80211_NODE_STAT(ni, tx_mgmt);	/* XXX tx_ctl? */
2377170530Ssam
2378184280Ssam	/* XXX locking */
2379184280Ssam	/* init/bump attempts counter */
2380184280Ssam	if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) == 0)
2381184280Ssam		tap->txa_attempts = 1;
2382184280Ssam	else
2383184280Ssam		tap->txa_attempts++;
2384184280Ssam	tap->txa_seqpending = seq;
2385184280Ssam	tap->txa_flags |= IEEE80211_AGGR_BARPEND;
2386170530Ssam
2387184280Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_11N,
2388184280Ssam	    ni, "send BAR: tid %u ctl 0x%x start %u (attempt %d)",
2389184280Ssam	    tid, barctl, seq, tap->txa_attempts);
2390184280Ssam
2391225013Sadrian	/*
2392225013Sadrian	 * ic_raw_xmit will free the node reference
2393225013Sadrian	 * regardless of queue/TX success or failure.
2394225013Sadrian	 */
2395248069Sadrian	IEEE80211_TX_LOCK(ic);
2396248069Sadrian	ret = ieee80211_raw_output(vap, ni, m, NULL);
2397248069Sadrian	IEEE80211_TX_UNLOCK(ic);
2398184280Ssam	if (ret != 0) {
2399242205Sadrian		IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_11N,
2400242205Sadrian		    ni, "send BAR: failed: (ret = %d)\n",
2401242205Sadrian		    ret);
2402184280Ssam		/* xmit failed, clear state flag */
2403184280Ssam		tap->txa_flags &= ~IEEE80211_AGGR_BARPEND;
2404234018Sadrian		vap->iv_stats.is_ampdu_bar_tx_fail++;
2405225013Sadrian		return ret;
2406184280Ssam	}
2407184280Ssam	/* XXX hack against tx complete happening before timer is started */
2408184280Ssam	if (tap->txa_flags & IEEE80211_AGGR_BARPEND)
2409184280Ssam		bar_start_timer(tap);
2410184280Ssam	return 0;
2411170530Ssambad:
2412242205Sadrian	IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2413242205Sadrian	    tap->txa_ni,
2414242205Sadrian	    "%s: bad! ret=%d",
2415242205Sadrian	    __func__, ret);
2416234018Sadrian	vap->iv_stats.is_ampdu_bar_tx_fail++;
2417170530Ssam	ieee80211_free_node(ni);
2418170530Ssam	return ret;
2419170530Ssam#undef senderr
2420170530Ssam}
2421170530Ssam
2422195377Ssamstatic int
2423195377Ssamht_action_output(struct ieee80211_node *ni, struct mbuf *m)
2424195377Ssam{
2425195377Ssam	struct ieee80211_bpf_params params;
2426195377Ssam
2427195377Ssam	memset(&params, 0, sizeof(params));
2428195377Ssam	params.ibp_pri = WME_AC_VO;
2429195377Ssam	params.ibp_rate0 = ni->ni_txparms->mgmtrate;
2430195377Ssam	/* NB: we know all frames are unicast */
2431195377Ssam	params.ibp_try0 = ni->ni_txparms->maxretry;
2432195377Ssam	params.ibp_power = ni->ni_txpower;
2433195377Ssam	return ieee80211_mgmt_output(ni, m, IEEE80211_FC0_SUBTYPE_ACTION,
2434195377Ssam	     &params);
2435195377Ssam}
2436195377Ssam
2437195377Ssam#define	ADDSHORT(frm, v) do {			\
2438195377Ssam	frm[0] = (v) & 0xff;			\
2439195377Ssam	frm[1] = (v) >> 8;			\
2440195377Ssam	frm += 2;				\
2441195377Ssam} while (0)
2442195377Ssam
2443170530Ssam/*
2444170530Ssam * Send an action management frame.  The arguments are stuff
2445170530Ssam * into a frame without inspection; the caller is assumed to
2446170530Ssam * prepare them carefully (e.g. based on the aggregation state).
2447170530Ssam */
2448195377Ssamstatic int
2449195377Ssamht_send_action_ba_addba(struct ieee80211_node *ni,
2450195377Ssam	int category, int action, void *arg0)
2451170530Ssam{
2452178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2453170530Ssam	struct ieee80211com *ic = ni->ni_ic;
2454195377Ssam	uint16_t *args = arg0;
2455170530Ssam	struct mbuf *m;
2456170530Ssam	uint8_t *frm;
2457195377Ssam
2458195377Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2459205277Srpaulo	    "send ADDBA %s: dialogtoken %d status %d "
2460195377Ssam	    "baparamset 0x%x (tid %d) batimeout 0x%x baseqctl 0x%x",
2461195377Ssam	    (action == IEEE80211_ACTION_BA_ADDBA_REQUEST) ?
2462195377Ssam		"request" : "response",
2463205277Srpaulo	    args[0], args[1], args[2], MS(args[2], IEEE80211_BAPS_TID),
2464205277Srpaulo	    args[3], args[4]);
2465195377Ssam
2466195377Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2467195377Ssam	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2468195377Ssam	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2469195377Ssam	ieee80211_ref_node(ni);
2470195377Ssam
2471195377Ssam	m = ieee80211_getmgtframe(&frm,
2472195377Ssam	    ic->ic_headroom + sizeof(struct ieee80211_frame),
2473195377Ssam	    sizeof(uint16_t)	/* action+category */
2474195377Ssam	    /* XXX may action payload */
2475195377Ssam	    + sizeof(struct ieee80211_action_ba_addbaresponse)
2476195377Ssam	);
2477195377Ssam	if (m != NULL) {
2478195377Ssam		*frm++ = category;
2479195377Ssam		*frm++ = action;
2480195377Ssam		*frm++ = args[0];		/* dialog token */
2481205277Srpaulo		if (action == IEEE80211_ACTION_BA_ADDBA_RESPONSE)
2482205277Srpaulo			ADDSHORT(frm, args[1]);	/* status code */
2483205277Srpaulo		ADDSHORT(frm, args[2]);		/* baparamset */
2484205277Srpaulo		ADDSHORT(frm, args[3]);		/* batimeout */
2485195377Ssam		if (action == IEEE80211_ACTION_BA_ADDBA_REQUEST)
2486205277Srpaulo			ADDSHORT(frm, args[4]);	/* baseqctl */
2487195377Ssam		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2488195377Ssam		return ht_action_output(ni, m);
2489195377Ssam	} else {
2490195377Ssam		vap->iv_stats.is_tx_nobuf++;
2491195377Ssam		ieee80211_free_node(ni);
2492195377Ssam		return ENOMEM;
2493195377Ssam	}
2494195377Ssam}
2495195377Ssam
2496195377Ssamstatic int
2497195377Ssamht_send_action_ba_delba(struct ieee80211_node *ni,
2498195377Ssam	int category, int action, void *arg0)
2499195377Ssam{
2500195377Ssam	struct ieee80211vap *vap = ni->ni_vap;
2501195377Ssam	struct ieee80211com *ic = ni->ni_ic;
2502195377Ssam	uint16_t *args = arg0;
2503195377Ssam	struct mbuf *m;
2504170530Ssam	uint16_t baparamset;
2505195377Ssam	uint8_t *frm;
2506170530Ssam
2507195377Ssam	baparamset = SM(args[0], IEEE80211_DELBAPS_TID)
2508195377Ssam		   | args[1]
2509195377Ssam		   ;
2510195377Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2511195377Ssam	    "send DELBA action: tid %d, initiator %d reason %d",
2512195377Ssam	    args[0], args[1], args[2]);
2513170530Ssam
2514178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2515195377Ssam	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2516195377Ssam	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2517170530Ssam	ieee80211_ref_node(ni);
2518170530Ssam
2519170530Ssam	m = ieee80211_getmgtframe(&frm,
2520195377Ssam	    ic->ic_headroom + sizeof(struct ieee80211_frame),
2521195377Ssam	    sizeof(uint16_t)	/* action+category */
2522195377Ssam	    /* XXX may action payload */
2523195377Ssam	    + sizeof(struct ieee80211_action_ba_addbaresponse)
2524170530Ssam	);
2525195377Ssam	if (m != NULL) {
2526195377Ssam		*frm++ = category;
2527195377Ssam		*frm++ = action;
2528195377Ssam		ADDSHORT(frm, baparamset);
2529195377Ssam		ADDSHORT(frm, args[2]);		/* reason code */
2530195377Ssam		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2531195377Ssam		return ht_action_output(ni, m);
2532195377Ssam	} else {
2533195377Ssam		vap->iv_stats.is_tx_nobuf++;
2534195377Ssam		ieee80211_free_node(ni);
2535195377Ssam		return ENOMEM;
2536195377Ssam	}
2537195377Ssam}
2538170530Ssam
2539195377Ssamstatic int
2540195377Ssamht_send_action_ht_txchwidth(struct ieee80211_node *ni,
2541195377Ssam	int category, int action, void *arg0)
2542195377Ssam{
2543195377Ssam	struct ieee80211vap *vap = ni->ni_vap;
2544195377Ssam	struct ieee80211com *ic = ni->ni_ic;
2545195377Ssam	struct mbuf *m;
2546195377Ssam	uint8_t *frm;
2547170530Ssam
2548195377Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2549195377Ssam	    "send HT txchwidth: width %d",
2550195377Ssam	    IEEE80211_IS_CHAN_HT40(ni->ni_chan) ? 40 : 20);
2551170530Ssam
2552195377Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2553195377Ssam	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2554195377Ssam	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2555195377Ssam	ieee80211_ref_node(ni);
2556170530Ssam
2557195377Ssam	m = ieee80211_getmgtframe(&frm,
2558195377Ssam	    ic->ic_headroom + sizeof(struct ieee80211_frame),
2559195377Ssam	    sizeof(uint16_t)	/* action+category */
2560195377Ssam	    /* XXX may action payload */
2561195377Ssam	    + sizeof(struct ieee80211_action_ba_addbaresponse)
2562195377Ssam	);
2563195377Ssam	if (m != NULL) {
2564195377Ssam		*frm++ = category;
2565195377Ssam		*frm++ = action;
2566195377Ssam		*frm++ = IEEE80211_IS_CHAN_HT40(ni->ni_chan) ?
2567195377Ssam			IEEE80211_A_HT_TXCHWIDTH_2040 :
2568195377Ssam			IEEE80211_A_HT_TXCHWIDTH_20;
2569195377Ssam		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2570195377Ssam		return ht_action_output(ni, m);
2571195377Ssam	} else {
2572195377Ssam		vap->iv_stats.is_tx_nobuf++;
2573195377Ssam		ieee80211_free_node(ni);
2574195377Ssam		return ENOMEM;
2575170530Ssam	}
2576195377Ssam}
2577170530Ssam#undef ADDSHORT
2578170530Ssam
2579170530Ssam/*
2580219600Sbschmidt * Construct the MCS bit mask for inclusion in an HT capabilities
2581219600Sbschmidt * information element.
2582170530Ssam */
2583219600Sbschmidtstatic void
2584219600Sbschmidtieee80211_set_mcsset(struct ieee80211com *ic, uint8_t *frm)
2585170530Ssam{
2586170530Ssam	int i;
2587219600Sbschmidt	uint8_t txparams;
2588170530Ssam
2589219600Sbschmidt	KASSERT((ic->ic_rxstream > 0 && ic->ic_rxstream <= 4),
2590219600Sbschmidt	    ("ic_rxstream %d out of range", ic->ic_rxstream));
2591219600Sbschmidt	KASSERT((ic->ic_txstream > 0 && ic->ic_txstream <= 4),
2592219600Sbschmidt	    ("ic_txstream %d out of range", ic->ic_txstream));
2593219600Sbschmidt
2594219600Sbschmidt	for (i = 0; i < ic->ic_rxstream * 8; i++)
2595219600Sbschmidt		setbit(frm, i);
2596219600Sbschmidt	if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
2597219600Sbschmidt	    (ic->ic_htcaps & IEEE80211_HTC_RXMCS32))
2598219600Sbschmidt		setbit(frm, 32);
2599219600Sbschmidt	if (ic->ic_htcaps & IEEE80211_HTC_RXUNEQUAL) {
2600219600Sbschmidt		if (ic->ic_rxstream >= 2) {
2601219600Sbschmidt			for (i = 33; i <= 38; i++)
2602219600Sbschmidt				setbit(frm, i);
2603170530Ssam		}
2604219600Sbschmidt		if (ic->ic_rxstream >= 3) {
2605219600Sbschmidt			for (i = 39; i <= 52; i++)
2606219600Sbschmidt				setbit(frm, i);
2607219600Sbschmidt		}
2608219600Sbschmidt		if (ic->ic_txstream >= 4) {
2609219600Sbschmidt			for (i = 53; i <= 76; i++)
2610219600Sbschmidt				setbit(frm, i);
2611219600Sbschmidt		}
2612170530Ssam	}
2613219600Sbschmidt
2614219600Sbschmidt	if (ic->ic_rxstream != ic->ic_txstream) {
2615219600Sbschmidt		txparams = 0x1;			/* TX MCS set defined */
2616219600Sbschmidt		txparams |= 0x2;		/* TX RX MCS not equal */
2617219600Sbschmidt		txparams |= (ic->ic_txstream - 1) << 2;	/* num TX streams */
2618219600Sbschmidt		if (ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL)
2619219600Sbschmidt			txparams |= 0x16;	/* TX unequal modulation sup */
2620219600Sbschmidt	} else
2621219600Sbschmidt		txparams = 0;
2622219600Sbschmidt	frm[12] = txparams;
2623170530Ssam}
2624170530Ssam
2625170530Ssam/*
2626170530Ssam * Add body of an HTCAP information element.
2627170530Ssam */
2628170530Ssamstatic uint8_t *
2629170530Ssamieee80211_add_htcap_body(uint8_t *frm, struct ieee80211_node *ni)
2630170530Ssam{
2631170530Ssam#define	ADDSHORT(frm, v) do {			\
2632170530Ssam	frm[0] = (v) & 0xff;			\
2633170530Ssam	frm[1] = (v) >> 8;			\
2634170530Ssam	frm += 2;				\
2635170530Ssam} while (0)
2636222683Sbschmidt	struct ieee80211com *ic = ni->ni_ic;
2637178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2638205513Srpaulo	uint16_t caps, extcaps;
2639173865Ssam	int rxmax, density;
2640170530Ssam
2641170530Ssam	/* HT capabilities */
2642178354Ssam	caps = vap->iv_htcaps & 0xffff;
2643173273Ssam	/*
2644173273Ssam	 * Note channel width depends on whether we are operating as
2645173273Ssam	 * a sta or not.  When operating as a sta we are generating
2646173273Ssam	 * a request based on our desired configuration.  Otherwise
2647173273Ssam	 * we are operational and the channel attributes identify
2648173273Ssam	 * how we've been setup (which might be different if a fixed
2649173273Ssam	 * channel is specified).
2650173273Ssam	 */
2651178354Ssam	if (vap->iv_opmode == IEEE80211_M_STA) {
2652173273Ssam		/* override 20/40 use based on config */
2653193655Ssam		if (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)
2654173273Ssam			caps |= IEEE80211_HTCAP_CHWIDTH40;
2655173273Ssam		else
2656173273Ssam			caps &= ~IEEE80211_HTCAP_CHWIDTH40;
2657173865Ssam		/* use advertised setting (XXX locally constraint) */
2658173865Ssam		rxmax = MS(ni->ni_htparam, IEEE80211_HTCAP_MAXRXAMPDU);
2659173865Ssam		density = MS(ni->ni_htparam, IEEE80211_HTCAP_MPDUDENSITY);
2660222683Sbschmidt
2661222683Sbschmidt		/*
2662222683Sbschmidt		 * NB: Hardware might support HT40 on some but not all
2663222683Sbschmidt		 * channels. We can't determine this earlier because only
2664222683Sbschmidt		 * after association the channel is upgraded to HT based
2665222683Sbschmidt		 * on the negotiated capabilities.
2666222683Sbschmidt		 */
2667222683Sbschmidt		if (ni->ni_chan != IEEE80211_CHAN_ANYC &&
2668222683Sbschmidt		    findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT40U) == NULL &&
2669222683Sbschmidt		    findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT40D) == NULL)
2670222683Sbschmidt			caps &= ~IEEE80211_HTCAP_CHWIDTH40;
2671173273Ssam	} else {
2672173273Ssam		/* override 20/40 use based on current channel */
2673178354Ssam		if (IEEE80211_IS_CHAN_HT40(ni->ni_chan))
2674173273Ssam			caps |= IEEE80211_HTCAP_CHWIDTH40;
2675173273Ssam		else
2676173273Ssam			caps &= ~IEEE80211_HTCAP_CHWIDTH40;
2677178354Ssam		rxmax = vap->iv_ampdu_rxmax;
2678178354Ssam		density = vap->iv_ampdu_density;
2679173273Ssam	}
2680170530Ssam	/* adjust short GI based on channel and config */
2681193655Ssam	if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20) == 0)
2682170530Ssam		caps &= ~IEEE80211_HTCAP_SHORTGI20;
2683193655Ssam	if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40) == 0 ||
2684170530Ssam	    (caps & IEEE80211_HTCAP_CHWIDTH40) == 0)
2685170530Ssam		caps &= ~IEEE80211_HTCAP_SHORTGI40;
2686170530Ssam	ADDSHORT(frm, caps);
2687170530Ssam
2688170530Ssam	/* HT parameters */
2689173865Ssam	*frm = SM(rxmax, IEEE80211_HTCAP_MAXRXAMPDU)
2690173865Ssam	     | SM(density, IEEE80211_HTCAP_MPDUDENSITY)
2691173273Ssam	     ;
2692170530Ssam	frm++;
2693170530Ssam
2694170530Ssam	/* pre-zero remainder of ie */
2695170530Ssam	memset(frm, 0, sizeof(struct ieee80211_ie_htcap) -
2696170530Ssam		__offsetof(struct ieee80211_ie_htcap, hc_mcsset));
2697170530Ssam
2698170530Ssam	/* supported MCS set */
2699173273Ssam	/*
2700219600Sbschmidt	 * XXX: For sta mode the rate set should be restricted based
2701219600Sbschmidt	 * on the AP's capabilities, but ni_htrates isn't setup when
2702219600Sbschmidt	 * we're called to form an AssocReq frame so for now we're
2703219600Sbschmidt	 * restricted to the device capabilities.
2704173273Ssam	 */
2705219600Sbschmidt	ieee80211_set_mcsset(ni->ni_ic, frm);
2706170530Ssam
2707205513Srpaulo	frm += __offsetof(struct ieee80211_ie_htcap, hc_extcap) -
2708205513Srpaulo		__offsetof(struct ieee80211_ie_htcap, hc_mcsset);
2709205513Srpaulo
2710205513Srpaulo	/* HT extended capabilities */
2711205513Srpaulo	extcaps = vap->iv_htextcaps & 0xffff;
2712205513Srpaulo
2713205513Srpaulo	ADDSHORT(frm, extcaps);
2714205513Srpaulo
2715170530Ssam	frm += sizeof(struct ieee80211_ie_htcap) -
2716205513Srpaulo		__offsetof(struct ieee80211_ie_htcap, hc_txbf);
2717205513Srpaulo
2718170530Ssam	return frm;
2719170530Ssam#undef ADDSHORT
2720170530Ssam}
2721170530Ssam
2722170530Ssam/*
2723170530Ssam * Add 802.11n HT capabilities information element
2724170530Ssam */
2725170530Ssamuint8_t *
2726170530Ssamieee80211_add_htcap(uint8_t *frm, struct ieee80211_node *ni)
2727170530Ssam{
2728170530Ssam	frm[0] = IEEE80211_ELEMID_HTCAP;
2729170530Ssam	frm[1] = sizeof(struct ieee80211_ie_htcap) - 2;
2730170530Ssam	return ieee80211_add_htcap_body(frm + 2, ni);
2731170530Ssam}
2732170530Ssam
2733170530Ssam/*
2734170530Ssam * Add Broadcom OUI wrapped standard HTCAP ie; this is
2735170530Ssam * used for compatibility w/ pre-draft implementations.
2736170530Ssam */
2737170530Ssamuint8_t *
2738170530Ssamieee80211_add_htcap_vendor(uint8_t *frm, struct ieee80211_node *ni)
2739170530Ssam{
2740170530Ssam	frm[0] = IEEE80211_ELEMID_VENDOR;
2741170530Ssam	frm[1] = 4 + sizeof(struct ieee80211_ie_htcap) - 2;
2742170530Ssam	frm[2] = (BCM_OUI >> 0) & 0xff;
2743170530Ssam	frm[3] = (BCM_OUI >> 8) & 0xff;
2744170530Ssam	frm[4] = (BCM_OUI >> 16) & 0xff;
2745170530Ssam	frm[5] = BCM_OUI_HTCAP;
2746170530Ssam	return ieee80211_add_htcap_body(frm + 6, ni);
2747170530Ssam}
2748170530Ssam
2749170530Ssam/*
2750170530Ssam * Construct the MCS bit mask of basic rates
2751170530Ssam * for inclusion in an HT information element.
2752170530Ssam */
2753170530Ssamstatic void
2754170530Ssamieee80211_set_basic_htrates(uint8_t *frm, const struct ieee80211_htrateset *rs)
2755170530Ssam{
2756170530Ssam	int i;
2757170530Ssam
2758170530Ssam	for (i = 0; i < rs->rs_nrates; i++) {
2759170530Ssam		int r = rs->rs_rates[i] & IEEE80211_RATE_VAL;
2760170530Ssam		if ((rs->rs_rates[i] & IEEE80211_RATE_BASIC) &&
2761170530Ssam		    r < IEEE80211_HTRATE_MAXSIZE) {
2762170530Ssam			/* NB: this assumes a particular implementation */
2763170530Ssam			setbit(frm, r);
2764170530Ssam		}
2765170530Ssam	}
2766170530Ssam}
2767170530Ssam
2768170530Ssam/*
2769172211Ssam * Update the HTINFO ie for a beacon frame.
2770172211Ssam */
2771172211Ssamvoid
2772178354Ssamieee80211_ht_update_beacon(struct ieee80211vap *vap,
2773172211Ssam	struct ieee80211_beacon_offsets *bo)
2774172211Ssam{
2775172211Ssam#define	PROTMODE	(IEEE80211_HTINFO_OPMODE|IEEE80211_HTINFO_NONHT_PRESENT)
2776250442Sadrian	struct ieee80211_node *ni;
2777250442Sadrian	const struct ieee80211_channel *bsschan;
2778178354Ssam	struct ieee80211com *ic = vap->iv_ic;
2779172211Ssam	struct ieee80211_ie_htinfo *ht =
2780172211Ssam	   (struct ieee80211_ie_htinfo *) bo->bo_htinfo;
2781172211Ssam
2782250442Sadrian	ni = ieee80211_ref_node(vap->iv_bss);
2783250442Sadrian	bsschan = ni->ni_chan;
2784250442Sadrian
2785172211Ssam	/* XXX only update on channel change */
2786178354Ssam	ht->hi_ctrlchannel = ieee80211_chan2ieee(ic, bsschan);
2787193655Ssam	if (vap->iv_flags_ht & IEEE80211_FHT_RIFS)
2788183256Ssam		ht->hi_byte1 = IEEE80211_HTINFO_RIFSMODE_PERM;
2789183256Ssam	else
2790183256Ssam		ht->hi_byte1 = IEEE80211_HTINFO_RIFSMODE_PROH;
2791178354Ssam	if (IEEE80211_IS_CHAN_HT40U(bsschan))
2792172211Ssam		ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_ABOVE;
2793178354Ssam	else if (IEEE80211_IS_CHAN_HT40D(bsschan))
2794172211Ssam		ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_BELOW;
2795172211Ssam	else
2796172211Ssam		ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_NONE;
2797178354Ssam	if (IEEE80211_IS_CHAN_HT40(bsschan))
2798172211Ssam		ht->hi_byte1 |= IEEE80211_HTINFO_TXWIDTH_2040;
2799172211Ssam
2800172211Ssam	/* protection mode */
2801172211Ssam	ht->hi_byte2 = (ht->hi_byte2 &~ PROTMODE) | ic->ic_curhtprotmode;
2802172211Ssam
2803250442Sadrian	ieee80211_free_node(ni);
2804250442Sadrian
2805172211Ssam	/* XXX propagate to vendor ie's */
2806172211Ssam#undef PROTMODE
2807172211Ssam}
2808172211Ssam
2809172211Ssam/*
2810170530Ssam * Add body of an HTINFO information element.
2811173273Ssam *
2812173273Ssam * NB: We don't use struct ieee80211_ie_htinfo because we can
2813173273Ssam * be called to fillin both a standard ie and a compat ie that
2814173273Ssam * has a vendor OUI at the front.
2815170530Ssam */
2816170530Ssamstatic uint8_t *
2817170530Ssamieee80211_add_htinfo_body(uint8_t *frm, struct ieee80211_node *ni)
2818170530Ssam{
2819183256Ssam	struct ieee80211vap *vap = ni->ni_vap;
2820170530Ssam	struct ieee80211com *ic = ni->ni_ic;
2821170530Ssam
2822170530Ssam	/* pre-zero remainder of ie */
2823170530Ssam	memset(frm, 0, sizeof(struct ieee80211_ie_htinfo) - 2);
2824170530Ssam
2825170530Ssam	/* primary/control channel center */
2826178354Ssam	*frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
2827170530Ssam
2828193655Ssam	if (vap->iv_flags_ht & IEEE80211_FHT_RIFS)
2829183256Ssam		frm[0] = IEEE80211_HTINFO_RIFSMODE_PERM;
2830183256Ssam	else
2831183256Ssam		frm[0] = IEEE80211_HTINFO_RIFSMODE_PROH;
2832178354Ssam	if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan))
2833170530Ssam		frm[0] |= IEEE80211_HTINFO_2NDCHAN_ABOVE;
2834178354Ssam	else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan))
2835170530Ssam		frm[0] |= IEEE80211_HTINFO_2NDCHAN_BELOW;
2836170530Ssam	else
2837170530Ssam		frm[0] |= IEEE80211_HTINFO_2NDCHAN_NONE;
2838178354Ssam	if (IEEE80211_IS_CHAN_HT40(ni->ni_chan))
2839170530Ssam		frm[0] |= IEEE80211_HTINFO_TXWIDTH_2040;
2840170530Ssam
2841172211Ssam	frm[1] = ic->ic_curhtprotmode;
2842170530Ssam
2843170530Ssam	frm += 5;
2844170530Ssam
2845170530Ssam	/* basic MCS set */
2846170530Ssam	ieee80211_set_basic_htrates(frm, &ni->ni_htrates);
2847170530Ssam	frm += sizeof(struct ieee80211_ie_htinfo) -
2848170530Ssam		__offsetof(struct ieee80211_ie_htinfo, hi_basicmcsset);
2849170530Ssam	return frm;
2850170530Ssam}
2851170530Ssam
2852170530Ssam/*
2853170530Ssam * Add 802.11n HT information information element.
2854170530Ssam */
2855170530Ssamuint8_t *
2856170530Ssamieee80211_add_htinfo(uint8_t *frm, struct ieee80211_node *ni)
2857170530Ssam{
2858170530Ssam	frm[0] = IEEE80211_ELEMID_HTINFO;
2859170530Ssam	frm[1] = sizeof(struct ieee80211_ie_htinfo) - 2;
2860170530Ssam	return ieee80211_add_htinfo_body(frm + 2, ni);
2861170530Ssam}
2862170530Ssam
2863170530Ssam/*
2864170530Ssam * Add Broadcom OUI wrapped standard HTINFO ie; this is
2865170530Ssam * used for compatibility w/ pre-draft implementations.
2866170530Ssam */
2867170530Ssamuint8_t *
2868170530Ssamieee80211_add_htinfo_vendor(uint8_t *frm, struct ieee80211_node *ni)
2869170530Ssam{
2870170530Ssam	frm[0] = IEEE80211_ELEMID_VENDOR;
2871170530Ssam	frm[1] = 4 + sizeof(struct ieee80211_ie_htinfo) - 2;
2872170530Ssam	frm[2] = (BCM_OUI >> 0) & 0xff;
2873170530Ssam	frm[3] = (BCM_OUI >> 8) & 0xff;
2874170530Ssam	frm[4] = (BCM_OUI >> 16) & 0xff;
2875170530Ssam	frm[5] = BCM_OUI_HTINFO;
2876170530Ssam	return ieee80211_add_htinfo_body(frm + 6, ni);
2877170530Ssam}
2878