ieee80211_ht.c revision 230619
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: stable/9/sys/net80211/ieee80211_ht.c 230619 2012-01-27 17:32:50Z bschmidt $");
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
310219598Sbschmidtht_getrate(struct ieee80211com *ic, int index, int mode, int ratetype)
311219598Sbschmidt{
312219598Sbschmidt	int mword, rate;
313219598Sbschmidt
314219598Sbschmidt	mword = ieee80211_rate2media(ic, index | IEEE80211_RATE_MCS, mode);
315219598Sbschmidt	if (IFM_SUBTYPE(mword) != IFM_IEEE80211_MCS)
316219598Sbschmidt		return (0);
317219598Sbschmidt	switch (ratetype) {
318219598Sbschmidt	case 0:
319219598Sbschmidt		rate = ieee80211_htrates[index].ht20_rate_800ns;
320219598Sbschmidt		break;
321219598Sbschmidt	case 1:
322219598Sbschmidt		rate = ieee80211_htrates[index].ht20_rate_400ns;
323219598Sbschmidt		break;
324219598Sbschmidt	case 2:
325219598Sbschmidt		rate = ieee80211_htrates[index].ht40_rate_800ns;
326219598Sbschmidt		break;
327219598Sbschmidt	default:
328219598Sbschmidt		rate = ieee80211_htrates[index].ht40_rate_400ns;
329219598Sbschmidt		break;
330219598Sbschmidt	}
331219598Sbschmidt	return (rate);
332219598Sbschmidt}
333219598Sbschmidt
334219598Sbschmidtstatic struct printranges {
335219598Sbschmidt	int	minmcs;
336219598Sbschmidt	int	maxmcs;
337219598Sbschmidt	int	txstream;
338219598Sbschmidt	int	ratetype;
339219598Sbschmidt	int	htcapflags;
340219598Sbschmidt} ranges[] = {
341219598Sbschmidt	{  0,  7, 1, 0, 0 },
342219598Sbschmidt	{  8, 15, 2, 0, 0 },
343219598Sbschmidt	{ 16, 23, 3, 0, 0 },
344219598Sbschmidt	{ 24, 31, 4, 0, 0 },
345219598Sbschmidt	{ 32,  0, 1, 2, IEEE80211_HTC_TXMCS32 },
346219598Sbschmidt	{ 33, 38, 2, 0, IEEE80211_HTC_TXUNEQUAL },
347219598Sbschmidt	{ 39, 52, 3, 0, IEEE80211_HTC_TXUNEQUAL },
348219598Sbschmidt	{ 53, 76, 4, 0, IEEE80211_HTC_TXUNEQUAL },
349219598Sbschmidt	{  0,  0, 0, 0, 0 },
350219598Sbschmidt};
351219598Sbschmidt
352170530Ssamstatic void
353219598Sbschmidtht_rateprint(struct ieee80211com *ic, int mode, int ratetype)
354170530Ssam{
355219598Sbschmidt	struct ifnet *ifp = ic->ic_ifp;
356219598Sbschmidt	int minrate, maxrate;
357219598Sbschmidt	struct printranges *range;
358170530Ssam
359219598Sbschmidt	for (range = ranges; range->txstream != 0; range++) {
360219598Sbschmidt		if (ic->ic_txstream < range->txstream)
361170530Ssam			continue;
362219598Sbschmidt		if (range->htcapflags &&
363219598Sbschmidt		    (ic->ic_htcaps & range->htcapflags) == 0)
364219598Sbschmidt			continue;
365219598Sbschmidt		if (ratetype < range->ratetype)
366219598Sbschmidt			continue;
367219598Sbschmidt		minrate = ht_getrate(ic, range->minmcs, mode, ratetype);
368219598Sbschmidt		maxrate = ht_getrate(ic, range->maxmcs, mode, ratetype);
369219598Sbschmidt		if (range->maxmcs) {
370219598Sbschmidt			if_printf(ifp, "MCS %d-%d: %d%sMbps - %d%sMbps\n",
371219598Sbschmidt			    range->minmcs, range->maxmcs,
372219598Sbschmidt			    minrate/2, ((minrate & 0x1) != 0 ? ".5" : ""),
373219598Sbschmidt			    maxrate/2, ((maxrate & 0x1) != 0 ? ".5" : ""));
374219598Sbschmidt		} else {
375219598Sbschmidt			if_printf(ifp, "MCS %d: %d%sMbps\n", range->minmcs,
376219598Sbschmidt			    minrate/2, ((minrate & 0x1) != 0 ? ".5" : ""));
377205277Srpaulo		}
378170530Ssam	}
379170530Ssam}
380170530Ssam
381205277Srpaulostatic void
382219598Sbschmidtht_announce(struct ieee80211com *ic, int mode)
383205277Srpaulo{
384205277Srpaulo	struct ifnet *ifp = ic->ic_ifp;
385205277Srpaulo	const char *modestr = ieee80211_phymode_name[mode];
386219598Sbschmidt
387219606Sbschmidt	if_printf(ifp, "%s MCS 20MHz\n", modestr);
388219598Sbschmidt	ht_rateprint(ic, mode, 0);
389219598Sbschmidt	if (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20) {
390219606Sbschmidt		if_printf(ifp, "%s MCS 20MHz SGI\n", modestr);
391219598Sbschmidt		ht_rateprint(ic, mode, 1);
392219598Sbschmidt	}
393219598Sbschmidt	if (ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
394219606Sbschmidt		if_printf(ifp, "%s MCS 40MHz:\n", modestr);
395219598Sbschmidt		ht_rateprint(ic, mode, 2);
396219598Sbschmidt	}
397219598Sbschmidt	if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
398219598Sbschmidt	    (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40)) {
399219606Sbschmidt		if_printf(ifp, "%s MCS 40MHz SGI:\n", modestr);
400219598Sbschmidt		ht_rateprint(ic, mode, 3);
401219598Sbschmidt	}
402205277Srpaulo}
403205277Srpaulo
404170530Ssamvoid
405170530Ssamieee80211_ht_announce(struct ieee80211com *ic)
406170530Ssam{
407219598Sbschmidt	struct ifnet *ifp = ic->ic_ifp;
408219598Sbschmidt
409219598Sbschmidt	if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) ||
410219598Sbschmidt	    isset(ic->ic_modecaps, IEEE80211_MODE_11NG))
411219598Sbschmidt		if_printf(ifp, "%dT%dR\n", ic->ic_txstream, ic->ic_rxstream);
412170530Ssam	if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA))
413219598Sbschmidt		ht_announce(ic, IEEE80211_MODE_11NA);
414170530Ssam	if (isset(ic->ic_modecaps, IEEE80211_MODE_11NG))
415219598Sbschmidt		ht_announce(ic, IEEE80211_MODE_11NG);
416170530Ssam}
417170530Ssam
418219602Sbschmidtstatic struct ieee80211_htrateset htrateset;
419219602Sbschmidt
420170530Ssamconst struct ieee80211_htrateset *
421170530Ssamieee80211_get_suphtrates(struct ieee80211com *ic,
422219602Sbschmidt    const struct ieee80211_channel *c)
423170530Ssam{
424219602Sbschmidt#define	ADDRATE(x)	do {						\
425219602Sbschmidt	htrateset.rs_rates[htrateset.rs_nrates] = x;			\
426219602Sbschmidt	htrateset.rs_nrates++;						\
427219602Sbschmidt} while (0)
428219602Sbschmidt	int i;
429219602Sbschmidt
430219602Sbschmidt	memset(&htrateset, 0, sizeof(struct ieee80211_htrateset));
431219602Sbschmidt	for (i = 0; i < ic->ic_txstream * 8; i++)
432219602Sbschmidt		ADDRATE(i);
433219602Sbschmidt	if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
434219602Sbschmidt	    (ic->ic_htcaps & IEEE80211_HTC_TXMCS32))
435230619Sbschmidt		ADDRATE(32);
436219602Sbschmidt	if (ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL) {
437219602Sbschmidt		if (ic->ic_txstream >= 2) {
438219602Sbschmidt			 for (i = 33; i <= 38; i++)
439219602Sbschmidt				ADDRATE(i);
440219602Sbschmidt		}
441219602Sbschmidt		if (ic->ic_txstream >= 3) {
442219602Sbschmidt			for (i = 39; i <= 52; i++)
443219602Sbschmidt				ADDRATE(i);
444219602Sbschmidt		}
445219602Sbschmidt		if (ic->ic_txstream == 4) {
446219602Sbschmidt			for (i = 53; i <= 76; i++)
447219602Sbschmidt				ADDRATE(i);
448219602Sbschmidt		}
449219602Sbschmidt	}
450219602Sbschmidt	return &htrateset;
451219602Sbschmidt#undef	ADDRATE
452170530Ssam}
453170530Ssam
454170530Ssam/*
455170530Ssam * Receive processing.
456170530Ssam */
457170530Ssam
458170530Ssam/*
459170530Ssam * Decap the encapsulated A-MSDU frames and dispatch all but
460170530Ssam * the last for delivery.  The last frame is returned for
461170530Ssam * delivery via the normal path.
462170530Ssam */
463170530Ssamstruct mbuf *
464170530Ssamieee80211_decap_amsdu(struct ieee80211_node *ni, struct mbuf *m)
465170530Ssam{
466178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
467173462Ssam	int framelen;
468170530Ssam	struct mbuf *n;
469170530Ssam
470170530Ssam	/* discard 802.3 header inserted by ieee80211_decap */
471170530Ssam	m_adj(m, sizeof(struct ether_header));
472170530Ssam
473178354Ssam	vap->iv_stats.is_amsdu_decap++;
474170530Ssam
475170530Ssam	for (;;) {
476170530Ssam		/*
477170530Ssam		 * Decap the first frame, bust it apart from the
478170530Ssam		 * remainder and deliver.  We leave the last frame
479170530Ssam		 * delivery to the caller (for consistency with other
480170530Ssam		 * code paths, could also do it here).
481170530Ssam		 */
482170530Ssam		m = ieee80211_decap1(m, &framelen);
483170530Ssam		if (m == NULL) {
484178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
485173462Ssam			    ni->ni_macaddr, "a-msdu", "%s", "decap failed");
486178354Ssam			vap->iv_stats.is_amsdu_tooshort++;
487170530Ssam			return NULL;
488170530Ssam		}
489173462Ssam		if (m->m_pkthdr.len == framelen)
490170530Ssam			break;
491170530Ssam		n = m_split(m, framelen, M_NOWAIT);
492170530Ssam		if (n == NULL) {
493178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
494170530Ssam			    ni->ni_macaddr, "a-msdu",
495170530Ssam			    "%s", "unable to split encapsulated frames");
496178354Ssam			vap->iv_stats.is_amsdu_split++;
497170530Ssam			m_freem(m);			/* NB: must reclaim */
498170530Ssam			return NULL;
499170530Ssam		}
500178354Ssam		vap->iv_deliver_data(vap, ni, m);
501170530Ssam
502170530Ssam		/*
503170530Ssam		 * Remove frame contents; each intermediate frame
504170530Ssam		 * is required to be aligned to a 4-byte boundary.
505170530Ssam		 */
506170530Ssam		m = n;
507170530Ssam		m_adj(m, roundup2(framelen, 4) - framelen);	/* padding */
508170530Ssam	}
509170530Ssam	return m;				/* last delivered by caller */
510170530Ssam}
511170530Ssam
512170530Ssam/*
513170530Ssam * Purge all frames in the A-MPDU re-order queue.
514170530Ssam */
515170530Ssamstatic void
516170530Ssamampdu_rx_purge(struct ieee80211_rx_ampdu *rap)
517170530Ssam{
518170530Ssam	struct mbuf *m;
519170530Ssam	int i;
520170530Ssam
521170530Ssam	for (i = 0; i < rap->rxa_wnd; i++) {
522170530Ssam		m = rap->rxa_m[i];
523170530Ssam		if (m != NULL) {
524170530Ssam			rap->rxa_m[i] = NULL;
525170530Ssam			rap->rxa_qbytes -= m->m_pkthdr.len;
526170530Ssam			m_freem(m);
527170530Ssam			if (--rap->rxa_qframes == 0)
528170530Ssam				break;
529170530Ssam		}
530170530Ssam	}
531170530Ssam	KASSERT(rap->rxa_qbytes == 0 && rap->rxa_qframes == 0,
532170530Ssam	    ("lost %u data, %u frames on ampdu rx q",
533170530Ssam	    rap->rxa_qbytes, rap->rxa_qframes));
534170530Ssam}
535170530Ssam
536170530Ssam/*
537178354Ssam * Start A-MPDU rx/re-order processing for the specified TID.
538178354Ssam */
539191552Ssamstatic int
540191552Ssamampdu_rx_start(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap,
541191552Ssam	int baparamset, int batimeout, int baseqctl)
542178354Ssam{
543191552Ssam	int bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
544191552Ssam
545178354Ssam	if (rap->rxa_flags & IEEE80211_AGGR_RUNNING) {
546178354Ssam		/*
547178354Ssam		 * AMPDU previously setup and not terminated with a DELBA,
548178354Ssam		 * flush the reorder q's in case anything remains.
549178354Ssam		 */
550178354Ssam		ampdu_rx_purge(rap);
551178354Ssam	}
552178354Ssam	memset(rap, 0, sizeof(*rap));
553178354Ssam	rap->rxa_wnd = (bufsiz == 0) ?
554178354Ssam	    IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
555191552Ssam	rap->rxa_start = MS(baseqctl, IEEE80211_BASEQ_START);
556178354Ssam	rap->rxa_flags |=  IEEE80211_AGGR_RUNNING | IEEE80211_AGGR_XCHGPEND;
557191552Ssam
558191552Ssam	return 0;
559178354Ssam}
560178354Ssam
561178354Ssam/*
562170530Ssam * Stop A-MPDU rx processing for the specified TID.
563170530Ssam */
564170530Ssamstatic void
565191552Ssamampdu_rx_stop(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
566170530Ssam{
567205277Srpaulo
568170530Ssam	ampdu_rx_purge(rap);
569178354Ssam	rap->rxa_flags &= ~(IEEE80211_AGGR_RUNNING | IEEE80211_AGGR_XCHGPEND);
570170530Ssam}
571170530Ssam
572170530Ssam/*
573170530Ssam * Dispatch a frame from the A-MPDU reorder queue.  The
574170530Ssam * frame is fed back into ieee80211_input marked with an
575183247Ssam * M_AMPDU_MPDU flag so it doesn't come back to us (it also
576170530Ssam * permits ieee80211_input to optimize re-processing).
577170530Ssam */
578170530Ssamstatic __inline void
579170530Ssamampdu_dispatch(struct ieee80211_node *ni, struct mbuf *m)
580170530Ssam{
581183247Ssam	m->m_flags |= M_AMPDU_MPDU;	/* bypass normal processing */
582192468Ssam	/* NB: rssi and noise are ignored w/ M_AMPDU_MPDU set */
583192468Ssam	(void) ieee80211_input(ni, m, 0, 0);
584170530Ssam}
585170530Ssam
586170530Ssam/*
587170530Ssam * Dispatch as many frames as possible from the re-order queue.
588170530Ssam * Frames will always be "at the front"; we process all frames
589170530Ssam * up to the first empty slot in the window.  On completion we
590170530Ssam * cleanup state if there are still pending frames in the current
591170530Ssam * BA window.  We assume the frame at slot 0 is already handled
592170530Ssam * by the caller; we always start at slot 1.
593170530Ssam */
594170530Ssamstatic void
595170530Ssamampdu_rx_dispatch(struct ieee80211_rx_ampdu *rap, struct ieee80211_node *ni)
596170530Ssam{
597178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
598170530Ssam	struct mbuf *m;
599170530Ssam	int i;
600170530Ssam
601170530Ssam	/* flush run of frames */
602170530Ssam	for (i = 1; i < rap->rxa_wnd; i++) {
603170530Ssam		m = rap->rxa_m[i];
604170530Ssam		if (m == NULL)
605170530Ssam			break;
606170530Ssam		rap->rxa_m[i] = NULL;
607170530Ssam		rap->rxa_qbytes -= m->m_pkthdr.len;
608170530Ssam		rap->rxa_qframes--;
609170530Ssam
610170530Ssam		ampdu_dispatch(ni, m);
611170530Ssam	}
612170530Ssam	/*
613170530Ssam	 * If frames remain, copy the mbuf pointers down so
614170530Ssam	 * they correspond to the offsets in the new window.
615170530Ssam	 */
616170530Ssam	if (rap->rxa_qframes != 0) {
617170530Ssam		int n = rap->rxa_qframes, j;
618170530Ssam		for (j = i+1; j < rap->rxa_wnd; j++) {
619170530Ssam			if (rap->rxa_m[j] != NULL) {
620170530Ssam				rap->rxa_m[j-i] = rap->rxa_m[j];
621170530Ssam				rap->rxa_m[j] = NULL;
622170530Ssam				if (--n == 0)
623170530Ssam					break;
624170530Ssam			}
625170530Ssam		}
626170530Ssam		KASSERT(n == 0, ("lost %d frames", n));
627178354Ssam		vap->iv_stats.is_ampdu_rx_copy += rap->rxa_qframes;
628170530Ssam	}
629173273Ssam	/*
630173273Ssam	 * Adjust the start of the BA window to
631173273Ssam	 * reflect the frames just dispatched.
632173273Ssam	 */
633173273Ssam	rap->rxa_start = IEEE80211_SEQ_ADD(rap->rxa_start, i);
634178354Ssam	vap->iv_stats.is_ampdu_rx_oor += i;
635170530Ssam}
636170530Ssam
637173273Ssam#ifdef IEEE80211_AMPDU_AGE
638170530Ssam/*
639173273Ssam * Dispatch all frames in the A-MPDU re-order queue.
640170530Ssam */
641170530Ssamstatic void
642173273Ssamampdu_rx_flush(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
643170530Ssam{
644178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
645170530Ssam	struct mbuf *m;
646170530Ssam	int i;
647170530Ssam
648173273Ssam	for (i = 0; i < rap->rxa_wnd; i++) {
649170530Ssam		m = rap->rxa_m[i];
650170530Ssam		if (m == NULL)
651170530Ssam			continue;
652170530Ssam		rap->rxa_m[i] = NULL;
653170530Ssam		rap->rxa_qbytes -= m->m_pkthdr.len;
654173273Ssam		rap->rxa_qframes--;
655178354Ssam		vap->iv_stats.is_ampdu_rx_oor++;
656173273Ssam
657170530Ssam		ampdu_dispatch(ni, m);
658173273Ssam		if (rap->rxa_qframes == 0)
659170530Ssam			break;
660170530Ssam	}
661170530Ssam}
662173273Ssam#endif /* IEEE80211_AMPDU_AGE */
663170530Ssam
664170530Ssam/*
665173273Ssam * Dispatch all frames in the A-MPDU re-order queue
666173273Ssam * preceding the specified sequence number.  This logic
667173273Ssam * handles window moves due to a received MSDU or BAR.
668173273Ssam */
669173273Ssamstatic void
670173273Ssamampdu_rx_flush_upto(struct ieee80211_node *ni,
671173273Ssam	struct ieee80211_rx_ampdu *rap, ieee80211_seq winstart)
672173273Ssam{
673178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
674173273Ssam	struct mbuf *m;
675173273Ssam	ieee80211_seq seqno;
676173273Ssam	int i;
677173273Ssam
678173273Ssam	/*
679173273Ssam	 * Flush any complete MSDU's with a sequence number lower
680173273Ssam	 * than winstart.  Gaps may exist.  Note that we may actually
681173273Ssam	 * dispatch frames past winstart if a run continues; this is
682173273Ssam	 * an optimization that avoids having to do a separate pass
683173273Ssam	 * to dispatch frames after moving the BA window start.
684173273Ssam	 */
685173273Ssam	seqno = rap->rxa_start;
686173273Ssam	for (i = 0; i < rap->rxa_wnd; i++) {
687173273Ssam		m = rap->rxa_m[i];
688173273Ssam		if (m != NULL) {
689173273Ssam			rap->rxa_m[i] = NULL;
690173273Ssam			rap->rxa_qbytes -= m->m_pkthdr.len;
691173273Ssam			rap->rxa_qframes--;
692178354Ssam			vap->iv_stats.is_ampdu_rx_oor++;
693173273Ssam
694173273Ssam			ampdu_dispatch(ni, m);
695173273Ssam		} else {
696173273Ssam			if (!IEEE80211_SEQ_BA_BEFORE(seqno, winstart))
697173273Ssam				break;
698173273Ssam		}
699173273Ssam		seqno = IEEE80211_SEQ_INC(seqno);
700173273Ssam	}
701173273Ssam	/*
702173273Ssam	 * If frames remain, copy the mbuf pointers down so
703173273Ssam	 * they correspond to the offsets in the new window.
704173273Ssam	 */
705173273Ssam	if (rap->rxa_qframes != 0) {
706173273Ssam		int n = rap->rxa_qframes, j;
707178354Ssam
708178354Ssam		/* NB: this loop assumes i > 0 and/or rxa_m[0] is NULL */
709178354Ssam		KASSERT(rap->rxa_m[0] == NULL,
710178354Ssam		    ("%s: BA window slot 0 occupied", __func__));
711173273Ssam		for (j = i+1; j < rap->rxa_wnd; j++) {
712173273Ssam			if (rap->rxa_m[j] != NULL) {
713173273Ssam				rap->rxa_m[j-i] = rap->rxa_m[j];
714173273Ssam				rap->rxa_m[j] = NULL;
715173273Ssam				if (--n == 0)
716173273Ssam					break;
717173273Ssam			}
718173273Ssam		}
719173273Ssam		KASSERT(n == 0, ("%s: lost %d frames, qframes %d off %d "
720173273Ssam		    "BA win <%d:%d> winstart %d",
721173273Ssam		    __func__, n, rap->rxa_qframes, i, rap->rxa_start,
722173273Ssam		    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
723173273Ssam		    winstart));
724178354Ssam		vap->iv_stats.is_ampdu_rx_copy += rap->rxa_qframes;
725173273Ssam	}
726173273Ssam	/*
727173273Ssam	 * Move the start of the BA window; we use the
728173273Ssam	 * sequence number of the last MSDU that was
729173273Ssam	 * passed up the stack+1 or winstart if stopped on
730173273Ssam	 * a gap in the reorder buffer.
731173273Ssam	 */
732173273Ssam	rap->rxa_start = seqno;
733173273Ssam}
734173273Ssam
735173273Ssam/*
736170530Ssam * Process a received QoS data frame for an HT station.  Handle
737170530Ssam * A-MPDU reordering: if this frame is received out of order
738170530Ssam * and falls within the BA window hold onto it.  Otherwise if
739173273Ssam * this frame completes a run, flush any pending frames.  We
740170530Ssam * return 1 if the frame is consumed.  A 0 is returned if
741170530Ssam * the frame should be processed normally by the caller.
742170530Ssam */
743170530Ssamint
744170530Ssamieee80211_ampdu_reorder(struct ieee80211_node *ni, struct mbuf *m)
745170530Ssam{
746170530Ssam#define	IEEE80211_FC0_QOSDATA \
747170530Ssam	(IEEE80211_FC0_TYPE_DATA|IEEE80211_FC0_SUBTYPE_QOS|IEEE80211_FC0_VERSION_0)
748173273Ssam#define	PROCESS		0	/* caller should process frame */
749173273Ssam#define	CONSUMED	1	/* frame consumed, caller does nothing */
750178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
751170530Ssam	struct ieee80211_qosframe *wh;
752170530Ssam	struct ieee80211_rx_ampdu *rap;
753170530Ssam	ieee80211_seq rxseq;
754170530Ssam	uint8_t tid;
755170530Ssam	int off;
756170530Ssam
757183247Ssam	KASSERT((m->m_flags & (M_AMPDU | M_AMPDU_MPDU)) == M_AMPDU,
758183247Ssam	    ("!a-mpdu or already re-ordered, flags 0x%x", m->m_flags));
759170530Ssam	KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT sta"));
760170530Ssam
761170530Ssam	/* NB: m_len known to be sufficient */
762170530Ssam	wh = mtod(m, struct ieee80211_qosframe *);
763183247Ssam	if (wh->i_fc[0] != IEEE80211_FC0_QOSDATA) {
764183247Ssam		/*
765183247Ssam		 * Not QoS data, shouldn't get here but just
766183247Ssam		 * return it to the caller for processing.
767183247Ssam		 */
768183247Ssam		return PROCESS;
769183247Ssam	}
770193840Ssam	if (IEEE80211_IS_DSTODS(wh))
771173273Ssam		tid = ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0];
772173273Ssam	else
773173273Ssam		tid = wh->i_qos[0];
774173273Ssam	tid &= IEEE80211_QOS_TID;
775170530Ssam	rap = &ni->ni_rx_ampdu[tid];
776170530Ssam	if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
777170530Ssam		/*
778170530Ssam		 * No ADDBA request yet, don't touch.
779170530Ssam		 */
780173273Ssam		return PROCESS;
781170530Ssam	}
782182827Ssam	rxseq = le16toh(*(uint16_t *)wh->i_seq);
783182827Ssam	if ((rxseq & IEEE80211_SEQ_FRAG_MASK) != 0) {
784182827Ssam		/*
785182827Ssam		 * Fragments are not allowed; toss.
786182827Ssam		 */
787182827Ssam		IEEE80211_DISCARD_MAC(vap,
788182827Ssam		    IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
789182827Ssam		    "A-MPDU", "fragment, rxseq 0x%x tid %u%s", rxseq, tid,
790182827Ssam		    wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
791182827Ssam		vap->iv_stats.is_ampdu_rx_drop++;
792182827Ssam		IEEE80211_NODE_STAT(ni, rx_drop);
793182827Ssam		m_freem(m);
794182827Ssam		return CONSUMED;
795182827Ssam	}
796182827Ssam	rxseq >>= IEEE80211_SEQ_SEQ_SHIFT;
797173273Ssam	rap->rxa_nframes++;
798173273Ssamagain:
799170530Ssam	if (rxseq == rap->rxa_start) {
800170530Ssam		/*
801170530Ssam		 * First frame in window.
802170530Ssam		 */
803170530Ssam		if (rap->rxa_qframes != 0) {
804170530Ssam			/*
805170530Ssam			 * Dispatch as many packets as we can.
806170530Ssam			 */
807170530Ssam			KASSERT(rap->rxa_m[0] == NULL, ("unexpected dup"));
808170530Ssam			ampdu_dispatch(ni, m);
809170530Ssam			ampdu_rx_dispatch(rap, ni);
810173273Ssam			return CONSUMED;
811170530Ssam		} else {
812170530Ssam			/*
813170530Ssam			 * In order; advance window and notify
814170530Ssam			 * caller to dispatch directly.
815170530Ssam			 */
816170530Ssam			rap->rxa_start = IEEE80211_SEQ_INC(rxseq);
817173273Ssam			return PROCESS;
818170530Ssam		}
819170530Ssam	}
820170530Ssam	/*
821173273Ssam	 * Frame is out of order; store if in the BA window.
822170530Ssam	 */
823170530Ssam	/* calculate offset in BA window */
824170530Ssam	off = IEEE80211_SEQ_SUB(rxseq, rap->rxa_start);
825173273Ssam	if (off < rap->rxa_wnd) {
826170530Ssam		/*
827173273Ssam		 * Common case (hopefully): in the BA window.
828205277Srpaulo		 * Sec 9.10.7.6.2 a) (p.137)
829173273Ssam		 */
830173273Ssam#ifdef IEEE80211_AMPDU_AGE
831173273Ssam		/*
832173273Ssam		 * Check for frames sitting too long in the reorder queue.
833173273Ssam		 * This should only ever happen if frames are not delivered
834173273Ssam		 * without the sender otherwise notifying us (e.g. with a
835173273Ssam		 * BAR to move the window).  Typically this happens because
836173273Ssam		 * of vendor bugs that cause the sequence number to jump.
837173273Ssam		 * When this happens we get a gap in the reorder queue that
838173273Ssam		 * leaves frame sitting on the queue until they get pushed
839173273Ssam		 * out due to window moves.  When the vendor does not send
840173273Ssam		 * BAR this move only happens due to explicit packet sends
841170530Ssam		 *
842173273Ssam		 * NB: we only track the time of the oldest frame in the
843173273Ssam		 * reorder q; this means that if we flush we might push
844173273Ssam		 * frames that still "new"; if this happens then subsequent
845173273Ssam		 * frames will result in BA window moves which cost something
846173273Ssam		 * but is still better than a big throughput dip.
847170530Ssam		 */
848173273Ssam		if (rap->rxa_qframes != 0) {
849173273Ssam			/* XXX honor batimeout? */
850173273Ssam			if (ticks - rap->rxa_age > ieee80211_ampdu_age) {
851173273Ssam				/*
852173273Ssam				 * Too long since we received the first
853173273Ssam				 * frame; flush the reorder buffer.
854173273Ssam				 */
855173273Ssam				if (rap->rxa_qframes != 0) {
856178354Ssam					vap->iv_stats.is_ampdu_rx_age +=
857173273Ssam					    rap->rxa_qframes;
858173273Ssam					ampdu_rx_flush(ni, rap);
859173273Ssam				}
860173273Ssam				rap->rxa_start = IEEE80211_SEQ_INC(rxseq);
861173273Ssam				return PROCESS;
862173273Ssam			}
863173273Ssam		} else {
864173273Ssam			/*
865173273Ssam			 * First frame, start aging timer.
866173273Ssam			 */
867173273Ssam			rap->rxa_age = ticks;
868173273Ssam		}
869173273Ssam#endif /* IEEE80211_AMPDU_AGE */
870173273Ssam		/* save packet */
871173273Ssam		if (rap->rxa_m[off] == NULL) {
872173273Ssam			rap->rxa_m[off] = m;
873173273Ssam			rap->rxa_qframes++;
874173273Ssam			rap->rxa_qbytes += m->m_pkthdr.len;
875178354Ssam			vap->iv_stats.is_ampdu_rx_reorder++;
876173273Ssam		} else {
877178354Ssam			IEEE80211_DISCARD_MAC(vap,
878173273Ssam			    IEEE80211_MSG_INPUT | IEEE80211_MSG_11N,
879173273Ssam			    ni->ni_macaddr, "a-mpdu duplicate",
880173273Ssam			    "seqno %u tid %u BA win <%u:%u>",
881173273Ssam			    rxseq, tid, rap->rxa_start,
882173273Ssam			    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1));
883178354Ssam			vap->iv_stats.is_rx_dup++;
884173273Ssam			IEEE80211_NODE_STAT(ni, rx_dup);
885173273Ssam			m_freem(m);
886173273Ssam		}
887173273Ssam		return CONSUMED;
888173273Ssam	}
889173273Ssam	if (off < IEEE80211_SEQ_BA_RANGE) {
890173273Ssam		/*
891173273Ssam		 * Outside the BA window, but within range;
892173273Ssam		 * flush the reorder q and move the window.
893205277Srpaulo		 * Sec 9.10.7.6.2 b) (p.138)
894173273Ssam		 */
895178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
896173273Ssam		    "move BA win <%u:%u> (%u frames) rxseq %u tid %u",
897170530Ssam		    rap->rxa_start,
898173273Ssam		    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
899170530Ssam		    rap->rxa_qframes, rxseq, tid);
900178354Ssam		vap->iv_stats.is_ampdu_rx_move++;
901170530Ssam
902173273Ssam		/*
903173273Ssam		 * The spec says to flush frames up to but not including:
904173273Ssam		 * 	WinStart_B = rxseq - rap->rxa_wnd + 1
905173273Ssam		 * Then insert the frame or notify the caller to process
906173273Ssam		 * it immediately.  We can safely do this by just starting
907173273Ssam		 * over again because we know the frame will now be within
908173273Ssam		 * the BA window.
909173273Ssam		 */
910173273Ssam		/* NB: rxa_wnd known to be >0 */
911173273Ssam		ampdu_rx_flush_upto(ni, rap,
912173273Ssam		    IEEE80211_SEQ_SUB(rxseq, rap->rxa_wnd-1));
913173273Ssam		goto again;
914170530Ssam	} else {
915170530Ssam		/*
916173273Ssam		 * Outside the BA window and out of range; toss.
917205277Srpaulo		 * Sec 9.10.7.6.2 c) (p.138)
918170530Ssam		 */
919178354Ssam		IEEE80211_DISCARD_MAC(vap,
920173273Ssam		    IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
921178354Ssam		    "MPDU", "BA win <%u:%u> (%u frames) rxseq %u tid %u%s",
922173273Ssam		    rap->rxa_start,
923173273Ssam		    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
924173273Ssam		    rap->rxa_qframes, rxseq, tid,
925173273Ssam		    wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
926178354Ssam		vap->iv_stats.is_ampdu_rx_drop++;
927173273Ssam		IEEE80211_NODE_STAT(ni, rx_drop);
928170530Ssam		m_freem(m);
929173273Ssam		return CONSUMED;
930170530Ssam	}
931173273Ssam#undef CONSUMED
932173273Ssam#undef PROCESS
933170530Ssam#undef IEEE80211_FC0_QOSDATA
934170530Ssam}
935170530Ssam
936170530Ssam/*
937170530Ssam * Process a BAR ctl frame.  Dispatch all frames up to
938170530Ssam * the sequence number of the frame.  If this frame is
939173273Ssam * out of range it's discarded.
940170530Ssam */
941170530Ssamvoid
942170530Ssamieee80211_recv_bar(struct ieee80211_node *ni, struct mbuf *m0)
943170530Ssam{
944178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
945170530Ssam	struct ieee80211_frame_bar *wh;
946170530Ssam	struct ieee80211_rx_ampdu *rap;
947170530Ssam	ieee80211_seq rxseq;
948170530Ssam	int tid, off;
949170530Ssam
950173273Ssam	if (!ieee80211_recv_bar_ena) {
951173273Ssam#if 0
952178354Ssam		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_11N,
953173273Ssam		    ni->ni_macaddr, "BAR", "%s", "processing disabled");
954173273Ssam#endif
955178354Ssam		vap->iv_stats.is_ampdu_bar_bad++;
956173273Ssam		return;
957173273Ssam	}
958170530Ssam	wh = mtod(m0, struct ieee80211_frame_bar *);
959170530Ssam	/* XXX check basic BAR */
960170530Ssam	tid = MS(le16toh(wh->i_ctl), IEEE80211_BAR_TID);
961170530Ssam	rap = &ni->ni_rx_ampdu[tid];
962170530Ssam	if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
963170530Ssam		/*
964170530Ssam		 * No ADDBA request yet, don't touch.
965170530Ssam		 */
966178354Ssam		IEEE80211_DISCARD_MAC(vap,
967170530Ssam		    IEEE80211_MSG_INPUT | IEEE80211_MSG_11N,
968170530Ssam		    ni->ni_macaddr, "BAR", "no BA stream, tid %u", tid);
969178354Ssam		vap->iv_stats.is_ampdu_bar_bad++;
970170530Ssam		return;
971170530Ssam	}
972178354Ssam	vap->iv_stats.is_ampdu_bar_rx++;
973170530Ssam	rxseq = le16toh(wh->i_seq) >> IEEE80211_SEQ_SEQ_SHIFT;
974173273Ssam	if (rxseq == rap->rxa_start)
975173273Ssam		return;
976170530Ssam	/* calculate offset in BA window */
977170530Ssam	off = IEEE80211_SEQ_SUB(rxseq, rap->rxa_start);
978173273Ssam	if (off < IEEE80211_SEQ_BA_RANGE) {
979170530Ssam		/*
980173273Ssam		 * Flush the reorder q up to rxseq and move the window.
981205277Srpaulo		 * Sec 9.10.7.6.3 a) (p.138)
982170530Ssam		 */
983178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
984173273Ssam		    "BAR moves BA win <%u:%u> (%u frames) rxseq %u tid %u",
985170530Ssam		    rap->rxa_start,
986173273Ssam		    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
987173273Ssam		    rap->rxa_qframes, rxseq, tid);
988178354Ssam		vap->iv_stats.is_ampdu_bar_move++;
989173273Ssam
990173273Ssam		ampdu_rx_flush_upto(ni, rap, rxseq);
991173273Ssam		if (off >= rap->rxa_wnd) {
992173273Ssam			/*
993173273Ssam			 * BAR specifies a window start to the right of BA
994173273Ssam			 * window; we must move it explicitly since
995173273Ssam			 * ampdu_rx_flush_upto will not.
996173273Ssam			 */
997173273Ssam			rap->rxa_start = rxseq;
998170530Ssam		}
999173273Ssam	} else {
1000170530Ssam		/*
1001173273Ssam		 * Out of range; toss.
1002205277Srpaulo		 * Sec 9.10.7.6.3 b) (p.138)
1003170530Ssam		 */
1004178354Ssam		IEEE80211_DISCARD_MAC(vap,
1005173273Ssam		    IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
1006173273Ssam		    "BAR", "BA win <%u:%u> (%u frames) rxseq %u tid %u%s",
1007173273Ssam		    rap->rxa_start,
1008173273Ssam		    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
1009173273Ssam		    rap->rxa_qframes, rxseq, tid,
1010173273Ssam		    wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
1011178354Ssam		vap->iv_stats.is_ampdu_bar_oow++;
1012173273Ssam		IEEE80211_NODE_STAT(ni, rx_drop);
1013170530Ssam	}
1014170530Ssam}
1015170530Ssam
1016170530Ssam/*
1017170530Ssam * Setup HT-specific state in a node.  Called only
1018170530Ssam * when HT use is negotiated so we don't do extra
1019170530Ssam * work for temporary and/or legacy sta's.
1020170530Ssam */
1021170530Ssamvoid
1022183254Ssamieee80211_ht_node_init(struct ieee80211_node *ni)
1023170530Ssam{
1024170530Ssam	struct ieee80211_tx_ampdu *tap;
1025170530Ssam	int ac;
1026170530Ssam
1027173273Ssam	if (ni->ni_flags & IEEE80211_NODE_HT) {
1028173273Ssam		/*
1029173273Ssam		 * Clean AMPDU state on re-associate.  This handles the case
1030173273Ssam		 * where a station leaves w/o notifying us and then returns
1031173273Ssam		 * before node is reaped for inactivity.
1032173273Ssam		 */
1033173273Ssam		ieee80211_ht_node_cleanup(ni);
1034173273Ssam	}
1035170530Ssam	for (ac = 0; ac < WME_NUM_AC; ac++) {
1036170530Ssam		tap = &ni->ni_tx_ampdu[ac];
1037170530Ssam		tap->txa_ac = ac;
1038184280Ssam		tap->txa_ni = ni;
1039173273Ssam		/* NB: further initialization deferred */
1040170530Ssam	}
1041173273Ssam	ni->ni_flags |= IEEE80211_NODE_HT | IEEE80211_NODE_AMPDU;
1042170530Ssam}
1043170530Ssam
1044170530Ssam/*
1045170530Ssam * Cleanup HT-specific state in a node.  Called only
1046170530Ssam * when HT use has been marked.
1047170530Ssam */
1048170530Ssamvoid
1049170530Ssamieee80211_ht_node_cleanup(struct ieee80211_node *ni)
1050170530Ssam{
1051191552Ssam	struct ieee80211com *ic = ni->ni_ic;
1052170530Ssam	int i;
1053170530Ssam
1054170530Ssam	KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT node"));
1055170530Ssam
1056170530Ssam	/* XXX optimize this */
1057170530Ssam	for (i = 0; i < WME_NUM_AC; i++) {
1058170530Ssam		struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[i];
1059184280Ssam		if (tap->txa_flags & IEEE80211_AGGR_SETUP)
1060184280Ssam			ampdu_tx_stop(tap);
1061170530Ssam	}
1062170530Ssam	for (i = 0; i < WME_NUM_TID; i++)
1063191552Ssam		ic->ic_ampdu_rx_stop(ni, &ni->ni_rx_ampdu[i]);
1064170530Ssam
1065170530Ssam	ni->ni_htcap = 0;
1066182828Ssam	ni->ni_flags &= ~IEEE80211_NODE_HT_ALL;
1067170530Ssam}
1068170530Ssam
1069178354Ssam/*
1070178354Ssam * Age out HT resources for a station.
1071178354Ssam */
1072178354Ssamvoid
1073178354Ssamieee80211_ht_node_age(struct ieee80211_node *ni)
1074178354Ssam{
1075178354Ssam#ifdef IEEE80211_AMPDU_AGE
1076178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1077178354Ssam	uint8_t tid;
1078178354Ssam#endif
1079178354Ssam
1080178354Ssam	KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT sta"));
1081178354Ssam
1082178354Ssam#ifdef IEEE80211_AMPDU_AGE
1083178354Ssam	for (tid = 0; tid < WME_NUM_TID; tid++) {
1084178354Ssam		struct ieee80211_rx_ampdu *rap;
1085178354Ssam
1086178354Ssam		rap = &ni->ni_rx_ampdu[tid];
1087178354Ssam		if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0)
1088178354Ssam			continue;
1089178354Ssam		if (rap->rxa_qframes == 0)
1090178354Ssam			continue;
1091178354Ssam		/*
1092178354Ssam		 * Check for frames sitting too long in the reorder queue.
1093178354Ssam		 * See above for more details on what's happening here.
1094178354Ssam		 */
1095178354Ssam		/* XXX honor batimeout? */
1096178354Ssam		if (ticks - rap->rxa_age > ieee80211_ampdu_age) {
1097178354Ssam			/*
1098178354Ssam			 * Too long since we received the first
1099178354Ssam			 * frame; flush the reorder buffer.
1100178354Ssam			 */
1101178354Ssam			vap->iv_stats.is_ampdu_rx_age += rap->rxa_qframes;
1102178354Ssam			ampdu_rx_flush(ni, rap);
1103178354Ssam		}
1104178354Ssam	}
1105178354Ssam#endif /* IEEE80211_AMPDU_AGE */
1106178354Ssam}
1107178354Ssam
1108173273Ssamstatic struct ieee80211_channel *
1109173273Ssamfindhtchan(struct ieee80211com *ic, struct ieee80211_channel *c, int htflags)
1110173273Ssam{
1111173273Ssam	return ieee80211_find_channel(ic, c->ic_freq,
1112173273Ssam	    (c->ic_flags &~ IEEE80211_CHAN_HT) | htflags);
1113173273Ssam}
1114173273Ssam
1115173273Ssam/*
1116173273Ssam * Adjust a channel to be HT/non-HT according to the vap's configuration.
1117173273Ssam */
1118173273Ssamstruct ieee80211_channel *
1119173273Ssamieee80211_ht_adjust_channel(struct ieee80211com *ic,
1120173273Ssam	struct ieee80211_channel *chan, int flags)
1121173273Ssam{
1122173273Ssam	struct ieee80211_channel *c;
1123173273Ssam
1124193655Ssam	if (flags & IEEE80211_FHT_HT) {
1125173273Ssam		/* promote to HT if possible */
1126193655Ssam		if (flags & IEEE80211_FHT_USEHT40) {
1127173273Ssam			if (!IEEE80211_IS_CHAN_HT40(chan)) {
1128173273Ssam				/* NB: arbitrarily pick ht40+ over ht40- */
1129173273Ssam				c = findhtchan(ic, chan, IEEE80211_CHAN_HT40U);
1130173273Ssam				if (c == NULL)
1131173273Ssam					c = findhtchan(ic, chan,
1132173273Ssam						IEEE80211_CHAN_HT40D);
1133173273Ssam				if (c == NULL)
1134173273Ssam					c = findhtchan(ic, chan,
1135173273Ssam						IEEE80211_CHAN_HT20);
1136173273Ssam				if (c != NULL)
1137173273Ssam					chan = c;
1138173273Ssam			}
1139173273Ssam		} else if (!IEEE80211_IS_CHAN_HT20(chan)) {
1140173273Ssam			c = findhtchan(ic, chan, IEEE80211_CHAN_HT20);
1141173273Ssam			if (c != NULL)
1142173273Ssam				chan = c;
1143173273Ssam		}
1144173273Ssam	} else if (IEEE80211_IS_CHAN_HT(chan)) {
1145173273Ssam		/* demote to legacy, HT use is disabled */
1146173273Ssam		c = ieee80211_find_channel(ic, chan->ic_freq,
1147173273Ssam		    chan->ic_flags &~ IEEE80211_CHAN_HT);
1148173273Ssam		if (c != NULL)
1149173273Ssam			chan = c;
1150173273Ssam	}
1151173273Ssam	return chan;
1152173273Ssam}
1153173273Ssam
1154173273Ssam/*
1155173273Ssam * Setup HT-specific state for a legacy WDS peer.
1156173273Ssam */
1157173273Ssamvoid
1158173273Ssamieee80211_ht_wds_init(struct ieee80211_node *ni)
1159173273Ssam{
1160178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1161173273Ssam	struct ieee80211_tx_ampdu *tap;
1162173273Ssam	int ac;
1163173273Ssam
1164193655Ssam	KASSERT(vap->iv_flags_ht & IEEE80211_FHT_HT, ("no HT requested"));
1165173273Ssam
1166173273Ssam	/* XXX check scan cache in case peer has an ap and we have info */
1167173273Ssam	/*
1168173273Ssam	 * If setup with a legacy channel; locate an HT channel.
1169173273Ssam	 * Otherwise if the inherited channel (from a companion
1170173273Ssam	 * AP) is suitable use it so we use the same location
1171173273Ssam	 * for the extension channel).
1172173273Ssam	 */
1173178354Ssam	ni->ni_chan = ieee80211_ht_adjust_channel(ni->ni_ic,
1174178354Ssam	    ni->ni_chan, ieee80211_htchanflags(ni->ni_chan));
1175173273Ssam
1176173273Ssam	ni->ni_htcap = 0;
1177193655Ssam	if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20)
1178173273Ssam		ni->ni_htcap |= IEEE80211_HTCAP_SHORTGI20;
1179173273Ssam	if (IEEE80211_IS_CHAN_HT40(ni->ni_chan)) {
1180173273Ssam		ni->ni_htcap |= IEEE80211_HTCAP_CHWIDTH40;
1181173273Ssam		ni->ni_chw = 40;
1182173273Ssam		if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan))
1183173273Ssam			ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_ABOVE;
1184173273Ssam		else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan))
1185173273Ssam			ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_BELOW;
1186193655Ssam		if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40)
1187173273Ssam			ni->ni_htcap |= IEEE80211_HTCAP_SHORTGI40;
1188173273Ssam	} else {
1189173273Ssam		ni->ni_chw = 20;
1190173273Ssam		ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_NONE;
1191173273Ssam	}
1192173273Ssam	ni->ni_htctlchan = ni->ni_chan->ic_ieee;
1193193655Ssam	if (vap->iv_flags_ht & IEEE80211_FHT_RIFS)
1194183256Ssam		ni->ni_flags |= IEEE80211_NODE_RIFS;
1195183256Ssam	/* XXX does it make sense to enable SMPS? */
1196173273Ssam
1197173273Ssam	ni->ni_htopmode = 0;		/* XXX need protection state */
1198173273Ssam	ni->ni_htstbc = 0;		/* XXX need info */
1199173273Ssam
1200173273Ssam	for (ac = 0; ac < WME_NUM_AC; ac++) {
1201173273Ssam		tap = &ni->ni_tx_ampdu[ac];
1202173273Ssam		tap->txa_ac = ac;
1203173273Ssam	}
1204193655Ssam	/* NB: AMPDU tx/rx governed by IEEE80211_FHT_AMPDU_{TX,RX} */
1205173273Ssam	ni->ni_flags |= IEEE80211_NODE_HT | IEEE80211_NODE_AMPDU;
1206173273Ssam}
1207173273Ssam
1208173273Ssam/*
1209173273Ssam * Notify hostap vaps of a change in the HTINFO ie.
1210173273Ssam */
1211173273Ssamstatic void
1212173273Ssamhtinfo_notify(struct ieee80211com *ic)
1213173273Ssam{
1214178354Ssam	struct ieee80211vap *vap;
1215178354Ssam	int first = 1;
1216178354Ssam
1217178354Ssam	IEEE80211_LOCK_ASSERT(ic);
1218178354Ssam
1219178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1220178354Ssam		if (vap->iv_opmode != IEEE80211_M_HOSTAP)
1221178354Ssam			continue;
1222183253Ssam		if (vap->iv_state != IEEE80211_S_RUN ||
1223183253Ssam		    !IEEE80211_IS_CHAN_HT(vap->iv_bss->ni_chan))
1224183253Ssam			continue;
1225178354Ssam		if (first) {
1226178354Ssam			IEEE80211_NOTE(vap,
1227178354Ssam			    IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N,
1228178354Ssam			    vap->iv_bss,
1229178354Ssam			    "HT bss occupancy change: %d sta, %d ht, "
1230178354Ssam			    "%d ht40%s, HT protmode now 0x%x"
1231178354Ssam			    , ic->ic_sta_assoc
1232178354Ssam			    , ic->ic_ht_sta_assoc
1233178354Ssam			    , ic->ic_ht40_sta_assoc
1234193655Ssam			    , (ic->ic_flags_ht & IEEE80211_FHT_NONHT_PR) ?
1235178354Ssam				 ", non-HT sta present" : ""
1236178354Ssam			    , ic->ic_curhtprotmode);
1237178354Ssam			first = 0;
1238178354Ssam		}
1239178354Ssam		ieee80211_beacon_notify(vap, IEEE80211_BEACON_HTINFO);
1240178354Ssam	}
1241173273Ssam}
1242173273Ssam
1243173273Ssam/*
1244173273Ssam * Calculate HT protection mode from current
1245173273Ssam * state and handle updates.
1246173273Ssam */
1247173273Ssamstatic void
1248173273Ssamhtinfo_update(struct ieee80211com *ic)
1249173273Ssam{
1250173273Ssam	uint8_t protmode;
1251173273Ssam
1252178354Ssam	if (ic->ic_sta_assoc != ic->ic_ht_sta_assoc) {
1253178354Ssam		protmode = IEEE80211_HTINFO_OPMODE_MIXED
1254178354Ssam			 | IEEE80211_HTINFO_NONHT_PRESENT;
1255193655Ssam	} else if (ic->ic_flags_ht & IEEE80211_FHT_NONHT_PR) {
1256173273Ssam		protmode = IEEE80211_HTINFO_OPMODE_PROTOPT
1257178354Ssam			 | IEEE80211_HTINFO_NONHT_PRESENT;
1258178354Ssam	} else if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
1259178354Ssam	    IEEE80211_IS_CHAN_HT40(ic->ic_bsschan) &&
1260173273Ssam	    ic->ic_sta_assoc != ic->ic_ht40_sta_assoc) {
1261173273Ssam		protmode = IEEE80211_HTINFO_OPMODE_HT20PR;
1262173273Ssam	} else {
1263173273Ssam		protmode = IEEE80211_HTINFO_OPMODE_PURE;
1264173273Ssam	}
1265173273Ssam	if (protmode != ic->ic_curhtprotmode) {
1266173273Ssam		ic->ic_curhtprotmode = protmode;
1267173273Ssam		htinfo_notify(ic);
1268173273Ssam	}
1269173273Ssam}
1270173273Ssam
1271173273Ssam/*
1272173273Ssam * Handle an HT station joining a BSS.
1273173273Ssam */
1274173273Ssamvoid
1275173273Ssamieee80211_ht_node_join(struct ieee80211_node *ni)
1276173273Ssam{
1277173273Ssam	struct ieee80211com *ic = ni->ni_ic;
1278173273Ssam
1279173273Ssam	IEEE80211_LOCK_ASSERT(ic);
1280173273Ssam
1281173273Ssam	if (ni->ni_flags & IEEE80211_NODE_HT) {
1282173273Ssam		ic->ic_ht_sta_assoc++;
1283173273Ssam		if (ni->ni_chw == 40)
1284173273Ssam			ic->ic_ht40_sta_assoc++;
1285173273Ssam	}
1286173273Ssam	htinfo_update(ic);
1287173273Ssam}
1288173273Ssam
1289173273Ssam/*
1290173273Ssam * Handle an HT station leaving a BSS.
1291173273Ssam */
1292173273Ssamvoid
1293173273Ssamieee80211_ht_node_leave(struct ieee80211_node *ni)
1294173273Ssam{
1295173273Ssam	struct ieee80211com *ic = ni->ni_ic;
1296173273Ssam
1297173273Ssam	IEEE80211_LOCK_ASSERT(ic);
1298173273Ssam
1299173273Ssam	if (ni->ni_flags & IEEE80211_NODE_HT) {
1300173273Ssam		ic->ic_ht_sta_assoc--;
1301173273Ssam		if (ni->ni_chw == 40)
1302173273Ssam			ic->ic_ht40_sta_assoc--;
1303173273Ssam	}
1304173273Ssam	htinfo_update(ic);
1305173273Ssam}
1306173273Ssam
1307173273Ssam/*
1308173273Ssam * Public version of htinfo_update; used for processing
1309178354Ssam * beacon frames from overlapping bss.
1310178354Ssam *
1311178354Ssam * Caller can specify either IEEE80211_HTINFO_OPMODE_MIXED
1312178354Ssam * (on receipt of a beacon that advertises MIXED) or
1313178354Ssam * IEEE80211_HTINFO_OPMODE_PROTOPT (on receipt of a beacon
1314178354Ssam * from an overlapping legacy bss).  We treat MIXED with
1315178354Ssam * a higher precedence than PROTOPT (i.e. we will not change
1316178354Ssam * change PROTOPT -> MIXED; only MIXED -> PROTOPT).  This
1317178354Ssam * corresponds to how we handle things in htinfo_update.
1318173273Ssam */
1319173273Ssamvoid
1320178354Ssamieee80211_htprot_update(struct ieee80211com *ic, int protmode)
1321173273Ssam{
1322178354Ssam#define	OPMODE(x)	SM(x, IEEE80211_HTINFO_OPMODE)
1323183246Ssam	IEEE80211_LOCK(ic);
1324178354Ssam
1325178354Ssam	/* track non-HT station presence */
1326178354Ssam	KASSERT(protmode & IEEE80211_HTINFO_NONHT_PRESENT,
1327183246Ssam	    ("protmode 0x%x", protmode));
1328193655Ssam	ic->ic_flags_ht |= IEEE80211_FHT_NONHT_PR;
1329178354Ssam	ic->ic_lastnonht = ticks;
1330178354Ssam
1331183246Ssam	if (protmode != ic->ic_curhtprotmode &&
1332183246Ssam	    (OPMODE(ic->ic_curhtprotmode) != IEEE80211_HTINFO_OPMODE_MIXED ||
1333183246Ssam	     OPMODE(protmode) == IEEE80211_HTINFO_OPMODE_PROTOPT)) {
1334183246Ssam		/* push beacon update */
1335183246Ssam		ic->ic_curhtprotmode = protmode;
1336183246Ssam		htinfo_notify(ic);
1337183246Ssam	}
1338181197Ssam	IEEE80211_UNLOCK(ic);
1339178354Ssam#undef OPMODE
1340173273Ssam}
1341173273Ssam
1342173273Ssam/*
1343173273Ssam * Time out presence of an overlapping bss with non-HT
1344173273Ssam * stations.  When operating in hostap mode we listen for
1345173273Ssam * beacons from other stations and if we identify a non-HT
1346173273Ssam * station is present we update the opmode field of the
1347173273Ssam * HTINFO ie.  To identify when all non-HT stations are
1348173273Ssam * gone we time out this condition.
1349173273Ssam */
1350173273Ssamvoid
1351173273Ssamieee80211_ht_timeout(struct ieee80211com *ic)
1352173273Ssam{
1353173273Ssam	IEEE80211_LOCK_ASSERT(ic);
1354173273Ssam
1355193655Ssam	if ((ic->ic_flags_ht & IEEE80211_FHT_NONHT_PR) &&
1356173273Ssam	    time_after(ticks, ic->ic_lastnonht + IEEE80211_NONHT_PRESENT_AGE)) {
1357173273Ssam#if 0
1358178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
1359173273Ssam		    "%s", "time out non-HT STA present on channel");
1360173273Ssam#endif
1361193655Ssam		ic->ic_flags_ht &= ~IEEE80211_FHT_NONHT_PR;
1362173273Ssam		htinfo_update(ic);
1363173273Ssam	}
1364173273Ssam}
1365173273Ssam
1366170530Ssam/* unalligned little endian access */
1367170530Ssam#define LE_READ_2(p)					\
1368170530Ssam	((uint16_t)					\
1369170530Ssam	 ((((const uint8_t *)(p))[0]      ) |		\
1370170530Ssam	  (((const uint8_t *)(p))[1] <<  8)))
1371170530Ssam
1372170530Ssam/*
1373170530Ssam * Process an 802.11n HT capabilities ie.
1374170530Ssam */
1375170530Ssamvoid
1376170530Ssamieee80211_parse_htcap(struct ieee80211_node *ni, const uint8_t *ie)
1377170530Ssam{
1378170530Ssam	if (ie[0] == IEEE80211_ELEMID_VENDOR) {
1379170530Ssam		/*
1380170530Ssam		 * Station used Vendor OUI ie to associate;
1381170530Ssam		 * mark the node so when we respond we'll use
1382170530Ssam		 * the Vendor OUI's and not the standard ie's.
1383170530Ssam		 */
1384170530Ssam		ni->ni_flags |= IEEE80211_NODE_HTCOMPAT;
1385170530Ssam		ie += 4;
1386170530Ssam	} else
1387170530Ssam		ni->ni_flags &= ~IEEE80211_NODE_HTCOMPAT;
1388170530Ssam
1389170530Ssam	ni->ni_htcap = LE_READ_2(ie +
1390170530Ssam		__offsetof(struct ieee80211_ie_htcap, hc_cap));
1391170530Ssam	ni->ni_htparam = ie[__offsetof(struct ieee80211_ie_htcap, hc_param)];
1392170530Ssam}
1393170530Ssam
1394183254Ssamstatic void
1395183254Ssamhtinfo_parse(struct ieee80211_node *ni,
1396183254Ssam	const struct ieee80211_ie_htinfo *htinfo)
1397170530Ssam{
1398170530Ssam	uint16_t w;
1399170530Ssam
1400170530Ssam	ni->ni_htctlchan = htinfo->hi_ctrlchannel;
1401170530Ssam	ni->ni_ht2ndchan = SM(htinfo->hi_byte1, IEEE80211_HTINFO_2NDCHAN);
1402172055Ssam	w = LE_READ_2(&htinfo->hi_byte2);
1403170530Ssam	ni->ni_htopmode = SM(w, IEEE80211_HTINFO_OPMODE);
1404170530Ssam	w = LE_READ_2(&htinfo->hi_byte45);
1405170530Ssam	ni->ni_htstbc = SM(w, IEEE80211_HTINFO_BASIC_STBCMCS);
1406183254Ssam}
1407183254Ssam
1408183254Ssam/*
1409183254Ssam * Parse an 802.11n HT info ie and save useful information
1410183254Ssam * to the node state.  Note this does not effect any state
1411183254Ssam * changes such as for channel width change.
1412183254Ssam */
1413183254Ssamvoid
1414183254Ssamieee80211_parse_htinfo(struct ieee80211_node *ni, const uint8_t *ie)
1415183254Ssam{
1416183254Ssam	if (ie[0] == IEEE80211_ELEMID_VENDOR)
1417183254Ssam		ie += 4;
1418183254Ssam	htinfo_parse(ni, (const struct ieee80211_ie_htinfo *) ie);
1419183254Ssam}
1420183254Ssam
1421183254Ssam/*
1422183254Ssam * Handle 11n channel switch.  Use the received HT ie's to
1423183254Ssam * identify the right channel to use.  If we cannot locate it
1424183254Ssam * in the channel table then fallback to legacy operation.
1425183254Ssam * Note that we use this information to identify the node's
1426183254Ssam * channel only; the caller is responsible for insuring any
1427183254Ssam * required channel change is done (e.g. in sta mode when
1428183254Ssam * parsing the contents of a beacon frame).
1429183254Ssam */
1430183254Ssamstatic void
1431183254Ssamhtinfo_update_chw(struct ieee80211_node *ni, int htflags)
1432183254Ssam{
1433183254Ssam	struct ieee80211com *ic = ni->ni_ic;
1434183254Ssam	struct ieee80211_channel *c;
1435183254Ssam	int chanflags;
1436183254Ssam
1437173273Ssam	chanflags = (ni->ni_chan->ic_flags &~ IEEE80211_CHAN_HT) | htflags;
1438173273Ssam	if (chanflags != ni->ni_chan->ic_flags) {
1439183254Ssam		/* XXX not right for ht40- */
1440173273Ssam		c = ieee80211_find_channel(ic, ni->ni_chan->ic_freq, chanflags);
1441178354Ssam		if (c == NULL && (htflags & IEEE80211_CHAN_HT40)) {
1442173273Ssam			/*
1443173273Ssam			 * No HT40 channel entry in our table; fall back
1444173273Ssam			 * to HT20 operation.  This should not happen.
1445173273Ssam			 */
1446173273Ssam			c = findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT20);
1447183254Ssam#if 0
1448183254Ssam			IEEE80211_NOTE(ni->ni_vap,
1449173273Ssam			    IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
1450173273Ssam			    "no HT40 channel (freq %u), falling back to HT20",
1451173273Ssam			    ni->ni_chan->ic_freq);
1452183254Ssam#endif
1453173273Ssam			/* XXX stat */
1454173273Ssam		}
1455173273Ssam		if (c != NULL && c != ni->ni_chan) {
1456183254Ssam			IEEE80211_NOTE(ni->ni_vap,
1457173273Ssam			    IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
1458173273Ssam			    "switch station to HT%d channel %u/0x%x",
1459173273Ssam			    IEEE80211_IS_CHAN_HT40(c) ? 40 : 20,
1460173273Ssam			    c->ic_freq, c->ic_flags);
1461173273Ssam			ni->ni_chan = c;
1462173273Ssam		}
1463173273Ssam		/* NB: caller responsible for forcing any channel change */
1464173273Ssam	}
1465173273Ssam	/* update node's tx channel width */
1466173273Ssam	ni->ni_chw = IEEE80211_IS_CHAN_HT40(ni->ni_chan)? 40 : 20;
1467170530Ssam}
1468170530Ssam
1469170530Ssam/*
1470183255Ssam * Update 11n MIMO PS state according to received htcap.
1471183255Ssam */
1472183255Ssamstatic __inline int
1473183255Ssamhtcap_update_mimo_ps(struct ieee80211_node *ni)
1474183255Ssam{
1475183255Ssam	uint16_t oflags = ni->ni_flags;
1476183255Ssam
1477183255Ssam	switch (ni->ni_htcap & IEEE80211_HTCAP_SMPS) {
1478183255Ssam	case IEEE80211_HTCAP_SMPS_DYNAMIC:
1479183255Ssam		ni->ni_flags |= IEEE80211_NODE_MIMO_PS;
1480183255Ssam		ni->ni_flags |= IEEE80211_NODE_MIMO_RTS;
1481183255Ssam		break;
1482183255Ssam	case IEEE80211_HTCAP_SMPS_ENA:
1483183255Ssam		ni->ni_flags |= IEEE80211_NODE_MIMO_PS;
1484183255Ssam		ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS;
1485183255Ssam		break;
1486183255Ssam	case IEEE80211_HTCAP_SMPS_OFF:
1487183255Ssam	default:		/* disable on rx of reserved value */
1488183255Ssam		ni->ni_flags &= ~IEEE80211_NODE_MIMO_PS;
1489183255Ssam		ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS;
1490183255Ssam		break;
1491183255Ssam	}
1492183255Ssam	return (oflags ^ ni->ni_flags);
1493183255Ssam}
1494183255Ssam
1495183255Ssam/*
1496183257Ssam * Update short GI state according to received htcap
1497183257Ssam * and local settings.
1498183257Ssam */
1499183257Ssamstatic __inline void
1500183257Ssamhtcap_update_shortgi(struct ieee80211_node *ni)
1501183257Ssam{
1502183257Ssam	struct ieee80211vap *vap = ni->ni_vap;
1503183257Ssam
1504183257Ssam	ni->ni_flags &= ~(IEEE80211_NODE_SGI20|IEEE80211_NODE_SGI40);
1505183257Ssam	if ((ni->ni_htcap & IEEE80211_HTCAP_SHORTGI20) &&
1506193655Ssam	    (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20))
1507183257Ssam		ni->ni_flags |= IEEE80211_NODE_SGI20;
1508183257Ssam	if ((ni->ni_htcap & IEEE80211_HTCAP_SHORTGI40) &&
1509193655Ssam	    (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40))
1510183257Ssam		ni->ni_flags |= IEEE80211_NODE_SGI40;
1511183257Ssam}
1512183257Ssam
1513183257Ssam/*
1514183254Ssam * Parse and update HT-related state extracted from
1515183254Ssam * the HT cap and info ie's.
1516183254Ssam */
1517183254Ssamvoid
1518183254Ssamieee80211_ht_updateparams(struct ieee80211_node *ni,
1519183254Ssam	const uint8_t *htcapie, const uint8_t *htinfoie)
1520183254Ssam{
1521183254Ssam	struct ieee80211vap *vap = ni->ni_vap;
1522183254Ssam	const struct ieee80211_ie_htinfo *htinfo;
1523183254Ssam	int htflags;
1524183254Ssam
1525183254Ssam	ieee80211_parse_htcap(ni, htcapie);
1526183255Ssam	if (vap->iv_htcaps & IEEE80211_HTCAP_SMPS)
1527183255Ssam		htcap_update_mimo_ps(ni);
1528183257Ssam	htcap_update_shortgi(ni);
1529183254Ssam
1530183254Ssam	if (htinfoie[0] == IEEE80211_ELEMID_VENDOR)
1531183254Ssam		htinfoie += 4;
1532183254Ssam	htinfo = (const struct ieee80211_ie_htinfo *) htinfoie;
1533183254Ssam	htinfo_parse(ni, htinfo);
1534183254Ssam
1535193655Ssam	htflags = (vap->iv_flags_ht & IEEE80211_FHT_HT) ?
1536183254Ssam	    IEEE80211_CHAN_HT20 : 0;
1537183254Ssam	/* NB: honor operating mode constraint */
1538183254Ssam	if ((htinfo->hi_byte1 & IEEE80211_HTINFO_TXWIDTH_2040) &&
1539193655Ssam	    (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)) {
1540183254Ssam		if (ni->ni_ht2ndchan == IEEE80211_HTINFO_2NDCHAN_ABOVE)
1541183254Ssam			htflags = IEEE80211_CHAN_HT40U;
1542183254Ssam		else if (ni->ni_ht2ndchan == IEEE80211_HTINFO_2NDCHAN_BELOW)
1543183254Ssam			htflags = IEEE80211_CHAN_HT40D;
1544183254Ssam	}
1545183254Ssam	htinfo_update_chw(ni, htflags);
1546183256Ssam
1547183256Ssam	if ((htinfo->hi_byte1 & IEEE80211_HTINFO_RIFSMODE_PERM) &&
1548193655Ssam	    (vap->iv_flags_ht & IEEE80211_FHT_RIFS))
1549183256Ssam		ni->ni_flags |= IEEE80211_NODE_RIFS;
1550183256Ssam	else
1551183256Ssam		ni->ni_flags &= ~IEEE80211_NODE_RIFS;
1552183254Ssam}
1553183254Ssam
1554183254Ssam/*
1555183254Ssam * Parse and update HT-related state extracted from the HT cap ie
1556183254Ssam * for a station joining an HT BSS.
1557183254Ssam */
1558183254Ssamvoid
1559183254Ssamieee80211_ht_updatehtcap(struct ieee80211_node *ni, const uint8_t *htcapie)
1560183254Ssam{
1561183254Ssam	struct ieee80211vap *vap = ni->ni_vap;
1562183254Ssam	int htflags;
1563183254Ssam
1564183254Ssam	ieee80211_parse_htcap(ni, htcapie);
1565183255Ssam	if (vap->iv_htcaps & IEEE80211_HTCAP_SMPS)
1566183255Ssam		htcap_update_mimo_ps(ni);
1567183257Ssam	htcap_update_shortgi(ni);
1568183254Ssam
1569183254Ssam	/* NB: honor operating mode constraint */
1570219606Sbschmidt	/* XXX 40 MHz intolerant */
1571193655Ssam	htflags = (vap->iv_flags_ht & IEEE80211_FHT_HT) ?
1572183254Ssam	    IEEE80211_CHAN_HT20 : 0;
1573183254Ssam	if ((ni->ni_htcap & IEEE80211_HTCAP_CHWIDTH40) &&
1574193655Ssam	    (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)) {
1575183254Ssam		if (IEEE80211_IS_CHAN_HT40U(vap->iv_bss->ni_chan))
1576183254Ssam			htflags = IEEE80211_CHAN_HT40U;
1577183254Ssam		else if (IEEE80211_IS_CHAN_HT40D(vap->iv_bss->ni_chan))
1578183254Ssam			htflags = IEEE80211_CHAN_HT40D;
1579183254Ssam	}
1580183254Ssam	htinfo_update_chw(ni, htflags);
1581183254Ssam}
1582183254Ssam
1583183254Ssam/*
1584170530Ssam * Install received HT rate set by parsing the HT cap ie.
1585170530Ssam */
1586170530Ssamint
1587170530Ssamieee80211_setup_htrates(struct ieee80211_node *ni, const uint8_t *ie, int flags)
1588170530Ssam{
1589219602Sbschmidt	struct ieee80211com *ic = ni->ni_ic;
1590178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1591170530Ssam	const struct ieee80211_ie_htcap *htcap;
1592170530Ssam	struct ieee80211_htrateset *rs;
1593219602Sbschmidt	int i, maxequalmcs, maxunequalmcs;
1594170530Ssam
1595219602Sbschmidt	maxequalmcs = ic->ic_txstream * 8 - 1;
1596219602Sbschmidt	if (ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL) {
1597219602Sbschmidt		if (ic->ic_txstream >= 2)
1598219602Sbschmidt			maxunequalmcs = 38;
1599219602Sbschmidt		if (ic->ic_txstream >= 3)
1600219602Sbschmidt			maxunequalmcs = 52;
1601219602Sbschmidt		if (ic->ic_txstream >= 4)
1602219602Sbschmidt			maxunequalmcs = 76;
1603219602Sbschmidt	} else
1604219602Sbschmidt		maxunequalmcs = 0;
1605219602Sbschmidt
1606170530Ssam	rs = &ni->ni_htrates;
1607170530Ssam	memset(rs, 0, sizeof(*rs));
1608170530Ssam	if (ie != NULL) {
1609170530Ssam		if (ie[0] == IEEE80211_ELEMID_VENDOR)
1610170530Ssam			ie += 4;
1611170530Ssam		htcap = (const struct ieee80211_ie_htcap *) ie;
1612170530Ssam		for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++) {
1613170530Ssam			if (isclr(htcap->hc_mcsset, i))
1614170530Ssam				continue;
1615170530Ssam			if (rs->rs_nrates == IEEE80211_HTRATE_MAXSIZE) {
1616178354Ssam				IEEE80211_NOTE(vap,
1617170530Ssam				    IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
1618170530Ssam				    "WARNING, HT rate set too large; only "
1619170530Ssam				    "using %u rates", IEEE80211_HTRATE_MAXSIZE);
1620178354Ssam				vap->iv_stats.is_rx_rstoobig++;
1621170530Ssam				break;
1622170530Ssam			}
1623219602Sbschmidt			if (i <= 31 && i > maxequalmcs)
1624219602Sbschmidt				continue;
1625219602Sbschmidt			if (i == 32 &&
1626219602Sbschmidt			    (ic->ic_htcaps & IEEE80211_HTC_TXMCS32) == 0)
1627219602Sbschmidt				continue;
1628219602Sbschmidt			if (i > 32 && i > maxunequalmcs)
1629219602Sbschmidt				continue;
1630170530Ssam			rs->rs_rates[rs->rs_nrates++] = i;
1631170530Ssam		}
1632170530Ssam	}
1633170530Ssam	return ieee80211_fix_rate(ni, (struct ieee80211_rateset *) rs, flags);
1634170530Ssam}
1635170530Ssam
1636170530Ssam/*
1637170530Ssam * Mark rates in a node's HT rate set as basic according
1638170530Ssam * to the information in the supplied HT info ie.
1639170530Ssam */
1640170530Ssamvoid
1641170530Ssamieee80211_setup_basic_htrates(struct ieee80211_node *ni, const uint8_t *ie)
1642170530Ssam{
1643170530Ssam	const struct ieee80211_ie_htinfo *htinfo;
1644170530Ssam	struct ieee80211_htrateset *rs;
1645170530Ssam	int i, j;
1646170530Ssam
1647170530Ssam	if (ie[0] == IEEE80211_ELEMID_VENDOR)
1648170530Ssam		ie += 4;
1649170530Ssam	htinfo = (const struct ieee80211_ie_htinfo *) ie;
1650170530Ssam	rs = &ni->ni_htrates;
1651170530Ssam	if (rs->rs_nrates == 0) {
1652178354Ssam		IEEE80211_NOTE(ni->ni_vap,
1653170530Ssam		    IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
1654170530Ssam		    "%s", "WARNING, empty HT rate set");
1655170530Ssam		return;
1656170530Ssam	}
1657170530Ssam	for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++) {
1658170530Ssam		if (isclr(htinfo->hi_basicmcsset, i))
1659170530Ssam			continue;
1660170530Ssam		for (j = 0; j < rs->rs_nrates; j++)
1661170530Ssam			if ((rs->rs_rates[j] & IEEE80211_RATE_VAL) == i)
1662170530Ssam				rs->rs_rates[j] |= IEEE80211_RATE_BASIC;
1663170530Ssam	}
1664170530Ssam}
1665170530Ssam
1666170530Ssamstatic void
1667184280Ssamampdu_tx_setup(struct ieee80211_tx_ampdu *tap)
1668184280Ssam{
1669184280Ssam	callout_init(&tap->txa_timer, CALLOUT_MPSAFE);
1670184280Ssam	tap->txa_flags |= IEEE80211_AGGR_SETUP;
1671184280Ssam}
1672184280Ssam
1673184280Ssamstatic void
1674184280Ssamampdu_tx_stop(struct ieee80211_tx_ampdu *tap)
1675184280Ssam{
1676184280Ssam	struct ieee80211_node *ni = tap->txa_ni;
1677184280Ssam	struct ieee80211com *ic = ni->ni_ic;
1678184280Ssam
1679184280Ssam	KASSERT(tap->txa_flags & IEEE80211_AGGR_SETUP,
1680184280Ssam	    ("txa_flags 0x%x ac %d", tap->txa_flags, tap->txa_ac));
1681184280Ssam
1682184280Ssam	/*
1683184280Ssam	 * Stop BA stream if setup so driver has a chance
1684184280Ssam	 * to reclaim any resources it might have allocated.
1685184280Ssam	 */
1686184280Ssam	ic->ic_addba_stop(ni, tap);
1687184280Ssam	/*
1688184280Ssam	 * Stop any pending BAR transmit.
1689184280Ssam	 */
1690184280Ssam	bar_stop_timer(tap);
1691184280Ssam
1692184280Ssam	tap->txa_lastsample = 0;
1693184280Ssam	tap->txa_avgpps = 0;
1694184280Ssam	/* NB: clearing NAK means we may re-send ADDBA */
1695184280Ssam	tap->txa_flags &= ~(IEEE80211_AGGR_SETUP | IEEE80211_AGGR_NAK);
1696184280Ssam}
1697184280Ssam
1698223331Sadrian/*
1699223331Sadrian * ADDBA response timeout.
1700223331Sadrian *
1701223331Sadrian * If software aggregation and per-TID queue management was done here,
1702223331Sadrian * that queue would be unpaused after the ADDBA timeout occurs.
1703223331Sadrian */
1704184280Ssamstatic void
1705170530Ssamaddba_timeout(void *arg)
1706170530Ssam{
1707170530Ssam	struct ieee80211_tx_ampdu *tap = arg;
1708223331Sadrian	struct ieee80211_node *ni = tap->txa_ni;
1709223331Sadrian	struct ieee80211com *ic = ni->ni_ic;
1710170530Ssam
1711170530Ssam	/* XXX ? */
1712170530Ssam	tap->txa_flags &= ~IEEE80211_AGGR_XCHGPEND;
1713170530Ssam	tap->txa_attempts++;
1714223331Sadrian	ic->ic_addba_response_timeout(ni, tap);
1715170530Ssam}
1716170530Ssam
1717170530Ssamstatic void
1718170530Ssamaddba_start_timeout(struct ieee80211_tx_ampdu *tap)
1719170530Ssam{
1720170530Ssam	/* XXX use CALLOUT_PENDING instead? */
1721178354Ssam	callout_reset(&tap->txa_timer, ieee80211_addba_timeout,
1722170530Ssam	    addba_timeout, tap);
1723170530Ssam	tap->txa_flags |= IEEE80211_AGGR_XCHGPEND;
1724178354Ssam	tap->txa_nextrequest = ticks + ieee80211_addba_timeout;
1725170530Ssam}
1726170530Ssam
1727170530Ssamstatic void
1728170530Ssamaddba_stop_timeout(struct ieee80211_tx_ampdu *tap)
1729170530Ssam{
1730170530Ssam	/* XXX use CALLOUT_PENDING instead? */
1731170530Ssam	if (tap->txa_flags & IEEE80211_AGGR_XCHGPEND) {
1732170530Ssam		callout_stop(&tap->txa_timer);
1733170530Ssam		tap->txa_flags &= ~IEEE80211_AGGR_XCHGPEND;
1734170530Ssam	}
1735170530Ssam}
1736170530Ssam
1737223331Sadrianstatic void
1738223331Sadriannull_addba_response_timeout(struct ieee80211_node *ni,
1739223331Sadrian    struct ieee80211_tx_ampdu *tap)
1740223331Sadrian{
1741223331Sadrian}
1742223331Sadrian
1743170530Ssam/*
1744170530Ssam * Default method for requesting A-MPDU tx aggregation.
1745170530Ssam * We setup the specified state block and start a timer
1746170530Ssam * to wait for an ADDBA response frame.
1747170530Ssam */
1748170530Ssamstatic int
1749170530Ssamieee80211_addba_request(struct ieee80211_node *ni,
1750170530Ssam	struct ieee80211_tx_ampdu *tap,
1751170530Ssam	int dialogtoken, int baparamset, int batimeout)
1752170530Ssam{
1753170530Ssam	int bufsiz;
1754170530Ssam
1755170530Ssam	/* XXX locking */
1756170530Ssam	tap->txa_token = dialogtoken;
1757170530Ssam	tap->txa_flags |= IEEE80211_AGGR_IMMEDIATE;
1758170530Ssam	bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
1759170530Ssam	tap->txa_wnd = (bufsiz == 0) ?
1760170530Ssam	    IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
1761170530Ssam	addba_start_timeout(tap);
1762170530Ssam	return 1;
1763170530Ssam}
1764170530Ssam
1765170530Ssam/*
1766170530Ssam * Default method for processing an A-MPDU tx aggregation
1767170530Ssam * response.  We shutdown any pending timer and update the
1768170530Ssam * state block according to the reply.
1769170530Ssam */
1770170530Ssamstatic int
1771170530Ssamieee80211_addba_response(struct ieee80211_node *ni,
1772170530Ssam	struct ieee80211_tx_ampdu *tap,
1773170530Ssam	int status, int baparamset, int batimeout)
1774170530Ssam{
1775184280Ssam	int bufsiz, tid;
1776170530Ssam
1777170530Ssam	/* XXX locking */
1778170530Ssam	addba_stop_timeout(tap);
1779170530Ssam	if (status == IEEE80211_STATUS_SUCCESS) {
1780170530Ssam		bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
1781170530Ssam		/* XXX override our request? */
1782170530Ssam		tap->txa_wnd = (bufsiz == 0) ?
1783170530Ssam		    IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
1784184280Ssam		/* XXX AC/TID */
1785184280Ssam		tid = MS(baparamset, IEEE80211_BAPS_TID);
1786170530Ssam		tap->txa_flags |= IEEE80211_AGGR_RUNNING;
1787184280Ssam		tap->txa_attempts = 0;
1788173273Ssam	} else {
1789173273Ssam		/* mark tid so we don't try again */
1790173273Ssam		tap->txa_flags |= IEEE80211_AGGR_NAK;
1791170530Ssam	}
1792170530Ssam	return 1;
1793170530Ssam}
1794170530Ssam
1795170530Ssam/*
1796170530Ssam * Default method for stopping A-MPDU tx aggregation.
1797170530Ssam * Any timer is cleared and we drain any pending frames.
1798170530Ssam */
1799170530Ssamstatic void
1800170530Ssamieee80211_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
1801170530Ssam{
1802170530Ssam	/* XXX locking */
1803170530Ssam	addba_stop_timeout(tap);
1804170530Ssam	if (tap->txa_flags & IEEE80211_AGGR_RUNNING) {
1805182830Ssam		/* XXX clear aggregation queue */
1806170530Ssam		tap->txa_flags &= ~IEEE80211_AGGR_RUNNING;
1807170530Ssam	}
1808170530Ssam	tap->txa_attempts = 0;
1809170530Ssam}
1810170530Ssam
1811170530Ssam/*
1812170530Ssam * Process a received action frame using the default aggregation
1813170530Ssam * policy.  We intercept ADDBA-related frames and use them to
1814170530Ssam * update our aggregation state.  All other frames are passed up
1815170530Ssam * for processing by ieee80211_recv_action.
1816170530Ssam */
1817195377Ssamstatic int
1818195377Ssamht_recv_action_ba_addba_request(struct ieee80211_node *ni,
1819195377Ssam	const struct ieee80211_frame *wh,
1820170530Ssam	const uint8_t *frm, const uint8_t *efrm)
1821170530Ssam{
1822170530Ssam	struct ieee80211com *ic = ni->ni_ic;
1823178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1824170530Ssam	struct ieee80211_rx_ampdu *rap;
1825195377Ssam	uint8_t dialogtoken;
1826195377Ssam	uint16_t baparamset, batimeout, baseqctl;
1827205277Srpaulo	uint16_t args[5];
1828195377Ssam	int tid;
1829170530Ssam
1830195377Ssam	dialogtoken = frm[2];
1831195377Ssam	baparamset = LE_READ_2(frm+3);
1832195377Ssam	batimeout = LE_READ_2(frm+5);
1833195377Ssam	baseqctl = LE_READ_2(frm+7);
1834170530Ssam
1835195377Ssam	tid = MS(baparamset, IEEE80211_BAPS_TID);
1836170530Ssam
1837195377Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1838195377Ssam	    "recv ADDBA request: dialogtoken %u baparamset 0x%x "
1839195377Ssam	    "(tid %d bufsiz %d) batimeout %d baseqctl %d:%d",
1840195377Ssam	    dialogtoken, baparamset,
1841195377Ssam	    tid, MS(baparamset, IEEE80211_BAPS_BUFSIZ),
1842195377Ssam	    batimeout,
1843195377Ssam	    MS(baseqctl, IEEE80211_BASEQ_START),
1844195377Ssam	    MS(baseqctl, IEEE80211_BASEQ_FRAG));
1845170530Ssam
1846195377Ssam	rap = &ni->ni_rx_ampdu[tid];
1847170530Ssam
1848195377Ssam	/* Send ADDBA response */
1849195377Ssam	args[0] = dialogtoken;
1850195377Ssam	/*
1851195377Ssam	 * NB: We ack only if the sta associated with HT and
1852195377Ssam	 * the ap is configured to do AMPDU rx (the latter
1853195377Ssam	 * violates the 11n spec and is mostly for testing).
1854195377Ssam	 */
1855195377Ssam	if ((ni->ni_flags & IEEE80211_NODE_AMPDU_RX) &&
1856195377Ssam	    (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_RX)) {
1857195377Ssam		/* XXX handle ampdu_rx_start failure */
1858195377Ssam		ic->ic_ampdu_rx_start(ni, rap,
1859195377Ssam		    baparamset, batimeout, baseqctl);
1860170530Ssam
1861195377Ssam		args[1] = IEEE80211_STATUS_SUCCESS;
1862195377Ssam	} else {
1863195377Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1864195377Ssam		    ni, "reject ADDBA request: %s",
1865195377Ssam		    ni->ni_flags & IEEE80211_NODE_AMPDU_RX ?
1866195377Ssam		       "administratively disabled" :
1867195377Ssam		       "not negotiated for station");
1868195377Ssam		vap->iv_stats.is_addba_reject++;
1869195377Ssam		args[1] = IEEE80211_STATUS_UNSPECIFIED;
1870195377Ssam	}
1871195377Ssam	/* XXX honor rap flags? */
1872195377Ssam	args[2] = IEEE80211_BAPS_POLICY_IMMEDIATE
1873195377Ssam		| SM(tid, IEEE80211_BAPS_TID)
1874195377Ssam		| SM(rap->rxa_wnd, IEEE80211_BAPS_BUFSIZ)
1875195377Ssam		;
1876195377Ssam	args[3] = 0;
1877205277Srpaulo	args[4] = 0;
1878195377Ssam	ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
1879195377Ssam		IEEE80211_ACTION_BA_ADDBA_RESPONSE, args);
1880195377Ssam	return 0;
1881195377Ssam}
1882170530Ssam
1883195377Ssamstatic int
1884195377Ssamht_recv_action_ba_addba_response(struct ieee80211_node *ni,
1885195377Ssam	const struct ieee80211_frame *wh,
1886195377Ssam	const uint8_t *frm, const uint8_t *efrm)
1887195377Ssam{
1888195377Ssam	struct ieee80211com *ic = ni->ni_ic;
1889195377Ssam	struct ieee80211vap *vap = ni->ni_vap;
1890195377Ssam	struct ieee80211_tx_ampdu *tap;
1891195377Ssam	uint8_t dialogtoken, policy;
1892195377Ssam	uint16_t baparamset, batimeout, code;
1893195377Ssam	int tid, ac, bufsiz;
1894170530Ssam
1895195377Ssam	dialogtoken = frm[2];
1896195377Ssam	code = LE_READ_2(frm+3);
1897195377Ssam	baparamset = LE_READ_2(frm+5);
1898195377Ssam	tid = MS(baparamset, IEEE80211_BAPS_TID);
1899195377Ssam	bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
1900195377Ssam	policy = MS(baparamset, IEEE80211_BAPS_POLICY);
1901195377Ssam	batimeout = LE_READ_2(frm+7);
1902195377Ssam
1903195377Ssam	ac = TID_TO_WME_AC(tid);
1904195377Ssam	tap = &ni->ni_tx_ampdu[ac];
1905195377Ssam	if ((tap->txa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
1906195377Ssam		IEEE80211_DISCARD_MAC(vap,
1907195377Ssam		    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1908195377Ssam		    ni->ni_macaddr, "ADDBA response",
1909195377Ssam		    "no pending ADDBA, tid %d dialogtoken %u "
1910195377Ssam		    "code %d", tid, dialogtoken, code);
1911195377Ssam		vap->iv_stats.is_addba_norequest++;
1912195377Ssam		return 0;
1913195377Ssam	}
1914195377Ssam	if (dialogtoken != tap->txa_token) {
1915195377Ssam		IEEE80211_DISCARD_MAC(vap,
1916195377Ssam		    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1917195377Ssam		    ni->ni_macaddr, "ADDBA response",
1918195377Ssam		    "dialogtoken mismatch: waiting for %d, "
1919195377Ssam		    "received %d, tid %d code %d",
1920195377Ssam		    tap->txa_token, dialogtoken, tid, code);
1921195377Ssam		vap->iv_stats.is_addba_badtoken++;
1922195377Ssam		return 0;
1923195377Ssam	}
1924195377Ssam	/* NB: assumes IEEE80211_AGGR_IMMEDIATE is 1 */
1925195377Ssam	if (policy != (tap->txa_flags & IEEE80211_AGGR_IMMEDIATE)) {
1926195377Ssam		IEEE80211_DISCARD_MAC(vap,
1927195377Ssam		    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1928195377Ssam		    ni->ni_macaddr, "ADDBA response",
1929195377Ssam		    "policy mismatch: expecting %s, "
1930195377Ssam		    "received %s, tid %d code %d",
1931195377Ssam		    tap->txa_flags & IEEE80211_AGGR_IMMEDIATE,
1932195377Ssam		    policy, tid, code);
1933195377Ssam		vap->iv_stats.is_addba_badpolicy++;
1934195377Ssam		return 0;
1935195377Ssam	}
1936182829Ssam#if 0
1937195377Ssam	/* XXX we take MIN in ieee80211_addba_response */
1938195377Ssam	if (bufsiz > IEEE80211_AGGR_BAWMAX) {
1939195377Ssam		IEEE80211_DISCARD_MAC(vap,
1940195377Ssam		    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1941195377Ssam		    ni->ni_macaddr, "ADDBA response",
1942195377Ssam		    "BA window too large: max %d, "
1943195377Ssam		    "received %d, tid %d code %d",
1944195377Ssam		    bufsiz, IEEE80211_AGGR_BAWMAX, tid, code);
1945195377Ssam		vap->iv_stats.is_addba_badbawinsize++;
1946195377Ssam		return 0;
1947195377Ssam	}
1948182829Ssam#endif
1949195377Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1950195377Ssam	    "recv ADDBA response: dialogtoken %u code %d "
1951195377Ssam	    "baparamset 0x%x (tid %d bufsiz %d) batimeout %d",
1952195377Ssam	    dialogtoken, code, baparamset, tid, bufsiz,
1953195377Ssam	    batimeout);
1954195377Ssam	ic->ic_addba_response(ni, tap, code, baparamset, batimeout);
1955195377Ssam	return 0;
1956195377Ssam}
1957170530Ssam
1958195377Ssamstatic int
1959195377Ssamht_recv_action_ba_delba(struct ieee80211_node *ni,
1960195377Ssam	const struct ieee80211_frame *wh,
1961195377Ssam	const uint8_t *frm, const uint8_t *efrm)
1962195377Ssam{
1963195377Ssam	struct ieee80211com *ic = ni->ni_ic;
1964195377Ssam	struct ieee80211_rx_ampdu *rap;
1965195377Ssam	struct ieee80211_tx_ampdu *tap;
1966195377Ssam	uint16_t baparamset, code;
1967195377Ssam	int tid, ac;
1968170530Ssam
1969195377Ssam	baparamset = LE_READ_2(frm+2);
1970195377Ssam	code = LE_READ_2(frm+4);
1971170530Ssam
1972195377Ssam	tid = MS(baparamset, IEEE80211_DELBAPS_TID);
1973170530Ssam
1974195377Ssam	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1975195377Ssam	    "recv DELBA: baparamset 0x%x (tid %d initiator %d) "
1976195377Ssam	    "code %d", baparamset, tid,
1977195377Ssam	    MS(baparamset, IEEE80211_DELBAPS_INIT), code);
1978195377Ssam
1979195377Ssam	if ((baparamset & IEEE80211_DELBAPS_INIT) == 0) {
1980195377Ssam		ac = TID_TO_WME_AC(tid);
1981195377Ssam		tap = &ni->ni_tx_ampdu[ac];
1982195377Ssam		ic->ic_addba_stop(ni, tap);
1983195377Ssam	} else {
1984195377Ssam		rap = &ni->ni_rx_ampdu[tid];
1985195377Ssam		ic->ic_ampdu_rx_stop(ni, rap);
1986170530Ssam	}
1987195377Ssam	return 0;
1988170530Ssam}
1989170530Ssam
1990195377Ssamstatic int
1991195377Ssamht_recv_action_ht_txchwidth(struct ieee80211_node *ni,
1992195377Ssam	const struct ieee80211_frame *wh,
1993170530Ssam	const uint8_t *frm, const uint8_t *efrm)
1994170530Ssam{
1995170530Ssam	int chw;
1996170530Ssam
1997195377Ssam	chw = (frm[2] == IEEE80211_A_HT_TXCHWIDTH_2040) ? 40 : 20;
1998195377Ssam
1999195377Ssam	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2000195377Ssam	    "%s: HT txchwidth, width %d%s",
2001195377Ssam	    __func__, chw, ni->ni_chw != chw ? "*" : "");
2002195377Ssam	if (chw != ni->ni_chw) {
2003195377Ssam		ni->ni_chw = chw;
2004195377Ssam		/* XXX notify on change */
2005170530Ssam	}
2006195377Ssam	return 0;
2007170530Ssam}
2008170530Ssam
2009195377Ssamstatic int
2010195377Ssamht_recv_action_ht_mimopwrsave(struct ieee80211_node *ni,
2011195377Ssam	const struct ieee80211_frame *wh,
2012195377Ssam	const uint8_t *frm, const uint8_t *efrm)
2013195377Ssam{
2014195377Ssam	const struct ieee80211_action_ht_mimopowersave *mps =
2015195377Ssam	    (const struct ieee80211_action_ht_mimopowersave *) frm;
2016195377Ssam
2017195377Ssam	/* XXX check iv_htcaps */
2018195377Ssam	if (mps->am_control & IEEE80211_A_HT_MIMOPWRSAVE_ENA)
2019195377Ssam		ni->ni_flags |= IEEE80211_NODE_MIMO_PS;
2020195377Ssam	else
2021195377Ssam		ni->ni_flags &= ~IEEE80211_NODE_MIMO_PS;
2022195377Ssam	if (mps->am_control & IEEE80211_A_HT_MIMOPWRSAVE_MODE)
2023195377Ssam		ni->ni_flags |= IEEE80211_NODE_MIMO_RTS;
2024195377Ssam	else
2025195377Ssam		ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS;
2026195377Ssam	/* XXX notify on change */
2027195377Ssam	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2028195377Ssam	    "%s: HT MIMO PS (%s%s)", __func__,
2029195377Ssam	    (ni->ni_flags & IEEE80211_NODE_MIMO_PS) ?  "on" : "off",
2030195377Ssam	    (ni->ni_flags & IEEE80211_NODE_MIMO_RTS) ?  "+rts" : ""
2031195377Ssam	);
2032195377Ssam	return 0;
2033195377Ssam}
2034195377Ssam
2035170530Ssam/*
2036170530Ssam * Transmit processing.
2037170530Ssam */
2038170530Ssam
2039170530Ssam/*
2040178354Ssam * Check if A-MPDU should be requested/enabled for a stream.
2041178354Ssam * We require a traffic rate above a per-AC threshold and we
2042178354Ssam * also handle backoff from previous failed attempts.
2043178354Ssam *
2044178354Ssam * Drivers may override this method to bring in information
2045178354Ssam * such as link state conditions in making the decision.
2046178354Ssam */
2047178354Ssamstatic int
2048178354Ssamieee80211_ampdu_enable(struct ieee80211_node *ni,
2049178354Ssam	struct ieee80211_tx_ampdu *tap)
2050178354Ssam{
2051178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2052178354Ssam
2053178354Ssam	if (tap->txa_avgpps < vap->iv_ampdu_mintraffic[tap->txa_ac])
2054178354Ssam		return 0;
2055178354Ssam	/* XXX check rssi? */
2056178354Ssam	if (tap->txa_attempts >= ieee80211_addba_maxtries &&
2057178354Ssam	    ticks < tap->txa_nextrequest) {
2058178354Ssam		/*
2059178354Ssam		 * Don't retry too often; txa_nextrequest is set
2060178354Ssam		 * to the minimum interval we'll retry after
2061178354Ssam		 * ieee80211_addba_maxtries failed attempts are made.
2062178354Ssam		 */
2063178354Ssam		return 0;
2064178354Ssam	}
2065178354Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
2066183249Ssam	    "enable AMPDU on %s, avgpps %d pkts %d",
2067183249Ssam	    ieee80211_wme_acnames[tap->txa_ac], tap->txa_avgpps, tap->txa_pkts);
2068178354Ssam	return 1;
2069178354Ssam}
2070178354Ssam
2071178354Ssam/*
2072170530Ssam * Request A-MPDU tx aggregation.  Setup local state and
2073170530Ssam * issue an ADDBA request.  BA use will only happen after
2074170530Ssam * the other end replies with ADDBA response.
2075170530Ssam */
2076170530Ssamint
2077170530Ssamieee80211_ampdu_request(struct ieee80211_node *ni,
2078170530Ssam	struct ieee80211_tx_ampdu *tap)
2079170530Ssam{
2080170530Ssam	struct ieee80211com *ic = ni->ni_ic;
2081205277Srpaulo	uint16_t args[5];
2082170530Ssam	int tid, dialogtoken;
2083170530Ssam	static int tokens = 0;	/* XXX */
2084170530Ssam
2085170530Ssam	/* XXX locking */
2086170530Ssam	if ((tap->txa_flags & IEEE80211_AGGR_SETUP) == 0) {
2087170530Ssam		/* do deferred setup of state */
2088184280Ssam		ampdu_tx_setup(tap);
2089170530Ssam	}
2090173273Ssam	/* XXX hack for not doing proper locking */
2091173273Ssam	tap->txa_flags &= ~IEEE80211_AGGR_NAK;
2092173273Ssam
2093170530Ssam	dialogtoken = (tokens+1) % 63;		/* XXX */
2094183245Ssam	tid = WME_AC_TO_TID(tap->txa_ac);
2095183245Ssam	tap->txa_start = ni->ni_txseqs[tid];
2096170530Ssam
2097170530Ssam	args[0] = dialogtoken;
2098205277Srpaulo	args[1] = 0;	/* NB: status code not used */
2099205277Srpaulo	args[2]	= IEEE80211_BAPS_POLICY_IMMEDIATE
2100170530Ssam		| SM(tid, IEEE80211_BAPS_TID)
2101170530Ssam		| SM(IEEE80211_AGGR_BAWMAX, IEEE80211_BAPS_BUFSIZ)
2102170530Ssam		;
2103205277Srpaulo	args[3] = 0;	/* batimeout */
2104170530Ssam	/* NB: do first so there's no race against reply */
2105205277Srpaulo	if (!ic->ic_addba_request(ni, tap, dialogtoken, args[2], args[3])) {
2106170530Ssam		/* unable to setup state, don't make request */
2107178354Ssam		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2108173273Ssam		    ni, "%s: could not setup BA stream for AC %d",
2109173273Ssam		    __func__, tap->txa_ac);
2110173273Ssam		/* defer next try so we don't slam the driver with requests */
2111178354Ssam		tap->txa_attempts = ieee80211_addba_maxtries;
2112178354Ssam		/* NB: check in case driver wants to override */
2113178354Ssam		if (tap->txa_nextrequest <= ticks)
2114178354Ssam			tap->txa_nextrequest = ticks + ieee80211_addba_backoff;
2115170530Ssam		return 0;
2116170530Ssam	}
2117170530Ssam	tokens = dialogtoken;			/* allocate token */
2118183245Ssam	/* NB: after calling ic_addba_request so driver can set txa_start */
2119205277Srpaulo	args[4] = SM(tap->txa_start, IEEE80211_BASEQ_START)
2120178953Ssam		| SM(0, IEEE80211_BASEQ_FRAG)
2121178953Ssam		;
2122170530Ssam	return ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
2123170530Ssam		IEEE80211_ACTION_BA_ADDBA_REQUEST, args);
2124170530Ssam}
2125170530Ssam
2126170530Ssam/*
2127173273Ssam * Terminate an AMPDU tx stream.  State is reclaimed
2128173273Ssam * and the peer notified with a DelBA Action frame.
2129173273Ssam */
2130173273Ssamvoid
2131183250Ssamieee80211_ampdu_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
2132183250Ssam	int reason)
2133173273Ssam{
2134173273Ssam	struct ieee80211com *ic = ni->ni_ic;
2135178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2136173273Ssam	uint16_t args[4];
2137173273Ssam
2138173273Ssam	/* XXX locking */
2139184280Ssam	tap->txa_flags &= ~IEEE80211_AGGR_BARPEND;
2140173273Ssam	if (IEEE80211_AMPDU_RUNNING(tap)) {
2141178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2142183250Ssam		    ni, "%s: stop BA stream for AC %d (reason %d)",
2143183250Ssam		    __func__, tap->txa_ac, reason);
2144178354Ssam		vap->iv_stats.is_ampdu_stop++;
2145173273Ssam
2146173273Ssam		ic->ic_addba_stop(ni, tap);
2147173273Ssam		args[0] = WME_AC_TO_TID(tap->txa_ac);
2148173273Ssam		args[1] = IEEE80211_DELBAPS_INIT;
2149183250Ssam		args[2] = reason;			/* XXX reason code */
2150195377Ssam		ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
2151173273Ssam			IEEE80211_ACTION_BA_DELBA, args);
2152173273Ssam	} else {
2153178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2154183250Ssam		    ni, "%s: BA stream for AC %d not running (reason %d)",
2155183250Ssam		    __func__, tap->txa_ac, reason);
2156178354Ssam		vap->iv_stats.is_ampdu_stop_failed++;
2157173273Ssam	}
2158173273Ssam}
2159173273Ssam
2160184280Ssamstatic void
2161184280Ssambar_timeout(void *arg)
2162184280Ssam{
2163184280Ssam	struct ieee80211_tx_ampdu *tap = arg;
2164184280Ssam	struct ieee80211_node *ni = tap->txa_ni;
2165184280Ssam
2166184280Ssam	KASSERT((tap->txa_flags & IEEE80211_AGGR_XCHGPEND) == 0,
2167184280Ssam	    ("bar/addba collision, flags 0x%x", tap->txa_flags));
2168184280Ssam
2169184280Ssam	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2170184280Ssam	    ni, "%s: tid %u flags 0x%x attempts %d", __func__,
2171184280Ssam	    tap->txa_ac, tap->txa_flags, tap->txa_attempts);
2172184280Ssam
2173184280Ssam	/* guard against race with bar_tx_complete */
2174184280Ssam	if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) == 0)
2175184280Ssam		return;
2176184280Ssam	/* XXX ? */
2177184280Ssam	if (tap->txa_attempts >= ieee80211_bar_maxtries)
2178184280Ssam		ieee80211_ampdu_stop(ni, tap, IEEE80211_REASON_TIMEOUT);
2179184280Ssam	else
2180184280Ssam		ieee80211_send_bar(ni, tap, tap->txa_seqpending);
2181184280Ssam}
2182184280Ssam
2183184280Ssamstatic void
2184184280Ssambar_start_timer(struct ieee80211_tx_ampdu *tap)
2185184280Ssam{
2186184280Ssam	callout_reset(&tap->txa_timer, ieee80211_bar_timeout, bar_timeout, tap);
2187184280Ssam}
2188184280Ssam
2189184280Ssamstatic void
2190184280Ssambar_stop_timer(struct ieee80211_tx_ampdu *tap)
2191184280Ssam{
2192184280Ssam	callout_stop(&tap->txa_timer);
2193184280Ssam}
2194184280Ssam
2195184280Ssamstatic void
2196184280Ssambar_tx_complete(struct ieee80211_node *ni, void *arg, int status)
2197184280Ssam{
2198184280Ssam	struct ieee80211_tx_ampdu *tap = arg;
2199184280Ssam
2200184280Ssam	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2201184280Ssam	    ni, "%s: tid %u flags 0x%x pending %d status %d",
2202184280Ssam	    __func__, tap->txa_ac, tap->txa_flags,
2203184280Ssam	    callout_pending(&tap->txa_timer), status);
2204184280Ssam
2205184280Ssam	/* XXX locking */
2206184280Ssam	if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) &&
2207184280Ssam	    callout_pending(&tap->txa_timer)) {
2208184280Ssam		struct ieee80211com *ic = ni->ni_ic;
2209184280Ssam
2210224907Sadrian		if (status == 0)		/* ACK'd */
2211184280Ssam			bar_stop_timer(tap);
2212184280Ssam		ic->ic_bar_response(ni, tap, status);
2213184280Ssam		/* NB: just let timer expire so we pace requests */
2214184280Ssam	}
2215184280Ssam}
2216184280Ssam
2217184280Ssamstatic void
2218184280Ssamieee80211_bar_response(struct ieee80211_node *ni,
2219184280Ssam	struct ieee80211_tx_ampdu *tap, int status)
2220184280Ssam{
2221184280Ssam
2222224907Sadrian	if (status == 0) {		/* got ACK */
2223184280Ssam		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2224184280Ssam		    ni, "BAR moves BA win <%u:%u> (%u frames) txseq %u tid %u",
2225184280Ssam		    tap->txa_start,
2226184280Ssam		    IEEE80211_SEQ_ADD(tap->txa_start, tap->txa_wnd-1),
2227184280Ssam		    tap->txa_qframes, tap->txa_seqpending,
2228184280Ssam		    WME_AC_TO_TID(tap->txa_ac));
2229184280Ssam
2230184280Ssam		/* NB: timer already stopped in bar_tx_complete */
2231184280Ssam		tap->txa_start = tap->txa_seqpending;
2232184280Ssam		tap->txa_flags &= ~IEEE80211_AGGR_BARPEND;
2233184280Ssam	}
2234184280Ssam}
2235184280Ssam
2236173273Ssam/*
2237170530Ssam * Transmit a BAR frame to the specified node.  The
2238170530Ssam * BAR contents are drawn from the supplied aggregation
2239170530Ssam * state associated with the node.
2240184280Ssam *
2241184280Ssam * NB: we only handle immediate ACK w/ compressed bitmap.
2242170530Ssam */
2243170530Ssamint
2244170530Ssamieee80211_send_bar(struct ieee80211_node *ni,
2245184280Ssam	struct ieee80211_tx_ampdu *tap, ieee80211_seq seq)
2246170530Ssam{
2247178354Ssam#define	senderr(_x, _v)	do { vap->iv_stats._v++; ret = _x; goto bad; } while (0)
2248178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2249170530Ssam	struct ieee80211com *ic = ni->ni_ic;
2250184280Ssam	struct ieee80211_frame_bar *bar;
2251170530Ssam	struct mbuf *m;
2252184280Ssam	uint16_t barctl, barseqctl;
2253170530Ssam	uint8_t *frm;
2254170530Ssam	int tid, ret;
2255170530Ssam
2256184280Ssam	if ((tap->txa_flags & IEEE80211_AGGR_RUNNING) == 0) {
2257184280Ssam		/* no ADDBA response, should not happen */
2258184280Ssam		/* XXX stat+msg */
2259184280Ssam		return EINVAL;
2260184280Ssam	}
2261184280Ssam	/* XXX locking */
2262184280Ssam	bar_stop_timer(tap);
2263184280Ssam
2264170530Ssam	ieee80211_ref_node(ni);
2265170530Ssam
2266184280Ssam	m = ieee80211_getmgtframe(&frm, ic->ic_headroom, sizeof(*bar));
2267170530Ssam	if (m == NULL)
2268170530Ssam		senderr(ENOMEM, is_tx_nobuf);
2269170530Ssam
2270184280Ssam	if (!ieee80211_add_callback(m, bar_tx_complete, tap)) {
2271184280Ssam		m_freem(m);
2272184280Ssam		senderr(ENOMEM, is_tx_nobuf);	/* XXX */
2273184280Ssam		/* NOTREACHED */
2274184280Ssam	}
2275184280Ssam
2276184280Ssam	bar = mtod(m, struct ieee80211_frame_bar *);
2277184280Ssam	bar->i_fc[0] = IEEE80211_FC0_VERSION_0 |
2278170530Ssam		IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_BAR;
2279184280Ssam	bar->i_fc[1] = 0;
2280184280Ssam	IEEE80211_ADDR_COPY(bar->i_ra, ni->ni_macaddr);
2281184280Ssam	IEEE80211_ADDR_COPY(bar->i_ta, vap->iv_myaddr);
2282170530Ssam
2283170530Ssam	tid = WME_AC_TO_TID(tap->txa_ac);
2284170530Ssam	barctl 	= (tap->txa_flags & IEEE80211_AGGR_IMMEDIATE ?
2285184280Ssam			0 : IEEE80211_BAR_NOACK)
2286184280Ssam		| IEEE80211_BAR_COMP
2287184280Ssam		| SM(tid, IEEE80211_BAR_TID)
2288170530Ssam		;
2289184280Ssam	barseqctl = SM(seq, IEEE80211_BAR_SEQ_START);
2290184280Ssam	/* NB: known to have proper alignment */
2291184280Ssam	bar->i_ctl = htole16(barctl);
2292184280Ssam	bar->i_seq = htole16(barseqctl);
2293184280Ssam	m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_bar);
2294170530Ssam
2295178354Ssam	M_WME_SETAC(m, WME_AC_VO);
2296178354Ssam
2297170530Ssam	IEEE80211_NODE_STAT(ni, tx_mgmt);	/* XXX tx_ctl? */
2298170530Ssam
2299184280Ssam	/* XXX locking */
2300184280Ssam	/* init/bump attempts counter */
2301184280Ssam	if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) == 0)
2302184280Ssam		tap->txa_attempts = 1;
2303184280Ssam	else
2304184280Ssam		tap->txa_attempts++;
2305184280Ssam	tap->txa_seqpending = seq;
2306184280Ssam	tap->txa_flags |= IEEE80211_AGGR_BARPEND;
2307170530Ssam
2308184280Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_11N,
2309184280Ssam	    ni, "send BAR: tid %u ctl 0x%x start %u (attempt %d)",
2310184280Ssam	    tid, barctl, seq, tap->txa_attempts);
2311184280Ssam
2312225013Sadrian	/*
2313225013Sadrian	 * ic_raw_xmit will free the node reference
2314225013Sadrian	 * regardless of queue/TX success or failure.
2315225013Sadrian	 */
2316184280Ssam	ret = ic->ic_raw_xmit(ni, m, NULL);
2317184280Ssam	if (ret != 0) {
2318184280Ssam		/* xmit failed, clear state flag */
2319184280Ssam		tap->txa_flags &= ~IEEE80211_AGGR_BARPEND;
2320225013Sadrian		return ret;
2321184280Ssam	}
2322184280Ssam	/* XXX hack against tx complete happening before timer is started */
2323184280Ssam	if (tap->txa_flags & IEEE80211_AGGR_BARPEND)
2324184280Ssam		bar_start_timer(tap);
2325184280Ssam	return 0;
2326170530Ssambad:
2327170530Ssam	ieee80211_free_node(ni);
2328170530Ssam	return ret;
2329170530Ssam#undef senderr
2330170530Ssam}
2331170530Ssam
2332195377Ssamstatic int
2333195377Ssamht_action_output(struct ieee80211_node *ni, struct mbuf *m)
2334195377Ssam{
2335195377Ssam	struct ieee80211_bpf_params params;
2336195377Ssam
2337195377Ssam	memset(&params, 0, sizeof(params));
2338195377Ssam	params.ibp_pri = WME_AC_VO;
2339195377Ssam	params.ibp_rate0 = ni->ni_txparms->mgmtrate;
2340195377Ssam	/* NB: we know all frames are unicast */
2341195377Ssam	params.ibp_try0 = ni->ni_txparms->maxretry;
2342195377Ssam	params.ibp_power = ni->ni_txpower;
2343195377Ssam	return ieee80211_mgmt_output(ni, m, IEEE80211_FC0_SUBTYPE_ACTION,
2344195377Ssam	     &params);
2345195377Ssam}
2346195377Ssam
2347195377Ssam#define	ADDSHORT(frm, v) do {			\
2348195377Ssam	frm[0] = (v) & 0xff;			\
2349195377Ssam	frm[1] = (v) >> 8;			\
2350195377Ssam	frm += 2;				\
2351195377Ssam} while (0)
2352195377Ssam
2353170530Ssam/*
2354170530Ssam * Send an action management frame.  The arguments are stuff
2355170530Ssam * into a frame without inspection; the caller is assumed to
2356170530Ssam * prepare them carefully (e.g. based on the aggregation state).
2357170530Ssam */
2358195377Ssamstatic int
2359195377Ssamht_send_action_ba_addba(struct ieee80211_node *ni,
2360195377Ssam	int category, int action, void *arg0)
2361170530Ssam{
2362178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2363170530Ssam	struct ieee80211com *ic = ni->ni_ic;
2364195377Ssam	uint16_t *args = arg0;
2365170530Ssam	struct mbuf *m;
2366170530Ssam	uint8_t *frm;
2367195377Ssam
2368195377Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2369205277Srpaulo	    "send ADDBA %s: dialogtoken %d status %d "
2370195377Ssam	    "baparamset 0x%x (tid %d) batimeout 0x%x baseqctl 0x%x",
2371195377Ssam	    (action == IEEE80211_ACTION_BA_ADDBA_REQUEST) ?
2372195377Ssam		"request" : "response",
2373205277Srpaulo	    args[0], args[1], args[2], MS(args[2], IEEE80211_BAPS_TID),
2374205277Srpaulo	    args[3], args[4]);
2375195377Ssam
2376195377Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2377195377Ssam	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2378195377Ssam	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2379195377Ssam	ieee80211_ref_node(ni);
2380195377Ssam
2381195377Ssam	m = ieee80211_getmgtframe(&frm,
2382195377Ssam	    ic->ic_headroom + sizeof(struct ieee80211_frame),
2383195377Ssam	    sizeof(uint16_t)	/* action+category */
2384195377Ssam	    /* XXX may action payload */
2385195377Ssam	    + sizeof(struct ieee80211_action_ba_addbaresponse)
2386195377Ssam	);
2387195377Ssam	if (m != NULL) {
2388195377Ssam		*frm++ = category;
2389195377Ssam		*frm++ = action;
2390195377Ssam		*frm++ = args[0];		/* dialog token */
2391205277Srpaulo		if (action == IEEE80211_ACTION_BA_ADDBA_RESPONSE)
2392205277Srpaulo			ADDSHORT(frm, args[1]);	/* status code */
2393205277Srpaulo		ADDSHORT(frm, args[2]);		/* baparamset */
2394205277Srpaulo		ADDSHORT(frm, args[3]);		/* batimeout */
2395195377Ssam		if (action == IEEE80211_ACTION_BA_ADDBA_REQUEST)
2396205277Srpaulo			ADDSHORT(frm, args[4]);	/* baseqctl */
2397195377Ssam		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2398195377Ssam		return ht_action_output(ni, m);
2399195377Ssam	} else {
2400195377Ssam		vap->iv_stats.is_tx_nobuf++;
2401195377Ssam		ieee80211_free_node(ni);
2402195377Ssam		return ENOMEM;
2403195377Ssam	}
2404195377Ssam}
2405195377Ssam
2406195377Ssamstatic int
2407195377Ssamht_send_action_ba_delba(struct ieee80211_node *ni,
2408195377Ssam	int category, int action, void *arg0)
2409195377Ssam{
2410195377Ssam	struct ieee80211vap *vap = ni->ni_vap;
2411195377Ssam	struct ieee80211com *ic = ni->ni_ic;
2412195377Ssam	uint16_t *args = arg0;
2413195377Ssam	struct mbuf *m;
2414170530Ssam	uint16_t baparamset;
2415195377Ssam	uint8_t *frm;
2416170530Ssam
2417195377Ssam	baparamset = SM(args[0], IEEE80211_DELBAPS_TID)
2418195377Ssam		   | args[1]
2419195377Ssam		   ;
2420195377Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2421195377Ssam	    "send DELBA action: tid %d, initiator %d reason %d",
2422195377Ssam	    args[0], args[1], args[2]);
2423170530Ssam
2424178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2425195377Ssam	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2426195377Ssam	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2427170530Ssam	ieee80211_ref_node(ni);
2428170530Ssam
2429170530Ssam	m = ieee80211_getmgtframe(&frm,
2430195377Ssam	    ic->ic_headroom + sizeof(struct ieee80211_frame),
2431195377Ssam	    sizeof(uint16_t)	/* action+category */
2432195377Ssam	    /* XXX may action payload */
2433195377Ssam	    + sizeof(struct ieee80211_action_ba_addbaresponse)
2434170530Ssam	);
2435195377Ssam	if (m != NULL) {
2436195377Ssam		*frm++ = category;
2437195377Ssam		*frm++ = action;
2438195377Ssam		ADDSHORT(frm, baparamset);
2439195377Ssam		ADDSHORT(frm, args[2]);		/* reason code */
2440195377Ssam		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2441195377Ssam		return ht_action_output(ni, m);
2442195377Ssam	} else {
2443195377Ssam		vap->iv_stats.is_tx_nobuf++;
2444195377Ssam		ieee80211_free_node(ni);
2445195377Ssam		return ENOMEM;
2446195377Ssam	}
2447195377Ssam}
2448170530Ssam
2449195377Ssamstatic int
2450195377Ssamht_send_action_ht_txchwidth(struct ieee80211_node *ni,
2451195377Ssam	int category, int action, void *arg0)
2452195377Ssam{
2453195377Ssam	struct ieee80211vap *vap = ni->ni_vap;
2454195377Ssam	struct ieee80211com *ic = ni->ni_ic;
2455195377Ssam	struct mbuf *m;
2456195377Ssam	uint8_t *frm;
2457170530Ssam
2458195377Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2459195377Ssam	    "send HT txchwidth: width %d",
2460195377Ssam	    IEEE80211_IS_CHAN_HT40(ni->ni_chan) ? 40 : 20);
2461170530Ssam
2462195377Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2463195377Ssam	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2464195377Ssam	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2465195377Ssam	ieee80211_ref_node(ni);
2466170530Ssam
2467195377Ssam	m = ieee80211_getmgtframe(&frm,
2468195377Ssam	    ic->ic_headroom + sizeof(struct ieee80211_frame),
2469195377Ssam	    sizeof(uint16_t)	/* action+category */
2470195377Ssam	    /* XXX may action payload */
2471195377Ssam	    + sizeof(struct ieee80211_action_ba_addbaresponse)
2472195377Ssam	);
2473195377Ssam	if (m != NULL) {
2474195377Ssam		*frm++ = category;
2475195377Ssam		*frm++ = action;
2476195377Ssam		*frm++ = IEEE80211_IS_CHAN_HT40(ni->ni_chan) ?
2477195377Ssam			IEEE80211_A_HT_TXCHWIDTH_2040 :
2478195377Ssam			IEEE80211_A_HT_TXCHWIDTH_20;
2479195377Ssam		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2480195377Ssam		return ht_action_output(ni, m);
2481195377Ssam	} else {
2482195377Ssam		vap->iv_stats.is_tx_nobuf++;
2483195377Ssam		ieee80211_free_node(ni);
2484195377Ssam		return ENOMEM;
2485170530Ssam	}
2486195377Ssam}
2487170530Ssam#undef ADDSHORT
2488170530Ssam
2489170530Ssam/*
2490219600Sbschmidt * Construct the MCS bit mask for inclusion in an HT capabilities
2491219600Sbschmidt * information element.
2492170530Ssam */
2493219600Sbschmidtstatic void
2494219600Sbschmidtieee80211_set_mcsset(struct ieee80211com *ic, uint8_t *frm)
2495170530Ssam{
2496170530Ssam	int i;
2497219600Sbschmidt	uint8_t txparams;
2498170530Ssam
2499219600Sbschmidt	KASSERT((ic->ic_rxstream > 0 && ic->ic_rxstream <= 4),
2500219600Sbschmidt	    ("ic_rxstream %d out of range", ic->ic_rxstream));
2501219600Sbschmidt	KASSERT((ic->ic_txstream > 0 && ic->ic_txstream <= 4),
2502219600Sbschmidt	    ("ic_txstream %d out of range", ic->ic_txstream));
2503219600Sbschmidt
2504219600Sbschmidt	for (i = 0; i < ic->ic_rxstream * 8; i++)
2505219600Sbschmidt		setbit(frm, i);
2506219600Sbschmidt	if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
2507219600Sbschmidt	    (ic->ic_htcaps & IEEE80211_HTC_RXMCS32))
2508219600Sbschmidt		setbit(frm, 32);
2509219600Sbschmidt	if (ic->ic_htcaps & IEEE80211_HTC_RXUNEQUAL) {
2510219600Sbschmidt		if (ic->ic_rxstream >= 2) {
2511219600Sbschmidt			for (i = 33; i <= 38; i++)
2512219600Sbschmidt				setbit(frm, i);
2513170530Ssam		}
2514219600Sbschmidt		if (ic->ic_rxstream >= 3) {
2515219600Sbschmidt			for (i = 39; i <= 52; i++)
2516219600Sbschmidt				setbit(frm, i);
2517219600Sbschmidt		}
2518219600Sbschmidt		if (ic->ic_txstream >= 4) {
2519219600Sbschmidt			for (i = 53; i <= 76; i++)
2520219600Sbschmidt				setbit(frm, i);
2521219600Sbschmidt		}
2522170530Ssam	}
2523219600Sbschmidt
2524219600Sbschmidt	if (ic->ic_rxstream != ic->ic_txstream) {
2525219600Sbschmidt		txparams = 0x1;			/* TX MCS set defined */
2526219600Sbschmidt		txparams |= 0x2;		/* TX RX MCS not equal */
2527219600Sbschmidt		txparams |= (ic->ic_txstream - 1) << 2;	/* num TX streams */
2528219600Sbschmidt		if (ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL)
2529219600Sbschmidt			txparams |= 0x16;	/* TX unequal modulation sup */
2530219600Sbschmidt	} else
2531219600Sbschmidt		txparams = 0;
2532219600Sbschmidt	frm[12] = txparams;
2533170530Ssam}
2534170530Ssam
2535170530Ssam/*
2536170530Ssam * Add body of an HTCAP information element.
2537170530Ssam */
2538170530Ssamstatic uint8_t *
2539170530Ssamieee80211_add_htcap_body(uint8_t *frm, struct ieee80211_node *ni)
2540170530Ssam{
2541170530Ssam#define	ADDSHORT(frm, v) do {			\
2542170530Ssam	frm[0] = (v) & 0xff;			\
2543170530Ssam	frm[1] = (v) >> 8;			\
2544170530Ssam	frm += 2;				\
2545170530Ssam} while (0)
2546222683Sbschmidt	struct ieee80211com *ic = ni->ni_ic;
2547178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2548205513Srpaulo	uint16_t caps, extcaps;
2549173865Ssam	int rxmax, density;
2550170530Ssam
2551170530Ssam	/* HT capabilities */
2552178354Ssam	caps = vap->iv_htcaps & 0xffff;
2553173273Ssam	/*
2554173273Ssam	 * Note channel width depends on whether we are operating as
2555173273Ssam	 * a sta or not.  When operating as a sta we are generating
2556173273Ssam	 * a request based on our desired configuration.  Otherwise
2557173273Ssam	 * we are operational and the channel attributes identify
2558173273Ssam	 * how we've been setup (which might be different if a fixed
2559173273Ssam	 * channel is specified).
2560173273Ssam	 */
2561178354Ssam	if (vap->iv_opmode == IEEE80211_M_STA) {
2562173273Ssam		/* override 20/40 use based on config */
2563193655Ssam		if (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)
2564173273Ssam			caps |= IEEE80211_HTCAP_CHWIDTH40;
2565173273Ssam		else
2566173273Ssam			caps &= ~IEEE80211_HTCAP_CHWIDTH40;
2567173865Ssam		/* use advertised setting (XXX locally constraint) */
2568173865Ssam		rxmax = MS(ni->ni_htparam, IEEE80211_HTCAP_MAXRXAMPDU);
2569173865Ssam		density = MS(ni->ni_htparam, IEEE80211_HTCAP_MPDUDENSITY);
2570222683Sbschmidt
2571222683Sbschmidt		/*
2572222683Sbschmidt		 * NB: Hardware might support HT40 on some but not all
2573222683Sbschmidt		 * channels. We can't determine this earlier because only
2574222683Sbschmidt		 * after association the channel is upgraded to HT based
2575222683Sbschmidt		 * on the negotiated capabilities.
2576222683Sbschmidt		 */
2577222683Sbschmidt		if (ni->ni_chan != IEEE80211_CHAN_ANYC &&
2578222683Sbschmidt		    findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT40U) == NULL &&
2579222683Sbschmidt		    findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT40D) == NULL)
2580222683Sbschmidt			caps &= ~IEEE80211_HTCAP_CHWIDTH40;
2581173273Ssam	} else {
2582173273Ssam		/* override 20/40 use based on current channel */
2583178354Ssam		if (IEEE80211_IS_CHAN_HT40(ni->ni_chan))
2584173273Ssam			caps |= IEEE80211_HTCAP_CHWIDTH40;
2585173273Ssam		else
2586173273Ssam			caps &= ~IEEE80211_HTCAP_CHWIDTH40;
2587178354Ssam		rxmax = vap->iv_ampdu_rxmax;
2588178354Ssam		density = vap->iv_ampdu_density;
2589173273Ssam	}
2590170530Ssam	/* adjust short GI based on channel and config */
2591193655Ssam	if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20) == 0)
2592170530Ssam		caps &= ~IEEE80211_HTCAP_SHORTGI20;
2593193655Ssam	if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40) == 0 ||
2594170530Ssam	    (caps & IEEE80211_HTCAP_CHWIDTH40) == 0)
2595170530Ssam		caps &= ~IEEE80211_HTCAP_SHORTGI40;
2596170530Ssam	ADDSHORT(frm, caps);
2597170530Ssam
2598170530Ssam	/* HT parameters */
2599173865Ssam	*frm = SM(rxmax, IEEE80211_HTCAP_MAXRXAMPDU)
2600173865Ssam	     | SM(density, IEEE80211_HTCAP_MPDUDENSITY)
2601173273Ssam	     ;
2602170530Ssam	frm++;
2603170530Ssam
2604170530Ssam	/* pre-zero remainder of ie */
2605170530Ssam	memset(frm, 0, sizeof(struct ieee80211_ie_htcap) -
2606170530Ssam		__offsetof(struct ieee80211_ie_htcap, hc_mcsset));
2607170530Ssam
2608170530Ssam	/* supported MCS set */
2609173273Ssam	/*
2610219600Sbschmidt	 * XXX: For sta mode the rate set should be restricted based
2611219600Sbschmidt	 * on the AP's capabilities, but ni_htrates isn't setup when
2612219600Sbschmidt	 * we're called to form an AssocReq frame so for now we're
2613219600Sbschmidt	 * restricted to the device capabilities.
2614173273Ssam	 */
2615219600Sbschmidt	ieee80211_set_mcsset(ni->ni_ic, frm);
2616170530Ssam
2617205513Srpaulo	frm += __offsetof(struct ieee80211_ie_htcap, hc_extcap) -
2618205513Srpaulo		__offsetof(struct ieee80211_ie_htcap, hc_mcsset);
2619205513Srpaulo
2620205513Srpaulo	/* HT extended capabilities */
2621205513Srpaulo	extcaps = vap->iv_htextcaps & 0xffff;
2622205513Srpaulo
2623205513Srpaulo	ADDSHORT(frm, extcaps);
2624205513Srpaulo
2625170530Ssam	frm += sizeof(struct ieee80211_ie_htcap) -
2626205513Srpaulo		__offsetof(struct ieee80211_ie_htcap, hc_txbf);
2627205513Srpaulo
2628170530Ssam	return frm;
2629170530Ssam#undef ADDSHORT
2630170530Ssam}
2631170530Ssam
2632170530Ssam/*
2633170530Ssam * Add 802.11n HT capabilities information element
2634170530Ssam */
2635170530Ssamuint8_t *
2636170530Ssamieee80211_add_htcap(uint8_t *frm, struct ieee80211_node *ni)
2637170530Ssam{
2638170530Ssam	frm[0] = IEEE80211_ELEMID_HTCAP;
2639170530Ssam	frm[1] = sizeof(struct ieee80211_ie_htcap) - 2;
2640170530Ssam	return ieee80211_add_htcap_body(frm + 2, ni);
2641170530Ssam}
2642170530Ssam
2643170530Ssam/*
2644170530Ssam * Add Broadcom OUI wrapped standard HTCAP ie; this is
2645170530Ssam * used for compatibility w/ pre-draft implementations.
2646170530Ssam */
2647170530Ssamuint8_t *
2648170530Ssamieee80211_add_htcap_vendor(uint8_t *frm, struct ieee80211_node *ni)
2649170530Ssam{
2650170530Ssam	frm[0] = IEEE80211_ELEMID_VENDOR;
2651170530Ssam	frm[1] = 4 + sizeof(struct ieee80211_ie_htcap) - 2;
2652170530Ssam	frm[2] = (BCM_OUI >> 0) & 0xff;
2653170530Ssam	frm[3] = (BCM_OUI >> 8) & 0xff;
2654170530Ssam	frm[4] = (BCM_OUI >> 16) & 0xff;
2655170530Ssam	frm[5] = BCM_OUI_HTCAP;
2656170530Ssam	return ieee80211_add_htcap_body(frm + 6, ni);
2657170530Ssam}
2658170530Ssam
2659170530Ssam/*
2660170530Ssam * Construct the MCS bit mask of basic rates
2661170530Ssam * for inclusion in an HT information element.
2662170530Ssam */
2663170530Ssamstatic void
2664170530Ssamieee80211_set_basic_htrates(uint8_t *frm, const struct ieee80211_htrateset *rs)
2665170530Ssam{
2666170530Ssam	int i;
2667170530Ssam
2668170530Ssam	for (i = 0; i < rs->rs_nrates; i++) {
2669170530Ssam		int r = rs->rs_rates[i] & IEEE80211_RATE_VAL;
2670170530Ssam		if ((rs->rs_rates[i] & IEEE80211_RATE_BASIC) &&
2671170530Ssam		    r < IEEE80211_HTRATE_MAXSIZE) {
2672170530Ssam			/* NB: this assumes a particular implementation */
2673170530Ssam			setbit(frm, r);
2674170530Ssam		}
2675170530Ssam	}
2676170530Ssam}
2677170530Ssam
2678170530Ssam/*
2679172211Ssam * Update the HTINFO ie for a beacon frame.
2680172211Ssam */
2681172211Ssamvoid
2682178354Ssamieee80211_ht_update_beacon(struct ieee80211vap *vap,
2683172211Ssam	struct ieee80211_beacon_offsets *bo)
2684172211Ssam{
2685172211Ssam#define	PROTMODE	(IEEE80211_HTINFO_OPMODE|IEEE80211_HTINFO_NONHT_PRESENT)
2686178354Ssam	const struct ieee80211_channel *bsschan = vap->iv_bss->ni_chan;
2687178354Ssam	struct ieee80211com *ic = vap->iv_ic;
2688172211Ssam	struct ieee80211_ie_htinfo *ht =
2689172211Ssam	   (struct ieee80211_ie_htinfo *) bo->bo_htinfo;
2690172211Ssam
2691172211Ssam	/* XXX only update on channel change */
2692178354Ssam	ht->hi_ctrlchannel = ieee80211_chan2ieee(ic, bsschan);
2693193655Ssam	if (vap->iv_flags_ht & IEEE80211_FHT_RIFS)
2694183256Ssam		ht->hi_byte1 = IEEE80211_HTINFO_RIFSMODE_PERM;
2695183256Ssam	else
2696183256Ssam		ht->hi_byte1 = IEEE80211_HTINFO_RIFSMODE_PROH;
2697178354Ssam	if (IEEE80211_IS_CHAN_HT40U(bsschan))
2698172211Ssam		ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_ABOVE;
2699178354Ssam	else if (IEEE80211_IS_CHAN_HT40D(bsschan))
2700172211Ssam		ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_BELOW;
2701172211Ssam	else
2702172211Ssam		ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_NONE;
2703178354Ssam	if (IEEE80211_IS_CHAN_HT40(bsschan))
2704172211Ssam		ht->hi_byte1 |= IEEE80211_HTINFO_TXWIDTH_2040;
2705172211Ssam
2706172211Ssam	/* protection mode */
2707172211Ssam	ht->hi_byte2 = (ht->hi_byte2 &~ PROTMODE) | ic->ic_curhtprotmode;
2708172211Ssam
2709172211Ssam	/* XXX propagate to vendor ie's */
2710172211Ssam#undef PROTMODE
2711172211Ssam}
2712172211Ssam
2713172211Ssam/*
2714170530Ssam * Add body of an HTINFO information element.
2715173273Ssam *
2716173273Ssam * NB: We don't use struct ieee80211_ie_htinfo because we can
2717173273Ssam * be called to fillin both a standard ie and a compat ie that
2718173273Ssam * has a vendor OUI at the front.
2719170530Ssam */
2720170530Ssamstatic uint8_t *
2721170530Ssamieee80211_add_htinfo_body(uint8_t *frm, struct ieee80211_node *ni)
2722170530Ssam{
2723183256Ssam	struct ieee80211vap *vap = ni->ni_vap;
2724170530Ssam	struct ieee80211com *ic = ni->ni_ic;
2725170530Ssam
2726170530Ssam	/* pre-zero remainder of ie */
2727170530Ssam	memset(frm, 0, sizeof(struct ieee80211_ie_htinfo) - 2);
2728170530Ssam
2729170530Ssam	/* primary/control channel center */
2730178354Ssam	*frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
2731170530Ssam
2732193655Ssam	if (vap->iv_flags_ht & IEEE80211_FHT_RIFS)
2733183256Ssam		frm[0] = IEEE80211_HTINFO_RIFSMODE_PERM;
2734183256Ssam	else
2735183256Ssam		frm[0] = IEEE80211_HTINFO_RIFSMODE_PROH;
2736178354Ssam	if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan))
2737170530Ssam		frm[0] |= IEEE80211_HTINFO_2NDCHAN_ABOVE;
2738178354Ssam	else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan))
2739170530Ssam		frm[0] |= IEEE80211_HTINFO_2NDCHAN_BELOW;
2740170530Ssam	else
2741170530Ssam		frm[0] |= IEEE80211_HTINFO_2NDCHAN_NONE;
2742178354Ssam	if (IEEE80211_IS_CHAN_HT40(ni->ni_chan))
2743170530Ssam		frm[0] |= IEEE80211_HTINFO_TXWIDTH_2040;
2744170530Ssam
2745172211Ssam	frm[1] = ic->ic_curhtprotmode;
2746170530Ssam
2747170530Ssam	frm += 5;
2748170530Ssam
2749170530Ssam	/* basic MCS set */
2750170530Ssam	ieee80211_set_basic_htrates(frm, &ni->ni_htrates);
2751170530Ssam	frm += sizeof(struct ieee80211_ie_htinfo) -
2752170530Ssam		__offsetof(struct ieee80211_ie_htinfo, hi_basicmcsset);
2753170530Ssam	return frm;
2754170530Ssam}
2755170530Ssam
2756170530Ssam/*
2757170530Ssam * Add 802.11n HT information information element.
2758170530Ssam */
2759170530Ssamuint8_t *
2760170530Ssamieee80211_add_htinfo(uint8_t *frm, struct ieee80211_node *ni)
2761170530Ssam{
2762170530Ssam	frm[0] = IEEE80211_ELEMID_HTINFO;
2763170530Ssam	frm[1] = sizeof(struct ieee80211_ie_htinfo) - 2;
2764170530Ssam	return ieee80211_add_htinfo_body(frm + 2, ni);
2765170530Ssam}
2766170530Ssam
2767170530Ssam/*
2768170530Ssam * Add Broadcom OUI wrapped standard HTINFO ie; this is
2769170530Ssam * used for compatibility w/ pre-draft implementations.
2770170530Ssam */
2771170530Ssamuint8_t *
2772170530Ssamieee80211_add_htinfo_vendor(uint8_t *frm, struct ieee80211_node *ni)
2773170530Ssam{
2774170530Ssam	frm[0] = IEEE80211_ELEMID_VENDOR;
2775170530Ssam	frm[1] = 4 + sizeof(struct ieee80211_ie_htinfo) - 2;
2776170530Ssam	frm[2] = (BCM_OUI >> 0) & 0xff;
2777170530Ssam	frm[3] = (BCM_OUI >> 8) & 0xff;
2778170530Ssam	frm[4] = (BCM_OUI >> 16) & 0xff;
2779170530Ssam	frm[5] = BCM_OUI_HTINFO;
2780170530Ssam	return ieee80211_add_htinfo_body(frm + 6, ni);
2781170530Ssam}
2782