mt76.h 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110
  1. /* SPDX-License-Identifier: BSD-3-Clause-Clear */
  2. /*
  3. * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
  4. */
  5. #ifndef __MT76_H
  6. #define __MT76_H
  7. #include <linux/kernel.h>
  8. #include <linux/io.h>
  9. #include <linux/spinlock.h>
  10. #include <linux/skbuff.h>
  11. #include <linux/leds.h>
  12. #include <linux/usb.h>
  13. #include <linux/average.h>
  14. #include <linux/soc/airoha/airoha_offload.h>
  15. #include <linux/soc/mediatek/mtk_wed.h>
  16. #include <net/mac80211.h>
  17. #include <net/page_pool/helpers.h>
  18. #include "util.h"
  19. #include "testmode.h"
  20. #define MT_MCU_RING_SIZE 32
  21. #define MT_RX_BUF_SIZE 2048
  22. #define MT_SKB_HEAD_LEN 256
  23. #define MT_MAX_NON_AQL_PKT 16
  24. #define MT_TXQ_FREE_THR 32
  25. #define MT76_TOKEN_FREE_THR 64
  26. #define MT_QFLAG_WED_RING GENMASK(1, 0)
  27. #define MT_QFLAG_WED_TYPE GENMASK(4, 2)
  28. #define MT_QFLAG_WED BIT(5)
  29. #define MT_QFLAG_WED_RRO BIT(6)
  30. #define MT_QFLAG_WED_RRO_EN BIT(7)
  31. #define MT_QFLAG_EMI_EN BIT(8)
  32. #define MT_QFLAG_NPU BIT(9)
  33. #define __MT_WED_Q(_type, _n) (MT_QFLAG_WED | \
  34. FIELD_PREP(MT_QFLAG_WED_TYPE, _type) | \
  35. FIELD_PREP(MT_QFLAG_WED_RING, _n))
  36. #define __MT_WED_RRO_Q(_type, _n) (MT_QFLAG_WED_RRO | __MT_WED_Q(_type, _n))
  37. #define MT_WED_Q_TX(_n) __MT_WED_Q(MT76_WED_Q_TX, _n)
  38. #define MT_WED_Q_RX(_n) __MT_WED_Q(MT76_WED_Q_RX, _n)
  39. #define MT_WED_Q_TXFREE __MT_WED_Q(MT76_WED_Q_TXFREE, 0)
  40. #define MT_WED_RRO_Q_DATA(_n) __MT_WED_RRO_Q(MT76_WED_RRO_Q_DATA, _n)
  41. #define MT_WED_RRO_Q_MSDU_PG(_n) __MT_WED_RRO_Q(MT76_WED_RRO_Q_MSDU_PG, _n)
  42. #define MT_WED_RRO_Q_IND __MT_WED_RRO_Q(MT76_WED_RRO_Q_IND, 0)
  43. #define MT_WED_RRO_Q_RXDMAD_C __MT_WED_RRO_Q(MT76_WED_RRO_Q_RXDMAD_C, 0)
  44. #define __MT_NPU_Q(_type, _n) (MT_QFLAG_NPU | \
  45. FIELD_PREP(MT_QFLAG_WED_TYPE, _type) | \
  46. FIELD_PREP(MT_QFLAG_WED_RING, _n))
  47. #define MT_NPU_Q_TX(_n) __MT_NPU_Q(MT76_WED_Q_TX, _n)
  48. #define MT_NPU_Q_RX(_n) __MT_NPU_Q(MT76_WED_Q_RX, _n)
  49. struct mt76_dev;
  50. struct mt76_phy;
  51. struct mt76_wcid;
  52. struct mt76s_intr;
  53. struct mt76_chanctx;
  54. struct mt76_vif_link;
  55. struct mt76_reg_pair {
  56. u32 reg;
  57. u32 value;
  58. };
  59. enum mt76_bus_type {
  60. MT76_BUS_MMIO,
  61. MT76_BUS_USB,
  62. MT76_BUS_SDIO,
  63. };
  64. enum mt76_wed_type {
  65. MT76_WED_Q_TX,
  66. MT76_WED_Q_TXFREE,
  67. MT76_WED_Q_RX,
  68. MT76_WED_RRO_Q_DATA,
  69. MT76_WED_RRO_Q_MSDU_PG,
  70. MT76_WED_RRO_Q_IND,
  71. MT76_WED_RRO_Q_RXDMAD_C,
  72. };
  73. enum mt76_hwrro_mode {
  74. MT76_HWRRO_OFF,
  75. MT76_HWRRO_V3,
  76. MT76_HWRRO_V3_1,
  77. };
  78. struct mt76_bus_ops {
  79. u32 (*rr)(struct mt76_dev *dev, u32 offset);
  80. void (*wr)(struct mt76_dev *dev, u32 offset, u32 val);
  81. u32 (*rmw)(struct mt76_dev *dev, u32 offset, u32 mask, u32 val);
  82. void (*write_copy)(struct mt76_dev *dev, u32 offset, const void *data,
  83. int len);
  84. void (*read_copy)(struct mt76_dev *dev, u32 offset, void *data,
  85. int len);
  86. int (*wr_rp)(struct mt76_dev *dev, u32 base,
  87. const struct mt76_reg_pair *rp, int len);
  88. int (*rd_rp)(struct mt76_dev *dev, u32 base,
  89. struct mt76_reg_pair *rp, int len);
  90. enum mt76_bus_type type;
  91. };
  92. #define mt76_is_usb(dev) ((dev)->bus->type == MT76_BUS_USB)
  93. #define mt76_is_mmio(dev) ((dev)->bus->type == MT76_BUS_MMIO)
  94. #define mt76_is_sdio(dev) ((dev)->bus->type == MT76_BUS_SDIO)
  95. enum mt76_txq_id {
  96. MT_TXQ_VO = IEEE80211_AC_VO,
  97. MT_TXQ_VI = IEEE80211_AC_VI,
  98. MT_TXQ_BE = IEEE80211_AC_BE,
  99. MT_TXQ_BK = IEEE80211_AC_BK,
  100. MT_TXQ_PSD,
  101. MT_TXQ_BEACON,
  102. MT_TXQ_CAB,
  103. __MT_TXQ_MAX
  104. };
  105. enum mt76_mcuq_id {
  106. MT_MCUQ_WM,
  107. MT_MCUQ_WA,
  108. MT_MCUQ_FWDL,
  109. __MT_MCUQ_MAX
  110. };
  111. enum mt76_rxq_id {
  112. MT_RXQ_MAIN,
  113. MT_RXQ_MCU,
  114. MT_RXQ_MCU_WA,
  115. MT_RXQ_BAND1,
  116. MT_RXQ_BAND1_WA,
  117. MT_RXQ_MAIN_WA,
  118. MT_RXQ_BAND2,
  119. MT_RXQ_BAND2_WA,
  120. MT_RXQ_RRO_BAND0,
  121. MT_RXQ_RRO_BAND1,
  122. MT_RXQ_RRO_BAND2,
  123. MT_RXQ_MSDU_PAGE_BAND0,
  124. MT_RXQ_MSDU_PAGE_BAND1,
  125. MT_RXQ_MSDU_PAGE_BAND2,
  126. MT_RXQ_TXFREE_BAND0,
  127. MT_RXQ_TXFREE_BAND1,
  128. MT_RXQ_TXFREE_BAND2,
  129. MT_RXQ_RRO_IND,
  130. MT_RXQ_RRO_RXDMAD_C,
  131. MT_RXQ_NPU0,
  132. MT_RXQ_NPU1,
  133. __MT_RXQ_MAX
  134. };
  135. enum mt76_band_id {
  136. MT_BAND0,
  137. MT_BAND1,
  138. MT_BAND2,
  139. __MT_MAX_BAND
  140. };
  141. enum mt76_cipher_type {
  142. MT_CIPHER_NONE,
  143. MT_CIPHER_WEP40,
  144. MT_CIPHER_TKIP,
  145. MT_CIPHER_TKIP_NO_MIC,
  146. MT_CIPHER_AES_CCMP,
  147. MT_CIPHER_WEP104,
  148. MT_CIPHER_BIP_CMAC_128,
  149. MT_CIPHER_WEP128,
  150. MT_CIPHER_WAPI,
  151. MT_CIPHER_CCMP_CCX,
  152. MT_CIPHER_CCMP_256,
  153. MT_CIPHER_GCMP,
  154. MT_CIPHER_GCMP_256,
  155. };
  156. enum mt76_dfs_state {
  157. MT_DFS_STATE_UNKNOWN,
  158. MT_DFS_STATE_DISABLED,
  159. MT_DFS_STATE_CAC,
  160. MT_DFS_STATE_ACTIVE,
  161. };
  162. #define MT76_RNR_SCAN_MAX_BSSIDS 16
  163. struct mt76_scan_rnr_param {
  164. u8 bssid[MT76_RNR_SCAN_MAX_BSSIDS][ETH_ALEN];
  165. u8 channel[MT76_RNR_SCAN_MAX_BSSIDS];
  166. u8 random_mac[ETH_ALEN];
  167. u8 seq_num;
  168. u8 bssid_num;
  169. u32 sreq_flag;
  170. };
  171. struct mt76_queue_buf {
  172. dma_addr_t addr;
  173. u16 len:15,
  174. skip_unmap:1;
  175. };
  176. struct mt76_tx_info {
  177. struct mt76_queue_buf buf[32];
  178. struct sk_buff *skb;
  179. int nbuf;
  180. u32 info;
  181. };
  182. struct mt76_queue_entry {
  183. union {
  184. void *buf;
  185. struct sk_buff *skb;
  186. };
  187. union {
  188. struct mt76_txwi_cache *txwi;
  189. struct urb *urb;
  190. int buf_sz;
  191. };
  192. dma_addr_t dma_addr[2];
  193. u16 dma_len[2];
  194. u16 wcid;
  195. bool skip_buf0:1;
  196. bool skip_buf1:1;
  197. bool done:1;
  198. };
  199. struct mt76_queue_regs {
  200. u32 desc_base;
  201. u32 ring_size;
  202. u32 cpu_idx;
  203. u32 dma_idx;
  204. } __packed __aligned(4);
  205. struct mt76_queue {
  206. struct mt76_queue_regs __iomem *regs;
  207. spinlock_t lock;
  208. spinlock_t cleanup_lock;
  209. struct mt76_queue_entry *entry;
  210. struct mt76_rro_desc *rro_desc;
  211. struct mt76_desc *desc;
  212. u16 first;
  213. u16 head;
  214. u16 tail;
  215. u8 hw_idx;
  216. u8 ep;
  217. int ndesc;
  218. int queued;
  219. int buf_size;
  220. bool stopped;
  221. bool blocked;
  222. u8 buf_offset;
  223. u16 flags;
  224. u8 magic_cnt;
  225. __le16 *emi_cpu_idx;
  226. struct mtk_wed_device *wed;
  227. struct mt76_dev *dev;
  228. u32 wed_regs;
  229. dma_addr_t desc_dma;
  230. struct sk_buff *rx_head;
  231. struct page_pool *page_pool;
  232. };
  233. struct mt76_mcu_ops {
  234. unsigned int max_retry;
  235. u32 headroom;
  236. u32 tailroom;
  237. int (*mcu_send_msg)(struct mt76_dev *dev, int cmd, const void *data,
  238. int len, bool wait_resp);
  239. int (*mcu_skb_prepare_msg)(struct mt76_dev *dev, struct sk_buff *skb,
  240. int cmd, int *seq);
  241. int (*mcu_skb_send_msg)(struct mt76_dev *dev, struct sk_buff *skb,
  242. int cmd, int *seq);
  243. int (*mcu_parse_response)(struct mt76_dev *dev, int cmd,
  244. struct sk_buff *skb, int seq);
  245. u32 (*mcu_rr)(struct mt76_dev *dev, u32 offset);
  246. void (*mcu_wr)(struct mt76_dev *dev, u32 offset, u32 val);
  247. int (*mcu_wr_rp)(struct mt76_dev *dev, u32 base,
  248. const struct mt76_reg_pair *rp, int len);
  249. int (*mcu_rd_rp)(struct mt76_dev *dev, u32 base,
  250. struct mt76_reg_pair *rp, int len);
  251. int (*mcu_restart)(struct mt76_dev *dev);
  252. };
  253. struct mt76_queue_ops {
  254. int (*init)(struct mt76_dev *dev,
  255. int (*poll)(struct napi_struct *napi, int budget));
  256. int (*alloc)(struct mt76_dev *dev, struct mt76_queue *q,
  257. int idx, int n_desc, int bufsize,
  258. u32 ring_base);
  259. int (*tx_queue_skb)(struct mt76_phy *phy, struct mt76_queue *q,
  260. enum mt76_txq_id qid, struct sk_buff *skb,
  261. struct mt76_wcid *wcid, struct ieee80211_sta *sta);
  262. int (*tx_queue_skb_raw)(struct mt76_dev *dev, struct mt76_queue *q,
  263. struct sk_buff *skb, u32 tx_info);
  264. void *(*dequeue)(struct mt76_dev *dev, struct mt76_queue *q, bool flush,
  265. int *len, u32 *info, bool *more);
  266. void (*rx_reset)(struct mt76_dev *dev, enum mt76_rxq_id qid);
  267. void (*tx_cleanup)(struct mt76_dev *dev, struct mt76_queue *q,
  268. bool flush);
  269. void (*rx_queue_init)(struct mt76_dev *dev, enum mt76_rxq_id qid,
  270. int (*poll)(struct napi_struct *napi, int budget));
  271. void (*rx_cleanup)(struct mt76_dev *dev, struct mt76_queue *q);
  272. void (*kick)(struct mt76_dev *dev, struct mt76_queue *q);
  273. void (*reset_q)(struct mt76_dev *dev, struct mt76_queue *q,
  274. bool reset_idx);
  275. };
  276. enum mt76_phy_type {
  277. MT_PHY_TYPE_CCK,
  278. MT_PHY_TYPE_OFDM,
  279. MT_PHY_TYPE_HT,
  280. MT_PHY_TYPE_HT_GF,
  281. MT_PHY_TYPE_VHT,
  282. MT_PHY_TYPE_HE_SU = 8,
  283. MT_PHY_TYPE_HE_EXT_SU,
  284. MT_PHY_TYPE_HE_TB,
  285. MT_PHY_TYPE_HE_MU,
  286. MT_PHY_TYPE_EHT_SU = 13,
  287. MT_PHY_TYPE_EHT_TRIG,
  288. MT_PHY_TYPE_EHT_MU,
  289. __MT_PHY_TYPE_MAX,
  290. };
  291. struct mt76_sta_stats {
  292. u64 tx_mode[__MT_PHY_TYPE_MAX];
  293. u64 tx_bw[5]; /* 20, 40, 80, 160, 320 */
  294. u64 tx_nss[4]; /* 1, 2, 3, 4 */
  295. u64 tx_mcs[16]; /* mcs idx */
  296. u64 tx_bytes;
  297. /* WED TX */
  298. u32 tx_packets; /* unit: MSDU */
  299. u32 tx_retries;
  300. u32 tx_failed;
  301. /* WED RX */
  302. u64 rx_bytes;
  303. u32 rx_packets;
  304. u32 rx_errors;
  305. u32 rx_drops;
  306. };
  307. enum mt76_wcid_flags {
  308. MT_WCID_FLAG_CHECK_PS,
  309. MT_WCID_FLAG_PS,
  310. MT_WCID_FLAG_4ADDR,
  311. MT_WCID_FLAG_HDR_TRANS,
  312. };
  313. #define MT76_N_WCIDS 1088
  314. /* stored in ieee80211_tx_info::hw_queue */
  315. #define MT_TX_HW_QUEUE_PHY GENMASK(3, 2)
  316. DECLARE_EWMA(signal, 10, 8);
  317. #define MT_WCID_TX_INFO_RATE GENMASK(15, 0)
  318. #define MT_WCID_TX_INFO_NSS GENMASK(17, 16)
  319. #define MT_WCID_TX_INFO_TXPWR_ADJ GENMASK(25, 18)
  320. #define MT_WCID_TX_INFO_SET BIT(31)
  321. struct mt76_wcid {
  322. struct mt76_rx_tid __rcu *aggr[IEEE80211_NUM_TIDS];
  323. atomic_t non_aql_packets;
  324. unsigned long flags;
  325. struct ewma_signal rssi;
  326. int inactive_count;
  327. struct rate_info rate;
  328. unsigned long ampdu_state;
  329. u16 idx;
  330. u8 hw_key_idx;
  331. u8 hw_key_idx2;
  332. u8 offchannel:1;
  333. u8 sta:1;
  334. u8 sta_disabled:1;
  335. u8 amsdu:1;
  336. u8 phy_idx:2;
  337. u8 link_id:4;
  338. bool link_valid;
  339. u8 rx_check_pn;
  340. u8 rx_key_pn[IEEE80211_NUM_TIDS + 1][6];
  341. u16 cipher;
  342. u32 tx_info;
  343. bool sw_iv;
  344. struct list_head tx_list;
  345. struct sk_buff_head tx_pending;
  346. struct sk_buff_head tx_offchannel;
  347. struct list_head list;
  348. struct idr pktid;
  349. struct mt76_sta_stats stats;
  350. struct list_head poll_list;
  351. struct mt76_wcid *def_wcid;
  352. };
  353. struct mt76_txq {
  354. u16 wcid;
  355. u16 agg_ssn;
  356. bool send_bar;
  357. bool aggr;
  358. };
  359. /* data0 */
  360. #define RRO_IND_DATA0_IND_REASON_MASK GENMASK(31, 28)
  361. #define RRO_IND_DATA0_START_SEQ_MASK GENMASK(27, 16)
  362. #define RRO_IND_DATA0_SEQ_ID_MASK GENMASK(11, 0)
  363. /* data1 */
  364. #define RRO_IND_DATA1_MAGIC_CNT_MASK GENMASK(31, 29)
  365. #define RRO_IND_DATA1_IND_COUNT_MASK GENMASK(12, 0)
  366. struct mt76_wed_rro_ind {
  367. __le32 data0;
  368. __le32 data1;
  369. };
  370. struct mt76_txwi_cache {
  371. struct list_head list;
  372. dma_addr_t dma_addr;
  373. union {
  374. struct sk_buff *skb;
  375. void *ptr;
  376. };
  377. u8 qid;
  378. };
  379. struct mt76_rx_tid {
  380. struct rcu_head rcu_head;
  381. struct mt76_dev *dev;
  382. spinlock_t lock;
  383. struct delayed_work reorder_work;
  384. u16 id;
  385. u16 head;
  386. u16 size;
  387. u16 nframes;
  388. u8 num;
  389. u8 started:1, stopped:1, timer_pending:1;
  390. struct sk_buff *reorder_buf[] __counted_by(size);
  391. };
  392. #define MT_TX_CB_DMA_DONE BIT(0)
  393. #define MT_TX_CB_TXS_DONE BIT(1)
  394. #define MT_TX_CB_TXS_FAILED BIT(2)
  395. #define MT_PACKET_ID_MASK GENMASK(6, 0)
  396. #define MT_PACKET_ID_NO_ACK 0
  397. #define MT_PACKET_ID_NO_SKB 1
  398. #define MT_PACKET_ID_WED 2
  399. #define MT_PACKET_ID_FIRST 3
  400. #define MT_PACKET_ID_HAS_RATE BIT(7)
  401. /* This is timer for when to give up when waiting for TXS callback,
  402. * with starting time being the time at which the DMA_DONE callback
  403. * was seen (so, we know packet was processed then, it should not take
  404. * long after that for firmware to send the TXS callback if it is going
  405. * to do so.)
  406. */
  407. #define MT_TX_STATUS_SKB_TIMEOUT (HZ / 4)
  408. struct mt76_tx_cb {
  409. unsigned long jiffies;
  410. u16 wcid;
  411. u8 pktid;
  412. u8 flags;
  413. };
  414. enum {
  415. MT76_STATE_INITIALIZED,
  416. MT76_STATE_REGISTERED,
  417. MT76_STATE_RUNNING,
  418. MT76_STATE_MCU_RUNNING,
  419. MT76_SCANNING,
  420. MT76_HW_SCANNING,
  421. MT76_HW_SCHED_SCANNING,
  422. MT76_RESTART,
  423. MT76_RESET,
  424. MT76_MCU_RESET,
  425. MT76_REMOVED,
  426. MT76_READING_STATS,
  427. MT76_STATE_POWER_OFF,
  428. MT76_STATE_SUSPEND,
  429. MT76_STATE_ROC,
  430. MT76_STATE_PM,
  431. MT76_STATE_WED_RESET,
  432. };
  433. enum mt76_sta_event {
  434. MT76_STA_EVENT_ASSOC,
  435. MT76_STA_EVENT_AUTHORIZE,
  436. MT76_STA_EVENT_DISASSOC,
  437. };
  438. struct mt76_hw_cap {
  439. bool has_2ghz;
  440. bool has_5ghz;
  441. bool has_6ghz;
  442. };
  443. #define MT_DRV_TXWI_NO_FREE BIT(0)
  444. #define MT_DRV_TX_ALIGNED4_SKBS BIT(1)
  445. #define MT_DRV_SW_RX_AIRTIME BIT(2)
  446. #define MT_DRV_RX_DMA_HDR BIT(3)
  447. #define MT_DRV_HW_MGMT_TXQ BIT(4)
  448. #define MT_DRV_AMSDU_OFFLOAD BIT(5)
  449. #define MT_DRV_IGNORE_TXS_FAILED BIT(6)
  450. struct mt76_driver_ops {
  451. u32 drv_flags;
  452. u32 survey_flags;
  453. u16 txwi_size;
  454. u16 token_size;
  455. u8 mcs_rates;
  456. unsigned int link_data_size;
  457. void (*update_survey)(struct mt76_phy *phy);
  458. int (*set_channel)(struct mt76_phy *phy);
  459. int (*tx_prepare_skb)(struct mt76_dev *dev, void *txwi_ptr,
  460. enum mt76_txq_id qid, struct mt76_wcid *wcid,
  461. struct ieee80211_sta *sta,
  462. struct mt76_tx_info *tx_info);
  463. void (*tx_complete_skb)(struct mt76_dev *dev,
  464. struct mt76_queue_entry *e);
  465. bool (*tx_status_data)(struct mt76_dev *dev, u8 *update);
  466. bool (*rx_check)(struct mt76_dev *dev, void *data, int len);
  467. void (*rx_skb)(struct mt76_dev *dev, enum mt76_rxq_id q,
  468. struct sk_buff *skb, u32 *info);
  469. void (*rx_poll_complete)(struct mt76_dev *dev, enum mt76_rxq_id q);
  470. void (*rx_rro_ind_process)(struct mt76_dev *dev, void *data);
  471. int (*rx_rro_add_msdu_page)(struct mt76_dev *dev, struct mt76_queue *q,
  472. dma_addr_t p, void *data);
  473. void (*sta_ps)(struct mt76_dev *dev, struct ieee80211_sta *sta,
  474. bool ps);
  475. int (*sta_add)(struct mt76_dev *dev, struct ieee80211_vif *vif,
  476. struct ieee80211_sta *sta);
  477. int (*sta_event)(struct mt76_dev *dev, struct ieee80211_vif *vif,
  478. struct ieee80211_sta *sta, enum mt76_sta_event ev);
  479. void (*sta_remove)(struct mt76_dev *dev, struct ieee80211_vif *vif,
  480. struct ieee80211_sta *sta);
  481. int (*vif_link_add)(struct mt76_phy *phy, struct ieee80211_vif *vif,
  482. struct ieee80211_bss_conf *link_conf,
  483. struct mt76_vif_link *mlink);
  484. void (*vif_link_remove)(struct mt76_phy *phy,
  485. struct ieee80211_vif *vif,
  486. struct ieee80211_bss_conf *link_conf,
  487. struct mt76_vif_link *mlink);
  488. };
  489. struct mt76_channel_state {
  490. u64 cc_active;
  491. u64 cc_busy;
  492. u64 cc_rx;
  493. u64 cc_bss_rx;
  494. u64 cc_tx;
  495. s8 noise;
  496. };
  497. struct mt76_sband {
  498. struct ieee80211_supported_band sband;
  499. struct mt76_channel_state *chan;
  500. };
  501. /* addr req mask */
  502. #define MT_VEND_TYPE_EEPROM BIT(31)
  503. #define MT_VEND_TYPE_CFG BIT(30)
  504. #define MT_VEND_TYPE_MASK (MT_VEND_TYPE_EEPROM | MT_VEND_TYPE_CFG)
  505. #define MT_VEND_ADDR(type, n) (MT_VEND_TYPE_##type | (n))
  506. enum mt_vendor_req {
  507. MT_VEND_DEV_MODE = 0x1,
  508. MT_VEND_WRITE = 0x2,
  509. MT_VEND_POWER_ON = 0x4,
  510. MT_VEND_MULTI_WRITE = 0x6,
  511. MT_VEND_MULTI_READ = 0x7,
  512. MT_VEND_READ_EEPROM = 0x9,
  513. MT_VEND_WRITE_FCE = 0x42,
  514. MT_VEND_WRITE_CFG = 0x46,
  515. MT_VEND_READ_CFG = 0x47,
  516. MT_VEND_READ_EXT = 0x63,
  517. MT_VEND_WRITE_EXT = 0x66,
  518. MT_VEND_FEATURE_SET = 0x91,
  519. };
  520. enum mt76u_in_ep {
  521. MT_EP_IN_PKT_RX,
  522. MT_EP_IN_CMD_RESP,
  523. __MT_EP_IN_MAX,
  524. };
  525. enum mt76u_out_ep {
  526. MT_EP_OUT_INBAND_CMD,
  527. MT_EP_OUT_AC_BE,
  528. MT_EP_OUT_AC_BK,
  529. MT_EP_OUT_AC_VI,
  530. MT_EP_OUT_AC_VO,
  531. MT_EP_OUT_HCCA,
  532. __MT_EP_OUT_MAX,
  533. };
  534. struct mt76_mcu {
  535. struct mutex mutex;
  536. u32 msg_seq;
  537. int timeout;
  538. struct sk_buff_head res_q;
  539. wait_queue_head_t wait;
  540. };
  541. #define MT_TX_SG_MAX_SIZE 8
  542. #define MT_RX_SG_MAX_SIZE 4
  543. #define MT_NUM_TX_ENTRIES 256
  544. #define MT_NUM_RX_ENTRIES 128
  545. #define MCU_RESP_URB_SIZE 1024
  546. struct mt76_usb {
  547. struct mutex usb_ctrl_mtx;
  548. u8 *data;
  549. u16 data_len;
  550. struct mt76_worker status_worker;
  551. struct mt76_worker rx_worker;
  552. struct work_struct stat_work;
  553. u8 out_ep[__MT_EP_OUT_MAX];
  554. u8 in_ep[__MT_EP_IN_MAX];
  555. bool sg_en;
  556. struct mt76u_mcu {
  557. u8 *data;
  558. /* multiple reads */
  559. struct mt76_reg_pair *rp;
  560. int rp_len;
  561. u32 base;
  562. } mcu;
  563. };
  564. #define MT76S_XMIT_BUF_SZ 0x3fe00
  565. #define MT76S_NUM_TX_ENTRIES 256
  566. #define MT76S_NUM_RX_ENTRIES 512
  567. struct mt76_sdio {
  568. struct mt76_worker txrx_worker;
  569. struct mt76_worker status_worker;
  570. struct mt76_worker net_worker;
  571. struct mt76_worker stat_worker;
  572. u8 *xmit_buf;
  573. u32 xmit_buf_sz;
  574. struct sdio_func *func;
  575. void *intr_data;
  576. u8 hw_ver;
  577. wait_queue_head_t wait;
  578. int pse_mcu_quota_max;
  579. struct {
  580. int pse_data_quota;
  581. int ple_data_quota;
  582. int pse_mcu_quota;
  583. int pse_page_size;
  584. int deficit;
  585. } sched;
  586. int (*parse_irq)(struct mt76_dev *dev, struct mt76s_intr *intr);
  587. };
  588. struct mt76_mmio {
  589. void __iomem *regs;
  590. spinlock_t irq_lock;
  591. u32 irqmask;
  592. struct mtk_wed_device wed;
  593. struct mtk_wed_device wed_hif2;
  594. struct completion wed_reset;
  595. struct completion wed_reset_complete;
  596. struct airoha_ppe_dev __rcu *ppe_dev;
  597. struct airoha_npu __rcu *npu;
  598. phys_addr_t phy_addr;
  599. int npu_type;
  600. };
  601. struct mt76_rx_status {
  602. union {
  603. struct mt76_wcid *wcid;
  604. u16 wcid_idx;
  605. };
  606. u32 reorder_time;
  607. u32 ampdu_ref;
  608. u32 timestamp;
  609. u8 iv[6];
  610. u8 phy_idx:2;
  611. u8 aggr:1;
  612. u8 qos_ctl;
  613. u16 seqno;
  614. u16 freq;
  615. u32 flag;
  616. u8 enc_flags;
  617. u8 encoding:3, bw:4;
  618. union {
  619. struct {
  620. u8 he_ru:3;
  621. u8 he_gi:2;
  622. u8 he_dcm:1;
  623. };
  624. struct {
  625. u8 ru:4;
  626. u8 gi:2;
  627. } eht;
  628. };
  629. u8 amsdu:1, first_amsdu:1, last_amsdu:1;
  630. u8 rate_idx;
  631. u8 nss:5, band:3;
  632. s8 signal;
  633. u8 chains;
  634. s8 chain_signal[IEEE80211_MAX_CHAINS];
  635. };
  636. struct mt76_freq_range_power {
  637. const struct cfg80211_sar_freq_ranges *range;
  638. s8 power;
  639. };
  640. struct mt76_testmode_ops {
  641. int (*set_state)(struct mt76_phy *phy, enum mt76_testmode_state state);
  642. int (*set_params)(struct mt76_phy *phy, struct nlattr **tb,
  643. enum mt76_testmode_state new_state);
  644. int (*dump_stats)(struct mt76_phy *phy, struct sk_buff *msg);
  645. };
  646. struct mt76_testmode_data {
  647. enum mt76_testmode_state state;
  648. u32 param_set[DIV_ROUND_UP(NUM_MT76_TM_ATTRS, 32)];
  649. struct sk_buff *tx_skb;
  650. u32 tx_count;
  651. u16 tx_mpdu_len;
  652. u8 tx_rate_mode;
  653. u8 tx_rate_idx;
  654. u8 tx_rate_nss;
  655. u8 tx_rate_sgi;
  656. u8 tx_rate_ldpc;
  657. u8 tx_rate_stbc;
  658. u8 tx_ltf;
  659. u8 tx_antenna_mask;
  660. u8 tx_spe_idx;
  661. u8 tx_duty_cycle;
  662. u32 tx_time;
  663. u32 tx_ipg;
  664. u32 freq_offset;
  665. u8 tx_power[4];
  666. u8 tx_power_control;
  667. u8 addr[3][ETH_ALEN];
  668. u32 tx_pending;
  669. u32 tx_queued;
  670. u16 tx_queued_limit;
  671. u32 tx_done;
  672. struct {
  673. u64 packets[__MT_RXQ_MAX];
  674. u64 fcs_error[__MT_RXQ_MAX];
  675. } rx_stats;
  676. };
  677. struct mt76_vif_link {
  678. u8 idx;
  679. u8 link_idx;
  680. u8 omac_idx;
  681. u8 band_idx;
  682. u8 wmm_idx;
  683. u8 scan_seq_num;
  684. u8 cipher;
  685. u8 basic_rates_idx;
  686. u8 mcast_rates_idx;
  687. u8 beacon_rates_idx;
  688. bool offchannel;
  689. struct ieee80211_chanctx_conf *ctx;
  690. struct mt76_wcid *wcid;
  691. struct mt76_vif_data *mvif;
  692. struct rcu_head rcu_head;
  693. };
  694. struct mt76_vif_data {
  695. struct mt76_vif_link __rcu *link[IEEE80211_MLD_MAX_NUM_LINKS];
  696. struct mt76_vif_link __rcu *offchannel_link;
  697. struct mt76_phy *roc_phy;
  698. u16 valid_links;
  699. u8 deflink_id;
  700. };
  701. struct mt76_phy {
  702. struct ieee80211_hw *hw;
  703. struct mt76_dev *dev;
  704. void *priv;
  705. unsigned long state;
  706. unsigned int num_sta;
  707. u8 band_idx;
  708. spinlock_t tx_lock;
  709. struct list_head tx_list;
  710. struct mt76_queue *q_tx[__MT_TXQ_MAX];
  711. struct cfg80211_chan_def chandef;
  712. struct cfg80211_chan_def main_chandef;
  713. bool offchannel;
  714. bool radar_enabled;
  715. struct delayed_work roc_work;
  716. struct ieee80211_vif *roc_vif;
  717. struct mt76_vif_link *roc_link;
  718. struct mt76_chanctx *chanctx;
  719. struct mt76_channel_state *chan_state;
  720. enum mt76_dfs_state dfs_state;
  721. ktime_t survey_time;
  722. u32 aggr_stats[32];
  723. struct mt76_hw_cap cap;
  724. struct mt76_sband sband_2g;
  725. struct mt76_sband sband_5g;
  726. struct mt76_sband sband_6g;
  727. u8 macaddr[ETH_ALEN];
  728. int txpower_cur;
  729. u8 antenna_mask;
  730. u16 chainmask;
  731. #ifdef CONFIG_NL80211_TESTMODE
  732. struct mt76_testmode_data test;
  733. #endif
  734. struct delayed_work mac_work;
  735. u8 mac_work_count;
  736. struct {
  737. struct sk_buff *head;
  738. struct sk_buff **tail;
  739. u16 seqno;
  740. } rx_amsdu[__MT_RXQ_MAX];
  741. struct mt76_freq_range_power *frp;
  742. struct {
  743. struct led_classdev cdev;
  744. char name[32];
  745. bool al;
  746. u8 pin;
  747. } leds;
  748. };
  749. struct mt76_dev {
  750. struct mt76_phy phy; /* must be first */
  751. struct mt76_phy *phys[__MT_MAX_BAND];
  752. struct mt76_phy *band_phys[NUM_NL80211_BANDS];
  753. struct ieee80211_hw *hw;
  754. spinlock_t wed_lock;
  755. spinlock_t lock;
  756. spinlock_t cc_lock;
  757. u32 cur_cc_bss_rx;
  758. struct mt76_rx_status rx_ampdu_status;
  759. u32 rx_ampdu_len;
  760. u32 rx_ampdu_ref;
  761. struct mutex mutex;
  762. const struct mt76_bus_ops *bus;
  763. const struct mt76_driver_ops *drv;
  764. const struct mt76_mcu_ops *mcu_ops;
  765. struct device *dev;
  766. struct device *dma_dev;
  767. struct mt76_mcu mcu;
  768. struct net_device *napi_dev;
  769. struct net_device *tx_napi_dev;
  770. spinlock_t rx_lock;
  771. struct napi_struct napi[__MT_RXQ_MAX];
  772. struct sk_buff_head rx_skb[__MT_RXQ_MAX];
  773. struct tasklet_struct irq_tasklet;
  774. struct list_head txwi_cache;
  775. struct list_head rxwi_cache;
  776. struct mt76_queue *q_mcu[__MT_MCUQ_MAX];
  777. struct mt76_queue q_rx[__MT_RXQ_MAX];
  778. const struct mt76_queue_ops *queue_ops;
  779. int tx_dma_idx[4];
  780. enum mt76_hwrro_mode hwrro_mode;
  781. struct mt76_worker tx_worker;
  782. struct napi_struct tx_napi;
  783. spinlock_t token_lock;
  784. struct idr token;
  785. u16 wed_token_count;
  786. u16 token_count;
  787. u16 token_start;
  788. u16 token_size;
  789. spinlock_t rx_token_lock;
  790. struct idr rx_token;
  791. u16 rx_token_size;
  792. wait_queue_head_t tx_wait;
  793. /* spinclock used to protect wcid pktid linked list */
  794. spinlock_t status_lock;
  795. u32 wcid_mask[DIV_ROUND_UP(MT76_N_WCIDS, 32)];
  796. u64 vif_mask;
  797. struct mt76_wcid global_wcid;
  798. struct mt76_wcid __rcu *wcid[MT76_N_WCIDS];
  799. struct list_head wcid_list;
  800. struct list_head sta_poll_list;
  801. spinlock_t sta_poll_lock;
  802. u32 rev;
  803. struct tasklet_struct pre_tbtt_tasklet;
  804. int beacon_int;
  805. u8 beacon_mask;
  806. struct debugfs_blob_wrapper eeprom;
  807. struct debugfs_blob_wrapper otp;
  808. char alpha2[3];
  809. enum nl80211_dfs_regions region;
  810. struct mt76_scan_rnr_param rnr;
  811. u32 debugfs_reg;
  812. u8 csa_complete;
  813. u32 rxfilter;
  814. struct delayed_work scan_work;
  815. struct {
  816. struct cfg80211_scan_request *req;
  817. struct ieee80211_channel *chan;
  818. struct ieee80211_vif *vif;
  819. struct mt76_vif_link *mlink;
  820. struct mt76_phy *phy;
  821. int chan_idx;
  822. } scan;
  823. #ifdef CONFIG_NL80211_TESTMODE
  824. const struct mt76_testmode_ops *test_ops;
  825. struct {
  826. const char *name;
  827. u32 offset;
  828. } test_mtd;
  829. #endif
  830. struct workqueue_struct *wq;
  831. union {
  832. struct mt76_mmio mmio;
  833. struct mt76_usb usb;
  834. struct mt76_sdio sdio;
  835. };
  836. atomic_t bus_hung;
  837. };
  838. /* per-phy stats. */
  839. struct mt76_mib_stats {
  840. u32 ack_fail_cnt;
  841. u32 fcs_err_cnt;
  842. u32 rts_cnt;
  843. u32 rts_retries_cnt;
  844. u32 ba_miss_cnt;
  845. u32 tx_bf_cnt;
  846. u32 tx_mu_bf_cnt;
  847. u32 tx_mu_mpdu_cnt;
  848. u32 tx_mu_acked_mpdu_cnt;
  849. u32 tx_su_acked_mpdu_cnt;
  850. u32 tx_bf_ibf_ppdu_cnt;
  851. u32 tx_bf_ebf_ppdu_cnt;
  852. u32 tx_bf_rx_fb_all_cnt;
  853. u32 tx_bf_rx_fb_eht_cnt;
  854. u32 tx_bf_rx_fb_he_cnt;
  855. u32 tx_bf_rx_fb_vht_cnt;
  856. u32 tx_bf_rx_fb_ht_cnt;
  857. u32 tx_bf_rx_fb_bw; /* value of last sample, not cumulative */
  858. u32 tx_bf_rx_fb_nc_cnt;
  859. u32 tx_bf_rx_fb_nr_cnt;
  860. u32 tx_bf_fb_cpl_cnt;
  861. u32 tx_bf_fb_trig_cnt;
  862. u32 tx_ampdu_cnt;
  863. u32 tx_stop_q_empty_cnt;
  864. u32 tx_mpdu_attempts_cnt;
  865. u32 tx_mpdu_success_cnt;
  866. u32 tx_pkt_ebf_cnt;
  867. u32 tx_pkt_ibf_cnt;
  868. u32 tx_rwp_fail_cnt;
  869. u32 tx_rwp_need_cnt;
  870. /* rx stats */
  871. u32 rx_fifo_full_cnt;
  872. u32 channel_idle_cnt;
  873. u32 primary_cca_busy_time;
  874. u32 secondary_cca_busy_time;
  875. u32 primary_energy_detect_time;
  876. u32 cck_mdrdy_time;
  877. u32 ofdm_mdrdy_time;
  878. u32 green_mdrdy_time;
  879. u32 rx_vector_mismatch_cnt;
  880. u32 rx_delimiter_fail_cnt;
  881. u32 rx_mrdy_cnt;
  882. u32 rx_len_mismatch_cnt;
  883. u32 rx_mpdu_cnt;
  884. u32 rx_ampdu_cnt;
  885. u32 rx_ampdu_bytes_cnt;
  886. u32 rx_ampdu_valid_subframe_cnt;
  887. u32 rx_ampdu_valid_subframe_bytes_cnt;
  888. u32 rx_pfdrop_cnt;
  889. u32 rx_vec_queue_overflow_drop_cnt;
  890. u32 rx_ba_cnt;
  891. u32 tx_amsdu[8];
  892. u32 tx_amsdu_cnt;
  893. /* mcu_muru_stats */
  894. u32 dl_cck_cnt;
  895. u32 dl_ofdm_cnt;
  896. u32 dl_htmix_cnt;
  897. u32 dl_htgf_cnt;
  898. u32 dl_vht_su_cnt;
  899. u32 dl_vht_2mu_cnt;
  900. u32 dl_vht_3mu_cnt;
  901. u32 dl_vht_4mu_cnt;
  902. u32 dl_he_su_cnt;
  903. u32 dl_he_ext_su_cnt;
  904. u32 dl_he_2ru_cnt;
  905. u32 dl_he_2mu_cnt;
  906. u32 dl_he_3ru_cnt;
  907. u32 dl_he_3mu_cnt;
  908. u32 dl_he_4ru_cnt;
  909. u32 dl_he_4mu_cnt;
  910. u32 dl_he_5to8ru_cnt;
  911. u32 dl_he_9to16ru_cnt;
  912. u32 dl_he_gtr16ru_cnt;
  913. u32 ul_hetrig_su_cnt;
  914. u32 ul_hetrig_2ru_cnt;
  915. u32 ul_hetrig_3ru_cnt;
  916. u32 ul_hetrig_4ru_cnt;
  917. u32 ul_hetrig_5to8ru_cnt;
  918. u32 ul_hetrig_9to16ru_cnt;
  919. u32 ul_hetrig_gtr16ru_cnt;
  920. u32 ul_hetrig_2mu_cnt;
  921. u32 ul_hetrig_3mu_cnt;
  922. u32 ul_hetrig_4mu_cnt;
  923. };
  924. struct mt76_power_limits {
  925. s8 cck[4];
  926. s8 ofdm[8];
  927. s8 mcs[4][10];
  928. s8 ru[7][12];
  929. s8 eht[16][16];
  930. struct {
  931. s8 cck[4];
  932. s8 ofdm[4];
  933. s8 ofdm_bf[4];
  934. s8 ru[7][10];
  935. s8 ru_bf[7][10];
  936. } path;
  937. };
  938. struct mt76_ethtool_worker_info {
  939. u64 *data;
  940. int idx;
  941. int initial_stat_idx;
  942. int worker_stat_count;
  943. int sta_count;
  944. };
  945. struct mt76_chanctx {
  946. struct mt76_phy *phy;
  947. };
  948. #define CCK_RATE(_idx, _rate) { \
  949. .bitrate = _rate, \
  950. .flags = IEEE80211_RATE_SHORT_PREAMBLE, \
  951. .hw_value = (MT_PHY_TYPE_CCK << 8) | (_idx), \
  952. .hw_value_short = (MT_PHY_TYPE_CCK << 8) | (4 + _idx), \
  953. }
  954. #define OFDM_RATE(_idx, _rate) { \
  955. .bitrate = _rate, \
  956. .hw_value = (MT_PHY_TYPE_OFDM << 8) | (_idx), \
  957. .hw_value_short = (MT_PHY_TYPE_OFDM << 8) | (_idx), \
  958. }
  959. extern struct ieee80211_rate mt76_rates[12];
  960. #define __mt76_rr(dev, ...) (dev)->bus->rr((dev), __VA_ARGS__)
  961. #define __mt76_wr(dev, ...) (dev)->bus->wr((dev), __VA_ARGS__)
  962. #define __mt76_rmw(dev, ...) (dev)->bus->rmw((dev), __VA_ARGS__)
  963. #define __mt76_wr_copy(dev, ...) (dev)->bus->write_copy((dev), __VA_ARGS__)
  964. #define __mt76_rr_copy(dev, ...) (dev)->bus->read_copy((dev), __VA_ARGS__)
  965. #define __mt76_set(dev, offset, val) __mt76_rmw(dev, offset, 0, val)
  966. #define __mt76_clear(dev, offset, val) __mt76_rmw(dev, offset, val, 0)
  967. #define mt76_rr(dev, ...) (dev)->mt76.bus->rr(&((dev)->mt76), __VA_ARGS__)
  968. #define mt76_wr(dev, ...) (dev)->mt76.bus->wr(&((dev)->mt76), __VA_ARGS__)
  969. #define mt76_rmw(dev, ...) (dev)->mt76.bus->rmw(&((dev)->mt76), __VA_ARGS__)
  970. #define mt76_wr_copy(dev, ...) (dev)->mt76.bus->write_copy(&((dev)->mt76), __VA_ARGS__)
  971. #define mt76_rr_copy(dev, ...) (dev)->mt76.bus->read_copy(&((dev)->mt76), __VA_ARGS__)
  972. #define mt76_wr_rp(dev, ...) (dev)->mt76.bus->wr_rp(&((dev)->mt76), __VA_ARGS__)
  973. #define mt76_rd_rp(dev, ...) (dev)->mt76.bus->rd_rp(&((dev)->mt76), __VA_ARGS__)
  974. #define mt76_mcu_restart(dev, ...) (dev)->mt76.mcu_ops->mcu_restart(&((dev)->mt76))
  975. #define mt76_set(dev, offset, val) mt76_rmw(dev, offset, 0, val)
  976. #define mt76_clear(dev, offset, val) mt76_rmw(dev, offset, val, 0)
  977. #define mt76_get_field(_dev, _reg, _field) \
  978. FIELD_GET(_field, mt76_rr(dev, _reg))
  979. #define mt76_rmw_field(_dev, _reg, _field, _val) \
  980. mt76_rmw(_dev, _reg, _field, FIELD_PREP(_field, _val))
  981. #define __mt76_rmw_field(_dev, _reg, _field, _val) \
  982. __mt76_rmw(_dev, _reg, _field, FIELD_PREP(_field, _val))
  983. #define mt76_hw(dev) (dev)->mphy.hw
  984. bool __mt76_poll(struct mt76_dev *dev, u32 offset, u32 mask, u32 val,
  985. int timeout);
  986. #define mt76_poll(dev, ...) __mt76_poll(&((dev)->mt76), __VA_ARGS__)
  987. bool ____mt76_poll_msec(struct mt76_dev *dev, u32 offset, u32 mask, u32 val,
  988. int timeout, int kick);
  989. #define __mt76_poll_msec(...) ____mt76_poll_msec(__VA_ARGS__, 10)
  990. #define mt76_poll_msec(dev, ...) ____mt76_poll_msec(&((dev)->mt76), __VA_ARGS__, 10)
  991. #define mt76_poll_msec_tick(dev, ...) ____mt76_poll_msec(&((dev)->mt76), __VA_ARGS__)
  992. void mt76_mmio_init(struct mt76_dev *dev, void __iomem *regs);
  993. void mt76_pci_disable_aspm(struct pci_dev *pdev);
  994. bool mt76_pci_aspm_supported(struct pci_dev *pdev);
  995. static inline u16 mt76_chip(struct mt76_dev *dev)
  996. {
  997. return dev->rev >> 16;
  998. }
  999. static inline u16 mt76_rev(struct mt76_dev *dev)
  1000. {
  1001. return dev->rev & 0xffff;
  1002. }
  1003. void mt76_wed_release_rx_buf(struct mtk_wed_device *wed);
  1004. void mt76_wed_offload_disable(struct mtk_wed_device *wed);
  1005. void mt76_wed_reset_complete(struct mtk_wed_device *wed);
  1006. void mt76_wed_dma_reset(struct mt76_dev *dev);
  1007. int mt76_wed_net_setup_tc(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  1008. struct net_device *netdev, enum tc_setup_type type,
  1009. void *type_data);
  1010. #ifdef CONFIG_NET_MEDIATEK_SOC_WED
  1011. u32 mt76_wed_init_rx_buf(struct mtk_wed_device *wed, int size);
  1012. int mt76_wed_offload_enable(struct mtk_wed_device *wed);
  1013. int mt76_wed_dma_setup(struct mt76_dev *dev, struct mt76_queue *q, bool reset);
  1014. #else
  1015. static inline u32 mt76_wed_init_rx_buf(struct mtk_wed_device *wed, int size)
  1016. {
  1017. return 0;
  1018. }
  1019. static inline int mt76_wed_offload_enable(struct mtk_wed_device *wed)
  1020. {
  1021. return 0;
  1022. }
  1023. static inline int mt76_wed_dma_setup(struct mt76_dev *dev, struct mt76_queue *q,
  1024. bool reset)
  1025. {
  1026. return 0;
  1027. }
  1028. #endif /* CONFIG_NET_MEDIATEK_SOC_WED */
  1029. #define mt76xx_chip(dev) mt76_chip(&((dev)->mt76))
  1030. #define mt76xx_rev(dev) mt76_rev(&((dev)->mt76))
  1031. #define mt76_init_queues(dev, ...) (dev)->mt76.queue_ops->init(&((dev)->mt76), __VA_ARGS__)
  1032. #define mt76_queue_alloc(dev, ...) (dev)->mt76.queue_ops->alloc(&((dev)->mt76), __VA_ARGS__)
  1033. #define mt76_tx_queue_skb_raw(dev, ...) (dev)->mt76.queue_ops->tx_queue_skb_raw(&((dev)->mt76), __VA_ARGS__)
  1034. #define mt76_tx_queue_skb(dev, ...) (dev)->mt76.queue_ops->tx_queue_skb(&((dev)->mphy), __VA_ARGS__)
  1035. #define mt76_queue_rx_reset(dev, ...) (dev)->mt76.queue_ops->rx_reset(&((dev)->mt76), __VA_ARGS__)
  1036. #define mt76_queue_tx_cleanup(dev, ...) (dev)->mt76.queue_ops->tx_cleanup(&((dev)->mt76), __VA_ARGS__)
  1037. #define mt76_queue_rx_init(dev, ...) (dev)->mt76.queue_ops->rx_queue_init(&((dev)->mt76), __VA_ARGS__)
  1038. #define mt76_queue_rx_cleanup(dev, ...) (dev)->mt76.queue_ops->rx_cleanup(&((dev)->mt76), __VA_ARGS__)
  1039. #define mt76_queue_kick(dev, ...) (dev)->mt76.queue_ops->kick(&((dev)->mt76), __VA_ARGS__)
  1040. #define mt76_queue_reset(dev, ...) (dev)->mt76.queue_ops->reset_q(&((dev)->mt76), __VA_ARGS__)
  1041. #define mt76_for_each_q_rx(dev, i) \
  1042. for (i = 0; i < ARRAY_SIZE((dev)->q_rx); i++) \
  1043. if ((dev)->q_rx[i].ndesc)
  1044. #define mt76_dereference(p, dev) \
  1045. rcu_dereference_protected(p, lockdep_is_held(&(dev)->mutex))
  1046. static inline struct mt76_dev *mt76_wed_to_dev(struct mtk_wed_device *wed)
  1047. {
  1048. #ifdef CONFIG_NET_MEDIATEK_SOC_WED
  1049. if (wed->wlan.hif2)
  1050. return container_of(wed, struct mt76_dev, mmio.wed_hif2);
  1051. #endif /* CONFIG_NET_MEDIATEK_SOC_WED */
  1052. return container_of(wed, struct mt76_dev, mmio.wed);
  1053. }
  1054. static inline struct mt76_wcid *
  1055. __mt76_wcid_ptr(struct mt76_dev *dev, u16 idx)
  1056. {
  1057. if (idx >= ARRAY_SIZE(dev->wcid))
  1058. return NULL;
  1059. return rcu_dereference(dev->wcid[idx]);
  1060. }
  1061. #define mt76_wcid_ptr(dev, idx) __mt76_wcid_ptr(&(dev)->mt76, idx)
  1062. struct mt76_dev *mt76_alloc_device(struct device *pdev, unsigned int size,
  1063. const struct ieee80211_ops *ops,
  1064. const struct mt76_driver_ops *drv_ops);
  1065. int mt76_register_device(struct mt76_dev *dev, bool vht,
  1066. struct ieee80211_rate *rates, int n_rates);
  1067. void mt76_unregister_device(struct mt76_dev *dev);
  1068. void mt76_free_device(struct mt76_dev *dev);
  1069. void mt76_reset_device(struct mt76_dev *dev);
  1070. void mt76_unregister_phy(struct mt76_phy *phy);
  1071. struct mt76_phy *mt76_alloc_radio_phy(struct mt76_dev *dev, unsigned int size,
  1072. u8 band_idx);
  1073. struct mt76_phy *mt76_alloc_phy(struct mt76_dev *dev, unsigned int size,
  1074. const struct ieee80211_ops *ops,
  1075. u8 band_idx);
  1076. int mt76_register_phy(struct mt76_phy *phy, bool vht,
  1077. struct ieee80211_rate *rates, int n_rates);
  1078. struct mt76_phy *mt76_vif_phy(struct ieee80211_hw *hw,
  1079. struct ieee80211_vif *vif);
  1080. struct dentry *mt76_register_debugfs_fops(struct mt76_phy *phy,
  1081. const struct file_operations *ops);
  1082. static inline struct dentry *mt76_register_debugfs(struct mt76_dev *dev)
  1083. {
  1084. return mt76_register_debugfs_fops(&dev->phy, NULL);
  1085. }
  1086. int mt76_queues_read(struct seq_file *s, void *data);
  1087. void mt76_seq_puts_array(struct seq_file *file, const char *str,
  1088. s8 *val, int len);
  1089. int mt76_eeprom_init(struct mt76_dev *dev, int len);
  1090. int mt76_eeprom_override(struct mt76_phy *phy);
  1091. int mt76_get_of_data_from_mtd(struct mt76_dev *dev, void *eep, int offset, int len);
  1092. int mt76_get_of_data_from_nvmem(struct mt76_dev *dev, void *eep,
  1093. const char *cell_name, int len);
  1094. struct mt76_queue *
  1095. mt76_init_queue(struct mt76_dev *dev, int qid, int idx, int n_desc,
  1096. int ring_base, void *wed, u32 flags);
  1097. static inline int mt76_init_tx_queue(struct mt76_phy *phy, int qid, int idx,
  1098. int n_desc, int ring_base, void *wed,
  1099. u32 flags)
  1100. {
  1101. struct mt76_queue *q;
  1102. q = mt76_init_queue(phy->dev, qid, idx, n_desc, ring_base, wed, flags);
  1103. if (IS_ERR(q))
  1104. return PTR_ERR(q);
  1105. phy->q_tx[qid] = q;
  1106. return 0;
  1107. }
  1108. static inline int mt76_init_mcu_queue(struct mt76_dev *dev, int qid, int idx,
  1109. int n_desc, int ring_base)
  1110. {
  1111. struct mt76_queue *q;
  1112. q = mt76_init_queue(dev, qid, idx, n_desc, ring_base, NULL, 0);
  1113. if (IS_ERR(q))
  1114. return PTR_ERR(q);
  1115. dev->q_mcu[qid] = q;
  1116. return 0;
  1117. }
  1118. static inline struct mt76_phy *
  1119. mt76_dev_phy(struct mt76_dev *dev, u8 phy_idx)
  1120. {
  1121. if ((phy_idx == MT_BAND1 && dev->phys[phy_idx]) ||
  1122. (phy_idx == MT_BAND2 && dev->phys[phy_idx]))
  1123. return dev->phys[phy_idx];
  1124. return &dev->phy;
  1125. }
  1126. static inline struct ieee80211_hw *
  1127. mt76_phy_hw(struct mt76_dev *dev, u8 phy_idx)
  1128. {
  1129. return mt76_dev_phy(dev, phy_idx)->hw;
  1130. }
  1131. static inline u8 *
  1132. mt76_get_txwi_ptr(struct mt76_dev *dev, struct mt76_txwi_cache *t)
  1133. {
  1134. return (u8 *)t - dev->drv->txwi_size;
  1135. }
  1136. /* increment with wrap-around */
  1137. static inline int mt76_incr(int val, int size)
  1138. {
  1139. return (val + 1) & (size - 1);
  1140. }
  1141. /* decrement with wrap-around */
  1142. static inline int mt76_decr(int val, int size)
  1143. {
  1144. return (val - 1) & (size - 1);
  1145. }
  1146. u8 mt76_ac_to_hwq(u8 ac);
  1147. static inline struct ieee80211_txq *
  1148. mtxq_to_txq(struct mt76_txq *mtxq)
  1149. {
  1150. void *ptr = mtxq;
  1151. return container_of(ptr, struct ieee80211_txq, drv_priv);
  1152. }
  1153. static inline struct ieee80211_sta *
  1154. wcid_to_sta(struct mt76_wcid *wcid)
  1155. {
  1156. void *ptr = wcid;
  1157. if (!wcid || !wcid->sta)
  1158. return NULL;
  1159. if (wcid->def_wcid)
  1160. ptr = wcid->def_wcid;
  1161. return container_of(ptr, struct ieee80211_sta, drv_priv);
  1162. }
  1163. static inline struct mt76_tx_cb *mt76_tx_skb_cb(struct sk_buff *skb)
  1164. {
  1165. BUILD_BUG_ON(sizeof(struct mt76_tx_cb) >
  1166. sizeof(IEEE80211_SKB_CB(skb)->status.status_driver_data));
  1167. return ((void *)IEEE80211_SKB_CB(skb)->status.status_driver_data);
  1168. }
  1169. static inline void *mt76_skb_get_hdr(struct sk_buff *skb)
  1170. {
  1171. struct mt76_rx_status mstat;
  1172. u8 *data = skb->data;
  1173. /* Alignment concerns */
  1174. BUILD_BUG_ON(sizeof(struct ieee80211_radiotap_he) % 4);
  1175. BUILD_BUG_ON(sizeof(struct ieee80211_radiotap_he_mu) % 4);
  1176. mstat = *((struct mt76_rx_status *)skb->cb);
  1177. if (mstat.flag & RX_FLAG_RADIOTAP_HE)
  1178. data += sizeof(struct ieee80211_radiotap_he);
  1179. if (mstat.flag & RX_FLAG_RADIOTAP_HE_MU)
  1180. data += sizeof(struct ieee80211_radiotap_he_mu);
  1181. return data;
  1182. }
  1183. static inline void mt76_insert_hdr_pad(struct sk_buff *skb)
  1184. {
  1185. int len = ieee80211_get_hdrlen_from_skb(skb);
  1186. if (len % 4 == 0)
  1187. return;
  1188. skb_push(skb, 2);
  1189. memmove(skb->data, skb->data + 2, len);
  1190. skb->data[len] = 0;
  1191. skb->data[len + 1] = 0;
  1192. }
  1193. static inline bool mt76_is_skb_pktid(u8 pktid)
  1194. {
  1195. if (pktid & MT_PACKET_ID_HAS_RATE)
  1196. return false;
  1197. return pktid >= MT_PACKET_ID_FIRST;
  1198. }
  1199. static inline u8 mt76_tx_power_path_delta(u8 path)
  1200. {
  1201. static const u8 path_delta[5] = { 0, 6, 9, 12, 14 };
  1202. u8 idx = path - 1;
  1203. return (idx < ARRAY_SIZE(path_delta)) ? path_delta[idx] : 0;
  1204. }
  1205. static inline bool mt76_testmode_enabled(struct mt76_phy *phy)
  1206. {
  1207. #ifdef CONFIG_NL80211_TESTMODE
  1208. return phy->test.state != MT76_TM_STATE_OFF;
  1209. #else
  1210. return false;
  1211. #endif
  1212. }
  1213. static inline bool mt76_is_testmode_skb(struct mt76_dev *dev,
  1214. struct sk_buff *skb,
  1215. struct ieee80211_hw **hw)
  1216. {
  1217. #ifdef CONFIG_NL80211_TESTMODE
  1218. int i;
  1219. for (i = 0; i < ARRAY_SIZE(dev->phys); i++) {
  1220. struct mt76_phy *phy = dev->phys[i];
  1221. if (phy && skb == phy->test.tx_skb) {
  1222. *hw = dev->phys[i]->hw;
  1223. return true;
  1224. }
  1225. }
  1226. return false;
  1227. #else
  1228. return false;
  1229. #endif
  1230. }
  1231. void mt76_rx(struct mt76_dev *dev, enum mt76_rxq_id q, struct sk_buff *skb);
  1232. void mt76_tx(struct mt76_phy *dev, struct ieee80211_sta *sta,
  1233. struct mt76_wcid *wcid, struct sk_buff *skb);
  1234. void mt76_wake_tx_queue(struct ieee80211_hw *hw, struct ieee80211_txq *txq);
  1235. void mt76_stop_tx_queues(struct mt76_phy *phy, struct ieee80211_sta *sta,
  1236. bool send_bar);
  1237. void mt76_tx_check_agg_ssn(struct ieee80211_sta *sta, struct sk_buff *skb);
  1238. void mt76_txq_schedule(struct mt76_phy *phy, enum mt76_txq_id qid);
  1239. void mt76_txq_schedule_all(struct mt76_phy *phy);
  1240. void mt76_tx_worker_run(struct mt76_dev *dev);
  1241. void mt76_tx_worker(struct mt76_worker *w);
  1242. void mt76_release_buffered_frames(struct ieee80211_hw *hw,
  1243. struct ieee80211_sta *sta,
  1244. u16 tids, int nframes,
  1245. enum ieee80211_frame_release_type reason,
  1246. bool more_data);
  1247. bool mt76_has_tx_pending(struct mt76_phy *phy);
  1248. int mt76_update_channel(struct mt76_phy *phy);
  1249. void mt76_update_survey(struct mt76_phy *phy);
  1250. void mt76_update_survey_active_time(struct mt76_phy *phy, ktime_t time);
  1251. int mt76_get_survey(struct ieee80211_hw *hw, int idx,
  1252. struct survey_info *survey);
  1253. int mt76_rx_signal(u8 chain_mask, s8 *chain_signal);
  1254. void mt76_set_stream_caps(struct mt76_phy *phy, bool vht);
  1255. int mt76_rx_aggr_start(struct mt76_dev *dev, struct mt76_wcid *wcid, u8 tid,
  1256. u16 ssn, u16 size);
  1257. void mt76_rx_aggr_stop(struct mt76_dev *dev, struct mt76_wcid *wcid, u8 tid);
  1258. void mt76_wcid_key_setup(struct mt76_dev *dev, struct mt76_wcid *wcid,
  1259. struct ieee80211_key_conf *key);
  1260. void mt76_tx_status_lock(struct mt76_dev *dev, struct sk_buff_head *list)
  1261. __acquires(&dev->status_lock);
  1262. void mt76_tx_status_unlock(struct mt76_dev *dev, struct sk_buff_head *list)
  1263. __releases(&dev->status_lock);
  1264. int mt76_tx_status_skb_add(struct mt76_dev *dev, struct mt76_wcid *wcid,
  1265. struct sk_buff *skb);
  1266. struct sk_buff *mt76_tx_status_skb_get(struct mt76_dev *dev,
  1267. struct mt76_wcid *wcid, int pktid,
  1268. struct sk_buff_head *list);
  1269. void mt76_tx_status_skb_done(struct mt76_dev *dev, struct sk_buff *skb,
  1270. struct sk_buff_head *list);
  1271. void __mt76_tx_complete_skb(struct mt76_dev *dev, u16 wcid, struct sk_buff *skb,
  1272. struct list_head *free_list);
  1273. static inline void
  1274. mt76_tx_complete_skb(struct mt76_dev *dev, u16 wcid, struct sk_buff *skb)
  1275. {
  1276. __mt76_tx_complete_skb(dev, wcid, skb, NULL);
  1277. }
  1278. void mt76_tx_status_check(struct mt76_dev *dev, bool flush);
  1279. int mt76_sta_state(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  1280. struct ieee80211_sta *sta,
  1281. enum ieee80211_sta_state old_state,
  1282. enum ieee80211_sta_state new_state);
  1283. void __mt76_sta_remove(struct mt76_phy *phy, struct ieee80211_vif *vif,
  1284. struct ieee80211_sta *sta);
  1285. void mt76_sta_pre_rcu_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  1286. struct ieee80211_sta *sta);
  1287. int mt76_get_min_avg_rssi(struct mt76_dev *dev, u8 phy_idx);
  1288. s8 mt76_get_power_bound(struct mt76_phy *phy, s8 txpower);
  1289. int mt76_get_txpower(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  1290. unsigned int link_id, int *dbm);
  1291. int mt76_init_sar_power(struct ieee80211_hw *hw,
  1292. const struct cfg80211_sar_specs *sar);
  1293. int mt76_get_sar_power(struct mt76_phy *phy,
  1294. struct ieee80211_channel *chan,
  1295. int power);
  1296. void mt76_csa_check(struct mt76_dev *dev);
  1297. void mt76_csa_finish(struct mt76_dev *dev);
  1298. int mt76_get_antenna(struct ieee80211_hw *hw, int radio_idx, u32 *tx_ant,
  1299. u32 *rx_ant);
  1300. int mt76_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta, bool set);
  1301. void mt76_insert_ccmp_hdr(struct sk_buff *skb, u8 key_id);
  1302. int mt76_get_rate(struct mt76_dev *dev,
  1303. struct ieee80211_supported_band *sband,
  1304. int idx, bool cck);
  1305. int mt76_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  1306. struct ieee80211_scan_request *hw_req);
  1307. void mt76_cancel_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif);
  1308. void mt76_sw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  1309. const u8 *mac);
  1310. void mt76_sw_scan_complete(struct ieee80211_hw *hw,
  1311. struct ieee80211_vif *vif);
  1312. enum mt76_dfs_state mt76_phy_dfs_state(struct mt76_phy *phy);
  1313. int mt76_add_chanctx(struct ieee80211_hw *hw,
  1314. struct ieee80211_chanctx_conf *conf);
  1315. void mt76_remove_chanctx(struct ieee80211_hw *hw,
  1316. struct ieee80211_chanctx_conf *conf);
  1317. void mt76_change_chanctx(struct ieee80211_hw *hw,
  1318. struct ieee80211_chanctx_conf *conf,
  1319. u32 changed);
  1320. int mt76_assign_vif_chanctx(struct ieee80211_hw *hw,
  1321. struct ieee80211_vif *vif,
  1322. struct ieee80211_bss_conf *link_conf,
  1323. struct ieee80211_chanctx_conf *conf);
  1324. void mt76_unassign_vif_chanctx(struct ieee80211_hw *hw,
  1325. struct ieee80211_vif *vif,
  1326. struct ieee80211_bss_conf *link_conf,
  1327. struct ieee80211_chanctx_conf *conf);
  1328. int mt76_switch_vif_chanctx(struct ieee80211_hw *hw,
  1329. struct ieee80211_vif_chanctx_switch *vifs,
  1330. int n_vifs,
  1331. enum ieee80211_chanctx_switch_mode mode);
  1332. int mt76_remain_on_channel(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  1333. struct ieee80211_channel *chan, int duration,
  1334. enum ieee80211_roc_type type);
  1335. int mt76_cancel_remain_on_channel(struct ieee80211_hw *hw,
  1336. struct ieee80211_vif *vif);
  1337. int mt76_testmode_cmd(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  1338. void *data, int len);
  1339. int mt76_testmode_dump(struct ieee80211_hw *hw, struct sk_buff *skb,
  1340. struct netlink_callback *cb, void *data, int len);
  1341. int mt76_testmode_set_state(struct mt76_phy *phy, enum mt76_testmode_state state);
  1342. int mt76_testmode_alloc_skb(struct mt76_phy *phy, u32 len);
  1343. #ifdef CONFIG_MT76_NPU
  1344. void mt76_npu_check_ppe(struct mt76_dev *dev, struct sk_buff *skb,
  1345. u32 info);
  1346. int mt76_npu_dma_add_buf(struct mt76_phy *phy, struct mt76_queue *q,
  1347. struct sk_buff *skb, struct mt76_queue_buf *buf,
  1348. void *txwi_ptr);
  1349. int mt76_npu_rx_queue_init(struct mt76_dev *dev, struct mt76_queue *q);
  1350. int mt76_npu_fill_rx_queue(struct mt76_dev *dev, struct mt76_queue *q);
  1351. void mt76_npu_queue_cleanup(struct mt76_dev *dev, struct mt76_queue *q);
  1352. void mt76_npu_disable_irqs(struct mt76_dev *dev);
  1353. int mt76_npu_init(struct mt76_dev *dev, phys_addr_t phy_addr, int type);
  1354. void mt76_npu_deinit(struct mt76_dev *dev);
  1355. void mt76_npu_queue_setup(struct mt76_dev *dev, struct mt76_queue *q);
  1356. void mt76_npu_txdesc_cleanup(struct mt76_queue *q, int index);
  1357. int mt76_npu_net_setup_tc(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  1358. struct net_device *dev, enum tc_setup_type type,
  1359. void *type_data);
  1360. #else
  1361. static inline void mt76_npu_check_ppe(struct mt76_dev *dev,
  1362. struct sk_buff *skb, u32 info)
  1363. {
  1364. }
  1365. static inline int mt76_npu_dma_add_buf(struct mt76_phy *phy,
  1366. struct mt76_queue *q,
  1367. struct sk_buff *skb,
  1368. struct mt76_queue_buf *buf,
  1369. void *txwi_ptr)
  1370. {
  1371. return -EOPNOTSUPP;
  1372. }
  1373. static inline int mt76_npu_fill_rx_queue(struct mt76_dev *dev,
  1374. struct mt76_queue *q)
  1375. {
  1376. return 0;
  1377. }
  1378. static inline void mt76_npu_queue_cleanup(struct mt76_dev *dev,
  1379. struct mt76_queue *q)
  1380. {
  1381. }
  1382. static inline void mt76_npu_disable_irqs(struct mt76_dev *dev)
  1383. {
  1384. }
  1385. static inline int mt76_npu_init(struct mt76_dev *dev, phys_addr_t phy_addr,
  1386. int type)
  1387. {
  1388. return 0;
  1389. }
  1390. static inline void mt76_npu_deinit(struct mt76_dev *dev)
  1391. {
  1392. }
  1393. static inline void mt76_npu_queue_setup(struct mt76_dev *dev,
  1394. struct mt76_queue *q)
  1395. {
  1396. }
  1397. static inline void mt76_npu_txdesc_cleanup(struct mt76_queue *q,
  1398. int index)
  1399. {
  1400. }
  1401. static inline int mt76_npu_net_setup_tc(struct ieee80211_hw *hw,
  1402. struct ieee80211_vif *vif,
  1403. struct net_device *dev,
  1404. enum tc_setup_type type,
  1405. void *type_data)
  1406. {
  1407. return -EOPNOTSUPP;
  1408. }
  1409. #endif /* CONFIG_MT76_NPU */
  1410. static inline bool mt76_npu_device_active(struct mt76_dev *dev)
  1411. {
  1412. return !!rcu_access_pointer(dev->mmio.npu);
  1413. }
  1414. static inline bool mt76_ppe_device_active(struct mt76_dev *dev)
  1415. {
  1416. return !!rcu_access_pointer(dev->mmio.ppe_dev);
  1417. }
  1418. static inline int mt76_npu_send_msg(struct airoha_npu *npu, int ifindex,
  1419. enum airoha_npu_wlan_set_cmd cmd,
  1420. u32 val, gfp_t gfp)
  1421. {
  1422. return airoha_npu_wlan_send_msg(npu, ifindex, cmd, &val, sizeof(val),
  1423. gfp);
  1424. }
  1425. static inline int mt76_npu_get_msg(struct airoha_npu *npu, int ifindex,
  1426. enum airoha_npu_wlan_get_cmd cmd,
  1427. u32 *val, gfp_t gfp)
  1428. {
  1429. return airoha_npu_wlan_get_msg(npu, ifindex, cmd, val, sizeof(*val),
  1430. gfp);
  1431. }
  1432. static inline void mt76_testmode_reset(struct mt76_phy *phy, bool disable)
  1433. {
  1434. #ifdef CONFIG_NL80211_TESTMODE
  1435. enum mt76_testmode_state state = MT76_TM_STATE_IDLE;
  1436. if (disable || phy->test.state == MT76_TM_STATE_OFF)
  1437. state = MT76_TM_STATE_OFF;
  1438. mt76_testmode_set_state(phy, state);
  1439. #endif
  1440. }
  1441. /* internal */
  1442. static inline struct ieee80211_hw *
  1443. mt76_tx_status_get_hw(struct mt76_dev *dev, struct sk_buff *skb)
  1444. {
  1445. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1446. u8 phy_idx = (info->hw_queue & MT_TX_HW_QUEUE_PHY) >> 2;
  1447. struct ieee80211_hw *hw = mt76_phy_hw(dev, phy_idx);
  1448. info->hw_queue &= ~MT_TX_HW_QUEUE_PHY;
  1449. return hw;
  1450. }
  1451. void mt76_put_txwi(struct mt76_dev *dev, struct mt76_txwi_cache *t);
  1452. void mt76_put_rxwi(struct mt76_dev *dev, struct mt76_txwi_cache *t);
  1453. struct mt76_txwi_cache *mt76_get_rxwi(struct mt76_dev *dev);
  1454. void mt76_free_pending_rxwi(struct mt76_dev *dev);
  1455. void mt76_rx_complete(struct mt76_dev *dev, struct sk_buff_head *frames,
  1456. struct napi_struct *napi);
  1457. void mt76_rx_poll_complete(struct mt76_dev *dev, enum mt76_rxq_id q,
  1458. struct napi_struct *napi);
  1459. void mt76_rx_aggr_reorder(struct sk_buff *skb, struct sk_buff_head *frames);
  1460. void mt76_testmode_tx_pending(struct mt76_phy *phy);
  1461. void mt76_queue_tx_complete(struct mt76_dev *dev, struct mt76_queue *q,
  1462. struct mt76_queue_entry *e);
  1463. int __mt76_set_channel(struct mt76_phy *phy, struct cfg80211_chan_def *chandef,
  1464. bool offchannel);
  1465. int mt76_set_channel(struct mt76_phy *phy, struct cfg80211_chan_def *chandef,
  1466. bool offchannel);
  1467. void mt76_scan_work(struct work_struct *work);
  1468. void mt76_abort_scan(struct mt76_dev *dev);
  1469. void mt76_roc_complete_work(struct work_struct *work);
  1470. void mt76_roc_complete(struct mt76_phy *phy);
  1471. void mt76_abort_roc(struct mt76_phy *phy);
  1472. struct mt76_vif_link *mt76_get_vif_phy_link(struct mt76_phy *phy,
  1473. struct ieee80211_vif *vif);
  1474. void mt76_put_vif_phy_link(struct mt76_phy *phy, struct ieee80211_vif *vif,
  1475. struct mt76_vif_link *mlink);
  1476. /* usb */
  1477. static inline bool mt76u_urb_error(struct urb *urb)
  1478. {
  1479. return urb->status &&
  1480. urb->status != -ECONNRESET &&
  1481. urb->status != -ESHUTDOWN &&
  1482. urb->status != -ENOENT;
  1483. }
  1484. static inline int
  1485. mt76u_bulk_msg(struct mt76_dev *dev, void *data, int len, int *actual_len,
  1486. int timeout, int ep)
  1487. {
  1488. struct usb_interface *uintf = to_usb_interface(dev->dev);
  1489. struct usb_device *udev = interface_to_usbdev(uintf);
  1490. struct mt76_usb *usb = &dev->usb;
  1491. unsigned int pipe;
  1492. if (actual_len)
  1493. pipe = usb_rcvbulkpipe(udev, usb->in_ep[ep]);
  1494. else
  1495. pipe = usb_sndbulkpipe(udev, usb->out_ep[ep]);
  1496. return usb_bulk_msg(udev, pipe, data, len, actual_len, timeout);
  1497. }
  1498. void mt76_ethtool_page_pool_stats(struct mt76_dev *dev, u64 *data, int *index);
  1499. void mt76_ethtool_worker(struct mt76_ethtool_worker_info *wi,
  1500. struct mt76_sta_stats *stats, bool eht);
  1501. int mt76_skb_adjust_pad(struct sk_buff *skb, int pad);
  1502. int __mt76u_vendor_request(struct mt76_dev *dev, u8 req, u8 req_type,
  1503. u16 val, u16 offset, void *buf, size_t len);
  1504. int mt76u_vendor_request(struct mt76_dev *dev, u8 req,
  1505. u8 req_type, u16 val, u16 offset,
  1506. void *buf, size_t len);
  1507. void mt76u_single_wr(struct mt76_dev *dev, const u8 req,
  1508. const u16 offset, const u32 val);
  1509. void mt76u_read_copy(struct mt76_dev *dev, u32 offset,
  1510. void *data, int len);
  1511. u32 ___mt76u_rr(struct mt76_dev *dev, u8 req, u8 req_type, u32 addr);
  1512. void ___mt76u_wr(struct mt76_dev *dev, u8 req, u8 req_type,
  1513. u32 addr, u32 val);
  1514. int __mt76u_init(struct mt76_dev *dev, struct usb_interface *intf,
  1515. struct mt76_bus_ops *ops);
  1516. int mt76u_init(struct mt76_dev *dev, struct usb_interface *intf);
  1517. int mt76u_alloc_mcu_queue(struct mt76_dev *dev);
  1518. int mt76u_alloc_queues(struct mt76_dev *dev);
  1519. void mt76u_stop_tx(struct mt76_dev *dev);
  1520. void mt76u_stop_rx(struct mt76_dev *dev);
  1521. int mt76u_resume_rx(struct mt76_dev *dev);
  1522. void mt76u_queues_deinit(struct mt76_dev *dev);
  1523. int mt76s_init(struct mt76_dev *dev, struct sdio_func *func,
  1524. const struct mt76_bus_ops *bus_ops);
  1525. int mt76s_alloc_rx_queue(struct mt76_dev *dev, enum mt76_rxq_id qid);
  1526. int mt76s_alloc_tx(struct mt76_dev *dev);
  1527. void mt76s_deinit(struct mt76_dev *dev);
  1528. void mt76s_sdio_irq(struct sdio_func *func);
  1529. void mt76s_txrx_worker(struct mt76_sdio *sdio);
  1530. bool mt76s_txqs_empty(struct mt76_dev *dev);
  1531. int mt76s_hw_init(struct mt76_dev *dev, struct sdio_func *func,
  1532. int hw_ver);
  1533. u32 mt76s_rr(struct mt76_dev *dev, u32 offset);
  1534. void mt76s_wr(struct mt76_dev *dev, u32 offset, u32 val);
  1535. u32 mt76s_rmw(struct mt76_dev *dev, u32 offset, u32 mask, u32 val);
  1536. u32 mt76s_read_pcr(struct mt76_dev *dev);
  1537. void mt76s_write_copy(struct mt76_dev *dev, u32 offset,
  1538. const void *data, int len);
  1539. void mt76s_read_copy(struct mt76_dev *dev, u32 offset,
  1540. void *data, int len);
  1541. int mt76s_wr_rp(struct mt76_dev *dev, u32 base,
  1542. const struct mt76_reg_pair *data,
  1543. int len);
  1544. int mt76s_rd_rp(struct mt76_dev *dev, u32 base,
  1545. struct mt76_reg_pair *data, int len);
  1546. struct sk_buff *
  1547. __mt76_mcu_msg_alloc(struct mt76_dev *dev, const void *data,
  1548. int len, int data_len, gfp_t gfp);
  1549. static inline struct sk_buff *
  1550. mt76_mcu_msg_alloc(struct mt76_dev *dev, const void *data,
  1551. int data_len)
  1552. {
  1553. return __mt76_mcu_msg_alloc(dev, data, data_len, data_len, GFP_KERNEL);
  1554. }
  1555. void mt76_mcu_rx_event(struct mt76_dev *dev, struct sk_buff *skb);
  1556. struct sk_buff *mt76_mcu_get_response(struct mt76_dev *dev,
  1557. unsigned long expires);
  1558. int mt76_mcu_send_and_get_msg(struct mt76_dev *dev, int cmd, const void *data,
  1559. int len, bool wait_resp, struct sk_buff **ret);
  1560. int mt76_mcu_skb_send_and_get_msg(struct mt76_dev *dev, struct sk_buff *skb,
  1561. int cmd, bool wait_resp, struct sk_buff **ret);
  1562. int __mt76_mcu_send_firmware(struct mt76_dev *dev, int cmd, const void *data,
  1563. int len, int max_len);
  1564. static inline int
  1565. mt76_mcu_send_firmware(struct mt76_dev *dev, int cmd, const void *data,
  1566. int len)
  1567. {
  1568. int max_len = 4096 - dev->mcu_ops->headroom;
  1569. return __mt76_mcu_send_firmware(dev, cmd, data, len, max_len);
  1570. }
  1571. static inline int
  1572. mt76_mcu_send_msg(struct mt76_dev *dev, int cmd, const void *data, int len,
  1573. bool wait_resp)
  1574. {
  1575. return mt76_mcu_send_and_get_msg(dev, cmd, data, len, wait_resp, NULL);
  1576. }
  1577. static inline int
  1578. mt76_mcu_skb_send_msg(struct mt76_dev *dev, struct sk_buff *skb, int cmd,
  1579. bool wait_resp)
  1580. {
  1581. return mt76_mcu_skb_send_and_get_msg(dev, skb, cmd, wait_resp, NULL);
  1582. }
  1583. void mt76_set_irq_mask(struct mt76_dev *dev, u32 addr, u32 clear, u32 set);
  1584. struct device_node *
  1585. mt76_find_power_limits_node(struct mt76_dev *dev);
  1586. struct device_node *
  1587. mt76_find_channel_node(struct device_node *np, struct ieee80211_channel *chan);
  1588. s8 mt76_get_rate_power_limits(struct mt76_phy *phy,
  1589. struct ieee80211_channel *chan,
  1590. struct mt76_power_limits *dest,
  1591. s8 target_power);
  1592. static inline bool mt76_queue_is_rx(struct mt76_dev *dev, struct mt76_queue *q)
  1593. {
  1594. int i;
  1595. for (i = 0; i < ARRAY_SIZE(dev->q_rx); i++) {
  1596. if (q == &dev->q_rx[i])
  1597. return true;
  1598. }
  1599. return false;
  1600. }
  1601. static inline bool mt76_queue_is_wed_tx_free(struct mt76_queue *q)
  1602. {
  1603. return (q->flags & MT_QFLAG_WED) &&
  1604. FIELD_GET(MT_QFLAG_WED_TYPE, q->flags) == MT76_WED_Q_TXFREE;
  1605. }
  1606. static inline bool mt76_queue_is_wed_rro(struct mt76_queue *q)
  1607. {
  1608. return q->flags & MT_QFLAG_WED_RRO;
  1609. }
  1610. static inline bool mt76_queue_is_wed_rro_ind(struct mt76_queue *q)
  1611. {
  1612. return mt76_queue_is_wed_rro(q) &&
  1613. FIELD_GET(MT_QFLAG_WED_TYPE, q->flags) == MT76_WED_RRO_Q_IND;
  1614. }
  1615. static inline bool mt76_queue_is_wed_rro_rxdmad_c(struct mt76_queue *q)
  1616. {
  1617. return mt76_queue_is_wed_rro(q) &&
  1618. FIELD_GET(MT_QFLAG_WED_TYPE, q->flags) == MT76_WED_RRO_Q_RXDMAD_C;
  1619. }
  1620. static inline bool mt76_queue_is_wed_rro_data(struct mt76_queue *q)
  1621. {
  1622. return mt76_queue_is_wed_rro(q) &&
  1623. FIELD_GET(MT_QFLAG_WED_TYPE, q->flags) == MT76_WED_RRO_Q_DATA;
  1624. }
  1625. static inline bool mt76_queue_is_wed_rro_msdu_pg(struct mt76_queue *q)
  1626. {
  1627. return mt76_queue_is_wed_rro(q) &&
  1628. FIELD_GET(MT_QFLAG_WED_TYPE, q->flags) ==
  1629. MT76_WED_RRO_Q_MSDU_PG;
  1630. }
  1631. static inline bool mt76_queue_is_wed_rx(struct mt76_queue *q)
  1632. {
  1633. return (q->flags & MT_QFLAG_WED) &&
  1634. FIELD_GET(MT_QFLAG_WED_TYPE, q->flags) == MT76_WED_Q_RX;
  1635. }
  1636. static inline bool mt76_queue_is_emi(struct mt76_queue *q)
  1637. {
  1638. return q->flags & MT_QFLAG_EMI_EN;
  1639. }
  1640. static inline bool mt76_queue_is_npu(struct mt76_queue *q)
  1641. {
  1642. return q->flags & MT_QFLAG_NPU;
  1643. }
  1644. static inline bool mt76_queue_is_npu_tx(struct mt76_queue *q)
  1645. {
  1646. return mt76_queue_is_npu(q) &&
  1647. FIELD_GET(MT_QFLAG_WED_TYPE, q->flags) == MT76_WED_Q_TX;
  1648. }
  1649. static inline bool mt76_queue_is_npu_rx(struct mt76_queue *q)
  1650. {
  1651. return mt76_queue_is_npu(q) &&
  1652. FIELD_GET(MT_QFLAG_WED_TYPE, q->flags) == MT76_WED_Q_RX;
  1653. }
  1654. struct mt76_txwi_cache *
  1655. mt76_token_release(struct mt76_dev *dev, int token, bool *wake);
  1656. int mt76_token_consume(struct mt76_dev *dev, struct mt76_txwi_cache **ptxwi);
  1657. void __mt76_set_tx_blocked(struct mt76_dev *dev, bool blocked);
  1658. struct mt76_txwi_cache *mt76_rx_token_release(struct mt76_dev *dev, int token);
  1659. int mt76_rx_token_consume(struct mt76_dev *dev, void *ptr,
  1660. struct mt76_txwi_cache *r, dma_addr_t phys);
  1661. int mt76_create_page_pool(struct mt76_dev *dev, struct mt76_queue *q);
  1662. static inline void mt76_put_page_pool_buf(void *buf, bool allow_direct)
  1663. {
  1664. struct page *page = virt_to_head_page(buf);
  1665. page_pool_put_full_page(pp_page_to_nmdesc(page)->pp, page,
  1666. allow_direct);
  1667. }
  1668. static inline void *
  1669. mt76_get_page_pool_buf(struct mt76_queue *q, u32 *offset, u32 size)
  1670. {
  1671. struct page *page;
  1672. page = page_pool_alloc_frag(q->page_pool, offset, size,
  1673. GFP_ATOMIC | __GFP_NOWARN | GFP_DMA32);
  1674. if (!page)
  1675. return NULL;
  1676. return page_address(page) + *offset;
  1677. }
  1678. static inline void mt76_set_tx_blocked(struct mt76_dev *dev, bool blocked)
  1679. {
  1680. spin_lock_bh(&dev->token_lock);
  1681. __mt76_set_tx_blocked(dev, blocked);
  1682. spin_unlock_bh(&dev->token_lock);
  1683. }
  1684. static inline int
  1685. mt76_token_get(struct mt76_dev *dev, struct mt76_txwi_cache **ptxwi)
  1686. {
  1687. int token;
  1688. spin_lock_bh(&dev->token_lock);
  1689. token = idr_alloc(&dev->token, *ptxwi, 0, dev->token_size, GFP_ATOMIC);
  1690. spin_unlock_bh(&dev->token_lock);
  1691. return token;
  1692. }
  1693. static inline struct mt76_txwi_cache *
  1694. mt76_token_put(struct mt76_dev *dev, int token)
  1695. {
  1696. struct mt76_txwi_cache *txwi;
  1697. spin_lock_bh(&dev->token_lock);
  1698. txwi = idr_remove(&dev->token, token);
  1699. spin_unlock_bh(&dev->token_lock);
  1700. return txwi;
  1701. }
  1702. void mt76_wcid_init(struct mt76_wcid *wcid, u8 band_idx);
  1703. void mt76_wcid_cleanup(struct mt76_dev *dev, struct mt76_wcid *wcid);
  1704. void mt76_wcid_add_poll(struct mt76_dev *dev, struct mt76_wcid *wcid);
  1705. static inline void
  1706. mt76_vif_init(struct ieee80211_vif *vif, struct mt76_vif_data *mvif)
  1707. {
  1708. struct mt76_vif_link *mlink = (struct mt76_vif_link *)vif->drv_priv;
  1709. mlink->mvif = mvif;
  1710. rcu_assign_pointer(mvif->link[0], mlink);
  1711. }
  1712. void mt76_vif_cleanup(struct mt76_dev *dev, struct ieee80211_vif *vif);
  1713. u16 mt76_select_links(struct ieee80211_vif *vif, int max_active_links);
  1714. static inline struct mt76_vif_link *
  1715. mt76_vif_link(struct mt76_dev *dev, struct ieee80211_vif *vif, int link_id)
  1716. {
  1717. struct mt76_vif_link *mlink = (struct mt76_vif_link *)vif->drv_priv;
  1718. struct mt76_vif_data *mvif = mlink->mvif;
  1719. if (!link_id)
  1720. return mlink;
  1721. return mt76_dereference(mvif->link[link_id], dev);
  1722. }
  1723. static inline struct mt76_vif_link *
  1724. mt76_vif_conf_link(struct mt76_dev *dev, struct ieee80211_vif *vif,
  1725. struct ieee80211_bss_conf *link_conf)
  1726. {
  1727. struct mt76_vif_link *mlink = (struct mt76_vif_link *)vif->drv_priv;
  1728. struct mt76_vif_data *mvif = mlink->mvif;
  1729. if (link_conf == &vif->bss_conf || !link_conf->link_id)
  1730. return mlink;
  1731. return mt76_dereference(mvif->link[link_conf->link_id], dev);
  1732. }
  1733. static inline struct mt76_phy *
  1734. mt76_vif_link_phy(struct mt76_vif_link *mlink)
  1735. {
  1736. struct mt76_chanctx *ctx;
  1737. if (!mlink->ctx)
  1738. return NULL;
  1739. ctx = (struct mt76_chanctx *)mlink->ctx->drv_priv;
  1740. return ctx->phy;
  1741. }
  1742. #endif