Skip to contentSkip to conversation

mtd: nand: tango: Enforce DMA direction type

PATCH New Boris Brezillon 3 participants 6 messages Feb 13, 2017 – Mar 16, 2017

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mtd: nand: tango: Enforce DMA direction type

do_dma() use an integer to pass the DMA data direction information and pass the same value to dmaengine_prep_slave_sg().

Currently, DMA_{FROM,TO}_DEVICE match DMA_{DEV_TO_MEM,MEM_TO_DEV} definitions so it works fine, but assuming this will always be the case is not safe.

Enforce enum dma_data_direction type in the function prototype and make the enum dma_data_direction -> enum dma_transfer_direction conversion explicit.

---
 drivers/mtd/nand/tango_nand.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/mtd/nand/tango_nand.c +6 -3
index 4a5e948c62df..9a0e2f85d865 100644
@@ -223,8 +223,8 @@ static void tango_dma_callback(void *arg)
 	complete(arg);
 }
 
-static int do_dma(struct tango_nfc *nfc, int dir, int cmd, const void *buf,
-		  int len, int page)
+static int do_dma(struct tango_nfc *nfc, enum dma_data_direction dir, int cmd,
+		  const void *buf, int len, int page)
 {
 	void __iomem *addr = nfc->reg_base + NFC_STATUS;
 	struct dma_chan *chan = nfc->chan;
@@ -238,7 +238,10 @@ static int do_dma(struct tango_nfc *nfc, int dir, int cmd, const void *buf,
 	if (dma_map_sg(chan->device->dev, &sg, 1, dir) != 1)
 		return -EIO;
 
-	desc = dmaengine_prep_slave_sg(chan, &sg, 1, dir, DMA_PREP_INTERRUPT);
+	desc = dmaengine_prep_slave_sg(chan, &sg, 1,
+				       dir == DMA_FROM_DEVICE ?
+				       DMA_DEV_TO_MEM : DMA_MEM_TO_DEV,
+				       DMA_PREP_INTERRUPT);
 	if (!desc)
 		goto dma_unmap;
 
Signature · 1 line
2.7.4
#1 lore raw

PATCH 1/1 mtd: nand: tango: Enforce DMA direction type

Am 13.02.2017 um 10:45 schrieb Boris Brezillon:

Quoted 34 lines

do_dma() use an integer to pass the DMA data direction information and pass the same value to dmaengine_prep_slave_sg().

Currently, DMA_{FROM,TO}_DEVICE match DMA_{DEV_TO_MEM,MEM_TO_DEV} definitions so it works fine, but assuming this will always be the case is not safe.

Enforce enum dma_data_direction type in the function prototype and make the enum dma_data_direction -> enum dma_transfer_direction conversion explicit.

---
 drivers/mtd/nand/tango_nand.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/mtd/nand/tango_nand.c +2 -2
index 4a5e948c62df..9a0e2f85d865 100644
@@ -223,8 +223,8 @@ static void tango_dma_callback(void *arg)
 	complete(arg);
 }
 
-static int do_dma(struct tango_nfc *nfc, int dir, int cmd, const void *buf,
-		  int len, int page)
+static int do_dma(struct tango_nfc *nfc, enum dma_data_direction dir, int cmd,
+		  const void *buf, int len, int page)
 {
 	void __iomem *addr = nfc->reg_base + NFC_STATUS;
 	struct dma_chan *chan = nfc->chan;
@@ -238,7 +238,10 @@ static int do_dma(struct tango_nfc *nfc, int dir, int cmd, const void *buf,
 	if (dma_map_sg(chan->device->dev, &sg, 1, dir) != 1)

drivers/mtd/nand/tango_nand.c static int do_dma(struct tango_nfc *nfc, int dir, int cmd, const void *buf, · line 247

Quoted 10 lines
drivers/mtd/nand/tango_nand.c +4 -1
 		return -EIO;
 
-	desc = dmaengine_prep_slave_sg(chan, &sg, 1, dir, DMA_PREP_INTERRUPT);
+	desc = dmaengine_prep_slave_sg(chan, &sg, 1,
+				       dir == DMA_FROM_DEVICE ?
+				       DMA_DEV_TO_MEM : DMA_MEM_TO_DEV,
+				       DMA_PREP_INTERRUPT);
 	if (!desc)
 		goto dma_unmap;
 

Thanks,
//richard

#2 lore raw

PATCH 1/1 mtd: nand: tango: Enforce DMA direction type

In reply to Boris Brezillon
Quoted 35 lines · Boris Brezillon

do_dma() use an integer to pass the DMA data direction information and pass the same value to dmaengine_prep_slave_sg().

Currently, DMA_{FROM,TO}_DEVICE match DMA_{DEV_TO_MEM,MEM_TO_DEV} definitions so it works fine, but assuming this will always be the case is not safe.

Enforce enum dma_data_direction type in the function prototype and make the enum dma_data_direction -> enum dma_transfer_direction conversion explicit.

---
 drivers/mtd/nand/tango_nand.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/mtd/nand/tango_nand.c +2 -2
index 4a5e948c62df..9a0e2f85d865 100644
@@ -223,8 +223,8 @@ static void tango_dma_callback(void *arg)
 	complete(arg);
 }
 
-static int do_dma(struct tango_nfc *nfc, int dir, int cmd, const void *buf,
-		  int len, int page)
+static int do_dma(struct tango_nfc *nfc, enum dma_data_direction dir, int cmd,
+		  const void *buf, int len, int page)
 {
 	void __iomem *addr = nfc->reg_base + NFC_STATUS;
 	struct dma_chan *chan = nfc->chan;
@@ -238,7 +238,10 @@ static int do_dma(struct tango_nfc *nfc, int dir, int cmd, const void *buf,
 	if (dma_map_sg(chan->device->dev, &sg, 1, dir) != 1)

drivers/mtd/nand/tango_nand.c static int do_dma(struct tango_nfc *nfc, int dir, int cmd, const void *buf, · line 247

Quoted 10 lines
drivers/mtd/nand/tango_nand.c +4 -1
 		return -EIO;
 
-	desc = dmaengine_prep_slave_sg(chan, &sg, 1, dir, DMA_PREP_INTERRUPT);
+	desc = dmaengine_prep_slave_sg(chan, &sg, 1,
+				       dir == DMA_FROM_DEVICE ?
+				       DMA_DEV_TO_MEM : DMA_MEM_TO_DEV,
+				       DMA_PREP_INTERRUPT);
 	if (!desc)
 		goto dma_unmap;
 

How about evaluating the ternary conditional into a temp var?

drivers/mtd/nand/tango_nand.c +2 -1
index 4a5e948c62df..c3f145528f1d 100644
@@ -231,6 +231,7 @@ static int do_dma(struct tango_nfc *nfc, int dir, int cmd, const void *buf,
        struct dma_async_tx_descriptor *desc;
        struct scatterlist sg;
        struct completion tx_done;
+       int xdir = dir == DMA_TO_DEVICE ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
        int err = -EIO;
        u32 res, val;
 
@@ -238,7 +239,7 @@ static int do_dma(struct tango_nfc *nfc, int dir, int cmd, const void *buf,
        if (dma_map_sg(chan->device->dev, &sg, 1, dir) != 1)
                return -EIO;
 
-       desc = dmaengine_prep_slave_sg(chan, &sg, 1, dir, DMA_PREP_INTERRUPT);
+       desc = dmaengine_prep_slave_sg(chan, &sg, 1, xdir, DMA_PREP_INTERRUPT);
        if (!desc)
                goto dma_unmap;
 
#3 lore raw

PATCH 1/1 mtd: nand: tango: Enforce DMA direction type

Quoted 38 lines · On Mon, 20 Feb 2017 13:30:25 +0100 Marc Gonzalez…
Quoted 36 lines · Boris Brezillon

do_dma() use an integer to pass the DMA data direction information and pass the same value to dmaengine_prep_slave_sg().

Currently, DMA_{FROM,TO}_DEVICE match DMA_{DEV_TO_MEM,MEM_TO_DEV} definitions so it works fine, but assuming this will always be the case is not safe.

Enforce enum dma_data_direction type in the function prototype and make the enum dma_data_direction -> enum dma_transfer_direction conversion explicit.

---
 drivers/mtd/nand/tango_nand.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/mtd/nand/tango_nand.c +2 -2
index 4a5e948c62df..9a0e2f85d865 100644
@@ -223,8 +223,8 @@ static void tango_dma_callback(void *arg)
 	complete(arg);
 }
 
-static int do_dma(struct tango_nfc *nfc, int dir, int cmd, const void *buf,
-		  int len, int page)
+static int do_dma(struct tango_nfc *nfc, enum dma_data_direction dir, int cmd,
+		  const void *buf, int len, int page)
 {
 	void __iomem *addr = nfc->reg_base + NFC_STATUS;
 	struct dma_chan *chan = nfc->chan;
@@ -238,7 +238,10 @@ static int do_dma(struct tango_nfc *nfc, int dir, int cmd, const void *buf,
 	if (dma_map_sg(chan->device->dev, &sg, 1, dir) != 1)
 		return -EIO;
 
Quoted 11 lines

drivers/mtd/nand/tango_nand.c static int do_dma(struct tango_nfc *nfc, int dir, int cmd, const void *buf, · line 245

Quoted 9 lines
drivers/mtd/nand/tango_nand.c +4 -1
-	desc = dmaengine_prep_slave_sg(chan, &sg, 1, dir, DMA_PREP_INTERRUPT);
+	desc = dmaengine_prep_slave_sg(chan, &sg, 1,
+				       dir == DMA_FROM_DEVICE ?
+				       DMA_DEV_TO_MEM : DMA_MEM_TO_DEV,
+				       DMA_PREP_INTERRUPT);
 	if (!desc)
 		goto dma_unmap;
 

How about evaluating the ternary conditional into a temp var?

Works for me.

Quoted 22 lines
drivers/mtd/nand/tango_nand.c +2 -1
index 4a5e948c62df..c3f145528f1d 100644
@@ -231,6 +231,7 @@ static int do_dma(struct tango_nfc *nfc, int dir, int cmd, const void *buf,
        struct dma_async_tx_descriptor *desc;
        struct scatterlist sg;
        struct completion tx_done;
+       int xdir = dir == DMA_TO_DEVICE ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
        int err = -EIO;
        u32 res, val;
 
@@ -238,7 +239,7 @@ static int do_dma(struct tango_nfc *nfc, int dir, int cmd, const void *buf,
        if (dma_map_sg(chan->device->dev, &sg, 1, dir) != 1)
                return -EIO;
 
-       desc = dmaengine_prep_slave_sg(chan, &sg, 1, dir, DMA_PREP_INTERRUPT);
+       desc = dmaengine_prep_slave_sg(chan, &sg, 1, xdir, DMA_PREP_INTERRUPT);
        if (!desc)
                goto dma_unmap;
 
#4 lore raw

PATCH v2 1/1 mtd: nand: tango: Enforce DMA direction type

From: Boris Brezillon <[email protected]>

do_dma() uses an int to pass the DMA data direction information and pass the same value to dmaengine_prep_slave_sg().

Currently, DMA_{FROM,TO}_DEVICE match DMA_{DEV_TO_MEM,MEM_TO_DEV} definitions so it works fine, but assuming this will always be the case is not safe.

Enforce enum dma_data_direction type in the function prototype and make the enum dma_data_direction -> enum dma_transfer_direction conversion explicit.

---
 drivers/mtd/nand/tango_nand.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/mtd/nand/tango_nand.c +5 -3
index 4a5e948c62df..05b6e1065203 100644
@@ -223,12 +223,13 @@ static void tango_dma_callback(void *arg)
 	complete(arg);
 }
 
-static int do_dma(struct tango_nfc *nfc, int dir, int cmd, const void *buf,
-		  int len, int page)
+static int do_dma(struct tango_nfc *nfc, enum dma_data_direction dir, int cmd,
+		  const void *buf, int len, int page)
 {
 	void __iomem *addr = nfc->reg_base + NFC_STATUS;
 	struct dma_chan *chan = nfc->chan;
 	struct dma_async_tx_descriptor *desc;
+	enum dma_transfer_direction tdir;
 	struct scatterlist sg;
 	struct completion tx_done;
 	int err = -EIO;
@@ -238,7 +239,8 @@ static int do_dma(struct tango_nfc *nfc, int dir, int cmd, const void *buf,
 	if (dma_map_sg(chan->device->dev, &sg, 1, dir) != 1)
 		return -EIO;
 
-	desc = dmaengine_prep_slave_sg(chan, &sg, 1, dir, DMA_PREP_INTERRUPT);
+	tdir = dir == DMA_TO_DEVICE ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
+	desc = dmaengine_prep_slave_sg(chan, &sg, 1, tdir, DMA_PREP_INTERRUPT);
 	if (!desc)
 		goto dma_unmap;
 
Signature · 1 line
3.14159265358979
author Applied
#5 lore raw

PATCH v2 1/1 mtd: nand: tango: Enforce DMA direction type

Quoted 9 lines · On Mon, 20 Feb 2017 14:10:07 +0100 Marc Gonzalez…

From: Boris Brezillon <[email protected]>

do_dma() uses an int to pass the DMA data direction information and pass the same value to dmaengine_prep_slave_sg().

Currently, DMA_{FROM,TO}_DEVICE match DMA_{DEV_TO_MEM,MEM_TO_DEV} definitions so it works fine, but assuming this will always be the case is not safe.

Quoted 7 lines

Enforce enum dma_data_direction type in the function prototype and make the enum dma_data_direction -> enum dma_transfer_direction conversion explicit.

Applied.

Quoted 34 lines
---
 drivers/mtd/nand/tango_nand.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/mtd/nand/tango_nand.c +5 -3
index 4a5e948c62df..05b6e1065203 100644
@@ -223,12 +223,13 @@ static void tango_dma_callback(void *arg)
 	complete(arg);
 }
 
-static int do_dma(struct tango_nfc *nfc, int dir, int cmd, const void *buf,
-		  int len, int page)
+static int do_dma(struct tango_nfc *nfc, enum dma_data_direction dir, int cmd,
+		  const void *buf, int len, int page)
 {
 	void __iomem *addr = nfc->reg_base + NFC_STATUS;
 	struct dma_chan *chan = nfc->chan;
 	struct dma_async_tx_descriptor *desc;
+	enum dma_transfer_direction tdir;
 	struct scatterlist sg;
 	struct completion tx_done;
 	int err = -EIO;
@@ -238,7 +239,8 @@ static int do_dma(struct tango_nfc *nfc, int dir, int cmd, const void *buf,
 	if (dma_map_sg(chan->device->dev, &sg, 1, dir) != 1)
 		return -EIO;
 
-	desc = dmaengine_prep_slave_sg(chan, &sg, 1, dir, DMA_PREP_INTERRUPT);
+	tdir = dir == DMA_TO_DEVICE ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
+	desc = dmaengine_prep_slave_sg(chan, &sg, 1, tdir, DMA_PREP_INTERRUPT);
 	if (!desc)
 		goto dma_unmap;