147ae6693SKyungmin Park /* 247ae6693SKyungmin Park * Copyright (c) International Business Machines Corp., 2006 347ae6693SKyungmin Park * 41a459660SWolfgang Denk * SPDX-License-Identifier: GPL-2.0+ 547ae6693SKyungmin Park * 647ae6693SKyungmin Park * Author: Artem Bityutskiy (Битюцкий Артём) 747ae6693SKyungmin Park */ 847ae6693SKyungmin Park 947ae6693SKyungmin Park #ifndef __LINUX_UBI_H__ 1047ae6693SKyungmin Park #define __LINUX_UBI_H__ 1147ae6693SKyungmin Park 1247ae6693SKyungmin Park #include <linux/types.h> 13ff94bc40SHeiko Schocher #ifndef __UBOOT__ 14ff94bc40SHeiko Schocher #include <linux/ioctl.h> 15*0195a7bbSHeiko Schocher #include <linux/scatterlist.h> 1647ae6693SKyungmin Park #include <mtd/ubi-user.h> 17ff94bc40SHeiko Schocher #endif 18ff94bc40SHeiko Schocher 19ff94bc40SHeiko Schocher /* All voumes/LEBs */ 20ff94bc40SHeiko Schocher #define UBI_ALL -1 2147ae6693SKyungmin Park 2247ae6693SKyungmin Park /* 23*0195a7bbSHeiko Schocher * Maximum number of scatter gather list entries, 24*0195a7bbSHeiko Schocher * we use only 64 to have a lower memory foot print. 25*0195a7bbSHeiko Schocher */ 26*0195a7bbSHeiko Schocher #define UBI_MAX_SG_COUNT 64 27*0195a7bbSHeiko Schocher 28*0195a7bbSHeiko Schocher /* 2947ae6693SKyungmin Park * enum ubi_open_mode - UBI volume open mode constants. 3047ae6693SKyungmin Park * 3147ae6693SKyungmin Park * UBI_READONLY: read-only mode 3247ae6693SKyungmin Park * UBI_READWRITE: read-write mode 3347ae6693SKyungmin Park * UBI_EXCLUSIVE: exclusive mode 34*0195a7bbSHeiko Schocher * UBI_METAONLY: modify only the volume meta-data, 35*0195a7bbSHeiko Schocher * i.e. the data stored in the volume table, but not in any of volume LEBs. 3647ae6693SKyungmin Park */ 3747ae6693SKyungmin Park enum { 3847ae6693SKyungmin Park UBI_READONLY = 1, 3947ae6693SKyungmin Park UBI_READWRITE, 40*0195a7bbSHeiko Schocher UBI_EXCLUSIVE, 41*0195a7bbSHeiko Schocher UBI_METAONLY 4247ae6693SKyungmin Park }; 4347ae6693SKyungmin Park 4447ae6693SKyungmin Park /** 4547ae6693SKyungmin Park * struct ubi_volume_info - UBI volume description data structure. 4647ae6693SKyungmin Park * @vol_id: volume ID 4747ae6693SKyungmin Park * @ubi_num: UBI device number this volume belongs to 4847ae6693SKyungmin Park * @size: how many physical eraseblocks are reserved for this volume 4947ae6693SKyungmin Park * @used_bytes: how many bytes of data this volume contains 5047ae6693SKyungmin Park * @used_ebs: how many physical eraseblocks of this volume actually contain any 5147ae6693SKyungmin Park * data 5247ae6693SKyungmin Park * @vol_type: volume type (%UBI_DYNAMIC_VOLUME or %UBI_STATIC_VOLUME) 5347ae6693SKyungmin Park * @corrupted: non-zero if the volume is corrupted (static volumes only) 5447ae6693SKyungmin Park * @upd_marker: non-zero if the volume has update marker set 5547ae6693SKyungmin Park * @alignment: volume alignment 5647ae6693SKyungmin Park * @usable_leb_size: how many bytes are available in logical eraseblocks of 5747ae6693SKyungmin Park * this volume 5847ae6693SKyungmin Park * @name_len: volume name length 5947ae6693SKyungmin Park * @name: volume name 6047ae6693SKyungmin Park * @cdev: UBI volume character device major and minor numbers 6147ae6693SKyungmin Park * 6247ae6693SKyungmin Park * The @corrupted flag is only relevant to static volumes and is always zero 6347ae6693SKyungmin Park * for dynamic ones. This is because UBI does not care about dynamic volume 6447ae6693SKyungmin Park * data protection and only cares about protecting static volume data. 6547ae6693SKyungmin Park * 6647ae6693SKyungmin Park * The @upd_marker flag is set if the volume update operation was interrupted. 6747ae6693SKyungmin Park * Before touching the volume data during the update operation, UBI first sets 6847ae6693SKyungmin Park * the update marker flag for this volume. If the volume update operation was 6947ae6693SKyungmin Park * further interrupted, the update marker indicates this. If the update marker 7047ae6693SKyungmin Park * is set, the contents of the volume is certainly damaged and a new volume 7147ae6693SKyungmin Park * update operation has to be started. 7247ae6693SKyungmin Park * 7347ae6693SKyungmin Park * To put it differently, @corrupted and @upd_marker fields have different 7447ae6693SKyungmin Park * semantics: 7547ae6693SKyungmin Park * o the @corrupted flag means that this static volume is corrupted for some 7647ae6693SKyungmin Park * reasons, but not because an interrupted volume update 7747ae6693SKyungmin Park * o the @upd_marker field means that the volume is damaged because of an 7847ae6693SKyungmin Park * interrupted update operation. 7947ae6693SKyungmin Park * 8047ae6693SKyungmin Park * I.e., the @corrupted flag is never set if the @upd_marker flag is set. 8147ae6693SKyungmin Park * 8247ae6693SKyungmin Park * The @used_bytes and @used_ebs fields are only really needed for static 8347ae6693SKyungmin Park * volumes and contain the number of bytes stored in this static volume and how 8447ae6693SKyungmin Park * many eraseblock this data occupies. In case of dynamic volumes, the 8547ae6693SKyungmin Park * @used_bytes field is equivalent to @size*@usable_leb_size, and the @used_ebs 8647ae6693SKyungmin Park * field is equivalent to @size. 8747ae6693SKyungmin Park * 8847ae6693SKyungmin Park * In general, logical eraseblock size is a property of the UBI device, not 8947ae6693SKyungmin Park * of the UBI volume. Indeed, the logical eraseblock size depends on the 9047ae6693SKyungmin Park * physical eraseblock size and on how much bytes UBI headers consume. But 9147ae6693SKyungmin Park * because of the volume alignment (@alignment), the usable size of logical 9247ae6693SKyungmin Park * eraseblocks if a volume may be less. The following equation is true: 9347ae6693SKyungmin Park * @usable_leb_size = LEB size - (LEB size mod @alignment), 9447ae6693SKyungmin Park * where LEB size is the logical eraseblock size defined by the UBI device. 9547ae6693SKyungmin Park * 9647ae6693SKyungmin Park * The alignment is multiple to the minimal flash input/output unit size or %1 9747ae6693SKyungmin Park * if all the available space is used. 9847ae6693SKyungmin Park * 9947ae6693SKyungmin Park * To put this differently, alignment may be considered is a way to change 10047ae6693SKyungmin Park * volume logical eraseblock sizes. 10147ae6693SKyungmin Park */ 10247ae6693SKyungmin Park struct ubi_volume_info { 10347ae6693SKyungmin Park int ubi_num; 10447ae6693SKyungmin Park int vol_id; 10547ae6693SKyungmin Park int size; 10647ae6693SKyungmin Park long long used_bytes; 10747ae6693SKyungmin Park int used_ebs; 10847ae6693SKyungmin Park int vol_type; 10947ae6693SKyungmin Park int corrupted; 11047ae6693SKyungmin Park int upd_marker; 11147ae6693SKyungmin Park int alignment; 11247ae6693SKyungmin Park int usable_leb_size; 11347ae6693SKyungmin Park int name_len; 11447ae6693SKyungmin Park const char *name; 11547ae6693SKyungmin Park dev_t cdev; 11647ae6693SKyungmin Park }; 11747ae6693SKyungmin Park 11847ae6693SKyungmin Park /** 119*0195a7bbSHeiko Schocher * struct ubi_sgl - UBI scatter gather list data structure. 120*0195a7bbSHeiko Schocher * @list_pos: current position in @sg[] 121*0195a7bbSHeiko Schocher * @page_pos: current position in @sg[@list_pos] 122*0195a7bbSHeiko Schocher * @sg: the scatter gather list itself 123*0195a7bbSHeiko Schocher * 124*0195a7bbSHeiko Schocher * ubi_sgl is a wrapper around a scatter list which keeps track of the 125*0195a7bbSHeiko Schocher * current position in the list and the current list item such that 126*0195a7bbSHeiko Schocher * it can be used across multiple ubi_leb_read_sg() calls. 127*0195a7bbSHeiko Schocher */ 128*0195a7bbSHeiko Schocher struct ubi_sgl { 129*0195a7bbSHeiko Schocher int list_pos; 130*0195a7bbSHeiko Schocher int page_pos; 131*0195a7bbSHeiko Schocher #ifndef __UBOOT__ 132*0195a7bbSHeiko Schocher struct scatterlist sg[UBI_MAX_SG_COUNT]; 133*0195a7bbSHeiko Schocher #endif 134*0195a7bbSHeiko Schocher }; 135*0195a7bbSHeiko Schocher 136*0195a7bbSHeiko Schocher /** 137*0195a7bbSHeiko Schocher * ubi_sgl_init - initialize an UBI scatter gather list data structure. 138*0195a7bbSHeiko Schocher * @usgl: the UBI scatter gather struct itself 139*0195a7bbSHeiko Schocher * 140*0195a7bbSHeiko Schocher * Please note that you still have to use sg_init_table() or any adequate 141*0195a7bbSHeiko Schocher * function to initialize the unterlaying struct scatterlist. 142*0195a7bbSHeiko Schocher */ 143*0195a7bbSHeiko Schocher static inline void ubi_sgl_init(struct ubi_sgl *usgl) 144*0195a7bbSHeiko Schocher { 145*0195a7bbSHeiko Schocher usgl->list_pos = 0; 146*0195a7bbSHeiko Schocher usgl->page_pos = 0; 147*0195a7bbSHeiko Schocher } 148*0195a7bbSHeiko Schocher 149*0195a7bbSHeiko Schocher /** 15047ae6693SKyungmin Park * struct ubi_device_info - UBI device description data structure. 15147ae6693SKyungmin Park * @ubi_num: ubi device number 15247ae6693SKyungmin Park * @leb_size: logical eraseblock size on this UBI device 153ff94bc40SHeiko Schocher * @leb_start: starting offset of logical eraseblocks within physical 154ff94bc40SHeiko Schocher * eraseblocks 15547ae6693SKyungmin Park * @min_io_size: minimal I/O unit size 156ff94bc40SHeiko Schocher * @max_write_size: maximum amount of bytes the underlying flash can write at a 157ff94bc40SHeiko Schocher * time (MTD write buffer size) 15847ae6693SKyungmin Park * @ro_mode: if this device is in read-only mode 15947ae6693SKyungmin Park * @cdev: UBI character device major and minor numbers 16047ae6693SKyungmin Park * 16147ae6693SKyungmin Park * Note, @leb_size is the logical eraseblock size offered by the UBI device. 16247ae6693SKyungmin Park * Volumes of this UBI device may have smaller logical eraseblock size if their 16347ae6693SKyungmin Park * alignment is not equivalent to %1. 164ff94bc40SHeiko Schocher * 165ff94bc40SHeiko Schocher * The @max_write_size field describes flash write maximum write unit. For 166ff94bc40SHeiko Schocher * example, NOR flash allows for changing individual bytes, so @min_io_size is 167ff94bc40SHeiko Schocher * %1. However, it does not mean than NOR flash has to write data byte-by-byte. 168ff94bc40SHeiko Schocher * Instead, CFI NOR flashes have a write-buffer of, e.g., 64 bytes, and when 169ff94bc40SHeiko Schocher * writing large chunks of data, they write 64-bytes at a time. Obviously, this 170ff94bc40SHeiko Schocher * improves write throughput. 171ff94bc40SHeiko Schocher * 172ff94bc40SHeiko Schocher * Also, the MTD device may have N interleaved (striped) flash chips 173ff94bc40SHeiko Schocher * underneath, in which case @min_io_size can be physical min. I/O size of 174ff94bc40SHeiko Schocher * single flash chip, while @max_write_size can be N * @min_io_size. 175ff94bc40SHeiko Schocher * 176ff94bc40SHeiko Schocher * The @max_write_size field is always greater or equivalent to @min_io_size. 177ff94bc40SHeiko Schocher * E.g., some NOR flashes may have (@min_io_size = 1, @max_write_size = 64). In 178ff94bc40SHeiko Schocher * contrast, NAND flashes usually have @min_io_size = @max_write_size = NAND 179ff94bc40SHeiko Schocher * page size. 18047ae6693SKyungmin Park */ 18147ae6693SKyungmin Park struct ubi_device_info { 18247ae6693SKyungmin Park int ubi_num; 18347ae6693SKyungmin Park int leb_size; 184ff94bc40SHeiko Schocher int leb_start; 18547ae6693SKyungmin Park int min_io_size; 186ff94bc40SHeiko Schocher int max_write_size; 18747ae6693SKyungmin Park int ro_mode; 188ff94bc40SHeiko Schocher #ifndef __UBOOT__ 18947ae6693SKyungmin Park dev_t cdev; 190ff94bc40SHeiko Schocher #endif 191ff94bc40SHeiko Schocher }; 192ff94bc40SHeiko Schocher 193ff94bc40SHeiko Schocher /* 194ff94bc40SHeiko Schocher * Volume notification types. 195ff94bc40SHeiko Schocher * @UBI_VOLUME_ADDED: a volume has been added (an UBI device was attached or a 196ff94bc40SHeiko Schocher * volume was created) 197ff94bc40SHeiko Schocher * @UBI_VOLUME_REMOVED: a volume has been removed (an UBI device was detached 198ff94bc40SHeiko Schocher * or a volume was removed) 199ff94bc40SHeiko Schocher * @UBI_VOLUME_RESIZED: a volume has been re-sized 200ff94bc40SHeiko Schocher * @UBI_VOLUME_RENAMED: a volume has been re-named 201ff94bc40SHeiko Schocher * @UBI_VOLUME_UPDATED: data has been written to a volume 202ff94bc40SHeiko Schocher * 203ff94bc40SHeiko Schocher * These constants define which type of event has happened when a volume 204ff94bc40SHeiko Schocher * notification function is invoked. 205ff94bc40SHeiko Schocher */ 206ff94bc40SHeiko Schocher enum { 207ff94bc40SHeiko Schocher UBI_VOLUME_ADDED, 208ff94bc40SHeiko Schocher UBI_VOLUME_REMOVED, 209ff94bc40SHeiko Schocher UBI_VOLUME_RESIZED, 210ff94bc40SHeiko Schocher UBI_VOLUME_RENAMED, 211ff94bc40SHeiko Schocher UBI_VOLUME_UPDATED, 212ff94bc40SHeiko Schocher }; 213ff94bc40SHeiko Schocher 214ff94bc40SHeiko Schocher /* 215ff94bc40SHeiko Schocher * struct ubi_notification - UBI notification description structure. 216ff94bc40SHeiko Schocher * @di: UBI device description object 217ff94bc40SHeiko Schocher * @vi: UBI volume description object 218ff94bc40SHeiko Schocher * 219ff94bc40SHeiko Schocher * UBI notifiers are called with a pointer to an object of this type. The 220ff94bc40SHeiko Schocher * object describes the notification. Namely, it provides a description of the 221ff94bc40SHeiko Schocher * UBI device and UBI volume the notification informs about. 222ff94bc40SHeiko Schocher */ 223ff94bc40SHeiko Schocher struct ubi_notification { 224ff94bc40SHeiko Schocher struct ubi_device_info di; 225ff94bc40SHeiko Schocher struct ubi_volume_info vi; 22647ae6693SKyungmin Park }; 22747ae6693SKyungmin Park 22847ae6693SKyungmin Park /* UBI descriptor given to users when they open UBI volumes */ 22947ae6693SKyungmin Park struct ubi_volume_desc; 23047ae6693SKyungmin Park 23147ae6693SKyungmin Park int ubi_get_device_info(int ubi_num, struct ubi_device_info *di); 23247ae6693SKyungmin Park void ubi_get_volume_info(struct ubi_volume_desc *desc, 23347ae6693SKyungmin Park struct ubi_volume_info *vi); 23447ae6693SKyungmin Park struct ubi_volume_desc *ubi_open_volume(int ubi_num, int vol_id, int mode); 23547ae6693SKyungmin Park struct ubi_volume_desc *ubi_open_volume_nm(int ubi_num, const char *name, 23647ae6693SKyungmin Park int mode); 237ff94bc40SHeiko Schocher struct ubi_volume_desc *ubi_open_volume_path(const char *pathname, int mode); 238ff94bc40SHeiko Schocher 239ff94bc40SHeiko Schocher #ifndef __UBOOT__ 240ff94bc40SHeiko Schocher typedef int (*notifier_fn_t)(void *nb, 241ff94bc40SHeiko Schocher unsigned long action, void *data); 242ff94bc40SHeiko Schocher 243ff94bc40SHeiko Schocher struct notifier_block { 244ff94bc40SHeiko Schocher notifier_fn_t notifier_call; 245ff94bc40SHeiko Schocher struct notifier_block *next; 246ff94bc40SHeiko Schocher void *next; 247ff94bc40SHeiko Schocher int priority; 248ff94bc40SHeiko Schocher }; 249ff94bc40SHeiko Schocher 250ff94bc40SHeiko Schocher int ubi_register_volume_notifier(struct notifier_block *nb, 251ff94bc40SHeiko Schocher int ignore_existing); 252ff94bc40SHeiko Schocher int ubi_unregister_volume_notifier(struct notifier_block *nb); 253ff94bc40SHeiko Schocher #endif 254ff94bc40SHeiko Schocher 25547ae6693SKyungmin Park void ubi_close_volume(struct ubi_volume_desc *desc); 25647ae6693SKyungmin Park int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset, 25747ae6693SKyungmin Park int len, int check); 258*0195a7bbSHeiko Schocher int ubi_leb_read_sg(struct ubi_volume_desc *desc, int lnum, struct ubi_sgl *sgl, 259*0195a7bbSHeiko Schocher int offset, int len, int check); 26047ae6693SKyungmin Park int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf, 261ff94bc40SHeiko Schocher int offset, int len); 26247ae6693SKyungmin Park int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf, 263ff94bc40SHeiko Schocher int len); 26447ae6693SKyungmin Park int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum); 26547ae6693SKyungmin Park int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum); 266ff94bc40SHeiko Schocher int ubi_leb_map(struct ubi_volume_desc *desc, int lnum); 26747ae6693SKyungmin Park int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum); 268ff94bc40SHeiko Schocher int ubi_sync(int ubi_num); 269ff94bc40SHeiko Schocher int ubi_flush(int ubi_num, int vol_id, int lnum); 27047ae6693SKyungmin Park 27147ae6693SKyungmin Park /* 27247ae6693SKyungmin Park * This function is the same as the 'ubi_leb_read()' function, but it does not 27347ae6693SKyungmin Park * provide the checking capability. 27447ae6693SKyungmin Park */ 27547ae6693SKyungmin Park static inline int ubi_read(struct ubi_volume_desc *desc, int lnum, char *buf, 27647ae6693SKyungmin Park int offset, int len) 27747ae6693SKyungmin Park { 27847ae6693SKyungmin Park return ubi_leb_read(desc, lnum, buf, offset, len, 0); 27947ae6693SKyungmin Park } 280*0195a7bbSHeiko Schocher 281*0195a7bbSHeiko Schocher /* 282*0195a7bbSHeiko Schocher * This function is the same as the 'ubi_leb_read_sg()' function, but it does 283*0195a7bbSHeiko Schocher * not provide the checking capability. 284*0195a7bbSHeiko Schocher */ 285*0195a7bbSHeiko Schocher static inline int ubi_read_sg(struct ubi_volume_desc *desc, int lnum, 286*0195a7bbSHeiko Schocher struct ubi_sgl *sgl, int offset, int len) 287*0195a7bbSHeiko Schocher { 288*0195a7bbSHeiko Schocher return ubi_leb_read_sg(desc, lnum, sgl, offset, len, 0); 289*0195a7bbSHeiko Schocher } 29047ae6693SKyungmin Park #endif /* !__LINUX_UBI_H__ */ 291