1801c135cSArtem B. Bityutskiy /* 2801c135cSArtem B. Bityutskiy * Copyright (c) International Business Machines Corp., 2006 3801c135cSArtem B. Bityutskiy * 4801c135cSArtem B. Bityutskiy * This program is free software; you can redistribute it and/or modify 5801c135cSArtem B. Bityutskiy * it under the terms of the GNU General Public License as published by 6801c135cSArtem B. Bityutskiy * the Free Software Foundation; either version 2 of the License, or 7801c135cSArtem B. Bityutskiy * (at your option) any later version. 8801c135cSArtem B. Bityutskiy * 9801c135cSArtem B. Bityutskiy * This program is distributed in the hope that it will be useful, 10801c135cSArtem B. Bityutskiy * but WITHOUT ANY WARRANTY; without even the implied warranty of 11801c135cSArtem B. Bityutskiy * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 12801c135cSArtem B. Bityutskiy * the GNU General Public License for more details. 13801c135cSArtem B. Bityutskiy * 14801c135cSArtem B. Bityutskiy * You should have received a copy of the GNU General Public License 15801c135cSArtem B. Bityutskiy * along with this program; if not, write to the Free Software 16801c135cSArtem B. Bityutskiy * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17801c135cSArtem B. Bityutskiy * 18801c135cSArtem B. Bityutskiy * Author: Artem Bityutskiy (Битюцкий Артём) 19801c135cSArtem B. Bityutskiy */ 20801c135cSArtem B. Bityutskiy 21801c135cSArtem B. Bityutskiy #ifndef __LINUX_UBI_H__ 22801c135cSArtem B. Bityutskiy #define __LINUX_UBI_H__ 23801c135cSArtem B. Bityutskiy 24feddbb34SArtem Bityutskiy #include <linux/ioctl.h> 25801c135cSArtem B. Bityutskiy #include <linux/types.h> 269ff08979SRichard Weinberger #include <linux/scatterlist.h> 27801c135cSArtem B. Bityutskiy #include <mtd/ubi-user.h> 28801c135cSArtem B. Bityutskiy 2905a3cb7dSArtem Bityutskiy /* All voumes/LEBs */ 3005a3cb7dSArtem Bityutskiy #define UBI_ALL -1 3105a3cb7dSArtem Bityutskiy 32801c135cSArtem B. Bityutskiy /* 339ff08979SRichard Weinberger * Maximum number of scatter gather list entries, 349ff08979SRichard Weinberger * we use only 64 to have a lower memory foot print. 359ff08979SRichard Weinberger */ 369ff08979SRichard Weinberger #define UBI_MAX_SG_COUNT 64 379ff08979SRichard Weinberger 389ff08979SRichard Weinberger /* 39801c135cSArtem B. Bityutskiy * enum ubi_open_mode - UBI volume open mode constants. 40801c135cSArtem B. Bityutskiy * 41801c135cSArtem B. Bityutskiy * UBI_READONLY: read-only mode 42801c135cSArtem B. Bityutskiy * UBI_READWRITE: read-write mode 43801c135cSArtem B. Bityutskiy * UBI_EXCLUSIVE: exclusive mode 44fafdd2bfSRichard Weinberger * UBI_METAONLY: modify only the volume meta-data, 45fafdd2bfSRichard Weinberger * i.e. the data stored in the volume table, but not in any of volume LEBs. 46801c135cSArtem B. Bityutskiy */ 47801c135cSArtem B. Bityutskiy enum { 48801c135cSArtem B. Bityutskiy UBI_READONLY = 1, 49801c135cSArtem B. Bityutskiy UBI_READWRITE, 50fafdd2bfSRichard Weinberger UBI_EXCLUSIVE, 51fafdd2bfSRichard Weinberger UBI_METAONLY 52801c135cSArtem B. Bityutskiy }; 53801c135cSArtem B. Bityutskiy 54801c135cSArtem B. Bityutskiy /** 55801c135cSArtem B. Bityutskiy * struct ubi_volume_info - UBI volume description data structure. 56801c135cSArtem B. Bityutskiy * @vol_id: volume ID 57801c135cSArtem B. Bityutskiy * @ubi_num: UBI device number this volume belongs to 58801c135cSArtem B. Bityutskiy * @size: how many physical eraseblocks are reserved for this volume 59801c135cSArtem B. Bityutskiy * @used_bytes: how many bytes of data this volume contains 60801c135cSArtem B. Bityutskiy * @used_ebs: how many physical eraseblocks of this volume actually contain any 61801c135cSArtem B. Bityutskiy * data 62801c135cSArtem B. Bityutskiy * @vol_type: volume type (%UBI_DYNAMIC_VOLUME or %UBI_STATIC_VOLUME) 63801c135cSArtem B. Bityutskiy * @corrupted: non-zero if the volume is corrupted (static volumes only) 64801c135cSArtem B. Bityutskiy * @upd_marker: non-zero if the volume has update marker set 65801c135cSArtem B. Bityutskiy * @alignment: volume alignment 66801c135cSArtem B. Bityutskiy * @usable_leb_size: how many bytes are available in logical eraseblocks of 67801c135cSArtem B. Bityutskiy * this volume 68801c135cSArtem B. Bityutskiy * @name_len: volume name length 69801c135cSArtem B. Bityutskiy * @name: volume name 70801c135cSArtem B. Bityutskiy * @cdev: UBI volume character device major and minor numbers 71801c135cSArtem B. Bityutskiy * 72801c135cSArtem B. Bityutskiy * The @corrupted flag is only relevant to static volumes and is always zero 73801c135cSArtem B. Bityutskiy * for dynamic ones. This is because UBI does not care about dynamic volume 74801c135cSArtem B. Bityutskiy * data protection and only cares about protecting static volume data. 75801c135cSArtem B. Bityutskiy * 76801c135cSArtem B. Bityutskiy * The @upd_marker flag is set if the volume update operation was interrupted. 77801c135cSArtem B. Bityutskiy * Before touching the volume data during the update operation, UBI first sets 78801c135cSArtem B. Bityutskiy * the update marker flag for this volume. If the volume update operation was 79801c135cSArtem B. Bityutskiy * further interrupted, the update marker indicates this. If the update marker 80801c135cSArtem B. Bityutskiy * is set, the contents of the volume is certainly damaged and a new volume 81801c135cSArtem B. Bityutskiy * update operation has to be started. 82801c135cSArtem B. Bityutskiy * 83801c135cSArtem B. Bityutskiy * To put it differently, @corrupted and @upd_marker fields have different 84801c135cSArtem B. Bityutskiy * semantics: 85801c135cSArtem B. Bityutskiy * o the @corrupted flag means that this static volume is corrupted for some 86801c135cSArtem B. Bityutskiy * reasons, but not because an interrupted volume update 87801c135cSArtem B. Bityutskiy * o the @upd_marker field means that the volume is damaged because of an 88801c135cSArtem B. Bityutskiy * interrupted update operation. 89801c135cSArtem B. Bityutskiy * 90801c135cSArtem B. Bityutskiy * I.e., the @corrupted flag is never set if the @upd_marker flag is set. 91801c135cSArtem B. Bityutskiy * 92801c135cSArtem B. Bityutskiy * The @used_bytes and @used_ebs fields are only really needed for static 93801c135cSArtem B. Bityutskiy * volumes and contain the number of bytes stored in this static volume and how 94801c135cSArtem B. Bityutskiy * many eraseblock this data occupies. In case of dynamic volumes, the 95801c135cSArtem B. Bityutskiy * @used_bytes field is equivalent to @size*@usable_leb_size, and the @used_ebs 96801c135cSArtem B. Bityutskiy * field is equivalent to @size. 97801c135cSArtem B. Bityutskiy * 98801c135cSArtem B. Bityutskiy * In general, logical eraseblock size is a property of the UBI device, not 99801c135cSArtem B. Bityutskiy * of the UBI volume. Indeed, the logical eraseblock size depends on the 100801c135cSArtem B. Bityutskiy * physical eraseblock size and on how much bytes UBI headers consume. But 101801c135cSArtem B. Bityutskiy * because of the volume alignment (@alignment), the usable size of logical 102801c135cSArtem B. Bityutskiy * eraseblocks if a volume may be less. The following equation is true: 103801c135cSArtem B. Bityutskiy * @usable_leb_size = LEB size - (LEB size mod @alignment), 104801c135cSArtem B. Bityutskiy * where LEB size is the logical eraseblock size defined by the UBI device. 105801c135cSArtem B. Bityutskiy * 106801c135cSArtem B. Bityutskiy * The alignment is multiple to the minimal flash input/output unit size or %1 107801c135cSArtem B. Bityutskiy * if all the available space is used. 108801c135cSArtem B. Bityutskiy * 109801c135cSArtem B. Bityutskiy * To put this differently, alignment may be considered is a way to change 110801c135cSArtem B. Bityutskiy * volume logical eraseblock sizes. 111801c135cSArtem B. Bityutskiy */ 112801c135cSArtem B. Bityutskiy struct ubi_volume_info { 113801c135cSArtem B. Bityutskiy int ubi_num; 114801c135cSArtem B. Bityutskiy int vol_id; 115801c135cSArtem B. Bityutskiy int size; 116801c135cSArtem B. Bityutskiy long long used_bytes; 117801c135cSArtem B. Bityutskiy int used_ebs; 118801c135cSArtem B. Bityutskiy int vol_type; 119801c135cSArtem B. Bityutskiy int corrupted; 120801c135cSArtem B. Bityutskiy int upd_marker; 121801c135cSArtem B. Bityutskiy int alignment; 122801c135cSArtem B. Bityutskiy int usable_leb_size; 123801c135cSArtem B. Bityutskiy int name_len; 124801c135cSArtem B. Bityutskiy const char *name; 125801c135cSArtem B. Bityutskiy dev_t cdev; 126801c135cSArtem B. Bityutskiy }; 127801c135cSArtem B. Bityutskiy 128801c135cSArtem B. Bityutskiy /** 1299ff08979SRichard Weinberger * struct ubi_sgl - UBI scatter gather list data structure. 1309ff08979SRichard Weinberger * @list_pos: current position in @sg[] 1319ff08979SRichard Weinberger * @page_pos: current position in @sg[@list_pos] 1329ff08979SRichard Weinberger * @sg: the scatter gather list itself 1339ff08979SRichard Weinberger * 1349ff08979SRichard Weinberger * ubi_sgl is a wrapper around a scatter list which keeps track of the 1359ff08979SRichard Weinberger * current position in the list and the current list item such that 1369ff08979SRichard Weinberger * it can be used across multiple ubi_leb_read_sg() calls. 1379ff08979SRichard Weinberger */ 1389ff08979SRichard Weinberger struct ubi_sgl { 1399ff08979SRichard Weinberger int list_pos; 1409ff08979SRichard Weinberger int page_pos; 1419ff08979SRichard Weinberger struct scatterlist sg[UBI_MAX_SG_COUNT]; 1429ff08979SRichard Weinberger }; 1439ff08979SRichard Weinberger 1449ff08979SRichard Weinberger /** 1459ff08979SRichard Weinberger * ubi_sgl_init - initialize an UBI scatter gather list data structure. 1469ff08979SRichard Weinberger * @usgl: the UBI scatter gather struct itself 1479ff08979SRichard Weinberger * 1489ff08979SRichard Weinberger * Please note that you still have to use sg_init_table() or any adequate 1499ff08979SRichard Weinberger * function to initialize the unterlaying struct scatterlist. 1509ff08979SRichard Weinberger */ 1519ff08979SRichard Weinberger static inline void ubi_sgl_init(struct ubi_sgl *usgl) 1529ff08979SRichard Weinberger { 1539ff08979SRichard Weinberger usgl->list_pos = 0; 1549ff08979SRichard Weinberger usgl->page_pos = 0; 1559ff08979SRichard Weinberger } 1569ff08979SRichard Weinberger 1579ff08979SRichard Weinberger /** 158801c135cSArtem B. Bityutskiy * struct ubi_device_info - UBI device description data structure. 159801c135cSArtem B. Bityutskiy * @ubi_num: ubi device number 160801c135cSArtem B. Bityutskiy * @leb_size: logical eraseblock size on this UBI device 161f43ec882SArtem Bityutskiy * @leb_start: starting offset of logical eraseblocks within physical 162f43ec882SArtem Bityutskiy * eraseblocks 163801c135cSArtem B. Bityutskiy * @min_io_size: minimal I/O unit size 16430b542efSArtem Bityutskiy * @max_write_size: maximum amount of bytes the underlying flash can write at a 16530b542efSArtem Bityutskiy * time (MTD write buffer size) 166801c135cSArtem B. Bityutskiy * @ro_mode: if this device is in read-only mode 167801c135cSArtem B. Bityutskiy * @cdev: UBI character device major and minor numbers 168801c135cSArtem B. Bityutskiy * 169801c135cSArtem B. Bityutskiy * Note, @leb_size is the logical eraseblock size offered by the UBI device. 170801c135cSArtem B. Bityutskiy * Volumes of this UBI device may have smaller logical eraseblock size if their 171801c135cSArtem B. Bityutskiy * alignment is not equivalent to %1. 17230b542efSArtem Bityutskiy * 17330b542efSArtem Bityutskiy * The @max_write_size field describes flash write maximum write unit. For 17430b542efSArtem Bityutskiy * example, NOR flash allows for changing individual bytes, so @min_io_size is 17530b542efSArtem Bityutskiy * %1. However, it does not mean than NOR flash has to write data byte-by-byte. 17630b542efSArtem Bityutskiy * Instead, CFI NOR flashes have a write-buffer of, e.g., 64 bytes, and when 17730b542efSArtem Bityutskiy * writing large chunks of data, they write 64-bytes at a time. Obviously, this 17830b542efSArtem Bityutskiy * improves write throughput. 17930b542efSArtem Bityutskiy * 18030b542efSArtem Bityutskiy * Also, the MTD device may have N interleaved (striped) flash chips 18130b542efSArtem Bityutskiy * underneath, in which case @min_io_size can be physical min. I/O size of 18230b542efSArtem Bityutskiy * single flash chip, while @max_write_size can be N * @min_io_size. 18330b542efSArtem Bityutskiy * 18430b542efSArtem Bityutskiy * The @max_write_size field is always greater or equivalent to @min_io_size. 18530b542efSArtem Bityutskiy * E.g., some NOR flashes may have (@min_io_size = 1, @max_write_size = 64). In 18630b542efSArtem Bityutskiy * contrast, NAND flashes usually have @min_io_size = @max_write_size = NAND 18730b542efSArtem Bityutskiy * page size. 188801c135cSArtem B. Bityutskiy */ 189801c135cSArtem B. Bityutskiy struct ubi_device_info { 190801c135cSArtem B. Bityutskiy int ubi_num; 191801c135cSArtem B. Bityutskiy int leb_size; 192f43ec882SArtem Bityutskiy int leb_start; 193801c135cSArtem B. Bityutskiy int min_io_size; 19430b542efSArtem Bityutskiy int max_write_size; 195801c135cSArtem B. Bityutskiy int ro_mode; 196801c135cSArtem B. Bityutskiy dev_t cdev; 197801c135cSArtem B. Bityutskiy }; 198801c135cSArtem B. Bityutskiy 1990e0ee1ccSDmitry Pervushin /* 20001dc9cc3SDavid Wagner * Volume notification types. 20101dc9cc3SDavid Wagner * @UBI_VOLUME_ADDED: a volume has been added (an UBI device was attached or a 20201dc9cc3SDavid Wagner * volume was created) 20301dc9cc3SDavid Wagner * @UBI_VOLUME_REMOVED: a volume has been removed (an UBI device was detached 20401dc9cc3SDavid Wagner * or a volume was removed) 20501dc9cc3SDavid Wagner * @UBI_VOLUME_RESIZED: a volume has been re-sized 20601dc9cc3SDavid Wagner * @UBI_VOLUME_RENAMED: a volume has been re-named 20701dc9cc3SDavid Wagner * @UBI_VOLUME_UPDATED: data has been written to a volume 2080e0ee1ccSDmitry Pervushin * 2090e0ee1ccSDmitry Pervushin * These constants define which type of event has happened when a volume 2100e0ee1ccSDmitry Pervushin * notification function is invoked. 2110e0ee1ccSDmitry Pervushin */ 2120e0ee1ccSDmitry Pervushin enum { 2130e0ee1ccSDmitry Pervushin UBI_VOLUME_ADDED, 2140e0ee1ccSDmitry Pervushin UBI_VOLUME_REMOVED, 2150e0ee1ccSDmitry Pervushin UBI_VOLUME_RESIZED, 2160e0ee1ccSDmitry Pervushin UBI_VOLUME_RENAMED, 2170e0ee1ccSDmitry Pervushin UBI_VOLUME_UPDATED, 2180e0ee1ccSDmitry Pervushin }; 2190e0ee1ccSDmitry Pervushin 2200e0ee1ccSDmitry Pervushin /* 2210e0ee1ccSDmitry Pervushin * struct ubi_notification - UBI notification description structure. 2220e0ee1ccSDmitry Pervushin * @di: UBI device description object 2230e0ee1ccSDmitry Pervushin * @vi: UBI volume description object 2240e0ee1ccSDmitry Pervushin * 2250e0ee1ccSDmitry Pervushin * UBI notifiers are called with a pointer to an object of this type. The 2260e0ee1ccSDmitry Pervushin * object describes the notification. Namely, it provides a description of the 2270e0ee1ccSDmitry Pervushin * UBI device and UBI volume the notification informs about. 2280e0ee1ccSDmitry Pervushin */ 2290e0ee1ccSDmitry Pervushin struct ubi_notification { 2300e0ee1ccSDmitry Pervushin struct ubi_device_info di; 2310e0ee1ccSDmitry Pervushin struct ubi_volume_info vi; 2320e0ee1ccSDmitry Pervushin }; 2330e0ee1ccSDmitry Pervushin 234801c135cSArtem B. Bityutskiy /* UBI descriptor given to users when they open UBI volumes */ 235801c135cSArtem B. Bityutskiy struct ubi_volume_desc; 236801c135cSArtem B. Bityutskiy 237801c135cSArtem B. Bityutskiy int ubi_get_device_info(int ubi_num, struct ubi_device_info *di); 238801c135cSArtem B. Bityutskiy void ubi_get_volume_info(struct ubi_volume_desc *desc, 239801c135cSArtem B. Bityutskiy struct ubi_volume_info *vi); 240801c135cSArtem B. Bityutskiy struct ubi_volume_desc *ubi_open_volume(int ubi_num, int vol_id, int mode); 241801c135cSArtem B. Bityutskiy struct ubi_volume_desc *ubi_open_volume_nm(int ubi_num, const char *name, 242801c135cSArtem B. Bityutskiy int mode); 243b5710284SCorentin Chary struct ubi_volume_desc *ubi_open_volume_path(const char *pathname, int mode); 244b5710284SCorentin Chary 2450e0ee1ccSDmitry Pervushin int ubi_register_volume_notifier(struct notifier_block *nb, 2460e0ee1ccSDmitry Pervushin int ignore_existing); 2470e0ee1ccSDmitry Pervushin int ubi_unregister_volume_notifier(struct notifier_block *nb); 2480e0ee1ccSDmitry Pervushin 249801c135cSArtem B. Bityutskiy void ubi_close_volume(struct ubi_volume_desc *desc); 250801c135cSArtem B. Bityutskiy int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset, 251801c135cSArtem B. Bityutskiy int len, int check); 2529ff08979SRichard Weinberger int ubi_leb_read_sg(struct ubi_volume_desc *desc, int lnum, struct ubi_sgl *sgl, 2539ff08979SRichard Weinberger int offset, int len, int check); 254801c135cSArtem B. Bityutskiy int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf, 255b36a261eSRichard Weinberger int offset, int len); 256801c135cSArtem B. Bityutskiy int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf, 257b36a261eSRichard Weinberger int len); 258801c135cSArtem B. Bityutskiy int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum); 259801c135cSArtem B. Bityutskiy int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum); 260b36a261eSRichard Weinberger int ubi_leb_map(struct ubi_volume_desc *desc, int lnum); 261801c135cSArtem B. Bityutskiy int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum); 262a5bf6190SArtem Bityutskiy int ubi_sync(int ubi_num); 26362f38455SJoel Reardon int ubi_flush(int ubi_num, int vol_id, int lnum); 264801c135cSArtem B. Bityutskiy 265801c135cSArtem B. Bityutskiy /* 266801c135cSArtem B. Bityutskiy * This function is the same as the 'ubi_leb_read()' function, but it does not 267801c135cSArtem B. Bityutskiy * provide the checking capability. 268801c135cSArtem B. Bityutskiy */ 269801c135cSArtem B. Bityutskiy static inline int ubi_read(struct ubi_volume_desc *desc, int lnum, char *buf, 270801c135cSArtem B. Bityutskiy int offset, int len) 271801c135cSArtem B. Bityutskiy { 272801c135cSArtem B. Bityutskiy return ubi_leb_read(desc, lnum, buf, offset, len, 0); 273801c135cSArtem B. Bityutskiy } 2749ff08979SRichard Weinberger 2759ff08979SRichard Weinberger /* 2769ff08979SRichard Weinberger * This function is the same as the 'ubi_leb_read_sg()' function, but it does 2779ff08979SRichard Weinberger * not provide the checking capability. 2789ff08979SRichard Weinberger */ 2799ff08979SRichard Weinberger static inline int ubi_read_sg(struct ubi_volume_desc *desc, int lnum, 2809ff08979SRichard Weinberger struct ubi_sgl *sgl, int offset, int len) 2819ff08979SRichard Weinberger { 2829ff08979SRichard Weinberger return ubi_leb_read_sg(desc, lnum, sgl, offset, len, 0); 2839ff08979SRichard Weinberger } 284801c135cSArtem B. Bityutskiy #endif /* !__LINUX_UBI_H__ */ 285