1170530Ssam/*-
2178354Ssam * Copyright (c) 2007-2008 Sam Leffler, Errno Consulting
3170530Ssam * All rights reserved.
4170530Ssam *
5170530Ssam * Redistribution and use in source and binary forms, with or without
6170530Ssam * modification, are permitted provided that the following conditions
7170530Ssam * are met:
8170530Ssam * 1. Redistributions of source code must retain the above copyright
9170530Ssam *    notice, this list of conditions and the following disclaimer.
10170530Ssam * 2. Redistributions in binary form must reproduce the above copyright
11170530Ssam *    notice, this list of conditions and the following disclaimer in the
12170530Ssam *    documentation and/or other materials provided with the distribution.
13170530Ssam *
14170530Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15170530Ssam * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16170530Ssam * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17170530Ssam * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18170530Ssam * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19170530Ssam * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20170530Ssam * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21170530Ssam * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22170530Ssam * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23170530Ssam * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24170530Ssam */
25170530Ssam
26170530Ssam#include <sys/cdefs.h>
27170530Ssam#ifdef __FreeBSD__
28170530Ssam__FBSDID("$FreeBSD$");
29170530Ssam#endif
30170530Ssam
31170530Ssam/*
32170530Ssam * IEEE 802.11n protocol support.
33170530Ssam */
34170530Ssam
35170530Ssam#include "opt_inet.h"
36178354Ssam#include "opt_wlan.h"
37170530Ssam
38170530Ssam#include <sys/param.h>
39170530Ssam#include <sys/kernel.h>
40170530Ssam#include <sys/systm.h>
41170530Ssam#include <sys/endian.h>
42170530Ssam
43170530Ssam#include <sys/socket.h>
44170530Ssam
45170530Ssam#include <net/if.h>
46170530Ssam#include <net/if_media.h>
47170530Ssam#include <net/ethernet.h>
48170530Ssam
49170530Ssam#include <net80211/ieee80211_var.h>
50195377Ssam#include <net80211/ieee80211_action.h>
51178354Ssam#include <net80211/ieee80211_input.h>
52170530Ssam
53170530Ssam/* define here, used throughout file */
54170530Ssam#define	MS(_v, _f)	(((_v) & _f) >> _f##_S)
55170530Ssam#define	SM(_v, _f)	(((_v) << _f##_S) & _f)
56170530Ssam
57219456Sbschmidtconst struct ieee80211_mcs_rates ieee80211_htrates[IEEE80211_HTRATE_MAXSIZE] = {
58219456Sbschmidt	{  13,  14,   27,   30 },	/* MCS 0 */
59219456Sbschmidt	{  26,  29,   54,   60 },	/* MCS 1 */
60219456Sbschmidt	{  39,  43,   81,   90 },	/* MCS 2 */
61219456Sbschmidt	{  52,  58,  108,  120 },	/* MCS 3 */
62219456Sbschmidt	{  78,  87,  162,  180 },	/* MCS 4 */
63219456Sbschmidt	{ 104, 116,  216,  240 },	/* MCS 5 */
64219456Sbschmidt	{ 117, 130,  243,  270 },	/* MCS 6 */
65219456Sbschmidt	{ 130, 144,  270,  300 },	/* MCS 7 */
66219456Sbschmidt	{  26,  29,   54,   60 },	/* MCS 8 */
67219456Sbschmidt	{  52,  58,  108,  120 },	/* MCS 9 */
68219456Sbschmidt	{  78,  87,  162,  180 },	/* MCS 10 */
69219456Sbschmidt	{ 104, 116,  216,  240 },	/* MCS 11 */
70219456Sbschmidt	{ 156, 173,  324,  360 },	/* MCS 12 */
71219456Sbschmidt	{ 208, 231,  432,  480 },	/* MCS 13 */
72219456Sbschmidt	{ 234, 260,  486,  540 },	/* MCS 14 */
73219456Sbschmidt	{ 260, 289,  540,  600 },	/* MCS 15 */
74219456Sbschmidt	{  39,  43,   81,   90 },	/* MCS 16 */
75219456Sbschmidt	{  78,  87,  162,  180 },	/* MCS 17 */
76219456Sbschmidt	{ 117, 130,  243,  270 },	/* MCS 18 */
77219456Sbschmidt	{ 156, 173,  324,  360 },	/* MCS 19 */
78219456Sbschmidt	{ 234, 260,  486,  540 },	/* MCS 20 */
79219456Sbschmidt	{ 312, 347,  648,  720 },	/* MCS 21 */
80219456Sbschmidt	{ 351, 390,  729,  810 },	/* MCS 22 */
81219456Sbschmidt	{ 390, 433,  810,  900 },	/* MCS 23 */
82219456Sbschmidt	{  52,  58,  108,  120 },	/* MCS 24 */
83219456Sbschmidt	{ 104, 116,  216,  240 },	/* MCS 25 */
84219456Sbschmidt	{ 156, 173,  324,  360 },	/* MCS 26 */
85219456Sbschmidt	{ 208, 231,  432,  480 },	/* MCS 27 */
86219456Sbschmidt	{ 312, 347,  648,  720 },	/* MCS 28 */
87219456Sbschmidt	{ 416, 462,  864,  960 },	/* MCS 29 */
88219456Sbschmidt	{ 468, 520,  972, 1080 },	/* MCS 30 */
89219456Sbschmidt	{ 520, 578, 1080, 1200 },	/* MCS 31 */
90219456Sbschmidt	{   0,   0,   12,   13 },	/* MCS 32 */
91219456Sbschmidt	{  78,  87,  162,  180 },	/* MCS 33 */
92219456Sbschmidt	{ 104, 116,  216,  240 },	/* MCS 34 */
93219456Sbschmidt	{ 130, 144,  270,  300 },	/* MCS 35 */
94219456Sbschmidt	{ 117, 130,  243,  270 },	/* MCS 36 */
95219456Sbschmidt	{ 156, 173,  324,  360 },	/* MCS 37 */
96219456Sbschmidt	{ 195, 217,  405,  450 },	/* MCS 38 */
97219456Sbschmidt	{ 104, 116,  216,  240 },	/* MCS 39 */
98219456Sbschmidt	{ 130, 144,  270,  300 },	/* MCS 40 */
99219456Sbschmidt	{ 130, 144,  270,  300 },	/* MCS 41 */
100219456Sbschmidt	{ 156, 173,  324,  360 },	/* MCS 42 */
101219456Sbschmidt	{ 182, 202,  378,  420 },	/* MCS 43 */
102219456Sbschmidt	{ 182, 202,  378,  420 },	/* MCS 44 */
103219456Sbschmidt	{ 208, 231,  432,  480 },	/* MCS 45 */
104219456Sbschmidt	{ 156, 173,  324,  360 },	/* MCS 46 */
105219456Sbschmidt	{ 195, 217,  405,  450 },	/* MCS 47 */
106219456Sbschmidt	{ 195, 217,  405,  450 },	/* MCS 48 */
107219456Sbschmidt	{ 234, 260,  486,  540 },	/* MCS 49 */
108219456Sbschmidt	{ 273, 303,  567,  630 },	/* MCS 50 */
109219456Sbschmidt	{ 273, 303,  567,  630 },	/* MCS 51 */
110219456Sbschmidt	{ 312, 347,  648,  720 },	/* MCS 52 */
111219456Sbschmidt	{ 130, 144,  270,  300 },	/* MCS 53 */
112219456Sbschmidt	{ 156, 173,  324,  360 },	/* MCS 54 */
113219456Sbschmidt	{ 182, 202,  378,  420 },	/* MCS 55 */
114219456Sbschmidt	{ 156, 173,  324,  360 },	/* MCS 56 */
115219456Sbschmidt	{ 182, 202,  378,  420 },	/* MCS 57 */
116219456Sbschmidt	{ 208, 231,  432,  480 },	/* MCS 58 */
117219456Sbschmidt	{ 234, 260,  486,  540 },	/* MCS 59 */
118219456Sbschmidt	{ 208, 231,  432,  480 },	/* MCS 60 */
119219456Sbschmidt	{ 234, 260,  486,  540 },	/* MCS 61 */
120219456Sbschmidt	{ 260, 289,  540,  600 },	/* MCS 62 */
121219456Sbschmidt	{ 260, 289,  540,  600 },	/* MCS 63 */
122219456Sbschmidt	{ 286, 318,  594,  660 },	/* MCS 64 */
123219456Sbschmidt	{ 195, 217,  405,  450 },	/* MCS 65 */
124219456Sbschmidt	{ 234, 260,  486,  540 },	/* MCS 66 */
125219456Sbschmidt	{ 273, 303,  567,  630 },	/* MCS 67 */
126219456Sbschmidt	{ 234, 260,  486,  540 },	/* MCS 68 */
127219456Sbschmidt	{ 273, 303,  567,  630 },	/* MCS 69 */
128219456Sbschmidt	{ 312, 347,  648,  720 },	/* MCS 70 */
129219456Sbschmidt	{ 351, 390,  729,  810 },	/* MCS 71 */
130219456Sbschmidt	{ 312, 347,  648,  720 },	/* MCS 72 */
131219456Sbschmidt	{ 351, 390,  729,  810 },	/* MCS 73 */
132219456Sbschmidt	{ 390, 433,  810,  900 },	/* MCS 74 */
133219456Sbschmidt	{ 390, 433,  810,  900 },	/* MCS 75 */
134219456Sbschmidt	{ 429, 477,  891,  990 },	/* MCS 76 */
135170530Ssam};
136170530Ssam
137173273Ssam#ifdef IEEE80211_AMPDU_AGE
138193115Ssamstatic	int ieee80211_ampdu_age = -1;	/* threshold for ampdu reorder q (ms) */
139193115SsamSYSCTL_PROC(_net_wlan, OID_AUTO, ampdu_age, CTLTYPE_INT | CTLFLAG_RW,
140193115Ssam	&ieee80211_ampdu_age, 0, ieee80211_sysctl_msecs_ticks, "I",
141193115Ssam	"AMPDU max reorder age (ms)");
142173273Ssam#endif
143173273Ssam
144193115Ssamstatic	int ieee80211_recv_bar_ena = 1;
145193115SsamSYSCTL_INT(_net_wlan, OID_AUTO, recv_bar, CTLFLAG_RW, &ieee80211_recv_bar_ena,
146193115Ssam	    0, "BAR frame processing (ena/dis)");
147193115Ssam
148193115Ssamstatic	int ieee80211_addba_timeout = -1;/* timeout for ADDBA response */
149193115SsamSYSCTL_PROC(_net_wlan, OID_AUTO, addba_timeout, CTLTYPE_INT | CTLFLAG_RW,
150193115Ssam	&ieee80211_addba_timeout, 0, ieee80211_sysctl_msecs_ticks, "I",
151193115Ssam	"ADDBA request timeout (ms)");
152193115Ssamstatic	int ieee80211_addba_backoff = -1;/* backoff after max ADDBA requests */
153193115SsamSYSCTL_PROC(_net_wlan, OID_AUTO, addba_backoff, CTLTYPE_INT | CTLFLAG_RW,
154193115Ssam	&ieee80211_addba_backoff, 0, ieee80211_sysctl_msecs_ticks, "I",
155193115Ssam	"ADDBA request backoff (ms)");
156193115Ssamstatic	int ieee80211_addba_maxtries = 3;/* max ADDBA requests before backoff */
157193115SsamSYSCTL_INT(_net_wlan, OID_AUTO, addba_maxtries, CTLTYPE_INT | CTLFLAG_RW,
158193115Ssam	&ieee80211_addba_maxtries, 0, "max ADDBA requests sent before backoff");
159193115Ssam
160193115Ssamstatic	int ieee80211_bar_timeout = -1;	/* timeout waiting for BAR response */
161193115Ssamstatic	int ieee80211_bar_maxtries = 50;/* max BAR requests before DELBA */
162193115Ssam
163195377Ssamstatic	ieee80211_recv_action_func ht_recv_action_ba_addba_request;
164195377Ssamstatic	ieee80211_recv_action_func ht_recv_action_ba_addba_response;
165195377Ssamstatic	ieee80211_recv_action_func ht_recv_action_ba_delba;
166195377Ssamstatic	ieee80211_recv_action_func ht_recv_action_ht_mimopwrsave;
167195377Ssamstatic	ieee80211_recv_action_func ht_recv_action_ht_txchwidth;
168195377Ssam
169195377Ssamstatic	ieee80211_send_action_func ht_send_action_ba_addba;
170195377Ssamstatic	ieee80211_send_action_func ht_send_action_ba_delba;
171195377Ssamstatic	ieee80211_send_action_func ht_send_action_ht_txchwidth;
172195377Ssam
173178354Ssamstatic void
174195377Ssamieee80211_ht_init(void)
175178354Ssam{
176195377Ssam	/*
177195377Ssam	 * Setup HT parameters that depends on the clock frequency.
178195377Ssam	 */
179178354Ssam#ifdef IEEE80211_AMPDU_AGE
180178354Ssam	ieee80211_ampdu_age = msecs_to_ticks(500);
181178354Ssam#endif
182178354Ssam	ieee80211_addba_timeout = msecs_to_ticks(250);
183178354Ssam	ieee80211_addba_backoff = msecs_to_ticks(10*1000);
184184280Ssam	ieee80211_bar_timeout = msecs_to_ticks(250);
185195377Ssam	/*
186195377Ssam	 * Register action frame handlers.
187195377Ssam	 */
188195377Ssam	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_BA,
189195377Ssam	    IEEE80211_ACTION_BA_ADDBA_REQUEST, ht_recv_action_ba_addba_request);
190195377Ssam	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_BA,
191195377Ssam	    IEEE80211_ACTION_BA_ADDBA_RESPONSE, ht_recv_action_ba_addba_response);
192195377Ssam	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_BA,
193195377Ssam	    IEEE80211_ACTION_BA_DELBA, ht_recv_action_ba_delba);
194195377Ssam	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_HT,
195195377Ssam	    IEEE80211_ACTION_HT_MIMOPWRSAVE, ht_recv_action_ht_mimopwrsave);
196195377Ssam	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_HT,
197195377Ssam	    IEEE80211_ACTION_HT_TXCHWIDTH, ht_recv_action_ht_txchwidth);
198195377Ssam
199195377Ssam	ieee80211_send_action_register(IEEE80211_ACTION_CAT_BA,
200195377Ssam	    IEEE80211_ACTION_BA_ADDBA_REQUEST, ht_send_action_ba_addba);
201195377Ssam	ieee80211_send_action_register(IEEE80211_ACTION_CAT_BA,
202195377Ssam	    IEEE80211_ACTION_BA_ADDBA_RESPONSE, ht_send_action_ba_addba);
203195377Ssam	ieee80211_send_action_register(IEEE80211_ACTION_CAT_BA,
204195377Ssam	    IEEE80211_ACTION_BA_DELBA, ht_send_action_ba_delba);
205195377Ssam	ieee80211_send_action_register(IEEE80211_ACTION_CAT_HT,
206195377Ssam	    IEEE80211_ACTION_HT_TXCHWIDTH, ht_send_action_ht_txchwidth);
207178354Ssam}
208195377SsamSYSINIT(wlan_ht, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_ht_init, NULL);
209170530Ssam
210178354Ssamstatic int ieee80211_ampdu_enable(struct ieee80211_node *ni,
211178354Ssam	struct ieee80211_tx_ampdu *tap);
212170530Ssamstatic int ieee80211_addba_request(struct ieee80211_node *ni,
213170530Ssam	struct ieee80211_tx_ampdu *tap,
214170530Ssam	int dialogtoken, int baparamset, int batimeout);
215170530Ssamstatic int ieee80211_addba_response(struct ieee80211_node *ni,
216170530Ssam	struct ieee80211_tx_ampdu *tap,
217170530Ssam	int code, int baparamset, int batimeout);
218170530Ssamstatic void ieee80211_addba_stop(struct ieee80211_node *ni,
219170530Ssam	struct ieee80211_tx_ampdu *tap);
220223331Sadrianstatic void null_addba_response_timeout(struct ieee80211_node *ni,
221223331Sadrian	struct ieee80211_tx_ampdu *tap);
222223331Sadrian
223184280Ssamstatic void ieee80211_bar_response(struct ieee80211_node *ni,
224184280Ssam	struct ieee80211_tx_ampdu *tap, int status);
225184280Ssamstatic void ampdu_tx_stop(struct ieee80211_tx_ampdu *tap);
226184280Ssamstatic void bar_stop_timer(struct ieee80211_tx_ampdu *tap);
227191552Ssamstatic int ampdu_rx_start(struct ieee80211_node *, struct ieee80211_rx_ampdu *,
228191552Ssam	int baparamset, int batimeout, int baseqctl);
229191552Ssamstatic void ampdu_rx_stop(struct ieee80211_node *, struct ieee80211_rx_ampdu *);
230170530Ssam
231170530Ssamvoid
232170530Ssamieee80211_ht_attach(struct ieee80211com *ic)
233170530Ssam{
234170530Ssam	/* setup default aggregation policy */
235195377Ssam	ic->ic_recv_action = ieee80211_recv_action;
236170530Ssam	ic->ic_send_action = ieee80211_send_action;
237178354Ssam	ic->ic_ampdu_enable = ieee80211_ampdu_enable;
238170530Ssam	ic->ic_addba_request = ieee80211_addba_request;
239170530Ssam	ic->ic_addba_response = ieee80211_addba_response;
240223331Sadrian	ic->ic_addba_response_timeout = null_addba_response_timeout;
241170530Ssam	ic->ic_addba_stop = ieee80211_addba_stop;
242184280Ssam	ic->ic_bar_response = ieee80211_bar_response;
243191552Ssam	ic->ic_ampdu_rx_start = ampdu_rx_start;
244191552Ssam	ic->ic_ampdu_rx_stop = ampdu_rx_stop;
245170530Ssam
246173273Ssam	ic->ic_htprotmode = IEEE80211_PROT_RTSCTS;
247173273Ssam	ic->ic_curhtprotmode = IEEE80211_HTINFO_OPMODE_PURE;
248178354Ssam}
249173273Ssam
250178354Ssamvoid
251178354Ssamieee80211_ht_detach(struct ieee80211com *ic)
252178354Ssam{
253178354Ssam}
254173273Ssam
255178354Ssamvoid
256178354Ssamieee80211_ht_vattach(struct ieee80211vap *vap)
257178354Ssam{
258178354Ssam
259178354Ssam	/* driver can override defaults */
260178354Ssam	vap->iv_ampdu_rxmax = IEEE80211_HTCAP_MAXRXAMPDU_8K;
261178354Ssam	vap->iv_ampdu_density = IEEE80211_HTCAP_MPDUDENSITY_NA;
262178354Ssam	vap->iv_ampdu_limit = vap->iv_ampdu_rxmax;
263178354Ssam	vap->iv_amsdu_limit = vap->iv_htcaps & IEEE80211_HTCAP_MAXAMSDU;
264178354Ssam	/* tx aggregation traffic thresholds */
265178354Ssam	vap->iv_ampdu_mintraffic[WME_AC_BK] = 128;
266178354Ssam	vap->iv_ampdu_mintraffic[WME_AC_BE] = 64;
267178354Ssam	vap->iv_ampdu_mintraffic[WME_AC_VO] = 32;
268178354Ssam	vap->iv_ampdu_mintraffic[WME_AC_VI] = 32;
269178354Ssam
270178354Ssam	if (vap->iv_htcaps & IEEE80211_HTC_HT) {
271170530Ssam		/*
272173273Ssam		 * Device is HT capable; enable all HT-related
273173273Ssam		 * facilities by default.
274170530Ssam		 * XXX these choices may be too aggressive.
275170530Ssam		 */
276193655Ssam		vap->iv_flags_ht |= IEEE80211_FHT_HT
277193655Ssam				 |  IEEE80211_FHT_HTCOMPAT
278193655Ssam				 ;
279178354Ssam		if (vap->iv_htcaps & IEEE80211_HTCAP_SHORTGI20)
280193655Ssam			vap->iv_flags_ht |= IEEE80211_FHT_SHORTGI20;
281173273Ssam		/* XXX infer from channel list? */
282178354Ssam		if (vap->iv_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
283193655Ssam			vap->iv_flags_ht |= IEEE80211_FHT_USEHT40;
284178354Ssam			if (vap->iv_htcaps & IEEE80211_HTCAP_SHORTGI40)
285193655Ssam				vap->iv_flags_ht |= IEEE80211_FHT_SHORTGI40;
286170530Ssam		}
287183256Ssam		/* enable RIFS if capable */
288183256Ssam		if (vap->iv_htcaps & IEEE80211_HTC_RIFS)
289193655Ssam			vap->iv_flags_ht |= IEEE80211_FHT_RIFS;
290183256Ssam
291170530Ssam		/* NB: A-MPDU and A-MSDU rx are mandated, these are tx only */
292193655Ssam		vap->iv_flags_ht |= IEEE80211_FHT_AMPDU_RX;
293178354Ssam		if (vap->iv_htcaps & IEEE80211_HTC_AMPDU)
294193655Ssam			vap->iv_flags_ht |= IEEE80211_FHT_AMPDU_TX;
295193655Ssam		vap->iv_flags_ht |= IEEE80211_FHT_AMSDU_RX;
296178354Ssam		if (vap->iv_htcaps & IEEE80211_HTC_AMSDU)
297193655Ssam			vap->iv_flags_ht |= IEEE80211_FHT_AMSDU_TX;
298170530Ssam	}
299178354Ssam	/* NB: disable default legacy WDS, too many issues right now */
300178354Ssam	if (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY)
301193655Ssam		vap->iv_flags_ht &= ~IEEE80211_FHT_HT;
302170530Ssam}
303170530Ssam
304170530Ssamvoid
305178354Ssamieee80211_ht_vdetach(struct ieee80211vap *vap)
306170530Ssam{
307170530Ssam}
308170530Ssam
309219598Sbschmidtstatic int
310234753Sdimht_getrate(struct ieee80211com *ic, int index, enum ieee80211_phymode mode,
311234753Sdim    int ratetype)
312219598Sbschmidt{
313219598Sbschmidt	int mword, rate;
314219598Sbschmidt
315219598Sbschmidt	mword = ieee80211_rate2media(ic, index | IEEE80211_RATE_MCS, mode);
316219598Sbschmidt	if (IFM_SUBTYPE(mword) != IFM_IEEE80211_MCS)
317219598Sbschmidt		return (0);
318219598Sbschmidt	switch (ratetype) {
319219598Sbschmidt	case 0:
320219598Sbschmidt		rate = ieee80211_htrates[index].ht20_rate_800ns;
321219598Sbschmidt		break;
322219598Sbschmidt	case 1:
323219598Sbschmidt		rate = ieee80211_htrates[index].ht20_rate_400ns;
324219598Sbschmidt		break;
325219598Sbschmidt	case 2:
326219598Sbschmidt		rate = ieee80211_htrates[index].ht40_rate_800ns;
327219598Sbschmidt		break;
328219598Sbschmidt	default:
329219598Sbschmidt		rate = ieee80211_htrates[index].ht40_rate_400ns;
330219598Sbschmidt		break;
331219598Sbschmidt	}
332219598Sbschmidt	return (rate);
333219598Sbschmidt}
334219598Sbschmidt
335219598Sbschmidtstatic struct printranges {
336219598Sbschmidt	int	minmcs;
337219598Sbschmidt	int	maxmcs;
338219598Sbschmidt	int	txstream;
339219598Sbschmidt	int	ratetype;
340219598Sbschmidt	int	htcapflags;
341219598Sbschmidt} ranges[] = {
342219598Sbschmidt	{  0,  7, 1, 0, 0 },
343219598Sbschmidt	{  8, 15, 2, 0, 0 },
344219598Sbschmidt	{ 16, 23, 3, 0, 0 },
345219598Sbschmidt	{ 24, 31, 4, 0, 0 },
346219598Sbschmidt	{ 32,  0, 1, 2, IEEE80211_HTC_TXMCS32 },
347219598Sbschmidt	{ 33, 38, 2, 0, IEEE80211_HTC_TXUNEQUAL },
348219598Sbschmidt	{ 39, 52, 3, 0, IEEE80211_HTC_TXUNEQUAL },
349219598Sbschmidt	{ 53, 76, 4, 0, IEEE80211_HTC_TXUNEQUAL },
350219598Sbschmidt	{  0,  0, 0, 0, 0 },
351219598Sbschmidt};
352219598Sbschmidt
353170530Ssamstatic void
354234753Sdimht_rateprint(struct ieee80211com *ic, enum ieee80211_phymode mode, int ratetype)
355170530Ssam{
356219598Sbschmidt	struct ifnet *ifp = ic->ic_ifp;
357219598Sbschmidt	int minrate, maxrate;
358219598Sbschmidt	struct printranges *range;
359170530Ssam
360219598Sbschmidt	for (range = ranges; range->txstream != 0; range++) {
361219598Sbschmidt		if (ic->ic_txstream < range->txstream)
362170530Ssam			continue;
363219598Sbschmidt		if (range->htcapflags &&
364219598Sbschmidt		    (ic->ic_htcaps & range->htcapflags) == 0)
365219598Sbschmidt			continue;
366219598Sbschmidt		if (ratetype < range->ratetype)
367219598Sbschmidt			continue;
368219598Sbschmidt		minrate = ht_getrate(ic, range->minmcs, mode, ratetype);
369219598Sbschmidt		maxrate = ht_getrate(ic, range->maxmcs, mode, ratetype);
370219598Sbschmidt		if (range->maxmcs) {
371219598Sbschmidt			if_printf(ifp, "MCS %d-%d: %d%sMbps - %d%sMbps\n",
372219598Sbschmidt			    range->minmcs, range->maxmcs,
373219598Sbschmidt			    minrate/2, ((minrate & 0x1) != 0 ? ".5" : ""),
374219598Sbschmidt			    maxrate/2, ((maxrate & 0x1) != 0 ? ".5" : ""));
375219598Sbschmidt		} else {
376219598Sbschmidt			if_printf(ifp, "MCS %d: %d%sMbps\n", range->minmcs,
377219598Sbschmidt			    minrate/2, ((minrate & 0x1) != 0 ? ".5" : ""));
378205277Srpaulo		}
379170530Ssam	}
380170530Ssam}
381170530Ssam
382205277Srpaulostatic void
383234753Sdimht_announce(struct ieee80211com *ic, enum ieee80211_phymode mode)
384205277Srpaulo{
385205277Srpaulo	struct ifnet *ifp = ic->ic_ifp;
386205277Srpaulo	const char *modestr = ieee80211_phymode_name[mode];
387219598Sbschmidt
388219606Sbschmidt	if_printf(ifp, "%s MCS 20MHz\n", modestr);
389219598Sbschmidt	ht_rateprint(ic, mode, 0);
390219598Sbschmidt	if (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20) {
391219606Sbschmidt		if_printf(ifp, "%s MCS 20MHz SGI\n", modestr);
392219598Sbschmidt		ht_rateprint(ic, mode, 1);
393219598Sbschmidt	}
394219598Sbschmidt	if (ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
395219606Sbschmidt		if_printf(ifp, "%s MCS 40MHz:\n", modestr);
396219598Sbschmidt		ht_rateprint(ic, mode, 2);
397219598Sbschmidt	}
398219598Sbschmidt	if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
399219598Sbschmidt	    (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40)) {
400219606Sbschmidt		if_printf(ifp, "%s MCS 40MHz SGI:\n", modestr);
401219598Sbschmidt		ht_rateprint(ic, mode, 3);
402219598Sbschmidt	}
403205277Srpaulo}
404205277Srpaulo
405170530Ssamvoid
406170530Ssamieee80211_ht_announce(struct ieee80211com *ic)
407170530Ssam{
408219598Sbschmidt	struct ifnet *ifp = ic->ic_ifp;
409219598Sbschmidt
410219598Sbschmidt	if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) ||
411219598Sbschmidt	    isset(ic->ic_modecaps, IEEE80211_MODE_11NG))
412219598Sbschmidt		if_printf(ifp, "%dT%dR\n", ic->ic_txstream, ic->ic_rxstream);
413170530Ssam	if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA))
414219598Sbschmidt		ht_announce(ic, IEEE80211_MODE_11NA);
415170530Ssam	if (isset(ic->ic_modecaps, IEEE80211_MODE_11NG))
416219598Sbschmidt		ht_announce(ic, IEEE80211_MODE_11NG);
417170530Ssam}
418170530Ssam
419219602Sbschmidtstatic struct ieee80211_htrateset htrateset;
420219602Sbschmidt
421170530Ssamconst struct ieee80211_htrateset *
422170530Ssamieee80211_get_suphtrates(struct ieee80211com *ic,
423219602Sbschmidt    const struct ieee80211_channel *c)
424170530Ssam{
425219602Sbschmidt#define	ADDRATE(x)	do {						\
426219602Sbschmidt	htrateset.rs_rates[htrateset.rs_nrates] = x;			\
427219602Sbschmidt	htrateset.rs_nrates++;						\
428219602Sbschmidt} while (0)
429219602Sbschmidt	int i;
430219602Sbschmidt
431219602Sbschmidt	memset(&htrateset, 0, sizeof(struct ieee80211_htrateset));
432219602Sbschmidt	for (i = 0; i < ic->ic_txstream * 8; i++)
433219602Sbschmidt		ADDRATE(i);
434219602Sbschmidt	if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
435219602Sbschmidt	    (ic->ic_htcaps & IEEE80211_HTC_TXMCS32))
436230619Sbschmidt		ADDRATE(32);
437219602Sbschmidt	if (ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL) {
438219602Sbschmidt		if (ic->ic_txstream >= 2) {
439219602Sbschmidt			 for (i = 33; i <= 38; i++)
440219602Sbschmidt				ADDRATE(i);
441219602Sbschmidt		}
442219602Sbschmidt		if (ic->ic_txstream >= 3) {
443219602Sbschmidt			for (i = 39; i <= 52; i++)
444219602Sbschmidt				ADDRATE(i);
445219602Sbschmidt		}
446219602Sbschmidt		if (ic->ic_txstream == 4) {
447219602Sbschmidt			for (i = 53; i <= 76; i++)
448219602Sbschmidt				ADDRATE(i);
449219602Sbschmidt		}
450219602Sbschmidt	}
451219602Sbschmidt	return &htrateset;
452219602Sbschmidt#undef	ADDRATE
453170530Ssam}
454170530Ssam
455170530Ssam/*
456170530Ssam * Receive processing.
457170530Ssam */
458170530Ssam
459170530Ssam/*
460170530Ssam * Decap the encapsulated A-MSDU frames and dispatch all but
461170530Ssam * the last for delivery.  The last frame is returned for
462170530Ssam * delivery via the normal path.
463170530Ssam */
464170530Ssamstruct mbuf *
465170530Ssamieee80211_decap_amsdu(struct ieee80211_node *ni, struct mbuf *m)
466170530Ssam{
467178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
468173462Ssam	int framelen;
469170530Ssam	struct mbuf *n;
470170530Ssam
471170530Ssam	/* discard 802.3 header inserted by ieee80211_decap */
472170530Ssam	m_adj(m, sizeof(struct ether_header));
473170530Ssam
474178354Ssam	vap->iv_stats.is_amsdu_decap++;
475170530Ssam
476170530Ssam	for (;;) {
477170530Ssam		/*
478170530Ssam		 * Decap the first frame, bust it apart from the
479170530Ssam		 * remainder and deliver.  We leave the last frame
480170530Ssam		 * delivery to the caller (for consistency with other
481170530Ssam		 * code paths, could also do it here).
482170530Ssam		 */
483170530Ssam		m = ieee80211_decap1(m, &framelen);
484170530Ssam		if (m == NULL) {
485178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
486173462Ssam			    ni->ni_macaddr, "a-msdu", "%s", "decap failed");
487178354Ssam			vap->iv_stats.is_amsdu_tooshort++;
488170530Ssam			return NULL;
489170530Ssam		}
490173462Ssam		if (m->m_pkthdr.len == framelen)
491170530Ssam			break;
492170530Ssam		n = m_split(m, framelen, M_NOWAIT);
493170530Ssam		if (n == NULL) {
494178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
495170530Ssam			    ni->ni_macaddr, "a-msdu",
496170530Ssam			    "%s", "unable to split encapsulated frames");
497178354Ssam			vap->iv_stats.is_amsdu_split++;
498170530Ssam			m_freem(m);			/* NB: must reclaim */
499170530Ssam			return NULL;
500170530Ssam		}
501178354Ssam		vap->iv_deliver_data(vap, ni, m);
502170530Ssam
503170530Ssam		/*
504170530Ssam		 * Remove frame contents; each intermediate frame
505170530Ssam		 * is required to be aligned to a 4-byte boundary.
506170530Ssam		 */
507170530Ssam		m = n;
508170530Ssam		m_adj(m, roundup2(framelen, 4) - framelen);	/* padding */
509170530Ssam	}
510170530Ssam	return m;				/* last delivered by caller */
511170530Ssam}
512170530Ssam
513170530Ssam/*
514170530Ssam * Purge all frames in the A-MPDU re-order queue.
515170530Ssam */
516170530Ssamstatic void
517170530Ssamampdu_rx_purge(struct ieee80211_rx_ampdu *rap)
518170530Ssam{
519170530Ssam	struct mbuf *m;
520170530Ssam	int i;
521170530Ssam
522170530Ssam	for (i = 0; i < rap->rxa_wnd; i++) {
523170530Ssam		m = rap->rxa_m[i];
524170530Ssam		if (m != NULL) {
525170530Ssam			rap->rxa_m[i] = NULL;
526170530Ssam			rap->rxa_qbytes -= m->m_pkthdr.len;
527170530Ssam			m_freem(m);
528170530Ssam			if (--rap->rxa_qframes == 0)
529170530Ssam				break;
530170530Ssam		}
531170530Ssam	}
532170530Ssam	KASSERT(rap->rxa_qbytes == 0 && rap->rxa_qframes == 0,
533170530Ssam	    ("lost %u data, %u frames on ampdu rx q",
534170530Ssam	    rap->rxa_qbytes, rap->rxa_qframes));
535170530Ssam}
536170530Ssam
537170530Ssam/*
538178354Ssam * Start A-MPDU rx/re-order processing for the specified TID.
539178354Ssam */
540191552Ssamstatic int
541191552Ssamampdu_rx_start(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap,
542191552Ssam	int baparamset, int batimeout, int baseqctl)
543178354Ssam{
544191552Ssam	int bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
545191552Ssam
546178354Ssam	if (rap->rxa_flags & IEEE80211_AGGR_RUNNING) {
547178354Ssam		/*
548178354Ssam		 * AMPDU previously setup and not terminated with a DELBA,
549178354Ssam		 * flush the reorder q's in case anything remains.
550178354Ssam		 */
551178354Ssam		ampdu_rx_purge(rap);
552178354Ssam	}
553178354Ssam	memset(rap, 0, sizeof(*rap));
554178354Ssam	rap->rxa_wnd = (bufsiz == 0) ?
555178354Ssam	    IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
556191552Ssam	rap->rxa_start = MS(baseqctl, IEEE80211_BASEQ_START);
557178354Ssam	rap->rxa_flags |=  IEEE80211_AGGR_RUNNING | IEEE80211_AGGR_XCHGPEND;
558191552Ssam
559191552Ssam	return 0;
560178354Ssam}
561178354Ssam
562178354Ssam/*
563170530Ssam * Stop A-MPDU rx processing for the specified TID.
564170530Ssam */
565170530Ssamstatic void
566191552Ssamampdu_rx_stop(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
567170530Ssam{
568205277Srpaulo
569170530Ssam	ampdu_rx_purge(rap);
570178354Ssam	rap->rxa_flags &= ~(IEEE80211_AGGR_RUNNING | IEEE80211_AGGR_XCHGPEND);
571170530Ssam}
572170530Ssam
573170530Ssam/*
574170530Ssam * Dispatch a frame from the A-MPDU reorder queue.  The
575170530Ssam * frame is fed back into ieee80211_input marked with an
576183247Ssam * M_AMPDU_MPDU flag so it doesn't come back to us (it also
577170530Ssam * permits ieee80211_input to optimize re-processing).
578170530Ssam */
579170530Ssamstatic __inline void
580170530Ssamampdu_dispatch(struct ieee80211_node *ni, struct mbuf *m)
581170530Ssam{
582183247Ssam	m->m_flags |= M_AMPDU_MPDU;	/* bypass normal processing */
583192468Ssam	/* NB: rssi and noise are ignored w/ M_AMPDU_MPDU set */
584192468Ssam	(void) ieee80211_input(ni, m, 0, 0);
585170530Ssam}
586170530Ssam
587170530Ssam/*
588170530Ssam * Dispatch as many frames as possible from the re-order queue.
589170530Ssam * Frames will always be "at the front"; we process all frames
590170530Ssam * up to the first empty slot in the window.  On completion we
591170530Ssam * cleanup state if there are still pending frames in the current
592170530Ssam * BA window.  We assume the frame at slot 0 is already handled
593170530Ssam * by the caller; we always start at slot 1.
594170530Ssam */
595170530Ssamstatic void
596170530Ssamampdu_rx_dispatch(struct ieee80211_rx_ampdu *rap, struct ieee80211_node *ni)
597170530Ssam{
598178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
599170530Ssam	struct mbuf *m;
600170530Ssam	int i;
601170530Ssam
602170530Ssam	/* flush run of frames */
603170530Ssam	for (i = 1; i < rap->rxa_wnd; i++) {
604170530Ssam		m = rap->rxa_m[i];
605170530Ssam		if (m == NULL)
606170530Ssam			break;
607170530Ssam		rap->rxa_m[i] = NULL;
608170530Ssam		rap->rxa_qbytes -= m->m_pkthdr.len;
609170530Ssam		rap->rxa_qframes--;
610170530Ssam
611170530Ssam		ampdu_dispatch(ni, m);
612170530Ssam	}
613170530Ssam	/*
614170530Ssam	 * If frames remain, copy the mbuf pointers down so
615170530Ssam	 * they correspond to the offsets in the new window.
616170530Ssam	 */
617170530Ssam	if (rap->rxa_qframes != 0) {
618170530Ssam		int n = rap->rxa_qframes, j;
619170530Ssam		for (j = i+1; j < rap->rxa_wnd; j++) {
620170530Ssam			if (rap->rxa_m[j] != NULL) {
621170530Ssam				rap->rxa_m[j-i] = rap->rxa_m[j];
622170530Ssam				rap->rxa_m[j] = NULL;
623170530Ssam				if (--n == 0)
624170530Ssam					break;
625170530Ssam			}
626170530Ssam		}
627170530Ssam		KASSERT(n == 0, ("lost %d frames", n));
628178354Ssam		vap->iv_stats.is_ampdu_rx_copy += rap->rxa_qframes;
629170530Ssam	}
630173273Ssam	/*
631173273Ssam	 * Adjust the start of the BA window to
632173273Ssam	 * reflect the frames just dispatched.
633173273Ssam	 */
634173273Ssam	rap->rxa_start = IEEE80211_SEQ_ADD(rap->rxa_start, i);
635178354Ssam	vap->iv_stats.is_ampdu_rx_oor += i;
636170530Ssam}
637170530Ssam
638173273Ssam#ifdef IEEE80211_AMPDU_AGE
639170530Ssam/*
640173273Ssam * Dispatch all frames in the A-MPDU re-order queue.
641170530Ssam */
642170530Ssamstatic void
643173273Ssamampdu_rx_flush(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
644170530Ssam{
645178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
646170530Ssam	struct mbuf *m;
647170530Ssam	int i;
648170530Ssam
649173273Ssam	for (i = 0; i < rap->rxa_wnd; i++) {
650170530Ssam		m = rap->rxa_m[i];
651170530Ssam		if (m == NULL)
652170530Ssam			continue;
653170530Ssam		rap->rxa_m[i] = NULL;
654170530Ssam		rap->rxa_qbytes -= m->m_pkthdr.len;
655173273Ssam		rap->rxa_qframes--;
656178354Ssam		vap->iv_stats.is_ampdu_rx_oor++;
657173273Ssam
658170530Ssam		ampdu_dispatch(ni, m);
659173273Ssam		if (rap->rxa_qframes == 0)
660170530Ssam			break;
661170530Ssam	}
662170530Ssam}
663173273Ssam#endif /* IEEE80211_AMPDU_AGE */
664170530Ssam
665170530Ssam/*
666173273Ssam * Dispatch all frames in the A-MPDU re-order queue
667173273Ssam * preceding the specified sequence number.  This logic
668173273Ssam * handles window moves due to a received MSDU or BAR.
669173273Ssam */
670173273Ssamstatic void
671173273Ssamampdu_rx_flush_upto(struct ieee80211_node *ni,
672173273Ssam	struct ieee80211_rx_ampdu *rap, ieee80211_seq winstart)
673173273Ssam{
674178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
675173273Ssam	struct mbuf *m;
676173273Ssam	ieee80211_seq seqno;
677173273Ssam	int i;
678173273Ssam
679173273Ssam	/*
680173273Ssam	 * Flush any complete MSDU's with a sequence number lower
681173273Ssam	 * than winstart.  Gaps may exist.  Note that we may actually
682173273Ssam	 * dispatch frames past winstart if a run continues; this is
683173273Ssam	 * an optimization that avoids having to do a separate pass
684173273Ssam	 * to dispatch frames after moving the BA window start.
685173273Ssam	 */
686173273Ssam	seqno = rap->rxa_start;
687173273Ssam	for (i = 0; i < rap->rxa_wnd; i++) {
688173273Ssam		m = rap->rxa_m[i];
689173273Ssam		if (m != NULL) {
690173273Ssam			rap->rxa_m[i] = NULL;
691173273Ssam			rap->rxa_qbytes -= m->m_pkthdr.len;
692173273Ssam			rap->rxa_qframes--;
693178354Ssam			vap->iv_stats.is_ampdu_rx_oor++;
694173273Ssam
695173273Ssam			ampdu_dispatch(ni, m);
696173273Ssam		} else {
697173273Ssam			if (!IEEE80211_SEQ_BA_BEFORE(seqno, winstart))
698173273Ssam				break;
699173273Ssam		}
700173273Ssam		seqno = IEEE80211_SEQ_INC(seqno);
701173273Ssam	}
702173273Ssam	/*
703173273Ssam	 * If frames remain, copy the mbuf pointers down so
704173273Ssam	 * they correspond to the offsets in the new window.
705173273Ssam	 */
706173273Ssam	if (rap->rxa_qframes != 0) {
707173273Ssam		int n = rap->rxa_qframes, j;
708178354Ssam
709178354Ssam		/* NB: this loop assumes i > 0 and/or rxa_m[0] is NULL */
710178354Ssam		KASSERT(rap->rxa_m[0] == NULL,
711178354Ssam		    ("%s: BA window slot 0 occupied", __func__));
712173273Ssam		for (j = i+1; j < rap->rxa_wnd; j++) {
713173273Ssam			if (rap->rxa_m[j] != NULL) {
714173273Ssam				rap->rxa_m[j-i] = rap->rxa_m[j];
715173273Ssam				rap->rxa_m[j] = NULL;
716173273Ssam				if (--n == 0)
717173273Ssam					break;
718173273Ssam			}
719173273Ssam		}
720173273Ssam		KASSERT(n == 0, ("%s: lost %d frames, qframes %d off %d "
721173273Ssam		    "BA win <%d:%d> winstart %d",
722173273Ssam		    __func__, n, rap->rxa_qframes, i, rap->rxa_start,
723173273Ssam		    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
724173273Ssam		    winstart));
725178354Ssam		vap->iv_stats.is_ampdu_rx_copy += rap->rxa_qframes;
726173273Ssam	}
727173273Ssam	/*
728173273Ssam	 * Move the start of the BA window; we use the
729173273Ssam	 * sequence number of the last MSDU that was
730173273Ssam	 * passed up the stack+1 or winstart if stopped on
731173273Ssam	 * a gap in the reorder buffer.
732173273Ssam	 */
733173273Ssam	rap->rxa_start = seqno;
734173273Ssam}
735173273Ssam
736173273Ssam/*
737170530Ssam * Process a received QoS data frame for an HT station.  Handle
738170530Ssam * A-MPDU reordering: if this frame is received out of order
739170530Ssam * and falls within the BA window hold onto it.  Otherwise if
740173273Ssam * this frame completes a run, flush any pending frames.  We
741170530Ssam * return 1 if the frame is consumed.  A 0 is returned if
742170530Ssam * the frame should be processed normally by the caller.
743170530Ssam */
744170530Ssamint
745170530Ssamieee80211_ampdu_reorder(struct ieee80211_node *ni, struct mbuf *m)
746170530Ssam{
747170530Ssam#define	IEEE80211_FC0_QOSDATA \
748170530Ssam	(IEEE80211_FC0_TYPE_DATA|IEEE80211_FC0_SUBTYPE_QOS|IEEE80211_FC0_VERSION_0)
749173273Ssam#define	PROCESS		0	/* caller should process frame */
750173273Ssam#define	CONSUMED	1	/* frame consumed, caller does nothing */
751178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
752170530Ssam	struct ieee80211_qosframe *wh;
753170530Ssam	struct ieee80211_rx_ampdu *rap;
754170530Ssam	ieee80211_seq rxseq;
755170530Ssam	uint8_t tid;
756170530Ssam	int off;
757170530Ssam
758183247Ssam	KASSERT((m->m_flags & (M_AMPDU | M_AMPDU_MPDU)) == M_AMPDU,
759183247Ssam	    ("!a-mpdu or already re-ordered, flags 0x%x", m->m_flags));
760170530Ssam	KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT sta"));
761170530Ssam
762170530Ssam	/* NB: m_len known to be sufficient */
763170530Ssam	wh = mtod(m, struct ieee80211_qosframe *);
764183247Ssam	if (wh->i_fc[0] != IEEE80211_FC0_QOSDATA) {
765183247Ssam		/*
766183247Ssam		 * Not QoS data, shouldn't get here but just
767183247Ssam		 * return it to the caller for processing.
768183247Ssam		 */
769183247Ssam		return PROCESS;
770183247Ssam	}
771193840Ssam	if (IEEE80211_IS_DSTODS(wh))
772173273Ssam		tid = ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0];
773173273Ssam	else
774173273Ssam		tid = wh->i_qos[0];
775173273Ssam	tid &= IEEE80211_QOS_TID;
776170530Ssam	rap = &ni->ni_rx_ampdu[tid];
777170530Ssam	if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
778170530Ssam		/*
779170530Ssam		 * No ADDBA request yet, don't touch.
780170530Ssam		 */
781173273Ssam		return PROCESS;
782170530Ssam	}
783182827Ssam	rxseq = le16toh(*(uint16_t *)wh->i_seq);
784182827Ssam	if ((rxseq & IEEE80211_SEQ_FRAG_MASK) != 0) {
785182827Ssam		/*
786182827Ssam		 * Fragments are not allowed; toss.
787182827Ssam		 */
788182827Ssam		IEEE80211_DISCARD_MAC(vap,
789182827Ssam		    IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
790182827Ssam		    "A-MPDU", "fragment, rxseq 0x%x tid %u%s", rxseq, tid,
791182827Ssam		    wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
792182827Ssam		vap->iv_stats.is_ampdu_rx_drop++;
793182827Ssam		IEEE80211_NODE_STAT(ni, rx_drop);
794182827Ssam		m_freem(m);
795182827Ssam		return CONSUMED;
796182827Ssam	}
797182827Ssam	rxseq >>= IEEE80211_SEQ_SEQ_SHIFT;
798173273Ssam	rap->rxa_nframes++;
799173273Ssamagain:
800170530Ssam	if (rxseq == rap->rxa_start) {
801170530Ssam		/*
802170530Ssam		 * First frame in window.
803170530Ssam		 */
804170530Ssam		if (rap->rxa_qframes != 0) {
805170530Ssam			/*
806170530Ssam			 * Dispatch as many packets as we can.
807170530Ssam			 */
808170530Ssam			KASSERT(rap->rxa_m[0] == NULL, ("unexpected dup"));
809170530Ssam			ampdu_dispatch(ni, m);
810170530Ssam			ampdu_rx_dispatch(rap, ni);
811173273Ssam			return CONSUMED;
812170530Ssam		} else {
813170530Ssam			/*
814170530Ssam			 * In order; advance window and notify
815170530Ssam			 * caller to dispatch directly.
816170530Ssam			 */
817170530Ssam			rap->rxa_start = IEEE80211_SEQ_INC(rxseq);
818173273Ssam			return PROCESS;
819170530Ssam		}
820170530Ssam	}
821170530Ssam	/*
822173273Ssam	 * Frame is out of order; store if in the BA window.
823170530Ssam	 */
824170530Ssam	/* calculate offset in BA window */
825170530Ssam	off = IEEE80211_SEQ_SUB(rxseq, rap->rxa_start);
826173273Ssam	if (off < rap->rxa_wnd) {
827170530Ssam		/*
828173273Ssam		 * Common case (hopefully): in the BA window.
829205277Srpaulo		 * Sec 9.10.7.6.2 a) (p.137)
830173273Ssam		 */
831173273Ssam#ifdef IEEE80211_AMPDU_AGE
832173273Ssam		/*
833173273Ssam		 * Check for frames sitting too long in the reorder queue.
834173273Ssam		 * This should only ever happen if frames are not delivered
835173273Ssam		 * without the sender otherwise notifying us (e.g. with a
836173273Ssam		 * BAR to move the window).  Typically this happens because
837173273Ssam		 * of vendor bugs that cause the sequence number to jump.
838173273Ssam		 * When this happens we get a gap in the reorder queue that
839173273Ssam		 * leaves frame sitting on the queue until they get pushed
840173273Ssam		 * out due to window moves.  When the vendor does not send
841173273Ssam		 * BAR this move only happens due to explicit packet sends
842170530Ssam		 *
843173273Ssam		 * NB: we only track the time of the oldest frame in the
844173273Ssam		 * reorder q; this means that if we flush we might push
845173273Ssam		 * frames that still "new"; if this happens then subsequent
846173273Ssam		 * frames will result in BA window moves which cost something
847173273Ssam		 * but is still better than a big throughput dip.
848170530Ssam		 */
849173273Ssam		if (rap->rxa_qframes != 0) {
850173273Ssam			/* XXX honor batimeout? */
851173273Ssam			if (ticks - rap->rxa_age > ieee80211_ampdu_age) {
852173273Ssam				/*
853173273Ssam				 * Too long since we received the first
854173273Ssam				 * frame; flush the reorder buffer.
855173273Ssam				 */
856173273Ssam				if (rap->rxa_qframes != 0) {
857178354Ssam					vap->iv_stats.is_ampdu_rx_age +=
858173273Ssam					    rap->rxa_qframes;
859173273Ssam					ampdu_rx_flush(ni, rap);
860173273Ssam				}
861173273Ssam				rap->rxa_start = IEEE80211_SEQ_INC(rxseq);
862173273Ssam				return PROCESS;
863173273Ssam			}
864173273Ssam		} else {
865173273Ssam			/*
866173273Ssam			 * First frame, start aging timer.
867173273Ssam			 */
868173273Ssam			rap->rxa_age = ticks;
869173273Ssam		}
870173273Ssam#endif /* IEEE80211_AMPDU_AGE */
871173273Ssam		/* save packet */
872173273Ssam		if (rap->rxa_m[off] == NULL) {
873173273Ssam			rap->rxa_m[off] = m;
874173273Ssam			rap->rxa_qframes++;
875173273Ssam			rap->rxa_qbytes += m->m_pkthdr.len;
876178354Ssam			vap->iv_stats.is_ampdu_rx_reorder++;
877173273Ssam		} else {
878178354Ssam			IEEE80211_DISCARD_MAC(vap,
879173273Ssam			    IEEE80211_MSG_INPUT | IEEE80211_MSG_11N,
880173273Ssam			    ni->ni_macaddr, "a-mpdu duplicate",
881173273Ssam			    "seqno %u tid %u BA win <%u:%u>",
882173273Ssam			    rxseq, tid, rap->rxa_start,
883173273Ssam			    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1));
884178354Ssam			vap->iv_stats.is_rx_dup++;
885173273Ssam			IEEE80211_NODE_STAT(ni, rx_dup);
886173273Ssam			m_freem(m);
887173273Ssam		}
888173273Ssam		return CONSUMED;
889173273Ssam	}
890173273Ssam	if (off < IEEE80211_SEQ_BA_RANGE) {
891173273Ssam		/*
892173273Ssam		 * Outside the BA window, but within range;
893173273Ssam		 * flush the reorder q and move the window.
894205277Srpaulo		 * Sec 9.10.7.6.2 b) (p.138)
895173273Ssam		 */
896178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
897173273Ssam		    "move BA win <%u:%u> (%u frames) rxseq %u tid %u",
898170530Ssam		    rap->rxa_start,
899173273Ssam		    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
900170530Ssam		    rap->rxa_qframes, rxseq, tid);
901178354Ssam		vap->iv_stats.is_ampdu_rx_move++;
902170530Ssam
903173273Ssam		/*
904173273Ssam		 * The spec says to flush frames up to but not including:
905173273Ssam		 * 	WinStart_B = rxseq - rap->rxa_wnd + 1
906173273Ssam		 * Then insert the frame or notify the caller to process
907173273Ssam		 * it immediately.  We can safely do this by just starting
908173273Ssam		 * over again because we know the frame will now be within
909173273Ssam		 * the BA window.
910173273Ssam		 */
911173273Ssam		/* NB: rxa_wnd known to be >0 */
912173273Ssam		ampdu_rx_flush_upto(ni, rap,
913173273Ssam		    IEEE80211_SEQ_SUB(rxseq, rap->rxa_wnd-1));
914173273Ssam		goto again;
915170530Ssam	} else {
916170530Ssam		/*
917173273Ssam		 * Outside the BA window and out of range; toss.
918205277Srpaulo		 * Sec 9.10.7.6.2 c) (p.138)
919170530Ssam		 */
920178354Ssam		IEEE80211_DISCARD_MAC(vap,
921173273Ssam		    IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
922178354Ssam		    "MPDU", "BA win <%u:%u> (%u frames) rxseq %u tid %u%s",
923173273Ssam		    rap->rxa_start,
924173273Ssam		    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
925173273Ssam		    rap->rxa_qframes, rxseq, tid,
926173273Ssam		    wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
927178354Ssam		vap->iv_stats.is_ampdu_rx_drop++;
928173273Ssam		IEEE80211_NODE_STAT(ni, rx_drop);
929170530Ssam		m_freem(m);
930173273Ssam		return CONSUMED;
931170530Ssam	}
932173273Ssam#undef CONSUMED
933173273Ssam#undef PROCESS
934170530Ssam#undef IEEE80211_FC0_QOSDATA
935170530Ssam}
936170530Ssam
937170530Ssam/*
938170530Ssam * Process a BAR ctl frame.  Dispatch all frames up to
939170530Ssam * the sequence number of the frame.  If this frame is
940173273Ssam * out of range it's discarded.
941170530Ssam */
942170530Ssamvoid
943170530Ssamieee80211_recv_bar(struct ieee80211_node *ni, struct mbuf *m0)
944170530Ssam{
945178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
946170530Ssam	struct ieee80211_frame_bar *wh;
947170530Ssam	struct ieee80211_rx_ampdu *rap;
948170530Ssam	ieee80211_seq rxseq;
949170530Ssam	int tid, off;
950170530Ssam
951173273Ssam	if (!ieee80211_recv_bar_ena) {
952173273Ssam#if 0
953178354Ssam		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_11N,
954173273Ssam		    ni->ni_macaddr, "BAR", "%s", "processing disabled");
955173273Ssam#endif
956178354Ssam		vap->iv_stats.is_ampdu_bar_bad++;
957173273Ssam		return;
958173273Ssam	}
959170530Ssam	wh = mtod(m0, struct ieee80211_frame_bar *);
960170530Ssam	/* XXX check basic BAR */
961170530Ssam	tid = MS(le16toh(wh->i_ctl), IEEE80211_BAR_TID);
962170530Ssam	rap = &ni->ni_rx_ampdu[tid];
963170530Ssam	if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
964170530Ssam		/*
965170530Ssam		 * No ADDBA request yet, don't touch.
966170530Ssam		 */
967178354Ssam		IEEE80211_DISCARD_MAC(vap,
968170530Ssam		    IEEE80211_MSG_INPUT | IEEE80211_MSG_11N,
969170530Ssam		    ni->ni_macaddr, "BAR", "no BA stream, tid %u", tid);
970178354Ssam		vap->iv_stats.is_ampdu_bar_bad++;
971170530Ssam		return;
972170530Ssam	}
973178354Ssam	vap->iv_stats.is_ampdu_bar_rx++;
974170530Ssam	rxseq = le16toh(wh->i_seq) >> IEEE80211_SEQ_SEQ_SHIFT;
975173273Ssam	if (rxseq == rap->rxa_start)
976173273Ssam		return;
977170530Ssam	/* calculate offset in BA window */
978170530Ssam	off = IEEE80211_SEQ_SUB(rxseq, rap->rxa_start);
979173273Ssam	if (off < IEEE80211_SEQ_BA_RANGE) {
980170530Ssam		/*
981173273Ssam		 * Flush the reorder q up to rxseq and move the window.
982205277Srpaulo		 * Sec 9.10.7.6.3 a) (p.138)
983170530Ssam		 */
984178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
985173273Ssam		    "BAR moves BA win <%u:%u> (%u frames) rxseq %u tid %u",
986170530Ssam		    rap->rxa_start,
987173273Ssam		    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
988173273Ssam		    rap->rxa_qframes, rxseq, tid);
989178354Ssam		vap->iv_stats.is_ampdu_bar_move++;
990173273Ssam
991173273Ssam		ampdu_rx_flush_upto(ni, rap, rxseq);
992173273Ssam		if (off >= rap->rxa_wnd) {
993173273Ssam			/*
994173273Ssam			 * BAR specifies a window start to the right of BA
995173273Ssam			 * window; we must move it explicitly since
996173273Ssam			 * ampdu_rx_flush_upto will not.
997173273Ssam			 */
998173273Ssam			rap->rxa_start = rxseq;
999170530Ssam		}
1000173273Ssam	} else {
1001170530Ssam		/*
1002173273Ssam		 * Out of range; toss.
1003205277Srpaulo		 * Sec 9.10.7.6.3 b) (p.138)
1004170530Ssam		 */
1005178354Ssam		IEEE80211_DISCARD_MAC(vap,
1006173273Ssam		    IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
1007173273Ssam		    "BAR", "BA win <%u:%u> (%u frames) rxseq %u tid %u%s",
1008173273Ssam		    rap->rxa_start,
1009173273Ssam		    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
1010173273Ssam		    rap->rxa_qframes, rxseq, tid,
1011173273Ssam		    wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
1012178354Ssam		vap->iv_stats.is_ampdu_bar_oow++;
1013173273Ssam		IEEE80211_NODE_STAT(ni, rx_drop);
1014170530Ssam	}
1015170530Ssam}
1016170530Ssam
1017170530Ssam/*
1018170530Ssam * Setup HT-specific state in a node.  Called only
1019170530Ssam * when HT use is negotiated so we don't do extra
1020170530Ssam * work for temporary and/or legacy sta's.
1021170530Ssam */
1022170530Ssamvoid
1023183254Ssamieee80211_ht_node_init(struct ieee80211_node *ni)
1024170530Ssam{
1025170530Ssam	struct ieee80211_tx_ampdu *tap;
1026170530Ssam	int ac;
1027170530Ssam
1028173273Ssam	if (ni->ni_flags & IEEE80211_NODE_HT) {
1029173273Ssam		/*
1030173273Ssam		 * Clean AMPDU state on re-associate.  This handles the case
1031173273Ssam		 * where a station leaves w/o notifying us and then returns
1032173273Ssam		 * before node is reaped for inactivity.
1033173273Ssam		 */
1034173273Ssam		ieee80211_ht_node_cleanup(ni);
1035173273Ssam	}
1036170530Ssam	for (ac = 0; ac < WME_NUM_AC; ac++) {
1037170530Ssam		tap = &ni->ni_tx_ampdu[ac];
1038170530Ssam		tap->txa_ac = ac;
1039184280Ssam		tap->txa_ni = ni;
1040173273Ssam		/* NB: further initialization deferred */
1041170530Ssam	}
1042173273Ssam	ni->ni_flags |= IEEE80211_NODE_HT | IEEE80211_NODE_AMPDU;
1043170530Ssam}
1044170530Ssam
1045170530Ssam/*
1046170530Ssam * Cleanup HT-specific state in a node.  Called only
1047170530Ssam * when HT use has been marked.
1048170530Ssam */
1049170530Ssamvoid
1050170530Ssamieee80211_ht_node_cleanup(struct ieee80211_node *ni)
1051170530Ssam{
1052191552Ssam	struct ieee80211com *ic = ni->ni_ic;
1053170530Ssam	int i;
1054170530Ssam
1055170530Ssam	KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT node"));
1056170530Ssam
1057170530Ssam	/* XXX optimize this */
1058170530Ssam	for (i = 0; i < WME_NUM_AC; i++) {
1059170530Ssam		struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[i];
1060184280Ssam		if (tap->txa_flags & IEEE80211_AGGR_SETUP)
1061184280Ssam			ampdu_tx_stop(tap);
1062170530Ssam	}
1063170530Ssam	for (i = 0; i < WME_NUM_TID; i++)
1064191552Ssam		ic->ic_ampdu_rx_stop(ni, &ni->ni_rx_ampdu[i]);
1065170530Ssam
1066170530Ssam	ni->ni_htcap = 0;
1067182828Ssam	ni->ni_flags &= ~IEEE80211_NODE_HT_ALL;
1068170530Ssam}
1069170530Ssam
1070178354Ssam/*
1071178354Ssam * Age out HT resources for a station.
1072178354Ssam */
1073178354Ssamvoid
1074178354Ssamieee80211_ht_node_age(struct ieee80211_node *ni)
1075178354Ssam{
1076178354Ssam#ifdef IEEE80211_AMPDU_AGE
1077178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1078178354Ssam	uint8_t tid;
1079178354Ssam#endif
1080178354Ssam
1081178354Ssam	KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT sta"));
1082178354Ssam
1083178354Ssam#ifdef IEEE80211_AMPDU_AGE
1084178354Ssam	for (tid = 0; tid < WME_NUM_TID; tid++) {
1085178354Ssam		struct ieee80211_rx_ampdu *rap;
1086178354Ssam
1087178354Ssam		rap = &ni->ni_rx_ampdu[tid];
1088178354Ssam		if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0)
1089178354Ssam			continue;
1090178354Ssam		if (rap->rxa_qframes == 0)
1091178354Ssam			continue;
1092178354Ssam		/*
1093178354Ssam		 * Check for frames sitting too long in the reorder queue.
1094178354Ssam		 * See above for more details on what's happening here.
1095178354Ssam		 */
1096178354Ssam		/* XXX honor batimeout? */
1097178354Ssam		if (ticks - rap->rxa_age > ieee80211_ampdu_age) {
1098178354Ssam			/*
1099178354Ssam			 * Too long since we received the first
1100178354Ssam			 * frame; flush the reorder buffer.
1101178354Ssam			 */
1102178354Ssam			vap->iv_stats.is_ampdu_rx_age += rap->rxa_qframes;
1103178354Ssam			ampdu_rx_flush(ni, rap);
1104178354Ssam		}
1105178354Ssam	}
1106178354Ssam#endif /* IEEE80211_AMPDU_AGE */
1107178354Ssam}
1108178354Ssam
1109173273Ssamstatic struct ieee80211_channel *
1110173273Ssamfindhtchan(struct ieee80211com *ic, struct ieee80211_channel *c, int htflags)
1111173273Ssam{
1112173273Ssam	return ieee80211_find_channel(ic, c->ic_freq,
1113173273Ssam	    (c->ic_flags &~ IEEE80211_CHAN_HT) | htflags);
1114173273Ssam}
1115173273Ssam
1116173273Ssam/*
1117173273Ssam * Adjust a channel to be HT/non-HT according to the vap's configuration.
1118173273Ssam */
1119173273Ssamstruct ieee80211_channel *
1120173273Ssamieee80211_ht_adjust_channel(struct ieee80211com *ic,
1121173273Ssam	struct ieee80211_channel *chan, int flags)
1122173273Ssam{
1123173273Ssam	struct ieee80211_channel *c;
1124173273Ssam
1125193655Ssam	if (flags & IEEE80211_FHT_HT) {
1126173273Ssam		/* promote to HT if possible */
1127193655Ssam		if (flags & IEEE80211_FHT_USEHT40) {
1128173273Ssam			if (!IEEE80211_IS_CHAN_HT40(chan)) {
1129173273Ssam				/* NB: arbitrarily pick ht40+ over ht40- */
1130173273Ssam				c = findhtchan(ic, chan, IEEE80211_CHAN_HT40U);
1131173273Ssam				if (c == NULL)
1132173273Ssam					c = findhtchan(ic, chan,
1133173273Ssam						IEEE80211_CHAN_HT40D);
1134173273Ssam				if (c == NULL)
1135173273Ssam					c = findhtchan(ic, chan,
1136173273Ssam						IEEE80211_CHAN_HT20);
1137173273Ssam				if (c != NULL)
1138173273Ssam					chan = c;
1139173273Ssam			}
1140173273Ssam		} else if (!IEEE80211_IS_CHAN_HT20(chan)) {
1141173273Ssam			c = findhtchan(ic, chan, IEEE80211_CHAN_HT20);
1142173273Ssam			if (c != NULL)
1143173273Ssam				chan = c;
1144173273Ssam		}
1145173273Ssam	} else if (IEEE80211_IS_CHAN_HT(chan)) {
1146173273Ssam		/* demote to legacy, HT use is disabled */
1147173273Ssam		c = ieee80211_find_channel(ic, chan->ic_freq,
1148173273Ssam		    chan->ic_flags &~ IEEE80211_CHAN_HT);
1149173273Ssam		if (c != NULL)
1150173273Ssam			chan = c;
1151173273Ssam	}
1152173273Ssam	return chan;
1153173273Ssam}
1154173273Ssam
1155173273Ssam/*
1156173273Ssam * Setup HT-specific state for a legacy WDS peer.
1157173273Ssam */
1158173273Ssamvoid
1159173273Ssamieee80211_ht_wds_init(struct ieee80211_node *ni)
1160173273Ssam{
1161178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1162173273Ssam	struct ieee80211_tx_ampdu *tap;
1163173273Ssam	int ac;
1164173273Ssam
1165193655Ssam	KASSERT(vap->iv_flags_ht & IEEE80211_FHT_HT, ("no HT requested"));
1166173273Ssam
1167173273Ssam	/* XXX check scan cache in case peer has an ap and we have info */
1168173273Ssam	/*
1169173273Ssam	 * If setup with a legacy channel; locate an HT channel.
1170173273Ssam	 * Otherwise if the inherited channel (from a companion
1171173273Ssam	 * AP) is suitable use it so we use the same location
1172173273Ssam	 * for the extension channel).
1173173273Ssam	 */
1174178354Ssam	ni->ni_chan = ieee80211_ht_adjust_channel(ni->ni_ic,
1175178354Ssam	    ni->ni_chan, ieee80211_htchanflags(ni->ni_chan));
1176173273Ssam
1177173273Ssam	ni->ni_htcap = 0;
1178193655Ssam	if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20)
1179173273Ssam		ni->ni_htcap |= IEEE80211_HTCAP_SHORTGI20;
1180173273Ssam	if (IEEE80211_IS_CHAN_HT40(ni->ni_chan)) {
1181173273Ssam		ni->ni_htcap |= IEEE80211_HTCAP_CHWIDTH40;
1182173273Ssam		ni->ni_chw = 40;
1183173273Ssam		if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan))
1184173273Ssam			ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_ABOVE;
1185173273Ssam		else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan))
1186173273Ssam			ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_BELOW;
1187193655Ssam		if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40)
1188173273Ssam			ni->ni_htcap |= IEEE80211_HTCAP_SHORTGI40;
1189173273Ssam	} else {
1190173273Ssam		ni->ni_chw = 20;
1191173273Ssam		ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_NONE;
1192173273Ssam	}
1193173273Ssam	ni->ni_htctlchan = ni->ni_chan->ic_ieee;
1194193655Ssam	if (vap->iv_flags_ht & IEEE80211_FHT_RIFS)
1195183256Ssam		ni->ni_flags |= IEEE80211_NODE_RIFS;
1196183256Ssam	/* XXX does it make sense to enable SMPS? */
1197173273Ssam
1198173273Ssam	ni->ni_htopmode = 0;		/* XXX need protection state */
1199173273Ssam	ni->ni_htstbc = 0;		/* XXX need info */
1200173273Ssam
1201173273Ssam	for (ac = 0; ac < WME_NUM_AC; ac++) {
1202173273Ssam		tap = &ni->ni_tx_ampdu[ac];
1203173273Ssam		tap->txa_ac = ac;
1204173273Ssam	}
1205193655Ssam	/* NB: AMPDU tx/rx governed by IEEE80211_FHT_AMPDU_{TX,RX} */
1206173273Ssam	ni->ni_flags |= IEEE80211_NODE_HT | IEEE80211_NODE_AMPDU;
1207173273Ssam}
1208173273Ssam
1209173273Ssam/*
1210173273Ssam * Notify hostap vaps of a change in the HTINFO ie.
1211173273Ssam */
1212173273Ssamstatic void
1213173273Ssamhtinfo_notify(struct ieee80211com *ic)
1214173273Ssam{
1215178354Ssam	struct ieee80211vap *vap;
1216178354Ssam	int first = 1;
1217178354Ssam
1218178354Ssam	IEEE80211_LOCK_ASSERT(ic);
1219178354Ssam
1220178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1221178354Ssam		if (vap->iv_opmode != IEEE80211_M_HOSTAP)
1222178354Ssam			continue;
1223183253Ssam		if (vap->iv_state != IEEE80211_S_RUN ||
1224183253Ssam		    !IEEE80211_IS_CHAN_HT(vap->iv_bss->ni_chan))
1225183253Ssam			continue;
1226178354Ssam		if (first) {
1227178354Ssam			IEEE80211_NOTE(vap,
1228178354Ssam			    IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N,
1229178354Ssam			    vap->iv_bss,
1230178354Ssam			    "HT bss occupancy change: %d sta, %d ht, "
1231178354Ssam			    "%d ht40%s, HT protmode now 0x%x"
1232178354Ssam			    , ic->ic_sta_assoc
1233178354Ssam			    , ic->ic_ht_sta_assoc
1234178354Ssam			    , ic->ic_ht40_sta_assoc
1235193655Ssam			    , (ic->ic_flags_ht & IEEE80211_FHT_NONHT_PR) ?
1236178354Ssam				 ", non-HT sta present" : ""
1237178354Ssam			    , ic->ic_curhtprotmode);
1238178354Ssam			first = 0;
1239178354Ssam		}
1240178354Ssam		ieee80211_beacon_notify(vap, IEEE80211_BEACON_HTINFO);
1241178354Ssam	}
1242173273Ssam}
1243173273Ssam
1244173273Ssam/*
1245173273Ssam * Calculate HT protection mode from current
1246173273Ssam * state and handle updates.
1247173273Ssam */
1248173273Ssamstatic void
1249173273Ssamhtinfo_update(struct ieee80211com *ic)
1250173273Ssam{
1251173273Ssam	uint8_t protmode;
1252173273Ssam
1253178354Ssam	if (ic->ic_sta_assoc != ic->ic_ht_sta_assoc) {
1254178354Ssam		protmode = IEEE80211_HTINFO_OPMODE_MIXED
1255178354Ssam			 | IEEE80211_HTINFO_NONHT_PRESENT;
1256193655Ssam	} else if (ic->ic_flags_ht & IEEE80211_FHT_NONHT_PR) {
1257173273Ssam		protmode = IEEE80211_HTINFO_OPMODE_PROTOPT
1258178354Ssam			 | IEEE80211_HTINFO_NONHT_PRESENT;
1259178354Ssam	} else if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
1260178354Ssam	    IEEE80211_IS_CHAN_HT40(ic->ic_bsschan) &&
1261173273Ssam	    ic->ic_sta_assoc != ic->ic_ht40_sta_assoc) {
1262173273Ssam		protmode = IEEE80211_HTINFO_OPMODE_HT20PR;
1263173273Ssam	} else {
1264173273Ssam		protmode = IEEE80211_HTINFO_OPMODE_PURE;
1265173273Ssam	}
1266173273Ssam	if (protmode != ic->ic_curhtprotmode) {
1267173273Ssam		ic->ic_curhtprotmode = protmode;
1268173273Ssam		htinfo_notify(ic);
1269173273Ssam	}
1270173273Ssam}
1271173273Ssam
1272173273Ssam/*
1273173273Ssam * Handle an HT station joining a BSS.
1274173273Ssam */
1275173273Ssamvoid
1276173273Ssamieee80211_ht_node_join(struct ieee80211_node *ni)
1277173273Ssam{
1278173273Ssam	struct ieee80211com *ic = ni->ni_ic;
1279173273Ssam
1280173273Ssam	IEEE80211_LOCK_ASSERT(ic);
1281173273Ssam
1282173273Ssam	if (ni->ni_flags & IEEE80211_NODE_HT) {
1283173273Ssam		ic->ic_ht_sta_assoc++;
1284173273Ssam		if (ni->ni_chw == 40)
1285173273Ssam			ic->ic_ht40_sta_assoc++;
1286173273Ssam	}
1287173273Ssam	htinfo_update(ic);
1288173273Ssam}
1289173273Ssam
1290173273Ssam/*
1291173273Ssam * Handle an HT station leaving a BSS.
1292173273Ssam */
1293173273Ssamvoid
1294173273Ssamieee80211_ht_node_leave(struct ieee80211_node *ni)
1295173273Ssam{
1296173273Ssam	struct ieee80211com *ic = ni->ni_ic;
1297173273Ssam
1298173273Ssam	IEEE80211_LOCK_ASSERT(ic);
1299173273Ssam
1300173273Ssam	if (ni->ni_flags & IEEE80211_NODE_HT) {
1301173273Ssam		ic->ic_ht_sta_assoc--;
1302173273Ssam		if (ni->ni_chw == 40)
1303173273Ssam			ic->ic_ht40_sta_assoc--;
1304173273Ssam	}
1305173273Ssam	htinfo_update(ic);
1306173273Ssam}
1307173273Ssam
1308173273Ssam/*
1309173273Ssam * Public version of htinfo_update; used for processing
1310178354Ssam * beacon frames from overlapping bss.
1311178354Ssam *
1312178354Ssam * Caller can specify either IEEE80211_HTINFO_OPMODE_MIXED
1313178354Ssam * (on receipt of a beacon that advertises MIXED) or
1314178354Ssam * IEEE80211_HTINFO_OPMODE_PROTOPT (on receipt of a beacon
1315178354Ssam * from an overlapping legacy bss).  We treat MIXED with
1316178354Ssam * a higher precedence than PROTOPT (i.e. we will not change
1317178354Ssam * change PROTOPT -> MIXED; only MIXED -> PROTOPT).  This
1318178354Ssam * corresponds to how we handle things in htinfo_update.
1319173273Ssam */
1320173273Ssamvoid
1321178354Ssamieee80211_htprot_update(struct ieee80211com *ic, int protmode)
1322173273Ssam{
1323178354Ssam#define	OPMODE(x)	SM(x, IEEE80211_HTINFO_OPMODE)
1324183246Ssam	IEEE80211_LOCK(ic);
1325178354Ssam
1326178354Ssam	/* track non-HT station presence */
1327178354Ssam	KASSERT(protmode & IEEE80211_HTINFO_NONHT_PRESENT,
1328183246Ssam	    ("protmode 0x%x", protmode));
1329193655Ssam	ic->ic_flags_ht |= IEEE80211_FHT_NONHT_PR;
1330178354Ssam	ic->ic_lastnonht = ticks;
1331178354Ssam
1332183246Ssam	if (protmode != ic->ic_curhtprotmode &&
1333183246Ssam	    (OPMODE(ic->ic_curhtprotmode) != IEEE80211_HTINFO_OPMODE_MIXED ||
1334183246Ssam	     OPMODE(protmode) == IEEE80211_HTINFO_OPMODE_PROTOPT)) {
1335183246Ssam		/* push beacon update */
1336183246Ssam		ic->ic_curhtprotmode = protmode;
1337183246Ssam		htinfo_notify(ic);
1338183246Ssam	}
1339181197Ssam	IEEE80211_UNLOCK(ic);
1340178354Ssam#undef OPMODE
1341173273Ssam}
1342173273Ssam
1343173273Ssam/*
1344173273Ssam * Time out presence of an overlapping bss with non-HT
1345173273Ssam * stations.  When operating in hostap mode we listen for
1346173273Ssam * beacons from other stations and if we identify a non-HT
1347173273Ssam * station is present we update the opmode field of the
1348173273Ssam * HTINFO ie.  To identify when all non-HT stations are
1349173273Ssam * gone we time out this condition.
1350173273Ssam */
1351173273Ssamvoid
1352173273Ssamieee80211_ht_timeout(struct ieee80211com *ic)
1353173273Ssam{
1354173273Ssam	IEEE80211_LOCK_ASSERT(ic);
1355173273Ssam
1356193655Ssam	if ((ic->ic_flags_ht & IEEE80211_FHT_NONHT_PR) &&
1357173273Ssam	    time_after(ticks, ic->ic_lastnonht + IEEE80211_NONHT_PRESENT_AGE)) {
1358173273Ssam#if 0
1359178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
1360173273Ssam		    "%s", "time out non-HT STA present on channel");
1361173273Ssam#endif
1362193655Ssam		ic->ic_flags_ht &= ~IEEE80211_FHT_NONHT_PR;
1363173273Ssam		htinfo_update(ic);
1364173273Ssam	}
1365173273Ssam}
1366173273Ssam
1367170530Ssam/* unalligned little endian access */
1368170530Ssam#define LE_READ_2(p)					\
1369170530Ssam	((uint16_t)					\
1370170530Ssam	 ((((const uint8_t *)(p))[0]      ) |		\
1371170530Ssam	  (((const uint8_t *)(p))[1] <<  8)))
1372170530Ssam
1373170530Ssam/*
1374170530Ssam * Process an 802.11n HT capabilities ie.
1375170530Ssam */
1376170530Ssamvoid
1377170530Ssamieee80211_parse_htcap(struct ieee80211_node *ni, const uint8_t *ie)
1378170530Ssam{
1379170530Ssam	if (ie[0] == IEEE80211_ELEMID_VENDOR) {
1380170530Ssam		/*
1381170530Ssam		 * Station used Vendor OUI ie to associate;
1382170530Ssam		 * mark the node so when we respond we'll use
1383170530Ssam		 * the Vendor OUI's and not the standard ie's.
1384170530Ssam		 */
1385170530Ssam		ni->ni_flags |= IEEE80211_NODE_HTCOMPAT;
1386170530Ssam		ie += 4;
1387170530Ssam	} else
1388170530Ssam		ni->ni_flags &= ~IEEE80211_NODE_HTCOMPAT;
1389170530Ssam
1390170530Ssam	ni->ni_htcap = LE_READ_2(ie +
1391170530Ssam		__offsetof(struct ieee80211_ie_htcap, hc_cap));
1392170530Ssam	ni->ni_htparam = ie[__offsetof(struct ieee80211_ie_htcap, hc_param)];
1393170530Ssam}
1394170530Ssam
1395183254Ssamstatic void
1396183254Ssamhtinfo_parse(struct ieee80211_node *ni,
1397183254Ssam	const struct ieee80211_ie_htinfo *htinfo)
1398170530Ssam{
1399170530Ssam	uint16_t w;
1400170530Ssam
1401170530Ssam	ni->ni_htctlchan = htinfo->hi_ctrlchannel;
1402170530Ssam	ni->ni_ht2ndchan = SM(htinfo->hi_byte1, IEEE80211_HTINFO_2NDCHAN);
1403172055Ssam	w = LE_READ_2(&htinfo->hi_byte2);
1404170530Ssam	ni->ni_htopmode = SM(w, IEEE80211_HTINFO_OPMODE);
1405170530Ssam	w = LE_READ_2(&htinfo->hi_byte45);
1406170530Ssam	ni->ni_htstbc = SM(w, IEEE80211_HTINFO_BASIC_STBCMCS);
1407183254Ssam}
1408183254Ssam
1409183254Ssam/*
1410183254Ssam * Parse an 802.11n HT info ie and save useful information
1411183254Ssam * to the node state.  Note this does not effect any state
1412183254Ssam * changes such as for channel width change.
1413183254Ssam */
1414183254Ssamvoid
1415183254Ssamieee80211_parse_htinfo(struct ieee80211_node *ni, const uint8_t *ie)
1416183254Ssam{
1417183254Ssam	if (ie[0] == IEEE80211_ELEMID_VENDOR)
1418183254Ssam		ie += 4;
1419183254Ssam	htinfo_parse(ni, (const struct ieee80211_ie_htinfo *) ie);
1420183254Ssam}
1421183254Ssam
1422183254Ssam/*
1423183254Ssam * Handle 11n channel switch.  Use the received HT ie's to
1424183254Ssam * identify the right channel to use.  If we cannot locate it
1425183254Ssam * in the channel table then fallback to legacy operation.
1426183254Ssam * Note that we use this information to identify the node's
1427183254Ssam * channel only; the caller is responsible for insuring any
1428183254Ssam * required channel change is done (e.g. in sta mode when
1429183254Ssam * parsing the contents of a beacon frame).
1430183254Ssam */
1431183254Ssamstatic void
1432183254Ssamhtinfo_update_chw(struct ieee80211_node *ni, int htflags)
1433183254Ssam{
1434183254Ssam	struct ieee80211com *ic = ni->ni_ic;
1435183254Ssam	struct ieee80211_channel *c;
1436183254Ssam	int chanflags;
1437183254Ssam
1438173273Ssam	chanflags = (ni->ni_chan->ic_flags &~ IEEE80211_CHAN_HT) | htflags;
1439173273Ssam	if (chanflags != ni->ni_chan->ic_flags) {
1440183254Ssam		/* XXX not right for ht40- */
1441173273Ssam		c = ieee80211_find_channel(ic, ni->ni_chan->ic_freq, chanflags);
1442178354Ssam		if (c == NULL && (htflags & IEEE80211_CHAN_HT40)) {
1443173273Ssam			/*
1444173273Ssam			 * No HT40 channel entry in our table; fall back
1445173273Ssam			 * to HT20 operation.  This should not happen.
1446173273Ssam			 */
1447173273Ssam			c = findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT20);
1448183254Ssam#if 0
1449183254Ssam			IEEE80211_NOTE(ni->ni_vap,
1450173273Ssam			    IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
1451173273Ssam			    "no HT40 channel (freq %u), falling back to HT20",
1452173273Ssam			    ni->ni_chan->ic_freq);
1453183254Ssam#endif
1454173273Ssam			/* XXX stat */
1455173273Ssam		}
1456173273Ssam		if (c != NULL && c != ni->ni_chan) {
1457183254Ssam			IEEE80211_NOTE(ni->ni_vap,
1458173273Ssam			    IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
1459173273Ssam			    "switch station to HT%d channel %u/0x%x",
1460173273Ssam			    IEEE80211_IS_CHAN_HT40(c) ? 40 : 20,
1461173273Ssam			    c->ic_freq, c->ic_flags);
1462173273Ssam			ni->ni_chan = c;
1463173273Ssam		}
1464173273Ssam		/* NB: caller responsible for forcing any channel change */
1465173273Ssam	}
1466173273Ssam	/* update node's tx channel width */
1467173273Ssam	ni->ni_chw = IEEE80211_IS_CHAN_HT40(ni->ni_chan)? 40 : 20;
1468170530Ssam}
1469170530Ssam
1470170530Ssam/*
1471183255Ssam * Update 11n MIMO PS state according to received htcap.
1472183255Ssam */
1473183255Ssamstatic __inline int
1474183255Ssamhtcap_update_mimo_ps(struct ieee80211_node *ni)
1475183255Ssam{
1476183255Ssam	uint16_t oflags = ni->ni_flags;
1477183255Ssam
1478183255Ssam	switch (ni->ni_htcap & IEEE80211_HTCAP_SMPS) {
1479183255Ssam	case IEEE80211_HTCAP_SMPS_DYNAMIC:
1480183255Ssam		ni->ni_flags |= IEEE80211_NODE_MIMO_PS;
1481183255Ssam		ni->ni_flags |= IEEE80211_NODE_MIMO_RTS;
1482183255Ssam		break;
1483183255Ssam	case IEEE80211_HTCAP_SMPS_ENA:
1484183255Ssam		ni->ni_flags |= IEEE80211_NODE_MIMO_PS;
1485183255Ssam		ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS;
1486183255Ssam		break;
1487183255Ssam	case IEEE80211_HTCAP_SMPS_OFF:
1488183255Ssam	default:		/* disable on rx of reserved value */
1489183255Ssam		ni->ni_flags &= ~IEEE80211_NODE_MIMO_PS;
1490183255Ssam		ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS;
1491183255Ssam		break;
1492183255Ssam	}
1493183255Ssam	return (oflags ^ ni->ni_flags);
1494183255Ssam}
1495183255Ssam
1496183255Ssam/*
1497183257Ssam * Update short GI state according to received htcap
1498183257Ssam * and local settings.
1499183257Ssam */
1500183257Ssamstatic __inline void
1501183257Ssamhtcap_update_shortgi(struct ieee80211_node *ni)
1502183257Ssam{
1503183257Ssam	struct ieee80211vap *vap = ni->ni_vap;
1504183257Ssam
1505183257Ssam	ni->ni_flags &= ~(IEEE80211_NODE_SGI20|IEEE80211_NODE_SGI40);
1506183257Ssam	if ((ni->ni_htcap & IEEE80211_HTCAP_SHORTGI20) &&
1507193655Ssam	    (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20))
1508183257Ssam		ni->ni_flags |= IEEE80211_NODE_SGI20;
1509183257Ssam	if ((ni->ni_htcap & IEEE80211_HTCAP_SHORTGI40) &&
1510193655Ssam	    (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40))
1511183257Ssam		ni->ni_flags |= IEEE80211_NODE_SGI40;
1512183257Ssam}
1513183257Ssam
1514183257Ssam/*
1515183254Ssam * Parse and update HT-related state extracted from
1516183254Ssam * the HT cap and info ie's.
1517183254Ssam */
1518183254Ssamvoid
1519183254Ssamieee80211_ht_updateparams(struct ieee80211_node *ni,
1520183254Ssam	const uint8_t *htcapie, const uint8_t *htinfoie)
1521183254Ssam{
1522183254Ssam	struct ieee80211vap *vap = ni->ni_vap;
1523183254Ssam	const struct ieee80211_ie_htinfo *htinfo;
1524183254Ssam	int htflags;
1525183254Ssam
1526183254Ssam	ieee80211_parse_htcap(ni, htcapie);
1527183255Ssam	if (vap->iv_htcaps & IEEE80211_HTCAP_SMPS)
1528183255Ssam		htcap_update_mimo_ps(ni);
1529183257Ssam	htcap_update_shortgi(ni);
1530183254Ssam
1531183254Ssam	if (htinfoie[0] == IEEE80211_ELEMID_VENDOR)
1532183254Ssam		htinfoie += 4;
1533183254Ssam	htinfo = (const struct ieee80211_ie_htinfo *) htinfoie;
1534183254Ssam	htinfo_parse(ni, htinfo);
1535183254Ssam
1536193655Ssam	htflags = (vap->iv_flags_ht & IEEE80211_FHT_HT) ?
1537183254Ssam	    IEEE80211_CHAN_HT20 : 0;
1538183254Ssam	/* NB: honor operating mode constraint */
1539183254Ssam	if ((htinfo->hi_byte1 & IEEE80211_HTINFO_TXWIDTH_2040) &&
1540193655Ssam	    (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)) {
1541183254Ssam		if (ni->ni_ht2ndchan == IEEE80211_HTINFO_2NDCHAN_ABOVE)
1542183254Ssam			htflags = IEEE80211_CHAN_HT40U;
1543183254Ssam		else if (ni->ni_ht2ndchan == IEEE80211_HTINFO_2NDCHAN_BELOW)
1544183254Ssam			htflags = IEEE80211_CHAN_HT40D;
1545183254Ssam	}
1546183254Ssam	htinfo_update_chw(ni, htflags);
1547183256Ssam
1548183256Ssam	if ((htinfo->hi_byte1 & IEEE80211_HTINFO_RIFSMODE_PERM) &&
1549193655Ssam	    (vap->iv_flags_ht & IEEE80211_FHT_RIFS))
1550183256Ssam		ni->ni_flags |= IEEE80211_NODE_RIFS;
1551183256Ssam	else
1552183256Ssam		ni->ni_flags &= ~IEEE80211_NODE_RIFS;
1553183254Ssam}
1554183254Ssam
1555183254Ssam/*
1556183254Ssam * Parse and update HT-related state extracted from the HT cap ie
1557183254Ssam * for a station joining an HT BSS.
1558183254Ssam */
1559183254Ssamvoid
1560183254Ssamieee80211_ht_updatehtcap(struct ieee80211_node *ni, const uint8_t *htcapie)
1561183254Ssam{
1562183254Ssam	struct ieee80211vap *vap = ni->ni_vap;
1563183254Ssam	int htflags;
1564183254Ssam
1565183254Ssam	ieee80211_parse_htcap(ni, htcapie);
1566183255Ssam	if (vap->iv_htcaps & IEEE80211_HTCAP_SMPS)
1567183255Ssam		htcap_update_mimo_ps(ni);
1568183257Ssam	htcap_update_shortgi(ni);
1569183254Ssam
1570183254Ssam	/* NB: honor operating mode constraint */
1571219606Sbschmidt	/* XXX 40 MHz intolerant */
1572193655Ssam	htflags = (vap->iv_flags_ht & IEEE80211_FHT_HT) ?
1573183254Ssam	    IEEE80211_CHAN_HT20 : 0;
1574183254Ssam	if ((ni->ni_htcap & IEEE80211_HTCAP_CHWIDTH40) &&
1575193655Ssam	    (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)) {
1576183254Ssam		if (IEEE80211_IS_CHAN_HT40U(vap->iv_bss->ni_chan))
1577183254Ssam			htflags = IEEE80211_CHAN_HT40U;
1578183254Ssam		else if (IEEE80211_IS_CHAN_HT40D(vap->iv_bss->ni_chan))
1579183254Ssam			htflags = IEEE80211_CHAN_HT40D;
1580183254Ssam	}
1581183254Ssam	htinfo_update_chw(ni, htflags);
1582183254Ssam}
1583183254Ssam
1584183254Ssam/*
1585170530Ssam * Install received HT rate set by parsing the HT cap ie.
1586170530Ssam */
1587170530Ssamint
1588170530Ssamieee80211_setup_htrates(struct ieee80211_node *ni, const uint8_t *ie, int flags)
1589170530Ssam{
1590219602Sbschmidt	struct ieee80211com *ic = ni->ni_ic;
1591178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1592170530Ssam	const struct ieee80211_ie_htcap *htcap;
1593170530Ssam	struct ieee80211_htrateset *rs;
1594219602Sbschmidt	int i, maxequalmcs, maxunequalmcs;
1595170530Ssam
1596219602Sbschmidt	maxequalmcs = ic->ic_txstream * 8 - 1;
1597219602Sbschmidt	if (ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL) {
1598219602Sbschmidt		if (ic->ic_txstream >= 2)
1599219602Sbschmidt			maxunequalmcs = 38;
1600219602Sbschmidt		if (ic->ic_txstream >= 3)
1601219602Sbschmidt			maxunequalmcs = 52;
1602219602Sbschmidt		if (ic->ic_txstream >= 4)
1603219602Sbschmidt			maxunequalmcs = 76;
1604219602Sbschmidt	} else
1605219602Sbschmidt		maxunequalmcs = 0;
1606219602Sbschmidt
1607170530Ssam	rs = &ni->ni_htrates;
1608170530Ssam	memset(rs, 0, sizeof(*rs));
1609170530Ssam	if (ie != NULL) {
1610170530Ssam		if (ie[0] == IEEE80211_ELEMID_VENDOR)
1611170530Ssam			ie += 4;
1612170530Ssam		htcap = (const struct ieee80211_ie_htcap *) ie;
1613170530Ssam		for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++) {
1614170530Ssam			if (isclr(htcap->hc_mcsset, i))
1615170530Ssam				continue;
1616170530Ssam			if (rs->rs_nrates == IEEE80211_HTRATE_MAXSIZE) {
1617178354Ssam				IEEE80211_NOTE(vap,
1618170530Ssam				    IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
1619170530Ssam				    "WARNING, HT rate set too large; only "
1620170530Ssam				    "using %u rates", IEEE80211_HTRATE_MAXSIZE);
1621178354Ssam				vap->iv_stats.is_rx_rstoobig++;
1622170530Ssam				break;
1623170530Ssam			}
1624219602Sbschmidt			if (i <= 31 && i > maxequalmcs)
1625219602Sbschmidt				continue;
1626219602Sbschmidt			if (i == 32 &&
1627219602Sbschmidt			    (ic->ic_htcaps & IEEE80211_HTC_TXMCS32) == 0)
1628219602Sbschmidt				continue;
1629219602Sbschmidt			if (i > 32 && i > maxunequalmcs)
1630219602Sbschmidt				continue;
1631170530Ssam			rs->rs_rates[rs->rs_nrates++] = i;
1632170530Ssam		}
1633170530Ssam	}
1634170530Ssam	return ieee80211_fix_rate(ni, (struct ieee80211_rateset *) rs, flags);
1635170530Ssam}
1636170530Ssam
1637170530Ssam/*
1638170530Ssam * Mark rates in a node's HT rate set as basic according
1639170530Ssam * to the information in the supplied HT info ie.
1640170530Ssam */
1641170530Ssamvoid
1642170530Ssamieee80211_setup_basic_htrates(struct ieee80211_node *ni, const uint8_t *ie)
1643170530Ssam{
1644170530Ssam	const struct ieee80211_ie_htinfo *htinfo;
1645170530Ssam	struct ieee80211_htrateset *rs;
1646170530Ssam	int i, j;
1647170530Ssam
1648170530Ssam	if (ie[0] == IEEE80211_ELEMID_VENDOR)
1649170530Ssam		ie += 4;
1650170530Ssam	htinfo = (const struct ieee80211_ie_htinfo *) ie;
1651170530Ssam	rs = &ni->ni_htrates;
1652170530Ssam	if (rs->rs_nrates == 0) {
1653178354Ssam		IEEE80211_NOTE(ni->ni_vap,
1654170530Ssam		    IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
1655170530Ssam		    "%s", "WARNING, empty HT rate set");
1656170530Ssam		return;
1657170530Ssam	}
1658170530Ssam	for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++) {
1659170530Ssam		if (isclr(htinfo->hi_basicmcsset, i))
1660170530Ssam			continue;
1661170530Ssam		for (j = 0; j < rs->rs_nrates; j++)
1662170530Ssam			if ((rs->rs_rates[j] & IEEE80211_RATE_VAL) == i)
1663170530Ssam				rs->rs_rates[j] |= IEEE80211_RATE_BASIC;
1664170530Ssam	}
1665170530Ssam}
1666170530Ssam
1667170530Ssamstatic void
1668184280Ssamampdu_tx_setup(struct ieee80211_tx_ampdu *tap)
1669184280Ssam{
1670184280Ssam	callout_init(&tap->txa_timer, CALLOUT_MPSAFE);
1671184280Ssam	tap->txa_flags |= IEEE80211_AGGR_SETUP;
1672184280Ssam}
1673184280Ssam
1674184280Ssamstatic void
1675184280Ssamampdu_tx_stop(struct ieee80211_tx_ampdu *tap)
1676184280Ssam{
1677184280Ssam	struct ieee80211_node *ni = tap->txa_ni;
1678184280Ssam	struct ieee80211com *ic = ni->ni_ic;
1679184280Ssam
1680184280Ssam	KASSERT(tap->txa_flags & IEEE80211_AGGR_SETUP,
1681184280Ssam	    ("txa_flags 0x%x ac %d", tap->txa_flags, tap->txa_ac));
1682184280Ssam
1683184280Ssam	/*
1684184280Ssam	 * Stop BA stream if setup so driver has a chance
1685184280Ssam	 * to reclaim any resources it might have allocated.
1686184280Ssam	 */
1687184280Ssam	ic->ic_addba_stop(ni, tap);
1688184280Ssam	/*
1689184280Ssam	 * Stop any pending BAR transmit.
1690184280Ssam	 */
1691184280Ssam	bar_stop_timer(tap);
1692184280Ssam
1693184280Ssam	tap->txa_lastsample = 0;
1694184280Ssam	tap->txa_avgpps = 0;
1695184280Ssam	/* NB: clearing NAK means we may re-send ADDBA */
1696184280Ssam	tap->txa_flags &= ~(IEEE80211_AGGR_SETUP | IEEE80211_AGGR_NAK);
1697184280Ssam}
1698184280Ssam
1699223331Sadrian/*
1700223331Sadrian * ADDBA response timeout.
1701223331Sadrian *
1702223331Sadrian * If software aggregation and per-TID queue management was done here,
1703223331Sadrian * that queue would be unpaused after the ADDBA timeout occurs.
1704223331Sadrian */
1705184280Ssamstatic void
1706170530Ssamaddba_timeout(void *arg)
1707170530Ssam{
1708170530Ssam	struct ieee80211_tx_ampdu *tap = arg;
1709223331Sadrian	struct ieee80211_node *ni = tap->txa_ni;
1710223331Sadrian	struct ieee80211com *ic = ni->ni_ic;
1711170530Ssam
1712170530Ssam	/* XXX ? */
1713170530Ssam	tap->txa_flags &= ~IEEE80211_AGGR_XCHGPEND;
1714170530Ssam	tap->txa_attempts++;
1715223331Sadrian	ic->ic_addba_response_timeout(ni, tap);
1716170530Ssam}
1717170530Ssam
1718170530Ssamstatic void
1719170530Ssamaddba_start_timeout(struct ieee80211_tx_ampdu *tap)
1720170530Ssam{
1721170530Ssam	/* XXX use CALLOUT_PENDING instead? */
1722178354Ssam	callout_reset(&tap->txa_timer, ieee80211_addba_timeout,
1723170530Ssam	    addba_timeout, tap);
1724170530Ssam	tap->txa_flags |= IEEE80211_AGGR_XCHGPEND;
1725178354Ssam	tap->txa_nextrequest = ticks + ieee80211_addba_timeout;
1726170530Ssam}
1727170530Ssam
1728170530Ssamstatic void
1729170530Ssamaddba_stop_timeout(struct ieee80211_tx_ampdu *tap)
1730170530Ssam{
1731170530Ssam	/* XXX use CALLOUT_PENDING instead? */
1732170530Ssam	if (tap->txa_flags & IEEE80211_AGGR_XCHGPEND) {
1733170530Ssam		callout_stop(&tap->txa_timer);
1734170530Ssam		tap->txa_flags &= ~IEEE80211_AGGR_XCHGPEND;
1735170530Ssam	}
1736170530Ssam}
1737170530Ssam
1738223331Sadrianstatic void
1739223331Sadriannull_addba_response_timeout(struct ieee80211_node *ni,
1740223331Sadrian    struct ieee80211_tx_ampdu *tap)
1741223331Sadrian{
1742223331Sadrian}
1743223331Sadrian
1744170530Ssam/*
1745170530Ssam * Default method for requesting A-MPDU tx aggregation.
1746170530Ssam * We setup the specified state block and start a timer
1747170530Ssam * to wait for an ADDBA response frame.
1748170530Ssam */
1749170530Ssamstatic int
1750170530Ssamieee80211_addba_request(struct ieee80211_node *ni,
1751170530Ssam	struct ieee80211_tx_ampdu *tap,
1752170530Ssam	int dialogtoken, int baparamset, int batimeout)
1753170530Ssam{
1754170530Ssam	int bufsiz;
1755170530Ssam
1756170530Ssam	/* XXX locking */
1757170530Ssam	tap->txa_token = dialogtoken;
1758170530Ssam	tap->txa_flags |= IEEE80211_AGGR_IMMEDIATE;
1759170530Ssam	bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
1760170530Ssam	tap->txa_wnd = (bufsiz == 0) ?
1761170530Ssam	    IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
1762170530Ssam	addba_start_timeout(tap);
1763170530Ssam	return 1;
1764170530Ssam}
1765170530Ssam
1766170530Ssam/*
1767170530Ssam * Default method for processing an A-MPDU tx aggregation
1768170530Ssam * response.  We shutdown any pending timer and update the
1769170530Ssam * state block according to the reply.
1770170530Ssam */
1771170530Ssamstatic int
1772170530Ssamieee80211_addba_response(struct ieee80211_node *ni,
1773170530Ssam	struct ieee80211_tx_ampdu *tap,
1774170530Ssam	int status, int baparamset, int batimeout)
1775170530Ssam{
1776184280Ssam	int bufsiz, tid;
1777170530Ssam
1778170530Ssam	/* XXX locking */
1779170530Ssam	addba_stop_timeout(tap);
1780170530Ssam	if (status == IEEE80211_STATUS_SUCCESS) {
1781170530Ssam		bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
1782170530Ssam		/* XXX override our request? */
1783170530Ssam		tap->txa_wnd = (bufsiz == 0) ?
1784170530Ssam		    IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
1785184280Ssam		/* XXX AC/TID */
1786184280Ssam		tid = MS(baparamset, IEEE80211_BAPS_TID);
1787170530Ssam		tap->txa_flags |= IEEE80211_AGGR_RUNNING;
1788184280Ssam		tap->txa_attempts = 0;
1789173273Ssam	} else {
1790173273Ssam		/* mark tid so we don't try again */
1791173273Ssam		tap->txa_flags |= IEEE80211_AGGR_NAK;
1792170530Ssam	}
1793170530Ssam	return 1;
1794170530Ssam}
1795170530Ssam
1796170530Ssam/*
1797170530Ssam * Default method for stopping A-MPDU tx aggregation.
1798170530Ssam * Any timer is cleared and we drain any pending frames.
1799170530Ssam */
1800170530Ssamstatic void
1801170530Ssamieee80211_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
1802170530Ssam{
1803170530Ssam	/* XXX locking */
1804170530Ssam	addba_stop_timeout(tap);
1805170530Ssam	if (tap->txa_flags & IEEE80211_AGGR_RUNNING) {
1806182830Ssam		/* XXX clear aggregation queue */
1807170530Ssam		tap->txa_flags &= ~IEEE80211_AGGR_RUNNING;
1808170530Ssam	}
1809170530Ssam	tap->txa_attempts = 0;
1810170530Ssam}
1811170530Ssam
1812170530Ssam/*
1813170530Ssam * Process a received action frame using the default aggregation
1814170530Ssam * policy.  We intercept ADDBA-related frames and use them to
1815170530Ssam * update our aggregation state.  All other frames are passed up
1816170530Ssam * for processing by ieee80211_recv_action.
1817170530Ssam */
1818195377Ssamstatic int
1819195377Ssamht_recv_action_ba_addba_request(struct ieee80211_node *ni,
1820195377Ssam	const struct ieee80211_frame *wh,
1821170530Ssam	const uint8_t *frm, const uint8_t *efrm)
1822170530Ssam{
1823170530Ssam	struct ieee80211com *ic = ni->ni_ic;
1824178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1825170530Ssam	struct ieee80211_rx_ampdu *rap;
1826195377Ssam	uint8_t dialogtoken;
1827195377Ssam	uint16_t baparamset, batimeout, baseqctl;
1828205277Srpaulo	uint16_t args[5];
1829195377Ssam	int tid;
1830170530Ssam
1831195377Ssam	dialogtoken = frm[2];
1832195377Ssam	baparamset = LE_READ_2(frm+3);
1833195377Ssam	batimeout = LE_READ_2(frm+5);
1834195377Ssam	baseqctl = LE_READ_2(frm+7);
1835170530Ssam
1836195377Ssam	tid = MS(baparamset, IEEE80211_BAPS_TID);
1837170530Ssam
1838195377Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1839195377Ssam	    "recv ADDBA request: dialogtoken %u baparamset 0x%x "
1840195377Ssam	    "(tid %d bufsiz %d) batimeout %d baseqctl %d:%d",
1841195377Ssam	    dialogtoken, baparamset,
1842195377Ssam	    tid, MS(baparamset, IEEE80211_BAPS_BUFSIZ),
1843195377Ssam	    batimeout,
1844195377Ssam	    MS(baseqctl, IEEE80211_BASEQ_START),
1845195377Ssam	    MS(baseqctl, IEEE80211_BASEQ_FRAG));
1846170530Ssam
1847195377Ssam	rap = &ni->ni_rx_ampdu[tid];
1848170530Ssam
1849195377Ssam	/* Send ADDBA response */
1850195377Ssam	args[0] = dialogtoken;
1851195377Ssam	/*
1852195377Ssam	 * NB: We ack only if the sta associated with HT and
1853195377Ssam	 * the ap is configured to do AMPDU rx (the latter
1854195377Ssam	 * violates the 11n spec and is mostly for testing).
1855195377Ssam	 */
1856195377Ssam	if ((ni->ni_flags & IEEE80211_NODE_AMPDU_RX) &&
1857195377Ssam	    (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_RX)) {
1858195377Ssam		/* XXX handle ampdu_rx_start failure */
1859195377Ssam		ic->ic_ampdu_rx_start(ni, rap,
1860195377Ssam		    baparamset, batimeout, baseqctl);
1861170530Ssam
1862195377Ssam		args[1] = IEEE80211_STATUS_SUCCESS;
1863195377Ssam	} else {
1864195377Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1865195377Ssam		    ni, "reject ADDBA request: %s",
1866195377Ssam		    ni->ni_flags & IEEE80211_NODE_AMPDU_RX ?
1867195377Ssam		       "administratively disabled" :
1868195377Ssam		       "not negotiated for station");
1869195377Ssam		vap->iv_stats.is_addba_reject++;
1870195377Ssam		args[1] = IEEE80211_STATUS_UNSPECIFIED;
1871195377Ssam	}
1872195377Ssam	/* XXX honor rap flags? */
1873195377Ssam	args[2] = IEEE80211_BAPS_POLICY_IMMEDIATE
1874195377Ssam		| SM(tid, IEEE80211_BAPS_TID)
1875195377Ssam		| SM(rap->rxa_wnd, IEEE80211_BAPS_BUFSIZ)
1876195377Ssam		;
1877195377Ssam	args[3] = 0;
1878205277Srpaulo	args[4] = 0;
1879195377Ssam	ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
1880195377Ssam		IEEE80211_ACTION_BA_ADDBA_RESPONSE, args);
1881195377Ssam	return 0;
1882195377Ssam}
1883170530Ssam
1884195377Ssamstatic int
1885195377Ssamht_recv_action_ba_addba_response(struct ieee80211_node *ni,
1886195377Ssam	const struct ieee80211_frame *wh,
1887195377Ssam	const uint8_t *frm, const uint8_t *efrm)
1888195377Ssam{
1889195377Ssam	struct ieee80211com *ic = ni->ni_ic;
1890195377Ssam	struct ieee80211vap *vap = ni->ni_vap;
1891195377Ssam	struct ieee80211_tx_ampdu *tap;
1892195377Ssam	uint8_t dialogtoken, policy;
1893195377Ssam	uint16_t baparamset, batimeout, code;
1894195377Ssam	int tid, ac, bufsiz;
1895170530Ssam
1896195377Ssam	dialogtoken = frm[2];
1897195377Ssam	code = LE_READ_2(frm+3);
1898195377Ssam	baparamset = LE_READ_2(frm+5);
1899195377Ssam	tid = MS(baparamset, IEEE80211_BAPS_TID);
1900195377Ssam	bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
1901195377Ssam	policy = MS(baparamset, IEEE80211_BAPS_POLICY);
1902195377Ssam	batimeout = LE_READ_2(frm+7);
1903195377Ssam
1904195377Ssam	ac = TID_TO_WME_AC(tid);
1905195377Ssam	tap = &ni->ni_tx_ampdu[ac];
1906195377Ssam	if ((tap->txa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
1907195377Ssam		IEEE80211_DISCARD_MAC(vap,
1908195377Ssam		    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1909195377Ssam		    ni->ni_macaddr, "ADDBA response",
1910195377Ssam		    "no pending ADDBA, tid %d dialogtoken %u "
1911195377Ssam		    "code %d", tid, dialogtoken, code);
1912195377Ssam		vap->iv_stats.is_addba_norequest++;
1913195377Ssam		return 0;
1914195377Ssam	}
1915195377Ssam	if (dialogtoken != tap->txa_token) {
1916195377Ssam		IEEE80211_DISCARD_MAC(vap,
1917195377Ssam		    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1918195377Ssam		    ni->ni_macaddr, "ADDBA response",
1919195377Ssam		    "dialogtoken mismatch: waiting for %d, "
1920195377Ssam		    "received %d, tid %d code %d",
1921195377Ssam		    tap->txa_token, dialogtoken, tid, code);
1922195377Ssam		vap->iv_stats.is_addba_badtoken++;
1923195377Ssam		return 0;
1924195377Ssam	}
1925195377Ssam	/* NB: assumes IEEE80211_AGGR_IMMEDIATE is 1 */
1926195377Ssam	if (policy != (tap->txa_flags & IEEE80211_AGGR_IMMEDIATE)) {
1927195377Ssam		IEEE80211_DISCARD_MAC(vap,
1928195377Ssam		    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1929195377Ssam		    ni->ni_macaddr, "ADDBA response",
1930195377Ssam		    "policy mismatch: expecting %s, "
1931195377Ssam		    "received %s, tid %d code %d",
1932195377Ssam		    tap->txa_flags & IEEE80211_AGGR_IMMEDIATE,
1933195377Ssam		    policy, tid, code);
1934195377Ssam		vap->iv_stats.is_addba_badpolicy++;
1935195377Ssam		return 0;
1936195377Ssam	}
1937182829Ssam#if 0
1938195377Ssam	/* XXX we take MIN in ieee80211_addba_response */
1939195377Ssam	if (bufsiz > IEEE80211_AGGR_BAWMAX) {
1940195377Ssam		IEEE80211_DISCARD_MAC(vap,
1941195377Ssam		    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1942195377Ssam		    ni->ni_macaddr, "ADDBA response",
1943195377Ssam		    "BA window too large: max %d, "
1944195377Ssam		    "received %d, tid %d code %d",
1945195377Ssam		    bufsiz, IEEE80211_AGGR_BAWMAX, tid, code);
1946195377Ssam		vap->iv_stats.is_addba_badbawinsize++;
1947195377Ssam		return 0;
1948195377Ssam	}
1949182829Ssam#endif
1950195377Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1951195377Ssam	    "recv ADDBA response: dialogtoken %u code %d "
1952195377Ssam	    "baparamset 0x%x (tid %d bufsiz %d) batimeout %d",
1953195377Ssam	    dialogtoken, code, baparamset, tid, bufsiz,
1954195377Ssam	    batimeout);
1955195377Ssam	ic->ic_addba_response(ni, tap, code, baparamset, batimeout);
1956195377Ssam	return 0;
1957195377Ssam}
1958170530Ssam
1959195377Ssamstatic int
1960195377Ssamht_recv_action_ba_delba(struct ieee80211_node *ni,
1961195377Ssam	const struct ieee80211_frame *wh,
1962195377Ssam	const uint8_t *frm, const uint8_t *efrm)
1963195377Ssam{
1964195377Ssam	struct ieee80211com *ic = ni->ni_ic;
1965195377Ssam	struct ieee80211_rx_ampdu *rap;
1966195377Ssam	struct ieee80211_tx_ampdu *tap;
1967195377Ssam	uint16_t baparamset, code;
1968195377Ssam	int tid, ac;
1969170530Ssam
1970195377Ssam	baparamset = LE_READ_2(frm+2);
1971195377Ssam	code = LE_READ_2(frm+4);
1972170530Ssam
1973195377Ssam	tid = MS(baparamset, IEEE80211_DELBAPS_TID);
1974170530Ssam
1975195377Ssam	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1976195377Ssam	    "recv DELBA: baparamset 0x%x (tid %d initiator %d) "
1977195377Ssam	    "code %d", baparamset, tid,
1978195377Ssam	    MS(baparamset, IEEE80211_DELBAPS_INIT), code);
1979195377Ssam
1980195377Ssam	if ((baparamset & IEEE80211_DELBAPS_INIT) == 0) {
1981195377Ssam		ac = TID_TO_WME_AC(tid);
1982195377Ssam		tap = &ni->ni_tx_ampdu[ac];
1983195377Ssam		ic->ic_addba_stop(ni, tap);
1984195377Ssam	} else {
1985195377Ssam		rap = &ni->ni_rx_ampdu[tid];
1986195377Ssam		ic->ic_ampdu_rx_stop(ni, rap);
1987170530Ssam	}
1988195377Ssam	return 0;
1989170530Ssam}
1990170530Ssam
1991195377Ssamstatic int
1992195377Ssamht_recv_action_ht_txchwidth(struct ieee80211_node *ni,
1993195377Ssam	const struct ieee80211_frame *wh,
1994170530Ssam	const uint8_t *frm, const uint8_t *efrm)
1995170530Ssam{
1996170530Ssam	int chw;
1997170530Ssam
1998195377Ssam	chw = (frm[2] == IEEE80211_A_HT_TXCHWIDTH_2040) ? 40 : 20;
1999195377Ssam
2000195377Ssam	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2001195377Ssam	    "%s: HT txchwidth, width %d%s",
2002195377Ssam	    __func__, chw, ni->ni_chw != chw ? "*" : "");
2003195377Ssam	if (chw != ni->ni_chw) {
2004195377Ssam		ni->ni_chw = chw;
2005195377Ssam		/* XXX notify on change */
2006170530Ssam	}
2007195377Ssam	return 0;
2008170530Ssam}
2009170530Ssam
2010195377Ssamstatic int
2011195377Ssamht_recv_action_ht_mimopwrsave(struct ieee80211_node *ni,
2012195377Ssam	const struct ieee80211_frame *wh,
2013195377Ssam	const uint8_t *frm, const uint8_t *efrm)
2014195377Ssam{
2015195377Ssam	const struct ieee80211_action_ht_mimopowersave *mps =
2016195377Ssam	    (const struct ieee80211_action_ht_mimopowersave *) frm;
2017195377Ssam
2018195377Ssam	/* XXX check iv_htcaps */
2019195377Ssam	if (mps->am_control & IEEE80211_A_HT_MIMOPWRSAVE_ENA)
2020195377Ssam		ni->ni_flags |= IEEE80211_NODE_MIMO_PS;
2021195377Ssam	else
2022195377Ssam		ni->ni_flags &= ~IEEE80211_NODE_MIMO_PS;
2023195377Ssam	if (mps->am_control & IEEE80211_A_HT_MIMOPWRSAVE_MODE)
2024195377Ssam		ni->ni_flags |= IEEE80211_NODE_MIMO_RTS;
2025195377Ssam	else
2026195377Ssam		ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS;
2027195377Ssam	/* XXX notify on change */
2028195377Ssam	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2029195377Ssam	    "%s: HT MIMO PS (%s%s)", __func__,
2030195377Ssam	    (ni->ni_flags & IEEE80211_NODE_MIMO_PS) ?  "on" : "off",
2031195377Ssam	    (ni->ni_flags & IEEE80211_NODE_MIMO_RTS) ?  "+rts" : ""
2032195377Ssam	);
2033195377Ssam	return 0;
2034195377Ssam}
2035195377Ssam
2036170530Ssam/*
2037170530Ssam * Transmit processing.
2038170530Ssam */
2039170530Ssam
2040170530Ssam/*
2041178354Ssam * Check if A-MPDU should be requested/enabled for a stream.
2042178354Ssam * We require a traffic rate above a per-AC threshold and we
2043178354Ssam * also handle backoff from previous failed attempts.
2044178354Ssam *
2045178354Ssam * Drivers may override this method to bring in information
2046178354Ssam * such as link state conditions in making the decision.
2047178354Ssam */
2048178354Ssamstatic int
2049178354Ssamieee80211_ampdu_enable(struct ieee80211_node *ni,
2050178354Ssam	struct ieee80211_tx_ampdu *tap)
2051178354Ssam{
2052178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2053178354Ssam
2054178354Ssam	if (tap->txa_avgpps < vap->iv_ampdu_mintraffic[tap->txa_ac])
2055178354Ssam		return 0;
2056178354Ssam	/* XXX check rssi? */
2057178354Ssam	if (tap->txa_attempts >= ieee80211_addba_maxtries &&
2058178354Ssam	    ticks < tap->txa_nextrequest) {
2059178354Ssam		/*
2060178354Ssam		 * Don't retry too often; txa_nextrequest is set
2061178354Ssam		 * to the minimum interval we'll retry after
2062178354Ssam		 * ieee80211_addba_maxtries failed attempts are made.
2063178354Ssam		 */
2064178354Ssam		return 0;
2065178354Ssam	}
2066178354Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
2067183249Ssam	    "enable AMPDU on %s, avgpps %d pkts %d",
2068183249Ssam	    ieee80211_wme_acnames[tap->txa_ac], tap->txa_avgpps, tap->txa_pkts);
2069178354Ssam	return 1;
2070178354Ssam}
2071178354Ssam
2072178354Ssam/*
2073170530Ssam * Request A-MPDU tx aggregation.  Setup local state and
2074170530Ssam * issue an ADDBA request.  BA use will only happen after
2075170530Ssam * the other end replies with ADDBA response.
2076170530Ssam */
2077170530Ssamint
2078170530Ssamieee80211_ampdu_request(struct ieee80211_node *ni,
2079170530Ssam	struct ieee80211_tx_ampdu *tap)
2080170530Ssam{
2081170530Ssam	struct ieee80211com *ic = ni->ni_ic;
2082205277Srpaulo	uint16_t args[5];
2083170530Ssam	int tid, dialogtoken;
2084170530Ssam	static int tokens = 0;	/* XXX */
2085170530Ssam
2086170530Ssam	/* XXX locking */
2087170530Ssam	if ((tap->txa_flags & IEEE80211_AGGR_SETUP) == 0) {
2088170530Ssam		/* do deferred setup of state */
2089184280Ssam		ampdu_tx_setup(tap);
2090170530Ssam	}
2091173273Ssam	/* XXX hack for not doing proper locking */
2092173273Ssam	tap->txa_flags &= ~IEEE80211_AGGR_NAK;
2093173273Ssam
2094170530Ssam	dialogtoken = (tokens+1) % 63;		/* XXX */
2095183245Ssam	tid = WME_AC_TO_TID(tap->txa_ac);
2096183245Ssam	tap->txa_start = ni->ni_txseqs[tid];
2097170530Ssam
2098170530Ssam	args[0] = dialogtoken;
2099205277Srpaulo	args[1] = 0;	/* NB: status code not used */
2100205277Srpaulo	args[2]	= IEEE80211_BAPS_POLICY_IMMEDIATE
2101170530Ssam		| SM(tid, IEEE80211_BAPS_TID)
2102170530Ssam		| SM(IEEE80211_AGGR_BAWMAX, IEEE80211_BAPS_BUFSIZ)
2103170530Ssam		;
2104205277Srpaulo	args[3] = 0;	/* batimeout */
2105170530Ssam	/* NB: do first so there's no race against reply */
2106205277Srpaulo	if (!ic->ic_addba_request(ni, tap, dialogtoken, args[2], args[3])) {
2107170530Ssam		/* unable to setup state, don't make request */
2108178354Ssam		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2109173273Ssam		    ni, "%s: could not setup BA stream for AC %d",
2110173273Ssam		    __func__, tap->txa_ac);
2111173273Ssam		/* defer next try so we don't slam the driver with requests */
2112178354Ssam		tap->txa_attempts = ieee80211_addba_maxtries;
2113178354Ssam		/* NB: check in case driver wants to override */
2114178354Ssam		if (tap->txa_nextrequest <= ticks)
2115178354Ssam			tap->txa_nextrequest = ticks + ieee80211_addba_backoff;
2116170530Ssam		return 0;
2117170530Ssam	}
2118170530Ssam	tokens = dialogtoken;			/* allocate token */
2119183245Ssam	/* NB: after calling ic_addba_request so driver can set txa_start */
2120205277Srpaulo	args[4] = SM(tap->txa_start, IEEE80211_BASEQ_START)
2121178953Ssam		| SM(0, IEEE80211_BASEQ_FRAG)
2122178953Ssam		;
2123170530Ssam	return ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
2124170530Ssam		IEEE80211_ACTION_BA_ADDBA_REQUEST, args);
2125170530Ssam}
2126170530Ssam
2127170530Ssam/*
2128173273Ssam * Terminate an AMPDU tx stream.  State is reclaimed
2129173273Ssam * and the peer notified with a DelBA Action frame.
2130173273Ssam */
2131173273Ssamvoid
2132183250Ssamieee80211_ampdu_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
2133183250Ssam	int reason)
2134173273Ssam{
2135173273Ssam	struct ieee80211com *ic = ni->ni_ic;
2136178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2137173273Ssam	uint16_t args[4];
2138173273Ssam
2139173273Ssam	/* XXX locking */
2140184280Ssam	tap->txa_flags &= ~IEEE80211_AGGR_BARPEND;
2141173273Ssam	if (IEEE80211_AMPDU_RUNNING(tap)) {
2142178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2143183250Ssam		    ni, "%s: stop BA stream for AC %d (reason %d)",
2144183250Ssam		    __func__, tap->txa_ac, reason);
2145178354Ssam		vap->iv_stats.is_ampdu_stop++;
2146173273Ssam
2147173273Ssam		ic->ic_addba_stop(ni, tap);
2148173273Ssam		args[0] = WME_AC_TO_TID(tap->txa_ac);
2149173273Ssam		args[1] = IEEE80211_DELBAPS_INIT;
2150183250Ssam		args[2] = reason;			/* XXX reason code */
2151195377Ssam		ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
2152173273Ssam			IEEE80211_ACTION_BA_DELBA, args);
2153173273Ssam	} else {
2154178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2155183250Ssam		    ni, "%s: BA stream for AC %d not running (reason %d)",
2156183250Ssam		    __func__, tap->txa_ac, reason);
2157178354Ssam		vap->iv_stats.is_ampdu_stop_failed++;
2158173273Ssam	}
2159173273Ssam}
2160173273Ssam
2161184280Ssamstatic void
2162184280Ssambar_timeout(void *arg)
2163184280Ssam{
2164184280Ssam	struct ieee80211_tx_ampdu *tap = arg;
2165184280Ssam	struct ieee80211_node *ni = tap->txa_ni;
2166184280Ssam
2167184280Ssam	KASSERT((tap->txa_flags & IEEE80211_AGGR_XCHGPEND) == 0,
2168184280Ssam	    ("bar/addba collision, flags 0x%x", tap->txa_flags));
2169184280Ssam
2170184280Ssam	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2171184280Ssam	    ni, "%s: tid %u flags 0x%x attempts %d", __func__,
2172184280Ssam	    tap->txa_ac, tap->txa_flags, tap->txa_attempts);
2173184280Ssam
2174184280Ssam	/* guard against race with bar_tx_complete */
2175184280Ssam	if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) == 0)
2176184280Ssam		return;
2177184280Ssam	/* XXX ? */
2178184280Ssam	if (tap->txa_attempts >= ieee80211_bar_maxtries)
2179184280Ssam		ieee80211_ampdu_stop(ni, tap, IEEE80211_REASON_TIMEOUT);
2180184280Ssam	else
2181184280Ssam		ieee80211_send_bar(ni, tap, tap->txa_seqpending);
2182184280Ssam}
2183184280Ssam
2184184280Ssamstatic void
2185184280Ssambar_start_timer(struct ieee80211_tx_ampdu *tap)
2186184280Ssam{
2187184280Ssam	callout_reset(&tap->txa_timer, ieee80211_bar_timeout, bar_timeout, tap);
2188184280Ssam}
2189184280Ssam
2190184280Ssamstatic void
2191184280Ssambar_stop_timer(struct ieee80211_tx_ampdu *tap)
2192184280Ssam{
2193184280Ssam	callout_stop(&tap->txa_timer);
2194184280Ssam}
2195184280Ssam
2196184280Ssamstatic void
2197184280Ssambar_tx_complete(struct ieee80211_node *ni, void *arg, int status)
2198184280Ssam{
2199184280Ssam	struct ieee80211_tx_ampdu *tap = arg;
2200184280Ssam
2201184280Ssam	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2202184280Ssam	    ni, "%s: tid %u flags 0x%x pending %d status %d",
2203184280Ssam	    __func__, tap->txa_ac, tap->txa_flags,
2204184280Ssam	    callout_pending(&tap->txa_timer), status);
2205184280Ssam
2206184280Ssam	/* XXX locking */
2207184280Ssam	if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) &&
2208184280Ssam	    callout_pending(&tap->txa_timer)) {
2209184280Ssam		struct ieee80211com *ic = ni->ni_ic;
2210184280Ssam
2211224907Sadrian		if (status == 0)		/* ACK'd */
2212184280Ssam			bar_stop_timer(tap);
2213184280Ssam		ic->ic_bar_response(ni, tap, status);
2214184280Ssam		/* NB: just let timer expire so we pace requests */
2215184280Ssam	}
2216184280Ssam}
2217184280Ssam
2218184280Ssamstatic void
2219184280Ssamieee80211_bar_response(struct ieee80211_node *ni,
2220184280Ssam	struct ieee80211_tx_ampdu *tap, int status)
2221184280Ssam{
2222184280Ssam
2223224907Sadrian	if (status == 0) {		/* got ACK */
2224184280Ssam		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2225184280Ssam		    ni, "BAR moves BA win <%u:%u> (%u frames) txseq %u tid %u",
2226184280Ssam		    tap->txa_start,
2227184280Ssam		    IEEE80211_SEQ_ADD(tap->txa_start, tap->txa_wnd-1),
2228184280Ssam		    tap->txa_qframes, tap->txa_seqpending,
2229184280Ssam		    WME_AC_TO_TID(tap->txa_ac));
2230184280Ssam
2231184280Ssam		/* NB: timer already stopped in bar_tx_complete */
2232184280Ssam		tap->txa_start = tap->txa_seqpending;
2233184280Ssam		tap->txa_flags &= ~IEEE80211_AGGR_BARPEND;
2234184280Ssam	}
2235184280Ssam}
2236184280Ssam
2237173273Ssam/*
2238170530Ssam * Transmit a BAR frame to the specified node.  The
2239170530Ssam * BAR contents are drawn from the supplied aggregation
2240170530Ssam * state associated with the node.
2241184280Ssam *
2242184280Ssam * NB: we only handle immediate ACK w/ compressed bitmap.
2243170530Ssam */
2244170530Ssamint
2245170530Ssamieee80211_send_bar(struct ieee80211_node *ni,
2246184280Ssam	struct ieee80211_tx_ampdu *tap, ieee80211_seq seq)
2247170530Ssam{
2248178354Ssam#define	senderr(_x, _v)	do { vap->iv_stats._v++; ret = _x; goto bad; } while (0)
2249178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2250170530Ssam	struct ieee80211com *ic = ni->ni_ic;
2251184280Ssam	struct ieee80211_frame_bar *bar;
2252170530Ssam	struct mbuf *m;
2253184280Ssam	uint16_t barctl, barseqctl;
2254170530Ssam	uint8_t *frm;
2255170530Ssam	int tid, ret;
2256170530Ssam
2257184280Ssam	if ((tap->txa_flags & IEEE80211_AGGR_RUNNING) == 0) {
2258184280Ssam		/* no ADDBA response, should not happen */
2259184280Ssam		/* XXX stat+msg */
2260184280Ssam		return EINVAL;
2261184280Ssam	}
2262184280Ssam	/* XXX locking */
2263184280Ssam	bar_stop_timer(tap);
2264184280Ssam
2265170530Ssam	ieee80211_ref_node(ni);
2266170530Ssam
2267184280Ssam	m = ieee80211_getmgtframe(&frm, ic->ic_headroom, sizeof(*bar));
2268170530Ssam	if (m == NULL)
2269170530Ssam		senderr(ENOMEM, is_tx_nobuf);
2270170530Ssam
2271184280Ssam	if (!ieee80211_add_callback(m, bar_tx_complete, tap)) {
2272184280Ssam		m_freem(m);
2273184280Ssam		senderr(ENOMEM, is_tx_nobuf);	/* XXX */
2274184280Ssam		/* NOTREACHED */
2275184280Ssam	}
2276184280Ssam
2277184280Ssam	bar = mtod(m, struct ieee80211_frame_bar *);
2278184280Ssam	bar->i_fc[0] = IEEE80211_FC0_VERSION_0 |
2279170530Ssam		IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_BAR;
2280184280Ssam	bar->i_fc[1] = 0;
2281184280Ssam	IEEE80211_ADDR_COPY(bar->i_ra, ni->ni_macaddr);
2282184280Ssam	IEEE80211_ADDR_COPY(bar->i_ta, vap->iv_myaddr);
2283170530Ssam
2284170530Ssam	tid = WME_AC_TO_TID(tap->txa_ac);
2285170530Ssam	barctl 	= (tap->txa_flags & IEEE80211_AGGR_IMMEDIATE ?
2286184280Ssam			0 : IEEE80211_BAR_NOACK)
2287184280Ssam		| IEEE80211_BAR_COMP
2288184280Ssam		| SM(tid, IEEE80211_BAR_TID)
2289170530Ssam		;
2290184280Ssam	barseqctl = SM(seq, IEEE80211_BAR_SEQ_START);
2291184280Ssam	/* NB: known to have proper alignment */
2292184280Ssam	bar->i_ctl = htole16(barctl);
2293184280Ssam	bar->i_seq = htole16(barseqctl);
2294184280Ssam	m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_bar);
2295170530Ssam
2296178354Ssam	M_WME_SETAC(m, WME_AC_VO);
2297178354Ssam
2298170530Ssam	IEEE80211_NODE_STAT(ni, tx_mgmt);	/* XXX tx_ctl? */
2299170530Ssam
2300184280Ssam	/* XXX locking */
2301184280Ssam	/* init/bump attempts counter */
2302184280Ssam	if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) == 0)
2303184280Ssam		tap->txa_attempts = 1;
2304184280Ssam	else
2305184280Ssam		tap->txa_attempts++;
2306184280Ssam	tap->txa_seqpending = seq;
2307184280Ssam	tap->txa_flags |= IEEE80211_AGGR_BARPEND;
2308170530Ssam
2309184280Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_11N,
2310184280Ssam	    ni, "send BAR: tid %u ctl 0x%x start %u (attempt %d)",
2311184280Ssam	    tid, barctl, seq, tap->txa_attempts);
2312184280Ssam
2313225013Sadrian	/*
2314225013Sadrian	 * ic_raw_xmit will free the node reference
2315225013Sadrian	 * regardless of queue/TX success or failure.
2316225013Sadrian	 */
2317184280Ssam	ret = ic->ic_raw_xmit(ni, m, NULL);
2318184280Ssam	if (ret != 0) {
2319184280Ssam		/* xmit failed, clear state flag */
2320184280Ssam		tap->txa_flags &= ~IEEE80211_AGGR_BARPEND;
2321225013Sadrian		return ret;
2322184280Ssam	}
2323184280Ssam	/* XXX hack against tx complete happening before timer is started */
2324184280Ssam	if (tap->txa_flags & IEEE80211_AGGR_BARPEND)
2325184280Ssam		bar_start_timer(tap);
2326184280Ssam	return 0;
2327170530Ssambad:
2328170530Ssam	ieee80211_free_node(ni);
2329170530Ssam	return ret;
2330170530Ssam#undef senderr
2331170530Ssam}
2332170530Ssam
2333195377Ssamstatic int
2334195377Ssamht_action_output(struct ieee80211_node *ni, struct mbuf *m)
2335195377Ssam{
2336195377Ssam	struct ieee80211_bpf_params params;
2337195377Ssam
2338195377Ssam	memset(&params, 0, sizeof(params));
2339195377Ssam	params.ibp_pri = WME_AC_VO;
2340195377Ssam	params.ibp_rate0 = ni->ni_txparms->mgmtrate;
2341195377Ssam	/* NB: we know all frames are unicast */
2342195377Ssam	params.ibp_try0 = ni->ni_txparms->maxretry;
2343195377Ssam	params.ibp_power = ni->ni_txpower;
2344195377Ssam	return ieee80211_mgmt_output(ni, m, IEEE80211_FC0_SUBTYPE_ACTION,
2345195377Ssam	     &params);
2346195377Ssam}
2347195377Ssam
2348195377Ssam#define	ADDSHORT(frm, v) do {			\
2349195377Ssam	frm[0] = (v) & 0xff;			\
2350195377Ssam	frm[1] = (v) >> 8;			\
2351195377Ssam	frm += 2;				\
2352195377Ssam} while (0)
2353195377Ssam
2354170530Ssam/*
2355170530Ssam * Send an action management frame.  The arguments are stuff
2356170530Ssam * into a frame without inspection; the caller is assumed to
2357170530Ssam * prepare them carefully (e.g. based on the aggregation state).
2358170530Ssam */
2359195377Ssamstatic int
2360195377Ssamht_send_action_ba_addba(struct ieee80211_node *ni,
2361195377Ssam	int category, int action, void *arg0)
2362170530Ssam{
2363178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2364170530Ssam	struct ieee80211com *ic = ni->ni_ic;
2365195377Ssam	uint16_t *args = arg0;
2366170530Ssam	struct mbuf *m;
2367170530Ssam	uint8_t *frm;
2368195377Ssam
2369195377Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2370205277Srpaulo	    "send ADDBA %s: dialogtoken %d status %d "
2371195377Ssam	    "baparamset 0x%x (tid %d) batimeout 0x%x baseqctl 0x%x",
2372195377Ssam	    (action == IEEE80211_ACTION_BA_ADDBA_REQUEST) ?
2373195377Ssam		"request" : "response",
2374205277Srpaulo	    args[0], args[1], args[2], MS(args[2], IEEE80211_BAPS_TID),
2375205277Srpaulo	    args[3], args[4]);
2376195377Ssam
2377195377Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2378195377Ssam	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2379195377Ssam	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2380195377Ssam	ieee80211_ref_node(ni);
2381195377Ssam
2382195377Ssam	m = ieee80211_getmgtframe(&frm,
2383195377Ssam	    ic->ic_headroom + sizeof(struct ieee80211_frame),
2384195377Ssam	    sizeof(uint16_t)	/* action+category */
2385195377Ssam	    /* XXX may action payload */
2386195377Ssam	    + sizeof(struct ieee80211_action_ba_addbaresponse)
2387195377Ssam	);
2388195377Ssam	if (m != NULL) {
2389195377Ssam		*frm++ = category;
2390195377Ssam		*frm++ = action;
2391195377Ssam		*frm++ = args[0];		/* dialog token */
2392205277Srpaulo		if (action == IEEE80211_ACTION_BA_ADDBA_RESPONSE)
2393205277Srpaulo			ADDSHORT(frm, args[1]);	/* status code */
2394205277Srpaulo		ADDSHORT(frm, args[2]);		/* baparamset */
2395205277Srpaulo		ADDSHORT(frm, args[3]);		/* batimeout */
2396195377Ssam		if (action == IEEE80211_ACTION_BA_ADDBA_REQUEST)
2397205277Srpaulo			ADDSHORT(frm, args[4]);	/* baseqctl */
2398195377Ssam		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2399195377Ssam		return ht_action_output(ni, m);
2400195377Ssam	} else {
2401195377Ssam		vap->iv_stats.is_tx_nobuf++;
2402195377Ssam		ieee80211_free_node(ni);
2403195377Ssam		return ENOMEM;
2404195377Ssam	}
2405195377Ssam}
2406195377Ssam
2407195377Ssamstatic int
2408195377Ssamht_send_action_ba_delba(struct ieee80211_node *ni,
2409195377Ssam	int category, int action, void *arg0)
2410195377Ssam{
2411195377Ssam	struct ieee80211vap *vap = ni->ni_vap;
2412195377Ssam	struct ieee80211com *ic = ni->ni_ic;
2413195377Ssam	uint16_t *args = arg0;
2414195377Ssam	struct mbuf *m;
2415170530Ssam	uint16_t baparamset;
2416195377Ssam	uint8_t *frm;
2417170530Ssam
2418195377Ssam	baparamset = SM(args[0], IEEE80211_DELBAPS_TID)
2419195377Ssam		   | args[1]
2420195377Ssam		   ;
2421195377Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2422195377Ssam	    "send DELBA action: tid %d, initiator %d reason %d",
2423195377Ssam	    args[0], args[1], args[2]);
2424170530Ssam
2425178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2426195377Ssam	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2427195377Ssam	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2428170530Ssam	ieee80211_ref_node(ni);
2429170530Ssam
2430170530Ssam	m = ieee80211_getmgtframe(&frm,
2431195377Ssam	    ic->ic_headroom + sizeof(struct ieee80211_frame),
2432195377Ssam	    sizeof(uint16_t)	/* action+category */
2433195377Ssam	    /* XXX may action payload */
2434195377Ssam	    + sizeof(struct ieee80211_action_ba_addbaresponse)
2435170530Ssam	);
2436195377Ssam	if (m != NULL) {
2437195377Ssam		*frm++ = category;
2438195377Ssam		*frm++ = action;
2439195377Ssam		ADDSHORT(frm, baparamset);
2440195377Ssam		ADDSHORT(frm, args[2]);		/* reason code */
2441195377Ssam		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2442195377Ssam		return ht_action_output(ni, m);
2443195377Ssam	} else {
2444195377Ssam		vap->iv_stats.is_tx_nobuf++;
2445195377Ssam		ieee80211_free_node(ni);
2446195377Ssam		return ENOMEM;
2447195377Ssam	}
2448195377Ssam}
2449170530Ssam
2450195377Ssamstatic int
2451195377Ssamht_send_action_ht_txchwidth(struct ieee80211_node *ni,
2452195377Ssam	int category, int action, void *arg0)
2453195377Ssam{
2454195377Ssam	struct ieee80211vap *vap = ni->ni_vap;
2455195377Ssam	struct ieee80211com *ic = ni->ni_ic;
2456195377Ssam	struct mbuf *m;
2457195377Ssam	uint8_t *frm;
2458170530Ssam
2459195377Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2460195377Ssam	    "send HT txchwidth: width %d",
2461195377Ssam	    IEEE80211_IS_CHAN_HT40(ni->ni_chan) ? 40 : 20);
2462170530Ssam
2463195377Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2464195377Ssam	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2465195377Ssam	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2466195377Ssam	ieee80211_ref_node(ni);
2467170530Ssam
2468195377Ssam	m = ieee80211_getmgtframe(&frm,
2469195377Ssam	    ic->ic_headroom + sizeof(struct ieee80211_frame),
2470195377Ssam	    sizeof(uint16_t)	/* action+category */
2471195377Ssam	    /* XXX may action payload */
2472195377Ssam	    + sizeof(struct ieee80211_action_ba_addbaresponse)
2473195377Ssam	);
2474195377Ssam	if (m != NULL) {
2475195377Ssam		*frm++ = category;
2476195377Ssam		*frm++ = action;
2477195377Ssam		*frm++ = IEEE80211_IS_CHAN_HT40(ni->ni_chan) ?
2478195377Ssam			IEEE80211_A_HT_TXCHWIDTH_2040 :
2479195377Ssam			IEEE80211_A_HT_TXCHWIDTH_20;
2480195377Ssam		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2481195377Ssam		return ht_action_output(ni, m);
2482195377Ssam	} else {
2483195377Ssam		vap->iv_stats.is_tx_nobuf++;
2484195377Ssam		ieee80211_free_node(ni);
2485195377Ssam		return ENOMEM;
2486170530Ssam	}
2487195377Ssam}
2488170530Ssam#undef ADDSHORT
2489170530Ssam
2490170530Ssam/*
2491219600Sbschmidt * Construct the MCS bit mask for inclusion in an HT capabilities
2492219600Sbschmidt * information element.
2493170530Ssam */
2494219600Sbschmidtstatic void
2495219600Sbschmidtieee80211_set_mcsset(struct ieee80211com *ic, uint8_t *frm)
2496170530Ssam{
2497170530Ssam	int i;
2498219600Sbschmidt	uint8_t txparams;
2499170530Ssam
2500219600Sbschmidt	KASSERT((ic->ic_rxstream > 0 && ic->ic_rxstream <= 4),
2501219600Sbschmidt	    ("ic_rxstream %d out of range", ic->ic_rxstream));
2502219600Sbschmidt	KASSERT((ic->ic_txstream > 0 && ic->ic_txstream <= 4),
2503219600Sbschmidt	    ("ic_txstream %d out of range", ic->ic_txstream));
2504219600Sbschmidt
2505219600Sbschmidt	for (i = 0; i < ic->ic_rxstream * 8; i++)
2506219600Sbschmidt		setbit(frm, i);
2507219600Sbschmidt	if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
2508219600Sbschmidt	    (ic->ic_htcaps & IEEE80211_HTC_RXMCS32))
2509219600Sbschmidt		setbit(frm, 32);
2510219600Sbschmidt	if (ic->ic_htcaps & IEEE80211_HTC_RXUNEQUAL) {
2511219600Sbschmidt		if (ic->ic_rxstream >= 2) {
2512219600Sbschmidt			for (i = 33; i <= 38; i++)
2513219600Sbschmidt				setbit(frm, i);
2514170530Ssam		}
2515219600Sbschmidt		if (ic->ic_rxstream >= 3) {
2516219600Sbschmidt			for (i = 39; i <= 52; i++)
2517219600Sbschmidt				setbit(frm, i);
2518219600Sbschmidt		}
2519219600Sbschmidt		if (ic->ic_txstream >= 4) {
2520219600Sbschmidt			for (i = 53; i <= 76; i++)
2521219600Sbschmidt				setbit(frm, i);
2522219600Sbschmidt		}
2523170530Ssam	}
2524219600Sbschmidt
2525219600Sbschmidt	if (ic->ic_rxstream != ic->ic_txstream) {
2526219600Sbschmidt		txparams = 0x1;			/* TX MCS set defined */
2527219600Sbschmidt		txparams |= 0x2;		/* TX RX MCS not equal */
2528219600Sbschmidt		txparams |= (ic->ic_txstream - 1) << 2;	/* num TX streams */
2529219600Sbschmidt		if (ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL)
2530219600Sbschmidt			txparams |= 0x16;	/* TX unequal modulation sup */
2531219600Sbschmidt	} else
2532219600Sbschmidt		txparams = 0;
2533219600Sbschmidt	frm[12] = txparams;
2534170530Ssam}
2535170530Ssam
2536170530Ssam/*
2537170530Ssam * Add body of an HTCAP information element.
2538170530Ssam */
2539170530Ssamstatic uint8_t *
2540170530Ssamieee80211_add_htcap_body(uint8_t *frm, struct ieee80211_node *ni)
2541170530Ssam{
2542170530Ssam#define	ADDSHORT(frm, v) do {			\
2543170530Ssam	frm[0] = (v) & 0xff;			\
2544170530Ssam	frm[1] = (v) >> 8;			\
2545170530Ssam	frm += 2;				\
2546170530Ssam} while (0)
2547222683Sbschmidt	struct ieee80211com *ic = ni->ni_ic;
2548178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2549205513Srpaulo	uint16_t caps, extcaps;
2550173865Ssam	int rxmax, density;
2551170530Ssam
2552170530Ssam	/* HT capabilities */
2553178354Ssam	caps = vap->iv_htcaps & 0xffff;
2554173273Ssam	/*
2555173273Ssam	 * Note channel width depends on whether we are operating as
2556173273Ssam	 * a sta or not.  When operating as a sta we are generating
2557173273Ssam	 * a request based on our desired configuration.  Otherwise
2558173273Ssam	 * we are operational and the channel attributes identify
2559173273Ssam	 * how we've been setup (which might be different if a fixed
2560173273Ssam	 * channel is specified).
2561173273Ssam	 */
2562178354Ssam	if (vap->iv_opmode == IEEE80211_M_STA) {
2563173273Ssam		/* override 20/40 use based on config */
2564193655Ssam		if (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)
2565173273Ssam			caps |= IEEE80211_HTCAP_CHWIDTH40;
2566173273Ssam		else
2567173273Ssam			caps &= ~IEEE80211_HTCAP_CHWIDTH40;
2568173865Ssam		/* use advertised setting (XXX locally constraint) */
2569173865Ssam		rxmax = MS(ni->ni_htparam, IEEE80211_HTCAP_MAXRXAMPDU);
2570173865Ssam		density = MS(ni->ni_htparam, IEEE80211_HTCAP_MPDUDENSITY);
2571222683Sbschmidt
2572222683Sbschmidt		/*
2573222683Sbschmidt		 * NB: Hardware might support HT40 on some but not all
2574222683Sbschmidt		 * channels. We can't determine this earlier because only
2575222683Sbschmidt		 * after association the channel is upgraded to HT based
2576222683Sbschmidt		 * on the negotiated capabilities.
2577222683Sbschmidt		 */
2578222683Sbschmidt		if (ni->ni_chan != IEEE80211_CHAN_ANYC &&
2579222683Sbschmidt		    findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT40U) == NULL &&
2580222683Sbschmidt		    findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT40D) == NULL)
2581222683Sbschmidt			caps &= ~IEEE80211_HTCAP_CHWIDTH40;
2582173273Ssam	} else {
2583173273Ssam		/* override 20/40 use based on current channel */
2584178354Ssam		if (IEEE80211_IS_CHAN_HT40(ni->ni_chan))
2585173273Ssam			caps |= IEEE80211_HTCAP_CHWIDTH40;
2586173273Ssam		else
2587173273Ssam			caps &= ~IEEE80211_HTCAP_CHWIDTH40;
2588178354Ssam		rxmax = vap->iv_ampdu_rxmax;
2589178354Ssam		density = vap->iv_ampdu_density;
2590173273Ssam	}
2591170530Ssam	/* adjust short GI based on channel and config */
2592193655Ssam	if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20) == 0)
2593170530Ssam		caps &= ~IEEE80211_HTCAP_SHORTGI20;
2594193655Ssam	if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40) == 0 ||
2595170530Ssam	    (caps & IEEE80211_HTCAP_CHWIDTH40) == 0)
2596170530Ssam		caps &= ~IEEE80211_HTCAP_SHORTGI40;
2597170530Ssam	ADDSHORT(frm, caps);
2598170530Ssam
2599170530Ssam	/* HT parameters */
2600173865Ssam	*frm = SM(rxmax, IEEE80211_HTCAP_MAXRXAMPDU)
2601173865Ssam	     | SM(density, IEEE80211_HTCAP_MPDUDENSITY)
2602173273Ssam	     ;
2603170530Ssam	frm++;
2604170530Ssam
2605170530Ssam	/* pre-zero remainder of ie */
2606170530Ssam	memset(frm, 0, sizeof(struct ieee80211_ie_htcap) -
2607170530Ssam		__offsetof(struct ieee80211_ie_htcap, hc_mcsset));
2608170530Ssam
2609170530Ssam	/* supported MCS set */
2610173273Ssam	/*
2611219600Sbschmidt	 * XXX: For sta mode the rate set should be restricted based
2612219600Sbschmidt	 * on the AP's capabilities, but ni_htrates isn't setup when
2613219600Sbschmidt	 * we're called to form an AssocReq frame so for now we're
2614219600Sbschmidt	 * restricted to the device capabilities.
2615173273Ssam	 */
2616219600Sbschmidt	ieee80211_set_mcsset(ni->ni_ic, frm);
2617170530Ssam
2618205513Srpaulo	frm += __offsetof(struct ieee80211_ie_htcap, hc_extcap) -
2619205513Srpaulo		__offsetof(struct ieee80211_ie_htcap, hc_mcsset);
2620205513Srpaulo
2621205513Srpaulo	/* HT extended capabilities */
2622205513Srpaulo	extcaps = vap->iv_htextcaps & 0xffff;
2623205513Srpaulo
2624205513Srpaulo	ADDSHORT(frm, extcaps);
2625205513Srpaulo
2626170530Ssam	frm += sizeof(struct ieee80211_ie_htcap) -
2627205513Srpaulo		__offsetof(struct ieee80211_ie_htcap, hc_txbf);
2628205513Srpaulo
2629170530Ssam	return frm;
2630170530Ssam#undef ADDSHORT
2631170530Ssam}
2632170530Ssam
2633170530Ssam/*
2634170530Ssam * Add 802.11n HT capabilities information element
2635170530Ssam */
2636170530Ssamuint8_t *
2637170530Ssamieee80211_add_htcap(uint8_t *frm, struct ieee80211_node *ni)
2638170530Ssam{
2639170530Ssam	frm[0] = IEEE80211_ELEMID_HTCAP;
2640170530Ssam	frm[1] = sizeof(struct ieee80211_ie_htcap) - 2;
2641170530Ssam	return ieee80211_add_htcap_body(frm + 2, ni);
2642170530Ssam}
2643170530Ssam
2644170530Ssam/*
2645170530Ssam * Add Broadcom OUI wrapped standard HTCAP ie; this is
2646170530Ssam * used for compatibility w/ pre-draft implementations.
2647170530Ssam */
2648170530Ssamuint8_t *
2649170530Ssamieee80211_add_htcap_vendor(uint8_t *frm, struct ieee80211_node *ni)
2650170530Ssam{
2651170530Ssam	frm[0] = IEEE80211_ELEMID_VENDOR;
2652170530Ssam	frm[1] = 4 + sizeof(struct ieee80211_ie_htcap) - 2;
2653170530Ssam	frm[2] = (BCM_OUI >> 0) & 0xff;
2654170530Ssam	frm[3] = (BCM_OUI >> 8) & 0xff;
2655170530Ssam	frm[4] = (BCM_OUI >> 16) & 0xff;
2656170530Ssam	frm[5] = BCM_OUI_HTCAP;
2657170530Ssam	return ieee80211_add_htcap_body(frm + 6, ni);
2658170530Ssam}
2659170530Ssam
2660170530Ssam/*
2661170530Ssam * Construct the MCS bit mask of basic rates
2662170530Ssam * for inclusion in an HT information element.
2663170530Ssam */
2664170530Ssamstatic void
2665170530Ssamieee80211_set_basic_htrates(uint8_t *frm, const struct ieee80211_htrateset *rs)
2666170530Ssam{
2667170530Ssam	int i;
2668170530Ssam
2669170530Ssam	for (i = 0; i < rs->rs_nrates; i++) {
2670170530Ssam		int r = rs->rs_rates[i] & IEEE80211_RATE_VAL;
2671170530Ssam		if ((rs->rs_rates[i] & IEEE80211_RATE_BASIC) &&
2672170530Ssam		    r < IEEE80211_HTRATE_MAXSIZE) {
2673170530Ssam			/* NB: this assumes a particular implementation */
2674170530Ssam			setbit(frm, r);
2675170530Ssam		}
2676170530Ssam	}
2677170530Ssam}
2678170530Ssam
2679170530Ssam/*
2680172211Ssam * Update the HTINFO ie for a beacon frame.
2681172211Ssam */
2682172211Ssamvoid
2683178354Ssamieee80211_ht_update_beacon(struct ieee80211vap *vap,
2684172211Ssam	struct ieee80211_beacon_offsets *bo)
2685172211Ssam{
2686172211Ssam#define	PROTMODE	(IEEE80211_HTINFO_OPMODE|IEEE80211_HTINFO_NONHT_PRESENT)
2687178354Ssam	const struct ieee80211_channel *bsschan = vap->iv_bss->ni_chan;
2688178354Ssam	struct ieee80211com *ic = vap->iv_ic;
2689172211Ssam	struct ieee80211_ie_htinfo *ht =
2690172211Ssam	   (struct ieee80211_ie_htinfo *) bo->bo_htinfo;
2691172211Ssam
2692172211Ssam	/* XXX only update on channel change */
2693178354Ssam	ht->hi_ctrlchannel = ieee80211_chan2ieee(ic, bsschan);
2694193655Ssam	if (vap->iv_flags_ht & IEEE80211_FHT_RIFS)
2695183256Ssam		ht->hi_byte1 = IEEE80211_HTINFO_RIFSMODE_PERM;
2696183256Ssam	else
2697183256Ssam		ht->hi_byte1 = IEEE80211_HTINFO_RIFSMODE_PROH;
2698178354Ssam	if (IEEE80211_IS_CHAN_HT40U(bsschan))
2699172211Ssam		ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_ABOVE;
2700178354Ssam	else if (IEEE80211_IS_CHAN_HT40D(bsschan))
2701172211Ssam		ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_BELOW;
2702172211Ssam	else
2703172211Ssam		ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_NONE;
2704178354Ssam	if (IEEE80211_IS_CHAN_HT40(bsschan))
2705172211Ssam		ht->hi_byte1 |= IEEE80211_HTINFO_TXWIDTH_2040;
2706172211Ssam
2707172211Ssam	/* protection mode */
2708172211Ssam	ht->hi_byte2 = (ht->hi_byte2 &~ PROTMODE) | ic->ic_curhtprotmode;
2709172211Ssam
2710172211Ssam	/* XXX propagate to vendor ie's */
2711172211Ssam#undef PROTMODE
2712172211Ssam}
2713172211Ssam
2714172211Ssam/*
2715170530Ssam * Add body of an HTINFO information element.
2716173273Ssam *
2717173273Ssam * NB: We don't use struct ieee80211_ie_htinfo because we can
2718173273Ssam * be called to fillin both a standard ie and a compat ie that
2719173273Ssam * has a vendor OUI at the front.
2720170530Ssam */
2721170530Ssamstatic uint8_t *
2722170530Ssamieee80211_add_htinfo_body(uint8_t *frm, struct ieee80211_node *ni)
2723170530Ssam{
2724183256Ssam	struct ieee80211vap *vap = ni->ni_vap;
2725170530Ssam	struct ieee80211com *ic = ni->ni_ic;
2726170530Ssam
2727170530Ssam	/* pre-zero remainder of ie */
2728170530Ssam	memset(frm, 0, sizeof(struct ieee80211_ie_htinfo) - 2);
2729170530Ssam
2730170530Ssam	/* primary/control channel center */
2731178354Ssam	*frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
2732170530Ssam
2733193655Ssam	if (vap->iv_flags_ht & IEEE80211_FHT_RIFS)
2734183256Ssam		frm[0] = IEEE80211_HTINFO_RIFSMODE_PERM;
2735183256Ssam	else
2736183256Ssam		frm[0] = IEEE80211_HTINFO_RIFSMODE_PROH;
2737178354Ssam	if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan))
2738170530Ssam		frm[0] |= IEEE80211_HTINFO_2NDCHAN_ABOVE;
2739178354Ssam	else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan))
2740170530Ssam		frm[0] |= IEEE80211_HTINFO_2NDCHAN_BELOW;
2741170530Ssam	else
2742170530Ssam		frm[0] |= IEEE80211_HTINFO_2NDCHAN_NONE;
2743178354Ssam	if (IEEE80211_IS_CHAN_HT40(ni->ni_chan))
2744170530Ssam		frm[0] |= IEEE80211_HTINFO_TXWIDTH_2040;
2745170530Ssam
2746172211Ssam	frm[1] = ic->ic_curhtprotmode;
2747170530Ssam
2748170530Ssam	frm += 5;
2749170530Ssam
2750170530Ssam	/* basic MCS set */
2751170530Ssam	ieee80211_set_basic_htrates(frm, &ni->ni_htrates);
2752170530Ssam	frm += sizeof(struct ieee80211_ie_htinfo) -
2753170530Ssam		__offsetof(struct ieee80211_ie_htinfo, hi_basicmcsset);
2754170530Ssam	return frm;
2755170530Ssam}
2756170530Ssam
2757170530Ssam/*
2758170530Ssam * Add 802.11n HT information information element.
2759170530Ssam */
2760170530Ssamuint8_t *
2761170530Ssamieee80211_add_htinfo(uint8_t *frm, struct ieee80211_node *ni)
2762170530Ssam{
2763170530Ssam	frm[0] = IEEE80211_ELEMID_HTINFO;
2764170530Ssam	frm[1] = sizeof(struct ieee80211_ie_htinfo) - 2;
2765170530Ssam	return ieee80211_add_htinfo_body(frm + 2, ni);
2766170530Ssam}
2767170530Ssam
2768170530Ssam/*
2769170530Ssam * Add Broadcom OUI wrapped standard HTINFO ie; this is
2770170530Ssam * used for compatibility w/ pre-draft implementations.
2771170530Ssam */
2772170530Ssamuint8_t *
2773170530Ssamieee80211_add_htinfo_vendor(uint8_t *frm, struct ieee80211_node *ni)
2774170530Ssam{
2775170530Ssam	frm[0] = IEEE80211_ELEMID_VENDOR;
2776170530Ssam	frm[1] = 4 + sizeof(struct ieee80211_ie_htinfo) - 2;
2777170530Ssam	frm[2] = (BCM_OUI >> 0) & 0xff;
2778170530Ssam	frm[3] = (BCM_OUI >> 8) & 0xff;
2779170530Ssam	frm[4] = (BCM_OUI >> 16) & 0xff;
2780170530Ssam	frm[5] = BCM_OUI_HTINFO;
2781170530Ssam	return ieee80211_add_htinfo_body(frm + 6, ni);
2782170530Ssam}
2783