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