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

Cache object: b5cf9e16df7e4e29808d9596a3c44f55


[ 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.