Skip to contentSkip to conversation

Convert m68k MVME147 WD33C93 SCSI driver to DMA API

PATCH Superseded Michael Schmitz 2 participants 10 messages Jul 12, 2022 – Jul 13, 2022

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover Convert m68k MVME147 WD33C93 SCSI driver to DMA API

This series was precipitated by Arnd removing CONFIG_VIRT_TO_BUS. The m68k WD33C93 still used virt_to_bus to convert virtual addresses to physical addresses suitable for the DMA engines (note m68k does not have an IOMMU and uses a direct mapping for DMA addresses).

Arnd suggested to use dma_map_single() to set up dma mappings instead of open-coding much the same in every driver dma_setup() function. An earlier patch series has converted the three Amiga WD33C93 drivers, this series now takes care of the sole remaining m68k WD33C93 driver.

The m68k VME mvme147 driver has no DMA addressing or alignment restrictions and can be converted in the same way as the Amiga a3000 one in my previous series, but requires conversion to a platform device driver first.

Changes from v1:
- use of devm_platform_ioremap_resource() to convert phys. resource to virtual address for chip registers.

Only compile tested so far, and hardware testing might be hard to do. I'd appreciate someone giving this a thorough review.

Cheers,

Michael

#1 lore raw

PATCH v2 1/5 m68k - add MVME147 SCSI base address to mvme147hw.h

The base address for the WD33C93 SCSI host adapter on mvme147 boards is missing from mvme147hw.h. This information will be needed for platform device conversion of the mvme147_scsi driver, so add it here.

---
 arch/m68k/include/asm/mvme147hw.h | 1 +
 1 file changed, 1 insertion(+)
arch/m68k/include/asm/mvme147hw.h +1 -0
index e28eb1c0e0bf..fd8c1e4fc7be 100644
@@ -93,6 +93,7 @@ struct pcc_regs {
 #define M147_SCC_B_ADDR		0xfffe3000
 #define M147_SCC_PCLK		5000000
 
+#define MVME147_SCSI_BASE	0xfffe4000
 #define MVME147_IRQ_SCSI_PORT	(IRQ_USER+0x45)
 #define MVME147_IRQ_SCSI_DMA	(IRQ_USER+0x46)
 
Signature · 1 line
2.17.1
#2 lore raw

PATCH v2 2/5 m68k - set up platform device for mvme147_scsi

In reply to Michael Schmitz

Set up a platform device for the mvme147_scsi driver. The platform device is required for conversion of the driver to the DMA API.

---
 arch/m68k/mvme147/config.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
arch/m68k/mvme147/config.c +21 -0
index 4e6218115f43..c6e7dfe3eb54 100644
@@ -21,6 +21,7 @@
 #include <linux/console.h>
 #include <linux/linkage.h>
 #include <linux/init.h>
+#include <linux/platform_device.h>
 #include <linux/major.h>
 #include <linux/rtc.h>
 #include <linux/interrupt.h>
@@ -188,3 +189,23 @@ int mvme147_hwclk(int op, struct rtc_time *t)
 	}
 	return 0;
 }
+
+static const struct resource mvme147_scsi_rsrc[] __initconst = {
+	DEFINE_RES_MEM(MVME147_SCSI_BASE, 0xff),
+	DEFINE_RES_IRQ(MVME147_IRQ_SCSI_PORT),
+};
+
+int __init mvme147_platform_init(void)
+{
+	struct platform_device *pdev;
+	int rv = 0;
+
+	pdev = platform_device_register_simple("mvme147-scsi", -1,
+		mvme147_scsi_rsrc, ARRAY_SIZE(mvme147_scsi_rsrc));
+	if (IS_ERR(pdev))
+		rv = PTR_ERR(pdev);
+
+	return rv;
+}
+
+arch_initcall(mvme147_platform_init);
Signature · 1 line
2.17.1
#3 lore raw

Re: [PATCH v2 2/5] m68k - set up platform device for mvme147_scsi

arch/m68k/mvme147/config.c int mvme147_hwclk(int op, struct rtc_time *t) · line 194

Quoted 3 lines · Michael Schmitz
arch/m68k/mvme147/config.c +3 -0
+
+static const struct resource mvme147_scsi_rsrc[] __initconst = {
+       DEFINE_RES_MEM(MVME147_SCSI_BASE, 0xff),

Still the wrong size?

arch/m68k/mvme147/config.c int mvme147_hwclk(int op, struct rtc_time *t) · line 204

Quoted 10 lines
arch/m68k/mvme147/config.c +10 -0
+       DEFINE_RES_IRQ(MVME147_IRQ_SCSI_PORT),
+};
+
+int __init mvme147_platform_init(void)
+{
+       struct platform_device *pdev;
+       int rv = 0;
+
+       pdev = platform_device_register_simple("mvme147-scsi", -1,
+               mvme147_scsi_rsrc, ARRAY_SIZE(mvme147_scsi_rsrc));

I think you actually have to use platform_device_register_full() to pass a DMA mask here: As I understand it, the dma_set_mask_and_coherent() call in the driver fails if the device is not already marked as dma capable by having an initial mask set.

The way this normally works is that the device gets created with a mask that reflects the capabilities of the bus, while the driver sets a mask based on what it wants to program into the device, and the dma-mapping interfaces ensure that we only use the intersection of those.

Arnd

#4 lore raw

Re: [PATCH v2 2/5] m68k - set up platform device for mvme147_scsi

Hi Arnd,

Am 12.07.2022 um 20:12 schrieb Arnd Bergmann:

Quoted 6 lines · Michael Schmitz

arch/m68k/mvme147/config.c int mvme147_hwclk(int op, struct rtc_time *t) · line 194

Quoted 3 lines
arch/m68k/mvme147/config.c +3 -0
+
+static const struct resource mvme147_scsi_rsrc[] __initconst = {
+       DEFINE_RES_MEM(MVME147_SCSI_BASE, 0xff),

Still the wrong size?

Too true - forgot to fix that, sorry.

Quoted 16 lines

arch/m68k/mvme147/config.c int mvme147_hwclk(int op, struct rtc_time *t) · line 204

Quoted 11 lines
arch/m68k/mvme147/config.c +10 -0
+       DEFINE_RES_IRQ(MVME147_IRQ_SCSI_PORT),
+};
+
+int __init mvme147_platform_init(void)
+{
+       struct platform_device *pdev;
+       int rv = 0;
+
+       pdev = platform_device_register_simple("mvme147-scsi", -1,
+               mvme147_scsi_rsrc, ARRAY_SIZE(mvme147_scsi_rsrc));

I think you actually have to use platform_device_register_full() to pass a DMA mask here: As I understand it, the dma_set_mask_and_coherent() call in the driver fails if the device is not already marked as dma capable by having an initial mask set.

I'll take a look at that - if true, this requires the amiga-a3000-scsi platform device set-up be changed in the same way (the gvp11 and a2091 drivers inherit the DMA mask from the Zorro bus default, so ought to work OK).

Cheers,

Michael

Quoted 7 lines

The way this normally works is that the device gets created with a mask that reflects the capabilities of the bus, while the driver sets a mask based on what it wants to program into the device, and the dma-mapping interfaces ensure that we only use the intersection of those.

Arnd

#5 lore raw

Re: [PATCH v2 2/5] m68k - set up platform device for mvme147_scsi

Hi Arnd,

Am 12.07.2022 um 21:07 schrieb Michael Schmitz:

Quoted 30 lines

Hi Arnd,

Am 12.07.2022 um 20:12 schrieb Arnd Bergmann:

Quoted 7 lines

arch/m68k/mvme147/config.c int mvme147_hwclk(int op, struct rtc_time *t) · line 194

Quoted 3 lines · Michael Schmitz
arch/m68k/mvme147/config.c +3 -0
+
+static const struct resource mvme147_scsi_rsrc[] __initconst = {
+       DEFINE_RES_MEM(MVME147_SCSI_BASE, 0xff),

Still the wrong size?

Too true - forgot to fix that, sorry.

Quoted 16 lines

arch/m68k/mvme147/config.c int mvme147_hwclk(int op, struct rtc_time *t) · line 204

Quoted 11 lines
arch/m68k/mvme147/config.c +10 -0
+       DEFINE_RES_IRQ(MVME147_IRQ_SCSI_PORT),
+};
+
+int __init mvme147_platform_init(void)
+{
+       struct platform_device *pdev;
+       int rv = 0;
+
+       pdev = platform_device_register_simple("mvme147-scsi", -1,
+               mvme147_scsi_rsrc, ARRAY_SIZE(mvme147_scsi_rsrc));

I think you actually have to use platform_device_register_full() to pass a DMA mask here: As I understand it, the dma_set_mask_and_coherent() call in the driver fails if the device is not already marked as dma capable by having an initial mask set.

Quoted 4 lines

I'll take a look at that - if true, this requires the amiga-a3000-scsi platform device set-up be changed in the same way (the gvp11 and a2091 drivers inherit the DMA mask from the Zorro bus default, so ought to work OK).

I think we are good with using platform_device_register_simple():

setup_pdev_dma_masks() sets the DMA mask to 32 bit when allocating a new platform device, and platform_device_register_full() only changes that when passed in a non-zero mask in pdevinfo.

platform_device_register_simple() leaves the pdevinfo mask zero, so the 32 bit default set in setup_pdev_dma_masks() survives.

Cheers,

Michael

Quoted 12 lines

Cheers,

Michael

Quoted 7 lines

The way this normally works is that the device gets created with a mask that reflects the capabilities of the bus, while the driver sets a mask based on what it wants to program into the device, and the dma-mapping interfaces ensure that we only use the intersection of those.

Arnd

#6 lore raw

Re: [PATCH v2 2/5] m68k - set up platform device for mvme147_scsi

Hi Arnd,

Quoted 11 lines · Michael Schmitz
Quoted 10 lines
Quoted 5 lines

I think you actually have to use platform_device_register_full() to pass a DMA mask here: As I understand it, the dma_set_mask_and_coherent() call in the driver fails if the device is not already marked as dma capable by having an initial mask set.

I'll take a look at that - if true, this requires the amiga-a3000-scsi platform device set-up be changed in the same way (the gvp11 and a2091 drivers inherit the DMA mask from the Zorro bus default, so ought to work OK).

Quoted 5 lines

I think we are good with using platform_device_register_simple():

setup_pdev_dma_masks() sets the DMA mask to 32 bit when allocating a new platform device, and platform_device_register_full() only changes that when passed in a non-zero mask in pdevinfo.

platform_device_register_simple() leaves the pdevinfo mask zero, so the 32 bit default set in setup_pdev_dma_masks() survives.

Verified by having pata_falcon print the default DMA mask (0xffffffff), then setting a 24 bit DMA mask and reading it back.

That did uncover an error I made in the gvp11 driver - the devices' default DMA mask there is given as a 32 bit integer, but the DMA API needs 64 bit. Will send a separate fix for that.

Cheers,

    Michael

Quoted 17 lines

Cheers,

    Michael

Quoted 12 lines

Cheers,

    Michael

Quoted 7 lines

The way this normally works is that the device gets created with a mask that reflects the capabilities of the bus, while the driver sets a mask based on what it wants to program into the device, and the dma-mapping interfaces ensure that we only use the intersection of those.

        Arnd

#7 lore raw

PATCH v2 3/5 m68k - add MMIO ioremap support for mvme147

In reply to Michael Schmitz

Converting the mvme147 SCSI driver to the DMA API requires use of ioremap() in order to get the kernel virtual address of the WD chip registers.

Add support for transparent mapping of the mvme147 MMIO region to arch/m68k/mm/kmap.c to enable use of ioremap() in that driver.

---
 arch/m68k/mm/kmap.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
arch/m68k/mm/kmap.c +11 -0
index 7594a945732b..2bcede2af902 100644
@@ -185,6 +185,13 @@ void __iomem *__ioremap(unsigned long physaddr, unsigned long size, int cachefla
 			return (void __iomem *)physaddr;
 	}
 #endif
+#ifdef CONFIG_MVME147
+	if (MACH_IS_MVME147) {
+		if (physaddr >= 0xe0000000 && cacheflag == IOMAP_NOCACHE_SER)
+			return (void __iomem *)physaddr;
+	}
+#endif
+
 #ifdef CONFIG_COLDFIRE
 	if (__cf_internalio(physaddr))
 		return (void __iomem *) physaddr;
@@ -308,6 +315,10 @@ void iounmap(void __iomem *addr)
 	if (MACH_IS_VIRT && (unsigned long)addr >= 0xff000000)
 		return;
 #endif
+#ifdef CONFIG_MVME147
+	if (MACH_IS_MVME147 && (unsigned long)addr >= 0xe000000)
+		return;
+#endif
 #ifdef CONFIG_COLDFIRE
 	if (cf_internalio(addr))
 		return;
Signature · 1 line
2.17.1
#8 lore raw

PATCH v2 4/5 scsi - convert mvme147.c driver to platform device

In reply to Michael Schmitz

Convert the mvme147.c driver to a platform device driver. This is required for conversion of the driver to the DMA API.

--

Changes from v1:

- change patch index from 3 to 3 (due to insertion of m68k kmap patch)

Arnd Bergmann:
- use devm_platform_ioremap_resource() for wd33c93 base address

---
 drivers/scsi/mvme147.c | 89 +++++++++++++++++++++++++++++-------------
 1 file changed, 61 insertions(+), 28 deletions(-)
drivers/scsi/mvme147.c +61 -28
index 472fa043094f..ea8afeec8e56 100644
@@ -3,6 +3,7 @@
 #include <linux/mm.h>
 #include <linux/blkdev.h>
 #include <linux/interrupt.h>
+#include <linux/platform_device.h>
 #include <linux/init.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
@@ -85,40 +86,52 @@ static struct scsi_host_template mvme147_host_template = {
 	.cmd_size		= sizeof(struct scsi_pointer),
 };
 
-static struct Scsi_Host *mvme147_shost;
-
-static int __init mvme147_init(void)
+static int __init mvme147_scsi_probe(struct platform_device *pdev)
 {
+	struct resource *mres, *ires;
+	struct Scsi_Host *mvme147_inst;
 	wd33c93_regs regs;
 	struct WD33C93_hostdata *hdata;
+	void __iomem *base;
 	int error = -ENOMEM;
 
-	if (!MACH_IS_MVME147)
-		return 0;
+	mres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	if (!mres)
+		return -ENODEV;
+
+	ires = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
+	if (!ires)
+		return -ENODEV;
+
+	if (!request_mem_region(mres->start, resource_size(mres), "wd33c93"))
+		return -EBUSY;
 
-	mvme147_shost = scsi_host_alloc(&mvme147_host_template,
+	mvme147_inst = scsi_host_alloc(&mvme147_host_template,
 			sizeof(struct WD33C93_hostdata));
-	if (!mvme147_shost)
+	if (!mvme147_inst)
 		goto err_out;
-	mvme147_shost->base = 0xfffe4000;
-	mvme147_shost->irq = MVME147_IRQ_SCSI_PORT;
 
-	regs.SASR = (volatile unsigned char *)0xfffe4000;
-	regs.SCMD = (volatile unsigned char *)0xfffe4001;
+	base = devm_platform_ioremap_resource(pdev, 0);
 
-	hdata = shost_priv(mvme147_shost);
+	mvme147_inst->base = (unsigned int) base;
+	mvme147_inst->irq = ires->start;
+
+	regs.SASR = (volatile unsigned char *)base;
+	regs.SCMD = (volatile unsigned char *)(base+1);
+
+	hdata = shost_priv(mvme147_inst);
 	hdata->no_sync = 0xff;
 	hdata->fast = 0;
 	hdata->dma_mode = CTRL_DMA;
 
-	wd33c93_init(mvme147_shost, regs, dma_setup, dma_stop, WD33C93_FS_8_10);
+	wd33c93_init(mvme147_inst, regs, dma_setup, dma_stop, WD33C93_FS_8_10);
 
-	error = request_irq(MVME147_IRQ_SCSI_PORT, mvme147_intr, 0,
-			"MVME147 SCSI PORT", mvme147_shost);
+	error = request_irq(ires->start, mvme147_intr, 0,
+			"MVME147 SCSI PORT", mvme147_inst);
 	if (error)
 		goto err_unregister;
-	error = request_irq(MVME147_IRQ_SCSI_DMA, mvme147_intr, 0,
-			"MVME147 SCSI DMA", mvme147_shost);
+	error = request_irq(ires->start+1, mvme147_intr, 0,
+			"MVME147 SCSI DMA", mvme147_inst);
 	if (error)
 		goto err_free_irq;
 #if 0	/* Disabled; causes problems booting */
@@ -133,30 +146,50 @@ static int __init mvme147_init(void)
 	m147_pcc->dma_cntrl = 0x00;	/* ensure DMA is stopped */
 	m147_pcc->dma_intr = 0x89;	/* Ack and enable ints */
 
-	error = scsi_add_host(mvme147_shost, NULL);
+	error = scsi_add_host(mvme147_inst, &pdev->dev);
 	if (error)
 		goto err_free_irq;
-	scsi_scan_host(mvme147_shost);
+
+	platform_set_drvdata(pdev, mvme147_inst);
+
+	scsi_scan_host(mvme147_inst);
 	return 0;
 
 err_free_irq:
-	free_irq(MVME147_IRQ_SCSI_PORT, mvme147_shost);
+	free_irq(ires->start, mvme147_inst);
 err_unregister:
-	scsi_host_put(mvme147_shost);
+	scsi_host_put(mvme147_inst);
 err_out:
 	return error;
 }
 
-static void __exit mvme147_exit(void)
+static int __exit mvme147_scsi_remove(struct platform_device *pdev)
 {
-	scsi_remove_host(mvme147_shost);
+	struct Scsi_Host *mvme147_inst = platform_get_drvdata(pdev);
+	struct resource *mres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	struct resource *ires = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
+
+	scsi_remove_host(mvme147_inst);
 
 	/* XXX Make sure DMA is stopped! */
-	free_irq(MVME147_IRQ_SCSI_PORT, mvme147_shost);
-	free_irq(MVME147_IRQ_SCSI_DMA, mvme147_shost);
+	free_irq(ires->start, mvme147_inst);
+	free_irq(ires->start+1, mvme147_inst);
 
-	scsi_host_put(mvme147_shost);
+	iounmap((void __iomem *)mvme147_inst->base);
+	scsi_host_put(mvme147_inst);
+	release_mem_region(mres->start, resource_size(mres));
+	return 0;
 }
 
-module_init(mvme147_init);
-module_exit(mvme147_exit);
+static struct platform_driver mvme147_scsi_driver = {
+	.remove = __exit_p(mvme147_scsi_remove),
+	.driver   = {
+		.name	= "mvme147-scsi",
+	},
+};
+
+module_platform_driver_probe(mvme147_scsi_driver, mvme147_scsi_probe);
+
+MODULE_DESCRIPTION("MVME147 built-in SCSI");
+MODULE_LICENSE("GPL");
+MODULE_ALIAS("platform:mvme147-scsi");
Signature · 1 line
2.17.1
#9 lore raw

PATCH v2 5/5 scsi - convert mvme147.c driver to DMA API

In reply to Michael Schmitz

Convert mvme147.c to DMA API to eliminate one of the last usages of virt_to_bus().

--

Changes from v1:
- change patch index from 3 to 4 (due to insertion of m68k kmap patch)

---
 drivers/scsi/mvme147.c | 33 ++++++++++++++++++++++++---------
 1 file changed, 24 insertions(+), 9 deletions(-)
drivers/scsi/mvme147.c +24 -9
index ea8afeec8e56..166248bef6cf 100644
@@ -21,6 +21,8 @@
 #include "wd33c93.h"
 #include "mvme147.h"
 
+#define DMA_DIR(d)   ((d == DATA_OUT_DIR) ? DMA_TO_DEVICE : DMA_FROM_DEVICE)
+
 static irqreturn_t mvme147_intr(int irq, void *data)
 {
 	struct Scsi_Host *instance = data;
@@ -35,10 +37,20 @@ static irqreturn_t mvme147_intr(int irq, void *data)
 static int dma_setup(struct scsi_cmnd *cmd, int dir_in)
 {
 	struct scsi_pointer *scsi_pointer = WD33C93_scsi_pointer(cmd);
+	unsigned long len = scsi_pointer->this_residual;
 	struct Scsi_Host *instance = cmd->device->host;
 	struct WD33C93_hostdata *hdata = shost_priv(instance);
 	unsigned char flags = 0x01;
-	unsigned long addr = virt_to_bus(scsi_pointer->ptr);
+	dma_addr_t addr;
+
+	addr = dma_map_single(instance->dma_dev, scsi_pointer->ptr,
+			      len, DMA_DIR(dir_in));
+	if (dma_mapping_error(instance->dma_dev, addr)) {
+		dev_warn(instance->dma_dev, "cannot map SCSI data block %p\n",
+			 scsi_pointer->ptr);
+		return 1;
+	}
+	scsi_pointer->dma_handle = addr;
 
 	/* setup dma direction */
 	if (!dir_in)
@@ -47,14 +59,6 @@ static int dma_setup(struct scsi_cmnd *cmd, int dir_in)
 	/* remember direction */
 	hdata->dma_dir = dir_in;
 
-	if (dir_in) {
-		/* invalidate any cache */
-		cache_clear(addr, scsi_pointer->this_residual);
-	} else {
-		/* push any dirty cache */
-		cache_push(addr, scsi_pointer->this_residual);
-	}
-
 	/* start DMA */
 	m147_pcc->dma_bcr = scsi_pointer->this_residual | (1 << 24);
 	m147_pcc->dma_dadr = addr;
@@ -67,7 +71,13 @@ static int dma_setup(struct scsi_cmnd *cmd, int dir_in)
 static void dma_stop(struct Scsi_Host *instance, struct scsi_cmnd *SCpnt,
 		     int status)
 {
+	struct scsi_pointer *scsi_pointer = WD33C93_scsi_pointer(SCpnt);
+	struct WD33C93_hostdata *hdata = shost_priv(instance);
+
 	m147_pcc->dma_cntrl = 0;
+	dma_unmap_single(instance->dma_dev, scsi_pointer->dma_handle,
+			 scsi_pointer->this_residual,
+			 DMA_DIR(hdata->dma_dir));
 }
 
 static struct scsi_host_template mvme147_host_template = {
@@ -95,6 +105,11 @@ static int __init mvme147_scsi_probe(struct platform_device *pdev)
 	void __iomem *base;
 	int error = -ENOMEM;
 
+	if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32))) {
+		dev_warn(&pdev->dev, "cannot use 32 bit DMA\n");
+		return -ENODEV;
+	}
+
 	mres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 	if (!mres)
 		return -ENODEV;
Signature · 1 line
2.17.1