2 * Copyright (c) 2007-2008 Sam Leffler, Errno Consulting
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
32 * IEEE 802.11n protocol support.
38 #include <sys/param.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/systm.h>
42 #include <sys/endian.h>
44 #include <sys/socket.h>
47 #include <net/if_var.h>
48 #include <net/if_media.h>
49 #include <net/ethernet.h>
51 #include <net80211/ieee80211_var.h>
52 #include <net80211/ieee80211_action.h>
53 #include <net80211/ieee80211_input.h>
55 /* define here, used throughout file */
56 #define MS(_v, _f) (((_v) & _f) >> _f##_S)
57 #define SM(_v, _f) (((_v) << _f##_S) & _f)
59 const struct ieee80211_mcs_rates ieee80211_htrates[IEEE80211_HTRATE_MAXSIZE] = {
60 { 13, 14, 27, 30 }, /* MCS 0 */
61 { 26, 29, 54, 60 }, /* MCS 1 */
62 { 39, 43, 81, 90 }, /* MCS 2 */
63 { 52, 58, 108, 120 }, /* MCS 3 */
64 { 78, 87, 162, 180 }, /* MCS 4 */
65 { 104, 116, 216, 240 }, /* MCS 5 */
66 { 117, 130, 243, 270 }, /* MCS 6 */
67 { 130, 144, 270, 300 }, /* MCS 7 */
68 { 26, 29, 54, 60 }, /* MCS 8 */
69 { 52, 58, 108, 120 }, /* MCS 9 */
70 { 78, 87, 162, 180 }, /* MCS 10 */
71 { 104, 116, 216, 240 }, /* MCS 11 */
72 { 156, 173, 324, 360 }, /* MCS 12 */
73 { 208, 231, 432, 480 }, /* MCS 13 */
74 { 234, 260, 486, 540 }, /* MCS 14 */
75 { 260, 289, 540, 600 }, /* MCS 15 */
76 { 39, 43, 81, 90 }, /* MCS 16 */
77 { 78, 87, 162, 180 }, /* MCS 17 */
78 { 117, 130, 243, 270 }, /* MCS 18 */
79 { 156, 173, 324, 360 }, /* MCS 19 */
80 { 234, 260, 486, 540 }, /* MCS 20 */
81 { 312, 347, 648, 720 }, /* MCS 21 */
82 { 351, 390, 729, 810 }, /* MCS 22 */
83 { 390, 433, 810, 900 }, /* MCS 23 */
84 { 52, 58, 108, 120 }, /* MCS 24 */
85 { 104, 116, 216, 240 }, /* MCS 25 */
86 { 156, 173, 324, 360 }, /* MCS 26 */
87 { 208, 231, 432, 480 }, /* MCS 27 */
88 { 312, 347, 648, 720 }, /* MCS 28 */
89 { 416, 462, 864, 960 }, /* MCS 29 */
90 { 468, 520, 972, 1080 }, /* MCS 30 */
91 { 520, 578, 1080, 1200 }, /* MCS 31 */
92 { 0, 0, 12, 13 }, /* MCS 32 */
93 { 78, 87, 162, 180 }, /* MCS 33 */
94 { 104, 116, 216, 240 }, /* MCS 34 */
95 { 130, 144, 270, 300 }, /* MCS 35 */
96 { 117, 130, 243, 270 }, /* MCS 36 */
97 { 156, 173, 324, 360 }, /* MCS 37 */
98 { 195, 217, 405, 450 }, /* MCS 38 */
99 { 104, 116, 216, 240 }, /* MCS 39 */
100 { 130, 144, 270, 300 }, /* MCS 40 */
101 { 130, 144, 270, 300 }, /* MCS 41 */
102 { 156, 173, 324, 360 }, /* MCS 42 */
103 { 182, 202, 378, 420 }, /* MCS 43 */
104 { 182, 202, 378, 420 }, /* MCS 44 */
105 { 208, 231, 432, 480 }, /* MCS 45 */
106 { 156, 173, 324, 360 }, /* MCS 46 */
107 { 195, 217, 405, 450 }, /* MCS 47 */
108 { 195, 217, 405, 450 }, /* MCS 48 */
109 { 234, 260, 486, 540 }, /* MCS 49 */
110 { 273, 303, 567, 630 }, /* MCS 50 */
111 { 273, 303, 567, 630 }, /* MCS 51 */
112 { 312, 347, 648, 720 }, /* MCS 52 */
113 { 130, 144, 270, 300 }, /* MCS 53 */
114 { 156, 173, 324, 360 }, /* MCS 54 */
115 { 182, 202, 378, 420 }, /* MCS 55 */
116 { 156, 173, 324, 360 }, /* MCS 56 */
117 { 182, 202, 378, 420 }, /* MCS 57 */
118 { 208, 231, 432, 480 }, /* MCS 58 */
119 { 234, 260, 486, 540 }, /* MCS 59 */
120 { 208, 231, 432, 480 }, /* MCS 60 */
121 { 234, 260, 486, 540 }, /* MCS 61 */
122 { 260, 289, 540, 600 }, /* MCS 62 */
123 { 260, 289, 540, 600 }, /* MCS 63 */
124 { 286, 318, 594, 660 }, /* MCS 64 */
125 { 195, 217, 405, 450 }, /* MCS 65 */
126 { 234, 260, 486, 540 }, /* MCS 66 */
127 { 273, 303, 567, 630 }, /* MCS 67 */
128 { 234, 260, 486, 540 }, /* MCS 68 */
129 { 273, 303, 567, 630 }, /* MCS 69 */
130 { 312, 347, 648, 720 }, /* MCS 70 */
131 { 351, 390, 729, 810 }, /* MCS 71 */
132 { 312, 347, 648, 720 }, /* MCS 72 */
133 { 351, 390, 729, 810 }, /* MCS 73 */
134 { 390, 433, 810, 900 }, /* MCS 74 */
135 { 390, 433, 810, 900 }, /* MCS 75 */
136 { 429, 477, 891, 990 }, /* MCS 76 */
139 static int ieee80211_ampdu_age = -1; /* threshold for ampdu reorder q (ms) */
140 SYSCTL_PROC(_net_wlan, OID_AUTO, ampdu_age, CTLTYPE_INT | CTLFLAG_RW,
141 &ieee80211_ampdu_age, 0, ieee80211_sysctl_msecs_ticks, "I",
142 "AMPDU max reorder age (ms)");
144 static int ieee80211_recv_bar_ena = 1;
145 SYSCTL_INT(_net_wlan, OID_AUTO, recv_bar, CTLFLAG_RW, &ieee80211_recv_bar_ena,
146 0, "BAR frame processing (ena/dis)");
148 static int ieee80211_addba_timeout = -1;/* timeout for ADDBA response */
149 SYSCTL_PROC(_net_wlan, OID_AUTO, addba_timeout, CTLTYPE_INT | CTLFLAG_RW,
150 &ieee80211_addba_timeout, 0, ieee80211_sysctl_msecs_ticks, "I",
151 "ADDBA request timeout (ms)");
152 static int ieee80211_addba_backoff = -1;/* backoff after max ADDBA requests */
153 SYSCTL_PROC(_net_wlan, OID_AUTO, addba_backoff, CTLTYPE_INT | CTLFLAG_RW,
154 &ieee80211_addba_backoff, 0, ieee80211_sysctl_msecs_ticks, "I",
155 "ADDBA request backoff (ms)");
156 static int ieee80211_addba_maxtries = 3;/* max ADDBA requests before backoff */
157 SYSCTL_INT(_net_wlan, OID_AUTO, addba_maxtries, CTLFLAG_RW,
158 &ieee80211_addba_maxtries, 0, "max ADDBA requests sent before backoff");
160 static int ieee80211_bar_timeout = -1; /* timeout waiting for BAR response */
161 static int ieee80211_bar_maxtries = 50;/* max BAR requests before DELBA */
163 static ieee80211_recv_action_func ht_recv_action_ba_addba_request;
164 static ieee80211_recv_action_func ht_recv_action_ba_addba_response;
165 static ieee80211_recv_action_func ht_recv_action_ba_delba;
166 static ieee80211_recv_action_func ht_recv_action_ht_mimopwrsave;
167 static ieee80211_recv_action_func ht_recv_action_ht_txchwidth;
169 static ieee80211_send_action_func ht_send_action_ba_addba;
170 static ieee80211_send_action_func ht_send_action_ba_delba;
171 static ieee80211_send_action_func ht_send_action_ht_txchwidth;
174 ieee80211_ht_init(void)
177 * Setup HT parameters that depends on the clock frequency.
179 ieee80211_ampdu_age = msecs_to_ticks(500);
180 ieee80211_addba_timeout = msecs_to_ticks(250);
181 ieee80211_addba_backoff = msecs_to_ticks(10*1000);
182 ieee80211_bar_timeout = msecs_to_ticks(250);
184 * Register action frame handlers.
186 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_BA,
187 IEEE80211_ACTION_BA_ADDBA_REQUEST, ht_recv_action_ba_addba_request);
188 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_BA,
189 IEEE80211_ACTION_BA_ADDBA_RESPONSE, ht_recv_action_ba_addba_response);
190 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_BA,
191 IEEE80211_ACTION_BA_DELBA, ht_recv_action_ba_delba);
192 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_HT,
193 IEEE80211_ACTION_HT_MIMOPWRSAVE, ht_recv_action_ht_mimopwrsave);
194 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_HT,
195 IEEE80211_ACTION_HT_TXCHWIDTH, ht_recv_action_ht_txchwidth);
197 ieee80211_send_action_register(IEEE80211_ACTION_CAT_BA,
198 IEEE80211_ACTION_BA_ADDBA_REQUEST, ht_send_action_ba_addba);
199 ieee80211_send_action_register(IEEE80211_ACTION_CAT_BA,
200 IEEE80211_ACTION_BA_ADDBA_RESPONSE, ht_send_action_ba_addba);
201 ieee80211_send_action_register(IEEE80211_ACTION_CAT_BA,
202 IEEE80211_ACTION_BA_DELBA, ht_send_action_ba_delba);
203 ieee80211_send_action_register(IEEE80211_ACTION_CAT_HT,
204 IEEE80211_ACTION_HT_TXCHWIDTH, ht_send_action_ht_txchwidth);
206 SYSINIT(wlan_ht, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_ht_init, NULL);
208 static int ieee80211_ampdu_enable(struct ieee80211_node *ni,
209 struct ieee80211_tx_ampdu *tap);
210 static int ieee80211_addba_request(struct ieee80211_node *ni,
211 struct ieee80211_tx_ampdu *tap,
212 int dialogtoken, int baparamset, int batimeout);
213 static int ieee80211_addba_response(struct ieee80211_node *ni,
214 struct ieee80211_tx_ampdu *tap,
215 int code, int baparamset, int batimeout);
216 static void ieee80211_addba_stop(struct ieee80211_node *ni,
217 struct ieee80211_tx_ampdu *tap);
218 static void null_addba_response_timeout(struct ieee80211_node *ni,
219 struct ieee80211_tx_ampdu *tap);
221 static void ieee80211_bar_response(struct ieee80211_node *ni,
222 struct ieee80211_tx_ampdu *tap, int status);
223 static void ampdu_tx_stop(struct ieee80211_tx_ampdu *tap);
224 static void bar_stop_timer(struct ieee80211_tx_ampdu *tap);
225 static int ampdu_rx_start(struct ieee80211_node *, struct ieee80211_rx_ampdu *,
226 int baparamset, int batimeout, int baseqctl);
227 static void ampdu_rx_stop(struct ieee80211_node *, struct ieee80211_rx_ampdu *);
230 ieee80211_ht_attach(struct ieee80211com *ic)
232 /* setup default aggregation policy */
233 ic->ic_recv_action = ieee80211_recv_action;
234 ic->ic_send_action = ieee80211_send_action;
235 ic->ic_ampdu_enable = ieee80211_ampdu_enable;
236 ic->ic_addba_request = ieee80211_addba_request;
237 ic->ic_addba_response = ieee80211_addba_response;
238 ic->ic_addba_response_timeout = null_addba_response_timeout;
239 ic->ic_addba_stop = ieee80211_addba_stop;
240 ic->ic_bar_response = ieee80211_bar_response;
241 ic->ic_ampdu_rx_start = ampdu_rx_start;
242 ic->ic_ampdu_rx_stop = ampdu_rx_stop;
244 ic->ic_htprotmode = IEEE80211_PROT_RTSCTS;
245 ic->ic_curhtprotmode = IEEE80211_HTINFO_OPMODE_PURE;
249 ieee80211_ht_detach(struct ieee80211com *ic)
254 ieee80211_ht_vattach(struct ieee80211vap *vap)
257 /* driver can override defaults */
258 vap->iv_ampdu_rxmax = IEEE80211_HTCAP_MAXRXAMPDU_8K;
259 vap->iv_ampdu_density = IEEE80211_HTCAP_MPDUDENSITY_NA;
260 vap->iv_ampdu_limit = vap->iv_ampdu_rxmax;
261 vap->iv_amsdu_limit = vap->iv_htcaps & IEEE80211_HTCAP_MAXAMSDU;
262 /* tx aggregation traffic thresholds */
263 vap->iv_ampdu_mintraffic[WME_AC_BK] = 128;
264 vap->iv_ampdu_mintraffic[WME_AC_BE] = 64;
265 vap->iv_ampdu_mintraffic[WME_AC_VO] = 32;
266 vap->iv_ampdu_mintraffic[WME_AC_VI] = 32;
268 if (vap->iv_htcaps & IEEE80211_HTC_HT) {
270 * Device is HT capable; enable all HT-related
271 * facilities by default.
272 * XXX these choices may be too aggressive.
274 vap->iv_flags_ht |= IEEE80211_FHT_HT
275 | IEEE80211_FHT_HTCOMPAT
277 if (vap->iv_htcaps & IEEE80211_HTCAP_SHORTGI20)
278 vap->iv_flags_ht |= IEEE80211_FHT_SHORTGI20;
279 /* XXX infer from channel list? */
280 if (vap->iv_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
281 vap->iv_flags_ht |= IEEE80211_FHT_USEHT40;
282 if (vap->iv_htcaps & IEEE80211_HTCAP_SHORTGI40)
283 vap->iv_flags_ht |= IEEE80211_FHT_SHORTGI40;
285 /* enable RIFS if capable */
286 if (vap->iv_htcaps & IEEE80211_HTC_RIFS)
287 vap->iv_flags_ht |= IEEE80211_FHT_RIFS;
289 /* NB: A-MPDU and A-MSDU rx are mandated, these are tx only */
290 vap->iv_flags_ht |= IEEE80211_FHT_AMPDU_RX;
291 if (vap->iv_htcaps & IEEE80211_HTC_AMPDU)
292 vap->iv_flags_ht |= IEEE80211_FHT_AMPDU_TX;
293 vap->iv_flags_ht |= IEEE80211_FHT_AMSDU_RX;
294 if (vap->iv_htcaps & IEEE80211_HTC_AMSDU)
295 vap->iv_flags_ht |= IEEE80211_FHT_AMSDU_TX;
297 if (vap->iv_htcaps & IEEE80211_HTCAP_TXSTBC)
298 vap->iv_flags_ht |= IEEE80211_FHT_STBC_TX;
299 if (vap->iv_htcaps & IEEE80211_HTCAP_RXSTBC)
300 vap->iv_flags_ht |= IEEE80211_FHT_STBC_RX;
302 if (vap->iv_htcaps & IEEE80211_HTCAP_LDPC)
303 vap->iv_flags_ht |= IEEE80211_FHT_LDPC_RX;
304 if (vap->iv_htcaps & IEEE80211_HTC_TXLDPC)
305 vap->iv_flags_ht |= IEEE80211_FHT_LDPC_TX;
307 /* NB: disable default legacy WDS, too many issues right now */
308 if (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY)
309 vap->iv_flags_ht &= ~IEEE80211_FHT_HT;
313 ieee80211_ht_vdetach(struct ieee80211vap *vap)
318 ht_getrate(struct ieee80211com *ic, int index, enum ieee80211_phymode mode,
323 mword = ieee80211_rate2media(ic, index | IEEE80211_RATE_MCS, mode);
324 if (IFM_SUBTYPE(mword) != IFM_IEEE80211_MCS)
328 rate = ieee80211_htrates[index].ht20_rate_800ns;
331 rate = ieee80211_htrates[index].ht20_rate_400ns;
334 rate = ieee80211_htrates[index].ht40_rate_800ns;
337 rate = ieee80211_htrates[index].ht40_rate_400ns;
343 static struct printranges {
354 { 32, 0, 1, 2, IEEE80211_HTC_TXMCS32 },
355 { 33, 38, 2, 0, IEEE80211_HTC_TXUNEQUAL },
356 { 39, 52, 3, 0, IEEE80211_HTC_TXUNEQUAL },
357 { 53, 76, 4, 0, IEEE80211_HTC_TXUNEQUAL },
362 ht_rateprint(struct ieee80211com *ic, enum ieee80211_phymode mode, int ratetype)
364 int minrate, maxrate;
365 struct printranges *range;
367 for (range = ranges; range->txstream != 0; range++) {
368 if (ic->ic_txstream < range->txstream)
370 if (range->htcapflags &&
371 (ic->ic_htcaps & range->htcapflags) == 0)
373 if (ratetype < range->ratetype)
375 minrate = ht_getrate(ic, range->minmcs, mode, ratetype);
376 maxrate = ht_getrate(ic, range->maxmcs, mode, ratetype);
378 ic_printf(ic, "MCS %d-%d: %d%sMbps - %d%sMbps\n",
379 range->minmcs, range->maxmcs,
380 minrate/2, ((minrate & 0x1) != 0 ? ".5" : ""),
381 maxrate/2, ((maxrate & 0x1) != 0 ? ".5" : ""));
383 ic_printf(ic, "MCS %d: %d%sMbps\n", range->minmcs,
384 minrate/2, ((minrate & 0x1) != 0 ? ".5" : ""));
390 ht_announce(struct ieee80211com *ic, enum ieee80211_phymode mode)
392 const char *modestr = ieee80211_phymode_name[mode];
394 ic_printf(ic, "%s MCS 20MHz\n", modestr);
395 ht_rateprint(ic, mode, 0);
396 if (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20) {
397 ic_printf(ic, "%s MCS 20MHz SGI\n", modestr);
398 ht_rateprint(ic, mode, 1);
400 if (ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
401 ic_printf(ic, "%s MCS 40MHz:\n", modestr);
402 ht_rateprint(ic, mode, 2);
404 if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
405 (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40)) {
406 ic_printf(ic, "%s MCS 40MHz SGI:\n", modestr);
407 ht_rateprint(ic, mode, 3);
412 ieee80211_ht_announce(struct ieee80211com *ic)
415 if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) ||
416 isset(ic->ic_modecaps, IEEE80211_MODE_11NG))
417 ic_printf(ic, "%dT%dR\n", ic->ic_txstream, ic->ic_rxstream);
418 if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA))
419 ht_announce(ic, IEEE80211_MODE_11NA);
420 if (isset(ic->ic_modecaps, IEEE80211_MODE_11NG))
421 ht_announce(ic, IEEE80211_MODE_11NG);
425 ieee80211_init_suphtrates(struct ieee80211com *ic)
427 #define ADDRATE(x) do { \
428 htrateset->rs_rates[htrateset->rs_nrates] = x; \
429 htrateset->rs_nrates++; \
431 struct ieee80211_htrateset *htrateset = &ic->ic_sup_htrates;
434 memset(htrateset, 0, sizeof(struct ieee80211_htrateset));
435 for (i = 0; i < ic->ic_txstream * 8; i++)
437 if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
438 (ic->ic_htcaps & IEEE80211_HTC_TXMCS32))
440 if (ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL) {
441 if (ic->ic_txstream >= 2) {
442 for (i = 33; i <= 38; i++)
445 if (ic->ic_txstream >= 3) {
446 for (i = 39; i <= 52; i++)
449 if (ic->ic_txstream == 4) {
450 for (i = 53; i <= 76; i++)
458 * Receive processing.
462 * Decap the encapsulated A-MSDU frames and dispatch all but
463 * the last for delivery. The last frame is returned for
464 * delivery via the normal path.
467 ieee80211_decap_amsdu(struct ieee80211_node *ni, struct mbuf *m)
469 struct ieee80211vap *vap = ni->ni_vap;
473 /* discard 802.3 header inserted by ieee80211_decap */
474 m_adj(m, sizeof(struct ether_header));
476 vap->iv_stats.is_amsdu_decap++;
480 * Decap the first frame, bust it apart from the
481 * remainder and deliver. We leave the last frame
482 * delivery to the caller (for consistency with other
483 * code paths, could also do it here).
485 m = ieee80211_decap1(m, &framelen);
487 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
488 ni->ni_macaddr, "a-msdu", "%s", "decap failed");
489 vap->iv_stats.is_amsdu_tooshort++;
492 if (m->m_pkthdr.len == framelen)
494 n = m_split(m, framelen, M_NOWAIT);
496 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
497 ni->ni_macaddr, "a-msdu",
498 "%s", "unable to split encapsulated frames");
499 vap->iv_stats.is_amsdu_split++;
500 m_freem(m); /* NB: must reclaim */
503 vap->iv_deliver_data(vap, ni, m);
506 * Remove frame contents; each intermediate frame
507 * is required to be aligned to a 4-byte boundary.
510 m_adj(m, roundup2(framelen, 4) - framelen); /* padding */
512 return m; /* last delivered by caller */
516 * Add the given frame to the current RX reorder slot.
518 * For future offloaded A-MSDU handling where multiple frames with
519 * the same sequence number show up here, this routine will append
520 * those frames as long as they're appropriately tagged.
523 ampdu_rx_add_slot(struct ieee80211_rx_ampdu *rap, int off, int tid,
525 struct ieee80211_node *ni,
528 struct ieee80211vap *vap = ni->ni_vap;
530 if (rap->rxa_m[off] == NULL) {
533 rap->rxa_qbytes += m->m_pkthdr.len;
534 vap->iv_stats.is_ampdu_rx_reorder++;
537 IEEE80211_DISCARD_MAC(vap,
538 IEEE80211_MSG_INPUT | IEEE80211_MSG_11N,
539 ni->ni_macaddr, "a-mpdu duplicate",
540 "seqno %u tid %u BA win <%u:%u>",
541 rxseq, tid, rap->rxa_start,
542 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1));
543 vap->iv_stats.is_rx_dup++;
544 IEEE80211_NODE_STAT(ni, rx_dup);
551 ampdu_rx_purge_slot(struct ieee80211_rx_ampdu *rap, int i)
559 rap->rxa_m[i] = NULL;
560 rap->rxa_qbytes -= m->m_pkthdr.len;
566 * Purge all frames in the A-MPDU re-order queue.
569 ampdu_rx_purge(struct ieee80211_rx_ampdu *rap)
573 for (i = 0; i < rap->rxa_wnd; i++) {
574 ampdu_rx_purge_slot(rap, i);
575 if (rap->rxa_qframes == 0)
578 KASSERT(rap->rxa_qbytes == 0 && rap->rxa_qframes == 0,
579 ("lost %u data, %u frames on ampdu rx q",
580 rap->rxa_qbytes, rap->rxa_qframes));
584 * Start A-MPDU rx/re-order processing for the specified TID.
587 ampdu_rx_start(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap,
588 int baparamset, int batimeout, int baseqctl)
590 int bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
592 if (rap->rxa_flags & IEEE80211_AGGR_RUNNING) {
594 * AMPDU previously setup and not terminated with a DELBA,
595 * flush the reorder q's in case anything remains.
599 memset(rap, 0, sizeof(*rap));
600 rap->rxa_wnd = (bufsiz == 0) ?
601 IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
602 rap->rxa_start = MS(baseqctl, IEEE80211_BASEQ_START);
603 rap->rxa_flags |= IEEE80211_AGGR_RUNNING | IEEE80211_AGGR_XCHGPEND;
609 * Public function; manually setup the RX ampdu state.
612 ieee80211_ampdu_rx_start_ext(struct ieee80211_node *ni, int tid, int seq, int baw)
614 struct ieee80211_rx_ampdu *rap;
616 /* XXX TODO: sanity check tid, seq, baw */
618 rap = &ni->ni_rx_ampdu[tid];
620 if (rap->rxa_flags & IEEE80211_AGGR_RUNNING) {
622 * AMPDU previously setup and not terminated with a DELBA,
623 * flush the reorder q's in case anything remains.
628 memset(rap, 0, sizeof(*rap));
629 rap->rxa_wnd = (baw== 0) ?
630 IEEE80211_AGGR_BAWMAX : min(baw, IEEE80211_AGGR_BAWMAX);
632 /* Wait for the first RX frame, use that as BAW */
634 rap->rxa_flags |= IEEE80211_AGGR_WAITRX;
636 rap->rxa_start = seq;
638 rap->rxa_flags |= IEEE80211_AGGR_RUNNING | IEEE80211_AGGR_XCHGPEND;
640 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, ni,
641 "%s: tid=%d, start=%d, wnd=%d, flags=0x%08x",
652 * Public function; manually stop the RX AMPDU state.
655 ieee80211_ampdu_rx_stop_ext(struct ieee80211_node *ni, int tid)
657 struct ieee80211_rx_ampdu *rap;
659 /* XXX TODO: sanity check tid, seq, baw */
660 rap = &ni->ni_rx_ampdu[tid];
661 ampdu_rx_stop(ni, rap);
665 * Stop A-MPDU rx processing for the specified TID.
668 ampdu_rx_stop(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
672 rap->rxa_flags &= ~(IEEE80211_AGGR_RUNNING
673 | IEEE80211_AGGR_XCHGPEND
674 | IEEE80211_AGGR_WAITRX);
678 * Dispatch a frame from the A-MPDU reorder queue. The
679 * frame is fed back into ieee80211_input marked with an
680 * M_AMPDU_MPDU flag so it doesn't come back to us (it also
681 * permits ieee80211_input to optimize re-processing).
684 ampdu_dispatch(struct ieee80211_node *ni, struct mbuf *m)
686 m->m_flags |= M_AMPDU_MPDU; /* bypass normal processing */
687 /* NB: rssi and noise are ignored w/ M_AMPDU_MPDU set */
688 (void) ieee80211_input(ni, m, 0, 0);
692 ampdu_dispatch_slot(struct ieee80211_rx_ampdu *rap, struct ieee80211_node *ni,
697 if (rap->rxa_m[i] == NULL)
701 rap->rxa_m[i] = NULL;
702 rap->rxa_qbytes -= m->m_pkthdr.len;
705 ampdu_dispatch(ni, m);
711 ampdu_rx_moveup(struct ieee80211_rx_ampdu *rap, struct ieee80211_node *ni,
714 struct ieee80211vap *vap = ni->ni_vap;
716 if (rap->rxa_qframes != 0) {
717 int n = rap->rxa_qframes, j;
719 if (winstart != -1) {
721 * NB: in window-sliding mode, loop assumes i > 0
722 * and/or rxa_m[0] is NULL
724 KASSERT(rap->rxa_m[0] == NULL,
725 ("%s: BA window slot 0 occupied", __func__));
727 for (j = i+1; j < rap->rxa_wnd; j++) {
728 if (rap->rxa_m[j] != NULL) {
729 rap->rxa_m[j-i] = rap->rxa_m[j];
730 rap->rxa_m[j] = NULL;
735 KASSERT(n == 0, ("%s: lost %d frames, qframes %d off %d "
736 "BA win <%d:%d> winstart %d",
737 __func__, n, rap->rxa_qframes, i, rap->rxa_start,
738 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
740 vap->iv_stats.is_ampdu_rx_copy += rap->rxa_qframes;
745 * Dispatch as many frames as possible from the re-order queue.
746 * Frames will always be "at the front"; we process all frames
747 * up to the first empty slot in the window. On completion we
748 * cleanup state if there are still pending frames in the current
749 * BA window. We assume the frame at slot 0 is already handled
750 * by the caller; we always start at slot 1.
753 ampdu_rx_dispatch(struct ieee80211_rx_ampdu *rap, struct ieee80211_node *ni)
755 struct ieee80211vap *vap = ni->ni_vap;
758 /* flush run of frames */
759 for (i = 1; i < rap->rxa_wnd; i++) {
760 if (ampdu_dispatch_slot(rap, ni, i) == 0)
765 * If frames remain, copy the mbuf pointers down so
766 * they correspond to the offsets in the new window.
768 ampdu_rx_moveup(rap, ni, i, -1);
771 * Adjust the start of the BA window to
772 * reflect the frames just dispatched.
774 rap->rxa_start = IEEE80211_SEQ_ADD(rap->rxa_start, i);
775 vap->iv_stats.is_ampdu_rx_oor += i;
779 * Dispatch all frames in the A-MPDU re-order queue.
782 ampdu_rx_flush(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
784 struct ieee80211vap *vap = ni->ni_vap;
787 for (i = 0; i < rap->rxa_wnd; i++) {
788 r = ampdu_dispatch_slot(rap, ni, i);
791 vap->iv_stats.is_ampdu_rx_oor += r;
793 if (rap->rxa_qframes == 0)
799 * Dispatch all frames in the A-MPDU re-order queue
800 * preceding the specified sequence number. This logic
801 * handles window moves due to a received MSDU or BAR.
804 ampdu_rx_flush_upto(struct ieee80211_node *ni,
805 struct ieee80211_rx_ampdu *rap, ieee80211_seq winstart)
807 struct ieee80211vap *vap = ni->ni_vap;
812 * Flush any complete MSDU's with a sequence number lower
813 * than winstart. Gaps may exist. Note that we may actually
814 * dispatch frames past winstart if a run continues; this is
815 * an optimization that avoids having to do a separate pass
816 * to dispatch frames after moving the BA window start.
818 seqno = rap->rxa_start;
819 for (i = 0; i < rap->rxa_wnd; i++) {
820 r = ampdu_dispatch_slot(rap, ni, i);
822 if (!IEEE80211_SEQ_BA_BEFORE(seqno, winstart))
825 vap->iv_stats.is_ampdu_rx_oor += r;
826 seqno = IEEE80211_SEQ_INC(seqno);
829 * If frames remain, copy the mbuf pointers down so
830 * they correspond to the offsets in the new window.
832 ampdu_rx_moveup(rap, ni, i, winstart);
835 * Move the start of the BA window; we use the
836 * sequence number of the last MSDU that was
837 * passed up the stack+1 or winstart if stopped on
838 * a gap in the reorder buffer.
840 rap->rxa_start = seqno;
844 * Process a received QoS data frame for an HT station. Handle
845 * A-MPDU reordering: if this frame is received out of order
846 * and falls within the BA window hold onto it. Otherwise if
847 * this frame completes a run, flush any pending frames. We
848 * return 1 if the frame is consumed. A 0 is returned if
849 * the frame should be processed normally by the caller.
852 ieee80211_ampdu_reorder(struct ieee80211_node *ni, struct mbuf *m,
853 const struct ieee80211_rx_stats *rxs)
855 #define PROCESS 0 /* caller should process frame */
856 #define CONSUMED 1 /* frame consumed, caller does nothing */
857 struct ieee80211vap *vap = ni->ni_vap;
858 struct ieee80211_qosframe *wh;
859 struct ieee80211_rx_ampdu *rap;
864 KASSERT((m->m_flags & (M_AMPDU | M_AMPDU_MPDU)) == M_AMPDU,
865 ("!a-mpdu or already re-ordered, flags 0x%x", m->m_flags));
866 KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT sta"));
868 /* NB: m_len known to be sufficient */
869 wh = mtod(m, struct ieee80211_qosframe *);
870 if (wh->i_fc[0] != IEEE80211_FC0_QOSDATA) {
872 * Not QoS data, shouldn't get here but just
873 * return it to the caller for processing.
879 * 802.11-2012 9.3.2.10 - Duplicate detection and recovery.
881 * Multicast QoS data frames are checked against a different
882 * counter, not the per-TID counter.
884 if (IEEE80211_IS_MULTICAST(wh->i_addr1))
887 if (IEEE80211_IS_DSTODS(wh))
888 tid = ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0];
891 tid &= IEEE80211_QOS_TID;
892 rap = &ni->ni_rx_ampdu[tid];
893 if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
895 * No ADDBA request yet, don't touch.
899 rxseq = le16toh(*(uint16_t *)wh->i_seq);
900 if ((rxseq & IEEE80211_SEQ_FRAG_MASK) != 0) {
902 * Fragments are not allowed; toss.
904 IEEE80211_DISCARD_MAC(vap,
905 IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
906 "A-MPDU", "fragment, rxseq 0x%x tid %u%s", rxseq, tid,
907 wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
908 vap->iv_stats.is_ampdu_rx_drop++;
909 IEEE80211_NODE_STAT(ni, rx_drop);
913 rxseq >>= IEEE80211_SEQ_SEQ_SHIFT;
917 * Handle waiting for the first frame to define the BAW.
918 * Some firmware doesn't provide the RX of the starting point
919 * of the BAW and we have to cope.
921 if (rap->rxa_flags & IEEE80211_AGGR_WAITRX) {
922 rap->rxa_flags &= ~IEEE80211_AGGR_WAITRX;
923 rap->rxa_start = rxseq;
926 if (rxseq == rap->rxa_start) {
928 * First frame in window.
930 if (rap->rxa_qframes != 0) {
932 * Dispatch as many packets as we can.
934 KASSERT(rap->rxa_m[0] == NULL, ("unexpected dup"));
935 ampdu_dispatch(ni, m);
936 ampdu_rx_dispatch(rap, ni);
940 * In order; advance window and notify
941 * caller to dispatch directly.
943 rap->rxa_start = IEEE80211_SEQ_INC(rxseq);
948 * Frame is out of order; store if in the BA window.
950 /* calculate offset in BA window */
951 off = IEEE80211_SEQ_SUB(rxseq, rap->rxa_start);
952 if (off < rap->rxa_wnd) {
954 * Common case (hopefully): in the BA window.
955 * Sec 9.10.7.6.2 a) (p.137)
959 * Check for frames sitting too long in the reorder queue.
960 * This should only ever happen if frames are not delivered
961 * without the sender otherwise notifying us (e.g. with a
962 * BAR to move the window). Typically this happens because
963 * of vendor bugs that cause the sequence number to jump.
964 * When this happens we get a gap in the reorder queue that
965 * leaves frame sitting on the queue until they get pushed
966 * out due to window moves. When the vendor does not send
967 * BAR this move only happens due to explicit packet sends
969 * NB: we only track the time of the oldest frame in the
970 * reorder q; this means that if we flush we might push
971 * frames that still "new"; if this happens then subsequent
972 * frames will result in BA window moves which cost something
973 * but is still better than a big throughput dip.
975 if (rap->rxa_qframes != 0) {
976 /* XXX honor batimeout? */
977 if (ticks - rap->rxa_age > ieee80211_ampdu_age) {
979 * Too long since we received the first
980 * frame; flush the reorder buffer.
982 if (rap->rxa_qframes != 0) {
983 vap->iv_stats.is_ampdu_rx_age +=
985 ampdu_rx_flush(ni, rap);
987 rap->rxa_start = IEEE80211_SEQ_INC(rxseq);
992 * First frame, start aging timer.
994 rap->rxa_age = ticks;
997 /* save packet - this consumes, no matter what */
998 ampdu_rx_add_slot(rap, off, tid, rxseq, ni, m);
1002 if (off < IEEE80211_SEQ_BA_RANGE) {
1004 * Outside the BA window, but within range;
1005 * flush the reorder q and move the window.
1006 * Sec 9.10.7.6.2 b) (p.138)
1008 IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
1009 "move BA win <%u:%u> (%u frames) rxseq %u tid %u",
1011 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
1012 rap->rxa_qframes, rxseq, tid);
1013 vap->iv_stats.is_ampdu_rx_move++;
1016 * The spec says to flush frames up to but not including:
1017 * WinStart_B = rxseq - rap->rxa_wnd + 1
1018 * Then insert the frame or notify the caller to process
1019 * it immediately. We can safely do this by just starting
1020 * over again because we know the frame will now be within
1023 /* NB: rxa_wnd known to be >0 */
1024 ampdu_rx_flush_upto(ni, rap,
1025 IEEE80211_SEQ_SUB(rxseq, rap->rxa_wnd-1));
1029 * Outside the BA window and out of range; toss.
1030 * Sec 9.10.7.6.2 c) (p.138)
1032 IEEE80211_DISCARD_MAC(vap,
1033 IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
1034 "MPDU", "BA win <%u:%u> (%u frames) rxseq %u tid %u%s",
1036 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
1037 rap->rxa_qframes, rxseq, tid,
1038 wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
1039 vap->iv_stats.is_ampdu_rx_drop++;
1040 IEEE80211_NODE_STAT(ni, rx_drop);
1049 * Process a BAR ctl frame. Dispatch all frames up to
1050 * the sequence number of the frame. If this frame is
1051 * out of range it's discarded.
1054 ieee80211_recv_bar(struct ieee80211_node *ni, struct mbuf *m0)
1056 struct ieee80211vap *vap = ni->ni_vap;
1057 struct ieee80211_frame_bar *wh;
1058 struct ieee80211_rx_ampdu *rap;
1059 ieee80211_seq rxseq;
1062 if (!ieee80211_recv_bar_ena) {
1064 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_11N,
1065 ni->ni_macaddr, "BAR", "%s", "processing disabled");
1067 vap->iv_stats.is_ampdu_bar_bad++;
1070 wh = mtod(m0, struct ieee80211_frame_bar *);
1071 /* XXX check basic BAR */
1072 tid = MS(le16toh(wh->i_ctl), IEEE80211_BAR_TID);
1073 rap = &ni->ni_rx_ampdu[tid];
1074 if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
1076 * No ADDBA request yet, don't touch.
1078 IEEE80211_DISCARD_MAC(vap,
1079 IEEE80211_MSG_INPUT | IEEE80211_MSG_11N,
1080 ni->ni_macaddr, "BAR", "no BA stream, tid %u", tid);
1081 vap->iv_stats.is_ampdu_bar_bad++;
1084 vap->iv_stats.is_ampdu_bar_rx++;
1085 rxseq = le16toh(wh->i_seq) >> IEEE80211_SEQ_SEQ_SHIFT;
1086 if (rxseq == rap->rxa_start)
1088 /* calculate offset in BA window */
1089 off = IEEE80211_SEQ_SUB(rxseq, rap->rxa_start);
1090 if (off < IEEE80211_SEQ_BA_RANGE) {
1092 * Flush the reorder q up to rxseq and move the window.
1093 * Sec 9.10.7.6.3 a) (p.138)
1095 IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
1096 "BAR moves BA win <%u:%u> (%u frames) rxseq %u tid %u",
1098 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
1099 rap->rxa_qframes, rxseq, tid);
1100 vap->iv_stats.is_ampdu_bar_move++;
1102 ampdu_rx_flush_upto(ni, rap, rxseq);
1103 if (off >= rap->rxa_wnd) {
1105 * BAR specifies a window start to the right of BA
1106 * window; we must move it explicitly since
1107 * ampdu_rx_flush_upto will not.
1109 rap->rxa_start = rxseq;
1113 * Out of range; toss.
1114 * Sec 9.10.7.6.3 b) (p.138)
1116 IEEE80211_DISCARD_MAC(vap,
1117 IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
1118 "BAR", "BA win <%u:%u> (%u frames) rxseq %u tid %u%s",
1120 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
1121 rap->rxa_qframes, rxseq, tid,
1122 wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
1123 vap->iv_stats.is_ampdu_bar_oow++;
1124 IEEE80211_NODE_STAT(ni, rx_drop);
1129 * Setup HT-specific state in a node. Called only
1130 * when HT use is negotiated so we don't do extra
1131 * work for temporary and/or legacy sta's.
1134 ieee80211_ht_node_init(struct ieee80211_node *ni)
1136 struct ieee80211_tx_ampdu *tap;
1139 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
1145 if (ni->ni_flags & IEEE80211_NODE_HT) {
1147 * Clean AMPDU state on re-associate. This handles the case
1148 * where a station leaves w/o notifying us and then returns
1149 * before node is reaped for inactivity.
1151 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
1153 "%s: calling cleanup (%p)",
1155 ieee80211_ht_node_cleanup(ni);
1157 for (tid = 0; tid < WME_NUM_TID; tid++) {
1158 tap = &ni->ni_tx_ampdu[tid];
1161 ieee80211_txampdu_init_pps(tap);
1162 /* NB: further initialization deferred */
1164 ni->ni_flags |= IEEE80211_NODE_HT | IEEE80211_NODE_AMPDU;
1168 * Cleanup HT-specific state in a node. Called only
1169 * when HT use has been marked.
1172 ieee80211_ht_node_cleanup(struct ieee80211_node *ni)
1174 struct ieee80211com *ic = ni->ni_ic;
1177 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
1182 KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT node"));
1184 /* XXX optimize this */
1185 for (i = 0; i < WME_NUM_TID; i++) {
1186 struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[i];
1187 if (tap->txa_flags & IEEE80211_AGGR_SETUP)
1190 for (i = 0; i < WME_NUM_TID; i++)
1191 ic->ic_ampdu_rx_stop(ni, &ni->ni_rx_ampdu[i]);
1194 ni->ni_flags &= ~IEEE80211_NODE_HT_ALL;
1198 * Age out HT resources for a station.
1201 ieee80211_ht_node_age(struct ieee80211_node *ni)
1203 struct ieee80211vap *vap = ni->ni_vap;
1206 KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT sta"));
1208 for (tid = 0; tid < WME_NUM_TID; tid++) {
1209 struct ieee80211_rx_ampdu *rap;
1211 rap = &ni->ni_rx_ampdu[tid];
1212 if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0)
1214 if (rap->rxa_qframes == 0)
1217 * Check for frames sitting too long in the reorder queue.
1218 * See above for more details on what's happening here.
1220 /* XXX honor batimeout? */
1221 if (ticks - rap->rxa_age > ieee80211_ampdu_age) {
1223 * Too long since we received the first
1224 * frame; flush the reorder buffer.
1226 vap->iv_stats.is_ampdu_rx_age += rap->rxa_qframes;
1227 ampdu_rx_flush(ni, rap);
1232 static struct ieee80211_channel *
1233 findhtchan(struct ieee80211com *ic, struct ieee80211_channel *c, int htflags)
1235 return ieee80211_find_channel(ic, c->ic_freq,
1236 (c->ic_flags &~ IEEE80211_CHAN_HT) | htflags);
1240 * Adjust a channel to be HT/non-HT according to the vap's configuration.
1242 struct ieee80211_channel *
1243 ieee80211_ht_adjust_channel(struct ieee80211com *ic,
1244 struct ieee80211_channel *chan, int flags)
1246 struct ieee80211_channel *c;
1248 if (flags & IEEE80211_FHT_HT) {
1249 /* promote to HT if possible */
1250 if (flags & IEEE80211_FHT_USEHT40) {
1251 if (!IEEE80211_IS_CHAN_HT40(chan)) {
1252 /* NB: arbitrarily pick ht40+ over ht40- */
1253 c = findhtchan(ic, chan, IEEE80211_CHAN_HT40U);
1255 c = findhtchan(ic, chan,
1256 IEEE80211_CHAN_HT40D);
1258 c = findhtchan(ic, chan,
1259 IEEE80211_CHAN_HT20);
1263 } else if (!IEEE80211_IS_CHAN_HT20(chan)) {
1264 c = findhtchan(ic, chan, IEEE80211_CHAN_HT20);
1268 } else if (IEEE80211_IS_CHAN_HT(chan)) {
1269 /* demote to legacy, HT use is disabled */
1270 c = ieee80211_find_channel(ic, chan->ic_freq,
1271 chan->ic_flags &~ IEEE80211_CHAN_HT);
1279 * Setup HT-specific state for a legacy WDS peer.
1282 ieee80211_ht_wds_init(struct ieee80211_node *ni)
1284 struct ieee80211vap *vap = ni->ni_vap;
1285 struct ieee80211_tx_ampdu *tap;
1288 KASSERT(vap->iv_flags_ht & IEEE80211_FHT_HT, ("no HT requested"));
1290 /* XXX check scan cache in case peer has an ap and we have info */
1292 * If setup with a legacy channel; locate an HT channel.
1293 * Otherwise if the inherited channel (from a companion
1294 * AP) is suitable use it so we use the same location
1295 * for the extension channel).
1297 ni->ni_chan = ieee80211_ht_adjust_channel(ni->ni_ic,
1298 ni->ni_chan, ieee80211_htchanflags(ni->ni_chan));
1301 if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20)
1302 ni->ni_htcap |= IEEE80211_HTCAP_SHORTGI20;
1303 if (IEEE80211_IS_CHAN_HT40(ni->ni_chan)) {
1304 ni->ni_htcap |= IEEE80211_HTCAP_CHWIDTH40;
1306 if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan))
1307 ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_ABOVE;
1308 else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan))
1309 ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_BELOW;
1310 if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40)
1311 ni->ni_htcap |= IEEE80211_HTCAP_SHORTGI40;
1314 ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_NONE;
1316 ni->ni_htctlchan = ni->ni_chan->ic_ieee;
1317 if (vap->iv_flags_ht & IEEE80211_FHT_RIFS)
1318 ni->ni_flags |= IEEE80211_NODE_RIFS;
1319 /* XXX does it make sense to enable SMPS? */
1321 ni->ni_htopmode = 0; /* XXX need protection state */
1322 ni->ni_htstbc = 0; /* XXX need info */
1324 for (tid = 0; tid < WME_NUM_TID; tid++) {
1325 tap = &ni->ni_tx_ampdu[tid];
1327 ieee80211_txampdu_init_pps(tap);
1329 /* NB: AMPDU tx/rx governed by IEEE80211_FHT_AMPDU_{TX,RX} */
1330 ni->ni_flags |= IEEE80211_NODE_HT | IEEE80211_NODE_AMPDU;
1334 * Notify hostap vaps of a change in the HTINFO ie.
1337 htinfo_notify(struct ieee80211com *ic)
1339 struct ieee80211vap *vap;
1342 IEEE80211_LOCK_ASSERT(ic);
1344 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1345 if (vap->iv_opmode != IEEE80211_M_HOSTAP)
1347 if (vap->iv_state != IEEE80211_S_RUN ||
1348 !IEEE80211_IS_CHAN_HT(vap->iv_bss->ni_chan))
1352 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N,
1354 "HT bss occupancy change: %d sta, %d ht, "
1355 "%d ht40%s, HT protmode now 0x%x"
1357 , ic->ic_ht_sta_assoc
1358 , ic->ic_ht40_sta_assoc
1359 , (ic->ic_flags_ht & IEEE80211_FHT_NONHT_PR) ?
1360 ", non-HT sta present" : ""
1361 , ic->ic_curhtprotmode);
1364 ieee80211_beacon_notify(vap, IEEE80211_BEACON_HTINFO);
1369 * Calculate HT protection mode from current
1370 * state and handle updates.
1373 htinfo_update(struct ieee80211com *ic)
1377 if (ic->ic_sta_assoc != ic->ic_ht_sta_assoc) {
1378 protmode = IEEE80211_HTINFO_OPMODE_MIXED
1379 | IEEE80211_HTINFO_NONHT_PRESENT;
1380 } else if (ic->ic_flags_ht & IEEE80211_FHT_NONHT_PR) {
1381 protmode = IEEE80211_HTINFO_OPMODE_PROTOPT
1382 | IEEE80211_HTINFO_NONHT_PRESENT;
1383 } else if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
1384 IEEE80211_IS_CHAN_HT40(ic->ic_bsschan) &&
1385 ic->ic_sta_assoc != ic->ic_ht40_sta_assoc) {
1386 protmode = IEEE80211_HTINFO_OPMODE_HT20PR;
1388 protmode = IEEE80211_HTINFO_OPMODE_PURE;
1390 if (protmode != ic->ic_curhtprotmode) {
1391 ic->ic_curhtprotmode = protmode;
1397 * Handle an HT station joining a BSS.
1400 ieee80211_ht_node_join(struct ieee80211_node *ni)
1402 struct ieee80211com *ic = ni->ni_ic;
1404 IEEE80211_LOCK_ASSERT(ic);
1406 if (ni->ni_flags & IEEE80211_NODE_HT) {
1407 ic->ic_ht_sta_assoc++;
1408 if (ni->ni_chw == 40)
1409 ic->ic_ht40_sta_assoc++;
1415 * Handle an HT station leaving a BSS.
1418 ieee80211_ht_node_leave(struct ieee80211_node *ni)
1420 struct ieee80211com *ic = ni->ni_ic;
1422 IEEE80211_LOCK_ASSERT(ic);
1424 if (ni->ni_flags & IEEE80211_NODE_HT) {
1425 ic->ic_ht_sta_assoc--;
1426 if (ni->ni_chw == 40)
1427 ic->ic_ht40_sta_assoc--;
1433 * Public version of htinfo_update; used for processing
1434 * beacon frames from overlapping bss.
1436 * Caller can specify either IEEE80211_HTINFO_OPMODE_MIXED
1437 * (on receipt of a beacon that advertises MIXED) or
1438 * IEEE80211_HTINFO_OPMODE_PROTOPT (on receipt of a beacon
1439 * from an overlapping legacy bss). We treat MIXED with
1440 * a higher precedence than PROTOPT (i.e. we will not change
1441 * change PROTOPT -> MIXED; only MIXED -> PROTOPT). This
1442 * corresponds to how we handle things in htinfo_update.
1445 ieee80211_htprot_update(struct ieee80211com *ic, int protmode)
1447 #define OPMODE(x) SM(x, IEEE80211_HTINFO_OPMODE)
1450 /* track non-HT station presence */
1451 KASSERT(protmode & IEEE80211_HTINFO_NONHT_PRESENT,
1452 ("protmode 0x%x", protmode));
1453 ic->ic_flags_ht |= IEEE80211_FHT_NONHT_PR;
1454 ic->ic_lastnonht = ticks;
1456 if (protmode != ic->ic_curhtprotmode &&
1457 (OPMODE(ic->ic_curhtprotmode) != IEEE80211_HTINFO_OPMODE_MIXED ||
1458 OPMODE(protmode) == IEEE80211_HTINFO_OPMODE_PROTOPT)) {
1459 /* push beacon update */
1460 ic->ic_curhtprotmode = protmode;
1463 IEEE80211_UNLOCK(ic);
1468 * Time out presence of an overlapping bss with non-HT
1469 * stations. When operating in hostap mode we listen for
1470 * beacons from other stations and if we identify a non-HT
1471 * station is present we update the opmode field of the
1472 * HTINFO ie. To identify when all non-HT stations are
1473 * gone we time out this condition.
1476 ieee80211_ht_timeout(struct ieee80211com *ic)
1478 IEEE80211_LOCK_ASSERT(ic);
1480 if ((ic->ic_flags_ht & IEEE80211_FHT_NONHT_PR) &&
1481 ieee80211_time_after(ticks, ic->ic_lastnonht + IEEE80211_NONHT_PRESENT_AGE)) {
1483 IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
1484 "%s", "time out non-HT STA present on channel");
1486 ic->ic_flags_ht &= ~IEEE80211_FHT_NONHT_PR;
1492 * Process an 802.11n HT capabilities ie.
1495 ieee80211_parse_htcap(struct ieee80211_node *ni, const uint8_t *ie)
1497 if (ie[0] == IEEE80211_ELEMID_VENDOR) {
1499 * Station used Vendor OUI ie to associate;
1500 * mark the node so when we respond we'll use
1501 * the Vendor OUI's and not the standard ie's.
1503 ni->ni_flags |= IEEE80211_NODE_HTCOMPAT;
1506 ni->ni_flags &= ~IEEE80211_NODE_HTCOMPAT;
1508 ni->ni_htcap = le16dec(ie +
1509 __offsetof(struct ieee80211_ie_htcap, hc_cap));
1510 ni->ni_htparam = ie[__offsetof(struct ieee80211_ie_htcap, hc_param)];
1514 htinfo_parse(struct ieee80211_node *ni,
1515 const struct ieee80211_ie_htinfo *htinfo)
1519 ni->ni_htctlchan = htinfo->hi_ctrlchannel;
1520 ni->ni_ht2ndchan = SM(htinfo->hi_byte1, IEEE80211_HTINFO_2NDCHAN);
1521 w = le16dec(&htinfo->hi_byte2);
1522 ni->ni_htopmode = SM(w, IEEE80211_HTINFO_OPMODE);
1523 w = le16dec(&htinfo->hi_byte45);
1524 ni->ni_htstbc = SM(w, IEEE80211_HTINFO_BASIC_STBCMCS);
1528 * Parse an 802.11n HT info ie and save useful information
1529 * to the node state. Note this does not effect any state
1530 * changes such as for channel width change.
1533 ieee80211_parse_htinfo(struct ieee80211_node *ni, const uint8_t *ie)
1535 if (ie[0] == IEEE80211_ELEMID_VENDOR)
1537 htinfo_parse(ni, (const struct ieee80211_ie_htinfo *) ie);
1541 * Handle 11n/11ac channel switch.
1543 * Use the received HT/VHT ie's to identify the right channel to use.
1544 * If we cannot locate it in the channel table then fallback to
1547 * Note that we use this information to identify the node's
1548 * channel only; the caller is responsible for insuring any
1549 * required channel change is done (e.g. in sta mode when
1550 * parsing the contents of a beacon frame).
1553 htinfo_update_chw(struct ieee80211_node *ni, int htflags, int vhtflags)
1555 struct ieee80211com *ic = ni->ni_ic;
1556 struct ieee80211_channel *c;
1561 * First step - do HT/VHT only channel lookup based on operating mode
1562 * flags. This involves masking out the VHT flags as well.
1563 * Otherwise we end up doing the full channel walk each time
1564 * we trigger this, which is expensive.
1566 chanflags = (ni->ni_chan->ic_flags &~
1567 (IEEE80211_CHAN_HT | IEEE80211_CHAN_VHT)) | htflags | vhtflags;
1569 if (chanflags == ni->ni_chan->ic_flags)
1573 * If HT /or/ VHT flags have changed then check both.
1574 * We need to start by picking a HT channel anyway.
1578 chanflags = (ni->ni_chan->ic_flags &~
1579 (IEEE80211_CHAN_HT | IEEE80211_CHAN_VHT)) | htflags;
1580 /* XXX not right for ht40- */
1581 c = ieee80211_find_channel(ic, ni->ni_chan->ic_freq, chanflags);
1582 if (c == NULL && (htflags & IEEE80211_CHAN_HT40)) {
1584 * No HT40 channel entry in our table; fall back
1585 * to HT20 operation. This should not happen.
1587 c = findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT20);
1589 IEEE80211_NOTE(ni->ni_vap,
1590 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
1591 "no HT40 channel (freq %u), falling back to HT20",
1592 ni->ni_chan->ic_freq);
1597 /* Nothing found - leave it alone; move onto VHT */
1602 * If it's non-HT, then bail out now.
1604 if (! IEEE80211_IS_CHAN_HT(c)) {
1605 IEEE80211_NOTE(ni->ni_vap,
1606 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
1607 "not HT; skipping VHT check (%u/0x%x)",
1608 c->ic_freq, c->ic_flags);
1613 * Next step - look at the current VHT flags and determine
1614 * if we need to upgrade. Mask out the VHT and HT flags since
1615 * the vhtflags field will already have the correct HT
1618 if (IEEE80211_CONF_VHT(ic) && ni->ni_vhtcap != 0 && vhtflags != 0) {
1619 chanflags = (c->ic_flags
1620 &~ (IEEE80211_CHAN_HT | IEEE80211_CHAN_VHT))
1622 IEEE80211_NOTE(ni->ni_vap,
1623 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N,
1625 "%s: VHT; chanwidth=0x%02x; vhtflags=0x%08x",
1626 __func__, ni->ni_vht_chanwidth, vhtflags);
1628 IEEE80211_NOTE(ni->ni_vap,
1629 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N,
1631 "%s: VHT; trying lookup for %d/0x%08x",
1632 __func__, c->ic_freq, chanflags);
1633 c = ieee80211_find_channel(ic, c->ic_freq, chanflags);
1636 /* Finally, if it's changed */
1637 if (c != NULL && c != ni->ni_chan) {
1638 IEEE80211_NOTE(ni->ni_vap,
1639 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
1640 "switch station to %s%d channel %u/0x%x",
1641 IEEE80211_IS_CHAN_VHT(c) ? "VHT" : "HT",
1642 IEEE80211_IS_CHAN_VHT80(c) ? 80 :
1643 (IEEE80211_IS_CHAN_HT40(c) ? 40 : 20),
1644 c->ic_freq, c->ic_flags);
1648 /* NB: caller responsible for forcing any channel change */
1651 /* update node's (11n) tx channel width */
1652 ni->ni_chw = IEEE80211_IS_CHAN_HT40(ni->ni_chan)? 40 : 20;
1657 * Update 11n MIMO PS state according to received htcap.
1660 htcap_update_mimo_ps(struct ieee80211_node *ni)
1662 uint16_t oflags = ni->ni_flags;
1664 switch (ni->ni_htcap & IEEE80211_HTCAP_SMPS) {
1665 case IEEE80211_HTCAP_SMPS_DYNAMIC:
1666 ni->ni_flags |= IEEE80211_NODE_MIMO_PS;
1667 ni->ni_flags |= IEEE80211_NODE_MIMO_RTS;
1669 case IEEE80211_HTCAP_SMPS_ENA:
1670 ni->ni_flags |= IEEE80211_NODE_MIMO_PS;
1671 ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS;
1673 case IEEE80211_HTCAP_SMPS_OFF:
1674 default: /* disable on rx of reserved value */
1675 ni->ni_flags &= ~IEEE80211_NODE_MIMO_PS;
1676 ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS;
1679 return (oflags ^ ni->ni_flags);
1683 * Update short GI state according to received htcap
1684 * and local settings.
1686 static __inline void
1687 htcap_update_shortgi(struct ieee80211_node *ni)
1689 struct ieee80211vap *vap = ni->ni_vap;
1691 ni->ni_flags &= ~(IEEE80211_NODE_SGI20|IEEE80211_NODE_SGI40);
1692 if ((ni->ni_htcap & IEEE80211_HTCAP_SHORTGI20) &&
1693 (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20))
1694 ni->ni_flags |= IEEE80211_NODE_SGI20;
1695 if ((ni->ni_htcap & IEEE80211_HTCAP_SHORTGI40) &&
1696 (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40))
1697 ni->ni_flags |= IEEE80211_NODE_SGI40;
1701 * Update LDPC state according to received htcap
1702 * and local settings.
1704 static __inline void
1705 htcap_update_ldpc(struct ieee80211_node *ni)
1707 struct ieee80211vap *vap = ni->ni_vap;
1709 if ((ni->ni_htcap & IEEE80211_HTCAP_LDPC) &&
1710 (vap->iv_flags_ht & IEEE80211_FHT_LDPC_TX))
1711 ni->ni_flags |= IEEE80211_NODE_LDPC;
1715 * Parse and update HT-related state extracted from
1716 * the HT cap and info ie's.
1718 * This is called from the STA management path and
1719 * the ieee80211_node_join() path. It will take into
1720 * account the IEs discovered during scanning and
1721 * adjust things accordingly.
1724 ieee80211_ht_updateparams(struct ieee80211_node *ni,
1725 const uint8_t *htcapie, const uint8_t *htinfoie)
1727 struct ieee80211vap *vap = ni->ni_vap;
1728 const struct ieee80211_ie_htinfo *htinfo;
1730 ieee80211_parse_htcap(ni, htcapie);
1731 if (vap->iv_htcaps & IEEE80211_HTCAP_SMPS)
1732 htcap_update_mimo_ps(ni);
1733 htcap_update_shortgi(ni);
1734 htcap_update_ldpc(ni);
1736 if (htinfoie[0] == IEEE80211_ELEMID_VENDOR)
1738 htinfo = (const struct ieee80211_ie_htinfo *) htinfoie;
1739 htinfo_parse(ni, htinfo);
1742 * Defer the node channel change; we need to now
1743 * update VHT parameters before we do it.
1746 if ((htinfo->hi_byte1 & IEEE80211_HTINFO_RIFSMODE_PERM) &&
1747 (vap->iv_flags_ht & IEEE80211_FHT_RIFS))
1748 ni->ni_flags |= IEEE80211_NODE_RIFS;
1750 ni->ni_flags &= ~IEEE80211_NODE_RIFS;
1754 ieee80211_vht_get_vhtflags(struct ieee80211_node *ni, uint32_t htflags)
1756 struct ieee80211vap *vap = ni->ni_vap;
1757 uint32_t vhtflags = 0;
1760 if (ni->ni_flags & IEEE80211_NODE_VHT && vap->iv_flags_vht & IEEE80211_FVHT_VHT) {
1761 if ((ni->ni_vht_chanwidth == IEEE80211_VHT_CHANWIDTH_160MHZ) &&
1762 /* XXX 2 means "160MHz and 80+80MHz", 1 means "160MHz" */
1763 (MS(vap->iv_vhtcaps,
1764 IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_MASK) >= 1) &&
1765 (vap->iv_flags_vht & IEEE80211_FVHT_USEVHT160)) {
1766 vhtflags = IEEE80211_CHAN_VHT160;
1767 /* Mirror the HT40 flags */
1768 if (htflags == IEEE80211_CHAN_HT40U) {
1769 vhtflags |= IEEE80211_CHAN_HT40U;
1770 } else if (htflags == IEEE80211_CHAN_HT40D) {
1771 vhtflags |= IEEE80211_CHAN_HT40D;
1773 } else if ((ni->ni_vht_chanwidth == IEEE80211_VHT_CHANWIDTH_80P80MHZ) &&
1774 /* XXX 2 means "160MHz and 80+80MHz" */
1775 (MS(vap->iv_vhtcaps,
1776 IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_MASK) == 2) &&
1777 (vap->iv_flags_vht & IEEE80211_FVHT_USEVHT80P80)) {
1778 vhtflags = IEEE80211_CHAN_VHT80_80;
1779 /* Mirror the HT40 flags */
1780 if (htflags == IEEE80211_CHAN_HT40U) {
1781 vhtflags |= IEEE80211_CHAN_HT40U;
1782 } else if (htflags == IEEE80211_CHAN_HT40D) {
1783 vhtflags |= IEEE80211_CHAN_HT40D;
1785 } else if ((ni->ni_vht_chanwidth == IEEE80211_VHT_CHANWIDTH_80MHZ) &&
1786 (vap->iv_flags_vht & IEEE80211_FVHT_USEVHT80)) {
1787 vhtflags = IEEE80211_CHAN_VHT80;
1788 /* Mirror the HT40 flags */
1789 if (htflags == IEEE80211_CHAN_HT40U) {
1790 vhtflags |= IEEE80211_CHAN_HT40U;
1791 } else if (htflags == IEEE80211_CHAN_HT40D) {
1792 vhtflags |= IEEE80211_CHAN_HT40D;
1794 } else if (ni->ni_vht_chanwidth == IEEE80211_VHT_CHANWIDTH_USE_HT) {
1795 /* Mirror the HT40 flags */
1797 * XXX TODO: if ht40 is disabled, but vht40 isn't
1798 * disabled then this logic will get very, very sad.
1799 * It's quite possible the only sane thing to do is
1800 * to not have vht40 as an option, and just obey
1801 * 'ht40' as that flag.
1803 if ((htflags == IEEE80211_CHAN_HT40U) &&
1804 (vap->iv_flags_vht & IEEE80211_FVHT_USEVHT40)) {
1805 vhtflags = IEEE80211_CHAN_VHT40U
1806 | IEEE80211_CHAN_HT40U;
1807 } else if (htflags == IEEE80211_CHAN_HT40D &&
1808 (vap->iv_flags_vht & IEEE80211_FVHT_USEVHT40)) {
1809 vhtflags = IEEE80211_CHAN_VHT40D
1810 | IEEE80211_CHAN_HT40D;
1811 } else if (htflags == IEEE80211_CHAN_HT20) {
1812 vhtflags = IEEE80211_CHAN_VHT20
1813 | IEEE80211_CHAN_HT20;
1816 vhtflags = IEEE80211_CHAN_VHT20;
1823 * Final part of updating the HT parameters.
1825 * This is called from the STA management path and
1826 * the ieee80211_node_join() path. It will take into
1827 * account the IEs discovered during scanning and
1828 * adjust things accordingly.
1830 * This is done after a call to ieee80211_ht_updateparams()
1831 * because it (and the upcoming VHT version of updateparams)
1832 * needs to ensure everything is parsed before htinfo_update_chw()
1833 * is called - which will change the channel config for the
1837 ieee80211_ht_updateparams_final(struct ieee80211_node *ni,
1838 const uint8_t *htcapie, const uint8_t *htinfoie)
1840 struct ieee80211vap *vap = ni->ni_vap;
1841 const struct ieee80211_ie_htinfo *htinfo;
1842 int htflags, vhtflags;
1845 htinfo = (const struct ieee80211_ie_htinfo *) htinfoie;
1847 htflags = (vap->iv_flags_ht & IEEE80211_FHT_HT) ?
1848 IEEE80211_CHAN_HT20 : 0;
1850 /* NB: honor operating mode constraint */
1851 if ((htinfo->hi_byte1 & IEEE80211_HTINFO_TXWIDTH_2040) &&
1852 (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)) {
1853 if (ni->ni_ht2ndchan == IEEE80211_HTINFO_2NDCHAN_ABOVE)
1854 htflags = IEEE80211_CHAN_HT40U;
1855 else if (ni->ni_ht2ndchan == IEEE80211_HTINFO_2NDCHAN_BELOW)
1856 htflags = IEEE80211_CHAN_HT40D;
1860 * VHT flags - do much the same; check whether VHT is available
1861 * and if so, what our ideal channel use would be based on our
1862 * capabilities and the (pre-parsed) VHT info IE.
1864 vhtflags = ieee80211_vht_get_vhtflags(ni, htflags);
1866 if (htinfo_update_chw(ni, htflags, vhtflags))
1873 * Parse and update HT-related state extracted from the HT cap ie
1874 * for a station joining an HT BSS.
1876 * This is called from the hostap path for each station.
1879 ieee80211_ht_updatehtcap(struct ieee80211_node *ni, const uint8_t *htcapie)
1881 struct ieee80211vap *vap = ni->ni_vap;
1883 ieee80211_parse_htcap(ni, htcapie);
1884 if (vap->iv_htcaps & IEEE80211_HTCAP_SMPS)
1885 htcap_update_mimo_ps(ni);
1886 htcap_update_shortgi(ni);
1887 htcap_update_ldpc(ni);
1891 * Called once HT and VHT capabilities are parsed in hostap mode -
1892 * this will adjust the channel configuration of the given node
1893 * based on the configuration and capabilities.
1896 ieee80211_ht_updatehtcap_final(struct ieee80211_node *ni)
1898 struct ieee80211vap *vap = ni->ni_vap;
1902 /* NB: honor operating mode constraint */
1903 /* XXX 40 MHz intolerant */
1904 htflags = (vap->iv_flags_ht & IEEE80211_FHT_HT) ?
1905 IEEE80211_CHAN_HT20 : 0;
1906 if ((ni->ni_htcap & IEEE80211_HTCAP_CHWIDTH40) &&
1907 (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)) {
1908 if (IEEE80211_IS_CHAN_HT40U(vap->iv_bss->ni_chan))
1909 htflags = IEEE80211_CHAN_HT40U;
1910 else if (IEEE80211_IS_CHAN_HT40D(vap->iv_bss->ni_chan))
1911 htflags = IEEE80211_CHAN_HT40D;
1914 * VHT flags - do much the same; check whether VHT is available
1915 * and if so, what our ideal channel use would be based on our
1916 * capabilities and the (pre-parsed) VHT info IE.
1918 vhtflags = ieee80211_vht_get_vhtflags(ni, htflags);
1920 (void) htinfo_update_chw(ni, htflags, vhtflags);
1924 * Install received HT rate set by parsing the HT cap ie.
1927 ieee80211_setup_htrates(struct ieee80211_node *ni, const uint8_t *ie, int flags)
1929 struct ieee80211com *ic = ni->ni_ic;
1930 struct ieee80211vap *vap = ni->ni_vap;
1931 const struct ieee80211_ie_htcap *htcap;
1932 struct ieee80211_htrateset *rs;
1933 int i, maxequalmcs, maxunequalmcs;
1935 maxequalmcs = ic->ic_txstream * 8 - 1;
1937 if (ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL) {
1938 if (ic->ic_txstream >= 2)
1940 if (ic->ic_txstream >= 3)
1942 if (ic->ic_txstream >= 4)
1946 rs = &ni->ni_htrates;
1947 memset(rs, 0, sizeof(*rs));
1949 if (ie[0] == IEEE80211_ELEMID_VENDOR)
1951 htcap = (const struct ieee80211_ie_htcap *) ie;
1952 for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++) {
1953 if (isclr(htcap->hc_mcsset, i))
1955 if (rs->rs_nrates == IEEE80211_HTRATE_MAXSIZE) {
1957 IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
1958 "WARNING, HT rate set too large; only "
1959 "using %u rates", IEEE80211_HTRATE_MAXSIZE);
1960 vap->iv_stats.is_rx_rstoobig++;
1963 if (i <= 31 && i > maxequalmcs)
1966 (ic->ic_htcaps & IEEE80211_HTC_TXMCS32) == 0)
1968 if (i > 32 && i > maxunequalmcs)
1970 rs->rs_rates[rs->rs_nrates++] = i;
1973 return ieee80211_fix_rate(ni, (struct ieee80211_rateset *) rs, flags);
1977 * Mark rates in a node's HT rate set as basic according
1978 * to the information in the supplied HT info ie.
1981 ieee80211_setup_basic_htrates(struct ieee80211_node *ni, const uint8_t *ie)
1983 const struct ieee80211_ie_htinfo *htinfo;
1984 struct ieee80211_htrateset *rs;
1987 if (ie[0] == IEEE80211_ELEMID_VENDOR)
1989 htinfo = (const struct ieee80211_ie_htinfo *) ie;
1990 rs = &ni->ni_htrates;
1991 if (rs->rs_nrates == 0) {
1992 IEEE80211_NOTE(ni->ni_vap,
1993 IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
1994 "%s", "WARNING, empty HT rate set");
1997 for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++) {
1998 if (isclr(htinfo->hi_basicmcsset, i))
2000 for (j = 0; j < rs->rs_nrates; j++)
2001 if ((rs->rs_rates[j] & IEEE80211_RATE_VAL) == i)
2002 rs->rs_rates[j] |= IEEE80211_RATE_BASIC;
2007 ampdu_tx_setup(struct ieee80211_tx_ampdu *tap)
2009 callout_init(&tap->txa_timer, 1);
2010 tap->txa_flags |= IEEE80211_AGGR_SETUP;
2011 tap->txa_lastsample = ticks;
2015 ampdu_tx_stop(struct ieee80211_tx_ampdu *tap)
2017 struct ieee80211_node *ni = tap->txa_ni;
2018 struct ieee80211com *ic = ni->ni_ic;
2020 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2025 KASSERT(tap->txa_flags & IEEE80211_AGGR_SETUP,
2026 ("txa_flags 0x%x tid %d ac %d", tap->txa_flags, tap->txa_tid,
2027 TID_TO_WME_AC(tap->txa_tid)));
2030 * Stop BA stream if setup so driver has a chance
2031 * to reclaim any resources it might have allocated.
2033 ic->ic_addba_stop(ni, tap);
2035 * Stop any pending BAR transmit.
2037 bar_stop_timer(tap);
2040 * Reset packet estimate.
2042 ieee80211_txampdu_init_pps(tap);
2044 /* NB: clearing NAK means we may re-send ADDBA */
2045 tap->txa_flags &= ~(IEEE80211_AGGR_SETUP | IEEE80211_AGGR_NAK);
2049 * ADDBA response timeout.
2051 * If software aggregation and per-TID queue management was done here,
2052 * that queue would be unpaused after the ADDBA timeout occurs.
2055 addba_timeout(void *arg)
2057 struct ieee80211_tx_ampdu *tap = arg;
2058 struct ieee80211_node *ni = tap->txa_ni;
2059 struct ieee80211com *ic = ni->ni_ic;
2062 tap->txa_flags &= ~IEEE80211_AGGR_XCHGPEND;
2063 tap->txa_attempts++;
2064 ic->ic_addba_response_timeout(ni, tap);
2068 addba_start_timeout(struct ieee80211_tx_ampdu *tap)
2070 /* XXX use CALLOUT_PENDING instead? */
2071 callout_reset(&tap->txa_timer, ieee80211_addba_timeout,
2072 addba_timeout, tap);
2073 tap->txa_flags |= IEEE80211_AGGR_XCHGPEND;
2074 tap->txa_nextrequest = ticks + ieee80211_addba_timeout;
2078 addba_stop_timeout(struct ieee80211_tx_ampdu *tap)
2080 /* XXX use CALLOUT_PENDING instead? */
2081 if (tap->txa_flags & IEEE80211_AGGR_XCHGPEND) {
2082 callout_stop(&tap->txa_timer);
2083 tap->txa_flags &= ~IEEE80211_AGGR_XCHGPEND;
2088 null_addba_response_timeout(struct ieee80211_node *ni,
2089 struct ieee80211_tx_ampdu *tap)
2094 * Default method for requesting A-MPDU tx aggregation.
2095 * We setup the specified state block and start a timer
2096 * to wait for an ADDBA response frame.
2099 ieee80211_addba_request(struct ieee80211_node *ni,
2100 struct ieee80211_tx_ampdu *tap,
2101 int dialogtoken, int baparamset, int batimeout)
2106 tap->txa_token = dialogtoken;
2107 tap->txa_flags |= IEEE80211_AGGR_IMMEDIATE;
2108 bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
2109 tap->txa_wnd = (bufsiz == 0) ?
2110 IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
2111 addba_start_timeout(tap);
2116 * Called by drivers that wish to request an ADDBA session be
2117 * setup. This brings it up and starts the request timer.
2120 ieee80211_ampdu_tx_request_ext(struct ieee80211_node *ni, int tid)
2122 struct ieee80211_tx_ampdu *tap;
2124 if (tid < 0 || tid > 15)
2126 tap = &ni->ni_tx_ampdu[tid];
2129 if ((tap->txa_flags & IEEE80211_AGGR_SETUP) == 0) {
2130 /* do deferred setup of state */
2131 ampdu_tx_setup(tap);
2133 /* XXX hack for not doing proper locking */
2134 tap->txa_flags &= ~IEEE80211_AGGR_NAK;
2135 addba_start_timeout(tap);
2140 * Called by drivers that have marked a session as active.
2143 ieee80211_ampdu_tx_request_active_ext(struct ieee80211_node *ni, int tid,
2146 struct ieee80211_tx_ampdu *tap;
2148 if (tid < 0 || tid > 15)
2150 tap = &ni->ni_tx_ampdu[tid];
2153 addba_stop_timeout(tap);
2155 tap->txa_flags |= IEEE80211_AGGR_RUNNING;
2156 tap->txa_attempts = 0;
2158 /* mark tid so we don't try again */
2159 tap->txa_flags |= IEEE80211_AGGR_NAK;
2165 * Default method for processing an A-MPDU tx aggregation
2166 * response. We shutdown any pending timer and update the
2167 * state block according to the reply.
2170 ieee80211_addba_response(struct ieee80211_node *ni,
2171 struct ieee80211_tx_ampdu *tap,
2172 int status, int baparamset, int batimeout)
2177 addba_stop_timeout(tap);
2178 if (status == IEEE80211_STATUS_SUCCESS) {
2179 bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
2180 /* XXX override our request? */
2181 tap->txa_wnd = (bufsiz == 0) ?
2182 IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
2184 tid = MS(baparamset, IEEE80211_BAPS_TID);
2185 tap->txa_flags |= IEEE80211_AGGR_RUNNING;
2186 tap->txa_attempts = 0;
2188 /* mark tid so we don't try again */
2189 tap->txa_flags |= IEEE80211_AGGR_NAK;
2195 * Default method for stopping A-MPDU tx aggregation.
2196 * Any timer is cleared and we drain any pending frames.
2199 ieee80211_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
2202 addba_stop_timeout(tap);
2203 if (tap->txa_flags & IEEE80211_AGGR_RUNNING) {
2204 /* XXX clear aggregation queue */
2205 tap->txa_flags &= ~IEEE80211_AGGR_RUNNING;
2207 tap->txa_attempts = 0;
2211 * Process a received action frame using the default aggregation
2212 * policy. We intercept ADDBA-related frames and use them to
2213 * update our aggregation state. All other frames are passed up
2214 * for processing by ieee80211_recv_action.
2217 ht_recv_action_ba_addba_request(struct ieee80211_node *ni,
2218 const struct ieee80211_frame *wh,
2219 const uint8_t *frm, const uint8_t *efrm)
2221 struct ieee80211com *ic = ni->ni_ic;
2222 struct ieee80211vap *vap = ni->ni_vap;
2223 struct ieee80211_rx_ampdu *rap;
2224 uint8_t dialogtoken;
2225 uint16_t baparamset, batimeout, baseqctl;
2229 dialogtoken = frm[2];
2230 baparamset = le16dec(frm+3);
2231 batimeout = le16dec(frm+5);
2232 baseqctl = le16dec(frm+7);
2234 tid = MS(baparamset, IEEE80211_BAPS_TID);
2236 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2237 "recv ADDBA request: dialogtoken %u baparamset 0x%x "
2238 "(tid %d bufsiz %d) batimeout %d baseqctl %d:%d",
2239 dialogtoken, baparamset,
2240 tid, MS(baparamset, IEEE80211_BAPS_BUFSIZ),
2242 MS(baseqctl, IEEE80211_BASEQ_START),
2243 MS(baseqctl, IEEE80211_BASEQ_FRAG));
2245 rap = &ni->ni_rx_ampdu[tid];
2247 /* Send ADDBA response */
2248 args[0] = dialogtoken;
2250 * NB: We ack only if the sta associated with HT and
2251 * the ap is configured to do AMPDU rx (the latter
2252 * violates the 11n spec and is mostly for testing).
2254 if ((ni->ni_flags & IEEE80211_NODE_AMPDU_RX) &&
2255 (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_RX)) {
2256 /* XXX handle ampdu_rx_start failure */
2257 ic->ic_ampdu_rx_start(ni, rap,
2258 baparamset, batimeout, baseqctl);
2260 args[1] = IEEE80211_STATUS_SUCCESS;
2262 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2263 ni, "reject ADDBA request: %s",
2264 ni->ni_flags & IEEE80211_NODE_AMPDU_RX ?
2265 "administratively disabled" :
2266 "not negotiated for station");
2267 vap->iv_stats.is_addba_reject++;
2268 args[1] = IEEE80211_STATUS_UNSPECIFIED;
2270 /* XXX honor rap flags? */
2271 args[2] = IEEE80211_BAPS_POLICY_IMMEDIATE
2272 | SM(tid, IEEE80211_BAPS_TID)
2273 | SM(rap->rxa_wnd, IEEE80211_BAPS_BUFSIZ)
2277 ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
2278 IEEE80211_ACTION_BA_ADDBA_RESPONSE, args);
2283 ht_recv_action_ba_addba_response(struct ieee80211_node *ni,
2284 const struct ieee80211_frame *wh,
2285 const uint8_t *frm, const uint8_t *efrm)
2287 struct ieee80211com *ic = ni->ni_ic;
2288 struct ieee80211vap *vap = ni->ni_vap;
2289 struct ieee80211_tx_ampdu *tap;
2290 uint8_t dialogtoken, policy;
2291 uint16_t baparamset, batimeout, code;
2294 dialogtoken = frm[2];
2295 code = le16dec(frm+3);
2296 baparamset = le16dec(frm+5);
2297 tid = MS(baparamset, IEEE80211_BAPS_TID);
2298 bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
2299 policy = MS(baparamset, IEEE80211_BAPS_POLICY);
2300 batimeout = le16dec(frm+7);
2302 tap = &ni->ni_tx_ampdu[tid];
2303 if ((tap->txa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
2304 IEEE80211_DISCARD_MAC(vap,
2305 IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2306 ni->ni_macaddr, "ADDBA response",
2307 "no pending ADDBA, tid %d dialogtoken %u "
2308 "code %d", tid, dialogtoken, code);
2309 vap->iv_stats.is_addba_norequest++;
2312 if (dialogtoken != tap->txa_token) {
2313 IEEE80211_DISCARD_MAC(vap,
2314 IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2315 ni->ni_macaddr, "ADDBA response",
2316 "dialogtoken mismatch: waiting for %d, "
2317 "received %d, tid %d code %d",
2318 tap->txa_token, dialogtoken, tid, code);
2319 vap->iv_stats.is_addba_badtoken++;
2322 /* NB: assumes IEEE80211_AGGR_IMMEDIATE is 1 */
2323 if (policy != (tap->txa_flags & IEEE80211_AGGR_IMMEDIATE)) {
2324 IEEE80211_DISCARD_MAC(vap,
2325 IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2326 ni->ni_macaddr, "ADDBA response",
2327 "policy mismatch: expecting %s, "
2328 "received %s, tid %d code %d",
2329 tap->txa_flags & IEEE80211_AGGR_IMMEDIATE,
2331 vap->iv_stats.is_addba_badpolicy++;
2335 /* XXX we take MIN in ieee80211_addba_response */
2336 if (bufsiz > IEEE80211_AGGR_BAWMAX) {
2337 IEEE80211_DISCARD_MAC(vap,
2338 IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2339 ni->ni_macaddr, "ADDBA response",
2340 "BA window too large: max %d, "
2341 "received %d, tid %d code %d",
2342 bufsiz, IEEE80211_AGGR_BAWMAX, tid, code);
2343 vap->iv_stats.is_addba_badbawinsize++;
2347 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2348 "recv ADDBA response: dialogtoken %u code %d "
2349 "baparamset 0x%x (tid %d bufsiz %d) batimeout %d",
2350 dialogtoken, code, baparamset, tid, bufsiz,
2352 ic->ic_addba_response(ni, tap, code, baparamset, batimeout);
2357 ht_recv_action_ba_delba(struct ieee80211_node *ni,
2358 const struct ieee80211_frame *wh,
2359 const uint8_t *frm, const uint8_t *efrm)
2361 struct ieee80211com *ic = ni->ni_ic;
2362 struct ieee80211_rx_ampdu *rap;
2363 struct ieee80211_tx_ampdu *tap;
2364 uint16_t baparamset, code;
2367 baparamset = le16dec(frm+2);
2368 code = le16dec(frm+4);
2370 tid = MS(baparamset, IEEE80211_DELBAPS_TID);
2372 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2373 "recv DELBA: baparamset 0x%x (tid %d initiator %d) "
2374 "code %d", baparamset, tid,
2375 MS(baparamset, IEEE80211_DELBAPS_INIT), code);
2377 if ((baparamset & IEEE80211_DELBAPS_INIT) == 0) {
2378 tap = &ni->ni_tx_ampdu[tid];
2379 ic->ic_addba_stop(ni, tap);
2381 rap = &ni->ni_rx_ampdu[tid];
2382 ic->ic_ampdu_rx_stop(ni, rap);
2388 ht_recv_action_ht_txchwidth(struct ieee80211_node *ni,
2389 const struct ieee80211_frame *wh,
2390 const uint8_t *frm, const uint8_t *efrm)
2394 chw = (frm[2] == IEEE80211_A_HT_TXCHWIDTH_2040) ? 40 : 20;
2396 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2397 "%s: HT txchwidth, width %d%s",
2398 __func__, chw, ni->ni_chw != chw ? "*" : "");
2399 if (chw != ni->ni_chw) {
2400 /* XXX does this need to change the ht40 station count? */
2402 /* XXX notify on change */
2408 ht_recv_action_ht_mimopwrsave(struct ieee80211_node *ni,
2409 const struct ieee80211_frame *wh,
2410 const uint8_t *frm, const uint8_t *efrm)
2412 const struct ieee80211_action_ht_mimopowersave *mps =
2413 (const struct ieee80211_action_ht_mimopowersave *) frm;
2415 /* XXX check iv_htcaps */
2416 if (mps->am_control & IEEE80211_A_HT_MIMOPWRSAVE_ENA)
2417 ni->ni_flags |= IEEE80211_NODE_MIMO_PS;
2419 ni->ni_flags &= ~IEEE80211_NODE_MIMO_PS;
2420 if (mps->am_control & IEEE80211_A_HT_MIMOPWRSAVE_MODE)
2421 ni->ni_flags |= IEEE80211_NODE_MIMO_RTS;
2423 ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS;
2424 /* XXX notify on change */
2425 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2426 "%s: HT MIMO PS (%s%s)", __func__,
2427 (ni->ni_flags & IEEE80211_NODE_MIMO_PS) ? "on" : "off",
2428 (ni->ni_flags & IEEE80211_NODE_MIMO_RTS) ? "+rts" : ""
2434 * Transmit processing.
2438 * Check if A-MPDU should be requested/enabled for a stream.
2439 * We require a traffic rate above a per-AC threshold and we
2440 * also handle backoff from previous failed attempts.
2442 * Drivers may override this method to bring in information
2443 * such as link state conditions in making the decision.
2446 ieee80211_ampdu_enable(struct ieee80211_node *ni,
2447 struct ieee80211_tx_ampdu *tap)
2449 struct ieee80211vap *vap = ni->ni_vap;
2451 if (tap->txa_avgpps <
2452 vap->iv_ampdu_mintraffic[TID_TO_WME_AC(tap->txa_tid)])
2454 /* XXX check rssi? */
2455 if (tap->txa_attempts >= ieee80211_addba_maxtries &&
2456 ieee80211_time_after(ticks, tap->txa_nextrequest)) {
2458 * Don't retry too often; txa_nextrequest is set
2459 * to the minimum interval we'll retry after
2460 * ieee80211_addba_maxtries failed attempts are made.
2464 IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
2465 "enable AMPDU on tid %d (%s), avgpps %d pkts %d attempt %d",
2466 tap->txa_tid, ieee80211_wme_acnames[TID_TO_WME_AC(tap->txa_tid)],
2467 tap->txa_avgpps, tap->txa_pkts, tap->txa_attempts);
2472 * Request A-MPDU tx aggregation. Setup local state and
2473 * issue an ADDBA request. BA use will only happen after
2474 * the other end replies with ADDBA response.
2477 ieee80211_ampdu_request(struct ieee80211_node *ni,
2478 struct ieee80211_tx_ampdu *tap)
2480 struct ieee80211com *ic = ni->ni_ic;
2482 int tid, dialogtoken;
2483 static int tokens = 0; /* XXX */
2486 if ((tap->txa_flags & IEEE80211_AGGR_SETUP) == 0) {
2487 /* do deferred setup of state */
2488 ampdu_tx_setup(tap);
2490 /* XXX hack for not doing proper locking */
2491 tap->txa_flags &= ~IEEE80211_AGGR_NAK;
2493 dialogtoken = (tokens+1) % 63; /* XXX */
2497 * XXX TODO: This is racy with any other parallel TX going on. :(
2499 tap->txa_start = ni->ni_txseqs[tid];
2501 args[0] = dialogtoken;
2502 args[1] = 0; /* NB: status code not used */
2503 args[2] = IEEE80211_BAPS_POLICY_IMMEDIATE
2504 | SM(tid, IEEE80211_BAPS_TID)
2505 | SM(IEEE80211_AGGR_BAWMAX, IEEE80211_BAPS_BUFSIZ)
2507 args[3] = 0; /* batimeout */
2508 /* NB: do first so there's no race against reply */
2509 if (!ic->ic_addba_request(ni, tap, dialogtoken, args[2], args[3])) {
2510 /* unable to setup state, don't make request */
2511 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2512 ni, "%s: could not setup BA stream for TID %d AC %d",
2513 __func__, tap->txa_tid, TID_TO_WME_AC(tap->txa_tid));
2514 /* defer next try so we don't slam the driver with requests */
2515 tap->txa_attempts = ieee80211_addba_maxtries;
2516 /* NB: check in case driver wants to override */
2517 if (tap->txa_nextrequest <= ticks)
2518 tap->txa_nextrequest = ticks + ieee80211_addba_backoff;
2521 tokens = dialogtoken; /* allocate token */
2522 /* NB: after calling ic_addba_request so driver can set txa_start */
2523 args[4] = SM(tap->txa_start, IEEE80211_BASEQ_START)
2524 | SM(0, IEEE80211_BASEQ_FRAG)
2526 return ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
2527 IEEE80211_ACTION_BA_ADDBA_REQUEST, args);
2531 * Terminate an AMPDU tx stream. State is reclaimed
2532 * and the peer notified with a DelBA Action frame.
2535 ieee80211_ampdu_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
2538 struct ieee80211com *ic = ni->ni_ic;
2539 struct ieee80211vap *vap = ni->ni_vap;
2543 tap->txa_flags &= ~IEEE80211_AGGR_BARPEND;
2544 if (IEEE80211_AMPDU_RUNNING(tap)) {
2545 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2546 ni, "%s: stop BA stream for TID %d (reason: %d (%s))",
2547 __func__, tap->txa_tid, reason,
2548 ieee80211_reason_to_string(reason));
2549 vap->iv_stats.is_ampdu_stop++;
2551 ic->ic_addba_stop(ni, tap);
2552 args[0] = tap->txa_tid;
2553 args[1] = IEEE80211_DELBAPS_INIT;
2554 args[2] = reason; /* XXX reason code */
2555 ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
2556 IEEE80211_ACTION_BA_DELBA, args);
2558 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2559 ni, "%s: BA stream for TID %d not running "
2560 "(reason: %d (%s))", __func__, tap->txa_tid, reason,
2561 ieee80211_reason_to_string(reason));
2562 vap->iv_stats.is_ampdu_stop_failed++;
2567 static void bar_start_timer(struct ieee80211_tx_ampdu *tap);
2570 bar_timeout(void *arg)
2572 struct ieee80211_tx_ampdu *tap = arg;
2573 struct ieee80211_node *ni = tap->txa_ni;
2575 KASSERT((tap->txa_flags & IEEE80211_AGGR_XCHGPEND) == 0,
2576 ("bar/addba collision, flags 0x%x", tap->txa_flags));
2578 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2579 ni, "%s: tid %u flags 0x%x attempts %d", __func__,
2580 tap->txa_tid, tap->txa_flags, tap->txa_attempts);
2582 /* guard against race with bar_tx_complete */
2583 if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) == 0)
2586 if (tap->txa_attempts >= ieee80211_bar_maxtries) {
2587 struct ieee80211com *ic = ni->ni_ic;
2589 ni->ni_vap->iv_stats.is_ampdu_bar_tx_fail++;
2591 * If (at least) the last BAR TX timeout was due to
2592 * an ieee80211_send_bar() failures, then we need
2593 * to make sure we notify the driver that a BAR
2594 * TX did occur and fail. This gives the driver
2595 * a chance to undo any queue pause that may
2598 ic->ic_bar_response(ni, tap, 1);
2599 ieee80211_ampdu_stop(ni, tap, IEEE80211_REASON_TIMEOUT);
2601 ni->ni_vap->iv_stats.is_ampdu_bar_tx_retry++;
2602 if (ieee80211_send_bar(ni, tap, tap->txa_seqpending) != 0) {
2603 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2604 ni, "%s: failed to TX, starting timer\n",
2607 * If ieee80211_send_bar() fails here, the
2608 * timer may have stopped and/or the pending
2609 * flag may be clear. Because of this,
2610 * fake the BARPEND and reset the timer.
2611 * A retransmission attempt will then occur
2612 * during the next timeout.
2615 tap->txa_flags |= IEEE80211_AGGR_BARPEND;
2616 bar_start_timer(tap);
2622 bar_start_timer(struct ieee80211_tx_ampdu *tap)
2624 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2628 callout_reset(&tap->txa_timer, ieee80211_bar_timeout, bar_timeout, tap);
2632 bar_stop_timer(struct ieee80211_tx_ampdu *tap)
2634 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2638 callout_stop(&tap->txa_timer);
2642 bar_tx_complete(struct ieee80211_node *ni, void *arg, int status)
2644 struct ieee80211_tx_ampdu *tap = arg;
2646 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2647 ni, "%s: tid %u flags 0x%x pending %d status %d",
2648 __func__, tap->txa_tid, tap->txa_flags,
2649 callout_pending(&tap->txa_timer), status);
2651 ni->ni_vap->iv_stats.is_ampdu_bar_tx++;
2653 if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) &&
2654 callout_pending(&tap->txa_timer)) {
2655 struct ieee80211com *ic = ni->ni_ic;
2657 if (status == 0) /* ACK'd */
2658 bar_stop_timer(tap);
2659 ic->ic_bar_response(ni, tap, status);
2660 /* NB: just let timer expire so we pace requests */
2665 ieee80211_bar_response(struct ieee80211_node *ni,
2666 struct ieee80211_tx_ampdu *tap, int status)
2669 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2673 if (status == 0) { /* got ACK */
2674 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2675 ni, "BAR moves BA win <%u:%u> (%u frames) txseq %u tid %u",
2677 IEEE80211_SEQ_ADD(tap->txa_start, tap->txa_wnd-1),
2678 tap->txa_qframes, tap->txa_seqpending,
2681 /* NB: timer already stopped in bar_tx_complete */
2682 tap->txa_start = tap->txa_seqpending;
2683 tap->txa_flags &= ~IEEE80211_AGGR_BARPEND;
2688 * Transmit a BAR frame to the specified node. The
2689 * BAR contents are drawn from the supplied aggregation
2690 * state associated with the node.
2692 * NB: we only handle immediate ACK w/ compressed bitmap.
2695 ieee80211_send_bar(struct ieee80211_node *ni,
2696 struct ieee80211_tx_ampdu *tap, ieee80211_seq seq)
2698 #define senderr(_x, _v) do { vap->iv_stats._v++; ret = _x; goto bad; } while (0)
2699 struct ieee80211vap *vap = ni->ni_vap;
2700 struct ieee80211com *ic = ni->ni_ic;
2701 struct ieee80211_frame_bar *bar;
2703 uint16_t barctl, barseqctl;
2708 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2713 if ((tap->txa_flags & IEEE80211_AGGR_RUNNING) == 0) {
2714 /* no ADDBA response, should not happen */
2719 bar_stop_timer(tap);
2721 ieee80211_ref_node(ni);
2723 m = ieee80211_getmgtframe(&frm, ic->ic_headroom, sizeof(*bar));
2725 senderr(ENOMEM, is_tx_nobuf);
2727 if (!ieee80211_add_callback(m, bar_tx_complete, tap)) {
2729 senderr(ENOMEM, is_tx_nobuf); /* XXX */
2733 bar = mtod(m, struct ieee80211_frame_bar *);
2734 bar->i_fc[0] = IEEE80211_FC0_VERSION_0 |
2735 IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_BAR;
2737 IEEE80211_ADDR_COPY(bar->i_ra, ni->ni_macaddr);
2738 IEEE80211_ADDR_COPY(bar->i_ta, vap->iv_myaddr);
2741 barctl = (tap->txa_flags & IEEE80211_AGGR_IMMEDIATE ?
2742 0 : IEEE80211_BAR_NOACK)
2743 | IEEE80211_BAR_COMP
2744 | SM(tid, IEEE80211_BAR_TID)
2746 barseqctl = SM(seq, IEEE80211_BAR_SEQ_START);
2747 /* NB: known to have proper alignment */
2748 bar->i_ctl = htole16(barctl);
2749 bar->i_seq = htole16(barseqctl);
2750 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_bar);
2752 M_WME_SETAC(m, WME_AC_VO);
2754 IEEE80211_NODE_STAT(ni, tx_mgmt); /* XXX tx_ctl? */
2757 /* init/bump attempts counter */
2758 if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) == 0)
2759 tap->txa_attempts = 1;
2761 tap->txa_attempts++;
2762 tap->txa_seqpending = seq;
2763 tap->txa_flags |= IEEE80211_AGGR_BARPEND;
2765 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_11N,
2766 ni, "send BAR: tid %u ctl 0x%x start %u (attempt %d)",
2767 tid, barctl, seq, tap->txa_attempts);
2770 * ic_raw_xmit will free the node reference
2771 * regardless of queue/TX success or failure.
2773 IEEE80211_TX_LOCK(ic);
2774 ret = ieee80211_raw_output(vap, ni, m, NULL);
2775 IEEE80211_TX_UNLOCK(ic);
2777 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_11N,
2778 ni, "send BAR: failed: (ret = %d)\n",
2780 /* xmit failed, clear state flag */
2781 tap->txa_flags &= ~IEEE80211_AGGR_BARPEND;
2782 vap->iv_stats.is_ampdu_bar_tx_fail++;
2785 /* XXX hack against tx complete happening before timer is started */
2786 if (tap->txa_flags & IEEE80211_AGGR_BARPEND)
2787 bar_start_timer(tap);
2790 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2794 vap->iv_stats.is_ampdu_bar_tx_fail++;
2795 ieee80211_free_node(ni);
2801 ht_action_output(struct ieee80211_node *ni, struct mbuf *m)
2803 struct ieee80211_bpf_params params;
2805 memset(¶ms, 0, sizeof(params));
2806 params.ibp_pri = WME_AC_VO;
2807 params.ibp_rate0 = ni->ni_txparms->mgmtrate;
2808 /* NB: we know all frames are unicast */
2809 params.ibp_try0 = ni->ni_txparms->maxretry;
2810 params.ibp_power = ni->ni_txpower;
2811 return ieee80211_mgmt_output(ni, m, IEEE80211_FC0_SUBTYPE_ACTION,
2815 #define ADDSHORT(frm, v) do { \
2816 frm[0] = (v) & 0xff; \
2817 frm[1] = (v) >> 8; \
2822 * Send an action management frame. The arguments are stuff
2823 * into a frame without inspection; the caller is assumed to
2824 * prepare them carefully (e.g. based on the aggregation state).
2827 ht_send_action_ba_addba(struct ieee80211_node *ni,
2828 int category, int action, void *arg0)
2830 struct ieee80211vap *vap = ni->ni_vap;
2831 struct ieee80211com *ic = ni->ni_ic;
2832 uint16_t *args = arg0;
2836 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2837 "send ADDBA %s: dialogtoken %d status %d "
2838 "baparamset 0x%x (tid %d) batimeout 0x%x baseqctl 0x%x",
2839 (action == IEEE80211_ACTION_BA_ADDBA_REQUEST) ?
2840 "request" : "response",
2841 args[0], args[1], args[2], MS(args[2], IEEE80211_BAPS_TID),
2844 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2845 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2846 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2847 ieee80211_ref_node(ni);
2849 m = ieee80211_getmgtframe(&frm,
2850 ic->ic_headroom + sizeof(struct ieee80211_frame),
2851 sizeof(uint16_t) /* action+category */
2852 /* XXX may action payload */
2853 + sizeof(struct ieee80211_action_ba_addbaresponse)
2858 *frm++ = args[0]; /* dialog token */
2859 if (action == IEEE80211_ACTION_BA_ADDBA_RESPONSE)
2860 ADDSHORT(frm, args[1]); /* status code */
2861 ADDSHORT(frm, args[2]); /* baparamset */
2862 ADDSHORT(frm, args[3]); /* batimeout */
2863 if (action == IEEE80211_ACTION_BA_ADDBA_REQUEST)
2864 ADDSHORT(frm, args[4]); /* baseqctl */
2865 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2866 return ht_action_output(ni, m);
2868 vap->iv_stats.is_tx_nobuf++;
2869 ieee80211_free_node(ni);
2875 ht_send_action_ba_delba(struct ieee80211_node *ni,
2876 int category, int action, void *arg0)
2878 struct ieee80211vap *vap = ni->ni_vap;
2879 struct ieee80211com *ic = ni->ni_ic;
2880 uint16_t *args = arg0;
2882 uint16_t baparamset;
2885 baparamset = SM(args[0], IEEE80211_DELBAPS_TID)
2888 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2889 "send DELBA action: tid %d, initiator %d reason %d (%s)",
2890 args[0], args[1], args[2], ieee80211_reason_to_string(args[2]));
2892 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2893 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2894 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2895 ieee80211_ref_node(ni);
2897 m = ieee80211_getmgtframe(&frm,
2898 ic->ic_headroom + sizeof(struct ieee80211_frame),
2899 sizeof(uint16_t) /* action+category */
2900 /* XXX may action payload */
2901 + sizeof(struct ieee80211_action_ba_addbaresponse)
2906 ADDSHORT(frm, baparamset);
2907 ADDSHORT(frm, args[2]); /* reason code */
2908 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2909 return ht_action_output(ni, m);
2911 vap->iv_stats.is_tx_nobuf++;
2912 ieee80211_free_node(ni);
2918 ht_send_action_ht_txchwidth(struct ieee80211_node *ni,
2919 int category, int action, void *arg0)
2921 struct ieee80211vap *vap = ni->ni_vap;
2922 struct ieee80211com *ic = ni->ni_ic;
2926 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2927 "send HT txchwidth: width %d",
2928 IEEE80211_IS_CHAN_HT40(ni->ni_chan) ? 40 : 20);
2930 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2931 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2932 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2933 ieee80211_ref_node(ni);
2935 m = ieee80211_getmgtframe(&frm,
2936 ic->ic_headroom + sizeof(struct ieee80211_frame),
2937 sizeof(uint16_t) /* action+category */
2938 /* XXX may action payload */
2939 + sizeof(struct ieee80211_action_ba_addbaresponse)
2944 *frm++ = IEEE80211_IS_CHAN_HT40(ni->ni_chan) ?
2945 IEEE80211_A_HT_TXCHWIDTH_2040 :
2946 IEEE80211_A_HT_TXCHWIDTH_20;
2947 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2948 return ht_action_output(ni, m);
2950 vap->iv_stats.is_tx_nobuf++;
2951 ieee80211_free_node(ni);
2958 * Construct the MCS bit mask for inclusion in an HT capabilities
2959 * information element.
2962 ieee80211_set_mcsset(struct ieee80211com *ic, uint8_t *frm)
2967 KASSERT((ic->ic_rxstream > 0 && ic->ic_rxstream <= 4),
2968 ("ic_rxstream %d out of range", ic->ic_rxstream));
2969 KASSERT((ic->ic_txstream > 0 && ic->ic_txstream <= 4),
2970 ("ic_txstream %d out of range", ic->ic_txstream));
2972 for (i = 0; i < ic->ic_rxstream * 8; i++)
2974 if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
2975 (ic->ic_htcaps & IEEE80211_HTC_RXMCS32))
2977 if (ic->ic_htcaps & IEEE80211_HTC_RXUNEQUAL) {
2978 if (ic->ic_rxstream >= 2) {
2979 for (i = 33; i <= 38; i++)
2982 if (ic->ic_rxstream >= 3) {
2983 for (i = 39; i <= 52; i++)
2986 if (ic->ic_txstream >= 4) {
2987 for (i = 53; i <= 76; i++)
2992 if (ic->ic_rxstream != ic->ic_txstream) {
2993 txparams = 0x1; /* TX MCS set defined */
2994 txparams |= 0x2; /* TX RX MCS not equal */
2995 txparams |= (ic->ic_txstream - 1) << 2; /* num TX streams */
2996 if (ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL)
2997 txparams |= 0x16; /* TX unequal modulation sup */
3004 * Add body of an HTCAP information element.
3007 ieee80211_add_htcap_body(uint8_t *frm, struct ieee80211_node *ni)
3009 #define ADDSHORT(frm, v) do { \
3010 frm[0] = (v) & 0xff; \
3011 frm[1] = (v) >> 8; \
3014 struct ieee80211com *ic = ni->ni_ic;
3015 struct ieee80211vap *vap = ni->ni_vap;
3016 uint16_t caps, extcaps;
3019 /* HT capabilities */
3020 caps = vap->iv_htcaps & 0xffff;
3022 * Note channel width depends on whether we are operating as
3023 * a sta or not. When operating as a sta we are generating
3024 * a request based on our desired configuration. Otherwise
3025 * we are operational and the channel attributes identify
3026 * how we've been setup (which might be different if a fixed
3027 * channel is specified).
3029 if (vap->iv_opmode == IEEE80211_M_STA) {
3030 /* override 20/40 use based on config */
3031 if (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)
3032 caps |= IEEE80211_HTCAP_CHWIDTH40;
3034 caps &= ~IEEE80211_HTCAP_CHWIDTH40;
3036 /* Start by using the advertised settings */
3037 rxmax = MS(ni->ni_htparam, IEEE80211_HTCAP_MAXRXAMPDU);
3038 density = MS(ni->ni_htparam, IEEE80211_HTCAP_MPDUDENSITY);
3040 IEEE80211_DPRINTF(vap, IEEE80211_MSG_11N,
3041 "%s: advertised rxmax=%d, density=%d, vap rxmax=%d, density=%d\n",
3045 vap->iv_ampdu_rxmax,
3046 vap->iv_ampdu_density);
3048 /* Cap at VAP rxmax */
3049 if (rxmax > vap->iv_ampdu_rxmax)
3050 rxmax = vap->iv_ampdu_rxmax;
3053 * If the VAP ampdu density value greater, use that.
3055 * (Larger density value == larger minimum gap between A-MPDU
3058 if (vap->iv_ampdu_density > density)
3059 density = vap->iv_ampdu_density;
3062 * NB: Hardware might support HT40 on some but not all
3063 * channels. We can't determine this earlier because only
3064 * after association the channel is upgraded to HT based
3065 * on the negotiated capabilities.
3067 if (ni->ni_chan != IEEE80211_CHAN_ANYC &&
3068 findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT40U) == NULL &&
3069 findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT40D) == NULL)
3070 caps &= ~IEEE80211_HTCAP_CHWIDTH40;
3072 /* override 20/40 use based on current channel */
3073 if (IEEE80211_IS_CHAN_HT40(ni->ni_chan))
3074 caps |= IEEE80211_HTCAP_CHWIDTH40;
3076 caps &= ~IEEE80211_HTCAP_CHWIDTH40;
3078 /* XXX TODO should it start by using advertised settings? */
3079 rxmax = vap->iv_ampdu_rxmax;
3080 density = vap->iv_ampdu_density;
3083 /* adjust short GI based on channel and config */
3084 if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20) == 0)
3085 caps &= ~IEEE80211_HTCAP_SHORTGI20;
3086 if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40) == 0 ||
3087 (caps & IEEE80211_HTCAP_CHWIDTH40) == 0)
3088 caps &= ~IEEE80211_HTCAP_SHORTGI40;
3090 /* adjust STBC based on receive capabilities */
3091 if ((vap->iv_flags_ht & IEEE80211_FHT_STBC_RX) == 0)
3092 caps &= ~IEEE80211_HTCAP_RXSTBC;
3094 /* adjust LDPC based on receive capabilites */
3095 if ((vap->iv_flags_ht & IEEE80211_FHT_LDPC_RX) == 0)
3096 caps &= ~IEEE80211_HTCAP_LDPC;
3098 ADDSHORT(frm, caps);
3101 *frm = SM(rxmax, IEEE80211_HTCAP_MAXRXAMPDU)
3102 | SM(density, IEEE80211_HTCAP_MPDUDENSITY)
3106 /* pre-zero remainder of ie */
3107 memset(frm, 0, sizeof(struct ieee80211_ie_htcap) -
3108 __offsetof(struct ieee80211_ie_htcap, hc_mcsset));
3110 /* supported MCS set */
3112 * XXX: For sta mode the rate set should be restricted based
3113 * on the AP's capabilities, but ni_htrates isn't setup when
3114 * we're called to form an AssocReq frame so for now we're
3115 * restricted to the device capabilities.
3117 ieee80211_set_mcsset(ni->ni_ic, frm);
3119 frm += __offsetof(struct ieee80211_ie_htcap, hc_extcap) -
3120 __offsetof(struct ieee80211_ie_htcap, hc_mcsset);
3122 /* HT extended capabilities */
3123 extcaps = vap->iv_htextcaps & 0xffff;
3125 ADDSHORT(frm, extcaps);
3127 frm += sizeof(struct ieee80211_ie_htcap) -
3128 __offsetof(struct ieee80211_ie_htcap, hc_txbf);
3135 * Add 802.11n HT capabilities information element
3138 ieee80211_add_htcap(uint8_t *frm, struct ieee80211_node *ni)
3140 frm[0] = IEEE80211_ELEMID_HTCAP;
3141 frm[1] = sizeof(struct ieee80211_ie_htcap) - 2;
3142 return ieee80211_add_htcap_body(frm + 2, ni);
3146 * Non-associated probe request - add HT capabilities based on
3147 * the current channel configuration.
3150 ieee80211_add_htcap_body_ch(uint8_t *frm, struct ieee80211vap *vap,
3151 struct ieee80211_channel *c)
3153 #define ADDSHORT(frm, v) do { \
3154 frm[0] = (v) & 0xff; \
3155 frm[1] = (v) >> 8; \
3158 struct ieee80211com *ic = vap->iv_ic;
3159 uint16_t caps, extcaps;
3162 /* HT capabilities */
3163 caps = vap->iv_htcaps & 0xffff;
3166 * We don't use this in STA mode; only in IBSS mode.
3167 * So in IBSS mode we base our HTCAP flags on the
3171 /* override 20/40 use based on current channel */
3172 if (IEEE80211_IS_CHAN_HT40(c))
3173 caps |= IEEE80211_HTCAP_CHWIDTH40;
3175 caps &= ~IEEE80211_HTCAP_CHWIDTH40;
3177 /* Use the currently configured values */
3178 rxmax = vap->iv_ampdu_rxmax;
3179 density = vap->iv_ampdu_density;
3181 /* adjust short GI based on channel and config */
3182 if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20) == 0)
3183 caps &= ~IEEE80211_HTCAP_SHORTGI20;
3184 if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40) == 0 ||
3185 (caps & IEEE80211_HTCAP_CHWIDTH40) == 0)
3186 caps &= ~IEEE80211_HTCAP_SHORTGI40;
3187 ADDSHORT(frm, caps);
3190 *frm = SM(rxmax, IEEE80211_HTCAP_MAXRXAMPDU)
3191 | SM(density, IEEE80211_HTCAP_MPDUDENSITY)
3195 /* pre-zero remainder of ie */
3196 memset(frm, 0, sizeof(struct ieee80211_ie_htcap) -
3197 __offsetof(struct ieee80211_ie_htcap, hc_mcsset));
3199 /* supported MCS set */
3201 * XXX: For sta mode the rate set should be restricted based
3202 * on the AP's capabilities, but ni_htrates isn't setup when
3203 * we're called to form an AssocReq frame so for now we're
3204 * restricted to the device capabilities.
3206 ieee80211_set_mcsset(ic, frm);
3208 frm += __offsetof(struct ieee80211_ie_htcap, hc_extcap) -
3209 __offsetof(struct ieee80211_ie_htcap, hc_mcsset);
3211 /* HT extended capabilities */
3212 extcaps = vap->iv_htextcaps & 0xffff;
3214 ADDSHORT(frm, extcaps);
3216 frm += sizeof(struct ieee80211_ie_htcap) -
3217 __offsetof(struct ieee80211_ie_htcap, hc_txbf);
3224 * Add 802.11n HT capabilities information element
3227 ieee80211_add_htcap_ch(uint8_t *frm, struct ieee80211vap *vap,
3228 struct ieee80211_channel *c)
3230 frm[0] = IEEE80211_ELEMID_HTCAP;
3231 frm[1] = sizeof(struct ieee80211_ie_htcap) - 2;
3232 return ieee80211_add_htcap_body_ch(frm + 2, vap, c);
3236 * Add Broadcom OUI wrapped standard HTCAP ie; this is
3237 * used for compatibility w/ pre-draft implementations.
3240 ieee80211_add_htcap_vendor(uint8_t *frm, struct ieee80211_node *ni)
3242 frm[0] = IEEE80211_ELEMID_VENDOR;
3243 frm[1] = 4 + sizeof(struct ieee80211_ie_htcap) - 2;
3244 frm[2] = (BCM_OUI >> 0) & 0xff;
3245 frm[3] = (BCM_OUI >> 8) & 0xff;
3246 frm[4] = (BCM_OUI >> 16) & 0xff;
3247 frm[5] = BCM_OUI_HTCAP;
3248 return ieee80211_add_htcap_body(frm + 6, ni);
3252 * Construct the MCS bit mask of basic rates
3253 * for inclusion in an HT information element.
3256 ieee80211_set_basic_htrates(uint8_t *frm, const struct ieee80211_htrateset *rs)
3260 for (i = 0; i < rs->rs_nrates; i++) {
3261 int r = rs->rs_rates[i] & IEEE80211_RATE_VAL;
3262 if ((rs->rs_rates[i] & IEEE80211_RATE_BASIC) &&
3263 r < IEEE80211_HTRATE_MAXSIZE) {
3264 /* NB: this assumes a particular implementation */
3271 * Update the HTINFO ie for a beacon frame.
3274 ieee80211_ht_update_beacon(struct ieee80211vap *vap,
3275 struct ieee80211_beacon_offsets *bo)
3277 #define PROTMODE (IEEE80211_HTINFO_OPMODE|IEEE80211_HTINFO_NONHT_PRESENT)
3278 struct ieee80211_node *ni;
3279 const struct ieee80211_channel *bsschan;
3280 struct ieee80211com *ic = vap->iv_ic;
3281 struct ieee80211_ie_htinfo *ht =
3282 (struct ieee80211_ie_htinfo *) bo->bo_htinfo;
3284 ni = ieee80211_ref_node(vap->iv_bss);
3285 bsschan = ni->ni_chan;
3287 /* XXX only update on channel change */
3288 ht->hi_ctrlchannel = ieee80211_chan2ieee(ic, bsschan);
3289 if (vap->iv_flags_ht & IEEE80211_FHT_RIFS)
3290 ht->hi_byte1 = IEEE80211_HTINFO_RIFSMODE_PERM;
3292 ht->hi_byte1 = IEEE80211_HTINFO_RIFSMODE_PROH;
3293 if (IEEE80211_IS_CHAN_HT40U(bsschan))
3294 ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_ABOVE;
3295 else if (IEEE80211_IS_CHAN_HT40D(bsschan))
3296 ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_BELOW;
3298 ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_NONE;
3299 if (IEEE80211_IS_CHAN_HT40(bsschan))
3300 ht->hi_byte1 |= IEEE80211_HTINFO_TXWIDTH_2040;
3302 /* protection mode */
3303 ht->hi_byte2 = (ht->hi_byte2 &~ PROTMODE) | ic->ic_curhtprotmode;
3305 ieee80211_free_node(ni);
3307 /* XXX propagate to vendor ie's */
3312 * Add body of an HTINFO information element.
3314 * NB: We don't use struct ieee80211_ie_htinfo because we can
3315 * be called to fillin both a standard ie and a compat ie that
3316 * has a vendor OUI at the front.
3319 ieee80211_add_htinfo_body(uint8_t *frm, struct ieee80211_node *ni)
3321 struct ieee80211vap *vap = ni->ni_vap;
3322 struct ieee80211com *ic = ni->ni_ic;
3324 /* pre-zero remainder of ie */
3325 memset(frm, 0, sizeof(struct ieee80211_ie_htinfo) - 2);
3327 /* primary/control channel center */
3328 *frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
3330 if (vap->iv_flags_ht & IEEE80211_FHT_RIFS)
3331 frm[0] = IEEE80211_HTINFO_RIFSMODE_PERM;
3333 frm[0] = IEEE80211_HTINFO_RIFSMODE_PROH;
3334 if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan))
3335 frm[0] |= IEEE80211_HTINFO_2NDCHAN_ABOVE;
3336 else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan))
3337 frm[0] |= IEEE80211_HTINFO_2NDCHAN_BELOW;
3339 frm[0] |= IEEE80211_HTINFO_2NDCHAN_NONE;
3340 if (IEEE80211_IS_CHAN_HT40(ni->ni_chan))
3341 frm[0] |= IEEE80211_HTINFO_TXWIDTH_2040;
3343 frm[1] = ic->ic_curhtprotmode;
3348 ieee80211_set_basic_htrates(frm, &ni->ni_htrates);
3349 frm += sizeof(struct ieee80211_ie_htinfo) -
3350 __offsetof(struct ieee80211_ie_htinfo, hi_basicmcsset);
3355 * Add 802.11n HT information information element.
3358 ieee80211_add_htinfo(uint8_t *frm, struct ieee80211_node *ni)
3360 frm[0] = IEEE80211_ELEMID_HTINFO;
3361 frm[1] = sizeof(struct ieee80211_ie_htinfo) - 2;
3362 return ieee80211_add_htinfo_body(frm + 2, ni);
3366 * Add Broadcom OUI wrapped standard HTINFO ie; this is
3367 * used for compatibility w/ pre-draft implementations.
3370 ieee80211_add_htinfo_vendor(uint8_t *frm, struct ieee80211_node *ni)
3372 frm[0] = IEEE80211_ELEMID_VENDOR;
3373 frm[1] = 4 + sizeof(struct ieee80211_ie_htinfo) - 2;
3374 frm[2] = (BCM_OUI >> 0) & 0xff;
3375 frm[3] = (BCM_OUI >> 8) & 0xff;
3376 frm[4] = (BCM_OUI >> 16) & 0xff;
3377 frm[5] = BCM_OUI_HTINFO;
3378 return ieee80211_add_htinfo_body(frm + 6, ni);