dma.c 22 KB

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