ieee80211_ht.c revision 219602
1170530Ssam/*-
2178354Ssam * Copyright (c) 2007-2008 Sam Leffler, Errno Consulting
3170530Ssam * All rights reserved.
4170530Ssam *
5170530Ssam * Redistribution and use in source and binary forms, with or without
6170530Ssam * modification, are permitted provided that the following conditions
7170530Ssam * are met:
8170530Ssam * 1. Redistributions of source code must retain the above copyright
9170530Ssam *    notice, this list of conditions and the following disclaimer.
10170530Ssam * 2. Redistributions in binary form must reproduce the above copyright
11170530Ssam *    notice, this list of conditions and the following disclaimer in the
12170530Ssam *    documentation and/or other materials provided with the distribution.
13170530Ssam *
14170530Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15170530Ssam * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16170530Ssam * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17170530Ssam * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18170530Ssam * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19170530Ssam * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20170530Ssam * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21170530Ssam * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22170530Ssam * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23170530Ssam * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24170530Ssam */
25170530Ssam
26170530Ssam#include <sys/cdefs.h>
27170530Ssam#ifdef __FreeBSD__
28170530Ssam__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_ht.c 219602 2011-03-13 12:16:23Z bschmidt $");
29170530Ssam#endif
30170530Ssam
31170530Ssam/*
32170530Ssam * IEEE 802.11n protocol support.
33170530Ssam */
34170530Ssam
35170530Ssam#include "opt_inet.h"
36178354Ssam#include "opt_wlan.h"
37170530Ssam
38170530Ssam#include <sys/param.h>
39170530Ssam#include <sys/kernel.h>
40170530Ssam#include <sys/systm.h>
41170530Ssam#include <sys/endian.h>
42170530Ssam
43170530Ssam#include <sys/socket.h>
44170530Ssam
45170530Ssam#include <net/if.h>
46170530Ssam#include <net/if_media.h>
47170530Ssam#include <net/ethernet.h>
48170530Ssam
49170530Ssam#include <net80211/ieee80211_var.h>
50195377Ssam#include <net80211/ieee80211_action.h>
51178354Ssam#include <net80211/ieee80211_input.h>
52170530Ssam
53170530Ssam/* define here, used throughout file */
54170530Ssam#define	MS(_v, _f)	(((_v) & _f) >> _f##_S)
55170530Ssam#define	SM(_v, _f)	(((_v) << _f##_S) & _f)
56170530Ssam
57219456Sbschmidtconst struct ieee80211_mcs_rates ieee80211_htrates[IEEE80211_HTRATE_MAXSIZE] = {
58219456Sbschmidt	{  13,  14,   27,   30 },	/* MCS 0 */
59219456Sbschmidt	{  26,  29,   54,   60 },	/* MCS 1 */
60219456Sbschmidt	{  39,  43,   81,   90 },	/* MCS 2 */
61219456Sbschmidt	{  52,  58,  108,  120 },	/* MCS 3 */
62219456Sbschmidt	{  78,  87,  162,  180 },	/* MCS 4 */
63219456Sbschmidt	{ 104, 116,  216,  240 },	/* MCS 5 */
64219456Sbschmidt	{ 117, 130,  243,  270 },	/* MCS 6 */
65219456Sbschmidt	{ 130, 144,  270,  300 },	/* MCS 7 */
66219456Sbschmidt	{  26,  29,   54,   60 },	/* MCS 8 */
67219456Sbschmidt	{  52,  58,  108,  120 },	/* MCS 9 */
68219456Sbschmidt	{  78,  87,  162,  180 },	/* MCS 10 */
69219456Sbschmidt	{ 104, 116,  216,  240 },	/* MCS 11 */
70219456Sbschmidt	{ 156, 173,  324,  360 },	/* MCS 12 */
71219456Sbschmidt	{ 208, 231,  432,  480 },	/* MCS 13 */
72219456Sbschmidt	{ 234, 260,  486,  540 },	/* MCS 14 */
73219456Sbschmidt	{ 260, 289,  540,  600 },	/* MCS 15 */
74219456Sbschmidt	{  39,  43,   81,   90 },	/* MCS 16 */
75219456Sbschmidt	{  78,  87,  162,  180 },	/* MCS 17 */
76219456Sbschmidt	{ 117, 130,  243,  270 },	/* MCS 18 */
77219456Sbschmidt	{ 156, 173,  324,  360 },	/* MCS 19 */
78219456Sbschmidt	{ 234, 260,  486,  540 },	/* MCS 20 */
79219456Sbschmidt	{ 312, 347,  648,  720 },	/* MCS 21 */
80219456Sbschmidt	{ 351, 390,  729,  810 },	/* MCS 22 */
81219456Sbschmidt	{ 390, 433,  810,  900 },	/* MCS 23 */
82219456Sbschmidt	{  52,  58,  108,  120 },	/* MCS 24 */
83219456Sbschmidt	{ 104, 116,  216,  240 },	/* MCS 25 */
84219456Sbschmidt	{ 156, 173,  324,  360 },	/* MCS 26 */
85219456Sbschmidt	{ 208, 231,  432,  480 },	/* MCS 27 */
86219456Sbschmidt	{ 312, 347,  648,  720 },	/* MCS 28 */
87219456Sbschmidt	{ 416, 462,  864,  960 },	/* MCS 29 */
88219456Sbschmidt	{ 468, 520,  972, 1080 },	/* MCS 30 */
89219456Sbschmidt	{ 520, 578, 1080, 1200 },	/* MCS 31 */
90219456Sbschmidt	{   0,   0,   12,   13 },	/* MCS 32 */
91219456Sbschmidt	{  78,  87,  162,  180 },	/* MCS 33 */
92219456Sbschmidt	{ 104, 116,  216,  240 },	/* MCS 34 */
93219456Sbschmidt	{ 130, 144,  270,  300 },	/* MCS 35 */
94219456Sbschmidt	{ 117, 130,  243,  270 },	/* MCS 36 */
95219456Sbschmidt	{ 156, 173,  324,  360 },	/* MCS 37 */
96219456Sbschmidt	{ 195, 217,  405,  450 },	/* MCS 38 */
97219456Sbschmidt	{ 104, 116,  216,  240 },	/* MCS 39 */
98219456Sbschmidt	{ 130, 144,  270,  300 },	/* MCS 40 */
99219456Sbschmidt	{ 130, 144,  270,  300 },	/* MCS 41 */
100219456Sbschmidt	{ 156, 173,  324,  360 },	/* MCS 42 */
101219456Sbschmidt	{ 182, 202,  378,  420 },	/* MCS 43 */
102219456Sbschmidt	{ 182, 202,  378,  420 },	/* MCS 44 */
103219456Sbschmidt	{ 208, 231,  432,  480 },	/* MCS 45 */
104219456Sbschmidt	{ 156, 173,  324,  360 },	/* MCS 46 */
105219456Sbschmidt	{ 195, 217,  405,  450 },	/* MCS 47 */
106219456Sbschmidt	{ 195, 217,  405,  450 },	/* MCS 48 */
107219456Sbschmidt	{ 234, 260,  486,  540 },	/* MCS 49 */
108219456Sbschmidt	{ 273, 303,  567,  630 },	/* MCS 50 */
109219456Sbschmidt	{ 273, 303,  567,  630 },	/* MCS 51 */
110219456Sbschmidt	{ 312, 347,  648,  720 },	/* MCS 52 */
111219456Sbschmidt	{ 130, 144,  270,  300 },	/* MCS 53 */
112219456Sbschmidt	{ 156, 173,  324,  360 },	/* MCS 54 */
113219456Sbschmidt	{ 182, 202,  378,  420 },	/* MCS 55 */
114219456Sbschmidt	{ 156, 173,  324,  360 },	/* MCS 56 */
115219456Sbschmidt	{ 182, 202,  378,  420 },	/* MCS 57 */
116219456Sbschmidt	{ 208, 231,  432,  480 },	/* MCS 58 */
117219456Sbschmidt	{ 234, 260,  486,  540 },	/* MCS 59 */
118219456Sbschmidt	{ 208, 231,  432,  480 },	/* MCS 60 */
119219456Sbschmidt	{ 234, 260,  486,  540 },	/* MCS 61 */
120219456Sbschmidt	{ 260, 289,  540,  600 },	/* MCS 62 */
121219456Sbschmidt	{ 260, 289,  540,  600 },	/* MCS 63 */
122219456Sbschmidt	{ 286, 318,  594,  660 },	/* MCS 64 */
123219456Sbschmidt	{ 195, 217,  405,  450 },	/* MCS 65 */
124219456Sbschmidt	{ 234, 260,  486,  540 },	/* MCS 66 */
125219456Sbschmidt	{ 273, 303,  567,  630 },	/* MCS 67 */
126219456Sbschmidt	{ 234, 260,  486,  540 },	/* MCS 68 */
127219456Sbschmidt	{ 273, 303,  567,  630 },	/* MCS 69 */
128219456Sbschmidt	{ 312, 347,  648,  720 },	/* MCS 70 */
129219456Sbschmidt	{ 351, 390,  729,  810 },	/* MCS 71 */
130219456Sbschmidt	{ 312, 347,  648,  720 },	/* MCS 72 */
131219456Sbschmidt	{ 351, 390,  729,  810 },	/* MCS 73 */
132219456Sbschmidt	{ 390, 433,  810,  900 },	/* MCS 74 */
133219456Sbschmidt	{ 390, 433,  810,  900 },	/* MCS 75 */
134219456Sbschmidt	{ 429, 477,  891,  990 },	/* MCS 76 */
135170530Ssam};
136170530Ssam
137173273Ssam#ifdef IEEE80211_AMPDU_AGE
138193115Ssamstatic	int ieee80211_ampdu_age = -1;	/* threshold for ampdu reorder q (ms) */
139193115SsamSYSCTL_PROC(_net_wlan, OID_AUTO, ampdu_age, CTLTYPE_INT | CTLFLAG_RW,
140193115Ssam	&ieee80211_ampdu_age, 0, ieee80211_sysctl_msecs_ticks, "I",
141193115Ssam	"AMPDU max reorder age (ms)");
142173273Ssam#endif
143173273Ssam
144193115Ssamstatic	int ieee80211_recv_bar_ena = 1;
145193115SsamSYSCTL_INT(_net_wlan, OID_AUTO, recv_bar, CTLFLAG_RW, &ieee80211_recv_bar_ena,
146193115Ssam	    0, "BAR frame processing (ena/dis)");
147193115Ssam
148193115Ssamstatic	int ieee80211_addba_timeout = -1;/* timeout for ADDBA response */
149193115SsamSYSCTL_PROC(_net_wlan, OID_AUTO, addba_timeout, CTLTYPE_INT | CTLFLAG_RW,
150193115Ssam	&ieee80211_addba_timeout, 0, ieee80211_sysctl_msecs_ticks, "I",
151193115Ssam	"ADDBA request timeout (ms)");
152193115Ssamstatic	int ieee80211_addba_backoff = -1;/* backoff after max ADDBA requests */
153193115SsamSYSCTL_PROC(_net_wlan, OID_AUTO, addba_backoff, CTLTYPE_INT | CTLFLAG_RW,
154193115Ssam	&ieee80211_addba_backoff, 0, ieee80211_sysctl_msecs_ticks, "I",
155193115Ssam	"ADDBA request backoff (ms)");
156193115Ssamstatic	int ieee80211_addba_maxtries = 3;/* max ADDBA requests before backoff */
157193115SsamSYSCTL_INT(_net_wlan, OID_AUTO, addba_maxtries, CTLTYPE_INT | CTLFLAG_RW,
158193115Ssam	&ieee80211_addba_maxtries, 0, "max ADDBA requests sent before backoff");
159193115Ssam
160193115Ssamstatic	int ieee80211_bar_timeout = -1;	/* timeout waiting for BAR response */
161193115Ssamstatic	int ieee80211_bar_maxtries = 50;/* max BAR requests before DELBA */
162193115Ssam
163195377Ssamstatic	ieee80211_recv_action_func ht_recv_action_ba_addba_request;
164195377Ssamstatic	ieee80211_recv_action_func ht_recv_action_ba_addba_response;
165195377Ssamstatic	ieee80211_recv_action_func ht_recv_action_ba_delba;
166195377Ssamstatic	ieee80211_recv_action_func ht_recv_action_ht_mimopwrsave;
167195377Ssamstatic	ieee80211_recv_action_func ht_recv_action_ht_txchwidth;
168195377Ssam
169195377Ssamstatic	ieee80211_send_action_func ht_send_action_ba_addba;
170195377Ssamstatic	ieee80211_send_action_func ht_send_action_ba_delba;
171195377Ssamstatic	ieee80211_send_action_func ht_send_action_ht_txchwidth;
172195377Ssam
173178354Ssamstatic void
174195377Ssamieee80211_ht_init(void)
175178354Ssam{
176195377Ssam	/*
177195377Ssam	 * Setup HT parameters that depends on the clock frequency.
178195377Ssam	 */
179178354Ssam#ifdef IEEE80211_AMPDU_AGE
180178354Ssam	ieee80211_ampdu_age = msecs_to_ticks(500);
181178354Ssam#endif
182178354Ssam	ieee80211_addba_timeout = msecs_to_ticks(250);
183178354Ssam	ieee80211_addba_backoff = msecs_to_ticks(10*1000);
184184280Ssam	ieee80211_bar_timeout = msecs_to_ticks(250);
185195377Ssam	/*
186195377Ssam	 * Register action frame handlers.
187195377Ssam	 */
188195377Ssam	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_BA,
189195377Ssam	    IEEE80211_ACTION_BA_ADDBA_REQUEST, ht_recv_action_ba_addba_request);
190195377Ssam	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_BA,
191195377Ssam	    IEEE80211_ACTION_BA_ADDBA_RESPONSE, ht_recv_action_ba_addba_response);
192195377Ssam	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_BA,
193195377Ssam	    IEEE80211_ACTION_BA_DELBA, ht_recv_action_ba_delba);
194195377Ssam	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_HT,
195195377Ssam	    IEEE80211_ACTION_HT_MIMOPWRSAVE, ht_recv_action_ht_mimopwrsave);
196195377Ssam	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_HT,
197195377Ssam	    IEEE80211_ACTION_HT_TXCHWIDTH, ht_recv_action_ht_txchwidth);
198195377Ssam
199195377Ssam	ieee80211_send_action_register(IEEE80211_ACTION_CAT_BA,
200195377Ssam	    IEEE80211_ACTION_BA_ADDBA_REQUEST, ht_send_action_ba_addba);
201195377Ssam	ieee80211_send_action_register(IEEE80211_ACTION_CAT_BA,
202195377Ssam	    IEEE80211_ACTION_BA_ADDBA_RESPONSE, ht_send_action_ba_addba);
203195377Ssam	ieee80211_send_action_register(IEEE80211_ACTION_CAT_BA,
204195377Ssam	    IEEE80211_ACTION_BA_DELBA, ht_send_action_ba_delba);
205195377Ssam	ieee80211_send_action_register(IEEE80211_ACTION_CAT_HT,
206195377Ssam	    IEEE80211_ACTION_HT_TXCHWIDTH, ht_send_action_ht_txchwidth);
207178354Ssam}
208195377SsamSYSINIT(wlan_ht, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_ht_init, NULL);
209170530Ssam
210178354Ssamstatic int ieee80211_ampdu_enable(struct ieee80211_node *ni,
211178354Ssam	struct ieee80211_tx_ampdu *tap);
212170530Ssamstatic int ieee80211_addba_request(struct ieee80211_node *ni,
213170530Ssam	struct ieee80211_tx_ampdu *tap,
214170530Ssam	int dialogtoken, int baparamset, int batimeout);
215170530Ssamstatic int ieee80211_addba_response(struct ieee80211_node *ni,
216170530Ssam	struct ieee80211_tx_ampdu *tap,
217170530Ssam	int code, int baparamset, int batimeout);
218170530Ssamstatic void ieee80211_addba_stop(struct ieee80211_node *ni,
219170530Ssam	struct ieee80211_tx_ampdu *tap);
220184280Ssamstatic void ieee80211_bar_response(struct ieee80211_node *ni,
221184280Ssam	struct ieee80211_tx_ampdu *tap, int status);
222184280Ssamstatic void ampdu_tx_stop(struct ieee80211_tx_ampdu *tap);
223184280Ssamstatic void bar_stop_timer(struct ieee80211_tx_ampdu *tap);
224191552Ssamstatic int ampdu_rx_start(struct ieee80211_node *, struct ieee80211_rx_ampdu *,
225191552Ssam	int baparamset, int batimeout, int baseqctl);
226191552Ssamstatic void ampdu_rx_stop(struct ieee80211_node *, struct ieee80211_rx_ampdu *);
227170530Ssam
228170530Ssamvoid
229170530Ssamieee80211_ht_attach(struct ieee80211com *ic)
230170530Ssam{
231170530Ssam	/* setup default aggregation policy */
232195377Ssam	ic->ic_recv_action = ieee80211_recv_action;
233170530Ssam	ic->ic_send_action = ieee80211_send_action;
234178354Ssam	ic->ic_ampdu_enable = ieee80211_ampdu_enable;
235170530Ssam	ic->ic_addba_request = ieee80211_addba_request;
236170530Ssam	ic->ic_addba_response = ieee80211_addba_response;
237170530Ssam	ic->ic_addba_stop = ieee80211_addba_stop;
238184280Ssam	ic->ic_bar_response = ieee80211_bar_response;
239191552Ssam	ic->ic_ampdu_rx_start = ampdu_rx_start;
240191552Ssam	ic->ic_ampdu_rx_stop = ampdu_rx_stop;
241170530Ssam
242173273Ssam	ic->ic_htprotmode = IEEE80211_PROT_RTSCTS;
243173273Ssam	ic->ic_curhtprotmode = IEEE80211_HTINFO_OPMODE_PURE;
244178354Ssam}
245173273Ssam
246178354Ssamvoid
247178354Ssamieee80211_ht_detach(struct ieee80211com *ic)
248178354Ssam{
249178354Ssam}
250173273Ssam
251178354Ssamvoid
252178354Ssamieee80211_ht_vattach(struct ieee80211vap *vap)
253178354Ssam{
254178354Ssam
255178354Ssam	/* driver can override defaults */
256178354Ssam	vap->iv_ampdu_rxmax = IEEE80211_HTCAP_MAXRXAMPDU_8K;
257178354Ssam	vap->iv_ampdu_density = IEEE80211_HTCAP_MPDUDENSITY_NA;
258178354Ssam	vap->iv_ampdu_limit = vap->iv_ampdu_rxmax;
259178354Ssam	vap->iv_amsdu_limit = vap->iv_htcaps & IEEE80211_HTCAP_MAXAMSDU;
260178354Ssam	/* tx aggregation traffic thresholds */
261178354Ssam	vap->iv_ampdu_mintraffic[WME_AC_BK] = 128;
262178354Ssam	vap->iv_ampdu_mintraffic[WME_AC_BE] = 64;
263178354Ssam	vap->iv_ampdu_mintraffic[WME_AC_VO] = 32;
264178354Ssam	vap->iv_ampdu_mintraffic[WME_AC_VI] = 32;
265178354Ssam
266178354Ssam	if (vap->iv_htcaps & IEEE80211_HTC_HT) {
267170530Ssam		/*
268173273Ssam		 * Device is HT capable; enable all HT-related
269173273Ssam		 * facilities by default.
270170530Ssam		 * XXX these choices may be too aggressive.
271170530Ssam		 */
272193655Ssam		vap->iv_flags_ht |= IEEE80211_FHT_HT
273193655Ssam				 |  IEEE80211_FHT_HTCOMPAT
274193655Ssam				 ;
275178354Ssam		if (vap->iv_htcaps & IEEE80211_HTCAP_SHORTGI20)
276193655Ssam			vap->iv_flags_ht |= IEEE80211_FHT_SHORTGI20;
277173273Ssam		/* XXX infer from channel list? */
278178354Ssam		if (vap->iv_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
279193655Ssam			vap->iv_flags_ht |= IEEE80211_FHT_USEHT40;
280178354Ssam			if (vap->iv_htcaps & IEEE80211_HTCAP_SHORTGI40)
281193655Ssam				vap->iv_flags_ht |= IEEE80211_FHT_SHORTGI40;
282170530Ssam		}
283183256Ssam		/* enable RIFS if capable */
284183256Ssam		if (vap->iv_htcaps & IEEE80211_HTC_RIFS)
285193655Ssam			vap->iv_flags_ht |= IEEE80211_FHT_RIFS;
286183256Ssam
287170530Ssam		/* NB: A-MPDU and A-MSDU rx are mandated, these are tx only */
288193655Ssam		vap->iv_flags_ht |= IEEE80211_FHT_AMPDU_RX;
289178354Ssam		if (vap->iv_htcaps & IEEE80211_HTC_AMPDU)
290193655Ssam			vap->iv_flags_ht |= IEEE80211_FHT_AMPDU_TX;
291193655Ssam		vap->iv_flags_ht |= IEEE80211_FHT_AMSDU_RX;
292178354Ssam		if (vap->iv_htcaps & IEEE80211_HTC_AMSDU)
293193655Ssam			vap->iv_flags_ht |= IEEE80211_FHT_AMSDU_TX;
294170530Ssam	}
295178354Ssam	/* NB: disable default legacy WDS, too many issues right now */
296178354Ssam	if (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY)
297193655Ssam		vap->iv_flags_ht &= ~IEEE80211_FHT_HT;
298170530Ssam}
299170530Ssam
300170530Ssamvoid
301178354Ssamieee80211_ht_vdetach(struct ieee80211vap *vap)
302170530Ssam{
303170530Ssam}
304170530Ssam
305219598Sbschmidtstatic int
306219598Sbschmidtht_getrate(struct ieee80211com *ic, int index, int mode, int ratetype)
307219598Sbschmidt{
308219598Sbschmidt	int mword, rate;
309219598Sbschmidt
310219598Sbschmidt	mword = ieee80211_rate2media(ic, index | IEEE80211_RATE_MCS, mode);
311219598Sbschmidt	if (IFM_SUBTYPE(mword) != IFM_IEEE80211_MCS)
312219598Sbschmidt		return (0);
313219598Sbschmidt	switch (ratetype) {
314219598Sbschmidt	case 0:
315219598Sbschmidt		rate = ieee80211_htrates[index].ht20_rate_800ns;
316219598Sbschmidt		break;
317219598Sbschmidt	case 1:
318219598Sbschmidt		rate = ieee80211_htrates[index].ht20_rate_400ns;
319219598Sbschmidt		break;
320219598Sbschmidt	case 2:
321219598Sbschmidt		rate = ieee80211_htrates[index].ht40_rate_800ns;
322219598Sbschmidt		break;
323219598Sbschmidt	default:
324219598Sbschmidt		rate = ieee80211_htrates[index].ht40_rate_400ns;
325219598Sbschmidt		break;
326219598Sbschmidt	}
327219598Sbschmidt	return (rate);
328219598Sbschmidt}
329219598Sbschmidt
330219598Sbschmidtstatic struct printranges {
331219598Sbschmidt	int	minmcs;
332219598Sbschmidt	int	maxmcs;
333219598Sbschmidt	int	txstream;
334219598Sbschmidt	int	ratetype;
335219598Sbschmidt	int	htcapflags;
336219598Sbschmidt} ranges[] = {
337219598Sbschmidt	{  0,  7, 1, 0, 0 },
338219598Sbschmidt	{  8, 15, 2, 0, 0 },
339219598Sbschmidt	{ 16, 23, 3, 0, 0 },
340219598Sbschmidt	{ 24, 31, 4, 0, 0 },
341219598Sbschmidt	{ 32,  0, 1, 2, IEEE80211_HTC_TXMCS32 },
342219598Sbschmidt	{ 33, 38, 2, 0, IEEE80211_HTC_TXUNEQUAL },
343219598Sbschmidt	{ 39, 52, 3, 0, IEEE80211_HTC_TXUNEQUAL },
344219598Sbschmidt	{ 53, 76, 4, 0, IEEE80211_HTC_TXUNEQUAL },
345219598Sbschmidt	{  0,  0, 0, 0, 0 },
346219598Sbschmidt};
347219598Sbschmidt
348170530Ssamstatic void
349219598Sbschmidtht_rateprint(struct ieee80211com *ic, int mode, int ratetype)
350170530Ssam{
351219598Sbschmidt	struct ifnet *ifp = ic->ic_ifp;
352219598Sbschmidt	int minrate, maxrate;
353219598Sbschmidt	struct printranges *range;
354170530Ssam
355219598Sbschmidt	for (range = ranges; range->txstream != 0; range++) {
356219598Sbschmidt		if (ic->ic_txstream < range->txstream)
357170530Ssam			continue;
358219598Sbschmidt		if (range->htcapflags &&
359219598Sbschmidt		    (ic->ic_htcaps & range->htcapflags) == 0)
360219598Sbschmidt			continue;
361219598Sbschmidt		if (ratetype < range->ratetype)
362219598Sbschmidt			continue;
363219598Sbschmidt		minrate = ht_getrate(ic, range->minmcs, mode, ratetype);
364219598Sbschmidt		maxrate = ht_getrate(ic, range->maxmcs, mode, ratetype);
365219598Sbschmidt		if (range->maxmcs) {
366219598Sbschmidt			if_printf(ifp, "MCS %d-%d: %d%sMbps - %d%sMbps\n",
367219598Sbschmidt			    range->minmcs, range->maxmcs,
368219598Sbschmidt			    minrate/2, ((minrate & 0x1) != 0 ? ".5" : ""),
369219598Sbschmidt			    maxrate/2, ((maxrate & 0x1) != 0 ? ".5" : ""));
370219598Sbschmidt		} else {
371219598Sbschmidt			if_printf(ifp, "MCS %d: %d%sMbps\n", range->minmcs,
372219598Sbschmidt			    minrate/2, ((minrate & 0x1) != 0 ? ".5" : ""));
373205277Srpaulo		}
374170530Ssam	}
375170530Ssam}
376170530Ssam
377205277Srpaulostatic void
378219598Sbschmidtht_announce(struct ieee80211com *ic, int mode)
379205277Srpaulo{
380205277Srpaulo	struct ifnet *ifp = ic->ic_ifp;
381205277Srpaulo	const char *modestr = ieee80211_phymode_name[mode];
382219598Sbschmidt
383219598Sbschmidt	if_printf(ifp, "%s MCS 20Mhz\n", modestr);
384219598Sbschmidt	ht_rateprint(ic, mode, 0);
385219598Sbschmidt	if (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20) {
386219598Sbschmidt		if_printf(ifp, "%s MCS 20Mhz SGI\n", modestr);
387219598Sbschmidt		ht_rateprint(ic, mode, 1);
388219598Sbschmidt	}
389219598Sbschmidt	if (ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
390219598Sbschmidt		if_printf(ifp, "%s MCS 40Mhz:\n", modestr);
391219598Sbschmidt		ht_rateprint(ic, mode, 2);
392219598Sbschmidt	}
393219598Sbschmidt	if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
394219598Sbschmidt	    (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40)) {
395219598Sbschmidt		if_printf(ifp, "%s MCS 40Mhz SGI:\n", modestr);
396219598Sbschmidt		ht_rateprint(ic, mode, 3);
397219598Sbschmidt	}
398205277Srpaulo}
399205277Srpaulo
400170530Ssamvoid
401170530Ssamieee80211_ht_announce(struct ieee80211com *ic)
402170530Ssam{
403219598Sbschmidt	struct ifnet *ifp = ic->ic_ifp;
404219598Sbschmidt
405219598Sbschmidt	if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) ||
406219598Sbschmidt	    isset(ic->ic_modecaps, IEEE80211_MODE_11NG))
407219598Sbschmidt		if_printf(ifp, "%dT%dR\n", ic->ic_txstream, ic->ic_rxstream);
408170530Ssam	if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA))
409219598Sbschmidt		ht_announce(ic, IEEE80211_MODE_11NA);
410170530Ssam	if (isset(ic->ic_modecaps, IEEE80211_MODE_11NG))
411219598Sbschmidt		ht_announce(ic, IEEE80211_MODE_11NG);
412170530Ssam}
413170530Ssam
414219602Sbschmidtstatic struct ieee80211_htrateset htrateset;
415219602Sbschmidt
416170530Ssamconst struct ieee80211_htrateset *
417170530Ssamieee80211_get_suphtrates(struct ieee80211com *ic,
418219602Sbschmidt    const struct ieee80211_channel *c)
419170530Ssam{
420219602Sbschmidt#define	ADDRATE(x)	do {						\
421219602Sbschmidt	htrateset.rs_rates[htrateset.rs_nrates] = x;			\
422219602Sbschmidt	htrateset.rs_nrates++;						\
423219602Sbschmidt} while (0)
424219602Sbschmidt	int i;
425219602Sbschmidt
426219602Sbschmidt	memset(&htrateset, 0, sizeof(struct ieee80211_htrateset));
427219602Sbschmidt	for (i = 0; i < ic->ic_txstream * 8; i++)
428219602Sbschmidt		ADDRATE(i);
429219602Sbschmidt	if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
430219602Sbschmidt	    (ic->ic_htcaps & IEEE80211_HTC_TXMCS32))
431219602Sbschmidt		ADDRATE(i);
432219602Sbschmidt	if (ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL) {
433219602Sbschmidt		if (ic->ic_txstream >= 2) {
434219602Sbschmidt			 for (i = 33; i <= 38; i++)
435219602Sbschmidt				ADDRATE(i);
436219602Sbschmidt		}
437219602Sbschmidt		if (ic->ic_txstream >= 3) {
438219602Sbschmidt			for (i = 39; i <= 52; i++)
439219602Sbschmidt				ADDRATE(i);
440219602Sbschmidt		}
441219602Sbschmidt		if (ic->ic_txstream == 4) {
442219602Sbschmidt			for (i = 53; i <= 76; i++)
443219602Sbschmidt				ADDRATE(i);
444219602Sbschmidt		}
445219602Sbschmidt	}
446219602Sbschmidt	return &htrateset;
447219602Sbschmidt#undef	ADDRATE
448170530Ssam}
449170530Ssam
450170530Ssam/*
451170530Ssam * Receive processing.
452170530Ssam */
453170530Ssam
454170530Ssam/*
455170530Ssam * Decap the encapsulated A-MSDU frames and dispatch all but
456170530Ssam * the last for delivery.  The last frame is returned for
457170530Ssam * delivery via the normal path.
458170530Ssam */
459170530Ssamstruct mbuf *
460170530Ssamieee80211_decap_amsdu(struct ieee80211_node *ni, struct mbuf *m)
461170530Ssam{
462178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
463173462Ssam	int framelen;
464170530Ssam	struct mbuf *n;
465170530Ssam
466170530Ssam	/* discard 802.3 header inserted by ieee80211_decap */
467170530Ssam	m_adj(m, sizeof(struct ether_header));
468170530Ssam
469178354Ssam	vap->iv_stats.is_amsdu_decap++;
470170530Ssam
471170530Ssam	for (;;) {
472170530Ssam		/*
473170530Ssam		 * Decap the first frame, bust it apart from the
474170530Ssam		 * remainder and deliver.  We leave the last frame
475170530Ssam		 * delivery to the caller (for consistency with other
476170530Ssam		 * code paths, could also do it here).
477170530Ssam		 */
478170530Ssam		m = ieee80211_decap1(m, &framelen);
479170530Ssam		if (m == NULL) {
480178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
481173462Ssam			    ni->ni_macaddr, "a-msdu", "%s", "decap failed");
482178354Ssam			vap->iv_stats.is_amsdu_tooshort++;
483170530Ssam			return NULL;
484170530Ssam		}
485173462Ssam		if (m->m_pkthdr.len == framelen)
486170530Ssam			break;
487170530Ssam		n = m_split(m, framelen, M_NOWAIT);
488170530Ssam		if (n == NULL) {
489178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
490170530Ssam			    ni->ni_macaddr, "a-msdu",
491170530Ssam			    "%s", "unable to split encapsulated frames");
492178354Ssam			vap->iv_stats.is_amsdu_split++;
493170530Ssam			m_freem(m);			/* NB: must reclaim */
494170530Ssam			return NULL;
495170530Ssam		}
496178354Ssam		vap->iv_deliver_data(vap, ni, m);
497170530Ssam
498170530Ssam		/*
499170530Ssam		 * Remove frame contents; each intermediate frame
500170530Ssam		 * is required to be aligned to a 4-byte boundary.
501170530Ssam		 */
502170530Ssam		m = n;
503170530Ssam		m_adj(m, roundup2(framelen, 4) - framelen);	/* padding */
504170530Ssam	}
505170530Ssam	return m;				/* last delivered by caller */
506170530Ssam}
507170530Ssam
508170530Ssam/*
509170530Ssam * Purge all frames in the A-MPDU re-order queue.
510170530Ssam */
511170530Ssamstatic void
512170530Ssamampdu_rx_purge(struct ieee80211_rx_ampdu *rap)
513170530Ssam{
514170530Ssam	struct mbuf *m;
515170530Ssam	int i;
516170530Ssam
517170530Ssam	for (i = 0; i < rap->rxa_wnd; i++) {
518170530Ssam		m = rap->rxa_m[i];
519170530Ssam		if (m != NULL) {
520170530Ssam			rap->rxa_m[i] = NULL;
521170530Ssam			rap->rxa_qbytes -= m->m_pkthdr.len;
522170530Ssam			m_freem(m);
523170530Ssam			if (--rap->rxa_qframes == 0)
524170530Ssam				break;
525170530Ssam		}
526170530Ssam	}
527170530Ssam	KASSERT(rap->rxa_qbytes == 0 && rap->rxa_qframes == 0,
528170530Ssam	    ("lost %u data, %u frames on ampdu rx q",
529170530Ssam	    rap->rxa_qbytes, rap->rxa_qframes));
530170530Ssam}
531170530Ssam
532170530Ssam/*
533178354Ssam * Start A-MPDU rx/re-order processing for the specified TID.
534178354Ssam */
535191552Ssamstatic int
536191552Ssamampdu_rx_start(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap,
537191552Ssam	int baparamset, int batimeout, int baseqctl)
538178354Ssam{
539191552Ssam	int bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
540191552Ssam
541178354Ssam	if (rap->rxa_flags & IEEE80211_AGGR_RUNNING) {
542178354Ssam		/*
543178354Ssam		 * AMPDU previously setup and not terminated with a DELBA,
544178354Ssam		 * flush the reorder q's in case anything remains.
545178354Ssam		 */
546178354Ssam		ampdu_rx_purge(rap);
547178354Ssam	}
548178354Ssam	memset(rap, 0, sizeof(*rap));
549178354Ssam	rap->rxa_wnd = (bufsiz == 0) ?
550178354Ssam	    IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
551191552Ssam	rap->rxa_start = MS(baseqctl, IEEE80211_BASEQ_START);
552178354Ssam	rap->rxa_flags |=  IEEE80211_AGGR_RUNNING | IEEE80211_AGGR_XCHGPEND;
553191552Ssam
554191552Ssam	return 0;
555178354Ssam}
556178354Ssam
557178354Ssam/*
558170530Ssam * Stop A-MPDU rx processing for the specified TID.
559170530Ssam */
560170530Ssamstatic void
561191552Ssamampdu_rx_stop(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
562170530Ssam{
563205277Srpaulo
564170530Ssam	ampdu_rx_purge(rap);
565178354Ssam	rap->rxa_flags &= ~(IEEE80211_AGGR_RUNNING | IEEE80211_AGGR_XCHGPEND);
566170530Ssam}
567170530Ssam
568170530Ssam/*
569170530Ssam * Dispatch a frame from the A-MPDU reorder queue.  The
570170530Ssam * frame is fed back into ieee80211_input marked with an
571183247Ssam * M_AMPDU_MPDU flag so it doesn't come back to us (it also
572170530Ssam * permits ieee80211_input to optimize re-processing).
573170530Ssam */
574170530Ssamstatic __inline void
575170530Ssamampdu_dispatch(struct ieee80211_node *ni, struct mbuf *m)
576170530Ssam{
577183247Ssam	m->m_flags |= M_AMPDU_MPDU;	/* bypass normal processing */
578192468Ssam	/* NB: rssi and noise are ignored w/ M_AMPDU_MPDU set */
579192468Ssam	(void) ieee80211_input(ni, m, 0, 0);
580170530Ssam}
581170530Ssam
582170530Ssam/*
583170530Ssam * Dispatch as many frames as possible from the re-order queue.
584170530Ssam * Frames will always be "at the front"; we process all frames
585170530Ssam * up to the first empty slot in the window.  On completion we
586170530Ssam * cleanup state if there are still pending frames in the current
587170530Ssam * BA window.  We assume the frame at slot 0 is already handled
588170530Ssam * by the caller; we always start at slot 1.
589170530Ssam */
590170530Ssamstatic void
591170530Ssamampdu_rx_dispatch(struct ieee80211_rx_ampdu *rap, struct ieee80211_node *ni)
592170530Ssam{
593178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
594170530Ssam	struct mbuf *m;
595170530Ssam	int i;
596170530Ssam
597170530Ssam	/* flush run of frames */
598170530Ssam	for (i = 1; i < rap->rxa_wnd; i++) {
599170530Ssam		m = rap->rxa_m[i];
600170530Ssam		if (m == NULL)
601170530Ssam			break;
602170530Ssam		rap->rxa_m[i] = NULL;
603170530Ssam		rap->rxa_qbytes -= m->m_pkthdr.len;
604170530Ssam		rap->rxa_qframes--;
605170530Ssam
606170530Ssam		ampdu_dispatch(ni, m);
607170530Ssam	}
608170530Ssam	/*
609170530Ssam	 * If frames remain, copy the mbuf pointers down so
610170530Ssam	 * they correspond to the offsets in the new window.
611170530Ssam	 */
612170530Ssam	if (rap->rxa_qframes != 0) {
613170530Ssam		int n = rap->rxa_qframes, j;
614170530Ssam		for (j = i+1; j < rap->rxa_wnd; j++) {
615170530Ssam			if (rap->rxa_m[j] != NULL) {
616170530Ssam				rap->rxa_m[j-i] = rap->rxa_m[j];
617170530Ssam				rap->rxa_m[j] = NULL;
618170530Ssam				if (--n == 0)
619170530Ssam					break;
620170530Ssam			}
621170530Ssam		}
622170530Ssam		KASSERT(n == 0, ("lost %d frames", n));
623178354Ssam		vap->iv_stats.is_ampdu_rx_copy += rap->rxa_qframes;
624170530Ssam	}
625173273Ssam	/*
626173273Ssam	 * Adjust the start of the BA window to
627173273Ssam	 * reflect the frames just dispatched.
628173273Ssam	 */
629173273Ssam	rap->rxa_start = IEEE80211_SEQ_ADD(rap->rxa_start, i);
630178354Ssam	vap->iv_stats.is_ampdu_rx_oor += i;
631170530Ssam}
632170530Ssam
633173273Ssam#ifdef IEEE80211_AMPDU_AGE
634170530Ssam/*
635173273Ssam * Dispatch all frames in the A-MPDU re-order queue.
636170530Ssam */
637170530Ssamstatic void
638173273Ssamampdu_rx_flush(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
639170530Ssam{
640178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
641170530Ssam	struct mbuf *m;
642170530Ssam	int i;
643170530Ssam
644173273Ssam	for (i = 0; i < rap->rxa_wnd; i++) {
645170530Ssam		m = rap->rxa_m[i];
646170530Ssam		if (m == NULL)
647170530Ssam			continue;
648170530Ssam		rap->rxa_m[i] = NULL;
649170530Ssam		rap->rxa_qbytes -= m->m_pkthdr.len;
650173273Ssam		rap->rxa_qframes--;
651178354Ssam		vap->iv_stats.is_ampdu_rx_oor++;
652173273Ssam
653170530Ssam		ampdu_dispatch(ni, m);
654173273Ssam		if (rap->rxa_qframes == 0)
655170530Ssam			break;
656170530Ssam	}
657170530Ssam}
658173273Ssam#endif /* IEEE80211_AMPDU_AGE */
659170530Ssam
660170530Ssam/*
661173273Ssam * Dispatch all frames in the A-MPDU re-order queue
662173273Ssam * preceding the specified sequence number.  This logic
663173273Ssam * handles window moves due to a received MSDU or BAR.
664173273Ssam */
665173273Ssamstatic void
666173273Ssamampdu_rx_flush_upto(struct ieee80211_node *ni,
667173273Ssam	struct ieee80211_rx_ampdu *rap, ieee80211_seq winstart)
668173273Ssam{
669178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
670173273Ssam	struct mbuf *m;
671173273Ssam	ieee80211_seq seqno;
672173273Ssam	int i;
673173273Ssam
674173273Ssam	/*
675173273Ssam	 * Flush any complete MSDU's with a sequence number lower
676173273Ssam	 * than winstart.  Gaps may exist.  Note that we may actually
677173273Ssam	 * dispatch frames past winstart if a run continues; this is
678173273Ssam	 * an optimization that avoids having to do a separate pass
679173273Ssam	 * to dispatch frames after moving the BA window start.
680173273Ssam	 */
681173273Ssam	seqno = rap->rxa_start;
682173273Ssam	for (i = 0; i < rap->rxa_wnd; i++) {
683173273Ssam		m = rap->rxa_m[i];
684173273Ssam		if (m != NULL) {
685173273Ssam			rap->rxa_m[i] = NULL;
686173273Ssam			rap->rxa_qbytes -= m->m_pkthdr.len;
687173273Ssam			rap->rxa_qframes--;
688178354Ssam			vap->iv_stats.is_ampdu_rx_oor++;
689173273Ssam
690173273Ssam			ampdu_dispatch(ni, m);
691173273Ssam		} else {
692173273Ssam			if (!IEEE80211_SEQ_BA_BEFORE(seqno, winstart))
693173273Ssam				break;
694173273Ssam		}
695173273Ssam		seqno = IEEE80211_SEQ_INC(seqno);
696173273Ssam	}
697173273Ssam	/*
698173273Ssam	 * If frames remain, copy the mbuf pointers down so
699173273Ssam	 * they correspond to the offsets in the new window.
700173273Ssam	 */
701173273Ssam	if (rap->rxa_qframes != 0) {
702173273Ssam		int n = rap->rxa_qframes, j;
703178354Ssam
704178354Ssam		/* NB: this loop assumes i > 0 and/or rxa_m[0] is NULL */
705178354Ssam		KASSERT(rap->rxa_m[0] == NULL,
706178354Ssam		    ("%s: BA window slot 0 occupied", __func__));
707173273Ssam		for (j = i+1; j < rap->rxa_wnd; j++) {
708173273Ssam			if (rap->rxa_m[j] != NULL) {
709173273Ssam				rap->rxa_m[j-i] = rap->rxa_m[j];
710173273Ssam				rap->rxa_m[j] = NULL;
711173273Ssam				if (--n == 0)
712173273Ssam					break;
713173273Ssam			}
714173273Ssam		}
715173273Ssam		KASSERT(n == 0, ("%s: lost %d frames, qframes %d off %d "
716173273Ssam		    "BA win <%d:%d> winstart %d",
717173273Ssam		    __func__, n, rap->rxa_qframes, i, rap->rxa_start,
718173273Ssam		    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
719173273Ssam		    winstart));
720178354Ssam		vap->iv_stats.is_ampdu_rx_copy += rap->rxa_qframes;
721173273Ssam	}
722173273Ssam	/*
723173273Ssam	 * Move the start of the BA window; we use the
724173273Ssam	 * sequence number of the last MSDU that was
725173273Ssam	 * passed up the stack+1 or winstart if stopped on
726173273Ssam	 * a gap in the reorder buffer.
727173273Ssam	 */
728173273Ssam	rap->rxa_start = seqno;
729173273Ssam}
730173273Ssam
731173273Ssam/*
732170530Ssam * Process a received QoS data frame for an HT station.  Handle
733170530Ssam * A-MPDU reordering: if this frame is received out of order
734170530Ssam * and falls within the BA window hold onto it.  Otherwise if
735173273Ssam * this frame completes a run, flush any pending frames.  We
736170530Ssam * return 1 if the frame is consumed.  A 0 is returned if
737170530Ssam * the frame should be processed normally by the caller.
738170530Ssam */
739170530Ssamint
740170530Ssamieee80211_ampdu_reorder(struct ieee80211_node *ni, struct mbuf *m)
741170530Ssam{
742170530Ssam#define	IEEE80211_FC0_QOSDATA \
743170530Ssam	(IEEE80211_FC0_TYPE_DATA|IEEE80211_FC0_SUBTYPE_QOS|IEEE80211_FC0_VERSION_0)
744173273Ssam#define	PROCESS		0	/* caller should process frame */
745173273Ssam#define	CONSUMED	1	/* frame consumed, caller does nothing */
746178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
747170530Ssam	struct ieee80211_qosframe *wh;
748170530Ssam	struct ieee80211_rx_ampdu *rap;
749170530Ssam	ieee80211_seq rxseq;
750170530Ssam	uint8_t tid;
751170530Ssam	int off;
752170530Ssam
753183247Ssam	KASSERT((m->m_flags & (M_AMPDU | M_AMPDU_MPDU)) == M_AMPDU,
754183247Ssam	    ("!a-mpdu or already re-ordered, flags 0x%x", m->m_flags));
755170530Ssam	KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT sta"));
756170530Ssam
757170530Ssam	/* NB: m_len known to be sufficient */
758170530Ssam	wh = mtod(m, struct ieee80211_qosframe *);
759183247Ssam	if (wh->i_fc[0] != IEEE80211_FC0_QOSDATA) {
760183247Ssam		/*
761183247Ssam		 * Not QoS data, shouldn't get here but just
762183247Ssam		 * return it to the caller for processing.
763183247Ssam		 */
764183247Ssam		return PROCESS;
765183247Ssam	}
766193840Ssam	if (IEEE80211_IS_DSTODS(wh))
767173273Ssam		tid = ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0];
768173273Ssam	else
769173273Ssam		tid = wh->i_qos[0];
770173273Ssam	tid &= IEEE80211_QOS_TID;
771170530Ssam	rap = &ni->ni_rx_ampdu[tid];
772170530Ssam	if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
773170530Ssam		/*
774170530Ssam		 * No ADDBA request yet, don't touch.
775170530Ssam		 */
776173273Ssam		return PROCESS;
777170530Ssam	}
778182827Ssam	rxseq = le16toh(*(uint16_t *)wh->i_seq);
779182827Ssam	if ((rxseq & IEEE80211_SEQ_FRAG_MASK) != 0) {
780182827Ssam		/*
781182827Ssam		 * Fragments are not allowed; toss.
782182827Ssam		 */
783182827Ssam		IEEE80211_DISCARD_MAC(vap,
784182827Ssam		    IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
785182827Ssam		    "A-MPDU", "fragment, rxseq 0x%x tid %u%s", rxseq, tid,
786182827Ssam		    wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
787182827Ssam		vap->iv_stats.is_ampdu_rx_drop++;
788182827Ssam		IEEE80211_NODE_STAT(ni, rx_drop);
789182827Ssam		m_freem(m);
790182827Ssam		return CONSUMED;
791182827Ssam	}
792182827Ssam	rxseq >>= IEEE80211_SEQ_SEQ_SHIFT;
793173273Ssam	rap->rxa_nframes++;
794173273Ssamagain:
795170530Ssam	if (rxseq == rap->rxa_start) {
796170530Ssam		/*
797170530Ssam		 * First frame in window.
798170530Ssam		 */
799170530Ssam		if (rap->rxa_qframes != 0) {
800170530Ssam			/*
801170530Ssam			 * Dispatch as many packets as we can.
802170530Ssam			 */
803170530Ssam			KASSERT(rap->rxa_m[0] == NULL, ("unexpected dup"));
804170530Ssam			ampdu_dispatch(ni, m);
805170530Ssam			ampdu_rx_dispatch(rap, ni);
806173273Ssam			return CONSUMED;
807170530Ssam		} else {
808170530Ssam			/*
809170530Ssam			 * In order; advance window and notify
810170530Ssam			 * caller to dispatch directly.
811170530Ssam			 */
812170530Ssam			rap->rxa_start = IEEE80211_SEQ_INC(rxseq);
813173273Ssam			return PROCESS;
814170530Ssam		}
815170530Ssam	}
816170530Ssam	/*
817173273Ssam	 * Frame is out of order; store if in the BA window.
818170530Ssam	 */
819170530Ssam	/* calculate offset in BA window */
820170530Ssam	off = IEEE80211_SEQ_SUB(rxseq, rap->rxa_start);
821173273Ssam	if (off < rap->rxa_wnd) {
822170530Ssam		/*
823173273Ssam		 * Common case (hopefully): in the BA window.
824205277Srpaulo		 * Sec 9.10.7.6.2 a) (p.137)
825173273Ssam		 */
826173273Ssam#ifdef IEEE80211_AMPDU_AGE
827173273Ssam		/*
828173273Ssam		 * Check for frames sitting too long in the reorder queue.
829173273Ssam		 * This should only ever happen if frames are not delivered
830173273Ssam		 * without the sender otherwise notifying us (e.g. with a
831173273Ssam		 * BAR to move the window).  Typically this happens because
832173273Ssam		 * of vendor bugs that cause the sequence number to jump.
833173273Ssam		 * When this happens we get a gap in the reorder queue that
834173273Ssam		 * leaves frame sitting on the queue until they get pushed
835173273Ssam		 * out due to window moves.  When the vendor does not send
836173273Ssam		 * BAR this move only happens due to explicit packet sends
837170530Ssam		 *
838173273Ssam		 * NB: we only track the time of the oldest frame in the
839173273Ssam		 * reorder q; this means that if we flush we might push
840173273Ssam		 * frames that still "new"; if this happens then subsequent
841173273Ssam		 * frames will result in BA window moves which cost something
842173273Ssam		 * but is still better than a big throughput dip.
843170530Ssam		 */
844173273Ssam		if (rap->rxa_qframes != 0) {
845173273Ssam			/* XXX honor batimeout? */
846173273Ssam			if (ticks - rap->rxa_age > ieee80211_ampdu_age) {
847173273Ssam				/*
848173273Ssam				 * Too long since we received the first
849173273Ssam				 * frame; flush the reorder buffer.
850173273Ssam				 */
851173273Ssam				if (rap->rxa_qframes != 0) {
852178354Ssam					vap->iv_stats.is_ampdu_rx_age +=
853173273Ssam					    rap->rxa_qframes;
854173273Ssam					ampdu_rx_flush(ni, rap);
855173273Ssam				}
856173273Ssam				rap->rxa_start = IEEE80211_SEQ_INC(rxseq);
857173273Ssam				return PROCESS;
858173273Ssam			}
859173273Ssam		} else {
860173273Ssam			/*
861173273Ssam			 * First frame, start aging timer.
862173273Ssam			 */
863173273Ssam			rap->rxa_age = ticks;
864173273Ssam		}
865173273Ssam#endif /* IEEE80211_AMPDU_AGE */
866173273Ssam		/* save packet */
867173273Ssam		if (rap->rxa_m[off] == NULL) {
868173273Ssam			rap->rxa_m[off] = m;
869173273Ssam			rap->rxa_qframes++;
870173273Ssam			rap->rxa_qbytes += m->m_pkthdr.len;
871178354Ssam			vap->iv_stats.is_ampdu_rx_reorder++;
872173273Ssam		} else {
873178354Ssam			IEEE80211_DISCARD_MAC(vap,
874173273Ssam			    IEEE80211_MSG_INPUT | IEEE80211_MSG_11N,
875173273Ssam			    ni->ni_macaddr, "a-mpdu duplicate",
876173273Ssam			    "seqno %u tid %u BA win <%u:%u>",
877173273Ssam			    rxseq, tid, rap->rxa_start,
878173273Ssam			    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1));
879178354Ssam			vap->iv_stats.is_rx_dup++;
880173273Ssam			IEEE80211_NODE_STAT(ni, rx_dup);
881173273Ssam			m_freem(m);
882173273Ssam		}
883173273Ssam		return CONSUMED;
884173273Ssam	}
885173273Ssam	if (off < IEEE80211_SEQ_BA_RANGE) {
886173273Ssam		/*
887173273Ssam		 * Outside the BA window, but within range;
888173273Ssam		 * flush the reorder q and move the window.
889205277Srpaulo		 * Sec 9.10.7.6.2 b) (p.138)
890173273Ssam		 */
891178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
892173273Ssam		    "move BA win <%u:%u> (%u frames) rxseq %u tid %u",
893170530Ssam		    rap->rxa_start,
894173273Ssam		    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
895170530Ssam		    rap->rxa_qframes, rxseq, tid);
896178354Ssam		vap->iv_stats.is_ampdu_rx_move++;
897170530Ssam
898173273Ssam		/*
899173273Ssam		 * The spec says to flush frames up to but not including:
900173273Ssam		 * 	WinStart_B = rxseq - rap->rxa_wnd + 1
901173273Ssam		 * Then insert the frame or notify the caller to process
902173273Ssam		 * it immediately.  We can safely do this by just starting
903173273Ssam		 * over again because we know the frame will now be within
904173273Ssam		 * the BA window.
905173273Ssam		 */
906173273Ssam		/* NB: rxa_wnd known to be >0 */
907173273Ssam		ampdu_rx_flush_upto(ni, rap,
908173273Ssam		    IEEE80211_SEQ_SUB(rxseq, rap->rxa_wnd-1));
909173273Ssam		goto again;
910170530Ssam	} else {
911170530Ssam		/*
912173273Ssam		 * Outside the BA window and out of range; toss.
913205277Srpaulo		 * Sec 9.10.7.6.2 c) (p.138)
914170530Ssam		 */
915178354Ssam		IEEE80211_DISCARD_MAC(vap,
916173273Ssam		    IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
917178354Ssam		    "MPDU", "BA win <%u:%u> (%u frames) rxseq %u tid %u%s",
918173273Ssam		    rap->rxa_start,
919173273Ssam		    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
920173273Ssam		    rap->rxa_qframes, rxseq, tid,
921173273Ssam		    wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
922178354Ssam		vap->iv_stats.is_ampdu_rx_drop++;
923173273Ssam		IEEE80211_NODE_STAT(ni, rx_drop);
924170530Ssam		m_freem(m);
925173273Ssam		return CONSUMED;
926170530Ssam	}
927173273Ssam#undef CONSUMED
928173273Ssam#undef PROCESS
929170530Ssam#undef IEEE80211_FC0_QOSDATA
930170530Ssam}
931170530Ssam
932170530Ssam/*
933170530Ssam * Process a BAR ctl frame.  Dispatch all frames up to
934170530Ssam * the sequence number of the frame.  If this frame is
935173273Ssam * out of range it's discarded.
936170530Ssam */
937170530Ssamvoid
938170530Ssamieee80211_recv_bar(struct ieee80211_node *ni, struct mbuf *m0)
939170530Ssam{
940178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
941170530Ssam	struct ieee80211_frame_bar *wh;
942170530Ssam	struct ieee80211_rx_ampdu *rap;
943170530Ssam	ieee80211_seq rxseq;
944170530Ssam	int tid, off;
945170530Ssam
946173273Ssam	if (!ieee80211_recv_bar_ena) {
947173273Ssam#if 0
948178354Ssam		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_11N,
949173273Ssam		    ni->ni_macaddr, "BAR", "%s", "processing disabled");
950173273Ssam#endif
951178354Ssam		vap->iv_stats.is_ampdu_bar_bad++;
952173273Ssam		return;
953173273Ssam	}
954170530Ssam	wh = mtod(m0, struct ieee80211_frame_bar *);
955170530Ssam	/* XXX check basic BAR */
956170530Ssam	tid = MS(le16toh(wh->i_ctl), IEEE80211_BAR_TID);
957170530Ssam	rap = &ni->ni_rx_ampdu[tid];
958170530Ssam	if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
959170530Ssam		/*
960170530Ssam		 * No ADDBA request yet, don't touch.
961170530Ssam		 */
962178354Ssam		IEEE80211_DISCARD_MAC(vap,
963170530Ssam		    IEEE80211_MSG_INPUT | IEEE80211_MSG_11N,
964170530Ssam		    ni->ni_macaddr, "BAR", "no BA stream, tid %u", tid);
965178354Ssam		vap->iv_stats.is_ampdu_bar_bad++;
966170530Ssam		return;
967170530Ssam	}
968178354Ssam	vap->iv_stats.is_ampdu_bar_rx++;
969170530Ssam	rxseq = le16toh(wh->i_seq) >> IEEE80211_SEQ_SEQ_SHIFT;
970173273Ssam	if (rxseq == rap->rxa_start)
971173273Ssam		return;
972170530Ssam	/* calculate offset in BA window */
973170530Ssam	off = IEEE80211_SEQ_SUB(rxseq, rap->rxa_start);
974173273Ssam	if (off < IEEE80211_SEQ_BA_RANGE) {
975170530Ssam		/*
976173273Ssam		 * Flush the reorder q up to rxseq and move the window.
977205277Srpaulo		 * Sec 9.10.7.6.3 a) (p.138)
978170530Ssam		 */
979178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
980173273Ssam		    "BAR moves BA win <%u:%u> (%u frames) rxseq %u tid %u",
981170530Ssam		    rap->rxa_start,
982173273Ssam		    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
983173273Ssam		    rap->rxa_qframes, rxseq, tid);
984178354Ssam		vap->iv_stats.is_ampdu_bar_move++;
985173273Ssam
986173273Ssam		ampdu_rx_flush_upto(ni, rap, rxseq);
987173273Ssam		if (off >= rap->rxa_wnd) {
988173273Ssam			/*
989173273Ssam			 * BAR specifies a window start to the right of BA
990173273Ssam			 * window; we must move it explicitly since
991173273Ssam			 * ampdu_rx_flush_upto will not.
992173273Ssam			 */
993173273Ssam			rap->rxa_start = rxseq;
994170530Ssam		}
995173273Ssam	} else {
996170530Ssam		/*
997173273Ssam		 * Out of range; toss.
998205277Srpaulo		 * Sec 9.10.7.6.3 b) (p.138)
999170530Ssam		 */
1000178354Ssam		IEEE80211_DISCARD_MAC(vap,
1001173273Ssam		    IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
1002173273Ssam		    "BAR", "BA win <%u:%u> (%u frames) rxseq %u tid %u%s",
1003173273Ssam		    rap->rxa_start,
1004173273Ssam		    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
1005173273Ssam		    rap->rxa_qframes, rxseq, tid,
1006173273Ssam		    wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
1007178354Ssam		vap->iv_stats.is_ampdu_bar_oow++;
1008173273Ssam		IEEE80211_NODE_STAT(ni, rx_drop);
1009170530Ssam	}
1010170530Ssam}
1011170530Ssam
1012170530Ssam/*
1013170530Ssam * Setup HT-specific state in a node.  Called only
1014170530Ssam * when HT use is negotiated so we don't do extra
1015170530Ssam * work for temporary and/or legacy sta's.
1016170530Ssam */
1017170530Ssamvoid
1018183254Ssamieee80211_ht_node_init(struct ieee80211_node *ni)
1019170530Ssam{
1020170530Ssam	struct ieee80211_tx_ampdu *tap;
1021170530Ssam	int ac;
1022170530Ssam
1023173273Ssam	if (ni->ni_flags & IEEE80211_NODE_HT) {
1024173273Ssam		/*
1025173273Ssam		 * Clean AMPDU state on re-associate.  This handles the case
1026173273Ssam		 * where a station leaves w/o notifying us and then returns
1027173273Ssam		 * before node is reaped for inactivity.
1028173273Ssam		 */
1029173273Ssam		ieee80211_ht_node_cleanup(ni);
1030173273Ssam	}
1031170530Ssam	for (ac = 0; ac < WME_NUM_AC; ac++) {
1032170530Ssam		tap = &ni->ni_tx_ampdu[ac];
1033170530Ssam		tap->txa_ac = ac;
1034184280Ssam		tap->txa_ni = ni;
1035173273Ssam		/* NB: further initialization deferred */
1036170530Ssam	}
1037173273Ssam	ni->ni_flags |= IEEE80211_NODE_HT | IEEE80211_NODE_AMPDU;
1038170530Ssam}
1039170530Ssam
1040170530Ssam/*
1041170530Ssam * Cleanup HT-specific state in a node.  Called only
1042170530Ssam * when HT use has been marked.
1043170530Ssam */
1044170530Ssamvoid
1045170530Ssamieee80211_ht_node_cleanup(struct ieee80211_node *ni)
1046170530Ssam{
1047191552Ssam	struct ieee80211com *ic = ni->ni_ic;
1048170530Ssam	int i;
1049170530Ssam
1050170530Ssam	KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT node"));
1051170530Ssam
1052170530Ssam	/* XXX optimize this */
1053170530Ssam	for (i = 0; i < WME_NUM_AC; i++) {
1054170530Ssam		struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[i];
1055184280Ssam		if (tap->txa_flags & IEEE80211_AGGR_SETUP)
1056184280Ssam			ampdu_tx_stop(tap);
1057170530Ssam	}
1058170530Ssam	for (i = 0; i < WME_NUM_TID; i++)
1059191552Ssam		ic->ic_ampdu_rx_stop(ni, &ni->ni_rx_ampdu[i]);
1060170530Ssam
1061170530Ssam	ni->ni_htcap = 0;
1062182828Ssam	ni->ni_flags &= ~IEEE80211_NODE_HT_ALL;
1063170530Ssam}
1064170530Ssam
1065178354Ssam/*
1066178354Ssam * Age out HT resources for a station.
1067178354Ssam */
1068178354Ssamvoid
1069178354Ssamieee80211_ht_node_age(struct ieee80211_node *ni)
1070178354Ssam{
1071178354Ssam#ifdef IEEE80211_AMPDU_AGE
1072178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1073178354Ssam	uint8_t tid;
1074178354Ssam#endif
1075178354Ssam
1076178354Ssam	KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT sta"));
1077178354Ssam
1078178354Ssam#ifdef IEEE80211_AMPDU_AGE
1079178354Ssam	for (tid = 0; tid < WME_NUM_TID; tid++) {
1080178354Ssam		struct ieee80211_rx_ampdu *rap;
1081178354Ssam
1082178354Ssam		rap = &ni->ni_rx_ampdu[tid];
1083178354Ssam		if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0)
1084178354Ssam			continue;
1085178354Ssam		if (rap->rxa_qframes == 0)
1086178354Ssam			continue;
1087178354Ssam		/*
1088178354Ssam		 * Check for frames sitting too long in the reorder queue.
1089178354Ssam		 * See above for more details on what's happening here.
1090178354Ssam		 */
1091178354Ssam		/* XXX honor batimeout? */
1092178354Ssam		if (ticks - rap->rxa_age > ieee80211_ampdu_age) {
1093178354Ssam			/*
1094178354Ssam			 * Too long since we received the first
1095178354Ssam			 * frame; flush the reorder buffer.
1096178354Ssam			 */
1097178354Ssam			vap->iv_stats.is_ampdu_rx_age += rap->rxa_qframes;
1098178354Ssam			ampdu_rx_flush(ni, rap);
1099178354Ssam		}
1100178354Ssam	}
1101178354Ssam#endif /* IEEE80211_AMPDU_AGE */
1102178354Ssam}
1103178354Ssam
1104173273Ssamstatic struct ieee80211_channel *
1105173273Ssamfindhtchan(struct ieee80211com *ic, struct ieee80211_channel *c, int htflags)
1106173273Ssam{
1107173273Ssam	return ieee80211_find_channel(ic, c->ic_freq,
1108173273Ssam	    (c->ic_flags &~ IEEE80211_CHAN_HT) | htflags);
1109173273Ssam}
1110173273Ssam
1111173273Ssam/*
1112173273Ssam * Adjust a channel to be HT/non-HT according to the vap's configuration.
1113173273Ssam */
1114173273Ssamstruct ieee80211_channel *
1115173273Ssamieee80211_ht_adjust_channel(struct ieee80211com *ic,
1116173273Ssam	struct ieee80211_channel *chan, int flags)
1117173273Ssam{
1118173273Ssam	struct ieee80211_channel *c;
1119173273Ssam
1120193655Ssam	if (flags & IEEE80211_FHT_HT) {
1121173273Ssam		/* promote to HT if possible */
1122193655Ssam		if (flags & IEEE80211_FHT_USEHT40) {
1123173273Ssam			if (!IEEE80211_IS_CHAN_HT40(chan)) {
1124173273Ssam				/* NB: arbitrarily pick ht40+ over ht40- */
1125173273Ssam				c = findhtchan(ic, chan, IEEE80211_CHAN_HT40U);
1126173273Ssam				if (c == NULL)
1127173273Ssam					c = findhtchan(ic, chan,
1128173273Ssam						IEEE80211_CHAN_HT40D);
1129173273Ssam				if (c == NULL)
1130173273Ssam					c = findhtchan(ic, chan,
1131173273Ssam						IEEE80211_CHAN_HT20);
1132173273Ssam				if (c != NULL)
1133173273Ssam					chan = c;
1134173273Ssam			}
1135173273Ssam		} else if (!IEEE80211_IS_CHAN_HT20(chan)) {
1136173273Ssam			c = findhtchan(ic, chan, IEEE80211_CHAN_HT20);
1137173273Ssam			if (c != NULL)
1138173273Ssam				chan = c;
1139173273Ssam		}
1140173273Ssam	} else if (IEEE80211_IS_CHAN_HT(chan)) {
1141173273Ssam		/* demote to legacy, HT use is disabled */
1142173273Ssam		c = ieee80211_find_channel(ic, chan->ic_freq,
1143173273Ssam		    chan->ic_flags &~ IEEE80211_CHAN_HT);
1144173273Ssam		if (c != NULL)
1145173273Ssam			chan = c;
1146173273Ssam	}
1147173273Ssam	return chan;
1148173273Ssam}
1149173273Ssam
1150173273Ssam/*
1151173273Ssam * Setup HT-specific state for a legacy WDS peer.
1152173273Ssam */
1153173273Ssamvoid
1154173273Ssamieee80211_ht_wds_init(struct ieee80211_node *ni)
1155173273Ssam{
1156178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1157173273Ssam	struct ieee80211_tx_ampdu *tap;
1158173273Ssam	int ac;
1159173273Ssam
1160193655Ssam	KASSERT(vap->iv_flags_ht & IEEE80211_FHT_HT, ("no HT requested"));
1161173273Ssam
1162173273Ssam	/* XXX check scan cache in case peer has an ap and we have info */
1163173273Ssam	/*
1164173273Ssam	 * If setup with a legacy channel; locate an HT channel.
1165173273Ssam	 * Otherwise if the inherited channel (from a companion
1166173273Ssam	 * AP) is suitable use it so we use the same location
1167173273Ssam	 * for the extension channel).
1168173273Ssam	 */
1169178354Ssam	ni->ni_chan = ieee80211_ht_adjust_channel(ni->ni_ic,
1170178354Ssam	    ni->ni_chan, ieee80211_htchanflags(ni->ni_chan));
1171173273Ssam
1172173273Ssam	ni->ni_htcap = 0;
1173193655Ssam	if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20)
1174173273Ssam		ni->ni_htcap |= IEEE80211_HTCAP_SHORTGI20;
1175173273Ssam	if (IEEE80211_IS_CHAN_HT40(ni->ni_chan)) {
1176173273Ssam		ni->ni_htcap |= IEEE80211_HTCAP_CHWIDTH40;
1177173273Ssam		ni->ni_chw = 40;
1178173273Ssam		if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan))
1179173273Ssam			ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_ABOVE;
1180173273Ssam		else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan))
1181173273Ssam			ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_BELOW;
1182193655Ssam		if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40)
1183173273Ssam			ni->ni_htcap |= IEEE80211_HTCAP_SHORTGI40;
1184173273Ssam	} else {
1185173273Ssam		ni->ni_chw = 20;
1186173273Ssam		ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_NONE;
1187173273Ssam	}
1188173273Ssam	ni->ni_htctlchan = ni->ni_chan->ic_ieee;
1189193655Ssam	if (vap->iv_flags_ht & IEEE80211_FHT_RIFS)
1190183256Ssam		ni->ni_flags |= IEEE80211_NODE_RIFS;
1191183256Ssam	/* XXX does it make sense to enable SMPS? */
1192173273Ssam
1193173273Ssam	ni->ni_htopmode = 0;		/* XXX need protection state */
1194173273Ssam	ni->ni_htstbc = 0;		/* XXX need info */
1195173273Ssam
1196173273Ssam	for (ac = 0; ac < WME_NUM_AC; ac++) {
1197173273Ssam		tap = &ni->ni_tx_ampdu[ac];
1198173273Ssam		tap->txa_ac = ac;
1199173273Ssam	}
1200193655Ssam	/* NB: AMPDU tx/rx governed by IEEE80211_FHT_AMPDU_{TX,RX} */
1201173273Ssam	ni->ni_flags |= IEEE80211_NODE_HT | IEEE80211_NODE_AMPDU;
1202173273Ssam}
1203173273Ssam
1204173273Ssam/*
1205173273Ssam * Notify hostap vaps of a change in the HTINFO ie.
1206173273Ssam */
1207173273Ssamstatic void
1208173273Ssamhtinfo_notify(struct ieee80211com *ic)
1209173273Ssam{
1210178354Ssam	struct ieee80211vap *vap;
1211178354Ssam	int first = 1;
1212178354Ssam
1213178354Ssam	IEEE80211_LOCK_ASSERT(ic);
1214178354Ssam
1215178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1216178354Ssam		if (vap->iv_opmode != IEEE80211_M_HOSTAP)
1217178354Ssam			continue;
1218183253Ssam		if (vap->iv_state != IEEE80211_S_RUN ||
1219183253Ssam		    !IEEE80211_IS_CHAN_HT(vap->iv_bss->ni_chan))
1220183253Ssam			continue;
1221178354Ssam		if (first) {
1222178354Ssam			IEEE80211_NOTE(vap,
1223178354Ssam			    IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N,
1224178354Ssam			    vap->iv_bss,
1225178354Ssam			    "HT bss occupancy change: %d sta, %d ht, "
1226178354Ssam			    "%d ht40%s, HT protmode now 0x%x"
1227178354Ssam			    , ic->ic_sta_assoc
1228178354Ssam			    , ic->ic_ht_sta_assoc
1229178354Ssam			    , ic->ic_ht40_sta_assoc
1230193655Ssam			    , (ic->ic_flags_ht & IEEE80211_FHT_NONHT_PR) ?
1231178354Ssam				 ", non-HT sta present" : ""
1232178354Ssam			    , ic->ic_curhtprotmode);
1233178354Ssam			first = 0;
1234178354Ssam		}
1235178354Ssam		ieee80211_beacon_notify(vap, IEEE80211_BEACON_HTINFO);
1236178354Ssam	}
1237173273Ssam}
1238173273Ssam
1239173273Ssam/*
1240173273Ssam * Calculate HT protection mode from current
1241173273Ssam * state and handle updates.
1242173273Ssam */
1243173273Ssamstatic void
1244173273Ssamhtinfo_update(struct ieee80211com *ic)
1245173273Ssam{
1246173273Ssam	uint8_t protmode;
1247173273Ssam
1248178354Ssam	if (ic->ic_sta_assoc != ic->ic_ht_sta_assoc) {
1249178354Ssam		protmode = IEEE80211_HTINFO_OPMODE_MIXED
1250178354Ssam			 | IEEE80211_HTINFO_NONHT_PRESENT;
1251193655Ssam	} else if (ic->ic_flags_ht & IEEE80211_FHT_NONHT_PR) {
1252173273Ssam		protmode = IEEE80211_HTINFO_OPMODE_PROTOPT
1253178354Ssam			 | IEEE80211_HTINFO_NONHT_PRESENT;
1254178354Ssam	} else if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
1255178354Ssam	    IEEE80211_IS_CHAN_HT40(ic->ic_bsschan) &&
1256173273Ssam	    ic->ic_sta_assoc != ic->ic_ht40_sta_assoc) {
1257173273Ssam		protmode = IEEE80211_HTINFO_OPMODE_HT20PR;
1258173273Ssam	} else {
1259173273Ssam		protmode = IEEE80211_HTINFO_OPMODE_PURE;
1260173273Ssam	}
1261173273Ssam	if (protmode != ic->ic_curhtprotmode) {
1262173273Ssam		ic->ic_curhtprotmode = protmode;
1263173273Ssam		htinfo_notify(ic);
1264173273Ssam	}
1265173273Ssam}
1266173273Ssam
1267173273Ssam/*
1268173273Ssam * Handle an HT station joining a BSS.
1269173273Ssam */
1270173273Ssamvoid
1271173273Ssamieee80211_ht_node_join(struct ieee80211_node *ni)
1272173273Ssam{
1273173273Ssam	struct ieee80211com *ic = ni->ni_ic;
1274173273Ssam
1275173273Ssam	IEEE80211_LOCK_ASSERT(ic);
1276173273Ssam
1277173273Ssam	if (ni->ni_flags & IEEE80211_NODE_HT) {
1278173273Ssam		ic->ic_ht_sta_assoc++;
1279173273Ssam		if (ni->ni_chw == 40)
1280173273Ssam			ic->ic_ht40_sta_assoc++;
1281173273Ssam	}
1282173273Ssam	htinfo_update(ic);
1283173273Ssam}
1284173273Ssam
1285173273Ssam/*
1286173273Ssam * Handle an HT station leaving a BSS.
1287173273Ssam */
1288173273Ssamvoid
1289173273Ssamieee80211_ht_node_leave(struct ieee80211_node *ni)
1290173273Ssam{
1291173273Ssam	struct ieee80211com *ic = ni->ni_ic;
1292173273Ssam
1293173273Ssam	IEEE80211_LOCK_ASSERT(ic);
1294173273Ssam
1295173273Ssam	if (ni->ni_flags & IEEE80211_NODE_HT) {
1296173273Ssam		ic->ic_ht_sta_assoc--;
1297173273Ssam		if (ni->ni_chw == 40)
1298173273Ssam			ic->ic_ht40_sta_assoc--;
1299173273Ssam	}
1300173273Ssam	htinfo_update(ic);
1301173273Ssam}
1302173273Ssam
1303173273Ssam/*
1304173273Ssam * Public version of htinfo_update; used for processing
1305178354Ssam * beacon frames from overlapping bss.
1306178354Ssam *
1307178354Ssam * Caller can specify either IEEE80211_HTINFO_OPMODE_MIXED
1308178354Ssam * (on receipt of a beacon that advertises MIXED) or
1309178354Ssam * IEEE80211_HTINFO_OPMODE_PROTOPT (on receipt of a beacon
1310178354Ssam * from an overlapping legacy bss).  We treat MIXED with
1311178354Ssam * a higher precedence than PROTOPT (i.e. we will not change
1312178354Ssam * change PROTOPT -> MIXED; only MIXED -> PROTOPT).  This
1313178354Ssam * corresponds to how we handle things in htinfo_update.
1314173273Ssam */
1315173273Ssamvoid
1316178354Ssamieee80211_htprot_update(struct ieee80211com *ic, int protmode)
1317173273Ssam{
1318178354Ssam#define	OPMODE(x)	SM(x, IEEE80211_HTINFO_OPMODE)
1319183246Ssam	IEEE80211_LOCK(ic);
1320178354Ssam
1321178354Ssam	/* track non-HT station presence */
1322178354Ssam	KASSERT(protmode & IEEE80211_HTINFO_NONHT_PRESENT,
1323183246Ssam	    ("protmode 0x%x", protmode));
1324193655Ssam	ic->ic_flags_ht |= IEEE80211_FHT_NONHT_PR;
1325178354Ssam	ic->ic_lastnonht = ticks;
1326178354Ssam
1327183246Ssam	if (protmode != ic->ic_curhtprotmode &&
1328183246Ssam	    (OPMODE(ic->ic_curhtprotmode) != IEEE80211_HTINFO_OPMODE_MIXED ||
1329183246Ssam	     OPMODE(protmode) == IEEE80211_HTINFO_OPMODE_PROTOPT)) {
1330183246Ssam		/* push beacon update */
1331183246Ssam		ic->ic_curhtprotmode = protmode;
1332183246Ssam		htinfo_notify(ic);
1333183246Ssam	}
1334181197Ssam	IEEE80211_UNLOCK(ic);
1335178354Ssam#undef OPMODE
1336173273Ssam}
1337173273Ssam
1338173273Ssam/*
1339173273Ssam * Time out presence of an overlapping bss with non-HT
1340173273Ssam * stations.  When operating in hostap mode we listen for
1341173273Ssam * beacons from other stations and if we identify a non-HT
1342173273Ssam * station is present we update the opmode field of the
1343173273Ssam * HTINFO ie.  To identify when all non-HT stations are
1344173273Ssam * gone we time out this condition.
1345173273Ssam */
1346173273Ssamvoid
1347173273Ssamieee80211_ht_timeout(struct ieee80211com *ic)
1348173273Ssam{
1349173273Ssam	IEEE80211_LOCK_ASSERT(ic);
1350173273Ssam
1351193655Ssam	if ((ic->ic_flags_ht & IEEE80211_FHT_NONHT_PR) &&
1352173273Ssam	    time_after(ticks, ic->ic_lastnonht + IEEE80211_NONHT_PRESENT_AGE)) {
1353173273Ssam#if 0
1354178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
1355173273Ssam		    "%s", "time out non-HT STA present on channel");
1356173273Ssam#endif
1357193655Ssam		ic->ic_flags_ht &= ~IEEE80211_FHT_NONHT_PR;
1358173273Ssam		htinfo_update(ic);
1359173273Ssam	}
1360173273Ssam}
1361173273Ssam
1362170530Ssam/* unalligned little endian access */
1363170530Ssam#define LE_READ_2(p)					\
1364170530Ssam	((uint16_t)					\
1365170530Ssam	 ((((const uint8_t *)(p))[0]      ) |		\
1366170530Ssam	  (((const uint8_t *)(p))[1] <<  8)))
1367170530Ssam
1368170530Ssam/*
1369170530Ssam * Process an 802.11n HT capabilities ie.
1370170530Ssam */
1371170530Ssamvoid
1372170530Ssamieee80211_parse_htcap(struct ieee80211_node *ni, const uint8_t *ie)
1373170530Ssam{
1374170530Ssam	if (ie[0] == IEEE80211_ELEMID_VENDOR) {
1375170530Ssam		/*
1376170530Ssam		 * Station used Vendor OUI ie to associate;
1377170530Ssam		 * mark the node so when we respond we'll use
1378170530Ssam		 * the Vendor OUI's and not the standard ie's.
1379170530Ssam		 */
1380170530Ssam		ni->ni_flags |= IEEE80211_NODE_HTCOMPAT;
1381170530Ssam		ie += 4;
1382170530Ssam	} else
1383170530Ssam		ni->ni_flags &= ~IEEE80211_NODE_HTCOMPAT;
1384170530Ssam
1385170530Ssam	ni->ni_htcap = LE_READ_2(ie +
1386170530Ssam		__offsetof(struct ieee80211_ie_htcap, hc_cap));
1387170530Ssam	ni->ni_htparam = ie[__offsetof(struct ieee80211_ie_htcap, hc_param)];
1388170530Ssam}
1389170530Ssam
1390183254Ssamstatic void
1391183254Ssamhtinfo_parse(struct ieee80211_node *ni,
1392183254Ssam	const struct ieee80211_ie_htinfo *htinfo)
1393170530Ssam{
1394170530Ssam	uint16_t w;
1395170530Ssam
1396170530Ssam	ni->ni_htctlchan = htinfo->hi_ctrlchannel;
1397170530Ssam	ni->ni_ht2ndchan = SM(htinfo->hi_byte1, IEEE80211_HTINFO_2NDCHAN);
1398172055Ssam	w = LE_READ_2(&htinfo->hi_byte2);
1399170530Ssam	ni->ni_htopmode = SM(w, IEEE80211_HTINFO_OPMODE);
1400170530Ssam	w = LE_READ_2(&htinfo->hi_byte45);
1401170530Ssam	ni->ni_htstbc = SM(w, IEEE80211_HTINFO_BASIC_STBCMCS);
1402183254Ssam}
1403183254Ssam
1404183254Ssam/*
1405183254Ssam * Parse an 802.11n HT info ie and save useful information
1406183254Ssam * to the node state.  Note this does not effect any state
1407183254Ssam * changes such as for channel width change.
1408183254Ssam */
1409183254Ssamvoid
1410183254Ssamieee80211_parse_htinfo(struct ieee80211_node *ni, const uint8_t *ie)
1411183254Ssam{
1412183254Ssam	if (ie[0] == IEEE80211_ELEMID_VENDOR)
1413183254Ssam		ie += 4;
1414183254Ssam	htinfo_parse(ni, (const struct ieee80211_ie_htinfo *) ie);
1415183254Ssam}
1416183254Ssam
1417183254Ssam/*
1418183254Ssam * Handle 11n channel switch.  Use the received HT ie's to
1419183254Ssam * identify the right channel to use.  If we cannot locate it
1420183254Ssam * in the channel table then fallback to legacy operation.
1421183254Ssam * Note that we use this information to identify the node's
1422183254Ssam * channel only; the caller is responsible for insuring any
1423183254Ssam * required channel change is done (e.g. in sta mode when
1424183254Ssam * parsing the contents of a beacon frame).
1425183254Ssam */
1426183254Ssamstatic void
1427183254Ssamhtinfo_update_chw(struct ieee80211_node *ni, int htflags)
1428183254Ssam{
1429183254Ssam	struct ieee80211com *ic = ni->ni_ic;
1430183254Ssam	struct ieee80211_channel *c;
1431183254Ssam	int chanflags;
1432183254Ssam
1433173273Ssam	chanflags = (ni->ni_chan->ic_flags &~ IEEE80211_CHAN_HT) | htflags;
1434173273Ssam	if (chanflags != ni->ni_chan->ic_flags) {
1435183254Ssam		/* XXX not right for ht40- */
1436173273Ssam		c = ieee80211_find_channel(ic, ni->ni_chan->ic_freq, chanflags);
1437178354Ssam		if (c == NULL && (htflags & IEEE80211_CHAN_HT40)) {
1438173273Ssam			/*
1439173273Ssam			 * No HT40 channel entry in our table; fall back
1440173273Ssam			 * to HT20 operation.  This should not happen.
1441173273Ssam			 */
1442173273Ssam			c = findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT20);
1443183254Ssam#if 0
1444183254Ssam			IEEE80211_NOTE(ni->ni_vap,
1445173273Ssam			    IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
1446173273Ssam			    "no HT40 channel (freq %u), falling back to HT20",
1447173273Ssam			    ni->ni_chan->ic_freq);
1448183254Ssam#endif
1449173273Ssam			/* XXX stat */
1450173273Ssam		}
1451173273Ssam		if (c != NULL && c != ni->ni_chan) {
1452183254Ssam			IEEE80211_NOTE(ni->ni_vap,
1453173273Ssam			    IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
1454173273Ssam			    "switch station to HT%d channel %u/0x%x",
1455173273Ssam			    IEEE80211_IS_CHAN_HT40(c) ? 40 : 20,
1456173273Ssam			    c->ic_freq, c->ic_flags);
1457173273Ssam			ni->ni_chan = c;
1458173273Ssam		}
1459173273Ssam		/* NB: caller responsible for forcing any channel change */
1460173273Ssam	}
1461173273Ssam	/* update node's tx channel width */
1462173273Ssam	ni->ni_chw = IEEE80211_IS_CHAN_HT40(ni->ni_chan)? 40 : 20;
1463170530Ssam}
1464170530Ssam
1465170530Ssam/*
1466183255Ssam * Update 11n MIMO PS state according to received htcap.
1467183255Ssam */
1468183255Ssamstatic __inline int
1469183255Ssamhtcap_update_mimo_ps(struct ieee80211_node *ni)
1470183255Ssam{
1471183255Ssam	uint16_t oflags = ni->ni_flags;
1472183255Ssam
1473183255Ssam	switch (ni->ni_htcap & IEEE80211_HTCAP_SMPS) {
1474183255Ssam	case IEEE80211_HTCAP_SMPS_DYNAMIC:
1475183255Ssam		ni->ni_flags |= IEEE80211_NODE_MIMO_PS;
1476183255Ssam		ni->ni_flags |= IEEE80211_NODE_MIMO_RTS;
1477183255Ssam		break;
1478183255Ssam	case IEEE80211_HTCAP_SMPS_ENA:
1479183255Ssam		ni->ni_flags |= IEEE80211_NODE_MIMO_PS;
1480183255Ssam		ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS;
1481183255Ssam		break;
1482183255Ssam	case IEEE80211_HTCAP_SMPS_OFF:
1483183255Ssam	default:		/* disable on rx of reserved value */
1484183255Ssam		ni->ni_flags &= ~IEEE80211_NODE_MIMO_PS;
1485183255Ssam		ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS;
1486183255Ssam		break;
1487183255Ssam	}
1488183255Ssam	return (oflags ^ ni->ni_flags);
1489183255Ssam}
1490183255Ssam
1491183255Ssam/*
1492183257Ssam * Update short GI state according to received htcap
1493183257Ssam * and local settings.
1494183257Ssam */
1495183257Ssamstatic __inline void
1496183257Ssamhtcap_update_shortgi(struct ieee80211_node *ni)
1497183257Ssam{
1498183257Ssam	struct ieee80211vap *vap = ni->ni_vap;
1499183257Ssam
1500183257Ssam	ni->ni_flags &= ~(IEEE80211_NODE_SGI20|IEEE80211_NODE_SGI40);
1501183257Ssam	if ((ni->ni_htcap & IEEE80211_HTCAP_SHORTGI20) &&
1502193655Ssam	    (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20))
1503183257Ssam		ni->ni_flags |= IEEE80211_NODE_SGI20;
1504183257Ssam	if ((ni->ni_htcap & IEEE80211_HTCAP_SHORTGI40) &&
1505193655Ssam	    (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40))
1506183257Ssam		ni->ni_flags |= IEEE80211_NODE_SGI40;
1507183257Ssam}
1508183257Ssam
1509183257Ssam/*
1510183254Ssam * Parse and update HT-related state extracted from
1511183254Ssam * the HT cap and info ie's.
1512183254Ssam */
1513183254Ssamvoid
1514183254Ssamieee80211_ht_updateparams(struct ieee80211_node *ni,
1515183254Ssam	const uint8_t *htcapie, const uint8_t *htinfoie)
1516183254Ssam{
1517183254Ssam	struct ieee80211vap *vap = ni->ni_vap;
1518183254Ssam	const struct ieee80211_ie_htinfo *htinfo;
1519183254Ssam	int htflags;
1520183254Ssam
1521183254Ssam	ieee80211_parse_htcap(ni, htcapie);
1522183255Ssam	if (vap->iv_htcaps & IEEE80211_HTCAP_SMPS)
1523183255Ssam		htcap_update_mimo_ps(ni);
1524183257Ssam	htcap_update_shortgi(ni);
1525183254Ssam
1526183254Ssam	if (htinfoie[0] == IEEE80211_ELEMID_VENDOR)
1527183254Ssam		htinfoie += 4;
1528183254Ssam	htinfo = (const struct ieee80211_ie_htinfo *) htinfoie;
1529183254Ssam	htinfo_parse(ni, htinfo);
1530183254Ssam
1531193655Ssam	htflags = (vap->iv_flags_ht & IEEE80211_FHT_HT) ?
1532183254Ssam	    IEEE80211_CHAN_HT20 : 0;
1533183254Ssam	/* NB: honor operating mode constraint */
1534183254Ssam	if ((htinfo->hi_byte1 & IEEE80211_HTINFO_TXWIDTH_2040) &&
1535193655Ssam	    (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)) {
1536183254Ssam		if (ni->ni_ht2ndchan == IEEE80211_HTINFO_2NDCHAN_ABOVE)
1537183254Ssam			htflags = IEEE80211_CHAN_HT40U;
1538183254Ssam		else if (ni->ni_ht2ndchan == IEEE80211_HTINFO_2NDCHAN_BELOW)
1539183254Ssam			htflags = IEEE80211_CHAN_HT40D;
1540183254Ssam	}
1541183254Ssam	htinfo_update_chw(ni, htflags);
1542183256Ssam
1543183256Ssam	if ((htinfo->hi_byte1 & IEEE80211_HTINFO_RIFSMODE_PERM) &&
1544193655Ssam	    (vap->iv_flags_ht & IEEE80211_FHT_RIFS))
1545183256Ssam		ni->ni_flags |= IEEE80211_NODE_RIFS;
1546183256Ssam	else
1547183256Ssam		ni->ni_flags &= ~IEEE80211_NODE_RIFS;
1548183254Ssam}
1549183254Ssam
1550183254Ssam/*
1551183254Ssam * Parse and update HT-related state extracted from the HT cap ie
1552183254Ssam * for a station joining an HT BSS.
1553183254Ssam */
1554183254Ssamvoid
1555183254Ssamieee80211_ht_updatehtcap(struct ieee80211_node *ni, const uint8_t *htcapie)
1556183254Ssam{
1557183254Ssam	struct ieee80211vap *vap = ni->ni_vap;
1558183254Ssam	int htflags;
1559183254Ssam
1560183254Ssam	ieee80211_parse_htcap(ni, htcapie);
1561183255Ssam	if (vap->iv_htcaps & IEEE80211_HTCAP_SMPS)
1562183255Ssam		htcap_update_mimo_ps(ni);
1563183257Ssam	htcap_update_shortgi(ni);
1564183254Ssam
1565183254Ssam	/* NB: honor operating mode constraint */
1566183254Ssam	/* XXX 40 MHZ intolerant */
1567193655Ssam	htflags = (vap->iv_flags_ht & IEEE80211_FHT_HT) ?
1568183254Ssam	    IEEE80211_CHAN_HT20 : 0;
1569183254Ssam	if ((ni->ni_htcap & IEEE80211_HTCAP_CHWIDTH40) &&
1570193655Ssam	    (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)) {
1571183254Ssam		if (IEEE80211_IS_CHAN_HT40U(vap->iv_bss->ni_chan))
1572183254Ssam			htflags = IEEE80211_CHAN_HT40U;
1573183254Ssam		else if (IEEE80211_IS_CHAN_HT40D(vap->iv_bss->ni_chan))
1574183254Ssam			htflags = IEEE80211_CHAN_HT40D;
1575183254Ssam	}
1576183254Ssam	htinfo_update_chw(ni, htflags);
1577183254Ssam}
1578183254Ssam
1579183254Ssam/*
1580170530Ssam * Install received HT rate set by parsing the HT cap ie.
1581170530Ssam */
1582170530Ssamint
1583170530Ssamieee80211_setup_htrates(struct ieee80211_node *ni, const uint8_t *ie, int flags)
1584170530Ssam{
1585219602Sbschmidt	struct ieee80211com *ic = ni->ni_ic;
1586178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1587170530Ssam	const struct ieee80211_ie_htcap *htcap;
1588170530Ssam	struct ieee80211_htrateset *rs;
1589219602Sbschmidt	int i, maxequalmcs, maxunequalmcs;
1590170530Ssam
1591219602Sbschmidt	maxequalmcs = ic->ic_txstream * 8 - 1;
1592219602Sbschmidt	if (ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL) {
1593219602Sbschmidt		if (ic->ic_txstream >= 2)
1594219602Sbschmidt			maxunequalmcs = 38;
1595219602Sbschmidt		if (ic->ic_txstream >= 3)
1596219602Sbschmidt			maxunequalmcs = 52;
1597219602Sbschmidt		if (ic->ic_txstream >= 4)
1598219602Sbschmidt			maxunequalmcs = 76;
1599219602Sbschmidt	} else
1600219602Sbschmidt		maxunequalmcs = 0;
1601219602Sbschmidt
1602170530Ssam	rs = &ni->ni_htrates;
1603170530Ssam	memset(rs, 0, sizeof(*rs));
1604170530Ssam	if (ie != NULL) {
1605170530Ssam		if (ie[0] == IEEE80211_ELEMID_VENDOR)
1606170530Ssam			ie += 4;
1607170530Ssam		htcap = (const struct ieee80211_ie_htcap *) ie;
1608170530Ssam		for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++) {
1609170530Ssam			if (isclr(htcap->hc_mcsset, i))
1610170530Ssam				continue;
1611170530Ssam			if (rs->rs_nrates == IEEE80211_HTRATE_MAXSIZE) {
1612178354Ssam				IEEE80211_NOTE(vap,
1613170530Ssam				    IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
1614170530Ssam				    "WARNING, HT rate set too large; only "
1615170530Ssam				    "using %u rates", IEEE80211_HTRATE_MAXSIZE);
1616178354Ssam				vap->iv_stats.is_rx_rstoobig++;
1617170530Ssam				break;
1618170530Ssam			}
1619219602Sbschmidt			if (i <= 31 && i > maxequalmcs)
1620219602Sbschmidt				continue;
1621219602Sbschmidt			if (i == 32 &&
1622219602Sbschmidt			    (ic->ic_htcaps & IEEE80211_HTC_TXMCS32) == 0)
1623219602Sbschmidt				continue;
1624219602Sbschmidt			if (i > 32 && i > maxunequalmcs)
1625219602Sbschmidt				continue;
1626170530Ssam			rs->rs_rates[rs->rs_nrates++] = i;
1627170530Ssam		}
1628170530Ssam	}
1629170530Ssam	return ieee80211_fix_rate(ni, (struct ieee80211_rateset *) rs, flags);
1630170530Ssam}
1631170530Ssam
1632170530Ssam/*
1633170530Ssam * Mark rates in a node's HT rate set as basic according
1634170530Ssam * to the information in the supplied HT info ie.
1635170530Ssam */
1636170530Ssamvoid
1637170530Ssamieee80211_setup_basic_htrates(struct ieee80211_node *ni, const uint8_t *ie)
1638170530Ssam{
1639170530Ssam	const struct ieee80211_ie_htinfo *htinfo;
1640170530Ssam	struct ieee80211_htrateset *rs;
1641170530Ssam	int i, j;
1642170530Ssam
1643170530Ssam	if (ie[0] == IEEE80211_ELEMID_VENDOR)
1644170530Ssam		ie += 4;
1645170530Ssam	htinfo = (const struct ieee80211_ie_htinfo *) ie;
1646170530Ssam	rs = &ni->ni_htrates;
1647170530Ssam	if (rs->rs_nrates == 0) {
1648178354Ssam		IEEE80211_NOTE(ni->ni_vap,
1649170530Ssam		    IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
1650170530Ssam		    "%s", "WARNING, empty HT rate set");
1651170530Ssam		return;
1652170530Ssam	}
1653170530Ssam	for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++) {
1654170530Ssam		if (isclr(htinfo->hi_basicmcsset, i))
1655170530Ssam			continue;
1656170530Ssam		for (j = 0; j < rs->rs_nrates; j++)
1657170530Ssam			if ((rs->rs_rates[j] & IEEE80211_RATE_VAL) == i)
1658170530Ssam				rs->rs_rates[j] |= IEEE80211_RATE_BASIC;
1659170530Ssam	}
1660170530Ssam}
1661170530Ssam
1662170530Ssamstatic void
1663184280Ssamampdu_tx_setup(struct ieee80211_tx_ampdu *tap)
1664184280Ssam{
1665184280Ssam	callout_init(&tap->txa_timer, CALLOUT_MPSAFE);
1666184280Ssam	tap->txa_flags |= IEEE80211_AGGR_SETUP;
1667184280Ssam}
1668184280Ssam
1669184280Ssamstatic void
1670184280Ssamampdu_tx_stop(struct ieee80211_tx_ampdu *tap)
1671184280Ssam{
1672184280Ssam	struct ieee80211_node *ni = tap->txa_ni;
1673184280Ssam	struct ieee80211com *ic = ni->ni_ic;
1674184280Ssam
1675184280Ssam	KASSERT(tap->txa_flags & IEEE80211_AGGR_SETUP,
1676184280Ssam	    ("txa_flags 0x%x ac %d", tap->txa_flags, tap->txa_ac));
1677184280Ssam
1678184280Ssam	/*
1679184280Ssam	 * Stop BA stream if setup so driver has a chance
1680184280Ssam	 * to reclaim any resources it might have allocated.
1681184280Ssam	 */
1682184280Ssam	ic->ic_addba_stop(ni, tap);
1683184280Ssam	/*
1684184280Ssam	 * Stop any pending BAR transmit.
1685184280Ssam	 */
1686184280Ssam	bar_stop_timer(tap);
1687184280Ssam
1688184280Ssam	tap->txa_lastsample = 0;
1689184280Ssam	tap->txa_avgpps = 0;
1690184280Ssam	/* NB: clearing NAK means we may re-send ADDBA */
1691184280Ssam	tap->txa_flags &= ~(IEEE80211_AGGR_SETUP | IEEE80211_AGGR_NAK);
1692184280Ssam}
1693184280Ssam
1694184280Ssamstatic void
1695170530Ssamaddba_timeout(void *arg)
1696170530Ssam{
1697170530Ssam	struct ieee80211_tx_ampdu *tap = arg;
1698170530Ssam
1699170530Ssam	/* XXX ? */
1700170530Ssam	tap->txa_flags &= ~IEEE80211_AGGR_XCHGPEND;
1701170530Ssam	tap->txa_attempts++;
1702170530Ssam}
1703170530Ssam
1704170530Ssamstatic void
1705170530Ssamaddba_start_timeout(struct ieee80211_tx_ampdu *tap)
1706170530Ssam{
1707170530Ssam	/* XXX use CALLOUT_PENDING instead? */
1708178354Ssam	callout_reset(&tap->txa_timer, ieee80211_addba_timeout,
1709170530Ssam	    addba_timeout, tap);
1710170530Ssam	tap->txa_flags |= IEEE80211_AGGR_XCHGPEND;
1711178354Ssam	tap->txa_nextrequest = ticks + ieee80211_addba_timeout;
1712170530Ssam}
1713170530Ssam
1714170530Ssamstatic void
1715170530Ssamaddba_stop_timeout(struct ieee80211_tx_ampdu *tap)
1716170530Ssam{
1717170530Ssam	/* XXX use CALLOUT_PENDING instead? */
1718170530Ssam	if (tap->txa_flags & IEEE80211_AGGR_XCHGPEND) {
1719170530Ssam		callout_stop(&tap->txa_timer);
1720170530Ssam		tap->txa_flags &= ~IEEE80211_AGGR_XCHGPEND;
1721170530Ssam	}
1722170530Ssam}
1723170530Ssam
1724170530Ssam/*
1725170530Ssam * Default method for requesting A-MPDU tx aggregation.
1726170530Ssam * We setup the specified state block and start a timer
1727170530Ssam * to wait for an ADDBA response frame.
1728170530Ssam */
1729170530Ssamstatic int
1730170530Ssamieee80211_addba_request(struct ieee80211_node *ni,
1731170530Ssam	struct ieee80211_tx_ampdu *tap,
1732170530Ssam	int dialogtoken, int baparamset, int batimeout)
1733170530Ssam{
1734170530Ssam	int bufsiz;
1735170530Ssam
1736170530Ssam	/* XXX locking */
1737170530Ssam	tap->txa_token = dialogtoken;
1738170530Ssam	tap->txa_flags |= IEEE80211_AGGR_IMMEDIATE;
1739170530Ssam	bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
1740170530Ssam	tap->txa_wnd = (bufsiz == 0) ?
1741170530Ssam	    IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
1742170530Ssam	addba_start_timeout(tap);
1743170530Ssam	return 1;
1744170530Ssam}
1745170530Ssam
1746170530Ssam/*
1747170530Ssam * Default method for processing an A-MPDU tx aggregation
1748170530Ssam * response.  We shutdown any pending timer and update the
1749170530Ssam * state block according to the reply.
1750170530Ssam */
1751170530Ssamstatic int
1752170530Ssamieee80211_addba_response(struct ieee80211_node *ni,
1753170530Ssam	struct ieee80211_tx_ampdu *tap,
1754170530Ssam	int status, int baparamset, int batimeout)
1755170530Ssam{
1756184280Ssam	int bufsiz, tid;
1757170530Ssam
1758170530Ssam	/* XXX locking */
1759170530Ssam	addba_stop_timeout(tap);
1760170530Ssam	if (status == IEEE80211_STATUS_SUCCESS) {
1761170530Ssam		bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
1762170530Ssam		/* XXX override our request? */
1763170530Ssam		tap->txa_wnd = (bufsiz == 0) ?
1764170530Ssam		    IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
1765184280Ssam		/* XXX AC/TID */
1766184280Ssam		tid = MS(baparamset, IEEE80211_BAPS_TID);
1767170530Ssam		tap->txa_flags |= IEEE80211_AGGR_RUNNING;
1768184280Ssam		tap->txa_attempts = 0;
1769173273Ssam	} else {
1770173273Ssam		/* mark tid so we don't try again */
1771173273Ssam		tap->txa_flags |= IEEE80211_AGGR_NAK;
1772170530Ssam	}
1773170530Ssam	return 1;
1774170530Ssam}
1775170530Ssam
1776170530Ssam/*
1777170530Ssam * Default method for stopping A-MPDU tx aggregation.
1778170530Ssam * Any timer is cleared and we drain any pending frames.
1779170530Ssam */
1780170530Ssamstatic void
1781170530Ssamieee80211_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
1782170530Ssam{
1783170530Ssam	/* XXX locking */
1784170530Ssam	addba_stop_timeout(tap);
1785170530Ssam	if (tap->txa_flags & IEEE80211_AGGR_RUNNING) {
1786182830Ssam		/* XXX clear aggregation queue */
1787170530Ssam		tap->txa_flags &= ~IEEE80211_AGGR_RUNNING;
1788170530Ssam	}
1789170530Ssam	tap->txa_attempts = 0;
1790170530Ssam}
1791170530Ssam
1792170530Ssam/*
1793170530Ssam * Process a received action frame using the default aggregation
1794170530Ssam * policy.  We intercept ADDBA-related frames and use them to
1795170530Ssam * update our aggregation state.  All other frames are passed up
1796170530Ssam * for processing by ieee80211_recv_action.
1797170530Ssam */
1798195377Ssamstatic int
1799195377Ssamht_recv_action_ba_addba_request(struct ieee80211_node *ni,
1800195377Ssam	const struct ieee80211_frame *wh,
1801170530Ssam	const uint8_t *frm, const uint8_t *efrm)
1802170530Ssam{
1803170530Ssam	struct ieee80211com *ic = ni->ni_ic;
1804178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1805170530Ssam	struct ieee80211_rx_ampdu *rap;
1806195377Ssam	uint8_t dialogtoken;
1807195377Ssam	uint16_t baparamset, batimeout, baseqctl;
1808205277Srpaulo	uint16_t args[5];
1809195377Ssam	int tid;
1810170530Ssam
1811195377Ssam	dialogtoken = frm[2];
1812195377Ssam	baparamset = LE_READ_2(frm+3);
1813195377Ssam	batimeout = LE_READ_2(frm+5);
1814195377Ssam	baseqctl = LE_READ_2(frm+7);
1815170530Ssam
1816195377Ssam	tid = MS(baparamset, IEEE80211_BAPS_TID);
1817170530Ssam
1818195377Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1819195377Ssam	    "recv ADDBA request: dialogtoken %u baparamset 0x%x "
1820195377Ssam	    "(tid %d bufsiz %d) batimeout %d baseqctl %d:%d",
1821195377Ssam	    dialogtoken, baparamset,
1822195377Ssam	    tid, MS(baparamset, IEEE80211_BAPS_BUFSIZ),
1823195377Ssam	    batimeout,
1824195377Ssam	    MS(baseqctl, IEEE80211_BASEQ_START),
1825195377Ssam	    MS(baseqctl, IEEE80211_BASEQ_FRAG));
1826170530Ssam
1827195377Ssam	rap = &ni->ni_rx_ampdu[tid];
1828170530Ssam
1829195377Ssam	/* Send ADDBA response */
1830195377Ssam	args[0] = dialogtoken;
1831195377Ssam	/*
1832195377Ssam	 * NB: We ack only if the sta associated with HT and
1833195377Ssam	 * the ap is configured to do AMPDU rx (the latter
1834195377Ssam	 * violates the 11n spec and is mostly for testing).
1835195377Ssam	 */
1836195377Ssam	if ((ni->ni_flags & IEEE80211_NODE_AMPDU_RX) &&
1837195377Ssam	    (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_RX)) {
1838195377Ssam		/* XXX handle ampdu_rx_start failure */
1839195377Ssam		ic->ic_ampdu_rx_start(ni, rap,
1840195377Ssam		    baparamset, batimeout, baseqctl);
1841170530Ssam
1842195377Ssam		args[1] = IEEE80211_STATUS_SUCCESS;
1843195377Ssam	} else {
1844195377Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1845195377Ssam		    ni, "reject ADDBA request: %s",
1846195377Ssam		    ni->ni_flags & IEEE80211_NODE_AMPDU_RX ?
1847195377Ssam		       "administratively disabled" :
1848195377Ssam		       "not negotiated for station");
1849195377Ssam		vap->iv_stats.is_addba_reject++;
1850195377Ssam		args[1] = IEEE80211_STATUS_UNSPECIFIED;
1851195377Ssam	}
1852195377Ssam	/* XXX honor rap flags? */
1853195377Ssam	args[2] = IEEE80211_BAPS_POLICY_IMMEDIATE
1854195377Ssam		| SM(tid, IEEE80211_BAPS_TID)
1855195377Ssam		| SM(rap->rxa_wnd, IEEE80211_BAPS_BUFSIZ)
1856195377Ssam		;
1857195377Ssam	args[3] = 0;
1858205277Srpaulo	args[4] = 0;
1859195377Ssam	ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
1860195377Ssam		IEEE80211_ACTION_BA_ADDBA_RESPONSE, args);
1861195377Ssam	return 0;
1862195377Ssam}
1863170530Ssam
1864195377Ssamstatic int
1865195377Ssamht_recv_action_ba_addba_response(struct ieee80211_node *ni,
1866195377Ssam	const struct ieee80211_frame *wh,
1867195377Ssam	const uint8_t *frm, const uint8_t *efrm)
1868195377Ssam{
1869195377Ssam	struct ieee80211com *ic = ni->ni_ic;
1870195377Ssam	struct ieee80211vap *vap = ni->ni_vap;
1871195377Ssam	struct ieee80211_tx_ampdu *tap;
1872195377Ssam	uint8_t dialogtoken, policy;
1873195377Ssam	uint16_t baparamset, batimeout, code;
1874195377Ssam	int tid, ac, bufsiz;
1875170530Ssam
1876195377Ssam	dialogtoken = frm[2];
1877195377Ssam	code = LE_READ_2(frm+3);
1878195377Ssam	baparamset = LE_READ_2(frm+5);
1879195377Ssam	tid = MS(baparamset, IEEE80211_BAPS_TID);
1880195377Ssam	bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
1881195377Ssam	policy = MS(baparamset, IEEE80211_BAPS_POLICY);
1882195377Ssam	batimeout = LE_READ_2(frm+7);
1883195377Ssam
1884195377Ssam	ac = TID_TO_WME_AC(tid);
1885195377Ssam	tap = &ni->ni_tx_ampdu[ac];
1886195377Ssam	if ((tap->txa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
1887195377Ssam		IEEE80211_DISCARD_MAC(vap,
1888195377Ssam		    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1889195377Ssam		    ni->ni_macaddr, "ADDBA response",
1890195377Ssam		    "no pending ADDBA, tid %d dialogtoken %u "
1891195377Ssam		    "code %d", tid, dialogtoken, code);
1892195377Ssam		vap->iv_stats.is_addba_norequest++;
1893195377Ssam		return 0;
1894195377Ssam	}
1895195377Ssam	if (dialogtoken != tap->txa_token) {
1896195377Ssam		IEEE80211_DISCARD_MAC(vap,
1897195377Ssam		    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1898195377Ssam		    ni->ni_macaddr, "ADDBA response",
1899195377Ssam		    "dialogtoken mismatch: waiting for %d, "
1900195377Ssam		    "received %d, tid %d code %d",
1901195377Ssam		    tap->txa_token, dialogtoken, tid, code);
1902195377Ssam		vap->iv_stats.is_addba_badtoken++;
1903195377Ssam		return 0;
1904195377Ssam	}
1905195377Ssam	/* NB: assumes IEEE80211_AGGR_IMMEDIATE is 1 */
1906195377Ssam	if (policy != (tap->txa_flags & IEEE80211_AGGR_IMMEDIATE)) {
1907195377Ssam		IEEE80211_DISCARD_MAC(vap,
1908195377Ssam		    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1909195377Ssam		    ni->ni_macaddr, "ADDBA response",
1910195377Ssam		    "policy mismatch: expecting %s, "
1911195377Ssam		    "received %s, tid %d code %d",
1912195377Ssam		    tap->txa_flags & IEEE80211_AGGR_IMMEDIATE,
1913195377Ssam		    policy, tid, code);
1914195377Ssam		vap->iv_stats.is_addba_badpolicy++;
1915195377Ssam		return 0;
1916195377Ssam	}
1917182829Ssam#if 0
1918195377Ssam	/* XXX we take MIN in ieee80211_addba_response */
1919195377Ssam	if (bufsiz > IEEE80211_AGGR_BAWMAX) {
1920195377Ssam		IEEE80211_DISCARD_MAC(vap,
1921195377Ssam		    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1922195377Ssam		    ni->ni_macaddr, "ADDBA response",
1923195377Ssam		    "BA window too large: max %d, "
1924195377Ssam		    "received %d, tid %d code %d",
1925195377Ssam		    bufsiz, IEEE80211_AGGR_BAWMAX, tid, code);
1926195377Ssam		vap->iv_stats.is_addba_badbawinsize++;
1927195377Ssam		return 0;
1928195377Ssam	}
1929182829Ssam#endif
1930195377Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1931195377Ssam	    "recv ADDBA response: dialogtoken %u code %d "
1932195377Ssam	    "baparamset 0x%x (tid %d bufsiz %d) batimeout %d",
1933195377Ssam	    dialogtoken, code, baparamset, tid, bufsiz,
1934195377Ssam	    batimeout);
1935195377Ssam	ic->ic_addba_response(ni, tap, code, baparamset, batimeout);
1936195377Ssam	return 0;
1937195377Ssam}
1938170530Ssam
1939195377Ssamstatic int
1940195377Ssamht_recv_action_ba_delba(struct ieee80211_node *ni,
1941195377Ssam	const struct ieee80211_frame *wh,
1942195377Ssam	const uint8_t *frm, const uint8_t *efrm)
1943195377Ssam{
1944195377Ssam	struct ieee80211com *ic = ni->ni_ic;
1945195377Ssam	struct ieee80211_rx_ampdu *rap;
1946195377Ssam	struct ieee80211_tx_ampdu *tap;
1947195377Ssam	uint16_t baparamset, code;
1948195377Ssam	int tid, ac;
1949170530Ssam
1950195377Ssam	baparamset = LE_READ_2(frm+2);
1951195377Ssam	code = LE_READ_2(frm+4);
1952170530Ssam
1953195377Ssam	tid = MS(baparamset, IEEE80211_DELBAPS_TID);
1954170530Ssam
1955195377Ssam	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1956195377Ssam	    "recv DELBA: baparamset 0x%x (tid %d initiator %d) "
1957195377Ssam	    "code %d", baparamset, tid,
1958195377Ssam	    MS(baparamset, IEEE80211_DELBAPS_INIT), code);
1959195377Ssam
1960195377Ssam	if ((baparamset & IEEE80211_DELBAPS_INIT) == 0) {
1961195377Ssam		ac = TID_TO_WME_AC(tid);
1962195377Ssam		tap = &ni->ni_tx_ampdu[ac];
1963195377Ssam		ic->ic_addba_stop(ni, tap);
1964195377Ssam	} else {
1965195377Ssam		rap = &ni->ni_rx_ampdu[tid];
1966195377Ssam		ic->ic_ampdu_rx_stop(ni, rap);
1967170530Ssam	}
1968195377Ssam	return 0;
1969170530Ssam}
1970170530Ssam
1971195377Ssamstatic int
1972195377Ssamht_recv_action_ht_txchwidth(struct ieee80211_node *ni,
1973195377Ssam	const struct ieee80211_frame *wh,
1974170530Ssam	const uint8_t *frm, const uint8_t *efrm)
1975170530Ssam{
1976170530Ssam	int chw;
1977170530Ssam
1978195377Ssam	chw = (frm[2] == IEEE80211_A_HT_TXCHWIDTH_2040) ? 40 : 20;
1979195377Ssam
1980195377Ssam	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1981195377Ssam	    "%s: HT txchwidth, width %d%s",
1982195377Ssam	    __func__, chw, ni->ni_chw != chw ? "*" : "");
1983195377Ssam	if (chw != ni->ni_chw) {
1984195377Ssam		ni->ni_chw = chw;
1985195377Ssam		/* XXX notify on change */
1986170530Ssam	}
1987195377Ssam	return 0;
1988170530Ssam}
1989170530Ssam
1990195377Ssamstatic int
1991195377Ssamht_recv_action_ht_mimopwrsave(struct ieee80211_node *ni,
1992195377Ssam	const struct ieee80211_frame *wh,
1993195377Ssam	const uint8_t *frm, const uint8_t *efrm)
1994195377Ssam{
1995195377Ssam	const struct ieee80211_action_ht_mimopowersave *mps =
1996195377Ssam	    (const struct ieee80211_action_ht_mimopowersave *) frm;
1997195377Ssam
1998195377Ssam	/* XXX check iv_htcaps */
1999195377Ssam	if (mps->am_control & IEEE80211_A_HT_MIMOPWRSAVE_ENA)
2000195377Ssam		ni->ni_flags |= IEEE80211_NODE_MIMO_PS;
2001195377Ssam	else
2002195377Ssam		ni->ni_flags &= ~IEEE80211_NODE_MIMO_PS;
2003195377Ssam	if (mps->am_control & IEEE80211_A_HT_MIMOPWRSAVE_MODE)
2004195377Ssam		ni->ni_flags |= IEEE80211_NODE_MIMO_RTS;
2005195377Ssam	else
2006195377Ssam		ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS;
2007195377Ssam	/* XXX notify on change */
2008195377Ssam	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2009195377Ssam	    "%s: HT MIMO PS (%s%s)", __func__,
2010195377Ssam	    (ni->ni_flags & IEEE80211_NODE_MIMO_PS) ?  "on" : "off",
2011195377Ssam	    (ni->ni_flags & IEEE80211_NODE_MIMO_RTS) ?  "+rts" : ""
2012195377Ssam	);
2013195377Ssam	return 0;
2014195377Ssam}
2015195377Ssam
2016170530Ssam/*
2017170530Ssam * Transmit processing.
2018170530Ssam */
2019170530Ssam
2020170530Ssam/*
2021178354Ssam * Check if A-MPDU should be requested/enabled for a stream.
2022178354Ssam * We require a traffic rate above a per-AC threshold and we
2023178354Ssam * also handle backoff from previous failed attempts.
2024178354Ssam *
2025178354Ssam * Drivers may override this method to bring in information
2026178354Ssam * such as link state conditions in making the decision.
2027178354Ssam */
2028178354Ssamstatic int
2029178354Ssamieee80211_ampdu_enable(struct ieee80211_node *ni,
2030178354Ssam	struct ieee80211_tx_ampdu *tap)
2031178354Ssam{
2032178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2033178354Ssam
2034178354Ssam	if (tap->txa_avgpps < vap->iv_ampdu_mintraffic[tap->txa_ac])
2035178354Ssam		return 0;
2036178354Ssam	/* XXX check rssi? */
2037178354Ssam	if (tap->txa_attempts >= ieee80211_addba_maxtries &&
2038178354Ssam	    ticks < tap->txa_nextrequest) {
2039178354Ssam		/*
2040178354Ssam		 * Don't retry too often; txa_nextrequest is set
2041178354Ssam		 * to the minimum interval we'll retry after
2042178354Ssam		 * ieee80211_addba_maxtries failed attempts are made.
2043178354Ssam		 */
2044178354Ssam		return 0;
2045178354Ssam	}
2046178354Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
2047183249Ssam	    "enable AMPDU on %s, avgpps %d pkts %d",
2048183249Ssam	    ieee80211_wme_acnames[tap->txa_ac], tap->txa_avgpps, tap->txa_pkts);
2049178354Ssam	return 1;
2050178354Ssam}
2051178354Ssam
2052178354Ssam/*
2053170530Ssam * Request A-MPDU tx aggregation.  Setup local state and
2054170530Ssam * issue an ADDBA request.  BA use will only happen after
2055170530Ssam * the other end replies with ADDBA response.
2056170530Ssam */
2057170530Ssamint
2058170530Ssamieee80211_ampdu_request(struct ieee80211_node *ni,
2059170530Ssam	struct ieee80211_tx_ampdu *tap)
2060170530Ssam{
2061170530Ssam	struct ieee80211com *ic = ni->ni_ic;
2062205277Srpaulo	uint16_t args[5];
2063170530Ssam	int tid, dialogtoken;
2064170530Ssam	static int tokens = 0;	/* XXX */
2065170530Ssam
2066170530Ssam	/* XXX locking */
2067170530Ssam	if ((tap->txa_flags & IEEE80211_AGGR_SETUP) == 0) {
2068170530Ssam		/* do deferred setup of state */
2069184280Ssam		ampdu_tx_setup(tap);
2070170530Ssam	}
2071173273Ssam	/* XXX hack for not doing proper locking */
2072173273Ssam	tap->txa_flags &= ~IEEE80211_AGGR_NAK;
2073173273Ssam
2074170530Ssam	dialogtoken = (tokens+1) % 63;		/* XXX */
2075183245Ssam	tid = WME_AC_TO_TID(tap->txa_ac);
2076183245Ssam	tap->txa_start = ni->ni_txseqs[tid];
2077170530Ssam
2078170530Ssam	args[0] = dialogtoken;
2079205277Srpaulo	args[1] = 0;	/* NB: status code not used */
2080205277Srpaulo	args[2]	= IEEE80211_BAPS_POLICY_IMMEDIATE
2081170530Ssam		| SM(tid, IEEE80211_BAPS_TID)
2082170530Ssam		| SM(IEEE80211_AGGR_BAWMAX, IEEE80211_BAPS_BUFSIZ)
2083170530Ssam		;
2084205277Srpaulo	args[3] = 0;	/* batimeout */
2085170530Ssam	/* NB: do first so there's no race against reply */
2086205277Srpaulo	if (!ic->ic_addba_request(ni, tap, dialogtoken, args[2], args[3])) {
2087170530Ssam		/* unable to setup state, don't make request */
2088178354Ssam		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2089173273Ssam		    ni, "%s: could not setup BA stream for AC %d",
2090173273Ssam		    __func__, tap->txa_ac);
2091173273Ssam		/* defer next try so we don't slam the driver with requests */
2092178354Ssam		tap->txa_attempts = ieee80211_addba_maxtries;
2093178354Ssam		/* NB: check in case driver wants to override */
2094178354Ssam		if (tap->txa_nextrequest <= ticks)
2095178354Ssam			tap->txa_nextrequest = ticks + ieee80211_addba_backoff;
2096170530Ssam		return 0;
2097170530Ssam	}
2098170530Ssam	tokens = dialogtoken;			/* allocate token */
2099183245Ssam	/* NB: after calling ic_addba_request so driver can set txa_start */
2100205277Srpaulo	args[4] = SM(tap->txa_start, IEEE80211_BASEQ_START)
2101178953Ssam		| SM(0, IEEE80211_BASEQ_FRAG)
2102178953Ssam		;
2103170530Ssam	return ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
2104170530Ssam		IEEE80211_ACTION_BA_ADDBA_REQUEST, args);
2105170530Ssam}
2106170530Ssam
2107170530Ssam/*
2108173273Ssam * Terminate an AMPDU tx stream.  State is reclaimed
2109173273Ssam * and the peer notified with a DelBA Action frame.
2110173273Ssam */
2111173273Ssamvoid
2112183250Ssamieee80211_ampdu_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
2113183250Ssam	int reason)
2114173273Ssam{
2115173273Ssam	struct ieee80211com *ic = ni->ni_ic;
2116178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2117173273Ssam	uint16_t args[4];
2118173273Ssam
2119173273Ssam	/* XXX locking */
2120184280Ssam	tap->txa_flags &= ~IEEE80211_AGGR_BARPEND;
2121173273Ssam	if (IEEE80211_AMPDU_RUNNING(tap)) {
2122178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2123183250Ssam		    ni, "%s: stop BA stream for AC %d (reason %d)",
2124183250Ssam		    __func__, tap->txa_ac, reason);
2125178354Ssam		vap->iv_stats.is_ampdu_stop++;
2126173273Ssam
2127173273Ssam		ic->ic_addba_stop(ni, tap);
2128173273Ssam		args[0] = WME_AC_TO_TID(tap->txa_ac);
2129173273Ssam		args[1] = IEEE80211_DELBAPS_INIT;
2130183250Ssam		args[2] = reason;			/* XXX reason code */
2131195377Ssam		ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
2132173273Ssam			IEEE80211_ACTION_BA_DELBA, args);
2133173273Ssam	} else {
2134178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2135183250Ssam		    ni, "%s: BA stream for AC %d not running (reason %d)",
2136183250Ssam		    __func__, tap->txa_ac, reason);
2137178354Ssam		vap->iv_stats.is_ampdu_stop_failed++;
2138173273Ssam	}
2139173273Ssam}
2140173273Ssam
2141184280Ssamstatic void
2142184280Ssambar_timeout(void *arg)
2143184280Ssam{
2144184280Ssam	struct ieee80211_tx_ampdu *tap = arg;
2145184280Ssam	struct ieee80211_node *ni = tap->txa_ni;
2146184280Ssam
2147184280Ssam	KASSERT((tap->txa_flags & IEEE80211_AGGR_XCHGPEND) == 0,
2148184280Ssam	    ("bar/addba collision, flags 0x%x", tap->txa_flags));
2149184280Ssam
2150184280Ssam	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2151184280Ssam	    ni, "%s: tid %u flags 0x%x attempts %d", __func__,
2152184280Ssam	    tap->txa_ac, tap->txa_flags, tap->txa_attempts);
2153184280Ssam
2154184280Ssam	/* guard against race with bar_tx_complete */
2155184280Ssam	if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) == 0)
2156184280Ssam		return;
2157184280Ssam	/* XXX ? */
2158184280Ssam	if (tap->txa_attempts >= ieee80211_bar_maxtries)
2159184280Ssam		ieee80211_ampdu_stop(ni, tap, IEEE80211_REASON_TIMEOUT);
2160184280Ssam	else
2161184280Ssam		ieee80211_send_bar(ni, tap, tap->txa_seqpending);
2162184280Ssam}
2163184280Ssam
2164184280Ssamstatic void
2165184280Ssambar_start_timer(struct ieee80211_tx_ampdu *tap)
2166184280Ssam{
2167184280Ssam	callout_reset(&tap->txa_timer, ieee80211_bar_timeout, bar_timeout, tap);
2168184280Ssam}
2169184280Ssam
2170184280Ssamstatic void
2171184280Ssambar_stop_timer(struct ieee80211_tx_ampdu *tap)
2172184280Ssam{
2173184280Ssam	callout_stop(&tap->txa_timer);
2174184280Ssam}
2175184280Ssam
2176184280Ssamstatic void
2177184280Ssambar_tx_complete(struct ieee80211_node *ni, void *arg, int status)
2178184280Ssam{
2179184280Ssam	struct ieee80211_tx_ampdu *tap = arg;
2180184280Ssam
2181184280Ssam	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2182184280Ssam	    ni, "%s: tid %u flags 0x%x pending %d status %d",
2183184280Ssam	    __func__, tap->txa_ac, tap->txa_flags,
2184184280Ssam	    callout_pending(&tap->txa_timer), status);
2185184280Ssam
2186184280Ssam	/* XXX locking */
2187184280Ssam	if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) &&
2188184280Ssam	    callout_pending(&tap->txa_timer)) {
2189184280Ssam		struct ieee80211com *ic = ni->ni_ic;
2190184280Ssam
2191184280Ssam		if (status)		/* ACK'd */
2192184280Ssam			bar_stop_timer(tap);
2193184280Ssam		ic->ic_bar_response(ni, tap, status);
2194184280Ssam		/* NB: just let timer expire so we pace requests */
2195184280Ssam	}
2196184280Ssam}
2197184280Ssam
2198184280Ssamstatic void
2199184280Ssamieee80211_bar_response(struct ieee80211_node *ni,
2200184280Ssam	struct ieee80211_tx_ampdu *tap, int status)
2201184280Ssam{
2202184280Ssam
2203184280Ssam	if (status != 0) {		/* got ACK */
2204184280Ssam		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2205184280Ssam		    ni, "BAR moves BA win <%u:%u> (%u frames) txseq %u tid %u",
2206184280Ssam		    tap->txa_start,
2207184280Ssam		    IEEE80211_SEQ_ADD(tap->txa_start, tap->txa_wnd-1),
2208184280Ssam		    tap->txa_qframes, tap->txa_seqpending,
2209184280Ssam		    WME_AC_TO_TID(tap->txa_ac));
2210184280Ssam
2211184280Ssam		/* NB: timer already stopped in bar_tx_complete */
2212184280Ssam		tap->txa_start = tap->txa_seqpending;
2213184280Ssam		tap->txa_flags &= ~IEEE80211_AGGR_BARPEND;
2214184280Ssam	}
2215184280Ssam}
2216184280Ssam
2217173273Ssam/*
2218170530Ssam * Transmit a BAR frame to the specified node.  The
2219170530Ssam * BAR contents are drawn from the supplied aggregation
2220170530Ssam * state associated with the node.
2221184280Ssam *
2222184280Ssam * NB: we only handle immediate ACK w/ compressed bitmap.
2223170530Ssam */
2224170530Ssamint
2225170530Ssamieee80211_send_bar(struct ieee80211_node *ni,
2226184280Ssam	struct ieee80211_tx_ampdu *tap, ieee80211_seq seq)
2227170530Ssam{
2228178354Ssam#define	senderr(_x, _v)	do { vap->iv_stats._v++; ret = _x; goto bad; } while (0)
2229178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2230170530Ssam	struct ieee80211com *ic = ni->ni_ic;
2231184280Ssam	struct ieee80211_frame_bar *bar;
2232170530Ssam	struct mbuf *m;
2233184280Ssam	uint16_t barctl, barseqctl;
2234170530Ssam	uint8_t *frm;
2235170530Ssam	int tid, ret;
2236170530Ssam
2237184280Ssam	if ((tap->txa_flags & IEEE80211_AGGR_RUNNING) == 0) {
2238184280Ssam		/* no ADDBA response, should not happen */
2239184280Ssam		/* XXX stat+msg */
2240184280Ssam		return EINVAL;
2241184280Ssam	}
2242184280Ssam	/* XXX locking */
2243184280Ssam	bar_stop_timer(tap);
2244184280Ssam
2245170530Ssam	ieee80211_ref_node(ni);
2246170530Ssam
2247184280Ssam	m = ieee80211_getmgtframe(&frm, ic->ic_headroom, sizeof(*bar));
2248170530Ssam	if (m == NULL)
2249170530Ssam		senderr(ENOMEM, is_tx_nobuf);
2250170530Ssam
2251184280Ssam	if (!ieee80211_add_callback(m, bar_tx_complete, tap)) {
2252184280Ssam		m_freem(m);
2253184280Ssam		senderr(ENOMEM, is_tx_nobuf);	/* XXX */
2254184280Ssam		/* NOTREACHED */
2255184280Ssam	}
2256184280Ssam
2257184280Ssam	bar = mtod(m, struct ieee80211_frame_bar *);
2258184280Ssam	bar->i_fc[0] = IEEE80211_FC0_VERSION_0 |
2259170530Ssam		IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_BAR;
2260184280Ssam	bar->i_fc[1] = 0;
2261184280Ssam	IEEE80211_ADDR_COPY(bar->i_ra, ni->ni_macaddr);
2262184280Ssam	IEEE80211_ADDR_COPY(bar->i_ta, vap->iv_myaddr);
2263170530Ssam
2264170530Ssam	tid = WME_AC_TO_TID(tap->txa_ac);
2265170530Ssam	barctl 	= (tap->txa_flags & IEEE80211_AGGR_IMMEDIATE ?
2266184280Ssam			0 : IEEE80211_BAR_NOACK)
2267184280Ssam		| IEEE80211_BAR_COMP
2268184280Ssam		| SM(tid, IEEE80211_BAR_TID)
2269170530Ssam		;
2270184280Ssam	barseqctl = SM(seq, IEEE80211_BAR_SEQ_START);
2271184280Ssam	/* NB: known to have proper alignment */
2272184280Ssam	bar->i_ctl = htole16(barctl);
2273184280Ssam	bar->i_seq = htole16(barseqctl);
2274184280Ssam	m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_bar);
2275170530Ssam
2276178354Ssam	M_WME_SETAC(m, WME_AC_VO);
2277178354Ssam
2278170530Ssam	IEEE80211_NODE_STAT(ni, tx_mgmt);	/* XXX tx_ctl? */
2279170530Ssam
2280184280Ssam	/* XXX locking */
2281184280Ssam	/* init/bump attempts counter */
2282184280Ssam	if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) == 0)
2283184280Ssam		tap->txa_attempts = 1;
2284184280Ssam	else
2285184280Ssam		tap->txa_attempts++;
2286184280Ssam	tap->txa_seqpending = seq;
2287184280Ssam	tap->txa_flags |= IEEE80211_AGGR_BARPEND;
2288170530Ssam
2289184280Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_11N,
2290184280Ssam	    ni, "send BAR: tid %u ctl 0x%x start %u (attempt %d)",
2291184280Ssam	    tid, barctl, seq, tap->txa_attempts);
2292184280Ssam
2293184280Ssam	ret = ic->ic_raw_xmit(ni, m, NULL);
2294184280Ssam	if (ret != 0) {
2295184280Ssam		/* xmit failed, clear state flag */
2296184280Ssam		tap->txa_flags &= ~IEEE80211_AGGR_BARPEND;
2297184280Ssam		goto bad;
2298184280Ssam	}
2299184280Ssam	/* XXX hack against tx complete happening before timer is started */
2300184280Ssam	if (tap->txa_flags & IEEE80211_AGGR_BARPEND)
2301184280Ssam		bar_start_timer(tap);
2302184280Ssam	return 0;
2303170530Ssambad:
2304170530Ssam	ieee80211_free_node(ni);
2305170530Ssam	return ret;
2306170530Ssam#undef senderr
2307170530Ssam}
2308170530Ssam
2309195377Ssamstatic int
2310195377Ssamht_action_output(struct ieee80211_node *ni, struct mbuf *m)
2311195377Ssam{
2312195377Ssam	struct ieee80211_bpf_params params;
2313195377Ssam
2314195377Ssam	memset(&params, 0, sizeof(params));
2315195377Ssam	params.ibp_pri = WME_AC_VO;
2316195377Ssam	params.ibp_rate0 = ni->ni_txparms->mgmtrate;
2317195377Ssam	/* NB: we know all frames are unicast */
2318195377Ssam	params.ibp_try0 = ni->ni_txparms->maxretry;
2319195377Ssam	params.ibp_power = ni->ni_txpower;
2320195377Ssam	return ieee80211_mgmt_output(ni, m, IEEE80211_FC0_SUBTYPE_ACTION,
2321195377Ssam	     &params);
2322195377Ssam}
2323195377Ssam
2324195377Ssam#define	ADDSHORT(frm, v) do {			\
2325195377Ssam	frm[0] = (v) & 0xff;			\
2326195377Ssam	frm[1] = (v) >> 8;			\
2327195377Ssam	frm += 2;				\
2328195377Ssam} while (0)
2329195377Ssam
2330170530Ssam/*
2331170530Ssam * Send an action management frame.  The arguments are stuff
2332170530Ssam * into a frame without inspection; the caller is assumed to
2333170530Ssam * prepare them carefully (e.g. based on the aggregation state).
2334170530Ssam */
2335195377Ssamstatic int
2336195377Ssamht_send_action_ba_addba(struct ieee80211_node *ni,
2337195377Ssam	int category, int action, void *arg0)
2338170530Ssam{
2339178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2340170530Ssam	struct ieee80211com *ic = ni->ni_ic;
2341195377Ssam	uint16_t *args = arg0;
2342170530Ssam	struct mbuf *m;
2343170530Ssam	uint8_t *frm;
2344195377Ssam
2345195377Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2346205277Srpaulo	    "send ADDBA %s: dialogtoken %d status %d "
2347195377Ssam	    "baparamset 0x%x (tid %d) batimeout 0x%x baseqctl 0x%x",
2348195377Ssam	    (action == IEEE80211_ACTION_BA_ADDBA_REQUEST) ?
2349195377Ssam		"request" : "response",
2350205277Srpaulo	    args[0], args[1], args[2], MS(args[2], IEEE80211_BAPS_TID),
2351205277Srpaulo	    args[3], args[4]);
2352195377Ssam
2353195377Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2354195377Ssam	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2355195377Ssam	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2356195377Ssam	ieee80211_ref_node(ni);
2357195377Ssam
2358195377Ssam	m = ieee80211_getmgtframe(&frm,
2359195377Ssam	    ic->ic_headroom + sizeof(struct ieee80211_frame),
2360195377Ssam	    sizeof(uint16_t)	/* action+category */
2361195377Ssam	    /* XXX may action payload */
2362195377Ssam	    + sizeof(struct ieee80211_action_ba_addbaresponse)
2363195377Ssam	);
2364195377Ssam	if (m != NULL) {
2365195377Ssam		*frm++ = category;
2366195377Ssam		*frm++ = action;
2367195377Ssam		*frm++ = args[0];		/* dialog token */
2368205277Srpaulo		if (action == IEEE80211_ACTION_BA_ADDBA_RESPONSE)
2369205277Srpaulo			ADDSHORT(frm, args[1]);	/* status code */
2370205277Srpaulo		ADDSHORT(frm, args[2]);		/* baparamset */
2371205277Srpaulo		ADDSHORT(frm, args[3]);		/* batimeout */
2372195377Ssam		if (action == IEEE80211_ACTION_BA_ADDBA_REQUEST)
2373205277Srpaulo			ADDSHORT(frm, args[4]);	/* baseqctl */
2374195377Ssam		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2375195377Ssam		return ht_action_output(ni, m);
2376195377Ssam	} else {
2377195377Ssam		vap->iv_stats.is_tx_nobuf++;
2378195377Ssam		ieee80211_free_node(ni);
2379195377Ssam		return ENOMEM;
2380195377Ssam	}
2381195377Ssam}
2382195377Ssam
2383195377Ssamstatic int
2384195377Ssamht_send_action_ba_delba(struct ieee80211_node *ni,
2385195377Ssam	int category, int action, void *arg0)
2386195377Ssam{
2387195377Ssam	struct ieee80211vap *vap = ni->ni_vap;
2388195377Ssam	struct ieee80211com *ic = ni->ni_ic;
2389195377Ssam	uint16_t *args = arg0;
2390195377Ssam	struct mbuf *m;
2391170530Ssam	uint16_t baparamset;
2392195377Ssam	uint8_t *frm;
2393170530Ssam
2394195377Ssam	baparamset = SM(args[0], IEEE80211_DELBAPS_TID)
2395195377Ssam		   | args[1]
2396195377Ssam		   ;
2397195377Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2398195377Ssam	    "send DELBA action: tid %d, initiator %d reason %d",
2399195377Ssam	    args[0], args[1], args[2]);
2400170530Ssam
2401178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2402195377Ssam	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2403195377Ssam	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2404170530Ssam	ieee80211_ref_node(ni);
2405170530Ssam
2406170530Ssam	m = ieee80211_getmgtframe(&frm,
2407195377Ssam	    ic->ic_headroom + sizeof(struct ieee80211_frame),
2408195377Ssam	    sizeof(uint16_t)	/* action+category */
2409195377Ssam	    /* XXX may action payload */
2410195377Ssam	    + sizeof(struct ieee80211_action_ba_addbaresponse)
2411170530Ssam	);
2412195377Ssam	if (m != NULL) {
2413195377Ssam		*frm++ = category;
2414195377Ssam		*frm++ = action;
2415195377Ssam		ADDSHORT(frm, baparamset);
2416195377Ssam		ADDSHORT(frm, args[2]);		/* reason code */
2417195377Ssam		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2418195377Ssam		return ht_action_output(ni, m);
2419195377Ssam	} else {
2420195377Ssam		vap->iv_stats.is_tx_nobuf++;
2421195377Ssam		ieee80211_free_node(ni);
2422195377Ssam		return ENOMEM;
2423195377Ssam	}
2424195377Ssam}
2425170530Ssam
2426195377Ssamstatic int
2427195377Ssamht_send_action_ht_txchwidth(struct ieee80211_node *ni,
2428195377Ssam	int category, int action, void *arg0)
2429195377Ssam{
2430195377Ssam	struct ieee80211vap *vap = ni->ni_vap;
2431195377Ssam	struct ieee80211com *ic = ni->ni_ic;
2432195377Ssam	struct mbuf *m;
2433195377Ssam	uint8_t *frm;
2434170530Ssam
2435195377Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2436195377Ssam	    "send HT txchwidth: width %d",
2437195377Ssam	    IEEE80211_IS_CHAN_HT40(ni->ni_chan) ? 40 : 20);
2438170530Ssam
2439195377Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2440195377Ssam	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2441195377Ssam	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2442195377Ssam	ieee80211_ref_node(ni);
2443170530Ssam
2444195377Ssam	m = ieee80211_getmgtframe(&frm,
2445195377Ssam	    ic->ic_headroom + sizeof(struct ieee80211_frame),
2446195377Ssam	    sizeof(uint16_t)	/* action+category */
2447195377Ssam	    /* XXX may action payload */
2448195377Ssam	    + sizeof(struct ieee80211_action_ba_addbaresponse)
2449195377Ssam	);
2450195377Ssam	if (m != NULL) {
2451195377Ssam		*frm++ = category;
2452195377Ssam		*frm++ = action;
2453195377Ssam		*frm++ = IEEE80211_IS_CHAN_HT40(ni->ni_chan) ?
2454195377Ssam			IEEE80211_A_HT_TXCHWIDTH_2040 :
2455195377Ssam			IEEE80211_A_HT_TXCHWIDTH_20;
2456195377Ssam		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2457195377Ssam		return ht_action_output(ni, m);
2458195377Ssam	} else {
2459195377Ssam		vap->iv_stats.is_tx_nobuf++;
2460195377Ssam		ieee80211_free_node(ni);
2461195377Ssam		return ENOMEM;
2462170530Ssam	}
2463195377Ssam}
2464170530Ssam#undef ADDSHORT
2465170530Ssam
2466170530Ssam/*
2467219600Sbschmidt * Construct the MCS bit mask for inclusion in an HT capabilities
2468219600Sbschmidt * information element.
2469170530Ssam */
2470219600Sbschmidtstatic void
2471219600Sbschmidtieee80211_set_mcsset(struct ieee80211com *ic, uint8_t *frm)
2472170530Ssam{
2473170530Ssam	int i;
2474219600Sbschmidt	uint8_t txparams;
2475170530Ssam
2476219600Sbschmidt	KASSERT((ic->ic_rxstream > 0 && ic->ic_rxstream <= 4),
2477219600Sbschmidt	    ("ic_rxstream %d out of range", ic->ic_rxstream));
2478219600Sbschmidt	KASSERT((ic->ic_txstream > 0 && ic->ic_txstream <= 4),
2479219600Sbschmidt	    ("ic_txstream %d out of range", ic->ic_txstream));
2480219600Sbschmidt
2481219600Sbschmidt	for (i = 0; i < ic->ic_rxstream * 8; i++)
2482219600Sbschmidt		setbit(frm, i);
2483219600Sbschmidt	if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
2484219600Sbschmidt	    (ic->ic_htcaps & IEEE80211_HTC_RXMCS32))
2485219600Sbschmidt		setbit(frm, 32);
2486219600Sbschmidt	if (ic->ic_htcaps & IEEE80211_HTC_RXUNEQUAL) {
2487219600Sbschmidt		if (ic->ic_rxstream >= 2) {
2488219600Sbschmidt			for (i = 33; i <= 38; i++)
2489219600Sbschmidt				setbit(frm, i);
2490170530Ssam		}
2491219600Sbschmidt		if (ic->ic_rxstream >= 3) {
2492219600Sbschmidt			for (i = 39; i <= 52; i++)
2493219600Sbschmidt				setbit(frm, i);
2494219600Sbschmidt		}
2495219600Sbschmidt		if (ic->ic_txstream >= 4) {
2496219600Sbschmidt			for (i = 53; i <= 76; i++)
2497219600Sbschmidt				setbit(frm, i);
2498219600Sbschmidt		}
2499170530Ssam	}
2500219600Sbschmidt
2501219600Sbschmidt	if (ic->ic_rxstream != ic->ic_txstream) {
2502219600Sbschmidt		txparams = 0x1;			/* TX MCS set defined */
2503219600Sbschmidt		txparams |= 0x2;		/* TX RX MCS not equal */
2504219600Sbschmidt		txparams |= (ic->ic_txstream - 1) << 2;	/* num TX streams */
2505219600Sbschmidt		if (ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL)
2506219600Sbschmidt			txparams |= 0x16;	/* TX unequal modulation sup */
2507219600Sbschmidt	} else
2508219600Sbschmidt		txparams = 0;
2509219600Sbschmidt	frm[12] = txparams;
2510170530Ssam}
2511170530Ssam
2512170530Ssam/*
2513170530Ssam * Add body of an HTCAP information element.
2514170530Ssam */
2515170530Ssamstatic uint8_t *
2516170530Ssamieee80211_add_htcap_body(uint8_t *frm, struct ieee80211_node *ni)
2517170530Ssam{
2518170530Ssam#define	ADDSHORT(frm, v) do {			\
2519170530Ssam	frm[0] = (v) & 0xff;			\
2520170530Ssam	frm[1] = (v) >> 8;			\
2521170530Ssam	frm += 2;				\
2522170530Ssam} while (0)
2523178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2524205513Srpaulo	uint16_t caps, extcaps;
2525173865Ssam	int rxmax, density;
2526170530Ssam
2527170530Ssam	/* HT capabilities */
2528178354Ssam	caps = vap->iv_htcaps & 0xffff;
2529173273Ssam	/*
2530173273Ssam	 * Note channel width depends on whether we are operating as
2531173273Ssam	 * a sta or not.  When operating as a sta we are generating
2532173273Ssam	 * a request based on our desired configuration.  Otherwise
2533173273Ssam	 * we are operational and the channel attributes identify
2534173273Ssam	 * how we've been setup (which might be different if a fixed
2535173273Ssam	 * channel is specified).
2536173273Ssam	 */
2537178354Ssam	if (vap->iv_opmode == IEEE80211_M_STA) {
2538173273Ssam		/* override 20/40 use based on config */
2539193655Ssam		if (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)
2540173273Ssam			caps |= IEEE80211_HTCAP_CHWIDTH40;
2541173273Ssam		else
2542173273Ssam			caps &= ~IEEE80211_HTCAP_CHWIDTH40;
2543173865Ssam		/* use advertised setting (XXX locally constraint) */
2544173865Ssam		rxmax = MS(ni->ni_htparam, IEEE80211_HTCAP_MAXRXAMPDU);
2545173865Ssam		density = MS(ni->ni_htparam, IEEE80211_HTCAP_MPDUDENSITY);
2546173273Ssam	} else {
2547173273Ssam		/* override 20/40 use based on current channel */
2548178354Ssam		if (IEEE80211_IS_CHAN_HT40(ni->ni_chan))
2549173273Ssam			caps |= IEEE80211_HTCAP_CHWIDTH40;
2550173273Ssam		else
2551173273Ssam			caps &= ~IEEE80211_HTCAP_CHWIDTH40;
2552178354Ssam		rxmax = vap->iv_ampdu_rxmax;
2553178354Ssam		density = vap->iv_ampdu_density;
2554173273Ssam	}
2555170530Ssam	/* adjust short GI based on channel and config */
2556193655Ssam	if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20) == 0)
2557170530Ssam		caps &= ~IEEE80211_HTCAP_SHORTGI20;
2558193655Ssam	if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40) == 0 ||
2559170530Ssam	    (caps & IEEE80211_HTCAP_CHWIDTH40) == 0)
2560170530Ssam		caps &= ~IEEE80211_HTCAP_SHORTGI40;
2561170530Ssam	ADDSHORT(frm, caps);
2562170530Ssam
2563170530Ssam	/* HT parameters */
2564173865Ssam	*frm = SM(rxmax, IEEE80211_HTCAP_MAXRXAMPDU)
2565173865Ssam	     | SM(density, IEEE80211_HTCAP_MPDUDENSITY)
2566173273Ssam	     ;
2567170530Ssam	frm++;
2568170530Ssam
2569170530Ssam	/* pre-zero remainder of ie */
2570170530Ssam	memset(frm, 0, sizeof(struct ieee80211_ie_htcap) -
2571170530Ssam		__offsetof(struct ieee80211_ie_htcap, hc_mcsset));
2572170530Ssam
2573170530Ssam	/* supported MCS set */
2574173273Ssam	/*
2575219600Sbschmidt	 * XXX: For sta mode the rate set should be restricted based
2576219600Sbschmidt	 * on the AP's capabilities, but ni_htrates isn't setup when
2577219600Sbschmidt	 * we're called to form an AssocReq frame so for now we're
2578219600Sbschmidt	 * restricted to the device capabilities.
2579173273Ssam	 */
2580219600Sbschmidt	ieee80211_set_mcsset(ni->ni_ic, frm);
2581170530Ssam
2582205513Srpaulo	frm += __offsetof(struct ieee80211_ie_htcap, hc_extcap) -
2583205513Srpaulo		__offsetof(struct ieee80211_ie_htcap, hc_mcsset);
2584205513Srpaulo
2585205513Srpaulo	/* HT extended capabilities */
2586205513Srpaulo	extcaps = vap->iv_htextcaps & 0xffff;
2587205513Srpaulo
2588205513Srpaulo	ADDSHORT(frm, extcaps);
2589205513Srpaulo
2590170530Ssam	frm += sizeof(struct ieee80211_ie_htcap) -
2591205513Srpaulo		__offsetof(struct ieee80211_ie_htcap, hc_txbf);
2592205513Srpaulo
2593170530Ssam	return frm;
2594170530Ssam#undef ADDSHORT
2595170530Ssam}
2596170530Ssam
2597170530Ssam/*
2598170530Ssam * Add 802.11n HT capabilities information element
2599170530Ssam */
2600170530Ssamuint8_t *
2601170530Ssamieee80211_add_htcap(uint8_t *frm, struct ieee80211_node *ni)
2602170530Ssam{
2603170530Ssam	frm[0] = IEEE80211_ELEMID_HTCAP;
2604170530Ssam	frm[1] = sizeof(struct ieee80211_ie_htcap) - 2;
2605170530Ssam	return ieee80211_add_htcap_body(frm + 2, ni);
2606170530Ssam}
2607170530Ssam
2608170530Ssam/*
2609170530Ssam * Add Broadcom OUI wrapped standard HTCAP ie; this is
2610170530Ssam * used for compatibility w/ pre-draft implementations.
2611170530Ssam */
2612170530Ssamuint8_t *
2613170530Ssamieee80211_add_htcap_vendor(uint8_t *frm, struct ieee80211_node *ni)
2614170530Ssam{
2615170530Ssam	frm[0] = IEEE80211_ELEMID_VENDOR;
2616170530Ssam	frm[1] = 4 + sizeof(struct ieee80211_ie_htcap) - 2;
2617170530Ssam	frm[2] = (BCM_OUI >> 0) & 0xff;
2618170530Ssam	frm[3] = (BCM_OUI >> 8) & 0xff;
2619170530Ssam	frm[4] = (BCM_OUI >> 16) & 0xff;
2620170530Ssam	frm[5] = BCM_OUI_HTCAP;
2621170530Ssam	return ieee80211_add_htcap_body(frm + 6, ni);
2622170530Ssam}
2623170530Ssam
2624170530Ssam/*
2625170530Ssam * Construct the MCS bit mask of basic rates
2626170530Ssam * for inclusion in an HT information element.
2627170530Ssam */
2628170530Ssamstatic void
2629170530Ssamieee80211_set_basic_htrates(uint8_t *frm, const struct ieee80211_htrateset *rs)
2630170530Ssam{
2631170530Ssam	int i;
2632170530Ssam
2633170530Ssam	for (i = 0; i < rs->rs_nrates; i++) {
2634170530Ssam		int r = rs->rs_rates[i] & IEEE80211_RATE_VAL;
2635170530Ssam		if ((rs->rs_rates[i] & IEEE80211_RATE_BASIC) &&
2636170530Ssam		    r < IEEE80211_HTRATE_MAXSIZE) {
2637170530Ssam			/* NB: this assumes a particular implementation */
2638170530Ssam			setbit(frm, r);
2639170530Ssam		}
2640170530Ssam	}
2641170530Ssam}
2642170530Ssam
2643170530Ssam/*
2644172211Ssam * Update the HTINFO ie for a beacon frame.
2645172211Ssam */
2646172211Ssamvoid
2647178354Ssamieee80211_ht_update_beacon(struct ieee80211vap *vap,
2648172211Ssam	struct ieee80211_beacon_offsets *bo)
2649172211Ssam{
2650172211Ssam#define	PROTMODE	(IEEE80211_HTINFO_OPMODE|IEEE80211_HTINFO_NONHT_PRESENT)
2651178354Ssam	const struct ieee80211_channel *bsschan = vap->iv_bss->ni_chan;
2652178354Ssam	struct ieee80211com *ic = vap->iv_ic;
2653172211Ssam	struct ieee80211_ie_htinfo *ht =
2654172211Ssam	   (struct ieee80211_ie_htinfo *) bo->bo_htinfo;
2655172211Ssam
2656172211Ssam	/* XXX only update on channel change */
2657178354Ssam	ht->hi_ctrlchannel = ieee80211_chan2ieee(ic, bsschan);
2658193655Ssam	if (vap->iv_flags_ht & IEEE80211_FHT_RIFS)
2659183256Ssam		ht->hi_byte1 = IEEE80211_HTINFO_RIFSMODE_PERM;
2660183256Ssam	else
2661183256Ssam		ht->hi_byte1 = IEEE80211_HTINFO_RIFSMODE_PROH;
2662178354Ssam	if (IEEE80211_IS_CHAN_HT40U(bsschan))
2663172211Ssam		ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_ABOVE;
2664178354Ssam	else if (IEEE80211_IS_CHAN_HT40D(bsschan))
2665172211Ssam		ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_BELOW;
2666172211Ssam	else
2667172211Ssam		ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_NONE;
2668178354Ssam	if (IEEE80211_IS_CHAN_HT40(bsschan))
2669172211Ssam		ht->hi_byte1 |= IEEE80211_HTINFO_TXWIDTH_2040;
2670172211Ssam
2671172211Ssam	/* protection mode */
2672172211Ssam	ht->hi_byte2 = (ht->hi_byte2 &~ PROTMODE) | ic->ic_curhtprotmode;
2673172211Ssam
2674172211Ssam	/* XXX propagate to vendor ie's */
2675172211Ssam#undef PROTMODE
2676172211Ssam}
2677172211Ssam
2678172211Ssam/*
2679170530Ssam * Add body of an HTINFO information element.
2680173273Ssam *
2681173273Ssam * NB: We don't use struct ieee80211_ie_htinfo because we can
2682173273Ssam * be called to fillin both a standard ie and a compat ie that
2683173273Ssam * has a vendor OUI at the front.
2684170530Ssam */
2685170530Ssamstatic uint8_t *
2686170530Ssamieee80211_add_htinfo_body(uint8_t *frm, struct ieee80211_node *ni)
2687170530Ssam{
2688183256Ssam	struct ieee80211vap *vap = ni->ni_vap;
2689170530Ssam	struct ieee80211com *ic = ni->ni_ic;
2690170530Ssam
2691170530Ssam	/* pre-zero remainder of ie */
2692170530Ssam	memset(frm, 0, sizeof(struct ieee80211_ie_htinfo) - 2);
2693170530Ssam
2694170530Ssam	/* primary/control channel center */
2695178354Ssam	*frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
2696170530Ssam
2697193655Ssam	if (vap->iv_flags_ht & IEEE80211_FHT_RIFS)
2698183256Ssam		frm[0] = IEEE80211_HTINFO_RIFSMODE_PERM;
2699183256Ssam	else
2700183256Ssam		frm[0] = IEEE80211_HTINFO_RIFSMODE_PROH;
2701178354Ssam	if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan))
2702170530Ssam		frm[0] |= IEEE80211_HTINFO_2NDCHAN_ABOVE;
2703178354Ssam	else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan))
2704170530Ssam		frm[0] |= IEEE80211_HTINFO_2NDCHAN_BELOW;
2705170530Ssam	else
2706170530Ssam		frm[0] |= IEEE80211_HTINFO_2NDCHAN_NONE;
2707178354Ssam	if (IEEE80211_IS_CHAN_HT40(ni->ni_chan))
2708170530Ssam		frm[0] |= IEEE80211_HTINFO_TXWIDTH_2040;
2709170530Ssam
2710172211Ssam	frm[1] = ic->ic_curhtprotmode;
2711170530Ssam
2712170530Ssam	frm += 5;
2713170530Ssam
2714170530Ssam	/* basic MCS set */
2715170530Ssam	ieee80211_set_basic_htrates(frm, &ni->ni_htrates);
2716170530Ssam	frm += sizeof(struct ieee80211_ie_htinfo) -
2717170530Ssam		__offsetof(struct ieee80211_ie_htinfo, hi_basicmcsset);
2718170530Ssam	return frm;
2719170530Ssam}
2720170530Ssam
2721170530Ssam/*
2722170530Ssam * Add 802.11n HT information information element.
2723170530Ssam */
2724170530Ssamuint8_t *
2725170530Ssamieee80211_add_htinfo(uint8_t *frm, struct ieee80211_node *ni)
2726170530Ssam{
2727170530Ssam	frm[0] = IEEE80211_ELEMID_HTINFO;
2728170530Ssam	frm[1] = sizeof(struct ieee80211_ie_htinfo) - 2;
2729170530Ssam	return ieee80211_add_htinfo_body(frm + 2, ni);
2730170530Ssam}
2731170530Ssam
2732170530Ssam/*
2733170530Ssam * Add Broadcom OUI wrapped standard HTINFO ie; this is
2734170530Ssam * used for compatibility w/ pre-draft implementations.
2735170530Ssam */
2736170530Ssamuint8_t *
2737170530Ssamieee80211_add_htinfo_vendor(uint8_t *frm, struct ieee80211_node *ni)
2738170530Ssam{
2739170530Ssam	frm[0] = IEEE80211_ELEMID_VENDOR;
2740170530Ssam	frm[1] = 4 + sizeof(struct ieee80211_ie_htinfo) - 2;
2741170530Ssam	frm[2] = (BCM_OUI >> 0) & 0xff;
2742170530Ssam	frm[3] = (BCM_OUI >> 8) & 0xff;
2743170530Ssam	frm[4] = (BCM_OUI >> 16) & 0xff;
2744170530Ssam	frm[5] = BCM_OUI_HTINFO;
2745170530Ssam	return ieee80211_add_htinfo_body(frm + 6, ni);
2746170530Ssam}
2747