ebh.h revision 1.1
1/* $NetBSD: ebh.h,v 1.1 2011/11/24 15:51:32 ahoka Exp $ */ 2 3/*- 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> 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#include <sys/param.h> 46#include <sys/kernel.h> 47#include <sys/cdefs.h> 48#include <sys/stdint.h> 49#include <sys/types.h> 50#include <sys/tree.h> 51#include <sys/queue.h> 52#include <sys/kmem.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> 58 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> 63 64/* Maximum retries when getting new PEB before exit with failure */ 65#define CHFS_MAX_GET_PEB_RETRIES 2 66 67/** 68 * LEB status 69 * 70 */ 71enum { 72 EBH_LEB_UNMAPPED = -1, 73 EBH_LEB_MAPPED, 74 EBH_LEB_DIRTY, 75 EBH_LEB_INVALID, 76 EBH_LEB_ERASE, 77 EBH_LEB_ERASED, 78 EBH_LEB_FREE, 79}; 80 81/** 82 * EB header status 83 */ 84enum { 85 EBHDR_LEB_OK = 0, 86 EBHDR_LEB_DIRTY, 87 EBHDR_LEB_INVALIDATED, 88 EBHDR_LEB_BADMAGIC, 89 EBHDR_LEB_BADCRC, 90 EBHDR_LEB_FREE, 91 EBHDR_LEB_NO_HDR, 92}; 93 94struct chfs_ebh; 95 96/** 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 102 */ 103struct chfs_ltree_entry { 104 RB_ENTRY(chfs_ltree_entry) rb; 105 int lnr; 106 int users; 107 krwlock_t mutex; 108}; 109 110/* Generate structure for Lock tree's red-black tree */ 111RB_HEAD(ltree_rbtree, chfs_ltree_entry); 112 113 114/** 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 121 */ 122struct chfs_scan_leb { 123 int erase_cnt; 124 int pebnr; 125 int lnr; 126 uint64_t info; 127 union { 128 TAILQ_ENTRY(chfs_scan_leb) queue; 129 RB_ENTRY(chfs_scan_leb) rb; 130 } u; 131}; 132 133TAILQ_HEAD(scan_leb_queue, chfs_scan_leb); 134RB_HEAD(scan_leb_used_rbtree, chfs_scan_leb); 135 136 137 138/** 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 148 * 149 * This structure contains information about the scanning for further 150 * processing. 151 */ 152struct 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; 158 uint64_t sum_of_ec; 159 int num_of_eb; 160 int bad_peb_cnt; 161}; 162 163/** 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 169 */ 170struct chfs_peb { 171 int erase_cnt; 172 int pebnr; 173 union { 174 TAILQ_ENTRY(chfs_peb) queue; 175 RB_ENTRY(chfs_peb) rb; 176 } u; 177}; 178 179/* Generate queue and rb-tree structures. */ 180TAILQ_HEAD(peb_queue, chfs_peb); 181RB_HEAD(peb_free_rbtree, chfs_peb); 182RB_HEAD(peb_in_use_rbtree, chfs_peb); 183 184/** 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 189 */ 190struct chfs_eb_hdr { 191 struct chfs_eb_ec_hdr ec_hdr; 192 union { 193 struct chfs_nor_eb_hdr nor_hdr; 194 struct chfs_nand_eb_hdr nand_hdr; 195 } u; 196}; 197 198/* 199 * struct chfs_ebh_ops - collection of operations which 200 * depends on flash type 201 * *************************************************************************** * 202 * Direct flash operations: 203 * 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: 212 * 213 * @process_eb: process an eraseblock information at scan 214 * *************************************************************************** * 215 * Misc operations: 216 * 217 * @create_eb_hdr: creates an eraseblock header based on flash type 218 * @calc_data_offs: calculates where the data starts 219 */ 220struct 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); 229 230 int (*process_eb)(struct chfs_ebh *ebh, struct chfs_scan_info *si, 231 int pebnr, struct chfs_eb_hdr *ebhdr); 232 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); 235}; 236 237/** 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 242 */ 243struct erase_thread { 244 lwp_t *eth_thread; 245 kcondvar_t eth_wakeup; 246 bool eth_running; 247}; 248 249 250/** 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 269 */ 270struct chfs_ebh { 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; 276 device_t flash_dev; 277 const struct flash_interface *flash_if; 278 struct chfs_ebh_ops *ops; 279 uint64_t *max_serial; 280 int *lmap; 281 //int *layout_map; 282 struct ltree_rbtree ltree; 283 //struct mutex alc_mutex; 284 kmutex_t ltree_lock; 285 kmutex_t alc_mutex; 286 kmutex_t erase_lock; 287 size_t eb_size; 288 size_t peb_nr; 289 flash_size_t flash_size; 290}; 291 292/** 293 * struct chfs_erase_info_priv - private information for erase 294 * @ebh: eraseblock handler 295 * @peb: physical eraseblock information 296 */ 297struct chfs_erase_info_priv { 298 struct chfs_ebh *ebh; 299 struct chfs_peb *peb; 300}; 301 302/* ebh.c */ 303 304int ebh_open(struct chfs_ebh *ebh, dev_t dev); 305int ebh_close(struct chfs_ebh *ebh); 306int ebh_read_leb(struct chfs_ebh *ebh, int lnr, char *buf, 307 uint32_t offset, size_t len, size_t *retlen); 308int ebh_write_leb(struct chfs_ebh *ebh, int lnr, char *buf, 309 uint32_t offset, size_t len, size_t *retlen); 310int ebh_erase_leb(struct chfs_ebh *ebh, int lnr); 311int ebh_map_leb(struct chfs_ebh *ebh, int lnr); 312int ebh_unmap_leb(struct chfs_ebh *ebh, int lnr); 313int ebh_is_mapped(struct chfs_ebh *ebh, int lnr); 314int ebh_change_leb(struct chfs_ebh *ebh, int lnr, char *buf, 315 size_t len, size_t *retlen); 316 317 318#endif /* EBH_H_ */ 319