Skip to contentSkip to conversation

NAND: Allow per-buffer allocation

PATCH New Marek Vasut 2 participants 3 messages Aug 9, 2011

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

PATCH 1/1 NAND: Allow per-buffer allocation

Don't allocate NAND buffers as one block, but allocate them separately. This allows systems where DMA to buffers happen to allocate these buffers properly aligned.

---
 drivers/mtd/nand/nand_base.c |   27 +++++++++++++++++++++------
 include/linux/mtd/nand.h     |    7 ++++---
 2 files changed, 25 insertions(+), 9 deletions(-)

NOTE: This less intrusive approach should avoid breaking older drivers (this is the reason for malloc()ing chip->buffers).

drivers/mtd/nand/nand_base.c +21 -6
index 1a95a91..be8469c 100644
@@ -2749,12 +2749,25 @@ int nand_scan_ident(struct mtd_info *mtd, int maxchips,
  */
 int nand_scan_tail(struct mtd_info *mtd)
 {
-	int i;
+	int i, bufsize;
+	uint8_t *buf;
 	struct nand_chip *chip = mtd->priv;
 
-	if (!(chip->options & NAND_OWN_BUFFERS))
-		chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL);
-	if (!chip->buffers)
+	if (!(chip->options & NAND_OWN_BUFFERS)) {
+		chip->buffers = malloc(sizeof(struct nand_buffers));
+		if (!chip->buffers)
+			return -ENOMEM;
+
+		bufsize = NAND_MAX_PAGESIZE + (3 * NAND_MAX_OOBSIZE);
+		buf = malloc(bufsize);
+
+		chip->buffers->buffer = (struct nand_buffers *)buf;
+		chip->buffers->ecccalc = buf;
+		chip->buffers->ecccode = buf + NAND_MAX_OOBSIZE;
+		chip->buffers->databuf = buf + (2 * NAND_MAX_OOBSIZE);
+	}
+
+	if (!chip->buffers->buffer)
 		return -ENOMEM;
 
 	/* Set the internal oob buffer location, just after the page data */
@@ -2996,6 +3009,8 @@ void nand_release(struct mtd_info *mtd)
 
 	/* Free bad block table memory */
 	kfree(chip->bbt);
-	if (!(chip->options & NAND_OWN_BUFFERS))
-		kfree(chip->buffers);
+	if (!(chip->options & NAND_OWN_BUFFERS)) {
+		free(chip->buffers->buffer);
+		free(chip->buffers);
+	}
 }
include/linux/mtd/nand.h +4 -3
index 987a2ec..c3449a9 100644
@@ -370,9 +370,10 @@ struct nand_ecc_ctrl {
  * consecutive order.
  */
 struct nand_buffers {
-	uint8_t	ecccalc[NAND_MAX_OOBSIZE];
-	uint8_t	ecccode[NAND_MAX_OOBSIZE];
-	uint8_t databuf[NAND_MAX_PAGESIZE + NAND_MAX_OOBSIZE];
+	uint8_t *buffer;
+	uint8_t	*ecccalc;
+	uint8_t	*ecccode;
+	uint8_t *databuf;
 };
 
 /**
Signature · 1 line
1.7.5.4
#1 lore raw

PATCH 1/1 NAND: Allow per-buffer allocation

Quoted 5 lines · Marek Vasut

Don't allocate NAND buffers as one block, but allocate them separately. This allows systems where DMA to buffers happen to allocate these buffers properly aligned.

That second sentence is hard to parse -- I think you mean something like, "This accommodates drivers which DMA to the buffers and have alignment constraints."

Will a similar change be needed in Linux?

Quoted 4 lines

int nand_scan_tail(struct mtd_info *mtd)
{

drivers/mtd/nand/nand_base.c +1 -1
-	int i;
+	int i, bufsize;

drivers/mtd/nand/nand_base.c int nand_scan_ident(struct mtd_info *mtd, int maxchips, · line 2759

Quoted 10 lines
drivers/mtd/nand/nand_base.c +5 -3
+	uint8_t *buf;
 	struct nand_chip *chip = mtd->priv;
 
-	if (!(chip->options & NAND_OWN_BUFFERS))
-		chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL);
-	if (!chip->buffers)
+	if (!(chip->options & NAND_OWN_BUFFERS)) {
+		chip->buffers = malloc(sizeof(struct nand_buffers));
+		if (!chip->buffers)
+			return -ENOMEM;

Why does the struct itself need to be dynamically allocated?

drivers/mtd/nand/nand_base.c int nand_scan_ident(struct mtd_info *mtd, int maxchips, · line 2771

Quoted 12 lines
drivers/mtd/nand/nand_base.c +11 -0
+
+		bufsize = NAND_MAX_PAGESIZE + (3 * NAND_MAX_OOBSIZE);
+		buf = malloc(bufsize);
+
+		chip->buffers->buffer = (struct nand_buffers *)buf;
+		chip->buffers->ecccalc = buf;
+		chip->buffers->ecccode = buf + NAND_MAX_OOBSIZE;
+		chip->buffers->databuf = buf + (2 * NAND_MAX_OOBSIZE);
+	}
+
+	if (!chip->buffers->buffer)
 		return -ENOMEM;

What does "buffer" mean now? What would a driver that supplies its own completely separate ecccalc/ecccode/databuf buffers put in "buffer"?

-Scott

#2 lore raw

PATCH 1/1 NAND: Allow per-buffer allocation

Quoted 10 lines · Scott Wood
Quoted 5 lines · Marek Vasut

Don't allocate NAND buffers as one block, but allocate them separately. This allows systems where DMA to buffers happen to allocate these buffers properly aligned.

That second sentence is hard to parse -- I think you mean something like, "This accommodates drivers which DMA to the buffers and have alignment constraints."

Yes, something like that. Sorry, it's 1.14 PM here.

Quoted 2 lines

Will a similar change be needed in Linux?

I'm not sure how much in sync we are with linux here. It'd be worth looking at.

Quoted 10 lines
Quoted 10 lines

int nand_scan_tail(struct mtd_info *mtd)
{

drivers/mtd/nand/nand_base.c +2 -1
-	int i;
+	int i, bufsize;
+	uint8_t *buf;
 
 	struct nand_chip *chip = mtd->priv;
Quoted 9 lines

drivers/mtd/nand/nand_base.c int nand_scan_ident(struct mtd_info *mtd, int maxchips, · line 2759

Quoted 7 lines
drivers/mtd/nand/nand_base.c +4 -3
-	if (!(chip->options & NAND_OWN_BUFFERS))
-		chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL);
-	if (!chip->buffers)
+	if (!(chip->options & NAND_OWN_BUFFERS)) {
+		chip->buffers = malloc(sizeof(struct nand_buffers));
+		if (!chip->buffers)
+			return -ENOMEM;

Why does the struct itself need to be dynamically allocated?

That was in the NOTE: ... to avoid breaking drivers. We can have that changed, but that'd be much more intrussive.

Quoted 13 lines
Quoted 13 lines
drivers/mtd/nand/nand_base.c +11 -0
+
+		bufsize = NAND_MAX_PAGESIZE + (3 * NAND_MAX_OOBSIZE);
+		buf = malloc(bufsize);
+
+		chip->buffers->buffer = (struct nand_buffers *)buf;
+		chip->buffers->ecccalc = buf;
+		chip->buffers->ecccode = buf + NAND_MAX_OOBSIZE;
+		chip->buffers->databuf = buf + (2 * NAND_MAX_OOBSIZE);
+	}
+
+	if (!chip->buffers->buffer)
Quoted 4 lines
Quoted 1 line

return -ENOMEM;

What does "buffer" mean now? What would a driver that supplies its own completely separate ecccalc/ecccode/databuf buffers put in "buffer"?

Maybe that condition should go also into the if() statement above. What do you think ?

Quoted 2 lines

-Scott