2 * SPDX-License-Identifier: BSD-2-Clause
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.
29 * IEEE 802.11n protocol support.
35 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/systm.h>
39 #include <sys/endian.h>
41 #include <sys/socket.h>
44 #include <net/if_var.h>
45 #include <net/if_media.h>
46 #include <net/ethernet.h>
48 #include <net80211/ieee80211_var.h>
49 #include <net80211/ieee80211_action.h>
50 #include <net80211/ieee80211_input.h>
52 const struct ieee80211_mcs_rates ieee80211_htrates[IEEE80211_HTRATE_MAXSIZE] = {
53 { 13, 14, 27, 30 }, /* MCS 0 */
54 { 26, 29, 54, 60 }, /* MCS 1 */
55 { 39, 43, 81, 90 }, /* MCS 2 */
56 { 52, 58, 108, 120 }, /* MCS 3 */
57 { 78, 87, 162, 180 }, /* MCS 4 */
58 { 104, 116, 216, 240 }, /* MCS 5 */
59 { 117, 130, 243, 270 }, /* MCS 6 */
60 { 130, 144, 270, 300 }, /* MCS 7 */
61 { 26, 29, 54, 60 }, /* MCS 8 */
62 { 52, 58, 108, 120 }, /* MCS 9 */
63 { 78, 87, 162, 180 }, /* MCS 10 */
64 { 104, 116, 216, 240 }, /* MCS 11 */
65 { 156, 173, 324, 360 }, /* MCS 12 */
66 { 208, 231, 432, 480 }, /* MCS 13 */
67 { 234, 260, 486, 540 }, /* MCS 14 */
68 { 260, 289, 540, 600 }, /* MCS 15 */
69 { 39, 43, 81, 90 }, /* MCS 16 */
70 { 78, 87, 162, 180 }, /* MCS 17 */
71 { 117, 130, 243, 270 }, /* MCS 18 */
72 { 156, 173, 324, 360 }, /* MCS 19 */
73 { 234, 260, 486, 540 }, /* MCS 20 */
74 { 312, 347, 648, 720 }, /* MCS 21 */
75 { 351, 390, 729, 810 }, /* MCS 22 */
76 { 390, 433, 810, 900 }, /* MCS 23 */
77 { 52, 58, 108, 120 }, /* MCS 24 */
78 { 104, 116, 216, 240 }, /* MCS 25 */
79 { 156, 173, 324, 360 }, /* MCS 26 */
80 { 208, 231, 432, 480 }, /* MCS 27 */
81 { 312, 347, 648, 720 }, /* MCS 28 */
82 { 416, 462, 864, 960 }, /* MCS 29 */
83 { 468, 520, 972, 1080 }, /* MCS 30 */
84 { 520, 578, 1080, 1200 }, /* MCS 31 */
85 { 0, 0, 12, 13 }, /* MCS 32 */
86 { 78, 87, 162, 180 }, /* MCS 33 */
87 { 104, 116, 216, 240 }, /* MCS 34 */
88 { 130, 144, 270, 300 }, /* MCS 35 */
89 { 117, 130, 243, 270 }, /* MCS 36 */
90 { 156, 173, 324, 360 }, /* MCS 37 */
91 { 195, 217, 405, 450 }, /* MCS 38 */
92 { 104, 116, 216, 240 }, /* MCS 39 */
93 { 130, 144, 270, 300 }, /* MCS 40 */
94 { 130, 144, 270, 300 }, /* MCS 41 */
95 { 156, 173, 324, 360 }, /* MCS 42 */
96 { 182, 202, 378, 420 }, /* MCS 43 */
97 { 182, 202, 378, 420 }, /* MCS 44 */
98 { 208, 231, 432, 480 }, /* MCS 45 */
99 { 156, 173, 324, 360 }, /* MCS 46 */
100 { 195, 217, 405, 450 }, /* MCS 47 */
101 { 195, 217, 405, 450 }, /* MCS 48 */
102 { 234, 260, 486, 540 }, /* MCS 49 */
103 { 273, 303, 567, 630 }, /* MCS 50 */
104 { 273, 303, 567, 630 }, /* MCS 51 */
105 { 312, 347, 648, 720 }, /* MCS 52 */
106 { 130, 144, 270, 300 }, /* MCS 53 */
107 { 156, 173, 324, 360 }, /* MCS 54 */
108 { 182, 202, 378, 420 }, /* MCS 55 */
109 { 156, 173, 324, 360 }, /* MCS 56 */
110 { 182, 202, 378, 420 }, /* MCS 57 */
111 { 208, 231, 432, 480 }, /* MCS 58 */
112 { 234, 260, 486, 540 }, /* MCS 59 */
113 { 208, 231, 432, 480 }, /* MCS 60 */
114 { 234, 260, 486, 540 }, /* MCS 61 */
115 { 260, 289, 540, 600 }, /* MCS 62 */
116 { 260, 289, 540, 600 }, /* MCS 63 */
117 { 286, 318, 594, 660 }, /* MCS 64 */
118 { 195, 217, 405, 450 }, /* MCS 65 */
119 { 234, 260, 486, 540 }, /* MCS 66 */
120 { 273, 303, 567, 630 }, /* MCS 67 */
121 { 234, 260, 486, 540 }, /* MCS 68 */
122 { 273, 303, 567, 630 }, /* MCS 69 */
123 { 312, 347, 648, 720 }, /* MCS 70 */
124 { 351, 390, 729, 810 }, /* MCS 71 */
125 { 312, 347, 648, 720 }, /* MCS 72 */
126 { 351, 390, 729, 810 }, /* MCS 73 */
127 { 390, 433, 810, 900 }, /* MCS 74 */
128 { 390, 433, 810, 900 }, /* MCS 75 */
129 { 429, 477, 891, 990 }, /* MCS 76 */
132 static int ieee80211_ampdu_age = -1; /* threshold for ampdu reorder q (ms) */
133 SYSCTL_PROC(_net_wlan, OID_AUTO, ampdu_age,
134 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
135 &ieee80211_ampdu_age, 0, ieee80211_sysctl_msecs_ticks, "I",
136 "AMPDU max reorder age (ms)");
138 static int ieee80211_recv_bar_ena = 1;
139 SYSCTL_INT(_net_wlan, OID_AUTO, recv_bar, CTLFLAG_RW, &ieee80211_recv_bar_ena,
140 0, "BAR frame processing (ena/dis)");
142 static int ieee80211_addba_timeout = -1;/* timeout for ADDBA response */
143 SYSCTL_PROC(_net_wlan, OID_AUTO, addba_timeout,
144 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
145 &ieee80211_addba_timeout, 0, ieee80211_sysctl_msecs_ticks, "I",
146 "ADDBA request timeout (ms)");
147 static int ieee80211_addba_backoff = -1;/* backoff after max ADDBA requests */
148 SYSCTL_PROC(_net_wlan, OID_AUTO, addba_backoff,
149 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
150 &ieee80211_addba_backoff, 0, ieee80211_sysctl_msecs_ticks, "I",
151 "ADDBA request backoff (ms)");
152 static int ieee80211_addba_maxtries = 3;/* max ADDBA requests before backoff */
153 SYSCTL_INT(_net_wlan, OID_AUTO, addba_maxtries, CTLFLAG_RW,
154 &ieee80211_addba_maxtries, 0, "max ADDBA requests sent before backoff");
156 static int ieee80211_bar_timeout = -1; /* timeout waiting for BAR response */
157 static int ieee80211_bar_maxtries = 50;/* max BAR requests before DELBA */
159 static ieee80211_recv_action_func ht_recv_action_ba_addba_request;
160 static ieee80211_recv_action_func ht_recv_action_ba_addba_response;
161 static ieee80211_recv_action_func ht_recv_action_ba_delba;
162 static ieee80211_recv_action_func ht_recv_action_ht_mimopwrsave;
163 static ieee80211_recv_action_func ht_recv_action_ht_txchwidth;
165 static ieee80211_send_action_func ht_send_action_ba_addba;
166 static ieee80211_send_action_func ht_send_action_ba_delba;
167 static ieee80211_send_action_func ht_send_action_ht_txchwidth;
170 ieee80211_ht_init(void)
173 * Setup HT parameters that depends on the clock frequency.
175 ieee80211_ampdu_age = msecs_to_ticks(500);
176 ieee80211_addba_timeout = msecs_to_ticks(250);
177 ieee80211_addba_backoff = msecs_to_ticks(10*1000);
178 ieee80211_bar_timeout = msecs_to_ticks(250);
180 * Register action frame handlers.
182 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_BA,
183 IEEE80211_ACTION_BA_ADDBA_REQUEST, ht_recv_action_ba_addba_request);
184 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_BA,
185 IEEE80211_ACTION_BA_ADDBA_RESPONSE, ht_recv_action_ba_addba_response);
186 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_BA,
187 IEEE80211_ACTION_BA_DELBA, ht_recv_action_ba_delba);
188 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_HT,
189 IEEE80211_ACTION_HT_MIMOPWRSAVE, ht_recv_action_ht_mimopwrsave);
190 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_HT,
191 IEEE80211_ACTION_HT_TXCHWIDTH, ht_recv_action_ht_txchwidth);
193 ieee80211_send_action_register(IEEE80211_ACTION_CAT_BA,
194 IEEE80211_ACTION_BA_ADDBA_REQUEST, ht_send_action_ba_addba);
195 ieee80211_send_action_register(IEEE80211_ACTION_CAT_BA,
196 IEEE80211_ACTION_BA_ADDBA_RESPONSE, ht_send_action_ba_addba);
197 ieee80211_send_action_register(IEEE80211_ACTION_CAT_BA,
198 IEEE80211_ACTION_BA_DELBA, ht_send_action_ba_delba);
199 ieee80211_send_action_register(IEEE80211_ACTION_CAT_HT,
200 IEEE80211_ACTION_HT_TXCHWIDTH, ht_send_action_ht_txchwidth);
202 SYSINIT(wlan_ht, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_ht_init, NULL);
204 static int ieee80211_ampdu_enable(struct ieee80211_node *ni,
205 struct ieee80211_tx_ampdu *tap);
206 static int ieee80211_addba_request(struct ieee80211_node *ni,
207 struct ieee80211_tx_ampdu *tap,
208 int dialogtoken, int baparamset, int batimeout);
209 static int ieee80211_addba_response(struct ieee80211_node *ni,
210 struct ieee80211_tx_ampdu *tap,
211 int code, int baparamset, int batimeout);
212 static void ieee80211_addba_stop(struct ieee80211_node *ni,
213 struct ieee80211_tx_ampdu *tap);
214 static void null_addba_response_timeout(struct ieee80211_node *ni,
215 struct ieee80211_tx_ampdu *tap);
217 static void ieee80211_bar_response(struct ieee80211_node *ni,
218 struct ieee80211_tx_ampdu *tap, int status);
219 static void ampdu_tx_stop(struct ieee80211_tx_ampdu *tap);
220 static void bar_stop_timer(struct ieee80211_tx_ampdu *tap);
221 static int ampdu_rx_start(struct ieee80211_node *, struct ieee80211_rx_ampdu *,
222 int baparamset, int batimeout, int baseqctl);
223 static void ampdu_rx_stop(struct ieee80211_node *, struct ieee80211_rx_ampdu *);
226 ieee80211_ht_attach(struct ieee80211com *ic)
228 /* setup default aggregation policy */
229 ic->ic_recv_action = ieee80211_recv_action;
230 ic->ic_send_action = ieee80211_send_action;
231 ic->ic_ampdu_enable = ieee80211_ampdu_enable;
232 ic->ic_addba_request = ieee80211_addba_request;
233 ic->ic_addba_response = ieee80211_addba_response;
234 ic->ic_addba_response_timeout = null_addba_response_timeout;
235 ic->ic_addba_stop = ieee80211_addba_stop;
236 ic->ic_bar_response = ieee80211_bar_response;
237 ic->ic_ampdu_rx_start = ampdu_rx_start;
238 ic->ic_ampdu_rx_stop = ampdu_rx_stop;
240 ic->ic_htprotmode = IEEE80211_PROT_RTSCTS;
241 ic->ic_curhtprotmode = IEEE80211_HTINFO_OPMODE_PURE;
245 ieee80211_ht_detach(struct ieee80211com *ic)
250 ieee80211_ht_vattach(struct ieee80211vap *vap)
253 /* driver can override defaults */
254 vap->iv_ampdu_rxmax = IEEE80211_HTCAP_MAXRXAMPDU_8K;
255 vap->iv_ampdu_density = IEEE80211_HTCAP_MPDUDENSITY_NA;
256 vap->iv_ampdu_limit = vap->iv_ampdu_rxmax;
257 vap->iv_amsdu_limit = vap->iv_htcaps & IEEE80211_HTCAP_MAXAMSDU;
258 /* tx aggregation traffic thresholds */
259 vap->iv_ampdu_mintraffic[WME_AC_BK] = 128;
260 vap->iv_ampdu_mintraffic[WME_AC_BE] = 64;
261 vap->iv_ampdu_mintraffic[WME_AC_VO] = 32;
262 vap->iv_ampdu_mintraffic[WME_AC_VI] = 32;
264 vap->iv_htprotmode = IEEE80211_PROT_RTSCTS;
265 vap->iv_curhtprotmode = IEEE80211_HTINFO_OPMODE_PURE;
267 if (vap->iv_htcaps & IEEE80211_HTC_HT) {
269 * Device is HT capable; enable all HT-related
270 * facilities by default.
271 * XXX these choices may be too aggressive.
273 vap->iv_flags_ht |= IEEE80211_FHT_HT
274 | IEEE80211_FHT_HTCOMPAT
276 if (vap->iv_htcaps & IEEE80211_HTCAP_SHORTGI20)
277 vap->iv_flags_ht |= IEEE80211_FHT_SHORTGI20;
278 /* XXX infer from channel list? */
279 if (vap->iv_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
280 vap->iv_flags_ht |= IEEE80211_FHT_USEHT40;
281 if (vap->iv_htcaps & IEEE80211_HTCAP_SHORTGI40)
282 vap->iv_flags_ht |= IEEE80211_FHT_SHORTGI40;
284 /* enable RIFS if capable */
285 if (vap->iv_htcaps & IEEE80211_HTC_RIFS)
286 vap->iv_flags_ht |= IEEE80211_FHT_RIFS;
288 /* NB: A-MPDU and A-MSDU rx are mandated, these are tx only */
289 vap->iv_flags_ht |= IEEE80211_FHT_AMPDU_RX;
290 if (vap->iv_htcaps & IEEE80211_HTC_AMPDU)
291 vap->iv_flags_ht |= IEEE80211_FHT_AMPDU_TX;
292 vap->iv_flags_ht |= IEEE80211_FHT_AMSDU_RX;
293 if (vap->iv_htcaps & IEEE80211_HTC_AMSDU)
294 vap->iv_flags_ht |= IEEE80211_FHT_AMSDU_TX;
296 if (vap->iv_htcaps & IEEE80211_HTCAP_TXSTBC)
297 vap->iv_flags_ht |= IEEE80211_FHT_STBC_TX;
298 if (vap->iv_htcaps & IEEE80211_HTCAP_RXSTBC)
299 vap->iv_flags_ht |= IEEE80211_FHT_STBC_RX;
301 if (vap->iv_htcaps & IEEE80211_HTCAP_LDPC)
302 vap->iv_flags_ht |= IEEE80211_FHT_LDPC_RX;
303 if (vap->iv_htcaps & IEEE80211_HTC_TXLDPC)
304 vap->iv_flags_ht |= IEEE80211_FHT_LDPC_TX;
306 /* NB: disable default legacy WDS, too many issues right now */
307 if (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY)
308 vap->iv_flags_ht &= ~IEEE80211_FHT_HT;
312 ieee80211_ht_vdetach(struct ieee80211vap *vap)
317 ht_getrate(struct ieee80211com *ic, int index, enum ieee80211_phymode mode,
322 mword = ieee80211_rate2media(ic, index | IEEE80211_RATE_MCS, mode);
323 if (IFM_SUBTYPE(mword) != IFM_IEEE80211_MCS)
327 rate = ieee80211_htrates[index].ht20_rate_800ns;
330 rate = ieee80211_htrates[index].ht20_rate_400ns;
333 rate = ieee80211_htrates[index].ht40_rate_800ns;
336 rate = ieee80211_htrates[index].ht40_rate_400ns;
342 static struct printranges {
353 { 32, 0, 1, 2, IEEE80211_HTC_TXMCS32 },
354 { 33, 38, 2, 0, IEEE80211_HTC_TXUNEQUAL },
355 { 39, 52, 3, 0, IEEE80211_HTC_TXUNEQUAL },
356 { 53, 76, 4, 0, IEEE80211_HTC_TXUNEQUAL },
361 ht_rateprint(struct ieee80211com *ic, enum ieee80211_phymode mode, int ratetype)
363 int minrate, maxrate;
364 struct printranges *range;
366 for (range = ranges; range->txstream != 0; range++) {
367 if (ic->ic_txstream < range->txstream)
369 if (range->htcapflags &&
370 (ic->ic_htcaps & range->htcapflags) == 0)
372 if (ratetype < range->ratetype)
374 minrate = ht_getrate(ic, range->minmcs, mode, ratetype);
375 maxrate = ht_getrate(ic, range->maxmcs, mode, ratetype);
377 ic_printf(ic, "MCS %d-%d: %d%sMbps - %d%sMbps\n",
378 range->minmcs, range->maxmcs,
379 minrate/2, ((minrate & 0x1) != 0 ? ".5" : ""),
380 maxrate/2, ((maxrate & 0x1) != 0 ? ".5" : ""));
382 ic_printf(ic, "MCS %d: %d%sMbps\n", range->minmcs,
383 minrate/2, ((minrate & 0x1) != 0 ? ".5" : ""));
389 ht_announce(struct ieee80211com *ic, enum ieee80211_phymode mode)
391 const char *modestr = ieee80211_phymode_name[mode];
393 ic_printf(ic, "%s MCS 20MHz\n", modestr);
394 ht_rateprint(ic, mode, 0);
395 if (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20) {
396 ic_printf(ic, "%s MCS 20MHz SGI\n", modestr);
397 ht_rateprint(ic, mode, 1);
399 if (ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
400 ic_printf(ic, "%s MCS 40MHz:\n", modestr);
401 ht_rateprint(ic, mode, 2);
403 if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
404 (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40)) {
405 ic_printf(ic, "%s MCS 40MHz SGI:\n", modestr);
406 ht_rateprint(ic, mode, 3);
411 ieee80211_ht_announce(struct ieee80211com *ic)
414 if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) ||
415 isset(ic->ic_modecaps, IEEE80211_MODE_11NG))
416 ic_printf(ic, "%dT%dR\n", ic->ic_txstream, ic->ic_rxstream);
417 if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA))
418 ht_announce(ic, IEEE80211_MODE_11NA);
419 if (isset(ic->ic_modecaps, IEEE80211_MODE_11NG))
420 ht_announce(ic, IEEE80211_MODE_11NG);
424 ieee80211_init_suphtrates(struct ieee80211com *ic)
426 #define ADDRATE(x) do { \
427 htrateset->rs_rates[htrateset->rs_nrates] = x; \
428 htrateset->rs_nrates++; \
430 struct ieee80211_htrateset *htrateset = &ic->ic_sup_htrates;
433 memset(htrateset, 0, sizeof(struct ieee80211_htrateset));
434 for (i = 0; i < ic->ic_txstream * 8; i++)
436 if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
437 (ic->ic_htcaps & IEEE80211_HTC_TXMCS32))
439 if (ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL) {
440 if (ic->ic_txstream >= 2) {
441 for (i = 33; i <= 38; i++)
444 if (ic->ic_txstream >= 3) {
445 for (i = 39; i <= 52; i++)
448 if (ic->ic_txstream == 4) {
449 for (i = 53; i <= 76; i++)
457 * Receive processing.
461 * Decap the encapsulated A-MSDU frames and dispatch all but
462 * the last for delivery. The last frame is returned for
463 * delivery via the normal path.
466 ieee80211_decap_amsdu(struct ieee80211_node *ni, struct mbuf *m)
468 struct ieee80211vap *vap = ni->ni_vap;
472 /* discard 802.3 header inserted by ieee80211_decap */
473 m_adj(m, sizeof(struct ether_header));
475 vap->iv_stats.is_amsdu_decap++;
479 * Decap the first frame, bust it apart from the
480 * remainder and deliver. We leave the last frame
481 * delivery to the caller (for consistency with other
482 * code paths, could also do it here).
484 m = ieee80211_decap1(m, &framelen);
486 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
487 ni->ni_macaddr, "a-msdu", "%s", "decap failed");
488 vap->iv_stats.is_amsdu_tooshort++;
491 if (m->m_pkthdr.len == framelen)
493 n = m_split(m, framelen, IEEE80211_M_NOWAIT);
495 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
496 ni->ni_macaddr, "a-msdu",
497 "%s", "unable to split encapsulated frames");
498 vap->iv_stats.is_amsdu_split++;
499 m_freem(m); /* NB: must reclaim */
502 vap->iv_deliver_data(vap, ni, m);
505 * Remove frame contents; each intermediate frame
506 * is required to be aligned to a 4-byte boundary.
509 m_adj(m, roundup2(framelen, 4) - framelen); /* padding */
511 return m; /* last delivered by caller */
515 ampdu_rx_purge_slot(struct ieee80211_rx_ampdu *rap, int i)
519 /* Walk the queue, removing frames as appropriate */
521 m = mbufq_dequeue(&rap->rxa_mq[i]);
524 rap->rxa_qbytes -= m->m_pkthdr.len;
531 * Add the given frame to the current RX reorder slot.
533 * For future offloaded A-MSDU handling where multiple frames with
534 * the same sequence number show up here, this routine will append
535 * those frames as long as they're appropriately tagged.
538 ampdu_rx_add_slot(struct ieee80211_rx_ampdu *rap, int off, int tid,
540 struct ieee80211_node *ni,
542 const struct ieee80211_rx_stats *rxs)
544 const struct ieee80211_rx_stats *rxs_final = NULL;
545 struct ieee80211vap *vap = ni->ni_vap;
547 #define PROCESS 0 /* caller should process frame */
548 #define CONSUMED 1 /* frame consumed, caller does nothing */
551 * Figure out if this is a duplicate frame for the given slot.
553 * We're assuming that the driver will hand us all the frames
554 * for a given AMSDU decap pass and if we get /a/ frame
555 * for an AMSDU decap then we'll get all of them.
557 * The tricksy bit is that we don't know when the /end/ of
558 * the decap pass is, because we aren't tracking state here
559 * per-slot to know that we've finished receiving the frame list.
561 * The driver sets RX_F_AMSDU and RX_F_AMSDU_MORE to tell us
562 * what's going on; so ideally we'd just check the frame at the
563 * end of the reassembly slot to see if its F_AMSDU w/ no F_AMSDU_MORE -
564 * that means we've received the whole AMSDU decap pass.
568 * Get the rxs of the final mbuf in the slot, if one exists.
570 if (!mbufq_empty(&rap->rxa_mq[off])) {
571 rxs_final = ieee80211_get_rx_params_ptr(mbufq_last(&rap->rxa_mq[off]));
574 /* Default to tossing the duplicate frame */
578 * Check to see if the final frame has F_AMSDU and F_AMSDU set, AND
579 * this frame has F_AMSDU set (MORE or otherwise.) That's a sign
580 * that more can come.
583 if ((rxs != NULL) && (rxs_final != NULL) &&
584 ieee80211_check_rxseq_amsdu(rxs) &&
585 ieee80211_check_rxseq_amsdu(rxs_final)) {
586 if (! ieee80211_check_rxseq_amsdu_more(rxs_final)) {
588 * amsdu_more() returning 0 means "it's not the
589 * final frame" so we can append more
597 * If the list is empty OR we have determined we can put more
598 * driver decap'ed AMSDU frames in here, then insert.
600 if (mbufq_empty(&rap->rxa_mq[off]) || (toss_dup == 0)) {
601 if (mbufq_enqueue(&rap->rxa_mq[off], m) != 0) {
602 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT | IEEE80211_MSG_11N,
605 "seqno %u tid %u BA win <%u:%u> off=%d, qlen=%d, maxqlen=%d",
606 rxseq, tid, rap->rxa_start,
607 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
609 mbufq_len(&rap->rxa_mq[off]),
610 rap->rxa_mq[off].mq_maxlen);
611 /* XXX error count */
616 rap->rxa_qbytes += m->m_pkthdr.len;
617 vap->iv_stats.is_ampdu_rx_reorder++;
619 * Statistics for AMSDU decap.
621 if (rxs != NULL && ieee80211_check_rxseq_amsdu(rxs)) {
622 if (ieee80211_check_rxseq_amsdu_more(rxs)) {
623 /* more=1, AMSDU, end of batch */
624 IEEE80211_NODE_STAT(ni, rx_amsdu_more_end);
626 IEEE80211_NODE_STAT(ni, rx_amsdu_more);
630 IEEE80211_DISCARD_MAC(vap,
631 IEEE80211_MSG_INPUT | IEEE80211_MSG_11N,
632 ni->ni_macaddr, "a-mpdu duplicate",
633 "seqno %u tid %u BA win <%u:%u>",
634 rxseq, tid, rap->rxa_start,
635 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1));
637 IEEE80211_DISCARD_MAC(vap,
638 IEEE80211_MSG_INPUT | IEEE80211_MSG_11N,
639 ni->ni_macaddr, "a-mpdu duplicate",
640 "seqno %d tid %u pktflags 0x%08x\n",
641 rxseq, tid, rxs->c_pktflags);
643 if (rxs_final != NULL) {
644 IEEE80211_DISCARD_MAC(vap,
645 IEEE80211_MSG_INPUT | IEEE80211_MSG_11N,
646 ni->ni_macaddr, "a-mpdu duplicate",
647 "final: pktflags 0x%08x\n",
648 rxs_final->c_pktflags);
650 vap->iv_stats.is_rx_dup++;
651 IEEE80211_NODE_STAT(ni, rx_dup);
660 * Purge all frames in the A-MPDU re-order queue.
663 ampdu_rx_purge(struct ieee80211_rx_ampdu *rap)
667 for (i = 0; i < rap->rxa_wnd; i++) {
668 ampdu_rx_purge_slot(rap, i);
669 if (rap->rxa_qframes == 0)
672 KASSERT(rap->rxa_qbytes == 0 && rap->rxa_qframes == 0,
673 ("lost %u data, %u frames on ampdu rx q",
674 rap->rxa_qbytes, rap->rxa_qframes));
678 ieee80211_ampdu_rx_init_rap(struct ieee80211_node *ni,
679 struct ieee80211_rx_ampdu *rap)
683 /* XXX TODO: ensure the queues are empty */
684 memset(rap, 0, sizeof(*rap));
685 for (i = 0; i < IEEE80211_AGGR_BAWMAX; i++)
686 mbufq_init(&rap->rxa_mq[i], 256);
690 * Start A-MPDU rx/re-order processing for the specified TID.
693 ampdu_rx_start(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap,
694 int baparamset, int batimeout, int baseqctl)
696 struct ieee80211vap *vap = ni->ni_vap;
697 int bufsiz = _IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_BUFSIZ);
699 if (rap->rxa_flags & IEEE80211_AGGR_RUNNING) {
701 * AMPDU previously setup and not terminated with a DELBA,
702 * flush the reorder q's in case anything remains.
706 ieee80211_ampdu_rx_init_rap(ni, rap);
707 rap->rxa_wnd = (bufsiz == 0) ?
708 IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
709 rap->rxa_start = _IEEE80211_MASKSHIFT(baseqctl, IEEE80211_BASEQ_START);
710 rap->rxa_flags |= IEEE80211_AGGR_RUNNING | IEEE80211_AGGR_XCHGPEND;
712 /* XXX this should be a configuration flag */
713 if ((vap->iv_htcaps & IEEE80211_HTC_RX_AMSDU_AMPDU) &&
714 (_IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_AMSDU)))
715 rap->rxa_flags |= IEEE80211_AGGR_AMSDU;
717 rap->rxa_flags &= ~IEEE80211_AGGR_AMSDU;
723 * Public function; manually setup the RX ampdu state.
726 ieee80211_ampdu_rx_start_ext(struct ieee80211_node *ni, int tid, int seq, int baw)
728 struct ieee80211_rx_ampdu *rap;
730 /* XXX TODO: sanity check tid, seq, baw */
732 rap = &ni->ni_rx_ampdu[tid];
734 if (rap->rxa_flags & IEEE80211_AGGR_RUNNING) {
736 * AMPDU previously setup and not terminated with a DELBA,
737 * flush the reorder q's in case anything remains.
742 ieee80211_ampdu_rx_init_rap(ni, rap);
744 rap->rxa_wnd = (baw== 0) ?
745 IEEE80211_AGGR_BAWMAX : min(baw, IEEE80211_AGGR_BAWMAX);
747 /* Wait for the first RX frame, use that as BAW */
749 rap->rxa_flags |= IEEE80211_AGGR_WAITRX;
751 rap->rxa_start = seq;
753 rap->rxa_flags |= IEEE80211_AGGR_RUNNING | IEEE80211_AGGR_XCHGPEND;
755 /* XXX TODO: no amsdu flag */
757 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, ni,
758 "%s: tid=%d, start=%d, wnd=%d, flags=0x%08x",
769 * Public function; manually stop the RX AMPDU state.
772 ieee80211_ampdu_rx_stop_ext(struct ieee80211_node *ni, int tid)
774 struct ieee80211_rx_ampdu *rap;
776 /* XXX TODO: sanity check tid, seq, baw */
777 rap = &ni->ni_rx_ampdu[tid];
778 ampdu_rx_stop(ni, rap);
782 * Stop A-MPDU rx processing for the specified TID.
785 ampdu_rx_stop(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
789 rap->rxa_flags &= ~(IEEE80211_AGGR_RUNNING
790 | IEEE80211_AGGR_XCHGPEND
791 | IEEE80211_AGGR_WAITRX);
795 * Dispatch a frame from the A-MPDU reorder queue. The
796 * frame is fed back into ieee80211_input marked with an
797 * M_AMPDU_MPDU flag so it doesn't come back to us (it also
798 * permits ieee80211_input to optimize re-processing).
801 ampdu_dispatch(struct ieee80211_node *ni, struct mbuf *m)
803 m->m_flags |= M_AMPDU_MPDU; /* bypass normal processing */
804 /* NB: rssi and noise are ignored w/ M_AMPDU_MPDU set */
805 (void) ieee80211_input(ni, m, 0, 0);
809 ampdu_dispatch_slot(struct ieee80211_rx_ampdu *rap, struct ieee80211_node *ni,
816 m = mbufq_dequeue(&rap->rxa_mq[i]);
821 rap->rxa_qbytes -= m->m_pkthdr.len;
824 ampdu_dispatch(ni, m);
830 ampdu_rx_moveup(struct ieee80211_rx_ampdu *rap, struct ieee80211_node *ni,
833 struct ieee80211vap *vap = ni->ni_vap;
836 * If frames remain, copy the mbuf pointers down so
837 * they correspond to the offsets in the new window.
839 if (rap->rxa_qframes != 0) {
840 int n = rap->rxa_qframes, j;
841 for (j = i+1; j < rap->rxa_wnd; j++) {
843 * Concat the list contents over, which will
844 * blank the source list for us.
846 if (mbufq_len(&rap->rxa_mq[j]) != 0) {
847 n = n - mbufq_len(&rap->rxa_mq[j]);
848 mbufq_concat(&rap->rxa_mq[j-i], &rap->rxa_mq[j]);
849 KASSERT(n >= 0, ("%s: n < 0 (%d)", __func__, n));
854 KASSERT(n == 0, ("%s: lost %d frames, qframes %d off %d "
855 "BA win <%d:%d> winstart %d",
856 __func__, n, rap->rxa_qframes, i, rap->rxa_start,
857 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
859 vap->iv_stats.is_ampdu_rx_copy += rap->rxa_qframes;
864 * Dispatch as many frames as possible from the re-order queue.
865 * Frames will always be "at the front"; we process all frames
866 * up to the first empty slot in the window. On completion we
867 * cleanup state if there are still pending frames in the current
868 * BA window. We assume the frame at slot 0 is already handled
869 * by the caller; we always start at slot 1.
872 ampdu_rx_dispatch(struct ieee80211_rx_ampdu *rap, struct ieee80211_node *ni)
874 struct ieee80211vap *vap = ni->ni_vap;
877 /* flush run of frames */
879 for (i = 1; i < rap->rxa_wnd; i++) {
880 r = ampdu_dispatch_slot(rap, ni, i);
887 ampdu_rx_moveup(rap, ni, i, -1);
890 * Adjust the start of the BA window to
891 * reflect the frames just dispatched.
893 rap->rxa_start = IEEE80211_SEQ_ADD(rap->rxa_start, i);
894 vap->iv_stats.is_ampdu_rx_oor += r2;
896 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, ni,
897 "%s: moved slot up %d slots to start at %d (%d frames)",
905 * Dispatch all frames in the A-MPDU re-order queue.
908 ampdu_rx_flush(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
912 for (i = 0; i < rap->rxa_wnd; i++) {
913 r = ampdu_dispatch_slot(rap, ni, i);
916 ni->ni_vap->iv_stats.is_ampdu_rx_oor += r;
918 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, ni,
919 "%s: moved slot up %d slots to start at %d (%d frames)",
925 if (rap->rxa_qframes == 0)
931 * Dispatch all frames in the A-MPDU re-order queue
932 * preceding the specified sequence number. This logic
933 * handles window moves due to a received MSDU or BAR.
936 ampdu_rx_flush_upto(struct ieee80211_node *ni,
937 struct ieee80211_rx_ampdu *rap, ieee80211_seq winstart)
939 struct ieee80211vap *vap = ni->ni_vap;
944 * Flush any complete MSDU's with a sequence number lower
945 * than winstart. Gaps may exist. Note that we may actually
946 * dispatch frames past winstart if a run continues; this is
947 * an optimization that avoids having to do a separate pass
948 * to dispatch frames after moving the BA window start.
950 seqno = rap->rxa_start;
951 for (i = 0; i < rap->rxa_wnd; i++) {
952 if ((r = mbufq_len(&rap->rxa_mq[i])) != 0) {
953 (void) ampdu_dispatch_slot(rap, ni, i);
955 if (!IEEE80211_SEQ_BA_BEFORE(seqno, winstart))
958 vap->iv_stats.is_ampdu_rx_oor += r;
959 seqno = IEEE80211_SEQ_INC(seqno);
961 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, ni,
962 "%s: moved slot up %d slots to start at %d (%d frames)",
970 * If frames remain, copy the mbuf pointers down so
971 * they correspond to the offsets in the new window.
973 ampdu_rx_moveup(rap, ni, i, winstart);
976 * Move the start of the BA window; we use the
977 * sequence number of the last MSDU that was
978 * passed up the stack+1 or winstart if stopped on
979 * a gap in the reorder buffer.
981 rap->rxa_start = seqno;
985 * Process a received QoS data frame for an HT station. Handle
986 * A-MPDU reordering: if this frame is received out of order
987 * and falls within the BA window hold onto it. Otherwise if
988 * this frame completes a run, flush any pending frames. We
989 * return 1 if the frame is consumed. A 0 is returned if
990 * the frame should be processed normally by the caller.
992 * A-MSDU: handle hardware decap'ed A-MSDU frames that are
993 * pretending to be MPDU's. They're dispatched directly if
994 * able; or attempted to put into the receive reordering slot.
997 ieee80211_ampdu_reorder(struct ieee80211_node *ni, struct mbuf *m,
998 const struct ieee80211_rx_stats *rxs)
1000 #define PROCESS 0 /* caller should process frame */
1001 #define CONSUMED 1 /* frame consumed, caller does nothing */
1002 struct ieee80211vap *vap = ni->ni_vap;
1003 struct ieee80211_qosframe *wh;
1004 struct ieee80211_rx_ampdu *rap;
1005 ieee80211_seq rxseq;
1008 int amsdu = ieee80211_check_rxseq_amsdu(rxs);
1009 int amsdu_end = ieee80211_check_rxseq_amsdu_more(rxs);
1011 KASSERT((m->m_flags & (M_AMPDU | M_AMPDU_MPDU)) == M_AMPDU,
1012 ("!a-mpdu or already re-ordered, flags 0x%x", m->m_flags));
1013 KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT sta"));
1015 /* NB: m_len known to be sufficient */
1016 wh = mtod(m, struct ieee80211_qosframe *);
1017 if (wh->i_fc[0] != IEEE80211_FC0_QOSDATA) {
1019 * Not QoS data, shouldn't get here but just
1020 * return it to the caller for processing.
1026 * 802.11-2012 9.3.2.10 - Duplicate detection and recovery.
1028 * Multicast QoS data frames are checked against a different
1029 * counter, not the per-TID counter.
1031 if (IEEE80211_IS_MULTICAST(wh->i_addr1))
1034 tid = ieee80211_getqos(wh)[0];
1035 tid &= IEEE80211_QOS_TID;
1036 rap = &ni->ni_rx_ampdu[tid];
1037 if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
1039 * No ADDBA request yet, don't touch.
1043 rxseq = le16toh(*(uint16_t *)wh->i_seq);
1044 if ((rxseq & IEEE80211_SEQ_FRAG_MASK) != 0) {
1046 * Fragments are not allowed; toss.
1048 IEEE80211_DISCARD_MAC(vap,
1049 IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
1050 "A-MPDU", "fragment, rxseq 0x%x tid %u%s", rxseq, tid,
1051 wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
1052 vap->iv_stats.is_ampdu_rx_drop++;
1053 IEEE80211_NODE_STAT(ni, rx_drop);
1057 rxseq >>= IEEE80211_SEQ_SEQ_SHIFT;
1061 * Handle waiting for the first frame to define the BAW.
1062 * Some firmware doesn't provide the RX of the starting point
1063 * of the BAW and we have to cope.
1065 if (rap->rxa_flags & IEEE80211_AGGR_WAITRX) {
1066 rap->rxa_flags &= ~IEEE80211_AGGR_WAITRX;
1067 rap->rxa_start = rxseq;
1070 if (rxseq == rap->rxa_start) {
1072 * First frame in window.
1074 if (rap->rxa_qframes != 0) {
1076 * Dispatch as many packets as we can.
1078 KASSERT(mbufq_empty(&rap->rxa_mq[0]), ("unexpected dup"));
1079 ampdu_dispatch(ni, m);
1080 ampdu_rx_dispatch(rap, ni);
1084 * In order; advance window if needed and notify
1085 * caller to dispatch directly.
1089 rap->rxa_start = IEEE80211_SEQ_INC(rxseq);
1090 IEEE80211_NODE_STAT(ni, rx_amsdu_more_end);
1092 IEEE80211_NODE_STAT(ni, rx_amsdu_more);
1095 rap->rxa_start = IEEE80211_SEQ_INC(rxseq);
1101 * Frame is out of order; store if in the BA window.
1103 /* calculate offset in BA window */
1104 off = IEEE80211_SEQ_SUB(rxseq, rap->rxa_start);
1105 if (off < rap->rxa_wnd) {
1107 * Common case (hopefully): in the BA window.
1108 * Sec 9.10.7.6.2 a) (p.137)
1112 * Check for frames sitting too long in the reorder queue.
1113 * This should only ever happen if frames are not delivered
1114 * without the sender otherwise notifying us (e.g. with a
1115 * BAR to move the window). Typically this happens because
1116 * of vendor bugs that cause the sequence number to jump.
1117 * When this happens we get a gap in the reorder queue that
1118 * leaves frame sitting on the queue until they get pushed
1119 * out due to window moves. When the vendor does not send
1120 * BAR this move only happens due to explicit packet sends
1122 * NB: we only track the time of the oldest frame in the
1123 * reorder q; this means that if we flush we might push
1124 * frames that still "new"; if this happens then subsequent
1125 * frames will result in BA window moves which cost something
1126 * but is still better than a big throughput dip.
1128 if (rap->rxa_qframes != 0) {
1129 /* XXX honor batimeout? */
1130 if (ticks - rap->rxa_age > ieee80211_ampdu_age) {
1132 * Too long since we received the first
1133 * frame; flush the reorder buffer.
1135 if (rap->rxa_qframes != 0) {
1136 vap->iv_stats.is_ampdu_rx_age +=
1138 ampdu_rx_flush(ni, rap);
1141 * Advance the window if needed and notify
1142 * the caller to dispatch directly.
1147 IEEE80211_SEQ_INC(rxseq);
1148 IEEE80211_NODE_STAT(ni,
1151 IEEE80211_NODE_STAT(ni,
1156 IEEE80211_SEQ_INC(rxseq);
1162 * First frame, start aging timer.
1164 rap->rxa_age = ticks;
1167 /* save packet - this consumes, no matter what */
1168 ampdu_rx_add_slot(rap, off, tid, rxseq, ni, m, rxs);
1171 if (off < IEEE80211_SEQ_BA_RANGE) {
1173 * Outside the BA window, but within range;
1174 * flush the reorder q and move the window.
1175 * Sec 9.10.7.6.2 b) (p.138)
1177 IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
1178 "move BA win <%u:%u> (%u frames) rxseq %u tid %u",
1180 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
1181 rap->rxa_qframes, rxseq, tid);
1182 vap->iv_stats.is_ampdu_rx_move++;
1185 * The spec says to flush frames up to but not including:
1186 * WinStart_B = rxseq - rap->rxa_wnd + 1
1187 * Then insert the frame or notify the caller to process
1188 * it immediately. We can safely do this by just starting
1189 * over again because we know the frame will now be within
1192 /* NB: rxa_wnd known to be >0 */
1193 ampdu_rx_flush_upto(ni, rap,
1194 IEEE80211_SEQ_SUB(rxseq, rap->rxa_wnd-1));
1198 * Outside the BA window and out of range; toss.
1199 * Sec 9.10.7.6.2 c) (p.138)
1201 IEEE80211_DISCARD_MAC(vap,
1202 IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
1203 "MPDU", "BA win <%u:%u> (%u frames) rxseq %u tid %u%s",
1205 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
1206 rap->rxa_qframes, rxseq, tid,
1207 wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
1208 vap->iv_stats.is_ampdu_rx_drop++;
1209 IEEE80211_NODE_STAT(ni, rx_drop);
1218 * Process a BAR ctl frame. Dispatch all frames up to
1219 * the sequence number of the frame. If this frame is
1220 * out of range it's discarded.
1223 ieee80211_recv_bar(struct ieee80211_node *ni, struct mbuf *m0)
1225 struct ieee80211vap *vap = ni->ni_vap;
1226 struct ieee80211_frame_bar *wh;
1227 struct ieee80211_rx_ampdu *rap;
1228 ieee80211_seq rxseq;
1231 if (!ieee80211_recv_bar_ena) {
1233 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_11N,
1234 ni->ni_macaddr, "BAR", "%s", "processing disabled");
1236 vap->iv_stats.is_ampdu_bar_bad++;
1239 wh = mtod(m0, struct ieee80211_frame_bar *);
1240 /* XXX check basic BAR */
1241 tid = _IEEE80211_MASKSHIFT(le16toh(wh->i_ctl), IEEE80211_BAR_TID);
1242 rap = &ni->ni_rx_ampdu[tid];
1243 if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
1245 * No ADDBA request yet, don't touch.
1247 IEEE80211_DISCARD_MAC(vap,
1248 IEEE80211_MSG_INPUT | IEEE80211_MSG_11N,
1249 ni->ni_macaddr, "BAR", "no BA stream, tid %u", tid);
1250 vap->iv_stats.is_ampdu_bar_bad++;
1253 vap->iv_stats.is_ampdu_bar_rx++;
1254 rxseq = le16toh(wh->i_seq) >> IEEE80211_SEQ_SEQ_SHIFT;
1255 if (rxseq == rap->rxa_start)
1257 /* calculate offset in BA window */
1258 off = IEEE80211_SEQ_SUB(rxseq, rap->rxa_start);
1259 if (off < IEEE80211_SEQ_BA_RANGE) {
1261 * Flush the reorder q up to rxseq and move the window.
1262 * Sec 9.10.7.6.3 a) (p.138)
1264 IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
1265 "BAR moves BA win <%u:%u> (%u frames) rxseq %u tid %u",
1267 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
1268 rap->rxa_qframes, rxseq, tid);
1269 vap->iv_stats.is_ampdu_bar_move++;
1271 ampdu_rx_flush_upto(ni, rap, rxseq);
1272 if (off >= rap->rxa_wnd) {
1274 * BAR specifies a window start to the right of BA
1275 * window; we must move it explicitly since
1276 * ampdu_rx_flush_upto will not.
1278 rap->rxa_start = rxseq;
1282 * Out of range; toss.
1283 * Sec 9.10.7.6.3 b) (p.138)
1285 IEEE80211_DISCARD_MAC(vap,
1286 IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
1287 "BAR", "BA win <%u:%u> (%u frames) rxseq %u tid %u%s",
1289 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
1290 rap->rxa_qframes, rxseq, tid,
1291 wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
1292 vap->iv_stats.is_ampdu_bar_oow++;
1293 IEEE80211_NODE_STAT(ni, rx_drop);
1298 * Setup HT-specific state in a node. Called only
1299 * when HT use is negotiated so we don't do extra
1300 * work for temporary and/or legacy sta's.
1303 ieee80211_ht_node_init(struct ieee80211_node *ni)
1305 struct ieee80211_tx_ampdu *tap;
1308 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
1314 if (ni->ni_flags & IEEE80211_NODE_HT) {
1316 * Clean AMPDU state on re-associate. This handles the case
1317 * where a station leaves w/o notifying us and then returns
1318 * before node is reaped for inactivity.
1320 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
1322 "%s: calling cleanup (%p)",
1324 ieee80211_ht_node_cleanup(ni);
1326 for (tid = 0; tid < WME_NUM_TID; tid++) {
1327 tap = &ni->ni_tx_ampdu[tid];
1330 ieee80211_txampdu_init_pps(tap);
1331 /* NB: further initialization deferred */
1332 ieee80211_ampdu_rx_init_rap(ni, &ni->ni_rx_ampdu[tid]);
1334 ni->ni_flags |= IEEE80211_NODE_HT | IEEE80211_NODE_AMPDU |
1335 IEEE80211_NODE_AMSDU;
1339 * Cleanup HT-specific state in a node. Called only
1340 * when HT use has been marked.
1343 ieee80211_ht_node_cleanup(struct ieee80211_node *ni)
1345 struct ieee80211com *ic = ni->ni_ic;
1348 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
1353 KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT node"));
1355 /* XXX optimize this */
1356 for (i = 0; i < WME_NUM_TID; i++) {
1357 struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[i];
1358 if (tap->txa_flags & IEEE80211_AGGR_SETUP)
1361 for (i = 0; i < WME_NUM_TID; i++)
1362 ic->ic_ampdu_rx_stop(ni, &ni->ni_rx_ampdu[i]);
1365 ni->ni_flags &= ~IEEE80211_NODE_HT_ALL;
1369 * Age out HT resources for a station.
1372 ieee80211_ht_node_age(struct ieee80211_node *ni)
1374 struct ieee80211vap *vap = ni->ni_vap;
1377 KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT sta"));
1379 for (tid = 0; tid < WME_NUM_TID; tid++) {
1380 struct ieee80211_rx_ampdu *rap;
1382 rap = &ni->ni_rx_ampdu[tid];
1383 if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0)
1385 if (rap->rxa_qframes == 0)
1388 * Check for frames sitting too long in the reorder queue.
1389 * See above for more details on what's happening here.
1391 /* XXX honor batimeout? */
1392 if (ticks - rap->rxa_age > ieee80211_ampdu_age) {
1394 * Too long since we received the first
1395 * frame; flush the reorder buffer.
1397 vap->iv_stats.is_ampdu_rx_age += rap->rxa_qframes;
1398 ampdu_rx_flush(ni, rap);
1403 static struct ieee80211_channel *
1404 findhtchan(struct ieee80211com *ic, struct ieee80211_channel *c, int htflags)
1406 return ieee80211_find_channel(ic, c->ic_freq,
1407 (c->ic_flags &~ IEEE80211_CHAN_HT) | htflags);
1411 * Adjust a channel to be HT/non-HT according to the vap's configuration.
1413 struct ieee80211_channel *
1414 ieee80211_ht_adjust_channel(struct ieee80211com *ic,
1415 struct ieee80211_channel *chan, int flags)
1417 struct ieee80211_channel *c;
1419 if (flags & IEEE80211_FHT_HT) {
1420 /* promote to HT if possible */
1421 if (flags & IEEE80211_FHT_USEHT40) {
1422 if (!IEEE80211_IS_CHAN_HT40(chan)) {
1423 /* NB: arbitrarily pick ht40+ over ht40- */
1424 c = findhtchan(ic, chan, IEEE80211_CHAN_HT40U);
1426 c = findhtchan(ic, chan,
1427 IEEE80211_CHAN_HT40D);
1429 c = findhtchan(ic, chan,
1430 IEEE80211_CHAN_HT20);
1434 } else if (!IEEE80211_IS_CHAN_HT20(chan)) {
1435 c = findhtchan(ic, chan, IEEE80211_CHAN_HT20);
1439 } else if (IEEE80211_IS_CHAN_HT(chan)) {
1440 /* demote to legacy, HT use is disabled */
1441 c = ieee80211_find_channel(ic, chan->ic_freq,
1442 chan->ic_flags &~ IEEE80211_CHAN_HT);
1450 * Setup HT-specific state for a legacy WDS peer.
1453 ieee80211_ht_wds_init(struct ieee80211_node *ni)
1455 struct ieee80211vap *vap = ni->ni_vap;
1456 struct ieee80211_tx_ampdu *tap;
1459 KASSERT(vap->iv_flags_ht & IEEE80211_FHT_HT, ("no HT requested"));
1461 /* XXX check scan cache in case peer has an ap and we have info */
1463 * If setup with a legacy channel; locate an HT channel.
1464 * Otherwise if the inherited channel (from a companion
1465 * AP) is suitable use it so we use the same location
1466 * for the extension channel).
1468 ni->ni_chan = ieee80211_ht_adjust_channel(ni->ni_ic,
1469 ni->ni_chan, ieee80211_htchanflags(ni->ni_chan));
1472 if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20)
1473 ni->ni_htcap |= IEEE80211_HTCAP_SHORTGI20;
1474 if (IEEE80211_IS_CHAN_HT40(ni->ni_chan)) {
1475 ni->ni_htcap |= IEEE80211_HTCAP_CHWIDTH40;
1477 if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan))
1478 ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_ABOVE;
1479 else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan))
1480 ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_BELOW;
1481 if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40)
1482 ni->ni_htcap |= IEEE80211_HTCAP_SHORTGI40;
1485 ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_NONE;
1487 ni->ni_htctlchan = ni->ni_chan->ic_ieee;
1488 if (vap->iv_flags_ht & IEEE80211_FHT_RIFS)
1489 ni->ni_flags |= IEEE80211_NODE_RIFS;
1490 /* XXX does it make sense to enable SMPS? */
1492 ni->ni_htopmode = 0; /* XXX need protection state */
1493 ni->ni_htstbc = 0; /* XXX need info */
1495 for (tid = 0; tid < WME_NUM_TID; tid++) {
1496 tap = &ni->ni_tx_ampdu[tid];
1498 ieee80211_txampdu_init_pps(tap);
1500 /* NB: AMPDU tx/rx governed by IEEE80211_FHT_AMPDU_{TX,RX} */
1501 ni->ni_flags |= IEEE80211_NODE_HT | IEEE80211_NODE_AMPDU |
1502 IEEE80211_NODE_AMSDU;
1506 * Notify a VAP of a change in the HTINFO ie if it's a hostap VAP.
1508 * This is to be called from the deferred HT protection update
1509 * task once the flags are updated.
1512 ieee80211_htinfo_notify(struct ieee80211vap *vap)
1515 IEEE80211_LOCK_ASSERT(vap->iv_ic);
1517 if (vap->iv_opmode != IEEE80211_M_HOSTAP)
1519 if (vap->iv_state != IEEE80211_S_RUN ||
1520 !IEEE80211_IS_CHAN_HT(vap->iv_bss->ni_chan))
1524 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N,
1526 "HT bss occupancy change: %d sta, %d ht, "
1527 "%d ht40%s, HT protmode now 0x%x"
1529 , vap->iv_ht_sta_assoc
1530 , vap->iv_ht40_sta_assoc
1531 , (vap->iv_flags_ht & IEEE80211_FHT_NONHT_PR) ?
1532 ", non-HT sta present" : ""
1533 , vap->iv_curhtprotmode);
1535 ieee80211_beacon_notify(vap, IEEE80211_BEACON_HTINFO);
1539 * Calculate HT protection mode from current
1540 * state and handle updates.
1543 htinfo_update(struct ieee80211vap *vap)
1545 struct ieee80211com *ic = vap->iv_ic;
1548 if (vap->iv_sta_assoc != vap->iv_ht_sta_assoc) {
1549 protmode = IEEE80211_HTINFO_OPMODE_MIXED
1550 | IEEE80211_HTINFO_NONHT_PRESENT;
1551 } else if (vap->iv_flags_ht & IEEE80211_FHT_NONHT_PR) {
1552 protmode = IEEE80211_HTINFO_OPMODE_PROTOPT
1553 | IEEE80211_HTINFO_NONHT_PRESENT;
1554 } else if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
1555 IEEE80211_IS_CHAN_HT40(ic->ic_bsschan) &&
1556 vap->iv_sta_assoc != vap->iv_ht40_sta_assoc) {
1557 protmode = IEEE80211_HTINFO_OPMODE_HT20PR;
1559 protmode = IEEE80211_HTINFO_OPMODE_PURE;
1561 if (protmode != vap->iv_curhtprotmode) {
1562 vap->iv_curhtprotmode = protmode;
1563 /* Update VAP with new protection mode */
1564 ieee80211_vap_update_ht_protmode(vap);
1569 * Handle an HT station joining a BSS.
1572 ieee80211_ht_node_join(struct ieee80211_node *ni)
1574 struct ieee80211vap *vap = ni->ni_vap;
1576 IEEE80211_LOCK_ASSERT(vap->iv_ic);
1578 if (ni->ni_flags & IEEE80211_NODE_HT) {
1579 vap->iv_ht_sta_assoc++;
1580 if (ni->ni_chw == 40)
1581 vap->iv_ht40_sta_assoc++;
1587 * Handle an HT station leaving a BSS.
1590 ieee80211_ht_node_leave(struct ieee80211_node *ni)
1592 struct ieee80211vap *vap = ni->ni_vap;
1594 IEEE80211_LOCK_ASSERT(vap->iv_ic);
1596 if (ni->ni_flags & IEEE80211_NODE_HT) {
1597 vap->iv_ht_sta_assoc--;
1598 if (ni->ni_chw == 40)
1599 vap->iv_ht40_sta_assoc--;
1605 * Public version of htinfo_update; used for processing
1606 * beacon frames from overlapping bss.
1608 * Caller can specify either IEEE80211_HTINFO_OPMODE_MIXED
1609 * (on receipt of a beacon that advertises MIXED) or
1610 * IEEE80211_HTINFO_OPMODE_PROTOPT (on receipt of a beacon
1611 * from an overlapping legacy bss). We treat MIXED with
1612 * a higher precedence than PROTOPT (i.e. we will not change
1613 * change PROTOPT -> MIXED; only MIXED -> PROTOPT). This
1614 * corresponds to how we handle things in htinfo_update.
1618 ieee80211_htprot_update(struct ieee80211vap *vap, int protmode)
1620 struct ieee80211com *ic = vap->iv_ic;
1621 #define OPMODE(x) _IEEE80211_SHIFTMASK(x, IEEE80211_HTINFO_OPMODE)
1624 /* track non-HT station presence */
1625 KASSERT(protmode & IEEE80211_HTINFO_NONHT_PRESENT,
1626 ("protmode 0x%x", protmode));
1627 vap->iv_flags_ht |= IEEE80211_FHT_NONHT_PR;
1628 vap->iv_lastnonht = ticks;
1630 if (protmode != vap->iv_curhtprotmode &&
1631 (OPMODE(vap->iv_curhtprotmode) != IEEE80211_HTINFO_OPMODE_MIXED ||
1632 OPMODE(protmode) == IEEE80211_HTINFO_OPMODE_PROTOPT)) {
1633 vap->iv_curhtprotmode = protmode;
1634 /* Update VAP with new protection mode */
1635 ieee80211_vap_update_ht_protmode(vap);
1637 IEEE80211_UNLOCK(ic);
1642 * Time out presence of an overlapping bss with non-HT
1643 * stations. When operating in hostap mode we listen for
1644 * beacons from other stations and if we identify a non-HT
1645 * station is present we update the opmode field of the
1646 * HTINFO ie. To identify when all non-HT stations are
1647 * gone we time out this condition.
1650 ieee80211_ht_timeout(struct ieee80211vap *vap)
1653 IEEE80211_LOCK_ASSERT(vap->iv_ic);
1655 if ((vap->iv_flags_ht & IEEE80211_FHT_NONHT_PR) &&
1656 ieee80211_time_after(ticks, vap->iv_lastnonht + IEEE80211_NONHT_PRESENT_AGE)) {
1657 IEEE80211_DPRINTF(vap, IEEE80211_MSG_11N,
1658 "%s", "time out non-HT STA present on channel");
1659 vap->iv_flags_ht &= ~IEEE80211_FHT_NONHT_PR;
1665 * Process an 802.11n HT capabilities ie.
1668 ieee80211_parse_htcap(struct ieee80211_node *ni, const uint8_t *ie)
1670 if (ie[0] == IEEE80211_ELEMID_VENDOR) {
1672 * Station used Vendor OUI ie to associate;
1673 * mark the node so when we respond we'll use
1674 * the Vendor OUI's and not the standard ie's.
1676 ni->ni_flags |= IEEE80211_NODE_HTCOMPAT;
1679 ni->ni_flags &= ~IEEE80211_NODE_HTCOMPAT;
1681 ni->ni_htcap = le16dec(ie +
1682 __offsetof(struct ieee80211_ie_htcap, hc_cap));
1683 ni->ni_htparam = ie[__offsetof(struct ieee80211_ie_htcap, hc_param)];
1687 htinfo_parse(struct ieee80211_node *ni,
1688 const struct ieee80211_ie_htinfo *htinfo)
1692 ni->ni_htctlchan = htinfo->hi_ctrlchannel;
1693 ni->ni_ht2ndchan = _IEEE80211_SHIFTMASK(htinfo->hi_byte1,
1694 IEEE80211_HTINFO_2NDCHAN);
1695 w = le16dec(&htinfo->hi_byte2);
1696 ni->ni_htopmode = _IEEE80211_SHIFTMASK(w, IEEE80211_HTINFO_OPMODE);
1697 w = le16dec(&htinfo->hi_byte45);
1698 ni->ni_htstbc = _IEEE80211_SHIFTMASK(w, IEEE80211_HTINFO_BASIC_STBCMCS);
1702 * Parse an 802.11n HT info ie and save useful information
1703 * to the node state. Note this does not effect any state
1704 * changes such as for channel width change.
1707 ieee80211_parse_htinfo(struct ieee80211_node *ni, const uint8_t *ie)
1709 if (ie[0] == IEEE80211_ELEMID_VENDOR)
1711 htinfo_parse(ni, (const struct ieee80211_ie_htinfo *) ie);
1715 * Handle 11n/11ac channel switch.
1717 * Use the received HT/VHT ie's to identify the right channel to use.
1718 * If we cannot locate it in the channel table then fallback to
1721 * Note that we use this information to identify the node's
1722 * channel only; the caller is responsible for insuring any
1723 * required channel change is done (e.g. in sta mode when
1724 * parsing the contents of a beacon frame).
1727 htinfo_update_chw(struct ieee80211_node *ni, int htflags, int vhtflags)
1729 struct ieee80211com *ic = ni->ni_ic;
1730 struct ieee80211_channel *c;
1735 * First step - do HT/VHT only channel lookup based on operating mode
1736 * flags. This involves masking out the VHT flags as well.
1737 * Otherwise we end up doing the full channel walk each time
1738 * we trigger this, which is expensive.
1740 chanflags = (ni->ni_chan->ic_flags &~
1741 (IEEE80211_CHAN_HT | IEEE80211_CHAN_VHT)) | htflags | vhtflags;
1743 if (chanflags == ni->ni_chan->ic_flags)
1747 * If HT /or/ VHT flags have changed then check both.
1748 * We need to start by picking a HT channel anyway.
1752 chanflags = (ni->ni_chan->ic_flags &~
1753 (IEEE80211_CHAN_HT | IEEE80211_CHAN_VHT)) | htflags;
1754 /* XXX not right for ht40- */
1755 c = ieee80211_find_channel(ic, ni->ni_chan->ic_freq, chanflags);
1756 if (c == NULL && (htflags & IEEE80211_CHAN_HT40)) {
1758 * No HT40 channel entry in our table; fall back
1759 * to HT20 operation. This should not happen.
1761 c = findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT20);
1763 IEEE80211_NOTE(ni->ni_vap,
1764 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
1765 "no HT40 channel (freq %u), falling back to HT20",
1766 ni->ni_chan->ic_freq);
1771 /* Nothing found - leave it alone; move onto VHT */
1776 * If it's non-HT, then bail out now.
1778 if (! IEEE80211_IS_CHAN_HT(c)) {
1779 IEEE80211_NOTE(ni->ni_vap,
1780 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
1781 "not HT; skipping VHT check (%u/0x%x)",
1782 c->ic_freq, c->ic_flags);
1787 * Next step - look at the current VHT flags and determine
1788 * if we need to upgrade. Mask out the VHT and HT flags since
1789 * the vhtflags field will already have the correct HT
1792 if (IEEE80211_CONF_VHT(ic) && ni->ni_vhtcap != 0 && vhtflags != 0) {
1793 chanflags = (c->ic_flags
1794 &~ (IEEE80211_CHAN_HT | IEEE80211_CHAN_VHT))
1796 IEEE80211_NOTE(ni->ni_vap,
1797 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N,
1799 "%s: VHT; chanwidth=0x%02x; vhtflags=0x%08x",
1800 __func__, ni->ni_vht_chanwidth, vhtflags);
1802 IEEE80211_NOTE(ni->ni_vap,
1803 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N,
1805 "%s: VHT; trying lookup for %d/0x%08x",
1806 __func__, c->ic_freq, chanflags);
1807 c = ieee80211_find_channel(ic, c->ic_freq, chanflags);
1810 /* Finally, if it's changed */
1811 if (c != NULL && c != ni->ni_chan) {
1812 IEEE80211_NOTE(ni->ni_vap,
1813 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
1814 "switch station to %s%d channel %u/0x%x",
1815 IEEE80211_IS_CHAN_VHT(c) ? "VHT" : "HT",
1816 IEEE80211_IS_CHAN_VHT80(c) ? 80 :
1817 (IEEE80211_IS_CHAN_HT40(c) ? 40 : 20),
1818 c->ic_freq, c->ic_flags);
1822 /* NB: caller responsible for forcing any channel change */
1825 /* update node's (11n) tx channel width */
1826 ni->ni_chw = IEEE80211_IS_CHAN_HT40(ni->ni_chan)? 40 : 20;
1831 * Update 11n MIMO PS state according to received htcap.
1834 htcap_update_mimo_ps(struct ieee80211_node *ni)
1836 uint16_t oflags = ni->ni_flags;
1838 switch (ni->ni_htcap & IEEE80211_HTCAP_SMPS) {
1839 case IEEE80211_HTCAP_SMPS_DYNAMIC:
1840 ni->ni_flags |= IEEE80211_NODE_MIMO_PS;
1841 ni->ni_flags |= IEEE80211_NODE_MIMO_RTS;
1843 case IEEE80211_HTCAP_SMPS_ENA:
1844 ni->ni_flags |= IEEE80211_NODE_MIMO_PS;
1845 ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS;
1847 case IEEE80211_HTCAP_SMPS_OFF:
1848 default: /* disable on rx of reserved value */
1849 ni->ni_flags &= ~IEEE80211_NODE_MIMO_PS;
1850 ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS;
1853 return (oflags ^ ni->ni_flags);
1857 * Update short GI state according to received htcap
1858 * and local settings.
1860 static __inline void
1861 htcap_update_shortgi(struct ieee80211_node *ni)
1863 struct ieee80211vap *vap = ni->ni_vap;
1865 ni->ni_flags &= ~(IEEE80211_NODE_SGI20|IEEE80211_NODE_SGI40);
1866 if ((ni->ni_htcap & IEEE80211_HTCAP_SHORTGI20) &&
1867 (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20))
1868 ni->ni_flags |= IEEE80211_NODE_SGI20;
1869 if ((ni->ni_htcap & IEEE80211_HTCAP_SHORTGI40) &&
1870 (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40))
1871 ni->ni_flags |= IEEE80211_NODE_SGI40;
1875 * Update LDPC state according to received htcap
1876 * and local settings.
1878 static __inline void
1879 htcap_update_ldpc(struct ieee80211_node *ni)
1881 struct ieee80211vap *vap = ni->ni_vap;
1883 if ((ni->ni_htcap & IEEE80211_HTCAP_LDPC) &&
1884 (vap->iv_flags_ht & IEEE80211_FHT_LDPC_TX))
1885 ni->ni_flags |= IEEE80211_NODE_LDPC;
1889 * Parse and update HT-related state extracted from
1890 * the HT cap and info ie's.
1892 * This is called from the STA management path and
1893 * the ieee80211_node_join() path. It will take into
1894 * account the IEs discovered during scanning and
1895 * adjust things accordingly.
1898 ieee80211_ht_updateparams(struct ieee80211_node *ni,
1899 const uint8_t *htcapie, const uint8_t *htinfoie)
1901 struct ieee80211vap *vap = ni->ni_vap;
1902 const struct ieee80211_ie_htinfo *htinfo;
1904 ieee80211_parse_htcap(ni, htcapie);
1905 if (vap->iv_htcaps & IEEE80211_HTC_SMPS)
1906 htcap_update_mimo_ps(ni);
1907 htcap_update_shortgi(ni);
1908 htcap_update_ldpc(ni);
1910 if (htinfoie[0] == IEEE80211_ELEMID_VENDOR)
1912 htinfo = (const struct ieee80211_ie_htinfo *) htinfoie;
1913 htinfo_parse(ni, htinfo);
1916 * Defer the node channel change; we need to now
1917 * update VHT parameters before we do it.
1920 if ((htinfo->hi_byte1 & IEEE80211_HTINFO_RIFSMODE_PERM) &&
1921 (vap->iv_flags_ht & IEEE80211_FHT_RIFS))
1922 ni->ni_flags |= IEEE80211_NODE_RIFS;
1924 ni->ni_flags &= ~IEEE80211_NODE_RIFS;
1928 ieee80211_vht_get_vhtflags(struct ieee80211_node *ni, uint32_t htflags)
1930 struct ieee80211vap *vap = ni->ni_vap;
1931 uint32_t vhtflags = 0;
1934 if (ni->ni_flags & IEEE80211_NODE_VHT && vap->iv_vht_flags & IEEE80211_FVHT_VHT) {
1935 if ((ni->ni_vht_chanwidth == IEEE80211_VHT_CHANWIDTH_160MHZ) &&
1936 /* XXX 2 means "160MHz and 80+80MHz", 1 means "160MHz" */
1937 (_IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
1938 IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_MASK) >= 1) &&
1939 (vap->iv_vht_flags & IEEE80211_FVHT_USEVHT160)) {
1940 vhtflags = IEEE80211_CHAN_VHT160;
1941 /* Mirror the HT40 flags */
1942 if (htflags == IEEE80211_CHAN_HT40U) {
1943 vhtflags |= IEEE80211_CHAN_HT40U;
1944 } else if (htflags == IEEE80211_CHAN_HT40D) {
1945 vhtflags |= IEEE80211_CHAN_HT40D;
1947 } else if ((ni->ni_vht_chanwidth == IEEE80211_VHT_CHANWIDTH_80P80MHZ) &&
1948 /* XXX 2 means "160MHz and 80+80MHz" */
1949 (_IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
1950 IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_MASK) == 2) &&
1951 (vap->iv_vht_flags & IEEE80211_FVHT_USEVHT80P80)) {
1952 vhtflags = IEEE80211_CHAN_VHT80P80;
1953 /* Mirror the HT40 flags */
1954 if (htflags == IEEE80211_CHAN_HT40U) {
1955 vhtflags |= IEEE80211_CHAN_HT40U;
1956 } else if (htflags == IEEE80211_CHAN_HT40D) {
1957 vhtflags |= IEEE80211_CHAN_HT40D;
1959 } else if ((ni->ni_vht_chanwidth == IEEE80211_VHT_CHANWIDTH_80MHZ) &&
1960 (vap->iv_vht_flags & IEEE80211_FVHT_USEVHT80)) {
1961 vhtflags = IEEE80211_CHAN_VHT80;
1962 /* Mirror the HT40 flags */
1963 if (htflags == IEEE80211_CHAN_HT40U) {
1964 vhtflags |= IEEE80211_CHAN_HT40U;
1965 } else if (htflags == IEEE80211_CHAN_HT40D) {
1966 vhtflags |= IEEE80211_CHAN_HT40D;
1968 } else if (ni->ni_vht_chanwidth == IEEE80211_VHT_CHANWIDTH_USE_HT) {
1969 /* Mirror the HT40 flags */
1971 * XXX TODO: if ht40 is disabled, but vht40 isn't
1972 * disabled then this logic will get very, very sad.
1973 * It's quite possible the only sane thing to do is
1974 * to not have vht40 as an option, and just obey
1975 * 'ht40' as that flag.
1977 if ((htflags == IEEE80211_CHAN_HT40U) &&
1978 (vap->iv_vht_flags & IEEE80211_FVHT_USEVHT40)) {
1979 vhtflags = IEEE80211_CHAN_VHT40U
1980 | IEEE80211_CHAN_HT40U;
1981 } else if (htflags == IEEE80211_CHAN_HT40D &&
1982 (vap->iv_vht_flags & IEEE80211_FVHT_USEVHT40)) {
1983 vhtflags = IEEE80211_CHAN_VHT40D
1984 | IEEE80211_CHAN_HT40D;
1985 } else if (htflags == IEEE80211_CHAN_HT20) {
1986 vhtflags = IEEE80211_CHAN_VHT20
1987 | IEEE80211_CHAN_HT20;
1990 vhtflags = IEEE80211_CHAN_VHT20;
1997 * Final part of updating the HT parameters.
1999 * This is called from the STA management path and
2000 * the ieee80211_node_join() path. It will take into
2001 * account the IEs discovered during scanning and
2002 * adjust things accordingly.
2004 * This is done after a call to ieee80211_ht_updateparams()
2005 * because it (and the upcoming VHT version of updateparams)
2006 * needs to ensure everything is parsed before htinfo_update_chw()
2007 * is called - which will change the channel config for the
2011 ieee80211_ht_updateparams_final(struct ieee80211_node *ni,
2012 const uint8_t *htcapie, const uint8_t *htinfoie)
2014 struct ieee80211vap *vap = ni->ni_vap;
2015 const struct ieee80211_ie_htinfo *htinfo;
2016 int htflags, vhtflags;
2019 htinfo = (const struct ieee80211_ie_htinfo *) htinfoie;
2021 htflags = (vap->iv_flags_ht & IEEE80211_FHT_HT) ?
2022 IEEE80211_CHAN_HT20 : 0;
2024 /* NB: honor operating mode constraint */
2025 if ((htinfo->hi_byte1 & IEEE80211_HTINFO_TXWIDTH_2040) &&
2026 (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)) {
2027 if (ni->ni_ht2ndchan == IEEE80211_HTINFO_2NDCHAN_ABOVE)
2028 htflags = IEEE80211_CHAN_HT40U;
2029 else if (ni->ni_ht2ndchan == IEEE80211_HTINFO_2NDCHAN_BELOW)
2030 htflags = IEEE80211_CHAN_HT40D;
2034 * VHT flags - do much the same; check whether VHT is available
2035 * and if so, what our ideal channel use would be based on our
2036 * capabilities and the (pre-parsed) VHT info IE.
2038 vhtflags = ieee80211_vht_get_vhtflags(ni, htflags);
2040 if (htinfo_update_chw(ni, htflags, vhtflags))
2047 * Parse and update HT-related state extracted from the HT cap ie
2048 * for a station joining an HT BSS.
2050 * This is called from the hostap path for each station.
2053 ieee80211_ht_updatehtcap(struct ieee80211_node *ni, const uint8_t *htcapie)
2055 struct ieee80211vap *vap = ni->ni_vap;
2057 ieee80211_parse_htcap(ni, htcapie);
2058 if (vap->iv_htcaps & IEEE80211_HTC_SMPS)
2059 htcap_update_mimo_ps(ni);
2060 htcap_update_shortgi(ni);
2061 htcap_update_ldpc(ni);
2065 * Called once HT and VHT capabilities are parsed in hostap mode -
2066 * this will adjust the channel configuration of the given node
2067 * based on the configuration and capabilities.
2070 ieee80211_ht_updatehtcap_final(struct ieee80211_node *ni)
2072 struct ieee80211vap *vap = ni->ni_vap;
2076 /* NB: honor operating mode constraint */
2077 /* XXX 40 MHz intolerant */
2078 htflags = (vap->iv_flags_ht & IEEE80211_FHT_HT) ?
2079 IEEE80211_CHAN_HT20 : 0;
2080 if ((ni->ni_htcap & IEEE80211_HTCAP_CHWIDTH40) &&
2081 (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)) {
2082 if (IEEE80211_IS_CHAN_HT40U(vap->iv_bss->ni_chan))
2083 htflags = IEEE80211_CHAN_HT40U;
2084 else if (IEEE80211_IS_CHAN_HT40D(vap->iv_bss->ni_chan))
2085 htflags = IEEE80211_CHAN_HT40D;
2088 * VHT flags - do much the same; check whether VHT is available
2089 * and if so, what our ideal channel use would be based on our
2090 * capabilities and the (pre-parsed) VHT info IE.
2092 vhtflags = ieee80211_vht_get_vhtflags(ni, htflags);
2094 (void) htinfo_update_chw(ni, htflags, vhtflags);
2098 * Install received HT rate set by parsing the HT cap ie.
2101 ieee80211_setup_htrates(struct ieee80211_node *ni, const uint8_t *ie, int flags)
2103 struct ieee80211com *ic = ni->ni_ic;
2104 struct ieee80211vap *vap = ni->ni_vap;
2105 const struct ieee80211_ie_htcap *htcap;
2106 struct ieee80211_htrateset *rs;
2107 int i, maxequalmcs, maxunequalmcs;
2109 maxequalmcs = ic->ic_txstream * 8 - 1;
2111 if (ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL) {
2112 if (ic->ic_txstream >= 2)
2114 if (ic->ic_txstream >= 3)
2116 if (ic->ic_txstream >= 4)
2120 rs = &ni->ni_htrates;
2121 memset(rs, 0, sizeof(*rs));
2123 if (ie[0] == IEEE80211_ELEMID_VENDOR)
2125 htcap = (const struct ieee80211_ie_htcap *) ie;
2126 for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++) {
2127 if (isclr(htcap->hc_mcsset, i))
2129 if (rs->rs_nrates == IEEE80211_HTRATE_MAXSIZE) {
2131 IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
2132 "WARNING, HT rate set too large; only "
2133 "using %u rates", IEEE80211_HTRATE_MAXSIZE);
2134 vap->iv_stats.is_rx_rstoobig++;
2137 if (i <= 31 && i > maxequalmcs)
2140 (ic->ic_htcaps & IEEE80211_HTC_TXMCS32) == 0)
2142 if (i > 32 && i > maxunequalmcs)
2144 rs->rs_rates[rs->rs_nrates++] = i;
2147 return ieee80211_fix_rate(ni, (struct ieee80211_rateset *) rs, flags);
2151 * Mark rates in a node's HT rate set as basic according
2152 * to the information in the supplied HT info ie.
2155 ieee80211_setup_basic_htrates(struct ieee80211_node *ni, const uint8_t *ie)
2157 const struct ieee80211_ie_htinfo *htinfo;
2158 struct ieee80211_htrateset *rs;
2161 if (ie[0] == IEEE80211_ELEMID_VENDOR)
2163 htinfo = (const struct ieee80211_ie_htinfo *) ie;
2164 rs = &ni->ni_htrates;
2165 if (rs->rs_nrates == 0) {
2166 IEEE80211_NOTE(ni->ni_vap,
2167 IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
2168 "%s", "WARNING, empty HT rate set");
2171 for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++) {
2172 if (isclr(htinfo->hi_basicmcsset, i))
2174 for (j = 0; j < rs->rs_nrates; j++)
2175 if ((rs->rs_rates[j] & IEEE80211_RATE_VAL) == i)
2176 rs->rs_rates[j] |= IEEE80211_RATE_BASIC;
2181 ampdu_tx_setup(struct ieee80211_tx_ampdu *tap)
2183 callout_init(&tap->txa_timer, 1);
2184 tap->txa_flags |= IEEE80211_AGGR_SETUP;
2185 tap->txa_lastsample = ticks;
2189 ampdu_tx_stop(struct ieee80211_tx_ampdu *tap)
2191 struct ieee80211_node *ni = tap->txa_ni;
2192 struct ieee80211com *ic = ni->ni_ic;
2194 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2199 KASSERT(tap->txa_flags & IEEE80211_AGGR_SETUP,
2200 ("txa_flags 0x%x tid %d ac %d", tap->txa_flags, tap->txa_tid,
2201 TID_TO_WME_AC(tap->txa_tid)));
2204 * Stop BA stream if setup so driver has a chance
2205 * to reclaim any resources it might have allocated.
2207 ic->ic_addba_stop(ni, tap);
2209 * Stop any pending BAR transmit.
2211 bar_stop_timer(tap);
2214 * Reset packet estimate.
2216 ieee80211_txampdu_init_pps(tap);
2218 /* NB: clearing NAK means we may re-send ADDBA */
2219 tap->txa_flags &= ~(IEEE80211_AGGR_SETUP | IEEE80211_AGGR_NAK);
2223 * ADDBA response timeout.
2225 * If software aggregation and per-TID queue management was done here,
2226 * that queue would be unpaused after the ADDBA timeout occurs.
2229 addba_timeout(void *arg)
2231 struct ieee80211_tx_ampdu *tap = arg;
2232 struct ieee80211_node *ni = tap->txa_ni;
2233 struct ieee80211com *ic = ni->ni_ic;
2236 tap->txa_flags &= ~IEEE80211_AGGR_XCHGPEND;
2237 tap->txa_attempts++;
2238 ic->ic_addba_response_timeout(ni, tap);
2242 addba_start_timeout(struct ieee80211_tx_ampdu *tap)
2244 /* XXX use CALLOUT_PENDING instead? */
2245 callout_reset(&tap->txa_timer, ieee80211_addba_timeout,
2246 addba_timeout, tap);
2247 tap->txa_flags |= IEEE80211_AGGR_XCHGPEND;
2248 tap->txa_nextrequest = ticks + ieee80211_addba_timeout;
2252 addba_stop_timeout(struct ieee80211_tx_ampdu *tap)
2254 /* XXX use CALLOUT_PENDING instead? */
2255 if (tap->txa_flags & IEEE80211_AGGR_XCHGPEND) {
2256 callout_stop(&tap->txa_timer);
2257 tap->txa_flags &= ~IEEE80211_AGGR_XCHGPEND;
2262 null_addba_response_timeout(struct ieee80211_node *ni,
2263 struct ieee80211_tx_ampdu *tap)
2268 * Default method for requesting A-MPDU tx aggregation.
2269 * We setup the specified state block and start a timer
2270 * to wait for an ADDBA response frame.
2273 ieee80211_addba_request(struct ieee80211_node *ni,
2274 struct ieee80211_tx_ampdu *tap,
2275 int dialogtoken, int baparamset, int batimeout)
2280 tap->txa_token = dialogtoken;
2281 tap->txa_flags |= IEEE80211_AGGR_IMMEDIATE;
2282 bufsiz = _IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_BUFSIZ);
2283 tap->txa_wnd = (bufsiz == 0) ?
2284 IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
2285 addba_start_timeout(tap);
2290 * Called by drivers that wish to request an ADDBA session be
2291 * setup. This brings it up and starts the request timer.
2294 ieee80211_ampdu_tx_request_ext(struct ieee80211_node *ni, int tid)
2296 struct ieee80211_tx_ampdu *tap;
2298 if (tid < 0 || tid > 15)
2300 tap = &ni->ni_tx_ampdu[tid];
2303 if ((tap->txa_flags & IEEE80211_AGGR_SETUP) == 0) {
2304 /* do deferred setup of state */
2305 ampdu_tx_setup(tap);
2307 /* XXX hack for not doing proper locking */
2308 tap->txa_flags &= ~IEEE80211_AGGR_NAK;
2309 addba_start_timeout(tap);
2314 * Called by drivers that have marked a session as active.
2317 ieee80211_ampdu_tx_request_active_ext(struct ieee80211_node *ni, int tid,
2320 struct ieee80211_tx_ampdu *tap;
2322 if (tid < 0 || tid > 15)
2324 tap = &ni->ni_tx_ampdu[tid];
2327 addba_stop_timeout(tap);
2329 tap->txa_flags |= IEEE80211_AGGR_RUNNING;
2330 tap->txa_attempts = 0;
2332 /* mark tid so we don't try again */
2333 tap->txa_flags |= IEEE80211_AGGR_NAK;
2339 * Default method for processing an A-MPDU tx aggregation
2340 * response. We shutdown any pending timer and update the
2341 * state block according to the reply.
2344 ieee80211_addba_response(struct ieee80211_node *ni,
2345 struct ieee80211_tx_ampdu *tap,
2346 int status, int baparamset, int batimeout)
2348 struct ieee80211vap *vap = ni->ni_vap;
2352 addba_stop_timeout(tap);
2353 if (status == IEEE80211_STATUS_SUCCESS) {
2354 bufsiz = _IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_BUFSIZ);
2355 /* XXX override our request? */
2356 tap->txa_wnd = (bufsiz == 0) ?
2357 IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
2359 tid = _IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_TID);
2361 tap->txa_flags |= IEEE80211_AGGR_RUNNING;
2362 tap->txa_attempts = 0;
2363 /* TODO: this should be a vap flag */
2364 if ((vap->iv_htcaps & IEEE80211_HTC_TX_AMSDU_AMPDU) &&
2365 (ni->ni_flags & IEEE80211_NODE_AMSDU_TX) &&
2366 (_IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_AMSDU)))
2367 tap->txa_flags |= IEEE80211_AGGR_AMSDU;
2369 tap->txa_flags &= ~IEEE80211_AGGR_AMSDU;
2371 /* mark tid so we don't try again */
2372 tap->txa_flags |= IEEE80211_AGGR_NAK;
2378 * Default method for stopping A-MPDU tx aggregation.
2379 * Any timer is cleared and we drain any pending frames.
2382 ieee80211_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
2385 addba_stop_timeout(tap);
2386 if (tap->txa_flags & IEEE80211_AGGR_RUNNING) {
2387 /* XXX clear aggregation queue */
2388 tap->txa_flags &= ~(IEEE80211_AGGR_RUNNING | IEEE80211_AGGR_AMSDU);
2390 tap->txa_attempts = 0;
2394 * Process a received action frame using the default aggregation
2395 * policy. We intercept ADDBA-related frames and use them to
2396 * update our aggregation state. All other frames are passed up
2397 * for processing by ieee80211_recv_action.
2400 ht_recv_action_ba_addba_request(struct ieee80211_node *ni,
2401 const struct ieee80211_frame *wh,
2402 const uint8_t *frm, const uint8_t *efrm)
2404 struct ieee80211com *ic = ni->ni_ic;
2405 struct ieee80211vap *vap = ni->ni_vap;
2406 struct ieee80211_rx_ampdu *rap;
2407 uint8_t dialogtoken;
2408 uint16_t baparamset, batimeout, baseqctl;
2412 dialogtoken = frm[2];
2413 baparamset = le16dec(frm+3);
2414 batimeout = le16dec(frm+5);
2415 baseqctl = le16dec(frm+7);
2417 tid = _IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_TID);
2419 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2420 "recv ADDBA request: dialogtoken %u baparamset 0x%x "
2421 "(tid %d bufsiz %d) batimeout %d baseqctl %d:%d amsdu %d",
2422 dialogtoken, baparamset,
2423 tid, _IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_BUFSIZ),
2425 _IEEE80211_MASKSHIFT(baseqctl, IEEE80211_BASEQ_START),
2426 _IEEE80211_MASKSHIFT(baseqctl, IEEE80211_BASEQ_FRAG),
2427 _IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_AMSDU));
2429 rap = &ni->ni_rx_ampdu[tid];
2431 /* Send ADDBA response */
2432 args[0] = dialogtoken;
2434 * NB: We ack only if the sta associated with HT and
2435 * the ap is configured to do AMPDU rx (the latter
2436 * violates the 11n spec and is mostly for testing).
2438 if ((ni->ni_flags & IEEE80211_NODE_AMPDU_RX) &&
2439 (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_RX)) {
2440 /* XXX TODO: handle ampdu_rx_start failure */
2441 ic->ic_ampdu_rx_start(ni, rap,
2442 baparamset, batimeout, baseqctl);
2444 args[1] = IEEE80211_STATUS_SUCCESS;
2446 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2447 ni, "reject ADDBA request: %s",
2448 ni->ni_flags & IEEE80211_NODE_AMPDU_RX ?
2449 "administratively disabled" :
2450 "not negotiated for station");
2451 vap->iv_stats.is_addba_reject++;
2452 args[1] = IEEE80211_STATUS_UNSPECIFIED;
2454 /* XXX honor rap flags? */
2455 args[2] = IEEE80211_BAPS_POLICY_IMMEDIATE
2456 | _IEEE80211_SHIFTMASK(tid, IEEE80211_BAPS_TID)
2457 | _IEEE80211_SHIFTMASK(rap->rxa_wnd, IEEE80211_BAPS_BUFSIZ)
2461 * TODO: we're out of iv_flags_ht fields; once
2462 * this is extended we should make this configurable.
2464 if ((baparamset & IEEE80211_BAPS_AMSDU) &&
2465 (ni->ni_flags & IEEE80211_NODE_AMSDU_RX) &&
2466 (vap->iv_htcaps & IEEE80211_HTC_RX_AMSDU_AMPDU))
2467 args[2] |= IEEE80211_BAPS_AMSDU;
2471 ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
2472 IEEE80211_ACTION_BA_ADDBA_RESPONSE, args);
2477 ht_recv_action_ba_addba_response(struct ieee80211_node *ni,
2478 const struct ieee80211_frame *wh,
2479 const uint8_t *frm, const uint8_t *efrm)
2481 struct ieee80211com *ic = ni->ni_ic;
2482 struct ieee80211vap *vap = ni->ni_vap;
2483 struct ieee80211_tx_ampdu *tap;
2484 uint8_t dialogtoken, policy;
2485 uint16_t baparamset, batimeout, code;
2487 #ifdef IEEE80211_DEBUG
2491 dialogtoken = frm[2];
2492 code = le16dec(frm+3);
2493 baparamset = le16dec(frm+5);
2494 tid = _IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_TID);
2495 #ifdef IEEE80211_DEBUG
2496 bufsiz = _IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_BUFSIZ);
2497 amsdu = !! _IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_AMSDU);
2499 policy = _IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_POLICY);
2500 batimeout = le16dec(frm+7);
2502 tap = &ni->ni_tx_ampdu[tid];
2503 if ((tap->txa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
2504 IEEE80211_DISCARD_MAC(vap,
2505 IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2506 ni->ni_macaddr, "ADDBA response",
2507 "no pending ADDBA, tid %d dialogtoken %u "
2508 "code %d", tid, dialogtoken, code);
2509 vap->iv_stats.is_addba_norequest++;
2512 if (dialogtoken != tap->txa_token) {
2513 IEEE80211_DISCARD_MAC(vap,
2514 IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2515 ni->ni_macaddr, "ADDBA response",
2516 "dialogtoken mismatch: waiting for %d, "
2517 "received %d, tid %d code %d",
2518 tap->txa_token, dialogtoken, tid, code);
2519 vap->iv_stats.is_addba_badtoken++;
2522 /* NB: assumes IEEE80211_AGGR_IMMEDIATE is 1 */
2523 if (policy != (tap->txa_flags & IEEE80211_AGGR_IMMEDIATE)) {
2524 IEEE80211_DISCARD_MAC(vap,
2525 IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2526 ni->ni_macaddr, "ADDBA response",
2527 "policy mismatch: expecting %s, "
2528 "received %s, tid %d code %d",
2529 tap->txa_flags & IEEE80211_AGGR_IMMEDIATE,
2531 vap->iv_stats.is_addba_badpolicy++;
2535 /* XXX we take MIN in ieee80211_addba_response */
2536 if (bufsiz > IEEE80211_AGGR_BAWMAX) {
2537 IEEE80211_DISCARD_MAC(vap,
2538 IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2539 ni->ni_macaddr, "ADDBA response",
2540 "BA window too large: max %d, "
2541 "received %d, tid %d code %d",
2542 bufsiz, IEEE80211_AGGR_BAWMAX, tid, code);
2543 vap->iv_stats.is_addba_badbawinsize++;
2548 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2549 "recv ADDBA response: dialogtoken %u code %d "
2550 "baparamset 0x%x (tid %d bufsiz %d amsdu %d) batimeout %d",
2551 dialogtoken, code, baparamset, tid,
2555 ic->ic_addba_response(ni, tap, code, baparamset, batimeout);
2560 ht_recv_action_ba_delba(struct ieee80211_node *ni,
2561 const struct ieee80211_frame *wh,
2562 const uint8_t *frm, const uint8_t *efrm)
2564 struct ieee80211com *ic = ni->ni_ic;
2565 struct ieee80211_rx_ampdu *rap;
2566 struct ieee80211_tx_ampdu *tap;
2567 uint16_t baparamset;
2568 #ifdef IEEE80211_DEBUG
2573 baparamset = le16dec(frm+2);
2574 #ifdef IEEE80211_DEBUG
2575 code = le16dec(frm+4);
2578 tid = _IEEE80211_MASKSHIFT(baparamset, IEEE80211_DELBAPS_TID);
2580 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2581 "recv DELBA: baparamset 0x%x (tid %d initiator %d) "
2582 "code %d", baparamset, tid,
2583 _IEEE80211_MASKSHIFT(baparamset, IEEE80211_DELBAPS_INIT), code);
2585 if ((baparamset & IEEE80211_DELBAPS_INIT) == 0) {
2586 tap = &ni->ni_tx_ampdu[tid];
2587 ic->ic_addba_stop(ni, tap);
2589 rap = &ni->ni_rx_ampdu[tid];
2590 ic->ic_ampdu_rx_stop(ni, rap);
2596 ht_recv_action_ht_txchwidth(struct ieee80211_node *ni,
2597 const struct ieee80211_frame *wh,
2598 const uint8_t *frm, const uint8_t *efrm)
2602 chw = (frm[2] == IEEE80211_A_HT_TXCHWIDTH_2040) ? 40 : 20;
2604 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2605 "%s: HT txchwidth, width %d%s",
2606 __func__, chw, ni->ni_chw != chw ? "*" : "");
2607 if (chw != ni->ni_chw) {
2608 /* XXX does this need to change the ht40 station count? */
2610 /* XXX notify on change */
2616 ht_recv_action_ht_mimopwrsave(struct ieee80211_node *ni,
2617 const struct ieee80211_frame *wh,
2618 const uint8_t *frm, const uint8_t *efrm)
2620 const struct ieee80211_action_ht_mimopowersave *mps =
2621 (const struct ieee80211_action_ht_mimopowersave *) frm;
2623 /* XXX check iv_htcaps */
2624 if (mps->am_control & IEEE80211_A_HT_MIMOPWRSAVE_ENA)
2625 ni->ni_flags |= IEEE80211_NODE_MIMO_PS;
2627 ni->ni_flags &= ~IEEE80211_NODE_MIMO_PS;
2628 if (mps->am_control & IEEE80211_A_HT_MIMOPWRSAVE_MODE)
2629 ni->ni_flags |= IEEE80211_NODE_MIMO_RTS;
2631 ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS;
2632 /* XXX notify on change */
2633 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2634 "%s: HT MIMO PS (%s%s)", __func__,
2635 (ni->ni_flags & IEEE80211_NODE_MIMO_PS) ? "on" : "off",
2636 (ni->ni_flags & IEEE80211_NODE_MIMO_RTS) ? "+rts" : ""
2642 * Transmit processing.
2646 * Check if A-MPDU should be requested/enabled for a stream.
2647 * We require a traffic rate above a per-AC threshold and we
2648 * also handle backoff from previous failed attempts.
2650 * Drivers may override this method to bring in information
2651 * such as link state conditions in making the decision.
2654 ieee80211_ampdu_enable(struct ieee80211_node *ni,
2655 struct ieee80211_tx_ampdu *tap)
2657 struct ieee80211vap *vap = ni->ni_vap;
2659 if (tap->txa_avgpps <
2660 vap->iv_ampdu_mintraffic[TID_TO_WME_AC(tap->txa_tid)])
2662 /* XXX check rssi? */
2663 if (tap->txa_attempts >= ieee80211_addba_maxtries &&
2664 ieee80211_time_after(ticks, tap->txa_nextrequest)) {
2666 * Don't retry too often; txa_nextrequest is set
2667 * to the minimum interval we'll retry after
2668 * ieee80211_addba_maxtries failed attempts are made.
2672 IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
2673 "enable AMPDU on tid %d (%s), avgpps %d pkts %d attempt %d",
2674 tap->txa_tid, ieee80211_wme_acnames[TID_TO_WME_AC(tap->txa_tid)],
2675 tap->txa_avgpps, tap->txa_pkts, tap->txa_attempts);
2680 * Request A-MPDU tx aggregation. Setup local state and
2681 * issue an ADDBA request. BA use will only happen after
2682 * the other end replies with ADDBA response.
2685 ieee80211_ampdu_request(struct ieee80211_node *ni,
2686 struct ieee80211_tx_ampdu *tap)
2688 struct ieee80211com *ic = ni->ni_ic;
2690 int tid, dialogtoken;
2691 static int tokens = 0; /* XXX */
2694 if ((tap->txa_flags & IEEE80211_AGGR_SETUP) == 0) {
2695 /* do deferred setup of state */
2696 ampdu_tx_setup(tap);
2698 /* XXX hack for not doing proper locking */
2699 tap->txa_flags &= ~IEEE80211_AGGR_NAK;
2701 dialogtoken = (tokens+1) % 63; /* XXX */
2705 * XXX TODO: This is racy with any other parallel TX going on. :(
2707 tap->txa_start = ni->ni_txseqs[tid];
2709 args[0] = dialogtoken;
2710 args[1] = 0; /* NB: status code not used */
2711 args[2] = IEEE80211_BAPS_POLICY_IMMEDIATE
2712 | _IEEE80211_SHIFTMASK(tid, IEEE80211_BAPS_TID)
2713 | _IEEE80211_SHIFTMASK(IEEE80211_AGGR_BAWMAX,
2714 IEEE80211_BAPS_BUFSIZ)
2717 /* XXX TODO: this should be a flag, not iv_htcaps */
2718 if ((ni->ni_flags & IEEE80211_NODE_AMSDU_TX) &&
2719 (ni->ni_vap->iv_htcaps & IEEE80211_HTC_TX_AMSDU_AMPDU))
2720 args[2] |= IEEE80211_BAPS_AMSDU;
2722 args[3] = 0; /* batimeout */
2723 /* NB: do first so there's no race against reply */
2724 if (!ic->ic_addba_request(ni, tap, dialogtoken, args[2], args[3])) {
2725 /* unable to setup state, don't make request */
2726 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2727 ni, "%s: could not setup BA stream for TID %d AC %d",
2728 __func__, tap->txa_tid, TID_TO_WME_AC(tap->txa_tid));
2729 /* defer next try so we don't slam the driver with requests */
2730 tap->txa_attempts = ieee80211_addba_maxtries;
2731 /* NB: check in case driver wants to override */
2732 if (tap->txa_nextrequest <= ticks)
2733 tap->txa_nextrequest = ticks + ieee80211_addba_backoff;
2736 tokens = dialogtoken; /* allocate token */
2737 /* NB: after calling ic_addba_request so driver can set txa_start */
2738 args[4] = _IEEE80211_SHIFTMASK(tap->txa_start, IEEE80211_BASEQ_START)
2739 | _IEEE80211_SHIFTMASK(0, IEEE80211_BASEQ_FRAG)
2741 return ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
2742 IEEE80211_ACTION_BA_ADDBA_REQUEST, args);
2746 * Terminate an AMPDU tx stream. State is reclaimed
2747 * and the peer notified with a DelBA Action frame.
2750 ieee80211_ampdu_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
2753 struct ieee80211com *ic = ni->ni_ic;
2754 struct ieee80211vap *vap = ni->ni_vap;
2758 tap->txa_flags &= ~IEEE80211_AGGR_BARPEND;
2759 if (IEEE80211_AMPDU_RUNNING(tap)) {
2760 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2761 ni, "%s: stop BA stream for TID %d (reason: %d (%s))",
2762 __func__, tap->txa_tid, reason,
2763 ieee80211_reason_to_string(reason));
2764 vap->iv_stats.is_ampdu_stop++;
2766 ic->ic_addba_stop(ni, tap);
2767 args[0] = tap->txa_tid;
2768 args[1] = IEEE80211_DELBAPS_INIT;
2769 args[2] = reason; /* XXX reason code */
2770 ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
2771 IEEE80211_ACTION_BA_DELBA, args);
2773 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2774 ni, "%s: BA stream for TID %d not running "
2775 "(reason: %d (%s))", __func__, tap->txa_tid, reason,
2776 ieee80211_reason_to_string(reason));
2777 vap->iv_stats.is_ampdu_stop_failed++;
2782 static void bar_start_timer(struct ieee80211_tx_ampdu *tap);
2785 bar_timeout(void *arg)
2787 struct ieee80211_tx_ampdu *tap = arg;
2788 struct ieee80211_node *ni = tap->txa_ni;
2790 KASSERT((tap->txa_flags & IEEE80211_AGGR_XCHGPEND) == 0,
2791 ("bar/addba collision, flags 0x%x", tap->txa_flags));
2793 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2794 ni, "%s: tid %u flags 0x%x attempts %d", __func__,
2795 tap->txa_tid, tap->txa_flags, tap->txa_attempts);
2797 /* guard against race with bar_tx_complete */
2798 if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) == 0)
2801 if (tap->txa_attempts >= ieee80211_bar_maxtries) {
2802 struct ieee80211com *ic = ni->ni_ic;
2804 ni->ni_vap->iv_stats.is_ampdu_bar_tx_fail++;
2806 * If (at least) the last BAR TX timeout was due to
2807 * an ieee80211_send_bar() failures, then we need
2808 * to make sure we notify the driver that a BAR
2809 * TX did occur and fail. This gives the driver
2810 * a chance to undo any queue pause that may
2813 ic->ic_bar_response(ni, tap, 1);
2814 ieee80211_ampdu_stop(ni, tap, IEEE80211_REASON_TIMEOUT);
2816 ni->ni_vap->iv_stats.is_ampdu_bar_tx_retry++;
2817 if (ieee80211_send_bar(ni, tap, tap->txa_seqpending) != 0) {
2818 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2819 ni, "%s: failed to TX, starting timer\n",
2822 * If ieee80211_send_bar() fails here, the
2823 * timer may have stopped and/or the pending
2824 * flag may be clear. Because of this,
2825 * fake the BARPEND and reset the timer.
2826 * A retransmission attempt will then occur
2827 * during the next timeout.
2830 tap->txa_flags |= IEEE80211_AGGR_BARPEND;
2831 bar_start_timer(tap);
2837 bar_start_timer(struct ieee80211_tx_ampdu *tap)
2839 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2843 callout_reset(&tap->txa_timer, ieee80211_bar_timeout, bar_timeout, tap);
2847 bar_stop_timer(struct ieee80211_tx_ampdu *tap)
2849 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2853 callout_stop(&tap->txa_timer);
2857 bar_tx_complete(struct ieee80211_node *ni, void *arg, int status)
2859 struct ieee80211_tx_ampdu *tap = arg;
2861 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2862 ni, "%s: tid %u flags 0x%x pending %d status %d",
2863 __func__, tap->txa_tid, tap->txa_flags,
2864 callout_pending(&tap->txa_timer), status);
2866 ni->ni_vap->iv_stats.is_ampdu_bar_tx++;
2868 if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) &&
2869 callout_pending(&tap->txa_timer)) {
2870 struct ieee80211com *ic = ni->ni_ic;
2872 if (status == 0) /* ACK'd */
2873 bar_stop_timer(tap);
2874 ic->ic_bar_response(ni, tap, status);
2875 /* NB: just let timer expire so we pace requests */
2880 ieee80211_bar_response(struct ieee80211_node *ni,
2881 struct ieee80211_tx_ampdu *tap, int status)
2884 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2888 if (status == 0) { /* got ACK */
2889 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2890 ni, "BAR moves BA win <%u:%u> (%u frames) txseq %u tid %u",
2892 IEEE80211_SEQ_ADD(tap->txa_start, tap->txa_wnd-1),
2893 tap->txa_qframes, tap->txa_seqpending,
2896 /* NB: timer already stopped in bar_tx_complete */
2897 tap->txa_start = tap->txa_seqpending;
2898 tap->txa_flags &= ~IEEE80211_AGGR_BARPEND;
2903 * Transmit a BAR frame to the specified node. The
2904 * BAR contents are drawn from the supplied aggregation
2905 * state associated with the node.
2907 * NB: we only handle immediate ACK w/ compressed bitmap.
2910 ieee80211_send_bar(struct ieee80211_node *ni,
2911 struct ieee80211_tx_ampdu *tap, ieee80211_seq seq)
2913 #define senderr(_x, _v) do { vap->iv_stats._v++; ret = _x; goto bad; } while (0)
2914 struct ieee80211vap *vap = ni->ni_vap;
2915 struct ieee80211com *ic = ni->ni_ic;
2916 struct ieee80211_frame_bar *bar;
2918 uint16_t barctl, barseqctl;
2922 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2927 if ((tap->txa_flags & IEEE80211_AGGR_RUNNING) == 0) {
2928 /* no ADDBA response, should not happen */
2933 bar_stop_timer(tap);
2935 ieee80211_ref_node(ni);
2937 m = ieee80211_getmgtframe(&frm, ic->ic_headroom, sizeof(*bar));
2939 senderr(ENOMEM, is_tx_nobuf);
2941 if (!ieee80211_add_callback(m, bar_tx_complete, tap)) {
2943 senderr(ENOMEM, is_tx_nobuf); /* XXX */
2947 bar = mtod(m, struct ieee80211_frame_bar *);
2948 bar->i_fc[0] = IEEE80211_FC0_VERSION_0 |
2949 IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_BAR;
2951 IEEE80211_ADDR_COPY(bar->i_ra, ni->ni_macaddr);
2952 IEEE80211_ADDR_COPY(bar->i_ta, vap->iv_myaddr);
2955 barctl = (tap->txa_flags & IEEE80211_AGGR_IMMEDIATE ?
2956 0 : IEEE80211_BAR_NOACK)
2957 | IEEE80211_BAR_COMP
2958 | _IEEE80211_SHIFTMASK(tid, IEEE80211_BAR_TID)
2960 barseqctl = _IEEE80211_SHIFTMASK(seq, IEEE80211_BAR_SEQ_START);
2961 /* NB: known to have proper alignment */
2962 bar->i_ctl = htole16(barctl);
2963 bar->i_seq = htole16(barseqctl);
2964 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_bar);
2966 M_WME_SETAC(m, WME_AC_VO);
2968 IEEE80211_NODE_STAT(ni, tx_mgmt); /* XXX tx_ctl? */
2971 /* init/bump attempts counter */
2972 if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) == 0)
2973 tap->txa_attempts = 1;
2975 tap->txa_attempts++;
2976 tap->txa_seqpending = seq;
2977 tap->txa_flags |= IEEE80211_AGGR_BARPEND;
2979 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_11N,
2980 ni, "send BAR: tid %u ctl 0x%x start %u (attempt %d)",
2981 tid, barctl, seq, tap->txa_attempts);
2984 * ic_raw_xmit will free the node reference
2985 * regardless of queue/TX success or failure.
2987 IEEE80211_TX_LOCK(ic);
2988 ret = ieee80211_raw_output(vap, ni, m, NULL);
2989 IEEE80211_TX_UNLOCK(ic);
2991 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_11N,
2992 ni, "send BAR: failed: (ret = %d)\n",
2994 /* xmit failed, clear state flag */
2995 tap->txa_flags &= ~IEEE80211_AGGR_BARPEND;
2996 vap->iv_stats.is_ampdu_bar_tx_fail++;
2999 /* XXX hack against tx complete happening before timer is started */
3000 if (tap->txa_flags & IEEE80211_AGGR_BARPEND)
3001 bar_start_timer(tap);
3004 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
3008 vap->iv_stats.is_ampdu_bar_tx_fail++;
3009 ieee80211_free_node(ni);
3015 ht_action_output(struct ieee80211_node *ni, struct mbuf *m)
3017 struct ieee80211_bpf_params params;
3019 memset(¶ms, 0, sizeof(params));
3020 params.ibp_pri = WME_AC_VO;
3021 params.ibp_rate0 = ni->ni_txparms->mgmtrate;
3022 /* NB: we know all frames are unicast */
3023 params.ibp_try0 = ni->ni_txparms->maxretry;
3024 params.ibp_power = ni->ni_txpower;
3025 return ieee80211_mgmt_output(ni, m, IEEE80211_FC0_SUBTYPE_ACTION,
3029 #define ADDSHORT(frm, v) do { \
3030 frm[0] = (v) & 0xff; \
3031 frm[1] = (v) >> 8; \
3036 * Send an action management frame. The arguments are stuff
3037 * into a frame without inspection; the caller is assumed to
3038 * prepare them carefully (e.g. based on the aggregation state).
3041 ht_send_action_ba_addba(struct ieee80211_node *ni,
3042 int category, int action, void *arg0)
3044 struct ieee80211vap *vap = ni->ni_vap;
3045 struct ieee80211com *ic = ni->ni_ic;
3046 uint16_t *args = arg0;
3050 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
3051 "send ADDBA %s: dialogtoken %d status %d "
3052 "baparamset 0x%x (tid %d amsdu %d) batimeout 0x%x baseqctl 0x%x",
3053 (action == IEEE80211_ACTION_BA_ADDBA_REQUEST) ?
3054 "request" : "response", args[0], args[1], args[2],
3055 _IEEE80211_MASKSHIFT(args[2], IEEE80211_BAPS_TID),
3056 _IEEE80211_MASKSHIFT(args[2], IEEE80211_BAPS_AMSDU),
3059 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
3060 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
3061 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
3062 ieee80211_ref_node(ni);
3064 m = ieee80211_getmgtframe(&frm,
3065 ic->ic_headroom + sizeof(struct ieee80211_frame),
3066 sizeof(uint16_t) /* action+category */
3067 /* XXX may action payload */
3068 + sizeof(struct ieee80211_action_ba_addbaresponse)
3073 *frm++ = args[0]; /* dialog token */
3074 if (action == IEEE80211_ACTION_BA_ADDBA_RESPONSE)
3075 ADDSHORT(frm, args[1]); /* status code */
3076 ADDSHORT(frm, args[2]); /* baparamset */
3077 ADDSHORT(frm, args[3]); /* batimeout */
3078 if (action == IEEE80211_ACTION_BA_ADDBA_REQUEST)
3079 ADDSHORT(frm, args[4]); /* baseqctl */
3080 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
3081 return ht_action_output(ni, m);
3083 vap->iv_stats.is_tx_nobuf++;
3084 ieee80211_free_node(ni);
3090 ht_send_action_ba_delba(struct ieee80211_node *ni,
3091 int category, int action, void *arg0)
3093 struct ieee80211vap *vap = ni->ni_vap;
3094 struct ieee80211com *ic = ni->ni_ic;
3095 uint16_t *args = arg0;
3097 uint16_t baparamset;
3100 baparamset = _IEEE80211_SHIFTMASK(args[0], IEEE80211_DELBAPS_TID)
3103 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
3104 "send DELBA action: tid %d, initiator %d reason %d (%s)",
3105 args[0], args[1], args[2], ieee80211_reason_to_string(args[2]));
3107 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
3108 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
3109 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
3110 ieee80211_ref_node(ni);
3112 m = ieee80211_getmgtframe(&frm,
3113 ic->ic_headroom + sizeof(struct ieee80211_frame),
3114 sizeof(uint16_t) /* action+category */
3115 /* XXX may action payload */
3116 + sizeof(struct ieee80211_action_ba_addbaresponse)
3121 ADDSHORT(frm, baparamset);
3122 ADDSHORT(frm, args[2]); /* reason code */
3123 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
3124 return ht_action_output(ni, m);
3126 vap->iv_stats.is_tx_nobuf++;
3127 ieee80211_free_node(ni);
3133 ht_send_action_ht_txchwidth(struct ieee80211_node *ni,
3134 int category, int action, void *arg0)
3136 struct ieee80211vap *vap = ni->ni_vap;
3137 struct ieee80211com *ic = ni->ni_ic;
3141 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
3142 "send HT txchwidth: width %d",
3143 IEEE80211_IS_CHAN_HT40(ni->ni_chan) ? 40 : 20);
3145 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
3146 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
3147 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
3148 ieee80211_ref_node(ni);
3150 m = ieee80211_getmgtframe(&frm,
3151 ic->ic_headroom + sizeof(struct ieee80211_frame),
3152 sizeof(uint16_t) /* action+category */
3153 /* XXX may action payload */
3154 + sizeof(struct ieee80211_action_ba_addbaresponse)
3159 *frm++ = IEEE80211_IS_CHAN_HT40(ni->ni_chan) ?
3160 IEEE80211_A_HT_TXCHWIDTH_2040 :
3161 IEEE80211_A_HT_TXCHWIDTH_20;
3162 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
3163 return ht_action_output(ni, m);
3165 vap->iv_stats.is_tx_nobuf++;
3166 ieee80211_free_node(ni);
3173 * Construct the MCS bit mask for inclusion in an HT capabilities
3174 * information element.
3177 ieee80211_set_mcsset(struct ieee80211com *ic, uint8_t *frm)
3182 KASSERT((ic->ic_rxstream > 0 && ic->ic_rxstream <= 4),
3183 ("ic_rxstream %d out of range", ic->ic_rxstream));
3184 KASSERT((ic->ic_txstream > 0 && ic->ic_txstream <= 4),
3185 ("ic_txstream %d out of range", ic->ic_txstream));
3187 for (i = 0; i < ic->ic_rxstream * 8; i++)
3189 if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
3190 (ic->ic_htcaps & IEEE80211_HTC_RXMCS32))
3192 if (ic->ic_htcaps & IEEE80211_HTC_RXUNEQUAL) {
3193 if (ic->ic_rxstream >= 2) {
3194 for (i = 33; i <= 38; i++)
3197 if (ic->ic_rxstream >= 3) {
3198 for (i = 39; i <= 52; i++)
3201 if (ic->ic_rxstream >= 4) {
3202 for (i = 53; i <= 76; i++)
3207 txparams = 0x1; /* TX MCS set defined */
3208 if (ic->ic_rxstream != ic->ic_txstream) {
3209 txparams |= 0x2; /* TX RX MCS not equal */
3210 txparams |= (ic->ic_txstream - 1) << 2; /* num TX streams */
3211 if (ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL)
3212 txparams |= 0x16; /* TX unequal modulation sup */
3219 * Add body of an HTCAP information element.
3222 ieee80211_add_htcap_body(uint8_t *frm, struct ieee80211_node *ni)
3224 #define ADDSHORT(frm, v) do { \
3225 frm[0] = (v) & 0xff; \
3226 frm[1] = (v) >> 8; \
3229 struct ieee80211com *ic = ni->ni_ic;
3230 struct ieee80211vap *vap = ni->ni_vap;
3231 uint16_t caps, extcaps;
3234 /* HT capabilities */
3235 caps = vap->iv_htcaps & 0xffff;
3237 * Note channel width depends on whether we are operating as
3238 * a sta or not. When operating as a sta we are generating
3239 * a request based on our desired configuration. Otherwise
3240 * we are operational and the channel attributes identify
3241 * how we've been setup (which might be different if a fixed
3242 * channel is specified).
3244 if (vap->iv_opmode == IEEE80211_M_STA) {
3245 /* override 20/40 use based on config */
3246 if (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)
3247 caps |= IEEE80211_HTCAP_CHWIDTH40;
3249 caps &= ~IEEE80211_HTCAP_CHWIDTH40;
3251 /* Start by using the advertised settings */
3252 rxmax = _IEEE80211_MASKSHIFT(ni->ni_htparam,
3253 IEEE80211_HTCAP_MAXRXAMPDU);
3254 density = _IEEE80211_MASKSHIFT(ni->ni_htparam,
3255 IEEE80211_HTCAP_MPDUDENSITY);
3257 IEEE80211_DPRINTF(vap, IEEE80211_MSG_11N,
3258 "%s: advertised rxmax=%d, density=%d, vap rxmax=%d, density=%d\n",
3262 vap->iv_ampdu_rxmax,
3263 vap->iv_ampdu_density);
3265 /* Cap at VAP rxmax */
3266 if (rxmax > vap->iv_ampdu_rxmax)
3267 rxmax = vap->iv_ampdu_rxmax;
3270 * If the VAP ampdu density value greater, use that.
3272 * (Larger density value == larger minimum gap between A-MPDU
3275 if (vap->iv_ampdu_density > density)
3276 density = vap->iv_ampdu_density;
3279 * NB: Hardware might support HT40 on some but not all
3280 * channels. We can't determine this earlier because only
3281 * after association the channel is upgraded to HT based
3282 * on the negotiated capabilities.
3284 if (ni->ni_chan != IEEE80211_CHAN_ANYC &&
3285 findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT40U) == NULL &&
3286 findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT40D) == NULL)
3287 caps &= ~IEEE80211_HTCAP_CHWIDTH40;
3289 /* override 20/40 use based on current channel */
3290 if (IEEE80211_IS_CHAN_HT40(ni->ni_chan))
3291 caps |= IEEE80211_HTCAP_CHWIDTH40;
3293 caps &= ~IEEE80211_HTCAP_CHWIDTH40;
3295 /* XXX TODO should it start by using advertised settings? */
3296 rxmax = vap->iv_ampdu_rxmax;
3297 density = vap->iv_ampdu_density;
3300 /* adjust short GI based on channel and config */
3301 if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20) == 0)
3302 caps &= ~IEEE80211_HTCAP_SHORTGI20;
3303 if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40) == 0 ||
3304 (caps & IEEE80211_HTCAP_CHWIDTH40) == 0)
3305 caps &= ~IEEE80211_HTCAP_SHORTGI40;
3307 /* adjust STBC based on receive capabilities */
3308 if ((vap->iv_flags_ht & IEEE80211_FHT_STBC_RX) == 0)
3309 caps &= ~IEEE80211_HTCAP_RXSTBC;
3311 /* adjust LDPC based on receive capabilites */
3312 if ((vap->iv_flags_ht & IEEE80211_FHT_LDPC_RX) == 0)
3313 caps &= ~IEEE80211_HTCAP_LDPC;
3315 ADDSHORT(frm, caps);
3318 *frm = _IEEE80211_SHIFTMASK(rxmax, IEEE80211_HTCAP_MAXRXAMPDU)
3319 | _IEEE80211_SHIFTMASK(density, IEEE80211_HTCAP_MPDUDENSITY)
3323 /* pre-zero remainder of ie */
3324 memset(frm, 0, sizeof(struct ieee80211_ie_htcap) -
3325 __offsetof(struct ieee80211_ie_htcap, hc_mcsset));
3327 /* supported MCS set */
3329 * XXX: For sta mode the rate set should be restricted based
3330 * on the AP's capabilities, but ni_htrates isn't setup when
3331 * we're called to form an AssocReq frame so for now we're
3332 * restricted to the device capabilities.
3334 ieee80211_set_mcsset(ni->ni_ic, frm);
3336 frm += __offsetof(struct ieee80211_ie_htcap, hc_extcap) -
3337 __offsetof(struct ieee80211_ie_htcap, hc_mcsset);
3339 /* HT extended capabilities */
3340 extcaps = vap->iv_htextcaps & 0xffff;
3342 ADDSHORT(frm, extcaps);
3344 frm += sizeof(struct ieee80211_ie_htcap) -
3345 __offsetof(struct ieee80211_ie_htcap, hc_txbf);
3352 * Add 802.11n HT capabilities information element
3355 ieee80211_add_htcap(uint8_t *frm, struct ieee80211_node *ni)
3357 frm[0] = IEEE80211_ELEMID_HTCAP;
3358 frm[1] = sizeof(struct ieee80211_ie_htcap) - 2;
3359 return ieee80211_add_htcap_body(frm + 2, ni);
3363 * Non-associated probe request - add HT capabilities based on
3364 * the current channel configuration.
3367 ieee80211_add_htcap_body_ch(uint8_t *frm, struct ieee80211vap *vap,
3368 struct ieee80211_channel *c)
3370 #define ADDSHORT(frm, v) do { \
3371 frm[0] = (v) & 0xff; \
3372 frm[1] = (v) >> 8; \
3375 struct ieee80211com *ic = vap->iv_ic;
3376 uint16_t caps, extcaps;
3379 /* HT capabilities */
3380 caps = vap->iv_htcaps & 0xffff;
3383 * We don't use this in STA mode; only in IBSS mode.
3384 * So in IBSS mode we base our HTCAP flags on the
3388 /* override 20/40 use based on current channel */
3389 if (IEEE80211_IS_CHAN_HT40(c))
3390 caps |= IEEE80211_HTCAP_CHWIDTH40;
3392 caps &= ~IEEE80211_HTCAP_CHWIDTH40;
3394 /* Use the currently configured values */
3395 rxmax = vap->iv_ampdu_rxmax;
3396 density = vap->iv_ampdu_density;
3398 /* adjust short GI based on channel and config */
3399 if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20) == 0)
3400 caps &= ~IEEE80211_HTCAP_SHORTGI20;
3401 if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40) == 0 ||
3402 (caps & IEEE80211_HTCAP_CHWIDTH40) == 0)
3403 caps &= ~IEEE80211_HTCAP_SHORTGI40;
3404 ADDSHORT(frm, caps);
3407 *frm = _IEEE80211_SHIFTMASK(rxmax, IEEE80211_HTCAP_MAXRXAMPDU)
3408 | _IEEE80211_SHIFTMASK(density, IEEE80211_HTCAP_MPDUDENSITY)
3412 /* pre-zero remainder of ie */
3413 memset(frm, 0, sizeof(struct ieee80211_ie_htcap) -
3414 __offsetof(struct ieee80211_ie_htcap, hc_mcsset));
3416 /* supported MCS set */
3418 * XXX: For sta mode the rate set should be restricted based
3419 * on the AP's capabilities, but ni_htrates isn't setup when
3420 * we're called to form an AssocReq frame so for now we're
3421 * restricted to the device capabilities.
3423 ieee80211_set_mcsset(ic, frm);
3425 frm += __offsetof(struct ieee80211_ie_htcap, hc_extcap) -
3426 __offsetof(struct ieee80211_ie_htcap, hc_mcsset);
3428 /* HT extended capabilities */
3429 extcaps = vap->iv_htextcaps & 0xffff;
3431 ADDSHORT(frm, extcaps);
3433 frm += sizeof(struct ieee80211_ie_htcap) -
3434 __offsetof(struct ieee80211_ie_htcap, hc_txbf);
3441 * Add 802.11n HT capabilities information element
3444 ieee80211_add_htcap_ch(uint8_t *frm, struct ieee80211vap *vap,
3445 struct ieee80211_channel *c)
3447 frm[0] = IEEE80211_ELEMID_HTCAP;
3448 frm[1] = sizeof(struct ieee80211_ie_htcap) - 2;
3449 return ieee80211_add_htcap_body_ch(frm + 2, vap, c);
3453 * Add Broadcom OUI wrapped standard HTCAP ie; this is
3454 * used for compatibility w/ pre-draft implementations.
3457 ieee80211_add_htcap_vendor(uint8_t *frm, struct ieee80211_node *ni)
3459 frm[0] = IEEE80211_ELEMID_VENDOR;
3460 frm[1] = 4 + sizeof(struct ieee80211_ie_htcap) - 2;
3461 frm[2] = (BCM_OUI >> 0) & 0xff;
3462 frm[3] = (BCM_OUI >> 8) & 0xff;
3463 frm[4] = (BCM_OUI >> 16) & 0xff;
3464 frm[5] = BCM_OUI_HTCAP;
3465 return ieee80211_add_htcap_body(frm + 6, ni);
3469 * Construct the MCS bit mask of basic rates
3470 * for inclusion in an HT information element.
3473 ieee80211_set_basic_htrates(uint8_t *frm, const struct ieee80211_htrateset *rs)
3477 for (i = 0; i < rs->rs_nrates; i++) {
3478 int r = rs->rs_rates[i] & IEEE80211_RATE_VAL;
3479 if ((rs->rs_rates[i] & IEEE80211_RATE_BASIC) &&
3480 r < IEEE80211_HTRATE_MAXSIZE) {
3481 /* NB: this assumes a particular implementation */
3488 * Update the HTINFO ie for a beacon frame.
3491 ieee80211_ht_update_beacon(struct ieee80211vap *vap,
3492 struct ieee80211_beacon_offsets *bo)
3494 #define PROTMODE (IEEE80211_HTINFO_OPMODE|IEEE80211_HTINFO_NONHT_PRESENT)
3495 struct ieee80211_node *ni;
3496 const struct ieee80211_channel *bsschan;
3497 struct ieee80211com *ic = vap->iv_ic;
3498 struct ieee80211_ie_htinfo *ht =
3499 (struct ieee80211_ie_htinfo *) bo->bo_htinfo;
3501 ni = ieee80211_ref_node(vap->iv_bss);
3502 bsschan = ni->ni_chan;
3504 /* XXX only update on channel change */
3505 ht->hi_ctrlchannel = ieee80211_chan2ieee(ic, bsschan);
3506 if (vap->iv_flags_ht & IEEE80211_FHT_RIFS)
3507 ht->hi_byte1 = IEEE80211_HTINFO_RIFSMODE_PERM;
3509 ht->hi_byte1 = IEEE80211_HTINFO_RIFSMODE_PROH;
3510 if (IEEE80211_IS_CHAN_HT40U(bsschan))
3511 ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_ABOVE;
3512 else if (IEEE80211_IS_CHAN_HT40D(bsschan))
3513 ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_BELOW;
3515 ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_NONE;
3516 if (IEEE80211_IS_CHAN_HT40(bsschan))
3517 ht->hi_byte1 |= IEEE80211_HTINFO_TXWIDTH_2040;
3519 /* protection mode */
3521 * XXX TODO: this uses the global flag, not the per-VAP flag.
3522 * Eventually (once the protection modes are done per-channel
3523 * rather than per-VAP) we can flip this over to be per-VAP but
3524 * using the channel protection mode.
3526 ht->hi_byte2 = (ht->hi_byte2 &~ PROTMODE) | ic->ic_curhtprotmode;
3528 ieee80211_free_node(ni);
3530 /* XXX propagate to vendor ie's */
3535 * Add body of an HTINFO information element.
3537 * NB: We don't use struct ieee80211_ie_htinfo because we can
3538 * be called to fillin both a standard ie and a compat ie that
3539 * has a vendor OUI at the front.
3542 ieee80211_add_htinfo_body(uint8_t *frm, struct ieee80211_node *ni)
3544 struct ieee80211vap *vap = ni->ni_vap;
3545 struct ieee80211com *ic = ni->ni_ic;
3547 /* pre-zero remainder of ie */
3548 memset(frm, 0, sizeof(struct ieee80211_ie_htinfo) - 2);
3550 /* primary/control channel center */
3551 *frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
3553 if (vap->iv_flags_ht & IEEE80211_FHT_RIFS)
3554 frm[0] = IEEE80211_HTINFO_RIFSMODE_PERM;
3556 frm[0] = IEEE80211_HTINFO_RIFSMODE_PROH;
3557 if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan))
3558 frm[0] |= IEEE80211_HTINFO_2NDCHAN_ABOVE;
3559 else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan))
3560 frm[0] |= IEEE80211_HTINFO_2NDCHAN_BELOW;
3562 frm[0] |= IEEE80211_HTINFO_2NDCHAN_NONE;
3563 if (IEEE80211_IS_CHAN_HT40(ni->ni_chan))
3564 frm[0] |= IEEE80211_HTINFO_TXWIDTH_2040;
3567 * Add current protection mode. Unlike for beacons,
3568 * this will respect the per-VAP flags.
3570 frm[1] = vap->iv_curhtprotmode;
3575 ieee80211_set_basic_htrates(frm, &ni->ni_htrates);
3576 frm += sizeof(struct ieee80211_ie_htinfo) -
3577 __offsetof(struct ieee80211_ie_htinfo, hi_basicmcsset);
3582 * Add 802.11n HT information element.
3585 ieee80211_add_htinfo(uint8_t *frm, struct ieee80211_node *ni)
3587 frm[0] = IEEE80211_ELEMID_HTINFO;
3588 frm[1] = sizeof(struct ieee80211_ie_htinfo) - 2;
3589 return ieee80211_add_htinfo_body(frm + 2, ni);
3593 * Add Broadcom OUI wrapped standard HTINFO ie; this is
3594 * used for compatibility w/ pre-draft implementations.
3597 ieee80211_add_htinfo_vendor(uint8_t *frm, struct ieee80211_node *ni)
3599 frm[0] = IEEE80211_ELEMID_VENDOR;
3600 frm[1] = 4 + sizeof(struct ieee80211_ie_htinfo) - 2;
3601 frm[2] = (BCM_OUI >> 0) & 0xff;
3602 frm[3] = (BCM_OUI >> 8) & 0xff;
3603 frm[4] = (BCM_OUI >> 16) & 0xff;
3604 frm[5] = BCM_OUI_HTINFO;
3605 return ieee80211_add_htinfo_body(frm + 6, ni);