linux/drivers/net/wireless/mediatek/mt76/mac80211.c
<<
>>
Prefs
   1// SPDX-License-Identifier: ISC
   2/*
   3 * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
   4 */
   5#include <linux/sched.h>
   6#include <linux/of.h>
   7#include "mt76.h"
   8
   9#define CHAN2G(_idx, _freq) {                   \
  10        .band = NL80211_BAND_2GHZ,              \
  11        .center_freq = (_freq),                 \
  12        .hw_value = (_idx),                     \
  13        .max_power = 30,                        \
  14}
  15
  16#define CHAN5G(_idx, _freq) {                   \
  17        .band = NL80211_BAND_5GHZ,              \
  18        .center_freq = (_freq),                 \
  19        .hw_value = (_idx),                     \
  20        .max_power = 30,                        \
  21}
  22
  23static const struct ieee80211_channel mt76_channels_2ghz[] = {
  24        CHAN2G(1, 2412),
  25        CHAN2G(2, 2417),
  26        CHAN2G(3, 2422),
  27        CHAN2G(4, 2427),
  28        CHAN2G(5, 2432),
  29        CHAN2G(6, 2437),
  30        CHAN2G(7, 2442),
  31        CHAN2G(8, 2447),
  32        CHAN2G(9, 2452),
  33        CHAN2G(10, 2457),
  34        CHAN2G(11, 2462),
  35        CHAN2G(12, 2467),
  36        CHAN2G(13, 2472),
  37        CHAN2G(14, 2484),
  38};
  39
  40static const struct ieee80211_channel mt76_channels_5ghz[] = {
  41        CHAN5G(36, 5180),
  42        CHAN5G(40, 5200),
  43        CHAN5G(44, 5220),
  44        CHAN5G(48, 5240),
  45
  46        CHAN5G(52, 5260),
  47        CHAN5G(56, 5280),
  48        CHAN5G(60, 5300),
  49        CHAN5G(64, 5320),
  50
  51        CHAN5G(100, 5500),
  52        CHAN5G(104, 5520),
  53        CHAN5G(108, 5540),
  54        CHAN5G(112, 5560),
  55        CHAN5G(116, 5580),
  56        CHAN5G(120, 5600),
  57        CHAN5G(124, 5620),
  58        CHAN5G(128, 5640),
  59        CHAN5G(132, 5660),
  60        CHAN5G(136, 5680),
  61        CHAN5G(140, 5700),
  62        CHAN5G(144, 5720),
  63
  64        CHAN5G(149, 5745),
  65        CHAN5G(153, 5765),
  66        CHAN5G(157, 5785),
  67        CHAN5G(161, 5805),
  68        CHAN5G(165, 5825),
  69        CHAN5G(169, 5845),
  70        CHAN5G(173, 5865),
  71};
  72
  73static const struct ieee80211_tpt_blink mt76_tpt_blink[] = {
  74        { .throughput =   0 * 1024, .blink_time = 334 },
  75        { .throughput =   1 * 1024, .blink_time = 260 },
  76        { .throughput =   5 * 1024, .blink_time = 220 },
  77        { .throughput =  10 * 1024, .blink_time = 190 },
  78        { .throughput =  20 * 1024, .blink_time = 170 },
  79        { .throughput =  50 * 1024, .blink_time = 150 },
  80        { .throughput =  70 * 1024, .blink_time = 130 },
  81        { .throughput = 100 * 1024, .blink_time = 110 },
  82        { .throughput = 200 * 1024, .blink_time =  80 },
  83        { .throughput = 300 * 1024, .blink_time =  50 },
  84};
  85
  86struct ieee80211_rate mt76_rates[] = {
  87        CCK_RATE(0, 10),
  88        CCK_RATE(1, 20),
  89        CCK_RATE(2, 55),
  90        CCK_RATE(3, 110),
  91        OFDM_RATE(11, 60),
  92        OFDM_RATE(15, 90),
  93        OFDM_RATE(10, 120),
  94        OFDM_RATE(14, 180),
  95        OFDM_RATE(9,  240),
  96        OFDM_RATE(13, 360),
  97        OFDM_RATE(8,  480),
  98        OFDM_RATE(12, 540),
  99};
 100EXPORT_SYMBOL_GPL(mt76_rates);
 101
 102static int mt76_led_init(struct mt76_dev *dev)
 103{
 104        struct device_node *np = dev->dev->of_node;
 105        struct ieee80211_hw *hw = dev->hw;
 106        int led_pin;
 107
 108        if (!dev->led_cdev.brightness_set && !dev->led_cdev.blink_set)
 109                return 0;
 110
 111        snprintf(dev->led_name, sizeof(dev->led_name),
 112                 "mt76-%s", wiphy_name(hw->wiphy));
 113
 114        dev->led_cdev.name = dev->led_name;
 115        dev->led_cdev.default_trigger =
 116                ieee80211_create_tpt_led_trigger(hw,
 117                                        IEEE80211_TPT_LEDTRIG_FL_RADIO,
 118                                        mt76_tpt_blink,
 119                                        ARRAY_SIZE(mt76_tpt_blink));
 120
 121        np = of_get_child_by_name(np, "led");
 122        if (np) {
 123                if (!of_property_read_u32(np, "led-sources", &led_pin))
 124                        dev->led_pin = led_pin;
 125                dev->led_al = of_property_read_bool(np, "led-active-low");
 126        }
 127
 128        return led_classdev_register(dev->dev, &dev->led_cdev);
 129}
 130
 131static void mt76_led_cleanup(struct mt76_dev *dev)
 132{
 133        if (!dev->led_cdev.brightness_set && !dev->led_cdev.blink_set)
 134                return;
 135
 136        led_classdev_unregister(&dev->led_cdev);
 137}
 138
 139static void mt76_init_stream_cap(struct mt76_phy *phy,
 140                                 struct ieee80211_supported_band *sband,
 141                                 bool vht)
 142{
 143        struct ieee80211_sta_ht_cap *ht_cap = &sband->ht_cap;
 144        int i, nstream = hweight8(phy->antenna_mask);
 145        struct ieee80211_sta_vht_cap *vht_cap;
 146        u16 mcs_map = 0;
 147
 148        if (nstream > 1)
 149                ht_cap->cap |= IEEE80211_HT_CAP_TX_STBC;
 150        else
 151                ht_cap->cap &= ~IEEE80211_HT_CAP_TX_STBC;
 152
 153        for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
 154                ht_cap->mcs.rx_mask[i] = i < nstream ? 0xff : 0;
 155
 156        if (!vht)
 157                return;
 158
 159        vht_cap = &sband->vht_cap;
 160        if (nstream > 1)
 161                vht_cap->cap |= IEEE80211_VHT_CAP_TXSTBC;
 162        else
 163                vht_cap->cap &= ~IEEE80211_VHT_CAP_TXSTBC;
 164
 165        for (i = 0; i < 8; i++) {
 166                if (i < nstream)
 167                        mcs_map |= (IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2));
 168                else
 169                        mcs_map |=
 170                                (IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2));
 171        }
 172        vht_cap->vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
 173        vht_cap->vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
 174}
 175
 176void mt76_set_stream_caps(struct mt76_phy *phy, bool vht)
 177{
 178        if (phy->cap.has_2ghz)
 179                mt76_init_stream_cap(phy, &phy->sband_2g.sband, false);
 180        if (phy->cap.has_5ghz)
 181                mt76_init_stream_cap(phy, &phy->sband_5g.sband, vht);
 182}
 183EXPORT_SYMBOL_GPL(mt76_set_stream_caps);
 184
 185static int
 186mt76_init_sband(struct mt76_phy *phy, struct mt76_sband *msband,
 187                const struct ieee80211_channel *chan, int n_chan,
 188                struct ieee80211_rate *rates, int n_rates, bool vht)
 189{
 190        struct ieee80211_supported_band *sband = &msband->sband;
 191        struct ieee80211_sta_vht_cap *vht_cap;
 192        struct ieee80211_sta_ht_cap *ht_cap;
 193        struct mt76_dev *dev = phy->dev;
 194        void *chanlist;
 195        int size;
 196
 197        size = n_chan * sizeof(*chan);
 198        chanlist = devm_kmemdup(dev->dev, chan, size, GFP_KERNEL);
 199        if (!chanlist)
 200                return -ENOMEM;
 201
 202        msband->chan = devm_kcalloc(dev->dev, n_chan, sizeof(*msband->chan),
 203                                    GFP_KERNEL);
 204        if (!msband->chan)
 205                return -ENOMEM;
 206
 207        sband->channels = chanlist;
 208        sband->n_channels = n_chan;
 209        sband->bitrates = rates;
 210        sband->n_bitrates = n_rates;
 211
 212        ht_cap = &sband->ht_cap;
 213        ht_cap->ht_supported = true;
 214        ht_cap->cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
 215                       IEEE80211_HT_CAP_GRN_FLD |
 216                       IEEE80211_HT_CAP_SGI_20 |
 217                       IEEE80211_HT_CAP_SGI_40 |
 218                       (1 << IEEE80211_HT_CAP_RX_STBC_SHIFT);
 219
 220        ht_cap->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
 221        ht_cap->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
 222
 223        mt76_init_stream_cap(phy, sband, vht);
 224
 225        if (!vht)
 226                return 0;
 227
 228        vht_cap = &sband->vht_cap;
 229        vht_cap->vht_supported = true;
 230        vht_cap->cap |= IEEE80211_VHT_CAP_RXLDPC |
 231                        IEEE80211_VHT_CAP_RXSTBC_1 |
 232                        IEEE80211_VHT_CAP_SHORT_GI_80 |
 233                        IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN |
 234                        IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN |
 235                        (3 << IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT);
 236
 237        return 0;
 238}
 239
 240static int
 241mt76_init_sband_2g(struct mt76_phy *phy, struct ieee80211_rate *rates,
 242                   int n_rates)
 243{
 244        phy->hw->wiphy->bands[NL80211_BAND_2GHZ] = &phy->sband_2g.sband;
 245
 246        return mt76_init_sband(phy, &phy->sband_2g, mt76_channels_2ghz,
 247                               ARRAY_SIZE(mt76_channels_2ghz), rates,
 248                               n_rates, false);
 249}
 250
 251static int
 252mt76_init_sband_5g(struct mt76_phy *phy, struct ieee80211_rate *rates,
 253                   int n_rates, bool vht)
 254{
 255        phy->hw->wiphy->bands[NL80211_BAND_5GHZ] = &phy->sband_5g.sband;
 256
 257        return mt76_init_sband(phy, &phy->sband_5g, mt76_channels_5ghz,
 258                               ARRAY_SIZE(mt76_channels_5ghz), rates,
 259                               n_rates, vht);
 260}
 261
 262static void
 263mt76_check_sband(struct mt76_phy *phy, struct mt76_sband *msband,
 264                 enum nl80211_band band)
 265{
 266        struct ieee80211_supported_band *sband = &msband->sband;
 267        bool found = false;
 268        int i;
 269
 270        if (!sband)
 271                return;
 272
 273        for (i = 0; i < sband->n_channels; i++) {
 274                if (sband->channels[i].flags & IEEE80211_CHAN_DISABLED)
 275                        continue;
 276
 277                found = true;
 278                break;
 279        }
 280
 281        if (found) {
 282                phy->chandef.chan = &sband->channels[0];
 283                phy->chan_state = &msband->chan[0];
 284                return;
 285        }
 286
 287        sband->n_channels = 0;
 288        phy->hw->wiphy->bands[band] = NULL;
 289}
 290
 291static void
 292mt76_phy_init(struct mt76_phy *phy, struct ieee80211_hw *hw)
 293{
 294        struct mt76_dev *dev = phy->dev;
 295        struct wiphy *wiphy = hw->wiphy;
 296
 297        SET_IEEE80211_DEV(hw, dev->dev);
 298        SET_IEEE80211_PERM_ADDR(hw, phy->macaddr);
 299
 300        wiphy->features |= NL80211_FEATURE_ACTIVE_MONITOR;
 301        wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH |
 302                        WIPHY_FLAG_SUPPORTS_TDLS |
 303                        WIPHY_FLAG_AP_UAPSD;
 304
 305        wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
 306        wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_AIRTIME_FAIRNESS);
 307        wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_AQL);
 308
 309        wiphy->available_antennas_tx = dev->phy.antenna_mask;
 310        wiphy->available_antennas_rx = dev->phy.antenna_mask;
 311
 312        hw->txq_data_size = sizeof(struct mt76_txq);
 313        hw->uapsd_max_sp_len = IEEE80211_WMM_IE_STA_QOSINFO_SP_ALL;
 314
 315        if (!hw->max_tx_fragments)
 316                hw->max_tx_fragments = 16;
 317
 318        ieee80211_hw_set(hw, SIGNAL_DBM);
 319        ieee80211_hw_set(hw, AMPDU_AGGREGATION);
 320        ieee80211_hw_set(hw, SUPPORTS_RC_TABLE);
 321        ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
 322        ieee80211_hw_set(hw, SUPPORTS_CLONED_SKBS);
 323        ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU);
 324        ieee80211_hw_set(hw, SUPPORTS_REORDERING_BUFFER);
 325
 326        if (!(dev->drv->drv_flags & MT_DRV_AMSDU_OFFLOAD)) {
 327                ieee80211_hw_set(hw, TX_AMSDU);
 328                ieee80211_hw_set(hw, TX_FRAG_LIST);
 329        }
 330
 331        ieee80211_hw_set(hw, MFP_CAPABLE);
 332        ieee80211_hw_set(hw, AP_LINK_PS);
 333        ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
 334}
 335
 336struct mt76_phy *
 337mt76_alloc_phy(struct mt76_dev *dev, unsigned int size,
 338               const struct ieee80211_ops *ops)
 339{
 340        struct ieee80211_hw *hw;
 341        unsigned int phy_size;
 342        struct mt76_phy *phy;
 343
 344        phy_size = ALIGN(sizeof(*phy), 8);
 345        hw = ieee80211_alloc_hw(size + phy_size, ops);
 346        if (!hw)
 347                return NULL;
 348
 349        phy = hw->priv;
 350        phy->dev = dev;
 351        phy->hw = hw;
 352        phy->priv = hw->priv + phy_size;
 353
 354        hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
 355        hw->wiphy->interface_modes =
 356                BIT(NL80211_IFTYPE_STATION) |
 357                BIT(NL80211_IFTYPE_AP) |
 358#ifdef CONFIG_MAC80211_MESH
 359                BIT(NL80211_IFTYPE_MESH_POINT) |
 360#endif
 361                BIT(NL80211_IFTYPE_P2P_CLIENT) |
 362                BIT(NL80211_IFTYPE_P2P_GO) |
 363                BIT(NL80211_IFTYPE_ADHOC);
 364
 365        return phy;
 366}
 367EXPORT_SYMBOL_GPL(mt76_alloc_phy);
 368
 369int mt76_register_phy(struct mt76_phy *phy, bool vht,
 370                      struct ieee80211_rate *rates, int n_rates)
 371{
 372        int ret;
 373
 374        mt76_phy_init(phy, phy->hw);
 375
 376        if (phy->cap.has_2ghz) {
 377                ret = mt76_init_sband_2g(phy, rates, n_rates);
 378                if (ret)
 379                        return ret;
 380        }
 381
 382        if (phy->cap.has_5ghz) {
 383                ret = mt76_init_sband_5g(phy, rates + 4, n_rates - 4, vht);
 384                if (ret)
 385                        return ret;
 386        }
 387
 388        wiphy_read_of_freq_limits(phy->hw->wiphy);
 389        mt76_check_sband(phy, &phy->sband_2g, NL80211_BAND_2GHZ);
 390        mt76_check_sband(phy, &phy->sband_5g, NL80211_BAND_5GHZ);
 391
 392        ret = ieee80211_register_hw(phy->hw);
 393        if (ret)
 394                return ret;
 395
 396        phy->dev->phy2 = phy;
 397
 398        return 0;
 399}
 400EXPORT_SYMBOL_GPL(mt76_register_phy);
 401
 402void mt76_unregister_phy(struct mt76_phy *phy)
 403{
 404        struct mt76_dev *dev = phy->dev;
 405
 406        mt76_tx_status_check(dev, NULL, true);
 407        ieee80211_unregister_hw(phy->hw);
 408        dev->phy2 = NULL;
 409}
 410EXPORT_SYMBOL_GPL(mt76_unregister_phy);
 411
 412struct mt76_dev *
 413mt76_alloc_device(struct device *pdev, unsigned int size,
 414                  const struct ieee80211_ops *ops,
 415                  const struct mt76_driver_ops *drv_ops)
 416{
 417        struct ieee80211_hw *hw;
 418        struct mt76_phy *phy;
 419        struct mt76_dev *dev;
 420        int i;
 421
 422        hw = ieee80211_alloc_hw(size, ops);
 423        if (!hw)
 424                return NULL;
 425
 426        dev = hw->priv;
 427        dev->hw = hw;
 428        dev->dev = pdev;
 429        dev->drv = drv_ops;
 430
 431        phy = &dev->phy;
 432        phy->dev = dev;
 433        phy->hw = hw;
 434
 435        spin_lock_init(&dev->rx_lock);
 436        spin_lock_init(&dev->lock);
 437        spin_lock_init(&dev->cc_lock);
 438        mutex_init(&dev->mutex);
 439        init_waitqueue_head(&dev->tx_wait);
 440        skb_queue_head_init(&dev->status_list);
 441
 442        skb_queue_head_init(&dev->mcu.res_q);
 443        init_waitqueue_head(&dev->mcu.wait);
 444        mutex_init(&dev->mcu.mutex);
 445        dev->tx_worker.fn = mt76_tx_worker;
 446
 447        hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
 448        hw->wiphy->interface_modes =
 449                BIT(NL80211_IFTYPE_STATION) |
 450                BIT(NL80211_IFTYPE_AP) |
 451#ifdef CONFIG_MAC80211_MESH
 452                BIT(NL80211_IFTYPE_MESH_POINT) |
 453#endif
 454                BIT(NL80211_IFTYPE_P2P_CLIENT) |
 455                BIT(NL80211_IFTYPE_P2P_GO) |
 456                BIT(NL80211_IFTYPE_ADHOC);
 457
 458        spin_lock_init(&dev->token_lock);
 459        idr_init(&dev->token);
 460
 461        INIT_LIST_HEAD(&dev->txwi_cache);
 462
 463        for (i = 0; i < ARRAY_SIZE(dev->q_rx); i++)
 464                skb_queue_head_init(&dev->rx_skb[i]);
 465
 466        dev->wq = alloc_ordered_workqueue("mt76", 0);
 467        if (!dev->wq) {
 468                ieee80211_free_hw(hw);
 469                return NULL;
 470        }
 471
 472        return dev;
 473}
 474EXPORT_SYMBOL_GPL(mt76_alloc_device);
 475
 476int mt76_register_device(struct mt76_dev *dev, bool vht,
 477                         struct ieee80211_rate *rates, int n_rates)
 478{
 479        struct ieee80211_hw *hw = dev->hw;
 480        struct mt76_phy *phy = &dev->phy;
 481        int ret;
 482
 483        dev_set_drvdata(dev->dev, dev);
 484        mt76_phy_init(phy, hw);
 485
 486        if (phy->cap.has_2ghz) {
 487                ret = mt76_init_sband_2g(phy, rates, n_rates);
 488                if (ret)
 489                        return ret;
 490        }
 491
 492        if (phy->cap.has_5ghz) {
 493                ret = mt76_init_sband_5g(phy, rates + 4, n_rates - 4, vht);
 494                if (ret)
 495                        return ret;
 496        }
 497
 498        wiphy_read_of_freq_limits(hw->wiphy);
 499        mt76_check_sband(&dev->phy, &phy->sband_2g, NL80211_BAND_2GHZ);
 500        mt76_check_sband(&dev->phy, &phy->sband_5g, NL80211_BAND_5GHZ);
 501
 502        if (IS_ENABLED(CONFIG_MT76_LEDS)) {
 503                ret = mt76_led_init(dev);
 504                if (ret)
 505                        return ret;
 506        }
 507
 508        ret = ieee80211_register_hw(hw);
 509        if (ret)
 510                return ret;
 511
 512        WARN_ON(mt76_worker_setup(hw, &dev->tx_worker, NULL, "tx"));
 513        sched_set_fifo_low(dev->tx_worker.task);
 514
 515        return 0;
 516}
 517EXPORT_SYMBOL_GPL(mt76_register_device);
 518
 519void mt76_unregister_device(struct mt76_dev *dev)
 520{
 521        struct ieee80211_hw *hw = dev->hw;
 522
 523        if (IS_ENABLED(CONFIG_MT76_LEDS))
 524                mt76_led_cleanup(dev);
 525        mt76_tx_status_check(dev, NULL, true);
 526        ieee80211_unregister_hw(hw);
 527}
 528EXPORT_SYMBOL_GPL(mt76_unregister_device);
 529
 530void mt76_free_device(struct mt76_dev *dev)
 531{
 532        mt76_worker_teardown(&dev->tx_worker);
 533        if (dev->wq) {
 534                destroy_workqueue(dev->wq);
 535                dev->wq = NULL;
 536        }
 537        ieee80211_free_hw(dev->hw);
 538}
 539EXPORT_SYMBOL_GPL(mt76_free_device);
 540
 541static void mt76_rx_release_amsdu(struct mt76_phy *phy, enum mt76_rxq_id q)
 542{
 543        struct sk_buff *skb = phy->rx_amsdu[q].head;
 544        struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
 545        struct mt76_dev *dev = phy->dev;
 546
 547        phy->rx_amsdu[q].head = NULL;
 548        phy->rx_amsdu[q].tail = NULL;
 549
 550        /*
 551         * Validate if the amsdu has a proper first subframe.
 552         * A single MSDU can be parsed as A-MSDU when the unauthenticated A-MSDU
 553         * flag of the QoS header gets flipped. In such cases, the first
 554         * subframe has a LLC/SNAP header in the location of the destination
 555         * address.
 556         */
 557        if (skb_shinfo(skb)->frag_list) {
 558                int offset = 0;
 559
 560                if (!(status->flag & RX_FLAG_8023)) {
 561                        offset = ieee80211_get_hdrlen_from_skb(skb);
 562
 563                        if ((status->flag &
 564                             (RX_FLAG_DECRYPTED | RX_FLAG_IV_STRIPPED)) ==
 565                            RX_FLAG_DECRYPTED)
 566                                offset += 8;
 567                }
 568
 569                if (ether_addr_equal(skb->data + offset, rfc1042_header)) {
 570                        dev_kfree_skb(skb);
 571                        return;
 572                }
 573        }
 574        __skb_queue_tail(&dev->rx_skb[q], skb);
 575}
 576
 577static void mt76_rx_release_burst(struct mt76_phy *phy, enum mt76_rxq_id q,
 578                                  struct sk_buff *skb)
 579{
 580        struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
 581
 582        if (phy->rx_amsdu[q].head &&
 583            (!status->amsdu || status->first_amsdu ||
 584             status->seqno != phy->rx_amsdu[q].seqno))
 585                mt76_rx_release_amsdu(phy, q);
 586
 587        if (!phy->rx_amsdu[q].head) {
 588                phy->rx_amsdu[q].tail = &skb_shinfo(skb)->frag_list;
 589                phy->rx_amsdu[q].seqno = status->seqno;
 590                phy->rx_amsdu[q].head = skb;
 591        } else {
 592                *phy->rx_amsdu[q].tail = skb;
 593                phy->rx_amsdu[q].tail = &skb->next;
 594        }
 595
 596        if (!status->amsdu || status->last_amsdu)
 597                mt76_rx_release_amsdu(phy, q);
 598}
 599
 600void mt76_rx(struct mt76_dev *dev, enum mt76_rxq_id q, struct sk_buff *skb)
 601{
 602        struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
 603        struct mt76_phy *phy = mt76_dev_phy(dev, status->ext_phy);
 604
 605        if (!test_bit(MT76_STATE_RUNNING, &phy->state)) {
 606                dev_kfree_skb(skb);
 607                return;
 608        }
 609
 610#ifdef CONFIG_NL80211_TESTMODE
 611        if (phy->test.state == MT76_TM_STATE_RX_FRAMES) {
 612                phy->test.rx_stats.packets[q]++;
 613                if (status->flag & RX_FLAG_FAILED_FCS_CRC)
 614                        phy->test.rx_stats.fcs_error[q]++;
 615        }
 616#endif
 617
 618        mt76_rx_release_burst(phy, q, skb);
 619}
 620EXPORT_SYMBOL_GPL(mt76_rx);
 621
 622bool mt76_has_tx_pending(struct mt76_phy *phy)
 623{
 624        struct mt76_queue *q;
 625        int i;
 626
 627        for (i = 0; i < __MT_TXQ_MAX; i++) {
 628                q = phy->q_tx[i];
 629                if (q && q->queued)
 630                        return true;
 631        }
 632
 633        return false;
 634}
 635EXPORT_SYMBOL_GPL(mt76_has_tx_pending);
 636
 637static struct mt76_channel_state *
 638mt76_channel_state(struct mt76_phy *phy, struct ieee80211_channel *c)
 639{
 640        struct mt76_sband *msband;
 641        int idx;
 642
 643        if (c->band == NL80211_BAND_2GHZ)
 644                msband = &phy->sband_2g;
 645        else
 646                msband = &phy->sband_5g;
 647
 648        idx = c - &msband->sband.channels[0];
 649        return &msband->chan[idx];
 650}
 651
 652void mt76_update_survey_active_time(struct mt76_phy *phy, ktime_t time)
 653{
 654        struct mt76_channel_state *state = phy->chan_state;
 655
 656        state->cc_active += ktime_to_us(ktime_sub(time,
 657                                                  phy->survey_time));
 658        phy->survey_time = time;
 659}
 660EXPORT_SYMBOL_GPL(mt76_update_survey_active_time);
 661
 662void mt76_update_survey(struct mt76_phy *phy)
 663{
 664        struct mt76_dev *dev = phy->dev;
 665        ktime_t cur_time;
 666
 667        if (dev->drv->update_survey)
 668                dev->drv->update_survey(phy);
 669
 670        cur_time = ktime_get_boottime();
 671        mt76_update_survey_active_time(phy, cur_time);
 672
 673        if (dev->drv->drv_flags & MT_DRV_SW_RX_AIRTIME) {
 674                struct mt76_channel_state *state = phy->chan_state;
 675
 676                spin_lock_bh(&dev->cc_lock);
 677                state->cc_bss_rx += dev->cur_cc_bss_rx;
 678                dev->cur_cc_bss_rx = 0;
 679                spin_unlock_bh(&dev->cc_lock);
 680        }
 681}
 682EXPORT_SYMBOL_GPL(mt76_update_survey);
 683
 684void mt76_set_channel(struct mt76_phy *phy)
 685{
 686        struct mt76_dev *dev = phy->dev;
 687        struct ieee80211_hw *hw = phy->hw;
 688        struct cfg80211_chan_def *chandef = &hw->conf.chandef;
 689        bool offchannel = hw->conf.flags & IEEE80211_CONF_OFFCHANNEL;
 690        int timeout = HZ / 5;
 691
 692        wait_event_timeout(dev->tx_wait, !mt76_has_tx_pending(phy), timeout);
 693        mt76_update_survey(phy);
 694
 695        phy->chandef = *chandef;
 696        phy->chan_state = mt76_channel_state(phy, chandef->chan);
 697
 698        if (!offchannel)
 699                phy->main_chan = chandef->chan;
 700
 701        if (chandef->chan != phy->main_chan)
 702                memset(phy->chan_state, 0, sizeof(*phy->chan_state));
 703}
 704EXPORT_SYMBOL_GPL(mt76_set_channel);
 705
 706int mt76_get_survey(struct ieee80211_hw *hw, int idx,
 707                    struct survey_info *survey)
 708{
 709        struct mt76_phy *phy = hw->priv;
 710        struct mt76_dev *dev = phy->dev;
 711        struct mt76_sband *sband;
 712        struct ieee80211_channel *chan;
 713        struct mt76_channel_state *state;
 714        int ret = 0;
 715
 716        mutex_lock(&dev->mutex);
 717        if (idx == 0 && dev->drv->update_survey)
 718                mt76_update_survey(phy);
 719
 720        sband = &phy->sband_2g;
 721        if (idx >= sband->sband.n_channels) {
 722                idx -= sband->sband.n_channels;
 723                sband = &phy->sband_5g;
 724        }
 725
 726        if (idx >= sband->sband.n_channels) {
 727                ret = -ENOENT;
 728                goto out;
 729        }
 730
 731        chan = &sband->sband.channels[idx];
 732        state = mt76_channel_state(phy, chan);
 733
 734        memset(survey, 0, sizeof(*survey));
 735        survey->channel = chan;
 736        survey->filled = SURVEY_INFO_TIME | SURVEY_INFO_TIME_BUSY;
 737        survey->filled |= dev->drv->survey_flags;
 738        if (state->noise)
 739                survey->filled |= SURVEY_INFO_NOISE_DBM;
 740
 741        if (chan == phy->main_chan) {
 742                survey->filled |= SURVEY_INFO_IN_USE;
 743
 744                if (dev->drv->drv_flags & MT_DRV_SW_RX_AIRTIME)
 745                        survey->filled |= SURVEY_INFO_TIME_BSS_RX;
 746        }
 747
 748        survey->time_busy = div_u64(state->cc_busy, 1000);
 749        survey->time_rx = div_u64(state->cc_rx, 1000);
 750        survey->time = div_u64(state->cc_active, 1000);
 751        survey->noise = state->noise;
 752
 753        spin_lock_bh(&dev->cc_lock);
 754        survey->time_bss_rx = div_u64(state->cc_bss_rx, 1000);
 755        survey->time_tx = div_u64(state->cc_tx, 1000);
 756        spin_unlock_bh(&dev->cc_lock);
 757
 758out:
 759        mutex_unlock(&dev->mutex);
 760
 761        return ret;
 762}
 763EXPORT_SYMBOL_GPL(mt76_get_survey);
 764
 765void mt76_wcid_key_setup(struct mt76_dev *dev, struct mt76_wcid *wcid,
 766                         struct ieee80211_key_conf *key)
 767{
 768        struct ieee80211_key_seq seq;
 769        int i;
 770
 771        wcid->rx_check_pn = false;
 772
 773        if (!key)
 774                return;
 775
 776        if (key->cipher != WLAN_CIPHER_SUITE_CCMP)
 777                return;
 778
 779        wcid->rx_check_pn = true;
 780        for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
 781                ieee80211_get_key_rx_seq(key, i, &seq);
 782                memcpy(wcid->rx_key_pn[i], seq.ccmp.pn, sizeof(seq.ccmp.pn));
 783        }
 784}
 785EXPORT_SYMBOL(mt76_wcid_key_setup);
 786
 787static void
 788mt76_rx_convert(struct mt76_dev *dev, struct sk_buff *skb,
 789                struct ieee80211_hw **hw,
 790                struct ieee80211_sta **sta)
 791{
 792        struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
 793        struct mt76_rx_status mstat;
 794
 795        mstat = *((struct mt76_rx_status *)skb->cb);
 796        memset(status, 0, sizeof(*status));
 797
 798        status->flag = mstat.flag;
 799        status->freq = mstat.freq;
 800        status->enc_flags = mstat.enc_flags;
 801        status->encoding = mstat.encoding;
 802        status->bw = mstat.bw;
 803        status->he_ru = mstat.he_ru;
 804        status->he_gi = mstat.he_gi;
 805        status->he_dcm = mstat.he_dcm;
 806        status->rate_idx = mstat.rate_idx;
 807        status->nss = mstat.nss;
 808        status->band = mstat.band;
 809        status->signal = mstat.signal;
 810        status->chains = mstat.chains;
 811        status->ampdu_reference = mstat.ampdu_ref;
 812        status->device_timestamp = mstat.timestamp;
 813        status->mactime = mstat.timestamp;
 814
 815        BUILD_BUG_ON(sizeof(mstat) > sizeof(skb->cb));
 816        BUILD_BUG_ON(sizeof(status->chain_signal) !=
 817                     sizeof(mstat.chain_signal));
 818        memcpy(status->chain_signal, mstat.chain_signal,
 819               sizeof(mstat.chain_signal));
 820
 821        *sta = wcid_to_sta(mstat.wcid);
 822        *hw = mt76_phy_hw(dev, mstat.ext_phy);
 823}
 824
 825static int
 826mt76_check_ccmp_pn(struct sk_buff *skb)
 827{
 828        struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
 829        struct mt76_wcid *wcid = status->wcid;
 830        struct ieee80211_hdr *hdr;
 831        u8 tidno = status->qos_ctl & IEEE80211_QOS_CTL_TID_MASK;
 832        int ret;
 833
 834        if (!(status->flag & RX_FLAG_DECRYPTED))
 835                return 0;
 836
 837        if (!wcid || !wcid->rx_check_pn)
 838                return 0;
 839
 840        if (!(status->flag & RX_FLAG_IV_STRIPPED)) {
 841                /*
 842                 * Validate the first fragment both here and in mac80211
 843                 * All further fragments will be validated by mac80211 only.
 844                 */
 845                hdr = mt76_skb_get_hdr(skb);
 846                if (ieee80211_is_frag(hdr) &&
 847                    !ieee80211_is_first_frag(hdr->frame_control))
 848                        return 0;
 849        }
 850
 851        BUILD_BUG_ON(sizeof(status->iv) != sizeof(wcid->rx_key_pn[0]));
 852        ret = memcmp(status->iv, wcid->rx_key_pn[tidno],
 853                     sizeof(status->iv));
 854        if (ret <= 0)
 855                return -EINVAL; /* replay */
 856
 857        memcpy(wcid->rx_key_pn[tidno], status->iv, sizeof(status->iv));
 858
 859        if (status->flag & RX_FLAG_IV_STRIPPED)
 860                status->flag |= RX_FLAG_PN_VALIDATED;
 861
 862        return 0;
 863}
 864
 865static void
 866mt76_airtime_report(struct mt76_dev *dev, struct mt76_rx_status *status,
 867                    int len)
 868{
 869        struct mt76_wcid *wcid = status->wcid;
 870        struct ieee80211_rx_status info = {
 871                .enc_flags = status->enc_flags,
 872                .rate_idx = status->rate_idx,
 873                .encoding = status->encoding,
 874                .band = status->band,
 875                .nss = status->nss,
 876                .bw = status->bw,
 877        };
 878        struct ieee80211_sta *sta;
 879        u32 airtime;
 880        u8 tidno = status->qos_ctl & IEEE80211_QOS_CTL_TID_MASK;
 881
 882        airtime = ieee80211_calc_rx_airtime(dev->hw, &info, len);
 883        spin_lock(&dev->cc_lock);
 884        dev->cur_cc_bss_rx += airtime;
 885        spin_unlock(&dev->cc_lock);
 886
 887        if (!wcid || !wcid->sta)
 888                return;
 889
 890        sta = container_of((void *)wcid, struct ieee80211_sta, drv_priv);
 891        ieee80211_sta_register_airtime(sta, tidno, 0, airtime);
 892}
 893
 894static void
 895mt76_airtime_flush_ampdu(struct mt76_dev *dev)
 896{
 897        struct mt76_wcid *wcid;
 898        int wcid_idx;
 899
 900        if (!dev->rx_ampdu_len)
 901                return;
 902
 903        wcid_idx = dev->rx_ampdu_status.wcid_idx;
 904        if (wcid_idx < ARRAY_SIZE(dev->wcid))
 905                wcid = rcu_dereference(dev->wcid[wcid_idx]);
 906        else
 907                wcid = NULL;
 908        dev->rx_ampdu_status.wcid = wcid;
 909
 910        mt76_airtime_report(dev, &dev->rx_ampdu_status, dev->rx_ampdu_len);
 911
 912        dev->rx_ampdu_len = 0;
 913        dev->rx_ampdu_ref = 0;
 914}
 915
 916static void
 917mt76_airtime_check(struct mt76_dev *dev, struct sk_buff *skb)
 918{
 919        struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
 920        struct mt76_wcid *wcid = status->wcid;
 921
 922        if (!(dev->drv->drv_flags & MT_DRV_SW_RX_AIRTIME))
 923                return;
 924
 925        if (!wcid || !wcid->sta) {
 926                struct ieee80211_hdr *hdr = mt76_skb_get_hdr(skb);
 927
 928                if (status->flag & RX_FLAG_8023)
 929                        return;
 930
 931                if (!ether_addr_equal(hdr->addr1, dev->phy.macaddr))
 932                        return;
 933
 934                wcid = NULL;
 935        }
 936
 937        if (!(status->flag & RX_FLAG_AMPDU_DETAILS) ||
 938            status->ampdu_ref != dev->rx_ampdu_ref)
 939                mt76_airtime_flush_ampdu(dev);
 940
 941        if (status->flag & RX_FLAG_AMPDU_DETAILS) {
 942                if (!dev->rx_ampdu_len ||
 943                    status->ampdu_ref != dev->rx_ampdu_ref) {
 944                        dev->rx_ampdu_status = *status;
 945                        dev->rx_ampdu_status.wcid_idx = wcid ? wcid->idx : 0xff;
 946                        dev->rx_ampdu_ref = status->ampdu_ref;
 947                }
 948
 949                dev->rx_ampdu_len += skb->len;
 950                return;
 951        }
 952
 953        mt76_airtime_report(dev, status, skb->len);
 954}
 955
 956static void
 957mt76_check_sta(struct mt76_dev *dev, struct sk_buff *skb)
 958{
 959        struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
 960        struct ieee80211_hdr *hdr = mt76_skb_get_hdr(skb);
 961        struct ieee80211_sta *sta;
 962        struct ieee80211_hw *hw;
 963        struct mt76_wcid *wcid = status->wcid;
 964        u8 tidno = status->qos_ctl & IEEE80211_QOS_CTL_TID_MASK;
 965        bool ps;
 966
 967        hw = mt76_phy_hw(dev, status->ext_phy);
 968        if (ieee80211_is_pspoll(hdr->frame_control) && !wcid &&
 969            !(status->flag & RX_FLAG_8023)) {
 970                sta = ieee80211_find_sta_by_ifaddr(hw, hdr->addr2, NULL);
 971                if (sta)
 972                        wcid = status->wcid = (struct mt76_wcid *)sta->drv_priv;
 973        }
 974
 975        mt76_airtime_check(dev, skb);
 976
 977        if (!wcid || !wcid->sta)
 978                return;
 979
 980        sta = container_of((void *)wcid, struct ieee80211_sta, drv_priv);
 981
 982        if (status->signal <= 0)
 983                ewma_signal_add(&wcid->rssi, -status->signal);
 984
 985        wcid->inactive_count = 0;
 986
 987        if (status->flag & RX_FLAG_8023)
 988                return;
 989
 990        if (!test_bit(MT_WCID_FLAG_CHECK_PS, &wcid->flags))
 991                return;
 992
 993        if (ieee80211_is_pspoll(hdr->frame_control)) {
 994                ieee80211_sta_pspoll(sta);
 995                return;
 996        }
 997
 998        if (ieee80211_has_morefrags(hdr->frame_control) ||
 999            !(ieee80211_is_mgmt(hdr->frame_control) ||
1000              ieee80211_is_data(hdr->frame_control)))
1001                return;
1002
1003        ps = ieee80211_has_pm(hdr->frame_control);
1004
1005        if (ps && (ieee80211_is_data_qos(hdr->frame_control) ||
1006                   ieee80211_is_qos_nullfunc(hdr->frame_control)))
1007                ieee80211_sta_uapsd_trigger(sta, tidno);
1008
1009        if (!!test_bit(MT_WCID_FLAG_PS, &wcid->flags) == ps)
1010                return;
1011
1012        if (ps)
1013                set_bit(MT_WCID_FLAG_PS, &wcid->flags);
1014        else
1015                clear_bit(MT_WCID_FLAG_PS, &wcid->flags);
1016
1017        dev->drv->sta_ps(dev, sta, ps);
1018        ieee80211_sta_ps_transition(sta, ps);
1019}
1020
1021void mt76_rx_complete(struct mt76_dev *dev, struct sk_buff_head *frames,
1022                      struct napi_struct *napi)
1023{
1024        struct ieee80211_sta *sta;
1025        struct ieee80211_hw *hw;
1026        struct sk_buff *skb, *tmp;
1027        LIST_HEAD(list);
1028
1029        spin_lock(&dev->rx_lock);
1030        while ((skb = __skb_dequeue(frames)) != NULL) {
1031                struct sk_buff *nskb = skb_shinfo(skb)->frag_list;
1032
1033                if (mt76_check_ccmp_pn(skb)) {
1034                        dev_kfree_skb(skb);
1035                        continue;
1036                }
1037
1038                skb_shinfo(skb)->frag_list = NULL;
1039                mt76_rx_convert(dev, skb, &hw, &sta);
1040                ieee80211_rx_list(hw, sta, skb, &list);
1041
1042                /* subsequent amsdu frames */
1043                while (nskb) {
1044                        skb = nskb;
1045                        nskb = nskb->next;
1046                        skb->next = NULL;
1047
1048                        mt76_rx_convert(dev, skb, &hw, &sta);
1049                        ieee80211_rx_list(hw, sta, skb, &list);
1050                }
1051        }
1052        spin_unlock(&dev->rx_lock);
1053
1054        if (!napi) {
1055                netif_receive_skb_list(&list);
1056                return;
1057        }
1058
1059        list_for_each_entry_safe(skb, tmp, &list, list) {
1060                skb_list_del_init(skb);
1061                napi_gro_receive(napi, skb);
1062        }
1063}
1064
1065void mt76_rx_poll_complete(struct mt76_dev *dev, enum mt76_rxq_id q,
1066                           struct napi_struct *napi)
1067{
1068        struct sk_buff_head frames;
1069        struct sk_buff *skb;
1070
1071        __skb_queue_head_init(&frames);
1072
1073        while ((skb = __skb_dequeue(&dev->rx_skb[q])) != NULL) {
1074                mt76_check_sta(dev, skb);
1075                mt76_rx_aggr_reorder(skb, &frames);
1076        }
1077
1078        mt76_rx_complete(dev, &frames, napi);
1079}
1080EXPORT_SYMBOL_GPL(mt76_rx_poll_complete);
1081
1082static int
1083mt76_sta_add(struct mt76_dev *dev, struct ieee80211_vif *vif,
1084             struct ieee80211_sta *sta, bool ext_phy)
1085{
1086        struct mt76_wcid *wcid = (struct mt76_wcid *)sta->drv_priv;
1087        int ret;
1088        int i;
1089
1090        mutex_lock(&dev->mutex);
1091
1092        ret = dev->drv->sta_add(dev, vif, sta);
1093        if (ret)
1094                goto out;
1095
1096        for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
1097                struct mt76_txq *mtxq;
1098
1099                if (!sta->txq[i])
1100                        continue;
1101
1102                mtxq = (struct mt76_txq *)sta->txq[i]->drv_priv;
1103                mtxq->wcid = wcid;
1104        }
1105
1106        ewma_signal_init(&wcid->rssi);
1107        if (ext_phy)
1108                mt76_wcid_mask_set(dev->wcid_phy_mask, wcid->idx);
1109        wcid->ext_phy = ext_phy;
1110        rcu_assign_pointer(dev->wcid[wcid->idx], wcid);
1111
1112out:
1113        mutex_unlock(&dev->mutex);
1114
1115        return ret;
1116}
1117
1118void __mt76_sta_remove(struct mt76_dev *dev, struct ieee80211_vif *vif,
1119                       struct ieee80211_sta *sta)
1120{
1121        struct mt76_wcid *wcid = (struct mt76_wcid *)sta->drv_priv;
1122        int i, idx = wcid->idx;
1123
1124        for (i = 0; i < ARRAY_SIZE(wcid->aggr); i++)
1125                mt76_rx_aggr_stop(dev, wcid, i);
1126
1127        if (dev->drv->sta_remove)
1128                dev->drv->sta_remove(dev, vif, sta);
1129
1130        mt76_tx_status_check(dev, wcid, true);
1131        mt76_wcid_mask_clear(dev->wcid_mask, idx);
1132        mt76_wcid_mask_clear(dev->wcid_phy_mask, idx);
1133}
1134EXPORT_SYMBOL_GPL(__mt76_sta_remove);
1135
1136static void
1137mt76_sta_remove(struct mt76_dev *dev, struct ieee80211_vif *vif,
1138                struct ieee80211_sta *sta)
1139{
1140        mutex_lock(&dev->mutex);
1141        __mt76_sta_remove(dev, vif, sta);
1142        mutex_unlock(&dev->mutex);
1143}
1144
1145int mt76_sta_state(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1146                   struct ieee80211_sta *sta,
1147                   enum ieee80211_sta_state old_state,
1148                   enum ieee80211_sta_state new_state)
1149{
1150        struct mt76_phy *phy = hw->priv;
1151        struct mt76_dev *dev = phy->dev;
1152        bool ext_phy = phy != &dev->phy;
1153
1154        if (old_state == IEEE80211_STA_NOTEXIST &&
1155            new_state == IEEE80211_STA_NONE)
1156                return mt76_sta_add(dev, vif, sta, ext_phy);
1157
1158        if (old_state == IEEE80211_STA_AUTH &&
1159            new_state == IEEE80211_STA_ASSOC &&
1160            dev->drv->sta_assoc)
1161                dev->drv->sta_assoc(dev, vif, sta);
1162
1163        if (old_state == IEEE80211_STA_NONE &&
1164            new_state == IEEE80211_STA_NOTEXIST)
1165                mt76_sta_remove(dev, vif, sta);
1166
1167        return 0;
1168}
1169EXPORT_SYMBOL_GPL(mt76_sta_state);
1170
1171void mt76_sta_pre_rcu_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1172                             struct ieee80211_sta *sta)
1173{
1174        struct mt76_phy *phy = hw->priv;
1175        struct mt76_dev *dev = phy->dev;
1176        struct mt76_wcid *wcid = (struct mt76_wcid *)sta->drv_priv;
1177
1178        mutex_lock(&dev->mutex);
1179        rcu_assign_pointer(dev->wcid[wcid->idx], NULL);
1180        mutex_unlock(&dev->mutex);
1181}
1182EXPORT_SYMBOL_GPL(mt76_sta_pre_rcu_remove);
1183
1184int mt76_get_txpower(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1185                     int *dbm)
1186{
1187        struct mt76_phy *phy = hw->priv;
1188        int n_chains = hweight8(phy->antenna_mask);
1189        int delta = mt76_tx_power_nss_delta(n_chains);
1190
1191        *dbm = DIV_ROUND_UP(phy->txpower_cur + delta, 2);
1192
1193        return 0;
1194}
1195EXPORT_SYMBOL_GPL(mt76_get_txpower);
1196
1197static void
1198__mt76_csa_finish(void *priv, u8 *mac, struct ieee80211_vif *vif)
1199{
1200        if (vif->csa_active && ieee80211_beacon_cntdwn_is_complete(vif))
1201                ieee80211_csa_finish(vif);
1202}
1203
1204void mt76_csa_finish(struct mt76_dev *dev)
1205{
1206        if (!dev->csa_complete)
1207                return;
1208
1209        ieee80211_iterate_active_interfaces_atomic(dev->hw,
1210                IEEE80211_IFACE_ITER_RESUME_ALL,
1211                __mt76_csa_finish, dev);
1212
1213        dev->csa_complete = 0;
1214}
1215EXPORT_SYMBOL_GPL(mt76_csa_finish);
1216
1217static void
1218__mt76_csa_check(void *priv, u8 *mac, struct ieee80211_vif *vif)
1219{
1220        struct mt76_dev *dev = priv;
1221
1222        if (!vif->csa_active)
1223                return;
1224
1225        dev->csa_complete |= ieee80211_beacon_cntdwn_is_complete(vif);
1226}
1227
1228void mt76_csa_check(struct mt76_dev *dev)
1229{
1230        ieee80211_iterate_active_interfaces_atomic(dev->hw,
1231                IEEE80211_IFACE_ITER_RESUME_ALL,
1232                __mt76_csa_check, dev);
1233}
1234EXPORT_SYMBOL_GPL(mt76_csa_check);
1235
1236int
1237mt76_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta, bool set)
1238{
1239        return 0;
1240}
1241EXPORT_SYMBOL_GPL(mt76_set_tim);
1242
1243void mt76_insert_ccmp_hdr(struct sk_buff *skb, u8 key_id)
1244{
1245        struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
1246        int hdr_len = ieee80211_get_hdrlen_from_skb(skb);
1247        u8 *hdr, *pn = status->iv;
1248
1249        __skb_push(skb, 8);
1250        memmove(skb->data, skb->data + 8, hdr_len);
1251        hdr = skb->data + hdr_len;
1252
1253        hdr[0] = pn[5];
1254        hdr[1] = pn[4];
1255        hdr[2] = 0;
1256        hdr[3] = 0x20 | (key_id << 6);
1257        hdr[4] = pn[3];
1258        hdr[5] = pn[2];
1259        hdr[6] = pn[1];
1260        hdr[7] = pn[0];
1261
1262        status->flag &= ~RX_FLAG_IV_STRIPPED;
1263}
1264EXPORT_SYMBOL_GPL(mt76_insert_ccmp_hdr);
1265
1266int mt76_get_rate(struct mt76_dev *dev,
1267                  struct ieee80211_supported_band *sband,
1268                  int idx, bool cck)
1269{
1270        int i, offset = 0, len = sband->n_bitrates;
1271
1272        if (cck) {
1273                if (sband == &dev->phy.sband_5g.sband)
1274                        return 0;
1275
1276                idx &= ~BIT(2); /* short preamble */
1277        } else if (sband == &dev->phy.sband_2g.sband) {
1278                offset = 4;
1279        }
1280
1281        for (i = offset; i < len; i++) {
1282                if ((sband->bitrates[i].hw_value & GENMASK(7, 0)) == idx)
1283                        return i;
1284        }
1285
1286        return 0;
1287}
1288EXPORT_SYMBOL_GPL(mt76_get_rate);
1289
1290void mt76_sw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1291                  const u8 *mac)
1292{
1293        struct mt76_phy *phy = hw->priv;
1294
1295        set_bit(MT76_SCANNING, &phy->state);
1296}
1297EXPORT_SYMBOL_GPL(mt76_sw_scan);
1298
1299void mt76_sw_scan_complete(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1300{
1301        struct mt76_phy *phy = hw->priv;
1302
1303        clear_bit(MT76_SCANNING, &phy->state);
1304}
1305EXPORT_SYMBOL_GPL(mt76_sw_scan_complete);
1306
1307int mt76_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
1308{
1309        struct mt76_phy *phy = hw->priv;
1310        struct mt76_dev *dev = phy->dev;
1311
1312        mutex_lock(&dev->mutex);
1313        *tx_ant = phy->antenna_mask;
1314        *rx_ant = phy->antenna_mask;
1315        mutex_unlock(&dev->mutex);
1316
1317        return 0;
1318}
1319EXPORT_SYMBOL_GPL(mt76_get_antenna);
1320
1321struct mt76_queue *
1322mt76_init_queue(struct mt76_dev *dev, int qid, int idx, int n_desc,
1323                int ring_base)
1324{
1325        struct mt76_queue *hwq;
1326        int err;
1327
1328        hwq = devm_kzalloc(dev->dev, sizeof(*hwq), GFP_KERNEL);
1329        if (!hwq)
1330                return ERR_PTR(-ENOMEM);
1331
1332        err = dev->queue_ops->alloc(dev, hwq, idx, n_desc, 0, ring_base);
1333        if (err < 0)
1334                return ERR_PTR(err);
1335
1336        return hwq;
1337}
1338EXPORT_SYMBOL_GPL(mt76_init_queue);
1339