Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * VHT handling
0004  *
0005  * Portions of this file
0006  * Copyright(c) 2015 - 2016 Intel Deutschland GmbH
0007  * Copyright (C) 2018 - 2022 Intel Corporation
0008  */
0009 
0010 #include <linux/ieee80211.h>
0011 #include <linux/export.h>
0012 #include <net/mac80211.h>
0013 #include "ieee80211_i.h"
0014 #include "rate.h"
0015 
0016 
0017 static void __check_vhtcap_disable(struct ieee80211_sub_if_data *sdata,
0018                    struct ieee80211_sta_vht_cap *vht_cap,
0019                    u32 flag)
0020 {
0021     __le32 le_flag = cpu_to_le32(flag);
0022 
0023     if (sdata->u.mgd.vht_capa_mask.vht_cap_info & le_flag &&
0024         !(sdata->u.mgd.vht_capa.vht_cap_info & le_flag))
0025         vht_cap->cap &= ~flag;
0026 }
0027 
0028 void ieee80211_apply_vhtcap_overrides(struct ieee80211_sub_if_data *sdata,
0029                       struct ieee80211_sta_vht_cap *vht_cap)
0030 {
0031     int i;
0032     u16 rxmcs_mask, rxmcs_cap, rxmcs_n, txmcs_mask, txmcs_cap, txmcs_n;
0033 
0034     if (!vht_cap->vht_supported)
0035         return;
0036 
0037     if (sdata->vif.type != NL80211_IFTYPE_STATION)
0038         return;
0039 
0040     __check_vhtcap_disable(sdata, vht_cap,
0041                    IEEE80211_VHT_CAP_RXLDPC);
0042     __check_vhtcap_disable(sdata, vht_cap,
0043                    IEEE80211_VHT_CAP_SHORT_GI_80);
0044     __check_vhtcap_disable(sdata, vht_cap,
0045                    IEEE80211_VHT_CAP_SHORT_GI_160);
0046     __check_vhtcap_disable(sdata, vht_cap,
0047                    IEEE80211_VHT_CAP_TXSTBC);
0048     __check_vhtcap_disable(sdata, vht_cap,
0049                    IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE);
0050     __check_vhtcap_disable(sdata, vht_cap,
0051                    IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE);
0052     __check_vhtcap_disable(sdata, vht_cap,
0053                    IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN);
0054     __check_vhtcap_disable(sdata, vht_cap,
0055                    IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN);
0056 
0057     /* Allow user to decrease AMPDU length exponent */
0058     if (sdata->u.mgd.vht_capa_mask.vht_cap_info &
0059         cpu_to_le32(IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK)) {
0060         u32 cap, n;
0061 
0062         n = le32_to_cpu(sdata->u.mgd.vht_capa.vht_cap_info) &
0063             IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
0064         n >>= IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
0065         cap = vht_cap->cap & IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
0066         cap >>= IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
0067 
0068         if (n < cap) {
0069             vht_cap->cap &=
0070                 ~IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
0071             vht_cap->cap |=
0072                 n << IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
0073         }
0074     }
0075 
0076     /* Allow the user to decrease MCSes */
0077     rxmcs_mask =
0078         le16_to_cpu(sdata->u.mgd.vht_capa_mask.supp_mcs.rx_mcs_map);
0079     rxmcs_n = le16_to_cpu(sdata->u.mgd.vht_capa.supp_mcs.rx_mcs_map);
0080     rxmcs_n &= rxmcs_mask;
0081     rxmcs_cap = le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
0082 
0083     txmcs_mask =
0084         le16_to_cpu(sdata->u.mgd.vht_capa_mask.supp_mcs.tx_mcs_map);
0085     txmcs_n = le16_to_cpu(sdata->u.mgd.vht_capa.supp_mcs.tx_mcs_map);
0086     txmcs_n &= txmcs_mask;
0087     txmcs_cap = le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map);
0088     for (i = 0; i < 8; i++) {
0089         u8 m, n, c;
0090 
0091         m = (rxmcs_mask >> 2*i) & IEEE80211_VHT_MCS_NOT_SUPPORTED;
0092         n = (rxmcs_n >> 2*i) & IEEE80211_VHT_MCS_NOT_SUPPORTED;
0093         c = (rxmcs_cap >> 2*i) & IEEE80211_VHT_MCS_NOT_SUPPORTED;
0094 
0095         if (m && ((c != IEEE80211_VHT_MCS_NOT_SUPPORTED && n < c) ||
0096               n == IEEE80211_VHT_MCS_NOT_SUPPORTED)) {
0097             rxmcs_cap &= ~(3 << 2*i);
0098             rxmcs_cap |= (rxmcs_n & (3 << 2*i));
0099         }
0100 
0101         m = (txmcs_mask >> 2*i) & IEEE80211_VHT_MCS_NOT_SUPPORTED;
0102         n = (txmcs_n >> 2*i) & IEEE80211_VHT_MCS_NOT_SUPPORTED;
0103         c = (txmcs_cap >> 2*i) & IEEE80211_VHT_MCS_NOT_SUPPORTED;
0104 
0105         if (m && ((c != IEEE80211_VHT_MCS_NOT_SUPPORTED && n < c) ||
0106               n == IEEE80211_VHT_MCS_NOT_SUPPORTED)) {
0107             txmcs_cap &= ~(3 << 2*i);
0108             txmcs_cap |= (txmcs_n & (3 << 2*i));
0109         }
0110     }
0111     vht_cap->vht_mcs.rx_mcs_map = cpu_to_le16(rxmcs_cap);
0112     vht_cap->vht_mcs.tx_mcs_map = cpu_to_le16(txmcs_cap);
0113 }
0114 
0115 void
0116 ieee80211_vht_cap_ie_to_sta_vht_cap(struct ieee80211_sub_if_data *sdata,
0117                     struct ieee80211_supported_band *sband,
0118                     const struct ieee80211_vht_cap *vht_cap_ie,
0119                     struct link_sta_info *link_sta)
0120 {
0121     struct ieee80211_sta_vht_cap *vht_cap = &link_sta->pub->vht_cap;
0122     struct ieee80211_sta_vht_cap own_cap;
0123     u32 cap_info, i;
0124     bool have_80mhz;
0125 
0126     memset(vht_cap, 0, sizeof(*vht_cap));
0127 
0128     if (!link_sta->pub->ht_cap.ht_supported)
0129         return;
0130 
0131     if (!vht_cap_ie || !sband->vht_cap.vht_supported)
0132         return;
0133 
0134     /* Allow VHT if at least one channel on the sband supports 80 MHz */
0135     have_80mhz = false;
0136     for (i = 0; i < sband->n_channels; i++) {
0137         if (sband->channels[i].flags & (IEEE80211_CHAN_DISABLED |
0138                         IEEE80211_CHAN_NO_80MHZ))
0139             continue;
0140 
0141         have_80mhz = true;
0142         break;
0143     }
0144 
0145     if (!have_80mhz)
0146         return;
0147 
0148     /*
0149      * A VHT STA must support 40 MHz, but if we verify that here
0150      * then we break a few things - some APs (e.g. Netgear R6300v2
0151      * and others based on the BCM4360 chipset) will unset this
0152      * capability bit when operating in 20 MHz.
0153      */
0154 
0155     vht_cap->vht_supported = true;
0156 
0157     own_cap = sband->vht_cap;
0158     /*
0159      * If user has specified capability overrides, take care
0160      * of that if the station we're setting up is the AP that
0161      * we advertised a restricted capability set to. Override
0162      * our own capabilities and then use those below.
0163      */
0164     if (sdata->vif.type == NL80211_IFTYPE_STATION &&
0165         !test_sta_flag(link_sta->sta, WLAN_STA_TDLS_PEER))
0166         ieee80211_apply_vhtcap_overrides(sdata, &own_cap);
0167 
0168     /* take some capabilities as-is */
0169     cap_info = le32_to_cpu(vht_cap_ie->vht_cap_info);
0170     vht_cap->cap = cap_info;
0171     vht_cap->cap &= IEEE80211_VHT_CAP_RXLDPC |
0172             IEEE80211_VHT_CAP_VHT_TXOP_PS |
0173             IEEE80211_VHT_CAP_HTC_VHT |
0174             IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK |
0175             IEEE80211_VHT_CAP_VHT_LINK_ADAPTATION_VHT_UNSOL_MFB |
0176             IEEE80211_VHT_CAP_VHT_LINK_ADAPTATION_VHT_MRQ_MFB |
0177             IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN |
0178             IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN;
0179 
0180     vht_cap->cap |= min_t(u32, cap_info & IEEE80211_VHT_CAP_MAX_MPDU_MASK,
0181                   own_cap.cap & IEEE80211_VHT_CAP_MAX_MPDU_MASK);
0182 
0183     /* and some based on our own capabilities */
0184     switch (own_cap.cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK) {
0185     case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ:
0186         vht_cap->cap |= cap_info &
0187                 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
0188         break;
0189     case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ:
0190         vht_cap->cap |= cap_info &
0191                 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
0192         break;
0193     default:
0194         /* nothing */
0195         break;
0196     }
0197 
0198     /* symmetric capabilities */
0199     vht_cap->cap |= cap_info & own_cap.cap &
0200             (IEEE80211_VHT_CAP_SHORT_GI_80 |
0201              IEEE80211_VHT_CAP_SHORT_GI_160);
0202 
0203     /* remaining ones */
0204     if (own_cap.cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)
0205         vht_cap->cap |= cap_info &
0206                 (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
0207                  IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
0208 
0209     if (own_cap.cap & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)
0210         vht_cap->cap |= cap_info &
0211                 (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
0212                  IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK);
0213 
0214     if (own_cap.cap & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)
0215         vht_cap->cap |= cap_info &
0216                 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
0217 
0218     if (own_cap.cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)
0219         vht_cap->cap |= cap_info &
0220                 IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE;
0221 
0222     if (own_cap.cap & IEEE80211_VHT_CAP_TXSTBC)
0223         vht_cap->cap |= cap_info & IEEE80211_VHT_CAP_RXSTBC_MASK;
0224 
0225     if (own_cap.cap & IEEE80211_VHT_CAP_RXSTBC_MASK)
0226         vht_cap->cap |= cap_info & IEEE80211_VHT_CAP_TXSTBC;
0227 
0228     /* Copy peer MCS info, the driver might need them. */
0229     memcpy(&vht_cap->vht_mcs, &vht_cap_ie->supp_mcs,
0230            sizeof(struct ieee80211_vht_mcs_info));
0231 
0232     /* copy EXT_NSS_BW Support value or remove the capability */
0233     if (ieee80211_hw_check(&sdata->local->hw, SUPPORTS_VHT_EXT_NSS_BW))
0234         vht_cap->cap |= (cap_info & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK);
0235     else
0236         vht_cap->vht_mcs.tx_highest &=
0237             ~cpu_to_le16(IEEE80211_VHT_EXT_NSS_BW_CAPABLE);
0238 
0239     /* but also restrict MCSes */
0240     for (i = 0; i < 8; i++) {
0241         u16 own_rx, own_tx, peer_rx, peer_tx;
0242 
0243         own_rx = le16_to_cpu(own_cap.vht_mcs.rx_mcs_map);
0244         own_rx = (own_rx >> i * 2) & IEEE80211_VHT_MCS_NOT_SUPPORTED;
0245 
0246         own_tx = le16_to_cpu(own_cap.vht_mcs.tx_mcs_map);
0247         own_tx = (own_tx >> i * 2) & IEEE80211_VHT_MCS_NOT_SUPPORTED;
0248 
0249         peer_rx = le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
0250         peer_rx = (peer_rx >> i * 2) & IEEE80211_VHT_MCS_NOT_SUPPORTED;
0251 
0252         peer_tx = le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map);
0253         peer_tx = (peer_tx >> i * 2) & IEEE80211_VHT_MCS_NOT_SUPPORTED;
0254 
0255         if (peer_tx != IEEE80211_VHT_MCS_NOT_SUPPORTED) {
0256             if (own_rx == IEEE80211_VHT_MCS_NOT_SUPPORTED)
0257                 peer_tx = IEEE80211_VHT_MCS_NOT_SUPPORTED;
0258             else if (own_rx < peer_tx)
0259                 peer_tx = own_rx;
0260         }
0261 
0262         if (peer_rx != IEEE80211_VHT_MCS_NOT_SUPPORTED) {
0263             if (own_tx == IEEE80211_VHT_MCS_NOT_SUPPORTED)
0264                 peer_rx = IEEE80211_VHT_MCS_NOT_SUPPORTED;
0265             else if (own_tx < peer_rx)
0266                 peer_rx = own_tx;
0267         }
0268 
0269         vht_cap->vht_mcs.rx_mcs_map &=
0270             ~cpu_to_le16(IEEE80211_VHT_MCS_NOT_SUPPORTED << i * 2);
0271         vht_cap->vht_mcs.rx_mcs_map |= cpu_to_le16(peer_rx << i * 2);
0272 
0273         vht_cap->vht_mcs.tx_mcs_map &=
0274             ~cpu_to_le16(IEEE80211_VHT_MCS_NOT_SUPPORTED << i * 2);
0275         vht_cap->vht_mcs.tx_mcs_map |= cpu_to_le16(peer_tx << i * 2);
0276     }
0277 
0278     /*
0279      * This is a workaround for VHT-enabled STAs which break the spec
0280      * and have the VHT-MCS Rx map filled in with value 3 for all eight
0281      * spacial streams, an example is AR9462.
0282      *
0283      * As per spec, in section 22.1.1 Introduction to the VHT PHY
0284      * A VHT STA shall support at least single spactial stream VHT-MCSs
0285      * 0 to 7 (transmit and receive) in all supported channel widths.
0286      */
0287     if (vht_cap->vht_mcs.rx_mcs_map == cpu_to_le16(0xFFFF)) {
0288         vht_cap->vht_supported = false;
0289         sdata_info(sdata,
0290                "Ignoring VHT IE from %pM (link:%pM) due to invalid rx_mcs_map\n",
0291                link_sta->sta->addr, link_sta->addr);
0292         return;
0293     }
0294 
0295     /* finally set up the bandwidth */
0296     switch (vht_cap->cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK) {
0297     case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ:
0298     case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ:
0299         link_sta->cur_max_bandwidth = IEEE80211_STA_RX_BW_160;
0300         break;
0301     default:
0302         link_sta->cur_max_bandwidth = IEEE80211_STA_RX_BW_80;
0303 
0304         if (!(vht_cap->vht_mcs.tx_highest &
0305                 cpu_to_le16(IEEE80211_VHT_EXT_NSS_BW_CAPABLE)))
0306             break;
0307 
0308         /*
0309          * If this is non-zero, then it does support 160 MHz after all,
0310          * in one form or the other. We don't distinguish here (or even
0311          * above) between 160 and 80+80 yet.
0312          */
0313         if (cap_info & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK)
0314             link_sta->cur_max_bandwidth =
0315                 IEEE80211_STA_RX_BW_160;
0316     }
0317 
0318     link_sta->pub->bandwidth = ieee80211_sta_cur_vht_bw(link_sta);
0319 
0320     /*
0321      * FIXME - should the amsdu len be per link? store per link
0322      * and maintain a minimum?
0323      */
0324     switch (vht_cap->cap & IEEE80211_VHT_CAP_MAX_MPDU_MASK) {
0325     case IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454:
0326         link_sta->sta->sta.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_VHT_11454;
0327         break;
0328     case IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_7991:
0329         link_sta->sta->sta.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_VHT_7991;
0330         break;
0331     case IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_3895:
0332     default:
0333         link_sta->sta->sta.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_VHT_3895;
0334         break;
0335     }
0336 }
0337 
0338 /* FIXME: move this to some better location - parses HE/EHT now */
0339 enum ieee80211_sta_rx_bandwidth
0340 ieee80211_sta_cap_rx_bw(struct link_sta_info *link_sta)
0341 {
0342     unsigned int link_id = link_sta->link_id;
0343     struct ieee80211_sub_if_data *sdata = link_sta->sta->sdata;
0344     struct ieee80211_sta_vht_cap *vht_cap = &link_sta->pub->vht_cap;
0345     struct ieee80211_sta_he_cap *he_cap = &link_sta->pub->he_cap;
0346     struct ieee80211_sta_eht_cap *eht_cap = &link_sta->pub->eht_cap;
0347     u32 cap_width;
0348 
0349     if (he_cap->has_he) {
0350         struct ieee80211_bss_conf *link_conf;
0351         enum ieee80211_sta_rx_bandwidth ret;
0352         u8 info;
0353 
0354         rcu_read_lock();
0355         link_conf = rcu_dereference(sdata->vif.link_conf[link_id]);
0356 
0357         if (eht_cap->has_eht &&
0358             link_conf->chandef.chan->band == NL80211_BAND_6GHZ) {
0359             info = eht_cap->eht_cap_elem.phy_cap_info[0];
0360 
0361             if (info & IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ) {
0362                 ret = IEEE80211_STA_RX_BW_320;
0363                 goto out;
0364             }
0365         }
0366 
0367         info = he_cap->he_cap_elem.phy_cap_info[0];
0368 
0369         if (link_conf->chandef.chan->band == NL80211_BAND_2GHZ) {
0370             if (info & IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G)
0371                 ret = IEEE80211_STA_RX_BW_40;
0372             else
0373                 ret = IEEE80211_STA_RX_BW_20;
0374             goto out;
0375         }
0376 
0377         if (info & IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G ||
0378             info & IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)
0379             ret = IEEE80211_STA_RX_BW_160;
0380         else if (info & IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G)
0381             ret = IEEE80211_STA_RX_BW_80;
0382         else
0383             ret = IEEE80211_STA_RX_BW_20;
0384 out:
0385         rcu_read_unlock();
0386 
0387         return ret;
0388     }
0389 
0390     if (!vht_cap->vht_supported)
0391         return link_sta->pub->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 ?
0392                 IEEE80211_STA_RX_BW_40 :
0393                 IEEE80211_STA_RX_BW_20;
0394 
0395     cap_width = vht_cap->cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
0396 
0397     if (cap_width == IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ ||
0398         cap_width == IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ)
0399         return IEEE80211_STA_RX_BW_160;
0400 
0401     /*
0402      * If this is non-zero, then it does support 160 MHz after all,
0403      * in one form or the other. We don't distinguish here (or even
0404      * above) between 160 and 80+80 yet.
0405      */
0406     if (vht_cap->cap & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK)
0407         return IEEE80211_STA_RX_BW_160;
0408 
0409     return IEEE80211_STA_RX_BW_80;
0410 }
0411 
0412 enum nl80211_chan_width
0413 ieee80211_sta_cap_chan_bw(struct link_sta_info *link_sta)
0414 {
0415     struct ieee80211_sta_vht_cap *vht_cap = &link_sta->pub->vht_cap;
0416     u32 cap_width;
0417 
0418     if (!vht_cap->vht_supported) {
0419         if (!link_sta->pub->ht_cap.ht_supported)
0420             return NL80211_CHAN_WIDTH_20_NOHT;
0421 
0422         return link_sta->pub->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 ?
0423                 NL80211_CHAN_WIDTH_40 : NL80211_CHAN_WIDTH_20;
0424     }
0425 
0426     cap_width = vht_cap->cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
0427 
0428     if (cap_width == IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ)
0429         return NL80211_CHAN_WIDTH_160;
0430     else if (cap_width == IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ)
0431         return NL80211_CHAN_WIDTH_80P80;
0432 
0433     return NL80211_CHAN_WIDTH_80;
0434 }
0435 
0436 enum nl80211_chan_width
0437 ieee80211_sta_rx_bw_to_chan_width(struct link_sta_info *link_sta)
0438 {
0439     enum ieee80211_sta_rx_bandwidth cur_bw =
0440         link_sta->pub->bandwidth;
0441     struct ieee80211_sta_vht_cap *vht_cap =
0442         &link_sta->pub->vht_cap;
0443     u32 cap_width;
0444 
0445     switch (cur_bw) {
0446     case IEEE80211_STA_RX_BW_20:
0447         if (!link_sta->pub->ht_cap.ht_supported)
0448             return NL80211_CHAN_WIDTH_20_NOHT;
0449         else
0450             return NL80211_CHAN_WIDTH_20;
0451     case IEEE80211_STA_RX_BW_40:
0452         return NL80211_CHAN_WIDTH_40;
0453     case IEEE80211_STA_RX_BW_80:
0454         return NL80211_CHAN_WIDTH_80;
0455     case IEEE80211_STA_RX_BW_160:
0456         cap_width =
0457             vht_cap->cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
0458 
0459         if (cap_width == IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ)
0460             return NL80211_CHAN_WIDTH_160;
0461 
0462         return NL80211_CHAN_WIDTH_80P80;
0463     default:
0464         return NL80211_CHAN_WIDTH_20;
0465     }
0466 }
0467 
0468 enum ieee80211_sta_rx_bandwidth
0469 ieee80211_chan_width_to_rx_bw(enum nl80211_chan_width width)
0470 {
0471     switch (width) {
0472     case NL80211_CHAN_WIDTH_20_NOHT:
0473     case NL80211_CHAN_WIDTH_20:
0474         return IEEE80211_STA_RX_BW_20;
0475     case NL80211_CHAN_WIDTH_40:
0476         return IEEE80211_STA_RX_BW_40;
0477     case NL80211_CHAN_WIDTH_80:
0478         return IEEE80211_STA_RX_BW_80;
0479     case NL80211_CHAN_WIDTH_160:
0480     case NL80211_CHAN_WIDTH_80P80:
0481         return IEEE80211_STA_RX_BW_160;
0482     case NL80211_CHAN_WIDTH_320:
0483         return IEEE80211_STA_RX_BW_320;
0484     default:
0485         WARN_ON_ONCE(1);
0486         return IEEE80211_STA_RX_BW_20;
0487     }
0488 }
0489 
0490 /* FIXME: rename/move - this deals with everything not just VHT */
0491 enum ieee80211_sta_rx_bandwidth
0492 ieee80211_sta_cur_vht_bw(struct link_sta_info *link_sta)
0493 {
0494     struct sta_info *sta = link_sta->sta;
0495     struct ieee80211_bss_conf *link_conf;
0496     enum nl80211_chan_width bss_width;
0497     enum ieee80211_sta_rx_bandwidth bw;
0498 
0499     rcu_read_lock();
0500     link_conf = rcu_dereference(sta->sdata->vif.link_conf[link_sta->link_id]);
0501     if (WARN_ON(!link_conf))
0502         bss_width = NL80211_CHAN_WIDTH_20_NOHT;
0503     else
0504         bss_width = link_conf->chandef.width;
0505     rcu_read_unlock();
0506 
0507     bw = ieee80211_sta_cap_rx_bw(link_sta);
0508     bw = min(bw, link_sta->cur_max_bandwidth);
0509 
0510     /* Don't consider AP's bandwidth for TDLS peers, section 11.23.1 of
0511      * IEEE80211-2016 specification makes higher bandwidth operation
0512      * possible on the TDLS link if the peers have wider bandwidth
0513      * capability.
0514      *
0515      * However, in this case, and only if the TDLS peer is authorized,
0516      * limit to the tdls_chandef so that the configuration here isn't
0517      * wider than what's actually requested on the channel context.
0518      */
0519     if (test_sta_flag(sta, WLAN_STA_TDLS_PEER) &&
0520         test_sta_flag(sta, WLAN_STA_TDLS_WIDER_BW) &&
0521         test_sta_flag(sta, WLAN_STA_AUTHORIZED) &&
0522         sta->tdls_chandef.chan)
0523         bw = min(bw, ieee80211_chan_width_to_rx_bw(sta->tdls_chandef.width));
0524     else
0525         bw = min(bw, ieee80211_chan_width_to_rx_bw(bss_width));
0526 
0527     return bw;
0528 }
0529 
0530 void ieee80211_sta_set_rx_nss(struct link_sta_info *link_sta)
0531 {
0532     u8 ht_rx_nss = 0, vht_rx_nss = 0, he_rx_nss = 0, eht_rx_nss = 0, rx_nss;
0533     bool support_160;
0534 
0535     /* if we received a notification already don't overwrite it */
0536     if (link_sta->pub->rx_nss)
0537         return;
0538 
0539     if (link_sta->pub->eht_cap.has_eht) {
0540         int i;
0541         const u8 *rx_nss_mcs = (void *)&link_sta->pub->eht_cap.eht_mcs_nss_supp;
0542 
0543         /* get the max nss for EHT over all possible bandwidths and mcs */
0544         for (i = 0; i < sizeof(struct ieee80211_eht_mcs_nss_supp); i++)
0545             eht_rx_nss = max_t(u8, eht_rx_nss,
0546                        u8_get_bits(rx_nss_mcs[i],
0547                                IEEE80211_EHT_MCS_NSS_RX));
0548     }
0549 
0550     if (link_sta->pub->he_cap.has_he) {
0551         int i;
0552         u8 rx_mcs_80 = 0, rx_mcs_160 = 0;
0553         const struct ieee80211_sta_he_cap *he_cap = &link_sta->pub->he_cap;
0554         u16 mcs_160_map =
0555             le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_160);
0556         u16 mcs_80_map = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_80);
0557 
0558         for (i = 7; i >= 0; i--) {
0559             u8 mcs_160 = (mcs_160_map >> (2 * i)) & 3;
0560 
0561             if (mcs_160 != IEEE80211_HE_MCS_NOT_SUPPORTED) {
0562                 rx_mcs_160 = i + 1;
0563                 break;
0564             }
0565         }
0566         for (i = 7; i >= 0; i--) {
0567             u8 mcs_80 = (mcs_80_map >> (2 * i)) & 3;
0568 
0569             if (mcs_80 != IEEE80211_HE_MCS_NOT_SUPPORTED) {
0570                 rx_mcs_80 = i + 1;
0571                 break;
0572             }
0573         }
0574 
0575         support_160 = he_cap->he_cap_elem.phy_cap_info[0] &
0576                   IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G;
0577 
0578         if (support_160)
0579             he_rx_nss = min(rx_mcs_80, rx_mcs_160);
0580         else
0581             he_rx_nss = rx_mcs_80;
0582     }
0583 
0584     if (link_sta->pub->ht_cap.ht_supported) {
0585         if (link_sta->pub->ht_cap.mcs.rx_mask[0])
0586             ht_rx_nss++;
0587         if (link_sta->pub->ht_cap.mcs.rx_mask[1])
0588             ht_rx_nss++;
0589         if (link_sta->pub->ht_cap.mcs.rx_mask[2])
0590             ht_rx_nss++;
0591         if (link_sta->pub->ht_cap.mcs.rx_mask[3])
0592             ht_rx_nss++;
0593         /* FIXME: consider rx_highest? */
0594     }
0595 
0596     if (link_sta->pub->vht_cap.vht_supported) {
0597         int i;
0598         u16 rx_mcs_map;
0599 
0600         rx_mcs_map = le16_to_cpu(link_sta->pub->vht_cap.vht_mcs.rx_mcs_map);
0601 
0602         for (i = 7; i >= 0; i--) {
0603             u8 mcs = (rx_mcs_map >> (2 * i)) & 3;
0604 
0605             if (mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED) {
0606                 vht_rx_nss = i + 1;
0607                 break;
0608             }
0609         }
0610         /* FIXME: consider rx_highest? */
0611     }
0612 
0613     rx_nss = max(vht_rx_nss, ht_rx_nss);
0614     rx_nss = max(he_rx_nss, rx_nss);
0615     rx_nss = max(eht_rx_nss, rx_nss);
0616     link_sta->pub->rx_nss = max_t(u8, 1, rx_nss);
0617 }
0618 
0619 u32 __ieee80211_vht_handle_opmode(struct ieee80211_sub_if_data *sdata,
0620                   struct link_sta_info *link_sta,
0621                   u8 opmode, enum nl80211_band band)
0622 {
0623     enum ieee80211_sta_rx_bandwidth new_bw;
0624     struct sta_opmode_info sta_opmode = {};
0625     u32 changed = 0;
0626     u8 nss;
0627 
0628     /* ignore - no support for BF yet */
0629     if (opmode & IEEE80211_OPMODE_NOTIF_RX_NSS_TYPE_BF)
0630         return 0;
0631 
0632     nss = opmode & IEEE80211_OPMODE_NOTIF_RX_NSS_MASK;
0633     nss >>= IEEE80211_OPMODE_NOTIF_RX_NSS_SHIFT;
0634     nss += 1;
0635 
0636     if (link_sta->pub->rx_nss != nss) {
0637         link_sta->pub->rx_nss = nss;
0638         sta_opmode.rx_nss = nss;
0639         changed |= IEEE80211_RC_NSS_CHANGED;
0640         sta_opmode.changed |= STA_OPMODE_N_SS_CHANGED;
0641     }
0642 
0643     switch (opmode & IEEE80211_OPMODE_NOTIF_CHANWIDTH_MASK) {
0644     case IEEE80211_OPMODE_NOTIF_CHANWIDTH_20MHZ:
0645         /* ignore IEEE80211_OPMODE_NOTIF_BW_160_80P80 must not be set */
0646         link_sta->cur_max_bandwidth = IEEE80211_STA_RX_BW_20;
0647         break;
0648     case IEEE80211_OPMODE_NOTIF_CHANWIDTH_40MHZ:
0649         /* ignore IEEE80211_OPMODE_NOTIF_BW_160_80P80 must not be set */
0650         link_sta->cur_max_bandwidth = IEEE80211_STA_RX_BW_40;
0651         break;
0652     case IEEE80211_OPMODE_NOTIF_CHANWIDTH_80MHZ:
0653         if (opmode & IEEE80211_OPMODE_NOTIF_BW_160_80P80)
0654             link_sta->cur_max_bandwidth = IEEE80211_STA_RX_BW_160;
0655         else
0656             link_sta->cur_max_bandwidth = IEEE80211_STA_RX_BW_80;
0657         break;
0658     case IEEE80211_OPMODE_NOTIF_CHANWIDTH_160MHZ:
0659         /* legacy only, no longer used by newer spec */
0660         link_sta->cur_max_bandwidth = IEEE80211_STA_RX_BW_160;
0661         break;
0662     }
0663 
0664     new_bw = ieee80211_sta_cur_vht_bw(link_sta);
0665     if (new_bw != link_sta->pub->bandwidth) {
0666         link_sta->pub->bandwidth = new_bw;
0667         sta_opmode.bw = ieee80211_sta_rx_bw_to_chan_width(link_sta);
0668         changed |= IEEE80211_RC_BW_CHANGED;
0669         sta_opmode.changed |= STA_OPMODE_MAX_BW_CHANGED;
0670     }
0671 
0672     if (sta_opmode.changed)
0673         cfg80211_sta_opmode_change_notify(sdata->dev, link_sta->addr,
0674                           &sta_opmode, GFP_KERNEL);
0675 
0676     return changed;
0677 }
0678 
0679 void ieee80211_process_mu_groups(struct ieee80211_sub_if_data *sdata,
0680                  struct ieee80211_link_data *link,
0681                  struct ieee80211_mgmt *mgmt)
0682 {
0683     struct ieee80211_bss_conf *link_conf = link->conf;
0684 
0685     if (!link_conf->mu_mimo_owner)
0686         return;
0687 
0688     if (!memcmp(mgmt->u.action.u.vht_group_notif.position,
0689             link_conf->mu_group.position, WLAN_USER_POSITION_LEN) &&
0690         !memcmp(mgmt->u.action.u.vht_group_notif.membership,
0691             link_conf->mu_group.membership, WLAN_MEMBERSHIP_LEN))
0692         return;
0693 
0694     memcpy(link_conf->mu_group.membership,
0695            mgmt->u.action.u.vht_group_notif.membership,
0696            WLAN_MEMBERSHIP_LEN);
0697     memcpy(link_conf->mu_group.position,
0698            mgmt->u.action.u.vht_group_notif.position,
0699            WLAN_USER_POSITION_LEN);
0700 
0701     ieee80211_link_info_change_notify(sdata, link,
0702                       BSS_CHANGED_MU_GROUPS);
0703 }
0704 
0705 void ieee80211_update_mu_groups(struct ieee80211_vif *vif, unsigned int link_id,
0706                 const u8 *membership, const u8 *position)
0707 {
0708     struct ieee80211_bss_conf *link_conf;
0709 
0710     rcu_read_lock();
0711     link_conf = rcu_dereference(vif->link_conf[link_id]);
0712 
0713     if (!WARN_ON_ONCE(!link_conf || !link_conf->mu_mimo_owner)) {
0714         memcpy(link_conf->mu_group.membership, membership,
0715                WLAN_MEMBERSHIP_LEN);
0716         memcpy(link_conf->mu_group.position, position,
0717                WLAN_USER_POSITION_LEN);
0718     }
0719     rcu_read_unlock();
0720 }
0721 EXPORT_SYMBOL_GPL(ieee80211_update_mu_groups);
0722 
0723 void ieee80211_vht_handle_opmode(struct ieee80211_sub_if_data *sdata,
0724                  struct link_sta_info *link_sta,
0725                  u8 opmode, enum nl80211_band band)
0726 {
0727     struct ieee80211_local *local = sdata->local;
0728     struct ieee80211_supported_band *sband = local->hw.wiphy->bands[band];
0729 
0730     u32 changed = __ieee80211_vht_handle_opmode(sdata, link_sta,
0731                             opmode, band);
0732 
0733     if (changed > 0) {
0734         ieee80211_recalc_min_chandef(sdata, link_sta->link_id);
0735         rate_control_rate_update(local, sband, link_sta->sta,
0736                      link_sta->link_id, changed);
0737     }
0738 }
0739 
0740 void ieee80211_get_vht_mask_from_cap(__le16 vht_cap,
0741                      u16 vht_mask[NL80211_VHT_NSS_MAX])
0742 {
0743     int i;
0744     u16 mask, cap = le16_to_cpu(vht_cap);
0745 
0746     for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
0747         mask = (cap >> i * 2) & IEEE80211_VHT_MCS_NOT_SUPPORTED;
0748         switch (mask) {
0749         case IEEE80211_VHT_MCS_SUPPORT_0_7:
0750             vht_mask[i] = 0x00FF;
0751             break;
0752         case IEEE80211_VHT_MCS_SUPPORT_0_8:
0753             vht_mask[i] = 0x01FF;
0754             break;
0755         case IEEE80211_VHT_MCS_SUPPORT_0_9:
0756             vht_mask[i] = 0x03FF;
0757             break;
0758         case IEEE80211_VHT_MCS_NOT_SUPPORTED:
0759         default:
0760             vht_mask[i] = 0;
0761             break;
0762         }
0763     }
0764 }