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/systm.h>
41 #include <sys/endian.h>
43 #include <sys/socket.h>
46 #include <net/if_media.h>
47 #include <net/ethernet.h>
49 #include <net80211/ieee80211_var.h>
50 #include <net80211/ieee80211_action.h>
51 #include <net80211/ieee80211_input.h>
53 /* define here, used throughout file */
54 #define MS(_v, _f) (((_v) & _f) >> _f##_S)
55 #define SM(_v, _f) (((_v) << _f##_S) & _f)
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 #ifdef IEEE80211_AMPDU_AGE
138 static int ieee80211_ampdu_age = -1; /* threshold for ampdu reorder q (ms) */
139 SYSCTL_PROC(_net_wlan, OID_AUTO, ampdu_age, CTLTYPE_INT | CTLFLAG_RW,
140 &ieee80211_ampdu_age, 0, ieee80211_sysctl_msecs_ticks, "I",
141 "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, CTLTYPE_INT | 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 #ifdef IEEE80211_AMPDU_AGE
180 ieee80211_ampdu_age = msecs_to_ticks(500);
182 ieee80211_addba_timeout = msecs_to_ticks(250);
183 ieee80211_addba_backoff = msecs_to_ticks(10*1000);
184 ieee80211_bar_timeout = msecs_to_ticks(250);
186 * Register action frame handlers.
188 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_BA,
189 IEEE80211_ACTION_BA_ADDBA_REQUEST, ht_recv_action_ba_addba_request);
190 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_BA,
191 IEEE80211_ACTION_BA_ADDBA_RESPONSE, ht_recv_action_ba_addba_response);
192 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_BA,
193 IEEE80211_ACTION_BA_DELBA, ht_recv_action_ba_delba);
194 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_HT,
195 IEEE80211_ACTION_HT_MIMOPWRSAVE, ht_recv_action_ht_mimopwrsave);
196 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_HT,
197 IEEE80211_ACTION_HT_TXCHWIDTH, ht_recv_action_ht_txchwidth);
199 ieee80211_send_action_register(IEEE80211_ACTION_CAT_BA,
200 IEEE80211_ACTION_BA_ADDBA_REQUEST, ht_send_action_ba_addba);
201 ieee80211_send_action_register(IEEE80211_ACTION_CAT_BA,
202 IEEE80211_ACTION_BA_ADDBA_RESPONSE, ht_send_action_ba_addba);
203 ieee80211_send_action_register(IEEE80211_ACTION_CAT_BA,
204 IEEE80211_ACTION_BA_DELBA, ht_send_action_ba_delba);
205 ieee80211_send_action_register(IEEE80211_ACTION_CAT_HT,
206 IEEE80211_ACTION_HT_TXCHWIDTH, ht_send_action_ht_txchwidth);
208 SYSINIT(wlan_ht, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_ht_init, NULL);
210 static int ieee80211_ampdu_enable(struct ieee80211_node *ni,
211 struct ieee80211_tx_ampdu *tap);
212 static int ieee80211_addba_request(struct ieee80211_node *ni,
213 struct ieee80211_tx_ampdu *tap,
214 int dialogtoken, int baparamset, int batimeout);
215 static int ieee80211_addba_response(struct ieee80211_node *ni,
216 struct ieee80211_tx_ampdu *tap,
217 int code, int baparamset, int batimeout);
218 static void ieee80211_addba_stop(struct ieee80211_node *ni,
219 struct ieee80211_tx_ampdu *tap);
220 static void null_addba_response_timeout(struct ieee80211_node *ni,
221 struct ieee80211_tx_ampdu *tap);
223 static void ieee80211_bar_response(struct ieee80211_node *ni,
224 struct ieee80211_tx_ampdu *tap, int status);
225 static void ampdu_tx_stop(struct ieee80211_tx_ampdu *tap);
226 static void bar_stop_timer(struct ieee80211_tx_ampdu *tap);
227 static int ampdu_rx_start(struct ieee80211_node *, struct ieee80211_rx_ampdu *,
228 int baparamset, int batimeout, int baseqctl);
229 static void ampdu_rx_stop(struct ieee80211_node *, struct ieee80211_rx_ampdu *);
232 ieee80211_ht_attach(struct ieee80211com *ic)
234 /* setup default aggregation policy */
235 ic->ic_recv_action = ieee80211_recv_action;
236 ic->ic_send_action = ieee80211_send_action;
237 ic->ic_ampdu_enable = ieee80211_ampdu_enable;
238 ic->ic_addba_request = ieee80211_addba_request;
239 ic->ic_addba_response = ieee80211_addba_response;
240 ic->ic_addba_response_timeout = null_addba_response_timeout;
241 ic->ic_addba_stop = ieee80211_addba_stop;
242 ic->ic_bar_response = ieee80211_bar_response;
243 ic->ic_ampdu_rx_start = ampdu_rx_start;
244 ic->ic_ampdu_rx_stop = ampdu_rx_stop;
246 ic->ic_htprotmode = IEEE80211_PROT_RTSCTS;
247 ic->ic_curhtprotmode = IEEE80211_HTINFO_OPMODE_PURE;
251 ieee80211_ht_detach(struct ieee80211com *ic)
256 ieee80211_ht_vattach(struct ieee80211vap *vap)
259 /* driver can override defaults */
260 vap->iv_ampdu_rxmax = IEEE80211_HTCAP_MAXRXAMPDU_8K;
261 vap->iv_ampdu_density = IEEE80211_HTCAP_MPDUDENSITY_NA;
262 vap->iv_ampdu_limit = vap->iv_ampdu_rxmax;
263 vap->iv_amsdu_limit = vap->iv_htcaps & IEEE80211_HTCAP_MAXAMSDU;
264 /* tx aggregation traffic thresholds */
265 vap->iv_ampdu_mintraffic[WME_AC_BK] = 128;
266 vap->iv_ampdu_mintraffic[WME_AC_BE] = 64;
267 vap->iv_ampdu_mintraffic[WME_AC_VO] = 32;
268 vap->iv_ampdu_mintraffic[WME_AC_VI] = 32;
270 if (vap->iv_htcaps & IEEE80211_HTC_HT) {
272 * Device is HT capable; enable all HT-related
273 * facilities by default.
274 * XXX these choices may be too aggressive.
276 vap->iv_flags_ht |= IEEE80211_FHT_HT
277 | IEEE80211_FHT_HTCOMPAT
279 if (vap->iv_htcaps & IEEE80211_HTCAP_SHORTGI20)
280 vap->iv_flags_ht |= IEEE80211_FHT_SHORTGI20;
281 /* XXX infer from channel list? */
282 if (vap->iv_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
283 vap->iv_flags_ht |= IEEE80211_FHT_USEHT40;
284 if (vap->iv_htcaps & IEEE80211_HTCAP_SHORTGI40)
285 vap->iv_flags_ht |= IEEE80211_FHT_SHORTGI40;
287 /* enable RIFS if capable */
288 if (vap->iv_htcaps & IEEE80211_HTC_RIFS)
289 vap->iv_flags_ht |= IEEE80211_FHT_RIFS;
291 /* NB: A-MPDU and A-MSDU rx are mandated, these are tx only */
292 vap->iv_flags_ht |= IEEE80211_FHT_AMPDU_RX;
293 if (vap->iv_htcaps & IEEE80211_HTC_AMPDU)
294 vap->iv_flags_ht |= IEEE80211_FHT_AMPDU_TX;
295 vap->iv_flags_ht |= IEEE80211_FHT_AMSDU_RX;
296 if (vap->iv_htcaps & IEEE80211_HTC_AMSDU)
297 vap->iv_flags_ht |= IEEE80211_FHT_AMSDU_TX;
299 /* NB: disable default legacy WDS, too many issues right now */
300 if (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY)
301 vap->iv_flags_ht &= ~IEEE80211_FHT_HT;
305 ieee80211_ht_vdetach(struct ieee80211vap *vap)
310 ht_getrate(struct ieee80211com *ic, int index, enum ieee80211_phymode mode,
315 mword = ieee80211_rate2media(ic, index | IEEE80211_RATE_MCS, mode);
316 if (IFM_SUBTYPE(mword) != IFM_IEEE80211_MCS)
320 rate = ieee80211_htrates[index].ht20_rate_800ns;
323 rate = ieee80211_htrates[index].ht20_rate_400ns;
326 rate = ieee80211_htrates[index].ht40_rate_800ns;
329 rate = ieee80211_htrates[index].ht40_rate_400ns;
335 static struct printranges {
346 { 32, 0, 1, 2, IEEE80211_HTC_TXMCS32 },
347 { 33, 38, 2, 0, IEEE80211_HTC_TXUNEQUAL },
348 { 39, 52, 3, 0, IEEE80211_HTC_TXUNEQUAL },
349 { 53, 76, 4, 0, IEEE80211_HTC_TXUNEQUAL },
354 ht_rateprint(struct ieee80211com *ic, enum ieee80211_phymode mode, int ratetype)
356 struct ifnet *ifp = ic->ic_ifp;
357 int minrate, maxrate;
358 struct printranges *range;
360 for (range = ranges; range->txstream != 0; range++) {
361 if (ic->ic_txstream < range->txstream)
363 if (range->htcapflags &&
364 (ic->ic_htcaps & range->htcapflags) == 0)
366 if (ratetype < range->ratetype)
368 minrate = ht_getrate(ic, range->minmcs, mode, ratetype);
369 maxrate = ht_getrate(ic, range->maxmcs, mode, ratetype);
371 if_printf(ifp, "MCS %d-%d: %d%sMbps - %d%sMbps\n",
372 range->minmcs, range->maxmcs,
373 minrate/2, ((minrate & 0x1) != 0 ? ".5" : ""),
374 maxrate/2, ((maxrate & 0x1) != 0 ? ".5" : ""));
376 if_printf(ifp, "MCS %d: %d%sMbps\n", range->minmcs,
377 minrate/2, ((minrate & 0x1) != 0 ? ".5" : ""));
383 ht_announce(struct ieee80211com *ic, enum ieee80211_phymode mode)
385 struct ifnet *ifp = ic->ic_ifp;
386 const char *modestr = ieee80211_phymode_name[mode];
388 if_printf(ifp, "%s MCS 20MHz\n", modestr);
389 ht_rateprint(ic, mode, 0);
390 if (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20) {
391 if_printf(ifp, "%s MCS 20MHz SGI\n", modestr);
392 ht_rateprint(ic, mode, 1);
394 if (ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
395 if_printf(ifp, "%s MCS 40MHz:\n", modestr);
396 ht_rateprint(ic, mode, 2);
398 if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
399 (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40)) {
400 if_printf(ifp, "%s MCS 40MHz SGI:\n", modestr);
401 ht_rateprint(ic, mode, 3);
406 ieee80211_ht_announce(struct ieee80211com *ic)
408 struct ifnet *ifp = ic->ic_ifp;
410 if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) ||
411 isset(ic->ic_modecaps, IEEE80211_MODE_11NG))
412 if_printf(ifp, "%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 * Stop A-MPDU rx processing for the specified TID.
566 ampdu_rx_stop(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
570 rap->rxa_flags &= ~(IEEE80211_AGGR_RUNNING | IEEE80211_AGGR_XCHGPEND);
574 * Dispatch a frame from the A-MPDU reorder queue. The
575 * frame is fed back into ieee80211_input marked with an
576 * M_AMPDU_MPDU flag so it doesn't come back to us (it also
577 * permits ieee80211_input to optimize re-processing).
580 ampdu_dispatch(struct ieee80211_node *ni, struct mbuf *m)
582 m->m_flags |= M_AMPDU_MPDU; /* bypass normal processing */
583 /* NB: rssi and noise are ignored w/ M_AMPDU_MPDU set */
584 (void) ieee80211_input(ni, m, 0, 0);
588 * Dispatch as many frames as possible from the re-order queue.
589 * Frames will always be "at the front"; we process all frames
590 * up to the first empty slot in the window. On completion we
591 * cleanup state if there are still pending frames in the current
592 * BA window. We assume the frame at slot 0 is already handled
593 * by the caller; we always start at slot 1.
596 ampdu_rx_dispatch(struct ieee80211_rx_ampdu *rap, struct ieee80211_node *ni)
598 struct ieee80211vap *vap = ni->ni_vap;
602 /* flush run of frames */
603 for (i = 1; i < rap->rxa_wnd; i++) {
607 rap->rxa_m[i] = NULL;
608 rap->rxa_qbytes -= m->m_pkthdr.len;
611 ampdu_dispatch(ni, m);
614 * If frames remain, copy the mbuf pointers down so
615 * they correspond to the offsets in the new window.
617 if (rap->rxa_qframes != 0) {
618 int n = rap->rxa_qframes, j;
619 for (j = i+1; j < rap->rxa_wnd; j++) {
620 if (rap->rxa_m[j] != NULL) {
621 rap->rxa_m[j-i] = rap->rxa_m[j];
622 rap->rxa_m[j] = NULL;
627 KASSERT(n == 0, ("lost %d frames", n));
628 vap->iv_stats.is_ampdu_rx_copy += rap->rxa_qframes;
631 * Adjust the start of the BA window to
632 * reflect the frames just dispatched.
634 rap->rxa_start = IEEE80211_SEQ_ADD(rap->rxa_start, i);
635 vap->iv_stats.is_ampdu_rx_oor += i;
638 #ifdef IEEE80211_AMPDU_AGE
640 * Dispatch all frames in the A-MPDU re-order queue.
643 ampdu_rx_flush(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
645 struct ieee80211vap *vap = ni->ni_vap;
649 for (i = 0; i < rap->rxa_wnd; i++) {
653 rap->rxa_m[i] = NULL;
654 rap->rxa_qbytes -= m->m_pkthdr.len;
656 vap->iv_stats.is_ampdu_rx_oor++;
658 ampdu_dispatch(ni, m);
659 if (rap->rxa_qframes == 0)
663 #endif /* IEEE80211_AMPDU_AGE */
666 * Dispatch all frames in the A-MPDU re-order queue
667 * preceding the specified sequence number. This logic
668 * handles window moves due to a received MSDU or BAR.
671 ampdu_rx_flush_upto(struct ieee80211_node *ni,
672 struct ieee80211_rx_ampdu *rap, ieee80211_seq winstart)
674 struct ieee80211vap *vap = ni->ni_vap;
680 * Flush any complete MSDU's with a sequence number lower
681 * than winstart. Gaps may exist. Note that we may actually
682 * dispatch frames past winstart if a run continues; this is
683 * an optimization that avoids having to do a separate pass
684 * to dispatch frames after moving the BA window start.
686 seqno = rap->rxa_start;
687 for (i = 0; i < rap->rxa_wnd; i++) {
690 rap->rxa_m[i] = NULL;
691 rap->rxa_qbytes -= m->m_pkthdr.len;
693 vap->iv_stats.is_ampdu_rx_oor++;
695 ampdu_dispatch(ni, m);
697 if (!IEEE80211_SEQ_BA_BEFORE(seqno, winstart))
700 seqno = IEEE80211_SEQ_INC(seqno);
703 * If frames remain, copy the mbuf pointers down so
704 * they correspond to the offsets in the new window.
706 if (rap->rxa_qframes != 0) {
707 int n = rap->rxa_qframes, j;
709 /* NB: this loop assumes i > 0 and/or rxa_m[0] is NULL */
710 KASSERT(rap->rxa_m[0] == NULL,
711 ("%s: BA window slot 0 occupied", __func__));
712 for (j = i+1; j < rap->rxa_wnd; j++) {
713 if (rap->rxa_m[j] != NULL) {
714 rap->rxa_m[j-i] = rap->rxa_m[j];
715 rap->rxa_m[j] = NULL;
720 KASSERT(n == 0, ("%s: lost %d frames, qframes %d off %d "
721 "BA win <%d:%d> winstart %d",
722 __func__, n, rap->rxa_qframes, i, rap->rxa_start,
723 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
725 vap->iv_stats.is_ampdu_rx_copy += rap->rxa_qframes;
728 * Move the start of the BA window; we use the
729 * sequence number of the last MSDU that was
730 * passed up the stack+1 or winstart if stopped on
731 * a gap in the reorder buffer.
733 rap->rxa_start = seqno;
737 * Process a received QoS data frame for an HT station. Handle
738 * A-MPDU reordering: if this frame is received out of order
739 * and falls within the BA window hold onto it. Otherwise if
740 * this frame completes a run, flush any pending frames. We
741 * return 1 if the frame is consumed. A 0 is returned if
742 * the frame should be processed normally by the caller.
745 ieee80211_ampdu_reorder(struct ieee80211_node *ni, struct mbuf *m)
747 #define IEEE80211_FC0_QOSDATA \
748 (IEEE80211_FC0_TYPE_DATA|IEEE80211_FC0_SUBTYPE_QOS|IEEE80211_FC0_VERSION_0)
749 #define PROCESS 0 /* caller should process frame */
750 #define CONSUMED 1 /* frame consumed, caller does nothing */
751 struct ieee80211vap *vap = ni->ni_vap;
752 struct ieee80211_qosframe *wh;
753 struct ieee80211_rx_ampdu *rap;
758 KASSERT((m->m_flags & (M_AMPDU | M_AMPDU_MPDU)) == M_AMPDU,
759 ("!a-mpdu or already re-ordered, flags 0x%x", m->m_flags));
760 KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT sta"));
762 /* NB: m_len known to be sufficient */
763 wh = mtod(m, struct ieee80211_qosframe *);
764 if (wh->i_fc[0] != IEEE80211_FC0_QOSDATA) {
766 * Not QoS data, shouldn't get here but just
767 * return it to the caller for processing.
771 if (IEEE80211_IS_DSTODS(wh))
772 tid = ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0];
775 tid &= IEEE80211_QOS_TID;
776 rap = &ni->ni_rx_ampdu[tid];
777 if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
779 * No ADDBA request yet, don't touch.
783 rxseq = le16toh(*(uint16_t *)wh->i_seq);
784 if ((rxseq & IEEE80211_SEQ_FRAG_MASK) != 0) {
786 * Fragments are not allowed; toss.
788 IEEE80211_DISCARD_MAC(vap,
789 IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
790 "A-MPDU", "fragment, rxseq 0x%x tid %u%s", rxseq, tid,
791 wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
792 vap->iv_stats.is_ampdu_rx_drop++;
793 IEEE80211_NODE_STAT(ni, rx_drop);
797 rxseq >>= IEEE80211_SEQ_SEQ_SHIFT;
800 if (rxseq == rap->rxa_start) {
802 * First frame in window.
804 if (rap->rxa_qframes != 0) {
806 * Dispatch as many packets as we can.
808 KASSERT(rap->rxa_m[0] == NULL, ("unexpected dup"));
809 ampdu_dispatch(ni, m);
810 ampdu_rx_dispatch(rap, ni);
814 * In order; advance window and notify
815 * caller to dispatch directly.
817 rap->rxa_start = IEEE80211_SEQ_INC(rxseq);
822 * Frame is out of order; store if in the BA window.
824 /* calculate offset in BA window */
825 off = IEEE80211_SEQ_SUB(rxseq, rap->rxa_start);
826 if (off < rap->rxa_wnd) {
828 * Common case (hopefully): in the BA window.
829 * Sec 9.10.7.6.2 a) (p.137)
831 #ifdef IEEE80211_AMPDU_AGE
833 * Check for frames sitting too long in the reorder queue.
834 * This should only ever happen if frames are not delivered
835 * without the sender otherwise notifying us (e.g. with a
836 * BAR to move the window). Typically this happens because
837 * of vendor bugs that cause the sequence number to jump.
838 * When this happens we get a gap in the reorder queue that
839 * leaves frame sitting on the queue until they get pushed
840 * out due to window moves. When the vendor does not send
841 * BAR this move only happens due to explicit packet sends
843 * NB: we only track the time of the oldest frame in the
844 * reorder q; this means that if we flush we might push
845 * frames that still "new"; if this happens then subsequent
846 * frames will result in BA window moves which cost something
847 * but is still better than a big throughput dip.
849 if (rap->rxa_qframes != 0) {
850 /* XXX honor batimeout? */
851 if (ticks - rap->rxa_age > ieee80211_ampdu_age) {
853 * Too long since we received the first
854 * frame; flush the reorder buffer.
856 if (rap->rxa_qframes != 0) {
857 vap->iv_stats.is_ampdu_rx_age +=
859 ampdu_rx_flush(ni, rap);
861 rap->rxa_start = IEEE80211_SEQ_INC(rxseq);
866 * First frame, start aging timer.
868 rap->rxa_age = ticks;
870 #endif /* IEEE80211_AMPDU_AGE */
872 if (rap->rxa_m[off] == NULL) {
875 rap->rxa_qbytes += m->m_pkthdr.len;
876 vap->iv_stats.is_ampdu_rx_reorder++;
878 IEEE80211_DISCARD_MAC(vap,
879 IEEE80211_MSG_INPUT | IEEE80211_MSG_11N,
880 ni->ni_macaddr, "a-mpdu duplicate",
881 "seqno %u tid %u BA win <%u:%u>",
882 rxseq, tid, rap->rxa_start,
883 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1));
884 vap->iv_stats.is_rx_dup++;
885 IEEE80211_NODE_STAT(ni, rx_dup);
890 if (off < IEEE80211_SEQ_BA_RANGE) {
892 * Outside the BA window, but within range;
893 * flush the reorder q and move the window.
894 * Sec 9.10.7.6.2 b) (p.138)
896 IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
897 "move BA win <%u:%u> (%u frames) rxseq %u tid %u",
899 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
900 rap->rxa_qframes, rxseq, tid);
901 vap->iv_stats.is_ampdu_rx_move++;
904 * The spec says to flush frames up to but not including:
905 * WinStart_B = rxseq - rap->rxa_wnd + 1
906 * Then insert the frame or notify the caller to process
907 * it immediately. We can safely do this by just starting
908 * over again because we know the frame will now be within
911 /* NB: rxa_wnd known to be >0 */
912 ampdu_rx_flush_upto(ni, rap,
913 IEEE80211_SEQ_SUB(rxseq, rap->rxa_wnd-1));
917 * Outside the BA window and out of range; toss.
918 * Sec 9.10.7.6.2 c) (p.138)
920 IEEE80211_DISCARD_MAC(vap,
921 IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
922 "MPDU", "BA win <%u:%u> (%u frames) rxseq %u tid %u%s",
924 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
925 rap->rxa_qframes, rxseq, tid,
926 wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
927 vap->iv_stats.is_ampdu_rx_drop++;
928 IEEE80211_NODE_STAT(ni, rx_drop);
934 #undef IEEE80211_FC0_QOSDATA
938 * Process a BAR ctl frame. Dispatch all frames up to
939 * the sequence number of the frame. If this frame is
940 * out of range it's discarded.
943 ieee80211_recv_bar(struct ieee80211_node *ni, struct mbuf *m0)
945 struct ieee80211vap *vap = ni->ni_vap;
946 struct ieee80211_frame_bar *wh;
947 struct ieee80211_rx_ampdu *rap;
951 if (!ieee80211_recv_bar_ena) {
953 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_11N,
954 ni->ni_macaddr, "BAR", "%s", "processing disabled");
956 vap->iv_stats.is_ampdu_bar_bad++;
959 wh = mtod(m0, struct ieee80211_frame_bar *);
960 /* XXX check basic BAR */
961 tid = MS(le16toh(wh->i_ctl), IEEE80211_BAR_TID);
962 rap = &ni->ni_rx_ampdu[tid];
963 if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
965 * No ADDBA request yet, don't touch.
967 IEEE80211_DISCARD_MAC(vap,
968 IEEE80211_MSG_INPUT | IEEE80211_MSG_11N,
969 ni->ni_macaddr, "BAR", "no BA stream, tid %u", tid);
970 vap->iv_stats.is_ampdu_bar_bad++;
973 vap->iv_stats.is_ampdu_bar_rx++;
974 rxseq = le16toh(wh->i_seq) >> IEEE80211_SEQ_SEQ_SHIFT;
975 if (rxseq == rap->rxa_start)
977 /* calculate offset in BA window */
978 off = IEEE80211_SEQ_SUB(rxseq, rap->rxa_start);
979 if (off < IEEE80211_SEQ_BA_RANGE) {
981 * Flush the reorder q up to rxseq and move the window.
982 * Sec 9.10.7.6.3 a) (p.138)
984 IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
985 "BAR moves BA win <%u:%u> (%u frames) rxseq %u tid %u",
987 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
988 rap->rxa_qframes, rxseq, tid);
989 vap->iv_stats.is_ampdu_bar_move++;
991 ampdu_rx_flush_upto(ni, rap, rxseq);
992 if (off >= rap->rxa_wnd) {
994 * BAR specifies a window start to the right of BA
995 * window; we must move it explicitly since
996 * ampdu_rx_flush_upto will not.
998 rap->rxa_start = rxseq;
1002 * Out of range; toss.
1003 * Sec 9.10.7.6.3 b) (p.138)
1005 IEEE80211_DISCARD_MAC(vap,
1006 IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
1007 "BAR", "BA win <%u:%u> (%u frames) rxseq %u tid %u%s",
1009 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
1010 rap->rxa_qframes, rxseq, tid,
1011 wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
1012 vap->iv_stats.is_ampdu_bar_oow++;
1013 IEEE80211_NODE_STAT(ni, rx_drop);
1018 * Setup HT-specific state in a node. Called only
1019 * when HT use is negotiated so we don't do extra
1020 * work for temporary and/or legacy sta's.
1023 ieee80211_ht_node_init(struct ieee80211_node *ni)
1025 struct ieee80211_tx_ampdu *tap;
1028 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
1033 if (ni->ni_flags & IEEE80211_NODE_HT) {
1035 * Clean AMPDU state on re-associate. This handles the case
1036 * where a station leaves w/o notifying us and then returns
1037 * before node is reaped for inactivity.
1039 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
1041 "%s: calling cleanup",
1043 ieee80211_ht_node_cleanup(ni);
1045 for (tid = 0; tid < WME_NUM_TID; tid++) {
1046 tap = &ni->ni_tx_ampdu[tid];
1049 /* NB: further initialization deferred */
1051 ni->ni_flags |= IEEE80211_NODE_HT | IEEE80211_NODE_AMPDU;
1055 * Cleanup HT-specific state in a node. Called only
1056 * when HT use has been marked.
1059 ieee80211_ht_node_cleanup(struct ieee80211_node *ni)
1061 struct ieee80211com *ic = ni->ni_ic;
1064 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
1069 KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT node"));
1071 /* XXX optimize this */
1072 for (i = 0; i < WME_NUM_TID; i++) {
1073 struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[i];
1074 if (tap->txa_flags & IEEE80211_AGGR_SETUP)
1077 for (i = 0; i < WME_NUM_TID; i++)
1078 ic->ic_ampdu_rx_stop(ni, &ni->ni_rx_ampdu[i]);
1081 ni->ni_flags &= ~IEEE80211_NODE_HT_ALL;
1085 * Age out HT resources for a station.
1088 ieee80211_ht_node_age(struct ieee80211_node *ni)
1090 #ifdef IEEE80211_AMPDU_AGE
1091 struct ieee80211vap *vap = ni->ni_vap;
1095 KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT sta"));
1097 #ifdef IEEE80211_AMPDU_AGE
1098 for (tid = 0; tid < WME_NUM_TID; tid++) {
1099 struct ieee80211_rx_ampdu *rap;
1101 rap = &ni->ni_rx_ampdu[tid];
1102 if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0)
1104 if (rap->rxa_qframes == 0)
1107 * Check for frames sitting too long in the reorder queue.
1108 * See above for more details on what's happening here.
1110 /* XXX honor batimeout? */
1111 if (ticks - rap->rxa_age > ieee80211_ampdu_age) {
1113 * Too long since we received the first
1114 * frame; flush the reorder buffer.
1116 vap->iv_stats.is_ampdu_rx_age += rap->rxa_qframes;
1117 ampdu_rx_flush(ni, rap);
1120 #endif /* IEEE80211_AMPDU_AGE */
1123 static struct ieee80211_channel *
1124 findhtchan(struct ieee80211com *ic, struct ieee80211_channel *c, int htflags)
1126 return ieee80211_find_channel(ic, c->ic_freq,
1127 (c->ic_flags &~ IEEE80211_CHAN_HT) | htflags);
1131 * Adjust a channel to be HT/non-HT according to the vap's configuration.
1133 struct ieee80211_channel *
1134 ieee80211_ht_adjust_channel(struct ieee80211com *ic,
1135 struct ieee80211_channel *chan, int flags)
1137 struct ieee80211_channel *c;
1139 if (flags & IEEE80211_FHT_HT) {
1140 /* promote to HT if possible */
1141 if (flags & IEEE80211_FHT_USEHT40) {
1142 if (!IEEE80211_IS_CHAN_HT40(chan)) {
1143 /* NB: arbitrarily pick ht40+ over ht40- */
1144 c = findhtchan(ic, chan, IEEE80211_CHAN_HT40U);
1146 c = findhtchan(ic, chan,
1147 IEEE80211_CHAN_HT40D);
1149 c = findhtchan(ic, chan,
1150 IEEE80211_CHAN_HT20);
1154 } else if (!IEEE80211_IS_CHAN_HT20(chan)) {
1155 c = findhtchan(ic, chan, IEEE80211_CHAN_HT20);
1159 } else if (IEEE80211_IS_CHAN_HT(chan)) {
1160 /* demote to legacy, HT use is disabled */
1161 c = ieee80211_find_channel(ic, chan->ic_freq,
1162 chan->ic_flags &~ IEEE80211_CHAN_HT);
1170 * Setup HT-specific state for a legacy WDS peer.
1173 ieee80211_ht_wds_init(struct ieee80211_node *ni)
1175 struct ieee80211vap *vap = ni->ni_vap;
1176 struct ieee80211_tx_ampdu *tap;
1179 KASSERT(vap->iv_flags_ht & IEEE80211_FHT_HT, ("no HT requested"));
1181 /* XXX check scan cache in case peer has an ap and we have info */
1183 * If setup with a legacy channel; locate an HT channel.
1184 * Otherwise if the inherited channel (from a companion
1185 * AP) is suitable use it so we use the same location
1186 * for the extension channel).
1188 ni->ni_chan = ieee80211_ht_adjust_channel(ni->ni_ic,
1189 ni->ni_chan, ieee80211_htchanflags(ni->ni_chan));
1192 if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20)
1193 ni->ni_htcap |= IEEE80211_HTCAP_SHORTGI20;
1194 if (IEEE80211_IS_CHAN_HT40(ni->ni_chan)) {
1195 ni->ni_htcap |= IEEE80211_HTCAP_CHWIDTH40;
1197 if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan))
1198 ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_ABOVE;
1199 else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan))
1200 ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_BELOW;
1201 if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40)
1202 ni->ni_htcap |= IEEE80211_HTCAP_SHORTGI40;
1205 ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_NONE;
1207 ni->ni_htctlchan = ni->ni_chan->ic_ieee;
1208 if (vap->iv_flags_ht & IEEE80211_FHT_RIFS)
1209 ni->ni_flags |= IEEE80211_NODE_RIFS;
1210 /* XXX does it make sense to enable SMPS? */
1212 ni->ni_htopmode = 0; /* XXX need protection state */
1213 ni->ni_htstbc = 0; /* XXX need info */
1215 for (tid = 0; tid < WME_NUM_TID; tid++) {
1216 tap = &ni->ni_tx_ampdu[tid];
1219 /* NB: AMPDU tx/rx governed by IEEE80211_FHT_AMPDU_{TX,RX} */
1220 ni->ni_flags |= IEEE80211_NODE_HT | IEEE80211_NODE_AMPDU;
1224 * Notify hostap vaps of a change in the HTINFO ie.
1227 htinfo_notify(struct ieee80211com *ic)
1229 struct ieee80211vap *vap;
1232 IEEE80211_LOCK_ASSERT(ic);
1234 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1235 if (vap->iv_opmode != IEEE80211_M_HOSTAP)
1237 if (vap->iv_state != IEEE80211_S_RUN ||
1238 !IEEE80211_IS_CHAN_HT(vap->iv_bss->ni_chan))
1242 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N,
1244 "HT bss occupancy change: %d sta, %d ht, "
1245 "%d ht40%s, HT protmode now 0x%x"
1247 , ic->ic_ht_sta_assoc
1248 , ic->ic_ht40_sta_assoc
1249 , (ic->ic_flags_ht & IEEE80211_FHT_NONHT_PR) ?
1250 ", non-HT sta present" : ""
1251 , ic->ic_curhtprotmode);
1254 ieee80211_beacon_notify(vap, IEEE80211_BEACON_HTINFO);
1259 * Calculate HT protection mode from current
1260 * state and handle updates.
1263 htinfo_update(struct ieee80211com *ic)
1267 if (ic->ic_sta_assoc != ic->ic_ht_sta_assoc) {
1268 protmode = IEEE80211_HTINFO_OPMODE_MIXED
1269 | IEEE80211_HTINFO_NONHT_PRESENT;
1270 } else if (ic->ic_flags_ht & IEEE80211_FHT_NONHT_PR) {
1271 protmode = IEEE80211_HTINFO_OPMODE_PROTOPT
1272 | IEEE80211_HTINFO_NONHT_PRESENT;
1273 } else if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
1274 IEEE80211_IS_CHAN_HT40(ic->ic_bsschan) &&
1275 ic->ic_sta_assoc != ic->ic_ht40_sta_assoc) {
1276 protmode = IEEE80211_HTINFO_OPMODE_HT20PR;
1278 protmode = IEEE80211_HTINFO_OPMODE_PURE;
1280 if (protmode != ic->ic_curhtprotmode) {
1281 ic->ic_curhtprotmode = protmode;
1287 * Handle an HT station joining a BSS.
1290 ieee80211_ht_node_join(struct ieee80211_node *ni)
1292 struct ieee80211com *ic = ni->ni_ic;
1294 IEEE80211_LOCK_ASSERT(ic);
1296 if (ni->ni_flags & IEEE80211_NODE_HT) {
1297 ic->ic_ht_sta_assoc++;
1298 if (ni->ni_chw == 40)
1299 ic->ic_ht40_sta_assoc++;
1305 * Handle an HT station leaving a BSS.
1308 ieee80211_ht_node_leave(struct ieee80211_node *ni)
1310 struct ieee80211com *ic = ni->ni_ic;
1312 IEEE80211_LOCK_ASSERT(ic);
1314 if (ni->ni_flags & IEEE80211_NODE_HT) {
1315 ic->ic_ht_sta_assoc--;
1316 if (ni->ni_chw == 40)
1317 ic->ic_ht40_sta_assoc--;
1323 * Public version of htinfo_update; used for processing
1324 * beacon frames from overlapping bss.
1326 * Caller can specify either IEEE80211_HTINFO_OPMODE_MIXED
1327 * (on receipt of a beacon that advertises MIXED) or
1328 * IEEE80211_HTINFO_OPMODE_PROTOPT (on receipt of a beacon
1329 * from an overlapping legacy bss). We treat MIXED with
1330 * a higher precedence than PROTOPT (i.e. we will not change
1331 * change PROTOPT -> MIXED; only MIXED -> PROTOPT). This
1332 * corresponds to how we handle things in htinfo_update.
1335 ieee80211_htprot_update(struct ieee80211com *ic, int protmode)
1337 #define OPMODE(x) SM(x, IEEE80211_HTINFO_OPMODE)
1340 /* track non-HT station presence */
1341 KASSERT(protmode & IEEE80211_HTINFO_NONHT_PRESENT,
1342 ("protmode 0x%x", protmode));
1343 ic->ic_flags_ht |= IEEE80211_FHT_NONHT_PR;
1344 ic->ic_lastnonht = ticks;
1346 if (protmode != ic->ic_curhtprotmode &&
1347 (OPMODE(ic->ic_curhtprotmode) != IEEE80211_HTINFO_OPMODE_MIXED ||
1348 OPMODE(protmode) == IEEE80211_HTINFO_OPMODE_PROTOPT)) {
1349 /* push beacon update */
1350 ic->ic_curhtprotmode = protmode;
1353 IEEE80211_UNLOCK(ic);
1358 * Time out presence of an overlapping bss with non-HT
1359 * stations. When operating in hostap mode we listen for
1360 * beacons from other stations and if we identify a non-HT
1361 * station is present we update the opmode field of the
1362 * HTINFO ie. To identify when all non-HT stations are
1363 * gone we time out this condition.
1366 ieee80211_ht_timeout(struct ieee80211com *ic)
1368 IEEE80211_LOCK_ASSERT(ic);
1370 if ((ic->ic_flags_ht & IEEE80211_FHT_NONHT_PR) &&
1371 time_after(ticks, ic->ic_lastnonht + IEEE80211_NONHT_PRESENT_AGE)) {
1373 IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
1374 "%s", "time out non-HT STA present on channel");
1376 ic->ic_flags_ht &= ~IEEE80211_FHT_NONHT_PR;
1381 /* unalligned little endian access */
1382 #define LE_READ_2(p) \
1384 ((((const uint8_t *)(p))[0] ) | \
1385 (((const uint8_t *)(p))[1] << 8)))
1388 * Process an 802.11n HT capabilities ie.
1391 ieee80211_parse_htcap(struct ieee80211_node *ni, const uint8_t *ie)
1393 if (ie[0] == IEEE80211_ELEMID_VENDOR) {
1395 * Station used Vendor OUI ie to associate;
1396 * mark the node so when we respond we'll use
1397 * the Vendor OUI's and not the standard ie's.
1399 ni->ni_flags |= IEEE80211_NODE_HTCOMPAT;
1402 ni->ni_flags &= ~IEEE80211_NODE_HTCOMPAT;
1404 ni->ni_htcap = LE_READ_2(ie +
1405 __offsetof(struct ieee80211_ie_htcap, hc_cap));
1406 ni->ni_htparam = ie[__offsetof(struct ieee80211_ie_htcap, hc_param)];
1410 htinfo_parse(struct ieee80211_node *ni,
1411 const struct ieee80211_ie_htinfo *htinfo)
1415 ni->ni_htctlchan = htinfo->hi_ctrlchannel;
1416 ni->ni_ht2ndchan = SM(htinfo->hi_byte1, IEEE80211_HTINFO_2NDCHAN);
1417 w = LE_READ_2(&htinfo->hi_byte2);
1418 ni->ni_htopmode = SM(w, IEEE80211_HTINFO_OPMODE);
1419 w = LE_READ_2(&htinfo->hi_byte45);
1420 ni->ni_htstbc = SM(w, IEEE80211_HTINFO_BASIC_STBCMCS);
1424 * Parse an 802.11n HT info ie and save useful information
1425 * to the node state. Note this does not effect any state
1426 * changes such as for channel width change.
1429 ieee80211_parse_htinfo(struct ieee80211_node *ni, const uint8_t *ie)
1431 if (ie[0] == IEEE80211_ELEMID_VENDOR)
1433 htinfo_parse(ni, (const struct ieee80211_ie_htinfo *) ie);
1437 * Handle 11n channel switch. Use the received HT ie's to
1438 * identify the right channel to use. If we cannot locate it
1439 * in the channel table then fallback to legacy operation.
1440 * Note that we use this information to identify the node's
1441 * channel only; the caller is responsible for insuring any
1442 * required channel change is done (e.g. in sta mode when
1443 * parsing the contents of a beacon frame).
1446 htinfo_update_chw(struct ieee80211_node *ni, int htflags)
1448 struct ieee80211com *ic = ni->ni_ic;
1449 struct ieee80211_channel *c;
1453 chanflags = (ni->ni_chan->ic_flags &~ IEEE80211_CHAN_HT) | htflags;
1454 if (chanflags != ni->ni_chan->ic_flags) {
1455 /* XXX not right for ht40- */
1456 c = ieee80211_find_channel(ic, ni->ni_chan->ic_freq, chanflags);
1457 if (c == NULL && (htflags & IEEE80211_CHAN_HT40)) {
1459 * No HT40 channel entry in our table; fall back
1460 * to HT20 operation. This should not happen.
1462 c = findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT20);
1464 IEEE80211_NOTE(ni->ni_vap,
1465 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
1466 "no HT40 channel (freq %u), falling back to HT20",
1467 ni->ni_chan->ic_freq);
1471 if (c != NULL && c != ni->ni_chan) {
1472 IEEE80211_NOTE(ni->ni_vap,
1473 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
1474 "switch station to HT%d channel %u/0x%x",
1475 IEEE80211_IS_CHAN_HT40(c) ? 40 : 20,
1476 c->ic_freq, c->ic_flags);
1480 /* NB: caller responsible for forcing any channel change */
1482 /* update node's tx channel width */
1483 ni->ni_chw = IEEE80211_IS_CHAN_HT40(ni->ni_chan)? 40 : 20;
1488 * Update 11n MIMO PS state according to received htcap.
1491 htcap_update_mimo_ps(struct ieee80211_node *ni)
1493 uint16_t oflags = ni->ni_flags;
1495 switch (ni->ni_htcap & IEEE80211_HTCAP_SMPS) {
1496 case IEEE80211_HTCAP_SMPS_DYNAMIC:
1497 ni->ni_flags |= IEEE80211_NODE_MIMO_PS;
1498 ni->ni_flags |= IEEE80211_NODE_MIMO_RTS;
1500 case IEEE80211_HTCAP_SMPS_ENA:
1501 ni->ni_flags |= IEEE80211_NODE_MIMO_PS;
1502 ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS;
1504 case IEEE80211_HTCAP_SMPS_OFF:
1505 default: /* disable on rx of reserved value */
1506 ni->ni_flags &= ~IEEE80211_NODE_MIMO_PS;
1507 ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS;
1510 return (oflags ^ ni->ni_flags);
1514 * Update short GI state according to received htcap
1515 * and local settings.
1517 static __inline void
1518 htcap_update_shortgi(struct ieee80211_node *ni)
1520 struct ieee80211vap *vap = ni->ni_vap;
1522 ni->ni_flags &= ~(IEEE80211_NODE_SGI20|IEEE80211_NODE_SGI40);
1523 if ((ni->ni_htcap & IEEE80211_HTCAP_SHORTGI20) &&
1524 (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20))
1525 ni->ni_flags |= IEEE80211_NODE_SGI20;
1526 if ((ni->ni_htcap & IEEE80211_HTCAP_SHORTGI40) &&
1527 (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40))
1528 ni->ni_flags |= IEEE80211_NODE_SGI40;
1532 * Parse and update HT-related state extracted from
1533 * the HT cap and info ie's.
1536 ieee80211_ht_updateparams(struct ieee80211_node *ni,
1537 const uint8_t *htcapie, const uint8_t *htinfoie)
1539 struct ieee80211vap *vap = ni->ni_vap;
1540 const struct ieee80211_ie_htinfo *htinfo;
1544 ieee80211_parse_htcap(ni, htcapie);
1545 if (vap->iv_htcaps & IEEE80211_HTCAP_SMPS)
1546 htcap_update_mimo_ps(ni);
1547 htcap_update_shortgi(ni);
1549 if (htinfoie[0] == IEEE80211_ELEMID_VENDOR)
1551 htinfo = (const struct ieee80211_ie_htinfo *) htinfoie;
1552 htinfo_parse(ni, htinfo);
1554 htflags = (vap->iv_flags_ht & IEEE80211_FHT_HT) ?
1555 IEEE80211_CHAN_HT20 : 0;
1556 /* NB: honor operating mode constraint */
1557 if ((htinfo->hi_byte1 & IEEE80211_HTINFO_TXWIDTH_2040) &&
1558 (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)) {
1559 if (ni->ni_ht2ndchan == IEEE80211_HTINFO_2NDCHAN_ABOVE)
1560 htflags = IEEE80211_CHAN_HT40U;
1561 else if (ni->ni_ht2ndchan == IEEE80211_HTINFO_2NDCHAN_BELOW)
1562 htflags = IEEE80211_CHAN_HT40D;
1564 if (htinfo_update_chw(ni, htflags))
1567 if ((htinfo->hi_byte1 & IEEE80211_HTINFO_RIFSMODE_PERM) &&
1568 (vap->iv_flags_ht & IEEE80211_FHT_RIFS))
1569 ni->ni_flags |= IEEE80211_NODE_RIFS;
1571 ni->ni_flags &= ~IEEE80211_NODE_RIFS;
1577 * Parse and update HT-related state extracted from the HT cap ie
1578 * for a station joining an HT BSS.
1581 ieee80211_ht_updatehtcap(struct ieee80211_node *ni, const uint8_t *htcapie)
1583 struct ieee80211vap *vap = ni->ni_vap;
1586 ieee80211_parse_htcap(ni, htcapie);
1587 if (vap->iv_htcaps & IEEE80211_HTCAP_SMPS)
1588 htcap_update_mimo_ps(ni);
1589 htcap_update_shortgi(ni);
1591 /* NB: honor operating mode constraint */
1592 /* XXX 40 MHz intolerant */
1593 htflags = (vap->iv_flags_ht & IEEE80211_FHT_HT) ?
1594 IEEE80211_CHAN_HT20 : 0;
1595 if ((ni->ni_htcap & IEEE80211_HTCAP_CHWIDTH40) &&
1596 (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)) {
1597 if (IEEE80211_IS_CHAN_HT40U(vap->iv_bss->ni_chan))
1598 htflags = IEEE80211_CHAN_HT40U;
1599 else if (IEEE80211_IS_CHAN_HT40D(vap->iv_bss->ni_chan))
1600 htflags = IEEE80211_CHAN_HT40D;
1602 (void) htinfo_update_chw(ni, htflags);
1606 * Install received HT rate set by parsing the HT cap ie.
1609 ieee80211_setup_htrates(struct ieee80211_node *ni, const uint8_t *ie, int flags)
1611 struct ieee80211com *ic = ni->ni_ic;
1612 struct ieee80211vap *vap = ni->ni_vap;
1613 const struct ieee80211_ie_htcap *htcap;
1614 struct ieee80211_htrateset *rs;
1615 int i, maxequalmcs, maxunequalmcs;
1617 maxequalmcs = ic->ic_txstream * 8 - 1;
1618 if (ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL) {
1619 if (ic->ic_txstream >= 2)
1621 if (ic->ic_txstream >= 3)
1623 if (ic->ic_txstream >= 4)
1628 rs = &ni->ni_htrates;
1629 memset(rs, 0, sizeof(*rs));
1631 if (ie[0] == IEEE80211_ELEMID_VENDOR)
1633 htcap = (const struct ieee80211_ie_htcap *) ie;
1634 for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++) {
1635 if (isclr(htcap->hc_mcsset, i))
1637 if (rs->rs_nrates == IEEE80211_HTRATE_MAXSIZE) {
1639 IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
1640 "WARNING, HT rate set too large; only "
1641 "using %u rates", IEEE80211_HTRATE_MAXSIZE);
1642 vap->iv_stats.is_rx_rstoobig++;
1645 if (i <= 31 && i > maxequalmcs)
1648 (ic->ic_htcaps & IEEE80211_HTC_TXMCS32) == 0)
1650 if (i > 32 && i > maxunequalmcs)
1652 rs->rs_rates[rs->rs_nrates++] = i;
1655 return ieee80211_fix_rate(ni, (struct ieee80211_rateset *) rs, flags);
1659 * Mark rates in a node's HT rate set as basic according
1660 * to the information in the supplied HT info ie.
1663 ieee80211_setup_basic_htrates(struct ieee80211_node *ni, const uint8_t *ie)
1665 const struct ieee80211_ie_htinfo *htinfo;
1666 struct ieee80211_htrateset *rs;
1669 if (ie[0] == IEEE80211_ELEMID_VENDOR)
1671 htinfo = (const struct ieee80211_ie_htinfo *) ie;
1672 rs = &ni->ni_htrates;
1673 if (rs->rs_nrates == 0) {
1674 IEEE80211_NOTE(ni->ni_vap,
1675 IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
1676 "%s", "WARNING, empty HT rate set");
1679 for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++) {
1680 if (isclr(htinfo->hi_basicmcsset, i))
1682 for (j = 0; j < rs->rs_nrates; j++)
1683 if ((rs->rs_rates[j] & IEEE80211_RATE_VAL) == i)
1684 rs->rs_rates[j] |= IEEE80211_RATE_BASIC;
1689 ampdu_tx_setup(struct ieee80211_tx_ampdu *tap)
1691 callout_init(&tap->txa_timer, CALLOUT_MPSAFE);
1692 tap->txa_flags |= IEEE80211_AGGR_SETUP;
1696 ampdu_tx_stop(struct ieee80211_tx_ampdu *tap)
1698 struct ieee80211_node *ni = tap->txa_ni;
1699 struct ieee80211com *ic = ni->ni_ic;
1701 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
1706 KASSERT(tap->txa_flags & IEEE80211_AGGR_SETUP,
1707 ("txa_flags 0x%x tid %d ac %d", tap->txa_flags, tap->txa_tid,
1708 TID_TO_WME_AC(tap->txa_tid)));
1711 * Stop BA stream if setup so driver has a chance
1712 * to reclaim any resources it might have allocated.
1714 ic->ic_addba_stop(ni, tap);
1716 * Stop any pending BAR transmit.
1718 bar_stop_timer(tap);
1720 tap->txa_lastsample = 0;
1721 tap->txa_avgpps = 0;
1722 /* NB: clearing NAK means we may re-send ADDBA */
1723 tap->txa_flags &= ~(IEEE80211_AGGR_SETUP | IEEE80211_AGGR_NAK);
1727 * ADDBA response timeout.
1729 * If software aggregation and per-TID queue management was done here,
1730 * that queue would be unpaused after the ADDBA timeout occurs.
1733 addba_timeout(void *arg)
1735 struct ieee80211_tx_ampdu *tap = arg;
1736 struct ieee80211_node *ni = tap->txa_ni;
1737 struct ieee80211com *ic = ni->ni_ic;
1740 tap->txa_flags &= ~IEEE80211_AGGR_XCHGPEND;
1741 tap->txa_attempts++;
1742 ic->ic_addba_response_timeout(ni, tap);
1746 addba_start_timeout(struct ieee80211_tx_ampdu *tap)
1748 /* XXX use CALLOUT_PENDING instead? */
1749 callout_reset(&tap->txa_timer, ieee80211_addba_timeout,
1750 addba_timeout, tap);
1751 tap->txa_flags |= IEEE80211_AGGR_XCHGPEND;
1752 tap->txa_nextrequest = ticks + ieee80211_addba_timeout;
1756 addba_stop_timeout(struct ieee80211_tx_ampdu *tap)
1758 /* XXX use CALLOUT_PENDING instead? */
1759 if (tap->txa_flags & IEEE80211_AGGR_XCHGPEND) {
1760 callout_stop(&tap->txa_timer);
1761 tap->txa_flags &= ~IEEE80211_AGGR_XCHGPEND;
1766 null_addba_response_timeout(struct ieee80211_node *ni,
1767 struct ieee80211_tx_ampdu *tap)
1772 * Default method for requesting A-MPDU tx aggregation.
1773 * We setup the specified state block and start a timer
1774 * to wait for an ADDBA response frame.
1777 ieee80211_addba_request(struct ieee80211_node *ni,
1778 struct ieee80211_tx_ampdu *tap,
1779 int dialogtoken, int baparamset, int batimeout)
1784 tap->txa_token = dialogtoken;
1785 tap->txa_flags |= IEEE80211_AGGR_IMMEDIATE;
1786 bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
1787 tap->txa_wnd = (bufsiz == 0) ?
1788 IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
1789 addba_start_timeout(tap);
1794 * Default method for processing an A-MPDU tx aggregation
1795 * response. We shutdown any pending timer and update the
1796 * state block according to the reply.
1799 ieee80211_addba_response(struct ieee80211_node *ni,
1800 struct ieee80211_tx_ampdu *tap,
1801 int status, int baparamset, int batimeout)
1806 addba_stop_timeout(tap);
1807 if (status == IEEE80211_STATUS_SUCCESS) {
1808 bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
1809 /* XXX override our request? */
1810 tap->txa_wnd = (bufsiz == 0) ?
1811 IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
1813 tid = MS(baparamset, IEEE80211_BAPS_TID);
1814 tap->txa_flags |= IEEE80211_AGGR_RUNNING;
1815 tap->txa_attempts = 0;
1817 /* mark tid so we don't try again */
1818 tap->txa_flags |= IEEE80211_AGGR_NAK;
1824 * Default method for stopping A-MPDU tx aggregation.
1825 * Any timer is cleared and we drain any pending frames.
1828 ieee80211_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
1831 addba_stop_timeout(tap);
1832 if (tap->txa_flags & IEEE80211_AGGR_RUNNING) {
1833 /* XXX clear aggregation queue */
1834 tap->txa_flags &= ~IEEE80211_AGGR_RUNNING;
1836 tap->txa_attempts = 0;
1840 * Process a received action frame using the default aggregation
1841 * policy. We intercept ADDBA-related frames and use them to
1842 * update our aggregation state. All other frames are passed up
1843 * for processing by ieee80211_recv_action.
1846 ht_recv_action_ba_addba_request(struct ieee80211_node *ni,
1847 const struct ieee80211_frame *wh,
1848 const uint8_t *frm, const uint8_t *efrm)
1850 struct ieee80211com *ic = ni->ni_ic;
1851 struct ieee80211vap *vap = ni->ni_vap;
1852 struct ieee80211_rx_ampdu *rap;
1853 uint8_t dialogtoken;
1854 uint16_t baparamset, batimeout, baseqctl;
1858 dialogtoken = frm[2];
1859 baparamset = LE_READ_2(frm+3);
1860 batimeout = LE_READ_2(frm+5);
1861 baseqctl = LE_READ_2(frm+7);
1863 tid = MS(baparamset, IEEE80211_BAPS_TID);
1865 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1866 "recv ADDBA request: dialogtoken %u baparamset 0x%x "
1867 "(tid %d bufsiz %d) batimeout %d baseqctl %d:%d",
1868 dialogtoken, baparamset,
1869 tid, MS(baparamset, IEEE80211_BAPS_BUFSIZ),
1871 MS(baseqctl, IEEE80211_BASEQ_START),
1872 MS(baseqctl, IEEE80211_BASEQ_FRAG));
1874 rap = &ni->ni_rx_ampdu[tid];
1876 /* Send ADDBA response */
1877 args[0] = dialogtoken;
1879 * NB: We ack only if the sta associated with HT and
1880 * the ap is configured to do AMPDU rx (the latter
1881 * violates the 11n spec and is mostly for testing).
1883 if ((ni->ni_flags & IEEE80211_NODE_AMPDU_RX) &&
1884 (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_RX)) {
1885 /* XXX handle ampdu_rx_start failure */
1886 ic->ic_ampdu_rx_start(ni, rap,
1887 baparamset, batimeout, baseqctl);
1889 args[1] = IEEE80211_STATUS_SUCCESS;
1891 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1892 ni, "reject ADDBA request: %s",
1893 ni->ni_flags & IEEE80211_NODE_AMPDU_RX ?
1894 "administratively disabled" :
1895 "not negotiated for station");
1896 vap->iv_stats.is_addba_reject++;
1897 args[1] = IEEE80211_STATUS_UNSPECIFIED;
1899 /* XXX honor rap flags? */
1900 args[2] = IEEE80211_BAPS_POLICY_IMMEDIATE
1901 | SM(tid, IEEE80211_BAPS_TID)
1902 | SM(rap->rxa_wnd, IEEE80211_BAPS_BUFSIZ)
1906 ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
1907 IEEE80211_ACTION_BA_ADDBA_RESPONSE, args);
1912 ht_recv_action_ba_addba_response(struct ieee80211_node *ni,
1913 const struct ieee80211_frame *wh,
1914 const uint8_t *frm, const uint8_t *efrm)
1916 struct ieee80211com *ic = ni->ni_ic;
1917 struct ieee80211vap *vap = ni->ni_vap;
1918 struct ieee80211_tx_ampdu *tap;
1919 uint8_t dialogtoken, policy;
1920 uint16_t baparamset, batimeout, code;
1923 dialogtoken = frm[2];
1924 code = LE_READ_2(frm+3);
1925 baparamset = LE_READ_2(frm+5);
1926 tid = MS(baparamset, IEEE80211_BAPS_TID);
1927 bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
1928 policy = MS(baparamset, IEEE80211_BAPS_POLICY);
1929 batimeout = LE_READ_2(frm+7);
1931 tap = &ni->ni_tx_ampdu[tid];
1932 if ((tap->txa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
1933 IEEE80211_DISCARD_MAC(vap,
1934 IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1935 ni->ni_macaddr, "ADDBA response",
1936 "no pending ADDBA, tid %d dialogtoken %u "
1937 "code %d", tid, dialogtoken, code);
1938 vap->iv_stats.is_addba_norequest++;
1941 if (dialogtoken != tap->txa_token) {
1942 IEEE80211_DISCARD_MAC(vap,
1943 IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1944 ni->ni_macaddr, "ADDBA response",
1945 "dialogtoken mismatch: waiting for %d, "
1946 "received %d, tid %d code %d",
1947 tap->txa_token, dialogtoken, tid, code);
1948 vap->iv_stats.is_addba_badtoken++;
1951 /* NB: assumes IEEE80211_AGGR_IMMEDIATE is 1 */
1952 if (policy != (tap->txa_flags & IEEE80211_AGGR_IMMEDIATE)) {
1953 IEEE80211_DISCARD_MAC(vap,
1954 IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1955 ni->ni_macaddr, "ADDBA response",
1956 "policy mismatch: expecting %s, "
1957 "received %s, tid %d code %d",
1958 tap->txa_flags & IEEE80211_AGGR_IMMEDIATE,
1960 vap->iv_stats.is_addba_badpolicy++;
1964 /* XXX we take MIN in ieee80211_addba_response */
1965 if (bufsiz > IEEE80211_AGGR_BAWMAX) {
1966 IEEE80211_DISCARD_MAC(vap,
1967 IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1968 ni->ni_macaddr, "ADDBA response",
1969 "BA window too large: max %d, "
1970 "received %d, tid %d code %d",
1971 bufsiz, IEEE80211_AGGR_BAWMAX, tid, code);
1972 vap->iv_stats.is_addba_badbawinsize++;
1976 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1977 "recv ADDBA response: dialogtoken %u code %d "
1978 "baparamset 0x%x (tid %d bufsiz %d) batimeout %d",
1979 dialogtoken, code, baparamset, tid, bufsiz,
1981 ic->ic_addba_response(ni, tap, code, baparamset, batimeout);
1986 ht_recv_action_ba_delba(struct ieee80211_node *ni,
1987 const struct ieee80211_frame *wh,
1988 const uint8_t *frm, const uint8_t *efrm)
1990 struct ieee80211com *ic = ni->ni_ic;
1991 struct ieee80211_rx_ampdu *rap;
1992 struct ieee80211_tx_ampdu *tap;
1993 uint16_t baparamset, code;
1996 baparamset = LE_READ_2(frm+2);
1997 code = LE_READ_2(frm+4);
1999 tid = MS(baparamset, IEEE80211_DELBAPS_TID);
2001 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2002 "recv DELBA: baparamset 0x%x (tid %d initiator %d) "
2003 "code %d", baparamset, tid,
2004 MS(baparamset, IEEE80211_DELBAPS_INIT), code);
2006 if ((baparamset & IEEE80211_DELBAPS_INIT) == 0) {
2007 tap = &ni->ni_tx_ampdu[tid];
2008 ic->ic_addba_stop(ni, tap);
2010 rap = &ni->ni_rx_ampdu[tid];
2011 ic->ic_ampdu_rx_stop(ni, rap);
2017 ht_recv_action_ht_txchwidth(struct ieee80211_node *ni,
2018 const struct ieee80211_frame *wh,
2019 const uint8_t *frm, const uint8_t *efrm)
2023 chw = (frm[2] == IEEE80211_A_HT_TXCHWIDTH_2040) ? 40 : 20;
2025 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2026 "%s: HT txchwidth, width %d%s",
2027 __func__, chw, ni->ni_chw != chw ? "*" : "");
2028 if (chw != ni->ni_chw) {
2030 /* XXX notify on change */
2036 ht_recv_action_ht_mimopwrsave(struct ieee80211_node *ni,
2037 const struct ieee80211_frame *wh,
2038 const uint8_t *frm, const uint8_t *efrm)
2040 const struct ieee80211_action_ht_mimopowersave *mps =
2041 (const struct ieee80211_action_ht_mimopowersave *) frm;
2043 /* XXX check iv_htcaps */
2044 if (mps->am_control & IEEE80211_A_HT_MIMOPWRSAVE_ENA)
2045 ni->ni_flags |= IEEE80211_NODE_MIMO_PS;
2047 ni->ni_flags &= ~IEEE80211_NODE_MIMO_PS;
2048 if (mps->am_control & IEEE80211_A_HT_MIMOPWRSAVE_MODE)
2049 ni->ni_flags |= IEEE80211_NODE_MIMO_RTS;
2051 ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS;
2052 /* XXX notify on change */
2053 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2054 "%s: HT MIMO PS (%s%s)", __func__,
2055 (ni->ni_flags & IEEE80211_NODE_MIMO_PS) ? "on" : "off",
2056 (ni->ni_flags & IEEE80211_NODE_MIMO_RTS) ? "+rts" : ""
2062 * Transmit processing.
2066 * Check if A-MPDU should be requested/enabled for a stream.
2067 * We require a traffic rate above a per-AC threshold and we
2068 * also handle backoff from previous failed attempts.
2070 * Drivers may override this method to bring in information
2071 * such as link state conditions in making the decision.
2074 ieee80211_ampdu_enable(struct ieee80211_node *ni,
2075 struct ieee80211_tx_ampdu *tap)
2077 struct ieee80211vap *vap = ni->ni_vap;
2079 if (tap->txa_avgpps <
2080 vap->iv_ampdu_mintraffic[TID_TO_WME_AC(tap->txa_tid)])
2082 /* XXX check rssi? */
2083 if (tap->txa_attempts >= ieee80211_addba_maxtries &&
2084 ticks < tap->txa_nextrequest) {
2086 * Don't retry too often; txa_nextrequest is set
2087 * to the minimum interval we'll retry after
2088 * ieee80211_addba_maxtries failed attempts are made.
2092 IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
2093 "enable AMPDU on tid %d (%s), avgpps %d pkts %d",
2094 tap->txa_tid, ieee80211_wme_acnames[TID_TO_WME_AC(tap->txa_tid)],
2095 tap->txa_avgpps, tap->txa_pkts);
2100 * Request A-MPDU tx aggregation. Setup local state and
2101 * issue an ADDBA request. BA use will only happen after
2102 * the other end replies with ADDBA response.
2105 ieee80211_ampdu_request(struct ieee80211_node *ni,
2106 struct ieee80211_tx_ampdu *tap)
2108 struct ieee80211com *ic = ni->ni_ic;
2110 int tid, dialogtoken;
2111 static int tokens = 0; /* XXX */
2114 if ((tap->txa_flags & IEEE80211_AGGR_SETUP) == 0) {
2115 /* do deferred setup of state */
2116 ampdu_tx_setup(tap);
2118 /* XXX hack for not doing proper locking */
2119 tap->txa_flags &= ~IEEE80211_AGGR_NAK;
2121 dialogtoken = (tokens+1) % 63; /* XXX */
2123 tap->txa_start = ni->ni_txseqs[tid];
2125 args[0] = dialogtoken;
2126 args[1] = 0; /* NB: status code not used */
2127 args[2] = IEEE80211_BAPS_POLICY_IMMEDIATE
2128 | SM(tid, IEEE80211_BAPS_TID)
2129 | SM(IEEE80211_AGGR_BAWMAX, IEEE80211_BAPS_BUFSIZ)
2131 args[3] = 0; /* batimeout */
2132 /* NB: do first so there's no race against reply */
2133 if (!ic->ic_addba_request(ni, tap, dialogtoken, args[2], args[3])) {
2134 /* unable to setup state, don't make request */
2135 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2136 ni, "%s: could not setup BA stream for TID %d AC %d",
2137 __func__, tap->txa_tid, TID_TO_WME_AC(tap->txa_tid));
2138 /* defer next try so we don't slam the driver with requests */
2139 tap->txa_attempts = ieee80211_addba_maxtries;
2140 /* NB: check in case driver wants to override */
2141 if (tap->txa_nextrequest <= ticks)
2142 tap->txa_nextrequest = ticks + ieee80211_addba_backoff;
2145 tokens = dialogtoken; /* allocate token */
2146 /* NB: after calling ic_addba_request so driver can set txa_start */
2147 args[4] = SM(tap->txa_start, IEEE80211_BASEQ_START)
2148 | SM(0, IEEE80211_BASEQ_FRAG)
2150 return ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
2151 IEEE80211_ACTION_BA_ADDBA_REQUEST, args);
2155 * Terminate an AMPDU tx stream. State is reclaimed
2156 * and the peer notified with a DelBA Action frame.
2159 ieee80211_ampdu_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
2162 struct ieee80211com *ic = ni->ni_ic;
2163 struct ieee80211vap *vap = ni->ni_vap;
2167 tap->txa_flags &= ~IEEE80211_AGGR_BARPEND;
2168 if (IEEE80211_AMPDU_RUNNING(tap)) {
2169 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2170 ni, "%s: stop BA stream for TID %d (reason %d)",
2171 __func__, tap->txa_tid, reason);
2172 vap->iv_stats.is_ampdu_stop++;
2174 ic->ic_addba_stop(ni, tap);
2175 args[0] = tap->txa_tid;
2176 args[1] = IEEE80211_DELBAPS_INIT;
2177 args[2] = reason; /* XXX reason code */
2178 ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
2179 IEEE80211_ACTION_BA_DELBA, args);
2181 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2182 ni, "%s: BA stream for TID %d not running (reason %d)",
2183 __func__, tap->txa_tid, reason);
2184 vap->iv_stats.is_ampdu_stop_failed++;
2189 static void bar_start_timer(struct ieee80211_tx_ampdu *tap);
2192 bar_timeout(void *arg)
2194 struct ieee80211_tx_ampdu *tap = arg;
2195 struct ieee80211_node *ni = tap->txa_ni;
2197 KASSERT((tap->txa_flags & IEEE80211_AGGR_XCHGPEND) == 0,
2198 ("bar/addba collision, flags 0x%x", tap->txa_flags));
2200 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2201 ni, "%s: tid %u flags 0x%x attempts %d", __func__,
2202 tap->txa_tid, tap->txa_flags, tap->txa_attempts);
2204 /* guard against race with bar_tx_complete */
2205 if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) == 0)
2208 if (tap->txa_attempts >= ieee80211_bar_maxtries) {
2209 struct ieee80211com *ic = ni->ni_ic;
2211 ni->ni_vap->iv_stats.is_ampdu_bar_tx_fail++;
2213 * If (at least) the last BAR TX timeout was due to
2214 * an ieee80211_send_bar() failures, then we need
2215 * to make sure we notify the driver that a BAR
2216 * TX did occur and fail. This gives the driver
2217 * a chance to undo any queue pause that may
2220 ic->ic_bar_response(ni, tap, 1);
2221 ieee80211_ampdu_stop(ni, tap, IEEE80211_REASON_TIMEOUT);
2223 ni->ni_vap->iv_stats.is_ampdu_bar_tx_retry++;
2224 if (ieee80211_send_bar(ni, tap, tap->txa_seqpending) != 0) {
2225 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2226 ni, "%s: failed to TX, starting timer\n",
2229 * If ieee80211_send_bar() fails here, the
2230 * timer may have stopped and/or the pending
2231 * flag may be clear. Because of this,
2232 * fake the BARPEND and reset the timer.
2233 * A retransmission attempt will then occur
2234 * during the next timeout.
2237 tap->txa_flags |= IEEE80211_AGGR_BARPEND;
2238 bar_start_timer(tap);
2244 bar_start_timer(struct ieee80211_tx_ampdu *tap)
2246 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2250 callout_reset(&tap->txa_timer, ieee80211_bar_timeout, bar_timeout, tap);
2254 bar_stop_timer(struct ieee80211_tx_ampdu *tap)
2256 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2260 callout_stop(&tap->txa_timer);
2264 bar_tx_complete(struct ieee80211_node *ni, void *arg, int status)
2266 struct ieee80211_tx_ampdu *tap = arg;
2268 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2269 ni, "%s: tid %u flags 0x%x pending %d status %d",
2270 __func__, tap->txa_tid, tap->txa_flags,
2271 callout_pending(&tap->txa_timer), status);
2273 ni->ni_vap->iv_stats.is_ampdu_bar_tx++;
2275 if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) &&
2276 callout_pending(&tap->txa_timer)) {
2277 struct ieee80211com *ic = ni->ni_ic;
2279 if (status == 0) /* ACK'd */
2280 bar_stop_timer(tap);
2281 ic->ic_bar_response(ni, tap, status);
2282 /* NB: just let timer expire so we pace requests */
2287 ieee80211_bar_response(struct ieee80211_node *ni,
2288 struct ieee80211_tx_ampdu *tap, int status)
2291 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2295 if (status == 0) { /* got ACK */
2296 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2297 ni, "BAR moves BA win <%u:%u> (%u frames) txseq %u tid %u",
2299 IEEE80211_SEQ_ADD(tap->txa_start, tap->txa_wnd-1),
2300 tap->txa_qframes, tap->txa_seqpending,
2303 /* NB: timer already stopped in bar_tx_complete */
2304 tap->txa_start = tap->txa_seqpending;
2305 tap->txa_flags &= ~IEEE80211_AGGR_BARPEND;
2310 * Transmit a BAR frame to the specified node. The
2311 * BAR contents are drawn from the supplied aggregation
2312 * state associated with the node.
2314 * NB: we only handle immediate ACK w/ compressed bitmap.
2317 ieee80211_send_bar(struct ieee80211_node *ni,
2318 struct ieee80211_tx_ampdu *tap, ieee80211_seq seq)
2320 #define senderr(_x, _v) do { vap->iv_stats._v++; ret = _x; goto bad; } while (0)
2321 struct ieee80211vap *vap = ni->ni_vap;
2322 struct ieee80211com *ic = ni->ni_ic;
2323 struct ieee80211_frame_bar *bar;
2325 uint16_t barctl, barseqctl;
2330 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2335 if ((tap->txa_flags & IEEE80211_AGGR_RUNNING) == 0) {
2336 /* no ADDBA response, should not happen */
2341 bar_stop_timer(tap);
2343 ieee80211_ref_node(ni);
2345 m = ieee80211_getmgtframe(&frm, ic->ic_headroom, sizeof(*bar));
2347 senderr(ENOMEM, is_tx_nobuf);
2349 if (!ieee80211_add_callback(m, bar_tx_complete, tap)) {
2351 senderr(ENOMEM, is_tx_nobuf); /* XXX */
2355 bar = mtod(m, struct ieee80211_frame_bar *);
2356 bar->i_fc[0] = IEEE80211_FC0_VERSION_0 |
2357 IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_BAR;
2359 IEEE80211_ADDR_COPY(bar->i_ra, ni->ni_macaddr);
2360 IEEE80211_ADDR_COPY(bar->i_ta, vap->iv_myaddr);
2363 barctl = (tap->txa_flags & IEEE80211_AGGR_IMMEDIATE ?
2364 0 : IEEE80211_BAR_NOACK)
2365 | IEEE80211_BAR_COMP
2366 | SM(tid, IEEE80211_BAR_TID)
2368 barseqctl = SM(seq, IEEE80211_BAR_SEQ_START);
2369 /* NB: known to have proper alignment */
2370 bar->i_ctl = htole16(barctl);
2371 bar->i_seq = htole16(barseqctl);
2372 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_bar);
2374 M_WME_SETAC(m, WME_AC_VO);
2376 IEEE80211_NODE_STAT(ni, tx_mgmt); /* XXX tx_ctl? */
2379 /* init/bump attempts counter */
2380 if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) == 0)
2381 tap->txa_attempts = 1;
2383 tap->txa_attempts++;
2384 tap->txa_seqpending = seq;
2385 tap->txa_flags |= IEEE80211_AGGR_BARPEND;
2387 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_11N,
2388 ni, "send BAR: tid %u ctl 0x%x start %u (attempt %d)",
2389 tid, barctl, seq, tap->txa_attempts);
2392 * ic_raw_xmit will free the node reference
2393 * regardless of queue/TX success or failure.
2395 IEEE80211_TX_LOCK(ic);
2396 ret = ieee80211_raw_output(vap, ni, m, NULL);
2397 IEEE80211_TX_UNLOCK(ic);
2399 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_11N,
2400 ni, "send BAR: failed: (ret = %d)\n",
2402 /* xmit failed, clear state flag */
2403 tap->txa_flags &= ~IEEE80211_AGGR_BARPEND;
2404 vap->iv_stats.is_ampdu_bar_tx_fail++;
2407 /* XXX hack against tx complete happening before timer is started */
2408 if (tap->txa_flags & IEEE80211_AGGR_BARPEND)
2409 bar_start_timer(tap);
2412 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2416 vap->iv_stats.is_ampdu_bar_tx_fail++;
2417 ieee80211_free_node(ni);
2423 ht_action_output(struct ieee80211_node *ni, struct mbuf *m)
2425 struct ieee80211_bpf_params params;
2427 memset(¶ms, 0, sizeof(params));
2428 params.ibp_pri = WME_AC_VO;
2429 params.ibp_rate0 = ni->ni_txparms->mgmtrate;
2430 /* NB: we know all frames are unicast */
2431 params.ibp_try0 = ni->ni_txparms->maxretry;
2432 params.ibp_power = ni->ni_txpower;
2433 return ieee80211_mgmt_output(ni, m, IEEE80211_FC0_SUBTYPE_ACTION,
2437 #define ADDSHORT(frm, v) do { \
2438 frm[0] = (v) & 0xff; \
2439 frm[1] = (v) >> 8; \
2444 * Send an action management frame. The arguments are stuff
2445 * into a frame without inspection; the caller is assumed to
2446 * prepare them carefully (e.g. based on the aggregation state).
2449 ht_send_action_ba_addba(struct ieee80211_node *ni,
2450 int category, int action, void *arg0)
2452 struct ieee80211vap *vap = ni->ni_vap;
2453 struct ieee80211com *ic = ni->ni_ic;
2454 uint16_t *args = arg0;
2458 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2459 "send ADDBA %s: dialogtoken %d status %d "
2460 "baparamset 0x%x (tid %d) batimeout 0x%x baseqctl 0x%x",
2461 (action == IEEE80211_ACTION_BA_ADDBA_REQUEST) ?
2462 "request" : "response",
2463 args[0], args[1], args[2], MS(args[2], IEEE80211_BAPS_TID),
2466 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2467 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2468 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2469 ieee80211_ref_node(ni);
2471 m = ieee80211_getmgtframe(&frm,
2472 ic->ic_headroom + sizeof(struct ieee80211_frame),
2473 sizeof(uint16_t) /* action+category */
2474 /* XXX may action payload */
2475 + sizeof(struct ieee80211_action_ba_addbaresponse)
2480 *frm++ = args[0]; /* dialog token */
2481 if (action == IEEE80211_ACTION_BA_ADDBA_RESPONSE)
2482 ADDSHORT(frm, args[1]); /* status code */
2483 ADDSHORT(frm, args[2]); /* baparamset */
2484 ADDSHORT(frm, args[3]); /* batimeout */
2485 if (action == IEEE80211_ACTION_BA_ADDBA_REQUEST)
2486 ADDSHORT(frm, args[4]); /* baseqctl */
2487 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2488 return ht_action_output(ni, m);
2490 vap->iv_stats.is_tx_nobuf++;
2491 ieee80211_free_node(ni);
2497 ht_send_action_ba_delba(struct ieee80211_node *ni,
2498 int category, int action, void *arg0)
2500 struct ieee80211vap *vap = ni->ni_vap;
2501 struct ieee80211com *ic = ni->ni_ic;
2502 uint16_t *args = arg0;
2504 uint16_t baparamset;
2507 baparamset = SM(args[0], IEEE80211_DELBAPS_TID)
2510 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2511 "send DELBA action: tid %d, initiator %d reason %d",
2512 args[0], args[1], args[2]);
2514 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2515 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2516 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2517 ieee80211_ref_node(ni);
2519 m = ieee80211_getmgtframe(&frm,
2520 ic->ic_headroom + sizeof(struct ieee80211_frame),
2521 sizeof(uint16_t) /* action+category */
2522 /* XXX may action payload */
2523 + sizeof(struct ieee80211_action_ba_addbaresponse)
2528 ADDSHORT(frm, baparamset);
2529 ADDSHORT(frm, args[2]); /* reason code */
2530 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2531 return ht_action_output(ni, m);
2533 vap->iv_stats.is_tx_nobuf++;
2534 ieee80211_free_node(ni);
2540 ht_send_action_ht_txchwidth(struct ieee80211_node *ni,
2541 int category, int action, void *arg0)
2543 struct ieee80211vap *vap = ni->ni_vap;
2544 struct ieee80211com *ic = ni->ni_ic;
2548 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2549 "send HT txchwidth: width %d",
2550 IEEE80211_IS_CHAN_HT40(ni->ni_chan) ? 40 : 20);
2552 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2553 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2554 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2555 ieee80211_ref_node(ni);
2557 m = ieee80211_getmgtframe(&frm,
2558 ic->ic_headroom + sizeof(struct ieee80211_frame),
2559 sizeof(uint16_t) /* action+category */
2560 /* XXX may action payload */
2561 + sizeof(struct ieee80211_action_ba_addbaresponse)
2566 *frm++ = IEEE80211_IS_CHAN_HT40(ni->ni_chan) ?
2567 IEEE80211_A_HT_TXCHWIDTH_2040 :
2568 IEEE80211_A_HT_TXCHWIDTH_20;
2569 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2570 return ht_action_output(ni, m);
2572 vap->iv_stats.is_tx_nobuf++;
2573 ieee80211_free_node(ni);
2580 * Construct the MCS bit mask for inclusion in an HT capabilities
2581 * information element.
2584 ieee80211_set_mcsset(struct ieee80211com *ic, uint8_t *frm)
2589 KASSERT((ic->ic_rxstream > 0 && ic->ic_rxstream <= 4),
2590 ("ic_rxstream %d out of range", ic->ic_rxstream));
2591 KASSERT((ic->ic_txstream > 0 && ic->ic_txstream <= 4),
2592 ("ic_txstream %d out of range", ic->ic_txstream));
2594 for (i = 0; i < ic->ic_rxstream * 8; i++)
2596 if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
2597 (ic->ic_htcaps & IEEE80211_HTC_RXMCS32))
2599 if (ic->ic_htcaps & IEEE80211_HTC_RXUNEQUAL) {
2600 if (ic->ic_rxstream >= 2) {
2601 for (i = 33; i <= 38; i++)
2604 if (ic->ic_rxstream >= 3) {
2605 for (i = 39; i <= 52; i++)
2608 if (ic->ic_txstream >= 4) {
2609 for (i = 53; i <= 76; i++)
2614 if (ic->ic_rxstream != ic->ic_txstream) {
2615 txparams = 0x1; /* TX MCS set defined */
2616 txparams |= 0x2; /* TX RX MCS not equal */
2617 txparams |= (ic->ic_txstream - 1) << 2; /* num TX streams */
2618 if (ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL)
2619 txparams |= 0x16; /* TX unequal modulation sup */
2626 * Add body of an HTCAP information element.
2629 ieee80211_add_htcap_body(uint8_t *frm, struct ieee80211_node *ni)
2631 #define ADDSHORT(frm, v) do { \
2632 frm[0] = (v) & 0xff; \
2633 frm[1] = (v) >> 8; \
2636 struct ieee80211com *ic = ni->ni_ic;
2637 struct ieee80211vap *vap = ni->ni_vap;
2638 uint16_t caps, extcaps;
2641 /* HT capabilities */
2642 caps = vap->iv_htcaps & 0xffff;
2644 * Note channel width depends on whether we are operating as
2645 * a sta or not. When operating as a sta we are generating
2646 * a request based on our desired configuration. Otherwise
2647 * we are operational and the channel attributes identify
2648 * how we've been setup (which might be different if a fixed
2649 * channel is specified).
2651 if (vap->iv_opmode == IEEE80211_M_STA) {
2652 /* override 20/40 use based on config */
2653 if (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)
2654 caps |= IEEE80211_HTCAP_CHWIDTH40;
2656 caps &= ~IEEE80211_HTCAP_CHWIDTH40;
2657 /* use advertised setting (XXX locally constraint) */
2658 rxmax = MS(ni->ni_htparam, IEEE80211_HTCAP_MAXRXAMPDU);
2659 density = MS(ni->ni_htparam, IEEE80211_HTCAP_MPDUDENSITY);
2662 * NB: Hardware might support HT40 on some but not all
2663 * channels. We can't determine this earlier because only
2664 * after association the channel is upgraded to HT based
2665 * on the negotiated capabilities.
2667 if (ni->ni_chan != IEEE80211_CHAN_ANYC &&
2668 findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT40U) == NULL &&
2669 findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT40D) == NULL)
2670 caps &= ~IEEE80211_HTCAP_CHWIDTH40;
2672 /* override 20/40 use based on current channel */
2673 if (IEEE80211_IS_CHAN_HT40(ni->ni_chan))
2674 caps |= IEEE80211_HTCAP_CHWIDTH40;
2676 caps &= ~IEEE80211_HTCAP_CHWIDTH40;
2677 rxmax = vap->iv_ampdu_rxmax;
2678 density = vap->iv_ampdu_density;
2680 /* adjust short GI based on channel and config */
2681 if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20) == 0)
2682 caps &= ~IEEE80211_HTCAP_SHORTGI20;
2683 if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40) == 0 ||
2684 (caps & IEEE80211_HTCAP_CHWIDTH40) == 0)
2685 caps &= ~IEEE80211_HTCAP_SHORTGI40;
2686 ADDSHORT(frm, caps);
2689 *frm = SM(rxmax, IEEE80211_HTCAP_MAXRXAMPDU)
2690 | SM(density, IEEE80211_HTCAP_MPDUDENSITY)
2694 /* pre-zero remainder of ie */
2695 memset(frm, 0, sizeof(struct ieee80211_ie_htcap) -
2696 __offsetof(struct ieee80211_ie_htcap, hc_mcsset));
2698 /* supported MCS set */
2700 * XXX: For sta mode the rate set should be restricted based
2701 * on the AP's capabilities, but ni_htrates isn't setup when
2702 * we're called to form an AssocReq frame so for now we're
2703 * restricted to the device capabilities.
2705 ieee80211_set_mcsset(ni->ni_ic, frm);
2707 frm += __offsetof(struct ieee80211_ie_htcap, hc_extcap) -
2708 __offsetof(struct ieee80211_ie_htcap, hc_mcsset);
2710 /* HT extended capabilities */
2711 extcaps = vap->iv_htextcaps & 0xffff;
2713 ADDSHORT(frm, extcaps);
2715 frm += sizeof(struct ieee80211_ie_htcap) -
2716 __offsetof(struct ieee80211_ie_htcap, hc_txbf);
2723 * Add 802.11n HT capabilities information element
2726 ieee80211_add_htcap(uint8_t *frm, struct ieee80211_node *ni)
2728 frm[0] = IEEE80211_ELEMID_HTCAP;
2729 frm[1] = sizeof(struct ieee80211_ie_htcap) - 2;
2730 return ieee80211_add_htcap_body(frm + 2, ni);
2734 * Add Broadcom OUI wrapped standard HTCAP ie; this is
2735 * used for compatibility w/ pre-draft implementations.
2738 ieee80211_add_htcap_vendor(uint8_t *frm, struct ieee80211_node *ni)
2740 frm[0] = IEEE80211_ELEMID_VENDOR;
2741 frm[1] = 4 + sizeof(struct ieee80211_ie_htcap) - 2;
2742 frm[2] = (BCM_OUI >> 0) & 0xff;
2743 frm[3] = (BCM_OUI >> 8) & 0xff;
2744 frm[4] = (BCM_OUI >> 16) & 0xff;
2745 frm[5] = BCM_OUI_HTCAP;
2746 return ieee80211_add_htcap_body(frm + 6, ni);
2750 * Construct the MCS bit mask of basic rates
2751 * for inclusion in an HT information element.
2754 ieee80211_set_basic_htrates(uint8_t *frm, const struct ieee80211_htrateset *rs)
2758 for (i = 0; i < rs->rs_nrates; i++) {
2759 int r = rs->rs_rates[i] & IEEE80211_RATE_VAL;
2760 if ((rs->rs_rates[i] & IEEE80211_RATE_BASIC) &&
2761 r < IEEE80211_HTRATE_MAXSIZE) {
2762 /* NB: this assumes a particular implementation */
2769 * Update the HTINFO ie for a beacon frame.
2772 ieee80211_ht_update_beacon(struct ieee80211vap *vap,
2773 struct ieee80211_beacon_offsets *bo)
2775 #define PROTMODE (IEEE80211_HTINFO_OPMODE|IEEE80211_HTINFO_NONHT_PRESENT)
2776 struct ieee80211_node *ni;
2777 const struct ieee80211_channel *bsschan;
2778 struct ieee80211com *ic = vap->iv_ic;
2779 struct ieee80211_ie_htinfo *ht =
2780 (struct ieee80211_ie_htinfo *) bo->bo_htinfo;
2782 ni = ieee80211_ref_node(vap->iv_bss);
2783 bsschan = ni->ni_chan;
2785 /* XXX only update on channel change */
2786 ht->hi_ctrlchannel = ieee80211_chan2ieee(ic, bsschan);
2787 if (vap->iv_flags_ht & IEEE80211_FHT_RIFS)
2788 ht->hi_byte1 = IEEE80211_HTINFO_RIFSMODE_PERM;
2790 ht->hi_byte1 = IEEE80211_HTINFO_RIFSMODE_PROH;
2791 if (IEEE80211_IS_CHAN_HT40U(bsschan))
2792 ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_ABOVE;
2793 else if (IEEE80211_IS_CHAN_HT40D(bsschan))
2794 ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_BELOW;
2796 ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_NONE;
2797 if (IEEE80211_IS_CHAN_HT40(bsschan))
2798 ht->hi_byte1 |= IEEE80211_HTINFO_TXWIDTH_2040;
2800 /* protection mode */
2801 ht->hi_byte2 = (ht->hi_byte2 &~ PROTMODE) | ic->ic_curhtprotmode;
2803 ieee80211_free_node(ni);
2805 /* XXX propagate to vendor ie's */
2810 * Add body of an HTINFO information element.
2812 * NB: We don't use struct ieee80211_ie_htinfo because we can
2813 * be called to fillin both a standard ie and a compat ie that
2814 * has a vendor OUI at the front.
2817 ieee80211_add_htinfo_body(uint8_t *frm, struct ieee80211_node *ni)
2819 struct ieee80211vap *vap = ni->ni_vap;
2820 struct ieee80211com *ic = ni->ni_ic;
2822 /* pre-zero remainder of ie */
2823 memset(frm, 0, sizeof(struct ieee80211_ie_htinfo) - 2);
2825 /* primary/control channel center */
2826 *frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
2828 if (vap->iv_flags_ht & IEEE80211_FHT_RIFS)
2829 frm[0] = IEEE80211_HTINFO_RIFSMODE_PERM;
2831 frm[0] = IEEE80211_HTINFO_RIFSMODE_PROH;
2832 if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan))
2833 frm[0] |= IEEE80211_HTINFO_2NDCHAN_ABOVE;
2834 else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan))
2835 frm[0] |= IEEE80211_HTINFO_2NDCHAN_BELOW;
2837 frm[0] |= IEEE80211_HTINFO_2NDCHAN_NONE;
2838 if (IEEE80211_IS_CHAN_HT40(ni->ni_chan))
2839 frm[0] |= IEEE80211_HTINFO_TXWIDTH_2040;
2841 frm[1] = ic->ic_curhtprotmode;
2846 ieee80211_set_basic_htrates(frm, &ni->ni_htrates);
2847 frm += sizeof(struct ieee80211_ie_htinfo) -
2848 __offsetof(struct ieee80211_ie_htinfo, hi_basicmcsset);
2853 * Add 802.11n HT information information element.
2856 ieee80211_add_htinfo(uint8_t *frm, struct ieee80211_node *ni)
2858 frm[0] = IEEE80211_ELEMID_HTINFO;
2859 frm[1] = sizeof(struct ieee80211_ie_htinfo) - 2;
2860 return ieee80211_add_htinfo_body(frm + 2, ni);
2864 * Add Broadcom OUI wrapped standard HTINFO ie; this is
2865 * used for compatibility w/ pre-draft implementations.
2868 ieee80211_add_htinfo_vendor(uint8_t *frm, struct ieee80211_node *ni)
2870 frm[0] = IEEE80211_ELEMID_VENDOR;
2871 frm[1] = 4 + sizeof(struct ieee80211_ie_htinfo) - 2;
2872 frm[2] = (BCM_OUI >> 0) & 0xff;
2873 frm[3] = (BCM_OUI >> 8) & 0xff;
2874 frm[4] = (BCM_OUI >> 16) & 0xff;
2875 frm[5] = BCM_OUI_HTINFO;
2876 return ieee80211_add_htinfo_body(frm + 6, ni);