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 if (vap->iv_htcaps & IEEE80211_HTCAP_LDPC)
303 vap->iv_flags_ht |= IEEE80211_FHT_LDPC_RX;
304 if (vap->iv_htcaps & IEEE80211_HTC_TXLDPC)
305 vap->iv_flags_ht |= IEEE80211_FHT_LDPC_TX;
307 /* NB: disable default legacy WDS, too many issues right now */
308 if (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY)
309 vap->iv_flags_ht &= ~IEEE80211_FHT_HT;
313 ieee80211_ht_vdetach(struct ieee80211vap *vap)
318 ht_getrate(struct ieee80211com *ic, int index, enum ieee80211_phymode mode,
323 mword = ieee80211_rate2media(ic, index | IEEE80211_RATE_MCS, mode);
324 if (IFM_SUBTYPE(mword) != IFM_IEEE80211_MCS)
328 rate = ieee80211_htrates[index].ht20_rate_800ns;
331 rate = ieee80211_htrates[index].ht20_rate_400ns;
334 rate = ieee80211_htrates[index].ht40_rate_800ns;
337 rate = ieee80211_htrates[index].ht40_rate_400ns;
343 static struct printranges {
354 { 32, 0, 1, 2, IEEE80211_HTC_TXMCS32 },
355 { 33, 38, 2, 0, IEEE80211_HTC_TXUNEQUAL },
356 { 39, 52, 3, 0, IEEE80211_HTC_TXUNEQUAL },
357 { 53, 76, 4, 0, IEEE80211_HTC_TXUNEQUAL },
362 ht_rateprint(struct ieee80211com *ic, enum ieee80211_phymode mode, int ratetype)
364 int minrate, maxrate;
365 struct printranges *range;
367 for (range = ranges; range->txstream != 0; range++) {
368 if (ic->ic_txstream < range->txstream)
370 if (range->htcapflags &&
371 (ic->ic_htcaps & range->htcapflags) == 0)
373 if (ratetype < range->ratetype)
375 minrate = ht_getrate(ic, range->minmcs, mode, ratetype);
376 maxrate = ht_getrate(ic, range->maxmcs, mode, ratetype);
378 ic_printf(ic, "MCS %d-%d: %d%sMbps - %d%sMbps\n",
379 range->minmcs, range->maxmcs,
380 minrate/2, ((minrate & 0x1) != 0 ? ".5" : ""),
381 maxrate/2, ((maxrate & 0x1) != 0 ? ".5" : ""));
383 ic_printf(ic, "MCS %d: %d%sMbps\n", range->minmcs,
384 minrate/2, ((minrate & 0x1) != 0 ? ".5" : ""));
390 ht_announce(struct ieee80211com *ic, enum ieee80211_phymode mode)
392 const char *modestr = ieee80211_phymode_name[mode];
394 ic_printf(ic, "%s MCS 20MHz\n", modestr);
395 ht_rateprint(ic, mode, 0);
396 if (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20) {
397 ic_printf(ic, "%s MCS 20MHz SGI\n", modestr);
398 ht_rateprint(ic, mode, 1);
400 if (ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
401 ic_printf(ic, "%s MCS 40MHz:\n", modestr);
402 ht_rateprint(ic, mode, 2);
404 if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
405 (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40)) {
406 ic_printf(ic, "%s MCS 40MHz SGI:\n", modestr);
407 ht_rateprint(ic, mode, 3);
412 ieee80211_ht_announce(struct ieee80211com *ic)
415 if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) ||
416 isset(ic->ic_modecaps, IEEE80211_MODE_11NG))
417 ic_printf(ic, "%dT%dR\n", ic->ic_txstream, ic->ic_rxstream);
418 if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA))
419 ht_announce(ic, IEEE80211_MODE_11NA);
420 if (isset(ic->ic_modecaps, IEEE80211_MODE_11NG))
421 ht_announce(ic, IEEE80211_MODE_11NG);
425 ieee80211_init_suphtrates(struct ieee80211com *ic)
427 #define ADDRATE(x) do { \
428 htrateset->rs_rates[htrateset->rs_nrates] = x; \
429 htrateset->rs_nrates++; \
431 struct ieee80211_htrateset *htrateset = &ic->ic_sup_htrates;
434 memset(htrateset, 0, sizeof(struct ieee80211_htrateset));
435 for (i = 0; i < ic->ic_txstream * 8; i++)
437 if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
438 (ic->ic_htcaps & IEEE80211_HTC_TXMCS32))
440 if (ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL) {
441 if (ic->ic_txstream >= 2) {
442 for (i = 33; i <= 38; i++)
445 if (ic->ic_txstream >= 3) {
446 for (i = 39; i <= 52; i++)
449 if (ic->ic_txstream == 4) {
450 for (i = 53; i <= 76; i++)
458 * Receive processing.
462 * Decap the encapsulated A-MSDU frames and dispatch all but
463 * the last for delivery. The last frame is returned for
464 * delivery via the normal path.
467 ieee80211_decap_amsdu(struct ieee80211_node *ni, struct mbuf *m)
469 struct ieee80211vap *vap = ni->ni_vap;
473 /* discard 802.3 header inserted by ieee80211_decap */
474 m_adj(m, sizeof(struct ether_header));
476 vap->iv_stats.is_amsdu_decap++;
480 * Decap the first frame, bust it apart from the
481 * remainder and deliver. We leave the last frame
482 * delivery to the caller (for consistency with other
483 * code paths, could also do it here).
485 m = ieee80211_decap1(m, &framelen);
487 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
488 ni->ni_macaddr, "a-msdu", "%s", "decap failed");
489 vap->iv_stats.is_amsdu_tooshort++;
492 if (m->m_pkthdr.len == framelen)
494 n = m_split(m, framelen, M_NOWAIT);
496 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
497 ni->ni_macaddr, "a-msdu",
498 "%s", "unable to split encapsulated frames");
499 vap->iv_stats.is_amsdu_split++;
500 m_freem(m); /* NB: must reclaim */
503 vap->iv_deliver_data(vap, ni, m);
506 * Remove frame contents; each intermediate frame
507 * is required to be aligned to a 4-byte boundary.
510 m_adj(m, roundup2(framelen, 4) - framelen); /* padding */
512 return m; /* last delivered by caller */
516 * Purge all frames in the A-MPDU re-order queue.
519 ampdu_rx_purge(struct ieee80211_rx_ampdu *rap)
524 for (i = 0; i < rap->rxa_wnd; i++) {
527 rap->rxa_m[i] = NULL;
528 rap->rxa_qbytes -= m->m_pkthdr.len;
530 if (--rap->rxa_qframes == 0)
534 KASSERT(rap->rxa_qbytes == 0 && rap->rxa_qframes == 0,
535 ("lost %u data, %u frames on ampdu rx q",
536 rap->rxa_qbytes, rap->rxa_qframes));
540 * Start A-MPDU rx/re-order processing for the specified TID.
543 ampdu_rx_start(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap,
544 int baparamset, int batimeout, int baseqctl)
546 int bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
548 if (rap->rxa_flags & IEEE80211_AGGR_RUNNING) {
550 * AMPDU previously setup and not terminated with a DELBA,
551 * flush the reorder q's in case anything remains.
555 memset(rap, 0, sizeof(*rap));
556 rap->rxa_wnd = (bufsiz == 0) ?
557 IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
558 rap->rxa_start = MS(baseqctl, IEEE80211_BASEQ_START);
559 rap->rxa_flags |= IEEE80211_AGGR_RUNNING | IEEE80211_AGGR_XCHGPEND;
565 * Public function; manually setup the RX ampdu state.
568 ieee80211_ampdu_rx_start_ext(struct ieee80211_node *ni, int tid, int seq, int baw)
570 struct ieee80211_rx_ampdu *rap;
572 /* XXX TODO: sanity check tid, seq, baw */
574 rap = &ni->ni_rx_ampdu[tid];
576 if (rap->rxa_flags & IEEE80211_AGGR_RUNNING) {
578 * AMPDU previously setup and not terminated with a DELBA,
579 * flush the reorder q's in case anything remains.
584 memset(rap, 0, sizeof(*rap));
585 rap->rxa_wnd = (baw== 0) ?
586 IEEE80211_AGGR_BAWMAX : min(baw, IEEE80211_AGGR_BAWMAX);
588 /* Wait for the first RX frame, use that as BAW */
590 rap->rxa_flags |= IEEE80211_AGGR_WAITRX;
592 rap->rxa_start = seq;
594 rap->rxa_flags |= IEEE80211_AGGR_RUNNING | IEEE80211_AGGR_XCHGPEND;
596 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, ni,
597 "%s: tid=%d, start=%d, wnd=%d, flags=0x%08x",
608 * Public function; manually stop the RX AMPDU state.
611 ieee80211_ampdu_rx_stop_ext(struct ieee80211_node *ni, int tid)
613 struct ieee80211_rx_ampdu *rap;
615 /* XXX TODO: sanity check tid, seq, baw */
616 rap = &ni->ni_rx_ampdu[tid];
617 ampdu_rx_stop(ni, rap);
621 * Stop A-MPDU rx processing for the specified TID.
624 ampdu_rx_stop(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
628 rap->rxa_flags &= ~(IEEE80211_AGGR_RUNNING
629 | IEEE80211_AGGR_XCHGPEND
630 | IEEE80211_AGGR_WAITRX);
634 * Dispatch a frame from the A-MPDU reorder queue. The
635 * frame is fed back into ieee80211_input marked with an
636 * M_AMPDU_MPDU flag so it doesn't come back to us (it also
637 * permits ieee80211_input to optimize re-processing).
640 ampdu_dispatch(struct ieee80211_node *ni, struct mbuf *m)
642 m->m_flags |= M_AMPDU_MPDU; /* bypass normal processing */
643 /* NB: rssi and noise are ignored w/ M_AMPDU_MPDU set */
644 (void) ieee80211_input(ni, m, 0, 0);
648 * Dispatch as many frames as possible from the re-order queue.
649 * Frames will always be "at the front"; we process all frames
650 * up to the first empty slot in the window. On completion we
651 * cleanup state if there are still pending frames in the current
652 * BA window. We assume the frame at slot 0 is already handled
653 * by the caller; we always start at slot 1.
656 ampdu_rx_dispatch(struct ieee80211_rx_ampdu *rap, struct ieee80211_node *ni)
658 struct ieee80211vap *vap = ni->ni_vap;
662 /* flush run of frames */
663 for (i = 1; i < rap->rxa_wnd; i++) {
667 rap->rxa_m[i] = NULL;
668 rap->rxa_qbytes -= m->m_pkthdr.len;
671 ampdu_dispatch(ni, m);
674 * If frames remain, copy the mbuf pointers down so
675 * they correspond to the offsets in the new window.
677 if (rap->rxa_qframes != 0) {
678 int n = rap->rxa_qframes, j;
679 for (j = i+1; j < rap->rxa_wnd; j++) {
680 if (rap->rxa_m[j] != NULL) {
681 rap->rxa_m[j-i] = rap->rxa_m[j];
682 rap->rxa_m[j] = NULL;
687 KASSERT(n == 0, ("lost %d frames", n));
688 vap->iv_stats.is_ampdu_rx_copy += rap->rxa_qframes;
691 * Adjust the start of the BA window to
692 * reflect the frames just dispatched.
694 rap->rxa_start = IEEE80211_SEQ_ADD(rap->rxa_start, i);
695 vap->iv_stats.is_ampdu_rx_oor += i;
699 * Dispatch all frames in the A-MPDU re-order queue.
702 ampdu_rx_flush(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
704 struct ieee80211vap *vap = ni->ni_vap;
708 for (i = 0; i < rap->rxa_wnd; i++) {
712 rap->rxa_m[i] = NULL;
713 rap->rxa_qbytes -= m->m_pkthdr.len;
715 vap->iv_stats.is_ampdu_rx_oor++;
717 ampdu_dispatch(ni, m);
718 if (rap->rxa_qframes == 0)
724 * Dispatch all frames in the A-MPDU re-order queue
725 * preceding the specified sequence number. This logic
726 * handles window moves due to a received MSDU or BAR.
729 ampdu_rx_flush_upto(struct ieee80211_node *ni,
730 struct ieee80211_rx_ampdu *rap, ieee80211_seq winstart)
732 struct ieee80211vap *vap = ni->ni_vap;
738 * Flush any complete MSDU's with a sequence number lower
739 * than winstart. Gaps may exist. Note that we may actually
740 * dispatch frames past winstart if a run continues; this is
741 * an optimization that avoids having to do a separate pass
742 * to dispatch frames after moving the BA window start.
744 seqno = rap->rxa_start;
745 for (i = 0; i < rap->rxa_wnd; i++) {
748 rap->rxa_m[i] = NULL;
749 rap->rxa_qbytes -= m->m_pkthdr.len;
751 vap->iv_stats.is_ampdu_rx_oor++;
753 ampdu_dispatch(ni, m);
755 if (!IEEE80211_SEQ_BA_BEFORE(seqno, winstart))
758 seqno = IEEE80211_SEQ_INC(seqno);
761 * If frames remain, copy the mbuf pointers down so
762 * they correspond to the offsets in the new window.
764 if (rap->rxa_qframes != 0) {
765 int n = rap->rxa_qframes, j;
767 /* NB: this loop assumes i > 0 and/or rxa_m[0] is NULL */
768 KASSERT(rap->rxa_m[0] == NULL,
769 ("%s: BA window slot 0 occupied", __func__));
770 for (j = i+1; j < rap->rxa_wnd; j++) {
771 if (rap->rxa_m[j] != NULL) {
772 rap->rxa_m[j-i] = rap->rxa_m[j];
773 rap->rxa_m[j] = NULL;
778 KASSERT(n == 0, ("%s: lost %d frames, qframes %d off %d "
779 "BA win <%d:%d> winstart %d",
780 __func__, n, rap->rxa_qframes, i, rap->rxa_start,
781 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
783 vap->iv_stats.is_ampdu_rx_copy += rap->rxa_qframes;
786 * Move the start of the BA window; we use the
787 * sequence number of the last MSDU that was
788 * passed up the stack+1 or winstart if stopped on
789 * a gap in the reorder buffer.
791 rap->rxa_start = seqno;
795 * Process a received QoS data frame for an HT station. Handle
796 * A-MPDU reordering: if this frame is received out of order
797 * and falls within the BA window hold onto it. Otherwise if
798 * this frame completes a run, flush any pending frames. We
799 * return 1 if the frame is consumed. A 0 is returned if
800 * the frame should be processed normally by the caller.
803 ieee80211_ampdu_reorder(struct ieee80211_node *ni, struct mbuf *m)
805 #define PROCESS 0 /* caller should process frame */
806 #define CONSUMED 1 /* frame consumed, caller does nothing */
807 struct ieee80211vap *vap = ni->ni_vap;
808 struct ieee80211_qosframe *wh;
809 struct ieee80211_rx_ampdu *rap;
814 KASSERT((m->m_flags & (M_AMPDU | M_AMPDU_MPDU)) == M_AMPDU,
815 ("!a-mpdu or already re-ordered, flags 0x%x", m->m_flags));
816 KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT sta"));
818 /* NB: m_len known to be sufficient */
819 wh = mtod(m, struct ieee80211_qosframe *);
820 if (wh->i_fc[0] != IEEE80211_FC0_QOSDATA) {
822 * Not QoS data, shouldn't get here but just
823 * return it to the caller for processing.
829 * 802.11-2012 9.3.2.10 - Duplicate detection and recovery.
831 * Multicast QoS data frames are checked against a different
832 * counter, not the per-TID counter.
834 if (IEEE80211_IS_MULTICAST(wh->i_addr1))
837 if (IEEE80211_IS_DSTODS(wh))
838 tid = ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0];
841 tid &= IEEE80211_QOS_TID;
842 rap = &ni->ni_rx_ampdu[tid];
843 if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
845 * No ADDBA request yet, don't touch.
849 rxseq = le16toh(*(uint16_t *)wh->i_seq);
850 if ((rxseq & IEEE80211_SEQ_FRAG_MASK) != 0) {
852 * Fragments are not allowed; toss.
854 IEEE80211_DISCARD_MAC(vap,
855 IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
856 "A-MPDU", "fragment, rxseq 0x%x tid %u%s", rxseq, tid,
857 wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
858 vap->iv_stats.is_ampdu_rx_drop++;
859 IEEE80211_NODE_STAT(ni, rx_drop);
863 rxseq >>= IEEE80211_SEQ_SEQ_SHIFT;
867 * Handle waiting for the first frame to define the BAW.
868 * Some firmware doesn't provide the RX of the starting point
869 * of the BAW and we have to cope.
871 if (rap->rxa_flags & IEEE80211_AGGR_WAITRX) {
872 rap->rxa_flags &= ~IEEE80211_AGGR_WAITRX;
873 rap->rxa_start = rxseq;
876 if (rxseq == rap->rxa_start) {
878 * First frame in window.
880 if (rap->rxa_qframes != 0) {
882 * Dispatch as many packets as we can.
884 KASSERT(rap->rxa_m[0] == NULL, ("unexpected dup"));
885 ampdu_dispatch(ni, m);
886 ampdu_rx_dispatch(rap, ni);
890 * In order; advance window and notify
891 * caller to dispatch directly.
893 rap->rxa_start = IEEE80211_SEQ_INC(rxseq);
898 * Frame is out of order; store if in the BA window.
900 /* calculate offset in BA window */
901 off = IEEE80211_SEQ_SUB(rxseq, rap->rxa_start);
902 if (off < rap->rxa_wnd) {
904 * Common case (hopefully): in the BA window.
905 * Sec 9.10.7.6.2 a) (p.137)
909 * Check for frames sitting too long in the reorder queue.
910 * This should only ever happen if frames are not delivered
911 * without the sender otherwise notifying us (e.g. with a
912 * BAR to move the window). Typically this happens because
913 * of vendor bugs that cause the sequence number to jump.
914 * When this happens we get a gap in the reorder queue that
915 * leaves frame sitting on the queue until they get pushed
916 * out due to window moves. When the vendor does not send
917 * BAR this move only happens due to explicit packet sends
919 * NB: we only track the time of the oldest frame in the
920 * reorder q; this means that if we flush we might push
921 * frames that still "new"; if this happens then subsequent
922 * frames will result in BA window moves which cost something
923 * but is still better than a big throughput dip.
925 if (rap->rxa_qframes != 0) {
926 /* XXX honor batimeout? */
927 if (ticks - rap->rxa_age > ieee80211_ampdu_age) {
929 * Too long since we received the first
930 * frame; flush the reorder buffer.
932 if (rap->rxa_qframes != 0) {
933 vap->iv_stats.is_ampdu_rx_age +=
935 ampdu_rx_flush(ni, rap);
937 rap->rxa_start = IEEE80211_SEQ_INC(rxseq);
942 * First frame, start aging timer.
944 rap->rxa_age = ticks;
948 if (rap->rxa_m[off] == NULL) {
951 rap->rxa_qbytes += m->m_pkthdr.len;
952 vap->iv_stats.is_ampdu_rx_reorder++;
954 IEEE80211_DISCARD_MAC(vap,
955 IEEE80211_MSG_INPUT | IEEE80211_MSG_11N,
956 ni->ni_macaddr, "a-mpdu duplicate",
957 "seqno %u tid %u BA win <%u:%u>",
958 rxseq, tid, rap->rxa_start,
959 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1));
960 vap->iv_stats.is_rx_dup++;
961 IEEE80211_NODE_STAT(ni, rx_dup);
966 if (off < IEEE80211_SEQ_BA_RANGE) {
968 * Outside the BA window, but within range;
969 * flush the reorder q and move the window.
970 * Sec 9.10.7.6.2 b) (p.138)
972 IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
973 "move BA win <%u:%u> (%u frames) rxseq %u tid %u",
975 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
976 rap->rxa_qframes, rxseq, tid);
977 vap->iv_stats.is_ampdu_rx_move++;
980 * The spec says to flush frames up to but not including:
981 * WinStart_B = rxseq - rap->rxa_wnd + 1
982 * Then insert the frame or notify the caller to process
983 * it immediately. We can safely do this by just starting
984 * over again because we know the frame will now be within
987 /* NB: rxa_wnd known to be >0 */
988 ampdu_rx_flush_upto(ni, rap,
989 IEEE80211_SEQ_SUB(rxseq, rap->rxa_wnd-1));
993 * Outside the BA window and out of range; toss.
994 * Sec 9.10.7.6.2 c) (p.138)
996 IEEE80211_DISCARD_MAC(vap,
997 IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
998 "MPDU", "BA win <%u:%u> (%u frames) rxseq %u tid %u%s",
1000 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
1001 rap->rxa_qframes, rxseq, tid,
1002 wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
1003 vap->iv_stats.is_ampdu_rx_drop++;
1004 IEEE80211_NODE_STAT(ni, rx_drop);
1013 * Process a BAR ctl frame. Dispatch all frames up to
1014 * the sequence number of the frame. If this frame is
1015 * out of range it's discarded.
1018 ieee80211_recv_bar(struct ieee80211_node *ni, struct mbuf *m0)
1020 struct ieee80211vap *vap = ni->ni_vap;
1021 struct ieee80211_frame_bar *wh;
1022 struct ieee80211_rx_ampdu *rap;
1023 ieee80211_seq rxseq;
1026 if (!ieee80211_recv_bar_ena) {
1028 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_11N,
1029 ni->ni_macaddr, "BAR", "%s", "processing disabled");
1031 vap->iv_stats.is_ampdu_bar_bad++;
1034 wh = mtod(m0, struct ieee80211_frame_bar *);
1035 /* XXX check basic BAR */
1036 tid = MS(le16toh(wh->i_ctl), IEEE80211_BAR_TID);
1037 rap = &ni->ni_rx_ampdu[tid];
1038 if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
1040 * No ADDBA request yet, don't touch.
1042 IEEE80211_DISCARD_MAC(vap,
1043 IEEE80211_MSG_INPUT | IEEE80211_MSG_11N,
1044 ni->ni_macaddr, "BAR", "no BA stream, tid %u", tid);
1045 vap->iv_stats.is_ampdu_bar_bad++;
1048 vap->iv_stats.is_ampdu_bar_rx++;
1049 rxseq = le16toh(wh->i_seq) >> IEEE80211_SEQ_SEQ_SHIFT;
1050 if (rxseq == rap->rxa_start)
1052 /* calculate offset in BA window */
1053 off = IEEE80211_SEQ_SUB(rxseq, rap->rxa_start);
1054 if (off < IEEE80211_SEQ_BA_RANGE) {
1056 * Flush the reorder q up to rxseq and move the window.
1057 * Sec 9.10.7.6.3 a) (p.138)
1059 IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
1060 "BAR moves BA win <%u:%u> (%u frames) rxseq %u tid %u",
1062 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
1063 rap->rxa_qframes, rxseq, tid);
1064 vap->iv_stats.is_ampdu_bar_move++;
1066 ampdu_rx_flush_upto(ni, rap, rxseq);
1067 if (off >= rap->rxa_wnd) {
1069 * BAR specifies a window start to the right of BA
1070 * window; we must move it explicitly since
1071 * ampdu_rx_flush_upto will not.
1073 rap->rxa_start = rxseq;
1077 * Out of range; toss.
1078 * Sec 9.10.7.6.3 b) (p.138)
1080 IEEE80211_DISCARD_MAC(vap,
1081 IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
1082 "BAR", "BA win <%u:%u> (%u frames) rxseq %u tid %u%s",
1084 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
1085 rap->rxa_qframes, rxseq, tid,
1086 wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
1087 vap->iv_stats.is_ampdu_bar_oow++;
1088 IEEE80211_NODE_STAT(ni, rx_drop);
1093 * Setup HT-specific state in a node. Called only
1094 * when HT use is negotiated so we don't do extra
1095 * work for temporary and/or legacy sta's.
1098 ieee80211_ht_node_init(struct ieee80211_node *ni)
1100 struct ieee80211_tx_ampdu *tap;
1103 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
1109 if (ni->ni_flags & IEEE80211_NODE_HT) {
1111 * Clean AMPDU state on re-associate. This handles the case
1112 * where a station leaves w/o notifying us and then returns
1113 * before node is reaped for inactivity.
1115 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
1117 "%s: calling cleanup (%p)",
1119 ieee80211_ht_node_cleanup(ni);
1121 for (tid = 0; tid < WME_NUM_TID; tid++) {
1122 tap = &ni->ni_tx_ampdu[tid];
1125 ieee80211_txampdu_init_pps(tap);
1126 /* NB: further initialization deferred */
1128 ni->ni_flags |= IEEE80211_NODE_HT | IEEE80211_NODE_AMPDU;
1132 * Cleanup HT-specific state in a node. Called only
1133 * when HT use has been marked.
1136 ieee80211_ht_node_cleanup(struct ieee80211_node *ni)
1138 struct ieee80211com *ic = ni->ni_ic;
1141 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
1146 KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT node"));
1148 /* XXX optimize this */
1149 for (i = 0; i < WME_NUM_TID; i++) {
1150 struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[i];
1151 if (tap->txa_flags & IEEE80211_AGGR_SETUP)
1154 for (i = 0; i < WME_NUM_TID; i++)
1155 ic->ic_ampdu_rx_stop(ni, &ni->ni_rx_ampdu[i]);
1158 ni->ni_flags &= ~IEEE80211_NODE_HT_ALL;
1162 * Age out HT resources for a station.
1165 ieee80211_ht_node_age(struct ieee80211_node *ni)
1167 struct ieee80211vap *vap = ni->ni_vap;
1170 KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT sta"));
1172 for (tid = 0; tid < WME_NUM_TID; tid++) {
1173 struct ieee80211_rx_ampdu *rap;
1175 rap = &ni->ni_rx_ampdu[tid];
1176 if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0)
1178 if (rap->rxa_qframes == 0)
1181 * Check for frames sitting too long in the reorder queue.
1182 * See above for more details on what's happening here.
1184 /* XXX honor batimeout? */
1185 if (ticks - rap->rxa_age > ieee80211_ampdu_age) {
1187 * Too long since we received the first
1188 * frame; flush the reorder buffer.
1190 vap->iv_stats.is_ampdu_rx_age += rap->rxa_qframes;
1191 ampdu_rx_flush(ni, rap);
1196 static struct ieee80211_channel *
1197 findhtchan(struct ieee80211com *ic, struct ieee80211_channel *c, int htflags)
1199 return ieee80211_find_channel(ic, c->ic_freq,
1200 (c->ic_flags &~ IEEE80211_CHAN_HT) | htflags);
1204 * Adjust a channel to be HT/non-HT according to the vap's configuration.
1206 struct ieee80211_channel *
1207 ieee80211_ht_adjust_channel(struct ieee80211com *ic,
1208 struct ieee80211_channel *chan, int flags)
1210 struct ieee80211_channel *c;
1212 if (flags & IEEE80211_FHT_HT) {
1213 /* promote to HT if possible */
1214 if (flags & IEEE80211_FHT_USEHT40) {
1215 if (!IEEE80211_IS_CHAN_HT40(chan)) {
1216 /* NB: arbitrarily pick ht40+ over ht40- */
1217 c = findhtchan(ic, chan, IEEE80211_CHAN_HT40U);
1219 c = findhtchan(ic, chan,
1220 IEEE80211_CHAN_HT40D);
1222 c = findhtchan(ic, chan,
1223 IEEE80211_CHAN_HT20);
1227 } else if (!IEEE80211_IS_CHAN_HT20(chan)) {
1228 c = findhtchan(ic, chan, IEEE80211_CHAN_HT20);
1232 } else if (IEEE80211_IS_CHAN_HT(chan)) {
1233 /* demote to legacy, HT use is disabled */
1234 c = ieee80211_find_channel(ic, chan->ic_freq,
1235 chan->ic_flags &~ IEEE80211_CHAN_HT);
1243 * Setup HT-specific state for a legacy WDS peer.
1246 ieee80211_ht_wds_init(struct ieee80211_node *ni)
1248 struct ieee80211vap *vap = ni->ni_vap;
1249 struct ieee80211_tx_ampdu *tap;
1252 KASSERT(vap->iv_flags_ht & IEEE80211_FHT_HT, ("no HT requested"));
1254 /* XXX check scan cache in case peer has an ap and we have info */
1256 * If setup with a legacy channel; locate an HT channel.
1257 * Otherwise if the inherited channel (from a companion
1258 * AP) is suitable use it so we use the same location
1259 * for the extension channel).
1261 ni->ni_chan = ieee80211_ht_adjust_channel(ni->ni_ic,
1262 ni->ni_chan, ieee80211_htchanflags(ni->ni_chan));
1265 if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20)
1266 ni->ni_htcap |= IEEE80211_HTCAP_SHORTGI20;
1267 if (IEEE80211_IS_CHAN_HT40(ni->ni_chan)) {
1268 ni->ni_htcap |= IEEE80211_HTCAP_CHWIDTH40;
1270 if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan))
1271 ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_ABOVE;
1272 else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan))
1273 ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_BELOW;
1274 if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40)
1275 ni->ni_htcap |= IEEE80211_HTCAP_SHORTGI40;
1278 ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_NONE;
1280 ni->ni_htctlchan = ni->ni_chan->ic_ieee;
1281 if (vap->iv_flags_ht & IEEE80211_FHT_RIFS)
1282 ni->ni_flags |= IEEE80211_NODE_RIFS;
1283 /* XXX does it make sense to enable SMPS? */
1285 ni->ni_htopmode = 0; /* XXX need protection state */
1286 ni->ni_htstbc = 0; /* XXX need info */
1288 for (tid = 0; tid < WME_NUM_TID; tid++) {
1289 tap = &ni->ni_tx_ampdu[tid];
1291 ieee80211_txampdu_init_pps(tap);
1293 /* NB: AMPDU tx/rx governed by IEEE80211_FHT_AMPDU_{TX,RX} */
1294 ni->ni_flags |= IEEE80211_NODE_HT | IEEE80211_NODE_AMPDU;
1298 * Notify hostap vaps of a change in the HTINFO ie.
1301 htinfo_notify(struct ieee80211com *ic)
1303 struct ieee80211vap *vap;
1306 IEEE80211_LOCK_ASSERT(ic);
1308 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1309 if (vap->iv_opmode != IEEE80211_M_HOSTAP)
1311 if (vap->iv_state != IEEE80211_S_RUN ||
1312 !IEEE80211_IS_CHAN_HT(vap->iv_bss->ni_chan))
1316 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N,
1318 "HT bss occupancy change: %d sta, %d ht, "
1319 "%d ht40%s, HT protmode now 0x%x"
1321 , ic->ic_ht_sta_assoc
1322 , ic->ic_ht40_sta_assoc
1323 , (ic->ic_flags_ht & IEEE80211_FHT_NONHT_PR) ?
1324 ", non-HT sta present" : ""
1325 , ic->ic_curhtprotmode);
1328 ieee80211_beacon_notify(vap, IEEE80211_BEACON_HTINFO);
1333 * Calculate HT protection mode from current
1334 * state and handle updates.
1337 htinfo_update(struct ieee80211com *ic)
1341 if (ic->ic_sta_assoc != ic->ic_ht_sta_assoc) {
1342 protmode = IEEE80211_HTINFO_OPMODE_MIXED
1343 | IEEE80211_HTINFO_NONHT_PRESENT;
1344 } else if (ic->ic_flags_ht & IEEE80211_FHT_NONHT_PR) {
1345 protmode = IEEE80211_HTINFO_OPMODE_PROTOPT
1346 | IEEE80211_HTINFO_NONHT_PRESENT;
1347 } else if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
1348 IEEE80211_IS_CHAN_HT40(ic->ic_bsschan) &&
1349 ic->ic_sta_assoc != ic->ic_ht40_sta_assoc) {
1350 protmode = IEEE80211_HTINFO_OPMODE_HT20PR;
1352 protmode = IEEE80211_HTINFO_OPMODE_PURE;
1354 if (protmode != ic->ic_curhtprotmode) {
1355 ic->ic_curhtprotmode = protmode;
1361 * Handle an HT station joining a BSS.
1364 ieee80211_ht_node_join(struct ieee80211_node *ni)
1366 struct ieee80211com *ic = ni->ni_ic;
1368 IEEE80211_LOCK_ASSERT(ic);
1370 if (ni->ni_flags & IEEE80211_NODE_HT) {
1371 ic->ic_ht_sta_assoc++;
1372 if (ni->ni_chw == 40)
1373 ic->ic_ht40_sta_assoc++;
1379 * Handle an HT station leaving a BSS.
1382 ieee80211_ht_node_leave(struct ieee80211_node *ni)
1384 struct ieee80211com *ic = ni->ni_ic;
1386 IEEE80211_LOCK_ASSERT(ic);
1388 if (ni->ni_flags & IEEE80211_NODE_HT) {
1389 ic->ic_ht_sta_assoc--;
1390 if (ni->ni_chw == 40)
1391 ic->ic_ht40_sta_assoc--;
1397 * Public version of htinfo_update; used for processing
1398 * beacon frames from overlapping bss.
1400 * Caller can specify either IEEE80211_HTINFO_OPMODE_MIXED
1401 * (on receipt of a beacon that advertises MIXED) or
1402 * IEEE80211_HTINFO_OPMODE_PROTOPT (on receipt of a beacon
1403 * from an overlapping legacy bss). We treat MIXED with
1404 * a higher precedence than PROTOPT (i.e. we will not change
1405 * change PROTOPT -> MIXED; only MIXED -> PROTOPT). This
1406 * corresponds to how we handle things in htinfo_update.
1409 ieee80211_htprot_update(struct ieee80211com *ic, int protmode)
1411 #define OPMODE(x) SM(x, IEEE80211_HTINFO_OPMODE)
1414 /* track non-HT station presence */
1415 KASSERT(protmode & IEEE80211_HTINFO_NONHT_PRESENT,
1416 ("protmode 0x%x", protmode));
1417 ic->ic_flags_ht |= IEEE80211_FHT_NONHT_PR;
1418 ic->ic_lastnonht = ticks;
1420 if (protmode != ic->ic_curhtprotmode &&
1421 (OPMODE(ic->ic_curhtprotmode) != IEEE80211_HTINFO_OPMODE_MIXED ||
1422 OPMODE(protmode) == IEEE80211_HTINFO_OPMODE_PROTOPT)) {
1423 /* push beacon update */
1424 ic->ic_curhtprotmode = protmode;
1427 IEEE80211_UNLOCK(ic);
1432 * Time out presence of an overlapping bss with non-HT
1433 * stations. When operating in hostap mode we listen for
1434 * beacons from other stations and if we identify a non-HT
1435 * station is present we update the opmode field of the
1436 * HTINFO ie. To identify when all non-HT stations are
1437 * gone we time out this condition.
1440 ieee80211_ht_timeout(struct ieee80211com *ic)
1442 IEEE80211_LOCK_ASSERT(ic);
1444 if ((ic->ic_flags_ht & IEEE80211_FHT_NONHT_PR) &&
1445 ieee80211_time_after(ticks, ic->ic_lastnonht + IEEE80211_NONHT_PRESENT_AGE)) {
1447 IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
1448 "%s", "time out non-HT STA present on channel");
1450 ic->ic_flags_ht &= ~IEEE80211_FHT_NONHT_PR;
1456 * Process an 802.11n HT capabilities ie.
1459 ieee80211_parse_htcap(struct ieee80211_node *ni, const uint8_t *ie)
1461 if (ie[0] == IEEE80211_ELEMID_VENDOR) {
1463 * Station used Vendor OUI ie to associate;
1464 * mark the node so when we respond we'll use
1465 * the Vendor OUI's and not the standard ie's.
1467 ni->ni_flags |= IEEE80211_NODE_HTCOMPAT;
1470 ni->ni_flags &= ~IEEE80211_NODE_HTCOMPAT;
1472 ni->ni_htcap = le16dec(ie +
1473 __offsetof(struct ieee80211_ie_htcap, hc_cap));
1474 ni->ni_htparam = ie[__offsetof(struct ieee80211_ie_htcap, hc_param)];
1478 htinfo_parse(struct ieee80211_node *ni,
1479 const struct ieee80211_ie_htinfo *htinfo)
1483 ni->ni_htctlchan = htinfo->hi_ctrlchannel;
1484 ni->ni_ht2ndchan = SM(htinfo->hi_byte1, IEEE80211_HTINFO_2NDCHAN);
1485 w = le16dec(&htinfo->hi_byte2);
1486 ni->ni_htopmode = SM(w, IEEE80211_HTINFO_OPMODE);
1487 w = le16dec(&htinfo->hi_byte45);
1488 ni->ni_htstbc = SM(w, IEEE80211_HTINFO_BASIC_STBCMCS);
1492 * Parse an 802.11n HT info ie and save useful information
1493 * to the node state. Note this does not effect any state
1494 * changes such as for channel width change.
1497 ieee80211_parse_htinfo(struct ieee80211_node *ni, const uint8_t *ie)
1499 if (ie[0] == IEEE80211_ELEMID_VENDOR)
1501 htinfo_parse(ni, (const struct ieee80211_ie_htinfo *) ie);
1505 * Handle 11n/11ac channel switch.
1507 * Use the received HT/VHT ie's to identify the right channel to use.
1508 * If we cannot locate it in the channel table then fallback to
1511 * Note that we use this information to identify the node's
1512 * channel only; the caller is responsible for insuring any
1513 * required channel change is done (e.g. in sta mode when
1514 * parsing the contents of a beacon frame).
1517 htinfo_update_chw(struct ieee80211_node *ni, int htflags, int vhtflags)
1519 struct ieee80211com *ic = ni->ni_ic;
1520 struct ieee80211_channel *c;
1525 * First step - do HT/VHT only channel lookup based on operating mode
1526 * flags. This involves masking out the VHT flags as well.
1527 * Otherwise we end up doing the full channel walk each time
1528 * we trigger this, which is expensive.
1530 chanflags = (ni->ni_chan->ic_flags &~
1531 (IEEE80211_CHAN_HT | IEEE80211_CHAN_VHT)) | htflags | vhtflags;
1533 if (chanflags == ni->ni_chan->ic_flags)
1537 * If HT /or/ VHT flags have changed then check both.
1538 * We need to start by picking a HT channel anyway.
1542 chanflags = (ni->ni_chan->ic_flags &~
1543 (IEEE80211_CHAN_HT | IEEE80211_CHAN_VHT)) | htflags;
1544 /* XXX not right for ht40- */
1545 c = ieee80211_find_channel(ic, ni->ni_chan->ic_freq, chanflags);
1546 if (c == NULL && (htflags & IEEE80211_CHAN_HT40)) {
1548 * No HT40 channel entry in our table; fall back
1549 * to HT20 operation. This should not happen.
1551 c = findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT20);
1553 IEEE80211_NOTE(ni->ni_vap,
1554 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
1555 "no HT40 channel (freq %u), falling back to HT20",
1556 ni->ni_chan->ic_freq);
1561 /* Nothing found - leave it alone; move onto VHT */
1566 * If it's non-HT, then bail out now.
1568 if (! IEEE80211_IS_CHAN_HT(c)) {
1569 IEEE80211_NOTE(ni->ni_vap,
1570 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
1571 "not HT; skipping VHT check (%u/0x%x)",
1572 c->ic_freq, c->ic_flags);
1577 * Next step - look at the current VHT flags and determine
1578 * if we need to upgrade. Mask out the VHT and HT flags since
1579 * the vhtflags field will already have the correct HT
1582 if (IEEE80211_CONF_VHT(ic) && ni->ni_vhtcap != 0 && vhtflags != 0) {
1583 chanflags = (c->ic_flags
1584 &~ (IEEE80211_CHAN_HT | IEEE80211_CHAN_VHT))
1586 IEEE80211_NOTE(ni->ni_vap,
1587 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N,
1589 "%s: VHT; chanwidth=0x%02x; vhtflags=0x%08x",
1590 __func__, ni->ni_vht_chanwidth, vhtflags);
1592 IEEE80211_NOTE(ni->ni_vap,
1593 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N,
1595 "%s: VHT; trying lookup for %d/0x%08x",
1596 __func__, c->ic_freq, chanflags);
1597 c = ieee80211_find_channel(ic, c->ic_freq, chanflags);
1600 /* Finally, if it's changed */
1601 if (c != NULL && c != ni->ni_chan) {
1602 IEEE80211_NOTE(ni->ni_vap,
1603 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
1604 "switch station to %s%d channel %u/0x%x",
1605 IEEE80211_IS_CHAN_VHT(c) ? "VHT" : "HT",
1606 IEEE80211_IS_CHAN_VHT80(c) ? 80 :
1607 (IEEE80211_IS_CHAN_HT40(c) ? 40 : 20),
1608 c->ic_freq, c->ic_flags);
1612 /* NB: caller responsible for forcing any channel change */
1615 /* update node's (11n) tx channel width */
1616 ni->ni_chw = IEEE80211_IS_CHAN_HT40(ni->ni_chan)? 40 : 20;
1621 * Update 11n MIMO PS state according to received htcap.
1624 htcap_update_mimo_ps(struct ieee80211_node *ni)
1626 uint16_t oflags = ni->ni_flags;
1628 switch (ni->ni_htcap & IEEE80211_HTCAP_SMPS) {
1629 case IEEE80211_HTCAP_SMPS_DYNAMIC:
1630 ni->ni_flags |= IEEE80211_NODE_MIMO_PS;
1631 ni->ni_flags |= IEEE80211_NODE_MIMO_RTS;
1633 case IEEE80211_HTCAP_SMPS_ENA:
1634 ni->ni_flags |= IEEE80211_NODE_MIMO_PS;
1635 ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS;
1637 case IEEE80211_HTCAP_SMPS_OFF:
1638 default: /* disable on rx of reserved value */
1639 ni->ni_flags &= ~IEEE80211_NODE_MIMO_PS;
1640 ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS;
1643 return (oflags ^ ni->ni_flags);
1647 * Update short GI state according to received htcap
1648 * and local settings.
1650 static __inline void
1651 htcap_update_shortgi(struct ieee80211_node *ni)
1653 struct ieee80211vap *vap = ni->ni_vap;
1655 ni->ni_flags &= ~(IEEE80211_NODE_SGI20|IEEE80211_NODE_SGI40);
1656 if ((ni->ni_htcap & IEEE80211_HTCAP_SHORTGI20) &&
1657 (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20))
1658 ni->ni_flags |= IEEE80211_NODE_SGI20;
1659 if ((ni->ni_htcap & IEEE80211_HTCAP_SHORTGI40) &&
1660 (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40))
1661 ni->ni_flags |= IEEE80211_NODE_SGI40;
1665 * Update LDPC state according to received htcap
1666 * and local settings.
1668 static __inline void
1669 htcap_update_ldpc(struct ieee80211_node *ni)
1671 struct ieee80211vap *vap = ni->ni_vap;
1673 if ((ni->ni_htcap & IEEE80211_HTCAP_LDPC) &&
1674 (vap->iv_flags_ht & IEEE80211_FHT_LDPC_TX))
1675 ni->ni_flags |= IEEE80211_NODE_LDPC;
1679 * Parse and update HT-related state extracted from
1680 * the HT cap and info ie's.
1682 * This is called from the STA management path and
1683 * the ieee80211_node_join() path. It will take into
1684 * account the IEs discovered during scanning and
1685 * adjust things accordingly.
1688 ieee80211_ht_updateparams(struct ieee80211_node *ni,
1689 const uint8_t *htcapie, const uint8_t *htinfoie)
1691 struct ieee80211vap *vap = ni->ni_vap;
1692 const struct ieee80211_ie_htinfo *htinfo;
1694 ieee80211_parse_htcap(ni, htcapie);
1695 if (vap->iv_htcaps & IEEE80211_HTCAP_SMPS)
1696 htcap_update_mimo_ps(ni);
1697 htcap_update_shortgi(ni);
1698 htcap_update_ldpc(ni);
1700 if (htinfoie[0] == IEEE80211_ELEMID_VENDOR)
1702 htinfo = (const struct ieee80211_ie_htinfo *) htinfoie;
1703 htinfo_parse(ni, htinfo);
1706 * Defer the node channel change; we need to now
1707 * update VHT parameters before we do it.
1710 if ((htinfo->hi_byte1 & IEEE80211_HTINFO_RIFSMODE_PERM) &&
1711 (vap->iv_flags_ht & IEEE80211_FHT_RIFS))
1712 ni->ni_flags |= IEEE80211_NODE_RIFS;
1714 ni->ni_flags &= ~IEEE80211_NODE_RIFS;
1718 ieee80211_vht_get_vhtflags(struct ieee80211_node *ni, uint32_t htflags)
1720 struct ieee80211vap *vap = ni->ni_vap;
1721 uint32_t vhtflags = 0;
1724 if (ni->ni_flags & IEEE80211_NODE_VHT && vap->iv_flags_vht & IEEE80211_FVHT_VHT) {
1725 if ((ni->ni_vht_chanwidth == IEEE80211_VHT_CHANWIDTH_160MHZ) &&
1726 /* XXX 2 means "160MHz and 80+80MHz", 1 means "160MHz" */
1727 (MS(vap->iv_vhtcaps,
1728 IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_MASK) >= 1) &&
1729 (vap->iv_flags_vht & IEEE80211_FVHT_USEVHT160)) {
1730 vhtflags = IEEE80211_CHAN_VHT160;
1731 /* Mirror the HT40 flags */
1732 if (htflags == IEEE80211_CHAN_HT40U) {
1733 vhtflags |= IEEE80211_CHAN_HT40U;
1734 } else if (htflags == IEEE80211_CHAN_HT40D) {
1735 vhtflags |= IEEE80211_CHAN_HT40D;
1737 } else if ((ni->ni_vht_chanwidth == IEEE80211_VHT_CHANWIDTH_80P80MHZ) &&
1738 /* XXX 2 means "160MHz and 80+80MHz" */
1739 (MS(vap->iv_vhtcaps,
1740 IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_MASK) == 2) &&
1741 (vap->iv_flags_vht & IEEE80211_FVHT_USEVHT80P80)) {
1742 vhtflags = IEEE80211_CHAN_VHT80_80;
1743 /* Mirror the HT40 flags */
1744 if (htflags == IEEE80211_CHAN_HT40U) {
1745 vhtflags |= IEEE80211_CHAN_HT40U;
1746 } else if (htflags == IEEE80211_CHAN_HT40D) {
1747 vhtflags |= IEEE80211_CHAN_HT40D;
1749 } else if ((ni->ni_vht_chanwidth == IEEE80211_VHT_CHANWIDTH_80MHZ) &&
1750 (vap->iv_flags_vht & IEEE80211_FVHT_USEVHT80)) {
1751 vhtflags = IEEE80211_CHAN_VHT80;
1752 /* Mirror the HT40 flags */
1753 if (htflags == IEEE80211_CHAN_HT40U) {
1754 vhtflags |= IEEE80211_CHAN_HT40U;
1755 } else if (htflags == IEEE80211_CHAN_HT40D) {
1756 vhtflags |= IEEE80211_CHAN_HT40D;
1758 } else if (ni->ni_vht_chanwidth == IEEE80211_VHT_CHANWIDTH_USE_HT) {
1759 /* Mirror the HT40 flags */
1761 * XXX TODO: if ht40 is disabled, but vht40 isn't
1762 * disabled then this logic will get very, very sad.
1763 * It's quite possible the only sane thing to do is
1764 * to not have vht40 as an option, and just obey
1765 * 'ht40' as that flag.
1767 if ((htflags == IEEE80211_CHAN_HT40U) &&
1768 (vap->iv_flags_vht & IEEE80211_FVHT_USEVHT40)) {
1769 vhtflags = IEEE80211_CHAN_VHT40U
1770 | IEEE80211_CHAN_HT40U;
1771 } else if (htflags == IEEE80211_CHAN_HT40D &&
1772 (vap->iv_flags_vht & IEEE80211_FVHT_USEVHT40)) {
1773 vhtflags = IEEE80211_CHAN_VHT40D
1774 | IEEE80211_CHAN_HT40D;
1775 } else if (htflags == IEEE80211_CHAN_HT20) {
1776 vhtflags = IEEE80211_CHAN_VHT20
1777 | IEEE80211_CHAN_HT20;
1780 vhtflags = IEEE80211_CHAN_VHT20;
1787 * Final part of updating the HT parameters.
1789 * This is called from the STA management path and
1790 * the ieee80211_node_join() path. It will take into
1791 * account the IEs discovered during scanning and
1792 * adjust things accordingly.
1794 * This is done after a call to ieee80211_ht_updateparams()
1795 * because it (and the upcoming VHT version of updateparams)
1796 * needs to ensure everything is parsed before htinfo_update_chw()
1797 * is called - which will change the channel config for the
1801 ieee80211_ht_updateparams_final(struct ieee80211_node *ni,
1802 const uint8_t *htcapie, const uint8_t *htinfoie)
1804 struct ieee80211vap *vap = ni->ni_vap;
1805 const struct ieee80211_ie_htinfo *htinfo;
1806 int htflags, vhtflags;
1809 htinfo = (const struct ieee80211_ie_htinfo *) htinfoie;
1811 htflags = (vap->iv_flags_ht & IEEE80211_FHT_HT) ?
1812 IEEE80211_CHAN_HT20 : 0;
1814 /* NB: honor operating mode constraint */
1815 if ((htinfo->hi_byte1 & IEEE80211_HTINFO_TXWIDTH_2040) &&
1816 (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)) {
1817 if (ni->ni_ht2ndchan == IEEE80211_HTINFO_2NDCHAN_ABOVE)
1818 htflags = IEEE80211_CHAN_HT40U;
1819 else if (ni->ni_ht2ndchan == IEEE80211_HTINFO_2NDCHAN_BELOW)
1820 htflags = IEEE80211_CHAN_HT40D;
1824 * VHT flags - do much the same; check whether VHT is available
1825 * and if so, what our ideal channel use would be based on our
1826 * capabilities and the (pre-parsed) VHT info IE.
1828 vhtflags = ieee80211_vht_get_vhtflags(ni, htflags);
1830 if (htinfo_update_chw(ni, htflags, vhtflags))
1837 * Parse and update HT-related state extracted from the HT cap ie
1838 * for a station joining an HT BSS.
1840 * This is called from the hostap path for each station.
1843 ieee80211_ht_updatehtcap(struct ieee80211_node *ni, const uint8_t *htcapie)
1845 struct ieee80211vap *vap = ni->ni_vap;
1847 ieee80211_parse_htcap(ni, htcapie);
1848 if (vap->iv_htcaps & IEEE80211_HTCAP_SMPS)
1849 htcap_update_mimo_ps(ni);
1850 htcap_update_shortgi(ni);
1851 htcap_update_ldpc(ni);
1855 * Called once HT and VHT capabilities are parsed in hostap mode -
1856 * this will adjust the channel configuration of the given node
1857 * based on the configuration and capabilities.
1860 ieee80211_ht_updatehtcap_final(struct ieee80211_node *ni)
1862 struct ieee80211vap *vap = ni->ni_vap;
1866 /* NB: honor operating mode constraint */
1867 /* XXX 40 MHz intolerant */
1868 htflags = (vap->iv_flags_ht & IEEE80211_FHT_HT) ?
1869 IEEE80211_CHAN_HT20 : 0;
1870 if ((ni->ni_htcap & IEEE80211_HTCAP_CHWIDTH40) &&
1871 (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)) {
1872 if (IEEE80211_IS_CHAN_HT40U(vap->iv_bss->ni_chan))
1873 htflags = IEEE80211_CHAN_HT40U;
1874 else if (IEEE80211_IS_CHAN_HT40D(vap->iv_bss->ni_chan))
1875 htflags = IEEE80211_CHAN_HT40D;
1878 * VHT flags - do much the same; check whether VHT is available
1879 * and if so, what our ideal channel use would be based on our
1880 * capabilities and the (pre-parsed) VHT info IE.
1882 vhtflags = ieee80211_vht_get_vhtflags(ni, htflags);
1884 (void) htinfo_update_chw(ni, htflags, vhtflags);
1888 * Install received HT rate set by parsing the HT cap ie.
1891 ieee80211_setup_htrates(struct ieee80211_node *ni, const uint8_t *ie, int flags)
1893 struct ieee80211com *ic = ni->ni_ic;
1894 struct ieee80211vap *vap = ni->ni_vap;
1895 const struct ieee80211_ie_htcap *htcap;
1896 struct ieee80211_htrateset *rs;
1897 int i, maxequalmcs, maxunequalmcs;
1899 maxequalmcs = ic->ic_txstream * 8 - 1;
1901 if (ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL) {
1902 if (ic->ic_txstream >= 2)
1904 if (ic->ic_txstream >= 3)
1906 if (ic->ic_txstream >= 4)
1910 rs = &ni->ni_htrates;
1911 memset(rs, 0, sizeof(*rs));
1913 if (ie[0] == IEEE80211_ELEMID_VENDOR)
1915 htcap = (const struct ieee80211_ie_htcap *) ie;
1916 for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++) {
1917 if (isclr(htcap->hc_mcsset, i))
1919 if (rs->rs_nrates == IEEE80211_HTRATE_MAXSIZE) {
1921 IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
1922 "WARNING, HT rate set too large; only "
1923 "using %u rates", IEEE80211_HTRATE_MAXSIZE);
1924 vap->iv_stats.is_rx_rstoobig++;
1927 if (i <= 31 && i > maxequalmcs)
1930 (ic->ic_htcaps & IEEE80211_HTC_TXMCS32) == 0)
1932 if (i > 32 && i > maxunequalmcs)
1934 rs->rs_rates[rs->rs_nrates++] = i;
1937 return ieee80211_fix_rate(ni, (struct ieee80211_rateset *) rs, flags);
1941 * Mark rates in a node's HT rate set as basic according
1942 * to the information in the supplied HT info ie.
1945 ieee80211_setup_basic_htrates(struct ieee80211_node *ni, const uint8_t *ie)
1947 const struct ieee80211_ie_htinfo *htinfo;
1948 struct ieee80211_htrateset *rs;
1951 if (ie[0] == IEEE80211_ELEMID_VENDOR)
1953 htinfo = (const struct ieee80211_ie_htinfo *) ie;
1954 rs = &ni->ni_htrates;
1955 if (rs->rs_nrates == 0) {
1956 IEEE80211_NOTE(ni->ni_vap,
1957 IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
1958 "%s", "WARNING, empty HT rate set");
1961 for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++) {
1962 if (isclr(htinfo->hi_basicmcsset, i))
1964 for (j = 0; j < rs->rs_nrates; j++)
1965 if ((rs->rs_rates[j] & IEEE80211_RATE_VAL) == i)
1966 rs->rs_rates[j] |= IEEE80211_RATE_BASIC;
1971 ampdu_tx_setup(struct ieee80211_tx_ampdu *tap)
1973 callout_init(&tap->txa_timer, 1);
1974 tap->txa_flags |= IEEE80211_AGGR_SETUP;
1975 tap->txa_lastsample = ticks;
1979 ampdu_tx_stop(struct ieee80211_tx_ampdu *tap)
1981 struct ieee80211_node *ni = tap->txa_ni;
1982 struct ieee80211com *ic = ni->ni_ic;
1984 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
1989 KASSERT(tap->txa_flags & IEEE80211_AGGR_SETUP,
1990 ("txa_flags 0x%x tid %d ac %d", tap->txa_flags, tap->txa_tid,
1991 TID_TO_WME_AC(tap->txa_tid)));
1994 * Stop BA stream if setup so driver has a chance
1995 * to reclaim any resources it might have allocated.
1997 ic->ic_addba_stop(ni, tap);
1999 * Stop any pending BAR transmit.
2001 bar_stop_timer(tap);
2004 * Reset packet estimate.
2006 ieee80211_txampdu_init_pps(tap);
2008 /* NB: clearing NAK means we may re-send ADDBA */
2009 tap->txa_flags &= ~(IEEE80211_AGGR_SETUP | IEEE80211_AGGR_NAK);
2013 * ADDBA response timeout.
2015 * If software aggregation and per-TID queue management was done here,
2016 * that queue would be unpaused after the ADDBA timeout occurs.
2019 addba_timeout(void *arg)
2021 struct ieee80211_tx_ampdu *tap = arg;
2022 struct ieee80211_node *ni = tap->txa_ni;
2023 struct ieee80211com *ic = ni->ni_ic;
2026 tap->txa_flags &= ~IEEE80211_AGGR_XCHGPEND;
2027 tap->txa_attempts++;
2028 ic->ic_addba_response_timeout(ni, tap);
2032 addba_start_timeout(struct ieee80211_tx_ampdu *tap)
2034 /* XXX use CALLOUT_PENDING instead? */
2035 callout_reset(&tap->txa_timer, ieee80211_addba_timeout,
2036 addba_timeout, tap);
2037 tap->txa_flags |= IEEE80211_AGGR_XCHGPEND;
2038 tap->txa_nextrequest = ticks + ieee80211_addba_timeout;
2042 addba_stop_timeout(struct ieee80211_tx_ampdu *tap)
2044 /* XXX use CALLOUT_PENDING instead? */
2045 if (tap->txa_flags & IEEE80211_AGGR_XCHGPEND) {
2046 callout_stop(&tap->txa_timer);
2047 tap->txa_flags &= ~IEEE80211_AGGR_XCHGPEND;
2052 null_addba_response_timeout(struct ieee80211_node *ni,
2053 struct ieee80211_tx_ampdu *tap)
2058 * Default method for requesting A-MPDU tx aggregation.
2059 * We setup the specified state block and start a timer
2060 * to wait for an ADDBA response frame.
2063 ieee80211_addba_request(struct ieee80211_node *ni,
2064 struct ieee80211_tx_ampdu *tap,
2065 int dialogtoken, int baparamset, int batimeout)
2070 tap->txa_token = dialogtoken;
2071 tap->txa_flags |= IEEE80211_AGGR_IMMEDIATE;
2072 bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
2073 tap->txa_wnd = (bufsiz == 0) ?
2074 IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
2075 addba_start_timeout(tap);
2080 * Called by drivers that wish to request an ADDBA session be
2081 * setup. This brings it up and starts the request timer.
2084 ieee80211_ampdu_tx_request_ext(struct ieee80211_node *ni, int tid)
2086 struct ieee80211_tx_ampdu *tap;
2088 if (tid < 0 || tid > 15)
2090 tap = &ni->ni_tx_ampdu[tid];
2093 if ((tap->txa_flags & IEEE80211_AGGR_SETUP) == 0) {
2094 /* do deferred setup of state */
2095 ampdu_tx_setup(tap);
2097 /* XXX hack for not doing proper locking */
2098 tap->txa_flags &= ~IEEE80211_AGGR_NAK;
2099 addba_start_timeout(tap);
2104 * Called by drivers that have marked a session as active.
2107 ieee80211_ampdu_tx_request_active_ext(struct ieee80211_node *ni, int tid,
2110 struct ieee80211_tx_ampdu *tap;
2112 if (tid < 0 || tid > 15)
2114 tap = &ni->ni_tx_ampdu[tid];
2117 addba_stop_timeout(tap);
2119 tap->txa_flags |= IEEE80211_AGGR_RUNNING;
2120 tap->txa_attempts = 0;
2122 /* mark tid so we don't try again */
2123 tap->txa_flags |= IEEE80211_AGGR_NAK;
2129 * Default method for processing an A-MPDU tx aggregation
2130 * response. We shutdown any pending timer and update the
2131 * state block according to the reply.
2134 ieee80211_addba_response(struct ieee80211_node *ni,
2135 struct ieee80211_tx_ampdu *tap,
2136 int status, int baparamset, int batimeout)
2141 addba_stop_timeout(tap);
2142 if (status == IEEE80211_STATUS_SUCCESS) {
2143 bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
2144 /* XXX override our request? */
2145 tap->txa_wnd = (bufsiz == 0) ?
2146 IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
2148 tid = MS(baparamset, IEEE80211_BAPS_TID);
2149 tap->txa_flags |= IEEE80211_AGGR_RUNNING;
2150 tap->txa_attempts = 0;
2152 /* mark tid so we don't try again */
2153 tap->txa_flags |= IEEE80211_AGGR_NAK;
2159 * Default method for stopping A-MPDU tx aggregation.
2160 * Any timer is cleared and we drain any pending frames.
2163 ieee80211_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
2166 addba_stop_timeout(tap);
2167 if (tap->txa_flags & IEEE80211_AGGR_RUNNING) {
2168 /* XXX clear aggregation queue */
2169 tap->txa_flags &= ~IEEE80211_AGGR_RUNNING;
2171 tap->txa_attempts = 0;
2175 * Process a received action frame using the default aggregation
2176 * policy. We intercept ADDBA-related frames and use them to
2177 * update our aggregation state. All other frames are passed up
2178 * for processing by ieee80211_recv_action.
2181 ht_recv_action_ba_addba_request(struct ieee80211_node *ni,
2182 const struct ieee80211_frame *wh,
2183 const uint8_t *frm, const uint8_t *efrm)
2185 struct ieee80211com *ic = ni->ni_ic;
2186 struct ieee80211vap *vap = ni->ni_vap;
2187 struct ieee80211_rx_ampdu *rap;
2188 uint8_t dialogtoken;
2189 uint16_t baparamset, batimeout, baseqctl;
2193 dialogtoken = frm[2];
2194 baparamset = le16dec(frm+3);
2195 batimeout = le16dec(frm+5);
2196 baseqctl = le16dec(frm+7);
2198 tid = MS(baparamset, IEEE80211_BAPS_TID);
2200 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2201 "recv ADDBA request: dialogtoken %u baparamset 0x%x "
2202 "(tid %d bufsiz %d) batimeout %d baseqctl %d:%d",
2203 dialogtoken, baparamset,
2204 tid, MS(baparamset, IEEE80211_BAPS_BUFSIZ),
2206 MS(baseqctl, IEEE80211_BASEQ_START),
2207 MS(baseqctl, IEEE80211_BASEQ_FRAG));
2209 rap = &ni->ni_rx_ampdu[tid];
2211 /* Send ADDBA response */
2212 args[0] = dialogtoken;
2214 * NB: We ack only if the sta associated with HT and
2215 * the ap is configured to do AMPDU rx (the latter
2216 * violates the 11n spec and is mostly for testing).
2218 if ((ni->ni_flags & IEEE80211_NODE_AMPDU_RX) &&
2219 (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_RX)) {
2220 /* XXX handle ampdu_rx_start failure */
2221 ic->ic_ampdu_rx_start(ni, rap,
2222 baparamset, batimeout, baseqctl);
2224 args[1] = IEEE80211_STATUS_SUCCESS;
2226 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2227 ni, "reject ADDBA request: %s",
2228 ni->ni_flags & IEEE80211_NODE_AMPDU_RX ?
2229 "administratively disabled" :
2230 "not negotiated for station");
2231 vap->iv_stats.is_addba_reject++;
2232 args[1] = IEEE80211_STATUS_UNSPECIFIED;
2234 /* XXX honor rap flags? */
2235 args[2] = IEEE80211_BAPS_POLICY_IMMEDIATE
2236 | SM(tid, IEEE80211_BAPS_TID)
2237 | SM(rap->rxa_wnd, IEEE80211_BAPS_BUFSIZ)
2241 ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
2242 IEEE80211_ACTION_BA_ADDBA_RESPONSE, args);
2247 ht_recv_action_ba_addba_response(struct ieee80211_node *ni,
2248 const struct ieee80211_frame *wh,
2249 const uint8_t *frm, const uint8_t *efrm)
2251 struct ieee80211com *ic = ni->ni_ic;
2252 struct ieee80211vap *vap = ni->ni_vap;
2253 struct ieee80211_tx_ampdu *tap;
2254 uint8_t dialogtoken, policy;
2255 uint16_t baparamset, batimeout, code;
2258 dialogtoken = frm[2];
2259 code = le16dec(frm+3);
2260 baparamset = le16dec(frm+5);
2261 tid = MS(baparamset, IEEE80211_BAPS_TID);
2262 bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
2263 policy = MS(baparamset, IEEE80211_BAPS_POLICY);
2264 batimeout = le16dec(frm+7);
2266 tap = &ni->ni_tx_ampdu[tid];
2267 if ((tap->txa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
2268 IEEE80211_DISCARD_MAC(vap,
2269 IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2270 ni->ni_macaddr, "ADDBA response",
2271 "no pending ADDBA, tid %d dialogtoken %u "
2272 "code %d", tid, dialogtoken, code);
2273 vap->iv_stats.is_addba_norequest++;
2276 if (dialogtoken != tap->txa_token) {
2277 IEEE80211_DISCARD_MAC(vap,
2278 IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2279 ni->ni_macaddr, "ADDBA response",
2280 "dialogtoken mismatch: waiting for %d, "
2281 "received %d, tid %d code %d",
2282 tap->txa_token, dialogtoken, tid, code);
2283 vap->iv_stats.is_addba_badtoken++;
2286 /* NB: assumes IEEE80211_AGGR_IMMEDIATE is 1 */
2287 if (policy != (tap->txa_flags & IEEE80211_AGGR_IMMEDIATE)) {
2288 IEEE80211_DISCARD_MAC(vap,
2289 IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2290 ni->ni_macaddr, "ADDBA response",
2291 "policy mismatch: expecting %s, "
2292 "received %s, tid %d code %d",
2293 tap->txa_flags & IEEE80211_AGGR_IMMEDIATE,
2295 vap->iv_stats.is_addba_badpolicy++;
2299 /* XXX we take MIN in ieee80211_addba_response */
2300 if (bufsiz > IEEE80211_AGGR_BAWMAX) {
2301 IEEE80211_DISCARD_MAC(vap,
2302 IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2303 ni->ni_macaddr, "ADDBA response",
2304 "BA window too large: max %d, "
2305 "received %d, tid %d code %d",
2306 bufsiz, IEEE80211_AGGR_BAWMAX, tid, code);
2307 vap->iv_stats.is_addba_badbawinsize++;
2311 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2312 "recv ADDBA response: dialogtoken %u code %d "
2313 "baparamset 0x%x (tid %d bufsiz %d) batimeout %d",
2314 dialogtoken, code, baparamset, tid, bufsiz,
2316 ic->ic_addba_response(ni, tap, code, baparamset, batimeout);
2321 ht_recv_action_ba_delba(struct ieee80211_node *ni,
2322 const struct ieee80211_frame *wh,
2323 const uint8_t *frm, const uint8_t *efrm)
2325 struct ieee80211com *ic = ni->ni_ic;
2326 struct ieee80211_rx_ampdu *rap;
2327 struct ieee80211_tx_ampdu *tap;
2328 uint16_t baparamset, code;
2331 baparamset = le16dec(frm+2);
2332 code = le16dec(frm+4);
2334 tid = MS(baparamset, IEEE80211_DELBAPS_TID);
2336 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2337 "recv DELBA: baparamset 0x%x (tid %d initiator %d) "
2338 "code %d", baparamset, tid,
2339 MS(baparamset, IEEE80211_DELBAPS_INIT), code);
2341 if ((baparamset & IEEE80211_DELBAPS_INIT) == 0) {
2342 tap = &ni->ni_tx_ampdu[tid];
2343 ic->ic_addba_stop(ni, tap);
2345 rap = &ni->ni_rx_ampdu[tid];
2346 ic->ic_ampdu_rx_stop(ni, rap);
2352 ht_recv_action_ht_txchwidth(struct ieee80211_node *ni,
2353 const struct ieee80211_frame *wh,
2354 const uint8_t *frm, const uint8_t *efrm)
2358 chw = (frm[2] == IEEE80211_A_HT_TXCHWIDTH_2040) ? 40 : 20;
2360 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2361 "%s: HT txchwidth, width %d%s",
2362 __func__, chw, ni->ni_chw != chw ? "*" : "");
2363 if (chw != ni->ni_chw) {
2364 /* XXX does this need to change the ht40 station count? */
2366 /* XXX notify on change */
2372 ht_recv_action_ht_mimopwrsave(struct ieee80211_node *ni,
2373 const struct ieee80211_frame *wh,
2374 const uint8_t *frm, const uint8_t *efrm)
2376 const struct ieee80211_action_ht_mimopowersave *mps =
2377 (const struct ieee80211_action_ht_mimopowersave *) frm;
2379 /* XXX check iv_htcaps */
2380 if (mps->am_control & IEEE80211_A_HT_MIMOPWRSAVE_ENA)
2381 ni->ni_flags |= IEEE80211_NODE_MIMO_PS;
2383 ni->ni_flags &= ~IEEE80211_NODE_MIMO_PS;
2384 if (mps->am_control & IEEE80211_A_HT_MIMOPWRSAVE_MODE)
2385 ni->ni_flags |= IEEE80211_NODE_MIMO_RTS;
2387 ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS;
2388 /* XXX notify on change */
2389 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2390 "%s: HT MIMO PS (%s%s)", __func__,
2391 (ni->ni_flags & IEEE80211_NODE_MIMO_PS) ? "on" : "off",
2392 (ni->ni_flags & IEEE80211_NODE_MIMO_RTS) ? "+rts" : ""
2398 * Transmit processing.
2402 * Check if A-MPDU should be requested/enabled for a stream.
2403 * We require a traffic rate above a per-AC threshold and we
2404 * also handle backoff from previous failed attempts.
2406 * Drivers may override this method to bring in information
2407 * such as link state conditions in making the decision.
2410 ieee80211_ampdu_enable(struct ieee80211_node *ni,
2411 struct ieee80211_tx_ampdu *tap)
2413 struct ieee80211vap *vap = ni->ni_vap;
2415 if (tap->txa_avgpps <
2416 vap->iv_ampdu_mintraffic[TID_TO_WME_AC(tap->txa_tid)])
2418 /* XXX check rssi? */
2419 if (tap->txa_attempts >= ieee80211_addba_maxtries &&
2420 ieee80211_time_after(ticks, tap->txa_nextrequest)) {
2422 * Don't retry too often; txa_nextrequest is set
2423 * to the minimum interval we'll retry after
2424 * ieee80211_addba_maxtries failed attempts are made.
2428 IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
2429 "enable AMPDU on tid %d (%s), avgpps %d pkts %d attempt %d",
2430 tap->txa_tid, ieee80211_wme_acnames[TID_TO_WME_AC(tap->txa_tid)],
2431 tap->txa_avgpps, tap->txa_pkts, tap->txa_attempts);
2436 * Request A-MPDU tx aggregation. Setup local state and
2437 * issue an ADDBA request. BA use will only happen after
2438 * the other end replies with ADDBA response.
2441 ieee80211_ampdu_request(struct ieee80211_node *ni,
2442 struct ieee80211_tx_ampdu *tap)
2444 struct ieee80211com *ic = ni->ni_ic;
2446 int tid, dialogtoken;
2447 static int tokens = 0; /* XXX */
2450 if ((tap->txa_flags & IEEE80211_AGGR_SETUP) == 0) {
2451 /* do deferred setup of state */
2452 ampdu_tx_setup(tap);
2454 /* XXX hack for not doing proper locking */
2455 tap->txa_flags &= ~IEEE80211_AGGR_NAK;
2457 dialogtoken = (tokens+1) % 63; /* XXX */
2461 * XXX TODO: This is racy with any other parallel TX going on. :(
2463 tap->txa_start = ni->ni_txseqs[tid];
2465 args[0] = dialogtoken;
2466 args[1] = 0; /* NB: status code not used */
2467 args[2] = IEEE80211_BAPS_POLICY_IMMEDIATE
2468 | SM(tid, IEEE80211_BAPS_TID)
2469 | SM(IEEE80211_AGGR_BAWMAX, IEEE80211_BAPS_BUFSIZ)
2471 args[3] = 0; /* batimeout */
2472 /* NB: do first so there's no race against reply */
2473 if (!ic->ic_addba_request(ni, tap, dialogtoken, args[2], args[3])) {
2474 /* unable to setup state, don't make request */
2475 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2476 ni, "%s: could not setup BA stream for TID %d AC %d",
2477 __func__, tap->txa_tid, TID_TO_WME_AC(tap->txa_tid));
2478 /* defer next try so we don't slam the driver with requests */
2479 tap->txa_attempts = ieee80211_addba_maxtries;
2480 /* NB: check in case driver wants to override */
2481 if (tap->txa_nextrequest <= ticks)
2482 tap->txa_nextrequest = ticks + ieee80211_addba_backoff;
2485 tokens = dialogtoken; /* allocate token */
2486 /* NB: after calling ic_addba_request so driver can set txa_start */
2487 args[4] = SM(tap->txa_start, IEEE80211_BASEQ_START)
2488 | SM(0, IEEE80211_BASEQ_FRAG)
2490 return ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
2491 IEEE80211_ACTION_BA_ADDBA_REQUEST, args);
2495 * Terminate an AMPDU tx stream. State is reclaimed
2496 * and the peer notified with a DelBA Action frame.
2499 ieee80211_ampdu_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
2502 struct ieee80211com *ic = ni->ni_ic;
2503 struct ieee80211vap *vap = ni->ni_vap;
2507 tap->txa_flags &= ~IEEE80211_AGGR_BARPEND;
2508 if (IEEE80211_AMPDU_RUNNING(tap)) {
2509 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2510 ni, "%s: stop BA stream for TID %d (reason: %d (%s))",
2511 __func__, tap->txa_tid, reason,
2512 ieee80211_reason_to_string(reason));
2513 vap->iv_stats.is_ampdu_stop++;
2515 ic->ic_addba_stop(ni, tap);
2516 args[0] = tap->txa_tid;
2517 args[1] = IEEE80211_DELBAPS_INIT;
2518 args[2] = reason; /* XXX reason code */
2519 ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
2520 IEEE80211_ACTION_BA_DELBA, args);
2522 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2523 ni, "%s: BA stream for TID %d not running "
2524 "(reason: %d (%s))", __func__, tap->txa_tid, reason,
2525 ieee80211_reason_to_string(reason));
2526 vap->iv_stats.is_ampdu_stop_failed++;
2531 static void bar_start_timer(struct ieee80211_tx_ampdu *tap);
2534 bar_timeout(void *arg)
2536 struct ieee80211_tx_ampdu *tap = arg;
2537 struct ieee80211_node *ni = tap->txa_ni;
2539 KASSERT((tap->txa_flags & IEEE80211_AGGR_XCHGPEND) == 0,
2540 ("bar/addba collision, flags 0x%x", tap->txa_flags));
2542 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2543 ni, "%s: tid %u flags 0x%x attempts %d", __func__,
2544 tap->txa_tid, tap->txa_flags, tap->txa_attempts);
2546 /* guard against race with bar_tx_complete */
2547 if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) == 0)
2550 if (tap->txa_attempts >= ieee80211_bar_maxtries) {
2551 struct ieee80211com *ic = ni->ni_ic;
2553 ni->ni_vap->iv_stats.is_ampdu_bar_tx_fail++;
2555 * If (at least) the last BAR TX timeout was due to
2556 * an ieee80211_send_bar() failures, then we need
2557 * to make sure we notify the driver that a BAR
2558 * TX did occur and fail. This gives the driver
2559 * a chance to undo any queue pause that may
2562 ic->ic_bar_response(ni, tap, 1);
2563 ieee80211_ampdu_stop(ni, tap, IEEE80211_REASON_TIMEOUT);
2565 ni->ni_vap->iv_stats.is_ampdu_bar_tx_retry++;
2566 if (ieee80211_send_bar(ni, tap, tap->txa_seqpending) != 0) {
2567 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2568 ni, "%s: failed to TX, starting timer\n",
2571 * If ieee80211_send_bar() fails here, the
2572 * timer may have stopped and/or the pending
2573 * flag may be clear. Because of this,
2574 * fake the BARPEND and reset the timer.
2575 * A retransmission attempt will then occur
2576 * during the next timeout.
2579 tap->txa_flags |= IEEE80211_AGGR_BARPEND;
2580 bar_start_timer(tap);
2586 bar_start_timer(struct ieee80211_tx_ampdu *tap)
2588 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2592 callout_reset(&tap->txa_timer, ieee80211_bar_timeout, bar_timeout, tap);
2596 bar_stop_timer(struct ieee80211_tx_ampdu *tap)
2598 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2602 callout_stop(&tap->txa_timer);
2606 bar_tx_complete(struct ieee80211_node *ni, void *arg, int status)
2608 struct ieee80211_tx_ampdu *tap = arg;
2610 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2611 ni, "%s: tid %u flags 0x%x pending %d status %d",
2612 __func__, tap->txa_tid, tap->txa_flags,
2613 callout_pending(&tap->txa_timer), status);
2615 ni->ni_vap->iv_stats.is_ampdu_bar_tx++;
2617 if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) &&
2618 callout_pending(&tap->txa_timer)) {
2619 struct ieee80211com *ic = ni->ni_ic;
2621 if (status == 0) /* ACK'd */
2622 bar_stop_timer(tap);
2623 ic->ic_bar_response(ni, tap, status);
2624 /* NB: just let timer expire so we pace requests */
2629 ieee80211_bar_response(struct ieee80211_node *ni,
2630 struct ieee80211_tx_ampdu *tap, int status)
2633 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2637 if (status == 0) { /* got ACK */
2638 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2639 ni, "BAR moves BA win <%u:%u> (%u frames) txseq %u tid %u",
2641 IEEE80211_SEQ_ADD(tap->txa_start, tap->txa_wnd-1),
2642 tap->txa_qframes, tap->txa_seqpending,
2645 /* NB: timer already stopped in bar_tx_complete */
2646 tap->txa_start = tap->txa_seqpending;
2647 tap->txa_flags &= ~IEEE80211_AGGR_BARPEND;
2652 * Transmit a BAR frame to the specified node. The
2653 * BAR contents are drawn from the supplied aggregation
2654 * state associated with the node.
2656 * NB: we only handle immediate ACK w/ compressed bitmap.
2659 ieee80211_send_bar(struct ieee80211_node *ni,
2660 struct ieee80211_tx_ampdu *tap, ieee80211_seq seq)
2662 #define senderr(_x, _v) do { vap->iv_stats._v++; ret = _x; goto bad; } while (0)
2663 struct ieee80211vap *vap = ni->ni_vap;
2664 struct ieee80211com *ic = ni->ni_ic;
2665 struct ieee80211_frame_bar *bar;
2667 uint16_t barctl, barseqctl;
2672 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2677 if ((tap->txa_flags & IEEE80211_AGGR_RUNNING) == 0) {
2678 /* no ADDBA response, should not happen */
2683 bar_stop_timer(tap);
2685 ieee80211_ref_node(ni);
2687 m = ieee80211_getmgtframe(&frm, ic->ic_headroom, sizeof(*bar));
2689 senderr(ENOMEM, is_tx_nobuf);
2691 if (!ieee80211_add_callback(m, bar_tx_complete, tap)) {
2693 senderr(ENOMEM, is_tx_nobuf); /* XXX */
2697 bar = mtod(m, struct ieee80211_frame_bar *);
2698 bar->i_fc[0] = IEEE80211_FC0_VERSION_0 |
2699 IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_BAR;
2701 IEEE80211_ADDR_COPY(bar->i_ra, ni->ni_macaddr);
2702 IEEE80211_ADDR_COPY(bar->i_ta, vap->iv_myaddr);
2705 barctl = (tap->txa_flags & IEEE80211_AGGR_IMMEDIATE ?
2706 0 : IEEE80211_BAR_NOACK)
2707 | IEEE80211_BAR_COMP
2708 | SM(tid, IEEE80211_BAR_TID)
2710 barseqctl = SM(seq, IEEE80211_BAR_SEQ_START);
2711 /* NB: known to have proper alignment */
2712 bar->i_ctl = htole16(barctl);
2713 bar->i_seq = htole16(barseqctl);
2714 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_bar);
2716 M_WME_SETAC(m, WME_AC_VO);
2718 IEEE80211_NODE_STAT(ni, tx_mgmt); /* XXX tx_ctl? */
2721 /* init/bump attempts counter */
2722 if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) == 0)
2723 tap->txa_attempts = 1;
2725 tap->txa_attempts++;
2726 tap->txa_seqpending = seq;
2727 tap->txa_flags |= IEEE80211_AGGR_BARPEND;
2729 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_11N,
2730 ni, "send BAR: tid %u ctl 0x%x start %u (attempt %d)",
2731 tid, barctl, seq, tap->txa_attempts);
2734 * ic_raw_xmit will free the node reference
2735 * regardless of queue/TX success or failure.
2737 IEEE80211_TX_LOCK(ic);
2738 ret = ieee80211_raw_output(vap, ni, m, NULL);
2739 IEEE80211_TX_UNLOCK(ic);
2741 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_11N,
2742 ni, "send BAR: failed: (ret = %d)\n",
2744 /* xmit failed, clear state flag */
2745 tap->txa_flags &= ~IEEE80211_AGGR_BARPEND;
2746 vap->iv_stats.is_ampdu_bar_tx_fail++;
2749 /* XXX hack against tx complete happening before timer is started */
2750 if (tap->txa_flags & IEEE80211_AGGR_BARPEND)
2751 bar_start_timer(tap);
2754 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2758 vap->iv_stats.is_ampdu_bar_tx_fail++;
2759 ieee80211_free_node(ni);
2765 ht_action_output(struct ieee80211_node *ni, struct mbuf *m)
2767 struct ieee80211_bpf_params params;
2769 memset(¶ms, 0, sizeof(params));
2770 params.ibp_pri = WME_AC_VO;
2771 params.ibp_rate0 = ni->ni_txparms->mgmtrate;
2772 /* NB: we know all frames are unicast */
2773 params.ibp_try0 = ni->ni_txparms->maxretry;
2774 params.ibp_power = ni->ni_txpower;
2775 return ieee80211_mgmt_output(ni, m, IEEE80211_FC0_SUBTYPE_ACTION,
2779 #define ADDSHORT(frm, v) do { \
2780 frm[0] = (v) & 0xff; \
2781 frm[1] = (v) >> 8; \
2786 * Send an action management frame. The arguments are stuff
2787 * into a frame without inspection; the caller is assumed to
2788 * prepare them carefully (e.g. based on the aggregation state).
2791 ht_send_action_ba_addba(struct ieee80211_node *ni,
2792 int category, int action, void *arg0)
2794 struct ieee80211vap *vap = ni->ni_vap;
2795 struct ieee80211com *ic = ni->ni_ic;
2796 uint16_t *args = arg0;
2800 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2801 "send ADDBA %s: dialogtoken %d status %d "
2802 "baparamset 0x%x (tid %d) batimeout 0x%x baseqctl 0x%x",
2803 (action == IEEE80211_ACTION_BA_ADDBA_REQUEST) ?
2804 "request" : "response",
2805 args[0], args[1], args[2], MS(args[2], IEEE80211_BAPS_TID),
2808 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2809 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2810 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2811 ieee80211_ref_node(ni);
2813 m = ieee80211_getmgtframe(&frm,
2814 ic->ic_headroom + sizeof(struct ieee80211_frame),
2815 sizeof(uint16_t) /* action+category */
2816 /* XXX may action payload */
2817 + sizeof(struct ieee80211_action_ba_addbaresponse)
2822 *frm++ = args[0]; /* dialog token */
2823 if (action == IEEE80211_ACTION_BA_ADDBA_RESPONSE)
2824 ADDSHORT(frm, args[1]); /* status code */
2825 ADDSHORT(frm, args[2]); /* baparamset */
2826 ADDSHORT(frm, args[3]); /* batimeout */
2827 if (action == IEEE80211_ACTION_BA_ADDBA_REQUEST)
2828 ADDSHORT(frm, args[4]); /* baseqctl */
2829 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2830 return ht_action_output(ni, m);
2832 vap->iv_stats.is_tx_nobuf++;
2833 ieee80211_free_node(ni);
2839 ht_send_action_ba_delba(struct ieee80211_node *ni,
2840 int category, int action, void *arg0)
2842 struct ieee80211vap *vap = ni->ni_vap;
2843 struct ieee80211com *ic = ni->ni_ic;
2844 uint16_t *args = arg0;
2846 uint16_t baparamset;
2849 baparamset = SM(args[0], IEEE80211_DELBAPS_TID)
2852 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2853 "send DELBA action: tid %d, initiator %d reason %d (%s)",
2854 args[0], args[1], args[2], ieee80211_reason_to_string(args[2]));
2856 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2857 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2858 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2859 ieee80211_ref_node(ni);
2861 m = ieee80211_getmgtframe(&frm,
2862 ic->ic_headroom + sizeof(struct ieee80211_frame),
2863 sizeof(uint16_t) /* action+category */
2864 /* XXX may action payload */
2865 + sizeof(struct ieee80211_action_ba_addbaresponse)
2870 ADDSHORT(frm, baparamset);
2871 ADDSHORT(frm, args[2]); /* reason code */
2872 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2873 return ht_action_output(ni, m);
2875 vap->iv_stats.is_tx_nobuf++;
2876 ieee80211_free_node(ni);
2882 ht_send_action_ht_txchwidth(struct ieee80211_node *ni,
2883 int category, int action, void *arg0)
2885 struct ieee80211vap *vap = ni->ni_vap;
2886 struct ieee80211com *ic = ni->ni_ic;
2890 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2891 "send HT txchwidth: width %d",
2892 IEEE80211_IS_CHAN_HT40(ni->ni_chan) ? 40 : 20);
2894 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2895 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2896 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2897 ieee80211_ref_node(ni);
2899 m = ieee80211_getmgtframe(&frm,
2900 ic->ic_headroom + sizeof(struct ieee80211_frame),
2901 sizeof(uint16_t) /* action+category */
2902 /* XXX may action payload */
2903 + sizeof(struct ieee80211_action_ba_addbaresponse)
2908 *frm++ = IEEE80211_IS_CHAN_HT40(ni->ni_chan) ?
2909 IEEE80211_A_HT_TXCHWIDTH_2040 :
2910 IEEE80211_A_HT_TXCHWIDTH_20;
2911 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2912 return ht_action_output(ni, m);
2914 vap->iv_stats.is_tx_nobuf++;
2915 ieee80211_free_node(ni);
2922 * Construct the MCS bit mask for inclusion in an HT capabilities
2923 * information element.
2926 ieee80211_set_mcsset(struct ieee80211com *ic, uint8_t *frm)
2931 KASSERT((ic->ic_rxstream > 0 && ic->ic_rxstream <= 4),
2932 ("ic_rxstream %d out of range", ic->ic_rxstream));
2933 KASSERT((ic->ic_txstream > 0 && ic->ic_txstream <= 4),
2934 ("ic_txstream %d out of range", ic->ic_txstream));
2936 for (i = 0; i < ic->ic_rxstream * 8; i++)
2938 if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
2939 (ic->ic_htcaps & IEEE80211_HTC_RXMCS32))
2941 if (ic->ic_htcaps & IEEE80211_HTC_RXUNEQUAL) {
2942 if (ic->ic_rxstream >= 2) {
2943 for (i = 33; i <= 38; i++)
2946 if (ic->ic_rxstream >= 3) {
2947 for (i = 39; i <= 52; i++)
2950 if (ic->ic_txstream >= 4) {
2951 for (i = 53; i <= 76; i++)
2956 if (ic->ic_rxstream != ic->ic_txstream) {
2957 txparams = 0x1; /* TX MCS set defined */
2958 txparams |= 0x2; /* TX RX MCS not equal */
2959 txparams |= (ic->ic_txstream - 1) << 2; /* num TX streams */
2960 if (ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL)
2961 txparams |= 0x16; /* TX unequal modulation sup */
2968 * Add body of an HTCAP information element.
2971 ieee80211_add_htcap_body(uint8_t *frm, struct ieee80211_node *ni)
2973 #define ADDSHORT(frm, v) do { \
2974 frm[0] = (v) & 0xff; \
2975 frm[1] = (v) >> 8; \
2978 struct ieee80211com *ic = ni->ni_ic;
2979 struct ieee80211vap *vap = ni->ni_vap;
2980 uint16_t caps, extcaps;
2983 /* HT capabilities */
2984 caps = vap->iv_htcaps & 0xffff;
2986 * Note channel width depends on whether we are operating as
2987 * a sta or not. When operating as a sta we are generating
2988 * a request based on our desired configuration. Otherwise
2989 * we are operational and the channel attributes identify
2990 * how we've been setup (which might be different if a fixed
2991 * channel is specified).
2993 if (vap->iv_opmode == IEEE80211_M_STA) {
2994 /* override 20/40 use based on config */
2995 if (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)
2996 caps |= IEEE80211_HTCAP_CHWIDTH40;
2998 caps &= ~IEEE80211_HTCAP_CHWIDTH40;
3000 /* Start by using the advertised settings */
3001 rxmax = MS(ni->ni_htparam, IEEE80211_HTCAP_MAXRXAMPDU);
3002 density = MS(ni->ni_htparam, IEEE80211_HTCAP_MPDUDENSITY);
3004 IEEE80211_DPRINTF(vap, IEEE80211_MSG_11N,
3005 "%s: advertised rxmax=%d, density=%d, vap rxmax=%d, density=%d\n",
3009 vap->iv_ampdu_rxmax,
3010 vap->iv_ampdu_density);
3012 /* Cap at VAP rxmax */
3013 if (rxmax > vap->iv_ampdu_rxmax)
3014 rxmax = vap->iv_ampdu_rxmax;
3017 * If the VAP ampdu density value greater, use that.
3019 * (Larger density value == larger minimum gap between A-MPDU
3022 if (vap->iv_ampdu_density > density)
3023 density = vap->iv_ampdu_density;
3026 * NB: Hardware might support HT40 on some but not all
3027 * channels. We can't determine this earlier because only
3028 * after association the channel is upgraded to HT based
3029 * on the negotiated capabilities.
3031 if (ni->ni_chan != IEEE80211_CHAN_ANYC &&
3032 findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT40U) == NULL &&
3033 findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT40D) == NULL)
3034 caps &= ~IEEE80211_HTCAP_CHWIDTH40;
3036 /* override 20/40 use based on current channel */
3037 if (IEEE80211_IS_CHAN_HT40(ni->ni_chan))
3038 caps |= IEEE80211_HTCAP_CHWIDTH40;
3040 caps &= ~IEEE80211_HTCAP_CHWIDTH40;
3042 /* XXX TODO should it start by using advertised settings? */
3043 rxmax = vap->iv_ampdu_rxmax;
3044 density = vap->iv_ampdu_density;
3047 /* adjust short GI based on channel and config */
3048 if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20) == 0)
3049 caps &= ~IEEE80211_HTCAP_SHORTGI20;
3050 if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40) == 0 ||
3051 (caps & IEEE80211_HTCAP_CHWIDTH40) == 0)
3052 caps &= ~IEEE80211_HTCAP_SHORTGI40;
3054 /* adjust STBC based on receive capabilities */
3055 if ((vap->iv_flags_ht & IEEE80211_FHT_STBC_RX) == 0)
3056 caps &= ~IEEE80211_HTCAP_RXSTBC;
3058 /* adjust LDPC based on receive capabilites */
3059 if ((vap->iv_flags_ht & IEEE80211_FHT_LDPC_RX) == 0)
3060 caps &= ~IEEE80211_HTCAP_LDPC;
3062 ADDSHORT(frm, caps);
3065 *frm = SM(rxmax, IEEE80211_HTCAP_MAXRXAMPDU)
3066 | SM(density, IEEE80211_HTCAP_MPDUDENSITY)
3070 /* pre-zero remainder of ie */
3071 memset(frm, 0, sizeof(struct ieee80211_ie_htcap) -
3072 __offsetof(struct ieee80211_ie_htcap, hc_mcsset));
3074 /* supported MCS set */
3076 * XXX: For sta mode the rate set should be restricted based
3077 * on the AP's capabilities, but ni_htrates isn't setup when
3078 * we're called to form an AssocReq frame so for now we're
3079 * restricted to the device capabilities.
3081 ieee80211_set_mcsset(ni->ni_ic, frm);
3083 frm += __offsetof(struct ieee80211_ie_htcap, hc_extcap) -
3084 __offsetof(struct ieee80211_ie_htcap, hc_mcsset);
3086 /* HT extended capabilities */
3087 extcaps = vap->iv_htextcaps & 0xffff;
3089 ADDSHORT(frm, extcaps);
3091 frm += sizeof(struct ieee80211_ie_htcap) -
3092 __offsetof(struct ieee80211_ie_htcap, hc_txbf);
3099 * Add 802.11n HT capabilities information element
3102 ieee80211_add_htcap(uint8_t *frm, struct ieee80211_node *ni)
3104 frm[0] = IEEE80211_ELEMID_HTCAP;
3105 frm[1] = sizeof(struct ieee80211_ie_htcap) - 2;
3106 return ieee80211_add_htcap_body(frm + 2, ni);
3110 * Non-associated probe request - add HT capabilities based on
3111 * the current channel configuration.
3114 ieee80211_add_htcap_body_ch(uint8_t *frm, struct ieee80211vap *vap,
3115 struct ieee80211_channel *c)
3117 #define ADDSHORT(frm, v) do { \
3118 frm[0] = (v) & 0xff; \
3119 frm[1] = (v) >> 8; \
3122 struct ieee80211com *ic = vap->iv_ic;
3123 uint16_t caps, extcaps;
3126 /* HT capabilities */
3127 caps = vap->iv_htcaps & 0xffff;
3130 * We don't use this in STA mode; only in IBSS mode.
3131 * So in IBSS mode we base our HTCAP flags on the
3135 /* override 20/40 use based on current channel */
3136 if (IEEE80211_IS_CHAN_HT40(c))
3137 caps |= IEEE80211_HTCAP_CHWIDTH40;
3139 caps &= ~IEEE80211_HTCAP_CHWIDTH40;
3141 /* Use the currently configured values */
3142 rxmax = vap->iv_ampdu_rxmax;
3143 density = vap->iv_ampdu_density;
3145 /* adjust short GI based on channel and config */
3146 if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20) == 0)
3147 caps &= ~IEEE80211_HTCAP_SHORTGI20;
3148 if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40) == 0 ||
3149 (caps & IEEE80211_HTCAP_CHWIDTH40) == 0)
3150 caps &= ~IEEE80211_HTCAP_SHORTGI40;
3151 ADDSHORT(frm, caps);
3154 *frm = SM(rxmax, IEEE80211_HTCAP_MAXRXAMPDU)
3155 | SM(density, IEEE80211_HTCAP_MPDUDENSITY)
3159 /* pre-zero remainder of ie */
3160 memset(frm, 0, sizeof(struct ieee80211_ie_htcap) -
3161 __offsetof(struct ieee80211_ie_htcap, hc_mcsset));
3163 /* supported MCS set */
3165 * XXX: For sta mode the rate set should be restricted based
3166 * on the AP's capabilities, but ni_htrates isn't setup when
3167 * we're called to form an AssocReq frame so for now we're
3168 * restricted to the device capabilities.
3170 ieee80211_set_mcsset(ic, frm);
3172 frm += __offsetof(struct ieee80211_ie_htcap, hc_extcap) -
3173 __offsetof(struct ieee80211_ie_htcap, hc_mcsset);
3175 /* HT extended capabilities */
3176 extcaps = vap->iv_htextcaps & 0xffff;
3178 ADDSHORT(frm, extcaps);
3180 frm += sizeof(struct ieee80211_ie_htcap) -
3181 __offsetof(struct ieee80211_ie_htcap, hc_txbf);
3188 * Add 802.11n HT capabilities information element
3191 ieee80211_add_htcap_ch(uint8_t *frm, struct ieee80211vap *vap,
3192 struct ieee80211_channel *c)
3194 frm[0] = IEEE80211_ELEMID_HTCAP;
3195 frm[1] = sizeof(struct ieee80211_ie_htcap) - 2;
3196 return ieee80211_add_htcap_body_ch(frm + 2, vap, c);
3200 * Add Broadcom OUI wrapped standard HTCAP ie; this is
3201 * used for compatibility w/ pre-draft implementations.
3204 ieee80211_add_htcap_vendor(uint8_t *frm, struct ieee80211_node *ni)
3206 frm[0] = IEEE80211_ELEMID_VENDOR;
3207 frm[1] = 4 + sizeof(struct ieee80211_ie_htcap) - 2;
3208 frm[2] = (BCM_OUI >> 0) & 0xff;
3209 frm[3] = (BCM_OUI >> 8) & 0xff;
3210 frm[4] = (BCM_OUI >> 16) & 0xff;
3211 frm[5] = BCM_OUI_HTCAP;
3212 return ieee80211_add_htcap_body(frm + 6, ni);
3216 * Construct the MCS bit mask of basic rates
3217 * for inclusion in an HT information element.
3220 ieee80211_set_basic_htrates(uint8_t *frm, const struct ieee80211_htrateset *rs)
3224 for (i = 0; i < rs->rs_nrates; i++) {
3225 int r = rs->rs_rates[i] & IEEE80211_RATE_VAL;
3226 if ((rs->rs_rates[i] & IEEE80211_RATE_BASIC) &&
3227 r < IEEE80211_HTRATE_MAXSIZE) {
3228 /* NB: this assumes a particular implementation */
3235 * Update the HTINFO ie for a beacon frame.
3238 ieee80211_ht_update_beacon(struct ieee80211vap *vap,
3239 struct ieee80211_beacon_offsets *bo)
3241 #define PROTMODE (IEEE80211_HTINFO_OPMODE|IEEE80211_HTINFO_NONHT_PRESENT)
3242 struct ieee80211_node *ni;
3243 const struct ieee80211_channel *bsschan;
3244 struct ieee80211com *ic = vap->iv_ic;
3245 struct ieee80211_ie_htinfo *ht =
3246 (struct ieee80211_ie_htinfo *) bo->bo_htinfo;
3248 ni = ieee80211_ref_node(vap->iv_bss);
3249 bsschan = ni->ni_chan;
3251 /* XXX only update on channel change */
3252 ht->hi_ctrlchannel = ieee80211_chan2ieee(ic, bsschan);
3253 if (vap->iv_flags_ht & IEEE80211_FHT_RIFS)
3254 ht->hi_byte1 = IEEE80211_HTINFO_RIFSMODE_PERM;
3256 ht->hi_byte1 = IEEE80211_HTINFO_RIFSMODE_PROH;
3257 if (IEEE80211_IS_CHAN_HT40U(bsschan))
3258 ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_ABOVE;
3259 else if (IEEE80211_IS_CHAN_HT40D(bsschan))
3260 ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_BELOW;
3262 ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_NONE;
3263 if (IEEE80211_IS_CHAN_HT40(bsschan))
3264 ht->hi_byte1 |= IEEE80211_HTINFO_TXWIDTH_2040;
3266 /* protection mode */
3267 ht->hi_byte2 = (ht->hi_byte2 &~ PROTMODE) | ic->ic_curhtprotmode;
3269 ieee80211_free_node(ni);
3271 /* XXX propagate to vendor ie's */
3276 * Add body of an HTINFO information element.
3278 * NB: We don't use struct ieee80211_ie_htinfo because we can
3279 * be called to fillin both a standard ie and a compat ie that
3280 * has a vendor OUI at the front.
3283 ieee80211_add_htinfo_body(uint8_t *frm, struct ieee80211_node *ni)
3285 struct ieee80211vap *vap = ni->ni_vap;
3286 struct ieee80211com *ic = ni->ni_ic;
3288 /* pre-zero remainder of ie */
3289 memset(frm, 0, sizeof(struct ieee80211_ie_htinfo) - 2);
3291 /* primary/control channel center */
3292 *frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
3294 if (vap->iv_flags_ht & IEEE80211_FHT_RIFS)
3295 frm[0] = IEEE80211_HTINFO_RIFSMODE_PERM;
3297 frm[0] = IEEE80211_HTINFO_RIFSMODE_PROH;
3298 if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan))
3299 frm[0] |= IEEE80211_HTINFO_2NDCHAN_ABOVE;
3300 else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan))
3301 frm[0] |= IEEE80211_HTINFO_2NDCHAN_BELOW;
3303 frm[0] |= IEEE80211_HTINFO_2NDCHAN_NONE;
3304 if (IEEE80211_IS_CHAN_HT40(ni->ni_chan))
3305 frm[0] |= IEEE80211_HTINFO_TXWIDTH_2040;
3307 frm[1] = ic->ic_curhtprotmode;
3312 ieee80211_set_basic_htrates(frm, &ni->ni_htrates);
3313 frm += sizeof(struct ieee80211_ie_htinfo) -
3314 __offsetof(struct ieee80211_ie_htinfo, hi_basicmcsset);
3319 * Add 802.11n HT information information element.
3322 ieee80211_add_htinfo(uint8_t *frm, struct ieee80211_node *ni)
3324 frm[0] = IEEE80211_ELEMID_HTINFO;
3325 frm[1] = sizeof(struct ieee80211_ie_htinfo) - 2;
3326 return ieee80211_add_htinfo_body(frm + 2, ni);
3330 * Add Broadcom OUI wrapped standard HTINFO ie; this is
3331 * used for compatibility w/ pre-draft implementations.
3334 ieee80211_add_htinfo_vendor(uint8_t *frm, struct ieee80211_node *ni)
3336 frm[0] = IEEE80211_ELEMID_VENDOR;
3337 frm[1] = 4 + sizeof(struct ieee80211_ie_htinfo) - 2;
3338 frm[2] = (BCM_OUI >> 0) & 0xff;
3339 frm[3] = (BCM_OUI >> 8) & 0xff;
3340 frm[4] = (BCM_OUI >> 16) & 0xff;
3341 frm[5] = BCM_OUI_HTINFO;
3342 return ieee80211_add_htinfo_body(frm + 6, ni);