blob: 036779b34787ea4bc3b6371dd0b6fda837dd6e09 [file] [log] [blame]
Kyungmin Park9e8248b2008-11-19 16:36:36 +01001/*
2 * Copyright (c) International Business Machines Corp., 2006
3 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02004 * SPDX-License-Identifier: GPL-2.0+
Kyungmin Park9e8248b2008-11-19 16:36:36 +01005 *
6 * Author: Artem Bityutskiy (Битюцкий Артём)
7 */
8
9#ifndef __LINUX_UBI_H__
10#define __LINUX_UBI_H__
11
Kyungmin Park9e8248b2008-11-19 16:36:36 +010012#include <linux/types.h>
Heiko Schocherf5895d12014-06-24 10:10:04 +020013#ifndef __UBOOT__
14#include <linux/ioctl.h>
Heiko Schocher94b66de2015-10-22 06:19:21 +020015#include <linux/scatterlist.h>
Kyungmin Park9e8248b2008-11-19 16:36:36 +010016#include <mtd/ubi-user.h>
Heiko Schocherf5895d12014-06-24 10:10:04 +020017#endif
18
19/* All voumes/LEBs */
20#define UBI_ALL -1
Kyungmin Park9e8248b2008-11-19 16:36:36 +010021
22/*
Heiko Schocher94b66de2015-10-22 06:19:21 +020023 * Maximum number of scatter gather list entries,
24 * we use only 64 to have a lower memory foot print.
25 */
26#define UBI_MAX_SG_COUNT 64
27
28/*
Kyungmin Park9e8248b2008-11-19 16:36:36 +010029 * enum ubi_open_mode - UBI volume open mode constants.
30 *
31 * UBI_READONLY: read-only mode
32 * UBI_READWRITE: read-write mode
33 * UBI_EXCLUSIVE: exclusive mode
Heiko Schocher94b66de2015-10-22 06:19:21 +020034 * UBI_METAONLY: modify only the volume meta-data,
35 * i.e. the data stored in the volume table, but not in any of volume LEBs.
Kyungmin Park9e8248b2008-11-19 16:36:36 +010036 */
37enum {
38 UBI_READONLY = 1,
39 UBI_READWRITE,
Heiko Schocher94b66de2015-10-22 06:19:21 +020040 UBI_EXCLUSIVE,
41 UBI_METAONLY
Kyungmin Park9e8248b2008-11-19 16:36:36 +010042};
43
44/**
45 * struct ubi_volume_info - UBI volume description data structure.
46 * @vol_id: volume ID
47 * @ubi_num: UBI device number this volume belongs to
48 * @size: how many physical eraseblocks are reserved for this volume
49 * @used_bytes: how many bytes of data this volume contains
50 * @used_ebs: how many physical eraseblocks of this volume actually contain any
Heiko Schocherf5895d12014-06-24 10:10:04 +020051 * data
Kyungmin Park9e8248b2008-11-19 16:36:36 +010052 * @vol_type: volume type (%UBI_DYNAMIC_VOLUME or %UBI_STATIC_VOLUME)
53 * @corrupted: non-zero if the volume is corrupted (static volumes only)
54 * @upd_marker: non-zero if the volume has update marker set
55 * @alignment: volume alignment
56 * @usable_leb_size: how many bytes are available in logical eraseblocks of
Heiko Schocherf5895d12014-06-24 10:10:04 +020057 * this volume
Kyungmin Park9e8248b2008-11-19 16:36:36 +010058 * @name_len: volume name length
59 * @name: volume name
60 * @cdev: UBI volume character device major and minor numbers
61 *
62 * The @corrupted flag is only relevant to static volumes and is always zero
63 * for dynamic ones. This is because UBI does not care about dynamic volume
64 * data protection and only cares about protecting static volume data.
65 *
66 * The @upd_marker flag is set if the volume update operation was interrupted.
67 * Before touching the volume data during the update operation, UBI first sets
68 * the update marker flag for this volume. If the volume update operation was
69 * further interrupted, the update marker indicates this. If the update marker
70 * is set, the contents of the volume is certainly damaged and a new volume
71 * update operation has to be started.
72 *
73 * To put it differently, @corrupted and @upd_marker fields have different
74 * semantics:
75 * o the @corrupted flag means that this static volume is corrupted for some
76 * reasons, but not because an interrupted volume update
77 * o the @upd_marker field means that the volume is damaged because of an
78 * interrupted update operation.
79 *
80 * I.e., the @corrupted flag is never set if the @upd_marker flag is set.
81 *
82 * The @used_bytes and @used_ebs fields are only really needed for static
83 * volumes and contain the number of bytes stored in this static volume and how
84 * many eraseblock this data occupies. In case of dynamic volumes, the
85 * @used_bytes field is equivalent to @size*@usable_leb_size, and the @used_ebs
86 * field is equivalent to @size.
87 *
88 * In general, logical eraseblock size is a property of the UBI device, not
89 * of the UBI volume. Indeed, the logical eraseblock size depends on the
90 * physical eraseblock size and on how much bytes UBI headers consume. But
91 * because of the volume alignment (@alignment), the usable size of logical
92 * eraseblocks if a volume may be less. The following equation is true:
Heiko Schocherf5895d12014-06-24 10:10:04 +020093 * @usable_leb_size = LEB size - (LEB size mod @alignment),
Kyungmin Park9e8248b2008-11-19 16:36:36 +010094 * where LEB size is the logical eraseblock size defined by the UBI device.
95 *
96 * The alignment is multiple to the minimal flash input/output unit size or %1
97 * if all the available space is used.
98 *
99 * To put this differently, alignment may be considered is a way to change
100 * volume logical eraseblock sizes.
101 */
102struct ubi_volume_info {
103 int ubi_num;
104 int vol_id;
105 int size;
106 long long used_bytes;
107 int used_ebs;
108 int vol_type;
109 int corrupted;
110 int upd_marker;
111 int alignment;
112 int usable_leb_size;
113 int name_len;
114 const char *name;
115 dev_t cdev;
116};
117
118/**
Heiko Schocher94b66de2015-10-22 06:19:21 +0200119 * struct ubi_sgl - UBI scatter gather list data structure.
120 * @list_pos: current position in @sg[]
121 * @page_pos: current position in @sg[@list_pos]
122 * @sg: the scatter gather list itself
123 *
124 * ubi_sgl is a wrapper around a scatter list which keeps track of the
125 * current position in the list and the current list item such that
126 * it can be used across multiple ubi_leb_read_sg() calls.
127 */
128struct ubi_sgl {
129 int list_pos;
130 int page_pos;
131#ifndef __UBOOT__
132 struct scatterlist sg[UBI_MAX_SG_COUNT];
133#endif
134};
135
136/**
137 * ubi_sgl_init - initialize an UBI scatter gather list data structure.
138 * @usgl: the UBI scatter gather struct itself
139 *
140 * Please note that you still have to use sg_init_table() or any adequate
141 * function to initialize the unterlaying struct scatterlist.
142 */
143static inline void ubi_sgl_init(struct ubi_sgl *usgl)
144{
145 usgl->list_pos = 0;
146 usgl->page_pos = 0;
147}
148
149/**
Kyungmin Park9e8248b2008-11-19 16:36:36 +0100150 * struct ubi_device_info - UBI device description data structure.
151 * @ubi_num: ubi device number
152 * @leb_size: logical eraseblock size on this UBI device
Heiko Schocherf5895d12014-06-24 10:10:04 +0200153 * @leb_start: starting offset of logical eraseblocks within physical
154 * eraseblocks
Kyungmin Park9e8248b2008-11-19 16:36:36 +0100155 * @min_io_size: minimal I/O unit size
Heiko Schocherf5895d12014-06-24 10:10:04 +0200156 * @max_write_size: maximum amount of bytes the underlying flash can write at a
157 * time (MTD write buffer size)
Kyungmin Park9e8248b2008-11-19 16:36:36 +0100158 * @ro_mode: if this device is in read-only mode
159 * @cdev: UBI character device major and minor numbers
160 *
161 * Note, @leb_size is the logical eraseblock size offered by the UBI device.
162 * Volumes of this UBI device may have smaller logical eraseblock size if their
163 * alignment is not equivalent to %1.
Heiko Schocherf5895d12014-06-24 10:10:04 +0200164 *
165 * The @max_write_size field describes flash write maximum write unit. For
166 * example, NOR flash allows for changing individual bytes, so @min_io_size is
167 * %1. However, it does not mean than NOR flash has to write data byte-by-byte.
168 * Instead, CFI NOR flashes have a write-buffer of, e.g., 64 bytes, and when
169 * writing large chunks of data, they write 64-bytes at a time. Obviously, this
170 * improves write throughput.
171 *
172 * Also, the MTD device may have N interleaved (striped) flash chips
173 * underneath, in which case @min_io_size can be physical min. I/O size of
174 * single flash chip, while @max_write_size can be N * @min_io_size.
175 *
176 * The @max_write_size field is always greater or equivalent to @min_io_size.
177 * E.g., some NOR flashes may have (@min_io_size = 1, @max_write_size = 64). In
178 * contrast, NAND flashes usually have @min_io_size = @max_write_size = NAND
179 * page size.
Kyungmin Park9e8248b2008-11-19 16:36:36 +0100180 */
181struct ubi_device_info {
182 int ubi_num;
183 int leb_size;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200184 int leb_start;
Kyungmin Park9e8248b2008-11-19 16:36:36 +0100185 int min_io_size;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200186 int max_write_size;
Kyungmin Park9e8248b2008-11-19 16:36:36 +0100187 int ro_mode;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200188#ifndef __UBOOT__
Kyungmin Park9e8248b2008-11-19 16:36:36 +0100189 dev_t cdev;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200190#endif
191};
192
193/*
194 * Volume notification types.
195 * @UBI_VOLUME_ADDED: a volume has been added (an UBI device was attached or a
196 * volume was created)
197 * @UBI_VOLUME_REMOVED: a volume has been removed (an UBI device was detached
198 * or a volume was removed)
199 * @UBI_VOLUME_RESIZED: a volume has been re-sized
200 * @UBI_VOLUME_RENAMED: a volume has been re-named
201 * @UBI_VOLUME_UPDATED: data has been written to a volume
202 *
203 * These constants define which type of event has happened when a volume
204 * notification function is invoked.
205 */
206enum {
207 UBI_VOLUME_ADDED,
208 UBI_VOLUME_REMOVED,
209 UBI_VOLUME_RESIZED,
210 UBI_VOLUME_RENAMED,
211 UBI_VOLUME_UPDATED,
212};
213
214/*
215 * struct ubi_notification - UBI notification description structure.
216 * @di: UBI device description object
217 * @vi: UBI volume description object
218 *
219 * UBI notifiers are called with a pointer to an object of this type. The
220 * object describes the notification. Namely, it provides a description of the
221 * UBI device and UBI volume the notification informs about.
222 */
223struct ubi_notification {
224 struct ubi_device_info di;
225 struct ubi_volume_info vi;
Kyungmin Park9e8248b2008-11-19 16:36:36 +0100226};
227
228/* UBI descriptor given to users when they open UBI volumes */
229struct ubi_volume_desc;
230
231int ubi_get_device_info(int ubi_num, struct ubi_device_info *di);
232void ubi_get_volume_info(struct ubi_volume_desc *desc,
233 struct ubi_volume_info *vi);
234struct ubi_volume_desc *ubi_open_volume(int ubi_num, int vol_id, int mode);
235struct ubi_volume_desc *ubi_open_volume_nm(int ubi_num, const char *name,
236 int mode);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200237struct ubi_volume_desc *ubi_open_volume_path(const char *pathname, int mode);
238
239#ifndef __UBOOT__
240typedef int (*notifier_fn_t)(void *nb,
241 unsigned long action, void *data);
242
243struct notifier_block {
244 notifier_fn_t notifier_call;
245 struct notifier_block *next;
246 void *next;
247 int priority;
248};
249
250int ubi_register_volume_notifier(struct notifier_block *nb,
251 int ignore_existing);
252int ubi_unregister_volume_notifier(struct notifier_block *nb);
253#endif
254
Kyungmin Park9e8248b2008-11-19 16:36:36 +0100255void ubi_close_volume(struct ubi_volume_desc *desc);
256int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
257 int len, int check);
Heiko Schocher94b66de2015-10-22 06:19:21 +0200258int ubi_leb_read_sg(struct ubi_volume_desc *desc, int lnum, struct ubi_sgl *sgl,
259 int offset, int len, int check);
Kyungmin Park9e8248b2008-11-19 16:36:36 +0100260int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
Heiko Schocherf5895d12014-06-24 10:10:04 +0200261 int offset, int len);
Kyungmin Park9e8248b2008-11-19 16:36:36 +0100262int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
Heiko Schocherf5895d12014-06-24 10:10:04 +0200263 int len);
Kyungmin Park9e8248b2008-11-19 16:36:36 +0100264int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum);
265int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200266int ubi_leb_map(struct ubi_volume_desc *desc, int lnum);
Kyungmin Park9e8248b2008-11-19 16:36:36 +0100267int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200268int ubi_sync(int ubi_num);
269int ubi_flush(int ubi_num, int vol_id, int lnum);
Kyungmin Park9e8248b2008-11-19 16:36:36 +0100270
271/*
272 * This function is the same as the 'ubi_leb_read()' function, but it does not
273 * provide the checking capability.
274 */
275static inline int ubi_read(struct ubi_volume_desc *desc, int lnum, char *buf,
276 int offset, int len)
277{
278 return ubi_leb_read(desc, lnum, buf, offset, len, 0);
279}
Heiko Schocher94b66de2015-10-22 06:19:21 +0200280
281/*
282 * This function is the same as the 'ubi_leb_read_sg()' function, but it does
283 * not provide the checking capability.
284 */
285static inline int ubi_read_sg(struct ubi_volume_desc *desc, int lnum,
286 struct ubi_sgl *sgl, int offset, int len)
287{
288 return ubi_leb_read_sg(desc, lnum, sgl, offset, len, 0);
289}
Kyungmin Park9e8248b2008-11-19 16:36:36 +0100290#endif /* !__LINUX_UBI_H__ */