Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: ISC
0002 /*
0003  * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
0004  */
0005 #include <linux/sched.h>
0006 #include <linux/of.h>
0007 #include "mt76.h"
0008 
0009 #define CHAN2G(_idx, _freq) {           \
0010     .band = NL80211_BAND_2GHZ,      \
0011     .center_freq = (_freq),         \
0012     .hw_value = (_idx),         \
0013     .max_power = 30,            \
0014 }
0015 
0016 #define CHAN5G(_idx, _freq) {           \
0017     .band = NL80211_BAND_5GHZ,      \
0018     .center_freq = (_freq),         \
0019     .hw_value = (_idx),         \
0020     .max_power = 30,            \
0021 }
0022 
0023 #define CHAN6G(_idx, _freq) {           \
0024     .band = NL80211_BAND_6GHZ,      \
0025     .center_freq = (_freq),         \
0026     .hw_value = (_idx),         \
0027     .max_power = 30,            \
0028 }
0029 
0030 static const struct ieee80211_channel mt76_channels_2ghz[] = {
0031     CHAN2G(1, 2412),
0032     CHAN2G(2, 2417),
0033     CHAN2G(3, 2422),
0034     CHAN2G(4, 2427),
0035     CHAN2G(5, 2432),
0036     CHAN2G(6, 2437),
0037     CHAN2G(7, 2442),
0038     CHAN2G(8, 2447),
0039     CHAN2G(9, 2452),
0040     CHAN2G(10, 2457),
0041     CHAN2G(11, 2462),
0042     CHAN2G(12, 2467),
0043     CHAN2G(13, 2472),
0044     CHAN2G(14, 2484),
0045 };
0046 
0047 static const struct ieee80211_channel mt76_channels_5ghz[] = {
0048     CHAN5G(36, 5180),
0049     CHAN5G(40, 5200),
0050     CHAN5G(44, 5220),
0051     CHAN5G(48, 5240),
0052 
0053     CHAN5G(52, 5260),
0054     CHAN5G(56, 5280),
0055     CHAN5G(60, 5300),
0056     CHAN5G(64, 5320),
0057 
0058     CHAN5G(100, 5500),
0059     CHAN5G(104, 5520),
0060     CHAN5G(108, 5540),
0061     CHAN5G(112, 5560),
0062     CHAN5G(116, 5580),
0063     CHAN5G(120, 5600),
0064     CHAN5G(124, 5620),
0065     CHAN5G(128, 5640),
0066     CHAN5G(132, 5660),
0067     CHAN5G(136, 5680),
0068     CHAN5G(140, 5700),
0069     CHAN5G(144, 5720),
0070 
0071     CHAN5G(149, 5745),
0072     CHAN5G(153, 5765),
0073     CHAN5G(157, 5785),
0074     CHAN5G(161, 5805),
0075     CHAN5G(165, 5825),
0076     CHAN5G(169, 5845),
0077     CHAN5G(173, 5865),
0078 };
0079 
0080 static const struct ieee80211_channel mt76_channels_6ghz[] = {
0081     /* UNII-5 */
0082     CHAN6G(1, 5955),
0083     CHAN6G(5, 5975),
0084     CHAN6G(9, 5995),
0085     CHAN6G(13, 6015),
0086     CHAN6G(17, 6035),
0087     CHAN6G(21, 6055),
0088     CHAN6G(25, 6075),
0089     CHAN6G(29, 6095),
0090     CHAN6G(33, 6115),
0091     CHAN6G(37, 6135),
0092     CHAN6G(41, 6155),
0093     CHAN6G(45, 6175),
0094     CHAN6G(49, 6195),
0095     CHAN6G(53, 6215),
0096     CHAN6G(57, 6235),
0097     CHAN6G(61, 6255),
0098     CHAN6G(65, 6275),
0099     CHAN6G(69, 6295),
0100     CHAN6G(73, 6315),
0101     CHAN6G(77, 6335),
0102     CHAN6G(81, 6355),
0103     CHAN6G(85, 6375),
0104     CHAN6G(89, 6395),
0105     CHAN6G(93, 6415),
0106     /* UNII-6 */
0107     CHAN6G(97, 6435),
0108     CHAN6G(101, 6455),
0109     CHAN6G(105, 6475),
0110     CHAN6G(109, 6495),
0111     CHAN6G(113, 6515),
0112     CHAN6G(117, 6535),
0113     /* UNII-7 */
0114     CHAN6G(121, 6555),
0115     CHAN6G(125, 6575),
0116     CHAN6G(129, 6595),
0117     CHAN6G(133, 6615),
0118     CHAN6G(137, 6635),
0119     CHAN6G(141, 6655),
0120     CHAN6G(145, 6675),
0121     CHAN6G(149, 6695),
0122     CHAN6G(153, 6715),
0123     CHAN6G(157, 6735),
0124     CHAN6G(161, 6755),
0125     CHAN6G(165, 6775),
0126     CHAN6G(169, 6795),
0127     CHAN6G(173, 6815),
0128     CHAN6G(177, 6835),
0129     CHAN6G(181, 6855),
0130     CHAN6G(185, 6875),
0131     /* UNII-8 */
0132     CHAN6G(189, 6895),
0133     CHAN6G(193, 6915),
0134     CHAN6G(197, 6935),
0135     CHAN6G(201, 6955),
0136     CHAN6G(205, 6975),
0137     CHAN6G(209, 6995),
0138     CHAN6G(213, 7015),
0139     CHAN6G(217, 7035),
0140     CHAN6G(221, 7055),
0141     CHAN6G(225, 7075),
0142     CHAN6G(229, 7095),
0143     CHAN6G(233, 7115),
0144 };
0145 
0146 static const struct ieee80211_tpt_blink mt76_tpt_blink[] = {
0147     { .throughput =   0 * 1024, .blink_time = 334 },
0148     { .throughput =   1 * 1024, .blink_time = 260 },
0149     { .throughput =   5 * 1024, .blink_time = 220 },
0150     { .throughput =  10 * 1024, .blink_time = 190 },
0151     { .throughput =  20 * 1024, .blink_time = 170 },
0152     { .throughput =  50 * 1024, .blink_time = 150 },
0153     { .throughput =  70 * 1024, .blink_time = 130 },
0154     { .throughput = 100 * 1024, .blink_time = 110 },
0155     { .throughput = 200 * 1024, .blink_time =  80 },
0156     { .throughput = 300 * 1024, .blink_time =  50 },
0157 };
0158 
0159 struct ieee80211_rate mt76_rates[] = {
0160     CCK_RATE(0, 10),
0161     CCK_RATE(1, 20),
0162     CCK_RATE(2, 55),
0163     CCK_RATE(3, 110),
0164     OFDM_RATE(11, 60),
0165     OFDM_RATE(15, 90),
0166     OFDM_RATE(10, 120),
0167     OFDM_RATE(14, 180),
0168     OFDM_RATE(9,  240),
0169     OFDM_RATE(13, 360),
0170     OFDM_RATE(8,  480),
0171     OFDM_RATE(12, 540),
0172 };
0173 EXPORT_SYMBOL_GPL(mt76_rates);
0174 
0175 static const struct cfg80211_sar_freq_ranges mt76_sar_freq_ranges[] = {
0176     { .start_freq = 2402, .end_freq = 2494, },
0177     { .start_freq = 5150, .end_freq = 5350, },
0178     { .start_freq = 5350, .end_freq = 5470, },
0179     { .start_freq = 5470, .end_freq = 5725, },
0180     { .start_freq = 5725, .end_freq = 5950, },
0181     { .start_freq = 5945, .end_freq = 6165, },
0182     { .start_freq = 6165, .end_freq = 6405, },
0183     { .start_freq = 6405, .end_freq = 6525, },
0184     { .start_freq = 6525, .end_freq = 6705, },
0185     { .start_freq = 6705, .end_freq = 6865, },
0186     { .start_freq = 6865, .end_freq = 7125, },
0187 };
0188 
0189 static const struct cfg80211_sar_capa mt76_sar_capa = {
0190     .type = NL80211_SAR_TYPE_POWER,
0191     .num_freq_ranges = ARRAY_SIZE(mt76_sar_freq_ranges),
0192     .freq_ranges = &mt76_sar_freq_ranges[0],
0193 };
0194 
0195 static int mt76_led_init(struct mt76_dev *dev)
0196 {
0197     struct device_node *np = dev->dev->of_node;
0198     struct ieee80211_hw *hw = dev->hw;
0199     int led_pin;
0200 
0201     if (!dev->led_cdev.brightness_set && !dev->led_cdev.blink_set)
0202         return 0;
0203 
0204     snprintf(dev->led_name, sizeof(dev->led_name),
0205          "mt76-%s", wiphy_name(hw->wiphy));
0206 
0207     dev->led_cdev.name = dev->led_name;
0208     dev->led_cdev.default_trigger =
0209         ieee80211_create_tpt_led_trigger(hw,
0210                     IEEE80211_TPT_LEDTRIG_FL_RADIO,
0211                     mt76_tpt_blink,
0212                     ARRAY_SIZE(mt76_tpt_blink));
0213 
0214     np = of_get_child_by_name(np, "led");
0215     if (np) {
0216         if (!of_property_read_u32(np, "led-sources", &led_pin))
0217             dev->led_pin = led_pin;
0218         dev->led_al = of_property_read_bool(np, "led-active-low");
0219         of_node_put(np);
0220     }
0221 
0222     return led_classdev_register(dev->dev, &dev->led_cdev);
0223 }
0224 
0225 static void mt76_led_cleanup(struct mt76_dev *dev)
0226 {
0227     if (!dev->led_cdev.brightness_set && !dev->led_cdev.blink_set)
0228         return;
0229 
0230     led_classdev_unregister(&dev->led_cdev);
0231 }
0232 
0233 static void mt76_init_stream_cap(struct mt76_phy *phy,
0234                  struct ieee80211_supported_band *sband,
0235                  bool vht)
0236 {
0237     struct ieee80211_sta_ht_cap *ht_cap = &sband->ht_cap;
0238     int i, nstream = hweight8(phy->antenna_mask);
0239     struct ieee80211_sta_vht_cap *vht_cap;
0240     u16 mcs_map = 0;
0241 
0242     if (nstream > 1)
0243         ht_cap->cap |= IEEE80211_HT_CAP_TX_STBC;
0244     else
0245         ht_cap->cap &= ~IEEE80211_HT_CAP_TX_STBC;
0246 
0247     for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
0248         ht_cap->mcs.rx_mask[i] = i < nstream ? 0xff : 0;
0249 
0250     if (!vht)
0251         return;
0252 
0253     vht_cap = &sband->vht_cap;
0254     if (nstream > 1)
0255         vht_cap->cap |= IEEE80211_VHT_CAP_TXSTBC;
0256     else
0257         vht_cap->cap &= ~IEEE80211_VHT_CAP_TXSTBC;
0258     vht_cap->cap |= IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN |
0259             IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN;
0260 
0261     for (i = 0; i < 8; i++) {
0262         if (i < nstream)
0263             mcs_map |= (IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2));
0264         else
0265             mcs_map |=
0266                 (IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2));
0267     }
0268     vht_cap->vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
0269     vht_cap->vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
0270     if (ieee80211_hw_check(phy->hw, SUPPORTS_VHT_EXT_NSS_BW))
0271         vht_cap->vht_mcs.tx_highest |=
0272                 cpu_to_le16(IEEE80211_VHT_EXT_NSS_BW_CAPABLE);
0273 }
0274 
0275 void mt76_set_stream_caps(struct mt76_phy *phy, bool vht)
0276 {
0277     if (phy->cap.has_2ghz)
0278         mt76_init_stream_cap(phy, &phy->sband_2g.sband, false);
0279     if (phy->cap.has_5ghz)
0280         mt76_init_stream_cap(phy, &phy->sband_5g.sband, vht);
0281     if (phy->cap.has_6ghz)
0282         mt76_init_stream_cap(phy, &phy->sband_6g.sband, vht);
0283 }
0284 EXPORT_SYMBOL_GPL(mt76_set_stream_caps);
0285 
0286 static int
0287 mt76_init_sband(struct mt76_phy *phy, struct mt76_sband *msband,
0288         const struct ieee80211_channel *chan, int n_chan,
0289         struct ieee80211_rate *rates, int n_rates,
0290         bool ht, bool vht)
0291 {
0292     struct ieee80211_supported_band *sband = &msband->sband;
0293     struct ieee80211_sta_vht_cap *vht_cap;
0294     struct ieee80211_sta_ht_cap *ht_cap;
0295     struct mt76_dev *dev = phy->dev;
0296     void *chanlist;
0297     int size;
0298 
0299     size = n_chan * sizeof(*chan);
0300     chanlist = devm_kmemdup(dev->dev, chan, size, GFP_KERNEL);
0301     if (!chanlist)
0302         return -ENOMEM;
0303 
0304     msband->chan = devm_kcalloc(dev->dev, n_chan, sizeof(*msband->chan),
0305                     GFP_KERNEL);
0306     if (!msband->chan)
0307         return -ENOMEM;
0308 
0309     sband->channels = chanlist;
0310     sband->n_channels = n_chan;
0311     sband->bitrates = rates;
0312     sband->n_bitrates = n_rates;
0313 
0314     if (!ht)
0315         return 0;
0316 
0317     ht_cap = &sband->ht_cap;
0318     ht_cap->ht_supported = true;
0319     ht_cap->cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
0320                IEEE80211_HT_CAP_GRN_FLD |
0321                IEEE80211_HT_CAP_SGI_20 |
0322                IEEE80211_HT_CAP_SGI_40 |
0323                (1 << IEEE80211_HT_CAP_RX_STBC_SHIFT);
0324 
0325     ht_cap->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
0326     ht_cap->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
0327 
0328     mt76_init_stream_cap(phy, sband, vht);
0329 
0330     if (!vht)
0331         return 0;
0332 
0333     vht_cap = &sband->vht_cap;
0334     vht_cap->vht_supported = true;
0335     vht_cap->cap |= IEEE80211_VHT_CAP_RXLDPC |
0336             IEEE80211_VHT_CAP_RXSTBC_1 |
0337             IEEE80211_VHT_CAP_SHORT_GI_80 |
0338             (3 << IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT);
0339 
0340     return 0;
0341 }
0342 
0343 static int
0344 mt76_init_sband_2g(struct mt76_phy *phy, struct ieee80211_rate *rates,
0345            int n_rates)
0346 {
0347     phy->hw->wiphy->bands[NL80211_BAND_2GHZ] = &phy->sband_2g.sband;
0348 
0349     return mt76_init_sband(phy, &phy->sband_2g, mt76_channels_2ghz,
0350                    ARRAY_SIZE(mt76_channels_2ghz), rates,
0351                    n_rates, true, false);
0352 }
0353 
0354 static int
0355 mt76_init_sband_5g(struct mt76_phy *phy, struct ieee80211_rate *rates,
0356            int n_rates, bool vht)
0357 {
0358     phy->hw->wiphy->bands[NL80211_BAND_5GHZ] = &phy->sband_5g.sband;
0359 
0360     return mt76_init_sband(phy, &phy->sband_5g, mt76_channels_5ghz,
0361                    ARRAY_SIZE(mt76_channels_5ghz), rates,
0362                    n_rates, true, vht);
0363 }
0364 
0365 static int
0366 mt76_init_sband_6g(struct mt76_phy *phy, struct ieee80211_rate *rates,
0367            int n_rates)
0368 {
0369     phy->hw->wiphy->bands[NL80211_BAND_6GHZ] = &phy->sband_6g.sband;
0370 
0371     return mt76_init_sband(phy, &phy->sband_6g, mt76_channels_6ghz,
0372                    ARRAY_SIZE(mt76_channels_6ghz), rates,
0373                    n_rates, false, false);
0374 }
0375 
0376 static void
0377 mt76_check_sband(struct mt76_phy *phy, struct mt76_sband *msband,
0378          enum nl80211_band band)
0379 {
0380     struct ieee80211_supported_band *sband = &msband->sband;
0381     bool found = false;
0382     int i;
0383 
0384     if (!sband)
0385         return;
0386 
0387     for (i = 0; i < sband->n_channels; i++) {
0388         if (sband->channels[i].flags & IEEE80211_CHAN_DISABLED)
0389             continue;
0390 
0391         found = true;
0392         break;
0393     }
0394 
0395     if (found) {
0396         phy->chandef.chan = &sband->channels[0];
0397         phy->chan_state = &msband->chan[0];
0398         return;
0399     }
0400 
0401     sband->n_channels = 0;
0402     phy->hw->wiphy->bands[band] = NULL;
0403 }
0404 
0405 static int
0406 mt76_phy_init(struct mt76_phy *phy, struct ieee80211_hw *hw)
0407 {
0408     struct mt76_dev *dev = phy->dev;
0409     struct wiphy *wiphy = hw->wiphy;
0410 
0411     SET_IEEE80211_DEV(hw, dev->dev);
0412     SET_IEEE80211_PERM_ADDR(hw, phy->macaddr);
0413 
0414     wiphy->features |= NL80211_FEATURE_ACTIVE_MONITOR;
0415     wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH |
0416             WIPHY_FLAG_SUPPORTS_TDLS |
0417             WIPHY_FLAG_AP_UAPSD;
0418 
0419     wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
0420     wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_AIRTIME_FAIRNESS);
0421     wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_AQL);
0422 
0423     wiphy->available_antennas_tx = phy->antenna_mask;
0424     wiphy->available_antennas_rx = phy->antenna_mask;
0425 
0426     wiphy->sar_capa = &mt76_sar_capa;
0427     phy->frp = devm_kcalloc(dev->dev, wiphy->sar_capa->num_freq_ranges,
0428                 sizeof(struct mt76_freq_range_power),
0429                 GFP_KERNEL);
0430     if (!phy->frp)
0431         return -ENOMEM;
0432 
0433     hw->txq_data_size = sizeof(struct mt76_txq);
0434     hw->uapsd_max_sp_len = IEEE80211_WMM_IE_STA_QOSINFO_SP_ALL;
0435 
0436     if (!hw->max_tx_fragments)
0437         hw->max_tx_fragments = 16;
0438 
0439     ieee80211_hw_set(hw, SIGNAL_DBM);
0440     ieee80211_hw_set(hw, AMPDU_AGGREGATION);
0441     ieee80211_hw_set(hw, SUPPORTS_RC_TABLE);
0442     ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
0443     ieee80211_hw_set(hw, SUPPORTS_CLONED_SKBS);
0444     ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU);
0445     ieee80211_hw_set(hw, SUPPORTS_REORDERING_BUFFER);
0446     ieee80211_hw_set(hw, TX_AMSDU);
0447     ieee80211_hw_set(hw, TX_FRAG_LIST);
0448     ieee80211_hw_set(hw, MFP_CAPABLE);
0449     ieee80211_hw_set(hw, AP_LINK_PS);
0450     ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
0451 
0452     return 0;
0453 }
0454 
0455 struct mt76_phy *
0456 mt76_alloc_phy(struct mt76_dev *dev, unsigned int size,
0457            const struct ieee80211_ops *ops, u8 band_idx)
0458 {
0459     struct ieee80211_hw *hw;
0460     unsigned int phy_size;
0461     struct mt76_phy *phy;
0462 
0463     phy_size = ALIGN(sizeof(*phy), 8);
0464     hw = ieee80211_alloc_hw(size + phy_size, ops);
0465     if (!hw)
0466         return NULL;
0467 
0468     phy = hw->priv;
0469     phy->dev = dev;
0470     phy->hw = hw;
0471     phy->priv = hw->priv + phy_size;
0472     phy->band_idx = band_idx;
0473 
0474     hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
0475     hw->wiphy->interface_modes =
0476         BIT(NL80211_IFTYPE_STATION) |
0477         BIT(NL80211_IFTYPE_AP) |
0478 #ifdef CONFIG_MAC80211_MESH
0479         BIT(NL80211_IFTYPE_MESH_POINT) |
0480 #endif
0481         BIT(NL80211_IFTYPE_P2P_CLIENT) |
0482         BIT(NL80211_IFTYPE_P2P_GO) |
0483         BIT(NL80211_IFTYPE_ADHOC);
0484 
0485     return phy;
0486 }
0487 EXPORT_SYMBOL_GPL(mt76_alloc_phy);
0488 
0489 int mt76_register_phy(struct mt76_phy *phy, bool vht,
0490               struct ieee80211_rate *rates, int n_rates)
0491 {
0492     int ret;
0493 
0494     ret = mt76_phy_init(phy, phy->hw);
0495     if (ret)
0496         return ret;
0497 
0498     if (phy->cap.has_2ghz) {
0499         ret = mt76_init_sband_2g(phy, rates, n_rates);
0500         if (ret)
0501             return ret;
0502     }
0503 
0504     if (phy->cap.has_5ghz) {
0505         ret = mt76_init_sband_5g(phy, rates + 4, n_rates - 4, vht);
0506         if (ret)
0507             return ret;
0508     }
0509 
0510     if (phy->cap.has_6ghz) {
0511         ret = mt76_init_sband_6g(phy, rates + 4, n_rates - 4);
0512         if (ret)
0513             return ret;
0514     }
0515 
0516     wiphy_read_of_freq_limits(phy->hw->wiphy);
0517     mt76_check_sband(phy, &phy->sband_2g, NL80211_BAND_2GHZ);
0518     mt76_check_sband(phy, &phy->sband_5g, NL80211_BAND_5GHZ);
0519     mt76_check_sband(phy, &phy->sband_6g, NL80211_BAND_6GHZ);
0520 
0521     ret = ieee80211_register_hw(phy->hw);
0522     if (ret)
0523         return ret;
0524 
0525     phy->dev->phys[phy->band_idx] = phy;
0526 
0527     return 0;
0528 }
0529 EXPORT_SYMBOL_GPL(mt76_register_phy);
0530 
0531 void mt76_unregister_phy(struct mt76_phy *phy)
0532 {
0533     struct mt76_dev *dev = phy->dev;
0534 
0535     mt76_tx_status_check(dev, true);
0536     ieee80211_unregister_hw(phy->hw);
0537     dev->phys[phy->band_idx] = NULL;
0538 }
0539 EXPORT_SYMBOL_GPL(mt76_unregister_phy);
0540 
0541 struct mt76_dev *
0542 mt76_alloc_device(struct device *pdev, unsigned int size,
0543           const struct ieee80211_ops *ops,
0544           const struct mt76_driver_ops *drv_ops)
0545 {
0546     struct ieee80211_hw *hw;
0547     struct mt76_phy *phy;
0548     struct mt76_dev *dev;
0549     int i;
0550 
0551     hw = ieee80211_alloc_hw(size, ops);
0552     if (!hw)
0553         return NULL;
0554 
0555     dev = hw->priv;
0556     dev->hw = hw;
0557     dev->dev = pdev;
0558     dev->drv = drv_ops;
0559     dev->dma_dev = pdev;
0560 
0561     phy = &dev->phy;
0562     phy->dev = dev;
0563     phy->hw = hw;
0564     phy->band_idx = MT_BAND0;
0565     dev->phys[phy->band_idx] = phy;
0566 
0567     spin_lock_init(&dev->rx_lock);
0568     spin_lock_init(&dev->lock);
0569     spin_lock_init(&dev->cc_lock);
0570     spin_lock_init(&dev->status_lock);
0571     mutex_init(&dev->mutex);
0572     init_waitqueue_head(&dev->tx_wait);
0573 
0574     skb_queue_head_init(&dev->mcu.res_q);
0575     init_waitqueue_head(&dev->mcu.wait);
0576     mutex_init(&dev->mcu.mutex);
0577     dev->tx_worker.fn = mt76_tx_worker;
0578 
0579     hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
0580     hw->wiphy->interface_modes =
0581         BIT(NL80211_IFTYPE_STATION) |
0582         BIT(NL80211_IFTYPE_AP) |
0583 #ifdef CONFIG_MAC80211_MESH
0584         BIT(NL80211_IFTYPE_MESH_POINT) |
0585 #endif
0586         BIT(NL80211_IFTYPE_P2P_CLIENT) |
0587         BIT(NL80211_IFTYPE_P2P_GO) |
0588         BIT(NL80211_IFTYPE_ADHOC);
0589 
0590     spin_lock_init(&dev->token_lock);
0591     idr_init(&dev->token);
0592 
0593     INIT_LIST_HEAD(&dev->wcid_list);
0594 
0595     INIT_LIST_HEAD(&dev->txwi_cache);
0596     dev->token_size = dev->drv->token_size;
0597 
0598     for (i = 0; i < ARRAY_SIZE(dev->q_rx); i++)
0599         skb_queue_head_init(&dev->rx_skb[i]);
0600 
0601     dev->wq = alloc_ordered_workqueue("mt76", 0);
0602     if (!dev->wq) {
0603         ieee80211_free_hw(hw);
0604         return NULL;
0605     }
0606 
0607     return dev;
0608 }
0609 EXPORT_SYMBOL_GPL(mt76_alloc_device);
0610 
0611 int mt76_register_device(struct mt76_dev *dev, bool vht,
0612              struct ieee80211_rate *rates, int n_rates)
0613 {
0614     struct ieee80211_hw *hw = dev->hw;
0615     struct mt76_phy *phy = &dev->phy;
0616     int ret;
0617 
0618     dev_set_drvdata(dev->dev, dev);
0619     ret = mt76_phy_init(phy, hw);
0620     if (ret)
0621         return ret;
0622 
0623     if (phy->cap.has_2ghz) {
0624         ret = mt76_init_sband_2g(phy, rates, n_rates);
0625         if (ret)
0626             return ret;
0627     }
0628 
0629     if (phy->cap.has_5ghz) {
0630         ret = mt76_init_sband_5g(phy, rates + 4, n_rates - 4, vht);
0631         if (ret)
0632             return ret;
0633     }
0634 
0635     if (phy->cap.has_6ghz) {
0636         ret = mt76_init_sband_6g(phy, rates + 4, n_rates - 4);
0637         if (ret)
0638             return ret;
0639     }
0640 
0641     wiphy_read_of_freq_limits(hw->wiphy);
0642     mt76_check_sband(&dev->phy, &phy->sband_2g, NL80211_BAND_2GHZ);
0643     mt76_check_sband(&dev->phy, &phy->sband_5g, NL80211_BAND_5GHZ);
0644     mt76_check_sband(&dev->phy, &phy->sband_6g, NL80211_BAND_6GHZ);
0645 
0646     if (IS_ENABLED(CONFIG_MT76_LEDS)) {
0647         ret = mt76_led_init(dev);
0648         if (ret)
0649             return ret;
0650     }
0651 
0652     ret = ieee80211_register_hw(hw);
0653     if (ret)
0654         return ret;
0655 
0656     WARN_ON(mt76_worker_setup(hw, &dev->tx_worker, NULL, "tx"));
0657     sched_set_fifo_low(dev->tx_worker.task);
0658 
0659     return 0;
0660 }
0661 EXPORT_SYMBOL_GPL(mt76_register_device);
0662 
0663 void mt76_unregister_device(struct mt76_dev *dev)
0664 {
0665     struct ieee80211_hw *hw = dev->hw;
0666 
0667     if (IS_ENABLED(CONFIG_MT76_LEDS))
0668         mt76_led_cleanup(dev);
0669     mt76_tx_status_check(dev, true);
0670     ieee80211_unregister_hw(hw);
0671 }
0672 EXPORT_SYMBOL_GPL(mt76_unregister_device);
0673 
0674 void mt76_free_device(struct mt76_dev *dev)
0675 {
0676     mt76_worker_teardown(&dev->tx_worker);
0677     if (dev->wq) {
0678         destroy_workqueue(dev->wq);
0679         dev->wq = NULL;
0680     }
0681     ieee80211_free_hw(dev->hw);
0682 }
0683 EXPORT_SYMBOL_GPL(mt76_free_device);
0684 
0685 static void mt76_rx_release_amsdu(struct mt76_phy *phy, enum mt76_rxq_id q)
0686 {
0687     struct sk_buff *skb = phy->rx_amsdu[q].head;
0688     struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
0689     struct mt76_dev *dev = phy->dev;
0690 
0691     phy->rx_amsdu[q].head = NULL;
0692     phy->rx_amsdu[q].tail = NULL;
0693 
0694     /*
0695      * Validate if the amsdu has a proper first subframe.
0696      * A single MSDU can be parsed as A-MSDU when the unauthenticated A-MSDU
0697      * flag of the QoS header gets flipped. In such cases, the first
0698      * subframe has a LLC/SNAP header in the location of the destination
0699      * address.
0700      */
0701     if (skb_shinfo(skb)->frag_list) {
0702         int offset = 0;
0703 
0704         if (!(status->flag & RX_FLAG_8023)) {
0705             offset = ieee80211_get_hdrlen_from_skb(skb);
0706 
0707             if ((status->flag &
0708                  (RX_FLAG_DECRYPTED | RX_FLAG_IV_STRIPPED)) ==
0709                 RX_FLAG_DECRYPTED)
0710                 offset += 8;
0711         }
0712 
0713         if (ether_addr_equal(skb->data + offset, rfc1042_header)) {
0714             dev_kfree_skb(skb);
0715             return;
0716         }
0717     }
0718     __skb_queue_tail(&dev->rx_skb[q], skb);
0719 }
0720 
0721 static void mt76_rx_release_burst(struct mt76_phy *phy, enum mt76_rxq_id q,
0722                   struct sk_buff *skb)
0723 {
0724     struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
0725 
0726     if (phy->rx_amsdu[q].head &&
0727         (!status->amsdu || status->first_amsdu ||
0728          status->seqno != phy->rx_amsdu[q].seqno))
0729         mt76_rx_release_amsdu(phy, q);
0730 
0731     if (!phy->rx_amsdu[q].head) {
0732         phy->rx_amsdu[q].tail = &skb_shinfo(skb)->frag_list;
0733         phy->rx_amsdu[q].seqno = status->seqno;
0734         phy->rx_amsdu[q].head = skb;
0735     } else {
0736         *phy->rx_amsdu[q].tail = skb;
0737         phy->rx_amsdu[q].tail = &skb->next;
0738     }
0739 
0740     if (!status->amsdu || status->last_amsdu)
0741         mt76_rx_release_amsdu(phy, q);
0742 }
0743 
0744 void mt76_rx(struct mt76_dev *dev, enum mt76_rxq_id q, struct sk_buff *skb)
0745 {
0746     struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
0747     struct mt76_phy *phy = mt76_dev_phy(dev, status->phy_idx);
0748 
0749     if (!test_bit(MT76_STATE_RUNNING, &phy->state)) {
0750         dev_kfree_skb(skb);
0751         return;
0752     }
0753 
0754 #ifdef CONFIG_NL80211_TESTMODE
0755     if (phy->test.state == MT76_TM_STATE_RX_FRAMES) {
0756         phy->test.rx_stats.packets[q]++;
0757         if (status->flag & RX_FLAG_FAILED_FCS_CRC)
0758             phy->test.rx_stats.fcs_error[q]++;
0759     }
0760 #endif
0761 
0762     mt76_rx_release_burst(phy, q, skb);
0763 }
0764 EXPORT_SYMBOL_GPL(mt76_rx);
0765 
0766 bool mt76_has_tx_pending(struct mt76_phy *phy)
0767 {
0768     struct mt76_queue *q;
0769     int i;
0770 
0771     for (i = 0; i < __MT_TXQ_MAX; i++) {
0772         q = phy->q_tx[i];
0773         if (q && q->queued)
0774             return true;
0775     }
0776 
0777     return false;
0778 }
0779 EXPORT_SYMBOL_GPL(mt76_has_tx_pending);
0780 
0781 static struct mt76_channel_state *
0782 mt76_channel_state(struct mt76_phy *phy, struct ieee80211_channel *c)
0783 {
0784     struct mt76_sband *msband;
0785     int idx;
0786 
0787     if (c->band == NL80211_BAND_2GHZ)
0788         msband = &phy->sband_2g;
0789     else if (c->band == NL80211_BAND_6GHZ)
0790         msband = &phy->sband_6g;
0791     else
0792         msband = &phy->sband_5g;
0793 
0794     idx = c - &msband->sband.channels[0];
0795     return &msband->chan[idx];
0796 }
0797 
0798 void mt76_update_survey_active_time(struct mt76_phy *phy, ktime_t time)
0799 {
0800     struct mt76_channel_state *state = phy->chan_state;
0801 
0802     state->cc_active += ktime_to_us(ktime_sub(time,
0803                           phy->survey_time));
0804     phy->survey_time = time;
0805 }
0806 EXPORT_SYMBOL_GPL(mt76_update_survey_active_time);
0807 
0808 void mt76_update_survey(struct mt76_phy *phy)
0809 {
0810     struct mt76_dev *dev = phy->dev;
0811     ktime_t cur_time;
0812 
0813     if (dev->drv->update_survey)
0814         dev->drv->update_survey(phy);
0815 
0816     cur_time = ktime_get_boottime();
0817     mt76_update_survey_active_time(phy, cur_time);
0818 
0819     if (dev->drv->drv_flags & MT_DRV_SW_RX_AIRTIME) {
0820         struct mt76_channel_state *state = phy->chan_state;
0821 
0822         spin_lock_bh(&dev->cc_lock);
0823         state->cc_bss_rx += dev->cur_cc_bss_rx;
0824         dev->cur_cc_bss_rx = 0;
0825         spin_unlock_bh(&dev->cc_lock);
0826     }
0827 }
0828 EXPORT_SYMBOL_GPL(mt76_update_survey);
0829 
0830 void mt76_set_channel(struct mt76_phy *phy)
0831 {
0832     struct mt76_dev *dev = phy->dev;
0833     struct ieee80211_hw *hw = phy->hw;
0834     struct cfg80211_chan_def *chandef = &hw->conf.chandef;
0835     bool offchannel = hw->conf.flags & IEEE80211_CONF_OFFCHANNEL;
0836     int timeout = HZ / 5;
0837 
0838     wait_event_timeout(dev->tx_wait, !mt76_has_tx_pending(phy), timeout);
0839     mt76_update_survey(phy);
0840 
0841     if (phy->chandef.chan->center_freq != chandef->chan->center_freq ||
0842         phy->chandef.width != chandef->width)
0843         phy->dfs_state = MT_DFS_STATE_UNKNOWN;
0844 
0845     phy->chandef = *chandef;
0846     phy->chan_state = mt76_channel_state(phy, chandef->chan);
0847 
0848     if (!offchannel)
0849         phy->main_chan = chandef->chan;
0850 
0851     if (chandef->chan != phy->main_chan)
0852         memset(phy->chan_state, 0, sizeof(*phy->chan_state));
0853 }
0854 EXPORT_SYMBOL_GPL(mt76_set_channel);
0855 
0856 int mt76_get_survey(struct ieee80211_hw *hw, int idx,
0857             struct survey_info *survey)
0858 {
0859     struct mt76_phy *phy = hw->priv;
0860     struct mt76_dev *dev = phy->dev;
0861     struct mt76_sband *sband;
0862     struct ieee80211_channel *chan;
0863     struct mt76_channel_state *state;
0864     int ret = 0;
0865 
0866     mutex_lock(&dev->mutex);
0867     if (idx == 0 && dev->drv->update_survey)
0868         mt76_update_survey(phy);
0869 
0870     if (idx >= phy->sband_2g.sband.n_channels +
0871            phy->sband_5g.sband.n_channels) {
0872         idx -= (phy->sband_2g.sband.n_channels +
0873             phy->sband_5g.sband.n_channels);
0874         sband = &phy->sband_6g;
0875     } else if (idx >= phy->sband_2g.sband.n_channels) {
0876         idx -= phy->sband_2g.sband.n_channels;
0877         sband = &phy->sband_5g;
0878     } else {
0879         sband = &phy->sband_2g;
0880     }
0881 
0882     if (idx >= sband->sband.n_channels) {
0883         ret = -ENOENT;
0884         goto out;
0885     }
0886 
0887     chan = &sband->sband.channels[idx];
0888     state = mt76_channel_state(phy, chan);
0889 
0890     memset(survey, 0, sizeof(*survey));
0891     survey->channel = chan;
0892     survey->filled = SURVEY_INFO_TIME | SURVEY_INFO_TIME_BUSY;
0893     survey->filled |= dev->drv->survey_flags;
0894     if (state->noise)
0895         survey->filled |= SURVEY_INFO_NOISE_DBM;
0896 
0897     if (chan == phy->main_chan) {
0898         survey->filled |= SURVEY_INFO_IN_USE;
0899 
0900         if (dev->drv->drv_flags & MT_DRV_SW_RX_AIRTIME)
0901             survey->filled |= SURVEY_INFO_TIME_BSS_RX;
0902     }
0903 
0904     survey->time_busy = div_u64(state->cc_busy, 1000);
0905     survey->time_rx = div_u64(state->cc_rx, 1000);
0906     survey->time = div_u64(state->cc_active, 1000);
0907     survey->noise = state->noise;
0908 
0909     spin_lock_bh(&dev->cc_lock);
0910     survey->time_bss_rx = div_u64(state->cc_bss_rx, 1000);
0911     survey->time_tx = div_u64(state->cc_tx, 1000);
0912     spin_unlock_bh(&dev->cc_lock);
0913 
0914 out:
0915     mutex_unlock(&dev->mutex);
0916 
0917     return ret;
0918 }
0919 EXPORT_SYMBOL_GPL(mt76_get_survey);
0920 
0921 void mt76_wcid_key_setup(struct mt76_dev *dev, struct mt76_wcid *wcid,
0922              struct ieee80211_key_conf *key)
0923 {
0924     struct ieee80211_key_seq seq;
0925     int i;
0926 
0927     wcid->rx_check_pn = false;
0928 
0929     if (!key)
0930         return;
0931 
0932     if (key->cipher != WLAN_CIPHER_SUITE_CCMP)
0933         return;
0934 
0935     wcid->rx_check_pn = true;
0936 
0937     /* data frame */
0938     for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
0939         ieee80211_get_key_rx_seq(key, i, &seq);
0940         memcpy(wcid->rx_key_pn[i], seq.ccmp.pn, sizeof(seq.ccmp.pn));
0941     }
0942 
0943     /* robust management frame */
0944     ieee80211_get_key_rx_seq(key, -1, &seq);
0945     memcpy(wcid->rx_key_pn[i], seq.ccmp.pn, sizeof(seq.ccmp.pn));
0946 
0947 }
0948 EXPORT_SYMBOL(mt76_wcid_key_setup);
0949 
0950 static int
0951 mt76_rx_signal(struct mt76_rx_status *status)
0952 {
0953     s8 *chain_signal = status->chain_signal;
0954     int signal = -128;
0955     u8 chains;
0956 
0957     for (chains = status->chains; chains; chains >>= 1, chain_signal++) {
0958         int cur, diff;
0959 
0960         cur = *chain_signal;
0961         if (!(chains & BIT(0)) ||
0962             cur > 0)
0963             continue;
0964 
0965         if (cur > signal)
0966             swap(cur, signal);
0967 
0968         diff = signal - cur;
0969         if (diff == 0)
0970             signal += 3;
0971         else if (diff <= 2)
0972             signal += 2;
0973         else if (diff <= 6)
0974             signal += 1;
0975     }
0976 
0977     return signal;
0978 }
0979 
0980 static void
0981 mt76_rx_convert(struct mt76_dev *dev, struct sk_buff *skb,
0982         struct ieee80211_hw **hw,
0983         struct ieee80211_sta **sta)
0984 {
0985     struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
0986     struct ieee80211_hdr *hdr = mt76_skb_get_hdr(skb);
0987     struct mt76_rx_status mstat;
0988 
0989     mstat = *((struct mt76_rx_status *)skb->cb);
0990     memset(status, 0, sizeof(*status));
0991 
0992     status->flag = mstat.flag;
0993     status->freq = mstat.freq;
0994     status->enc_flags = mstat.enc_flags;
0995     status->encoding = mstat.encoding;
0996     status->bw = mstat.bw;
0997     status->he_ru = mstat.he_ru;
0998     status->he_gi = mstat.he_gi;
0999     status->he_dcm = mstat.he_dcm;
1000     status->rate_idx = mstat.rate_idx;
1001     status->nss = mstat.nss;
1002     status->band = mstat.band;
1003     status->signal = mstat.signal;
1004     status->chains = mstat.chains;
1005     status->ampdu_reference = mstat.ampdu_ref;
1006     status->device_timestamp = mstat.timestamp;
1007     status->mactime = mstat.timestamp;
1008     status->signal = mt76_rx_signal(&mstat);
1009     if (status->signal <= -128)
1010         status->flag |= RX_FLAG_NO_SIGNAL_VAL;
1011 
1012     if (ieee80211_is_beacon(hdr->frame_control) ||
1013         ieee80211_is_probe_resp(hdr->frame_control))
1014         status->boottime_ns = ktime_get_boottime_ns();
1015 
1016     BUILD_BUG_ON(sizeof(mstat) > sizeof(skb->cb));
1017     BUILD_BUG_ON(sizeof(status->chain_signal) !=
1018              sizeof(mstat.chain_signal));
1019     memcpy(status->chain_signal, mstat.chain_signal,
1020            sizeof(mstat.chain_signal));
1021 
1022     *sta = wcid_to_sta(mstat.wcid);
1023     *hw = mt76_phy_hw(dev, mstat.phy_idx);
1024 }
1025 
1026 static void
1027 mt76_check_ccmp_pn(struct sk_buff *skb)
1028 {
1029     struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
1030     struct mt76_wcid *wcid = status->wcid;
1031     struct ieee80211_hdr *hdr;
1032     int security_idx;
1033     int ret;
1034 
1035     if (!(status->flag & RX_FLAG_DECRYPTED))
1036         return;
1037 
1038     if (status->flag & RX_FLAG_ONLY_MONITOR)
1039         return;
1040 
1041     if (!wcid || !wcid->rx_check_pn)
1042         return;
1043 
1044     security_idx = status->qos_ctl & IEEE80211_QOS_CTL_TID_MASK;
1045     if (status->flag & RX_FLAG_8023)
1046         goto skip_hdr_check;
1047 
1048     hdr = mt76_skb_get_hdr(skb);
1049     if (!(status->flag & RX_FLAG_IV_STRIPPED)) {
1050         /*
1051          * Validate the first fragment both here and in mac80211
1052          * All further fragments will be validated by mac80211 only.
1053          */
1054         if (ieee80211_is_frag(hdr) &&
1055             !ieee80211_is_first_frag(hdr->frame_control))
1056             return;
1057     }
1058 
1059     /* IEEE 802.11-2020, 12.5.3.4.4 "PN and replay detection" c):
1060      *
1061      * the recipient shall maintain a single replay counter for received
1062      * individually addressed robust Management frames that are received
1063      * with the To DS subfield equal to 0, [...]
1064      */
1065     if (ieee80211_is_mgmt(hdr->frame_control) &&
1066         !ieee80211_has_tods(hdr->frame_control))
1067         security_idx = IEEE80211_NUM_TIDS;
1068 
1069 skip_hdr_check:
1070     BUILD_BUG_ON(sizeof(status->iv) != sizeof(wcid->rx_key_pn[0]));
1071     ret = memcmp(status->iv, wcid->rx_key_pn[security_idx],
1072              sizeof(status->iv));
1073     if (ret <= 0) {
1074         status->flag |= RX_FLAG_ONLY_MONITOR;
1075         return;
1076     }
1077 
1078     memcpy(wcid->rx_key_pn[security_idx], status->iv, sizeof(status->iv));
1079 
1080     if (status->flag & RX_FLAG_IV_STRIPPED)
1081         status->flag |= RX_FLAG_PN_VALIDATED;
1082 }
1083 
1084 static void
1085 mt76_airtime_report(struct mt76_dev *dev, struct mt76_rx_status *status,
1086             int len)
1087 {
1088     struct mt76_wcid *wcid = status->wcid;
1089     struct ieee80211_rx_status info = {
1090         .enc_flags = status->enc_flags,
1091         .rate_idx = status->rate_idx,
1092         .encoding = status->encoding,
1093         .band = status->band,
1094         .nss = status->nss,
1095         .bw = status->bw,
1096     };
1097     struct ieee80211_sta *sta;
1098     u32 airtime;
1099     u8 tidno = status->qos_ctl & IEEE80211_QOS_CTL_TID_MASK;
1100 
1101     airtime = ieee80211_calc_rx_airtime(dev->hw, &info, len);
1102     spin_lock(&dev->cc_lock);
1103     dev->cur_cc_bss_rx += airtime;
1104     spin_unlock(&dev->cc_lock);
1105 
1106     if (!wcid || !wcid->sta)
1107         return;
1108 
1109     sta = container_of((void *)wcid, struct ieee80211_sta, drv_priv);
1110     ieee80211_sta_register_airtime(sta, tidno, 0, airtime);
1111 }
1112 
1113 static void
1114 mt76_airtime_flush_ampdu(struct mt76_dev *dev)
1115 {
1116     struct mt76_wcid *wcid;
1117     int wcid_idx;
1118 
1119     if (!dev->rx_ampdu_len)
1120         return;
1121 
1122     wcid_idx = dev->rx_ampdu_status.wcid_idx;
1123     if (wcid_idx < ARRAY_SIZE(dev->wcid))
1124         wcid = rcu_dereference(dev->wcid[wcid_idx]);
1125     else
1126         wcid = NULL;
1127     dev->rx_ampdu_status.wcid = wcid;
1128 
1129     mt76_airtime_report(dev, &dev->rx_ampdu_status, dev->rx_ampdu_len);
1130 
1131     dev->rx_ampdu_len = 0;
1132     dev->rx_ampdu_ref = 0;
1133 }
1134 
1135 static void
1136 mt76_airtime_check(struct mt76_dev *dev, struct sk_buff *skb)
1137 {
1138     struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
1139     struct mt76_wcid *wcid = status->wcid;
1140 
1141     if (!(dev->drv->drv_flags & MT_DRV_SW_RX_AIRTIME))
1142         return;
1143 
1144     if (!wcid || !wcid->sta) {
1145         struct ieee80211_hdr *hdr = mt76_skb_get_hdr(skb);
1146 
1147         if (status->flag & RX_FLAG_8023)
1148             return;
1149 
1150         if (!ether_addr_equal(hdr->addr1, dev->phy.macaddr))
1151             return;
1152 
1153         wcid = NULL;
1154     }
1155 
1156     if (!(status->flag & RX_FLAG_AMPDU_DETAILS) ||
1157         status->ampdu_ref != dev->rx_ampdu_ref)
1158         mt76_airtime_flush_ampdu(dev);
1159 
1160     if (status->flag & RX_FLAG_AMPDU_DETAILS) {
1161         if (!dev->rx_ampdu_len ||
1162             status->ampdu_ref != dev->rx_ampdu_ref) {
1163             dev->rx_ampdu_status = *status;
1164             dev->rx_ampdu_status.wcid_idx = wcid ? wcid->idx : 0xff;
1165             dev->rx_ampdu_ref = status->ampdu_ref;
1166         }
1167 
1168         dev->rx_ampdu_len += skb->len;
1169         return;
1170     }
1171 
1172     mt76_airtime_report(dev, status, skb->len);
1173 }
1174 
1175 static void
1176 mt76_check_sta(struct mt76_dev *dev, struct sk_buff *skb)
1177 {
1178     struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
1179     struct ieee80211_hdr *hdr = mt76_skb_get_hdr(skb);
1180     struct ieee80211_sta *sta;
1181     struct ieee80211_hw *hw;
1182     struct mt76_wcid *wcid = status->wcid;
1183     u8 tidno = status->qos_ctl & IEEE80211_QOS_CTL_TID_MASK;
1184     bool ps;
1185 
1186     hw = mt76_phy_hw(dev, status->phy_idx);
1187     if (ieee80211_is_pspoll(hdr->frame_control) && !wcid &&
1188         !(status->flag & RX_FLAG_8023)) {
1189         sta = ieee80211_find_sta_by_ifaddr(hw, hdr->addr2, NULL);
1190         if (sta)
1191             wcid = status->wcid = (struct mt76_wcid *)sta->drv_priv;
1192     }
1193 
1194     mt76_airtime_check(dev, skb);
1195 
1196     if (!wcid || !wcid->sta)
1197         return;
1198 
1199     sta = container_of((void *)wcid, struct ieee80211_sta, drv_priv);
1200 
1201     if (status->signal <= 0)
1202         ewma_signal_add(&wcid->rssi, -status->signal);
1203 
1204     wcid->inactive_count = 0;
1205 
1206     if (status->flag & RX_FLAG_8023)
1207         return;
1208 
1209     if (!test_bit(MT_WCID_FLAG_CHECK_PS, &wcid->flags))
1210         return;
1211 
1212     if (ieee80211_is_pspoll(hdr->frame_control)) {
1213         ieee80211_sta_pspoll(sta);
1214         return;
1215     }
1216 
1217     if (ieee80211_has_morefrags(hdr->frame_control) ||
1218         !(ieee80211_is_mgmt(hdr->frame_control) ||
1219           ieee80211_is_data(hdr->frame_control)))
1220         return;
1221 
1222     ps = ieee80211_has_pm(hdr->frame_control);
1223 
1224     if (ps && (ieee80211_is_data_qos(hdr->frame_control) ||
1225            ieee80211_is_qos_nullfunc(hdr->frame_control)))
1226         ieee80211_sta_uapsd_trigger(sta, tidno);
1227 
1228     if (!!test_bit(MT_WCID_FLAG_PS, &wcid->flags) == ps)
1229         return;
1230 
1231     if (ps)
1232         set_bit(MT_WCID_FLAG_PS, &wcid->flags);
1233 
1234     dev->drv->sta_ps(dev, sta, ps);
1235 
1236     if (!ps)
1237         clear_bit(MT_WCID_FLAG_PS, &wcid->flags);
1238 
1239     ieee80211_sta_ps_transition(sta, ps);
1240 }
1241 
1242 void mt76_rx_complete(struct mt76_dev *dev, struct sk_buff_head *frames,
1243               struct napi_struct *napi)
1244 {
1245     struct ieee80211_sta *sta;
1246     struct ieee80211_hw *hw;
1247     struct sk_buff *skb, *tmp;
1248     LIST_HEAD(list);
1249 
1250     spin_lock(&dev->rx_lock);
1251     while ((skb = __skb_dequeue(frames)) != NULL) {
1252         struct sk_buff *nskb = skb_shinfo(skb)->frag_list;
1253 
1254         mt76_check_ccmp_pn(skb);
1255         skb_shinfo(skb)->frag_list = NULL;
1256         mt76_rx_convert(dev, skb, &hw, &sta);
1257         ieee80211_rx_list(hw, sta, skb, &list);
1258 
1259         /* subsequent amsdu frames */
1260         while (nskb) {
1261             skb = nskb;
1262             nskb = nskb->next;
1263             skb->next = NULL;
1264 
1265             mt76_rx_convert(dev, skb, &hw, &sta);
1266             ieee80211_rx_list(hw, sta, skb, &list);
1267         }
1268     }
1269     spin_unlock(&dev->rx_lock);
1270 
1271     if (!napi) {
1272         netif_receive_skb_list(&list);
1273         return;
1274     }
1275 
1276     list_for_each_entry_safe(skb, tmp, &list, list) {
1277         skb_list_del_init(skb);
1278         napi_gro_receive(napi, skb);
1279     }
1280 }
1281 
1282 void mt76_rx_poll_complete(struct mt76_dev *dev, enum mt76_rxq_id q,
1283                struct napi_struct *napi)
1284 {
1285     struct sk_buff_head frames;
1286     struct sk_buff *skb;
1287 
1288     __skb_queue_head_init(&frames);
1289 
1290     while ((skb = __skb_dequeue(&dev->rx_skb[q])) != NULL) {
1291         mt76_check_sta(dev, skb);
1292         mt76_rx_aggr_reorder(skb, &frames);
1293     }
1294 
1295     mt76_rx_complete(dev, &frames, napi);
1296 }
1297 EXPORT_SYMBOL_GPL(mt76_rx_poll_complete);
1298 
1299 static int
1300 mt76_sta_add(struct mt76_phy *phy, struct ieee80211_vif *vif,
1301          struct ieee80211_sta *sta)
1302 {
1303     struct mt76_wcid *wcid = (struct mt76_wcid *)sta->drv_priv;
1304     struct mt76_dev *dev = phy->dev;
1305     int ret;
1306     int i;
1307 
1308     mutex_lock(&dev->mutex);
1309 
1310     ret = dev->drv->sta_add(dev, vif, sta);
1311     if (ret)
1312         goto out;
1313 
1314     for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
1315         struct mt76_txq *mtxq;
1316 
1317         if (!sta->txq[i])
1318             continue;
1319 
1320         mtxq = (struct mt76_txq *)sta->txq[i]->drv_priv;
1321         mtxq->wcid = wcid->idx;
1322     }
1323 
1324     ewma_signal_init(&wcid->rssi);
1325     if (phy->band_idx == MT_BAND1)
1326         mt76_wcid_mask_set(dev->wcid_phy_mask, wcid->idx);
1327     wcid->phy_idx = phy->band_idx;
1328     rcu_assign_pointer(dev->wcid[wcid->idx], wcid);
1329 
1330     mt76_packet_id_init(wcid);
1331 out:
1332     mutex_unlock(&dev->mutex);
1333 
1334     return ret;
1335 }
1336 
1337 void __mt76_sta_remove(struct mt76_dev *dev, struct ieee80211_vif *vif,
1338                struct ieee80211_sta *sta)
1339 {
1340     struct mt76_wcid *wcid = (struct mt76_wcid *)sta->drv_priv;
1341     int i, idx = wcid->idx;
1342 
1343     for (i = 0; i < ARRAY_SIZE(wcid->aggr); i++)
1344         mt76_rx_aggr_stop(dev, wcid, i);
1345 
1346     if (dev->drv->sta_remove)
1347         dev->drv->sta_remove(dev, vif, sta);
1348 
1349     mt76_packet_id_flush(dev, wcid);
1350 
1351     mt76_wcid_mask_clear(dev->wcid_mask, idx);
1352     mt76_wcid_mask_clear(dev->wcid_phy_mask, idx);
1353 }
1354 EXPORT_SYMBOL_GPL(__mt76_sta_remove);
1355 
1356 static void
1357 mt76_sta_remove(struct mt76_dev *dev, struct ieee80211_vif *vif,
1358         struct ieee80211_sta *sta)
1359 {
1360     mutex_lock(&dev->mutex);
1361     __mt76_sta_remove(dev, vif, sta);
1362     mutex_unlock(&dev->mutex);
1363 }
1364 
1365 int mt76_sta_state(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1366            struct ieee80211_sta *sta,
1367            enum ieee80211_sta_state old_state,
1368            enum ieee80211_sta_state new_state)
1369 {
1370     struct mt76_phy *phy = hw->priv;
1371     struct mt76_dev *dev = phy->dev;
1372 
1373     if (old_state == IEEE80211_STA_NOTEXIST &&
1374         new_state == IEEE80211_STA_NONE)
1375         return mt76_sta_add(phy, vif, sta);
1376 
1377     if (old_state == IEEE80211_STA_AUTH &&
1378         new_state == IEEE80211_STA_ASSOC &&
1379         dev->drv->sta_assoc)
1380         dev->drv->sta_assoc(dev, vif, sta);
1381 
1382     if (old_state == IEEE80211_STA_NONE &&
1383         new_state == IEEE80211_STA_NOTEXIST)
1384         mt76_sta_remove(dev, vif, sta);
1385 
1386     return 0;
1387 }
1388 EXPORT_SYMBOL_GPL(mt76_sta_state);
1389 
1390 void mt76_sta_pre_rcu_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1391                  struct ieee80211_sta *sta)
1392 {
1393     struct mt76_phy *phy = hw->priv;
1394     struct mt76_dev *dev = phy->dev;
1395     struct mt76_wcid *wcid = (struct mt76_wcid *)sta->drv_priv;
1396 
1397     mutex_lock(&dev->mutex);
1398     spin_lock_bh(&dev->status_lock);
1399     rcu_assign_pointer(dev->wcid[wcid->idx], NULL);
1400     spin_unlock_bh(&dev->status_lock);
1401     mutex_unlock(&dev->mutex);
1402 }
1403 EXPORT_SYMBOL_GPL(mt76_sta_pre_rcu_remove);
1404 
1405 int mt76_get_txpower(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1406              int *dbm)
1407 {
1408     struct mt76_phy *phy = hw->priv;
1409     int n_chains = hweight8(phy->antenna_mask);
1410     int delta = mt76_tx_power_nss_delta(n_chains);
1411 
1412     *dbm = DIV_ROUND_UP(phy->txpower_cur + delta, 2);
1413 
1414     return 0;
1415 }
1416 EXPORT_SYMBOL_GPL(mt76_get_txpower);
1417 
1418 int mt76_init_sar_power(struct ieee80211_hw *hw,
1419             const struct cfg80211_sar_specs *sar)
1420 {
1421     struct mt76_phy *phy = hw->priv;
1422     const struct cfg80211_sar_capa *capa = hw->wiphy->sar_capa;
1423     int i;
1424 
1425     if (sar->type != NL80211_SAR_TYPE_POWER || !sar->num_sub_specs)
1426         return -EINVAL;
1427 
1428     for (i = 0; i < sar->num_sub_specs; i++) {
1429         u32 index = sar->sub_specs[i].freq_range_index;
1430         /* SAR specifies power limitaton in 0.25dbm */
1431         s32 power = sar->sub_specs[i].power >> 1;
1432 
1433         if (power > 127 || power < -127)
1434             power = 127;
1435 
1436         phy->frp[index].range = &capa->freq_ranges[index];
1437         phy->frp[index].power = power;
1438     }
1439 
1440     return 0;
1441 }
1442 EXPORT_SYMBOL_GPL(mt76_init_sar_power);
1443 
1444 int mt76_get_sar_power(struct mt76_phy *phy,
1445                struct ieee80211_channel *chan,
1446                int power)
1447 {
1448     const struct cfg80211_sar_capa *capa = phy->hw->wiphy->sar_capa;
1449     int freq, i;
1450 
1451     if (!capa || !phy->frp)
1452         return power;
1453 
1454     if (power > 127 || power < -127)
1455         power = 127;
1456 
1457     freq = ieee80211_channel_to_frequency(chan->hw_value, chan->band);
1458     for (i = 0 ; i < capa->num_freq_ranges; i++) {
1459         if (phy->frp[i].range &&
1460             freq >= phy->frp[i].range->start_freq &&
1461             freq < phy->frp[i].range->end_freq) {
1462             power = min_t(int, phy->frp[i].power, power);
1463             break;
1464         }
1465     }
1466 
1467     return power;
1468 }
1469 EXPORT_SYMBOL_GPL(mt76_get_sar_power);
1470 
1471 static void
1472 __mt76_csa_finish(void *priv, u8 *mac, struct ieee80211_vif *vif)
1473 {
1474     if (vif->bss_conf.csa_active && ieee80211_beacon_cntdwn_is_complete(vif))
1475         ieee80211_csa_finish(vif);
1476 }
1477 
1478 void mt76_csa_finish(struct mt76_dev *dev)
1479 {
1480     if (!dev->csa_complete)
1481         return;
1482 
1483     ieee80211_iterate_active_interfaces_atomic(dev->hw,
1484         IEEE80211_IFACE_ITER_RESUME_ALL,
1485         __mt76_csa_finish, dev);
1486 
1487     dev->csa_complete = 0;
1488 }
1489 EXPORT_SYMBOL_GPL(mt76_csa_finish);
1490 
1491 static void
1492 __mt76_csa_check(void *priv, u8 *mac, struct ieee80211_vif *vif)
1493 {
1494     struct mt76_dev *dev = priv;
1495 
1496     if (!vif->bss_conf.csa_active)
1497         return;
1498 
1499     dev->csa_complete |= ieee80211_beacon_cntdwn_is_complete(vif);
1500 }
1501 
1502 void mt76_csa_check(struct mt76_dev *dev)
1503 {
1504     ieee80211_iterate_active_interfaces_atomic(dev->hw,
1505         IEEE80211_IFACE_ITER_RESUME_ALL,
1506         __mt76_csa_check, dev);
1507 }
1508 EXPORT_SYMBOL_GPL(mt76_csa_check);
1509 
1510 int
1511 mt76_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta, bool set)
1512 {
1513     return 0;
1514 }
1515 EXPORT_SYMBOL_GPL(mt76_set_tim);
1516 
1517 void mt76_insert_ccmp_hdr(struct sk_buff *skb, u8 key_id)
1518 {
1519     struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
1520     int hdr_len = ieee80211_get_hdrlen_from_skb(skb);
1521     u8 *hdr, *pn = status->iv;
1522 
1523     __skb_push(skb, 8);
1524     memmove(skb->data, skb->data + 8, hdr_len);
1525     hdr = skb->data + hdr_len;
1526 
1527     hdr[0] = pn[5];
1528     hdr[1] = pn[4];
1529     hdr[2] = 0;
1530     hdr[3] = 0x20 | (key_id << 6);
1531     hdr[4] = pn[3];
1532     hdr[5] = pn[2];
1533     hdr[6] = pn[1];
1534     hdr[7] = pn[0];
1535 
1536     status->flag &= ~RX_FLAG_IV_STRIPPED;
1537 }
1538 EXPORT_SYMBOL_GPL(mt76_insert_ccmp_hdr);
1539 
1540 int mt76_get_rate(struct mt76_dev *dev,
1541           struct ieee80211_supported_band *sband,
1542           int idx, bool cck)
1543 {
1544     int i, offset = 0, len = sband->n_bitrates;
1545 
1546     if (cck) {
1547         if (sband != &dev->phy.sband_2g.sband)
1548             return 0;
1549 
1550         idx &= ~BIT(2); /* short preamble */
1551     } else if (sband == &dev->phy.sband_2g.sband) {
1552         offset = 4;
1553     }
1554 
1555     for (i = offset; i < len; i++) {
1556         if ((sband->bitrates[i].hw_value & GENMASK(7, 0)) == idx)
1557             return i;
1558     }
1559 
1560     return 0;
1561 }
1562 EXPORT_SYMBOL_GPL(mt76_get_rate);
1563 
1564 void mt76_sw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1565           const u8 *mac)
1566 {
1567     struct mt76_phy *phy = hw->priv;
1568 
1569     set_bit(MT76_SCANNING, &phy->state);
1570 }
1571 EXPORT_SYMBOL_GPL(mt76_sw_scan);
1572 
1573 void mt76_sw_scan_complete(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1574 {
1575     struct mt76_phy *phy = hw->priv;
1576 
1577     clear_bit(MT76_SCANNING, &phy->state);
1578 }
1579 EXPORT_SYMBOL_GPL(mt76_sw_scan_complete);
1580 
1581 int mt76_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
1582 {
1583     struct mt76_phy *phy = hw->priv;
1584     struct mt76_dev *dev = phy->dev;
1585 
1586     mutex_lock(&dev->mutex);
1587     *tx_ant = phy->antenna_mask;
1588     *rx_ant = phy->antenna_mask;
1589     mutex_unlock(&dev->mutex);
1590 
1591     return 0;
1592 }
1593 EXPORT_SYMBOL_GPL(mt76_get_antenna);
1594 
1595 struct mt76_queue *
1596 mt76_init_queue(struct mt76_dev *dev, int qid, int idx, int n_desc,
1597         int ring_base, u32 flags)
1598 {
1599     struct mt76_queue *hwq;
1600     int err;
1601 
1602     hwq = devm_kzalloc(dev->dev, sizeof(*hwq), GFP_KERNEL);
1603     if (!hwq)
1604         return ERR_PTR(-ENOMEM);
1605 
1606     hwq->flags = flags;
1607 
1608     err = dev->queue_ops->alloc(dev, hwq, idx, n_desc, 0, ring_base);
1609     if (err < 0)
1610         return ERR_PTR(err);
1611 
1612     return hwq;
1613 }
1614 EXPORT_SYMBOL_GPL(mt76_init_queue);
1615 
1616 u16 mt76_calculate_default_rate(struct mt76_phy *phy, int rateidx)
1617 {
1618     int offset = 0;
1619 
1620     if (phy->chandef.chan->band != NL80211_BAND_2GHZ)
1621         offset = 4;
1622 
1623     /* pick the lowest rate for hidden nodes */
1624     if (rateidx < 0)
1625         rateidx = 0;
1626 
1627     rateidx += offset;
1628     if (rateidx >= ARRAY_SIZE(mt76_rates))
1629         rateidx = offset;
1630 
1631     return mt76_rates[rateidx].hw_value;
1632 }
1633 EXPORT_SYMBOL_GPL(mt76_calculate_default_rate);
1634 
1635 void mt76_ethtool_worker(struct mt76_ethtool_worker_info *wi,
1636              struct mt76_sta_stats *stats)
1637 {
1638     int i, ei = wi->initial_stat_idx;
1639     u64 *data = wi->data;
1640 
1641     wi->sta_count++;
1642 
1643     data[ei++] += stats->tx_mode[MT_PHY_TYPE_CCK];
1644     data[ei++] += stats->tx_mode[MT_PHY_TYPE_OFDM];
1645     data[ei++] += stats->tx_mode[MT_PHY_TYPE_HT];
1646     data[ei++] += stats->tx_mode[MT_PHY_TYPE_HT_GF];
1647     data[ei++] += stats->tx_mode[MT_PHY_TYPE_VHT];
1648     data[ei++] += stats->tx_mode[MT_PHY_TYPE_HE_SU];
1649     data[ei++] += stats->tx_mode[MT_PHY_TYPE_HE_EXT_SU];
1650     data[ei++] += stats->tx_mode[MT_PHY_TYPE_HE_TB];
1651     data[ei++] += stats->tx_mode[MT_PHY_TYPE_HE_MU];
1652 
1653     for (i = 0; i < ARRAY_SIZE(stats->tx_bw); i++)
1654         data[ei++] += stats->tx_bw[i];
1655 
1656     for (i = 0; i < 12; i++)
1657         data[ei++] += stats->tx_mcs[i];
1658 
1659     wi->worker_stat_count = ei - wi->initial_stat_idx;
1660 }
1661 EXPORT_SYMBOL_GPL(mt76_ethtool_worker);
1662 
1663 enum mt76_dfs_state mt76_phy_dfs_state(struct mt76_phy *phy)
1664 {
1665     struct ieee80211_hw *hw = phy->hw;
1666     struct mt76_dev *dev = phy->dev;
1667 
1668     if (dev->region == NL80211_DFS_UNSET ||
1669         test_bit(MT76_SCANNING, &phy->state))
1670         return MT_DFS_STATE_DISABLED;
1671 
1672     if (!hw->conf.radar_enabled) {
1673         if ((hw->conf.flags & IEEE80211_CONF_MONITOR) &&
1674             (phy->chandef.chan->flags & IEEE80211_CHAN_RADAR))
1675             return MT_DFS_STATE_ACTIVE;
1676 
1677         return MT_DFS_STATE_DISABLED;
1678     }
1679 
1680     if (!cfg80211_reg_can_beacon(hw->wiphy, &phy->chandef, NL80211_IFTYPE_AP))
1681         return MT_DFS_STATE_CAC;
1682 
1683     return MT_DFS_STATE_ACTIVE;
1684 }
1685 EXPORT_SYMBOL_GPL(mt76_phy_dfs_state);