ebh.h revision 1.2 1 1.2 ttoth /* $NetBSD: ebh.h,v 1.2 2012/04/13 14:50:35 ttoth Exp $ */
2 1.1 ahoka
3 1.1 ahoka /*-
4 1.1 ahoka * Copyright (c) 2010 Department of Software Engineering,
5 1.1 ahoka * University of Szeged, Hungary
6 1.1 ahoka * Copyright (c) 2010 David Tengeri <dtengeri (at) inf.u-szeged.hu>
7 1.1 ahoka * Copyright (c) 2010 Adam Hoka <ahoka (at) NetBSD.org>
8 1.1 ahoka * All rights reserved.
9 1.1 ahoka *
10 1.1 ahoka * This code is derived from software contributed to The NetBSD Foundation
11 1.1 ahoka * by the Department of Software Engineering, University of Szeged, Hungary
12 1.1 ahoka *
13 1.1 ahoka * Redistribution and use in source and binary forms, with or without
14 1.1 ahoka * modification, are permitted provided that the following conditions
15 1.1 ahoka * are met:
16 1.1 ahoka * 1. Redistributions of source code must retain the above copyright
17 1.1 ahoka * notice, this list of conditions and the following disclaimer.
18 1.1 ahoka * 2. Redistributions in binary form must reproduce the above copyright
19 1.1 ahoka * notice, this list of conditions and the following disclaimer in the
20 1.1 ahoka * documentation and/or other materials provided with the distribution.
21 1.1 ahoka *
22 1.1 ahoka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 1.1 ahoka * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 1.1 ahoka * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 1.1 ahoka * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 1.1 ahoka * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 1.1 ahoka * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 1.1 ahoka * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 1.1 ahoka * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 1.1 ahoka * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 ahoka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 ahoka * SUCH DAMAGE.
33 1.1 ahoka */
34 1.1 ahoka
35 1.1 ahoka /*
36 1.1 ahoka * ebh.h
37 1.1 ahoka *
38 1.1 ahoka * Created on: 2009.11.03.
39 1.1 ahoka * Author: dtengeri
40 1.1 ahoka */
41 1.1 ahoka
42 1.1 ahoka #ifndef EBH_H_
43 1.1 ahoka #define EBH_H_
44 1.1 ahoka
45 1.2 ttoth #ifdef _KERNEL
46 1.1 ahoka #include <sys/param.h>
47 1.1 ahoka #include <sys/kernel.h>
48 1.1 ahoka #include <sys/cdefs.h>
49 1.1 ahoka #include <sys/stdint.h>
50 1.1 ahoka #include <sys/types.h>
51 1.1 ahoka #include <sys/tree.h>
52 1.1 ahoka #include <sys/queue.h>
53 1.1 ahoka #include <sys/kmem.h>
54 1.1 ahoka #include <sys/endian.h>
55 1.1 ahoka #include <sys/rwlock.h>
56 1.1 ahoka #include <sys/condvar.h>
57 1.1 ahoka #include <sys/mutex.h>
58 1.1 ahoka #include <sys/kthread.h>
59 1.1 ahoka
60 1.1 ahoka #include <dev/flash/flash.h>
61 1.2 ttoth #include "debug.h"
62 1.2 ttoth #include "ebh_misc.h"
63 1.2 ttoth #endif /* _KERNEL */
64 1.1 ahoka
65 1.2 ttoth #include "ebh_media.h"
66 1.2 ttoth
67 1.2 ttoth /**
68 1.2 ttoth * struct chfs_eb_hdr - in-memory representation of eraseblock headers
69 1.2 ttoth * @ec_hdr: erase counter header ob eraseblock
70 1.2 ttoth * @u.nor_hdr: eraseblock header on NOR flash
71 1.2 ttoth * @u.nand_hdr: eraseblock header on NAND flash
72 1.2 ttoth */
73 1.2 ttoth struct chfs_eb_hdr {
74 1.2 ttoth struct chfs_eb_ec_hdr ec_hdr;
75 1.2 ttoth union {
76 1.2 ttoth struct chfs_nor_eb_hdr nor_hdr;
77 1.2 ttoth struct chfs_nand_eb_hdr nand_hdr;
78 1.2 ttoth } u;
79 1.2 ttoth };
80 1.2 ttoth
81 1.2 ttoth #ifdef _KERNEL
82 1.1 ahoka /* Maximum retries when getting new PEB before exit with failure */
83 1.1 ahoka #define CHFS_MAX_GET_PEB_RETRIES 2
84 1.1 ahoka
85 1.1 ahoka /**
86 1.1 ahoka * LEB status
87 1.1 ahoka *
88 1.1 ahoka */
89 1.1 ahoka enum {
90 1.1 ahoka EBH_LEB_UNMAPPED = -1,
91 1.1 ahoka EBH_LEB_MAPPED,
92 1.1 ahoka EBH_LEB_DIRTY,
93 1.1 ahoka EBH_LEB_INVALID,
94 1.1 ahoka EBH_LEB_ERASE,
95 1.1 ahoka EBH_LEB_ERASED,
96 1.1 ahoka EBH_LEB_FREE,
97 1.1 ahoka };
98 1.1 ahoka
99 1.1 ahoka /**
100 1.1 ahoka * EB header status
101 1.1 ahoka */
102 1.1 ahoka enum {
103 1.1 ahoka EBHDR_LEB_OK = 0,
104 1.1 ahoka EBHDR_LEB_DIRTY,
105 1.1 ahoka EBHDR_LEB_INVALIDATED,
106 1.1 ahoka EBHDR_LEB_BADMAGIC,
107 1.1 ahoka EBHDR_LEB_BADCRC,
108 1.1 ahoka EBHDR_LEB_FREE,
109 1.1 ahoka EBHDR_LEB_NO_HDR,
110 1.1 ahoka };
111 1.1 ahoka
112 1.1 ahoka struct chfs_ebh;
113 1.1 ahoka
114 1.1 ahoka /**
115 1.1 ahoka * struct chfs_ltree_entry - an netry in the lock tree
116 1.1 ahoka * @rb: RB-node of the tree
117 1.1 ahoka * @lnr: logical eraseblock number
118 1.1 ahoka * @users: counts the tasks that are using or want to use the eraseblock
119 1.1 ahoka * @mutex: read/write mutex to lock the eraseblock
120 1.1 ahoka */
121 1.1 ahoka struct chfs_ltree_entry {
122 1.1 ahoka RB_ENTRY(chfs_ltree_entry) rb;
123 1.1 ahoka int lnr;
124 1.1 ahoka int users;
125 1.1 ahoka krwlock_t mutex;
126 1.1 ahoka };
127 1.1 ahoka
128 1.1 ahoka /* Generate structure for Lock tree's red-black tree */
129 1.1 ahoka RB_HEAD(ltree_rbtree, chfs_ltree_entry);
130 1.1 ahoka
131 1.1 ahoka
132 1.1 ahoka /**
133 1.1 ahoka * struct chfs_scan_leb - scanning infomration about a physical eraseblock
134 1.1 ahoka * @erase_cnt: erase counter
135 1.1 ahoka * @pebnr: physical eraseblock number
136 1.1 ahoka * @info: the status of the PEB's eraseblock header when NOR serial when NAND
137 1.1 ahoka * @u.list: link in one of the eraseblock list
138 1.1 ahoka * @u.rb: link in the used RB-tree of chfs_scan_info
139 1.1 ahoka */
140 1.1 ahoka struct chfs_scan_leb {
141 1.1 ahoka int erase_cnt;
142 1.1 ahoka int pebnr;
143 1.1 ahoka int lnr;
144 1.1 ahoka uint64_t info;
145 1.1 ahoka union {
146 1.1 ahoka TAILQ_ENTRY(chfs_scan_leb) queue;
147 1.1 ahoka RB_ENTRY(chfs_scan_leb) rb;
148 1.1 ahoka } u;
149 1.1 ahoka };
150 1.1 ahoka
151 1.1 ahoka TAILQ_HEAD(scan_leb_queue, chfs_scan_leb);
152 1.1 ahoka RB_HEAD(scan_leb_used_rbtree, chfs_scan_leb);
153 1.1 ahoka
154 1.1 ahoka
155 1.1 ahoka /**
156 1.1 ahoka * struct chfs_scan_info - chfs scanning information
157 1.1 ahoka * @corrupted: queue of corrupted physical eraseblocks
158 1.1 ahoka * @free: queue of free physical eraseblocks
159 1.1 ahoka * @erase: queue of the physical eraseblocks signed to erase
160 1.1 ahoka * @erased: queue of physical eraseblocks that contain no header
161 1.1 ahoka * @used: RB-tree of used PEBs describing by chfs_scan_leb
162 1.1 ahoka * @sum_of_ec: summary of erase counters
163 1.1 ahoka * @num_of_eb: number of free and used eraseblocks
164 1.1 ahoka * @bad_peb_cnt: counter of bad eraseblocks
165 1.1 ahoka *
166 1.1 ahoka * This structure contains information about the scanning for further
167 1.1 ahoka * processing.
168 1.1 ahoka */
169 1.1 ahoka struct chfs_scan_info {
170 1.1 ahoka struct scan_leb_queue corrupted;
171 1.1 ahoka struct scan_leb_queue free;
172 1.1 ahoka struct scan_leb_queue erase;
173 1.1 ahoka struct scan_leb_queue erased;
174 1.1 ahoka struct scan_leb_used_rbtree used;
175 1.1 ahoka uint64_t sum_of_ec;
176 1.1 ahoka int num_of_eb;
177 1.1 ahoka int bad_peb_cnt;
178 1.1 ahoka };
179 1.1 ahoka
180 1.1 ahoka /**
181 1.1 ahoka * struct chfs_peb - PEB information for erasing and wear leveling
182 1.1 ahoka * @erase_cnt: erase counter of the physical eraseblock
183 1.1 ahoka * @pebnr: physical eraseblock number
184 1.1 ahoka * @u.queue: link to the queue of the PEBs waiting for erase
185 1.1 ahoka * @u.rb: link to the RB-tree to the free PEBs
186 1.1 ahoka */
187 1.1 ahoka struct chfs_peb {
188 1.1 ahoka int erase_cnt;
189 1.1 ahoka int pebnr;
190 1.1 ahoka union {
191 1.1 ahoka TAILQ_ENTRY(chfs_peb) queue;
192 1.1 ahoka RB_ENTRY(chfs_peb) rb;
193 1.1 ahoka } u;
194 1.1 ahoka };
195 1.1 ahoka
196 1.1 ahoka /* Generate queue and rb-tree structures. */
197 1.1 ahoka TAILQ_HEAD(peb_queue, chfs_peb);
198 1.1 ahoka RB_HEAD(peb_free_rbtree, chfs_peb);
199 1.1 ahoka RB_HEAD(peb_in_use_rbtree, chfs_peb);
200 1.1 ahoka
201 1.1 ahoka
202 1.1 ahoka /*
203 1.1 ahoka * struct chfs_ebh_ops - collection of operations which
204 1.1 ahoka * depends on flash type
205 1.1 ahoka * *************************************************************************** *
206 1.1 ahoka * Direct flash operations:
207 1.1 ahoka *
208 1.1 ahoka * @read_eb_hdr: read eraseblock header from media
209 1.1 ahoka * @write_eb_hdr: write eraseblock header to media
210 1.1 ahoka * @check_eb_hdr: validates eraseblock header
211 1.1 ahoka * @mark_eb_hdr_dirty_flash: marks eraseblock dirty on flash
212 1.1 ahoka * @invalidate_eb_hdr: invalidates eraseblock header
213 1.1 ahoka * @mark_eb_hdr_free: marks eraseblock header free (after erase)
214 1.1 ahoka * *************************************************************************** *
215 1.1 ahoka * Scanning operations:
216 1.1 ahoka *
217 1.1 ahoka * @process_eb: process an eraseblock information at scan
218 1.1 ahoka * *************************************************************************** *
219 1.1 ahoka * Misc operations:
220 1.1 ahoka *
221 1.1 ahoka * @create_eb_hdr: creates an eraseblock header based on flash type
222 1.1 ahoka * @calc_data_offs: calculates where the data starts
223 1.1 ahoka */
224 1.1 ahoka struct chfs_ebh_ops {
225 1.1 ahoka int (*read_eb_hdr)(struct chfs_ebh *ebh, int pebnr,
226 1.1 ahoka struct chfs_eb_hdr *ebhdr);
227 1.1 ahoka int (*write_eb_hdr)(struct chfs_ebh *ebh, int pebnr,
228 1.1 ahoka struct chfs_eb_hdr *ebhdr);
229 1.1 ahoka int (*check_eb_hdr)(struct chfs_ebh *ebh, void *buf);
230 1.1 ahoka int (*mark_eb_hdr_dirty_flash)(struct chfs_ebh *ebh, int pebnr, int lid);
231 1.1 ahoka int (*invalidate_eb_hdr)(struct chfs_ebh *ebh, int pebnr);
232 1.1 ahoka int (*mark_eb_hdr_free)(struct chfs_ebh *ebh, int pebnr, int ec);
233 1.1 ahoka
234 1.1 ahoka int (*process_eb)(struct chfs_ebh *ebh, struct chfs_scan_info *si,
235 1.1 ahoka int pebnr, struct chfs_eb_hdr *ebhdr);
236 1.1 ahoka
237 1.1 ahoka int (*create_eb_hdr)(struct chfs_eb_hdr *ebhdr, int lnr);
238 1.1 ahoka int (*calc_data_offs)(struct chfs_ebh *ebh, int pebnr, int offset);
239 1.1 ahoka };
240 1.1 ahoka
241 1.1 ahoka /**
242 1.1 ahoka * struct erase_thread - background thread for erasing
243 1.1 ahoka * @thread: pointer to thread structure
244 1.1 ahoka * @wakeup: conditional variable for sleeping if there isn't any job to do
245 1.1 ahoka * @running: flag to signal a thread shutdown
246 1.1 ahoka */
247 1.1 ahoka struct erase_thread {
248 1.1 ahoka lwp_t *eth_thread;
249 1.1 ahoka kcondvar_t eth_wakeup;
250 1.1 ahoka bool eth_running;
251 1.1 ahoka };
252 1.1 ahoka
253 1.1 ahoka
254 1.1 ahoka /**
255 1.1 ahoka * struct chfs_ebh - eraseblock handler descriptor
256 1.1 ahoka * @mtd: mtd device descriptor
257 1.1 ahoka * @eb_size: eraseblock size
258 1.1 ahoka * @peb_nr: number of PEBs
259 1.1 ahoka * @lmap: LEB to PEB mapping
260 1.1 ahoka * @layout_map: the LEBs layout (NOT USED YET)
261 1.1 ahoka * @ltree: the lock tree
262 1.1 ahoka * @ltree_lock: protects the tree
263 1.1 ahoka * @alc_mutex: serializes "atomic LEB change" operation
264 1.1 ahoka * @free: RB-tree of the free easeblocks
265 1.1 ahoka * @in_use: RB-tree of PEBs are in use
266 1.1 ahoka * @to_erase: list of the PEBs waiting for erase
267 1.1 ahoka * @fully_erased: list of PEBs that have been erased but don't have header
268 1.1 ahoka * @erase_lock: list and tree lock for fully_erased and to_erase lists and
269 1.1 ahoka * for the free RB-tree
270 1.1 ahoka * @bg_erase: background thread for eraseing PEBs.
271 1.1 ahoka * @ops: collection of operations which depends on flash type
272 1.1 ahoka * @max_serial: max serial number of eraseblocks, only used on NAND
273 1.1 ahoka */
274 1.1 ahoka struct chfs_ebh {
275 1.1 ahoka struct peb_free_rbtree free;
276 1.1 ahoka struct peb_in_use_rbtree in_use;
277 1.1 ahoka struct peb_queue to_erase;
278 1.1 ahoka struct peb_queue fully_erased;
279 1.1 ahoka struct erase_thread bg_erase;
280 1.1 ahoka device_t flash_dev;
281 1.1 ahoka const struct flash_interface *flash_if;
282 1.1 ahoka struct chfs_ebh_ops *ops;
283 1.1 ahoka uint64_t *max_serial;
284 1.1 ahoka int *lmap;
285 1.1 ahoka //int *layout_map;
286 1.1 ahoka struct ltree_rbtree ltree;
287 1.1 ahoka //struct mutex alc_mutex;
288 1.1 ahoka kmutex_t ltree_lock;
289 1.1 ahoka kmutex_t alc_mutex;
290 1.1 ahoka kmutex_t erase_lock;
291 1.1 ahoka size_t eb_size;
292 1.1 ahoka size_t peb_nr;
293 1.1 ahoka flash_size_t flash_size;
294 1.1 ahoka };
295 1.1 ahoka
296 1.1 ahoka /**
297 1.1 ahoka * struct chfs_erase_info_priv - private information for erase
298 1.1 ahoka * @ebh: eraseblock handler
299 1.1 ahoka * @peb: physical eraseblock information
300 1.1 ahoka */
301 1.1 ahoka struct chfs_erase_info_priv {
302 1.1 ahoka struct chfs_ebh *ebh;
303 1.1 ahoka struct chfs_peb *peb;
304 1.1 ahoka };
305 1.1 ahoka
306 1.1 ahoka /* ebh.c */
307 1.1 ahoka
308 1.1 ahoka int ebh_open(struct chfs_ebh *ebh, dev_t dev);
309 1.1 ahoka int ebh_close(struct chfs_ebh *ebh);
310 1.1 ahoka int ebh_read_leb(struct chfs_ebh *ebh, int lnr, char *buf,
311 1.1 ahoka uint32_t offset, size_t len, size_t *retlen);
312 1.1 ahoka int ebh_write_leb(struct chfs_ebh *ebh, int lnr, char *buf,
313 1.1 ahoka uint32_t offset, size_t len, size_t *retlen);
314 1.1 ahoka int ebh_erase_leb(struct chfs_ebh *ebh, int lnr);
315 1.1 ahoka int ebh_map_leb(struct chfs_ebh *ebh, int lnr);
316 1.1 ahoka int ebh_unmap_leb(struct chfs_ebh *ebh, int lnr);
317 1.1 ahoka int ebh_is_mapped(struct chfs_ebh *ebh, int lnr);
318 1.1 ahoka int ebh_change_leb(struct chfs_ebh *ebh, int lnr, char *buf,
319 1.1 ahoka size_t len, size_t *retlen);
320 1.1 ahoka
321 1.2 ttoth #endif /* _KERNEL */
322 1.1 ahoka
323 1.1 ahoka #endif /* EBH_H_ */
324