linux/drivers/net/wireless/mediatek/mt76/mt7615/main.c
<<
>>
Prefs
   1// SPDX-License-Identifier: ISC
   2/* Copyright (C) 2019 MediaTek Inc.
   3 *
   4 * Author: Roy Luo <royluo@google.com>
   5 *         Ryder Lee <ryder.lee@mediatek.com>
   6 *         Felix Fietkau <nbd@nbd.name>
   7 *         Lorenzo Bianconi <lorenzo@kernel.org>
   8 */
   9
  10#include <linux/etherdevice.h>
  11#include <linux/module.h>
  12#include "mt7615.h"
  13#include "mcu.h"
  14
  15static bool mt7615_dev_running(struct mt7615_dev *dev)
  16{
  17        struct mt7615_phy *phy;
  18
  19        if (test_bit(MT76_STATE_RUNNING, &dev->mphy.state))
  20                return true;
  21
  22        phy = mt7615_ext_phy(dev);
  23
  24        return phy && test_bit(MT76_STATE_RUNNING, &phy->mt76->state);
  25}
  26
  27static int mt7615_start(struct ieee80211_hw *hw)
  28{
  29        struct mt7615_dev *dev = mt7615_hw_dev(hw);
  30        struct mt7615_phy *phy = mt7615_hw_phy(hw);
  31        unsigned long timeout;
  32        bool running;
  33        int ret;
  34
  35        if (!mt7615_wait_for_mcu_init(dev))
  36                return -EIO;
  37
  38        mt7615_mutex_acquire(dev);
  39
  40        running = mt7615_dev_running(dev);
  41
  42        if (!running) {
  43                ret = mt7615_mcu_set_pm(dev, 0, 0);
  44                if (ret)
  45                        goto out;
  46
  47                ret = mt76_connac_mcu_set_mac_enable(&dev->mt76, 0, true, false);
  48                if (ret)
  49                        goto out;
  50
  51                mt7615_mac_enable_nf(dev, 0);
  52        }
  53
  54        if (phy != &dev->phy) {
  55                ret = mt7615_mcu_set_pm(dev, 1, 0);
  56                if (ret)
  57                        goto out;
  58
  59                ret = mt76_connac_mcu_set_mac_enable(&dev->mt76, 1, true, false);
  60                if (ret)
  61                        goto out;
  62
  63                mt7615_mac_enable_nf(dev, 1);
  64        }
  65
  66        if (mt7615_firmware_offload(dev)) {
  67                ret = mt76_connac_mcu_set_channel_domain(phy->mt76);
  68                if (ret)
  69                        goto out;
  70
  71                ret = mt76_connac_mcu_set_rate_txpower(phy->mt76);
  72                if (ret)
  73                        goto out;
  74        }
  75
  76        ret = mt7615_mcu_set_chan_info(phy, MCU_EXT_CMD(SET_RX_PATH));
  77        if (ret)
  78                goto out;
  79
  80        set_bit(MT76_STATE_RUNNING, &phy->mt76->state);
  81
  82        timeout = mt7615_get_macwork_timeout(dev);
  83        ieee80211_queue_delayed_work(hw, &phy->mt76->mac_work, timeout);
  84
  85        if (!running)
  86                mt7615_mac_reset_counters(dev);
  87
  88out:
  89        mt7615_mutex_release(dev);
  90
  91        return ret;
  92}
  93
  94static void mt7615_stop(struct ieee80211_hw *hw)
  95{
  96        struct mt7615_dev *dev = mt7615_hw_dev(hw);
  97        struct mt7615_phy *phy = mt7615_hw_phy(hw);
  98
  99        cancel_delayed_work_sync(&phy->mt76->mac_work);
 100        del_timer_sync(&phy->roc_timer);
 101        cancel_work_sync(&phy->roc_work);
 102
 103        cancel_delayed_work_sync(&dev->pm.ps_work);
 104        cancel_work_sync(&dev->pm.wake_work);
 105
 106        mt76_connac_free_pending_tx_skbs(&dev->pm, NULL);
 107
 108        mt7615_mutex_acquire(dev);
 109
 110        mt76_testmode_reset(phy->mt76, true);
 111
 112        clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
 113        cancel_delayed_work_sync(&phy->scan_work);
 114
 115        if (phy != &dev->phy) {
 116                mt7615_mcu_set_pm(dev, 1, 1);
 117                mt76_connac_mcu_set_mac_enable(&dev->mt76, 1, false, false);
 118        }
 119
 120        if (!mt7615_dev_running(dev)) {
 121                mt7615_mcu_set_pm(dev, 0, 1);
 122                mt76_connac_mcu_set_mac_enable(&dev->mt76, 0, false, false);
 123        }
 124
 125        mt7615_mutex_release(dev);
 126}
 127
 128static inline int get_free_idx(u32 mask, u8 start, u8 end)
 129{
 130        return ffs(~mask & GENMASK(end, start));
 131}
 132
 133static int get_omac_idx(enum nl80211_iftype type, u64 mask)
 134{
 135        int i;
 136
 137        switch (type) {
 138        case NL80211_IFTYPE_STATION:
 139                /* prefer hw bssid slot 1-3 */
 140                i = get_free_idx(mask, HW_BSSID_1, HW_BSSID_3);
 141                if (i)
 142                        return i - 1;
 143
 144                /* next, try to find a free repeater entry for the sta */
 145                i = get_free_idx(mask >> REPEATER_BSSID_START, 0,
 146                                 REPEATER_BSSID_MAX - REPEATER_BSSID_START);
 147                if (i)
 148                        return i + 32 - 1;
 149
 150                i = get_free_idx(mask, EXT_BSSID_1, EXT_BSSID_MAX);
 151                if (i)
 152                        return i - 1;
 153
 154                if (~mask & BIT(HW_BSSID_0))
 155                        return HW_BSSID_0;
 156
 157                break;
 158        case NL80211_IFTYPE_ADHOC:
 159        case NL80211_IFTYPE_MESH_POINT:
 160        case NL80211_IFTYPE_MONITOR:
 161        case NL80211_IFTYPE_AP:
 162                /* ap uses hw bssid 0 and ext bssid */
 163                if (~mask & BIT(HW_BSSID_0))
 164                        return HW_BSSID_0;
 165
 166                i = get_free_idx(mask, EXT_BSSID_1, EXT_BSSID_MAX);
 167                if (i)
 168                        return i - 1;
 169
 170                break;
 171        default:
 172                WARN_ON(1);
 173                break;
 174        }
 175
 176        return -1;
 177}
 178
 179static int mt7615_add_interface(struct ieee80211_hw *hw,
 180                                struct ieee80211_vif *vif)
 181{
 182        struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
 183        struct mt7615_dev *dev = mt7615_hw_dev(hw);
 184        struct mt7615_phy *phy = mt7615_hw_phy(hw);
 185        struct mt76_txq *mtxq;
 186        bool ext_phy = phy != &dev->phy;
 187        int idx, ret = 0;
 188
 189        mt7615_mutex_acquire(dev);
 190
 191        mt76_testmode_reset(phy->mt76, true);
 192
 193        if (vif->type == NL80211_IFTYPE_MONITOR &&
 194            is_zero_ether_addr(vif->addr))
 195                phy->monitor_vif = vif;
 196
 197        mvif->mt76.idx = __ffs64(~dev->mt76.vif_mask);
 198        if (mvif->mt76.idx >= MT7615_MAX_INTERFACES) {
 199                ret = -ENOSPC;
 200                goto out;
 201        }
 202
 203        idx = get_omac_idx(vif->type, dev->omac_mask);
 204        if (idx < 0) {
 205                ret = -ENOSPC;
 206                goto out;
 207        }
 208        mvif->mt76.omac_idx = idx;
 209
 210        mvif->mt76.band_idx = ext_phy;
 211        mvif->mt76.wmm_idx = vif->type != NL80211_IFTYPE_AP;
 212        if (ext_phy)
 213                mvif->mt76.wmm_idx += 2;
 214
 215        dev->mt76.vif_mask |= BIT_ULL(mvif->mt76.idx);
 216        dev->omac_mask |= BIT_ULL(mvif->mt76.omac_idx);
 217        phy->omac_mask |= BIT_ULL(mvif->mt76.omac_idx);
 218
 219        ret = mt7615_mcu_set_dbdc(dev);
 220        if (ret)
 221                goto out;
 222
 223        idx = MT7615_WTBL_RESERVED - mvif->mt76.idx;
 224
 225        INIT_LIST_HEAD(&mvif->sta.poll_list);
 226        mvif->sta.wcid.idx = idx;
 227        mvif->sta.wcid.ext_phy = mvif->mt76.band_idx;
 228        mvif->sta.wcid.hw_key_idx = -1;
 229        mt76_packet_id_init(&mvif->sta.wcid);
 230
 231        mt7615_mac_wtbl_update(dev, idx,
 232                               MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
 233
 234        rcu_assign_pointer(dev->mt76.wcid[idx], &mvif->sta.wcid);
 235        if (vif->txq) {
 236                mtxq = (struct mt76_txq *)vif->txq->drv_priv;
 237                mtxq->wcid = idx;
 238        }
 239
 240        ret = mt7615_mcu_add_dev_info(phy, vif, true);
 241out:
 242        mt7615_mutex_release(dev);
 243
 244        return ret;
 245}
 246
 247static void mt7615_remove_interface(struct ieee80211_hw *hw,
 248                                    struct ieee80211_vif *vif)
 249{
 250        struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
 251        struct mt7615_sta *msta = &mvif->sta;
 252        struct mt7615_dev *dev = mt7615_hw_dev(hw);
 253        struct mt7615_phy *phy = mt7615_hw_phy(hw);
 254        int idx = msta->wcid.idx;
 255
 256        mt7615_mutex_acquire(dev);
 257
 258        mt7615_mcu_add_bss_info(phy, vif, NULL, false);
 259        mt7615_mcu_sta_add(phy, vif, NULL, false);
 260
 261        mt76_testmode_reset(phy->mt76, true);
 262        if (vif == phy->monitor_vif)
 263            phy->monitor_vif = NULL;
 264
 265        mt76_connac_free_pending_tx_skbs(&dev->pm, &msta->wcid);
 266
 267        mt7615_mcu_add_dev_info(phy, vif, false);
 268
 269        rcu_assign_pointer(dev->mt76.wcid[idx], NULL);
 270
 271        dev->mt76.vif_mask &= ~BIT_ULL(mvif->mt76.idx);
 272        dev->omac_mask &= ~BIT_ULL(mvif->mt76.omac_idx);
 273        phy->omac_mask &= ~BIT_ULL(mvif->mt76.omac_idx);
 274
 275        mt7615_mutex_release(dev);
 276
 277        spin_lock_bh(&dev->sta_poll_lock);
 278        if (!list_empty(&msta->poll_list))
 279                list_del_init(&msta->poll_list);
 280        spin_unlock_bh(&dev->sta_poll_lock);
 281
 282        mt76_packet_id_flush(&dev->mt76, &mvif->sta.wcid);
 283}
 284
 285static void mt7615_init_dfs_state(struct mt7615_phy *phy)
 286{
 287        struct mt76_phy *mphy = phy->mt76;
 288        struct ieee80211_hw *hw = mphy->hw;
 289        struct cfg80211_chan_def *chandef = &hw->conf.chandef;
 290
 291        if (hw->conf.flags & IEEE80211_CONF_OFFCHANNEL)
 292                return;
 293
 294        if (!(chandef->chan->flags & IEEE80211_CHAN_RADAR) &&
 295            !(mphy->chandef.chan->flags & IEEE80211_CHAN_RADAR))
 296                return;
 297
 298        if (mphy->chandef.chan->center_freq == chandef->chan->center_freq &&
 299            mphy->chandef.width == chandef->width)
 300                return;
 301
 302        phy->dfs_state = -1;
 303}
 304
 305int mt7615_set_channel(struct mt7615_phy *phy)
 306{
 307        struct mt7615_dev *dev = phy->dev;
 308        bool ext_phy = phy != &dev->phy;
 309        int ret;
 310
 311        cancel_delayed_work_sync(&phy->mt76->mac_work);
 312
 313        mt7615_mutex_acquire(dev);
 314
 315        set_bit(MT76_RESET, &phy->mt76->state);
 316
 317        mt7615_init_dfs_state(phy);
 318        mt76_set_channel(phy->mt76);
 319
 320        if (is_mt7615(&dev->mt76) && dev->flash_eeprom) {
 321                ret = mt7615_mcu_apply_rx_dcoc(phy);
 322                if (ret)
 323                        goto out;
 324
 325                ret = mt7615_mcu_apply_tx_dpd(phy);
 326                if (ret)
 327                        goto out;
 328        }
 329
 330        ret = mt7615_mcu_set_chan_info(phy, MCU_EXT_CMD(CHANNEL_SWITCH));
 331        if (ret)
 332                goto out;
 333
 334        mt7615_mac_set_timing(phy);
 335        ret = mt7615_dfs_init_radar_detector(phy);
 336        if (ret)
 337                goto out;
 338
 339        mt7615_mac_cca_stats_reset(phy);
 340        ret = mt7615_mcu_set_sku_en(phy, true);
 341        if (ret)
 342                goto out;
 343
 344        mt7615_mac_reset_counters(dev);
 345        phy->noise = 0;
 346        phy->chfreq = mt76_rr(dev, MT_CHFREQ(ext_phy));
 347
 348out:
 349        clear_bit(MT76_RESET, &phy->mt76->state);
 350
 351        mt7615_mutex_release(dev);
 352
 353        mt76_worker_schedule(&dev->mt76.tx_worker);
 354        if (!mt76_testmode_enabled(phy->mt76)) {
 355                unsigned long timeout = mt7615_get_macwork_timeout(dev);
 356
 357                ieee80211_queue_delayed_work(phy->mt76->hw,
 358                                             &phy->mt76->mac_work, timeout);
 359        }
 360
 361        return ret;
 362}
 363
 364static int mt7615_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
 365                          struct ieee80211_vif *vif, struct ieee80211_sta *sta,
 366                          struct ieee80211_key_conf *key)
 367{
 368        struct mt7615_dev *dev = mt7615_hw_dev(hw);
 369        struct mt7615_phy *phy = mt7615_hw_phy(hw);
 370        struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
 371        struct mt7615_sta *msta = sta ? (struct mt7615_sta *)sta->drv_priv :
 372                                  &mvif->sta;
 373        struct mt76_wcid *wcid = &msta->wcid;
 374        int idx = key->keyidx, err = 0;
 375        u8 *wcid_keyidx = &wcid->hw_key_idx;
 376
 377        /* The hardware does not support per-STA RX GTK, fallback
 378         * to software mode for these.
 379         */
 380        if ((vif->type == NL80211_IFTYPE_ADHOC ||
 381             vif->type == NL80211_IFTYPE_MESH_POINT) &&
 382            (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
 383             key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
 384            !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
 385                return -EOPNOTSUPP;
 386
 387        /* fall back to sw encryption for unsupported ciphers */
 388        switch (key->cipher) {
 389        case WLAN_CIPHER_SUITE_AES_CMAC:
 390                wcid_keyidx = &wcid->hw_key_idx2;
 391                key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIE;
 392                break;
 393        case WLAN_CIPHER_SUITE_TKIP:
 394        case WLAN_CIPHER_SUITE_CCMP:
 395        case WLAN_CIPHER_SUITE_CCMP_256:
 396        case WLAN_CIPHER_SUITE_GCMP:
 397        case WLAN_CIPHER_SUITE_GCMP_256:
 398        case WLAN_CIPHER_SUITE_SMS4:
 399                break;
 400        case WLAN_CIPHER_SUITE_WEP40:
 401        case WLAN_CIPHER_SUITE_WEP104:
 402        default:
 403                return -EOPNOTSUPP;
 404        }
 405
 406        mt7615_mutex_acquire(dev);
 407
 408        if (cmd == SET_KEY && !sta && !mvif->mt76.cipher) {
 409                mvif->mt76.cipher = mt76_connac_mcu_get_cipher(key->cipher);
 410                mt7615_mcu_add_bss_info(phy, vif, NULL, true);
 411        }
 412
 413        if (cmd == SET_KEY)
 414                *wcid_keyidx = idx;
 415        else if (idx == *wcid_keyidx)
 416                *wcid_keyidx = -1;
 417        else
 418                goto out;
 419
 420        mt76_wcid_key_setup(&dev->mt76, wcid,
 421                            cmd == SET_KEY ? key : NULL);
 422
 423        if (mt76_is_mmio(&dev->mt76))
 424                err = mt7615_mac_wtbl_set_key(dev, wcid, key, cmd);
 425        else
 426                err = __mt7615_mac_wtbl_set_key(dev, wcid, key, cmd);
 427
 428out:
 429        mt7615_mutex_release(dev);
 430
 431        return err;
 432}
 433
 434static int mt7615_set_sar_specs(struct ieee80211_hw *hw,
 435                                const struct cfg80211_sar_specs *sar)
 436{
 437        struct mt7615_phy *phy = mt7615_hw_phy(hw);
 438        int err;
 439
 440        if (!cfg80211_chandef_valid(&phy->mt76->chandef))
 441                return -EINVAL;
 442
 443        err = mt76_init_sar_power(hw, sar);
 444        if (err)
 445                return err;
 446
 447        if (mt7615_firmware_offload(phy->dev))
 448                return mt76_connac_mcu_set_rate_txpower(phy->mt76);
 449
 450        ieee80211_stop_queues(hw);
 451        err = mt7615_set_channel(phy);
 452        ieee80211_wake_queues(hw);
 453
 454        return err;
 455}
 456
 457static int mt7615_config(struct ieee80211_hw *hw, u32 changed)
 458{
 459        struct mt7615_dev *dev = mt7615_hw_dev(hw);
 460        struct mt7615_phy *phy = mt7615_hw_phy(hw);
 461        bool band = phy != &dev->phy;
 462        int ret = 0;
 463
 464        if (changed & (IEEE80211_CONF_CHANGE_CHANNEL |
 465                       IEEE80211_CONF_CHANGE_POWER)) {
 466#ifdef CONFIG_NL80211_TESTMODE
 467                if (phy->mt76->test.state != MT76_TM_STATE_OFF) {
 468                        mt7615_mutex_acquire(dev);
 469                        mt76_testmode_reset(phy->mt76, false);
 470                        mt7615_mutex_release(dev);
 471                }
 472#endif
 473                ieee80211_stop_queues(hw);
 474                ret = mt7615_set_channel(phy);
 475                ieee80211_wake_queues(hw);
 476        }
 477
 478        mt7615_mutex_acquire(dev);
 479
 480        if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
 481                mt76_testmode_reset(phy->mt76, true);
 482
 483                if (!(hw->conf.flags & IEEE80211_CONF_MONITOR))
 484                        phy->rxfilter |= MT_WF_RFCR_DROP_OTHER_UC;
 485                else
 486                        phy->rxfilter &= ~MT_WF_RFCR_DROP_OTHER_UC;
 487
 488                mt76_wr(dev, MT_WF_RFCR(band), phy->rxfilter);
 489        }
 490
 491        mt7615_mutex_release(dev);
 492
 493        return ret;
 494}
 495
 496static int
 497mt7615_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif, u16 queue,
 498               const struct ieee80211_tx_queue_params *params)
 499{
 500        struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
 501        struct mt7615_dev *dev = mt7615_hw_dev(hw);
 502        int err;
 503
 504        mt7615_mutex_acquire(dev);
 505
 506        queue = mt7615_lmac_mapping(dev, queue);
 507        queue += mvif->wmm_idx * MT7615_MAX_WMM_SETS;
 508        err = mt7615_mcu_set_wmm(dev, queue, params);
 509
 510        mt7615_mutex_release(dev);
 511
 512        return err;
 513}
 514
 515static void mt7615_configure_filter(struct ieee80211_hw *hw,
 516                                    unsigned int changed_flags,
 517                                    unsigned int *total_flags,
 518                                    u64 multicast)
 519{
 520        struct mt7615_dev *dev = mt7615_hw_dev(hw);
 521        struct mt7615_phy *phy = mt7615_hw_phy(hw);
 522        bool band = phy != &dev->phy;
 523
 524        u32 ctl_flags = MT_WF_RFCR1_DROP_ACK |
 525                        MT_WF_RFCR1_DROP_BF_POLL |
 526                        MT_WF_RFCR1_DROP_BA |
 527                        MT_WF_RFCR1_DROP_CFEND |
 528                        MT_WF_RFCR1_DROP_CFACK;
 529        u32 flags = 0;
 530
 531        mt7615_mutex_acquire(dev);
 532
 533#define MT76_FILTER(_flag, _hw) do { \
 534                flags |= *total_flags & FIF_##_flag;                    \
 535                phy->rxfilter &= ~(_hw);                                \
 536                if (!mt76_testmode_enabled(phy->mt76))                  \
 537                        phy->rxfilter |= !(flags & FIF_##_flag) * (_hw);\
 538        } while (0)
 539
 540        phy->rxfilter &= ~(MT_WF_RFCR_DROP_OTHER_BSS |
 541                           MT_WF_RFCR_DROP_FRAME_REPORT |
 542                           MT_WF_RFCR_DROP_PROBEREQ |
 543                           MT_WF_RFCR_DROP_MCAST_FILTERED |
 544                           MT_WF_RFCR_DROP_MCAST |
 545                           MT_WF_RFCR_DROP_BCAST |
 546                           MT_WF_RFCR_DROP_DUPLICATE |
 547                           MT_WF_RFCR_DROP_A2_BSSID |
 548                           MT_WF_RFCR_DROP_UNWANTED_CTL |
 549                           MT_WF_RFCR_DROP_STBC_MULTI);
 550
 551        if (phy->n_beacon_vif || !mt7615_firmware_offload(dev))
 552                phy->rxfilter &= ~MT_WF_RFCR_DROP_OTHER_BEACON;
 553
 554        MT76_FILTER(OTHER_BSS, MT_WF_RFCR_DROP_OTHER_TIM |
 555                               MT_WF_RFCR_DROP_A3_MAC |
 556                               MT_WF_RFCR_DROP_A3_BSSID);
 557
 558        MT76_FILTER(FCSFAIL, MT_WF_RFCR_DROP_FCSFAIL);
 559
 560        MT76_FILTER(CONTROL, MT_WF_RFCR_DROP_CTS |
 561                             MT_WF_RFCR_DROP_RTS |
 562                             MT_WF_RFCR_DROP_CTL_RSV |
 563                             MT_WF_RFCR_DROP_NDPA);
 564
 565        *total_flags = flags;
 566        mt76_wr(dev, MT_WF_RFCR(band), phy->rxfilter);
 567
 568        if (*total_flags & FIF_CONTROL)
 569                mt76_clear(dev, MT_WF_RFCR1(band), ctl_flags);
 570        else
 571                mt76_set(dev, MT_WF_RFCR1(band), ctl_flags);
 572
 573        mt7615_mutex_release(dev);
 574}
 575
 576static void mt7615_bss_info_changed(struct ieee80211_hw *hw,
 577                                    struct ieee80211_vif *vif,
 578                                    struct ieee80211_bss_conf *info,
 579                                    u32 changed)
 580{
 581        struct mt7615_dev *dev = mt7615_hw_dev(hw);
 582        struct mt7615_phy *phy = mt7615_hw_phy(hw);
 583
 584        mt7615_mutex_acquire(dev);
 585
 586        if (changed & BSS_CHANGED_ERP_SLOT) {
 587                int slottime = info->use_short_slot ? 9 : 20;
 588
 589                if (slottime != phy->slottime) {
 590                        phy->slottime = slottime;
 591                        mt7615_mac_set_timing(phy);
 592                }
 593        }
 594
 595        if (changed & BSS_CHANGED_BEACON_ENABLED && info->enable_beacon) {
 596                mt7615_mcu_add_bss_info(phy, vif, NULL, true);
 597                mt7615_mcu_sta_add(phy, vif, NULL, true);
 598
 599                if (mt7615_firmware_offload(dev) && vif->p2p)
 600                        mt76_connac_mcu_set_p2p_oppps(hw, vif);
 601        }
 602
 603        if (changed & (BSS_CHANGED_BEACON |
 604                       BSS_CHANGED_BEACON_ENABLED))
 605                mt7615_mcu_add_beacon(dev, hw, vif, info->enable_beacon);
 606
 607        if (changed & BSS_CHANGED_PS)
 608                mt76_connac_mcu_set_vif_ps(&dev->mt76, vif);
 609
 610        if ((changed & BSS_CHANGED_ARP_FILTER) &&
 611            mt7615_firmware_offload(dev)) {
 612                struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
 613
 614                mt76_connac_mcu_update_arp_filter(&dev->mt76, &mvif->mt76,
 615                                                  info);
 616        }
 617
 618        if (changed & BSS_CHANGED_ASSOC)
 619                mt7615_mac_set_beacon_filter(phy, vif, info->assoc);
 620
 621        mt7615_mutex_release(dev);
 622}
 623
 624static void
 625mt7615_channel_switch_beacon(struct ieee80211_hw *hw,
 626                             struct ieee80211_vif *vif,
 627                             struct cfg80211_chan_def *chandef)
 628{
 629        struct mt7615_dev *dev = mt7615_hw_dev(hw);
 630
 631        mt7615_mutex_acquire(dev);
 632        mt7615_mcu_add_beacon(dev, hw, vif, true);
 633        mt7615_mutex_release(dev);
 634}
 635
 636int mt7615_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
 637                       struct ieee80211_sta *sta)
 638{
 639        struct mt7615_dev *dev = container_of(mdev, struct mt7615_dev, mt76);
 640        struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
 641        struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
 642        struct mt7615_phy *phy;
 643        int idx, err;
 644
 645        idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7615_WTBL_STA - 1);
 646        if (idx < 0)
 647                return -ENOSPC;
 648
 649        INIT_LIST_HEAD(&msta->poll_list);
 650        msta->vif = mvif;
 651        msta->wcid.sta = 1;
 652        msta->wcid.idx = idx;
 653        msta->wcid.ext_phy = mvif->mt76.band_idx;
 654
 655        phy = mvif->mt76.band_idx ? mt7615_ext_phy(dev) : &dev->phy;
 656        err = mt76_connac_pm_wake(phy->mt76, &dev->pm);
 657        if (err)
 658                return err;
 659
 660        if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls) {
 661                err = mt7615_mcu_add_bss_info(phy, vif, sta, true);
 662                if (err)
 663                        return err;
 664        }
 665
 666        mt7615_mac_wtbl_update(dev, idx,
 667                               MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
 668        err = mt7615_mcu_sta_add(&dev->phy, vif, sta, true);
 669        if (err)
 670                return err;
 671
 672        mt76_connac_power_save_sched(phy->mt76, &dev->pm);
 673
 674        return err;
 675}
 676EXPORT_SYMBOL_GPL(mt7615_mac_sta_add);
 677
 678void mt7615_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
 679                           struct ieee80211_sta *sta)
 680{
 681        struct mt7615_dev *dev = container_of(mdev, struct mt7615_dev, mt76);
 682        struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
 683        struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
 684        struct mt7615_phy *phy;
 685
 686        mt76_connac_free_pending_tx_skbs(&dev->pm, &msta->wcid);
 687
 688        phy = mvif->mt76.band_idx ? mt7615_ext_phy(dev) : &dev->phy;
 689        mt76_connac_pm_wake(phy->mt76, &dev->pm);
 690
 691        mt7615_mcu_sta_add(&dev->phy, vif, sta, false);
 692        mt7615_mac_wtbl_update(dev, msta->wcid.idx,
 693                               MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
 694        if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
 695                mt7615_mcu_add_bss_info(phy, vif, sta, false);
 696
 697        spin_lock_bh(&dev->sta_poll_lock);
 698        if (!list_empty(&msta->poll_list))
 699                list_del_init(&msta->poll_list);
 700        spin_unlock_bh(&dev->sta_poll_lock);
 701
 702        mt76_connac_power_save_sched(phy->mt76, &dev->pm);
 703}
 704EXPORT_SYMBOL_GPL(mt7615_mac_sta_remove);
 705
 706static void mt7615_sta_rate_tbl_update(struct ieee80211_hw *hw,
 707                                       struct ieee80211_vif *vif,
 708                                       struct ieee80211_sta *sta)
 709{
 710        struct mt7615_dev *dev = mt7615_hw_dev(hw);
 711        struct mt7615_phy *phy = mt7615_hw_phy(hw);
 712        struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
 713        struct ieee80211_sta_rates *sta_rates = rcu_dereference(sta->rates);
 714        int i;
 715
 716        if (!sta_rates)
 717                return;
 718
 719        spin_lock_bh(&dev->mt76.lock);
 720        for (i = 0; i < ARRAY_SIZE(msta->rates); i++) {
 721                msta->rates[i].idx = sta_rates->rate[i].idx;
 722                msta->rates[i].count = sta_rates->rate[i].count;
 723                msta->rates[i].flags = sta_rates->rate[i].flags;
 724
 725                if (msta->rates[i].idx < 0 || !msta->rates[i].count)
 726                        break;
 727        }
 728        msta->n_rates = i;
 729        if (mt76_connac_pm_ref(phy->mt76, &dev->pm)) {
 730                mt7615_mac_set_rates(phy, msta, NULL, msta->rates);
 731                mt76_connac_pm_unref(phy->mt76, &dev->pm);
 732        }
 733        spin_unlock_bh(&dev->mt76.lock);
 734}
 735
 736void mt7615_tx_worker(struct mt76_worker *w)
 737{
 738        struct mt7615_dev *dev = container_of(w, struct mt7615_dev,
 739                                              mt76.tx_worker);
 740
 741        if (!mt76_connac_pm_ref(&dev->mphy, &dev->pm)) {
 742                queue_work(dev->mt76.wq, &dev->pm.wake_work);
 743                return;
 744        }
 745
 746        mt76_tx_worker_run(&dev->mt76);
 747        mt76_connac_pm_unref(&dev->mphy, &dev->pm);
 748}
 749
 750static void mt7615_tx(struct ieee80211_hw *hw,
 751                      struct ieee80211_tx_control *control,
 752                      struct sk_buff *skb)
 753{
 754        struct mt7615_dev *dev = mt7615_hw_dev(hw);
 755        struct mt76_phy *mphy = hw->priv;
 756        struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
 757        struct ieee80211_vif *vif = info->control.vif;
 758        struct mt76_wcid *wcid = &dev->mt76.global_wcid;
 759        struct mt7615_sta *msta = NULL;
 760        int qid;
 761
 762        if (control->sta) {
 763                msta = (struct mt7615_sta *)control->sta->drv_priv;
 764                wcid = &msta->wcid;
 765        }
 766
 767        if (vif && !control->sta) {
 768                struct mt7615_vif *mvif;
 769
 770                mvif = (struct mt7615_vif *)vif->drv_priv;
 771                msta = &mvif->sta;
 772                wcid = &msta->wcid;
 773        }
 774
 775        if (mt76_connac_pm_ref(mphy, &dev->pm)) {
 776                mt76_tx(mphy, control->sta, wcid, skb);
 777                mt76_connac_pm_unref(mphy, &dev->pm);
 778                return;
 779        }
 780
 781        qid = skb_get_queue_mapping(skb);
 782        if (qid >= MT_TXQ_PSD) {
 783                qid = IEEE80211_AC_BE;
 784                skb_set_queue_mapping(skb, qid);
 785        }
 786
 787        mt76_connac_pm_queue_skb(hw, &dev->pm, wcid, skb);
 788}
 789
 790static int mt7615_set_rts_threshold(struct ieee80211_hw *hw, u32 val)
 791{
 792        struct mt7615_dev *dev = mt7615_hw_dev(hw);
 793        struct mt7615_phy *phy = mt7615_hw_phy(hw);
 794        int err, band = phy != &dev->phy;
 795
 796        mt7615_mutex_acquire(dev);
 797        err = mt76_connac_mcu_set_rts_thresh(&dev->mt76, val, band);
 798        mt7615_mutex_release(dev);
 799
 800        return err;
 801}
 802
 803static int
 804mt7615_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
 805                    struct ieee80211_ampdu_params *params)
 806{
 807        enum ieee80211_ampdu_mlme_action action = params->action;
 808        struct mt7615_dev *dev = mt7615_hw_dev(hw);
 809        struct ieee80211_sta *sta = params->sta;
 810        struct ieee80211_txq *txq = sta->txq[params->tid];
 811        struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
 812        u16 tid = params->tid;
 813        u16 ssn = params->ssn;
 814        struct mt76_txq *mtxq;
 815        int ret = 0;
 816
 817        if (!txq)
 818                return -EINVAL;
 819
 820        mtxq = (struct mt76_txq *)txq->drv_priv;
 821
 822        mt7615_mutex_acquire(dev);
 823
 824        switch (action) {
 825        case IEEE80211_AMPDU_RX_START:
 826                mt76_rx_aggr_start(&dev->mt76, &msta->wcid, tid, ssn,
 827                                   params->buf_size);
 828                ret = mt7615_mcu_add_rx_ba(dev, params, true);
 829                break;
 830        case IEEE80211_AMPDU_RX_STOP:
 831                mt76_rx_aggr_stop(&dev->mt76, &msta->wcid, tid);
 832                ret = mt7615_mcu_add_rx_ba(dev, params, false);
 833                break;
 834        case IEEE80211_AMPDU_TX_OPERATIONAL:
 835                mtxq->aggr = true;
 836                mtxq->send_bar = false;
 837                ret = mt7615_mcu_add_tx_ba(dev, params, true);
 838                ssn = mt7615_mac_get_sta_tid_sn(dev, msta->wcid.idx, tid);
 839                ieee80211_send_bar(vif, sta->addr, tid,
 840                                   IEEE80211_SN_TO_SEQ(ssn));
 841                break;
 842        case IEEE80211_AMPDU_TX_STOP_FLUSH:
 843        case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
 844                mtxq->aggr = false;
 845                ret = mt7615_mcu_add_tx_ba(dev, params, false);
 846                break;
 847        case IEEE80211_AMPDU_TX_START:
 848                ssn = mt7615_mac_get_sta_tid_sn(dev, msta->wcid.idx, tid);
 849                params->ssn = ssn;
 850                ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
 851                break;
 852        case IEEE80211_AMPDU_TX_STOP_CONT:
 853                mtxq->aggr = false;
 854                ret = mt7615_mcu_add_tx_ba(dev, params, false);
 855                ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
 856                break;
 857        }
 858        mt7615_mutex_release(dev);
 859
 860        return ret;
 861}
 862
 863static int
 864mt7615_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
 865               struct ieee80211_sta *sta)
 866{
 867    return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NOTEXIST,
 868                          IEEE80211_STA_NONE);
 869}
 870
 871static int
 872mt7615_sta_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
 873                  struct ieee80211_sta *sta)
 874{
 875    return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NONE,
 876                          IEEE80211_STA_NOTEXIST);
 877}
 878
 879static int
 880mt7615_get_stats(struct ieee80211_hw *hw,
 881                 struct ieee80211_low_level_stats *stats)
 882{
 883        struct mt7615_phy *phy = mt7615_hw_phy(hw);
 884        struct mib_stats *mib = &phy->mib;
 885
 886        mt7615_mutex_acquire(phy->dev);
 887
 888        stats->dot11RTSSuccessCount = mib->rts_cnt;
 889        stats->dot11RTSFailureCount = mib->rts_retries_cnt;
 890        stats->dot11FCSErrorCount = mib->fcs_err_cnt;
 891        stats->dot11ACKFailureCount = mib->ack_fail_cnt;
 892
 893        mt7615_mutex_release(phy->dev);
 894
 895        return 0;
 896}
 897
 898static u64
 899mt7615_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
 900{
 901        struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
 902        struct mt7615_dev *dev = mt7615_hw_dev(hw);
 903        union {
 904                u64 t64;
 905                u32 t32[2];
 906        } tsf;
 907        u16 idx = mvif->mt76.omac_idx;
 908        u32 reg;
 909
 910        idx = idx > HW_BSSID_MAX ? HW_BSSID_0 : idx;
 911        reg = idx > 1 ? MT_LPON_TCR2(idx): MT_LPON_TCR0(idx);
 912
 913        mt7615_mutex_acquire(dev);
 914
 915        /* TSF read */
 916        mt76_rmw(dev, reg, MT_LPON_TCR_MODE, MT_LPON_TCR_READ);
 917        tsf.t32[0] = mt76_rr(dev, MT_LPON_UTTR0);
 918        tsf.t32[1] = mt76_rr(dev, MT_LPON_UTTR1);
 919
 920        mt7615_mutex_release(dev);
 921
 922        return tsf.t64;
 923}
 924
 925static void
 926mt7615_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
 927               u64 timestamp)
 928{
 929        struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
 930        struct mt7615_dev *dev = mt7615_hw_dev(hw);
 931        union {
 932                u64 t64;
 933                u32 t32[2];
 934        } tsf = { .t64 = timestamp, };
 935        u16 idx = mvif->mt76.omac_idx;
 936        u32 reg;
 937
 938        idx = idx > HW_BSSID_MAX ? HW_BSSID_0 : idx;
 939        reg = idx > 1 ? MT_LPON_TCR2(idx): MT_LPON_TCR0(idx);
 940
 941        mt7615_mutex_acquire(dev);
 942
 943        mt76_wr(dev, MT_LPON_UTTR0, tsf.t32[0]);
 944        mt76_wr(dev, MT_LPON_UTTR1, tsf.t32[1]);
 945        /* TSF software overwrite */
 946        mt76_rmw(dev, reg, MT_LPON_TCR_MODE, MT_LPON_TCR_WRITE);
 947
 948        mt7615_mutex_release(dev);
 949}
 950
 951static void
 952mt7615_offset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
 953                  s64 timestamp)
 954{
 955        struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
 956        struct mt7615_dev *dev = mt7615_hw_dev(hw);
 957        union {
 958                u64 t64;
 959                u32 t32[2];
 960        } tsf = { .t64 = timestamp, };
 961        u16 idx = mvif->mt76.omac_idx;
 962        u32 reg;
 963
 964        idx = idx > HW_BSSID_MAX ? HW_BSSID_0 : idx;
 965        reg = idx > 1 ? MT_LPON_TCR2(idx): MT_LPON_TCR0(idx);
 966
 967        mt7615_mutex_acquire(dev);
 968
 969        mt76_wr(dev, MT_LPON_UTTR0, tsf.t32[0]);
 970        mt76_wr(dev, MT_LPON_UTTR1, tsf.t32[1]);
 971        /* TSF software adjust*/
 972        mt76_rmw(dev, reg, MT_LPON_TCR_MODE, MT_LPON_TCR_ADJUST);
 973
 974        mt7615_mutex_release(dev);
 975}
 976
 977static void
 978mt7615_set_coverage_class(struct ieee80211_hw *hw, s16 coverage_class)
 979{
 980        struct mt7615_phy *phy = mt7615_hw_phy(hw);
 981        struct mt7615_dev *dev = phy->dev;
 982
 983        mt7615_mutex_acquire(dev);
 984        phy->coverage_class = max_t(s16, coverage_class, 0);
 985        mt7615_mac_set_timing(phy);
 986        mt7615_mutex_release(dev);
 987}
 988
 989static int
 990mt7615_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
 991{
 992        struct mt7615_dev *dev = mt7615_hw_dev(hw);
 993        struct mt7615_phy *phy = mt7615_hw_phy(hw);
 994        int max_nss = hweight8(hw->wiphy->available_antennas_tx);
 995        bool ext_phy = phy != &dev->phy;
 996
 997        if (!tx_ant || tx_ant != rx_ant || ffs(tx_ant) > max_nss)
 998                return -EINVAL;
 999
1000        if ((BIT(hweight8(tx_ant)) - 1) != tx_ant)
1001                tx_ant = BIT(ffs(tx_ant) - 1) - 1;
1002
1003        mt7615_mutex_acquire(dev);
1004
1005        phy->mt76->antenna_mask = tx_ant;
1006        if (ext_phy) {
1007                if (dev->chainmask == 0xf)
1008                        tx_ant <<= 2;
1009                else
1010                        tx_ant <<= 1;
1011        }
1012        phy->mt76->chainmask = tx_ant;
1013
1014        mt76_set_stream_caps(phy->mt76, true);
1015
1016        mt7615_mutex_release(dev);
1017
1018        return 0;
1019}
1020
1021static void mt7615_roc_iter(void *priv, u8 *mac,
1022                            struct ieee80211_vif *vif)
1023{
1024        struct mt7615_phy *phy = priv;
1025
1026        mt7615_mcu_set_roc(phy, vif, NULL, 0);
1027}
1028
1029void mt7615_roc_work(struct work_struct *work)
1030{
1031        struct mt7615_phy *phy;
1032
1033        phy = (struct mt7615_phy *)container_of(work, struct mt7615_phy,
1034                                                roc_work);
1035
1036        if (!test_and_clear_bit(MT76_STATE_ROC, &phy->mt76->state))
1037                return;
1038
1039        mt7615_mutex_acquire(phy->dev);
1040        ieee80211_iterate_active_interfaces(phy->mt76->hw,
1041                                            IEEE80211_IFACE_ITER_RESUME_ALL,
1042                                            mt7615_roc_iter, phy);
1043        mt7615_mutex_release(phy->dev);
1044        ieee80211_remain_on_channel_expired(phy->mt76->hw);
1045}
1046
1047void mt7615_roc_timer(struct timer_list *timer)
1048{
1049        struct mt7615_phy *phy = from_timer(phy, timer, roc_timer);
1050
1051        ieee80211_queue_work(phy->mt76->hw, &phy->roc_work);
1052}
1053
1054void mt7615_scan_work(struct work_struct *work)
1055{
1056        struct mt7615_phy *phy;
1057
1058        phy = (struct mt7615_phy *)container_of(work, struct mt7615_phy,
1059                                                scan_work.work);
1060
1061        while (true) {
1062                struct mt7615_mcu_rxd *rxd;
1063                struct sk_buff *skb;
1064
1065                spin_lock_bh(&phy->dev->mt76.lock);
1066                skb = __skb_dequeue(&phy->scan_event_list);
1067                spin_unlock_bh(&phy->dev->mt76.lock);
1068
1069                if (!skb)
1070                        break;
1071
1072                rxd = (struct mt7615_mcu_rxd *)skb->data;
1073                if (rxd->eid == MCU_EVENT_SCHED_SCAN_DONE) {
1074                        ieee80211_sched_scan_results(phy->mt76->hw);
1075                } else if (test_and_clear_bit(MT76_HW_SCANNING,
1076                                              &phy->mt76->state)) {
1077                        struct cfg80211_scan_info info = {
1078                                .aborted = false,
1079                        };
1080
1081                        ieee80211_scan_completed(phy->mt76->hw, &info);
1082                }
1083                dev_kfree_skb(skb);
1084        }
1085}
1086
1087static int
1088mt7615_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1089               struct ieee80211_scan_request *req)
1090{
1091        struct mt7615_dev *dev = mt7615_hw_dev(hw);
1092        struct mt76_phy *mphy = hw->priv;
1093        int err;
1094
1095        /* fall-back to sw-scan */
1096        if (!mt7615_firmware_offload(dev))
1097                return 1;
1098
1099        mt7615_mutex_acquire(dev);
1100        err = mt76_connac_mcu_hw_scan(mphy, vif, req);
1101        mt7615_mutex_release(dev);
1102
1103        return err;
1104}
1105
1106static void
1107mt7615_cancel_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1108{
1109        struct mt7615_dev *dev = mt7615_hw_dev(hw);
1110        struct mt76_phy *mphy = hw->priv;
1111
1112        mt7615_mutex_acquire(dev);
1113        mt76_connac_mcu_cancel_hw_scan(mphy, vif);
1114        mt7615_mutex_release(dev);
1115}
1116
1117static int
1118mt7615_start_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1119                        struct cfg80211_sched_scan_request *req,
1120                        struct ieee80211_scan_ies *ies)
1121{
1122        struct mt7615_dev *dev = mt7615_hw_dev(hw);
1123        struct mt76_phy *mphy = hw->priv;
1124        int err;
1125
1126        if (!mt7615_firmware_offload(dev))
1127                return -EOPNOTSUPP;
1128
1129        mt7615_mutex_acquire(dev);
1130
1131        err = mt76_connac_mcu_sched_scan_req(mphy, vif, req);
1132        if (err < 0)
1133                goto out;
1134
1135        err = mt76_connac_mcu_sched_scan_enable(mphy, vif, true);
1136out:
1137        mt7615_mutex_release(dev);
1138
1139        return err;
1140}
1141
1142static int
1143mt7615_stop_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1144{
1145        struct mt7615_dev *dev = mt7615_hw_dev(hw);
1146        struct mt76_phy *mphy = hw->priv;
1147        int err;
1148
1149        if (!mt7615_firmware_offload(dev))
1150                return -EOPNOTSUPP;
1151
1152        mt7615_mutex_acquire(dev);
1153        err = mt76_connac_mcu_sched_scan_enable(mphy, vif, false);
1154        mt7615_mutex_release(dev);
1155
1156        return err;
1157}
1158
1159static int mt7615_remain_on_channel(struct ieee80211_hw *hw,
1160                                    struct ieee80211_vif *vif,
1161                                    struct ieee80211_channel *chan,
1162                                    int duration,
1163                                    enum ieee80211_roc_type type)
1164{
1165        struct mt7615_phy *phy = mt7615_hw_phy(hw);
1166        int err;
1167
1168        if (test_and_set_bit(MT76_STATE_ROC, &phy->mt76->state))
1169                return 0;
1170
1171        mt7615_mutex_acquire(phy->dev);
1172
1173        err = mt7615_mcu_set_roc(phy, vif, chan, duration);
1174        if (err < 0) {
1175                clear_bit(MT76_STATE_ROC, &phy->mt76->state);
1176                goto out;
1177        }
1178
1179        if (!wait_event_timeout(phy->roc_wait, phy->roc_grant, HZ)) {
1180                mt7615_mcu_set_roc(phy, vif, NULL, 0);
1181                clear_bit(MT76_STATE_ROC, &phy->mt76->state);
1182                err = -ETIMEDOUT;
1183        }
1184
1185out:
1186        mt7615_mutex_release(phy->dev);
1187
1188        return err;
1189}
1190
1191static int mt7615_cancel_remain_on_channel(struct ieee80211_hw *hw,
1192                                           struct ieee80211_vif *vif)
1193{
1194        struct mt7615_phy *phy = mt7615_hw_phy(hw);
1195        int err;
1196
1197        if (!test_and_clear_bit(MT76_STATE_ROC, &phy->mt76->state))
1198                return 0;
1199
1200        del_timer_sync(&phy->roc_timer);
1201        cancel_work_sync(&phy->roc_work);
1202
1203        mt7615_mutex_acquire(phy->dev);
1204        err = mt7615_mcu_set_roc(phy, vif, NULL, 0);
1205        mt7615_mutex_release(phy->dev);
1206
1207        return err;
1208}
1209
1210static void mt7615_sta_set_decap_offload(struct ieee80211_hw *hw,
1211                                 struct ieee80211_vif *vif,
1212                                 struct ieee80211_sta *sta,
1213                                 bool enabled)
1214{
1215        struct mt7615_dev *dev = mt7615_hw_dev(hw);
1216        struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
1217
1218        if (enabled)
1219                set_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1220        else
1221                clear_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1222
1223        mt7615_mcu_set_sta_decap_offload(dev, vif, sta);
1224}
1225
1226#ifdef CONFIG_PM
1227static int mt7615_suspend(struct ieee80211_hw *hw,
1228                          struct cfg80211_wowlan *wowlan)
1229{
1230        struct mt7615_phy *phy = mt7615_hw_phy(hw);
1231        struct mt7615_dev *dev = mt7615_hw_dev(hw);
1232        int err = 0;
1233
1234        cancel_delayed_work_sync(&dev->pm.ps_work);
1235        mt76_connac_free_pending_tx_skbs(&dev->pm, NULL);
1236
1237        mt7615_mutex_acquire(dev);
1238
1239        clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
1240        cancel_delayed_work_sync(&phy->scan_work);
1241        cancel_delayed_work_sync(&phy->mt76->mac_work);
1242
1243        set_bit(MT76_STATE_SUSPEND, &phy->mt76->state);
1244        ieee80211_iterate_active_interfaces(hw,
1245                                            IEEE80211_IFACE_ITER_RESUME_ALL,
1246                                            mt76_connac_mcu_set_suspend_iter,
1247                                            phy->mt76);
1248
1249        if (!mt7615_dev_running(dev))
1250                err = mt76_connac_mcu_set_hif_suspend(&dev->mt76, true);
1251
1252        mt7615_mutex_release(dev);
1253
1254        return err;
1255}
1256
1257static int mt7615_resume(struct ieee80211_hw *hw)
1258{
1259        struct mt7615_phy *phy = mt7615_hw_phy(hw);
1260        struct mt7615_dev *dev = mt7615_hw_dev(hw);
1261        unsigned long timeout;
1262        bool running;
1263
1264        mt7615_mutex_acquire(dev);
1265
1266        running = mt7615_dev_running(dev);
1267        set_bit(MT76_STATE_RUNNING, &phy->mt76->state);
1268
1269        if (!running) {
1270                int err;
1271
1272                err = mt76_connac_mcu_set_hif_suspend(&dev->mt76, false);
1273                if (err < 0) {
1274                        mt7615_mutex_release(dev);
1275                        return err;
1276                }
1277        }
1278
1279        clear_bit(MT76_STATE_SUSPEND, &phy->mt76->state);
1280        ieee80211_iterate_active_interfaces(hw,
1281                                            IEEE80211_IFACE_ITER_RESUME_ALL,
1282                                            mt76_connac_mcu_set_suspend_iter,
1283                                            phy->mt76);
1284
1285        timeout = mt7615_get_macwork_timeout(dev);
1286        ieee80211_queue_delayed_work(hw, &phy->mt76->mac_work, timeout);
1287
1288        mt7615_mutex_release(dev);
1289
1290        return 0;
1291}
1292
1293static void mt7615_set_wakeup(struct ieee80211_hw *hw, bool enabled)
1294{
1295        struct mt7615_dev *dev = mt7615_hw_dev(hw);
1296        struct mt76_dev *mdev = &dev->mt76;
1297
1298        device_set_wakeup_enable(mdev->dev, enabled);
1299}
1300
1301static void mt7615_set_rekey_data(struct ieee80211_hw *hw,
1302                                  struct ieee80211_vif *vif,
1303                                  struct cfg80211_gtk_rekey_data *data)
1304{
1305        struct mt7615_dev *dev = mt7615_hw_dev(hw);
1306
1307        mt7615_mutex_acquire(dev);
1308        mt76_connac_mcu_update_gtk_rekey(hw, vif, data);
1309        mt7615_mutex_release(dev);
1310}
1311#endif /* CONFIG_PM */
1312
1313const struct ieee80211_ops mt7615_ops = {
1314        .tx = mt7615_tx,
1315        .start = mt7615_start,
1316        .stop = mt7615_stop,
1317        .add_interface = mt7615_add_interface,
1318        .remove_interface = mt7615_remove_interface,
1319        .config = mt7615_config,
1320        .conf_tx = mt7615_conf_tx,
1321        .configure_filter = mt7615_configure_filter,
1322        .bss_info_changed = mt7615_bss_info_changed,
1323        .sta_add = mt7615_sta_add,
1324        .sta_remove = mt7615_sta_remove,
1325        .sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
1326        .set_key = mt7615_set_key,
1327        .sta_set_decap_offload = mt7615_sta_set_decap_offload,
1328        .ampdu_action = mt7615_ampdu_action,
1329        .set_rts_threshold = mt7615_set_rts_threshold,
1330        .wake_tx_queue = mt76_wake_tx_queue,
1331        .sta_rate_tbl_update = mt7615_sta_rate_tbl_update,
1332        .sw_scan_start = mt76_sw_scan,
1333        .sw_scan_complete = mt76_sw_scan_complete,
1334        .release_buffered_frames = mt76_release_buffered_frames,
1335        .get_txpower = mt76_get_txpower,
1336        .channel_switch_beacon = mt7615_channel_switch_beacon,
1337        .get_stats = mt7615_get_stats,
1338        .get_tsf = mt7615_get_tsf,
1339        .set_tsf = mt7615_set_tsf,
1340        .offset_tsf = mt7615_offset_tsf,
1341        .get_survey = mt76_get_survey,
1342        .get_antenna = mt76_get_antenna,
1343        .set_antenna = mt7615_set_antenna,
1344        .set_coverage_class = mt7615_set_coverage_class,
1345        .hw_scan = mt7615_hw_scan,
1346        .cancel_hw_scan = mt7615_cancel_hw_scan,
1347        .sched_scan_start = mt7615_start_sched_scan,
1348        .sched_scan_stop = mt7615_stop_sched_scan,
1349        .remain_on_channel = mt7615_remain_on_channel,
1350        .cancel_remain_on_channel = mt7615_cancel_remain_on_channel,
1351        CFG80211_TESTMODE_CMD(mt76_testmode_cmd)
1352        CFG80211_TESTMODE_DUMP(mt76_testmode_dump)
1353#ifdef CONFIG_PM
1354        .suspend = mt7615_suspend,
1355        .resume = mt7615_resume,
1356        .set_wakeup = mt7615_set_wakeup,
1357        .set_rekey_data = mt7615_set_rekey_data,
1358#endif /* CONFIG_PM */
1359        .set_sar_specs = mt7615_set_sar_specs,
1360};
1361EXPORT_SYMBOL_GPL(mt7615_ops);
1362
1363MODULE_LICENSE("Dual BSD/GPL");
1364