Switchtec Switch DMA Engine Driver
Subsystems (2)
Kelvin Cao
maintainerv2 cover Switchtec Switch DMA Engine Driver
Hi,
This is v2 of the Switchtec Switch DMA Engine Driver, incorporating changes for the review comments of the initial post.
Changes in v2:
- Move put_device(dma_dev->dev) before kfree(swdma_dev) as dma_dev is part of swdma_dev.
- Convert dev_ print calls to pci_ print calls to make the use of print functions consistent within switchtec_dma_create().
- Remove some dev_ print calls, which use device pointer as handles, to ensure there's no reference issue when the device is unbound.
- Remove unused .driver_data from pci_device_id structure.
Kelvin Cao (1):
dmaengine: switchtec-dma: Introduce Switchtec DMA engine PCI driver
MAINTAINERS | 5 + drivers/dma/Kconfig | 9 + drivers/dma/Makefile | 1 + drivers/dma/switchtec_dma.c | 1734 +++++++++++++++++++++++++++++++++++ 4 files changed, 1749 insertions(+) create mode 100644 drivers/dma/switchtec_dma.c
Signature · 1 line
2.25.1
Kelvin Cao
maintainerv2 cover Switchtec Switch DMA Engine Driver
Hi,
This is v2 of the Switchtec Switch DMA Engine Driver, incorporating changes for the review comments of the initial post.
Changes in v2:
- Move put_device(dma_dev->dev) before kfree(swdma_dev) as dma_dev is part of swdma_dev.
- Convert dev_ print calls to pci_ print calls to make the use of print functions consistent within switchtec_dma_create().
- Remove some dev_ print calls, which use device pointer as handles, to ensure there's no reference issue when the device is unbound.
- Remove unused .driver_data from pci_device_id structure.
Kelvin Cao (1):
dmaengine: switchtec-dma: Introduce Switchtec DMA engine PCI driver
MAINTAINERS | 5 + drivers/dma/Kconfig | 9 + drivers/dma/Makefile | 1 + drivers/dma/switchtec_dma.c | 1734 +++++++++++++++++++++++++++++++++++ 4 files changed, 1749 insertions(+) create mode 100644 drivers/dma/switchtec_dma.c
Signature · 1 line
2.25.1
Kelvin Cao
maintainerPATCH v2 1/1 dmaengine: switchtec-dma: Introduce Switchtec DMA engine PCI driver
In reply to Kelvin CaoSome Switchtec Switches can expose DMA engines via extra PCI functions on the upstream ports. At most one such function can be supported on each upstream port. Each function can have one or more DMA channels.
Implement core PCI driver skeleton and register DMA engine callbacks.
--- MAINTAINERS | 5 + drivers/dma/Kconfig | 9 + drivers/dma/Makefile | 1 + drivers/dma/switchtec_dma.c | 1734 +++++++++++++++++++++++++++++++++++ 4 files changed, 1749 insertions(+) create mode 100644 drivers/dma/switchtec_dma.c
MAINTAINERS +5 -0
@@ -20158,6 +20158,11 @@ S: Supported F: include/net/switchdev.h F: net/switchdev/ +SWITCHTEC DMA DRIVER +M: Kelvin Cao <[email protected]> +S: Maintained +F: drivers/dma/switchtec_dma.c + SY8106A REGULATOR DRIVER M: Icenowy Zheng <[email protected]> S: Maintained
drivers/dma/Kconfig +9 -0
@@ -748,6 +748,15 @@ config XILINX_ZYNQMP_DPDMA driver provides the dmaengine required by the DisplayPort subsystem display driver. +config SWITCHTEC_DMA + tristate "Switchtec PSX/PFX Switch DMA Engine Support" + depends on PCI + select DMA_ENGINE + help + Some Switchtec PSX/PFX PCIe Switches support additional DMA engines. + These are exposed via an extra function on the switch's upstream + port. + # driver files source "drivers/dma/bestcomm/Kconfig"
drivers/dma/Makefile +1 -0
@@ -81,6 +81,7 @@ obj-$(CONFIG_XGENE_DMA) += xgene-dma.o obj-$(CONFIG_ST_FDMA) += st_fdma.o obj-$(CONFIG_FSL_DPAA2_QDMA) += fsl-dpaa2-qdma/ obj-$(CONFIG_INTEL_LDMA) += lgm/ +obj-$(CONFIG_SWITCHTEC_DMA) += switchtec_dma.o obj-y += mediatek/ obj-y += qcom/
drivers/dma/switchtec_dma.c new file +1734 -0
1,734 lines not on this page · raw message
Signature · 1 line
2.25.1
Re: [PATCH v2 1/1] dmaengine: switchtec-dma: Introduce Switchtec DMA engine PCI driver
Le 03/04/2023 à 20:06, Kelvin Cao a écrit :
Quoted 9 lines
Some Switchtec Switches can expose DMA engines via extra PCI functions on the upstream ports. At most one such function can be supported on each upstream port. Each function can have one or more DMA channels.
Implement core PCI driver skeleton and register DMA engine callbacks.
Signed-off-by Kelvin Cao <[email protected]>Co-developed-by George Ge <[email protected]>Signed-off-by George Ge <[email protected]>
Quoted 8 lines
--- MAINTAINERS | 5 + drivers/dma/Kconfig | 9 + drivers/dma/Makefile | 1 + drivers/dma/switchtec_dma.c | 1734 +++++++++++++++++++++++++++++++++++ 4 files changed, 1749 insertions(+)create mode 100644
drivers/dma/switchtec_dma.c
Hi,
just a few nit, should you find them useful.
[...]
drivers/dma/switchtec_dma.c line 655
Quoted 12 lines
drivers/dma/switchtec_dma.c new file +12
-0+static void switchtec_dma_process_desc (struct switchtec_dma_chan *swdma_chan) +{ +struct device *chan_dev =to_chan_dev (swdma_chan); +struct dmaengine_result res; +struct switchtec_dma_desc *desc; +struct switchtec_dma_hw_ce *ce; +__le16 phase_tag; +int tail; +int cid; +int se_idx; +u32 sts_code; +int i =0 ;
No need to init.
[...]
Quoted 22 lines
drivers/dma/switchtec_dma.c new file +22
-0+static int switchtec_dma_alloc_desc (struct switchtec_dma_chan *swdma_chan) +{ +struct switchtec_dma_dev *swdma_dev = swdma_chan->swdma_dev; +struct pci_dev *pdev; +struct chan_fw_regs __iomem *chan_fw = swdma_chan->mmio_chan_fw; +size_t size; +struct switchtec_dma_desc *desc; +int rc; +int i; + + swdma_chan->head = swdma_chan->tail =0 ; + swdma_chan->cq_tail =0 ; + + size =SWITCHTEC_DMA_SQ_SIZE *sizeof (*swdma_chan->hw_sq); + swdma_chan->hw_sq =dma_alloc_coherent (swdma_dev->dma_dev.dev, size, + &swdma_chan->dma_addr_sq, +GFP_KERNEL ); +if (!swdma_chan->hw_sq) { + rc = -ENOMEM ; + goto free_and_exit; + } +
drivers/dma/switchtec_dma.c line 1161
Quoted 10 lines
drivers/dma/switchtec_dma.c new file +10
-0+ size =SWITCHTEC_DMA_CQ_SIZE *sizeof (*swdma_chan->hw_cq); + swdma_chan->hw_cq =dma_alloc_coherent (swdma_dev->dma_dev.dev, size, + &swdma_chan->dma_addr_cq, +GFP_KERNEL ); +if (!swdma_chan->hw_cq) { + rc = -ENOMEM ; + goto free_and_exit; + } + +memset (swdma_chan->hw_cq,0 , size);
The memory allocated with dma_alloc_coherent() is already zeroed.
drivers/dma/switchtec_dma.c line 1174
Quoted 13 lines
drivers/dma/switchtec_dma.c new file +13
-0+ +/* reset host phase tag */ + swdma_chan->phase_tag =0 ; + + size =sizeof (*swdma_chan->desc_ring); + swdma_chan->desc_ring =kcalloc (SWITCHTEC_DMA_RING_SIZE , size, +GFP_KERNEL ); +if (!swdma_chan->desc_ring) { + rc = -ENOMEM ; + goto free_and_exit; + } + +memset (swdma_chan->desc_ring,0 ,SWITCHTEC_DMA_RING_SIZE * size);
This is already kcalloc()'ed.
CJ
[email protected]
maintainerRe: [PATCH v2 1/1] dmaengine: switchtec-dma: Introduce Switchtec DMA engine PCI driver
Quoted 109 lines · Christophe JAILLET
EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe
Quoted 13 lines · Kelvin Cao
Some Switchtec Switches can expose DMA engines via extra PCI functions
on the upstream ports. At most one such function can be supported on
each upstream port. Each function can have one or more DMA channels.Implement core PCI driver skeleton and register DMA engine callbacks.
Signed-off-by Kelvin Cao <[email protected]>Co-developed-by George Ge <[email protected]>Signed-off-by George Ge <[email protected]>Quoted 9 lines
--- MAINTAINERS | 5 + drivers/dma/Kconfig | 9 + drivers/dma/Makefile | 1 + drivers/dma/switchtec_dma.c | 1734 +++++++++++++++++++++++++++++++++++ 4 files changed, 1749 insertions(+)create mode 100644
drivers/dma/switchtec_dma.cHi,
just a few nit, should you find them useful.[...]
drivers/dma/switchtec_dma.c line 655
Quoted 13 lines
+static void
switchtec_dma_process_desc(struct switchtec_dma_chan *swdma_chan)drivers/dma/switchtec_dma.c new file +11
-0+{ +struct device *chan_dev =to_chan_dev (swdma_chan); +struct dmaengine_result res; +struct switchtec_dma_desc *desc; +struct switchtec_dma_hw_ce *ce; +__le16 phase_tag; +int tail; +int cid; +int se_idx; +u32 sts_code; +int i =0 ;No need to init.
[...]
Quoted 31 lines
+static int
switchtec_dma_alloc_desc(struct switchtec_dma_chan *swdma_chan)drivers/dma/switchtec_dma.c new file +4
-0+{ +struct switchtec_dma_dev *swdma_dev = swdma_chan->swdma_dev; +struct pci_dev *pdev; + struct chan_fw_regs __iomem *chan_fw = swdma_chan-Quoted 1 line
mmio_chan_fw;drivers/dma/switchtec_dma.c new file +10
-0+size_t size; +struct switchtec_dma_desc *desc; +int rc; +int i; + + swdma_chan->head = swdma_chan->tail =0 ; + swdma_chan->cq_tail =0 ; + + size =SWITCHTEC_DMA_SQ_SIZE *sizeof (*swdma_chan->hw_sq); + swdma_chan->hw_sq = dma_alloc_coherent(swdma_dev-Quoted 1 line
dma_dev.dev, size,
+ &swdma_chan-
Quoted 1 line
dma_addr_sq,drivers/dma/switchtec_dma.c new file +8
-0+ GFP_KERNEL); +if (!swdma_chan->hw_sq) { + rc = -ENOMEM ; + goto free_and_exit; + } + + size =SWITCHTEC_DMA_CQ_SIZE *sizeof (*swdma_chan->hw_cq); + swdma_chan->hw_cq = dma_alloc_coherent(swdma_dev-Quoted 1 line
dma_dev.dev, size,
+ &swdma_chan-
Quoted 1 line
dma_addr_cq,drivers/dma/switchtec_dma.c line 1161
Quoted 7 lines
drivers/dma/switchtec_dma.c new file +7
-0+ GFP_KERNEL); +if (!swdma_chan->hw_cq) { + rc = -ENOMEM ; + goto free_and_exit; + } + +memset (swdma_chan->hw_cq,0 , size);The memory allocated with
dma_alloc_coherent()is already zeroed.Quoted 14 lines
drivers/dma/switchtec_dma.c new file +6
-0+ +/* reset host phase tag */ + swdma_chan->phase_tag =0 ; + + size =sizeof (*swdma_chan->desc_ring); + swdma_chan->desc_ring = kcalloc(SWITCHTEC_DMA_RING_SIZE,size,
drivers/dma/switchtec_dma.c new file +7
-0+ GFP_KERNEL); +if (!swdma_chan->desc_ring) { + rc = -ENOMEM ; + goto free_and_exit; + } + + memset(swdma_chan->desc_ring, 0, SWITCHTEC_DMA_RING_SIZE *Quoted 1 line
size);
This is already kcalloc()'ed.
Quoted 1 line
CJ
Hi CJ,
Thanks for the review, will fix them in the next version.
Kelvin
Re: [PATCH v2 1/1] dmaengine: switchtec-dma: Introduce Switchtec DMA engine PCI driver
In reply to Kelvin CaoQuoted 5 lines · Kelvin Cao
drivers/dma/switchtec_dma.c new file +5
-0+#define HALT_RETRY 100 +static int halt_channel (struct switchtec_dma_chan *swdma_chan) +{ +u32 status; +struct chan_hw_regs __iomem *chan_hw = swdma_chan->mmio_chan_hw;
drivers/dma/switchtec_dma.c line 393
Quoted 10 lines
drivers/dma/switchtec_dma.c new file +10
-0+int retry =HALT_RETRY ; +struct pci_dev *pdev; +int ret; + +rcu_read_lock (); + pdev =rcu_dereference (swdma_chan->swdma_dev->pdev); +if (!pdev) { + ret = -ENODEV ; + goto unlock_and_exit; + }
This whole RCU critical section around every access to ->pdev scheme looks a bit bothersome to me. This means that all the low-level PCI ops are done in RCU critical section. Is this something you came up with or is it copied from other drivers?
Normally we'd do an unregistration from the dmaengine subsystem first, which might do a RCU synchronization at a high level, and then we're sure that none of the methods gets called again on the unregistered device.
Can't this driver (and the dmaengine core) support an operation mode where you set a shutdown flag at the beginning
Quoted 6 lines
drivers/dma/switchtec_dma.c new file +6
-0+ +spin_lock (&swdma_chan->hw_ctrl_lock); +writeb (SWITCHTEC_CHAN_CTRL_HALT , &chan_hw->ctrl); + + ret = -EIO ; + do {
drivers/dma/switchtec_dma.c line 409
Quoted 10 lines
drivers/dma/switchtec_dma.c new file +10
-0+ status =readl (&chan_hw->status); + +if (status &SWITCHTEC_CHAN_STS_HALTED ) { + ret =0 ; +break ; + } + +udelay (1000 ); + }while (retry--); +spin_unlock (&swdma_chan->hw_ctrl_lock);
Why is the lock needed while reading the status and waiting for it with long delays?
drivers/dma/switchtec_dma.c line 477
Quoted 10 lines
drivers/dma/switchtec_dma.c new file +10
-0+spin_lock (&swdma_chan->hw_ctrl_lock); + ctrl =SWITCHTEC_CHAN_CTRL_RESET ; + ctrl |=SWITCHTEC_CHAN_CTRL_ERR_PAUSE ; +writel (ctrl, &chan_hw->ctrl); + +udelay (1000 ); + + ctrl =SWITCHTEC_CHAN_CTRL_ERR_PAUSE ; +writel (ctrl, &chan_hw->ctrl); +spin_unlock (&swdma_chan->hw_ctrl_lock);
This looks broken. PCIe MMIO Write TLPs an might not never reach the device until you do a read from it. So instead of the udelay you probably need to do a read* operation to some register that has no side effects.
Also what is the point of the ctrl local variable? Just passing the values directly would be shorted and easier to read.
drivers/dma/switchtec_dma.c line 502
Quoted 8 lines
drivers/dma/switchtec_dma.c new file +8
-0+spin_lock (&swdma_chan->hw_ctrl_lock); +writeb (SWITCHTEC_CHAN_CTRL_PAUSE , &chan_hw->ctrl); +spin_unlock (&swdma_chan->hw_ctrl_lock); + +rcu_read_unlock (); + +/* wait 60ms to ensure no pending CEs */ +msleep (60 );
Without a previous read* to flush the posted writes you're not actually waiting 60ms here from the device POV.
Quoted 5 lines
drivers/dma/switchtec_dma.c new file +5
-0+spin_lock (&swdma_chan->hw_ctrl_lock); +writeb (SWITCHTEC_CHAN_CTRL_PAUSE , &chan_hw->ctrl); + + ret = -EIO ; + do {
drivers/dma/switchtec_dma.c line 539
Quoted 10 lines
drivers/dma/switchtec_dma.c new file +10
-0+ status =readl (&chan_hw->status); + +if (status &SWITCHTEC_CHAN_STS_PAUSED ) { + ret =0 ; +break ; + } + +udelay (1000 ); + }while (retry--); +spin_unlock (&swdma_chan->hw_ctrl_lock);
Same comment about waiting under a spinlock here.
drivers/dma/switchtec_dma.c line 576
Quoted 11 lines
drivers/dma/switchtec_dma.c new file +11
-0+do { + status =readl (&chan_hw->status); + +if (!(status &SWITCHTEC_CHAN_STS_PAUSED )) { + ret =0 ; +break ; + } + +udelay (1000 ); + }while (retry--); +spin_unlock (&swdma_chan->hw_ctrl_lock);
.. and here.
It might also be useful to have a helper to wait for a certain bit to be set or not set in the status register rather than duplicating the code.
drivers/dma/switchtec_dma.c line 831
Quoted 11 lines
drivers/dma/switchtec_dma.c new file +11
-0+static void switchtec_dma_synchronize (struct dma_chan *chan) +{ +struct pci_dev *pdev; +struct switchtec_dma_chan *swdma_chan =to_switchtec_dma_chan (chan); +int rc; + +rcu_read_lock (); + pdev =rcu_dereference (swdma_chan->swdma_dev->pdev); +if (pdev) +synchronize_irq (swdma_chan->irq); +rcu_read_unlock ();
synchronize_irq can sleep and must not be called in a RCU critical section.
drivers/dma/switchtec_dma.c line 932
Quoted 8 lines
drivers/dma/switchtec_dma.c new file +8
-0+if (type >=UNKNOWN_TRANSACTION ) + goto err_unlock; + +if (type ==MEMCPY ) +if (len >SWITCHTEC_DESC_MAX_SIZE ) + goto err_unlock; + +if ((type ==WIMM ) && (len ==8 ))
Maybe do a switch on the type to make the code more readable?
Also no need for the inner braces in the last line here.
Quoted 5 lines
drivers/dma/switchtec_dma.c new file +5
-0+static irqreturn_t switchtec_dma_isr (int irq,void *chan) +{ +struct switchtec_dma_chan *swdma_chan = chan; + +if (swdma_chan->comp_ring_active)
drivers/dma/switchtec_dma.c line 1101
Quoted 10 lines
drivers/dma/switchtec_dma.c new file +10
-0+tasklet_schedule (&swdma_chan->desc_task); + +return IRQ_HANDLED ; +} + +static irqreturn_t switchtec_dma_chan_status_isr (int irq,void *dma) +{ +struct switchtec_dma_dev *swdma_dev = dma; + +tasklet_schedule (&swdma_dev->chan_status_task);
What speaks against simply using threaded irqs here instead of the tasklets?
drivers/dma/switchtec_dma.c line 1161
Quoted 9 lines
drivers/dma/switchtec_dma.c new file +9
-0+ swdma_chan->hw_cq =dma_alloc_coherent (swdma_dev->dma_dev.dev, size, + &swdma_chan->dma_addr_cq, +GFP_KERNEL ); +if (!swdma_chan->hw_cq) { + rc = -ENOMEM ; + goto free_and_exit; + } + +memset (swdma_chan->hw_cq,0 , size);
dma_alloc_coherent always returns zeroed memory, no need to zero it again.
drivers/dma/switchtec_dma.c line 1174
Quoted 13 lines
drivers/dma/switchtec_dma.c new file +13
-0+ +/* reset host phase tag */ + swdma_chan->phase_tag =0 ; + + size =sizeof (*swdma_chan->desc_ring); + swdma_chan->desc_ring =kcalloc (SWITCHTEC_DMA_RING_SIZE , size, +GFP_KERNEL ); +if (!swdma_chan->desc_ring) { + rc = -ENOMEM ; + goto free_and_exit; + } + +memset (swdma_chan->desc_ring,0 ,SWITCHTEC_DMA_RING_SIZE * size);
kcalloc also already zeroes the memory.
drivers/dma/switchtec_dma.c line 1208
Quoted 4 lines
drivers/dma/switchtec_dma.c new file +4
-0+writew ((__force u16 )cpu_to_le16 (SWITCHTEC_DMA_SQ_SIZE ), + &swdma_chan->mmio_chan_fw->sq_size); +writew ((__force u16 )cpu_to_le16 (SWITCHTEC_DMA_CQ_SIZE ), + &swdma_chan->mmio_chan_fw->cq_size);
This looks broken to me, writew always expects cpu endian arguments and byte swaps on big endian systems.
And if it isn't bogus: __force casts need a good detailed comment explaining them.
Quoted 1 line
+void
switchtec_chan_kobject_del(struct switchtec_dma_chan *swdma_chan);
This function doesn't actually seem to exists, and thus doesn't need a forward declaration.
drivers/dma/switchtec_dma.c line 1545
Quoted 12 lines
drivers/dma/switchtec_dma.c new file +12
-0+if (!swdma_dev) +return -ENOMEM ; + + bar =pcim_iomap_table (pdev)[0 ]; + swdma_dev->bar = bar; + + swdma_dev->mmio_dmac_ver = bar +SWITCHTEC_DMAC_VERSION_OFFSET ; + swdma_dev->mmio_dmac_cap = bar +SWITCHTEC_DMAC_CAPABILITY_OFFSET ; + swdma_dev->mmio_dmac_status = bar +SWITCHTEC_DMAC_STATUS_OFFSET ; + swdma_dev->mmio_dmac_ctrl = bar +SWITCHTEC_DMAC_CONTROL_OFFSET ; + swdma_dev->mmio_chan_hw_all = bar +SWITCHTEC_DMAC_CHAN_CTRL_OFFSET ; + swdma_dev->mmio_chan_fw_all = bar +SWITCHTEC_DMAC_CHAN_CFG_STS_OFFSET ;
These are all either unused or only used once or twice. I'd drop all the extra pointers and just do the little bit of arithmetics in the callers.
But I find the whole pcim_iomap_table concept very confusing to the reader of the driver, and given that it doesn't really use many devm or pcim routines I'd suggest removing them all and sticking to one well understood way of manging resource lifetimes.
drivers/dma/switchtec_dma.c line 1652
Quoted 5 lines
drivers/dma/switchtec_dma.c new file +5
-0+ rc =dma_set_mask_and_coherent (&pdev->dev,DMA_BIT_MASK (64 )); +if (rc) + rc =dma_set_mask_and_coherent (&pdev->dev,DMA_BIT_MASK (32 )); +if (rc) +return rc;
dma_set_mask_and_coherent for a smaller mask will never succeed when trying to set it to a larger one failed. So you can remove the second call here.
Quoted 1 line
+#define
MICROSEMI_VENDOR_ID0x11f8
This is already in include/linux/pci_ids.h asPCI_VENDOR_ID_MICROSEMI, please use that.
drivers/dma/switchtec_dma.c line 1703
Quoted 10 lines
drivers/dma/switchtec_dma.c new file +10
-0+ +#define SWITCHTEC_PCI_DEVICE (device_id) \ + { \ + .vendor = MICROSEMI_VENDOR_ID, \ + .device = device_id, \ + .subvendor = PCI_ANY_ID, \ + .subdevice = PCI_ANY_ID, \ + .class = PCI_CLASS_SYSTEM_OTHER << 8, \ + .class_mask = 0xFFFFFFFF, \ + }
Please just use
PCI_DEVICE(PCI_VENDOR_ID_MICROSEMI, 0x1234),
instead.
drivers/dma/switchtec_dma.c line 1706
Quoted 1 line
drivers/dma/switchtec_dma.c new file +1
-0+ SWITCHTEC_PCI_DEVICE(0x4000), //PFX 100XG4
and please use normal /* */ comments.
Logan Gunthorpe
maintainerRe: [PATCH v2 1/1] dmaengine: switchtec-dma: Introduce Switchtec DMA engine PCI driver
Quoted 13 lines · Christoph Hellwig
Quoted 12 lines · Kelvin Cao
drivers/dma/switchtec_dma.c new file +12
-0+#define HALT_RETRY 100 +static int halt_channel (struct switchtec_dma_chan *swdma_chan) +{ +u32 status; +struct chan_hw_regs __iomem *chan_hw = swdma_chan->mmio_chan_hw; +int retry =HALT_RETRY ; +struct pci_dev *pdev; +int ret; + +rcu_read_lock (); + pdev =rcu_dereference (swdma_chan->swdma_dev->pdev); +if (!pdev) {
Quoted 8 lines
drivers/dma/switchtec_dma.c line 393
Quoted 3 lines
drivers/dma/switchtec_dma.c new file +3
-0+ ret = -ENODEV ; + goto unlock_and_exit; + }This whole RCU critical section around every access to ->pdev scheme looks a bit bothersome to me. This means that all the low-level PCI ops are done in RCU critical section. Is this something you came up with or is it copied from other drivers?
I suspect they copied it from plx_dma driver that I wrote ;(, though that driver uses rcu_dereference a bit more sparingly (only on stop, issue_pending and when allocating and freeing a channel).
Quoted 7 lines
Normally we'd do an unregistration from the dmaengine subsystem first, which might do a RCU synchronization at a high level, and then we're sure that none of the methods gets called again on the unregistered device.
Can't this driver (and the dmaengine core) support an operation mode where you set a shutdown flag at the beginning
The dmaengine code didn't support hot unplug at all. I believe most drivers are likely to crash if this happens. When I wrote the plx-dma engine, I had to make a bunch of changes to dmaengine just so the framework didn't crash when I tested this. The framework is pretty thin, so there's not much to synchronize on to indicate other threads are not in the middle of issuing new IO when a flag is set.
Quoted 4 lines
drivers/dma/switchtec_dma.c line 1101
Quoted 1 line
drivers/dma/switchtec_dma.c new file +1
-0+tasklet_schedule (&swdma_dev->chan_status_task);What speaks against simply using threaded irqs here instead of the tasklets?
Almost all the dmaengine drivers use tasklets. I don't know if this is the best approach, but my understanding was that it was due to needing low latency in processing the completed descriptors, otherwise it can be hard to reach the full bandwidth of the dma engine.
Logan
[email protected]
maintainerRe: [PATCH v2 1/1] dmaengine: switchtec-dma: Introduce Switchtec DMA engine PCI driver
Quoted 31 lines · Logan Gunthorpe
Quoted 31 lines
EXTERNAL EMAIL: Do not click links or open attachments unless you > know the content is safe
Quoted 26 lines · Christoph Hellwig
Quoted 26 lines · Kelvin Cao
Quoted 9 lines
Quoted 9 lines
(quoted diff) +2
-0+#define HALT_RETRY 100 +static int halt_channel(struct switchtec_dma_chan
*swdma_chan)
+{
+ u32 status;
+ structchan_hw_regs__iomem*chan_hw= > > >swdma_chan->mmio_chan_hw;
+ int retry =HALT_RETRY;
+ structpci_dev*pdev;Quoted 8 lines
Quoted 8 lines
+ int ret;
+
+rcu_read_lock();
+ pdev =rcu_dereference(swdma_chan->swdma_dev->pdev); + if (!pdev) {
+ ret =-ENODEV;
+ gotounlock_and_exit;
+ }This whole RCU critical section around every access to ->pdev >
Quoted 1 line
scheme
looks a bit bothersome to me. This means that all the low- level
PCI ops are done in RCU critical section. Is this something you
came up with or is it copied from other drivers?
Quoted 6 lines
Quoted 6 lines
I suspect they copied it from
plx_dmadriver that I wrote ;(, though
that driver usesrcu_dereferencea bit more sparingly (only on stop,issue_pendingand when allocating and freeing a channel).
Thanks Logan for jumping in. The RCU stuff was copied from plx_dma driver.
Quoted 13 lines
Quoted 13 lines
Quoted 7 lines
Quoted 7 lines
Normally we'd do an unregistration from the dmaengine subsystem first, which might do a RCU synchronization at a high level, and then we're sure that none of the methods gets called again on the unregistered device.
Can't this driver (and the dmaengine core) support an operation mode where you set a shutdown flag at the beginning
The dmaengine code didn't support hot unplug at all. I believe most drivers are likely to crash if this happens. When I wrote the plx- dma
engine, I had to make a bunch of changes to dmaengine just so the framework didn't crash when I tested this. The framework is pretty
Quoted 4 lines
Quoted 4 lines
Quoted 1 line
thin,
so there's not much to synchronize on to indicate other threads are
Quoted 1 line
not
in the middle of issuing new IO when a flag is set.
A bit more history, this is the unbind issue Logan spotted and fixed:
https://lore.kernel.org/all/[email protected]/
Quoted 16 lines
Quoted 16 lines
Quoted 6 lines
Quoted 6 lines
Quoted 2 lines
Quoted 2 lines
+
tasklet_schedule(&swdma_dev->chan_status_task);What speaks against simply using threaded irqs here instead of the
tasklets?Almost all the dmaengine drivers use tasklets. I don't know if this
Quoted 1 line
is
the best approach, but my understanding was that it was due to > needing
low latency in processing the completed descriptors, otherwise it can > be
hard to reach the full bandwidth of the dma engine.Logan
[email protected]
maintainerRe: [PATCH v2 1/1] dmaengine: switchtec-dma: Introduce Switchtec DMA engine PCI driver
In reply to Christoph HellwigQuoted 45 lines · Christoph Hellwig
Quoted 45 lines
EXTERNAL EMAIL: Do not click links or open attachments unless you > know the content is safe
Quoted 6 lines · Kelvin Cao
Quoted 6 lines
drivers/dma/switchtec_dma.c new file +5
-0+#define HALT_RETRY 100 +static int halt_channel (struct switchtec_dma_chan *swdma_chan) +{ + u32 status; + struct chan_hw_regs __iomem *chan_hw = > > swdma_chan-Quoted 1 line
mmio_chan_hw;Quoted 10 lines
Quoted 10 lines
+ int retry =
HALT_RETRY;
+ structpci_dev*pdev;
+ int ret;
+
+rcu_read_lock();
+ pdev =rcu_dereference(swdma_chan->swdma_dev->pdev); + if (!pdev) {
+ ret =-ENODEV;
+ gotounlock_and_exit;
+ }This whole RCU critical section around every access to ->pdev scheme
looks a bit bothersome to me. This means that all the low-level PCI ops are done in RCU critical section. Is this something you came up with or is it copied from other drivers?Normally we'd do an unregistration from the dmaengine subsystem first, which might do a RCU synchronization at a high level, and then we're sure that none of the methods gets called again on the unregistered device.
Can't this driver (and the dmaengine core) support an operation mode where you set a shutdown flag at the beginning
Quoted 11 lines
Quoted 11 lines
drivers/dma/switchtec_dma.c new file +11
-0+ + spin_lock(&swdma_chan->hw_ctrl_lock); + writeb(SWITCHTEC_CHAN_CTRL_HALT, &chan_hw->ctrl); + + ret = -EIO; + do { + status = readl(&chan_hw->status); + + if (status & SWITCHTEC_CHAN_STS_HALTED) { + ret = 0; + break;
Quoted 8 lines
Quoted 8 lines
Quoted 5 lines
Quoted 5 lines
+ }
+
+ udelay(1000);
+ } while (retry--);
+spin_unlock(&swdma_chan->hw_ctrl_lock);Why is the lock needed while reading the status and waiting for it with long delays?
There's (low) chance of access to the same ctrl register from other paths which might change the value of status in an unexpected way. It also prevents the hardware operation from being interrupted until it indicates it has finished by a bit set in the status register.
Quoted 17 lines
Quoted 17 lines
Quoted 11 lines
drivers/dma/switchtec_dma.c line 477
Quoted 11 lines
+
spin_lock(&swdma_chan->hw_ctrl_lock);
+ ctrl =SWITCHTEC_CHAN_CTRL_RESET;
+ ctrl |=SWITCHTEC_CHAN_CTRL_ERR_PAUSE;
+writel(ctrl, &chan_hw->ctrl);drivers/dma/switchtec_dma.c new file +6
-0+ + udelay(1000); + + ctrl = SWITCHTEC_CHAN_CTRL_ERR_PAUSE; + writel(ctrl, &chan_hw->ctrl); + spin_unlock(&swdma_chan->hw_ctrl_lock);This looks broken. PCIe MMIO Write TLPs an might not never reach the device until you do a read from it. So instead of the udelay you probably need to do a read* operation to some register that has no side effects.
Good point.
Quoted 2 lines
Quoted 2 lines
Also what is the point of the ctrl local variable? Just passing the values directly would be shorted and easier to read.
OK
Quoted 12 lines
Quoted 12 lines
Quoted 9 lines
drivers/dma/switchtec_dma.c line 502
Quoted 9 lines
+
spin_lock(&swdma_chan->hw_ctrl_lock);
+writeb(SWITCHTEC_CHAN_CTRL_PAUSE, &chan_hw->ctrl); +spin_unlock(&swdma_chan->hw_ctrl_lock);drivers/dma/switchtec_dma.c new file +5
-0+ + rcu_read_unlock(); + + /* wait 60ms to ensure no pending CEs */ + msleep(60);Without a previous read* to flush the posted writes you're not actually waiting 60ms here from the device POV.
Yes.
Quoted 33 lines
Quoted 33 lines
Quoted 16 lines
Quoted 6 lines
+
spin_lock(&swdma_chan->hw_ctrl_lock);
+writeb(SWITCHTEC_CHAN_CTRL_PAUSE, &chan_hw->ctrl);drivers/dma/switchtec_dma.c new file +3
-0+ + ret = -EIO; + do {drivers/dma/switchtec_dma.c line 539
Quoted 10 lines
drivers/dma/switchtec_dma.c new file +10
-0+ status = readl(&chan_hw->status); + + if (status & SWITCHTEC_CHAN_STS_PAUSED) { + ret = 0; + break; + } + + udelay(1000); + } while (retry--); + spin_unlock(&swdma_chan->hw_ctrl_lock);Same comment about waiting under a spinlock here.
Quoted 11 lines
drivers/dma/switchtec_dma.c line 576
Quoted 11 lines
+ do {
+ status =readl(&chan_hw->status);drivers/dma/switchtec_dma.c new file +9
-0+ + if (!(status & SWITCHTEC_CHAN_STS_PAUSED)) { + ret = 0; + break; + } + + udelay(1000); + } while (retry--); + spin_unlock(&swdma_chan->hw_ctrl_lock);.. and here.
Quoted 3 lines
Quoted 3 lines
It might also be useful to have a helper to wait for a certain bit to be set or not set in the status register rather than duplicating the code.
Will update.
Quoted 16 lines
Quoted 16 lines
Quoted 13 lines
Quoted 5 lines
drivers/dma/switchtec_dma.c new file +4
-0+static void switchtec_dma_synchronize (struct dma_chan *chan) +{ + struct pci_dev *pdev; + struct switchtec_dma_chan *swdma_chan = > >Quoted 8 lines
to_switchtec_dma_chan(chan);
+ int rc;
+
+rcu_read_lock();
+ pdev =rcu_dereference(swdma_chan->swdma_dev->pdev); + if (pdev)
+synchronize_irq(swdma_chan->irq);
+rcu_read_unlock();
synchronize_irqcan sleep and must not be called in a RCU critical section.
Will fix.
Quoted 65 lines
Quoted 65 lines
Quoted 9 lines
drivers/dma/switchtec_dma.c line 932
Quoted 9 lines
+ if (type >=
UNKNOWN_TRANSACTION)
+ gotoerr_unlock;drivers/dma/switchtec_dma.c new file +6
-0+ + if (type == MEMCPY) + if (len > SWITCHTEC_DESC_MAX_SIZE) + goto err_unlock; + + if ((type == WIMM) && (len == 8))Maybe do a switch on the type to make the code more readable?
Also no need for the inner braces in the last line here.
Quoted 11 lines
Quoted 11 lines
drivers/dma/switchtec_dma.c new file +11
-0+static irqreturn_t switchtec_dma_isr (int irq,void *chan) +{ + struct switchtec_dma_chan *swdma_chan = chan; + + if (swdma_chan->comp_ring_active) + tasklet_schedule(&swdma_chan->desc_task); + + return IRQ_HANDLED; +} + +static irqreturn_t switchtec_dma_chan_status_isr(int irq, voidQuoted 5 lines
drivers/dma/switchtec_dma.c line 1101
Quoted 5 lines
Quoted 1 line
Quoted 1 line
*dma)
drivers/dma/switchtec_dma.c new file +4
-0+{ + struct switchtec_dma_dev *swdma_dev = dma; + + tasklet_schedule(&swdma_dev->chan_status_task);What speaks against simply using threaded irqs here instead of the tasklets?
Quoted 11 lines
Quoted 11 lines
+
swdma_chan->hw_cq= > > dma_alloc_coherent(swdma_dev-Quoted 1 line
dma_dev.dev, size,
+ > > &swdma_chan-
Quoted 1 line
dma_addr_cq,+
GFP_KERNEL); + if (!swdma_chan->hw_cq) {
+ rc =-ENOMEM;
+ gotofree_and_exit;
+ }
+
+memset(swdma_chan->hw_cq, 0, size);
dma_alloc_coherentalways returns zeroed memory, no need to zero it again.Quoted 15 lines
Quoted 15 lines
drivers/dma/switchtec_dma.c new file +6
-0+ + /* reset host phase tag */ + swdma_chan->phase_tag = 0; + + size = sizeof(*swdma_chan->desc_ring); + swdma_chan->desc_ring = kcalloc(SWITCHTEC_DMA_RING_SIZE,Quoted 1 line
Quoted 1 line
size,
+
GFP_KERNEL);
+ if (!swdma_chan->desc_ring) {
+ rc =-ENOMEM;
+ gotofree_and_exit;
+ }
+
+ memset(swdma_chan->desc_ring, 0,SWITCHTEC_DMA_RING_SIZE
* > > size);
Quoted 1 line
Quoted 1 line
kcalloc also already zeroes the memory.
Memset has been fixed in v3 posted earlier.
Thanks,
Kelvin
Re: [PATCH v2 1/1] dmaengine: switchtec-dma: Introduce Switchtec DMA engine PCI driver
Quoted 6 lines · On Thu, Apr 13, 2023 at 10:40:41PM +0000, [email protected] wrote:
Quoted 2 lines
Quoted 2 lines
Why is the lock needed while reading the status and waiting for it with long delays?
There's (low) chance of access to the same ctrl register from other paths which might change the value of status in an unexpected way. It also prevents the hardware operation from being interrupted until it indicates it has finished by a bit set in the status register.
Well, the lock is obviously required to protecte the reads to the register. But why do you need to hold the lock over the reads and the delay?
[email protected]
maintainerRe: [PATCH v2 1/1] dmaengine: switchtec-dma: Introduce Switchtec DMA engine PCI driver
Quoted 16 lines · Christoph Hellwig
Quoted 16 lines
EXTERNAL EMAIL: Do not click links or open attachments unless you > know the content is safe
Quoted 10 lines · On Thu, Apr 13, 2023 at 10:40:41PM +0000, > [email protected] wrote:
Quoted 10 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Why is the lock needed while reading the status and waiting
for it with long delays?There's (low) chance of access to the same ctrl register from other
paths which might change the value of status in an unexpected way. > > It
also prevents the hardware operation from being interrupted until > > it
indicates it has finished by a bit set in the status register.
Quoted 3 lines
Quoted 3 lines
Well, the lock is obviously required to protecte the reads to the register. But why do you need to hold the lock over the reads and the delay?
I wanted to protect the complete hardware operation, from kick-off by ctrl writing to status change indicating completion, which might involves delays in between when more than 1 status checks required.
[email protected]
maintainerRe: [PATCH v2 1/1] dmaengine: switchtec-dma: Introduce Switchtec DMA engine PCI driver
In reply to Christoph HellwigQuoted 8 lines · Christoph Hellwig
Quoted 5 lines
+ writew((__force u16)cpu_to_le16(SWITCHTEC_DMA_SQ_SIZE), +
&swdma_chan->mmio_chan_fw->sq_size);
+ writew((__force u16)cpu_to_le16(SWITCHTEC_DMA_CQ_SIZE), +&swdma_chan->mmio_chan_fw->cq_size);This looks broken to me, writew always expects cpu endian arguments and byte swaps on big endian systems.
Do you mean writew assumes the peripherals be little-endian, and will do the swap when host is bit-endian?
Quoted 24 lines
And if it isn't bogus:
__forcecasts need a good detailed comment explaining them.Quoted 2 lines
+void
switchtec_chan_kobject_del(struct switchtec_dma_chan *swdma_chan);This function doesn't actually seem to exists, and thus doesn't need a forward declaration.
Quoted 14 lines
+ if (!swdma_dev)
+ return-ENOMEM;drivers/dma/switchtec_dma.c new file +5
-0+ + bar = pcim_iomap_table(pdev)[0]; + swdma_dev->bar = bar; + + swdma_dev->mmio_dmac_ver = bar +
SWITCHTEC_DMAC_VERSION_OFFSET;
+swdma_dev->mmio_dmac_cap= bar +SWITCHTEC_DMAC_CAPABILITY_OFFSET;
+swdma_dev->mmio_dmac_status= bar +SWITCHTEC_DMAC_STATUS_OFFSET;
+swdma_dev->mmio_dmac_ctrl= bar +SWITCHTEC_DMAC_CONTROL_OFFSET;
Quoted 8 lines
Quoted 4 lines
+
swdma_dev->mmio_chan_hw_all= bar +SWITCHTEC_DMAC_CHAN_CTRL_OFFSET;
+swdma_dev->mmio_chan_fw_all= bar +SWITCHTEC_DMAC_CHAN_CFG_STS_OFFSET;These are all either unused or only used once or twice. I'd drop all the extra pointers and just do the little bit of arithmetics in the callers.
Will clean it up.
Quoted 5 lines
But I find the whole
pcim_iomap_tableconcept very confusing to the reader of the driver, and given that it doesn't really use many devm or pcim routines I'd suggest removing them all and sticking to one well understood way of manging resource lifetimes.
I didn't get it. Do you have specific suggestion?
Quoted 8 lines
Quoted 7 lines
+ rc =
dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)); + if (rc)
+ rc =dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
+ if (rc)
+ return rc;
Quoted 4 lines
dma_set_mask_and_coherentfor a smaller mask will never succeed when trying to set it to a larger one failed. So you can remove the second
call here.
By default the kernel assumes the device can address 32-bit address space, I wonder why it wouldn't allow 32-bit mask when it failes 64- bit?
Quoted 19 lines
Quoted 2 lines
+#define
MICROSEMI_VENDOR_ID0x11f8This is already in
include/linux/pci_ids.hasPCI_VENDOR_ID_MICROSEMI, please use that.drivers/dma/switchtec_dma.c line 1703
Quoted 10 lines
drivers/dma/switchtec_dma.c new file +10
-0+ +#define SWITCHTEC_PCI_DEVICE (device_id) \ + { \ + .vendor = MICROSEMI_VENDOR_ID, \ + .device = device_id, \ + .subvendor = PCI_ANY_ID, \ + .subdevice = PCI_ANY_ID, \ + .class = PCI_CLASS_SYSTEM_OTHER << 8, \ + .class_mask = 0xFFFFFFFF, \ + }Please just use
Quoted 7 lines
PCI_DEVICE(PCI_VENDOR_ID_MICROSEMI, 0x1234),instead.
Quoted 1 line
+ SWITCHTEC_PCI_DEVICE(0x4000), //PFX 100XG4
and please use normal /* */ comments.
Ok.
Thanks,
Kelvin
Re: [PATCH v2 1/1] dmaengine: switchtec-dma: Introduce Switchtec DMA engine PCI driver
Quoted 7 lines · On Thu, Apr 13, 2023 at 11:22:14PM +0000, [email protected] wrote:
Quoted 6 lines · Christoph Hellwig
Quoted 5 lines
+ writew((__force u16)cpu_to_le16(SWITCHTEC_DMA_SQ_SIZE), +
&swdma_chan->mmio_chan_fw->sq_size);
+ writew((__force u16)cpu_to_le16(SWITCHTEC_DMA_CQ_SIZE), +&swdma_chan->mmio_chan_fw->cq_size);
Quoted 5 lines
Quoted 2 lines
This looks broken to me, writew always expects cpu endian arguments and byte swaps on big endian systems.
Do you mean writew assumes the peripherals be little-endian, and will do the swap when host is bit-endian?
Yes. All the standard mmio accessors ({read,write}{b,w,l,q}) do that.
Quoted 6 lines
Quoted 4 lines
But I find the whole
pcim_iomap_tableconcept very confusing to the reader of the driver, and given that it doesn't really use many devm or pcim routines I'd suggest removing them all and sticking to one well understood way of manging resource lifetimes.I didn't get it. Do you have specific suggestion?
- instead of pcim_enable_device call pci_enable_device, and then just call pci_disable_device in ->remove and on error
- instead of pcim_iomap_regions, call pci_request_mem_regions on probe, and pci_release_mem_regions on release / fail, and then do an ioremap(pci_resource_start(pdev, 1) for the actual bar (and iounmap on release/fail) instead of pcim_iomap_table.
Quoted 7 lines
Quoted 4 lines
dma_set_mask_and_coherentfor a smaller mask will never succeed when trying to set it to a larger one failed. So you can remove the second
call here.By default the kernel assumes the device can address 32-bit address space,
Yes.
Quoted 2 lines
I wonder why it wouldn't allow 32-bit mask when it failes 64- bit?
The kernel never fails setting a 64-bit DMA mask, it only fails setting too small masks.
Kelvin Cao
maintainerPATCH v2 1/1 dmaengine: switchtec-dma: Introduce Switchtec DMA engine PCI driver
In reply to Kelvin CaoSome Switchtec Switches can expose DMA engines via extra PCI functions on the upstream ports. At most one such function can be supported on each upstream port. Each function can have one or more DMA channels.
Implement core PCI driver skeleton and register DMA engine callbacks.
--- MAINTAINERS | 5 + drivers/dma/Kconfig | 9 + drivers/dma/Makefile | 1 + drivers/dma/switchtec_dma.c | 1734 +++++++++++++++++++++++++++++++++++ 4 files changed, 1749 insertions(+) create mode 100644 drivers/dma/switchtec_dma.c
MAINTAINERS +5 -0
@@ -20158,6 +20158,11 @@ S: Supported F: include/net/switchdev.h F: net/switchdev/ +SWITCHTEC DMA DRIVER +M: Kelvin Cao <[email protected]> +S: Maintained +F: drivers/dma/switchtec_dma.c + SY8106A REGULATOR DRIVER M: Icenowy Zheng <[email protected]> S: Maintained
drivers/dma/Kconfig +9 -0
@@ -748,6 +748,15 @@ config XILINX_ZYNQMP_DPDMA driver provides the dmaengine required by the DisplayPort subsystem display driver. +config SWITCHTEC_DMA + tristate "Switchtec PSX/PFX Switch DMA Engine Support" + depends on PCI + select DMA_ENGINE + help + Some Switchtec PSX/PFX PCIe Switches support additional DMA engines. + These are exposed via an extra function on the switch's upstream + port. + # driver files source "drivers/dma/bestcomm/Kconfig"
drivers/dma/Makefile +1 -0
@@ -81,6 +81,7 @@ obj-$(CONFIG_XGENE_DMA) += xgene-dma.o obj-$(CONFIG_ST_FDMA) += st_fdma.o obj-$(CONFIG_FSL_DPAA2_QDMA) += fsl-dpaa2-qdma/ obj-$(CONFIG_INTEL_LDMA) += lgm/ +obj-$(CONFIG_SWITCHTEC_DMA) += switchtec_dma.o obj-y += mediatek/ obj-y += qcom/
drivers/dma/switchtec_dma.c new file +1734 -0
1,734 lines not on this page · raw message
Signature · 1 line
2.25.1