1 /* SPDX-License-Identifier: ISC */
3 * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
9 #include <linux/kernel.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>
20 #define MT_MCU_RING_SIZE 32
21 #define MT_RX_BUF_SIZE 2048
22 #define MT_SKB_HEAD_LEN 256
24 #define MT_MAX_NON_AQL_PKT 16
25 #define MT_TXQ_FREE_THR 32
27 #define MT76_TOKEN_FREE_THR 64
34 struct mt76_reg_pair {
46 u32 (*rr)(struct mt76_dev *dev, u32 offset);
47 void (*wr)(struct mt76_dev *dev, u32 offset, u32 val);
48 u32 (*rmw)(struct mt76_dev *dev, u32 offset, u32 mask, u32 val);
49 void (*write_copy)(struct mt76_dev *dev, u32 offset, const void *data,
51 void (*read_copy)(struct mt76_dev *dev, u32 offset, void *data,
53 int (*wr_rp)(struct mt76_dev *dev, u32 base,
54 const struct mt76_reg_pair *rp, int len);
55 int (*rd_rp)(struct mt76_dev *dev, u32 base,
56 struct mt76_reg_pair *rp, int len);
57 enum mt76_bus_type type;
60 #define mt76_is_usb(dev) ((dev)->bus->type == MT76_BUS_USB)
61 #define mt76_is_mmio(dev) ((dev)->bus->type == MT76_BUS_MMIO)
62 #define mt76_is_sdio(dev) ((dev)->bus->type == MT76_BUS_SDIO)
65 MT_TXQ_VO = IEEE80211_AC_VO,
66 MT_TXQ_VI = IEEE80211_AC_VI,
67 MT_TXQ_BE = IEEE80211_AC_BE,
68 MT_TXQ_BK = IEEE80211_AC_BK,
92 enum mt76_cipher_type {
96 MT_CIPHER_TKIP_NO_MIC,
99 MT_CIPHER_BIP_CMAC_128,
108 enum mt76_dfs_state {
109 MT_DFS_STATE_UNKNOWN,
110 MT_DFS_STATE_DISABLED,
115 struct mt76_queue_buf {
121 struct mt76_tx_info {
122 struct mt76_queue_buf buf[32];
128 struct mt76_queue_entry {
134 struct mt76_txwi_cache *txwi;
146 struct mt76_queue_regs {
151 } __packed __aligned(4);
154 struct mt76_queue_regs __iomem *regs;
157 spinlock_t cleanup_lock;
158 struct mt76_queue_entry *entry;
159 struct mt76_desc *desc;
175 struct sk_buff *rx_head;
176 struct page_frag_cache rx_page;
179 struct mt76_mcu_ops {
183 int (*mcu_send_msg)(struct mt76_dev *dev, int cmd, const void *data,
184 int len, bool wait_resp);
185 int (*mcu_skb_send_msg)(struct mt76_dev *dev, struct sk_buff *skb,
187 int (*mcu_parse_response)(struct mt76_dev *dev, int cmd,
188 struct sk_buff *skb, int seq);
189 u32 (*mcu_rr)(struct mt76_dev *dev, u32 offset);
190 void (*mcu_wr)(struct mt76_dev *dev, u32 offset, u32 val);
191 int (*mcu_wr_rp)(struct mt76_dev *dev, u32 base,
192 const struct mt76_reg_pair *rp, int len);
193 int (*mcu_rd_rp)(struct mt76_dev *dev, u32 base,
194 struct mt76_reg_pair *rp, int len);
195 int (*mcu_restart)(struct mt76_dev *dev);
198 struct mt76_queue_ops {
199 int (*init)(struct mt76_dev *dev,
200 int (*poll)(struct napi_struct *napi, int budget));
202 int (*alloc)(struct mt76_dev *dev, struct mt76_queue *q,
203 int idx, int n_desc, int bufsize,
206 int (*tx_queue_skb)(struct mt76_dev *dev, struct mt76_queue *q,
207 struct sk_buff *skb, struct mt76_wcid *wcid,
208 struct ieee80211_sta *sta);
210 int (*tx_queue_skb_raw)(struct mt76_dev *dev, struct mt76_queue *q,
211 struct sk_buff *skb, u32 tx_info);
213 void *(*dequeue)(struct mt76_dev *dev, struct mt76_queue *q, bool flush,
214 int *len, u32 *info, bool *more);
216 void (*rx_reset)(struct mt76_dev *dev, enum mt76_rxq_id qid);
218 void (*tx_cleanup)(struct mt76_dev *dev, struct mt76_queue *q,
221 void (*rx_cleanup)(struct mt76_dev *dev, struct mt76_queue *q);
223 void (*kick)(struct mt76_dev *dev, struct mt76_queue *q);
225 void (*reset_q)(struct mt76_dev *dev, struct mt76_queue *q);
228 enum mt76_wcid_flags {
229 MT_WCID_FLAG_CHECK_PS,
232 MT_WCID_FLAG_HDR_TRANS,
235 #define MT76_N_WCIDS 544
237 /* stored in ieee80211_tx_info::hw_queue */
238 #define MT_TX_HW_QUEUE_EXT_PHY BIT(3)
240 DECLARE_EWMA(signal, 10, 8);
242 #define MT_WCID_TX_INFO_RATE GENMASK(15, 0)
243 #define MT_WCID_TX_INFO_NSS GENMASK(17, 16)
244 #define MT_WCID_TX_INFO_TXPWR_ADJ GENMASK(25, 18)
245 #define MT_WCID_TX_INFO_SET BIT(31)
248 struct mt76_rx_tid __rcu *aggr[IEEE80211_NUM_TIDS];
250 atomic_t non_aql_packets;
253 struct ewma_signal rssi;
256 struct rate_info rate;
267 u8 rx_key_pn[IEEE80211_NUM_TIDS + 1][6];
273 struct list_head list;
285 struct mt76_txwi_cache {
286 struct list_head list;
293 struct rcu_head rcu_head;
295 struct mt76_dev *dev;
298 struct delayed_work reorder_work;
306 u8 started:1, stopped:1, timer_pending:1;
308 struct sk_buff *reorder_buf[];
311 #define MT_TX_CB_DMA_DONE BIT(0)
312 #define MT_TX_CB_TXS_DONE BIT(1)
313 #define MT_TX_CB_TXS_FAILED BIT(2)
315 #define MT_PACKET_ID_MASK GENMASK(6, 0)
316 #define MT_PACKET_ID_NO_ACK 0
317 #define MT_PACKET_ID_NO_SKB 1
318 #define MT_PACKET_ID_FIRST 2
319 #define MT_PACKET_ID_HAS_RATE BIT(7)
320 /* This is timer for when to give up when waiting for TXS callback,
321 * with starting time being the time at which the DMA_DONE callback
322 * was seen (so, we know packet was processed then, it should not take
323 * long after that for firmware to send the TXS callback if it is going
326 #define MT_TX_STATUS_SKB_TIMEOUT (HZ / 4)
329 unsigned long jiffies;
336 MT76_STATE_INITIALIZED,
338 MT76_STATE_MCU_RUNNING,
341 MT76_HW_SCHED_SCANNING,
347 MT76_STATE_POWER_OFF,
359 #define MT_DRV_TXWI_NO_FREE BIT(0)
360 #define MT_DRV_TX_ALIGNED4_SKBS BIT(1)
361 #define MT_DRV_SW_RX_AIRTIME BIT(2)
362 #define MT_DRV_RX_DMA_HDR BIT(3)
363 #define MT_DRV_HW_MGMT_TXQ BIT(4)
365 struct mt76_driver_ops {
372 void (*update_survey)(struct mt76_phy *phy);
374 int (*tx_prepare_skb)(struct mt76_dev *dev, void *txwi_ptr,
375 enum mt76_txq_id qid, struct mt76_wcid *wcid,
376 struct ieee80211_sta *sta,
377 struct mt76_tx_info *tx_info);
379 void (*tx_complete_skb)(struct mt76_dev *dev,
380 struct mt76_queue_entry *e);
382 bool (*tx_status_data)(struct mt76_dev *dev, u8 *update);
384 bool (*rx_check)(struct mt76_dev *dev, void *data, int len);
386 void (*rx_skb)(struct mt76_dev *dev, enum mt76_rxq_id q,
387 struct sk_buff *skb);
389 void (*rx_poll_complete)(struct mt76_dev *dev, enum mt76_rxq_id q);
391 void (*sta_ps)(struct mt76_dev *dev, struct ieee80211_sta *sta,
394 int (*sta_add)(struct mt76_dev *dev, struct ieee80211_vif *vif,
395 struct ieee80211_sta *sta);
397 void (*sta_assoc)(struct mt76_dev *dev, struct ieee80211_vif *vif,
398 struct ieee80211_sta *sta);
400 void (*sta_remove)(struct mt76_dev *dev, struct ieee80211_vif *vif,
401 struct ieee80211_sta *sta);
404 struct mt76_channel_state {
415 struct ieee80211_supported_band sband;
416 struct mt76_channel_state *chan;
419 struct mt76_rate_power {
433 #define MT_VEND_TYPE_EEPROM BIT(31)
434 #define MT_VEND_TYPE_CFG BIT(30)
435 #define MT_VEND_TYPE_MASK (MT_VEND_TYPE_EEPROM | MT_VEND_TYPE_CFG)
437 #define MT_VEND_ADDR(type, n) (MT_VEND_TYPE_##type | (n))
439 MT_VEND_DEV_MODE = 0x1,
441 MT_VEND_POWER_ON = 0x4,
442 MT_VEND_MULTI_WRITE = 0x6,
443 MT_VEND_MULTI_READ = 0x7,
444 MT_VEND_READ_EEPROM = 0x9,
445 MT_VEND_WRITE_FCE = 0x42,
446 MT_VEND_WRITE_CFG = 0x46,
447 MT_VEND_READ_CFG = 0x47,
448 MT_VEND_READ_EXT = 0x63,
449 MT_VEND_WRITE_EXT = 0x66,
450 MT_VEND_FEATURE_SET = 0x91,
460 MT_EP_OUT_INBAND_CMD,
474 struct sk_buff_head res_q;
475 wait_queue_head_t wait;
478 #define MT_TX_SG_MAX_SIZE 8
479 #define MT_RX_SG_MAX_SIZE 4
480 #define MT_NUM_TX_ENTRIES 256
481 #define MT_NUM_RX_ENTRIES 128
482 #define MCU_RESP_URB_SIZE 1024
484 struct mutex usb_ctrl_mtx;
488 struct mt76_worker status_worker;
489 struct mt76_worker rx_worker;
491 struct work_struct stat_work;
493 u8 out_ep[__MT_EP_OUT_MAX];
494 u8 in_ep[__MT_EP_IN_MAX];
500 struct mt76_reg_pair *rp;
507 #define MT76S_XMIT_BUF_SZ 0x3fe00
508 #define MT76S_NUM_TX_ENTRIES 256
509 #define MT76S_NUM_RX_ENTRIES 512
511 struct mt76_worker txrx_worker;
512 struct mt76_worker status_worker;
513 struct mt76_worker net_worker;
515 struct work_struct stat_work;
520 struct sdio_func *func;
523 wait_queue_head_t wait;
533 int (*parse_irq)(struct mt76_dev *dev, struct mt76s_intr *intr);
542 struct mt76_rx_status {
544 struct mt76_wcid *wcid;
563 u8 encoding:2, bw:3, he_ru:3;
564 u8 he_gi:2, he_dcm:1;
565 u8 amsdu:1, first_amsdu:1, last_amsdu:1;
571 s8 chain_signal[IEEE80211_MAX_CHAINS];
574 struct mt76_freq_range_power {
575 const struct cfg80211_sar_freq_ranges *range;
579 struct mt76_testmode_ops {
580 int (*set_state)(struct mt76_phy *phy, enum mt76_testmode_state state);
581 int (*set_params)(struct mt76_phy *phy, struct nlattr **tb,
582 enum mt76_testmode_state new_state);
583 int (*dump_stats)(struct mt76_phy *phy, struct sk_buff *msg);
586 struct mt76_testmode_data {
587 enum mt76_testmode_state state;
589 u32 param_set[DIV_ROUND_UP(NUM_MT76_TM_ATTRS, 32)];
590 struct sk_buff *tx_skb;
615 u8 addr[3][ETH_ALEN];
622 u64 packets[__MT_RXQ_MAX];
623 u64 fcs_error[__MT_RXQ_MAX];
637 struct ieee80211_hw *hw;
638 struct mt76_dev *dev;
643 struct mt76_queue *q_tx[__MT_TXQ_MAX];
645 struct cfg80211_chan_def chandef;
646 struct ieee80211_channel *main_chan;
648 struct mt76_channel_state *chan_state;
649 enum mt76_dfs_state dfs_state;
652 struct mt76_hw_cap cap;
653 struct mt76_sband sband_2g;
654 struct mt76_sband sband_5g;
655 struct mt76_sband sband_6g;
657 u8 macaddr[ETH_ALEN];
663 #ifdef CONFIG_NL80211_TESTMODE
664 struct mt76_testmode_data test;
667 struct delayed_work mac_work;
671 struct sk_buff *head;
672 struct sk_buff **tail;
674 } rx_amsdu[__MT_RXQ_MAX];
676 struct mt76_freq_range_power *frp;
680 struct mt76_phy phy; /* must be first */
682 struct mt76_phy *phy2;
684 struct ieee80211_hw *hw;
691 struct mt76_rx_status rx_ampdu_status;
697 const struct mt76_bus_ops *bus;
698 const struct mt76_driver_ops *drv;
699 const struct mt76_mcu_ops *mcu_ops;
704 struct net_device napi_dev;
705 struct net_device tx_napi_dev;
707 struct napi_struct napi[__MT_RXQ_MAX];
708 struct sk_buff_head rx_skb[__MT_RXQ_MAX];
710 struct list_head txwi_cache;
711 struct mt76_queue *q_mcu[__MT_MCUQ_MAX];
712 struct mt76_queue q_rx[__MT_RXQ_MAX];
713 const struct mt76_queue_ops *queue_ops;
716 struct mt76_worker tx_worker;
717 struct napi_struct tx_napi;
719 spinlock_t token_lock;
723 wait_queue_head_t tx_wait;
724 /* spinclock used to protect wcid pktid linked list */
725 spinlock_t status_lock;
727 u32 wcid_mask[DIV_ROUND_UP(MT76_N_WCIDS, 32)];
728 u32 wcid_phy_mask[DIV_ROUND_UP(MT76_N_WCIDS, 32)];
732 struct mt76_wcid global_wcid;
733 struct mt76_wcid __rcu *wcid[MT76_N_WCIDS];
734 struct list_head wcid_list;
740 struct tasklet_struct pre_tbtt_tasklet;
744 struct debugfs_blob_wrapper eeprom;
745 struct debugfs_blob_wrapper otp;
747 struct mt76_rate_power rate_power;
750 enum nl80211_dfs_regions region;
754 struct led_classdev led_cdev;
763 #ifdef CONFIG_NL80211_TESTMODE
764 const struct mt76_testmode_ops *test_ops;
770 struct workqueue_struct *wq;
773 struct mt76_mmio mmio;
775 struct mt76_sdio sdio;
779 struct mt76_power_limits {
792 MT_PHY_TYPE_HE_SU = 8,
793 MT_PHY_TYPE_HE_EXT_SU,
796 __MT_PHY_TYPE_HE_MAX,
799 struct mt76_sta_stats {
800 u64 tx_mode[__MT_PHY_TYPE_HE_MAX];
801 u64 tx_bw[4]; /* 20, 40, 80, 160 */
802 u64 tx_nss[4]; /* 1, 2, 3, 4 */
803 u64 tx_mcs[16]; /* mcs idx */
806 struct mt76_ethtool_worker_info {
809 int initial_stat_idx;
810 int worker_stat_count;
814 #define CCK_RATE(_idx, _rate) { \
816 .flags = IEEE80211_RATE_SHORT_PREAMBLE, \
817 .hw_value = (MT_PHY_TYPE_CCK << 8) | (_idx), \
818 .hw_value_short = (MT_PHY_TYPE_CCK << 8) | (4 + _idx), \
821 #define OFDM_RATE(_idx, _rate) { \
823 .hw_value = (MT_PHY_TYPE_OFDM << 8) | (_idx), \
824 .hw_value_short = (MT_PHY_TYPE_OFDM << 8) | (_idx), \
827 extern struct ieee80211_rate mt76_rates[12];
829 #define __mt76_rr(dev, ...) (dev)->bus->rr((dev), __VA_ARGS__)
830 #define __mt76_wr(dev, ...) (dev)->bus->wr((dev), __VA_ARGS__)
831 #define __mt76_rmw(dev, ...) (dev)->bus->rmw((dev), __VA_ARGS__)
832 #define __mt76_wr_copy(dev, ...) (dev)->bus->write_copy((dev), __VA_ARGS__)
833 #define __mt76_rr_copy(dev, ...) (dev)->bus->read_copy((dev), __VA_ARGS__)
835 #define __mt76_set(dev, offset, val) __mt76_rmw(dev, offset, 0, val)
836 #define __mt76_clear(dev, offset, val) __mt76_rmw(dev, offset, val, 0)
838 #define mt76_rr(dev, ...) (dev)->mt76.bus->rr(&((dev)->mt76), __VA_ARGS__)
839 #define mt76_wr(dev, ...) (dev)->mt76.bus->wr(&((dev)->mt76), __VA_ARGS__)
840 #define mt76_rmw(dev, ...) (dev)->mt76.bus->rmw(&((dev)->mt76), __VA_ARGS__)
841 #define mt76_wr_copy(dev, ...) (dev)->mt76.bus->write_copy(&((dev)->mt76), __VA_ARGS__)
842 #define mt76_rr_copy(dev, ...) (dev)->mt76.bus->read_copy(&((dev)->mt76), __VA_ARGS__)
843 #define mt76_wr_rp(dev, ...) (dev)->mt76.bus->wr_rp(&((dev)->mt76), __VA_ARGS__)
844 #define mt76_rd_rp(dev, ...) (dev)->mt76.bus->rd_rp(&((dev)->mt76), __VA_ARGS__)
847 #define mt76_mcu_restart(dev, ...) (dev)->mt76.mcu_ops->mcu_restart(&((dev)->mt76))
848 #define __mt76_mcu_restart(dev, ...) (dev)->mcu_ops->mcu_restart((dev))
850 #define mt76_set(dev, offset, val) mt76_rmw(dev, offset, 0, val)
851 #define mt76_clear(dev, offset, val) mt76_rmw(dev, offset, val, 0)
853 #define mt76_get_field(_dev, _reg, _field) \
854 FIELD_GET(_field, mt76_rr(dev, _reg))
856 #define mt76_rmw_field(_dev, _reg, _field, _val) \
857 mt76_rmw(_dev, _reg, _field, FIELD_PREP(_field, _val))
859 #define __mt76_rmw_field(_dev, _reg, _field, _val) \
860 __mt76_rmw(_dev, _reg, _field, FIELD_PREP(_field, _val))
862 #define mt76_hw(dev) (dev)->mphy.hw
864 static inline struct ieee80211_hw *
865 mt76_wcid_hw(struct mt76_dev *dev, u16 wcid)
867 if (wcid <= MT76_N_WCIDS &&
868 mt76_wcid_mask_test(dev->wcid_phy_mask, wcid))
869 return dev->phy2->hw;
874 bool __mt76_poll(struct mt76_dev *dev, u32 offset, u32 mask, u32 val,
877 #define mt76_poll(dev, ...) __mt76_poll(&((dev)->mt76), __VA_ARGS__)
879 bool __mt76_poll_msec(struct mt76_dev *dev, u32 offset, u32 mask, u32 val,
882 #define mt76_poll_msec(dev, ...) __mt76_poll_msec(&((dev)->mt76), __VA_ARGS__)
884 void mt76_mmio_init(struct mt76_dev *dev, void __iomem *regs);
885 void mt76_pci_disable_aspm(struct pci_dev *pdev);
887 static inline u16 mt76_chip(struct mt76_dev *dev)
889 return dev->rev >> 16;
892 static inline u16 mt76_rev(struct mt76_dev *dev)
894 return dev->rev & 0xffff;
897 #define mt76xx_chip(dev) mt76_chip(&((dev)->mt76))
898 #define mt76xx_rev(dev) mt76_rev(&((dev)->mt76))
900 #define mt76_init_queues(dev, ...) (dev)->mt76.queue_ops->init(&((dev)->mt76), __VA_ARGS__)
901 #define mt76_queue_alloc(dev, ...) (dev)->mt76.queue_ops->alloc(&((dev)->mt76), __VA_ARGS__)
902 #define mt76_tx_queue_skb_raw(dev, ...) (dev)->mt76.queue_ops->tx_queue_skb_raw(&((dev)->mt76), __VA_ARGS__)
903 #define mt76_tx_queue_skb(dev, ...) (dev)->mt76.queue_ops->tx_queue_skb(&((dev)->mt76), __VA_ARGS__)
904 #define mt76_queue_rx_reset(dev, ...) (dev)->mt76.queue_ops->rx_reset(&((dev)->mt76), __VA_ARGS__)
905 #define mt76_queue_tx_cleanup(dev, ...) (dev)->mt76.queue_ops->tx_cleanup(&((dev)->mt76), __VA_ARGS__)
906 #define mt76_queue_rx_cleanup(dev, ...) (dev)->mt76.queue_ops->rx_cleanup(&((dev)->mt76), __VA_ARGS__)
907 #define mt76_queue_kick(dev, ...) (dev)->mt76.queue_ops->kick(&((dev)->mt76), __VA_ARGS__)
908 #define mt76_queue_reset(dev, ...) (dev)->mt76.queue_ops->reset_q(&((dev)->mt76), __VA_ARGS__)
910 #define mt76_for_each_q_rx(dev, i) \
911 for (i = 0; i < ARRAY_SIZE((dev)->q_rx); i++) \
912 if ((dev)->q_rx[i].ndesc)
914 struct mt76_dev *mt76_alloc_device(struct device *pdev, unsigned int size,
915 const struct ieee80211_ops *ops,
916 const struct mt76_driver_ops *drv_ops);
917 int mt76_register_device(struct mt76_dev *dev, bool vht,
918 struct ieee80211_rate *rates, int n_rates);
919 void mt76_unregister_device(struct mt76_dev *dev);
920 void mt76_free_device(struct mt76_dev *dev);
921 void mt76_unregister_phy(struct mt76_phy *phy);
923 struct mt76_phy *mt76_alloc_phy(struct mt76_dev *dev, unsigned int size,
924 const struct ieee80211_ops *ops);
925 int mt76_register_phy(struct mt76_phy *phy, bool vht,
926 struct ieee80211_rate *rates, int n_rates);
928 struct dentry *mt76_register_debugfs_fops(struct mt76_phy *phy,
929 const struct file_operations *ops);
930 static inline struct dentry *mt76_register_debugfs(struct mt76_dev *dev)
932 return mt76_register_debugfs_fops(&dev->phy, NULL);
935 int mt76_queues_read(struct seq_file *s, void *data);
936 void mt76_seq_puts_array(struct seq_file *file, const char *str,
939 int mt76_eeprom_init(struct mt76_dev *dev, int len);
940 void mt76_eeprom_override(struct mt76_phy *phy);
941 int mt76_get_of_eeprom(struct mt76_dev *dev, void *data, int offset, int len);
944 mt76_init_queue(struct mt76_dev *dev, int qid, int idx, int n_desc,
946 u16 mt76_calculate_default_rate(struct mt76_phy *phy, int rateidx);
947 static inline int mt76_init_tx_queue(struct mt76_phy *phy, int qid, int idx,
948 int n_desc, int ring_base)
950 struct mt76_queue *q;
952 q = mt76_init_queue(phy->dev, qid, idx, n_desc, ring_base);
962 static inline int mt76_init_mcu_queue(struct mt76_dev *dev, int qid, int idx,
963 int n_desc, int ring_base)
965 struct mt76_queue *q;
967 q = mt76_init_queue(dev, qid, idx, n_desc, ring_base);
971 q->qid = __MT_TXQ_MAX + qid;
977 static inline struct mt76_phy *
978 mt76_dev_phy(struct mt76_dev *dev, bool phy_ext)
980 if (phy_ext && dev->phy2)
985 static inline struct ieee80211_hw *
986 mt76_phy_hw(struct mt76_dev *dev, bool phy_ext)
988 return mt76_dev_phy(dev, phy_ext)->hw;
992 mt76_get_txwi_ptr(struct mt76_dev *dev, struct mt76_txwi_cache *t)
994 return (u8 *)t - dev->drv->txwi_size;
997 /* increment with wrap-around */
998 static inline int mt76_incr(int val, int size)
1000 return (val + 1) & (size - 1);
1003 /* decrement with wrap-around */
1004 static inline int mt76_decr(int val, int size)
1006 return (val - 1) & (size - 1);
1009 u8 mt76_ac_to_hwq(u8 ac);
1011 static inline struct ieee80211_txq *
1012 mtxq_to_txq(struct mt76_txq *mtxq)
1016 return container_of(ptr, struct ieee80211_txq, drv_priv);
1019 static inline struct ieee80211_sta *
1020 wcid_to_sta(struct mt76_wcid *wcid)
1024 if (!wcid || !wcid->sta)
1027 return container_of(ptr, struct ieee80211_sta, drv_priv);
1030 static inline struct mt76_tx_cb *mt76_tx_skb_cb(struct sk_buff *skb)
1032 BUILD_BUG_ON(sizeof(struct mt76_tx_cb) >
1033 sizeof(IEEE80211_SKB_CB(skb)->status.status_driver_data));
1034 return ((void *)IEEE80211_SKB_CB(skb)->status.status_driver_data);
1037 static inline void *mt76_skb_get_hdr(struct sk_buff *skb)
1039 struct mt76_rx_status mstat;
1040 u8 *data = skb->data;
1042 /* Alignment concerns */
1043 BUILD_BUG_ON(sizeof(struct ieee80211_radiotap_he) % 4);
1044 BUILD_BUG_ON(sizeof(struct ieee80211_radiotap_he_mu) % 4);
1046 mstat = *((struct mt76_rx_status *)skb->cb);
1048 if (mstat.flag & RX_FLAG_RADIOTAP_HE)
1049 data += sizeof(struct ieee80211_radiotap_he);
1050 if (mstat.flag & RX_FLAG_RADIOTAP_HE_MU)
1051 data += sizeof(struct ieee80211_radiotap_he_mu);
1056 static inline void mt76_insert_hdr_pad(struct sk_buff *skb)
1058 int len = ieee80211_get_hdrlen_from_skb(skb);
1064 memmove(skb->data, skb->data + 2, len);
1067 skb->data[len + 1] = 0;
1070 static inline bool mt76_is_skb_pktid(u8 pktid)
1072 if (pktid & MT_PACKET_ID_HAS_RATE)
1075 return pktid >= MT_PACKET_ID_FIRST;
1078 static inline u8 mt76_tx_power_nss_delta(u8 nss)
1080 static const u8 nss_delta[4] = { 0, 6, 9, 12 };
1082 return nss_delta[nss - 1];
1085 static inline bool mt76_testmode_enabled(struct mt76_phy *phy)
1087 #ifdef CONFIG_NL80211_TESTMODE
1088 return phy->test.state != MT76_TM_STATE_OFF;
1094 static inline bool mt76_is_testmode_skb(struct mt76_dev *dev,
1095 struct sk_buff *skb,
1096 struct ieee80211_hw **hw)
1098 #ifdef CONFIG_NL80211_TESTMODE
1099 if (skb == dev->phy.test.tx_skb)
1101 else if (dev->phy2 && skb == dev->phy2->test.tx_skb)
1102 *hw = dev->phy2->hw;
1111 void mt76_rx(struct mt76_dev *dev, enum mt76_rxq_id q, struct sk_buff *skb);
1112 void mt76_tx(struct mt76_phy *dev, struct ieee80211_sta *sta,
1113 struct mt76_wcid *wcid, struct sk_buff *skb);
1114 void mt76_wake_tx_queue(struct ieee80211_hw *hw, struct ieee80211_txq *txq);
1115 void mt76_stop_tx_queues(struct mt76_phy *phy, struct ieee80211_sta *sta,
1117 void mt76_tx_check_agg_ssn(struct ieee80211_sta *sta, struct sk_buff *skb);
1118 void mt76_txq_schedule(struct mt76_phy *phy, enum mt76_txq_id qid);
1119 void mt76_txq_schedule_all(struct mt76_phy *phy);
1120 void mt76_tx_worker_run(struct mt76_dev *dev);
1121 void mt76_tx_worker(struct mt76_worker *w);
1122 void mt76_release_buffered_frames(struct ieee80211_hw *hw,
1123 struct ieee80211_sta *sta,
1124 u16 tids, int nframes,
1125 enum ieee80211_frame_release_type reason,
1127 bool mt76_has_tx_pending(struct mt76_phy *phy);
1128 void mt76_set_channel(struct mt76_phy *phy);
1129 void mt76_update_survey(struct mt76_phy *phy);
1130 void mt76_update_survey_active_time(struct mt76_phy *phy, ktime_t time);
1131 int mt76_get_survey(struct ieee80211_hw *hw, int idx,
1132 struct survey_info *survey);
1133 void mt76_set_stream_caps(struct mt76_phy *phy, bool vht);
1135 int mt76_rx_aggr_start(struct mt76_dev *dev, struct mt76_wcid *wcid, u8 tid,
1137 void mt76_rx_aggr_stop(struct mt76_dev *dev, struct mt76_wcid *wcid, u8 tid);
1139 void mt76_wcid_key_setup(struct mt76_dev *dev, struct mt76_wcid *wcid,
1140 struct ieee80211_key_conf *key);
1142 void mt76_tx_status_lock(struct mt76_dev *dev, struct sk_buff_head *list)
1143 __acquires(&dev->status_lock);
1144 void mt76_tx_status_unlock(struct mt76_dev *dev, struct sk_buff_head *list)
1145 __releases(&dev->status_lock);
1147 int mt76_tx_status_skb_add(struct mt76_dev *dev, struct mt76_wcid *wcid,
1148 struct sk_buff *skb);
1149 struct sk_buff *mt76_tx_status_skb_get(struct mt76_dev *dev,
1150 struct mt76_wcid *wcid, int pktid,
1151 struct sk_buff_head *list);
1152 void mt76_tx_status_skb_done(struct mt76_dev *dev, struct sk_buff *skb,
1153 struct sk_buff_head *list);
1154 void __mt76_tx_complete_skb(struct mt76_dev *dev, u16 wcid, struct sk_buff *skb,
1155 struct list_head *free_list);
1157 mt76_tx_complete_skb(struct mt76_dev *dev, u16 wcid, struct sk_buff *skb)
1159 __mt76_tx_complete_skb(dev, wcid, skb, NULL);
1162 void mt76_tx_status_check(struct mt76_dev *dev, bool flush);
1163 int mt76_sta_state(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1164 struct ieee80211_sta *sta,
1165 enum ieee80211_sta_state old_state,
1166 enum ieee80211_sta_state new_state);
1167 void __mt76_sta_remove(struct mt76_dev *dev, struct ieee80211_vif *vif,
1168 struct ieee80211_sta *sta);
1169 void mt76_sta_pre_rcu_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1170 struct ieee80211_sta *sta);
1172 int mt76_get_min_avg_rssi(struct mt76_dev *dev, bool ext_phy);
1174 int mt76_get_txpower(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1176 int mt76_init_sar_power(struct ieee80211_hw *hw,
1177 const struct cfg80211_sar_specs *sar);
1178 int mt76_get_sar_power(struct mt76_phy *phy,
1179 struct ieee80211_channel *chan,
1182 void mt76_csa_check(struct mt76_dev *dev);
1183 void mt76_csa_finish(struct mt76_dev *dev);
1185 int mt76_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant);
1186 int mt76_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta, bool set);
1187 void mt76_insert_ccmp_hdr(struct sk_buff *skb, u8 key_id);
1188 int mt76_get_rate(struct mt76_dev *dev,
1189 struct ieee80211_supported_band *sband,
1191 void mt76_sw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1193 void mt76_sw_scan_complete(struct ieee80211_hw *hw,
1194 struct ieee80211_vif *vif);
1195 enum mt76_dfs_state mt76_phy_dfs_state(struct mt76_phy *phy);
1196 int mt76_testmode_cmd(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1197 void *data, int len);
1198 int mt76_testmode_dump(struct ieee80211_hw *hw, struct sk_buff *skb,
1199 struct netlink_callback *cb, void *data, int len);
1200 int mt76_testmode_set_state(struct mt76_phy *phy, enum mt76_testmode_state state);
1201 int mt76_testmode_alloc_skb(struct mt76_phy *phy, u32 len);
1203 static inline void mt76_testmode_reset(struct mt76_phy *phy, bool disable)
1205 #ifdef CONFIG_NL80211_TESTMODE
1206 enum mt76_testmode_state state = MT76_TM_STATE_IDLE;
1208 if (disable || phy->test.state == MT76_TM_STATE_OFF)
1209 state = MT76_TM_STATE_OFF;
1211 mt76_testmode_set_state(phy, state);
1217 static inline struct ieee80211_hw *
1218 mt76_tx_status_get_hw(struct mt76_dev *dev, struct sk_buff *skb)
1220 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1221 struct ieee80211_hw *hw = dev->phy.hw;
1223 if ((info->hw_queue & MT_TX_HW_QUEUE_EXT_PHY) && dev->phy2)
1226 info->hw_queue &= ~MT_TX_HW_QUEUE_EXT_PHY;
1231 void mt76_put_txwi(struct mt76_dev *dev, struct mt76_txwi_cache *t);
1232 void mt76_rx_complete(struct mt76_dev *dev, struct sk_buff_head *frames,
1233 struct napi_struct *napi);
1234 void mt76_rx_poll_complete(struct mt76_dev *dev, enum mt76_rxq_id q,
1235 struct napi_struct *napi);
1236 void mt76_rx_aggr_reorder(struct sk_buff *skb, struct sk_buff_head *frames);
1237 void mt76_testmode_tx_pending(struct mt76_phy *phy);
1238 void mt76_queue_tx_complete(struct mt76_dev *dev, struct mt76_queue *q,
1239 struct mt76_queue_entry *e);
1242 static inline bool mt76u_urb_error(struct urb *urb)
1244 return urb->status &&
1245 urb->status != -ECONNRESET &&
1246 urb->status != -ESHUTDOWN &&
1247 urb->status != -ENOENT;
1250 /* Map hardware queues to usb endpoints */
1251 static inline u8 q2ep(u8 qid)
1253 /* TODO: take management packets to queue 5 */
1258 mt76u_bulk_msg(struct mt76_dev *dev, void *data, int len, int *actual_len,
1259 int timeout, int ep)
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;
1267 pipe = usb_rcvbulkpipe(udev, usb->in_ep[ep]);
1269 pipe = usb_sndbulkpipe(udev, usb->out_ep[ep]);
1271 return usb_bulk_msg(udev, pipe, data, len, actual_len, timeout);
1274 void mt76_ethtool_worker(struct mt76_ethtool_worker_info *wi,
1275 struct mt76_sta_stats *stats);
1276 int mt76_skb_adjust_pad(struct sk_buff *skb, int pad);
1277 int __mt76u_vendor_request(struct mt76_dev *dev, u8 req, u8 req_type,
1278 u16 val, u16 offset, void *buf, size_t len);
1279 int mt76u_vendor_request(struct mt76_dev *dev, u8 req,
1280 u8 req_type, u16 val, u16 offset,
1281 void *buf, size_t len);
1282 void mt76u_single_wr(struct mt76_dev *dev, const u8 req,
1283 const u16 offset, const u32 val);
1284 void mt76u_read_copy(struct mt76_dev *dev, u32 offset,
1285 void *data, int len);
1286 u32 ___mt76u_rr(struct mt76_dev *dev, u8 req, u8 req_type, u32 addr);
1287 void ___mt76u_wr(struct mt76_dev *dev, u8 req, u8 req_type,
1289 int __mt76u_init(struct mt76_dev *dev, struct usb_interface *intf,
1290 struct mt76_bus_ops *ops);
1291 int mt76u_init(struct mt76_dev *dev, struct usb_interface *intf);
1292 int mt76u_alloc_mcu_queue(struct mt76_dev *dev);
1293 int mt76u_alloc_queues(struct mt76_dev *dev);
1294 void mt76u_stop_tx(struct mt76_dev *dev);
1295 void mt76u_stop_rx(struct mt76_dev *dev);
1296 int mt76u_resume_rx(struct mt76_dev *dev);
1297 void mt76u_queues_deinit(struct mt76_dev *dev);
1299 int mt76s_init(struct mt76_dev *dev, struct sdio_func *func,
1300 const struct mt76_bus_ops *bus_ops);
1301 int mt76s_alloc_rx_queue(struct mt76_dev *dev, enum mt76_rxq_id qid);
1302 int mt76s_alloc_tx(struct mt76_dev *dev);
1303 void mt76s_deinit(struct mt76_dev *dev);
1304 void mt76s_sdio_irq(struct sdio_func *func);
1305 void mt76s_txrx_worker(struct mt76_sdio *sdio);
1306 bool mt76s_txqs_empty(struct mt76_dev *dev);
1307 int mt76s_hw_init(struct mt76_dev *dev, struct sdio_func *func,
1309 u32 mt76s_rr(struct mt76_dev *dev, u32 offset);
1310 void mt76s_wr(struct mt76_dev *dev, u32 offset, u32 val);
1311 u32 mt76s_rmw(struct mt76_dev *dev, u32 offset, u32 mask, u32 val);
1312 u32 mt76s_read_pcr(struct mt76_dev *dev);
1313 void mt76s_write_copy(struct mt76_dev *dev, u32 offset,
1314 const void *data, int len);
1315 void mt76s_read_copy(struct mt76_dev *dev, u32 offset,
1316 void *data, int len);
1317 int mt76s_wr_rp(struct mt76_dev *dev, u32 base,
1318 const struct mt76_reg_pair *data,
1320 int mt76s_rd_rp(struct mt76_dev *dev, u32 base,
1321 struct mt76_reg_pair *data, int len);
1324 mt76_mcu_msg_alloc(struct mt76_dev *dev, const void *data,
1326 void mt76_mcu_rx_event(struct mt76_dev *dev, struct sk_buff *skb);
1327 struct sk_buff *mt76_mcu_get_response(struct mt76_dev *dev,
1328 unsigned long expires);
1329 int mt76_mcu_send_and_get_msg(struct mt76_dev *dev, int cmd, const void *data,
1330 int len, bool wait_resp, struct sk_buff **ret);
1331 int mt76_mcu_skb_send_and_get_msg(struct mt76_dev *dev, struct sk_buff *skb,
1332 int cmd, bool wait_resp, struct sk_buff **ret);
1333 int __mt76_mcu_send_firmware(struct mt76_dev *dev, int cmd, const void *data,
1334 int len, int max_len);
1336 mt76_mcu_send_firmware(struct mt76_dev *dev, int cmd, const void *data,
1339 int max_len = 4096 - dev->mcu_ops->headroom;
1341 return __mt76_mcu_send_firmware(dev, cmd, data, len, max_len);
1345 mt76_mcu_send_msg(struct mt76_dev *dev, int cmd, const void *data, int len,
1348 return mt76_mcu_send_and_get_msg(dev, cmd, data, len, wait_resp, NULL);
1352 mt76_mcu_skb_send_msg(struct mt76_dev *dev, struct sk_buff *skb, int cmd,
1355 return mt76_mcu_skb_send_and_get_msg(dev, skb, cmd, wait_resp, NULL);
1358 void mt76_set_irq_mask(struct mt76_dev *dev, u32 addr, u32 clear, u32 set);
1360 s8 mt76_get_rate_power_limits(struct mt76_phy *phy,
1361 struct ieee80211_channel *chan,
1362 struct mt76_power_limits *dest,
1365 struct mt76_txwi_cache *
1366 mt76_token_release(struct mt76_dev *dev, int token, bool *wake);
1367 int mt76_token_consume(struct mt76_dev *dev, struct mt76_txwi_cache **ptxwi);
1368 void __mt76_set_tx_blocked(struct mt76_dev *dev, bool blocked);
1370 static inline void mt76_set_tx_blocked(struct mt76_dev *dev, bool blocked)
1372 spin_lock_bh(&dev->token_lock);
1373 __mt76_set_tx_blocked(dev, blocked);
1374 spin_unlock_bh(&dev->token_lock);
1378 mt76_token_get(struct mt76_dev *dev, struct mt76_txwi_cache **ptxwi)
1382 spin_lock_bh(&dev->token_lock);
1383 token = idr_alloc(&dev->token, *ptxwi, 0, dev->drv->token_size,
1385 spin_unlock_bh(&dev->token_lock);
1390 static inline struct mt76_txwi_cache *
1391 mt76_token_put(struct mt76_dev *dev, int token)
1393 struct mt76_txwi_cache *txwi;
1395 spin_lock_bh(&dev->token_lock);
1396 txwi = idr_remove(&dev->token, token);
1397 spin_unlock_bh(&dev->token_lock);
1402 static inline void mt76_packet_id_init(struct mt76_wcid *wcid)
1404 INIT_LIST_HEAD(&wcid->list);
1405 idr_init(&wcid->pktid);
1409 mt76_packet_id_flush(struct mt76_dev *dev, struct mt76_wcid *wcid)
1411 struct sk_buff_head list;
1413 mt76_tx_status_lock(dev, &list);
1414 mt76_tx_status_skb_get(dev, wcid, -1, &list);
1415 mt76_tx_status_unlock(dev, &list);
1417 idr_destroy(&wcid->pktid);