Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: ISC */
0002 /*
0003  * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
0004  */
0005 
0006 #ifndef __MT76_H
0007 #define __MT76_H
0008 
0009 #include <linux/kernel.h>
0010 #include <linux/io.h>
0011 #include <linux/spinlock.h>
0012 #include <linux/skbuff.h>
0013 #include <linux/leds.h>
0014 #include <linux/usb.h>
0015 #include <linux/average.h>
0016 #include <linux/soc/mediatek/mtk_wed.h>
0017 #include <net/mac80211.h>
0018 #include "util.h"
0019 #include "testmode.h"
0020 
0021 #define MT_MCU_RING_SIZE    32
0022 #define MT_RX_BUF_SIZE      2048
0023 #define MT_SKB_HEAD_LEN     256
0024 
0025 #define MT_MAX_NON_AQL_PKT  16
0026 #define MT_TXQ_FREE_THR     32
0027 
0028 #define MT76_TOKEN_FREE_THR 64
0029 
0030 #define MT_QFLAG_WED_RING   GENMASK(1, 0)
0031 #define MT_QFLAG_WED_TYPE   GENMASK(3, 2)
0032 #define MT_QFLAG_WED        BIT(4)
0033 
0034 #define __MT_WED_Q(_type, _n)   (MT_QFLAG_WED | \
0035                  FIELD_PREP(MT_QFLAG_WED_TYPE, _type) | \
0036                  FIELD_PREP(MT_QFLAG_WED_RING, _n))
0037 #define MT_WED_Q_TX(_n)     __MT_WED_Q(MT76_WED_Q_TX, _n)
0038 #define MT_WED_Q_TXFREE     __MT_WED_Q(MT76_WED_Q_TXFREE, 0)
0039 
0040 struct mt76_dev;
0041 struct mt76_phy;
0042 struct mt76_wcid;
0043 struct mt76s_intr;
0044 
0045 struct mt76_reg_pair {
0046     u32 reg;
0047     u32 value;
0048 };
0049 
0050 enum mt76_bus_type {
0051     MT76_BUS_MMIO,
0052     MT76_BUS_USB,
0053     MT76_BUS_SDIO,
0054 };
0055 
0056 enum mt76_wed_type {
0057     MT76_WED_Q_TX,
0058     MT76_WED_Q_TXFREE,
0059 };
0060 
0061 struct mt76_bus_ops {
0062     u32 (*rr)(struct mt76_dev *dev, u32 offset);
0063     void (*wr)(struct mt76_dev *dev, u32 offset, u32 val);
0064     u32 (*rmw)(struct mt76_dev *dev, u32 offset, u32 mask, u32 val);
0065     void (*write_copy)(struct mt76_dev *dev, u32 offset, const void *data,
0066                int len);
0067     void (*read_copy)(struct mt76_dev *dev, u32 offset, void *data,
0068               int len);
0069     int (*wr_rp)(struct mt76_dev *dev, u32 base,
0070              const struct mt76_reg_pair *rp, int len);
0071     int (*rd_rp)(struct mt76_dev *dev, u32 base,
0072              struct mt76_reg_pair *rp, int len);
0073     enum mt76_bus_type type;
0074 };
0075 
0076 #define mt76_is_usb(dev) ((dev)->bus->type == MT76_BUS_USB)
0077 #define mt76_is_mmio(dev) ((dev)->bus->type == MT76_BUS_MMIO)
0078 #define mt76_is_sdio(dev) ((dev)->bus->type == MT76_BUS_SDIO)
0079 
0080 enum mt76_txq_id {
0081     MT_TXQ_VO = IEEE80211_AC_VO,
0082     MT_TXQ_VI = IEEE80211_AC_VI,
0083     MT_TXQ_BE = IEEE80211_AC_BE,
0084     MT_TXQ_BK = IEEE80211_AC_BK,
0085     MT_TXQ_PSD,
0086     MT_TXQ_BEACON,
0087     MT_TXQ_CAB,
0088     __MT_TXQ_MAX
0089 };
0090 
0091 enum mt76_mcuq_id {
0092     MT_MCUQ_WM,
0093     MT_MCUQ_WA,
0094     MT_MCUQ_FWDL,
0095     __MT_MCUQ_MAX
0096 };
0097 
0098 enum mt76_rxq_id {
0099     MT_RXQ_MAIN,
0100     MT_RXQ_MCU,
0101     MT_RXQ_MCU_WA,
0102     MT_RXQ_BAND1,
0103     MT_RXQ_BAND1_WA,
0104     MT_RXQ_MAIN_WA,
0105     MT_RXQ_BAND2,
0106     MT_RXQ_BAND2_WA,
0107     __MT_RXQ_MAX
0108 };
0109 
0110 enum mt76_band_id {
0111     MT_BAND0,
0112     MT_BAND1,
0113     MT_BAND2,
0114     __MT_MAX_BAND
0115 };
0116 
0117 enum mt76_cipher_type {
0118     MT_CIPHER_NONE,
0119     MT_CIPHER_WEP40,
0120     MT_CIPHER_TKIP,
0121     MT_CIPHER_TKIP_NO_MIC,
0122     MT_CIPHER_AES_CCMP,
0123     MT_CIPHER_WEP104,
0124     MT_CIPHER_BIP_CMAC_128,
0125     MT_CIPHER_WEP128,
0126     MT_CIPHER_WAPI,
0127     MT_CIPHER_CCMP_CCX,
0128     MT_CIPHER_CCMP_256,
0129     MT_CIPHER_GCMP,
0130     MT_CIPHER_GCMP_256,
0131 };
0132 
0133 enum mt76_dfs_state {
0134     MT_DFS_STATE_UNKNOWN,
0135     MT_DFS_STATE_DISABLED,
0136     MT_DFS_STATE_CAC,
0137     MT_DFS_STATE_ACTIVE,
0138 };
0139 
0140 struct mt76_queue_buf {
0141     dma_addr_t addr;
0142     u16 len;
0143     bool skip_unmap;
0144 };
0145 
0146 struct mt76_tx_info {
0147     struct mt76_queue_buf buf[32];
0148     struct sk_buff *skb;
0149     int nbuf;
0150     u32 info;
0151 };
0152 
0153 struct mt76_queue_entry {
0154     union {
0155         void *buf;
0156         struct sk_buff *skb;
0157     };
0158     union {
0159         struct mt76_txwi_cache *txwi;
0160         struct urb *urb;
0161         int buf_sz;
0162     };
0163     u32 dma_addr[2];
0164     u16 dma_len[2];
0165     u16 wcid;
0166     bool skip_buf0:1;
0167     bool skip_buf1:1;
0168     bool done:1;
0169 };
0170 
0171 struct mt76_queue_regs {
0172     u32 desc_base;
0173     u32 ring_size;
0174     u32 cpu_idx;
0175     u32 dma_idx;
0176 } __packed __aligned(4);
0177 
0178 struct mt76_queue {
0179     struct mt76_queue_regs __iomem *regs;
0180 
0181     spinlock_t lock;
0182     spinlock_t cleanup_lock;
0183     struct mt76_queue_entry *entry;
0184     struct mt76_desc *desc;
0185 
0186     u16 first;
0187     u16 head;
0188     u16 tail;
0189     int ndesc;
0190     int queued;
0191     int buf_size;
0192     bool stopped;
0193     bool blocked;
0194 
0195     u8 buf_offset;
0196     u8 hw_idx;
0197     u8 flags;
0198 
0199     u32 wed_regs;
0200 
0201     dma_addr_t desc_dma;
0202     struct sk_buff *rx_head;
0203     struct page_frag_cache rx_page;
0204 };
0205 
0206 struct mt76_mcu_ops {
0207     u32 headroom;
0208     u32 tailroom;
0209 
0210     int (*mcu_send_msg)(struct mt76_dev *dev, int cmd, const void *data,
0211                 int len, bool wait_resp);
0212     int (*mcu_skb_send_msg)(struct mt76_dev *dev, struct sk_buff *skb,
0213                 int cmd, int *seq);
0214     int (*mcu_parse_response)(struct mt76_dev *dev, int cmd,
0215                   struct sk_buff *skb, int seq);
0216     u32 (*mcu_rr)(struct mt76_dev *dev, u32 offset);
0217     void (*mcu_wr)(struct mt76_dev *dev, u32 offset, u32 val);
0218     int (*mcu_wr_rp)(struct mt76_dev *dev, u32 base,
0219              const struct mt76_reg_pair *rp, int len);
0220     int (*mcu_rd_rp)(struct mt76_dev *dev, u32 base,
0221              struct mt76_reg_pair *rp, int len);
0222     int (*mcu_restart)(struct mt76_dev *dev);
0223 };
0224 
0225 struct mt76_queue_ops {
0226     int (*init)(struct mt76_dev *dev,
0227             int (*poll)(struct napi_struct *napi, int budget));
0228 
0229     int (*alloc)(struct mt76_dev *dev, struct mt76_queue *q,
0230              int idx, int n_desc, int bufsize,
0231              u32 ring_base);
0232 
0233     int (*tx_queue_skb)(struct mt76_dev *dev, struct mt76_queue *q,
0234                 enum mt76_txq_id qid, struct sk_buff *skb,
0235                 struct mt76_wcid *wcid, struct ieee80211_sta *sta);
0236 
0237     int (*tx_queue_skb_raw)(struct mt76_dev *dev, struct mt76_queue *q,
0238                 struct sk_buff *skb, u32 tx_info);
0239 
0240     void *(*dequeue)(struct mt76_dev *dev, struct mt76_queue *q, bool flush,
0241              int *len, u32 *info, bool *more);
0242 
0243     void (*rx_reset)(struct mt76_dev *dev, enum mt76_rxq_id qid);
0244 
0245     void (*tx_cleanup)(struct mt76_dev *dev, struct mt76_queue *q,
0246                bool flush);
0247 
0248     void (*rx_cleanup)(struct mt76_dev *dev, struct mt76_queue *q);
0249 
0250     void (*kick)(struct mt76_dev *dev, struct mt76_queue *q);
0251 
0252     void (*reset_q)(struct mt76_dev *dev, struct mt76_queue *q);
0253 };
0254 
0255 enum mt76_wcid_flags {
0256     MT_WCID_FLAG_CHECK_PS,
0257     MT_WCID_FLAG_PS,
0258     MT_WCID_FLAG_4ADDR,
0259     MT_WCID_FLAG_HDR_TRANS,
0260 };
0261 
0262 #define MT76_N_WCIDS 544
0263 
0264 /* stored in ieee80211_tx_info::hw_queue */
0265 #define MT_TX_HW_QUEUE_PHY      GENMASK(3, 2)
0266 
0267 DECLARE_EWMA(signal, 10, 8);
0268 
0269 #define MT_WCID_TX_INFO_RATE        GENMASK(15, 0)
0270 #define MT_WCID_TX_INFO_NSS     GENMASK(17, 16)
0271 #define MT_WCID_TX_INFO_TXPWR_ADJ   GENMASK(25, 18)
0272 #define MT_WCID_TX_INFO_SET     BIT(31)
0273 
0274 struct mt76_wcid {
0275     struct mt76_rx_tid __rcu *aggr[IEEE80211_NUM_TIDS];
0276 
0277     atomic_t non_aql_packets;
0278     unsigned long flags;
0279 
0280     struct ewma_signal rssi;
0281     int inactive_count;
0282 
0283     struct rate_info rate;
0284 
0285     u16 idx;
0286     u8 hw_key_idx;
0287     u8 hw_key_idx2;
0288 
0289     u8 sta:1;
0290     u8 amsdu:1;
0291     u8 phy_idx:2;
0292 
0293     u8 rx_check_pn;
0294     u8 rx_key_pn[IEEE80211_NUM_TIDS + 1][6];
0295     u16 cipher;
0296 
0297     u32 tx_info;
0298     bool sw_iv;
0299 
0300     struct list_head list;
0301     struct idr pktid;
0302 };
0303 
0304 struct mt76_txq {
0305     u16 wcid;
0306 
0307     u16 agg_ssn;
0308     bool send_bar;
0309     bool aggr;
0310 };
0311 
0312 struct mt76_txwi_cache {
0313     struct list_head list;
0314     dma_addr_t dma_addr;
0315 
0316     struct sk_buff *skb;
0317 };
0318 
0319 struct mt76_rx_tid {
0320     struct rcu_head rcu_head;
0321 
0322     struct mt76_dev *dev;
0323 
0324     spinlock_t lock;
0325     struct delayed_work reorder_work;
0326 
0327     u16 head;
0328     u16 size;
0329     u16 nframes;
0330 
0331     u8 num;
0332 
0333     u8 started:1, stopped:1, timer_pending:1;
0334 
0335     struct sk_buff *reorder_buf[];
0336 };
0337 
0338 #define MT_TX_CB_DMA_DONE       BIT(0)
0339 #define MT_TX_CB_TXS_DONE       BIT(1)
0340 #define MT_TX_CB_TXS_FAILED     BIT(2)
0341 
0342 #define MT_PACKET_ID_MASK       GENMASK(6, 0)
0343 #define MT_PACKET_ID_NO_ACK     0
0344 #define MT_PACKET_ID_NO_SKB     1
0345 #define MT_PACKET_ID_FIRST      2
0346 #define MT_PACKET_ID_HAS_RATE       BIT(7)
0347 /* This is timer for when to give up when waiting for TXS callback,
0348  * with starting time being the time at which the DMA_DONE callback
0349  * was seen (so, we know packet was processed then, it should not take
0350  * long after that for firmware to send the TXS callback if it is going
0351  * to do so.)
0352  */
0353 #define MT_TX_STATUS_SKB_TIMEOUT    (HZ / 4)
0354 
0355 struct mt76_tx_cb {
0356     unsigned long jiffies;
0357     u16 wcid;
0358     u8 pktid;
0359     u8 flags;
0360 };
0361 
0362 enum {
0363     MT76_STATE_INITIALIZED,
0364     MT76_STATE_RUNNING,
0365     MT76_STATE_MCU_RUNNING,
0366     MT76_SCANNING,
0367     MT76_HW_SCANNING,
0368     MT76_HW_SCHED_SCANNING,
0369     MT76_RESTART,
0370     MT76_RESET,
0371     MT76_MCU_RESET,
0372     MT76_REMOVED,
0373     MT76_READING_STATS,
0374     MT76_STATE_POWER_OFF,
0375     MT76_STATE_SUSPEND,
0376     MT76_STATE_ROC,
0377     MT76_STATE_PM,
0378 };
0379 
0380 struct mt76_hw_cap {
0381     bool has_2ghz;
0382     bool has_5ghz;
0383     bool has_6ghz;
0384 };
0385 
0386 #define MT_DRV_TXWI_NO_FREE     BIT(0)
0387 #define MT_DRV_TX_ALIGNED4_SKBS     BIT(1)
0388 #define MT_DRV_SW_RX_AIRTIME        BIT(2)
0389 #define MT_DRV_RX_DMA_HDR       BIT(3)
0390 #define MT_DRV_HW_MGMT_TXQ      BIT(4)
0391 
0392 struct mt76_driver_ops {
0393     u32 drv_flags;
0394     u32 survey_flags;
0395     u16 txwi_size;
0396     u16 token_size;
0397     u8 mcs_rates;
0398 
0399     void (*update_survey)(struct mt76_phy *phy);
0400 
0401     int (*tx_prepare_skb)(struct mt76_dev *dev, void *txwi_ptr,
0402                   enum mt76_txq_id qid, struct mt76_wcid *wcid,
0403                   struct ieee80211_sta *sta,
0404                   struct mt76_tx_info *tx_info);
0405 
0406     void (*tx_complete_skb)(struct mt76_dev *dev,
0407                 struct mt76_queue_entry *e);
0408 
0409     bool (*tx_status_data)(struct mt76_dev *dev, u8 *update);
0410 
0411     bool (*rx_check)(struct mt76_dev *dev, void *data, int len);
0412 
0413     void (*rx_skb)(struct mt76_dev *dev, enum mt76_rxq_id q,
0414                struct sk_buff *skb);
0415 
0416     void (*rx_poll_complete)(struct mt76_dev *dev, enum mt76_rxq_id q);
0417 
0418     void (*sta_ps)(struct mt76_dev *dev, struct ieee80211_sta *sta,
0419                bool ps);
0420 
0421     int (*sta_add)(struct mt76_dev *dev, struct ieee80211_vif *vif,
0422                struct ieee80211_sta *sta);
0423 
0424     void (*sta_assoc)(struct mt76_dev *dev, struct ieee80211_vif *vif,
0425               struct ieee80211_sta *sta);
0426 
0427     void (*sta_remove)(struct mt76_dev *dev, struct ieee80211_vif *vif,
0428                struct ieee80211_sta *sta);
0429 };
0430 
0431 struct mt76_channel_state {
0432     u64 cc_active;
0433     u64 cc_busy;
0434     u64 cc_rx;
0435     u64 cc_bss_rx;
0436     u64 cc_tx;
0437 
0438     s8 noise;
0439 };
0440 
0441 struct mt76_sband {
0442     struct ieee80211_supported_band sband;
0443     struct mt76_channel_state *chan;
0444 };
0445 
0446 struct mt76_rate_power {
0447     union {
0448         struct {
0449             s8 cck[4];
0450             s8 ofdm[8];
0451             s8 stbc[10];
0452             s8 ht[16];
0453             s8 vht[10];
0454         };
0455         s8 all[48];
0456     };
0457 };
0458 
0459 /* addr req mask */
0460 #define MT_VEND_TYPE_EEPROM BIT(31)
0461 #define MT_VEND_TYPE_CFG    BIT(30)
0462 #define MT_VEND_TYPE_MASK   (MT_VEND_TYPE_EEPROM | MT_VEND_TYPE_CFG)
0463 
0464 #define MT_VEND_ADDR(type, n)   (MT_VEND_TYPE_##type | (n))
0465 enum mt_vendor_req {
0466     MT_VEND_DEV_MODE =  0x1,
0467     MT_VEND_WRITE =     0x2,
0468     MT_VEND_POWER_ON =  0x4,
0469     MT_VEND_MULTI_WRITE =   0x6,
0470     MT_VEND_MULTI_READ =    0x7,
0471     MT_VEND_READ_EEPROM =   0x9,
0472     MT_VEND_WRITE_FCE = 0x42,
0473     MT_VEND_WRITE_CFG = 0x46,
0474     MT_VEND_READ_CFG =  0x47,
0475     MT_VEND_READ_EXT =  0x63,
0476     MT_VEND_WRITE_EXT = 0x66,
0477     MT_VEND_FEATURE_SET =   0x91,
0478 };
0479 
0480 enum mt76u_in_ep {
0481     MT_EP_IN_PKT_RX,
0482     MT_EP_IN_CMD_RESP,
0483     __MT_EP_IN_MAX,
0484 };
0485 
0486 enum mt76u_out_ep {
0487     MT_EP_OUT_INBAND_CMD,
0488     MT_EP_OUT_AC_BE,
0489     MT_EP_OUT_AC_BK,
0490     MT_EP_OUT_AC_VI,
0491     MT_EP_OUT_AC_VO,
0492     MT_EP_OUT_HCCA,
0493     __MT_EP_OUT_MAX,
0494 };
0495 
0496 struct mt76_mcu {
0497     struct mutex mutex;
0498     u32 msg_seq;
0499     int timeout;
0500 
0501     struct sk_buff_head res_q;
0502     wait_queue_head_t wait;
0503 };
0504 
0505 #define MT_TX_SG_MAX_SIZE   8
0506 #define MT_RX_SG_MAX_SIZE   4
0507 #define MT_NUM_TX_ENTRIES   256
0508 #define MT_NUM_RX_ENTRIES   128
0509 #define MCU_RESP_URB_SIZE   1024
0510 struct mt76_usb {
0511     struct mutex usb_ctrl_mtx;
0512     u8 *data;
0513     u16 data_len;
0514 
0515     struct mt76_worker status_worker;
0516     struct mt76_worker rx_worker;
0517 
0518     struct work_struct stat_work;
0519 
0520     u8 out_ep[__MT_EP_OUT_MAX];
0521     u8 in_ep[__MT_EP_IN_MAX];
0522     bool sg_en;
0523 
0524     struct mt76u_mcu {
0525         u8 *data;
0526         /* multiple reads */
0527         struct mt76_reg_pair *rp;
0528         int rp_len;
0529         u32 base;
0530         bool burst;
0531     } mcu;
0532 };
0533 
0534 #define MT76S_XMIT_BUF_SZ   0x3fe00
0535 #define MT76S_NUM_TX_ENTRIES    256
0536 #define MT76S_NUM_RX_ENTRIES    512
0537 struct mt76_sdio {
0538     struct mt76_worker txrx_worker;
0539     struct mt76_worker status_worker;
0540     struct mt76_worker net_worker;
0541 
0542     struct work_struct stat_work;
0543 
0544     u8 *xmit_buf;
0545     u32 xmit_buf_sz;
0546 
0547     struct sdio_func *func;
0548     void *intr_data;
0549     u8 hw_ver;
0550     wait_queue_head_t wait;
0551 
0552     struct {
0553         int pse_data_quota;
0554         int ple_data_quota;
0555         int pse_mcu_quota;
0556         int pse_page_size;
0557         int deficit;
0558     } sched;
0559 
0560     int (*parse_irq)(struct mt76_dev *dev, struct mt76s_intr *intr);
0561 };
0562 
0563 struct mt76_mmio {
0564     void __iomem *regs;
0565     spinlock_t irq_lock;
0566     u32 irqmask;
0567 
0568     struct mtk_wed_device wed;
0569 };
0570 
0571 struct mt76_rx_status {
0572     union {
0573         struct mt76_wcid *wcid;
0574         u16 wcid_idx;
0575     };
0576 
0577     u32 reorder_time;
0578 
0579     u32 ampdu_ref;
0580     u32 timestamp;
0581 
0582     u8 iv[6];
0583 
0584     u8 phy_idx:2;
0585     u8 aggr:1;
0586     u8 qos_ctl;
0587     u16 seqno;
0588 
0589     u16 freq;
0590     u32 flag;
0591     u8 enc_flags;
0592     u8 encoding:2, bw:3, he_ru:3;
0593     u8 he_gi:2, he_dcm:1;
0594     u8 amsdu:1, first_amsdu:1, last_amsdu:1;
0595     u8 rate_idx;
0596     u8 nss;
0597     u8 band;
0598     s8 signal;
0599     u8 chains;
0600     s8 chain_signal[IEEE80211_MAX_CHAINS];
0601 };
0602 
0603 struct mt76_freq_range_power {
0604     const struct cfg80211_sar_freq_ranges *range;
0605     s8 power;
0606 };
0607 
0608 struct mt76_testmode_ops {
0609     int (*set_state)(struct mt76_phy *phy, enum mt76_testmode_state state);
0610     int (*set_params)(struct mt76_phy *phy, struct nlattr **tb,
0611               enum mt76_testmode_state new_state);
0612     int (*dump_stats)(struct mt76_phy *phy, struct sk_buff *msg);
0613 };
0614 
0615 struct mt76_testmode_data {
0616     enum mt76_testmode_state state;
0617 
0618     u32 param_set[DIV_ROUND_UP(NUM_MT76_TM_ATTRS, 32)];
0619     struct sk_buff *tx_skb;
0620 
0621     u32 tx_count;
0622     u16 tx_mpdu_len;
0623 
0624     u8 tx_rate_mode;
0625     u8 tx_rate_idx;
0626     u8 tx_rate_nss;
0627     u8 tx_rate_sgi;
0628     u8 tx_rate_ldpc;
0629     u8 tx_rate_stbc;
0630     u8 tx_ltf;
0631 
0632     u8 tx_antenna_mask;
0633     u8 tx_spe_idx;
0634 
0635     u8 tx_duty_cycle;
0636     u32 tx_time;
0637     u32 tx_ipg;
0638 
0639     u32 freq_offset;
0640 
0641     u8 tx_power[4];
0642     u8 tx_power_control;
0643 
0644     u8 addr[3][ETH_ALEN];
0645 
0646     u32 tx_pending;
0647     u32 tx_queued;
0648     u16 tx_queued_limit;
0649     u32 tx_done;
0650     struct {
0651         u64 packets[__MT_RXQ_MAX];
0652         u64 fcs_error[__MT_RXQ_MAX];
0653     } rx_stats;
0654 };
0655 
0656 struct mt76_vif {
0657     u8 idx;
0658     u8 omac_idx;
0659     u8 band_idx;
0660     u8 wmm_idx;
0661     u8 scan_seq_num;
0662     u8 cipher;
0663 };
0664 
0665 struct mt76_phy {
0666     struct ieee80211_hw *hw;
0667     struct mt76_dev *dev;
0668     void *priv;
0669 
0670     unsigned long state;
0671     u8 band_idx;
0672 
0673     struct mt76_queue *q_tx[__MT_TXQ_MAX];
0674 
0675     struct cfg80211_chan_def chandef;
0676     struct ieee80211_channel *main_chan;
0677 
0678     struct mt76_channel_state *chan_state;
0679     enum mt76_dfs_state dfs_state;
0680     ktime_t survey_time;
0681 
0682     struct mt76_hw_cap cap;
0683     struct mt76_sband sband_2g;
0684     struct mt76_sband sband_5g;
0685     struct mt76_sband sband_6g;
0686 
0687     u8 macaddr[ETH_ALEN];
0688 
0689     int txpower_cur;
0690     u8 antenna_mask;
0691     u16 chainmask;
0692 
0693 #ifdef CONFIG_NL80211_TESTMODE
0694     struct mt76_testmode_data test;
0695 #endif
0696 
0697     struct delayed_work mac_work;
0698     u8 mac_work_count;
0699 
0700     struct {
0701         struct sk_buff *head;
0702         struct sk_buff **tail;
0703         u16 seqno;
0704     } rx_amsdu[__MT_RXQ_MAX];
0705 
0706     struct mt76_freq_range_power *frp;
0707 };
0708 
0709 struct mt76_dev {
0710     struct mt76_phy phy; /* must be first */
0711     struct mt76_phy *phys[__MT_MAX_BAND];
0712 
0713     struct ieee80211_hw *hw;
0714 
0715     spinlock_t lock;
0716     spinlock_t cc_lock;
0717 
0718     u32 cur_cc_bss_rx;
0719 
0720     struct mt76_rx_status rx_ampdu_status;
0721     u32 rx_ampdu_len;
0722     u32 rx_ampdu_ref;
0723 
0724     struct mutex mutex;
0725 
0726     const struct mt76_bus_ops *bus;
0727     const struct mt76_driver_ops *drv;
0728     const struct mt76_mcu_ops *mcu_ops;
0729     struct device *dev;
0730     struct device *dma_dev;
0731 
0732     struct mt76_mcu mcu;
0733 
0734     struct net_device napi_dev;
0735     struct net_device tx_napi_dev;
0736     spinlock_t rx_lock;
0737     struct napi_struct napi[__MT_RXQ_MAX];
0738     struct sk_buff_head rx_skb[__MT_RXQ_MAX];
0739 
0740     struct list_head txwi_cache;
0741     struct mt76_queue *q_mcu[__MT_MCUQ_MAX];
0742     struct mt76_queue q_rx[__MT_RXQ_MAX];
0743     const struct mt76_queue_ops *queue_ops;
0744     int tx_dma_idx[4];
0745 
0746     struct mt76_worker tx_worker;
0747     struct napi_struct tx_napi;
0748 
0749     spinlock_t token_lock;
0750     struct idr token;
0751     u16 wed_token_count;
0752     u16 token_count;
0753     u16 token_size;
0754 
0755     wait_queue_head_t tx_wait;
0756     /* spinclock used to protect wcid pktid linked list */
0757     spinlock_t status_lock;
0758 
0759     u32 wcid_mask[DIV_ROUND_UP(MT76_N_WCIDS, 32)];
0760     u32 wcid_phy_mask[DIV_ROUND_UP(MT76_N_WCIDS, 32)];
0761 
0762     u64 vif_mask;
0763 
0764     struct mt76_wcid global_wcid;
0765     struct mt76_wcid __rcu *wcid[MT76_N_WCIDS];
0766     struct list_head wcid_list;
0767 
0768     u32 rev;
0769 
0770     u32 aggr_stats[32];
0771 
0772     struct tasklet_struct pre_tbtt_tasklet;
0773     int beacon_int;
0774     u8 beacon_mask;
0775 
0776     struct debugfs_blob_wrapper eeprom;
0777     struct debugfs_blob_wrapper otp;
0778 
0779     struct mt76_rate_power rate_power;
0780 
0781     char alpha2[3];
0782     enum nl80211_dfs_regions region;
0783 
0784     u32 debugfs_reg;
0785 
0786     struct led_classdev led_cdev;
0787     char led_name[32];
0788     bool led_al;
0789     u8 led_pin;
0790 
0791     u8 csa_complete;
0792 
0793     u32 rxfilter;
0794 
0795 #ifdef CONFIG_NL80211_TESTMODE
0796     const struct mt76_testmode_ops *test_ops;
0797     struct {
0798         const char *name;
0799         u32 offset;
0800     } test_mtd;
0801 #endif
0802     struct workqueue_struct *wq;
0803 
0804     union {
0805         struct mt76_mmio mmio;
0806         struct mt76_usb usb;
0807         struct mt76_sdio sdio;
0808     };
0809 };
0810 
0811 struct mt76_power_limits {
0812     s8 cck[4];
0813     s8 ofdm[8];
0814     s8 mcs[4][10];
0815     s8 ru[7][12];
0816 };
0817 
0818 enum mt76_phy_type {
0819     MT_PHY_TYPE_CCK,
0820     MT_PHY_TYPE_OFDM,
0821     MT_PHY_TYPE_HT,
0822     MT_PHY_TYPE_HT_GF,
0823     MT_PHY_TYPE_VHT,
0824     MT_PHY_TYPE_HE_SU = 8,
0825     MT_PHY_TYPE_HE_EXT_SU,
0826     MT_PHY_TYPE_HE_TB,
0827     MT_PHY_TYPE_HE_MU,
0828     __MT_PHY_TYPE_HE_MAX,
0829 };
0830 
0831 struct mt76_sta_stats {
0832     u64 tx_mode[__MT_PHY_TYPE_HE_MAX];
0833     u64 tx_bw[4];       /* 20, 40, 80, 160 */
0834     u64 tx_nss[4];      /* 1, 2, 3, 4 */
0835     u64 tx_mcs[16];     /* mcs idx */
0836 };
0837 
0838 struct mt76_ethtool_worker_info {
0839     u64 *data;
0840     int idx;
0841     int initial_stat_idx;
0842     int worker_stat_count;
0843     int sta_count;
0844 };
0845 
0846 #define CCK_RATE(_idx, _rate) {                 \
0847     .bitrate = _rate,                   \
0848     .flags = IEEE80211_RATE_SHORT_PREAMBLE,         \
0849     .hw_value = (MT_PHY_TYPE_CCK << 8) | (_idx),        \
0850     .hw_value_short = (MT_PHY_TYPE_CCK << 8) | (4 + _idx),  \
0851 }
0852 
0853 #define OFDM_RATE(_idx, _rate) {                \
0854     .bitrate = _rate,                   \
0855     .hw_value = (MT_PHY_TYPE_OFDM << 8) | (_idx),       \
0856     .hw_value_short = (MT_PHY_TYPE_OFDM << 8) | (_idx), \
0857 }
0858 
0859 extern struct ieee80211_rate mt76_rates[12];
0860 
0861 #define __mt76_rr(dev, ...) (dev)->bus->rr((dev), __VA_ARGS__)
0862 #define __mt76_wr(dev, ...) (dev)->bus->wr((dev), __VA_ARGS__)
0863 #define __mt76_rmw(dev, ...)    (dev)->bus->rmw((dev), __VA_ARGS__)
0864 #define __mt76_wr_copy(dev, ...)    (dev)->bus->write_copy((dev), __VA_ARGS__)
0865 #define __mt76_rr_copy(dev, ...)    (dev)->bus->read_copy((dev), __VA_ARGS__)
0866 
0867 #define __mt76_set(dev, offset, val)    __mt76_rmw(dev, offset, 0, val)
0868 #define __mt76_clear(dev, offset, val)  __mt76_rmw(dev, offset, val, 0)
0869 
0870 #define mt76_rr(dev, ...)   (dev)->mt76.bus->rr(&((dev)->mt76), __VA_ARGS__)
0871 #define mt76_wr(dev, ...)   (dev)->mt76.bus->wr(&((dev)->mt76), __VA_ARGS__)
0872 #define mt76_rmw(dev, ...)  (dev)->mt76.bus->rmw(&((dev)->mt76), __VA_ARGS__)
0873 #define mt76_wr_copy(dev, ...)  (dev)->mt76.bus->write_copy(&((dev)->mt76), __VA_ARGS__)
0874 #define mt76_rr_copy(dev, ...)  (dev)->mt76.bus->read_copy(&((dev)->mt76), __VA_ARGS__)
0875 #define mt76_wr_rp(dev, ...)    (dev)->mt76.bus->wr_rp(&((dev)->mt76), __VA_ARGS__)
0876 #define mt76_rd_rp(dev, ...)    (dev)->mt76.bus->rd_rp(&((dev)->mt76), __VA_ARGS__)
0877 
0878 
0879 #define mt76_mcu_restart(dev, ...)  (dev)->mt76.mcu_ops->mcu_restart(&((dev)->mt76))
0880 #define __mt76_mcu_restart(dev, ...)    (dev)->mcu_ops->mcu_restart((dev))
0881 
0882 #define mt76_set(dev, offset, val)  mt76_rmw(dev, offset, 0, val)
0883 #define mt76_clear(dev, offset, val)    mt76_rmw(dev, offset, val, 0)
0884 
0885 #define mt76_get_field(_dev, _reg, _field)      \
0886     FIELD_GET(_field, mt76_rr(dev, _reg))
0887 
0888 #define mt76_rmw_field(_dev, _reg, _field, _val)    \
0889     mt76_rmw(_dev, _reg, _field, FIELD_PREP(_field, _val))
0890 
0891 #define __mt76_rmw_field(_dev, _reg, _field, _val)  \
0892     __mt76_rmw(_dev, _reg, _field, FIELD_PREP(_field, _val))
0893 
0894 #define mt76_hw(dev) (dev)->mphy.hw
0895 
0896 bool __mt76_poll(struct mt76_dev *dev, u32 offset, u32 mask, u32 val,
0897          int timeout);
0898 
0899 #define mt76_poll(dev, ...) __mt76_poll(&((dev)->mt76), __VA_ARGS__)
0900 
0901 bool __mt76_poll_msec(struct mt76_dev *dev, u32 offset, u32 mask, u32 val,
0902               int timeout);
0903 
0904 #define mt76_poll_msec(dev, ...) __mt76_poll_msec(&((dev)->mt76), __VA_ARGS__)
0905 
0906 void mt76_mmio_init(struct mt76_dev *dev, void __iomem *regs);
0907 void mt76_pci_disable_aspm(struct pci_dev *pdev);
0908 
0909 static inline u16 mt76_chip(struct mt76_dev *dev)
0910 {
0911     return dev->rev >> 16;
0912 }
0913 
0914 static inline u16 mt76_rev(struct mt76_dev *dev)
0915 {
0916     return dev->rev & 0xffff;
0917 }
0918 
0919 #define mt76xx_chip(dev) mt76_chip(&((dev)->mt76))
0920 #define mt76xx_rev(dev) mt76_rev(&((dev)->mt76))
0921 
0922 #define mt76_init_queues(dev, ...)      (dev)->mt76.queue_ops->init(&((dev)->mt76), __VA_ARGS__)
0923 #define mt76_queue_alloc(dev, ...)  (dev)->mt76.queue_ops->alloc(&((dev)->mt76), __VA_ARGS__)
0924 #define mt76_tx_queue_skb_raw(dev, ...) (dev)->mt76.queue_ops->tx_queue_skb_raw(&((dev)->mt76), __VA_ARGS__)
0925 #define mt76_tx_queue_skb(dev, ...) (dev)->mt76.queue_ops->tx_queue_skb(&((dev)->mt76), __VA_ARGS__)
0926 #define mt76_queue_rx_reset(dev, ...)   (dev)->mt76.queue_ops->rx_reset(&((dev)->mt76), __VA_ARGS__)
0927 #define mt76_queue_tx_cleanup(dev, ...) (dev)->mt76.queue_ops->tx_cleanup(&((dev)->mt76), __VA_ARGS__)
0928 #define mt76_queue_rx_cleanup(dev, ...) (dev)->mt76.queue_ops->rx_cleanup(&((dev)->mt76), __VA_ARGS__)
0929 #define mt76_queue_kick(dev, ...)   (dev)->mt76.queue_ops->kick(&((dev)->mt76), __VA_ARGS__)
0930 #define mt76_queue_reset(dev, ...)  (dev)->mt76.queue_ops->reset_q(&((dev)->mt76), __VA_ARGS__)
0931 
0932 #define mt76_for_each_q_rx(dev, i)  \
0933     for (i = 0; i < ARRAY_SIZE((dev)->q_rx); i++)   \
0934         if ((dev)->q_rx[i].ndesc)
0935 
0936 struct mt76_dev *mt76_alloc_device(struct device *pdev, unsigned int size,
0937                    const struct ieee80211_ops *ops,
0938                    const struct mt76_driver_ops *drv_ops);
0939 int mt76_register_device(struct mt76_dev *dev, bool vht,
0940              struct ieee80211_rate *rates, int n_rates);
0941 void mt76_unregister_device(struct mt76_dev *dev);
0942 void mt76_free_device(struct mt76_dev *dev);
0943 void mt76_unregister_phy(struct mt76_phy *phy);
0944 
0945 struct mt76_phy *mt76_alloc_phy(struct mt76_dev *dev, unsigned int size,
0946                 const struct ieee80211_ops *ops,
0947                 u8 band_idx);
0948 int mt76_register_phy(struct mt76_phy *phy, bool vht,
0949               struct ieee80211_rate *rates, int n_rates);
0950 
0951 struct dentry *mt76_register_debugfs_fops(struct mt76_phy *phy,
0952                       const struct file_operations *ops);
0953 static inline struct dentry *mt76_register_debugfs(struct mt76_dev *dev)
0954 {
0955     return mt76_register_debugfs_fops(&dev->phy, NULL);
0956 }
0957 
0958 int mt76_queues_read(struct seq_file *s, void *data);
0959 void mt76_seq_puts_array(struct seq_file *file, const char *str,
0960              s8 *val, int len);
0961 
0962 int mt76_eeprom_init(struct mt76_dev *dev, int len);
0963 void mt76_eeprom_override(struct mt76_phy *phy);
0964 int mt76_get_of_eeprom(struct mt76_dev *dev, void *data, int offset, int len);
0965 
0966 struct mt76_queue *
0967 mt76_init_queue(struct mt76_dev *dev, int qid, int idx, int n_desc,
0968         int ring_base, u32 flags);
0969 u16 mt76_calculate_default_rate(struct mt76_phy *phy, int rateidx);
0970 static inline int mt76_init_tx_queue(struct mt76_phy *phy, int qid, int idx,
0971                      int n_desc, int ring_base, u32 flags)
0972 {
0973     struct mt76_queue *q;
0974 
0975     q = mt76_init_queue(phy->dev, qid, idx, n_desc, ring_base, flags);
0976     if (IS_ERR(q))
0977         return PTR_ERR(q);
0978 
0979     phy->q_tx[qid] = q;
0980 
0981     return 0;
0982 }
0983 
0984 static inline int mt76_init_mcu_queue(struct mt76_dev *dev, int qid, int idx,
0985                       int n_desc, int ring_base)
0986 {
0987     struct mt76_queue *q;
0988 
0989     q = mt76_init_queue(dev, qid, idx, n_desc, ring_base, 0);
0990     if (IS_ERR(q))
0991         return PTR_ERR(q);
0992 
0993     dev->q_mcu[qid] = q;
0994 
0995     return 0;
0996 }
0997 
0998 static inline struct mt76_phy *
0999 mt76_dev_phy(struct mt76_dev *dev, u8 phy_idx)
1000 {
1001     if ((phy_idx == MT_BAND1 && dev->phys[phy_idx]) ||
1002         (phy_idx == MT_BAND2 && dev->phys[phy_idx]))
1003         return dev->phys[phy_idx];
1004 
1005     return &dev->phy;
1006 }
1007 
1008 static inline struct ieee80211_hw *
1009 mt76_phy_hw(struct mt76_dev *dev, u8 phy_idx)
1010 {
1011     return mt76_dev_phy(dev, phy_idx)->hw;
1012 }
1013 
1014 static inline u8 *
1015 mt76_get_txwi_ptr(struct mt76_dev *dev, struct mt76_txwi_cache *t)
1016 {
1017     return (u8 *)t - dev->drv->txwi_size;
1018 }
1019 
1020 /* increment with wrap-around */
1021 static inline int mt76_incr(int val, int size)
1022 {
1023     return (val + 1) & (size - 1);
1024 }
1025 
1026 /* decrement with wrap-around */
1027 static inline int mt76_decr(int val, int size)
1028 {
1029     return (val - 1) & (size - 1);
1030 }
1031 
1032 u8 mt76_ac_to_hwq(u8 ac);
1033 
1034 static inline struct ieee80211_txq *
1035 mtxq_to_txq(struct mt76_txq *mtxq)
1036 {
1037     void *ptr = mtxq;
1038 
1039     return container_of(ptr, struct ieee80211_txq, drv_priv);
1040 }
1041 
1042 static inline struct ieee80211_sta *
1043 wcid_to_sta(struct mt76_wcid *wcid)
1044 {
1045     void *ptr = wcid;
1046 
1047     if (!wcid || !wcid->sta)
1048         return NULL;
1049 
1050     return container_of(ptr, struct ieee80211_sta, drv_priv);
1051 }
1052 
1053 static inline struct mt76_tx_cb *mt76_tx_skb_cb(struct sk_buff *skb)
1054 {
1055     BUILD_BUG_ON(sizeof(struct mt76_tx_cb) >
1056              sizeof(IEEE80211_SKB_CB(skb)->status.status_driver_data));
1057     return ((void *)IEEE80211_SKB_CB(skb)->status.status_driver_data);
1058 }
1059 
1060 static inline void *mt76_skb_get_hdr(struct sk_buff *skb)
1061 {
1062     struct mt76_rx_status mstat;
1063     u8 *data = skb->data;
1064 
1065     /* Alignment concerns */
1066     BUILD_BUG_ON(sizeof(struct ieee80211_radiotap_he) % 4);
1067     BUILD_BUG_ON(sizeof(struct ieee80211_radiotap_he_mu) % 4);
1068 
1069     mstat = *((struct mt76_rx_status *)skb->cb);
1070 
1071     if (mstat.flag & RX_FLAG_RADIOTAP_HE)
1072         data += sizeof(struct ieee80211_radiotap_he);
1073     if (mstat.flag & RX_FLAG_RADIOTAP_HE_MU)
1074         data += sizeof(struct ieee80211_radiotap_he_mu);
1075 
1076     return data;
1077 }
1078 
1079 static inline void mt76_insert_hdr_pad(struct sk_buff *skb)
1080 {
1081     int len = ieee80211_get_hdrlen_from_skb(skb);
1082 
1083     if (len % 4 == 0)
1084         return;
1085 
1086     skb_push(skb, 2);
1087     memmove(skb->data, skb->data + 2, len);
1088 
1089     skb->data[len] = 0;
1090     skb->data[len + 1] = 0;
1091 }
1092 
1093 static inline bool mt76_is_skb_pktid(u8 pktid)
1094 {
1095     if (pktid & MT_PACKET_ID_HAS_RATE)
1096         return false;
1097 
1098     return pktid >= MT_PACKET_ID_FIRST;
1099 }
1100 
1101 static inline u8 mt76_tx_power_nss_delta(u8 nss)
1102 {
1103     static const u8 nss_delta[4] = { 0, 6, 9, 12 };
1104 
1105     return nss_delta[nss - 1];
1106 }
1107 
1108 static inline bool mt76_testmode_enabled(struct mt76_phy *phy)
1109 {
1110 #ifdef CONFIG_NL80211_TESTMODE
1111     return phy->test.state != MT76_TM_STATE_OFF;
1112 #else
1113     return false;
1114 #endif
1115 }
1116 
1117 static inline bool mt76_is_testmode_skb(struct mt76_dev *dev,
1118                     struct sk_buff *skb,
1119                     struct ieee80211_hw **hw)
1120 {
1121 #ifdef CONFIG_NL80211_TESTMODE
1122     int i;
1123 
1124     for (i = 0; i < ARRAY_SIZE(dev->phys); i++) {
1125         struct mt76_phy *phy = dev->phys[i];
1126 
1127         if (phy && skb == phy->test.tx_skb) {
1128             *hw = dev->phys[i]->hw;
1129             return true;
1130         }
1131     }
1132     return false;
1133 #else
1134     return false;
1135 #endif
1136 }
1137 
1138 void mt76_rx(struct mt76_dev *dev, enum mt76_rxq_id q, struct sk_buff *skb);
1139 void mt76_tx(struct mt76_phy *dev, struct ieee80211_sta *sta,
1140          struct mt76_wcid *wcid, struct sk_buff *skb);
1141 void mt76_wake_tx_queue(struct ieee80211_hw *hw, struct ieee80211_txq *txq);
1142 void mt76_stop_tx_queues(struct mt76_phy *phy, struct ieee80211_sta *sta,
1143              bool send_bar);
1144 void mt76_tx_check_agg_ssn(struct ieee80211_sta *sta, struct sk_buff *skb);
1145 void mt76_txq_schedule(struct mt76_phy *phy, enum mt76_txq_id qid);
1146 void mt76_txq_schedule_all(struct mt76_phy *phy);
1147 void mt76_tx_worker_run(struct mt76_dev *dev);
1148 void mt76_tx_worker(struct mt76_worker *w);
1149 void mt76_release_buffered_frames(struct ieee80211_hw *hw,
1150                   struct ieee80211_sta *sta,
1151                   u16 tids, int nframes,
1152                   enum ieee80211_frame_release_type reason,
1153                   bool more_data);
1154 bool mt76_has_tx_pending(struct mt76_phy *phy);
1155 void mt76_set_channel(struct mt76_phy *phy);
1156 void mt76_update_survey(struct mt76_phy *phy);
1157 void mt76_update_survey_active_time(struct mt76_phy *phy, ktime_t time);
1158 int mt76_get_survey(struct ieee80211_hw *hw, int idx,
1159             struct survey_info *survey);
1160 void mt76_set_stream_caps(struct mt76_phy *phy, bool vht);
1161 
1162 int mt76_rx_aggr_start(struct mt76_dev *dev, struct mt76_wcid *wcid, u8 tid,
1163                u16 ssn, u16 size);
1164 void mt76_rx_aggr_stop(struct mt76_dev *dev, struct mt76_wcid *wcid, u8 tid);
1165 
1166 void mt76_wcid_key_setup(struct mt76_dev *dev, struct mt76_wcid *wcid,
1167              struct ieee80211_key_conf *key);
1168 
1169 void mt76_tx_status_lock(struct mt76_dev *dev, struct sk_buff_head *list)
1170              __acquires(&dev->status_lock);
1171 void mt76_tx_status_unlock(struct mt76_dev *dev, struct sk_buff_head *list)
1172                __releases(&dev->status_lock);
1173 
1174 int mt76_tx_status_skb_add(struct mt76_dev *dev, struct mt76_wcid *wcid,
1175                struct sk_buff *skb);
1176 struct sk_buff *mt76_tx_status_skb_get(struct mt76_dev *dev,
1177                        struct mt76_wcid *wcid, int pktid,
1178                        struct sk_buff_head *list);
1179 void mt76_tx_status_skb_done(struct mt76_dev *dev, struct sk_buff *skb,
1180                  struct sk_buff_head *list);
1181 void __mt76_tx_complete_skb(struct mt76_dev *dev, u16 wcid, struct sk_buff *skb,
1182                 struct list_head *free_list);
1183 static inline void
1184 mt76_tx_complete_skb(struct mt76_dev *dev, u16 wcid, struct sk_buff *skb)
1185 {
1186     __mt76_tx_complete_skb(dev, wcid, skb, NULL);
1187 }
1188 
1189 void mt76_tx_status_check(struct mt76_dev *dev, bool flush);
1190 int mt76_sta_state(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1191            struct ieee80211_sta *sta,
1192            enum ieee80211_sta_state old_state,
1193            enum ieee80211_sta_state new_state);
1194 void __mt76_sta_remove(struct mt76_dev *dev, struct ieee80211_vif *vif,
1195                struct ieee80211_sta *sta);
1196 void mt76_sta_pre_rcu_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1197                  struct ieee80211_sta *sta);
1198 
1199 int mt76_get_min_avg_rssi(struct mt76_dev *dev, bool ext_phy);
1200 
1201 int mt76_get_txpower(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1202              int *dbm);
1203 int mt76_init_sar_power(struct ieee80211_hw *hw,
1204             const struct cfg80211_sar_specs *sar);
1205 int mt76_get_sar_power(struct mt76_phy *phy,
1206                struct ieee80211_channel *chan,
1207                int power);
1208 
1209 void mt76_csa_check(struct mt76_dev *dev);
1210 void mt76_csa_finish(struct mt76_dev *dev);
1211 
1212 int mt76_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant);
1213 int mt76_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta, bool set);
1214 void mt76_insert_ccmp_hdr(struct sk_buff *skb, u8 key_id);
1215 int mt76_get_rate(struct mt76_dev *dev,
1216           struct ieee80211_supported_band *sband,
1217           int idx, bool cck);
1218 void mt76_sw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1219           const u8 *mac);
1220 void mt76_sw_scan_complete(struct ieee80211_hw *hw,
1221                struct ieee80211_vif *vif);
1222 enum mt76_dfs_state mt76_phy_dfs_state(struct mt76_phy *phy);
1223 int mt76_testmode_cmd(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1224               void *data, int len);
1225 int mt76_testmode_dump(struct ieee80211_hw *hw, struct sk_buff *skb,
1226                struct netlink_callback *cb, void *data, int len);
1227 int mt76_testmode_set_state(struct mt76_phy *phy, enum mt76_testmode_state state);
1228 int mt76_testmode_alloc_skb(struct mt76_phy *phy, u32 len);
1229 
1230 static inline void mt76_testmode_reset(struct mt76_phy *phy, bool disable)
1231 {
1232 #ifdef CONFIG_NL80211_TESTMODE
1233     enum mt76_testmode_state state = MT76_TM_STATE_IDLE;
1234 
1235     if (disable || phy->test.state == MT76_TM_STATE_OFF)
1236         state = MT76_TM_STATE_OFF;
1237 
1238     mt76_testmode_set_state(phy, state);
1239 #endif
1240 }
1241 
1242 
1243 /* internal */
1244 static inline struct ieee80211_hw *
1245 mt76_tx_status_get_hw(struct mt76_dev *dev, struct sk_buff *skb)
1246 {
1247     struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1248     u8 phy_idx = (info->hw_queue & MT_TX_HW_QUEUE_PHY) >> 2;
1249     struct ieee80211_hw *hw = mt76_phy_hw(dev, phy_idx);
1250 
1251     info->hw_queue &= ~MT_TX_HW_QUEUE_PHY;
1252 
1253     return hw;
1254 }
1255 
1256 void mt76_put_txwi(struct mt76_dev *dev, struct mt76_txwi_cache *t);
1257 void mt76_rx_complete(struct mt76_dev *dev, struct sk_buff_head *frames,
1258               struct napi_struct *napi);
1259 void mt76_rx_poll_complete(struct mt76_dev *dev, enum mt76_rxq_id q,
1260                struct napi_struct *napi);
1261 void mt76_rx_aggr_reorder(struct sk_buff *skb, struct sk_buff_head *frames);
1262 void mt76_testmode_tx_pending(struct mt76_phy *phy);
1263 void mt76_queue_tx_complete(struct mt76_dev *dev, struct mt76_queue *q,
1264                 struct mt76_queue_entry *e);
1265 
1266 /* usb */
1267 static inline bool mt76u_urb_error(struct urb *urb)
1268 {
1269     return urb->status &&
1270            urb->status != -ECONNRESET &&
1271            urb->status != -ESHUTDOWN &&
1272            urb->status != -ENOENT;
1273 }
1274 
1275 /* Map hardware queues to usb endpoints */
1276 static inline u8 q2ep(u8 qid)
1277 {
1278     /* TODO: take management packets to queue 5 */
1279     return qid + 1;
1280 }
1281 
1282 static inline int
1283 mt76u_bulk_msg(struct mt76_dev *dev, void *data, int len, int *actual_len,
1284            int timeout, int ep)
1285 {
1286     struct usb_interface *uintf = to_usb_interface(dev->dev);
1287     struct usb_device *udev = interface_to_usbdev(uintf);
1288     struct mt76_usb *usb = &dev->usb;
1289     unsigned int pipe;
1290 
1291     if (actual_len)
1292         pipe = usb_rcvbulkpipe(udev, usb->in_ep[ep]);
1293     else
1294         pipe = usb_sndbulkpipe(udev, usb->out_ep[ep]);
1295 
1296     return usb_bulk_msg(udev, pipe, data, len, actual_len, timeout);
1297 }
1298 
1299 void mt76_ethtool_worker(struct mt76_ethtool_worker_info *wi,
1300              struct mt76_sta_stats *stats);
1301 int mt76_skb_adjust_pad(struct sk_buff *skb, int pad);
1302 int __mt76u_vendor_request(struct mt76_dev *dev, u8 req, u8 req_type,
1303                u16 val, u16 offset, void *buf, size_t len);
1304 int mt76u_vendor_request(struct mt76_dev *dev, u8 req,
1305              u8 req_type, u16 val, u16 offset,
1306              void *buf, size_t len);
1307 void mt76u_single_wr(struct mt76_dev *dev, const u8 req,
1308              const u16 offset, const u32 val);
1309 void mt76u_read_copy(struct mt76_dev *dev, u32 offset,
1310              void *data, int len);
1311 u32 ___mt76u_rr(struct mt76_dev *dev, u8 req, u8 req_type, u32 addr);
1312 void ___mt76u_wr(struct mt76_dev *dev, u8 req, u8 req_type,
1313          u32 addr, u32 val);
1314 int __mt76u_init(struct mt76_dev *dev, struct usb_interface *intf,
1315          struct mt76_bus_ops *ops);
1316 int mt76u_init(struct mt76_dev *dev, struct usb_interface *intf);
1317 int mt76u_alloc_mcu_queue(struct mt76_dev *dev);
1318 int mt76u_alloc_queues(struct mt76_dev *dev);
1319 void mt76u_stop_tx(struct mt76_dev *dev);
1320 void mt76u_stop_rx(struct mt76_dev *dev);
1321 int mt76u_resume_rx(struct mt76_dev *dev);
1322 void mt76u_queues_deinit(struct mt76_dev *dev);
1323 
1324 int mt76s_init(struct mt76_dev *dev, struct sdio_func *func,
1325            const struct mt76_bus_ops *bus_ops);
1326 int mt76s_alloc_rx_queue(struct mt76_dev *dev, enum mt76_rxq_id qid);
1327 int mt76s_alloc_tx(struct mt76_dev *dev);
1328 void mt76s_deinit(struct mt76_dev *dev);
1329 void mt76s_sdio_irq(struct sdio_func *func);
1330 void mt76s_txrx_worker(struct mt76_sdio *sdio);
1331 bool mt76s_txqs_empty(struct mt76_dev *dev);
1332 int mt76s_hw_init(struct mt76_dev *dev, struct sdio_func *func,
1333           int hw_ver);
1334 u32 mt76s_rr(struct mt76_dev *dev, u32 offset);
1335 void mt76s_wr(struct mt76_dev *dev, u32 offset, u32 val);
1336 u32 mt76s_rmw(struct mt76_dev *dev, u32 offset, u32 mask, u32 val);
1337 u32 mt76s_read_pcr(struct mt76_dev *dev);
1338 void mt76s_write_copy(struct mt76_dev *dev, u32 offset,
1339               const void *data, int len);
1340 void mt76s_read_copy(struct mt76_dev *dev, u32 offset,
1341              void *data, int len);
1342 int mt76s_wr_rp(struct mt76_dev *dev, u32 base,
1343         const struct mt76_reg_pair *data,
1344         int len);
1345 int mt76s_rd_rp(struct mt76_dev *dev, u32 base,
1346         struct mt76_reg_pair *data, int len);
1347 
1348 struct sk_buff *
1349 __mt76_mcu_msg_alloc(struct mt76_dev *dev, const void *data,
1350              int len, int data_len, gfp_t gfp);
1351 static inline struct sk_buff *
1352 mt76_mcu_msg_alloc(struct mt76_dev *dev, const void *data,
1353            int data_len)
1354 {
1355     return __mt76_mcu_msg_alloc(dev, data, data_len, data_len, GFP_KERNEL);
1356 }
1357 
1358 void mt76_mcu_rx_event(struct mt76_dev *dev, struct sk_buff *skb);
1359 struct sk_buff *mt76_mcu_get_response(struct mt76_dev *dev,
1360                       unsigned long expires);
1361 int mt76_mcu_send_and_get_msg(struct mt76_dev *dev, int cmd, const void *data,
1362                   int len, bool wait_resp, struct sk_buff **ret);
1363 int mt76_mcu_skb_send_and_get_msg(struct mt76_dev *dev, struct sk_buff *skb,
1364                   int cmd, bool wait_resp, struct sk_buff **ret);
1365 int __mt76_mcu_send_firmware(struct mt76_dev *dev, int cmd, const void *data,
1366                  int len, int max_len);
1367 static inline int
1368 mt76_mcu_send_firmware(struct mt76_dev *dev, int cmd, const void *data,
1369                int len)
1370 {
1371     int max_len = 4096 - dev->mcu_ops->headroom;
1372 
1373     return __mt76_mcu_send_firmware(dev, cmd, data, len, max_len);
1374 }
1375 
1376 static inline int
1377 mt76_mcu_send_msg(struct mt76_dev *dev, int cmd, const void *data, int len,
1378           bool wait_resp)
1379 {
1380     return mt76_mcu_send_and_get_msg(dev, cmd, data, len, wait_resp, NULL);
1381 }
1382 
1383 static inline int
1384 mt76_mcu_skb_send_msg(struct mt76_dev *dev, struct sk_buff *skb, int cmd,
1385               bool wait_resp)
1386 {
1387     return mt76_mcu_skb_send_and_get_msg(dev, skb, cmd, wait_resp, NULL);
1388 }
1389 
1390 void mt76_set_irq_mask(struct mt76_dev *dev, u32 addr, u32 clear, u32 set);
1391 
1392 s8 mt76_get_rate_power_limits(struct mt76_phy *phy,
1393                   struct ieee80211_channel *chan,
1394                   struct mt76_power_limits *dest,
1395                   s8 target_power);
1396 
1397 struct mt76_txwi_cache *
1398 mt76_token_release(struct mt76_dev *dev, int token, bool *wake);
1399 int mt76_token_consume(struct mt76_dev *dev, struct mt76_txwi_cache **ptxwi);
1400 void __mt76_set_tx_blocked(struct mt76_dev *dev, bool blocked);
1401 
1402 static inline void mt76_set_tx_blocked(struct mt76_dev *dev, bool blocked)
1403 {
1404     spin_lock_bh(&dev->token_lock);
1405     __mt76_set_tx_blocked(dev, blocked);
1406     spin_unlock_bh(&dev->token_lock);
1407 }
1408 
1409 static inline int
1410 mt76_token_get(struct mt76_dev *dev, struct mt76_txwi_cache **ptxwi)
1411 {
1412     int token;
1413 
1414     spin_lock_bh(&dev->token_lock);
1415     token = idr_alloc(&dev->token, *ptxwi, 0, dev->token_size, GFP_ATOMIC);
1416     spin_unlock_bh(&dev->token_lock);
1417 
1418     return token;
1419 }
1420 
1421 static inline struct mt76_txwi_cache *
1422 mt76_token_put(struct mt76_dev *dev, int token)
1423 {
1424     struct mt76_txwi_cache *txwi;
1425 
1426     spin_lock_bh(&dev->token_lock);
1427     txwi = idr_remove(&dev->token, token);
1428     spin_unlock_bh(&dev->token_lock);
1429 
1430     return txwi;
1431 }
1432 
1433 static inline void mt76_packet_id_init(struct mt76_wcid *wcid)
1434 {
1435     INIT_LIST_HEAD(&wcid->list);
1436     idr_init(&wcid->pktid);
1437 }
1438 
1439 static inline void
1440 mt76_packet_id_flush(struct mt76_dev *dev, struct mt76_wcid *wcid)
1441 {
1442     struct sk_buff_head list;
1443 
1444     mt76_tx_status_lock(dev, &list);
1445     mt76_tx_status_skb_get(dev, wcid, -1, &list);
1446     mt76_tx_status_unlock(dev, &list);
1447 
1448     idr_destroy(&wcid->pktid);
1449 }
1450 
1451 #endif