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: releng/11.0/sys/net80211/ieee80211_ht.c 300232 2016-05-19 21:08:33Z avos $");
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>
40295126Sglebius#include <sys/malloc.h>
41170530Ssam#include <sys/systm.h>
42170530Ssam#include <sys/endian.h>
43170530Ssam
44170530Ssam#include <sys/socket.h>
45170530Ssam
46170530Ssam#include <net/if.h>
47257176Sglebius#include <net/if_var.h>
48170530Ssam#include <net/if_media.h>
49170530Ssam#include <net/ethernet.h>
50170530Ssam
51170530Ssam#include <net80211/ieee80211_var.h>
52195377Ssam#include <net80211/ieee80211_action.h>
53178354Ssam#include <net80211/ieee80211_input.h>
54170530Ssam
55170530Ssam/* define here, used throughout file */
56170530Ssam#define	MS(_v, _f)	(((_v) & _f) >> _f##_S)
57170530Ssam#define	SM(_v, _f)	(((_v) << _f##_S) & _f)
58170530Ssam
59219456Sbschmidtconst struct ieee80211_mcs_rates ieee80211_htrates[IEEE80211_HTRATE_MAXSIZE] = {
60219456Sbschmidt	{  13,  14,   27,   30 },	/* MCS 0 */
61219456Sbschmidt	{  26,  29,   54,   60 },	/* MCS 1 */
62219456Sbschmidt	{  39,  43,   81,   90 },	/* MCS 2 */
63219456Sbschmidt	{  52,  58,  108,  120 },	/* MCS 3 */
64219456Sbschmidt	{  78,  87,  162,  180 },	/* MCS 4 */
65219456Sbschmidt	{ 104, 116,  216,  240 },	/* MCS 5 */
66219456Sbschmidt	{ 117, 130,  243,  270 },	/* MCS 6 */
67219456Sbschmidt	{ 130, 144,  270,  300 },	/* MCS 7 */
68219456Sbschmidt	{  26,  29,   54,   60 },	/* MCS 8 */
69219456Sbschmidt	{  52,  58,  108,  120 },	/* MCS 9 */
70219456Sbschmidt	{  78,  87,  162,  180 },	/* MCS 10 */
71219456Sbschmidt	{ 104, 116,  216,  240 },	/* MCS 11 */
72219456Sbschmidt	{ 156, 173,  324,  360 },	/* MCS 12 */
73219456Sbschmidt	{ 208, 231,  432,  480 },	/* MCS 13 */
74219456Sbschmidt	{ 234, 260,  486,  540 },	/* MCS 14 */
75219456Sbschmidt	{ 260, 289,  540,  600 },	/* MCS 15 */
76219456Sbschmidt	{  39,  43,   81,   90 },	/* MCS 16 */
77219456Sbschmidt	{  78,  87,  162,  180 },	/* MCS 17 */
78219456Sbschmidt	{ 117, 130,  243,  270 },	/* MCS 18 */
79219456Sbschmidt	{ 156, 173,  324,  360 },	/* MCS 19 */
80219456Sbschmidt	{ 234, 260,  486,  540 },	/* MCS 20 */
81219456Sbschmidt	{ 312, 347,  648,  720 },	/* MCS 21 */
82219456Sbschmidt	{ 351, 390,  729,  810 },	/* MCS 22 */
83219456Sbschmidt	{ 390, 433,  810,  900 },	/* MCS 23 */
84219456Sbschmidt	{  52,  58,  108,  120 },	/* MCS 24 */
85219456Sbschmidt	{ 104, 116,  216,  240 },	/* MCS 25 */
86219456Sbschmidt	{ 156, 173,  324,  360 },	/* MCS 26 */
87219456Sbschmidt	{ 208, 231,  432,  480 },	/* MCS 27 */
88219456Sbschmidt	{ 312, 347,  648,  720 },	/* MCS 28 */
89219456Sbschmidt	{ 416, 462,  864,  960 },	/* MCS 29 */
90219456Sbschmidt	{ 468, 520,  972, 1080 },	/* MCS 30 */
91219456Sbschmidt	{ 520, 578, 1080, 1200 },	/* MCS 31 */
92219456Sbschmidt	{   0,   0,   12,   13 },	/* MCS 32 */
93219456Sbschmidt	{  78,  87,  162,  180 },	/* MCS 33 */
94219456Sbschmidt	{ 104, 116,  216,  240 },	/* MCS 34 */
95219456Sbschmidt	{ 130, 144,  270,  300 },	/* MCS 35 */
96219456Sbschmidt	{ 117, 130,  243,  270 },	/* MCS 36 */
97219456Sbschmidt	{ 156, 173,  324,  360 },	/* MCS 37 */
98219456Sbschmidt	{ 195, 217,  405,  450 },	/* MCS 38 */
99219456Sbschmidt	{ 104, 116,  216,  240 },	/* MCS 39 */
100219456Sbschmidt	{ 130, 144,  270,  300 },	/* MCS 40 */
101219456Sbschmidt	{ 130, 144,  270,  300 },	/* MCS 41 */
102219456Sbschmidt	{ 156, 173,  324,  360 },	/* MCS 42 */
103219456Sbschmidt	{ 182, 202,  378,  420 },	/* MCS 43 */
104219456Sbschmidt	{ 182, 202,  378,  420 },	/* MCS 44 */
105219456Sbschmidt	{ 208, 231,  432,  480 },	/* MCS 45 */
106219456Sbschmidt	{ 156, 173,  324,  360 },	/* MCS 46 */
107219456Sbschmidt	{ 195, 217,  405,  450 },	/* MCS 47 */
108219456Sbschmidt	{ 195, 217,  405,  450 },	/* MCS 48 */
109219456Sbschmidt	{ 234, 260,  486,  540 },	/* MCS 49 */
110219456Sbschmidt	{ 273, 303,  567,  630 },	/* MCS 50 */
111219456Sbschmidt	{ 273, 303,  567,  630 },	/* MCS 51 */
112219456Sbschmidt	{ 312, 347,  648,  720 },	/* MCS 52 */
113219456Sbschmidt	{ 130, 144,  270,  300 },	/* MCS 53 */
114219456Sbschmidt	{ 156, 173,  324,  360 },	/* MCS 54 */
115219456Sbschmidt	{ 182, 202,  378,  420 },	/* MCS 55 */
116219456Sbschmidt	{ 156, 173,  324,  360 },	/* MCS 56 */
117219456Sbschmidt	{ 182, 202,  378,  420 },	/* MCS 57 */
118219456Sbschmidt	{ 208, 231,  432,  480 },	/* MCS 58 */
119219456Sbschmidt	{ 234, 260,  486,  540 },	/* MCS 59 */
120219456Sbschmidt	{ 208, 231,  432,  480 },	/* MCS 60 */
121219456Sbschmidt	{ 234, 260,  486,  540 },	/* MCS 61 */
122219456Sbschmidt	{ 260, 289,  540,  600 },	/* MCS 62 */
123219456Sbschmidt	{ 260, 289,  540,  600 },	/* MCS 63 */
124219456Sbschmidt	{ 286, 318,  594,  660 },	/* MCS 64 */
125219456Sbschmidt	{ 195, 217,  405,  450 },	/* MCS 65 */
126219456Sbschmidt	{ 234, 260,  486,  540 },	/* MCS 66 */
127219456Sbschmidt	{ 273, 303,  567,  630 },	/* MCS 67 */
128219456Sbschmidt	{ 234, 260,  486,  540 },	/* MCS 68 */
129219456Sbschmidt	{ 273, 303,  567,  630 },	/* MCS 69 */
130219456Sbschmidt	{ 312, 347,  648,  720 },	/* MCS 70 */
131219456Sbschmidt	{ 351, 390,  729,  810 },	/* MCS 71 */
132219456Sbschmidt	{ 312, 347,  648,  720 },	/* MCS 72 */
133219456Sbschmidt	{ 351, 390,  729,  810 },	/* MCS 73 */
134219456Sbschmidt	{ 390, 433,  810,  900 },	/* MCS 74 */
135219456Sbschmidt	{ 390, 433,  810,  900 },	/* MCS 75 */
136219456Sbschmidt	{ 429, 477,  891,  990 },	/* MCS 76 */
137170530Ssam};
138170530Ssam
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
144193115Ssamstatic	int ieee80211_recv_bar_ena = 1;
145193115SsamSYSCTL_INT(_net_wlan, OID_AUTO, recv_bar, CTLFLAG_RW, &ieee80211_recv_bar_ena,
146193115Ssam	    0, "BAR frame processing (ena/dis)");
147193115Ssam
148193115Ssamstatic	int ieee80211_addba_timeout = -1;/* timeout for ADDBA response */
149193115SsamSYSCTL_PROC(_net_wlan, OID_AUTO, addba_timeout, CTLTYPE_INT | CTLFLAG_RW,
150193115Ssam	&ieee80211_addba_timeout, 0, ieee80211_sysctl_msecs_ticks, "I",
151193115Ssam	"ADDBA request timeout (ms)");
152193115Ssamstatic	int ieee80211_addba_backoff = -1;/* backoff after max ADDBA requests */
153193115SsamSYSCTL_PROC(_net_wlan, OID_AUTO, addba_backoff, CTLTYPE_INT | CTLFLAG_RW,
154193115Ssam	&ieee80211_addba_backoff, 0, ieee80211_sysctl_msecs_ticks, "I",
155193115Ssam	"ADDBA request backoff (ms)");
156193115Ssamstatic	int ieee80211_addba_maxtries = 3;/* max ADDBA requests before backoff */
157273377ShselaskySYSCTL_INT(_net_wlan, OID_AUTO, addba_maxtries, CTLFLAG_RW,
158193115Ssam	&ieee80211_addba_maxtries, 0, "max ADDBA requests sent before backoff");
159193115Ssam
160193115Ssamstatic	int ieee80211_bar_timeout = -1;	/* timeout waiting for BAR response */
161193115Ssamstatic	int ieee80211_bar_maxtries = 50;/* max BAR requests before DELBA */
162193115Ssam
163195377Ssamstatic	ieee80211_recv_action_func ht_recv_action_ba_addba_request;
164195377Ssamstatic	ieee80211_recv_action_func ht_recv_action_ba_addba_response;
165195377Ssamstatic	ieee80211_recv_action_func ht_recv_action_ba_delba;
166195377Ssamstatic	ieee80211_recv_action_func ht_recv_action_ht_mimopwrsave;
167195377Ssamstatic	ieee80211_recv_action_func ht_recv_action_ht_txchwidth;
168195377Ssam
169195377Ssamstatic	ieee80211_send_action_func ht_send_action_ba_addba;
170195377Ssamstatic	ieee80211_send_action_func ht_send_action_ba_delba;
171195377Ssamstatic	ieee80211_send_action_func ht_send_action_ht_txchwidth;
172195377Ssam
173178354Ssamstatic void
174195377Ssamieee80211_ht_init(void)
175178354Ssam{
176195377Ssam	/*
177195377Ssam	 * Setup HT parameters that depends on the clock frequency.
178195377Ssam	 */
179178354Ssam	ieee80211_ampdu_age = msecs_to_ticks(500);
180178354Ssam	ieee80211_addba_timeout = msecs_to_ticks(250);
181178354Ssam	ieee80211_addba_backoff = msecs_to_ticks(10*1000);
182184280Ssam	ieee80211_bar_timeout = msecs_to_ticks(250);
183195377Ssam	/*
184195377Ssam	 * Register action frame handlers.
185195377Ssam	 */
186195377Ssam	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_BA,
187195377Ssam	    IEEE80211_ACTION_BA_ADDBA_REQUEST, ht_recv_action_ba_addba_request);
188195377Ssam	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_BA,
189195377Ssam	    IEEE80211_ACTION_BA_ADDBA_RESPONSE, ht_recv_action_ba_addba_response);
190195377Ssam	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_BA,
191195377Ssam	    IEEE80211_ACTION_BA_DELBA, ht_recv_action_ba_delba);
192195377Ssam	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_HT,
193195377Ssam	    IEEE80211_ACTION_HT_MIMOPWRSAVE, ht_recv_action_ht_mimopwrsave);
194195377Ssam	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_HT,
195195377Ssam	    IEEE80211_ACTION_HT_TXCHWIDTH, ht_recv_action_ht_txchwidth);
196195377Ssam
197195377Ssam	ieee80211_send_action_register(IEEE80211_ACTION_CAT_BA,
198195377Ssam	    IEEE80211_ACTION_BA_ADDBA_REQUEST, ht_send_action_ba_addba);
199195377Ssam	ieee80211_send_action_register(IEEE80211_ACTION_CAT_BA,
200195377Ssam	    IEEE80211_ACTION_BA_ADDBA_RESPONSE, ht_send_action_ba_addba);
201195377Ssam	ieee80211_send_action_register(IEEE80211_ACTION_CAT_BA,
202195377Ssam	    IEEE80211_ACTION_BA_DELBA, ht_send_action_ba_delba);
203195377Ssam	ieee80211_send_action_register(IEEE80211_ACTION_CAT_HT,
204195377Ssam	    IEEE80211_ACTION_HT_TXCHWIDTH, ht_send_action_ht_txchwidth);
205178354Ssam}
206195377SsamSYSINIT(wlan_ht, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_ht_init, NULL);
207170530Ssam
208178354Ssamstatic int ieee80211_ampdu_enable(struct ieee80211_node *ni,
209178354Ssam	struct ieee80211_tx_ampdu *tap);
210170530Ssamstatic int ieee80211_addba_request(struct ieee80211_node *ni,
211170530Ssam	struct ieee80211_tx_ampdu *tap,
212170530Ssam	int dialogtoken, int baparamset, int batimeout);
213170530Ssamstatic int ieee80211_addba_response(struct ieee80211_node *ni,
214170530Ssam	struct ieee80211_tx_ampdu *tap,
215170530Ssam	int code, int baparamset, int batimeout);
216170530Ssamstatic void ieee80211_addba_stop(struct ieee80211_node *ni,
217170530Ssam	struct ieee80211_tx_ampdu *tap);
218223331Sadrianstatic void null_addba_response_timeout(struct ieee80211_node *ni,
219223331Sadrian	struct ieee80211_tx_ampdu *tap);
220223331Sadrian
221184280Ssamstatic void ieee80211_bar_response(struct ieee80211_node *ni,
222184280Ssam	struct ieee80211_tx_ampdu *tap, int status);
223184280Ssamstatic void ampdu_tx_stop(struct ieee80211_tx_ampdu *tap);
224184280Ssamstatic void bar_stop_timer(struct ieee80211_tx_ampdu *tap);
225191552Ssamstatic int ampdu_rx_start(struct ieee80211_node *, struct ieee80211_rx_ampdu *,
226191552Ssam	int baparamset, int batimeout, int baseqctl);
227191552Ssamstatic void ampdu_rx_stop(struct ieee80211_node *, struct ieee80211_rx_ampdu *);
228170530Ssam
229170530Ssamvoid
230170530Ssamieee80211_ht_attach(struct ieee80211com *ic)
231170530Ssam{
232170530Ssam	/* setup default aggregation policy */
233195377Ssam	ic->ic_recv_action = ieee80211_recv_action;
234170530Ssam	ic->ic_send_action = ieee80211_send_action;
235178354Ssam	ic->ic_ampdu_enable = ieee80211_ampdu_enable;
236170530Ssam	ic->ic_addba_request = ieee80211_addba_request;
237170530Ssam	ic->ic_addba_response = ieee80211_addba_response;
238223331Sadrian	ic->ic_addba_response_timeout = null_addba_response_timeout;
239170530Ssam	ic->ic_addba_stop = ieee80211_addba_stop;
240184280Ssam	ic->ic_bar_response = ieee80211_bar_response;
241191552Ssam	ic->ic_ampdu_rx_start = ampdu_rx_start;
242191552Ssam	ic->ic_ampdu_rx_stop = ampdu_rx_stop;
243170530Ssam
244173273Ssam	ic->ic_htprotmode = IEEE80211_PROT_RTSCTS;
245173273Ssam	ic->ic_curhtprotmode = IEEE80211_HTINFO_OPMODE_PURE;
246178354Ssam}
247173273Ssam
248178354Ssamvoid
249178354Ssamieee80211_ht_detach(struct ieee80211com *ic)
250178354Ssam{
251178354Ssam}
252173273Ssam
253178354Ssamvoid
254178354Ssamieee80211_ht_vattach(struct ieee80211vap *vap)
255178354Ssam{
256178354Ssam
257178354Ssam	/* driver can override defaults */
258178354Ssam	vap->iv_ampdu_rxmax = IEEE80211_HTCAP_MAXRXAMPDU_8K;
259178354Ssam	vap->iv_ampdu_density = IEEE80211_HTCAP_MPDUDENSITY_NA;
260178354Ssam	vap->iv_ampdu_limit = vap->iv_ampdu_rxmax;
261178354Ssam	vap->iv_amsdu_limit = vap->iv_htcaps & IEEE80211_HTCAP_MAXAMSDU;
262178354Ssam	/* tx aggregation traffic thresholds */
263178354Ssam	vap->iv_ampdu_mintraffic[WME_AC_BK] = 128;
264178354Ssam	vap->iv_ampdu_mintraffic[WME_AC_BE] = 64;
265178354Ssam	vap->iv_ampdu_mintraffic[WME_AC_VO] = 32;
266178354Ssam	vap->iv_ampdu_mintraffic[WME_AC_VI] = 32;
267178354Ssam
268178354Ssam	if (vap->iv_htcaps & IEEE80211_HTC_HT) {
269170530Ssam		/*
270173273Ssam		 * Device is HT capable; enable all HT-related
271173273Ssam		 * facilities by default.
272170530Ssam		 * XXX these choices may be too aggressive.
273170530Ssam		 */
274193655Ssam		vap->iv_flags_ht |= IEEE80211_FHT_HT
275193655Ssam				 |  IEEE80211_FHT_HTCOMPAT
276193655Ssam				 ;
277178354Ssam		if (vap->iv_htcaps & IEEE80211_HTCAP_SHORTGI20)
278193655Ssam			vap->iv_flags_ht |= IEEE80211_FHT_SHORTGI20;
279173273Ssam		/* XXX infer from channel list? */
280178354Ssam		if (vap->iv_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
281193655Ssam			vap->iv_flags_ht |= IEEE80211_FHT_USEHT40;
282178354Ssam			if (vap->iv_htcaps & IEEE80211_HTCAP_SHORTGI40)
283193655Ssam				vap->iv_flags_ht |= IEEE80211_FHT_SHORTGI40;
284170530Ssam		}
285183256Ssam		/* enable RIFS if capable */
286183256Ssam		if (vap->iv_htcaps & IEEE80211_HTC_RIFS)
287193655Ssam			vap->iv_flags_ht |= IEEE80211_FHT_RIFS;
288183256Ssam
289170530Ssam		/* NB: A-MPDU and A-MSDU rx are mandated, these are tx only */
290193655Ssam		vap->iv_flags_ht |= IEEE80211_FHT_AMPDU_RX;
291178354Ssam		if (vap->iv_htcaps & IEEE80211_HTC_AMPDU)
292193655Ssam			vap->iv_flags_ht |= IEEE80211_FHT_AMPDU_TX;
293193655Ssam		vap->iv_flags_ht |= IEEE80211_FHT_AMSDU_RX;
294178354Ssam		if (vap->iv_htcaps & IEEE80211_HTC_AMSDU)
295193655Ssam			vap->iv_flags_ht |= IEEE80211_FHT_AMSDU_TX;
296298604Sadrian
297298604Sadrian		if (vap->iv_htcaps & IEEE80211_HTCAP_TXSTBC)
298298604Sadrian			vap->iv_flags_ht |= IEEE80211_FHT_STBC_TX;
299298604Sadrian		if (vap->iv_htcaps & IEEE80211_HTCAP_RXSTBC)
300298604Sadrian			vap->iv_flags_ht |= IEEE80211_FHT_STBC_RX;
301170530Ssam	}
302178354Ssam	/* NB: disable default legacy WDS, too many issues right now */
303178354Ssam	if (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY)
304193655Ssam		vap->iv_flags_ht &= ~IEEE80211_FHT_HT;
305170530Ssam}
306170530Ssam
307170530Ssamvoid
308178354Ssamieee80211_ht_vdetach(struct ieee80211vap *vap)
309170530Ssam{
310170530Ssam}
311170530Ssam
312219598Sbschmidtstatic int
313228621Sbschmidtht_getrate(struct ieee80211com *ic, int index, enum ieee80211_phymode mode,
314228621Sbschmidt    int ratetype)
315219598Sbschmidt{
316219598Sbschmidt	int mword, rate;
317219598Sbschmidt
318219598Sbschmidt	mword = ieee80211_rate2media(ic, index | IEEE80211_RATE_MCS, mode);
319219598Sbschmidt	if (IFM_SUBTYPE(mword) != IFM_IEEE80211_MCS)
320219598Sbschmidt		return (0);
321219598Sbschmidt	switch (ratetype) {
322219598Sbschmidt	case 0:
323219598Sbschmidt		rate = ieee80211_htrates[index].ht20_rate_800ns;
324219598Sbschmidt		break;
325219598Sbschmidt	case 1:
326219598Sbschmidt		rate = ieee80211_htrates[index].ht20_rate_400ns;
327219598Sbschmidt		break;
328219598Sbschmidt	case 2:
329219598Sbschmidt		rate = ieee80211_htrates[index].ht40_rate_800ns;
330219598Sbschmidt		break;
331219598Sbschmidt	default:
332219598Sbschmidt		rate = ieee80211_htrates[index].ht40_rate_400ns;
333219598Sbschmidt		break;
334219598Sbschmidt	}
335219598Sbschmidt	return (rate);
336219598Sbschmidt}
337219598Sbschmidt
338219598Sbschmidtstatic struct printranges {
339219598Sbschmidt	int	minmcs;
340219598Sbschmidt	int	maxmcs;
341219598Sbschmidt	int	txstream;
342219598Sbschmidt	int	ratetype;
343219598Sbschmidt	int	htcapflags;
344219598Sbschmidt} ranges[] = {
345219598Sbschmidt	{  0,  7, 1, 0, 0 },
346219598Sbschmidt	{  8, 15, 2, 0, 0 },
347219598Sbschmidt	{ 16, 23, 3, 0, 0 },
348219598Sbschmidt	{ 24, 31, 4, 0, 0 },
349219598Sbschmidt	{ 32,  0, 1, 2, IEEE80211_HTC_TXMCS32 },
350219598Sbschmidt	{ 33, 38, 2, 0, IEEE80211_HTC_TXUNEQUAL },
351219598Sbschmidt	{ 39, 52, 3, 0, IEEE80211_HTC_TXUNEQUAL },
352219598Sbschmidt	{ 53, 76, 4, 0, IEEE80211_HTC_TXUNEQUAL },
353219598Sbschmidt	{  0,  0, 0, 0, 0 },
354219598Sbschmidt};
355219598Sbschmidt
356170530Ssamstatic void
357228621Sbschmidtht_rateprint(struct ieee80211com *ic, enum ieee80211_phymode mode, int ratetype)
358170530Ssam{
359219598Sbschmidt	int minrate, maxrate;
360219598Sbschmidt	struct printranges *range;
361170530Ssam
362219598Sbschmidt	for (range = ranges; range->txstream != 0; range++) {
363219598Sbschmidt		if (ic->ic_txstream < range->txstream)
364170530Ssam			continue;
365219598Sbschmidt		if (range->htcapflags &&
366219598Sbschmidt		    (ic->ic_htcaps & range->htcapflags) == 0)
367219598Sbschmidt			continue;
368219598Sbschmidt		if (ratetype < range->ratetype)
369219598Sbschmidt			continue;
370219598Sbschmidt		minrate = ht_getrate(ic, range->minmcs, mode, ratetype);
371219598Sbschmidt		maxrate = ht_getrate(ic, range->maxmcs, mode, ratetype);
372219598Sbschmidt		if (range->maxmcs) {
373283529Sglebius			ic_printf(ic, "MCS %d-%d: %d%sMbps - %d%sMbps\n",
374219598Sbschmidt			    range->minmcs, range->maxmcs,
375219598Sbschmidt			    minrate/2, ((minrate & 0x1) != 0 ? ".5" : ""),
376219598Sbschmidt			    maxrate/2, ((maxrate & 0x1) != 0 ? ".5" : ""));
377219598Sbschmidt		} else {
378283529Sglebius			ic_printf(ic, "MCS %d: %d%sMbps\n", range->minmcs,
379219598Sbschmidt			    minrate/2, ((minrate & 0x1) != 0 ? ".5" : ""));
380205277Srpaulo		}
381170530Ssam	}
382170530Ssam}
383170530Ssam
384205277Srpaulostatic void
385228621Sbschmidtht_announce(struct ieee80211com *ic, enum ieee80211_phymode mode)
386205277Srpaulo{
387205277Srpaulo	const char *modestr = ieee80211_phymode_name[mode];
388219598Sbschmidt
389283529Sglebius	ic_printf(ic, "%s MCS 20MHz\n", modestr);
390219598Sbschmidt	ht_rateprint(ic, mode, 0);
391219598Sbschmidt	if (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20) {
392283529Sglebius		ic_printf(ic, "%s MCS 20MHz SGI\n", modestr);
393219598Sbschmidt		ht_rateprint(ic, mode, 1);
394219598Sbschmidt	}
395219598Sbschmidt	if (ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
396283529Sglebius		ic_printf(ic, "%s MCS 40MHz:\n", modestr);
397219598Sbschmidt		ht_rateprint(ic, mode, 2);
398219598Sbschmidt	}
399219598Sbschmidt	if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
400219598Sbschmidt	    (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40)) {
401283529Sglebius		ic_printf(ic, "%s MCS 40MHz SGI:\n", modestr);
402219598Sbschmidt		ht_rateprint(ic, mode, 3);
403219598Sbschmidt	}
404205277Srpaulo}
405205277Srpaulo
406170530Ssamvoid
407170530Ssamieee80211_ht_announce(struct ieee80211com *ic)
408170530Ssam{
409219598Sbschmidt
410219598Sbschmidt	if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) ||
411219598Sbschmidt	    isset(ic->ic_modecaps, IEEE80211_MODE_11NG))
412283529Sglebius		ic_printf(ic, "%dT%dR\n", ic->ic_txstream, ic->ic_rxstream);
413170530Ssam	if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA))
414219598Sbschmidt		ht_announce(ic, IEEE80211_MODE_11NA);
415170530Ssam	if (isset(ic->ic_modecaps, IEEE80211_MODE_11NG))
416219598Sbschmidt		ht_announce(ic, IEEE80211_MODE_11NG);
417170530Ssam}
418170530Ssam
419219602Sbschmidtstatic struct ieee80211_htrateset htrateset;
420219602Sbschmidt
421170530Ssamconst struct ieee80211_htrateset *
422170530Ssamieee80211_get_suphtrates(struct ieee80211com *ic,
423219602Sbschmidt    const struct ieee80211_channel *c)
424170530Ssam{
425219602Sbschmidt#define	ADDRATE(x)	do {						\
426219602Sbschmidt	htrateset.rs_rates[htrateset.rs_nrates] = x;			\
427219602Sbschmidt	htrateset.rs_nrates++;						\
428219602Sbschmidt} while (0)
429219602Sbschmidt	int i;
430219602Sbschmidt
431219602Sbschmidt	memset(&htrateset, 0, sizeof(struct ieee80211_htrateset));
432219602Sbschmidt	for (i = 0; i < ic->ic_txstream * 8; i++)
433219602Sbschmidt		ADDRATE(i);
434219602Sbschmidt	if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
435219602Sbschmidt	    (ic->ic_htcaps & IEEE80211_HTC_TXMCS32))
436229284Sbschmidt		ADDRATE(32);
437219602Sbschmidt	if (ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL) {
438219602Sbschmidt		if (ic->ic_txstream >= 2) {
439219602Sbschmidt			 for (i = 33; i <= 38; i++)
440219602Sbschmidt				ADDRATE(i);
441219602Sbschmidt		}
442219602Sbschmidt		if (ic->ic_txstream >= 3) {
443219602Sbschmidt			for (i = 39; i <= 52; i++)
444219602Sbschmidt				ADDRATE(i);
445219602Sbschmidt		}
446219602Sbschmidt		if (ic->ic_txstream == 4) {
447219602Sbschmidt			for (i = 53; i <= 76; i++)
448219602Sbschmidt				ADDRATE(i);
449219602Sbschmidt		}
450219602Sbschmidt	}
451219602Sbschmidt	return &htrateset;
452219602Sbschmidt#undef	ADDRATE
453170530Ssam}
454170530Ssam
455170530Ssam/*
456170530Ssam * Receive processing.
457170530Ssam */
458170530Ssam
459170530Ssam/*
460170530Ssam * Decap the encapsulated A-MSDU frames and dispatch all but
461170530Ssam * the last for delivery.  The last frame is returned for
462170530Ssam * delivery via the normal path.
463170530Ssam */
464170530Ssamstruct mbuf *
465170530Ssamieee80211_decap_amsdu(struct ieee80211_node *ni, struct mbuf *m)
466170530Ssam{
467178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
468173462Ssam	int framelen;
469170530Ssam	struct mbuf *n;
470170530Ssam
471170530Ssam	/* discard 802.3 header inserted by ieee80211_decap */
472170530Ssam	m_adj(m, sizeof(struct ether_header));
473170530Ssam
474178354Ssam	vap->iv_stats.is_amsdu_decap++;
475170530Ssam
476170530Ssam	for (;;) {
477170530Ssam		/*
478170530Ssam		 * Decap the first frame, bust it apart from the
479170530Ssam		 * remainder and deliver.  We leave the last frame
480170530Ssam		 * delivery to the caller (for consistency with other
481170530Ssam		 * code paths, could also do it here).
482170530Ssam		 */
483170530Ssam		m = ieee80211_decap1(m, &framelen);
484170530Ssam		if (m == NULL) {
485178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
486173462Ssam			    ni->ni_macaddr, "a-msdu", "%s", "decap failed");
487178354Ssam			vap->iv_stats.is_amsdu_tooshort++;
488170530Ssam			return NULL;
489170530Ssam		}
490173462Ssam		if (m->m_pkthdr.len == framelen)
491170530Ssam			break;
492170530Ssam		n = m_split(m, framelen, M_NOWAIT);
493170530Ssam		if (n == NULL) {
494178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
495170530Ssam			    ni->ni_macaddr, "a-msdu",
496170530Ssam			    "%s", "unable to split encapsulated frames");
497178354Ssam			vap->iv_stats.is_amsdu_split++;
498170530Ssam			m_freem(m);			/* NB: must reclaim */
499170530Ssam			return NULL;
500170530Ssam		}
501178354Ssam		vap->iv_deliver_data(vap, ni, m);
502170530Ssam
503170530Ssam		/*
504170530Ssam		 * Remove frame contents; each intermediate frame
505170530Ssam		 * is required to be aligned to a 4-byte boundary.
506170530Ssam		 */
507170530Ssam		m = n;
508170530Ssam		m_adj(m, roundup2(framelen, 4) - framelen);	/* padding */
509170530Ssam	}
510170530Ssam	return m;				/* last delivered by caller */
511170530Ssam}
512170530Ssam
513170530Ssam/*
514170530Ssam * Purge all frames in the A-MPDU re-order queue.
515170530Ssam */
516170530Ssamstatic void
517170530Ssamampdu_rx_purge(struct ieee80211_rx_ampdu *rap)
518170530Ssam{
519170530Ssam	struct mbuf *m;
520170530Ssam	int i;
521170530Ssam
522170530Ssam	for (i = 0; i < rap->rxa_wnd; i++) {
523170530Ssam		m = rap->rxa_m[i];
524170530Ssam		if (m != NULL) {
525170530Ssam			rap->rxa_m[i] = NULL;
526170530Ssam			rap->rxa_qbytes -= m->m_pkthdr.len;
527170530Ssam			m_freem(m);
528170530Ssam			if (--rap->rxa_qframes == 0)
529170530Ssam				break;
530170530Ssam		}
531170530Ssam	}
532170530Ssam	KASSERT(rap->rxa_qbytes == 0 && rap->rxa_qframes == 0,
533170530Ssam	    ("lost %u data, %u frames on ampdu rx q",
534170530Ssam	    rap->rxa_qbytes, rap->rxa_qframes));
535170530Ssam}
536170530Ssam
537170530Ssam/*
538178354Ssam * Start A-MPDU rx/re-order processing for the specified TID.
539178354Ssam */
540191552Ssamstatic int
541191552Ssamampdu_rx_start(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap,
542191552Ssam	int baparamset, int batimeout, int baseqctl)
543178354Ssam{
544191552Ssam	int bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
545191552Ssam
546178354Ssam	if (rap->rxa_flags & IEEE80211_AGGR_RUNNING) {
547178354Ssam		/*
548178354Ssam		 * AMPDU previously setup and not terminated with a DELBA,
549178354Ssam		 * flush the reorder q's in case anything remains.
550178354Ssam		 */
551178354Ssam		ampdu_rx_purge(rap);
552178354Ssam	}
553178354Ssam	memset(rap, 0, sizeof(*rap));
554178354Ssam	rap->rxa_wnd = (bufsiz == 0) ?
555178354Ssam	    IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
556191552Ssam	rap->rxa_start = MS(baseqctl, IEEE80211_BASEQ_START);
557178354Ssam	rap->rxa_flags |=  IEEE80211_AGGR_RUNNING | IEEE80211_AGGR_XCHGPEND;
558191552Ssam
559191552Ssam	return 0;
560178354Ssam}
561178354Ssam
562178354Ssam/*
563287948Sadrian * Public function; manually setup the RX ampdu state.
564287948Sadrian */
565287948Sadrianint
566287948Sadrianieee80211_ampdu_rx_start_ext(struct ieee80211_node *ni, int tid, int seq, int baw)
567287948Sadrian{
568287948Sadrian	struct ieee80211_rx_ampdu *rap;
569287948Sadrian
570287948Sadrian	/* XXX TODO: sanity check tid, seq, baw */
571287948Sadrian
572287948Sadrian	rap = &ni->ni_rx_ampdu[tid];
573287948Sadrian
574287948Sadrian	if (rap->rxa_flags & IEEE80211_AGGR_RUNNING) {
575287948Sadrian		/*
576287948Sadrian		 * AMPDU previously setup and not terminated with a DELBA,
577287948Sadrian		 * flush the reorder q's in case anything remains.
578287948Sadrian		 */
579287948Sadrian		ampdu_rx_purge(rap);
580287948Sadrian	}
581287948Sadrian
582287948Sadrian	memset(rap, 0, sizeof(*rap));
583287948Sadrian	rap->rxa_wnd = (baw== 0) ?
584287948Sadrian	    IEEE80211_AGGR_BAWMAX : min(baw, IEEE80211_AGGR_BAWMAX);
585287948Sadrian	rap->rxa_start = seq;
586287948Sadrian	rap->rxa_flags |=  IEEE80211_AGGR_RUNNING | IEEE80211_AGGR_XCHGPEND;
587287948Sadrian
588287948Sadrian	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, ni,
589287948Sadrian	    "%s: tid=%d, start=%d, wnd=%d, flags=0x%08x\n",
590287948Sadrian	    __func__,
591287948Sadrian	    tid,
592287948Sadrian	    seq,
593287948Sadrian	    rap->rxa_wnd,
594287948Sadrian	    rap->rxa_flags);
595287948Sadrian
596287948Sadrian	return 0;
597287948Sadrian}
598287948Sadrian
599287948Sadrian/*
600170530Ssam * Stop A-MPDU rx processing for the specified TID.
601170530Ssam */
602170530Ssamstatic void
603191552Ssamampdu_rx_stop(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
604170530Ssam{
605205277Srpaulo
606170530Ssam	ampdu_rx_purge(rap);
607178354Ssam	rap->rxa_flags &= ~(IEEE80211_AGGR_RUNNING | IEEE80211_AGGR_XCHGPEND);
608170530Ssam}
609170530Ssam
610170530Ssam/*
611170530Ssam * Dispatch a frame from the A-MPDU reorder queue.  The
612170530Ssam * frame is fed back into ieee80211_input marked with an
613183247Ssam * M_AMPDU_MPDU flag so it doesn't come back to us (it also
614170530Ssam * permits ieee80211_input to optimize re-processing).
615170530Ssam */
616170530Ssamstatic __inline void
617170530Ssamampdu_dispatch(struct ieee80211_node *ni, struct mbuf *m)
618170530Ssam{
619183247Ssam	m->m_flags |= M_AMPDU_MPDU;	/* bypass normal processing */
620192468Ssam	/* NB: rssi and noise are ignored w/ M_AMPDU_MPDU set */
621192468Ssam	(void) ieee80211_input(ni, m, 0, 0);
622170530Ssam}
623170530Ssam
624170530Ssam/*
625170530Ssam * Dispatch as many frames as possible from the re-order queue.
626170530Ssam * Frames will always be "at the front"; we process all frames
627170530Ssam * up to the first empty slot in the window.  On completion we
628170530Ssam * cleanup state if there are still pending frames in the current
629170530Ssam * BA window.  We assume the frame at slot 0 is already handled
630170530Ssam * by the caller; we always start at slot 1.
631170530Ssam */
632170530Ssamstatic void
633170530Ssamampdu_rx_dispatch(struct ieee80211_rx_ampdu *rap, struct ieee80211_node *ni)
634170530Ssam{
635178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
636170530Ssam	struct mbuf *m;
637170530Ssam	int i;
638170530Ssam
639170530Ssam	/* flush run of frames */
640170530Ssam	for (i = 1; i < rap->rxa_wnd; i++) {
641170530Ssam		m = rap->rxa_m[i];
642170530Ssam		if (m == NULL)
643170530Ssam			break;
644170530Ssam		rap->rxa_m[i] = NULL;
645170530Ssam		rap->rxa_qbytes -= m->m_pkthdr.len;
646170530Ssam		rap->rxa_qframes--;
647170530Ssam
648170530Ssam		ampdu_dispatch(ni, m);
649170530Ssam	}
650170530Ssam	/*
651170530Ssam	 * If frames remain, copy the mbuf pointers down so
652170530Ssam	 * they correspond to the offsets in the new window.
653170530Ssam	 */
654170530Ssam	if (rap->rxa_qframes != 0) {
655170530Ssam		int n = rap->rxa_qframes, j;
656170530Ssam		for (j = i+1; j < rap->rxa_wnd; j++) {
657170530Ssam			if (rap->rxa_m[j] != NULL) {
658170530Ssam				rap->rxa_m[j-i] = rap->rxa_m[j];
659170530Ssam				rap->rxa_m[j] = NULL;
660170530Ssam				if (--n == 0)
661170530Ssam					break;
662170530Ssam			}
663170530Ssam		}
664170530Ssam		KASSERT(n == 0, ("lost %d frames", n));
665178354Ssam		vap->iv_stats.is_ampdu_rx_copy += rap->rxa_qframes;
666170530Ssam	}
667173273Ssam	/*
668173273Ssam	 * Adjust the start of the BA window to
669173273Ssam	 * reflect the frames just dispatched.
670173273Ssam	 */
671173273Ssam	rap->rxa_start = IEEE80211_SEQ_ADD(rap->rxa_start, i);
672178354Ssam	vap->iv_stats.is_ampdu_rx_oor += i;
673170530Ssam}
674170530Ssam
675170530Ssam/*
676173273Ssam * Dispatch all frames in the A-MPDU re-order queue.
677170530Ssam */
678170530Ssamstatic void
679173273Ssamampdu_rx_flush(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
680170530Ssam{
681178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
682170530Ssam	struct mbuf *m;
683170530Ssam	int i;
684170530Ssam
685173273Ssam	for (i = 0; i < rap->rxa_wnd; i++) {
686170530Ssam		m = rap->rxa_m[i];
687170530Ssam		if (m == NULL)
688170530Ssam			continue;
689170530Ssam		rap->rxa_m[i] = NULL;
690170530Ssam		rap->rxa_qbytes -= m->m_pkthdr.len;
691173273Ssam		rap->rxa_qframes--;
692178354Ssam		vap->iv_stats.is_ampdu_rx_oor++;
693173273Ssam
694170530Ssam		ampdu_dispatch(ni, m);
695173273Ssam		if (rap->rxa_qframes == 0)
696170530Ssam			break;
697170530Ssam	}
698170530Ssam}
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		 */
866297774Sadrian
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		}
905297774Sadrian
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,
1065297769Sadrian	    "%s: called (%p)",
1066297769Sadrian	    __func__,
1067297769Sadrian	    ni);
1068242205Sadrian
1069173273Ssam	if (ni->ni_flags & IEEE80211_NODE_HT) {
1070173273Ssam		/*
1071173273Ssam		 * Clean AMPDU state on re-associate.  This handles the case
1072173273Ssam		 * where a station leaves w/o notifying us and then returns
1073173273Ssam		 * before node is reaped for inactivity.
1074173273Ssam		 */
1075242205Sadrian		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
1076242205Sadrian		    ni,
1077297769Sadrian		    "%s: calling cleanup (%p)",
1078297769Sadrian		    __func__, ni);
1079173273Ssam		ieee80211_ht_node_cleanup(ni);
1080173273Ssam	}
1081234324Sadrian	for (tid = 0; tid < WME_NUM_TID; tid++) {
1082234324Sadrian		tap = &ni->ni_tx_ampdu[tid];
1083234324Sadrian		tap->txa_tid = tid;
1084184280Ssam		tap->txa_ni = ni;
1085288315Sadrian		ieee80211_txampdu_init_pps(tap);
1086173273Ssam		/* NB: further initialization deferred */
1087170530Ssam	}
1088173273Ssam	ni->ni_flags |= IEEE80211_NODE_HT | IEEE80211_NODE_AMPDU;
1089170530Ssam}
1090170530Ssam
1091170530Ssam/*
1092170530Ssam * Cleanup HT-specific state in a node.  Called only
1093170530Ssam * when HT use has been marked.
1094170530Ssam */
1095170530Ssamvoid
1096170530Ssamieee80211_ht_node_cleanup(struct ieee80211_node *ni)
1097170530Ssam{
1098191552Ssam	struct ieee80211com *ic = ni->ni_ic;
1099170530Ssam	int i;
1100170530Ssam
1101242205Sadrian	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
1102242205Sadrian	    ni,
1103297769Sadrian	    "%s: called (%p)",
1104297769Sadrian	    __func__, ni);
1105242205Sadrian
1106170530Ssam	KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT node"));
1107170530Ssam
1108170530Ssam	/* XXX optimize this */
1109234324Sadrian	for (i = 0; i < WME_NUM_TID; i++) {
1110170530Ssam		struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[i];
1111184280Ssam		if (tap->txa_flags & IEEE80211_AGGR_SETUP)
1112184280Ssam			ampdu_tx_stop(tap);
1113170530Ssam	}
1114170530Ssam	for (i = 0; i < WME_NUM_TID; i++)
1115191552Ssam		ic->ic_ampdu_rx_stop(ni, &ni->ni_rx_ampdu[i]);
1116170530Ssam
1117170530Ssam	ni->ni_htcap = 0;
1118182828Ssam	ni->ni_flags &= ~IEEE80211_NODE_HT_ALL;
1119170530Ssam}
1120170530Ssam
1121178354Ssam/*
1122178354Ssam * Age out HT resources for a station.
1123178354Ssam */
1124178354Ssamvoid
1125178354Ssamieee80211_ht_node_age(struct ieee80211_node *ni)
1126178354Ssam{
1127178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1128178354Ssam	uint8_t tid;
1129178354Ssam
1130178354Ssam	KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT sta"));
1131178354Ssam
1132178354Ssam	for (tid = 0; tid < WME_NUM_TID; tid++) {
1133178354Ssam		struct ieee80211_rx_ampdu *rap;
1134178354Ssam
1135178354Ssam		rap = &ni->ni_rx_ampdu[tid];
1136178354Ssam		if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0)
1137178354Ssam			continue;
1138178354Ssam		if (rap->rxa_qframes == 0)
1139178354Ssam			continue;
1140178354Ssam		/*
1141178354Ssam		 * Check for frames sitting too long in the reorder queue.
1142178354Ssam		 * See above for more details on what's happening here.
1143178354Ssam		 */
1144178354Ssam		/* XXX honor batimeout? */
1145178354Ssam		if (ticks - rap->rxa_age > ieee80211_ampdu_age) {
1146178354Ssam			/*
1147178354Ssam			 * Too long since we received the first
1148178354Ssam			 * frame; flush the reorder buffer.
1149178354Ssam			 */
1150178354Ssam			vap->iv_stats.is_ampdu_rx_age += rap->rxa_qframes;
1151178354Ssam			ampdu_rx_flush(ni, rap);
1152178354Ssam		}
1153178354Ssam	}
1154178354Ssam}
1155178354Ssam
1156173273Ssamstatic struct ieee80211_channel *
1157173273Ssamfindhtchan(struct ieee80211com *ic, struct ieee80211_channel *c, int htflags)
1158173273Ssam{
1159173273Ssam	return ieee80211_find_channel(ic, c->ic_freq,
1160173273Ssam	    (c->ic_flags &~ IEEE80211_CHAN_HT) | htflags);
1161173273Ssam}
1162173273Ssam
1163173273Ssam/*
1164173273Ssam * Adjust a channel to be HT/non-HT according to the vap's configuration.
1165173273Ssam */
1166173273Ssamstruct ieee80211_channel *
1167173273Ssamieee80211_ht_adjust_channel(struct ieee80211com *ic,
1168173273Ssam	struct ieee80211_channel *chan, int flags)
1169173273Ssam{
1170173273Ssam	struct ieee80211_channel *c;
1171173273Ssam
1172193655Ssam	if (flags & IEEE80211_FHT_HT) {
1173173273Ssam		/* promote to HT if possible */
1174193655Ssam		if (flags & IEEE80211_FHT_USEHT40) {
1175173273Ssam			if (!IEEE80211_IS_CHAN_HT40(chan)) {
1176173273Ssam				/* NB: arbitrarily pick ht40+ over ht40- */
1177173273Ssam				c = findhtchan(ic, chan, IEEE80211_CHAN_HT40U);
1178173273Ssam				if (c == NULL)
1179173273Ssam					c = findhtchan(ic, chan,
1180173273Ssam						IEEE80211_CHAN_HT40D);
1181173273Ssam				if (c == NULL)
1182173273Ssam					c = findhtchan(ic, chan,
1183173273Ssam						IEEE80211_CHAN_HT20);
1184173273Ssam				if (c != NULL)
1185173273Ssam					chan = c;
1186173273Ssam			}
1187173273Ssam		} else if (!IEEE80211_IS_CHAN_HT20(chan)) {
1188173273Ssam			c = findhtchan(ic, chan, IEEE80211_CHAN_HT20);
1189173273Ssam			if (c != NULL)
1190173273Ssam				chan = c;
1191173273Ssam		}
1192173273Ssam	} else if (IEEE80211_IS_CHAN_HT(chan)) {
1193173273Ssam		/* demote to legacy, HT use is disabled */
1194173273Ssam		c = ieee80211_find_channel(ic, chan->ic_freq,
1195173273Ssam		    chan->ic_flags &~ IEEE80211_CHAN_HT);
1196173273Ssam		if (c != NULL)
1197173273Ssam			chan = c;
1198173273Ssam	}
1199173273Ssam	return chan;
1200173273Ssam}
1201173273Ssam
1202173273Ssam/*
1203173273Ssam * Setup HT-specific state for a legacy WDS peer.
1204173273Ssam */
1205173273Ssamvoid
1206173273Ssamieee80211_ht_wds_init(struct ieee80211_node *ni)
1207173273Ssam{
1208178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1209173273Ssam	struct ieee80211_tx_ampdu *tap;
1210234324Sadrian	int tid;
1211173273Ssam
1212193655Ssam	KASSERT(vap->iv_flags_ht & IEEE80211_FHT_HT, ("no HT requested"));
1213173273Ssam
1214173273Ssam	/* XXX check scan cache in case peer has an ap and we have info */
1215173273Ssam	/*
1216173273Ssam	 * If setup with a legacy channel; locate an HT channel.
1217173273Ssam	 * Otherwise if the inherited channel (from a companion
1218173273Ssam	 * AP) is suitable use it so we use the same location
1219173273Ssam	 * for the extension channel).
1220173273Ssam	 */
1221178354Ssam	ni->ni_chan = ieee80211_ht_adjust_channel(ni->ni_ic,
1222178354Ssam	    ni->ni_chan, ieee80211_htchanflags(ni->ni_chan));
1223173273Ssam
1224173273Ssam	ni->ni_htcap = 0;
1225193655Ssam	if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20)
1226173273Ssam		ni->ni_htcap |= IEEE80211_HTCAP_SHORTGI20;
1227173273Ssam	if (IEEE80211_IS_CHAN_HT40(ni->ni_chan)) {
1228173273Ssam		ni->ni_htcap |= IEEE80211_HTCAP_CHWIDTH40;
1229173273Ssam		ni->ni_chw = 40;
1230173273Ssam		if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan))
1231173273Ssam			ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_ABOVE;
1232173273Ssam		else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan))
1233173273Ssam			ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_BELOW;
1234193655Ssam		if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40)
1235173273Ssam			ni->ni_htcap |= IEEE80211_HTCAP_SHORTGI40;
1236173273Ssam	} else {
1237173273Ssam		ni->ni_chw = 20;
1238173273Ssam		ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_NONE;
1239173273Ssam	}
1240173273Ssam	ni->ni_htctlchan = ni->ni_chan->ic_ieee;
1241193655Ssam	if (vap->iv_flags_ht & IEEE80211_FHT_RIFS)
1242183256Ssam		ni->ni_flags |= IEEE80211_NODE_RIFS;
1243183256Ssam	/* XXX does it make sense to enable SMPS? */
1244173273Ssam
1245173273Ssam	ni->ni_htopmode = 0;		/* XXX need protection state */
1246173273Ssam	ni->ni_htstbc = 0;		/* XXX need info */
1247173273Ssam
1248234324Sadrian	for (tid = 0; tid < WME_NUM_TID; tid++) {
1249234324Sadrian		tap = &ni->ni_tx_ampdu[tid];
1250234324Sadrian		tap->txa_tid = tid;
1251288315Sadrian		ieee80211_txampdu_init_pps(tap);
1252173273Ssam	}
1253193655Ssam	/* NB: AMPDU tx/rx governed by IEEE80211_FHT_AMPDU_{TX,RX} */
1254173273Ssam	ni->ni_flags |= IEEE80211_NODE_HT | IEEE80211_NODE_AMPDU;
1255173273Ssam}
1256173273Ssam
1257173273Ssam/*
1258173273Ssam * Notify hostap vaps of a change in the HTINFO ie.
1259173273Ssam */
1260173273Ssamstatic void
1261173273Ssamhtinfo_notify(struct ieee80211com *ic)
1262173273Ssam{
1263178354Ssam	struct ieee80211vap *vap;
1264178354Ssam	int first = 1;
1265178354Ssam
1266178354Ssam	IEEE80211_LOCK_ASSERT(ic);
1267178354Ssam
1268178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1269178354Ssam		if (vap->iv_opmode != IEEE80211_M_HOSTAP)
1270178354Ssam			continue;
1271183253Ssam		if (vap->iv_state != IEEE80211_S_RUN ||
1272183253Ssam		    !IEEE80211_IS_CHAN_HT(vap->iv_bss->ni_chan))
1273183253Ssam			continue;
1274178354Ssam		if (first) {
1275178354Ssam			IEEE80211_NOTE(vap,
1276178354Ssam			    IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N,
1277178354Ssam			    vap->iv_bss,
1278178354Ssam			    "HT bss occupancy change: %d sta, %d ht, "
1279178354Ssam			    "%d ht40%s, HT protmode now 0x%x"
1280178354Ssam			    , ic->ic_sta_assoc
1281178354Ssam			    , ic->ic_ht_sta_assoc
1282178354Ssam			    , ic->ic_ht40_sta_assoc
1283193655Ssam			    , (ic->ic_flags_ht & IEEE80211_FHT_NONHT_PR) ?
1284178354Ssam				 ", non-HT sta present" : ""
1285178354Ssam			    , ic->ic_curhtprotmode);
1286178354Ssam			first = 0;
1287178354Ssam		}
1288178354Ssam		ieee80211_beacon_notify(vap, IEEE80211_BEACON_HTINFO);
1289178354Ssam	}
1290173273Ssam}
1291173273Ssam
1292173273Ssam/*
1293173273Ssam * Calculate HT protection mode from current
1294173273Ssam * state and handle updates.
1295173273Ssam */
1296173273Ssamstatic void
1297173273Ssamhtinfo_update(struct ieee80211com *ic)
1298173273Ssam{
1299173273Ssam	uint8_t protmode;
1300173273Ssam
1301178354Ssam	if (ic->ic_sta_assoc != ic->ic_ht_sta_assoc) {
1302178354Ssam		protmode = IEEE80211_HTINFO_OPMODE_MIXED
1303178354Ssam			 | IEEE80211_HTINFO_NONHT_PRESENT;
1304193655Ssam	} else if (ic->ic_flags_ht & IEEE80211_FHT_NONHT_PR) {
1305173273Ssam		protmode = IEEE80211_HTINFO_OPMODE_PROTOPT
1306178354Ssam			 | IEEE80211_HTINFO_NONHT_PRESENT;
1307178354Ssam	} else if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
1308178354Ssam	    IEEE80211_IS_CHAN_HT40(ic->ic_bsschan) &&
1309173273Ssam	    ic->ic_sta_assoc != ic->ic_ht40_sta_assoc) {
1310173273Ssam		protmode = IEEE80211_HTINFO_OPMODE_HT20PR;
1311173273Ssam	} else {
1312173273Ssam		protmode = IEEE80211_HTINFO_OPMODE_PURE;
1313173273Ssam	}
1314173273Ssam	if (protmode != ic->ic_curhtprotmode) {
1315173273Ssam		ic->ic_curhtprotmode = protmode;
1316173273Ssam		htinfo_notify(ic);
1317173273Ssam	}
1318173273Ssam}
1319173273Ssam
1320173273Ssam/*
1321173273Ssam * Handle an HT station joining a BSS.
1322173273Ssam */
1323173273Ssamvoid
1324173273Ssamieee80211_ht_node_join(struct ieee80211_node *ni)
1325173273Ssam{
1326173273Ssam	struct ieee80211com *ic = ni->ni_ic;
1327173273Ssam
1328173273Ssam	IEEE80211_LOCK_ASSERT(ic);
1329173273Ssam
1330173273Ssam	if (ni->ni_flags & IEEE80211_NODE_HT) {
1331173273Ssam		ic->ic_ht_sta_assoc++;
1332173273Ssam		if (ni->ni_chw == 40)
1333173273Ssam			ic->ic_ht40_sta_assoc++;
1334173273Ssam	}
1335173273Ssam	htinfo_update(ic);
1336173273Ssam}
1337173273Ssam
1338173273Ssam/*
1339173273Ssam * Handle an HT station leaving a BSS.
1340173273Ssam */
1341173273Ssamvoid
1342173273Ssamieee80211_ht_node_leave(struct ieee80211_node *ni)
1343173273Ssam{
1344173273Ssam	struct ieee80211com *ic = ni->ni_ic;
1345173273Ssam
1346173273Ssam	IEEE80211_LOCK_ASSERT(ic);
1347173273Ssam
1348173273Ssam	if (ni->ni_flags & IEEE80211_NODE_HT) {
1349173273Ssam		ic->ic_ht_sta_assoc--;
1350173273Ssam		if (ni->ni_chw == 40)
1351173273Ssam			ic->ic_ht40_sta_assoc--;
1352173273Ssam	}
1353173273Ssam	htinfo_update(ic);
1354173273Ssam}
1355173273Ssam
1356173273Ssam/*
1357173273Ssam * Public version of htinfo_update; used for processing
1358178354Ssam * beacon frames from overlapping bss.
1359178354Ssam *
1360178354Ssam * Caller can specify either IEEE80211_HTINFO_OPMODE_MIXED
1361178354Ssam * (on receipt of a beacon that advertises MIXED) or
1362178354Ssam * IEEE80211_HTINFO_OPMODE_PROTOPT (on receipt of a beacon
1363178354Ssam * from an overlapping legacy bss).  We treat MIXED with
1364178354Ssam * a higher precedence than PROTOPT (i.e. we will not change
1365178354Ssam * change PROTOPT -> MIXED; only MIXED -> PROTOPT).  This
1366178354Ssam * corresponds to how we handle things in htinfo_update.
1367173273Ssam */
1368173273Ssamvoid
1369178354Ssamieee80211_htprot_update(struct ieee80211com *ic, int protmode)
1370173273Ssam{
1371178354Ssam#define	OPMODE(x)	SM(x, IEEE80211_HTINFO_OPMODE)
1372183246Ssam	IEEE80211_LOCK(ic);
1373178354Ssam
1374178354Ssam	/* track non-HT station presence */
1375178354Ssam	KASSERT(protmode & IEEE80211_HTINFO_NONHT_PRESENT,
1376183246Ssam	    ("protmode 0x%x", protmode));
1377193655Ssam	ic->ic_flags_ht |= IEEE80211_FHT_NONHT_PR;
1378178354Ssam	ic->ic_lastnonht = ticks;
1379178354Ssam
1380183246Ssam	if (protmode != ic->ic_curhtprotmode &&
1381183246Ssam	    (OPMODE(ic->ic_curhtprotmode) != IEEE80211_HTINFO_OPMODE_MIXED ||
1382183246Ssam	     OPMODE(protmode) == IEEE80211_HTINFO_OPMODE_PROTOPT)) {
1383183246Ssam		/* push beacon update */
1384183246Ssam		ic->ic_curhtprotmode = protmode;
1385183246Ssam		htinfo_notify(ic);
1386183246Ssam	}
1387181197Ssam	IEEE80211_UNLOCK(ic);
1388178354Ssam#undef OPMODE
1389173273Ssam}
1390173273Ssam
1391173273Ssam/*
1392173273Ssam * Time out presence of an overlapping bss with non-HT
1393173273Ssam * stations.  When operating in hostap mode we listen for
1394173273Ssam * beacons from other stations and if we identify a non-HT
1395173273Ssam * station is present we update the opmode field of the
1396173273Ssam * HTINFO ie.  To identify when all non-HT stations are
1397173273Ssam * gone we time out this condition.
1398173273Ssam */
1399173273Ssamvoid
1400173273Ssamieee80211_ht_timeout(struct ieee80211com *ic)
1401173273Ssam{
1402173273Ssam	IEEE80211_LOCK_ASSERT(ic);
1403173273Ssam
1404193655Ssam	if ((ic->ic_flags_ht & IEEE80211_FHT_NONHT_PR) &&
1405297405Sadrian	    ieee80211_time_after(ticks, ic->ic_lastnonht + IEEE80211_NONHT_PRESENT_AGE)) {
1406173273Ssam#if 0
1407178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
1408173273Ssam		    "%s", "time out non-HT STA present on channel");
1409173273Ssam#endif
1410193655Ssam		ic->ic_flags_ht &= ~IEEE80211_FHT_NONHT_PR;
1411173273Ssam		htinfo_update(ic);
1412173273Ssam	}
1413173273Ssam}
1414173273Ssam
1415170530Ssam/*
1416170530Ssam * Process an 802.11n HT capabilities ie.
1417170530Ssam */
1418170530Ssamvoid
1419170530Ssamieee80211_parse_htcap(struct ieee80211_node *ni, const uint8_t *ie)
1420170530Ssam{
1421170530Ssam	if (ie[0] == IEEE80211_ELEMID_VENDOR) {
1422170530Ssam		/*
1423170530Ssam		 * Station used Vendor OUI ie to associate;
1424170530Ssam		 * mark the node so when we respond we'll use
1425170530Ssam		 * the Vendor OUI's and not the standard ie's.
1426170530Ssam		 */
1427170530Ssam		ni->ni_flags |= IEEE80211_NODE_HTCOMPAT;
1428170530Ssam		ie += 4;
1429170530Ssam	} else
1430170530Ssam		ni->ni_flags &= ~IEEE80211_NODE_HTCOMPAT;
1431170530Ssam
1432298359Savos	ni->ni_htcap = le16dec(ie +
1433170530Ssam		__offsetof(struct ieee80211_ie_htcap, hc_cap));
1434170530Ssam	ni->ni_htparam = ie[__offsetof(struct ieee80211_ie_htcap, hc_param)];
1435170530Ssam}
1436170530Ssam
1437183254Ssamstatic void
1438183254Ssamhtinfo_parse(struct ieee80211_node *ni,
1439183254Ssam	const struct ieee80211_ie_htinfo *htinfo)
1440170530Ssam{
1441170530Ssam	uint16_t w;
1442170530Ssam
1443170530Ssam	ni->ni_htctlchan = htinfo->hi_ctrlchannel;
1444170530Ssam	ni->ni_ht2ndchan = SM(htinfo->hi_byte1, IEEE80211_HTINFO_2NDCHAN);
1445298359Savos	w = le16dec(&htinfo->hi_byte2);
1446170530Ssam	ni->ni_htopmode = SM(w, IEEE80211_HTINFO_OPMODE);
1447298359Savos	w = le16dec(&htinfo->hi_byte45);
1448170530Ssam	ni->ni_htstbc = SM(w, IEEE80211_HTINFO_BASIC_STBCMCS);
1449183254Ssam}
1450183254Ssam
1451183254Ssam/*
1452183254Ssam * Parse an 802.11n HT info ie and save useful information
1453183254Ssam * to the node state.  Note this does not effect any state
1454183254Ssam * changes such as for channel width change.
1455183254Ssam */
1456183254Ssamvoid
1457183254Ssamieee80211_parse_htinfo(struct ieee80211_node *ni, const uint8_t *ie)
1458183254Ssam{
1459183254Ssam	if (ie[0] == IEEE80211_ELEMID_VENDOR)
1460183254Ssam		ie += 4;
1461183254Ssam	htinfo_parse(ni, (const struct ieee80211_ie_htinfo *) ie);
1462183254Ssam}
1463183254Ssam
1464183254Ssam/*
1465183254Ssam * Handle 11n channel switch.  Use the received HT ie's to
1466183254Ssam * identify the right channel to use.  If we cannot locate it
1467183254Ssam * in the channel table then fallback to legacy operation.
1468183254Ssam * Note that we use this information to identify the node's
1469183254Ssam * channel only; the caller is responsible for insuring any
1470183254Ssam * required channel change is done (e.g. in sta mode when
1471183254Ssam * parsing the contents of a beacon frame).
1472183254Ssam */
1473233452Sadrianstatic int
1474183254Ssamhtinfo_update_chw(struct ieee80211_node *ni, int htflags)
1475183254Ssam{
1476183254Ssam	struct ieee80211com *ic = ni->ni_ic;
1477183254Ssam	struct ieee80211_channel *c;
1478183254Ssam	int chanflags;
1479233452Sadrian	int ret = 0;
1480183254Ssam
1481173273Ssam	chanflags = (ni->ni_chan->ic_flags &~ IEEE80211_CHAN_HT) | htflags;
1482173273Ssam	if (chanflags != ni->ni_chan->ic_flags) {
1483183254Ssam		/* XXX not right for ht40- */
1484173273Ssam		c = ieee80211_find_channel(ic, ni->ni_chan->ic_freq, chanflags);
1485178354Ssam		if (c == NULL && (htflags & IEEE80211_CHAN_HT40)) {
1486173273Ssam			/*
1487173273Ssam			 * No HT40 channel entry in our table; fall back
1488173273Ssam			 * to HT20 operation.  This should not happen.
1489173273Ssam			 */
1490173273Ssam			c = findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT20);
1491183254Ssam#if 0
1492183254Ssam			IEEE80211_NOTE(ni->ni_vap,
1493173273Ssam			    IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
1494173273Ssam			    "no HT40 channel (freq %u), falling back to HT20",
1495173273Ssam			    ni->ni_chan->ic_freq);
1496183254Ssam#endif
1497173273Ssam			/* XXX stat */
1498173273Ssam		}
1499173273Ssam		if (c != NULL && c != ni->ni_chan) {
1500183254Ssam			IEEE80211_NOTE(ni->ni_vap,
1501173273Ssam			    IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
1502173273Ssam			    "switch station to HT%d channel %u/0x%x",
1503173273Ssam			    IEEE80211_IS_CHAN_HT40(c) ? 40 : 20,
1504173273Ssam			    c->ic_freq, c->ic_flags);
1505173273Ssam			ni->ni_chan = c;
1506233452Sadrian			ret = 1;
1507173273Ssam		}
1508173273Ssam		/* NB: caller responsible for forcing any channel change */
1509173273Ssam	}
1510173273Ssam	/* update node's tx channel width */
1511173273Ssam	ni->ni_chw = IEEE80211_IS_CHAN_HT40(ni->ni_chan)? 40 : 20;
1512233452Sadrian	return (ret);
1513170530Ssam}
1514170530Ssam
1515170530Ssam/*
1516183255Ssam * Update 11n MIMO PS state according to received htcap.
1517183255Ssam */
1518183255Ssamstatic __inline int
1519183255Ssamhtcap_update_mimo_ps(struct ieee80211_node *ni)
1520183255Ssam{
1521183255Ssam	uint16_t oflags = ni->ni_flags;
1522183255Ssam
1523183255Ssam	switch (ni->ni_htcap & IEEE80211_HTCAP_SMPS) {
1524183255Ssam	case IEEE80211_HTCAP_SMPS_DYNAMIC:
1525183255Ssam		ni->ni_flags |= IEEE80211_NODE_MIMO_PS;
1526183255Ssam		ni->ni_flags |= IEEE80211_NODE_MIMO_RTS;
1527183255Ssam		break;
1528183255Ssam	case IEEE80211_HTCAP_SMPS_ENA:
1529183255Ssam		ni->ni_flags |= IEEE80211_NODE_MIMO_PS;
1530183255Ssam		ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS;
1531183255Ssam		break;
1532183255Ssam	case IEEE80211_HTCAP_SMPS_OFF:
1533183255Ssam	default:		/* disable on rx of reserved value */
1534183255Ssam		ni->ni_flags &= ~IEEE80211_NODE_MIMO_PS;
1535183255Ssam		ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS;
1536183255Ssam		break;
1537183255Ssam	}
1538183255Ssam	return (oflags ^ ni->ni_flags);
1539183255Ssam}
1540183255Ssam
1541183255Ssam/*
1542183257Ssam * Update short GI state according to received htcap
1543183257Ssam * and local settings.
1544183257Ssam */
1545183257Ssamstatic __inline void
1546183257Ssamhtcap_update_shortgi(struct ieee80211_node *ni)
1547183257Ssam{
1548183257Ssam	struct ieee80211vap *vap = ni->ni_vap;
1549183257Ssam
1550183257Ssam	ni->ni_flags &= ~(IEEE80211_NODE_SGI20|IEEE80211_NODE_SGI40);
1551183257Ssam	if ((ni->ni_htcap & IEEE80211_HTCAP_SHORTGI20) &&
1552193655Ssam	    (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20))
1553183257Ssam		ni->ni_flags |= IEEE80211_NODE_SGI20;
1554183257Ssam	if ((ni->ni_htcap & IEEE80211_HTCAP_SHORTGI40) &&
1555193655Ssam	    (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40))
1556183257Ssam		ni->ni_flags |= IEEE80211_NODE_SGI40;
1557183257Ssam}
1558183257Ssam
1559183257Ssam/*
1560183254Ssam * Parse and update HT-related state extracted from
1561183254Ssam * the HT cap and info ie's.
1562183254Ssam */
1563233452Sadrianint
1564183254Ssamieee80211_ht_updateparams(struct ieee80211_node *ni,
1565183254Ssam	const uint8_t *htcapie, const uint8_t *htinfoie)
1566183254Ssam{
1567183254Ssam	struct ieee80211vap *vap = ni->ni_vap;
1568183254Ssam	const struct ieee80211_ie_htinfo *htinfo;
1569183254Ssam	int htflags;
1570233452Sadrian	int ret = 0;
1571183254Ssam
1572183254Ssam	ieee80211_parse_htcap(ni, htcapie);
1573183255Ssam	if (vap->iv_htcaps & IEEE80211_HTCAP_SMPS)
1574183255Ssam		htcap_update_mimo_ps(ni);
1575183257Ssam	htcap_update_shortgi(ni);
1576183254Ssam
1577183254Ssam	if (htinfoie[0] == IEEE80211_ELEMID_VENDOR)
1578183254Ssam		htinfoie += 4;
1579183254Ssam	htinfo = (const struct ieee80211_ie_htinfo *) htinfoie;
1580183254Ssam	htinfo_parse(ni, htinfo);
1581183254Ssam
1582193655Ssam	htflags = (vap->iv_flags_ht & IEEE80211_FHT_HT) ?
1583183254Ssam	    IEEE80211_CHAN_HT20 : 0;
1584183254Ssam	/* NB: honor operating mode constraint */
1585183254Ssam	if ((htinfo->hi_byte1 & IEEE80211_HTINFO_TXWIDTH_2040) &&
1586193655Ssam	    (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)) {
1587183254Ssam		if (ni->ni_ht2ndchan == IEEE80211_HTINFO_2NDCHAN_ABOVE)
1588183254Ssam			htflags = IEEE80211_CHAN_HT40U;
1589183254Ssam		else if (ni->ni_ht2ndchan == IEEE80211_HTINFO_2NDCHAN_BELOW)
1590183254Ssam			htflags = IEEE80211_CHAN_HT40D;
1591183254Ssam	}
1592233452Sadrian	if (htinfo_update_chw(ni, htflags))
1593233452Sadrian		ret = 1;
1594183256Ssam
1595183256Ssam	if ((htinfo->hi_byte1 & IEEE80211_HTINFO_RIFSMODE_PERM) &&
1596193655Ssam	    (vap->iv_flags_ht & IEEE80211_FHT_RIFS))
1597183256Ssam		ni->ni_flags |= IEEE80211_NODE_RIFS;
1598183256Ssam	else
1599183256Ssam		ni->ni_flags &= ~IEEE80211_NODE_RIFS;
1600233452Sadrian
1601233452Sadrian	return (ret);
1602183254Ssam}
1603183254Ssam
1604183254Ssam/*
1605183254Ssam * Parse and update HT-related state extracted from the HT cap ie
1606183254Ssam * for a station joining an HT BSS.
1607183254Ssam */
1608183254Ssamvoid
1609183254Ssamieee80211_ht_updatehtcap(struct ieee80211_node *ni, const uint8_t *htcapie)
1610183254Ssam{
1611183254Ssam	struct ieee80211vap *vap = ni->ni_vap;
1612183254Ssam	int htflags;
1613183254Ssam
1614183254Ssam	ieee80211_parse_htcap(ni, htcapie);
1615183255Ssam	if (vap->iv_htcaps & IEEE80211_HTCAP_SMPS)
1616183255Ssam		htcap_update_mimo_ps(ni);
1617183257Ssam	htcap_update_shortgi(ni);
1618183254Ssam
1619183254Ssam	/* NB: honor operating mode constraint */
1620219606Sbschmidt	/* XXX 40 MHz intolerant */
1621193655Ssam	htflags = (vap->iv_flags_ht & IEEE80211_FHT_HT) ?
1622183254Ssam	    IEEE80211_CHAN_HT20 : 0;
1623183254Ssam	if ((ni->ni_htcap & IEEE80211_HTCAP_CHWIDTH40) &&
1624193655Ssam	    (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)) {
1625183254Ssam		if (IEEE80211_IS_CHAN_HT40U(vap->iv_bss->ni_chan))
1626183254Ssam			htflags = IEEE80211_CHAN_HT40U;
1627183254Ssam		else if (IEEE80211_IS_CHAN_HT40D(vap->iv_bss->ni_chan))
1628183254Ssam			htflags = IEEE80211_CHAN_HT40D;
1629183254Ssam	}
1630233452Sadrian	(void) htinfo_update_chw(ni, htflags);
1631183254Ssam}
1632183254Ssam
1633183254Ssam/*
1634170530Ssam * Install received HT rate set by parsing the HT cap ie.
1635170530Ssam */
1636170530Ssamint
1637170530Ssamieee80211_setup_htrates(struct ieee80211_node *ni, const uint8_t *ie, int flags)
1638170530Ssam{
1639219602Sbschmidt	struct ieee80211com *ic = ni->ni_ic;
1640178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1641170530Ssam	const struct ieee80211_ie_htcap *htcap;
1642170530Ssam	struct ieee80211_htrateset *rs;
1643219602Sbschmidt	int i, maxequalmcs, maxunequalmcs;
1644170530Ssam
1645219602Sbschmidt	maxequalmcs = ic->ic_txstream * 8 - 1;
1646300232Savos	maxunequalmcs = 0;
1647219602Sbschmidt	if (ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL) {
1648219602Sbschmidt		if (ic->ic_txstream >= 2)
1649219602Sbschmidt			maxunequalmcs = 38;
1650219602Sbschmidt		if (ic->ic_txstream >= 3)
1651219602Sbschmidt			maxunequalmcs = 52;
1652219602Sbschmidt		if (ic->ic_txstream >= 4)
1653219602Sbschmidt			maxunequalmcs = 76;
1654300232Savos	}
1655219602Sbschmidt
1656170530Ssam	rs = &ni->ni_htrates;
1657170530Ssam	memset(rs, 0, sizeof(*rs));
1658170530Ssam	if (ie != NULL) {
1659170530Ssam		if (ie[0] == IEEE80211_ELEMID_VENDOR)
1660170530Ssam			ie += 4;
1661170530Ssam		htcap = (const struct ieee80211_ie_htcap *) ie;
1662170530Ssam		for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++) {
1663170530Ssam			if (isclr(htcap->hc_mcsset, i))
1664170530Ssam				continue;
1665170530Ssam			if (rs->rs_nrates == IEEE80211_HTRATE_MAXSIZE) {
1666178354Ssam				IEEE80211_NOTE(vap,
1667170530Ssam				    IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
1668170530Ssam				    "WARNING, HT rate set too large; only "
1669170530Ssam				    "using %u rates", IEEE80211_HTRATE_MAXSIZE);
1670178354Ssam				vap->iv_stats.is_rx_rstoobig++;
1671170530Ssam				break;
1672170530Ssam			}
1673219602Sbschmidt			if (i <= 31 && i > maxequalmcs)
1674219602Sbschmidt				continue;
1675219602Sbschmidt			if (i == 32 &&
1676219602Sbschmidt			    (ic->ic_htcaps & IEEE80211_HTC_TXMCS32) == 0)
1677219602Sbschmidt				continue;
1678219602Sbschmidt			if (i > 32 && i > maxunequalmcs)
1679219602Sbschmidt				continue;
1680170530Ssam			rs->rs_rates[rs->rs_nrates++] = i;
1681170530Ssam		}
1682170530Ssam	}
1683170530Ssam	return ieee80211_fix_rate(ni, (struct ieee80211_rateset *) rs, flags);
1684170530Ssam}
1685170530Ssam
1686170530Ssam/*
1687170530Ssam * Mark rates in a node's HT rate set as basic according
1688170530Ssam * to the information in the supplied HT info ie.
1689170530Ssam */
1690170530Ssamvoid
1691170530Ssamieee80211_setup_basic_htrates(struct ieee80211_node *ni, const uint8_t *ie)
1692170530Ssam{
1693170530Ssam	const struct ieee80211_ie_htinfo *htinfo;
1694170530Ssam	struct ieee80211_htrateset *rs;
1695170530Ssam	int i, j;
1696170530Ssam
1697170530Ssam	if (ie[0] == IEEE80211_ELEMID_VENDOR)
1698170530Ssam		ie += 4;
1699170530Ssam	htinfo = (const struct ieee80211_ie_htinfo *) ie;
1700170530Ssam	rs = &ni->ni_htrates;
1701170530Ssam	if (rs->rs_nrates == 0) {
1702178354Ssam		IEEE80211_NOTE(ni->ni_vap,
1703170530Ssam		    IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
1704170530Ssam		    "%s", "WARNING, empty HT rate set");
1705170530Ssam		return;
1706170530Ssam	}
1707170530Ssam	for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++) {
1708170530Ssam		if (isclr(htinfo->hi_basicmcsset, i))
1709170530Ssam			continue;
1710170530Ssam		for (j = 0; j < rs->rs_nrates; j++)
1711170530Ssam			if ((rs->rs_rates[j] & IEEE80211_RATE_VAL) == i)
1712170530Ssam				rs->rs_rates[j] |= IEEE80211_RATE_BASIC;
1713170530Ssam	}
1714170530Ssam}
1715170530Ssam
1716170530Ssamstatic void
1717184280Ssamampdu_tx_setup(struct ieee80211_tx_ampdu *tap)
1718184280Ssam{
1719283291Sjkim	callout_init(&tap->txa_timer, 1);
1720184280Ssam	tap->txa_flags |= IEEE80211_AGGR_SETUP;
1721280349Sadrian	tap->txa_lastsample = ticks;
1722184280Ssam}
1723184280Ssam
1724184280Ssamstatic void
1725184280Ssamampdu_tx_stop(struct ieee80211_tx_ampdu *tap)
1726184280Ssam{
1727184280Ssam	struct ieee80211_node *ni = tap->txa_ni;
1728184280Ssam	struct ieee80211com *ic = ni->ni_ic;
1729184280Ssam
1730242205Sadrian	IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
1731242205Sadrian	    tap->txa_ni,
1732242205Sadrian	    "%s: called",
1733242205Sadrian	    __func__);
1734242205Sadrian
1735184280Ssam	KASSERT(tap->txa_flags & IEEE80211_AGGR_SETUP,
1736234324Sadrian	    ("txa_flags 0x%x tid %d ac %d", tap->txa_flags, tap->txa_tid,
1737234324Sadrian	    TID_TO_WME_AC(tap->txa_tid)));
1738184280Ssam
1739184280Ssam	/*
1740184280Ssam	 * Stop BA stream if setup so driver has a chance
1741184280Ssam	 * to reclaim any resources it might have allocated.
1742184280Ssam	 */
1743184280Ssam	ic->ic_addba_stop(ni, tap);
1744184280Ssam	/*
1745184280Ssam	 * Stop any pending BAR transmit.
1746184280Ssam	 */
1747184280Ssam	bar_stop_timer(tap);
1748184280Ssam
1749280349Sadrian	/*
1750280349Sadrian	 * Reset packet estimate.
1751280349Sadrian	 */
1752288315Sadrian	ieee80211_txampdu_init_pps(tap);
1753280349Sadrian
1754184280Ssam	/* NB: clearing NAK means we may re-send ADDBA */
1755184280Ssam	tap->txa_flags &= ~(IEEE80211_AGGR_SETUP | IEEE80211_AGGR_NAK);
1756184280Ssam}
1757184280Ssam
1758223331Sadrian/*
1759223331Sadrian * ADDBA response timeout.
1760223331Sadrian *
1761223331Sadrian * If software aggregation and per-TID queue management was done here,
1762223331Sadrian * that queue would be unpaused after the ADDBA timeout occurs.
1763223331Sadrian */
1764184280Ssamstatic void
1765170530Ssamaddba_timeout(void *arg)
1766170530Ssam{
1767170530Ssam	struct ieee80211_tx_ampdu *tap = arg;
1768223331Sadrian	struct ieee80211_node *ni = tap->txa_ni;
1769223331Sadrian	struct ieee80211com *ic = ni->ni_ic;
1770170530Ssam
1771170530Ssam	/* XXX ? */
1772170530Ssam	tap->txa_flags &= ~IEEE80211_AGGR_XCHGPEND;
1773170530Ssam	tap->txa_attempts++;
1774223331Sadrian	ic->ic_addba_response_timeout(ni, tap);
1775170530Ssam}
1776170530Ssam
1777170530Ssamstatic void
1778170530Ssamaddba_start_timeout(struct ieee80211_tx_ampdu *tap)
1779170530Ssam{
1780170530Ssam	/* XXX use CALLOUT_PENDING instead? */
1781178354Ssam	callout_reset(&tap->txa_timer, ieee80211_addba_timeout,
1782170530Ssam	    addba_timeout, tap);
1783170530Ssam	tap->txa_flags |= IEEE80211_AGGR_XCHGPEND;
1784178354Ssam	tap->txa_nextrequest = ticks + ieee80211_addba_timeout;
1785170530Ssam}
1786170530Ssam
1787170530Ssamstatic void
1788170530Ssamaddba_stop_timeout(struct ieee80211_tx_ampdu *tap)
1789170530Ssam{
1790170530Ssam	/* XXX use CALLOUT_PENDING instead? */
1791170530Ssam	if (tap->txa_flags & IEEE80211_AGGR_XCHGPEND) {
1792170530Ssam		callout_stop(&tap->txa_timer);
1793170530Ssam		tap->txa_flags &= ~IEEE80211_AGGR_XCHGPEND;
1794170530Ssam	}
1795170530Ssam}
1796170530Ssam
1797223331Sadrianstatic void
1798223331Sadriannull_addba_response_timeout(struct ieee80211_node *ni,
1799223331Sadrian    struct ieee80211_tx_ampdu *tap)
1800223331Sadrian{
1801223331Sadrian}
1802223331Sadrian
1803170530Ssam/*
1804170530Ssam * Default method for requesting A-MPDU tx aggregation.
1805170530Ssam * We setup the specified state block and start a timer
1806170530Ssam * to wait for an ADDBA response frame.
1807170530Ssam */
1808170530Ssamstatic int
1809170530Ssamieee80211_addba_request(struct ieee80211_node *ni,
1810170530Ssam	struct ieee80211_tx_ampdu *tap,
1811170530Ssam	int dialogtoken, int baparamset, int batimeout)
1812170530Ssam{
1813170530Ssam	int bufsiz;
1814170530Ssam
1815170530Ssam	/* XXX locking */
1816170530Ssam	tap->txa_token = dialogtoken;
1817170530Ssam	tap->txa_flags |= IEEE80211_AGGR_IMMEDIATE;
1818170530Ssam	bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
1819170530Ssam	tap->txa_wnd = (bufsiz == 0) ?
1820170530Ssam	    IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
1821170530Ssam	addba_start_timeout(tap);
1822170530Ssam	return 1;
1823170530Ssam}
1824170530Ssam
1825170530Ssam/*
1826288085Sadrian * Called by drivers that wish to request an ADDBA session be
1827288085Sadrian * setup.  This brings it up and starts the request timer.
1828288085Sadrian */
1829288085Sadrianint
1830288085Sadrianieee80211_ampdu_tx_request_ext(struct ieee80211_node *ni, int tid)
1831288085Sadrian{
1832288085Sadrian	struct ieee80211_tx_ampdu *tap;
1833288085Sadrian
1834288085Sadrian	if (tid < 0 || tid > 15)
1835288085Sadrian		return (0);
1836288085Sadrian	tap = &ni->ni_tx_ampdu[tid];
1837288085Sadrian
1838288085Sadrian	/* XXX locking */
1839288085Sadrian	if ((tap->txa_flags & IEEE80211_AGGR_SETUP) == 0) {
1840288085Sadrian		/* do deferred setup of state */
1841288085Sadrian		ampdu_tx_setup(tap);
1842288085Sadrian	}
1843288085Sadrian	/* XXX hack for not doing proper locking */
1844288085Sadrian	tap->txa_flags &= ~IEEE80211_AGGR_NAK;
1845288085Sadrian	addba_start_timeout(tap);
1846288085Sadrian	return (1);
1847288085Sadrian}
1848288085Sadrian
1849288085Sadrian/*
1850288085Sadrian * Called by drivers that have marked a session as active.
1851288085Sadrian */
1852288085Sadrianint
1853288085Sadrianieee80211_ampdu_tx_request_active_ext(struct ieee80211_node *ni, int tid,
1854288085Sadrian    int status)
1855288085Sadrian{
1856288085Sadrian	struct ieee80211_tx_ampdu *tap;
1857288085Sadrian
1858288085Sadrian	if (tid < 0 || tid > 15)
1859288085Sadrian		return (0);
1860288085Sadrian	tap = &ni->ni_tx_ampdu[tid];
1861288085Sadrian
1862288085Sadrian	/* XXX locking */
1863288085Sadrian	addba_stop_timeout(tap);
1864288085Sadrian	if (status == 1) {
1865288085Sadrian		tap->txa_flags |= IEEE80211_AGGR_RUNNING;
1866288085Sadrian		tap->txa_attempts = 0;
1867288085Sadrian	} else {
1868288085Sadrian		/* mark tid so we don't try again */
1869288085Sadrian		tap->txa_flags |= IEEE80211_AGGR_NAK;
1870288085Sadrian	}
1871288085Sadrian	return (1);
1872288085Sadrian}
1873288085Sadrian
1874288085Sadrian/*
1875170530Ssam * Default method for processing an A-MPDU tx aggregation
1876170530Ssam * response.  We shutdown any pending timer and update the
1877170530Ssam * state block according to the reply.
1878170530Ssam */
1879170530Ssamstatic int
1880170530Ssamieee80211_addba_response(struct ieee80211_node *ni,
1881170530Ssam	struct ieee80211_tx_ampdu *tap,
1882170530Ssam	int status, int baparamset, int batimeout)
1883170530Ssam{
1884184280Ssam	int bufsiz, tid;
1885170530Ssam
1886170530Ssam	/* XXX locking */
1887170530Ssam	addba_stop_timeout(tap);
1888170530Ssam	if (status == IEEE80211_STATUS_SUCCESS) {
1889170530Ssam		bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
1890170530Ssam		/* XXX override our request? */
1891170530Ssam		tap->txa_wnd = (bufsiz == 0) ?
1892170530Ssam		    IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
1893184280Ssam		/* XXX AC/TID */
1894184280Ssam		tid = MS(baparamset, IEEE80211_BAPS_TID);
1895170530Ssam		tap->txa_flags |= IEEE80211_AGGR_RUNNING;
1896184280Ssam		tap->txa_attempts = 0;
1897173273Ssam	} else {
1898173273Ssam		/* mark tid so we don't try again */
1899173273Ssam		tap->txa_flags |= IEEE80211_AGGR_NAK;
1900170530Ssam	}
1901170530Ssam	return 1;
1902170530Ssam}
1903170530Ssam
1904170530Ssam/*
1905170530Ssam * Default method for stopping A-MPDU tx aggregation.
1906170530Ssam * Any timer is cleared and we drain any pending frames.
1907170530Ssam */
1908170530Ssamstatic void
1909170530Ssamieee80211_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
1910170530Ssam{
1911170530Ssam	/* XXX locking */
1912170530Ssam	addba_stop_timeout(tap);
1913170530Ssam	if (tap->txa_flags & IEEE80211_AGGR_RUNNING) {
1914182830Ssam		/* XXX clear aggregation queue */
1915170530Ssam		tap->txa_flags &= ~IEEE80211_AGGR_RUNNING;
1916170530Ssam	}
1917170530Ssam	tap->txa_attempts = 0;
1918170530Ssam}
1919170530Ssam
1920170530Ssam/*
1921170530Ssam * Process a received action frame using the default aggregation
1922170530Ssam * policy.  We intercept ADDBA-related frames and use them to
1923170530Ssam * update our aggregation state.  All other frames are passed up
1924170530Ssam * for processing by ieee80211_recv_action.
1925170530Ssam */
1926195377Ssamstatic int
1927195377Ssamht_recv_action_ba_addba_request(struct ieee80211_node *ni,
1928195377Ssam	const struct ieee80211_frame *wh,
1929170530Ssam	const uint8_t *frm, const uint8_t *efrm)
1930170530Ssam{
1931170530Ssam	struct ieee80211com *ic = ni->ni_ic;
1932178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1933170530Ssam	struct ieee80211_rx_ampdu *rap;
1934195377Ssam	uint8_t dialogtoken;
1935195377Ssam	uint16_t baparamset, batimeout, baseqctl;
1936205277Srpaulo	uint16_t args[5];
1937195377Ssam	int tid;
1938170530Ssam
1939195377Ssam	dialogtoken = frm[2];
1940298359Savos	baparamset = le16dec(frm+3);
1941298359Savos	batimeout = le16dec(frm+5);
1942298359Savos	baseqctl = le16dec(frm+7);
1943170530Ssam
1944195377Ssam	tid = MS(baparamset, IEEE80211_BAPS_TID);
1945170530Ssam
1946195377Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1947195377Ssam	    "recv ADDBA request: dialogtoken %u baparamset 0x%x "
1948195377Ssam	    "(tid %d bufsiz %d) batimeout %d baseqctl %d:%d",
1949195377Ssam	    dialogtoken, baparamset,
1950195377Ssam	    tid, MS(baparamset, IEEE80211_BAPS_BUFSIZ),
1951195377Ssam	    batimeout,
1952195377Ssam	    MS(baseqctl, IEEE80211_BASEQ_START),
1953195377Ssam	    MS(baseqctl, IEEE80211_BASEQ_FRAG));
1954170530Ssam
1955195377Ssam	rap = &ni->ni_rx_ampdu[tid];
1956170530Ssam
1957195377Ssam	/* Send ADDBA response */
1958195377Ssam	args[0] = dialogtoken;
1959195377Ssam	/*
1960195377Ssam	 * NB: We ack only if the sta associated with HT and
1961195377Ssam	 * the ap is configured to do AMPDU rx (the latter
1962195377Ssam	 * violates the 11n spec and is mostly for testing).
1963195377Ssam	 */
1964195377Ssam	if ((ni->ni_flags & IEEE80211_NODE_AMPDU_RX) &&
1965195377Ssam	    (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_RX)) {
1966195377Ssam		/* XXX handle ampdu_rx_start failure */
1967195377Ssam		ic->ic_ampdu_rx_start(ni, rap,
1968195377Ssam		    baparamset, batimeout, baseqctl);
1969170530Ssam
1970195377Ssam		args[1] = IEEE80211_STATUS_SUCCESS;
1971195377Ssam	} else {
1972195377Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1973195377Ssam		    ni, "reject ADDBA request: %s",
1974195377Ssam		    ni->ni_flags & IEEE80211_NODE_AMPDU_RX ?
1975195377Ssam		       "administratively disabled" :
1976195377Ssam		       "not negotiated for station");
1977195377Ssam		vap->iv_stats.is_addba_reject++;
1978195377Ssam		args[1] = IEEE80211_STATUS_UNSPECIFIED;
1979195377Ssam	}
1980195377Ssam	/* XXX honor rap flags? */
1981195377Ssam	args[2] = IEEE80211_BAPS_POLICY_IMMEDIATE
1982195377Ssam		| SM(tid, IEEE80211_BAPS_TID)
1983195377Ssam		| SM(rap->rxa_wnd, IEEE80211_BAPS_BUFSIZ)
1984195377Ssam		;
1985195377Ssam	args[3] = 0;
1986205277Srpaulo	args[4] = 0;
1987195377Ssam	ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
1988195377Ssam		IEEE80211_ACTION_BA_ADDBA_RESPONSE, args);
1989195377Ssam	return 0;
1990195377Ssam}
1991170530Ssam
1992195377Ssamstatic int
1993195377Ssamht_recv_action_ba_addba_response(struct ieee80211_node *ni,
1994195377Ssam	const struct ieee80211_frame *wh,
1995195377Ssam	const uint8_t *frm, const uint8_t *efrm)
1996195377Ssam{
1997195377Ssam	struct ieee80211com *ic = ni->ni_ic;
1998195377Ssam	struct ieee80211vap *vap = ni->ni_vap;
1999195377Ssam	struct ieee80211_tx_ampdu *tap;
2000195377Ssam	uint8_t dialogtoken, policy;
2001195377Ssam	uint16_t baparamset, batimeout, code;
2002234324Sadrian	int tid, bufsiz;
2003170530Ssam
2004195377Ssam	dialogtoken = frm[2];
2005298359Savos	code = le16dec(frm+3);
2006298359Savos	baparamset = le16dec(frm+5);
2007195377Ssam	tid = MS(baparamset, IEEE80211_BAPS_TID);
2008195377Ssam	bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
2009195377Ssam	policy = MS(baparamset, IEEE80211_BAPS_POLICY);
2010298359Savos	batimeout = le16dec(frm+7);
2011195377Ssam
2012234324Sadrian	tap = &ni->ni_tx_ampdu[tid];
2013195377Ssam	if ((tap->txa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
2014195377Ssam		IEEE80211_DISCARD_MAC(vap,
2015195377Ssam		    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2016195377Ssam		    ni->ni_macaddr, "ADDBA response",
2017195377Ssam		    "no pending ADDBA, tid %d dialogtoken %u "
2018195377Ssam		    "code %d", tid, dialogtoken, code);
2019195377Ssam		vap->iv_stats.is_addba_norequest++;
2020195377Ssam		return 0;
2021195377Ssam	}
2022195377Ssam	if (dialogtoken != tap->txa_token) {
2023195377Ssam		IEEE80211_DISCARD_MAC(vap,
2024195377Ssam		    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2025195377Ssam		    ni->ni_macaddr, "ADDBA response",
2026195377Ssam		    "dialogtoken mismatch: waiting for %d, "
2027195377Ssam		    "received %d, tid %d code %d",
2028195377Ssam		    tap->txa_token, dialogtoken, tid, code);
2029195377Ssam		vap->iv_stats.is_addba_badtoken++;
2030195377Ssam		return 0;
2031195377Ssam	}
2032195377Ssam	/* NB: assumes IEEE80211_AGGR_IMMEDIATE is 1 */
2033195377Ssam	if (policy != (tap->txa_flags & IEEE80211_AGGR_IMMEDIATE)) {
2034195377Ssam		IEEE80211_DISCARD_MAC(vap,
2035195377Ssam		    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2036195377Ssam		    ni->ni_macaddr, "ADDBA response",
2037195377Ssam		    "policy mismatch: expecting %s, "
2038195377Ssam		    "received %s, tid %d code %d",
2039195377Ssam		    tap->txa_flags & IEEE80211_AGGR_IMMEDIATE,
2040195377Ssam		    policy, tid, code);
2041195377Ssam		vap->iv_stats.is_addba_badpolicy++;
2042195377Ssam		return 0;
2043195377Ssam	}
2044182829Ssam#if 0
2045195377Ssam	/* XXX we take MIN in ieee80211_addba_response */
2046195377Ssam	if (bufsiz > IEEE80211_AGGR_BAWMAX) {
2047195377Ssam		IEEE80211_DISCARD_MAC(vap,
2048195377Ssam		    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2049195377Ssam		    ni->ni_macaddr, "ADDBA response",
2050195377Ssam		    "BA window too large: max %d, "
2051195377Ssam		    "received %d, tid %d code %d",
2052195377Ssam		    bufsiz, IEEE80211_AGGR_BAWMAX, tid, code);
2053195377Ssam		vap->iv_stats.is_addba_badbawinsize++;
2054195377Ssam		return 0;
2055195377Ssam	}
2056182829Ssam#endif
2057195377Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2058195377Ssam	    "recv ADDBA response: dialogtoken %u code %d "
2059195377Ssam	    "baparamset 0x%x (tid %d bufsiz %d) batimeout %d",
2060195377Ssam	    dialogtoken, code, baparamset, tid, bufsiz,
2061195377Ssam	    batimeout);
2062195377Ssam	ic->ic_addba_response(ni, tap, code, baparamset, batimeout);
2063195377Ssam	return 0;
2064195377Ssam}
2065170530Ssam
2066195377Ssamstatic int
2067195377Ssamht_recv_action_ba_delba(struct ieee80211_node *ni,
2068195377Ssam	const struct ieee80211_frame *wh,
2069195377Ssam	const uint8_t *frm, const uint8_t *efrm)
2070195377Ssam{
2071195377Ssam	struct ieee80211com *ic = ni->ni_ic;
2072195377Ssam	struct ieee80211_rx_ampdu *rap;
2073195377Ssam	struct ieee80211_tx_ampdu *tap;
2074195377Ssam	uint16_t baparamset, code;
2075234324Sadrian	int tid;
2076170530Ssam
2077298359Savos	baparamset = le16dec(frm+2);
2078298359Savos	code = le16dec(frm+4);
2079170530Ssam
2080195377Ssam	tid = MS(baparamset, IEEE80211_DELBAPS_TID);
2081170530Ssam
2082195377Ssam	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2083195377Ssam	    "recv DELBA: baparamset 0x%x (tid %d initiator %d) "
2084195377Ssam	    "code %d", baparamset, tid,
2085195377Ssam	    MS(baparamset, IEEE80211_DELBAPS_INIT), code);
2086195377Ssam
2087195377Ssam	if ((baparamset & IEEE80211_DELBAPS_INIT) == 0) {
2088234324Sadrian		tap = &ni->ni_tx_ampdu[tid];
2089195377Ssam		ic->ic_addba_stop(ni, tap);
2090195377Ssam	} else {
2091195377Ssam		rap = &ni->ni_rx_ampdu[tid];
2092195377Ssam		ic->ic_ampdu_rx_stop(ni, rap);
2093170530Ssam	}
2094195377Ssam	return 0;
2095170530Ssam}
2096170530Ssam
2097195377Ssamstatic int
2098195377Ssamht_recv_action_ht_txchwidth(struct ieee80211_node *ni,
2099195377Ssam	const struct ieee80211_frame *wh,
2100170530Ssam	const uint8_t *frm, const uint8_t *efrm)
2101170530Ssam{
2102170530Ssam	int chw;
2103170530Ssam
2104195377Ssam	chw = (frm[2] == IEEE80211_A_HT_TXCHWIDTH_2040) ? 40 : 20;
2105195377Ssam
2106195377Ssam	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2107195377Ssam	    "%s: HT txchwidth, width %d%s",
2108195377Ssam	    __func__, chw, ni->ni_chw != chw ? "*" : "");
2109195377Ssam	if (chw != ni->ni_chw) {
2110195377Ssam		ni->ni_chw = chw;
2111195377Ssam		/* XXX notify on change */
2112170530Ssam	}
2113195377Ssam	return 0;
2114170530Ssam}
2115170530Ssam
2116195377Ssamstatic int
2117195377Ssamht_recv_action_ht_mimopwrsave(struct ieee80211_node *ni,
2118195377Ssam	const struct ieee80211_frame *wh,
2119195377Ssam	const uint8_t *frm, const uint8_t *efrm)
2120195377Ssam{
2121195377Ssam	const struct ieee80211_action_ht_mimopowersave *mps =
2122195377Ssam	    (const struct ieee80211_action_ht_mimopowersave *) frm;
2123195377Ssam
2124195377Ssam	/* XXX check iv_htcaps */
2125195377Ssam	if (mps->am_control & IEEE80211_A_HT_MIMOPWRSAVE_ENA)
2126195377Ssam		ni->ni_flags |= IEEE80211_NODE_MIMO_PS;
2127195377Ssam	else
2128195377Ssam		ni->ni_flags &= ~IEEE80211_NODE_MIMO_PS;
2129195377Ssam	if (mps->am_control & IEEE80211_A_HT_MIMOPWRSAVE_MODE)
2130195377Ssam		ni->ni_flags |= IEEE80211_NODE_MIMO_RTS;
2131195377Ssam	else
2132195377Ssam		ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS;
2133195377Ssam	/* XXX notify on change */
2134195377Ssam	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2135195377Ssam	    "%s: HT MIMO PS (%s%s)", __func__,
2136195377Ssam	    (ni->ni_flags & IEEE80211_NODE_MIMO_PS) ?  "on" : "off",
2137195377Ssam	    (ni->ni_flags & IEEE80211_NODE_MIMO_RTS) ?  "+rts" : ""
2138195377Ssam	);
2139195377Ssam	return 0;
2140195377Ssam}
2141195377Ssam
2142170530Ssam/*
2143170530Ssam * Transmit processing.
2144170530Ssam */
2145170530Ssam
2146170530Ssam/*
2147178354Ssam * Check if A-MPDU should be requested/enabled for a stream.
2148178354Ssam * We require a traffic rate above a per-AC threshold and we
2149178354Ssam * also handle backoff from previous failed attempts.
2150178354Ssam *
2151178354Ssam * Drivers may override this method to bring in information
2152178354Ssam * such as link state conditions in making the decision.
2153178354Ssam */
2154178354Ssamstatic int
2155178354Ssamieee80211_ampdu_enable(struct ieee80211_node *ni,
2156178354Ssam	struct ieee80211_tx_ampdu *tap)
2157178354Ssam{
2158178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2159178354Ssam
2160234324Sadrian	if (tap->txa_avgpps <
2161234324Sadrian	    vap->iv_ampdu_mintraffic[TID_TO_WME_AC(tap->txa_tid)])
2162178354Ssam		return 0;
2163178354Ssam	/* XXX check rssi? */
2164178354Ssam	if (tap->txa_attempts >= ieee80211_addba_maxtries &&
2165297775Sadrian	    ieee80211_time_after(ticks, tap->txa_nextrequest)) {
2166178354Ssam		/*
2167178354Ssam		 * Don't retry too often; txa_nextrequest is set
2168178354Ssam		 * to the minimum interval we'll retry after
2169178354Ssam		 * ieee80211_addba_maxtries failed attempts are made.
2170178354Ssam		 */
2171178354Ssam		return 0;
2172178354Ssam	}
2173178354Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
2174291303Sadrian	    "enable AMPDU on tid %d (%s), avgpps %d pkts %d attempt %d",
2175234324Sadrian	    tap->txa_tid, ieee80211_wme_acnames[TID_TO_WME_AC(tap->txa_tid)],
2176291303Sadrian	    tap->txa_avgpps, tap->txa_pkts, tap->txa_attempts);
2177178354Ssam	return 1;
2178178354Ssam}
2179178354Ssam
2180178354Ssam/*
2181170530Ssam * Request A-MPDU tx aggregation.  Setup local state and
2182170530Ssam * issue an ADDBA request.  BA use will only happen after
2183170530Ssam * the other end replies with ADDBA response.
2184170530Ssam */
2185170530Ssamint
2186170530Ssamieee80211_ampdu_request(struct ieee80211_node *ni,
2187170530Ssam	struct ieee80211_tx_ampdu *tap)
2188170530Ssam{
2189170530Ssam	struct ieee80211com *ic = ni->ni_ic;
2190205277Srpaulo	uint16_t args[5];
2191170530Ssam	int tid, dialogtoken;
2192170530Ssam	static int tokens = 0;	/* XXX */
2193170530Ssam
2194170530Ssam	/* XXX locking */
2195170530Ssam	if ((tap->txa_flags & IEEE80211_AGGR_SETUP) == 0) {
2196170530Ssam		/* do deferred setup of state */
2197184280Ssam		ampdu_tx_setup(tap);
2198170530Ssam	}
2199173273Ssam	/* XXX hack for not doing proper locking */
2200173273Ssam	tap->txa_flags &= ~IEEE80211_AGGR_NAK;
2201173273Ssam
2202170530Ssam	dialogtoken = (tokens+1) % 63;		/* XXX */
2203234324Sadrian	tid = tap->txa_tid;
2204183245Ssam	tap->txa_start = ni->ni_txseqs[tid];
2205170530Ssam
2206170530Ssam	args[0] = dialogtoken;
2207205277Srpaulo	args[1] = 0;	/* NB: status code not used */
2208205277Srpaulo	args[2]	= IEEE80211_BAPS_POLICY_IMMEDIATE
2209170530Ssam		| SM(tid, IEEE80211_BAPS_TID)
2210170530Ssam		| SM(IEEE80211_AGGR_BAWMAX, IEEE80211_BAPS_BUFSIZ)
2211170530Ssam		;
2212205277Srpaulo	args[3] = 0;	/* batimeout */
2213170530Ssam	/* NB: do first so there's no race against reply */
2214205277Srpaulo	if (!ic->ic_addba_request(ni, tap, dialogtoken, args[2], args[3])) {
2215170530Ssam		/* unable to setup state, don't make request */
2216178354Ssam		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2217234324Sadrian		    ni, "%s: could not setup BA stream for TID %d AC %d",
2218234324Sadrian		    __func__, tap->txa_tid, TID_TO_WME_AC(tap->txa_tid));
2219173273Ssam		/* defer next try so we don't slam the driver with requests */
2220178354Ssam		tap->txa_attempts = ieee80211_addba_maxtries;
2221178354Ssam		/* NB: check in case driver wants to override */
2222178354Ssam		if (tap->txa_nextrequest <= ticks)
2223178354Ssam			tap->txa_nextrequest = ticks + ieee80211_addba_backoff;
2224170530Ssam		return 0;
2225170530Ssam	}
2226170530Ssam	tokens = dialogtoken;			/* allocate token */
2227183245Ssam	/* NB: after calling ic_addba_request so driver can set txa_start */
2228205277Srpaulo	args[4] = SM(tap->txa_start, IEEE80211_BASEQ_START)
2229178953Ssam		| SM(0, IEEE80211_BASEQ_FRAG)
2230178953Ssam		;
2231170530Ssam	return ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
2232170530Ssam		IEEE80211_ACTION_BA_ADDBA_REQUEST, args);
2233170530Ssam}
2234170530Ssam
2235170530Ssam/*
2236173273Ssam * Terminate an AMPDU tx stream.  State is reclaimed
2237173273Ssam * and the peer notified with a DelBA Action frame.
2238173273Ssam */
2239173273Ssamvoid
2240183250Ssamieee80211_ampdu_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
2241183250Ssam	int reason)
2242173273Ssam{
2243173273Ssam	struct ieee80211com *ic = ni->ni_ic;
2244178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2245173273Ssam	uint16_t args[4];
2246173273Ssam
2247173273Ssam	/* XXX locking */
2248184280Ssam	tap->txa_flags &= ~IEEE80211_AGGR_BARPEND;
2249173273Ssam	if (IEEE80211_AMPDU_RUNNING(tap)) {
2250178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2251298364Savos		    ni, "%s: stop BA stream for TID %d (reason: %d (%s))",
2252298364Savos		    __func__, tap->txa_tid, reason,
2253298364Savos		    ieee80211_reason_to_string(reason));
2254178354Ssam		vap->iv_stats.is_ampdu_stop++;
2255173273Ssam
2256173273Ssam		ic->ic_addba_stop(ni, tap);
2257234324Sadrian		args[0] = tap->txa_tid;
2258173273Ssam		args[1] = IEEE80211_DELBAPS_INIT;
2259183250Ssam		args[2] = reason;			/* XXX reason code */
2260195377Ssam		ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
2261173273Ssam			IEEE80211_ACTION_BA_DELBA, args);
2262173273Ssam	} else {
2263178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2264298364Savos		    ni, "%s: BA stream for TID %d not running "
2265298364Savos		    "(reason: %d (%s))", __func__, tap->txa_tid, reason,
2266298364Savos		    ieee80211_reason_to_string(reason));
2267178354Ssam		vap->iv_stats.is_ampdu_stop_failed++;
2268173273Ssam	}
2269173273Ssam}
2270173273Ssam
2271235801Sadrian/* XXX */
2272235801Sadrianstatic void bar_start_timer(struct ieee80211_tx_ampdu *tap);
2273235801Sadrian
2274184280Ssamstatic void
2275184280Ssambar_timeout(void *arg)
2276184280Ssam{
2277184280Ssam	struct ieee80211_tx_ampdu *tap = arg;
2278184280Ssam	struct ieee80211_node *ni = tap->txa_ni;
2279184280Ssam
2280184280Ssam	KASSERT((tap->txa_flags & IEEE80211_AGGR_XCHGPEND) == 0,
2281184280Ssam	    ("bar/addba collision, flags 0x%x", tap->txa_flags));
2282184280Ssam
2283184280Ssam	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2284184280Ssam	    ni, "%s: tid %u flags 0x%x attempts %d", __func__,
2285234324Sadrian	    tap->txa_tid, tap->txa_flags, tap->txa_attempts);
2286184280Ssam
2287184280Ssam	/* guard against race with bar_tx_complete */
2288184280Ssam	if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) == 0)
2289184280Ssam		return;
2290184280Ssam	/* XXX ? */
2291234018Sadrian	if (tap->txa_attempts >= ieee80211_bar_maxtries) {
2292235801Sadrian		struct ieee80211com *ic = ni->ni_ic;
2293235801Sadrian
2294234018Sadrian		ni->ni_vap->iv_stats.is_ampdu_bar_tx_fail++;
2295235801Sadrian		/*
2296235801Sadrian		 * If (at least) the last BAR TX timeout was due to
2297235801Sadrian		 * an ieee80211_send_bar() failures, then we need
2298235801Sadrian		 * to make sure we notify the driver that a BAR
2299235801Sadrian		 * TX did occur and fail.  This gives the driver
2300235801Sadrian		 * a chance to undo any queue pause that may
2301298995Spfg		 * have occurred.
2302235801Sadrian		 */
2303235801Sadrian		ic->ic_bar_response(ni, tap, 1);
2304184280Ssam		ieee80211_ampdu_stop(ni, tap, IEEE80211_REASON_TIMEOUT);
2305234018Sadrian	} else {
2306234018Sadrian		ni->ni_vap->iv_stats.is_ampdu_bar_tx_retry++;
2307235801Sadrian		if (ieee80211_send_bar(ni, tap, tap->txa_seqpending) != 0) {
2308242205Sadrian			IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2309242205Sadrian			    ni, "%s: failed to TX, starting timer\n",
2310242205Sadrian			    __func__);
2311235801Sadrian			/*
2312235801Sadrian			 * If ieee80211_send_bar() fails here, the
2313235801Sadrian			 * timer may have stopped and/or the pending
2314235801Sadrian			 * flag may be clear.  Because of this,
2315235801Sadrian			 * fake the BARPEND and reset the timer.
2316235801Sadrian			 * A retransmission attempt will then occur
2317235801Sadrian			 * during the next timeout.
2318235801Sadrian			 */
2319235801Sadrian			/* XXX locking */
2320235801Sadrian			tap->txa_flags |= IEEE80211_AGGR_BARPEND;
2321235801Sadrian			bar_start_timer(tap);
2322235801Sadrian		}
2323234018Sadrian	}
2324184280Ssam}
2325184280Ssam
2326184280Ssamstatic void
2327184280Ssambar_start_timer(struct ieee80211_tx_ampdu *tap)
2328184280Ssam{
2329242205Sadrian	IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2330242205Sadrian	    tap->txa_ni,
2331242205Sadrian	    "%s: called",
2332242205Sadrian	    __func__);
2333184280Ssam	callout_reset(&tap->txa_timer, ieee80211_bar_timeout, bar_timeout, tap);
2334184280Ssam}
2335184280Ssam
2336184280Ssamstatic void
2337184280Ssambar_stop_timer(struct ieee80211_tx_ampdu *tap)
2338184280Ssam{
2339242205Sadrian	IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2340242205Sadrian	    tap->txa_ni,
2341242205Sadrian	    "%s: called",
2342242205Sadrian	    __func__);
2343184280Ssam	callout_stop(&tap->txa_timer);
2344184280Ssam}
2345184280Ssam
2346184280Ssamstatic void
2347184280Ssambar_tx_complete(struct ieee80211_node *ni, void *arg, int status)
2348184280Ssam{
2349184280Ssam	struct ieee80211_tx_ampdu *tap = arg;
2350184280Ssam
2351184280Ssam	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2352184280Ssam	    ni, "%s: tid %u flags 0x%x pending %d status %d",
2353234324Sadrian	    __func__, tap->txa_tid, tap->txa_flags,
2354184280Ssam	    callout_pending(&tap->txa_timer), status);
2355184280Ssam
2356234018Sadrian	ni->ni_vap->iv_stats.is_ampdu_bar_tx++;
2357184280Ssam	/* XXX locking */
2358184280Ssam	if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) &&
2359184280Ssam	    callout_pending(&tap->txa_timer)) {
2360184280Ssam		struct ieee80211com *ic = ni->ni_ic;
2361184280Ssam
2362224907Sadrian		if (status == 0)		/* ACK'd */
2363184280Ssam			bar_stop_timer(tap);
2364184280Ssam		ic->ic_bar_response(ni, tap, status);
2365184280Ssam		/* NB: just let timer expire so we pace requests */
2366184280Ssam	}
2367184280Ssam}
2368184280Ssam
2369184280Ssamstatic void
2370184280Ssamieee80211_bar_response(struct ieee80211_node *ni,
2371184280Ssam	struct ieee80211_tx_ampdu *tap, int status)
2372184280Ssam{
2373184280Ssam
2374242205Sadrian	IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2375242205Sadrian	    tap->txa_ni,
2376242205Sadrian	    "%s: called",
2377242205Sadrian	    __func__);
2378224907Sadrian	if (status == 0) {		/* got ACK */
2379184280Ssam		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2380184280Ssam		    ni, "BAR moves BA win <%u:%u> (%u frames) txseq %u tid %u",
2381184280Ssam		    tap->txa_start,
2382184280Ssam		    IEEE80211_SEQ_ADD(tap->txa_start, tap->txa_wnd-1),
2383184280Ssam		    tap->txa_qframes, tap->txa_seqpending,
2384234324Sadrian		    tap->txa_tid);
2385184280Ssam
2386184280Ssam		/* NB: timer already stopped in bar_tx_complete */
2387184280Ssam		tap->txa_start = tap->txa_seqpending;
2388184280Ssam		tap->txa_flags &= ~IEEE80211_AGGR_BARPEND;
2389184280Ssam	}
2390184280Ssam}
2391184280Ssam
2392173273Ssam/*
2393170530Ssam * Transmit a BAR frame to the specified node.  The
2394170530Ssam * BAR contents are drawn from the supplied aggregation
2395170530Ssam * state associated with the node.
2396184280Ssam *
2397184280Ssam * NB: we only handle immediate ACK w/ compressed bitmap.
2398170530Ssam */
2399170530Ssamint
2400170530Ssamieee80211_send_bar(struct ieee80211_node *ni,
2401184280Ssam	struct ieee80211_tx_ampdu *tap, ieee80211_seq seq)
2402170530Ssam{
2403178354Ssam#define	senderr(_x, _v)	do { vap->iv_stats._v++; ret = _x; goto bad; } while (0)
2404178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2405170530Ssam	struct ieee80211com *ic = ni->ni_ic;
2406184280Ssam	struct ieee80211_frame_bar *bar;
2407170530Ssam	struct mbuf *m;
2408184280Ssam	uint16_t barctl, barseqctl;
2409170530Ssam	uint8_t *frm;
2410170530Ssam	int tid, ret;
2411170530Ssam
2412242205Sadrian
2413242205Sadrian	IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2414242205Sadrian	    tap->txa_ni,
2415242205Sadrian	    "%s: called",
2416242205Sadrian	    __func__);
2417242205Sadrian
2418184280Ssam	if ((tap->txa_flags & IEEE80211_AGGR_RUNNING) == 0) {
2419184280Ssam		/* no ADDBA response, should not happen */
2420184280Ssam		/* XXX stat+msg */
2421184280Ssam		return EINVAL;
2422184280Ssam	}
2423184280Ssam	/* XXX locking */
2424184280Ssam	bar_stop_timer(tap);
2425184280Ssam
2426170530Ssam	ieee80211_ref_node(ni);
2427170530Ssam
2428184280Ssam	m = ieee80211_getmgtframe(&frm, ic->ic_headroom, sizeof(*bar));
2429170530Ssam	if (m == NULL)
2430170530Ssam		senderr(ENOMEM, is_tx_nobuf);
2431170530Ssam
2432184280Ssam	if (!ieee80211_add_callback(m, bar_tx_complete, tap)) {
2433184280Ssam		m_freem(m);
2434184280Ssam		senderr(ENOMEM, is_tx_nobuf);	/* XXX */
2435184280Ssam		/* NOTREACHED */
2436184280Ssam	}
2437184280Ssam
2438184280Ssam	bar = mtod(m, struct ieee80211_frame_bar *);
2439184280Ssam	bar->i_fc[0] = IEEE80211_FC0_VERSION_0 |
2440170530Ssam		IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_BAR;
2441184280Ssam	bar->i_fc[1] = 0;
2442184280Ssam	IEEE80211_ADDR_COPY(bar->i_ra, ni->ni_macaddr);
2443184280Ssam	IEEE80211_ADDR_COPY(bar->i_ta, vap->iv_myaddr);
2444170530Ssam
2445234324Sadrian	tid = tap->txa_tid;
2446170530Ssam	barctl 	= (tap->txa_flags & IEEE80211_AGGR_IMMEDIATE ?
2447184280Ssam			0 : IEEE80211_BAR_NOACK)
2448184280Ssam		| IEEE80211_BAR_COMP
2449184280Ssam		| SM(tid, IEEE80211_BAR_TID)
2450170530Ssam		;
2451184280Ssam	barseqctl = SM(seq, IEEE80211_BAR_SEQ_START);
2452184280Ssam	/* NB: known to have proper alignment */
2453184280Ssam	bar->i_ctl = htole16(barctl);
2454184280Ssam	bar->i_seq = htole16(barseqctl);
2455184280Ssam	m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_bar);
2456170530Ssam
2457178354Ssam	M_WME_SETAC(m, WME_AC_VO);
2458178354Ssam
2459170530Ssam	IEEE80211_NODE_STAT(ni, tx_mgmt);	/* XXX tx_ctl? */
2460170530Ssam
2461184280Ssam	/* XXX locking */
2462184280Ssam	/* init/bump attempts counter */
2463184280Ssam	if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) == 0)
2464184280Ssam		tap->txa_attempts = 1;
2465184280Ssam	else
2466184280Ssam		tap->txa_attempts++;
2467184280Ssam	tap->txa_seqpending = seq;
2468184280Ssam	tap->txa_flags |= IEEE80211_AGGR_BARPEND;
2469170530Ssam
2470184280Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_11N,
2471184280Ssam	    ni, "send BAR: tid %u ctl 0x%x start %u (attempt %d)",
2472184280Ssam	    tid, barctl, seq, tap->txa_attempts);
2473184280Ssam
2474225013Sadrian	/*
2475225013Sadrian	 * ic_raw_xmit will free the node reference
2476225013Sadrian	 * regardless of queue/TX success or failure.
2477225013Sadrian	 */
2478248069Sadrian	IEEE80211_TX_LOCK(ic);
2479248069Sadrian	ret = ieee80211_raw_output(vap, ni, m, NULL);
2480248069Sadrian	IEEE80211_TX_UNLOCK(ic);
2481184280Ssam	if (ret != 0) {
2482242205Sadrian		IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_11N,
2483242205Sadrian		    ni, "send BAR: failed: (ret = %d)\n",
2484242205Sadrian		    ret);
2485184280Ssam		/* xmit failed, clear state flag */
2486184280Ssam		tap->txa_flags &= ~IEEE80211_AGGR_BARPEND;
2487234018Sadrian		vap->iv_stats.is_ampdu_bar_tx_fail++;
2488225013Sadrian		return ret;
2489184280Ssam	}
2490184280Ssam	/* XXX hack against tx complete happening before timer is started */
2491184280Ssam	if (tap->txa_flags & IEEE80211_AGGR_BARPEND)
2492184280Ssam		bar_start_timer(tap);
2493184280Ssam	return 0;
2494170530Ssambad:
2495242205Sadrian	IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2496242205Sadrian	    tap->txa_ni,
2497242205Sadrian	    "%s: bad! ret=%d",
2498242205Sadrian	    __func__, ret);
2499234018Sadrian	vap->iv_stats.is_ampdu_bar_tx_fail++;
2500170530Ssam	ieee80211_free_node(ni);
2501170530Ssam	return ret;
2502170530Ssam#undef senderr
2503170530Ssam}
2504170530Ssam
2505195377Ssamstatic int
2506195377Ssamht_action_output(struct ieee80211_node *ni, struct mbuf *m)
2507195377Ssam{
2508195377Ssam	struct ieee80211_bpf_params params;
2509195377Ssam
2510195377Ssam	memset(&params, 0, sizeof(params));
2511195377Ssam	params.ibp_pri = WME_AC_VO;
2512195377Ssam	params.ibp_rate0 = ni->ni_txparms->mgmtrate;
2513195377Ssam	/* NB: we know all frames are unicast */
2514195377Ssam	params.ibp_try0 = ni->ni_txparms->maxretry;
2515195377Ssam	params.ibp_power = ni->ni_txpower;
2516195377Ssam	return ieee80211_mgmt_output(ni, m, IEEE80211_FC0_SUBTYPE_ACTION,
2517195377Ssam	     &params);
2518195377Ssam}
2519195377Ssam
2520195377Ssam#define	ADDSHORT(frm, v) do {			\
2521195377Ssam	frm[0] = (v) & 0xff;			\
2522195377Ssam	frm[1] = (v) >> 8;			\
2523195377Ssam	frm += 2;				\
2524195377Ssam} while (0)
2525195377Ssam
2526170530Ssam/*
2527170530Ssam * Send an action management frame.  The arguments are stuff
2528170530Ssam * into a frame without inspection; the caller is assumed to
2529170530Ssam * prepare them carefully (e.g. based on the aggregation state).
2530170530Ssam */
2531195377Ssamstatic int
2532195377Ssamht_send_action_ba_addba(struct ieee80211_node *ni,
2533195377Ssam	int category, int action, void *arg0)
2534170530Ssam{
2535178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2536170530Ssam	struct ieee80211com *ic = ni->ni_ic;
2537195377Ssam	uint16_t *args = arg0;
2538170530Ssam	struct mbuf *m;
2539170530Ssam	uint8_t *frm;
2540195377Ssam
2541195377Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2542205277Srpaulo	    "send ADDBA %s: dialogtoken %d status %d "
2543195377Ssam	    "baparamset 0x%x (tid %d) batimeout 0x%x baseqctl 0x%x",
2544195377Ssam	    (action == IEEE80211_ACTION_BA_ADDBA_REQUEST) ?
2545195377Ssam		"request" : "response",
2546205277Srpaulo	    args[0], args[1], args[2], MS(args[2], IEEE80211_BAPS_TID),
2547205277Srpaulo	    args[3], args[4]);
2548195377Ssam
2549195377Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2550195377Ssam	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2551195377Ssam	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2552195377Ssam	ieee80211_ref_node(ni);
2553195377Ssam
2554195377Ssam	m = ieee80211_getmgtframe(&frm,
2555195377Ssam	    ic->ic_headroom + sizeof(struct ieee80211_frame),
2556195377Ssam	    sizeof(uint16_t)	/* action+category */
2557195377Ssam	    /* XXX may action payload */
2558195377Ssam	    + sizeof(struct ieee80211_action_ba_addbaresponse)
2559195377Ssam	);
2560195377Ssam	if (m != NULL) {
2561195377Ssam		*frm++ = category;
2562195377Ssam		*frm++ = action;
2563195377Ssam		*frm++ = args[0];		/* dialog token */
2564205277Srpaulo		if (action == IEEE80211_ACTION_BA_ADDBA_RESPONSE)
2565205277Srpaulo			ADDSHORT(frm, args[1]);	/* status code */
2566205277Srpaulo		ADDSHORT(frm, args[2]);		/* baparamset */
2567205277Srpaulo		ADDSHORT(frm, args[3]);		/* batimeout */
2568195377Ssam		if (action == IEEE80211_ACTION_BA_ADDBA_REQUEST)
2569205277Srpaulo			ADDSHORT(frm, args[4]);	/* baseqctl */
2570195377Ssam		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2571195377Ssam		return ht_action_output(ni, m);
2572195377Ssam	} else {
2573195377Ssam		vap->iv_stats.is_tx_nobuf++;
2574195377Ssam		ieee80211_free_node(ni);
2575195377Ssam		return ENOMEM;
2576195377Ssam	}
2577195377Ssam}
2578195377Ssam
2579195377Ssamstatic int
2580195377Ssamht_send_action_ba_delba(struct ieee80211_node *ni,
2581195377Ssam	int category, int action, void *arg0)
2582195377Ssam{
2583195377Ssam	struct ieee80211vap *vap = ni->ni_vap;
2584195377Ssam	struct ieee80211com *ic = ni->ni_ic;
2585195377Ssam	uint16_t *args = arg0;
2586195377Ssam	struct mbuf *m;
2587170530Ssam	uint16_t baparamset;
2588195377Ssam	uint8_t *frm;
2589170530Ssam
2590195377Ssam	baparamset = SM(args[0], IEEE80211_DELBAPS_TID)
2591195377Ssam		   | args[1]
2592195377Ssam		   ;
2593195377Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2594298364Savos	    "send DELBA action: tid %d, initiator %d reason %d (%s)",
2595298364Savos	    args[0], args[1], args[2], ieee80211_reason_to_string(args[2]));
2596170530Ssam
2597178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2598195377Ssam	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2599195377Ssam	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2600170530Ssam	ieee80211_ref_node(ni);
2601170530Ssam
2602170530Ssam	m = ieee80211_getmgtframe(&frm,
2603195377Ssam	    ic->ic_headroom + sizeof(struct ieee80211_frame),
2604195377Ssam	    sizeof(uint16_t)	/* action+category */
2605195377Ssam	    /* XXX may action payload */
2606195377Ssam	    + sizeof(struct ieee80211_action_ba_addbaresponse)
2607170530Ssam	);
2608195377Ssam	if (m != NULL) {
2609195377Ssam		*frm++ = category;
2610195377Ssam		*frm++ = action;
2611195377Ssam		ADDSHORT(frm, baparamset);
2612195377Ssam		ADDSHORT(frm, args[2]);		/* reason code */
2613195377Ssam		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2614195377Ssam		return ht_action_output(ni, m);
2615195377Ssam	} else {
2616195377Ssam		vap->iv_stats.is_tx_nobuf++;
2617195377Ssam		ieee80211_free_node(ni);
2618195377Ssam		return ENOMEM;
2619195377Ssam	}
2620195377Ssam}
2621170530Ssam
2622195377Ssamstatic int
2623195377Ssamht_send_action_ht_txchwidth(struct ieee80211_node *ni,
2624195377Ssam	int category, int action, void *arg0)
2625195377Ssam{
2626195377Ssam	struct ieee80211vap *vap = ni->ni_vap;
2627195377Ssam	struct ieee80211com *ic = ni->ni_ic;
2628195377Ssam	struct mbuf *m;
2629195377Ssam	uint8_t *frm;
2630170530Ssam
2631195377Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2632195377Ssam	    "send HT txchwidth: width %d",
2633195377Ssam	    IEEE80211_IS_CHAN_HT40(ni->ni_chan) ? 40 : 20);
2634170530Ssam
2635195377Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2636195377Ssam	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2637195377Ssam	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2638195377Ssam	ieee80211_ref_node(ni);
2639170530Ssam
2640195377Ssam	m = ieee80211_getmgtframe(&frm,
2641195377Ssam	    ic->ic_headroom + sizeof(struct ieee80211_frame),
2642195377Ssam	    sizeof(uint16_t)	/* action+category */
2643195377Ssam	    /* XXX may action payload */
2644195377Ssam	    + sizeof(struct ieee80211_action_ba_addbaresponse)
2645195377Ssam	);
2646195377Ssam	if (m != NULL) {
2647195377Ssam		*frm++ = category;
2648195377Ssam		*frm++ = action;
2649195377Ssam		*frm++ = IEEE80211_IS_CHAN_HT40(ni->ni_chan) ?
2650195377Ssam			IEEE80211_A_HT_TXCHWIDTH_2040 :
2651195377Ssam			IEEE80211_A_HT_TXCHWIDTH_20;
2652195377Ssam		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2653195377Ssam		return ht_action_output(ni, m);
2654195377Ssam	} else {
2655195377Ssam		vap->iv_stats.is_tx_nobuf++;
2656195377Ssam		ieee80211_free_node(ni);
2657195377Ssam		return ENOMEM;
2658170530Ssam	}
2659195377Ssam}
2660170530Ssam#undef ADDSHORT
2661170530Ssam
2662170530Ssam/*
2663219600Sbschmidt * Construct the MCS bit mask for inclusion in an HT capabilities
2664219600Sbschmidt * information element.
2665170530Ssam */
2666219600Sbschmidtstatic void
2667219600Sbschmidtieee80211_set_mcsset(struct ieee80211com *ic, uint8_t *frm)
2668170530Ssam{
2669170530Ssam	int i;
2670219600Sbschmidt	uint8_t txparams;
2671170530Ssam
2672219600Sbschmidt	KASSERT((ic->ic_rxstream > 0 && ic->ic_rxstream <= 4),
2673219600Sbschmidt	    ("ic_rxstream %d out of range", ic->ic_rxstream));
2674219600Sbschmidt	KASSERT((ic->ic_txstream > 0 && ic->ic_txstream <= 4),
2675219600Sbschmidt	    ("ic_txstream %d out of range", ic->ic_txstream));
2676219600Sbschmidt
2677219600Sbschmidt	for (i = 0; i < ic->ic_rxstream * 8; i++)
2678219600Sbschmidt		setbit(frm, i);
2679219600Sbschmidt	if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
2680219600Sbschmidt	    (ic->ic_htcaps & IEEE80211_HTC_RXMCS32))
2681219600Sbschmidt		setbit(frm, 32);
2682219600Sbschmidt	if (ic->ic_htcaps & IEEE80211_HTC_RXUNEQUAL) {
2683219600Sbschmidt		if (ic->ic_rxstream >= 2) {
2684219600Sbschmidt			for (i = 33; i <= 38; i++)
2685219600Sbschmidt				setbit(frm, i);
2686170530Ssam		}
2687219600Sbschmidt		if (ic->ic_rxstream >= 3) {
2688219600Sbschmidt			for (i = 39; i <= 52; i++)
2689219600Sbschmidt				setbit(frm, i);
2690219600Sbschmidt		}
2691219600Sbschmidt		if (ic->ic_txstream >= 4) {
2692219600Sbschmidt			for (i = 53; i <= 76; i++)
2693219600Sbschmidt				setbit(frm, i);
2694219600Sbschmidt		}
2695170530Ssam	}
2696219600Sbschmidt
2697219600Sbschmidt	if (ic->ic_rxstream != ic->ic_txstream) {
2698219600Sbschmidt		txparams = 0x1;			/* TX MCS set defined */
2699219600Sbschmidt		txparams |= 0x2;		/* TX RX MCS not equal */
2700219600Sbschmidt		txparams |= (ic->ic_txstream - 1) << 2;	/* num TX streams */
2701219600Sbschmidt		if (ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL)
2702219600Sbschmidt			txparams |= 0x16;	/* TX unequal modulation sup */
2703219600Sbschmidt	} else
2704219600Sbschmidt		txparams = 0;
2705219600Sbschmidt	frm[12] = txparams;
2706170530Ssam}
2707170530Ssam
2708170530Ssam/*
2709170530Ssam * Add body of an HTCAP information element.
2710170530Ssam */
2711170530Ssamstatic uint8_t *
2712170530Ssamieee80211_add_htcap_body(uint8_t *frm, struct ieee80211_node *ni)
2713170530Ssam{
2714170530Ssam#define	ADDSHORT(frm, v) do {			\
2715170530Ssam	frm[0] = (v) & 0xff;			\
2716170530Ssam	frm[1] = (v) >> 8;			\
2717170530Ssam	frm += 2;				\
2718170530Ssam} while (0)
2719222683Sbschmidt	struct ieee80211com *ic = ni->ni_ic;
2720178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2721205513Srpaulo	uint16_t caps, extcaps;
2722173865Ssam	int rxmax, density;
2723170530Ssam
2724170530Ssam	/* HT capabilities */
2725178354Ssam	caps = vap->iv_htcaps & 0xffff;
2726173273Ssam	/*
2727173273Ssam	 * Note channel width depends on whether we are operating as
2728173273Ssam	 * a sta or not.  When operating as a sta we are generating
2729173273Ssam	 * a request based on our desired configuration.  Otherwise
2730173273Ssam	 * we are operational and the channel attributes identify
2731173273Ssam	 * how we've been setup (which might be different if a fixed
2732173273Ssam	 * channel is specified).
2733173273Ssam	 */
2734178354Ssam	if (vap->iv_opmode == IEEE80211_M_STA) {
2735173273Ssam		/* override 20/40 use based on config */
2736193655Ssam		if (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)
2737173273Ssam			caps |= IEEE80211_HTCAP_CHWIDTH40;
2738173273Ssam		else
2739173273Ssam			caps &= ~IEEE80211_HTCAP_CHWIDTH40;
2740282704Sadrian
2741282704Sadrian		/* Start by using the advertised settings */
2742173865Ssam		rxmax = MS(ni->ni_htparam, IEEE80211_HTCAP_MAXRXAMPDU);
2743173865Ssam		density = MS(ni->ni_htparam, IEEE80211_HTCAP_MPDUDENSITY);
2744222683Sbschmidt
2745299074Sadrian		IEEE80211_DPRINTF(vap, IEEE80211_MSG_11N,
2746299074Sadrian		    "%s: advertised rxmax=%d, density=%d, vap rxmax=%d, density=%d\n",
2747299074Sadrian		    __func__,
2748299074Sadrian		    rxmax,
2749299074Sadrian		    density,
2750299074Sadrian		    vap->iv_ampdu_rxmax,
2751299074Sadrian		    vap->iv_ampdu_density);
2752299074Sadrian
2753282704Sadrian		/* Cap at VAP rxmax */
2754282704Sadrian		if (rxmax > vap->iv_ampdu_rxmax)
2755282704Sadrian			rxmax = vap->iv_ampdu_rxmax;
2756282704Sadrian
2757222683Sbschmidt		/*
2758282704Sadrian		 * If the VAP ampdu density value greater, use that.
2759282704Sadrian		 *
2760282704Sadrian		 * (Larger density value == larger minimum gap between A-MPDU
2761282704Sadrian		 * subframes.)
2762282704Sadrian		 */
2763282704Sadrian		if (vap->iv_ampdu_density > density)
2764282704Sadrian			density = vap->iv_ampdu_density;
2765282704Sadrian
2766282704Sadrian		/*
2767222683Sbschmidt		 * NB: Hardware might support HT40 on some but not all
2768222683Sbschmidt		 * channels. We can't determine this earlier because only
2769222683Sbschmidt		 * after association the channel is upgraded to HT based
2770222683Sbschmidt		 * on the negotiated capabilities.
2771222683Sbschmidt		 */
2772222683Sbschmidt		if (ni->ni_chan != IEEE80211_CHAN_ANYC &&
2773222683Sbschmidt		    findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT40U) == NULL &&
2774222683Sbschmidt		    findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT40D) == NULL)
2775222683Sbschmidt			caps &= ~IEEE80211_HTCAP_CHWIDTH40;
2776173273Ssam	} else {
2777173273Ssam		/* override 20/40 use based on current channel */
2778178354Ssam		if (IEEE80211_IS_CHAN_HT40(ni->ni_chan))
2779173273Ssam			caps |= IEEE80211_HTCAP_CHWIDTH40;
2780173273Ssam		else
2781173273Ssam			caps &= ~IEEE80211_HTCAP_CHWIDTH40;
2782282704Sadrian
2783282704Sadrian		/* XXX TODO should it start by using advertised settings? */
2784178354Ssam		rxmax = vap->iv_ampdu_rxmax;
2785178354Ssam		density = vap->iv_ampdu_density;
2786173273Ssam	}
2787282704Sadrian
2788170530Ssam	/* adjust short GI based on channel and config */
2789193655Ssam	if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20) == 0)
2790170530Ssam		caps &= ~IEEE80211_HTCAP_SHORTGI20;
2791193655Ssam	if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40) == 0 ||
2792170530Ssam	    (caps & IEEE80211_HTCAP_CHWIDTH40) == 0)
2793170530Ssam		caps &= ~IEEE80211_HTCAP_SHORTGI40;
2794298604Sadrian
2795298604Sadrian	/* adjust STBC based on receive capabilities */
2796298604Sadrian	if ((vap->iv_flags_ht & IEEE80211_FHT_STBC_RX) == 0)
2797298604Sadrian		caps &= ~IEEE80211_HTCAP_RXSTBC;
2798298604Sadrian
2799298604Sadrian	/* XXX TODO: adjust LDPC based on receive capabilities */
2800298604Sadrian
2801170530Ssam	ADDSHORT(frm, caps);
2802170530Ssam
2803170530Ssam	/* HT parameters */
2804173865Ssam	*frm = SM(rxmax, IEEE80211_HTCAP_MAXRXAMPDU)
2805173865Ssam	     | SM(density, IEEE80211_HTCAP_MPDUDENSITY)
2806173273Ssam	     ;
2807170530Ssam	frm++;
2808170530Ssam
2809170530Ssam	/* pre-zero remainder of ie */
2810170530Ssam	memset(frm, 0, sizeof(struct ieee80211_ie_htcap) -
2811170530Ssam		__offsetof(struct ieee80211_ie_htcap, hc_mcsset));
2812170530Ssam
2813170530Ssam	/* supported MCS set */
2814173273Ssam	/*
2815219600Sbschmidt	 * XXX: For sta mode the rate set should be restricted based
2816219600Sbschmidt	 * on the AP's capabilities, but ni_htrates isn't setup when
2817219600Sbschmidt	 * we're called to form an AssocReq frame so for now we're
2818219600Sbschmidt	 * restricted to the device capabilities.
2819173273Ssam	 */
2820219600Sbschmidt	ieee80211_set_mcsset(ni->ni_ic, frm);
2821170530Ssam
2822205513Srpaulo	frm += __offsetof(struct ieee80211_ie_htcap, hc_extcap) -
2823205513Srpaulo		__offsetof(struct ieee80211_ie_htcap, hc_mcsset);
2824205513Srpaulo
2825205513Srpaulo	/* HT extended capabilities */
2826205513Srpaulo	extcaps = vap->iv_htextcaps & 0xffff;
2827205513Srpaulo
2828205513Srpaulo	ADDSHORT(frm, extcaps);
2829205513Srpaulo
2830170530Ssam	frm += sizeof(struct ieee80211_ie_htcap) -
2831205513Srpaulo		__offsetof(struct ieee80211_ie_htcap, hc_txbf);
2832205513Srpaulo
2833170530Ssam	return frm;
2834170530Ssam#undef ADDSHORT
2835170530Ssam}
2836170530Ssam
2837170530Ssam/*
2838170530Ssam * Add 802.11n HT capabilities information element
2839170530Ssam */
2840170530Ssamuint8_t *
2841170530Ssamieee80211_add_htcap(uint8_t *frm, struct ieee80211_node *ni)
2842170530Ssam{
2843170530Ssam	frm[0] = IEEE80211_ELEMID_HTCAP;
2844170530Ssam	frm[1] = sizeof(struct ieee80211_ie_htcap) - 2;
2845170530Ssam	return ieee80211_add_htcap_body(frm + 2, ni);
2846170530Ssam}
2847170530Ssam
2848170530Ssam/*
2849170530Ssam * Add Broadcom OUI wrapped standard HTCAP ie; this is
2850170530Ssam * used for compatibility w/ pre-draft implementations.
2851170530Ssam */
2852170530Ssamuint8_t *
2853170530Ssamieee80211_add_htcap_vendor(uint8_t *frm, struct ieee80211_node *ni)
2854170530Ssam{
2855170530Ssam	frm[0] = IEEE80211_ELEMID_VENDOR;
2856170530Ssam	frm[1] = 4 + sizeof(struct ieee80211_ie_htcap) - 2;
2857170530Ssam	frm[2] = (BCM_OUI >> 0) & 0xff;
2858170530Ssam	frm[3] = (BCM_OUI >> 8) & 0xff;
2859170530Ssam	frm[4] = (BCM_OUI >> 16) & 0xff;
2860170530Ssam	frm[5] = BCM_OUI_HTCAP;
2861170530Ssam	return ieee80211_add_htcap_body(frm + 6, ni);
2862170530Ssam}
2863170530Ssam
2864170530Ssam/*
2865170530Ssam * Construct the MCS bit mask of basic rates
2866170530Ssam * for inclusion in an HT information element.
2867170530Ssam */
2868170530Ssamstatic void
2869170530Ssamieee80211_set_basic_htrates(uint8_t *frm, const struct ieee80211_htrateset *rs)
2870170530Ssam{
2871170530Ssam	int i;
2872170530Ssam
2873170530Ssam	for (i = 0; i < rs->rs_nrates; i++) {
2874170530Ssam		int r = rs->rs_rates[i] & IEEE80211_RATE_VAL;
2875170530Ssam		if ((rs->rs_rates[i] & IEEE80211_RATE_BASIC) &&
2876170530Ssam		    r < IEEE80211_HTRATE_MAXSIZE) {
2877170530Ssam			/* NB: this assumes a particular implementation */
2878170530Ssam			setbit(frm, r);
2879170530Ssam		}
2880170530Ssam	}
2881170530Ssam}
2882170530Ssam
2883170530Ssam/*
2884172211Ssam * Update the HTINFO ie for a beacon frame.
2885172211Ssam */
2886172211Ssamvoid
2887178354Ssamieee80211_ht_update_beacon(struct ieee80211vap *vap,
2888172211Ssam	struct ieee80211_beacon_offsets *bo)
2889172211Ssam{
2890172211Ssam#define	PROTMODE	(IEEE80211_HTINFO_OPMODE|IEEE80211_HTINFO_NONHT_PRESENT)
2891250442Sadrian	struct ieee80211_node *ni;
2892250442Sadrian	const struct ieee80211_channel *bsschan;
2893178354Ssam	struct ieee80211com *ic = vap->iv_ic;
2894172211Ssam	struct ieee80211_ie_htinfo *ht =
2895172211Ssam	   (struct ieee80211_ie_htinfo *) bo->bo_htinfo;
2896172211Ssam
2897250442Sadrian	ni = ieee80211_ref_node(vap->iv_bss);
2898250442Sadrian	bsschan = ni->ni_chan;
2899250442Sadrian
2900172211Ssam	/* XXX only update on channel change */
2901178354Ssam	ht->hi_ctrlchannel = ieee80211_chan2ieee(ic, bsschan);
2902193655Ssam	if (vap->iv_flags_ht & IEEE80211_FHT_RIFS)
2903183256Ssam		ht->hi_byte1 = IEEE80211_HTINFO_RIFSMODE_PERM;
2904183256Ssam	else
2905183256Ssam		ht->hi_byte1 = IEEE80211_HTINFO_RIFSMODE_PROH;
2906178354Ssam	if (IEEE80211_IS_CHAN_HT40U(bsschan))
2907172211Ssam		ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_ABOVE;
2908178354Ssam	else if (IEEE80211_IS_CHAN_HT40D(bsschan))
2909172211Ssam		ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_BELOW;
2910172211Ssam	else
2911172211Ssam		ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_NONE;
2912178354Ssam	if (IEEE80211_IS_CHAN_HT40(bsschan))
2913172211Ssam		ht->hi_byte1 |= IEEE80211_HTINFO_TXWIDTH_2040;
2914172211Ssam
2915172211Ssam	/* protection mode */
2916172211Ssam	ht->hi_byte2 = (ht->hi_byte2 &~ PROTMODE) | ic->ic_curhtprotmode;
2917172211Ssam
2918250442Sadrian	ieee80211_free_node(ni);
2919250442Sadrian
2920172211Ssam	/* XXX propagate to vendor ie's */
2921172211Ssam#undef PROTMODE
2922172211Ssam}
2923172211Ssam
2924172211Ssam/*
2925170530Ssam * Add body of an HTINFO information element.
2926173273Ssam *
2927173273Ssam * NB: We don't use struct ieee80211_ie_htinfo because we can
2928173273Ssam * be called to fillin both a standard ie and a compat ie that
2929173273Ssam * has a vendor OUI at the front.
2930170530Ssam */
2931170530Ssamstatic uint8_t *
2932170530Ssamieee80211_add_htinfo_body(uint8_t *frm, struct ieee80211_node *ni)
2933170530Ssam{
2934183256Ssam	struct ieee80211vap *vap = ni->ni_vap;
2935170530Ssam	struct ieee80211com *ic = ni->ni_ic;
2936170530Ssam
2937170530Ssam	/* pre-zero remainder of ie */
2938170530Ssam	memset(frm, 0, sizeof(struct ieee80211_ie_htinfo) - 2);
2939170530Ssam
2940170530Ssam	/* primary/control channel center */
2941178354Ssam	*frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
2942170530Ssam
2943193655Ssam	if (vap->iv_flags_ht & IEEE80211_FHT_RIFS)
2944183256Ssam		frm[0] = IEEE80211_HTINFO_RIFSMODE_PERM;
2945183256Ssam	else
2946183256Ssam		frm[0] = IEEE80211_HTINFO_RIFSMODE_PROH;
2947178354Ssam	if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan))
2948170530Ssam		frm[0] |= IEEE80211_HTINFO_2NDCHAN_ABOVE;
2949178354Ssam	else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan))
2950170530Ssam		frm[0] |= IEEE80211_HTINFO_2NDCHAN_BELOW;
2951170530Ssam	else
2952170530Ssam		frm[0] |= IEEE80211_HTINFO_2NDCHAN_NONE;
2953178354Ssam	if (IEEE80211_IS_CHAN_HT40(ni->ni_chan))
2954170530Ssam		frm[0] |= IEEE80211_HTINFO_TXWIDTH_2040;
2955170530Ssam
2956172211Ssam	frm[1] = ic->ic_curhtprotmode;
2957170530Ssam
2958170530Ssam	frm += 5;
2959170530Ssam
2960170530Ssam	/* basic MCS set */
2961170530Ssam	ieee80211_set_basic_htrates(frm, &ni->ni_htrates);
2962170530Ssam	frm += sizeof(struct ieee80211_ie_htinfo) -
2963170530Ssam		__offsetof(struct ieee80211_ie_htinfo, hi_basicmcsset);
2964170530Ssam	return frm;
2965170530Ssam}
2966170530Ssam
2967170530Ssam/*
2968170530Ssam * Add 802.11n HT information information element.
2969170530Ssam */
2970170530Ssamuint8_t *
2971170530Ssamieee80211_add_htinfo(uint8_t *frm, struct ieee80211_node *ni)
2972170530Ssam{
2973170530Ssam	frm[0] = IEEE80211_ELEMID_HTINFO;
2974170530Ssam	frm[1] = sizeof(struct ieee80211_ie_htinfo) - 2;
2975170530Ssam	return ieee80211_add_htinfo_body(frm + 2, ni);
2976170530Ssam}
2977170530Ssam
2978170530Ssam/*
2979170530Ssam * Add Broadcom OUI wrapped standard HTINFO ie; this is
2980170530Ssam * used for compatibility w/ pre-draft implementations.
2981170530Ssam */
2982170530Ssamuint8_t *
2983170530Ssamieee80211_add_htinfo_vendor(uint8_t *frm, struct ieee80211_node *ni)
2984170530Ssam{
2985170530Ssam	frm[0] = IEEE80211_ELEMID_VENDOR;
2986170530Ssam	frm[1] = 4 + sizeof(struct ieee80211_ie_htinfo) - 2;
2987170530Ssam	frm[2] = (BCM_OUI >> 0) & 0xff;
2988170530Ssam	frm[3] = (BCM_OUI >> 8) & 0xff;
2989170530Ssam	frm[4] = (BCM_OUI >> 16) & 0xff;
2990170530Ssam	frm[5] = BCM_OUI_HTINFO;
2991170530Ssam	return ieee80211_add_htinfo_body(frm + 6, ni);
2992170530Ssam}
2993