xref: /freebsd/sys/net80211/ieee80211_ht.c (revision b69b7fe7589c5eea9a274859fd48ea430c8f3a3e)
1 /*-
2  * Copyright (c) 2007-2008 Sam Leffler, Errno Consulting
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include <sys/cdefs.h>
27 #ifdef __FreeBSD__
28 __FBSDID("$FreeBSD$");
29 #endif
30 
31 /*
32  * IEEE 802.11n protocol support.
33  */
34 
35 #include "opt_inet.h"
36 #include "opt_wlan.h"
37 
38 #include <sys/param.h>
39 #include <sys/kernel.h>
40 #include <sys/systm.h>
41 #include <sys/endian.h>
42 
43 #include <sys/socket.h>
44 
45 #include <net/if.h>
46 #include <net/if_media.h>
47 #include <net/ethernet.h>
48 
49 #include <net80211/ieee80211_var.h>
50 #include <net80211/ieee80211_input.h>
51 
52 /* define here, used throughout file */
53 #define	MS(_v, _f)	(((_v) & _f) >> _f##_S)
54 #define	SM(_v, _f)	(((_v) << _f##_S) & _f)
55 
56 const struct ieee80211_mcs_rates ieee80211_htrates[16] = {
57 	{  13,  14,  27,  30 },	/* MCS 0 */
58 	{  26,  29,  54,  60 },	/* MCS 1 */
59 	{  39,  43,  81,  90 },	/* MCS 2 */
60 	{  52,  58, 108, 120 },	/* MCS 3 */
61 	{  78,  87, 162, 180 },	/* MCS 4 */
62 	{ 104, 116, 216, 240 },	/* MCS 5 */
63 	{ 117, 130, 243, 270 },	/* MCS 6 */
64 	{ 130, 144, 270, 300 },	/* MCS 7 */
65 	{  26,  29,  54,  60 },	/* MCS 8 */
66 	{  52,  58, 108, 120 },	/* MCS 9 */
67 	{  78,  87, 162, 180 },	/* MCS 10 */
68 	{ 104, 116, 216, 240 },	/* MCS 11 */
69 	{ 156, 173, 324, 360 },	/* MCS 12 */
70 	{ 208, 231, 432, 480 },	/* MCS 13 */
71 	{ 234, 260, 486, 540 },	/* MCS 14 */
72 	{ 260, 289, 540, 600 }	/* MCS 15 */
73 };
74 
75 static const struct ieee80211_htrateset ieee80211_rateset_11n =
76 	{ 16, {
77 	          0,   1,   2,   3,   4,  5,   6,  7,  8,  9,
78 		 10,  11,  12,  13,  14,  15 }
79 	};
80 
81 #ifdef IEEE80211_AMPDU_AGE
82 /* XXX public for sysctl hookup */
83 int	ieee80211_ampdu_age = -1;	/* threshold for ampdu reorder q (ms) */
84 #endif
85 int	ieee80211_recv_bar_ena = 1;
86 int	ieee80211_addba_timeout = -1;	/* timeout waiting for ADDBA response */
87 int	ieee80211_addba_backoff = -1;	/* backoff after max ADDBA requests */
88 int	ieee80211_addba_maxtries = 3;	/* max ADDBA requests before backoff */
89 
90 /*
91  * Setup HT parameters that depends on the clock frequency.
92  */
93 static void
94 ieee80211_ht_setup(void)
95 {
96 #ifdef IEEE80211_AMPDU_AGE
97 	ieee80211_ampdu_age = msecs_to_ticks(500);
98 #endif
99 	ieee80211_addba_timeout = msecs_to_ticks(250);
100 	ieee80211_addba_backoff = msecs_to_ticks(10*1000);
101 }
102 SYSINIT(wlan_ht, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_ht_setup, NULL);
103 
104 static int ieee80211_ampdu_enable(struct ieee80211_node *ni,
105 	struct ieee80211_tx_ampdu *tap);
106 static int ieee80211_addba_request(struct ieee80211_node *ni,
107 	struct ieee80211_tx_ampdu *tap,
108 	int dialogtoken, int baparamset, int batimeout);
109 static int ieee80211_addba_response(struct ieee80211_node *ni,
110 	struct ieee80211_tx_ampdu *tap,
111 	int code, int baparamset, int batimeout);
112 static void ieee80211_addba_stop(struct ieee80211_node *ni,
113 	struct ieee80211_tx_ampdu *tap);
114 static void ieee80211_aggr_recv_action(struct ieee80211_node *ni,
115 	const uint8_t *frm, const uint8_t *efrm);
116 
117 void
118 ieee80211_ht_attach(struct ieee80211com *ic)
119 {
120 	/* setup default aggregation policy */
121 	ic->ic_recv_action = ieee80211_aggr_recv_action;
122 	ic->ic_send_action = ieee80211_send_action;
123 	ic->ic_ampdu_enable = ieee80211_ampdu_enable;
124 	ic->ic_addba_request = ieee80211_addba_request;
125 	ic->ic_addba_response = ieee80211_addba_response;
126 	ic->ic_addba_stop = ieee80211_addba_stop;
127 
128 	ic->ic_htprotmode = IEEE80211_PROT_RTSCTS;
129 	ic->ic_curhtprotmode = IEEE80211_HTINFO_OPMODE_PURE;
130 }
131 
132 void
133 ieee80211_ht_detach(struct ieee80211com *ic)
134 {
135 }
136 
137 void
138 ieee80211_ht_vattach(struct ieee80211vap *vap)
139 {
140 
141 	/* driver can override defaults */
142 	vap->iv_ampdu_rxmax = IEEE80211_HTCAP_MAXRXAMPDU_8K;
143 	vap->iv_ampdu_density = IEEE80211_HTCAP_MPDUDENSITY_NA;
144 	vap->iv_ampdu_limit = vap->iv_ampdu_rxmax;
145 	vap->iv_amsdu_limit = vap->iv_htcaps & IEEE80211_HTCAP_MAXAMSDU;
146 	/* tx aggregation traffic thresholds */
147 	vap->iv_ampdu_mintraffic[WME_AC_BK] = 128;
148 	vap->iv_ampdu_mintraffic[WME_AC_BE] = 64;
149 	vap->iv_ampdu_mintraffic[WME_AC_VO] = 32;
150 	vap->iv_ampdu_mintraffic[WME_AC_VI] = 32;
151 
152 	if (vap->iv_htcaps & IEEE80211_HTC_HT) {
153 		/*
154 		 * Device is HT capable; enable all HT-related
155 		 * facilities by default.
156 		 * XXX these choices may be too aggressive.
157 		 */
158 		vap->iv_flags_ext |= IEEE80211_FEXT_HT
159 				  |  IEEE80211_FEXT_HTCOMPAT
160 				  ;
161 		if (vap->iv_htcaps & IEEE80211_HTCAP_SHORTGI20)
162 			vap->iv_flags_ext |= IEEE80211_FEXT_SHORTGI20;
163 		/* XXX infer from channel list? */
164 		if (vap->iv_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
165 			vap->iv_flags_ext |= IEEE80211_FEXT_USEHT40;
166 			if (vap->iv_htcaps & IEEE80211_HTCAP_SHORTGI40)
167 				vap->iv_flags_ext |= IEEE80211_FEXT_SHORTGI40;
168 		}
169 		/* NB: A-MPDU and A-MSDU rx are mandated, these are tx only */
170 		vap->iv_flags_ext |= IEEE80211_FEXT_AMPDU_RX;
171 		if (vap->iv_htcaps & IEEE80211_HTC_AMPDU)
172 			vap->iv_flags_ext |= IEEE80211_FEXT_AMPDU_TX;
173 		vap->iv_flags_ext |= IEEE80211_FEXT_AMSDU_RX;
174 		if (vap->iv_htcaps & IEEE80211_HTC_AMSDU)
175 			vap->iv_flags_ext |= IEEE80211_FEXT_AMSDU_TX;
176 	}
177 	/* NB: disable default legacy WDS, too many issues right now */
178 	if (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY)
179 		vap->iv_flags_ext &= ~IEEE80211_FEXT_HT;
180 }
181 
182 void
183 ieee80211_ht_vdetach(struct ieee80211vap *vap)
184 {
185 }
186 
187 static void
188 ht_announce(struct ieee80211com *ic, int mode,
189 	const struct ieee80211_htrateset *rs)
190 {
191 	struct ifnet *ifp = ic->ic_ifp;
192 	int i, rate, mword;
193 
194 	if_printf(ifp, "%s MCS: ", ieee80211_phymode_name[mode]);
195 	for (i = 0; i < rs->rs_nrates; i++) {
196 		mword = ieee80211_rate2media(ic,
197 		    rs->rs_rates[i] | IEEE80211_RATE_MCS, mode);
198 		if (IFM_SUBTYPE(mword) != IFM_IEEE80211_MCS)
199 			continue;
200 		rate = ieee80211_htrates[rs->rs_rates[i]].ht40_rate_400ns;
201 		printf("%s%d%sMbps", (i != 0 ? " " : ""),
202 		    rate / 2, ((rate & 0x1) != 0 ? ".5" : ""));
203 	}
204 	printf("\n");
205 }
206 
207 void
208 ieee80211_ht_announce(struct ieee80211com *ic)
209 {
210 	if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA))
211 		ht_announce(ic, IEEE80211_MODE_11NA, &ieee80211_rateset_11n);
212 	if (isset(ic->ic_modecaps, IEEE80211_MODE_11NG))
213 		ht_announce(ic, IEEE80211_MODE_11NG, &ieee80211_rateset_11n);
214 }
215 
216 const struct ieee80211_htrateset *
217 ieee80211_get_suphtrates(struct ieee80211com *ic,
218 	const struct ieee80211_channel *c)
219 {
220 	return &ieee80211_rateset_11n;
221 }
222 
223 /*
224  * Receive processing.
225  */
226 
227 /*
228  * Decap the encapsulated A-MSDU frames and dispatch all but
229  * the last for delivery.  The last frame is returned for
230  * delivery via the normal path.
231  */
232 struct mbuf *
233 ieee80211_decap_amsdu(struct ieee80211_node *ni, struct mbuf *m)
234 {
235 	struct ieee80211vap *vap = ni->ni_vap;
236 	int framelen;
237 	struct mbuf *n;
238 
239 	/* discard 802.3 header inserted by ieee80211_decap */
240 	m_adj(m, sizeof(struct ether_header));
241 
242 	vap->iv_stats.is_amsdu_decap++;
243 
244 	for (;;) {
245 		/*
246 		 * Decap the first frame, bust it apart from the
247 		 * remainder and deliver.  We leave the last frame
248 		 * delivery to the caller (for consistency with other
249 		 * code paths, could also do it here).
250 		 */
251 		m = ieee80211_decap1(m, &framelen);
252 		if (m == NULL) {
253 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
254 			    ni->ni_macaddr, "a-msdu", "%s", "decap failed");
255 			vap->iv_stats.is_amsdu_tooshort++;
256 			return NULL;
257 		}
258 		if (m->m_pkthdr.len == framelen)
259 			break;
260 		n = m_split(m, framelen, M_NOWAIT);
261 		if (n == NULL) {
262 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
263 			    ni->ni_macaddr, "a-msdu",
264 			    "%s", "unable to split encapsulated frames");
265 			vap->iv_stats.is_amsdu_split++;
266 			m_freem(m);			/* NB: must reclaim */
267 			return NULL;
268 		}
269 		vap->iv_deliver_data(vap, ni, m);
270 
271 		/*
272 		 * Remove frame contents; each intermediate frame
273 		 * is required to be aligned to a 4-byte boundary.
274 		 */
275 		m = n;
276 		m_adj(m, roundup2(framelen, 4) - framelen);	/* padding */
277 	}
278 	return m;				/* last delivered by caller */
279 }
280 
281 /*
282  * Purge all frames in the A-MPDU re-order queue.
283  */
284 static void
285 ampdu_rx_purge(struct ieee80211_rx_ampdu *rap)
286 {
287 	struct mbuf *m;
288 	int i;
289 
290 	for (i = 0; i < rap->rxa_wnd; i++) {
291 		m = rap->rxa_m[i];
292 		if (m != NULL) {
293 			rap->rxa_m[i] = NULL;
294 			rap->rxa_qbytes -= m->m_pkthdr.len;
295 			m_freem(m);
296 			if (--rap->rxa_qframes == 0)
297 				break;
298 		}
299 	}
300 	KASSERT(rap->rxa_qbytes == 0 && rap->rxa_qframes == 0,
301 	    ("lost %u data, %u frames on ampdu rx q",
302 	    rap->rxa_qbytes, rap->rxa_qframes));
303 }
304 
305 /*
306  * Start A-MPDU rx/re-order processing for the specified TID.
307  */
308 static void
309 ampdu_rx_start(struct ieee80211_rx_ampdu *rap, int bufsiz, int start)
310 {
311 	if (rap->rxa_flags & IEEE80211_AGGR_RUNNING) {
312 		/*
313 		 * AMPDU previously setup and not terminated with a DELBA,
314 		 * flush the reorder q's in case anything remains.
315 		 */
316 		ampdu_rx_purge(rap);
317 	}
318 	memset(rap, 0, sizeof(*rap));
319 	rap->rxa_wnd = (bufsiz == 0) ?
320 	    IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
321 	rap->rxa_start = start;
322 	rap->rxa_flags |=  IEEE80211_AGGR_RUNNING | IEEE80211_AGGR_XCHGPEND;
323 }
324 
325 /*
326  * Stop A-MPDU rx processing for the specified TID.
327  */
328 static void
329 ampdu_rx_stop(struct ieee80211_rx_ampdu *rap)
330 {
331 	ampdu_rx_purge(rap);
332 	rap->rxa_flags &= ~(IEEE80211_AGGR_RUNNING | IEEE80211_AGGR_XCHGPEND);
333 }
334 
335 /*
336  * Dispatch a frame from the A-MPDU reorder queue.  The
337  * frame is fed back into ieee80211_input marked with an
338  * M_AMPDU flag so it doesn't come back to us (it also
339  * permits ieee80211_input to optimize re-processing).
340  */
341 static __inline void
342 ampdu_dispatch(struct ieee80211_node *ni, struct mbuf *m)
343 {
344 	m->m_flags |= M_AMPDU;	/* bypass normal processing */
345 	/* NB: rssi, noise, and rstamp are ignored w/ M_AMPDU set */
346 	(void) ieee80211_input(ni, m, 0, 0, 0);
347 }
348 
349 /*
350  * Dispatch as many frames as possible from the re-order queue.
351  * Frames will always be "at the front"; we process all frames
352  * up to the first empty slot in the window.  On completion we
353  * cleanup state if there are still pending frames in the current
354  * BA window.  We assume the frame at slot 0 is already handled
355  * by the caller; we always start at slot 1.
356  */
357 static void
358 ampdu_rx_dispatch(struct ieee80211_rx_ampdu *rap, struct ieee80211_node *ni)
359 {
360 	struct ieee80211vap *vap = ni->ni_vap;
361 	struct mbuf *m;
362 	int i;
363 
364 	/* flush run of frames */
365 	for (i = 1; i < rap->rxa_wnd; i++) {
366 		m = rap->rxa_m[i];
367 		if (m == NULL)
368 			break;
369 		rap->rxa_m[i] = NULL;
370 		rap->rxa_qbytes -= m->m_pkthdr.len;
371 		rap->rxa_qframes--;
372 
373 		ampdu_dispatch(ni, m);
374 	}
375 	/*
376 	 * If frames remain, copy the mbuf pointers down so
377 	 * they correspond to the offsets in the new window.
378 	 */
379 	if (rap->rxa_qframes != 0) {
380 		int n = rap->rxa_qframes, j;
381 		for (j = i+1; j < rap->rxa_wnd; j++) {
382 			if (rap->rxa_m[j] != NULL) {
383 				rap->rxa_m[j-i] = rap->rxa_m[j];
384 				rap->rxa_m[j] = NULL;
385 				if (--n == 0)
386 					break;
387 			}
388 		}
389 		KASSERT(n == 0, ("lost %d frames", n));
390 		vap->iv_stats.is_ampdu_rx_copy += rap->rxa_qframes;
391 	}
392 	/*
393 	 * Adjust the start of the BA window to
394 	 * reflect the frames just dispatched.
395 	 */
396 	rap->rxa_start = IEEE80211_SEQ_ADD(rap->rxa_start, i);
397 	vap->iv_stats.is_ampdu_rx_oor += i;
398 }
399 
400 #ifdef IEEE80211_AMPDU_AGE
401 /*
402  * Dispatch all frames in the A-MPDU re-order queue.
403  */
404 static void
405 ampdu_rx_flush(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
406 {
407 	struct ieee80211vap *vap = ni->ni_vap;
408 	struct mbuf *m;
409 	int i;
410 
411 	for (i = 0; i < rap->rxa_wnd; i++) {
412 		m = rap->rxa_m[i];
413 		if (m == NULL)
414 			continue;
415 		rap->rxa_m[i] = NULL;
416 		rap->rxa_qbytes -= m->m_pkthdr.len;
417 		rap->rxa_qframes--;
418 		vap->iv_stats.is_ampdu_rx_oor++;
419 
420 		ampdu_dispatch(ni, m);
421 		if (rap->rxa_qframes == 0)
422 			break;
423 	}
424 }
425 #endif /* IEEE80211_AMPDU_AGE */
426 
427 /*
428  * Dispatch all frames in the A-MPDU re-order queue
429  * preceding the specified sequence number.  This logic
430  * handles window moves due to a received MSDU or BAR.
431  */
432 static void
433 ampdu_rx_flush_upto(struct ieee80211_node *ni,
434 	struct ieee80211_rx_ampdu *rap, ieee80211_seq winstart)
435 {
436 	struct ieee80211vap *vap = ni->ni_vap;
437 	struct mbuf *m;
438 	ieee80211_seq seqno;
439 	int i;
440 
441 	/*
442 	 * Flush any complete MSDU's with a sequence number lower
443 	 * than winstart.  Gaps may exist.  Note that we may actually
444 	 * dispatch frames past winstart if a run continues; this is
445 	 * an optimization that avoids having to do a separate pass
446 	 * to dispatch frames after moving the BA window start.
447 	 */
448 	seqno = rap->rxa_start;
449 	for (i = 0; i < rap->rxa_wnd; i++) {
450 		m = rap->rxa_m[i];
451 		if (m != NULL) {
452 			rap->rxa_m[i] = NULL;
453 			rap->rxa_qbytes -= m->m_pkthdr.len;
454 			rap->rxa_qframes--;
455 			vap->iv_stats.is_ampdu_rx_oor++;
456 
457 			ampdu_dispatch(ni, m);
458 		} else {
459 			if (!IEEE80211_SEQ_BA_BEFORE(seqno, winstart))
460 				break;
461 		}
462 		seqno = IEEE80211_SEQ_INC(seqno);
463 	}
464 	/*
465 	 * If frames remain, copy the mbuf pointers down so
466 	 * they correspond to the offsets in the new window.
467 	 */
468 	if (rap->rxa_qframes != 0) {
469 		int n = rap->rxa_qframes, j;
470 
471 		/* NB: this loop assumes i > 0 and/or rxa_m[0] is NULL */
472 		KASSERT(rap->rxa_m[0] == NULL,
473 		    ("%s: BA window slot 0 occupied", __func__));
474 		for (j = i+1; j < rap->rxa_wnd; j++) {
475 			if (rap->rxa_m[j] != NULL) {
476 				rap->rxa_m[j-i] = rap->rxa_m[j];
477 				rap->rxa_m[j] = NULL;
478 				if (--n == 0)
479 					break;
480 			}
481 		}
482 		KASSERT(n == 0, ("%s: lost %d frames, qframes %d off %d "
483 		    "BA win <%d:%d> winstart %d",
484 		    __func__, n, rap->rxa_qframes, i, rap->rxa_start,
485 		    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
486 		    winstart));
487 		vap->iv_stats.is_ampdu_rx_copy += rap->rxa_qframes;
488 	}
489 	/*
490 	 * Move the start of the BA window; we use the
491 	 * sequence number of the last MSDU that was
492 	 * passed up the stack+1 or winstart if stopped on
493 	 * a gap in the reorder buffer.
494 	 */
495 	rap->rxa_start = seqno;
496 }
497 
498 /*
499  * Process a received QoS data frame for an HT station.  Handle
500  * A-MPDU reordering: if this frame is received out of order
501  * and falls within the BA window hold onto it.  Otherwise if
502  * this frame completes a run, flush any pending frames.  We
503  * return 1 if the frame is consumed.  A 0 is returned if
504  * the frame should be processed normally by the caller.
505  */
506 int
507 ieee80211_ampdu_reorder(struct ieee80211_node *ni, struct mbuf *m)
508 {
509 #define	IEEE80211_FC0_QOSDATA \
510 	(IEEE80211_FC0_TYPE_DATA|IEEE80211_FC0_SUBTYPE_QOS|IEEE80211_FC0_VERSION_0)
511 #define	PROCESS		0	/* caller should process frame */
512 #define	CONSUMED	1	/* frame consumed, caller does nothing */
513 	struct ieee80211vap *vap = ni->ni_vap;
514 	struct ieee80211_qosframe *wh;
515 	struct ieee80211_rx_ampdu *rap;
516 	ieee80211_seq rxseq;
517 	uint8_t tid;
518 	int off;
519 
520 	KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT sta"));
521 
522 	/* NB: m_len known to be sufficient */
523 	wh = mtod(m, struct ieee80211_qosframe *);
524 	KASSERT(wh->i_fc[0] == IEEE80211_FC0_QOSDATA, ("not QoS data"));
525 
526 	if ((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) == IEEE80211_FC1_DIR_DSTODS)
527 		tid = ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0];
528 	else
529 		tid = wh->i_qos[0];
530 	tid &= IEEE80211_QOS_TID;
531 	rap = &ni->ni_rx_ampdu[tid];
532 	if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
533 		/*
534 		 * No ADDBA request yet, don't touch.
535 		 */
536 		return PROCESS;
537 	}
538 	rxseq = le16toh(*(uint16_t *)wh->i_seq);
539 	if ((rxseq & IEEE80211_SEQ_FRAG_MASK) != 0) {
540 		/*
541 		 * Fragments are not allowed; toss.
542 		 */
543 		IEEE80211_DISCARD_MAC(vap,
544 		    IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
545 		    "A-MPDU", "fragment, rxseq 0x%x tid %u%s", rxseq, tid,
546 		    wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
547 		vap->iv_stats.is_ampdu_rx_drop++;
548 		IEEE80211_NODE_STAT(ni, rx_drop);
549 		m_freem(m);
550 		return CONSUMED;
551 	}
552 	rxseq >>= IEEE80211_SEQ_SEQ_SHIFT;
553 	rap->rxa_nframes++;
554 again:
555 	if (rxseq == rap->rxa_start) {
556 		/*
557 		 * First frame in window.
558 		 */
559 		if (rap->rxa_qframes != 0) {
560 			/*
561 			 * Dispatch as many packets as we can.
562 			 */
563 			KASSERT(rap->rxa_m[0] == NULL, ("unexpected dup"));
564 			ampdu_dispatch(ni, m);
565 			ampdu_rx_dispatch(rap, ni);
566 			return CONSUMED;
567 		} else {
568 			/*
569 			 * In order; advance window and notify
570 			 * caller to dispatch directly.
571 			 */
572 			rap->rxa_start = IEEE80211_SEQ_INC(rxseq);
573 			return PROCESS;
574 		}
575 	}
576 	/*
577 	 * Frame is out of order; store if in the BA window.
578 	 */
579 	/* calculate offset in BA window */
580 	off = IEEE80211_SEQ_SUB(rxseq, rap->rxa_start);
581 	if (off < rap->rxa_wnd) {
582 		/*
583 		 * Common case (hopefully): in the BA window.
584 		 * Sec 9.10.7.6 a) (D2.04 p.118 line 47)
585 		 */
586 #ifdef IEEE80211_AMPDU_AGE
587 		/*
588 		 * Check for frames sitting too long in the reorder queue.
589 		 * This should only ever happen if frames are not delivered
590 		 * without the sender otherwise notifying us (e.g. with a
591 		 * BAR to move the window).  Typically this happens because
592 		 * of vendor bugs that cause the sequence number to jump.
593 		 * When this happens we get a gap in the reorder queue that
594 		 * leaves frame sitting on the queue until they get pushed
595 		 * out due to window moves.  When the vendor does not send
596 		 * BAR this move only happens due to explicit packet sends
597 		 *
598 		 * NB: we only track the time of the oldest frame in the
599 		 * reorder q; this means that if we flush we might push
600 		 * frames that still "new"; if this happens then subsequent
601 		 * frames will result in BA window moves which cost something
602 		 * but is still better than a big throughput dip.
603 		 */
604 		if (rap->rxa_qframes != 0) {
605 			/* XXX honor batimeout? */
606 			if (ticks - rap->rxa_age > ieee80211_ampdu_age) {
607 				/*
608 				 * Too long since we received the first
609 				 * frame; flush the reorder buffer.
610 				 */
611 				if (rap->rxa_qframes != 0) {
612 					vap->iv_stats.is_ampdu_rx_age +=
613 					    rap->rxa_qframes;
614 					ampdu_rx_flush(ni, rap);
615 				}
616 				rap->rxa_start = IEEE80211_SEQ_INC(rxseq);
617 				return PROCESS;
618 			}
619 		} else {
620 			/*
621 			 * First frame, start aging timer.
622 			 */
623 			rap->rxa_age = ticks;
624 		}
625 #endif /* IEEE80211_AMPDU_AGE */
626 		/* save packet */
627 		if (rap->rxa_m[off] == NULL) {
628 			rap->rxa_m[off] = m;
629 			rap->rxa_qframes++;
630 			rap->rxa_qbytes += m->m_pkthdr.len;
631 			vap->iv_stats.is_ampdu_rx_reorder++;
632 		} else {
633 			IEEE80211_DISCARD_MAC(vap,
634 			    IEEE80211_MSG_INPUT | IEEE80211_MSG_11N,
635 			    ni->ni_macaddr, "a-mpdu duplicate",
636 			    "seqno %u tid %u BA win <%u:%u>",
637 			    rxseq, tid, rap->rxa_start,
638 			    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1));
639 			vap->iv_stats.is_rx_dup++;
640 			IEEE80211_NODE_STAT(ni, rx_dup);
641 			m_freem(m);
642 		}
643 		return CONSUMED;
644 	}
645 	if (off < IEEE80211_SEQ_BA_RANGE) {
646 		/*
647 		 * Outside the BA window, but within range;
648 		 * flush the reorder q and move the window.
649 		 * Sec 9.10.7.6 b) (D2.04 p.118 line 60)
650 		 */
651 		IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
652 		    "move BA win <%u:%u> (%u frames) rxseq %u tid %u",
653 		    rap->rxa_start,
654 		    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
655 		    rap->rxa_qframes, rxseq, tid);
656 		vap->iv_stats.is_ampdu_rx_move++;
657 
658 		/*
659 		 * The spec says to flush frames up to but not including:
660 		 * 	WinStart_B = rxseq - rap->rxa_wnd + 1
661 		 * Then insert the frame or notify the caller to process
662 		 * it immediately.  We can safely do this by just starting
663 		 * over again because we know the frame will now be within
664 		 * the BA window.
665 		 */
666 		/* NB: rxa_wnd known to be >0 */
667 		ampdu_rx_flush_upto(ni, rap,
668 		    IEEE80211_SEQ_SUB(rxseq, rap->rxa_wnd-1));
669 		goto again;
670 	} else {
671 		/*
672 		 * Outside the BA window and out of range; toss.
673 		 * Sec 9.10.7.6 c) (D2.04 p.119 line 16)
674 		 */
675 		IEEE80211_DISCARD_MAC(vap,
676 		    IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
677 		    "MPDU", "BA win <%u:%u> (%u frames) rxseq %u tid %u%s",
678 		    rap->rxa_start,
679 		    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
680 		    rap->rxa_qframes, rxseq, tid,
681 		    wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
682 		vap->iv_stats.is_ampdu_rx_drop++;
683 		IEEE80211_NODE_STAT(ni, rx_drop);
684 		m_freem(m);
685 		return CONSUMED;
686 	}
687 #undef CONSUMED
688 #undef PROCESS
689 #undef IEEE80211_FC0_QOSDATA
690 }
691 
692 /*
693  * Process a BAR ctl frame.  Dispatch all frames up to
694  * the sequence number of the frame.  If this frame is
695  * out of range it's discarded.
696  */
697 void
698 ieee80211_recv_bar(struct ieee80211_node *ni, struct mbuf *m0)
699 {
700 	struct ieee80211vap *vap = ni->ni_vap;
701 	struct ieee80211_frame_bar *wh;
702 	struct ieee80211_rx_ampdu *rap;
703 	ieee80211_seq rxseq;
704 	int tid, off;
705 
706 	if (!ieee80211_recv_bar_ena) {
707 #if 0
708 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_11N,
709 		    ni->ni_macaddr, "BAR", "%s", "processing disabled");
710 #endif
711 		vap->iv_stats.is_ampdu_bar_bad++;
712 		return;
713 	}
714 	wh = mtod(m0, struct ieee80211_frame_bar *);
715 	/* XXX check basic BAR */
716 	tid = MS(le16toh(wh->i_ctl), IEEE80211_BAR_TID);
717 	rap = &ni->ni_rx_ampdu[tid];
718 	if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
719 		/*
720 		 * No ADDBA request yet, don't touch.
721 		 */
722 		IEEE80211_DISCARD_MAC(vap,
723 		    IEEE80211_MSG_INPUT | IEEE80211_MSG_11N,
724 		    ni->ni_macaddr, "BAR", "no BA stream, tid %u", tid);
725 		vap->iv_stats.is_ampdu_bar_bad++;
726 		return;
727 	}
728 	vap->iv_stats.is_ampdu_bar_rx++;
729 	rxseq = le16toh(wh->i_seq) >> IEEE80211_SEQ_SEQ_SHIFT;
730 	if (rxseq == rap->rxa_start)
731 		return;
732 	/* calculate offset in BA window */
733 	off = IEEE80211_SEQ_SUB(rxseq, rap->rxa_start);
734 	if (off < IEEE80211_SEQ_BA_RANGE) {
735 		/*
736 		 * Flush the reorder q up to rxseq and move the window.
737 		 * Sec 9.10.7.6 a) (D2.04 p.119 line 22)
738 		 */
739 		IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
740 		    "BAR moves BA win <%u:%u> (%u frames) rxseq %u tid %u",
741 		    rap->rxa_start,
742 		    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
743 		    rap->rxa_qframes, rxseq, tid);
744 		vap->iv_stats.is_ampdu_bar_move++;
745 
746 		ampdu_rx_flush_upto(ni, rap, rxseq);
747 		if (off >= rap->rxa_wnd) {
748 			/*
749 			 * BAR specifies a window start to the right of BA
750 			 * window; we must move it explicitly since
751 			 * ampdu_rx_flush_upto will not.
752 			 */
753 			rap->rxa_start = rxseq;
754 		}
755 	} else {
756 		/*
757 		 * Out of range; toss.
758 		 * Sec 9.10.7.6 b) (D2.04 p.119 line 41)
759 		 */
760 		IEEE80211_DISCARD_MAC(vap,
761 		    IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
762 		    "BAR", "BA win <%u:%u> (%u frames) rxseq %u tid %u%s",
763 		    rap->rxa_start,
764 		    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
765 		    rap->rxa_qframes, rxseq, tid,
766 		    wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
767 		vap->iv_stats.is_ampdu_bar_oow++;
768 		IEEE80211_NODE_STAT(ni, rx_drop);
769 	}
770 }
771 
772 /*
773  * Setup HT-specific state in a node.  Called only
774  * when HT use is negotiated so we don't do extra
775  * work for temporary and/or legacy sta's.
776  */
777 void
778 ieee80211_ht_node_init(struct ieee80211_node *ni, const uint8_t *htcap)
779 {
780 	struct ieee80211_tx_ampdu *tap;
781 	int ac;
782 
783 	if (ni->ni_flags & IEEE80211_NODE_HT) {
784 		/*
785 		 * Clean AMPDU state on re-associate.  This handles the case
786 		 * where a station leaves w/o notifying us and then returns
787 		 * before node is reaped for inactivity.
788 		 */
789 		ieee80211_ht_node_cleanup(ni);
790 	}
791 	ieee80211_parse_htcap(ni, htcap);
792 	for (ac = 0; ac < WME_NUM_AC; ac++) {
793 		tap = &ni->ni_tx_ampdu[ac];
794 		tap->txa_ac = ac;
795 		/* NB: further initialization deferred */
796 	}
797 	ni->ni_flags |= IEEE80211_NODE_HT | IEEE80211_NODE_AMPDU;
798 }
799 
800 /*
801  * Cleanup HT-specific state in a node.  Called only
802  * when HT use has been marked.
803  */
804 void
805 ieee80211_ht_node_cleanup(struct ieee80211_node *ni)
806 {
807 	struct ieee80211com *ic = ni->ni_ic;
808 	int i;
809 
810 	KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT node"));
811 
812 	/* XXX optimize this */
813 	for (i = 0; i < WME_NUM_AC; i++) {
814 		struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[i];
815 		if (tap->txa_flags & IEEE80211_AGGR_SETUP) {
816 			/*
817 			 * Stop BA stream if setup so driver has a chance
818 			 * to reclaim any resources it might have allocated.
819 			 */
820 			ic->ic_addba_stop(ni, &ni->ni_tx_ampdu[i]);
821 			tap->txa_lastsample = 0;
822 			tap->txa_avgpps = 0;
823 			/* NB: clearing NAK means we may re-send ADDBA */
824 			tap->txa_flags &=
825 			    ~(IEEE80211_AGGR_SETUP | IEEE80211_AGGR_NAK);
826 		}
827 	}
828 	for (i = 0; i < WME_NUM_TID; i++)
829 		ampdu_rx_stop(&ni->ni_rx_ampdu[i]);
830 
831 	ni->ni_htcap = 0;
832 	ni->ni_flags &= ~IEEE80211_NODE_HT_ALL;
833 }
834 
835 /*
836  * Age out HT resources for a station.
837  */
838 void
839 ieee80211_ht_node_age(struct ieee80211_node *ni)
840 {
841 #ifdef IEEE80211_AMPDU_AGE
842 	struct ieee80211vap *vap = ni->ni_vap;
843 	uint8_t tid;
844 #endif
845 
846 	KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT sta"));
847 
848 #ifdef IEEE80211_AMPDU_AGE
849 	for (tid = 0; tid < WME_NUM_TID; tid++) {
850 		struct ieee80211_rx_ampdu *rap;
851 
852 		rap = &ni->ni_rx_ampdu[tid];
853 		if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0)
854 			continue;
855 		if (rap->rxa_qframes == 0)
856 			continue;
857 		/*
858 		 * Check for frames sitting too long in the reorder queue.
859 		 * See above for more details on what's happening here.
860 		 */
861 		/* XXX honor batimeout? */
862 		if (ticks - rap->rxa_age > ieee80211_ampdu_age) {
863 			/*
864 			 * Too long since we received the first
865 			 * frame; flush the reorder buffer.
866 			 */
867 			vap->iv_stats.is_ampdu_rx_age += rap->rxa_qframes;
868 			ampdu_rx_flush(ni, rap);
869 		}
870 	}
871 #endif /* IEEE80211_AMPDU_AGE */
872 }
873 
874 static struct ieee80211_channel *
875 findhtchan(struct ieee80211com *ic, struct ieee80211_channel *c, int htflags)
876 {
877 	return ieee80211_find_channel(ic, c->ic_freq,
878 	    (c->ic_flags &~ IEEE80211_CHAN_HT) | htflags);
879 }
880 
881 /*
882  * Adjust a channel to be HT/non-HT according to the vap's configuration.
883  */
884 struct ieee80211_channel *
885 ieee80211_ht_adjust_channel(struct ieee80211com *ic,
886 	struct ieee80211_channel *chan, int flags)
887 {
888 	struct ieee80211_channel *c;
889 
890 	if (flags & IEEE80211_FEXT_HT) {
891 		/* promote to HT if possible */
892 		if (flags & IEEE80211_FEXT_USEHT40) {
893 			if (!IEEE80211_IS_CHAN_HT40(chan)) {
894 				/* NB: arbitrarily pick ht40+ over ht40- */
895 				c = findhtchan(ic, chan, IEEE80211_CHAN_HT40U);
896 				if (c == NULL)
897 					c = findhtchan(ic, chan,
898 						IEEE80211_CHAN_HT40D);
899 				if (c == NULL)
900 					c = findhtchan(ic, chan,
901 						IEEE80211_CHAN_HT20);
902 				if (c != NULL)
903 					chan = c;
904 			}
905 		} else if (!IEEE80211_IS_CHAN_HT20(chan)) {
906 			c = findhtchan(ic, chan, IEEE80211_CHAN_HT20);
907 			if (c != NULL)
908 				chan = c;
909 		}
910 	} else if (IEEE80211_IS_CHAN_HT(chan)) {
911 		/* demote to legacy, HT use is disabled */
912 		c = ieee80211_find_channel(ic, chan->ic_freq,
913 		    chan->ic_flags &~ IEEE80211_CHAN_HT);
914 		if (c != NULL)
915 			chan = c;
916 	}
917 	return chan;
918 }
919 
920 /*
921  * Setup HT-specific state for a legacy WDS peer.
922  */
923 void
924 ieee80211_ht_wds_init(struct ieee80211_node *ni)
925 {
926 	struct ieee80211vap *vap = ni->ni_vap;
927 	struct ieee80211_tx_ampdu *tap;
928 	int ac;
929 
930 	KASSERT(vap->iv_flags_ext & IEEE80211_FEXT_HT, ("no HT requested"));
931 
932 	/* XXX check scan cache in case peer has an ap and we have info */
933 	/*
934 	 * If setup with a legacy channel; locate an HT channel.
935 	 * Otherwise if the inherited channel (from a companion
936 	 * AP) is suitable use it so we use the same location
937 	 * for the extension channel).
938 	 */
939 	ni->ni_chan = ieee80211_ht_adjust_channel(ni->ni_ic,
940 	    ni->ni_chan, ieee80211_htchanflags(ni->ni_chan));
941 
942 	ni->ni_htcap = 0;
943 	if (vap->iv_flags_ext & IEEE80211_FEXT_SHORTGI20)
944 		ni->ni_htcap |= IEEE80211_HTCAP_SHORTGI20;
945 	if (IEEE80211_IS_CHAN_HT40(ni->ni_chan)) {
946 		ni->ni_htcap |= IEEE80211_HTCAP_CHWIDTH40;
947 		ni->ni_chw = 40;
948 		if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan))
949 			ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_ABOVE;
950 		else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan))
951 			ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_BELOW;
952 		if (vap->iv_flags_ext & IEEE80211_FEXT_SHORTGI40)
953 			ni->ni_htcap |= IEEE80211_HTCAP_SHORTGI40;
954 	} else {
955 		ni->ni_chw = 20;
956 		ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_NONE;
957 	}
958 	ni->ni_htctlchan = ni->ni_chan->ic_ieee;
959 
960 	ni->ni_htopmode = 0;		/* XXX need protection state */
961 	ni->ni_htstbc = 0;		/* XXX need info */
962 
963 	for (ac = 0; ac < WME_NUM_AC; ac++) {
964 		tap = &ni->ni_tx_ampdu[ac];
965 		tap->txa_ac = ac;
966 	}
967 	/* NB: AMPDU tx/rx governed by IEEE80211_FEXT_AMPDU_{TX,RX} */
968 	ni->ni_flags |= IEEE80211_NODE_HT | IEEE80211_NODE_AMPDU;
969 }
970 
971 /*
972  * Notify hostap vaps of a change in the HTINFO ie.
973  */
974 static void
975 htinfo_notify(struct ieee80211com *ic)
976 {
977 	struct ieee80211vap *vap;
978 	int first = 1;
979 
980 	IEEE80211_LOCK_ASSERT(ic);
981 
982 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
983 		if (vap->iv_opmode != IEEE80211_M_HOSTAP)
984 			continue;
985 		if (first) {
986 			IEEE80211_NOTE(vap,
987 			    IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N,
988 			    vap->iv_bss,
989 			    "HT bss occupancy change: %d sta, %d ht, "
990 			    "%d ht40%s, HT protmode now 0x%x"
991 			    , ic->ic_sta_assoc
992 			    , ic->ic_ht_sta_assoc
993 			    , ic->ic_ht40_sta_assoc
994 			    , (ic->ic_flags_ext & IEEE80211_FEXT_NONHT_PR) ?
995 				 ", non-HT sta present" : ""
996 			    , ic->ic_curhtprotmode);
997 			first = 0;
998 		}
999 		ieee80211_beacon_notify(vap, IEEE80211_BEACON_HTINFO);
1000 	}
1001 }
1002 
1003 /*
1004  * Calculate HT protection mode from current
1005  * state and handle updates.
1006  */
1007 static void
1008 htinfo_update(struct ieee80211com *ic)
1009 {
1010 	uint8_t protmode;
1011 
1012 	if (ic->ic_sta_assoc != ic->ic_ht_sta_assoc) {
1013 		protmode = IEEE80211_HTINFO_OPMODE_MIXED
1014 			 | IEEE80211_HTINFO_NONHT_PRESENT;
1015 	} else if (ic->ic_flags_ext & IEEE80211_FEXT_NONHT_PR) {
1016 		protmode = IEEE80211_HTINFO_OPMODE_PROTOPT
1017 			 | IEEE80211_HTINFO_NONHT_PRESENT;
1018 	} else if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
1019 	    IEEE80211_IS_CHAN_HT40(ic->ic_bsschan) &&
1020 	    ic->ic_sta_assoc != ic->ic_ht40_sta_assoc) {
1021 		protmode = IEEE80211_HTINFO_OPMODE_HT20PR;
1022 	} else {
1023 		protmode = IEEE80211_HTINFO_OPMODE_PURE;
1024 	}
1025 	if (protmode != ic->ic_curhtprotmode) {
1026 		ic->ic_curhtprotmode = protmode;
1027 		htinfo_notify(ic);
1028 	}
1029 }
1030 
1031 /*
1032  * Handle an HT station joining a BSS.
1033  */
1034 void
1035 ieee80211_ht_node_join(struct ieee80211_node *ni)
1036 {
1037 	struct ieee80211com *ic = ni->ni_ic;
1038 
1039 	IEEE80211_LOCK_ASSERT(ic);
1040 
1041 	if (ni->ni_flags & IEEE80211_NODE_HT) {
1042 		ic->ic_ht_sta_assoc++;
1043 		if (ni->ni_chw == 40)
1044 			ic->ic_ht40_sta_assoc++;
1045 	}
1046 	htinfo_update(ic);
1047 }
1048 
1049 /*
1050  * Handle an HT station leaving a BSS.
1051  */
1052 void
1053 ieee80211_ht_node_leave(struct ieee80211_node *ni)
1054 {
1055 	struct ieee80211com *ic = ni->ni_ic;
1056 
1057 	IEEE80211_LOCK_ASSERT(ic);
1058 
1059 	if (ni->ni_flags & IEEE80211_NODE_HT) {
1060 		ic->ic_ht_sta_assoc--;
1061 		if (ni->ni_chw == 40)
1062 			ic->ic_ht40_sta_assoc--;
1063 	}
1064 	htinfo_update(ic);
1065 }
1066 
1067 /*
1068  * Public version of htinfo_update; used for processing
1069  * beacon frames from overlapping bss.
1070  *
1071  * Caller can specify either IEEE80211_HTINFO_OPMODE_MIXED
1072  * (on receipt of a beacon that advertises MIXED) or
1073  * IEEE80211_HTINFO_OPMODE_PROTOPT (on receipt of a beacon
1074  * from an overlapping legacy bss).  We treat MIXED with
1075  * a higher precedence than PROTOPT (i.e. we will not change
1076  * change PROTOPT -> MIXED; only MIXED -> PROTOPT).  This
1077  * corresponds to how we handle things in htinfo_update.
1078  */
1079 void
1080 ieee80211_htprot_update(struct ieee80211com *ic, int protmode)
1081 {
1082 #define	OPMODE(x)	SM(x, IEEE80211_HTINFO_OPMODE)
1083 	IEEE80211_LOCK(ic);
1084 
1085 	/* track non-HT station presence */
1086 	KASSERT(protmode & IEEE80211_HTINFO_NONHT_PRESENT,
1087 	    ("protmode 0x%x", protmode));
1088 	ic->ic_flags_ext |= IEEE80211_FEXT_NONHT_PR;
1089 	ic->ic_lastnonht = ticks;
1090 
1091 	if (protmode != ic->ic_curhtprotmode &&
1092 	    (OPMODE(ic->ic_curhtprotmode) != IEEE80211_HTINFO_OPMODE_MIXED ||
1093 	     OPMODE(protmode) == IEEE80211_HTINFO_OPMODE_PROTOPT)) {
1094 		/* push beacon update */
1095 		ic->ic_curhtprotmode = protmode;
1096 		htinfo_notify(ic);
1097 	}
1098 	IEEE80211_UNLOCK(ic);
1099 #undef OPMODE
1100 }
1101 
1102 /*
1103  * Time out presence of an overlapping bss with non-HT
1104  * stations.  When operating in hostap mode we listen for
1105  * beacons from other stations and if we identify a non-HT
1106  * station is present we update the opmode field of the
1107  * HTINFO ie.  To identify when all non-HT stations are
1108  * gone we time out this condition.
1109  */
1110 void
1111 ieee80211_ht_timeout(struct ieee80211com *ic)
1112 {
1113 	IEEE80211_LOCK_ASSERT(ic);
1114 
1115 	if ((ic->ic_flags_ext & IEEE80211_FEXT_NONHT_PR) &&
1116 	    time_after(ticks, ic->ic_lastnonht + IEEE80211_NONHT_PRESENT_AGE)) {
1117 #if 0
1118 		IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
1119 		    "%s", "time out non-HT STA present on channel");
1120 #endif
1121 		ic->ic_flags_ext &= ~IEEE80211_FEXT_NONHT_PR;
1122 		htinfo_update(ic);
1123 	}
1124 }
1125 
1126 /* unalligned little endian access */
1127 #define LE_READ_2(p)					\
1128 	((uint16_t)					\
1129 	 ((((const uint8_t *)(p))[0]      ) |		\
1130 	  (((const uint8_t *)(p))[1] <<  8)))
1131 
1132 /*
1133  * Process an 802.11n HT capabilities ie.
1134  */
1135 void
1136 ieee80211_parse_htcap(struct ieee80211_node *ni, const uint8_t *ie)
1137 {
1138 	struct ieee80211vap *vap = ni->ni_vap;
1139 
1140 	if (ie[0] == IEEE80211_ELEMID_VENDOR) {
1141 		/*
1142 		 * Station used Vendor OUI ie to associate;
1143 		 * mark the node so when we respond we'll use
1144 		 * the Vendor OUI's and not the standard ie's.
1145 		 */
1146 		ni->ni_flags |= IEEE80211_NODE_HTCOMPAT;
1147 		ie += 4;
1148 	} else
1149 		ni->ni_flags &= ~IEEE80211_NODE_HTCOMPAT;
1150 
1151 	ni->ni_htcap = LE_READ_2(ie +
1152 		__offsetof(struct ieee80211_ie_htcap, hc_cap));
1153 	ni->ni_htparam = ie[__offsetof(struct ieee80211_ie_htcap, hc_param)];
1154 	/* XXX needed or will ieee80211_parse_htinfo always be called? */
1155 	ni->ni_chw = (ni->ni_htcap & IEEE80211_HTCAP_CHWIDTH40) &&
1156 		     (vap->iv_flags_ext & IEEE80211_FEXT_USEHT40) ? 40 : 20;
1157 }
1158 
1159 /*
1160  * Process an 802.11n HT info ie and update the node state.
1161  * Note that we handle use this information to identify the
1162  * correct channel (HT20, HT40+, HT40-, legacy).  The caller
1163  * is responsible for insuring any required channel change is
1164  * done (e.g. in sta mode when parsing the contents of a
1165  * beacon frame).
1166  */
1167 void
1168 ieee80211_parse_htinfo(struct ieee80211_node *ni, const uint8_t *ie)
1169 {
1170 	struct ieee80211com *ic = ni->ni_ic;
1171 	struct ieee80211vap *vap = ni->ni_vap;
1172  	const struct ieee80211_ie_htinfo *htinfo;
1173 	struct ieee80211_channel *c;
1174 	uint16_t w;
1175 	int htflags, chanflags;
1176 
1177 	if (ie[0] == IEEE80211_ELEMID_VENDOR)
1178 		ie += 4;
1179  	htinfo = (const struct ieee80211_ie_htinfo *) ie;
1180 	ni->ni_htctlchan = htinfo->hi_ctrlchannel;
1181 	ni->ni_ht2ndchan = SM(htinfo->hi_byte1, IEEE80211_HTINFO_2NDCHAN);
1182 	w = LE_READ_2(&htinfo->hi_byte2);
1183 	ni->ni_htopmode = SM(w, IEEE80211_HTINFO_OPMODE);
1184 	w = LE_READ_2(&htinfo->hi_byte45);
1185 	ni->ni_htstbc = SM(w, IEEE80211_HTINFO_BASIC_STBCMCS);
1186 	/*
1187 	 * Handle 11n channel switch.  Use the received HT ie's to
1188 	 * identify the right channel to use.  If we cannot locate it
1189 	 * in the channel table then fallback to legacy operation.
1190 	 */
1191 	/* NB: honor operating mode constraint */
1192 	htflags = (vap->iv_flags_ext & IEEE80211_FEXT_HT) ?
1193 	    IEEE80211_CHAN_HT20 : 0;
1194 	if ((htinfo->hi_byte1 & IEEE80211_HTINFO_TXWIDTH_2040) &&
1195 	    (vap->iv_flags_ext & IEEE80211_FEXT_USEHT40)) {
1196 		if (ni->ni_ht2ndchan == IEEE80211_HTINFO_2NDCHAN_ABOVE)
1197 			htflags = IEEE80211_CHAN_HT40U;
1198 		else if (ni->ni_ht2ndchan == IEEE80211_HTINFO_2NDCHAN_BELOW)
1199 			htflags = IEEE80211_CHAN_HT40D;
1200 	}
1201 	chanflags = (ni->ni_chan->ic_flags &~ IEEE80211_CHAN_HT) | htflags;
1202 	if (chanflags != ni->ni_chan->ic_flags) {
1203 		c = ieee80211_find_channel(ic, ni->ni_chan->ic_freq, chanflags);
1204 		if (c == NULL && (htflags & IEEE80211_CHAN_HT40)) {
1205 			/*
1206 			 * No HT40 channel entry in our table; fall back
1207 			 * to HT20 operation.  This should not happen.
1208 			 */
1209 			c = findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT20);
1210 			IEEE80211_NOTE(vap,
1211 			    IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
1212 			    "no HT40 channel (freq %u), falling back to HT20",
1213 			    ni->ni_chan->ic_freq);
1214 			/* XXX stat */
1215 		}
1216 		if (c != NULL && c != ni->ni_chan) {
1217 			IEEE80211_NOTE(vap,
1218 			    IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
1219 			    "switch station to HT%d channel %u/0x%x",
1220 			    IEEE80211_IS_CHAN_HT40(c) ? 40 : 20,
1221 			    c->ic_freq, c->ic_flags);
1222 			ni->ni_chan = c;
1223 		}
1224 		/* NB: caller responsible for forcing any channel change */
1225 	}
1226 	/* update node's tx channel width */
1227 	ni->ni_chw = IEEE80211_IS_CHAN_HT40(ni->ni_chan)? 40 : 20;
1228 }
1229 
1230 /*
1231  * Install received HT rate set by parsing the HT cap ie.
1232  */
1233 int
1234 ieee80211_setup_htrates(struct ieee80211_node *ni, const uint8_t *ie, int flags)
1235 {
1236 	struct ieee80211vap *vap = ni->ni_vap;
1237 	const struct ieee80211_ie_htcap *htcap;
1238 	struct ieee80211_htrateset *rs;
1239 	int i;
1240 
1241 	rs = &ni->ni_htrates;
1242 	memset(rs, 0, sizeof(*rs));
1243 	if (ie != NULL) {
1244 		if (ie[0] == IEEE80211_ELEMID_VENDOR)
1245 			ie += 4;
1246 		htcap = (const struct ieee80211_ie_htcap *) ie;
1247 		for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++) {
1248 			if (isclr(htcap->hc_mcsset, i))
1249 				continue;
1250 			if (rs->rs_nrates == IEEE80211_HTRATE_MAXSIZE) {
1251 				IEEE80211_NOTE(vap,
1252 				    IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
1253 				    "WARNING, HT rate set too large; only "
1254 				    "using %u rates", IEEE80211_HTRATE_MAXSIZE);
1255 				vap->iv_stats.is_rx_rstoobig++;
1256 				break;
1257 			}
1258 			rs->rs_rates[rs->rs_nrates++] = i;
1259 		}
1260 	}
1261 	return ieee80211_fix_rate(ni, (struct ieee80211_rateset *) rs, flags);
1262 }
1263 
1264 /*
1265  * Mark rates in a node's HT rate set as basic according
1266  * to the information in the supplied HT info ie.
1267  */
1268 void
1269 ieee80211_setup_basic_htrates(struct ieee80211_node *ni, const uint8_t *ie)
1270 {
1271 	const struct ieee80211_ie_htinfo *htinfo;
1272 	struct ieee80211_htrateset *rs;
1273 	int i, j;
1274 
1275 	if (ie[0] == IEEE80211_ELEMID_VENDOR)
1276 		ie += 4;
1277 	htinfo = (const struct ieee80211_ie_htinfo *) ie;
1278 	rs = &ni->ni_htrates;
1279 	if (rs->rs_nrates == 0) {
1280 		IEEE80211_NOTE(ni->ni_vap,
1281 		    IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
1282 		    "%s", "WARNING, empty HT rate set");
1283 		return;
1284 	}
1285 	for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++) {
1286 		if (isclr(htinfo->hi_basicmcsset, i))
1287 			continue;
1288 		for (j = 0; j < rs->rs_nrates; j++)
1289 			if ((rs->rs_rates[j] & IEEE80211_RATE_VAL) == i)
1290 				rs->rs_rates[j] |= IEEE80211_RATE_BASIC;
1291 	}
1292 }
1293 
1294 static void
1295 addba_timeout(void *arg)
1296 {
1297 	struct ieee80211_tx_ampdu *tap = arg;
1298 
1299 	/* XXX ? */
1300 	tap->txa_flags &= ~IEEE80211_AGGR_XCHGPEND;
1301 	tap->txa_attempts++;
1302 }
1303 
1304 static void
1305 addba_start_timeout(struct ieee80211_tx_ampdu *tap)
1306 {
1307 	/* XXX use CALLOUT_PENDING instead? */
1308 	callout_reset(&tap->txa_timer, ieee80211_addba_timeout,
1309 	    addba_timeout, tap);
1310 	tap->txa_flags |= IEEE80211_AGGR_XCHGPEND;
1311 	tap->txa_nextrequest = ticks + ieee80211_addba_timeout;
1312 }
1313 
1314 static void
1315 addba_stop_timeout(struct ieee80211_tx_ampdu *tap)
1316 {
1317 	/* XXX use CALLOUT_PENDING instead? */
1318 	if (tap->txa_flags & IEEE80211_AGGR_XCHGPEND) {
1319 		callout_stop(&tap->txa_timer);
1320 		tap->txa_flags &= ~IEEE80211_AGGR_XCHGPEND;
1321 	}
1322 }
1323 
1324 /*
1325  * Default method for requesting A-MPDU tx aggregation.
1326  * We setup the specified state block and start a timer
1327  * to wait for an ADDBA response frame.
1328  */
1329 static int
1330 ieee80211_addba_request(struct ieee80211_node *ni,
1331 	struct ieee80211_tx_ampdu *tap,
1332 	int dialogtoken, int baparamset, int batimeout)
1333 {
1334 	int bufsiz;
1335 
1336 	/* XXX locking */
1337 	tap->txa_token = dialogtoken;
1338 	tap->txa_flags |= IEEE80211_AGGR_IMMEDIATE;
1339 	tap->txa_start = 0;
1340 	bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
1341 	tap->txa_wnd = (bufsiz == 0) ?
1342 	    IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
1343 	addba_start_timeout(tap);
1344 	return 1;
1345 }
1346 
1347 /*
1348  * Default method for processing an A-MPDU tx aggregation
1349  * response.  We shutdown any pending timer and update the
1350  * state block according to the reply.
1351  */
1352 static int
1353 ieee80211_addba_response(struct ieee80211_node *ni,
1354 	struct ieee80211_tx_ampdu *tap,
1355 	int status, int baparamset, int batimeout)
1356 {
1357 	int bufsiz;
1358 
1359 	/* XXX locking */
1360 	addba_stop_timeout(tap);
1361 	if (status == IEEE80211_STATUS_SUCCESS) {
1362 		bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
1363 		/* XXX override our request? */
1364 		tap->txa_wnd = (bufsiz == 0) ?
1365 		    IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
1366 		tap->txa_flags |= IEEE80211_AGGR_RUNNING;
1367 	} else {
1368 		/* mark tid so we don't try again */
1369 		tap->txa_flags |= IEEE80211_AGGR_NAK;
1370 	}
1371 	return 1;
1372 }
1373 
1374 /*
1375  * Default method for stopping A-MPDU tx aggregation.
1376  * Any timer is cleared and we drain any pending frames.
1377  */
1378 static void
1379 ieee80211_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
1380 {
1381 	/* XXX locking */
1382 	addba_stop_timeout(tap);
1383 	if (tap->txa_flags & IEEE80211_AGGR_RUNNING) {
1384 		/* XXX clear aggregation queue */
1385 		tap->txa_flags &= ~IEEE80211_AGGR_RUNNING;
1386 	}
1387 	tap->txa_attempts = 0;
1388 }
1389 
1390 /*
1391  * Process a received action frame using the default aggregation
1392  * policy.  We intercept ADDBA-related frames and use them to
1393  * update our aggregation state.  All other frames are passed up
1394  * for processing by ieee80211_recv_action.
1395  */
1396 static void
1397 ieee80211_aggr_recv_action(struct ieee80211_node *ni,
1398 	const uint8_t *frm, const uint8_t *efrm)
1399 {
1400 	struct ieee80211com *ic = ni->ni_ic;
1401 	struct ieee80211vap *vap = ni->ni_vap;
1402 	const struct ieee80211_action *ia;
1403 	struct ieee80211_rx_ampdu *rap;
1404 	struct ieee80211_tx_ampdu *tap;
1405 	uint8_t dialogtoken, policy;
1406 	uint16_t baparamset, batimeout, baseqctl, code;
1407 	uint16_t args[4];
1408 	int tid, ac, bufsiz;
1409 
1410 	ia = (const struct ieee80211_action *) frm;
1411 	switch (ia->ia_category) {
1412 	case IEEE80211_ACTION_CAT_BA:
1413 		switch (ia->ia_action) {
1414 		case IEEE80211_ACTION_BA_ADDBA_REQUEST:
1415 			dialogtoken = frm[2];
1416 			baparamset = LE_READ_2(frm+3);
1417 			batimeout = LE_READ_2(frm+5);
1418 			baseqctl = LE_READ_2(frm+7);
1419 
1420 			tid = MS(baparamset, IEEE80211_BAPS_TID);
1421 			bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
1422 
1423 			IEEE80211_NOTE(vap,
1424 			    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1425 			    "recv ADDBA request: dialogtoken %u "
1426 			    "baparamset 0x%x (tid %d bufsiz %d) batimeout %d "
1427 			    "baseqctl %d:%d",
1428 			    dialogtoken, baparamset, tid, bufsiz, batimeout,
1429 			    MS(baseqctl, IEEE80211_BASEQ_START),
1430 			    MS(baseqctl, IEEE80211_BASEQ_FRAG));
1431 
1432 			rap = &ni->ni_rx_ampdu[tid];
1433 
1434 			/* Send ADDBA response */
1435 			args[0] = dialogtoken;
1436 			/*
1437 			 * NB: We ack only if the sta associated with HT and
1438 			 * the ap is configured to do AMPDU rx (the latter
1439 			 * violates the 11n spec and is mostly for testing).
1440 			 */
1441 			if ((ni->ni_flags & IEEE80211_NODE_AMPDU_RX) &&
1442 			    (vap->iv_flags_ext & IEEE80211_FEXT_AMPDU_RX)) {
1443 				ampdu_rx_start(rap, bufsiz,
1444 				    MS(baseqctl, IEEE80211_BASEQ_START));
1445 
1446 				args[1] = IEEE80211_STATUS_SUCCESS;
1447 			} else {
1448 				IEEE80211_NOTE(vap,
1449 				    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1450 				    ni, "reject ADDBA request: %s",
1451 				    ni->ni_flags & IEEE80211_NODE_AMPDU_RX ?
1452 				       "administratively disabled" :
1453 				       "not negotiated for station");
1454 				vap->iv_stats.is_addba_reject++;
1455 				args[1] = IEEE80211_STATUS_UNSPECIFIED;
1456 			}
1457 			/* XXX honor rap flags? */
1458 			args[2] = IEEE80211_BAPS_POLICY_IMMEDIATE
1459 				| SM(tid, IEEE80211_BAPS_TID)
1460 				| SM(rap->rxa_wnd, IEEE80211_BAPS_BUFSIZ)
1461 				;
1462 			args[3] = 0;
1463 			ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
1464 				IEEE80211_ACTION_BA_ADDBA_RESPONSE, args);
1465 			return;
1466 
1467 		case IEEE80211_ACTION_BA_ADDBA_RESPONSE:
1468 			dialogtoken = frm[2];
1469 			code = LE_READ_2(frm+3);
1470 			baparamset = LE_READ_2(frm+5);
1471 			tid = MS(baparamset, IEEE80211_BAPS_TID);
1472 			bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
1473 			policy = MS(baparamset, IEEE80211_BAPS_POLICY);
1474 			batimeout = LE_READ_2(frm+7);
1475 
1476 			ac = TID_TO_WME_AC(tid);
1477 			tap = &ni->ni_tx_ampdu[ac];
1478 			if ((tap->txa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
1479 				IEEE80211_DISCARD_MAC(vap,
1480 				    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1481 				    ni->ni_macaddr, "ADDBA response",
1482 				    "no pending ADDBA, tid %d dialogtoken %u "
1483 				    "code %d", tid, dialogtoken, code);
1484 				vap->iv_stats.is_addba_norequest++;
1485 				return;
1486 			}
1487 			if (dialogtoken != tap->txa_token) {
1488 				IEEE80211_DISCARD_MAC(vap,
1489 				    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1490 				    ni->ni_macaddr, "ADDBA response",
1491 				    "dialogtoken mismatch: waiting for %d, "
1492 				    "received %d, tid %d code %d",
1493 				    tap->txa_token, dialogtoken, tid, code);
1494 				vap->iv_stats.is_addba_badtoken++;
1495 				return;
1496 			}
1497 			/* NB: assumes IEEE80211_AGGR_IMMEDIATE is 1 */
1498 			if (policy != (tap->txa_flags & IEEE80211_AGGR_IMMEDIATE)) {
1499 				IEEE80211_DISCARD_MAC(vap,
1500 				    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1501 				    ni->ni_macaddr, "ADDBA response",
1502 				    "policy mismatch: expecting %s, "
1503 				    "received %s, tid %d code %d",
1504 				    tap->txa_flags & IEEE80211_AGGR_IMMEDIATE,
1505 				    policy, tid, code);
1506 				vap->iv_stats.is_addba_badpolicy++;
1507 				return;
1508 			}
1509 #if 0
1510 			/* XXX we take MIN in ieee80211_addba_response */
1511 			if (bufsiz > IEEE80211_AGGR_BAWMAX) {
1512 				IEEE80211_DISCARD_MAC(vap,
1513 				    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1514 				    ni->ni_macaddr, "ADDBA response",
1515 				    "BA window too large: max %d, "
1516 				    "received %d, tid %d code %d",
1517 				    bufsiz, IEEE80211_AGGR_BAWMAX, tid, code);
1518 				vap->iv_stats.is_addba_badbawinsize++;
1519 				return;
1520 			}
1521 #endif
1522 
1523 			IEEE80211_NOTE(vap,
1524 			    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1525 			    "recv ADDBA response: dialogtoken %u code %d "
1526 			    "baparamset 0x%x (tid %d bufsiz %d) batimeout %d",
1527 			    dialogtoken, code, baparamset, tid, bufsiz,
1528 			    batimeout);
1529 			ic->ic_addba_response(ni, tap,
1530 				code, baparamset, batimeout);
1531 			return;
1532 
1533 		case IEEE80211_ACTION_BA_DELBA:
1534 			baparamset = LE_READ_2(frm+2);
1535 			code = LE_READ_2(frm+4);
1536 
1537 			tid = MS(baparamset, IEEE80211_DELBAPS_TID);
1538 
1539 			IEEE80211_NOTE(vap,
1540 			    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1541 			    "recv DELBA: baparamset 0x%x (tid %d initiator %d) "
1542 			    "code %d", baparamset, tid,
1543 			    MS(baparamset, IEEE80211_DELBAPS_INIT), code);
1544 
1545 			if ((baparamset & IEEE80211_DELBAPS_INIT) == 0) {
1546 				ac = TID_TO_WME_AC(tid);
1547 				tap = &ni->ni_tx_ampdu[ac];
1548 				ic->ic_addba_stop(ni, tap);
1549 			} else {
1550 				rap = &ni->ni_rx_ampdu[tid];
1551 				ampdu_rx_stop(rap);
1552 			}
1553 			return;
1554 		}
1555 		break;
1556 	}
1557 	ieee80211_recv_action(ni, frm, efrm);
1558 }
1559 
1560 /*
1561  * Process a received 802.11n action frame.
1562  * Aggregation-related frames are assumed to be handled
1563  * already; we handle any other frames we can, otherwise
1564  * complain about being unsupported (with debugging).
1565  */
1566 void
1567 ieee80211_recv_action(struct ieee80211_node *ni,
1568 	const uint8_t *frm, const uint8_t *efrm)
1569 {
1570 	struct ieee80211vap *vap = ni->ni_vap;
1571 	const struct ieee80211_action *ia;
1572 	int chw;
1573 
1574 	ia = (const struct ieee80211_action *) frm;
1575 	switch (ia->ia_category) {
1576 	case IEEE80211_ACTION_CAT_BA:
1577 		IEEE80211_NOTE(vap,
1578 		    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1579 		    "%s: BA action %d not implemented", __func__,
1580 		    ia->ia_action);
1581 		vap->iv_stats.is_rx_mgtdiscard++;
1582 		break;
1583 	case IEEE80211_ACTION_CAT_HT:
1584 		switch (ia->ia_action) {
1585 		case IEEE80211_ACTION_HT_TXCHWIDTH:
1586 			chw = frm[2] == IEEE80211_A_HT_TXCHWIDTH_2040 ? 40 : 20;
1587 			IEEE80211_NOTE(vap,
1588 			    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1589 		            "%s: HT txchwidth, width %d%s",
1590 			    __func__, chw, ni->ni_chw != chw ? "*" : "");
1591 			if (chw != ni->ni_chw) {
1592 				ni->ni_chw = chw;
1593 				/* XXX notify on change */
1594 			}
1595 			break;
1596 		case IEEE80211_ACTION_HT_MIMOPWRSAVE:
1597 			IEEE80211_NOTE(vap,
1598 			    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1599 		            "%s: HT MIMO PS", __func__);
1600 			break;
1601 		default:
1602 			IEEE80211_NOTE(vap,
1603 			   IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1604 		           "%s: HT action %d not implemented", __func__,
1605 			   ia->ia_action);
1606 			vap->iv_stats.is_rx_mgtdiscard++;
1607 			break;
1608 		}
1609 		break;
1610 	default:
1611 		IEEE80211_NOTE(vap,
1612 		    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1613 		    "%s: category %d not implemented", __func__,
1614 		    ia->ia_category);
1615 		vap->iv_stats.is_rx_mgtdiscard++;
1616 		break;
1617 	}
1618 }
1619 
1620 /*
1621  * Transmit processing.
1622  */
1623 
1624 /*
1625  * Check if A-MPDU should be requested/enabled for a stream.
1626  * We require a traffic rate above a per-AC threshold and we
1627  * also handle backoff from previous failed attempts.
1628  *
1629  * Drivers may override this method to bring in information
1630  * such as link state conditions in making the decision.
1631  */
1632 static int
1633 ieee80211_ampdu_enable(struct ieee80211_node *ni,
1634 	struct ieee80211_tx_ampdu *tap)
1635 {
1636 	struct ieee80211vap *vap = ni->ni_vap;
1637 
1638 	if (tap->txa_avgpps < vap->iv_ampdu_mintraffic[tap->txa_ac])
1639 		return 0;
1640 	/* XXX check rssi? */
1641 	if (tap->txa_attempts >= ieee80211_addba_maxtries &&
1642 	    ticks < tap->txa_nextrequest) {
1643 		/*
1644 		 * Don't retry too often; txa_nextrequest is set
1645 		 * to the minimum interval we'll retry after
1646 		 * ieee80211_addba_maxtries failed attempts are made.
1647 		 */
1648 		return 0;
1649 	}
1650 	IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
1651 	    "%s: enable AMPDU on %s, avgpps %d pkts %d",
1652 	    __func__, ieee80211_wme_acnames[tap->txa_ac],
1653 	    tap->txa_avgpps, tap->txa_pkts);
1654 	return 1;
1655 }
1656 
1657 /*
1658  * Request A-MPDU tx aggregation.  Setup local state and
1659  * issue an ADDBA request.  BA use will only happen after
1660  * the other end replies with ADDBA response.
1661  */
1662 int
1663 ieee80211_ampdu_request(struct ieee80211_node *ni,
1664 	struct ieee80211_tx_ampdu *tap)
1665 {
1666 	struct ieee80211com *ic = ni->ni_ic;
1667 	uint16_t args[4];
1668 	int tid, dialogtoken;
1669 	static int tokens = 0;	/* XXX */
1670 
1671 	/* XXX locking */
1672 	if ((tap->txa_flags & IEEE80211_AGGR_SETUP) == 0) {
1673 		/* do deferred setup of state */
1674 		callout_init(&tap->txa_timer, CALLOUT_MPSAFE);
1675 		tap->txa_flags |= IEEE80211_AGGR_SETUP;
1676 	}
1677 	/* XXX hack for not doing proper locking */
1678 	tap->txa_flags &= ~IEEE80211_AGGR_NAK;
1679 
1680 	dialogtoken = (tokens+1) % 63;		/* XXX */
1681 	tid = WME_AC_TO_TID(tap->txa_ac);
1682 	tap->txa_start = ni->ni_txseqs[tid];
1683 
1684 	tid = WME_AC_TO_TID(tap->txa_ac);
1685 	args[0] = dialogtoken;
1686 	args[1]	= IEEE80211_BAPS_POLICY_IMMEDIATE
1687 		| SM(tid, IEEE80211_BAPS_TID)
1688 		| SM(IEEE80211_AGGR_BAWMAX, IEEE80211_BAPS_BUFSIZ)
1689 		;
1690 	args[2] = 0;	/* batimeout */
1691 	/* NB: do first so there's no race against reply */
1692 	if (!ic->ic_addba_request(ni, tap, dialogtoken, args[1], args[2])) {
1693 		/* unable to setup state, don't make request */
1694 		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
1695 		    ni, "%s: could not setup BA stream for AC %d",
1696 		    __func__, tap->txa_ac);
1697 		/* defer next try so we don't slam the driver with requests */
1698 		tap->txa_attempts = ieee80211_addba_maxtries;
1699 		/* NB: check in case driver wants to override */
1700 		if (tap->txa_nextrequest <= ticks)
1701 			tap->txa_nextrequest = ticks + ieee80211_addba_backoff;
1702 		return 0;
1703 	}
1704 	tokens = dialogtoken;			/* allocate token */
1705 	/* NB: after calling ic_addba_request so driver can set txa_start */
1706 	args[3] = SM(tap->txa_start, IEEE80211_BASEQ_START)
1707 		| SM(0, IEEE80211_BASEQ_FRAG)
1708 		;
1709 	return ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
1710 		IEEE80211_ACTION_BA_ADDBA_REQUEST, args);
1711 }
1712 
1713 /*
1714  * Terminate an AMPDU tx stream.  State is reclaimed
1715  * and the peer notified with a DelBA Action frame.
1716  */
1717 void
1718 ieee80211_ampdu_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
1719 {
1720 	struct ieee80211com *ic = ni->ni_ic;
1721 	struct ieee80211vap *vap = ni->ni_vap;
1722 	uint16_t args[4];
1723 
1724 	/* XXX locking */
1725 	if (IEEE80211_AMPDU_RUNNING(tap)) {
1726 		IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1727 		    ni, "%s: stop BA stream for AC %d", __func__, tap->txa_ac);
1728 		vap->iv_stats.is_ampdu_stop++;
1729 
1730 		ic->ic_addba_stop(ni, tap);
1731 		args[0] = WME_AC_TO_TID(tap->txa_ac);
1732 		args[1] = IEEE80211_DELBAPS_INIT;
1733 		args[2] = 1;				/* XXX reason code */
1734 		ieee80211_send_action(ni, IEEE80211_ACTION_CAT_BA,
1735 			IEEE80211_ACTION_BA_DELBA, args);
1736 	} else {
1737 		IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1738 		    ni, "%s: BA stream for AC %d not running",
1739 		    __func__, tap->txa_ac);
1740 		vap->iv_stats.is_ampdu_stop_failed++;
1741 	}
1742 }
1743 
1744 /*
1745  * Transmit a BAR frame to the specified node.  The
1746  * BAR contents are drawn from the supplied aggregation
1747  * state associated with the node.
1748  */
1749 int
1750 ieee80211_send_bar(struct ieee80211_node *ni,
1751 	const struct ieee80211_tx_ampdu *tap)
1752 {
1753 #define	senderr(_x, _v)	do { vap->iv_stats._v++; ret = _x; goto bad; } while (0)
1754 #define	ADDSHORT(frm, v) do {			\
1755 	frm[0] = (v) & 0xff;			\
1756 	frm[1] = (v) >> 8;			\
1757 	frm += 2;				\
1758 } while (0)
1759 	struct ieee80211vap *vap = ni->ni_vap;
1760 	struct ieee80211com *ic = ni->ni_ic;
1761 	struct ieee80211_frame_min *wh;
1762 	struct mbuf *m;
1763 	uint8_t *frm;
1764 	uint16_t barctl, barseqctl;
1765 	int tid, ret;
1766 
1767 	ieee80211_ref_node(ni);
1768 
1769 	m = ieee80211_getmgtframe(&frm,
1770 		ic->ic_headroom + sizeof(struct ieee80211_frame_min),
1771 		sizeof(struct ieee80211_ba_request)
1772 	);
1773 	if (m == NULL)
1774 		senderr(ENOMEM, is_tx_nobuf);
1775 
1776 	wh = mtod(m, struct ieee80211_frame_min *);
1777 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 |
1778 		IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_BAR;
1779 	wh->i_fc[1] = 0;
1780 	IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
1781 	IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1782 
1783 	tid = WME_AC_TO_TID(tap->txa_ac);
1784 	barctl 	= (tap->txa_flags & IEEE80211_AGGR_IMMEDIATE ?
1785 			IEEE80211_BAPS_POLICY_IMMEDIATE :
1786 			IEEE80211_BAPS_POLICY_DELAYED)
1787 		| SM(tid, IEEE80211_BAPS_TID)
1788 		| SM(tap->txa_wnd, IEEE80211_BAPS_BUFSIZ)
1789 		;
1790 	barseqctl = SM(tap->txa_start, IEEE80211_BASEQ_START)
1791 		| SM(0, IEEE80211_BASEQ_FRAG)
1792 		;
1793 	ADDSHORT(frm, barctl);
1794 	ADDSHORT(frm, barseqctl);
1795 	m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
1796 
1797 	M_WME_SETAC(m, WME_AC_VO);
1798 
1799 	IEEE80211_NODE_STAT(ni, tx_mgmt);	/* XXX tx_ctl? */
1800 
1801 	IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
1802 	    ni, "send bar frame (tid %u start %u) on channel %u",
1803 	    tid, tap->txa_start, ieee80211_chan2ieee(ic, ic->ic_curchan));
1804 
1805 	return ic->ic_raw_xmit(ni, m, NULL);
1806 bad:
1807 	ieee80211_free_node(ni);
1808 	return ret;
1809 #undef ADDSHORT
1810 #undef senderr
1811 }
1812 
1813 /*
1814  * Send an action management frame.  The arguments are stuff
1815  * into a frame without inspection; the caller is assumed to
1816  * prepare them carefully (e.g. based on the aggregation state).
1817  */
1818 int
1819 ieee80211_send_action(struct ieee80211_node *ni,
1820 	int category, int action, uint16_t args[4])
1821 {
1822 #define	senderr(_x, _v)	do { vap->iv_stats._v++; ret = _x; goto bad; } while (0)
1823 #define	ADDSHORT(frm, v) do {			\
1824 	frm[0] = (v) & 0xff;			\
1825 	frm[1] = (v) >> 8;			\
1826 	frm += 2;				\
1827 } while (0)
1828 	struct ieee80211vap *vap = ni->ni_vap;
1829 	struct ieee80211com *ic = ni->ni_ic;
1830 	struct mbuf *m;
1831 	uint8_t *frm;
1832 	uint16_t baparamset;
1833 	int ret;
1834 
1835 	KASSERT(ni != NULL, ("null node"));
1836 
1837 	/*
1838 	 * Hold a reference on the node so it doesn't go away until after
1839 	 * the xmit is complete all the way in the driver.  On error we
1840 	 * will remove our reference.
1841 	 */
1842 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1843 		"ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
1844 		__func__, __LINE__,
1845 		ni, ether_sprintf(ni->ni_macaddr),
1846 		ieee80211_node_refcnt(ni)+1);
1847 	ieee80211_ref_node(ni);
1848 
1849 	m = ieee80211_getmgtframe(&frm,
1850 		ic->ic_headroom + sizeof(struct ieee80211_frame),
1851 		  sizeof(uint16_t)	/* action+category */
1852 		/* XXX may action payload */
1853 		+ sizeof(struct ieee80211_action_ba_addbaresponse)
1854 	);
1855 	if (m == NULL)
1856 		senderr(ENOMEM, is_tx_nobuf);
1857 
1858 	*frm++ = category;
1859 	*frm++ = action;
1860 	switch (category) {
1861 	case IEEE80211_ACTION_CAT_BA:
1862 		switch (action) {
1863 		case IEEE80211_ACTION_BA_ADDBA_REQUEST:
1864 			IEEE80211_NOTE(vap,
1865 			    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1866 			    "send ADDBA request: dialogtoken %d "
1867 			    "baparamset 0x%x (tid %d) batimeout 0x%x baseqctl 0x%x",
1868 			    args[0], args[1], MS(args[1], IEEE80211_BAPS_TID),
1869 			    args[2], args[3]);
1870 
1871 			*frm++ = args[0];	/* dialog token */
1872 			ADDSHORT(frm, args[1]);	/* baparamset */
1873 			ADDSHORT(frm, args[2]);	/* batimeout */
1874 			ADDSHORT(frm, args[3]);	/* baseqctl */
1875 			break;
1876 		case IEEE80211_ACTION_BA_ADDBA_RESPONSE:
1877 			IEEE80211_NOTE(vap,
1878 			    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1879 			    "send ADDBA response: dialogtoken %d status %d "
1880 			    "baparamset 0x%x (tid %d) batimeout %d",
1881 			    args[0], args[1], args[2],
1882 			    MS(args[2], IEEE80211_BAPS_TID), args[3]);
1883 
1884 			*frm++ = args[0];	/* dialog token */
1885 			ADDSHORT(frm, args[1]);	/* statuscode */
1886 			ADDSHORT(frm, args[2]);	/* baparamset */
1887 			ADDSHORT(frm, args[3]);	/* batimeout */
1888 			break;
1889 		case IEEE80211_ACTION_BA_DELBA:
1890 			/* XXX */
1891 			baparamset = SM(args[0], IEEE80211_DELBAPS_TID)
1892 				   | args[1]
1893 				   ;
1894 			ADDSHORT(frm, baparamset);
1895 			ADDSHORT(frm, args[2]);	/* reason code */
1896 
1897 			IEEE80211_NOTE(vap,
1898 			    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1899 			    "send DELBA action: tid %d, initiator %d reason %d",
1900 			    args[0], args[1], args[2]);
1901 			break;
1902 		default:
1903 			goto badaction;
1904 		}
1905 		break;
1906 	case IEEE80211_ACTION_CAT_HT:
1907 		switch (action) {
1908 		case IEEE80211_ACTION_HT_TXCHWIDTH:
1909 			IEEE80211_NOTE(vap,
1910 			    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1911 			    ni, "send HT txchwidth: width %d",
1912 			    IEEE80211_IS_CHAN_HT40(ni->ni_chan) ? 40 : 20
1913 			);
1914 			*frm++ = IEEE80211_IS_CHAN_HT40(ni->ni_chan) ?
1915 				IEEE80211_A_HT_TXCHWIDTH_2040 :
1916 				IEEE80211_A_HT_TXCHWIDTH_20;
1917 			break;
1918 		default:
1919 			goto badaction;
1920 		}
1921 		break;
1922 	default:
1923 	badaction:
1924 		IEEE80211_NOTE(vap,
1925 		    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1926 		    "%s: unsupported category %d action %d", __func__,
1927 		    category, action);
1928 		senderr(EINVAL, is_tx_unknownmgt);
1929 		/* NOTREACHED */
1930 	}
1931 	m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
1932 
1933 	return ieee80211_mgmt_output(ni, m, IEEE80211_FC0_SUBTYPE_ACTION);
1934 bad:
1935 	ieee80211_free_node(ni);
1936 	if (m != NULL)
1937 		m_freem(m);
1938 	return ret;
1939 #undef ADDSHORT
1940 #undef senderr
1941 }
1942 
1943 /*
1944  * Construct the MCS bit mask for inclusion
1945  * in an HT information element.
1946  */
1947 static void
1948 ieee80211_set_htrates(uint8_t *frm, const struct ieee80211_htrateset *rs)
1949 {
1950 	int i;
1951 
1952 	for (i = 0; i < rs->rs_nrates; i++) {
1953 		int r = rs->rs_rates[i] & IEEE80211_RATE_VAL;
1954 		if (r < IEEE80211_HTRATE_MAXSIZE) {	/* XXX? */
1955 			/* NB: this assumes a particular implementation */
1956 			setbit(frm, r);
1957 		}
1958 	}
1959 }
1960 
1961 /*
1962  * Add body of an HTCAP information element.
1963  */
1964 static uint8_t *
1965 ieee80211_add_htcap_body(uint8_t *frm, struct ieee80211_node *ni)
1966 {
1967 #define	ADDSHORT(frm, v) do {			\
1968 	frm[0] = (v) & 0xff;			\
1969 	frm[1] = (v) >> 8;			\
1970 	frm += 2;				\
1971 } while (0)
1972 	struct ieee80211vap *vap = ni->ni_vap;
1973 	uint16_t caps;
1974 	int rxmax, density;
1975 
1976 	/* HT capabilities */
1977 	caps = vap->iv_htcaps & 0xffff;
1978 	/*
1979 	 * Note channel width depends on whether we are operating as
1980 	 * a sta or not.  When operating as a sta we are generating
1981 	 * a request based on our desired configuration.  Otherwise
1982 	 * we are operational and the channel attributes identify
1983 	 * how we've been setup (which might be different if a fixed
1984 	 * channel is specified).
1985 	 */
1986 	if (vap->iv_opmode == IEEE80211_M_STA) {
1987 		/* override 20/40 use based on config */
1988 		if (vap->iv_flags_ext & IEEE80211_FEXT_USEHT40)
1989 			caps |= IEEE80211_HTCAP_CHWIDTH40;
1990 		else
1991 			caps &= ~IEEE80211_HTCAP_CHWIDTH40;
1992 		/* use advertised setting (XXX locally constraint) */
1993 		rxmax = MS(ni->ni_htparam, IEEE80211_HTCAP_MAXRXAMPDU);
1994 		density = MS(ni->ni_htparam, IEEE80211_HTCAP_MPDUDENSITY);
1995 	} else {
1996 		/* override 20/40 use based on current channel */
1997 		if (IEEE80211_IS_CHAN_HT40(ni->ni_chan))
1998 			caps |= IEEE80211_HTCAP_CHWIDTH40;
1999 		else
2000 			caps &= ~IEEE80211_HTCAP_CHWIDTH40;
2001 		rxmax = vap->iv_ampdu_rxmax;
2002 		density = vap->iv_ampdu_density;
2003 	}
2004 	/* adjust short GI based on channel and config */
2005 	if ((vap->iv_flags_ext & IEEE80211_FEXT_SHORTGI20) == 0)
2006 		caps &= ~IEEE80211_HTCAP_SHORTGI20;
2007 	if ((vap->iv_flags_ext & IEEE80211_FEXT_SHORTGI40) == 0 ||
2008 	    (caps & IEEE80211_HTCAP_CHWIDTH40) == 0)
2009 		caps &= ~IEEE80211_HTCAP_SHORTGI40;
2010 	ADDSHORT(frm, caps);
2011 
2012 	/* HT parameters */
2013 	*frm = SM(rxmax, IEEE80211_HTCAP_MAXRXAMPDU)
2014 	     | SM(density, IEEE80211_HTCAP_MPDUDENSITY)
2015 	     ;
2016 	frm++;
2017 
2018 	/* pre-zero remainder of ie */
2019 	memset(frm, 0, sizeof(struct ieee80211_ie_htcap) -
2020 		__offsetof(struct ieee80211_ie_htcap, hc_mcsset));
2021 
2022 	/* supported MCS set */
2023 	/*
2024 	 * XXX it would better to get the rate set from ni_htrates
2025 	 * so we can restrict it but for sta mode ni_htrates isn't
2026 	 * setup when we're called to form an AssocReq frame so for
2027 	 * now we're restricted to the default HT rate set.
2028 	 */
2029 	ieee80211_set_htrates(frm, &ieee80211_rateset_11n);
2030 
2031 	frm += sizeof(struct ieee80211_ie_htcap) -
2032 		__offsetof(struct ieee80211_ie_htcap, hc_mcsset);
2033 	return frm;
2034 #undef ADDSHORT
2035 }
2036 
2037 /*
2038  * Add 802.11n HT capabilities information element
2039  */
2040 uint8_t *
2041 ieee80211_add_htcap(uint8_t *frm, struct ieee80211_node *ni)
2042 {
2043 	frm[0] = IEEE80211_ELEMID_HTCAP;
2044 	frm[1] = sizeof(struct ieee80211_ie_htcap) - 2;
2045 	return ieee80211_add_htcap_body(frm + 2, ni);
2046 }
2047 
2048 /*
2049  * Add Broadcom OUI wrapped standard HTCAP ie; this is
2050  * used for compatibility w/ pre-draft implementations.
2051  */
2052 uint8_t *
2053 ieee80211_add_htcap_vendor(uint8_t *frm, struct ieee80211_node *ni)
2054 {
2055 	frm[0] = IEEE80211_ELEMID_VENDOR;
2056 	frm[1] = 4 + sizeof(struct ieee80211_ie_htcap) - 2;
2057 	frm[2] = (BCM_OUI >> 0) & 0xff;
2058 	frm[3] = (BCM_OUI >> 8) & 0xff;
2059 	frm[4] = (BCM_OUI >> 16) & 0xff;
2060 	frm[5] = BCM_OUI_HTCAP;
2061 	return ieee80211_add_htcap_body(frm + 6, ni);
2062 }
2063 
2064 /*
2065  * Construct the MCS bit mask of basic rates
2066  * for inclusion in an HT information element.
2067  */
2068 static void
2069 ieee80211_set_basic_htrates(uint8_t *frm, const struct ieee80211_htrateset *rs)
2070 {
2071 	int i;
2072 
2073 	for (i = 0; i < rs->rs_nrates; i++) {
2074 		int r = rs->rs_rates[i] & IEEE80211_RATE_VAL;
2075 		if ((rs->rs_rates[i] & IEEE80211_RATE_BASIC) &&
2076 		    r < IEEE80211_HTRATE_MAXSIZE) {
2077 			/* NB: this assumes a particular implementation */
2078 			setbit(frm, r);
2079 		}
2080 	}
2081 }
2082 
2083 /*
2084  * Update the HTINFO ie for a beacon frame.
2085  */
2086 void
2087 ieee80211_ht_update_beacon(struct ieee80211vap *vap,
2088 	struct ieee80211_beacon_offsets *bo)
2089 {
2090 #define	PROTMODE	(IEEE80211_HTINFO_OPMODE|IEEE80211_HTINFO_NONHT_PRESENT)
2091 	const struct ieee80211_channel *bsschan = vap->iv_bss->ni_chan;
2092 	struct ieee80211com *ic = vap->iv_ic;
2093 	struct ieee80211_ie_htinfo *ht =
2094 	   (struct ieee80211_ie_htinfo *) bo->bo_htinfo;
2095 
2096 	/* XXX only update on channel change */
2097 	ht->hi_ctrlchannel = ieee80211_chan2ieee(ic, bsschan);
2098 	ht->hi_byte1 = IEEE80211_HTINFO_RIFSMODE_PROH;
2099 	if (IEEE80211_IS_CHAN_HT40U(bsschan))
2100 		ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_ABOVE;
2101 	else if (IEEE80211_IS_CHAN_HT40D(bsschan))
2102 		ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_BELOW;
2103 	else
2104 		ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_NONE;
2105 	if (IEEE80211_IS_CHAN_HT40(bsschan))
2106 		ht->hi_byte1 |= IEEE80211_HTINFO_TXWIDTH_2040;
2107 
2108 	/* protection mode */
2109 	ht->hi_byte2 = (ht->hi_byte2 &~ PROTMODE) | ic->ic_curhtprotmode;
2110 
2111 	/* XXX propagate to vendor ie's */
2112 #undef PROTMODE
2113 }
2114 
2115 /*
2116  * Add body of an HTINFO information element.
2117  *
2118  * NB: We don't use struct ieee80211_ie_htinfo because we can
2119  * be called to fillin both a standard ie and a compat ie that
2120  * has a vendor OUI at the front.
2121  */
2122 static uint8_t *
2123 ieee80211_add_htinfo_body(uint8_t *frm, struct ieee80211_node *ni)
2124 {
2125 	struct ieee80211com *ic = ni->ni_ic;
2126 
2127 	/* pre-zero remainder of ie */
2128 	memset(frm, 0, sizeof(struct ieee80211_ie_htinfo) - 2);
2129 
2130 	/* primary/control channel center */
2131 	*frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
2132 
2133 	frm[0] = IEEE80211_HTINFO_RIFSMODE_PROH;
2134 	if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan))
2135 		frm[0] |= IEEE80211_HTINFO_2NDCHAN_ABOVE;
2136 	else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan))
2137 		frm[0] |= IEEE80211_HTINFO_2NDCHAN_BELOW;
2138 	else
2139 		frm[0] |= IEEE80211_HTINFO_2NDCHAN_NONE;
2140 	if (IEEE80211_IS_CHAN_HT40(ni->ni_chan))
2141 		frm[0] |= IEEE80211_HTINFO_TXWIDTH_2040;
2142 
2143 	frm[1] = ic->ic_curhtprotmode;
2144 
2145 	frm += 5;
2146 
2147 	/* basic MCS set */
2148 	ieee80211_set_basic_htrates(frm, &ni->ni_htrates);
2149 	frm += sizeof(struct ieee80211_ie_htinfo) -
2150 		__offsetof(struct ieee80211_ie_htinfo, hi_basicmcsset);
2151 	return frm;
2152 }
2153 
2154 /*
2155  * Add 802.11n HT information information element.
2156  */
2157 uint8_t *
2158 ieee80211_add_htinfo(uint8_t *frm, struct ieee80211_node *ni)
2159 {
2160 	frm[0] = IEEE80211_ELEMID_HTINFO;
2161 	frm[1] = sizeof(struct ieee80211_ie_htinfo) - 2;
2162 	return ieee80211_add_htinfo_body(frm + 2, ni);
2163 }
2164 
2165 /*
2166  * Add Broadcom OUI wrapped standard HTINFO ie; this is
2167  * used for compatibility w/ pre-draft implementations.
2168  */
2169 uint8_t *
2170 ieee80211_add_htinfo_vendor(uint8_t *frm, struct ieee80211_node *ni)
2171 {
2172 	frm[0] = IEEE80211_ELEMID_VENDOR;
2173 	frm[1] = 4 + sizeof(struct ieee80211_ie_htinfo) - 2;
2174 	frm[2] = (BCM_OUI >> 0) & 0xff;
2175 	frm[3] = (BCM_OUI >> 8) & 0xff;
2176 	frm[4] = (BCM_OUI >> 16) & 0xff;
2177 	frm[5] = BCM_OUI_HTINFO;
2178 	return ieee80211_add_htinfo_body(frm + 6, ni);
2179 }
2180