Re: [PATCH RFC v2 2/2] dmaengine: avalon: Intel Avalon-MM DMA Interface for PCIe test

2019-10-09 Thread Dan Carpenter
On Wed, Oct 09, 2019 at 12:12:31PM +0200, Alexander Gordeev wrote:
> diff --git a/drivers/dma/avalon-test/avalon-dev.c 
> b/drivers/dma/avalon-test/avalon-dev.c
> new file mode 100644
> index ..9e83f777f937
> --- /dev/null
> +++ b/drivers/dma/avalon-test/avalon-dev.c
> @@ -0,0 +1,65 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * Avalon DMA driver
> + *
> + * Author: Alexander Gordeev 
> + */
> +#include 
> +#include 
> +#include 
> +
> +#include "avalon-dev.h"
> +#include "avalon-ioctl.h"
> +#include "avalon-mmap.h"
> +
> +const struct file_operations avalon_dev_fops = {
> + .llseek = generic_file_llseek,
> + .unlocked_ioctl = avalon_dev_ioctl,
> + .mmap   = avalon_dev_mmap,
> +};
> +
> +static struct avalon_dev avalon_dev;
> +
> +static int __init avalon_drv_init(void)
> +{
> + struct avalon_dev *adev = _dev;
> + struct dma_chan *chan;
> + dma_cap_mask_t mask;
> + int ret;
> +
> + dma_cap_zero(mask);
> + dma_cap_set(DMA_SLAVE, mask);
> +
> + chan = dma_request_channel(mask, NULL, NULL);
> + if (!chan)
> + return -ENODEV;
> +
> + adev->dma_chan  = chan;
> +
> + adev->misc_dev.minor= MISC_DYNAMIC_MINOR;
> + adev->misc_dev.name = DEVICE_NAME;
> + adev->misc_dev.nodename = DEVICE_NAME;
> + adev->misc_dev.fops = _dev_fops;
> + adev->misc_dev.mode = 0644;
> +
> + ret = misc_register(>misc_dev);
> + if (ret)
> + return ret;

dma_release_channel(chan);

Btw, I find the error labels very frustrating to read.  They all are
"come-from" style labels.

p = malloc();
if (!p)
goto malloc_failed;

That doesn't tell me anything about what the goto does.  It should be
like:

if (ret)
goto release_dma;

return 0;

release_dma:
dma_release_channel(chan);

return ret;

> +
> + return 0;
> +}
> +
> +static void __exit avalon_drv_exit(void)
> +{
> + struct avalon_dev *adev = _dev;
> +
> + misc_deregister(>misc_dev);
> + dma_release_channel(adev->dma_chan);
> +}
> +
> +module_init(avalon_drv_init);
> +module_exit(avalon_drv_exit);
> +
> +MODULE_AUTHOR("Alexander Gordeev ");
> +MODULE_DESCRIPTION("Avalon DMA control driver");
> +MODULE_LICENSE("GPL v2");
> diff --git a/drivers/dma/avalon-test/avalon-dev.h 
> b/drivers/dma/avalon-test/avalon-dev.h
> new file mode 100644
> index ..f06362ebf110
> --- /dev/null
> +++ b/drivers/dma/avalon-test/avalon-dev.h
> @@ -0,0 +1,33 @@
> +/* SPDX-License-Identifier: GPL-2.0 */
> +/*
> + * Avalon DMA driver
> + *
> + * Author: Alexander Gordeev 
> + */
> +#ifndef __AVALON_DEV_H__
> +#define __AVALON_DEV_H__
> +
> +#include 
> +#include 
> +
> +#include "../avalon/avalon-hw.h"
> +
> +#define TARGET_MEM_BASE  CONFIG_AVALON_TEST_TARGET_BASE
> +#define TARGET_MEM_SIZE  CONFIG_AVALON_TEST_TARGET_SIZE
> +
> +#define TARGET_DMA_SIZE (2 * AVALON_DMA_MAX_TANSFER_SIZE)
> +#define TARGET_DMA_SIZE_SG  TARGET_MEM_SIZE
> +
> +#define DEVICE_NAME  "avalon-dev"
> +
> +struct avalon_dev {
> + struct dma_chan *dma_chan;
> + struct miscdevice misc_dev;
> +};
> +
> +static inline struct device *chan_to_dev(struct dma_chan *chan)
> +{
> + return chan->device->dev;
> +}
> +
> +#endif
> diff --git a/drivers/dma/avalon-test/avalon-ioctl.c 
> b/drivers/dma/avalon-test/avalon-ioctl.c
> new file mode 100644
> index ..b90cdedf4400
> --- /dev/null
> +++ b/drivers/dma/avalon-test/avalon-ioctl.c
> @@ -0,0 +1,128 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * Avalon DMA driver
> + *
> + * Author: Alexander Gordeev 
> + */
> +#include 
> +#include 
> +#include 
> +
> +#include 
> +
> +#include "avalon-xfer.h"
> +
> +long avalon_dev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
> +{
> + struct avalon_dev *adev = container_of(file->private_data,
> + struct avalon_dev, misc_dev);
> + struct dma_chan *chan = adev->dma_chan;
> + struct device *dev = chan_to_dev(chan);
> + struct iovec iovec[2];
> + void __user *buf = NULL, __user *buf_rd = NULL, __user *buf_wr = NULL;
> + size_t len = 0, len_rd = 0, len_wr = 0;
> + int ret = 0;
> +
> + dev_dbg(dev, "%s(%d) { cmd %x", __FUNCTION__, __LINE__, cmd);

Use ftrace and delete this debug code.

> +
> + switch (cmd) {
> + case IOCTL_AVALON_DMA_GET_INFO: {
> + struct avalon_dma_info info = {
> + .mem_addr   = TARGET_MEM_BASE,
> + .mem_size   = TARGET_MEM_SIZE,
> + .dma_size   = TARGET_DMA_SIZE,
> + .dma_size_sg= TARGET_DMA_SIZE_SG,
> + };
> +
> + if (copy_to_user((void *)arg, , sizeof(info))) {
> + ret = -EFAULT;
> + goto done;
> + }
> +
> + goto done;
> + }
> + case 

Re: [PATCH RFC v2 2/2] dmaengine: avalon: Intel Avalon-MM DMA Interface for PCIe test

2019-10-09 Thread Greg KH
On Wed, Oct 09, 2019 at 12:12:31PM +0200, Alexander Gordeev wrote:
> +config AVALON_TEST_TARGET_BASE
> + hex "Target device base address"
> + default "0x7000"
> +
> +config AVALON_TEST_TARGET_SIZE
> + hex "Target device memory size"
> + default "0x1000"

Don't put stuff like this as a configuration option, requiring the
kernel to be rebuilt.  Make it dynamic, or from device tree, but not
like this.

thanks,

greg k-h
___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH RFC v2 2/2] dmaengine: avalon: Intel Avalon-MM DMA Interface for PCIe test

2019-10-09 Thread Alexander Gordeev
This is sample implementation of a driver that uses "avalon-dma"
driver interface to perform data transfers between on-chip and
system memory in devices using Avalon-MM DMA Interface for PCIe
design. Companion user-level tool could be found at
https://github.com/a-gordeev/avalon-tool.git

CC: Michael Chen 
CC: de...@driverdev.osuosl.org
CC: dmaeng...@vger.kernel.org

Signed-off-by: Alexander Gordeev 
---
 drivers/dma/Kconfig |   1 +
 drivers/dma/Makefile|   1 +
 drivers/dma/avalon-test/Kconfig |  23 +
 drivers/dma/avalon-test/Makefile|  14 +
 drivers/dma/avalon-test/avalon-dev.c|  65 +++
 drivers/dma/avalon-test/avalon-dev.h|  33 ++
 drivers/dma/avalon-test/avalon-ioctl.c  | 128 +
 drivers/dma/avalon-test/avalon-ioctl.h  |  12 +
 drivers/dma/avalon-test/avalon-mmap.c   |  93 
 drivers/dma/avalon-test/avalon-mmap.h   |  12 +
 drivers/dma/avalon-test/avalon-sg-buf.c | 132 +
 drivers/dma/avalon-test/avalon-sg-buf.h |  26 +
 drivers/dma/avalon-test/avalon-util.c   |  54 ++
 drivers/dma/avalon-test/avalon-util.h   |  12 +
 drivers/dma/avalon-test/avalon-xfer.c   | 697 
 drivers/dma/avalon-test/avalon-xfer.h   |  28 +
 include/uapi/linux/avalon-ioctl.h   |  32 ++
 17 files changed, 1363 insertions(+)
 create mode 100644 drivers/dma/avalon-test/Kconfig
 create mode 100644 drivers/dma/avalon-test/Makefile
 create mode 100644 drivers/dma/avalon-test/avalon-dev.c
 create mode 100644 drivers/dma/avalon-test/avalon-dev.h
 create mode 100644 drivers/dma/avalon-test/avalon-ioctl.c
 create mode 100644 drivers/dma/avalon-test/avalon-ioctl.h
 create mode 100644 drivers/dma/avalon-test/avalon-mmap.c
 create mode 100644 drivers/dma/avalon-test/avalon-mmap.h
 create mode 100644 drivers/dma/avalon-test/avalon-sg-buf.c
 create mode 100644 drivers/dma/avalon-test/avalon-sg-buf.h
 create mode 100644 drivers/dma/avalon-test/avalon-util.c
 create mode 100644 drivers/dma/avalon-test/avalon-util.h
 create mode 100644 drivers/dma/avalon-test/avalon-xfer.c
 create mode 100644 drivers/dma/avalon-test/avalon-xfer.h
 create mode 100644 include/uapi/linux/avalon-ioctl.h

diff --git a/drivers/dma/Kconfig b/drivers/dma/Kconfig
index f6f43480a4a4..4b3c6a6baf4c 100644
--- a/drivers/dma/Kconfig
+++ b/drivers/dma/Kconfig
@@ -670,6 +670,7 @@ source "drivers/dma/sh/Kconfig"
 source "drivers/dma/ti/Kconfig"
 
 source "drivers/dma/avalon/Kconfig"
+source "drivers/dma/avalon-test/Kconfig"
 
 # clients
 comment "DMA Clients"
diff --git a/drivers/dma/Makefile b/drivers/dma/Makefile
index fd7e11417b73..eb3ee7f6cac6 100644
--- a/drivers/dma/Makefile
+++ b/drivers/dma/Makefile
@@ -76,6 +76,7 @@ obj-$(CONFIG_XGENE_DMA) += xgene-dma.o
 obj-$(CONFIG_ZX_DMA) += zx_dma.o
 obj-$(CONFIG_ST_FDMA) += st_fdma.o
 obj-$(CONFIG_AVALON_DMA) += avalon/
+obj-$(CONFIG_AVALON_TEST) += avalon-test/
 
 obj-y += mediatek/
 obj-y += qcom/
diff --git a/drivers/dma/avalon-test/Kconfig b/drivers/dma/avalon-test/Kconfig
new file mode 100644
index ..021c28fe77a6
--- /dev/null
+++ b/drivers/dma/avalon-test/Kconfig
@@ -0,0 +1,23 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# Avalon DMA engine
+#
+# Author: Alexander Gordeev 
+#
+config AVALON_TEST
+   select AVALON_DMA
+   tristate "Intel Avalon-MM DMA Interface for PCIe test driver"
+   help
+ This selects a test driver for Avalon-MM DMA Interface for PCI
+
+if AVALON_TEST
+
+config AVALON_TEST_TARGET_BASE
+   hex "Target device base address"
+   default "0x7000"
+
+config AVALON_TEST_TARGET_SIZE
+   hex "Target device memory size"
+   default "0x1000"
+
+endif
diff --git a/drivers/dma/avalon-test/Makefile b/drivers/dma/avalon-test/Makefile
new file mode 100644
index ..63351c52478a
--- /dev/null
+++ b/drivers/dma/avalon-test/Makefile
@@ -0,0 +1,14 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# Avalon DMA driver
+#
+# Author: Alexander Gordeev 
+#
+obj-$(CONFIG_AVALON_TEST)  += avalon-test.o
+
+avalon-test-objs :=avalon-dev.o \
+   avalon-ioctl.o \
+   avalon-mmap.o \
+   avalon-sg-buf.o \
+   avalon-xfer.o \
+   avalon-util.o
diff --git a/drivers/dma/avalon-test/avalon-dev.c 
b/drivers/dma/avalon-test/avalon-dev.c
new file mode 100644
index ..9e83f777f937
--- /dev/null
+++ b/drivers/dma/avalon-test/avalon-dev.c
@@ -0,0 +1,65 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Avalon DMA driver
+ *
+ * Author: Alexander Gordeev 
+ */
+#include 
+#include 
+#include 
+
+#include "avalon-dev.h"
+#include "avalon-ioctl.h"
+#include "avalon-mmap.h"
+
+const struct file_operations avalon_dev_fops = {
+   .llseek = generic_file_llseek,
+   .unlocked_ioctl = avalon_dev_ioctl,
+   .mmap   = avalon_dev_mmap,
+};
+
+static struct avalon_dev avalon_dev;
+
+static int __init avalon_drv_init(void)
+{
+   struct avalon_dev *adev = _dev;