1 /* $NetBSD: ebh.h,v 1.1 2011/11/24 15:51:32 ahoka Exp $ */
4 * Copyright (c) 2010 Department of Software Engineering,
5 * University of Szeged, Hungary
6 * Copyright (c) 2010 David Tengeri <dtengeri@inf.u-szeged.hu>
7 * Copyright (c) 2010 Adam Hoka <ahoka@NetBSD.org>
10 * This code is derived from software contributed to The NetBSD Foundation
11 * by the Department of Software Engineering, University of Szeged, Hungary
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
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.
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
38 * Created on: 2009.11.03.
45 #include <sys/param.h>
46 #include <sys/kernel.h>
47 #include <sys/cdefs.h>
48 #include <sys/stdint.h>
49 #include <sys/types.h>
51 #include <sys/queue.h>
53 #include <sys/endian.h>
54 #include <sys/rwlock.h>
55 #include <sys/condvar.h>
56 #include <sys/mutex.h>
57 #include <sys/kthread.h>
59 #include <dev/flash/flash.h>
60 #include <ufs/chfs/ebh_media.h>
61 #include <ufs/chfs/debug.h>
62 #include <ufs/chfs/ebh_misc.h>
64 /* Maximum retries when getting new PEB before exit with failure */
65 #define CHFS_MAX_GET_PEB_RETRIES 2
72 EBH_LEB_UNMAPPED
= -1,
87 EBHDR_LEB_INVALIDATED
,
97 * struct chfs_ltree_entry - an netry in the lock tree
98 * @rb: RB-node of the tree
99 * @lnr: logical eraseblock number
100 * @users: counts the tasks that are using or want to use the eraseblock
101 * @mutex: read/write mutex to lock the eraseblock
103 struct chfs_ltree_entry
{
104 RB_ENTRY(chfs_ltree_entry
) rb
;
110 /* Generate structure for Lock tree's red-black tree */
111 RB_HEAD(ltree_rbtree
, chfs_ltree_entry
);
115 * struct chfs_scan_leb - scanning infomration about a physical eraseblock
116 * @erase_cnt: erase counter
117 * @pebnr: physical eraseblock number
118 * @info: the status of the PEB's eraseblock header when NOR serial when NAND
119 * @u.list: link in one of the eraseblock list
120 * @u.rb: link in the used RB-tree of chfs_scan_info
122 struct chfs_scan_leb
{
128 TAILQ_ENTRY(chfs_scan_leb
) queue
;
129 RB_ENTRY(chfs_scan_leb
) rb
;
133 TAILQ_HEAD(scan_leb_queue
, chfs_scan_leb
);
134 RB_HEAD(scan_leb_used_rbtree
, chfs_scan_leb
);
139 * struct chfs_scan_info - chfs scanning information
140 * @corrupted: queue of corrupted physical eraseblocks
141 * @free: queue of free physical eraseblocks
142 * @erase: queue of the physical eraseblocks signed to erase
143 * @erased: queue of physical eraseblocks that contain no header
144 * @used: RB-tree of used PEBs describing by chfs_scan_leb
145 * @sum_of_ec: summary of erase counters
146 * @num_of_eb: number of free and used eraseblocks
147 * @bad_peb_cnt: counter of bad eraseblocks
149 * This structure contains information about the scanning for further
152 struct chfs_scan_info
{
153 struct scan_leb_queue corrupted
;
154 struct scan_leb_queue free
;
155 struct scan_leb_queue erase
;
156 struct scan_leb_queue erased
;
157 struct scan_leb_used_rbtree used
;
164 * struct chfs_peb - PEB information for erasing and wear leveling
165 * @erase_cnt: erase counter of the physical eraseblock
166 * @pebnr: physical eraseblock number
167 * @u.queue: link to the queue of the PEBs waiting for erase
168 * @u.rb: link to the RB-tree to the free PEBs
174 TAILQ_ENTRY(chfs_peb
) queue
;
175 RB_ENTRY(chfs_peb
) rb
;
179 /* Generate queue and rb-tree structures. */
180 TAILQ_HEAD(peb_queue
, chfs_peb
);
181 RB_HEAD(peb_free_rbtree
, chfs_peb
);
182 RB_HEAD(peb_in_use_rbtree
, chfs_peb
);
185 * struct chfs_eb_hdr - in-memory representation of eraseblock headers
186 * @ec_hdr: erase counter header ob eraseblock
187 * @u.nor_hdr: eraseblock header on NOR flash
188 * @u.nand_hdr: eraseblock header on NAND flash
191 struct chfs_eb_ec_hdr ec_hdr
;
193 struct chfs_nor_eb_hdr nor_hdr
;
194 struct chfs_nand_eb_hdr nand_hdr
;
199 * struct chfs_ebh_ops - collection of operations which
200 * depends on flash type
201 * *************************************************************************** *
202 * Direct flash operations:
204 * @read_eb_hdr: read eraseblock header from media
205 * @write_eb_hdr: write eraseblock header to media
206 * @check_eb_hdr: validates eraseblock header
207 * @mark_eb_hdr_dirty_flash: marks eraseblock dirty on flash
208 * @invalidate_eb_hdr: invalidates eraseblock header
209 * @mark_eb_hdr_free: marks eraseblock header free (after erase)
210 * *************************************************************************** *
211 * Scanning operations:
213 * @process_eb: process an eraseblock information at scan
214 * *************************************************************************** *
217 * @create_eb_hdr: creates an eraseblock header based on flash type
218 * @calc_data_offs: calculates where the data starts
220 struct chfs_ebh_ops
{
221 int (*read_eb_hdr
)(struct chfs_ebh
*ebh
, int pebnr
,
222 struct chfs_eb_hdr
*ebhdr
);
223 int (*write_eb_hdr
)(struct chfs_ebh
*ebh
, int pebnr
,
224 struct chfs_eb_hdr
*ebhdr
);
225 int (*check_eb_hdr
)(struct chfs_ebh
*ebh
, void *buf
);
226 int (*mark_eb_hdr_dirty_flash
)(struct chfs_ebh
*ebh
, int pebnr
, int lid
);
227 int (*invalidate_eb_hdr
)(struct chfs_ebh
*ebh
, int pebnr
);
228 int (*mark_eb_hdr_free
)(struct chfs_ebh
*ebh
, int pebnr
, int ec
);
230 int (*process_eb
)(struct chfs_ebh
*ebh
, struct chfs_scan_info
*si
,
231 int pebnr
, struct chfs_eb_hdr
*ebhdr
);
233 int (*create_eb_hdr
)(struct chfs_eb_hdr
*ebhdr
, int lnr
);
234 int (*calc_data_offs
)(struct chfs_ebh
*ebh
, int pebnr
, int offset
);
238 * struct erase_thread - background thread for erasing
239 * @thread: pointer to thread structure
240 * @wakeup: conditional variable for sleeping if there isn't any job to do
241 * @running: flag to signal a thread shutdown
243 struct erase_thread
{
245 kcondvar_t eth_wakeup
;
251 * struct chfs_ebh - eraseblock handler descriptor
252 * @mtd: mtd device descriptor
253 * @eb_size: eraseblock size
254 * @peb_nr: number of PEBs
255 * @lmap: LEB to PEB mapping
256 * @layout_map: the LEBs layout (NOT USED YET)
257 * @ltree: the lock tree
258 * @ltree_lock: protects the tree
259 * @alc_mutex: serializes "atomic LEB change" operation
260 * @free: RB-tree of the free easeblocks
261 * @in_use: RB-tree of PEBs are in use
262 * @to_erase: list of the PEBs waiting for erase
263 * @fully_erased: list of PEBs that have been erased but don't have header
264 * @erase_lock: list and tree lock for fully_erased and to_erase lists and
265 * for the free RB-tree
266 * @bg_erase: background thread for eraseing PEBs.
267 * @ops: collection of operations which depends on flash type
268 * @max_serial: max serial number of eraseblocks, only used on NAND
271 struct peb_free_rbtree free
;
272 struct peb_in_use_rbtree in_use
;
273 struct peb_queue to_erase
;
274 struct peb_queue fully_erased
;
275 struct erase_thread bg_erase
;
277 const struct flash_interface
*flash_if
;
278 struct chfs_ebh_ops
*ops
;
279 uint64_t *max_serial
;
282 struct ltree_rbtree ltree
;
283 //struct mutex alc_mutex;
289 flash_size_t flash_size
;
293 * struct chfs_erase_info_priv - private information for erase
294 * @ebh: eraseblock handler
295 * @peb: physical eraseblock information
297 struct chfs_erase_info_priv
{
298 struct chfs_ebh
*ebh
;
299 struct chfs_peb
*peb
;
304 int ebh_open(struct chfs_ebh
*ebh
, dev_t dev
);
305 int ebh_close(struct chfs_ebh
*ebh
);
306 int ebh_read_leb(struct chfs_ebh
*ebh
, int lnr
, char *buf
,
307 uint32_t offset
, size_t len
, size_t *retlen
);
308 int ebh_write_leb(struct chfs_ebh
*ebh
, int lnr
, char *buf
,
309 uint32_t offset
, size_t len
, size_t *retlen
);
310 int ebh_erase_leb(struct chfs_ebh
*ebh
, int lnr
);
311 int ebh_map_leb(struct chfs_ebh
*ebh
, int lnr
);
312 int ebh_unmap_leb(struct chfs_ebh
*ebh
, int lnr
);
313 int ebh_is_mapped(struct chfs_ebh
*ebh
, int lnr
);
314 int ebh_change_leb(struct chfs_ebh
*ebh
, int lnr
, char *buf
,
315 size_t len
, size_t *retlen
);