mediatek: mt76-6e-usb Fix to build error
[platform/kernel/linux-rpi.git] / drivers / net / wireless / mediatek / mt76-6e-usb / mt76.h
1 /* SPDX-License-Identifier: ISC */
2 /*
3  * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
4  */
5
6 #ifndef __MT76_H
7 #define __MT76_H
8
9 #include <linux/kernel.h>
10 #include <linux/io.h>
11 #include <linux/spinlock.h>
12 #include <linux/skbuff.h>
13 #include <linux/leds.h>
14 #include <linux/usb.h>
15 #include <linux/average.h>
16 #include <net/mac80211.h>
17 #include "util.h"
18 #include "testmode.h"
19
20 #ifdef IEEE80211_HE_PHY_CAP9_NOMIMAL_PKT_PADDING_16US
21 #define IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_0US                  0x0
22 #define IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_8US                  0x1
23 #define IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_16US                 0x2
24 #define IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_RESERVED             0x3
25 #define IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_MASK                 0xc0
26 #endif
27
28 #define MT_MCU_RING_SIZE        32
29 #define MT_RX_BUF_SIZE          2048
30 #define MT_SKB_HEAD_LEN         256
31
32 #define MT_MAX_NON_AQL_PKT      16
33 #define MT_TXQ_FREE_THR         32
34
35 #define MT76_TOKEN_FREE_THR     64
36
37 struct mt76_dev;
38 struct mt76_phy;
39 struct mt76_wcid;
40 struct mt76s_intr;
41
42 struct mt76_reg_pair {
43         u32 reg;
44         u32 value;
45 };
46
47 enum mt76_bus_type {
48         MT76_BUS_MMIO,
49         MT76_BUS_USB,
50         MT76_BUS_SDIO,
51 };
52
53 struct mt76_bus_ops {
54         u32 (*rr)(struct mt76_dev *dev, u32 offset);
55         void (*wr)(struct mt76_dev *dev, u32 offset, u32 val);
56         u32 (*rmw)(struct mt76_dev *dev, u32 offset, u32 mask, u32 val);
57         void (*write_copy)(struct mt76_dev *dev, u32 offset, const void *data,
58                            int len);
59         void (*read_copy)(struct mt76_dev *dev, u32 offset, void *data,
60                           int len);
61         int (*wr_rp)(struct mt76_dev *dev, u32 base,
62                      const struct mt76_reg_pair *rp, int len);
63         int (*rd_rp)(struct mt76_dev *dev, u32 base,
64                      struct mt76_reg_pair *rp, int len);
65         enum mt76_bus_type type;
66 };
67
68 #define mt76_is_usb(dev) ((dev)->bus->type == MT76_BUS_USB)
69 #define mt76_is_mmio(dev) ((dev)->bus->type == MT76_BUS_MMIO)
70 #define mt76_is_sdio(dev) ((dev)->bus->type == MT76_BUS_SDIO)
71
72 enum mt76_txq_id {
73         MT_TXQ_VO = IEEE80211_AC_VO,
74         MT_TXQ_VI = IEEE80211_AC_VI,
75         MT_TXQ_BE = IEEE80211_AC_BE,
76         MT_TXQ_BK = IEEE80211_AC_BK,
77         MT_TXQ_PSD,
78         MT_TXQ_BEACON,
79         MT_TXQ_CAB,
80         __MT_TXQ_MAX
81 };
82
83 enum mt76_mcuq_id {
84         MT_MCUQ_WM,
85         MT_MCUQ_WA,
86         MT_MCUQ_FWDL,
87         __MT_MCUQ_MAX
88 };
89
90 enum mt76_rxq_id {
91         MT_RXQ_MAIN,
92         MT_RXQ_MCU,
93         MT_RXQ_MCU_WA,
94         MT_RXQ_EXT,
95         MT_RXQ_EXT_WA,
96         MT_RXQ_MAIN_WA,
97         __MT_RXQ_MAX
98 };
99
100 enum mt76_cipher_type {
101         MT_CIPHER_NONE,
102         MT_CIPHER_WEP40,
103         MT_CIPHER_TKIP,
104         MT_CIPHER_TKIP_NO_MIC,
105         MT_CIPHER_AES_CCMP,
106         MT_CIPHER_WEP104,
107         MT_CIPHER_BIP_CMAC_128,
108         MT_CIPHER_WEP128,
109         MT_CIPHER_WAPI,
110         MT_CIPHER_CCMP_CCX,
111         MT_CIPHER_CCMP_256,
112         MT_CIPHER_GCMP,
113         MT_CIPHER_GCMP_256,
114 };
115
116 enum mt76_dfs_state {
117         MT_DFS_STATE_UNKNOWN,
118         MT_DFS_STATE_DISABLED,
119         MT_DFS_STATE_CAC,
120         MT_DFS_STATE_ACTIVE,
121 };
122
123 struct mt76_queue_buf {
124         dma_addr_t addr;
125         u16 len;
126         bool skip_unmap;
127 };
128
129 struct mt76_tx_info {
130         struct mt76_queue_buf buf[32];
131         struct sk_buff *skb;
132         int nbuf;
133         u32 info;
134 };
135
136 struct mt76_queue_entry {
137         union {
138                 void *buf;
139                 struct sk_buff *skb;
140         };
141         union {
142                 struct mt76_txwi_cache *txwi;
143                 struct urb *urb;
144                 int buf_sz;
145         };
146         u32 dma_addr[2];
147         u16 dma_len[2];
148         u16 wcid;
149         bool skip_buf0:1;
150         bool skip_buf1:1;
151         bool done:1;
152 };
153
154 struct mt76_queue_regs {
155         u32 desc_base;
156         u32 ring_size;
157         u32 cpu_idx;
158         u32 dma_idx;
159 } __packed __aligned(4);
160
161 struct mt76_queue {
162         struct mt76_queue_regs __iomem *regs;
163
164         spinlock_t lock;
165         spinlock_t cleanup_lock;
166         struct mt76_queue_entry *entry;
167         struct mt76_desc *desc;
168
169         u16 first;
170         u16 head;
171         u16 tail;
172         int ndesc;
173         int queued;
174         int buf_size;
175         bool stopped;
176         bool blocked;
177
178         u8 buf_offset;
179         u8 hw_idx;
180         u8 qid;
181
182         dma_addr_t desc_dma;
183         struct sk_buff *rx_head;
184         struct page_frag_cache rx_page;
185 };
186
187 struct mt76_mcu_ops {
188         u32 headroom;
189         u32 tailroom;
190
191         int (*mcu_send_msg)(struct mt76_dev *dev, int cmd, const void *data,
192                             int len, bool wait_resp);
193         int (*mcu_skb_send_msg)(struct mt76_dev *dev, struct sk_buff *skb,
194                                 int cmd, int *seq);
195         int (*mcu_parse_response)(struct mt76_dev *dev, int cmd,
196                                   struct sk_buff *skb, int seq);
197         u32 (*mcu_rr)(struct mt76_dev *dev, u32 offset);
198         void (*mcu_wr)(struct mt76_dev *dev, u32 offset, u32 val);
199         int (*mcu_wr_rp)(struct mt76_dev *dev, u32 base,
200                          const struct mt76_reg_pair *rp, int len);
201         int (*mcu_rd_rp)(struct mt76_dev *dev, u32 base,
202                          struct mt76_reg_pair *rp, int len);
203         int (*mcu_restart)(struct mt76_dev *dev);
204 };
205
206 struct mt76_queue_ops {
207         int (*init)(struct mt76_dev *dev,
208                     int (*poll)(struct napi_struct *napi, int budget));
209
210         int (*alloc)(struct mt76_dev *dev, struct mt76_queue *q,
211                      int idx, int n_desc, int bufsize,
212                      u32 ring_base);
213
214         int (*tx_queue_skb)(struct mt76_dev *dev, struct mt76_queue *q,
215                             struct sk_buff *skb, struct mt76_wcid *wcid,
216                             struct ieee80211_sta *sta);
217
218         int (*tx_queue_skb_raw)(struct mt76_dev *dev, struct mt76_queue *q,
219                                 struct sk_buff *skb, u32 tx_info);
220
221         void *(*dequeue)(struct mt76_dev *dev, struct mt76_queue *q, bool flush,
222                          int *len, u32 *info, bool *more);
223
224         void (*rx_reset)(struct mt76_dev *dev, enum mt76_rxq_id qid);
225
226         void (*tx_cleanup)(struct mt76_dev *dev, struct mt76_queue *q,
227                            bool flush);
228
229         void (*rx_cleanup)(struct mt76_dev *dev, struct mt76_queue *q);
230
231         void (*kick)(struct mt76_dev *dev, struct mt76_queue *q);
232
233         void (*reset_q)(struct mt76_dev *dev, struct mt76_queue *q);
234 };
235
236 enum mt76_wcid_flags {
237         MT_WCID_FLAG_CHECK_PS,
238         MT_WCID_FLAG_PS,
239         MT_WCID_FLAG_4ADDR,
240         MT_WCID_FLAG_HDR_TRANS,
241 };
242
243 #define MT76_N_WCIDS 544
244
245 /* stored in ieee80211_tx_info::hw_queue */
246 #define MT_TX_HW_QUEUE_EXT_PHY          BIT(3)
247
248 DECLARE_EWMA(signal, 10, 8);
249
250 #define MT_WCID_TX_INFO_RATE            GENMASK(15, 0)
251 #define MT_WCID_TX_INFO_NSS             GENMASK(17, 16)
252 #define MT_WCID_TX_INFO_TXPWR_ADJ       GENMASK(25, 18)
253 #define MT_WCID_TX_INFO_SET             BIT(31)
254
255 struct mt76_wcid {
256         struct mt76_rx_tid __rcu *aggr[IEEE80211_NUM_TIDS];
257
258         atomic_t non_aql_packets;
259         unsigned long flags;
260
261         struct ewma_signal rssi;
262         int inactive_count;
263
264         struct rate_info rate;
265
266         u16 idx;
267         u8 hw_key_idx;
268         u8 hw_key_idx2;
269
270         u8 sta:1;
271         u8 ext_phy:1;
272         u8 amsdu:1;
273
274         u8 rx_check_pn;
275         u8 rx_key_pn[IEEE80211_NUM_TIDS + 1][6];
276         u16 cipher;
277
278         u32 tx_info;
279         bool sw_iv;
280
281         struct list_head list;
282         struct idr pktid;
283 };
284
285 struct mt76_txq {
286         u16 wcid;
287
288         u16 agg_ssn;
289         bool send_bar;
290         bool aggr;
291 };
292
293 struct mt76_txwi_cache {
294         struct list_head list;
295         dma_addr_t dma_addr;
296
297         struct sk_buff *skb;
298 };
299
300 struct mt76_rx_tid {
301         struct rcu_head rcu_head;
302
303         struct mt76_dev *dev;
304
305         spinlock_t lock;
306         struct delayed_work reorder_work;
307
308         u16 head;
309         u16 size;
310         u16 nframes;
311
312         u8 num;
313
314         u8 started:1, stopped:1, timer_pending:1;
315
316         struct sk_buff *reorder_buf[];
317 };
318
319 #define MT_TX_CB_DMA_DONE               BIT(0)
320 #define MT_TX_CB_TXS_DONE               BIT(1)
321 #define MT_TX_CB_TXS_FAILED             BIT(2)
322
323 #define MT_PACKET_ID_MASK               GENMASK(6, 0)
324 #define MT_PACKET_ID_NO_ACK             0
325 #define MT_PACKET_ID_NO_SKB             1
326 #define MT_PACKET_ID_FIRST              2
327 #define MT_PACKET_ID_HAS_RATE           BIT(7)
328 /* This is timer for when to give up when waiting for TXS callback,
329  * with starting time being the time at which the DMA_DONE callback
330  * was seen (so, we know packet was processed then, it should not take
331  * long after that for firmware to send the TXS callback if it is going
332  * to do so.)
333  */
334 #define MT_TX_STATUS_SKB_TIMEOUT        (HZ / 4)
335
336 struct mt76_tx_cb {
337         unsigned long jiffies;
338         u16 wcid;
339         u8 pktid;
340         u8 flags;
341 };
342
343 enum {
344         MT76_STATE_INITIALIZED,
345         MT76_STATE_RUNNING,
346         MT76_STATE_MCU_RUNNING,
347         MT76_SCANNING,
348         MT76_HW_SCANNING,
349         MT76_HW_SCHED_SCANNING,
350         MT76_RESTART,
351         MT76_RESET,
352         MT76_MCU_RESET,
353         MT76_REMOVED,
354         MT76_READING_STATS,
355         MT76_STATE_POWER_OFF,
356         MT76_STATE_SUSPEND,
357         MT76_STATE_ROC,
358         MT76_STATE_PM,
359 };
360
361 struct mt76_hw_cap {
362         bool has_2ghz;
363         bool has_5ghz;
364         bool has_6ghz;
365 };
366
367 #define MT_DRV_TXWI_NO_FREE             BIT(0)
368 #define MT_DRV_TX_ALIGNED4_SKBS         BIT(1)
369 #define MT_DRV_SW_RX_AIRTIME            BIT(2)
370 #define MT_DRV_RX_DMA_HDR               BIT(3)
371 #define MT_DRV_HW_MGMT_TXQ              BIT(4)
372
373 struct mt76_driver_ops {
374         u32 drv_flags;
375         u32 survey_flags;
376         u16 txwi_size;
377         u16 token_size;
378         u8 mcs_rates;
379
380         void (*update_survey)(struct mt76_phy *phy);
381
382         int (*tx_prepare_skb)(struct mt76_dev *dev, void *txwi_ptr,
383                               enum mt76_txq_id qid, struct mt76_wcid *wcid,
384                               struct ieee80211_sta *sta,
385                               struct mt76_tx_info *tx_info);
386
387         void (*tx_complete_skb)(struct mt76_dev *dev,
388                                 struct mt76_queue_entry *e);
389
390         bool (*tx_status_data)(struct mt76_dev *dev, u8 *update);
391
392         bool (*rx_check)(struct mt76_dev *dev, void *data, int len);
393
394         void (*rx_skb)(struct mt76_dev *dev, enum mt76_rxq_id q,
395                        struct sk_buff *skb);
396
397         void (*rx_poll_complete)(struct mt76_dev *dev, enum mt76_rxq_id q);
398
399         void (*sta_ps)(struct mt76_dev *dev, struct ieee80211_sta *sta,
400                        bool ps);
401
402         int (*sta_add)(struct mt76_dev *dev, struct ieee80211_vif *vif,
403                        struct ieee80211_sta *sta);
404
405         void (*sta_assoc)(struct mt76_dev *dev, struct ieee80211_vif *vif,
406                           struct ieee80211_sta *sta);
407
408         void (*sta_remove)(struct mt76_dev *dev, struct ieee80211_vif *vif,
409                            struct ieee80211_sta *sta);
410 };
411
412 struct mt76_channel_state {
413         u64 cc_active;
414         u64 cc_busy;
415         u64 cc_rx;
416         u64 cc_bss_rx;
417         u64 cc_tx;
418
419         s8 noise;
420 };
421
422 struct mt76_sband {
423         struct ieee80211_supported_band sband;
424         struct mt76_channel_state *chan;
425 };
426
427 struct mt76_rate_power {
428         union {
429                 struct {
430                         s8 cck[4];
431                         s8 ofdm[8];
432                         s8 stbc[10];
433                         s8 ht[16];
434                         s8 vht[10];
435                 };
436                 s8 all[48];
437         };
438 };
439
440 /* addr req mask */
441 #define MT_VEND_TYPE_EEPROM     BIT(31)
442 #define MT_VEND_TYPE_CFG        BIT(30)
443 #define MT_VEND_TYPE_MASK       (MT_VEND_TYPE_EEPROM | MT_VEND_TYPE_CFG)
444
445 #define MT_VEND_ADDR(type, n)   (MT_VEND_TYPE_##type | (n))
446 enum mt_vendor_req {
447         MT_VEND_DEV_MODE =      0x1,
448         MT_VEND_WRITE =         0x2,
449         MT_VEND_POWER_ON =      0x4,
450         MT_VEND_MULTI_WRITE =   0x6,
451         MT_VEND_MULTI_READ =    0x7,
452         MT_VEND_READ_EEPROM =   0x9,
453         MT_VEND_WRITE_FCE =     0x42,
454         MT_VEND_WRITE_CFG =     0x46,
455         MT_VEND_READ_CFG =      0x47,
456         MT_VEND_READ_EXT =      0x63,
457         MT_VEND_WRITE_EXT =     0x66,
458         MT_VEND_FEATURE_SET =   0x91,
459 };
460
461 enum mt76u_in_ep {
462         MT_EP_IN_PKT_RX,
463         MT_EP_IN_CMD_RESP,
464         __MT_EP_IN_MAX,
465 };
466
467 enum mt76u_out_ep {
468         MT_EP_OUT_INBAND_CMD,
469         MT_EP_OUT_AC_BE,
470         MT_EP_OUT_AC_BK,
471         MT_EP_OUT_AC_VI,
472         MT_EP_OUT_AC_VO,
473         MT_EP_OUT_HCCA,
474         __MT_EP_OUT_MAX,
475 };
476
477 struct mt76_mcu {
478         struct mutex mutex;
479         u32 msg_seq;
480         int timeout;
481
482         struct sk_buff_head res_q;
483         wait_queue_head_t wait;
484 };
485
486 #define MT_TX_SG_MAX_SIZE       8
487 #define MT_RX_SG_MAX_SIZE       4
488 #define MT_NUM_TX_ENTRIES       256
489 #define MT_NUM_RX_ENTRIES       128
490 #define MCU_RESP_URB_SIZE       1024
491 struct mt76_usb {
492         struct mutex usb_ctrl_mtx;
493         u8 *data;
494         u16 data_len;
495
496         struct mt76_worker status_worker;
497         struct mt76_worker rx_worker;
498
499         struct work_struct stat_work;
500
501         u8 out_ep[__MT_EP_OUT_MAX];
502         u8 in_ep[__MT_EP_IN_MAX];
503         bool sg_en;
504
505         struct mt76u_mcu {
506                 u8 *data;
507                 /* multiple reads */
508                 struct mt76_reg_pair *rp;
509                 int rp_len;
510                 u32 base;
511                 bool burst;
512         } mcu;
513 };
514
515 #define MT76S_XMIT_BUF_SZ       0x3fe00
516 #define MT76S_NUM_TX_ENTRIES    256
517 #define MT76S_NUM_RX_ENTRIES    512
518 struct mt76_sdio {
519         struct mt76_worker txrx_worker;
520         struct mt76_worker status_worker;
521         struct mt76_worker net_worker;
522
523         struct work_struct stat_work;
524
525         u8 *xmit_buf;
526         u32 xmit_buf_sz;
527
528         struct sdio_func *func;
529         void *intr_data;
530         u8 hw_ver;
531         wait_queue_head_t wait;
532
533         struct {
534                 int pse_data_quota;
535                 int ple_data_quota;
536                 int pse_mcu_quota;
537                 int pse_page_size;
538                 int deficit;
539         } sched;
540
541         int (*parse_irq)(struct mt76_dev *dev, struct mt76s_intr *intr);
542 };
543
544 struct mt76_mmio {
545         void __iomem *regs;
546         spinlock_t irq_lock;
547         u32 irqmask;
548 };
549
550 struct mt76_rx_status {
551         union {
552                 struct mt76_wcid *wcid;
553                 u16 wcid_idx;
554         };
555
556         u32 reorder_time;
557
558         u32 ampdu_ref;
559         u32 timestamp;
560
561         u8 iv[6];
562
563         u8 ext_phy:1;
564         u8 aggr:1;
565         u8 qos_ctl;
566         u16 seqno;
567
568         u16 freq;
569         u32 flag;
570         u8 enc_flags;
571         u8 encoding:2, bw:3, he_ru:3;
572         u8 he_gi:2, he_dcm:1;
573         u8 amsdu:1, first_amsdu:1, last_amsdu:1;
574         u8 rate_idx;
575         u8 nss;
576         u8 band;
577         s8 signal;
578         u8 chains;
579         s8 chain_signal[IEEE80211_MAX_CHAINS];
580 };
581
582 struct mt76_freq_range_power {
583         const struct cfg80211_sar_freq_ranges *range;
584         s8 power;
585 };
586
587 struct mt76_testmode_ops {
588         int (*set_state)(struct mt76_phy *phy, enum mt76_testmode_state state);
589         int (*set_params)(struct mt76_phy *phy, struct nlattr **tb,
590                           enum mt76_testmode_state new_state);
591         int (*dump_stats)(struct mt76_phy *phy, struct sk_buff *msg);
592 };
593
594 struct mt76_testmode_data {
595         enum mt76_testmode_state state;
596
597         u32 param_set[DIV_ROUND_UP(NUM_MT76_TM_ATTRS, 32)];
598         struct sk_buff *tx_skb;
599
600         u32 tx_count;
601         u16 tx_mpdu_len;
602
603         u8 tx_rate_mode;
604         u8 tx_rate_idx;
605         u8 tx_rate_nss;
606         u8 tx_rate_sgi;
607         u8 tx_rate_ldpc;
608         u8 tx_rate_stbc;
609         u8 tx_ltf;
610
611         u8 tx_antenna_mask;
612         u8 tx_spe_idx;
613
614         u8 tx_duty_cycle;
615         u32 tx_time;
616         u32 tx_ipg;
617
618         u32 freq_offset;
619
620         u8 tx_power[4];
621         u8 tx_power_control;
622
623         u8 addr[3][ETH_ALEN];
624
625         u32 tx_pending;
626         u32 tx_queued;
627         u16 tx_queued_limit;
628         u32 tx_done;
629         struct {
630                 u64 packets[__MT_RXQ_MAX];
631                 u64 fcs_error[__MT_RXQ_MAX];
632         } rx_stats;
633 };
634
635 struct mt76_vif {
636         u8 idx;
637         u8 omac_idx;
638         u8 band_idx;
639         u8 wmm_idx;
640         u8 scan_seq_num;
641         u8 cipher;
642 };
643
644 struct mt76_phy {
645         struct ieee80211_hw *hw;
646         struct mt76_dev *dev;
647         void *priv;
648
649         unsigned long state;
650
651         struct mt76_queue *q_tx[__MT_TXQ_MAX];
652
653         struct cfg80211_chan_def chandef;
654         struct ieee80211_channel *main_chan;
655
656         struct mt76_channel_state *chan_state;
657         enum mt76_dfs_state dfs_state;
658         ktime_t survey_time;
659
660         struct mt76_hw_cap cap;
661         struct mt76_sband sband_2g;
662         struct mt76_sband sband_5g;
663         struct mt76_sband sband_6g;
664
665         u8 macaddr[ETH_ALEN];
666
667         int txpower_cur;
668         u8 antenna_mask;
669         u16 chainmask;
670
671 #ifdef CONFIG_NL80211_TESTMODE
672         struct mt76_testmode_data test;
673 #endif
674
675         struct delayed_work mac_work;
676         u8 mac_work_count;
677
678         struct {
679                 struct sk_buff *head;
680                 struct sk_buff **tail;
681                 u16 seqno;
682         } rx_amsdu[__MT_RXQ_MAX];
683
684         struct mt76_freq_range_power *frp;
685 };
686
687 struct mt76_dev {
688         struct mt76_phy phy; /* must be first */
689
690         struct mt76_phy *phy2;
691
692         struct ieee80211_hw *hw;
693
694         spinlock_t lock;
695         spinlock_t cc_lock;
696
697         u32 cur_cc_bss_rx;
698
699         struct mt76_rx_status rx_ampdu_status;
700         u32 rx_ampdu_len;
701         u32 rx_ampdu_ref;
702
703         struct mutex mutex;
704
705         const struct mt76_bus_ops *bus;
706         const struct mt76_driver_ops *drv;
707         const struct mt76_mcu_ops *mcu_ops;
708         struct device *dev;
709
710         struct mt76_mcu mcu;
711
712         struct net_device napi_dev;
713         struct net_device tx_napi_dev;
714         spinlock_t rx_lock;
715         struct napi_struct napi[__MT_RXQ_MAX];
716         struct sk_buff_head rx_skb[__MT_RXQ_MAX];
717
718         struct list_head txwi_cache;
719         struct mt76_queue *q_mcu[__MT_MCUQ_MAX];
720         struct mt76_queue q_rx[__MT_RXQ_MAX];
721         const struct mt76_queue_ops *queue_ops;
722         int tx_dma_idx[4];
723
724         struct mt76_worker tx_worker;
725         struct napi_struct tx_napi;
726
727         spinlock_t token_lock;
728         struct idr token;
729         int token_count;
730
731         wait_queue_head_t tx_wait;
732         /* spinclock used to protect wcid pktid linked list */
733         spinlock_t status_lock;
734
735         u32 wcid_mask[DIV_ROUND_UP(MT76_N_WCIDS, 32)];
736         u32 wcid_phy_mask[DIV_ROUND_UP(MT76_N_WCIDS, 32)];
737
738         u32 vif_mask;
739
740         struct mt76_wcid global_wcid;
741         struct mt76_wcid __rcu *wcid[MT76_N_WCIDS];
742         struct list_head wcid_list;
743
744         u32 rev;
745
746         u32 aggr_stats[32];
747
748         struct tasklet_struct pre_tbtt_tasklet;
749         int beacon_int;
750         u8 beacon_mask;
751
752         struct debugfs_blob_wrapper eeprom;
753         struct debugfs_blob_wrapper otp;
754
755         struct mt76_rate_power rate_power;
756
757         char alpha2[3];
758         enum nl80211_dfs_regions region;
759
760         u32 debugfs_reg;
761
762         struct led_classdev led_cdev;
763         char led_name[32];
764         bool led_al;
765         u8 led_pin;
766
767         u8 csa_complete;
768
769         u32 rxfilter;
770
771 #ifdef CONFIG_NL80211_TESTMODE
772         const struct mt76_testmode_ops *test_ops;
773         struct {
774                 const char *name;
775                 u32 offset;
776         } test_mtd;
777 #endif
778         struct workqueue_struct *wq;
779
780         union {
781                 struct mt76_mmio mmio;
782                 struct mt76_usb usb;
783                 struct mt76_sdio sdio;
784         };
785 };
786
787 struct mt76_power_limits {
788         s8 cck[4];
789         s8 ofdm[8];
790         s8 mcs[4][10];
791         s8 ru[7][12];
792 };
793
794 enum mt76_phy_type {
795         MT_PHY_TYPE_CCK,
796         MT_PHY_TYPE_OFDM,
797         MT_PHY_TYPE_HT,
798         MT_PHY_TYPE_HT_GF,
799         MT_PHY_TYPE_VHT,
800         MT_PHY_TYPE_HE_SU = 8,
801         MT_PHY_TYPE_HE_EXT_SU,
802         MT_PHY_TYPE_HE_TB,
803         MT_PHY_TYPE_HE_MU,
804         __MT_PHY_TYPE_HE_MAX,
805 };
806
807 struct mt76_sta_stats {
808         u64 tx_mode[__MT_PHY_TYPE_HE_MAX];
809         u64 tx_bw[4];           /* 20, 40, 80, 160 */
810         u64 tx_nss[4];          /* 1, 2, 3, 4 */
811         u64 tx_mcs[16];         /* mcs idx */
812 };
813
814 struct mt76_ethtool_worker_info {
815         u64 *data;
816         int idx;
817         int initial_stat_idx;
818         int worker_stat_count;
819         int sta_count;
820 };
821
822 #define CCK_RATE(_idx, _rate) {                                 \
823         .bitrate = _rate,                                       \
824         .flags = IEEE80211_RATE_SHORT_PREAMBLE,                 \
825         .hw_value = (MT_PHY_TYPE_CCK << 8) | (_idx),            \
826         .hw_value_short = (MT_PHY_TYPE_CCK << 8) | (4 + _idx),  \
827 }
828
829 #define OFDM_RATE(_idx, _rate) {                                \
830         .bitrate = _rate,                                       \
831         .hw_value = (MT_PHY_TYPE_OFDM << 8) | (_idx),           \
832         .hw_value_short = (MT_PHY_TYPE_OFDM << 8) | (_idx),     \
833 }
834
835 extern struct ieee80211_rate mt76_rates[12];
836
837 #define __mt76_rr(dev, ...)     (dev)->bus->rr((dev), __VA_ARGS__)
838 #define __mt76_wr(dev, ...)     (dev)->bus->wr((dev), __VA_ARGS__)
839 #define __mt76_rmw(dev, ...)    (dev)->bus->rmw((dev), __VA_ARGS__)
840 #define __mt76_wr_copy(dev, ...)        (dev)->bus->write_copy((dev), __VA_ARGS__)
841 #define __mt76_rr_copy(dev, ...)        (dev)->bus->read_copy((dev), __VA_ARGS__)
842
843 #define __mt76_set(dev, offset, val)    __mt76_rmw(dev, offset, 0, val)
844 #define __mt76_clear(dev, offset, val)  __mt76_rmw(dev, offset, val, 0)
845
846 #define mt76_rr(dev, ...)       (dev)->mt76.bus->rr(&((dev)->mt76), __VA_ARGS__)
847 #define mt76_wr(dev, ...)       (dev)->mt76.bus->wr(&((dev)->mt76), __VA_ARGS__)
848 #define mt76_rmw(dev, ...)      (dev)->mt76.bus->rmw(&((dev)->mt76), __VA_ARGS__)
849 #define mt76_wr_copy(dev, ...)  (dev)->mt76.bus->write_copy(&((dev)->mt76), __VA_ARGS__)
850 #define mt76_rr_copy(dev, ...)  (dev)->mt76.bus->read_copy(&((dev)->mt76), __VA_ARGS__)
851 #define mt76_wr_rp(dev, ...)    (dev)->mt76.bus->wr_rp(&((dev)->mt76), __VA_ARGS__)
852 #define mt76_rd_rp(dev, ...)    (dev)->mt76.bus->rd_rp(&((dev)->mt76), __VA_ARGS__)
853
854
855 #define mt76_mcu_restart(dev, ...)      (dev)->mt76.mcu_ops->mcu_restart(&((dev)->mt76))
856 #define __mt76_mcu_restart(dev, ...)    (dev)->mcu_ops->mcu_restart((dev))
857
858 #define mt76_set(dev, offset, val)      mt76_rmw(dev, offset, 0, val)
859 #define mt76_clear(dev, offset, val)    mt76_rmw(dev, offset, val, 0)
860
861 #define mt76_get_field(_dev, _reg, _field)              \
862         FIELD_GET(_field, mt76_rr(dev, _reg))
863
864 #define mt76_rmw_field(_dev, _reg, _field, _val)        \
865         mt76_rmw(_dev, _reg, _field, FIELD_PREP(_field, _val))
866
867 #define __mt76_rmw_field(_dev, _reg, _field, _val)      \
868         __mt76_rmw(_dev, _reg, _field, FIELD_PREP(_field, _val))
869
870 #define mt76_hw(dev) (dev)->mphy.hw
871
872 static inline struct ieee80211_hw *
873 mt76_wcid_hw(struct mt76_dev *dev, u16 wcid)
874 {
875         if (wcid <= MT76_N_WCIDS &&
876             mt76_wcid_mask_test(dev->wcid_phy_mask, wcid))
877                 return dev->phy2->hw;
878
879         return dev->phy.hw;
880 }
881
882 bool __mt76_poll(struct mt76_dev *dev, u32 offset, u32 mask, u32 val,
883                  int timeout);
884
885 #define mt76_poll(dev, ...) __mt76_poll(&((dev)->mt76), __VA_ARGS__)
886
887 bool __mt76_poll_msec(struct mt76_dev *dev, u32 offset, u32 mask, u32 val,
888                       int timeout);
889
890 #define mt76_poll_msec(dev, ...) __mt76_poll_msec(&((dev)->mt76), __VA_ARGS__)
891
892 void mt76_mmio_init(struct mt76_dev *dev, void __iomem *regs);
893 void mt76_pci_disable_aspm(struct pci_dev *pdev);
894
895 static inline u16 mt76_chip(struct mt76_dev *dev)
896 {
897         return dev->rev >> 16;
898 }
899
900 static inline u16 mt76_rev(struct mt76_dev *dev)
901 {
902         return dev->rev & 0xffff;
903 }
904
905 #define mt76xx_chip(dev) mt76_chip(&((dev)->mt76))
906 #define mt76xx_rev(dev) mt76_rev(&((dev)->mt76))
907
908 #define mt76_init_queues(dev, ...)              (dev)->mt76.queue_ops->init(&((dev)->mt76), __VA_ARGS__)
909 #define mt76_queue_alloc(dev, ...)      (dev)->mt76.queue_ops->alloc(&((dev)->mt76), __VA_ARGS__)
910 #define mt76_tx_queue_skb_raw(dev, ...) (dev)->mt76.queue_ops->tx_queue_skb_raw(&((dev)->mt76), __VA_ARGS__)
911 #define mt76_tx_queue_skb(dev, ...)     (dev)->mt76.queue_ops->tx_queue_skb(&((dev)->mt76), __VA_ARGS__)
912 #define mt76_queue_rx_reset(dev, ...)   (dev)->mt76.queue_ops->rx_reset(&((dev)->mt76), __VA_ARGS__)
913 #define mt76_queue_tx_cleanup(dev, ...) (dev)->mt76.queue_ops->tx_cleanup(&((dev)->mt76), __VA_ARGS__)
914 #define mt76_queue_rx_cleanup(dev, ...) (dev)->mt76.queue_ops->rx_cleanup(&((dev)->mt76), __VA_ARGS__)
915 #define mt76_queue_kick(dev, ...)       (dev)->mt76.queue_ops->kick(&((dev)->mt76), __VA_ARGS__)
916 #define mt76_queue_reset(dev, ...)      (dev)->mt76.queue_ops->reset_q(&((dev)->mt76), __VA_ARGS__)
917
918 #define mt76_for_each_q_rx(dev, i)      \
919         for (i = 0; i < ARRAY_SIZE((dev)->q_rx); i++)   \
920                 if ((dev)->q_rx[i].ndesc)
921
922 struct mt76_dev *mt76_alloc_device(struct device *pdev, unsigned int size,
923                                    const struct ieee80211_ops *ops,
924                                    const struct mt76_driver_ops *drv_ops);
925 int mt76_register_device(struct mt76_dev *dev, bool vht,
926                          struct ieee80211_rate *rates, int n_rates);
927 void mt76_unregister_device(struct mt76_dev *dev);
928 void mt76_free_device(struct mt76_dev *dev);
929 void mt76_unregister_phy(struct mt76_phy *phy);
930
931 struct mt76_phy *mt76_alloc_phy(struct mt76_dev *dev, unsigned int size,
932                                 const struct ieee80211_ops *ops);
933 int mt76_register_phy(struct mt76_phy *phy, bool vht,
934                       struct ieee80211_rate *rates, int n_rates);
935
936 struct dentry *mt76_register_debugfs_fops(struct mt76_phy *phy,
937                                           const struct file_operations *ops);
938 static inline struct dentry *mt76_register_debugfs(struct mt76_dev *dev)
939 {
940         return mt76_register_debugfs_fops(&dev->phy, NULL);
941 }
942
943 int mt76_queues_read(struct seq_file *s, void *data);
944 void mt76_seq_puts_array(struct seq_file *file, const char *str,
945                          s8 *val, int len);
946
947 int mt76_eeprom_init(struct mt76_dev *dev, int len);
948 void mt76_eeprom_override(struct mt76_phy *phy);
949 int mt76_get_of_eeprom(struct mt76_dev *dev, void *data, int offset, int len);
950
951 struct mt76_queue *
952 mt76_init_queue(struct mt76_dev *dev, int qid, int idx, int n_desc,
953                 int ring_base);
954 u16 mt76_calculate_default_rate(struct mt76_phy *phy, int rateidx);
955 static inline int mt76_init_tx_queue(struct mt76_phy *phy, int qid, int idx,
956                                      int n_desc, int ring_base)
957 {
958         struct mt76_queue *q;
959
960         q = mt76_init_queue(phy->dev, qid, idx, n_desc, ring_base);
961         if (IS_ERR(q))
962                 return PTR_ERR(q);
963
964         q->qid = qid;
965         phy->q_tx[qid] = q;
966
967         return 0;
968 }
969
970 static inline int mt76_init_mcu_queue(struct mt76_dev *dev, int qid, int idx,
971                                       int n_desc, int ring_base)
972 {
973         struct mt76_queue *q;
974
975         q = mt76_init_queue(dev, qid, idx, n_desc, ring_base);
976         if (IS_ERR(q))
977                 return PTR_ERR(q);
978
979         q->qid = __MT_TXQ_MAX + qid;
980         dev->q_mcu[qid] = q;
981
982         return 0;
983 }
984
985 static inline struct mt76_phy *
986 mt76_dev_phy(struct mt76_dev *dev, bool phy_ext)
987 {
988         if (phy_ext && dev->phy2)
989                 return dev->phy2;
990         return &dev->phy;
991 }
992
993 static inline struct ieee80211_hw *
994 mt76_phy_hw(struct mt76_dev *dev, bool phy_ext)
995 {
996         return mt76_dev_phy(dev, phy_ext)->hw;
997 }
998
999 static inline u8 *
1000 mt76_get_txwi_ptr(struct mt76_dev *dev, struct mt76_txwi_cache *t)
1001 {
1002         return (u8 *)t - dev->drv->txwi_size;
1003 }
1004
1005 /* increment with wrap-around */
1006 static inline int mt76_incr(int val, int size)
1007 {
1008         return (val + 1) & (size - 1);
1009 }
1010
1011 /* decrement with wrap-around */
1012 static inline int mt76_decr(int val, int size)
1013 {
1014         return (val - 1) & (size - 1);
1015 }
1016
1017 u8 mt76_ac_to_hwq(u8 ac);
1018
1019 static inline struct ieee80211_txq *
1020 mtxq_to_txq(struct mt76_txq *mtxq)
1021 {
1022         void *ptr = mtxq;
1023
1024         return container_of(ptr, struct ieee80211_txq, drv_priv);
1025 }
1026
1027 static inline struct ieee80211_sta *
1028 wcid_to_sta(struct mt76_wcid *wcid)
1029 {
1030         void *ptr = wcid;
1031
1032         if (!wcid || !wcid->sta)
1033                 return NULL;
1034
1035         return container_of(ptr, struct ieee80211_sta, drv_priv);
1036 }
1037
1038 static inline struct mt76_tx_cb *mt76_tx_skb_cb(struct sk_buff *skb)
1039 {
1040         BUILD_BUG_ON(sizeof(struct mt76_tx_cb) >
1041                      sizeof(IEEE80211_SKB_CB(skb)->status.status_driver_data));
1042         return ((void *)IEEE80211_SKB_CB(skb)->status.status_driver_data);
1043 }
1044
1045 static inline void *mt76_skb_get_hdr(struct sk_buff *skb)
1046 {
1047         struct mt76_rx_status mstat;
1048         u8 *data = skb->data;
1049
1050         /* Alignment concerns */
1051         BUILD_BUG_ON(sizeof(struct ieee80211_radiotap_he) % 4);
1052         BUILD_BUG_ON(sizeof(struct ieee80211_radiotap_he_mu) % 4);
1053
1054         mstat = *((struct mt76_rx_status *)skb->cb);
1055
1056         if (mstat.flag & RX_FLAG_RADIOTAP_HE)
1057                 data += sizeof(struct ieee80211_radiotap_he);
1058         if (mstat.flag & RX_FLAG_RADIOTAP_HE_MU)
1059                 data += sizeof(struct ieee80211_radiotap_he_mu);
1060
1061         return data;
1062 }
1063
1064 static inline void mt76_insert_hdr_pad(struct sk_buff *skb)
1065 {
1066         int len = ieee80211_get_hdrlen_from_skb(skb);
1067
1068         if (len % 4 == 0)
1069                 return;
1070
1071         skb_push(skb, 2);
1072         memmove(skb->data, skb->data + 2, len);
1073
1074         skb->data[len] = 0;
1075         skb->data[len + 1] = 0;
1076 }
1077
1078 static inline bool mt76_is_skb_pktid(u8 pktid)
1079 {
1080         if (pktid & MT_PACKET_ID_HAS_RATE)
1081                 return false;
1082
1083         return pktid >= MT_PACKET_ID_FIRST;
1084 }
1085
1086 static inline u8 mt76_tx_power_nss_delta(u8 nss)
1087 {
1088         static const u8 nss_delta[4] = { 0, 6, 9, 12 };
1089
1090         return nss_delta[nss - 1];
1091 }
1092
1093 static inline bool mt76_testmode_enabled(struct mt76_phy *phy)
1094 {
1095 #ifdef CONFIG_NL80211_TESTMODE
1096         return phy->test.state != MT76_TM_STATE_OFF;
1097 #else
1098         return false;
1099 #endif
1100 }
1101
1102 static inline bool mt76_is_testmode_skb(struct mt76_dev *dev,
1103                                         struct sk_buff *skb,
1104                                         struct ieee80211_hw **hw)
1105 {
1106 #ifdef CONFIG_NL80211_TESTMODE
1107         if (skb == dev->phy.test.tx_skb)
1108                 *hw = dev->phy.hw;
1109         else if (dev->phy2 && skb == dev->phy2->test.tx_skb)
1110                 *hw = dev->phy2->hw;
1111         else
1112                 return false;
1113         return true;
1114 #else
1115         return false;
1116 #endif
1117 }
1118
1119 void mt76_rx(struct mt76_dev *dev, enum mt76_rxq_id q, struct sk_buff *skb);
1120 void mt76_tx(struct mt76_phy *dev, struct ieee80211_sta *sta,
1121              struct mt76_wcid *wcid, struct sk_buff *skb);
1122 void mt76_wake_tx_queue(struct ieee80211_hw *hw, struct ieee80211_txq *txq);
1123 void mt76_stop_tx_queues(struct mt76_phy *phy, struct ieee80211_sta *sta,
1124                          bool send_bar);
1125 void mt76_tx_check_agg_ssn(struct ieee80211_sta *sta, struct sk_buff *skb);
1126 void mt76_txq_schedule(struct mt76_phy *phy, enum mt76_txq_id qid);
1127 void mt76_txq_schedule_all(struct mt76_phy *phy);
1128 void mt76_tx_worker_run(struct mt76_dev *dev);
1129 void mt76_tx_worker(struct mt76_worker *w);
1130 void mt76_release_buffered_frames(struct ieee80211_hw *hw,
1131                                   struct ieee80211_sta *sta,
1132                                   u16 tids, int nframes,
1133                                   enum ieee80211_frame_release_type reason,
1134                                   bool more_data);
1135 bool mt76_has_tx_pending(struct mt76_phy *phy);
1136 void mt76_set_channel(struct mt76_phy *phy);
1137 void mt76_update_survey(struct mt76_phy *phy);
1138 void mt76_update_survey_active_time(struct mt76_phy *phy, ktime_t time);
1139 int mt76_get_survey(struct ieee80211_hw *hw, int idx,
1140                     struct survey_info *survey);
1141 void mt76_set_stream_caps(struct mt76_phy *phy, bool vht);
1142
1143 int mt76_rx_aggr_start(struct mt76_dev *dev, struct mt76_wcid *wcid, u8 tid,
1144                        u16 ssn, u16 size);
1145 void mt76_rx_aggr_stop(struct mt76_dev *dev, struct mt76_wcid *wcid, u8 tid);
1146
1147 void mt76_wcid_key_setup(struct mt76_dev *dev, struct mt76_wcid *wcid,
1148                          struct ieee80211_key_conf *key);
1149
1150 void mt76_tx_status_lock(struct mt76_dev *dev, struct sk_buff_head *list)
1151                          __acquires(&dev->status_lock);
1152 void mt76_tx_status_unlock(struct mt76_dev *dev, struct sk_buff_head *list)
1153                            __releases(&dev->status_lock);
1154
1155 int mt76_tx_status_skb_add(struct mt76_dev *dev, struct mt76_wcid *wcid,
1156                            struct sk_buff *skb);
1157 struct sk_buff *mt76_tx_status_skb_get(struct mt76_dev *dev,
1158                                        struct mt76_wcid *wcid, int pktid,
1159                                        struct sk_buff_head *list);
1160 void mt76_tx_status_skb_done(struct mt76_dev *dev, struct sk_buff *skb,
1161                              struct sk_buff_head *list);
1162 void __mt76_tx_complete_skb(struct mt76_dev *dev, u16 wcid, struct sk_buff *skb,
1163                             struct list_head *free_list);
1164 static inline void
1165 mt76_tx_complete_skb(struct mt76_dev *dev, u16 wcid, struct sk_buff *skb)
1166 {
1167     __mt76_tx_complete_skb(dev, wcid, skb, NULL);
1168 }
1169
1170 void mt76_tx_status_check(struct mt76_dev *dev, bool flush);
1171 int mt76_sta_state(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1172                    struct ieee80211_sta *sta,
1173                    enum ieee80211_sta_state old_state,
1174                    enum ieee80211_sta_state new_state);
1175 void __mt76_sta_remove(struct mt76_dev *dev, struct ieee80211_vif *vif,
1176                        struct ieee80211_sta *sta);
1177 void mt76_sta_pre_rcu_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1178                              struct ieee80211_sta *sta);
1179
1180 int mt76_get_min_avg_rssi(struct mt76_dev *dev, bool ext_phy);
1181
1182 int mt76_get_txpower(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1183                      int *dbm);
1184 int mt76_init_sar_power(struct ieee80211_hw *hw,
1185                         const struct cfg80211_sar_specs *sar);
1186 int mt76_get_sar_power(struct mt76_phy *phy,
1187                        struct ieee80211_channel *chan,
1188                        int power);
1189
1190 void mt76_csa_check(struct mt76_dev *dev);
1191 void mt76_csa_finish(struct mt76_dev *dev);
1192
1193 int mt76_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant);
1194 int mt76_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta, bool set);
1195 void mt76_insert_ccmp_hdr(struct sk_buff *skb, u8 key_id);
1196 int mt76_get_rate(struct mt76_dev *dev,
1197                   struct ieee80211_supported_band *sband,
1198                   int idx, bool cck);
1199 void mt76_sw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1200                   const u8 *mac);
1201 void mt76_sw_scan_complete(struct ieee80211_hw *hw,
1202                            struct ieee80211_vif *vif);
1203 enum mt76_dfs_state mt76_phy_dfs_state(struct mt76_phy *phy);
1204 int mt76_testmode_cmd(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1205                       void *data, int len);
1206 int mt76_testmode_dump(struct ieee80211_hw *hw, struct sk_buff *skb,
1207                        struct netlink_callback *cb, void *data, int len);
1208 int mt76_testmode_set_state(struct mt76_phy *phy, enum mt76_testmode_state state);
1209 int mt76_testmode_alloc_skb(struct mt76_phy *phy, u32 len);
1210
1211 static inline void mt76_testmode_reset(struct mt76_phy *phy, bool disable)
1212 {
1213 #ifdef CONFIG_NL80211_TESTMODE
1214         enum mt76_testmode_state state = MT76_TM_STATE_IDLE;
1215
1216         if (disable || phy->test.state == MT76_TM_STATE_OFF)
1217                 state = MT76_TM_STATE_OFF;
1218
1219         mt76_testmode_set_state(phy, state);
1220 #endif
1221 }
1222
1223
1224 /* internal */
1225 static inline struct ieee80211_hw *
1226 mt76_tx_status_get_hw(struct mt76_dev *dev, struct sk_buff *skb)
1227 {
1228         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1229         struct ieee80211_hw *hw = dev->phy.hw;
1230
1231         if ((info->hw_queue & MT_TX_HW_QUEUE_EXT_PHY) && dev->phy2)
1232                 hw = dev->phy2->hw;
1233
1234         info->hw_queue &= ~MT_TX_HW_QUEUE_EXT_PHY;
1235
1236         return hw;
1237 }
1238
1239 void mt76_put_txwi(struct mt76_dev *dev, struct mt76_txwi_cache *t);
1240 void mt76_rx_complete(struct mt76_dev *dev, struct sk_buff_head *frames,
1241                       struct napi_struct *napi);
1242 void mt76_rx_poll_complete(struct mt76_dev *dev, enum mt76_rxq_id q,
1243                            struct napi_struct *napi);
1244 void mt76_rx_aggr_reorder(struct sk_buff *skb, struct sk_buff_head *frames);
1245 void mt76_testmode_tx_pending(struct mt76_phy *phy);
1246 void mt76_queue_tx_complete(struct mt76_dev *dev, struct mt76_queue *q,
1247                             struct mt76_queue_entry *e);
1248
1249 /* usb */
1250 static inline bool mt76u_urb_error(struct urb *urb)
1251 {
1252         return urb->status &&
1253                urb->status != -ECONNRESET &&
1254                urb->status != -ESHUTDOWN &&
1255                urb->status != -ENOENT;
1256 }
1257
1258 /* Map hardware queues to usb endpoints */
1259 static inline u8 q2ep(u8 qid)
1260 {
1261         /* TODO: take management packets to queue 5 */
1262         return qid + 1;
1263 }
1264
1265 static inline int
1266 mt76u_bulk_msg(struct mt76_dev *dev, void *data, int len, int *actual_len,
1267                int timeout, int ep)
1268 {
1269         struct usb_interface *uintf = to_usb_interface(dev->dev);
1270         struct usb_device *udev = interface_to_usbdev(uintf);
1271         struct mt76_usb *usb = &dev->usb;
1272         unsigned int pipe;
1273
1274         if (actual_len)
1275                 pipe = usb_rcvbulkpipe(udev, usb->in_ep[ep]);
1276         else
1277                 pipe = usb_sndbulkpipe(udev, usb->out_ep[ep]);
1278
1279         return usb_bulk_msg(udev, pipe, data, len, actual_len, timeout);
1280 }
1281
1282 void mt76_ethtool_worker(struct mt76_ethtool_worker_info *wi,
1283                          struct mt76_sta_stats *stats);
1284 int mt76_skb_adjust_pad(struct sk_buff *skb, int pad);
1285 int __mt76u_vendor_request(struct mt76_dev *dev, u8 req, u8 req_type,
1286                            u16 val, u16 offset, void *buf, size_t len);
1287 int mt76u_vendor_request(struct mt76_dev *dev, u8 req,
1288                          u8 req_type, u16 val, u16 offset,
1289                          void *buf, size_t len);
1290 void mt76u_single_wr(struct mt76_dev *dev, const u8 req,
1291                      const u16 offset, const u32 val);
1292 void mt76u_read_copy(struct mt76_dev *dev, u32 offset,
1293                      void *data, int len);
1294 u32 ___mt76u_rr(struct mt76_dev *dev, u8 req, u8 req_type, u32 addr);
1295 void ___mt76u_wr(struct mt76_dev *dev, u8 req, u8 req_type,
1296                  u32 addr, u32 val);
1297 int __mt76u_init(struct mt76_dev *dev, struct usb_interface *intf,
1298                  struct mt76_bus_ops *ops);
1299 int mt76u_init(struct mt76_dev *dev, struct usb_interface *intf);
1300 int mt76u_alloc_mcu_queue(struct mt76_dev *dev);
1301 int mt76u_alloc_queues(struct mt76_dev *dev);
1302 void mt76u_stop_tx(struct mt76_dev *dev);
1303 void mt76u_stop_rx(struct mt76_dev *dev);
1304 int mt76u_resume_rx(struct mt76_dev *dev);
1305 void mt76u_queues_deinit(struct mt76_dev *dev);
1306
1307 int mt76s_init(struct mt76_dev *dev, struct sdio_func *func,
1308                const struct mt76_bus_ops *bus_ops);
1309 int mt76s_alloc_rx_queue(struct mt76_dev *dev, enum mt76_rxq_id qid);
1310 int mt76s_alloc_tx(struct mt76_dev *dev);
1311 void mt76s_deinit(struct mt76_dev *dev);
1312 void mt76s_sdio_irq(struct sdio_func *func);
1313 void mt76s_txrx_worker(struct mt76_sdio *sdio);
1314 bool mt76s_txqs_empty(struct mt76_dev *dev);
1315 int mt76s_hw_init(struct mt76_dev *dev, struct sdio_func *func,
1316                   int hw_ver);
1317 u32 mt76s_rr(struct mt76_dev *dev, u32 offset);
1318 void mt76s_wr(struct mt76_dev *dev, u32 offset, u32 val);
1319 u32 mt76s_rmw(struct mt76_dev *dev, u32 offset, u32 mask, u32 val);
1320 u32 mt76s_read_pcr(struct mt76_dev *dev);
1321 void mt76s_write_copy(struct mt76_dev *dev, u32 offset,
1322                       const void *data, int len);
1323 void mt76s_read_copy(struct mt76_dev *dev, u32 offset,
1324                      void *data, int len);
1325 int mt76s_wr_rp(struct mt76_dev *dev, u32 base,
1326                 const struct mt76_reg_pair *data,
1327                 int len);
1328 int mt76s_rd_rp(struct mt76_dev *dev, u32 base,
1329                 struct mt76_reg_pair *data, int len);
1330
1331 struct sk_buff *
1332 mt76_mcu_msg_alloc(struct mt76_dev *dev, const void *data,
1333                    int data_len);
1334 void mt76_mcu_rx_event(struct mt76_dev *dev, struct sk_buff *skb);
1335 struct sk_buff *mt76_mcu_get_response(struct mt76_dev *dev,
1336                                       unsigned long expires);
1337 int mt76_mcu_send_and_get_msg(struct mt76_dev *dev, int cmd, const void *data,
1338                               int len, bool wait_resp, struct sk_buff **ret);
1339 int mt76_mcu_skb_send_and_get_msg(struct mt76_dev *dev, struct sk_buff *skb,
1340                                   int cmd, bool wait_resp, struct sk_buff **ret);
1341 int __mt76_mcu_send_firmware(struct mt76_dev *dev, int cmd, const void *data,
1342                              int len, int max_len);
1343 static inline int
1344 mt76_mcu_send_firmware(struct mt76_dev *dev, int cmd, const void *data,
1345                        int len)
1346 {
1347         int max_len = 4096 - dev->mcu_ops->headroom;
1348
1349         return __mt76_mcu_send_firmware(dev, cmd, data, len, max_len);
1350 }
1351
1352 static inline int
1353 mt76_mcu_send_msg(struct mt76_dev *dev, int cmd, const void *data, int len,
1354                   bool wait_resp)
1355 {
1356         return mt76_mcu_send_and_get_msg(dev, cmd, data, len, wait_resp, NULL);
1357 }
1358
1359 static inline int
1360 mt76_mcu_skb_send_msg(struct mt76_dev *dev, struct sk_buff *skb, int cmd,
1361                       bool wait_resp)
1362 {
1363         return mt76_mcu_skb_send_and_get_msg(dev, skb, cmd, wait_resp, NULL);
1364 }
1365
1366 void mt76_set_irq_mask(struct mt76_dev *dev, u32 addr, u32 clear, u32 set);
1367
1368 s8 mt76_get_rate_power_limits(struct mt76_phy *phy,
1369                               struct ieee80211_channel *chan,
1370                               struct mt76_power_limits *dest,
1371                               s8 target_power);
1372
1373 struct mt76_txwi_cache *
1374 mt76_token_release(struct mt76_dev *dev, int token, bool *wake);
1375 int mt76_token_consume(struct mt76_dev *dev, struct mt76_txwi_cache **ptxwi);
1376 void __mt76_set_tx_blocked(struct mt76_dev *dev, bool blocked);
1377
1378 static inline void mt76_set_tx_blocked(struct mt76_dev *dev, bool blocked)
1379 {
1380         spin_lock_bh(&dev->token_lock);
1381         __mt76_set_tx_blocked(dev, blocked);
1382         spin_unlock_bh(&dev->token_lock);
1383 }
1384
1385 static inline int
1386 mt76_token_get(struct mt76_dev *dev, struct mt76_txwi_cache **ptxwi)
1387 {
1388         int token;
1389
1390         spin_lock_bh(&dev->token_lock);
1391         token = idr_alloc(&dev->token, *ptxwi, 0, dev->drv->token_size,
1392                           GFP_ATOMIC);
1393         spin_unlock_bh(&dev->token_lock);
1394
1395         return token;
1396 }
1397
1398 static inline struct mt76_txwi_cache *
1399 mt76_token_put(struct mt76_dev *dev, int token)
1400 {
1401         struct mt76_txwi_cache *txwi;
1402
1403         spin_lock_bh(&dev->token_lock);
1404         txwi = idr_remove(&dev->token, token);
1405         spin_unlock_bh(&dev->token_lock);
1406
1407         return txwi;
1408 }
1409
1410 static inline void mt76_packet_id_init(struct mt76_wcid *wcid)
1411 {
1412         INIT_LIST_HEAD(&wcid->list);
1413         idr_init(&wcid->pktid);
1414 }
1415
1416 static inline void
1417 mt76_packet_id_flush(struct mt76_dev *dev, struct mt76_wcid *wcid)
1418 {
1419         struct sk_buff_head list;
1420
1421         mt76_tx_status_lock(dev, &list);
1422         mt76_tx_status_skb_get(dev, wcid, -1, &list);
1423         mt76_tx_status_unlock(dev, &list);
1424
1425         idr_destroy(&wcid->pktid);
1426 }
1427
1428 #endif