Path: blob/main/sys/contrib/dev/mediatek/mt76/mt76.h
48375 views
/* SPDX-License-Identifier: ISC */1/*2* Copyright (C) 2016 Felix Fietkau <[email protected]>3*/45#ifndef __MT76_H6#define __MT76_H78#include <linux/kernel.h>9#include <linux/io.h>10#include <linux/spinlock.h>11#include <linux/skbuff.h>12#include <linux/leds.h>13#include <linux/usb.h>14#include <linux/average.h>15#include <linux/soc/mediatek/mtk_wed.h>16#if defined(__FreeBSD__)17#include <linux/wait.h>18#include <linux/bitfield.h>19#include <linux/debugfs.h>20#include <linux/pci.h>21#include <linux/interrupt.h>22#endif23#include <net/mac80211.h>24#include <net/page_pool/helpers.h>25#include "util.h"26#include "testmode.h"2728#define MT_MCU_RING_SIZE 3229#define MT_RX_BUF_SIZE 204830#define MT_SKB_HEAD_LEN 2563132#define MT_MAX_NON_AQL_PKT 1633#define MT_TXQ_FREE_THR 323435#define MT76_TOKEN_FREE_THR 643637#define MT_QFLAG_WED_RING GENMASK(1, 0)38#define MT_QFLAG_WED_TYPE GENMASK(4, 2)39#define MT_QFLAG_WED BIT(5)40#define MT_QFLAG_WED_RRO BIT(6)41#define MT_QFLAG_WED_RRO_EN BIT(7)4243#define __MT_WED_Q(_type, _n) (MT_QFLAG_WED | \44FIELD_PREP(MT_QFLAG_WED_TYPE, _type) | \45FIELD_PREP(MT_QFLAG_WED_RING, _n))46#define __MT_WED_RRO_Q(_type, _n) (MT_QFLAG_WED_RRO | __MT_WED_Q(_type, _n))4748#define MT_WED_Q_TX(_n) __MT_WED_Q(MT76_WED_Q_TX, _n)49#define MT_WED_Q_RX(_n) __MT_WED_Q(MT76_WED_Q_RX, _n)50#define MT_WED_Q_TXFREE __MT_WED_Q(MT76_WED_Q_TXFREE, 0)51#define MT_WED_RRO_Q_DATA(_n) __MT_WED_RRO_Q(MT76_WED_RRO_Q_DATA, _n)52#define MT_WED_RRO_Q_MSDU_PG(_n) __MT_WED_RRO_Q(MT76_WED_RRO_Q_MSDU_PG, _n)53#define MT_WED_RRO_Q_IND __MT_WED_RRO_Q(MT76_WED_RRO_Q_IND, 0)5455struct mt76_dev;56struct mt76_phy;57struct mt76_wcid;58struct mt76s_intr;59struct mt76_chanctx;60struct mt76_vif_link;6162struct mt76_reg_pair {63u32 reg;64u32 value;65};6667enum mt76_bus_type {68MT76_BUS_MMIO,69MT76_BUS_USB,70MT76_BUS_SDIO,71};7273enum mt76_wed_type {74MT76_WED_Q_TX,75MT76_WED_Q_TXFREE,76MT76_WED_Q_RX,77MT76_WED_RRO_Q_DATA,78MT76_WED_RRO_Q_MSDU_PG,79MT76_WED_RRO_Q_IND,80};8182struct mt76_bus_ops {83u32 (*rr)(struct mt76_dev *dev, u32 offset);84void (*wr)(struct mt76_dev *dev, u32 offset, u32 val);85u32 (*rmw)(struct mt76_dev *dev, u32 offset, u32 mask, u32 val);86void (*write_copy)(struct mt76_dev *dev, u32 offset, const void *data,87int len);88void (*read_copy)(struct mt76_dev *dev, u32 offset, void *data,89int len);90int (*wr_rp)(struct mt76_dev *dev, u32 base,91const struct mt76_reg_pair *rp, int len);92int (*rd_rp)(struct mt76_dev *dev, u32 base,93struct mt76_reg_pair *rp, int len);94enum mt76_bus_type type;95};9697#define mt76_is_usb(dev) ((dev)->bus->type == MT76_BUS_USB)98#define mt76_is_mmio(dev) ((dev)->bus->type == MT76_BUS_MMIO)99#define mt76_is_sdio(dev) ((dev)->bus->type == MT76_BUS_SDIO)100101enum mt76_txq_id {102MT_TXQ_VO = IEEE80211_AC_VO,103MT_TXQ_VI = IEEE80211_AC_VI,104MT_TXQ_BE = IEEE80211_AC_BE,105MT_TXQ_BK = IEEE80211_AC_BK,106MT_TXQ_PSD,107MT_TXQ_BEACON,108MT_TXQ_CAB,109__MT_TXQ_MAX110};111112enum mt76_mcuq_id {113MT_MCUQ_WM,114MT_MCUQ_WA,115MT_MCUQ_FWDL,116__MT_MCUQ_MAX117};118119enum mt76_rxq_id {120MT_RXQ_MAIN,121MT_RXQ_MCU,122MT_RXQ_MCU_WA,123MT_RXQ_BAND1,124MT_RXQ_BAND1_WA,125MT_RXQ_MAIN_WA,126MT_RXQ_BAND2,127MT_RXQ_BAND2_WA,128MT_RXQ_RRO_BAND0,129MT_RXQ_RRO_BAND1,130MT_RXQ_RRO_BAND2,131MT_RXQ_MSDU_PAGE_BAND0,132MT_RXQ_MSDU_PAGE_BAND1,133MT_RXQ_MSDU_PAGE_BAND2,134MT_RXQ_TXFREE_BAND0,135MT_RXQ_TXFREE_BAND1,136MT_RXQ_TXFREE_BAND2,137MT_RXQ_RRO_IND,138__MT_RXQ_MAX139};140141enum mt76_band_id {142MT_BAND0,143MT_BAND1,144MT_BAND2,145__MT_MAX_BAND146};147148enum mt76_cipher_type {149MT_CIPHER_NONE,150MT_CIPHER_WEP40,151MT_CIPHER_TKIP,152MT_CIPHER_TKIP_NO_MIC,153MT_CIPHER_AES_CCMP,154MT_CIPHER_WEP104,155MT_CIPHER_BIP_CMAC_128,156MT_CIPHER_WEP128,157MT_CIPHER_WAPI,158MT_CIPHER_CCMP_CCX,159MT_CIPHER_CCMP_256,160MT_CIPHER_GCMP,161MT_CIPHER_GCMP_256,162};163164enum mt76_dfs_state {165MT_DFS_STATE_UNKNOWN,166MT_DFS_STATE_DISABLED,167MT_DFS_STATE_CAC,168MT_DFS_STATE_ACTIVE,169};170171#define MT76_RNR_SCAN_MAX_BSSIDS 16172struct mt76_scan_rnr_param {173u8 bssid[MT76_RNR_SCAN_MAX_BSSIDS][ETH_ALEN];174u8 channel[MT76_RNR_SCAN_MAX_BSSIDS];175u8 random_mac[ETH_ALEN];176u8 seq_num;177u8 bssid_num;178u32 sreq_flag;179};180181struct mt76_queue_buf {182dma_addr_t addr;183u16 len:15,184skip_unmap:1;185};186187struct mt76_tx_info {188struct mt76_queue_buf buf[32];189struct sk_buff *skb;190int nbuf;191u32 info;192};193194struct mt76_queue_entry {195union {196void *buf;197struct sk_buff *skb;198};199union {200struct mt76_txwi_cache *txwi;201struct urb *urb;202int buf_sz;203};204dma_addr_t dma_addr[2];205u16 dma_len[2];206u16 wcid;207bool skip_buf0:1;208bool skip_buf1:1;209bool done:1;210};211212struct mt76_queue_regs {213u32 desc_base;214u32 ring_size;215u32 cpu_idx;216u32 dma_idx;217} __packed __aligned(4);218219struct mt76_queue {220struct mt76_queue_regs __iomem *regs;221222spinlock_t lock;223spinlock_t cleanup_lock;224struct mt76_queue_entry *entry;225struct mt76_rro_desc *rro_desc;226struct mt76_desc *desc;227228u16 first;229u16 head;230u16 tail;231u8 hw_idx;232u8 ep;233int ndesc;234int queued;235int buf_size;236bool stopped;237bool blocked;238239u8 buf_offset;240u16 flags;241242struct mtk_wed_device *wed;243u32 wed_regs;244245dma_addr_t desc_dma;246struct sk_buff *rx_head;247struct page_pool *page_pool;248};249250struct mt76_mcu_ops {251unsigned int max_retry;252u32 headroom;253u32 tailroom;254255int (*mcu_send_msg)(struct mt76_dev *dev, int cmd, const void *data,256int len, bool wait_resp);257int (*mcu_skb_prepare_msg)(struct mt76_dev *dev, struct sk_buff *skb,258int cmd, int *seq);259int (*mcu_skb_send_msg)(struct mt76_dev *dev, struct sk_buff *skb,260int cmd, int *seq);261int (*mcu_parse_response)(struct mt76_dev *dev, int cmd,262struct sk_buff *skb, int seq);263u32 (*mcu_rr)(struct mt76_dev *dev, u32 offset);264void (*mcu_wr)(struct mt76_dev *dev, u32 offset, u32 val);265int (*mcu_wr_rp)(struct mt76_dev *dev, u32 base,266const struct mt76_reg_pair *rp, int len);267int (*mcu_rd_rp)(struct mt76_dev *dev, u32 base,268struct mt76_reg_pair *rp, int len);269int (*mcu_restart)(struct mt76_dev *dev);270};271272struct mt76_queue_ops {273int (*init)(struct mt76_dev *dev,274int (*poll)(struct napi_struct *napi, int budget));275276int (*alloc)(struct mt76_dev *dev, struct mt76_queue *q,277int idx, int n_desc, int bufsize,278u32 ring_base);279280int (*tx_queue_skb)(struct mt76_phy *phy, struct mt76_queue *q,281enum mt76_txq_id qid, struct sk_buff *skb,282struct mt76_wcid *wcid, struct ieee80211_sta *sta);283284int (*tx_queue_skb_raw)(struct mt76_dev *dev, struct mt76_queue *q,285struct sk_buff *skb, u32 tx_info);286287void *(*dequeue)(struct mt76_dev *dev, struct mt76_queue *q, bool flush,288int *len, u32 *info, bool *more);289290void (*rx_reset)(struct mt76_dev *dev, enum mt76_rxq_id qid);291292void (*tx_cleanup)(struct mt76_dev *dev, struct mt76_queue *q,293bool flush);294295void (*rx_cleanup)(struct mt76_dev *dev, struct mt76_queue *q);296297void (*kick)(struct mt76_dev *dev, struct mt76_queue *q);298299void (*reset_q)(struct mt76_dev *dev, struct mt76_queue *q);300};301302enum mt76_phy_type {303MT_PHY_TYPE_CCK,304MT_PHY_TYPE_OFDM,305MT_PHY_TYPE_HT,306MT_PHY_TYPE_HT_GF,307MT_PHY_TYPE_VHT,308MT_PHY_TYPE_HE_SU = 8,309MT_PHY_TYPE_HE_EXT_SU,310MT_PHY_TYPE_HE_TB,311MT_PHY_TYPE_HE_MU,312MT_PHY_TYPE_EHT_SU = 13,313MT_PHY_TYPE_EHT_TRIG,314MT_PHY_TYPE_EHT_MU,315__MT_PHY_TYPE_MAX,316};317318struct mt76_sta_stats {319u64 tx_mode[__MT_PHY_TYPE_MAX];320u64 tx_bw[5]; /* 20, 40, 80, 160, 320 */321u64 tx_nss[4]; /* 1, 2, 3, 4 */322u64 tx_mcs[16]; /* mcs idx */323u64 tx_bytes;324/* WED TX */325u32 tx_packets; /* unit: MSDU */326u32 tx_retries;327u32 tx_failed;328/* WED RX */329u64 rx_bytes;330u32 rx_packets;331u32 rx_errors;332u32 rx_drops;333};334335enum mt76_wcid_flags {336MT_WCID_FLAG_CHECK_PS,337MT_WCID_FLAG_PS,338MT_WCID_FLAG_4ADDR,339MT_WCID_FLAG_HDR_TRANS,340};341342#define MT76_N_WCIDS 1088343344/* stored in ieee80211_tx_info::hw_queue */345#define MT_TX_HW_QUEUE_PHY GENMASK(3, 2)346347DECLARE_EWMA(signal, 10, 8);348349#define MT_WCID_TX_INFO_RATE GENMASK(15, 0)350#define MT_WCID_TX_INFO_NSS GENMASK(17, 16)351#define MT_WCID_TX_INFO_TXPWR_ADJ GENMASK(25, 18)352#define MT_WCID_TX_INFO_SET BIT(31)353354struct mt76_wcid {355struct mt76_rx_tid __rcu *aggr[IEEE80211_NUM_TIDS];356357atomic_t non_aql_packets;358unsigned long flags;359360struct ewma_signal rssi;361int inactive_count;362363struct rate_info rate;364unsigned long ampdu_state;365366u16 idx;367u8 hw_key_idx;368u8 hw_key_idx2;369370u8 offchannel:1;371u8 sta:1;372u8 sta_disabled:1;373u8 amsdu:1;374u8 phy_idx:2;375u8 link_id:4;376bool link_valid;377378u8 rx_check_pn;379u8 rx_key_pn[IEEE80211_NUM_TIDS + 1][6];380u16 cipher;381382u32 tx_info;383bool sw_iv;384385struct list_head tx_list;386struct sk_buff_head tx_pending;387struct sk_buff_head tx_offchannel;388389struct list_head list;390struct idr pktid;391392struct mt76_sta_stats stats;393394struct list_head poll_list;395396struct mt76_wcid *def_wcid;397};398399struct mt76_txq {400u16 wcid;401402u16 agg_ssn;403bool send_bar;404bool aggr;405};406407struct mt76_wed_rro_ind {408u32 se_id : 12;409u32 rsv : 4;410u32 start_sn : 12;411u32 ind_reason : 4;412u32 ind_cnt : 13;413u32 win_sz : 3;414u32 rsv2 : 13;415u32 magic_cnt : 3;416};417418struct mt76_txwi_cache {419struct list_head list;420dma_addr_t dma_addr;421422union {423struct sk_buff *skb;424void *ptr;425};426};427428struct mt76_rx_tid {429struct rcu_head rcu_head;430431struct mt76_dev *dev;432433spinlock_t lock;434struct delayed_work reorder_work;435436u16 id;437u16 head;438u16 size;439u16 nframes;440441u8 num;442443u8 started:1, stopped:1, timer_pending:1;444445struct sk_buff *reorder_buf[] __counted_by(size);446};447448#define MT_TX_CB_DMA_DONE BIT(0)449#define MT_TX_CB_TXS_DONE BIT(1)450#define MT_TX_CB_TXS_FAILED BIT(2)451452#define MT_PACKET_ID_MASK GENMASK(6, 0)453#define MT_PACKET_ID_NO_ACK 0454#define MT_PACKET_ID_NO_SKB 1455#define MT_PACKET_ID_WED 2456#define MT_PACKET_ID_FIRST 3457#define MT_PACKET_ID_HAS_RATE BIT(7)458/* This is timer for when to give up when waiting for TXS callback,459* with starting time being the time at which the DMA_DONE callback460* was seen (so, we know packet was processed then, it should not take461* long after that for firmware to send the TXS callback if it is going462* to do so.)463*/464#define MT_TX_STATUS_SKB_TIMEOUT (HZ / 4)465466struct mt76_tx_cb {467unsigned long jiffies;468u16 wcid;469u8 pktid;470u8 flags;471};472473enum {474MT76_STATE_INITIALIZED,475MT76_STATE_REGISTERED,476MT76_STATE_RUNNING,477MT76_STATE_MCU_RUNNING,478MT76_SCANNING,479MT76_HW_SCANNING,480MT76_HW_SCHED_SCANNING,481MT76_RESTART,482MT76_RESET,483MT76_MCU_RESET,484MT76_REMOVED,485MT76_READING_STATS,486MT76_STATE_POWER_OFF,487MT76_STATE_SUSPEND,488MT76_STATE_ROC,489MT76_STATE_PM,490MT76_STATE_WED_RESET,491};492493enum mt76_sta_event {494MT76_STA_EVENT_ASSOC,495MT76_STA_EVENT_AUTHORIZE,496MT76_STA_EVENT_DISASSOC,497};498499struct mt76_hw_cap {500bool has_2ghz;501bool has_5ghz;502bool has_6ghz;503};504505#define MT_DRV_TXWI_NO_FREE BIT(0)506#define MT_DRV_TX_ALIGNED4_SKBS BIT(1)507#define MT_DRV_SW_RX_AIRTIME BIT(2)508#define MT_DRV_RX_DMA_HDR BIT(3)509#define MT_DRV_HW_MGMT_TXQ BIT(4)510#define MT_DRV_AMSDU_OFFLOAD BIT(5)511#define MT_DRV_IGNORE_TXS_FAILED BIT(6)512513struct mt76_driver_ops {514u32 drv_flags;515u32 survey_flags;516u16 txwi_size;517u16 token_size;518u8 mcs_rates;519520unsigned int link_data_size;521522void (*update_survey)(struct mt76_phy *phy);523int (*set_channel)(struct mt76_phy *phy);524525int (*tx_prepare_skb)(struct mt76_dev *dev, void *txwi_ptr,526enum mt76_txq_id qid, struct mt76_wcid *wcid,527struct ieee80211_sta *sta,528struct mt76_tx_info *tx_info);529530void (*tx_complete_skb)(struct mt76_dev *dev,531struct mt76_queue_entry *e);532533bool (*tx_status_data)(struct mt76_dev *dev, u8 *update);534535bool (*rx_check)(struct mt76_dev *dev, void *data, int len);536537void (*rx_skb)(struct mt76_dev *dev, enum mt76_rxq_id q,538struct sk_buff *skb, u32 *info);539540void (*rx_poll_complete)(struct mt76_dev *dev, enum mt76_rxq_id q);541542void (*sta_ps)(struct mt76_dev *dev, struct ieee80211_sta *sta,543bool ps);544545int (*sta_add)(struct mt76_dev *dev, struct ieee80211_vif *vif,546struct ieee80211_sta *sta);547548int (*sta_event)(struct mt76_dev *dev, struct ieee80211_vif *vif,549struct ieee80211_sta *sta, enum mt76_sta_event ev);550551void (*sta_remove)(struct mt76_dev *dev, struct ieee80211_vif *vif,552struct ieee80211_sta *sta);553554int (*vif_link_add)(struct mt76_phy *phy, struct ieee80211_vif *vif,555struct ieee80211_bss_conf *link_conf,556struct mt76_vif_link *mlink);557558void (*vif_link_remove)(struct mt76_phy *phy,559struct ieee80211_vif *vif,560struct ieee80211_bss_conf *link_conf,561struct mt76_vif_link *mlink);562};563564struct mt76_channel_state {565u64 cc_active;566u64 cc_busy;567u64 cc_rx;568u64 cc_bss_rx;569u64 cc_tx;570571s8 noise;572};573574struct mt76_sband {575struct ieee80211_supported_band sband;576struct mt76_channel_state *chan;577};578579/* addr req mask */580#define MT_VEND_TYPE_EEPROM BIT(31)581#define MT_VEND_TYPE_CFG BIT(30)582#define MT_VEND_TYPE_MASK (MT_VEND_TYPE_EEPROM | MT_VEND_TYPE_CFG)583584#define MT_VEND_ADDR(type, n) (MT_VEND_TYPE_##type | (n))585enum mt_vendor_req {586MT_VEND_DEV_MODE = 0x1,587MT_VEND_WRITE = 0x2,588MT_VEND_POWER_ON = 0x4,589MT_VEND_MULTI_WRITE = 0x6,590MT_VEND_MULTI_READ = 0x7,591MT_VEND_READ_EEPROM = 0x9,592MT_VEND_WRITE_FCE = 0x42,593MT_VEND_WRITE_CFG = 0x46,594MT_VEND_READ_CFG = 0x47,595MT_VEND_READ_EXT = 0x63,596MT_VEND_WRITE_EXT = 0x66,597MT_VEND_FEATURE_SET = 0x91,598};599600enum mt76u_in_ep {601MT_EP_IN_PKT_RX,602MT_EP_IN_CMD_RESP,603__MT_EP_IN_MAX,604};605606enum mt76u_out_ep {607MT_EP_OUT_INBAND_CMD,608MT_EP_OUT_AC_BE,609MT_EP_OUT_AC_BK,610MT_EP_OUT_AC_VI,611MT_EP_OUT_AC_VO,612MT_EP_OUT_HCCA,613__MT_EP_OUT_MAX,614};615616struct mt76_mcu {617struct mutex mutex;618u32 msg_seq;619int timeout;620621struct sk_buff_head res_q;622wait_queue_head_t wait;623};624625#define MT_TX_SG_MAX_SIZE 8626#define MT_RX_SG_MAX_SIZE 4627#define MT_NUM_TX_ENTRIES 256628#define MT_NUM_RX_ENTRIES 128629#define MCU_RESP_URB_SIZE 1024630struct mt76_usb {631struct mutex usb_ctrl_mtx;632u8 *data;633u16 data_len;634635struct mt76_worker status_worker;636struct mt76_worker rx_worker;637638struct work_struct stat_work;639640u8 out_ep[__MT_EP_OUT_MAX];641u8 in_ep[__MT_EP_IN_MAX];642bool sg_en;643644struct mt76u_mcu {645u8 *data;646/* multiple reads */647struct mt76_reg_pair *rp;648int rp_len;649u32 base;650} mcu;651};652653#define MT76S_XMIT_BUF_SZ 0x3fe00654#define MT76S_NUM_TX_ENTRIES 256655#define MT76S_NUM_RX_ENTRIES 512656struct mt76_sdio {657struct mt76_worker txrx_worker;658struct mt76_worker status_worker;659struct mt76_worker net_worker;660struct mt76_worker stat_worker;661662u8 *xmit_buf;663u32 xmit_buf_sz;664665struct sdio_func *func;666void *intr_data;667u8 hw_ver;668wait_queue_head_t wait;669670int pse_mcu_quota_max;671struct {672int pse_data_quota;673int ple_data_quota;674int pse_mcu_quota;675int pse_page_size;676int deficit;677} sched;678679int (*parse_irq)(struct mt76_dev *dev, struct mt76s_intr *intr);680};681682struct mt76_mmio {683void __iomem *regs;684spinlock_t irq_lock;685u32 irqmask;686687struct mtk_wed_device wed;688struct mtk_wed_device wed_hif2;689struct completion wed_reset;690struct completion wed_reset_complete;691};692693struct mt76_rx_status {694union {695struct mt76_wcid *wcid;696u16 wcid_idx;697};698699u32 reorder_time;700701u32 ampdu_ref;702u32 timestamp;703704u8 iv[6];705706u8 phy_idx:2;707u8 aggr:1;708u8 qos_ctl;709u16 seqno;710711u16 freq;712u32 flag;713u8 enc_flags;714u8 encoding:3, bw:4;715union {716struct {717u8 he_ru:3;718u8 he_gi:2;719u8 he_dcm:1;720};721struct {722u8 ru:4;723u8 gi:2;724} eht;725};726727u8 amsdu:1, first_amsdu:1, last_amsdu:1;728u8 rate_idx;729u8 nss:5, band:3;730s8 signal;731u8 chains;732s8 chain_signal[IEEE80211_MAX_CHAINS];733};734735struct mt76_freq_range_power {736const struct cfg80211_sar_freq_ranges *range;737s8 power;738};739740struct mt76_testmode_ops {741int (*set_state)(struct mt76_phy *phy, enum mt76_testmode_state state);742int (*set_params)(struct mt76_phy *phy, struct nlattr **tb,743enum mt76_testmode_state new_state);744int (*dump_stats)(struct mt76_phy *phy, struct sk_buff *msg);745};746747struct mt76_testmode_data {748enum mt76_testmode_state state;749750u32 param_set[DIV_ROUND_UP(NUM_MT76_TM_ATTRS, 32)];751struct sk_buff *tx_skb;752753u32 tx_count;754u16 tx_mpdu_len;755756u8 tx_rate_mode;757u8 tx_rate_idx;758u8 tx_rate_nss;759u8 tx_rate_sgi;760u8 tx_rate_ldpc;761u8 tx_rate_stbc;762u8 tx_ltf;763764u8 tx_antenna_mask;765u8 tx_spe_idx;766767u8 tx_duty_cycle;768u32 tx_time;769u32 tx_ipg;770771u32 freq_offset;772773u8 tx_power[4];774u8 tx_power_control;775776u8 addr[3][ETH_ALEN];777778u32 tx_pending;779u32 tx_queued;780u16 tx_queued_limit;781u32 tx_done;782struct {783u64 packets[__MT_RXQ_MAX];784u64 fcs_error[__MT_RXQ_MAX];785} rx_stats;786};787788struct mt76_vif_link {789u8 idx;790u8 link_idx;791u8 omac_idx;792u8 band_idx;793u8 wmm_idx;794u8 scan_seq_num;795u8 cipher;796u8 basic_rates_idx;797u8 mcast_rates_idx;798u8 beacon_rates_idx;799bool offchannel;800struct ieee80211_chanctx_conf *ctx;801struct mt76_wcid *wcid;802struct mt76_vif_data *mvif;803struct rcu_head rcu_head;804};805806struct mt76_vif_data {807struct mt76_vif_link __rcu *link[IEEE80211_MLD_MAX_NUM_LINKS];808struct mt76_vif_link __rcu *offchannel_link;809810struct mt76_phy *roc_phy;811u16 valid_links;812u8 deflink_id;813};814815struct mt76_phy {816struct ieee80211_hw *hw;817struct mt76_dev *dev;818void *priv;819820unsigned long state;821unsigned int num_sta;822u8 band_idx;823824spinlock_t tx_lock;825struct list_head tx_list;826struct mt76_queue *q_tx[__MT_TXQ_MAX];827828struct cfg80211_chan_def chandef;829struct cfg80211_chan_def main_chandef;830bool offchannel;831bool radar_enabled;832833struct delayed_work roc_work;834struct ieee80211_vif *roc_vif;835struct mt76_vif_link *roc_link;836837struct mt76_chanctx *chanctx;838839struct mt76_channel_state *chan_state;840enum mt76_dfs_state dfs_state;841ktime_t survey_time;842843u32 aggr_stats[32];844845struct mt76_hw_cap cap;846struct mt76_sband sband_2g;847struct mt76_sband sband_5g;848struct mt76_sband sband_6g;849850u8 macaddr[ETH_ALEN];851852int txpower_cur;853u8 antenna_mask;854u16 chainmask;855856#ifdef CONFIG_NL80211_TESTMODE857struct mt76_testmode_data test;858#endif859860struct delayed_work mac_work;861u8 mac_work_count;862863struct {864struct sk_buff *head;865struct sk_buff **tail;866u16 seqno;867} rx_amsdu[__MT_RXQ_MAX];868869struct mt76_freq_range_power *frp;870871struct {872struct led_classdev cdev;873char name[32];874bool al;875u8 pin;876} leds;877};878879struct mt76_dev {880struct mt76_phy phy; /* must be first */881struct mt76_phy *phys[__MT_MAX_BAND];882struct mt76_phy *band_phys[NUM_NL80211_BANDS];883884struct ieee80211_hw *hw;885886spinlock_t wed_lock;887spinlock_t lock;888spinlock_t cc_lock;889890u32 cur_cc_bss_rx;891892struct mt76_rx_status rx_ampdu_status;893u32 rx_ampdu_len;894u32 rx_ampdu_ref;895896struct mutex mutex;897898const struct mt76_bus_ops *bus;899const struct mt76_driver_ops *drv;900const struct mt76_mcu_ops *mcu_ops;901struct device *dev;902struct device *dma_dev;903904struct mt76_mcu mcu;905906struct net_device *napi_dev;907struct net_device *tx_napi_dev;908spinlock_t rx_lock;909struct napi_struct napi[__MT_RXQ_MAX];910struct sk_buff_head rx_skb[__MT_RXQ_MAX];911struct tasklet_struct irq_tasklet;912913struct list_head txwi_cache;914struct list_head rxwi_cache;915struct mt76_queue *q_mcu[__MT_MCUQ_MAX];916struct mt76_queue q_rx[__MT_RXQ_MAX];917const struct mt76_queue_ops *queue_ops;918int tx_dma_idx[4];919920struct mt76_worker tx_worker;921struct napi_struct tx_napi;922923spinlock_t token_lock;924struct idr token;925u16 wed_token_count;926u16 token_count;927u16 token_size;928929spinlock_t rx_token_lock;930struct idr rx_token;931u16 rx_token_size;932933wait_queue_head_t tx_wait;934/* spinclock used to protect wcid pktid linked list */935spinlock_t status_lock;936937u32 wcid_mask[DIV_ROUND_UP(MT76_N_WCIDS, 32)];938939u64 vif_mask;940941struct mt76_wcid global_wcid;942struct mt76_wcid __rcu *wcid[MT76_N_WCIDS];943struct list_head wcid_list;944945struct list_head sta_poll_list;946spinlock_t sta_poll_lock;947948u32 rev;949950struct tasklet_struct pre_tbtt_tasklet;951int beacon_int;952u8 beacon_mask;953954struct debugfs_blob_wrapper eeprom;955struct debugfs_blob_wrapper otp;956957char alpha2[3];958enum nl80211_dfs_regions region;959960struct mt76_scan_rnr_param rnr;961962u32 debugfs_reg;963964u8 csa_complete;965966u32 rxfilter;967968struct delayed_work scan_work;969struct {970struct cfg80211_scan_request *req;971struct ieee80211_channel *chan;972struct ieee80211_vif *vif;973struct mt76_vif_link *mlink;974struct mt76_phy *phy;975int chan_idx;976} scan;977978#ifdef CONFIG_NL80211_TESTMODE979const struct mt76_testmode_ops *test_ops;980struct {981const char *name;982u32 offset;983} test_mtd;984#endif985struct workqueue_struct *wq;986987union {988struct mt76_mmio mmio;989struct mt76_usb usb;990struct mt76_sdio sdio;991};992993atomic_t bus_hung;994};995996/* per-phy stats. */997struct mt76_mib_stats {998u32 ack_fail_cnt;999u32 fcs_err_cnt;1000u32 rts_cnt;1001u32 rts_retries_cnt;1002u32 ba_miss_cnt;1003u32 tx_bf_cnt;1004u32 tx_mu_bf_cnt;1005u32 tx_mu_mpdu_cnt;1006u32 tx_mu_acked_mpdu_cnt;1007u32 tx_su_acked_mpdu_cnt;1008u32 tx_bf_ibf_ppdu_cnt;1009u32 tx_bf_ebf_ppdu_cnt;10101011u32 tx_bf_rx_fb_all_cnt;1012u32 tx_bf_rx_fb_eht_cnt;1013u32 tx_bf_rx_fb_he_cnt;1014u32 tx_bf_rx_fb_vht_cnt;1015u32 tx_bf_rx_fb_ht_cnt;10161017u32 tx_bf_rx_fb_bw; /* value of last sample, not cumulative */1018u32 tx_bf_rx_fb_nc_cnt;1019u32 tx_bf_rx_fb_nr_cnt;1020u32 tx_bf_fb_cpl_cnt;1021u32 tx_bf_fb_trig_cnt;10221023u32 tx_ampdu_cnt;1024u32 tx_stop_q_empty_cnt;1025u32 tx_mpdu_attempts_cnt;1026u32 tx_mpdu_success_cnt;1027u32 tx_pkt_ebf_cnt;1028u32 tx_pkt_ibf_cnt;10291030u32 tx_rwp_fail_cnt;1031u32 tx_rwp_need_cnt;10321033/* rx stats */1034u32 rx_fifo_full_cnt;1035u32 channel_idle_cnt;1036u32 primary_cca_busy_time;1037u32 secondary_cca_busy_time;1038u32 primary_energy_detect_time;1039u32 cck_mdrdy_time;1040u32 ofdm_mdrdy_time;1041u32 green_mdrdy_time;1042u32 rx_vector_mismatch_cnt;1043u32 rx_delimiter_fail_cnt;1044u32 rx_mrdy_cnt;1045u32 rx_len_mismatch_cnt;1046u32 rx_mpdu_cnt;1047u32 rx_ampdu_cnt;1048u32 rx_ampdu_bytes_cnt;1049u32 rx_ampdu_valid_subframe_cnt;1050u32 rx_ampdu_valid_subframe_bytes_cnt;1051u32 rx_pfdrop_cnt;1052u32 rx_vec_queue_overflow_drop_cnt;1053u32 rx_ba_cnt;10541055u32 tx_amsdu[8];1056u32 tx_amsdu_cnt;10571058/* mcu_muru_stats */1059u32 dl_cck_cnt;1060u32 dl_ofdm_cnt;1061u32 dl_htmix_cnt;1062u32 dl_htgf_cnt;1063u32 dl_vht_su_cnt;1064u32 dl_vht_2mu_cnt;1065u32 dl_vht_3mu_cnt;1066u32 dl_vht_4mu_cnt;1067u32 dl_he_su_cnt;1068u32 dl_he_ext_su_cnt;1069u32 dl_he_2ru_cnt;1070u32 dl_he_2mu_cnt;1071u32 dl_he_3ru_cnt;1072u32 dl_he_3mu_cnt;1073u32 dl_he_4ru_cnt;1074u32 dl_he_4mu_cnt;1075u32 dl_he_5to8ru_cnt;1076u32 dl_he_9to16ru_cnt;1077u32 dl_he_gtr16ru_cnt;10781079u32 ul_hetrig_su_cnt;1080u32 ul_hetrig_2ru_cnt;1081u32 ul_hetrig_3ru_cnt;1082u32 ul_hetrig_4ru_cnt;1083u32 ul_hetrig_5to8ru_cnt;1084u32 ul_hetrig_9to16ru_cnt;1085u32 ul_hetrig_gtr16ru_cnt;1086u32 ul_hetrig_2mu_cnt;1087u32 ul_hetrig_3mu_cnt;1088u32 ul_hetrig_4mu_cnt;1089};10901091struct mt76_power_limits {1092s8 cck[4];1093s8 ofdm[8];1094s8 mcs[4][10];1095s8 ru[7][12];1096s8 eht[16][16];1097};10981099struct mt76_ethtool_worker_info {1100u64 *data;1101int idx;1102int initial_stat_idx;1103int worker_stat_count;1104int sta_count;1105};11061107struct mt76_chanctx {1108struct mt76_phy *phy;1109};11101111#define CCK_RATE(_idx, _rate) { \1112.bitrate = _rate, \1113.flags = IEEE80211_RATE_SHORT_PREAMBLE, \1114.hw_value = (MT_PHY_TYPE_CCK << 8) | (_idx), \1115.hw_value_short = (MT_PHY_TYPE_CCK << 8) | (4 + _idx), \1116}11171118#define OFDM_RATE(_idx, _rate) { \1119.bitrate = _rate, \1120.hw_value = (MT_PHY_TYPE_OFDM << 8) | (_idx), \1121.hw_value_short = (MT_PHY_TYPE_OFDM << 8) | (_idx), \1122}11231124extern struct ieee80211_rate mt76_rates[12];11251126#define __mt76_rr(dev, ...) (dev)->bus->rr((dev), __VA_ARGS__)1127#define __mt76_wr(dev, ...) (dev)->bus->wr((dev), __VA_ARGS__)1128#define __mt76_rmw(dev, ...) (dev)->bus->rmw((dev), __VA_ARGS__)1129#define __mt76_wr_copy(dev, ...) (dev)->bus->write_copy((dev), __VA_ARGS__)1130#define __mt76_rr_copy(dev, ...) (dev)->bus->read_copy((dev), __VA_ARGS__)11311132#define __mt76_set(dev, offset, val) __mt76_rmw(dev, offset, 0, val)1133#define __mt76_clear(dev, offset, val) __mt76_rmw(dev, offset, val, 0)11341135#define mt76_rr(dev, ...) (dev)->mt76.bus->rr(&((dev)->mt76), __VA_ARGS__)1136#define mt76_wr(dev, ...) (dev)->mt76.bus->wr(&((dev)->mt76), __VA_ARGS__)1137#define mt76_rmw(dev, ...) (dev)->mt76.bus->rmw(&((dev)->mt76), __VA_ARGS__)1138#define mt76_wr_copy(dev, ...) (dev)->mt76.bus->write_copy(&((dev)->mt76), __VA_ARGS__)1139#define mt76_rr_copy(dev, ...) (dev)->mt76.bus->read_copy(&((dev)->mt76), __VA_ARGS__)1140#define mt76_wr_rp(dev, ...) (dev)->mt76.bus->wr_rp(&((dev)->mt76), __VA_ARGS__)1141#define mt76_rd_rp(dev, ...) (dev)->mt76.bus->rd_rp(&((dev)->mt76), __VA_ARGS__)114211431144#define mt76_mcu_restart(dev, ...) (dev)->mt76.mcu_ops->mcu_restart(&((dev)->mt76))11451146#define mt76_set(dev, offset, val) mt76_rmw(dev, offset, 0, val)1147#define mt76_clear(dev, offset, val) mt76_rmw(dev, offset, val, 0)11481149#define mt76_get_field(_dev, _reg, _field) \1150FIELD_GET(_field, mt76_rr(dev, _reg))11511152#define mt76_rmw_field(_dev, _reg, _field, _val) \1153mt76_rmw(_dev, _reg, _field, FIELD_PREP(_field, _val))11541155#define __mt76_rmw_field(_dev, _reg, _field, _val) \1156__mt76_rmw(_dev, _reg, _field, FIELD_PREP(_field, _val))11571158#define mt76_hw(dev) (dev)->mphy.hw11591160bool __mt76_poll(struct mt76_dev *dev, u32 offset, u32 mask, u32 val,1161int timeout);11621163#define mt76_poll(dev, ...) __mt76_poll(&((dev)->mt76), __VA_ARGS__)11641165bool ____mt76_poll_msec(struct mt76_dev *dev, u32 offset, u32 mask, u32 val,1166int timeout, int kick);1167#define __mt76_poll_msec(...) ____mt76_poll_msec(__VA_ARGS__, 10)1168#define mt76_poll_msec(dev, ...) ____mt76_poll_msec(&((dev)->mt76), __VA_ARGS__, 10)1169#define mt76_poll_msec_tick(dev, ...) ____mt76_poll_msec(&((dev)->mt76), __VA_ARGS__)11701171void mt76_mmio_init(struct mt76_dev *dev, void __iomem *regs);1172void mt76_pci_disable_aspm(struct pci_dev *pdev);1173bool mt76_pci_aspm_supported(struct pci_dev *pdev);11741175static inline u16 mt76_chip(struct mt76_dev *dev)1176{1177return dev->rev >> 16;1178}11791180static inline u16 mt76_rev(struct mt76_dev *dev)1181{1182return dev->rev & 0xffff;1183}11841185void mt76_wed_release_rx_buf(struct mtk_wed_device *wed);1186void mt76_wed_offload_disable(struct mtk_wed_device *wed);1187void mt76_wed_reset_complete(struct mtk_wed_device *wed);1188void mt76_wed_dma_reset(struct mt76_dev *dev);1189int mt76_wed_net_setup_tc(struct ieee80211_hw *hw, struct ieee80211_vif *vif,1190struct net_device *netdev, enum tc_setup_type type,1191void *type_data);1192#ifdef CONFIG_NET_MEDIATEK_SOC_WED1193u32 mt76_wed_init_rx_buf(struct mtk_wed_device *wed, int size);1194int mt76_wed_offload_enable(struct mtk_wed_device *wed);1195int mt76_wed_dma_setup(struct mt76_dev *dev, struct mt76_queue *q, bool reset);1196#else1197static inline u32 mt76_wed_init_rx_buf(struct mtk_wed_device *wed, int size)1198{1199return 0;1200}12011202static inline int mt76_wed_offload_enable(struct mtk_wed_device *wed)1203{1204return 0;1205}12061207static inline int mt76_wed_dma_setup(struct mt76_dev *dev, struct mt76_queue *q,1208bool reset)1209{1210return 0;1211}1212#endif /* CONFIG_NET_MEDIATEK_SOC_WED */12131214#define mt76xx_chip(dev) mt76_chip(&((dev)->mt76))1215#define mt76xx_rev(dev) mt76_rev(&((dev)->mt76))12161217#define mt76_init_queues(dev, ...) (dev)->mt76.queue_ops->init(&((dev)->mt76), __VA_ARGS__)1218#define mt76_queue_alloc(dev, ...) (dev)->mt76.queue_ops->alloc(&((dev)->mt76), __VA_ARGS__)1219#define mt76_tx_queue_skb_raw(dev, ...) (dev)->mt76.queue_ops->tx_queue_skb_raw(&((dev)->mt76), __VA_ARGS__)1220#define mt76_tx_queue_skb(dev, ...) (dev)->mt76.queue_ops->tx_queue_skb(&((dev)->mphy), __VA_ARGS__)1221#define mt76_queue_rx_reset(dev, ...) (dev)->mt76.queue_ops->rx_reset(&((dev)->mt76), __VA_ARGS__)1222#define mt76_queue_tx_cleanup(dev, ...) (dev)->mt76.queue_ops->tx_cleanup(&((dev)->mt76), __VA_ARGS__)1223#define mt76_queue_rx_cleanup(dev, ...) (dev)->mt76.queue_ops->rx_cleanup(&((dev)->mt76), __VA_ARGS__)1224#define mt76_queue_kick(dev, ...) (dev)->mt76.queue_ops->kick(&((dev)->mt76), __VA_ARGS__)1225#define mt76_queue_reset(dev, ...) (dev)->mt76.queue_ops->reset_q(&((dev)->mt76), __VA_ARGS__)12261227#define mt76_for_each_q_rx(dev, i) \1228for (i = 0; i < ARRAY_SIZE((dev)->q_rx); i++) \1229if ((dev)->q_rx[i].ndesc)123012311232#define mt76_dereference(p, dev) \1233rcu_dereference_protected(p, lockdep_is_held(&(dev)->mutex))12341235static inline struct mt76_wcid *1236__mt76_wcid_ptr(struct mt76_dev *dev, u16 idx)1237{1238if (idx >= ARRAY_SIZE(dev->wcid))1239return NULL;1240return rcu_dereference(dev->wcid[idx]);1241}12421243#define mt76_wcid_ptr(dev, idx) __mt76_wcid_ptr(&(dev)->mt76, idx)12441245struct mt76_dev *mt76_alloc_device(struct device *pdev, unsigned int size,1246const struct ieee80211_ops *ops,1247const struct mt76_driver_ops *drv_ops);1248int mt76_register_device(struct mt76_dev *dev, bool vht,1249struct ieee80211_rate *rates, int n_rates);1250void mt76_unregister_device(struct mt76_dev *dev);1251void mt76_free_device(struct mt76_dev *dev);1252void mt76_reset_device(struct mt76_dev *dev);1253void mt76_unregister_phy(struct mt76_phy *phy);12541255struct mt76_phy *mt76_alloc_radio_phy(struct mt76_dev *dev, unsigned int size,1256u8 band_idx);1257struct mt76_phy *mt76_alloc_phy(struct mt76_dev *dev, unsigned int size,1258const struct ieee80211_ops *ops,1259u8 band_idx);1260int mt76_register_phy(struct mt76_phy *phy, bool vht,1261struct ieee80211_rate *rates, int n_rates);1262struct mt76_phy *mt76_vif_phy(struct ieee80211_hw *hw,1263struct ieee80211_vif *vif);12641265struct dentry *mt76_register_debugfs_fops(struct mt76_phy *phy,1266const struct file_operations *ops);1267static inline struct dentry *mt76_register_debugfs(struct mt76_dev *dev)1268{1269return mt76_register_debugfs_fops(&dev->phy, NULL);1270}12711272int mt76_queues_read(struct seq_file *s, void *data);1273void mt76_seq_puts_array(struct seq_file *file, const char *str,1274s8 *val, int len);12751276int mt76_eeprom_init(struct mt76_dev *dev, int len);1277void mt76_eeprom_override(struct mt76_phy *phy);1278int mt76_get_of_data_from_mtd(struct mt76_dev *dev, void *eep, int offset, int len);1279int mt76_get_of_data_from_nvmem(struct mt76_dev *dev, void *eep,1280const char *cell_name, int len);12811282struct mt76_queue *1283mt76_init_queue(struct mt76_dev *dev, int qid, int idx, int n_desc,1284int ring_base, void *wed, u32 flags);1285static inline int mt76_init_tx_queue(struct mt76_phy *phy, int qid, int idx,1286int n_desc, int ring_base, void *wed,1287u32 flags)1288{1289struct mt76_queue *q;12901291q = mt76_init_queue(phy->dev, qid, idx, n_desc, ring_base, wed, flags);1292if (IS_ERR(q))1293return PTR_ERR(q);12941295phy->q_tx[qid] = q;12961297return 0;1298}12991300static inline int mt76_init_mcu_queue(struct mt76_dev *dev, int qid, int idx,1301int n_desc, int ring_base)1302{1303struct mt76_queue *q;13041305q = mt76_init_queue(dev, qid, idx, n_desc, ring_base, NULL, 0);1306if (IS_ERR(q))1307return PTR_ERR(q);13081309dev->q_mcu[qid] = q;13101311return 0;1312}13131314static inline struct mt76_phy *1315mt76_dev_phy(struct mt76_dev *dev, u8 phy_idx)1316{1317if ((phy_idx == MT_BAND1 && dev->phys[phy_idx]) ||1318(phy_idx == MT_BAND2 && dev->phys[phy_idx]))1319return dev->phys[phy_idx];13201321return &dev->phy;1322}13231324static inline struct ieee80211_hw *1325mt76_phy_hw(struct mt76_dev *dev, u8 phy_idx)1326{1327return mt76_dev_phy(dev, phy_idx)->hw;1328}13291330static inline u8 *1331mt76_get_txwi_ptr(struct mt76_dev *dev, struct mt76_txwi_cache *t)1332{1333return (u8 *)t - dev->drv->txwi_size;1334}13351336/* increment with wrap-around */1337static inline int mt76_incr(int val, int size)1338{1339return (val + 1) & (size - 1);1340}13411342/* decrement with wrap-around */1343static inline int mt76_decr(int val, int size)1344{1345return (val - 1) & (size - 1);1346}13471348u8 mt76_ac_to_hwq(u8 ac);13491350static inline struct ieee80211_txq *1351mtxq_to_txq(struct mt76_txq *mtxq)1352{1353void *ptr = mtxq;13541355return container_of(ptr, struct ieee80211_txq, drv_priv);1356}13571358static inline struct ieee80211_sta *1359wcid_to_sta(struct mt76_wcid *wcid)1360{1361void *ptr = wcid;13621363if (!wcid || !wcid->sta)1364return NULL;13651366if (wcid->def_wcid)1367ptr = wcid->def_wcid;13681369return container_of(ptr, struct ieee80211_sta, drv_priv);1370}13711372static inline struct mt76_tx_cb *mt76_tx_skb_cb(struct sk_buff *skb)1373{1374BUILD_BUG_ON(sizeof(struct mt76_tx_cb) >1375sizeof(IEEE80211_SKB_CB(skb)->status.status_driver_data));1376return ((void *)IEEE80211_SKB_CB(skb)->status.status_driver_data);1377}13781379static inline void *mt76_skb_get_hdr(struct sk_buff *skb)1380{1381struct mt76_rx_status mstat;1382u8 *data = skb->data;13831384/* Alignment concerns */1385BUILD_BUG_ON(sizeof(struct ieee80211_radiotap_he) % 4);1386BUILD_BUG_ON(sizeof(struct ieee80211_radiotap_he_mu) % 4);13871388mstat = *((struct mt76_rx_status *)skb->cb);13891390if (mstat.flag & RX_FLAG_RADIOTAP_HE)1391data += sizeof(struct ieee80211_radiotap_he);1392if (mstat.flag & RX_FLAG_RADIOTAP_HE_MU)1393data += sizeof(struct ieee80211_radiotap_he_mu);13941395return data;1396}13971398static inline void mt76_insert_hdr_pad(struct sk_buff *skb)1399{1400int len = ieee80211_get_hdrlen_from_skb(skb);14011402if (len % 4 == 0)1403return;14041405skb_push(skb, 2);1406memmove(skb->data, skb->data + 2, len);14071408skb->data[len] = 0;1409skb->data[len + 1] = 0;1410}14111412static inline bool mt76_is_skb_pktid(u8 pktid)1413{1414if (pktid & MT_PACKET_ID_HAS_RATE)1415return false;14161417return pktid >= MT_PACKET_ID_FIRST;1418}14191420static inline u8 mt76_tx_power_path_delta(u8 path)1421{1422static const u8 path_delta[5] = { 0, 6, 9, 12, 14 };1423u8 idx = path - 1;14241425return (idx < ARRAY_SIZE(path_delta)) ? path_delta[idx] : 0;1426}14271428static inline bool mt76_testmode_enabled(struct mt76_phy *phy)1429{1430#ifdef CONFIG_NL80211_TESTMODE1431return phy->test.state != MT76_TM_STATE_OFF;1432#else1433return false;1434#endif1435}14361437static inline bool mt76_is_testmode_skb(struct mt76_dev *dev,1438struct sk_buff *skb,1439struct ieee80211_hw **hw)1440{1441#ifdef CONFIG_NL80211_TESTMODE1442int i;14431444for (i = 0; i < ARRAY_SIZE(dev->phys); i++) {1445struct mt76_phy *phy = dev->phys[i];14461447if (phy && skb == phy->test.tx_skb) {1448*hw = dev->phys[i]->hw;1449return true;1450}1451}1452return false;1453#else1454return false;1455#endif1456}14571458void mt76_rx(struct mt76_dev *dev, enum mt76_rxq_id q, struct sk_buff *skb);1459void mt76_tx(struct mt76_phy *dev, struct ieee80211_sta *sta,1460struct mt76_wcid *wcid, struct sk_buff *skb);1461void mt76_wake_tx_queue(struct ieee80211_hw *hw, struct ieee80211_txq *txq);1462void mt76_stop_tx_queues(struct mt76_phy *phy, struct ieee80211_sta *sta,1463bool send_bar);1464void mt76_tx_check_agg_ssn(struct ieee80211_sta *sta, struct sk_buff *skb);1465void mt76_txq_schedule(struct mt76_phy *phy, enum mt76_txq_id qid);1466void mt76_txq_schedule_all(struct mt76_phy *phy);1467void mt76_tx_worker_run(struct mt76_dev *dev);1468void mt76_tx_worker(struct mt76_worker *w);1469void mt76_release_buffered_frames(struct ieee80211_hw *hw,1470struct ieee80211_sta *sta,1471u16 tids, int nframes,1472enum ieee80211_frame_release_type reason,1473bool more_data);1474bool mt76_has_tx_pending(struct mt76_phy *phy);1475int mt76_update_channel(struct mt76_phy *phy);1476void mt76_update_survey(struct mt76_phy *phy);1477void mt76_update_survey_active_time(struct mt76_phy *phy, ktime_t time);1478int mt76_get_survey(struct ieee80211_hw *hw, int idx,1479struct survey_info *survey);1480int mt76_rx_signal(u8 chain_mask, s8 *chain_signal);1481void mt76_set_stream_caps(struct mt76_phy *phy, bool vht);14821483int mt76_rx_aggr_start(struct mt76_dev *dev, struct mt76_wcid *wcid, u8 tid,1484u16 ssn, u16 size);1485void mt76_rx_aggr_stop(struct mt76_dev *dev, struct mt76_wcid *wcid, u8 tid);14861487void mt76_wcid_key_setup(struct mt76_dev *dev, struct mt76_wcid *wcid,1488struct ieee80211_key_conf *key);14891490void mt76_tx_status_lock(struct mt76_dev *dev, struct sk_buff_head *list)1491__acquires(&dev->status_lock);1492void mt76_tx_status_unlock(struct mt76_dev *dev, struct sk_buff_head *list)1493__releases(&dev->status_lock);14941495int mt76_tx_status_skb_add(struct mt76_dev *dev, struct mt76_wcid *wcid,1496struct sk_buff *skb);1497struct sk_buff *mt76_tx_status_skb_get(struct mt76_dev *dev,1498struct mt76_wcid *wcid, int pktid,1499struct sk_buff_head *list);1500void mt76_tx_status_skb_done(struct mt76_dev *dev, struct sk_buff *skb,1501struct sk_buff_head *list);1502void __mt76_tx_complete_skb(struct mt76_dev *dev, u16 wcid, struct sk_buff *skb,1503struct list_head *free_list);1504static inline void1505mt76_tx_complete_skb(struct mt76_dev *dev, u16 wcid, struct sk_buff *skb)1506{1507__mt76_tx_complete_skb(dev, wcid, skb, NULL);1508}15091510void mt76_tx_status_check(struct mt76_dev *dev, bool flush);1511int mt76_sta_state(struct ieee80211_hw *hw, struct ieee80211_vif *vif,1512struct ieee80211_sta *sta,1513enum ieee80211_sta_state old_state,1514enum ieee80211_sta_state new_state);1515void __mt76_sta_remove(struct mt76_phy *phy, struct ieee80211_vif *vif,1516struct ieee80211_sta *sta);1517void mt76_sta_pre_rcu_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,1518struct ieee80211_sta *sta);15191520int mt76_get_min_avg_rssi(struct mt76_dev *dev, u8 phy_idx);15211522s8 mt76_get_power_bound(struct mt76_phy *phy, s8 txpower);15231524int mt76_get_txpower(struct ieee80211_hw *hw, struct ieee80211_vif *vif,1525unsigned int link_id, int *dbm);1526int mt76_init_sar_power(struct ieee80211_hw *hw,1527const struct cfg80211_sar_specs *sar);1528int mt76_get_sar_power(struct mt76_phy *phy,1529struct ieee80211_channel *chan,1530int power);15311532void mt76_csa_check(struct mt76_dev *dev);1533void mt76_csa_finish(struct mt76_dev *dev);15341535int mt76_get_antenna(struct ieee80211_hw *hw, int radio_idx, u32 *tx_ant,1536u32 *rx_ant);1537int mt76_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta, bool set);1538void mt76_insert_ccmp_hdr(struct sk_buff *skb, u8 key_id);1539int mt76_get_rate(struct mt76_dev *dev,1540struct ieee80211_supported_band *sband,1541int idx, bool cck);1542int mt76_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,1543struct ieee80211_scan_request *hw_req);1544void mt76_cancel_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif);1545void mt76_sw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,1546const u8 *mac);1547void mt76_sw_scan_complete(struct ieee80211_hw *hw,1548struct ieee80211_vif *vif);1549enum mt76_dfs_state mt76_phy_dfs_state(struct mt76_phy *phy);1550int mt76_add_chanctx(struct ieee80211_hw *hw,1551struct ieee80211_chanctx_conf *conf);1552void mt76_remove_chanctx(struct ieee80211_hw *hw,1553struct ieee80211_chanctx_conf *conf);1554void mt76_change_chanctx(struct ieee80211_hw *hw,1555struct ieee80211_chanctx_conf *conf,1556u32 changed);1557int mt76_assign_vif_chanctx(struct ieee80211_hw *hw,1558struct ieee80211_vif *vif,1559struct ieee80211_bss_conf *link_conf,1560struct ieee80211_chanctx_conf *conf);1561void mt76_unassign_vif_chanctx(struct ieee80211_hw *hw,1562struct ieee80211_vif *vif,1563struct ieee80211_bss_conf *link_conf,1564struct ieee80211_chanctx_conf *conf);1565int mt76_switch_vif_chanctx(struct ieee80211_hw *hw,1566struct ieee80211_vif_chanctx_switch *vifs,1567int n_vifs,1568enum ieee80211_chanctx_switch_mode mode);1569int mt76_remain_on_channel(struct ieee80211_hw *hw, struct ieee80211_vif *vif,1570struct ieee80211_channel *chan, int duration,1571enum ieee80211_roc_type type);1572int mt76_cancel_remain_on_channel(struct ieee80211_hw *hw,1573struct ieee80211_vif *vif);1574int mt76_testmode_cmd(struct ieee80211_hw *hw, struct ieee80211_vif *vif,1575void *data, int len);1576int mt76_testmode_dump(struct ieee80211_hw *hw, struct sk_buff *skb,1577struct netlink_callback *cb, void *data, int len);1578int mt76_testmode_set_state(struct mt76_phy *phy, enum mt76_testmode_state state);1579int mt76_testmode_alloc_skb(struct mt76_phy *phy, u32 len);15801581static inline void mt76_testmode_reset(struct mt76_phy *phy, bool disable)1582{1583#ifdef CONFIG_NL80211_TESTMODE1584enum mt76_testmode_state state = MT76_TM_STATE_IDLE;15851586if (disable || phy->test.state == MT76_TM_STATE_OFF)1587state = MT76_TM_STATE_OFF;15881589mt76_testmode_set_state(phy, state);1590#endif1591}159215931594/* internal */1595static inline struct ieee80211_hw *1596mt76_tx_status_get_hw(struct mt76_dev *dev, struct sk_buff *skb)1597{1598struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);1599u8 phy_idx = (info->hw_queue & MT_TX_HW_QUEUE_PHY) >> 2;1600struct ieee80211_hw *hw = mt76_phy_hw(dev, phy_idx);16011602info->hw_queue &= ~MT_TX_HW_QUEUE_PHY;16031604return hw;1605}16061607void mt76_put_txwi(struct mt76_dev *dev, struct mt76_txwi_cache *t);1608void mt76_put_rxwi(struct mt76_dev *dev, struct mt76_txwi_cache *t);1609struct mt76_txwi_cache *mt76_get_rxwi(struct mt76_dev *dev);1610void mt76_free_pending_rxwi(struct mt76_dev *dev);1611void mt76_rx_complete(struct mt76_dev *dev, struct sk_buff_head *frames,1612struct napi_struct *napi);1613void mt76_rx_poll_complete(struct mt76_dev *dev, enum mt76_rxq_id q,1614struct napi_struct *napi);1615void mt76_rx_aggr_reorder(struct sk_buff *skb, struct sk_buff_head *frames);1616void mt76_testmode_tx_pending(struct mt76_phy *phy);1617void mt76_queue_tx_complete(struct mt76_dev *dev, struct mt76_queue *q,1618struct mt76_queue_entry *e);1619int __mt76_set_channel(struct mt76_phy *phy, struct cfg80211_chan_def *chandef,1620bool offchannel);1621int mt76_set_channel(struct mt76_phy *phy, struct cfg80211_chan_def *chandef,1622bool offchannel);1623void mt76_scan_work(struct work_struct *work);1624void mt76_abort_scan(struct mt76_dev *dev);1625void mt76_roc_complete_work(struct work_struct *work);1626void mt76_abort_roc(struct mt76_phy *phy);1627struct mt76_vif_link *mt76_get_vif_phy_link(struct mt76_phy *phy,1628struct ieee80211_vif *vif);1629void mt76_put_vif_phy_link(struct mt76_phy *phy, struct ieee80211_vif *vif,1630struct mt76_vif_link *mlink);16311632/* usb */1633static inline bool mt76u_urb_error(struct urb *urb)1634{1635return urb->status &&1636urb->status != -ECONNRESET &&1637urb->status != -ESHUTDOWN &&1638urb->status != -ENOENT;1639}16401641static inline int1642mt76u_bulk_msg(struct mt76_dev *dev, void *data, int len, int *actual_len,1643int timeout, int ep)1644{1645#if defined(__FreeBSD__) && !defined(CONFIG_USB)1646return (0);1647#else1648struct usb_interface *uintf = to_usb_interface(dev->dev);1649struct usb_device *udev = interface_to_usbdev(uintf);1650struct mt76_usb *usb = &dev->usb;1651unsigned int pipe;16521653if (actual_len)1654pipe = usb_rcvbulkpipe(udev, usb->in_ep[ep]);1655else1656pipe = usb_sndbulkpipe(udev, usb->out_ep[ep]);16571658return usb_bulk_msg(udev, pipe, data, len, actual_len, timeout);1659#endif1660}16611662void mt76_ethtool_page_pool_stats(struct mt76_dev *dev, u64 *data, int *index);1663void mt76_ethtool_worker(struct mt76_ethtool_worker_info *wi,1664struct mt76_sta_stats *stats, bool eht);1665int mt76_skb_adjust_pad(struct sk_buff *skb, int pad);1666int __mt76u_vendor_request(struct mt76_dev *dev, u8 req, u8 req_type,1667u16 val, u16 offset, void *buf, size_t len);1668int mt76u_vendor_request(struct mt76_dev *dev, u8 req,1669u8 req_type, u16 val, u16 offset,1670void *buf, size_t len);1671void mt76u_single_wr(struct mt76_dev *dev, const u8 req,1672const u16 offset, const u32 val);1673void mt76u_read_copy(struct mt76_dev *dev, u32 offset,1674void *data, int len);1675u32 ___mt76u_rr(struct mt76_dev *dev, u8 req, u8 req_type, u32 addr);1676void ___mt76u_wr(struct mt76_dev *dev, u8 req, u8 req_type,1677u32 addr, u32 val);1678int __mt76u_init(struct mt76_dev *dev, struct usb_interface *intf,1679struct mt76_bus_ops *ops);1680int mt76u_init(struct mt76_dev *dev, struct usb_interface *intf);1681int mt76u_alloc_mcu_queue(struct mt76_dev *dev);1682int mt76u_alloc_queues(struct mt76_dev *dev);1683void mt76u_stop_tx(struct mt76_dev *dev);1684void mt76u_stop_rx(struct mt76_dev *dev);1685int mt76u_resume_rx(struct mt76_dev *dev);1686void mt76u_queues_deinit(struct mt76_dev *dev);16871688int mt76s_init(struct mt76_dev *dev, struct sdio_func *func,1689const struct mt76_bus_ops *bus_ops);1690int mt76s_alloc_rx_queue(struct mt76_dev *dev, enum mt76_rxq_id qid);1691int mt76s_alloc_tx(struct mt76_dev *dev);1692void mt76s_deinit(struct mt76_dev *dev);1693void mt76s_sdio_irq(struct sdio_func *func);1694void mt76s_txrx_worker(struct mt76_sdio *sdio);1695bool mt76s_txqs_empty(struct mt76_dev *dev);1696int mt76s_hw_init(struct mt76_dev *dev, struct sdio_func *func,1697int hw_ver);1698u32 mt76s_rr(struct mt76_dev *dev, u32 offset);1699void mt76s_wr(struct mt76_dev *dev, u32 offset, u32 val);1700u32 mt76s_rmw(struct mt76_dev *dev, u32 offset, u32 mask, u32 val);1701u32 mt76s_read_pcr(struct mt76_dev *dev);1702void mt76s_write_copy(struct mt76_dev *dev, u32 offset,1703const void *data, int len);1704void mt76s_read_copy(struct mt76_dev *dev, u32 offset,1705void *data, int len);1706int mt76s_wr_rp(struct mt76_dev *dev, u32 base,1707const struct mt76_reg_pair *data,1708int len);1709int mt76s_rd_rp(struct mt76_dev *dev, u32 base,1710struct mt76_reg_pair *data, int len);17111712struct sk_buff *1713__mt76_mcu_msg_alloc(struct mt76_dev *dev, const void *data,1714int len, int data_len, gfp_t gfp);1715static inline struct sk_buff *1716mt76_mcu_msg_alloc(struct mt76_dev *dev, const void *data,1717int data_len)1718{1719return __mt76_mcu_msg_alloc(dev, data, data_len, data_len, GFP_KERNEL);1720}17211722void mt76_mcu_rx_event(struct mt76_dev *dev, struct sk_buff *skb);1723struct sk_buff *mt76_mcu_get_response(struct mt76_dev *dev,1724unsigned long expires);1725int mt76_mcu_send_and_get_msg(struct mt76_dev *dev, int cmd, const void *data,1726int len, bool wait_resp, struct sk_buff **ret);1727int mt76_mcu_skb_send_and_get_msg(struct mt76_dev *dev, struct sk_buff *skb,1728int cmd, bool wait_resp, struct sk_buff **ret);1729#if defined(__linux__)1730int __mt76_mcu_send_firmware(struct mt76_dev *dev, int cmd, const void *data,1731#elif defined(__FreeBSD__)1732int __mt76_mcu_send_firmware(struct mt76_dev *dev, int cmd, const u8 *data,1733#endif1734int len, int max_len);1735static inline int1736mt76_mcu_send_firmware(struct mt76_dev *dev, int cmd, const void *data,1737int len)1738{1739int max_len = 4096 - dev->mcu_ops->headroom;17401741return __mt76_mcu_send_firmware(dev, cmd, data, len, max_len);1742}17431744static inline int1745mt76_mcu_send_msg(struct mt76_dev *dev, int cmd, const void *data, int len,1746bool wait_resp)1747{1748return mt76_mcu_send_and_get_msg(dev, cmd, data, len, wait_resp, NULL);1749}17501751static inline int1752mt76_mcu_skb_send_msg(struct mt76_dev *dev, struct sk_buff *skb, int cmd,1753bool wait_resp)1754{1755return mt76_mcu_skb_send_and_get_msg(dev, skb, cmd, wait_resp, NULL);1756}17571758void mt76_set_irq_mask(struct mt76_dev *dev, u32 addr, u32 clear, u32 set);17591760struct device_node *1761mt76_find_power_limits_node(struct mt76_dev *dev);1762struct device_node *1763mt76_find_channel_node(struct device_node *np, struct ieee80211_channel *chan);17641765s8 mt76_get_rate_power_limits(struct mt76_phy *phy,1766struct ieee80211_channel *chan,1767struct mt76_power_limits *dest,1768s8 target_power);17691770static inline bool mt76_queue_is_rx(struct mt76_dev *dev, struct mt76_queue *q)1771{1772int i;17731774for (i = 0; i < ARRAY_SIZE(dev->q_rx); i++) {1775if (q == &dev->q_rx[i])1776return true;1777}17781779return false;1780}17811782static inline bool mt76_queue_is_wed_tx_free(struct mt76_queue *q)1783{1784return (q->flags & MT_QFLAG_WED) &&1785FIELD_GET(MT_QFLAG_WED_TYPE, q->flags) == MT76_WED_Q_TXFREE;1786}17871788static inline bool mt76_queue_is_wed_rro(struct mt76_queue *q)1789{1790return q->flags & MT_QFLAG_WED_RRO;1791}17921793static inline bool mt76_queue_is_wed_rro_ind(struct mt76_queue *q)1794{1795return mt76_queue_is_wed_rro(q) &&1796FIELD_GET(MT_QFLAG_WED_TYPE, q->flags) == MT76_WED_RRO_Q_IND;1797}17981799static inline bool mt76_queue_is_wed_rro_data(struct mt76_queue *q)1800{1801return mt76_queue_is_wed_rro(q) &&1802(FIELD_GET(MT_QFLAG_WED_TYPE, q->flags) == MT76_WED_RRO_Q_DATA ||1803FIELD_GET(MT_QFLAG_WED_TYPE, q->flags) == MT76_WED_RRO_Q_MSDU_PG);1804}18051806static inline bool mt76_queue_is_wed_rx(struct mt76_queue *q)1807{1808if (!(q->flags & MT_QFLAG_WED))1809return false;18101811return FIELD_GET(MT_QFLAG_WED_TYPE, q->flags) == MT76_WED_Q_RX ||1812mt76_queue_is_wed_rro_ind(q) || mt76_queue_is_wed_rro_data(q);18131814}18151816struct mt76_txwi_cache *1817mt76_token_release(struct mt76_dev *dev, int token, bool *wake);1818int mt76_token_consume(struct mt76_dev *dev, struct mt76_txwi_cache **ptxwi);1819void __mt76_set_tx_blocked(struct mt76_dev *dev, bool blocked);1820struct mt76_txwi_cache *mt76_rx_token_release(struct mt76_dev *dev, int token);1821int mt76_rx_token_consume(struct mt76_dev *dev, void *ptr,1822struct mt76_txwi_cache *r, dma_addr_t phys);1823int mt76_create_page_pool(struct mt76_dev *dev, struct mt76_queue *q);1824static inline void mt76_put_page_pool_buf(void *buf, bool allow_direct)1825{1826struct page *page = virt_to_head_page(buf);18271828page_pool_put_full_page(pp_page_to_nmdesc(page)->pp, page,1829allow_direct);1830}18311832static inline void *1833mt76_get_page_pool_buf(struct mt76_queue *q, u32 *offset, u32 size)1834{1835struct page *page;18361837page = page_pool_dev_alloc_frag(q->page_pool, offset, size);1838if (!page)1839return NULL;18401841#if defined(__linux__)1842return page_address(page) + *offset;1843#elif defined(__FreeBSD__)1844return (void *)((uintptr_t)page_address(page) + *offset);1845#endif1846}18471848static inline void mt76_set_tx_blocked(struct mt76_dev *dev, bool blocked)1849{1850spin_lock_bh(&dev->token_lock);1851__mt76_set_tx_blocked(dev, blocked);1852spin_unlock_bh(&dev->token_lock);1853}18541855static inline int1856mt76_token_get(struct mt76_dev *dev, struct mt76_txwi_cache **ptxwi)1857{1858int token;18591860spin_lock_bh(&dev->token_lock);1861token = idr_alloc(&dev->token, *ptxwi, 0, dev->token_size, GFP_ATOMIC);1862spin_unlock_bh(&dev->token_lock);18631864return token;1865}18661867static inline struct mt76_txwi_cache *1868mt76_token_put(struct mt76_dev *dev, int token)1869{1870struct mt76_txwi_cache *txwi;18711872spin_lock_bh(&dev->token_lock);1873txwi = idr_remove(&dev->token, token);1874spin_unlock_bh(&dev->token_lock);18751876return txwi;1877}18781879void mt76_wcid_init(struct mt76_wcid *wcid, u8 band_idx);1880void mt76_wcid_cleanup(struct mt76_dev *dev, struct mt76_wcid *wcid);1881void mt76_wcid_add_poll(struct mt76_dev *dev, struct mt76_wcid *wcid);18821883static inline void1884mt76_vif_init(struct ieee80211_vif *vif, struct mt76_vif_data *mvif)1885{1886struct mt76_vif_link *mlink = (struct mt76_vif_link *)vif->drv_priv;18871888mlink->mvif = mvif;1889rcu_assign_pointer(mvif->link[0], mlink);1890}18911892void mt76_vif_cleanup(struct mt76_dev *dev, struct ieee80211_vif *vif);18931894static inline struct mt76_vif_link *1895mt76_vif_link(struct mt76_dev *dev, struct ieee80211_vif *vif, int link_id)1896{1897struct mt76_vif_link *mlink = (struct mt76_vif_link *)vif->drv_priv;1898struct mt76_vif_data *mvif = mlink->mvif;18991900if (!link_id)1901return mlink;19021903return mt76_dereference(mvif->link[link_id], dev);1904}19051906static inline struct mt76_vif_link *1907mt76_vif_conf_link(struct mt76_dev *dev, struct ieee80211_vif *vif,1908struct ieee80211_bss_conf *link_conf)1909{1910struct mt76_vif_link *mlink = (struct mt76_vif_link *)vif->drv_priv;1911struct mt76_vif_data *mvif = mlink->mvif;19121913if (link_conf == &vif->bss_conf || !link_conf->link_id)1914return mlink;19151916return mt76_dereference(mvif->link[link_conf->link_id], dev);1917}19181919static inline struct mt76_phy *1920mt76_vif_link_phy(struct mt76_vif_link *mlink)1921{1922struct mt76_chanctx *ctx;19231924if (!mlink->ctx)1925return NULL;19261927ctx = (struct mt76_chanctx *)mlink->ctx->drv_priv;19281929return ctx->phy;1930}19311932#endif193319341935