Skip to content

Commit cc26c3c

Browse files
computersforpeaceDavid Woodhouse
authored andcommitted
mtd: nand: expand nand_ecc_layout, deprecate ioctl ECCGETLAYOUT
struct nand_ecclayout is too small for many new chips; OOB regions can be as large as 448 bytes and may increase more in the future. Thus, copying that struct to user-space with the ECCGETLAYOUT ioctl is not a good idea; the ioctl would have to be updated every time there's a change to the current largest size. Instead, the old nand_ecclayout is renamed to nand_ecclayout_user and a new struct nand_ecclayout is created that can accomodate larger sizes and expand without affecting the user-space. struct nand_ecclayout can still be used in board drivers without modification -- at least for now. A new function is provided to convert from the new to the old in order to allow the deprecated ioctl to continue to work with truncated data. Perhaps the ioctl, the conversion process, and the struct nand_ecclayout_user can be removed altogether in the future. Note: There are comments in nand/davinci_nand.c::nand_davinci_probe() regarding this issue; this driver (and maybe others) can be updated to account for extra space. All kernel drivers can use the expanded nand_ecclayout as a drop-in replacement and ignore its benefits. Signed-off-by: Brian Norris <computersforpeace@gmail.com> Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com> Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
1 parent f4a2da0 commit cc26c3c

6 files changed

Lines changed: 67 additions & 7 deletions

File tree

‎drivers/mtd/mtdchar.c‎

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,39 @@ static int mtd_do_readoob(struct mtd_info *mtd, uint64_t start,
477477
return ret;
478478
}
479479

480+
/*
481+
* Copies (and truncates, if necessary) data from the larger struct,
482+
* nand_ecclayout, to the smaller, deprecated layout struct,
483+
* nand_ecclayout_user. This is necessary only to suppport the deprecated
484+
* API ioctl ECCGETLAYOUT while allowing all new functionality to use
485+
* nand_ecclayout flexibly (i.e. the struct may change size in new
486+
* releases without requiring major rewrites).
487+
*/
488+
static int shrink_ecclayout(const struct nand_ecclayout *from,
489+
struct nand_ecclayout_user *to)
490+
{
491+
int i;
492+
493+
if (!from || !to)
494+
return -EINVAL;
495+
496+
memset(to, 0, sizeof(*to));
497+
498+
to->eccbytes = min((int)from->eccbytes, MTD_MAX_ECCPOS_ENTRIES_OLD);
499+
for (i = 0; i < to->eccbytes; i++)
500+
to->eccpos[i] = from->eccpos[i];
501+
502+
for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES; i++) {
503+
if (from->oobfree[i].length == 0 &&
504+
from->oobfree[i].offset == 0)
505+
break;
506+
to->oobavail += from->oobfree[i].length;
507+
to->oobfree[i] = from->oobfree[i];
508+
}
509+
510+
return 0;
511+
}
512+
480513
static int mtd_ioctl(struct file *file, u_int cmd, u_long arg)
481514
{
482515
struct mtd_file_info *mfi = file->private_data;
@@ -812,14 +845,23 @@ static int mtd_ioctl(struct file *file, u_int cmd, u_long arg)
812845
}
813846
#endif
814847

848+
/* This ioctl is being deprecated - it truncates the ecc layout */
815849
case ECCGETLAYOUT:
816850
{
851+
struct nand_ecclayout_user *usrlay;
852+
817853
if (!mtd->ecclayout)
818854
return -EOPNOTSUPP;
819855

820-
if (copy_to_user(argp, mtd->ecclayout,
821-
sizeof(struct nand_ecclayout)))
822-
return -EFAULT;
856+
usrlay = kmalloc(sizeof(*usrlay), GFP_KERNEL);
857+
if (!usrlay)
858+
return -ENOMEM;
859+
860+
shrink_ecclayout(mtd->ecclayout, usrlay);
861+
862+
if (copy_to_user(argp, usrlay, sizeof(*usrlay)))
863+
ret = -EFAULT;
864+
kfree(usrlay);
823865
break;
824866
}
825867

‎drivers/mtd/nand/davinci_nand.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,9 @@ static int __init nand_davinci_probe(struct platform_device *pdev)
749749
* breaks userspace ioctl interface with mtd-utils. Once we
750750
* resolve this issue, NAND_ECC_HW_OOB_FIRST mode can be used
751751
* for the 4KiB page chips.
752+
*
753+
* TODO: Note that nand_ecclayout has now been expanded and can
754+
* hold plenty of OOB entries.
752755
*/
753756
dev_warn(&pdev->dev, "no 4-bit ECC support yet "
754757
"for 4KiB-page NAND\n");

‎include/linux/mtd/mtd.h‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,21 @@ struct mtd_oob_ops {
110110
uint8_t *oobbuf;
111111
};
112112

113+
#define MTD_MAX_OOBFREE_ENTRIES_LARGE 32
114+
#define MTD_MAX_ECCPOS_ENTRIES_LARGE 448
115+
#define MTD_MAX_ECCPOS_ENTRIES_OLD 64 /* Previous maximum */
116+
/*
117+
* Correct ECC layout control structure. This replaces old nand_ecclayout
118+
* (mtd-abi.h) that is exported via ECCGETLAYOUT ioctl. It should be expandable
119+
* in the future simply by the above macros.
120+
*/
121+
struct nand_ecclayout {
122+
__u32 eccbytes;
123+
__u32 eccpos[MTD_MAX_ECCPOS_ENTRIES_LARGE];
124+
__u32 oobavail;
125+
struct nand_oobfree oobfree[MTD_MAX_OOBFREE_ENTRIES_LARGE];
126+
};
127+
113128
struct mtd_info {
114129
u_char type;
115130
uint32_t flags;

‎include/linux/mtd/partitions.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ struct mtd_partition {
3939
uint64_t size; /* partition size */
4040
uint64_t offset; /* offset within the master MTD space */
4141
uint32_t mask_flags; /* master MTD flags to mask out for this partition */
42-
struct nand_ecclayout *ecclayout; /* out of band layout for this partition (NAND only)*/
42+
struct nand_ecclayout *ecclayout; /* out of band layout for this partition (NAND only) */
4343
};
4444

4545
#define MTDPART_OFS_NXTBLK (-2)

‎include/mtd/mtd-abi.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ struct otp_info {
119119
#define OTPGETREGIONCOUNT _IOW('M', 14, int)
120120
#define OTPGETREGIONINFO _IOW('M', 15, struct otp_info)
121121
#define OTPLOCK _IOR('M', 16, struct otp_info)
122-
#define ECCGETLAYOUT _IOR('M', 17, struct nand_ecclayout)
122+
#define ECCGETLAYOUT _IOR('M', 17, struct nand_ecclayout_user)
123123
#define ECCGETSTATS _IOR('M', 18, struct mtd_ecc_stats)
124124
#define MTDFILEMODE _IO('M', 19)
125125
#define MEMERASE64 _IOW('M', 20, struct erase_info_user64)
@@ -148,7 +148,7 @@ struct nand_oobfree {
148148
* ECC layout control structure. Exported to userspace for
149149
* diagnosis and to allow creation of raw images
150150
*/
151-
struct nand_ecclayout {
151+
struct nand_ecclayout_user {
152152
__u32 eccbytes;
153153
__u32 eccpos[64];
154154
__u32 oobavail;

‎include/mtd/mtd-user.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ typedef struct mtd_info_user mtd_info_t;
2929
typedef struct erase_info_user erase_info_t;
3030
typedef struct region_info_user region_info_t;
3131
typedef struct nand_oobinfo nand_oobinfo_t;
32-
typedef struct nand_ecclayout nand_ecclayout_t;
32+
typedef struct nand_ecclayout_user nand_ecclayout_t;
3333

3434
#endif /* __MTD_USER_H__ */

0 commit comments

Comments
 (0)