Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: ISC
0002 /* Copyright (C) 2020 MediaTek Inc. */
0003 
0004 #include <linux/etherdevice.h>
0005 #include <linux/platform_device.h>
0006 #include <linux/pci.h>
0007 #include <linux/module.h>
0008 #include "mt7915.h"
0009 #include "mcu.h"
0010 
0011 static bool mt7915_dev_running(struct mt7915_dev *dev)
0012 {
0013     struct mt7915_phy *phy;
0014 
0015     if (test_bit(MT76_STATE_RUNNING, &dev->mphy.state))
0016         return true;
0017 
0018     phy = mt7915_ext_phy(dev);
0019 
0020     return phy && test_bit(MT76_STATE_RUNNING, &phy->mt76->state);
0021 }
0022 
0023 static int mt7915_start(struct ieee80211_hw *hw)
0024 {
0025     struct mt7915_dev *dev = mt7915_hw_dev(hw);
0026     struct mt7915_phy *phy = mt7915_hw_phy(hw);
0027     bool running;
0028     int ret;
0029 
0030     flush_work(&dev->init_work);
0031 
0032     mutex_lock(&dev->mt76.mutex);
0033 
0034     running = mt7915_dev_running(dev);
0035 
0036     if (!running) {
0037         ret = mt76_connac_mcu_set_pm(&dev->mt76, 0, 0);
0038         if (ret)
0039             goto out;
0040 
0041         ret = mt7915_mcu_set_mac(dev, 0, true, true);
0042         if (ret)
0043             goto out;
0044 
0045         mt7915_mac_enable_nf(dev, 0);
0046     }
0047 
0048     if (phy != &dev->phy || phy->band_idx) {
0049         ret = mt76_connac_mcu_set_pm(&dev->mt76, 1, 0);
0050         if (ret)
0051             goto out;
0052 
0053         ret = mt7915_mcu_set_mac(dev, 1, true, true);
0054         if (ret)
0055             goto out;
0056 
0057         mt7915_mac_enable_nf(dev, 1);
0058     }
0059 
0060     ret = mt76_connac_mcu_set_rts_thresh(&dev->mt76, 0x92b,
0061                          phy != &dev->phy);
0062     if (ret)
0063         goto out;
0064 
0065     ret = mt7915_mcu_set_sku_en(phy, true);
0066     if (ret)
0067         goto out;
0068 
0069     ret = mt7915_mcu_set_chan_info(phy, MCU_EXT_CMD(SET_RX_PATH));
0070     if (ret)
0071         goto out;
0072 
0073     set_bit(MT76_STATE_RUNNING, &phy->mt76->state);
0074 
0075     if (!mt76_testmode_enabled(phy->mt76))
0076         ieee80211_queue_delayed_work(hw, &phy->mt76->mac_work,
0077                          MT7915_WATCHDOG_TIME);
0078 
0079     if (!running)
0080         mt7915_mac_reset_counters(phy);
0081 
0082 out:
0083     mutex_unlock(&dev->mt76.mutex);
0084 
0085     return ret;
0086 }
0087 
0088 static void mt7915_stop(struct ieee80211_hw *hw)
0089 {
0090     struct mt7915_dev *dev = mt7915_hw_dev(hw);
0091     struct mt7915_phy *phy = mt7915_hw_phy(hw);
0092 
0093     cancel_delayed_work_sync(&phy->mt76->mac_work);
0094 
0095     mutex_lock(&dev->mt76.mutex);
0096 
0097     mt76_testmode_reset(phy->mt76, true);
0098 
0099     clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
0100 
0101     if (phy != &dev->phy) {
0102         mt76_connac_mcu_set_pm(&dev->mt76, 1, 1);
0103         mt7915_mcu_set_mac(dev, 1, false, false);
0104     }
0105 
0106     if (!mt7915_dev_running(dev)) {
0107         mt76_connac_mcu_set_pm(&dev->mt76, 0, 1);
0108         mt7915_mcu_set_mac(dev, 0, false, false);
0109     }
0110 
0111     mutex_unlock(&dev->mt76.mutex);
0112 }
0113 
0114 static inline int get_free_idx(u32 mask, u8 start, u8 end)
0115 {
0116     return ffs(~mask & GENMASK(end, start));
0117 }
0118 
0119 static int get_omac_idx(enum nl80211_iftype type, u64 mask)
0120 {
0121     int i;
0122 
0123     switch (type) {
0124     case NL80211_IFTYPE_MESH_POINT:
0125     case NL80211_IFTYPE_ADHOC:
0126     case NL80211_IFTYPE_STATION:
0127         /* prefer hw bssid slot 1-3 */
0128         i = get_free_idx(mask, HW_BSSID_1, HW_BSSID_3);
0129         if (i)
0130             return i - 1;
0131 
0132         if (type != NL80211_IFTYPE_STATION)
0133             break;
0134 
0135         i = get_free_idx(mask, EXT_BSSID_1, EXT_BSSID_MAX);
0136         if (i)
0137             return i - 1;
0138 
0139         if (~mask & BIT(HW_BSSID_0))
0140             return HW_BSSID_0;
0141 
0142         break;
0143     case NL80211_IFTYPE_MONITOR:
0144     case NL80211_IFTYPE_AP:
0145         /* ap uses hw bssid 0 and ext bssid */
0146         if (~mask & BIT(HW_BSSID_0))
0147             return HW_BSSID_0;
0148 
0149         i = get_free_idx(mask, EXT_BSSID_1, EXT_BSSID_MAX);
0150         if (i)
0151             return i - 1;
0152 
0153         break;
0154     default:
0155         WARN_ON(1);
0156         break;
0157     }
0158 
0159     return -1;
0160 }
0161 
0162 static void mt7915_init_bitrate_mask(struct ieee80211_vif *vif)
0163 {
0164     struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
0165     int i;
0166 
0167     for (i = 0; i < ARRAY_SIZE(mvif->bitrate_mask.control); i++) {
0168         mvif->bitrate_mask.control[i].gi = NL80211_TXRATE_DEFAULT_GI;
0169         mvif->bitrate_mask.control[i].he_gi = 0xff;
0170         mvif->bitrate_mask.control[i].he_ltf = 0xff;
0171         mvif->bitrate_mask.control[i].legacy = GENMASK(31, 0);
0172         memset(mvif->bitrate_mask.control[i].ht_mcs, 0xff,
0173                sizeof(mvif->bitrate_mask.control[i].ht_mcs));
0174         memset(mvif->bitrate_mask.control[i].vht_mcs, 0xff,
0175                sizeof(mvif->bitrate_mask.control[i].vht_mcs));
0176         memset(mvif->bitrate_mask.control[i].he_mcs, 0xff,
0177                sizeof(mvif->bitrate_mask.control[i].he_mcs));
0178     }
0179 }
0180 
0181 static int mt7915_add_interface(struct ieee80211_hw *hw,
0182                 struct ieee80211_vif *vif)
0183 {
0184     struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
0185     struct mt7915_dev *dev = mt7915_hw_dev(hw);
0186     struct mt7915_phy *phy = mt7915_hw_phy(hw);
0187     struct mt76_txq *mtxq;
0188     bool ext_phy = phy != &dev->phy;
0189     int idx, ret = 0;
0190 
0191     mutex_lock(&dev->mt76.mutex);
0192 
0193     mt76_testmode_reset(phy->mt76, true);
0194 
0195     if (vif->type == NL80211_IFTYPE_MONITOR &&
0196         is_zero_ether_addr(vif->addr))
0197         phy->monitor_vif = vif;
0198 
0199     mvif->mt76.idx = __ffs64(~dev->mt76.vif_mask);
0200     if (mvif->mt76.idx >= (MT7915_MAX_INTERFACES << dev->dbdc_support)) {
0201         ret = -ENOSPC;
0202         goto out;
0203     }
0204 
0205     idx = get_omac_idx(vif->type, phy->omac_mask);
0206     if (idx < 0) {
0207         ret = -ENOSPC;
0208         goto out;
0209     }
0210     mvif->mt76.omac_idx = idx;
0211     mvif->phy = phy;
0212     mvif->mt76.band_idx = phy->band_idx;
0213 
0214     mvif->mt76.wmm_idx = vif->type != NL80211_IFTYPE_AP;
0215     if (ext_phy)
0216         mvif->mt76.wmm_idx += 2;
0217 
0218     ret = mt7915_mcu_add_dev_info(phy, vif, true);
0219     if (ret)
0220         goto out;
0221 
0222     dev->mt76.vif_mask |= BIT_ULL(mvif->mt76.idx);
0223     phy->omac_mask |= BIT_ULL(mvif->mt76.omac_idx);
0224 
0225     idx = MT7915_WTBL_RESERVED - mvif->mt76.idx;
0226 
0227     INIT_LIST_HEAD(&mvif->sta.rc_list);
0228     INIT_LIST_HEAD(&mvif->sta.poll_list);
0229     mvif->sta.wcid.idx = idx;
0230     mvif->sta.wcid.phy_idx = ext_phy;
0231     mvif->sta.wcid.hw_key_idx = -1;
0232     mvif->sta.wcid.tx_info |= MT_WCID_TX_INFO_SET;
0233     mt76_packet_id_init(&mvif->sta.wcid);
0234 
0235     mt7915_mac_wtbl_update(dev, idx,
0236                    MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
0237 
0238     if (vif->txq) {
0239         mtxq = (struct mt76_txq *)vif->txq->drv_priv;
0240         mtxq->wcid = idx;
0241     }
0242 
0243     if (vif->type != NL80211_IFTYPE_AP &&
0244         (!mvif->mt76.omac_idx || mvif->mt76.omac_idx > 3))
0245         vif->offload_flags = 0;
0246     vif->offload_flags |= IEEE80211_OFFLOAD_ENCAP_4ADDR;
0247 
0248     mt7915_init_bitrate_mask(vif);
0249     memset(&mvif->cap, -1, sizeof(mvif->cap));
0250 
0251     mt7915_mcu_add_bss_info(phy, vif, true);
0252     mt7915_mcu_add_sta(dev, vif, NULL, true);
0253     rcu_assign_pointer(dev->mt76.wcid[idx], &mvif->sta.wcid);
0254 
0255 out:
0256     mutex_unlock(&dev->mt76.mutex);
0257 
0258     return ret;
0259 }
0260 
0261 static void mt7915_remove_interface(struct ieee80211_hw *hw,
0262                     struct ieee80211_vif *vif)
0263 {
0264     struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
0265     struct mt7915_sta *msta = &mvif->sta;
0266     struct mt7915_dev *dev = mt7915_hw_dev(hw);
0267     struct mt7915_phy *phy = mt7915_hw_phy(hw);
0268     int idx = msta->wcid.idx;
0269 
0270     mt7915_mcu_add_bss_info(phy, vif, false);
0271     mt7915_mcu_add_sta(dev, vif, NULL, false);
0272 
0273     mutex_lock(&dev->mt76.mutex);
0274     mt76_testmode_reset(phy->mt76, true);
0275     mutex_unlock(&dev->mt76.mutex);
0276 
0277     if (vif == phy->monitor_vif)
0278         phy->monitor_vif = NULL;
0279 
0280     mt7915_mcu_add_dev_info(phy, vif, false);
0281 
0282     rcu_assign_pointer(dev->mt76.wcid[idx], NULL);
0283 
0284     mutex_lock(&dev->mt76.mutex);
0285     dev->mt76.vif_mask &= ~BIT_ULL(mvif->mt76.idx);
0286     phy->omac_mask &= ~BIT_ULL(mvif->mt76.omac_idx);
0287     mutex_unlock(&dev->mt76.mutex);
0288 
0289     spin_lock_bh(&dev->sta_poll_lock);
0290     if (!list_empty(&msta->poll_list))
0291         list_del_init(&msta->poll_list);
0292     spin_unlock_bh(&dev->sta_poll_lock);
0293 
0294     mt76_packet_id_flush(&dev->mt76, &msta->wcid);
0295 }
0296 
0297 int mt7915_set_channel(struct mt7915_phy *phy)
0298 {
0299     struct mt7915_dev *dev = phy->dev;
0300     int ret;
0301 
0302     cancel_delayed_work_sync(&phy->mt76->mac_work);
0303 
0304     mutex_lock(&dev->mt76.mutex);
0305     set_bit(MT76_RESET, &phy->mt76->state);
0306 
0307     mt76_set_channel(phy->mt76);
0308 
0309     if (dev->flash_mode) {
0310         ret = mt7915_mcu_apply_tx_dpd(phy);
0311         if (ret)
0312             goto out;
0313     }
0314 
0315     ret = mt7915_mcu_set_chan_info(phy, MCU_EXT_CMD(CHANNEL_SWITCH));
0316     if (ret)
0317         goto out;
0318 
0319     mt7915_mac_set_timing(phy);
0320     ret = mt7915_dfs_init_radar_detector(phy);
0321     mt7915_mac_cca_stats_reset(phy);
0322 
0323     mt7915_mac_reset_counters(phy);
0324     phy->noise = 0;
0325 
0326 out:
0327     clear_bit(MT76_RESET, &phy->mt76->state);
0328     mutex_unlock(&dev->mt76.mutex);
0329 
0330     mt76_txq_schedule_all(phy->mt76);
0331 
0332     if (!mt76_testmode_enabled(phy->mt76))
0333         ieee80211_queue_delayed_work(phy->mt76->hw,
0334                          &phy->mt76->mac_work,
0335                          MT7915_WATCHDOG_TIME);
0336 
0337     return ret;
0338 }
0339 
0340 static int mt7915_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
0341               struct ieee80211_vif *vif, struct ieee80211_sta *sta,
0342               struct ieee80211_key_conf *key)
0343 {
0344     struct mt7915_dev *dev = mt7915_hw_dev(hw);
0345     struct mt7915_phy *phy = mt7915_hw_phy(hw);
0346     struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
0347     struct mt7915_sta *msta = sta ? (struct mt7915_sta *)sta->drv_priv :
0348                   &mvif->sta;
0349     struct mt76_wcid *wcid = &msta->wcid;
0350     u8 *wcid_keyidx = &wcid->hw_key_idx;
0351     int idx = key->keyidx;
0352     int err = 0;
0353 
0354     /* The hardware does not support per-STA RX GTK, fallback
0355      * to software mode for these.
0356      */
0357     if ((vif->type == NL80211_IFTYPE_ADHOC ||
0358          vif->type == NL80211_IFTYPE_MESH_POINT) &&
0359         (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
0360          key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
0361         !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
0362         return -EOPNOTSUPP;
0363 
0364     /* fall back to sw encryption for unsupported ciphers */
0365     switch (key->cipher) {
0366     case WLAN_CIPHER_SUITE_AES_CMAC:
0367         wcid_keyidx = &wcid->hw_key_idx2;
0368         key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIE;
0369         break;
0370     case WLAN_CIPHER_SUITE_TKIP:
0371     case WLAN_CIPHER_SUITE_CCMP:
0372     case WLAN_CIPHER_SUITE_CCMP_256:
0373     case WLAN_CIPHER_SUITE_GCMP:
0374     case WLAN_CIPHER_SUITE_GCMP_256:
0375     case WLAN_CIPHER_SUITE_SMS4:
0376         break;
0377     case WLAN_CIPHER_SUITE_WEP40:
0378     case WLAN_CIPHER_SUITE_WEP104:
0379     default:
0380         return -EOPNOTSUPP;
0381     }
0382 
0383     mutex_lock(&dev->mt76.mutex);
0384 
0385     if (cmd == SET_KEY && !sta && !mvif->mt76.cipher) {
0386         mvif->mt76.cipher = mt76_connac_mcu_get_cipher(key->cipher);
0387         mt7915_mcu_add_bss_info(phy, vif, true);
0388     }
0389 
0390     if (cmd == SET_KEY)
0391         *wcid_keyidx = idx;
0392     else if (idx == *wcid_keyidx)
0393         *wcid_keyidx = -1;
0394     else
0395         goto out;
0396 
0397     mt76_wcid_key_setup(&dev->mt76, wcid,
0398                 cmd == SET_KEY ? key : NULL);
0399 
0400     err = mt76_connac_mcu_add_key(&dev->mt76, vif, &msta->bip,
0401                       key, MCU_EXT_CMD(STA_REC_UPDATE),
0402                       &msta->wcid, cmd);
0403 out:
0404     mutex_unlock(&dev->mt76.mutex);
0405 
0406     return err;
0407 }
0408 
0409 static int mt7915_set_sar_specs(struct ieee80211_hw *hw,
0410                 const struct cfg80211_sar_specs *sar)
0411 {
0412     struct mt7915_phy *phy = mt7915_hw_phy(hw);
0413     struct mt7915_dev *dev = mt7915_hw_dev(hw);
0414     int err = -EINVAL;
0415 
0416     mutex_lock(&dev->mt76.mutex);
0417     if (!cfg80211_chandef_valid(&phy->mt76->chandef))
0418         goto out;
0419 
0420     err = mt76_init_sar_power(hw, sar);
0421     if (err)
0422         goto out;
0423 
0424     err = mt7915_mcu_set_txpower_sku(phy);
0425 out:
0426     mutex_unlock(&dev->mt76.mutex);
0427 
0428     return err;
0429 }
0430 
0431 static int mt7915_config(struct ieee80211_hw *hw, u32 changed)
0432 {
0433     struct mt7915_dev *dev = mt7915_hw_dev(hw);
0434     struct mt7915_phy *phy = mt7915_hw_phy(hw);
0435     bool band = phy != &dev->phy;
0436     int ret;
0437 
0438     if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
0439 #ifdef CONFIG_NL80211_TESTMODE
0440         if (phy->mt76->test.state != MT76_TM_STATE_OFF) {
0441             mutex_lock(&dev->mt76.mutex);
0442             mt76_testmode_reset(phy->mt76, false);
0443             mutex_unlock(&dev->mt76.mutex);
0444         }
0445 #endif
0446         ieee80211_stop_queues(hw);
0447         ret = mt7915_set_channel(phy);
0448         if (ret)
0449             return ret;
0450         ieee80211_wake_queues(hw);
0451     }
0452 
0453     if (changed & IEEE80211_CONF_CHANGE_POWER) {
0454         ret = mt7915_mcu_set_txpower_sku(phy);
0455         if (ret)
0456             return ret;
0457     }
0458 
0459     mutex_lock(&dev->mt76.mutex);
0460 
0461     if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
0462         bool enabled = !!(hw->conf.flags & IEEE80211_CONF_MONITOR);
0463 
0464         if (!enabled)
0465             phy->rxfilter |= MT_WF_RFCR_DROP_OTHER_UC;
0466         else
0467             phy->rxfilter &= ~MT_WF_RFCR_DROP_OTHER_UC;
0468 
0469         mt76_rmw_field(dev, MT_DMA_DCR0(band), MT_DMA_DCR0_RXD_G5_EN,
0470                    enabled);
0471         mt76_testmode_reset(phy->mt76, true);
0472         mt76_wr(dev, MT_WF_RFCR(band), phy->rxfilter);
0473     }
0474 
0475     mutex_unlock(&dev->mt76.mutex);
0476 
0477     return 0;
0478 }
0479 
0480 static int
0481 mt7915_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
0482            unsigned int link_id, u16 queue,
0483            const struct ieee80211_tx_queue_params *params)
0484 {
0485     struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
0486 
0487     /* no need to update right away, we'll get BSS_CHANGED_QOS */
0488     queue = mt76_connac_lmac_mapping(queue);
0489     mvif->queue_params[queue] = *params;
0490 
0491     return 0;
0492 }
0493 
0494 static void mt7915_configure_filter(struct ieee80211_hw *hw,
0495                     unsigned int changed_flags,
0496                     unsigned int *total_flags,
0497                     u64 multicast)
0498 {
0499     struct mt7915_dev *dev = mt7915_hw_dev(hw);
0500     struct mt7915_phy *phy = mt7915_hw_phy(hw);
0501     bool band = phy != &dev->phy;
0502     u32 ctl_flags = MT_WF_RFCR1_DROP_ACK |
0503             MT_WF_RFCR1_DROP_BF_POLL |
0504             MT_WF_RFCR1_DROP_BA |
0505             MT_WF_RFCR1_DROP_CFEND |
0506             MT_WF_RFCR1_DROP_CFACK;
0507     u32 flags = 0;
0508 
0509 #define MT76_FILTER(_flag, _hw) do {                    \
0510         flags |= *total_flags & FIF_##_flag;            \
0511         phy->rxfilter &= ~(_hw);                \
0512         phy->rxfilter |= !(flags & FIF_##_flag) * (_hw);    \
0513     } while (0)
0514 
0515     mutex_lock(&dev->mt76.mutex);
0516 
0517     phy->rxfilter &= ~(MT_WF_RFCR_DROP_OTHER_BSS |
0518                MT_WF_RFCR_DROP_OTHER_BEACON |
0519                MT_WF_RFCR_DROP_FRAME_REPORT |
0520                MT_WF_RFCR_DROP_PROBEREQ |
0521                MT_WF_RFCR_DROP_MCAST_FILTERED |
0522                MT_WF_RFCR_DROP_MCAST |
0523                MT_WF_RFCR_DROP_BCAST |
0524                MT_WF_RFCR_DROP_DUPLICATE |
0525                MT_WF_RFCR_DROP_A2_BSSID |
0526                MT_WF_RFCR_DROP_UNWANTED_CTL |
0527                MT_WF_RFCR_DROP_STBC_MULTI);
0528 
0529     MT76_FILTER(OTHER_BSS, MT_WF_RFCR_DROP_OTHER_TIM |
0530                    MT_WF_RFCR_DROP_A3_MAC |
0531                    MT_WF_RFCR_DROP_A3_BSSID);
0532 
0533     MT76_FILTER(FCSFAIL, MT_WF_RFCR_DROP_FCSFAIL);
0534 
0535     MT76_FILTER(CONTROL, MT_WF_RFCR_DROP_CTS |
0536                  MT_WF_RFCR_DROP_RTS |
0537                  MT_WF_RFCR_DROP_CTL_RSV |
0538                  MT_WF_RFCR_DROP_NDPA);
0539 
0540     *total_flags = flags;
0541     mt76_wr(dev, MT_WF_RFCR(band), phy->rxfilter);
0542 
0543     if (*total_flags & FIF_CONTROL)
0544         mt76_clear(dev, MT_WF_RFCR1(band), ctl_flags);
0545     else
0546         mt76_set(dev, MT_WF_RFCR1(band), ctl_flags);
0547 
0548     mutex_unlock(&dev->mt76.mutex);
0549 }
0550 
0551 static void
0552 mt7915_update_bss_color(struct ieee80211_hw *hw,
0553             struct ieee80211_vif *vif,
0554             struct cfg80211_he_bss_color *bss_color)
0555 {
0556     struct mt7915_dev *dev = mt7915_hw_dev(hw);
0557 
0558     switch (vif->type) {
0559     case NL80211_IFTYPE_AP: {
0560         struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
0561 
0562         if (mvif->mt76.omac_idx > HW_BSSID_MAX)
0563             return;
0564         fallthrough;
0565     }
0566     case NL80211_IFTYPE_STATION:
0567         mt7915_mcu_update_bss_color(dev, vif, bss_color);
0568         break;
0569     default:
0570         break;
0571     }
0572 }
0573 
0574 static void mt7915_bss_info_changed(struct ieee80211_hw *hw,
0575                     struct ieee80211_vif *vif,
0576                     struct ieee80211_bss_conf *info,
0577                     u64 changed)
0578 {
0579     struct mt7915_phy *phy = mt7915_hw_phy(hw);
0580     struct mt7915_dev *dev = mt7915_hw_dev(hw);
0581 
0582     mutex_lock(&dev->mt76.mutex);
0583 
0584     /*
0585      * station mode uses BSSID to map the wlan entry to a peer,
0586      * and then peer references bss_info_rfch to set bandwidth cap.
0587      */
0588     if (changed & BSS_CHANGED_BSSID &&
0589         vif->type == NL80211_IFTYPE_STATION) {
0590         bool join = !is_zero_ether_addr(info->bssid);
0591 
0592         mt7915_mcu_add_bss_info(phy, vif, join);
0593         mt7915_mcu_add_sta(dev, vif, NULL, join);
0594     }
0595 
0596     if (changed & BSS_CHANGED_ASSOC) {
0597         mt7915_mcu_add_bss_info(phy, vif, vif->cfg.assoc);
0598         mt7915_mcu_add_obss_spr(dev, vif, info->he_obss_pd.enable);
0599     }
0600 
0601     if (changed & BSS_CHANGED_ERP_SLOT) {
0602         int slottime = info->use_short_slot ? 9 : 20;
0603 
0604         if (slottime != phy->slottime) {
0605             phy->slottime = slottime;
0606             mt7915_mac_set_timing(phy);
0607         }
0608     }
0609 
0610     if (changed & BSS_CHANGED_BEACON_ENABLED && info->enable_beacon) {
0611         mt7915_mcu_add_bss_info(phy, vif, true);
0612         mt7915_mcu_add_sta(dev, vif, NULL, true);
0613     }
0614 
0615     /* ensure that enable txcmd_mode after bss_info */
0616     if (changed & (BSS_CHANGED_QOS | BSS_CHANGED_BEACON_ENABLED))
0617         mt7915_mcu_set_tx(dev, vif);
0618 
0619     if (changed & BSS_CHANGED_HE_OBSS_PD)
0620         mt7915_mcu_add_obss_spr(dev, vif, info->he_obss_pd.enable);
0621 
0622     if (changed & BSS_CHANGED_HE_BSS_COLOR)
0623         mt7915_update_bss_color(hw, vif, &info->he_bss_color);
0624 
0625     if (changed & (BSS_CHANGED_BEACON |
0626                BSS_CHANGED_BEACON_ENABLED |
0627                BSS_CHANGED_UNSOL_BCAST_PROBE_RESP |
0628                BSS_CHANGED_FILS_DISCOVERY))
0629         mt7915_mcu_add_beacon(hw, vif, info->enable_beacon, changed);
0630 
0631     mutex_unlock(&dev->mt76.mutex);
0632 }
0633 
0634 static void
0635 mt7915_channel_switch_beacon(struct ieee80211_hw *hw,
0636                  struct ieee80211_vif *vif,
0637                  struct cfg80211_chan_def *chandef)
0638 {
0639     struct mt7915_dev *dev = mt7915_hw_dev(hw);
0640 
0641     mutex_lock(&dev->mt76.mutex);
0642     mt7915_mcu_add_beacon(hw, vif, true, BSS_CHANGED_BEACON);
0643     mutex_unlock(&dev->mt76.mutex);
0644 }
0645 
0646 int mt7915_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
0647                struct ieee80211_sta *sta)
0648 {
0649     struct mt7915_dev *dev = container_of(mdev, struct mt7915_dev, mt76);
0650     struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
0651     struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
0652     bool ext_phy = mvif->phy != &dev->phy;
0653     int ret, idx;
0654 
0655     idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7915_WTBL_STA);
0656     if (idx < 0)
0657         return -ENOSPC;
0658 
0659     INIT_LIST_HEAD(&msta->rc_list);
0660     INIT_LIST_HEAD(&msta->poll_list);
0661     msta->vif = mvif;
0662     msta->wcid.sta = 1;
0663     msta->wcid.idx = idx;
0664     msta->wcid.phy_idx = ext_phy;
0665     msta->wcid.tx_info |= MT_WCID_TX_INFO_SET;
0666     msta->jiffies = jiffies;
0667 
0668     mt7915_mac_wtbl_update(dev, idx,
0669                    MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
0670 
0671     ret = mt7915_mcu_add_sta(dev, vif, sta, true);
0672     if (ret)
0673         return ret;
0674 
0675     return mt7915_mcu_add_rate_ctrl(dev, vif, sta, false);
0676 }
0677 
0678 void mt7915_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
0679                struct ieee80211_sta *sta)
0680 {
0681     struct mt7915_dev *dev = container_of(mdev, struct mt7915_dev, mt76);
0682     struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
0683     int i;
0684 
0685     mt7915_mcu_add_sta(dev, vif, sta, false);
0686 
0687     mt7915_mac_wtbl_update(dev, msta->wcid.idx,
0688                    MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
0689 
0690     for (i = 0; i < ARRAY_SIZE(msta->twt.flow); i++)
0691         mt7915_mac_twt_teardown_flow(dev, msta, i);
0692 
0693     spin_lock_bh(&dev->sta_poll_lock);
0694     if (!list_empty(&msta->poll_list))
0695         list_del_init(&msta->poll_list);
0696     if (!list_empty(&msta->rc_list))
0697         list_del_init(&msta->rc_list);
0698     spin_unlock_bh(&dev->sta_poll_lock);
0699 }
0700 
0701 static void mt7915_tx(struct ieee80211_hw *hw,
0702               struct ieee80211_tx_control *control,
0703               struct sk_buff *skb)
0704 {
0705     struct mt7915_dev *dev = mt7915_hw_dev(hw);
0706     struct mt76_phy *mphy = hw->priv;
0707     struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
0708     struct ieee80211_vif *vif = info->control.vif;
0709     struct mt76_wcid *wcid = &dev->mt76.global_wcid;
0710 
0711     if (control->sta) {
0712         struct mt7915_sta *sta;
0713 
0714         sta = (struct mt7915_sta *)control->sta->drv_priv;
0715         wcid = &sta->wcid;
0716     }
0717 
0718     if (vif && !control->sta) {
0719         struct mt7915_vif *mvif;
0720 
0721         mvif = (struct mt7915_vif *)vif->drv_priv;
0722         wcid = &mvif->sta.wcid;
0723     }
0724 
0725     mt76_tx(mphy, control->sta, wcid, skb);
0726 }
0727 
0728 static int mt7915_set_rts_threshold(struct ieee80211_hw *hw, u32 val)
0729 {
0730     struct mt7915_dev *dev = mt7915_hw_dev(hw);
0731     struct mt7915_phy *phy = mt7915_hw_phy(hw);
0732     int ret;
0733 
0734     mutex_lock(&dev->mt76.mutex);
0735     ret = mt76_connac_mcu_set_rts_thresh(&dev->mt76, val, phy != &dev->phy);
0736     mutex_unlock(&dev->mt76.mutex);
0737 
0738     return ret;
0739 }
0740 
0741 static int
0742 mt7915_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
0743             struct ieee80211_ampdu_params *params)
0744 {
0745     enum ieee80211_ampdu_mlme_action action = params->action;
0746     struct mt7915_dev *dev = mt7915_hw_dev(hw);
0747     struct ieee80211_sta *sta = params->sta;
0748     struct ieee80211_txq *txq = sta->txq[params->tid];
0749     struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
0750     u16 tid = params->tid;
0751     u16 ssn = params->ssn;
0752     struct mt76_txq *mtxq;
0753     int ret = 0;
0754 
0755     if (!txq)
0756         return -EINVAL;
0757 
0758     mtxq = (struct mt76_txq *)txq->drv_priv;
0759 
0760     mutex_lock(&dev->mt76.mutex);
0761     switch (action) {
0762     case IEEE80211_AMPDU_RX_START:
0763         mt76_rx_aggr_start(&dev->mt76, &msta->wcid, tid, ssn,
0764                    params->buf_size);
0765         ret = mt7915_mcu_add_rx_ba(dev, params, true);
0766         break;
0767     case IEEE80211_AMPDU_RX_STOP:
0768         mt76_rx_aggr_stop(&dev->mt76, &msta->wcid, tid);
0769         ret = mt7915_mcu_add_rx_ba(dev, params, false);
0770         break;
0771     case IEEE80211_AMPDU_TX_OPERATIONAL:
0772         mtxq->aggr = true;
0773         mtxq->send_bar = false;
0774         ret = mt7915_mcu_add_tx_ba(dev, params, true);
0775         break;
0776     case IEEE80211_AMPDU_TX_STOP_FLUSH:
0777     case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
0778         mtxq->aggr = false;
0779         clear_bit(tid, &msta->ampdu_state);
0780         ret = mt7915_mcu_add_tx_ba(dev, params, false);
0781         break;
0782     case IEEE80211_AMPDU_TX_START:
0783         set_bit(tid, &msta->ampdu_state);
0784         ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
0785         break;
0786     case IEEE80211_AMPDU_TX_STOP_CONT:
0787         mtxq->aggr = false;
0788         clear_bit(tid, &msta->ampdu_state);
0789         ret = mt7915_mcu_add_tx_ba(dev, params, false);
0790         ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
0791         break;
0792     }
0793     mutex_unlock(&dev->mt76.mutex);
0794 
0795     return ret;
0796 }
0797 
0798 static int
0799 mt7915_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
0800            struct ieee80211_sta *sta)
0801 {
0802     return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NOTEXIST,
0803                   IEEE80211_STA_NONE);
0804 }
0805 
0806 static int
0807 mt7915_sta_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
0808           struct ieee80211_sta *sta)
0809 {
0810     return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NONE,
0811                   IEEE80211_STA_NOTEXIST);
0812 }
0813 
0814 static int
0815 mt7915_get_stats(struct ieee80211_hw *hw,
0816          struct ieee80211_low_level_stats *stats)
0817 {
0818     struct mt7915_phy *phy = mt7915_hw_phy(hw);
0819     struct mt7915_dev *dev = mt7915_hw_dev(hw);
0820     struct mib_stats *mib = &phy->mib;
0821 
0822     mutex_lock(&dev->mt76.mutex);
0823 
0824     stats->dot11RTSSuccessCount = mib->rts_cnt;
0825     stats->dot11RTSFailureCount = mib->rts_retries_cnt;
0826     stats->dot11FCSErrorCount = mib->fcs_err_cnt;
0827     stats->dot11ACKFailureCount = mib->ack_fail_cnt;
0828 
0829     mutex_unlock(&dev->mt76.mutex);
0830 
0831     return 0;
0832 }
0833 
0834 u64 __mt7915_get_tsf(struct ieee80211_hw *hw, struct mt7915_vif *mvif)
0835 {
0836     struct mt7915_dev *dev = mt7915_hw_dev(hw);
0837     struct mt7915_phy *phy = mt7915_hw_phy(hw);
0838     bool band = phy != &dev->phy;
0839     union {
0840         u64 t64;
0841         u32 t32[2];
0842     } tsf;
0843     u16 n;
0844 
0845     lockdep_assert_held(&dev->mt76.mutex);
0846 
0847     n = mvif->mt76.omac_idx > HW_BSSID_MAX ? HW_BSSID_0
0848                            : mvif->mt76.omac_idx;
0849     /* TSF software read */
0850     if (is_mt7915(&dev->mt76))
0851         mt76_rmw(dev, MT_LPON_TCR(band, n), MT_LPON_TCR_SW_MODE,
0852              MT_LPON_TCR_SW_READ);
0853     else
0854         mt76_rmw(dev, MT_LPON_TCR_MT7916(band, n), MT_LPON_TCR_SW_MODE,
0855              MT_LPON_TCR_SW_READ);
0856     tsf.t32[0] = mt76_rr(dev, MT_LPON_UTTR0(band));
0857     tsf.t32[1] = mt76_rr(dev, MT_LPON_UTTR1(band));
0858 
0859     return tsf.t64;
0860 }
0861 
0862 static u64
0863 mt7915_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
0864 {
0865     struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
0866     struct mt7915_dev *dev = mt7915_hw_dev(hw);
0867     u64 ret;
0868 
0869     mutex_lock(&dev->mt76.mutex);
0870     ret = __mt7915_get_tsf(hw, mvif);
0871     mutex_unlock(&dev->mt76.mutex);
0872 
0873     return ret;
0874 }
0875 
0876 static void
0877 mt7915_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
0878            u64 timestamp)
0879 {
0880     struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
0881     struct mt7915_dev *dev = mt7915_hw_dev(hw);
0882     struct mt7915_phy *phy = mt7915_hw_phy(hw);
0883     bool band = phy != &dev->phy;
0884     union {
0885         u64 t64;
0886         u32 t32[2];
0887     } tsf = { .t64 = timestamp, };
0888     u16 n;
0889 
0890     mutex_lock(&dev->mt76.mutex);
0891 
0892     n = mvif->mt76.omac_idx > HW_BSSID_MAX ? HW_BSSID_0
0893                            : mvif->mt76.omac_idx;
0894     mt76_wr(dev, MT_LPON_UTTR0(band), tsf.t32[0]);
0895     mt76_wr(dev, MT_LPON_UTTR1(band), tsf.t32[1]);
0896     /* TSF software overwrite */
0897     if (is_mt7915(&dev->mt76))
0898         mt76_rmw(dev, MT_LPON_TCR(band, n), MT_LPON_TCR_SW_MODE,
0899              MT_LPON_TCR_SW_WRITE);
0900     else
0901         mt76_rmw(dev, MT_LPON_TCR_MT7916(band, n), MT_LPON_TCR_SW_MODE,
0902              MT_LPON_TCR_SW_WRITE);
0903 
0904     mutex_unlock(&dev->mt76.mutex);
0905 }
0906 
0907 static void
0908 mt7915_offset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
0909           s64 timestamp)
0910 {
0911     struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
0912     struct mt7915_dev *dev = mt7915_hw_dev(hw);
0913     struct mt7915_phy *phy = mt7915_hw_phy(hw);
0914     bool band = phy != &dev->phy;
0915     union {
0916         u64 t64;
0917         u32 t32[2];
0918     } tsf = { .t64 = timestamp, };
0919     u16 n;
0920 
0921     mutex_lock(&dev->mt76.mutex);
0922 
0923     n = mvif->mt76.omac_idx > HW_BSSID_MAX ? HW_BSSID_0
0924                            : mvif->mt76.omac_idx;
0925     mt76_wr(dev, MT_LPON_UTTR0(band), tsf.t32[0]);
0926     mt76_wr(dev, MT_LPON_UTTR1(band), tsf.t32[1]);
0927     /* TSF software adjust*/
0928     if (is_mt7915(&dev->mt76))
0929         mt76_rmw(dev, MT_LPON_TCR(band, n), MT_LPON_TCR_SW_MODE,
0930              MT_LPON_TCR_SW_ADJUST);
0931     else
0932         mt76_rmw(dev, MT_LPON_TCR_MT7916(band, n), MT_LPON_TCR_SW_MODE,
0933              MT_LPON_TCR_SW_ADJUST);
0934 
0935     mutex_unlock(&dev->mt76.mutex);
0936 }
0937 
0938 static void
0939 mt7915_set_coverage_class(struct ieee80211_hw *hw, s16 coverage_class)
0940 {
0941     struct mt7915_phy *phy = mt7915_hw_phy(hw);
0942     struct mt7915_dev *dev = phy->dev;
0943 
0944     mutex_lock(&dev->mt76.mutex);
0945     phy->coverage_class = max_t(s16, coverage_class, 0);
0946     mt7915_mac_set_timing(phy);
0947     mutex_unlock(&dev->mt76.mutex);
0948 }
0949 
0950 static int
0951 mt7915_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
0952 {
0953     struct mt7915_dev *dev = mt7915_hw_dev(hw);
0954     struct mt7915_phy *phy = mt7915_hw_phy(hw);
0955     int max_nss = hweight8(hw->wiphy->available_antennas_tx);
0956     bool ext_phy = phy != &dev->phy;
0957 
0958     if (!tx_ant || tx_ant != rx_ant || ffs(tx_ant) > max_nss)
0959         return -EINVAL;
0960 
0961     if ((BIT(hweight8(tx_ant)) - 1) != tx_ant)
0962         tx_ant = BIT(ffs(tx_ant) - 1) - 1;
0963 
0964     mutex_lock(&dev->mt76.mutex);
0965 
0966     phy->mt76->antenna_mask = tx_ant;
0967 
0968     if (ext_phy)
0969         tx_ant <<= dev->chainshift;
0970 
0971     phy->mt76->chainmask = tx_ant;
0972 
0973     mt76_set_stream_caps(phy->mt76, true);
0974     mt7915_set_stream_vht_txbf_caps(phy);
0975     mt7915_set_stream_he_caps(phy);
0976 
0977     mutex_unlock(&dev->mt76.mutex);
0978 
0979     return 0;
0980 }
0981 
0982 static void mt7915_sta_statistics(struct ieee80211_hw *hw,
0983                   struct ieee80211_vif *vif,
0984                   struct ieee80211_sta *sta,
0985                   struct station_info *sinfo)
0986 {
0987     struct mt7915_phy *phy = mt7915_hw_phy(hw);
0988     struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
0989     struct rate_info *txrate = &msta->wcid.rate;
0990     struct rate_info rxrate = {};
0991 
0992     if (is_mt7915(&phy->dev->mt76) &&
0993         !mt7915_mcu_get_rx_rate(phy, vif, sta, &rxrate)) {
0994         sinfo->rxrate = rxrate;
0995         sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
0996     }
0997 
0998     if (!txrate->legacy && !txrate->flags)
0999         return;
1000 
1001     if (txrate->legacy) {
1002         sinfo->txrate.legacy = txrate->legacy;
1003     } else {
1004         sinfo->txrate.mcs = txrate->mcs;
1005         sinfo->txrate.nss = txrate->nss;
1006         sinfo->txrate.bw = txrate->bw;
1007         sinfo->txrate.he_gi = txrate->he_gi;
1008         sinfo->txrate.he_dcm = txrate->he_dcm;
1009         sinfo->txrate.he_ru_alloc = txrate->he_ru_alloc;
1010     }
1011     sinfo->txrate.flags = txrate->flags;
1012     sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
1013 }
1014 
1015 static void mt7915_sta_rc_work(void *data, struct ieee80211_sta *sta)
1016 {
1017     struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
1018     struct mt7915_dev *dev = msta->vif->phy->dev;
1019     u32 *changed = data;
1020 
1021     spin_lock_bh(&dev->sta_poll_lock);
1022     msta->changed |= *changed;
1023     if (list_empty(&msta->rc_list))
1024         list_add_tail(&msta->rc_list, &dev->sta_rc_list);
1025     spin_unlock_bh(&dev->sta_poll_lock);
1026 }
1027 
1028 static void mt7915_sta_rc_update(struct ieee80211_hw *hw,
1029                  struct ieee80211_vif *vif,
1030                  struct ieee80211_sta *sta,
1031                  u32 changed)
1032 {
1033     struct mt7915_phy *phy = mt7915_hw_phy(hw);
1034     struct mt7915_dev *dev = phy->dev;
1035 
1036     mt7915_sta_rc_work(&changed, sta);
1037     ieee80211_queue_work(hw, &dev->rc_work);
1038 }
1039 
1040 static int
1041 mt7915_set_bitrate_mask(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1042             const struct cfg80211_bitrate_mask *mask)
1043 {
1044     struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
1045     struct mt7915_phy *phy = mt7915_hw_phy(hw);
1046     struct mt7915_dev *dev = phy->dev;
1047     u32 changed = IEEE80211_RC_SUPP_RATES_CHANGED;
1048 
1049     mvif->bitrate_mask = *mask;
1050 
1051     /* if multiple rates across different preambles are given we can
1052      * reconfigure this info with all peers using sta_rec command with
1053      * the below exception cases.
1054      * - single rate : if a rate is passed along with different preambles,
1055      * we select the highest one as fixed rate. i.e VHT MCS for VHT peers.
1056      * - multiple rates: if it's not in range format i.e 0-{7,8,9} for VHT
1057      * then multiple MCS setting (MCS 4,5,6) is not supported.
1058      */
1059     ieee80211_iterate_stations_atomic(hw, mt7915_sta_rc_work, &changed);
1060     ieee80211_queue_work(hw, &dev->rc_work);
1061 
1062     return 0;
1063 }
1064 
1065 static void mt7915_sta_set_4addr(struct ieee80211_hw *hw,
1066                  struct ieee80211_vif *vif,
1067                  struct ieee80211_sta *sta,
1068                  bool enabled)
1069 {
1070     struct mt7915_dev *dev = mt7915_hw_dev(hw);
1071     struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
1072 
1073     if (enabled)
1074         set_bit(MT_WCID_FLAG_4ADDR, &msta->wcid.flags);
1075     else
1076         clear_bit(MT_WCID_FLAG_4ADDR, &msta->wcid.flags);
1077 
1078     mt76_connac_mcu_wtbl_update_hdr_trans(&dev->mt76, vif, sta);
1079 }
1080 
1081 static void mt7915_sta_set_decap_offload(struct ieee80211_hw *hw,
1082                  struct ieee80211_vif *vif,
1083                  struct ieee80211_sta *sta,
1084                  bool enabled)
1085 {
1086     struct mt7915_dev *dev = mt7915_hw_dev(hw);
1087     struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
1088 
1089     if (enabled)
1090         set_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1091     else
1092         clear_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1093 
1094     mt76_connac_mcu_wtbl_update_hdr_trans(&dev->mt76, vif, sta);
1095 }
1096 
1097 static const char mt7915_gstrings_stats[][ETH_GSTRING_LEN] = {
1098     "tx_ampdu_cnt",
1099     "tx_stop_q_empty_cnt",
1100     "tx_mpdu_attempts",
1101     "tx_mpdu_success",
1102     "tx_rwp_fail_cnt",
1103     "tx_rwp_need_cnt",
1104     "tx_pkt_ebf_cnt",
1105     "tx_pkt_ibf_cnt",
1106     "tx_ampdu_len:0-1",
1107     "tx_ampdu_len:2-10",
1108     "tx_ampdu_len:11-19",
1109     "tx_ampdu_len:20-28",
1110     "tx_ampdu_len:29-37",
1111     "tx_ampdu_len:38-46",
1112     "tx_ampdu_len:47-55",
1113     "tx_ampdu_len:56-79",
1114     "tx_ampdu_len:80-103",
1115     "tx_ampdu_len:104-127",
1116     "tx_ampdu_len:128-151",
1117     "tx_ampdu_len:152-175",
1118     "tx_ampdu_len:176-199",
1119     "tx_ampdu_len:200-223",
1120     "tx_ampdu_len:224-247",
1121     "ba_miss_count",
1122     "tx_beamformer_ppdu_iBF",
1123     "tx_beamformer_ppdu_eBF",
1124     "tx_beamformer_rx_feedback_all",
1125     "tx_beamformer_rx_feedback_he",
1126     "tx_beamformer_rx_feedback_vht",
1127     "tx_beamformer_rx_feedback_ht",
1128     "tx_beamformer_rx_feedback_bw", /* zero based idx: 20, 40, 80, 160 */
1129     "tx_beamformer_rx_feedback_nc",
1130     "tx_beamformer_rx_feedback_nr",
1131     "tx_beamformee_ok_feedback_pkts",
1132     "tx_beamformee_feedback_trig",
1133     "tx_mu_beamforming",
1134     "tx_mu_mpdu",
1135     "tx_mu_successful_mpdu",
1136     "tx_su_successful_mpdu",
1137     "tx_msdu_pack_1",
1138     "tx_msdu_pack_2",
1139     "tx_msdu_pack_3",
1140     "tx_msdu_pack_4",
1141     "tx_msdu_pack_5",
1142     "tx_msdu_pack_6",
1143     "tx_msdu_pack_7",
1144     "tx_msdu_pack_8",
1145 
1146     /* rx counters */
1147     "rx_fifo_full_cnt",
1148     "rx_mpdu_cnt",
1149     "channel_idle_cnt",
1150     "primary_cca_busy_time",
1151     "secondary_cca_busy_time",
1152     "primary_energy_detect_time",
1153     "cck_mdrdy_time",
1154     "ofdm_mdrdy_time",
1155     "green_mdrdy_time",
1156     "rx_vector_mismatch_cnt",
1157     "rx_delimiter_fail_cnt",
1158     "rx_mrdy_cnt",
1159     "rx_len_mismatch_cnt",
1160     "rx_ampdu_cnt",
1161     "rx_ampdu_bytes_cnt",
1162     "rx_ampdu_valid_subframe_cnt",
1163     "rx_ampdu_valid_subframe_b_cnt",
1164     "rx_pfdrop_cnt",
1165     "rx_vec_queue_overflow_drop_cnt",
1166     "rx_ba_cnt",
1167 
1168     /* per vif counters */
1169     "v_tx_mode_cck",
1170     "v_tx_mode_ofdm",
1171     "v_tx_mode_ht",
1172     "v_tx_mode_ht_gf",
1173     "v_tx_mode_vht",
1174     "v_tx_mode_he_su",
1175     "v_tx_mode_he_ext_su",
1176     "v_tx_mode_he_tb",
1177     "v_tx_mode_he_mu",
1178     "v_tx_bw_20",
1179     "v_tx_bw_40",
1180     "v_tx_bw_80",
1181     "v_tx_bw_160",
1182     "v_tx_mcs_0",
1183     "v_tx_mcs_1",
1184     "v_tx_mcs_2",
1185     "v_tx_mcs_3",
1186     "v_tx_mcs_4",
1187     "v_tx_mcs_5",
1188     "v_tx_mcs_6",
1189     "v_tx_mcs_7",
1190     "v_tx_mcs_8",
1191     "v_tx_mcs_9",
1192     "v_tx_mcs_10",
1193     "v_tx_mcs_11",
1194 };
1195 
1196 #define MT7915_SSTATS_LEN ARRAY_SIZE(mt7915_gstrings_stats)
1197 
1198 /* Ethtool related API */
1199 static
1200 void mt7915_get_et_strings(struct ieee80211_hw *hw,
1201                struct ieee80211_vif *vif,
1202                u32 sset, u8 *data)
1203 {
1204     if (sset == ETH_SS_STATS)
1205         memcpy(data, *mt7915_gstrings_stats,
1206                sizeof(mt7915_gstrings_stats));
1207 }
1208 
1209 static
1210 int mt7915_get_et_sset_count(struct ieee80211_hw *hw,
1211                  struct ieee80211_vif *vif, int sset)
1212 {
1213     if (sset == ETH_SS_STATS)
1214         return MT7915_SSTATS_LEN;
1215 
1216     return 0;
1217 }
1218 
1219 static void mt7915_ethtool_worker(void *wi_data, struct ieee80211_sta *sta)
1220 {
1221     struct mt76_ethtool_worker_info *wi = wi_data;
1222     struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
1223 
1224     if (msta->vif->mt76.idx != wi->idx)
1225         return;
1226 
1227     mt76_ethtool_worker(wi, &msta->stats);
1228 }
1229 
1230 static
1231 void mt7915_get_et_stats(struct ieee80211_hw *hw,
1232              struct ieee80211_vif *vif,
1233              struct ethtool_stats *stats, u64 *data)
1234 {
1235     struct mt7915_dev *dev = mt7915_hw_dev(hw);
1236     struct mt7915_phy *phy = mt7915_hw_phy(hw);
1237     struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
1238     struct mt76_ethtool_worker_info wi = {
1239         .data = data,
1240         .idx = mvif->mt76.idx,
1241     };
1242     struct mib_stats *mib = &phy->mib;
1243     /* See mt7915_ampdu_stat_read_phy, etc */
1244     int i, n, ei = 0;
1245 
1246     mutex_lock(&dev->mt76.mutex);
1247 
1248     mt7915_mac_update_stats(phy);
1249 
1250     data[ei++] = mib->tx_ampdu_cnt;
1251     data[ei++] = mib->tx_stop_q_empty_cnt;
1252     data[ei++] = mib->tx_mpdu_attempts_cnt;
1253     data[ei++] = mib->tx_mpdu_success_cnt;
1254     data[ei++] = mib->tx_rwp_fail_cnt;
1255     data[ei++] = mib->tx_rwp_need_cnt;
1256     data[ei++] = mib->tx_pkt_ebf_cnt;
1257     data[ei++] = mib->tx_pkt_ibf_cnt;
1258 
1259     /* Tx ampdu stat */
1260     n = phy->band_idx ? ARRAY_SIZE(dev->mt76.aggr_stats) / 2 : 0;
1261     for (i = 0; i < 15 /*ARRAY_SIZE(bound)*/; i++)
1262         data[ei++] = dev->mt76.aggr_stats[i + n];
1263 
1264     data[ei++] = phy->mib.ba_miss_cnt;
1265 
1266     /* Tx Beamformer monitor */
1267     data[ei++] = mib->tx_bf_ibf_ppdu_cnt;
1268     data[ei++] = mib->tx_bf_ebf_ppdu_cnt;
1269 
1270     /* Tx Beamformer Rx feedback monitor */
1271     data[ei++] = mib->tx_bf_rx_fb_all_cnt;
1272     data[ei++] = mib->tx_bf_rx_fb_he_cnt;
1273     data[ei++] = mib->tx_bf_rx_fb_vht_cnt;
1274     data[ei++] = mib->tx_bf_rx_fb_ht_cnt;
1275 
1276     data[ei++] = mib->tx_bf_rx_fb_bw;
1277     data[ei++] = mib->tx_bf_rx_fb_nc_cnt;
1278     data[ei++] = mib->tx_bf_rx_fb_nr_cnt;
1279 
1280     /* Tx Beamformee Rx NDPA & Tx feedback report */
1281     data[ei++] = mib->tx_bf_fb_cpl_cnt;
1282     data[ei++] = mib->tx_bf_fb_trig_cnt;
1283 
1284     /* Tx SU & MU counters */
1285     data[ei++] = mib->tx_bf_cnt;
1286     data[ei++] = mib->tx_mu_mpdu_cnt;
1287     data[ei++] = mib->tx_mu_acked_mpdu_cnt;
1288     data[ei++] = mib->tx_su_acked_mpdu_cnt;
1289 
1290     /* Tx amsdu info (pack-count histogram) */
1291     for (i = 0; i < ARRAY_SIZE(mib->tx_amsdu); i++)
1292         data[ei++] = mib->tx_amsdu[i];
1293 
1294     /* rx counters */
1295     data[ei++] = mib->rx_fifo_full_cnt;
1296     data[ei++] = mib->rx_mpdu_cnt;
1297     data[ei++] = mib->channel_idle_cnt;
1298     data[ei++] = mib->primary_cca_busy_time;
1299     data[ei++] = mib->secondary_cca_busy_time;
1300     data[ei++] = mib->primary_energy_detect_time;
1301     data[ei++] = mib->cck_mdrdy_time;
1302     data[ei++] = mib->ofdm_mdrdy_time;
1303     data[ei++] = mib->green_mdrdy_time;
1304     data[ei++] = mib->rx_vector_mismatch_cnt;
1305     data[ei++] = mib->rx_delimiter_fail_cnt;
1306     data[ei++] = mib->rx_mrdy_cnt;
1307     data[ei++] = mib->rx_len_mismatch_cnt;
1308     data[ei++] = mib->rx_ampdu_cnt;
1309     data[ei++] = mib->rx_ampdu_bytes_cnt;
1310     data[ei++] = mib->rx_ampdu_valid_subframe_cnt;
1311     data[ei++] = mib->rx_ampdu_valid_subframe_bytes_cnt;
1312     data[ei++] = mib->rx_pfdrop_cnt;
1313     data[ei++] = mib->rx_vec_queue_overflow_drop_cnt;
1314     data[ei++] = mib->rx_ba_cnt;
1315 
1316     /* Add values for all stations owned by this vif */
1317     wi.initial_stat_idx = ei;
1318     ieee80211_iterate_stations_atomic(hw, mt7915_ethtool_worker, &wi);
1319 
1320     mutex_unlock(&dev->mt76.mutex);
1321 
1322     if (wi.sta_count == 0)
1323         return;
1324 
1325     ei += wi.worker_stat_count;
1326     if (ei != MT7915_SSTATS_LEN)
1327         dev_err(dev->mt76.dev, "ei: %d  MT7915_SSTATS_LEN: %d",
1328             ei, (int)MT7915_SSTATS_LEN);
1329 }
1330 
1331 static void
1332 mt7915_twt_teardown_request(struct ieee80211_hw *hw,
1333                 struct ieee80211_sta *sta,
1334                 u8 flowid)
1335 {
1336     struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
1337     struct mt7915_dev *dev = mt7915_hw_dev(hw);
1338 
1339     mutex_lock(&dev->mt76.mutex);
1340     mt7915_mac_twt_teardown_flow(dev, msta, flowid);
1341     mutex_unlock(&dev->mt76.mutex);
1342 }
1343 
1344 static int
1345 mt7915_set_radar_background(struct ieee80211_hw *hw,
1346                 struct cfg80211_chan_def *chandef)
1347 {
1348     struct mt7915_phy *phy = mt7915_hw_phy(hw);
1349     struct mt7915_dev *dev = phy->dev;
1350     int ret = -EINVAL;
1351     bool running;
1352 
1353     mutex_lock(&dev->mt76.mutex);
1354 
1355     if (dev->mt76.region == NL80211_DFS_UNSET)
1356         goto out;
1357 
1358     if (dev->rdd2_phy && dev->rdd2_phy != phy) {
1359         /* rdd2 is already locked */
1360         ret = -EBUSY;
1361         goto out;
1362     }
1363 
1364     /* rdd2 already configured on a radar channel */
1365     running = dev->rdd2_phy &&
1366           cfg80211_chandef_valid(&dev->rdd2_chandef) &&
1367           !!(dev->rdd2_chandef.chan->flags & IEEE80211_CHAN_RADAR);
1368 
1369     if (!chandef || running ||
1370         !(chandef->chan->flags & IEEE80211_CHAN_RADAR)) {
1371         ret = mt7915_mcu_rdd_background_enable(phy, NULL);
1372         if (ret)
1373             goto out;
1374 
1375         if (!running)
1376             goto update_phy;
1377     }
1378 
1379     ret = mt7915_mcu_rdd_background_enable(phy, chandef);
1380     if (ret)
1381         goto out;
1382 
1383 update_phy:
1384     dev->rdd2_phy = chandef ? phy : NULL;
1385     if (chandef)
1386         dev->rdd2_chandef = *chandef;
1387 out:
1388     mutex_unlock(&dev->mt76.mutex);
1389 
1390     return ret;
1391 }
1392 
1393 #ifdef CONFIG_NET_MEDIATEK_SOC_WED
1394 static int
1395 mt7915_net_fill_forward_path(struct ieee80211_hw *hw,
1396                  struct ieee80211_vif *vif,
1397                  struct ieee80211_sta *sta,
1398                  struct net_device_path_ctx *ctx,
1399                  struct net_device_path *path)
1400 {
1401     struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
1402     struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
1403     struct mt7915_dev *dev = mt7915_hw_dev(hw);
1404     struct mt7915_phy *phy = mt7915_hw_phy(hw);
1405     struct mtk_wed_device *wed = &dev->mt76.mmio.wed;
1406 
1407     if (!mtk_wed_device_active(wed))
1408         return -ENODEV;
1409 
1410     if (msta->wcid.idx > 0xff)
1411         return -EIO;
1412 
1413     path->type = DEV_PATH_MTK_WDMA;
1414     path->dev = ctx->dev;
1415     path->mtk_wdma.wdma_idx = wed->wdma_idx;
1416     path->mtk_wdma.bss = mvif->mt76.idx;
1417     path->mtk_wdma.wcid = msta->wcid.idx;
1418     path->mtk_wdma.queue = phy != &dev->phy;
1419 
1420     ctx->dev = NULL;
1421 
1422     return 0;
1423 }
1424 #endif
1425 
1426 const struct ieee80211_ops mt7915_ops = {
1427     .tx = mt7915_tx,
1428     .start = mt7915_start,
1429     .stop = mt7915_stop,
1430     .add_interface = mt7915_add_interface,
1431     .remove_interface = mt7915_remove_interface,
1432     .config = mt7915_config,
1433     .conf_tx = mt7915_conf_tx,
1434     .configure_filter = mt7915_configure_filter,
1435     .bss_info_changed = mt7915_bss_info_changed,
1436     .sta_add = mt7915_sta_add,
1437     .sta_remove = mt7915_sta_remove,
1438     .sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
1439     .sta_rc_update = mt7915_sta_rc_update,
1440     .set_key = mt7915_set_key,
1441     .ampdu_action = mt7915_ampdu_action,
1442     .set_rts_threshold = mt7915_set_rts_threshold,
1443     .wake_tx_queue = mt76_wake_tx_queue,
1444     .sw_scan_start = mt76_sw_scan,
1445     .sw_scan_complete = mt76_sw_scan_complete,
1446     .release_buffered_frames = mt76_release_buffered_frames,
1447     .get_txpower = mt76_get_txpower,
1448     .set_sar_specs = mt7915_set_sar_specs,
1449     .channel_switch_beacon = mt7915_channel_switch_beacon,
1450     .get_stats = mt7915_get_stats,
1451     .get_et_sset_count = mt7915_get_et_sset_count,
1452     .get_et_stats = mt7915_get_et_stats,
1453     .get_et_strings = mt7915_get_et_strings,
1454     .get_tsf = mt7915_get_tsf,
1455     .set_tsf = mt7915_set_tsf,
1456     .offset_tsf = mt7915_offset_tsf,
1457     .get_survey = mt76_get_survey,
1458     .get_antenna = mt76_get_antenna,
1459     .set_antenna = mt7915_set_antenna,
1460     .set_bitrate_mask = mt7915_set_bitrate_mask,
1461     .set_coverage_class = mt7915_set_coverage_class,
1462     .sta_statistics = mt7915_sta_statistics,
1463     .sta_set_4addr = mt7915_sta_set_4addr,
1464     .sta_set_decap_offload = mt7915_sta_set_decap_offload,
1465     .add_twt_setup = mt7915_mac_add_twt_setup,
1466     .twt_teardown_request = mt7915_twt_teardown_request,
1467     CFG80211_TESTMODE_CMD(mt76_testmode_cmd)
1468     CFG80211_TESTMODE_DUMP(mt76_testmode_dump)
1469 #ifdef CONFIG_MAC80211_DEBUGFS
1470     .sta_add_debugfs = mt7915_sta_add_debugfs,
1471 #endif
1472     .set_radar_background = mt7915_set_radar_background,
1473 #ifdef CONFIG_NET_MEDIATEK_SOC_WED
1474     .net_fill_forward_path = mt7915_net_fill_forward_path,
1475 #endif
1476 };