10e3d6777SRyder Lee // SPDX-License-Identifier: ISC
217f1de56SFelix Fietkau /*
317f1de56SFelix Fietkau * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
417f1de56SFelix Fietkau */
517f1de56SFelix Fietkau
617f1de56SFelix Fietkau #include <linux/dma-mapping.h>
717f1de56SFelix Fietkau #include "mt76.h"
817f1de56SFelix Fietkau #include "dma.h"
917f1de56SFelix Fietkau
10f68d6762SFelix Fietkau #if IS_ENABLED(CONFIG_NET_MEDIATEK_SOC_WED)
11f68d6762SFelix Fietkau
122e420b88SLorenzo Bianconi #define Q_READ(_q, _field) ({ \
13f68d6762SFelix Fietkau u32 _offset = offsetof(struct mt76_queue_regs, _field); \
14f68d6762SFelix Fietkau u32 _val; \
15f68d6762SFelix Fietkau if ((_q)->flags & MT_QFLAG_WED) \
162e420b88SLorenzo Bianconi _val = mtk_wed_device_reg_read((_q)->wed, \
17f68d6762SFelix Fietkau ((_q)->wed_regs + \
18f68d6762SFelix Fietkau _offset)); \
19f68d6762SFelix Fietkau else \
20f68d6762SFelix Fietkau _val = readl(&(_q)->regs->_field); \
21f68d6762SFelix Fietkau _val; \
22f68d6762SFelix Fietkau })
23f68d6762SFelix Fietkau
242e420b88SLorenzo Bianconi #define Q_WRITE(_q, _field, _val) do { \
25f68d6762SFelix Fietkau u32 _offset = offsetof(struct mt76_queue_regs, _field); \
26f68d6762SFelix Fietkau if ((_q)->flags & MT_QFLAG_WED) \
272e420b88SLorenzo Bianconi mtk_wed_device_reg_write((_q)->wed, \
28f68d6762SFelix Fietkau ((_q)->wed_regs + _offset), \
29f68d6762SFelix Fietkau _val); \
30f68d6762SFelix Fietkau else \
31f68d6762SFelix Fietkau writel(_val, &(_q)->regs->_field); \
32f68d6762SFelix Fietkau } while (0)
33f68d6762SFelix Fietkau
34f68d6762SFelix Fietkau #else
35f68d6762SFelix Fietkau
362e420b88SLorenzo Bianconi #define Q_READ(_q, _field) readl(&(_q)->regs->_field)
372e420b88SLorenzo Bianconi #define Q_WRITE(_q, _field, _val) writel(_val, &(_q)->regs->_field)
38cc9fd945SFelix Fietkau
39f68d6762SFelix Fietkau #endif
40cc9fd945SFelix Fietkau
41dd57a95cSFelix Fietkau static struct mt76_txwi_cache *
mt76_alloc_txwi(struct mt76_dev * dev)42dd57a95cSFelix Fietkau mt76_alloc_txwi(struct mt76_dev *dev)
43dd57a95cSFelix Fietkau {
44dd57a95cSFelix Fietkau struct mt76_txwi_cache *t;
45dd57a95cSFelix Fietkau dma_addr_t addr;
46dd57a95cSFelix Fietkau u8 *txwi;
47dd57a95cSFelix Fietkau int size;
48dd57a95cSFelix Fietkau
49dd57a95cSFelix Fietkau size = L1_CACHE_ALIGN(dev->drv->txwi_size + sizeof(*t));
50402e0109SFelix Fietkau txwi = kzalloc(size, GFP_ATOMIC);
51dd57a95cSFelix Fietkau if (!txwi)
52dd57a95cSFelix Fietkau return NULL;
53dd57a95cSFelix Fietkau
54d1ddc536SFelix Fietkau addr = dma_map_single(dev->dma_dev, txwi, dev->drv->txwi_size,
55dd57a95cSFelix Fietkau DMA_TO_DEVICE);
565d0e7ddeSDmitry Antipov if (unlikely(dma_mapping_error(dev->dma_dev, addr))) {
575d0e7ddeSDmitry Antipov kfree(txwi);
585d0e7ddeSDmitry Antipov return NULL;
595d0e7ddeSDmitry Antipov }
605d0e7ddeSDmitry Antipov
61dd57a95cSFelix Fietkau t = (struct mt76_txwi_cache *)(txwi + dev->drv->txwi_size);
62dd57a95cSFelix Fietkau t->dma_addr = addr;
63dd57a95cSFelix Fietkau
64dd57a95cSFelix Fietkau return t;
65dd57a95cSFelix Fietkau }
66dd57a95cSFelix Fietkau
67dd57a95cSFelix Fietkau static struct mt76_txwi_cache *
mt76_alloc_rxwi(struct mt76_dev * dev)682666beceSSujuan Chen mt76_alloc_rxwi(struct mt76_dev *dev)
692666beceSSujuan Chen {
702666beceSSujuan Chen struct mt76_txwi_cache *t;
712666beceSSujuan Chen
722666beceSSujuan Chen t = kzalloc(L1_CACHE_ALIGN(sizeof(*t)), GFP_ATOMIC);
732666beceSSujuan Chen if (!t)
742666beceSSujuan Chen return NULL;
752666beceSSujuan Chen
762666beceSSujuan Chen t->ptr = NULL;
772666beceSSujuan Chen return t;
782666beceSSujuan Chen }
792666beceSSujuan Chen
802666beceSSujuan Chen static struct mt76_txwi_cache *
__mt76_get_txwi(struct mt76_dev * dev)81dd57a95cSFelix Fietkau __mt76_get_txwi(struct mt76_dev *dev)
82dd57a95cSFelix Fietkau {
83dd57a95cSFelix Fietkau struct mt76_txwi_cache *t = NULL;
84dd57a95cSFelix Fietkau
85dd57a95cSFelix Fietkau spin_lock(&dev->lock);
86dd57a95cSFelix Fietkau if (!list_empty(&dev->txwi_cache)) {
87dd57a95cSFelix Fietkau t = list_first_entry(&dev->txwi_cache, struct mt76_txwi_cache,
88dd57a95cSFelix Fietkau list);
89dd57a95cSFelix Fietkau list_del(&t->list);
90dd57a95cSFelix Fietkau }
91dd57a95cSFelix Fietkau spin_unlock(&dev->lock);
92dd57a95cSFelix Fietkau
93dd57a95cSFelix Fietkau return t;
94dd57a95cSFelix Fietkau }
95dd57a95cSFelix Fietkau
96dd57a95cSFelix Fietkau static struct mt76_txwi_cache *
__mt76_get_rxwi(struct mt76_dev * dev)972666beceSSujuan Chen __mt76_get_rxwi(struct mt76_dev *dev)
982666beceSSujuan Chen {
992666beceSSujuan Chen struct mt76_txwi_cache *t = NULL;
1002666beceSSujuan Chen
10119527314SLorenzo Bianconi spin_lock_bh(&dev->wed_lock);
1022666beceSSujuan Chen if (!list_empty(&dev->rxwi_cache)) {
1032666beceSSujuan Chen t = list_first_entry(&dev->rxwi_cache, struct mt76_txwi_cache,
1042666beceSSujuan Chen list);
1052666beceSSujuan Chen list_del(&t->list);
1062666beceSSujuan Chen }
10719527314SLorenzo Bianconi spin_unlock_bh(&dev->wed_lock);
1082666beceSSujuan Chen
1092666beceSSujuan Chen return t;
1102666beceSSujuan Chen }
1112666beceSSujuan Chen
1122666beceSSujuan Chen static struct mt76_txwi_cache *
mt76_get_txwi(struct mt76_dev * dev)113dd57a95cSFelix Fietkau mt76_get_txwi(struct mt76_dev *dev)
114dd57a95cSFelix Fietkau {
115dd57a95cSFelix Fietkau struct mt76_txwi_cache *t = __mt76_get_txwi(dev);
116dd57a95cSFelix Fietkau
117dd57a95cSFelix Fietkau if (t)
118dd57a95cSFelix Fietkau return t;
119dd57a95cSFelix Fietkau
120dd57a95cSFelix Fietkau return mt76_alloc_txwi(dev);
121dd57a95cSFelix Fietkau }
122dd57a95cSFelix Fietkau
1232666beceSSujuan Chen struct mt76_txwi_cache *
mt76_get_rxwi(struct mt76_dev * dev)1242666beceSSujuan Chen mt76_get_rxwi(struct mt76_dev *dev)
1252666beceSSujuan Chen {
1262666beceSSujuan Chen struct mt76_txwi_cache *t = __mt76_get_rxwi(dev);
1272666beceSSujuan Chen
1282666beceSSujuan Chen if (t)
1292666beceSSujuan Chen return t;
1302666beceSSujuan Chen
1312666beceSSujuan Chen return mt76_alloc_rxwi(dev);
1322666beceSSujuan Chen }
1332666beceSSujuan Chen EXPORT_SYMBOL_GPL(mt76_get_rxwi);
1342666beceSSujuan Chen
135dd57a95cSFelix Fietkau void
mt76_put_txwi(struct mt76_dev * dev,struct mt76_txwi_cache * t)136dd57a95cSFelix Fietkau mt76_put_txwi(struct mt76_dev *dev, struct mt76_txwi_cache *t)
137dd57a95cSFelix Fietkau {
138dd57a95cSFelix Fietkau if (!t)
139dd57a95cSFelix Fietkau return;
140dd57a95cSFelix Fietkau
141dd57a95cSFelix Fietkau spin_lock(&dev->lock);
142dd57a95cSFelix Fietkau list_add(&t->list, &dev->txwi_cache);
143dd57a95cSFelix Fietkau spin_unlock(&dev->lock);
144dd57a95cSFelix Fietkau }
145dd57a95cSFelix Fietkau EXPORT_SYMBOL_GPL(mt76_put_txwi);
146dd57a95cSFelix Fietkau
1472666beceSSujuan Chen void
mt76_put_rxwi(struct mt76_dev * dev,struct mt76_txwi_cache * t)1482666beceSSujuan Chen mt76_put_rxwi(struct mt76_dev *dev, struct mt76_txwi_cache *t)
1492666beceSSujuan Chen {
1502666beceSSujuan Chen if (!t)
1512666beceSSujuan Chen return;
1522666beceSSujuan Chen
15319527314SLorenzo Bianconi spin_lock_bh(&dev->wed_lock);
1542666beceSSujuan Chen list_add(&t->list, &dev->rxwi_cache);
15519527314SLorenzo Bianconi spin_unlock_bh(&dev->wed_lock);
1562666beceSSujuan Chen }
1572666beceSSujuan Chen EXPORT_SYMBOL_GPL(mt76_put_rxwi);
1582666beceSSujuan Chen
159dd57a95cSFelix Fietkau static void
mt76_free_pending_txwi(struct mt76_dev * dev)160dd57a95cSFelix Fietkau mt76_free_pending_txwi(struct mt76_dev *dev)
161dd57a95cSFelix Fietkau {
162dd57a95cSFelix Fietkau struct mt76_txwi_cache *t;
163dd57a95cSFelix Fietkau
1645f0ce584SLorenzo Bianconi local_bh_disable();
165402e0109SFelix Fietkau while ((t = __mt76_get_txwi(dev)) != NULL) {
166d1ddc536SFelix Fietkau dma_unmap_single(dev->dma_dev, t->dma_addr, dev->drv->txwi_size,
167dd57a95cSFelix Fietkau DMA_TO_DEVICE);
168402e0109SFelix Fietkau kfree(mt76_get_txwi_ptr(dev, t));
169402e0109SFelix Fietkau }
1705f0ce584SLorenzo Bianconi local_bh_enable();
171dd57a95cSFelix Fietkau }
172dd57a95cSFelix Fietkau
173a97a467aSSujuan Chen void
mt76_free_pending_rxwi(struct mt76_dev * dev)1742666beceSSujuan Chen mt76_free_pending_rxwi(struct mt76_dev *dev)
1752666beceSSujuan Chen {
1762666beceSSujuan Chen struct mt76_txwi_cache *t;
1772666beceSSujuan Chen
1782666beceSSujuan Chen local_bh_disable();
1792666beceSSujuan Chen while ((t = __mt76_get_rxwi(dev)) != NULL) {
1802666beceSSujuan Chen if (t->ptr)
1812f5c3c77SLorenzo Bianconi mt76_put_page_pool_buf(t->ptr, false);
1822666beceSSujuan Chen kfree(t);
1832666beceSSujuan Chen }
1842666beceSSujuan Chen local_bh_enable();
1852666beceSSujuan Chen }
186a97a467aSSujuan Chen EXPORT_SYMBOL_GPL(mt76_free_pending_rxwi);
1872666beceSSujuan Chen
1882666beceSSujuan Chen static void
mt76_dma_sync_idx(struct mt76_dev * dev,struct mt76_queue * q)1893990465dSLorenzo Bianconi mt76_dma_sync_idx(struct mt76_dev *dev, struct mt76_queue *q)
1903990465dSLorenzo Bianconi {
1912e420b88SLorenzo Bianconi Q_WRITE(q, desc_base, q->desc_dma);
192950d0abbSBo Jiao if (q->flags & MT_QFLAG_WED_RRO_EN)
193950d0abbSBo Jiao Q_WRITE(q, ring_size, MT_DMA_RRO_EN | q->ndesc);
194950d0abbSBo Jiao else
1952e420b88SLorenzo Bianconi Q_WRITE(q, ring_size, q->ndesc);
1962e420b88SLorenzo Bianconi q->head = Q_READ(q, dma_idx);
1973990465dSLorenzo Bianconi q->tail = q->head;
1983990465dSLorenzo Bianconi }
1993990465dSLorenzo Bianconi
__mt76_dma_queue_reset(struct mt76_dev * dev,struct mt76_queue * q,bool reset_idx)2008a7386e7SLorenzo Bianconi void __mt76_dma_queue_reset(struct mt76_dev *dev, struct mt76_queue *q,
2015bb7a655SLorenzo Bianconi bool reset_idx)
2023990465dSLorenzo Bianconi {
203f9b627f1SBo Jiao if (!q || !q->ndesc)
2043990465dSLorenzo Bianconi return;
2053990465dSLorenzo Bianconi
206950d0abbSBo Jiao if (!mt76_queue_is_wed_rro_ind(q)) {
207950d0abbSBo Jiao int i;
208950d0abbSBo Jiao
2093990465dSLorenzo Bianconi /* clear descriptors */
2103990465dSLorenzo Bianconi for (i = 0; i < q->ndesc; i++)
2113990465dSLorenzo Bianconi q->desc[i].ctrl = cpu_to_le32(MT_DMA_CTL_DMA_DONE);
212950d0abbSBo Jiao }
2133990465dSLorenzo Bianconi
2145bb7a655SLorenzo Bianconi if (reset_idx) {
2152e420b88SLorenzo Bianconi Q_WRITE(q, cpu_idx, 0);
2162e420b88SLorenzo Bianconi Q_WRITE(q, dma_idx, 0);
2175bb7a655SLorenzo Bianconi }
2183990465dSLorenzo Bianconi mt76_dma_sync_idx(dev, q);
2193990465dSLorenzo Bianconi }
2203990465dSLorenzo Bianconi
mt76_dma_queue_reset(struct mt76_dev * dev,struct mt76_queue * q)2218a7386e7SLorenzo Bianconi void mt76_dma_queue_reset(struct mt76_dev *dev, struct mt76_queue *q)
2225bb7a655SLorenzo Bianconi {
2235bb7a655SLorenzo Bianconi __mt76_dma_queue_reset(dev, q, true);
2245bb7a655SLorenzo Bianconi }
2255bb7a655SLorenzo Bianconi
22617f1de56SFelix Fietkau static int
mt76_dma_add_rx_buf(struct mt76_dev * dev,struct mt76_queue * q,struct mt76_queue_buf * buf,void * data)227953519b3SFelix Fietkau mt76_dma_add_rx_buf(struct mt76_dev *dev, struct mt76_queue *q,
228953519b3SFelix Fietkau struct mt76_queue_buf *buf, void *data)
229953519b3SFelix Fietkau {
230953519b3SFelix Fietkau struct mt76_queue_entry *entry = &q->entry[q->head];
231953519b3SFelix Fietkau struct mt76_txwi_cache *txwi = NULL;
232950d0abbSBo Jiao struct mt76_desc *desc;
233953519b3SFelix Fietkau int idx = q->head;
2344920a3a1SSujuan Chen u32 buf1 = 0, ctrl;
235953519b3SFelix Fietkau int rx_token;
236953519b3SFelix Fietkau
237950d0abbSBo Jiao if (mt76_queue_is_wed_rro_ind(q)) {
238950d0abbSBo Jiao struct mt76_wed_rro_desc *rro_desc;
239950d0abbSBo Jiao
240950d0abbSBo Jiao rro_desc = (struct mt76_wed_rro_desc *)q->desc;
241950d0abbSBo Jiao data = &rro_desc[q->head];
242950d0abbSBo Jiao goto done;
243950d0abbSBo Jiao }
244950d0abbSBo Jiao
245950d0abbSBo Jiao desc = &q->desc[q->head];
246953519b3SFelix Fietkau ctrl = FIELD_PREP(MT_DMA_CTL_SD_LEN0, buf[0].len);
2474920a3a1SSujuan Chen #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
2484920a3a1SSujuan Chen buf1 = FIELD_PREP(MT_DMA_CTL_SDP0_H, buf->addr >> 32);
2494920a3a1SSujuan Chen #endif
250953519b3SFelix Fietkau
25158bcd4edSLorenzo Bianconi if (mt76_queue_is_wed_rx(q)) {
252953519b3SFelix Fietkau txwi = mt76_get_rxwi(dev);
253953519b3SFelix Fietkau if (!txwi)
254953519b3SFelix Fietkau return -ENOMEM;
255953519b3SFelix Fietkau
256953519b3SFelix Fietkau rx_token = mt76_rx_token_consume(dev, data, txwi, buf->addr);
257953519b3SFelix Fietkau if (rx_token < 0) {
258953519b3SFelix Fietkau mt76_put_rxwi(dev, txwi);
259953519b3SFelix Fietkau return -ENOMEM;
260953519b3SFelix Fietkau }
261953519b3SFelix Fietkau
262953519b3SFelix Fietkau buf1 |= FIELD_PREP(MT_DMA_CTL_TOKEN, rx_token);
263953519b3SFelix Fietkau ctrl |= MT_DMA_CTL_TO_HOST;
264953519b3SFelix Fietkau }
265953519b3SFelix Fietkau
266953519b3SFelix Fietkau WRITE_ONCE(desc->buf0, cpu_to_le32(buf->addr));
267953519b3SFelix Fietkau WRITE_ONCE(desc->buf1, cpu_to_le32(buf1));
268953519b3SFelix Fietkau WRITE_ONCE(desc->ctrl, cpu_to_le32(ctrl));
269953519b3SFelix Fietkau WRITE_ONCE(desc->info, 0);
270953519b3SFelix Fietkau
271950d0abbSBo Jiao done:
272953519b3SFelix Fietkau entry->dma_addr[0] = buf->addr;
273953519b3SFelix Fietkau entry->dma_len[0] = buf->len;
274953519b3SFelix Fietkau entry->txwi = txwi;
275953519b3SFelix Fietkau entry->buf = data;
276953519b3SFelix Fietkau entry->wcid = 0xffff;
277953519b3SFelix Fietkau entry->skip_buf1 = true;
278953519b3SFelix Fietkau q->head = (q->head + 1) % q->ndesc;
279953519b3SFelix Fietkau q->queued++;
280953519b3SFelix Fietkau
281953519b3SFelix Fietkau return idx;
282953519b3SFelix Fietkau }
283953519b3SFelix Fietkau
284953519b3SFelix Fietkau static int
mt76_dma_add_buf(struct mt76_dev * dev,struct mt76_queue * q,struct mt76_queue_buf * buf,int nbufs,u32 info,struct sk_buff * skb,void * txwi)28517f1de56SFelix Fietkau mt76_dma_add_buf(struct mt76_dev *dev, struct mt76_queue *q,
28617f1de56SFelix Fietkau struct mt76_queue_buf *buf, int nbufs, u32 info,
28717f1de56SFelix Fietkau struct sk_buff *skb, void *txwi)
28817f1de56SFelix Fietkau {
28975d4bf1fSFelix Fietkau struct mt76_queue_entry *entry;
29017f1de56SFelix Fietkau struct mt76_desc *desc;
29117f1de56SFelix Fietkau int i, idx = -1;
292fe13dad8SLorenzo Bianconi u32 ctrl, next;
29317f1de56SFelix Fietkau
294953519b3SFelix Fietkau if (txwi) {
295953519b3SFelix Fietkau q->entry[q->head].txwi = DMA_DUMMY_DATA;
296953519b3SFelix Fietkau q->entry[q->head].skip_buf0 = true;
297953519b3SFelix Fietkau }
298953519b3SFelix Fietkau
29917f1de56SFelix Fietkau for (i = 0; i < nbufs; i += 2, buf += 2) {
30017f1de56SFelix Fietkau u32 buf0 = buf[0].addr, buf1 = 0;
30117f1de56SFelix Fietkau
30275d4bf1fSFelix Fietkau idx = q->head;
303fe13dad8SLorenzo Bianconi next = (q->head + 1) % q->ndesc;
30475d4bf1fSFelix Fietkau
30575d4bf1fSFelix Fietkau desc = &q->desc[idx];
30675d4bf1fSFelix Fietkau entry = &q->entry[idx];
30775d4bf1fSFelix Fietkau
30827d5c528SFelix Fietkau if (buf[0].skip_unmap)
30975d4bf1fSFelix Fietkau entry->skip_buf0 = true;
31075d4bf1fSFelix Fietkau entry->skip_buf1 = i == nbufs - 1;
31175d4bf1fSFelix Fietkau
31275d4bf1fSFelix Fietkau entry->dma_addr[0] = buf[0].addr;
31375d4bf1fSFelix Fietkau entry->dma_len[0] = buf[0].len;
31427d5c528SFelix Fietkau
31517f1de56SFelix Fietkau ctrl = FIELD_PREP(MT_DMA_CTL_SD_LEN0, buf[0].len);
3164920a3a1SSujuan Chen #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
3174920a3a1SSujuan Chen info |= FIELD_PREP(MT_DMA_CTL_SDP0_H, buf[0].addr >> 32);
3184920a3a1SSujuan Chen #endif
31917f1de56SFelix Fietkau if (i < nbufs - 1) {
32075d4bf1fSFelix Fietkau entry->dma_addr[1] = buf[1].addr;
32175d4bf1fSFelix Fietkau entry->dma_len[1] = buf[1].len;
32217f1de56SFelix Fietkau buf1 = buf[1].addr;
32317f1de56SFelix Fietkau ctrl |= FIELD_PREP(MT_DMA_CTL_SD_LEN1, buf[1].len);
3244920a3a1SSujuan Chen #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
3254920a3a1SSujuan Chen info |= FIELD_PREP(MT_DMA_CTL_SDP1_H,
3264920a3a1SSujuan Chen buf[1].addr >> 32);
3274920a3a1SSujuan Chen #endif
32827d5c528SFelix Fietkau if (buf[1].skip_unmap)
32975d4bf1fSFelix Fietkau entry->skip_buf1 = true;
33017f1de56SFelix Fietkau }
33117f1de56SFelix Fietkau
33217f1de56SFelix Fietkau if (i == nbufs - 1)
33317f1de56SFelix Fietkau ctrl |= MT_DMA_CTL_LAST_SEC0;
33417f1de56SFelix Fietkau else if (i == nbufs - 2)
33517f1de56SFelix Fietkau ctrl |= MT_DMA_CTL_LAST_SEC1;
33617f1de56SFelix Fietkau
33717f1de56SFelix Fietkau WRITE_ONCE(desc->buf0, cpu_to_le32(buf0));
33817f1de56SFelix Fietkau WRITE_ONCE(desc->buf1, cpu_to_le32(buf1));
33917f1de56SFelix Fietkau WRITE_ONCE(desc->info, cpu_to_le32(info));
34017f1de56SFelix Fietkau WRITE_ONCE(desc->ctrl, cpu_to_le32(ctrl));
34117f1de56SFelix Fietkau
342fe13dad8SLorenzo Bianconi q->head = next;
34317f1de56SFelix Fietkau q->queued++;
34417f1de56SFelix Fietkau }
34517f1de56SFelix Fietkau
34617f1de56SFelix Fietkau q->entry[idx].txwi = txwi;
34717f1de56SFelix Fietkau q->entry[idx].skb = skb;
3486d51cae2SFelix Fietkau q->entry[idx].wcid = 0xffff;
34917f1de56SFelix Fietkau
35017f1de56SFelix Fietkau return idx;
35117f1de56SFelix Fietkau }
35217f1de56SFelix Fietkau
35317f1de56SFelix Fietkau static void
mt76_dma_tx_cleanup_idx(struct mt76_dev * dev,struct mt76_queue * q,int idx,struct mt76_queue_entry * prev_e)35417f1de56SFelix Fietkau mt76_dma_tx_cleanup_idx(struct mt76_dev *dev, struct mt76_queue *q, int idx,
35517f1de56SFelix Fietkau struct mt76_queue_entry *prev_e)
35617f1de56SFelix Fietkau {
35717f1de56SFelix Fietkau struct mt76_queue_entry *e = &q->entry[idx];
35817f1de56SFelix Fietkau
35975d4bf1fSFelix Fietkau if (!e->skip_buf0)
360d1ddc536SFelix Fietkau dma_unmap_single(dev->dma_dev, e->dma_addr[0], e->dma_len[0],
36117f1de56SFelix Fietkau DMA_TO_DEVICE);
36217f1de56SFelix Fietkau
36375d4bf1fSFelix Fietkau if (!e->skip_buf1)
364d1ddc536SFelix Fietkau dma_unmap_single(dev->dma_dev, e->dma_addr[1], e->dma_len[1],
36517f1de56SFelix Fietkau DMA_TO_DEVICE);
36617f1de56SFelix Fietkau
367598da386SLorenzo Bianconi if (e->txwi == DMA_DUMMY_DATA)
36817f1de56SFelix Fietkau e->txwi = NULL;
36917f1de56SFelix Fietkau
37017f1de56SFelix Fietkau *prev_e = *e;
37117f1de56SFelix Fietkau memset(e, 0, sizeof(*e));
37217f1de56SFelix Fietkau }
37317f1de56SFelix Fietkau
37417f1de56SFelix Fietkau static void
mt76_dma_kick_queue(struct mt76_dev * dev,struct mt76_queue * q)3758f6c4f7bSFelix Fietkau mt76_dma_kick_queue(struct mt76_dev *dev, struct mt76_queue *q)
3768f6c4f7bSFelix Fietkau {
3772d681047SFelix Fietkau wmb();
3782e420b88SLorenzo Bianconi Q_WRITE(q, cpu_idx, q->head);
37917f1de56SFelix Fietkau }
38017f1de56SFelix Fietkau
38117f1de56SFelix Fietkau static void
mt76_dma_tx_cleanup(struct mt76_dev * dev,struct mt76_queue * q,bool flush)382e5655492SLorenzo Bianconi mt76_dma_tx_cleanup(struct mt76_dev *dev, struct mt76_queue *q, bool flush)
38317f1de56SFelix Fietkau {
38417f1de56SFelix Fietkau struct mt76_queue_entry entry;
3850b51f186SFelix Fietkau int last;
38617f1de56SFelix Fietkau
387f9b627f1SBo Jiao if (!q || !q->ndesc)
38817f1de56SFelix Fietkau return;
38917f1de56SFelix Fietkau
3909716ef04SFelix Fietkau spin_lock_bh(&q->cleanup_lock);
39117f1de56SFelix Fietkau if (flush)
39217f1de56SFelix Fietkau last = -1;
39317f1de56SFelix Fietkau else
3942e420b88SLorenzo Bianconi last = Q_READ(q, dma_idx);
39517f1de56SFelix Fietkau
3960b51f186SFelix Fietkau while (q->queued > 0 && q->tail != last) {
39717f1de56SFelix Fietkau mt76_dma_tx_cleanup_idx(dev, q, q->tail, &entry);
398fe5b5ab5SFelix Fietkau mt76_queue_tx_complete(dev, q, &entry);
39917f1de56SFelix Fietkau
40017f1de56SFelix Fietkau if (entry.txwi) {
4019ec0b821SFelix Fietkau if (!(dev->drv->drv_flags & MT_DRV_TXWI_NO_FREE))
40217f1de56SFelix Fietkau mt76_put_txwi(dev, entry.txwi);
40317f1de56SFelix Fietkau }
40417f1de56SFelix Fietkau
40517f1de56SFelix Fietkau if (!flush && q->tail == last)
4062e420b88SLorenzo Bianconi last = Q_READ(q, dma_idx);
4075a95ca41SFelix Fietkau }
4089716ef04SFelix Fietkau spin_unlock_bh(&q->cleanup_lock);
4095a95ca41SFelix Fietkau
4108f6c4f7bSFelix Fietkau if (flush) {
4110b51f186SFelix Fietkau spin_lock_bh(&q->lock);
41217f1de56SFelix Fietkau mt76_dma_sync_idx(dev, q);
4138f6c4f7bSFelix Fietkau mt76_dma_kick_queue(dev, q);
4140b51f186SFelix Fietkau spin_unlock_bh(&q->lock);
4158f6c4f7bSFelix Fietkau }
41617f1de56SFelix Fietkau
41726e40d4cSFelix Fietkau if (!q->queued)
41826e40d4cSFelix Fietkau wake_up(&dev->tx_wait);
41917f1de56SFelix Fietkau }
42017f1de56SFelix Fietkau
42117f1de56SFelix Fietkau static void *
mt76_dma_get_buf(struct mt76_dev * dev,struct mt76_queue * q,int idx,int * len,u32 * info,bool * more,bool * drop)42217f1de56SFelix Fietkau mt76_dma_get_buf(struct mt76_dev *dev, struct mt76_queue *q, int idx,
423cd372b8cSLorenzo Bianconi int *len, u32 *info, bool *more, bool *drop)
42417f1de56SFelix Fietkau {
42517f1de56SFelix Fietkau struct mt76_queue_entry *e = &q->entry[idx];
42617f1de56SFelix Fietkau struct mt76_desc *desc = &q->desc[idx];
427950d0abbSBo Jiao u32 ctrl, desc_info, buf1;
428950d0abbSBo Jiao void *buf = e->buf;
42917f1de56SFelix Fietkau
430950d0abbSBo Jiao if (mt76_queue_is_wed_rro_ind(q))
431950d0abbSBo Jiao goto done;
432950d0abbSBo Jiao
433950d0abbSBo Jiao ctrl = le32_to_cpu(READ_ONCE(desc->ctrl));
43417f1de56SFelix Fietkau if (len) {
435cd372b8cSLorenzo Bianconi *len = FIELD_GET(MT_DMA_CTL_SD_LEN0, ctrl);
436cd372b8cSLorenzo Bianconi *more = !(ctrl & MT_DMA_CTL_LAST_SEC0);
43717f1de56SFelix Fietkau }
43817f1de56SFelix Fietkau
439950d0abbSBo Jiao desc_info = le32_to_cpu(desc->info);
44017f1de56SFelix Fietkau if (info)
441950d0abbSBo Jiao *info = desc_info;
442950d0abbSBo Jiao
443950d0abbSBo Jiao buf1 = le32_to_cpu(desc->buf1);
444950d0abbSBo Jiao mt76_dma_should_drop_buf(drop, ctrl, buf1, desc_info);
44517f1de56SFelix Fietkau
44658bcd4edSLorenzo Bianconi if (mt76_queue_is_wed_rx(q)) {
447e4d2b8bcSPeter Chiu u32 token = FIELD_GET(MT_DMA_CTL_TOKEN, buf1);
448cd372b8cSLorenzo Bianconi struct mt76_txwi_cache *t = mt76_rx_token_release(dev, token);
449cd372b8cSLorenzo Bianconi
450cd372b8cSLorenzo Bianconi if (!t)
451cd372b8cSLorenzo Bianconi return NULL;
452cd372b8cSLorenzo Bianconi
4532f5c3c77SLorenzo Bianconi dma_sync_single_for_cpu(dev->dma_dev, t->dma_addr,
454cd372b8cSLorenzo Bianconi SKB_WITH_OVERHEAD(q->buf_size),
4552f5c3c77SLorenzo Bianconi page_pool_get_dma_dir(q->page_pool));
456cd372b8cSLorenzo Bianconi
457cd372b8cSLorenzo Bianconi buf = t->ptr;
458cd372b8cSLorenzo Bianconi t->dma_addr = 0;
459cd372b8cSLorenzo Bianconi t->ptr = NULL;
460cd372b8cSLorenzo Bianconi
461cd372b8cSLorenzo Bianconi mt76_put_rxwi(dev, t);
462950d0abbSBo Jiao if (drop)
463e4d2b8bcSPeter Chiu *drop |= !!(buf1 & MT_DMA_CTL_WO_DROP);
464cd372b8cSLorenzo Bianconi } else {
4652f5c3c77SLorenzo Bianconi dma_sync_single_for_cpu(dev->dma_dev, e->dma_addr[0],
466cd372b8cSLorenzo Bianconi SKB_WITH_OVERHEAD(q->buf_size),
4672f5c3c77SLorenzo Bianconi page_pool_get_dma_dir(q->page_pool));
468cd372b8cSLorenzo Bianconi }
46917f1de56SFelix Fietkau
470950d0abbSBo Jiao done:
471950d0abbSBo Jiao e->buf = NULL;
47217f1de56SFelix Fietkau return buf;
47317f1de56SFelix Fietkau }
47417f1de56SFelix Fietkau
47517f1de56SFelix Fietkau static void *
mt76_dma_dequeue(struct mt76_dev * dev,struct mt76_queue * q,bool flush,int * len,u32 * info,bool * more,bool * drop)47617f1de56SFelix Fietkau mt76_dma_dequeue(struct mt76_dev *dev, struct mt76_queue *q, bool flush,
477cd372b8cSLorenzo Bianconi int *len, u32 *info, bool *more, bool *drop)
47817f1de56SFelix Fietkau {
47917f1de56SFelix Fietkau int idx = q->tail;
48017f1de56SFelix Fietkau
48117f1de56SFelix Fietkau *more = false;
48217f1de56SFelix Fietkau if (!q->queued)
48317f1de56SFelix Fietkau return NULL;
48417f1de56SFelix Fietkau
485950d0abbSBo Jiao if (mt76_queue_is_wed_rro_data(q))
486950d0abbSBo Jiao return NULL;
487950d0abbSBo Jiao
488950d0abbSBo Jiao if (!mt76_queue_is_wed_rro_ind(q)) {
4895ffc6b5aSFelix Fietkau if (flush)
4905ffc6b5aSFelix Fietkau q->desc[idx].ctrl |= cpu_to_le32(MT_DMA_CTL_DMA_DONE);
4915ffc6b5aSFelix Fietkau else if (!(q->desc[idx].ctrl & cpu_to_le32(MT_DMA_CTL_DMA_DONE)))
49217f1de56SFelix Fietkau return NULL;
493950d0abbSBo Jiao }
49417f1de56SFelix Fietkau
49517f1de56SFelix Fietkau q->tail = (q->tail + 1) % q->ndesc;
49617f1de56SFelix Fietkau q->queued--;
49717f1de56SFelix Fietkau
498cd372b8cSLorenzo Bianconi return mt76_dma_get_buf(dev, q, idx, len, info, more, drop);
49917f1de56SFelix Fietkau }
50017f1de56SFelix Fietkau
5015ed31128SLorenzo Bianconi static int
mt76_dma_tx_queue_skb_raw(struct mt76_dev * dev,struct mt76_queue * q,struct sk_buff * skb,u32 tx_info)502d95093a1SLorenzo Bianconi mt76_dma_tx_queue_skb_raw(struct mt76_dev *dev, struct mt76_queue *q,
5035ed31128SLorenzo Bianconi struct sk_buff *skb, u32 tx_info)
5045ed31128SLorenzo Bianconi {
505b4403ceeSFelix Fietkau struct mt76_queue_buf buf = {};
5065ed31128SLorenzo Bianconi dma_addr_t addr;
5075ed31128SLorenzo Bianconi
5081e64fdd4SBo Jiao if (test_bit(MT76_MCU_RESET, &dev->phy.state))
5091e64fdd4SBo Jiao goto error;
5101e64fdd4SBo Jiao
51193eaec76SFelix Fietkau if (q->queued + 1 >= q->ndesc - 1)
51293eaec76SFelix Fietkau goto error;
51393eaec76SFelix Fietkau
514d1ddc536SFelix Fietkau addr = dma_map_single(dev->dma_dev, skb->data, skb->len,
5155ed31128SLorenzo Bianconi DMA_TO_DEVICE);
516d1ddc536SFelix Fietkau if (unlikely(dma_mapping_error(dev->dma_dev, addr)))
51793eaec76SFelix Fietkau goto error;
5185ed31128SLorenzo Bianconi
5195ed31128SLorenzo Bianconi buf.addr = addr;
5205ed31128SLorenzo Bianconi buf.len = skb->len;
5215ed31128SLorenzo Bianconi
5225ed31128SLorenzo Bianconi spin_lock_bh(&q->lock);
5235ed31128SLorenzo Bianconi mt76_dma_add_buf(dev, q, &buf, 1, tx_info, skb, NULL);
5245ed31128SLorenzo Bianconi mt76_dma_kick_queue(dev, q);
5255ed31128SLorenzo Bianconi spin_unlock_bh(&q->lock);
5265ed31128SLorenzo Bianconi
5275ed31128SLorenzo Bianconi return 0;
52893eaec76SFelix Fietkau
52993eaec76SFelix Fietkau error:
53093eaec76SFelix Fietkau dev_kfree_skb(skb);
53193eaec76SFelix Fietkau return -ENOMEM;
5325ed31128SLorenzo Bianconi }
5335ed31128SLorenzo Bianconi
534eb9ca7ecSLorenzo Bianconi static int
mt76_dma_tx_queue_skb(struct mt76_phy * phy,struct mt76_queue * q,enum mt76_txq_id qid,struct sk_buff * skb,struct mt76_wcid * wcid,struct ieee80211_sta * sta)5355d581c33SFelix Fietkau mt76_dma_tx_queue_skb(struct mt76_phy *phy, struct mt76_queue *q,
536d08295f5SFelix Fietkau enum mt76_txq_id qid, struct sk_buff *skb,
537d08295f5SFelix Fietkau struct mt76_wcid *wcid, struct ieee80211_sta *sta)
538fcdd99ceSLorenzo Bianconi {
53994e4f579SFelix Fietkau struct ieee80211_tx_status status = {
54094e4f579SFelix Fietkau .sta = sta,
54194e4f579SFelix Fietkau };
542cfaae9e6SLorenzo Bianconi struct mt76_tx_info tx_info = {
543cfaae9e6SLorenzo Bianconi .skb = skb,
544cfaae9e6SLorenzo Bianconi };
5455d581c33SFelix Fietkau struct mt76_dev *dev = phy->dev;
546e394b575SFelix Fietkau struct ieee80211_hw *hw;
547b5903c47SLorenzo Bianconi int len, n = 0, ret = -ENOMEM;
548fcdd99ceSLorenzo Bianconi struct mt76_txwi_cache *t;
549fcdd99ceSLorenzo Bianconi struct sk_buff *iter;
550fcdd99ceSLorenzo Bianconi dma_addr_t addr;
551f3950a41SLorenzo Bianconi u8 *txwi;
552fcdd99ceSLorenzo Bianconi
5535d581c33SFelix Fietkau if (test_bit(MT76_RESET, &phy->state))
5541e64fdd4SBo Jiao goto free_skb;
5551e64fdd4SBo Jiao
556fcdd99ceSLorenzo Bianconi t = mt76_get_txwi(dev);
55794e4f579SFelix Fietkau if (!t)
55894e4f579SFelix Fietkau goto free_skb;
55994e4f579SFelix Fietkau
560f3950a41SLorenzo Bianconi txwi = mt76_get_txwi_ptr(dev, t);
561fcdd99ceSLorenzo Bianconi
56288046b2cSFelix Fietkau skb->prev = skb->next = NULL;
5639ec0b821SFelix Fietkau if (dev->drv->drv_flags & MT_DRV_TX_ALIGNED4_SKBS)
56466105538SLorenzo Bianconi mt76_insert_hdr_pad(skb);
56566105538SLorenzo Bianconi
566eb071ba7SLorenzo Bianconi len = skb_headlen(skb);
567d1ddc536SFelix Fietkau addr = dma_map_single(dev->dma_dev, skb->data, len, DMA_TO_DEVICE);
568d1ddc536SFelix Fietkau if (unlikely(dma_mapping_error(dev->dma_dev, addr)))
569fcdd99ceSLorenzo Bianconi goto free;
570fcdd99ceSLorenzo Bianconi
571b5903c47SLorenzo Bianconi tx_info.buf[n].addr = t->dma_addr;
572b5903c47SLorenzo Bianconi tx_info.buf[n++].len = dev->drv->txwi_size;
573b5903c47SLorenzo Bianconi tx_info.buf[n].addr = addr;
574b5903c47SLorenzo Bianconi tx_info.buf[n++].len = len;
575fcdd99ceSLorenzo Bianconi
576fcdd99ceSLorenzo Bianconi skb_walk_frags(skb, iter) {
577b5903c47SLorenzo Bianconi if (n == ARRAY_SIZE(tx_info.buf))
578fcdd99ceSLorenzo Bianconi goto unmap;
579fcdd99ceSLorenzo Bianconi
580d1ddc536SFelix Fietkau addr = dma_map_single(dev->dma_dev, iter->data, iter->len,
581fcdd99ceSLorenzo Bianconi DMA_TO_DEVICE);
582d1ddc536SFelix Fietkau if (unlikely(dma_mapping_error(dev->dma_dev, addr)))
583fcdd99ceSLorenzo Bianconi goto unmap;
584fcdd99ceSLorenzo Bianconi
585b5903c47SLorenzo Bianconi tx_info.buf[n].addr = addr;
586b5903c47SLorenzo Bianconi tx_info.buf[n++].len = iter->len;
587fcdd99ceSLorenzo Bianconi }
588b5903c47SLorenzo Bianconi tx_info.nbuf = n;
589fcdd99ceSLorenzo Bianconi
590ae064fc0SFelix Fietkau if (q->queued + (tx_info.nbuf + 1) / 2 >= q->ndesc - 1) {
591ae064fc0SFelix Fietkau ret = -ENOMEM;
592ae064fc0SFelix Fietkau goto unmap;
593ae064fc0SFelix Fietkau }
594ae064fc0SFelix Fietkau
595d1ddc536SFelix Fietkau dma_sync_single_for_cpu(dev->dma_dev, t->dma_addr, dev->drv->txwi_size,
596eb071ba7SLorenzo Bianconi DMA_TO_DEVICE);
597d08295f5SFelix Fietkau ret = dev->drv->tx_prepare_skb(dev, txwi, qid, wcid, sta, &tx_info);
598d1ddc536SFelix Fietkau dma_sync_single_for_device(dev->dma_dev, t->dma_addr, dev->drv->txwi_size,
599eb071ba7SLorenzo Bianconi DMA_TO_DEVICE);
600eb071ba7SLorenzo Bianconi if (ret < 0)
601fcdd99ceSLorenzo Bianconi goto unmap;
602fcdd99ceSLorenzo Bianconi
603b5903c47SLorenzo Bianconi return mt76_dma_add_buf(dev, q, tx_info.buf, tx_info.nbuf,
604cfaae9e6SLorenzo Bianconi tx_info.info, tx_info.skb, t);
605fcdd99ceSLorenzo Bianconi
606fcdd99ceSLorenzo Bianconi unmap:
607fcdd99ceSLorenzo Bianconi for (n--; n > 0; n--)
608d1ddc536SFelix Fietkau dma_unmap_single(dev->dma_dev, tx_info.buf[n].addr,
609b5903c47SLorenzo Bianconi tx_info.buf[n].len, DMA_TO_DEVICE);
610fcdd99ceSLorenzo Bianconi
611fcdd99ceSLorenzo Bianconi free:
612f0efa862SFelix Fietkau #ifdef CONFIG_NL80211_TESTMODE
613f0efa862SFelix Fietkau /* fix tx_done accounting on queue overflow */
614c918c74dSShayne Chen if (mt76_is_testmode_skb(dev, skb, &hw)) {
615c918c74dSShayne Chen struct mt76_phy *phy = hw->priv;
616c918c74dSShayne Chen
617c918c74dSShayne Chen if (tx_info.skb == phy->test.tx_skb)
618c918c74dSShayne Chen phy->test.tx_done--;
619c918c74dSShayne Chen }
620f0efa862SFelix Fietkau #endif
621f0efa862SFelix Fietkau
622fcdd99ceSLorenzo Bianconi mt76_put_txwi(dev, t);
62394e4f579SFelix Fietkau
62494e4f579SFelix Fietkau free_skb:
62594e4f579SFelix Fietkau status.skb = tx_info.skb;
62694e4f579SFelix Fietkau hw = mt76_tx_status_get_hw(dev, tx_info.skb);
6275b8ccdfbSFelix Fietkau spin_lock_bh(&dev->rx_lock);
62894e4f579SFelix Fietkau ieee80211_tx_status_ext(hw, &status);
6295b8ccdfbSFelix Fietkau spin_unlock_bh(&dev->rx_lock);
63094e4f579SFelix Fietkau
631fcdd99ceSLorenzo Bianconi return ret;
632fcdd99ceSLorenzo Bianconi }
633fcdd99ceSLorenzo Bianconi
mt76_dma_rx_fill(struct mt76_dev * dev,struct mt76_queue * q,bool allow_direct)6348a7386e7SLorenzo Bianconi int mt76_dma_rx_fill(struct mt76_dev *dev, struct mt76_queue *q,
6352f5c3c77SLorenzo Bianconi bool allow_direct)
63617f1de56SFelix Fietkau {
63717f1de56SFelix Fietkau int len = SKB_WITH_OVERHEAD(q->buf_size);
6382f5c3c77SLorenzo Bianconi int frames = 0;
63917f1de56SFelix Fietkau
640f9b627f1SBo Jiao if (!q->ndesc)
641f9b627f1SBo Jiao return 0;
642f9b627f1SBo Jiao
64317f1de56SFelix Fietkau spin_lock_bh(&q->lock);
64417f1de56SFelix Fietkau
64517f1de56SFelix Fietkau while (q->queued < q->ndesc - 1) {
646950d0abbSBo Jiao struct mt76_queue_buf qbuf = {};
6472f5c3c77SLorenzo Bianconi enum dma_data_direction dir;
6482f5c3c77SLorenzo Bianconi dma_addr_t addr;
6492f5c3c77SLorenzo Bianconi int offset;
650950d0abbSBo Jiao void *buf = NULL;
651950d0abbSBo Jiao
652950d0abbSBo Jiao if (mt76_queue_is_wed_rro_ind(q))
653950d0abbSBo Jiao goto done;
65417f1de56SFelix Fietkau
6552f5c3c77SLorenzo Bianconi buf = mt76_get_page_pool_buf(q, &offset, q->buf_size);
65617f1de56SFelix Fietkau if (!buf)
65717f1de56SFelix Fietkau break;
65817f1de56SFelix Fietkau
6592f5c3c77SLorenzo Bianconi addr = page_pool_get_dma_addr(virt_to_head_page(buf)) + offset;
6602f5c3c77SLorenzo Bianconi dir = page_pool_get_dma_dir(q->page_pool);
6612f5c3c77SLorenzo Bianconi dma_sync_single_for_device(dev->dma_dev, addr, len, dir);
66217f1de56SFelix Fietkau
6632f5c3c77SLorenzo Bianconi qbuf.addr = addr + q->buf_offset;
664950d0abbSBo Jiao done:
6652f5c3c77SLorenzo Bianconi qbuf.len = len - q->buf_offset;
666577298ecSLorenzo Bianconi qbuf.skip_unmap = false;
667953519b3SFelix Fietkau if (mt76_dma_add_rx_buf(dev, q, &qbuf, buf) < 0) {
6682f5c3c77SLorenzo Bianconi mt76_put_page_pool_buf(buf, allow_direct);
66996f134dcSLorenzo Bianconi break;
67096f134dcSLorenzo Bianconi }
67117f1de56SFelix Fietkau frames++;
67217f1de56SFelix Fietkau }
67317f1de56SFelix Fietkau
674950d0abbSBo Jiao if (frames || mt76_queue_is_wed_rx(q))
67517f1de56SFelix Fietkau mt76_dma_kick_queue(dev, q);
67617f1de56SFelix Fietkau
67717f1de56SFelix Fietkau spin_unlock_bh(&q->lock);
67817f1de56SFelix Fietkau
67917f1de56SFelix Fietkau return frames;
68017f1de56SFelix Fietkau }
68117f1de56SFelix Fietkau
682f68d6762SFelix Fietkau static int
mt76_dma_alloc_queue(struct mt76_dev * dev,struct mt76_queue * q,int idx,int n_desc,int bufsize,u32 ring_base)683f68d6762SFelix Fietkau mt76_dma_alloc_queue(struct mt76_dev *dev, struct mt76_queue *q,
684f68d6762SFelix Fietkau int idx, int n_desc, int bufsize,
685f68d6762SFelix Fietkau u32 ring_base)
686f68d6762SFelix Fietkau {
687f68d6762SFelix Fietkau int ret, size;
688f68d6762SFelix Fietkau
689f68d6762SFelix Fietkau spin_lock_init(&q->lock);
690f68d6762SFelix Fietkau spin_lock_init(&q->cleanup_lock);
691f68d6762SFelix Fietkau
692f68d6762SFelix Fietkau q->regs = dev->mmio.regs + ring_base + idx * MT_RING_SIZE;
693f68d6762SFelix Fietkau q->ndesc = n_desc;
694f68d6762SFelix Fietkau q->buf_size = bufsize;
695f68d6762SFelix Fietkau q->hw_idx = idx;
696f68d6762SFelix Fietkau
697950d0abbSBo Jiao size = mt76_queue_is_wed_rro_ind(q) ? sizeof(struct mt76_wed_rro_desc)
698950d0abbSBo Jiao : sizeof(struct mt76_desc);
699950d0abbSBo Jiao q->desc = dmam_alloc_coherent(dev->dma_dev, q->ndesc * size,
700950d0abbSBo Jiao &q->desc_dma, GFP_KERNEL);
701f68d6762SFelix Fietkau if (!q->desc)
702f68d6762SFelix Fietkau return -ENOMEM;
703f68d6762SFelix Fietkau
704950d0abbSBo Jiao if (mt76_queue_is_wed_rro_ind(q)) {
705950d0abbSBo Jiao struct mt76_wed_rro_desc *rro_desc;
706950d0abbSBo Jiao int i;
707950d0abbSBo Jiao
708950d0abbSBo Jiao rro_desc = (struct mt76_wed_rro_desc *)q->desc;
709950d0abbSBo Jiao for (i = 0; i < q->ndesc; i++) {
710950d0abbSBo Jiao struct mt76_wed_rro_ind *cmd;
711950d0abbSBo Jiao
712950d0abbSBo Jiao cmd = (struct mt76_wed_rro_ind *)&rro_desc[i];
713950d0abbSBo Jiao cmd->magic_cnt = MT_DMA_WED_IND_CMD_CNT - 1;
714950d0abbSBo Jiao }
715950d0abbSBo Jiao }
716950d0abbSBo Jiao
717f68d6762SFelix Fietkau size = q->ndesc * sizeof(*q->entry);
718f68d6762SFelix Fietkau q->entry = devm_kzalloc(dev->dev, size, GFP_KERNEL);
719f68d6762SFelix Fietkau if (!q->entry)
720f68d6762SFelix Fietkau return -ENOMEM;
721f68d6762SFelix Fietkau
7222f5c3c77SLorenzo Bianconi ret = mt76_create_page_pool(dev, q);
7232f5c3c77SLorenzo Bianconi if (ret)
7242f5c3c77SLorenzo Bianconi return ret;
7252f5c3c77SLorenzo Bianconi
7268a7386e7SLorenzo Bianconi ret = mt76_wed_dma_setup(dev, q, false);
727f68d6762SFelix Fietkau if (ret)
728f68d6762SFelix Fietkau return ret;
729f68d6762SFelix Fietkau
730950d0abbSBo Jiao if (mtk_wed_device_active(&dev->mmio.wed)) {
731950d0abbSBo Jiao if ((mtk_wed_get_rx_capa(&dev->mmio.wed) && mt76_queue_is_wed_rro(q)) ||
732950d0abbSBo Jiao mt76_queue_is_wed_tx_free(q))
733950d0abbSBo Jiao return 0;
734950d0abbSBo Jiao }
735950d0abbSBo Jiao
736f68d6762SFelix Fietkau mt76_dma_queue_reset(dev, q);
737f68d6762SFelix Fietkau
738f68d6762SFelix Fietkau return 0;
739f68d6762SFelix Fietkau }
740f68d6762SFelix Fietkau
74117f1de56SFelix Fietkau static void
mt76_dma_rx_cleanup(struct mt76_dev * dev,struct mt76_queue * q)74217f1de56SFelix Fietkau mt76_dma_rx_cleanup(struct mt76_dev *dev, struct mt76_queue *q)
74317f1de56SFelix Fietkau {
74417f1de56SFelix Fietkau void *buf;
74517f1de56SFelix Fietkau bool more;
74617f1de56SFelix Fietkau
747f9b627f1SBo Jiao if (!q->ndesc)
748f9b627f1SBo Jiao return;
749f9b627f1SBo Jiao
75017f1de56SFelix Fietkau do {
751ef444ad0SSean Wang spin_lock_bh(&q->lock);
752cd372b8cSLorenzo Bianconi buf = mt76_dma_dequeue(dev, q, true, NULL, NULL, &more, NULL);
753ef444ad0SSean Wang spin_unlock_bh(&q->lock);
754ef444ad0SSean Wang
75517f1de56SFelix Fietkau if (!buf)
75617f1de56SFelix Fietkau break;
75717f1de56SFelix Fietkau
758950d0abbSBo Jiao if (!mt76_queue_is_wed_rro(q))
7592f5c3c77SLorenzo Bianconi mt76_put_page_pool_buf(buf, false);
76017f1de56SFelix Fietkau } while (1);
7611b88b47eSLorenzo Bianconi
762ef444ad0SSean Wang spin_lock_bh(&q->lock);
7631b88b47eSLorenzo Bianconi if (q->rx_head) {
7641b88b47eSLorenzo Bianconi dev_kfree_skb(q->rx_head);
7651b88b47eSLorenzo Bianconi q->rx_head = NULL;
7661b88b47eSLorenzo Bianconi }
7671b88b47eSLorenzo Bianconi
76817f1de56SFelix Fietkau spin_unlock_bh(&q->lock);
76917f1de56SFelix Fietkau }
77017f1de56SFelix Fietkau
77117f1de56SFelix Fietkau static void
mt76_dma_rx_reset(struct mt76_dev * dev,enum mt76_rxq_id qid)77217f1de56SFelix Fietkau mt76_dma_rx_reset(struct mt76_dev *dev, enum mt76_rxq_id qid)
77317f1de56SFelix Fietkau {
77417f1de56SFelix Fietkau struct mt76_queue *q = &dev->q_rx[qid];
77517f1de56SFelix Fietkau
776f9b627f1SBo Jiao if (!q->ndesc)
777f9b627f1SBo Jiao return;
778f9b627f1SBo Jiao
779950d0abbSBo Jiao if (!mt76_queue_is_wed_rro_ind(q)) {
780950d0abbSBo Jiao int i;
781950d0abbSBo Jiao
78217f1de56SFelix Fietkau for (i = 0; i < q->ndesc; i++)
7832703bafcSFelix Fietkau q->desc[i].ctrl = cpu_to_le32(MT_DMA_CTL_DMA_DONE);
784950d0abbSBo Jiao }
78517f1de56SFelix Fietkau
78617f1de56SFelix Fietkau mt76_dma_rx_cleanup(dev, q);
7873bc4b811SSujuan Chen
7883bc4b811SSujuan Chen /* reset WED rx queues */
7898a7386e7SLorenzo Bianconi mt76_wed_dma_setup(dev, q, true);
79000d2ced0SLorenzo Bianconi
79100d2ced0SLorenzo Bianconi if (mt76_queue_is_wed_tx_free(q))
79200d2ced0SLorenzo Bianconi return;
79300d2ced0SLorenzo Bianconi
79400d2ced0SLorenzo Bianconi if (mtk_wed_device_active(&dev->mmio.wed) &&
79500d2ced0SLorenzo Bianconi mt76_queue_is_wed_rro(q))
79600d2ced0SLorenzo Bianconi return;
79700d2ced0SLorenzo Bianconi
79817f1de56SFelix Fietkau mt76_dma_sync_idx(dev, q);
7992f5c3c77SLorenzo Bianconi mt76_dma_rx_fill(dev, q, false);
80017f1de56SFelix Fietkau }
80117f1de56SFelix Fietkau
80217f1de56SFelix Fietkau static void
mt76_add_fragment(struct mt76_dev * dev,struct mt76_queue * q,void * data,int len,bool more,u32 info,bool allow_direct)80317f1de56SFelix Fietkau mt76_add_fragment(struct mt76_dev *dev, struct mt76_queue *q, void *data,
804cd607f2cSFelix Fietkau int len, bool more, u32 info, bool allow_direct)
80517f1de56SFelix Fietkau {
80617f1de56SFelix Fietkau struct sk_buff *skb = q->rx_head;
807b102f0c5SFelix Fietkau struct skb_shared_info *shinfo = skb_shinfo(skb);
808d0bd52c5SLorenzo Bianconi int nr_frags = shinfo->nr_frags;
80917f1de56SFelix Fietkau
810d0bd52c5SLorenzo Bianconi if (nr_frags < ARRAY_SIZE(shinfo->frags)) {
81193a1d479SLorenzo Bianconi struct page *page = virt_to_head_page(data);
81293a1d479SLorenzo Bianconi int offset = data - page_address(page) + q->buf_offset;
81393a1d479SLorenzo Bianconi
814d0bd52c5SLorenzo Bianconi skb_add_rx_frag(skb, nr_frags, page, offset, len, q->buf_size);
81593a1d479SLorenzo Bianconi } else {
816cd607f2cSFelix Fietkau mt76_put_page_pool_buf(data, allow_direct);
817b102f0c5SFelix Fietkau }
81817f1de56SFelix Fietkau
81917f1de56SFelix Fietkau if (more)
82017f1de56SFelix Fietkau return;
82117f1de56SFelix Fietkau
82217f1de56SFelix Fietkau q->rx_head = NULL;
823d0bd52c5SLorenzo Bianconi if (nr_frags < ARRAY_SIZE(shinfo->frags))
824c3137942SSujuan Chen dev->drv->rx_skb(dev, q - dev->q_rx, skb, &info);
825d0bd52c5SLorenzo Bianconi else
826d0bd52c5SLorenzo Bianconi dev_kfree_skb(skb);
82717f1de56SFelix Fietkau }
82817f1de56SFelix Fietkau
82917f1de56SFelix Fietkau static int
mt76_dma_rx_process(struct mt76_dev * dev,struct mt76_queue * q,int budget)83017f1de56SFelix Fietkau mt76_dma_rx_process(struct mt76_dev *dev, struct mt76_queue *q, int budget)
83117f1de56SFelix Fietkau {
832f68d6762SFelix Fietkau int len, data_len, done = 0, dma_idx;
83317f1de56SFelix Fietkau struct sk_buff *skb;
83417f1de56SFelix Fietkau unsigned char *data;
835f68d6762SFelix Fietkau bool check_ddone = false;
836cd607f2cSFelix Fietkau bool allow_direct = !mt76_queue_is_wed_rx(q);
83717f1de56SFelix Fietkau bool more;
83817f1de56SFelix Fietkau
839f68d6762SFelix Fietkau if (IS_ENABLED(CONFIG_NET_MEDIATEK_SOC_WED) &&
840132d74d3SLorenzo Bianconi mt76_queue_is_wed_tx_free(q)) {
8412e420b88SLorenzo Bianconi dma_idx = Q_READ(q, dma_idx);
842f68d6762SFelix Fietkau check_ddone = true;
843f68d6762SFelix Fietkau }
844f68d6762SFelix Fietkau
84517f1de56SFelix Fietkau while (done < budget) {
846cd372b8cSLorenzo Bianconi bool drop = false;
84717f1de56SFelix Fietkau u32 info;
84817f1de56SFelix Fietkau
849f68d6762SFelix Fietkau if (check_ddone) {
850f68d6762SFelix Fietkau if (q->tail == dma_idx)
8512e420b88SLorenzo Bianconi dma_idx = Q_READ(q, dma_idx);
852f68d6762SFelix Fietkau
853f68d6762SFelix Fietkau if (q->tail == dma_idx)
854f68d6762SFelix Fietkau break;
855f68d6762SFelix Fietkau }
856f68d6762SFelix Fietkau
857cd372b8cSLorenzo Bianconi data = mt76_dma_dequeue(dev, q, false, &len, &info, &more,
858cd372b8cSLorenzo Bianconi &drop);
85917f1de56SFelix Fietkau if (!data)
86017f1de56SFelix Fietkau break;
86117f1de56SFelix Fietkau
862cd372b8cSLorenzo Bianconi if (drop)
863cd372b8cSLorenzo Bianconi goto free_frag;
864cd372b8cSLorenzo Bianconi
86587e86f90SLorenzo Bianconi if (q->rx_head)
86687e86f90SLorenzo Bianconi data_len = q->buf_size;
86787e86f90SLorenzo Bianconi else
86887e86f90SLorenzo Bianconi data_len = SKB_WITH_OVERHEAD(q->buf_size);
86987e86f90SLorenzo Bianconi
87087e86f90SLorenzo Bianconi if (data_len < len + q->buf_offset) {
8719fe31054SFelix Fietkau dev_kfree_skb(q->rx_head);
8729fe31054SFelix Fietkau q->rx_head = NULL;
873fbe50d9aSFelix Fietkau goto free_frag;
8749fe31054SFelix Fietkau }
8759fe31054SFelix Fietkau
87617f1de56SFelix Fietkau if (q->rx_head) {
877cd607f2cSFelix Fietkau mt76_add_fragment(dev, q, data, len, more, info,
878cd607f2cSFelix Fietkau allow_direct);
87917f1de56SFelix Fietkau continue;
88017f1de56SFelix Fietkau }
88117f1de56SFelix Fietkau
882fbe50d9aSFelix Fietkau if (!more && dev->drv->rx_check &&
883fbe50d9aSFelix Fietkau !(dev->drv->rx_check(dev, data, len)))
884fbe50d9aSFelix Fietkau goto free_frag;
885fbe50d9aSFelix Fietkau
886f4d63a87SFelix Fietkau skb = napi_build_skb(data, q->buf_size);
887fbe50d9aSFelix Fietkau if (!skb)
888fbe50d9aSFelix Fietkau goto free_frag;
889fbe50d9aSFelix Fietkau
89017f1de56SFelix Fietkau skb_reserve(skb, q->buf_offset);
8912f5c3c77SLorenzo Bianconi skb_mark_for_recycle(skb);
89217f1de56SFelix Fietkau
893443dc85aSFelix Fietkau *(u32 *)skb->cb = info;
89417f1de56SFelix Fietkau
89517f1de56SFelix Fietkau __skb_put(skb, len);
89617f1de56SFelix Fietkau done++;
89717f1de56SFelix Fietkau
89817f1de56SFelix Fietkau if (more) {
89917f1de56SFelix Fietkau q->rx_head = skb;
90017f1de56SFelix Fietkau continue;
90117f1de56SFelix Fietkau }
90217f1de56SFelix Fietkau
903c3137942SSujuan Chen dev->drv->rx_skb(dev, q - dev->q_rx, skb, &info);
904fbe50d9aSFelix Fietkau continue;
905fbe50d9aSFelix Fietkau
906fbe50d9aSFelix Fietkau free_frag:
907cd607f2cSFelix Fietkau mt76_put_page_pool_buf(data, allow_direct);
90817f1de56SFelix Fietkau }
90917f1de56SFelix Fietkau
9102f5c3c77SLorenzo Bianconi mt76_dma_rx_fill(dev, q, true);
91117f1de56SFelix Fietkau return done;
91217f1de56SFelix Fietkau }
91317f1de56SFelix Fietkau
mt76_dma_rx_poll(struct napi_struct * napi,int budget)914cb8ed33dSLorenzo Bianconi int mt76_dma_rx_poll(struct napi_struct *napi, int budget)
91517f1de56SFelix Fietkau {
91617f1de56SFelix Fietkau struct mt76_dev *dev;
9172b4307f5SFelix Fietkau int qid, done = 0, cur;
91817f1de56SFelix Fietkau
919*08f116c9SBreno Leitao dev = mt76_priv(napi->dev);
92017f1de56SFelix Fietkau qid = napi - dev->napi;
92117f1de56SFelix Fietkau
9229c68a57bSFelix Fietkau rcu_read_lock();
9239c68a57bSFelix Fietkau
9242b4307f5SFelix Fietkau do {
9252b4307f5SFelix Fietkau cur = mt76_dma_rx_process(dev, &dev->q_rx[qid], budget - done);
92681e850efSLorenzo Bianconi mt76_rx_poll_complete(dev, qid, napi);
9272b4307f5SFelix Fietkau done += cur;
9282b4307f5SFelix Fietkau } while (cur && done < budget);
9292b4307f5SFelix Fietkau
9309c68a57bSFelix Fietkau rcu_read_unlock();
9319c68a57bSFelix Fietkau
9323e0705acSFelix Fietkau if (done < budget && napi_complete(napi))
93317f1de56SFelix Fietkau dev->drv->rx_poll_complete(dev, qid);
93417f1de56SFelix Fietkau
93517f1de56SFelix Fietkau return done;
93617f1de56SFelix Fietkau }
937cb8ed33dSLorenzo Bianconi EXPORT_SYMBOL_GPL(mt76_dma_rx_poll);
93817f1de56SFelix Fietkau
93917f1de56SFelix Fietkau static int
mt76_dma_init(struct mt76_dev * dev,int (* poll)(struct napi_struct * napi,int budget))940cb8ed33dSLorenzo Bianconi mt76_dma_init(struct mt76_dev *dev,
941cb8ed33dSLorenzo Bianconi int (*poll)(struct napi_struct *napi, int budget))
94217f1de56SFelix Fietkau {
943*08f116c9SBreno Leitao struct mt76_dev **priv;
94417f1de56SFelix Fietkau int i;
94517f1de56SFelix Fietkau
946*08f116c9SBreno Leitao dev->napi_dev = alloc_netdev_dummy(sizeof(struct mt76_dev *));
947*08f116c9SBreno Leitao if (!dev->napi_dev)
948*08f116c9SBreno Leitao return -ENOMEM;
949*08f116c9SBreno Leitao
950*08f116c9SBreno Leitao /* napi_dev private data points to mt76_dev parent, so, mt76_dev
951*08f116c9SBreno Leitao * can be retrieved given napi_dev
952*08f116c9SBreno Leitao */
953*08f116c9SBreno Leitao priv = netdev_priv(dev->napi_dev);
954*08f116c9SBreno Leitao *priv = dev;
955*08f116c9SBreno Leitao
956*08f116c9SBreno Leitao dev->tx_napi_dev = alloc_netdev_dummy(sizeof(struct mt76_dev *));
957*08f116c9SBreno Leitao if (!dev->tx_napi_dev) {
958*08f116c9SBreno Leitao free_netdev(dev->napi_dev);
959*08f116c9SBreno Leitao return -ENOMEM;
960*08f116c9SBreno Leitao }
961*08f116c9SBreno Leitao priv = netdev_priv(dev->tx_napi_dev);
962*08f116c9SBreno Leitao *priv = dev;
963*08f116c9SBreno Leitao
964*08f116c9SBreno Leitao snprintf(dev->napi_dev->name, sizeof(dev->napi_dev->name), "%s",
965aa40528aSFelix Fietkau wiphy_name(dev->hw->wiphy));
966*08f116c9SBreno Leitao dev->napi_dev->threaded = 1;
96736b7fce1SLorenzo Bianconi init_completion(&dev->mmio.wed_reset);
96836b7fce1SLorenzo Bianconi init_completion(&dev->mmio.wed_reset_complete);
96917f1de56SFelix Fietkau
970f473b42aSFelix Fietkau mt76_for_each_q_rx(dev, i) {
971*08f116c9SBreno Leitao netif_napi_add(dev->napi_dev, &dev->napi[i], poll);
9722f5c3c77SLorenzo Bianconi mt76_dma_rx_fill(dev, &dev->q_rx[i], false);
97317f1de56SFelix Fietkau napi_enable(&dev->napi[i]);
97417f1de56SFelix Fietkau }
97517f1de56SFelix Fietkau
97617f1de56SFelix Fietkau return 0;
97717f1de56SFelix Fietkau }
97817f1de56SFelix Fietkau
97917f1de56SFelix Fietkau static const struct mt76_queue_ops mt76_dma_ops = {
98017f1de56SFelix Fietkau .init = mt76_dma_init,
98117f1de56SFelix Fietkau .alloc = mt76_dma_alloc_queue,
9823990465dSLorenzo Bianconi .reset_q = mt76_dma_queue_reset,
9835ed31128SLorenzo Bianconi .tx_queue_skb_raw = mt76_dma_tx_queue_skb_raw,
984469d4818SLorenzo Bianconi .tx_queue_skb = mt76_dma_tx_queue_skb,
98517f1de56SFelix Fietkau .tx_cleanup = mt76_dma_tx_cleanup,
986c001df97SLorenzo Bianconi .rx_cleanup = mt76_dma_rx_cleanup,
98717f1de56SFelix Fietkau .rx_reset = mt76_dma_rx_reset,
98817f1de56SFelix Fietkau .kick = mt76_dma_kick_queue,
98917f1de56SFelix Fietkau };
99017f1de56SFelix Fietkau
mt76_dma_attach(struct mt76_dev * dev)991bceac167SRyder Lee void mt76_dma_attach(struct mt76_dev *dev)
99217f1de56SFelix Fietkau {
99317f1de56SFelix Fietkau dev->queue_ops = &mt76_dma_ops;
99417f1de56SFelix Fietkau }
99517f1de56SFelix Fietkau EXPORT_SYMBOL_GPL(mt76_dma_attach);
99617f1de56SFelix Fietkau
mt76_dma_cleanup(struct mt76_dev * dev)99717f1de56SFelix Fietkau void mt76_dma_cleanup(struct mt76_dev *dev)
99817f1de56SFelix Fietkau {
99917f1de56SFelix Fietkau int i;
100017f1de56SFelix Fietkau
1001781eef5bSFelix Fietkau mt76_worker_disable(&dev->tx_worker);
10024875e346SLorenzo Bianconi netif_napi_del(&dev->tx_napi);
1003e637763bSLorenzo Bianconi
1004dc44c45cSLorenzo Bianconi for (i = 0; i < ARRAY_SIZE(dev->phys); i++) {
1005dc44c45cSLorenzo Bianconi struct mt76_phy *phy = dev->phys[i];
1006dc44c45cSLorenzo Bianconi int j;
1007dc44c45cSLorenzo Bianconi
1008dc44c45cSLorenzo Bianconi if (!phy)
1009dc44c45cSLorenzo Bianconi continue;
1010dc44c45cSLorenzo Bianconi
1011dc44c45cSLorenzo Bianconi for (j = 0; j < ARRAY_SIZE(phy->q_tx); j++)
1012dc44c45cSLorenzo Bianconi mt76_dma_tx_cleanup(dev, phy->q_tx[j], true);
101391990519SLorenzo Bianconi }
101417f1de56SFelix Fietkau
1015e637763bSLorenzo Bianconi for (i = 0; i < ARRAY_SIZE(dev->q_mcu); i++)
1016e637763bSLorenzo Bianconi mt76_dma_tx_cleanup(dev, dev->q_mcu[i], true);
1017e637763bSLorenzo Bianconi
1018f473b42aSFelix Fietkau mt76_for_each_q_rx(dev, i) {
101952546e27SLorenzo Bianconi struct mt76_queue *q = &dev->q_rx[i];
102052546e27SLorenzo Bianconi
1021950d0abbSBo Jiao if (mtk_wed_device_active(&dev->mmio.wed) &&
1022950d0abbSBo Jiao mt76_queue_is_wed_rro(q))
1023950d0abbSBo Jiao continue;
1024950d0abbSBo Jiao
102517f1de56SFelix Fietkau netif_napi_del(&dev->napi[i]);
102652546e27SLorenzo Bianconi mt76_dma_rx_cleanup(dev, q);
10272f5c3c77SLorenzo Bianconi
10282f5c3c77SLorenzo Bianconi page_pool_destroy(q->page_pool);
102917f1de56SFelix Fietkau }
1030dd57a95cSFelix Fietkau
1031f68d6762SFelix Fietkau if (mtk_wed_device_active(&dev->mmio.wed))
1032f68d6762SFelix Fietkau mtk_wed_device_detach(&dev->mmio.wed);
103383eafc92SSujuan Chen
103483eafc92SSujuan Chen if (mtk_wed_device_active(&dev->mmio.wed_hif2))
103583eafc92SSujuan Chen mtk_wed_device_detach(&dev->mmio.wed_hif2);
103683eafc92SSujuan Chen
103783eafc92SSujuan Chen mt76_free_pending_txwi(dev);
103883eafc92SSujuan Chen mt76_free_pending_rxwi(dev);
1039*08f116c9SBreno Leitao free_netdev(dev->napi_dev);
1040*08f116c9SBreno Leitao free_netdev(dev->tx_napi_dev);
104117f1de56SFelix Fietkau }
104217f1de56SFelix Fietkau EXPORT_SYMBOL_GPL(mt76_dma_cleanup);
1043