2 * Copyright (c) 2007-2008 Sam Leffler, Errno Consulting
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
32 * IEEE 802.11n protocol support.
38 #include <sys/param.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/systm.h>
42 #include <sys/endian.h>
44 #include <sys/socket.h>
47 #include <net/if_var.h>
48 #include <net/if_media.h>
49 #include <net/ethernet.h>
51 #include <net80211/ieee80211_var.h>
52 #include <net80211/ieee80211_action.h>
53 #include <net80211/ieee80211_input.h>
55 /* define here, used throughout file */
56 #define MS(_v, _f) (((_v) & _f) >> _f##_S)
57 #define SM(_v, _f) (((_v) << _f##_S) & _f)
59 const struct ieee80211_mcs_rates ieee80211_htrates[IEEE80211_HTRATE_MAXSIZE] = {
60 { 13, 14, 27, 30 }, /* MCS 0 */
61 { 26, 29, 54, 60 }, /* MCS 1 */
62 { 39, 43, 81, 90 }, /* MCS 2 */
63 { 52, 58, 108, 120 }, /* MCS 3 */
64 { 78, 87, 162, 180 }, /* MCS 4 */
65 { 104, 116, 216, 240 }, /* MCS 5 */
66 { 117, 130, 243, 270 }, /* MCS 6 */
67 { 130, 144, 270, 300 }, /* MCS 7 */
68 { 26, 29, 54, 60 }, /* MCS 8 */
69 { 52, 58, 108, 120 }, /* MCS 9 */
70 { 78, 87, 162, 180 }, /* MCS 10 */
71 { 104, 116, 216, 240 }, /* MCS 11 */
72 { 156, 173, 324, 360 }, /* MCS 12 */
73 { 208, 231, 432, 480 }, /* MCS 13 */
74 { 234, 260, 486, 540 }, /* MCS 14 */
75 { 260, 289, 540, 600 }, /* MCS 15 */
76 { 39, 43, 81, 90 }, /* MCS 16 */
77 { 78, 87, 162, 180 }, /* MCS 17 */
78 { 117, 130, 243, 270 }, /* MCS 18 */
79 { 156, 173, 324, 360 }, /* MCS 19 */
80 { 234, 260, 486, 540 }, /* MCS 20 */
81 { 312, 347, 648, 720 }, /* MCS 21 */
82 { 351, 390, 729, 810 }, /* MCS 22 */
83 { 390, 433, 810, 900 }, /* MCS 23 */
84 { 52, 58, 108, 120 }, /* MCS 24 */
85 { 104, 116, 216, 240 }, /* MCS 25 */
86 { 156, 173, 324, 360 }, /* MCS 26 */
87 { 208, 231, 432, 480 }, /* MCS 27 */
88 { 312, 347, 648, 720 }, /* MCS 28 */
89 { 416, 462, 864, 960 }, /* MCS 29 */
90 { 468, 520, 972, 1080 }, /* MCS 30 */
91 { 520, 578, 1080, 1200 }, /* MCS 31 */
92 { 0, 0, 12, 13 }, /* MCS 32 */
93 { 78, 87, 162, 180 }, /* MCS 33 */
94 { 104, 116, 216, 240 }, /* MCS 34 */
95 { 130, 144, 270, 300 }, /* MCS 35 */
96 { 117, 130, 243, 270 }, /* MCS 36 */
97 { 156, 173, 324, 360 }, /* MCS 37 */
98 { 195, 217, 405, 450 }, /* MCS 38 */
99 { 104, 116, 216, 240 }, /* MCS 39 */
100 { 130, 144, 270, 300 }, /* MCS 40 */
101 { 130, 144, 270, 300 }, /* MCS 41 */
102 { 156, 173, 324, 360 }, /* MCS 42 */
103 { 182, 202, 378, 420 }, /* MCS 43 */
104 { 182, 202, 378, 420 }, /* MCS 44 */
105 { 208, 231, 432, 480 }, /* MCS 45 */
106 { 156, 173, 324, 360 }, /* MCS 46 */
107 { 195, 217, 405, 450 }, /* MCS 47 */
108 { 195, 217, 405, 450 }, /* MCS 48 */
109 { 234, 260, 486, 540 }, /* MCS 49 */
110 { 273, 303, 567, 630 }, /* MCS 50 */
111 { 273, 303, 567, 630 }, /* MCS 51 */
112 { 312, 347, 648, 720 }, /* MCS 52 */
113 { 130, 144, 270, 300 }, /* MCS 53 */
114 { 156, 173, 324, 360 }, /* MCS 54 */
115 { 182, 202, 378, 420 }, /* MCS 55 */
116 { 156, 173, 324, 360 }, /* MCS 56 */
117 { 182, 202, 378, 420 }, /* MCS 57 */
118 { 208, 231, 432, 480 }, /* MCS 58 */
119 { 234, 260, 486, 540 }, /* MCS 59 */
120 { 208, 231, 432, 480 }, /* MCS 60 */
121 { 234, 260, 486, 540 }, /* MCS 61 */
122 { 260, 289, 540, 600 }, /* MCS 62 */
123 { 260, 289, 540, 600 }, /* MCS 63 */
124 { 286, 318, 594, 660 }, /* MCS 64 */
125 { 195, 217, 405, 450 }, /* MCS 65 */
126 { 234, 260, 486, 540 }, /* MCS 66 */
127 { 273, 303, 567, 630 }, /* MCS 67 */
128 { 234, 260, 486, 540 }, /* MCS 68 */
129 { 273, 303, 567, 630 }, /* MCS 69 */
130 { 312, 347, 648, 720 }, /* MCS 70 */
131 { 351, 390, 729, 810 }, /* MCS 71 */
132 { 312, 347, 648, 720 }, /* MCS 72 */
133 { 351, 390, 729, 810 }, /* MCS 73 */
134 { 390, 433, 810, 900 }, /* MCS 74 */
135 { 390, 433, 810, 900 }, /* MCS 75 */
136 { 429, 477, 891, 990 }, /* MCS 76 */
139 static int ieee80211_ampdu_age = -1; /* threshold for ampdu reorder q (ms) */
140 SYSCTL_PROC(_net_wlan, OID_AUTO, ampdu_age, CTLTYPE_INT | CTLFLAG_RW,
141 &ieee80211_ampdu_age, 0, ieee80211_sysctl_msecs_ticks, "I",
142 "AMPDU max reorder age (ms)");
144 static int ieee80211_recv_bar_ena = 1;
145 SYSCTL_INT(_net_wlan, OID_AUTO, recv_bar, CTLFLAG_RW, &ieee80211_recv_bar_ena,
146 0, "BAR frame processing (ena/dis)");
148 static int ieee80211_addba_timeout = -1;/* timeout for ADDBA response */
149 SYSCTL_PROC(_net_wlan, OID_AUTO, addba_timeout, CTLTYPE_INT | CTLFLAG_RW,
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, CTLTYPE_INT | CTLFLAG_RW,
154 &ieee80211_addba_backoff, 0, ieee80211_sysctl_msecs_ticks, "I",
155 "ADDBA request backoff (ms)");
156 static int ieee80211_addba_maxtries = 3;/* max ADDBA requests before backoff */
157 SYSCTL_INT(_net_wlan, OID_AUTO, addba_maxtries, CTLFLAG_RW,
158 &ieee80211_addba_maxtries, 0, "max ADDBA requests sent before backoff");
160 static int ieee80211_bar_timeout = -1; /* timeout waiting for BAR response */
161 static int ieee80211_bar_maxtries = 50;/* max BAR requests before DELBA */
163 static ieee80211_recv_action_func ht_recv_action_ba_addba_request;
164 static ieee80211_recv_action_func ht_recv_action_ba_addba_response;
165 static ieee80211_recv_action_func ht_recv_action_ba_delba;
166 static ieee80211_recv_action_func ht_recv_action_ht_mimopwrsave;
167 static ieee80211_recv_action_func ht_recv_action_ht_txchwidth;
169 static ieee80211_send_action_func ht_send_action_ba_addba;
170 static ieee80211_send_action_func ht_send_action_ba_delba;
171 static ieee80211_send_action_func ht_send_action_ht_txchwidth;
174 ieee80211_ht_init(void)
177 * Setup HT parameters that depends on the clock frequency.
179 ieee80211_ampdu_age = msecs_to_ticks(500);
180 ieee80211_addba_timeout = msecs_to_ticks(250);
181 ieee80211_addba_backoff = msecs_to_ticks(10*1000);
182 ieee80211_bar_timeout = msecs_to_ticks(250);
184 * Register action frame handlers.
186 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_BA,
187 IEEE80211_ACTION_BA_ADDBA_REQUEST, ht_recv_action_ba_addba_request);
188 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_BA,
189 IEEE80211_ACTION_BA_ADDBA_RESPONSE, ht_recv_action_ba_addba_response);
190 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_BA,
191 IEEE80211_ACTION_BA_DELBA, ht_recv_action_ba_delba);
192 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_HT,
193 IEEE80211_ACTION_HT_MIMOPWRSAVE, ht_recv_action_ht_mimopwrsave);
194 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_HT,
195 IEEE80211_ACTION_HT_TXCHWIDTH, ht_recv_action_ht_txchwidth);
197 ieee80211_send_action_register(IEEE80211_ACTION_CAT_BA,
198 IEEE80211_ACTION_BA_ADDBA_REQUEST, ht_send_action_ba_addba);
199 ieee80211_send_action_register(IEEE80211_ACTION_CAT_BA,
200 IEEE80211_ACTION_BA_ADDBA_RESPONSE, ht_send_action_ba_addba);
201 ieee80211_send_action_register(IEEE80211_ACTION_CAT_BA,
202 IEEE80211_ACTION_BA_DELBA, ht_send_action_ba_delba);
203 ieee80211_send_action_register(IEEE80211_ACTION_CAT_HT,
204 IEEE80211_ACTION_HT_TXCHWIDTH, ht_send_action_ht_txchwidth);
206 SYSINIT(wlan_ht, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_ht_init, NULL);
208 static int ieee80211_ampdu_enable(struct ieee80211_node *ni,
209 struct ieee80211_tx_ampdu *tap);
210 static int ieee80211_addba_request(struct ieee80211_node *ni,
211 struct ieee80211_tx_ampdu *tap,
212 int dialogtoken, int baparamset, int batimeout);
213 static int ieee80211_addba_response(struct ieee80211_node *ni,
214 struct ieee80211_tx_ampdu *tap,
215 int code, int baparamset, int batimeout);
216 static void ieee80211_addba_stop(struct ieee80211_node *ni,
217 struct ieee80211_tx_ampdu *tap);
218 static void null_addba_response_timeout(struct ieee80211_node *ni,
219 struct ieee80211_tx_ampdu *tap);
221 static void ieee80211_bar_response(struct ieee80211_node *ni,
222 struct ieee80211_tx_ampdu *tap, int status);
223 static void ampdu_tx_stop(struct ieee80211_tx_ampdu *tap);
224 static void bar_stop_timer(struct ieee80211_tx_ampdu *tap);
225 static int ampdu_rx_start(struct ieee80211_node *, struct ieee80211_rx_ampdu *,
226 int baparamset, int batimeout, int baseqctl);
227 static void ampdu_rx_stop(struct ieee80211_node *, struct ieee80211_rx_ampdu *);
230 ieee80211_ht_attach(struct ieee80211com *ic)
232 /* setup default aggregation policy */
233 ic->ic_recv_action = ieee80211_recv_action;
234 ic->ic_send_action = ieee80211_send_action;
235 ic->ic_ampdu_enable = ieee80211_ampdu_enable;
236 ic->ic_addba_request = ieee80211_addba_request;
237 ic->ic_addba_response = ieee80211_addba_response;
238 ic->ic_addba_response_timeout = null_addba_response_timeout;
239 ic->ic_addba_stop = ieee80211_addba_stop;
240 ic->ic_bar_response = ieee80211_bar_response;
241 ic->ic_ampdu_rx_start = ampdu_rx_start;
242 ic->ic_ampdu_rx_stop = ampdu_rx_stop;
244 ic->ic_htprotmode = IEEE80211_PROT_RTSCTS;
245 ic->ic_curhtprotmode = IEEE80211_HTINFO_OPMODE_PURE;
249 ieee80211_ht_detach(struct ieee80211com *ic)
254 ieee80211_ht_vattach(struct ieee80211vap *vap)
257 /* driver can override defaults */
258 vap->iv_ampdu_rxmax = IEEE80211_HTCAP_MAXRXAMPDU_8K;
259 vap->iv_ampdu_density = IEEE80211_HTCAP_MPDUDENSITY_NA;
260 vap->iv_ampdu_limit = vap->iv_ampdu_rxmax;
261 vap->iv_amsdu_limit = vap->iv_htcaps & IEEE80211_HTCAP_MAXAMSDU;
262 /* tx aggregation traffic thresholds */
263 vap->iv_ampdu_mintraffic[WME_AC_BK] = 128;
264 vap->iv_ampdu_mintraffic[WME_AC_BE] = 64;
265 vap->iv_ampdu_mintraffic[WME_AC_VO] = 32;
266 vap->iv_ampdu_mintraffic[WME_AC_VI] = 32;
268 if (vap->iv_htcaps & IEEE80211_HTC_HT) {
270 * Device is HT capable; enable all HT-related
271 * facilities by default.
272 * XXX these choices may be too aggressive.
274 vap->iv_flags_ht |= IEEE80211_FHT_HT
275 | IEEE80211_FHT_HTCOMPAT
277 if (vap->iv_htcaps & IEEE80211_HTCAP_SHORTGI20)
278 vap->iv_flags_ht |= IEEE80211_FHT_SHORTGI20;
279 /* XXX infer from channel list? */
280 if (vap->iv_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
281 vap->iv_flags_ht |= IEEE80211_FHT_USEHT40;
282 if (vap->iv_htcaps & IEEE80211_HTCAP_SHORTGI40)
283 vap->iv_flags_ht |= IEEE80211_FHT_SHORTGI40;
285 /* enable RIFS if capable */
286 if (vap->iv_htcaps & IEEE80211_HTC_RIFS)
287 vap->iv_flags_ht |= IEEE80211_FHT_RIFS;
289 /* NB: A-MPDU and A-MSDU rx are mandated, these are tx only */
290 vap->iv_flags_ht |= IEEE80211_FHT_AMPDU_RX;
291 if (vap->iv_htcaps & IEEE80211_HTC_AMPDU)
292 vap->iv_flags_ht |= IEEE80211_FHT_AMPDU_TX;
293 vap->iv_flags_ht |= IEEE80211_FHT_AMSDU_RX;
294 if (vap->iv_htcaps & IEEE80211_HTC_AMSDU)
295 vap->iv_flags_ht |= IEEE80211_FHT_AMSDU_TX;
297 if (vap->iv_htcaps & IEEE80211_HTCAP_TXSTBC)
298 vap->iv_flags_ht |= IEEE80211_FHT_STBC_TX;
299 if (vap->iv_htcaps & IEEE80211_HTCAP_RXSTBC)
300 vap->iv_flags_ht |= IEEE80211_FHT_STBC_RX;
302 /* NB: disable default legacy WDS, too many issues right now */
303 if (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY)
304 vap->iv_flags_ht &= ~IEEE80211_FHT_HT;
308 ieee80211_ht_vdetach(struct ieee80211vap *vap)
313 ht_getrate(struct ieee80211com *ic, int index, enum ieee80211_phymode mode,
318 mword = ieee80211_rate2media(ic, index | IEEE80211_RATE_MCS, mode);
319 if (IFM_SUBTYPE(mword) != IFM_IEEE80211_MCS)
323 rate = ieee80211_htrates[index].ht20_rate_800ns;
326 rate = ieee80211_htrates[index].ht20_rate_400ns;
329 rate = ieee80211_htrates[index].ht40_rate_800ns;
332 rate = ieee80211_htrates[index].ht40_rate_400ns;
338 static struct printranges {
349 { 32, 0, 1, 2, IEEE80211_HTC_TXMCS32 },
350 { 33, 38, 2, 0, IEEE80211_HTC_TXUNEQUAL },
351 { 39, 52, 3, 0, IEEE80211_HTC_TXUNEQUAL },
352 { 53, 76, 4, 0, IEEE80211_HTC_TXUNEQUAL },
357 ht_rateprint(struct ieee80211com *ic, enum ieee80211_phymode mode, int ratetype)
359 int minrate, maxrate;
360 struct printranges *range;
362 for (range = ranges; range->txstream != 0; range++) {
363 if (ic->ic_txstream < range->txstream)
365 if (range->htcapflags &&
366 (ic->ic_htcaps & range->htcapflags) == 0)
368 if (ratetype < range->ratetype)
370 minrate = ht_getrate(ic, range->minmcs, mode, ratetype);
371 maxrate = ht_getrate(ic, range->maxmcs, mode, ratetype);
373 ic_printf(ic, "MCS %d-%d: %d%sMbps - %d%sMbps\n",
374 range->minmcs, range->maxmcs,
375 minrate/2, ((minrate & 0x1) != 0 ? ".5" : ""),
376 maxrate/2, ((maxrate & 0x1) != 0 ? ".5" : ""));
378 ic_printf(ic, "MCS %d: %d%sMbps\n", range->minmcs,
379 minrate/2, ((minrate & 0x1) != 0 ? ".5" : ""));
385 ht_announce(struct ieee80211com *ic, enum ieee80211_phymode mode)
387 const char *modestr = ieee80211_phymode_name[mode];
389 ic_printf(ic, "%s MCS 20MHz\n", modestr);
390 ht_rateprint(ic, mode, 0);
391 if (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20) {
392 ic_printf(ic, "%s MCS 20MHz SGI\n", modestr);
393 ht_rateprint(ic, mode, 1);
395 if (ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
396 ic_printf(ic, "%s MCS 40MHz:\n", modestr);
397 ht_rateprint(ic, mode, 2);
399 if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
400 (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40)) {
401 ic_printf(ic, "%s MCS 40MHz SGI:\n", modestr);
402 ht_rateprint(ic, mode, 3);
407 ieee80211_ht_announce(struct ieee80211com *ic)
410 if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) ||
411 isset(ic->ic_modecaps, IEEE80211_MODE_11NG))
412 ic_printf(ic, "%dT%dR\n", ic->ic_txstream, ic->ic_rxstream);
413 if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA))
414 ht_announce(ic, IEEE80211_MODE_11NA);
415 if (isset(ic->ic_modecaps, IEEE80211_MODE_11NG))
416 ht_announce(ic, IEEE80211_MODE_11NG);
419 static struct ieee80211_htrateset htrateset;
421 const struct ieee80211_htrateset *
422 ieee80211_get_suphtrates(struct ieee80211com *ic,
423 const struct ieee80211_channel *c)
425 #define ADDRATE(x) do { \
426 htrateset.rs_rates[htrateset.rs_nrates] = x; \
427 htrateset.rs_nrates++; \
431 memset(&htrateset, 0, sizeof(struct ieee80211_htrateset));
432 for (i = 0; i < ic->ic_txstream * 8; i++)
434 if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
435 (ic->ic_htcaps & IEEE80211_HTC_TXMCS32))
437 if (ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL) {
438 if (ic->ic_txstream >= 2) {
439 for (i = 33; i <= 38; i++)
442 if (ic->ic_txstream >= 3) {
443 for (i = 39; i <= 52; i++)
446 if (ic->ic_txstream == 4) {
447 for (i = 53; i <= 76; i++)
456 * Receive processing.
460 * Decap the encapsulated A-MSDU frames and dispatch all but
461 * the last for delivery. The last frame is returned for
462 * delivery via the normal path.
465 ieee80211_decap_amsdu(struct ieee80211_node *ni, struct mbuf *m)
467 struct ieee80211vap *vap = ni->ni_vap;
471 /* discard 802.3 header inserted by ieee80211_decap */
472 m_adj(m, sizeof(struct ether_header));
474 vap->iv_stats.is_amsdu_decap++;
478 * Decap the first frame, bust it apart from the
479 * remainder and deliver. We leave the last frame
480 * delivery to the caller (for consistency with other
481 * code paths, could also do it here).
483 m = ieee80211_decap1(m, &framelen);
485 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
486 ni->ni_macaddr, "a-msdu", "%s", "decap failed");
487 vap->iv_stats.is_amsdu_tooshort++;
490 if (m->m_pkthdr.len == framelen)
492 n = m_split(m, framelen, M_NOWAIT);
494 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
495 ni->ni_macaddr, "a-msdu",
496 "%s", "unable to split encapsulated frames");
497 vap->iv_stats.is_amsdu_split++;
498 m_freem(m); /* NB: must reclaim */
501 vap->iv_deliver_data(vap, ni, m);
504 * Remove frame contents; each intermediate frame
505 * is required to be aligned to a 4-byte boundary.
508 m_adj(m, roundup2(framelen, 4) - framelen); /* padding */
510 return m; /* last delivered by caller */
514 * Purge all frames in the A-MPDU re-order queue.
517 ampdu_rx_purge(struct ieee80211_rx_ampdu *rap)
522 for (i = 0; i < rap->rxa_wnd; i++) {
525 rap->rxa_m[i] = NULL;
526 rap->rxa_qbytes -= m->m_pkthdr.len;
528 if (--rap->rxa_qframes == 0)
532 KASSERT(rap->rxa_qbytes == 0 && rap->rxa_qframes == 0,
533 ("lost %u data, %u frames on ampdu rx q",
534 rap->rxa_qbytes, rap->rxa_qframes));
538 * Start A-MPDU rx/re-order processing for the specified TID.
541 ampdu_rx_start(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap,
542 int baparamset, int batimeout, int baseqctl)
544 int bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
546 if (rap->rxa_flags & IEEE80211_AGGR_RUNNING) {
548 * AMPDU previously setup and not terminated with a DELBA,
549 * flush the reorder q's in case anything remains.
553 memset(rap, 0, sizeof(*rap));
554 rap->rxa_wnd = (bufsiz == 0) ?
555 IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
556 rap->rxa_start = MS(baseqctl, IEEE80211_BASEQ_START);
557 rap->rxa_flags |= IEEE80211_AGGR_RUNNING | IEEE80211_AGGR_XCHGPEND;
563 * Public function; manually setup the RX ampdu state.
566 ieee80211_ampdu_rx_start_ext(struct ieee80211_node *ni, int tid, int seq, int baw)
568 struct ieee80211_rx_ampdu *rap;
570 /* XXX TODO: sanity check tid, seq, baw */
572 rap = &ni->ni_rx_ampdu[tid];
574 if (rap->rxa_flags & IEEE80211_AGGR_RUNNING) {
576 * AMPDU previously setup and not terminated with a DELBA,
577 * flush the reorder q's in case anything remains.
582 memset(rap, 0, sizeof(*rap));
583 rap->rxa_wnd = (baw== 0) ?
584 IEEE80211_AGGR_BAWMAX : min(baw, IEEE80211_AGGR_BAWMAX);
585 rap->rxa_start = seq;
586 rap->rxa_flags |= IEEE80211_AGGR_RUNNING | IEEE80211_AGGR_XCHGPEND;
588 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, ni,
589 "%s: tid=%d, start=%d, wnd=%d, flags=0x%08x\n",
600 * Stop A-MPDU rx processing for the specified TID.
603 ampdu_rx_stop(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
607 rap->rxa_flags &= ~(IEEE80211_AGGR_RUNNING | IEEE80211_AGGR_XCHGPEND);
611 * Dispatch a frame from the A-MPDU reorder queue. The
612 * frame is fed back into ieee80211_input marked with an
613 * M_AMPDU_MPDU flag so it doesn't come back to us (it also
614 * permits ieee80211_input to optimize re-processing).
617 ampdu_dispatch(struct ieee80211_node *ni, struct mbuf *m)
619 m->m_flags |= M_AMPDU_MPDU; /* bypass normal processing */
620 /* NB: rssi and noise are ignored w/ M_AMPDU_MPDU set */
621 (void) ieee80211_input(ni, m, 0, 0);
625 * Dispatch as many frames as possible from the re-order queue.
626 * Frames will always be "at the front"; we process all frames
627 * up to the first empty slot in the window. On completion we
628 * cleanup state if there are still pending frames in the current
629 * BA window. We assume the frame at slot 0 is already handled
630 * by the caller; we always start at slot 1.
633 ampdu_rx_dispatch(struct ieee80211_rx_ampdu *rap, struct ieee80211_node *ni)
635 struct ieee80211vap *vap = ni->ni_vap;
639 /* flush run of frames */
640 for (i = 1; i < rap->rxa_wnd; i++) {
644 rap->rxa_m[i] = NULL;
645 rap->rxa_qbytes -= m->m_pkthdr.len;
648 ampdu_dispatch(ni, m);
651 * If frames remain, copy the mbuf pointers down so
652 * they correspond to the offsets in the new window.
654 if (rap->rxa_qframes != 0) {
655 int n = rap->rxa_qframes, j;
656 for (j = i+1; j < rap->rxa_wnd; j++) {
657 if (rap->rxa_m[j] != NULL) {
658 rap->rxa_m[j-i] = rap->rxa_m[j];
659 rap->rxa_m[j] = NULL;
664 KASSERT(n == 0, ("lost %d frames", n));
665 vap->iv_stats.is_ampdu_rx_copy += rap->rxa_qframes;
668 * Adjust the start of the BA window to
669 * reflect the frames just dispatched.
671 rap->rxa_start = IEEE80211_SEQ_ADD(rap->rxa_start, i);
672 vap->iv_stats.is_ampdu_rx_oor += i;
676 * Dispatch all frames in the A-MPDU re-order queue.
679 ampdu_rx_flush(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
681 struct ieee80211vap *vap = ni->ni_vap;
685 for (i = 0; i < rap->rxa_wnd; i++) {
689 rap->rxa_m[i] = NULL;
690 rap->rxa_qbytes -= m->m_pkthdr.len;
692 vap->iv_stats.is_ampdu_rx_oor++;
694 ampdu_dispatch(ni, m);
695 if (rap->rxa_qframes == 0)
701 * Dispatch all frames in the A-MPDU re-order queue
702 * preceding the specified sequence number. This logic
703 * handles window moves due to a received MSDU or BAR.
706 ampdu_rx_flush_upto(struct ieee80211_node *ni,
707 struct ieee80211_rx_ampdu *rap, ieee80211_seq winstart)
709 struct ieee80211vap *vap = ni->ni_vap;
715 * Flush any complete MSDU's with a sequence number lower
716 * than winstart. Gaps may exist. Note that we may actually
717 * dispatch frames past winstart if a run continues; this is
718 * an optimization that avoids having to do a separate pass
719 * to dispatch frames after moving the BA window start.
721 seqno = rap->rxa_start;
722 for (i = 0; i < rap->rxa_wnd; i++) {
725 rap->rxa_m[i] = NULL;
726 rap->rxa_qbytes -= m->m_pkthdr.len;
728 vap->iv_stats.is_ampdu_rx_oor++;
730 ampdu_dispatch(ni, m);
732 if (!IEEE80211_SEQ_BA_BEFORE(seqno, winstart))
735 seqno = IEEE80211_SEQ_INC(seqno);
738 * If frames remain, copy the mbuf pointers down so
739 * they correspond to the offsets in the new window.
741 if (rap->rxa_qframes != 0) {
742 int n = rap->rxa_qframes, j;
744 /* NB: this loop assumes i > 0 and/or rxa_m[0] is NULL */
745 KASSERT(rap->rxa_m[0] == NULL,
746 ("%s: BA window slot 0 occupied", __func__));
747 for (j = i+1; j < rap->rxa_wnd; j++) {
748 if (rap->rxa_m[j] != NULL) {
749 rap->rxa_m[j-i] = rap->rxa_m[j];
750 rap->rxa_m[j] = NULL;
755 KASSERT(n == 0, ("%s: lost %d frames, qframes %d off %d "
756 "BA win <%d:%d> winstart %d",
757 __func__, n, rap->rxa_qframes, i, rap->rxa_start,
758 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
760 vap->iv_stats.is_ampdu_rx_copy += rap->rxa_qframes;
763 * Move the start of the BA window; we use the
764 * sequence number of the last MSDU that was
765 * passed up the stack+1 or winstart if stopped on
766 * a gap in the reorder buffer.
768 rap->rxa_start = seqno;
772 * Process a received QoS data frame for an HT station. Handle
773 * A-MPDU reordering: if this frame is received out of order
774 * and falls within the BA window hold onto it. Otherwise if
775 * this frame completes a run, flush any pending frames. We
776 * return 1 if the frame is consumed. A 0 is returned if
777 * the frame should be processed normally by the caller.
780 ieee80211_ampdu_reorder(struct ieee80211_node *ni, struct mbuf *m)
782 #define IEEE80211_FC0_QOSDATA \
783 (IEEE80211_FC0_TYPE_DATA|IEEE80211_FC0_SUBTYPE_QOS|IEEE80211_FC0_VERSION_0)
784 #define PROCESS 0 /* caller should process frame */
785 #define CONSUMED 1 /* frame consumed, caller does nothing */
786 struct ieee80211vap *vap = ni->ni_vap;
787 struct ieee80211_qosframe *wh;
788 struct ieee80211_rx_ampdu *rap;
793 KASSERT((m->m_flags & (M_AMPDU | M_AMPDU_MPDU)) == M_AMPDU,
794 ("!a-mpdu or already re-ordered, flags 0x%x", m->m_flags));
795 KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT sta"));
797 /* NB: m_len known to be sufficient */
798 wh = mtod(m, struct ieee80211_qosframe *);
799 if (wh->i_fc[0] != IEEE80211_FC0_QOSDATA) {
801 * Not QoS data, shouldn't get here but just
802 * return it to the caller for processing.
806 tid = ieee80211_getqos(wh)[0];
807 tid &= IEEE80211_QOS_TID;
808 rap = &ni->ni_rx_ampdu[tid];
809 if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
811 * No ADDBA request yet, don't touch.
815 rxseq = le16toh(*(uint16_t *)wh->i_seq);
816 if ((rxseq & IEEE80211_SEQ_FRAG_MASK) != 0) {
818 * Fragments are not allowed; toss.
820 IEEE80211_DISCARD_MAC(vap,
821 IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
822 "A-MPDU", "fragment, rxseq 0x%x tid %u%s", rxseq, tid,
823 wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
824 vap->iv_stats.is_ampdu_rx_drop++;
825 IEEE80211_NODE_STAT(ni, rx_drop);
829 rxseq >>= IEEE80211_SEQ_SEQ_SHIFT;
832 if (rxseq == rap->rxa_start) {
834 * First frame in window.
836 if (rap->rxa_qframes != 0) {
838 * Dispatch as many packets as we can.
840 KASSERT(rap->rxa_m[0] == NULL, ("unexpected dup"));
841 ampdu_dispatch(ni, m);
842 ampdu_rx_dispatch(rap, ni);
846 * In order; advance window and notify
847 * caller to dispatch directly.
849 rap->rxa_start = IEEE80211_SEQ_INC(rxseq);
854 * Frame is out of order; store if in the BA window.
856 /* calculate offset in BA window */
857 off = IEEE80211_SEQ_SUB(rxseq, rap->rxa_start);
858 if (off < rap->rxa_wnd) {
860 * Common case (hopefully): in the BA window.
861 * Sec 9.10.7.6.2 a) (p.137)
865 * Check for frames sitting too long in the reorder queue.
866 * This should only ever happen if frames are not delivered
867 * without the sender otherwise notifying us (e.g. with a
868 * BAR to move the window). Typically this happens because
869 * of vendor bugs that cause the sequence number to jump.
870 * When this happens we get a gap in the reorder queue that
871 * leaves frame sitting on the queue until they get pushed
872 * out due to window moves. When the vendor does not send
873 * BAR this move only happens due to explicit packet sends
875 * NB: we only track the time of the oldest frame in the
876 * reorder q; this means that if we flush we might push
877 * frames that still "new"; if this happens then subsequent
878 * frames will result in BA window moves which cost something
879 * but is still better than a big throughput dip.
881 if (rap->rxa_qframes != 0) {
882 /* XXX honor batimeout? */
883 if (ticks - rap->rxa_age > ieee80211_ampdu_age) {
885 * Too long since we received the first
886 * frame; flush the reorder buffer.
888 if (rap->rxa_qframes != 0) {
889 vap->iv_stats.is_ampdu_rx_age +=
891 ampdu_rx_flush(ni, rap);
893 rap->rxa_start = IEEE80211_SEQ_INC(rxseq);
898 * First frame, start aging timer.
900 rap->rxa_age = ticks;
904 if (rap->rxa_m[off] == NULL) {
907 rap->rxa_qbytes += m->m_pkthdr.len;
908 vap->iv_stats.is_ampdu_rx_reorder++;
910 IEEE80211_DISCARD_MAC(vap,
911 IEEE80211_MSG_INPUT | IEEE80211_MSG_11N,
912 ni->ni_macaddr, "a-mpdu duplicate",
913 "seqno %u tid %u BA win <%u:%u>",
914 rxseq, tid, rap->rxa_start,
915 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1));
916 vap->iv_stats.is_rx_dup++;
917 IEEE80211_NODE_STAT(ni, rx_dup);
922 if (off < IEEE80211_SEQ_BA_RANGE) {
924 * Outside the BA window, but within range;
925 * flush the reorder q and move the window.
926 * Sec 9.10.7.6.2 b) (p.138)
928 IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
929 "move BA win <%u:%u> (%u frames) rxseq %u tid %u",
931 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
932 rap->rxa_qframes, rxseq, tid);
933 vap->iv_stats.is_ampdu_rx_move++;
936 * The spec says to flush frames up to but not including:
937 * WinStart_B = rxseq - rap->rxa_wnd + 1
938 * Then insert the frame or notify the caller to process
939 * it immediately. We can safely do this by just starting
940 * over again because we know the frame will now be within
943 /* NB: rxa_wnd known to be >0 */
944 ampdu_rx_flush_upto(ni, rap,
945 IEEE80211_SEQ_SUB(rxseq, rap->rxa_wnd-1));
949 * Outside the BA window and out of range; toss.
950 * Sec 9.10.7.6.2 c) (p.138)
952 IEEE80211_DISCARD_MAC(vap,
953 IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
954 "MPDU", "BA win <%u:%u> (%u frames) rxseq %u tid %u%s",
956 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
957 rap->rxa_qframes, rxseq, tid,
958 wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
959 vap->iv_stats.is_ampdu_rx_drop++;
960 IEEE80211_NODE_STAT(ni, rx_drop);
966 #undef IEEE80211_FC0_QOSDATA
970 * Process a BAR ctl frame. Dispatch all frames up to
971 * the sequence number of the frame. If this frame is
972 * out of range it's discarded.
975 ieee80211_recv_bar(struct ieee80211_node *ni, struct mbuf *m0)
977 struct ieee80211vap *vap = ni->ni_vap;
978 struct ieee80211_frame_bar *wh;
979 struct ieee80211_rx_ampdu *rap;
983 if (!ieee80211_recv_bar_ena) {
985 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_11N,
986 ni->ni_macaddr, "BAR", "%s", "processing disabled");
988 vap->iv_stats.is_ampdu_bar_bad++;
991 wh = mtod(m0, struct ieee80211_frame_bar *);
992 /* XXX check basic BAR */
993 tid = MS(le16toh(wh->i_ctl), IEEE80211_BAR_TID);
994 rap = &ni->ni_rx_ampdu[tid];
995 if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
997 * No ADDBA request yet, don't touch.
999 IEEE80211_DISCARD_MAC(vap,
1000 IEEE80211_MSG_INPUT | IEEE80211_MSG_11N,
1001 ni->ni_macaddr, "BAR", "no BA stream, tid %u", tid);
1002 vap->iv_stats.is_ampdu_bar_bad++;
1005 vap->iv_stats.is_ampdu_bar_rx++;
1006 rxseq = le16toh(wh->i_seq) >> IEEE80211_SEQ_SEQ_SHIFT;
1007 if (rxseq == rap->rxa_start)
1009 /* calculate offset in BA window */
1010 off = IEEE80211_SEQ_SUB(rxseq, rap->rxa_start);
1011 if (off < IEEE80211_SEQ_BA_RANGE) {
1013 * Flush the reorder q up to rxseq and move the window.
1014 * Sec 9.10.7.6.3 a) (p.138)
1016 IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
1017 "BAR moves BA win <%u:%u> (%u frames) rxseq %u tid %u",
1019 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
1020 rap->rxa_qframes, rxseq, tid);
1021 vap->iv_stats.is_ampdu_bar_move++;
1023 ampdu_rx_flush_upto(ni, rap, rxseq);
1024 if (off >= rap->rxa_wnd) {
1026 * BAR specifies a window start to the right of BA
1027 * window; we must move it explicitly since
1028 * ampdu_rx_flush_upto will not.
1030 rap->rxa_start = rxseq;
1034 * Out of range; toss.
1035 * Sec 9.10.7.6.3 b) (p.138)
1037 IEEE80211_DISCARD_MAC(vap,
1038 IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
1039 "BAR", "BA win <%u:%u> (%u frames) rxseq %u tid %u%s",
1041 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
1042 rap->rxa_qframes, rxseq, tid,
1043 wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
1044 vap->iv_stats.is_ampdu_bar_oow++;
1045 IEEE80211_NODE_STAT(ni, rx_drop);
1050 * Setup HT-specific state in a node. Called only
1051 * when HT use is negotiated so we don't do extra
1052 * work for temporary and/or legacy sta's.
1055 ieee80211_ht_node_init(struct ieee80211_node *ni)
1057 struct ieee80211_tx_ampdu *tap;
1060 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
1066 if (ni->ni_flags & IEEE80211_NODE_HT) {
1068 * Clean AMPDU state on re-associate. This handles the case
1069 * where a station leaves w/o notifying us and then returns
1070 * before node is reaped for inactivity.
1072 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
1074 "%s: calling cleanup (%p)",
1076 ieee80211_ht_node_cleanup(ni);
1078 for (tid = 0; tid < WME_NUM_TID; tid++) {
1079 tap = &ni->ni_tx_ampdu[tid];
1082 ieee80211_txampdu_init_pps(tap);
1083 /* NB: further initialization deferred */
1085 ni->ni_flags |= IEEE80211_NODE_HT | IEEE80211_NODE_AMPDU;
1089 * Cleanup HT-specific state in a node. Called only
1090 * when HT use has been marked.
1093 ieee80211_ht_node_cleanup(struct ieee80211_node *ni)
1095 struct ieee80211com *ic = ni->ni_ic;
1098 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
1103 KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT node"));
1105 /* XXX optimize this */
1106 for (i = 0; i < WME_NUM_TID; i++) {
1107 struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[i];
1108 if (tap->txa_flags & IEEE80211_AGGR_SETUP)
1111 for (i = 0; i < WME_NUM_TID; i++)
1112 ic->ic_ampdu_rx_stop(ni, &ni->ni_rx_ampdu[i]);
1115 ni->ni_flags &= ~IEEE80211_NODE_HT_ALL;
1119 * Age out HT resources for a station.
1122 ieee80211_ht_node_age(struct ieee80211_node *ni)
1124 struct ieee80211vap *vap = ni->ni_vap;
1127 KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT sta"));
1129 for (tid = 0; tid < WME_NUM_TID; tid++) {
1130 struct ieee80211_rx_ampdu *rap;
1132 rap = &ni->ni_rx_ampdu[tid];
1133 if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0)
1135 if (rap->rxa_qframes == 0)
1138 * Check for frames sitting too long in the reorder queue.
1139 * See above for more details on what's happening here.
1141 /* XXX honor batimeout? */
1142 if (ticks - rap->rxa_age > ieee80211_ampdu_age) {
1144 * Too long since we received the first
1145 * frame; flush the reorder buffer.
1147 vap->iv_stats.is_ampdu_rx_age += rap->rxa_qframes;
1148 ampdu_rx_flush(ni, rap);
1153 static struct ieee80211_channel *
1154 findhtchan(struct ieee80211com *ic, struct ieee80211_channel *c, int htflags)
1156 return ieee80211_find_channel(ic, c->ic_freq,
1157 (c->ic_flags &~ IEEE80211_CHAN_HT) | htflags);
1161 * Adjust a channel to be HT/non-HT according to the vap's configuration.
1163 struct ieee80211_channel *
1164 ieee80211_ht_adjust_channel(struct ieee80211com *ic,
1165 struct ieee80211_channel *chan, int flags)
1167 struct ieee80211_channel *c;
1169 if (flags & IEEE80211_FHT_HT) {
1170 /* promote to HT if possible */
1171 if (flags & IEEE80211_FHT_USEHT40) {
1172 if (!IEEE80211_IS_CHAN_HT40(chan)) {
1173 /* NB: arbitrarily pick ht40+ over ht40- */
1174 c = findhtchan(ic, chan, IEEE80211_CHAN_HT40U);
1176 c = findhtchan(ic, chan,
1177 IEEE80211_CHAN_HT40D);
1179 c = findhtchan(ic, chan,
1180 IEEE80211_CHAN_HT20);
1184 } else if (!IEEE80211_IS_CHAN_HT20(chan)) {
1185 c = findhtchan(ic, chan, IEEE80211_CHAN_HT20);
1189 } else if (IEEE80211_IS_CHAN_HT(chan)) {
1190 /* demote to legacy, HT use is disabled */
1191 c = ieee80211_find_channel(ic, chan->ic_freq,
1192 chan->ic_flags &~ IEEE80211_CHAN_HT);
1200 * Setup HT-specific state for a legacy WDS peer.
1203 ieee80211_ht_wds_init(struct ieee80211_node *ni)
1205 struct ieee80211vap *vap = ni->ni_vap;
1206 struct ieee80211_tx_ampdu *tap;
1209 KASSERT(vap->iv_flags_ht & IEEE80211_FHT_HT, ("no HT requested"));
1211 /* XXX check scan cache in case peer has an ap and we have info */
1213 * If setup with a legacy channel; locate an HT channel.
1214 * Otherwise if the inherited channel (from a companion
1215 * AP) is suitable use it so we use the same location
1216 * for the extension channel).
1218 ni->ni_chan = ieee80211_ht_adjust_channel(ni->ni_ic,
1219 ni->ni_chan, ieee80211_htchanflags(ni->ni_chan));
1222 if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20)
1223 ni->ni_htcap |= IEEE80211_HTCAP_SHORTGI20;
1224 if (IEEE80211_IS_CHAN_HT40(ni->ni_chan)) {
1225 ni->ni_htcap |= IEEE80211_HTCAP_CHWIDTH40;
1227 if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan))
1228 ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_ABOVE;
1229 else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan))
1230 ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_BELOW;
1231 if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40)
1232 ni->ni_htcap |= IEEE80211_HTCAP_SHORTGI40;
1235 ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_NONE;
1237 ni->ni_htctlchan = ni->ni_chan->ic_ieee;
1238 if (vap->iv_flags_ht & IEEE80211_FHT_RIFS)
1239 ni->ni_flags |= IEEE80211_NODE_RIFS;
1240 /* XXX does it make sense to enable SMPS? */
1242 ni->ni_htopmode = 0; /* XXX need protection state */
1243 ni->ni_htstbc = 0; /* XXX need info */
1245 for (tid = 0; tid < WME_NUM_TID; tid++) {
1246 tap = &ni->ni_tx_ampdu[tid];
1248 ieee80211_txampdu_init_pps(tap);
1250 /* NB: AMPDU tx/rx governed by IEEE80211_FHT_AMPDU_{TX,RX} */
1251 ni->ni_flags |= IEEE80211_NODE_HT | IEEE80211_NODE_AMPDU;
1255 * Notify hostap vaps of a change in the HTINFO ie.
1258 htinfo_notify(struct ieee80211com *ic)
1260 struct ieee80211vap *vap;
1263 IEEE80211_LOCK_ASSERT(ic);
1265 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1266 if (vap->iv_opmode != IEEE80211_M_HOSTAP)
1268 if (vap->iv_state != IEEE80211_S_RUN ||
1269 !IEEE80211_IS_CHAN_HT(vap->iv_bss->ni_chan))
1273 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N,
1275 "HT bss occupancy change: %d sta, %d ht, "
1276 "%d ht40%s, HT protmode now 0x%x"
1278 , ic->ic_ht_sta_assoc
1279 , ic->ic_ht40_sta_assoc
1280 , (ic->ic_flags_ht & IEEE80211_FHT_NONHT_PR) ?
1281 ", non-HT sta present" : ""
1282 , ic->ic_curhtprotmode);
1285 ieee80211_beacon_notify(vap, IEEE80211_BEACON_HTINFO);
1290 * Calculate HT protection mode from current
1291 * state and handle updates.
1294 htinfo_update(struct ieee80211com *ic)
1298 if (ic->ic_sta_assoc != ic->ic_ht_sta_assoc) {
1299 protmode = IEEE80211_HTINFO_OPMODE_MIXED
1300 | IEEE80211_HTINFO_NONHT_PRESENT;
1301 } else if (ic->ic_flags_ht & IEEE80211_FHT_NONHT_PR) {
1302 protmode = IEEE80211_HTINFO_OPMODE_PROTOPT
1303 | IEEE80211_HTINFO_NONHT_PRESENT;
1304 } else if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
1305 IEEE80211_IS_CHAN_HT40(ic->ic_bsschan) &&
1306 ic->ic_sta_assoc != ic->ic_ht40_sta_assoc) {
1307 protmode = IEEE80211_HTINFO_OPMODE_HT20PR;
1309 protmode = IEEE80211_HTINFO_OPMODE_PURE;
1311 if (protmode != ic->ic_curhtprotmode) {
1312 ic->ic_curhtprotmode = protmode;
1318 * Handle an HT station joining a BSS.
1321 ieee80211_ht_node_join(struct ieee80211_node *ni)
1323 struct ieee80211com *ic = ni->ni_ic;
1325 IEEE80211_LOCK_ASSERT(ic);
1327 if (ni->ni_flags & IEEE80211_NODE_HT) {
1328 ic->ic_ht_sta_assoc++;
1329 if (ni->ni_chw == 40)
1330 ic->ic_ht40_sta_assoc++;
1336 * Handle an HT station leaving a BSS.
1339 ieee80211_ht_node_leave(struct ieee80211_node *ni)
1341 struct ieee80211com *ic = ni->ni_ic;
1343 IEEE80211_LOCK_ASSERT(ic);
1345 if (ni->ni_flags & IEEE80211_NODE_HT) {
1346 ic->ic_ht_sta_assoc--;
1347 if (ni->ni_chw == 40)
1348 ic->ic_ht40_sta_assoc--;
1354 * Public version of htinfo_update; used for processing
1355 * beacon frames from overlapping bss.
1357 * Caller can specify either IEEE80211_HTINFO_OPMODE_MIXED
1358 * (on receipt of a beacon that advertises MIXED) or
1359 * IEEE80211_HTINFO_OPMODE_PROTOPT (on receipt of a beacon
1360 * from an overlapping legacy bss). We treat MIXED with
1361 * a higher precedence than PROTOPT (i.e. we will not change
1362 * change PROTOPT -> MIXED; only MIXED -> PROTOPT). This
1363 * corresponds to how we handle things in htinfo_update.
1366 ieee80211_htprot_update(struct ieee80211com *ic, int protmode)
1368 #define OPMODE(x) SM(x, IEEE80211_HTINFO_OPMODE)
1371 /* track non-HT station presence */
1372 KASSERT(protmode & IEEE80211_HTINFO_NONHT_PRESENT,
1373 ("protmode 0x%x", protmode));
1374 ic->ic_flags_ht |= IEEE80211_FHT_NONHT_PR;
1375 ic->ic_lastnonht = ticks;
1377 if (protmode != ic->ic_curhtprotmode &&
1378 (OPMODE(ic->ic_curhtprotmode) != IEEE80211_HTINFO_OPMODE_MIXED ||
1379 OPMODE(protmode) == IEEE80211_HTINFO_OPMODE_PROTOPT)) {
1380 /* push beacon update */
1381 ic->ic_curhtprotmode = protmode;
1384 IEEE80211_UNLOCK(ic);
1389 * Time out presence of an overlapping bss with non-HT
1390 * stations. When operating in hostap mode we listen for
1391 * beacons from other stations and if we identify a non-HT
1392 * station is present we update the opmode field of the
1393 * HTINFO ie. To identify when all non-HT stations are
1394 * gone we time out this condition.
1397 ieee80211_ht_timeout(struct ieee80211com *ic)
1399 IEEE80211_LOCK_ASSERT(ic);
1401 if ((ic->ic_flags_ht & IEEE80211_FHT_NONHT_PR) &&
1402 ieee80211_time_after(ticks, ic->ic_lastnonht + IEEE80211_NONHT_PRESENT_AGE)) {
1404 IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
1405 "%s", "time out non-HT STA present on channel");
1407 ic->ic_flags_ht &= ~IEEE80211_FHT_NONHT_PR;
1413 * Process an 802.11n HT capabilities ie.
1416 ieee80211_parse_htcap(struct ieee80211_node *ni, const uint8_t *ie)
1418 if (ie[0] == IEEE80211_ELEMID_VENDOR) {
1420 * Station used Vendor OUI ie to associate;
1421 * mark the node so when we respond we'll use
1422 * the Vendor OUI's and not the standard ie's.
1424 ni->ni_flags |= IEEE80211_NODE_HTCOMPAT;
1427 ni->ni_flags &= ~IEEE80211_NODE_HTCOMPAT;
1429 ni->ni_htcap = le16dec(ie +
1430 __offsetof(struct ieee80211_ie_htcap, hc_cap));
1431 ni->ni_htparam = ie[__offsetof(struct ieee80211_ie_htcap, hc_param)];
1435 htinfo_parse(struct ieee80211_node *ni,
1436 const struct ieee80211_ie_htinfo *htinfo)
1440 ni->ni_htctlchan = htinfo->hi_ctrlchannel;
1441 ni->ni_ht2ndchan = SM(htinfo->hi_byte1, IEEE80211_HTINFO_2NDCHAN);
1442 w = le16dec(&htinfo->hi_byte2);
1443 ni->ni_htopmode = SM(w, IEEE80211_HTINFO_OPMODE);
1444 w = le16dec(&htinfo->hi_byte45);
1445 ni->ni_htstbc = SM(w, IEEE80211_HTINFO_BASIC_STBCMCS);
1449 * Parse an 802.11n HT info ie and save useful information
1450 * to the node state. Note this does not effect any state
1451 * changes such as for channel width change.
1454 ieee80211_parse_htinfo(struct ieee80211_node *ni, const uint8_t *ie)
1456 if (ie[0] == IEEE80211_ELEMID_VENDOR)
1458 htinfo_parse(ni, (const struct ieee80211_ie_htinfo *) ie);
1462 * Handle 11n channel switch. Use the received HT ie's to
1463 * identify the right channel to use. If we cannot locate it
1464 * in the channel table then fallback to legacy operation.
1465 * Note that we use this information to identify the node's
1466 * channel only; the caller is responsible for insuring any
1467 * required channel change is done (e.g. in sta mode when
1468 * parsing the contents of a beacon frame).
1471 htinfo_update_chw(struct ieee80211_node *ni, int htflags)
1473 struct ieee80211com *ic = ni->ni_ic;
1474 struct ieee80211_channel *c;
1478 chanflags = (ni->ni_chan->ic_flags &~ IEEE80211_CHAN_HT) | htflags;
1479 if (chanflags != ni->ni_chan->ic_flags) {
1480 /* XXX not right for ht40- */
1481 c = ieee80211_find_channel(ic, ni->ni_chan->ic_freq, chanflags);
1482 if (c == NULL && (htflags & IEEE80211_CHAN_HT40)) {
1484 * No HT40 channel entry in our table; fall back
1485 * to HT20 operation. This should not happen.
1487 c = findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT20);
1489 IEEE80211_NOTE(ni->ni_vap,
1490 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
1491 "no HT40 channel (freq %u), falling back to HT20",
1492 ni->ni_chan->ic_freq);
1496 if (c != NULL && c != ni->ni_chan) {
1497 IEEE80211_NOTE(ni->ni_vap,
1498 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
1499 "switch station to HT%d channel %u/0x%x",
1500 IEEE80211_IS_CHAN_HT40(c) ? 40 : 20,
1501 c->ic_freq, c->ic_flags);
1505 /* NB: caller responsible for forcing any channel change */
1507 /* update node's tx channel width */
1508 ni->ni_chw = IEEE80211_IS_CHAN_HT40(ni->ni_chan)? 40 : 20;
1513 * Update 11n MIMO PS state according to received htcap.
1516 htcap_update_mimo_ps(struct ieee80211_node *ni)
1518 uint16_t oflags = ni->ni_flags;
1520 switch (ni->ni_htcap & IEEE80211_HTCAP_SMPS) {
1521 case IEEE80211_HTCAP_SMPS_DYNAMIC:
1522 ni->ni_flags |= IEEE80211_NODE_MIMO_PS;
1523 ni->ni_flags |= IEEE80211_NODE_MIMO_RTS;
1525 case IEEE80211_HTCAP_SMPS_ENA:
1526 ni->ni_flags |= IEEE80211_NODE_MIMO_PS;
1527 ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS;
1529 case IEEE80211_HTCAP_SMPS_OFF:
1530 default: /* disable on rx of reserved value */
1531 ni->ni_flags &= ~IEEE80211_NODE_MIMO_PS;
1532 ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS;
1535 return (oflags ^ ni->ni_flags);
1539 * Update short GI state according to received htcap
1540 * and local settings.
1542 static __inline void
1543 htcap_update_shortgi(struct ieee80211_node *ni)
1545 struct ieee80211vap *vap = ni->ni_vap;
1547 ni->ni_flags &= ~(IEEE80211_NODE_SGI20|IEEE80211_NODE_SGI40);
1548 if ((ni->ni_htcap & IEEE80211_HTCAP_SHORTGI20) &&
1549 (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20))
1550 ni->ni_flags |= IEEE80211_NODE_SGI20;
1551 if ((ni->ni_htcap & IEEE80211_HTCAP_SHORTGI40) &&
1552 (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40))
1553 ni->ni_flags |= IEEE80211_NODE_SGI40;
1557 * Parse and update HT-related state extracted from
1558 * the HT cap and info ie's.
1561 ieee80211_ht_updateparams(struct ieee80211_node *ni,
1562 const uint8_t *htcapie, const uint8_t *htinfoie)
1564 struct ieee80211vap *vap = ni->ni_vap;
1565 const struct ieee80211_ie_htinfo *htinfo;
1569 ieee80211_parse_htcap(ni, htcapie);
1570 if (vap->iv_htcaps & IEEE80211_HTC_SMPS)
1571 htcap_update_mimo_ps(ni);
1572 htcap_update_shortgi(ni);
1574 if (htinfoie[0] == IEEE80211_ELEMID_VENDOR)
1576 htinfo = (const struct ieee80211_ie_htinfo *) htinfoie;
1577 htinfo_parse(ni, htinfo);
1579 htflags = (vap->iv_flags_ht & IEEE80211_FHT_HT) ?
1580 IEEE80211_CHAN_HT20 : 0;
1581 /* NB: honor operating mode constraint */
1582 if ((htinfo->hi_byte1 & IEEE80211_HTINFO_TXWIDTH_2040) &&
1583 (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)) {
1584 if (ni->ni_ht2ndchan == IEEE80211_HTINFO_2NDCHAN_ABOVE)
1585 htflags = IEEE80211_CHAN_HT40U;
1586 else if (ni->ni_ht2ndchan == IEEE80211_HTINFO_2NDCHAN_BELOW)
1587 htflags = IEEE80211_CHAN_HT40D;
1589 if (htinfo_update_chw(ni, htflags))
1592 if ((htinfo->hi_byte1 & IEEE80211_HTINFO_RIFSMODE_PERM) &&
1593 (vap->iv_flags_ht & IEEE80211_FHT_RIFS))
1594 ni->ni_flags |= IEEE80211_NODE_RIFS;
1596 ni->ni_flags &= ~IEEE80211_NODE_RIFS;
1602 * Parse and update HT-related state extracted from the HT cap ie
1603 * for a station joining an HT BSS.
1606 ieee80211_ht_updatehtcap(struct ieee80211_node *ni, const uint8_t *htcapie)
1608 struct ieee80211vap *vap = ni->ni_vap;
1611 ieee80211_parse_htcap(ni, htcapie);
1612 if (vap->iv_htcaps & IEEE80211_HTC_SMPS)
1613 htcap_update_mimo_ps(ni);
1614 htcap_update_shortgi(ni);
1616 /* NB: honor operating mode constraint */
1617 /* XXX 40 MHz intolerant */
1618 htflags = (vap->iv_flags_ht & IEEE80211_FHT_HT) ?
1619 IEEE80211_CHAN_HT20 : 0;
1620 if ((ni->ni_htcap & IEEE80211_HTCAP_CHWIDTH40) &&
1621 (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)) {
1622 if (IEEE80211_IS_CHAN_HT40U(vap->iv_bss->ni_chan))
1623 htflags = IEEE80211_CHAN_HT40U;
1624 else if (IEEE80211_IS_CHAN_HT40D(vap->iv_bss->ni_chan))
1625 htflags = IEEE80211_CHAN_HT40D;
1627 (void) htinfo_update_chw(ni, htflags);
1631 * Install received HT rate set by parsing the HT cap ie.
1634 ieee80211_setup_htrates(struct ieee80211_node *ni, const uint8_t *ie, int flags)
1636 struct ieee80211com *ic = ni->ni_ic;
1637 struct ieee80211vap *vap = ni->ni_vap;
1638 const struct ieee80211_ie_htcap *htcap;
1639 struct ieee80211_htrateset *rs;
1640 int i, maxequalmcs, maxunequalmcs;
1642 maxequalmcs = ic->ic_txstream * 8 - 1;
1644 if (ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL) {
1645 if (ic->ic_txstream >= 2)
1647 if (ic->ic_txstream >= 3)
1649 if (ic->ic_txstream >= 4)
1653 rs = &ni->ni_htrates;
1654 memset(rs, 0, sizeof(*rs));
1656 if (ie[0] == IEEE80211_ELEMID_VENDOR)
1658 htcap = (const struct ieee80211_ie_htcap *) ie;
1659 for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++) {
1660 if (isclr(htcap->hc_mcsset, i))
1662 if (rs->rs_nrates == IEEE80211_HTRATE_MAXSIZE) {
1664 IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
1665 "WARNING, HT rate set too large; only "
1666 "using %u rates", IEEE80211_HTRATE_MAXSIZE);
1667 vap->iv_stats.is_rx_rstoobig++;
1670 if (i <= 31 && i > maxequalmcs)
1673 (ic->ic_htcaps & IEEE80211_HTC_TXMCS32) == 0)
1675 if (i > 32 && i > maxunequalmcs)
1677 rs->rs_rates[rs->rs_nrates++] = i;
1680 return ieee80211_fix_rate(ni, (struct ieee80211_rateset *) rs, flags);
1684 * Mark rates in a node's HT rate set as basic according
1685 * to the information in the supplied HT info ie.
1688 ieee80211_setup_basic_htrates(struct ieee80211_node *ni, const uint8_t *ie)
1690 const struct ieee80211_ie_htinfo *htinfo;
1691 struct ieee80211_htrateset *rs;
1694 if (ie[0] == IEEE80211_ELEMID_VENDOR)
1696 htinfo = (const struct ieee80211_ie_htinfo *) ie;
1697 rs = &ni->ni_htrates;
1698 if (rs->rs_nrates == 0) {
1699 IEEE80211_NOTE(ni->ni_vap,
1700 IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
1701 "%s", "WARNING, empty HT rate set");
1704 for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++) {
1705 if (isclr(htinfo->hi_basicmcsset, i))
1707 for (j = 0; j < rs->rs_nrates; j++)
1708 if ((rs->rs_rates[j] & IEEE80211_RATE_VAL) == i)
1709 rs->rs_rates[j] |= IEEE80211_RATE_BASIC;
1714 ampdu_tx_setup(struct ieee80211_tx_ampdu *tap)
1716 callout_init(&tap->txa_timer, 1);
1717 tap->txa_flags |= IEEE80211_AGGR_SETUP;
1718 tap->txa_lastsample = ticks;
1722 ampdu_tx_stop(struct ieee80211_tx_ampdu *tap)
1724 struct ieee80211_node *ni = tap->txa_ni;
1725 struct ieee80211com *ic = ni->ni_ic;
1727 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
1732 KASSERT(tap->txa_flags & IEEE80211_AGGR_SETUP,
1733 ("txa_flags 0x%x tid %d ac %d", tap->txa_flags, tap->txa_tid,
1734 TID_TO_WME_AC(tap->txa_tid)));
1737 * Stop BA stream if setup so driver has a chance
1738 * to reclaim any resources it might have allocated.
1740 ic->ic_addba_stop(ni, tap);
1742 * Stop any pending BAR transmit.
1744 bar_stop_timer(tap);
1747 * Reset packet estimate.
1749 ieee80211_txampdu_init_pps(tap);
1751 /* NB: clearing NAK means we may re-send ADDBA */
1752 tap->txa_flags &= ~(IEEE80211_AGGR_SETUP | IEEE80211_AGGR_NAK);
1756 * ADDBA response timeout.
1758 * If software aggregation and per-TID queue management was done here,
1759 * that queue would be unpaused after the ADDBA timeout occurs.
1762 addba_timeout(void *arg)
1764 struct ieee80211_tx_ampdu *tap = arg;
1765 struct ieee80211_node *ni = tap->txa_ni;
1766 struct ieee80211com *ic = ni->ni_ic;
1769 tap->txa_flags &= ~IEEE80211_AGGR_XCHGPEND;
1770 tap->txa_attempts++;
1771 ic->ic_addba_response_timeout(ni, tap);
1775 addba_start_timeout(struct ieee80211_tx_ampdu *tap)
1777 /* XXX use CALLOUT_PENDING instead? */
1778 callout_reset(&tap->txa_timer, ieee80211_addba_timeout,
1779 addba_timeout, tap);
1780 tap->txa_flags |= IEEE80211_AGGR_XCHGPEND;
1781 tap->txa_nextrequest = ticks + ieee80211_addba_timeout;
1785 addba_stop_timeout(struct ieee80211_tx_ampdu *tap)
1787 /* XXX use CALLOUT_PENDING instead? */
1788 if (tap->txa_flags & IEEE80211_AGGR_XCHGPEND) {
1789 callout_stop(&tap->txa_timer);
1790 tap->txa_flags &= ~IEEE80211_AGGR_XCHGPEND;
1795 null_addba_response_timeout(struct ieee80211_node *ni,
1796 struct ieee80211_tx_ampdu *tap)
1801 * Default method for requesting A-MPDU tx aggregation.
1802 * We setup the specified state block and start a timer
1803 * to wait for an ADDBA response frame.
1806 ieee80211_addba_request(struct ieee80211_node *ni,
1807 struct ieee80211_tx_ampdu *tap,
1808 int dialogtoken, int baparamset, int batimeout)
1813 tap->txa_token = dialogtoken;
1814 tap->txa_flags |= IEEE80211_AGGR_IMMEDIATE;
1815 bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
1816 tap->txa_wnd = (bufsiz == 0) ?
1817 IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
1818 addba_start_timeout(tap);
1823 * Called by drivers that wish to request an ADDBA session be
1824 * setup. This brings it up and starts the request timer.
1827 ieee80211_ampdu_tx_request_ext(struct ieee80211_node *ni, int tid)
1829 struct ieee80211_tx_ampdu *tap;
1831 if (tid < 0 || tid > 15)
1833 tap = &ni->ni_tx_ampdu[tid];
1836 if ((tap->txa_flags & IEEE80211_AGGR_SETUP) == 0) {
1837 /* do deferred setup of state */
1838 ampdu_tx_setup(tap);
1840 /* XXX hack for not doing proper locking */
1841 tap->txa_flags &= ~IEEE80211_AGGR_NAK;
1842 addba_start_timeout(tap);
1847 * Called by drivers that have marked a session as active.
1850 ieee80211_ampdu_tx_request_active_ext(struct ieee80211_node *ni, int tid,
1853 struct ieee80211_tx_ampdu *tap;
1855 if (tid < 0 || tid > 15)
1857 tap = &ni->ni_tx_ampdu[tid];
1860 addba_stop_timeout(tap);
1862 tap->txa_flags |= IEEE80211_AGGR_RUNNING;
1863 tap->txa_attempts = 0;
1865 /* mark tid so we don't try again */
1866 tap->txa_flags |= IEEE80211_AGGR_NAK;
1872 * Default method for processing an A-MPDU tx aggregation
1873 * response. We shutdown any pending timer and update the
1874 * state block according to the reply.
1877 ieee80211_addba_response(struct ieee80211_node *ni,
1878 struct ieee80211_tx_ampdu *tap,
1879 int status, int baparamset, int batimeout)
1884 addba_stop_timeout(tap);
1885 if (status == IEEE80211_STATUS_SUCCESS) {
1886 bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
1887 /* XXX override our request? */
1888 tap->txa_wnd = (bufsiz == 0) ?
1889 IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
1891 tid = MS(baparamset, IEEE80211_BAPS_TID);
1892 tap->txa_flags |= IEEE80211_AGGR_RUNNING;
1893 tap->txa_attempts = 0;
1895 /* mark tid so we don't try again */
1896 tap->txa_flags |= IEEE80211_AGGR_NAK;
1902 * Default method for stopping A-MPDU tx aggregation.
1903 * Any timer is cleared and we drain any pending frames.
1906 ieee80211_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
1909 addba_stop_timeout(tap);
1910 if (tap->txa_flags & IEEE80211_AGGR_RUNNING) {
1911 /* XXX clear aggregation queue */
1912 tap->txa_flags &= ~IEEE80211_AGGR_RUNNING;
1914 tap->txa_attempts = 0;
1918 * Process a received action frame using the default aggregation
1919 * policy. We intercept ADDBA-related frames and use them to
1920 * update our aggregation state. All other frames are passed up
1921 * for processing by ieee80211_recv_action.
1924 ht_recv_action_ba_addba_request(struct ieee80211_node *ni,
1925 const struct ieee80211_frame *wh,
1926 const uint8_t *frm, const uint8_t *efrm)
1928 struct ieee80211com *ic = ni->ni_ic;
1929 struct ieee80211vap *vap = ni->ni_vap;
1930 struct ieee80211_rx_ampdu *rap;
1931 uint8_t dialogtoken;
1932 uint16_t baparamset, batimeout, baseqctl;
1936 dialogtoken = frm[2];
1937 baparamset = le16dec(frm+3);
1938 batimeout = le16dec(frm+5);
1939 baseqctl = le16dec(frm+7);
1941 tid = MS(baparamset, IEEE80211_BAPS_TID);
1943 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1944 "recv ADDBA request: dialogtoken %u baparamset 0x%x "
1945 "(tid %d bufsiz %d) batimeout %d baseqctl %d:%d",
1946 dialogtoken, baparamset,
1947 tid, MS(baparamset, IEEE80211_BAPS_BUFSIZ),
1949 MS(baseqctl, IEEE80211_BASEQ_START),
1950 MS(baseqctl, IEEE80211_BASEQ_FRAG));
1952 rap = &ni->ni_rx_ampdu[tid];
1954 /* Send ADDBA response */
1955 args[0] = dialogtoken;
1957 * NB: We ack only if the sta associated with HT and
1958 * the ap is configured to do AMPDU rx (the latter
1959 * violates the 11n spec and is mostly for testing).
1961 if ((ni->ni_flags & IEEE80211_NODE_AMPDU_RX) &&
1962 (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_RX)) {
1963 /* XXX handle ampdu_rx_start failure */
1964 ic->ic_ampdu_rx_start(ni, rap,
1965 baparamset, batimeout, baseqctl);
1967 args[1] = IEEE80211_STATUS_SUCCESS;
1969 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1970 ni, "reject ADDBA request: %s",
1971 ni->ni_flags & IEEE80211_NODE_AMPDU_RX ?
1972 "administratively disabled" :
1973 "not negotiated for station");
1974 vap->iv_stats.is_addba_reject++;
1975 args[1] = IEEE80211_STATUS_UNSPECIFIED;
1977 /* XXX honor rap flags? */
1978 args[2] = IEEE80211_BAPS_POLICY_IMMEDIATE
1979 | SM(tid, IEEE80211_BAPS_TID)
1980 | SM(rap->rxa_wnd, IEEE80211_BAPS_BUFSIZ)
1984 ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
1985 IEEE80211_ACTION_BA_ADDBA_RESPONSE, args);
1990 ht_recv_action_ba_addba_response(struct ieee80211_node *ni,
1991 const struct ieee80211_frame *wh,
1992 const uint8_t *frm, const uint8_t *efrm)
1994 struct ieee80211com *ic = ni->ni_ic;
1995 struct ieee80211vap *vap = ni->ni_vap;
1996 struct ieee80211_tx_ampdu *tap;
1997 uint8_t dialogtoken, policy;
1998 uint16_t baparamset, batimeout, code;
2001 dialogtoken = frm[2];
2002 code = le16dec(frm+3);
2003 baparamset = le16dec(frm+5);
2004 tid = MS(baparamset, IEEE80211_BAPS_TID);
2005 bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
2006 policy = MS(baparamset, IEEE80211_BAPS_POLICY);
2007 batimeout = le16dec(frm+7);
2009 tap = &ni->ni_tx_ampdu[tid];
2010 if ((tap->txa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
2011 IEEE80211_DISCARD_MAC(vap,
2012 IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2013 ni->ni_macaddr, "ADDBA response",
2014 "no pending ADDBA, tid %d dialogtoken %u "
2015 "code %d", tid, dialogtoken, code);
2016 vap->iv_stats.is_addba_norequest++;
2019 if (dialogtoken != tap->txa_token) {
2020 IEEE80211_DISCARD_MAC(vap,
2021 IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2022 ni->ni_macaddr, "ADDBA response",
2023 "dialogtoken mismatch: waiting for %d, "
2024 "received %d, tid %d code %d",
2025 tap->txa_token, dialogtoken, tid, code);
2026 vap->iv_stats.is_addba_badtoken++;
2029 /* NB: assumes IEEE80211_AGGR_IMMEDIATE is 1 */
2030 if (policy != (tap->txa_flags & IEEE80211_AGGR_IMMEDIATE)) {
2031 IEEE80211_DISCARD_MAC(vap,
2032 IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2033 ni->ni_macaddr, "ADDBA response",
2034 "policy mismatch: expecting %s, "
2035 "received %s, tid %d code %d",
2036 tap->txa_flags & IEEE80211_AGGR_IMMEDIATE,
2038 vap->iv_stats.is_addba_badpolicy++;
2042 /* XXX we take MIN in ieee80211_addba_response */
2043 if (bufsiz > IEEE80211_AGGR_BAWMAX) {
2044 IEEE80211_DISCARD_MAC(vap,
2045 IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2046 ni->ni_macaddr, "ADDBA response",
2047 "BA window too large: max %d, "
2048 "received %d, tid %d code %d",
2049 bufsiz, IEEE80211_AGGR_BAWMAX, tid, code);
2050 vap->iv_stats.is_addba_badbawinsize++;
2054 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2055 "recv ADDBA response: dialogtoken %u code %d "
2056 "baparamset 0x%x (tid %d bufsiz %d) batimeout %d",
2057 dialogtoken, code, baparamset, tid, bufsiz,
2059 ic->ic_addba_response(ni, tap, code, baparamset, batimeout);
2064 ht_recv_action_ba_delba(struct ieee80211_node *ni,
2065 const struct ieee80211_frame *wh,
2066 const uint8_t *frm, const uint8_t *efrm)
2068 struct ieee80211com *ic = ni->ni_ic;
2069 struct ieee80211_rx_ampdu *rap;
2070 struct ieee80211_tx_ampdu *tap;
2071 uint16_t baparamset, code;
2074 baparamset = le16dec(frm+2);
2075 code = le16dec(frm+4);
2077 tid = MS(baparamset, IEEE80211_DELBAPS_TID);
2079 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2080 "recv DELBA: baparamset 0x%x (tid %d initiator %d) "
2081 "code %d", baparamset, tid,
2082 MS(baparamset, IEEE80211_DELBAPS_INIT), code);
2084 if ((baparamset & IEEE80211_DELBAPS_INIT) == 0) {
2085 tap = &ni->ni_tx_ampdu[tid];
2086 ic->ic_addba_stop(ni, tap);
2088 rap = &ni->ni_rx_ampdu[tid];
2089 ic->ic_ampdu_rx_stop(ni, rap);
2095 ht_recv_action_ht_txchwidth(struct ieee80211_node *ni,
2096 const struct ieee80211_frame *wh,
2097 const uint8_t *frm, const uint8_t *efrm)
2101 chw = (frm[2] == IEEE80211_A_HT_TXCHWIDTH_2040) ? 40 : 20;
2103 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2104 "%s: HT txchwidth, width %d%s",
2105 __func__, chw, ni->ni_chw != chw ? "*" : "");
2106 if (chw != ni->ni_chw) {
2108 /* XXX notify on change */
2114 ht_recv_action_ht_mimopwrsave(struct ieee80211_node *ni,
2115 const struct ieee80211_frame *wh,
2116 const uint8_t *frm, const uint8_t *efrm)
2118 const struct ieee80211_action_ht_mimopowersave *mps =
2119 (const struct ieee80211_action_ht_mimopowersave *) frm;
2121 /* XXX check iv_htcaps */
2122 if (mps->am_control & IEEE80211_A_HT_MIMOPWRSAVE_ENA)
2123 ni->ni_flags |= IEEE80211_NODE_MIMO_PS;
2125 ni->ni_flags &= ~IEEE80211_NODE_MIMO_PS;
2126 if (mps->am_control & IEEE80211_A_HT_MIMOPWRSAVE_MODE)
2127 ni->ni_flags |= IEEE80211_NODE_MIMO_RTS;
2129 ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS;
2130 /* XXX notify on change */
2131 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2132 "%s: HT MIMO PS (%s%s)", __func__,
2133 (ni->ni_flags & IEEE80211_NODE_MIMO_PS) ? "on" : "off",
2134 (ni->ni_flags & IEEE80211_NODE_MIMO_RTS) ? "+rts" : ""
2140 * Transmit processing.
2144 * Check if A-MPDU should be requested/enabled for a stream.
2145 * We require a traffic rate above a per-AC threshold and we
2146 * also handle backoff from previous failed attempts.
2148 * Drivers may override this method to bring in information
2149 * such as link state conditions in making the decision.
2152 ieee80211_ampdu_enable(struct ieee80211_node *ni,
2153 struct ieee80211_tx_ampdu *tap)
2155 struct ieee80211vap *vap = ni->ni_vap;
2157 if (tap->txa_avgpps <
2158 vap->iv_ampdu_mintraffic[TID_TO_WME_AC(tap->txa_tid)])
2160 /* XXX check rssi? */
2161 if (tap->txa_attempts >= ieee80211_addba_maxtries &&
2162 ieee80211_time_after(ticks, tap->txa_nextrequest)) {
2164 * Don't retry too often; txa_nextrequest is set
2165 * to the minimum interval we'll retry after
2166 * ieee80211_addba_maxtries failed attempts are made.
2170 IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
2171 "enable AMPDU on tid %d (%s), avgpps %d pkts %d attempt %d",
2172 tap->txa_tid, ieee80211_wme_acnames[TID_TO_WME_AC(tap->txa_tid)],
2173 tap->txa_avgpps, tap->txa_pkts, tap->txa_attempts);
2178 * Request A-MPDU tx aggregation. Setup local state and
2179 * issue an ADDBA request. BA use will only happen after
2180 * the other end replies with ADDBA response.
2183 ieee80211_ampdu_request(struct ieee80211_node *ni,
2184 struct ieee80211_tx_ampdu *tap)
2186 struct ieee80211com *ic = ni->ni_ic;
2188 int tid, dialogtoken;
2189 static int tokens = 0; /* XXX */
2192 if ((tap->txa_flags & IEEE80211_AGGR_SETUP) == 0) {
2193 /* do deferred setup of state */
2194 ampdu_tx_setup(tap);
2196 /* XXX hack for not doing proper locking */
2197 tap->txa_flags &= ~IEEE80211_AGGR_NAK;
2199 dialogtoken = (tokens+1) % 63; /* XXX */
2201 tap->txa_start = ni->ni_txseqs[tid];
2203 args[0] = dialogtoken;
2204 args[1] = 0; /* NB: status code not used */
2205 args[2] = IEEE80211_BAPS_POLICY_IMMEDIATE
2206 | SM(tid, IEEE80211_BAPS_TID)
2207 | SM(IEEE80211_AGGR_BAWMAX, IEEE80211_BAPS_BUFSIZ)
2209 args[3] = 0; /* batimeout */
2210 /* NB: do first so there's no race against reply */
2211 if (!ic->ic_addba_request(ni, tap, dialogtoken, args[2], args[3])) {
2212 /* unable to setup state, don't make request */
2213 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2214 ni, "%s: could not setup BA stream for TID %d AC %d",
2215 __func__, tap->txa_tid, TID_TO_WME_AC(tap->txa_tid));
2216 /* defer next try so we don't slam the driver with requests */
2217 tap->txa_attempts = ieee80211_addba_maxtries;
2218 /* NB: check in case driver wants to override */
2219 if (tap->txa_nextrequest <= ticks)
2220 tap->txa_nextrequest = ticks + ieee80211_addba_backoff;
2223 tokens = dialogtoken; /* allocate token */
2224 /* NB: after calling ic_addba_request so driver can set txa_start */
2225 args[4] = SM(tap->txa_start, IEEE80211_BASEQ_START)
2226 | SM(0, IEEE80211_BASEQ_FRAG)
2228 return ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
2229 IEEE80211_ACTION_BA_ADDBA_REQUEST, args);
2233 * Terminate an AMPDU tx stream. State is reclaimed
2234 * and the peer notified with a DelBA Action frame.
2237 ieee80211_ampdu_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
2240 struct ieee80211com *ic = ni->ni_ic;
2241 struct ieee80211vap *vap = ni->ni_vap;
2245 tap->txa_flags &= ~IEEE80211_AGGR_BARPEND;
2246 if (IEEE80211_AMPDU_RUNNING(tap)) {
2247 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2248 ni, "%s: stop BA stream for TID %d (reason: %d (%s))",
2249 __func__, tap->txa_tid, reason,
2250 ieee80211_reason_to_string(reason));
2251 vap->iv_stats.is_ampdu_stop++;
2253 ic->ic_addba_stop(ni, tap);
2254 args[0] = tap->txa_tid;
2255 args[1] = IEEE80211_DELBAPS_INIT;
2256 args[2] = reason; /* XXX reason code */
2257 ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
2258 IEEE80211_ACTION_BA_DELBA, args);
2260 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2261 ni, "%s: BA stream for TID %d not running "
2262 "(reason: %d (%s))", __func__, tap->txa_tid, reason,
2263 ieee80211_reason_to_string(reason));
2264 vap->iv_stats.is_ampdu_stop_failed++;
2269 static void bar_start_timer(struct ieee80211_tx_ampdu *tap);
2272 bar_timeout(void *arg)
2274 struct ieee80211_tx_ampdu *tap = arg;
2275 struct ieee80211_node *ni = tap->txa_ni;
2277 KASSERT((tap->txa_flags & IEEE80211_AGGR_XCHGPEND) == 0,
2278 ("bar/addba collision, flags 0x%x", tap->txa_flags));
2280 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2281 ni, "%s: tid %u flags 0x%x attempts %d", __func__,
2282 tap->txa_tid, tap->txa_flags, tap->txa_attempts);
2284 /* guard against race with bar_tx_complete */
2285 if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) == 0)
2288 if (tap->txa_attempts >= ieee80211_bar_maxtries) {
2289 struct ieee80211com *ic = ni->ni_ic;
2291 ni->ni_vap->iv_stats.is_ampdu_bar_tx_fail++;
2293 * If (at least) the last BAR TX timeout was due to
2294 * an ieee80211_send_bar() failures, then we need
2295 * to make sure we notify the driver that a BAR
2296 * TX did occur and fail. This gives the driver
2297 * a chance to undo any queue pause that may
2300 ic->ic_bar_response(ni, tap, 1);
2301 ieee80211_ampdu_stop(ni, tap, IEEE80211_REASON_TIMEOUT);
2303 ni->ni_vap->iv_stats.is_ampdu_bar_tx_retry++;
2304 if (ieee80211_send_bar(ni, tap, tap->txa_seqpending) != 0) {
2305 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2306 ni, "%s: failed to TX, starting timer\n",
2309 * If ieee80211_send_bar() fails here, the
2310 * timer may have stopped and/or the pending
2311 * flag may be clear. Because of this,
2312 * fake the BARPEND and reset the timer.
2313 * A retransmission attempt will then occur
2314 * during the next timeout.
2317 tap->txa_flags |= IEEE80211_AGGR_BARPEND;
2318 bar_start_timer(tap);
2324 bar_start_timer(struct ieee80211_tx_ampdu *tap)
2326 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2330 callout_reset(&tap->txa_timer, ieee80211_bar_timeout, bar_timeout, tap);
2334 bar_stop_timer(struct ieee80211_tx_ampdu *tap)
2336 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2340 callout_stop(&tap->txa_timer);
2344 bar_tx_complete(struct ieee80211_node *ni, void *arg, int status)
2346 struct ieee80211_tx_ampdu *tap = arg;
2348 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2349 ni, "%s: tid %u flags 0x%x pending %d status %d",
2350 __func__, tap->txa_tid, tap->txa_flags,
2351 callout_pending(&tap->txa_timer), status);
2353 ni->ni_vap->iv_stats.is_ampdu_bar_tx++;
2355 if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) &&
2356 callout_pending(&tap->txa_timer)) {
2357 struct ieee80211com *ic = ni->ni_ic;
2359 if (status == 0) /* ACK'd */
2360 bar_stop_timer(tap);
2361 ic->ic_bar_response(ni, tap, status);
2362 /* NB: just let timer expire so we pace requests */
2367 ieee80211_bar_response(struct ieee80211_node *ni,
2368 struct ieee80211_tx_ampdu *tap, int status)
2371 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2375 if (status == 0) { /* got ACK */
2376 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2377 ni, "BAR moves BA win <%u:%u> (%u frames) txseq %u tid %u",
2379 IEEE80211_SEQ_ADD(tap->txa_start, tap->txa_wnd-1),
2380 tap->txa_qframes, tap->txa_seqpending,
2383 /* NB: timer already stopped in bar_tx_complete */
2384 tap->txa_start = tap->txa_seqpending;
2385 tap->txa_flags &= ~IEEE80211_AGGR_BARPEND;
2390 * Transmit a BAR frame to the specified node. The
2391 * BAR contents are drawn from the supplied aggregation
2392 * state associated with the node.
2394 * NB: we only handle immediate ACK w/ compressed bitmap.
2397 ieee80211_send_bar(struct ieee80211_node *ni,
2398 struct ieee80211_tx_ampdu *tap, ieee80211_seq seq)
2400 #define senderr(_x, _v) do { vap->iv_stats._v++; ret = _x; goto bad; } while (0)
2401 struct ieee80211vap *vap = ni->ni_vap;
2402 struct ieee80211com *ic = ni->ni_ic;
2403 struct ieee80211_frame_bar *bar;
2405 uint16_t barctl, barseqctl;
2410 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2415 if ((tap->txa_flags & IEEE80211_AGGR_RUNNING) == 0) {
2416 /* no ADDBA response, should not happen */
2421 bar_stop_timer(tap);
2423 ieee80211_ref_node(ni);
2425 m = ieee80211_getmgtframe(&frm, ic->ic_headroom, sizeof(*bar));
2427 senderr(ENOMEM, is_tx_nobuf);
2429 if (!ieee80211_add_callback(m, bar_tx_complete, tap)) {
2431 senderr(ENOMEM, is_tx_nobuf); /* XXX */
2435 bar = mtod(m, struct ieee80211_frame_bar *);
2436 bar->i_fc[0] = IEEE80211_FC0_VERSION_0 |
2437 IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_BAR;
2439 IEEE80211_ADDR_COPY(bar->i_ra, ni->ni_macaddr);
2440 IEEE80211_ADDR_COPY(bar->i_ta, vap->iv_myaddr);
2443 barctl = (tap->txa_flags & IEEE80211_AGGR_IMMEDIATE ?
2444 0 : IEEE80211_BAR_NOACK)
2445 | IEEE80211_BAR_COMP
2446 | SM(tid, IEEE80211_BAR_TID)
2448 barseqctl = SM(seq, IEEE80211_BAR_SEQ_START);
2449 /* NB: known to have proper alignment */
2450 bar->i_ctl = htole16(barctl);
2451 bar->i_seq = htole16(barseqctl);
2452 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_bar);
2454 M_WME_SETAC(m, WME_AC_VO);
2456 IEEE80211_NODE_STAT(ni, tx_mgmt); /* XXX tx_ctl? */
2459 /* init/bump attempts counter */
2460 if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) == 0)
2461 tap->txa_attempts = 1;
2463 tap->txa_attempts++;
2464 tap->txa_seqpending = seq;
2465 tap->txa_flags |= IEEE80211_AGGR_BARPEND;
2467 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_11N,
2468 ni, "send BAR: tid %u ctl 0x%x start %u (attempt %d)",
2469 tid, barctl, seq, tap->txa_attempts);
2472 * ic_raw_xmit will free the node reference
2473 * regardless of queue/TX success or failure.
2475 IEEE80211_TX_LOCK(ic);
2476 ret = ieee80211_raw_output(vap, ni, m, NULL);
2477 IEEE80211_TX_UNLOCK(ic);
2479 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_11N,
2480 ni, "send BAR: failed: (ret = %d)\n",
2482 /* xmit failed, clear state flag */
2483 tap->txa_flags &= ~IEEE80211_AGGR_BARPEND;
2484 vap->iv_stats.is_ampdu_bar_tx_fail++;
2487 /* XXX hack against tx complete happening before timer is started */
2488 if (tap->txa_flags & IEEE80211_AGGR_BARPEND)
2489 bar_start_timer(tap);
2492 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2496 vap->iv_stats.is_ampdu_bar_tx_fail++;
2497 ieee80211_free_node(ni);
2503 ht_action_output(struct ieee80211_node *ni, struct mbuf *m)
2505 struct ieee80211_bpf_params params;
2507 memset(¶ms, 0, sizeof(params));
2508 params.ibp_pri = WME_AC_VO;
2509 params.ibp_rate0 = ni->ni_txparms->mgmtrate;
2510 /* NB: we know all frames are unicast */
2511 params.ibp_try0 = ni->ni_txparms->maxretry;
2512 params.ibp_power = ni->ni_txpower;
2513 return ieee80211_mgmt_output(ni, m, IEEE80211_FC0_SUBTYPE_ACTION,
2517 #define ADDSHORT(frm, v) do { \
2518 frm[0] = (v) & 0xff; \
2519 frm[1] = (v) >> 8; \
2524 * Send an action management frame. The arguments are stuff
2525 * into a frame without inspection; the caller is assumed to
2526 * prepare them carefully (e.g. based on the aggregation state).
2529 ht_send_action_ba_addba(struct ieee80211_node *ni,
2530 int category, int action, void *arg0)
2532 struct ieee80211vap *vap = ni->ni_vap;
2533 struct ieee80211com *ic = ni->ni_ic;
2534 uint16_t *args = arg0;
2538 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2539 "send ADDBA %s: dialogtoken %d status %d "
2540 "baparamset 0x%x (tid %d) batimeout 0x%x baseqctl 0x%x",
2541 (action == IEEE80211_ACTION_BA_ADDBA_REQUEST) ?
2542 "request" : "response",
2543 args[0], args[1], args[2], MS(args[2], IEEE80211_BAPS_TID),
2546 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2547 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2548 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2549 ieee80211_ref_node(ni);
2551 m = ieee80211_getmgtframe(&frm,
2552 ic->ic_headroom + sizeof(struct ieee80211_frame),
2553 sizeof(uint16_t) /* action+category */
2554 /* XXX may action payload */
2555 + sizeof(struct ieee80211_action_ba_addbaresponse)
2560 *frm++ = args[0]; /* dialog token */
2561 if (action == IEEE80211_ACTION_BA_ADDBA_RESPONSE)
2562 ADDSHORT(frm, args[1]); /* status code */
2563 ADDSHORT(frm, args[2]); /* baparamset */
2564 ADDSHORT(frm, args[3]); /* batimeout */
2565 if (action == IEEE80211_ACTION_BA_ADDBA_REQUEST)
2566 ADDSHORT(frm, args[4]); /* baseqctl */
2567 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2568 return ht_action_output(ni, m);
2570 vap->iv_stats.is_tx_nobuf++;
2571 ieee80211_free_node(ni);
2577 ht_send_action_ba_delba(struct ieee80211_node *ni,
2578 int category, int action, void *arg0)
2580 struct ieee80211vap *vap = ni->ni_vap;
2581 struct ieee80211com *ic = ni->ni_ic;
2582 uint16_t *args = arg0;
2584 uint16_t baparamset;
2587 baparamset = SM(args[0], IEEE80211_DELBAPS_TID)
2590 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2591 "send DELBA action: tid %d, initiator %d reason %d (%s)",
2592 args[0], args[1], args[2], ieee80211_reason_to_string(args[2]));
2594 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2595 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2596 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2597 ieee80211_ref_node(ni);
2599 m = ieee80211_getmgtframe(&frm,
2600 ic->ic_headroom + sizeof(struct ieee80211_frame),
2601 sizeof(uint16_t) /* action+category */
2602 /* XXX may action payload */
2603 + sizeof(struct ieee80211_action_ba_addbaresponse)
2608 ADDSHORT(frm, baparamset);
2609 ADDSHORT(frm, args[2]); /* reason code */
2610 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2611 return ht_action_output(ni, m);
2613 vap->iv_stats.is_tx_nobuf++;
2614 ieee80211_free_node(ni);
2620 ht_send_action_ht_txchwidth(struct ieee80211_node *ni,
2621 int category, int action, void *arg0)
2623 struct ieee80211vap *vap = ni->ni_vap;
2624 struct ieee80211com *ic = ni->ni_ic;
2628 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2629 "send HT txchwidth: width %d",
2630 IEEE80211_IS_CHAN_HT40(ni->ni_chan) ? 40 : 20);
2632 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2633 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2634 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2635 ieee80211_ref_node(ni);
2637 m = ieee80211_getmgtframe(&frm,
2638 ic->ic_headroom + sizeof(struct ieee80211_frame),
2639 sizeof(uint16_t) /* action+category */
2640 /* XXX may action payload */
2641 + sizeof(struct ieee80211_action_ba_addbaresponse)
2646 *frm++ = IEEE80211_IS_CHAN_HT40(ni->ni_chan) ?
2647 IEEE80211_A_HT_TXCHWIDTH_2040 :
2648 IEEE80211_A_HT_TXCHWIDTH_20;
2649 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2650 return ht_action_output(ni, m);
2652 vap->iv_stats.is_tx_nobuf++;
2653 ieee80211_free_node(ni);
2660 * Construct the MCS bit mask for inclusion in an HT capabilities
2661 * information element.
2664 ieee80211_set_mcsset(struct ieee80211com *ic, uint8_t *frm)
2669 KASSERT((ic->ic_rxstream > 0 && ic->ic_rxstream <= 4),
2670 ("ic_rxstream %d out of range", ic->ic_rxstream));
2671 KASSERT((ic->ic_txstream > 0 && ic->ic_txstream <= 4),
2672 ("ic_txstream %d out of range", ic->ic_txstream));
2674 for (i = 0; i < ic->ic_rxstream * 8; i++)
2676 if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
2677 (ic->ic_htcaps & IEEE80211_HTC_RXMCS32))
2679 if (ic->ic_htcaps & IEEE80211_HTC_RXUNEQUAL) {
2680 if (ic->ic_rxstream >= 2) {
2681 for (i = 33; i <= 38; i++)
2684 if (ic->ic_rxstream >= 3) {
2685 for (i = 39; i <= 52; i++)
2688 if (ic->ic_txstream >= 4) {
2689 for (i = 53; i <= 76; i++)
2694 if (ic->ic_rxstream != ic->ic_txstream) {
2695 txparams = 0x1; /* TX MCS set defined */
2696 txparams |= 0x2; /* TX RX MCS not equal */
2697 txparams |= (ic->ic_txstream - 1) << 2; /* num TX streams */
2698 if (ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL)
2699 txparams |= 0x16; /* TX unequal modulation sup */
2706 * Add body of an HTCAP information element.
2709 ieee80211_add_htcap_body(uint8_t *frm, struct ieee80211_node *ni)
2711 #define ADDSHORT(frm, v) do { \
2712 frm[0] = (v) & 0xff; \
2713 frm[1] = (v) >> 8; \
2716 struct ieee80211com *ic = ni->ni_ic;
2717 struct ieee80211vap *vap = ni->ni_vap;
2718 uint16_t caps, extcaps;
2721 /* HT capabilities */
2722 caps = vap->iv_htcaps & 0xffff;
2724 * Note channel width depends on whether we are operating as
2725 * a sta or not. When operating as a sta we are generating
2726 * a request based on our desired configuration. Otherwise
2727 * we are operational and the channel attributes identify
2728 * how we've been setup (which might be different if a fixed
2729 * channel is specified).
2731 if (vap->iv_opmode == IEEE80211_M_STA) {
2732 /* override 20/40 use based on config */
2733 if (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)
2734 caps |= IEEE80211_HTCAP_CHWIDTH40;
2736 caps &= ~IEEE80211_HTCAP_CHWIDTH40;
2738 /* Start by using the advertised settings */
2739 rxmax = MS(ni->ni_htparam, IEEE80211_HTCAP_MAXRXAMPDU);
2740 density = MS(ni->ni_htparam, IEEE80211_HTCAP_MPDUDENSITY);
2742 IEEE80211_DPRINTF(vap, IEEE80211_MSG_11N,
2743 "%s: advertised rxmax=%d, density=%d, vap rxmax=%d, density=%d\n",
2747 vap->iv_ampdu_rxmax,
2748 vap->iv_ampdu_density);
2750 /* Cap at VAP rxmax */
2751 if (rxmax > vap->iv_ampdu_rxmax)
2752 rxmax = vap->iv_ampdu_rxmax;
2755 * If the VAP ampdu density value greater, use that.
2757 * (Larger density value == larger minimum gap between A-MPDU
2760 if (vap->iv_ampdu_density > density)
2761 density = vap->iv_ampdu_density;
2764 * NB: Hardware might support HT40 on some but not all
2765 * channels. We can't determine this earlier because only
2766 * after association the channel is upgraded to HT based
2767 * on the negotiated capabilities.
2769 if (ni->ni_chan != IEEE80211_CHAN_ANYC &&
2770 findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT40U) == NULL &&
2771 findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT40D) == NULL)
2772 caps &= ~IEEE80211_HTCAP_CHWIDTH40;
2774 /* override 20/40 use based on current channel */
2775 if (IEEE80211_IS_CHAN_HT40(ni->ni_chan))
2776 caps |= IEEE80211_HTCAP_CHWIDTH40;
2778 caps &= ~IEEE80211_HTCAP_CHWIDTH40;
2780 /* XXX TODO should it start by using advertised settings? */
2781 rxmax = vap->iv_ampdu_rxmax;
2782 density = vap->iv_ampdu_density;
2785 /* adjust short GI based on channel and config */
2786 if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20) == 0)
2787 caps &= ~IEEE80211_HTCAP_SHORTGI20;
2788 if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40) == 0 ||
2789 (caps & IEEE80211_HTCAP_CHWIDTH40) == 0)
2790 caps &= ~IEEE80211_HTCAP_SHORTGI40;
2792 /* adjust STBC based on receive capabilities */
2793 if ((vap->iv_flags_ht & IEEE80211_FHT_STBC_RX) == 0)
2794 caps &= ~IEEE80211_HTCAP_RXSTBC;
2796 /* XXX TODO: adjust LDPC based on receive capabilities */
2798 ADDSHORT(frm, caps);
2801 *frm = SM(rxmax, IEEE80211_HTCAP_MAXRXAMPDU)
2802 | SM(density, IEEE80211_HTCAP_MPDUDENSITY)
2806 /* pre-zero remainder of ie */
2807 memset(frm, 0, sizeof(struct ieee80211_ie_htcap) -
2808 __offsetof(struct ieee80211_ie_htcap, hc_mcsset));
2810 /* supported MCS set */
2812 * XXX: For sta mode the rate set should be restricted based
2813 * on the AP's capabilities, but ni_htrates isn't setup when
2814 * we're called to form an AssocReq frame so for now we're
2815 * restricted to the device capabilities.
2817 ieee80211_set_mcsset(ni->ni_ic, frm);
2819 frm += __offsetof(struct ieee80211_ie_htcap, hc_extcap) -
2820 __offsetof(struct ieee80211_ie_htcap, hc_mcsset);
2822 /* HT extended capabilities */
2823 extcaps = vap->iv_htextcaps & 0xffff;
2825 ADDSHORT(frm, extcaps);
2827 frm += sizeof(struct ieee80211_ie_htcap) -
2828 __offsetof(struct ieee80211_ie_htcap, hc_txbf);
2835 * Add 802.11n HT capabilities information element
2838 ieee80211_add_htcap(uint8_t *frm, struct ieee80211_node *ni)
2840 frm[0] = IEEE80211_ELEMID_HTCAP;
2841 frm[1] = sizeof(struct ieee80211_ie_htcap) - 2;
2842 return ieee80211_add_htcap_body(frm + 2, ni);
2846 * Non-associated probe request - add HT capabilities based on
2847 * the current channel configuration.
2850 ieee80211_add_htcap_body_ch(uint8_t *frm, struct ieee80211vap *vap,
2851 struct ieee80211_channel *c)
2853 #define ADDSHORT(frm, v) do { \
2854 frm[0] = (v) & 0xff; \
2855 frm[1] = (v) >> 8; \
2858 struct ieee80211com *ic = vap->iv_ic;
2859 uint16_t caps, extcaps;
2862 /* HT capabilities */
2863 caps = vap->iv_htcaps & 0xffff;
2866 * We don't use this in STA mode; only in IBSS mode.
2867 * So in IBSS mode we base our HTCAP flags on the
2871 /* override 20/40 use based on current channel */
2872 if (IEEE80211_IS_CHAN_HT40(c))
2873 caps |= IEEE80211_HTCAP_CHWIDTH40;
2875 caps &= ~IEEE80211_HTCAP_CHWIDTH40;
2877 /* Use the currently configured values */
2878 rxmax = vap->iv_ampdu_rxmax;
2879 density = vap->iv_ampdu_density;
2881 /* adjust short GI based on channel and config */
2882 if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20) == 0)
2883 caps &= ~IEEE80211_HTCAP_SHORTGI20;
2884 if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40) == 0 ||
2885 (caps & IEEE80211_HTCAP_CHWIDTH40) == 0)
2886 caps &= ~IEEE80211_HTCAP_SHORTGI40;
2887 ADDSHORT(frm, caps);
2890 *frm = SM(rxmax, IEEE80211_HTCAP_MAXRXAMPDU)
2891 | SM(density, IEEE80211_HTCAP_MPDUDENSITY)
2895 /* pre-zero remainder of ie */
2896 memset(frm, 0, sizeof(struct ieee80211_ie_htcap) -
2897 __offsetof(struct ieee80211_ie_htcap, hc_mcsset));
2899 /* supported MCS set */
2901 * XXX: For sta mode the rate set should be restricted based
2902 * on the AP's capabilities, but ni_htrates isn't setup when
2903 * we're called to form an AssocReq frame so for now we're
2904 * restricted to the device capabilities.
2906 ieee80211_set_mcsset(ic, frm);
2908 frm += __offsetof(struct ieee80211_ie_htcap, hc_extcap) -
2909 __offsetof(struct ieee80211_ie_htcap, hc_mcsset);
2911 /* HT extended capabilities */
2912 extcaps = vap->iv_htextcaps & 0xffff;
2914 ADDSHORT(frm, extcaps);
2916 frm += sizeof(struct ieee80211_ie_htcap) -
2917 __offsetof(struct ieee80211_ie_htcap, hc_txbf);
2924 * Add 802.11n HT capabilities information element
2927 ieee80211_add_htcap_ch(uint8_t *frm, struct ieee80211vap *vap,
2928 struct ieee80211_channel *c)
2930 frm[0] = IEEE80211_ELEMID_HTCAP;
2931 frm[1] = sizeof(struct ieee80211_ie_htcap) - 2;
2932 return ieee80211_add_htcap_body_ch(frm + 2, vap, c);
2936 * Add Broadcom OUI wrapped standard HTCAP ie; this is
2937 * used for compatibility w/ pre-draft implementations.
2940 ieee80211_add_htcap_vendor(uint8_t *frm, struct ieee80211_node *ni)
2942 frm[0] = IEEE80211_ELEMID_VENDOR;
2943 frm[1] = 4 + sizeof(struct ieee80211_ie_htcap) - 2;
2944 frm[2] = (BCM_OUI >> 0) & 0xff;
2945 frm[3] = (BCM_OUI >> 8) & 0xff;
2946 frm[4] = (BCM_OUI >> 16) & 0xff;
2947 frm[5] = BCM_OUI_HTCAP;
2948 return ieee80211_add_htcap_body(frm + 6, ni);
2952 * Construct the MCS bit mask of basic rates
2953 * for inclusion in an HT information element.
2956 ieee80211_set_basic_htrates(uint8_t *frm, const struct ieee80211_htrateset *rs)
2960 for (i = 0; i < rs->rs_nrates; i++) {
2961 int r = rs->rs_rates[i] & IEEE80211_RATE_VAL;
2962 if ((rs->rs_rates[i] & IEEE80211_RATE_BASIC) &&
2963 r < IEEE80211_HTRATE_MAXSIZE) {
2964 /* NB: this assumes a particular implementation */
2971 * Update the HTINFO ie for a beacon frame.
2974 ieee80211_ht_update_beacon(struct ieee80211vap *vap,
2975 struct ieee80211_beacon_offsets *bo)
2977 #define PROTMODE (IEEE80211_HTINFO_OPMODE|IEEE80211_HTINFO_NONHT_PRESENT)
2978 struct ieee80211_node *ni;
2979 const struct ieee80211_channel *bsschan;
2980 struct ieee80211com *ic = vap->iv_ic;
2981 struct ieee80211_ie_htinfo *ht =
2982 (struct ieee80211_ie_htinfo *) bo->bo_htinfo;
2984 ni = ieee80211_ref_node(vap->iv_bss);
2985 bsschan = ni->ni_chan;
2987 /* XXX only update on channel change */
2988 ht->hi_ctrlchannel = ieee80211_chan2ieee(ic, bsschan);
2989 if (vap->iv_flags_ht & IEEE80211_FHT_RIFS)
2990 ht->hi_byte1 = IEEE80211_HTINFO_RIFSMODE_PERM;
2992 ht->hi_byte1 = IEEE80211_HTINFO_RIFSMODE_PROH;
2993 if (IEEE80211_IS_CHAN_HT40U(bsschan))
2994 ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_ABOVE;
2995 else if (IEEE80211_IS_CHAN_HT40D(bsschan))
2996 ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_BELOW;
2998 ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_NONE;
2999 if (IEEE80211_IS_CHAN_HT40(bsschan))
3000 ht->hi_byte1 |= IEEE80211_HTINFO_TXWIDTH_2040;
3002 /* protection mode */
3003 ht->hi_byte2 = (ht->hi_byte2 &~ PROTMODE) | ic->ic_curhtprotmode;
3005 ieee80211_free_node(ni);
3007 /* XXX propagate to vendor ie's */
3012 * Add body of an HTINFO information element.
3014 * NB: We don't use struct ieee80211_ie_htinfo because we can
3015 * be called to fillin both a standard ie and a compat ie that
3016 * has a vendor OUI at the front.
3019 ieee80211_add_htinfo_body(uint8_t *frm, struct ieee80211_node *ni)
3021 struct ieee80211vap *vap = ni->ni_vap;
3022 struct ieee80211com *ic = ni->ni_ic;
3024 /* pre-zero remainder of ie */
3025 memset(frm, 0, sizeof(struct ieee80211_ie_htinfo) - 2);
3027 /* primary/control channel center */
3028 *frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
3030 if (vap->iv_flags_ht & IEEE80211_FHT_RIFS)
3031 frm[0] = IEEE80211_HTINFO_RIFSMODE_PERM;
3033 frm[0] = IEEE80211_HTINFO_RIFSMODE_PROH;
3034 if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan))
3035 frm[0] |= IEEE80211_HTINFO_2NDCHAN_ABOVE;
3036 else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan))
3037 frm[0] |= IEEE80211_HTINFO_2NDCHAN_BELOW;
3039 frm[0] |= IEEE80211_HTINFO_2NDCHAN_NONE;
3040 if (IEEE80211_IS_CHAN_HT40(ni->ni_chan))
3041 frm[0] |= IEEE80211_HTINFO_TXWIDTH_2040;
3043 frm[1] = ic->ic_curhtprotmode;
3048 ieee80211_set_basic_htrates(frm, &ni->ni_htrates);
3049 frm += sizeof(struct ieee80211_ie_htinfo) -
3050 __offsetof(struct ieee80211_ie_htinfo, hi_basicmcsset);
3055 * Add 802.11n HT information element.
3058 ieee80211_add_htinfo(uint8_t *frm, struct ieee80211_node *ni)
3060 frm[0] = IEEE80211_ELEMID_HTINFO;
3061 frm[1] = sizeof(struct ieee80211_ie_htinfo) - 2;
3062 return ieee80211_add_htinfo_body(frm + 2, ni);
3066 * Add Broadcom OUI wrapped standard HTINFO ie; this is
3067 * used for compatibility w/ pre-draft implementations.
3070 ieee80211_add_htinfo_vendor(uint8_t *frm, struct ieee80211_node *ni)
3072 frm[0] = IEEE80211_ELEMID_VENDOR;
3073 frm[1] = 4 + sizeof(struct ieee80211_ie_htinfo) - 2;
3074 frm[2] = (BCM_OUI >> 0) & 0xff;
3075 frm[3] = (BCM_OUI >> 8) & 0xff;
3076 frm[4] = (BCM_OUI >> 16) & 0xff;
3077 frm[5] = BCM_OUI_HTINFO;
3078 return ieee80211_add_htinfo_body(frm + 6, ni);