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