ieee80211_ht.c revision 288085
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 288085 2015-09-22 02:25:29Z adrian $");
29170530Ssam#endif
30170530Ssam
31170530Ssam/*
32170530Ssam * IEEE 802.11n protocol support.
33170530Ssam */
34170530Ssam
35170530Ssam#include "opt_inet.h"
36178354Ssam#include "opt_wlan.h"
37170530Ssam
38170530Ssam#include <sys/param.h>
39170530Ssam#include <sys/kernel.h>
40170530Ssam#include <sys/systm.h>
41170530Ssam#include <sys/endian.h>
42170530Ssam
43170530Ssam#include <sys/socket.h>
44170530Ssam
45170530Ssam#include <net/if.h>
46257176Sglebius#include <net/if_var.h>
47170530Ssam#include <net/if_media.h>
48170530Ssam#include <net/ethernet.h>
49170530Ssam
50170530Ssam#include <net80211/ieee80211_var.h>
51195377Ssam#include <net80211/ieee80211_action.h>
52178354Ssam#include <net80211/ieee80211_input.h>
53170530Ssam
54170530Ssam/* define here, used throughout file */
55170530Ssam#define	MS(_v, _f)	(((_v) & _f) >> _f##_S)
56170530Ssam#define	SM(_v, _f)	(((_v) << _f##_S) & _f)
57170530Ssam
58219456Sbschmidtconst struct ieee80211_mcs_rates ieee80211_htrates[IEEE80211_HTRATE_MAXSIZE] = {
59219456Sbschmidt	{  13,  14,   27,   30 },	/* MCS 0 */
60219456Sbschmidt	{  26,  29,   54,   60 },	/* MCS 1 */
61219456Sbschmidt	{  39,  43,   81,   90 },	/* MCS 2 */
62219456Sbschmidt	{  52,  58,  108,  120 },	/* MCS 3 */
63219456Sbschmidt	{  78,  87,  162,  180 },	/* MCS 4 */
64219456Sbschmidt	{ 104, 116,  216,  240 },	/* MCS 5 */
65219456Sbschmidt	{ 117, 130,  243,  270 },	/* MCS 6 */
66219456Sbschmidt	{ 130, 144,  270,  300 },	/* MCS 7 */
67219456Sbschmidt	{  26,  29,   54,   60 },	/* MCS 8 */
68219456Sbschmidt	{  52,  58,  108,  120 },	/* MCS 9 */
69219456Sbschmidt	{  78,  87,  162,  180 },	/* MCS 10 */
70219456Sbschmidt	{ 104, 116,  216,  240 },	/* MCS 11 */
71219456Sbschmidt	{ 156, 173,  324,  360 },	/* MCS 12 */
72219456Sbschmidt	{ 208, 231,  432,  480 },	/* MCS 13 */
73219456Sbschmidt	{ 234, 260,  486,  540 },	/* MCS 14 */
74219456Sbschmidt	{ 260, 289,  540,  600 },	/* MCS 15 */
75219456Sbschmidt	{  39,  43,   81,   90 },	/* MCS 16 */
76219456Sbschmidt	{  78,  87,  162,  180 },	/* MCS 17 */
77219456Sbschmidt	{ 117, 130,  243,  270 },	/* MCS 18 */
78219456Sbschmidt	{ 156, 173,  324,  360 },	/* MCS 19 */
79219456Sbschmidt	{ 234, 260,  486,  540 },	/* MCS 20 */
80219456Sbschmidt	{ 312, 347,  648,  720 },	/* MCS 21 */
81219456Sbschmidt	{ 351, 390,  729,  810 },	/* MCS 22 */
82219456Sbschmidt	{ 390, 433,  810,  900 },	/* MCS 23 */
83219456Sbschmidt	{  52,  58,  108,  120 },	/* MCS 24 */
84219456Sbschmidt	{ 104, 116,  216,  240 },	/* MCS 25 */
85219456Sbschmidt	{ 156, 173,  324,  360 },	/* MCS 26 */
86219456Sbschmidt	{ 208, 231,  432,  480 },	/* MCS 27 */
87219456Sbschmidt	{ 312, 347,  648,  720 },	/* MCS 28 */
88219456Sbschmidt	{ 416, 462,  864,  960 },	/* MCS 29 */
89219456Sbschmidt	{ 468, 520,  972, 1080 },	/* MCS 30 */
90219456Sbschmidt	{ 520, 578, 1080, 1200 },	/* MCS 31 */
91219456Sbschmidt	{   0,   0,   12,   13 },	/* MCS 32 */
92219456Sbschmidt	{  78,  87,  162,  180 },	/* MCS 33 */
93219456Sbschmidt	{ 104, 116,  216,  240 },	/* MCS 34 */
94219456Sbschmidt	{ 130, 144,  270,  300 },	/* MCS 35 */
95219456Sbschmidt	{ 117, 130,  243,  270 },	/* MCS 36 */
96219456Sbschmidt	{ 156, 173,  324,  360 },	/* MCS 37 */
97219456Sbschmidt	{ 195, 217,  405,  450 },	/* MCS 38 */
98219456Sbschmidt	{ 104, 116,  216,  240 },	/* MCS 39 */
99219456Sbschmidt	{ 130, 144,  270,  300 },	/* MCS 40 */
100219456Sbschmidt	{ 130, 144,  270,  300 },	/* MCS 41 */
101219456Sbschmidt	{ 156, 173,  324,  360 },	/* MCS 42 */
102219456Sbschmidt	{ 182, 202,  378,  420 },	/* MCS 43 */
103219456Sbschmidt	{ 182, 202,  378,  420 },	/* MCS 44 */
104219456Sbschmidt	{ 208, 231,  432,  480 },	/* MCS 45 */
105219456Sbschmidt	{ 156, 173,  324,  360 },	/* MCS 46 */
106219456Sbschmidt	{ 195, 217,  405,  450 },	/* MCS 47 */
107219456Sbschmidt	{ 195, 217,  405,  450 },	/* MCS 48 */
108219456Sbschmidt	{ 234, 260,  486,  540 },	/* MCS 49 */
109219456Sbschmidt	{ 273, 303,  567,  630 },	/* MCS 50 */
110219456Sbschmidt	{ 273, 303,  567,  630 },	/* MCS 51 */
111219456Sbschmidt	{ 312, 347,  648,  720 },	/* MCS 52 */
112219456Sbschmidt	{ 130, 144,  270,  300 },	/* MCS 53 */
113219456Sbschmidt	{ 156, 173,  324,  360 },	/* MCS 54 */
114219456Sbschmidt	{ 182, 202,  378,  420 },	/* MCS 55 */
115219456Sbschmidt	{ 156, 173,  324,  360 },	/* MCS 56 */
116219456Sbschmidt	{ 182, 202,  378,  420 },	/* MCS 57 */
117219456Sbschmidt	{ 208, 231,  432,  480 },	/* MCS 58 */
118219456Sbschmidt	{ 234, 260,  486,  540 },	/* MCS 59 */
119219456Sbschmidt	{ 208, 231,  432,  480 },	/* MCS 60 */
120219456Sbschmidt	{ 234, 260,  486,  540 },	/* MCS 61 */
121219456Sbschmidt	{ 260, 289,  540,  600 },	/* MCS 62 */
122219456Sbschmidt	{ 260, 289,  540,  600 },	/* MCS 63 */
123219456Sbschmidt	{ 286, 318,  594,  660 },	/* MCS 64 */
124219456Sbschmidt	{ 195, 217,  405,  450 },	/* MCS 65 */
125219456Sbschmidt	{ 234, 260,  486,  540 },	/* MCS 66 */
126219456Sbschmidt	{ 273, 303,  567,  630 },	/* MCS 67 */
127219456Sbschmidt	{ 234, 260,  486,  540 },	/* MCS 68 */
128219456Sbschmidt	{ 273, 303,  567,  630 },	/* MCS 69 */
129219456Sbschmidt	{ 312, 347,  648,  720 },	/* MCS 70 */
130219456Sbschmidt	{ 351, 390,  729,  810 },	/* MCS 71 */
131219456Sbschmidt	{ 312, 347,  648,  720 },	/* MCS 72 */
132219456Sbschmidt	{ 351, 390,  729,  810 },	/* MCS 73 */
133219456Sbschmidt	{ 390, 433,  810,  900 },	/* MCS 74 */
134219456Sbschmidt	{ 390, 433,  810,  900 },	/* MCS 75 */
135219456Sbschmidt	{ 429, 477,  891,  990 },	/* MCS 76 */
136170530Ssam};
137170530Ssam
138173273Ssam#ifdef IEEE80211_AMPDU_AGE
139193115Ssamstatic	int ieee80211_ampdu_age = -1;	/* threshold for ampdu reorder q (ms) */
140193115SsamSYSCTL_PROC(_net_wlan, OID_AUTO, ampdu_age, CTLTYPE_INT | CTLFLAG_RW,
141193115Ssam	&ieee80211_ampdu_age, 0, ieee80211_sysctl_msecs_ticks, "I",
142193115Ssam	"AMPDU max reorder age (ms)");
143173273Ssam#endif
144173273Ssam
145193115Ssamstatic	int ieee80211_recv_bar_ena = 1;
146193115SsamSYSCTL_INT(_net_wlan, OID_AUTO, recv_bar, CTLFLAG_RW, &ieee80211_recv_bar_ena,
147193115Ssam	    0, "BAR frame processing (ena/dis)");
148193115Ssam
149193115Ssamstatic	int ieee80211_addba_timeout = -1;/* timeout for ADDBA response */
150193115SsamSYSCTL_PROC(_net_wlan, OID_AUTO, addba_timeout, CTLTYPE_INT | CTLFLAG_RW,
151193115Ssam	&ieee80211_addba_timeout, 0, ieee80211_sysctl_msecs_ticks, "I",
152193115Ssam	"ADDBA request timeout (ms)");
153193115Ssamstatic	int ieee80211_addba_backoff = -1;/* backoff after max ADDBA requests */
154193115SsamSYSCTL_PROC(_net_wlan, OID_AUTO, addba_backoff, CTLTYPE_INT | CTLFLAG_RW,
155193115Ssam	&ieee80211_addba_backoff, 0, ieee80211_sysctl_msecs_ticks, "I",
156193115Ssam	"ADDBA request backoff (ms)");
157193115Ssamstatic	int ieee80211_addba_maxtries = 3;/* max ADDBA requests before backoff */
158273377ShselaskySYSCTL_INT(_net_wlan, OID_AUTO, addba_maxtries, CTLFLAG_RW,
159193115Ssam	&ieee80211_addba_maxtries, 0, "max ADDBA requests sent before backoff");
160193115Ssam
161193115Ssamstatic	int ieee80211_bar_timeout = -1;	/* timeout waiting for BAR response */
162193115Ssamstatic	int ieee80211_bar_maxtries = 50;/* max BAR requests before DELBA */
163193115Ssam
164195377Ssamstatic	ieee80211_recv_action_func ht_recv_action_ba_addba_request;
165195377Ssamstatic	ieee80211_recv_action_func ht_recv_action_ba_addba_response;
166195377Ssamstatic	ieee80211_recv_action_func ht_recv_action_ba_delba;
167195377Ssamstatic	ieee80211_recv_action_func ht_recv_action_ht_mimopwrsave;
168195377Ssamstatic	ieee80211_recv_action_func ht_recv_action_ht_txchwidth;
169195377Ssam
170195377Ssamstatic	ieee80211_send_action_func ht_send_action_ba_addba;
171195377Ssamstatic	ieee80211_send_action_func ht_send_action_ba_delba;
172195377Ssamstatic	ieee80211_send_action_func ht_send_action_ht_txchwidth;
173195377Ssam
174178354Ssamstatic void
175195377Ssamieee80211_ht_init(void)
176178354Ssam{
177195377Ssam	/*
178195377Ssam	 * Setup HT parameters that depends on the clock frequency.
179195377Ssam	 */
180178354Ssam#ifdef IEEE80211_AMPDU_AGE
181178354Ssam	ieee80211_ampdu_age = msecs_to_ticks(500);
182178354Ssam#endif
183178354Ssam	ieee80211_addba_timeout = msecs_to_ticks(250);
184178354Ssam	ieee80211_addba_backoff = msecs_to_ticks(10*1000);
185184280Ssam	ieee80211_bar_timeout = msecs_to_ticks(250);
186195377Ssam	/*
187195377Ssam	 * Register action frame handlers.
188195377Ssam	 */
189195377Ssam	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_BA,
190195377Ssam	    IEEE80211_ACTION_BA_ADDBA_REQUEST, ht_recv_action_ba_addba_request);
191195377Ssam	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_BA,
192195377Ssam	    IEEE80211_ACTION_BA_ADDBA_RESPONSE, ht_recv_action_ba_addba_response);
193195377Ssam	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_BA,
194195377Ssam	    IEEE80211_ACTION_BA_DELBA, ht_recv_action_ba_delba);
195195377Ssam	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_HT,
196195377Ssam	    IEEE80211_ACTION_HT_MIMOPWRSAVE, ht_recv_action_ht_mimopwrsave);
197195377Ssam	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_HT,
198195377Ssam	    IEEE80211_ACTION_HT_TXCHWIDTH, ht_recv_action_ht_txchwidth);
199195377Ssam
200195377Ssam	ieee80211_send_action_register(IEEE80211_ACTION_CAT_BA,
201195377Ssam	    IEEE80211_ACTION_BA_ADDBA_REQUEST, ht_send_action_ba_addba);
202195377Ssam	ieee80211_send_action_register(IEEE80211_ACTION_CAT_BA,
203195377Ssam	    IEEE80211_ACTION_BA_ADDBA_RESPONSE, ht_send_action_ba_addba);
204195377Ssam	ieee80211_send_action_register(IEEE80211_ACTION_CAT_BA,
205195377Ssam	    IEEE80211_ACTION_BA_DELBA, ht_send_action_ba_delba);
206195377Ssam	ieee80211_send_action_register(IEEE80211_ACTION_CAT_HT,
207195377Ssam	    IEEE80211_ACTION_HT_TXCHWIDTH, ht_send_action_ht_txchwidth);
208178354Ssam}
209195377SsamSYSINIT(wlan_ht, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_ht_init, NULL);
210170530Ssam
211178354Ssamstatic int ieee80211_ampdu_enable(struct ieee80211_node *ni,
212178354Ssam	struct ieee80211_tx_ampdu *tap);
213170530Ssamstatic int ieee80211_addba_request(struct ieee80211_node *ni,
214170530Ssam	struct ieee80211_tx_ampdu *tap,
215170530Ssam	int dialogtoken, int baparamset, int batimeout);
216170530Ssamstatic int ieee80211_addba_response(struct ieee80211_node *ni,
217170530Ssam	struct ieee80211_tx_ampdu *tap,
218170530Ssam	int code, int baparamset, int batimeout);
219170530Ssamstatic void ieee80211_addba_stop(struct ieee80211_node *ni,
220170530Ssam	struct ieee80211_tx_ampdu *tap);
221223331Sadrianstatic void null_addba_response_timeout(struct ieee80211_node *ni,
222223331Sadrian	struct ieee80211_tx_ampdu *tap);
223223331Sadrian
224184280Ssamstatic void ieee80211_bar_response(struct ieee80211_node *ni,
225184280Ssam	struct ieee80211_tx_ampdu *tap, int status);
226184280Ssamstatic void ampdu_tx_stop(struct ieee80211_tx_ampdu *tap);
227184280Ssamstatic void bar_stop_timer(struct ieee80211_tx_ampdu *tap);
228191552Ssamstatic int ampdu_rx_start(struct ieee80211_node *, struct ieee80211_rx_ampdu *,
229191552Ssam	int baparamset, int batimeout, int baseqctl);
230191552Ssamstatic void ampdu_rx_stop(struct ieee80211_node *, struct ieee80211_rx_ampdu *);
231170530Ssam
232170530Ssamvoid
233170530Ssamieee80211_ht_attach(struct ieee80211com *ic)
234170530Ssam{
235170530Ssam	/* setup default aggregation policy */
236195377Ssam	ic->ic_recv_action = ieee80211_recv_action;
237170530Ssam	ic->ic_send_action = ieee80211_send_action;
238178354Ssam	ic->ic_ampdu_enable = ieee80211_ampdu_enable;
239170530Ssam	ic->ic_addba_request = ieee80211_addba_request;
240170530Ssam	ic->ic_addba_response = ieee80211_addba_response;
241223331Sadrian	ic->ic_addba_response_timeout = null_addba_response_timeout;
242170530Ssam	ic->ic_addba_stop = ieee80211_addba_stop;
243184280Ssam	ic->ic_bar_response = ieee80211_bar_response;
244191552Ssam	ic->ic_ampdu_rx_start = ampdu_rx_start;
245191552Ssam	ic->ic_ampdu_rx_stop = ampdu_rx_stop;
246170530Ssam
247173273Ssam	ic->ic_htprotmode = IEEE80211_PROT_RTSCTS;
248173273Ssam	ic->ic_curhtprotmode = IEEE80211_HTINFO_OPMODE_PURE;
249178354Ssam}
250173273Ssam
251178354Ssamvoid
252178354Ssamieee80211_ht_detach(struct ieee80211com *ic)
253178354Ssam{
254178354Ssam}
255173273Ssam
256178354Ssamvoid
257178354Ssamieee80211_ht_vattach(struct ieee80211vap *vap)
258178354Ssam{
259178354Ssam
260178354Ssam	/* driver can override defaults */
261178354Ssam	vap->iv_ampdu_rxmax = IEEE80211_HTCAP_MAXRXAMPDU_8K;
262178354Ssam	vap->iv_ampdu_density = IEEE80211_HTCAP_MPDUDENSITY_NA;
263178354Ssam	vap->iv_ampdu_limit = vap->iv_ampdu_rxmax;
264178354Ssam	vap->iv_amsdu_limit = vap->iv_htcaps & IEEE80211_HTCAP_MAXAMSDU;
265178354Ssam	/* tx aggregation traffic thresholds */
266178354Ssam	vap->iv_ampdu_mintraffic[WME_AC_BK] = 128;
267178354Ssam	vap->iv_ampdu_mintraffic[WME_AC_BE] = 64;
268178354Ssam	vap->iv_ampdu_mintraffic[WME_AC_VO] = 32;
269178354Ssam	vap->iv_ampdu_mintraffic[WME_AC_VI] = 32;
270178354Ssam
271178354Ssam	if (vap->iv_htcaps & IEEE80211_HTC_HT) {
272170530Ssam		/*
273173273Ssam		 * Device is HT capable; enable all HT-related
274173273Ssam		 * facilities by default.
275170530Ssam		 * XXX these choices may be too aggressive.
276170530Ssam		 */
277193655Ssam		vap->iv_flags_ht |= IEEE80211_FHT_HT
278193655Ssam				 |  IEEE80211_FHT_HTCOMPAT
279193655Ssam				 ;
280178354Ssam		if (vap->iv_htcaps & IEEE80211_HTCAP_SHORTGI20)
281193655Ssam			vap->iv_flags_ht |= IEEE80211_FHT_SHORTGI20;
282173273Ssam		/* XXX infer from channel list? */
283178354Ssam		if (vap->iv_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
284193655Ssam			vap->iv_flags_ht |= IEEE80211_FHT_USEHT40;
285178354Ssam			if (vap->iv_htcaps & IEEE80211_HTCAP_SHORTGI40)
286193655Ssam				vap->iv_flags_ht |= IEEE80211_FHT_SHORTGI40;
287170530Ssam		}
288183256Ssam		/* enable RIFS if capable */
289183256Ssam		if (vap->iv_htcaps & IEEE80211_HTC_RIFS)
290193655Ssam			vap->iv_flags_ht |= IEEE80211_FHT_RIFS;
291183256Ssam
292170530Ssam		/* NB: A-MPDU and A-MSDU rx are mandated, these are tx only */
293193655Ssam		vap->iv_flags_ht |= IEEE80211_FHT_AMPDU_RX;
294178354Ssam		if (vap->iv_htcaps & IEEE80211_HTC_AMPDU)
295193655Ssam			vap->iv_flags_ht |= IEEE80211_FHT_AMPDU_TX;
296193655Ssam		vap->iv_flags_ht |= IEEE80211_FHT_AMSDU_RX;
297178354Ssam		if (vap->iv_htcaps & IEEE80211_HTC_AMSDU)
298193655Ssam			vap->iv_flags_ht |= IEEE80211_FHT_AMSDU_TX;
299170530Ssam	}
300178354Ssam	/* NB: disable default legacy WDS, too many issues right now */
301178354Ssam	if (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY)
302193655Ssam		vap->iv_flags_ht &= ~IEEE80211_FHT_HT;
303170530Ssam}
304170530Ssam
305170530Ssamvoid
306178354Ssamieee80211_ht_vdetach(struct ieee80211vap *vap)
307170530Ssam{
308170530Ssam}
309170530Ssam
310219598Sbschmidtstatic int
311228621Sbschmidtht_getrate(struct ieee80211com *ic, int index, enum ieee80211_phymode mode,
312228621Sbschmidt    int ratetype)
313219598Sbschmidt{
314219598Sbschmidt	int mword, rate;
315219598Sbschmidt
316219598Sbschmidt	mword = ieee80211_rate2media(ic, index | IEEE80211_RATE_MCS, mode);
317219598Sbschmidt	if (IFM_SUBTYPE(mword) != IFM_IEEE80211_MCS)
318219598Sbschmidt		return (0);
319219598Sbschmidt	switch (ratetype) {
320219598Sbschmidt	case 0:
321219598Sbschmidt		rate = ieee80211_htrates[index].ht20_rate_800ns;
322219598Sbschmidt		break;
323219598Sbschmidt	case 1:
324219598Sbschmidt		rate = ieee80211_htrates[index].ht20_rate_400ns;
325219598Sbschmidt		break;
326219598Sbschmidt	case 2:
327219598Sbschmidt		rate = ieee80211_htrates[index].ht40_rate_800ns;
328219598Sbschmidt		break;
329219598Sbschmidt	default:
330219598Sbschmidt		rate = ieee80211_htrates[index].ht40_rate_400ns;
331219598Sbschmidt		break;
332219598Sbschmidt	}
333219598Sbschmidt	return (rate);
334219598Sbschmidt}
335219598Sbschmidt
336219598Sbschmidtstatic struct printranges {
337219598Sbschmidt	int	minmcs;
338219598Sbschmidt	int	maxmcs;
339219598Sbschmidt	int	txstream;
340219598Sbschmidt	int	ratetype;
341219598Sbschmidt	int	htcapflags;
342219598Sbschmidt} ranges[] = {
343219598Sbschmidt	{  0,  7, 1, 0, 0 },
344219598Sbschmidt	{  8, 15, 2, 0, 0 },
345219598Sbschmidt	{ 16, 23, 3, 0, 0 },
346219598Sbschmidt	{ 24, 31, 4, 0, 0 },
347219598Sbschmidt	{ 32,  0, 1, 2, IEEE80211_HTC_TXMCS32 },
348219598Sbschmidt	{ 33, 38, 2, 0, IEEE80211_HTC_TXUNEQUAL },
349219598Sbschmidt	{ 39, 52, 3, 0, IEEE80211_HTC_TXUNEQUAL },
350219598Sbschmidt	{ 53, 76, 4, 0, IEEE80211_HTC_TXUNEQUAL },
351219598Sbschmidt	{  0,  0, 0, 0, 0 },
352219598Sbschmidt};
353219598Sbschmidt
354170530Ssamstatic void
355228621Sbschmidtht_rateprint(struct ieee80211com *ic, enum ieee80211_phymode mode, int ratetype)
356170530Ssam{
357219598Sbschmidt	int minrate, maxrate;
358219598Sbschmidt	struct printranges *range;
359170530Ssam
360219598Sbschmidt	for (range = ranges; range->txstream != 0; range++) {
361219598Sbschmidt		if (ic->ic_txstream < range->txstream)
362170530Ssam			continue;
363219598Sbschmidt		if (range->htcapflags &&
364219598Sbschmidt		    (ic->ic_htcaps & range->htcapflags) == 0)
365219598Sbschmidt			continue;
366219598Sbschmidt		if (ratetype < range->ratetype)
367219598Sbschmidt			continue;
368219598Sbschmidt		minrate = ht_getrate(ic, range->minmcs, mode, ratetype);
369219598Sbschmidt		maxrate = ht_getrate(ic, range->maxmcs, mode, ratetype);
370219598Sbschmidt		if (range->maxmcs) {
371283529Sglebius			ic_printf(ic, "MCS %d-%d: %d%sMbps - %d%sMbps\n",
372219598Sbschmidt			    range->minmcs, range->maxmcs,
373219598Sbschmidt			    minrate/2, ((minrate & 0x1) != 0 ? ".5" : ""),
374219598Sbschmidt			    maxrate/2, ((maxrate & 0x1) != 0 ? ".5" : ""));
375219598Sbschmidt		} else {
376283529Sglebius			ic_printf(ic, "MCS %d: %d%sMbps\n", range->minmcs,
377219598Sbschmidt			    minrate/2, ((minrate & 0x1) != 0 ? ".5" : ""));
378205277Srpaulo		}
379170530Ssam	}
380170530Ssam}
381170530Ssam
382205277Srpaulostatic void
383228621Sbschmidtht_announce(struct ieee80211com *ic, enum ieee80211_phymode mode)
384205277Srpaulo{
385205277Srpaulo	const char *modestr = ieee80211_phymode_name[mode];
386219598Sbschmidt
387283529Sglebius	ic_printf(ic, "%s MCS 20MHz\n", modestr);
388219598Sbschmidt	ht_rateprint(ic, mode, 0);
389219598Sbschmidt	if (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20) {
390283529Sglebius		ic_printf(ic, "%s MCS 20MHz SGI\n", modestr);
391219598Sbschmidt		ht_rateprint(ic, mode, 1);
392219598Sbschmidt	}
393219598Sbschmidt	if (ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
394283529Sglebius		ic_printf(ic, "%s MCS 40MHz:\n", modestr);
395219598Sbschmidt		ht_rateprint(ic, mode, 2);
396219598Sbschmidt	}
397219598Sbschmidt	if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
398219598Sbschmidt	    (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40)) {
399283529Sglebius		ic_printf(ic, "%s MCS 40MHz SGI:\n", modestr);
400219598Sbschmidt		ht_rateprint(ic, mode, 3);
401219598Sbschmidt	}
402205277Srpaulo}
403205277Srpaulo
404170530Ssamvoid
405170530Ssamieee80211_ht_announce(struct ieee80211com *ic)
406170530Ssam{
407219598Sbschmidt
408219598Sbschmidt	if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) ||
409219598Sbschmidt	    isset(ic->ic_modecaps, IEEE80211_MODE_11NG))
410283529Sglebius		ic_printf(ic, "%dT%dR\n", ic->ic_txstream, ic->ic_rxstream);
411170530Ssam	if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA))
412219598Sbschmidt		ht_announce(ic, IEEE80211_MODE_11NA);
413170530Ssam	if (isset(ic->ic_modecaps, IEEE80211_MODE_11NG))
414219598Sbschmidt		ht_announce(ic, IEEE80211_MODE_11NG);
415170530Ssam}
416170530Ssam
417219602Sbschmidtstatic struct ieee80211_htrateset htrateset;
418219602Sbschmidt
419170530Ssamconst struct ieee80211_htrateset *
420170530Ssamieee80211_get_suphtrates(struct ieee80211com *ic,
421219602Sbschmidt    const struct ieee80211_channel *c)
422170530Ssam{
423219602Sbschmidt#define	ADDRATE(x)	do {						\
424219602Sbschmidt	htrateset.rs_rates[htrateset.rs_nrates] = x;			\
425219602Sbschmidt	htrateset.rs_nrates++;						\
426219602Sbschmidt} while (0)
427219602Sbschmidt	int i;
428219602Sbschmidt
429219602Sbschmidt	memset(&htrateset, 0, sizeof(struct ieee80211_htrateset));
430219602Sbschmidt	for (i = 0; i < ic->ic_txstream * 8; i++)
431219602Sbschmidt		ADDRATE(i);
432219602Sbschmidt	if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
433219602Sbschmidt	    (ic->ic_htcaps & IEEE80211_HTC_TXMCS32))
434229284Sbschmidt		ADDRATE(32);
435219602Sbschmidt	if (ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL) {
436219602Sbschmidt		if (ic->ic_txstream >= 2) {
437219602Sbschmidt			 for (i = 33; i <= 38; i++)
438219602Sbschmidt				ADDRATE(i);
439219602Sbschmidt		}
440219602Sbschmidt		if (ic->ic_txstream >= 3) {
441219602Sbschmidt			for (i = 39; i <= 52; i++)
442219602Sbschmidt				ADDRATE(i);
443219602Sbschmidt		}
444219602Sbschmidt		if (ic->ic_txstream == 4) {
445219602Sbschmidt			for (i = 53; i <= 76; i++)
446219602Sbschmidt				ADDRATE(i);
447219602Sbschmidt		}
448219602Sbschmidt	}
449219602Sbschmidt	return &htrateset;
450219602Sbschmidt#undef	ADDRATE
451170530Ssam}
452170530Ssam
453170530Ssam/*
454170530Ssam * Receive processing.
455170530Ssam */
456170530Ssam
457170530Ssam/*
458170530Ssam * Decap the encapsulated A-MSDU frames and dispatch all but
459170530Ssam * the last for delivery.  The last frame is returned for
460170530Ssam * delivery via the normal path.
461170530Ssam */
462170530Ssamstruct mbuf *
463170530Ssamieee80211_decap_amsdu(struct ieee80211_node *ni, struct mbuf *m)
464170530Ssam{
465178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
466173462Ssam	int framelen;
467170530Ssam	struct mbuf *n;
468170530Ssam
469170530Ssam	/* discard 802.3 header inserted by ieee80211_decap */
470170530Ssam	m_adj(m, sizeof(struct ether_header));
471170530Ssam
472178354Ssam	vap->iv_stats.is_amsdu_decap++;
473170530Ssam
474170530Ssam	for (;;) {
475170530Ssam		/*
476170530Ssam		 * Decap the first frame, bust it apart from the
477170530Ssam		 * remainder and deliver.  We leave the last frame
478170530Ssam		 * delivery to the caller (for consistency with other
479170530Ssam		 * code paths, could also do it here).
480170530Ssam		 */
481170530Ssam		m = ieee80211_decap1(m, &framelen);
482170530Ssam		if (m == NULL) {
483178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
484173462Ssam			    ni->ni_macaddr, "a-msdu", "%s", "decap failed");
485178354Ssam			vap->iv_stats.is_amsdu_tooshort++;
486170530Ssam			return NULL;
487170530Ssam		}
488173462Ssam		if (m->m_pkthdr.len == framelen)
489170530Ssam			break;
490170530Ssam		n = m_split(m, framelen, M_NOWAIT);
491170530Ssam		if (n == NULL) {
492178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
493170530Ssam			    ni->ni_macaddr, "a-msdu",
494170530Ssam			    "%s", "unable to split encapsulated frames");
495178354Ssam			vap->iv_stats.is_amsdu_split++;
496170530Ssam			m_freem(m);			/* NB: must reclaim */
497170530Ssam			return NULL;
498170530Ssam		}
499178354Ssam		vap->iv_deliver_data(vap, ni, m);
500170530Ssam
501170530Ssam		/*
502170530Ssam		 * Remove frame contents; each intermediate frame
503170530Ssam		 * is required to be aligned to a 4-byte boundary.
504170530Ssam		 */
505170530Ssam		m = n;
506170530Ssam		m_adj(m, roundup2(framelen, 4) - framelen);	/* padding */
507170530Ssam	}
508170530Ssam	return m;				/* last delivered by caller */
509170530Ssam}
510170530Ssam
511170530Ssam/*
512170530Ssam * Purge all frames in the A-MPDU re-order queue.
513170530Ssam */
514170530Ssamstatic void
515170530Ssamampdu_rx_purge(struct ieee80211_rx_ampdu *rap)
516170530Ssam{
517170530Ssam	struct mbuf *m;
518170530Ssam	int i;
519170530Ssam
520170530Ssam	for (i = 0; i < rap->rxa_wnd; i++) {
521170530Ssam		m = rap->rxa_m[i];
522170530Ssam		if (m != NULL) {
523170530Ssam			rap->rxa_m[i] = NULL;
524170530Ssam			rap->rxa_qbytes -= m->m_pkthdr.len;
525170530Ssam			m_freem(m);
526170530Ssam			if (--rap->rxa_qframes == 0)
527170530Ssam				break;
528170530Ssam		}
529170530Ssam	}
530170530Ssam	KASSERT(rap->rxa_qbytes == 0 && rap->rxa_qframes == 0,
531170530Ssam	    ("lost %u data, %u frames on ampdu rx q",
532170530Ssam	    rap->rxa_qbytes, rap->rxa_qframes));
533170530Ssam}
534170530Ssam
535170530Ssam/*
536178354Ssam * Start A-MPDU rx/re-order processing for the specified TID.
537178354Ssam */
538191552Ssamstatic int
539191552Ssamampdu_rx_start(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap,
540191552Ssam	int baparamset, int batimeout, int baseqctl)
541178354Ssam{
542191552Ssam	int bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
543191552Ssam
544178354Ssam	if (rap->rxa_flags & IEEE80211_AGGR_RUNNING) {
545178354Ssam		/*
546178354Ssam		 * AMPDU previously setup and not terminated with a DELBA,
547178354Ssam		 * flush the reorder q's in case anything remains.
548178354Ssam		 */
549178354Ssam		ampdu_rx_purge(rap);
550178354Ssam	}
551178354Ssam	memset(rap, 0, sizeof(*rap));
552178354Ssam	rap->rxa_wnd = (bufsiz == 0) ?
553178354Ssam	    IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
554191552Ssam	rap->rxa_start = MS(baseqctl, IEEE80211_BASEQ_START);
555178354Ssam	rap->rxa_flags |=  IEEE80211_AGGR_RUNNING | IEEE80211_AGGR_XCHGPEND;
556191552Ssam
557191552Ssam	return 0;
558178354Ssam}
559178354Ssam
560178354Ssam/*
561287948Sadrian * Public function; manually setup the RX ampdu state.
562287948Sadrian */
563287948Sadrianint
564287948Sadrianieee80211_ampdu_rx_start_ext(struct ieee80211_node *ni, int tid, int seq, int baw)
565287948Sadrian{
566287948Sadrian	struct ieee80211_rx_ampdu *rap;
567287948Sadrian
568287948Sadrian	/* XXX TODO: sanity check tid, seq, baw */
569287948Sadrian
570287948Sadrian	rap = &ni->ni_rx_ampdu[tid];
571287948Sadrian
572287948Sadrian	if (rap->rxa_flags & IEEE80211_AGGR_RUNNING) {
573287948Sadrian		/*
574287948Sadrian		 * AMPDU previously setup and not terminated with a DELBA,
575287948Sadrian		 * flush the reorder q's in case anything remains.
576287948Sadrian		 */
577287948Sadrian		ampdu_rx_purge(rap);
578287948Sadrian	}
579287948Sadrian
580287948Sadrian	memset(rap, 0, sizeof(*rap));
581287948Sadrian	rap->rxa_wnd = (baw== 0) ?
582287948Sadrian	    IEEE80211_AGGR_BAWMAX : min(baw, IEEE80211_AGGR_BAWMAX);
583287948Sadrian	rap->rxa_start = seq;
584287948Sadrian	rap->rxa_flags |=  IEEE80211_AGGR_RUNNING | IEEE80211_AGGR_XCHGPEND;
585287948Sadrian
586287948Sadrian	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, ni,
587287948Sadrian	    "%s: tid=%d, start=%d, wnd=%d, flags=0x%08x\n",
588287948Sadrian	    __func__,
589287948Sadrian	    tid,
590287948Sadrian	    seq,
591287948Sadrian	    rap->rxa_wnd,
592287948Sadrian	    rap->rxa_flags);
593287948Sadrian
594287948Sadrian	return 0;
595287948Sadrian}
596287948Sadrian
597287948Sadrian/*
598170530Ssam * Stop A-MPDU rx processing for the specified TID.
599170530Ssam */
600170530Ssamstatic void
601191552Ssamampdu_rx_stop(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
602170530Ssam{
603205277Srpaulo
604170530Ssam	ampdu_rx_purge(rap);
605178354Ssam	rap->rxa_flags &= ~(IEEE80211_AGGR_RUNNING | IEEE80211_AGGR_XCHGPEND);
606170530Ssam}
607170530Ssam
608170530Ssam/*
609170530Ssam * Dispatch a frame from the A-MPDU reorder queue.  The
610170530Ssam * frame is fed back into ieee80211_input marked with an
611183247Ssam * M_AMPDU_MPDU flag so it doesn't come back to us (it also
612170530Ssam * permits ieee80211_input to optimize re-processing).
613170530Ssam */
614170530Ssamstatic __inline void
615170530Ssamampdu_dispatch(struct ieee80211_node *ni, struct mbuf *m)
616170530Ssam{
617183247Ssam	m->m_flags |= M_AMPDU_MPDU;	/* bypass normal processing */
618192468Ssam	/* NB: rssi and noise are ignored w/ M_AMPDU_MPDU set */
619192468Ssam	(void) ieee80211_input(ni, m, 0, 0);
620170530Ssam}
621170530Ssam
622170530Ssam/*
623170530Ssam * Dispatch as many frames as possible from the re-order queue.
624170530Ssam * Frames will always be "at the front"; we process all frames
625170530Ssam * up to the first empty slot in the window.  On completion we
626170530Ssam * cleanup state if there are still pending frames in the current
627170530Ssam * BA window.  We assume the frame at slot 0 is already handled
628170530Ssam * by the caller; we always start at slot 1.
629170530Ssam */
630170530Ssamstatic void
631170530Ssamampdu_rx_dispatch(struct ieee80211_rx_ampdu *rap, struct ieee80211_node *ni)
632170530Ssam{
633178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
634170530Ssam	struct mbuf *m;
635170530Ssam	int i;
636170530Ssam
637170530Ssam	/* flush run of frames */
638170530Ssam	for (i = 1; i < rap->rxa_wnd; i++) {
639170530Ssam		m = rap->rxa_m[i];
640170530Ssam		if (m == NULL)
641170530Ssam			break;
642170530Ssam		rap->rxa_m[i] = NULL;
643170530Ssam		rap->rxa_qbytes -= m->m_pkthdr.len;
644170530Ssam		rap->rxa_qframes--;
645170530Ssam
646170530Ssam		ampdu_dispatch(ni, m);
647170530Ssam	}
648170530Ssam	/*
649170530Ssam	 * If frames remain, copy the mbuf pointers down so
650170530Ssam	 * they correspond to the offsets in the new window.
651170530Ssam	 */
652170530Ssam	if (rap->rxa_qframes != 0) {
653170530Ssam		int n = rap->rxa_qframes, j;
654170530Ssam		for (j = i+1; j < rap->rxa_wnd; j++) {
655170530Ssam			if (rap->rxa_m[j] != NULL) {
656170530Ssam				rap->rxa_m[j-i] = rap->rxa_m[j];
657170530Ssam				rap->rxa_m[j] = NULL;
658170530Ssam				if (--n == 0)
659170530Ssam					break;
660170530Ssam			}
661170530Ssam		}
662170530Ssam		KASSERT(n == 0, ("lost %d frames", n));
663178354Ssam		vap->iv_stats.is_ampdu_rx_copy += rap->rxa_qframes;
664170530Ssam	}
665173273Ssam	/*
666173273Ssam	 * Adjust the start of the BA window to
667173273Ssam	 * reflect the frames just dispatched.
668173273Ssam	 */
669173273Ssam	rap->rxa_start = IEEE80211_SEQ_ADD(rap->rxa_start, i);
670178354Ssam	vap->iv_stats.is_ampdu_rx_oor += i;
671170530Ssam}
672170530Ssam
673173273Ssam#ifdef IEEE80211_AMPDU_AGE
674170530Ssam/*
675173273Ssam * Dispatch all frames in the A-MPDU re-order queue.
676170530Ssam */
677170530Ssamstatic void
678173273Ssamampdu_rx_flush(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
679170530Ssam{
680178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
681170530Ssam	struct mbuf *m;
682170530Ssam	int i;
683170530Ssam
684173273Ssam	for (i = 0; i < rap->rxa_wnd; i++) {
685170530Ssam		m = rap->rxa_m[i];
686170530Ssam		if (m == NULL)
687170530Ssam			continue;
688170530Ssam		rap->rxa_m[i] = NULL;
689170530Ssam		rap->rxa_qbytes -= m->m_pkthdr.len;
690173273Ssam		rap->rxa_qframes--;
691178354Ssam		vap->iv_stats.is_ampdu_rx_oor++;
692173273Ssam
693170530Ssam		ampdu_dispatch(ni, m);
694173273Ssam		if (rap->rxa_qframes == 0)
695170530Ssam			break;
696170530Ssam	}
697170530Ssam}
698173273Ssam#endif /* IEEE80211_AMPDU_AGE */
699170530Ssam
700170530Ssam/*
701173273Ssam * Dispatch all frames in the A-MPDU re-order queue
702173273Ssam * preceding the specified sequence number.  This logic
703173273Ssam * handles window moves due to a received MSDU or BAR.
704173273Ssam */
705173273Ssamstatic void
706173273Ssamampdu_rx_flush_upto(struct ieee80211_node *ni,
707173273Ssam	struct ieee80211_rx_ampdu *rap, ieee80211_seq winstart)
708173273Ssam{
709178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
710173273Ssam	struct mbuf *m;
711173273Ssam	ieee80211_seq seqno;
712173273Ssam	int i;
713173273Ssam
714173273Ssam	/*
715173273Ssam	 * Flush any complete MSDU's with a sequence number lower
716173273Ssam	 * than winstart.  Gaps may exist.  Note that we may actually
717173273Ssam	 * dispatch frames past winstart if a run continues; this is
718173273Ssam	 * an optimization that avoids having to do a separate pass
719173273Ssam	 * to dispatch frames after moving the BA window start.
720173273Ssam	 */
721173273Ssam	seqno = rap->rxa_start;
722173273Ssam	for (i = 0; i < rap->rxa_wnd; i++) {
723173273Ssam		m = rap->rxa_m[i];
724173273Ssam		if (m != NULL) {
725173273Ssam			rap->rxa_m[i] = NULL;
726173273Ssam			rap->rxa_qbytes -= m->m_pkthdr.len;
727173273Ssam			rap->rxa_qframes--;
728178354Ssam			vap->iv_stats.is_ampdu_rx_oor++;
729173273Ssam
730173273Ssam			ampdu_dispatch(ni, m);
731173273Ssam		} else {
732173273Ssam			if (!IEEE80211_SEQ_BA_BEFORE(seqno, winstart))
733173273Ssam				break;
734173273Ssam		}
735173273Ssam		seqno = IEEE80211_SEQ_INC(seqno);
736173273Ssam	}
737173273Ssam	/*
738173273Ssam	 * If frames remain, copy the mbuf pointers down so
739173273Ssam	 * they correspond to the offsets in the new window.
740173273Ssam	 */
741173273Ssam	if (rap->rxa_qframes != 0) {
742173273Ssam		int n = rap->rxa_qframes, j;
743178354Ssam
744178354Ssam		/* NB: this loop assumes i > 0 and/or rxa_m[0] is NULL */
745178354Ssam		KASSERT(rap->rxa_m[0] == NULL,
746178354Ssam		    ("%s: BA window slot 0 occupied", __func__));
747173273Ssam		for (j = i+1; j < rap->rxa_wnd; j++) {
748173273Ssam			if (rap->rxa_m[j] != NULL) {
749173273Ssam				rap->rxa_m[j-i] = rap->rxa_m[j];
750173273Ssam				rap->rxa_m[j] = NULL;
751173273Ssam				if (--n == 0)
752173273Ssam					break;
753173273Ssam			}
754173273Ssam		}
755173273Ssam		KASSERT(n == 0, ("%s: lost %d frames, qframes %d off %d "
756173273Ssam		    "BA win <%d:%d> winstart %d",
757173273Ssam		    __func__, n, rap->rxa_qframes, i, rap->rxa_start,
758173273Ssam		    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
759173273Ssam		    winstart));
760178354Ssam		vap->iv_stats.is_ampdu_rx_copy += rap->rxa_qframes;
761173273Ssam	}
762173273Ssam	/*
763173273Ssam	 * Move the start of the BA window; we use the
764173273Ssam	 * sequence number of the last MSDU that was
765173273Ssam	 * passed up the stack+1 or winstart if stopped on
766173273Ssam	 * a gap in the reorder buffer.
767173273Ssam	 */
768173273Ssam	rap->rxa_start = seqno;
769173273Ssam}
770173273Ssam
771173273Ssam/*
772170530Ssam * Process a received QoS data frame for an HT station.  Handle
773170530Ssam * A-MPDU reordering: if this frame is received out of order
774170530Ssam * and falls within the BA window hold onto it.  Otherwise if
775173273Ssam * this frame completes a run, flush any pending frames.  We
776170530Ssam * return 1 if the frame is consumed.  A 0 is returned if
777170530Ssam * the frame should be processed normally by the caller.
778170530Ssam */
779170530Ssamint
780170530Ssamieee80211_ampdu_reorder(struct ieee80211_node *ni, struct mbuf *m)
781170530Ssam{
782170530Ssam#define	IEEE80211_FC0_QOSDATA \
783170530Ssam	(IEEE80211_FC0_TYPE_DATA|IEEE80211_FC0_SUBTYPE_QOS|IEEE80211_FC0_VERSION_0)
784173273Ssam#define	PROCESS		0	/* caller should process frame */
785173273Ssam#define	CONSUMED	1	/* frame consumed, caller does nothing */
786178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
787170530Ssam	struct ieee80211_qosframe *wh;
788170530Ssam	struct ieee80211_rx_ampdu *rap;
789170530Ssam	ieee80211_seq rxseq;
790170530Ssam	uint8_t tid;
791170530Ssam	int off;
792170530Ssam
793183247Ssam	KASSERT((m->m_flags & (M_AMPDU | M_AMPDU_MPDU)) == M_AMPDU,
794183247Ssam	    ("!a-mpdu or already re-ordered, flags 0x%x", m->m_flags));
795170530Ssam	KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT sta"));
796170530Ssam
797170530Ssam	/* NB: m_len known to be sufficient */
798170530Ssam	wh = mtod(m, struct ieee80211_qosframe *);
799183247Ssam	if (wh->i_fc[0] != IEEE80211_FC0_QOSDATA) {
800183247Ssam		/*
801183247Ssam		 * Not QoS data, shouldn't get here but just
802183247Ssam		 * return it to the caller for processing.
803183247Ssam		 */
804183247Ssam		return PROCESS;
805183247Ssam	}
806193840Ssam	if (IEEE80211_IS_DSTODS(wh))
807173273Ssam		tid = ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0];
808173273Ssam	else
809173273Ssam		tid = wh->i_qos[0];
810173273Ssam	tid &= IEEE80211_QOS_TID;
811170530Ssam	rap = &ni->ni_rx_ampdu[tid];
812170530Ssam	if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
813170530Ssam		/*
814170530Ssam		 * No ADDBA request yet, don't touch.
815170530Ssam		 */
816173273Ssam		return PROCESS;
817170530Ssam	}
818182827Ssam	rxseq = le16toh(*(uint16_t *)wh->i_seq);
819182827Ssam	if ((rxseq & IEEE80211_SEQ_FRAG_MASK) != 0) {
820182827Ssam		/*
821182827Ssam		 * Fragments are not allowed; toss.
822182827Ssam		 */
823182827Ssam		IEEE80211_DISCARD_MAC(vap,
824182827Ssam		    IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
825182827Ssam		    "A-MPDU", "fragment, rxseq 0x%x tid %u%s", rxseq, tid,
826182827Ssam		    wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
827182827Ssam		vap->iv_stats.is_ampdu_rx_drop++;
828182827Ssam		IEEE80211_NODE_STAT(ni, rx_drop);
829182827Ssam		m_freem(m);
830182827Ssam		return CONSUMED;
831182827Ssam	}
832182827Ssam	rxseq >>= IEEE80211_SEQ_SEQ_SHIFT;
833173273Ssam	rap->rxa_nframes++;
834173273Ssamagain:
835170530Ssam	if (rxseq == rap->rxa_start) {
836170530Ssam		/*
837170530Ssam		 * First frame in window.
838170530Ssam		 */
839170530Ssam		if (rap->rxa_qframes != 0) {
840170530Ssam			/*
841170530Ssam			 * Dispatch as many packets as we can.
842170530Ssam			 */
843170530Ssam			KASSERT(rap->rxa_m[0] == NULL, ("unexpected dup"));
844170530Ssam			ampdu_dispatch(ni, m);
845170530Ssam			ampdu_rx_dispatch(rap, ni);
846173273Ssam			return CONSUMED;
847170530Ssam		} else {
848170530Ssam			/*
849170530Ssam			 * In order; advance window and notify
850170530Ssam			 * caller to dispatch directly.
851170530Ssam			 */
852170530Ssam			rap->rxa_start = IEEE80211_SEQ_INC(rxseq);
853173273Ssam			return PROCESS;
854170530Ssam		}
855170530Ssam	}
856170530Ssam	/*
857173273Ssam	 * Frame is out of order; store if in the BA window.
858170530Ssam	 */
859170530Ssam	/* calculate offset in BA window */
860170530Ssam	off = IEEE80211_SEQ_SUB(rxseq, rap->rxa_start);
861173273Ssam	if (off < rap->rxa_wnd) {
862170530Ssam		/*
863173273Ssam		 * Common case (hopefully): in the BA window.
864205277Srpaulo		 * Sec 9.10.7.6.2 a) (p.137)
865173273Ssam		 */
866173273Ssam#ifdef IEEE80211_AMPDU_AGE
867173273Ssam		/*
868173273Ssam		 * Check for frames sitting too long in the reorder queue.
869173273Ssam		 * This should only ever happen if frames are not delivered
870173273Ssam		 * without the sender otherwise notifying us (e.g. with a
871173273Ssam		 * BAR to move the window).  Typically this happens because
872173273Ssam		 * of vendor bugs that cause the sequence number to jump.
873173273Ssam		 * When this happens we get a gap in the reorder queue that
874173273Ssam		 * leaves frame sitting on the queue until they get pushed
875173273Ssam		 * out due to window moves.  When the vendor does not send
876173273Ssam		 * BAR this move only happens due to explicit packet sends
877170530Ssam		 *
878173273Ssam		 * NB: we only track the time of the oldest frame in the
879173273Ssam		 * reorder q; this means that if we flush we might push
880173273Ssam		 * frames that still "new"; if this happens then subsequent
881173273Ssam		 * frames will result in BA window moves which cost something
882173273Ssam		 * but is still better than a big throughput dip.
883170530Ssam		 */
884173273Ssam		if (rap->rxa_qframes != 0) {
885173273Ssam			/* XXX honor batimeout? */
886173273Ssam			if (ticks - rap->rxa_age > ieee80211_ampdu_age) {
887173273Ssam				/*
888173273Ssam				 * Too long since we received the first
889173273Ssam				 * frame; flush the reorder buffer.
890173273Ssam				 */
891173273Ssam				if (rap->rxa_qframes != 0) {
892178354Ssam					vap->iv_stats.is_ampdu_rx_age +=
893173273Ssam					    rap->rxa_qframes;
894173273Ssam					ampdu_rx_flush(ni, rap);
895173273Ssam				}
896173273Ssam				rap->rxa_start = IEEE80211_SEQ_INC(rxseq);
897173273Ssam				return PROCESS;
898173273Ssam			}
899173273Ssam		} else {
900173273Ssam			/*
901173273Ssam			 * First frame, start aging timer.
902173273Ssam			 */
903173273Ssam			rap->rxa_age = ticks;
904173273Ssam		}
905173273Ssam#endif /* IEEE80211_AMPDU_AGE */
906173273Ssam		/* save packet */
907173273Ssam		if (rap->rxa_m[off] == NULL) {
908173273Ssam			rap->rxa_m[off] = m;
909173273Ssam			rap->rxa_qframes++;
910173273Ssam			rap->rxa_qbytes += m->m_pkthdr.len;
911178354Ssam			vap->iv_stats.is_ampdu_rx_reorder++;
912173273Ssam		} else {
913178354Ssam			IEEE80211_DISCARD_MAC(vap,
914173273Ssam			    IEEE80211_MSG_INPUT | IEEE80211_MSG_11N,
915173273Ssam			    ni->ni_macaddr, "a-mpdu duplicate",
916173273Ssam			    "seqno %u tid %u BA win <%u:%u>",
917173273Ssam			    rxseq, tid, rap->rxa_start,
918173273Ssam			    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1));
919178354Ssam			vap->iv_stats.is_rx_dup++;
920173273Ssam			IEEE80211_NODE_STAT(ni, rx_dup);
921173273Ssam			m_freem(m);
922173273Ssam		}
923173273Ssam		return CONSUMED;
924173273Ssam	}
925173273Ssam	if (off < IEEE80211_SEQ_BA_RANGE) {
926173273Ssam		/*
927173273Ssam		 * Outside the BA window, but within range;
928173273Ssam		 * flush the reorder q and move the window.
929205277Srpaulo		 * Sec 9.10.7.6.2 b) (p.138)
930173273Ssam		 */
931178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
932173273Ssam		    "move BA win <%u:%u> (%u frames) rxseq %u tid %u",
933170530Ssam		    rap->rxa_start,
934173273Ssam		    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
935170530Ssam		    rap->rxa_qframes, rxseq, tid);
936178354Ssam		vap->iv_stats.is_ampdu_rx_move++;
937170530Ssam
938173273Ssam		/*
939173273Ssam		 * The spec says to flush frames up to but not including:
940173273Ssam		 * 	WinStart_B = rxseq - rap->rxa_wnd + 1
941173273Ssam		 * Then insert the frame or notify the caller to process
942173273Ssam		 * it immediately.  We can safely do this by just starting
943173273Ssam		 * over again because we know the frame will now be within
944173273Ssam		 * the BA window.
945173273Ssam		 */
946173273Ssam		/* NB: rxa_wnd known to be >0 */
947173273Ssam		ampdu_rx_flush_upto(ni, rap,
948173273Ssam		    IEEE80211_SEQ_SUB(rxseq, rap->rxa_wnd-1));
949173273Ssam		goto again;
950170530Ssam	} else {
951170530Ssam		/*
952173273Ssam		 * Outside the BA window and out of range; toss.
953205277Srpaulo		 * Sec 9.10.7.6.2 c) (p.138)
954170530Ssam		 */
955178354Ssam		IEEE80211_DISCARD_MAC(vap,
956173273Ssam		    IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
957178354Ssam		    "MPDU", "BA win <%u:%u> (%u frames) rxseq %u tid %u%s",
958173273Ssam		    rap->rxa_start,
959173273Ssam		    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
960173273Ssam		    rap->rxa_qframes, rxseq, tid,
961173273Ssam		    wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
962178354Ssam		vap->iv_stats.is_ampdu_rx_drop++;
963173273Ssam		IEEE80211_NODE_STAT(ni, rx_drop);
964170530Ssam		m_freem(m);
965173273Ssam		return CONSUMED;
966170530Ssam	}
967173273Ssam#undef CONSUMED
968173273Ssam#undef PROCESS
969170530Ssam#undef IEEE80211_FC0_QOSDATA
970170530Ssam}
971170530Ssam
972170530Ssam/*
973170530Ssam * Process a BAR ctl frame.  Dispatch all frames up to
974170530Ssam * the sequence number of the frame.  If this frame is
975173273Ssam * out of range it's discarded.
976170530Ssam */
977170530Ssamvoid
978170530Ssamieee80211_recv_bar(struct ieee80211_node *ni, struct mbuf *m0)
979170530Ssam{
980178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
981170530Ssam	struct ieee80211_frame_bar *wh;
982170530Ssam	struct ieee80211_rx_ampdu *rap;
983170530Ssam	ieee80211_seq rxseq;
984170530Ssam	int tid, off;
985170530Ssam
986173273Ssam	if (!ieee80211_recv_bar_ena) {
987173273Ssam#if 0
988178354Ssam		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_11N,
989173273Ssam		    ni->ni_macaddr, "BAR", "%s", "processing disabled");
990173273Ssam#endif
991178354Ssam		vap->iv_stats.is_ampdu_bar_bad++;
992173273Ssam		return;
993173273Ssam	}
994170530Ssam	wh = mtod(m0, struct ieee80211_frame_bar *);
995170530Ssam	/* XXX check basic BAR */
996170530Ssam	tid = MS(le16toh(wh->i_ctl), IEEE80211_BAR_TID);
997170530Ssam	rap = &ni->ni_rx_ampdu[tid];
998170530Ssam	if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
999170530Ssam		/*
1000170530Ssam		 * No ADDBA request yet, don't touch.
1001170530Ssam		 */
1002178354Ssam		IEEE80211_DISCARD_MAC(vap,
1003170530Ssam		    IEEE80211_MSG_INPUT | IEEE80211_MSG_11N,
1004170530Ssam		    ni->ni_macaddr, "BAR", "no BA stream, tid %u", tid);
1005178354Ssam		vap->iv_stats.is_ampdu_bar_bad++;
1006170530Ssam		return;
1007170530Ssam	}
1008178354Ssam	vap->iv_stats.is_ampdu_bar_rx++;
1009170530Ssam	rxseq = le16toh(wh->i_seq) >> IEEE80211_SEQ_SEQ_SHIFT;
1010173273Ssam	if (rxseq == rap->rxa_start)
1011173273Ssam		return;
1012170530Ssam	/* calculate offset in BA window */
1013170530Ssam	off = IEEE80211_SEQ_SUB(rxseq, rap->rxa_start);
1014173273Ssam	if (off < IEEE80211_SEQ_BA_RANGE) {
1015170530Ssam		/*
1016173273Ssam		 * Flush the reorder q up to rxseq and move the window.
1017205277Srpaulo		 * Sec 9.10.7.6.3 a) (p.138)
1018170530Ssam		 */
1019178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
1020173273Ssam		    "BAR moves BA win <%u:%u> (%u frames) rxseq %u tid %u",
1021170530Ssam		    rap->rxa_start,
1022173273Ssam		    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
1023173273Ssam		    rap->rxa_qframes, rxseq, tid);
1024178354Ssam		vap->iv_stats.is_ampdu_bar_move++;
1025173273Ssam
1026173273Ssam		ampdu_rx_flush_upto(ni, rap, rxseq);
1027173273Ssam		if (off >= rap->rxa_wnd) {
1028173273Ssam			/*
1029173273Ssam			 * BAR specifies a window start to the right of BA
1030173273Ssam			 * window; we must move it explicitly since
1031173273Ssam			 * ampdu_rx_flush_upto will not.
1032173273Ssam			 */
1033173273Ssam			rap->rxa_start = rxseq;
1034170530Ssam		}
1035173273Ssam	} else {
1036170530Ssam		/*
1037173273Ssam		 * Out of range; toss.
1038205277Srpaulo		 * Sec 9.10.7.6.3 b) (p.138)
1039170530Ssam		 */
1040178354Ssam		IEEE80211_DISCARD_MAC(vap,
1041173273Ssam		    IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
1042173273Ssam		    "BAR", "BA win <%u:%u> (%u frames) rxseq %u tid %u%s",
1043173273Ssam		    rap->rxa_start,
1044173273Ssam		    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
1045173273Ssam		    rap->rxa_qframes, rxseq, tid,
1046173273Ssam		    wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
1047178354Ssam		vap->iv_stats.is_ampdu_bar_oow++;
1048173273Ssam		IEEE80211_NODE_STAT(ni, rx_drop);
1049170530Ssam	}
1050170530Ssam}
1051170530Ssam
1052170530Ssam/*
1053170530Ssam * Setup HT-specific state in a node.  Called only
1054170530Ssam * when HT use is negotiated so we don't do extra
1055170530Ssam * work for temporary and/or legacy sta's.
1056170530Ssam */
1057170530Ssamvoid
1058183254Ssamieee80211_ht_node_init(struct ieee80211_node *ni)
1059170530Ssam{
1060170530Ssam	struct ieee80211_tx_ampdu *tap;
1061234324Sadrian	int tid;
1062170530Ssam
1063242205Sadrian	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
1064242205Sadrian	    ni,
1065242205Sadrian	    "%s: called",
1066242205Sadrian	    __func__);
1067242205Sadrian
1068173273Ssam	if (ni->ni_flags & IEEE80211_NODE_HT) {
1069173273Ssam		/*
1070173273Ssam		 * Clean AMPDU state on re-associate.  This handles the case
1071173273Ssam		 * where a station leaves w/o notifying us and then returns
1072173273Ssam		 * before node is reaped for inactivity.
1073173273Ssam		 */
1074242205Sadrian		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
1075242205Sadrian		    ni,
1076242205Sadrian		    "%s: calling cleanup",
1077242205Sadrian		    __func__);
1078173273Ssam		ieee80211_ht_node_cleanup(ni);
1079173273Ssam	}
1080234324Sadrian	for (tid = 0; tid < WME_NUM_TID; tid++) {
1081234324Sadrian		tap = &ni->ni_tx_ampdu[tid];
1082234324Sadrian		tap->txa_tid = tid;
1083184280Ssam		tap->txa_ni = ni;
1084280349Sadrian		tap->txa_lastsample = ticks;
1085173273Ssam		/* NB: further initialization deferred */
1086170530Ssam	}
1087173273Ssam	ni->ni_flags |= IEEE80211_NODE_HT | IEEE80211_NODE_AMPDU;
1088170530Ssam}
1089170530Ssam
1090170530Ssam/*
1091170530Ssam * Cleanup HT-specific state in a node.  Called only
1092170530Ssam * when HT use has been marked.
1093170530Ssam */
1094170530Ssamvoid
1095170530Ssamieee80211_ht_node_cleanup(struct ieee80211_node *ni)
1096170530Ssam{
1097191552Ssam	struct ieee80211com *ic = ni->ni_ic;
1098170530Ssam	int i;
1099170530Ssam
1100242205Sadrian	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
1101242205Sadrian	    ni,
1102242205Sadrian	    "%s: called",
1103242205Sadrian	    __func__);
1104242205Sadrian
1105170530Ssam	KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT node"));
1106170530Ssam
1107170530Ssam	/* XXX optimize this */
1108234324Sadrian	for (i = 0; i < WME_NUM_TID; i++) {
1109170530Ssam		struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[i];
1110184280Ssam		if (tap->txa_flags & IEEE80211_AGGR_SETUP)
1111184280Ssam			ampdu_tx_stop(tap);
1112170530Ssam	}
1113170530Ssam	for (i = 0; i < WME_NUM_TID; i++)
1114191552Ssam		ic->ic_ampdu_rx_stop(ni, &ni->ni_rx_ampdu[i]);
1115170530Ssam
1116170530Ssam	ni->ni_htcap = 0;
1117182828Ssam	ni->ni_flags &= ~IEEE80211_NODE_HT_ALL;
1118170530Ssam}
1119170530Ssam
1120178354Ssam/*
1121178354Ssam * Age out HT resources for a station.
1122178354Ssam */
1123178354Ssamvoid
1124178354Ssamieee80211_ht_node_age(struct ieee80211_node *ni)
1125178354Ssam{
1126178354Ssam#ifdef IEEE80211_AMPDU_AGE
1127178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1128178354Ssam	uint8_t tid;
1129178354Ssam#endif
1130178354Ssam
1131178354Ssam	KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT sta"));
1132178354Ssam
1133178354Ssam#ifdef IEEE80211_AMPDU_AGE
1134178354Ssam	for (tid = 0; tid < WME_NUM_TID; tid++) {
1135178354Ssam		struct ieee80211_rx_ampdu *rap;
1136178354Ssam
1137178354Ssam		rap = &ni->ni_rx_ampdu[tid];
1138178354Ssam		if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0)
1139178354Ssam			continue;
1140178354Ssam		if (rap->rxa_qframes == 0)
1141178354Ssam			continue;
1142178354Ssam		/*
1143178354Ssam		 * Check for frames sitting too long in the reorder queue.
1144178354Ssam		 * See above for more details on what's happening here.
1145178354Ssam		 */
1146178354Ssam		/* XXX honor batimeout? */
1147178354Ssam		if (ticks - rap->rxa_age > ieee80211_ampdu_age) {
1148178354Ssam			/*
1149178354Ssam			 * Too long since we received the first
1150178354Ssam			 * frame; flush the reorder buffer.
1151178354Ssam			 */
1152178354Ssam			vap->iv_stats.is_ampdu_rx_age += rap->rxa_qframes;
1153178354Ssam			ampdu_rx_flush(ni, rap);
1154178354Ssam		}
1155178354Ssam	}
1156178354Ssam#endif /* IEEE80211_AMPDU_AGE */
1157178354Ssam}
1158178354Ssam
1159173273Ssamstatic struct ieee80211_channel *
1160173273Ssamfindhtchan(struct ieee80211com *ic, struct ieee80211_channel *c, int htflags)
1161173273Ssam{
1162173273Ssam	return ieee80211_find_channel(ic, c->ic_freq,
1163173273Ssam	    (c->ic_flags &~ IEEE80211_CHAN_HT) | htflags);
1164173273Ssam}
1165173273Ssam
1166173273Ssam/*
1167173273Ssam * Adjust a channel to be HT/non-HT according to the vap's configuration.
1168173273Ssam */
1169173273Ssamstruct ieee80211_channel *
1170173273Ssamieee80211_ht_adjust_channel(struct ieee80211com *ic,
1171173273Ssam	struct ieee80211_channel *chan, int flags)
1172173273Ssam{
1173173273Ssam	struct ieee80211_channel *c;
1174173273Ssam
1175193655Ssam	if (flags & IEEE80211_FHT_HT) {
1176173273Ssam		/* promote to HT if possible */
1177193655Ssam		if (flags & IEEE80211_FHT_USEHT40) {
1178173273Ssam			if (!IEEE80211_IS_CHAN_HT40(chan)) {
1179173273Ssam				/* NB: arbitrarily pick ht40+ over ht40- */
1180173273Ssam				c = findhtchan(ic, chan, IEEE80211_CHAN_HT40U);
1181173273Ssam				if (c == NULL)
1182173273Ssam					c = findhtchan(ic, chan,
1183173273Ssam						IEEE80211_CHAN_HT40D);
1184173273Ssam				if (c == NULL)
1185173273Ssam					c = findhtchan(ic, chan,
1186173273Ssam						IEEE80211_CHAN_HT20);
1187173273Ssam				if (c != NULL)
1188173273Ssam					chan = c;
1189173273Ssam			}
1190173273Ssam		} else if (!IEEE80211_IS_CHAN_HT20(chan)) {
1191173273Ssam			c = findhtchan(ic, chan, IEEE80211_CHAN_HT20);
1192173273Ssam			if (c != NULL)
1193173273Ssam				chan = c;
1194173273Ssam		}
1195173273Ssam	} else if (IEEE80211_IS_CHAN_HT(chan)) {
1196173273Ssam		/* demote to legacy, HT use is disabled */
1197173273Ssam		c = ieee80211_find_channel(ic, chan->ic_freq,
1198173273Ssam		    chan->ic_flags &~ IEEE80211_CHAN_HT);
1199173273Ssam		if (c != NULL)
1200173273Ssam			chan = c;
1201173273Ssam	}
1202173273Ssam	return chan;
1203173273Ssam}
1204173273Ssam
1205173273Ssam/*
1206173273Ssam * Setup HT-specific state for a legacy WDS peer.
1207173273Ssam */
1208173273Ssamvoid
1209173273Ssamieee80211_ht_wds_init(struct ieee80211_node *ni)
1210173273Ssam{
1211178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1212173273Ssam	struct ieee80211_tx_ampdu *tap;
1213234324Sadrian	int tid;
1214173273Ssam
1215193655Ssam	KASSERT(vap->iv_flags_ht & IEEE80211_FHT_HT, ("no HT requested"));
1216173273Ssam
1217173273Ssam	/* XXX check scan cache in case peer has an ap and we have info */
1218173273Ssam	/*
1219173273Ssam	 * If setup with a legacy channel; locate an HT channel.
1220173273Ssam	 * Otherwise if the inherited channel (from a companion
1221173273Ssam	 * AP) is suitable use it so we use the same location
1222173273Ssam	 * for the extension channel).
1223173273Ssam	 */
1224178354Ssam	ni->ni_chan = ieee80211_ht_adjust_channel(ni->ni_ic,
1225178354Ssam	    ni->ni_chan, ieee80211_htchanflags(ni->ni_chan));
1226173273Ssam
1227173273Ssam	ni->ni_htcap = 0;
1228193655Ssam	if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20)
1229173273Ssam		ni->ni_htcap |= IEEE80211_HTCAP_SHORTGI20;
1230173273Ssam	if (IEEE80211_IS_CHAN_HT40(ni->ni_chan)) {
1231173273Ssam		ni->ni_htcap |= IEEE80211_HTCAP_CHWIDTH40;
1232173273Ssam		ni->ni_chw = 40;
1233173273Ssam		if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan))
1234173273Ssam			ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_ABOVE;
1235173273Ssam		else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan))
1236173273Ssam			ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_BELOW;
1237193655Ssam		if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40)
1238173273Ssam			ni->ni_htcap |= IEEE80211_HTCAP_SHORTGI40;
1239173273Ssam	} else {
1240173273Ssam		ni->ni_chw = 20;
1241173273Ssam		ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_NONE;
1242173273Ssam	}
1243173273Ssam	ni->ni_htctlchan = ni->ni_chan->ic_ieee;
1244193655Ssam	if (vap->iv_flags_ht & IEEE80211_FHT_RIFS)
1245183256Ssam		ni->ni_flags |= IEEE80211_NODE_RIFS;
1246183256Ssam	/* XXX does it make sense to enable SMPS? */
1247173273Ssam
1248173273Ssam	ni->ni_htopmode = 0;		/* XXX need protection state */
1249173273Ssam	ni->ni_htstbc = 0;		/* XXX need info */
1250173273Ssam
1251234324Sadrian	for (tid = 0; tid < WME_NUM_TID; tid++) {
1252234324Sadrian		tap = &ni->ni_tx_ampdu[tid];
1253234324Sadrian		tap->txa_tid = tid;
1254280349Sadrian		tap->txa_lastsample = ticks;
1255173273Ssam	}
1256193655Ssam	/* NB: AMPDU tx/rx governed by IEEE80211_FHT_AMPDU_{TX,RX} */
1257173273Ssam	ni->ni_flags |= IEEE80211_NODE_HT | IEEE80211_NODE_AMPDU;
1258173273Ssam}
1259173273Ssam
1260173273Ssam/*
1261173273Ssam * Notify hostap vaps of a change in the HTINFO ie.
1262173273Ssam */
1263173273Ssamstatic void
1264173273Ssamhtinfo_notify(struct ieee80211com *ic)
1265173273Ssam{
1266178354Ssam	struct ieee80211vap *vap;
1267178354Ssam	int first = 1;
1268178354Ssam
1269178354Ssam	IEEE80211_LOCK_ASSERT(ic);
1270178354Ssam
1271178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1272178354Ssam		if (vap->iv_opmode != IEEE80211_M_HOSTAP)
1273178354Ssam			continue;
1274183253Ssam		if (vap->iv_state != IEEE80211_S_RUN ||
1275183253Ssam		    !IEEE80211_IS_CHAN_HT(vap->iv_bss->ni_chan))
1276183253Ssam			continue;
1277178354Ssam		if (first) {
1278178354Ssam			IEEE80211_NOTE(vap,
1279178354Ssam			    IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N,
1280178354Ssam			    vap->iv_bss,
1281178354Ssam			    "HT bss occupancy change: %d sta, %d ht, "
1282178354Ssam			    "%d ht40%s, HT protmode now 0x%x"
1283178354Ssam			    , ic->ic_sta_assoc
1284178354Ssam			    , ic->ic_ht_sta_assoc
1285178354Ssam			    , ic->ic_ht40_sta_assoc
1286193655Ssam			    , (ic->ic_flags_ht & IEEE80211_FHT_NONHT_PR) ?
1287178354Ssam				 ", non-HT sta present" : ""
1288178354Ssam			    , ic->ic_curhtprotmode);
1289178354Ssam			first = 0;
1290178354Ssam		}
1291178354Ssam		ieee80211_beacon_notify(vap, IEEE80211_BEACON_HTINFO);
1292178354Ssam	}
1293173273Ssam}
1294173273Ssam
1295173273Ssam/*
1296173273Ssam * Calculate HT protection mode from current
1297173273Ssam * state and handle updates.
1298173273Ssam */
1299173273Ssamstatic void
1300173273Ssamhtinfo_update(struct ieee80211com *ic)
1301173273Ssam{
1302173273Ssam	uint8_t protmode;
1303173273Ssam
1304178354Ssam	if (ic->ic_sta_assoc != ic->ic_ht_sta_assoc) {
1305178354Ssam		protmode = IEEE80211_HTINFO_OPMODE_MIXED
1306178354Ssam			 | IEEE80211_HTINFO_NONHT_PRESENT;
1307193655Ssam	} else if (ic->ic_flags_ht & IEEE80211_FHT_NONHT_PR) {
1308173273Ssam		protmode = IEEE80211_HTINFO_OPMODE_PROTOPT
1309178354Ssam			 | IEEE80211_HTINFO_NONHT_PRESENT;
1310178354Ssam	} else if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
1311178354Ssam	    IEEE80211_IS_CHAN_HT40(ic->ic_bsschan) &&
1312173273Ssam	    ic->ic_sta_assoc != ic->ic_ht40_sta_assoc) {
1313173273Ssam		protmode = IEEE80211_HTINFO_OPMODE_HT20PR;
1314173273Ssam	} else {
1315173273Ssam		protmode = IEEE80211_HTINFO_OPMODE_PURE;
1316173273Ssam	}
1317173273Ssam	if (protmode != ic->ic_curhtprotmode) {
1318173273Ssam		ic->ic_curhtprotmode = protmode;
1319173273Ssam		htinfo_notify(ic);
1320173273Ssam	}
1321173273Ssam}
1322173273Ssam
1323173273Ssam/*
1324173273Ssam * Handle an HT station joining a BSS.
1325173273Ssam */
1326173273Ssamvoid
1327173273Ssamieee80211_ht_node_join(struct ieee80211_node *ni)
1328173273Ssam{
1329173273Ssam	struct ieee80211com *ic = ni->ni_ic;
1330173273Ssam
1331173273Ssam	IEEE80211_LOCK_ASSERT(ic);
1332173273Ssam
1333173273Ssam	if (ni->ni_flags & IEEE80211_NODE_HT) {
1334173273Ssam		ic->ic_ht_sta_assoc++;
1335173273Ssam		if (ni->ni_chw == 40)
1336173273Ssam			ic->ic_ht40_sta_assoc++;
1337173273Ssam	}
1338173273Ssam	htinfo_update(ic);
1339173273Ssam}
1340173273Ssam
1341173273Ssam/*
1342173273Ssam * Handle an HT station leaving a BSS.
1343173273Ssam */
1344173273Ssamvoid
1345173273Ssamieee80211_ht_node_leave(struct ieee80211_node *ni)
1346173273Ssam{
1347173273Ssam	struct ieee80211com *ic = ni->ni_ic;
1348173273Ssam
1349173273Ssam	IEEE80211_LOCK_ASSERT(ic);
1350173273Ssam
1351173273Ssam	if (ni->ni_flags & IEEE80211_NODE_HT) {
1352173273Ssam		ic->ic_ht_sta_assoc--;
1353173273Ssam		if (ni->ni_chw == 40)
1354173273Ssam			ic->ic_ht40_sta_assoc--;
1355173273Ssam	}
1356173273Ssam	htinfo_update(ic);
1357173273Ssam}
1358173273Ssam
1359173273Ssam/*
1360173273Ssam * Public version of htinfo_update; used for processing
1361178354Ssam * beacon frames from overlapping bss.
1362178354Ssam *
1363178354Ssam * Caller can specify either IEEE80211_HTINFO_OPMODE_MIXED
1364178354Ssam * (on receipt of a beacon that advertises MIXED) or
1365178354Ssam * IEEE80211_HTINFO_OPMODE_PROTOPT (on receipt of a beacon
1366178354Ssam * from an overlapping legacy bss).  We treat MIXED with
1367178354Ssam * a higher precedence than PROTOPT (i.e. we will not change
1368178354Ssam * change PROTOPT -> MIXED; only MIXED -> PROTOPT).  This
1369178354Ssam * corresponds to how we handle things in htinfo_update.
1370173273Ssam */
1371173273Ssamvoid
1372178354Ssamieee80211_htprot_update(struct ieee80211com *ic, int protmode)
1373173273Ssam{
1374178354Ssam#define	OPMODE(x)	SM(x, IEEE80211_HTINFO_OPMODE)
1375183246Ssam	IEEE80211_LOCK(ic);
1376178354Ssam
1377178354Ssam	/* track non-HT station presence */
1378178354Ssam	KASSERT(protmode & IEEE80211_HTINFO_NONHT_PRESENT,
1379183246Ssam	    ("protmode 0x%x", protmode));
1380193655Ssam	ic->ic_flags_ht |= IEEE80211_FHT_NONHT_PR;
1381178354Ssam	ic->ic_lastnonht = ticks;
1382178354Ssam
1383183246Ssam	if (protmode != ic->ic_curhtprotmode &&
1384183246Ssam	    (OPMODE(ic->ic_curhtprotmode) != IEEE80211_HTINFO_OPMODE_MIXED ||
1385183246Ssam	     OPMODE(protmode) == IEEE80211_HTINFO_OPMODE_PROTOPT)) {
1386183246Ssam		/* push beacon update */
1387183246Ssam		ic->ic_curhtprotmode = protmode;
1388183246Ssam		htinfo_notify(ic);
1389183246Ssam	}
1390181197Ssam	IEEE80211_UNLOCK(ic);
1391178354Ssam#undef OPMODE
1392173273Ssam}
1393173273Ssam
1394173273Ssam/*
1395173273Ssam * Time out presence of an overlapping bss with non-HT
1396173273Ssam * stations.  When operating in hostap mode we listen for
1397173273Ssam * beacons from other stations and if we identify a non-HT
1398173273Ssam * station is present we update the opmode field of the
1399173273Ssam * HTINFO ie.  To identify when all non-HT stations are
1400173273Ssam * gone we time out this condition.
1401173273Ssam */
1402173273Ssamvoid
1403173273Ssamieee80211_ht_timeout(struct ieee80211com *ic)
1404173273Ssam{
1405173273Ssam	IEEE80211_LOCK_ASSERT(ic);
1406173273Ssam
1407193655Ssam	if ((ic->ic_flags_ht & IEEE80211_FHT_NONHT_PR) &&
1408173273Ssam	    time_after(ticks, ic->ic_lastnonht + IEEE80211_NONHT_PRESENT_AGE)) {
1409173273Ssam#if 0
1410178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
1411173273Ssam		    "%s", "time out non-HT STA present on channel");
1412173273Ssam#endif
1413193655Ssam		ic->ic_flags_ht &= ~IEEE80211_FHT_NONHT_PR;
1414173273Ssam		htinfo_update(ic);
1415173273Ssam	}
1416173273Ssam}
1417173273Ssam
1418170530Ssam/*
1419170530Ssam * Process an 802.11n HT capabilities ie.
1420170530Ssam */
1421170530Ssamvoid
1422170530Ssamieee80211_parse_htcap(struct ieee80211_node *ni, const uint8_t *ie)
1423170530Ssam{
1424170530Ssam	if (ie[0] == IEEE80211_ELEMID_VENDOR) {
1425170530Ssam		/*
1426170530Ssam		 * Station used Vendor OUI ie to associate;
1427170530Ssam		 * mark the node so when we respond we'll use
1428170530Ssam		 * the Vendor OUI's and not the standard ie's.
1429170530Ssam		 */
1430170530Ssam		ni->ni_flags |= IEEE80211_NODE_HTCOMPAT;
1431170530Ssam		ie += 4;
1432170530Ssam	} else
1433170530Ssam		ni->ni_flags &= ~IEEE80211_NODE_HTCOMPAT;
1434170530Ssam
1435170530Ssam	ni->ni_htcap = LE_READ_2(ie +
1436170530Ssam		__offsetof(struct ieee80211_ie_htcap, hc_cap));
1437170530Ssam	ni->ni_htparam = ie[__offsetof(struct ieee80211_ie_htcap, hc_param)];
1438170530Ssam}
1439170530Ssam
1440183254Ssamstatic void
1441183254Ssamhtinfo_parse(struct ieee80211_node *ni,
1442183254Ssam	const struct ieee80211_ie_htinfo *htinfo)
1443170530Ssam{
1444170530Ssam	uint16_t w;
1445170530Ssam
1446170530Ssam	ni->ni_htctlchan = htinfo->hi_ctrlchannel;
1447170530Ssam	ni->ni_ht2ndchan = SM(htinfo->hi_byte1, IEEE80211_HTINFO_2NDCHAN);
1448172055Ssam	w = LE_READ_2(&htinfo->hi_byte2);
1449170530Ssam	ni->ni_htopmode = SM(w, IEEE80211_HTINFO_OPMODE);
1450170530Ssam	w = LE_READ_2(&htinfo->hi_byte45);
1451170530Ssam	ni->ni_htstbc = SM(w, IEEE80211_HTINFO_BASIC_STBCMCS);
1452183254Ssam}
1453183254Ssam
1454183254Ssam/*
1455183254Ssam * Parse an 802.11n HT info ie and save useful information
1456183254Ssam * to the node state.  Note this does not effect any state
1457183254Ssam * changes such as for channel width change.
1458183254Ssam */
1459183254Ssamvoid
1460183254Ssamieee80211_parse_htinfo(struct ieee80211_node *ni, const uint8_t *ie)
1461183254Ssam{
1462183254Ssam	if (ie[0] == IEEE80211_ELEMID_VENDOR)
1463183254Ssam		ie += 4;
1464183254Ssam	htinfo_parse(ni, (const struct ieee80211_ie_htinfo *) ie);
1465183254Ssam}
1466183254Ssam
1467183254Ssam/*
1468183254Ssam * Handle 11n channel switch.  Use the received HT ie's to
1469183254Ssam * identify the right channel to use.  If we cannot locate it
1470183254Ssam * in the channel table then fallback to legacy operation.
1471183254Ssam * Note that we use this information to identify the node's
1472183254Ssam * channel only; the caller is responsible for insuring any
1473183254Ssam * required channel change is done (e.g. in sta mode when
1474183254Ssam * parsing the contents of a beacon frame).
1475183254Ssam */
1476233452Sadrianstatic int
1477183254Ssamhtinfo_update_chw(struct ieee80211_node *ni, int htflags)
1478183254Ssam{
1479183254Ssam	struct ieee80211com *ic = ni->ni_ic;
1480183254Ssam	struct ieee80211_channel *c;
1481183254Ssam	int chanflags;
1482233452Sadrian	int ret = 0;
1483183254Ssam
1484173273Ssam	chanflags = (ni->ni_chan->ic_flags &~ IEEE80211_CHAN_HT) | htflags;
1485173273Ssam	if (chanflags != ni->ni_chan->ic_flags) {
1486183254Ssam		/* XXX not right for ht40- */
1487173273Ssam		c = ieee80211_find_channel(ic, ni->ni_chan->ic_freq, chanflags);
1488178354Ssam		if (c == NULL && (htflags & IEEE80211_CHAN_HT40)) {
1489173273Ssam			/*
1490173273Ssam			 * No HT40 channel entry in our table; fall back
1491173273Ssam			 * to HT20 operation.  This should not happen.
1492173273Ssam			 */
1493173273Ssam			c = findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT20);
1494183254Ssam#if 0
1495183254Ssam			IEEE80211_NOTE(ni->ni_vap,
1496173273Ssam			    IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
1497173273Ssam			    "no HT40 channel (freq %u), falling back to HT20",
1498173273Ssam			    ni->ni_chan->ic_freq);
1499183254Ssam#endif
1500173273Ssam			/* XXX stat */
1501173273Ssam		}
1502173273Ssam		if (c != NULL && c != ni->ni_chan) {
1503183254Ssam			IEEE80211_NOTE(ni->ni_vap,
1504173273Ssam			    IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
1505173273Ssam			    "switch station to HT%d channel %u/0x%x",
1506173273Ssam			    IEEE80211_IS_CHAN_HT40(c) ? 40 : 20,
1507173273Ssam			    c->ic_freq, c->ic_flags);
1508173273Ssam			ni->ni_chan = c;
1509233452Sadrian			ret = 1;
1510173273Ssam		}
1511173273Ssam		/* NB: caller responsible for forcing any channel change */
1512173273Ssam	}
1513173273Ssam	/* update node's tx channel width */
1514173273Ssam	ni->ni_chw = IEEE80211_IS_CHAN_HT40(ni->ni_chan)? 40 : 20;
1515233452Sadrian	return (ret);
1516170530Ssam}
1517170530Ssam
1518170530Ssam/*
1519183255Ssam * Update 11n MIMO PS state according to received htcap.
1520183255Ssam */
1521183255Ssamstatic __inline int
1522183255Ssamhtcap_update_mimo_ps(struct ieee80211_node *ni)
1523183255Ssam{
1524183255Ssam	uint16_t oflags = ni->ni_flags;
1525183255Ssam
1526183255Ssam	switch (ni->ni_htcap & IEEE80211_HTCAP_SMPS) {
1527183255Ssam	case IEEE80211_HTCAP_SMPS_DYNAMIC:
1528183255Ssam		ni->ni_flags |= IEEE80211_NODE_MIMO_PS;
1529183255Ssam		ni->ni_flags |= IEEE80211_NODE_MIMO_RTS;
1530183255Ssam		break;
1531183255Ssam	case IEEE80211_HTCAP_SMPS_ENA:
1532183255Ssam		ni->ni_flags |= IEEE80211_NODE_MIMO_PS;
1533183255Ssam		ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS;
1534183255Ssam		break;
1535183255Ssam	case IEEE80211_HTCAP_SMPS_OFF:
1536183255Ssam	default:		/* disable on rx of reserved value */
1537183255Ssam		ni->ni_flags &= ~IEEE80211_NODE_MIMO_PS;
1538183255Ssam		ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS;
1539183255Ssam		break;
1540183255Ssam	}
1541183255Ssam	return (oflags ^ ni->ni_flags);
1542183255Ssam}
1543183255Ssam
1544183255Ssam/*
1545183257Ssam * Update short GI state according to received htcap
1546183257Ssam * and local settings.
1547183257Ssam */
1548183257Ssamstatic __inline void
1549183257Ssamhtcap_update_shortgi(struct ieee80211_node *ni)
1550183257Ssam{
1551183257Ssam	struct ieee80211vap *vap = ni->ni_vap;
1552183257Ssam
1553183257Ssam	ni->ni_flags &= ~(IEEE80211_NODE_SGI20|IEEE80211_NODE_SGI40);
1554183257Ssam	if ((ni->ni_htcap & IEEE80211_HTCAP_SHORTGI20) &&
1555193655Ssam	    (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20))
1556183257Ssam		ni->ni_flags |= IEEE80211_NODE_SGI20;
1557183257Ssam	if ((ni->ni_htcap & IEEE80211_HTCAP_SHORTGI40) &&
1558193655Ssam	    (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40))
1559183257Ssam		ni->ni_flags |= IEEE80211_NODE_SGI40;
1560183257Ssam}
1561183257Ssam
1562183257Ssam/*
1563183254Ssam * Parse and update HT-related state extracted from
1564183254Ssam * the HT cap and info ie's.
1565183254Ssam */
1566233452Sadrianint
1567183254Ssamieee80211_ht_updateparams(struct ieee80211_node *ni,
1568183254Ssam	const uint8_t *htcapie, const uint8_t *htinfoie)
1569183254Ssam{
1570183254Ssam	struct ieee80211vap *vap = ni->ni_vap;
1571183254Ssam	const struct ieee80211_ie_htinfo *htinfo;
1572183254Ssam	int htflags;
1573233452Sadrian	int ret = 0;
1574183254Ssam
1575183254Ssam	ieee80211_parse_htcap(ni, htcapie);
1576183255Ssam	if (vap->iv_htcaps & IEEE80211_HTCAP_SMPS)
1577183255Ssam		htcap_update_mimo_ps(ni);
1578183257Ssam	htcap_update_shortgi(ni);
1579183254Ssam
1580183254Ssam	if (htinfoie[0] == IEEE80211_ELEMID_VENDOR)
1581183254Ssam		htinfoie += 4;
1582183254Ssam	htinfo = (const struct ieee80211_ie_htinfo *) htinfoie;
1583183254Ssam	htinfo_parse(ni, htinfo);
1584183254Ssam
1585193655Ssam	htflags = (vap->iv_flags_ht & IEEE80211_FHT_HT) ?
1586183254Ssam	    IEEE80211_CHAN_HT20 : 0;
1587183254Ssam	/* NB: honor operating mode constraint */
1588183254Ssam	if ((htinfo->hi_byte1 & IEEE80211_HTINFO_TXWIDTH_2040) &&
1589193655Ssam	    (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)) {
1590183254Ssam		if (ni->ni_ht2ndchan == IEEE80211_HTINFO_2NDCHAN_ABOVE)
1591183254Ssam			htflags = IEEE80211_CHAN_HT40U;
1592183254Ssam		else if (ni->ni_ht2ndchan == IEEE80211_HTINFO_2NDCHAN_BELOW)
1593183254Ssam			htflags = IEEE80211_CHAN_HT40D;
1594183254Ssam	}
1595233452Sadrian	if (htinfo_update_chw(ni, htflags))
1596233452Sadrian		ret = 1;
1597183256Ssam
1598183256Ssam	if ((htinfo->hi_byte1 & IEEE80211_HTINFO_RIFSMODE_PERM) &&
1599193655Ssam	    (vap->iv_flags_ht & IEEE80211_FHT_RIFS))
1600183256Ssam		ni->ni_flags |= IEEE80211_NODE_RIFS;
1601183256Ssam	else
1602183256Ssam		ni->ni_flags &= ~IEEE80211_NODE_RIFS;
1603233452Sadrian
1604233452Sadrian	return (ret);
1605183254Ssam}
1606183254Ssam
1607183254Ssam/*
1608183254Ssam * Parse and update HT-related state extracted from the HT cap ie
1609183254Ssam * for a station joining an HT BSS.
1610183254Ssam */
1611183254Ssamvoid
1612183254Ssamieee80211_ht_updatehtcap(struct ieee80211_node *ni, const uint8_t *htcapie)
1613183254Ssam{
1614183254Ssam	struct ieee80211vap *vap = ni->ni_vap;
1615183254Ssam	int htflags;
1616183254Ssam
1617183254Ssam	ieee80211_parse_htcap(ni, htcapie);
1618183255Ssam	if (vap->iv_htcaps & IEEE80211_HTCAP_SMPS)
1619183255Ssam		htcap_update_mimo_ps(ni);
1620183257Ssam	htcap_update_shortgi(ni);
1621183254Ssam
1622183254Ssam	/* NB: honor operating mode constraint */
1623219606Sbschmidt	/* XXX 40 MHz intolerant */
1624193655Ssam	htflags = (vap->iv_flags_ht & IEEE80211_FHT_HT) ?
1625183254Ssam	    IEEE80211_CHAN_HT20 : 0;
1626183254Ssam	if ((ni->ni_htcap & IEEE80211_HTCAP_CHWIDTH40) &&
1627193655Ssam	    (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)) {
1628183254Ssam		if (IEEE80211_IS_CHAN_HT40U(vap->iv_bss->ni_chan))
1629183254Ssam			htflags = IEEE80211_CHAN_HT40U;
1630183254Ssam		else if (IEEE80211_IS_CHAN_HT40D(vap->iv_bss->ni_chan))
1631183254Ssam			htflags = IEEE80211_CHAN_HT40D;
1632183254Ssam	}
1633233452Sadrian	(void) htinfo_update_chw(ni, htflags);
1634183254Ssam}
1635183254Ssam
1636183254Ssam/*
1637170530Ssam * Install received HT rate set by parsing the HT cap ie.
1638170530Ssam */
1639170530Ssamint
1640170530Ssamieee80211_setup_htrates(struct ieee80211_node *ni, const uint8_t *ie, int flags)
1641170530Ssam{
1642219602Sbschmidt	struct ieee80211com *ic = ni->ni_ic;
1643178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1644170530Ssam	const struct ieee80211_ie_htcap *htcap;
1645170530Ssam	struct ieee80211_htrateset *rs;
1646219602Sbschmidt	int i, maxequalmcs, maxunequalmcs;
1647170530Ssam
1648219602Sbschmidt	maxequalmcs = ic->ic_txstream * 8 - 1;
1649219602Sbschmidt	if (ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL) {
1650219602Sbschmidt		if (ic->ic_txstream >= 2)
1651219602Sbschmidt			maxunequalmcs = 38;
1652219602Sbschmidt		if (ic->ic_txstream >= 3)
1653219602Sbschmidt			maxunequalmcs = 52;
1654219602Sbschmidt		if (ic->ic_txstream >= 4)
1655219602Sbschmidt			maxunequalmcs = 76;
1656219602Sbschmidt	} else
1657219602Sbschmidt		maxunequalmcs = 0;
1658219602Sbschmidt
1659170530Ssam	rs = &ni->ni_htrates;
1660170530Ssam	memset(rs, 0, sizeof(*rs));
1661170530Ssam	if (ie != NULL) {
1662170530Ssam		if (ie[0] == IEEE80211_ELEMID_VENDOR)
1663170530Ssam			ie += 4;
1664170530Ssam		htcap = (const struct ieee80211_ie_htcap *) ie;
1665170530Ssam		for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++) {
1666170530Ssam			if (isclr(htcap->hc_mcsset, i))
1667170530Ssam				continue;
1668170530Ssam			if (rs->rs_nrates == IEEE80211_HTRATE_MAXSIZE) {
1669178354Ssam				IEEE80211_NOTE(vap,
1670170530Ssam				    IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
1671170530Ssam				    "WARNING, HT rate set too large; only "
1672170530Ssam				    "using %u rates", IEEE80211_HTRATE_MAXSIZE);
1673178354Ssam				vap->iv_stats.is_rx_rstoobig++;
1674170530Ssam				break;
1675170530Ssam			}
1676219602Sbschmidt			if (i <= 31 && i > maxequalmcs)
1677219602Sbschmidt				continue;
1678219602Sbschmidt			if (i == 32 &&
1679219602Sbschmidt			    (ic->ic_htcaps & IEEE80211_HTC_TXMCS32) == 0)
1680219602Sbschmidt				continue;
1681219602Sbschmidt			if (i > 32 && i > maxunequalmcs)
1682219602Sbschmidt				continue;
1683170530Ssam			rs->rs_rates[rs->rs_nrates++] = i;
1684170530Ssam		}
1685170530Ssam	}
1686170530Ssam	return ieee80211_fix_rate(ni, (struct ieee80211_rateset *) rs, flags);
1687170530Ssam}
1688170530Ssam
1689170530Ssam/*
1690170530Ssam * Mark rates in a node's HT rate set as basic according
1691170530Ssam * to the information in the supplied HT info ie.
1692170530Ssam */
1693170530Ssamvoid
1694170530Ssamieee80211_setup_basic_htrates(struct ieee80211_node *ni, const uint8_t *ie)
1695170530Ssam{
1696170530Ssam	const struct ieee80211_ie_htinfo *htinfo;
1697170530Ssam	struct ieee80211_htrateset *rs;
1698170530Ssam	int i, j;
1699170530Ssam
1700170530Ssam	if (ie[0] == IEEE80211_ELEMID_VENDOR)
1701170530Ssam		ie += 4;
1702170530Ssam	htinfo = (const struct ieee80211_ie_htinfo *) ie;
1703170530Ssam	rs = &ni->ni_htrates;
1704170530Ssam	if (rs->rs_nrates == 0) {
1705178354Ssam		IEEE80211_NOTE(ni->ni_vap,
1706170530Ssam		    IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
1707170530Ssam		    "%s", "WARNING, empty HT rate set");
1708170530Ssam		return;
1709170530Ssam	}
1710170530Ssam	for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++) {
1711170530Ssam		if (isclr(htinfo->hi_basicmcsset, i))
1712170530Ssam			continue;
1713170530Ssam		for (j = 0; j < rs->rs_nrates; j++)
1714170530Ssam			if ((rs->rs_rates[j] & IEEE80211_RATE_VAL) == i)
1715170530Ssam				rs->rs_rates[j] |= IEEE80211_RATE_BASIC;
1716170530Ssam	}
1717170530Ssam}
1718170530Ssam
1719170530Ssamstatic void
1720184280Ssamampdu_tx_setup(struct ieee80211_tx_ampdu *tap)
1721184280Ssam{
1722283291Sjkim	callout_init(&tap->txa_timer, 1);
1723184280Ssam	tap->txa_flags |= IEEE80211_AGGR_SETUP;
1724280349Sadrian	tap->txa_lastsample = ticks;
1725184280Ssam}
1726184280Ssam
1727184280Ssamstatic void
1728184280Ssamampdu_tx_stop(struct ieee80211_tx_ampdu *tap)
1729184280Ssam{
1730184280Ssam	struct ieee80211_node *ni = tap->txa_ni;
1731184280Ssam	struct ieee80211com *ic = ni->ni_ic;
1732184280Ssam
1733242205Sadrian	IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
1734242205Sadrian	    tap->txa_ni,
1735242205Sadrian	    "%s: called",
1736242205Sadrian	    __func__);
1737242205Sadrian
1738184280Ssam	KASSERT(tap->txa_flags & IEEE80211_AGGR_SETUP,
1739234324Sadrian	    ("txa_flags 0x%x tid %d ac %d", tap->txa_flags, tap->txa_tid,
1740234324Sadrian	    TID_TO_WME_AC(tap->txa_tid)));
1741184280Ssam
1742184280Ssam	/*
1743184280Ssam	 * Stop BA stream if setup so driver has a chance
1744184280Ssam	 * to reclaim any resources it might have allocated.
1745184280Ssam	 */
1746184280Ssam	ic->ic_addba_stop(ni, tap);
1747184280Ssam	/*
1748184280Ssam	 * Stop any pending BAR transmit.
1749184280Ssam	 */
1750184280Ssam	bar_stop_timer(tap);
1751184280Ssam
1752280349Sadrian	/*
1753280349Sadrian	 * Reset packet estimate.
1754280349Sadrian	 */
1755280349Sadrian	tap->txa_lastsample = ticks;
1756184280Ssam	tap->txa_avgpps = 0;
1757280349Sadrian
1758184280Ssam	/* NB: clearing NAK means we may re-send ADDBA */
1759184280Ssam	tap->txa_flags &= ~(IEEE80211_AGGR_SETUP | IEEE80211_AGGR_NAK);
1760184280Ssam}
1761184280Ssam
1762223331Sadrian/*
1763223331Sadrian * ADDBA response timeout.
1764223331Sadrian *
1765223331Sadrian * If software aggregation and per-TID queue management was done here,
1766223331Sadrian * that queue would be unpaused after the ADDBA timeout occurs.
1767223331Sadrian */
1768184280Ssamstatic void
1769170530Ssamaddba_timeout(void *arg)
1770170530Ssam{
1771170530Ssam	struct ieee80211_tx_ampdu *tap = arg;
1772223331Sadrian	struct ieee80211_node *ni = tap->txa_ni;
1773223331Sadrian	struct ieee80211com *ic = ni->ni_ic;
1774170530Ssam
1775170530Ssam	/* XXX ? */
1776170530Ssam	tap->txa_flags &= ~IEEE80211_AGGR_XCHGPEND;
1777170530Ssam	tap->txa_attempts++;
1778223331Sadrian	ic->ic_addba_response_timeout(ni, tap);
1779170530Ssam}
1780170530Ssam
1781170530Ssamstatic void
1782170530Ssamaddba_start_timeout(struct ieee80211_tx_ampdu *tap)
1783170530Ssam{
1784170530Ssam	/* XXX use CALLOUT_PENDING instead? */
1785178354Ssam	callout_reset(&tap->txa_timer, ieee80211_addba_timeout,
1786170530Ssam	    addba_timeout, tap);
1787170530Ssam	tap->txa_flags |= IEEE80211_AGGR_XCHGPEND;
1788178354Ssam	tap->txa_nextrequest = ticks + ieee80211_addba_timeout;
1789170530Ssam}
1790170530Ssam
1791170530Ssamstatic void
1792170530Ssamaddba_stop_timeout(struct ieee80211_tx_ampdu *tap)
1793170530Ssam{
1794170530Ssam	/* XXX use CALLOUT_PENDING instead? */
1795170530Ssam	if (tap->txa_flags & IEEE80211_AGGR_XCHGPEND) {
1796170530Ssam		callout_stop(&tap->txa_timer);
1797170530Ssam		tap->txa_flags &= ~IEEE80211_AGGR_XCHGPEND;
1798170530Ssam	}
1799170530Ssam}
1800170530Ssam
1801223331Sadrianstatic void
1802223331Sadriannull_addba_response_timeout(struct ieee80211_node *ni,
1803223331Sadrian    struct ieee80211_tx_ampdu *tap)
1804223331Sadrian{
1805223331Sadrian}
1806223331Sadrian
1807170530Ssam/*
1808170530Ssam * Default method for requesting A-MPDU tx aggregation.
1809170530Ssam * We setup the specified state block and start a timer
1810170530Ssam * to wait for an ADDBA response frame.
1811170530Ssam */
1812170530Ssamstatic int
1813170530Ssamieee80211_addba_request(struct ieee80211_node *ni,
1814170530Ssam	struct ieee80211_tx_ampdu *tap,
1815170530Ssam	int dialogtoken, int baparamset, int batimeout)
1816170530Ssam{
1817170530Ssam	int bufsiz;
1818170530Ssam
1819170530Ssam	/* XXX locking */
1820170530Ssam	tap->txa_token = dialogtoken;
1821170530Ssam	tap->txa_flags |= IEEE80211_AGGR_IMMEDIATE;
1822170530Ssam	bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
1823170530Ssam	tap->txa_wnd = (bufsiz == 0) ?
1824170530Ssam	    IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
1825170530Ssam	addba_start_timeout(tap);
1826170530Ssam	return 1;
1827170530Ssam}
1828170530Ssam
1829170530Ssam/*
1830288085Sadrian * Called by drivers that wish to request an ADDBA session be
1831288085Sadrian * setup.  This brings it up and starts the request timer.
1832288085Sadrian */
1833288085Sadrianint
1834288085Sadrianieee80211_ampdu_tx_request_ext(struct ieee80211_node *ni, int tid)
1835288085Sadrian{
1836288085Sadrian	struct ieee80211_tx_ampdu *tap;
1837288085Sadrian
1838288085Sadrian	if (tid < 0 || tid > 15)
1839288085Sadrian		return (0);
1840288085Sadrian	tap = &ni->ni_tx_ampdu[tid];
1841288085Sadrian
1842288085Sadrian	/* XXX locking */
1843288085Sadrian	if ((tap->txa_flags & IEEE80211_AGGR_SETUP) == 0) {
1844288085Sadrian		/* do deferred setup of state */
1845288085Sadrian		ampdu_tx_setup(tap);
1846288085Sadrian	}
1847288085Sadrian	/* XXX hack for not doing proper locking */
1848288085Sadrian	tap->txa_flags &= ~IEEE80211_AGGR_NAK;
1849288085Sadrian	addba_start_timeout(tap);
1850288085Sadrian	return (1);
1851288085Sadrian}
1852288085Sadrian
1853288085Sadrian/*
1854288085Sadrian * Called by drivers that have marked a session as active.
1855288085Sadrian */
1856288085Sadrianint
1857288085Sadrianieee80211_ampdu_tx_request_active_ext(struct ieee80211_node *ni, int tid,
1858288085Sadrian    int status)
1859288085Sadrian{
1860288085Sadrian	struct ieee80211_tx_ampdu *tap;
1861288085Sadrian
1862288085Sadrian	if (tid < 0 || tid > 15)
1863288085Sadrian		return (0);
1864288085Sadrian	tap = &ni->ni_tx_ampdu[tid];
1865288085Sadrian
1866288085Sadrian	/* XXX locking */
1867288085Sadrian	addba_stop_timeout(tap);
1868288085Sadrian	if (status == 1) {
1869288085Sadrian		tap->txa_flags |= IEEE80211_AGGR_RUNNING;
1870288085Sadrian		tap->txa_attempts = 0;
1871288085Sadrian	} else {
1872288085Sadrian		/* mark tid so we don't try again */
1873288085Sadrian		tap->txa_flags |= IEEE80211_AGGR_NAK;
1874288085Sadrian	}
1875288085Sadrian	return (1);
1876288085Sadrian}
1877288085Sadrian
1878288085Sadrian/*
1879170530Ssam * Default method for processing an A-MPDU tx aggregation
1880170530Ssam * response.  We shutdown any pending timer and update the
1881170530Ssam * state block according to the reply.
1882170530Ssam */
1883170530Ssamstatic int
1884170530Ssamieee80211_addba_response(struct ieee80211_node *ni,
1885170530Ssam	struct ieee80211_tx_ampdu *tap,
1886170530Ssam	int status, int baparamset, int batimeout)
1887170530Ssam{
1888184280Ssam	int bufsiz, tid;
1889170530Ssam
1890170530Ssam	/* XXX locking */
1891170530Ssam	addba_stop_timeout(tap);
1892170530Ssam	if (status == IEEE80211_STATUS_SUCCESS) {
1893170530Ssam		bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
1894170530Ssam		/* XXX override our request? */
1895170530Ssam		tap->txa_wnd = (bufsiz == 0) ?
1896170530Ssam		    IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
1897184280Ssam		/* XXX AC/TID */
1898184280Ssam		tid = MS(baparamset, IEEE80211_BAPS_TID);
1899170530Ssam		tap->txa_flags |= IEEE80211_AGGR_RUNNING;
1900184280Ssam		tap->txa_attempts = 0;
1901173273Ssam	} else {
1902173273Ssam		/* mark tid so we don't try again */
1903173273Ssam		tap->txa_flags |= IEEE80211_AGGR_NAK;
1904170530Ssam	}
1905170530Ssam	return 1;
1906170530Ssam}
1907170530Ssam
1908170530Ssam/*
1909170530Ssam * Default method for stopping A-MPDU tx aggregation.
1910170530Ssam * Any timer is cleared and we drain any pending frames.
1911170530Ssam */
1912170530Ssamstatic void
1913170530Ssamieee80211_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
1914170530Ssam{
1915170530Ssam	/* XXX locking */
1916170530Ssam	addba_stop_timeout(tap);
1917170530Ssam	if (tap->txa_flags & IEEE80211_AGGR_RUNNING) {
1918182830Ssam		/* XXX clear aggregation queue */
1919170530Ssam		tap->txa_flags &= ~IEEE80211_AGGR_RUNNING;
1920170530Ssam	}
1921170530Ssam	tap->txa_attempts = 0;
1922170530Ssam}
1923170530Ssam
1924170530Ssam/*
1925170530Ssam * Process a received action frame using the default aggregation
1926170530Ssam * policy.  We intercept ADDBA-related frames and use them to
1927170530Ssam * update our aggregation state.  All other frames are passed up
1928170530Ssam * for processing by ieee80211_recv_action.
1929170530Ssam */
1930195377Ssamstatic int
1931195377Ssamht_recv_action_ba_addba_request(struct ieee80211_node *ni,
1932195377Ssam	const struct ieee80211_frame *wh,
1933170530Ssam	const uint8_t *frm, const uint8_t *efrm)
1934170530Ssam{
1935170530Ssam	struct ieee80211com *ic = ni->ni_ic;
1936178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1937170530Ssam	struct ieee80211_rx_ampdu *rap;
1938195377Ssam	uint8_t dialogtoken;
1939195377Ssam	uint16_t baparamset, batimeout, baseqctl;
1940205277Srpaulo	uint16_t args[5];
1941195377Ssam	int tid;
1942170530Ssam
1943195377Ssam	dialogtoken = frm[2];
1944195377Ssam	baparamset = LE_READ_2(frm+3);
1945195377Ssam	batimeout = LE_READ_2(frm+5);
1946195377Ssam	baseqctl = LE_READ_2(frm+7);
1947170530Ssam
1948195377Ssam	tid = MS(baparamset, IEEE80211_BAPS_TID);
1949170530Ssam
1950195377Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1951195377Ssam	    "recv ADDBA request: dialogtoken %u baparamset 0x%x "
1952195377Ssam	    "(tid %d bufsiz %d) batimeout %d baseqctl %d:%d",
1953195377Ssam	    dialogtoken, baparamset,
1954195377Ssam	    tid, MS(baparamset, IEEE80211_BAPS_BUFSIZ),
1955195377Ssam	    batimeout,
1956195377Ssam	    MS(baseqctl, IEEE80211_BASEQ_START),
1957195377Ssam	    MS(baseqctl, IEEE80211_BASEQ_FRAG));
1958170530Ssam
1959195377Ssam	rap = &ni->ni_rx_ampdu[tid];
1960170530Ssam
1961195377Ssam	/* Send ADDBA response */
1962195377Ssam	args[0] = dialogtoken;
1963195377Ssam	/*
1964195377Ssam	 * NB: We ack only if the sta associated with HT and
1965195377Ssam	 * the ap is configured to do AMPDU rx (the latter
1966195377Ssam	 * violates the 11n spec and is mostly for testing).
1967195377Ssam	 */
1968195377Ssam	if ((ni->ni_flags & IEEE80211_NODE_AMPDU_RX) &&
1969195377Ssam	    (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_RX)) {
1970195377Ssam		/* XXX handle ampdu_rx_start failure */
1971195377Ssam		ic->ic_ampdu_rx_start(ni, rap,
1972195377Ssam		    baparamset, batimeout, baseqctl);
1973170530Ssam
1974195377Ssam		args[1] = IEEE80211_STATUS_SUCCESS;
1975195377Ssam	} else {
1976195377Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1977195377Ssam		    ni, "reject ADDBA request: %s",
1978195377Ssam		    ni->ni_flags & IEEE80211_NODE_AMPDU_RX ?
1979195377Ssam		       "administratively disabled" :
1980195377Ssam		       "not negotiated for station");
1981195377Ssam		vap->iv_stats.is_addba_reject++;
1982195377Ssam		args[1] = IEEE80211_STATUS_UNSPECIFIED;
1983195377Ssam	}
1984195377Ssam	/* XXX honor rap flags? */
1985195377Ssam	args[2] = IEEE80211_BAPS_POLICY_IMMEDIATE
1986195377Ssam		| SM(tid, IEEE80211_BAPS_TID)
1987195377Ssam		| SM(rap->rxa_wnd, IEEE80211_BAPS_BUFSIZ)
1988195377Ssam		;
1989195377Ssam	args[3] = 0;
1990205277Srpaulo	args[4] = 0;
1991195377Ssam	ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
1992195377Ssam		IEEE80211_ACTION_BA_ADDBA_RESPONSE, args);
1993195377Ssam	return 0;
1994195377Ssam}
1995170530Ssam
1996195377Ssamstatic int
1997195377Ssamht_recv_action_ba_addba_response(struct ieee80211_node *ni,
1998195377Ssam	const struct ieee80211_frame *wh,
1999195377Ssam	const uint8_t *frm, const uint8_t *efrm)
2000195377Ssam{
2001195377Ssam	struct ieee80211com *ic = ni->ni_ic;
2002195377Ssam	struct ieee80211vap *vap = ni->ni_vap;
2003195377Ssam	struct ieee80211_tx_ampdu *tap;
2004195377Ssam	uint8_t dialogtoken, policy;
2005195377Ssam	uint16_t baparamset, batimeout, code;
2006234324Sadrian	int tid, bufsiz;
2007170530Ssam
2008195377Ssam	dialogtoken = frm[2];
2009195377Ssam	code = LE_READ_2(frm+3);
2010195377Ssam	baparamset = LE_READ_2(frm+5);
2011195377Ssam	tid = MS(baparamset, IEEE80211_BAPS_TID);
2012195377Ssam	bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
2013195377Ssam	policy = MS(baparamset, IEEE80211_BAPS_POLICY);
2014195377Ssam	batimeout = LE_READ_2(frm+7);
2015195377Ssam
2016234324Sadrian	tap = &ni->ni_tx_ampdu[tid];
2017195377Ssam	if ((tap->txa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
2018195377Ssam		IEEE80211_DISCARD_MAC(vap,
2019195377Ssam		    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2020195377Ssam		    ni->ni_macaddr, "ADDBA response",
2021195377Ssam		    "no pending ADDBA, tid %d dialogtoken %u "
2022195377Ssam		    "code %d", tid, dialogtoken, code);
2023195377Ssam		vap->iv_stats.is_addba_norequest++;
2024195377Ssam		return 0;
2025195377Ssam	}
2026195377Ssam	if (dialogtoken != tap->txa_token) {
2027195377Ssam		IEEE80211_DISCARD_MAC(vap,
2028195377Ssam		    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2029195377Ssam		    ni->ni_macaddr, "ADDBA response",
2030195377Ssam		    "dialogtoken mismatch: waiting for %d, "
2031195377Ssam		    "received %d, tid %d code %d",
2032195377Ssam		    tap->txa_token, dialogtoken, tid, code);
2033195377Ssam		vap->iv_stats.is_addba_badtoken++;
2034195377Ssam		return 0;
2035195377Ssam	}
2036195377Ssam	/* NB: assumes IEEE80211_AGGR_IMMEDIATE is 1 */
2037195377Ssam	if (policy != (tap->txa_flags & IEEE80211_AGGR_IMMEDIATE)) {
2038195377Ssam		IEEE80211_DISCARD_MAC(vap,
2039195377Ssam		    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2040195377Ssam		    ni->ni_macaddr, "ADDBA response",
2041195377Ssam		    "policy mismatch: expecting %s, "
2042195377Ssam		    "received %s, tid %d code %d",
2043195377Ssam		    tap->txa_flags & IEEE80211_AGGR_IMMEDIATE,
2044195377Ssam		    policy, tid, code);
2045195377Ssam		vap->iv_stats.is_addba_badpolicy++;
2046195377Ssam		return 0;
2047195377Ssam	}
2048182829Ssam#if 0
2049195377Ssam	/* XXX we take MIN in ieee80211_addba_response */
2050195377Ssam	if (bufsiz > IEEE80211_AGGR_BAWMAX) {
2051195377Ssam		IEEE80211_DISCARD_MAC(vap,
2052195377Ssam		    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2053195377Ssam		    ni->ni_macaddr, "ADDBA response",
2054195377Ssam		    "BA window too large: max %d, "
2055195377Ssam		    "received %d, tid %d code %d",
2056195377Ssam		    bufsiz, IEEE80211_AGGR_BAWMAX, tid, code);
2057195377Ssam		vap->iv_stats.is_addba_badbawinsize++;
2058195377Ssam		return 0;
2059195377Ssam	}
2060182829Ssam#endif
2061195377Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2062195377Ssam	    "recv ADDBA response: dialogtoken %u code %d "
2063195377Ssam	    "baparamset 0x%x (tid %d bufsiz %d) batimeout %d",
2064195377Ssam	    dialogtoken, code, baparamset, tid, bufsiz,
2065195377Ssam	    batimeout);
2066195377Ssam	ic->ic_addba_response(ni, tap, code, baparamset, batimeout);
2067195377Ssam	return 0;
2068195377Ssam}
2069170530Ssam
2070195377Ssamstatic int
2071195377Ssamht_recv_action_ba_delba(struct ieee80211_node *ni,
2072195377Ssam	const struct ieee80211_frame *wh,
2073195377Ssam	const uint8_t *frm, const uint8_t *efrm)
2074195377Ssam{
2075195377Ssam	struct ieee80211com *ic = ni->ni_ic;
2076195377Ssam	struct ieee80211_rx_ampdu *rap;
2077195377Ssam	struct ieee80211_tx_ampdu *tap;
2078195377Ssam	uint16_t baparamset, code;
2079234324Sadrian	int tid;
2080170530Ssam
2081195377Ssam	baparamset = LE_READ_2(frm+2);
2082195377Ssam	code = LE_READ_2(frm+4);
2083170530Ssam
2084195377Ssam	tid = MS(baparamset, IEEE80211_DELBAPS_TID);
2085170530Ssam
2086195377Ssam	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2087195377Ssam	    "recv DELBA: baparamset 0x%x (tid %d initiator %d) "
2088195377Ssam	    "code %d", baparamset, tid,
2089195377Ssam	    MS(baparamset, IEEE80211_DELBAPS_INIT), code);
2090195377Ssam
2091195377Ssam	if ((baparamset & IEEE80211_DELBAPS_INIT) == 0) {
2092234324Sadrian		tap = &ni->ni_tx_ampdu[tid];
2093195377Ssam		ic->ic_addba_stop(ni, tap);
2094195377Ssam	} else {
2095195377Ssam		rap = &ni->ni_rx_ampdu[tid];
2096195377Ssam		ic->ic_ampdu_rx_stop(ni, rap);
2097170530Ssam	}
2098195377Ssam	return 0;
2099170530Ssam}
2100170530Ssam
2101195377Ssamstatic int
2102195377Ssamht_recv_action_ht_txchwidth(struct ieee80211_node *ni,
2103195377Ssam	const struct ieee80211_frame *wh,
2104170530Ssam	const uint8_t *frm, const uint8_t *efrm)
2105170530Ssam{
2106170530Ssam	int chw;
2107170530Ssam
2108195377Ssam	chw = (frm[2] == IEEE80211_A_HT_TXCHWIDTH_2040) ? 40 : 20;
2109195377Ssam
2110195377Ssam	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2111195377Ssam	    "%s: HT txchwidth, width %d%s",
2112195377Ssam	    __func__, chw, ni->ni_chw != chw ? "*" : "");
2113195377Ssam	if (chw != ni->ni_chw) {
2114195377Ssam		ni->ni_chw = chw;
2115195377Ssam		/* XXX notify on change */
2116170530Ssam	}
2117195377Ssam	return 0;
2118170530Ssam}
2119170530Ssam
2120195377Ssamstatic int
2121195377Ssamht_recv_action_ht_mimopwrsave(struct ieee80211_node *ni,
2122195377Ssam	const struct ieee80211_frame *wh,
2123195377Ssam	const uint8_t *frm, const uint8_t *efrm)
2124195377Ssam{
2125195377Ssam	const struct ieee80211_action_ht_mimopowersave *mps =
2126195377Ssam	    (const struct ieee80211_action_ht_mimopowersave *) frm;
2127195377Ssam
2128195377Ssam	/* XXX check iv_htcaps */
2129195377Ssam	if (mps->am_control & IEEE80211_A_HT_MIMOPWRSAVE_ENA)
2130195377Ssam		ni->ni_flags |= IEEE80211_NODE_MIMO_PS;
2131195377Ssam	else
2132195377Ssam		ni->ni_flags &= ~IEEE80211_NODE_MIMO_PS;
2133195377Ssam	if (mps->am_control & IEEE80211_A_HT_MIMOPWRSAVE_MODE)
2134195377Ssam		ni->ni_flags |= IEEE80211_NODE_MIMO_RTS;
2135195377Ssam	else
2136195377Ssam		ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS;
2137195377Ssam	/* XXX notify on change */
2138195377Ssam	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2139195377Ssam	    "%s: HT MIMO PS (%s%s)", __func__,
2140195377Ssam	    (ni->ni_flags & IEEE80211_NODE_MIMO_PS) ?  "on" : "off",
2141195377Ssam	    (ni->ni_flags & IEEE80211_NODE_MIMO_RTS) ?  "+rts" : ""
2142195377Ssam	);
2143195377Ssam	return 0;
2144195377Ssam}
2145195377Ssam
2146170530Ssam/*
2147170530Ssam * Transmit processing.
2148170530Ssam */
2149170530Ssam
2150170530Ssam/*
2151178354Ssam * Check if A-MPDU should be requested/enabled for a stream.
2152178354Ssam * We require a traffic rate above a per-AC threshold and we
2153178354Ssam * also handle backoff from previous failed attempts.
2154178354Ssam *
2155178354Ssam * Drivers may override this method to bring in information
2156178354Ssam * such as link state conditions in making the decision.
2157178354Ssam */
2158178354Ssamstatic int
2159178354Ssamieee80211_ampdu_enable(struct ieee80211_node *ni,
2160178354Ssam	struct ieee80211_tx_ampdu *tap)
2161178354Ssam{
2162178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2163178354Ssam
2164234324Sadrian	if (tap->txa_avgpps <
2165234324Sadrian	    vap->iv_ampdu_mintraffic[TID_TO_WME_AC(tap->txa_tid)])
2166178354Ssam		return 0;
2167178354Ssam	/* XXX check rssi? */
2168178354Ssam	if (tap->txa_attempts >= ieee80211_addba_maxtries &&
2169178354Ssam	    ticks < tap->txa_nextrequest) {
2170178354Ssam		/*
2171178354Ssam		 * Don't retry too often; txa_nextrequest is set
2172178354Ssam		 * to the minimum interval we'll retry after
2173178354Ssam		 * ieee80211_addba_maxtries failed attempts are made.
2174178354Ssam		 */
2175178354Ssam		return 0;
2176178354Ssam	}
2177178354Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
2178234324Sadrian	    "enable AMPDU on tid %d (%s), avgpps %d pkts %d",
2179234324Sadrian	    tap->txa_tid, ieee80211_wme_acnames[TID_TO_WME_AC(tap->txa_tid)],
2180234324Sadrian	    tap->txa_avgpps, tap->txa_pkts);
2181178354Ssam	return 1;
2182178354Ssam}
2183178354Ssam
2184178354Ssam/*
2185170530Ssam * Request A-MPDU tx aggregation.  Setup local state and
2186170530Ssam * issue an ADDBA request.  BA use will only happen after
2187170530Ssam * the other end replies with ADDBA response.
2188170530Ssam */
2189170530Ssamint
2190170530Ssamieee80211_ampdu_request(struct ieee80211_node *ni,
2191170530Ssam	struct ieee80211_tx_ampdu *tap)
2192170530Ssam{
2193170530Ssam	struct ieee80211com *ic = ni->ni_ic;
2194205277Srpaulo	uint16_t args[5];
2195170530Ssam	int tid, dialogtoken;
2196170530Ssam	static int tokens = 0;	/* XXX */
2197170530Ssam
2198170530Ssam	/* XXX locking */
2199170530Ssam	if ((tap->txa_flags & IEEE80211_AGGR_SETUP) == 0) {
2200170530Ssam		/* do deferred setup of state */
2201184280Ssam		ampdu_tx_setup(tap);
2202170530Ssam	}
2203173273Ssam	/* XXX hack for not doing proper locking */
2204173273Ssam	tap->txa_flags &= ~IEEE80211_AGGR_NAK;
2205173273Ssam
2206170530Ssam	dialogtoken = (tokens+1) % 63;		/* XXX */
2207234324Sadrian	tid = tap->txa_tid;
2208183245Ssam	tap->txa_start = ni->ni_txseqs[tid];
2209170530Ssam
2210170530Ssam	args[0] = dialogtoken;
2211205277Srpaulo	args[1] = 0;	/* NB: status code not used */
2212205277Srpaulo	args[2]	= IEEE80211_BAPS_POLICY_IMMEDIATE
2213170530Ssam		| SM(tid, IEEE80211_BAPS_TID)
2214170530Ssam		| SM(IEEE80211_AGGR_BAWMAX, IEEE80211_BAPS_BUFSIZ)
2215170530Ssam		;
2216205277Srpaulo	args[3] = 0;	/* batimeout */
2217170530Ssam	/* NB: do first so there's no race against reply */
2218205277Srpaulo	if (!ic->ic_addba_request(ni, tap, dialogtoken, args[2], args[3])) {
2219170530Ssam		/* unable to setup state, don't make request */
2220178354Ssam		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2221234324Sadrian		    ni, "%s: could not setup BA stream for TID %d AC %d",
2222234324Sadrian		    __func__, tap->txa_tid, TID_TO_WME_AC(tap->txa_tid));
2223173273Ssam		/* defer next try so we don't slam the driver with requests */
2224178354Ssam		tap->txa_attempts = ieee80211_addba_maxtries;
2225178354Ssam		/* NB: check in case driver wants to override */
2226178354Ssam		if (tap->txa_nextrequest <= ticks)
2227178354Ssam			tap->txa_nextrequest = ticks + ieee80211_addba_backoff;
2228170530Ssam		return 0;
2229170530Ssam	}
2230170530Ssam	tokens = dialogtoken;			/* allocate token */
2231183245Ssam	/* NB: after calling ic_addba_request so driver can set txa_start */
2232205277Srpaulo	args[4] = SM(tap->txa_start, IEEE80211_BASEQ_START)
2233178953Ssam		| SM(0, IEEE80211_BASEQ_FRAG)
2234178953Ssam		;
2235170530Ssam	return ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
2236170530Ssam		IEEE80211_ACTION_BA_ADDBA_REQUEST, args);
2237170530Ssam}
2238170530Ssam
2239170530Ssam/*
2240173273Ssam * Terminate an AMPDU tx stream.  State is reclaimed
2241173273Ssam * and the peer notified with a DelBA Action frame.
2242173273Ssam */
2243173273Ssamvoid
2244183250Ssamieee80211_ampdu_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
2245183250Ssam	int reason)
2246173273Ssam{
2247173273Ssam	struct ieee80211com *ic = ni->ni_ic;
2248178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2249173273Ssam	uint16_t args[4];
2250173273Ssam
2251173273Ssam	/* XXX locking */
2252184280Ssam	tap->txa_flags &= ~IEEE80211_AGGR_BARPEND;
2253173273Ssam	if (IEEE80211_AMPDU_RUNNING(tap)) {
2254178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2255234324Sadrian		    ni, "%s: stop BA stream for TID %d (reason %d)",
2256234324Sadrian		    __func__, tap->txa_tid, reason);
2257178354Ssam		vap->iv_stats.is_ampdu_stop++;
2258173273Ssam
2259173273Ssam		ic->ic_addba_stop(ni, tap);
2260234324Sadrian		args[0] = tap->txa_tid;
2261173273Ssam		args[1] = IEEE80211_DELBAPS_INIT;
2262183250Ssam		args[2] = reason;			/* XXX reason code */
2263195377Ssam		ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
2264173273Ssam			IEEE80211_ACTION_BA_DELBA, args);
2265173273Ssam	} else {
2266178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2267234324Sadrian		    ni, "%s: BA stream for TID %d not running (reason %d)",
2268234324Sadrian		    __func__, tap->txa_tid, reason);
2269178354Ssam		vap->iv_stats.is_ampdu_stop_failed++;
2270173273Ssam	}
2271173273Ssam}
2272173273Ssam
2273235801Sadrian/* XXX */
2274235801Sadrianstatic void bar_start_timer(struct ieee80211_tx_ampdu *tap);
2275235801Sadrian
2276184280Ssamstatic void
2277184280Ssambar_timeout(void *arg)
2278184280Ssam{
2279184280Ssam	struct ieee80211_tx_ampdu *tap = arg;
2280184280Ssam	struct ieee80211_node *ni = tap->txa_ni;
2281184280Ssam
2282184280Ssam	KASSERT((tap->txa_flags & IEEE80211_AGGR_XCHGPEND) == 0,
2283184280Ssam	    ("bar/addba collision, flags 0x%x", tap->txa_flags));
2284184280Ssam
2285184280Ssam	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2286184280Ssam	    ni, "%s: tid %u flags 0x%x attempts %d", __func__,
2287234324Sadrian	    tap->txa_tid, tap->txa_flags, tap->txa_attempts);
2288184280Ssam
2289184280Ssam	/* guard against race with bar_tx_complete */
2290184280Ssam	if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) == 0)
2291184280Ssam		return;
2292184280Ssam	/* XXX ? */
2293234018Sadrian	if (tap->txa_attempts >= ieee80211_bar_maxtries) {
2294235801Sadrian		struct ieee80211com *ic = ni->ni_ic;
2295235801Sadrian
2296234018Sadrian		ni->ni_vap->iv_stats.is_ampdu_bar_tx_fail++;
2297235801Sadrian		/*
2298235801Sadrian		 * If (at least) the last BAR TX timeout was due to
2299235801Sadrian		 * an ieee80211_send_bar() failures, then we need
2300235801Sadrian		 * to make sure we notify the driver that a BAR
2301235801Sadrian		 * TX did occur and fail.  This gives the driver
2302235801Sadrian		 * a chance to undo any queue pause that may
2303235801Sadrian		 * have occured.
2304235801Sadrian		 */
2305235801Sadrian		ic->ic_bar_response(ni, tap, 1);
2306184280Ssam		ieee80211_ampdu_stop(ni, tap, IEEE80211_REASON_TIMEOUT);
2307234018Sadrian	} else {
2308234018Sadrian		ni->ni_vap->iv_stats.is_ampdu_bar_tx_retry++;
2309235801Sadrian		if (ieee80211_send_bar(ni, tap, tap->txa_seqpending) != 0) {
2310242205Sadrian			IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2311242205Sadrian			    ni, "%s: failed to TX, starting timer\n",
2312242205Sadrian			    __func__);
2313235801Sadrian			/*
2314235801Sadrian			 * If ieee80211_send_bar() fails here, the
2315235801Sadrian			 * timer may have stopped and/or the pending
2316235801Sadrian			 * flag may be clear.  Because of this,
2317235801Sadrian			 * fake the BARPEND and reset the timer.
2318235801Sadrian			 * A retransmission attempt will then occur
2319235801Sadrian			 * during the next timeout.
2320235801Sadrian			 */
2321235801Sadrian			/* XXX locking */
2322235801Sadrian			tap->txa_flags |= IEEE80211_AGGR_BARPEND;
2323235801Sadrian			bar_start_timer(tap);
2324235801Sadrian		}
2325234018Sadrian	}
2326184280Ssam}
2327184280Ssam
2328184280Ssamstatic void
2329184280Ssambar_start_timer(struct ieee80211_tx_ampdu *tap)
2330184280Ssam{
2331242205Sadrian	IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2332242205Sadrian	    tap->txa_ni,
2333242205Sadrian	    "%s: called",
2334242205Sadrian	    __func__);
2335184280Ssam	callout_reset(&tap->txa_timer, ieee80211_bar_timeout, bar_timeout, tap);
2336184280Ssam}
2337184280Ssam
2338184280Ssamstatic void
2339184280Ssambar_stop_timer(struct ieee80211_tx_ampdu *tap)
2340184280Ssam{
2341242205Sadrian	IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2342242205Sadrian	    tap->txa_ni,
2343242205Sadrian	    "%s: called",
2344242205Sadrian	    __func__);
2345184280Ssam	callout_stop(&tap->txa_timer);
2346184280Ssam}
2347184280Ssam
2348184280Ssamstatic void
2349184280Ssambar_tx_complete(struct ieee80211_node *ni, void *arg, int status)
2350184280Ssam{
2351184280Ssam	struct ieee80211_tx_ampdu *tap = arg;
2352184280Ssam
2353184280Ssam	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2354184280Ssam	    ni, "%s: tid %u flags 0x%x pending %d status %d",
2355234324Sadrian	    __func__, tap->txa_tid, tap->txa_flags,
2356184280Ssam	    callout_pending(&tap->txa_timer), status);
2357184280Ssam
2358234018Sadrian	ni->ni_vap->iv_stats.is_ampdu_bar_tx++;
2359184280Ssam	/* XXX locking */
2360184280Ssam	if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) &&
2361184280Ssam	    callout_pending(&tap->txa_timer)) {
2362184280Ssam		struct ieee80211com *ic = ni->ni_ic;
2363184280Ssam
2364224907Sadrian		if (status == 0)		/* ACK'd */
2365184280Ssam			bar_stop_timer(tap);
2366184280Ssam		ic->ic_bar_response(ni, tap, status);
2367184280Ssam		/* NB: just let timer expire so we pace requests */
2368184280Ssam	}
2369184280Ssam}
2370184280Ssam
2371184280Ssamstatic void
2372184280Ssamieee80211_bar_response(struct ieee80211_node *ni,
2373184280Ssam	struct ieee80211_tx_ampdu *tap, int status)
2374184280Ssam{
2375184280Ssam
2376242205Sadrian	IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2377242205Sadrian	    tap->txa_ni,
2378242205Sadrian	    "%s: called",
2379242205Sadrian	    __func__);
2380224907Sadrian	if (status == 0) {		/* got ACK */
2381184280Ssam		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2382184280Ssam		    ni, "BAR moves BA win <%u:%u> (%u frames) txseq %u tid %u",
2383184280Ssam		    tap->txa_start,
2384184280Ssam		    IEEE80211_SEQ_ADD(tap->txa_start, tap->txa_wnd-1),
2385184280Ssam		    tap->txa_qframes, tap->txa_seqpending,
2386234324Sadrian		    tap->txa_tid);
2387184280Ssam
2388184280Ssam		/* NB: timer already stopped in bar_tx_complete */
2389184280Ssam		tap->txa_start = tap->txa_seqpending;
2390184280Ssam		tap->txa_flags &= ~IEEE80211_AGGR_BARPEND;
2391184280Ssam	}
2392184280Ssam}
2393184280Ssam
2394173273Ssam/*
2395170530Ssam * Transmit a BAR frame to the specified node.  The
2396170530Ssam * BAR contents are drawn from the supplied aggregation
2397170530Ssam * state associated with the node.
2398184280Ssam *
2399184280Ssam * NB: we only handle immediate ACK w/ compressed bitmap.
2400170530Ssam */
2401170530Ssamint
2402170530Ssamieee80211_send_bar(struct ieee80211_node *ni,
2403184280Ssam	struct ieee80211_tx_ampdu *tap, ieee80211_seq seq)
2404170530Ssam{
2405178354Ssam#define	senderr(_x, _v)	do { vap->iv_stats._v++; ret = _x; goto bad; } while (0)
2406178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2407170530Ssam	struct ieee80211com *ic = ni->ni_ic;
2408184280Ssam	struct ieee80211_frame_bar *bar;
2409170530Ssam	struct mbuf *m;
2410184280Ssam	uint16_t barctl, barseqctl;
2411170530Ssam	uint8_t *frm;
2412170530Ssam	int tid, ret;
2413170530Ssam
2414242205Sadrian
2415242205Sadrian	IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2416242205Sadrian	    tap->txa_ni,
2417242205Sadrian	    "%s: called",
2418242205Sadrian	    __func__);
2419242205Sadrian
2420184280Ssam	if ((tap->txa_flags & IEEE80211_AGGR_RUNNING) == 0) {
2421184280Ssam		/* no ADDBA response, should not happen */
2422184280Ssam		/* XXX stat+msg */
2423184280Ssam		return EINVAL;
2424184280Ssam	}
2425184280Ssam	/* XXX locking */
2426184280Ssam	bar_stop_timer(tap);
2427184280Ssam
2428170530Ssam	ieee80211_ref_node(ni);
2429170530Ssam
2430184280Ssam	m = ieee80211_getmgtframe(&frm, ic->ic_headroom, sizeof(*bar));
2431170530Ssam	if (m == NULL)
2432170530Ssam		senderr(ENOMEM, is_tx_nobuf);
2433170530Ssam
2434184280Ssam	if (!ieee80211_add_callback(m, bar_tx_complete, tap)) {
2435184280Ssam		m_freem(m);
2436184280Ssam		senderr(ENOMEM, is_tx_nobuf);	/* XXX */
2437184280Ssam		/* NOTREACHED */
2438184280Ssam	}
2439184280Ssam
2440184280Ssam	bar = mtod(m, struct ieee80211_frame_bar *);
2441184280Ssam	bar->i_fc[0] = IEEE80211_FC0_VERSION_0 |
2442170530Ssam		IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_BAR;
2443184280Ssam	bar->i_fc[1] = 0;
2444184280Ssam	IEEE80211_ADDR_COPY(bar->i_ra, ni->ni_macaddr);
2445184280Ssam	IEEE80211_ADDR_COPY(bar->i_ta, vap->iv_myaddr);
2446170530Ssam
2447234324Sadrian	tid = tap->txa_tid;
2448170530Ssam	barctl 	= (tap->txa_flags & IEEE80211_AGGR_IMMEDIATE ?
2449184280Ssam			0 : IEEE80211_BAR_NOACK)
2450184280Ssam		| IEEE80211_BAR_COMP
2451184280Ssam		| SM(tid, IEEE80211_BAR_TID)
2452170530Ssam		;
2453184280Ssam	barseqctl = SM(seq, IEEE80211_BAR_SEQ_START);
2454184280Ssam	/* NB: known to have proper alignment */
2455184280Ssam	bar->i_ctl = htole16(barctl);
2456184280Ssam	bar->i_seq = htole16(barseqctl);
2457184280Ssam	m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_bar);
2458170530Ssam
2459178354Ssam	M_WME_SETAC(m, WME_AC_VO);
2460178354Ssam
2461170530Ssam	IEEE80211_NODE_STAT(ni, tx_mgmt);	/* XXX tx_ctl? */
2462170530Ssam
2463184280Ssam	/* XXX locking */
2464184280Ssam	/* init/bump attempts counter */
2465184280Ssam	if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) == 0)
2466184280Ssam		tap->txa_attempts = 1;
2467184280Ssam	else
2468184280Ssam		tap->txa_attempts++;
2469184280Ssam	tap->txa_seqpending = seq;
2470184280Ssam	tap->txa_flags |= IEEE80211_AGGR_BARPEND;
2471170530Ssam
2472184280Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_11N,
2473184280Ssam	    ni, "send BAR: tid %u ctl 0x%x start %u (attempt %d)",
2474184280Ssam	    tid, barctl, seq, tap->txa_attempts);
2475184280Ssam
2476225013Sadrian	/*
2477225013Sadrian	 * ic_raw_xmit will free the node reference
2478225013Sadrian	 * regardless of queue/TX success or failure.
2479225013Sadrian	 */
2480248069Sadrian	IEEE80211_TX_LOCK(ic);
2481248069Sadrian	ret = ieee80211_raw_output(vap, ni, m, NULL);
2482248069Sadrian	IEEE80211_TX_UNLOCK(ic);
2483184280Ssam	if (ret != 0) {
2484242205Sadrian		IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_11N,
2485242205Sadrian		    ni, "send BAR: failed: (ret = %d)\n",
2486242205Sadrian		    ret);
2487184280Ssam		/* xmit failed, clear state flag */
2488184280Ssam		tap->txa_flags &= ~IEEE80211_AGGR_BARPEND;
2489234018Sadrian		vap->iv_stats.is_ampdu_bar_tx_fail++;
2490225013Sadrian		return ret;
2491184280Ssam	}
2492184280Ssam	/* XXX hack against tx complete happening before timer is started */
2493184280Ssam	if (tap->txa_flags & IEEE80211_AGGR_BARPEND)
2494184280Ssam		bar_start_timer(tap);
2495184280Ssam	return 0;
2496170530Ssambad:
2497242205Sadrian	IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2498242205Sadrian	    tap->txa_ni,
2499242205Sadrian	    "%s: bad! ret=%d",
2500242205Sadrian	    __func__, ret);
2501234018Sadrian	vap->iv_stats.is_ampdu_bar_tx_fail++;
2502170530Ssam	ieee80211_free_node(ni);
2503170530Ssam	return ret;
2504170530Ssam#undef senderr
2505170530Ssam}
2506170530Ssam
2507195377Ssamstatic int
2508195377Ssamht_action_output(struct ieee80211_node *ni, struct mbuf *m)
2509195377Ssam{
2510195377Ssam	struct ieee80211_bpf_params params;
2511195377Ssam
2512195377Ssam	memset(&params, 0, sizeof(params));
2513195377Ssam	params.ibp_pri = WME_AC_VO;
2514195377Ssam	params.ibp_rate0 = ni->ni_txparms->mgmtrate;
2515195377Ssam	/* NB: we know all frames are unicast */
2516195377Ssam	params.ibp_try0 = ni->ni_txparms->maxretry;
2517195377Ssam	params.ibp_power = ni->ni_txpower;
2518195377Ssam	return ieee80211_mgmt_output(ni, m, IEEE80211_FC0_SUBTYPE_ACTION,
2519195377Ssam	     &params);
2520195377Ssam}
2521195377Ssam
2522195377Ssam#define	ADDSHORT(frm, v) do {			\
2523195377Ssam	frm[0] = (v) & 0xff;			\
2524195377Ssam	frm[1] = (v) >> 8;			\
2525195377Ssam	frm += 2;				\
2526195377Ssam} while (0)
2527195377Ssam
2528170530Ssam/*
2529170530Ssam * Send an action management frame.  The arguments are stuff
2530170530Ssam * into a frame without inspection; the caller is assumed to
2531170530Ssam * prepare them carefully (e.g. based on the aggregation state).
2532170530Ssam */
2533195377Ssamstatic int
2534195377Ssamht_send_action_ba_addba(struct ieee80211_node *ni,
2535195377Ssam	int category, int action, void *arg0)
2536170530Ssam{
2537178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2538170530Ssam	struct ieee80211com *ic = ni->ni_ic;
2539195377Ssam	uint16_t *args = arg0;
2540170530Ssam	struct mbuf *m;
2541170530Ssam	uint8_t *frm;
2542195377Ssam
2543195377Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2544205277Srpaulo	    "send ADDBA %s: dialogtoken %d status %d "
2545195377Ssam	    "baparamset 0x%x (tid %d) batimeout 0x%x baseqctl 0x%x",
2546195377Ssam	    (action == IEEE80211_ACTION_BA_ADDBA_REQUEST) ?
2547195377Ssam		"request" : "response",
2548205277Srpaulo	    args[0], args[1], args[2], MS(args[2], IEEE80211_BAPS_TID),
2549205277Srpaulo	    args[3], args[4]);
2550195377Ssam
2551195377Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2552195377Ssam	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2553195377Ssam	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2554195377Ssam	ieee80211_ref_node(ni);
2555195377Ssam
2556195377Ssam	m = ieee80211_getmgtframe(&frm,
2557195377Ssam	    ic->ic_headroom + sizeof(struct ieee80211_frame),
2558195377Ssam	    sizeof(uint16_t)	/* action+category */
2559195377Ssam	    /* XXX may action payload */
2560195377Ssam	    + sizeof(struct ieee80211_action_ba_addbaresponse)
2561195377Ssam	);
2562195377Ssam	if (m != NULL) {
2563195377Ssam		*frm++ = category;
2564195377Ssam		*frm++ = action;
2565195377Ssam		*frm++ = args[0];		/* dialog token */
2566205277Srpaulo		if (action == IEEE80211_ACTION_BA_ADDBA_RESPONSE)
2567205277Srpaulo			ADDSHORT(frm, args[1]);	/* status code */
2568205277Srpaulo		ADDSHORT(frm, args[2]);		/* baparamset */
2569205277Srpaulo		ADDSHORT(frm, args[3]);		/* batimeout */
2570195377Ssam		if (action == IEEE80211_ACTION_BA_ADDBA_REQUEST)
2571205277Srpaulo			ADDSHORT(frm, args[4]);	/* baseqctl */
2572195377Ssam		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2573195377Ssam		return ht_action_output(ni, m);
2574195377Ssam	} else {
2575195377Ssam		vap->iv_stats.is_tx_nobuf++;
2576195377Ssam		ieee80211_free_node(ni);
2577195377Ssam		return ENOMEM;
2578195377Ssam	}
2579195377Ssam}
2580195377Ssam
2581195377Ssamstatic int
2582195377Ssamht_send_action_ba_delba(struct ieee80211_node *ni,
2583195377Ssam	int category, int action, void *arg0)
2584195377Ssam{
2585195377Ssam	struct ieee80211vap *vap = ni->ni_vap;
2586195377Ssam	struct ieee80211com *ic = ni->ni_ic;
2587195377Ssam	uint16_t *args = arg0;
2588195377Ssam	struct mbuf *m;
2589170530Ssam	uint16_t baparamset;
2590195377Ssam	uint8_t *frm;
2591170530Ssam
2592195377Ssam	baparamset = SM(args[0], IEEE80211_DELBAPS_TID)
2593195377Ssam		   | args[1]
2594195377Ssam		   ;
2595195377Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2596195377Ssam	    "send DELBA action: tid %d, initiator %d reason %d",
2597195377Ssam	    args[0], args[1], args[2]);
2598170530Ssam
2599178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2600195377Ssam	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2601195377Ssam	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2602170530Ssam	ieee80211_ref_node(ni);
2603170530Ssam
2604170530Ssam	m = ieee80211_getmgtframe(&frm,
2605195377Ssam	    ic->ic_headroom + sizeof(struct ieee80211_frame),
2606195377Ssam	    sizeof(uint16_t)	/* action+category */
2607195377Ssam	    /* XXX may action payload */
2608195377Ssam	    + sizeof(struct ieee80211_action_ba_addbaresponse)
2609170530Ssam	);
2610195377Ssam	if (m != NULL) {
2611195377Ssam		*frm++ = category;
2612195377Ssam		*frm++ = action;
2613195377Ssam		ADDSHORT(frm, baparamset);
2614195377Ssam		ADDSHORT(frm, args[2]);		/* reason code */
2615195377Ssam		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2616195377Ssam		return ht_action_output(ni, m);
2617195377Ssam	} else {
2618195377Ssam		vap->iv_stats.is_tx_nobuf++;
2619195377Ssam		ieee80211_free_node(ni);
2620195377Ssam		return ENOMEM;
2621195377Ssam	}
2622195377Ssam}
2623170530Ssam
2624195377Ssamstatic int
2625195377Ssamht_send_action_ht_txchwidth(struct ieee80211_node *ni,
2626195377Ssam	int category, int action, void *arg0)
2627195377Ssam{
2628195377Ssam	struct ieee80211vap *vap = ni->ni_vap;
2629195377Ssam	struct ieee80211com *ic = ni->ni_ic;
2630195377Ssam	struct mbuf *m;
2631195377Ssam	uint8_t *frm;
2632170530Ssam
2633195377Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2634195377Ssam	    "send HT txchwidth: width %d",
2635195377Ssam	    IEEE80211_IS_CHAN_HT40(ni->ni_chan) ? 40 : 20);
2636170530Ssam
2637195377Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2638195377Ssam	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2639195377Ssam	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2640195377Ssam	ieee80211_ref_node(ni);
2641170530Ssam
2642195377Ssam	m = ieee80211_getmgtframe(&frm,
2643195377Ssam	    ic->ic_headroom + sizeof(struct ieee80211_frame),
2644195377Ssam	    sizeof(uint16_t)	/* action+category */
2645195377Ssam	    /* XXX may action payload */
2646195377Ssam	    + sizeof(struct ieee80211_action_ba_addbaresponse)
2647195377Ssam	);
2648195377Ssam	if (m != NULL) {
2649195377Ssam		*frm++ = category;
2650195377Ssam		*frm++ = action;
2651195377Ssam		*frm++ = IEEE80211_IS_CHAN_HT40(ni->ni_chan) ?
2652195377Ssam			IEEE80211_A_HT_TXCHWIDTH_2040 :
2653195377Ssam			IEEE80211_A_HT_TXCHWIDTH_20;
2654195377Ssam		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2655195377Ssam		return ht_action_output(ni, m);
2656195377Ssam	} else {
2657195377Ssam		vap->iv_stats.is_tx_nobuf++;
2658195377Ssam		ieee80211_free_node(ni);
2659195377Ssam		return ENOMEM;
2660170530Ssam	}
2661195377Ssam}
2662170530Ssam#undef ADDSHORT
2663170530Ssam
2664170530Ssam/*
2665219600Sbschmidt * Construct the MCS bit mask for inclusion in an HT capabilities
2666219600Sbschmidt * information element.
2667170530Ssam */
2668219600Sbschmidtstatic void
2669219600Sbschmidtieee80211_set_mcsset(struct ieee80211com *ic, uint8_t *frm)
2670170530Ssam{
2671170530Ssam	int i;
2672219600Sbschmidt	uint8_t txparams;
2673170530Ssam
2674219600Sbschmidt	KASSERT((ic->ic_rxstream > 0 && ic->ic_rxstream <= 4),
2675219600Sbschmidt	    ("ic_rxstream %d out of range", ic->ic_rxstream));
2676219600Sbschmidt	KASSERT((ic->ic_txstream > 0 && ic->ic_txstream <= 4),
2677219600Sbschmidt	    ("ic_txstream %d out of range", ic->ic_txstream));
2678219600Sbschmidt
2679219600Sbschmidt	for (i = 0; i < ic->ic_rxstream * 8; i++)
2680219600Sbschmidt		setbit(frm, i);
2681219600Sbschmidt	if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
2682219600Sbschmidt	    (ic->ic_htcaps & IEEE80211_HTC_RXMCS32))
2683219600Sbschmidt		setbit(frm, 32);
2684219600Sbschmidt	if (ic->ic_htcaps & IEEE80211_HTC_RXUNEQUAL) {
2685219600Sbschmidt		if (ic->ic_rxstream >= 2) {
2686219600Sbschmidt			for (i = 33; i <= 38; i++)
2687219600Sbschmidt				setbit(frm, i);
2688170530Ssam		}
2689219600Sbschmidt		if (ic->ic_rxstream >= 3) {
2690219600Sbschmidt			for (i = 39; i <= 52; i++)
2691219600Sbschmidt				setbit(frm, i);
2692219600Sbschmidt		}
2693219600Sbschmidt		if (ic->ic_txstream >= 4) {
2694219600Sbschmidt			for (i = 53; i <= 76; i++)
2695219600Sbschmidt				setbit(frm, i);
2696219600Sbschmidt		}
2697170530Ssam	}
2698219600Sbschmidt
2699219600Sbschmidt	if (ic->ic_rxstream != ic->ic_txstream) {
2700219600Sbschmidt		txparams = 0x1;			/* TX MCS set defined */
2701219600Sbschmidt		txparams |= 0x2;		/* TX RX MCS not equal */
2702219600Sbschmidt		txparams |= (ic->ic_txstream - 1) << 2;	/* num TX streams */
2703219600Sbschmidt		if (ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL)
2704219600Sbschmidt			txparams |= 0x16;	/* TX unequal modulation sup */
2705219600Sbschmidt	} else
2706219600Sbschmidt		txparams = 0;
2707219600Sbschmidt	frm[12] = txparams;
2708170530Ssam}
2709170530Ssam
2710170530Ssam/*
2711170530Ssam * Add body of an HTCAP information element.
2712170530Ssam */
2713170530Ssamstatic uint8_t *
2714170530Ssamieee80211_add_htcap_body(uint8_t *frm, struct ieee80211_node *ni)
2715170530Ssam{
2716170530Ssam#define	ADDSHORT(frm, v) do {			\
2717170530Ssam	frm[0] = (v) & 0xff;			\
2718170530Ssam	frm[1] = (v) >> 8;			\
2719170530Ssam	frm += 2;				\
2720170530Ssam} while (0)
2721222683Sbschmidt	struct ieee80211com *ic = ni->ni_ic;
2722178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2723205513Srpaulo	uint16_t caps, extcaps;
2724173865Ssam	int rxmax, density;
2725170530Ssam
2726170530Ssam	/* HT capabilities */
2727178354Ssam	caps = vap->iv_htcaps & 0xffff;
2728173273Ssam	/*
2729173273Ssam	 * Note channel width depends on whether we are operating as
2730173273Ssam	 * a sta or not.  When operating as a sta we are generating
2731173273Ssam	 * a request based on our desired configuration.  Otherwise
2732173273Ssam	 * we are operational and the channel attributes identify
2733173273Ssam	 * how we've been setup (which might be different if a fixed
2734173273Ssam	 * channel is specified).
2735173273Ssam	 */
2736178354Ssam	if (vap->iv_opmode == IEEE80211_M_STA) {
2737173273Ssam		/* override 20/40 use based on config */
2738193655Ssam		if (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)
2739173273Ssam			caps |= IEEE80211_HTCAP_CHWIDTH40;
2740173273Ssam		else
2741173273Ssam			caps &= ~IEEE80211_HTCAP_CHWIDTH40;
2742282704Sadrian
2743282704Sadrian		/* Start by using the advertised settings */
2744173865Ssam		rxmax = MS(ni->ni_htparam, IEEE80211_HTCAP_MAXRXAMPDU);
2745173865Ssam		density = MS(ni->ni_htparam, IEEE80211_HTCAP_MPDUDENSITY);
2746222683Sbschmidt
2747282704Sadrian		/* Cap at VAP rxmax */
2748282704Sadrian		if (rxmax > vap->iv_ampdu_rxmax)
2749282704Sadrian			rxmax = vap->iv_ampdu_rxmax;
2750282704Sadrian
2751222683Sbschmidt		/*
2752282704Sadrian		 * If the VAP ampdu density value greater, use that.
2753282704Sadrian		 *
2754282704Sadrian		 * (Larger density value == larger minimum gap between A-MPDU
2755282704Sadrian		 * subframes.)
2756282704Sadrian		 */
2757282704Sadrian		if (vap->iv_ampdu_density > density)
2758282704Sadrian			density = vap->iv_ampdu_density;
2759282704Sadrian
2760282704Sadrian		/*
2761222683Sbschmidt		 * NB: Hardware might support HT40 on some but not all
2762222683Sbschmidt		 * channels. We can't determine this earlier because only
2763222683Sbschmidt		 * after association the channel is upgraded to HT based
2764222683Sbschmidt		 * on the negotiated capabilities.
2765222683Sbschmidt		 */
2766222683Sbschmidt		if (ni->ni_chan != IEEE80211_CHAN_ANYC &&
2767222683Sbschmidt		    findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT40U) == NULL &&
2768222683Sbschmidt		    findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT40D) == NULL)
2769222683Sbschmidt			caps &= ~IEEE80211_HTCAP_CHWIDTH40;
2770173273Ssam	} else {
2771173273Ssam		/* override 20/40 use based on current channel */
2772178354Ssam		if (IEEE80211_IS_CHAN_HT40(ni->ni_chan))
2773173273Ssam			caps |= IEEE80211_HTCAP_CHWIDTH40;
2774173273Ssam		else
2775173273Ssam			caps &= ~IEEE80211_HTCAP_CHWIDTH40;
2776282704Sadrian
2777282704Sadrian		/* XXX TODO should it start by using advertised settings? */
2778178354Ssam		rxmax = vap->iv_ampdu_rxmax;
2779178354Ssam		density = vap->iv_ampdu_density;
2780173273Ssam	}
2781282704Sadrian
2782170530Ssam	/* adjust short GI based on channel and config */
2783193655Ssam	if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20) == 0)
2784170530Ssam		caps &= ~IEEE80211_HTCAP_SHORTGI20;
2785193655Ssam	if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40) == 0 ||
2786170530Ssam	    (caps & IEEE80211_HTCAP_CHWIDTH40) == 0)
2787170530Ssam		caps &= ~IEEE80211_HTCAP_SHORTGI40;
2788170530Ssam	ADDSHORT(frm, caps);
2789170530Ssam
2790170530Ssam	/* HT parameters */
2791173865Ssam	*frm = SM(rxmax, IEEE80211_HTCAP_MAXRXAMPDU)
2792173865Ssam	     | SM(density, IEEE80211_HTCAP_MPDUDENSITY)
2793173273Ssam	     ;
2794170530Ssam	frm++;
2795170530Ssam
2796170530Ssam	/* pre-zero remainder of ie */
2797170530Ssam	memset(frm, 0, sizeof(struct ieee80211_ie_htcap) -
2798170530Ssam		__offsetof(struct ieee80211_ie_htcap, hc_mcsset));
2799170530Ssam
2800170530Ssam	/* supported MCS set */
2801173273Ssam	/*
2802219600Sbschmidt	 * XXX: For sta mode the rate set should be restricted based
2803219600Sbschmidt	 * on the AP's capabilities, but ni_htrates isn't setup when
2804219600Sbschmidt	 * we're called to form an AssocReq frame so for now we're
2805219600Sbschmidt	 * restricted to the device capabilities.
2806173273Ssam	 */
2807219600Sbschmidt	ieee80211_set_mcsset(ni->ni_ic, frm);
2808170530Ssam
2809205513Srpaulo	frm += __offsetof(struct ieee80211_ie_htcap, hc_extcap) -
2810205513Srpaulo		__offsetof(struct ieee80211_ie_htcap, hc_mcsset);
2811205513Srpaulo
2812205513Srpaulo	/* HT extended capabilities */
2813205513Srpaulo	extcaps = vap->iv_htextcaps & 0xffff;
2814205513Srpaulo
2815205513Srpaulo	ADDSHORT(frm, extcaps);
2816205513Srpaulo
2817170530Ssam	frm += sizeof(struct ieee80211_ie_htcap) -
2818205513Srpaulo		__offsetof(struct ieee80211_ie_htcap, hc_txbf);
2819205513Srpaulo
2820170530Ssam	return frm;
2821170530Ssam#undef ADDSHORT
2822170530Ssam}
2823170530Ssam
2824170530Ssam/*
2825170530Ssam * Add 802.11n HT capabilities information element
2826170530Ssam */
2827170530Ssamuint8_t *
2828170530Ssamieee80211_add_htcap(uint8_t *frm, struct ieee80211_node *ni)
2829170530Ssam{
2830170530Ssam	frm[0] = IEEE80211_ELEMID_HTCAP;
2831170530Ssam	frm[1] = sizeof(struct ieee80211_ie_htcap) - 2;
2832170530Ssam	return ieee80211_add_htcap_body(frm + 2, ni);
2833170530Ssam}
2834170530Ssam
2835170530Ssam/*
2836170530Ssam * Add Broadcom OUI wrapped standard HTCAP ie; this is
2837170530Ssam * used for compatibility w/ pre-draft implementations.
2838170530Ssam */
2839170530Ssamuint8_t *
2840170530Ssamieee80211_add_htcap_vendor(uint8_t *frm, struct ieee80211_node *ni)
2841170530Ssam{
2842170530Ssam	frm[0] = IEEE80211_ELEMID_VENDOR;
2843170530Ssam	frm[1] = 4 + sizeof(struct ieee80211_ie_htcap) - 2;
2844170530Ssam	frm[2] = (BCM_OUI >> 0) & 0xff;
2845170530Ssam	frm[3] = (BCM_OUI >> 8) & 0xff;
2846170530Ssam	frm[4] = (BCM_OUI >> 16) & 0xff;
2847170530Ssam	frm[5] = BCM_OUI_HTCAP;
2848170530Ssam	return ieee80211_add_htcap_body(frm + 6, ni);
2849170530Ssam}
2850170530Ssam
2851170530Ssam/*
2852170530Ssam * Construct the MCS bit mask of basic rates
2853170530Ssam * for inclusion in an HT information element.
2854170530Ssam */
2855170530Ssamstatic void
2856170530Ssamieee80211_set_basic_htrates(uint8_t *frm, const struct ieee80211_htrateset *rs)
2857170530Ssam{
2858170530Ssam	int i;
2859170530Ssam
2860170530Ssam	for (i = 0; i < rs->rs_nrates; i++) {
2861170530Ssam		int r = rs->rs_rates[i] & IEEE80211_RATE_VAL;
2862170530Ssam		if ((rs->rs_rates[i] & IEEE80211_RATE_BASIC) &&
2863170530Ssam		    r < IEEE80211_HTRATE_MAXSIZE) {
2864170530Ssam			/* NB: this assumes a particular implementation */
2865170530Ssam			setbit(frm, r);
2866170530Ssam		}
2867170530Ssam	}
2868170530Ssam}
2869170530Ssam
2870170530Ssam/*
2871172211Ssam * Update the HTINFO ie for a beacon frame.
2872172211Ssam */
2873172211Ssamvoid
2874178354Ssamieee80211_ht_update_beacon(struct ieee80211vap *vap,
2875172211Ssam	struct ieee80211_beacon_offsets *bo)
2876172211Ssam{
2877172211Ssam#define	PROTMODE	(IEEE80211_HTINFO_OPMODE|IEEE80211_HTINFO_NONHT_PRESENT)
2878250442Sadrian	struct ieee80211_node *ni;
2879250442Sadrian	const struct ieee80211_channel *bsschan;
2880178354Ssam	struct ieee80211com *ic = vap->iv_ic;
2881172211Ssam	struct ieee80211_ie_htinfo *ht =
2882172211Ssam	   (struct ieee80211_ie_htinfo *) bo->bo_htinfo;
2883172211Ssam
2884250442Sadrian	ni = ieee80211_ref_node(vap->iv_bss);
2885250442Sadrian	bsschan = ni->ni_chan;
2886250442Sadrian
2887172211Ssam	/* XXX only update on channel change */
2888178354Ssam	ht->hi_ctrlchannel = ieee80211_chan2ieee(ic, bsschan);
2889193655Ssam	if (vap->iv_flags_ht & IEEE80211_FHT_RIFS)
2890183256Ssam		ht->hi_byte1 = IEEE80211_HTINFO_RIFSMODE_PERM;
2891183256Ssam	else
2892183256Ssam		ht->hi_byte1 = IEEE80211_HTINFO_RIFSMODE_PROH;
2893178354Ssam	if (IEEE80211_IS_CHAN_HT40U(bsschan))
2894172211Ssam		ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_ABOVE;
2895178354Ssam	else if (IEEE80211_IS_CHAN_HT40D(bsschan))
2896172211Ssam		ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_BELOW;
2897172211Ssam	else
2898172211Ssam		ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_NONE;
2899178354Ssam	if (IEEE80211_IS_CHAN_HT40(bsschan))
2900172211Ssam		ht->hi_byte1 |= IEEE80211_HTINFO_TXWIDTH_2040;
2901172211Ssam
2902172211Ssam	/* protection mode */
2903172211Ssam	ht->hi_byte2 = (ht->hi_byte2 &~ PROTMODE) | ic->ic_curhtprotmode;
2904172211Ssam
2905250442Sadrian	ieee80211_free_node(ni);
2906250442Sadrian
2907172211Ssam	/* XXX propagate to vendor ie's */
2908172211Ssam#undef PROTMODE
2909172211Ssam}
2910172211Ssam
2911172211Ssam/*
2912170530Ssam * Add body of an HTINFO information element.
2913173273Ssam *
2914173273Ssam * NB: We don't use struct ieee80211_ie_htinfo because we can
2915173273Ssam * be called to fillin both a standard ie and a compat ie that
2916173273Ssam * has a vendor OUI at the front.
2917170530Ssam */
2918170530Ssamstatic uint8_t *
2919170530Ssamieee80211_add_htinfo_body(uint8_t *frm, struct ieee80211_node *ni)
2920170530Ssam{
2921183256Ssam	struct ieee80211vap *vap = ni->ni_vap;
2922170530Ssam	struct ieee80211com *ic = ni->ni_ic;
2923170530Ssam
2924170530Ssam	/* pre-zero remainder of ie */
2925170530Ssam	memset(frm, 0, sizeof(struct ieee80211_ie_htinfo) - 2);
2926170530Ssam
2927170530Ssam	/* primary/control channel center */
2928178354Ssam	*frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
2929170530Ssam
2930193655Ssam	if (vap->iv_flags_ht & IEEE80211_FHT_RIFS)
2931183256Ssam		frm[0] = IEEE80211_HTINFO_RIFSMODE_PERM;
2932183256Ssam	else
2933183256Ssam		frm[0] = IEEE80211_HTINFO_RIFSMODE_PROH;
2934178354Ssam	if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan))
2935170530Ssam		frm[0] |= IEEE80211_HTINFO_2NDCHAN_ABOVE;
2936178354Ssam	else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan))
2937170530Ssam		frm[0] |= IEEE80211_HTINFO_2NDCHAN_BELOW;
2938170530Ssam	else
2939170530Ssam		frm[0] |= IEEE80211_HTINFO_2NDCHAN_NONE;
2940178354Ssam	if (IEEE80211_IS_CHAN_HT40(ni->ni_chan))
2941170530Ssam		frm[0] |= IEEE80211_HTINFO_TXWIDTH_2040;
2942170530Ssam
2943172211Ssam	frm[1] = ic->ic_curhtprotmode;
2944170530Ssam
2945170530Ssam	frm += 5;
2946170530Ssam
2947170530Ssam	/* basic MCS set */
2948170530Ssam	ieee80211_set_basic_htrates(frm, &ni->ni_htrates);
2949170530Ssam	frm += sizeof(struct ieee80211_ie_htinfo) -
2950170530Ssam		__offsetof(struct ieee80211_ie_htinfo, hi_basicmcsset);
2951170530Ssam	return frm;
2952170530Ssam}
2953170530Ssam
2954170530Ssam/*
2955170530Ssam * Add 802.11n HT information information element.
2956170530Ssam */
2957170530Ssamuint8_t *
2958170530Ssamieee80211_add_htinfo(uint8_t *frm, struct ieee80211_node *ni)
2959170530Ssam{
2960170530Ssam	frm[0] = IEEE80211_ELEMID_HTINFO;
2961170530Ssam	frm[1] = sizeof(struct ieee80211_ie_htinfo) - 2;
2962170530Ssam	return ieee80211_add_htinfo_body(frm + 2, ni);
2963170530Ssam}
2964170530Ssam
2965170530Ssam/*
2966170530Ssam * Add Broadcom OUI wrapped standard HTINFO ie; this is
2967170530Ssam * used for compatibility w/ pre-draft implementations.
2968170530Ssam */
2969170530Ssamuint8_t *
2970170530Ssamieee80211_add_htinfo_vendor(uint8_t *frm, struct ieee80211_node *ni)
2971170530Ssam{
2972170530Ssam	frm[0] = IEEE80211_ELEMID_VENDOR;
2973170530Ssam	frm[1] = 4 + sizeof(struct ieee80211_ie_htinfo) - 2;
2974170530Ssam	frm[2] = (BCM_OUI >> 0) & 0xff;
2975170530Ssam	frm[3] = (BCM_OUI >> 8) & 0xff;
2976170530Ssam	frm[4] = (BCM_OUI >> 16) & 0xff;
2977170530Ssam	frm[5] = BCM_OUI_HTINFO;
2978170530Ssam	return ieee80211_add_htinfo_body(frm + 6, ni);
2979170530Ssam}
2980