dma.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192
  1. // SPDX-License-Identifier: BSD-3-Clause-Clear
  2. /*
  3. * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
  4. */
  5. #include <linux/dma-mapping.h>
  6. #include "mt76.h"
  7. #include "dma.h"
  8. static struct mt76_txwi_cache *
  9. mt76_alloc_txwi(struct mt76_dev *dev)
  10. {
  11. struct mt76_txwi_cache *t;
  12. dma_addr_t addr;
  13. u8 *txwi;
  14. int size;
  15. size = L1_CACHE_ALIGN(dev->drv->txwi_size + sizeof(*t));
  16. txwi = kzalloc(size, GFP_ATOMIC);
  17. if (!txwi)
  18. return NULL;
  19. addr = dma_map_single(dev->dma_dev, txwi, dev->drv->txwi_size,
  20. DMA_TO_DEVICE);
  21. if (unlikely(dma_mapping_error(dev->dma_dev, addr))) {
  22. kfree(txwi);
  23. return NULL;
  24. }
  25. t = (struct mt76_txwi_cache *)(txwi + dev->drv->txwi_size);
  26. t->dma_addr = addr;
  27. return t;
  28. }
  29. static struct mt76_txwi_cache *
  30. mt76_alloc_rxwi(struct mt76_dev *dev)
  31. {
  32. struct mt76_txwi_cache *t;
  33. t = kzalloc(L1_CACHE_ALIGN(sizeof(*t)), GFP_ATOMIC);
  34. if (!t)
  35. return NULL;
  36. t->ptr = NULL;
  37. return t;
  38. }
  39. static struct mt76_txwi_cache *
  40. __mt76_get_txwi(struct mt76_dev *dev)
  41. {
  42. struct mt76_txwi_cache *t = NULL;
  43. spin_lock(&dev->lock);
  44. if (!list_empty(&dev->txwi_cache)) {
  45. t = list_first_entry(&dev->txwi_cache, struct mt76_txwi_cache,
  46. list);
  47. list_del(&t->list);
  48. }
  49. spin_unlock(&dev->lock);
  50. return t;
  51. }
  52. static struct mt76_txwi_cache *
  53. __mt76_get_rxwi(struct mt76_dev *dev)
  54. {
  55. struct mt76_txwi_cache *t = NULL;
  56. spin_lock_bh(&dev->wed_lock);
  57. if (!list_empty(&dev->rxwi_cache)) {
  58. t = list_first_entry(&dev->rxwi_cache, struct mt76_txwi_cache,
  59. list);
  60. list_del(&t->list);
  61. }
  62. spin_unlock_bh(&dev->wed_lock);
  63. return t;
  64. }
  65. static struct mt76_txwi_cache *
  66. mt76_get_txwi(struct mt76_dev *dev)
  67. {
  68. struct mt76_txwi_cache *t = __mt76_get_txwi(dev);
  69. if (t)
  70. return t;
  71. return mt76_alloc_txwi(dev);
  72. }
  73. struct mt76_txwi_cache *
  74. mt76_get_rxwi(struct mt76_dev *dev)
  75. {
  76. struct mt76_txwi_cache *t = __mt76_get_rxwi(dev);
  77. if (t)
  78. return t;
  79. return mt76_alloc_rxwi(dev);
  80. }
  81. EXPORT_SYMBOL_GPL(mt76_get_rxwi);
  82. void
  83. mt76_put_txwi(struct mt76_dev *dev, struct mt76_txwi_cache *t)
  84. {
  85. if (!t)
  86. return;
  87. spin_lock(&dev->lock);
  88. list_add(&t->list, &dev->txwi_cache);
  89. spin_unlock(&dev->lock);
  90. }
  91. EXPORT_SYMBOL_GPL(mt76_put_txwi);
  92. void
  93. mt76_put_rxwi(struct mt76_dev *dev, struct mt76_txwi_cache *t)
  94. {
  95. if (!t)
  96. return;
  97. spin_lock_bh(&dev->wed_lock);
  98. list_add(&t->list, &dev->rxwi_cache);
  99. spin_unlock_bh(&dev->wed_lock);
  100. }
  101. EXPORT_SYMBOL_GPL(mt76_put_rxwi);
  102. static void
  103. mt76_free_pending_txwi(struct mt76_dev *dev)
  104. {
  105. struct mt76_txwi_cache *t;
  106. local_bh_disable();
  107. while ((t = __mt76_get_txwi(dev)) != NULL) {
  108. dma_unmap_single(dev->dma_dev, t->dma_addr, dev->drv->txwi_size,
  109. DMA_TO_DEVICE);
  110. kfree(mt76_get_txwi_ptr(dev, t));
  111. }
  112. local_bh_enable();
  113. }
  114. void
  115. mt76_free_pending_rxwi(struct mt76_dev *dev)
  116. {
  117. struct mt76_txwi_cache *t;
  118. local_bh_disable();
  119. while ((t = __mt76_get_rxwi(dev)) != NULL) {
  120. if (t->ptr)
  121. mt76_put_page_pool_buf(t->ptr, false);
  122. kfree(t);
  123. }
  124. local_bh_enable();
  125. }
  126. EXPORT_SYMBOL_GPL(mt76_free_pending_rxwi);
  127. static void
  128. mt76_dma_queue_magic_cnt_init(struct mt76_dev *dev, struct mt76_queue *q)
  129. {
  130. if (!mt76_queue_is_wed_rro(q))
  131. return;
  132. q->magic_cnt = 0;
  133. if (mt76_queue_is_wed_rro_ind(q)) {
  134. struct mt76_wed_rro_desc *rro_desc;
  135. u32 data1 = FIELD_PREP(RRO_IND_DATA1_MAGIC_CNT_MASK,
  136. MT_DMA_WED_IND_CMD_CNT - 1);
  137. int i;
  138. rro_desc = (struct mt76_wed_rro_desc *)q->desc;
  139. for (i = 0; i < q->ndesc; i++) {
  140. struct mt76_wed_rro_ind *cmd;
  141. cmd = (struct mt76_wed_rro_ind *)&rro_desc[i];
  142. cmd->data1 = cpu_to_le32(data1);
  143. }
  144. } else if (mt76_queue_is_wed_rro_rxdmad_c(q)) {
  145. struct mt76_rro_rxdmad_c *dmad = (void *)q->desc;
  146. u32 data3 = FIELD_PREP(RRO_RXDMAD_DATA3_MAGIC_CNT_MASK,
  147. MT_DMA_MAGIC_CNT - 1);
  148. int i;
  149. for (i = 0; i < q->ndesc; i++)
  150. dmad[i].data3 = cpu_to_le32(data3);
  151. }
  152. }
  153. static void
  154. mt76_dma_sync_idx(struct mt76_dev *dev, struct mt76_queue *q)
  155. {
  156. Q_WRITE(q, desc_base, q->desc_dma);
  157. if ((q->flags & MT_QFLAG_WED_RRO_EN) && !mt76_npu_device_active(dev))
  158. Q_WRITE(q, ring_size, MT_DMA_RRO_EN | q->ndesc);
  159. else
  160. Q_WRITE(q, ring_size, q->ndesc);
  161. if (mt76_queue_is_npu_tx(q)) {
  162. writel(q->desc_dma, &q->regs->desc_base);
  163. writel(q->ndesc, &q->regs->ring_size);
  164. }
  165. q->head = Q_READ(q, dma_idx);
  166. q->tail = q->head;
  167. }
  168. void mt76_dma_queue_reset(struct mt76_dev *dev, struct mt76_queue *q,
  169. bool reset_idx)
  170. {
  171. if (!q || !q->ndesc)
  172. return;
  173. if (!mt76_queue_is_wed_rro_ind(q) &&
  174. !mt76_queue_is_wed_rro_rxdmad_c(q) && !mt76_queue_is_npu(q)) {
  175. int i;
  176. /* clear descriptors */
  177. for (i = 0; i < q->ndesc; i++)
  178. q->desc[i].ctrl = cpu_to_le32(MT_DMA_CTL_DMA_DONE);
  179. }
  180. mt76_dma_queue_magic_cnt_init(dev, q);
  181. if (reset_idx) {
  182. if (mt76_queue_is_emi(q))
  183. *q->emi_cpu_idx = 0;
  184. else
  185. Q_WRITE(q, cpu_idx, 0);
  186. Q_WRITE(q, dma_idx, 0);
  187. }
  188. mt76_dma_sync_idx(dev, q);
  189. }
  190. static int
  191. mt76_dma_add_rx_buf(struct mt76_dev *dev, struct mt76_queue *q,
  192. struct mt76_queue_buf *buf, void *data)
  193. {
  194. struct mt76_queue_entry *entry = &q->entry[q->head];
  195. struct mt76_txwi_cache *txwi = NULL;
  196. u32 buf1 = 0, ctrl, info = 0;
  197. struct mt76_desc *desc;
  198. int idx = q->head;
  199. int rx_token;
  200. if (mt76_queue_is_wed_rro_ind(q)) {
  201. struct mt76_wed_rro_desc *rro_desc;
  202. rro_desc = (struct mt76_wed_rro_desc *)q->desc;
  203. data = &rro_desc[q->head];
  204. goto done;
  205. } else if (mt76_queue_is_wed_rro_rxdmad_c(q)) {
  206. data = &q->desc[q->head];
  207. goto done;
  208. }
  209. desc = &q->desc[q->head];
  210. ctrl = FIELD_PREP(MT_DMA_CTL_SD_LEN0, buf[0].len);
  211. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  212. buf1 = FIELD_PREP(MT_DMA_CTL_SDP0_H, buf->addr >> 32);
  213. #endif
  214. if (mt76_queue_is_wed_rx(q) || mt76_queue_is_wed_rro_data(q)) {
  215. txwi = mt76_get_rxwi(dev);
  216. if (!txwi)
  217. return -ENOMEM;
  218. rx_token = mt76_rx_token_consume(dev, data, txwi, buf->addr);
  219. if (rx_token < 0) {
  220. mt76_put_rxwi(dev, txwi);
  221. return -ENOMEM;
  222. }
  223. buf1 |= FIELD_PREP(MT_DMA_CTL_TOKEN, rx_token);
  224. ctrl |= MT_DMA_CTL_TO_HOST;
  225. txwi->qid = q - dev->q_rx;
  226. }
  227. if (mt76_queue_is_wed_rro_msdu_pg(q) &&
  228. dev->drv->rx_rro_add_msdu_page) {
  229. if (dev->drv->rx_rro_add_msdu_page(dev, q, buf->addr, data))
  230. return -ENOMEM;
  231. }
  232. if (q->flags & MT_QFLAG_WED_RRO_EN) {
  233. info |= FIELD_PREP(MT_DMA_MAGIC_MASK, q->magic_cnt);
  234. if ((q->head + 1) == q->ndesc)
  235. q->magic_cnt = (q->magic_cnt + 1) % MT_DMA_MAGIC_CNT;
  236. }
  237. WRITE_ONCE(desc->buf0, cpu_to_le32(buf->addr));
  238. WRITE_ONCE(desc->buf1, cpu_to_le32(buf1));
  239. WRITE_ONCE(desc->ctrl, cpu_to_le32(ctrl));
  240. WRITE_ONCE(desc->info, cpu_to_le32(info));
  241. done:
  242. entry->dma_addr[0] = buf->addr;
  243. entry->dma_len[0] = buf->len;
  244. entry->txwi = txwi;
  245. entry->buf = data;
  246. entry->wcid = 0xffff;
  247. entry->skip_buf1 = true;
  248. q->head = (q->head + 1) % q->ndesc;
  249. q->queued++;
  250. return idx;
  251. }
  252. static int
  253. mt76_dma_add_buf(struct mt76_dev *dev, struct mt76_queue *q,
  254. struct mt76_queue_buf *buf, int nbufs, u32 info,
  255. struct sk_buff *skb, void *txwi)
  256. {
  257. struct mt76_queue_entry *entry;
  258. struct mt76_desc *desc;
  259. int i, idx = -1;
  260. u32 ctrl, next;
  261. if (txwi) {
  262. q->entry[q->head].txwi = DMA_DUMMY_DATA;
  263. q->entry[q->head].skip_buf0 = true;
  264. }
  265. for (i = 0; i < nbufs; i += 2, buf += 2) {
  266. u32 buf0 = buf[0].addr, buf1 = 0;
  267. idx = q->head;
  268. next = (q->head + 1) % q->ndesc;
  269. desc = &q->desc[idx];
  270. entry = &q->entry[idx];
  271. if (buf[0].skip_unmap)
  272. entry->skip_buf0 = true;
  273. entry->skip_buf1 = i == nbufs - 1;
  274. entry->dma_addr[0] = buf[0].addr;
  275. entry->dma_len[0] = buf[0].len;
  276. ctrl = FIELD_PREP(MT_DMA_CTL_SD_LEN0, buf[0].len);
  277. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  278. info |= FIELD_PREP(MT_DMA_CTL_SDP0_H, buf[0].addr >> 32);
  279. #endif
  280. if (i < nbufs - 1) {
  281. entry->dma_addr[1] = buf[1].addr;
  282. entry->dma_len[1] = buf[1].len;
  283. buf1 = buf[1].addr;
  284. ctrl |= FIELD_PREP(MT_DMA_CTL_SD_LEN1, buf[1].len);
  285. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  286. info |= FIELD_PREP(MT_DMA_CTL_SDP1_H,
  287. buf[1].addr >> 32);
  288. #endif
  289. if (buf[1].skip_unmap)
  290. entry->skip_buf1 = true;
  291. }
  292. if (i == nbufs - 1)
  293. ctrl |= MT_DMA_CTL_LAST_SEC0;
  294. else if (i == nbufs - 2)
  295. ctrl |= MT_DMA_CTL_LAST_SEC1;
  296. WRITE_ONCE(desc->buf0, cpu_to_le32(buf0));
  297. WRITE_ONCE(desc->buf1, cpu_to_le32(buf1));
  298. WRITE_ONCE(desc->info, cpu_to_le32(info));
  299. WRITE_ONCE(desc->ctrl, cpu_to_le32(ctrl));
  300. q->head = next;
  301. q->queued++;
  302. }
  303. q->entry[idx].txwi = txwi;
  304. q->entry[idx].skb = skb;
  305. q->entry[idx].wcid = 0xffff;
  306. return idx;
  307. }
  308. static void
  309. mt76_dma_tx_cleanup_idx(struct mt76_dev *dev, struct mt76_queue *q, int idx,
  310. struct mt76_queue_entry *prev_e)
  311. {
  312. struct mt76_queue_entry *e = &q->entry[idx];
  313. if (!e->skip_buf0)
  314. dma_unmap_single(dev->dma_dev, e->dma_addr[0], e->dma_len[0],
  315. DMA_TO_DEVICE);
  316. if (!e->skip_buf1)
  317. dma_unmap_single(dev->dma_dev, e->dma_addr[1], e->dma_len[1],
  318. DMA_TO_DEVICE);
  319. if (e->txwi == DMA_DUMMY_DATA)
  320. e->txwi = NULL;
  321. *prev_e = *e;
  322. memset(e, 0, sizeof(*e));
  323. }
  324. static void
  325. mt76_dma_kick_queue(struct mt76_dev *dev, struct mt76_queue *q)
  326. {
  327. wmb();
  328. if (mt76_queue_is_emi(q))
  329. *q->emi_cpu_idx = cpu_to_le16(q->head);
  330. else
  331. Q_WRITE(q, cpu_idx, q->head);
  332. }
  333. static void
  334. mt76_dma_tx_cleanup(struct mt76_dev *dev, struct mt76_queue *q, bool flush)
  335. {
  336. struct mt76_queue_entry entry;
  337. int last;
  338. if (!q || !q->ndesc)
  339. return;
  340. spin_lock_bh(&q->cleanup_lock);
  341. if (flush)
  342. last = -1;
  343. else
  344. last = Q_READ(q, dma_idx);
  345. while (q->queued > 0 && q->tail != last) {
  346. mt76_dma_tx_cleanup_idx(dev, q, q->tail, &entry);
  347. mt76_npu_txdesc_cleanup(q, q->tail);
  348. mt76_queue_tx_complete(dev, q, &entry);
  349. if (entry.txwi) {
  350. if (!(dev->drv->drv_flags & MT_DRV_TXWI_NO_FREE))
  351. mt76_put_txwi(dev, entry.txwi);
  352. }
  353. if (!flush && q->tail == last)
  354. last = Q_READ(q, dma_idx);
  355. }
  356. spin_unlock_bh(&q->cleanup_lock);
  357. if (flush) {
  358. spin_lock_bh(&q->lock);
  359. mt76_dma_sync_idx(dev, q);
  360. mt76_dma_kick_queue(dev, q);
  361. spin_unlock_bh(&q->lock);
  362. }
  363. if (!q->queued)
  364. wake_up(&dev->tx_wait);
  365. }
  366. static void *
  367. mt76_dma_get_rxdmad_c_buf(struct mt76_dev *dev, struct mt76_queue *q,
  368. int idx, int *len, bool *more)
  369. {
  370. struct mt76_queue_entry *e = &q->entry[idx];
  371. struct mt76_rro_rxdmad_c *dmad = e->buf;
  372. u32 data1 = le32_to_cpu(dmad->data1);
  373. u32 data2 = le32_to_cpu(dmad->data2);
  374. struct mt76_txwi_cache *t;
  375. u16 rx_token_id;
  376. u8 ind_reason;
  377. void *buf;
  378. rx_token_id = FIELD_GET(RRO_RXDMAD_DATA2_RX_TOKEN_ID_MASK, data2);
  379. t = mt76_rx_token_release(dev, rx_token_id);
  380. if (!t)
  381. return ERR_PTR(-EAGAIN);
  382. q = &dev->q_rx[t->qid];
  383. dma_sync_single_for_cpu(dev->dma_dev, t->dma_addr,
  384. SKB_WITH_OVERHEAD(q->buf_size),
  385. page_pool_get_dma_dir(q->page_pool));
  386. if (len)
  387. *len = FIELD_GET(RRO_RXDMAD_DATA1_SDL0_MASK, data1);
  388. if (more)
  389. *more = !FIELD_GET(RRO_RXDMAD_DATA1_LS_MASK, data1);
  390. buf = t->ptr;
  391. ind_reason = FIELD_GET(RRO_RXDMAD_DATA2_IND_REASON_MASK, data2);
  392. if (ind_reason == MT_DMA_WED_IND_REASON_REPEAT ||
  393. ind_reason == MT_DMA_WED_IND_REASON_OLDPKT) {
  394. mt76_put_page_pool_buf(buf, false);
  395. buf = ERR_PTR(-EAGAIN);
  396. }
  397. t->ptr = NULL;
  398. t->dma_addr = 0;
  399. mt76_put_rxwi(dev, t);
  400. return buf;
  401. }
  402. static void *
  403. mt76_dma_get_buf(struct mt76_dev *dev, struct mt76_queue *q, int idx,
  404. int *len, u32 *info, bool *more, bool *drop, bool flush)
  405. {
  406. struct mt76_queue_entry *e = &q->entry[idx];
  407. struct mt76_desc *desc = &q->desc[idx];
  408. u32 ctrl, desc_info, buf1;
  409. void *buf = e->buf;
  410. if (mt76_queue_is_wed_rro_rxdmad_c(q) && !flush)
  411. buf = mt76_dma_get_rxdmad_c_buf(dev, q, idx, len, more);
  412. if (mt76_queue_is_wed_rro(q))
  413. goto done;
  414. ctrl = le32_to_cpu(READ_ONCE(desc->ctrl));
  415. if (len) {
  416. *len = FIELD_GET(MT_DMA_CTL_SD_LEN0, ctrl);
  417. *more = !(ctrl & MT_DMA_CTL_LAST_SEC0);
  418. }
  419. desc_info = le32_to_cpu(desc->info);
  420. if (info)
  421. *info = desc_info;
  422. buf1 = le32_to_cpu(desc->buf1);
  423. mt76_dma_should_drop_buf(drop, ctrl, buf1, desc_info);
  424. if (mt76_queue_is_wed_rx(q)) {
  425. u32 token = FIELD_GET(MT_DMA_CTL_TOKEN, buf1);
  426. struct mt76_txwi_cache *t = mt76_rx_token_release(dev, token);
  427. if (!t)
  428. return NULL;
  429. dma_sync_single_for_cpu(dev->dma_dev, t->dma_addr,
  430. SKB_WITH_OVERHEAD(q->buf_size),
  431. page_pool_get_dma_dir(q->page_pool));
  432. buf = t->ptr;
  433. t->dma_addr = 0;
  434. t->ptr = NULL;
  435. mt76_put_rxwi(dev, t);
  436. if (drop)
  437. *drop |= !!(buf1 & MT_DMA_CTL_WO_DROP);
  438. } else {
  439. dma_sync_single_for_cpu(dev->dma_dev, e->dma_addr[0],
  440. SKB_WITH_OVERHEAD(q->buf_size),
  441. page_pool_get_dma_dir(q->page_pool));
  442. }
  443. done:
  444. e->buf = NULL;
  445. return buf;
  446. }
  447. static void *
  448. mt76_dma_dequeue(struct mt76_dev *dev, struct mt76_queue *q, bool flush,
  449. int *len, u32 *info, bool *more, bool *drop)
  450. {
  451. int idx = q->tail;
  452. *more = false;
  453. if (!q->queued)
  454. return NULL;
  455. if (mt76_queue_is_wed_rro_data(q) || mt76_queue_is_wed_rro_msdu_pg(q))
  456. goto done;
  457. if (mt76_queue_is_wed_rro_ind(q)) {
  458. struct mt76_wed_rro_ind *cmd;
  459. u8 magic_cnt;
  460. if (flush)
  461. goto done;
  462. cmd = q->entry[idx].buf;
  463. magic_cnt = FIELD_GET(RRO_IND_DATA1_MAGIC_CNT_MASK,
  464. le32_to_cpu(cmd->data1));
  465. if (magic_cnt != q->magic_cnt)
  466. return NULL;
  467. if (q->tail == q->ndesc - 1)
  468. q->magic_cnt = (q->magic_cnt + 1) % MT_DMA_WED_IND_CMD_CNT;
  469. } else if (mt76_queue_is_wed_rro_rxdmad_c(q)) {
  470. struct mt76_rro_rxdmad_c *dmad;
  471. u16 magic_cnt;
  472. if (flush)
  473. goto done;
  474. dmad = q->entry[idx].buf;
  475. magic_cnt = FIELD_GET(RRO_RXDMAD_DATA3_MAGIC_CNT_MASK,
  476. le32_to_cpu(dmad->data3));
  477. if (magic_cnt != q->magic_cnt)
  478. return NULL;
  479. if (q->tail == q->ndesc - 1)
  480. q->magic_cnt = (q->magic_cnt + 1) % MT_DMA_MAGIC_CNT;
  481. } else {
  482. if (flush)
  483. q->desc[idx].ctrl |= cpu_to_le32(MT_DMA_CTL_DMA_DONE);
  484. else if (!(q->desc[idx].ctrl & cpu_to_le32(MT_DMA_CTL_DMA_DONE)))
  485. return NULL;
  486. }
  487. done:
  488. q->tail = (q->tail + 1) % q->ndesc;
  489. q->queued--;
  490. return mt76_dma_get_buf(dev, q, idx, len, info, more, drop, flush);
  491. }
  492. static int
  493. mt76_dma_tx_queue_skb_raw(struct mt76_dev *dev, struct mt76_queue *q,
  494. struct sk_buff *skb, u32 tx_info)
  495. {
  496. struct mt76_queue_buf buf = {};
  497. dma_addr_t addr;
  498. if (test_bit(MT76_MCU_RESET, &dev->phy.state))
  499. goto error;
  500. if (q->queued + 1 >= q->ndesc - 1)
  501. goto error;
  502. addr = dma_map_single(dev->dma_dev, skb->data, skb->len,
  503. DMA_TO_DEVICE);
  504. if (unlikely(dma_mapping_error(dev->dma_dev, addr)))
  505. goto error;
  506. buf.addr = addr;
  507. buf.len = skb->len;
  508. spin_lock_bh(&q->lock);
  509. mt76_dma_add_buf(dev, q, &buf, 1, tx_info, skb, NULL);
  510. mt76_dma_kick_queue(dev, q);
  511. spin_unlock_bh(&q->lock);
  512. return 0;
  513. error:
  514. dev_kfree_skb(skb);
  515. return -ENOMEM;
  516. }
  517. static int
  518. mt76_dma_tx_queue_skb(struct mt76_phy *phy, struct mt76_queue *q,
  519. enum mt76_txq_id qid, struct sk_buff *skb,
  520. struct mt76_wcid *wcid, struct ieee80211_sta *sta)
  521. {
  522. struct ieee80211_tx_status status = {
  523. .sta = sta,
  524. };
  525. struct mt76_tx_info tx_info = {
  526. .skb = skb,
  527. };
  528. struct mt76_dev *dev = phy->dev;
  529. struct ieee80211_hw *hw;
  530. int len, n = 0, ret = -ENOMEM;
  531. struct mt76_txwi_cache *t;
  532. struct sk_buff *iter;
  533. dma_addr_t addr;
  534. u8 *txwi;
  535. if (test_bit(MT76_RESET, &phy->state))
  536. goto free_skb;
  537. /* TODO: Take into account unlinear skbs */
  538. if (mt76_npu_device_active(dev) && skb_linearize(skb))
  539. goto free_skb;
  540. t = mt76_get_txwi(dev);
  541. if (!t)
  542. goto free_skb;
  543. txwi = mt76_get_txwi_ptr(dev, t);
  544. skb->prev = skb->next = NULL;
  545. if (dev->drv->drv_flags & MT_DRV_TX_ALIGNED4_SKBS)
  546. mt76_insert_hdr_pad(skb);
  547. len = skb_headlen(skb);
  548. addr = dma_map_single(dev->dma_dev, skb->data, len, DMA_TO_DEVICE);
  549. if (unlikely(dma_mapping_error(dev->dma_dev, addr)))
  550. goto free;
  551. tx_info.buf[n].addr = t->dma_addr;
  552. tx_info.buf[n++].len = dev->drv->txwi_size;
  553. tx_info.buf[n].addr = addr;
  554. tx_info.buf[n++].len = len;
  555. skb_walk_frags(skb, iter) {
  556. if (n == ARRAY_SIZE(tx_info.buf))
  557. goto unmap;
  558. addr = dma_map_single(dev->dma_dev, iter->data, iter->len,
  559. DMA_TO_DEVICE);
  560. if (unlikely(dma_mapping_error(dev->dma_dev, addr)))
  561. goto unmap;
  562. tx_info.buf[n].addr = addr;
  563. tx_info.buf[n++].len = iter->len;
  564. }
  565. tx_info.nbuf = n;
  566. if (q->queued + (tx_info.nbuf + 1) / 2 >= q->ndesc - 1) {
  567. ret = -ENOMEM;
  568. goto unmap;
  569. }
  570. dma_sync_single_for_cpu(dev->dma_dev, t->dma_addr, dev->drv->txwi_size,
  571. DMA_TO_DEVICE);
  572. ret = dev->drv->tx_prepare_skb(dev, txwi, qid, wcid, sta, &tx_info);
  573. dma_sync_single_for_device(dev->dma_dev, t->dma_addr, dev->drv->txwi_size,
  574. DMA_TO_DEVICE);
  575. if (ret < 0)
  576. goto unmap;
  577. if (mt76_npu_device_active(dev))
  578. return mt76_npu_dma_add_buf(phy, q, skb, &tx_info.buf[1], txwi);
  579. return mt76_dma_add_buf(dev, q, tx_info.buf, tx_info.nbuf,
  580. tx_info.info, tx_info.skb, t);
  581. unmap:
  582. for (n--; n > 0; n--)
  583. dma_unmap_single(dev->dma_dev, tx_info.buf[n].addr,
  584. tx_info.buf[n].len, DMA_TO_DEVICE);
  585. free:
  586. #ifdef CONFIG_NL80211_TESTMODE
  587. /* fix tx_done accounting on queue overflow */
  588. if (mt76_is_testmode_skb(dev, skb, &hw)) {
  589. struct mt76_phy *phy = hw->priv;
  590. if (tx_info.skb == phy->test.tx_skb)
  591. phy->test.tx_done--;
  592. }
  593. #endif
  594. mt76_put_txwi(dev, t);
  595. free_skb:
  596. status.skb = tx_info.skb;
  597. hw = mt76_tx_status_get_hw(dev, tx_info.skb);
  598. spin_lock_bh(&dev->rx_lock);
  599. ieee80211_tx_status_ext(hw, &status);
  600. spin_unlock_bh(&dev->rx_lock);
  601. return ret;
  602. }
  603. static int
  604. mt76_dma_rx_fill_buf(struct mt76_dev *dev, struct mt76_queue *q,
  605. bool allow_direct)
  606. {
  607. int len = SKB_WITH_OVERHEAD(q->buf_size);
  608. int frames = 0;
  609. if (!q->ndesc)
  610. return 0;
  611. while (q->queued < q->ndesc - 1) {
  612. struct mt76_queue_buf qbuf = {};
  613. void *buf = NULL;
  614. int offset;
  615. if (mt76_queue_is_wed_rro_ind(q) ||
  616. mt76_queue_is_wed_rro_rxdmad_c(q))
  617. goto done;
  618. buf = mt76_get_page_pool_buf(q, &offset, q->buf_size);
  619. if (!buf)
  620. break;
  621. qbuf.addr = page_pool_get_dma_addr(virt_to_head_page(buf)) +
  622. offset + q->buf_offset;
  623. done:
  624. qbuf.len = len - q->buf_offset;
  625. qbuf.skip_unmap = false;
  626. if (mt76_dma_add_rx_buf(dev, q, &qbuf, buf) < 0) {
  627. mt76_put_page_pool_buf(buf, allow_direct);
  628. break;
  629. }
  630. frames++;
  631. }
  632. if (frames || mt76_queue_is_wed_rx(q))
  633. mt76_dma_kick_queue(dev, q);
  634. return frames;
  635. }
  636. int mt76_dma_rx_fill(struct mt76_dev *dev, struct mt76_queue *q,
  637. bool allow_direct)
  638. {
  639. int frames;
  640. spin_lock_bh(&q->lock);
  641. frames = mt76_dma_rx_fill_buf(dev, q, allow_direct);
  642. spin_unlock_bh(&q->lock);
  643. return frames;
  644. }
  645. static int
  646. mt76_dma_alloc_queue(struct mt76_dev *dev, struct mt76_queue *q,
  647. int idx, int n_desc, int bufsize,
  648. u32 ring_base)
  649. {
  650. int ret, size;
  651. spin_lock_init(&q->lock);
  652. spin_lock_init(&q->cleanup_lock);
  653. q->regs = dev->mmio.regs + ring_base + idx * MT_RING_SIZE;
  654. q->ndesc = n_desc;
  655. q->buf_size = bufsize;
  656. q->hw_idx = idx;
  657. q->dev = dev;
  658. if (mt76_queue_is_wed_rro_ind(q))
  659. size = sizeof(struct mt76_wed_rro_desc);
  660. else if (mt76_queue_is_npu_tx(q))
  661. size = sizeof(struct airoha_npu_tx_dma_desc);
  662. else if (mt76_queue_is_npu_rx(q))
  663. size = sizeof(struct airoha_npu_rx_dma_desc);
  664. else
  665. size = sizeof(struct mt76_desc);
  666. q->desc = dmam_alloc_coherent(dev->dma_dev, q->ndesc * size,
  667. &q->desc_dma, GFP_KERNEL);
  668. if (!q->desc)
  669. return -ENOMEM;
  670. mt76_dma_queue_magic_cnt_init(dev, q);
  671. size = q->ndesc * sizeof(*q->entry);
  672. q->entry = devm_kzalloc(dev->dev, size, GFP_KERNEL);
  673. if (!q->entry)
  674. return -ENOMEM;
  675. ret = mt76_create_page_pool(dev, q);
  676. if (ret)
  677. return ret;
  678. mt76_npu_queue_setup(dev, q);
  679. ret = mt76_wed_dma_setup(dev, q, false);
  680. if (ret)
  681. return ret;
  682. if (mtk_wed_device_active(&dev->mmio.wed)) {
  683. if ((mtk_wed_get_rx_capa(&dev->mmio.wed) && mt76_queue_is_wed_rro(q)) ||
  684. mt76_queue_is_wed_tx_free(q))
  685. return 0;
  686. }
  687. /* HW specific driver is supposed to reset brand-new EMI queues since
  688. * it needs to set cpu index pointer.
  689. */
  690. mt76_dma_queue_reset(dev, q, !mt76_queue_is_emi(q));
  691. return 0;
  692. }
  693. static void
  694. mt76_dma_rx_cleanup(struct mt76_dev *dev, struct mt76_queue *q)
  695. {
  696. void *buf;
  697. bool more;
  698. if (!q->ndesc)
  699. return;
  700. if (mt76_queue_is_npu(q)) {
  701. mt76_npu_queue_cleanup(dev, q);
  702. return;
  703. }
  704. do {
  705. spin_lock_bh(&q->lock);
  706. buf = mt76_dma_dequeue(dev, q, true, NULL, NULL, &more, NULL);
  707. spin_unlock_bh(&q->lock);
  708. if (!buf)
  709. break;
  710. if (!mt76_queue_is_wed_rro(q))
  711. mt76_put_page_pool_buf(buf, false);
  712. } while (1);
  713. spin_lock_bh(&q->lock);
  714. if (q->rx_head) {
  715. dev_kfree_skb(q->rx_head);
  716. q->rx_head = NULL;
  717. }
  718. spin_unlock_bh(&q->lock);
  719. }
  720. static void
  721. mt76_dma_rx_reset(struct mt76_dev *dev, enum mt76_rxq_id qid)
  722. {
  723. struct mt76_queue *q = &dev->q_rx[qid];
  724. if (!q->ndesc)
  725. return;
  726. if (!mt76_queue_is_wed_rro_ind(q) &&
  727. !mt76_queue_is_wed_rro_rxdmad_c(q) && !mt76_queue_is_npu(q)) {
  728. int i;
  729. for (i = 0; i < q->ndesc; i++)
  730. q->desc[i].ctrl = cpu_to_le32(MT_DMA_CTL_DMA_DONE);
  731. }
  732. mt76_dma_rx_cleanup(dev, q);
  733. /* reset WED rx queues */
  734. mt76_wed_dma_setup(dev, q, true);
  735. if (mt76_queue_is_wed_tx_free(q))
  736. return;
  737. if (mtk_wed_device_active(&dev->mmio.wed) &&
  738. mt76_queue_is_wed_rro(q))
  739. return;
  740. mt76_dma_sync_idx(dev, q);
  741. if (mt76_queue_is_npu(q))
  742. mt76_npu_fill_rx_queue(dev, q);
  743. else
  744. mt76_dma_rx_fill(dev, q, false);
  745. }
  746. static void
  747. mt76_add_fragment(struct mt76_dev *dev, struct mt76_queue *q, void *data,
  748. int len, bool more, u32 info, bool allow_direct)
  749. {
  750. struct sk_buff *skb = q->rx_head;
  751. struct skb_shared_info *shinfo = skb_shinfo(skb);
  752. int nr_frags = shinfo->nr_frags;
  753. if (nr_frags < ARRAY_SIZE(shinfo->frags)) {
  754. struct page *page = virt_to_head_page(data);
  755. int offset = data - page_address(page) + q->buf_offset;
  756. skb_add_rx_frag(skb, nr_frags, page, offset, len, q->buf_size);
  757. } else {
  758. mt76_put_page_pool_buf(data, allow_direct);
  759. }
  760. if (more)
  761. return;
  762. q->rx_head = NULL;
  763. if (nr_frags < ARRAY_SIZE(shinfo->frags))
  764. dev->drv->rx_skb(dev, q - dev->q_rx, skb, &info);
  765. else
  766. dev_kfree_skb(skb);
  767. }
  768. static int
  769. mt76_dma_rx_process(struct mt76_dev *dev, struct mt76_queue *q, int budget)
  770. {
  771. int len, data_len, done = 0, dma_idx;
  772. struct sk_buff *skb;
  773. unsigned char *data;
  774. bool check_ddone = false;
  775. bool allow_direct = !mt76_queue_is_wed_rx(q);
  776. bool more;
  777. if ((q->flags & MT_QFLAG_WED_RRO_EN) ||
  778. (IS_ENABLED(CONFIG_NET_MEDIATEK_SOC_WED) &&
  779. mt76_queue_is_wed_tx_free(q))) {
  780. dma_idx = Q_READ(q, dma_idx);
  781. check_ddone = true;
  782. }
  783. while (done < budget) {
  784. bool drop = false;
  785. u32 info;
  786. if (check_ddone) {
  787. if (q->tail == dma_idx)
  788. dma_idx = Q_READ(q, dma_idx);
  789. if (q->tail == dma_idx)
  790. break;
  791. }
  792. data = mt76_dma_dequeue(dev, q, false, &len, &info, &more,
  793. &drop);
  794. if (!data)
  795. break;
  796. if (PTR_ERR(data) == -EAGAIN) {
  797. done++;
  798. continue;
  799. }
  800. if (mt76_queue_is_wed_rro_ind(q) && dev->drv->rx_rro_ind_process)
  801. dev->drv->rx_rro_ind_process(dev, data);
  802. if (mt76_queue_is_wed_rro(q) &&
  803. !mt76_queue_is_wed_rro_rxdmad_c(q)) {
  804. done++;
  805. continue;
  806. }
  807. if (drop)
  808. goto free_frag;
  809. if (q->rx_head)
  810. data_len = q->buf_size;
  811. else
  812. data_len = SKB_WITH_OVERHEAD(q->buf_size);
  813. if (data_len < len + q->buf_offset) {
  814. dev_kfree_skb(q->rx_head);
  815. q->rx_head = NULL;
  816. goto free_frag;
  817. }
  818. if (q->rx_head) {
  819. mt76_add_fragment(dev, q, data, len, more, info,
  820. allow_direct);
  821. continue;
  822. }
  823. if (!more && dev->drv->rx_check &&
  824. !(dev->drv->rx_check(dev, data, len)))
  825. goto free_frag;
  826. skb = napi_build_skb(data, q->buf_size);
  827. if (!skb)
  828. goto free_frag;
  829. skb_reserve(skb, q->buf_offset);
  830. skb_mark_for_recycle(skb);
  831. *(u32 *)skb->cb = info;
  832. __skb_put(skb, len);
  833. done++;
  834. if (more) {
  835. q->rx_head = skb;
  836. continue;
  837. }
  838. dev->drv->rx_skb(dev, q - dev->q_rx, skb, &info);
  839. continue;
  840. free_frag:
  841. mt76_put_page_pool_buf(data, allow_direct);
  842. }
  843. mt76_dma_rx_fill(dev, q, true);
  844. return done;
  845. }
  846. int mt76_dma_rx_poll(struct napi_struct *napi, int budget)
  847. {
  848. struct mt76_dev *dev;
  849. int qid, done = 0, cur;
  850. dev = mt76_priv(napi->dev);
  851. qid = napi - dev->napi;
  852. rcu_read_lock();
  853. do {
  854. cur = mt76_dma_rx_process(dev, &dev->q_rx[qid], budget - done);
  855. mt76_rx_poll_complete(dev, qid, napi);
  856. done += cur;
  857. } while (cur && done < budget);
  858. rcu_read_unlock();
  859. if (done < budget && napi_complete(napi))
  860. dev->drv->rx_poll_complete(dev, qid);
  861. return done;
  862. }
  863. EXPORT_SYMBOL_GPL(mt76_dma_rx_poll);
  864. static void
  865. mt76_dma_rx_queue_init(struct mt76_dev *dev, enum mt76_rxq_id qid,
  866. int (*poll)(struct napi_struct *napi, int budget))
  867. {
  868. netif_napi_add(dev->napi_dev, &dev->napi[qid], poll);
  869. mt76_dma_rx_fill_buf(dev, &dev->q_rx[qid], false);
  870. napi_enable(&dev->napi[qid]);
  871. }
  872. static int
  873. mt76_dma_init(struct mt76_dev *dev,
  874. int (*poll)(struct napi_struct *napi, int budget))
  875. {
  876. struct mt76_dev **priv;
  877. int i;
  878. dev->napi_dev = alloc_netdev_dummy(sizeof(struct mt76_dev *));
  879. if (!dev->napi_dev)
  880. return -ENOMEM;
  881. /* napi_dev private data points to mt76_dev parent, so, mt76_dev
  882. * can be retrieved given napi_dev
  883. */
  884. priv = netdev_priv(dev->napi_dev);
  885. *priv = dev;
  886. dev->tx_napi_dev = alloc_netdev_dummy(sizeof(struct mt76_dev *));
  887. if (!dev->tx_napi_dev) {
  888. free_netdev(dev->napi_dev);
  889. return -ENOMEM;
  890. }
  891. priv = netdev_priv(dev->tx_napi_dev);
  892. *priv = dev;
  893. snprintf(dev->napi_dev->name, sizeof(dev->napi_dev->name), "%s",
  894. wiphy_name(dev->hw->wiphy));
  895. dev->napi_dev->threaded = 1;
  896. init_completion(&dev->mmio.wed_reset);
  897. init_completion(&dev->mmio.wed_reset_complete);
  898. mt76_for_each_q_rx(dev, i) {
  899. if (mt76_queue_is_wed_rro(&dev->q_rx[i]))
  900. continue;
  901. mt76_dma_rx_queue_init(dev, i, poll);
  902. }
  903. return 0;
  904. }
  905. static const struct mt76_queue_ops mt76_dma_ops = {
  906. .init = mt76_dma_init,
  907. .alloc = mt76_dma_alloc_queue,
  908. .reset_q = mt76_dma_queue_reset,
  909. .tx_queue_skb_raw = mt76_dma_tx_queue_skb_raw,
  910. .tx_queue_skb = mt76_dma_tx_queue_skb,
  911. .tx_cleanup = mt76_dma_tx_cleanup,
  912. .rx_queue_init = mt76_dma_rx_queue_init,
  913. .rx_cleanup = mt76_dma_rx_cleanup,
  914. .rx_reset = mt76_dma_rx_reset,
  915. .kick = mt76_dma_kick_queue,
  916. };
  917. void mt76_dma_attach(struct mt76_dev *dev)
  918. {
  919. dev->queue_ops = &mt76_dma_ops;
  920. }
  921. EXPORT_SYMBOL_GPL(mt76_dma_attach);
  922. void mt76_dma_cleanup(struct mt76_dev *dev)
  923. {
  924. int i;
  925. mt76_worker_disable(&dev->tx_worker);
  926. napi_disable(&dev->tx_napi);
  927. netif_napi_del(&dev->tx_napi);
  928. for (i = 0; i < ARRAY_SIZE(dev->phys); i++) {
  929. struct mt76_phy *phy = dev->phys[i];
  930. int j;
  931. if (!phy)
  932. continue;
  933. for (j = 0; j < ARRAY_SIZE(phy->q_tx); j++)
  934. mt76_dma_tx_cleanup(dev, phy->q_tx[j], true);
  935. }
  936. for (i = 0; i < ARRAY_SIZE(dev->q_mcu); i++)
  937. mt76_dma_tx_cleanup(dev, dev->q_mcu[i], true);
  938. mt76_for_each_q_rx(dev, i) {
  939. struct mt76_queue *q = &dev->q_rx[i];
  940. if (mtk_wed_device_active(&dev->mmio.wed) &&
  941. mt76_queue_is_wed_rro(q))
  942. continue;
  943. netif_napi_del(&dev->napi[i]);
  944. mt76_dma_rx_cleanup(dev, q);
  945. page_pool_destroy(q->page_pool);
  946. }
  947. if (mtk_wed_device_active(&dev->mmio.wed))
  948. mtk_wed_device_detach(&dev->mmio.wed);
  949. if (mtk_wed_device_active(&dev->mmio.wed_hif2))
  950. mtk_wed_device_detach(&dev->mmio.wed_hif2);
  951. mt76_free_pending_txwi(dev);
  952. mt76_free_pending_rxwi(dev);
  953. free_netdev(dev->napi_dev);
  954. free_netdev(dev->tx_napi_dev);
  955. }
  956. EXPORT_SYMBOL_GPL(mt76_dma_cleanup);