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 128
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,
91 enum mt76_cipher_type {
95 MT_CIPHER_TKIP_NO_MIC,
98 MT_CIPHER_BIP_CMAC_128,
107 struct mt76_queue_buf {
113 struct mt76_tx_info {
114 struct mt76_queue_buf buf[32];
120 struct mt76_queue_entry {
126 struct mt76_txwi_cache *txwi;
138 struct mt76_queue_regs {
143 } __packed __aligned(4);
146 struct mt76_queue_regs __iomem *regs;
149 spinlock_t cleanup_lock;
150 struct mt76_queue_entry *entry;
151 struct mt76_desc *desc;
167 struct sk_buff *rx_head;
168 struct page_frag_cache rx_page;
171 struct mt76_mcu_ops {
175 int (*mcu_send_msg)(struct mt76_dev *dev, int cmd, const void *data,
176 int len, bool wait_resp);
177 int (*mcu_skb_send_msg)(struct mt76_dev *dev, struct sk_buff *skb,
179 int (*mcu_parse_response)(struct mt76_dev *dev, int cmd,
180 struct sk_buff *skb, int seq);
181 u32 (*mcu_rr)(struct mt76_dev *dev, u32 offset);
182 void (*mcu_wr)(struct mt76_dev *dev, u32 offset, u32 val);
183 int (*mcu_wr_rp)(struct mt76_dev *dev, u32 base,
184 const struct mt76_reg_pair *rp, int len);
185 int (*mcu_rd_rp)(struct mt76_dev *dev, u32 base,
186 struct mt76_reg_pair *rp, int len);
187 int (*mcu_restart)(struct mt76_dev *dev);
190 struct mt76_queue_ops {
191 int (*init)(struct mt76_dev *dev,
192 int (*poll)(struct napi_struct *napi, int budget));
194 int (*alloc)(struct mt76_dev *dev, struct mt76_queue *q,
195 int idx, int n_desc, int bufsize,
198 int (*tx_queue_skb)(struct mt76_dev *dev, struct mt76_queue *q,
199 struct sk_buff *skb, struct mt76_wcid *wcid,
200 struct ieee80211_sta *sta);
202 int (*tx_queue_skb_raw)(struct mt76_dev *dev, struct mt76_queue *q,
203 struct sk_buff *skb, u32 tx_info);
205 void *(*dequeue)(struct mt76_dev *dev, struct mt76_queue *q, bool flush,
206 int *len, u32 *info, bool *more);
208 void (*rx_reset)(struct mt76_dev *dev, enum mt76_rxq_id qid);
210 void (*tx_cleanup)(struct mt76_dev *dev, struct mt76_queue *q,
213 void (*rx_cleanup)(struct mt76_dev *dev, struct mt76_queue *q);
215 void (*kick)(struct mt76_dev *dev, struct mt76_queue *q);
217 void (*reset_q)(struct mt76_dev *dev, struct mt76_queue *q);
220 enum mt76_wcid_flags {
221 MT_WCID_FLAG_CHECK_PS,
224 MT_WCID_FLAG_HDR_TRANS,
227 #define MT76_N_WCIDS 288
229 /* stored in ieee80211_tx_info::hw_queue */
230 #define MT_TX_HW_QUEUE_EXT_PHY BIT(3)
232 DECLARE_EWMA(signal, 10, 8);
234 #define MT_WCID_TX_INFO_RATE GENMASK(15, 0)
235 #define MT_WCID_TX_INFO_NSS GENMASK(17, 16)
236 #define MT_WCID_TX_INFO_TXPWR_ADJ GENMASK(25, 18)
237 #define MT_WCID_TX_INFO_SET BIT(31)
240 struct mt76_rx_tid __rcu *aggr[IEEE80211_NUM_TIDS];
242 atomic_t non_aql_packets;
245 struct ewma_signal rssi;
248 struct rate_info rate;
259 u8 rx_key_pn[IEEE80211_NUM_TIDS + 1][6];
265 struct list_head list;
270 struct mt76_wcid *wcid;
277 struct mt76_txwi_cache {
278 struct list_head list;
285 struct rcu_head rcu_head;
287 struct mt76_dev *dev;
290 struct delayed_work reorder_work;
298 u8 started:1, stopped:1, timer_pending:1;
300 struct sk_buff *reorder_buf[];
303 #define MT_TX_CB_DMA_DONE BIT(0)
304 #define MT_TX_CB_TXS_DONE BIT(1)
305 #define MT_TX_CB_TXS_FAILED BIT(2)
307 #define MT_PACKET_ID_MASK GENMASK(6, 0)
308 #define MT_PACKET_ID_NO_ACK 0
309 #define MT_PACKET_ID_NO_SKB 1
310 #define MT_PACKET_ID_FIRST 2
311 #define MT_PACKET_ID_HAS_RATE BIT(7)
312 /* This is timer for when to give up when waiting for TXS callback,
313 * with starting time being the time at which the DMA_DONE callback
314 * was seen (so, we know packet was processed then, it should not take
315 * long after that for firmware to send the TXS callback if it is going
318 #define MT_TX_STATUS_SKB_TIMEOUT (HZ / 4)
321 unsigned long jiffies;
328 MT76_STATE_INITIALIZED,
330 MT76_STATE_MCU_RUNNING,
333 MT76_HW_SCHED_SCANNING,
339 MT76_STATE_POWER_OFF,
351 #define MT_DRV_TXWI_NO_FREE BIT(0)
352 #define MT_DRV_TX_ALIGNED4_SKBS BIT(1)
353 #define MT_DRV_SW_RX_AIRTIME BIT(2)
354 #define MT_DRV_RX_DMA_HDR BIT(3)
355 #define MT_DRV_HW_MGMT_TXQ BIT(4)
357 struct mt76_driver_ops {
364 void (*update_survey)(struct mt76_phy *phy);
366 int (*tx_prepare_skb)(struct mt76_dev *dev, void *txwi_ptr,
367 enum mt76_txq_id qid, struct mt76_wcid *wcid,
368 struct ieee80211_sta *sta,
369 struct mt76_tx_info *tx_info);
371 void (*tx_complete_skb)(struct mt76_dev *dev,
372 struct mt76_queue_entry *e);
374 bool (*tx_status_data)(struct mt76_dev *dev, u8 *update);
376 bool (*rx_check)(struct mt76_dev *dev, void *data, int len);
378 void (*rx_skb)(struct mt76_dev *dev, enum mt76_rxq_id q,
379 struct sk_buff *skb);
381 void (*rx_poll_complete)(struct mt76_dev *dev, enum mt76_rxq_id q);
383 void (*sta_ps)(struct mt76_dev *dev, struct ieee80211_sta *sta,
386 int (*sta_add)(struct mt76_dev *dev, struct ieee80211_vif *vif,
387 struct ieee80211_sta *sta);
389 void (*sta_assoc)(struct mt76_dev *dev, struct ieee80211_vif *vif,
390 struct ieee80211_sta *sta);
392 void (*sta_remove)(struct mt76_dev *dev, struct ieee80211_vif *vif,
393 struct ieee80211_sta *sta);
396 struct mt76_channel_state {
407 struct ieee80211_supported_band sband;
408 struct mt76_channel_state *chan;
411 struct mt76_rate_power {
425 #define MT_VEND_TYPE_EEPROM BIT(31)
426 #define MT_VEND_TYPE_CFG BIT(30)
427 #define MT_VEND_TYPE_MASK (MT_VEND_TYPE_EEPROM | MT_VEND_TYPE_CFG)
429 #define MT_VEND_ADDR(type, n) (MT_VEND_TYPE_##type | (n))
431 MT_VEND_DEV_MODE = 0x1,
433 MT_VEND_POWER_ON = 0x4,
434 MT_VEND_MULTI_WRITE = 0x6,
435 MT_VEND_MULTI_READ = 0x7,
436 MT_VEND_READ_EEPROM = 0x9,
437 MT_VEND_WRITE_FCE = 0x42,
438 MT_VEND_WRITE_CFG = 0x46,
439 MT_VEND_READ_CFG = 0x47,
440 MT_VEND_READ_EXT = 0x63,
441 MT_VEND_WRITE_EXT = 0x66,
442 MT_VEND_FEATURE_SET = 0x91,
452 MT_EP_OUT_INBAND_CMD,
466 struct sk_buff_head res_q;
467 wait_queue_head_t wait;
470 #define MT_TX_SG_MAX_SIZE 8
471 #define MT_RX_SG_MAX_SIZE 4
472 #define MT_NUM_TX_ENTRIES 256
473 #define MT_NUM_RX_ENTRIES 128
474 #define MCU_RESP_URB_SIZE 1024
476 struct mutex usb_ctrl_mtx;
480 struct mt76_worker status_worker;
481 struct mt76_worker rx_worker;
483 struct work_struct stat_work;
485 u8 out_ep[__MT_EP_OUT_MAX];
486 u8 in_ep[__MT_EP_IN_MAX];
492 struct mt76_reg_pair *rp;
499 #define MT76S_XMIT_BUF_SZ (16 * PAGE_SIZE)
500 #define MT76S_NUM_TX_ENTRIES 256
501 #define MT76S_NUM_RX_ENTRIES 512
503 struct mt76_worker txrx_worker;
504 struct mt76_worker status_worker;
505 struct mt76_worker net_worker;
507 struct work_struct stat_work;
509 u8 *xmit_buf[IEEE80211_NUM_ACS + 2];
511 struct sdio_func *func;
514 wait_queue_head_t wait;
524 int (*parse_irq)(struct mt76_dev *dev, struct mt76s_intr *intr);
533 struct mt76_rx_status {
535 struct mt76_wcid *wcid;
554 u8 encoding:2, bw:3, he_ru:3;
555 u8 he_gi:2, he_dcm:1;
556 u8 amsdu:1, first_amsdu:1, last_amsdu:1;
562 s8 chain_signal[IEEE80211_MAX_CHAINS];
565 struct mt76_freq_range_power {
566 const struct cfg80211_sar_freq_ranges *range;
570 struct mt76_testmode_ops {
571 int (*set_state)(struct mt76_phy *phy, enum mt76_testmode_state state);
572 int (*set_params)(struct mt76_phy *phy, struct nlattr **tb,
573 enum mt76_testmode_state new_state);
574 int (*dump_stats)(struct mt76_phy *phy, struct sk_buff *msg);
577 struct mt76_testmode_data {
578 enum mt76_testmode_state state;
580 u32 param_set[DIV_ROUND_UP(NUM_MT76_TM_ATTRS, 32)];
581 struct sk_buff *tx_skb;
606 u8 addr[3][ETH_ALEN];
613 u64 packets[__MT_RXQ_MAX];
614 u64 fcs_error[__MT_RXQ_MAX];
627 struct ieee80211_hw *hw;
628 struct mt76_dev *dev;
633 struct mt76_queue *q_tx[__MT_TXQ_MAX];
635 struct cfg80211_chan_def chandef;
636 struct ieee80211_channel *main_chan;
638 struct mt76_channel_state *chan_state;
641 struct mt76_hw_cap cap;
642 struct mt76_sband sband_2g;
643 struct mt76_sband sband_5g;
644 struct mt76_sband sband_6g;
646 u8 macaddr[ETH_ALEN];
652 #ifdef CONFIG_NL80211_TESTMODE
653 struct mt76_testmode_data test;
656 struct delayed_work mac_work;
660 struct sk_buff *head;
661 struct sk_buff **tail;
663 } rx_amsdu[__MT_RXQ_MAX];
665 struct mt76_freq_range_power *frp;
669 struct mt76_phy phy; /* must be first */
671 struct mt76_phy *phy2;
673 struct ieee80211_hw *hw;
680 struct mt76_rx_status rx_ampdu_status;
686 const struct mt76_bus_ops *bus;
687 const struct mt76_driver_ops *drv;
688 const struct mt76_mcu_ops *mcu_ops;
693 struct net_device napi_dev;
694 struct net_device tx_napi_dev;
696 struct napi_struct napi[__MT_RXQ_MAX];
697 struct sk_buff_head rx_skb[__MT_RXQ_MAX];
699 struct list_head txwi_cache;
700 struct mt76_queue *q_mcu[__MT_MCUQ_MAX];
701 struct mt76_queue q_rx[__MT_RXQ_MAX];
702 const struct mt76_queue_ops *queue_ops;
705 struct mt76_worker tx_worker;
706 struct napi_struct tx_napi;
708 spinlock_t token_lock;
712 wait_queue_head_t tx_wait;
713 /* spinclock used to protect wcid pktid linked list */
714 spinlock_t status_lock;
716 u32 wcid_mask[DIV_ROUND_UP(MT76_N_WCIDS, 32)];
717 u32 wcid_phy_mask[DIV_ROUND_UP(MT76_N_WCIDS, 32)];
721 struct mt76_wcid global_wcid;
722 struct mt76_wcid __rcu *wcid[MT76_N_WCIDS];
723 struct list_head wcid_list;
729 struct tasklet_struct pre_tbtt_tasklet;
733 struct debugfs_blob_wrapper eeprom;
734 struct debugfs_blob_wrapper otp;
736 struct mt76_rate_power rate_power;
739 enum nl80211_dfs_regions region;
743 struct led_classdev led_cdev;
752 #ifdef CONFIG_NL80211_TESTMODE
753 const struct mt76_testmode_ops *test_ops;
759 struct workqueue_struct *wq;
762 struct mt76_mmio mmio;
764 struct mt76_sdio sdio;
768 struct mt76_power_limits {
781 MT_PHY_TYPE_HE_SU = 8,
782 MT_PHY_TYPE_HE_EXT_SU,
785 __MT_PHY_TYPE_HE_MAX,
788 struct mt76_sta_stats {
789 u64 tx_mode[__MT_PHY_TYPE_HE_MAX];
790 u64 tx_bw[4]; /* 20, 40, 80, 160 */
791 u64 tx_nss[4]; /* 1, 2, 3, 4 */
792 u64 tx_mcs[16]; /* mcs idx */
795 struct mt76_ethtool_worker_info {
798 int initial_stat_idx;
799 int worker_stat_count;
803 #define CCK_RATE(_idx, _rate) { \
805 .flags = IEEE80211_RATE_SHORT_PREAMBLE, \
806 .hw_value = (MT_PHY_TYPE_CCK << 8) | (_idx), \
807 .hw_value_short = (MT_PHY_TYPE_CCK << 8) | (4 + _idx), \
810 #define OFDM_RATE(_idx, _rate) { \
812 .hw_value = (MT_PHY_TYPE_OFDM << 8) | (_idx), \
813 .hw_value_short = (MT_PHY_TYPE_OFDM << 8) | (_idx), \
816 extern struct ieee80211_rate mt76_rates[12];
818 #define __mt76_rr(dev, ...) (dev)->bus->rr((dev), __VA_ARGS__)
819 #define __mt76_wr(dev, ...) (dev)->bus->wr((dev), __VA_ARGS__)
820 #define __mt76_rmw(dev, ...) (dev)->bus->rmw((dev), __VA_ARGS__)
821 #define __mt76_wr_copy(dev, ...) (dev)->bus->write_copy((dev), __VA_ARGS__)
822 #define __mt76_rr_copy(dev, ...) (dev)->bus->read_copy((dev), __VA_ARGS__)
824 #define __mt76_set(dev, offset, val) __mt76_rmw(dev, offset, 0, val)
825 #define __mt76_clear(dev, offset, val) __mt76_rmw(dev, offset, val, 0)
827 #define mt76_rr(dev, ...) (dev)->mt76.bus->rr(&((dev)->mt76), __VA_ARGS__)
828 #define mt76_wr(dev, ...) (dev)->mt76.bus->wr(&((dev)->mt76), __VA_ARGS__)
829 #define mt76_rmw(dev, ...) (dev)->mt76.bus->rmw(&((dev)->mt76), __VA_ARGS__)
830 #define mt76_wr_copy(dev, ...) (dev)->mt76.bus->write_copy(&((dev)->mt76), __VA_ARGS__)
831 #define mt76_rr_copy(dev, ...) (dev)->mt76.bus->read_copy(&((dev)->mt76), __VA_ARGS__)
832 #define mt76_wr_rp(dev, ...) (dev)->mt76.bus->wr_rp(&((dev)->mt76), __VA_ARGS__)
833 #define mt76_rd_rp(dev, ...) (dev)->mt76.bus->rd_rp(&((dev)->mt76), __VA_ARGS__)
836 #define mt76_mcu_restart(dev, ...) (dev)->mt76.mcu_ops->mcu_restart(&((dev)->mt76))
837 #define __mt76_mcu_restart(dev, ...) (dev)->mcu_ops->mcu_restart((dev))
839 #define mt76_set(dev, offset, val) mt76_rmw(dev, offset, 0, val)
840 #define mt76_clear(dev, offset, val) mt76_rmw(dev, offset, val, 0)
842 #define mt76_get_field(_dev, _reg, _field) \
843 FIELD_GET(_field, mt76_rr(dev, _reg))
845 #define mt76_rmw_field(_dev, _reg, _field, _val) \
846 mt76_rmw(_dev, _reg, _field, FIELD_PREP(_field, _val))
848 #define __mt76_rmw_field(_dev, _reg, _field, _val) \
849 __mt76_rmw(_dev, _reg, _field, FIELD_PREP(_field, _val))
851 #define mt76_hw(dev) (dev)->mphy.hw
853 static inline struct ieee80211_hw *
854 mt76_wcid_hw(struct mt76_dev *dev, u16 wcid)
856 if (wcid <= MT76_N_WCIDS &&
857 mt76_wcid_mask_test(dev->wcid_phy_mask, wcid))
858 return dev->phy2->hw;
863 bool __mt76_poll(struct mt76_dev *dev, u32 offset, u32 mask, u32 val,
866 #define mt76_poll(dev, ...) __mt76_poll(&((dev)->mt76), __VA_ARGS__)
868 bool __mt76_poll_msec(struct mt76_dev *dev, u32 offset, u32 mask, u32 val,
871 #define mt76_poll_msec(dev, ...) __mt76_poll_msec(&((dev)->mt76), __VA_ARGS__)
873 void mt76_mmio_init(struct mt76_dev *dev, void __iomem *regs);
874 void mt76_pci_disable_aspm(struct pci_dev *pdev);
876 static inline u16 mt76_chip(struct mt76_dev *dev)
878 return dev->rev >> 16;
881 static inline u16 mt76_rev(struct mt76_dev *dev)
883 return dev->rev & 0xffff;
886 #define mt76xx_chip(dev) mt76_chip(&((dev)->mt76))
887 #define mt76xx_rev(dev) mt76_rev(&((dev)->mt76))
889 #define mt76_init_queues(dev, ...) (dev)->mt76.queue_ops->init(&((dev)->mt76), __VA_ARGS__)
890 #define mt76_queue_alloc(dev, ...) (dev)->mt76.queue_ops->alloc(&((dev)->mt76), __VA_ARGS__)
891 #define mt76_tx_queue_skb_raw(dev, ...) (dev)->mt76.queue_ops->tx_queue_skb_raw(&((dev)->mt76), __VA_ARGS__)
892 #define mt76_tx_queue_skb(dev, ...) (dev)->mt76.queue_ops->tx_queue_skb(&((dev)->mt76), __VA_ARGS__)
893 #define mt76_queue_rx_reset(dev, ...) (dev)->mt76.queue_ops->rx_reset(&((dev)->mt76), __VA_ARGS__)
894 #define mt76_queue_tx_cleanup(dev, ...) (dev)->mt76.queue_ops->tx_cleanup(&((dev)->mt76), __VA_ARGS__)
895 #define mt76_queue_rx_cleanup(dev, ...) (dev)->mt76.queue_ops->rx_cleanup(&((dev)->mt76), __VA_ARGS__)
896 #define mt76_queue_kick(dev, ...) (dev)->mt76.queue_ops->kick(&((dev)->mt76), __VA_ARGS__)
897 #define mt76_queue_reset(dev, ...) (dev)->mt76.queue_ops->reset_q(&((dev)->mt76), __VA_ARGS__)
899 #define mt76_for_each_q_rx(dev, i) \
900 for (i = 0; i < ARRAY_SIZE((dev)->q_rx) && \
901 (dev)->q_rx[i].ndesc; i++)
903 struct mt76_dev *mt76_alloc_device(struct device *pdev, unsigned int size,
904 const struct ieee80211_ops *ops,
905 const struct mt76_driver_ops *drv_ops);
906 int mt76_register_device(struct mt76_dev *dev, bool vht,
907 struct ieee80211_rate *rates, int n_rates);
908 void mt76_unregister_device(struct mt76_dev *dev);
909 void mt76_free_device(struct mt76_dev *dev);
910 void mt76_unregister_phy(struct mt76_phy *phy);
912 struct mt76_phy *mt76_alloc_phy(struct mt76_dev *dev, unsigned int size,
913 const struct ieee80211_ops *ops);
914 int mt76_register_phy(struct mt76_phy *phy, bool vht,
915 struct ieee80211_rate *rates, int n_rates);
917 struct dentry *mt76_register_debugfs_fops(struct mt76_phy *phy,
918 const struct file_operations *ops);
919 static inline struct dentry *mt76_register_debugfs(struct mt76_dev *dev)
921 return mt76_register_debugfs_fops(&dev->phy, NULL);
924 int mt76_queues_read(struct seq_file *s, void *data);
925 void mt76_seq_puts_array(struct seq_file *file, const char *str,
928 int mt76_eeprom_init(struct mt76_dev *dev, int len);
929 void mt76_eeprom_override(struct mt76_phy *phy);
930 int mt76_get_of_eeprom(struct mt76_dev *dev, void *data, int offset, int len);
933 mt76_init_queue(struct mt76_dev *dev, int qid, int idx, int n_desc,
935 u16 mt76_calculate_default_rate(struct mt76_phy *phy, int rateidx);
936 static inline int mt76_init_tx_queue(struct mt76_phy *phy, int qid, int idx,
937 int n_desc, int ring_base)
939 struct mt76_queue *q;
941 q = mt76_init_queue(phy->dev, qid, idx, n_desc, ring_base);
951 static inline int mt76_init_mcu_queue(struct mt76_dev *dev, int qid, int idx,
952 int n_desc, int ring_base)
954 struct mt76_queue *q;
956 q = mt76_init_queue(dev, qid, idx, n_desc, ring_base);
960 q->qid = __MT_TXQ_MAX + qid;
966 static inline struct mt76_phy *
967 mt76_dev_phy(struct mt76_dev *dev, bool phy_ext)
969 if (phy_ext && dev->phy2)
974 static inline struct ieee80211_hw *
975 mt76_phy_hw(struct mt76_dev *dev, bool phy_ext)
977 return mt76_dev_phy(dev, phy_ext)->hw;
981 mt76_get_txwi_ptr(struct mt76_dev *dev, struct mt76_txwi_cache *t)
983 return (u8 *)t - dev->drv->txwi_size;
986 /* increment with wrap-around */
987 static inline int mt76_incr(int val, int size)
989 return (val + 1) & (size - 1);
992 /* decrement with wrap-around */
993 static inline int mt76_decr(int val, int size)
995 return (val - 1) & (size - 1);
998 u8 mt76_ac_to_hwq(u8 ac);
1000 static inline struct ieee80211_txq *
1001 mtxq_to_txq(struct mt76_txq *mtxq)
1005 return container_of(ptr, struct ieee80211_txq, drv_priv);
1008 static inline struct ieee80211_sta *
1009 wcid_to_sta(struct mt76_wcid *wcid)
1013 if (!wcid || !wcid->sta)
1016 return container_of(ptr, struct ieee80211_sta, drv_priv);
1019 static inline struct mt76_tx_cb *mt76_tx_skb_cb(struct sk_buff *skb)
1021 BUILD_BUG_ON(sizeof(struct mt76_tx_cb) >
1022 sizeof(IEEE80211_SKB_CB(skb)->status.status_driver_data));
1023 return ((void *)IEEE80211_SKB_CB(skb)->status.status_driver_data);
1026 static inline void *mt76_skb_get_hdr(struct sk_buff *skb)
1028 struct mt76_rx_status mstat;
1029 u8 *data = skb->data;
1031 /* Alignment concerns */
1032 BUILD_BUG_ON(sizeof(struct ieee80211_radiotap_he) % 4);
1033 BUILD_BUG_ON(sizeof(struct ieee80211_radiotap_he_mu) % 4);
1035 mstat = *((struct mt76_rx_status *)skb->cb);
1037 if (mstat.flag & RX_FLAG_RADIOTAP_HE)
1038 data += sizeof(struct ieee80211_radiotap_he);
1039 if (mstat.flag & RX_FLAG_RADIOTAP_HE_MU)
1040 data += sizeof(struct ieee80211_radiotap_he_mu);
1045 static inline void mt76_insert_hdr_pad(struct sk_buff *skb)
1047 int len = ieee80211_get_hdrlen_from_skb(skb);
1053 memmove(skb->data, skb->data + 2, len);
1056 skb->data[len + 1] = 0;
1059 static inline bool mt76_is_skb_pktid(u8 pktid)
1061 if (pktid & MT_PACKET_ID_HAS_RATE)
1064 return pktid >= MT_PACKET_ID_FIRST;
1067 static inline u8 mt76_tx_power_nss_delta(u8 nss)
1069 static const u8 nss_delta[4] = { 0, 6, 9, 12 };
1071 return nss_delta[nss - 1];
1074 static inline bool mt76_testmode_enabled(struct mt76_phy *phy)
1076 #ifdef CONFIG_NL80211_TESTMODE
1077 return phy->test.state != MT76_TM_STATE_OFF;
1083 static inline bool mt76_is_testmode_skb(struct mt76_dev *dev,
1084 struct sk_buff *skb,
1085 struct ieee80211_hw **hw)
1087 #ifdef CONFIG_NL80211_TESTMODE
1088 if (skb == dev->phy.test.tx_skb)
1090 else if (dev->phy2 && skb == dev->phy2->test.tx_skb)
1091 *hw = dev->phy2->hw;
1100 void mt76_rx(struct mt76_dev *dev, enum mt76_rxq_id q, struct sk_buff *skb);
1101 void mt76_tx(struct mt76_phy *dev, struct ieee80211_sta *sta,
1102 struct mt76_wcid *wcid, struct sk_buff *skb);
1103 void mt76_wake_tx_queue(struct ieee80211_hw *hw, struct ieee80211_txq *txq);
1104 void mt76_stop_tx_queues(struct mt76_phy *phy, struct ieee80211_sta *sta,
1106 void mt76_tx_check_agg_ssn(struct ieee80211_sta *sta, struct sk_buff *skb);
1107 void mt76_txq_schedule(struct mt76_phy *phy, enum mt76_txq_id qid);
1108 void mt76_txq_schedule_all(struct mt76_phy *phy);
1109 void mt76_tx_worker_run(struct mt76_dev *dev);
1110 void mt76_tx_worker(struct mt76_worker *w);
1111 void mt76_release_buffered_frames(struct ieee80211_hw *hw,
1112 struct ieee80211_sta *sta,
1113 u16 tids, int nframes,
1114 enum ieee80211_frame_release_type reason,
1116 bool mt76_has_tx_pending(struct mt76_phy *phy);
1117 void mt76_set_channel(struct mt76_phy *phy);
1118 void mt76_update_survey(struct mt76_phy *phy);
1119 void mt76_update_survey_active_time(struct mt76_phy *phy, ktime_t time);
1120 int mt76_get_survey(struct ieee80211_hw *hw, int idx,
1121 struct survey_info *survey);
1122 void mt76_set_stream_caps(struct mt76_phy *phy, bool vht);
1124 int mt76_rx_aggr_start(struct mt76_dev *dev, struct mt76_wcid *wcid, u8 tid,
1126 void mt76_rx_aggr_stop(struct mt76_dev *dev, struct mt76_wcid *wcid, u8 tid);
1128 void mt76_wcid_key_setup(struct mt76_dev *dev, struct mt76_wcid *wcid,
1129 struct ieee80211_key_conf *key);
1131 void mt76_tx_status_lock(struct mt76_dev *dev, struct sk_buff_head *list)
1132 __acquires(&dev->status_lock);
1133 void mt76_tx_status_unlock(struct mt76_dev *dev, struct sk_buff_head *list)
1134 __releases(&dev->status_lock);
1136 int mt76_tx_status_skb_add(struct mt76_dev *dev, struct mt76_wcid *wcid,
1137 struct sk_buff *skb);
1138 struct sk_buff *mt76_tx_status_skb_get(struct mt76_dev *dev,
1139 struct mt76_wcid *wcid, int pktid,
1140 struct sk_buff_head *list);
1141 void mt76_tx_status_skb_done(struct mt76_dev *dev, struct sk_buff *skb,
1142 struct sk_buff_head *list);
1143 void __mt76_tx_complete_skb(struct mt76_dev *dev, u16 wcid, struct sk_buff *skb,
1144 struct list_head *free_list);
1146 mt76_tx_complete_skb(struct mt76_dev *dev, u16 wcid, struct sk_buff *skb)
1148 __mt76_tx_complete_skb(dev, wcid, skb, NULL);
1151 void mt76_tx_status_check(struct mt76_dev *dev, bool flush);
1152 int mt76_sta_state(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1153 struct ieee80211_sta *sta,
1154 enum ieee80211_sta_state old_state,
1155 enum ieee80211_sta_state new_state);
1156 void __mt76_sta_remove(struct mt76_dev *dev, struct ieee80211_vif *vif,
1157 struct ieee80211_sta *sta);
1158 void mt76_sta_pre_rcu_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1159 struct ieee80211_sta *sta);
1161 int mt76_get_min_avg_rssi(struct mt76_dev *dev, bool ext_phy);
1163 int mt76_get_txpower(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1165 int mt76_init_sar_power(struct ieee80211_hw *hw,
1166 const struct cfg80211_sar_specs *sar);
1167 int mt76_get_sar_power(struct mt76_phy *phy,
1168 struct ieee80211_channel *chan,
1171 void mt76_csa_check(struct mt76_dev *dev);
1172 void mt76_csa_finish(struct mt76_dev *dev);
1174 int mt76_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant);
1175 int mt76_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta, bool set);
1176 void mt76_insert_ccmp_hdr(struct sk_buff *skb, u8 key_id);
1177 int mt76_get_rate(struct mt76_dev *dev,
1178 struct ieee80211_supported_band *sband,
1180 void mt76_sw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1182 void mt76_sw_scan_complete(struct ieee80211_hw *hw,
1183 struct ieee80211_vif *vif);
1184 int mt76_testmode_cmd(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1185 void *data, int len);
1186 int mt76_testmode_dump(struct ieee80211_hw *hw, struct sk_buff *skb,
1187 struct netlink_callback *cb, void *data, int len);
1188 int mt76_testmode_set_state(struct mt76_phy *phy, enum mt76_testmode_state state);
1189 int mt76_testmode_alloc_skb(struct mt76_phy *phy, u32 len);
1191 static inline void mt76_testmode_reset(struct mt76_phy *phy, bool disable)
1193 #ifdef CONFIG_NL80211_TESTMODE
1194 enum mt76_testmode_state state = MT76_TM_STATE_IDLE;
1196 if (disable || phy->test.state == MT76_TM_STATE_OFF)
1197 state = MT76_TM_STATE_OFF;
1199 mt76_testmode_set_state(phy, state);
1205 static inline struct ieee80211_hw *
1206 mt76_tx_status_get_hw(struct mt76_dev *dev, struct sk_buff *skb)
1208 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1209 struct ieee80211_hw *hw = dev->phy.hw;
1211 if ((info->hw_queue & MT_TX_HW_QUEUE_EXT_PHY) && dev->phy2)
1214 info->hw_queue &= ~MT_TX_HW_QUEUE_EXT_PHY;
1219 void mt76_put_txwi(struct mt76_dev *dev, struct mt76_txwi_cache *t);
1220 void mt76_rx_complete(struct mt76_dev *dev, struct sk_buff_head *frames,
1221 struct napi_struct *napi);
1222 void mt76_rx_poll_complete(struct mt76_dev *dev, enum mt76_rxq_id q,
1223 struct napi_struct *napi);
1224 void mt76_rx_aggr_reorder(struct sk_buff *skb, struct sk_buff_head *frames);
1225 void mt76_testmode_tx_pending(struct mt76_phy *phy);
1226 void mt76_queue_tx_complete(struct mt76_dev *dev, struct mt76_queue *q,
1227 struct mt76_queue_entry *e);
1230 static inline bool mt76u_urb_error(struct urb *urb)
1232 return urb->status &&
1233 urb->status != -ECONNRESET &&
1234 urb->status != -ESHUTDOWN &&
1235 urb->status != -ENOENT;
1238 /* Map hardware queues to usb endpoints */
1239 static inline u8 q2ep(u8 qid)
1241 /* TODO: take management packets to queue 5 */
1246 mt76u_bulk_msg(struct mt76_dev *dev, void *data, int len, int *actual_len,
1247 int timeout, int ep)
1249 struct usb_interface *uintf = to_usb_interface(dev->dev);
1250 struct usb_device *udev = interface_to_usbdev(uintf);
1251 struct mt76_usb *usb = &dev->usb;
1255 pipe = usb_rcvbulkpipe(udev, usb->in_ep[ep]);
1257 pipe = usb_sndbulkpipe(udev, usb->out_ep[ep]);
1259 return usb_bulk_msg(udev, pipe, data, len, actual_len, timeout);
1262 void mt76_ethtool_worker(struct mt76_ethtool_worker_info *wi,
1263 struct mt76_sta_stats *stats);
1264 int mt76_skb_adjust_pad(struct sk_buff *skb, int pad);
1265 int mt76u_vendor_request(struct mt76_dev *dev, u8 req,
1266 u8 req_type, u16 val, u16 offset,
1267 void *buf, size_t len);
1268 void mt76u_single_wr(struct mt76_dev *dev, const u8 req,
1269 const u16 offset, const u32 val);
1270 int mt76u_init(struct mt76_dev *dev, struct usb_interface *intf,
1272 int mt76u_alloc_mcu_queue(struct mt76_dev *dev);
1273 int mt76u_alloc_queues(struct mt76_dev *dev);
1274 void mt76u_stop_tx(struct mt76_dev *dev);
1275 void mt76u_stop_rx(struct mt76_dev *dev);
1276 int mt76u_resume_rx(struct mt76_dev *dev);
1277 void mt76u_queues_deinit(struct mt76_dev *dev);
1279 int mt76s_init(struct mt76_dev *dev, struct sdio_func *func,
1280 const struct mt76_bus_ops *bus_ops);
1281 int mt76s_alloc_rx_queue(struct mt76_dev *dev, enum mt76_rxq_id qid);
1282 int mt76s_alloc_tx(struct mt76_dev *dev);
1283 void mt76s_deinit(struct mt76_dev *dev);
1284 void mt76s_sdio_irq(struct sdio_func *func);
1285 void mt76s_txrx_worker(struct mt76_sdio *sdio);
1286 bool mt76s_txqs_empty(struct mt76_dev *dev);
1287 int mt76s_hw_init(struct mt76_dev *dev, struct sdio_func *func,
1289 u32 mt76s_rr(struct mt76_dev *dev, u32 offset);
1290 void mt76s_wr(struct mt76_dev *dev, u32 offset, u32 val);
1291 u32 mt76s_rmw(struct mt76_dev *dev, u32 offset, u32 mask, u32 val);
1292 u32 mt76s_read_pcr(struct mt76_dev *dev);
1293 void mt76s_write_copy(struct mt76_dev *dev, u32 offset,
1294 const void *data, int len);
1295 void mt76s_read_copy(struct mt76_dev *dev, u32 offset,
1296 void *data, int len);
1297 int mt76s_wr_rp(struct mt76_dev *dev, u32 base,
1298 const struct mt76_reg_pair *data,
1300 int mt76s_rd_rp(struct mt76_dev *dev, u32 base,
1301 struct mt76_reg_pair *data, int len);
1304 mt76_mcu_msg_alloc(struct mt76_dev *dev, const void *data,
1306 void mt76_mcu_rx_event(struct mt76_dev *dev, struct sk_buff *skb);
1307 struct sk_buff *mt76_mcu_get_response(struct mt76_dev *dev,
1308 unsigned long expires);
1309 int mt76_mcu_send_and_get_msg(struct mt76_dev *dev, int cmd, const void *data,
1310 int len, bool wait_resp, struct sk_buff **ret);
1311 int mt76_mcu_skb_send_and_get_msg(struct mt76_dev *dev, struct sk_buff *skb,
1312 int cmd, bool wait_resp, struct sk_buff **ret);
1313 int __mt76_mcu_send_firmware(struct mt76_dev *dev, int cmd, const void *data,
1314 int len, int max_len);
1316 mt76_mcu_send_firmware(struct mt76_dev *dev, int cmd, const void *data,
1319 int max_len = 4096 - dev->mcu_ops->headroom;
1321 return __mt76_mcu_send_firmware(dev, cmd, data, len, max_len);
1325 mt76_mcu_send_msg(struct mt76_dev *dev, int cmd, const void *data, int len,
1328 return mt76_mcu_send_and_get_msg(dev, cmd, data, len, wait_resp, NULL);
1332 mt76_mcu_skb_send_msg(struct mt76_dev *dev, struct sk_buff *skb, int cmd,
1335 return mt76_mcu_skb_send_and_get_msg(dev, skb, cmd, wait_resp, NULL);
1338 void mt76_set_irq_mask(struct mt76_dev *dev, u32 addr, u32 clear, u32 set);
1340 s8 mt76_get_rate_power_limits(struct mt76_phy *phy,
1341 struct ieee80211_channel *chan,
1342 struct mt76_power_limits *dest,
1345 struct mt76_txwi_cache *
1346 mt76_token_release(struct mt76_dev *dev, int token, bool *wake);
1347 int mt76_token_consume(struct mt76_dev *dev, struct mt76_txwi_cache **ptxwi);
1348 void __mt76_set_tx_blocked(struct mt76_dev *dev, bool blocked);
1350 static inline void mt76_set_tx_blocked(struct mt76_dev *dev, bool blocked)
1352 spin_lock_bh(&dev->token_lock);
1353 __mt76_set_tx_blocked(dev, blocked);
1354 spin_unlock_bh(&dev->token_lock);
1358 mt76_token_get(struct mt76_dev *dev, struct mt76_txwi_cache **ptxwi)
1362 spin_lock_bh(&dev->token_lock);
1363 token = idr_alloc(&dev->token, *ptxwi, 0, dev->drv->token_size,
1365 spin_unlock_bh(&dev->token_lock);
1370 static inline struct mt76_txwi_cache *
1371 mt76_token_put(struct mt76_dev *dev, int token)
1373 struct mt76_txwi_cache *txwi;
1375 spin_lock_bh(&dev->token_lock);
1376 txwi = idr_remove(&dev->token, token);
1377 spin_unlock_bh(&dev->token_lock);
1382 static inline void mt76_packet_id_init(struct mt76_wcid *wcid)
1384 INIT_LIST_HEAD(&wcid->list);
1385 idr_init(&wcid->pktid);
1389 mt76_packet_id_flush(struct mt76_dev *dev, struct mt76_wcid *wcid)
1391 struct sk_buff_head list;
1393 mt76_tx_status_lock(dev, &list);
1394 mt76_tx_status_skb_get(dev, wcid, -1, &list);
1395 mt76_tx_status_unlock(dev, &list);
1397 idr_destroy(&wcid->pktid);