Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: ISC
0002 /* Copyright (C) 2019 MediaTek Inc.
0003  *
0004  * Author: Roy Luo <royluo@google.com>
0005  *         Ryder Lee <ryder.lee@mediatek.com>
0006  *         Felix Fietkau <nbd@nbd.name>
0007  *         Lorenzo Bianconi <lorenzo@kernel.org>
0008  */
0009 
0010 #include <linux/etherdevice.h>
0011 #include <linux/module.h>
0012 #include "mt7615.h"
0013 #include "mcu.h"
0014 
0015 static bool mt7615_dev_running(struct mt7615_dev *dev)
0016 {
0017     struct mt7615_phy *phy;
0018 
0019     if (test_bit(MT76_STATE_RUNNING, &dev->mphy.state))
0020         return true;
0021 
0022     phy = mt7615_ext_phy(dev);
0023 
0024     return phy && test_bit(MT76_STATE_RUNNING, &phy->mt76->state);
0025 }
0026 
0027 static int mt7615_start(struct ieee80211_hw *hw)
0028 {
0029     struct mt7615_dev *dev = mt7615_hw_dev(hw);
0030     struct mt7615_phy *phy = mt7615_hw_phy(hw);
0031     unsigned long timeout;
0032     bool running;
0033     int ret;
0034 
0035     if (!mt7615_wait_for_mcu_init(dev))
0036         return -EIO;
0037 
0038     mt7615_mutex_acquire(dev);
0039 
0040     running = mt7615_dev_running(dev);
0041 
0042     if (!running) {
0043         ret = mt7615_mcu_set_pm(dev, 0, 0);
0044         if (ret)
0045             goto out;
0046 
0047         ret = mt76_connac_mcu_set_mac_enable(&dev->mt76, 0, true, false);
0048         if (ret)
0049             goto out;
0050 
0051         mt7615_mac_enable_nf(dev, 0);
0052     }
0053 
0054     if (phy != &dev->phy) {
0055         ret = mt7615_mcu_set_pm(dev, 1, 0);
0056         if (ret)
0057             goto out;
0058 
0059         ret = mt76_connac_mcu_set_mac_enable(&dev->mt76, 1, true, false);
0060         if (ret)
0061             goto out;
0062 
0063         mt7615_mac_enable_nf(dev, 1);
0064     }
0065 
0066     if (mt7615_firmware_offload(dev)) {
0067         ret = mt76_connac_mcu_set_channel_domain(phy->mt76);
0068         if (ret)
0069             goto out;
0070 
0071         ret = mt76_connac_mcu_set_rate_txpower(phy->mt76);
0072         if (ret)
0073             goto out;
0074     }
0075 
0076     ret = mt7615_mcu_set_chan_info(phy, MCU_EXT_CMD(SET_RX_PATH));
0077     if (ret)
0078         goto out;
0079 
0080     set_bit(MT76_STATE_RUNNING, &phy->mt76->state);
0081 
0082     timeout = mt7615_get_macwork_timeout(dev);
0083     ieee80211_queue_delayed_work(hw, &phy->mt76->mac_work, timeout);
0084 
0085     if (!running)
0086         mt7615_mac_reset_counters(dev);
0087 
0088 out:
0089     mt7615_mutex_release(dev);
0090 
0091     return ret;
0092 }
0093 
0094 static void mt7615_stop(struct ieee80211_hw *hw)
0095 {
0096     struct mt7615_dev *dev = mt7615_hw_dev(hw);
0097     struct mt7615_phy *phy = mt7615_hw_phy(hw);
0098 
0099     cancel_delayed_work_sync(&phy->mt76->mac_work);
0100     del_timer_sync(&phy->roc_timer);
0101     cancel_work_sync(&phy->roc_work);
0102 
0103     cancel_delayed_work_sync(&dev->pm.ps_work);
0104     cancel_work_sync(&dev->pm.wake_work);
0105 
0106     mt76_connac_free_pending_tx_skbs(&dev->pm, NULL);
0107 
0108     mt7615_mutex_acquire(dev);
0109 
0110     mt76_testmode_reset(phy->mt76, true);
0111 
0112     clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
0113     cancel_delayed_work_sync(&phy->scan_work);
0114 
0115     if (phy != &dev->phy) {
0116         mt7615_mcu_set_pm(dev, 1, 1);
0117         mt76_connac_mcu_set_mac_enable(&dev->mt76, 1, false, false);
0118     }
0119 
0120     if (!mt7615_dev_running(dev)) {
0121         mt7615_mcu_set_pm(dev, 0, 1);
0122         mt76_connac_mcu_set_mac_enable(&dev->mt76, 0, false, false);
0123     }
0124 
0125     mt7615_mutex_release(dev);
0126 }
0127 
0128 static inline int get_free_idx(u32 mask, u8 start, u8 end)
0129 {
0130     return ffs(~mask & GENMASK(end, start));
0131 }
0132 
0133 static int get_omac_idx(enum nl80211_iftype type, u64 mask)
0134 {
0135     int i;
0136 
0137     switch (type) {
0138     case NL80211_IFTYPE_STATION:
0139         /* prefer hw bssid slot 1-3 */
0140         i = get_free_idx(mask, HW_BSSID_1, HW_BSSID_3);
0141         if (i)
0142             return i - 1;
0143 
0144         /* next, try to find a free repeater entry for the sta */
0145         i = get_free_idx(mask >> REPEATER_BSSID_START, 0,
0146                  REPEATER_BSSID_MAX - REPEATER_BSSID_START);
0147         if (i)
0148             return i + 32 - 1;
0149 
0150         i = get_free_idx(mask, EXT_BSSID_1, EXT_BSSID_MAX);
0151         if (i)
0152             return i - 1;
0153 
0154         if (~mask & BIT(HW_BSSID_0))
0155             return HW_BSSID_0;
0156 
0157         break;
0158     case NL80211_IFTYPE_ADHOC:
0159     case NL80211_IFTYPE_MESH_POINT:
0160     case NL80211_IFTYPE_MONITOR:
0161     case NL80211_IFTYPE_AP:
0162         /* ap uses hw bssid 0 and ext bssid */
0163         if (~mask & BIT(HW_BSSID_0))
0164             return HW_BSSID_0;
0165 
0166         i = get_free_idx(mask, EXT_BSSID_1, EXT_BSSID_MAX);
0167         if (i)
0168             return i - 1;
0169 
0170         break;
0171     default:
0172         WARN_ON(1);
0173         break;
0174     }
0175 
0176     return -1;
0177 }
0178 
0179 static int mt7615_add_interface(struct ieee80211_hw *hw,
0180                 struct ieee80211_vif *vif)
0181 {
0182     struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
0183     struct mt7615_dev *dev = mt7615_hw_dev(hw);
0184     struct mt7615_phy *phy = mt7615_hw_phy(hw);
0185     struct mt76_txq *mtxq;
0186     bool ext_phy = phy != &dev->phy;
0187     int idx, ret = 0;
0188 
0189     mt7615_mutex_acquire(dev);
0190 
0191     mt76_testmode_reset(phy->mt76, true);
0192 
0193     if (vif->type == NL80211_IFTYPE_MONITOR &&
0194         is_zero_ether_addr(vif->addr))
0195         phy->monitor_vif = vif;
0196 
0197     mvif->mt76.idx = __ffs64(~dev->mt76.vif_mask);
0198     if (mvif->mt76.idx >= MT7615_MAX_INTERFACES) {
0199         ret = -ENOSPC;
0200         goto out;
0201     }
0202 
0203     idx = get_omac_idx(vif->type, dev->omac_mask);
0204     if (idx < 0) {
0205         ret = -ENOSPC;
0206         goto out;
0207     }
0208     mvif->mt76.omac_idx = idx;
0209 
0210     mvif->mt76.band_idx = ext_phy;
0211     mvif->mt76.wmm_idx = vif->type != NL80211_IFTYPE_AP;
0212     if (ext_phy)
0213         mvif->mt76.wmm_idx += 2;
0214 
0215     dev->mt76.vif_mask |= BIT_ULL(mvif->mt76.idx);
0216     dev->omac_mask |= BIT_ULL(mvif->mt76.omac_idx);
0217     phy->omac_mask |= BIT_ULL(mvif->mt76.omac_idx);
0218 
0219     ret = mt7615_mcu_set_dbdc(dev);
0220     if (ret)
0221         goto out;
0222 
0223     idx = MT7615_WTBL_RESERVED - mvif->mt76.idx;
0224 
0225     INIT_LIST_HEAD(&mvif->sta.poll_list);
0226     mvif->sta.wcid.idx = idx;
0227     mvif->sta.wcid.phy_idx = mvif->mt76.band_idx;
0228     mvif->sta.wcid.hw_key_idx = -1;
0229     mt76_packet_id_init(&mvif->sta.wcid);
0230 
0231     mt7615_mac_wtbl_update(dev, idx,
0232                    MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
0233 
0234     rcu_assign_pointer(dev->mt76.wcid[idx], &mvif->sta.wcid);
0235     if (vif->txq) {
0236         mtxq = (struct mt76_txq *)vif->txq->drv_priv;
0237         mtxq->wcid = idx;
0238     }
0239 
0240     ret = mt7615_mcu_add_dev_info(phy, vif, true);
0241 out:
0242     mt7615_mutex_release(dev);
0243 
0244     return ret;
0245 }
0246 
0247 static void mt7615_remove_interface(struct ieee80211_hw *hw,
0248                     struct ieee80211_vif *vif)
0249 {
0250     struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
0251     struct mt7615_sta *msta = &mvif->sta;
0252     struct mt7615_dev *dev = mt7615_hw_dev(hw);
0253     struct mt7615_phy *phy = mt7615_hw_phy(hw);
0254     int idx = msta->wcid.idx;
0255 
0256     mt7615_mutex_acquire(dev);
0257 
0258     mt7615_mcu_add_bss_info(phy, vif, NULL, false);
0259     mt7615_mcu_sta_add(phy, vif, NULL, false);
0260 
0261     mt76_testmode_reset(phy->mt76, true);
0262     if (vif == phy->monitor_vif)
0263         phy->monitor_vif = NULL;
0264 
0265     mt76_connac_free_pending_tx_skbs(&dev->pm, &msta->wcid);
0266 
0267     mt7615_mcu_add_dev_info(phy, vif, false);
0268 
0269     rcu_assign_pointer(dev->mt76.wcid[idx], NULL);
0270 
0271     dev->mt76.vif_mask &= ~BIT_ULL(mvif->mt76.idx);
0272     dev->omac_mask &= ~BIT_ULL(mvif->mt76.omac_idx);
0273     phy->omac_mask &= ~BIT_ULL(mvif->mt76.omac_idx);
0274 
0275     mt7615_mutex_release(dev);
0276 
0277     spin_lock_bh(&dev->sta_poll_lock);
0278     if (!list_empty(&msta->poll_list))
0279         list_del_init(&msta->poll_list);
0280     spin_unlock_bh(&dev->sta_poll_lock);
0281 
0282     mt76_packet_id_flush(&dev->mt76, &mvif->sta.wcid);
0283 }
0284 
0285 int mt7615_set_channel(struct mt7615_phy *phy)
0286 {
0287     struct mt7615_dev *dev = phy->dev;
0288     bool ext_phy = phy != &dev->phy;
0289     int ret;
0290 
0291     cancel_delayed_work_sync(&phy->mt76->mac_work);
0292 
0293     mt7615_mutex_acquire(dev);
0294 
0295     set_bit(MT76_RESET, &phy->mt76->state);
0296 
0297     mt76_set_channel(phy->mt76);
0298 
0299     if (is_mt7615(&dev->mt76) && dev->flash_eeprom) {
0300         ret = mt7615_mcu_apply_rx_dcoc(phy);
0301         if (ret)
0302             goto out;
0303 
0304         ret = mt7615_mcu_apply_tx_dpd(phy);
0305         if (ret)
0306             goto out;
0307     }
0308 
0309     ret = mt7615_mcu_set_chan_info(phy, MCU_EXT_CMD(CHANNEL_SWITCH));
0310     if (ret)
0311         goto out;
0312 
0313     mt7615_mac_set_timing(phy);
0314     ret = mt7615_dfs_init_radar_detector(phy);
0315     if (ret)
0316         goto out;
0317 
0318     mt7615_mac_cca_stats_reset(phy);
0319     ret = mt7615_mcu_set_sku_en(phy, true);
0320     if (ret)
0321         goto out;
0322 
0323     mt7615_mac_reset_counters(dev);
0324     phy->noise = 0;
0325     phy->chfreq = mt76_rr(dev, MT_CHFREQ(ext_phy));
0326 
0327 out:
0328     clear_bit(MT76_RESET, &phy->mt76->state);
0329 
0330     mt7615_mutex_release(dev);
0331 
0332     mt76_worker_schedule(&dev->mt76.tx_worker);
0333     if (!mt76_testmode_enabled(phy->mt76)) {
0334         unsigned long timeout = mt7615_get_macwork_timeout(dev);
0335 
0336         ieee80211_queue_delayed_work(phy->mt76->hw,
0337                          &phy->mt76->mac_work, timeout);
0338     }
0339 
0340     return ret;
0341 }
0342 
0343 static int mt7615_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
0344               struct ieee80211_vif *vif, struct ieee80211_sta *sta,
0345               struct ieee80211_key_conf *key)
0346 {
0347     struct mt7615_dev *dev = mt7615_hw_dev(hw);
0348     struct mt7615_phy *phy = mt7615_hw_phy(hw);
0349     struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
0350     struct mt7615_sta *msta = sta ? (struct mt7615_sta *)sta->drv_priv :
0351                   &mvif->sta;
0352     struct mt76_wcid *wcid = &msta->wcid;
0353     int idx = key->keyidx, err = 0;
0354     u8 *wcid_keyidx = &wcid->hw_key_idx;
0355 
0356     /* The hardware does not support per-STA RX GTK, fallback
0357      * to software mode for these.
0358      */
0359     if ((vif->type == NL80211_IFTYPE_ADHOC ||
0360          vif->type == NL80211_IFTYPE_MESH_POINT) &&
0361         (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
0362          key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
0363         !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
0364         return -EOPNOTSUPP;
0365 
0366     /* fall back to sw encryption for unsupported ciphers */
0367     switch (key->cipher) {
0368     case WLAN_CIPHER_SUITE_AES_CMAC:
0369         wcid_keyidx = &wcid->hw_key_idx2;
0370         key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIE;
0371         break;
0372     case WLAN_CIPHER_SUITE_TKIP:
0373     case WLAN_CIPHER_SUITE_CCMP:
0374     case WLAN_CIPHER_SUITE_CCMP_256:
0375     case WLAN_CIPHER_SUITE_GCMP:
0376     case WLAN_CIPHER_SUITE_GCMP_256:
0377     case WLAN_CIPHER_SUITE_SMS4:
0378         break;
0379     case WLAN_CIPHER_SUITE_WEP40:
0380     case WLAN_CIPHER_SUITE_WEP104:
0381     default:
0382         return -EOPNOTSUPP;
0383     }
0384 
0385     mt7615_mutex_acquire(dev);
0386 
0387     if (cmd == SET_KEY && !sta && !mvif->mt76.cipher) {
0388         mvif->mt76.cipher = mt76_connac_mcu_get_cipher(key->cipher);
0389         mt7615_mcu_add_bss_info(phy, vif, NULL, true);
0390     }
0391 
0392     if (cmd == SET_KEY)
0393         *wcid_keyidx = idx;
0394     else if (idx == *wcid_keyidx)
0395         *wcid_keyidx = -1;
0396     else
0397         goto out;
0398 
0399     mt76_wcid_key_setup(&dev->mt76, wcid,
0400                 cmd == SET_KEY ? key : NULL);
0401 
0402     if (mt76_is_mmio(&dev->mt76))
0403         err = mt7615_mac_wtbl_set_key(dev, wcid, key, cmd);
0404     else
0405         err = __mt7615_mac_wtbl_set_key(dev, wcid, key, cmd);
0406 
0407 out:
0408     mt7615_mutex_release(dev);
0409 
0410     return err;
0411 }
0412 
0413 static int mt7615_set_sar_specs(struct ieee80211_hw *hw,
0414                 const struct cfg80211_sar_specs *sar)
0415 {
0416     struct mt7615_phy *phy = mt7615_hw_phy(hw);
0417     int err;
0418 
0419     if (!cfg80211_chandef_valid(&phy->mt76->chandef))
0420         return -EINVAL;
0421 
0422     err = mt76_init_sar_power(hw, sar);
0423     if (err)
0424         return err;
0425 
0426     if (mt7615_firmware_offload(phy->dev))
0427         return mt76_connac_mcu_set_rate_txpower(phy->mt76);
0428 
0429     ieee80211_stop_queues(hw);
0430     err = mt7615_set_channel(phy);
0431     ieee80211_wake_queues(hw);
0432 
0433     return err;
0434 }
0435 
0436 static int mt7615_config(struct ieee80211_hw *hw, u32 changed)
0437 {
0438     struct mt7615_dev *dev = mt7615_hw_dev(hw);
0439     struct mt7615_phy *phy = mt7615_hw_phy(hw);
0440     bool band = phy != &dev->phy;
0441     int ret = 0;
0442 
0443     if (changed & (IEEE80211_CONF_CHANGE_CHANNEL |
0444                IEEE80211_CONF_CHANGE_POWER)) {
0445 #ifdef CONFIG_NL80211_TESTMODE
0446         if (phy->mt76->test.state != MT76_TM_STATE_OFF) {
0447             mt7615_mutex_acquire(dev);
0448             mt76_testmode_reset(phy->mt76, false);
0449             mt7615_mutex_release(dev);
0450         }
0451 #endif
0452         ieee80211_stop_queues(hw);
0453         ret = mt7615_set_channel(phy);
0454         ieee80211_wake_queues(hw);
0455     }
0456 
0457     mt7615_mutex_acquire(dev);
0458 
0459     if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
0460         mt76_testmode_reset(phy->mt76, true);
0461 
0462         if (!(hw->conf.flags & IEEE80211_CONF_MONITOR))
0463             phy->rxfilter |= MT_WF_RFCR_DROP_OTHER_UC;
0464         else
0465             phy->rxfilter &= ~MT_WF_RFCR_DROP_OTHER_UC;
0466 
0467         mt76_wr(dev, MT_WF_RFCR(band), phy->rxfilter);
0468     }
0469 
0470     mt7615_mutex_release(dev);
0471 
0472     return ret;
0473 }
0474 
0475 static int
0476 mt7615_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
0477            unsigned int link_id, u16 queue,
0478            const struct ieee80211_tx_queue_params *params)
0479 {
0480     struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
0481     struct mt7615_dev *dev = mt7615_hw_dev(hw);
0482     int err;
0483 
0484     mt7615_mutex_acquire(dev);
0485 
0486     queue = mt7615_lmac_mapping(dev, queue);
0487     queue += mvif->wmm_idx * MT7615_MAX_WMM_SETS;
0488     err = mt7615_mcu_set_wmm(dev, queue, params);
0489 
0490     mt7615_mutex_release(dev);
0491 
0492     return err;
0493 }
0494 
0495 static void mt7615_configure_filter(struct ieee80211_hw *hw,
0496                     unsigned int changed_flags,
0497                     unsigned int *total_flags,
0498                     u64 multicast)
0499 {
0500     struct mt7615_dev *dev = mt7615_hw_dev(hw);
0501     struct mt7615_phy *phy = mt7615_hw_phy(hw);
0502     bool band = phy != &dev->phy;
0503 
0504     u32 ctl_flags = MT_WF_RFCR1_DROP_ACK |
0505             MT_WF_RFCR1_DROP_BF_POLL |
0506             MT_WF_RFCR1_DROP_BA |
0507             MT_WF_RFCR1_DROP_CFEND |
0508             MT_WF_RFCR1_DROP_CFACK;
0509     u32 flags = 0;
0510 
0511     mt7615_mutex_acquire(dev);
0512 
0513 #define MT76_FILTER(_flag, _hw) do { \
0514         flags |= *total_flags & FIF_##_flag;            \
0515         phy->rxfilter &= ~(_hw);                \
0516         if (!mt76_testmode_enabled(phy->mt76))          \
0517             phy->rxfilter |= !(flags & FIF_##_flag) * (_hw);\
0518     } while (0)
0519 
0520     phy->rxfilter &= ~(MT_WF_RFCR_DROP_OTHER_BSS |
0521                MT_WF_RFCR_DROP_FRAME_REPORT |
0522                MT_WF_RFCR_DROP_PROBEREQ |
0523                MT_WF_RFCR_DROP_MCAST_FILTERED |
0524                MT_WF_RFCR_DROP_MCAST |
0525                MT_WF_RFCR_DROP_BCAST |
0526                MT_WF_RFCR_DROP_DUPLICATE |
0527                MT_WF_RFCR_DROP_A2_BSSID |
0528                MT_WF_RFCR_DROP_UNWANTED_CTL |
0529                MT_WF_RFCR_DROP_STBC_MULTI);
0530 
0531     if (phy->n_beacon_vif || !mt7615_firmware_offload(dev))
0532         phy->rxfilter &= ~MT_WF_RFCR_DROP_OTHER_BEACON;
0533 
0534     MT76_FILTER(OTHER_BSS, MT_WF_RFCR_DROP_OTHER_TIM |
0535                    MT_WF_RFCR_DROP_A3_MAC |
0536                    MT_WF_RFCR_DROP_A3_BSSID);
0537 
0538     MT76_FILTER(FCSFAIL, MT_WF_RFCR_DROP_FCSFAIL);
0539 
0540     MT76_FILTER(CONTROL, MT_WF_RFCR_DROP_CTS |
0541                  MT_WF_RFCR_DROP_RTS |
0542                  MT_WF_RFCR_DROP_CTL_RSV |
0543                  MT_WF_RFCR_DROP_NDPA);
0544 
0545     *total_flags = flags;
0546     mt76_wr(dev, MT_WF_RFCR(band), phy->rxfilter);
0547 
0548     if (*total_flags & FIF_CONTROL)
0549         mt76_clear(dev, MT_WF_RFCR1(band), ctl_flags);
0550     else
0551         mt76_set(dev, MT_WF_RFCR1(band), ctl_flags);
0552 
0553     mt7615_mutex_release(dev);
0554 }
0555 
0556 static void mt7615_bss_info_changed(struct ieee80211_hw *hw,
0557                     struct ieee80211_vif *vif,
0558                     struct ieee80211_bss_conf *info,
0559                     u64 changed)
0560 {
0561     struct mt7615_dev *dev = mt7615_hw_dev(hw);
0562     struct mt7615_phy *phy = mt7615_hw_phy(hw);
0563 
0564     mt7615_mutex_acquire(dev);
0565 
0566     if (changed & BSS_CHANGED_ERP_SLOT) {
0567         int slottime = info->use_short_slot ? 9 : 20;
0568 
0569         if (slottime != phy->slottime) {
0570             phy->slottime = slottime;
0571             mt7615_mac_set_timing(phy);
0572         }
0573     }
0574 
0575     if (changed & BSS_CHANGED_BEACON_ENABLED && info->enable_beacon) {
0576         mt7615_mcu_add_bss_info(phy, vif, NULL, true);
0577         mt7615_mcu_sta_add(phy, vif, NULL, true);
0578 
0579         if (mt7615_firmware_offload(dev) && vif->p2p)
0580             mt76_connac_mcu_set_p2p_oppps(hw, vif);
0581     }
0582 
0583     if (changed & (BSS_CHANGED_BEACON |
0584                BSS_CHANGED_BEACON_ENABLED))
0585         mt7615_mcu_add_beacon(dev, hw, vif, info->enable_beacon);
0586 
0587     if (changed & BSS_CHANGED_PS)
0588         mt76_connac_mcu_set_vif_ps(&dev->mt76, vif);
0589 
0590     if ((changed & BSS_CHANGED_ARP_FILTER) &&
0591         mt7615_firmware_offload(dev)) {
0592         struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
0593 
0594         mt76_connac_mcu_update_arp_filter(&dev->mt76, &mvif->mt76,
0595                           info);
0596     }
0597 
0598     if (changed & BSS_CHANGED_ASSOC)
0599         mt7615_mac_set_beacon_filter(phy, vif, vif->cfg.assoc);
0600 
0601     mt7615_mutex_release(dev);
0602 }
0603 
0604 static void
0605 mt7615_channel_switch_beacon(struct ieee80211_hw *hw,
0606                  struct ieee80211_vif *vif,
0607                  struct cfg80211_chan_def *chandef)
0608 {
0609     struct mt7615_dev *dev = mt7615_hw_dev(hw);
0610 
0611     mt7615_mutex_acquire(dev);
0612     mt7615_mcu_add_beacon(dev, hw, vif, true);
0613     mt7615_mutex_release(dev);
0614 }
0615 
0616 int mt7615_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
0617                struct ieee80211_sta *sta)
0618 {
0619     struct mt7615_dev *dev = container_of(mdev, struct mt7615_dev, mt76);
0620     struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
0621     struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
0622     struct mt7615_phy *phy;
0623     int idx, err;
0624 
0625     idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7615_WTBL_STA - 1);
0626     if (idx < 0)
0627         return -ENOSPC;
0628 
0629     INIT_LIST_HEAD(&msta->poll_list);
0630     msta->vif = mvif;
0631     msta->wcid.sta = 1;
0632     msta->wcid.idx = idx;
0633     msta->wcid.phy_idx = mvif->mt76.band_idx;
0634 
0635     phy = mvif->mt76.band_idx ? mt7615_ext_phy(dev) : &dev->phy;
0636     err = mt76_connac_pm_wake(phy->mt76, &dev->pm);
0637     if (err)
0638         return err;
0639 
0640     if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls) {
0641         err = mt7615_mcu_add_bss_info(phy, vif, sta, true);
0642         if (err)
0643             return err;
0644     }
0645 
0646     mt7615_mac_wtbl_update(dev, idx,
0647                    MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
0648     err = mt7615_mcu_sta_add(&dev->phy, vif, sta, true);
0649     if (err)
0650         return err;
0651 
0652     mt76_connac_power_save_sched(phy->mt76, &dev->pm);
0653 
0654     return err;
0655 }
0656 EXPORT_SYMBOL_GPL(mt7615_mac_sta_add);
0657 
0658 void mt7615_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
0659                struct ieee80211_sta *sta)
0660 {
0661     struct mt7615_dev *dev = container_of(mdev, struct mt7615_dev, mt76);
0662     struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
0663     struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
0664     struct mt7615_phy *phy;
0665 
0666     mt76_connac_free_pending_tx_skbs(&dev->pm, &msta->wcid);
0667 
0668     phy = mvif->mt76.band_idx ? mt7615_ext_phy(dev) : &dev->phy;
0669     mt76_connac_pm_wake(phy->mt76, &dev->pm);
0670 
0671     mt7615_mcu_sta_add(&dev->phy, vif, sta, false);
0672     mt7615_mac_wtbl_update(dev, msta->wcid.idx,
0673                    MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
0674     if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
0675         mt7615_mcu_add_bss_info(phy, vif, sta, false);
0676 
0677     spin_lock_bh(&dev->sta_poll_lock);
0678     if (!list_empty(&msta->poll_list))
0679         list_del_init(&msta->poll_list);
0680     spin_unlock_bh(&dev->sta_poll_lock);
0681 
0682     mt76_connac_power_save_sched(phy->mt76, &dev->pm);
0683 }
0684 EXPORT_SYMBOL_GPL(mt7615_mac_sta_remove);
0685 
0686 static void mt7615_sta_rate_tbl_update(struct ieee80211_hw *hw,
0687                        struct ieee80211_vif *vif,
0688                        struct ieee80211_sta *sta)
0689 {
0690     struct mt7615_dev *dev = mt7615_hw_dev(hw);
0691     struct mt7615_phy *phy = mt7615_hw_phy(hw);
0692     struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
0693     struct ieee80211_sta_rates *sta_rates = rcu_dereference(sta->rates);
0694     int i;
0695 
0696     if (!sta_rates)
0697         return;
0698 
0699     spin_lock_bh(&dev->mt76.lock);
0700     for (i = 0; i < ARRAY_SIZE(msta->rates); i++) {
0701         msta->rates[i].idx = sta_rates->rate[i].idx;
0702         msta->rates[i].count = sta_rates->rate[i].count;
0703         msta->rates[i].flags = sta_rates->rate[i].flags;
0704 
0705         if (msta->rates[i].idx < 0 || !msta->rates[i].count)
0706             break;
0707     }
0708     msta->n_rates = i;
0709     if (mt76_connac_pm_ref(phy->mt76, &dev->pm)) {
0710         mt7615_mac_set_rates(phy, msta, NULL, msta->rates);
0711         mt76_connac_pm_unref(phy->mt76, &dev->pm);
0712     }
0713     spin_unlock_bh(&dev->mt76.lock);
0714 }
0715 
0716 void mt7615_tx_worker(struct mt76_worker *w)
0717 {
0718     struct mt7615_dev *dev = container_of(w, struct mt7615_dev,
0719                           mt76.tx_worker);
0720 
0721     if (!mt76_connac_pm_ref(&dev->mphy, &dev->pm)) {
0722         queue_work(dev->mt76.wq, &dev->pm.wake_work);
0723         return;
0724     }
0725 
0726     mt76_tx_worker_run(&dev->mt76);
0727     mt76_connac_pm_unref(&dev->mphy, &dev->pm);
0728 }
0729 
0730 static void mt7615_tx(struct ieee80211_hw *hw,
0731               struct ieee80211_tx_control *control,
0732               struct sk_buff *skb)
0733 {
0734     struct mt7615_dev *dev = mt7615_hw_dev(hw);
0735     struct mt76_phy *mphy = hw->priv;
0736     struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
0737     struct ieee80211_vif *vif = info->control.vif;
0738     struct mt76_wcid *wcid = &dev->mt76.global_wcid;
0739     struct mt7615_sta *msta = NULL;
0740     int qid;
0741 
0742     if (control->sta) {
0743         msta = (struct mt7615_sta *)control->sta->drv_priv;
0744         wcid = &msta->wcid;
0745     }
0746 
0747     if (vif && !control->sta) {
0748         struct mt7615_vif *mvif;
0749 
0750         mvif = (struct mt7615_vif *)vif->drv_priv;
0751         msta = &mvif->sta;
0752         wcid = &msta->wcid;
0753     }
0754 
0755     if (mt76_connac_pm_ref(mphy, &dev->pm)) {
0756         mt76_tx(mphy, control->sta, wcid, skb);
0757         mt76_connac_pm_unref(mphy, &dev->pm);
0758         return;
0759     }
0760 
0761     qid = skb_get_queue_mapping(skb);
0762     if (qid >= MT_TXQ_PSD) {
0763         qid = IEEE80211_AC_BE;
0764         skb_set_queue_mapping(skb, qid);
0765     }
0766 
0767     mt76_connac_pm_queue_skb(hw, &dev->pm, wcid, skb);
0768 }
0769 
0770 static int mt7615_set_rts_threshold(struct ieee80211_hw *hw, u32 val)
0771 {
0772     struct mt7615_dev *dev = mt7615_hw_dev(hw);
0773     struct mt7615_phy *phy = mt7615_hw_phy(hw);
0774     int err, band = phy != &dev->phy;
0775 
0776     mt7615_mutex_acquire(dev);
0777     err = mt76_connac_mcu_set_rts_thresh(&dev->mt76, val, band);
0778     mt7615_mutex_release(dev);
0779 
0780     return err;
0781 }
0782 
0783 static int
0784 mt7615_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
0785             struct ieee80211_ampdu_params *params)
0786 {
0787     enum ieee80211_ampdu_mlme_action action = params->action;
0788     struct mt7615_dev *dev = mt7615_hw_dev(hw);
0789     struct ieee80211_sta *sta = params->sta;
0790     struct ieee80211_txq *txq = sta->txq[params->tid];
0791     struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
0792     u16 tid = params->tid;
0793     u16 ssn = params->ssn;
0794     struct mt76_txq *mtxq;
0795     int ret = 0;
0796 
0797     if (!txq)
0798         return -EINVAL;
0799 
0800     mtxq = (struct mt76_txq *)txq->drv_priv;
0801 
0802     mt7615_mutex_acquire(dev);
0803 
0804     switch (action) {
0805     case IEEE80211_AMPDU_RX_START:
0806         mt76_rx_aggr_start(&dev->mt76, &msta->wcid, tid, ssn,
0807                    params->buf_size);
0808         ret = mt7615_mcu_add_rx_ba(dev, params, true);
0809         break;
0810     case IEEE80211_AMPDU_RX_STOP:
0811         mt76_rx_aggr_stop(&dev->mt76, &msta->wcid, tid);
0812         ret = mt7615_mcu_add_rx_ba(dev, params, false);
0813         break;
0814     case IEEE80211_AMPDU_TX_OPERATIONAL:
0815         mtxq->aggr = true;
0816         mtxq->send_bar = false;
0817         ret = mt7615_mcu_add_tx_ba(dev, params, true);
0818         ssn = mt7615_mac_get_sta_tid_sn(dev, msta->wcid.idx, tid);
0819         ieee80211_send_bar(vif, sta->addr, tid,
0820                    IEEE80211_SN_TO_SEQ(ssn));
0821         break;
0822     case IEEE80211_AMPDU_TX_STOP_FLUSH:
0823     case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
0824         mtxq->aggr = false;
0825         ret = mt7615_mcu_add_tx_ba(dev, params, false);
0826         break;
0827     case IEEE80211_AMPDU_TX_START:
0828         ssn = mt7615_mac_get_sta_tid_sn(dev, msta->wcid.idx, tid);
0829         params->ssn = ssn;
0830         ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
0831         break;
0832     case IEEE80211_AMPDU_TX_STOP_CONT:
0833         mtxq->aggr = false;
0834         ret = mt7615_mcu_add_tx_ba(dev, params, false);
0835         ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
0836         break;
0837     }
0838     mt7615_mutex_release(dev);
0839 
0840     return ret;
0841 }
0842 
0843 static int
0844 mt7615_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
0845            struct ieee80211_sta *sta)
0846 {
0847     return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NOTEXIST,
0848               IEEE80211_STA_NONE);
0849 }
0850 
0851 static int
0852 mt7615_sta_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
0853           struct ieee80211_sta *sta)
0854 {
0855     return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NONE,
0856               IEEE80211_STA_NOTEXIST);
0857 }
0858 
0859 static int
0860 mt7615_get_stats(struct ieee80211_hw *hw,
0861          struct ieee80211_low_level_stats *stats)
0862 {
0863     struct mt7615_phy *phy = mt7615_hw_phy(hw);
0864     struct mib_stats *mib = &phy->mib;
0865 
0866     mt7615_mutex_acquire(phy->dev);
0867 
0868     stats->dot11RTSSuccessCount = mib->rts_cnt;
0869     stats->dot11RTSFailureCount = mib->rts_retries_cnt;
0870     stats->dot11FCSErrorCount = mib->fcs_err_cnt;
0871     stats->dot11ACKFailureCount = mib->ack_fail_cnt;
0872 
0873     mt7615_mutex_release(phy->dev);
0874 
0875     return 0;
0876 }
0877 
0878 static u64
0879 mt7615_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
0880 {
0881     struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
0882     struct mt7615_dev *dev = mt7615_hw_dev(hw);
0883     union {
0884         u64 t64;
0885         u32 t32[2];
0886     } tsf;
0887     u16 idx = mvif->mt76.omac_idx;
0888     u32 reg;
0889 
0890     idx = idx > HW_BSSID_MAX ? HW_BSSID_0 : idx;
0891     reg = idx > 1 ? MT_LPON_TCR2(idx): MT_LPON_TCR0(idx);
0892 
0893     mt7615_mutex_acquire(dev);
0894 
0895     /* TSF read */
0896     mt76_rmw(dev, reg, MT_LPON_TCR_MODE, MT_LPON_TCR_READ);
0897     tsf.t32[0] = mt76_rr(dev, MT_LPON_UTTR0);
0898     tsf.t32[1] = mt76_rr(dev, MT_LPON_UTTR1);
0899 
0900     mt7615_mutex_release(dev);
0901 
0902     return tsf.t64;
0903 }
0904 
0905 static void
0906 mt7615_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
0907            u64 timestamp)
0908 {
0909     struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
0910     struct mt7615_dev *dev = mt7615_hw_dev(hw);
0911     union {
0912         u64 t64;
0913         u32 t32[2];
0914     } tsf = { .t64 = timestamp, };
0915     u16 idx = mvif->mt76.omac_idx;
0916     u32 reg;
0917 
0918     idx = idx > HW_BSSID_MAX ? HW_BSSID_0 : idx;
0919     reg = idx > 1 ? MT_LPON_TCR2(idx): MT_LPON_TCR0(idx);
0920 
0921     mt7615_mutex_acquire(dev);
0922 
0923     mt76_wr(dev, MT_LPON_UTTR0, tsf.t32[0]);
0924     mt76_wr(dev, MT_LPON_UTTR1, tsf.t32[1]);
0925     /* TSF software overwrite */
0926     mt76_rmw(dev, reg, MT_LPON_TCR_MODE, MT_LPON_TCR_WRITE);
0927 
0928     mt7615_mutex_release(dev);
0929 }
0930 
0931 static void
0932 mt7615_offset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
0933           s64 timestamp)
0934 {
0935     struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
0936     struct mt7615_dev *dev = mt7615_hw_dev(hw);
0937     union {
0938         u64 t64;
0939         u32 t32[2];
0940     } tsf = { .t64 = timestamp, };
0941     u16 idx = mvif->mt76.omac_idx;
0942     u32 reg;
0943 
0944     idx = idx > HW_BSSID_MAX ? HW_BSSID_0 : idx;
0945     reg = idx > 1 ? MT_LPON_TCR2(idx): MT_LPON_TCR0(idx);
0946 
0947     mt7615_mutex_acquire(dev);
0948 
0949     mt76_wr(dev, MT_LPON_UTTR0, tsf.t32[0]);
0950     mt76_wr(dev, MT_LPON_UTTR1, tsf.t32[1]);
0951     /* TSF software adjust*/
0952     mt76_rmw(dev, reg, MT_LPON_TCR_MODE, MT_LPON_TCR_ADJUST);
0953 
0954     mt7615_mutex_release(dev);
0955 }
0956 
0957 static void
0958 mt7615_set_coverage_class(struct ieee80211_hw *hw, s16 coverage_class)
0959 {
0960     struct mt7615_phy *phy = mt7615_hw_phy(hw);
0961     struct mt7615_dev *dev = phy->dev;
0962 
0963     mt7615_mutex_acquire(dev);
0964     phy->coverage_class = max_t(s16, coverage_class, 0);
0965     mt7615_mac_set_timing(phy);
0966     mt7615_mutex_release(dev);
0967 }
0968 
0969 static int
0970 mt7615_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
0971 {
0972     struct mt7615_dev *dev = mt7615_hw_dev(hw);
0973     struct mt7615_phy *phy = mt7615_hw_phy(hw);
0974     int max_nss = hweight8(hw->wiphy->available_antennas_tx);
0975     bool ext_phy = phy != &dev->phy;
0976 
0977     if (!tx_ant || tx_ant != rx_ant || ffs(tx_ant) > max_nss)
0978         return -EINVAL;
0979 
0980     if ((BIT(hweight8(tx_ant)) - 1) != tx_ant)
0981         tx_ant = BIT(ffs(tx_ant) - 1) - 1;
0982 
0983     mt7615_mutex_acquire(dev);
0984 
0985     phy->mt76->antenna_mask = tx_ant;
0986     if (ext_phy) {
0987         if (dev->chainmask == 0xf)
0988             tx_ant <<= 2;
0989         else
0990             tx_ant <<= 1;
0991     }
0992     phy->mt76->chainmask = tx_ant;
0993 
0994     mt76_set_stream_caps(phy->mt76, true);
0995 
0996     mt7615_mutex_release(dev);
0997 
0998     return 0;
0999 }
1000 
1001 static void mt7615_roc_iter(void *priv, u8 *mac,
1002                 struct ieee80211_vif *vif)
1003 {
1004     struct mt7615_phy *phy = priv;
1005 
1006     mt7615_mcu_set_roc(phy, vif, NULL, 0);
1007 }
1008 
1009 void mt7615_roc_work(struct work_struct *work)
1010 {
1011     struct mt7615_phy *phy;
1012 
1013     phy = (struct mt7615_phy *)container_of(work, struct mt7615_phy,
1014                         roc_work);
1015 
1016     if (!test_and_clear_bit(MT76_STATE_ROC, &phy->mt76->state))
1017         return;
1018 
1019     mt7615_mutex_acquire(phy->dev);
1020     ieee80211_iterate_active_interfaces(phy->mt76->hw,
1021                         IEEE80211_IFACE_ITER_RESUME_ALL,
1022                         mt7615_roc_iter, phy);
1023     mt7615_mutex_release(phy->dev);
1024     ieee80211_remain_on_channel_expired(phy->mt76->hw);
1025 }
1026 
1027 void mt7615_roc_timer(struct timer_list *timer)
1028 {
1029     struct mt7615_phy *phy = from_timer(phy, timer, roc_timer);
1030 
1031     ieee80211_queue_work(phy->mt76->hw, &phy->roc_work);
1032 }
1033 
1034 void mt7615_scan_work(struct work_struct *work)
1035 {
1036     struct mt7615_phy *phy;
1037 
1038     phy = (struct mt7615_phy *)container_of(work, struct mt7615_phy,
1039                         scan_work.work);
1040 
1041     while (true) {
1042         struct mt7615_mcu_rxd *rxd;
1043         struct sk_buff *skb;
1044 
1045         spin_lock_bh(&phy->dev->mt76.lock);
1046         skb = __skb_dequeue(&phy->scan_event_list);
1047         spin_unlock_bh(&phy->dev->mt76.lock);
1048 
1049         if (!skb)
1050             break;
1051 
1052         rxd = (struct mt7615_mcu_rxd *)skb->data;
1053         if (rxd->eid == MCU_EVENT_SCHED_SCAN_DONE) {
1054             ieee80211_sched_scan_results(phy->mt76->hw);
1055         } else if (test_and_clear_bit(MT76_HW_SCANNING,
1056                           &phy->mt76->state)) {
1057             struct cfg80211_scan_info info = {
1058                 .aborted = false,
1059             };
1060 
1061             ieee80211_scan_completed(phy->mt76->hw, &info);
1062         }
1063         dev_kfree_skb(skb);
1064     }
1065 }
1066 
1067 static int
1068 mt7615_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1069            struct ieee80211_scan_request *req)
1070 {
1071     struct mt7615_dev *dev = mt7615_hw_dev(hw);
1072     struct mt76_phy *mphy = hw->priv;
1073     int err;
1074 
1075     /* fall-back to sw-scan */
1076     if (!mt7615_firmware_offload(dev))
1077         return 1;
1078 
1079     mt7615_mutex_acquire(dev);
1080     err = mt76_connac_mcu_hw_scan(mphy, vif, req);
1081     mt7615_mutex_release(dev);
1082 
1083     return err;
1084 }
1085 
1086 static void
1087 mt7615_cancel_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1088 {
1089     struct mt7615_dev *dev = mt7615_hw_dev(hw);
1090     struct mt76_phy *mphy = hw->priv;
1091 
1092     mt7615_mutex_acquire(dev);
1093     mt76_connac_mcu_cancel_hw_scan(mphy, vif);
1094     mt7615_mutex_release(dev);
1095 }
1096 
1097 static int
1098 mt7615_start_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1099             struct cfg80211_sched_scan_request *req,
1100             struct ieee80211_scan_ies *ies)
1101 {
1102     struct mt7615_dev *dev = mt7615_hw_dev(hw);
1103     struct mt76_phy *mphy = hw->priv;
1104     int err;
1105 
1106     if (!mt7615_firmware_offload(dev))
1107         return -EOPNOTSUPP;
1108 
1109     mt7615_mutex_acquire(dev);
1110 
1111     err = mt76_connac_mcu_sched_scan_req(mphy, vif, req);
1112     if (err < 0)
1113         goto out;
1114 
1115     err = mt76_connac_mcu_sched_scan_enable(mphy, vif, true);
1116 out:
1117     mt7615_mutex_release(dev);
1118 
1119     return err;
1120 }
1121 
1122 static int
1123 mt7615_stop_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1124 {
1125     struct mt7615_dev *dev = mt7615_hw_dev(hw);
1126     struct mt76_phy *mphy = hw->priv;
1127     int err;
1128 
1129     if (!mt7615_firmware_offload(dev))
1130         return -EOPNOTSUPP;
1131 
1132     mt7615_mutex_acquire(dev);
1133     err = mt76_connac_mcu_sched_scan_enable(mphy, vif, false);
1134     mt7615_mutex_release(dev);
1135 
1136     return err;
1137 }
1138 
1139 static int mt7615_remain_on_channel(struct ieee80211_hw *hw,
1140                     struct ieee80211_vif *vif,
1141                     struct ieee80211_channel *chan,
1142                     int duration,
1143                     enum ieee80211_roc_type type)
1144 {
1145     struct mt7615_phy *phy = mt7615_hw_phy(hw);
1146     int err;
1147 
1148     if (test_and_set_bit(MT76_STATE_ROC, &phy->mt76->state))
1149         return 0;
1150 
1151     mt7615_mutex_acquire(phy->dev);
1152 
1153     err = mt7615_mcu_set_roc(phy, vif, chan, duration);
1154     if (err < 0) {
1155         clear_bit(MT76_STATE_ROC, &phy->mt76->state);
1156         goto out;
1157     }
1158 
1159     if (!wait_event_timeout(phy->roc_wait, phy->roc_grant, HZ)) {
1160         mt7615_mcu_set_roc(phy, vif, NULL, 0);
1161         clear_bit(MT76_STATE_ROC, &phy->mt76->state);
1162         err = -ETIMEDOUT;
1163     }
1164 
1165 out:
1166     mt7615_mutex_release(phy->dev);
1167 
1168     return err;
1169 }
1170 
1171 static int mt7615_cancel_remain_on_channel(struct ieee80211_hw *hw,
1172                        struct ieee80211_vif *vif)
1173 {
1174     struct mt7615_phy *phy = mt7615_hw_phy(hw);
1175     int err;
1176 
1177     if (!test_and_clear_bit(MT76_STATE_ROC, &phy->mt76->state))
1178         return 0;
1179 
1180     del_timer_sync(&phy->roc_timer);
1181     cancel_work_sync(&phy->roc_work);
1182 
1183     mt7615_mutex_acquire(phy->dev);
1184     err = mt7615_mcu_set_roc(phy, vif, NULL, 0);
1185     mt7615_mutex_release(phy->dev);
1186 
1187     return err;
1188 }
1189 
1190 static void mt7615_sta_set_decap_offload(struct ieee80211_hw *hw,
1191                  struct ieee80211_vif *vif,
1192                  struct ieee80211_sta *sta,
1193                  bool enabled)
1194 {
1195     struct mt7615_dev *dev = mt7615_hw_dev(hw);
1196     struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
1197 
1198     if (enabled)
1199         set_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1200     else
1201         clear_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1202 
1203     mt7615_mcu_set_sta_decap_offload(dev, vif, sta);
1204 }
1205 
1206 #ifdef CONFIG_PM
1207 static int mt7615_suspend(struct ieee80211_hw *hw,
1208               struct cfg80211_wowlan *wowlan)
1209 {
1210     struct mt7615_phy *phy = mt7615_hw_phy(hw);
1211     struct mt7615_dev *dev = mt7615_hw_dev(hw);
1212     int err = 0;
1213 
1214     cancel_delayed_work_sync(&dev->pm.ps_work);
1215     mt76_connac_free_pending_tx_skbs(&dev->pm, NULL);
1216 
1217     mt7615_mutex_acquire(dev);
1218 
1219     clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
1220     cancel_delayed_work_sync(&phy->scan_work);
1221     cancel_delayed_work_sync(&phy->mt76->mac_work);
1222 
1223     set_bit(MT76_STATE_SUSPEND, &phy->mt76->state);
1224     ieee80211_iterate_active_interfaces(hw,
1225                         IEEE80211_IFACE_ITER_RESUME_ALL,
1226                         mt76_connac_mcu_set_suspend_iter,
1227                         phy->mt76);
1228 
1229     if (!mt7615_dev_running(dev))
1230         err = mt76_connac_mcu_set_hif_suspend(&dev->mt76, true);
1231 
1232     mt7615_mutex_release(dev);
1233 
1234     return err;
1235 }
1236 
1237 static int mt7615_resume(struct ieee80211_hw *hw)
1238 {
1239     struct mt7615_phy *phy = mt7615_hw_phy(hw);
1240     struct mt7615_dev *dev = mt7615_hw_dev(hw);
1241     unsigned long timeout;
1242     bool running;
1243 
1244     mt7615_mutex_acquire(dev);
1245 
1246     running = mt7615_dev_running(dev);
1247     set_bit(MT76_STATE_RUNNING, &phy->mt76->state);
1248 
1249     if (!running) {
1250         int err;
1251 
1252         err = mt76_connac_mcu_set_hif_suspend(&dev->mt76, false);
1253         if (err < 0) {
1254             mt7615_mutex_release(dev);
1255             return err;
1256         }
1257     }
1258 
1259     clear_bit(MT76_STATE_SUSPEND, &phy->mt76->state);
1260     ieee80211_iterate_active_interfaces(hw,
1261                         IEEE80211_IFACE_ITER_RESUME_ALL,
1262                         mt76_connac_mcu_set_suspend_iter,
1263                         phy->mt76);
1264 
1265     timeout = mt7615_get_macwork_timeout(dev);
1266     ieee80211_queue_delayed_work(hw, &phy->mt76->mac_work, timeout);
1267 
1268     mt7615_mutex_release(dev);
1269 
1270     return 0;
1271 }
1272 
1273 static void mt7615_set_wakeup(struct ieee80211_hw *hw, bool enabled)
1274 {
1275     struct mt7615_dev *dev = mt7615_hw_dev(hw);
1276     struct mt76_dev *mdev = &dev->mt76;
1277 
1278     device_set_wakeup_enable(mdev->dev, enabled);
1279 }
1280 
1281 static void mt7615_set_rekey_data(struct ieee80211_hw *hw,
1282                   struct ieee80211_vif *vif,
1283                   struct cfg80211_gtk_rekey_data *data)
1284 {
1285     struct mt7615_dev *dev = mt7615_hw_dev(hw);
1286 
1287     mt7615_mutex_acquire(dev);
1288     mt76_connac_mcu_update_gtk_rekey(hw, vif, data);
1289     mt7615_mutex_release(dev);
1290 }
1291 #endif /* CONFIG_PM */
1292 
1293 const struct ieee80211_ops mt7615_ops = {
1294     .tx = mt7615_tx,
1295     .start = mt7615_start,
1296     .stop = mt7615_stop,
1297     .add_interface = mt7615_add_interface,
1298     .remove_interface = mt7615_remove_interface,
1299     .config = mt7615_config,
1300     .conf_tx = mt7615_conf_tx,
1301     .configure_filter = mt7615_configure_filter,
1302     .bss_info_changed = mt7615_bss_info_changed,
1303     .sta_add = mt7615_sta_add,
1304     .sta_remove = mt7615_sta_remove,
1305     .sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
1306     .set_key = mt7615_set_key,
1307     .sta_set_decap_offload = mt7615_sta_set_decap_offload,
1308     .ampdu_action = mt7615_ampdu_action,
1309     .set_rts_threshold = mt7615_set_rts_threshold,
1310     .wake_tx_queue = mt76_wake_tx_queue,
1311     .sta_rate_tbl_update = mt7615_sta_rate_tbl_update,
1312     .sw_scan_start = mt76_sw_scan,
1313     .sw_scan_complete = mt76_sw_scan_complete,
1314     .release_buffered_frames = mt76_release_buffered_frames,
1315     .get_txpower = mt76_get_txpower,
1316     .channel_switch_beacon = mt7615_channel_switch_beacon,
1317     .get_stats = mt7615_get_stats,
1318     .get_tsf = mt7615_get_tsf,
1319     .set_tsf = mt7615_set_tsf,
1320     .offset_tsf = mt7615_offset_tsf,
1321     .get_survey = mt76_get_survey,
1322     .get_antenna = mt76_get_antenna,
1323     .set_antenna = mt7615_set_antenna,
1324     .set_coverage_class = mt7615_set_coverage_class,
1325     .hw_scan = mt7615_hw_scan,
1326     .cancel_hw_scan = mt7615_cancel_hw_scan,
1327     .sched_scan_start = mt7615_start_sched_scan,
1328     .sched_scan_stop = mt7615_stop_sched_scan,
1329     .remain_on_channel = mt7615_remain_on_channel,
1330     .cancel_remain_on_channel = mt7615_cancel_remain_on_channel,
1331     CFG80211_TESTMODE_CMD(mt76_testmode_cmd)
1332     CFG80211_TESTMODE_DUMP(mt76_testmode_dump)
1333 #ifdef CONFIG_PM
1334     .suspend = mt7615_suspend,
1335     .resume = mt7615_resume,
1336     .set_wakeup = mt7615_set_wakeup,
1337     .set_rekey_data = mt7615_set_rekey_data,
1338 #endif /* CONFIG_PM */
1339     .set_sar_specs = mt7615_set_sar_specs,
1340 };
1341 EXPORT_SYMBOL_GPL(mt7615_ops);
1342 
1343 MODULE_LICENSE("Dual BSD/GPL");