linux/drivers/net/wireless/mediatek/mt76/mt7615/usb.c
<<
>>
Prefs
   1// SPDX-License-Identifier: ISC
   2/* Copyright (C) 2019 MediaTek Inc.
   3 *
   4 * Author: Felix Fietkau <nbd@nbd.name>
   5 *         Lorenzo Bianconi <lorenzo@kernel.org>
   6 *         Sean Wang <sean.wang@mediatek.com>
   7 */
   8
   9#include <linux/kernel.h>
  10#include <linux/module.h>
  11#include <linux/usb.h>
  12
  13#include "mt7615.h"
  14#include "mac.h"
  15#include "mcu.h"
  16#include "regs.h"
  17
  18static const u32 mt7663u_reg_map[] = {
  19        [MT_TOP_CFG_BASE]       = 0x80020000,
  20        [MT_HW_BASE]            = 0x80000000,
  21        [MT_DMA_SHDL_BASE]      = 0x5000a000,
  22        [MT_HIF_BASE]           = 0x50000000,
  23        [MT_CSR_BASE]           = 0x40000000,
  24        [MT_EFUSE_ADDR_BASE]    = 0x78011000,
  25        [MT_TOP_MISC_BASE]      = 0x81020000,
  26        [MT_PLE_BASE]           = 0x82060000,
  27        [MT_PSE_BASE]           = 0x82068000,
  28        [MT_PHY_BASE]           = 0x82070000,
  29        [MT_WTBL_BASE_ADDR]     = 0x820e0000,
  30        [MT_CFG_BASE]           = 0x820f0000,
  31        [MT_AGG_BASE]           = 0x820f2000,
  32        [MT_ARB_BASE]           = 0x820f3000,
  33        [MT_TMAC_BASE]          = 0x820f4000,
  34        [MT_RMAC_BASE]          = 0x820f5000,
  35        [MT_DMA_BASE]           = 0x820f7000,
  36        [MT_PF_BASE]            = 0x820f8000,
  37        [MT_WTBL_BASE_ON]       = 0x820f9000,
  38        [MT_WTBL_BASE_OFF]      = 0x820f9800,
  39        [MT_LPON_BASE]          = 0x820fb000,
  40        [MT_MIB_BASE]           = 0x820fd000,
  41};
  42
  43static const struct usb_device_id mt7615_device_table[] = {
  44        { USB_DEVICE_AND_INTERFACE_INFO(0x0e8d, 0x7663, 0xff, 0xff, 0xff) },
  45        { },
  46};
  47
  48static void mt7663u_stop(struct ieee80211_hw *hw)
  49{
  50        struct mt7615_phy *phy = mt7615_hw_phy(hw);
  51        struct mt7615_dev *dev = hw->priv;
  52
  53        clear_bit(MT76_STATE_RUNNING, &dev->mphy.state);
  54        del_timer_sync(&phy->roc_timer);
  55        cancel_work_sync(&phy->roc_work);
  56        cancel_delayed_work_sync(&phy->scan_work);
  57        cancel_delayed_work_sync(&phy->mac_work);
  58        mt76u_stop_tx(&dev->mt76);
  59}
  60
  61static void mt7663u_cleanup(struct mt7615_dev *dev)
  62{
  63        clear_bit(MT76_STATE_INITIALIZED, &dev->mphy.state);
  64        mt76u_queues_deinit(&dev->mt76);
  65}
  66
  67static void
  68mt7663u_mac_write_txwi(struct mt7615_dev *dev, struct mt76_wcid *wcid,
  69                       enum mt76_txq_id qid, struct ieee80211_sta *sta,
  70                       struct sk_buff *skb)
  71{
  72        struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  73        struct ieee80211_key_conf *key = info->control.hw_key;
  74        __le32 *txwi;
  75        int pid;
  76
  77        if (!wcid)
  78                wcid = &dev->mt76.global_wcid;
  79
  80        pid = mt76_tx_status_skb_add(&dev->mt76, wcid, skb);
  81
  82        txwi = (__le32 *)(skb->data - MT_USB_TXD_SIZE);
  83        memset(txwi, 0, MT_USB_TXD_SIZE);
  84        mt7615_mac_write_txwi(dev, txwi, skb, wcid, sta, pid, key, false);
  85        skb_push(skb, MT_USB_TXD_SIZE);
  86}
  87
  88static int
  89__mt7663u_mac_set_rates(struct mt7615_dev *dev,
  90                        struct mt7615_wtbl_desc *wd)
  91{
  92        struct mt7615_rate_desc *rate = &wd->rate;
  93        struct mt7615_sta *sta = wd->sta;
  94        u32 w5, w27, addr, val;
  95
  96        lockdep_assert_held(&dev->mt76.mutex);
  97
  98        if (!sta)
  99                return -EINVAL;
 100
 101        if (!mt76_poll(dev, MT_WTBL_UPDATE, MT_WTBL_UPDATE_BUSY, 0, 5000))
 102                return -ETIMEDOUT;
 103
 104        addr = mt7615_mac_wtbl_addr(dev, sta->wcid.idx);
 105
 106        w27 = mt76_rr(dev, addr + 27 * 4);
 107        w27 &= ~MT_WTBL_W27_CC_BW_SEL;
 108        w27 |= FIELD_PREP(MT_WTBL_W27_CC_BW_SEL, rate->bw);
 109
 110        w5 = mt76_rr(dev, addr + 5 * 4);
 111        w5 &= ~(MT_WTBL_W5_BW_CAP | MT_WTBL_W5_CHANGE_BW_RATE |
 112                MT_WTBL_W5_MPDU_OK_COUNT |
 113                MT_WTBL_W5_MPDU_FAIL_COUNT |
 114                MT_WTBL_W5_RATE_IDX);
 115        w5 |= FIELD_PREP(MT_WTBL_W5_BW_CAP, rate->bw) |
 116              FIELD_PREP(MT_WTBL_W5_CHANGE_BW_RATE,
 117                         rate->bw_idx ? rate->bw_idx - 1 : 7);
 118
 119        mt76_wr(dev, MT_WTBL_RIUCR0, w5);
 120
 121        mt76_wr(dev, MT_WTBL_RIUCR1,
 122                FIELD_PREP(MT_WTBL_RIUCR1_RATE0, rate->probe_val) |
 123                FIELD_PREP(MT_WTBL_RIUCR1_RATE1, rate->val[0]) |
 124                FIELD_PREP(MT_WTBL_RIUCR1_RATE2_LO, rate->val[1]));
 125
 126        mt76_wr(dev, MT_WTBL_RIUCR2,
 127                FIELD_PREP(MT_WTBL_RIUCR2_RATE2_HI, rate->val[1] >> 8) |
 128                FIELD_PREP(MT_WTBL_RIUCR2_RATE3, rate->val[1]) |
 129                FIELD_PREP(MT_WTBL_RIUCR2_RATE4, rate->val[2]) |
 130                FIELD_PREP(MT_WTBL_RIUCR2_RATE5_LO, rate->val[2]));
 131
 132        mt76_wr(dev, MT_WTBL_RIUCR3,
 133                FIELD_PREP(MT_WTBL_RIUCR3_RATE5_HI, rate->val[2] >> 4) |
 134                FIELD_PREP(MT_WTBL_RIUCR3_RATE6, rate->val[3]) |
 135                FIELD_PREP(MT_WTBL_RIUCR3_RATE7, rate->val[3]));
 136
 137        mt76_wr(dev, MT_WTBL_UPDATE,
 138                FIELD_PREP(MT_WTBL_UPDATE_WLAN_IDX, sta->wcid.idx) |
 139                MT_WTBL_UPDATE_RATE_UPDATE |
 140                MT_WTBL_UPDATE_TX_COUNT_CLEAR);
 141
 142        mt76_wr(dev, addr + 27 * 4, w27);
 143
 144        mt76_set(dev, MT_LPON_T0CR, MT_LPON_T0CR_MODE); /* TSF read */
 145        val = mt76_rr(dev, MT_LPON_UTTR0);
 146        sta->rate_set_tsf = (val & ~BIT(0)) | rate->rateset;
 147
 148        if (!(sta->wcid.tx_info & MT_WCID_TX_INFO_SET))
 149                mt76_poll(dev, MT_WTBL_UPDATE, MT_WTBL_UPDATE_BUSY, 0, 5000);
 150
 151        sta->rate_count = 2 * MT7615_RATE_RETRY * sta->n_rates;
 152        sta->wcid.tx_info |= MT_WCID_TX_INFO_SET;
 153
 154        return 0;
 155}
 156
 157static int
 158__mt7663u_mac_set_key(struct mt7615_dev *dev,
 159                      struct mt7615_wtbl_desc *wd)
 160{
 161        struct mt7615_key_desc *key = &wd->key;
 162        struct mt7615_sta *sta = wd->sta;
 163        enum mt7615_cipher_type cipher;
 164        struct mt76_wcid *wcid;
 165        int err;
 166
 167        lockdep_assert_held(&dev->mt76.mutex);
 168
 169        if (!sta)
 170                return -EINVAL;
 171
 172        cipher = mt7615_mac_get_cipher(key->cipher);
 173        if (cipher == MT_CIPHER_NONE)
 174                return -EOPNOTSUPP;
 175
 176        wcid = &wd->sta->wcid;
 177
 178        mt7615_mac_wtbl_update_cipher(dev, wcid, cipher, key->cmd);
 179        err = mt7615_mac_wtbl_update_key(dev, wcid, key->key, key->keylen,
 180                                         cipher, key->cmd);
 181        if (err < 0)
 182                return err;
 183
 184        err = mt7615_mac_wtbl_update_pk(dev, wcid, cipher, key->keyidx,
 185                                        key->cmd);
 186        if (err < 0)
 187                return err;
 188
 189        if (key->cmd == SET_KEY)
 190                wcid->cipher |= BIT(cipher);
 191        else
 192                wcid->cipher &= ~BIT(cipher);
 193
 194        return 0;
 195}
 196
 197void mt7663u_wtbl_work(struct work_struct *work)
 198{
 199        struct mt7615_wtbl_desc *wd, *wd_next;
 200        struct mt7615_dev *dev;
 201
 202        dev = (struct mt7615_dev *)container_of(work, struct mt7615_dev,
 203                                                wtbl_work);
 204
 205        list_for_each_entry_safe(wd, wd_next, &dev->wd_head, node) {
 206                spin_lock_bh(&dev->mt76.lock);
 207                list_del(&wd->node);
 208                spin_unlock_bh(&dev->mt76.lock);
 209
 210                mutex_lock(&dev->mt76.mutex);
 211                switch (wd->type) {
 212                case MT7615_WTBL_RATE_DESC:
 213                        __mt7663u_mac_set_rates(dev, wd);
 214                        break;
 215                case MT7615_WTBL_KEY_DESC:
 216                        __mt7663u_mac_set_key(dev, wd);
 217                        break;
 218                }
 219                mutex_unlock(&dev->mt76.mutex);
 220
 221                kfree(wd);
 222        }
 223}
 224
 225static void
 226mt7663u_tx_complete_skb(struct mt76_dev *mdev, enum mt76_txq_id qid,
 227                        struct mt76_queue_entry *e)
 228{
 229        skb_pull(e->skb, MT_USB_HDR_SIZE + MT_USB_TXD_SIZE);
 230        mt76_tx_complete_skb(mdev, e->skb);
 231}
 232
 233static int
 234mt7663u_tx_prepare_skb(struct mt76_dev *mdev, void *txwi_ptr,
 235                       enum mt76_txq_id qid, struct mt76_wcid *wcid,
 236                       struct ieee80211_sta *sta,
 237                       struct mt76_tx_info *tx_info)
 238{
 239        struct mt7615_dev *dev = container_of(mdev, struct mt7615_dev, mt76);
 240        struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx_info->skb);
 241
 242        if (info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE) {
 243                struct mt7615_sta *msta;
 244
 245                msta = container_of(wcid, struct mt7615_sta, wcid);
 246                spin_lock_bh(&dev->mt76.lock);
 247                mt7615_mac_set_rates(&dev->phy, msta, &info->control.rates[0],
 248                                     msta->rates);
 249                msta->rate_probe = true;
 250                spin_unlock_bh(&dev->mt76.lock);
 251        }
 252        mt7663u_mac_write_txwi(dev, wcid, qid, sta, tx_info->skb);
 253
 254        return mt76u_skb_dma_info(tx_info->skb, tx_info->skb->len);
 255}
 256
 257static bool mt7663u_tx_status_data(struct mt76_dev *mdev, u8 *update)
 258{
 259        struct mt7615_dev *dev = container_of(mdev, struct mt7615_dev, mt76);
 260
 261        mutex_lock(&dev->mt76.mutex);
 262        mt7615_mac_sta_poll(dev);
 263        mutex_unlock(&dev->mt76.mutex);
 264
 265        return 0;
 266}
 267
 268static int mt7663u_probe(struct usb_interface *usb_intf,
 269                         const struct usb_device_id *id)
 270{
 271        static const struct mt76_driver_ops drv_ops = {
 272                .txwi_size = MT_USB_TXD_SIZE,
 273                .drv_flags = MT_DRV_RX_DMA_HDR | MT_DRV_HW_MGMT_TXQ,
 274                .tx_prepare_skb = mt7663u_tx_prepare_skb,
 275                .tx_complete_skb = mt7663u_tx_complete_skb,
 276                .tx_status_data = mt7663u_tx_status_data,
 277                .rx_skb = mt7615_queue_rx_skb,
 278                .sta_ps = mt7615_sta_ps,
 279                .sta_add = mt7615_mac_sta_add,
 280                .sta_remove = mt7615_mac_sta_remove,
 281                .update_survey = mt7615_update_channel,
 282        };
 283        struct usb_device *udev = interface_to_usbdev(usb_intf);
 284        struct ieee80211_ops *ops;
 285        struct mt7615_dev *dev;
 286        struct mt76_dev *mdev;
 287        int ret;
 288
 289        ops = devm_kmemdup(&usb_intf->dev, &mt7615_ops, sizeof(mt7615_ops),
 290                           GFP_KERNEL);
 291        if (!ops)
 292                return -ENOMEM;
 293
 294        ops->stop = mt7663u_stop;
 295
 296        mdev = mt76_alloc_device(&usb_intf->dev, sizeof(*dev), ops, &drv_ops);
 297        if (!mdev)
 298                return -ENOMEM;
 299
 300        dev = container_of(mdev, struct mt7615_dev, mt76);
 301        udev = usb_get_dev(udev);
 302        usb_reset_device(udev);
 303
 304        usb_set_intfdata(usb_intf, dev);
 305
 306        dev->reg_map = mt7663u_reg_map;
 307        dev->ops = ops;
 308        ret = mt76u_init(mdev, usb_intf, true);
 309        if (ret < 0)
 310                goto error;
 311
 312        mdev->rev = (mt76_rr(dev, MT_HW_CHIPID) << 16) |
 313                    (mt76_rr(dev, MT_HW_REV) & 0xff);
 314        dev_dbg(mdev->dev, "ASIC revision: %04x\n", mdev->rev);
 315
 316        if (mt76_poll_msec(dev, MT_CONN_ON_MISC, MT_TOP_MISC2_FW_PWR_ON,
 317                           FW_STATE_PWR_ON << 1, 500)) {
 318                dev_dbg(dev->mt76.dev, "Usb device already powered on\n");
 319                set_bit(MT76_STATE_POWER_OFF, &dev->mphy.state);
 320                goto alloc_queues;
 321        }
 322
 323        ret = mt76u_vendor_request(&dev->mt76, MT_VEND_POWER_ON,
 324                                   USB_DIR_OUT | USB_TYPE_VENDOR,
 325                                   0x0, 0x1, NULL, 0);
 326        if (ret)
 327                goto error;
 328
 329        if (!mt76_poll_msec(dev, MT_CONN_ON_MISC, MT_TOP_MISC2_FW_PWR_ON,
 330                            FW_STATE_PWR_ON << 1, 500)) {
 331                dev_err(dev->mt76.dev, "Timeout for power on\n");
 332                ret = -EIO;
 333                goto error;
 334        }
 335
 336alloc_queues:
 337        ret = mt76u_alloc_mcu_queue(&dev->mt76);
 338        if (ret)
 339                goto error_free_q;
 340
 341        ret = mt76u_alloc_queues(&dev->mt76);
 342        if (ret)
 343                goto error_free_q;
 344
 345        ret = mt7663u_register_device(dev);
 346        if (ret)
 347                goto error_free_q;
 348
 349        return 0;
 350
 351error_free_q:
 352        mt76u_queues_deinit(&dev->mt76);
 353error:
 354        mt76u_deinit(&dev->mt76);
 355        usb_set_intfdata(usb_intf, NULL);
 356        usb_put_dev(interface_to_usbdev(usb_intf));
 357
 358        ieee80211_free_hw(mdev->hw);
 359
 360        return ret;
 361}
 362
 363static void mt7663u_disconnect(struct usb_interface *usb_intf)
 364{
 365        struct mt7615_dev *dev = usb_get_intfdata(usb_intf);
 366
 367        if (!test_bit(MT76_STATE_INITIALIZED, &dev->mphy.state))
 368                return;
 369
 370        ieee80211_unregister_hw(dev->mt76.hw);
 371        mt7663u_cleanup(dev);
 372
 373        usb_set_intfdata(usb_intf, NULL);
 374        usb_put_dev(interface_to_usbdev(usb_intf));
 375
 376        mt76u_deinit(&dev->mt76);
 377        ieee80211_free_hw(dev->mt76.hw);
 378}
 379
 380#ifdef CONFIG_PM
 381static int mt7663u_suspend(struct usb_interface *intf, pm_message_t state)
 382{
 383        struct mt7615_dev *dev = usb_get_intfdata(intf);
 384
 385        if (!test_bit(MT76_STATE_SUSPEND, &dev->mphy.state) &&
 386            mt7615_firmware_offload(dev)) {
 387                int err;
 388
 389                err = mt7615_mcu_set_hif_suspend(dev, true);
 390                if (err < 0)
 391                        return err;
 392        }
 393
 394        mt76u_stop_rx(&dev->mt76);
 395
 396        mt76u_stop_tx(&dev->mt76);
 397        tasklet_kill(&dev->mt76.tx_tasklet);
 398
 399        return 0;
 400}
 401
 402static int mt7663u_resume(struct usb_interface *intf)
 403{
 404        struct mt7615_dev *dev = usb_get_intfdata(intf);
 405        int err;
 406
 407        err = mt76u_vendor_request(&dev->mt76, MT_VEND_FEATURE_SET,
 408                                   USB_DIR_OUT | USB_TYPE_VENDOR,
 409                                   0x5, 0x0, NULL, 0);
 410        if (err)
 411                return err;
 412
 413        err = mt76u_resume_rx(&dev->mt76);
 414        if (err < 0)
 415                return err;
 416
 417        if (!test_bit(MT76_STATE_SUSPEND, &dev->mphy.state) &&
 418            mt7615_firmware_offload(dev))
 419                err = mt7615_mcu_set_hif_suspend(dev, false);
 420
 421        return err;
 422}
 423#endif /* CONFIG_PM */
 424
 425MODULE_DEVICE_TABLE(usb, mt7615_device_table);
 426MODULE_FIRMWARE(MT7663_OFFLOAD_FIRMWARE_N9);
 427MODULE_FIRMWARE(MT7663_OFFLOAD_ROM_PATCH);
 428MODULE_FIRMWARE(MT7663_FIRMWARE_N9);
 429MODULE_FIRMWARE(MT7663_ROM_PATCH);
 430
 431static struct usb_driver mt7663u_driver = {
 432        .name           = KBUILD_MODNAME,
 433        .id_table       = mt7615_device_table,
 434        .probe          = mt7663u_probe,
 435        .disconnect     = mt7663u_disconnect,
 436#ifdef CONFIG_PM
 437        .suspend        = mt7663u_suspend,
 438        .resume         = mt7663u_resume,
 439        .reset_resume   = mt7663u_resume,
 440#endif /* CONFIG_PM */
 441        .soft_unbind    = 1,
 442        .disable_hub_initiated_lpm = 1,
 443};
 444module_usb_driver(mt7663u_driver);
 445
 446MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
 447MODULE_AUTHOR("Lorenzo Bianconi <lorenzo@kernel.org>");
 448MODULE_LICENSE("Dual BSD/GPL");
 449