2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2007-2008 Sam Leffler, Errno Consulting
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
34 * IEEE 802.11n protocol support.
40 #include <sys/param.h>
41 #include <sys/kernel.h>
42 #include <sys/malloc.h>
43 #include <sys/systm.h>
44 #include <sys/endian.h>
46 #include <sys/socket.h>
49 #include <net/if_var.h>
50 #include <net/if_media.h>
51 #include <net/ethernet.h>
53 #include <net80211/ieee80211_var.h>
54 #include <net80211/ieee80211_action.h>
55 #include <net80211/ieee80211_input.h>
57 const struct ieee80211_mcs_rates ieee80211_htrates[IEEE80211_HTRATE_MAXSIZE] = {
58 { 13, 14, 27, 30 }, /* MCS 0 */
59 { 26, 29, 54, 60 }, /* MCS 1 */
60 { 39, 43, 81, 90 }, /* MCS 2 */
61 { 52, 58, 108, 120 }, /* MCS 3 */
62 { 78, 87, 162, 180 }, /* MCS 4 */
63 { 104, 116, 216, 240 }, /* MCS 5 */
64 { 117, 130, 243, 270 }, /* MCS 6 */
65 { 130, 144, 270, 300 }, /* MCS 7 */
66 { 26, 29, 54, 60 }, /* MCS 8 */
67 { 52, 58, 108, 120 }, /* MCS 9 */
68 { 78, 87, 162, 180 }, /* MCS 10 */
69 { 104, 116, 216, 240 }, /* MCS 11 */
70 { 156, 173, 324, 360 }, /* MCS 12 */
71 { 208, 231, 432, 480 }, /* MCS 13 */
72 { 234, 260, 486, 540 }, /* MCS 14 */
73 { 260, 289, 540, 600 }, /* MCS 15 */
74 { 39, 43, 81, 90 }, /* MCS 16 */
75 { 78, 87, 162, 180 }, /* MCS 17 */
76 { 117, 130, 243, 270 }, /* MCS 18 */
77 { 156, 173, 324, 360 }, /* MCS 19 */
78 { 234, 260, 486, 540 }, /* MCS 20 */
79 { 312, 347, 648, 720 }, /* MCS 21 */
80 { 351, 390, 729, 810 }, /* MCS 22 */
81 { 390, 433, 810, 900 }, /* MCS 23 */
82 { 52, 58, 108, 120 }, /* MCS 24 */
83 { 104, 116, 216, 240 }, /* MCS 25 */
84 { 156, 173, 324, 360 }, /* MCS 26 */
85 { 208, 231, 432, 480 }, /* MCS 27 */
86 { 312, 347, 648, 720 }, /* MCS 28 */
87 { 416, 462, 864, 960 }, /* MCS 29 */
88 { 468, 520, 972, 1080 }, /* MCS 30 */
89 { 520, 578, 1080, 1200 }, /* MCS 31 */
90 { 0, 0, 12, 13 }, /* MCS 32 */
91 { 78, 87, 162, 180 }, /* MCS 33 */
92 { 104, 116, 216, 240 }, /* MCS 34 */
93 { 130, 144, 270, 300 }, /* MCS 35 */
94 { 117, 130, 243, 270 }, /* MCS 36 */
95 { 156, 173, 324, 360 }, /* MCS 37 */
96 { 195, 217, 405, 450 }, /* MCS 38 */
97 { 104, 116, 216, 240 }, /* MCS 39 */
98 { 130, 144, 270, 300 }, /* MCS 40 */
99 { 130, 144, 270, 300 }, /* MCS 41 */
100 { 156, 173, 324, 360 }, /* MCS 42 */
101 { 182, 202, 378, 420 }, /* MCS 43 */
102 { 182, 202, 378, 420 }, /* MCS 44 */
103 { 208, 231, 432, 480 }, /* MCS 45 */
104 { 156, 173, 324, 360 }, /* MCS 46 */
105 { 195, 217, 405, 450 }, /* MCS 47 */
106 { 195, 217, 405, 450 }, /* MCS 48 */
107 { 234, 260, 486, 540 }, /* MCS 49 */
108 { 273, 303, 567, 630 }, /* MCS 50 */
109 { 273, 303, 567, 630 }, /* MCS 51 */
110 { 312, 347, 648, 720 }, /* MCS 52 */
111 { 130, 144, 270, 300 }, /* MCS 53 */
112 { 156, 173, 324, 360 }, /* MCS 54 */
113 { 182, 202, 378, 420 }, /* MCS 55 */
114 { 156, 173, 324, 360 }, /* MCS 56 */
115 { 182, 202, 378, 420 }, /* MCS 57 */
116 { 208, 231, 432, 480 }, /* MCS 58 */
117 { 234, 260, 486, 540 }, /* MCS 59 */
118 { 208, 231, 432, 480 }, /* MCS 60 */
119 { 234, 260, 486, 540 }, /* MCS 61 */
120 { 260, 289, 540, 600 }, /* MCS 62 */
121 { 260, 289, 540, 600 }, /* MCS 63 */
122 { 286, 318, 594, 660 }, /* MCS 64 */
123 { 195, 217, 405, 450 }, /* MCS 65 */
124 { 234, 260, 486, 540 }, /* MCS 66 */
125 { 273, 303, 567, 630 }, /* MCS 67 */
126 { 234, 260, 486, 540 }, /* MCS 68 */
127 { 273, 303, 567, 630 }, /* MCS 69 */
128 { 312, 347, 648, 720 }, /* MCS 70 */
129 { 351, 390, 729, 810 }, /* MCS 71 */
130 { 312, 347, 648, 720 }, /* MCS 72 */
131 { 351, 390, 729, 810 }, /* MCS 73 */
132 { 390, 433, 810, 900 }, /* MCS 74 */
133 { 390, 433, 810, 900 }, /* MCS 75 */
134 { 429, 477, 891, 990 }, /* MCS 76 */
137 static int ieee80211_ampdu_age = -1; /* threshold for ampdu reorder q (ms) */
138 SYSCTL_PROC(_net_wlan, OID_AUTO, ampdu_age,
139 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
140 &ieee80211_ampdu_age, 0, ieee80211_sysctl_msecs_ticks, "I",
141 "AMPDU max reorder age (ms)");
143 static int ieee80211_recv_bar_ena = 1;
144 SYSCTL_INT(_net_wlan, OID_AUTO, recv_bar, CTLFLAG_RW, &ieee80211_recv_bar_ena,
145 0, "BAR frame processing (ena/dis)");
147 static int ieee80211_addba_timeout = -1;/* timeout for ADDBA response */
148 SYSCTL_PROC(_net_wlan, OID_AUTO, addba_timeout,
149 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
150 &ieee80211_addba_timeout, 0, ieee80211_sysctl_msecs_ticks, "I",
151 "ADDBA request timeout (ms)");
152 static int ieee80211_addba_backoff = -1;/* backoff after max ADDBA requests */
153 SYSCTL_PROC(_net_wlan, OID_AUTO, addba_backoff,
154 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
155 &ieee80211_addba_backoff, 0, ieee80211_sysctl_msecs_ticks, "I",
156 "ADDBA request backoff (ms)");
157 static int ieee80211_addba_maxtries = 3;/* max ADDBA requests before backoff */
158 SYSCTL_INT(_net_wlan, OID_AUTO, addba_maxtries, CTLFLAG_RW,
159 &ieee80211_addba_maxtries, 0, "max ADDBA requests sent before backoff");
161 static int ieee80211_bar_timeout = -1; /* timeout waiting for BAR response */
162 static int ieee80211_bar_maxtries = 50;/* max BAR requests before DELBA */
164 static ieee80211_recv_action_func ht_recv_action_ba_addba_request;
165 static ieee80211_recv_action_func ht_recv_action_ba_addba_response;
166 static ieee80211_recv_action_func ht_recv_action_ba_delba;
167 static ieee80211_recv_action_func ht_recv_action_ht_mimopwrsave;
168 static ieee80211_recv_action_func ht_recv_action_ht_txchwidth;
170 static ieee80211_send_action_func ht_send_action_ba_addba;
171 static ieee80211_send_action_func ht_send_action_ba_delba;
172 static ieee80211_send_action_func ht_send_action_ht_txchwidth;
175 ieee80211_ht_init(void)
178 * Setup HT parameters that depends on the clock frequency.
180 ieee80211_ampdu_age = msecs_to_ticks(500);
181 ieee80211_addba_timeout = msecs_to_ticks(250);
182 ieee80211_addba_backoff = msecs_to_ticks(10*1000);
183 ieee80211_bar_timeout = msecs_to_ticks(250);
185 * Register action frame handlers.
187 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_BA,
188 IEEE80211_ACTION_BA_ADDBA_REQUEST, ht_recv_action_ba_addba_request);
189 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_BA,
190 IEEE80211_ACTION_BA_ADDBA_RESPONSE, ht_recv_action_ba_addba_response);
191 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_BA,
192 IEEE80211_ACTION_BA_DELBA, ht_recv_action_ba_delba);
193 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_HT,
194 IEEE80211_ACTION_HT_MIMOPWRSAVE, ht_recv_action_ht_mimopwrsave);
195 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_HT,
196 IEEE80211_ACTION_HT_TXCHWIDTH, ht_recv_action_ht_txchwidth);
198 ieee80211_send_action_register(IEEE80211_ACTION_CAT_BA,
199 IEEE80211_ACTION_BA_ADDBA_REQUEST, ht_send_action_ba_addba);
200 ieee80211_send_action_register(IEEE80211_ACTION_CAT_BA,
201 IEEE80211_ACTION_BA_ADDBA_RESPONSE, ht_send_action_ba_addba);
202 ieee80211_send_action_register(IEEE80211_ACTION_CAT_BA,
203 IEEE80211_ACTION_BA_DELBA, ht_send_action_ba_delba);
204 ieee80211_send_action_register(IEEE80211_ACTION_CAT_HT,
205 IEEE80211_ACTION_HT_TXCHWIDTH, ht_send_action_ht_txchwidth);
207 SYSINIT(wlan_ht, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_ht_init, NULL);
209 static int ieee80211_ampdu_enable(struct ieee80211_node *ni,
210 struct ieee80211_tx_ampdu *tap);
211 static int ieee80211_addba_request(struct ieee80211_node *ni,
212 struct ieee80211_tx_ampdu *tap,
213 int dialogtoken, int baparamset, int batimeout);
214 static int ieee80211_addba_response(struct ieee80211_node *ni,
215 struct ieee80211_tx_ampdu *tap,
216 int code, int baparamset, int batimeout);
217 static void ieee80211_addba_stop(struct ieee80211_node *ni,
218 struct ieee80211_tx_ampdu *tap);
219 static void null_addba_response_timeout(struct ieee80211_node *ni,
220 struct ieee80211_tx_ampdu *tap);
222 static void ieee80211_bar_response(struct ieee80211_node *ni,
223 struct ieee80211_tx_ampdu *tap, int status);
224 static void ampdu_tx_stop(struct ieee80211_tx_ampdu *tap);
225 static void bar_stop_timer(struct ieee80211_tx_ampdu *tap);
226 static int ampdu_rx_start(struct ieee80211_node *, struct ieee80211_rx_ampdu *,
227 int baparamset, int batimeout, int baseqctl);
228 static void ampdu_rx_stop(struct ieee80211_node *, struct ieee80211_rx_ampdu *);
231 ieee80211_ht_attach(struct ieee80211com *ic)
233 /* setup default aggregation policy */
234 ic->ic_recv_action = ieee80211_recv_action;
235 ic->ic_send_action = ieee80211_send_action;
236 ic->ic_ampdu_enable = ieee80211_ampdu_enable;
237 ic->ic_addba_request = ieee80211_addba_request;
238 ic->ic_addba_response = ieee80211_addba_response;
239 ic->ic_addba_response_timeout = null_addba_response_timeout;
240 ic->ic_addba_stop = ieee80211_addba_stop;
241 ic->ic_bar_response = ieee80211_bar_response;
242 ic->ic_ampdu_rx_start = ampdu_rx_start;
243 ic->ic_ampdu_rx_stop = ampdu_rx_stop;
245 ic->ic_htprotmode = IEEE80211_PROT_RTSCTS;
246 ic->ic_curhtprotmode = IEEE80211_HTINFO_OPMODE_PURE;
250 ieee80211_ht_detach(struct ieee80211com *ic)
255 ieee80211_ht_vattach(struct ieee80211vap *vap)
258 /* driver can override defaults */
259 vap->iv_ampdu_rxmax = IEEE80211_HTCAP_MAXRXAMPDU_8K;
260 vap->iv_ampdu_density = IEEE80211_HTCAP_MPDUDENSITY_NA;
261 vap->iv_ampdu_limit = vap->iv_ampdu_rxmax;
262 vap->iv_amsdu_limit = vap->iv_htcaps & IEEE80211_HTCAP_MAXAMSDU;
263 /* tx aggregation traffic thresholds */
264 vap->iv_ampdu_mintraffic[WME_AC_BK] = 128;
265 vap->iv_ampdu_mintraffic[WME_AC_BE] = 64;
266 vap->iv_ampdu_mintraffic[WME_AC_VO] = 32;
267 vap->iv_ampdu_mintraffic[WME_AC_VI] = 32;
269 vap->iv_htprotmode = IEEE80211_PROT_RTSCTS;
270 vap->iv_curhtprotmode = IEEE80211_HTINFO_OPMODE_PURE;
272 if (vap->iv_htcaps & IEEE80211_HTC_HT) {
274 * Device is HT capable; enable all HT-related
275 * facilities by default.
276 * XXX these choices may be too aggressive.
278 vap->iv_flags_ht |= IEEE80211_FHT_HT
279 | IEEE80211_FHT_HTCOMPAT
281 if (vap->iv_htcaps & IEEE80211_HTCAP_SHORTGI20)
282 vap->iv_flags_ht |= IEEE80211_FHT_SHORTGI20;
283 /* XXX infer from channel list? */
284 if (vap->iv_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
285 vap->iv_flags_ht |= IEEE80211_FHT_USEHT40;
286 if (vap->iv_htcaps & IEEE80211_HTCAP_SHORTGI40)
287 vap->iv_flags_ht |= IEEE80211_FHT_SHORTGI40;
289 /* enable RIFS if capable */
290 if (vap->iv_htcaps & IEEE80211_HTC_RIFS)
291 vap->iv_flags_ht |= IEEE80211_FHT_RIFS;
293 /* NB: A-MPDU and A-MSDU rx are mandated, these are tx only */
294 vap->iv_flags_ht |= IEEE80211_FHT_AMPDU_RX;
295 if (vap->iv_htcaps & IEEE80211_HTC_AMPDU)
296 vap->iv_flags_ht |= IEEE80211_FHT_AMPDU_TX;
297 vap->iv_flags_ht |= IEEE80211_FHT_AMSDU_RX;
298 if (vap->iv_htcaps & IEEE80211_HTC_AMSDU)
299 vap->iv_flags_ht |= IEEE80211_FHT_AMSDU_TX;
301 if (vap->iv_htcaps & IEEE80211_HTCAP_TXSTBC)
302 vap->iv_flags_ht |= IEEE80211_FHT_STBC_TX;
303 if (vap->iv_htcaps & IEEE80211_HTCAP_RXSTBC)
304 vap->iv_flags_ht |= IEEE80211_FHT_STBC_RX;
306 if (vap->iv_htcaps & IEEE80211_HTCAP_LDPC)
307 vap->iv_flags_ht |= IEEE80211_FHT_LDPC_RX;
308 if (vap->iv_htcaps & IEEE80211_HTC_TXLDPC)
309 vap->iv_flags_ht |= IEEE80211_FHT_LDPC_TX;
311 /* NB: disable default legacy WDS, too many issues right now */
312 if (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY)
313 vap->iv_flags_ht &= ~IEEE80211_FHT_HT;
317 ieee80211_ht_vdetach(struct ieee80211vap *vap)
322 ht_getrate(struct ieee80211com *ic, int index, enum ieee80211_phymode mode,
327 mword = ieee80211_rate2media(ic, index | IEEE80211_RATE_MCS, mode);
328 if (IFM_SUBTYPE(mword) != IFM_IEEE80211_MCS)
332 rate = ieee80211_htrates[index].ht20_rate_800ns;
335 rate = ieee80211_htrates[index].ht20_rate_400ns;
338 rate = ieee80211_htrates[index].ht40_rate_800ns;
341 rate = ieee80211_htrates[index].ht40_rate_400ns;
347 static struct printranges {
358 { 32, 0, 1, 2, IEEE80211_HTC_TXMCS32 },
359 { 33, 38, 2, 0, IEEE80211_HTC_TXUNEQUAL },
360 { 39, 52, 3, 0, IEEE80211_HTC_TXUNEQUAL },
361 { 53, 76, 4, 0, IEEE80211_HTC_TXUNEQUAL },
366 ht_rateprint(struct ieee80211com *ic, enum ieee80211_phymode mode, int ratetype)
368 int minrate, maxrate;
369 struct printranges *range;
371 for (range = ranges; range->txstream != 0; range++) {
372 if (ic->ic_txstream < range->txstream)
374 if (range->htcapflags &&
375 (ic->ic_htcaps & range->htcapflags) == 0)
377 if (ratetype < range->ratetype)
379 minrate = ht_getrate(ic, range->minmcs, mode, ratetype);
380 maxrate = ht_getrate(ic, range->maxmcs, mode, ratetype);
382 ic_printf(ic, "MCS %d-%d: %d%sMbps - %d%sMbps\n",
383 range->minmcs, range->maxmcs,
384 minrate/2, ((minrate & 0x1) != 0 ? ".5" : ""),
385 maxrate/2, ((maxrate & 0x1) != 0 ? ".5" : ""));
387 ic_printf(ic, "MCS %d: %d%sMbps\n", range->minmcs,
388 minrate/2, ((minrate & 0x1) != 0 ? ".5" : ""));
394 ht_announce(struct ieee80211com *ic, enum ieee80211_phymode mode)
396 const char *modestr = ieee80211_phymode_name[mode];
398 ic_printf(ic, "%s MCS 20MHz\n", modestr);
399 ht_rateprint(ic, mode, 0);
400 if (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20) {
401 ic_printf(ic, "%s MCS 20MHz SGI\n", modestr);
402 ht_rateprint(ic, mode, 1);
404 if (ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
405 ic_printf(ic, "%s MCS 40MHz:\n", modestr);
406 ht_rateprint(ic, mode, 2);
408 if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
409 (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40)) {
410 ic_printf(ic, "%s MCS 40MHz SGI:\n", modestr);
411 ht_rateprint(ic, mode, 3);
416 ieee80211_ht_announce(struct ieee80211com *ic)
419 if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) ||
420 isset(ic->ic_modecaps, IEEE80211_MODE_11NG))
421 ic_printf(ic, "%dT%dR\n", ic->ic_txstream, ic->ic_rxstream);
422 if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA))
423 ht_announce(ic, IEEE80211_MODE_11NA);
424 if (isset(ic->ic_modecaps, IEEE80211_MODE_11NG))
425 ht_announce(ic, IEEE80211_MODE_11NG);
429 ieee80211_init_suphtrates(struct ieee80211com *ic)
431 #define ADDRATE(x) do { \
432 htrateset->rs_rates[htrateset->rs_nrates] = x; \
433 htrateset->rs_nrates++; \
435 struct ieee80211_htrateset *htrateset = &ic->ic_sup_htrates;
438 memset(htrateset, 0, sizeof(struct ieee80211_htrateset));
439 for (i = 0; i < ic->ic_txstream * 8; i++)
441 if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
442 (ic->ic_htcaps & IEEE80211_HTC_TXMCS32))
444 if (ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL) {
445 if (ic->ic_txstream >= 2) {
446 for (i = 33; i <= 38; i++)
449 if (ic->ic_txstream >= 3) {
450 for (i = 39; i <= 52; i++)
453 if (ic->ic_txstream == 4) {
454 for (i = 53; i <= 76; i++)
462 * Receive processing.
466 * Decap the encapsulated A-MSDU frames and dispatch all but
467 * the last for delivery. The last frame is returned for
468 * delivery via the normal path.
471 ieee80211_decap_amsdu(struct ieee80211_node *ni, struct mbuf *m)
473 struct ieee80211vap *vap = ni->ni_vap;
477 /* discard 802.3 header inserted by ieee80211_decap */
478 m_adj(m, sizeof(struct ether_header));
480 vap->iv_stats.is_amsdu_decap++;
484 * Decap the first frame, bust it apart from the
485 * remainder and deliver. We leave the last frame
486 * delivery to the caller (for consistency with other
487 * code paths, could also do it here).
489 m = ieee80211_decap1(m, &framelen);
491 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
492 ni->ni_macaddr, "a-msdu", "%s", "decap failed");
493 vap->iv_stats.is_amsdu_tooshort++;
496 if (m->m_pkthdr.len == framelen)
498 n = m_split(m, framelen, M_NOWAIT);
500 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
501 ni->ni_macaddr, "a-msdu",
502 "%s", "unable to split encapsulated frames");
503 vap->iv_stats.is_amsdu_split++;
504 m_freem(m); /* NB: must reclaim */
507 vap->iv_deliver_data(vap, ni, m);
510 * Remove frame contents; each intermediate frame
511 * is required to be aligned to a 4-byte boundary.
514 m_adj(m, roundup2(framelen, 4) - framelen); /* padding */
516 return m; /* last delivered by caller */
520 ampdu_rx_purge_slot(struct ieee80211_rx_ampdu *rap, int i)
524 /* Walk the queue, removing frames as appropriate */
525 while (mbufq_len(&rap->rxa_mq[i]) != 0) {
526 m = mbufq_dequeue(&rap->rxa_mq[i]);
529 rap->rxa_qbytes -= m->m_pkthdr.len;
536 * Add the given frame to the current RX reorder slot.
538 * For future offloaded A-MSDU handling where multiple frames with
539 * the same sequence number show up here, this routine will append
540 * those frames as long as they're appropriately tagged.
543 ampdu_rx_add_slot(struct ieee80211_rx_ampdu *rap, int off, int tid,
545 struct ieee80211_node *ni,
547 const struct ieee80211_rx_stats *rxs)
549 const struct ieee80211_rx_stats *rxs_final = NULL;
550 struct ieee80211vap *vap = ni->ni_vap;
552 #define PROCESS 0 /* caller should process frame */
553 #define CONSUMED 1 /* frame consumed, caller does nothing */
556 * Figure out if this is a duplicate frame for the given slot.
558 * We're assuming that the driver will hand us all the frames
559 * for a given AMSDU decap pass and if we get /a/ frame
560 * for an AMSDU decap then we'll get all of them.
562 * The tricksy bit is that we don't know when the /end/ of
563 * the decap pass is, because we aren't tracking state here
564 * per-slot to know that we've finished receiving the frame list.
566 * The driver sets RX_F_AMSDU and RX_F_AMSDU_MORE to tell us
567 * what's going on; so ideally we'd just check the frame at the
568 * end of the reassembly slot to see if its F_AMSDU w/ no F_AMSDU_MORE -
569 * that means we've received the whole AMSDU decap pass.
573 * Get the rxs of the final mbuf in the slot, if one exists.
575 if (mbufq_len(&rap->rxa_mq[off]) != 0) {
576 rxs_final = ieee80211_get_rx_params_ptr(mbufq_last(&rap->rxa_mq[off]));
579 /* Default to tossing the duplicate frame */
583 * Check to see if the final frame has F_AMSDU and F_AMSDU set, AND
584 * this frame has F_AMSDU set (MORE or otherwise.) That's a sign
585 * that more can come.
588 if ((rxs != NULL) && (rxs_final != NULL) &&
589 ieee80211_check_rxseq_amsdu(rxs) &&
590 ieee80211_check_rxseq_amsdu(rxs_final)) {
591 if (! ieee80211_check_rxseq_amsdu_more(rxs_final)) {
593 * amsdu_more() returning 0 means "it's not the
594 * final frame" so we can append more
602 * If the list is empty OR we have determined we can put more
603 * driver decap'ed AMSDU frames in here, then insert.
605 if ((mbufq_len(&rap->rxa_mq[off]) == 0) || (toss_dup == 0)) {
606 if (mbufq_enqueue(&rap->rxa_mq[off], m) != 0) {
607 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT | IEEE80211_MSG_11N,
610 "seqno %u tid %u BA win <%u:%u> off=%d, qlen=%d, maxqlen=%d",
611 rxseq, tid, rap->rxa_start,
612 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
614 mbufq_len(&rap->rxa_mq[off]),
615 rap->rxa_mq[off].mq_maxlen);
616 /* XXX error count */
621 rap->rxa_qbytes += m->m_pkthdr.len;
622 vap->iv_stats.is_ampdu_rx_reorder++;
624 * Statistics for AMSDU decap.
626 if (rxs != NULL && ieee80211_check_rxseq_amsdu(rxs)) {
627 if (ieee80211_check_rxseq_amsdu_more(rxs)) {
628 /* more=1, AMSDU, end of batch */
629 IEEE80211_NODE_STAT(ni, rx_amsdu_more_end);
631 IEEE80211_NODE_STAT(ni, rx_amsdu_more);
635 IEEE80211_DISCARD_MAC(vap,
636 IEEE80211_MSG_INPUT | IEEE80211_MSG_11N,
637 ni->ni_macaddr, "a-mpdu duplicate",
638 "seqno %u tid %u BA win <%u:%u>",
639 rxseq, tid, rap->rxa_start,
640 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1));
642 IEEE80211_DISCARD_MAC(vap,
643 IEEE80211_MSG_INPUT | IEEE80211_MSG_11N,
644 ni->ni_macaddr, "a-mpdu duplicate",
645 "seqno %d tid %u pktflags 0x%08x\n",
646 rxseq, tid, rxs->c_pktflags);
648 if (rxs_final != NULL) {
649 IEEE80211_DISCARD_MAC(vap,
650 IEEE80211_MSG_INPUT | IEEE80211_MSG_11N,
651 ni->ni_macaddr, "a-mpdu duplicate",
652 "final: pktflags 0x%08x\n",
653 rxs_final->c_pktflags);
655 vap->iv_stats.is_rx_dup++;
656 IEEE80211_NODE_STAT(ni, rx_dup);
665 * Purge all frames in the A-MPDU re-order queue.
668 ampdu_rx_purge(struct ieee80211_rx_ampdu *rap)
672 for (i = 0; i < rap->rxa_wnd; i++) {
673 ampdu_rx_purge_slot(rap, i);
674 if (rap->rxa_qframes == 0)
677 KASSERT(rap->rxa_qbytes == 0 && rap->rxa_qframes == 0,
678 ("lost %u data, %u frames on ampdu rx q",
679 rap->rxa_qbytes, rap->rxa_qframes));
683 ieee80211_ampdu_rx_init_rap(struct ieee80211_node *ni,
684 struct ieee80211_rx_ampdu *rap)
688 /* XXX TODO: ensure the queues are empty */
689 memset(rap, 0, sizeof(*rap));
690 for (i = 0; i < IEEE80211_AGGR_BAWMAX; i++)
691 mbufq_init(&rap->rxa_mq[i], 256);
695 * Start A-MPDU rx/re-order processing for the specified TID.
698 ampdu_rx_start(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap,
699 int baparamset, int batimeout, int baseqctl)
701 struct ieee80211vap *vap = ni->ni_vap;
702 int bufsiz = _IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_BUFSIZ);
704 if (rap->rxa_flags & IEEE80211_AGGR_RUNNING) {
706 * AMPDU previously setup and not terminated with a DELBA,
707 * flush the reorder q's in case anything remains.
711 ieee80211_ampdu_rx_init_rap(ni, rap);
712 rap->rxa_wnd = (bufsiz == 0) ?
713 IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
714 rap->rxa_start = _IEEE80211_MASKSHIFT(baseqctl, IEEE80211_BASEQ_START);
715 rap->rxa_flags |= IEEE80211_AGGR_RUNNING | IEEE80211_AGGR_XCHGPEND;
717 /* XXX this should be a configuration flag */
718 if ((vap->iv_htcaps & IEEE80211_HTC_RX_AMSDU_AMPDU) &&
719 (_IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_AMSDU)))
720 rap->rxa_flags |= IEEE80211_AGGR_AMSDU;
722 rap->rxa_flags &= ~IEEE80211_AGGR_AMSDU;
728 * Public function; manually setup the RX ampdu state.
731 ieee80211_ampdu_rx_start_ext(struct ieee80211_node *ni, int tid, int seq, int baw)
733 struct ieee80211_rx_ampdu *rap;
735 /* XXX TODO: sanity check tid, seq, baw */
737 rap = &ni->ni_rx_ampdu[tid];
739 if (rap->rxa_flags & IEEE80211_AGGR_RUNNING) {
741 * AMPDU previously setup and not terminated with a DELBA,
742 * flush the reorder q's in case anything remains.
747 ieee80211_ampdu_rx_init_rap(ni, rap);
749 rap->rxa_wnd = (baw== 0) ?
750 IEEE80211_AGGR_BAWMAX : min(baw, IEEE80211_AGGR_BAWMAX);
752 /* Wait for the first RX frame, use that as BAW */
754 rap->rxa_flags |= IEEE80211_AGGR_WAITRX;
756 rap->rxa_start = seq;
758 rap->rxa_flags |= IEEE80211_AGGR_RUNNING | IEEE80211_AGGR_XCHGPEND;
760 /* XXX TODO: no amsdu flag */
762 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, ni,
763 "%s: tid=%d, start=%d, wnd=%d, flags=0x%08x",
774 * Public function; manually stop the RX AMPDU state.
777 ieee80211_ampdu_rx_stop_ext(struct ieee80211_node *ni, int tid)
779 struct ieee80211_rx_ampdu *rap;
781 /* XXX TODO: sanity check tid, seq, baw */
782 rap = &ni->ni_rx_ampdu[tid];
783 ampdu_rx_stop(ni, rap);
787 * Stop A-MPDU rx processing for the specified TID.
790 ampdu_rx_stop(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
794 rap->rxa_flags &= ~(IEEE80211_AGGR_RUNNING
795 | IEEE80211_AGGR_XCHGPEND
796 | IEEE80211_AGGR_WAITRX);
800 * Dispatch a frame from the A-MPDU reorder queue. The
801 * frame is fed back into ieee80211_input marked with an
802 * M_AMPDU_MPDU flag so it doesn't come back to us (it also
803 * permits ieee80211_input to optimize re-processing).
806 ampdu_dispatch(struct ieee80211_node *ni, struct mbuf *m)
808 m->m_flags |= M_AMPDU_MPDU; /* bypass normal processing */
809 /* NB: rssi and noise are ignored w/ M_AMPDU_MPDU set */
810 (void) ieee80211_input(ni, m, 0, 0);
814 ampdu_dispatch_slot(struct ieee80211_rx_ampdu *rap, struct ieee80211_node *ni,
820 while (mbufq_len(&rap->rxa_mq[i]) != 0) {
821 m = mbufq_dequeue(&rap->rxa_mq[i]);
826 rap->rxa_qbytes -= m->m_pkthdr.len;
829 ampdu_dispatch(ni, m);
835 ampdu_rx_moveup(struct ieee80211_rx_ampdu *rap, struct ieee80211_node *ni,
838 struct ieee80211vap *vap = ni->ni_vap;
841 * If frames remain, copy the mbuf pointers down so
842 * they correspond to the offsets in the new window.
844 if (rap->rxa_qframes != 0) {
845 int n = rap->rxa_qframes, j;
846 for (j = i+1; j < rap->rxa_wnd; j++) {
848 * Concat the list contents over, which will
849 * blank the source list for us.
851 if (mbufq_len(&rap->rxa_mq[j]) != 0) {
852 n = n - mbufq_len(&rap->rxa_mq[j]);
853 mbufq_concat(&rap->rxa_mq[j-i], &rap->rxa_mq[j]);
854 KASSERT(n >= 0, ("%s: n < 0 (%d)", __func__, n));
859 KASSERT(n == 0, ("%s: lost %d frames, qframes %d off %d "
860 "BA win <%d:%d> winstart %d",
861 __func__, n, rap->rxa_qframes, i, rap->rxa_start,
862 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
864 vap->iv_stats.is_ampdu_rx_copy += rap->rxa_qframes;
869 * Dispatch as many frames as possible from the re-order queue.
870 * Frames will always be "at the front"; we process all frames
871 * up to the first empty slot in the window. On completion we
872 * cleanup state if there are still pending frames in the current
873 * BA window. We assume the frame at slot 0 is already handled
874 * by the caller; we always start at slot 1.
877 ampdu_rx_dispatch(struct ieee80211_rx_ampdu *rap, struct ieee80211_node *ni)
879 struct ieee80211vap *vap = ni->ni_vap;
882 /* flush run of frames */
884 for (i = 1; i < rap->rxa_wnd; i++) {
885 r = ampdu_dispatch_slot(rap, ni, i);
892 ampdu_rx_moveup(rap, ni, i, -1);
895 * Adjust the start of the BA window to
896 * reflect the frames just dispatched.
898 rap->rxa_start = IEEE80211_SEQ_ADD(rap->rxa_start, i);
899 vap->iv_stats.is_ampdu_rx_oor += r2;
901 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, ni,
902 "%s: moved slot up %d slots to start at %d (%d frames)",
910 * Dispatch all frames in the A-MPDU re-order queue.
913 ampdu_rx_flush(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
917 for (i = 0; i < rap->rxa_wnd; i++) {
918 r = ampdu_dispatch_slot(rap, ni, i);
921 ni->ni_vap->iv_stats.is_ampdu_rx_oor += r;
923 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, ni,
924 "%s: moved slot up %d slots to start at %d (%d frames)",
930 if (rap->rxa_qframes == 0)
936 * Dispatch all frames in the A-MPDU re-order queue
937 * preceding the specified sequence number. This logic
938 * handles window moves due to a received MSDU or BAR.
941 ampdu_rx_flush_upto(struct ieee80211_node *ni,
942 struct ieee80211_rx_ampdu *rap, ieee80211_seq winstart)
944 struct ieee80211vap *vap = ni->ni_vap;
949 * Flush any complete MSDU's with a sequence number lower
950 * than winstart. Gaps may exist. Note that we may actually
951 * dispatch frames past winstart if a run continues; this is
952 * an optimization that avoids having to do a separate pass
953 * to dispatch frames after moving the BA window start.
955 seqno = rap->rxa_start;
956 for (i = 0; i < rap->rxa_wnd; i++) {
957 if ((r = mbufq_len(&rap->rxa_mq[i])) != 0) {
958 (void) ampdu_dispatch_slot(rap, ni, i);
960 if (!IEEE80211_SEQ_BA_BEFORE(seqno, winstart))
963 vap->iv_stats.is_ampdu_rx_oor += r;
964 seqno = IEEE80211_SEQ_INC(seqno);
966 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, ni,
967 "%s: moved slot up %d slots to start at %d (%d frames)",
975 * If frames remain, copy the mbuf pointers down so
976 * they correspond to the offsets in the new window.
978 ampdu_rx_moveup(rap, ni, i, winstart);
981 * Move the start of the BA window; we use the
982 * sequence number of the last MSDU that was
983 * passed up the stack+1 or winstart if stopped on
984 * a gap in the reorder buffer.
986 rap->rxa_start = seqno;
990 * Process a received QoS data frame for an HT station. Handle
991 * A-MPDU reordering: if this frame is received out of order
992 * and falls within the BA window hold onto it. Otherwise if
993 * this frame completes a run, flush any pending frames. We
994 * return 1 if the frame is consumed. A 0 is returned if
995 * the frame should be processed normally by the caller.
997 * A-MSDU: handle hardware decap'ed A-MSDU frames that are
998 * pretending to be MPDU's. They're dispatched directly if
999 * able; or attempted to put into the receive reordering slot.
1002 ieee80211_ampdu_reorder(struct ieee80211_node *ni, struct mbuf *m,
1003 const struct ieee80211_rx_stats *rxs)
1005 #define PROCESS 0 /* caller should process frame */
1006 #define CONSUMED 1 /* frame consumed, caller does nothing */
1007 struct ieee80211vap *vap = ni->ni_vap;
1008 struct ieee80211_qosframe *wh;
1009 struct ieee80211_rx_ampdu *rap;
1010 ieee80211_seq rxseq;
1013 int amsdu = ieee80211_check_rxseq_amsdu(rxs);
1014 int amsdu_end = ieee80211_check_rxseq_amsdu_more(rxs);
1016 KASSERT((m->m_flags & (M_AMPDU | M_AMPDU_MPDU)) == M_AMPDU,
1017 ("!a-mpdu or already re-ordered, flags 0x%x", m->m_flags));
1018 KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT sta"));
1020 /* NB: m_len known to be sufficient */
1021 wh = mtod(m, struct ieee80211_qosframe *);
1022 if (wh->i_fc[0] != IEEE80211_FC0_QOSDATA) {
1024 * Not QoS data, shouldn't get here but just
1025 * return it to the caller for processing.
1031 * 802.11-2012 9.3.2.10 - Duplicate detection and recovery.
1033 * Multicast QoS data frames are checked against a different
1034 * counter, not the per-TID counter.
1036 if (IEEE80211_IS_MULTICAST(wh->i_addr1))
1039 tid = ieee80211_getqos(wh)[0];
1040 tid &= IEEE80211_QOS_TID;
1041 rap = &ni->ni_rx_ampdu[tid];
1042 if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
1044 * No ADDBA request yet, don't touch.
1048 rxseq = le16toh(*(uint16_t *)wh->i_seq);
1049 if ((rxseq & IEEE80211_SEQ_FRAG_MASK) != 0) {
1051 * Fragments are not allowed; toss.
1053 IEEE80211_DISCARD_MAC(vap,
1054 IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
1055 "A-MPDU", "fragment, rxseq 0x%x tid %u%s", rxseq, tid,
1056 wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
1057 vap->iv_stats.is_ampdu_rx_drop++;
1058 IEEE80211_NODE_STAT(ni, rx_drop);
1062 rxseq >>= IEEE80211_SEQ_SEQ_SHIFT;
1066 * Handle waiting for the first frame to define the BAW.
1067 * Some firmware doesn't provide the RX of the starting point
1068 * of the BAW and we have to cope.
1070 if (rap->rxa_flags & IEEE80211_AGGR_WAITRX) {
1071 rap->rxa_flags &= ~IEEE80211_AGGR_WAITRX;
1072 rap->rxa_start = rxseq;
1075 if (rxseq == rap->rxa_start) {
1077 * First frame in window.
1079 if (rap->rxa_qframes != 0) {
1081 * Dispatch as many packets as we can.
1083 KASSERT((mbufq_len(&rap->rxa_mq[0]) == 0), ("unexpected dup"));
1084 ampdu_dispatch(ni, m);
1085 ampdu_rx_dispatch(rap, ni);
1089 * In order; advance window if needed and notify
1090 * caller to dispatch directly.
1094 rap->rxa_start = IEEE80211_SEQ_INC(rxseq);
1095 IEEE80211_NODE_STAT(ni, rx_amsdu_more_end);
1097 IEEE80211_NODE_STAT(ni, rx_amsdu_more);
1100 rap->rxa_start = IEEE80211_SEQ_INC(rxseq);
1106 * Frame is out of order; store if in the BA window.
1108 /* calculate offset in BA window */
1109 off = IEEE80211_SEQ_SUB(rxseq, rap->rxa_start);
1110 if (off < rap->rxa_wnd) {
1112 * Common case (hopefully): in the BA window.
1113 * Sec 9.10.7.6.2 a) (p.137)
1117 * Check for frames sitting too long in the reorder queue.
1118 * This should only ever happen if frames are not delivered
1119 * without the sender otherwise notifying us (e.g. with a
1120 * BAR to move the window). Typically this happens because
1121 * of vendor bugs that cause the sequence number to jump.
1122 * When this happens we get a gap in the reorder queue that
1123 * leaves frame sitting on the queue until they get pushed
1124 * out due to window moves. When the vendor does not send
1125 * BAR this move only happens due to explicit packet sends
1127 * NB: we only track the time of the oldest frame in the
1128 * reorder q; this means that if we flush we might push
1129 * frames that still "new"; if this happens then subsequent
1130 * frames will result in BA window moves which cost something
1131 * but is still better than a big throughput dip.
1133 if (rap->rxa_qframes != 0) {
1134 /* XXX honor batimeout? */
1135 if (ticks - rap->rxa_age > ieee80211_ampdu_age) {
1137 * Too long since we received the first
1138 * frame; flush the reorder buffer.
1140 if (rap->rxa_qframes != 0) {
1141 vap->iv_stats.is_ampdu_rx_age +=
1143 ampdu_rx_flush(ni, rap);
1146 * Advance the window if needed and notify
1147 * the caller to dispatch directly.
1152 IEEE80211_SEQ_INC(rxseq);
1153 IEEE80211_NODE_STAT(ni,
1156 IEEE80211_NODE_STAT(ni,
1161 IEEE80211_SEQ_INC(rxseq);
1167 * First frame, start aging timer.
1169 rap->rxa_age = ticks;
1172 /* save packet - this consumes, no matter what */
1173 ampdu_rx_add_slot(rap, off, tid, rxseq, ni, m, rxs);
1176 if (off < IEEE80211_SEQ_BA_RANGE) {
1178 * Outside the BA window, but within range;
1179 * flush the reorder q and move the window.
1180 * Sec 9.10.7.6.2 b) (p.138)
1182 IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
1183 "move BA win <%u:%u> (%u frames) rxseq %u tid %u",
1185 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
1186 rap->rxa_qframes, rxseq, tid);
1187 vap->iv_stats.is_ampdu_rx_move++;
1190 * The spec says to flush frames up to but not including:
1191 * WinStart_B = rxseq - rap->rxa_wnd + 1
1192 * Then insert the frame or notify the caller to process
1193 * it immediately. We can safely do this by just starting
1194 * over again because we know the frame will now be within
1197 /* NB: rxa_wnd known to be >0 */
1198 ampdu_rx_flush_upto(ni, rap,
1199 IEEE80211_SEQ_SUB(rxseq, rap->rxa_wnd-1));
1203 * Outside the BA window and out of range; toss.
1204 * Sec 9.10.7.6.2 c) (p.138)
1206 IEEE80211_DISCARD_MAC(vap,
1207 IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
1208 "MPDU", "BA win <%u:%u> (%u frames) rxseq %u tid %u%s",
1210 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
1211 rap->rxa_qframes, rxseq, tid,
1212 wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
1213 vap->iv_stats.is_ampdu_rx_drop++;
1214 IEEE80211_NODE_STAT(ni, rx_drop);
1223 * Process a BAR ctl frame. Dispatch all frames up to
1224 * the sequence number of the frame. If this frame is
1225 * out of range it's discarded.
1228 ieee80211_recv_bar(struct ieee80211_node *ni, struct mbuf *m0)
1230 struct ieee80211vap *vap = ni->ni_vap;
1231 struct ieee80211_frame_bar *wh;
1232 struct ieee80211_rx_ampdu *rap;
1233 ieee80211_seq rxseq;
1236 if (!ieee80211_recv_bar_ena) {
1238 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_11N,
1239 ni->ni_macaddr, "BAR", "%s", "processing disabled");
1241 vap->iv_stats.is_ampdu_bar_bad++;
1244 wh = mtod(m0, struct ieee80211_frame_bar *);
1245 /* XXX check basic BAR */
1246 tid = _IEEE80211_MASKSHIFT(le16toh(wh->i_ctl), IEEE80211_BAR_TID);
1247 rap = &ni->ni_rx_ampdu[tid];
1248 if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
1250 * No ADDBA request yet, don't touch.
1252 IEEE80211_DISCARD_MAC(vap,
1253 IEEE80211_MSG_INPUT | IEEE80211_MSG_11N,
1254 ni->ni_macaddr, "BAR", "no BA stream, tid %u", tid);
1255 vap->iv_stats.is_ampdu_bar_bad++;
1258 vap->iv_stats.is_ampdu_bar_rx++;
1259 rxseq = le16toh(wh->i_seq) >> IEEE80211_SEQ_SEQ_SHIFT;
1260 if (rxseq == rap->rxa_start)
1262 /* calculate offset in BA window */
1263 off = IEEE80211_SEQ_SUB(rxseq, rap->rxa_start);
1264 if (off < IEEE80211_SEQ_BA_RANGE) {
1266 * Flush the reorder q up to rxseq and move the window.
1267 * Sec 9.10.7.6.3 a) (p.138)
1269 IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
1270 "BAR moves BA win <%u:%u> (%u frames) rxseq %u tid %u",
1272 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
1273 rap->rxa_qframes, rxseq, tid);
1274 vap->iv_stats.is_ampdu_bar_move++;
1276 ampdu_rx_flush_upto(ni, rap, rxseq);
1277 if (off >= rap->rxa_wnd) {
1279 * BAR specifies a window start to the right of BA
1280 * window; we must move it explicitly since
1281 * ampdu_rx_flush_upto will not.
1283 rap->rxa_start = rxseq;
1287 * Out of range; toss.
1288 * Sec 9.10.7.6.3 b) (p.138)
1290 IEEE80211_DISCARD_MAC(vap,
1291 IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
1292 "BAR", "BA win <%u:%u> (%u frames) rxseq %u tid %u%s",
1294 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
1295 rap->rxa_qframes, rxseq, tid,
1296 wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
1297 vap->iv_stats.is_ampdu_bar_oow++;
1298 IEEE80211_NODE_STAT(ni, rx_drop);
1303 * Setup HT-specific state in a node. Called only
1304 * when HT use is negotiated so we don't do extra
1305 * work for temporary and/or legacy sta's.
1308 ieee80211_ht_node_init(struct ieee80211_node *ni)
1310 struct ieee80211_tx_ampdu *tap;
1313 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
1319 if (ni->ni_flags & IEEE80211_NODE_HT) {
1321 * Clean AMPDU state on re-associate. This handles the case
1322 * where a station leaves w/o notifying us and then returns
1323 * before node is reaped for inactivity.
1325 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
1327 "%s: calling cleanup (%p)",
1329 ieee80211_ht_node_cleanup(ni);
1331 for (tid = 0; tid < WME_NUM_TID; tid++) {
1332 tap = &ni->ni_tx_ampdu[tid];
1335 ieee80211_txampdu_init_pps(tap);
1336 /* NB: further initialization deferred */
1337 ieee80211_ampdu_rx_init_rap(ni, &ni->ni_rx_ampdu[tid]);
1339 ni->ni_flags |= IEEE80211_NODE_HT | IEEE80211_NODE_AMPDU |
1340 IEEE80211_NODE_AMSDU;
1344 * Cleanup HT-specific state in a node. Called only
1345 * when HT use has been marked.
1348 ieee80211_ht_node_cleanup(struct ieee80211_node *ni)
1350 struct ieee80211com *ic = ni->ni_ic;
1353 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
1358 KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT node"));
1360 /* XXX optimize this */
1361 for (i = 0; i < WME_NUM_TID; i++) {
1362 struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[i];
1363 if (tap->txa_flags & IEEE80211_AGGR_SETUP)
1366 for (i = 0; i < WME_NUM_TID; i++)
1367 ic->ic_ampdu_rx_stop(ni, &ni->ni_rx_ampdu[i]);
1370 ni->ni_flags &= ~IEEE80211_NODE_HT_ALL;
1374 * Age out HT resources for a station.
1377 ieee80211_ht_node_age(struct ieee80211_node *ni)
1379 struct ieee80211vap *vap = ni->ni_vap;
1382 KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT sta"));
1384 for (tid = 0; tid < WME_NUM_TID; tid++) {
1385 struct ieee80211_rx_ampdu *rap;
1387 rap = &ni->ni_rx_ampdu[tid];
1388 if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0)
1390 if (rap->rxa_qframes == 0)
1393 * Check for frames sitting too long in the reorder queue.
1394 * See above for more details on what's happening here.
1396 /* XXX honor batimeout? */
1397 if (ticks - rap->rxa_age > ieee80211_ampdu_age) {
1399 * Too long since we received the first
1400 * frame; flush the reorder buffer.
1402 vap->iv_stats.is_ampdu_rx_age += rap->rxa_qframes;
1403 ampdu_rx_flush(ni, rap);
1408 static struct ieee80211_channel *
1409 findhtchan(struct ieee80211com *ic, struct ieee80211_channel *c, int htflags)
1411 return ieee80211_find_channel(ic, c->ic_freq,
1412 (c->ic_flags &~ IEEE80211_CHAN_HT) | htflags);
1416 * Adjust a channel to be HT/non-HT according to the vap's configuration.
1418 struct ieee80211_channel *
1419 ieee80211_ht_adjust_channel(struct ieee80211com *ic,
1420 struct ieee80211_channel *chan, int flags)
1422 struct ieee80211_channel *c;
1424 if (flags & IEEE80211_FHT_HT) {
1425 /* promote to HT if possible */
1426 if (flags & IEEE80211_FHT_USEHT40) {
1427 if (!IEEE80211_IS_CHAN_HT40(chan)) {
1428 /* NB: arbitrarily pick ht40+ over ht40- */
1429 c = findhtchan(ic, chan, IEEE80211_CHAN_HT40U);
1431 c = findhtchan(ic, chan,
1432 IEEE80211_CHAN_HT40D);
1434 c = findhtchan(ic, chan,
1435 IEEE80211_CHAN_HT20);
1439 } else if (!IEEE80211_IS_CHAN_HT20(chan)) {
1440 c = findhtchan(ic, chan, IEEE80211_CHAN_HT20);
1444 } else if (IEEE80211_IS_CHAN_HT(chan)) {
1445 /* demote to legacy, HT use is disabled */
1446 c = ieee80211_find_channel(ic, chan->ic_freq,
1447 chan->ic_flags &~ IEEE80211_CHAN_HT);
1455 * Setup HT-specific state for a legacy WDS peer.
1458 ieee80211_ht_wds_init(struct ieee80211_node *ni)
1460 struct ieee80211vap *vap = ni->ni_vap;
1461 struct ieee80211_tx_ampdu *tap;
1464 KASSERT(vap->iv_flags_ht & IEEE80211_FHT_HT, ("no HT requested"));
1466 /* XXX check scan cache in case peer has an ap and we have info */
1468 * If setup with a legacy channel; locate an HT channel.
1469 * Otherwise if the inherited channel (from a companion
1470 * AP) is suitable use it so we use the same location
1471 * for the extension channel).
1473 ni->ni_chan = ieee80211_ht_adjust_channel(ni->ni_ic,
1474 ni->ni_chan, ieee80211_htchanflags(ni->ni_chan));
1477 if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20)
1478 ni->ni_htcap |= IEEE80211_HTCAP_SHORTGI20;
1479 if (IEEE80211_IS_CHAN_HT40(ni->ni_chan)) {
1480 ni->ni_htcap |= IEEE80211_HTCAP_CHWIDTH40;
1482 if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan))
1483 ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_ABOVE;
1484 else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan))
1485 ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_BELOW;
1486 if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40)
1487 ni->ni_htcap |= IEEE80211_HTCAP_SHORTGI40;
1490 ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_NONE;
1492 ni->ni_htctlchan = ni->ni_chan->ic_ieee;
1493 if (vap->iv_flags_ht & IEEE80211_FHT_RIFS)
1494 ni->ni_flags |= IEEE80211_NODE_RIFS;
1495 /* XXX does it make sense to enable SMPS? */
1497 ni->ni_htopmode = 0; /* XXX need protection state */
1498 ni->ni_htstbc = 0; /* XXX need info */
1500 for (tid = 0; tid < WME_NUM_TID; tid++) {
1501 tap = &ni->ni_tx_ampdu[tid];
1503 ieee80211_txampdu_init_pps(tap);
1505 /* NB: AMPDU tx/rx governed by IEEE80211_FHT_AMPDU_{TX,RX} */
1506 ni->ni_flags |= IEEE80211_NODE_HT | IEEE80211_NODE_AMPDU |
1507 IEEE80211_NODE_AMSDU;
1511 * Notify a VAP of a change in the HTINFO ie if it's a hostap VAP.
1513 * This is to be called from the deferred HT protection update
1514 * task once the flags are updated.
1517 ieee80211_htinfo_notify(struct ieee80211vap *vap)
1520 IEEE80211_LOCK_ASSERT(vap->iv_ic);
1522 if (vap->iv_opmode != IEEE80211_M_HOSTAP)
1524 if (vap->iv_state != IEEE80211_S_RUN ||
1525 !IEEE80211_IS_CHAN_HT(vap->iv_bss->ni_chan))
1529 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N,
1531 "HT bss occupancy change: %d sta, %d ht, "
1532 "%d ht40%s, HT protmode now 0x%x"
1534 , vap->iv_ht_sta_assoc
1535 , vap->iv_ht40_sta_assoc
1536 , (vap->iv_flags_ht & IEEE80211_FHT_NONHT_PR) ?
1537 ", non-HT sta present" : ""
1538 , vap->iv_curhtprotmode);
1540 ieee80211_beacon_notify(vap, IEEE80211_BEACON_HTINFO);
1544 * Calculate HT protection mode from current
1545 * state and handle updates.
1548 htinfo_update(struct ieee80211vap *vap)
1550 struct ieee80211com *ic = vap->iv_ic;
1553 if (vap->iv_sta_assoc != vap->iv_ht_sta_assoc) {
1554 protmode = IEEE80211_HTINFO_OPMODE_MIXED
1555 | IEEE80211_HTINFO_NONHT_PRESENT;
1556 } else if (vap->iv_flags_ht & IEEE80211_FHT_NONHT_PR) {
1557 protmode = IEEE80211_HTINFO_OPMODE_PROTOPT
1558 | IEEE80211_HTINFO_NONHT_PRESENT;
1559 } else if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
1560 IEEE80211_IS_CHAN_HT40(ic->ic_bsschan) &&
1561 vap->iv_sta_assoc != vap->iv_ht40_sta_assoc) {
1562 protmode = IEEE80211_HTINFO_OPMODE_HT20PR;
1564 protmode = IEEE80211_HTINFO_OPMODE_PURE;
1566 if (protmode != vap->iv_curhtprotmode) {
1567 vap->iv_curhtprotmode = protmode;
1568 /* Update VAP with new protection mode */
1569 ieee80211_vap_update_ht_protmode(vap);
1574 * Handle an HT station joining a BSS.
1577 ieee80211_ht_node_join(struct ieee80211_node *ni)
1579 struct ieee80211vap *vap = ni->ni_vap;
1581 IEEE80211_LOCK_ASSERT(vap->iv_ic);
1583 if (ni->ni_flags & IEEE80211_NODE_HT) {
1584 vap->iv_ht_sta_assoc++;
1585 if (ni->ni_chw == 40)
1586 vap->iv_ht40_sta_assoc++;
1592 * Handle an HT station leaving a BSS.
1595 ieee80211_ht_node_leave(struct ieee80211_node *ni)
1597 struct ieee80211vap *vap = ni->ni_vap;
1599 IEEE80211_LOCK_ASSERT(vap->iv_ic);
1601 if (ni->ni_flags & IEEE80211_NODE_HT) {
1602 vap->iv_ht_sta_assoc--;
1603 if (ni->ni_chw == 40)
1604 vap->iv_ht40_sta_assoc--;
1610 * Public version of htinfo_update; used for processing
1611 * beacon frames from overlapping bss.
1613 * Caller can specify either IEEE80211_HTINFO_OPMODE_MIXED
1614 * (on receipt of a beacon that advertises MIXED) or
1615 * IEEE80211_HTINFO_OPMODE_PROTOPT (on receipt of a beacon
1616 * from an overlapping legacy bss). We treat MIXED with
1617 * a higher precedence than PROTOPT (i.e. we will not change
1618 * change PROTOPT -> MIXED; only MIXED -> PROTOPT). This
1619 * corresponds to how we handle things in htinfo_update.
1623 ieee80211_htprot_update(struct ieee80211vap *vap, int protmode)
1625 struct ieee80211com *ic = vap->iv_ic;
1626 #define OPMODE(x) _IEEE80211_SHIFTMASK(x, IEEE80211_HTINFO_OPMODE)
1629 /* track non-HT station presence */
1630 KASSERT(protmode & IEEE80211_HTINFO_NONHT_PRESENT,
1631 ("protmode 0x%x", protmode));
1632 vap->iv_flags_ht |= IEEE80211_FHT_NONHT_PR;
1633 vap->iv_lastnonht = ticks;
1635 if (protmode != vap->iv_curhtprotmode &&
1636 (OPMODE(vap->iv_curhtprotmode) != IEEE80211_HTINFO_OPMODE_MIXED ||
1637 OPMODE(protmode) == IEEE80211_HTINFO_OPMODE_PROTOPT)) {
1638 vap->iv_curhtprotmode = protmode;
1639 /* Update VAP with new protection mode */
1640 ieee80211_vap_update_ht_protmode(vap);
1642 IEEE80211_UNLOCK(ic);
1647 * Time out presence of an overlapping bss with non-HT
1648 * stations. When operating in hostap mode we listen for
1649 * beacons from other stations and if we identify a non-HT
1650 * station is present we update the opmode field of the
1651 * HTINFO ie. To identify when all non-HT stations are
1652 * gone we time out this condition.
1655 ieee80211_ht_timeout(struct ieee80211vap *vap)
1658 IEEE80211_LOCK_ASSERT(vap->iv_ic);
1660 if ((vap->iv_flags_ht & IEEE80211_FHT_NONHT_PR) &&
1661 ieee80211_time_after(ticks, vap->iv_lastnonht + IEEE80211_NONHT_PRESENT_AGE)) {
1662 IEEE80211_DPRINTF(vap, IEEE80211_MSG_11N,
1663 "%s", "time out non-HT STA present on channel");
1664 vap->iv_flags_ht &= ~IEEE80211_FHT_NONHT_PR;
1670 * Process an 802.11n HT capabilities ie.
1673 ieee80211_parse_htcap(struct ieee80211_node *ni, const uint8_t *ie)
1675 if (ie[0] == IEEE80211_ELEMID_VENDOR) {
1677 * Station used Vendor OUI ie to associate;
1678 * mark the node so when we respond we'll use
1679 * the Vendor OUI's and not the standard ie's.
1681 ni->ni_flags |= IEEE80211_NODE_HTCOMPAT;
1684 ni->ni_flags &= ~IEEE80211_NODE_HTCOMPAT;
1686 ni->ni_htcap = le16dec(ie +
1687 __offsetof(struct ieee80211_ie_htcap, hc_cap));
1688 ni->ni_htparam = ie[__offsetof(struct ieee80211_ie_htcap, hc_param)];
1692 htinfo_parse(struct ieee80211_node *ni,
1693 const struct ieee80211_ie_htinfo *htinfo)
1697 ni->ni_htctlchan = htinfo->hi_ctrlchannel;
1698 ni->ni_ht2ndchan = _IEEE80211_SHIFTMASK(htinfo->hi_byte1,
1699 IEEE80211_HTINFO_2NDCHAN);
1700 w = le16dec(&htinfo->hi_byte2);
1701 ni->ni_htopmode = _IEEE80211_SHIFTMASK(w, IEEE80211_HTINFO_OPMODE);
1702 w = le16dec(&htinfo->hi_byte45);
1703 ni->ni_htstbc = _IEEE80211_SHIFTMASK(w, IEEE80211_HTINFO_BASIC_STBCMCS);
1707 * Parse an 802.11n HT info ie and save useful information
1708 * to the node state. Note this does not effect any state
1709 * changes such as for channel width change.
1712 ieee80211_parse_htinfo(struct ieee80211_node *ni, const uint8_t *ie)
1714 if (ie[0] == IEEE80211_ELEMID_VENDOR)
1716 htinfo_parse(ni, (const struct ieee80211_ie_htinfo *) ie);
1720 * Handle 11n/11ac channel switch.
1722 * Use the received HT/VHT ie's to identify the right channel to use.
1723 * If we cannot locate it in the channel table then fallback to
1726 * Note that we use this information to identify the node's
1727 * channel only; the caller is responsible for insuring any
1728 * required channel change is done (e.g. in sta mode when
1729 * parsing the contents of a beacon frame).
1732 htinfo_update_chw(struct ieee80211_node *ni, int htflags, int vhtflags)
1734 struct ieee80211com *ic = ni->ni_ic;
1735 struct ieee80211_channel *c;
1740 * First step - do HT/VHT only channel lookup based on operating mode
1741 * flags. This involves masking out the VHT flags as well.
1742 * Otherwise we end up doing the full channel walk each time
1743 * we trigger this, which is expensive.
1745 chanflags = (ni->ni_chan->ic_flags &~
1746 (IEEE80211_CHAN_HT | IEEE80211_CHAN_VHT)) | htflags | vhtflags;
1748 if (chanflags == ni->ni_chan->ic_flags)
1752 * If HT /or/ VHT flags have changed then check both.
1753 * We need to start by picking a HT channel anyway.
1757 chanflags = (ni->ni_chan->ic_flags &~
1758 (IEEE80211_CHAN_HT | IEEE80211_CHAN_VHT)) | htflags;
1759 /* XXX not right for ht40- */
1760 c = ieee80211_find_channel(ic, ni->ni_chan->ic_freq, chanflags);
1761 if (c == NULL && (htflags & IEEE80211_CHAN_HT40)) {
1763 * No HT40 channel entry in our table; fall back
1764 * to HT20 operation. This should not happen.
1766 c = findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT20);
1768 IEEE80211_NOTE(ni->ni_vap,
1769 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
1770 "no HT40 channel (freq %u), falling back to HT20",
1771 ni->ni_chan->ic_freq);
1776 /* Nothing found - leave it alone; move onto VHT */
1781 * If it's non-HT, then bail out now.
1783 if (! IEEE80211_IS_CHAN_HT(c)) {
1784 IEEE80211_NOTE(ni->ni_vap,
1785 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
1786 "not HT; skipping VHT check (%u/0x%x)",
1787 c->ic_freq, c->ic_flags);
1792 * Next step - look at the current VHT flags and determine
1793 * if we need to upgrade. Mask out the VHT and HT flags since
1794 * the vhtflags field will already have the correct HT
1797 if (IEEE80211_CONF_VHT(ic) && ni->ni_vhtcap != 0 && vhtflags != 0) {
1798 chanflags = (c->ic_flags
1799 &~ (IEEE80211_CHAN_HT | IEEE80211_CHAN_VHT))
1801 IEEE80211_NOTE(ni->ni_vap,
1802 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N,
1804 "%s: VHT; chanwidth=0x%02x; vhtflags=0x%08x",
1805 __func__, ni->ni_vht_chanwidth, vhtflags);
1807 IEEE80211_NOTE(ni->ni_vap,
1808 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N,
1810 "%s: VHT; trying lookup for %d/0x%08x",
1811 __func__, c->ic_freq, chanflags);
1812 c = ieee80211_find_channel(ic, c->ic_freq, chanflags);
1815 /* Finally, if it's changed */
1816 if (c != NULL && c != ni->ni_chan) {
1817 IEEE80211_NOTE(ni->ni_vap,
1818 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
1819 "switch station to %s%d channel %u/0x%x",
1820 IEEE80211_IS_CHAN_VHT(c) ? "VHT" : "HT",
1821 IEEE80211_IS_CHAN_VHT80(c) ? 80 :
1822 (IEEE80211_IS_CHAN_HT40(c) ? 40 : 20),
1823 c->ic_freq, c->ic_flags);
1827 /* NB: caller responsible for forcing any channel change */
1830 /* update node's (11n) tx channel width */
1831 ni->ni_chw = IEEE80211_IS_CHAN_HT40(ni->ni_chan)? 40 : 20;
1836 * Update 11n MIMO PS state according to received htcap.
1839 htcap_update_mimo_ps(struct ieee80211_node *ni)
1841 uint16_t oflags = ni->ni_flags;
1843 switch (ni->ni_htcap & IEEE80211_HTCAP_SMPS) {
1844 case IEEE80211_HTCAP_SMPS_DYNAMIC:
1845 ni->ni_flags |= IEEE80211_NODE_MIMO_PS;
1846 ni->ni_flags |= IEEE80211_NODE_MIMO_RTS;
1848 case IEEE80211_HTCAP_SMPS_ENA:
1849 ni->ni_flags |= IEEE80211_NODE_MIMO_PS;
1850 ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS;
1852 case IEEE80211_HTCAP_SMPS_OFF:
1853 default: /* disable on rx of reserved value */
1854 ni->ni_flags &= ~IEEE80211_NODE_MIMO_PS;
1855 ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS;
1858 return (oflags ^ ni->ni_flags);
1862 * Update short GI state according to received htcap
1863 * and local settings.
1865 static __inline void
1866 htcap_update_shortgi(struct ieee80211_node *ni)
1868 struct ieee80211vap *vap = ni->ni_vap;
1870 ni->ni_flags &= ~(IEEE80211_NODE_SGI20|IEEE80211_NODE_SGI40);
1871 if ((ni->ni_htcap & IEEE80211_HTCAP_SHORTGI20) &&
1872 (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20))
1873 ni->ni_flags |= IEEE80211_NODE_SGI20;
1874 if ((ni->ni_htcap & IEEE80211_HTCAP_SHORTGI40) &&
1875 (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40))
1876 ni->ni_flags |= IEEE80211_NODE_SGI40;
1880 * Update LDPC state according to received htcap
1881 * and local settings.
1883 static __inline void
1884 htcap_update_ldpc(struct ieee80211_node *ni)
1886 struct ieee80211vap *vap = ni->ni_vap;
1888 if ((ni->ni_htcap & IEEE80211_HTCAP_LDPC) &&
1889 (vap->iv_flags_ht & IEEE80211_FHT_LDPC_TX))
1890 ni->ni_flags |= IEEE80211_NODE_LDPC;
1894 * Parse and update HT-related state extracted from
1895 * the HT cap and info ie's.
1897 * This is called from the STA management path and
1898 * the ieee80211_node_join() path. It will take into
1899 * account the IEs discovered during scanning and
1900 * adjust things accordingly.
1903 ieee80211_ht_updateparams(struct ieee80211_node *ni,
1904 const uint8_t *htcapie, const uint8_t *htinfoie)
1906 struct ieee80211vap *vap = ni->ni_vap;
1907 const struct ieee80211_ie_htinfo *htinfo;
1909 ieee80211_parse_htcap(ni, htcapie);
1910 if (vap->iv_htcaps & IEEE80211_HTC_SMPS)
1911 htcap_update_mimo_ps(ni);
1912 htcap_update_shortgi(ni);
1913 htcap_update_ldpc(ni);
1915 if (htinfoie[0] == IEEE80211_ELEMID_VENDOR)
1917 htinfo = (const struct ieee80211_ie_htinfo *) htinfoie;
1918 htinfo_parse(ni, htinfo);
1921 * Defer the node channel change; we need to now
1922 * update VHT parameters before we do it.
1925 if ((htinfo->hi_byte1 & IEEE80211_HTINFO_RIFSMODE_PERM) &&
1926 (vap->iv_flags_ht & IEEE80211_FHT_RIFS))
1927 ni->ni_flags |= IEEE80211_NODE_RIFS;
1929 ni->ni_flags &= ~IEEE80211_NODE_RIFS;
1933 ieee80211_vht_get_vhtflags(struct ieee80211_node *ni, uint32_t htflags)
1935 struct ieee80211vap *vap = ni->ni_vap;
1936 uint32_t vhtflags = 0;
1939 if (ni->ni_flags & IEEE80211_NODE_VHT && vap->iv_flags_vht & IEEE80211_FVHT_VHT) {
1940 if ((ni->ni_vht_chanwidth == IEEE80211_VHT_CHANWIDTH_160MHZ) &&
1941 /* XXX 2 means "160MHz and 80+80MHz", 1 means "160MHz" */
1942 (_IEEE80211_MASKSHIFT(vap->iv_vhtcaps,
1943 IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_MASK) >= 1) &&
1944 (vap->iv_flags_vht & IEEE80211_FVHT_USEVHT160)) {
1945 vhtflags = IEEE80211_CHAN_VHT160;
1946 /* Mirror the HT40 flags */
1947 if (htflags == IEEE80211_CHAN_HT40U) {
1948 vhtflags |= IEEE80211_CHAN_HT40U;
1949 } else if (htflags == IEEE80211_CHAN_HT40D) {
1950 vhtflags |= IEEE80211_CHAN_HT40D;
1952 } else if ((ni->ni_vht_chanwidth == IEEE80211_VHT_CHANWIDTH_80P80MHZ) &&
1953 /* XXX 2 means "160MHz and 80+80MHz" */
1954 (_IEEE80211_MASKSHIFT(vap->iv_vhtcaps,
1955 IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_MASK) == 2) &&
1956 (vap->iv_flags_vht & IEEE80211_FVHT_USEVHT80P80)) {
1957 vhtflags = IEEE80211_CHAN_VHT80P80;
1958 /* Mirror the HT40 flags */
1959 if (htflags == IEEE80211_CHAN_HT40U) {
1960 vhtflags |= IEEE80211_CHAN_HT40U;
1961 } else if (htflags == IEEE80211_CHAN_HT40D) {
1962 vhtflags |= IEEE80211_CHAN_HT40D;
1964 } else if ((ni->ni_vht_chanwidth == IEEE80211_VHT_CHANWIDTH_80MHZ) &&
1965 (vap->iv_flags_vht & IEEE80211_FVHT_USEVHT80)) {
1966 vhtflags = IEEE80211_CHAN_VHT80;
1967 /* Mirror the HT40 flags */
1968 if (htflags == IEEE80211_CHAN_HT40U) {
1969 vhtflags |= IEEE80211_CHAN_HT40U;
1970 } else if (htflags == IEEE80211_CHAN_HT40D) {
1971 vhtflags |= IEEE80211_CHAN_HT40D;
1973 } else if (ni->ni_vht_chanwidth == IEEE80211_VHT_CHANWIDTH_USE_HT) {
1974 /* Mirror the HT40 flags */
1976 * XXX TODO: if ht40 is disabled, but vht40 isn't
1977 * disabled then this logic will get very, very sad.
1978 * It's quite possible the only sane thing to do is
1979 * to not have vht40 as an option, and just obey
1980 * 'ht40' as that flag.
1982 if ((htflags == IEEE80211_CHAN_HT40U) &&
1983 (vap->iv_flags_vht & IEEE80211_FVHT_USEVHT40)) {
1984 vhtflags = IEEE80211_CHAN_VHT40U
1985 | IEEE80211_CHAN_HT40U;
1986 } else if (htflags == IEEE80211_CHAN_HT40D &&
1987 (vap->iv_flags_vht & IEEE80211_FVHT_USEVHT40)) {
1988 vhtflags = IEEE80211_CHAN_VHT40D
1989 | IEEE80211_CHAN_HT40D;
1990 } else if (htflags == IEEE80211_CHAN_HT20) {
1991 vhtflags = IEEE80211_CHAN_VHT20
1992 | IEEE80211_CHAN_HT20;
1995 vhtflags = IEEE80211_CHAN_VHT20;
2002 * Final part of updating the HT parameters.
2004 * This is called from the STA management path and
2005 * the ieee80211_node_join() path. It will take into
2006 * account the IEs discovered during scanning and
2007 * adjust things accordingly.
2009 * This is done after a call to ieee80211_ht_updateparams()
2010 * because it (and the upcoming VHT version of updateparams)
2011 * needs to ensure everything is parsed before htinfo_update_chw()
2012 * is called - which will change the channel config for the
2016 ieee80211_ht_updateparams_final(struct ieee80211_node *ni,
2017 const uint8_t *htcapie, const uint8_t *htinfoie)
2019 struct ieee80211vap *vap = ni->ni_vap;
2020 const struct ieee80211_ie_htinfo *htinfo;
2021 int htflags, vhtflags;
2024 htinfo = (const struct ieee80211_ie_htinfo *) htinfoie;
2026 htflags = (vap->iv_flags_ht & IEEE80211_FHT_HT) ?
2027 IEEE80211_CHAN_HT20 : 0;
2029 /* NB: honor operating mode constraint */
2030 if ((htinfo->hi_byte1 & IEEE80211_HTINFO_TXWIDTH_2040) &&
2031 (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)) {
2032 if (ni->ni_ht2ndchan == IEEE80211_HTINFO_2NDCHAN_ABOVE)
2033 htflags = IEEE80211_CHAN_HT40U;
2034 else if (ni->ni_ht2ndchan == IEEE80211_HTINFO_2NDCHAN_BELOW)
2035 htflags = IEEE80211_CHAN_HT40D;
2039 * VHT flags - do much the same; check whether VHT is available
2040 * and if so, what our ideal channel use would be based on our
2041 * capabilities and the (pre-parsed) VHT info IE.
2043 vhtflags = ieee80211_vht_get_vhtflags(ni, htflags);
2045 if (htinfo_update_chw(ni, htflags, vhtflags))
2052 * Parse and update HT-related state extracted from the HT cap ie
2053 * for a station joining an HT BSS.
2055 * This is called from the hostap path for each station.
2058 ieee80211_ht_updatehtcap(struct ieee80211_node *ni, const uint8_t *htcapie)
2060 struct ieee80211vap *vap = ni->ni_vap;
2062 ieee80211_parse_htcap(ni, htcapie);
2063 if (vap->iv_htcaps & IEEE80211_HTC_SMPS)
2064 htcap_update_mimo_ps(ni);
2065 htcap_update_shortgi(ni);
2066 htcap_update_ldpc(ni);
2070 * Called once HT and VHT capabilities are parsed in hostap mode -
2071 * this will adjust the channel configuration of the given node
2072 * based on the configuration and capabilities.
2075 ieee80211_ht_updatehtcap_final(struct ieee80211_node *ni)
2077 struct ieee80211vap *vap = ni->ni_vap;
2081 /* NB: honor operating mode constraint */
2082 /* XXX 40 MHz intolerant */
2083 htflags = (vap->iv_flags_ht & IEEE80211_FHT_HT) ?
2084 IEEE80211_CHAN_HT20 : 0;
2085 if ((ni->ni_htcap & IEEE80211_HTCAP_CHWIDTH40) &&
2086 (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)) {
2087 if (IEEE80211_IS_CHAN_HT40U(vap->iv_bss->ni_chan))
2088 htflags = IEEE80211_CHAN_HT40U;
2089 else if (IEEE80211_IS_CHAN_HT40D(vap->iv_bss->ni_chan))
2090 htflags = IEEE80211_CHAN_HT40D;
2093 * VHT flags - do much the same; check whether VHT is available
2094 * and if so, what our ideal channel use would be based on our
2095 * capabilities and the (pre-parsed) VHT info IE.
2097 vhtflags = ieee80211_vht_get_vhtflags(ni, htflags);
2099 (void) htinfo_update_chw(ni, htflags, vhtflags);
2103 * Install received HT rate set by parsing the HT cap ie.
2106 ieee80211_setup_htrates(struct ieee80211_node *ni, const uint8_t *ie, int flags)
2108 struct ieee80211com *ic = ni->ni_ic;
2109 struct ieee80211vap *vap = ni->ni_vap;
2110 const struct ieee80211_ie_htcap *htcap;
2111 struct ieee80211_htrateset *rs;
2112 int i, maxequalmcs, maxunequalmcs;
2114 maxequalmcs = ic->ic_txstream * 8 - 1;
2116 if (ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL) {
2117 if (ic->ic_txstream >= 2)
2119 if (ic->ic_txstream >= 3)
2121 if (ic->ic_txstream >= 4)
2125 rs = &ni->ni_htrates;
2126 memset(rs, 0, sizeof(*rs));
2128 if (ie[0] == IEEE80211_ELEMID_VENDOR)
2130 htcap = (const struct ieee80211_ie_htcap *) ie;
2131 for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++) {
2132 if (isclr(htcap->hc_mcsset, i))
2134 if (rs->rs_nrates == IEEE80211_HTRATE_MAXSIZE) {
2136 IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
2137 "WARNING, HT rate set too large; only "
2138 "using %u rates", IEEE80211_HTRATE_MAXSIZE);
2139 vap->iv_stats.is_rx_rstoobig++;
2142 if (i <= 31 && i > maxequalmcs)
2145 (ic->ic_htcaps & IEEE80211_HTC_TXMCS32) == 0)
2147 if (i > 32 && i > maxunequalmcs)
2149 rs->rs_rates[rs->rs_nrates++] = i;
2152 return ieee80211_fix_rate(ni, (struct ieee80211_rateset *) rs, flags);
2156 * Mark rates in a node's HT rate set as basic according
2157 * to the information in the supplied HT info ie.
2160 ieee80211_setup_basic_htrates(struct ieee80211_node *ni, const uint8_t *ie)
2162 const struct ieee80211_ie_htinfo *htinfo;
2163 struct ieee80211_htrateset *rs;
2166 if (ie[0] == IEEE80211_ELEMID_VENDOR)
2168 htinfo = (const struct ieee80211_ie_htinfo *) ie;
2169 rs = &ni->ni_htrates;
2170 if (rs->rs_nrates == 0) {
2171 IEEE80211_NOTE(ni->ni_vap,
2172 IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
2173 "%s", "WARNING, empty HT rate set");
2176 for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++) {
2177 if (isclr(htinfo->hi_basicmcsset, i))
2179 for (j = 0; j < rs->rs_nrates; j++)
2180 if ((rs->rs_rates[j] & IEEE80211_RATE_VAL) == i)
2181 rs->rs_rates[j] |= IEEE80211_RATE_BASIC;
2186 ampdu_tx_setup(struct ieee80211_tx_ampdu *tap)
2188 callout_init(&tap->txa_timer, 1);
2189 tap->txa_flags |= IEEE80211_AGGR_SETUP;
2190 tap->txa_lastsample = ticks;
2194 ampdu_tx_stop(struct ieee80211_tx_ampdu *tap)
2196 struct ieee80211_node *ni = tap->txa_ni;
2197 struct ieee80211com *ic = ni->ni_ic;
2199 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2204 KASSERT(tap->txa_flags & IEEE80211_AGGR_SETUP,
2205 ("txa_flags 0x%x tid %d ac %d", tap->txa_flags, tap->txa_tid,
2206 TID_TO_WME_AC(tap->txa_tid)));
2209 * Stop BA stream if setup so driver has a chance
2210 * to reclaim any resources it might have allocated.
2212 ic->ic_addba_stop(ni, tap);
2214 * Stop any pending BAR transmit.
2216 bar_stop_timer(tap);
2219 * Reset packet estimate.
2221 ieee80211_txampdu_init_pps(tap);
2223 /* NB: clearing NAK means we may re-send ADDBA */
2224 tap->txa_flags &= ~(IEEE80211_AGGR_SETUP | IEEE80211_AGGR_NAK);
2228 * ADDBA response timeout.
2230 * If software aggregation and per-TID queue management was done here,
2231 * that queue would be unpaused after the ADDBA timeout occurs.
2234 addba_timeout(void *arg)
2236 struct ieee80211_tx_ampdu *tap = arg;
2237 struct ieee80211_node *ni = tap->txa_ni;
2238 struct ieee80211com *ic = ni->ni_ic;
2241 tap->txa_flags &= ~IEEE80211_AGGR_XCHGPEND;
2242 tap->txa_attempts++;
2243 ic->ic_addba_response_timeout(ni, tap);
2247 addba_start_timeout(struct ieee80211_tx_ampdu *tap)
2249 /* XXX use CALLOUT_PENDING instead? */
2250 callout_reset(&tap->txa_timer, ieee80211_addba_timeout,
2251 addba_timeout, tap);
2252 tap->txa_flags |= IEEE80211_AGGR_XCHGPEND;
2253 tap->txa_nextrequest = ticks + ieee80211_addba_timeout;
2257 addba_stop_timeout(struct ieee80211_tx_ampdu *tap)
2259 /* XXX use CALLOUT_PENDING instead? */
2260 if (tap->txa_flags & IEEE80211_AGGR_XCHGPEND) {
2261 callout_stop(&tap->txa_timer);
2262 tap->txa_flags &= ~IEEE80211_AGGR_XCHGPEND;
2267 null_addba_response_timeout(struct ieee80211_node *ni,
2268 struct ieee80211_tx_ampdu *tap)
2273 * Default method for requesting A-MPDU tx aggregation.
2274 * We setup the specified state block and start a timer
2275 * to wait for an ADDBA response frame.
2278 ieee80211_addba_request(struct ieee80211_node *ni,
2279 struct ieee80211_tx_ampdu *tap,
2280 int dialogtoken, int baparamset, int batimeout)
2285 tap->txa_token = dialogtoken;
2286 tap->txa_flags |= IEEE80211_AGGR_IMMEDIATE;
2287 bufsiz = _IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_BUFSIZ);
2288 tap->txa_wnd = (bufsiz == 0) ?
2289 IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
2290 addba_start_timeout(tap);
2295 * Called by drivers that wish to request an ADDBA session be
2296 * setup. This brings it up and starts the request timer.
2299 ieee80211_ampdu_tx_request_ext(struct ieee80211_node *ni, int tid)
2301 struct ieee80211_tx_ampdu *tap;
2303 if (tid < 0 || tid > 15)
2305 tap = &ni->ni_tx_ampdu[tid];
2308 if ((tap->txa_flags & IEEE80211_AGGR_SETUP) == 0) {
2309 /* do deferred setup of state */
2310 ampdu_tx_setup(tap);
2312 /* XXX hack for not doing proper locking */
2313 tap->txa_flags &= ~IEEE80211_AGGR_NAK;
2314 addba_start_timeout(tap);
2319 * Called by drivers that have marked a session as active.
2322 ieee80211_ampdu_tx_request_active_ext(struct ieee80211_node *ni, int tid,
2325 struct ieee80211_tx_ampdu *tap;
2327 if (tid < 0 || tid > 15)
2329 tap = &ni->ni_tx_ampdu[tid];
2332 addba_stop_timeout(tap);
2334 tap->txa_flags |= IEEE80211_AGGR_RUNNING;
2335 tap->txa_attempts = 0;
2337 /* mark tid so we don't try again */
2338 tap->txa_flags |= IEEE80211_AGGR_NAK;
2344 * Default method for processing an A-MPDU tx aggregation
2345 * response. We shutdown any pending timer and update the
2346 * state block according to the reply.
2349 ieee80211_addba_response(struct ieee80211_node *ni,
2350 struct ieee80211_tx_ampdu *tap,
2351 int status, int baparamset, int batimeout)
2353 struct ieee80211vap *vap = ni->ni_vap;
2357 addba_stop_timeout(tap);
2358 if (status == IEEE80211_STATUS_SUCCESS) {
2359 bufsiz = _IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_BUFSIZ);
2360 /* XXX override our request? */
2361 tap->txa_wnd = (bufsiz == 0) ?
2362 IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
2363 tid = _IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_TID);
2364 tap->txa_flags |= IEEE80211_AGGR_RUNNING;
2365 tap->txa_attempts = 0;
2366 /* TODO: this should be a vap flag */
2367 if ((vap->iv_htcaps & IEEE80211_HTC_TX_AMSDU_AMPDU) &&
2368 (ni->ni_flags & IEEE80211_NODE_AMSDU_TX) &&
2369 (_IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_AMSDU)))
2370 tap->txa_flags |= IEEE80211_AGGR_AMSDU;
2372 tap->txa_flags &= ~IEEE80211_AGGR_AMSDU;
2374 /* mark tid so we don't try again */
2375 tap->txa_flags |= IEEE80211_AGGR_NAK;
2381 * Default method for stopping A-MPDU tx aggregation.
2382 * Any timer is cleared and we drain any pending frames.
2385 ieee80211_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
2388 addba_stop_timeout(tap);
2389 if (tap->txa_flags & IEEE80211_AGGR_RUNNING) {
2390 /* XXX clear aggregation queue */
2391 tap->txa_flags &= ~(IEEE80211_AGGR_RUNNING | IEEE80211_AGGR_AMSDU);
2393 tap->txa_attempts = 0;
2397 * Process a received action frame using the default aggregation
2398 * policy. We intercept ADDBA-related frames and use them to
2399 * update our aggregation state. All other frames are passed up
2400 * for processing by ieee80211_recv_action.
2403 ht_recv_action_ba_addba_request(struct ieee80211_node *ni,
2404 const struct ieee80211_frame *wh,
2405 const uint8_t *frm, const uint8_t *efrm)
2407 struct ieee80211com *ic = ni->ni_ic;
2408 struct ieee80211vap *vap = ni->ni_vap;
2409 struct ieee80211_rx_ampdu *rap;
2410 uint8_t dialogtoken;
2411 uint16_t baparamset, batimeout, baseqctl;
2415 dialogtoken = frm[2];
2416 baparamset = le16dec(frm+3);
2417 batimeout = le16dec(frm+5);
2418 baseqctl = le16dec(frm+7);
2420 tid = _IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_TID);
2422 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2423 "recv ADDBA request: dialogtoken %u baparamset 0x%x "
2424 "(tid %d bufsiz %d) batimeout %d baseqctl %d:%d amsdu %d",
2425 dialogtoken, baparamset,
2426 tid, _IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_BUFSIZ),
2428 _IEEE80211_MASKSHIFT(baseqctl, IEEE80211_BASEQ_START),
2429 _IEEE80211_MASKSHIFT(baseqctl, IEEE80211_BASEQ_FRAG),
2430 _IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_AMSDU));
2432 rap = &ni->ni_rx_ampdu[tid];
2434 /* Send ADDBA response */
2435 args[0] = dialogtoken;
2437 * NB: We ack only if the sta associated with HT and
2438 * the ap is configured to do AMPDU rx (the latter
2439 * violates the 11n spec and is mostly for testing).
2441 if ((ni->ni_flags & IEEE80211_NODE_AMPDU_RX) &&
2442 (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_RX)) {
2443 /* XXX TODO: handle ampdu_rx_start failure */
2444 ic->ic_ampdu_rx_start(ni, rap,
2445 baparamset, batimeout, baseqctl);
2447 args[1] = IEEE80211_STATUS_SUCCESS;
2449 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2450 ni, "reject ADDBA request: %s",
2451 ni->ni_flags & IEEE80211_NODE_AMPDU_RX ?
2452 "administratively disabled" :
2453 "not negotiated for station");
2454 vap->iv_stats.is_addba_reject++;
2455 args[1] = IEEE80211_STATUS_UNSPECIFIED;
2457 /* XXX honor rap flags? */
2458 args[2] = IEEE80211_BAPS_POLICY_IMMEDIATE
2459 | _IEEE80211_SHIFTMASK(tid, IEEE80211_BAPS_TID)
2460 | _IEEE80211_SHIFTMASK(rap->rxa_wnd, IEEE80211_BAPS_BUFSIZ)
2464 * TODO: we're out of iv_flags_ht fields; once
2465 * this is extended we should make this configurable.
2467 if ((baparamset & IEEE80211_BAPS_AMSDU) &&
2468 (ni->ni_flags & IEEE80211_NODE_AMSDU_RX) &&
2469 (vap->iv_htcaps & IEEE80211_HTC_RX_AMSDU_AMPDU))
2470 args[2] |= IEEE80211_BAPS_AMSDU;
2474 ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
2475 IEEE80211_ACTION_BA_ADDBA_RESPONSE, args);
2480 ht_recv_action_ba_addba_response(struct ieee80211_node *ni,
2481 const struct ieee80211_frame *wh,
2482 const uint8_t *frm, const uint8_t *efrm)
2484 struct ieee80211com *ic = ni->ni_ic;
2485 struct ieee80211vap *vap = ni->ni_vap;
2486 struct ieee80211_tx_ampdu *tap;
2487 uint8_t dialogtoken, policy;
2488 uint16_t baparamset, batimeout, code;
2492 dialogtoken = frm[2];
2493 code = le16dec(frm+3);
2494 baparamset = le16dec(frm+5);
2495 tid = _IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_TID);
2496 bufsiz = _IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_BUFSIZ);
2497 policy = _IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_POLICY);
2498 amsdu = !! _IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_AMSDU);
2499 batimeout = le16dec(frm+7);
2501 tap = &ni->ni_tx_ampdu[tid];
2502 if ((tap->txa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
2503 IEEE80211_DISCARD_MAC(vap,
2504 IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2505 ni->ni_macaddr, "ADDBA response",
2506 "no pending ADDBA, tid %d dialogtoken %u "
2507 "code %d", tid, dialogtoken, code);
2508 vap->iv_stats.is_addba_norequest++;
2511 if (dialogtoken != tap->txa_token) {
2512 IEEE80211_DISCARD_MAC(vap,
2513 IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2514 ni->ni_macaddr, "ADDBA response",
2515 "dialogtoken mismatch: waiting for %d, "
2516 "received %d, tid %d code %d",
2517 tap->txa_token, dialogtoken, tid, code);
2518 vap->iv_stats.is_addba_badtoken++;
2521 /* NB: assumes IEEE80211_AGGR_IMMEDIATE is 1 */
2522 if (policy != (tap->txa_flags & IEEE80211_AGGR_IMMEDIATE)) {
2523 IEEE80211_DISCARD_MAC(vap,
2524 IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2525 ni->ni_macaddr, "ADDBA response",
2526 "policy mismatch: expecting %s, "
2527 "received %s, tid %d code %d",
2528 tap->txa_flags & IEEE80211_AGGR_IMMEDIATE,
2530 vap->iv_stats.is_addba_badpolicy++;
2534 /* XXX we take MIN in ieee80211_addba_response */
2535 if (bufsiz > IEEE80211_AGGR_BAWMAX) {
2536 IEEE80211_DISCARD_MAC(vap,
2537 IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2538 ni->ni_macaddr, "ADDBA response",
2539 "BA window too large: max %d, "
2540 "received %d, tid %d code %d",
2541 bufsiz, IEEE80211_AGGR_BAWMAX, tid, code);
2542 vap->iv_stats.is_addba_badbawinsize++;
2547 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2548 "recv ADDBA response: dialogtoken %u code %d "
2549 "baparamset 0x%x (tid %d bufsiz %d amsdu %d) batimeout %d",
2550 dialogtoken, code, baparamset, tid,
2554 ic->ic_addba_response(ni, tap, code, baparamset, batimeout);
2559 ht_recv_action_ba_delba(struct ieee80211_node *ni,
2560 const struct ieee80211_frame *wh,
2561 const uint8_t *frm, const uint8_t *efrm)
2563 struct ieee80211com *ic = ni->ni_ic;
2564 struct ieee80211_rx_ampdu *rap;
2565 struct ieee80211_tx_ampdu *tap;
2566 uint16_t baparamset, code;
2569 baparamset = le16dec(frm+2);
2570 code = le16dec(frm+4);
2572 tid = _IEEE80211_MASKSHIFT(baparamset, IEEE80211_DELBAPS_TID);
2574 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2575 "recv DELBA: baparamset 0x%x (tid %d initiator %d) "
2576 "code %d", baparamset, tid,
2577 _IEEE80211_MASKSHIFT(baparamset, IEEE80211_DELBAPS_INIT), code);
2579 if ((baparamset & IEEE80211_DELBAPS_INIT) == 0) {
2580 tap = &ni->ni_tx_ampdu[tid];
2581 ic->ic_addba_stop(ni, tap);
2583 rap = &ni->ni_rx_ampdu[tid];
2584 ic->ic_ampdu_rx_stop(ni, rap);
2590 ht_recv_action_ht_txchwidth(struct ieee80211_node *ni,
2591 const struct ieee80211_frame *wh,
2592 const uint8_t *frm, const uint8_t *efrm)
2596 chw = (frm[2] == IEEE80211_A_HT_TXCHWIDTH_2040) ? 40 : 20;
2598 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2599 "%s: HT txchwidth, width %d%s",
2600 __func__, chw, ni->ni_chw != chw ? "*" : "");
2601 if (chw != ni->ni_chw) {
2602 /* XXX does this need to change the ht40 station count? */
2604 /* XXX notify on change */
2610 ht_recv_action_ht_mimopwrsave(struct ieee80211_node *ni,
2611 const struct ieee80211_frame *wh,
2612 const uint8_t *frm, const uint8_t *efrm)
2614 const struct ieee80211_action_ht_mimopowersave *mps =
2615 (const struct ieee80211_action_ht_mimopowersave *) frm;
2617 /* XXX check iv_htcaps */
2618 if (mps->am_control & IEEE80211_A_HT_MIMOPWRSAVE_ENA)
2619 ni->ni_flags |= IEEE80211_NODE_MIMO_PS;
2621 ni->ni_flags &= ~IEEE80211_NODE_MIMO_PS;
2622 if (mps->am_control & IEEE80211_A_HT_MIMOPWRSAVE_MODE)
2623 ni->ni_flags |= IEEE80211_NODE_MIMO_RTS;
2625 ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS;
2626 /* XXX notify on change */
2627 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2628 "%s: HT MIMO PS (%s%s)", __func__,
2629 (ni->ni_flags & IEEE80211_NODE_MIMO_PS) ? "on" : "off",
2630 (ni->ni_flags & IEEE80211_NODE_MIMO_RTS) ? "+rts" : ""
2636 * Transmit processing.
2640 * Check if A-MPDU should be requested/enabled for a stream.
2641 * We require a traffic rate above a per-AC threshold and we
2642 * also handle backoff from previous failed attempts.
2644 * Drivers may override this method to bring in information
2645 * such as link state conditions in making the decision.
2648 ieee80211_ampdu_enable(struct ieee80211_node *ni,
2649 struct ieee80211_tx_ampdu *tap)
2651 struct ieee80211vap *vap = ni->ni_vap;
2653 if (tap->txa_avgpps <
2654 vap->iv_ampdu_mintraffic[TID_TO_WME_AC(tap->txa_tid)])
2656 /* XXX check rssi? */
2657 if (tap->txa_attempts >= ieee80211_addba_maxtries &&
2658 ieee80211_time_after(ticks, tap->txa_nextrequest)) {
2660 * Don't retry too often; txa_nextrequest is set
2661 * to the minimum interval we'll retry after
2662 * ieee80211_addba_maxtries failed attempts are made.
2666 IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
2667 "enable AMPDU on tid %d (%s), avgpps %d pkts %d attempt %d",
2668 tap->txa_tid, ieee80211_wme_acnames[TID_TO_WME_AC(tap->txa_tid)],
2669 tap->txa_avgpps, tap->txa_pkts, tap->txa_attempts);
2674 * Request A-MPDU tx aggregation. Setup local state and
2675 * issue an ADDBA request. BA use will only happen after
2676 * the other end replies with ADDBA response.
2679 ieee80211_ampdu_request(struct ieee80211_node *ni,
2680 struct ieee80211_tx_ampdu *tap)
2682 struct ieee80211com *ic = ni->ni_ic;
2684 int tid, dialogtoken;
2685 static int tokens = 0; /* XXX */
2688 if ((tap->txa_flags & IEEE80211_AGGR_SETUP) == 0) {
2689 /* do deferred setup of state */
2690 ampdu_tx_setup(tap);
2692 /* XXX hack for not doing proper locking */
2693 tap->txa_flags &= ~IEEE80211_AGGR_NAK;
2695 dialogtoken = (tokens+1) % 63; /* XXX */
2699 * XXX TODO: This is racy with any other parallel TX going on. :(
2701 tap->txa_start = ni->ni_txseqs[tid];
2703 args[0] = dialogtoken;
2704 args[1] = 0; /* NB: status code not used */
2705 args[2] = IEEE80211_BAPS_POLICY_IMMEDIATE
2706 | _IEEE80211_SHIFTMASK(tid, IEEE80211_BAPS_TID)
2707 | _IEEE80211_SHIFTMASK(IEEE80211_AGGR_BAWMAX,
2708 IEEE80211_BAPS_BUFSIZ)
2711 /* XXX TODO: this should be a flag, not iv_htcaps */
2712 if ((ni->ni_flags & IEEE80211_NODE_AMSDU_TX) &&
2713 (ni->ni_vap->iv_htcaps & IEEE80211_HTC_TX_AMSDU_AMPDU))
2714 args[2] |= IEEE80211_BAPS_AMSDU;
2716 args[3] = 0; /* batimeout */
2717 /* NB: do first so there's no race against reply */
2718 if (!ic->ic_addba_request(ni, tap, dialogtoken, args[2], args[3])) {
2719 /* unable to setup state, don't make request */
2720 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2721 ni, "%s: could not setup BA stream for TID %d AC %d",
2722 __func__, tap->txa_tid, TID_TO_WME_AC(tap->txa_tid));
2723 /* defer next try so we don't slam the driver with requests */
2724 tap->txa_attempts = ieee80211_addba_maxtries;
2725 /* NB: check in case driver wants to override */
2726 if (tap->txa_nextrequest <= ticks)
2727 tap->txa_nextrequest = ticks + ieee80211_addba_backoff;
2730 tokens = dialogtoken; /* allocate token */
2731 /* NB: after calling ic_addba_request so driver can set txa_start */
2732 args[4] = _IEEE80211_SHIFTMASK(tap->txa_start, IEEE80211_BASEQ_START)
2733 | _IEEE80211_SHIFTMASK(0, IEEE80211_BASEQ_FRAG)
2735 return ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
2736 IEEE80211_ACTION_BA_ADDBA_REQUEST, args);
2740 * Terminate an AMPDU tx stream. State is reclaimed
2741 * and the peer notified with a DelBA Action frame.
2744 ieee80211_ampdu_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
2747 struct ieee80211com *ic = ni->ni_ic;
2748 struct ieee80211vap *vap = ni->ni_vap;
2752 tap->txa_flags &= ~IEEE80211_AGGR_BARPEND;
2753 if (IEEE80211_AMPDU_RUNNING(tap)) {
2754 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2755 ni, "%s: stop BA stream for TID %d (reason: %d (%s))",
2756 __func__, tap->txa_tid, reason,
2757 ieee80211_reason_to_string(reason));
2758 vap->iv_stats.is_ampdu_stop++;
2760 ic->ic_addba_stop(ni, tap);
2761 args[0] = tap->txa_tid;
2762 args[1] = IEEE80211_DELBAPS_INIT;
2763 args[2] = reason; /* XXX reason code */
2764 ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
2765 IEEE80211_ACTION_BA_DELBA, args);
2767 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2768 ni, "%s: BA stream for TID %d not running "
2769 "(reason: %d (%s))", __func__, tap->txa_tid, reason,
2770 ieee80211_reason_to_string(reason));
2771 vap->iv_stats.is_ampdu_stop_failed++;
2776 static void bar_start_timer(struct ieee80211_tx_ampdu *tap);
2779 bar_timeout(void *arg)
2781 struct ieee80211_tx_ampdu *tap = arg;
2782 struct ieee80211_node *ni = tap->txa_ni;
2784 KASSERT((tap->txa_flags & IEEE80211_AGGR_XCHGPEND) == 0,
2785 ("bar/addba collision, flags 0x%x", tap->txa_flags));
2787 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2788 ni, "%s: tid %u flags 0x%x attempts %d", __func__,
2789 tap->txa_tid, tap->txa_flags, tap->txa_attempts);
2791 /* guard against race with bar_tx_complete */
2792 if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) == 0)
2795 if (tap->txa_attempts >= ieee80211_bar_maxtries) {
2796 struct ieee80211com *ic = ni->ni_ic;
2798 ni->ni_vap->iv_stats.is_ampdu_bar_tx_fail++;
2800 * If (at least) the last BAR TX timeout was due to
2801 * an ieee80211_send_bar() failures, then we need
2802 * to make sure we notify the driver that a BAR
2803 * TX did occur and fail. This gives the driver
2804 * a chance to undo any queue pause that may
2807 ic->ic_bar_response(ni, tap, 1);
2808 ieee80211_ampdu_stop(ni, tap, IEEE80211_REASON_TIMEOUT);
2810 ni->ni_vap->iv_stats.is_ampdu_bar_tx_retry++;
2811 if (ieee80211_send_bar(ni, tap, tap->txa_seqpending) != 0) {
2812 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2813 ni, "%s: failed to TX, starting timer\n",
2816 * If ieee80211_send_bar() fails here, the
2817 * timer may have stopped and/or the pending
2818 * flag may be clear. Because of this,
2819 * fake the BARPEND and reset the timer.
2820 * A retransmission attempt will then occur
2821 * during the next timeout.
2824 tap->txa_flags |= IEEE80211_AGGR_BARPEND;
2825 bar_start_timer(tap);
2831 bar_start_timer(struct ieee80211_tx_ampdu *tap)
2833 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2837 callout_reset(&tap->txa_timer, ieee80211_bar_timeout, bar_timeout, tap);
2841 bar_stop_timer(struct ieee80211_tx_ampdu *tap)
2843 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2847 callout_stop(&tap->txa_timer);
2851 bar_tx_complete(struct ieee80211_node *ni, void *arg, int status)
2853 struct ieee80211_tx_ampdu *tap = arg;
2855 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2856 ni, "%s: tid %u flags 0x%x pending %d status %d",
2857 __func__, tap->txa_tid, tap->txa_flags,
2858 callout_pending(&tap->txa_timer), status);
2860 ni->ni_vap->iv_stats.is_ampdu_bar_tx++;
2862 if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) &&
2863 callout_pending(&tap->txa_timer)) {
2864 struct ieee80211com *ic = ni->ni_ic;
2866 if (status == 0) /* ACK'd */
2867 bar_stop_timer(tap);
2868 ic->ic_bar_response(ni, tap, status);
2869 /* NB: just let timer expire so we pace requests */
2874 ieee80211_bar_response(struct ieee80211_node *ni,
2875 struct ieee80211_tx_ampdu *tap, int status)
2878 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2882 if (status == 0) { /* got ACK */
2883 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2884 ni, "BAR moves BA win <%u:%u> (%u frames) txseq %u tid %u",
2886 IEEE80211_SEQ_ADD(tap->txa_start, tap->txa_wnd-1),
2887 tap->txa_qframes, tap->txa_seqpending,
2890 /* NB: timer already stopped in bar_tx_complete */
2891 tap->txa_start = tap->txa_seqpending;
2892 tap->txa_flags &= ~IEEE80211_AGGR_BARPEND;
2897 * Transmit a BAR frame to the specified node. The
2898 * BAR contents are drawn from the supplied aggregation
2899 * state associated with the node.
2901 * NB: we only handle immediate ACK w/ compressed bitmap.
2904 ieee80211_send_bar(struct ieee80211_node *ni,
2905 struct ieee80211_tx_ampdu *tap, ieee80211_seq seq)
2907 #define senderr(_x, _v) do { vap->iv_stats._v++; ret = _x; goto bad; } while (0)
2908 struct ieee80211vap *vap = ni->ni_vap;
2909 struct ieee80211com *ic = ni->ni_ic;
2910 struct ieee80211_frame_bar *bar;
2912 uint16_t barctl, barseqctl;
2916 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2921 if ((tap->txa_flags & IEEE80211_AGGR_RUNNING) == 0) {
2922 /* no ADDBA response, should not happen */
2927 bar_stop_timer(tap);
2929 ieee80211_ref_node(ni);
2931 m = ieee80211_getmgtframe(&frm, ic->ic_headroom, sizeof(*bar));
2933 senderr(ENOMEM, is_tx_nobuf);
2935 if (!ieee80211_add_callback(m, bar_tx_complete, tap)) {
2937 senderr(ENOMEM, is_tx_nobuf); /* XXX */
2941 bar = mtod(m, struct ieee80211_frame_bar *);
2942 bar->i_fc[0] = IEEE80211_FC0_VERSION_0 |
2943 IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_BAR;
2945 IEEE80211_ADDR_COPY(bar->i_ra, ni->ni_macaddr);
2946 IEEE80211_ADDR_COPY(bar->i_ta, vap->iv_myaddr);
2949 barctl = (tap->txa_flags & IEEE80211_AGGR_IMMEDIATE ?
2950 0 : IEEE80211_BAR_NOACK)
2951 | IEEE80211_BAR_COMP
2952 | _IEEE80211_SHIFTMASK(tid, IEEE80211_BAR_TID)
2954 barseqctl = _IEEE80211_SHIFTMASK(seq, IEEE80211_BAR_SEQ_START);
2955 /* NB: known to have proper alignment */
2956 bar->i_ctl = htole16(barctl);
2957 bar->i_seq = htole16(barseqctl);
2958 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_bar);
2960 M_WME_SETAC(m, WME_AC_VO);
2962 IEEE80211_NODE_STAT(ni, tx_mgmt); /* XXX tx_ctl? */
2965 /* init/bump attempts counter */
2966 if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) == 0)
2967 tap->txa_attempts = 1;
2969 tap->txa_attempts++;
2970 tap->txa_seqpending = seq;
2971 tap->txa_flags |= IEEE80211_AGGR_BARPEND;
2973 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_11N,
2974 ni, "send BAR: tid %u ctl 0x%x start %u (attempt %d)",
2975 tid, barctl, seq, tap->txa_attempts);
2978 * ic_raw_xmit will free the node reference
2979 * regardless of queue/TX success or failure.
2981 IEEE80211_TX_LOCK(ic);
2982 ret = ieee80211_raw_output(vap, ni, m, NULL);
2983 IEEE80211_TX_UNLOCK(ic);
2985 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_11N,
2986 ni, "send BAR: failed: (ret = %d)\n",
2988 /* xmit failed, clear state flag */
2989 tap->txa_flags &= ~IEEE80211_AGGR_BARPEND;
2990 vap->iv_stats.is_ampdu_bar_tx_fail++;
2993 /* XXX hack against tx complete happening before timer is started */
2994 if (tap->txa_flags & IEEE80211_AGGR_BARPEND)
2995 bar_start_timer(tap);
2998 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
3002 vap->iv_stats.is_ampdu_bar_tx_fail++;
3003 ieee80211_free_node(ni);
3009 ht_action_output(struct ieee80211_node *ni, struct mbuf *m)
3011 struct ieee80211_bpf_params params;
3013 memset(¶ms, 0, sizeof(params));
3014 params.ibp_pri = WME_AC_VO;
3015 params.ibp_rate0 = ni->ni_txparms->mgmtrate;
3016 /* NB: we know all frames are unicast */
3017 params.ibp_try0 = ni->ni_txparms->maxretry;
3018 params.ibp_power = ni->ni_txpower;
3019 return ieee80211_mgmt_output(ni, m, IEEE80211_FC0_SUBTYPE_ACTION,
3023 #define ADDSHORT(frm, v) do { \
3024 frm[0] = (v) & 0xff; \
3025 frm[1] = (v) >> 8; \
3030 * Send an action management frame. The arguments are stuff
3031 * into a frame without inspection; the caller is assumed to
3032 * prepare them carefully (e.g. based on the aggregation state).
3035 ht_send_action_ba_addba(struct ieee80211_node *ni,
3036 int category, int action, void *arg0)
3038 struct ieee80211vap *vap = ni->ni_vap;
3039 struct ieee80211com *ic = ni->ni_ic;
3040 uint16_t *args = arg0;
3044 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
3045 "send ADDBA %s: dialogtoken %d status %d "
3046 "baparamset 0x%x (tid %d amsdu %d) batimeout 0x%x baseqctl 0x%x",
3047 (action == IEEE80211_ACTION_BA_ADDBA_REQUEST) ?
3048 "request" : "response", args[0], args[1], args[2],
3049 _IEEE80211_MASKSHIFT(args[2], IEEE80211_BAPS_TID),
3050 _IEEE80211_MASKSHIFT(args[2], IEEE80211_BAPS_AMSDU),
3053 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
3054 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
3055 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
3056 ieee80211_ref_node(ni);
3058 m = ieee80211_getmgtframe(&frm,
3059 ic->ic_headroom + sizeof(struct ieee80211_frame),
3060 sizeof(uint16_t) /* action+category */
3061 /* XXX may action payload */
3062 + sizeof(struct ieee80211_action_ba_addbaresponse)
3067 *frm++ = args[0]; /* dialog token */
3068 if (action == IEEE80211_ACTION_BA_ADDBA_RESPONSE)
3069 ADDSHORT(frm, args[1]); /* status code */
3070 ADDSHORT(frm, args[2]); /* baparamset */
3071 ADDSHORT(frm, args[3]); /* batimeout */
3072 if (action == IEEE80211_ACTION_BA_ADDBA_REQUEST)
3073 ADDSHORT(frm, args[4]); /* baseqctl */
3074 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
3075 return ht_action_output(ni, m);
3077 vap->iv_stats.is_tx_nobuf++;
3078 ieee80211_free_node(ni);
3084 ht_send_action_ba_delba(struct ieee80211_node *ni,
3085 int category, int action, void *arg0)
3087 struct ieee80211vap *vap = ni->ni_vap;
3088 struct ieee80211com *ic = ni->ni_ic;
3089 uint16_t *args = arg0;
3091 uint16_t baparamset;
3094 baparamset = _IEEE80211_SHIFTMASK(args[0], IEEE80211_DELBAPS_TID)
3097 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
3098 "send DELBA action: tid %d, initiator %d reason %d (%s)",
3099 args[0], args[1], args[2], ieee80211_reason_to_string(args[2]));
3101 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
3102 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
3103 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
3104 ieee80211_ref_node(ni);
3106 m = ieee80211_getmgtframe(&frm,
3107 ic->ic_headroom + sizeof(struct ieee80211_frame),
3108 sizeof(uint16_t) /* action+category */
3109 /* XXX may action payload */
3110 + sizeof(struct ieee80211_action_ba_addbaresponse)
3115 ADDSHORT(frm, baparamset);
3116 ADDSHORT(frm, args[2]); /* reason code */
3117 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
3118 return ht_action_output(ni, m);
3120 vap->iv_stats.is_tx_nobuf++;
3121 ieee80211_free_node(ni);
3127 ht_send_action_ht_txchwidth(struct ieee80211_node *ni,
3128 int category, int action, void *arg0)
3130 struct ieee80211vap *vap = ni->ni_vap;
3131 struct ieee80211com *ic = ni->ni_ic;
3135 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
3136 "send HT txchwidth: width %d",
3137 IEEE80211_IS_CHAN_HT40(ni->ni_chan) ? 40 : 20);
3139 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
3140 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
3141 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
3142 ieee80211_ref_node(ni);
3144 m = ieee80211_getmgtframe(&frm,
3145 ic->ic_headroom + sizeof(struct ieee80211_frame),
3146 sizeof(uint16_t) /* action+category */
3147 /* XXX may action payload */
3148 + sizeof(struct ieee80211_action_ba_addbaresponse)
3153 *frm++ = IEEE80211_IS_CHAN_HT40(ni->ni_chan) ?
3154 IEEE80211_A_HT_TXCHWIDTH_2040 :
3155 IEEE80211_A_HT_TXCHWIDTH_20;
3156 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
3157 return ht_action_output(ni, m);
3159 vap->iv_stats.is_tx_nobuf++;
3160 ieee80211_free_node(ni);
3167 * Construct the MCS bit mask for inclusion in an HT capabilities
3168 * information element.
3171 ieee80211_set_mcsset(struct ieee80211com *ic, uint8_t *frm)
3176 KASSERT((ic->ic_rxstream > 0 && ic->ic_rxstream <= 4),
3177 ("ic_rxstream %d out of range", ic->ic_rxstream));
3178 KASSERT((ic->ic_txstream > 0 && ic->ic_txstream <= 4),
3179 ("ic_txstream %d out of range", ic->ic_txstream));
3181 for (i = 0; i < ic->ic_rxstream * 8; i++)
3183 if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
3184 (ic->ic_htcaps & IEEE80211_HTC_RXMCS32))
3186 if (ic->ic_htcaps & IEEE80211_HTC_RXUNEQUAL) {
3187 if (ic->ic_rxstream >= 2) {
3188 for (i = 33; i <= 38; i++)
3191 if (ic->ic_rxstream >= 3) {
3192 for (i = 39; i <= 52; i++)
3195 if (ic->ic_txstream >= 4) {
3196 for (i = 53; i <= 76; i++)
3201 if (ic->ic_rxstream != ic->ic_txstream) {
3202 txparams = 0x1; /* TX MCS set defined */
3203 txparams |= 0x2; /* TX RX MCS not equal */
3204 txparams |= (ic->ic_txstream - 1) << 2; /* num TX streams */
3205 if (ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL)
3206 txparams |= 0x16; /* TX unequal modulation sup */
3213 * Add body of an HTCAP information element.
3216 ieee80211_add_htcap_body(uint8_t *frm, struct ieee80211_node *ni)
3218 #define ADDSHORT(frm, v) do { \
3219 frm[0] = (v) & 0xff; \
3220 frm[1] = (v) >> 8; \
3223 struct ieee80211com *ic = ni->ni_ic;
3224 struct ieee80211vap *vap = ni->ni_vap;
3225 uint16_t caps, extcaps;
3228 /* HT capabilities */
3229 caps = vap->iv_htcaps & 0xffff;
3231 * Note channel width depends on whether we are operating as
3232 * a sta or not. When operating as a sta we are generating
3233 * a request based on our desired configuration. Otherwise
3234 * we are operational and the channel attributes identify
3235 * how we've been setup (which might be different if a fixed
3236 * channel is specified).
3238 if (vap->iv_opmode == IEEE80211_M_STA) {
3239 /* override 20/40 use based on config */
3240 if (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)
3241 caps |= IEEE80211_HTCAP_CHWIDTH40;
3243 caps &= ~IEEE80211_HTCAP_CHWIDTH40;
3245 /* Start by using the advertised settings */
3246 rxmax = _IEEE80211_MASKSHIFT(ni->ni_htparam,
3247 IEEE80211_HTCAP_MAXRXAMPDU);
3248 density = _IEEE80211_MASKSHIFT(ni->ni_htparam,
3249 IEEE80211_HTCAP_MPDUDENSITY);
3251 IEEE80211_DPRINTF(vap, IEEE80211_MSG_11N,
3252 "%s: advertised rxmax=%d, density=%d, vap rxmax=%d, density=%d\n",
3256 vap->iv_ampdu_rxmax,
3257 vap->iv_ampdu_density);
3259 /* Cap at VAP rxmax */
3260 if (rxmax > vap->iv_ampdu_rxmax)
3261 rxmax = vap->iv_ampdu_rxmax;
3264 * If the VAP ampdu density value greater, use that.
3266 * (Larger density value == larger minimum gap between A-MPDU
3269 if (vap->iv_ampdu_density > density)
3270 density = vap->iv_ampdu_density;
3273 * NB: Hardware might support HT40 on some but not all
3274 * channels. We can't determine this earlier because only
3275 * after association the channel is upgraded to HT based
3276 * on the negotiated capabilities.
3278 if (ni->ni_chan != IEEE80211_CHAN_ANYC &&
3279 findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT40U) == NULL &&
3280 findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT40D) == NULL)
3281 caps &= ~IEEE80211_HTCAP_CHWIDTH40;
3283 /* override 20/40 use based on current channel */
3284 if (IEEE80211_IS_CHAN_HT40(ni->ni_chan))
3285 caps |= IEEE80211_HTCAP_CHWIDTH40;
3287 caps &= ~IEEE80211_HTCAP_CHWIDTH40;
3289 /* XXX TODO should it start by using advertised settings? */
3290 rxmax = vap->iv_ampdu_rxmax;
3291 density = vap->iv_ampdu_density;
3294 /* adjust short GI based on channel and config */
3295 if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20) == 0)
3296 caps &= ~IEEE80211_HTCAP_SHORTGI20;
3297 if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40) == 0 ||
3298 (caps & IEEE80211_HTCAP_CHWIDTH40) == 0)
3299 caps &= ~IEEE80211_HTCAP_SHORTGI40;
3301 /* adjust STBC based on receive capabilities */
3302 if ((vap->iv_flags_ht & IEEE80211_FHT_STBC_RX) == 0)
3303 caps &= ~IEEE80211_HTCAP_RXSTBC;
3305 /* adjust LDPC based on receive capabilites */
3306 if ((vap->iv_flags_ht & IEEE80211_FHT_LDPC_RX) == 0)
3307 caps &= ~IEEE80211_HTCAP_LDPC;
3309 ADDSHORT(frm, caps);
3312 *frm = _IEEE80211_SHIFTMASK(rxmax, IEEE80211_HTCAP_MAXRXAMPDU)
3313 | _IEEE80211_SHIFTMASK(density, IEEE80211_HTCAP_MPDUDENSITY)
3317 /* pre-zero remainder of ie */
3318 memset(frm, 0, sizeof(struct ieee80211_ie_htcap) -
3319 __offsetof(struct ieee80211_ie_htcap, hc_mcsset));
3321 /* supported MCS set */
3323 * XXX: For sta mode the rate set should be restricted based
3324 * on the AP's capabilities, but ni_htrates isn't setup when
3325 * we're called to form an AssocReq frame so for now we're
3326 * restricted to the device capabilities.
3328 ieee80211_set_mcsset(ni->ni_ic, frm);
3330 frm += __offsetof(struct ieee80211_ie_htcap, hc_extcap) -
3331 __offsetof(struct ieee80211_ie_htcap, hc_mcsset);
3333 /* HT extended capabilities */
3334 extcaps = vap->iv_htextcaps & 0xffff;
3336 ADDSHORT(frm, extcaps);
3338 frm += sizeof(struct ieee80211_ie_htcap) -
3339 __offsetof(struct ieee80211_ie_htcap, hc_txbf);
3346 * Add 802.11n HT capabilities information element
3349 ieee80211_add_htcap(uint8_t *frm, struct ieee80211_node *ni)
3351 frm[0] = IEEE80211_ELEMID_HTCAP;
3352 frm[1] = sizeof(struct ieee80211_ie_htcap) - 2;
3353 return ieee80211_add_htcap_body(frm + 2, ni);
3357 * Non-associated probe request - add HT capabilities based on
3358 * the current channel configuration.
3361 ieee80211_add_htcap_body_ch(uint8_t *frm, struct ieee80211vap *vap,
3362 struct ieee80211_channel *c)
3364 #define ADDSHORT(frm, v) do { \
3365 frm[0] = (v) & 0xff; \
3366 frm[1] = (v) >> 8; \
3369 struct ieee80211com *ic = vap->iv_ic;
3370 uint16_t caps, extcaps;
3373 /* HT capabilities */
3374 caps = vap->iv_htcaps & 0xffff;
3377 * We don't use this in STA mode; only in IBSS mode.
3378 * So in IBSS mode we base our HTCAP flags on the
3382 /* override 20/40 use based on current channel */
3383 if (IEEE80211_IS_CHAN_HT40(c))
3384 caps |= IEEE80211_HTCAP_CHWIDTH40;
3386 caps &= ~IEEE80211_HTCAP_CHWIDTH40;
3388 /* Use the currently configured values */
3389 rxmax = vap->iv_ampdu_rxmax;
3390 density = vap->iv_ampdu_density;
3392 /* adjust short GI based on channel and config */
3393 if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20) == 0)
3394 caps &= ~IEEE80211_HTCAP_SHORTGI20;
3395 if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40) == 0 ||
3396 (caps & IEEE80211_HTCAP_CHWIDTH40) == 0)
3397 caps &= ~IEEE80211_HTCAP_SHORTGI40;
3398 ADDSHORT(frm, caps);
3401 *frm = _IEEE80211_SHIFTMASK(rxmax, IEEE80211_HTCAP_MAXRXAMPDU)
3402 | _IEEE80211_SHIFTMASK(density, IEEE80211_HTCAP_MPDUDENSITY)
3406 /* pre-zero remainder of ie */
3407 memset(frm, 0, sizeof(struct ieee80211_ie_htcap) -
3408 __offsetof(struct ieee80211_ie_htcap, hc_mcsset));
3410 /* supported MCS set */
3412 * XXX: For sta mode the rate set should be restricted based
3413 * on the AP's capabilities, but ni_htrates isn't setup when
3414 * we're called to form an AssocReq frame so for now we're
3415 * restricted to the device capabilities.
3417 ieee80211_set_mcsset(ic, frm);
3419 frm += __offsetof(struct ieee80211_ie_htcap, hc_extcap) -
3420 __offsetof(struct ieee80211_ie_htcap, hc_mcsset);
3422 /* HT extended capabilities */
3423 extcaps = vap->iv_htextcaps & 0xffff;
3425 ADDSHORT(frm, extcaps);
3427 frm += sizeof(struct ieee80211_ie_htcap) -
3428 __offsetof(struct ieee80211_ie_htcap, hc_txbf);
3435 * Add 802.11n HT capabilities information element
3438 ieee80211_add_htcap_ch(uint8_t *frm, struct ieee80211vap *vap,
3439 struct ieee80211_channel *c)
3441 frm[0] = IEEE80211_ELEMID_HTCAP;
3442 frm[1] = sizeof(struct ieee80211_ie_htcap) - 2;
3443 return ieee80211_add_htcap_body_ch(frm + 2, vap, c);
3447 * Add Broadcom OUI wrapped standard HTCAP ie; this is
3448 * used for compatibility w/ pre-draft implementations.
3451 ieee80211_add_htcap_vendor(uint8_t *frm, struct ieee80211_node *ni)
3453 frm[0] = IEEE80211_ELEMID_VENDOR;
3454 frm[1] = 4 + sizeof(struct ieee80211_ie_htcap) - 2;
3455 frm[2] = (BCM_OUI >> 0) & 0xff;
3456 frm[3] = (BCM_OUI >> 8) & 0xff;
3457 frm[4] = (BCM_OUI >> 16) & 0xff;
3458 frm[5] = BCM_OUI_HTCAP;
3459 return ieee80211_add_htcap_body(frm + 6, ni);
3463 * Construct the MCS bit mask of basic rates
3464 * for inclusion in an HT information element.
3467 ieee80211_set_basic_htrates(uint8_t *frm, const struct ieee80211_htrateset *rs)
3471 for (i = 0; i < rs->rs_nrates; i++) {
3472 int r = rs->rs_rates[i] & IEEE80211_RATE_VAL;
3473 if ((rs->rs_rates[i] & IEEE80211_RATE_BASIC) &&
3474 r < IEEE80211_HTRATE_MAXSIZE) {
3475 /* NB: this assumes a particular implementation */
3482 * Update the HTINFO ie for a beacon frame.
3485 ieee80211_ht_update_beacon(struct ieee80211vap *vap,
3486 struct ieee80211_beacon_offsets *bo)
3488 #define PROTMODE (IEEE80211_HTINFO_OPMODE|IEEE80211_HTINFO_NONHT_PRESENT)
3489 struct ieee80211_node *ni;
3490 const struct ieee80211_channel *bsschan;
3491 struct ieee80211com *ic = vap->iv_ic;
3492 struct ieee80211_ie_htinfo *ht =
3493 (struct ieee80211_ie_htinfo *) bo->bo_htinfo;
3495 ni = ieee80211_ref_node(vap->iv_bss);
3496 bsschan = ni->ni_chan;
3498 /* XXX only update on channel change */
3499 ht->hi_ctrlchannel = ieee80211_chan2ieee(ic, bsschan);
3500 if (vap->iv_flags_ht & IEEE80211_FHT_RIFS)
3501 ht->hi_byte1 = IEEE80211_HTINFO_RIFSMODE_PERM;
3503 ht->hi_byte1 = IEEE80211_HTINFO_RIFSMODE_PROH;
3504 if (IEEE80211_IS_CHAN_HT40U(bsschan))
3505 ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_ABOVE;
3506 else if (IEEE80211_IS_CHAN_HT40D(bsschan))
3507 ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_BELOW;
3509 ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_NONE;
3510 if (IEEE80211_IS_CHAN_HT40(bsschan))
3511 ht->hi_byte1 |= IEEE80211_HTINFO_TXWIDTH_2040;
3513 /* protection mode */
3515 * XXX TODO: this uses the global flag, not the per-VAP flag.
3516 * Eventually (once the protection modes are done per-channel
3517 * rather than per-VAP) we can flip this over to be per-VAP but
3518 * using the channel protection mode.
3520 ht->hi_byte2 = (ht->hi_byte2 &~ PROTMODE) | ic->ic_curhtprotmode;
3522 ieee80211_free_node(ni);
3524 /* XXX propagate to vendor ie's */
3529 * Add body of an HTINFO information element.
3531 * NB: We don't use struct ieee80211_ie_htinfo because we can
3532 * be called to fillin both a standard ie and a compat ie that
3533 * has a vendor OUI at the front.
3536 ieee80211_add_htinfo_body(uint8_t *frm, struct ieee80211_node *ni)
3538 struct ieee80211vap *vap = ni->ni_vap;
3539 struct ieee80211com *ic = ni->ni_ic;
3541 /* pre-zero remainder of ie */
3542 memset(frm, 0, sizeof(struct ieee80211_ie_htinfo) - 2);
3544 /* primary/control channel center */
3545 *frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
3547 if (vap->iv_flags_ht & IEEE80211_FHT_RIFS)
3548 frm[0] = IEEE80211_HTINFO_RIFSMODE_PERM;
3550 frm[0] = IEEE80211_HTINFO_RIFSMODE_PROH;
3551 if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan))
3552 frm[0] |= IEEE80211_HTINFO_2NDCHAN_ABOVE;
3553 else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan))
3554 frm[0] |= IEEE80211_HTINFO_2NDCHAN_BELOW;
3556 frm[0] |= IEEE80211_HTINFO_2NDCHAN_NONE;
3557 if (IEEE80211_IS_CHAN_HT40(ni->ni_chan))
3558 frm[0] |= IEEE80211_HTINFO_TXWIDTH_2040;
3561 * Add current protection mode. Unlike for beacons,
3562 * this will respect the per-VAP flags.
3564 frm[1] = vap->iv_curhtprotmode;
3569 ieee80211_set_basic_htrates(frm, &ni->ni_htrates);
3570 frm += sizeof(struct ieee80211_ie_htinfo) -
3571 __offsetof(struct ieee80211_ie_htinfo, hi_basicmcsset);
3576 * Add 802.11n HT information element.
3579 ieee80211_add_htinfo(uint8_t *frm, struct ieee80211_node *ni)
3581 frm[0] = IEEE80211_ELEMID_HTINFO;
3582 frm[1] = sizeof(struct ieee80211_ie_htinfo) - 2;
3583 return ieee80211_add_htinfo_body(frm + 2, ni);
3587 * Add Broadcom OUI wrapped standard HTINFO ie; this is
3588 * used for compatibility w/ pre-draft implementations.
3591 ieee80211_add_htinfo_vendor(uint8_t *frm, struct ieee80211_node *ni)
3593 frm[0] = IEEE80211_ELEMID_VENDOR;
3594 frm[1] = 4 + sizeof(struct ieee80211_ie_htinfo) - 2;
3595 frm[2] = (BCM_OUI >> 0) & 0xff;
3596 frm[3] = (BCM_OUI >> 8) & 0xff;
3597 frm[4] = (BCM_OUI >> 16) & 0xff;
3598 frm[5] = BCM_OUI_HTINFO;
3599 return ieee80211_add_htinfo_body(frm + 6, ni);