2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2007-2008 Sam Leffler, Errno Consulting
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
34 * IEEE 802.11n protocol support.
40 #include <sys/param.h>
41 #include <sys/kernel.h>
42 #include <sys/malloc.h>
43 #include <sys/systm.h>
44 #include <sys/endian.h>
46 #include <sys/socket.h>
49 #include <net/if_var.h>
50 #include <net/if_media.h>
51 #include <net/ethernet.h>
53 #include <net80211/ieee80211_var.h>
54 #include <net80211/ieee80211_action.h>
55 #include <net80211/ieee80211_input.h>
57 /* define here, used throughout file */
58 #define MS(_v, _f) (((_v) & _f) >> _f##_S)
59 #define SM(_v, _f) (((_v) << _f##_S) & _f)
61 const struct ieee80211_mcs_rates ieee80211_htrates[IEEE80211_HTRATE_MAXSIZE] = {
62 { 13, 14, 27, 30 }, /* MCS 0 */
63 { 26, 29, 54, 60 }, /* MCS 1 */
64 { 39, 43, 81, 90 }, /* MCS 2 */
65 { 52, 58, 108, 120 }, /* MCS 3 */
66 { 78, 87, 162, 180 }, /* MCS 4 */
67 { 104, 116, 216, 240 }, /* MCS 5 */
68 { 117, 130, 243, 270 }, /* MCS 6 */
69 { 130, 144, 270, 300 }, /* MCS 7 */
70 { 26, 29, 54, 60 }, /* MCS 8 */
71 { 52, 58, 108, 120 }, /* MCS 9 */
72 { 78, 87, 162, 180 }, /* MCS 10 */
73 { 104, 116, 216, 240 }, /* MCS 11 */
74 { 156, 173, 324, 360 }, /* MCS 12 */
75 { 208, 231, 432, 480 }, /* MCS 13 */
76 { 234, 260, 486, 540 }, /* MCS 14 */
77 { 260, 289, 540, 600 }, /* MCS 15 */
78 { 39, 43, 81, 90 }, /* MCS 16 */
79 { 78, 87, 162, 180 }, /* MCS 17 */
80 { 117, 130, 243, 270 }, /* MCS 18 */
81 { 156, 173, 324, 360 }, /* MCS 19 */
82 { 234, 260, 486, 540 }, /* MCS 20 */
83 { 312, 347, 648, 720 }, /* MCS 21 */
84 { 351, 390, 729, 810 }, /* MCS 22 */
85 { 390, 433, 810, 900 }, /* MCS 23 */
86 { 52, 58, 108, 120 }, /* MCS 24 */
87 { 104, 116, 216, 240 }, /* MCS 25 */
88 { 156, 173, 324, 360 }, /* MCS 26 */
89 { 208, 231, 432, 480 }, /* MCS 27 */
90 { 312, 347, 648, 720 }, /* MCS 28 */
91 { 416, 462, 864, 960 }, /* MCS 29 */
92 { 468, 520, 972, 1080 }, /* MCS 30 */
93 { 520, 578, 1080, 1200 }, /* MCS 31 */
94 { 0, 0, 12, 13 }, /* MCS 32 */
95 { 78, 87, 162, 180 }, /* MCS 33 */
96 { 104, 116, 216, 240 }, /* MCS 34 */
97 { 130, 144, 270, 300 }, /* MCS 35 */
98 { 117, 130, 243, 270 }, /* MCS 36 */
99 { 156, 173, 324, 360 }, /* MCS 37 */
100 { 195, 217, 405, 450 }, /* MCS 38 */
101 { 104, 116, 216, 240 }, /* MCS 39 */
102 { 130, 144, 270, 300 }, /* MCS 40 */
103 { 130, 144, 270, 300 }, /* MCS 41 */
104 { 156, 173, 324, 360 }, /* MCS 42 */
105 { 182, 202, 378, 420 }, /* MCS 43 */
106 { 182, 202, 378, 420 }, /* MCS 44 */
107 { 208, 231, 432, 480 }, /* MCS 45 */
108 { 156, 173, 324, 360 }, /* MCS 46 */
109 { 195, 217, 405, 450 }, /* MCS 47 */
110 { 195, 217, 405, 450 }, /* MCS 48 */
111 { 234, 260, 486, 540 }, /* MCS 49 */
112 { 273, 303, 567, 630 }, /* MCS 50 */
113 { 273, 303, 567, 630 }, /* MCS 51 */
114 { 312, 347, 648, 720 }, /* MCS 52 */
115 { 130, 144, 270, 300 }, /* MCS 53 */
116 { 156, 173, 324, 360 }, /* MCS 54 */
117 { 182, 202, 378, 420 }, /* MCS 55 */
118 { 156, 173, 324, 360 }, /* MCS 56 */
119 { 182, 202, 378, 420 }, /* MCS 57 */
120 { 208, 231, 432, 480 }, /* MCS 58 */
121 { 234, 260, 486, 540 }, /* MCS 59 */
122 { 208, 231, 432, 480 }, /* MCS 60 */
123 { 234, 260, 486, 540 }, /* MCS 61 */
124 { 260, 289, 540, 600 }, /* MCS 62 */
125 { 260, 289, 540, 600 }, /* MCS 63 */
126 { 286, 318, 594, 660 }, /* MCS 64 */
127 { 195, 217, 405, 450 }, /* MCS 65 */
128 { 234, 260, 486, 540 }, /* MCS 66 */
129 { 273, 303, 567, 630 }, /* MCS 67 */
130 { 234, 260, 486, 540 }, /* MCS 68 */
131 { 273, 303, 567, 630 }, /* MCS 69 */
132 { 312, 347, 648, 720 }, /* MCS 70 */
133 { 351, 390, 729, 810 }, /* MCS 71 */
134 { 312, 347, 648, 720 }, /* MCS 72 */
135 { 351, 390, 729, 810 }, /* MCS 73 */
136 { 390, 433, 810, 900 }, /* MCS 74 */
137 { 390, 433, 810, 900 }, /* MCS 75 */
138 { 429, 477, 891, 990 }, /* MCS 76 */
141 static int ieee80211_ampdu_age = -1; /* threshold for ampdu reorder q (ms) */
142 SYSCTL_PROC(_net_wlan, OID_AUTO, ampdu_age, CTLTYPE_INT | CTLFLAG_RW,
143 &ieee80211_ampdu_age, 0, ieee80211_sysctl_msecs_ticks, "I",
144 "AMPDU max reorder age (ms)");
146 static int ieee80211_recv_bar_ena = 1;
147 SYSCTL_INT(_net_wlan, OID_AUTO, recv_bar, CTLFLAG_RW, &ieee80211_recv_bar_ena,
148 0, "BAR frame processing (ena/dis)");
150 static int ieee80211_addba_timeout = -1;/* timeout for ADDBA response */
151 SYSCTL_PROC(_net_wlan, OID_AUTO, addba_timeout, CTLTYPE_INT | CTLFLAG_RW,
152 &ieee80211_addba_timeout, 0, ieee80211_sysctl_msecs_ticks, "I",
153 "ADDBA request timeout (ms)");
154 static int ieee80211_addba_backoff = -1;/* backoff after max ADDBA requests */
155 SYSCTL_PROC(_net_wlan, OID_AUTO, addba_backoff, CTLTYPE_INT | CTLFLAG_RW,
156 &ieee80211_addba_backoff, 0, ieee80211_sysctl_msecs_ticks, "I",
157 "ADDBA request backoff (ms)");
158 static int ieee80211_addba_maxtries = 3;/* max ADDBA requests before backoff */
159 SYSCTL_INT(_net_wlan, OID_AUTO, addba_maxtries, CTLFLAG_RW,
160 &ieee80211_addba_maxtries, 0, "max ADDBA requests sent before backoff");
162 static int ieee80211_bar_timeout = -1; /* timeout waiting for BAR response */
163 static int ieee80211_bar_maxtries = 50;/* max BAR requests before DELBA */
165 static ieee80211_recv_action_func ht_recv_action_ba_addba_request;
166 static ieee80211_recv_action_func ht_recv_action_ba_addba_response;
167 static ieee80211_recv_action_func ht_recv_action_ba_delba;
168 static ieee80211_recv_action_func ht_recv_action_ht_mimopwrsave;
169 static ieee80211_recv_action_func ht_recv_action_ht_txchwidth;
171 static ieee80211_send_action_func ht_send_action_ba_addba;
172 static ieee80211_send_action_func ht_send_action_ba_delba;
173 static ieee80211_send_action_func ht_send_action_ht_txchwidth;
176 ieee80211_ht_init(void)
179 * Setup HT parameters that depends on the clock frequency.
181 ieee80211_ampdu_age = msecs_to_ticks(500);
182 ieee80211_addba_timeout = msecs_to_ticks(250);
183 ieee80211_addba_backoff = msecs_to_ticks(10*1000);
184 ieee80211_bar_timeout = msecs_to_ticks(250);
186 * Register action frame handlers.
188 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_BA,
189 IEEE80211_ACTION_BA_ADDBA_REQUEST, ht_recv_action_ba_addba_request);
190 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_BA,
191 IEEE80211_ACTION_BA_ADDBA_RESPONSE, ht_recv_action_ba_addba_response);
192 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_BA,
193 IEEE80211_ACTION_BA_DELBA, ht_recv_action_ba_delba);
194 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_HT,
195 IEEE80211_ACTION_HT_MIMOPWRSAVE, ht_recv_action_ht_mimopwrsave);
196 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_HT,
197 IEEE80211_ACTION_HT_TXCHWIDTH, ht_recv_action_ht_txchwidth);
199 ieee80211_send_action_register(IEEE80211_ACTION_CAT_BA,
200 IEEE80211_ACTION_BA_ADDBA_REQUEST, ht_send_action_ba_addba);
201 ieee80211_send_action_register(IEEE80211_ACTION_CAT_BA,
202 IEEE80211_ACTION_BA_ADDBA_RESPONSE, ht_send_action_ba_addba);
203 ieee80211_send_action_register(IEEE80211_ACTION_CAT_BA,
204 IEEE80211_ACTION_BA_DELBA, ht_send_action_ba_delba);
205 ieee80211_send_action_register(IEEE80211_ACTION_CAT_HT,
206 IEEE80211_ACTION_HT_TXCHWIDTH, ht_send_action_ht_txchwidth);
208 SYSINIT(wlan_ht, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_ht_init, NULL);
210 static int ieee80211_ampdu_enable(struct ieee80211_node *ni,
211 struct ieee80211_tx_ampdu *tap);
212 static int ieee80211_addba_request(struct ieee80211_node *ni,
213 struct ieee80211_tx_ampdu *tap,
214 int dialogtoken, int baparamset, int batimeout);
215 static int ieee80211_addba_response(struct ieee80211_node *ni,
216 struct ieee80211_tx_ampdu *tap,
217 int code, int baparamset, int batimeout);
218 static void ieee80211_addba_stop(struct ieee80211_node *ni,
219 struct ieee80211_tx_ampdu *tap);
220 static void null_addba_response_timeout(struct ieee80211_node *ni,
221 struct ieee80211_tx_ampdu *tap);
223 static void ieee80211_bar_response(struct ieee80211_node *ni,
224 struct ieee80211_tx_ampdu *tap, int status);
225 static void ampdu_tx_stop(struct ieee80211_tx_ampdu *tap);
226 static void bar_stop_timer(struct ieee80211_tx_ampdu *tap);
227 static int ampdu_rx_start(struct ieee80211_node *, struct ieee80211_rx_ampdu *,
228 int baparamset, int batimeout, int baseqctl);
229 static void ampdu_rx_stop(struct ieee80211_node *, struct ieee80211_rx_ampdu *);
232 ieee80211_ht_attach(struct ieee80211com *ic)
234 /* setup default aggregation policy */
235 ic->ic_recv_action = ieee80211_recv_action;
236 ic->ic_send_action = ieee80211_send_action;
237 ic->ic_ampdu_enable = ieee80211_ampdu_enable;
238 ic->ic_addba_request = ieee80211_addba_request;
239 ic->ic_addba_response = ieee80211_addba_response;
240 ic->ic_addba_response_timeout = null_addba_response_timeout;
241 ic->ic_addba_stop = ieee80211_addba_stop;
242 ic->ic_bar_response = ieee80211_bar_response;
243 ic->ic_ampdu_rx_start = ampdu_rx_start;
244 ic->ic_ampdu_rx_stop = ampdu_rx_stop;
246 ic->ic_htprotmode = IEEE80211_PROT_RTSCTS;
247 ic->ic_curhtprotmode = IEEE80211_HTINFO_OPMODE_PURE;
251 ieee80211_ht_detach(struct ieee80211com *ic)
256 ieee80211_ht_vattach(struct ieee80211vap *vap)
259 /* driver can override defaults */
260 vap->iv_ampdu_rxmax = IEEE80211_HTCAP_MAXRXAMPDU_8K;
261 vap->iv_ampdu_density = IEEE80211_HTCAP_MPDUDENSITY_NA;
262 vap->iv_ampdu_limit = vap->iv_ampdu_rxmax;
263 vap->iv_amsdu_limit = vap->iv_htcaps & IEEE80211_HTCAP_MAXAMSDU;
264 /* tx aggregation traffic thresholds */
265 vap->iv_ampdu_mintraffic[WME_AC_BK] = 128;
266 vap->iv_ampdu_mintraffic[WME_AC_BE] = 64;
267 vap->iv_ampdu_mintraffic[WME_AC_VO] = 32;
268 vap->iv_ampdu_mintraffic[WME_AC_VI] = 32;
270 if (vap->iv_htcaps & IEEE80211_HTC_HT) {
272 * Device is HT capable; enable all HT-related
273 * facilities by default.
274 * XXX these choices may be too aggressive.
276 vap->iv_flags_ht |= IEEE80211_FHT_HT
277 | IEEE80211_FHT_HTCOMPAT
279 if (vap->iv_htcaps & IEEE80211_HTCAP_SHORTGI20)
280 vap->iv_flags_ht |= IEEE80211_FHT_SHORTGI20;
281 /* XXX infer from channel list? */
282 if (vap->iv_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
283 vap->iv_flags_ht |= IEEE80211_FHT_USEHT40;
284 if (vap->iv_htcaps & IEEE80211_HTCAP_SHORTGI40)
285 vap->iv_flags_ht |= IEEE80211_FHT_SHORTGI40;
287 /* enable RIFS if capable */
288 if (vap->iv_htcaps & IEEE80211_HTC_RIFS)
289 vap->iv_flags_ht |= IEEE80211_FHT_RIFS;
291 /* NB: A-MPDU and A-MSDU rx are mandated, these are tx only */
292 vap->iv_flags_ht |= IEEE80211_FHT_AMPDU_RX;
293 if (vap->iv_htcaps & IEEE80211_HTC_AMPDU)
294 vap->iv_flags_ht |= IEEE80211_FHT_AMPDU_TX;
295 vap->iv_flags_ht |= IEEE80211_FHT_AMSDU_RX;
296 if (vap->iv_htcaps & IEEE80211_HTC_AMSDU)
297 vap->iv_flags_ht |= IEEE80211_FHT_AMSDU_TX;
299 if (vap->iv_htcaps & IEEE80211_HTCAP_TXSTBC)
300 vap->iv_flags_ht |= IEEE80211_FHT_STBC_TX;
301 if (vap->iv_htcaps & IEEE80211_HTCAP_RXSTBC)
302 vap->iv_flags_ht |= IEEE80211_FHT_STBC_RX;
304 if (vap->iv_htcaps & IEEE80211_HTCAP_LDPC)
305 vap->iv_flags_ht |= IEEE80211_FHT_LDPC_RX;
306 if (vap->iv_htcaps & IEEE80211_HTC_TXLDPC)
307 vap->iv_flags_ht |= IEEE80211_FHT_LDPC_TX;
309 /* NB: disable default legacy WDS, too many issues right now */
310 if (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY)
311 vap->iv_flags_ht &= ~IEEE80211_FHT_HT;
315 ieee80211_ht_vdetach(struct ieee80211vap *vap)
320 ht_getrate(struct ieee80211com *ic, int index, enum ieee80211_phymode mode,
325 mword = ieee80211_rate2media(ic, index | IEEE80211_RATE_MCS, mode);
326 if (IFM_SUBTYPE(mword) != IFM_IEEE80211_MCS)
330 rate = ieee80211_htrates[index].ht20_rate_800ns;
333 rate = ieee80211_htrates[index].ht20_rate_400ns;
336 rate = ieee80211_htrates[index].ht40_rate_800ns;
339 rate = ieee80211_htrates[index].ht40_rate_400ns;
345 static struct printranges {
356 { 32, 0, 1, 2, IEEE80211_HTC_TXMCS32 },
357 { 33, 38, 2, 0, IEEE80211_HTC_TXUNEQUAL },
358 { 39, 52, 3, 0, IEEE80211_HTC_TXUNEQUAL },
359 { 53, 76, 4, 0, IEEE80211_HTC_TXUNEQUAL },
364 ht_rateprint(struct ieee80211com *ic, enum ieee80211_phymode mode, int ratetype)
366 int minrate, maxrate;
367 struct printranges *range;
369 for (range = ranges; range->txstream != 0; range++) {
370 if (ic->ic_txstream < range->txstream)
372 if (range->htcapflags &&
373 (ic->ic_htcaps & range->htcapflags) == 0)
375 if (ratetype < range->ratetype)
377 minrate = ht_getrate(ic, range->minmcs, mode, ratetype);
378 maxrate = ht_getrate(ic, range->maxmcs, mode, ratetype);
380 ic_printf(ic, "MCS %d-%d: %d%sMbps - %d%sMbps\n",
381 range->minmcs, range->maxmcs,
382 minrate/2, ((minrate & 0x1) != 0 ? ".5" : ""),
383 maxrate/2, ((maxrate & 0x1) != 0 ? ".5" : ""));
385 ic_printf(ic, "MCS %d: %d%sMbps\n", range->minmcs,
386 minrate/2, ((minrate & 0x1) != 0 ? ".5" : ""));
392 ht_announce(struct ieee80211com *ic, enum ieee80211_phymode mode)
394 const char *modestr = ieee80211_phymode_name[mode];
396 ic_printf(ic, "%s MCS 20MHz\n", modestr);
397 ht_rateprint(ic, mode, 0);
398 if (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20) {
399 ic_printf(ic, "%s MCS 20MHz SGI\n", modestr);
400 ht_rateprint(ic, mode, 1);
402 if (ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
403 ic_printf(ic, "%s MCS 40MHz:\n", modestr);
404 ht_rateprint(ic, mode, 2);
406 if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
407 (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40)) {
408 ic_printf(ic, "%s MCS 40MHz SGI:\n", modestr);
409 ht_rateprint(ic, mode, 3);
414 ieee80211_ht_announce(struct ieee80211com *ic)
417 if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) ||
418 isset(ic->ic_modecaps, IEEE80211_MODE_11NG))
419 ic_printf(ic, "%dT%dR\n", ic->ic_txstream, ic->ic_rxstream);
420 if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA))
421 ht_announce(ic, IEEE80211_MODE_11NA);
422 if (isset(ic->ic_modecaps, IEEE80211_MODE_11NG))
423 ht_announce(ic, IEEE80211_MODE_11NG);
427 ieee80211_init_suphtrates(struct ieee80211com *ic)
429 #define ADDRATE(x) do { \
430 htrateset->rs_rates[htrateset->rs_nrates] = x; \
431 htrateset->rs_nrates++; \
433 struct ieee80211_htrateset *htrateset = &ic->ic_sup_htrates;
436 memset(htrateset, 0, sizeof(struct ieee80211_htrateset));
437 for (i = 0; i < ic->ic_txstream * 8; i++)
439 if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
440 (ic->ic_htcaps & IEEE80211_HTC_TXMCS32))
442 if (ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL) {
443 if (ic->ic_txstream >= 2) {
444 for (i = 33; i <= 38; i++)
447 if (ic->ic_txstream >= 3) {
448 for (i = 39; i <= 52; i++)
451 if (ic->ic_txstream == 4) {
452 for (i = 53; i <= 76; i++)
460 * Receive processing.
464 * Decap the encapsulated A-MSDU frames and dispatch all but
465 * the last for delivery. The last frame is returned for
466 * delivery via the normal path.
469 ieee80211_decap_amsdu(struct ieee80211_node *ni, struct mbuf *m)
471 struct ieee80211vap *vap = ni->ni_vap;
475 /* discard 802.3 header inserted by ieee80211_decap */
476 m_adj(m, sizeof(struct ether_header));
478 vap->iv_stats.is_amsdu_decap++;
482 * Decap the first frame, bust it apart from the
483 * remainder and deliver. We leave the last frame
484 * delivery to the caller (for consistency with other
485 * code paths, could also do it here).
487 m = ieee80211_decap1(m, &framelen);
489 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
490 ni->ni_macaddr, "a-msdu", "%s", "decap failed");
491 vap->iv_stats.is_amsdu_tooshort++;
494 if (m->m_pkthdr.len == framelen)
496 n = m_split(m, framelen, M_NOWAIT);
498 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
499 ni->ni_macaddr, "a-msdu",
500 "%s", "unable to split encapsulated frames");
501 vap->iv_stats.is_amsdu_split++;
502 m_freem(m); /* NB: must reclaim */
505 vap->iv_deliver_data(vap, ni, m);
508 * Remove frame contents; each intermediate frame
509 * is required to be aligned to a 4-byte boundary.
512 m_adj(m, roundup2(framelen, 4) - framelen); /* padding */
514 return m; /* last delivered by caller */
518 * Add the given frame to the current RX reorder slot.
520 * For future offloaded A-MSDU handling where multiple frames with
521 * the same sequence number show up here, this routine will append
522 * those frames as long as they're appropriately tagged.
525 ampdu_rx_add_slot(struct ieee80211_rx_ampdu *rap, int off, int tid,
527 struct ieee80211_node *ni,
530 struct ieee80211vap *vap = ni->ni_vap;
532 if (rap->rxa_m[off] == NULL) {
535 rap->rxa_qbytes += m->m_pkthdr.len;
536 vap->iv_stats.is_ampdu_rx_reorder++;
539 IEEE80211_DISCARD_MAC(vap,
540 IEEE80211_MSG_INPUT | IEEE80211_MSG_11N,
541 ni->ni_macaddr, "a-mpdu duplicate",
542 "seqno %u tid %u BA win <%u:%u>",
543 rxseq, tid, rap->rxa_start,
544 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1));
545 vap->iv_stats.is_rx_dup++;
546 IEEE80211_NODE_STAT(ni, rx_dup);
553 ampdu_rx_purge_slot(struct ieee80211_rx_ampdu *rap, int i)
561 rap->rxa_m[i] = NULL;
562 rap->rxa_qbytes -= m->m_pkthdr.len;
568 * Purge all frames in the A-MPDU re-order queue.
571 ampdu_rx_purge(struct ieee80211_rx_ampdu *rap)
575 for (i = 0; i < rap->rxa_wnd; i++) {
576 ampdu_rx_purge_slot(rap, i);
577 if (rap->rxa_qframes == 0)
580 KASSERT(rap->rxa_qbytes == 0 && rap->rxa_qframes == 0,
581 ("lost %u data, %u frames on ampdu rx q",
582 rap->rxa_qbytes, rap->rxa_qframes));
586 * Start A-MPDU rx/re-order processing for the specified TID.
589 ampdu_rx_start(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap,
590 int baparamset, int batimeout, int baseqctl)
592 int bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
594 if (rap->rxa_flags & IEEE80211_AGGR_RUNNING) {
596 * AMPDU previously setup and not terminated with a DELBA,
597 * flush the reorder q's in case anything remains.
601 memset(rap, 0, sizeof(*rap));
602 rap->rxa_wnd = (bufsiz == 0) ?
603 IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
604 rap->rxa_start = MS(baseqctl, IEEE80211_BASEQ_START);
605 rap->rxa_flags |= IEEE80211_AGGR_RUNNING | IEEE80211_AGGR_XCHGPEND;
611 * Public function; manually setup the RX ampdu state.
614 ieee80211_ampdu_rx_start_ext(struct ieee80211_node *ni, int tid, int seq, int baw)
616 struct ieee80211_rx_ampdu *rap;
618 /* XXX TODO: sanity check tid, seq, baw */
620 rap = &ni->ni_rx_ampdu[tid];
622 if (rap->rxa_flags & IEEE80211_AGGR_RUNNING) {
624 * AMPDU previously setup and not terminated with a DELBA,
625 * flush the reorder q's in case anything remains.
630 memset(rap, 0, sizeof(*rap));
631 rap->rxa_wnd = (baw== 0) ?
632 IEEE80211_AGGR_BAWMAX : min(baw, IEEE80211_AGGR_BAWMAX);
634 /* Wait for the first RX frame, use that as BAW */
636 rap->rxa_flags |= IEEE80211_AGGR_WAITRX;
638 rap->rxa_start = seq;
640 rap->rxa_flags |= IEEE80211_AGGR_RUNNING | IEEE80211_AGGR_XCHGPEND;
642 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, ni,
643 "%s: tid=%d, start=%d, wnd=%d, flags=0x%08x",
654 * Public function; manually stop the RX AMPDU state.
657 ieee80211_ampdu_rx_stop_ext(struct ieee80211_node *ni, int tid)
659 struct ieee80211_rx_ampdu *rap;
661 /* XXX TODO: sanity check tid, seq, baw */
662 rap = &ni->ni_rx_ampdu[tid];
663 ampdu_rx_stop(ni, rap);
667 * Stop A-MPDU rx processing for the specified TID.
670 ampdu_rx_stop(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
674 rap->rxa_flags &= ~(IEEE80211_AGGR_RUNNING
675 | IEEE80211_AGGR_XCHGPEND
676 | IEEE80211_AGGR_WAITRX);
680 * Dispatch a frame from the A-MPDU reorder queue. The
681 * frame is fed back into ieee80211_input marked with an
682 * M_AMPDU_MPDU flag so it doesn't come back to us (it also
683 * permits ieee80211_input to optimize re-processing).
686 ampdu_dispatch(struct ieee80211_node *ni, struct mbuf *m)
688 m->m_flags |= M_AMPDU_MPDU; /* bypass normal processing */
689 /* NB: rssi and noise are ignored w/ M_AMPDU_MPDU set */
690 (void) ieee80211_input(ni, m, 0, 0);
694 ampdu_dispatch_slot(struct ieee80211_rx_ampdu *rap, struct ieee80211_node *ni,
699 if (rap->rxa_m[i] == NULL)
703 rap->rxa_m[i] = NULL;
704 rap->rxa_qbytes -= m->m_pkthdr.len;
707 ampdu_dispatch(ni, m);
713 ampdu_rx_moveup(struct ieee80211_rx_ampdu *rap, struct ieee80211_node *ni,
716 struct ieee80211vap *vap = ni->ni_vap;
718 if (rap->rxa_qframes != 0) {
719 int n = rap->rxa_qframes, j;
721 if (winstart != -1) {
723 * NB: in window-sliding mode, loop assumes i > 0
724 * and/or rxa_m[0] is NULL
726 KASSERT(rap->rxa_m[0] == NULL,
727 ("%s: BA window slot 0 occupied", __func__));
729 for (j = i+1; j < rap->rxa_wnd; j++) {
730 if (rap->rxa_m[j] != NULL) {
731 rap->rxa_m[j-i] = rap->rxa_m[j];
732 rap->rxa_m[j] = NULL;
737 KASSERT(n == 0, ("%s: lost %d frames, qframes %d off %d "
738 "BA win <%d:%d> winstart %d",
739 __func__, n, rap->rxa_qframes, i, rap->rxa_start,
740 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
742 vap->iv_stats.is_ampdu_rx_copy += rap->rxa_qframes;
747 * Dispatch as many frames as possible from the re-order queue.
748 * Frames will always be "at the front"; we process all frames
749 * up to the first empty slot in the window. On completion we
750 * cleanup state if there are still pending frames in the current
751 * BA window. We assume the frame at slot 0 is already handled
752 * by the caller; we always start at slot 1.
755 ampdu_rx_dispatch(struct ieee80211_rx_ampdu *rap, struct ieee80211_node *ni)
757 struct ieee80211vap *vap = ni->ni_vap;
760 /* flush run of frames */
761 for (i = 1; i < rap->rxa_wnd; i++) {
762 if (ampdu_dispatch_slot(rap, ni, i) == 0)
767 * If frames remain, copy the mbuf pointers down so
768 * they correspond to the offsets in the new window.
770 ampdu_rx_moveup(rap, ni, i, -1);
773 * Adjust the start of the BA window to
774 * reflect the frames just dispatched.
776 rap->rxa_start = IEEE80211_SEQ_ADD(rap->rxa_start, i);
777 vap->iv_stats.is_ampdu_rx_oor += i;
781 * Dispatch all frames in the A-MPDU re-order queue.
784 ampdu_rx_flush(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
786 struct ieee80211vap *vap = ni->ni_vap;
789 for (i = 0; i < rap->rxa_wnd; i++) {
790 r = ampdu_dispatch_slot(rap, ni, i);
793 vap->iv_stats.is_ampdu_rx_oor += r;
795 if (rap->rxa_qframes == 0)
801 * Dispatch all frames in the A-MPDU re-order queue
802 * preceding the specified sequence number. This logic
803 * handles window moves due to a received MSDU or BAR.
806 ampdu_rx_flush_upto(struct ieee80211_node *ni,
807 struct ieee80211_rx_ampdu *rap, ieee80211_seq winstart)
809 struct ieee80211vap *vap = ni->ni_vap;
814 * Flush any complete MSDU's with a sequence number lower
815 * than winstart. Gaps may exist. Note that we may actually
816 * dispatch frames past winstart if a run continues; this is
817 * an optimization that avoids having to do a separate pass
818 * to dispatch frames after moving the BA window start.
820 seqno = rap->rxa_start;
821 for (i = 0; i < rap->rxa_wnd; i++) {
822 r = ampdu_dispatch_slot(rap, ni, i);
824 if (!IEEE80211_SEQ_BA_BEFORE(seqno, winstart))
827 vap->iv_stats.is_ampdu_rx_oor += r;
828 seqno = IEEE80211_SEQ_INC(seqno);
831 * If frames remain, copy the mbuf pointers down so
832 * they correspond to the offsets in the new window.
834 ampdu_rx_moveup(rap, ni, i, winstart);
837 * Move the start of the BA window; we use the
838 * sequence number of the last MSDU that was
839 * passed up the stack+1 or winstart if stopped on
840 * a gap in the reorder buffer.
842 rap->rxa_start = seqno;
846 * Process a received QoS data frame for an HT station. Handle
847 * A-MPDU reordering: if this frame is received out of order
848 * and falls within the BA window hold onto it. Otherwise if
849 * this frame completes a run, flush any pending frames. We
850 * return 1 if the frame is consumed. A 0 is returned if
851 * the frame should be processed normally by the caller.
854 ieee80211_ampdu_reorder(struct ieee80211_node *ni, struct mbuf *m,
855 const struct ieee80211_rx_stats *rxs)
857 #define PROCESS 0 /* caller should process frame */
858 #define CONSUMED 1 /* frame consumed, caller does nothing */
859 struct ieee80211vap *vap = ni->ni_vap;
860 struct ieee80211_qosframe *wh;
861 struct ieee80211_rx_ampdu *rap;
866 KASSERT((m->m_flags & (M_AMPDU | M_AMPDU_MPDU)) == M_AMPDU,
867 ("!a-mpdu or already re-ordered, flags 0x%x", m->m_flags));
868 KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT sta"));
870 /* NB: m_len known to be sufficient */
871 wh = mtod(m, struct ieee80211_qosframe *);
872 if (wh->i_fc[0] != IEEE80211_FC0_QOSDATA) {
874 * Not QoS data, shouldn't get here but just
875 * return it to the caller for processing.
881 * 802.11-2012 9.3.2.10 - Duplicate detection and recovery.
883 * Multicast QoS data frames are checked against a different
884 * counter, not the per-TID counter.
886 if (IEEE80211_IS_MULTICAST(wh->i_addr1))
889 tid = ieee80211_getqos(wh)[0];
890 tid &= IEEE80211_QOS_TID;
891 rap = &ni->ni_rx_ampdu[tid];
892 if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
894 * No ADDBA request yet, don't touch.
898 rxseq = le16toh(*(uint16_t *)wh->i_seq);
899 if ((rxseq & IEEE80211_SEQ_FRAG_MASK) != 0) {
901 * Fragments are not allowed; toss.
903 IEEE80211_DISCARD_MAC(vap,
904 IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
905 "A-MPDU", "fragment, rxseq 0x%x tid %u%s", rxseq, tid,
906 wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
907 vap->iv_stats.is_ampdu_rx_drop++;
908 IEEE80211_NODE_STAT(ni, rx_drop);
912 rxseq >>= IEEE80211_SEQ_SEQ_SHIFT;
916 * Handle waiting for the first frame to define the BAW.
917 * Some firmware doesn't provide the RX of the starting point
918 * of the BAW and we have to cope.
920 if (rap->rxa_flags & IEEE80211_AGGR_WAITRX) {
921 rap->rxa_flags &= ~IEEE80211_AGGR_WAITRX;
922 rap->rxa_start = rxseq;
925 if (rxseq == rap->rxa_start) {
927 * First frame in window.
929 if (rap->rxa_qframes != 0) {
931 * Dispatch as many packets as we can.
933 KASSERT(rap->rxa_m[0] == NULL, ("unexpected dup"));
934 ampdu_dispatch(ni, m);
935 ampdu_rx_dispatch(rap, ni);
939 * In order; advance window and notify
940 * caller to dispatch directly.
942 rap->rxa_start = IEEE80211_SEQ_INC(rxseq);
947 * Frame is out of order; store if in the BA window.
949 /* calculate offset in BA window */
950 off = IEEE80211_SEQ_SUB(rxseq, rap->rxa_start);
951 if (off < rap->rxa_wnd) {
953 * Common case (hopefully): in the BA window.
954 * Sec 9.10.7.6.2 a) (p.137)
958 * Check for frames sitting too long in the reorder queue.
959 * This should only ever happen if frames are not delivered
960 * without the sender otherwise notifying us (e.g. with a
961 * BAR to move the window). Typically this happens because
962 * of vendor bugs that cause the sequence number to jump.
963 * When this happens we get a gap in the reorder queue that
964 * leaves frame sitting on the queue until they get pushed
965 * out due to window moves. When the vendor does not send
966 * BAR this move only happens due to explicit packet sends
968 * NB: we only track the time of the oldest frame in the
969 * reorder q; this means that if we flush we might push
970 * frames that still "new"; if this happens then subsequent
971 * frames will result in BA window moves which cost something
972 * but is still better than a big throughput dip.
974 if (rap->rxa_qframes != 0) {
975 /* XXX honor batimeout? */
976 if (ticks - rap->rxa_age > ieee80211_ampdu_age) {
978 * Too long since we received the first
979 * frame; flush the reorder buffer.
981 if (rap->rxa_qframes != 0) {
982 vap->iv_stats.is_ampdu_rx_age +=
984 ampdu_rx_flush(ni, rap);
986 rap->rxa_start = IEEE80211_SEQ_INC(rxseq);
991 * First frame, start aging timer.
993 rap->rxa_age = ticks;
996 /* save packet - this consumes, no matter what */
997 ampdu_rx_add_slot(rap, off, tid, rxseq, ni, m);
1001 if (off < IEEE80211_SEQ_BA_RANGE) {
1003 * Outside the BA window, but within range;
1004 * flush the reorder q and move the window.
1005 * Sec 9.10.7.6.2 b) (p.138)
1007 IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
1008 "move BA win <%u:%u> (%u frames) rxseq %u tid %u",
1010 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
1011 rap->rxa_qframes, rxseq, tid);
1012 vap->iv_stats.is_ampdu_rx_move++;
1015 * The spec says to flush frames up to but not including:
1016 * WinStart_B = rxseq - rap->rxa_wnd + 1
1017 * Then insert the frame or notify the caller to process
1018 * it immediately. We can safely do this by just starting
1019 * over again because we know the frame will now be within
1022 /* NB: rxa_wnd known to be >0 */
1023 ampdu_rx_flush_upto(ni, rap,
1024 IEEE80211_SEQ_SUB(rxseq, rap->rxa_wnd-1));
1028 * Outside the BA window and out of range; toss.
1029 * Sec 9.10.7.6.2 c) (p.138)
1031 IEEE80211_DISCARD_MAC(vap,
1032 IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
1033 "MPDU", "BA win <%u:%u> (%u frames) rxseq %u tid %u%s",
1035 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
1036 rap->rxa_qframes, rxseq, tid,
1037 wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
1038 vap->iv_stats.is_ampdu_rx_drop++;
1039 IEEE80211_NODE_STAT(ni, rx_drop);
1048 * Process a BAR ctl frame. Dispatch all frames up to
1049 * the sequence number of the frame. If this frame is
1050 * out of range it's discarded.
1053 ieee80211_recv_bar(struct ieee80211_node *ni, struct mbuf *m0)
1055 struct ieee80211vap *vap = ni->ni_vap;
1056 struct ieee80211_frame_bar *wh;
1057 struct ieee80211_rx_ampdu *rap;
1058 ieee80211_seq rxseq;
1061 if (!ieee80211_recv_bar_ena) {
1063 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_11N,
1064 ni->ni_macaddr, "BAR", "%s", "processing disabled");
1066 vap->iv_stats.is_ampdu_bar_bad++;
1069 wh = mtod(m0, struct ieee80211_frame_bar *);
1070 /* XXX check basic BAR */
1071 tid = MS(le16toh(wh->i_ctl), IEEE80211_BAR_TID);
1072 rap = &ni->ni_rx_ampdu[tid];
1073 if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
1075 * No ADDBA request yet, don't touch.
1077 IEEE80211_DISCARD_MAC(vap,
1078 IEEE80211_MSG_INPUT | IEEE80211_MSG_11N,
1079 ni->ni_macaddr, "BAR", "no BA stream, tid %u", tid);
1080 vap->iv_stats.is_ampdu_bar_bad++;
1083 vap->iv_stats.is_ampdu_bar_rx++;
1084 rxseq = le16toh(wh->i_seq) >> IEEE80211_SEQ_SEQ_SHIFT;
1085 if (rxseq == rap->rxa_start)
1087 /* calculate offset in BA window */
1088 off = IEEE80211_SEQ_SUB(rxseq, rap->rxa_start);
1089 if (off < IEEE80211_SEQ_BA_RANGE) {
1091 * Flush the reorder q up to rxseq and move the window.
1092 * Sec 9.10.7.6.3 a) (p.138)
1094 IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
1095 "BAR moves BA win <%u:%u> (%u frames) rxseq %u tid %u",
1097 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
1098 rap->rxa_qframes, rxseq, tid);
1099 vap->iv_stats.is_ampdu_bar_move++;
1101 ampdu_rx_flush_upto(ni, rap, rxseq);
1102 if (off >= rap->rxa_wnd) {
1104 * BAR specifies a window start to the right of BA
1105 * window; we must move it explicitly since
1106 * ampdu_rx_flush_upto will not.
1108 rap->rxa_start = rxseq;
1112 * Out of range; toss.
1113 * Sec 9.10.7.6.3 b) (p.138)
1115 IEEE80211_DISCARD_MAC(vap,
1116 IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
1117 "BAR", "BA win <%u:%u> (%u frames) rxseq %u tid %u%s",
1119 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
1120 rap->rxa_qframes, rxseq, tid,
1121 wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
1122 vap->iv_stats.is_ampdu_bar_oow++;
1123 IEEE80211_NODE_STAT(ni, rx_drop);
1128 * Setup HT-specific state in a node. Called only
1129 * when HT use is negotiated so we don't do extra
1130 * work for temporary and/or legacy sta's.
1133 ieee80211_ht_node_init(struct ieee80211_node *ni)
1135 struct ieee80211_tx_ampdu *tap;
1138 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
1144 if (ni->ni_flags & IEEE80211_NODE_HT) {
1146 * Clean AMPDU state on re-associate. This handles the case
1147 * where a station leaves w/o notifying us and then returns
1148 * before node is reaped for inactivity.
1150 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
1152 "%s: calling cleanup (%p)",
1154 ieee80211_ht_node_cleanup(ni);
1156 for (tid = 0; tid < WME_NUM_TID; tid++) {
1157 tap = &ni->ni_tx_ampdu[tid];
1160 ieee80211_txampdu_init_pps(tap);
1161 /* NB: further initialization deferred */
1163 ni->ni_flags |= IEEE80211_NODE_HT | IEEE80211_NODE_AMPDU;
1167 * Cleanup HT-specific state in a node. Called only
1168 * when HT use has been marked.
1171 ieee80211_ht_node_cleanup(struct ieee80211_node *ni)
1173 struct ieee80211com *ic = ni->ni_ic;
1176 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
1181 KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT node"));
1183 /* XXX optimize this */
1184 for (i = 0; i < WME_NUM_TID; i++) {
1185 struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[i];
1186 if (tap->txa_flags & IEEE80211_AGGR_SETUP)
1189 for (i = 0; i < WME_NUM_TID; i++)
1190 ic->ic_ampdu_rx_stop(ni, &ni->ni_rx_ampdu[i]);
1193 ni->ni_flags &= ~IEEE80211_NODE_HT_ALL;
1197 * Age out HT resources for a station.
1200 ieee80211_ht_node_age(struct ieee80211_node *ni)
1202 struct ieee80211vap *vap = ni->ni_vap;
1205 KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT sta"));
1207 for (tid = 0; tid < WME_NUM_TID; tid++) {
1208 struct ieee80211_rx_ampdu *rap;
1210 rap = &ni->ni_rx_ampdu[tid];
1211 if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0)
1213 if (rap->rxa_qframes == 0)
1216 * Check for frames sitting too long in the reorder queue.
1217 * See above for more details on what's happening here.
1219 /* XXX honor batimeout? */
1220 if (ticks - rap->rxa_age > ieee80211_ampdu_age) {
1222 * Too long since we received the first
1223 * frame; flush the reorder buffer.
1225 vap->iv_stats.is_ampdu_rx_age += rap->rxa_qframes;
1226 ampdu_rx_flush(ni, rap);
1231 static struct ieee80211_channel *
1232 findhtchan(struct ieee80211com *ic, struct ieee80211_channel *c, int htflags)
1234 return ieee80211_find_channel(ic, c->ic_freq,
1235 (c->ic_flags &~ IEEE80211_CHAN_HT) | htflags);
1239 * Adjust a channel to be HT/non-HT according to the vap's configuration.
1241 struct ieee80211_channel *
1242 ieee80211_ht_adjust_channel(struct ieee80211com *ic,
1243 struct ieee80211_channel *chan, int flags)
1245 struct ieee80211_channel *c;
1247 if (flags & IEEE80211_FHT_HT) {
1248 /* promote to HT if possible */
1249 if (flags & IEEE80211_FHT_USEHT40) {
1250 if (!IEEE80211_IS_CHAN_HT40(chan)) {
1251 /* NB: arbitrarily pick ht40+ over ht40- */
1252 c = findhtchan(ic, chan, IEEE80211_CHAN_HT40U);
1254 c = findhtchan(ic, chan,
1255 IEEE80211_CHAN_HT40D);
1257 c = findhtchan(ic, chan,
1258 IEEE80211_CHAN_HT20);
1262 } else if (!IEEE80211_IS_CHAN_HT20(chan)) {
1263 c = findhtchan(ic, chan, IEEE80211_CHAN_HT20);
1267 } else if (IEEE80211_IS_CHAN_HT(chan)) {
1268 /* demote to legacy, HT use is disabled */
1269 c = ieee80211_find_channel(ic, chan->ic_freq,
1270 chan->ic_flags &~ IEEE80211_CHAN_HT);
1278 * Setup HT-specific state for a legacy WDS peer.
1281 ieee80211_ht_wds_init(struct ieee80211_node *ni)
1283 struct ieee80211vap *vap = ni->ni_vap;
1284 struct ieee80211_tx_ampdu *tap;
1287 KASSERT(vap->iv_flags_ht & IEEE80211_FHT_HT, ("no HT requested"));
1289 /* XXX check scan cache in case peer has an ap and we have info */
1291 * If setup with a legacy channel; locate an HT channel.
1292 * Otherwise if the inherited channel (from a companion
1293 * AP) is suitable use it so we use the same location
1294 * for the extension channel).
1296 ni->ni_chan = ieee80211_ht_adjust_channel(ni->ni_ic,
1297 ni->ni_chan, ieee80211_htchanflags(ni->ni_chan));
1300 if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20)
1301 ni->ni_htcap |= IEEE80211_HTCAP_SHORTGI20;
1302 if (IEEE80211_IS_CHAN_HT40(ni->ni_chan)) {
1303 ni->ni_htcap |= IEEE80211_HTCAP_CHWIDTH40;
1305 if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan))
1306 ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_ABOVE;
1307 else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan))
1308 ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_BELOW;
1309 if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40)
1310 ni->ni_htcap |= IEEE80211_HTCAP_SHORTGI40;
1313 ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_NONE;
1315 ni->ni_htctlchan = ni->ni_chan->ic_ieee;
1316 if (vap->iv_flags_ht & IEEE80211_FHT_RIFS)
1317 ni->ni_flags |= IEEE80211_NODE_RIFS;
1318 /* XXX does it make sense to enable SMPS? */
1320 ni->ni_htopmode = 0; /* XXX need protection state */
1321 ni->ni_htstbc = 0; /* XXX need info */
1323 for (tid = 0; tid < WME_NUM_TID; tid++) {
1324 tap = &ni->ni_tx_ampdu[tid];
1326 ieee80211_txampdu_init_pps(tap);
1328 /* NB: AMPDU tx/rx governed by IEEE80211_FHT_AMPDU_{TX,RX} */
1329 ni->ni_flags |= IEEE80211_NODE_HT | IEEE80211_NODE_AMPDU;
1333 * Notify hostap vaps of a change in the HTINFO ie.
1336 htinfo_notify(struct ieee80211com *ic)
1338 struct ieee80211vap *vap;
1341 IEEE80211_LOCK_ASSERT(ic);
1343 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1344 if (vap->iv_opmode != IEEE80211_M_HOSTAP)
1346 if (vap->iv_state != IEEE80211_S_RUN ||
1347 !IEEE80211_IS_CHAN_HT(vap->iv_bss->ni_chan))
1351 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N,
1353 "HT bss occupancy change: %d sta, %d ht, "
1354 "%d ht40%s, HT protmode now 0x%x"
1356 , ic->ic_ht_sta_assoc
1357 , ic->ic_ht40_sta_assoc
1358 , (ic->ic_flags_ht & IEEE80211_FHT_NONHT_PR) ?
1359 ", non-HT sta present" : ""
1360 , ic->ic_curhtprotmode);
1363 ieee80211_beacon_notify(vap, IEEE80211_BEACON_HTINFO);
1368 * Calculate HT protection mode from current
1369 * state and handle updates.
1372 htinfo_update(struct ieee80211com *ic)
1376 if (ic->ic_sta_assoc != ic->ic_ht_sta_assoc) {
1377 protmode = IEEE80211_HTINFO_OPMODE_MIXED
1378 | IEEE80211_HTINFO_NONHT_PRESENT;
1379 } else if (ic->ic_flags_ht & IEEE80211_FHT_NONHT_PR) {
1380 protmode = IEEE80211_HTINFO_OPMODE_PROTOPT
1381 | IEEE80211_HTINFO_NONHT_PRESENT;
1382 } else if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
1383 IEEE80211_IS_CHAN_HT40(ic->ic_bsschan) &&
1384 ic->ic_sta_assoc != ic->ic_ht40_sta_assoc) {
1385 protmode = IEEE80211_HTINFO_OPMODE_HT20PR;
1387 protmode = IEEE80211_HTINFO_OPMODE_PURE;
1389 if (protmode != ic->ic_curhtprotmode) {
1390 ic->ic_curhtprotmode = protmode;
1396 * Handle an HT station joining a BSS.
1399 ieee80211_ht_node_join(struct ieee80211_node *ni)
1401 struct ieee80211com *ic = ni->ni_ic;
1403 IEEE80211_LOCK_ASSERT(ic);
1405 if (ni->ni_flags & IEEE80211_NODE_HT) {
1406 ic->ic_ht_sta_assoc++;
1407 if (ni->ni_chw == 40)
1408 ic->ic_ht40_sta_assoc++;
1414 * Handle an HT station leaving a BSS.
1417 ieee80211_ht_node_leave(struct ieee80211_node *ni)
1419 struct ieee80211com *ic = ni->ni_ic;
1421 IEEE80211_LOCK_ASSERT(ic);
1423 if (ni->ni_flags & IEEE80211_NODE_HT) {
1424 ic->ic_ht_sta_assoc--;
1425 if (ni->ni_chw == 40)
1426 ic->ic_ht40_sta_assoc--;
1432 * Public version of htinfo_update; used for processing
1433 * beacon frames from overlapping bss.
1435 * Caller can specify either IEEE80211_HTINFO_OPMODE_MIXED
1436 * (on receipt of a beacon that advertises MIXED) or
1437 * IEEE80211_HTINFO_OPMODE_PROTOPT (on receipt of a beacon
1438 * from an overlapping legacy bss). We treat MIXED with
1439 * a higher precedence than PROTOPT (i.e. we will not change
1440 * change PROTOPT -> MIXED; only MIXED -> PROTOPT). This
1441 * corresponds to how we handle things in htinfo_update.
1444 ieee80211_htprot_update(struct ieee80211com *ic, int protmode)
1446 #define OPMODE(x) SM(x, IEEE80211_HTINFO_OPMODE)
1449 /* track non-HT station presence */
1450 KASSERT(protmode & IEEE80211_HTINFO_NONHT_PRESENT,
1451 ("protmode 0x%x", protmode));
1452 ic->ic_flags_ht |= IEEE80211_FHT_NONHT_PR;
1453 ic->ic_lastnonht = ticks;
1455 if (protmode != ic->ic_curhtprotmode &&
1456 (OPMODE(ic->ic_curhtprotmode) != IEEE80211_HTINFO_OPMODE_MIXED ||
1457 OPMODE(protmode) == IEEE80211_HTINFO_OPMODE_PROTOPT)) {
1458 /* push beacon update */
1459 ic->ic_curhtprotmode = protmode;
1462 IEEE80211_UNLOCK(ic);
1467 * Time out presence of an overlapping bss with non-HT
1468 * stations. When operating in hostap mode we listen for
1469 * beacons from other stations and if we identify a non-HT
1470 * station is present we update the opmode field of the
1471 * HTINFO ie. To identify when all non-HT stations are
1472 * gone we time out this condition.
1475 ieee80211_ht_timeout(struct ieee80211com *ic)
1477 IEEE80211_LOCK_ASSERT(ic);
1479 if ((ic->ic_flags_ht & IEEE80211_FHT_NONHT_PR) &&
1480 ieee80211_time_after(ticks, ic->ic_lastnonht + IEEE80211_NONHT_PRESENT_AGE)) {
1482 IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
1483 "%s", "time out non-HT STA present on channel");
1485 ic->ic_flags_ht &= ~IEEE80211_FHT_NONHT_PR;
1491 * Process an 802.11n HT capabilities ie.
1494 ieee80211_parse_htcap(struct ieee80211_node *ni, const uint8_t *ie)
1496 if (ie[0] == IEEE80211_ELEMID_VENDOR) {
1498 * Station used Vendor OUI ie to associate;
1499 * mark the node so when we respond we'll use
1500 * the Vendor OUI's and not the standard ie's.
1502 ni->ni_flags |= IEEE80211_NODE_HTCOMPAT;
1505 ni->ni_flags &= ~IEEE80211_NODE_HTCOMPAT;
1507 ni->ni_htcap = le16dec(ie +
1508 __offsetof(struct ieee80211_ie_htcap, hc_cap));
1509 ni->ni_htparam = ie[__offsetof(struct ieee80211_ie_htcap, hc_param)];
1513 htinfo_parse(struct ieee80211_node *ni,
1514 const struct ieee80211_ie_htinfo *htinfo)
1518 ni->ni_htctlchan = htinfo->hi_ctrlchannel;
1519 ni->ni_ht2ndchan = SM(htinfo->hi_byte1, IEEE80211_HTINFO_2NDCHAN);
1520 w = le16dec(&htinfo->hi_byte2);
1521 ni->ni_htopmode = SM(w, IEEE80211_HTINFO_OPMODE);
1522 w = le16dec(&htinfo->hi_byte45);
1523 ni->ni_htstbc = SM(w, IEEE80211_HTINFO_BASIC_STBCMCS);
1527 * Parse an 802.11n HT info ie and save useful information
1528 * to the node state. Note this does not effect any state
1529 * changes such as for channel width change.
1532 ieee80211_parse_htinfo(struct ieee80211_node *ni, const uint8_t *ie)
1534 if (ie[0] == IEEE80211_ELEMID_VENDOR)
1536 htinfo_parse(ni, (const struct ieee80211_ie_htinfo *) ie);
1540 * Handle 11n/11ac channel switch.
1542 * Use the received HT/VHT ie's to identify the right channel to use.
1543 * If we cannot locate it in the channel table then fallback to
1546 * Note that we use this information to identify the node's
1547 * channel only; the caller is responsible for insuring any
1548 * required channel change is done (e.g. in sta mode when
1549 * parsing the contents of a beacon frame).
1552 htinfo_update_chw(struct ieee80211_node *ni, int htflags, int vhtflags)
1554 struct ieee80211com *ic = ni->ni_ic;
1555 struct ieee80211_channel *c;
1560 * First step - do HT/VHT only channel lookup based on operating mode
1561 * flags. This involves masking out the VHT flags as well.
1562 * Otherwise we end up doing the full channel walk each time
1563 * we trigger this, which is expensive.
1565 chanflags = (ni->ni_chan->ic_flags &~
1566 (IEEE80211_CHAN_HT | IEEE80211_CHAN_VHT)) | htflags | vhtflags;
1568 if (chanflags == ni->ni_chan->ic_flags)
1572 * If HT /or/ VHT flags have changed then check both.
1573 * We need to start by picking a HT channel anyway.
1577 chanflags = (ni->ni_chan->ic_flags &~
1578 (IEEE80211_CHAN_HT | IEEE80211_CHAN_VHT)) | htflags;
1579 /* XXX not right for ht40- */
1580 c = ieee80211_find_channel(ic, ni->ni_chan->ic_freq, chanflags);
1581 if (c == NULL && (htflags & IEEE80211_CHAN_HT40)) {
1583 * No HT40 channel entry in our table; fall back
1584 * to HT20 operation. This should not happen.
1586 c = findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT20);
1588 IEEE80211_NOTE(ni->ni_vap,
1589 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
1590 "no HT40 channel (freq %u), falling back to HT20",
1591 ni->ni_chan->ic_freq);
1596 /* Nothing found - leave it alone; move onto VHT */
1601 * If it's non-HT, then bail out now.
1603 if (! IEEE80211_IS_CHAN_HT(c)) {
1604 IEEE80211_NOTE(ni->ni_vap,
1605 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
1606 "not HT; skipping VHT check (%u/0x%x)",
1607 c->ic_freq, c->ic_flags);
1612 * Next step - look at the current VHT flags and determine
1613 * if we need to upgrade. Mask out the VHT and HT flags since
1614 * the vhtflags field will already have the correct HT
1617 if (IEEE80211_CONF_VHT(ic) && ni->ni_vhtcap != 0 && vhtflags != 0) {
1618 chanflags = (c->ic_flags
1619 &~ (IEEE80211_CHAN_HT | IEEE80211_CHAN_VHT))
1621 IEEE80211_NOTE(ni->ni_vap,
1622 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N,
1624 "%s: VHT; chanwidth=0x%02x; vhtflags=0x%08x",
1625 __func__, ni->ni_vht_chanwidth, vhtflags);
1627 IEEE80211_NOTE(ni->ni_vap,
1628 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N,
1630 "%s: VHT; trying lookup for %d/0x%08x",
1631 __func__, c->ic_freq, chanflags);
1632 c = ieee80211_find_channel(ic, c->ic_freq, chanflags);
1635 /* Finally, if it's changed */
1636 if (c != NULL && c != ni->ni_chan) {
1637 IEEE80211_NOTE(ni->ni_vap,
1638 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
1639 "switch station to %s%d channel %u/0x%x",
1640 IEEE80211_IS_CHAN_VHT(c) ? "VHT" : "HT",
1641 IEEE80211_IS_CHAN_VHT80(c) ? 80 :
1642 (IEEE80211_IS_CHAN_HT40(c) ? 40 : 20),
1643 c->ic_freq, c->ic_flags);
1647 /* NB: caller responsible for forcing any channel change */
1650 /* update node's (11n) tx channel width */
1651 ni->ni_chw = IEEE80211_IS_CHAN_HT40(ni->ni_chan)? 40 : 20;
1656 * Update 11n MIMO PS state according to received htcap.
1659 htcap_update_mimo_ps(struct ieee80211_node *ni)
1661 uint16_t oflags = ni->ni_flags;
1663 switch (ni->ni_htcap & IEEE80211_HTCAP_SMPS) {
1664 case IEEE80211_HTCAP_SMPS_DYNAMIC:
1665 ni->ni_flags |= IEEE80211_NODE_MIMO_PS;
1666 ni->ni_flags |= IEEE80211_NODE_MIMO_RTS;
1668 case IEEE80211_HTCAP_SMPS_ENA:
1669 ni->ni_flags |= IEEE80211_NODE_MIMO_PS;
1670 ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS;
1672 case IEEE80211_HTCAP_SMPS_OFF:
1673 default: /* disable on rx of reserved value */
1674 ni->ni_flags &= ~IEEE80211_NODE_MIMO_PS;
1675 ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS;
1678 return (oflags ^ ni->ni_flags);
1682 * Update short GI state according to received htcap
1683 * and local settings.
1685 static __inline void
1686 htcap_update_shortgi(struct ieee80211_node *ni)
1688 struct ieee80211vap *vap = ni->ni_vap;
1690 ni->ni_flags &= ~(IEEE80211_NODE_SGI20|IEEE80211_NODE_SGI40);
1691 if ((ni->ni_htcap & IEEE80211_HTCAP_SHORTGI20) &&
1692 (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20))
1693 ni->ni_flags |= IEEE80211_NODE_SGI20;
1694 if ((ni->ni_htcap & IEEE80211_HTCAP_SHORTGI40) &&
1695 (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40))
1696 ni->ni_flags |= IEEE80211_NODE_SGI40;
1700 * Update LDPC state according to received htcap
1701 * and local settings.
1703 static __inline void
1704 htcap_update_ldpc(struct ieee80211_node *ni)
1706 struct ieee80211vap *vap = ni->ni_vap;
1708 if ((ni->ni_htcap & IEEE80211_HTCAP_LDPC) &&
1709 (vap->iv_flags_ht & IEEE80211_FHT_LDPC_TX))
1710 ni->ni_flags |= IEEE80211_NODE_LDPC;
1714 * Parse and update HT-related state extracted from
1715 * the HT cap and info ie's.
1717 * This is called from the STA management path and
1718 * the ieee80211_node_join() path. It will take into
1719 * account the IEs discovered during scanning and
1720 * adjust things accordingly.
1723 ieee80211_ht_updateparams(struct ieee80211_node *ni,
1724 const uint8_t *htcapie, const uint8_t *htinfoie)
1726 struct ieee80211vap *vap = ni->ni_vap;
1727 const struct ieee80211_ie_htinfo *htinfo;
1729 ieee80211_parse_htcap(ni, htcapie);
1730 if (vap->iv_htcaps & IEEE80211_HTCAP_SMPS)
1731 htcap_update_mimo_ps(ni);
1732 htcap_update_shortgi(ni);
1733 htcap_update_ldpc(ni);
1735 if (htinfoie[0] == IEEE80211_ELEMID_VENDOR)
1737 htinfo = (const struct ieee80211_ie_htinfo *) htinfoie;
1738 htinfo_parse(ni, htinfo);
1741 * Defer the node channel change; we need to now
1742 * update VHT parameters before we do it.
1745 if ((htinfo->hi_byte1 & IEEE80211_HTINFO_RIFSMODE_PERM) &&
1746 (vap->iv_flags_ht & IEEE80211_FHT_RIFS))
1747 ni->ni_flags |= IEEE80211_NODE_RIFS;
1749 ni->ni_flags &= ~IEEE80211_NODE_RIFS;
1753 ieee80211_vht_get_vhtflags(struct ieee80211_node *ni, uint32_t htflags)
1755 struct ieee80211vap *vap = ni->ni_vap;
1756 uint32_t vhtflags = 0;
1759 if (ni->ni_flags & IEEE80211_NODE_VHT && vap->iv_flags_vht & IEEE80211_FVHT_VHT) {
1760 if ((ni->ni_vht_chanwidth == IEEE80211_VHT_CHANWIDTH_160MHZ) &&
1761 /* XXX 2 means "160MHz and 80+80MHz", 1 means "160MHz" */
1762 (MS(vap->iv_vhtcaps,
1763 IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_MASK) >= 1) &&
1764 (vap->iv_flags_vht & IEEE80211_FVHT_USEVHT160)) {
1765 vhtflags = IEEE80211_CHAN_VHT160;
1766 /* Mirror the HT40 flags */
1767 if (htflags == IEEE80211_CHAN_HT40U) {
1768 vhtflags |= IEEE80211_CHAN_HT40U;
1769 } else if (htflags == IEEE80211_CHAN_HT40D) {
1770 vhtflags |= IEEE80211_CHAN_HT40D;
1772 } else if ((ni->ni_vht_chanwidth == IEEE80211_VHT_CHANWIDTH_80P80MHZ) &&
1773 /* XXX 2 means "160MHz and 80+80MHz" */
1774 (MS(vap->iv_vhtcaps,
1775 IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_MASK) == 2) &&
1776 (vap->iv_flags_vht & IEEE80211_FVHT_USEVHT80P80)) {
1777 vhtflags = IEEE80211_CHAN_VHT80_80;
1778 /* Mirror the HT40 flags */
1779 if (htflags == IEEE80211_CHAN_HT40U) {
1780 vhtflags |= IEEE80211_CHAN_HT40U;
1781 } else if (htflags == IEEE80211_CHAN_HT40D) {
1782 vhtflags |= IEEE80211_CHAN_HT40D;
1784 } else if ((ni->ni_vht_chanwidth == IEEE80211_VHT_CHANWIDTH_80MHZ) &&
1785 (vap->iv_flags_vht & IEEE80211_FVHT_USEVHT80)) {
1786 vhtflags = IEEE80211_CHAN_VHT80;
1787 /* Mirror the HT40 flags */
1788 if (htflags == IEEE80211_CHAN_HT40U) {
1789 vhtflags |= IEEE80211_CHAN_HT40U;
1790 } else if (htflags == IEEE80211_CHAN_HT40D) {
1791 vhtflags |= IEEE80211_CHAN_HT40D;
1793 } else if (ni->ni_vht_chanwidth == IEEE80211_VHT_CHANWIDTH_USE_HT) {
1794 /* Mirror the HT40 flags */
1796 * XXX TODO: if ht40 is disabled, but vht40 isn't
1797 * disabled then this logic will get very, very sad.
1798 * It's quite possible the only sane thing to do is
1799 * to not have vht40 as an option, and just obey
1800 * 'ht40' as that flag.
1802 if ((htflags == IEEE80211_CHAN_HT40U) &&
1803 (vap->iv_flags_vht & IEEE80211_FVHT_USEVHT40)) {
1804 vhtflags = IEEE80211_CHAN_VHT40U
1805 | IEEE80211_CHAN_HT40U;
1806 } else if (htflags == IEEE80211_CHAN_HT40D &&
1807 (vap->iv_flags_vht & IEEE80211_FVHT_USEVHT40)) {
1808 vhtflags = IEEE80211_CHAN_VHT40D
1809 | IEEE80211_CHAN_HT40D;
1810 } else if (htflags == IEEE80211_CHAN_HT20) {
1811 vhtflags = IEEE80211_CHAN_VHT20
1812 | IEEE80211_CHAN_HT20;
1815 vhtflags = IEEE80211_CHAN_VHT20;
1822 * Final part of updating the HT parameters.
1824 * This is called from the STA management path and
1825 * the ieee80211_node_join() path. It will take into
1826 * account the IEs discovered during scanning and
1827 * adjust things accordingly.
1829 * This is done after a call to ieee80211_ht_updateparams()
1830 * because it (and the upcoming VHT version of updateparams)
1831 * needs to ensure everything is parsed before htinfo_update_chw()
1832 * is called - which will change the channel config for the
1836 ieee80211_ht_updateparams_final(struct ieee80211_node *ni,
1837 const uint8_t *htcapie, const uint8_t *htinfoie)
1839 struct ieee80211vap *vap = ni->ni_vap;
1840 const struct ieee80211_ie_htinfo *htinfo;
1841 int htflags, vhtflags;
1844 htinfo = (const struct ieee80211_ie_htinfo *) htinfoie;
1846 htflags = (vap->iv_flags_ht & IEEE80211_FHT_HT) ?
1847 IEEE80211_CHAN_HT20 : 0;
1849 /* NB: honor operating mode constraint */
1850 if ((htinfo->hi_byte1 & IEEE80211_HTINFO_TXWIDTH_2040) &&
1851 (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)) {
1852 if (ni->ni_ht2ndchan == IEEE80211_HTINFO_2NDCHAN_ABOVE)
1853 htflags = IEEE80211_CHAN_HT40U;
1854 else if (ni->ni_ht2ndchan == IEEE80211_HTINFO_2NDCHAN_BELOW)
1855 htflags = IEEE80211_CHAN_HT40D;
1859 * VHT flags - do much the same; check whether VHT is available
1860 * and if so, what our ideal channel use would be based on our
1861 * capabilities and the (pre-parsed) VHT info IE.
1863 vhtflags = ieee80211_vht_get_vhtflags(ni, htflags);
1865 if (htinfo_update_chw(ni, htflags, vhtflags))
1872 * Parse and update HT-related state extracted from the HT cap ie
1873 * for a station joining an HT BSS.
1875 * This is called from the hostap path for each station.
1878 ieee80211_ht_updatehtcap(struct ieee80211_node *ni, const uint8_t *htcapie)
1880 struct ieee80211vap *vap = ni->ni_vap;
1882 ieee80211_parse_htcap(ni, htcapie);
1883 if (vap->iv_htcaps & IEEE80211_HTCAP_SMPS)
1884 htcap_update_mimo_ps(ni);
1885 htcap_update_shortgi(ni);
1886 htcap_update_ldpc(ni);
1890 * Called once HT and VHT capabilities are parsed in hostap mode -
1891 * this will adjust the channel configuration of the given node
1892 * based on the configuration and capabilities.
1895 ieee80211_ht_updatehtcap_final(struct ieee80211_node *ni)
1897 struct ieee80211vap *vap = ni->ni_vap;
1901 /* NB: honor operating mode constraint */
1902 /* XXX 40 MHz intolerant */
1903 htflags = (vap->iv_flags_ht & IEEE80211_FHT_HT) ?
1904 IEEE80211_CHAN_HT20 : 0;
1905 if ((ni->ni_htcap & IEEE80211_HTCAP_CHWIDTH40) &&
1906 (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)) {
1907 if (IEEE80211_IS_CHAN_HT40U(vap->iv_bss->ni_chan))
1908 htflags = IEEE80211_CHAN_HT40U;
1909 else if (IEEE80211_IS_CHAN_HT40D(vap->iv_bss->ni_chan))
1910 htflags = IEEE80211_CHAN_HT40D;
1913 * VHT flags - do much the same; check whether VHT is available
1914 * and if so, what our ideal channel use would be based on our
1915 * capabilities and the (pre-parsed) VHT info IE.
1917 vhtflags = ieee80211_vht_get_vhtflags(ni, htflags);
1919 (void) htinfo_update_chw(ni, htflags, vhtflags);
1923 * Install received HT rate set by parsing the HT cap ie.
1926 ieee80211_setup_htrates(struct ieee80211_node *ni, const uint8_t *ie, int flags)
1928 struct ieee80211com *ic = ni->ni_ic;
1929 struct ieee80211vap *vap = ni->ni_vap;
1930 const struct ieee80211_ie_htcap *htcap;
1931 struct ieee80211_htrateset *rs;
1932 int i, maxequalmcs, maxunequalmcs;
1934 maxequalmcs = ic->ic_txstream * 8 - 1;
1936 if (ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL) {
1937 if (ic->ic_txstream >= 2)
1939 if (ic->ic_txstream >= 3)
1941 if (ic->ic_txstream >= 4)
1945 rs = &ni->ni_htrates;
1946 memset(rs, 0, sizeof(*rs));
1948 if (ie[0] == IEEE80211_ELEMID_VENDOR)
1950 htcap = (const struct ieee80211_ie_htcap *) ie;
1951 for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++) {
1952 if (isclr(htcap->hc_mcsset, i))
1954 if (rs->rs_nrates == IEEE80211_HTRATE_MAXSIZE) {
1956 IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
1957 "WARNING, HT rate set too large; only "
1958 "using %u rates", IEEE80211_HTRATE_MAXSIZE);
1959 vap->iv_stats.is_rx_rstoobig++;
1962 if (i <= 31 && i > maxequalmcs)
1965 (ic->ic_htcaps & IEEE80211_HTC_TXMCS32) == 0)
1967 if (i > 32 && i > maxunequalmcs)
1969 rs->rs_rates[rs->rs_nrates++] = i;
1972 return ieee80211_fix_rate(ni, (struct ieee80211_rateset *) rs, flags);
1976 * Mark rates in a node's HT rate set as basic according
1977 * to the information in the supplied HT info ie.
1980 ieee80211_setup_basic_htrates(struct ieee80211_node *ni, const uint8_t *ie)
1982 const struct ieee80211_ie_htinfo *htinfo;
1983 struct ieee80211_htrateset *rs;
1986 if (ie[0] == IEEE80211_ELEMID_VENDOR)
1988 htinfo = (const struct ieee80211_ie_htinfo *) ie;
1989 rs = &ni->ni_htrates;
1990 if (rs->rs_nrates == 0) {
1991 IEEE80211_NOTE(ni->ni_vap,
1992 IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
1993 "%s", "WARNING, empty HT rate set");
1996 for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++) {
1997 if (isclr(htinfo->hi_basicmcsset, i))
1999 for (j = 0; j < rs->rs_nrates; j++)
2000 if ((rs->rs_rates[j] & IEEE80211_RATE_VAL) == i)
2001 rs->rs_rates[j] |= IEEE80211_RATE_BASIC;
2006 ampdu_tx_setup(struct ieee80211_tx_ampdu *tap)
2008 callout_init(&tap->txa_timer, 1);
2009 tap->txa_flags |= IEEE80211_AGGR_SETUP;
2010 tap->txa_lastsample = ticks;
2014 ampdu_tx_stop(struct ieee80211_tx_ampdu *tap)
2016 struct ieee80211_node *ni = tap->txa_ni;
2017 struct ieee80211com *ic = ni->ni_ic;
2019 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2024 KASSERT(tap->txa_flags & IEEE80211_AGGR_SETUP,
2025 ("txa_flags 0x%x tid %d ac %d", tap->txa_flags, tap->txa_tid,
2026 TID_TO_WME_AC(tap->txa_tid)));
2029 * Stop BA stream if setup so driver has a chance
2030 * to reclaim any resources it might have allocated.
2032 ic->ic_addba_stop(ni, tap);
2034 * Stop any pending BAR transmit.
2036 bar_stop_timer(tap);
2039 * Reset packet estimate.
2041 ieee80211_txampdu_init_pps(tap);
2043 /* NB: clearing NAK means we may re-send ADDBA */
2044 tap->txa_flags &= ~(IEEE80211_AGGR_SETUP | IEEE80211_AGGR_NAK);
2048 * ADDBA response timeout.
2050 * If software aggregation and per-TID queue management was done here,
2051 * that queue would be unpaused after the ADDBA timeout occurs.
2054 addba_timeout(void *arg)
2056 struct ieee80211_tx_ampdu *tap = arg;
2057 struct ieee80211_node *ni = tap->txa_ni;
2058 struct ieee80211com *ic = ni->ni_ic;
2061 tap->txa_flags &= ~IEEE80211_AGGR_XCHGPEND;
2062 tap->txa_attempts++;
2063 ic->ic_addba_response_timeout(ni, tap);
2067 addba_start_timeout(struct ieee80211_tx_ampdu *tap)
2069 /* XXX use CALLOUT_PENDING instead? */
2070 callout_reset(&tap->txa_timer, ieee80211_addba_timeout,
2071 addba_timeout, tap);
2072 tap->txa_flags |= IEEE80211_AGGR_XCHGPEND;
2073 tap->txa_nextrequest = ticks + ieee80211_addba_timeout;
2077 addba_stop_timeout(struct ieee80211_tx_ampdu *tap)
2079 /* XXX use CALLOUT_PENDING instead? */
2080 if (tap->txa_flags & IEEE80211_AGGR_XCHGPEND) {
2081 callout_stop(&tap->txa_timer);
2082 tap->txa_flags &= ~IEEE80211_AGGR_XCHGPEND;
2087 null_addba_response_timeout(struct ieee80211_node *ni,
2088 struct ieee80211_tx_ampdu *tap)
2093 * Default method for requesting A-MPDU tx aggregation.
2094 * We setup the specified state block and start a timer
2095 * to wait for an ADDBA response frame.
2098 ieee80211_addba_request(struct ieee80211_node *ni,
2099 struct ieee80211_tx_ampdu *tap,
2100 int dialogtoken, int baparamset, int batimeout)
2105 tap->txa_token = dialogtoken;
2106 tap->txa_flags |= IEEE80211_AGGR_IMMEDIATE;
2107 bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
2108 tap->txa_wnd = (bufsiz == 0) ?
2109 IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
2110 addba_start_timeout(tap);
2115 * Called by drivers that wish to request an ADDBA session be
2116 * setup. This brings it up and starts the request timer.
2119 ieee80211_ampdu_tx_request_ext(struct ieee80211_node *ni, int tid)
2121 struct ieee80211_tx_ampdu *tap;
2123 if (tid < 0 || tid > 15)
2125 tap = &ni->ni_tx_ampdu[tid];
2128 if ((tap->txa_flags & IEEE80211_AGGR_SETUP) == 0) {
2129 /* do deferred setup of state */
2130 ampdu_tx_setup(tap);
2132 /* XXX hack for not doing proper locking */
2133 tap->txa_flags &= ~IEEE80211_AGGR_NAK;
2134 addba_start_timeout(tap);
2139 * Called by drivers that have marked a session as active.
2142 ieee80211_ampdu_tx_request_active_ext(struct ieee80211_node *ni, int tid,
2145 struct ieee80211_tx_ampdu *tap;
2147 if (tid < 0 || tid > 15)
2149 tap = &ni->ni_tx_ampdu[tid];
2152 addba_stop_timeout(tap);
2154 tap->txa_flags |= IEEE80211_AGGR_RUNNING;
2155 tap->txa_attempts = 0;
2157 /* mark tid so we don't try again */
2158 tap->txa_flags |= IEEE80211_AGGR_NAK;
2164 * Default method for processing an A-MPDU tx aggregation
2165 * response. We shutdown any pending timer and update the
2166 * state block according to the reply.
2169 ieee80211_addba_response(struct ieee80211_node *ni,
2170 struct ieee80211_tx_ampdu *tap,
2171 int status, int baparamset, int batimeout)
2176 addba_stop_timeout(tap);
2177 if (status == IEEE80211_STATUS_SUCCESS) {
2178 bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
2179 /* XXX override our request? */
2180 tap->txa_wnd = (bufsiz == 0) ?
2181 IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
2183 tid = MS(baparamset, IEEE80211_BAPS_TID);
2184 tap->txa_flags |= IEEE80211_AGGR_RUNNING;
2185 tap->txa_attempts = 0;
2187 /* mark tid so we don't try again */
2188 tap->txa_flags |= IEEE80211_AGGR_NAK;
2194 * Default method for stopping A-MPDU tx aggregation.
2195 * Any timer is cleared and we drain any pending frames.
2198 ieee80211_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
2201 addba_stop_timeout(tap);
2202 if (tap->txa_flags & IEEE80211_AGGR_RUNNING) {
2203 /* XXX clear aggregation queue */
2204 tap->txa_flags &= ~IEEE80211_AGGR_RUNNING;
2206 tap->txa_attempts = 0;
2210 * Process a received action frame using the default aggregation
2211 * policy. We intercept ADDBA-related frames and use them to
2212 * update our aggregation state. All other frames are passed up
2213 * for processing by ieee80211_recv_action.
2216 ht_recv_action_ba_addba_request(struct ieee80211_node *ni,
2217 const struct ieee80211_frame *wh,
2218 const uint8_t *frm, const uint8_t *efrm)
2220 struct ieee80211com *ic = ni->ni_ic;
2221 struct ieee80211vap *vap = ni->ni_vap;
2222 struct ieee80211_rx_ampdu *rap;
2223 uint8_t dialogtoken;
2224 uint16_t baparamset, batimeout, baseqctl;
2228 dialogtoken = frm[2];
2229 baparamset = le16dec(frm+3);
2230 batimeout = le16dec(frm+5);
2231 baseqctl = le16dec(frm+7);
2233 tid = MS(baparamset, IEEE80211_BAPS_TID);
2235 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2236 "recv ADDBA request: dialogtoken %u baparamset 0x%x "
2237 "(tid %d bufsiz %d) batimeout %d baseqctl %d:%d",
2238 dialogtoken, baparamset,
2239 tid, MS(baparamset, IEEE80211_BAPS_BUFSIZ),
2241 MS(baseqctl, IEEE80211_BASEQ_START),
2242 MS(baseqctl, IEEE80211_BASEQ_FRAG));
2244 rap = &ni->ni_rx_ampdu[tid];
2246 /* Send ADDBA response */
2247 args[0] = dialogtoken;
2249 * NB: We ack only if the sta associated with HT and
2250 * the ap is configured to do AMPDU rx (the latter
2251 * violates the 11n spec and is mostly for testing).
2253 if ((ni->ni_flags & IEEE80211_NODE_AMPDU_RX) &&
2254 (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_RX)) {
2255 /* XXX handle ampdu_rx_start failure */
2256 ic->ic_ampdu_rx_start(ni, rap,
2257 baparamset, batimeout, baseqctl);
2259 args[1] = IEEE80211_STATUS_SUCCESS;
2261 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2262 ni, "reject ADDBA request: %s",
2263 ni->ni_flags & IEEE80211_NODE_AMPDU_RX ?
2264 "administratively disabled" :
2265 "not negotiated for station");
2266 vap->iv_stats.is_addba_reject++;
2267 args[1] = IEEE80211_STATUS_UNSPECIFIED;
2269 /* XXX honor rap flags? */
2270 args[2] = IEEE80211_BAPS_POLICY_IMMEDIATE
2271 | SM(tid, IEEE80211_BAPS_TID)
2272 | SM(rap->rxa_wnd, IEEE80211_BAPS_BUFSIZ)
2276 ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
2277 IEEE80211_ACTION_BA_ADDBA_RESPONSE, args);
2282 ht_recv_action_ba_addba_response(struct ieee80211_node *ni,
2283 const struct ieee80211_frame *wh,
2284 const uint8_t *frm, const uint8_t *efrm)
2286 struct ieee80211com *ic = ni->ni_ic;
2287 struct ieee80211vap *vap = ni->ni_vap;
2288 struct ieee80211_tx_ampdu *tap;
2289 uint8_t dialogtoken, policy;
2290 uint16_t baparamset, batimeout, code;
2293 dialogtoken = frm[2];
2294 code = le16dec(frm+3);
2295 baparamset = le16dec(frm+5);
2296 tid = MS(baparamset, IEEE80211_BAPS_TID);
2297 bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
2298 policy = MS(baparamset, IEEE80211_BAPS_POLICY);
2299 batimeout = le16dec(frm+7);
2301 tap = &ni->ni_tx_ampdu[tid];
2302 if ((tap->txa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
2303 IEEE80211_DISCARD_MAC(vap,
2304 IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2305 ni->ni_macaddr, "ADDBA response",
2306 "no pending ADDBA, tid %d dialogtoken %u "
2307 "code %d", tid, dialogtoken, code);
2308 vap->iv_stats.is_addba_norequest++;
2311 if (dialogtoken != tap->txa_token) {
2312 IEEE80211_DISCARD_MAC(vap,
2313 IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2314 ni->ni_macaddr, "ADDBA response",
2315 "dialogtoken mismatch: waiting for %d, "
2316 "received %d, tid %d code %d",
2317 tap->txa_token, dialogtoken, tid, code);
2318 vap->iv_stats.is_addba_badtoken++;
2321 /* NB: assumes IEEE80211_AGGR_IMMEDIATE is 1 */
2322 if (policy != (tap->txa_flags & IEEE80211_AGGR_IMMEDIATE)) {
2323 IEEE80211_DISCARD_MAC(vap,
2324 IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2325 ni->ni_macaddr, "ADDBA response",
2326 "policy mismatch: expecting %s, "
2327 "received %s, tid %d code %d",
2328 tap->txa_flags & IEEE80211_AGGR_IMMEDIATE,
2330 vap->iv_stats.is_addba_badpolicy++;
2334 /* XXX we take MIN in ieee80211_addba_response */
2335 if (bufsiz > IEEE80211_AGGR_BAWMAX) {
2336 IEEE80211_DISCARD_MAC(vap,
2337 IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2338 ni->ni_macaddr, "ADDBA response",
2339 "BA window too large: max %d, "
2340 "received %d, tid %d code %d",
2341 bufsiz, IEEE80211_AGGR_BAWMAX, tid, code);
2342 vap->iv_stats.is_addba_badbawinsize++;
2346 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2347 "recv ADDBA response: dialogtoken %u code %d "
2348 "baparamset 0x%x (tid %d bufsiz %d) batimeout %d",
2349 dialogtoken, code, baparamset, tid, bufsiz,
2351 ic->ic_addba_response(ni, tap, code, baparamset, batimeout);
2356 ht_recv_action_ba_delba(struct ieee80211_node *ni,
2357 const struct ieee80211_frame *wh,
2358 const uint8_t *frm, const uint8_t *efrm)
2360 struct ieee80211com *ic = ni->ni_ic;
2361 struct ieee80211_rx_ampdu *rap;
2362 struct ieee80211_tx_ampdu *tap;
2363 uint16_t baparamset, code;
2366 baparamset = le16dec(frm+2);
2367 code = le16dec(frm+4);
2369 tid = MS(baparamset, IEEE80211_DELBAPS_TID);
2371 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2372 "recv DELBA: baparamset 0x%x (tid %d initiator %d) "
2373 "code %d", baparamset, tid,
2374 MS(baparamset, IEEE80211_DELBAPS_INIT), code);
2376 if ((baparamset & IEEE80211_DELBAPS_INIT) == 0) {
2377 tap = &ni->ni_tx_ampdu[tid];
2378 ic->ic_addba_stop(ni, tap);
2380 rap = &ni->ni_rx_ampdu[tid];
2381 ic->ic_ampdu_rx_stop(ni, rap);
2387 ht_recv_action_ht_txchwidth(struct ieee80211_node *ni,
2388 const struct ieee80211_frame *wh,
2389 const uint8_t *frm, const uint8_t *efrm)
2393 chw = (frm[2] == IEEE80211_A_HT_TXCHWIDTH_2040) ? 40 : 20;
2395 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2396 "%s: HT txchwidth, width %d%s",
2397 __func__, chw, ni->ni_chw != chw ? "*" : "");
2398 if (chw != ni->ni_chw) {
2399 /* XXX does this need to change the ht40 station count? */
2401 /* XXX notify on change */
2407 ht_recv_action_ht_mimopwrsave(struct ieee80211_node *ni,
2408 const struct ieee80211_frame *wh,
2409 const uint8_t *frm, const uint8_t *efrm)
2411 const struct ieee80211_action_ht_mimopowersave *mps =
2412 (const struct ieee80211_action_ht_mimopowersave *) frm;
2414 /* XXX check iv_htcaps */
2415 if (mps->am_control & IEEE80211_A_HT_MIMOPWRSAVE_ENA)
2416 ni->ni_flags |= IEEE80211_NODE_MIMO_PS;
2418 ni->ni_flags &= ~IEEE80211_NODE_MIMO_PS;
2419 if (mps->am_control & IEEE80211_A_HT_MIMOPWRSAVE_MODE)
2420 ni->ni_flags |= IEEE80211_NODE_MIMO_RTS;
2422 ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS;
2423 /* XXX notify on change */
2424 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2425 "%s: HT MIMO PS (%s%s)", __func__,
2426 (ni->ni_flags & IEEE80211_NODE_MIMO_PS) ? "on" : "off",
2427 (ni->ni_flags & IEEE80211_NODE_MIMO_RTS) ? "+rts" : ""
2433 * Transmit processing.
2437 * Check if A-MPDU should be requested/enabled for a stream.
2438 * We require a traffic rate above a per-AC threshold and we
2439 * also handle backoff from previous failed attempts.
2441 * Drivers may override this method to bring in information
2442 * such as link state conditions in making the decision.
2445 ieee80211_ampdu_enable(struct ieee80211_node *ni,
2446 struct ieee80211_tx_ampdu *tap)
2448 struct ieee80211vap *vap = ni->ni_vap;
2450 if (tap->txa_avgpps <
2451 vap->iv_ampdu_mintraffic[TID_TO_WME_AC(tap->txa_tid)])
2453 /* XXX check rssi? */
2454 if (tap->txa_attempts >= ieee80211_addba_maxtries &&
2455 ieee80211_time_after(ticks, tap->txa_nextrequest)) {
2457 * Don't retry too often; txa_nextrequest is set
2458 * to the minimum interval we'll retry after
2459 * ieee80211_addba_maxtries failed attempts are made.
2463 IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
2464 "enable AMPDU on tid %d (%s), avgpps %d pkts %d attempt %d",
2465 tap->txa_tid, ieee80211_wme_acnames[TID_TO_WME_AC(tap->txa_tid)],
2466 tap->txa_avgpps, tap->txa_pkts, tap->txa_attempts);
2471 * Request A-MPDU tx aggregation. Setup local state and
2472 * issue an ADDBA request. BA use will only happen after
2473 * the other end replies with ADDBA response.
2476 ieee80211_ampdu_request(struct ieee80211_node *ni,
2477 struct ieee80211_tx_ampdu *tap)
2479 struct ieee80211com *ic = ni->ni_ic;
2481 int tid, dialogtoken;
2482 static int tokens = 0; /* XXX */
2485 if ((tap->txa_flags & IEEE80211_AGGR_SETUP) == 0) {
2486 /* do deferred setup of state */
2487 ampdu_tx_setup(tap);
2489 /* XXX hack for not doing proper locking */
2490 tap->txa_flags &= ~IEEE80211_AGGR_NAK;
2492 dialogtoken = (tokens+1) % 63; /* XXX */
2496 * XXX TODO: This is racy with any other parallel TX going on. :(
2498 tap->txa_start = ni->ni_txseqs[tid];
2500 args[0] = dialogtoken;
2501 args[1] = 0; /* NB: status code not used */
2502 args[2] = IEEE80211_BAPS_POLICY_IMMEDIATE
2503 | SM(tid, IEEE80211_BAPS_TID)
2504 | SM(IEEE80211_AGGR_BAWMAX, IEEE80211_BAPS_BUFSIZ)
2506 args[3] = 0; /* batimeout */
2507 /* NB: do first so there's no race against reply */
2508 if (!ic->ic_addba_request(ni, tap, dialogtoken, args[2], args[3])) {
2509 /* unable to setup state, don't make request */
2510 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2511 ni, "%s: could not setup BA stream for TID %d AC %d",
2512 __func__, tap->txa_tid, TID_TO_WME_AC(tap->txa_tid));
2513 /* defer next try so we don't slam the driver with requests */
2514 tap->txa_attempts = ieee80211_addba_maxtries;
2515 /* NB: check in case driver wants to override */
2516 if (tap->txa_nextrequest <= ticks)
2517 tap->txa_nextrequest = ticks + ieee80211_addba_backoff;
2520 tokens = dialogtoken; /* allocate token */
2521 /* NB: after calling ic_addba_request so driver can set txa_start */
2522 args[4] = SM(tap->txa_start, IEEE80211_BASEQ_START)
2523 | SM(0, IEEE80211_BASEQ_FRAG)
2525 return ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
2526 IEEE80211_ACTION_BA_ADDBA_REQUEST, args);
2530 * Terminate an AMPDU tx stream. State is reclaimed
2531 * and the peer notified with a DelBA Action frame.
2534 ieee80211_ampdu_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
2537 struct ieee80211com *ic = ni->ni_ic;
2538 struct ieee80211vap *vap = ni->ni_vap;
2542 tap->txa_flags &= ~IEEE80211_AGGR_BARPEND;
2543 if (IEEE80211_AMPDU_RUNNING(tap)) {
2544 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2545 ni, "%s: stop BA stream for TID %d (reason: %d (%s))",
2546 __func__, tap->txa_tid, reason,
2547 ieee80211_reason_to_string(reason));
2548 vap->iv_stats.is_ampdu_stop++;
2550 ic->ic_addba_stop(ni, tap);
2551 args[0] = tap->txa_tid;
2552 args[1] = IEEE80211_DELBAPS_INIT;
2553 args[2] = reason; /* XXX reason code */
2554 ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
2555 IEEE80211_ACTION_BA_DELBA, args);
2557 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2558 ni, "%s: BA stream for TID %d not running "
2559 "(reason: %d (%s))", __func__, tap->txa_tid, reason,
2560 ieee80211_reason_to_string(reason));
2561 vap->iv_stats.is_ampdu_stop_failed++;
2566 static void bar_start_timer(struct ieee80211_tx_ampdu *tap);
2569 bar_timeout(void *arg)
2571 struct ieee80211_tx_ampdu *tap = arg;
2572 struct ieee80211_node *ni = tap->txa_ni;
2574 KASSERT((tap->txa_flags & IEEE80211_AGGR_XCHGPEND) == 0,
2575 ("bar/addba collision, flags 0x%x", tap->txa_flags));
2577 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2578 ni, "%s: tid %u flags 0x%x attempts %d", __func__,
2579 tap->txa_tid, tap->txa_flags, tap->txa_attempts);
2581 /* guard against race with bar_tx_complete */
2582 if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) == 0)
2585 if (tap->txa_attempts >= ieee80211_bar_maxtries) {
2586 struct ieee80211com *ic = ni->ni_ic;
2588 ni->ni_vap->iv_stats.is_ampdu_bar_tx_fail++;
2590 * If (at least) the last BAR TX timeout was due to
2591 * an ieee80211_send_bar() failures, then we need
2592 * to make sure we notify the driver that a BAR
2593 * TX did occur and fail. This gives the driver
2594 * a chance to undo any queue pause that may
2597 ic->ic_bar_response(ni, tap, 1);
2598 ieee80211_ampdu_stop(ni, tap, IEEE80211_REASON_TIMEOUT);
2600 ni->ni_vap->iv_stats.is_ampdu_bar_tx_retry++;
2601 if (ieee80211_send_bar(ni, tap, tap->txa_seqpending) != 0) {
2602 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2603 ni, "%s: failed to TX, starting timer\n",
2606 * If ieee80211_send_bar() fails here, the
2607 * timer may have stopped and/or the pending
2608 * flag may be clear. Because of this,
2609 * fake the BARPEND and reset the timer.
2610 * A retransmission attempt will then occur
2611 * during the next timeout.
2614 tap->txa_flags |= IEEE80211_AGGR_BARPEND;
2615 bar_start_timer(tap);
2621 bar_start_timer(struct ieee80211_tx_ampdu *tap)
2623 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2627 callout_reset(&tap->txa_timer, ieee80211_bar_timeout, bar_timeout, tap);
2631 bar_stop_timer(struct ieee80211_tx_ampdu *tap)
2633 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2637 callout_stop(&tap->txa_timer);
2641 bar_tx_complete(struct ieee80211_node *ni, void *arg, int status)
2643 struct ieee80211_tx_ampdu *tap = arg;
2645 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2646 ni, "%s: tid %u flags 0x%x pending %d status %d",
2647 __func__, tap->txa_tid, tap->txa_flags,
2648 callout_pending(&tap->txa_timer), status);
2650 ni->ni_vap->iv_stats.is_ampdu_bar_tx++;
2652 if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) &&
2653 callout_pending(&tap->txa_timer)) {
2654 struct ieee80211com *ic = ni->ni_ic;
2656 if (status == 0) /* ACK'd */
2657 bar_stop_timer(tap);
2658 ic->ic_bar_response(ni, tap, status);
2659 /* NB: just let timer expire so we pace requests */
2664 ieee80211_bar_response(struct ieee80211_node *ni,
2665 struct ieee80211_tx_ampdu *tap, int status)
2668 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2672 if (status == 0) { /* got ACK */
2673 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2674 ni, "BAR moves BA win <%u:%u> (%u frames) txseq %u tid %u",
2676 IEEE80211_SEQ_ADD(tap->txa_start, tap->txa_wnd-1),
2677 tap->txa_qframes, tap->txa_seqpending,
2680 /* NB: timer already stopped in bar_tx_complete */
2681 tap->txa_start = tap->txa_seqpending;
2682 tap->txa_flags &= ~IEEE80211_AGGR_BARPEND;
2687 * Transmit a BAR frame to the specified node. The
2688 * BAR contents are drawn from the supplied aggregation
2689 * state associated with the node.
2691 * NB: we only handle immediate ACK w/ compressed bitmap.
2694 ieee80211_send_bar(struct ieee80211_node *ni,
2695 struct ieee80211_tx_ampdu *tap, ieee80211_seq seq)
2697 #define senderr(_x, _v) do { vap->iv_stats._v++; ret = _x; goto bad; } while (0)
2698 struct ieee80211vap *vap = ni->ni_vap;
2699 struct ieee80211com *ic = ni->ni_ic;
2700 struct ieee80211_frame_bar *bar;
2702 uint16_t barctl, barseqctl;
2707 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2712 if ((tap->txa_flags & IEEE80211_AGGR_RUNNING) == 0) {
2713 /* no ADDBA response, should not happen */
2718 bar_stop_timer(tap);
2720 ieee80211_ref_node(ni);
2722 m = ieee80211_getmgtframe(&frm, ic->ic_headroom, sizeof(*bar));
2724 senderr(ENOMEM, is_tx_nobuf);
2726 if (!ieee80211_add_callback(m, bar_tx_complete, tap)) {
2728 senderr(ENOMEM, is_tx_nobuf); /* XXX */
2732 bar = mtod(m, struct ieee80211_frame_bar *);
2733 bar->i_fc[0] = IEEE80211_FC0_VERSION_0 |
2734 IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_BAR;
2736 IEEE80211_ADDR_COPY(bar->i_ra, ni->ni_macaddr);
2737 IEEE80211_ADDR_COPY(bar->i_ta, vap->iv_myaddr);
2740 barctl = (tap->txa_flags & IEEE80211_AGGR_IMMEDIATE ?
2741 0 : IEEE80211_BAR_NOACK)
2742 | IEEE80211_BAR_COMP
2743 | SM(tid, IEEE80211_BAR_TID)
2745 barseqctl = SM(seq, IEEE80211_BAR_SEQ_START);
2746 /* NB: known to have proper alignment */
2747 bar->i_ctl = htole16(barctl);
2748 bar->i_seq = htole16(barseqctl);
2749 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_bar);
2751 M_WME_SETAC(m, WME_AC_VO);
2753 IEEE80211_NODE_STAT(ni, tx_mgmt); /* XXX tx_ctl? */
2756 /* init/bump attempts counter */
2757 if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) == 0)
2758 tap->txa_attempts = 1;
2760 tap->txa_attempts++;
2761 tap->txa_seqpending = seq;
2762 tap->txa_flags |= IEEE80211_AGGR_BARPEND;
2764 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_11N,
2765 ni, "send BAR: tid %u ctl 0x%x start %u (attempt %d)",
2766 tid, barctl, seq, tap->txa_attempts);
2769 * ic_raw_xmit will free the node reference
2770 * regardless of queue/TX success or failure.
2772 IEEE80211_TX_LOCK(ic);
2773 ret = ieee80211_raw_output(vap, ni, m, NULL);
2774 IEEE80211_TX_UNLOCK(ic);
2776 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_11N,
2777 ni, "send BAR: failed: (ret = %d)\n",
2779 /* xmit failed, clear state flag */
2780 tap->txa_flags &= ~IEEE80211_AGGR_BARPEND;
2781 vap->iv_stats.is_ampdu_bar_tx_fail++;
2784 /* XXX hack against tx complete happening before timer is started */
2785 if (tap->txa_flags & IEEE80211_AGGR_BARPEND)
2786 bar_start_timer(tap);
2789 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2793 vap->iv_stats.is_ampdu_bar_tx_fail++;
2794 ieee80211_free_node(ni);
2800 ht_action_output(struct ieee80211_node *ni, struct mbuf *m)
2802 struct ieee80211_bpf_params params;
2804 memset(¶ms, 0, sizeof(params));
2805 params.ibp_pri = WME_AC_VO;
2806 params.ibp_rate0 = ni->ni_txparms->mgmtrate;
2807 /* NB: we know all frames are unicast */
2808 params.ibp_try0 = ni->ni_txparms->maxretry;
2809 params.ibp_power = ni->ni_txpower;
2810 return ieee80211_mgmt_output(ni, m, IEEE80211_FC0_SUBTYPE_ACTION,
2814 #define ADDSHORT(frm, v) do { \
2815 frm[0] = (v) & 0xff; \
2816 frm[1] = (v) >> 8; \
2821 * Send an action management frame. The arguments are stuff
2822 * into a frame without inspection; the caller is assumed to
2823 * prepare them carefully (e.g. based on the aggregation state).
2826 ht_send_action_ba_addba(struct ieee80211_node *ni,
2827 int category, int action, void *arg0)
2829 struct ieee80211vap *vap = ni->ni_vap;
2830 struct ieee80211com *ic = ni->ni_ic;
2831 uint16_t *args = arg0;
2835 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2836 "send ADDBA %s: dialogtoken %d status %d "
2837 "baparamset 0x%x (tid %d) batimeout 0x%x baseqctl 0x%x",
2838 (action == IEEE80211_ACTION_BA_ADDBA_REQUEST) ?
2839 "request" : "response",
2840 args[0], args[1], args[2], MS(args[2], IEEE80211_BAPS_TID),
2843 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2844 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2845 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2846 ieee80211_ref_node(ni);
2848 m = ieee80211_getmgtframe(&frm,
2849 ic->ic_headroom + sizeof(struct ieee80211_frame),
2850 sizeof(uint16_t) /* action+category */
2851 /* XXX may action payload */
2852 + sizeof(struct ieee80211_action_ba_addbaresponse)
2857 *frm++ = args[0]; /* dialog token */
2858 if (action == IEEE80211_ACTION_BA_ADDBA_RESPONSE)
2859 ADDSHORT(frm, args[1]); /* status code */
2860 ADDSHORT(frm, args[2]); /* baparamset */
2861 ADDSHORT(frm, args[3]); /* batimeout */
2862 if (action == IEEE80211_ACTION_BA_ADDBA_REQUEST)
2863 ADDSHORT(frm, args[4]); /* baseqctl */
2864 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2865 return ht_action_output(ni, m);
2867 vap->iv_stats.is_tx_nobuf++;
2868 ieee80211_free_node(ni);
2874 ht_send_action_ba_delba(struct ieee80211_node *ni,
2875 int category, int action, void *arg0)
2877 struct ieee80211vap *vap = ni->ni_vap;
2878 struct ieee80211com *ic = ni->ni_ic;
2879 uint16_t *args = arg0;
2881 uint16_t baparamset;
2884 baparamset = SM(args[0], IEEE80211_DELBAPS_TID)
2887 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2888 "send DELBA action: tid %d, initiator %d reason %d (%s)",
2889 args[0], args[1], args[2], ieee80211_reason_to_string(args[2]));
2891 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2892 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2893 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2894 ieee80211_ref_node(ni);
2896 m = ieee80211_getmgtframe(&frm,
2897 ic->ic_headroom + sizeof(struct ieee80211_frame),
2898 sizeof(uint16_t) /* action+category */
2899 /* XXX may action payload */
2900 + sizeof(struct ieee80211_action_ba_addbaresponse)
2905 ADDSHORT(frm, baparamset);
2906 ADDSHORT(frm, args[2]); /* reason code */
2907 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2908 return ht_action_output(ni, m);
2910 vap->iv_stats.is_tx_nobuf++;
2911 ieee80211_free_node(ni);
2917 ht_send_action_ht_txchwidth(struct ieee80211_node *ni,
2918 int category, int action, void *arg0)
2920 struct ieee80211vap *vap = ni->ni_vap;
2921 struct ieee80211com *ic = ni->ni_ic;
2925 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2926 "send HT txchwidth: width %d",
2927 IEEE80211_IS_CHAN_HT40(ni->ni_chan) ? 40 : 20);
2929 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2930 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2931 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2932 ieee80211_ref_node(ni);
2934 m = ieee80211_getmgtframe(&frm,
2935 ic->ic_headroom + sizeof(struct ieee80211_frame),
2936 sizeof(uint16_t) /* action+category */
2937 /* XXX may action payload */
2938 + sizeof(struct ieee80211_action_ba_addbaresponse)
2943 *frm++ = IEEE80211_IS_CHAN_HT40(ni->ni_chan) ?
2944 IEEE80211_A_HT_TXCHWIDTH_2040 :
2945 IEEE80211_A_HT_TXCHWIDTH_20;
2946 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2947 return ht_action_output(ni, m);
2949 vap->iv_stats.is_tx_nobuf++;
2950 ieee80211_free_node(ni);
2957 * Construct the MCS bit mask for inclusion in an HT capabilities
2958 * information element.
2961 ieee80211_set_mcsset(struct ieee80211com *ic, uint8_t *frm)
2966 KASSERT((ic->ic_rxstream > 0 && ic->ic_rxstream <= 4),
2967 ("ic_rxstream %d out of range", ic->ic_rxstream));
2968 KASSERT((ic->ic_txstream > 0 && ic->ic_txstream <= 4),
2969 ("ic_txstream %d out of range", ic->ic_txstream));
2971 for (i = 0; i < ic->ic_rxstream * 8; i++)
2973 if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
2974 (ic->ic_htcaps & IEEE80211_HTC_RXMCS32))
2976 if (ic->ic_htcaps & IEEE80211_HTC_RXUNEQUAL) {
2977 if (ic->ic_rxstream >= 2) {
2978 for (i = 33; i <= 38; i++)
2981 if (ic->ic_rxstream >= 3) {
2982 for (i = 39; i <= 52; i++)
2985 if (ic->ic_txstream >= 4) {
2986 for (i = 53; i <= 76; i++)
2991 if (ic->ic_rxstream != ic->ic_txstream) {
2992 txparams = 0x1; /* TX MCS set defined */
2993 txparams |= 0x2; /* TX RX MCS not equal */
2994 txparams |= (ic->ic_txstream - 1) << 2; /* num TX streams */
2995 if (ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL)
2996 txparams |= 0x16; /* TX unequal modulation sup */
3003 * Add body of an HTCAP information element.
3006 ieee80211_add_htcap_body(uint8_t *frm, struct ieee80211_node *ni)
3008 #define ADDSHORT(frm, v) do { \
3009 frm[0] = (v) & 0xff; \
3010 frm[1] = (v) >> 8; \
3013 struct ieee80211com *ic = ni->ni_ic;
3014 struct ieee80211vap *vap = ni->ni_vap;
3015 uint16_t caps, extcaps;
3018 /* HT capabilities */
3019 caps = vap->iv_htcaps & 0xffff;
3021 * Note channel width depends on whether we are operating as
3022 * a sta or not. When operating as a sta we are generating
3023 * a request based on our desired configuration. Otherwise
3024 * we are operational and the channel attributes identify
3025 * how we've been setup (which might be different if a fixed
3026 * channel is specified).
3028 if (vap->iv_opmode == IEEE80211_M_STA) {
3029 /* override 20/40 use based on config */
3030 if (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)
3031 caps |= IEEE80211_HTCAP_CHWIDTH40;
3033 caps &= ~IEEE80211_HTCAP_CHWIDTH40;
3035 /* Start by using the advertised settings */
3036 rxmax = MS(ni->ni_htparam, IEEE80211_HTCAP_MAXRXAMPDU);
3037 density = MS(ni->ni_htparam, IEEE80211_HTCAP_MPDUDENSITY);
3039 IEEE80211_DPRINTF(vap, IEEE80211_MSG_11N,
3040 "%s: advertised rxmax=%d, density=%d, vap rxmax=%d, density=%d\n",
3044 vap->iv_ampdu_rxmax,
3045 vap->iv_ampdu_density);
3047 /* Cap at VAP rxmax */
3048 if (rxmax > vap->iv_ampdu_rxmax)
3049 rxmax = vap->iv_ampdu_rxmax;
3052 * If the VAP ampdu density value greater, use that.
3054 * (Larger density value == larger minimum gap between A-MPDU
3057 if (vap->iv_ampdu_density > density)
3058 density = vap->iv_ampdu_density;
3061 * NB: Hardware might support HT40 on some but not all
3062 * channels. We can't determine this earlier because only
3063 * after association the channel is upgraded to HT based
3064 * on the negotiated capabilities.
3066 if (ni->ni_chan != IEEE80211_CHAN_ANYC &&
3067 findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT40U) == NULL &&
3068 findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT40D) == NULL)
3069 caps &= ~IEEE80211_HTCAP_CHWIDTH40;
3071 /* override 20/40 use based on current channel */
3072 if (IEEE80211_IS_CHAN_HT40(ni->ni_chan))
3073 caps |= IEEE80211_HTCAP_CHWIDTH40;
3075 caps &= ~IEEE80211_HTCAP_CHWIDTH40;
3077 /* XXX TODO should it start by using advertised settings? */
3078 rxmax = vap->iv_ampdu_rxmax;
3079 density = vap->iv_ampdu_density;
3082 /* adjust short GI based on channel and config */
3083 if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20) == 0)
3084 caps &= ~IEEE80211_HTCAP_SHORTGI20;
3085 if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40) == 0 ||
3086 (caps & IEEE80211_HTCAP_CHWIDTH40) == 0)
3087 caps &= ~IEEE80211_HTCAP_SHORTGI40;
3089 /* adjust STBC based on receive capabilities */
3090 if ((vap->iv_flags_ht & IEEE80211_FHT_STBC_RX) == 0)
3091 caps &= ~IEEE80211_HTCAP_RXSTBC;
3093 /* adjust LDPC based on receive capabilites */
3094 if ((vap->iv_flags_ht & IEEE80211_FHT_LDPC_RX) == 0)
3095 caps &= ~IEEE80211_HTCAP_LDPC;
3097 ADDSHORT(frm, caps);
3100 *frm = SM(rxmax, IEEE80211_HTCAP_MAXRXAMPDU)
3101 | SM(density, IEEE80211_HTCAP_MPDUDENSITY)
3105 /* pre-zero remainder of ie */
3106 memset(frm, 0, sizeof(struct ieee80211_ie_htcap) -
3107 __offsetof(struct ieee80211_ie_htcap, hc_mcsset));
3109 /* supported MCS set */
3111 * XXX: For sta mode the rate set should be restricted based
3112 * on the AP's capabilities, but ni_htrates isn't setup when
3113 * we're called to form an AssocReq frame so for now we're
3114 * restricted to the device capabilities.
3116 ieee80211_set_mcsset(ni->ni_ic, frm);
3118 frm += __offsetof(struct ieee80211_ie_htcap, hc_extcap) -
3119 __offsetof(struct ieee80211_ie_htcap, hc_mcsset);
3121 /* HT extended capabilities */
3122 extcaps = vap->iv_htextcaps & 0xffff;
3124 ADDSHORT(frm, extcaps);
3126 frm += sizeof(struct ieee80211_ie_htcap) -
3127 __offsetof(struct ieee80211_ie_htcap, hc_txbf);
3134 * Add 802.11n HT capabilities information element
3137 ieee80211_add_htcap(uint8_t *frm, struct ieee80211_node *ni)
3139 frm[0] = IEEE80211_ELEMID_HTCAP;
3140 frm[1] = sizeof(struct ieee80211_ie_htcap) - 2;
3141 return ieee80211_add_htcap_body(frm + 2, ni);
3145 * Non-associated probe request - add HT capabilities based on
3146 * the current channel configuration.
3149 ieee80211_add_htcap_body_ch(uint8_t *frm, struct ieee80211vap *vap,
3150 struct ieee80211_channel *c)
3152 #define ADDSHORT(frm, v) do { \
3153 frm[0] = (v) & 0xff; \
3154 frm[1] = (v) >> 8; \
3157 struct ieee80211com *ic = vap->iv_ic;
3158 uint16_t caps, extcaps;
3161 /* HT capabilities */
3162 caps = vap->iv_htcaps & 0xffff;
3165 * We don't use this in STA mode; only in IBSS mode.
3166 * So in IBSS mode we base our HTCAP flags on the
3170 /* override 20/40 use based on current channel */
3171 if (IEEE80211_IS_CHAN_HT40(c))
3172 caps |= IEEE80211_HTCAP_CHWIDTH40;
3174 caps &= ~IEEE80211_HTCAP_CHWIDTH40;
3176 /* Use the currently configured values */
3177 rxmax = vap->iv_ampdu_rxmax;
3178 density = vap->iv_ampdu_density;
3180 /* adjust short GI based on channel and config */
3181 if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20) == 0)
3182 caps &= ~IEEE80211_HTCAP_SHORTGI20;
3183 if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40) == 0 ||
3184 (caps & IEEE80211_HTCAP_CHWIDTH40) == 0)
3185 caps &= ~IEEE80211_HTCAP_SHORTGI40;
3186 ADDSHORT(frm, caps);
3189 *frm = SM(rxmax, IEEE80211_HTCAP_MAXRXAMPDU)
3190 | SM(density, IEEE80211_HTCAP_MPDUDENSITY)
3194 /* pre-zero remainder of ie */
3195 memset(frm, 0, sizeof(struct ieee80211_ie_htcap) -
3196 __offsetof(struct ieee80211_ie_htcap, hc_mcsset));
3198 /* supported MCS set */
3200 * XXX: For sta mode the rate set should be restricted based
3201 * on the AP's capabilities, but ni_htrates isn't setup when
3202 * we're called to form an AssocReq frame so for now we're
3203 * restricted to the device capabilities.
3205 ieee80211_set_mcsset(ic, frm);
3207 frm += __offsetof(struct ieee80211_ie_htcap, hc_extcap) -
3208 __offsetof(struct ieee80211_ie_htcap, hc_mcsset);
3210 /* HT extended capabilities */
3211 extcaps = vap->iv_htextcaps & 0xffff;
3213 ADDSHORT(frm, extcaps);
3215 frm += sizeof(struct ieee80211_ie_htcap) -
3216 __offsetof(struct ieee80211_ie_htcap, hc_txbf);
3223 * Add 802.11n HT capabilities information element
3226 ieee80211_add_htcap_ch(uint8_t *frm, struct ieee80211vap *vap,
3227 struct ieee80211_channel *c)
3229 frm[0] = IEEE80211_ELEMID_HTCAP;
3230 frm[1] = sizeof(struct ieee80211_ie_htcap) - 2;
3231 return ieee80211_add_htcap_body_ch(frm + 2, vap, c);
3235 * Add Broadcom OUI wrapped standard HTCAP ie; this is
3236 * used for compatibility w/ pre-draft implementations.
3239 ieee80211_add_htcap_vendor(uint8_t *frm, struct ieee80211_node *ni)
3241 frm[0] = IEEE80211_ELEMID_VENDOR;
3242 frm[1] = 4 + sizeof(struct ieee80211_ie_htcap) - 2;
3243 frm[2] = (BCM_OUI >> 0) & 0xff;
3244 frm[3] = (BCM_OUI >> 8) & 0xff;
3245 frm[4] = (BCM_OUI >> 16) & 0xff;
3246 frm[5] = BCM_OUI_HTCAP;
3247 return ieee80211_add_htcap_body(frm + 6, ni);
3251 * Construct the MCS bit mask of basic rates
3252 * for inclusion in an HT information element.
3255 ieee80211_set_basic_htrates(uint8_t *frm, const struct ieee80211_htrateset *rs)
3259 for (i = 0; i < rs->rs_nrates; i++) {
3260 int r = rs->rs_rates[i] & IEEE80211_RATE_VAL;
3261 if ((rs->rs_rates[i] & IEEE80211_RATE_BASIC) &&
3262 r < IEEE80211_HTRATE_MAXSIZE) {
3263 /* NB: this assumes a particular implementation */
3270 * Update the HTINFO ie for a beacon frame.
3273 ieee80211_ht_update_beacon(struct ieee80211vap *vap,
3274 struct ieee80211_beacon_offsets *bo)
3276 #define PROTMODE (IEEE80211_HTINFO_OPMODE|IEEE80211_HTINFO_NONHT_PRESENT)
3277 struct ieee80211_node *ni;
3278 const struct ieee80211_channel *bsschan;
3279 struct ieee80211com *ic = vap->iv_ic;
3280 struct ieee80211_ie_htinfo *ht =
3281 (struct ieee80211_ie_htinfo *) bo->bo_htinfo;
3283 ni = ieee80211_ref_node(vap->iv_bss);
3284 bsschan = ni->ni_chan;
3286 /* XXX only update on channel change */
3287 ht->hi_ctrlchannel = ieee80211_chan2ieee(ic, bsschan);
3288 if (vap->iv_flags_ht & IEEE80211_FHT_RIFS)
3289 ht->hi_byte1 = IEEE80211_HTINFO_RIFSMODE_PERM;
3291 ht->hi_byte1 = IEEE80211_HTINFO_RIFSMODE_PROH;
3292 if (IEEE80211_IS_CHAN_HT40U(bsschan))
3293 ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_ABOVE;
3294 else if (IEEE80211_IS_CHAN_HT40D(bsschan))
3295 ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_BELOW;
3297 ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_NONE;
3298 if (IEEE80211_IS_CHAN_HT40(bsschan))
3299 ht->hi_byte1 |= IEEE80211_HTINFO_TXWIDTH_2040;
3301 /* protection mode */
3302 ht->hi_byte2 = (ht->hi_byte2 &~ PROTMODE) | ic->ic_curhtprotmode;
3304 ieee80211_free_node(ni);
3306 /* XXX propagate to vendor ie's */
3311 * Add body of an HTINFO information element.
3313 * NB: We don't use struct ieee80211_ie_htinfo because we can
3314 * be called to fillin both a standard ie and a compat ie that
3315 * has a vendor OUI at the front.
3318 ieee80211_add_htinfo_body(uint8_t *frm, struct ieee80211_node *ni)
3320 struct ieee80211vap *vap = ni->ni_vap;
3321 struct ieee80211com *ic = ni->ni_ic;
3323 /* pre-zero remainder of ie */
3324 memset(frm, 0, sizeof(struct ieee80211_ie_htinfo) - 2);
3326 /* primary/control channel center */
3327 *frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
3329 if (vap->iv_flags_ht & IEEE80211_FHT_RIFS)
3330 frm[0] = IEEE80211_HTINFO_RIFSMODE_PERM;
3332 frm[0] = IEEE80211_HTINFO_RIFSMODE_PROH;
3333 if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan))
3334 frm[0] |= IEEE80211_HTINFO_2NDCHAN_ABOVE;
3335 else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan))
3336 frm[0] |= IEEE80211_HTINFO_2NDCHAN_BELOW;
3338 frm[0] |= IEEE80211_HTINFO_2NDCHAN_NONE;
3339 if (IEEE80211_IS_CHAN_HT40(ni->ni_chan))
3340 frm[0] |= IEEE80211_HTINFO_TXWIDTH_2040;
3342 frm[1] = ic->ic_curhtprotmode;
3347 ieee80211_set_basic_htrates(frm, &ni->ni_htrates);
3348 frm += sizeof(struct ieee80211_ie_htinfo) -
3349 __offsetof(struct ieee80211_ie_htinfo, hi_basicmcsset);
3354 * Add 802.11n HT information element.
3357 ieee80211_add_htinfo(uint8_t *frm, struct ieee80211_node *ni)
3359 frm[0] = IEEE80211_ELEMID_HTINFO;
3360 frm[1] = sizeof(struct ieee80211_ie_htinfo) - 2;
3361 return ieee80211_add_htinfo_body(frm + 2, ni);
3365 * Add Broadcom OUI wrapped standard HTINFO ie; this is
3366 * used for compatibility w/ pre-draft implementations.
3369 ieee80211_add_htinfo_vendor(uint8_t *frm, struct ieee80211_node *ni)
3371 frm[0] = IEEE80211_ELEMID_VENDOR;
3372 frm[1] = 4 + sizeof(struct ieee80211_ie_htinfo) - 2;
3373 frm[2] = (BCM_OUI >> 0) & 0xff;
3374 frm[3] = (BCM_OUI >> 8) & 0xff;
3375 frm[4] = (BCM_OUI >> 16) & 0xff;
3376 frm[5] = BCM_OUI_HTINFO;
3377 return ieee80211_add_htinfo_body(frm + 6, ni);