2 * Copyright (c) 2007-2008 Sam Leffler, Errno Consulting
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
32 * IEEE 802.11n protocol support.
38 #include <sys/param.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/systm.h>
42 #include <sys/endian.h>
44 #include <sys/socket.h>
47 #include <net/if_var.h>
48 #include <net/if_media.h>
49 #include <net/ethernet.h>
51 #include <net80211/ieee80211_var.h>
52 #include <net80211/ieee80211_action.h>
53 #include <net80211/ieee80211_input.h>
55 /* define here, used throughout file */
56 #define MS(_v, _f) (((_v) & _f) >> _f##_S)
57 #define SM(_v, _f) (((_v) << _f##_S) & _f)
59 const struct ieee80211_mcs_rates ieee80211_htrates[IEEE80211_HTRATE_MAXSIZE] = {
60 { 13, 14, 27, 30 }, /* MCS 0 */
61 { 26, 29, 54, 60 }, /* MCS 1 */
62 { 39, 43, 81, 90 }, /* MCS 2 */
63 { 52, 58, 108, 120 }, /* MCS 3 */
64 { 78, 87, 162, 180 }, /* MCS 4 */
65 { 104, 116, 216, 240 }, /* MCS 5 */
66 { 117, 130, 243, 270 }, /* MCS 6 */
67 { 130, 144, 270, 300 }, /* MCS 7 */
68 { 26, 29, 54, 60 }, /* MCS 8 */
69 { 52, 58, 108, 120 }, /* MCS 9 */
70 { 78, 87, 162, 180 }, /* MCS 10 */
71 { 104, 116, 216, 240 }, /* MCS 11 */
72 { 156, 173, 324, 360 }, /* MCS 12 */
73 { 208, 231, 432, 480 }, /* MCS 13 */
74 { 234, 260, 486, 540 }, /* MCS 14 */
75 { 260, 289, 540, 600 }, /* MCS 15 */
76 { 39, 43, 81, 90 }, /* MCS 16 */
77 { 78, 87, 162, 180 }, /* MCS 17 */
78 { 117, 130, 243, 270 }, /* MCS 18 */
79 { 156, 173, 324, 360 }, /* MCS 19 */
80 { 234, 260, 486, 540 }, /* MCS 20 */
81 { 312, 347, 648, 720 }, /* MCS 21 */
82 { 351, 390, 729, 810 }, /* MCS 22 */
83 { 390, 433, 810, 900 }, /* MCS 23 */
84 { 52, 58, 108, 120 }, /* MCS 24 */
85 { 104, 116, 216, 240 }, /* MCS 25 */
86 { 156, 173, 324, 360 }, /* MCS 26 */
87 { 208, 231, 432, 480 }, /* MCS 27 */
88 { 312, 347, 648, 720 }, /* MCS 28 */
89 { 416, 462, 864, 960 }, /* MCS 29 */
90 { 468, 520, 972, 1080 }, /* MCS 30 */
91 { 520, 578, 1080, 1200 }, /* MCS 31 */
92 { 0, 0, 12, 13 }, /* MCS 32 */
93 { 78, 87, 162, 180 }, /* MCS 33 */
94 { 104, 116, 216, 240 }, /* MCS 34 */
95 { 130, 144, 270, 300 }, /* MCS 35 */
96 { 117, 130, 243, 270 }, /* MCS 36 */
97 { 156, 173, 324, 360 }, /* MCS 37 */
98 { 195, 217, 405, 450 }, /* MCS 38 */
99 { 104, 116, 216, 240 }, /* MCS 39 */
100 { 130, 144, 270, 300 }, /* MCS 40 */
101 { 130, 144, 270, 300 }, /* MCS 41 */
102 { 156, 173, 324, 360 }, /* MCS 42 */
103 { 182, 202, 378, 420 }, /* MCS 43 */
104 { 182, 202, 378, 420 }, /* MCS 44 */
105 { 208, 231, 432, 480 }, /* MCS 45 */
106 { 156, 173, 324, 360 }, /* MCS 46 */
107 { 195, 217, 405, 450 }, /* MCS 47 */
108 { 195, 217, 405, 450 }, /* MCS 48 */
109 { 234, 260, 486, 540 }, /* MCS 49 */
110 { 273, 303, 567, 630 }, /* MCS 50 */
111 { 273, 303, 567, 630 }, /* MCS 51 */
112 { 312, 347, 648, 720 }, /* MCS 52 */
113 { 130, 144, 270, 300 }, /* MCS 53 */
114 { 156, 173, 324, 360 }, /* MCS 54 */
115 { 182, 202, 378, 420 }, /* MCS 55 */
116 { 156, 173, 324, 360 }, /* MCS 56 */
117 { 182, 202, 378, 420 }, /* MCS 57 */
118 { 208, 231, 432, 480 }, /* MCS 58 */
119 { 234, 260, 486, 540 }, /* MCS 59 */
120 { 208, 231, 432, 480 }, /* MCS 60 */
121 { 234, 260, 486, 540 }, /* MCS 61 */
122 { 260, 289, 540, 600 }, /* MCS 62 */
123 { 260, 289, 540, 600 }, /* MCS 63 */
124 { 286, 318, 594, 660 }, /* MCS 64 */
125 { 195, 217, 405, 450 }, /* MCS 65 */
126 { 234, 260, 486, 540 }, /* MCS 66 */
127 { 273, 303, 567, 630 }, /* MCS 67 */
128 { 234, 260, 486, 540 }, /* MCS 68 */
129 { 273, 303, 567, 630 }, /* MCS 69 */
130 { 312, 347, 648, 720 }, /* MCS 70 */
131 { 351, 390, 729, 810 }, /* MCS 71 */
132 { 312, 347, 648, 720 }, /* MCS 72 */
133 { 351, 390, 729, 810 }, /* MCS 73 */
134 { 390, 433, 810, 900 }, /* MCS 74 */
135 { 390, 433, 810, 900 }, /* MCS 75 */
136 { 429, 477, 891, 990 }, /* MCS 76 */
139 static int ieee80211_ampdu_age = -1; /* threshold for ampdu reorder q (ms) */
140 SYSCTL_PROC(_net_wlan, OID_AUTO, ampdu_age, CTLTYPE_INT | CTLFLAG_RW,
141 &ieee80211_ampdu_age, 0, ieee80211_sysctl_msecs_ticks, "I",
142 "AMPDU max reorder age (ms)");
144 static int ieee80211_recv_bar_ena = 1;
145 SYSCTL_INT(_net_wlan, OID_AUTO, recv_bar, CTLFLAG_RW, &ieee80211_recv_bar_ena,
146 0, "BAR frame processing (ena/dis)");
148 static int ieee80211_addba_timeout = -1;/* timeout for ADDBA response */
149 SYSCTL_PROC(_net_wlan, OID_AUTO, addba_timeout, CTLTYPE_INT | CTLFLAG_RW,
150 &ieee80211_addba_timeout, 0, ieee80211_sysctl_msecs_ticks, "I",
151 "ADDBA request timeout (ms)");
152 static int ieee80211_addba_backoff = -1;/* backoff after max ADDBA requests */
153 SYSCTL_PROC(_net_wlan, OID_AUTO, addba_backoff, CTLTYPE_INT | CTLFLAG_RW,
154 &ieee80211_addba_backoff, 0, ieee80211_sysctl_msecs_ticks, "I",
155 "ADDBA request backoff (ms)");
156 static int ieee80211_addba_maxtries = 3;/* max ADDBA requests before backoff */
157 SYSCTL_INT(_net_wlan, OID_AUTO, addba_maxtries, CTLFLAG_RW,
158 &ieee80211_addba_maxtries, 0, "max ADDBA requests sent before backoff");
160 static int ieee80211_bar_timeout = -1; /* timeout waiting for BAR response */
161 static int ieee80211_bar_maxtries = 50;/* max BAR requests before DELBA */
163 static ieee80211_recv_action_func ht_recv_action_ba_addba_request;
164 static ieee80211_recv_action_func ht_recv_action_ba_addba_response;
165 static ieee80211_recv_action_func ht_recv_action_ba_delba;
166 static ieee80211_recv_action_func ht_recv_action_ht_mimopwrsave;
167 static ieee80211_recv_action_func ht_recv_action_ht_txchwidth;
169 static ieee80211_send_action_func ht_send_action_ba_addba;
170 static ieee80211_send_action_func ht_send_action_ba_delba;
171 static ieee80211_send_action_func ht_send_action_ht_txchwidth;
174 ieee80211_ht_init(void)
177 * Setup HT parameters that depends on the clock frequency.
179 ieee80211_ampdu_age = msecs_to_ticks(500);
180 ieee80211_addba_timeout = msecs_to_ticks(250);
181 ieee80211_addba_backoff = msecs_to_ticks(10*1000);
182 ieee80211_bar_timeout = msecs_to_ticks(250);
184 * Register action frame handlers.
186 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_BA,
187 IEEE80211_ACTION_BA_ADDBA_REQUEST, ht_recv_action_ba_addba_request);
188 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_BA,
189 IEEE80211_ACTION_BA_ADDBA_RESPONSE, ht_recv_action_ba_addba_response);
190 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_BA,
191 IEEE80211_ACTION_BA_DELBA, ht_recv_action_ba_delba);
192 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_HT,
193 IEEE80211_ACTION_HT_MIMOPWRSAVE, ht_recv_action_ht_mimopwrsave);
194 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_HT,
195 IEEE80211_ACTION_HT_TXCHWIDTH, ht_recv_action_ht_txchwidth);
197 ieee80211_send_action_register(IEEE80211_ACTION_CAT_BA,
198 IEEE80211_ACTION_BA_ADDBA_REQUEST, ht_send_action_ba_addba);
199 ieee80211_send_action_register(IEEE80211_ACTION_CAT_BA,
200 IEEE80211_ACTION_BA_ADDBA_RESPONSE, ht_send_action_ba_addba);
201 ieee80211_send_action_register(IEEE80211_ACTION_CAT_BA,
202 IEEE80211_ACTION_BA_DELBA, ht_send_action_ba_delba);
203 ieee80211_send_action_register(IEEE80211_ACTION_CAT_HT,
204 IEEE80211_ACTION_HT_TXCHWIDTH, ht_send_action_ht_txchwidth);
206 SYSINIT(wlan_ht, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_ht_init, NULL);
208 static int ieee80211_ampdu_enable(struct ieee80211_node *ni,
209 struct ieee80211_tx_ampdu *tap);
210 static int ieee80211_addba_request(struct ieee80211_node *ni,
211 struct ieee80211_tx_ampdu *tap,
212 int dialogtoken, int baparamset, int batimeout);
213 static int ieee80211_addba_response(struct ieee80211_node *ni,
214 struct ieee80211_tx_ampdu *tap,
215 int code, int baparamset, int batimeout);
216 static void ieee80211_addba_stop(struct ieee80211_node *ni,
217 struct ieee80211_tx_ampdu *tap);
218 static void null_addba_response_timeout(struct ieee80211_node *ni,
219 struct ieee80211_tx_ampdu *tap);
221 static void ieee80211_bar_response(struct ieee80211_node *ni,
222 struct ieee80211_tx_ampdu *tap, int status);
223 static void ampdu_tx_stop(struct ieee80211_tx_ampdu *tap);
224 static void bar_stop_timer(struct ieee80211_tx_ampdu *tap);
225 static int ampdu_rx_start(struct ieee80211_node *, struct ieee80211_rx_ampdu *,
226 int baparamset, int batimeout, int baseqctl);
227 static void ampdu_rx_stop(struct ieee80211_node *, struct ieee80211_rx_ampdu *);
230 ieee80211_ht_attach(struct ieee80211com *ic)
232 /* setup default aggregation policy */
233 ic->ic_recv_action = ieee80211_recv_action;
234 ic->ic_send_action = ieee80211_send_action;
235 ic->ic_ampdu_enable = ieee80211_ampdu_enable;
236 ic->ic_addba_request = ieee80211_addba_request;
237 ic->ic_addba_response = ieee80211_addba_response;
238 ic->ic_addba_response_timeout = null_addba_response_timeout;
239 ic->ic_addba_stop = ieee80211_addba_stop;
240 ic->ic_bar_response = ieee80211_bar_response;
241 ic->ic_ampdu_rx_start = ampdu_rx_start;
242 ic->ic_ampdu_rx_stop = ampdu_rx_stop;
244 ic->ic_htprotmode = IEEE80211_PROT_RTSCTS;
245 ic->ic_curhtprotmode = IEEE80211_HTINFO_OPMODE_PURE;
249 ieee80211_ht_detach(struct ieee80211com *ic)
254 ieee80211_ht_vattach(struct ieee80211vap *vap)
257 /* driver can override defaults */
258 vap->iv_ampdu_rxmax = IEEE80211_HTCAP_MAXRXAMPDU_8K;
259 vap->iv_ampdu_density = IEEE80211_HTCAP_MPDUDENSITY_NA;
260 vap->iv_ampdu_limit = vap->iv_ampdu_rxmax;
261 vap->iv_amsdu_limit = vap->iv_htcaps & IEEE80211_HTCAP_MAXAMSDU;
262 /* tx aggregation traffic thresholds */
263 vap->iv_ampdu_mintraffic[WME_AC_BK] = 128;
264 vap->iv_ampdu_mintraffic[WME_AC_BE] = 64;
265 vap->iv_ampdu_mintraffic[WME_AC_VO] = 32;
266 vap->iv_ampdu_mintraffic[WME_AC_VI] = 32;
268 if (vap->iv_htcaps & IEEE80211_HTC_HT) {
270 * Device is HT capable; enable all HT-related
271 * facilities by default.
272 * XXX these choices may be too aggressive.
274 vap->iv_flags_ht |= IEEE80211_FHT_HT
275 | IEEE80211_FHT_HTCOMPAT
277 if (vap->iv_htcaps & IEEE80211_HTCAP_SHORTGI20)
278 vap->iv_flags_ht |= IEEE80211_FHT_SHORTGI20;
279 /* XXX infer from channel list? */
280 if (vap->iv_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
281 vap->iv_flags_ht |= IEEE80211_FHT_USEHT40;
282 if (vap->iv_htcaps & IEEE80211_HTCAP_SHORTGI40)
283 vap->iv_flags_ht |= IEEE80211_FHT_SHORTGI40;
285 /* enable RIFS if capable */
286 if (vap->iv_htcaps & IEEE80211_HTC_RIFS)
287 vap->iv_flags_ht |= IEEE80211_FHT_RIFS;
289 /* NB: A-MPDU and A-MSDU rx are mandated, these are tx only */
290 vap->iv_flags_ht |= IEEE80211_FHT_AMPDU_RX;
291 if (vap->iv_htcaps & IEEE80211_HTC_AMPDU)
292 vap->iv_flags_ht |= IEEE80211_FHT_AMPDU_TX;
293 vap->iv_flags_ht |= IEEE80211_FHT_AMSDU_RX;
294 if (vap->iv_htcaps & IEEE80211_HTC_AMSDU)
295 vap->iv_flags_ht |= IEEE80211_FHT_AMSDU_TX;
297 if (vap->iv_htcaps & IEEE80211_HTCAP_TXSTBC)
298 vap->iv_flags_ht |= IEEE80211_FHT_STBC_TX;
299 if (vap->iv_htcaps & IEEE80211_HTCAP_RXSTBC)
300 vap->iv_flags_ht |= IEEE80211_FHT_STBC_RX;
302 /* NB: disable default legacy WDS, too many issues right now */
303 if (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY)
304 vap->iv_flags_ht &= ~IEEE80211_FHT_HT;
308 ieee80211_ht_vdetach(struct ieee80211vap *vap)
313 ht_getrate(struct ieee80211com *ic, int index, enum ieee80211_phymode mode,
318 mword = ieee80211_rate2media(ic, index | IEEE80211_RATE_MCS, mode);
319 if (IFM_SUBTYPE(mword) != IFM_IEEE80211_MCS)
323 rate = ieee80211_htrates[index].ht20_rate_800ns;
326 rate = ieee80211_htrates[index].ht20_rate_400ns;
329 rate = ieee80211_htrates[index].ht40_rate_800ns;
332 rate = ieee80211_htrates[index].ht40_rate_400ns;
338 static struct printranges {
349 { 32, 0, 1, 2, IEEE80211_HTC_TXMCS32 },
350 { 33, 38, 2, 0, IEEE80211_HTC_TXUNEQUAL },
351 { 39, 52, 3, 0, IEEE80211_HTC_TXUNEQUAL },
352 { 53, 76, 4, 0, IEEE80211_HTC_TXUNEQUAL },
357 ht_rateprint(struct ieee80211com *ic, enum ieee80211_phymode mode, int ratetype)
359 int minrate, maxrate;
360 struct printranges *range;
362 for (range = ranges; range->txstream != 0; range++) {
363 if (ic->ic_txstream < range->txstream)
365 if (range->htcapflags &&
366 (ic->ic_htcaps & range->htcapflags) == 0)
368 if (ratetype < range->ratetype)
370 minrate = ht_getrate(ic, range->minmcs, mode, ratetype);
371 maxrate = ht_getrate(ic, range->maxmcs, mode, ratetype);
373 ic_printf(ic, "MCS %d-%d: %d%sMbps - %d%sMbps\n",
374 range->minmcs, range->maxmcs,
375 minrate/2, ((minrate & 0x1) != 0 ? ".5" : ""),
376 maxrate/2, ((maxrate & 0x1) != 0 ? ".5" : ""));
378 ic_printf(ic, "MCS %d: %d%sMbps\n", range->minmcs,
379 minrate/2, ((minrate & 0x1) != 0 ? ".5" : ""));
385 ht_announce(struct ieee80211com *ic, enum ieee80211_phymode mode)
387 const char *modestr = ieee80211_phymode_name[mode];
389 ic_printf(ic, "%s MCS 20MHz\n", modestr);
390 ht_rateprint(ic, mode, 0);
391 if (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20) {
392 ic_printf(ic, "%s MCS 20MHz SGI\n", modestr);
393 ht_rateprint(ic, mode, 1);
395 if (ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
396 ic_printf(ic, "%s MCS 40MHz:\n", modestr);
397 ht_rateprint(ic, mode, 2);
399 if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
400 (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40)) {
401 ic_printf(ic, "%s MCS 40MHz SGI:\n", modestr);
402 ht_rateprint(ic, mode, 3);
407 ieee80211_ht_announce(struct ieee80211com *ic)
410 if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) ||
411 isset(ic->ic_modecaps, IEEE80211_MODE_11NG))
412 ic_printf(ic, "%dT%dR\n", ic->ic_txstream, ic->ic_rxstream);
413 if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA))
414 ht_announce(ic, IEEE80211_MODE_11NA);
415 if (isset(ic->ic_modecaps, IEEE80211_MODE_11NG))
416 ht_announce(ic, IEEE80211_MODE_11NG);
419 static struct ieee80211_htrateset htrateset;
421 const struct ieee80211_htrateset *
422 ieee80211_get_suphtrates(struct ieee80211com *ic,
423 const struct ieee80211_channel *c)
425 #define ADDRATE(x) do { \
426 htrateset.rs_rates[htrateset.rs_nrates] = x; \
427 htrateset.rs_nrates++; \
431 memset(&htrateset, 0, sizeof(struct ieee80211_htrateset));
432 for (i = 0; i < ic->ic_txstream * 8; i++)
434 if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
435 (ic->ic_htcaps & IEEE80211_HTC_TXMCS32))
437 if (ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL) {
438 if (ic->ic_txstream >= 2) {
439 for (i = 33; i <= 38; i++)
442 if (ic->ic_txstream >= 3) {
443 for (i = 39; i <= 52; i++)
446 if (ic->ic_txstream == 4) {
447 for (i = 53; i <= 76; i++)
456 * Receive processing.
460 * Decap the encapsulated A-MSDU frames and dispatch all but
461 * the last for delivery. The last frame is returned for
462 * delivery via the normal path.
465 ieee80211_decap_amsdu(struct ieee80211_node *ni, struct mbuf *m)
467 struct ieee80211vap *vap = ni->ni_vap;
471 /* discard 802.3 header inserted by ieee80211_decap */
472 m_adj(m, sizeof(struct ether_header));
474 vap->iv_stats.is_amsdu_decap++;
478 * Decap the first frame, bust it apart from the
479 * remainder and deliver. We leave the last frame
480 * delivery to the caller (for consistency with other
481 * code paths, could also do it here).
483 m = ieee80211_decap1(m, &framelen);
485 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
486 ni->ni_macaddr, "a-msdu", "%s", "decap failed");
487 vap->iv_stats.is_amsdu_tooshort++;
490 if (m->m_pkthdr.len == framelen)
492 n = m_split(m, framelen, M_NOWAIT);
494 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
495 ni->ni_macaddr, "a-msdu",
496 "%s", "unable to split encapsulated frames");
497 vap->iv_stats.is_amsdu_split++;
498 m_freem(m); /* NB: must reclaim */
501 vap->iv_deliver_data(vap, ni, m);
504 * Remove frame contents; each intermediate frame
505 * is required to be aligned to a 4-byte boundary.
508 m_adj(m, roundup2(framelen, 4) - framelen); /* padding */
510 return m; /* last delivered by caller */
514 * Purge all frames in the A-MPDU re-order queue.
517 ampdu_rx_purge(struct ieee80211_rx_ampdu *rap)
522 for (i = 0; i < rap->rxa_wnd; i++) {
525 rap->rxa_m[i] = NULL;
526 rap->rxa_qbytes -= m->m_pkthdr.len;
528 if (--rap->rxa_qframes == 0)
532 KASSERT(rap->rxa_qbytes == 0 && rap->rxa_qframes == 0,
533 ("lost %u data, %u frames on ampdu rx q",
534 rap->rxa_qbytes, rap->rxa_qframes));
538 * Start A-MPDU rx/re-order processing for the specified TID.
541 ampdu_rx_start(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap,
542 int baparamset, int batimeout, int baseqctl)
544 int bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
546 if (rap->rxa_flags & IEEE80211_AGGR_RUNNING) {
548 * AMPDU previously setup and not terminated with a DELBA,
549 * flush the reorder q's in case anything remains.
553 memset(rap, 0, sizeof(*rap));
554 rap->rxa_wnd = (bufsiz == 0) ?
555 IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
556 rap->rxa_start = MS(baseqctl, IEEE80211_BASEQ_START);
557 rap->rxa_flags |= IEEE80211_AGGR_RUNNING | IEEE80211_AGGR_XCHGPEND;
563 * Public function; manually setup the RX ampdu state.
566 ieee80211_ampdu_rx_start_ext(struct ieee80211_node *ni, int tid, int seq, int baw)
568 struct ieee80211_rx_ampdu *rap;
570 /* XXX TODO: sanity check tid, seq, baw */
572 rap = &ni->ni_rx_ampdu[tid];
574 if (rap->rxa_flags & IEEE80211_AGGR_RUNNING) {
576 * AMPDU previously setup and not terminated with a DELBA,
577 * flush the reorder q's in case anything remains.
582 memset(rap, 0, sizeof(*rap));
583 rap->rxa_wnd = (baw== 0) ?
584 IEEE80211_AGGR_BAWMAX : min(baw, IEEE80211_AGGR_BAWMAX);
586 /* Wait for the first RX frame, use that as BAW */
588 rap->rxa_flags |= IEEE80211_AGGR_WAITRX;
590 rap->rxa_start = seq;
592 rap->rxa_flags |= IEEE80211_AGGR_RUNNING | IEEE80211_AGGR_XCHGPEND;
594 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, ni,
595 "%s: tid=%d, start=%d, wnd=%d, flags=0x%08x",
606 * Public function; manually stop the RX AMPDU state.
609 ieee80211_ampdu_rx_stop_ext(struct ieee80211_node *ni, int tid)
611 struct ieee80211_rx_ampdu *rap;
613 /* XXX TODO: sanity check tid, seq, baw */
614 rap = &ni->ni_rx_ampdu[tid];
615 ampdu_rx_stop(ni, rap);
619 * Stop A-MPDU rx processing for the specified TID.
622 ampdu_rx_stop(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
626 rap->rxa_flags &= ~(IEEE80211_AGGR_RUNNING
627 | IEEE80211_AGGR_XCHGPEND
628 | IEEE80211_AGGR_WAITRX);
632 * Dispatch a frame from the A-MPDU reorder queue. The
633 * frame is fed back into ieee80211_input marked with an
634 * M_AMPDU_MPDU flag so it doesn't come back to us (it also
635 * permits ieee80211_input to optimize re-processing).
638 ampdu_dispatch(struct ieee80211_node *ni, struct mbuf *m)
640 m->m_flags |= M_AMPDU_MPDU; /* bypass normal processing */
641 /* NB: rssi and noise are ignored w/ M_AMPDU_MPDU set */
642 (void) ieee80211_input(ni, m, 0, 0);
646 * Dispatch as many frames as possible from the re-order queue.
647 * Frames will always be "at the front"; we process all frames
648 * up to the first empty slot in the window. On completion we
649 * cleanup state if there are still pending frames in the current
650 * BA window. We assume the frame at slot 0 is already handled
651 * by the caller; we always start at slot 1.
654 ampdu_rx_dispatch(struct ieee80211_rx_ampdu *rap, struct ieee80211_node *ni)
656 struct ieee80211vap *vap = ni->ni_vap;
660 /* flush run of frames */
661 for (i = 1; i < rap->rxa_wnd; i++) {
665 rap->rxa_m[i] = NULL;
666 rap->rxa_qbytes -= m->m_pkthdr.len;
669 ampdu_dispatch(ni, m);
672 * If frames remain, copy the mbuf pointers down so
673 * they correspond to the offsets in the new window.
675 if (rap->rxa_qframes != 0) {
676 int n = rap->rxa_qframes, j;
677 for (j = i+1; j < rap->rxa_wnd; j++) {
678 if (rap->rxa_m[j] != NULL) {
679 rap->rxa_m[j-i] = rap->rxa_m[j];
680 rap->rxa_m[j] = NULL;
685 KASSERT(n == 0, ("lost %d frames", n));
686 vap->iv_stats.is_ampdu_rx_copy += rap->rxa_qframes;
689 * Adjust the start of the BA window to
690 * reflect the frames just dispatched.
692 rap->rxa_start = IEEE80211_SEQ_ADD(rap->rxa_start, i);
693 vap->iv_stats.is_ampdu_rx_oor += i;
697 * Dispatch all frames in the A-MPDU re-order queue.
700 ampdu_rx_flush(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
702 struct ieee80211vap *vap = ni->ni_vap;
706 for (i = 0; i < rap->rxa_wnd; i++) {
710 rap->rxa_m[i] = NULL;
711 rap->rxa_qbytes -= m->m_pkthdr.len;
713 vap->iv_stats.is_ampdu_rx_oor++;
715 ampdu_dispatch(ni, m);
716 if (rap->rxa_qframes == 0)
722 * Dispatch all frames in the A-MPDU re-order queue
723 * preceding the specified sequence number. This logic
724 * handles window moves due to a received MSDU or BAR.
727 ampdu_rx_flush_upto(struct ieee80211_node *ni,
728 struct ieee80211_rx_ampdu *rap, ieee80211_seq winstart)
730 struct ieee80211vap *vap = ni->ni_vap;
736 * Flush any complete MSDU's with a sequence number lower
737 * than winstart. Gaps may exist. Note that we may actually
738 * dispatch frames past winstart if a run continues; this is
739 * an optimization that avoids having to do a separate pass
740 * to dispatch frames after moving the BA window start.
742 seqno = rap->rxa_start;
743 for (i = 0; i < rap->rxa_wnd; i++) {
746 rap->rxa_m[i] = NULL;
747 rap->rxa_qbytes -= m->m_pkthdr.len;
749 vap->iv_stats.is_ampdu_rx_oor++;
751 ampdu_dispatch(ni, m);
753 if (!IEEE80211_SEQ_BA_BEFORE(seqno, winstart))
756 seqno = IEEE80211_SEQ_INC(seqno);
759 * If frames remain, copy the mbuf pointers down so
760 * they correspond to the offsets in the new window.
762 if (rap->rxa_qframes != 0) {
763 int n = rap->rxa_qframes, j;
765 /* NB: this loop assumes i > 0 and/or rxa_m[0] is NULL */
766 KASSERT(rap->rxa_m[0] == NULL,
767 ("%s: BA window slot 0 occupied", __func__));
768 for (j = i+1; j < rap->rxa_wnd; j++) {
769 if (rap->rxa_m[j] != NULL) {
770 rap->rxa_m[j-i] = rap->rxa_m[j];
771 rap->rxa_m[j] = NULL;
776 KASSERT(n == 0, ("%s: lost %d frames, qframes %d off %d "
777 "BA win <%d:%d> winstart %d",
778 __func__, n, rap->rxa_qframes, i, rap->rxa_start,
779 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
781 vap->iv_stats.is_ampdu_rx_copy += rap->rxa_qframes;
784 * Move the start of the BA window; we use the
785 * sequence number of the last MSDU that was
786 * passed up the stack+1 or winstart if stopped on
787 * a gap in the reorder buffer.
789 rap->rxa_start = seqno;
793 * Process a received QoS data frame for an HT station. Handle
794 * A-MPDU reordering: if this frame is received out of order
795 * and falls within the BA window hold onto it. Otherwise if
796 * this frame completes a run, flush any pending frames. We
797 * return 1 if the frame is consumed. A 0 is returned if
798 * the frame should be processed normally by the caller.
801 ieee80211_ampdu_reorder(struct ieee80211_node *ni, struct mbuf *m)
803 #define PROCESS 0 /* caller should process frame */
804 #define CONSUMED 1 /* frame consumed, caller does nothing */
805 struct ieee80211vap *vap = ni->ni_vap;
806 struct ieee80211_qosframe *wh;
807 struct ieee80211_rx_ampdu *rap;
812 KASSERT((m->m_flags & (M_AMPDU | M_AMPDU_MPDU)) == M_AMPDU,
813 ("!a-mpdu or already re-ordered, flags 0x%x", m->m_flags));
814 KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT sta"));
816 /* NB: m_len known to be sufficient */
817 wh = mtod(m, struct ieee80211_qosframe *);
818 if (wh->i_fc[0] != IEEE80211_FC0_QOSDATA) {
820 * Not QoS data, shouldn't get here but just
821 * return it to the caller for processing.
825 if (IEEE80211_IS_DSTODS(wh))
826 tid = ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0];
829 tid &= IEEE80211_QOS_TID;
830 rap = &ni->ni_rx_ampdu[tid];
831 if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
833 * No ADDBA request yet, don't touch.
837 rxseq = le16toh(*(uint16_t *)wh->i_seq);
838 if ((rxseq & IEEE80211_SEQ_FRAG_MASK) != 0) {
840 * Fragments are not allowed; toss.
842 IEEE80211_DISCARD_MAC(vap,
843 IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
844 "A-MPDU", "fragment, rxseq 0x%x tid %u%s", rxseq, tid,
845 wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
846 vap->iv_stats.is_ampdu_rx_drop++;
847 IEEE80211_NODE_STAT(ni, rx_drop);
851 rxseq >>= IEEE80211_SEQ_SEQ_SHIFT;
855 * Handle waiting for the first frame to define the BAW.
856 * Some firmware doesn't provide the RX of the starting point
857 * of the BAW and we have to cope.
859 if (rap->rxa_flags & IEEE80211_AGGR_WAITRX) {
860 rap->rxa_flags &= ~IEEE80211_AGGR_WAITRX;
861 rap->rxa_start = rxseq;
864 if (rxseq == rap->rxa_start) {
866 * First frame in window.
868 if (rap->rxa_qframes != 0) {
870 * Dispatch as many packets as we can.
872 KASSERT(rap->rxa_m[0] == NULL, ("unexpected dup"));
873 ampdu_dispatch(ni, m);
874 ampdu_rx_dispatch(rap, ni);
878 * In order; advance window and notify
879 * caller to dispatch directly.
881 rap->rxa_start = IEEE80211_SEQ_INC(rxseq);
886 * Frame is out of order; store if in the BA window.
888 /* calculate offset in BA window */
889 off = IEEE80211_SEQ_SUB(rxseq, rap->rxa_start);
890 if (off < rap->rxa_wnd) {
892 * Common case (hopefully): in the BA window.
893 * Sec 9.10.7.6.2 a) (p.137)
897 * Check for frames sitting too long in the reorder queue.
898 * This should only ever happen if frames are not delivered
899 * without the sender otherwise notifying us (e.g. with a
900 * BAR to move the window). Typically this happens because
901 * of vendor bugs that cause the sequence number to jump.
902 * When this happens we get a gap in the reorder queue that
903 * leaves frame sitting on the queue until they get pushed
904 * out due to window moves. When the vendor does not send
905 * BAR this move only happens due to explicit packet sends
907 * NB: we only track the time of the oldest frame in the
908 * reorder q; this means that if we flush we might push
909 * frames that still "new"; if this happens then subsequent
910 * frames will result in BA window moves which cost something
911 * but is still better than a big throughput dip.
913 if (rap->rxa_qframes != 0) {
914 /* XXX honor batimeout? */
915 if (ticks - rap->rxa_age > ieee80211_ampdu_age) {
917 * Too long since we received the first
918 * frame; flush the reorder buffer.
920 if (rap->rxa_qframes != 0) {
921 vap->iv_stats.is_ampdu_rx_age +=
923 ampdu_rx_flush(ni, rap);
925 rap->rxa_start = IEEE80211_SEQ_INC(rxseq);
930 * First frame, start aging timer.
932 rap->rxa_age = ticks;
936 if (rap->rxa_m[off] == NULL) {
939 rap->rxa_qbytes += m->m_pkthdr.len;
940 vap->iv_stats.is_ampdu_rx_reorder++;
942 IEEE80211_DISCARD_MAC(vap,
943 IEEE80211_MSG_INPUT | IEEE80211_MSG_11N,
944 ni->ni_macaddr, "a-mpdu duplicate",
945 "seqno %u tid %u BA win <%u:%u>",
946 rxseq, tid, rap->rxa_start,
947 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1));
948 vap->iv_stats.is_rx_dup++;
949 IEEE80211_NODE_STAT(ni, rx_dup);
954 if (off < IEEE80211_SEQ_BA_RANGE) {
956 * Outside the BA window, but within range;
957 * flush the reorder q and move the window.
958 * Sec 9.10.7.6.2 b) (p.138)
960 IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
961 "move BA win <%u:%u> (%u frames) rxseq %u tid %u",
963 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
964 rap->rxa_qframes, rxseq, tid);
965 vap->iv_stats.is_ampdu_rx_move++;
968 * The spec says to flush frames up to but not including:
969 * WinStart_B = rxseq - rap->rxa_wnd + 1
970 * Then insert the frame or notify the caller to process
971 * it immediately. We can safely do this by just starting
972 * over again because we know the frame will now be within
975 /* NB: rxa_wnd known to be >0 */
976 ampdu_rx_flush_upto(ni, rap,
977 IEEE80211_SEQ_SUB(rxseq, rap->rxa_wnd-1));
981 * Outside the BA window and out of range; toss.
982 * Sec 9.10.7.6.2 c) (p.138)
984 IEEE80211_DISCARD_MAC(vap,
985 IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
986 "MPDU", "BA win <%u:%u> (%u frames) rxseq %u tid %u%s",
988 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
989 rap->rxa_qframes, rxseq, tid,
990 wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
991 vap->iv_stats.is_ampdu_rx_drop++;
992 IEEE80211_NODE_STAT(ni, rx_drop);
1001 * Process a BAR ctl frame. Dispatch all frames up to
1002 * the sequence number of the frame. If this frame is
1003 * out of range it's discarded.
1006 ieee80211_recv_bar(struct ieee80211_node *ni, struct mbuf *m0)
1008 struct ieee80211vap *vap = ni->ni_vap;
1009 struct ieee80211_frame_bar *wh;
1010 struct ieee80211_rx_ampdu *rap;
1011 ieee80211_seq rxseq;
1014 if (!ieee80211_recv_bar_ena) {
1016 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_11N,
1017 ni->ni_macaddr, "BAR", "%s", "processing disabled");
1019 vap->iv_stats.is_ampdu_bar_bad++;
1022 wh = mtod(m0, struct ieee80211_frame_bar *);
1023 /* XXX check basic BAR */
1024 tid = MS(le16toh(wh->i_ctl), IEEE80211_BAR_TID);
1025 rap = &ni->ni_rx_ampdu[tid];
1026 if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
1028 * No ADDBA request yet, don't touch.
1030 IEEE80211_DISCARD_MAC(vap,
1031 IEEE80211_MSG_INPUT | IEEE80211_MSG_11N,
1032 ni->ni_macaddr, "BAR", "no BA stream, tid %u", tid);
1033 vap->iv_stats.is_ampdu_bar_bad++;
1036 vap->iv_stats.is_ampdu_bar_rx++;
1037 rxseq = le16toh(wh->i_seq) >> IEEE80211_SEQ_SEQ_SHIFT;
1038 if (rxseq == rap->rxa_start)
1040 /* calculate offset in BA window */
1041 off = IEEE80211_SEQ_SUB(rxseq, rap->rxa_start);
1042 if (off < IEEE80211_SEQ_BA_RANGE) {
1044 * Flush the reorder q up to rxseq and move the window.
1045 * Sec 9.10.7.6.3 a) (p.138)
1047 IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
1048 "BAR moves BA win <%u:%u> (%u frames) rxseq %u tid %u",
1050 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
1051 rap->rxa_qframes, rxseq, tid);
1052 vap->iv_stats.is_ampdu_bar_move++;
1054 ampdu_rx_flush_upto(ni, rap, rxseq);
1055 if (off >= rap->rxa_wnd) {
1057 * BAR specifies a window start to the right of BA
1058 * window; we must move it explicitly since
1059 * ampdu_rx_flush_upto will not.
1061 rap->rxa_start = rxseq;
1065 * Out of range; toss.
1066 * Sec 9.10.7.6.3 b) (p.138)
1068 IEEE80211_DISCARD_MAC(vap,
1069 IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
1070 "BAR", "BA win <%u:%u> (%u frames) rxseq %u tid %u%s",
1072 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
1073 rap->rxa_qframes, rxseq, tid,
1074 wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
1075 vap->iv_stats.is_ampdu_bar_oow++;
1076 IEEE80211_NODE_STAT(ni, rx_drop);
1081 * Setup HT-specific state in a node. Called only
1082 * when HT use is negotiated so we don't do extra
1083 * work for temporary and/or legacy sta's.
1086 ieee80211_ht_node_init(struct ieee80211_node *ni)
1088 struct ieee80211_tx_ampdu *tap;
1091 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
1097 if (ni->ni_flags & IEEE80211_NODE_HT) {
1099 * Clean AMPDU state on re-associate. This handles the case
1100 * where a station leaves w/o notifying us and then returns
1101 * before node is reaped for inactivity.
1103 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
1105 "%s: calling cleanup (%p)",
1107 ieee80211_ht_node_cleanup(ni);
1109 for (tid = 0; tid < WME_NUM_TID; tid++) {
1110 tap = &ni->ni_tx_ampdu[tid];
1113 ieee80211_txampdu_init_pps(tap);
1114 /* NB: further initialization deferred */
1116 ni->ni_flags |= IEEE80211_NODE_HT | IEEE80211_NODE_AMPDU;
1120 * Cleanup HT-specific state in a node. Called only
1121 * when HT use has been marked.
1124 ieee80211_ht_node_cleanup(struct ieee80211_node *ni)
1126 struct ieee80211com *ic = ni->ni_ic;
1129 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
1134 KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT node"));
1136 /* XXX optimize this */
1137 for (i = 0; i < WME_NUM_TID; i++) {
1138 struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[i];
1139 if (tap->txa_flags & IEEE80211_AGGR_SETUP)
1142 for (i = 0; i < WME_NUM_TID; i++)
1143 ic->ic_ampdu_rx_stop(ni, &ni->ni_rx_ampdu[i]);
1146 ni->ni_flags &= ~IEEE80211_NODE_HT_ALL;
1150 * Age out HT resources for a station.
1153 ieee80211_ht_node_age(struct ieee80211_node *ni)
1155 struct ieee80211vap *vap = ni->ni_vap;
1158 KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT sta"));
1160 for (tid = 0; tid < WME_NUM_TID; tid++) {
1161 struct ieee80211_rx_ampdu *rap;
1163 rap = &ni->ni_rx_ampdu[tid];
1164 if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0)
1166 if (rap->rxa_qframes == 0)
1169 * Check for frames sitting too long in the reorder queue.
1170 * See above for more details on what's happening here.
1172 /* XXX honor batimeout? */
1173 if (ticks - rap->rxa_age > ieee80211_ampdu_age) {
1175 * Too long since we received the first
1176 * frame; flush the reorder buffer.
1178 vap->iv_stats.is_ampdu_rx_age += rap->rxa_qframes;
1179 ampdu_rx_flush(ni, rap);
1184 static struct ieee80211_channel *
1185 findhtchan(struct ieee80211com *ic, struct ieee80211_channel *c, int htflags)
1187 return ieee80211_find_channel(ic, c->ic_freq,
1188 (c->ic_flags &~ IEEE80211_CHAN_HT) | htflags);
1192 * Adjust a channel to be HT/non-HT according to the vap's configuration.
1194 struct ieee80211_channel *
1195 ieee80211_ht_adjust_channel(struct ieee80211com *ic,
1196 struct ieee80211_channel *chan, int flags)
1198 struct ieee80211_channel *c;
1200 if (flags & IEEE80211_FHT_HT) {
1201 /* promote to HT if possible */
1202 if (flags & IEEE80211_FHT_USEHT40) {
1203 if (!IEEE80211_IS_CHAN_HT40(chan)) {
1204 /* NB: arbitrarily pick ht40+ over ht40- */
1205 c = findhtchan(ic, chan, IEEE80211_CHAN_HT40U);
1207 c = findhtchan(ic, chan,
1208 IEEE80211_CHAN_HT40D);
1210 c = findhtchan(ic, chan,
1211 IEEE80211_CHAN_HT20);
1215 } else if (!IEEE80211_IS_CHAN_HT20(chan)) {
1216 c = findhtchan(ic, chan, IEEE80211_CHAN_HT20);
1220 } else if (IEEE80211_IS_CHAN_HT(chan)) {
1221 /* demote to legacy, HT use is disabled */
1222 c = ieee80211_find_channel(ic, chan->ic_freq,
1223 chan->ic_flags &~ IEEE80211_CHAN_HT);
1231 * Setup HT-specific state for a legacy WDS peer.
1234 ieee80211_ht_wds_init(struct ieee80211_node *ni)
1236 struct ieee80211vap *vap = ni->ni_vap;
1237 struct ieee80211_tx_ampdu *tap;
1240 KASSERT(vap->iv_flags_ht & IEEE80211_FHT_HT, ("no HT requested"));
1242 /* XXX check scan cache in case peer has an ap and we have info */
1244 * If setup with a legacy channel; locate an HT channel.
1245 * Otherwise if the inherited channel (from a companion
1246 * AP) is suitable use it so we use the same location
1247 * for the extension channel).
1249 ni->ni_chan = ieee80211_ht_adjust_channel(ni->ni_ic,
1250 ni->ni_chan, ieee80211_htchanflags(ni->ni_chan));
1253 if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20)
1254 ni->ni_htcap |= IEEE80211_HTCAP_SHORTGI20;
1255 if (IEEE80211_IS_CHAN_HT40(ni->ni_chan)) {
1256 ni->ni_htcap |= IEEE80211_HTCAP_CHWIDTH40;
1258 if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan))
1259 ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_ABOVE;
1260 else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan))
1261 ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_BELOW;
1262 if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40)
1263 ni->ni_htcap |= IEEE80211_HTCAP_SHORTGI40;
1266 ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_NONE;
1268 ni->ni_htctlchan = ni->ni_chan->ic_ieee;
1269 if (vap->iv_flags_ht & IEEE80211_FHT_RIFS)
1270 ni->ni_flags |= IEEE80211_NODE_RIFS;
1271 /* XXX does it make sense to enable SMPS? */
1273 ni->ni_htopmode = 0; /* XXX need protection state */
1274 ni->ni_htstbc = 0; /* XXX need info */
1276 for (tid = 0; tid < WME_NUM_TID; tid++) {
1277 tap = &ni->ni_tx_ampdu[tid];
1279 ieee80211_txampdu_init_pps(tap);
1281 /* NB: AMPDU tx/rx governed by IEEE80211_FHT_AMPDU_{TX,RX} */
1282 ni->ni_flags |= IEEE80211_NODE_HT | IEEE80211_NODE_AMPDU;
1286 * Notify hostap vaps of a change in the HTINFO ie.
1289 htinfo_notify(struct ieee80211com *ic)
1291 struct ieee80211vap *vap;
1294 IEEE80211_LOCK_ASSERT(ic);
1296 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1297 if (vap->iv_opmode != IEEE80211_M_HOSTAP)
1299 if (vap->iv_state != IEEE80211_S_RUN ||
1300 !IEEE80211_IS_CHAN_HT(vap->iv_bss->ni_chan))
1304 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N,
1306 "HT bss occupancy change: %d sta, %d ht, "
1307 "%d ht40%s, HT protmode now 0x%x"
1309 , ic->ic_ht_sta_assoc
1310 , ic->ic_ht40_sta_assoc
1311 , (ic->ic_flags_ht & IEEE80211_FHT_NONHT_PR) ?
1312 ", non-HT sta present" : ""
1313 , ic->ic_curhtprotmode);
1316 ieee80211_beacon_notify(vap, IEEE80211_BEACON_HTINFO);
1321 * Calculate HT protection mode from current
1322 * state and handle updates.
1325 htinfo_update(struct ieee80211com *ic)
1329 if (ic->ic_sta_assoc != ic->ic_ht_sta_assoc) {
1330 protmode = IEEE80211_HTINFO_OPMODE_MIXED
1331 | IEEE80211_HTINFO_NONHT_PRESENT;
1332 } else if (ic->ic_flags_ht & IEEE80211_FHT_NONHT_PR) {
1333 protmode = IEEE80211_HTINFO_OPMODE_PROTOPT
1334 | IEEE80211_HTINFO_NONHT_PRESENT;
1335 } else if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
1336 IEEE80211_IS_CHAN_HT40(ic->ic_bsschan) &&
1337 ic->ic_sta_assoc != ic->ic_ht40_sta_assoc) {
1338 protmode = IEEE80211_HTINFO_OPMODE_HT20PR;
1340 protmode = IEEE80211_HTINFO_OPMODE_PURE;
1342 if (protmode != ic->ic_curhtprotmode) {
1343 ic->ic_curhtprotmode = protmode;
1349 * Handle an HT station joining a BSS.
1352 ieee80211_ht_node_join(struct ieee80211_node *ni)
1354 struct ieee80211com *ic = ni->ni_ic;
1356 IEEE80211_LOCK_ASSERT(ic);
1358 if (ni->ni_flags & IEEE80211_NODE_HT) {
1359 ic->ic_ht_sta_assoc++;
1360 if (ni->ni_chw == 40)
1361 ic->ic_ht40_sta_assoc++;
1367 * Handle an HT station leaving a BSS.
1370 ieee80211_ht_node_leave(struct ieee80211_node *ni)
1372 struct ieee80211com *ic = ni->ni_ic;
1374 IEEE80211_LOCK_ASSERT(ic);
1376 if (ni->ni_flags & IEEE80211_NODE_HT) {
1377 ic->ic_ht_sta_assoc--;
1378 if (ni->ni_chw == 40)
1379 ic->ic_ht40_sta_assoc--;
1385 * Public version of htinfo_update; used for processing
1386 * beacon frames from overlapping bss.
1388 * Caller can specify either IEEE80211_HTINFO_OPMODE_MIXED
1389 * (on receipt of a beacon that advertises MIXED) or
1390 * IEEE80211_HTINFO_OPMODE_PROTOPT (on receipt of a beacon
1391 * from an overlapping legacy bss). We treat MIXED with
1392 * a higher precedence than PROTOPT (i.e. we will not change
1393 * change PROTOPT -> MIXED; only MIXED -> PROTOPT). This
1394 * corresponds to how we handle things in htinfo_update.
1397 ieee80211_htprot_update(struct ieee80211com *ic, int protmode)
1399 #define OPMODE(x) SM(x, IEEE80211_HTINFO_OPMODE)
1402 /* track non-HT station presence */
1403 KASSERT(protmode & IEEE80211_HTINFO_NONHT_PRESENT,
1404 ("protmode 0x%x", protmode));
1405 ic->ic_flags_ht |= IEEE80211_FHT_NONHT_PR;
1406 ic->ic_lastnonht = ticks;
1408 if (protmode != ic->ic_curhtprotmode &&
1409 (OPMODE(ic->ic_curhtprotmode) != IEEE80211_HTINFO_OPMODE_MIXED ||
1410 OPMODE(protmode) == IEEE80211_HTINFO_OPMODE_PROTOPT)) {
1411 /* push beacon update */
1412 ic->ic_curhtprotmode = protmode;
1415 IEEE80211_UNLOCK(ic);
1420 * Time out presence of an overlapping bss with non-HT
1421 * stations. When operating in hostap mode we listen for
1422 * beacons from other stations and if we identify a non-HT
1423 * station is present we update the opmode field of the
1424 * HTINFO ie. To identify when all non-HT stations are
1425 * gone we time out this condition.
1428 ieee80211_ht_timeout(struct ieee80211com *ic)
1430 IEEE80211_LOCK_ASSERT(ic);
1432 if ((ic->ic_flags_ht & IEEE80211_FHT_NONHT_PR) &&
1433 ieee80211_time_after(ticks, ic->ic_lastnonht + IEEE80211_NONHT_PRESENT_AGE)) {
1435 IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
1436 "%s", "time out non-HT STA present on channel");
1438 ic->ic_flags_ht &= ~IEEE80211_FHT_NONHT_PR;
1444 * Process an 802.11n HT capabilities ie.
1447 ieee80211_parse_htcap(struct ieee80211_node *ni, const uint8_t *ie)
1449 if (ie[0] == IEEE80211_ELEMID_VENDOR) {
1451 * Station used Vendor OUI ie to associate;
1452 * mark the node so when we respond we'll use
1453 * the Vendor OUI's and not the standard ie's.
1455 ni->ni_flags |= IEEE80211_NODE_HTCOMPAT;
1458 ni->ni_flags &= ~IEEE80211_NODE_HTCOMPAT;
1460 ni->ni_htcap = le16dec(ie +
1461 __offsetof(struct ieee80211_ie_htcap, hc_cap));
1462 ni->ni_htparam = ie[__offsetof(struct ieee80211_ie_htcap, hc_param)];
1466 htinfo_parse(struct ieee80211_node *ni,
1467 const struct ieee80211_ie_htinfo *htinfo)
1471 ni->ni_htctlchan = htinfo->hi_ctrlchannel;
1472 ni->ni_ht2ndchan = SM(htinfo->hi_byte1, IEEE80211_HTINFO_2NDCHAN);
1473 w = le16dec(&htinfo->hi_byte2);
1474 ni->ni_htopmode = SM(w, IEEE80211_HTINFO_OPMODE);
1475 w = le16dec(&htinfo->hi_byte45);
1476 ni->ni_htstbc = SM(w, IEEE80211_HTINFO_BASIC_STBCMCS);
1480 * Parse an 802.11n HT info ie and save useful information
1481 * to the node state. Note this does not effect any state
1482 * changes such as for channel width change.
1485 ieee80211_parse_htinfo(struct ieee80211_node *ni, const uint8_t *ie)
1487 if (ie[0] == IEEE80211_ELEMID_VENDOR)
1489 htinfo_parse(ni, (const struct ieee80211_ie_htinfo *) ie);
1493 * Handle 11n/11ac channel switch.
1495 * Use the received HT/VHT ie's to identify the right channel to use.
1496 * If we cannot locate it in the channel table then fallback to
1499 * Note that we use this information to identify the node's
1500 * channel only; the caller is responsible for insuring any
1501 * required channel change is done (e.g. in sta mode when
1502 * parsing the contents of a beacon frame).
1505 htinfo_update_chw(struct ieee80211_node *ni, int htflags, int vhtflags)
1507 struct ieee80211com *ic = ni->ni_ic;
1508 struct ieee80211_channel *c;
1513 * First step - do HT/VHT only channel lookup based on operating mode
1514 * flags. This involves masking out the VHT flags as well.
1515 * Otherwise we end up doing the full channel walk each time
1516 * we trigger this, which is expensive.
1518 chanflags = (ni->ni_chan->ic_flags &~
1519 (IEEE80211_CHAN_HT | IEEE80211_CHAN_VHT)) | htflags | vhtflags;
1521 if (chanflags == ni->ni_chan->ic_flags)
1525 * If HT /or/ VHT flags have changed then check both.
1526 * We need to start by picking a HT channel anyway.
1530 chanflags = (ni->ni_chan->ic_flags &~
1531 (IEEE80211_CHAN_HT | IEEE80211_CHAN_VHT)) | htflags;
1532 /* XXX not right for ht40- */
1533 c = ieee80211_find_channel(ic, ni->ni_chan->ic_freq, chanflags);
1534 if (c == NULL && (htflags & IEEE80211_CHAN_HT40)) {
1536 * No HT40 channel entry in our table; fall back
1537 * to HT20 operation. This should not happen.
1539 c = findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT20);
1541 IEEE80211_NOTE(ni->ni_vap,
1542 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
1543 "no HT40 channel (freq %u), falling back to HT20",
1544 ni->ni_chan->ic_freq);
1549 /* Nothing found - leave it alone; move onto VHT */
1554 * If it's non-HT, then bail out now.
1556 if (! IEEE80211_IS_CHAN_HT(c)) {
1557 IEEE80211_NOTE(ni->ni_vap,
1558 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
1559 "not HT; skipping VHT check (%u/0x%x)",
1560 c->ic_freq, c->ic_flags);
1565 * Next step - look at the current VHT flags and determine
1566 * if we need to upgrade. Mask out the VHT and HT flags since
1567 * the vhtflags field will already have the correct HT
1570 if (IEEE80211_CONF_VHT(ic) && ni->ni_vhtcap != 0 && vhtflags != 0) {
1571 chanflags = (c->ic_flags
1572 &~ (IEEE80211_CHAN_HT | IEEE80211_CHAN_VHT))
1574 IEEE80211_NOTE(ni->ni_vap,
1575 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N,
1577 "%s: VHT; chanwidth=0x%02x; vhtflags=0x%08x",
1578 __func__, ni->ni_vht_chanwidth, vhtflags);
1580 IEEE80211_NOTE(ni->ni_vap,
1581 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N,
1583 "%s: VHT; trying lookup for %d/0x%08x",
1584 __func__, c->ic_freq, chanflags);
1585 c = ieee80211_find_channel(ic, c->ic_freq, chanflags);
1588 /* Finally, if it's changed */
1589 if (c != NULL && c != ni->ni_chan) {
1590 IEEE80211_NOTE(ni->ni_vap,
1591 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
1592 "switch station to %s%d channel %u/0x%x",
1593 IEEE80211_IS_CHAN_VHT(c) ? "VHT" : "HT",
1594 IEEE80211_IS_CHAN_VHT80(c) ? 80 :
1595 (IEEE80211_IS_CHAN_HT40(c) ? 40 : 20),
1596 c->ic_freq, c->ic_flags);
1600 /* NB: caller responsible for forcing any channel change */
1603 /* update node's (11n) tx channel width */
1604 ni->ni_chw = IEEE80211_IS_CHAN_HT40(ni->ni_chan)? 40 : 20;
1609 * Update 11n MIMO PS state according to received htcap.
1612 htcap_update_mimo_ps(struct ieee80211_node *ni)
1614 uint16_t oflags = ni->ni_flags;
1616 switch (ni->ni_htcap & IEEE80211_HTCAP_SMPS) {
1617 case IEEE80211_HTCAP_SMPS_DYNAMIC:
1618 ni->ni_flags |= IEEE80211_NODE_MIMO_PS;
1619 ni->ni_flags |= IEEE80211_NODE_MIMO_RTS;
1621 case IEEE80211_HTCAP_SMPS_ENA:
1622 ni->ni_flags |= IEEE80211_NODE_MIMO_PS;
1623 ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS;
1625 case IEEE80211_HTCAP_SMPS_OFF:
1626 default: /* disable on rx of reserved value */
1627 ni->ni_flags &= ~IEEE80211_NODE_MIMO_PS;
1628 ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS;
1631 return (oflags ^ ni->ni_flags);
1635 * Update short GI state according to received htcap
1636 * and local settings.
1638 static __inline void
1639 htcap_update_shortgi(struct ieee80211_node *ni)
1641 struct ieee80211vap *vap = ni->ni_vap;
1643 ni->ni_flags &= ~(IEEE80211_NODE_SGI20|IEEE80211_NODE_SGI40);
1644 if ((ni->ni_htcap & IEEE80211_HTCAP_SHORTGI20) &&
1645 (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20))
1646 ni->ni_flags |= IEEE80211_NODE_SGI20;
1647 if ((ni->ni_htcap & IEEE80211_HTCAP_SHORTGI40) &&
1648 (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40))
1649 ni->ni_flags |= IEEE80211_NODE_SGI40;
1653 * Parse and update HT-related state extracted from
1654 * the HT cap and info ie's.
1656 * This is called from the STA management path and
1657 * the ieee80211_node_join() path. It will take into
1658 * account the IEs discovered during scanning and
1659 * adjust things accordingly.
1662 ieee80211_ht_updateparams(struct ieee80211_node *ni,
1663 const uint8_t *htcapie, const uint8_t *htinfoie)
1665 struct ieee80211vap *vap = ni->ni_vap;
1666 const struct ieee80211_ie_htinfo *htinfo;
1668 ieee80211_parse_htcap(ni, htcapie);
1669 if (vap->iv_htcaps & IEEE80211_HTCAP_SMPS)
1670 htcap_update_mimo_ps(ni);
1671 htcap_update_shortgi(ni);
1673 if (htinfoie[0] == IEEE80211_ELEMID_VENDOR)
1675 htinfo = (const struct ieee80211_ie_htinfo *) htinfoie;
1676 htinfo_parse(ni, htinfo);
1679 * Defer the node channel change; we need to now
1680 * update VHT parameters before we do it.
1683 if ((htinfo->hi_byte1 & IEEE80211_HTINFO_RIFSMODE_PERM) &&
1684 (vap->iv_flags_ht & IEEE80211_FHT_RIFS))
1685 ni->ni_flags |= IEEE80211_NODE_RIFS;
1687 ni->ni_flags &= ~IEEE80211_NODE_RIFS;
1691 ieee80211_vht_get_vhtflags(struct ieee80211_node *ni, uint32_t htflags)
1693 struct ieee80211vap *vap = ni->ni_vap;
1694 uint32_t vhtflags = 0;
1697 if (ni->ni_flags & IEEE80211_NODE_VHT && vap->iv_flags_vht & IEEE80211_FVHT_VHT) {
1698 if ((ni->ni_vht_chanwidth == IEEE80211_VHT_CHANWIDTH_160MHZ) &&
1699 /* XXX 2 means "160MHz and 80+80MHz", 1 means "160MHz" */
1700 (MS(vap->iv_vhtcaps,
1701 IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_MASK) >= 1) &&
1702 (vap->iv_flags_vht & IEEE80211_FVHT_USEVHT160)) {
1703 vhtflags = IEEE80211_CHAN_VHT160;
1704 /* Mirror the HT40 flags */
1705 if (htflags == IEEE80211_CHAN_HT40U) {
1706 vhtflags |= IEEE80211_CHAN_HT40U;
1707 } else if (htflags == IEEE80211_CHAN_HT40D) {
1708 vhtflags |= IEEE80211_CHAN_HT40D;
1710 } else if ((ni->ni_vht_chanwidth == IEEE80211_VHT_CHANWIDTH_80P80MHZ) &&
1711 /* XXX 2 means "160MHz and 80+80MHz" */
1712 (MS(vap->iv_vhtcaps,
1713 IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_MASK) == 2) &&
1714 (vap->iv_flags_vht & IEEE80211_FVHT_USEVHT80P80)) {
1715 vhtflags = IEEE80211_CHAN_VHT80_80;
1716 /* Mirror the HT40 flags */
1717 if (htflags == IEEE80211_CHAN_HT40U) {
1718 vhtflags |= IEEE80211_CHAN_HT40U;
1719 } else if (htflags == IEEE80211_CHAN_HT40D) {
1720 vhtflags |= IEEE80211_CHAN_HT40D;
1722 } else if ((ni->ni_vht_chanwidth == IEEE80211_VHT_CHANWIDTH_80MHZ) &&
1723 (vap->iv_flags_vht & IEEE80211_FVHT_USEVHT80)) {
1724 vhtflags = IEEE80211_CHAN_VHT80;
1725 /* Mirror the HT40 flags */
1726 if (htflags == IEEE80211_CHAN_HT40U) {
1727 vhtflags |= IEEE80211_CHAN_HT40U;
1728 } else if (htflags == IEEE80211_CHAN_HT40D) {
1729 vhtflags |= IEEE80211_CHAN_HT40D;
1731 } else if (ni->ni_vht_chanwidth == IEEE80211_VHT_CHANWIDTH_USE_HT) {
1732 /* Mirror the HT40 flags */
1734 * XXX TODO: if ht40 is disabled, but vht40 isn't
1735 * disabled then this logic will get very, very sad.
1736 * It's quite possible the only sane thing to do is
1737 * to not have vht40 as an option, and just obey
1738 * 'ht40' as that flag.
1740 if ((htflags == IEEE80211_CHAN_HT40U) &&
1741 (vap->iv_flags_vht & IEEE80211_FVHT_USEVHT40)) {
1742 vhtflags = IEEE80211_CHAN_VHT40U
1743 | IEEE80211_CHAN_HT40U;
1744 } else if (htflags == IEEE80211_CHAN_HT40D &&
1745 (vap->iv_flags_vht & IEEE80211_FVHT_USEVHT40)) {
1746 vhtflags = IEEE80211_CHAN_VHT40D
1747 | IEEE80211_CHAN_HT40D;
1748 } else if (htflags == IEEE80211_CHAN_HT20) {
1749 vhtflags = IEEE80211_CHAN_VHT20
1750 | IEEE80211_CHAN_HT20;
1753 vhtflags = IEEE80211_CHAN_VHT20;
1760 * Final part of updating the HT parameters.
1762 * This is called from the STA management path and
1763 * the ieee80211_node_join() path. It will take into
1764 * account the IEs discovered during scanning and
1765 * adjust things accordingly.
1767 * This is done after a call to ieee80211_ht_updateparams()
1768 * because it (and the upcoming VHT version of updateparams)
1769 * needs to ensure everything is parsed before htinfo_update_chw()
1770 * is called - which will change the channel config for the
1774 ieee80211_ht_updateparams_final(struct ieee80211_node *ni,
1775 const uint8_t *htcapie, const uint8_t *htinfoie)
1777 struct ieee80211vap *vap = ni->ni_vap;
1778 const struct ieee80211_ie_htinfo *htinfo;
1779 int htflags, vhtflags;
1782 htinfo = (const struct ieee80211_ie_htinfo *) htinfoie;
1784 htflags = (vap->iv_flags_ht & IEEE80211_FHT_HT) ?
1785 IEEE80211_CHAN_HT20 : 0;
1787 /* NB: honor operating mode constraint */
1788 if ((htinfo->hi_byte1 & IEEE80211_HTINFO_TXWIDTH_2040) &&
1789 (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)) {
1790 if (ni->ni_ht2ndchan == IEEE80211_HTINFO_2NDCHAN_ABOVE)
1791 htflags = IEEE80211_CHAN_HT40U;
1792 else if (ni->ni_ht2ndchan == IEEE80211_HTINFO_2NDCHAN_BELOW)
1793 htflags = IEEE80211_CHAN_HT40D;
1797 * VHT flags - do much the same; check whether VHT is available
1798 * and if so, what our ideal channel use would be based on our
1799 * capabilities and the (pre-parsed) VHT info IE.
1801 vhtflags = ieee80211_vht_get_vhtflags(ni, htflags);
1803 if (htinfo_update_chw(ni, htflags, vhtflags))
1810 * Parse and update HT-related state extracted from the HT cap ie
1811 * for a station joining an HT BSS.
1813 * This is called from the hostap path for each station.
1816 ieee80211_ht_updatehtcap(struct ieee80211_node *ni, const uint8_t *htcapie)
1818 struct ieee80211vap *vap = ni->ni_vap;
1820 ieee80211_parse_htcap(ni, htcapie);
1821 if (vap->iv_htcaps & IEEE80211_HTCAP_SMPS)
1822 htcap_update_mimo_ps(ni);
1823 htcap_update_shortgi(ni);
1827 * Called once HT and VHT capabilities are parsed in hostap mode -
1828 * this will adjust the channel configuration of the given node
1829 * based on the configuration and capabilities.
1832 ieee80211_ht_updatehtcap_final(struct ieee80211_node *ni)
1834 struct ieee80211vap *vap = ni->ni_vap;
1838 /* NB: honor operating mode constraint */
1839 /* XXX 40 MHz intolerant */
1840 htflags = (vap->iv_flags_ht & IEEE80211_FHT_HT) ?
1841 IEEE80211_CHAN_HT20 : 0;
1842 if ((ni->ni_htcap & IEEE80211_HTCAP_CHWIDTH40) &&
1843 (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)) {
1844 if (IEEE80211_IS_CHAN_HT40U(vap->iv_bss->ni_chan))
1845 htflags = IEEE80211_CHAN_HT40U;
1846 else if (IEEE80211_IS_CHAN_HT40D(vap->iv_bss->ni_chan))
1847 htflags = IEEE80211_CHAN_HT40D;
1850 * VHT flags - do much the same; check whether VHT is available
1851 * and if so, what our ideal channel use would be based on our
1852 * capabilities and the (pre-parsed) VHT info IE.
1854 vhtflags = ieee80211_vht_get_vhtflags(ni, htflags);
1856 (void) htinfo_update_chw(ni, htflags, vhtflags);
1860 * Install received HT rate set by parsing the HT cap ie.
1863 ieee80211_setup_htrates(struct ieee80211_node *ni, const uint8_t *ie, int flags)
1865 struct ieee80211com *ic = ni->ni_ic;
1866 struct ieee80211vap *vap = ni->ni_vap;
1867 const struct ieee80211_ie_htcap *htcap;
1868 struct ieee80211_htrateset *rs;
1869 int i, maxequalmcs, maxunequalmcs;
1871 maxequalmcs = ic->ic_txstream * 8 - 1;
1873 if (ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL) {
1874 if (ic->ic_txstream >= 2)
1876 if (ic->ic_txstream >= 3)
1878 if (ic->ic_txstream >= 4)
1882 rs = &ni->ni_htrates;
1883 memset(rs, 0, sizeof(*rs));
1885 if (ie[0] == IEEE80211_ELEMID_VENDOR)
1887 htcap = (const struct ieee80211_ie_htcap *) ie;
1888 for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++) {
1889 if (isclr(htcap->hc_mcsset, i))
1891 if (rs->rs_nrates == IEEE80211_HTRATE_MAXSIZE) {
1893 IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
1894 "WARNING, HT rate set too large; only "
1895 "using %u rates", IEEE80211_HTRATE_MAXSIZE);
1896 vap->iv_stats.is_rx_rstoobig++;
1899 if (i <= 31 && i > maxequalmcs)
1902 (ic->ic_htcaps & IEEE80211_HTC_TXMCS32) == 0)
1904 if (i > 32 && i > maxunequalmcs)
1906 rs->rs_rates[rs->rs_nrates++] = i;
1909 return ieee80211_fix_rate(ni, (struct ieee80211_rateset *) rs, flags);
1913 * Mark rates in a node's HT rate set as basic according
1914 * to the information in the supplied HT info ie.
1917 ieee80211_setup_basic_htrates(struct ieee80211_node *ni, const uint8_t *ie)
1919 const struct ieee80211_ie_htinfo *htinfo;
1920 struct ieee80211_htrateset *rs;
1923 if (ie[0] == IEEE80211_ELEMID_VENDOR)
1925 htinfo = (const struct ieee80211_ie_htinfo *) ie;
1926 rs = &ni->ni_htrates;
1927 if (rs->rs_nrates == 0) {
1928 IEEE80211_NOTE(ni->ni_vap,
1929 IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
1930 "%s", "WARNING, empty HT rate set");
1933 for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++) {
1934 if (isclr(htinfo->hi_basicmcsset, i))
1936 for (j = 0; j < rs->rs_nrates; j++)
1937 if ((rs->rs_rates[j] & IEEE80211_RATE_VAL) == i)
1938 rs->rs_rates[j] |= IEEE80211_RATE_BASIC;
1943 ampdu_tx_setup(struct ieee80211_tx_ampdu *tap)
1945 callout_init(&tap->txa_timer, 1);
1946 tap->txa_flags |= IEEE80211_AGGR_SETUP;
1947 tap->txa_lastsample = ticks;
1951 ampdu_tx_stop(struct ieee80211_tx_ampdu *tap)
1953 struct ieee80211_node *ni = tap->txa_ni;
1954 struct ieee80211com *ic = ni->ni_ic;
1956 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
1961 KASSERT(tap->txa_flags & IEEE80211_AGGR_SETUP,
1962 ("txa_flags 0x%x tid %d ac %d", tap->txa_flags, tap->txa_tid,
1963 TID_TO_WME_AC(tap->txa_tid)));
1966 * Stop BA stream if setup so driver has a chance
1967 * to reclaim any resources it might have allocated.
1969 ic->ic_addba_stop(ni, tap);
1971 * Stop any pending BAR transmit.
1973 bar_stop_timer(tap);
1976 * Reset packet estimate.
1978 ieee80211_txampdu_init_pps(tap);
1980 /* NB: clearing NAK means we may re-send ADDBA */
1981 tap->txa_flags &= ~(IEEE80211_AGGR_SETUP | IEEE80211_AGGR_NAK);
1985 * ADDBA response timeout.
1987 * If software aggregation and per-TID queue management was done here,
1988 * that queue would be unpaused after the ADDBA timeout occurs.
1991 addba_timeout(void *arg)
1993 struct ieee80211_tx_ampdu *tap = arg;
1994 struct ieee80211_node *ni = tap->txa_ni;
1995 struct ieee80211com *ic = ni->ni_ic;
1998 tap->txa_flags &= ~IEEE80211_AGGR_XCHGPEND;
1999 tap->txa_attempts++;
2000 ic->ic_addba_response_timeout(ni, tap);
2004 addba_start_timeout(struct ieee80211_tx_ampdu *tap)
2006 /* XXX use CALLOUT_PENDING instead? */
2007 callout_reset(&tap->txa_timer, ieee80211_addba_timeout,
2008 addba_timeout, tap);
2009 tap->txa_flags |= IEEE80211_AGGR_XCHGPEND;
2010 tap->txa_nextrequest = ticks + ieee80211_addba_timeout;
2014 addba_stop_timeout(struct ieee80211_tx_ampdu *tap)
2016 /* XXX use CALLOUT_PENDING instead? */
2017 if (tap->txa_flags & IEEE80211_AGGR_XCHGPEND) {
2018 callout_stop(&tap->txa_timer);
2019 tap->txa_flags &= ~IEEE80211_AGGR_XCHGPEND;
2024 null_addba_response_timeout(struct ieee80211_node *ni,
2025 struct ieee80211_tx_ampdu *tap)
2030 * Default method for requesting A-MPDU tx aggregation.
2031 * We setup the specified state block and start a timer
2032 * to wait for an ADDBA response frame.
2035 ieee80211_addba_request(struct ieee80211_node *ni,
2036 struct ieee80211_tx_ampdu *tap,
2037 int dialogtoken, int baparamset, int batimeout)
2042 tap->txa_token = dialogtoken;
2043 tap->txa_flags |= IEEE80211_AGGR_IMMEDIATE;
2044 bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
2045 tap->txa_wnd = (bufsiz == 0) ?
2046 IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
2047 addba_start_timeout(tap);
2052 * Called by drivers that wish to request an ADDBA session be
2053 * setup. This brings it up and starts the request timer.
2056 ieee80211_ampdu_tx_request_ext(struct ieee80211_node *ni, int tid)
2058 struct ieee80211_tx_ampdu *tap;
2060 if (tid < 0 || tid > 15)
2062 tap = &ni->ni_tx_ampdu[tid];
2065 if ((tap->txa_flags & IEEE80211_AGGR_SETUP) == 0) {
2066 /* do deferred setup of state */
2067 ampdu_tx_setup(tap);
2069 /* XXX hack for not doing proper locking */
2070 tap->txa_flags &= ~IEEE80211_AGGR_NAK;
2071 addba_start_timeout(tap);
2076 * Called by drivers that have marked a session as active.
2079 ieee80211_ampdu_tx_request_active_ext(struct ieee80211_node *ni, int tid,
2082 struct ieee80211_tx_ampdu *tap;
2084 if (tid < 0 || tid > 15)
2086 tap = &ni->ni_tx_ampdu[tid];
2089 addba_stop_timeout(tap);
2091 tap->txa_flags |= IEEE80211_AGGR_RUNNING;
2092 tap->txa_attempts = 0;
2094 /* mark tid so we don't try again */
2095 tap->txa_flags |= IEEE80211_AGGR_NAK;
2101 * Default method for processing an A-MPDU tx aggregation
2102 * response. We shutdown any pending timer and update the
2103 * state block according to the reply.
2106 ieee80211_addba_response(struct ieee80211_node *ni,
2107 struct ieee80211_tx_ampdu *tap,
2108 int status, int baparamset, int batimeout)
2113 addba_stop_timeout(tap);
2114 if (status == IEEE80211_STATUS_SUCCESS) {
2115 bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
2116 /* XXX override our request? */
2117 tap->txa_wnd = (bufsiz == 0) ?
2118 IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
2120 tid = MS(baparamset, IEEE80211_BAPS_TID);
2121 tap->txa_flags |= IEEE80211_AGGR_RUNNING;
2122 tap->txa_attempts = 0;
2124 /* mark tid so we don't try again */
2125 tap->txa_flags |= IEEE80211_AGGR_NAK;
2131 * Default method for stopping A-MPDU tx aggregation.
2132 * Any timer is cleared and we drain any pending frames.
2135 ieee80211_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
2138 addba_stop_timeout(tap);
2139 if (tap->txa_flags & IEEE80211_AGGR_RUNNING) {
2140 /* XXX clear aggregation queue */
2141 tap->txa_flags &= ~IEEE80211_AGGR_RUNNING;
2143 tap->txa_attempts = 0;
2147 * Process a received action frame using the default aggregation
2148 * policy. We intercept ADDBA-related frames and use them to
2149 * update our aggregation state. All other frames are passed up
2150 * for processing by ieee80211_recv_action.
2153 ht_recv_action_ba_addba_request(struct ieee80211_node *ni,
2154 const struct ieee80211_frame *wh,
2155 const uint8_t *frm, const uint8_t *efrm)
2157 struct ieee80211com *ic = ni->ni_ic;
2158 struct ieee80211vap *vap = ni->ni_vap;
2159 struct ieee80211_rx_ampdu *rap;
2160 uint8_t dialogtoken;
2161 uint16_t baparamset, batimeout, baseqctl;
2165 dialogtoken = frm[2];
2166 baparamset = le16dec(frm+3);
2167 batimeout = le16dec(frm+5);
2168 baseqctl = le16dec(frm+7);
2170 tid = MS(baparamset, IEEE80211_BAPS_TID);
2172 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2173 "recv ADDBA request: dialogtoken %u baparamset 0x%x "
2174 "(tid %d bufsiz %d) batimeout %d baseqctl %d:%d",
2175 dialogtoken, baparamset,
2176 tid, MS(baparamset, IEEE80211_BAPS_BUFSIZ),
2178 MS(baseqctl, IEEE80211_BASEQ_START),
2179 MS(baseqctl, IEEE80211_BASEQ_FRAG));
2181 rap = &ni->ni_rx_ampdu[tid];
2183 /* Send ADDBA response */
2184 args[0] = dialogtoken;
2186 * NB: We ack only if the sta associated with HT and
2187 * the ap is configured to do AMPDU rx (the latter
2188 * violates the 11n spec and is mostly for testing).
2190 if ((ni->ni_flags & IEEE80211_NODE_AMPDU_RX) &&
2191 (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_RX)) {
2192 /* XXX handle ampdu_rx_start failure */
2193 ic->ic_ampdu_rx_start(ni, rap,
2194 baparamset, batimeout, baseqctl);
2196 args[1] = IEEE80211_STATUS_SUCCESS;
2198 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2199 ni, "reject ADDBA request: %s",
2200 ni->ni_flags & IEEE80211_NODE_AMPDU_RX ?
2201 "administratively disabled" :
2202 "not negotiated for station");
2203 vap->iv_stats.is_addba_reject++;
2204 args[1] = IEEE80211_STATUS_UNSPECIFIED;
2206 /* XXX honor rap flags? */
2207 args[2] = IEEE80211_BAPS_POLICY_IMMEDIATE
2208 | SM(tid, IEEE80211_BAPS_TID)
2209 | SM(rap->rxa_wnd, IEEE80211_BAPS_BUFSIZ)
2213 ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
2214 IEEE80211_ACTION_BA_ADDBA_RESPONSE, args);
2219 ht_recv_action_ba_addba_response(struct ieee80211_node *ni,
2220 const struct ieee80211_frame *wh,
2221 const uint8_t *frm, const uint8_t *efrm)
2223 struct ieee80211com *ic = ni->ni_ic;
2224 struct ieee80211vap *vap = ni->ni_vap;
2225 struct ieee80211_tx_ampdu *tap;
2226 uint8_t dialogtoken, policy;
2227 uint16_t baparamset, batimeout, code;
2230 dialogtoken = frm[2];
2231 code = le16dec(frm+3);
2232 baparamset = le16dec(frm+5);
2233 tid = MS(baparamset, IEEE80211_BAPS_TID);
2234 bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
2235 policy = MS(baparamset, IEEE80211_BAPS_POLICY);
2236 batimeout = le16dec(frm+7);
2238 tap = &ni->ni_tx_ampdu[tid];
2239 if ((tap->txa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
2240 IEEE80211_DISCARD_MAC(vap,
2241 IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2242 ni->ni_macaddr, "ADDBA response",
2243 "no pending ADDBA, tid %d dialogtoken %u "
2244 "code %d", tid, dialogtoken, code);
2245 vap->iv_stats.is_addba_norequest++;
2248 if (dialogtoken != tap->txa_token) {
2249 IEEE80211_DISCARD_MAC(vap,
2250 IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2251 ni->ni_macaddr, "ADDBA response",
2252 "dialogtoken mismatch: waiting for %d, "
2253 "received %d, tid %d code %d",
2254 tap->txa_token, dialogtoken, tid, code);
2255 vap->iv_stats.is_addba_badtoken++;
2258 /* NB: assumes IEEE80211_AGGR_IMMEDIATE is 1 */
2259 if (policy != (tap->txa_flags & IEEE80211_AGGR_IMMEDIATE)) {
2260 IEEE80211_DISCARD_MAC(vap,
2261 IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2262 ni->ni_macaddr, "ADDBA response",
2263 "policy mismatch: expecting %s, "
2264 "received %s, tid %d code %d",
2265 tap->txa_flags & IEEE80211_AGGR_IMMEDIATE,
2267 vap->iv_stats.is_addba_badpolicy++;
2271 /* XXX we take MIN in ieee80211_addba_response */
2272 if (bufsiz > IEEE80211_AGGR_BAWMAX) {
2273 IEEE80211_DISCARD_MAC(vap,
2274 IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2275 ni->ni_macaddr, "ADDBA response",
2276 "BA window too large: max %d, "
2277 "received %d, tid %d code %d",
2278 bufsiz, IEEE80211_AGGR_BAWMAX, tid, code);
2279 vap->iv_stats.is_addba_badbawinsize++;
2283 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2284 "recv ADDBA response: dialogtoken %u code %d "
2285 "baparamset 0x%x (tid %d bufsiz %d) batimeout %d",
2286 dialogtoken, code, baparamset, tid, bufsiz,
2288 ic->ic_addba_response(ni, tap, code, baparamset, batimeout);
2293 ht_recv_action_ba_delba(struct ieee80211_node *ni,
2294 const struct ieee80211_frame *wh,
2295 const uint8_t *frm, const uint8_t *efrm)
2297 struct ieee80211com *ic = ni->ni_ic;
2298 struct ieee80211_rx_ampdu *rap;
2299 struct ieee80211_tx_ampdu *tap;
2300 uint16_t baparamset, code;
2303 baparamset = le16dec(frm+2);
2304 code = le16dec(frm+4);
2306 tid = MS(baparamset, IEEE80211_DELBAPS_TID);
2308 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2309 "recv DELBA: baparamset 0x%x (tid %d initiator %d) "
2310 "code %d", baparamset, tid,
2311 MS(baparamset, IEEE80211_DELBAPS_INIT), code);
2313 if ((baparamset & IEEE80211_DELBAPS_INIT) == 0) {
2314 tap = &ni->ni_tx_ampdu[tid];
2315 ic->ic_addba_stop(ni, tap);
2317 rap = &ni->ni_rx_ampdu[tid];
2318 ic->ic_ampdu_rx_stop(ni, rap);
2324 ht_recv_action_ht_txchwidth(struct ieee80211_node *ni,
2325 const struct ieee80211_frame *wh,
2326 const uint8_t *frm, const uint8_t *efrm)
2330 chw = (frm[2] == IEEE80211_A_HT_TXCHWIDTH_2040) ? 40 : 20;
2332 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2333 "%s: HT txchwidth, width %d%s",
2334 __func__, chw, ni->ni_chw != chw ? "*" : "");
2335 if (chw != ni->ni_chw) {
2336 /* XXX does this need to change the ht40 station count? */
2338 /* XXX notify on change */
2344 ht_recv_action_ht_mimopwrsave(struct ieee80211_node *ni,
2345 const struct ieee80211_frame *wh,
2346 const uint8_t *frm, const uint8_t *efrm)
2348 const struct ieee80211_action_ht_mimopowersave *mps =
2349 (const struct ieee80211_action_ht_mimopowersave *) frm;
2351 /* XXX check iv_htcaps */
2352 if (mps->am_control & IEEE80211_A_HT_MIMOPWRSAVE_ENA)
2353 ni->ni_flags |= IEEE80211_NODE_MIMO_PS;
2355 ni->ni_flags &= ~IEEE80211_NODE_MIMO_PS;
2356 if (mps->am_control & IEEE80211_A_HT_MIMOPWRSAVE_MODE)
2357 ni->ni_flags |= IEEE80211_NODE_MIMO_RTS;
2359 ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS;
2360 /* XXX notify on change */
2361 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2362 "%s: HT MIMO PS (%s%s)", __func__,
2363 (ni->ni_flags & IEEE80211_NODE_MIMO_PS) ? "on" : "off",
2364 (ni->ni_flags & IEEE80211_NODE_MIMO_RTS) ? "+rts" : ""
2370 * Transmit processing.
2374 * Check if A-MPDU should be requested/enabled for a stream.
2375 * We require a traffic rate above a per-AC threshold and we
2376 * also handle backoff from previous failed attempts.
2378 * Drivers may override this method to bring in information
2379 * such as link state conditions in making the decision.
2382 ieee80211_ampdu_enable(struct ieee80211_node *ni,
2383 struct ieee80211_tx_ampdu *tap)
2385 struct ieee80211vap *vap = ni->ni_vap;
2387 if (tap->txa_avgpps <
2388 vap->iv_ampdu_mintraffic[TID_TO_WME_AC(tap->txa_tid)])
2390 /* XXX check rssi? */
2391 if (tap->txa_attempts >= ieee80211_addba_maxtries &&
2392 ieee80211_time_after(ticks, tap->txa_nextrequest)) {
2394 * Don't retry too often; txa_nextrequest is set
2395 * to the minimum interval we'll retry after
2396 * ieee80211_addba_maxtries failed attempts are made.
2400 IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
2401 "enable AMPDU on tid %d (%s), avgpps %d pkts %d attempt %d",
2402 tap->txa_tid, ieee80211_wme_acnames[TID_TO_WME_AC(tap->txa_tid)],
2403 tap->txa_avgpps, tap->txa_pkts, tap->txa_attempts);
2408 * Request A-MPDU tx aggregation. Setup local state and
2409 * issue an ADDBA request. BA use will only happen after
2410 * the other end replies with ADDBA response.
2413 ieee80211_ampdu_request(struct ieee80211_node *ni,
2414 struct ieee80211_tx_ampdu *tap)
2416 struct ieee80211com *ic = ni->ni_ic;
2418 int tid, dialogtoken;
2419 static int tokens = 0; /* XXX */
2422 if ((tap->txa_flags & IEEE80211_AGGR_SETUP) == 0) {
2423 /* do deferred setup of state */
2424 ampdu_tx_setup(tap);
2426 /* XXX hack for not doing proper locking */
2427 tap->txa_flags &= ~IEEE80211_AGGR_NAK;
2429 dialogtoken = (tokens+1) % 63; /* XXX */
2433 * XXX TODO: This is racy with any other parallel TX going on. :(
2435 tap->txa_start = ni->ni_txseqs[tid];
2437 args[0] = dialogtoken;
2438 args[1] = 0; /* NB: status code not used */
2439 args[2] = IEEE80211_BAPS_POLICY_IMMEDIATE
2440 | SM(tid, IEEE80211_BAPS_TID)
2441 | SM(IEEE80211_AGGR_BAWMAX, IEEE80211_BAPS_BUFSIZ)
2443 args[3] = 0; /* batimeout */
2444 /* NB: do first so there's no race against reply */
2445 if (!ic->ic_addba_request(ni, tap, dialogtoken, args[2], args[3])) {
2446 /* unable to setup state, don't make request */
2447 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2448 ni, "%s: could not setup BA stream for TID %d AC %d",
2449 __func__, tap->txa_tid, TID_TO_WME_AC(tap->txa_tid));
2450 /* defer next try so we don't slam the driver with requests */
2451 tap->txa_attempts = ieee80211_addba_maxtries;
2452 /* NB: check in case driver wants to override */
2453 if (tap->txa_nextrequest <= ticks)
2454 tap->txa_nextrequest = ticks + ieee80211_addba_backoff;
2457 tokens = dialogtoken; /* allocate token */
2458 /* NB: after calling ic_addba_request so driver can set txa_start */
2459 args[4] = SM(tap->txa_start, IEEE80211_BASEQ_START)
2460 | SM(0, IEEE80211_BASEQ_FRAG)
2462 return ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
2463 IEEE80211_ACTION_BA_ADDBA_REQUEST, args);
2467 * Terminate an AMPDU tx stream. State is reclaimed
2468 * and the peer notified with a DelBA Action frame.
2471 ieee80211_ampdu_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
2474 struct ieee80211com *ic = ni->ni_ic;
2475 struct ieee80211vap *vap = ni->ni_vap;
2479 tap->txa_flags &= ~IEEE80211_AGGR_BARPEND;
2480 if (IEEE80211_AMPDU_RUNNING(tap)) {
2481 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2482 ni, "%s: stop BA stream for TID %d (reason: %d (%s))",
2483 __func__, tap->txa_tid, reason,
2484 ieee80211_reason_to_string(reason));
2485 vap->iv_stats.is_ampdu_stop++;
2487 ic->ic_addba_stop(ni, tap);
2488 args[0] = tap->txa_tid;
2489 args[1] = IEEE80211_DELBAPS_INIT;
2490 args[2] = reason; /* XXX reason code */
2491 ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
2492 IEEE80211_ACTION_BA_DELBA, args);
2494 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2495 ni, "%s: BA stream for TID %d not running "
2496 "(reason: %d (%s))", __func__, tap->txa_tid, reason,
2497 ieee80211_reason_to_string(reason));
2498 vap->iv_stats.is_ampdu_stop_failed++;
2503 static void bar_start_timer(struct ieee80211_tx_ampdu *tap);
2506 bar_timeout(void *arg)
2508 struct ieee80211_tx_ampdu *tap = arg;
2509 struct ieee80211_node *ni = tap->txa_ni;
2511 KASSERT((tap->txa_flags & IEEE80211_AGGR_XCHGPEND) == 0,
2512 ("bar/addba collision, flags 0x%x", tap->txa_flags));
2514 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2515 ni, "%s: tid %u flags 0x%x attempts %d", __func__,
2516 tap->txa_tid, tap->txa_flags, tap->txa_attempts);
2518 /* guard against race with bar_tx_complete */
2519 if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) == 0)
2522 if (tap->txa_attempts >= ieee80211_bar_maxtries) {
2523 struct ieee80211com *ic = ni->ni_ic;
2525 ni->ni_vap->iv_stats.is_ampdu_bar_tx_fail++;
2527 * If (at least) the last BAR TX timeout was due to
2528 * an ieee80211_send_bar() failures, then we need
2529 * to make sure we notify the driver that a BAR
2530 * TX did occur and fail. This gives the driver
2531 * a chance to undo any queue pause that may
2534 ic->ic_bar_response(ni, tap, 1);
2535 ieee80211_ampdu_stop(ni, tap, IEEE80211_REASON_TIMEOUT);
2537 ni->ni_vap->iv_stats.is_ampdu_bar_tx_retry++;
2538 if (ieee80211_send_bar(ni, tap, tap->txa_seqpending) != 0) {
2539 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2540 ni, "%s: failed to TX, starting timer\n",
2543 * If ieee80211_send_bar() fails here, the
2544 * timer may have stopped and/or the pending
2545 * flag may be clear. Because of this,
2546 * fake the BARPEND and reset the timer.
2547 * A retransmission attempt will then occur
2548 * during the next timeout.
2551 tap->txa_flags |= IEEE80211_AGGR_BARPEND;
2552 bar_start_timer(tap);
2558 bar_start_timer(struct ieee80211_tx_ampdu *tap)
2560 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2564 callout_reset(&tap->txa_timer, ieee80211_bar_timeout, bar_timeout, tap);
2568 bar_stop_timer(struct ieee80211_tx_ampdu *tap)
2570 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2574 callout_stop(&tap->txa_timer);
2578 bar_tx_complete(struct ieee80211_node *ni, void *arg, int status)
2580 struct ieee80211_tx_ampdu *tap = arg;
2582 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2583 ni, "%s: tid %u flags 0x%x pending %d status %d",
2584 __func__, tap->txa_tid, tap->txa_flags,
2585 callout_pending(&tap->txa_timer), status);
2587 ni->ni_vap->iv_stats.is_ampdu_bar_tx++;
2589 if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) &&
2590 callout_pending(&tap->txa_timer)) {
2591 struct ieee80211com *ic = ni->ni_ic;
2593 if (status == 0) /* ACK'd */
2594 bar_stop_timer(tap);
2595 ic->ic_bar_response(ni, tap, status);
2596 /* NB: just let timer expire so we pace requests */
2601 ieee80211_bar_response(struct ieee80211_node *ni,
2602 struct ieee80211_tx_ampdu *tap, int status)
2605 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2609 if (status == 0) { /* got ACK */
2610 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2611 ni, "BAR moves BA win <%u:%u> (%u frames) txseq %u tid %u",
2613 IEEE80211_SEQ_ADD(tap->txa_start, tap->txa_wnd-1),
2614 tap->txa_qframes, tap->txa_seqpending,
2617 /* NB: timer already stopped in bar_tx_complete */
2618 tap->txa_start = tap->txa_seqpending;
2619 tap->txa_flags &= ~IEEE80211_AGGR_BARPEND;
2624 * Transmit a BAR frame to the specified node. The
2625 * BAR contents are drawn from the supplied aggregation
2626 * state associated with the node.
2628 * NB: we only handle immediate ACK w/ compressed bitmap.
2631 ieee80211_send_bar(struct ieee80211_node *ni,
2632 struct ieee80211_tx_ampdu *tap, ieee80211_seq seq)
2634 #define senderr(_x, _v) do { vap->iv_stats._v++; ret = _x; goto bad; } while (0)
2635 struct ieee80211vap *vap = ni->ni_vap;
2636 struct ieee80211com *ic = ni->ni_ic;
2637 struct ieee80211_frame_bar *bar;
2639 uint16_t barctl, barseqctl;
2644 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2649 if ((tap->txa_flags & IEEE80211_AGGR_RUNNING) == 0) {
2650 /* no ADDBA response, should not happen */
2655 bar_stop_timer(tap);
2657 ieee80211_ref_node(ni);
2659 m = ieee80211_getmgtframe(&frm, ic->ic_headroom, sizeof(*bar));
2661 senderr(ENOMEM, is_tx_nobuf);
2663 if (!ieee80211_add_callback(m, bar_tx_complete, tap)) {
2665 senderr(ENOMEM, is_tx_nobuf); /* XXX */
2669 bar = mtod(m, struct ieee80211_frame_bar *);
2670 bar->i_fc[0] = IEEE80211_FC0_VERSION_0 |
2671 IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_BAR;
2673 IEEE80211_ADDR_COPY(bar->i_ra, ni->ni_macaddr);
2674 IEEE80211_ADDR_COPY(bar->i_ta, vap->iv_myaddr);
2677 barctl = (tap->txa_flags & IEEE80211_AGGR_IMMEDIATE ?
2678 0 : IEEE80211_BAR_NOACK)
2679 | IEEE80211_BAR_COMP
2680 | SM(tid, IEEE80211_BAR_TID)
2682 barseqctl = SM(seq, IEEE80211_BAR_SEQ_START);
2683 /* NB: known to have proper alignment */
2684 bar->i_ctl = htole16(barctl);
2685 bar->i_seq = htole16(barseqctl);
2686 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_bar);
2688 M_WME_SETAC(m, WME_AC_VO);
2690 IEEE80211_NODE_STAT(ni, tx_mgmt); /* XXX tx_ctl? */
2693 /* init/bump attempts counter */
2694 if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) == 0)
2695 tap->txa_attempts = 1;
2697 tap->txa_attempts++;
2698 tap->txa_seqpending = seq;
2699 tap->txa_flags |= IEEE80211_AGGR_BARPEND;
2701 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_11N,
2702 ni, "send BAR: tid %u ctl 0x%x start %u (attempt %d)",
2703 tid, barctl, seq, tap->txa_attempts);
2706 * ic_raw_xmit will free the node reference
2707 * regardless of queue/TX success or failure.
2709 IEEE80211_TX_LOCK(ic);
2710 ret = ieee80211_raw_output(vap, ni, m, NULL);
2711 IEEE80211_TX_UNLOCK(ic);
2713 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_11N,
2714 ni, "send BAR: failed: (ret = %d)\n",
2716 /* xmit failed, clear state flag */
2717 tap->txa_flags &= ~IEEE80211_AGGR_BARPEND;
2718 vap->iv_stats.is_ampdu_bar_tx_fail++;
2721 /* XXX hack against tx complete happening before timer is started */
2722 if (tap->txa_flags & IEEE80211_AGGR_BARPEND)
2723 bar_start_timer(tap);
2726 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2730 vap->iv_stats.is_ampdu_bar_tx_fail++;
2731 ieee80211_free_node(ni);
2737 ht_action_output(struct ieee80211_node *ni, struct mbuf *m)
2739 struct ieee80211_bpf_params params;
2741 memset(¶ms, 0, sizeof(params));
2742 params.ibp_pri = WME_AC_VO;
2743 params.ibp_rate0 = ni->ni_txparms->mgmtrate;
2744 /* NB: we know all frames are unicast */
2745 params.ibp_try0 = ni->ni_txparms->maxretry;
2746 params.ibp_power = ni->ni_txpower;
2747 return ieee80211_mgmt_output(ni, m, IEEE80211_FC0_SUBTYPE_ACTION,
2751 #define ADDSHORT(frm, v) do { \
2752 frm[0] = (v) & 0xff; \
2753 frm[1] = (v) >> 8; \
2758 * Send an action management frame. The arguments are stuff
2759 * into a frame without inspection; the caller is assumed to
2760 * prepare them carefully (e.g. based on the aggregation state).
2763 ht_send_action_ba_addba(struct ieee80211_node *ni,
2764 int category, int action, void *arg0)
2766 struct ieee80211vap *vap = ni->ni_vap;
2767 struct ieee80211com *ic = ni->ni_ic;
2768 uint16_t *args = arg0;
2772 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2773 "send ADDBA %s: dialogtoken %d status %d "
2774 "baparamset 0x%x (tid %d) batimeout 0x%x baseqctl 0x%x",
2775 (action == IEEE80211_ACTION_BA_ADDBA_REQUEST) ?
2776 "request" : "response",
2777 args[0], args[1], args[2], MS(args[2], IEEE80211_BAPS_TID),
2780 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2781 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2782 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2783 ieee80211_ref_node(ni);
2785 m = ieee80211_getmgtframe(&frm,
2786 ic->ic_headroom + sizeof(struct ieee80211_frame),
2787 sizeof(uint16_t) /* action+category */
2788 /* XXX may action payload */
2789 + sizeof(struct ieee80211_action_ba_addbaresponse)
2794 *frm++ = args[0]; /* dialog token */
2795 if (action == IEEE80211_ACTION_BA_ADDBA_RESPONSE)
2796 ADDSHORT(frm, args[1]); /* status code */
2797 ADDSHORT(frm, args[2]); /* baparamset */
2798 ADDSHORT(frm, args[3]); /* batimeout */
2799 if (action == IEEE80211_ACTION_BA_ADDBA_REQUEST)
2800 ADDSHORT(frm, args[4]); /* baseqctl */
2801 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2802 return ht_action_output(ni, m);
2804 vap->iv_stats.is_tx_nobuf++;
2805 ieee80211_free_node(ni);
2811 ht_send_action_ba_delba(struct ieee80211_node *ni,
2812 int category, int action, void *arg0)
2814 struct ieee80211vap *vap = ni->ni_vap;
2815 struct ieee80211com *ic = ni->ni_ic;
2816 uint16_t *args = arg0;
2818 uint16_t baparamset;
2821 baparamset = SM(args[0], IEEE80211_DELBAPS_TID)
2824 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2825 "send DELBA action: tid %d, initiator %d reason %d (%s)",
2826 args[0], args[1], args[2], ieee80211_reason_to_string(args[2]));
2828 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2829 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2830 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2831 ieee80211_ref_node(ni);
2833 m = ieee80211_getmgtframe(&frm,
2834 ic->ic_headroom + sizeof(struct ieee80211_frame),
2835 sizeof(uint16_t) /* action+category */
2836 /* XXX may action payload */
2837 + sizeof(struct ieee80211_action_ba_addbaresponse)
2842 ADDSHORT(frm, baparamset);
2843 ADDSHORT(frm, args[2]); /* reason code */
2844 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2845 return ht_action_output(ni, m);
2847 vap->iv_stats.is_tx_nobuf++;
2848 ieee80211_free_node(ni);
2854 ht_send_action_ht_txchwidth(struct ieee80211_node *ni,
2855 int category, int action, void *arg0)
2857 struct ieee80211vap *vap = ni->ni_vap;
2858 struct ieee80211com *ic = ni->ni_ic;
2862 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2863 "send HT txchwidth: width %d",
2864 IEEE80211_IS_CHAN_HT40(ni->ni_chan) ? 40 : 20);
2866 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2867 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2868 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2869 ieee80211_ref_node(ni);
2871 m = ieee80211_getmgtframe(&frm,
2872 ic->ic_headroom + sizeof(struct ieee80211_frame),
2873 sizeof(uint16_t) /* action+category */
2874 /* XXX may action payload */
2875 + sizeof(struct ieee80211_action_ba_addbaresponse)
2880 *frm++ = IEEE80211_IS_CHAN_HT40(ni->ni_chan) ?
2881 IEEE80211_A_HT_TXCHWIDTH_2040 :
2882 IEEE80211_A_HT_TXCHWIDTH_20;
2883 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2884 return ht_action_output(ni, m);
2886 vap->iv_stats.is_tx_nobuf++;
2887 ieee80211_free_node(ni);
2894 * Construct the MCS bit mask for inclusion in an HT capabilities
2895 * information element.
2898 ieee80211_set_mcsset(struct ieee80211com *ic, uint8_t *frm)
2903 KASSERT((ic->ic_rxstream > 0 && ic->ic_rxstream <= 4),
2904 ("ic_rxstream %d out of range", ic->ic_rxstream));
2905 KASSERT((ic->ic_txstream > 0 && ic->ic_txstream <= 4),
2906 ("ic_txstream %d out of range", ic->ic_txstream));
2908 for (i = 0; i < ic->ic_rxstream * 8; i++)
2910 if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
2911 (ic->ic_htcaps & IEEE80211_HTC_RXMCS32))
2913 if (ic->ic_htcaps & IEEE80211_HTC_RXUNEQUAL) {
2914 if (ic->ic_rxstream >= 2) {
2915 for (i = 33; i <= 38; i++)
2918 if (ic->ic_rxstream >= 3) {
2919 for (i = 39; i <= 52; i++)
2922 if (ic->ic_txstream >= 4) {
2923 for (i = 53; i <= 76; i++)
2928 if (ic->ic_rxstream != ic->ic_txstream) {
2929 txparams = 0x1; /* TX MCS set defined */
2930 txparams |= 0x2; /* TX RX MCS not equal */
2931 txparams |= (ic->ic_txstream - 1) << 2; /* num TX streams */
2932 if (ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL)
2933 txparams |= 0x16; /* TX unequal modulation sup */
2940 * Add body of an HTCAP information element.
2943 ieee80211_add_htcap_body(uint8_t *frm, struct ieee80211_node *ni)
2945 #define ADDSHORT(frm, v) do { \
2946 frm[0] = (v) & 0xff; \
2947 frm[1] = (v) >> 8; \
2950 struct ieee80211com *ic = ni->ni_ic;
2951 struct ieee80211vap *vap = ni->ni_vap;
2952 uint16_t caps, extcaps;
2955 /* HT capabilities */
2956 caps = vap->iv_htcaps & 0xffff;
2958 * Note channel width depends on whether we are operating as
2959 * a sta or not. When operating as a sta we are generating
2960 * a request based on our desired configuration. Otherwise
2961 * we are operational and the channel attributes identify
2962 * how we've been setup (which might be different if a fixed
2963 * channel is specified).
2965 if (vap->iv_opmode == IEEE80211_M_STA) {
2966 /* override 20/40 use based on config */
2967 if (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)
2968 caps |= IEEE80211_HTCAP_CHWIDTH40;
2970 caps &= ~IEEE80211_HTCAP_CHWIDTH40;
2972 /* Start by using the advertised settings */
2973 rxmax = MS(ni->ni_htparam, IEEE80211_HTCAP_MAXRXAMPDU);
2974 density = MS(ni->ni_htparam, IEEE80211_HTCAP_MPDUDENSITY);
2976 IEEE80211_DPRINTF(vap, IEEE80211_MSG_11N,
2977 "%s: advertised rxmax=%d, density=%d, vap rxmax=%d, density=%d\n",
2981 vap->iv_ampdu_rxmax,
2982 vap->iv_ampdu_density);
2984 /* Cap at VAP rxmax */
2985 if (rxmax > vap->iv_ampdu_rxmax)
2986 rxmax = vap->iv_ampdu_rxmax;
2989 * If the VAP ampdu density value greater, use that.
2991 * (Larger density value == larger minimum gap between A-MPDU
2994 if (vap->iv_ampdu_density > density)
2995 density = vap->iv_ampdu_density;
2998 * NB: Hardware might support HT40 on some but not all
2999 * channels. We can't determine this earlier because only
3000 * after association the channel is upgraded to HT based
3001 * on the negotiated capabilities.
3003 if (ni->ni_chan != IEEE80211_CHAN_ANYC &&
3004 findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT40U) == NULL &&
3005 findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT40D) == NULL)
3006 caps &= ~IEEE80211_HTCAP_CHWIDTH40;
3008 /* override 20/40 use based on current channel */
3009 if (IEEE80211_IS_CHAN_HT40(ni->ni_chan))
3010 caps |= IEEE80211_HTCAP_CHWIDTH40;
3012 caps &= ~IEEE80211_HTCAP_CHWIDTH40;
3014 /* XXX TODO should it start by using advertised settings? */
3015 rxmax = vap->iv_ampdu_rxmax;
3016 density = vap->iv_ampdu_density;
3019 /* adjust short GI based on channel and config */
3020 if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20) == 0)
3021 caps &= ~IEEE80211_HTCAP_SHORTGI20;
3022 if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40) == 0 ||
3023 (caps & IEEE80211_HTCAP_CHWIDTH40) == 0)
3024 caps &= ~IEEE80211_HTCAP_SHORTGI40;
3026 /* adjust STBC based on receive capabilities */
3027 if ((vap->iv_flags_ht & IEEE80211_FHT_STBC_RX) == 0)
3028 caps &= ~IEEE80211_HTCAP_RXSTBC;
3030 /* XXX TODO: adjust LDPC based on receive capabilities */
3032 ADDSHORT(frm, caps);
3035 *frm = SM(rxmax, IEEE80211_HTCAP_MAXRXAMPDU)
3036 | SM(density, IEEE80211_HTCAP_MPDUDENSITY)
3040 /* pre-zero remainder of ie */
3041 memset(frm, 0, sizeof(struct ieee80211_ie_htcap) -
3042 __offsetof(struct ieee80211_ie_htcap, hc_mcsset));
3044 /* supported MCS set */
3046 * XXX: For sta mode the rate set should be restricted based
3047 * on the AP's capabilities, but ni_htrates isn't setup when
3048 * we're called to form an AssocReq frame so for now we're
3049 * restricted to the device capabilities.
3051 ieee80211_set_mcsset(ni->ni_ic, frm);
3053 frm += __offsetof(struct ieee80211_ie_htcap, hc_extcap) -
3054 __offsetof(struct ieee80211_ie_htcap, hc_mcsset);
3056 /* HT extended capabilities */
3057 extcaps = vap->iv_htextcaps & 0xffff;
3059 ADDSHORT(frm, extcaps);
3061 frm += sizeof(struct ieee80211_ie_htcap) -
3062 __offsetof(struct ieee80211_ie_htcap, hc_txbf);
3069 * Add 802.11n HT capabilities information element
3072 ieee80211_add_htcap(uint8_t *frm, struct ieee80211_node *ni)
3074 frm[0] = IEEE80211_ELEMID_HTCAP;
3075 frm[1] = sizeof(struct ieee80211_ie_htcap) - 2;
3076 return ieee80211_add_htcap_body(frm + 2, ni);
3080 * Non-associated probe request - add HT capabilities based on
3081 * the current channel configuration.
3084 ieee80211_add_htcap_body_ch(uint8_t *frm, struct ieee80211vap *vap,
3085 struct ieee80211_channel *c)
3087 #define ADDSHORT(frm, v) do { \
3088 frm[0] = (v) & 0xff; \
3089 frm[1] = (v) >> 8; \
3092 struct ieee80211com *ic = vap->iv_ic;
3093 uint16_t caps, extcaps;
3096 /* HT capabilities */
3097 caps = vap->iv_htcaps & 0xffff;
3100 * We don't use this in STA mode; only in IBSS mode.
3101 * So in IBSS mode we base our HTCAP flags on the
3105 /* override 20/40 use based on current channel */
3106 if (IEEE80211_IS_CHAN_HT40(c))
3107 caps |= IEEE80211_HTCAP_CHWIDTH40;
3109 caps &= ~IEEE80211_HTCAP_CHWIDTH40;
3111 /* Use the currently configured values */
3112 rxmax = vap->iv_ampdu_rxmax;
3113 density = vap->iv_ampdu_density;
3115 /* adjust short GI based on channel and config */
3116 if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20) == 0)
3117 caps &= ~IEEE80211_HTCAP_SHORTGI20;
3118 if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40) == 0 ||
3119 (caps & IEEE80211_HTCAP_CHWIDTH40) == 0)
3120 caps &= ~IEEE80211_HTCAP_SHORTGI40;
3121 ADDSHORT(frm, caps);
3124 *frm = SM(rxmax, IEEE80211_HTCAP_MAXRXAMPDU)
3125 | SM(density, IEEE80211_HTCAP_MPDUDENSITY)
3129 /* pre-zero remainder of ie */
3130 memset(frm, 0, sizeof(struct ieee80211_ie_htcap) -
3131 __offsetof(struct ieee80211_ie_htcap, hc_mcsset));
3133 /* supported MCS set */
3135 * XXX: For sta mode the rate set should be restricted based
3136 * on the AP's capabilities, but ni_htrates isn't setup when
3137 * we're called to form an AssocReq frame so for now we're
3138 * restricted to the device capabilities.
3140 ieee80211_set_mcsset(ic, frm);
3142 frm += __offsetof(struct ieee80211_ie_htcap, hc_extcap) -
3143 __offsetof(struct ieee80211_ie_htcap, hc_mcsset);
3145 /* HT extended capabilities */
3146 extcaps = vap->iv_htextcaps & 0xffff;
3148 ADDSHORT(frm, extcaps);
3150 frm += sizeof(struct ieee80211_ie_htcap) -
3151 __offsetof(struct ieee80211_ie_htcap, hc_txbf);
3158 * Add 802.11n HT capabilities information element
3161 ieee80211_add_htcap_ch(uint8_t *frm, struct ieee80211vap *vap,
3162 struct ieee80211_channel *c)
3164 frm[0] = IEEE80211_ELEMID_HTCAP;
3165 frm[1] = sizeof(struct ieee80211_ie_htcap) - 2;
3166 return ieee80211_add_htcap_body_ch(frm + 2, vap, c);
3170 * Add Broadcom OUI wrapped standard HTCAP ie; this is
3171 * used for compatibility w/ pre-draft implementations.
3174 ieee80211_add_htcap_vendor(uint8_t *frm, struct ieee80211_node *ni)
3176 frm[0] = IEEE80211_ELEMID_VENDOR;
3177 frm[1] = 4 + sizeof(struct ieee80211_ie_htcap) - 2;
3178 frm[2] = (BCM_OUI >> 0) & 0xff;
3179 frm[3] = (BCM_OUI >> 8) & 0xff;
3180 frm[4] = (BCM_OUI >> 16) & 0xff;
3181 frm[5] = BCM_OUI_HTCAP;
3182 return ieee80211_add_htcap_body(frm + 6, ni);
3186 * Construct the MCS bit mask of basic rates
3187 * for inclusion in an HT information element.
3190 ieee80211_set_basic_htrates(uint8_t *frm, const struct ieee80211_htrateset *rs)
3194 for (i = 0; i < rs->rs_nrates; i++) {
3195 int r = rs->rs_rates[i] & IEEE80211_RATE_VAL;
3196 if ((rs->rs_rates[i] & IEEE80211_RATE_BASIC) &&
3197 r < IEEE80211_HTRATE_MAXSIZE) {
3198 /* NB: this assumes a particular implementation */
3205 * Update the HTINFO ie for a beacon frame.
3208 ieee80211_ht_update_beacon(struct ieee80211vap *vap,
3209 struct ieee80211_beacon_offsets *bo)
3211 #define PROTMODE (IEEE80211_HTINFO_OPMODE|IEEE80211_HTINFO_NONHT_PRESENT)
3212 struct ieee80211_node *ni;
3213 const struct ieee80211_channel *bsschan;
3214 struct ieee80211com *ic = vap->iv_ic;
3215 struct ieee80211_ie_htinfo *ht =
3216 (struct ieee80211_ie_htinfo *) bo->bo_htinfo;
3218 ni = ieee80211_ref_node(vap->iv_bss);
3219 bsschan = ni->ni_chan;
3221 /* XXX only update on channel change */
3222 ht->hi_ctrlchannel = ieee80211_chan2ieee(ic, bsschan);
3223 if (vap->iv_flags_ht & IEEE80211_FHT_RIFS)
3224 ht->hi_byte1 = IEEE80211_HTINFO_RIFSMODE_PERM;
3226 ht->hi_byte1 = IEEE80211_HTINFO_RIFSMODE_PROH;
3227 if (IEEE80211_IS_CHAN_HT40U(bsschan))
3228 ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_ABOVE;
3229 else if (IEEE80211_IS_CHAN_HT40D(bsschan))
3230 ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_BELOW;
3232 ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_NONE;
3233 if (IEEE80211_IS_CHAN_HT40(bsschan))
3234 ht->hi_byte1 |= IEEE80211_HTINFO_TXWIDTH_2040;
3236 /* protection mode */
3237 ht->hi_byte2 = (ht->hi_byte2 &~ PROTMODE) | ic->ic_curhtprotmode;
3239 ieee80211_free_node(ni);
3241 /* XXX propagate to vendor ie's */
3246 * Add body of an HTINFO information element.
3248 * NB: We don't use struct ieee80211_ie_htinfo because we can
3249 * be called to fillin both a standard ie and a compat ie that
3250 * has a vendor OUI at the front.
3253 ieee80211_add_htinfo_body(uint8_t *frm, struct ieee80211_node *ni)
3255 struct ieee80211vap *vap = ni->ni_vap;
3256 struct ieee80211com *ic = ni->ni_ic;
3258 /* pre-zero remainder of ie */
3259 memset(frm, 0, sizeof(struct ieee80211_ie_htinfo) - 2);
3261 /* primary/control channel center */
3262 *frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
3264 if (vap->iv_flags_ht & IEEE80211_FHT_RIFS)
3265 frm[0] = IEEE80211_HTINFO_RIFSMODE_PERM;
3267 frm[0] = IEEE80211_HTINFO_RIFSMODE_PROH;
3268 if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan))
3269 frm[0] |= IEEE80211_HTINFO_2NDCHAN_ABOVE;
3270 else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan))
3271 frm[0] |= IEEE80211_HTINFO_2NDCHAN_BELOW;
3273 frm[0] |= IEEE80211_HTINFO_2NDCHAN_NONE;
3274 if (IEEE80211_IS_CHAN_HT40(ni->ni_chan))
3275 frm[0] |= IEEE80211_HTINFO_TXWIDTH_2040;
3277 frm[1] = ic->ic_curhtprotmode;
3282 ieee80211_set_basic_htrates(frm, &ni->ni_htrates);
3283 frm += sizeof(struct ieee80211_ie_htinfo) -
3284 __offsetof(struct ieee80211_ie_htinfo, hi_basicmcsset);
3289 * Add 802.11n HT information information element.
3292 ieee80211_add_htinfo(uint8_t *frm, struct ieee80211_node *ni)
3294 frm[0] = IEEE80211_ELEMID_HTINFO;
3295 frm[1] = sizeof(struct ieee80211_ie_htinfo) - 2;
3296 return ieee80211_add_htinfo_body(frm + 2, ni);
3300 * Add Broadcom OUI wrapped standard HTINFO ie; this is
3301 * used for compatibility w/ pre-draft implementations.
3304 ieee80211_add_htinfo_vendor(uint8_t *frm, struct ieee80211_node *ni)
3306 frm[0] = IEEE80211_ELEMID_VENDOR;
3307 frm[1] = 4 + sizeof(struct ieee80211_ie_htinfo) - 2;
3308 frm[2] = (BCM_OUI >> 0) & 0xff;
3309 frm[3] = (BCM_OUI >> 8) & 0xff;
3310 frm[4] = (BCM_OUI >> 16) & 0xff;
3311 frm[5] = BCM_OUI_HTINFO;
3312 return ieee80211_add_htinfo_body(frm + 6, ni);