[PATCH] MTD: mtdconcat NAND/Sibley support

Belyakov, Alexander alexander.belyakov at intel.com
Wed Apr 26 07:36:08 EDT 2006


JFFS2 (and not only JFFS2) does not work on top of concatenated MTD
device in case of NAND and Sibley chips due to missing
concat_block_isbad(), concat_block_markbad(), concat_writev() and
concat_writev_ecc() functions. If anyone cares - the patch below fixes
that issue.


---

diff -uNr a/drivers/mtd/mtdconcat.c b/drivers/mtd/mtdconcat.c
--- a/drivers/mtd/mtdconcat.c	2006-04-07 20:56:47.000000000 +0400
+++ b/drivers/mtd/mtdconcat.c	2006-04-26 15:26:21.000000000 +0400
@@ -141,6 +141,97 @@
 }
 
 static int
+concat_writev (struct mtd_info *mtd, const struct kvec *vecs, unsigned
long count, 
+		loff_t to, size_t * retlen)
+{
+	int i, page, len, total_len, ret = 0, written = 0, cnt = 0,
towrite;
+	u_char *bufstart;
+	char* data_poi;
+	char* data_buf;
+	loff_t write_offset;
+	int rl_wr;
+	u_int32_t pagesize;
+
+	if (mtd->flags & MTD_PROGRAM_REGIONS) {
+		pagesize = MTD_PROGREGION_SIZE(mtd);
+	} else {
+		pagesize = mtd->oobblock;
+	}
+    
+	data_buf = kmalloc(pagesize, GFP_KERNEL);
+
+	/* Preset written len for early exit */
+	*retlen = 0;
+
+	/* Calculate total length of data */
+	total_len = 0;
+	for (i = 0; i < count; i++)
+		total_len += (int) vecs[i].iov_len;
+
+	/* Do not allow write past end of page */
+	if ((to + total_len) > mtd->size) {
+		kfree(data_buf);
+		return -EINVAL;
+	}
+
+	/* Setup start page */
+	page = ((int) to) / pagesize;
+	towrite = (page + 1) * pagesize - to;
+	write_offset = to;
+	written = 0;
+	
+	/* Loop until all iovecs' data has been written */
+	len = 0;
+	while (len < total_len) {
+    		bufstart = (u_char *)vecs->iov_base;
+		bufstart += written;
+		data_poi = bufstart;
+
+    		/* If the given tuple is >= reet of page then
+		 * write it out from the iov
+		 */
+		if ( (vecs->iov_len-written) >= towrite) {
+			ret = mtd->write(mtd, write_offset, towrite,
&rl_wr, data_poi);
+			if(ret)
+	    			break;
+			len += towrite;
+			page ++;
+			write_offset = page * pagesize;
+			towrite = pagesize;
+			written += towrite;
+			if (vecs->iov_len  == written) {
+				vecs ++;
+				written = 0;
+			}
+		} else {
+			cnt = 0;
+			while (cnt < towrite ) {
+				data_buf[cnt++] = ((u_char *)
vecs->iov_base)[written++];
+				if (vecs->iov_len == written ) {
+					if((cnt+len) == total_len )
+						break;
+					vecs ++;
+					written = 0;
+				}
+			}
+			data_poi = data_buf;
+			ret = mtd->write(mtd, write_offset, cnt, &rl_wr,
data_poi);
+			if (ret)
+				break;
+			len += cnt;
+			page ++;
+			write_offset = page * pagesize;
+			towrite = pagesize;
+		}
+	}
+
+	if(retlen)
+		*retlen = len;
+	kfree(data_buf);
+	return ret;
+}
+
+static int
 concat_read_ecc(struct mtd_info *mtd, loff_t from, size_t len,
 		size_t * retlen, u_char * buf, u_char * eccbuf,
 		struct nand_oobinfo *oobsel)
@@ -251,6 +342,123 @@
 }
 
 static int
+concat_writev_ecc (struct mtd_info *mtd, const struct kvec *vecs,
unsigned long count, 
+		loff_t to, size_t * retlen, u_char *eccbuf, struct
nand_oobinfo *oobsel)
+{
+	int i, page, len, total_len, ret = 0, written = 0, cnt = 0,
towrite;
+	u_char *bufstart;
+	char* data_poi;
+	char* data_buf;
+	loff_t write_offset;
+	int rl_wr;
+	u_int32_t pagesize;
+
+	if (mtd->flags & MTD_PROGRAM_REGIONS) {
+		pagesize = MTD_PROGREGION_SIZE(mtd);
+	} else {
+		pagesize = mtd->oobblock;
+	}
+    
+	data_buf = kmalloc(pagesize, GFP_KERNEL);
+    
+	if (!(mtd->flags & MTD_WRITEABLE))
+		return -EROFS;
+
+	/* Preset written len for early exit */
+	*retlen = 0;
+
+	/* Calculate total length of data */
+	total_len = 0;
+	for (i = 0; i < count; i++)
+		total_len += (int) vecs[i].iov_len;
+
+	/* check if no data is going to be written */
+	if (!total_len) {
+		kfree(data_buf);
+		return 0;
+	}
+
+	/* Do not allow write past end of page */
+	if ((to + total_len) > mtd->size) {
+		DEBUG (MTD_DEBUG_LEVEL0, "concat_writev_ecc(): Attempted
write past end of device\n");
+		kfree(data_buf);
+		return -EINVAL;
+	}
+    
+	/* Check alignment */
+	if ((to & (pagesize - 1)) || (total_len & (pagesize - 1))) {
+		DEBUG (MTD_DEBUG_LEVEL0, "concat_writev_ecc(): Attempted
write not aligned data!\n");
+		kfree(data_buf);
+		return -EINVAL;
+	}
+    
+	/* Setup start page. Notaligned data is not allowed for
write_ecc. */
+	page = ((int) to) / pagesize;
+	towrite = (page + 1) * pagesize - to;
+	write_offset = to;
+	written = 0; 
+	/* Loop until all iovecs' data has been written */
+	len = 0;
+	while (len < total_len) {
+		bufstart = (u_char *)vecs->iov_base;
+		bufstart += written;
+		data_poi = bufstart;
+
+	        /* If the given tuple is >= reet of page then
+		* write it out from the iov
+		*/
+		if ( (vecs->iov_len-written) >= towrite) {
+			ret = mtd->write_ecc(mtd, write_offset, towrite,
+						&rl_wr, data_poi,
eccbuf, oobsel);
+			if (ret)
+	    			break;
+			len += towrite;
+			page ++;
+			write_offset = page * pagesize;
+			towrite = pagesize;
+			written += towrite;
+			if (vecs->iov_len  == written) {
+	    			vecs ++;
+	    			written = 0;
+	    		}
+	    
+			if(eccbuf)
+				eccbuf += mtd->oobavail;
+		} else {
+			cnt = 0;
+			while (cnt < towrite ) {
+				data_buf[cnt++] = ((u_char *)
vecs->iov_base)[written++];
+	    			if (vecs->iov_len == written ) {
+					if ((cnt+len) == total_len )
+						break;
+					vecs ++;
+					written = 0;
+				}
+			}
+			
+			data_poi = data_buf;
+			ret = mtd->write_ecc(mtd, write_offset, cnt, 
+						&rl_wr, data_poi,
eccbuf, oobsel);
+			if (ret)
+				break;
+			len += cnt;
+			page ++;
+			write_offset = page * pagesize;
+			towrite = pagesize;
+	    
+			if(eccbuf)
+				eccbuf += mtd->oobavail;
+		}
+	}
+
+	if(retlen)
+		*retlen = len;
+	kfree(data_buf);
+    
+	return ret;
+}
+
+static int
 concat_read_oob(struct mtd_info *mtd, loff_t from, size_t len,
 		size_t * retlen, u_char * buf)
 {
@@ -638,6 +846,60 @@
 	}
 }
 
+static int concat_block_isbad(struct mtd_info *mtd, loff_t ofs)
+{
+	struct mtd_concat *concat = CONCAT(mtd);
+	int i, res = 0;
+	
+	if (!concat->subdev[0]->block_isbad)
+		return res;
+
+	if (ofs > mtd->size)
+		return -EINVAL;
+		
+	for (i = 0; i < concat->num_subdev; i++) {
+		struct mtd_info *subdev = concat->subdev[i];
+
+		if (ofs >= subdev->size) {
+			ofs -= subdev->size;
+			continue;
+		}
+		
+		res = subdev->block_isbad(subdev, ofs);
+
+		break;
+	}
+
+	return res;
+}
+
+static int concat_block_markbad(struct mtd_info *mtd, loff_t ofs)
+{
+	struct mtd_concat *concat = CONCAT(mtd);
+	int i, err = -EINVAL;
+	
+	if (!concat->subdev[0]->block_markbad)
+		return 0;
+
+	if (ofs > mtd->size)
+		return -EINVAL;
+		
+	for (i = 0; i < concat->num_subdev; i++) {
+		struct mtd_info *subdev = concat->subdev[i];
+
+		if (ofs >= subdev->size) {
+			ofs -= subdev->size;
+			continue;
+		}
+		
+		err = subdev->block_markbad(subdev, ofs);
+
+		break;
+	}
+
+	return err;
+}
+
 /*
  * This function constructs a virtual MTD device by concatenating
  * num_devs MTD devices. A pointer to the new device object is
@@ -685,12 +947,18 @@
 	concat->mtd.eccsize = subdev[0]->eccsize;
 	if (subdev[0]->read_ecc)
 		concat->mtd.read_ecc = concat_read_ecc;
-	if (subdev[0]->write_ecc)
+	if (subdev[0]->write_ecc) {
 		concat->mtd.write_ecc = concat_write_ecc;
+		concat->mtd.writev_ecc = concat_writev_ecc;
+	}
 	if (subdev[0]->read_oob)
 		concat->mtd.read_oob = concat_read_oob;
 	if (subdev[0]->write_oob)
 		concat->mtd.write_oob = concat_write_oob;
+	if (subdev[0]->block_isbad)
+		concat->mtd.block_isbad = concat_block_isbad;
+	if (subdev[0]->block_markbad)
+		concat->mtd.block_markbad = concat_block_markbad;
 
 	concat->subdev[0] = subdev[0];
 
@@ -736,17 +1004,16 @@
 
 	}
 
+	if(concat->mtd.type == MTD_NANDFLASH)
+
memcpy(&concat->mtd.oobinfo,&subdev[0]->oobinfo,sizeof(struct
nand_oobinfo));
+
 	concat->num_subdev = num_devs;
 	concat->mtd.name = name;
 
-	/*
-	 * NOTE: for now, we do not provide any readv()/writev() methods
-	 *       because they are messy to implement and they are not
-	 *       used to a great extent anyway.
-	 */
 	concat->mtd.erase = concat_erase;
 	concat->mtd.read = concat_read;
 	concat->mtd.write = concat_write;
+	concat->mtd.writev = concat_writev;
 	concat->mtd.sync = concat_sync;
 	concat->mtd.lock = concat_lock;
 	concat->mtd.unlock = concat_unlock;

--
Thanks,
Alexander Belyakov

-------
The content of this message is my personal opinion only and although
I am an employee of Intel, the statements I make here in no way
represent 
Intel's position on the issue




More information about the linux-mtd mailing list