The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/net80211/ieee80211_ht.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 2007-2008 Sam Leffler, Errno Consulting
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    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.
   13  *
   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.
   24  */
   25 
   26 #include <sys/cdefs.h>
   27 #ifdef __FreeBSD__
   28 __FBSDID("$FreeBSD$");
   29 #endif
   30 
   31 /*
   32  * IEEE 802.11n protocol support.
   33  */
   34 
   35 #include "opt_inet.h"
   36 #include "opt_wlan.h"
   37 
   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>
   43  
   44 #include <sys/socket.h>
   45 
   46 #include <net/if.h>
   47 #include <net/if_var.h>
   48 #include <net/if_media.h>
   49 #include <net/ethernet.h>
   50 
   51 #include <net80211/ieee80211_var.h>
   52 #include <net80211/ieee80211_action.h>
   53 #include <net80211/ieee80211_input.h>
   54 
   55 /* define here, used throughout file */
   56 #define MS(_v, _f)      (((_v) & _f) >> _f##_S)
   57 #define SM(_v, _f)      (((_v) << _f##_S) & _f)
   58 
   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 */
  137 };
  138 
  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)");
  143 
  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)");
  147 
  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");
  159 
  160 static  int ieee80211_bar_timeout = -1; /* timeout waiting for BAR response */
  161 static  int ieee80211_bar_maxtries = 50;/* max BAR requests before DELBA */
  162 
  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;
  168 
  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;
  172 
  173 static void
  174 ieee80211_ht_init(void)
  175 {
  176         /*
  177          * Setup HT parameters that depends on the clock frequency.
  178          */
  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);
  183         /*
  184          * Register action frame handlers.
  185          */
  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);
  196 
  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);
  205 }
  206 SYSINIT(wlan_ht, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_ht_init, NULL);
  207 
  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);
  220 
  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 *);
  228 
  229 void
  230 ieee80211_ht_attach(struct ieee80211com *ic)
  231 {
  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;
  243 
  244         ic->ic_htprotmode = IEEE80211_PROT_RTSCTS;
  245         ic->ic_curhtprotmode = IEEE80211_HTINFO_OPMODE_PURE;
  246 }
  247 
  248 void
  249 ieee80211_ht_detach(struct ieee80211com *ic)
  250 {
  251 }
  252 
  253 void
  254 ieee80211_ht_vattach(struct ieee80211vap *vap)
  255 {
  256 
  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;
  267 
  268         if (vap->iv_htcaps & IEEE80211_HTC_HT) {
  269                 /*
  270                  * Device is HT capable; enable all HT-related
  271                  * facilities by default.
  272                  * XXX these choices may be too aggressive.
  273                  */
  274                 vap->iv_flags_ht |= IEEE80211_FHT_HT
  275                                  |  IEEE80211_FHT_HTCOMPAT
  276                                  ;
  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;
  284                 }
  285                 /* enable RIFS if capable */
  286                 if (vap->iv_htcaps & IEEE80211_HTC_RIFS)
  287                         vap->iv_flags_ht |= IEEE80211_FHT_RIFS;
  288 
  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;
  296 
  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;
  301         }
  302         /* NB: disable default legacy WDS, too many issues right now */
  303         if (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY)
  304                 vap->iv_flags_ht &= ~IEEE80211_FHT_HT;
  305 }
  306 
  307 void
  308 ieee80211_ht_vdetach(struct ieee80211vap *vap)
  309 {
  310 }
  311 
  312 static int
  313 ht_getrate(struct ieee80211com *ic, int index, enum ieee80211_phymode mode,
  314     int ratetype)
  315 {
  316         int mword, rate;
  317 
  318         mword = ieee80211_rate2media(ic, index | IEEE80211_RATE_MCS, mode);
  319         if (IFM_SUBTYPE(mword) != IFM_IEEE80211_MCS)
  320                 return (0);
  321         switch (ratetype) {
  322         case 0:
  323                 rate = ieee80211_htrates[index].ht20_rate_800ns;
  324                 break;
  325         case 1:
  326                 rate = ieee80211_htrates[index].ht20_rate_400ns;
  327                 break;
  328         case 2:
  329                 rate = ieee80211_htrates[index].ht40_rate_800ns;
  330                 break;
  331         default:
  332                 rate = ieee80211_htrates[index].ht40_rate_400ns;
  333                 break;
  334         }
  335         return (rate);
  336 }
  337 
  338 static struct printranges {
  339         int     minmcs;
  340         int     maxmcs;
  341         int     txstream;
  342         int     ratetype;
  343         int     htcapflags;
  344 } ranges[] = {
  345         {  0,  7, 1, 0, 0 },
  346         {  8, 15, 2, 0, 0 },
  347         { 16, 23, 3, 0, 0 },
  348         { 24, 31, 4, 0, 0 },
  349         { 32,  0, 1, 2, IEEE80211_HTC_TXMCS32 },
  350         { 33, 38, 2, 0, IEEE80211_HTC_TXUNEQUAL },
  351         { 39, 52, 3, 0, IEEE80211_HTC_TXUNEQUAL },
  352         { 53, 76, 4, 0, IEEE80211_HTC_TXUNEQUAL },
  353         {  0,  0, 0, 0, 0 },
  354 };
  355 
  356 static void
  357 ht_rateprint(struct ieee80211com *ic, enum ieee80211_phymode mode, int ratetype)
  358 {
  359         int minrate, maxrate;
  360         struct printranges *range;
  361 
  362         for (range = ranges; range->txstream != 0; range++) {
  363                 if (ic->ic_txstream < range->txstream)
  364                         continue;
  365                 if (range->htcapflags &&
  366                     (ic->ic_htcaps & range->htcapflags) == 0)
  367                         continue;
  368                 if (ratetype < range->ratetype)
  369                         continue;
  370                 minrate = ht_getrate(ic, range->minmcs, mode, ratetype);
  371                 maxrate = ht_getrate(ic, range->maxmcs, mode, ratetype);
  372                 if (range->maxmcs) {
  373                         ic_printf(ic, "MCS %d-%d: %d%sMbps - %d%sMbps\n",
  374                             range->minmcs, range->maxmcs,
  375                             minrate/2, ((minrate & 0x1) != 0 ? ".5" : ""),
  376                             maxrate/2, ((maxrate & 0x1) != 0 ? ".5" : ""));
  377                 } else {
  378                         ic_printf(ic, "MCS %d: %d%sMbps\n", range->minmcs,
  379                             minrate/2, ((minrate & 0x1) != 0 ? ".5" : ""));
  380                 }
  381         }
  382 }
  383 
  384 static void
  385 ht_announce(struct ieee80211com *ic, enum ieee80211_phymode mode)
  386 {
  387         const char *modestr = ieee80211_phymode_name[mode];
  388 
  389         ic_printf(ic, "%s MCS 20MHz\n", modestr);
  390         ht_rateprint(ic, mode, 0);
  391         if (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20) {
  392                 ic_printf(ic, "%s MCS 20MHz SGI\n", modestr);
  393                 ht_rateprint(ic, mode, 1);
  394         }
  395         if (ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
  396                 ic_printf(ic, "%s MCS 40MHz:\n", modestr);
  397                 ht_rateprint(ic, mode, 2);
  398         }
  399         if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
  400             (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40)) {
  401                 ic_printf(ic, "%s MCS 40MHz SGI:\n", modestr);
  402                 ht_rateprint(ic, mode, 3);
  403         }
  404 }
  405 
  406 void
  407 ieee80211_ht_announce(struct ieee80211com *ic)
  408 {
  409 
  410         if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) ||
  411             isset(ic->ic_modecaps, IEEE80211_MODE_11NG))
  412                 ic_printf(ic, "%dT%dR\n", ic->ic_txstream, ic->ic_rxstream);
  413         if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA))
  414                 ht_announce(ic, IEEE80211_MODE_11NA);
  415         if (isset(ic->ic_modecaps, IEEE80211_MODE_11NG))
  416                 ht_announce(ic, IEEE80211_MODE_11NG);
  417 }
  418 
  419 static struct ieee80211_htrateset htrateset;
  420 
  421 const struct ieee80211_htrateset *
  422 ieee80211_get_suphtrates(struct ieee80211com *ic,
  423     const struct ieee80211_channel *c)
  424 {
  425 #define ADDRATE(x)      do {                                            \
  426         htrateset.rs_rates[htrateset.rs_nrates] = x;                    \
  427         htrateset.rs_nrates++;                                          \
  428 } while (0)
  429         int i;
  430 
  431         memset(&htrateset, 0, sizeof(struct ieee80211_htrateset));
  432         for (i = 0; i < ic->ic_txstream * 8; i++)
  433                 ADDRATE(i);
  434         if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
  435             (ic->ic_htcaps & IEEE80211_HTC_TXMCS32))
  436                 ADDRATE(32);
  437         if (ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL) {
  438                 if (ic->ic_txstream >= 2) {
  439                          for (i = 33; i <= 38; i++)
  440                                 ADDRATE(i);
  441                 }
  442                 if (ic->ic_txstream >= 3) {
  443                         for (i = 39; i <= 52; i++)
  444                                 ADDRATE(i);
  445                 }
  446                 if (ic->ic_txstream == 4) {
  447                         for (i = 53; i <= 76; i++)
  448                                 ADDRATE(i);
  449                 }
  450         }
  451         return &htrateset;
  452 #undef  ADDRATE
  453 }
  454 
  455 /*
  456  * Receive processing.
  457  */
  458 
  459 /*
  460  * Decap the encapsulated A-MSDU frames and dispatch all but
  461  * the last for delivery.  The last frame is returned for 
  462  * delivery via the normal path.
  463  */
  464 struct mbuf *
  465 ieee80211_decap_amsdu(struct ieee80211_node *ni, struct mbuf *m)
  466 {
  467         struct ieee80211vap *vap = ni->ni_vap;
  468         int framelen;
  469         struct mbuf *n;
  470 
  471         /* discard 802.3 header inserted by ieee80211_decap */
  472         m_adj(m, sizeof(struct ether_header));
  473 
  474         vap->iv_stats.is_amsdu_decap++;
  475 
  476         for (;;) {
  477                 /*
  478                  * Decap the first frame, bust it apart from the
  479                  * remainder and deliver.  We leave the last frame
  480                  * delivery to the caller (for consistency with other
  481                  * code paths, could also do it here).
  482                  */
  483                 m = ieee80211_decap1(m, &framelen);
  484                 if (m == NULL) {
  485                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
  486                             ni->ni_macaddr, "a-msdu", "%s", "decap failed");
  487                         vap->iv_stats.is_amsdu_tooshort++;
  488                         return NULL;
  489                 }
  490                 if (m->m_pkthdr.len == framelen)
  491                         break;
  492                 n = m_split(m, framelen, M_NOWAIT);
  493                 if (n == NULL) {
  494                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
  495                             ni->ni_macaddr, "a-msdu",
  496                             "%s", "unable to split encapsulated frames");
  497                         vap->iv_stats.is_amsdu_split++;
  498                         m_freem(m);                     /* NB: must reclaim */
  499                         return NULL;
  500                 }
  501                 vap->iv_deliver_data(vap, ni, m);
  502 
  503                 /*
  504                  * Remove frame contents; each intermediate frame
  505                  * is required to be aligned to a 4-byte boundary.
  506                  */
  507                 m = n;
  508                 m_adj(m, roundup2(framelen, 4) - framelen);     /* padding */
  509         }
  510         return m;                               /* last delivered by caller */
  511 }
  512 
  513 /*
  514  * Purge all frames in the A-MPDU re-order queue.
  515  */
  516 static void
  517 ampdu_rx_purge(struct ieee80211_rx_ampdu *rap)
  518 {
  519         struct mbuf *m;
  520         int i;
  521 
  522         for (i = 0; i < rap->rxa_wnd; i++) {
  523                 m = rap->rxa_m[i];
  524                 if (m != NULL) {
  525                         rap->rxa_m[i] = NULL;
  526                         rap->rxa_qbytes -= m->m_pkthdr.len;
  527                         m_freem(m);
  528                         if (--rap->rxa_qframes == 0)
  529                                 break;
  530                 }
  531         }
  532         KASSERT(rap->rxa_qbytes == 0 && rap->rxa_qframes == 0,
  533             ("lost %u data, %u frames on ampdu rx q",
  534             rap->rxa_qbytes, rap->rxa_qframes));
  535 }
  536 
  537 /*
  538  * Start A-MPDU rx/re-order processing for the specified TID.
  539  */
  540 static int
  541 ampdu_rx_start(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap,
  542         int baparamset, int batimeout, int baseqctl)
  543 {
  544         int bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
  545 
  546         if (rap->rxa_flags & IEEE80211_AGGR_RUNNING) {
  547                 /*
  548                  * AMPDU previously setup and not terminated with a DELBA,
  549                  * flush the reorder q's in case anything remains.
  550                  */
  551                 ampdu_rx_purge(rap);
  552         }
  553         memset(rap, 0, sizeof(*rap));
  554         rap->rxa_wnd = (bufsiz == 0) ?
  555             IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
  556         rap->rxa_start = MS(baseqctl, IEEE80211_BASEQ_START);
  557         rap->rxa_flags |=  IEEE80211_AGGR_RUNNING | IEEE80211_AGGR_XCHGPEND;
  558 
  559         return 0;
  560 }
  561 
  562 /*
  563  * Public function; manually setup the RX ampdu state.
  564  */
  565 int
  566 ieee80211_ampdu_rx_start_ext(struct ieee80211_node *ni, int tid, int seq, int baw)
  567 {
  568         struct ieee80211_rx_ampdu *rap;
  569 
  570         /* XXX TODO: sanity check tid, seq, baw */
  571 
  572         rap = &ni->ni_rx_ampdu[tid];
  573 
  574         if (rap->rxa_flags & IEEE80211_AGGR_RUNNING) {
  575                 /*
  576                  * AMPDU previously setup and not terminated with a DELBA,
  577                  * flush the reorder q's in case anything remains.
  578                  */
  579                 ampdu_rx_purge(rap);
  580         }
  581 
  582         memset(rap, 0, sizeof(*rap));
  583         rap->rxa_wnd = (baw== 0) ?
  584             IEEE80211_AGGR_BAWMAX : min(baw, IEEE80211_AGGR_BAWMAX);
  585         rap->rxa_start = seq;
  586         rap->rxa_flags |=  IEEE80211_AGGR_RUNNING | IEEE80211_AGGR_XCHGPEND;
  587 
  588         IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, ni,
  589             "%s: tid=%d, start=%d, wnd=%d, flags=0x%08x\n",
  590             __func__,
  591             tid,
  592             seq,
  593             rap->rxa_wnd,
  594             rap->rxa_flags);
  595 
  596         return 0;
  597 }
  598 
  599 /*
  600  * Stop A-MPDU rx processing for the specified TID.
  601  */
  602 static void
  603 ampdu_rx_stop(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
  604 {
  605 
  606         ampdu_rx_purge(rap);
  607         rap->rxa_flags &= ~(IEEE80211_AGGR_RUNNING | IEEE80211_AGGR_XCHGPEND);
  608 }
  609 
  610 /*
  611  * Dispatch a frame from the A-MPDU reorder queue.  The
  612  * frame is fed back into ieee80211_input marked with an
  613  * M_AMPDU_MPDU flag so it doesn't come back to us (it also
  614  * permits ieee80211_input to optimize re-processing).
  615  */
  616 static __inline void
  617 ampdu_dispatch(struct ieee80211_node *ni, struct mbuf *m)
  618 {
  619         m->m_flags |= M_AMPDU_MPDU;     /* bypass normal processing */
  620         /* NB: rssi and noise are ignored w/ M_AMPDU_MPDU set */
  621         (void) ieee80211_input(ni, m, 0, 0);
  622 }
  623 
  624 /*
  625  * Dispatch as many frames as possible from the re-order queue.
  626  * Frames will always be "at the front"; we process all frames
  627  * up to the first empty slot in the window.  On completion we
  628  * cleanup state if there are still pending frames in the current
  629  * BA window.  We assume the frame at slot 0 is already handled
  630  * by the caller; we always start at slot 1.
  631  */
  632 static void
  633 ampdu_rx_dispatch(struct ieee80211_rx_ampdu *rap, struct ieee80211_node *ni)
  634 {
  635         struct ieee80211vap *vap = ni->ni_vap;
  636         struct mbuf *m;
  637         int i;
  638 
  639         /* flush run of frames */
  640         for (i = 1; i < rap->rxa_wnd; i++) {
  641                 m = rap->rxa_m[i];
  642                 if (m == NULL)
  643                         break;
  644                 rap->rxa_m[i] = NULL;
  645                 rap->rxa_qbytes -= m->m_pkthdr.len;
  646                 rap->rxa_qframes--;
  647 
  648                 ampdu_dispatch(ni, m);
  649         }
  650         /*
  651          * If frames remain, copy the mbuf pointers down so
  652          * they correspond to the offsets in the new window.
  653          */
  654         if (rap->rxa_qframes != 0) {
  655                 int n = rap->rxa_qframes, j;
  656                 for (j = i+1; j < rap->rxa_wnd; j++) {
  657                         if (rap->rxa_m[j] != NULL) {
  658                                 rap->rxa_m[j-i] = rap->rxa_m[j];
  659                                 rap->rxa_m[j] = NULL;
  660                                 if (--n == 0)
  661                                         break;
  662                         }
  663                 }
  664                 KASSERT(n == 0, ("lost %d frames", n));
  665                 vap->iv_stats.is_ampdu_rx_copy += rap->rxa_qframes;
  666         }
  667         /*
  668          * Adjust the start of the BA window to
  669          * reflect the frames just dispatched.
  670          */
  671         rap->rxa_start = IEEE80211_SEQ_ADD(rap->rxa_start, i);
  672         vap->iv_stats.is_ampdu_rx_oor += i;
  673 }
  674 
  675 /*
  676  * Dispatch all frames in the A-MPDU re-order queue.
  677  */
  678 static void
  679 ampdu_rx_flush(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
  680 {
  681         struct ieee80211vap *vap = ni->ni_vap;
  682         struct mbuf *m;
  683         int i;
  684 
  685         for (i = 0; i < rap->rxa_wnd; i++) {
  686                 m = rap->rxa_m[i];
  687                 if (m == NULL)
  688                         continue;
  689                 rap->rxa_m[i] = NULL;
  690                 rap->rxa_qbytes -= m->m_pkthdr.len;
  691                 rap->rxa_qframes--;
  692                 vap->iv_stats.is_ampdu_rx_oor++;
  693 
  694                 ampdu_dispatch(ni, m);
  695                 if (rap->rxa_qframes == 0)
  696                         break;
  697         }
  698 }
  699 
  700 /*
  701  * Dispatch all frames in the A-MPDU re-order queue
  702  * preceding the specified sequence number.  This logic
  703  * handles window moves due to a received MSDU or BAR.
  704  */
  705 static void
  706 ampdu_rx_flush_upto(struct ieee80211_node *ni,
  707         struct ieee80211_rx_ampdu *rap, ieee80211_seq winstart)
  708 {
  709         struct ieee80211vap *vap = ni->ni_vap;
  710         struct mbuf *m;
  711         ieee80211_seq seqno;
  712         int i;
  713 
  714         /*
  715          * Flush any complete MSDU's with a sequence number lower
  716          * than winstart.  Gaps may exist.  Note that we may actually
  717          * dispatch frames past winstart if a run continues; this is
  718          * an optimization that avoids having to do a separate pass
  719          * to dispatch frames after moving the BA window start.
  720          */
  721         seqno = rap->rxa_start;
  722         for (i = 0; i < rap->rxa_wnd; i++) {
  723                 m = rap->rxa_m[i];
  724                 if (m != NULL) {
  725                         rap->rxa_m[i] = NULL;
  726                         rap->rxa_qbytes -= m->m_pkthdr.len;
  727                         rap->rxa_qframes--;
  728                         vap->iv_stats.is_ampdu_rx_oor++;
  729 
  730                         ampdu_dispatch(ni, m);
  731                 } else {
  732                         if (!IEEE80211_SEQ_BA_BEFORE(seqno, winstart))
  733                                 break;
  734                 }
  735                 seqno = IEEE80211_SEQ_INC(seqno);
  736         }
  737         /*
  738          * If frames remain, copy the mbuf pointers down so
  739          * they correspond to the offsets in the new window.
  740          */
  741         if (rap->rxa_qframes != 0) {
  742                 int n = rap->rxa_qframes, j;
  743 
  744                 /* NB: this loop assumes i > 0 and/or rxa_m[0] is NULL */
  745                 KASSERT(rap->rxa_m[0] == NULL,
  746                     ("%s: BA window slot 0 occupied", __func__));
  747                 for (j = i+1; j < rap->rxa_wnd; j++) {
  748                         if (rap->rxa_m[j] != NULL) {
  749                                 rap->rxa_m[j-i] = rap->rxa_m[j];
  750                                 rap->rxa_m[j] = NULL;
  751                                 if (--n == 0)
  752                                         break;
  753                         }
  754                 }
  755                 KASSERT(n == 0, ("%s: lost %d frames, qframes %d off %d "
  756                     "BA win <%d:%d> winstart %d",
  757                     __func__, n, rap->rxa_qframes, i, rap->rxa_start,
  758                     IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
  759                     winstart));
  760                 vap->iv_stats.is_ampdu_rx_copy += rap->rxa_qframes;
  761         }
  762         /*
  763          * Move the start of the BA window; we use the
  764          * sequence number of the last MSDU that was
  765          * passed up the stack+1 or winstart if stopped on
  766          * a gap in the reorder buffer.
  767          */
  768         rap->rxa_start = seqno;
  769 }
  770 
  771 /*
  772  * Process a received QoS data frame for an HT station.  Handle
  773  * A-MPDU reordering: if this frame is received out of order
  774  * and falls within the BA window hold onto it.  Otherwise if
  775  * this frame completes a run, flush any pending frames.  We
  776  * return 1 if the frame is consumed.  A 0 is returned if
  777  * the frame should be processed normally by the caller.
  778  */
  779 int
  780 ieee80211_ampdu_reorder(struct ieee80211_node *ni, struct mbuf *m)
  781 {
  782 #define IEEE80211_FC0_QOSDATA \
  783         (IEEE80211_FC0_TYPE_DATA|IEEE80211_FC0_SUBTYPE_QOS|IEEE80211_FC0_VERSION_0)
  784 #define PROCESS         0       /* caller should process frame */
  785 #define CONSUMED        1       /* frame consumed, caller does nothing */
  786         struct ieee80211vap *vap = ni->ni_vap;
  787         struct ieee80211_qosframe *wh;
  788         struct ieee80211_rx_ampdu *rap;
  789         ieee80211_seq rxseq;
  790         uint8_t tid;
  791         int off;
  792 
  793         KASSERT((m->m_flags & (M_AMPDU | M_AMPDU_MPDU)) == M_AMPDU,
  794             ("!a-mpdu or already re-ordered, flags 0x%x", m->m_flags));
  795         KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT sta"));
  796 
  797         /* NB: m_len known to be sufficient */
  798         wh = mtod(m, struct ieee80211_qosframe *);
  799         if (wh->i_fc[0] != IEEE80211_FC0_QOSDATA) {
  800                 /*
  801                  * Not QoS data, shouldn't get here but just
  802                  * return it to the caller for processing.
  803                  */
  804                 return PROCESS;
  805         }
  806         tid = ieee80211_getqos(wh)[0];
  807         tid &= IEEE80211_QOS_TID;
  808         rap = &ni->ni_rx_ampdu[tid];
  809         if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
  810                 /*
  811                  * No ADDBA request yet, don't touch.
  812                  */
  813                 return PROCESS;
  814         }
  815         rxseq = le16toh(*(uint16_t *)wh->i_seq);
  816         if ((rxseq & IEEE80211_SEQ_FRAG_MASK) != 0) {
  817                 /*
  818                  * Fragments are not allowed; toss.
  819                  */
  820                 IEEE80211_DISCARD_MAC(vap,
  821                     IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
  822                     "A-MPDU", "fragment, rxseq 0x%x tid %u%s", rxseq, tid,
  823                     wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
  824                 vap->iv_stats.is_ampdu_rx_drop++;
  825                 IEEE80211_NODE_STAT(ni, rx_drop);
  826                 m_freem(m);
  827                 return CONSUMED;
  828         }
  829         rxseq >>= IEEE80211_SEQ_SEQ_SHIFT;
  830         rap->rxa_nframes++;
  831 again:
  832         if (rxseq == rap->rxa_start) {
  833                 /*
  834                  * First frame in window.
  835                  */
  836                 if (rap->rxa_qframes != 0) {
  837                         /*
  838                          * Dispatch as many packets as we can.
  839                          */
  840                         KASSERT(rap->rxa_m[0] == NULL, ("unexpected dup"));
  841                         ampdu_dispatch(ni, m);
  842                         ampdu_rx_dispatch(rap, ni);
  843                         return CONSUMED;
  844                 } else {
  845                         /*
  846                          * In order; advance window and notify
  847                          * caller to dispatch directly.
  848                          */
  849                         rap->rxa_start = IEEE80211_SEQ_INC(rxseq);
  850                         return PROCESS;
  851                 }
  852         }
  853         /*
  854          * Frame is out of order; store if in the BA window.
  855          */
  856         /* calculate offset in BA window */
  857         off = IEEE80211_SEQ_SUB(rxseq, rap->rxa_start);
  858         if (off < rap->rxa_wnd) {
  859                 /*
  860                  * Common case (hopefully): in the BA window.
  861                  * Sec 9.10.7.6.2 a) (p.137)
  862                  */
  863 
  864                 /* 
  865                  * Check for frames sitting too long in the reorder queue.
  866                  * This should only ever happen if frames are not delivered
  867                  * without the sender otherwise notifying us (e.g. with a
  868                  * BAR to move the window).  Typically this happens because
  869                  * of vendor bugs that cause the sequence number to jump.
  870                  * When this happens we get a gap in the reorder queue that
  871                  * leaves frame sitting on the queue until they get pushed
  872                  * out due to window moves.  When the vendor does not send
  873                  * BAR this move only happens due to explicit packet sends
  874                  *
  875                  * NB: we only track the time of the oldest frame in the
  876                  * reorder q; this means that if we flush we might push
  877                  * frames that still "new"; if this happens then subsequent
  878                  * frames will result in BA window moves which cost something
  879                  * but is still better than a big throughput dip.
  880                  */
  881                 if (rap->rxa_qframes != 0) {
  882                         /* XXX honor batimeout? */
  883                         if (ticks - rap->rxa_age > ieee80211_ampdu_age) {
  884                                 /*
  885                                  * Too long since we received the first
  886                                  * frame; flush the reorder buffer.
  887                                  */
  888                                 if (rap->rxa_qframes != 0) {
  889                                         vap->iv_stats.is_ampdu_rx_age +=
  890                                             rap->rxa_qframes;
  891                                         ampdu_rx_flush(ni, rap);
  892                                 }
  893                                 rap->rxa_start = IEEE80211_SEQ_INC(rxseq);
  894                                 return PROCESS;
  895                         }
  896                 } else {
  897                         /*
  898                          * First frame, start aging timer.
  899                          */
  900                         rap->rxa_age = ticks;
  901                 }
  902 
  903                 /* save packet */
  904                 if (rap->rxa_m[off] == NULL) {
  905                         rap->rxa_m[off] = m;
  906                         rap->rxa_qframes++;
  907                         rap->rxa_qbytes += m->m_pkthdr.len;
  908                         vap->iv_stats.is_ampdu_rx_reorder++;
  909                 } else {
  910                         IEEE80211_DISCARD_MAC(vap,
  911                             IEEE80211_MSG_INPUT | IEEE80211_MSG_11N,
  912                             ni->ni_macaddr, "a-mpdu duplicate",
  913                             "seqno %u tid %u BA win <%u:%u>",
  914                             rxseq, tid, rap->rxa_start,
  915                             IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1));
  916                         vap->iv_stats.is_rx_dup++;
  917                         IEEE80211_NODE_STAT(ni, rx_dup);
  918                         m_freem(m);
  919                 }
  920                 return CONSUMED;
  921         }
  922         if (off < IEEE80211_SEQ_BA_RANGE) {
  923                 /*
  924                  * Outside the BA window, but within range;
  925                  * flush the reorder q and move the window.
  926                  * Sec 9.10.7.6.2 b) (p.138)
  927                  */
  928                 IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
  929                     "move BA win <%u:%u> (%u frames) rxseq %u tid %u",
  930                     rap->rxa_start,
  931                     IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
  932                     rap->rxa_qframes, rxseq, tid);
  933                 vap->iv_stats.is_ampdu_rx_move++;
  934 
  935                 /*
  936                  * The spec says to flush frames up to but not including:
  937                  *      WinStart_B = rxseq - rap->rxa_wnd + 1
  938                  * Then insert the frame or notify the caller to process
  939                  * it immediately.  We can safely do this by just starting
  940                  * over again because we know the frame will now be within
  941                  * the BA window.
  942                  */
  943                 /* NB: rxa_wnd known to be >0 */
  944                 ampdu_rx_flush_upto(ni, rap,
  945                     IEEE80211_SEQ_SUB(rxseq, rap->rxa_wnd-1));
  946                 goto again;
  947         } else {
  948                 /*
  949                  * Outside the BA window and out of range; toss.
  950                  * Sec 9.10.7.6.2 c) (p.138)
  951                  */
  952                 IEEE80211_DISCARD_MAC(vap,
  953                     IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
  954                     "MPDU", "BA win <%u:%u> (%u frames) rxseq %u tid %u%s",
  955                     rap->rxa_start,
  956                     IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
  957                     rap->rxa_qframes, rxseq, tid,
  958                     wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
  959                 vap->iv_stats.is_ampdu_rx_drop++;
  960                 IEEE80211_NODE_STAT(ni, rx_drop);
  961                 m_freem(m);
  962                 return CONSUMED;
  963         }
  964 #undef CONSUMED
  965 #undef PROCESS
  966 #undef IEEE80211_FC0_QOSDATA
  967 }
  968 
  969 /*
  970  * Process a BAR ctl frame.  Dispatch all frames up to
  971  * the sequence number of the frame.  If this frame is
  972  * out of range it's discarded.
  973  */
  974 void
  975 ieee80211_recv_bar(struct ieee80211_node *ni, struct mbuf *m0)
  976 {
  977         struct ieee80211vap *vap = ni->ni_vap;
  978         struct ieee80211_frame_bar *wh;
  979         struct ieee80211_rx_ampdu *rap;
  980         ieee80211_seq rxseq;
  981         int tid, off;
  982 
  983         if (!ieee80211_recv_bar_ena) {
  984 #if 0
  985                 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_11N,
  986                     ni->ni_macaddr, "BAR", "%s", "processing disabled");
  987 #endif
  988                 vap->iv_stats.is_ampdu_bar_bad++;
  989                 return;
  990         }
  991         wh = mtod(m0, struct ieee80211_frame_bar *);
  992         /* XXX check basic BAR */
  993         tid = MS(le16toh(wh->i_ctl), IEEE80211_BAR_TID);
  994         rap = &ni->ni_rx_ampdu[tid];
  995         if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
  996                 /*
  997                  * No ADDBA request yet, don't touch.
  998                  */
  999                 IEEE80211_DISCARD_MAC(vap,
 1000                     IEEE80211_MSG_INPUT | IEEE80211_MSG_11N,
 1001                     ni->ni_macaddr, "BAR", "no BA stream, tid %u", tid);
 1002                 vap->iv_stats.is_ampdu_bar_bad++;
 1003                 return;
 1004         }
 1005         vap->iv_stats.is_ampdu_bar_rx++;
 1006         rxseq = le16toh(wh->i_seq) >> IEEE80211_SEQ_SEQ_SHIFT;
 1007         if (rxseq == rap->rxa_start)
 1008                 return;
 1009         /* calculate offset in BA window */
 1010         off = IEEE80211_SEQ_SUB(rxseq, rap->rxa_start);
 1011         if (off < IEEE80211_SEQ_BA_RANGE) {
 1012                 /*
 1013                  * Flush the reorder q up to rxseq and move the window.
 1014                  * Sec 9.10.7.6.3 a) (p.138)
 1015                  */
 1016                 IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
 1017                     "BAR moves BA win <%u:%u> (%u frames) rxseq %u tid %u",
 1018                     rap->rxa_start,
 1019                     IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
 1020                     rap->rxa_qframes, rxseq, tid);
 1021                 vap->iv_stats.is_ampdu_bar_move++;
 1022 
 1023                 ampdu_rx_flush_upto(ni, rap, rxseq);
 1024                 if (off >= rap->rxa_wnd) {
 1025                         /*
 1026                          * BAR specifies a window start to the right of BA
 1027                          * window; we must move it explicitly since
 1028                          * ampdu_rx_flush_upto will not.
 1029                          */
 1030                         rap->rxa_start = rxseq;
 1031                 }
 1032         } else {
 1033                 /*
 1034                  * Out of range; toss.
 1035                  * Sec 9.10.7.6.3 b) (p.138)
 1036                  */
 1037                 IEEE80211_DISCARD_MAC(vap,
 1038                     IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
 1039                     "BAR", "BA win <%u:%u> (%u frames) rxseq %u tid %u%s",
 1040                     rap->rxa_start,
 1041                     IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
 1042                     rap->rxa_qframes, rxseq, tid,
 1043                     wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
 1044                 vap->iv_stats.is_ampdu_bar_oow++;
 1045                 IEEE80211_NODE_STAT(ni, rx_drop);
 1046         }
 1047 }
 1048 
 1049 /*
 1050  * Setup HT-specific state in a node.  Called only
 1051  * when HT use is negotiated so we don't do extra
 1052  * work for temporary and/or legacy sta's.
 1053  */
 1054 void
 1055 ieee80211_ht_node_init(struct ieee80211_node *ni)
 1056 {
 1057         struct ieee80211_tx_ampdu *tap;
 1058         int tid;
 1059 
 1060         IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
 1061             ni,
 1062             "%s: called (%p)",
 1063             __func__,
 1064             ni);
 1065 
 1066         if (ni->ni_flags & IEEE80211_NODE_HT) {
 1067                 /*
 1068                  * Clean AMPDU state on re-associate.  This handles the case
 1069                  * where a station leaves w/o notifying us and then returns
 1070                  * before node is reaped for inactivity.
 1071                  */
 1072                 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
 1073                     ni,
 1074                     "%s: calling cleanup (%p)",
 1075                     __func__, ni);
 1076                 ieee80211_ht_node_cleanup(ni);
 1077         }
 1078         for (tid = 0; tid < WME_NUM_TID; tid++) {
 1079                 tap = &ni->ni_tx_ampdu[tid];
 1080                 tap->txa_tid = tid;
 1081                 tap->txa_ni = ni;
 1082                 ieee80211_txampdu_init_pps(tap);
 1083                 /* NB: further initialization deferred */
 1084         }
 1085         ni->ni_flags |= IEEE80211_NODE_HT | IEEE80211_NODE_AMPDU;
 1086 }
 1087 
 1088 /*
 1089  * Cleanup HT-specific state in a node.  Called only
 1090  * when HT use has been marked.
 1091  */
 1092 void
 1093 ieee80211_ht_node_cleanup(struct ieee80211_node *ni)
 1094 {
 1095         struct ieee80211com *ic = ni->ni_ic;
 1096         int i;
 1097 
 1098         IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
 1099             ni,
 1100             "%s: called (%p)",
 1101             __func__, ni);
 1102 
 1103         KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT node"));
 1104 
 1105         /* XXX optimize this */
 1106         for (i = 0; i < WME_NUM_TID; i++) {
 1107                 struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[i];
 1108                 if (tap->txa_flags & IEEE80211_AGGR_SETUP)
 1109                         ampdu_tx_stop(tap);
 1110         }
 1111         for (i = 0; i < WME_NUM_TID; i++)
 1112                 ic->ic_ampdu_rx_stop(ni, &ni->ni_rx_ampdu[i]);
 1113 
 1114         ni->ni_htcap = 0;
 1115         ni->ni_flags &= ~IEEE80211_NODE_HT_ALL;
 1116 }
 1117 
 1118 /*
 1119  * Age out HT resources for a station.
 1120  */
 1121 void
 1122 ieee80211_ht_node_age(struct ieee80211_node *ni)
 1123 {
 1124         struct ieee80211vap *vap = ni->ni_vap;
 1125         uint8_t tid;
 1126 
 1127         KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT sta"));
 1128 
 1129         for (tid = 0; tid < WME_NUM_TID; tid++) {
 1130                 struct ieee80211_rx_ampdu *rap;
 1131 
 1132                 rap = &ni->ni_rx_ampdu[tid];
 1133                 if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0)
 1134                         continue;
 1135                 if (rap->rxa_qframes == 0)
 1136                         continue;
 1137                 /* 
 1138                  * Check for frames sitting too long in the reorder queue.
 1139                  * See above for more details on what's happening here.
 1140                  */
 1141                 /* XXX honor batimeout? */
 1142                 if (ticks - rap->rxa_age > ieee80211_ampdu_age) {
 1143                         /*
 1144                          * Too long since we received the first
 1145                          * frame; flush the reorder buffer.
 1146                          */
 1147                         vap->iv_stats.is_ampdu_rx_age += rap->rxa_qframes;
 1148                         ampdu_rx_flush(ni, rap);
 1149                 }
 1150         }
 1151 }
 1152 
 1153 static struct ieee80211_channel *
 1154 findhtchan(struct ieee80211com *ic, struct ieee80211_channel *c, int htflags)
 1155 {
 1156         return ieee80211_find_channel(ic, c->ic_freq,
 1157             (c->ic_flags &~ IEEE80211_CHAN_HT) | htflags);
 1158 }
 1159 
 1160 /*
 1161  * Adjust a channel to be HT/non-HT according to the vap's configuration.
 1162  */
 1163 struct ieee80211_channel *
 1164 ieee80211_ht_adjust_channel(struct ieee80211com *ic,
 1165         struct ieee80211_channel *chan, int flags)
 1166 {
 1167         struct ieee80211_channel *c;
 1168 
 1169         if (flags & IEEE80211_FHT_HT) {
 1170                 /* promote to HT if possible */
 1171                 if (flags & IEEE80211_FHT_USEHT40) {
 1172                         if (!IEEE80211_IS_CHAN_HT40(chan)) {
 1173                                 /* NB: arbitrarily pick ht40+ over ht40- */
 1174                                 c = findhtchan(ic, chan, IEEE80211_CHAN_HT40U);
 1175                                 if (c == NULL)
 1176                                         c = findhtchan(ic, chan,
 1177                                                 IEEE80211_CHAN_HT40D);
 1178                                 if (c == NULL)
 1179                                         c = findhtchan(ic, chan,
 1180                                                 IEEE80211_CHAN_HT20);
 1181                                 if (c != NULL)
 1182                                         chan = c;
 1183                         }
 1184                 } else if (!IEEE80211_IS_CHAN_HT20(chan)) {
 1185                         c = findhtchan(ic, chan, IEEE80211_CHAN_HT20);
 1186                         if (c != NULL)
 1187                                 chan = c;
 1188                 }
 1189         } else if (IEEE80211_IS_CHAN_HT(chan)) {
 1190                 /* demote to legacy, HT use is disabled */
 1191                 c = ieee80211_find_channel(ic, chan->ic_freq,
 1192                     chan->ic_flags &~ IEEE80211_CHAN_HT);
 1193                 if (c != NULL)
 1194                         chan = c;
 1195         }
 1196         return chan;
 1197 }
 1198 
 1199 /*
 1200  * Setup HT-specific state for a legacy WDS peer.
 1201  */
 1202 void
 1203 ieee80211_ht_wds_init(struct ieee80211_node *ni)
 1204 {
 1205         struct ieee80211vap *vap = ni->ni_vap;
 1206         struct ieee80211_tx_ampdu *tap;
 1207         int tid;
 1208 
 1209         KASSERT(vap->iv_flags_ht & IEEE80211_FHT_HT, ("no HT requested"));
 1210 
 1211         /* XXX check scan cache in case peer has an ap and we have info */
 1212         /*
 1213          * If setup with a legacy channel; locate an HT channel.
 1214          * Otherwise if the inherited channel (from a companion
 1215          * AP) is suitable use it so we use the same location
 1216          * for the extension channel).
 1217          */
 1218         ni->ni_chan = ieee80211_ht_adjust_channel(ni->ni_ic,
 1219             ni->ni_chan, ieee80211_htchanflags(ni->ni_chan));
 1220 
 1221         ni->ni_htcap = 0;
 1222         if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20)
 1223                 ni->ni_htcap |= IEEE80211_HTCAP_SHORTGI20;
 1224         if (IEEE80211_IS_CHAN_HT40(ni->ni_chan)) {
 1225                 ni->ni_htcap |= IEEE80211_HTCAP_CHWIDTH40;
 1226                 ni->ni_chw = 40;
 1227                 if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan))
 1228                         ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_ABOVE;
 1229                 else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan))
 1230                         ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_BELOW;
 1231                 if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40)
 1232                         ni->ni_htcap |= IEEE80211_HTCAP_SHORTGI40;
 1233         } else {
 1234                 ni->ni_chw = 20;
 1235                 ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_NONE;
 1236         }
 1237         ni->ni_htctlchan = ni->ni_chan->ic_ieee;
 1238         if (vap->iv_flags_ht & IEEE80211_FHT_RIFS)
 1239                 ni->ni_flags |= IEEE80211_NODE_RIFS;
 1240         /* XXX does it make sense to enable SMPS? */
 1241 
 1242         ni->ni_htopmode = 0;            /* XXX need protection state */
 1243         ni->ni_htstbc = 0;              /* XXX need info */
 1244 
 1245         for (tid = 0; tid < WME_NUM_TID; tid++) {
 1246                 tap = &ni->ni_tx_ampdu[tid];
 1247                 tap->txa_tid = tid;
 1248                 ieee80211_txampdu_init_pps(tap);
 1249         }
 1250         /* NB: AMPDU tx/rx governed by IEEE80211_FHT_AMPDU_{TX,RX} */
 1251         ni->ni_flags |= IEEE80211_NODE_HT | IEEE80211_NODE_AMPDU;
 1252 }
 1253 
 1254 /*
 1255  * Notify hostap vaps of a change in the HTINFO ie.
 1256  */
 1257 static void
 1258 htinfo_notify(struct ieee80211com *ic)
 1259 {
 1260         struct ieee80211vap *vap;
 1261         int first = 1;
 1262 
 1263         IEEE80211_LOCK_ASSERT(ic);
 1264 
 1265         TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
 1266                 if (vap->iv_opmode != IEEE80211_M_HOSTAP)
 1267                         continue;
 1268                 if (vap->iv_state != IEEE80211_S_RUN ||
 1269                     !IEEE80211_IS_CHAN_HT(vap->iv_bss->ni_chan))
 1270                         continue;
 1271                 if (first) {
 1272                         IEEE80211_NOTE(vap,
 1273                             IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N,
 1274                             vap->iv_bss,
 1275                             "HT bss occupancy change: %d sta, %d ht, "
 1276                             "%d ht40%s, HT protmode now 0x%x"
 1277                             , ic->ic_sta_assoc
 1278                             , ic->ic_ht_sta_assoc
 1279                             , ic->ic_ht40_sta_assoc
 1280                             , (ic->ic_flags_ht & IEEE80211_FHT_NONHT_PR) ?
 1281                                  ", non-HT sta present" : ""
 1282                             , ic->ic_curhtprotmode);
 1283                         first = 0;
 1284                 }
 1285                 ieee80211_beacon_notify(vap, IEEE80211_BEACON_HTINFO);
 1286         }
 1287 }
 1288 
 1289 /*
 1290  * Calculate HT protection mode from current
 1291  * state and handle updates.
 1292  */
 1293 static void
 1294 htinfo_update(struct ieee80211com *ic)
 1295 {
 1296         uint8_t protmode;
 1297 
 1298         if (ic->ic_sta_assoc != ic->ic_ht_sta_assoc) {
 1299                 protmode = IEEE80211_HTINFO_OPMODE_MIXED
 1300                          | IEEE80211_HTINFO_NONHT_PRESENT;
 1301         } else if (ic->ic_flags_ht & IEEE80211_FHT_NONHT_PR) {
 1302                 protmode = IEEE80211_HTINFO_OPMODE_PROTOPT
 1303                          | IEEE80211_HTINFO_NONHT_PRESENT;
 1304         } else if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
 1305             IEEE80211_IS_CHAN_HT40(ic->ic_bsschan) && 
 1306             ic->ic_sta_assoc != ic->ic_ht40_sta_assoc) {
 1307                 protmode = IEEE80211_HTINFO_OPMODE_HT20PR;
 1308         } else {
 1309                 protmode = IEEE80211_HTINFO_OPMODE_PURE;
 1310         }
 1311         if (protmode != ic->ic_curhtprotmode) {
 1312                 ic->ic_curhtprotmode = protmode;
 1313                 htinfo_notify(ic);
 1314         }
 1315 }
 1316 
 1317 /*
 1318  * Handle an HT station joining a BSS.
 1319  */
 1320 void
 1321 ieee80211_ht_node_join(struct ieee80211_node *ni)
 1322 {
 1323         struct ieee80211com *ic = ni->ni_ic;
 1324 
 1325         IEEE80211_LOCK_ASSERT(ic);
 1326 
 1327         if (ni->ni_flags & IEEE80211_NODE_HT) {
 1328                 ic->ic_ht_sta_assoc++;
 1329                 if (ni->ni_chw == 40)
 1330                         ic->ic_ht40_sta_assoc++;
 1331         }
 1332         htinfo_update(ic);
 1333 }
 1334 
 1335 /*
 1336  * Handle an HT station leaving a BSS.
 1337  */
 1338 void
 1339 ieee80211_ht_node_leave(struct ieee80211_node *ni)
 1340 {
 1341         struct ieee80211com *ic = ni->ni_ic;
 1342 
 1343         IEEE80211_LOCK_ASSERT(ic);
 1344 
 1345         if (ni->ni_flags & IEEE80211_NODE_HT) {
 1346                 ic->ic_ht_sta_assoc--;
 1347                 if (ni->ni_chw == 40)
 1348                         ic->ic_ht40_sta_assoc--;
 1349         }
 1350         htinfo_update(ic);
 1351 }
 1352 
 1353 /*
 1354  * Public version of htinfo_update; used for processing
 1355  * beacon frames from overlapping bss.
 1356  *
 1357  * Caller can specify either IEEE80211_HTINFO_OPMODE_MIXED
 1358  * (on receipt of a beacon that advertises MIXED) or
 1359  * IEEE80211_HTINFO_OPMODE_PROTOPT (on receipt of a beacon
 1360  * from an overlapping legacy bss).  We treat MIXED with
 1361  * a higher precedence than PROTOPT (i.e. we will not change
 1362  * change PROTOPT -> MIXED; only MIXED -> PROTOPT).  This
 1363  * corresponds to how we handle things in htinfo_update.
 1364  */
 1365 void
 1366 ieee80211_htprot_update(struct ieee80211com *ic, int protmode)
 1367 {
 1368 #define OPMODE(x)       SM(x, IEEE80211_HTINFO_OPMODE)
 1369         IEEE80211_LOCK(ic);
 1370 
 1371         /* track non-HT station presence */
 1372         KASSERT(protmode & IEEE80211_HTINFO_NONHT_PRESENT,
 1373             ("protmode 0x%x", protmode));
 1374         ic->ic_flags_ht |= IEEE80211_FHT_NONHT_PR;
 1375         ic->ic_lastnonht = ticks;
 1376 
 1377         if (protmode != ic->ic_curhtprotmode &&
 1378             (OPMODE(ic->ic_curhtprotmode) != IEEE80211_HTINFO_OPMODE_MIXED ||
 1379              OPMODE(protmode) == IEEE80211_HTINFO_OPMODE_PROTOPT)) {
 1380                 /* push beacon update */
 1381                 ic->ic_curhtprotmode = protmode;
 1382                 htinfo_notify(ic);
 1383         }
 1384         IEEE80211_UNLOCK(ic);
 1385 #undef OPMODE
 1386 }
 1387 
 1388 /*
 1389  * Time out presence of an overlapping bss with non-HT
 1390  * stations.  When operating in hostap mode we listen for
 1391  * beacons from other stations and if we identify a non-HT
 1392  * station is present we update the opmode field of the
 1393  * HTINFO ie.  To identify when all non-HT stations are
 1394  * gone we time out this condition.
 1395  */
 1396 void
 1397 ieee80211_ht_timeout(struct ieee80211com *ic)
 1398 {
 1399         IEEE80211_LOCK_ASSERT(ic);
 1400 
 1401         if ((ic->ic_flags_ht & IEEE80211_FHT_NONHT_PR) &&
 1402             ieee80211_time_after(ticks, ic->ic_lastnonht + IEEE80211_NONHT_PRESENT_AGE)) {
 1403 #if 0
 1404                 IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
 1405                     "%s", "time out non-HT STA present on channel");
 1406 #endif
 1407                 ic->ic_flags_ht &= ~IEEE80211_FHT_NONHT_PR;
 1408                 htinfo_update(ic);
 1409         }
 1410 }
 1411 
 1412 /*
 1413  * Process an 802.11n HT capabilities ie.
 1414  */
 1415 void
 1416 ieee80211_parse_htcap(struct ieee80211_node *ni, const uint8_t *ie)
 1417 {
 1418         if (ie[0] == IEEE80211_ELEMID_VENDOR) {
 1419                 /*
 1420                  * Station used Vendor OUI ie to associate;
 1421                  * mark the node so when we respond we'll use
 1422                  * the Vendor OUI's and not the standard ie's.
 1423                  */
 1424                 ni->ni_flags |= IEEE80211_NODE_HTCOMPAT;
 1425                 ie += 4;
 1426         } else
 1427                 ni->ni_flags &= ~IEEE80211_NODE_HTCOMPAT;
 1428 
 1429         ni->ni_htcap = le16dec(ie +
 1430                 __offsetof(struct ieee80211_ie_htcap, hc_cap));
 1431         ni->ni_htparam = ie[__offsetof(struct ieee80211_ie_htcap, hc_param)];
 1432 }
 1433 
 1434 static void
 1435 htinfo_parse(struct ieee80211_node *ni,
 1436         const struct ieee80211_ie_htinfo *htinfo)
 1437 {
 1438         uint16_t w;
 1439 
 1440         ni->ni_htctlchan = htinfo->hi_ctrlchannel;
 1441         ni->ni_ht2ndchan = SM(htinfo->hi_byte1, IEEE80211_HTINFO_2NDCHAN);
 1442         w = le16dec(&htinfo->hi_byte2);
 1443         ni->ni_htopmode = SM(w, IEEE80211_HTINFO_OPMODE);
 1444         w = le16dec(&htinfo->hi_byte45);
 1445         ni->ni_htstbc = SM(w, IEEE80211_HTINFO_BASIC_STBCMCS);
 1446 }
 1447 
 1448 /*
 1449  * Parse an 802.11n HT info ie and save useful information
 1450  * to the node state.  Note this does not effect any state
 1451  * changes such as for channel width change.
 1452  */
 1453 void
 1454 ieee80211_parse_htinfo(struct ieee80211_node *ni, const uint8_t *ie)
 1455 {
 1456         if (ie[0] == IEEE80211_ELEMID_VENDOR)
 1457                 ie += 4;
 1458         htinfo_parse(ni, (const struct ieee80211_ie_htinfo *) ie);
 1459 }
 1460 
 1461 /*
 1462  * Handle 11n channel switch.  Use the received HT ie's to
 1463  * identify the right channel to use.  If we cannot locate it
 1464  * in the channel table then fallback to legacy operation.
 1465  * Note that we use this information to identify the node's
 1466  * channel only; the caller is responsible for insuring any
 1467  * required channel change is done (e.g. in sta mode when
 1468  * parsing the contents of a beacon frame).
 1469  */
 1470 static int
 1471 htinfo_update_chw(struct ieee80211_node *ni, int htflags)
 1472 {
 1473         struct ieee80211com *ic = ni->ni_ic;
 1474         struct ieee80211_channel *c;
 1475         int chanflags;
 1476         int ret = 0;
 1477 
 1478         chanflags = (ni->ni_chan->ic_flags &~ IEEE80211_CHAN_HT) | htflags;
 1479         if (chanflags != ni->ni_chan->ic_flags) {
 1480                 /* XXX not right for ht40- */
 1481                 c = ieee80211_find_channel(ic, ni->ni_chan->ic_freq, chanflags);
 1482                 if (c == NULL && (htflags & IEEE80211_CHAN_HT40)) {
 1483                         /*
 1484                          * No HT40 channel entry in our table; fall back
 1485                          * to HT20 operation.  This should not happen.
 1486                          */
 1487                         c = findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT20);
 1488 #if 0
 1489                         IEEE80211_NOTE(ni->ni_vap,
 1490                             IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
 1491                             "no HT40 channel (freq %u), falling back to HT20",
 1492                             ni->ni_chan->ic_freq);
 1493 #endif
 1494                         /* XXX stat */
 1495                 }
 1496                 if (c != NULL && c != ni->ni_chan) {
 1497                         IEEE80211_NOTE(ni->ni_vap,
 1498                             IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
 1499                             "switch station to HT%d channel %u/0x%x",
 1500                             IEEE80211_IS_CHAN_HT40(c) ? 40 : 20,
 1501                             c->ic_freq, c->ic_flags);
 1502                         ni->ni_chan = c;
 1503                         ret = 1;
 1504                 }
 1505                 /* NB: caller responsible for forcing any channel change */
 1506         }
 1507         /* update node's tx channel width */
 1508         ni->ni_chw = IEEE80211_IS_CHAN_HT40(ni->ni_chan)? 40 : 20;
 1509         return (ret);
 1510 }
 1511 
 1512 /*
 1513  * Update 11n MIMO PS state according to received htcap.
 1514  */
 1515 static __inline int
 1516 htcap_update_mimo_ps(struct ieee80211_node *ni)
 1517 {
 1518         uint16_t oflags = ni->ni_flags;
 1519 
 1520         switch (ni->ni_htcap & IEEE80211_HTCAP_SMPS) {
 1521         case IEEE80211_HTCAP_SMPS_DYNAMIC:
 1522                 ni->ni_flags |= IEEE80211_NODE_MIMO_PS;
 1523                 ni->ni_flags |= IEEE80211_NODE_MIMO_RTS;
 1524                 break;
 1525         case IEEE80211_HTCAP_SMPS_ENA:
 1526                 ni->ni_flags |= IEEE80211_NODE_MIMO_PS;
 1527                 ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS;
 1528                 break;
 1529         case IEEE80211_HTCAP_SMPS_OFF:
 1530         default:                /* disable on rx of reserved value */
 1531                 ni->ni_flags &= ~IEEE80211_NODE_MIMO_PS;
 1532                 ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS;
 1533                 break;
 1534         }
 1535         return (oflags ^ ni->ni_flags);
 1536 }
 1537 
 1538 /*
 1539  * Update short GI state according to received htcap
 1540  * and local settings.
 1541  */
 1542 static __inline void
 1543 htcap_update_shortgi(struct ieee80211_node *ni)
 1544 {
 1545         struct ieee80211vap *vap = ni->ni_vap;
 1546 
 1547         ni->ni_flags &= ~(IEEE80211_NODE_SGI20|IEEE80211_NODE_SGI40);
 1548         if ((ni->ni_htcap & IEEE80211_HTCAP_SHORTGI20) &&
 1549             (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20))
 1550                 ni->ni_flags |= IEEE80211_NODE_SGI20;
 1551         if ((ni->ni_htcap & IEEE80211_HTCAP_SHORTGI40) &&
 1552             (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40))
 1553                 ni->ni_flags |= IEEE80211_NODE_SGI40;
 1554 }
 1555 
 1556 /*
 1557  * Parse and update HT-related state extracted from
 1558  * the HT cap and info ie's.
 1559  */
 1560 int
 1561 ieee80211_ht_updateparams(struct ieee80211_node *ni,
 1562         const uint8_t *htcapie, const uint8_t *htinfoie)
 1563 {
 1564         struct ieee80211vap *vap = ni->ni_vap;
 1565         const struct ieee80211_ie_htinfo *htinfo;
 1566         int htflags;
 1567         int ret = 0;
 1568 
 1569         ieee80211_parse_htcap(ni, htcapie);
 1570         if (vap->iv_htcaps & IEEE80211_HTC_SMPS)
 1571                 htcap_update_mimo_ps(ni);
 1572         htcap_update_shortgi(ni);
 1573 
 1574         if (htinfoie[0] == IEEE80211_ELEMID_VENDOR)
 1575                 htinfoie += 4;
 1576         htinfo = (const struct ieee80211_ie_htinfo *) htinfoie;
 1577         htinfo_parse(ni, htinfo);
 1578 
 1579         htflags = (vap->iv_flags_ht & IEEE80211_FHT_HT) ?
 1580             IEEE80211_CHAN_HT20 : 0;
 1581         /* NB: honor operating mode constraint */
 1582         if ((htinfo->hi_byte1 & IEEE80211_HTINFO_TXWIDTH_2040) &&
 1583             (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)) {
 1584                 if (ni->ni_ht2ndchan == IEEE80211_HTINFO_2NDCHAN_ABOVE)
 1585                         htflags = IEEE80211_CHAN_HT40U;
 1586                 else if (ni->ni_ht2ndchan == IEEE80211_HTINFO_2NDCHAN_BELOW)
 1587                         htflags = IEEE80211_CHAN_HT40D;
 1588         }
 1589         if (htinfo_update_chw(ni, htflags))
 1590                 ret = 1;
 1591 
 1592         if ((htinfo->hi_byte1 & IEEE80211_HTINFO_RIFSMODE_PERM) &&
 1593             (vap->iv_flags_ht & IEEE80211_FHT_RIFS))
 1594                 ni->ni_flags |= IEEE80211_NODE_RIFS;
 1595         else
 1596                 ni->ni_flags &= ~IEEE80211_NODE_RIFS;
 1597 
 1598         return (ret);
 1599 }
 1600 
 1601 /*
 1602  * Parse and update HT-related state extracted from the HT cap ie
 1603  * for a station joining an HT BSS.
 1604  */
 1605 void
 1606 ieee80211_ht_updatehtcap(struct ieee80211_node *ni, const uint8_t *htcapie)
 1607 {
 1608         struct ieee80211vap *vap = ni->ni_vap;
 1609         int htflags;
 1610 
 1611         ieee80211_parse_htcap(ni, htcapie);
 1612         if (vap->iv_htcaps & IEEE80211_HTC_SMPS)
 1613                 htcap_update_mimo_ps(ni);
 1614         htcap_update_shortgi(ni);
 1615 
 1616         /* NB: honor operating mode constraint */
 1617         /* XXX 40 MHz intolerant */
 1618         htflags = (vap->iv_flags_ht & IEEE80211_FHT_HT) ?
 1619             IEEE80211_CHAN_HT20 : 0;
 1620         if ((ni->ni_htcap & IEEE80211_HTCAP_CHWIDTH40) &&
 1621             (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)) {
 1622                 if (IEEE80211_IS_CHAN_HT40U(vap->iv_bss->ni_chan))
 1623                         htflags = IEEE80211_CHAN_HT40U;
 1624                 else if (IEEE80211_IS_CHAN_HT40D(vap->iv_bss->ni_chan))
 1625                         htflags = IEEE80211_CHAN_HT40D;
 1626         }
 1627         (void) htinfo_update_chw(ni, htflags);
 1628 }
 1629 
 1630 /*
 1631  * Install received HT rate set by parsing the HT cap ie.
 1632  */
 1633 int
 1634 ieee80211_setup_htrates(struct ieee80211_node *ni, const uint8_t *ie, int flags)
 1635 {
 1636         struct ieee80211com *ic = ni->ni_ic;
 1637         struct ieee80211vap *vap = ni->ni_vap;
 1638         const struct ieee80211_ie_htcap *htcap;
 1639         struct ieee80211_htrateset *rs;
 1640         int i, maxequalmcs, maxunequalmcs;
 1641 
 1642         maxequalmcs = ic->ic_txstream * 8 - 1;
 1643         maxunequalmcs = 0;
 1644         if (ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL) {
 1645                 if (ic->ic_txstream >= 2)
 1646                         maxunequalmcs = 38;
 1647                 if (ic->ic_txstream >= 3)
 1648                         maxunequalmcs = 52;
 1649                 if (ic->ic_txstream >= 4)
 1650                         maxunequalmcs = 76;
 1651         }
 1652 
 1653         rs = &ni->ni_htrates;
 1654         memset(rs, 0, sizeof(*rs));
 1655         if (ie != NULL) {
 1656                 if (ie[0] == IEEE80211_ELEMID_VENDOR)
 1657                         ie += 4;
 1658                 htcap = (const struct ieee80211_ie_htcap *) ie;
 1659                 for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++) {
 1660                         if (isclr(htcap->hc_mcsset, i))
 1661                                 continue;
 1662                         if (rs->rs_nrates == IEEE80211_HTRATE_MAXSIZE) {
 1663                                 IEEE80211_NOTE(vap,
 1664                                     IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
 1665                                     "WARNING, HT rate set too large; only "
 1666                                     "using %u rates", IEEE80211_HTRATE_MAXSIZE);
 1667                                 vap->iv_stats.is_rx_rstoobig++;
 1668                                 break;
 1669                         }
 1670                         if (i <= 31 && i > maxequalmcs)
 1671                                 continue;
 1672                         if (i == 32 &&
 1673                             (ic->ic_htcaps & IEEE80211_HTC_TXMCS32) == 0)
 1674                                 continue;
 1675                         if (i > 32 && i > maxunequalmcs)
 1676                                 continue;
 1677                         rs->rs_rates[rs->rs_nrates++] = i;
 1678                 }
 1679         }
 1680         return ieee80211_fix_rate(ni, (struct ieee80211_rateset *) rs, flags);
 1681 }
 1682 
 1683 /*
 1684  * Mark rates in a node's HT rate set as basic according
 1685  * to the information in the supplied HT info ie.
 1686  */
 1687 void
 1688 ieee80211_setup_basic_htrates(struct ieee80211_node *ni, const uint8_t *ie)
 1689 {
 1690         const struct ieee80211_ie_htinfo *htinfo;
 1691         struct ieee80211_htrateset *rs;
 1692         int i, j;
 1693 
 1694         if (ie[0] == IEEE80211_ELEMID_VENDOR)
 1695                 ie += 4;
 1696         htinfo = (const struct ieee80211_ie_htinfo *) ie;
 1697         rs = &ni->ni_htrates;
 1698         if (rs->rs_nrates == 0) {
 1699                 IEEE80211_NOTE(ni->ni_vap,
 1700                     IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
 1701                     "%s", "WARNING, empty HT rate set");
 1702                 return;
 1703         }
 1704         for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++) {
 1705                 if (isclr(htinfo->hi_basicmcsset, i))
 1706                         continue;
 1707                 for (j = 0; j < rs->rs_nrates; j++)
 1708                         if ((rs->rs_rates[j] & IEEE80211_RATE_VAL) == i)
 1709                                 rs->rs_rates[j] |= IEEE80211_RATE_BASIC;
 1710         }
 1711 }
 1712 
 1713 static void
 1714 ampdu_tx_setup(struct ieee80211_tx_ampdu *tap)
 1715 {
 1716         callout_init(&tap->txa_timer, 1);
 1717         tap->txa_flags |= IEEE80211_AGGR_SETUP;
 1718         tap->txa_lastsample = ticks;
 1719 }
 1720 
 1721 static void
 1722 ampdu_tx_stop(struct ieee80211_tx_ampdu *tap)
 1723 {
 1724         struct ieee80211_node *ni = tap->txa_ni;
 1725         struct ieee80211com *ic = ni->ni_ic;
 1726 
 1727         IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
 1728             tap->txa_ni,
 1729             "%s: called",
 1730             __func__);
 1731 
 1732         KASSERT(tap->txa_flags & IEEE80211_AGGR_SETUP,
 1733             ("txa_flags 0x%x tid %d ac %d", tap->txa_flags, tap->txa_tid,
 1734             TID_TO_WME_AC(tap->txa_tid)));
 1735 
 1736         /*
 1737          * Stop BA stream if setup so driver has a chance
 1738          * to reclaim any resources it might have allocated.
 1739          */
 1740         ic->ic_addba_stop(ni, tap);
 1741         /*
 1742          * Stop any pending BAR transmit.
 1743          */
 1744         bar_stop_timer(tap);
 1745 
 1746         /*
 1747          * Reset packet estimate.
 1748          */
 1749         ieee80211_txampdu_init_pps(tap);
 1750 
 1751         /* NB: clearing NAK means we may re-send ADDBA */ 
 1752         tap->txa_flags &= ~(IEEE80211_AGGR_SETUP | IEEE80211_AGGR_NAK);
 1753 }
 1754 
 1755 /*
 1756  * ADDBA response timeout.
 1757  *
 1758  * If software aggregation and per-TID queue management was done here,
 1759  * that queue would be unpaused after the ADDBA timeout occurs.
 1760  */
 1761 static void
 1762 addba_timeout(void *arg)
 1763 {
 1764         struct ieee80211_tx_ampdu *tap = arg;
 1765         struct ieee80211_node *ni = tap->txa_ni;
 1766         struct ieee80211com *ic = ni->ni_ic;
 1767 
 1768         /* XXX ? */
 1769         tap->txa_flags &= ~IEEE80211_AGGR_XCHGPEND;
 1770         tap->txa_attempts++;
 1771         ic->ic_addba_response_timeout(ni, tap);
 1772 }
 1773 
 1774 static void
 1775 addba_start_timeout(struct ieee80211_tx_ampdu *tap)
 1776 {
 1777         /* XXX use CALLOUT_PENDING instead? */
 1778         callout_reset(&tap->txa_timer, ieee80211_addba_timeout,
 1779             addba_timeout, tap);
 1780         tap->txa_flags |= IEEE80211_AGGR_XCHGPEND;
 1781         tap->txa_nextrequest = ticks + ieee80211_addba_timeout;
 1782 }
 1783 
 1784 static void
 1785 addba_stop_timeout(struct ieee80211_tx_ampdu *tap)
 1786 {
 1787         /* XXX use CALLOUT_PENDING instead? */
 1788         if (tap->txa_flags & IEEE80211_AGGR_XCHGPEND) {
 1789                 callout_stop(&tap->txa_timer);
 1790                 tap->txa_flags &= ~IEEE80211_AGGR_XCHGPEND;
 1791         }
 1792 }
 1793 
 1794 static void
 1795 null_addba_response_timeout(struct ieee80211_node *ni,
 1796     struct ieee80211_tx_ampdu *tap)
 1797 {
 1798 }
 1799 
 1800 /*
 1801  * Default method for requesting A-MPDU tx aggregation.
 1802  * We setup the specified state block and start a timer
 1803  * to wait for an ADDBA response frame.
 1804  */
 1805 static int
 1806 ieee80211_addba_request(struct ieee80211_node *ni,
 1807         struct ieee80211_tx_ampdu *tap,
 1808         int dialogtoken, int baparamset, int batimeout)
 1809 {
 1810         int bufsiz;
 1811 
 1812         /* XXX locking */
 1813         tap->txa_token = dialogtoken;
 1814         tap->txa_flags |= IEEE80211_AGGR_IMMEDIATE;
 1815         bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
 1816         tap->txa_wnd = (bufsiz == 0) ?
 1817             IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
 1818         addba_start_timeout(tap);
 1819         return 1;
 1820 }
 1821 
 1822 /*
 1823  * Called by drivers that wish to request an ADDBA session be
 1824  * setup.  This brings it up and starts the request timer.
 1825  */
 1826 int
 1827 ieee80211_ampdu_tx_request_ext(struct ieee80211_node *ni, int tid)
 1828 {
 1829         struct ieee80211_tx_ampdu *tap;
 1830 
 1831         if (tid < 0 || tid > 15)
 1832                 return (0);
 1833         tap = &ni->ni_tx_ampdu[tid];
 1834 
 1835         /* XXX locking */
 1836         if ((tap->txa_flags & IEEE80211_AGGR_SETUP) == 0) {
 1837                 /* do deferred setup of state */
 1838                 ampdu_tx_setup(tap);
 1839         }
 1840         /* XXX hack for not doing proper locking */
 1841         tap->txa_flags &= ~IEEE80211_AGGR_NAK;
 1842         addba_start_timeout(tap);
 1843         return (1);
 1844 }
 1845 
 1846 /*
 1847  * Called by drivers that have marked a session as active.
 1848  */
 1849 int
 1850 ieee80211_ampdu_tx_request_active_ext(struct ieee80211_node *ni, int tid,
 1851     int status)
 1852 {
 1853         struct ieee80211_tx_ampdu *tap;
 1854 
 1855         if (tid < 0 || tid > 15)
 1856                 return (0);
 1857         tap = &ni->ni_tx_ampdu[tid];
 1858 
 1859         /* XXX locking */
 1860         addba_stop_timeout(tap);
 1861         if (status == 1) {
 1862                 tap->txa_flags |= IEEE80211_AGGR_RUNNING;
 1863                 tap->txa_attempts = 0;
 1864         } else {
 1865                 /* mark tid so we don't try again */
 1866                 tap->txa_flags |= IEEE80211_AGGR_NAK;
 1867         }
 1868         return (1);
 1869 }
 1870 
 1871 /*
 1872  * Default method for processing an A-MPDU tx aggregation
 1873  * response.  We shutdown any pending timer and update the
 1874  * state block according to the reply.
 1875  */
 1876 static int
 1877 ieee80211_addba_response(struct ieee80211_node *ni,
 1878         struct ieee80211_tx_ampdu *tap,
 1879         int status, int baparamset, int batimeout)
 1880 {
 1881         int bufsiz, tid;
 1882 
 1883         /* XXX locking */
 1884         addba_stop_timeout(tap);
 1885         if (status == IEEE80211_STATUS_SUCCESS) {
 1886                 bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
 1887                 /* XXX override our request? */
 1888                 tap->txa_wnd = (bufsiz == 0) ?
 1889                     IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
 1890                 /* XXX AC/TID */
 1891                 tid = MS(baparamset, IEEE80211_BAPS_TID);
 1892                 tap->txa_flags |= IEEE80211_AGGR_RUNNING;
 1893                 tap->txa_attempts = 0;
 1894         } else {
 1895                 /* mark tid so we don't try again */
 1896                 tap->txa_flags |= IEEE80211_AGGR_NAK;
 1897         }
 1898         return 1;
 1899 }
 1900 
 1901 /*
 1902  * Default method for stopping A-MPDU tx aggregation.
 1903  * Any timer is cleared and we drain any pending frames.
 1904  */
 1905 static void
 1906 ieee80211_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
 1907 {
 1908         /* XXX locking */
 1909         addba_stop_timeout(tap);
 1910         if (tap->txa_flags & IEEE80211_AGGR_RUNNING) {
 1911                 /* XXX clear aggregation queue */
 1912                 tap->txa_flags &= ~IEEE80211_AGGR_RUNNING;
 1913         }
 1914         tap->txa_attempts = 0;
 1915 }
 1916 
 1917 /*
 1918  * Process a received action frame using the default aggregation
 1919  * policy.  We intercept ADDBA-related frames and use them to
 1920  * update our aggregation state.  All other frames are passed up
 1921  * for processing by ieee80211_recv_action.
 1922  */
 1923 static int
 1924 ht_recv_action_ba_addba_request(struct ieee80211_node *ni,
 1925         const struct ieee80211_frame *wh,
 1926         const uint8_t *frm, const uint8_t *efrm)
 1927 {
 1928         struct ieee80211com *ic = ni->ni_ic;
 1929         struct ieee80211vap *vap = ni->ni_vap;
 1930         struct ieee80211_rx_ampdu *rap;
 1931         uint8_t dialogtoken;
 1932         uint16_t baparamset, batimeout, baseqctl;
 1933         uint16_t args[5];
 1934         int tid;
 1935 
 1936         dialogtoken = frm[2];
 1937         baparamset = le16dec(frm+3);
 1938         batimeout = le16dec(frm+5);
 1939         baseqctl = le16dec(frm+7);
 1940 
 1941         tid = MS(baparamset, IEEE80211_BAPS_TID);
 1942 
 1943         IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
 1944             "recv ADDBA request: dialogtoken %u baparamset 0x%x "
 1945             "(tid %d bufsiz %d) batimeout %d baseqctl %d:%d",
 1946             dialogtoken, baparamset,
 1947             tid, MS(baparamset, IEEE80211_BAPS_BUFSIZ),
 1948             batimeout,
 1949             MS(baseqctl, IEEE80211_BASEQ_START),
 1950             MS(baseqctl, IEEE80211_BASEQ_FRAG));
 1951 
 1952         rap = &ni->ni_rx_ampdu[tid];
 1953 
 1954         /* Send ADDBA response */
 1955         args[0] = dialogtoken;
 1956         /*
 1957          * NB: We ack only if the sta associated with HT and
 1958          * the ap is configured to do AMPDU rx (the latter
 1959          * violates the 11n spec and is mostly for testing).
 1960          */
 1961         if ((ni->ni_flags & IEEE80211_NODE_AMPDU_RX) &&
 1962             (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_RX)) {
 1963                 /* XXX handle ampdu_rx_start failure */
 1964                 ic->ic_ampdu_rx_start(ni, rap,
 1965                     baparamset, batimeout, baseqctl);
 1966 
 1967                 args[1] = IEEE80211_STATUS_SUCCESS;
 1968         } else {
 1969                 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
 1970                     ni, "reject ADDBA request: %s",
 1971                     ni->ni_flags & IEEE80211_NODE_AMPDU_RX ?
 1972                        "administratively disabled" :
 1973                        "not negotiated for station");
 1974                 vap->iv_stats.is_addba_reject++;
 1975                 args[1] = IEEE80211_STATUS_UNSPECIFIED;
 1976         }
 1977         /* XXX honor rap flags? */
 1978         args[2] = IEEE80211_BAPS_POLICY_IMMEDIATE
 1979                 | SM(tid, IEEE80211_BAPS_TID)
 1980                 | SM(rap->rxa_wnd, IEEE80211_BAPS_BUFSIZ)
 1981                 ;
 1982         args[3] = 0;
 1983         args[4] = 0;
 1984         ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
 1985                 IEEE80211_ACTION_BA_ADDBA_RESPONSE, args);
 1986         return 0;
 1987 }
 1988 
 1989 static int
 1990 ht_recv_action_ba_addba_response(struct ieee80211_node *ni,
 1991         const struct ieee80211_frame *wh,
 1992         const uint8_t *frm, const uint8_t *efrm)
 1993 {
 1994         struct ieee80211com *ic = ni->ni_ic;
 1995         struct ieee80211vap *vap = ni->ni_vap;
 1996         struct ieee80211_tx_ampdu *tap;
 1997         uint8_t dialogtoken, policy;
 1998         uint16_t baparamset, batimeout, code;
 1999         int tid, bufsiz;
 2000 
 2001         dialogtoken = frm[2];
 2002         code = le16dec(frm+3);
 2003         baparamset = le16dec(frm+5);
 2004         tid = MS(baparamset, IEEE80211_BAPS_TID);
 2005         bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
 2006         policy = MS(baparamset, IEEE80211_BAPS_POLICY);
 2007         batimeout = le16dec(frm+7);
 2008 
 2009         tap = &ni->ni_tx_ampdu[tid];
 2010         if ((tap->txa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
 2011                 IEEE80211_DISCARD_MAC(vap,
 2012                     IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
 2013                     ni->ni_macaddr, "ADDBA response",
 2014                     "no pending ADDBA, tid %d dialogtoken %u "
 2015                     "code %d", tid, dialogtoken, code);
 2016                 vap->iv_stats.is_addba_norequest++;
 2017                 return 0;
 2018         }
 2019         if (dialogtoken != tap->txa_token) {
 2020                 IEEE80211_DISCARD_MAC(vap,
 2021                     IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
 2022                     ni->ni_macaddr, "ADDBA response",
 2023                     "dialogtoken mismatch: waiting for %d, "
 2024                     "received %d, tid %d code %d",
 2025                     tap->txa_token, dialogtoken, tid, code);
 2026                 vap->iv_stats.is_addba_badtoken++;
 2027                 return 0;
 2028         }
 2029         /* NB: assumes IEEE80211_AGGR_IMMEDIATE is 1 */
 2030         if (policy != (tap->txa_flags & IEEE80211_AGGR_IMMEDIATE)) {
 2031                 IEEE80211_DISCARD_MAC(vap,
 2032                     IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
 2033                     ni->ni_macaddr, "ADDBA response",
 2034                     "policy mismatch: expecting %s, "
 2035                     "received %s, tid %d code %d",
 2036                     tap->txa_flags & IEEE80211_AGGR_IMMEDIATE,
 2037                     policy, tid, code);
 2038                 vap->iv_stats.is_addba_badpolicy++;
 2039                 return 0;
 2040         }
 2041 #if 0
 2042         /* XXX we take MIN in ieee80211_addba_response */
 2043         if (bufsiz > IEEE80211_AGGR_BAWMAX) {
 2044                 IEEE80211_DISCARD_MAC(vap,
 2045                     IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
 2046                     ni->ni_macaddr, "ADDBA response",
 2047                     "BA window too large: max %d, "
 2048                     "received %d, tid %d code %d",
 2049                     bufsiz, IEEE80211_AGGR_BAWMAX, tid, code);
 2050                 vap->iv_stats.is_addba_badbawinsize++;
 2051                 return 0;
 2052         }
 2053 #endif
 2054         IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
 2055             "recv ADDBA response: dialogtoken %u code %d "
 2056             "baparamset 0x%x (tid %d bufsiz %d) batimeout %d",
 2057             dialogtoken, code, baparamset, tid, bufsiz,
 2058             batimeout);
 2059         ic->ic_addba_response(ni, tap, code, baparamset, batimeout);
 2060         return 0;
 2061 }
 2062 
 2063 static int
 2064 ht_recv_action_ba_delba(struct ieee80211_node *ni,
 2065         const struct ieee80211_frame *wh,
 2066         const uint8_t *frm, const uint8_t *efrm)
 2067 {
 2068         struct ieee80211com *ic = ni->ni_ic;
 2069         struct ieee80211_rx_ampdu *rap;
 2070         struct ieee80211_tx_ampdu *tap;
 2071         uint16_t baparamset, code;
 2072         int tid;
 2073 
 2074         baparamset = le16dec(frm+2);
 2075         code = le16dec(frm+4);
 2076 
 2077         tid = MS(baparamset, IEEE80211_DELBAPS_TID);
 2078 
 2079         IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
 2080             "recv DELBA: baparamset 0x%x (tid %d initiator %d) "
 2081             "code %d", baparamset, tid,
 2082             MS(baparamset, IEEE80211_DELBAPS_INIT), code);
 2083 
 2084         if ((baparamset & IEEE80211_DELBAPS_INIT) == 0) {
 2085                 tap = &ni->ni_tx_ampdu[tid];
 2086                 ic->ic_addba_stop(ni, tap);
 2087         } else {
 2088                 rap = &ni->ni_rx_ampdu[tid];
 2089                 ic->ic_ampdu_rx_stop(ni, rap);
 2090         }
 2091         return 0;
 2092 }
 2093 
 2094 static int
 2095 ht_recv_action_ht_txchwidth(struct ieee80211_node *ni,
 2096         const struct ieee80211_frame *wh,
 2097         const uint8_t *frm, const uint8_t *efrm)
 2098 {
 2099         int chw;
 2100 
 2101         chw = (frm[2] == IEEE80211_A_HT_TXCHWIDTH_2040) ? 40 : 20;
 2102 
 2103         IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
 2104             "%s: HT txchwidth, width %d%s",
 2105             __func__, chw, ni->ni_chw != chw ? "*" : "");
 2106         if (chw != ni->ni_chw) {
 2107                 ni->ni_chw = chw;
 2108                 /* XXX notify on change */
 2109         }
 2110         return 0;
 2111 }
 2112 
 2113 static int
 2114 ht_recv_action_ht_mimopwrsave(struct ieee80211_node *ni,
 2115         const struct ieee80211_frame *wh,
 2116         const uint8_t *frm, const uint8_t *efrm)
 2117 {
 2118         const struct ieee80211_action_ht_mimopowersave *mps =
 2119             (const struct ieee80211_action_ht_mimopowersave *) frm;
 2120 
 2121         /* XXX check iv_htcaps */
 2122         if (mps->am_control & IEEE80211_A_HT_MIMOPWRSAVE_ENA)
 2123                 ni->ni_flags |= IEEE80211_NODE_MIMO_PS;
 2124         else
 2125                 ni->ni_flags &= ~IEEE80211_NODE_MIMO_PS;
 2126         if (mps->am_control & IEEE80211_A_HT_MIMOPWRSAVE_MODE)
 2127                 ni->ni_flags |= IEEE80211_NODE_MIMO_RTS;
 2128         else
 2129                 ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS;
 2130         /* XXX notify on change */
 2131         IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
 2132             "%s: HT MIMO PS (%s%s)", __func__,
 2133             (ni->ni_flags & IEEE80211_NODE_MIMO_PS) ?  "on" : "off",
 2134             (ni->ni_flags & IEEE80211_NODE_MIMO_RTS) ?  "+rts" : ""
 2135         );
 2136         return 0;
 2137 }
 2138 
 2139 /*
 2140  * Transmit processing.
 2141  */
 2142 
 2143 /*
 2144  * Check if A-MPDU should be requested/enabled for a stream.
 2145  * We require a traffic rate above a per-AC threshold and we
 2146  * also handle backoff from previous failed attempts.
 2147  *
 2148  * Drivers may override this method to bring in information
 2149  * such as link state conditions in making the decision.
 2150  */
 2151 static int
 2152 ieee80211_ampdu_enable(struct ieee80211_node *ni,
 2153         struct ieee80211_tx_ampdu *tap)
 2154 {
 2155         struct ieee80211vap *vap = ni->ni_vap;
 2156 
 2157         if (tap->txa_avgpps <
 2158             vap->iv_ampdu_mintraffic[TID_TO_WME_AC(tap->txa_tid)])
 2159                 return 0;
 2160         /* XXX check rssi? */
 2161         if (tap->txa_attempts >= ieee80211_addba_maxtries &&
 2162             ieee80211_time_after(ticks, tap->txa_nextrequest)) {
 2163                 /*
 2164                  * Don't retry too often; txa_nextrequest is set
 2165                  * to the minimum interval we'll retry after
 2166                  * ieee80211_addba_maxtries failed attempts are made.
 2167                  */
 2168                 return 0;
 2169         }
 2170         IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
 2171             "enable AMPDU on tid %d (%s), avgpps %d pkts %d attempt %d",
 2172             tap->txa_tid, ieee80211_wme_acnames[TID_TO_WME_AC(tap->txa_tid)],
 2173             tap->txa_avgpps, tap->txa_pkts, tap->txa_attempts);
 2174         return 1;
 2175 }
 2176 
 2177 /*
 2178  * Request A-MPDU tx aggregation.  Setup local state and
 2179  * issue an ADDBA request.  BA use will only happen after
 2180  * the other end replies with ADDBA response.
 2181  */
 2182 int
 2183 ieee80211_ampdu_request(struct ieee80211_node *ni,
 2184         struct ieee80211_tx_ampdu *tap)
 2185 {
 2186         struct ieee80211com *ic = ni->ni_ic;
 2187         uint16_t args[5];
 2188         int tid, dialogtoken;
 2189         static int tokens = 0;  /* XXX */
 2190 
 2191         /* XXX locking */
 2192         if ((tap->txa_flags & IEEE80211_AGGR_SETUP) == 0) {
 2193                 /* do deferred setup of state */
 2194                 ampdu_tx_setup(tap);
 2195         }
 2196         /* XXX hack for not doing proper locking */
 2197         tap->txa_flags &= ~IEEE80211_AGGR_NAK;
 2198 
 2199         dialogtoken = (tokens+1) % 63;          /* XXX */
 2200         tid = tap->txa_tid;
 2201         tap->txa_start = ni->ni_txseqs[tid];
 2202 
 2203         args[0] = dialogtoken;
 2204         args[1] = 0;    /* NB: status code not used */
 2205         args[2] = IEEE80211_BAPS_POLICY_IMMEDIATE
 2206                 | SM(tid, IEEE80211_BAPS_TID)
 2207                 | SM(IEEE80211_AGGR_BAWMAX, IEEE80211_BAPS_BUFSIZ)
 2208                 ;
 2209         args[3] = 0;    /* batimeout */
 2210         /* NB: do first so there's no race against reply */
 2211         if (!ic->ic_addba_request(ni, tap, dialogtoken, args[2], args[3])) {
 2212                 /* unable to setup state, don't make request */
 2213                 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
 2214                     ni, "%s: could not setup BA stream for TID %d AC %d",
 2215                     __func__, tap->txa_tid, TID_TO_WME_AC(tap->txa_tid));
 2216                 /* defer next try so we don't slam the driver with requests */
 2217                 tap->txa_attempts = ieee80211_addba_maxtries;
 2218                 /* NB: check in case driver wants to override */
 2219                 if (tap->txa_nextrequest <= ticks)
 2220                         tap->txa_nextrequest = ticks + ieee80211_addba_backoff;
 2221                 return 0;
 2222         }
 2223         tokens = dialogtoken;                   /* allocate token */
 2224         /* NB: after calling ic_addba_request so driver can set txa_start */
 2225         args[4] = SM(tap->txa_start, IEEE80211_BASEQ_START)
 2226                 | SM(0, IEEE80211_BASEQ_FRAG)
 2227                 ;
 2228         return ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
 2229                 IEEE80211_ACTION_BA_ADDBA_REQUEST, args);
 2230 }
 2231 
 2232 /*
 2233  * Terminate an AMPDU tx stream.  State is reclaimed
 2234  * and the peer notified with a DelBA Action frame.
 2235  */
 2236 void
 2237 ieee80211_ampdu_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
 2238         int reason)
 2239 {
 2240         struct ieee80211com *ic = ni->ni_ic;
 2241         struct ieee80211vap *vap = ni->ni_vap;
 2242         uint16_t args[4];
 2243 
 2244         /* XXX locking */
 2245         tap->txa_flags &= ~IEEE80211_AGGR_BARPEND;
 2246         if (IEEE80211_AMPDU_RUNNING(tap)) {
 2247                 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
 2248                     ni, "%s: stop BA stream for TID %d (reason: %d (%s))",
 2249                     __func__, tap->txa_tid, reason,
 2250                     ieee80211_reason_to_string(reason));
 2251                 vap->iv_stats.is_ampdu_stop++;
 2252 
 2253                 ic->ic_addba_stop(ni, tap);
 2254                 args[0] = tap->txa_tid;
 2255                 args[1] = IEEE80211_DELBAPS_INIT;
 2256                 args[2] = reason;                       /* XXX reason code */
 2257                 ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
 2258                         IEEE80211_ACTION_BA_DELBA, args);
 2259         } else {
 2260                 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
 2261                     ni, "%s: BA stream for TID %d not running "
 2262                     "(reason: %d (%s))", __func__, tap->txa_tid, reason,
 2263                     ieee80211_reason_to_string(reason));
 2264                 vap->iv_stats.is_ampdu_stop_failed++;
 2265         }
 2266 }
 2267 
 2268 /* XXX */
 2269 static void bar_start_timer(struct ieee80211_tx_ampdu *tap);
 2270 
 2271 static void
 2272 bar_timeout(void *arg)
 2273 {
 2274         struct ieee80211_tx_ampdu *tap = arg;
 2275         struct ieee80211_node *ni = tap->txa_ni;
 2276 
 2277         KASSERT((tap->txa_flags & IEEE80211_AGGR_XCHGPEND) == 0,
 2278             ("bar/addba collision, flags 0x%x", tap->txa_flags));
 2279 
 2280         IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
 2281             ni, "%s: tid %u flags 0x%x attempts %d", __func__,
 2282             tap->txa_tid, tap->txa_flags, tap->txa_attempts);
 2283 
 2284         /* guard against race with bar_tx_complete */
 2285         if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) == 0)
 2286                 return;
 2287         /* XXX ? */
 2288         if (tap->txa_attempts >= ieee80211_bar_maxtries) {
 2289                 struct ieee80211com *ic = ni->ni_ic;
 2290 
 2291                 ni->ni_vap->iv_stats.is_ampdu_bar_tx_fail++;
 2292                 /*
 2293                  * If (at least) the last BAR TX timeout was due to
 2294                  * an ieee80211_send_bar() failures, then we need
 2295                  * to make sure we notify the driver that a BAR
 2296                  * TX did occur and fail.  This gives the driver
 2297                  * a chance to undo any queue pause that may
 2298                  * have occurred.
 2299                  */
 2300                 ic->ic_bar_response(ni, tap, 1);
 2301                 ieee80211_ampdu_stop(ni, tap, IEEE80211_REASON_TIMEOUT);
 2302         } else {
 2303                 ni->ni_vap->iv_stats.is_ampdu_bar_tx_retry++;
 2304                 if (ieee80211_send_bar(ni, tap, tap->txa_seqpending) != 0) {
 2305                         IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
 2306                             ni, "%s: failed to TX, starting timer\n",
 2307                             __func__);
 2308                         /*
 2309                          * If ieee80211_send_bar() fails here, the
 2310                          * timer may have stopped and/or the pending
 2311                          * flag may be clear.  Because of this,
 2312                          * fake the BARPEND and reset the timer.
 2313                          * A retransmission attempt will then occur
 2314                          * during the next timeout.
 2315                          */
 2316                         /* XXX locking */
 2317                         tap->txa_flags |= IEEE80211_AGGR_BARPEND;
 2318                         bar_start_timer(tap);
 2319                 }
 2320         }
 2321 }
 2322 
 2323 static void
 2324 bar_start_timer(struct ieee80211_tx_ampdu *tap)
 2325 {
 2326         IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
 2327             tap->txa_ni,
 2328             "%s: called",
 2329             __func__);
 2330         callout_reset(&tap->txa_timer, ieee80211_bar_timeout, bar_timeout, tap);
 2331 }
 2332 
 2333 static void
 2334 bar_stop_timer(struct ieee80211_tx_ampdu *tap)
 2335 {
 2336         IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
 2337             tap->txa_ni,
 2338             "%s: called",
 2339             __func__);
 2340         callout_stop(&tap->txa_timer);
 2341 }
 2342 
 2343 static void
 2344 bar_tx_complete(struct ieee80211_node *ni, void *arg, int status)
 2345 {
 2346         struct ieee80211_tx_ampdu *tap = arg;
 2347 
 2348         IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
 2349             ni, "%s: tid %u flags 0x%x pending %d status %d",
 2350             __func__, tap->txa_tid, tap->txa_flags,
 2351             callout_pending(&tap->txa_timer), status);
 2352 
 2353         ni->ni_vap->iv_stats.is_ampdu_bar_tx++;
 2354         /* XXX locking */
 2355         if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) &&
 2356             callout_pending(&tap->txa_timer)) {
 2357                 struct ieee80211com *ic = ni->ni_ic;
 2358 
 2359                 if (status == 0)                /* ACK'd */
 2360                         bar_stop_timer(tap);
 2361                 ic->ic_bar_response(ni, tap, status);
 2362                 /* NB: just let timer expire so we pace requests */
 2363         }
 2364 }
 2365 
 2366 static void
 2367 ieee80211_bar_response(struct ieee80211_node *ni,
 2368         struct ieee80211_tx_ampdu *tap, int status)
 2369 {
 2370 
 2371         IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
 2372             tap->txa_ni,
 2373             "%s: called",
 2374             __func__);
 2375         if (status == 0) {              /* got ACK */
 2376                 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
 2377                     ni, "BAR moves BA win <%u:%u> (%u frames) txseq %u tid %u",
 2378                     tap->txa_start,
 2379                     IEEE80211_SEQ_ADD(tap->txa_start, tap->txa_wnd-1),
 2380                     tap->txa_qframes, tap->txa_seqpending,
 2381                     tap->txa_tid);
 2382 
 2383                 /* NB: timer already stopped in bar_tx_complete */
 2384                 tap->txa_start = tap->txa_seqpending;
 2385                 tap->txa_flags &= ~IEEE80211_AGGR_BARPEND;
 2386         }
 2387 }
 2388 
 2389 /*
 2390  * Transmit a BAR frame to the specified node.  The
 2391  * BAR contents are drawn from the supplied aggregation
 2392  * state associated with the node.
 2393  *
 2394  * NB: we only handle immediate ACK w/ compressed bitmap.
 2395  */
 2396 int
 2397 ieee80211_send_bar(struct ieee80211_node *ni,
 2398         struct ieee80211_tx_ampdu *tap, ieee80211_seq seq)
 2399 {
 2400 #define senderr(_x, _v) do { vap->iv_stats._v++; ret = _x; goto bad; } while (0)
 2401         struct ieee80211vap *vap = ni->ni_vap;
 2402         struct ieee80211com *ic = ni->ni_ic;
 2403         struct ieee80211_frame_bar *bar;
 2404         struct mbuf *m;
 2405         uint16_t barctl, barseqctl;
 2406         uint8_t *frm;
 2407         int tid, ret;
 2408 
 2409 
 2410         IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
 2411             tap->txa_ni,
 2412             "%s: called",
 2413             __func__);
 2414 
 2415         if ((tap->txa_flags & IEEE80211_AGGR_RUNNING) == 0) {
 2416                 /* no ADDBA response, should not happen */
 2417                 /* XXX stat+msg */
 2418                 return EINVAL;
 2419         }
 2420         /* XXX locking */
 2421         bar_stop_timer(tap);
 2422 
 2423         ieee80211_ref_node(ni);
 2424 
 2425         m = ieee80211_getmgtframe(&frm, ic->ic_headroom, sizeof(*bar));
 2426         if (m == NULL)
 2427                 senderr(ENOMEM, is_tx_nobuf);
 2428 
 2429         if (!ieee80211_add_callback(m, bar_tx_complete, tap)) {
 2430                 m_freem(m);
 2431                 senderr(ENOMEM, is_tx_nobuf);   /* XXX */
 2432                 /* NOTREACHED */
 2433         }
 2434 
 2435         bar = mtod(m, struct ieee80211_frame_bar *);
 2436         bar->i_fc[0] = IEEE80211_FC0_VERSION_0 |
 2437                 IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_BAR;
 2438         bar->i_fc[1] = 0;
 2439         IEEE80211_ADDR_COPY(bar->i_ra, ni->ni_macaddr);
 2440         IEEE80211_ADDR_COPY(bar->i_ta, vap->iv_myaddr);
 2441 
 2442         tid = tap->txa_tid;
 2443         barctl  = (tap->txa_flags & IEEE80211_AGGR_IMMEDIATE ?
 2444                         0 : IEEE80211_BAR_NOACK)
 2445                 | IEEE80211_BAR_COMP
 2446                 | SM(tid, IEEE80211_BAR_TID)
 2447                 ;
 2448         barseqctl = SM(seq, IEEE80211_BAR_SEQ_START);
 2449         /* NB: known to have proper alignment */
 2450         bar->i_ctl = htole16(barctl);
 2451         bar->i_seq = htole16(barseqctl);
 2452         m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_bar);
 2453 
 2454         M_WME_SETAC(m, WME_AC_VO);
 2455 
 2456         IEEE80211_NODE_STAT(ni, tx_mgmt);       /* XXX tx_ctl? */
 2457 
 2458         /* XXX locking */
 2459         /* init/bump attempts counter */
 2460         if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) == 0)
 2461                 tap->txa_attempts = 1;
 2462         else
 2463                 tap->txa_attempts++;
 2464         tap->txa_seqpending = seq;
 2465         tap->txa_flags |= IEEE80211_AGGR_BARPEND;
 2466 
 2467         IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_11N,
 2468             ni, "send BAR: tid %u ctl 0x%x start %u (attempt %d)",
 2469             tid, barctl, seq, tap->txa_attempts);
 2470 
 2471         /*
 2472          * ic_raw_xmit will free the node reference
 2473          * regardless of queue/TX success or failure.
 2474          */
 2475         IEEE80211_TX_LOCK(ic);
 2476         ret = ieee80211_raw_output(vap, ni, m, NULL);
 2477         IEEE80211_TX_UNLOCK(ic);
 2478         if (ret != 0) {
 2479                 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_11N,
 2480                     ni, "send BAR: failed: (ret = %d)\n",
 2481                     ret);
 2482                 /* xmit failed, clear state flag */
 2483                 tap->txa_flags &= ~IEEE80211_AGGR_BARPEND;
 2484                 vap->iv_stats.is_ampdu_bar_tx_fail++;
 2485                 return ret;
 2486         }
 2487         /* XXX hack against tx complete happening before timer is started */
 2488         if (tap->txa_flags & IEEE80211_AGGR_BARPEND)
 2489                 bar_start_timer(tap);
 2490         return 0;
 2491 bad:
 2492         IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
 2493             tap->txa_ni,
 2494             "%s: bad! ret=%d",
 2495             __func__, ret);
 2496         vap->iv_stats.is_ampdu_bar_tx_fail++;
 2497         ieee80211_free_node(ni);
 2498         return ret;
 2499 #undef senderr
 2500 }
 2501 
 2502 static int
 2503 ht_action_output(struct ieee80211_node *ni, struct mbuf *m)
 2504 {
 2505         struct ieee80211_bpf_params params;
 2506 
 2507         memset(&params, 0, sizeof(params));
 2508         params.ibp_pri = WME_AC_VO;
 2509         params.ibp_rate0 = ni->ni_txparms->mgmtrate;
 2510         /* NB: we know all frames are unicast */
 2511         params.ibp_try0 = ni->ni_txparms->maxretry;
 2512         params.ibp_power = ni->ni_txpower;
 2513         return ieee80211_mgmt_output(ni, m, IEEE80211_FC0_SUBTYPE_ACTION,
 2514              &params);
 2515 }
 2516 
 2517 #define ADDSHORT(frm, v) do {                   \
 2518         frm[0] = (v) & 0xff;                    \
 2519         frm[1] = (v) >> 8;                      \
 2520         frm += 2;                               \
 2521 } while (0)
 2522 
 2523 /*
 2524  * Send an action management frame.  The arguments are stuff
 2525  * into a frame without inspection; the caller is assumed to
 2526  * prepare them carefully (e.g. based on the aggregation state).
 2527  */
 2528 static int
 2529 ht_send_action_ba_addba(struct ieee80211_node *ni,
 2530         int category, int action, void *arg0)
 2531 {
 2532         struct ieee80211vap *vap = ni->ni_vap;
 2533         struct ieee80211com *ic = ni->ni_ic;
 2534         uint16_t *args = arg0;
 2535         struct mbuf *m;
 2536         uint8_t *frm;
 2537 
 2538         IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
 2539             "send ADDBA %s: dialogtoken %d status %d "
 2540             "baparamset 0x%x (tid %d) batimeout 0x%x baseqctl 0x%x",
 2541             (action == IEEE80211_ACTION_BA_ADDBA_REQUEST) ?
 2542                 "request" : "response",
 2543             args[0], args[1], args[2], MS(args[2], IEEE80211_BAPS_TID),
 2544             args[3], args[4]);
 2545 
 2546         IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
 2547             "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
 2548             ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
 2549         ieee80211_ref_node(ni);
 2550 
 2551         m = ieee80211_getmgtframe(&frm,
 2552             ic->ic_headroom + sizeof(struct ieee80211_frame),
 2553             sizeof(uint16_t)    /* action+category */
 2554             /* XXX may action payload */
 2555             + sizeof(struct ieee80211_action_ba_addbaresponse)
 2556         );
 2557         if (m != NULL) {
 2558                 *frm++ = category;
 2559                 *frm++ = action;
 2560                 *frm++ = args[0];               /* dialog token */
 2561                 if (action == IEEE80211_ACTION_BA_ADDBA_RESPONSE)
 2562                         ADDSHORT(frm, args[1]); /* status code */
 2563                 ADDSHORT(frm, args[2]);         /* baparamset */
 2564                 ADDSHORT(frm, args[3]);         /* batimeout */
 2565                 if (action == IEEE80211_ACTION_BA_ADDBA_REQUEST)
 2566                         ADDSHORT(frm, args[4]); /* baseqctl */
 2567                 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
 2568                 return ht_action_output(ni, m);
 2569         } else {
 2570                 vap->iv_stats.is_tx_nobuf++;
 2571                 ieee80211_free_node(ni);
 2572                 return ENOMEM;
 2573         }
 2574 }
 2575 
 2576 static int
 2577 ht_send_action_ba_delba(struct ieee80211_node *ni,
 2578         int category, int action, void *arg0)
 2579 {
 2580         struct ieee80211vap *vap = ni->ni_vap;
 2581         struct ieee80211com *ic = ni->ni_ic;
 2582         uint16_t *args = arg0;
 2583         struct mbuf *m;
 2584         uint16_t baparamset;
 2585         uint8_t *frm;
 2586 
 2587         baparamset = SM(args[0], IEEE80211_DELBAPS_TID)
 2588                    | args[1]
 2589                    ;
 2590         IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
 2591             "send DELBA action: tid %d, initiator %d reason %d (%s)",
 2592             args[0], args[1], args[2], ieee80211_reason_to_string(args[2]));
 2593 
 2594         IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
 2595             "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
 2596             ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
 2597         ieee80211_ref_node(ni);
 2598 
 2599         m = ieee80211_getmgtframe(&frm,
 2600             ic->ic_headroom + sizeof(struct ieee80211_frame),
 2601             sizeof(uint16_t)    /* action+category */
 2602             /* XXX may action payload */
 2603             + sizeof(struct ieee80211_action_ba_addbaresponse)
 2604         );
 2605         if (m != NULL) {
 2606                 *frm++ = category;
 2607                 *frm++ = action;
 2608                 ADDSHORT(frm, baparamset);
 2609                 ADDSHORT(frm, args[2]);         /* reason code */
 2610                 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
 2611                 return ht_action_output(ni, m);
 2612         } else {
 2613                 vap->iv_stats.is_tx_nobuf++;
 2614                 ieee80211_free_node(ni);
 2615                 return ENOMEM;
 2616         }
 2617 }
 2618 
 2619 static int
 2620 ht_send_action_ht_txchwidth(struct ieee80211_node *ni,
 2621         int category, int action, void *arg0)
 2622 {
 2623         struct ieee80211vap *vap = ni->ni_vap;
 2624         struct ieee80211com *ic = ni->ni_ic;
 2625         struct mbuf *m;
 2626         uint8_t *frm;
 2627 
 2628         IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
 2629             "send HT txchwidth: width %d",
 2630             IEEE80211_IS_CHAN_HT40(ni->ni_chan) ? 40 : 20);
 2631 
 2632         IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
 2633             "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
 2634             ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
 2635         ieee80211_ref_node(ni);
 2636 
 2637         m = ieee80211_getmgtframe(&frm,
 2638             ic->ic_headroom + sizeof(struct ieee80211_frame),
 2639             sizeof(uint16_t)    /* action+category */
 2640             /* XXX may action payload */
 2641             + sizeof(struct ieee80211_action_ba_addbaresponse)
 2642         );
 2643         if (m != NULL) {
 2644                 *frm++ = category;
 2645                 *frm++ = action;
 2646                 *frm++ = IEEE80211_IS_CHAN_HT40(ni->ni_chan) ? 
 2647                         IEEE80211_A_HT_TXCHWIDTH_2040 :
 2648                         IEEE80211_A_HT_TXCHWIDTH_20;
 2649                 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
 2650                 return ht_action_output(ni, m);
 2651         } else {
 2652                 vap->iv_stats.is_tx_nobuf++;
 2653                 ieee80211_free_node(ni);
 2654                 return ENOMEM;
 2655         }
 2656 }
 2657 #undef ADDSHORT
 2658 
 2659 /*
 2660  * Construct the MCS bit mask for inclusion in an HT capabilities
 2661  * information element.
 2662  */
 2663 static void
 2664 ieee80211_set_mcsset(struct ieee80211com *ic, uint8_t *frm)
 2665 {
 2666         int i;
 2667         uint8_t txparams;
 2668 
 2669         KASSERT((ic->ic_rxstream > 0 && ic->ic_rxstream <= 4),
 2670             ("ic_rxstream %d out of range", ic->ic_rxstream));
 2671         KASSERT((ic->ic_txstream > 0 && ic->ic_txstream <= 4),
 2672             ("ic_txstream %d out of range", ic->ic_txstream));
 2673 
 2674         for (i = 0; i < ic->ic_rxstream * 8; i++)
 2675                 setbit(frm, i);
 2676         if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
 2677             (ic->ic_htcaps & IEEE80211_HTC_RXMCS32))
 2678                 setbit(frm, 32);
 2679         if (ic->ic_htcaps & IEEE80211_HTC_RXUNEQUAL) {
 2680                 if (ic->ic_rxstream >= 2) {
 2681                         for (i = 33; i <= 38; i++)
 2682                                 setbit(frm, i);
 2683                 }
 2684                 if (ic->ic_rxstream >= 3) {
 2685                         for (i = 39; i <= 52; i++)
 2686                                 setbit(frm, i);
 2687                 }
 2688                 if (ic->ic_txstream >= 4) {
 2689                         for (i = 53; i <= 76; i++)
 2690                                 setbit(frm, i);
 2691                 }
 2692         }
 2693 
 2694         if (ic->ic_rxstream != ic->ic_txstream) {
 2695                 txparams = 0x1;                 /* TX MCS set defined */
 2696                 txparams |= 0x2;                /* TX RX MCS not equal */
 2697                 txparams |= (ic->ic_txstream - 1) << 2; /* num TX streams */
 2698                 if (ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL)
 2699                         txparams |= 0x16;       /* TX unequal modulation sup */
 2700         } else
 2701                 txparams = 0;
 2702         frm[12] = txparams;
 2703 }
 2704 
 2705 /*
 2706  * Add body of an HTCAP information element.
 2707  */
 2708 static uint8_t *
 2709 ieee80211_add_htcap_body(uint8_t *frm, struct ieee80211_node *ni)
 2710 {
 2711 #define ADDSHORT(frm, v) do {                   \
 2712         frm[0] = (v) & 0xff;                    \
 2713         frm[1] = (v) >> 8;                      \
 2714         frm += 2;                               \
 2715 } while (0)
 2716         struct ieee80211com *ic = ni->ni_ic;
 2717         struct ieee80211vap *vap = ni->ni_vap;
 2718         uint16_t caps, extcaps;
 2719         int rxmax, density;
 2720 
 2721         /* HT capabilities */
 2722         caps = vap->iv_htcaps & 0xffff;
 2723         /*
 2724          * Note channel width depends on whether we are operating as
 2725          * a sta or not.  When operating as a sta we are generating
 2726          * a request based on our desired configuration.  Otherwise
 2727          * we are operational and the channel attributes identify
 2728          * how we've been setup (which might be different if a fixed
 2729          * channel is specified).
 2730          */
 2731         if (vap->iv_opmode == IEEE80211_M_STA) {
 2732                 /* override 20/40 use based on config */
 2733                 if (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)
 2734                         caps |= IEEE80211_HTCAP_CHWIDTH40;
 2735                 else
 2736                         caps &= ~IEEE80211_HTCAP_CHWIDTH40;
 2737 
 2738                 /* Start by using the advertised settings */
 2739                 rxmax = MS(ni->ni_htparam, IEEE80211_HTCAP_MAXRXAMPDU);
 2740                 density = MS(ni->ni_htparam, IEEE80211_HTCAP_MPDUDENSITY);
 2741 
 2742                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_11N,
 2743                     "%s: advertised rxmax=%d, density=%d, vap rxmax=%d, density=%d\n",
 2744                     __func__,
 2745                     rxmax,
 2746                     density,
 2747                     vap->iv_ampdu_rxmax,
 2748                     vap->iv_ampdu_density);
 2749 
 2750                 /* Cap at VAP rxmax */
 2751                 if (rxmax > vap->iv_ampdu_rxmax)
 2752                         rxmax = vap->iv_ampdu_rxmax;
 2753 
 2754                 /*
 2755                  * If the VAP ampdu density value greater, use that.
 2756                  *
 2757                  * (Larger density value == larger minimum gap between A-MPDU
 2758                  * subframes.)
 2759                  */
 2760                 if (vap->iv_ampdu_density > density)
 2761                         density = vap->iv_ampdu_density;
 2762 
 2763                 /*
 2764                  * NB: Hardware might support HT40 on some but not all
 2765                  * channels. We can't determine this earlier because only
 2766                  * after association the channel is upgraded to HT based
 2767                  * on the negotiated capabilities.
 2768                  */
 2769                 if (ni->ni_chan != IEEE80211_CHAN_ANYC &&
 2770                     findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT40U) == NULL &&
 2771                     findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT40D) == NULL)
 2772                         caps &= ~IEEE80211_HTCAP_CHWIDTH40;
 2773         } else {
 2774                 /* override 20/40 use based on current channel */
 2775                 if (IEEE80211_IS_CHAN_HT40(ni->ni_chan))
 2776                         caps |= IEEE80211_HTCAP_CHWIDTH40;
 2777                 else
 2778                         caps &= ~IEEE80211_HTCAP_CHWIDTH40;
 2779 
 2780                 /* XXX TODO should it start by using advertised settings? */
 2781                 rxmax = vap->iv_ampdu_rxmax;
 2782                 density = vap->iv_ampdu_density;
 2783         }
 2784 
 2785         /* adjust short GI based on channel and config */
 2786         if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20) == 0)
 2787                 caps &= ~IEEE80211_HTCAP_SHORTGI20;
 2788         if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40) == 0 ||
 2789             (caps & IEEE80211_HTCAP_CHWIDTH40) == 0)
 2790                 caps &= ~IEEE80211_HTCAP_SHORTGI40;
 2791 
 2792         /* adjust STBC based on receive capabilities */
 2793         if ((vap->iv_flags_ht & IEEE80211_FHT_STBC_RX) == 0)
 2794                 caps &= ~IEEE80211_HTCAP_RXSTBC;
 2795 
 2796         /* XXX TODO: adjust LDPC based on receive capabilities */
 2797 
 2798         ADDSHORT(frm, caps);
 2799 
 2800         /* HT parameters */
 2801         *frm = SM(rxmax, IEEE80211_HTCAP_MAXRXAMPDU)
 2802              | SM(density, IEEE80211_HTCAP_MPDUDENSITY)
 2803              ;
 2804         frm++;
 2805 
 2806         /* pre-zero remainder of ie */
 2807         memset(frm, 0, sizeof(struct ieee80211_ie_htcap) - 
 2808                 __offsetof(struct ieee80211_ie_htcap, hc_mcsset));
 2809 
 2810         /* supported MCS set */
 2811         /*
 2812          * XXX: For sta mode the rate set should be restricted based
 2813          * on the AP's capabilities, but ni_htrates isn't setup when
 2814          * we're called to form an AssocReq frame so for now we're
 2815          * restricted to the device capabilities.
 2816          */
 2817         ieee80211_set_mcsset(ni->ni_ic, frm);
 2818 
 2819         frm += __offsetof(struct ieee80211_ie_htcap, hc_extcap) -
 2820                 __offsetof(struct ieee80211_ie_htcap, hc_mcsset);
 2821 
 2822         /* HT extended capabilities */
 2823         extcaps = vap->iv_htextcaps & 0xffff;
 2824 
 2825         ADDSHORT(frm, extcaps);
 2826 
 2827         frm += sizeof(struct ieee80211_ie_htcap) -
 2828                 __offsetof(struct ieee80211_ie_htcap, hc_txbf);
 2829 
 2830         return frm;
 2831 #undef ADDSHORT
 2832 }
 2833 
 2834 /*
 2835  * Add 802.11n HT capabilities information element
 2836  */
 2837 uint8_t *
 2838 ieee80211_add_htcap(uint8_t *frm, struct ieee80211_node *ni)
 2839 {
 2840         frm[0] = IEEE80211_ELEMID_HTCAP;
 2841         frm[1] = sizeof(struct ieee80211_ie_htcap) - 2;
 2842         return ieee80211_add_htcap_body(frm + 2, ni);
 2843 }
 2844 
 2845 /*
 2846  * Non-associated probe request - add HT capabilities based on
 2847  * the current channel configuration.
 2848  */
 2849 static uint8_t *
 2850 ieee80211_add_htcap_body_ch(uint8_t *frm, struct ieee80211vap *vap,
 2851     struct ieee80211_channel *c)
 2852 {
 2853 #define ADDSHORT(frm, v) do {                   \
 2854         frm[0] = (v) & 0xff;                    \
 2855         frm[1] = (v) >> 8;                      \
 2856         frm += 2;                               \
 2857 } while (0)
 2858         struct ieee80211com *ic = vap->iv_ic;
 2859         uint16_t caps, extcaps;
 2860         int rxmax, density;
 2861 
 2862         /* HT capabilities */
 2863         caps = vap->iv_htcaps & 0xffff;
 2864 
 2865         /*
 2866          * We don't use this in STA mode; only in IBSS mode.
 2867          * So in IBSS mode we base our HTCAP flags on the
 2868          * given channel.
 2869          */
 2870 
 2871         /* override 20/40 use based on current channel */
 2872         if (IEEE80211_IS_CHAN_HT40(c))
 2873                 caps |= IEEE80211_HTCAP_CHWIDTH40;
 2874         else
 2875                 caps &= ~IEEE80211_HTCAP_CHWIDTH40;
 2876 
 2877         /* Use the currently configured values */
 2878         rxmax = vap->iv_ampdu_rxmax;
 2879         density = vap->iv_ampdu_density;
 2880 
 2881         /* adjust short GI based on channel and config */
 2882         if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20) == 0)
 2883                 caps &= ~IEEE80211_HTCAP_SHORTGI20;
 2884         if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40) == 0 ||
 2885             (caps & IEEE80211_HTCAP_CHWIDTH40) == 0)
 2886                 caps &= ~IEEE80211_HTCAP_SHORTGI40;
 2887         ADDSHORT(frm, caps);
 2888 
 2889         /* HT parameters */
 2890         *frm = SM(rxmax, IEEE80211_HTCAP_MAXRXAMPDU)
 2891              | SM(density, IEEE80211_HTCAP_MPDUDENSITY)
 2892              ;
 2893         frm++;
 2894 
 2895         /* pre-zero remainder of ie */
 2896         memset(frm, 0, sizeof(struct ieee80211_ie_htcap) - 
 2897                 __offsetof(struct ieee80211_ie_htcap, hc_mcsset));
 2898 
 2899         /* supported MCS set */
 2900         /*
 2901          * XXX: For sta mode the rate set should be restricted based
 2902          * on the AP's capabilities, but ni_htrates isn't setup when
 2903          * we're called to form an AssocReq frame so for now we're
 2904          * restricted to the device capabilities.
 2905          */
 2906         ieee80211_set_mcsset(ic, frm);
 2907 
 2908         frm += __offsetof(struct ieee80211_ie_htcap, hc_extcap) -
 2909                 __offsetof(struct ieee80211_ie_htcap, hc_mcsset);
 2910 
 2911         /* HT extended capabilities */
 2912         extcaps = vap->iv_htextcaps & 0xffff;
 2913 
 2914         ADDSHORT(frm, extcaps);
 2915 
 2916         frm += sizeof(struct ieee80211_ie_htcap) -
 2917                 __offsetof(struct ieee80211_ie_htcap, hc_txbf);
 2918 
 2919         return frm;
 2920 #undef ADDSHORT
 2921 }
 2922 
 2923 /*
 2924  * Add 802.11n HT capabilities information element
 2925  */
 2926 uint8_t *
 2927 ieee80211_add_htcap_ch(uint8_t *frm, struct ieee80211vap *vap,
 2928     struct ieee80211_channel *c)
 2929 {
 2930         frm[0] = IEEE80211_ELEMID_HTCAP;
 2931         frm[1] = sizeof(struct ieee80211_ie_htcap) - 2;
 2932         return ieee80211_add_htcap_body_ch(frm + 2, vap, c);
 2933 }
 2934 
 2935 /*
 2936  * Add Broadcom OUI wrapped standard HTCAP ie; this is
 2937  * used for compatibility w/ pre-draft implementations.
 2938  */
 2939 uint8_t *
 2940 ieee80211_add_htcap_vendor(uint8_t *frm, struct ieee80211_node *ni)
 2941 {
 2942         frm[0] = IEEE80211_ELEMID_VENDOR;
 2943         frm[1] = 4 + sizeof(struct ieee80211_ie_htcap) - 2;
 2944         frm[2] = (BCM_OUI >> 0) & 0xff;
 2945         frm[3] = (BCM_OUI >> 8) & 0xff;
 2946         frm[4] = (BCM_OUI >> 16) & 0xff;
 2947         frm[5] = BCM_OUI_HTCAP;
 2948         return ieee80211_add_htcap_body(frm + 6, ni);
 2949 }
 2950 
 2951 /*
 2952  * Construct the MCS bit mask of basic rates
 2953  * for inclusion in an HT information element.
 2954  */
 2955 static void
 2956 ieee80211_set_basic_htrates(uint8_t *frm, const struct ieee80211_htrateset *rs)
 2957 {
 2958         int i;
 2959 
 2960         for (i = 0; i < rs->rs_nrates; i++) {
 2961                 int r = rs->rs_rates[i] & IEEE80211_RATE_VAL;
 2962                 if ((rs->rs_rates[i] & IEEE80211_RATE_BASIC) &&
 2963                     r < IEEE80211_HTRATE_MAXSIZE) {
 2964                         /* NB: this assumes a particular implementation */
 2965                         setbit(frm, r);
 2966                 }
 2967         }
 2968 }
 2969 
 2970 /*
 2971  * Update the HTINFO ie for a beacon frame.
 2972  */
 2973 void
 2974 ieee80211_ht_update_beacon(struct ieee80211vap *vap,
 2975         struct ieee80211_beacon_offsets *bo)
 2976 {
 2977 #define PROTMODE        (IEEE80211_HTINFO_OPMODE|IEEE80211_HTINFO_NONHT_PRESENT)
 2978         struct ieee80211_node *ni;
 2979         const struct ieee80211_channel *bsschan;
 2980         struct ieee80211com *ic = vap->iv_ic;
 2981         struct ieee80211_ie_htinfo *ht =
 2982            (struct ieee80211_ie_htinfo *) bo->bo_htinfo;
 2983 
 2984         ni = ieee80211_ref_node(vap->iv_bss);
 2985         bsschan = ni->ni_chan;
 2986 
 2987         /* XXX only update on channel change */
 2988         ht->hi_ctrlchannel = ieee80211_chan2ieee(ic, bsschan);
 2989         if (vap->iv_flags_ht & IEEE80211_FHT_RIFS)
 2990                 ht->hi_byte1 = IEEE80211_HTINFO_RIFSMODE_PERM;
 2991         else
 2992                 ht->hi_byte1 = IEEE80211_HTINFO_RIFSMODE_PROH;
 2993         if (IEEE80211_IS_CHAN_HT40U(bsschan))
 2994                 ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_ABOVE;
 2995         else if (IEEE80211_IS_CHAN_HT40D(bsschan))
 2996                 ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_BELOW;
 2997         else
 2998                 ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_NONE;
 2999         if (IEEE80211_IS_CHAN_HT40(bsschan))
 3000                 ht->hi_byte1 |= IEEE80211_HTINFO_TXWIDTH_2040;
 3001 
 3002         /* protection mode */
 3003         ht->hi_byte2 = (ht->hi_byte2 &~ PROTMODE) | ic->ic_curhtprotmode;
 3004 
 3005         ieee80211_free_node(ni);
 3006 
 3007         /* XXX propagate to vendor ie's */
 3008 #undef PROTMODE
 3009 }
 3010 
 3011 /*
 3012  * Add body of an HTINFO information element.
 3013  *
 3014  * NB: We don't use struct ieee80211_ie_htinfo because we can
 3015  * be called to fillin both a standard ie and a compat ie that
 3016  * has a vendor OUI at the front.
 3017  */
 3018 static uint8_t *
 3019 ieee80211_add_htinfo_body(uint8_t *frm, struct ieee80211_node *ni)
 3020 {
 3021         struct ieee80211vap *vap = ni->ni_vap;
 3022         struct ieee80211com *ic = ni->ni_ic;
 3023 
 3024         /* pre-zero remainder of ie */
 3025         memset(frm, 0, sizeof(struct ieee80211_ie_htinfo) - 2);
 3026 
 3027         /* primary/control channel center */
 3028         *frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
 3029 
 3030         if (vap->iv_flags_ht & IEEE80211_FHT_RIFS)
 3031                 frm[0] = IEEE80211_HTINFO_RIFSMODE_PERM;
 3032         else
 3033                 frm[0] = IEEE80211_HTINFO_RIFSMODE_PROH;
 3034         if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan))
 3035                 frm[0] |= IEEE80211_HTINFO_2NDCHAN_ABOVE;
 3036         else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan))
 3037                 frm[0] |= IEEE80211_HTINFO_2NDCHAN_BELOW;
 3038         else
 3039                 frm[0] |= IEEE80211_HTINFO_2NDCHAN_NONE;
 3040         if (IEEE80211_IS_CHAN_HT40(ni->ni_chan))
 3041                 frm[0] |= IEEE80211_HTINFO_TXWIDTH_2040;
 3042 
 3043         frm[1] = ic->ic_curhtprotmode;
 3044 
 3045         frm += 5;
 3046 
 3047         /* basic MCS set */
 3048         ieee80211_set_basic_htrates(frm, &ni->ni_htrates);
 3049         frm += sizeof(struct ieee80211_ie_htinfo) -
 3050                 __offsetof(struct ieee80211_ie_htinfo, hi_basicmcsset);
 3051         return frm;
 3052 }
 3053 
 3054 /*
 3055  * Add 802.11n HT information element.
 3056  */
 3057 uint8_t *
 3058 ieee80211_add_htinfo(uint8_t *frm, struct ieee80211_node *ni)
 3059 {
 3060         frm[0] = IEEE80211_ELEMID_HTINFO;
 3061         frm[1] = sizeof(struct ieee80211_ie_htinfo) - 2;
 3062         return ieee80211_add_htinfo_body(frm + 2, ni);
 3063 }
 3064 
 3065 /*
 3066  * Add Broadcom OUI wrapped standard HTINFO ie; this is
 3067  * used for compatibility w/ pre-draft implementations.
 3068  */
 3069 uint8_t *
 3070 ieee80211_add_htinfo_vendor(uint8_t *frm, struct ieee80211_node *ni)
 3071 {
 3072         frm[0] = IEEE80211_ELEMID_VENDOR;
 3073         frm[1] = 4 + sizeof(struct ieee80211_ie_htinfo) - 2;
 3074         frm[2] = (BCM_OUI >> 0) & 0xff;
 3075         frm[3] = (BCM_OUI >> 8) & 0xff;
 3076         frm[4] = (BCM_OUI >> 16) & 0xff;
 3077         frm[5] = BCM_OUI_HTINFO;
 3078         return ieee80211_add_htinfo_body(frm + 6, ni);
 3079 }

Cache object: bcaaa00592183eaa781255a35bffd77e


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.