2 * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <linux/dma-mapping.h>
22 mt76_dma_alloc_queue(struct mt76_dev
*dev
, struct mt76_queue
*q
,
23 int idx
, int n_desc
, int bufsize
,
29 spin_lock_init(&q
->lock
);
31 q
->regs
= dev
->mmio
.regs
+ ring_base
+ idx
* MT_RING_SIZE
;
33 q
->buf_size
= bufsize
;
36 size
= q
->ndesc
* sizeof(struct mt76_desc
);
37 q
->desc
= dmam_alloc_coherent(dev
->dev
, size
, &q
->desc_dma
, GFP_KERNEL
);
41 size
= q
->ndesc
* sizeof(*q
->entry
);
42 q
->entry
= devm_kzalloc(dev
->dev
, size
, GFP_KERNEL
);
46 /* clear descriptors */
47 for (i
= 0; i
< q
->ndesc
; i
++)
48 q
->desc
[i
].ctrl
= cpu_to_le32(MT_DMA_CTL_DMA_DONE
);
50 writel(q
->desc_dma
, &q
->regs
->desc_base
);
51 writel(0, &q
->regs
->cpu_idx
);
52 writel(0, &q
->regs
->dma_idx
);
53 writel(q
->ndesc
, &q
->regs
->ring_size
);
59 mt76_dma_add_buf(struct mt76_dev
*dev
, struct mt76_queue
*q
,
60 struct mt76_queue_buf
*buf
, int nbufs
, u32 info
,
61 struct sk_buff
*skb
, void *txwi
)
63 struct mt76_desc
*desc
;
68 q
->entry
[q
->head
].txwi
= DMA_DUMMY_DATA
;
70 for (i
= 0; i
< nbufs
; i
+= 2, buf
+= 2) {
71 u32 buf0
= buf
[0].addr
, buf1
= 0;
73 ctrl
= FIELD_PREP(MT_DMA_CTL_SD_LEN0
, buf
[0].len
);
76 ctrl
|= FIELD_PREP(MT_DMA_CTL_SD_LEN1
, buf
[1].len
);
80 ctrl
|= MT_DMA_CTL_LAST_SEC0
;
81 else if (i
== nbufs
- 2)
82 ctrl
|= MT_DMA_CTL_LAST_SEC1
;
85 q
->head
= (q
->head
+ 1) % q
->ndesc
;
89 WRITE_ONCE(desc
->buf0
, cpu_to_le32(buf0
));
90 WRITE_ONCE(desc
->buf1
, cpu_to_le32(buf1
));
91 WRITE_ONCE(desc
->info
, cpu_to_le32(info
));
92 WRITE_ONCE(desc
->ctrl
, cpu_to_le32(ctrl
));
97 q
->entry
[idx
].txwi
= txwi
;
98 q
->entry
[idx
].skb
= skb
;
104 mt76_dma_tx_cleanup_idx(struct mt76_dev
*dev
, struct mt76_queue
*q
, int idx
,
105 struct mt76_queue_entry
*prev_e
)
107 struct mt76_queue_entry
*e
= &q
->entry
[idx
];
108 __le32 __ctrl
= READ_ONCE(q
->desc
[idx
].ctrl
);
109 u32 ctrl
= le32_to_cpu(__ctrl
);
111 if (!e
->txwi
|| !e
->skb
) {
112 __le32 addr
= READ_ONCE(q
->desc
[idx
].buf0
);
113 u32 len
= FIELD_GET(MT_DMA_CTL_SD_LEN0
, ctrl
);
115 dma_unmap_single(dev
->dev
, le32_to_cpu(addr
), len
,
119 if (!(ctrl
& MT_DMA_CTL_LAST_SEC0
)) {
120 __le32 addr
= READ_ONCE(q
->desc
[idx
].buf1
);
121 u32 len
= FIELD_GET(MT_DMA_CTL_SD_LEN1
, ctrl
);
123 dma_unmap_single(dev
->dev
, le32_to_cpu(addr
), len
,
127 if (e
->txwi
== DMA_DUMMY_DATA
)
130 if (e
->skb
== DMA_DUMMY_DATA
)
134 memset(e
, 0, sizeof(*e
));
138 mt76_dma_sync_idx(struct mt76_dev
*dev
, struct mt76_queue
*q
)
140 writel(q
->desc_dma
, &q
->regs
->desc_base
);
141 writel(q
->ndesc
, &q
->regs
->ring_size
);
142 q
->head
= readl(&q
->regs
->dma_idx
);
144 writel(q
->head
, &q
->regs
->cpu_idx
);
148 mt76_dma_tx_cleanup(struct mt76_dev
*dev
, enum mt76_txq_id qid
, bool flush
)
150 struct mt76_sw_queue
*sq
= &dev
->q_tx
[qid
];
151 struct mt76_queue
*q
= sq
->q
;
152 struct mt76_queue_entry entry
;
153 unsigned int n_swq_queued
[4] = {};
154 unsigned int n_queued
= 0;
164 last
= readl(&q
->regs
->dma_idx
);
166 while ((q
->queued
> n_queued
) && q
->tail
!= last
) {
167 mt76_dma_tx_cleanup_idx(dev
, q
, q
->tail
, &entry
);
169 n_swq_queued
[entry
.qid
]++;
171 q
->tail
= (q
->tail
+ 1) % q
->ndesc
;
175 dev
->drv
->tx_complete_skb(dev
, qid
, &entry
);
178 if (!(dev
->drv
->txwi_flags
& MT_TXWI_NO_FREE
))
179 mt76_put_txwi(dev
, entry
.txwi
);
183 if (!flush
&& q
->tail
== last
)
184 last
= readl(&q
->regs
->dma_idx
);
187 spin_lock_bh(&q
->lock
);
189 q
->queued
-= n_queued
;
190 for (i
= 0; i
< ARRAY_SIZE(n_swq_queued
); i
++) {
191 if (!n_swq_queued
[i
])
194 dev
->q_tx
[i
].swq_queued
-= n_swq_queued
[i
];
198 mt76_dma_sync_idx(dev
, q
);
200 wake
= wake
&& q
->stopped
&&
201 qid
< IEEE80211_NUM_ACS
&& q
->queued
< q
->ndesc
- 8;
206 wake_up(&dev
->tx_wait
);
208 spin_unlock_bh(&q
->lock
);
211 ieee80211_wake_queue(dev
->hw
, qid
);
215 mt76_dma_get_buf(struct mt76_dev
*dev
, struct mt76_queue
*q
, int idx
,
216 int *len
, u32
*info
, bool *more
)
218 struct mt76_queue_entry
*e
= &q
->entry
[idx
];
219 struct mt76_desc
*desc
= &q
->desc
[idx
];
222 int buf_len
= SKB_WITH_OVERHEAD(q
->buf_size
);
224 buf_addr
= le32_to_cpu(READ_ONCE(desc
->buf0
));
226 u32 ctl
= le32_to_cpu(READ_ONCE(desc
->ctrl
));
227 *len
= FIELD_GET(MT_DMA_CTL_SD_LEN0
, ctl
);
228 *more
= !(ctl
& MT_DMA_CTL_LAST_SEC0
);
232 *info
= le32_to_cpu(desc
->info
);
234 dma_unmap_single(dev
->dev
, buf_addr
, buf_len
, DMA_FROM_DEVICE
);
241 mt76_dma_dequeue(struct mt76_dev
*dev
, struct mt76_queue
*q
, bool flush
,
242 int *len
, u32
*info
, bool *more
)
250 if (!flush
&& !(q
->desc
[idx
].ctrl
& cpu_to_le32(MT_DMA_CTL_DMA_DONE
)))
253 q
->tail
= (q
->tail
+ 1) % q
->ndesc
;
256 return mt76_dma_get_buf(dev
, q
, idx
, len
, info
, more
);
260 mt76_dma_kick_queue(struct mt76_dev
*dev
, struct mt76_queue
*q
)
262 writel(q
->head
, &q
->regs
->cpu_idx
);
266 mt76_dma_tx_queue_skb_raw(struct mt76_dev
*dev
, enum mt76_txq_id qid
,
267 struct sk_buff
*skb
, u32 tx_info
)
269 struct mt76_queue
*q
= dev
->q_tx
[qid
].q
;
270 struct mt76_queue_buf buf
;
273 addr
= dma_map_single(dev
->dev
, skb
->data
, skb
->len
,
275 if (unlikely(dma_mapping_error(dev
->dev
, addr
)))
281 spin_lock_bh(&q
->lock
);
282 mt76_dma_add_buf(dev
, q
, &buf
, 1, tx_info
, skb
, NULL
);
283 mt76_dma_kick_queue(dev
, q
);
284 spin_unlock_bh(&q
->lock
);
290 mt76_dma_tx_queue_skb(struct mt76_dev
*dev
, enum mt76_txq_id qid
,
291 struct sk_buff
*skb
, struct mt76_wcid
*wcid
,
292 struct ieee80211_sta
*sta
)
294 struct mt76_queue
*q
= dev
->q_tx
[qid
].q
;
295 struct mt76_tx_info tx_info
= {
298 int len
, n
= 0, ret
= -ENOMEM
;
299 struct mt76_queue_entry e
;
300 struct mt76_txwi_cache
*t
;
301 struct sk_buff
*iter
;
305 t
= mt76_get_txwi(dev
);
307 ieee80211_free_txskb(dev
->hw
, skb
);
310 txwi
= mt76_get_txwi_ptr(dev
, t
);
312 skb
->prev
= skb
->next
= NULL
;
313 if (dev
->drv
->tx_aligned4_skbs
)
314 mt76_insert_hdr_pad(skb
);
316 len
= skb_headlen(skb
);
317 addr
= dma_map_single(dev
->dev
, skb
->data
, len
, DMA_TO_DEVICE
);
318 if (unlikely(dma_mapping_error(dev
->dev
, addr
)))
321 tx_info
.buf
[n
].addr
= t
->dma_addr
;
322 tx_info
.buf
[n
++].len
= dev
->drv
->txwi_size
;
323 tx_info
.buf
[n
].addr
= addr
;
324 tx_info
.buf
[n
++].len
= len
;
326 skb_walk_frags(skb
, iter
) {
327 if (n
== ARRAY_SIZE(tx_info
.buf
))
330 addr
= dma_map_single(dev
->dev
, iter
->data
, iter
->len
,
332 if (unlikely(dma_mapping_error(dev
->dev
, addr
)))
335 tx_info
.buf
[n
].addr
= addr
;
336 tx_info
.buf
[n
++].len
= iter
->len
;
340 dma_sync_single_for_cpu(dev
->dev
, t
->dma_addr
, dev
->drv
->txwi_size
,
342 ret
= dev
->drv
->tx_prepare_skb(dev
, txwi
, qid
, wcid
, sta
, &tx_info
);
343 dma_sync_single_for_device(dev
->dev
, t
->dma_addr
, dev
->drv
->txwi_size
,
348 if (q
->queued
+ (tx_info
.nbuf
+ 1) / 2 >= q
->ndesc
- 1) {
353 return mt76_dma_add_buf(dev
, q
, tx_info
.buf
, tx_info
.nbuf
,
354 tx_info
.info
, tx_info
.skb
, t
);
357 for (n
--; n
> 0; n
--)
358 dma_unmap_single(dev
->dev
, tx_info
.buf
[n
].addr
,
359 tx_info
.buf
[n
].len
, DMA_TO_DEVICE
);
364 dev
->drv
->tx_complete_skb(dev
, qid
, &e
);
365 mt76_put_txwi(dev
, t
);
370 mt76_dma_rx_fill(struct mt76_dev
*dev
, struct mt76_queue
*q
)
375 int len
= SKB_WITH_OVERHEAD(q
->buf_size
);
376 int offset
= q
->buf_offset
;
379 spin_lock_bh(&q
->lock
);
381 while (q
->queued
< q
->ndesc
- 1) {
382 struct mt76_queue_buf qbuf
;
384 buf
= page_frag_alloc(&q
->rx_page
, q
->buf_size
, GFP_ATOMIC
);
388 addr
= dma_map_single(dev
->dev
, buf
, len
, DMA_FROM_DEVICE
);
389 if (unlikely(dma_mapping_error(dev
->dev
, addr
))) {
394 qbuf
.addr
= addr
+ offset
;
395 qbuf
.len
= len
- offset
;
396 idx
= mt76_dma_add_buf(dev
, q
, &qbuf
, 1, 0, buf
, NULL
);
401 mt76_dma_kick_queue(dev
, q
);
403 spin_unlock_bh(&q
->lock
);
409 mt76_dma_rx_cleanup(struct mt76_dev
*dev
, struct mt76_queue
*q
)
415 spin_lock_bh(&q
->lock
);
417 buf
= mt76_dma_dequeue(dev
, q
, true, NULL
, NULL
, &more
);
423 spin_unlock_bh(&q
->lock
);
428 page
= virt_to_page(q
->rx_page
.va
);
429 __page_frag_cache_drain(page
, q
->rx_page
.pagecnt_bias
);
430 memset(&q
->rx_page
, 0, sizeof(q
->rx_page
));
434 mt76_dma_rx_reset(struct mt76_dev
*dev
, enum mt76_rxq_id qid
)
436 struct mt76_queue
*q
= &dev
->q_rx
[qid
];
439 for (i
= 0; i
< q
->ndesc
; i
++)
440 q
->desc
[i
].ctrl
&= ~cpu_to_le32(MT_DMA_CTL_DMA_DONE
);
442 mt76_dma_rx_cleanup(dev
, q
);
443 mt76_dma_sync_idx(dev
, q
);
444 mt76_dma_rx_fill(dev
, q
);
448 mt76_add_fragment(struct mt76_dev
*dev
, struct mt76_queue
*q
, void *data
,
451 struct page
*page
= virt_to_head_page(data
);
452 int offset
= data
- page_address(page
);
453 struct sk_buff
*skb
= q
->rx_head
;
455 offset
+= q
->buf_offset
;
456 skb_add_rx_frag(skb
, skb_shinfo(skb
)->nr_frags
, page
, offset
, len
,
463 dev
->drv
->rx_skb(dev
, q
- dev
->q_rx
, skb
);
467 mt76_dma_rx_process(struct mt76_dev
*dev
, struct mt76_queue
*q
, int budget
)
469 int len
, data_len
, done
= 0;
474 while (done
< budget
) {
477 data
= mt76_dma_dequeue(dev
, q
, false, &len
, &info
, &more
);
482 data_len
= q
->buf_size
;
484 data_len
= SKB_WITH_OVERHEAD(q
->buf_size
);
486 if (data_len
< len
+ q
->buf_offset
) {
487 dev_kfree_skb(q
->rx_head
);
495 mt76_add_fragment(dev
, q
, data
, len
, more
);
499 skb
= build_skb(data
, q
->buf_size
);
504 skb_reserve(skb
, q
->buf_offset
);
506 if (q
== &dev
->q_rx
[MT_RXQ_MCU
]) {
507 u32
*rxfce
= (u32
*) skb
->cb
;
519 dev
->drv
->rx_skb(dev
, q
- dev
->q_rx
, skb
);
522 mt76_dma_rx_fill(dev
, q
);
527 mt76_dma_rx_poll(struct napi_struct
*napi
, int budget
)
529 struct mt76_dev
*dev
;
530 int qid
, done
= 0, cur
;
532 dev
= container_of(napi
->dev
, struct mt76_dev
, napi_dev
);
533 qid
= napi
- dev
->napi
;
538 cur
= mt76_dma_rx_process(dev
, &dev
->q_rx
[qid
], budget
- done
);
539 mt76_rx_poll_complete(dev
, qid
, napi
);
541 } while (cur
&& done
< budget
);
547 dev
->drv
->rx_poll_complete(dev
, qid
);
554 mt76_dma_init(struct mt76_dev
*dev
)
558 init_dummy_netdev(&dev
->napi_dev
);
560 for (i
= 0; i
< ARRAY_SIZE(dev
->q_rx
); i
++) {
561 netif_napi_add(&dev
->napi_dev
, &dev
->napi
[i
], mt76_dma_rx_poll
,
563 mt76_dma_rx_fill(dev
, &dev
->q_rx
[i
]);
564 skb_queue_head_init(&dev
->rx_skb
[i
]);
565 napi_enable(&dev
->napi
[i
]);
571 static const struct mt76_queue_ops mt76_dma_ops
= {
572 .init
= mt76_dma_init
,
573 .alloc
= mt76_dma_alloc_queue
,
574 .tx_queue_skb_raw
= mt76_dma_tx_queue_skb_raw
,
575 .tx_queue_skb
= mt76_dma_tx_queue_skb
,
576 .tx_cleanup
= mt76_dma_tx_cleanup
,
577 .rx_reset
= mt76_dma_rx_reset
,
578 .kick
= mt76_dma_kick_queue
,
581 void mt76_dma_attach(struct mt76_dev
*dev
)
583 dev
->queue_ops
= &mt76_dma_ops
;
585 EXPORT_SYMBOL_GPL(mt76_dma_attach
);
587 void mt76_dma_cleanup(struct mt76_dev
*dev
)
591 for (i
= 0; i
< ARRAY_SIZE(dev
->q_tx
); i
++)
592 mt76_dma_tx_cleanup(dev
, i
, true);
594 for (i
= 0; i
< ARRAY_SIZE(dev
->q_rx
); i
++) {
595 netif_napi_del(&dev
->napi
[i
]);
596 mt76_dma_rx_cleanup(dev
, &dev
->q_rx
[i
]);
599 EXPORT_SYMBOL_GPL(mt76_dma_cleanup
);