23 #include <linux/errno.h>
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/pci.h>
38 static inline struct tsi721_bdma_chan *to_tsi721_chan(
struct dma_chan *
chan)
40 return container_of(chan,
struct tsi721_bdma_chan, dchan);
55 struct tsi721_tx_desc *tsi721_dma_first_active(
56 struct tsi721_bdma_chan *bdma_chan)
59 struct tsi721_tx_desc, desc_node);
62 static int tsi721_bdma_ch_init(
struct tsi721_bdma_chan *bdma_chan)
65 struct device *
dev = bdma_chan->dchan.device->dev;
70 int bd_num = bdma_chan->bd_num;
72 dev_dbg(dev,
"Init Block DMA Engine, CH%d\n", bdma_chan->
id);
75 bd_ptr = dma_zalloc_coherent(dev,
81 bdma_chan->bd_phys = bd_phys;
82 bdma_chan->bd_base = bd_ptr;
84 dev_dbg(dev,
"DMA descriptors @ %p (phys = %llx)\n",
85 bd_ptr, (
unsigned long long)bd_phys);
91 sts_ptr = dma_zalloc_coherent(dev,
99 bdma_chan->bd_base =
NULL;
103 bdma_chan->sts_phys = sts_phys;
104 bdma_chan->sts_base = sts_ptr;
105 bdma_chan->sts_size = sts_size;
108 "desc status FIFO @ %p (phys = %llx) size=0x%x\n",
109 sts_ptr, (
unsigned long long)sts_phys, sts_size);
140 bdma_chan->wr_count = bdma_chan->wr_count_next = 0;
141 bdma_chan->sts_rdptr = 0;
147 static int tsi721_bdma_ch_free(
struct tsi721_bdma_chan *bdma_chan)
151 if (bdma_chan->bd_base ==
NULL)
165 bdma_chan->bd_base, bdma_chan->bd_phys);
166 bdma_chan->bd_base =
NULL;
171 bdma_chan->sts_base, bdma_chan->sts_phys);
172 bdma_chan->sts_base =
NULL;
177 tsi721_bdma_interrupt_enable(
struct tsi721_bdma_chan *bdma_chan,
int enable)
197 static bool tsi721_dma_is_idle(
struct tsi721_bdma_chan *bdma_chan)
210 tasklet_schedule(&bdma_chan->tasklet);
213 #ifdef CONFIG_PCI_MSI
223 struct tsi721_bdma_chan *bdma_chan =
ptr;
231 static void tsi721_start_dma(
struct tsi721_bdma_chan *bdma_chan)
233 if (!tsi721_dma_is_idle(bdma_chan)) {
234 dev_err(bdma_chan->dchan.device->dev,
235 "BUG: Attempt to start non-idle channel\n");
239 if (bdma_chan->wr_count == bdma_chan->wr_count_next) {
240 dev_err(bdma_chan->dchan.device->dev,
241 "BUG: Attempt to start DMA with no BDs ready\n");
245 dev_dbg(bdma_chan->dchan.device->dev,
246 "tx_chan: %p, chan: %d, regs: %p\n",
247 bdma_chan, bdma_chan->dchan.chan_id, bdma_chan->regs);
253 bdma_chan->wr_count = bdma_chan->wr_count_next;
256 static void tsi721_desc_put(
struct tsi721_bdma_chan *bdma_chan,
257 struct tsi721_tx_desc *
desc)
259 dev_dbg(bdma_chan->dchan.device->dev,
260 "Put desc: %p into free list\n", desc);
263 spin_lock_bh(&bdma_chan->lock);
264 list_splice_init(&desc->tx_list, &bdma_chan->free_list);
265 list_add(&desc->desc_node, &bdma_chan->free_list);
266 bdma_chan->wr_count_next = bdma_chan->wr_count;
267 spin_unlock_bh(&bdma_chan->lock);
272 struct tsi721_tx_desc *tsi721_desc_get(
struct tsi721_bdma_chan *bdma_chan)
274 struct tsi721_tx_desc *
tx_desc, *_tx_desc;
275 struct tsi721_tx_desc *
ret =
NULL;
278 spin_lock_bh(&bdma_chan->lock);
280 &bdma_chan->free_list, desc_node) {
281 if (async_tx_test_ack(&tx_desc->txd)) {
286 dev_dbg(bdma_chan->dchan.device->dev,
287 "desc %p not ACKed\n", tx_desc);
290 i = bdma_chan->wr_count_next % bdma_chan->bd_num;
291 if (i == bdma_chan->bd_num - 1) {
293 bdma_chan->wr_count_next++;
296 bdma_chan->wr_count_next++;
297 tx_desc->txd.phys = bdma_chan->bd_phys +
301 spin_unlock_bh(&bdma_chan->lock);
307 tsi721_fill_desc(
struct tsi721_bdma_chan *bdma_chan,
315 dev_err(bdma_chan->dchan.device->dev,
316 "SG element is too large\n");
320 dev_dbg(bdma_chan->dchan.device->dev,
321 "desc: 0x%llx, addr: 0x%llx len: 0x%x\n",
325 dev_dbg(bdma_chan->dchan.device->dev,
326 "bd_ptr = %p did=%d raddr=0x%llx\n",
327 bd_ptr, desc->destid, desc->rio_addr);
331 (rtype << 19) | desc->destid);
336 rio_addr = (desc->rio_addr >> 2) |
337 ((
u64)(desc->rio_addr_u & 0x3) << 62);
343 bd_ptr->
t1.s_dist = 0;
344 bd_ptr->
t1.s_size = 0;
349 static void tsi721_dma_chain_complete(
struct tsi721_bdma_chan *bdma_chan,
350 struct tsi721_tx_desc *desc)
356 list_splice_init(&desc->tx_list, &bdma_chan->free_list);
357 list_move(&desc->desc_node, &bdma_chan->free_list);
358 bdma_chan->completed_cookie = txd->
cookie;
364 static void tsi721_dma_complete_all(
struct tsi721_bdma_chan *bdma_chan)
366 struct tsi721_tx_desc *desc, *
_d;
369 BUG_ON(!tsi721_dma_is_idle(bdma_chan));
371 if (!list_empty(&bdma_chan->queue))
372 tsi721_start_dma(bdma_chan);
374 list_splice_init(&bdma_chan->active_list, &
list);
375 list_splice_init(&bdma_chan->queue, &bdma_chan->active_list);
378 tsi721_dma_chain_complete(bdma_chan, desc);
381 static
void tsi721_clr_stat(
struct tsi721_bdma_chan *bdma_chan)
388 srd_ptr = bdma_chan->sts_rdptr;
389 sts_ptr = bdma_chan->sts_base;
392 for (i = 0; i < 8 && sts_ptr[
j]; i++, j++)
396 srd_ptr %= bdma_chan->sts_size;
401 bdma_chan->sts_rdptr = srd_ptr;
404 static void tsi721_advance_work(
struct tsi721_bdma_chan *bdma_chan)
406 if (list_empty(&bdma_chan->active_list) ||
407 list_is_singular(&bdma_chan->active_list)) {
408 dev_dbg(bdma_chan->dchan.device->dev,
409 "%s: Active_list empty\n", __func__);
410 tsi721_dma_complete_all(bdma_chan);
412 dev_dbg(bdma_chan->dchan.device->dev,
413 "%s: Active_list NOT empty\n", __func__);
414 tsi721_dma_chain_complete(bdma_chan,
415 tsi721_dma_first_active(bdma_chan));
416 tsi721_start_dma(bdma_chan);
420 static void tsi721_dma_tasklet(
unsigned long data)
422 struct tsi721_bdma_chan *bdma_chan = (
struct tsi721_bdma_chan *)data;
423 u32 dmac_int, dmac_sts;
426 dev_dbg(bdma_chan->dchan.device->dev,
"%s: DMAC%d_INT = 0x%x\n",
427 __func__, bdma_chan->id, dmac_int);
433 dev_err(bdma_chan->dchan.device->dev,
434 "%s: DMA ERROR - DMAC%d_STS = 0x%x\n",
435 __func__, bdma_chan->id, dmac_sts);
439 dev_err(bdma_chan->dchan.device->dev,
440 "%s: DMAC%d descriptor status FIFO is full\n",
441 __func__, bdma_chan->id);
445 tsi721_clr_stat(bdma_chan);
446 spin_lock(&bdma_chan->lock);
447 tsi721_advance_work(bdma_chan);
448 spin_unlock(&bdma_chan->lock);
457 struct tsi721_tx_desc *desc = to_tsi721_desc(txd);
458 struct tsi721_bdma_chan *bdma_chan = to_tsi721_chan(txd->
chan);
461 spin_lock_bh(&bdma_chan->lock);
463 cookie = txd->
chan->cookie;
469 if (list_empty(&bdma_chan->active_list)) {
471 tsi721_start_dma(bdma_chan);
476 spin_unlock_bh(&bdma_chan->lock);
480 static int tsi721_alloc_chan_resources(
struct dma_chan *dchan)
482 struct tsi721_bdma_chan *bdma_chan = to_tsi721_chan(dchan);
483 #ifdef CONFIG_PCI_MSI
486 struct tsi721_tx_desc *desc =
NULL;
491 if (bdma_chan->bd_base)
492 return bdma_chan->bd_num - 1;
495 if (tsi721_bdma_ch_init(bdma_chan)) {
497 " channel %d, aborting\n", bdma_chan->id);
502 desc = kcalloc((bdma_chan->bd_num - 1),
sizeof(
struct tsi721_tx_desc),
506 "Failed to allocate logical descriptors\n");
511 bdma_chan->tx_desc = desc;
513 for (i = 0; i < bdma_chan->bd_num - 1; i++) {
515 desc[
i].txd.tx_submit = tsi721_tx_submit;
517 INIT_LIST_HEAD(&desc[i].
tx_list);
521 spin_lock_bh(&bdma_chan->lock);
522 list_splice(&tmp_list, &bdma_chan->free_list);
523 bdma_chan->completed_cookie = dchan->
cookie = 1;
524 spin_unlock_bh(&bdma_chan->lock);
526 #ifdef CONFIG_PCI_MSI
530 priv->msix[TSI721_VECT_DMA0_DONE +
531 bdma_chan->id].vector,
533 priv->msix[TSI721_VECT_DMA0_DONE +
534 bdma_chan->id].irq_name,
539 "Unable to allocate MSI-X interrupt for "
540 "BDMA%d-DONE\n", bdma_chan->id);
545 bdma_chan->id].vector,
547 priv->msix[TSI721_VECT_DMA0_INT +
548 bdma_chan->id].irq_name,
553 "Unable to allocate MSI-X interrupt for "
554 "BDMA%d-INT\n", bdma_chan->id);
556 priv->msix[TSI721_VECT_DMA0_DONE +
557 bdma_chan->id].vector,
565 tasklet_enable(&bdma_chan->tasklet);
566 tsi721_bdma_interrupt_enable(bdma_chan, 1);
568 return bdma_chan->bd_num - 1;
572 tsi721_bdma_ch_free(bdma_chan);
576 static void tsi721_free_chan_resources(
struct dma_chan *dchan)
578 struct tsi721_bdma_chan *bdma_chan = to_tsi721_chan(dchan);
579 #ifdef CONFIG_PCI_MSI
586 if (bdma_chan->bd_base ==
NULL)
589 BUG_ON(!list_empty(&bdma_chan->active_list));
590 BUG_ON(!list_empty(&bdma_chan->queue));
592 tasklet_disable(&bdma_chan->tasklet);
594 spin_lock_bh(&bdma_chan->lock);
595 list_splice_init(&bdma_chan->free_list, &
list);
596 spin_unlock_bh(&bdma_chan->lock);
598 tsi721_bdma_interrupt_enable(bdma_chan, 0);
600 #ifdef CONFIG_PCI_MSI
602 free_irq(priv->msix[TSI721_VECT_DMA0_DONE +
603 bdma_chan->id].vector, (
void *)bdma_chan);
604 free_irq(priv->msix[TSI721_VECT_DMA0_INT +
605 bdma_chan->id].vector, (
void *)bdma_chan);
609 tsi721_bdma_ch_free(bdma_chan);
610 kfree(bdma_chan->tx_desc);
617 struct tsi721_bdma_chan *bdma_chan = to_tsi721_chan(dchan);
622 spin_lock_bh(&bdma_chan->lock);
623 last_completed = bdma_chan->completed_cookie;
624 last_used = dchan->
cookie;
625 spin_unlock_bh(&bdma_chan->lock);
627 ret = dma_async_is_complete(cookie, last_completed, last_used);
629 dma_set_tx_state(txstate, last_completed, last_used, 0);
632 "%s: exit, ret: %d, last_completed: %d, last_used: %d\n",
633 __func__, ret, last_completed, last_used);
638 static void tsi721_issue_pending(
struct dma_chan *dchan)
640 struct tsi721_bdma_chan *bdma_chan = to_tsi721_chan(dchan);
644 if (tsi721_dma_is_idle(bdma_chan)) {
645 spin_lock_bh(&bdma_chan->lock);
646 tsi721_advance_work(bdma_chan);
647 spin_unlock_bh(&bdma_chan->lock);
650 "%s: DMA channel still busy\n", __func__);
659 struct tsi721_bdma_chan *bdma_chan = to_tsi721_chan(dchan);
660 struct tsi721_tx_desc *desc =
NULL;
663 struct rio_dma_ext *rext =
tinfo;
664 u64 rio_addr = rext->rio_addr;
666 u32 sys_size = dma_to_mport(dchan->
device)->sys_size;
669 if (!sgl || !sg_len) {
677 switch (rext->wr_type) {
681 case RDW_ALL_NWRITE_R:
684 case RDW_LAST_NWRITE_R:
691 "%s: Unsupported DMA direction option\n", __func__);
699 desc = tsi721_desc_get(bdma_chan);
702 "Not enough descriptors available\n");
709 desc->interrupt =
false;
711 desc->destid = rext->destid;
712 desc->rio_addr = rio_addr;
713 desc->rio_addr_u = 0;
715 err = tsi721_fill_desc(bdma_chan, desc, sg, rtype, sys_size);
718 "Failed to build desc: %d\n", err);
730 first->txd.cookie = -
EBUSY;
731 desc->txd.flags =
flags;
736 tsi721_desc_put(bdma_chan, first);
743 struct tsi721_bdma_chan *bdma_chan = to_tsi721_chan(dchan);
744 struct tsi721_tx_desc *desc, *
_d;
752 spin_lock_bh(&bdma_chan->lock);
757 list_splice_init(&bdma_chan->active_list, &
list);
758 list_splice_init(&bdma_chan->queue, &
list);
761 tsi721_dma_chain_complete(bdma_chan, desc);
763 spin_unlock_bh(&bdma_chan->lock);
775 mport->dma.dev = &priv->pdev->dev;
776 mport->dma.chancnt = nr_channels;
778 INIT_LIST_HEAD(&mport->dma.channels);
780 for (i = 0; i < nr_channels; i++) {
781 struct tsi721_bdma_chan *bdma_chan = &priv->bdma[
i];
786 bdma_chan->bd_num = 64;
789 bdma_chan->dchan.device = &mport->dma;
790 bdma_chan->dchan.cookie = 1;
791 bdma_chan->dchan.chan_id =
i;
796 INIT_LIST_HEAD(&bdma_chan->active_list);
797 INIT_LIST_HEAD(&bdma_chan->queue);
798 INIT_LIST_HEAD(&bdma_chan->free_list);
801 (
unsigned long)bdma_chan);
802 tasklet_disable(&bdma_chan->tasklet);
804 &mport->dma.channels);
811 mport->dma.device_alloc_chan_resources = tsi721_alloc_chan_resources;
812 mport->dma.device_free_chan_resources = tsi721_free_chan_resources;
813 mport->dma.device_tx_status = tsi721_tx_status;
814 mport->dma.device_issue_pending = tsi721_issue_pending;
815 mport->dma.device_prep_slave_sg = tsi721_prep_rio_sg;
816 mport->dma.device_control = tsi721_device_control;
820 dev_err(&priv->pdev->dev,
"Failed to register DMA device\n");