1 /* SPDX-License-Identifier: GPL-2.0-or-later */
9 #include <linux/types.h>
10 #include <linux/uio.h>
11 #include <linux/list.h>
12 #include <linux/notifier.h>
13 #include <linux/device.h>
15 #include <linux/nvmem-provider.h>
17 #include <mtd/mtd-abi.h>
19 #include <asm/div64.h>
21 #define MTD_FAIL_ADDR_UNKNOWN -1LL
26 * If the erase fails, fail_addr might indicate exactly which block failed. If
27 * fail_addr = MTD_FAIL_ADDR_UNKNOWN, the failure was not at the device level
28 * or was not specific to any particular block.
36 struct mtd_erase_region_info {
37 uint64_t offset; /* At which this region starts, from the beginning of the MTD */
38 uint32_t erasesize; /* For this region */
39 uint32_t numblocks; /* Number of blocks of erasesize in this region */
40 unsigned long *lockmap; /* If keeping bitmap of locks */
44 * struct mtd_oob_ops - oob operation operands
45 * @mode: operation mode
47 * @len: number of data bytes to write/read
49 * @retlen: number of data bytes written/read
51 * @ooblen: number of oob bytes to write/read
52 * @oobretlen: number of oob bytes written/read
53 * @ooboffs: offset of oob data in the oob area (only relevant when
54 * mode = MTD_OPS_PLACE_OOB or MTD_OPS_RAW)
55 * @datbuf: data buffer - if NULL only oob data are read/written
56 * @oobbuf: oob data buffer
58 * Note, some MTD drivers do not allow you to write more than one OOB area at
59 * one go. If you try to do that on such an MTD device, -EINVAL will be
60 * returned. If you want to make your implementation portable on all kind of MTD
61 * devices you should split the write request into several sub-requests when the
62 * request crosses a page boundary.
76 * struct mtd_oob_region - oob region definition
77 * @offset: region offset
78 * @length: region length
80 * This structure describes a region of the OOB area, and is used
81 * to retrieve ECC or free bytes sections.
82 * Each section is defined by an offset within the OOB area and a
85 struct mtd_oob_region {
91 * struct mtd_ooblayout_ops - NAND OOB layout operations
92 * @ecc: function returning an ECC region in the OOB area.
93 * Should return -ERANGE if %section exceeds the total number of
95 * @free: function returning a free region in the OOB area.
96 * Should return -ERANGE if %section exceeds the total number of
99 struct mtd_ooblayout_ops {
100 int (*ecc)(struct mtd_info *mtd, int section,
101 struct mtd_oob_region *oobecc);
102 int (*free)(struct mtd_info *mtd, int section,
103 struct mtd_oob_region *oobfree);
107 * struct mtd_pairing_info - page pairing information
112 * The term "pair" is used here, even though TLC NANDs might group pages by 3
113 * (3 bits in a single cell). A pair should regroup all pages that are sharing
114 * the same cell. Pairs are then indexed in ascending order.
116 * @group is defining the position of a page in a given pair. It can also be
117 * seen as the bit position in the cell: page attached to bit 0 belongs to
118 * group 0, page attached to bit 1 belongs to group 1, etc.
121 * The H27UCG8T2BTR-BC datasheet describes the following pairing scheme:
125 * pair-0 page-0 page-4
126 * pair-1 page-1 page-5
127 * pair-2 page-2 page-8
129 * pair-127 page-251 page-255
132 * Note that the "group" and "pair" terms were extracted from Samsung and
133 * Hynix datasheets, and might be referenced under other names in other
134 * datasheets (Micron is describing this concept as "shared pages").
136 struct mtd_pairing_info {
142 * struct mtd_pairing_scheme - page pairing scheme description
144 * @ngroups: number of groups. Should be related to the number of bits
146 * @get_info: converts a write-unit (page number within an erase block) into
147 * mtd_pairing information (pair + group). This function should
148 * fill the info parameter based on the wunit index or return
149 * -EINVAL if the wunit parameter is invalid.
150 * @get_wunit: converts pairing information into a write-unit (page) number.
151 * This function should return the wunit index pointed by the
152 * pairing information described in the info argument. It should
153 * return -EINVAL, if there's no wunit corresponding to the
154 * passed pairing information.
156 * See mtd_pairing_info documentation for a detailed explanation of the
157 * pair and group concepts.
159 * The mtd_pairing_scheme structure provides a generic solution to represent
160 * NAND page pairing scheme. Instead of exposing two big tables to do the
161 * write-unit <-> (pair + group) conversions, we ask the MTD drivers to
162 * implement the ->get_info() and ->get_wunit() functions.
164 * MTD users will then be able to query these information by using the
165 * mtd_pairing_info_to_wunit() and mtd_wunit_to_pairing_info() helpers.
167 * @ngroups is here to help MTD users iterating over all the pages in a
168 * given pair. This value can be retrieved by MTD users using the
169 * mtd_pairing_groups() helper.
171 * Examples are given in the mtd_pairing_info_to_wunit() and
172 * mtd_wunit_to_pairing_info() documentation.
174 struct mtd_pairing_scheme {
176 int (*get_info)(struct mtd_info *mtd, int wunit,
177 struct mtd_pairing_info *info);
178 int (*get_wunit)(struct mtd_info *mtd,
179 const struct mtd_pairing_info *info);
182 struct module; /* only needed for owner field in mtd_info */
185 * struct mtd_debug_info - debugging information for an MTD device.
187 * @dfs_dir: direntry object of the MTD device debugfs directory
189 struct mtd_debug_info {
190 struct dentry *dfs_dir;
192 const char *partname;
197 * struct mtd_part - MTD partition specific fields
199 * @node: list node used to add an MTD partition to the parent partition list
200 * @offset: offset of the partition relatively to the parent offset
201 * @size: partition size. Should be equal to mtd->size unless
202 * MTD_SLC_ON_MLC_EMULATION is set
203 * @flags: original flags (before the mtdpart logic decided to tweak them based
204 * on flash constraints, like eraseblock/pagesize alignment)
206 * This struct is embedded in mtd_info and contains partition-specific
210 struct list_head node;
217 * struct mtd_master - MTD master specific fields
219 * @partitions_lock: lock protecting accesses to the partition list. Protects
220 * not only the master partition list, but also all
222 * @suspended: et to 1 when the device is suspended, 0 otherwise
224 * This struct is embedded in mtd_info and contains master-specific
225 * properties/fields. The master is the root MTD device from the MTD partition
229 struct mutex partitions_lock;
230 struct mutex chrdev_lock;
231 unsigned int suspended : 1;
237 uint64_t size; // Total size of the MTD
239 /* "Major" erase size for the device. Naïve users may take this
240 * to be the only erase size available, or may use the more detailed
241 * information below if they desire
244 /* Minimal writable flash unit size. In case of NOR flash it is 1 (even
245 * though individual bits can be cleared), in case of NAND flash it is
246 * one NAND page (or half, or one-fourths of it), in case of ECC-ed NOR
247 * it is of ECC block size, etc. It is illegal to have writesize = 0.
248 * Any driver registering a struct mtd_info must ensure a writesize of
254 * Size of the write buffer used by the MTD. MTD devices having a write
255 * buffer can write multiple writesize chunks at a time. E.g. while
256 * writing 4 * writesize bytes to a device with 2 * writesize bytes
257 * buffer the MTD driver can (but doesn't have to) do 2 writesize
258 * operations, but not 4. Currently, all NANDs have writebufsize
259 * equivalent to writesize (NAND page size). Some NOR flashes do have
260 * writebufsize greater than writesize.
262 uint32_t writebufsize;
264 uint32_t oobsize; // Amount of OOB data per block (e.g. 16)
265 uint32_t oobavail; // Available OOB bytes per block
268 * If erasesize is a power of 2 then the shift is stored in
269 * erasesize_shift otherwise erasesize_shift is zero. Ditto writesize.
271 unsigned int erasesize_shift;
272 unsigned int writesize_shift;
273 /* Masks based on erasesize_shift and writesize_shift */
274 unsigned int erasesize_mask;
275 unsigned int writesize_mask;
278 * read ops return -EUCLEAN if max number of bitflips corrected on any
279 * one region comprising an ecc step equals or exceeds this value.
280 * Settable by driver, else defaults to ecc_strength. User can override
281 * in sysfs. N.B. The meaning of the -EUCLEAN return code has changed;
282 * see Documentation/ABI/testing/sysfs-class-mtd for more detail.
284 unsigned int bitflip_threshold;
286 /* Kernel-only stuff starts here. */
290 /* OOB layout description */
291 const struct mtd_ooblayout_ops *ooblayout;
293 /* NAND pairing scheme, only provided for MLC/TLC NANDs */
294 const struct mtd_pairing_scheme *pairing;
296 /* the ecc step size. */
297 unsigned int ecc_step_size;
299 /* max number of correctible bit errors per ecc step */
300 unsigned int ecc_strength;
302 /* Data for variable erase regions. If numeraseregions is zero,
303 * it means that the whole device has erasesize as given above.
306 struct mtd_erase_region_info *eraseregions;
309 * Do not call via these pointers, use corresponding mtd_*()
312 int (*_erase) (struct mtd_info *mtd, struct erase_info *instr);
313 int (*_point) (struct mtd_info *mtd, loff_t from, size_t len,
314 size_t *retlen, void **virt, resource_size_t *phys);
315 int (*_unpoint) (struct mtd_info *mtd, loff_t from, size_t len);
316 int (*_read) (struct mtd_info *mtd, loff_t from, size_t len,
317 size_t *retlen, u_char *buf);
318 int (*_write) (struct mtd_info *mtd, loff_t to, size_t len,
319 size_t *retlen, const u_char *buf);
320 int (*_panic_write) (struct mtd_info *mtd, loff_t to, size_t len,
321 size_t *retlen, const u_char *buf);
322 int (*_read_oob) (struct mtd_info *mtd, loff_t from,
323 struct mtd_oob_ops *ops);
324 int (*_write_oob) (struct mtd_info *mtd, loff_t to,
325 struct mtd_oob_ops *ops);
326 int (*_get_fact_prot_info) (struct mtd_info *mtd, size_t len,
327 size_t *retlen, struct otp_info *buf);
328 int (*_read_fact_prot_reg) (struct mtd_info *mtd, loff_t from,
329 size_t len, size_t *retlen, u_char *buf);
330 int (*_get_user_prot_info) (struct mtd_info *mtd, size_t len,
331 size_t *retlen, struct otp_info *buf);
332 int (*_read_user_prot_reg) (struct mtd_info *mtd, loff_t from,
333 size_t len, size_t *retlen, u_char *buf);
334 int (*_write_user_prot_reg) (struct mtd_info *mtd, loff_t to,
335 size_t len, size_t *retlen,
337 int (*_lock_user_prot_reg) (struct mtd_info *mtd, loff_t from,
339 int (*_erase_user_prot_reg) (struct mtd_info *mtd, loff_t from,
341 int (*_writev) (struct mtd_info *mtd, const struct kvec *vecs,
342 unsigned long count, loff_t to, size_t *retlen);
343 void (*_sync) (struct mtd_info *mtd);
344 int (*_lock) (struct mtd_info *mtd, loff_t ofs, uint64_t len);
345 int (*_unlock) (struct mtd_info *mtd, loff_t ofs, uint64_t len);
346 int (*_is_locked) (struct mtd_info *mtd, loff_t ofs, uint64_t len);
347 int (*_block_isreserved) (struct mtd_info *mtd, loff_t ofs);
348 int (*_block_isbad) (struct mtd_info *mtd, loff_t ofs);
349 int (*_block_markbad) (struct mtd_info *mtd, loff_t ofs);
350 int (*_max_bad_blocks) (struct mtd_info *mtd, loff_t ofs, size_t len);
351 int (*_suspend) (struct mtd_info *mtd);
352 void (*_resume) (struct mtd_info *mtd);
353 void (*_reboot) (struct mtd_info *mtd);
355 * If the driver is something smart, like UBI, it may need to maintain
356 * its own reference counting. The below functions are only for driver.
358 int (*_get_device) (struct mtd_info *mtd);
359 void (*_put_device) (struct mtd_info *mtd);
362 * flag indicates a panic write, low level drivers can take appropriate
363 * action if required to ensure writes go through
365 bool oops_panic_write;
367 struct notifier_block reboot_notifier; /* default mode before reboot */
369 /* ECC status information */
370 struct mtd_ecc_stats ecc_stats;
371 /* Subpage shift (NAND) */
376 struct module *owner;
379 struct mtd_debug_info dbg;
380 struct nvmem_device *nvmem;
381 struct nvmem_device *otp_user_nvmem;
382 struct nvmem_device *otp_factory_nvmem;
385 * Parent device from the MTD partition point of view.
387 * MTD masters do not have any parent, MTD partitions do. The parent
388 * MTD device can itself be a partition.
390 struct mtd_info *parent;
392 /* List of partitions attached to this MTD device */
393 struct list_head partitions;
396 struct mtd_part part;
397 struct mtd_master master;
401 static inline struct mtd_info *mtd_get_master(struct mtd_info *mtd)
409 static inline u64 mtd_get_master_ofs(struct mtd_info *mtd, u64 ofs)
411 while (mtd->parent) {
412 ofs += mtd->part.offset;
419 static inline bool mtd_is_partition(const struct mtd_info *mtd)
424 static inline bool mtd_has_partitions(const struct mtd_info *mtd)
426 return !list_empty(&mtd->partitions);
429 int mtd_ooblayout_ecc(struct mtd_info *mtd, int section,
430 struct mtd_oob_region *oobecc);
431 int mtd_ooblayout_find_eccregion(struct mtd_info *mtd, int eccbyte,
433 struct mtd_oob_region *oobregion);
434 int mtd_ooblayout_get_eccbytes(struct mtd_info *mtd, u8 *eccbuf,
435 const u8 *oobbuf, int start, int nbytes);
436 int mtd_ooblayout_set_eccbytes(struct mtd_info *mtd, const u8 *eccbuf,
437 u8 *oobbuf, int start, int nbytes);
438 int mtd_ooblayout_free(struct mtd_info *mtd, int section,
439 struct mtd_oob_region *oobfree);
440 int mtd_ooblayout_get_databytes(struct mtd_info *mtd, u8 *databuf,
441 const u8 *oobbuf, int start, int nbytes);
442 int mtd_ooblayout_set_databytes(struct mtd_info *mtd, const u8 *databuf,
443 u8 *oobbuf, int start, int nbytes);
444 int mtd_ooblayout_count_freebytes(struct mtd_info *mtd);
445 int mtd_ooblayout_count_eccbytes(struct mtd_info *mtd);
447 static inline void mtd_set_ooblayout(struct mtd_info *mtd,
448 const struct mtd_ooblayout_ops *ooblayout)
450 mtd->ooblayout = ooblayout;
453 static inline void mtd_set_pairing_scheme(struct mtd_info *mtd,
454 const struct mtd_pairing_scheme *pairing)
456 mtd->pairing = pairing;
459 static inline void mtd_set_of_node(struct mtd_info *mtd,
460 struct device_node *np)
462 mtd->dev.of_node = np;
464 of_property_read_string(np, "label", &mtd->name);
467 static inline struct device_node *mtd_get_of_node(struct mtd_info *mtd)
469 return dev_of_node(&mtd->dev);
472 static inline u32 mtd_oobavail(struct mtd_info *mtd, struct mtd_oob_ops *ops)
474 return ops->mode == MTD_OPS_AUTO_OOB ? mtd->oobavail : mtd->oobsize;
477 static inline int mtd_max_bad_blocks(struct mtd_info *mtd,
478 loff_t ofs, size_t len)
480 struct mtd_info *master = mtd_get_master(mtd);
482 if (!master->_max_bad_blocks)
485 if (mtd->size < (len + ofs) || ofs < 0)
488 return master->_max_bad_blocks(master, mtd_get_master_ofs(mtd, ofs),
492 int mtd_wunit_to_pairing_info(struct mtd_info *mtd, int wunit,
493 struct mtd_pairing_info *info);
494 int mtd_pairing_info_to_wunit(struct mtd_info *mtd,
495 const struct mtd_pairing_info *info);
496 int mtd_pairing_groups(struct mtd_info *mtd);
497 int mtd_erase(struct mtd_info *mtd, struct erase_info *instr);
498 int mtd_point(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
499 void **virt, resource_size_t *phys);
500 int mtd_unpoint(struct mtd_info *mtd, loff_t from, size_t len);
501 unsigned long mtd_get_unmapped_area(struct mtd_info *mtd, unsigned long len,
502 unsigned long offset, unsigned long flags);
503 int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
505 int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
507 int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
510 int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops);
511 int mtd_write_oob(struct mtd_info *mtd, loff_t to, struct mtd_oob_ops *ops);
513 int mtd_get_fact_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
514 struct otp_info *buf);
515 int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
516 size_t *retlen, u_char *buf);
517 int mtd_get_user_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
518 struct otp_info *buf);
519 int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
520 size_t *retlen, u_char *buf);
521 int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len,
522 size_t *retlen, const u_char *buf);
523 int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len);
524 int mtd_erase_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len);
526 int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
527 unsigned long count, loff_t to, size_t *retlen);
529 static inline void mtd_sync(struct mtd_info *mtd)
531 struct mtd_info *master = mtd_get_master(mtd);
534 master->_sync(master);
537 int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
538 int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
539 int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len);
540 int mtd_block_isreserved(struct mtd_info *mtd, loff_t ofs);
541 int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs);
542 int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs);
544 static inline int mtd_suspend(struct mtd_info *mtd)
546 struct mtd_info *master = mtd_get_master(mtd);
549 if (master->master.suspended)
552 ret = master->_suspend ? master->_suspend(master) : 0;
556 master->master.suspended = 1;
560 static inline void mtd_resume(struct mtd_info *mtd)
562 struct mtd_info *master = mtd_get_master(mtd);
564 if (!master->master.suspended)
568 master->_resume(master);
570 master->master.suspended = 0;
573 static inline uint32_t mtd_div_by_eb(uint64_t sz, struct mtd_info *mtd)
575 if (mtd->erasesize_shift)
576 return sz >> mtd->erasesize_shift;
577 do_div(sz, mtd->erasesize);
581 static inline uint32_t mtd_mod_by_eb(uint64_t sz, struct mtd_info *mtd)
583 if (mtd->erasesize_shift)
584 return sz & mtd->erasesize_mask;
585 return do_div(sz, mtd->erasesize);
589 * mtd_align_erase_req - Adjust an erase request to align things on eraseblock
591 * @mtd: the MTD device this erase request applies on
592 * @req: the erase request to adjust
594 * This function will adjust @req->addr and @req->len to align them on
595 * @mtd->erasesize. Of course we expect @mtd->erasesize to be != 0.
597 static inline void mtd_align_erase_req(struct mtd_info *mtd,
598 struct erase_info *req)
602 if (WARN_ON(!mtd->erasesize))
605 mod = mtd_mod_by_eb(req->addr, mtd);
611 mod = mtd_mod_by_eb(req->addr + req->len, mtd);
613 req->len += mtd->erasesize - mod;
616 static inline uint32_t mtd_div_by_ws(uint64_t sz, struct mtd_info *mtd)
618 if (mtd->writesize_shift)
619 return sz >> mtd->writesize_shift;
620 do_div(sz, mtd->writesize);
624 static inline uint32_t mtd_mod_by_ws(uint64_t sz, struct mtd_info *mtd)
626 if (mtd->writesize_shift)
627 return sz & mtd->writesize_mask;
628 return do_div(sz, mtd->writesize);
631 static inline int mtd_wunit_per_eb(struct mtd_info *mtd)
633 struct mtd_info *master = mtd_get_master(mtd);
635 return master->erasesize / mtd->writesize;
638 static inline int mtd_offset_to_wunit(struct mtd_info *mtd, loff_t offs)
640 return mtd_div_by_ws(mtd_mod_by_eb(offs, mtd), mtd);
643 static inline loff_t mtd_wunit_to_offset(struct mtd_info *mtd, loff_t base,
646 return base + (wunit * mtd->writesize);
650 static inline int mtd_has_oob(const struct mtd_info *mtd)
652 struct mtd_info *master = mtd_get_master((struct mtd_info *)mtd);
654 return master->_read_oob && master->_write_oob;
657 static inline int mtd_type_is_nand(const struct mtd_info *mtd)
659 return mtd->type == MTD_NANDFLASH || mtd->type == MTD_MLCNANDFLASH;
662 static inline int mtd_can_have_bb(const struct mtd_info *mtd)
664 struct mtd_info *master = mtd_get_master((struct mtd_info *)mtd);
666 return !!master->_block_isbad;
669 /* Kernel-side ioctl definitions */
671 struct mtd_partition;
672 struct mtd_part_parser_data;
674 extern int mtd_device_parse_register(struct mtd_info *mtd,
675 const char * const *part_probe_types,
676 struct mtd_part_parser_data *parser_data,
677 const struct mtd_partition *defparts,
679 #define mtd_device_register(master, parts, nr_parts) \
680 mtd_device_parse_register(master, NULL, NULL, parts, nr_parts)
681 extern int mtd_device_unregister(struct mtd_info *master);
682 extern struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num);
683 extern int __get_mtd_device(struct mtd_info *mtd);
684 extern void __put_mtd_device(struct mtd_info *mtd);
685 extern struct mtd_info *get_mtd_device_nm(const char *name);
686 extern void put_mtd_device(struct mtd_info *mtd);
689 struct mtd_notifier {
690 void (*add)(struct mtd_info *mtd);
691 void (*remove)(struct mtd_info *mtd);
692 struct list_head list;
696 extern void register_mtd_user (struct mtd_notifier *new);
697 extern int unregister_mtd_user (struct mtd_notifier *old);
698 void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size);
700 static inline int mtd_is_bitflip(int err) {
701 return err == -EUCLEAN;
704 static inline int mtd_is_eccerr(int err) {
705 return err == -EBADMSG;
708 static inline int mtd_is_bitflip_or_eccerr(int err) {
709 return mtd_is_bitflip(err) || mtd_is_eccerr(err);
712 unsigned mtd_mmap_capabilities(struct mtd_info *mtd);
714 extern char *mtd_expert_analysis_warning;
715 extern bool mtd_expert_analysis_mode;
717 #endif /* __MTD_MTD_H__ */