mt76.h 43 KB

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