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
12f68d6762SFelix Fietkau #define Q_READ(_dev, _q, _field) ({ \
13f68d6762SFelix Fietkau u32 _offset = offsetof(struct mt76_queue_regs, _field); \
14f68d6762SFelix Fietkau u32 _val; \
15f68d6762SFelix Fietkau if ((_q)->flags & MT_QFLAG_WED) \
16f68d6762SFelix Fietkau _val = mtk_wed_device_reg_read(&(_dev)->mmio.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
24f68d6762SFelix Fietkau #define Q_WRITE(_dev, _q, _field, _val) do { \
25f68d6762SFelix Fietkau u32 _offset = offsetof(struct mt76_queue_regs, _field); \
26f68d6762SFelix Fietkau if ((_q)->flags & MT_QFLAG_WED) \
27f68d6762SFelix Fietkau mtk_wed_device_reg_write(&(_dev)->mmio.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
36cc9fd945SFelix Fietkau #define Q_READ(_dev, _q, _field) readl(&(_q)->regs->_field)
37cc9fd945SFelix Fietkau #define Q_WRITE(_dev, _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);
56dd57a95cSFelix Fietkau t = (struct mt76_txwi_cache *)(txwi + dev->drv->txwi_size);
57dd57a95cSFelix Fietkau t->dma_addr = addr;
58dd57a95cSFelix Fietkau
59dd57a95cSFelix Fietkau return t;
60dd57a95cSFelix Fietkau }
61dd57a95cSFelix Fietkau
62dd57a95cSFelix Fietkau static struct mt76_txwi_cache *
mt76_alloc_rxwi(struct mt76_dev * dev)632666beceSSujuan Chen mt76_alloc_rxwi(struct mt76_dev *dev)
642666beceSSujuan Chen {
652666beceSSujuan Chen struct mt76_txwi_cache *t;
662666beceSSujuan Chen
672666beceSSujuan Chen t = kzalloc(L1_CACHE_ALIGN(sizeof(*t)), GFP_ATOMIC);
682666beceSSujuan Chen if (!t)
692666beceSSujuan Chen return NULL;
702666beceSSujuan Chen
712666beceSSujuan Chen t->ptr = NULL;
722666beceSSujuan Chen return t;
732666beceSSujuan Chen }
742666beceSSujuan Chen
752666beceSSujuan Chen static struct mt76_txwi_cache *
__mt76_get_txwi(struct mt76_dev * dev)76dd57a95cSFelix Fietkau __mt76_get_txwi(struct mt76_dev *dev)
77dd57a95cSFelix Fietkau {
78dd57a95cSFelix Fietkau struct mt76_txwi_cache *t = NULL;
79dd57a95cSFelix Fietkau
80dd57a95cSFelix Fietkau spin_lock(&dev->lock);
81dd57a95cSFelix Fietkau if (!list_empty(&dev->txwi_cache)) {
82dd57a95cSFelix Fietkau t = list_first_entry(&dev->txwi_cache, struct mt76_txwi_cache,
83dd57a95cSFelix Fietkau list);
84dd57a95cSFelix Fietkau list_del(&t->list);
85dd57a95cSFelix Fietkau }
86dd57a95cSFelix Fietkau spin_unlock(&dev->lock);
87dd57a95cSFelix Fietkau
88dd57a95cSFelix Fietkau return t;
89dd57a95cSFelix Fietkau }
90dd57a95cSFelix Fietkau
91dd57a95cSFelix Fietkau static struct mt76_txwi_cache *
__mt76_get_rxwi(struct mt76_dev * dev)922666beceSSujuan Chen __mt76_get_rxwi(struct mt76_dev *dev)
932666beceSSujuan Chen {
942666beceSSujuan Chen struct mt76_txwi_cache *t = NULL;
952666beceSSujuan Chen
9619527314SLorenzo Bianconi spin_lock_bh(&dev->wed_lock);
972666beceSSujuan Chen if (!list_empty(&dev->rxwi_cache)) {
982666beceSSujuan Chen t = list_first_entry(&dev->rxwi_cache, struct mt76_txwi_cache,
992666beceSSujuan Chen list);
1002666beceSSujuan Chen list_del(&t->list);
1012666beceSSujuan Chen }
10219527314SLorenzo Bianconi spin_unlock_bh(&dev->wed_lock);
1032666beceSSujuan Chen
1042666beceSSujuan Chen return t;
1052666beceSSujuan Chen }
1062666beceSSujuan Chen
1072666beceSSujuan Chen static struct mt76_txwi_cache *
mt76_get_txwi(struct mt76_dev * dev)108dd57a95cSFelix Fietkau mt76_get_txwi(struct mt76_dev *dev)
109dd57a95cSFelix Fietkau {
110dd57a95cSFelix Fietkau struct mt76_txwi_cache *t = __mt76_get_txwi(dev);
111dd57a95cSFelix Fietkau
112dd57a95cSFelix Fietkau if (t)
113dd57a95cSFelix Fietkau return t;
114dd57a95cSFelix Fietkau
115dd57a95cSFelix Fietkau return mt76_alloc_txwi(dev);
116dd57a95cSFelix Fietkau }
117dd57a95cSFelix Fietkau
1182666beceSSujuan Chen struct mt76_txwi_cache *
mt76_get_rxwi(struct mt76_dev * dev)1192666beceSSujuan Chen mt76_get_rxwi(struct mt76_dev *dev)
1202666beceSSujuan Chen {
1212666beceSSujuan Chen struct mt76_txwi_cache *t = __mt76_get_rxwi(dev);
1222666beceSSujuan Chen
1232666beceSSujuan Chen if (t)
1242666beceSSujuan Chen return t;
1252666beceSSujuan Chen
1262666beceSSujuan Chen return mt76_alloc_rxwi(dev);
1272666beceSSujuan Chen }
1282666beceSSujuan Chen EXPORT_SYMBOL_GPL(mt76_get_rxwi);
1292666beceSSujuan Chen
130dd57a95cSFelix Fietkau void
mt76_put_txwi(struct mt76_dev * dev,struct mt76_txwi_cache * t)131dd57a95cSFelix Fietkau mt76_put_txwi(struct mt76_dev *dev, struct mt76_txwi_cache *t)
132dd57a95cSFelix Fietkau {
133dd57a95cSFelix Fietkau if (!t)
134dd57a95cSFelix Fietkau return;
135dd57a95cSFelix Fietkau
136dd57a95cSFelix Fietkau spin_lock(&dev->lock);
137dd57a95cSFelix Fietkau list_add(&t->list, &dev->txwi_cache);
138dd57a95cSFelix Fietkau spin_unlock(&dev->lock);
139dd57a95cSFelix Fietkau }
140dd57a95cSFelix Fietkau EXPORT_SYMBOL_GPL(mt76_put_txwi);
141dd57a95cSFelix Fietkau
1422666beceSSujuan Chen void
mt76_put_rxwi(struct mt76_dev * dev,struct mt76_txwi_cache * t)1432666beceSSujuan Chen mt76_put_rxwi(struct mt76_dev *dev, struct mt76_txwi_cache *t)
1442666beceSSujuan Chen {
1452666beceSSujuan Chen if (!t)
1462666beceSSujuan Chen return;
1472666beceSSujuan Chen
14819527314SLorenzo Bianconi spin_lock_bh(&dev->wed_lock);
1492666beceSSujuan Chen list_add(&t->list, &dev->rxwi_cache);
15019527314SLorenzo Bianconi spin_unlock_bh(&dev->wed_lock);
1512666beceSSujuan Chen }
1522666beceSSujuan Chen EXPORT_SYMBOL_GPL(mt76_put_rxwi);
1532666beceSSujuan Chen
154dd57a95cSFelix Fietkau static void
mt76_free_pending_txwi(struct mt76_dev * dev)155dd57a95cSFelix Fietkau mt76_free_pending_txwi(struct mt76_dev *dev)
156dd57a95cSFelix Fietkau {
157dd57a95cSFelix Fietkau struct mt76_txwi_cache *t;
158dd57a95cSFelix Fietkau
1595f0ce584SLorenzo Bianconi local_bh_disable();
160402e0109SFelix Fietkau while ((t = __mt76_get_txwi(dev)) != NULL) {
161d1ddc536SFelix Fietkau dma_unmap_single(dev->dma_dev, t->dma_addr, dev->drv->txwi_size,
162dd57a95cSFelix Fietkau DMA_TO_DEVICE);
163402e0109SFelix Fietkau kfree(mt76_get_txwi_ptr(dev, t));
164402e0109SFelix Fietkau }
1655f0ce584SLorenzo Bianconi local_bh_enable();
166dd57a95cSFelix Fietkau }
167dd57a95cSFelix Fietkau
168a97a467aSSujuan Chen void
mt76_free_pending_rxwi(struct mt76_dev * dev)1692666beceSSujuan Chen mt76_free_pending_rxwi(struct mt76_dev *dev)
1702666beceSSujuan Chen {
1712666beceSSujuan Chen struct mt76_txwi_cache *t;
1722666beceSSujuan Chen
1732666beceSSujuan Chen local_bh_disable();
1742666beceSSujuan Chen while ((t = __mt76_get_rxwi(dev)) != NULL) {
1752666beceSSujuan Chen if (t->ptr)
1762f5c3c77SLorenzo Bianconi mt76_put_page_pool_buf(t->ptr, false);
1772666beceSSujuan Chen kfree(t);
1782666beceSSujuan Chen }
1792666beceSSujuan Chen local_bh_enable();
1802666beceSSujuan Chen }
181a97a467aSSujuan Chen EXPORT_SYMBOL_GPL(mt76_free_pending_rxwi);
1822666beceSSujuan Chen
1832666beceSSujuan Chen static void
mt76_dma_sync_idx(struct mt76_dev * dev,struct mt76_queue * q)1843990465dSLorenzo Bianconi mt76_dma_sync_idx(struct mt76_dev *dev, struct mt76_queue *q)
1853990465dSLorenzo Bianconi {
186cc9fd945SFelix Fietkau Q_WRITE(dev, q, desc_base, q->desc_dma);
187cc9fd945SFelix Fietkau Q_WRITE(dev, q, ring_size, q->ndesc);
188cc9fd945SFelix Fietkau q->head = Q_READ(dev, q, dma_idx);
1893990465dSLorenzo Bianconi q->tail = q->head;
1903990465dSLorenzo Bianconi }
1913990465dSLorenzo Bianconi
1923990465dSLorenzo Bianconi static void
mt76_dma_queue_reset(struct mt76_dev * dev,struct mt76_queue * q)1933990465dSLorenzo Bianconi mt76_dma_queue_reset(struct mt76_dev *dev, struct mt76_queue *q)
1943990465dSLorenzo Bianconi {
1953990465dSLorenzo Bianconi int i;
1963990465dSLorenzo Bianconi
197f9b627f1SBo Jiao if (!q || !q->ndesc)
1983990465dSLorenzo Bianconi return;
1993990465dSLorenzo Bianconi
2003990465dSLorenzo Bianconi /* clear descriptors */
2013990465dSLorenzo Bianconi for (i = 0; i < q->ndesc; i++)
2023990465dSLorenzo Bianconi q->desc[i].ctrl = cpu_to_le32(MT_DMA_CTL_DMA_DONE);
2033990465dSLorenzo Bianconi
204cc9fd945SFelix Fietkau Q_WRITE(dev, q, cpu_idx, 0);
205cc9fd945SFelix Fietkau Q_WRITE(dev, q, dma_idx, 0);
2063990465dSLorenzo Bianconi mt76_dma_sync_idx(dev, q);
2073990465dSLorenzo Bianconi }
2083990465dSLorenzo Bianconi
20917f1de56SFelix Fietkau static int
mt76_dma_add_rx_buf(struct mt76_dev * dev,struct mt76_queue * q,struct mt76_queue_buf * buf,void * data)210953519b3SFelix Fietkau mt76_dma_add_rx_buf(struct mt76_dev *dev, struct mt76_queue *q,
211953519b3SFelix Fietkau struct mt76_queue_buf *buf, void *data)
212953519b3SFelix Fietkau {
213953519b3SFelix Fietkau struct mt76_desc *desc = &q->desc[q->head];
214953519b3SFelix Fietkau struct mt76_queue_entry *entry = &q->entry[q->head];
215953519b3SFelix Fietkau struct mt76_txwi_cache *txwi = NULL;
216953519b3SFelix Fietkau u32 buf1 = 0, ctrl;
217953519b3SFelix Fietkau int idx = q->head;
218953519b3SFelix Fietkau int rx_token;
219953519b3SFelix Fietkau
220953519b3SFelix Fietkau ctrl = FIELD_PREP(MT_DMA_CTL_SD_LEN0, buf[0].len);
221953519b3SFelix Fietkau
22258bcd4edSLorenzo Bianconi if (mt76_queue_is_wed_rx(q)) {
223953519b3SFelix Fietkau txwi = mt76_get_rxwi(dev);
224953519b3SFelix Fietkau if (!txwi)
225953519b3SFelix Fietkau return -ENOMEM;
226953519b3SFelix Fietkau
227953519b3SFelix Fietkau rx_token = mt76_rx_token_consume(dev, data, txwi, buf->addr);
228953519b3SFelix Fietkau if (rx_token < 0) {
229953519b3SFelix Fietkau mt76_put_rxwi(dev, txwi);
230953519b3SFelix Fietkau return -ENOMEM;
231953519b3SFelix Fietkau }
232953519b3SFelix Fietkau
233953519b3SFelix Fietkau buf1 |= FIELD_PREP(MT_DMA_CTL_TOKEN, rx_token);
234953519b3SFelix Fietkau ctrl |= MT_DMA_CTL_TO_HOST;
235953519b3SFelix Fietkau }
236953519b3SFelix Fietkau
237953519b3SFelix Fietkau WRITE_ONCE(desc->buf0, cpu_to_le32(buf->addr));
238953519b3SFelix Fietkau WRITE_ONCE(desc->buf1, cpu_to_le32(buf1));
239953519b3SFelix Fietkau WRITE_ONCE(desc->ctrl, cpu_to_le32(ctrl));
240953519b3SFelix Fietkau WRITE_ONCE(desc->info, 0);
241953519b3SFelix Fietkau
242953519b3SFelix Fietkau entry->dma_addr[0] = buf->addr;
243953519b3SFelix Fietkau entry->dma_len[0] = buf->len;
244953519b3SFelix Fietkau entry->txwi = txwi;
245953519b3SFelix Fietkau entry->buf = data;
246953519b3SFelix Fietkau entry->wcid = 0xffff;
247953519b3SFelix Fietkau entry->skip_buf1 = true;
248953519b3SFelix Fietkau q->head = (q->head + 1) % q->ndesc;
249953519b3SFelix Fietkau q->queued++;
250953519b3SFelix Fietkau
251953519b3SFelix Fietkau return idx;
252953519b3SFelix Fietkau }
253953519b3SFelix Fietkau
254953519b3SFelix 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)25517f1de56SFelix Fietkau mt76_dma_add_buf(struct mt76_dev *dev, struct mt76_queue *q,
25617f1de56SFelix Fietkau struct mt76_queue_buf *buf, int nbufs, u32 info,
25717f1de56SFelix Fietkau struct sk_buff *skb, void *txwi)
25817f1de56SFelix Fietkau {
25975d4bf1fSFelix Fietkau struct mt76_queue_entry *entry;
26017f1de56SFelix Fietkau struct mt76_desc *desc;
26117f1de56SFelix Fietkau int i, idx = -1;
262fe13dad8SLorenzo Bianconi u32 ctrl, next;
26317f1de56SFelix Fietkau
264953519b3SFelix Fietkau if (txwi) {
265953519b3SFelix Fietkau q->entry[q->head].txwi = DMA_DUMMY_DATA;
266953519b3SFelix Fietkau q->entry[q->head].skip_buf0 = true;
267953519b3SFelix Fietkau }
268953519b3SFelix Fietkau
26917f1de56SFelix Fietkau for (i = 0; i < nbufs; i += 2, buf += 2) {
27017f1de56SFelix Fietkau u32 buf0 = buf[0].addr, buf1 = 0;
27117f1de56SFelix Fietkau
27275d4bf1fSFelix Fietkau idx = q->head;
273fe13dad8SLorenzo Bianconi next = (q->head + 1) % q->ndesc;
27475d4bf1fSFelix Fietkau
27575d4bf1fSFelix Fietkau desc = &q->desc[idx];
27675d4bf1fSFelix Fietkau entry = &q->entry[idx];
27775d4bf1fSFelix Fietkau
27827d5c528SFelix Fietkau if (buf[0].skip_unmap)
27975d4bf1fSFelix Fietkau entry->skip_buf0 = true;
28075d4bf1fSFelix Fietkau entry->skip_buf1 = i == nbufs - 1;
28175d4bf1fSFelix Fietkau
28275d4bf1fSFelix Fietkau entry->dma_addr[0] = buf[0].addr;
28375d4bf1fSFelix Fietkau entry->dma_len[0] = buf[0].len;
28427d5c528SFelix Fietkau
28517f1de56SFelix Fietkau ctrl = FIELD_PREP(MT_DMA_CTL_SD_LEN0, buf[0].len);
28617f1de56SFelix Fietkau if (i < nbufs - 1) {
28775d4bf1fSFelix Fietkau entry->dma_addr[1] = buf[1].addr;
28875d4bf1fSFelix Fietkau entry->dma_len[1] = buf[1].len;
28917f1de56SFelix Fietkau buf1 = buf[1].addr;
29017f1de56SFelix Fietkau ctrl |= FIELD_PREP(MT_DMA_CTL_SD_LEN1, buf[1].len);
29127d5c528SFelix Fietkau if (buf[1].skip_unmap)
29275d4bf1fSFelix Fietkau entry->skip_buf1 = true;
29317f1de56SFelix Fietkau }
29417f1de56SFelix Fietkau
29517f1de56SFelix Fietkau if (i == nbufs - 1)
29617f1de56SFelix Fietkau ctrl |= MT_DMA_CTL_LAST_SEC0;
29717f1de56SFelix Fietkau else if (i == nbufs - 2)
29817f1de56SFelix Fietkau ctrl |= MT_DMA_CTL_LAST_SEC1;
29917f1de56SFelix Fietkau
30017f1de56SFelix Fietkau WRITE_ONCE(desc->buf0, cpu_to_le32(buf0));
30117f1de56SFelix Fietkau WRITE_ONCE(desc->buf1, cpu_to_le32(buf1));
30217f1de56SFelix Fietkau WRITE_ONCE(desc->info, cpu_to_le32(info));
30317f1de56SFelix Fietkau WRITE_ONCE(desc->ctrl, cpu_to_le32(ctrl));
30417f1de56SFelix Fietkau
305fe13dad8SLorenzo Bianconi q->head = next;
30617f1de56SFelix Fietkau q->queued++;
30717f1de56SFelix Fietkau }
30817f1de56SFelix Fietkau
30917f1de56SFelix Fietkau q->entry[idx].txwi = txwi;
31017f1de56SFelix Fietkau q->entry[idx].skb = skb;
3116d51cae2SFelix Fietkau q->entry[idx].wcid = 0xffff;
31217f1de56SFelix Fietkau
31317f1de56SFelix Fietkau return idx;
31417f1de56SFelix Fietkau }
31517f1de56SFelix Fietkau
31617f1de56SFelix Fietkau static void
mt76_dma_tx_cleanup_idx(struct mt76_dev * dev,struct mt76_queue * q,int idx,struct mt76_queue_entry * prev_e)31717f1de56SFelix Fietkau mt76_dma_tx_cleanup_idx(struct mt76_dev *dev, struct mt76_queue *q, int idx,
31817f1de56SFelix Fietkau struct mt76_queue_entry *prev_e)
31917f1de56SFelix Fietkau {
32017f1de56SFelix Fietkau struct mt76_queue_entry *e = &q->entry[idx];
32117f1de56SFelix Fietkau
32275d4bf1fSFelix Fietkau if (!e->skip_buf0)
323d1ddc536SFelix Fietkau dma_unmap_single(dev->dma_dev, e->dma_addr[0], e->dma_len[0],
32417f1de56SFelix Fietkau DMA_TO_DEVICE);
32517f1de56SFelix Fietkau
32675d4bf1fSFelix Fietkau if (!e->skip_buf1)
327d1ddc536SFelix Fietkau dma_unmap_single(dev->dma_dev, e->dma_addr[1], e->dma_len[1],
32817f1de56SFelix Fietkau DMA_TO_DEVICE);
32917f1de56SFelix Fietkau
330598da386SLorenzo Bianconi if (e->txwi == DMA_DUMMY_DATA)
33117f1de56SFelix Fietkau e->txwi = NULL;
33217f1de56SFelix Fietkau
33317f1de56SFelix Fietkau *prev_e = *e;
33417f1de56SFelix Fietkau memset(e, 0, sizeof(*e));
33517f1de56SFelix Fietkau }
33617f1de56SFelix Fietkau
33717f1de56SFelix Fietkau static void
mt76_dma_kick_queue(struct mt76_dev * dev,struct mt76_queue * q)3388f6c4f7bSFelix Fietkau mt76_dma_kick_queue(struct mt76_dev *dev, struct mt76_queue *q)
3398f6c4f7bSFelix Fietkau {
3402d681047SFelix Fietkau wmb();
341cc9fd945SFelix Fietkau Q_WRITE(dev, q, cpu_idx, q->head);
34217f1de56SFelix Fietkau }
34317f1de56SFelix Fietkau
34417f1de56SFelix Fietkau static void
mt76_dma_tx_cleanup(struct mt76_dev * dev,struct mt76_queue * q,bool flush)345e5655492SLorenzo Bianconi mt76_dma_tx_cleanup(struct mt76_dev *dev, struct mt76_queue *q, bool flush)
34617f1de56SFelix Fietkau {
34717f1de56SFelix Fietkau struct mt76_queue_entry entry;
3480b51f186SFelix Fietkau int last;
34917f1de56SFelix Fietkau
350f9b627f1SBo Jiao if (!q || !q->ndesc)
35117f1de56SFelix Fietkau return;
35217f1de56SFelix Fietkau
3539716ef04SFelix Fietkau spin_lock_bh(&q->cleanup_lock);
35417f1de56SFelix Fietkau if (flush)
35517f1de56SFelix Fietkau last = -1;
35617f1de56SFelix Fietkau else
357cc9fd945SFelix Fietkau last = Q_READ(dev, q, dma_idx);
35817f1de56SFelix Fietkau
3590b51f186SFelix Fietkau while (q->queued > 0 && q->tail != last) {
36017f1de56SFelix Fietkau mt76_dma_tx_cleanup_idx(dev, q, q->tail, &entry);
361fe5b5ab5SFelix Fietkau mt76_queue_tx_complete(dev, q, &entry);
36217f1de56SFelix Fietkau
36317f1de56SFelix Fietkau if (entry.txwi) {
3649ec0b821SFelix Fietkau if (!(dev->drv->drv_flags & MT_DRV_TXWI_NO_FREE))
36517f1de56SFelix Fietkau mt76_put_txwi(dev, entry.txwi);
36617f1de56SFelix Fietkau }
36717f1de56SFelix Fietkau
36817f1de56SFelix Fietkau if (!flush && q->tail == last)
369cc9fd945SFelix Fietkau last = Q_READ(dev, q, dma_idx);
3705a95ca41SFelix Fietkau }
3719716ef04SFelix Fietkau spin_unlock_bh(&q->cleanup_lock);
3725a95ca41SFelix Fietkau
3738f6c4f7bSFelix Fietkau if (flush) {
3740b51f186SFelix Fietkau spin_lock_bh(&q->lock);
37517f1de56SFelix Fietkau mt76_dma_sync_idx(dev, q);
3768f6c4f7bSFelix Fietkau mt76_dma_kick_queue(dev, q);
3770b51f186SFelix Fietkau spin_unlock_bh(&q->lock);
3788f6c4f7bSFelix Fietkau }
37917f1de56SFelix Fietkau
38026e40d4cSFelix Fietkau if (!q->queued)
38126e40d4cSFelix Fietkau wake_up(&dev->tx_wait);
38217f1de56SFelix Fietkau }
38317f1de56SFelix Fietkau
38417f1de56SFelix 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)38517f1de56SFelix Fietkau mt76_dma_get_buf(struct mt76_dev *dev, struct mt76_queue *q, int idx,
386cd372b8cSLorenzo Bianconi int *len, u32 *info, bool *more, bool *drop)
38717f1de56SFelix Fietkau {
38817f1de56SFelix Fietkau struct mt76_queue_entry *e = &q->entry[idx];
38917f1de56SFelix Fietkau struct mt76_desc *desc = &q->desc[idx];
390cd372b8cSLorenzo Bianconi void *buf;
39117f1de56SFelix Fietkau
39217f1de56SFelix Fietkau if (len) {
393cd372b8cSLorenzo Bianconi u32 ctrl = le32_to_cpu(READ_ONCE(desc->ctrl));
394cd372b8cSLorenzo Bianconi *len = FIELD_GET(MT_DMA_CTL_SD_LEN0, ctrl);
395cd372b8cSLorenzo Bianconi *more = !(ctrl & MT_DMA_CTL_LAST_SEC0);
39617f1de56SFelix Fietkau }
39717f1de56SFelix Fietkau
39817f1de56SFelix Fietkau if (info)
39917f1de56SFelix Fietkau *info = le32_to_cpu(desc->info);
40017f1de56SFelix Fietkau
40158bcd4edSLorenzo Bianconi if (mt76_queue_is_wed_rx(q)) {
402e4d2b8bcSPeter Chiu u32 buf1 = le32_to_cpu(desc->buf1);
403e4d2b8bcSPeter Chiu u32 token = FIELD_GET(MT_DMA_CTL_TOKEN, buf1);
404cd372b8cSLorenzo Bianconi struct mt76_txwi_cache *t = mt76_rx_token_release(dev, token);
405cd372b8cSLorenzo Bianconi
406cd372b8cSLorenzo Bianconi if (!t)
407cd372b8cSLorenzo Bianconi return NULL;
408cd372b8cSLorenzo Bianconi
4092f5c3c77SLorenzo Bianconi dma_sync_single_for_cpu(dev->dma_dev, t->dma_addr,
410cd372b8cSLorenzo Bianconi SKB_WITH_OVERHEAD(q->buf_size),
4112f5c3c77SLorenzo Bianconi page_pool_get_dma_dir(q->page_pool));
412cd372b8cSLorenzo Bianconi
413cd372b8cSLorenzo Bianconi buf = t->ptr;
414cd372b8cSLorenzo Bianconi t->dma_addr = 0;
415cd372b8cSLorenzo Bianconi t->ptr = NULL;
416cd372b8cSLorenzo Bianconi
417cd372b8cSLorenzo Bianconi mt76_put_rxwi(dev, t);
418cd372b8cSLorenzo Bianconi
419cd372b8cSLorenzo Bianconi if (drop) {
420cd372b8cSLorenzo Bianconi u32 ctrl = le32_to_cpu(READ_ONCE(desc->ctrl));
421cd372b8cSLorenzo Bianconi
422cd372b8cSLorenzo Bianconi *drop = !!(ctrl & (MT_DMA_CTL_TO_HOST_A |
423cd372b8cSLorenzo Bianconi MT_DMA_CTL_DROP));
424e4d2b8bcSPeter Chiu
425e4d2b8bcSPeter Chiu *drop |= !!(buf1 & MT_DMA_CTL_WO_DROP);
426cd372b8cSLorenzo Bianconi }
427cd372b8cSLorenzo Bianconi } else {
428cd372b8cSLorenzo Bianconi buf = e->buf;
42917f1de56SFelix Fietkau e->buf = NULL;
4302f5c3c77SLorenzo Bianconi dma_sync_single_for_cpu(dev->dma_dev, e->dma_addr[0],
431cd372b8cSLorenzo Bianconi SKB_WITH_OVERHEAD(q->buf_size),
4322f5c3c77SLorenzo Bianconi page_pool_get_dma_dir(q->page_pool));
433cd372b8cSLorenzo Bianconi }
43417f1de56SFelix Fietkau
43517f1de56SFelix Fietkau return buf;
43617f1de56SFelix Fietkau }
43717f1de56SFelix Fietkau
43817f1de56SFelix Fietkau static void *
mt76_dma_dequeue(struct mt76_dev * dev,struct mt76_queue * q,bool flush,int * len,u32 * info,bool * more,bool * drop)43917f1de56SFelix Fietkau mt76_dma_dequeue(struct mt76_dev *dev, struct mt76_queue *q, bool flush,
440cd372b8cSLorenzo Bianconi int *len, u32 *info, bool *more, bool *drop)
44117f1de56SFelix Fietkau {
44217f1de56SFelix Fietkau int idx = q->tail;
44317f1de56SFelix Fietkau
44417f1de56SFelix Fietkau *more = false;
44517f1de56SFelix Fietkau if (!q->queued)
44617f1de56SFelix Fietkau return NULL;
44717f1de56SFelix Fietkau
4485ffc6b5aSFelix Fietkau if (flush)
4495ffc6b5aSFelix Fietkau q->desc[idx].ctrl |= cpu_to_le32(MT_DMA_CTL_DMA_DONE);
4505ffc6b5aSFelix Fietkau else if (!(q->desc[idx].ctrl & cpu_to_le32(MT_DMA_CTL_DMA_DONE)))
45117f1de56SFelix Fietkau return NULL;
45217f1de56SFelix Fietkau
45317f1de56SFelix Fietkau q->tail = (q->tail + 1) % q->ndesc;
45417f1de56SFelix Fietkau q->queued--;
45517f1de56SFelix Fietkau
456cd372b8cSLorenzo Bianconi return mt76_dma_get_buf(dev, q, idx, len, info, more, drop);
45717f1de56SFelix Fietkau }
45817f1de56SFelix Fietkau
4595ed31128SLorenzo Bianconi static int
mt76_dma_tx_queue_skb_raw(struct mt76_dev * dev,struct mt76_queue * q,struct sk_buff * skb,u32 tx_info)460d95093a1SLorenzo Bianconi mt76_dma_tx_queue_skb_raw(struct mt76_dev *dev, struct mt76_queue *q,
4615ed31128SLorenzo Bianconi struct sk_buff *skb, u32 tx_info)
4625ed31128SLorenzo Bianconi {
463b4403ceeSFelix Fietkau struct mt76_queue_buf buf = {};
4645ed31128SLorenzo Bianconi dma_addr_t addr;
4655ed31128SLorenzo Bianconi
4661e64fdd4SBo Jiao if (test_bit(MT76_MCU_RESET, &dev->phy.state))
4671e64fdd4SBo Jiao goto error;
4681e64fdd4SBo Jiao
46993eaec76SFelix Fietkau if (q->queued + 1 >= q->ndesc - 1)
47093eaec76SFelix Fietkau goto error;
47193eaec76SFelix Fietkau
472d1ddc536SFelix Fietkau addr = dma_map_single(dev->dma_dev, skb->data, skb->len,
4735ed31128SLorenzo Bianconi DMA_TO_DEVICE);
474d1ddc536SFelix Fietkau if (unlikely(dma_mapping_error(dev->dma_dev, addr)))
47593eaec76SFelix Fietkau goto error;
4765ed31128SLorenzo Bianconi
4775ed31128SLorenzo Bianconi buf.addr = addr;
4785ed31128SLorenzo Bianconi buf.len = skb->len;
4795ed31128SLorenzo Bianconi
4805ed31128SLorenzo Bianconi spin_lock_bh(&q->lock);
4815ed31128SLorenzo Bianconi mt76_dma_add_buf(dev, q, &buf, 1, tx_info, skb, NULL);
4825ed31128SLorenzo Bianconi mt76_dma_kick_queue(dev, q);
4835ed31128SLorenzo Bianconi spin_unlock_bh(&q->lock);
4845ed31128SLorenzo Bianconi
4855ed31128SLorenzo Bianconi return 0;
48693eaec76SFelix Fietkau
48793eaec76SFelix Fietkau error:
48893eaec76SFelix Fietkau dev_kfree_skb(skb);
48993eaec76SFelix Fietkau return -ENOMEM;
4905ed31128SLorenzo Bianconi }
4915ed31128SLorenzo Bianconi
492eb9ca7ecSLorenzo Bianconi static int
mt76_dma_tx_queue_skb(struct mt76_dev * dev,struct mt76_queue * q,enum mt76_txq_id qid,struct sk_buff * skb,struct mt76_wcid * wcid,struct ieee80211_sta * sta)49389870594SLorenzo Bianconi mt76_dma_tx_queue_skb(struct mt76_dev *dev, struct mt76_queue *q,
494d08295f5SFelix Fietkau enum mt76_txq_id qid, struct sk_buff *skb,
495d08295f5SFelix Fietkau struct mt76_wcid *wcid, struct ieee80211_sta *sta)
496fcdd99ceSLorenzo Bianconi {
49794e4f579SFelix Fietkau struct ieee80211_tx_status status = {
49894e4f579SFelix Fietkau .sta = sta,
49994e4f579SFelix Fietkau };
500cfaae9e6SLorenzo Bianconi struct mt76_tx_info tx_info = {
501cfaae9e6SLorenzo Bianconi .skb = skb,
502cfaae9e6SLorenzo Bianconi };
503e394b575SFelix Fietkau struct ieee80211_hw *hw;
504b5903c47SLorenzo Bianconi int len, n = 0, ret = -ENOMEM;
505fcdd99ceSLorenzo Bianconi struct mt76_txwi_cache *t;
506fcdd99ceSLorenzo Bianconi struct sk_buff *iter;
507fcdd99ceSLorenzo Bianconi dma_addr_t addr;
508f3950a41SLorenzo Bianconi u8 *txwi;
509fcdd99ceSLorenzo Bianconi
5101e64fdd4SBo Jiao if (test_bit(MT76_RESET, &dev->phy.state))
5111e64fdd4SBo Jiao goto free_skb;
5121e64fdd4SBo Jiao
513fcdd99ceSLorenzo Bianconi t = mt76_get_txwi(dev);
51494e4f579SFelix Fietkau if (!t)
51594e4f579SFelix Fietkau goto free_skb;
51694e4f579SFelix Fietkau
517f3950a41SLorenzo Bianconi txwi = mt76_get_txwi_ptr(dev, t);
518fcdd99ceSLorenzo Bianconi
51988046b2cSFelix Fietkau skb->prev = skb->next = NULL;
5209ec0b821SFelix Fietkau if (dev->drv->drv_flags & MT_DRV_TX_ALIGNED4_SKBS)
52166105538SLorenzo Bianconi mt76_insert_hdr_pad(skb);
52266105538SLorenzo Bianconi
523eb071ba7SLorenzo Bianconi len = skb_headlen(skb);
524d1ddc536SFelix Fietkau addr = dma_map_single(dev->dma_dev, skb->data, len, DMA_TO_DEVICE);
525d1ddc536SFelix Fietkau if (unlikely(dma_mapping_error(dev->dma_dev, addr)))
526fcdd99ceSLorenzo Bianconi goto free;
527fcdd99ceSLorenzo Bianconi
528b5903c47SLorenzo Bianconi tx_info.buf[n].addr = t->dma_addr;
529b5903c47SLorenzo Bianconi tx_info.buf[n++].len = dev->drv->txwi_size;
530b5903c47SLorenzo Bianconi tx_info.buf[n].addr = addr;
531b5903c47SLorenzo Bianconi tx_info.buf[n++].len = len;
532fcdd99ceSLorenzo Bianconi
533fcdd99ceSLorenzo Bianconi skb_walk_frags(skb, iter) {
534b5903c47SLorenzo Bianconi if (n == ARRAY_SIZE(tx_info.buf))
535fcdd99ceSLorenzo Bianconi goto unmap;
536fcdd99ceSLorenzo Bianconi
537d1ddc536SFelix Fietkau addr = dma_map_single(dev->dma_dev, iter->data, iter->len,
538fcdd99ceSLorenzo Bianconi DMA_TO_DEVICE);
539d1ddc536SFelix Fietkau if (unlikely(dma_mapping_error(dev->dma_dev, addr)))
540fcdd99ceSLorenzo Bianconi goto unmap;
541fcdd99ceSLorenzo Bianconi
542b5903c47SLorenzo Bianconi tx_info.buf[n].addr = addr;
543b5903c47SLorenzo Bianconi tx_info.buf[n++].len = iter->len;
544fcdd99ceSLorenzo Bianconi }
545b5903c47SLorenzo Bianconi tx_info.nbuf = n;
546fcdd99ceSLorenzo Bianconi
547ae064fc0SFelix Fietkau if (q->queued + (tx_info.nbuf + 1) / 2 >= q->ndesc - 1) {
548ae064fc0SFelix Fietkau ret = -ENOMEM;
549ae064fc0SFelix Fietkau goto unmap;
550ae064fc0SFelix Fietkau }
551ae064fc0SFelix Fietkau
552d1ddc536SFelix Fietkau dma_sync_single_for_cpu(dev->dma_dev, t->dma_addr, dev->drv->txwi_size,
553eb071ba7SLorenzo Bianconi DMA_TO_DEVICE);
554d08295f5SFelix Fietkau ret = dev->drv->tx_prepare_skb(dev, txwi, qid, wcid, sta, &tx_info);
555d1ddc536SFelix Fietkau dma_sync_single_for_device(dev->dma_dev, t->dma_addr, dev->drv->txwi_size,
556eb071ba7SLorenzo Bianconi DMA_TO_DEVICE);
557eb071ba7SLorenzo Bianconi if (ret < 0)
558fcdd99ceSLorenzo Bianconi goto unmap;
559fcdd99ceSLorenzo Bianconi
560b5903c47SLorenzo Bianconi return mt76_dma_add_buf(dev, q, tx_info.buf, tx_info.nbuf,
561cfaae9e6SLorenzo Bianconi tx_info.info, tx_info.skb, t);
562fcdd99ceSLorenzo Bianconi
563fcdd99ceSLorenzo Bianconi unmap:
564fcdd99ceSLorenzo Bianconi for (n--; n > 0; n--)
565d1ddc536SFelix Fietkau dma_unmap_single(dev->dma_dev, tx_info.buf[n].addr,
566b5903c47SLorenzo Bianconi tx_info.buf[n].len, DMA_TO_DEVICE);
567fcdd99ceSLorenzo Bianconi
568fcdd99ceSLorenzo Bianconi free:
569f0efa862SFelix Fietkau #ifdef CONFIG_NL80211_TESTMODE
570f0efa862SFelix Fietkau /* fix tx_done accounting on queue overflow */
571c918c74dSShayne Chen if (mt76_is_testmode_skb(dev, skb, &hw)) {
572c918c74dSShayne Chen struct mt76_phy *phy = hw->priv;
573c918c74dSShayne Chen
574c918c74dSShayne Chen if (tx_info.skb == phy->test.tx_skb)
575c918c74dSShayne Chen phy->test.tx_done--;
576c918c74dSShayne Chen }
577f0efa862SFelix Fietkau #endif
578f0efa862SFelix Fietkau
579fcdd99ceSLorenzo Bianconi mt76_put_txwi(dev, t);
58094e4f579SFelix Fietkau
58194e4f579SFelix Fietkau free_skb:
58294e4f579SFelix Fietkau status.skb = tx_info.skb;
58394e4f579SFelix Fietkau hw = mt76_tx_status_get_hw(dev, tx_info.skb);
5845b8ccdfbSFelix Fietkau spin_lock_bh(&dev->rx_lock);
58594e4f579SFelix Fietkau ieee80211_tx_status_ext(hw, &status);
5865b8ccdfbSFelix Fietkau spin_unlock_bh(&dev->rx_lock);
58794e4f579SFelix Fietkau
588fcdd99ceSLorenzo Bianconi return ret;
589fcdd99ceSLorenzo Bianconi }
590fcdd99ceSLorenzo Bianconi
59117f1de56SFelix Fietkau static int
mt76_dma_rx_fill(struct mt76_dev * dev,struct mt76_queue * q,bool allow_direct)5922f5c3c77SLorenzo Bianconi mt76_dma_rx_fill(struct mt76_dev *dev, struct mt76_queue *q,
5932f5c3c77SLorenzo Bianconi bool allow_direct)
59417f1de56SFelix Fietkau {
59517f1de56SFelix Fietkau int len = SKB_WITH_OVERHEAD(q->buf_size);
5962f5c3c77SLorenzo Bianconi int frames = 0;
59717f1de56SFelix Fietkau
598f9b627f1SBo Jiao if (!q->ndesc)
599f9b627f1SBo Jiao return 0;
600f9b627f1SBo Jiao
60117f1de56SFelix Fietkau spin_lock_bh(&q->lock);
60217f1de56SFelix Fietkau
60317f1de56SFelix Fietkau while (q->queued < q->ndesc - 1) {
6042f5c3c77SLorenzo Bianconi enum dma_data_direction dir;
60517f1de56SFelix Fietkau struct mt76_queue_buf qbuf;
6062f5c3c77SLorenzo Bianconi dma_addr_t addr;
6072f5c3c77SLorenzo Bianconi int offset;
6082f5c3c77SLorenzo Bianconi void *buf;
60917f1de56SFelix Fietkau
6102f5c3c77SLorenzo Bianconi buf = mt76_get_page_pool_buf(q, &offset, q->buf_size);
61117f1de56SFelix Fietkau if (!buf)
61217f1de56SFelix Fietkau break;
61317f1de56SFelix Fietkau
6142f5c3c77SLorenzo Bianconi addr = page_pool_get_dma_addr(virt_to_head_page(buf)) + offset;
6152f5c3c77SLorenzo Bianconi dir = page_pool_get_dma_dir(q->page_pool);
6162f5c3c77SLorenzo Bianconi dma_sync_single_for_device(dev->dma_dev, addr, len, dir);
61717f1de56SFelix Fietkau
6182f5c3c77SLorenzo Bianconi qbuf.addr = addr + q->buf_offset;
6192f5c3c77SLorenzo Bianconi qbuf.len = len - q->buf_offset;
620577298ecSLorenzo Bianconi qbuf.skip_unmap = false;
621953519b3SFelix Fietkau if (mt76_dma_add_rx_buf(dev, q, &qbuf, buf) < 0) {
6222f5c3c77SLorenzo Bianconi mt76_put_page_pool_buf(buf, allow_direct);
62396f134dcSLorenzo Bianconi break;
62496f134dcSLorenzo Bianconi }
62517f1de56SFelix Fietkau frames++;
62617f1de56SFelix Fietkau }
62717f1de56SFelix Fietkau
62817f1de56SFelix Fietkau if (frames)
62917f1de56SFelix Fietkau mt76_dma_kick_queue(dev, q);
63017f1de56SFelix Fietkau
63117f1de56SFelix Fietkau spin_unlock_bh(&q->lock);
63217f1de56SFelix Fietkau
63317f1de56SFelix Fietkau return frames;
63417f1de56SFelix Fietkau }
63517f1de56SFelix Fietkau
mt76_dma_wed_setup(struct mt76_dev * dev,struct mt76_queue * q,bool reset)6361d5f5d55SSujuan Chen int mt76_dma_wed_setup(struct mt76_dev *dev, struct mt76_queue *q, bool reset)
637f68d6762SFelix Fietkau {
638f68d6762SFelix Fietkau #ifdef CONFIG_NET_MEDIATEK_SOC_WED
639f68d6762SFelix Fietkau struct mtk_wed_device *wed = &dev->mmio.wed;
640f68d6762SFelix Fietkau int ret, type, ring;
6411d5f5d55SSujuan Chen u8 flags;
642f68d6762SFelix Fietkau
6431d5f5d55SSujuan Chen if (!q || !q->ndesc)
6441d5f5d55SSujuan Chen return -EINVAL;
6451d5f5d55SSujuan Chen
6461d5f5d55SSujuan Chen flags = q->flags;
647f68d6762SFelix Fietkau if (!mtk_wed_device_active(wed))
648f68d6762SFelix Fietkau q->flags &= ~MT_QFLAG_WED;
649f68d6762SFelix Fietkau
650f68d6762SFelix Fietkau if (!(q->flags & MT_QFLAG_WED))
651f68d6762SFelix Fietkau return 0;
652f68d6762SFelix Fietkau
653f68d6762SFelix Fietkau type = FIELD_GET(MT_QFLAG_WED_TYPE, q->flags);
654f68d6762SFelix Fietkau ring = FIELD_GET(MT_QFLAG_WED_RING, q->flags);
655f68d6762SFelix Fietkau
656f68d6762SFelix Fietkau switch (type) {
657f68d6762SFelix Fietkau case MT76_WED_Q_TX:
6581d5f5d55SSujuan Chen ret = mtk_wed_device_tx_ring_setup(wed, ring, q->regs, reset);
659f68d6762SFelix Fietkau if (!ret)
660f68d6762SFelix Fietkau q->wed_regs = wed->tx_ring[ring].reg_base;
661f68d6762SFelix Fietkau break;
662f68d6762SFelix Fietkau case MT76_WED_Q_TXFREE:
663f68d6762SFelix Fietkau /* WED txfree queue needs ring to be initialized before setup */
664f68d6762SFelix Fietkau q->flags = 0;
665f68d6762SFelix Fietkau mt76_dma_queue_reset(dev, q);
6662f5c3c77SLorenzo Bianconi mt76_dma_rx_fill(dev, q, false);
667f68d6762SFelix Fietkau q->flags = flags;
668f68d6762SFelix Fietkau
669f68d6762SFelix Fietkau ret = mtk_wed_device_txfree_ring_setup(wed, q->regs);
670f68d6762SFelix Fietkau if (!ret)
671f68d6762SFelix Fietkau q->wed_regs = wed->txfree_ring.reg_base;
672f68d6762SFelix Fietkau break;
67352546e27SLorenzo Bianconi case MT76_WED_Q_RX:
6741d5f5d55SSujuan Chen ret = mtk_wed_device_rx_ring_setup(wed, ring, q->regs, reset);
67552546e27SLorenzo Bianconi if (!ret)
67652546e27SLorenzo Bianconi q->wed_regs = wed->rx_ring[ring].reg_base;
67752546e27SLorenzo Bianconi break;
678f68d6762SFelix Fietkau default:
679f68d6762SFelix Fietkau ret = -EINVAL;
680f68d6762SFelix Fietkau }
681f68d6762SFelix Fietkau
682f68d6762SFelix Fietkau return ret;
683f68d6762SFelix Fietkau #else
684f68d6762SFelix Fietkau return 0;
685f68d6762SFelix Fietkau #endif
686f68d6762SFelix Fietkau }
6871d5f5d55SSujuan Chen EXPORT_SYMBOL_GPL(mt76_dma_wed_setup);
688f68d6762SFelix Fietkau
689f68d6762SFelix 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)690f68d6762SFelix Fietkau mt76_dma_alloc_queue(struct mt76_dev *dev, struct mt76_queue *q,
691f68d6762SFelix Fietkau int idx, int n_desc, int bufsize,
692f68d6762SFelix Fietkau u32 ring_base)
693f68d6762SFelix Fietkau {
694f68d6762SFelix Fietkau int ret, size;
695f68d6762SFelix Fietkau
696f68d6762SFelix Fietkau spin_lock_init(&q->lock);
697f68d6762SFelix Fietkau spin_lock_init(&q->cleanup_lock);
698f68d6762SFelix Fietkau
699f68d6762SFelix Fietkau q->regs = dev->mmio.regs + ring_base + idx * MT_RING_SIZE;
700f68d6762SFelix Fietkau q->ndesc = n_desc;
701f68d6762SFelix Fietkau q->buf_size = bufsize;
702f68d6762SFelix Fietkau q->hw_idx = idx;
703f68d6762SFelix Fietkau
704f68d6762SFelix Fietkau size = q->ndesc * sizeof(struct mt76_desc);
705f68d6762SFelix Fietkau q->desc = dmam_alloc_coherent(dev->dma_dev, size, &q->desc_dma, GFP_KERNEL);
706f68d6762SFelix Fietkau if (!q->desc)
707f68d6762SFelix Fietkau return -ENOMEM;
708f68d6762SFelix Fietkau
709f68d6762SFelix Fietkau size = q->ndesc * sizeof(*q->entry);
710f68d6762SFelix Fietkau q->entry = devm_kzalloc(dev->dev, size, GFP_KERNEL);
711f68d6762SFelix Fietkau if (!q->entry)
712f68d6762SFelix Fietkau return -ENOMEM;
713f68d6762SFelix Fietkau
7142f5c3c77SLorenzo Bianconi ret = mt76_create_page_pool(dev, q);
7152f5c3c77SLorenzo Bianconi if (ret)
7162f5c3c77SLorenzo Bianconi return ret;
7172f5c3c77SLorenzo Bianconi
7181d5f5d55SSujuan Chen ret = mt76_dma_wed_setup(dev, q, false);
719f68d6762SFelix Fietkau if (ret)
720f68d6762SFelix Fietkau return ret;
721f68d6762SFelix Fietkau
722f68d6762SFelix Fietkau if (q->flags != MT_WED_Q_TXFREE)
723f68d6762SFelix Fietkau mt76_dma_queue_reset(dev, q);
724f68d6762SFelix Fietkau
725f68d6762SFelix Fietkau return 0;
726f68d6762SFelix Fietkau }
727f68d6762SFelix Fietkau
72817f1de56SFelix Fietkau static void
mt76_dma_rx_cleanup(struct mt76_dev * dev,struct mt76_queue * q)72917f1de56SFelix Fietkau mt76_dma_rx_cleanup(struct mt76_dev *dev, struct mt76_queue *q)
73017f1de56SFelix Fietkau {
73117f1de56SFelix Fietkau void *buf;
73217f1de56SFelix Fietkau bool more;
73317f1de56SFelix Fietkau
734f9b627f1SBo Jiao if (!q->ndesc)
735f9b627f1SBo Jiao return;
736f9b627f1SBo Jiao
73717f1de56SFelix Fietkau spin_lock_bh(&q->lock);
7381b88b47eSLorenzo Bianconi
73917f1de56SFelix Fietkau do {
740cd372b8cSLorenzo Bianconi buf = mt76_dma_dequeue(dev, q, true, NULL, NULL, &more, NULL);
74117f1de56SFelix Fietkau if (!buf)
74217f1de56SFelix Fietkau break;
74317f1de56SFelix Fietkau
7442f5c3c77SLorenzo Bianconi mt76_put_page_pool_buf(buf, false);
74517f1de56SFelix Fietkau } while (1);
7461b88b47eSLorenzo Bianconi
7471b88b47eSLorenzo Bianconi if (q->rx_head) {
7481b88b47eSLorenzo Bianconi dev_kfree_skb(q->rx_head);
7491b88b47eSLorenzo Bianconi q->rx_head = NULL;
7501b88b47eSLorenzo Bianconi }
7511b88b47eSLorenzo Bianconi
75217f1de56SFelix Fietkau spin_unlock_bh(&q->lock);
75317f1de56SFelix Fietkau }
75417f1de56SFelix Fietkau
75517f1de56SFelix Fietkau static void
mt76_dma_rx_reset(struct mt76_dev * dev,enum mt76_rxq_id qid)75617f1de56SFelix Fietkau mt76_dma_rx_reset(struct mt76_dev *dev, enum mt76_rxq_id qid)
75717f1de56SFelix Fietkau {
75817f1de56SFelix Fietkau struct mt76_queue *q = &dev->q_rx[qid];
75917f1de56SFelix Fietkau int i;
76017f1de56SFelix Fietkau
761f9b627f1SBo Jiao if (!q->ndesc)
762f9b627f1SBo Jiao return;
763f9b627f1SBo Jiao
76417f1de56SFelix Fietkau for (i = 0; i < q->ndesc; i++)
7652703bafcSFelix Fietkau q->desc[i].ctrl = cpu_to_le32(MT_DMA_CTL_DMA_DONE);
76617f1de56SFelix Fietkau
76717f1de56SFelix Fietkau mt76_dma_rx_cleanup(dev, q);
7683bc4b811SSujuan Chen
7693bc4b811SSujuan Chen /* reset WED rx queues */
7703bc4b811SSujuan Chen mt76_dma_wed_setup(dev, q, true);
7713bc4b811SSujuan Chen if (q->flags != MT_WED_Q_TXFREE) {
77217f1de56SFelix Fietkau mt76_dma_sync_idx(dev, q);
7732f5c3c77SLorenzo Bianconi mt76_dma_rx_fill(dev, q, false);
77417f1de56SFelix Fietkau }
7753bc4b811SSujuan Chen }
77617f1de56SFelix Fietkau
77717f1de56SFelix 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)77817f1de56SFelix Fietkau mt76_add_fragment(struct mt76_dev *dev, struct mt76_queue *q, void *data,
779*e4006c5aSFelix Fietkau int len, bool more, u32 info, bool allow_direct)
78017f1de56SFelix Fietkau {
78117f1de56SFelix Fietkau struct sk_buff *skb = q->rx_head;
782b102f0c5SFelix Fietkau struct skb_shared_info *shinfo = skb_shinfo(skb);
783d0bd52c5SLorenzo Bianconi int nr_frags = shinfo->nr_frags;
78417f1de56SFelix Fietkau
785d0bd52c5SLorenzo Bianconi if (nr_frags < ARRAY_SIZE(shinfo->frags)) {
78693a1d479SLorenzo Bianconi struct page *page = virt_to_head_page(data);
78793a1d479SLorenzo Bianconi int offset = data - page_address(page) + q->buf_offset;
78893a1d479SLorenzo Bianconi
789d0bd52c5SLorenzo Bianconi skb_add_rx_frag(skb, nr_frags, page, offset, len, q->buf_size);
79093a1d479SLorenzo Bianconi } else {
791*e4006c5aSFelix Fietkau mt76_put_page_pool_buf(data, allow_direct);
792b102f0c5SFelix Fietkau }
79317f1de56SFelix Fietkau
79417f1de56SFelix Fietkau if (more)
79517f1de56SFelix Fietkau return;
79617f1de56SFelix Fietkau
79717f1de56SFelix Fietkau q->rx_head = NULL;
798d0bd52c5SLorenzo Bianconi if (nr_frags < ARRAY_SIZE(shinfo->frags))
799c3137942SSujuan Chen dev->drv->rx_skb(dev, q - dev->q_rx, skb, &info);
800d0bd52c5SLorenzo Bianconi else
801d0bd52c5SLorenzo Bianconi dev_kfree_skb(skb);
80217f1de56SFelix Fietkau }
80317f1de56SFelix Fietkau
80417f1de56SFelix Fietkau static int
mt76_dma_rx_process(struct mt76_dev * dev,struct mt76_queue * q,int budget)80517f1de56SFelix Fietkau mt76_dma_rx_process(struct mt76_dev *dev, struct mt76_queue *q, int budget)
80617f1de56SFelix Fietkau {
807f68d6762SFelix Fietkau int len, data_len, done = 0, dma_idx;
80817f1de56SFelix Fietkau struct sk_buff *skb;
80917f1de56SFelix Fietkau unsigned char *data;
810f68d6762SFelix Fietkau bool check_ddone = false;
811*e4006c5aSFelix Fietkau bool allow_direct = !mt76_queue_is_wed_rx(q);
81217f1de56SFelix Fietkau bool more;
81317f1de56SFelix Fietkau
814f68d6762SFelix Fietkau if (IS_ENABLED(CONFIG_NET_MEDIATEK_SOC_WED) &&
815f68d6762SFelix Fietkau q->flags == MT_WED_Q_TXFREE) {
816f68d6762SFelix Fietkau dma_idx = Q_READ(dev, q, dma_idx);
817f68d6762SFelix Fietkau check_ddone = true;
818f68d6762SFelix Fietkau }
819f68d6762SFelix Fietkau
82017f1de56SFelix Fietkau while (done < budget) {
821cd372b8cSLorenzo Bianconi bool drop = false;
82217f1de56SFelix Fietkau u32 info;
82317f1de56SFelix Fietkau
824f68d6762SFelix Fietkau if (check_ddone) {
825f68d6762SFelix Fietkau if (q->tail == dma_idx)
826f68d6762SFelix Fietkau dma_idx = Q_READ(dev, q, dma_idx);
827f68d6762SFelix Fietkau
828f68d6762SFelix Fietkau if (q->tail == dma_idx)
829f68d6762SFelix Fietkau break;
830f68d6762SFelix Fietkau }
831f68d6762SFelix Fietkau
832cd372b8cSLorenzo Bianconi data = mt76_dma_dequeue(dev, q, false, &len, &info, &more,
833cd372b8cSLorenzo Bianconi &drop);
83417f1de56SFelix Fietkau if (!data)
83517f1de56SFelix Fietkau break;
83617f1de56SFelix Fietkau
837cd372b8cSLorenzo Bianconi if (drop)
838cd372b8cSLorenzo Bianconi goto free_frag;
839cd372b8cSLorenzo Bianconi
84087e86f90SLorenzo Bianconi if (q->rx_head)
84187e86f90SLorenzo Bianconi data_len = q->buf_size;
84287e86f90SLorenzo Bianconi else
84387e86f90SLorenzo Bianconi data_len = SKB_WITH_OVERHEAD(q->buf_size);
84487e86f90SLorenzo Bianconi
84587e86f90SLorenzo Bianconi if (data_len < len + q->buf_offset) {
8469fe31054SFelix Fietkau dev_kfree_skb(q->rx_head);
8479fe31054SFelix Fietkau q->rx_head = NULL;
848fbe50d9aSFelix Fietkau goto free_frag;
8499fe31054SFelix Fietkau }
8509fe31054SFelix Fietkau
85117f1de56SFelix Fietkau if (q->rx_head) {
852*e4006c5aSFelix Fietkau mt76_add_fragment(dev, q, data, len, more, info,
853*e4006c5aSFelix Fietkau allow_direct);
85417f1de56SFelix Fietkau continue;
85517f1de56SFelix Fietkau }
85617f1de56SFelix Fietkau
857fbe50d9aSFelix Fietkau if (!more && dev->drv->rx_check &&
858fbe50d9aSFelix Fietkau !(dev->drv->rx_check(dev, data, len)))
859fbe50d9aSFelix Fietkau goto free_frag;
860fbe50d9aSFelix Fietkau
861f4d63a87SFelix Fietkau skb = napi_build_skb(data, q->buf_size);
862fbe50d9aSFelix Fietkau if (!skb)
863fbe50d9aSFelix Fietkau goto free_frag;
864fbe50d9aSFelix Fietkau
86517f1de56SFelix Fietkau skb_reserve(skb, q->buf_offset);
8662f5c3c77SLorenzo Bianconi skb_mark_for_recycle(skb);
86717f1de56SFelix Fietkau
868443dc85aSFelix Fietkau *(u32 *)skb->cb = info;
86917f1de56SFelix Fietkau
87017f1de56SFelix Fietkau __skb_put(skb, len);
87117f1de56SFelix Fietkau done++;
87217f1de56SFelix Fietkau
87317f1de56SFelix Fietkau if (more) {
87417f1de56SFelix Fietkau q->rx_head = skb;
87517f1de56SFelix Fietkau continue;
87617f1de56SFelix Fietkau }
87717f1de56SFelix Fietkau
878c3137942SSujuan Chen dev->drv->rx_skb(dev, q - dev->q_rx, skb, &info);
879fbe50d9aSFelix Fietkau continue;
880fbe50d9aSFelix Fietkau
881fbe50d9aSFelix Fietkau free_frag:
882*e4006c5aSFelix Fietkau mt76_put_page_pool_buf(data, allow_direct);
88317f1de56SFelix Fietkau }
88417f1de56SFelix Fietkau
8852f5c3c77SLorenzo Bianconi mt76_dma_rx_fill(dev, q, true);
88617f1de56SFelix Fietkau return done;
88717f1de56SFelix Fietkau }
88817f1de56SFelix Fietkau
mt76_dma_rx_poll(struct napi_struct * napi,int budget)889cb8ed33dSLorenzo Bianconi int mt76_dma_rx_poll(struct napi_struct *napi, int budget)
89017f1de56SFelix Fietkau {
89117f1de56SFelix Fietkau struct mt76_dev *dev;
8922b4307f5SFelix Fietkau int qid, done = 0, cur;
89317f1de56SFelix Fietkau
89417f1de56SFelix Fietkau dev = container_of(napi->dev, struct mt76_dev, napi_dev);
89517f1de56SFelix Fietkau qid = napi - dev->napi;
89617f1de56SFelix Fietkau
8979c68a57bSFelix Fietkau rcu_read_lock();
8989c68a57bSFelix Fietkau
8992b4307f5SFelix Fietkau do {
9002b4307f5SFelix Fietkau cur = mt76_dma_rx_process(dev, &dev->q_rx[qid], budget - done);
90181e850efSLorenzo Bianconi mt76_rx_poll_complete(dev, qid, napi);
9022b4307f5SFelix Fietkau done += cur;
9032b4307f5SFelix Fietkau } while (cur && done < budget);
9042b4307f5SFelix Fietkau
9059c68a57bSFelix Fietkau rcu_read_unlock();
9069c68a57bSFelix Fietkau
9073e0705acSFelix Fietkau if (done < budget && napi_complete(napi))
90817f1de56SFelix Fietkau dev->drv->rx_poll_complete(dev, qid);
90917f1de56SFelix Fietkau
91017f1de56SFelix Fietkau return done;
91117f1de56SFelix Fietkau }
912cb8ed33dSLorenzo Bianconi EXPORT_SYMBOL_GPL(mt76_dma_rx_poll);
91317f1de56SFelix Fietkau
91417f1de56SFelix Fietkau static int
mt76_dma_init(struct mt76_dev * dev,int (* poll)(struct napi_struct * napi,int budget))915cb8ed33dSLorenzo Bianconi mt76_dma_init(struct mt76_dev *dev,
916cb8ed33dSLorenzo Bianconi int (*poll)(struct napi_struct *napi, int budget))
91717f1de56SFelix Fietkau {
91817f1de56SFelix Fietkau int i;
91917f1de56SFelix Fietkau
92017f1de56SFelix Fietkau init_dummy_netdev(&dev->napi_dev);
921aa40528aSFelix Fietkau init_dummy_netdev(&dev->tx_napi_dev);
922aa40528aSFelix Fietkau snprintf(dev->napi_dev.name, sizeof(dev->napi_dev.name), "%s",
923aa40528aSFelix Fietkau wiphy_name(dev->hw->wiphy));
924aa40528aSFelix Fietkau dev->napi_dev.threaded = 1;
92536b7fce1SLorenzo Bianconi init_completion(&dev->mmio.wed_reset);
92636b7fce1SLorenzo Bianconi init_completion(&dev->mmio.wed_reset_complete);
92717f1de56SFelix Fietkau
928f473b42aSFelix Fietkau mt76_for_each_q_rx(dev, i) {
929b48b89f9SJakub Kicinski netif_napi_add(&dev->napi_dev, &dev->napi[i], poll);
9302f5c3c77SLorenzo Bianconi mt76_dma_rx_fill(dev, &dev->q_rx[i], false);
93117f1de56SFelix Fietkau napi_enable(&dev->napi[i]);
93217f1de56SFelix Fietkau }
93317f1de56SFelix Fietkau
93417f1de56SFelix Fietkau return 0;
93517f1de56SFelix Fietkau }
93617f1de56SFelix Fietkau
93717f1de56SFelix Fietkau static const struct mt76_queue_ops mt76_dma_ops = {
93817f1de56SFelix Fietkau .init = mt76_dma_init,
93917f1de56SFelix Fietkau .alloc = mt76_dma_alloc_queue,
9403990465dSLorenzo Bianconi .reset_q = mt76_dma_queue_reset,
9415ed31128SLorenzo Bianconi .tx_queue_skb_raw = mt76_dma_tx_queue_skb_raw,
942469d4818SLorenzo Bianconi .tx_queue_skb = mt76_dma_tx_queue_skb,
94317f1de56SFelix Fietkau .tx_cleanup = mt76_dma_tx_cleanup,
944c001df97SLorenzo Bianconi .rx_cleanup = mt76_dma_rx_cleanup,
94517f1de56SFelix Fietkau .rx_reset = mt76_dma_rx_reset,
94617f1de56SFelix Fietkau .kick = mt76_dma_kick_queue,
94717f1de56SFelix Fietkau };
94817f1de56SFelix Fietkau
mt76_dma_attach(struct mt76_dev * dev)949bceac167SRyder Lee void mt76_dma_attach(struct mt76_dev *dev)
95017f1de56SFelix Fietkau {
95117f1de56SFelix Fietkau dev->queue_ops = &mt76_dma_ops;
95217f1de56SFelix Fietkau }
95317f1de56SFelix Fietkau EXPORT_SYMBOL_GPL(mt76_dma_attach);
95417f1de56SFelix Fietkau
mt76_dma_cleanup(struct mt76_dev * dev)95517f1de56SFelix Fietkau void mt76_dma_cleanup(struct mt76_dev *dev)
95617f1de56SFelix Fietkau {
95717f1de56SFelix Fietkau int i;
95817f1de56SFelix Fietkau
959781eef5bSFelix Fietkau mt76_worker_disable(&dev->tx_worker);
9604875e346SLorenzo Bianconi netif_napi_del(&dev->tx_napi);
961e637763bSLorenzo Bianconi
962dc44c45cSLorenzo Bianconi for (i = 0; i < ARRAY_SIZE(dev->phys); i++) {
963dc44c45cSLorenzo Bianconi struct mt76_phy *phy = dev->phys[i];
964dc44c45cSLorenzo Bianconi int j;
965dc44c45cSLorenzo Bianconi
966dc44c45cSLorenzo Bianconi if (!phy)
967dc44c45cSLorenzo Bianconi continue;
968dc44c45cSLorenzo Bianconi
969dc44c45cSLorenzo Bianconi for (j = 0; j < ARRAY_SIZE(phy->q_tx); j++)
970dc44c45cSLorenzo Bianconi mt76_dma_tx_cleanup(dev, phy->q_tx[j], true);
97191990519SLorenzo Bianconi }
97217f1de56SFelix Fietkau
973e637763bSLorenzo Bianconi for (i = 0; i < ARRAY_SIZE(dev->q_mcu); i++)
974e637763bSLorenzo Bianconi mt76_dma_tx_cleanup(dev, dev->q_mcu[i], true);
975e637763bSLorenzo Bianconi
976f473b42aSFelix Fietkau mt76_for_each_q_rx(dev, i) {
97752546e27SLorenzo Bianconi struct mt76_queue *q = &dev->q_rx[i];
97852546e27SLorenzo Bianconi
97917f1de56SFelix Fietkau netif_napi_del(&dev->napi[i]);
98052546e27SLorenzo Bianconi mt76_dma_rx_cleanup(dev, q);
9812f5c3c77SLorenzo Bianconi
9822f5c3c77SLorenzo Bianconi page_pool_destroy(q->page_pool);
98317f1de56SFelix Fietkau }
984dd57a95cSFelix Fietkau
985dd57a95cSFelix Fietkau mt76_free_pending_txwi(dev);
9862666beceSSujuan Chen mt76_free_pending_rxwi(dev);
987f68d6762SFelix Fietkau
988f68d6762SFelix Fietkau if (mtk_wed_device_active(&dev->mmio.wed))
989f68d6762SFelix Fietkau mtk_wed_device_detach(&dev->mmio.wed);
99017f1de56SFelix Fietkau }
99117f1de56SFelix Fietkau EXPORT_SYMBOL_GPL(mt76_dma_cleanup);
992