Home | History | Annotate | Line # | Download | only in chfs
ebh.c revision 1.2
      1  1.2  ahoka /*	$NetBSD: ebh.c,v 1.2 2011/11/25 11:15:24 ahoka 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) 2009 Ferenc Havasi <havasi (at) inf.u-szeged.hu>
      7  1.1  ahoka  * Copyright (C) 2009 Zoltan Sogor <weth (at) inf.u-szeged.hu>
      8  1.1  ahoka  * Copyright (C) 2009 David Tengeri <dtengeri (at) inf.u-szeged.hu>
      9  1.1  ahoka  * Copyright (C) 2009 Tamas Toth <ttoth (at) inf.u-szeged.hu>
     10  1.1  ahoka  * Copyright (C) 2010 Adam Hoka <ahoka (at) NetBSD.org>
     11  1.1  ahoka  * All rights reserved.
     12  1.1  ahoka  *
     13  1.1  ahoka  * This code is derived from software contributed to The NetBSD Foundation
     14  1.1  ahoka  * by the Department of Software Engineering, University of Szeged, Hungary
     15  1.1  ahoka  *
     16  1.1  ahoka  * Redistribution and use in source and binary forms, with or without
     17  1.1  ahoka  * modification, are permitted provided that the following conditions
     18  1.1  ahoka  * are met:
     19  1.1  ahoka  * 1. Redistributions of source code must retain the above copyright
     20  1.1  ahoka  *    notice, this list of conditions and the following disclaimer.
     21  1.1  ahoka  * 2. Redistributions in binary form must reproduce the above copyright
     22  1.1  ahoka  *    notice, this list of conditions and the following disclaimer in the
     23  1.1  ahoka  *    documentation and/or other materials provided with the distribution.
     24  1.1  ahoka  *
     25  1.1  ahoka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     26  1.1  ahoka  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     27  1.1  ahoka  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     28  1.1  ahoka  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     29  1.1  ahoka  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     30  1.1  ahoka  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     31  1.1  ahoka  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     32  1.1  ahoka  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     33  1.1  ahoka  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34  1.1  ahoka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35  1.1  ahoka  * SUCH DAMAGE.
     36  1.1  ahoka  */
     37  1.1  ahoka 
     38  1.1  ahoka #include "ebh.h"
     39  1.1  ahoka 
     40  1.1  ahoka /*****************************************************************************/
     41  1.1  ahoka /* Flash specific operations						     */
     42  1.1  ahoka /*****************************************************************************/
     43  1.1  ahoka int nor_create_eb_hdr(struct chfs_eb_hdr *ebhdr, int lnr);
     44  1.1  ahoka int nand_create_eb_hdr(struct chfs_eb_hdr *ebhdr, int lnr);
     45  1.1  ahoka int nor_calc_data_offs(struct chfs_ebh *ebh, int pebnr, int offset);
     46  1.1  ahoka int nand_calc_data_offs(struct chfs_ebh *ebh, int pebnr, int offset);
     47  1.1  ahoka int nor_read_eb_hdr(struct chfs_ebh *ebh, int pebnr, struct chfs_eb_hdr *ebhdr);
     48  1.1  ahoka int nand_read_eb_hdr(struct chfs_ebh *ebh, int pebnr, struct chfs_eb_hdr *ebhdr);
     49  1.1  ahoka int nor_write_eb_hdr(struct chfs_ebh *ebh, int pebnr, struct chfs_eb_hdr *ebhdr);
     50  1.1  ahoka int nand_write_eb_hdr(struct chfs_ebh *ebh, int pebnr,struct chfs_eb_hdr *ebhdr);
     51  1.1  ahoka int nor_check_eb_hdr(struct chfs_ebh *ebh, void *buf);
     52  1.1  ahoka int nand_check_eb_hdr(struct chfs_ebh *ebh, void *buf);
     53  1.1  ahoka int nor_mark_eb_hdr_dirty_flash(struct chfs_ebh *ebh, int pebnr, int lid);
     54  1.1  ahoka int nor_invalidate_eb_hdr(struct chfs_ebh *ebh, int pebnr);
     55  1.1  ahoka int mark_eb_hdr_free(struct chfs_ebh *ebh, int pebnr, int ec);
     56  1.1  ahoka 
     57  1.1  ahoka int ltree_entry_cmp(struct chfs_ltree_entry *le1, struct chfs_ltree_entry *le2);
     58  1.1  ahoka int peb_in_use_cmp(struct chfs_peb *peb1, struct chfs_peb *peb2);
     59  1.1  ahoka int peb_free_cmp(struct chfs_peb *peb1, struct chfs_peb *peb2);
     60  1.1  ahoka int add_peb_to_erase_queue(struct chfs_ebh *ebh, int pebnr, int ec,struct peb_queue *queue);
     61  1.1  ahoka struct chfs_peb * find_peb_in_use(struct chfs_ebh *ebh, int pebnr);
     62  1.1  ahoka int add_peb_to_free(struct chfs_ebh *ebh, int pebnr, int ec);
     63  1.1  ahoka int add_peb_to_in_use(struct chfs_ebh *ebh, int pebnr, int ec);
     64  1.1  ahoka void erase_callback(struct flash_erase_instruction *ei);
     65  1.1  ahoka int free_peb(struct chfs_ebh *ebh);
     66  1.1  ahoka int release_peb(struct chfs_ebh *ebh, int pebnr);
     67  1.1  ahoka void erase_thread(void *data);
     68  1.1  ahoka static void erase_thread_start(struct chfs_ebh *ebh);
     69  1.1  ahoka static void erase_thread_stop(struct chfs_ebh *ebh);
     70  1.1  ahoka int scan_leb_used_cmp(struct chfs_scan_leb *sleb1, struct chfs_scan_leb *sleb2);
     71  1.1  ahoka int nor_scan_add_to_used(struct chfs_ebh *ebh, struct chfs_scan_info *si,struct chfs_eb_hdr *ebhdr, int pebnr, int leb_status);
     72  1.1  ahoka int nor_process_eb(struct chfs_ebh *ebh, struct chfs_scan_info *si,
     73  1.1  ahoka     int pebnr, struct chfs_eb_hdr *ebhdr);
     74  1.1  ahoka int nand_scan_add_to_used(struct chfs_ebh *ebh, struct chfs_scan_info *si,struct chfs_eb_hdr *ebhdr, int pebnr);
     75  1.1  ahoka int nand_process_eb(struct chfs_ebh *ebh, struct chfs_scan_info *si,
     76  1.1  ahoka     int pebnr, struct chfs_eb_hdr *ebhdr);
     77  1.1  ahoka struct chfs_scan_info *chfs_scan(struct chfs_ebh *ebh);
     78  1.1  ahoka void scan_info_destroy(struct chfs_scan_info *si);
     79  1.1  ahoka int scan_media(struct chfs_ebh *ebh);
     80  1.1  ahoka int get_peb(struct chfs_ebh *ebh);
     81  1.1  ahoka /**
     82  1.1  ahoka  * nor_create_eb_hdr - creates an eraseblock header for NOR flash
     83  1.1  ahoka  * @ebhdr: ebhdr to set
     84  1.1  ahoka  * @lnr: LEB number
     85  1.1  ahoka  */
     86  1.1  ahoka int
     87  1.1  ahoka nor_create_eb_hdr(struct chfs_eb_hdr *ebhdr, int lnr)
     88  1.1  ahoka {
     89  1.1  ahoka 	ebhdr->u.nor_hdr.lid = htole32(lnr);
     90  1.1  ahoka 	return 0;
     91  1.1  ahoka }
     92  1.1  ahoka 
     93  1.1  ahoka /**
     94  1.1  ahoka  * nand_create_eb_hdr - creates an eraseblock header for NAND flash
     95  1.1  ahoka  * @ebhdr: ebhdr to set
     96  1.1  ahoka  * @lnr: LEB number
     97  1.1  ahoka  */
     98  1.1  ahoka int
     99  1.1  ahoka nand_create_eb_hdr(struct chfs_eb_hdr *ebhdr, int lnr)
    100  1.1  ahoka {
    101  1.1  ahoka 	ebhdr->u.nand_hdr.lid = htole32(lnr);
    102  1.1  ahoka 	return 0;
    103  1.1  ahoka }
    104  1.1  ahoka 
    105  1.1  ahoka /**
    106  1.1  ahoka  * nor_calc_data_offs - calculates data offset on NOR flash
    107  1.1  ahoka  * @ebh: chfs eraseblock handler
    108  1.1  ahoka  * @pebnr: eraseblock number
    109  1.1  ahoka  * @offset: offset within the eraseblock
    110  1.1  ahoka  */
    111  1.1  ahoka int
    112  1.1  ahoka nor_calc_data_offs(struct chfs_ebh *ebh, int pebnr, int offset)
    113  1.1  ahoka {
    114  1.1  ahoka 	return pebnr * ebh->flash_if->erasesize + offset +
    115  1.1  ahoka 	    CHFS_EB_EC_HDR_SIZE + CHFS_EB_HDR_NOR_SIZE;
    116  1.1  ahoka }
    117  1.1  ahoka 
    118  1.1  ahoka /**
    119  1.1  ahoka  * nand_calc_data_offs - calculates data offset on NAND flash
    120  1.1  ahoka  * @ebh: chfs eraseblock handler
    121  1.1  ahoka  * @pebnr: eraseblock number
    122  1.1  ahoka  * @offset: offset within the eraseblock
    123  1.1  ahoka  */
    124  1.1  ahoka int
    125  1.1  ahoka nand_calc_data_offs(struct chfs_ebh *ebh, int pebnr, int offset)
    126  1.1  ahoka {
    127  1.1  ahoka 	return pebnr * ebh->flash_if->erasesize + offset +
    128  1.1  ahoka 	    2 * ebh->flash_if->page_size;
    129  1.1  ahoka }
    130  1.1  ahoka 
    131  1.1  ahoka /**
    132  1.1  ahoka  * nor_read_eb_hdr - read ereaseblock header from NOR flash
    133  1.1  ahoka  *
    134  1.1  ahoka  * @ebh: chfs eraseblock handler
    135  1.1  ahoka  * @pebnr: eraseblock number
    136  1.1  ahoka  * @ebhdr: whereto store the data
    137  1.1  ahoka  *
    138  1.1  ahoka  * Reads the eraseblock header from media.
    139  1.1  ahoka  * Returns zero in case of success, error code in case of fail.
    140  1.1  ahoka  */
    141  1.1  ahoka int
    142  1.1  ahoka nor_read_eb_hdr(struct chfs_ebh *ebh,
    143  1.1  ahoka     int pebnr, struct chfs_eb_hdr *ebhdr)
    144  1.1  ahoka {
    145  1.1  ahoka 	int ret;
    146  1.1  ahoka 	size_t retlen;
    147  1.1  ahoka 	off_t ofs = pebnr * ebh->flash_if->erasesize;
    148  1.1  ahoka 
    149  1.1  ahoka 	KASSERT(pebnr >= 0 && pebnr < ebh->peb_nr);
    150  1.1  ahoka 
    151  1.1  ahoka 	ret = flash_read(ebh->flash_dev,
    152  1.1  ahoka 	    ofs, CHFS_EB_EC_HDR_SIZE,
    153  1.1  ahoka 	    &retlen, (unsigned char *) &ebhdr->ec_hdr);
    154  1.1  ahoka 
    155  1.1  ahoka 	if (ret || retlen != CHFS_EB_EC_HDR_SIZE)
    156  1.1  ahoka 		return ret;
    157  1.1  ahoka 
    158  1.1  ahoka 	ofs += CHFS_EB_EC_HDR_SIZE;
    159  1.1  ahoka 	ret = flash_read(ebh->flash_dev,
    160  1.1  ahoka 	    ofs, CHFS_EB_HDR_NOR_SIZE,
    161  1.1  ahoka 	    &retlen, (unsigned char *) &ebhdr->u.nor_hdr);
    162  1.1  ahoka 
    163  1.1  ahoka 	if (ret || retlen != CHFS_EB_HDR_NOR_SIZE)
    164  1.1  ahoka 		return ret;
    165  1.1  ahoka 
    166  1.1  ahoka 	return 0;
    167  1.1  ahoka }
    168  1.1  ahoka 
    169  1.1  ahoka /**
    170  1.1  ahoka  * nand_read_eb_hdr - read ereaseblock header from NAND flash
    171  1.1  ahoka  *
    172  1.1  ahoka  * @ebh: chfs eraseblock handler
    173  1.1  ahoka  * @pebnr: eraseblock number
    174  1.1  ahoka  * @ebhdr: whereto store the data
    175  1.1  ahoka  *
    176  1.1  ahoka  * Reads the eraseblock header from media. It is on the first two page.
    177  1.1  ahoka  * Returns zero in case of success, error code in case of fail.
    178  1.1  ahoka  */
    179  1.1  ahoka int
    180  1.1  ahoka nand_read_eb_hdr(struct chfs_ebh *ebh, int pebnr,
    181  1.1  ahoka     struct chfs_eb_hdr *ebhdr)
    182  1.1  ahoka {
    183  1.1  ahoka 	int ret;
    184  1.1  ahoka 	size_t retlen;
    185  1.1  ahoka 	off_t ofs;
    186  1.1  ahoka 
    187  1.1  ahoka 	KASSERT(pebnr >= 0 && pebnr < ebh->peb_nr);
    188  1.1  ahoka 
    189  1.1  ahoka 	/* Read erase counter header from the first page. */
    190  1.1  ahoka 	ofs = pebnr * ebh->flash_if->erasesize;
    191  1.1  ahoka 	ret = flash_read(ebh->flash_dev,
    192  1.1  ahoka 	    ofs, CHFS_EB_EC_HDR_SIZE, &retlen,
    193  1.1  ahoka 	    (unsigned char *) &ebhdr->ec_hdr);
    194  1.1  ahoka 	if (ret || retlen != CHFS_EB_EC_HDR_SIZE)
    195  1.1  ahoka 		return ret;
    196  1.1  ahoka 
    197  1.1  ahoka 	/* Read NAND eraseblock header from the second page */
    198  1.1  ahoka 	ofs += ebh->flash_if->page_size;
    199  1.1  ahoka 	ret = flash_read(ebh->flash_dev,
    200  1.1  ahoka 	    ofs, CHFS_EB_HDR_NAND_SIZE, &retlen,
    201  1.1  ahoka 	    (unsigned char *) &ebhdr->u.nand_hdr);
    202  1.1  ahoka 	if (ret || retlen != CHFS_EB_HDR_NAND_SIZE)
    203  1.1  ahoka 		return ret;
    204  1.1  ahoka 
    205  1.1  ahoka 	return 0;
    206  1.1  ahoka }
    207  1.1  ahoka 
    208  1.1  ahoka /**
    209  1.1  ahoka  * nor_write_eb_hdr - write ereaseblock header to NOR flash
    210  1.1  ahoka  *
    211  1.1  ahoka  * @ebh: chfs eraseblock handler
    212  1.1  ahoka  * @pebnr: eraseblock number whereto write
    213  1.1  ahoka  * @ebh: ebh to write
    214  1.1  ahoka  *
    215  1.1  ahoka  * Writes the eraseblock header to media.
    216  1.1  ahoka  * Returns zero in case of success, error code in case of fail.
    217  1.1  ahoka  */
    218  1.1  ahoka int
    219  1.1  ahoka nor_write_eb_hdr(struct chfs_ebh *ebh, int pebnr, struct chfs_eb_hdr *ebhdr)
    220  1.1  ahoka {
    221  1.1  ahoka 	int ret, crc;
    222  1.1  ahoka 	size_t retlen;
    223  1.1  ahoka 
    224  1.1  ahoka 	off_t ofs = pebnr * ebh->flash_if->erasesize + CHFS_EB_EC_HDR_SIZE;
    225  1.1  ahoka 
    226  1.1  ahoka 	ebhdr->u.nor_hdr.lid = ebhdr->u.nor_hdr.lid
    227  1.1  ahoka 	    | htole32(CHFS_LID_NOT_DIRTY_BIT);
    228  1.1  ahoka 
    229  1.1  ahoka 	crc = crc32(0, (uint8_t *)&ebhdr->u.nor_hdr + 4,
    230  1.1  ahoka 	    CHFS_EB_HDR_NOR_SIZE - 4);
    231  1.1  ahoka 	ebhdr->u.nand_hdr.crc = htole32(crc);
    232  1.1  ahoka 
    233  1.1  ahoka 	KASSERT(pebnr >= 0 && pebnr < ebh->peb_nr);
    234  1.1  ahoka 
    235  1.1  ahoka 	ret = flash_write(ebh->flash_dev,
    236  1.1  ahoka 	    ofs, CHFS_EB_HDR_NOR_SIZE, &retlen,
    237  1.1  ahoka 	    (unsigned char *) &ebhdr->u.nor_hdr);
    238  1.1  ahoka 
    239  1.1  ahoka 	if (ret || retlen != CHFS_EB_HDR_NOR_SIZE)
    240  1.1  ahoka 		return ret;
    241  1.1  ahoka 
    242  1.1  ahoka 	return 0;
    243  1.1  ahoka }
    244  1.1  ahoka 
    245  1.1  ahoka /**
    246  1.1  ahoka  * nand_write_eb_hdr - write ereaseblock header to NAND flash
    247  1.1  ahoka  *
    248  1.1  ahoka  * @ebh: chfs eraseblock handler
    249  1.1  ahoka  * @pebnr: eraseblock number whereto write
    250  1.1  ahoka  * @ebh: ebh to write
    251  1.1  ahoka  *
    252  1.1  ahoka  * Writes the eraseblock header to media.
    253  1.1  ahoka  * Returns zero in case of success, error code in case of fail.
    254  1.1  ahoka  */
    255  1.1  ahoka int
    256  1.1  ahoka nand_write_eb_hdr(struct chfs_ebh *ebh, int pebnr,
    257  1.1  ahoka     struct chfs_eb_hdr *ebhdr)
    258  1.1  ahoka {
    259  1.1  ahoka 	int ret, crc;
    260  1.1  ahoka 	size_t retlen;
    261  1.1  ahoka 	flash_off_t ofs;
    262  1.1  ahoka 
    263  1.1  ahoka 	KASSERT(pebnr >= 0 && pebnr < ebh->peb_nr);
    264  1.1  ahoka 
    265  1.1  ahoka 	ofs = pebnr * ebh->flash_if->erasesize +
    266  1.1  ahoka 	    ebh->flash_if->page_size;
    267  1.1  ahoka 
    268  1.1  ahoka 	ebhdr->u.nand_hdr.serial = htole64(++(*ebh->max_serial));
    269  1.1  ahoka 
    270  1.1  ahoka 	crc = crc32(0, (uint8_t *)&ebhdr->u.nand_hdr + 4,
    271  1.1  ahoka 	    CHFS_EB_HDR_NAND_SIZE - 4);
    272  1.1  ahoka 	ebhdr->u.nand_hdr.crc = htole32(crc);
    273  1.1  ahoka 
    274  1.1  ahoka 	ret = flash_write(ebh->flash_dev, ofs,
    275  1.1  ahoka 	    CHFS_EB_HDR_NAND_SIZE, &retlen,
    276  1.1  ahoka 	    (unsigned char *) &ebhdr->u.nand_hdr);
    277  1.1  ahoka 
    278  1.1  ahoka 	if (ret || retlen != CHFS_EB_HDR_NAND_SIZE)
    279  1.1  ahoka 		return ret;
    280  1.1  ahoka 
    281  1.1  ahoka 	return 0;
    282  1.1  ahoka }
    283  1.1  ahoka 
    284  1.1  ahoka /**
    285  1.1  ahoka  * nor_check_eb_hdr - check ereaseblock header read from NOR flash
    286  1.1  ahoka  *
    287  1.1  ahoka  * @ebh: chfs eraseblock handler
    288  1.1  ahoka  * @buf: eraseblock header to check
    289  1.1  ahoka  *
    290  1.1  ahoka  * Returns eraseblock header status.
    291  1.1  ahoka  */
    292  1.1  ahoka int
    293  1.1  ahoka nor_check_eb_hdr(struct chfs_ebh *ebh, void *buf)
    294  1.1  ahoka {
    295  1.1  ahoka 	uint32_t magic, crc, hdr_crc;
    296  1.1  ahoka 	struct chfs_eb_hdr *ebhdr = buf;
    297  1.1  ahoka 	le32 lid_save;
    298  1.1  ahoka 
    299  1.1  ahoka 	//check is there a header
    300  1.1  ahoka 	if (check_pattern((void *) &ebhdr->ec_hdr,
    301  1.1  ahoka 		0xFF, 0, CHFS_EB_EC_HDR_SIZE)) {
    302  1.1  ahoka 		dbg_ebh("no header found\n");
    303  1.1  ahoka 		return EBHDR_LEB_NO_HDR;
    304  1.1  ahoka 	}
    305  1.1  ahoka 
    306  1.1  ahoka 	// check magic
    307  1.1  ahoka 	magic = le32toh(ebhdr->ec_hdr.magic);
    308  1.1  ahoka 	if (magic != CHFS_MAGIC_BITMASK) {
    309  1.1  ahoka 		dbg_ebh("bad magic bitmask(exp: %x found %x)\n",
    310  1.1  ahoka 		    CHFS_MAGIC_BITMASK, magic);
    311  1.1  ahoka 		return EBHDR_LEB_BADMAGIC;
    312  1.1  ahoka 	}
    313  1.1  ahoka 
    314  1.1  ahoka 	// check CRC_EC
    315  1.1  ahoka 	hdr_crc = le32toh(ebhdr->ec_hdr.crc_ec);
    316  1.1  ahoka 	crc = crc32(0, (uint8_t *) &ebhdr->ec_hdr + 8, 4);
    317  1.1  ahoka 	if (hdr_crc != crc) {
    318  1.1  ahoka 		dbg_ebh("bad crc_ec found\n");
    319  1.1  ahoka 		return EBHDR_LEB_BADCRC;
    320  1.1  ahoka 	}
    321  1.1  ahoka 
    322  1.1  ahoka 	/* check if the PEB is free: magic, crc_ec and erase_cnt is good and
    323  1.1  ahoka 	 * everything else is FFF..
    324  1.1  ahoka 	 */
    325  1.1  ahoka 	if (check_pattern((void *) &ebhdr->u.nor_hdr, 0xFF, 0,
    326  1.1  ahoka 		CHFS_EB_HDR_NOR_SIZE)) {
    327  1.1  ahoka 		dbg_ebh("free peb found\n");
    328  1.1  ahoka 		return EBHDR_LEB_FREE;
    329  1.1  ahoka 	}
    330  1.1  ahoka 
    331  1.1  ahoka 	// check invalidated (CRC == LID == 0)
    332  1.1  ahoka 	if (ebhdr->u.nor_hdr.crc == 0 && ebhdr->u.nor_hdr.lid == 0) {
    333  1.1  ahoka 		dbg_ebh("invalidated ebhdr found\n");
    334  1.1  ahoka 		return EBHDR_LEB_INVALIDATED;
    335  1.1  ahoka 	}
    336  1.1  ahoka 
    337  1.1  ahoka 	// check CRC
    338  1.1  ahoka 	hdr_crc = le32toh(ebhdr->u.nor_hdr.crc);
    339  1.1  ahoka 	lid_save = ebhdr->u.nor_hdr.lid;
    340  1.1  ahoka 
    341  1.1  ahoka 	// mark lid as not dirty for crc calc
    342  1.1  ahoka 	ebhdr->u.nor_hdr.lid = ebhdr->u.nor_hdr.lid | htole32(
    343  1.1  ahoka 		CHFS_LID_NOT_DIRTY_BIT);
    344  1.1  ahoka 	crc = crc32(0, (uint8_t *) &ebhdr->u.nor_hdr + 4,
    345  1.1  ahoka 	    CHFS_EB_HDR_NOR_SIZE - 4);
    346  1.1  ahoka 	// restore the original lid value in ebh
    347  1.1  ahoka 	ebhdr->u.nor_hdr.lid = lid_save;
    348  1.1  ahoka 
    349  1.1  ahoka 	if (crc != hdr_crc) {
    350  1.1  ahoka 		dbg_ebh("bad crc found\n");
    351  1.1  ahoka 		return EBHDR_LEB_BADCRC;
    352  1.1  ahoka 	}
    353  1.1  ahoka 
    354  1.1  ahoka 	// check dirty
    355  1.1  ahoka 	if (!(le32toh(lid_save) & CHFS_LID_NOT_DIRTY_BIT)) {
    356  1.1  ahoka 		dbg_ebh("dirty ebhdr found\n");
    357  1.1  ahoka 		return EBHDR_LEB_DIRTY;
    358  1.1  ahoka 	}
    359  1.1  ahoka 
    360  1.1  ahoka 	return EBHDR_LEB_OK;
    361  1.1  ahoka }
    362  1.1  ahoka 
    363  1.1  ahoka /**
    364  1.1  ahoka  * nand_check_eb_hdr - check ereaseblock header read from NAND flash
    365  1.1  ahoka  *
    366  1.1  ahoka  * @ebh: chfs eraseblock handler
    367  1.1  ahoka  * @buf: eraseblock header to check
    368  1.1  ahoka  *
    369  1.1  ahoka  * Returns eraseblock header status.
    370  1.1  ahoka  */
    371  1.1  ahoka int
    372  1.1  ahoka nand_check_eb_hdr(struct chfs_ebh *ebh, void *buf)
    373  1.1  ahoka {
    374  1.1  ahoka 	uint32_t magic, crc, hdr_crc;
    375  1.1  ahoka 	struct chfs_eb_hdr *ebhdr = buf;
    376  1.1  ahoka 
    377  1.1  ahoka 	//check is there a header
    378  1.1  ahoka 	if (check_pattern((void *) &ebhdr->ec_hdr,
    379  1.1  ahoka 		0xFF, 0, CHFS_EB_EC_HDR_SIZE)) {
    380  1.1  ahoka 		dbg_ebh("no header found\n");
    381  1.1  ahoka 		return EBHDR_LEB_NO_HDR;
    382  1.1  ahoka 	}
    383  1.1  ahoka 
    384  1.1  ahoka 	// check magic
    385  1.1  ahoka 	magic = le32toh(ebhdr->ec_hdr.magic);
    386  1.1  ahoka 	if (magic != CHFS_MAGIC_BITMASK) {
    387  1.1  ahoka 		dbg_ebh("bad magic bitmask(exp: %x found %x)\n",
    388  1.1  ahoka 		    CHFS_MAGIC_BITMASK, magic);
    389  1.1  ahoka 		return EBHDR_LEB_BADMAGIC;
    390  1.1  ahoka 	}
    391  1.1  ahoka 
    392  1.1  ahoka 	// check CRC_EC
    393  1.1  ahoka 	hdr_crc = le32toh(ebhdr->ec_hdr.crc_ec);
    394  1.1  ahoka 	crc = crc32(0, (uint8_t *) &ebhdr->ec_hdr + 8, 4);
    395  1.1  ahoka 	if (hdr_crc != crc) {
    396  1.1  ahoka 		dbg_ebh("bad crc_ec found\n");
    397  1.1  ahoka 		return EBHDR_LEB_BADCRC;
    398  1.1  ahoka 	}
    399  1.1  ahoka 
    400  1.1  ahoka 	/* check if the PEB is free: magic, crc_ec and erase_cnt is good and
    401  1.1  ahoka 	 * everything else is FFF..
    402  1.1  ahoka 	 */
    403  1.1  ahoka 	if (check_pattern((void *) &ebhdr->u.nand_hdr, 0xFF, 0,
    404  1.1  ahoka 		CHFS_EB_HDR_NAND_SIZE)) {
    405  1.1  ahoka 		dbg_ebh("free peb found\n");
    406  1.1  ahoka 		return EBHDR_LEB_FREE;
    407  1.1  ahoka 	}
    408  1.1  ahoka 
    409  1.1  ahoka 	// check CRC
    410  1.1  ahoka 	hdr_crc = le32toh(ebhdr->u.nand_hdr.crc);
    411  1.1  ahoka 
    412  1.1  ahoka 	crc = crc32(0, (uint8_t *) &ebhdr->u.nand_hdr + 4,
    413  1.1  ahoka 	    CHFS_EB_HDR_NAND_SIZE - 4);
    414  1.1  ahoka 
    415  1.1  ahoka 	if (crc != hdr_crc) {
    416  1.1  ahoka 		dbg_ebh("bad crc found\n");
    417  1.1  ahoka 		return EBHDR_LEB_BADCRC;
    418  1.1  ahoka 	}
    419  1.1  ahoka 
    420  1.1  ahoka 	return EBHDR_LEB_OK;
    421  1.1  ahoka }
    422  1.1  ahoka 
    423  1.1  ahoka /**
    424  1.1  ahoka  * nor_mark_eb_hdr_dirty_flash- mark ereaseblock header dirty on NOR flash
    425  1.1  ahoka  *
    426  1.1  ahoka  * @ebh: chfs eraseblock handler
    427  1.1  ahoka  * @pebnr: eraseblock number
    428  1.1  ahoka  * @lid: leb id (it's bit number 31 will be set to 0)
    429  1.1  ahoka  *
    430  1.1  ahoka  * It pulls the CHFS_LID_NOT_DIRTY_BIT to zero on flash.
    431  1.1  ahoka  *
    432  1.1  ahoka  * Returns zero in case of success, error code in case of fail.
    433  1.1  ahoka  */
    434  1.1  ahoka int
    435  1.1  ahoka nor_mark_eb_hdr_dirty_flash(struct chfs_ebh *ebh, int pebnr, int lid)
    436  1.1  ahoka {
    437  1.1  ahoka 	int ret;
    438  1.1  ahoka 	size_t retlen;
    439  1.1  ahoka 	off_t ofs;
    440  1.1  ahoka 
    441  1.1  ahoka 	/* mark leb id dirty */
    442  1.1  ahoka 	lid = htole32(lid & CHFS_LID_DIRTY_BIT_MASK);
    443  1.1  ahoka 
    444  1.1  ahoka 	/* calculate position */
    445  1.1  ahoka 	ofs = pebnr * ebh->flash_if->erasesize + CHFS_EB_EC_HDR_SIZE
    446  1.1  ahoka 	    + CHFS_GET_MEMBER_POS(struct chfs_nor_eb_hdr , lid);
    447  1.1  ahoka 
    448  1.1  ahoka 	ret = flash_write(ebh->flash_dev, ofs, sizeof(lid), &retlen,
    449  1.1  ahoka 	    (unsigned char *) &lid);
    450  1.1  ahoka 	if (ret || retlen != sizeof(lid)) {
    451  1.1  ahoka 		chfs_err("can't mark peb dirty");
    452  1.1  ahoka 		return ret;
    453  1.1  ahoka 	}
    454  1.1  ahoka 
    455  1.1  ahoka 	return 0;
    456  1.1  ahoka }
    457  1.1  ahoka 
    458  1.1  ahoka /**
    459  1.1  ahoka  * nor_invalidate_eb_hdr - invalidate ereaseblock header on NOR flash
    460  1.1  ahoka  *
    461  1.1  ahoka  * @ebh: chfs eraseblock handler
    462  1.1  ahoka  * @pebnr: eraseblock number
    463  1.1  ahoka  *
    464  1.1  ahoka  * Sets crc and lip field to zero.
    465  1.1  ahoka  * Returns zero in case of success, error code in case of fail.
    466  1.1  ahoka  */
    467  1.1  ahoka int
    468  1.1  ahoka nor_invalidate_eb_hdr(struct chfs_ebh *ebh, int pebnr)
    469  1.1  ahoka {
    470  1.1  ahoka 	int ret;
    471  1.1  ahoka 	size_t retlen;
    472  1.1  ahoka 	off_t ofs;
    473  1.1  ahoka 	char zero_buf[CHFS_INVALIDATE_SIZE];
    474  1.1  ahoka 
    475  1.1  ahoka 	/* fill with zero */
    476  1.1  ahoka 	memset(zero_buf, 0x0, CHFS_INVALIDATE_SIZE);
    477  1.1  ahoka 
    478  1.1  ahoka 	/* calculate position (!!! lid is directly behind crc !!!) */
    479  1.1  ahoka 	ofs = pebnr * ebh->flash_if->erasesize + CHFS_EB_EC_HDR_SIZE
    480  1.1  ahoka 	    + CHFS_GET_MEMBER_POS(struct chfs_nor_eb_hdr, crc);
    481  1.1  ahoka 
    482  1.1  ahoka 	ret = flash_write(ebh->flash_dev,
    483  1.1  ahoka 	    ofs, CHFS_INVALIDATE_SIZE, &retlen,
    484  1.1  ahoka 	    (unsigned char *) &zero_buf);
    485  1.1  ahoka 	if (ret || retlen != CHFS_INVALIDATE_SIZE) {
    486  1.1  ahoka 		chfs_err("can't invalidate peb");
    487  1.1  ahoka 		return ret;
    488  1.1  ahoka 	}
    489  1.1  ahoka 
    490  1.1  ahoka 	return 0;
    491  1.1  ahoka }
    492  1.1  ahoka 
    493  1.1  ahoka /**
    494  1.1  ahoka  * mark_eb_hdr_free - free ereaseblock header on NOR or NAND flash
    495  1.1  ahoka  *
    496  1.1  ahoka  * @ebh: chfs eraseblock handler
    497  1.1  ahoka  * @pebnr: eraseblock number
    498  1.1  ahoka  * @ec: erase counter of PEB
    499  1.1  ahoka  *
    500  1.1  ahoka  * Write out the magic and erase counter to the physical eraseblock.
    501  1.1  ahoka  * Returns zero in case of success, error code in case of fail.
    502  1.1  ahoka  */
    503  1.1  ahoka int
    504  1.1  ahoka mark_eb_hdr_free(struct chfs_ebh *ebh, int pebnr, int ec)
    505  1.1  ahoka {
    506  1.1  ahoka 	int ret, crc;
    507  1.1  ahoka 	size_t retlen;
    508  1.1  ahoka 	off_t ofs;
    509  1.1  ahoka 	struct chfs_eb_hdr *ebhdr;
    510  1.1  ahoka 	ebhdr = kmem_alloc(sizeof(struct chfs_eb_hdr), KM_SLEEP);
    511  1.1  ahoka 
    512  1.1  ahoka 	ebhdr->ec_hdr.magic = htole32(CHFS_MAGIC_BITMASK);
    513  1.1  ahoka 	ebhdr->ec_hdr.erase_cnt = htole32(ec);
    514  1.1  ahoka 	crc = crc32(0, (uint8_t *) &ebhdr->ec_hdr + 8, 4);
    515  1.1  ahoka 	ebhdr->ec_hdr.crc_ec = htole32(crc);
    516  1.1  ahoka 
    517  1.1  ahoka 	ofs = pebnr * ebh->flash_if->erasesize;
    518  1.1  ahoka 
    519  1.1  ahoka 	KASSERT(sizeof(ebhdr->ec_hdr) == CHFS_EB_EC_HDR_SIZE);
    520  1.1  ahoka 
    521  1.1  ahoka 	ret = flash_write(ebh->flash_dev,
    522  1.1  ahoka 	    ofs, CHFS_EB_EC_HDR_SIZE, &retlen,
    523  1.1  ahoka 	    (unsigned char *) &ebhdr->ec_hdr);
    524  1.1  ahoka 
    525  1.1  ahoka 	if (ret || retlen != CHFS_EB_EC_HDR_SIZE) {
    526  1.1  ahoka 		chfs_err("can't mark peb as free: %d\n", pebnr);
    527  1.1  ahoka 		kmem_free(ebhdr, sizeof(struct chfs_eb_hdr));
    528  1.1  ahoka 		return ret;
    529  1.1  ahoka 	}
    530  1.1  ahoka 
    531  1.1  ahoka 	kmem_free(ebhdr, sizeof(struct chfs_eb_hdr));
    532  1.1  ahoka 	return 0;
    533  1.1  ahoka }
    534  1.1  ahoka 
    535  1.1  ahoka /*****************************************************************************/
    536  1.1  ahoka /* End of Flash specific operations					     */
    537  1.1  ahoka /*****************************************************************************/
    538  1.1  ahoka 
    539  1.1  ahoka /*****************************************************************************/
    540  1.1  ahoka /* Lock Tree								     */
    541  1.1  ahoka /*****************************************************************************/
    542  1.1  ahoka 
    543  1.1  ahoka int
    544  1.1  ahoka ltree_entry_cmp(struct chfs_ltree_entry *le1,
    545  1.1  ahoka     struct chfs_ltree_entry *le2)
    546  1.1  ahoka {
    547  1.1  ahoka 	return (le1->lnr - le2->lnr);
    548  1.1  ahoka }
    549  1.1  ahoka 
    550  1.1  ahoka /* Generate functions for Lock tree's red-black tree */
    551  1.1  ahoka RB_PROTOTYPE( ltree_rbtree, chfs_ltree_entry, rb, ltree_entry_cmp);
    552  1.1  ahoka RB_GENERATE( ltree_rbtree, chfs_ltree_entry, rb, ltree_entry_cmp);
    553  1.1  ahoka 
    554  1.1  ahoka 
    555  1.1  ahoka /**
    556  1.1  ahoka  * ltree_lookup - looks up a logical eraseblock in the lock tree
    557  1.1  ahoka  * @ebh: chfs eraseblock handler
    558  1.1  ahoka  * @lid: identifier of the logical eraseblock
    559  1.1  ahoka  *
    560  1.1  ahoka  * This function returns a pointer to the wanted &struct chfs_ltree_entry
    561  1.1  ahoka  * if the logical eraseblock is in the lock tree, so it is locked, NULL
    562  1.1  ahoka  * otherwise.
    563  1.1  ahoka  * @ebh->ltree_lock has to be locked!
    564  1.1  ahoka  */
    565  1.1  ahoka static struct chfs_ltree_entry *
    566  1.1  ahoka ltree_lookup(struct chfs_ebh *ebh, int lnr)
    567  1.1  ahoka {
    568  1.1  ahoka 	struct chfs_ltree_entry le, *result;
    569  1.1  ahoka 	le.lnr = lnr;
    570  1.1  ahoka 	result = RB_FIND(ltree_rbtree, &ebh->ltree, &le);
    571  1.1  ahoka 	return result;
    572  1.1  ahoka }
    573  1.1  ahoka 
    574  1.1  ahoka /**
    575  1.1  ahoka  * ltree_add_entry - add an entry to the lock tree
    576  1.1  ahoka  * @ebh: chfs eraseblock handler
    577  1.1  ahoka  * @lnr: identifier of the logical eraseblock
    578  1.1  ahoka  *
    579  1.1  ahoka  * This function adds a new logical eraseblock entry identified with @lnr to the
    580  1.1  ahoka  * lock tree. If the entry is already in the tree, it increases the user
    581  1.1  ahoka  * counter.
    582  1.1  ahoka  * Returns NULL if can not allocate memory for lock tree entry, or a pointer
    583  1.1  ahoka  * to the inserted entry otherwise.
    584  1.1  ahoka  */
    585  1.1  ahoka static struct chfs_ltree_entry *
    586  1.1  ahoka ltree_add_entry(struct chfs_ebh *ebh, int lnr)
    587  1.1  ahoka {
    588  1.1  ahoka 	struct chfs_ltree_entry *le, *result;
    589  1.1  ahoka 
    590  1.1  ahoka 	le = kmem_alloc(sizeof(struct chfs_ltree_entry), KM_SLEEP);
    591  1.1  ahoka 
    592  1.1  ahoka 	le->lnr = lnr;
    593  1.1  ahoka 	le->users = 1;
    594  1.1  ahoka 	rw_init(&le->mutex);
    595  1.1  ahoka 
    596  1.1  ahoka 	//dbg_ebh("enter ltree lock\n");
    597  1.1  ahoka 	mutex_enter(&ebh->ltree_lock);
    598  1.1  ahoka 	//dbg_ebh("insert\n");
    599  1.1  ahoka 	result = RB_INSERT(ltree_rbtree, &ebh->ltree, le);
    600  1.1  ahoka 	//dbg_ebh("inserted\n");
    601  1.1  ahoka 	if (result) {
    602  1.1  ahoka 		//The entry is already in the tree
    603  1.1  ahoka 		result->users++;
    604  1.1  ahoka 		kmem_free(le, sizeof(struct chfs_ltree_entry));
    605  1.1  ahoka 	}
    606  1.1  ahoka 	else {
    607  1.1  ahoka 		result = le;
    608  1.1  ahoka 	}
    609  1.1  ahoka 	mutex_exit(&ebh->ltree_lock);
    610  1.1  ahoka 
    611  1.1  ahoka 	return result;
    612  1.1  ahoka }
    613  1.1  ahoka 
    614  1.1  ahoka /**
    615  1.1  ahoka  * leb_read_lock - lock a logical eraseblock for read
    616  1.1  ahoka  * @ebh: chfs eraseblock handler
    617  1.1  ahoka  * @lnr: identifier of the logical eraseblock
    618  1.1  ahoka  *
    619  1.1  ahoka  * Returns zero in case of success, error code in case of fail.
    620  1.1  ahoka  */
    621  1.1  ahoka static int
    622  1.1  ahoka leb_read_lock(struct chfs_ebh *ebh, int lnr)
    623  1.1  ahoka {
    624  1.1  ahoka 	struct chfs_ltree_entry *le;
    625  1.1  ahoka 
    626  1.1  ahoka 	le = ltree_add_entry(ebh, lnr);
    627  1.1  ahoka 	if (!le)
    628  1.1  ahoka 		return ENOMEM;
    629  1.1  ahoka 
    630  1.1  ahoka 	rw_enter(&le->mutex, RW_READER);
    631  1.1  ahoka 	return 0;
    632  1.1  ahoka }
    633  1.1  ahoka 
    634  1.1  ahoka /**
    635  1.1  ahoka  * leb_read_unlock - unlock a logical eraseblock from read
    636  1.1  ahoka  * @ebh: chfs eraseblock handler
    637  1.1  ahoka  * @lnr: identifier of the logical eraseblock
    638  1.1  ahoka  *
    639  1.1  ahoka  * This function unlocks a logical eraseblock from read and delete it from the
    640  1.1  ahoka  * lock tree is there are no more users of it.
    641  1.1  ahoka  */
    642  1.1  ahoka static void
    643  1.1  ahoka leb_read_unlock(struct chfs_ebh *ebh, int lnr)
    644  1.1  ahoka {
    645  1.1  ahoka 	struct chfs_ltree_entry *le;
    646  1.1  ahoka 
    647  1.1  ahoka 	mutex_enter(&ebh->ltree_lock);
    648  1.1  ahoka 	//dbg_ebh("LOCK: ebh->ltree_lock spin locked in leb_read_unlock()\n");
    649  1.1  ahoka 	le = ltree_lookup(ebh, lnr);
    650  1.1  ahoka 	if (!le)
    651  1.1  ahoka 		goto out;
    652  1.1  ahoka 
    653  1.1  ahoka 	le->users -= 1;
    654  1.1  ahoka 	KASSERT(le->users >= 0);
    655  1.1  ahoka 	rw_exit(&le->mutex);
    656  1.1  ahoka 	if (le->users == 0) {
    657  1.1  ahoka 		le = RB_REMOVE(ltree_rbtree, &ebh->ltree, le);
    658  1.1  ahoka 		if (le) {
    659  1.1  ahoka 			KASSERT(!rw_lock_held(&le->mutex));
    660  1.1  ahoka 			rw_destroy(&le->mutex);
    661  1.1  ahoka 
    662  1.1  ahoka 			kmem_free(le, sizeof(struct chfs_ltree_entry));
    663  1.1  ahoka 		}
    664  1.1  ahoka 	}
    665  1.1  ahoka 
    666  1.1  ahoka out:
    667  1.1  ahoka 	mutex_exit(&ebh->ltree_lock);
    668  1.1  ahoka 	//dbg_ebh("UNLOCK: ebh->ltree_lock spin unlocked in leb_read_unlock()\n");
    669  1.1  ahoka }
    670  1.1  ahoka 
    671  1.1  ahoka /**
    672  1.1  ahoka  * leb_write_lock - lock a logical eraseblock for write
    673  1.1  ahoka  * @ebh: chfs eraseblock handler
    674  1.1  ahoka  * @lnr: identifier of the logical eraseblock
    675  1.1  ahoka  *
    676  1.1  ahoka  * Returns zero in case of success, error code in case of fail.
    677  1.1  ahoka  */
    678  1.1  ahoka static int
    679  1.1  ahoka leb_write_lock(struct chfs_ebh *ebh, int lnr)
    680  1.1  ahoka {
    681  1.1  ahoka 	struct chfs_ltree_entry *le;
    682  1.1  ahoka 
    683  1.1  ahoka 	le = ltree_add_entry(ebh, lnr);
    684  1.1  ahoka 	if (!le)
    685  1.1  ahoka 		return ENOMEM;
    686  1.1  ahoka 
    687  1.1  ahoka 	rw_enter(&le->mutex, RW_WRITER);
    688  1.1  ahoka 	return 0;
    689  1.1  ahoka }
    690  1.1  ahoka 
    691  1.1  ahoka /**
    692  1.1  ahoka  * leb_write_unlock - unlock a logical eraseblock from write
    693  1.1  ahoka  * @ebh: chfs eraseblock handler
    694  1.1  ahoka  * @lnr: identifier of the logical eraseblock
    695  1.1  ahoka  *
    696  1.1  ahoka  * This function unlocks a logical eraseblock from write and delete it from the
    697  1.1  ahoka  * lock tree is there are no more users of it.
    698  1.1  ahoka  */
    699  1.1  ahoka static void
    700  1.1  ahoka leb_write_unlock(struct chfs_ebh *ebh, int lnr)
    701  1.1  ahoka {
    702  1.1  ahoka 	struct chfs_ltree_entry *le;
    703  1.1  ahoka 
    704  1.1  ahoka 	mutex_enter(&ebh->ltree_lock);
    705  1.1  ahoka 	//dbg_ebh("LOCK: ebh->ltree_lock spin locked in leb_write_unlock()\n");
    706  1.1  ahoka 	le = ltree_lookup(ebh, lnr);
    707  1.1  ahoka 	if (!le)
    708  1.1  ahoka 		goto out;
    709  1.1  ahoka 
    710  1.1  ahoka 	le->users -= 1;
    711  1.1  ahoka 	KASSERT(le->users >= 0);
    712  1.1  ahoka 	rw_exit(&le->mutex);
    713  1.1  ahoka 	if (le->users == 0) {
    714  1.1  ahoka 		RB_REMOVE(ltree_rbtree, &ebh->ltree, le);
    715  1.1  ahoka 
    716  1.1  ahoka 		KASSERT(!rw_lock_held(&le->mutex));
    717  1.1  ahoka 		rw_destroy(&le->mutex);
    718  1.1  ahoka 
    719  1.1  ahoka 		kmem_free(le, sizeof(struct chfs_ltree_entry));
    720  1.1  ahoka 	}
    721  1.1  ahoka 
    722  1.1  ahoka out:
    723  1.1  ahoka 	mutex_exit(&ebh->ltree_lock);
    724  1.1  ahoka 	//dbg_ebh("UNLOCK: ebh->ltree_lock spin unlocked in leb_write_unlock()\n");
    725  1.1  ahoka }
    726  1.1  ahoka 
    727  1.1  ahoka /*****************************************************************************/
    728  1.1  ahoka /* End of Lock Tree							     */
    729  1.1  ahoka /*****************************************************************************/
    730  1.1  ahoka 
    731  1.1  ahoka /*****************************************************************************/
    732  1.1  ahoka /* Erase related operations						     */
    733  1.1  ahoka /*****************************************************************************/
    734  1.1  ahoka 
    735  1.1  ahoka /**
    736  1.1  ahoka  * If the first argument is smaller than the second, the function
    737  1.1  ahoka  * returns a value smaller than zero. If they are equal, the function re-
    738  1.1  ahoka  * turns zero. Otherwise, it should return a value greater than zero.
    739  1.1  ahoka  */
    740  1.1  ahoka int
    741  1.1  ahoka peb_in_use_cmp(struct chfs_peb *peb1, struct chfs_peb *peb2)
    742  1.1  ahoka {
    743  1.1  ahoka 	return (peb1->pebnr - peb2->pebnr);
    744  1.1  ahoka }
    745  1.1  ahoka 
    746  1.1  ahoka int
    747  1.1  ahoka peb_free_cmp(struct chfs_peb *peb1, struct chfs_peb *peb2)
    748  1.1  ahoka {
    749  1.1  ahoka 	int comp;
    750  1.1  ahoka 
    751  1.1  ahoka 	comp = peb1->erase_cnt - peb2->erase_cnt;
    752  1.1  ahoka 	if (0 == comp)
    753  1.1  ahoka 		comp = peb1->pebnr - peb2->pebnr;
    754  1.1  ahoka 
    755  1.1  ahoka 	return comp;
    756  1.1  ahoka }
    757  1.1  ahoka 
    758  1.1  ahoka /* Generate functions for in use PEB's red-black tree */
    759  1.1  ahoka RB_PROTOTYPE(peb_in_use_rbtree, chfs_peb, u.rb, peb_in_use_cmp);
    760  1.1  ahoka RB_GENERATE(peb_in_use_rbtree, chfs_peb, u.rb, peb_in_use_cmp);
    761  1.1  ahoka RB_PROTOTYPE(peb_free_rbtree, chfs_peb, u.rb, peb_free_cmp);
    762  1.1  ahoka RB_GENERATE(peb_free_rbtree, chfs_peb, u.rb, peb_free_cmp);
    763  1.1  ahoka 
    764  1.1  ahoka /**
    765  1.1  ahoka  * add_peb_to_erase_queue: adds a PEB to to_erase/fully_erased queue
    766  1.1  ahoka  * @ebh - chfs eraseblock handler
    767  1.1  ahoka  * @pebnr - physical eraseblock's number
    768  1.1  ahoka  * @ec - erase counter of PEB
    769  1.1  ahoka  * @queue: the queue to add to
    770  1.1  ahoka  *
    771  1.1  ahoka  * This function adds a PEB to the erase queue specified by @queue.
    772  1.1  ahoka  * The @ebh->erase_lock must be locked before using this.
    773  1.1  ahoka  * Returns zero in case of success, error code in case of fail.
    774  1.1  ahoka  */
    775  1.1  ahoka int
    776  1.1  ahoka add_peb_to_erase_queue(struct chfs_ebh *ebh, int pebnr, int ec,
    777  1.1  ahoka     struct peb_queue *queue)
    778  1.1  ahoka {
    779  1.1  ahoka 	struct chfs_peb *peb;
    780  1.1  ahoka 
    781  1.1  ahoka 	peb = kmem_alloc(sizeof(struct chfs_peb), KM_SLEEP);
    782  1.1  ahoka 
    783  1.1  ahoka 	peb->erase_cnt = ec;
    784  1.1  ahoka 	peb->pebnr = pebnr;
    785  1.1  ahoka 
    786  1.1  ahoka 	TAILQ_INSERT_TAIL(queue, peb, u.queue);
    787  1.1  ahoka 
    788  1.1  ahoka 	return 0;
    789  1.1  ahoka 
    790  1.1  ahoka }
    791  1.1  ahoka //TODO
    792  1.1  ahoka /**
    793  1.1  ahoka  * find_peb_in_use - looks up a PEB in the RB-tree of used blocks
    794  1.1  ahoka  * @ebh - chfs eraseblock handler
    795  1.1  ahoka  *
    796  1.1  ahoka  * This function returns a pointer to the PEB found in the tree,
    797  1.1  ahoka  * NULL otherwise.
    798  1.1  ahoka  * The @ebh->erase_lock must be locked before using this.
    799  1.1  ahoka  */
    800  1.1  ahoka struct chfs_peb *
    801  1.1  ahoka find_peb_in_use(struct chfs_ebh *ebh, int pebnr)
    802  1.1  ahoka {
    803  1.1  ahoka 	struct chfs_peb peb, *result;
    804  1.1  ahoka 	peb.pebnr = pebnr;
    805  1.1  ahoka 	result = RB_FIND(peb_in_use_rbtree, &ebh->in_use, &peb);
    806  1.1  ahoka 	return result;
    807  1.1  ahoka }
    808  1.1  ahoka 
    809  1.1  ahoka /**
    810  1.1  ahoka  * add_peb_to_free - adds a PEB to the RB-tree of free PEBs
    811  1.1  ahoka  * @ebh - chfs eraseblock handler
    812  1.1  ahoka  * @pebnr - physical eraseblock's number
    813  1.1  ahoka  * @ec - erase counter of PEB
    814  1.1  ahoka  *
    815  1.1  ahoka  *
    816  1.1  ahoka  * This function adds a physical eraseblock to the RB-tree of free PEBs
    817  1.1  ahoka  * stored in the @ebh. The key is the erase counter and pebnr.
    818  1.1  ahoka  * The @ebh->erase_lock must be locked before using this.
    819  1.1  ahoka  * Returns zero in case of success, error code in case of fail.
    820  1.1  ahoka  */
    821  1.1  ahoka int
    822  1.1  ahoka add_peb_to_free(struct chfs_ebh *ebh, int pebnr, int ec)
    823  1.1  ahoka {
    824  1.1  ahoka 	struct chfs_peb *peb, *result;
    825  1.1  ahoka 
    826  1.1  ahoka 	peb = kmem_alloc(sizeof(struct chfs_peb), KM_SLEEP);
    827  1.1  ahoka 
    828  1.1  ahoka 	peb->erase_cnt = ec;
    829  1.1  ahoka 	peb->pebnr = pebnr;
    830  1.1  ahoka 	result = RB_INSERT(peb_free_rbtree, &ebh->free, peb);
    831  1.1  ahoka 	if (result)
    832  1.1  ahoka 		return 1;
    833  1.1  ahoka 
    834  1.1  ahoka 	return 0;
    835  1.1  ahoka }
    836  1.1  ahoka 
    837  1.1  ahoka /**
    838  1.1  ahoka  * add_peb_to_in_use - adds a PEB to the RB-tree of used PEBs
    839  1.1  ahoka  * @ebh - chfs eraseblock handler
    840  1.1  ahoka  * @pebnr - physical eraseblock's number
    841  1.1  ahoka  * @ec - erase counter of PEB
    842  1.1  ahoka  *
    843  1.1  ahoka  *
    844  1.1  ahoka  * This function adds a physical eraseblock to the RB-tree of used PEBs
    845  1.1  ahoka  * stored in the @ebh. The key is pebnr.
    846  1.1  ahoka  * The @ebh->erase_lock must be locked before using this.
    847  1.1  ahoka  * Returns zero in case of success, error code in case of fail.
    848  1.1  ahoka  */
    849  1.1  ahoka int
    850  1.1  ahoka add_peb_to_in_use(struct chfs_ebh *ebh, int pebnr, int ec)
    851  1.1  ahoka {
    852  1.1  ahoka 	struct chfs_peb *peb, *result;
    853  1.1  ahoka 
    854  1.1  ahoka 	peb = kmem_alloc(sizeof(struct chfs_peb), KM_SLEEP);
    855  1.1  ahoka 
    856  1.1  ahoka 	peb->erase_cnt = ec;
    857  1.1  ahoka 	peb->pebnr = pebnr;
    858  1.1  ahoka 	result = RB_INSERT(peb_in_use_rbtree, &ebh->in_use, peb);
    859  1.1  ahoka 	if (result)
    860  1.1  ahoka 		return 1;
    861  1.1  ahoka 
    862  1.1  ahoka 	return 0;
    863  1.1  ahoka }
    864  1.1  ahoka 
    865  1.1  ahoka /**
    866  1.1  ahoka  * erase_callback - callback function for flash erase
    867  1.1  ahoka  * @ei: erase information
    868  1.1  ahoka  */
    869  1.1  ahoka void
    870  1.1  ahoka erase_callback(struct flash_erase_instruction *ei)
    871  1.1  ahoka {
    872  1.1  ahoka 	int err;
    873  1.1  ahoka 	struct chfs_erase_info_priv *priv = (void *) ei->ei_priv;
    874  1.1  ahoka 	//dbg_ebh("ERASE_CALLBACK() CALLED\n");
    875  1.1  ahoka 	struct chfs_ebh *ebh = priv->ebh;
    876  1.1  ahoka 	struct chfs_peb *peb = priv->peb;
    877  1.1  ahoka 
    878  1.1  ahoka 	peb->erase_cnt += 1;
    879  1.1  ahoka 
    880  1.1  ahoka 	if (ei->ei_state == FLASH_ERASE_DONE) {
    881  1.1  ahoka 
    882  1.1  ahoka 		/* Write out erase counter */
    883  1.1  ahoka 		err = ebh->ops->mark_eb_hdr_free(ebh,
    884  1.1  ahoka 		    peb->pebnr, peb->erase_cnt);
    885  1.1  ahoka 		if (err) {
    886  1.1  ahoka 			/* cannot mark PEB as free,so erase it again */
    887  1.1  ahoka 			chfs_err(
    888  1.1  ahoka 				"cannot mark eraseblock as free, PEB: %d\n",
    889  1.1  ahoka 				peb->pebnr);
    890  1.1  ahoka 			mutex_enter(&ebh->erase_lock);
    891  1.1  ahoka 			/*dbg_ebh("LOCK: ebh->erase_lock spin locked in erase_callback() "
    892  1.1  ahoka 			  "after mark ebhdr free\n");*/
    893  1.1  ahoka 			add_peb_to_erase_queue(ebh, peb->pebnr, peb->erase_cnt,
    894  1.1  ahoka 			    &ebh->to_erase);
    895  1.1  ahoka 			mutex_exit(&ebh->erase_lock);
    896  1.1  ahoka 			/*dbg_ebh("UNLOCK: ebh->erase_lock spin unlocked in erase_callback() "
    897  1.1  ahoka 			  "after mark ebhdr free\n");*/
    898  1.1  ahoka 			kmem_free(peb, sizeof(struct chfs_peb));
    899  1.1  ahoka 			return;
    900  1.1  ahoka 		}
    901  1.1  ahoka 
    902  1.1  ahoka 		mutex_enter(&ebh->erase_lock);
    903  1.1  ahoka 		/*dbg_ebh("LOCK: ebh->erase_lock spin locked in erase_callback()\n");*/
    904  1.1  ahoka 		err = add_peb_to_free(ebh, peb->pebnr, peb->erase_cnt);
    905  1.1  ahoka 		mutex_exit(&ebh->erase_lock);
    906  1.1  ahoka 		/*dbg_ebh("UNLOCK: ebh->erase_lock spin unlocked in erase_callback()\n");*/
    907  1.1  ahoka 		kmem_free(peb, sizeof(struct chfs_peb));
    908  1.1  ahoka 	} else {
    909  1.1  ahoka 		/*
    910  1.1  ahoka 		 * Erase is finished, but there was a problem,
    911  1.1  ahoka 		 * so erase PEB again
    912  1.1  ahoka 		 */
    913  1.1  ahoka 		chfs_err("erase failed, state is: 0x%x\n", ei->ei_state);
    914  1.1  ahoka 		add_peb_to_erase_queue(ebh, peb->pebnr, peb->erase_cnt, &ebh->to_erase);
    915  1.1  ahoka 		kmem_free(peb, sizeof(struct chfs_peb));
    916  1.1  ahoka 	}
    917  1.1  ahoka }
    918  1.1  ahoka 
    919  1.1  ahoka /**
    920  1.1  ahoka  * free_peb: free a PEB
    921  1.1  ahoka  * @ebh: chfs eraseblock handler
    922  1.1  ahoka  *
    923  1.1  ahoka  * This function erases the first physical eraseblock from one of the erase
    924  1.1  ahoka  * lists and adds to the RB-tree of free PEBs.
    925  1.1  ahoka  * Returns zero in case of succes, error code in case of fail.
    926  1.1  ahoka  */
    927  1.1  ahoka int
    928  1.1  ahoka free_peb(struct chfs_ebh *ebh)
    929  1.1  ahoka {
    930  1.1  ahoka 	int err, retries = 0;
    931  1.1  ahoka 	off_t ofs;
    932  1.1  ahoka 	struct chfs_peb *peb = NULL;
    933  1.1  ahoka 	struct flash_erase_instruction *ei;
    934  1.1  ahoka 
    935  1.1  ahoka 	KASSERT(mutex_owned(&ebh->erase_lock));
    936  1.1  ahoka 
    937  1.1  ahoka 	if (!TAILQ_EMPTY(&ebh->fully_erased)) {
    938  1.1  ahoka 		//dbg_ebh("[FREE PEB] got a fully erased block\n");
    939  1.1  ahoka 		peb = TAILQ_FIRST(&ebh->fully_erased);
    940  1.1  ahoka 		TAILQ_REMOVE(&ebh->fully_erased, peb, u.queue);
    941  1.1  ahoka 		err = ebh->ops->mark_eb_hdr_free(ebh,
    942  1.1  ahoka 		    peb->pebnr, peb->erase_cnt);
    943  1.1  ahoka 		if (err) {
    944  1.1  ahoka 			goto out_free;
    945  1.1  ahoka 		}
    946  1.1  ahoka 		err = add_peb_to_free(ebh, peb->pebnr, peb->erase_cnt);
    947  1.1  ahoka 		goto out_free;
    948  1.1  ahoka 	}
    949  1.1  ahoka 	/* Erase PEB */
    950  1.1  ahoka 	//dbg_ebh("[FREE PEB] eraseing a block\n");
    951  1.1  ahoka 	peb = TAILQ_FIRST(&ebh->to_erase);
    952  1.1  ahoka 	TAILQ_REMOVE(&ebh->to_erase, peb, u.queue);
    953  1.1  ahoka 	mutex_exit(&ebh->erase_lock);
    954  1.1  ahoka 	//dbg_ebh("UNLOCK: ebh->erase_lock spin unlocked in free_peb()\n");
    955  1.1  ahoka 	ofs = peb->pebnr * ebh->flash_if->erasesize;
    956  1.1  ahoka 
    957  1.1  ahoka 	/* XXX where do we free this? */
    958  1.1  ahoka 	ei = kmem_alloc(sizeof(struct flash_erase_instruction)
    959  1.1  ahoka 	    + sizeof(struct chfs_erase_info_priv), KM_SLEEP);
    960  1.1  ahoka retry:
    961  1.1  ahoka 	memset(ei, 0, sizeof(*ei));
    962  1.1  ahoka 
    963  1.1  ahoka //	ei->ei_if = ebh->flash_if;
    964  1.1  ahoka 	ei->ei_addr = ofs;
    965  1.1  ahoka 	ei->ei_len = ebh->flash_if->erasesize;
    966  1.1  ahoka 	ei->ei_callback = erase_callback;
    967  1.1  ahoka 	ei->ei_priv = (unsigned long) (&ei[1]);
    968  1.1  ahoka 
    969  1.1  ahoka 	((struct chfs_erase_info_priv *) ei->ei_priv)->ebh = ebh;
    970  1.1  ahoka 	((struct chfs_erase_info_priv *) ei->ei_priv)->peb = peb;
    971  1.1  ahoka 
    972  1.1  ahoka 	err = flash_erase(ebh->flash_dev, ei);
    973  1.1  ahoka 	dbg_ebh("erased peb: %d\n", peb->pebnr);
    974  1.1  ahoka 
    975  1.1  ahoka 	/* einval would mean we did something wrong */
    976  1.1  ahoka 	KASSERT(err != EINVAL);
    977  1.1  ahoka 
    978  1.1  ahoka 	if (err) {
    979  1.1  ahoka 		dbg_ebh("errno: %d, ei->ei_state: %d\n", err, ei->ei_state);
    980  1.1  ahoka 		if (CHFS_MAX_GET_PEB_RETRIES < ++retries &&
    981  1.1  ahoka 		    ei->ei_state == FLASH_ERASE_FAILED) {
    982  1.1  ahoka 			/* The block went bad mark it */
    983  1.1  ahoka 			dbg_ebh("ebh markbad! 0x%jx\n", (uintmax_t )ofs);
    984  1.1  ahoka 			err = flash_block_markbad(ebh->flash_dev, ofs);
    985  1.1  ahoka 			if (!err) {
    986  1.1  ahoka 				ebh->peb_nr--;
    987  1.1  ahoka 			}
    988  1.1  ahoka 
    989  1.1  ahoka 			goto out;
    990  1.1  ahoka 		}
    991  1.1  ahoka 		chfs_err("can not erase PEB: %d, try again\n", peb->pebnr);
    992  1.1  ahoka 		goto retry;
    993  1.1  ahoka 	}
    994  1.1  ahoka 
    995  1.1  ahoka out:
    996  1.1  ahoka 	/* lock the erase_lock, because it was locked
    997  1.1  ahoka 	 * when the function was called */
    998  1.1  ahoka 	mutex_enter(&ebh->erase_lock);
    999  1.1  ahoka 	return err;
   1000  1.1  ahoka 
   1001  1.1  ahoka out_free:
   1002  1.1  ahoka 	kmem_free(peb, sizeof(struct chfs_peb));
   1003  1.1  ahoka 	return err;
   1004  1.1  ahoka }
   1005  1.1  ahoka 
   1006  1.1  ahoka /**
   1007  1.1  ahoka  * release_peb - schedule an erase for the PEB
   1008  1.1  ahoka  * @ebh: chfs eraseblock handler
   1009  1.1  ahoka  * @pebnr: physical eraseblock number
   1010  1.1  ahoka  *
   1011  1.1  ahoka  * This function get the peb identified by @pebnr from the in_use RB-tree of
   1012  1.1  ahoka  * @ebh, removes it and schedule an erase for it.
   1013  1.1  ahoka  *
   1014  1.1  ahoka  * Returns zero on success, error code in case of fail.
   1015  1.1  ahoka  */
   1016  1.1  ahoka int
   1017  1.1  ahoka release_peb(struct chfs_ebh *ebh, int pebnr)
   1018  1.1  ahoka {
   1019  1.1  ahoka 	int err = 0;
   1020  1.1  ahoka 	struct chfs_peb *peb;
   1021  1.1  ahoka 
   1022  1.1  ahoka 	mutex_enter(&ebh->erase_lock);
   1023  1.1  ahoka 
   1024  1.1  ahoka 	//dbg_ebh("LOCK: ebh->erase_lock spin locked in release_peb()\n");
   1025  1.1  ahoka 	peb = find_peb_in_use(ebh, pebnr);
   1026  1.1  ahoka 	if (!peb) {
   1027  1.1  ahoka 		chfs_err("LEB is mapped, but is not in the 'in_use' "
   1028  1.1  ahoka 		    "tree of ebh\n");
   1029  1.1  ahoka 		goto out_unlock;
   1030  1.1  ahoka 	}
   1031  1.1  ahoka 	err = add_peb_to_erase_queue(ebh, peb->pebnr, peb->erase_cnt,
   1032  1.1  ahoka 	    &ebh->to_erase);
   1033  1.1  ahoka 
   1034  1.1  ahoka 	if (err)
   1035  1.1  ahoka 		goto out_unlock;
   1036  1.1  ahoka 
   1037  1.1  ahoka 	RB_REMOVE(peb_in_use_rbtree, &ebh->in_use, peb);
   1038  1.1  ahoka out_unlock:
   1039  1.1  ahoka 	mutex_exit(&ebh->erase_lock);
   1040  1.1  ahoka 	//dbg_ebh("UNLOCK: ebh->erase_lock spin unlocked in release_peb()"
   1041  1.1  ahoka 	//		" at out_unlock\n");
   1042  1.1  ahoka 	return err;
   1043  1.1  ahoka }
   1044  1.1  ahoka 
   1045  1.1  ahoka /**
   1046  1.1  ahoka  * erase_thread - background thread for erasing PEBs
   1047  1.1  ahoka  * @data: pointer to the eraseblock handler
   1048  1.1  ahoka  */
   1049  1.1  ahoka /*void
   1050  1.1  ahoka   erase_thread(void *data)
   1051  1.1  ahoka   {
   1052  1.1  ahoka   struct chfs_ebh *ebh = data;
   1053  1.1  ahoka 
   1054  1.1  ahoka   dbg_ebh("erase thread started\n");
   1055  1.1  ahoka   while (ebh->bg_erase.eth_running) {
   1056  1.1  ahoka   int err;
   1057  1.1  ahoka 
   1058  1.1  ahoka   mutex_enter(&ebh->erase_lock);
   1059  1.1  ahoka   dbg_ebh("LOCK: ebh->erase_lock spin locked in erase_thread()\n");
   1060  1.1  ahoka   if (TAILQ_EMPTY(&ebh->to_erase) && TAILQ_EMPTY(&ebh->fully_erased)) {
   1061  1.1  ahoka   dbg_ebh("thread has nothing to do\n");
   1062  1.1  ahoka   mutex_exit(&ebh->erase_lock);
   1063  1.1  ahoka   mutex_enter(&ebh->bg_erase.eth_thread_mtx);
   1064  1.1  ahoka   cv_timedwait_sig(&ebh->bg_erase.eth_wakeup,
   1065  1.1  ahoka   &ebh->bg_erase.eth_thread_mtx, mstohz(100));
   1066  1.1  ahoka   mutex_exit(&ebh->bg_erase.eth_thread_mtx);
   1067  1.1  ahoka 
   1068  1.1  ahoka   dbg_ebh("UNLOCK: ebh->erase_lock spin unlocked in erase_thread()\n");
   1069  1.1  ahoka   continue;
   1070  1.1  ahoka   }
   1071  1.1  ahoka   mutex_exit(&ebh->erase_lock);
   1072  1.1  ahoka   dbg_ebh("UNLOCK: ebh->erase_lock spin unlocked in erase_thread()\n");
   1073  1.1  ahoka 
   1074  1.1  ahoka   err = free_peb(ebh);
   1075  1.1  ahoka   if (err)
   1076  1.1  ahoka   chfs_err("freeing PEB failed in the background thread: %d\n", err);
   1077  1.1  ahoka 
   1078  1.1  ahoka   }
   1079  1.1  ahoka   dbg_ebh("erase thread stopped\n");
   1080  1.1  ahoka   kthread_exit(0);
   1081  1.1  ahoka   }*/
   1082  1.1  ahoka 
   1083  1.1  ahoka /**
   1084  1.1  ahoka  * erase_thread - background thread for erasing PEBs
   1085  1.1  ahoka  * @data: pointer to the eraseblock handler
   1086  1.1  ahoka  */
   1087  1.1  ahoka void
   1088  1.1  ahoka erase_thread(void *data) {
   1089  1.1  ahoka 	dbg_ebh("[EBH THREAD] erase thread started\n");
   1090  1.1  ahoka 
   1091  1.1  ahoka 	struct chfs_ebh *ebh = data;
   1092  1.1  ahoka 	int err;
   1093  1.1  ahoka 
   1094  1.1  ahoka 	mutex_enter(&ebh->erase_lock);
   1095  1.1  ahoka 	while (ebh->bg_erase.eth_running) {
   1096  1.1  ahoka 		if (TAILQ_EMPTY(&ebh->to_erase) &&
   1097  1.1  ahoka 		    TAILQ_EMPTY(&ebh->fully_erased)) {
   1098  1.1  ahoka 			cv_timedwait_sig(&ebh->bg_erase.eth_wakeup,
   1099  1.1  ahoka 			    &ebh->erase_lock, mstohz(100));
   1100  1.1  ahoka 		} else {
   1101  1.1  ahoka 			/* XXX exiting this mutex is a bit odd here as
   1102  1.1  ahoka 			 * free_peb instantly reenters it...
   1103  1.1  ahoka 			 */
   1104  1.1  ahoka 			err = free_peb(ebh);
   1105  1.1  ahoka 			mutex_exit(&ebh->erase_lock);
   1106  1.1  ahoka 			if (err) {
   1107  1.1  ahoka 				chfs_err("freeing PEB failed in the"
   1108  1.1  ahoka 				    " background thread: %d\n", err);
   1109  1.1  ahoka 			}
   1110  1.1  ahoka 			mutex_enter(&ebh->erase_lock);
   1111  1.1  ahoka 		}
   1112  1.1  ahoka 	}
   1113  1.1  ahoka 	mutex_exit(&ebh->erase_lock);
   1114  1.1  ahoka 
   1115  1.1  ahoka 	dbg_ebh("[EBH THREAD] erase thread stopped\n");
   1116  1.1  ahoka 	kthread_exit(0);
   1117  1.1  ahoka }
   1118  1.1  ahoka 
   1119  1.1  ahoka /**
   1120  1.1  ahoka  * erase_thread_start - init and start erase thread
   1121  1.1  ahoka  * @ebh: eraseblock handler
   1122  1.1  ahoka  */
   1123  1.1  ahoka static void
   1124  1.1  ahoka erase_thread_start(struct chfs_ebh *ebh)
   1125  1.1  ahoka {
   1126  1.1  ahoka 	cv_init(&ebh->bg_erase.eth_wakeup, "ebheracv");
   1127  1.1  ahoka 
   1128  1.1  ahoka 	ebh->bg_erase.eth_running = true;
   1129  1.1  ahoka 	kthread_create(PRI_NONE, KTHREAD_MPSAFE | KTHREAD_MUSTJOIN, NULL,
   1130  1.1  ahoka 	    erase_thread, ebh, &ebh->bg_erase.eth_thread, "ebherase");
   1131  1.1  ahoka }
   1132  1.1  ahoka 
   1133  1.1  ahoka /**
   1134  1.1  ahoka  * erase_thread_stop - stop background erase thread
   1135  1.1  ahoka  * @ebh: eraseblock handler
   1136  1.1  ahoka  */
   1137  1.1  ahoka static void
   1138  1.1  ahoka erase_thread_stop(struct chfs_ebh *ebh)
   1139  1.1  ahoka {
   1140  1.1  ahoka 	ebh->bg_erase.eth_running = false;
   1141  1.1  ahoka 	cv_signal(&ebh->bg_erase.eth_wakeup);
   1142  1.1  ahoka 	dbg_ebh("[EBH THREAD STOP] signaled\n");
   1143  1.1  ahoka 
   1144  1.1  ahoka 	kthread_join(ebh->bg_erase.eth_thread);
   1145  1.1  ahoka #ifdef BROKEN_KTH_JOIN
   1146  1.1  ahoka 	kpause("chfsebhjointh", false, mstohz(1000), NULL);
   1147  1.1  ahoka #endif
   1148  1.1  ahoka 
   1149  1.1  ahoka 	cv_destroy(&ebh->bg_erase.eth_wakeup);
   1150  1.1  ahoka }
   1151  1.1  ahoka 
   1152  1.1  ahoka /*****************************************************************************/
   1153  1.1  ahoka /* End of Erase related operations					     */
   1154  1.1  ahoka /*****************************************************************************/
   1155  1.1  ahoka 
   1156  1.1  ahoka /*****************************************************************************/
   1157  1.1  ahoka /* Scan related operations						     */
   1158  1.1  ahoka /*****************************************************************************/
   1159  1.1  ahoka int
   1160  1.1  ahoka scan_leb_used_cmp(struct chfs_scan_leb *sleb1, struct chfs_scan_leb *sleb2)
   1161  1.1  ahoka {
   1162  1.1  ahoka 	return (sleb1->lnr - sleb2->lnr);
   1163  1.1  ahoka }
   1164  1.1  ahoka 
   1165  1.1  ahoka RB_PROTOTYPE(scan_leb_used_rbtree, chfs_scan_leb, u.rb, scan_leb_used_cmp);
   1166  1.1  ahoka RB_GENERATE(scan_leb_used_rbtree, chfs_scan_leb, u.rb, scan_leb_used_cmp);
   1167  1.1  ahoka 
   1168  1.1  ahoka /**
   1169  1.1  ahoka  * scan_add_to_queue - adds a physical eraseblock to one of the
   1170  1.1  ahoka  *                     eraseblock queue
   1171  1.1  ahoka  * @si: chfs scanning information
   1172  1.1  ahoka  * @pebnr: physical eraseblock number
   1173  1.1  ahoka  * @erase_cnt: erase counter of the physical eraseblock
   1174  1.1  ahoka  * @list: the list to add to
   1175  1.1  ahoka  *
   1176  1.1  ahoka  * This function adds a physical eraseblock to one of the lists in the scanning
   1177  1.1  ahoka  * information.
   1178  1.1  ahoka  * Returns zero in case of success, negative error code in case of fail.
   1179  1.1  ahoka  */
   1180  1.1  ahoka static int
   1181  1.1  ahoka scan_add_to_queue(struct chfs_scan_info *si, int pebnr, int erase_cnt,
   1182  1.1  ahoka     struct scan_leb_queue *queue)
   1183  1.1  ahoka {
   1184  1.1  ahoka 	struct chfs_scan_leb *sleb;
   1185  1.1  ahoka 
   1186  1.1  ahoka 	sleb = kmem_alloc(sizeof(struct chfs_scan_leb), KM_SLEEP);
   1187  1.1  ahoka 
   1188  1.1  ahoka 	sleb->pebnr = pebnr;
   1189  1.1  ahoka 	sleb->erase_cnt = erase_cnt;
   1190  1.1  ahoka 	TAILQ_INSERT_TAIL(queue, sleb, u.queue);
   1191  1.1  ahoka 	return 0;
   1192  1.1  ahoka }
   1193  1.1  ahoka 
   1194  1.1  ahoka /*
   1195  1.1  ahoka  * nor_scan_add_to_used - add a physical eraseblock to the
   1196  1.1  ahoka  *                        used tree of scan info
   1197  1.1  ahoka  * @ebh: chfs eraseblock handler
   1198  1.1  ahoka  * @si: chfs scanning information
   1199  1.1  ahoka  * @ebhdr: eraseblock header
   1200  1.1  ahoka  * @pebnr: physical eraseblock number
   1201  1.1  ahoka  * @leb_status: the status of the PEB's eraseblock header
   1202  1.1  ahoka  *
   1203  1.1  ahoka  * This function adds a PEB to the used tree of the scanning information.
   1204  1.1  ahoka  * It handles the situations if there are more physical eraseblock referencing
   1205  1.1  ahoka  * to the same logical eraseblock.
   1206  1.1  ahoka  * Returns zero in case of success, error code in case of fail.
   1207  1.1  ahoka  */
   1208  1.1  ahoka int
   1209  1.1  ahoka nor_scan_add_to_used(struct chfs_ebh *ebh, struct chfs_scan_info *si,
   1210  1.1  ahoka     struct chfs_eb_hdr *ebhdr, int pebnr, int leb_status)
   1211  1.1  ahoka {
   1212  1.1  ahoka 	int err, lnr, ec;
   1213  1.1  ahoka 	struct chfs_scan_leb *sleb, *old;
   1214  1.1  ahoka 
   1215  1.1  ahoka 	lnr = CHFS_GET_LID(ebhdr->u.nor_hdr.lid);
   1216  1.1  ahoka 	ec = le32toh(ebhdr->ec_hdr.erase_cnt);
   1217  1.1  ahoka 
   1218  1.1  ahoka 	sleb = kmem_alloc(sizeof(struct chfs_scan_leb), KM_SLEEP);
   1219  1.1  ahoka 
   1220  1.1  ahoka 	sleb->erase_cnt = ec;
   1221  1.1  ahoka 	sleb->lnr = lnr;
   1222  1.1  ahoka 	sleb->pebnr = pebnr;
   1223  1.1  ahoka 	sleb->info = leb_status;
   1224  1.1  ahoka 
   1225  1.1  ahoka 	old = RB_INSERT(scan_leb_used_rbtree, &si->used, sleb);
   1226  1.1  ahoka 	if (old) {
   1227  1.1  ahoka 		kmem_free(sleb, sizeof(struct chfs_scan_leb));
   1228  1.1  ahoka 		/* There is already an eraseblock in the used tree */
   1229  1.1  ahoka 		/* If the new one is bad */
   1230  1.1  ahoka 		if (EBHDR_LEB_DIRTY == leb_status &&
   1231  1.1  ahoka 		    EBHDR_LEB_OK == old->info) {
   1232  1.1  ahoka 			return scan_add_to_queue(si, pebnr, ec, &si->erase);
   1233  1.1  ahoka 		} else {
   1234  1.1  ahoka 			err = scan_add_to_queue(si, old->pebnr,
   1235  1.1  ahoka 			    old->erase_cnt, &si->erase);
   1236  1.1  ahoka 			if (err) {
   1237  1.1  ahoka 				return err;
   1238  1.1  ahoka 			}
   1239  1.1  ahoka 
   1240  1.1  ahoka 			old->erase_cnt = ec;
   1241  1.1  ahoka 			old->lnr = lnr;
   1242  1.1  ahoka 			old->pebnr = pebnr;
   1243  1.1  ahoka 			old->info = leb_status;
   1244  1.1  ahoka 			return 0;
   1245  1.1  ahoka 		}
   1246  1.1  ahoka 	}
   1247  1.1  ahoka 	return 0;
   1248  1.1  ahoka }
   1249  1.1  ahoka 
   1250  1.1  ahoka /**
   1251  1.1  ahoka  * nor_process eb -read the headers from NOR flash, check them and add to
   1252  1.1  ahoka  * 				   the scanning information
   1253  1.1  ahoka  * @ebh: chfs eraseblock handler
   1254  1.1  ahoka  * @si: chfs scanning information
   1255  1.1  ahoka  * @pebnr: physical eraseblock number
   1256  1.1  ahoka  *
   1257  1.1  ahoka  * Returns zero in case of success, error code in case of fail.
   1258  1.1  ahoka  */
   1259  1.1  ahoka int
   1260  1.1  ahoka nor_process_eb(struct chfs_ebh *ebh, struct chfs_scan_info *si,
   1261  1.1  ahoka     int pebnr, struct chfs_eb_hdr *ebhdr)
   1262  1.1  ahoka {
   1263  1.1  ahoka 	int err, erase_cnt, leb_status;
   1264  1.1  ahoka 
   1265  1.1  ahoka 	err = ebh->ops->read_eb_hdr(ebh, pebnr, ebhdr);
   1266  1.1  ahoka 	if (err)
   1267  1.1  ahoka 		return err;
   1268  1.1  ahoka 
   1269  1.1  ahoka 	erase_cnt = le32toh(ebhdr->ec_hdr.erase_cnt);
   1270  1.1  ahoka 	dbg_ebh("erase_cnt: %d\n", erase_cnt);
   1271  1.1  ahoka 	leb_status = ebh->ops->check_eb_hdr(ebh, ebhdr);
   1272  1.1  ahoka 	if (EBHDR_LEB_BADMAGIC == leb_status ||
   1273  1.1  ahoka 	    EBHDR_LEB_BADCRC == leb_status) {
   1274  1.1  ahoka 		err = scan_add_to_queue(si, pebnr, erase_cnt, &si->corrupted);
   1275  1.1  ahoka 		return err;
   1276  1.1  ahoka 	}
   1277  1.1  ahoka 	else if (EBHDR_LEB_FREE == leb_status) {
   1278  1.1  ahoka 		err = scan_add_to_queue(si, pebnr, erase_cnt, &si->free);
   1279  1.1  ahoka 		goto count_mean;
   1280  1.1  ahoka 	}
   1281  1.1  ahoka 	else if (EBHDR_LEB_NO_HDR == leb_status) {
   1282  1.1  ahoka 		err = scan_add_to_queue(si, pebnr, erase_cnt, &si->erased);
   1283  1.1  ahoka 		return err;
   1284  1.1  ahoka 	}
   1285  1.1  ahoka 	else if (EBHDR_LEB_INVALIDATED == leb_status) {
   1286  1.1  ahoka 		err = scan_add_to_queue(si, pebnr, erase_cnt, &si->erase);
   1287  1.1  ahoka 		return err;
   1288  1.1  ahoka 	}
   1289  1.1  ahoka 
   1290  1.1  ahoka 	err = nor_scan_add_to_used(ebh, si, ebhdr, pebnr, leb_status);
   1291  1.1  ahoka 	if (err)
   1292  1.1  ahoka 		return err;
   1293  1.1  ahoka 
   1294  1.1  ahoka 
   1295  1.1  ahoka count_mean:
   1296  1.1  ahoka 	si->sum_of_ec += erase_cnt;
   1297  1.1  ahoka 	si->num_of_eb++;
   1298  1.1  ahoka 
   1299  1.1  ahoka 	return err;
   1300  1.1  ahoka }
   1301  1.1  ahoka 
   1302  1.1  ahoka /*
   1303  1.1  ahoka  * nand_scan_add_to_used - add a physical eraseblock to the
   1304  1.1  ahoka  *                         used tree of scan info
   1305  1.1  ahoka  * @ebh: chfs eraseblock handler
   1306  1.1  ahoka  * @si: chfs scanning information
   1307  1.1  ahoka  * @ebhdr: eraseblock header
   1308  1.1  ahoka  * @pebnr: physical eraseblock number
   1309  1.1  ahoka  * @leb_status: the status of the PEB's eraseblock header
   1310  1.1  ahoka  *
   1311  1.1  ahoka  * This function adds a PEB to the used tree of the scanning information.
   1312  1.1  ahoka  * It handles the situations if there are more physical eraseblock referencing
   1313  1.1  ahoka  * to the same logical eraseblock.
   1314  1.1  ahoka  * Returns zero in case of success, error code in case of fail.
   1315  1.1  ahoka  */
   1316  1.1  ahoka int
   1317  1.1  ahoka nand_scan_add_to_used(struct chfs_ebh *ebh, struct chfs_scan_info *si,
   1318  1.1  ahoka     struct chfs_eb_hdr *ebhdr, int pebnr)
   1319  1.1  ahoka {
   1320  1.1  ahoka 	int err, lnr, ec;
   1321  1.1  ahoka 	struct chfs_scan_leb *sleb, *old;
   1322  1.1  ahoka 	uint64_t serial = le64toh(ebhdr->u.nand_hdr.serial);
   1323  1.1  ahoka 
   1324  1.1  ahoka 	lnr = CHFS_GET_LID(ebhdr->u.nor_hdr.lid);
   1325  1.1  ahoka 	ec = le32toh(ebhdr->ec_hdr.erase_cnt);
   1326  1.1  ahoka 
   1327  1.1  ahoka 	sleb = kmem_alloc(sizeof(struct chfs_scan_leb), KM_SLEEP);
   1328  1.1  ahoka 
   1329  1.1  ahoka 	sleb->erase_cnt = ec;
   1330  1.1  ahoka 	sleb->lnr = lnr;
   1331  1.1  ahoka 	sleb->pebnr = pebnr;
   1332  1.1  ahoka 	sleb->info = serial;
   1333  1.1  ahoka 
   1334  1.1  ahoka 	old = RB_INSERT(scan_leb_used_rbtree, &si->used, sleb);
   1335  1.1  ahoka 	if (old) {
   1336  1.1  ahoka 		kmem_free(sleb, sizeof(struct chfs_scan_leb));
   1337  1.1  ahoka 		/* There is already an eraseblock in the used tree */
   1338  1.1  ahoka 		/* If the new one is bad */
   1339  1.1  ahoka 		if (serial < old->info)
   1340  1.1  ahoka 			return scan_add_to_queue(si, pebnr, ec, &si->erase);
   1341  1.1  ahoka 		else {
   1342  1.1  ahoka 			err = scan_add_to_queue(si,
   1343  1.1  ahoka 			    old->pebnr, old->erase_cnt, &si->erase);
   1344  1.1  ahoka 			if (err)
   1345  1.1  ahoka 				return err;
   1346  1.1  ahoka 
   1347  1.1  ahoka 			old->erase_cnt = ec;
   1348  1.1  ahoka 			old->lnr = lnr;
   1349  1.1  ahoka 			old->pebnr = pebnr;
   1350  1.1  ahoka 			old->info = serial;
   1351  1.1  ahoka 			return 0;
   1352  1.1  ahoka 		}
   1353  1.1  ahoka 	}
   1354  1.1  ahoka 	return 0;
   1355  1.1  ahoka }
   1356  1.1  ahoka 
   1357  1.1  ahoka /**
   1358  1.1  ahoka  * nand_process eb -read the headers from NAND flash, check them and add to the
   1359  1.1  ahoka  * 					scanning information
   1360  1.1  ahoka  * @ebh: chfs eraseblock handler
   1361  1.1  ahoka  * @si: chfs scanning information
   1362  1.1  ahoka  * @pebnr: physical eraseblock number
   1363  1.1  ahoka  *
   1364  1.1  ahoka  * Returns zero in case of success, error code in case of fail.
   1365  1.1  ahoka  */
   1366  1.1  ahoka int
   1367  1.1  ahoka nand_process_eb(struct chfs_ebh *ebh, struct chfs_scan_info *si,
   1368  1.1  ahoka     int pebnr, struct chfs_eb_hdr *ebhdr)
   1369  1.1  ahoka {
   1370  1.1  ahoka 	int err, erase_cnt, leb_status;
   1371  1.1  ahoka 	uint64_t max_serial;
   1372  1.2  ahoka 	/* isbad() is defined on some ancient platforms, heh */
   1373  1.2  ahoka 	bool is_bad;
   1374  1.1  ahoka 
   1375  1.1  ahoka 	/* Check block is bad */
   1376  1.1  ahoka 	err = flash_block_isbad(ebh->flash_dev,
   1377  1.2  ahoka 	    pebnr * ebh->flash_if->erasesize, &is_bad);
   1378  1.1  ahoka 	if (err) {
   1379  1.1  ahoka 		chfs_err("checking block is bad failed\n");
   1380  1.1  ahoka 		return err;
   1381  1.1  ahoka 	}
   1382  1.2  ahoka 	if (is_bad) {
   1383  1.1  ahoka 		si->bad_peb_cnt++;
   1384  1.1  ahoka 		return 0;
   1385  1.1  ahoka 	}
   1386  1.1  ahoka 
   1387  1.1  ahoka 	err = ebh->ops->read_eb_hdr(ebh, pebnr, ebhdr);
   1388  1.1  ahoka 	if (err)
   1389  1.1  ahoka 		return err;
   1390  1.1  ahoka 
   1391  1.1  ahoka 	erase_cnt = le32toh(ebhdr->ec_hdr.erase_cnt);
   1392  1.1  ahoka 	leb_status = ebh->ops->check_eb_hdr(ebh, ebhdr);
   1393  1.1  ahoka 	if (EBHDR_LEB_BADMAGIC == leb_status ||
   1394  1.1  ahoka 	    EBHDR_LEB_BADCRC == leb_status) {
   1395  1.1  ahoka 		err = scan_add_to_queue(si, pebnr, erase_cnt, &si->corrupted);
   1396  1.1  ahoka 		return err;
   1397  1.1  ahoka 	}
   1398  1.1  ahoka 	else if (EBHDR_LEB_FREE == leb_status) {
   1399  1.1  ahoka 		err = scan_add_to_queue(si, pebnr, erase_cnt, &si->free);
   1400  1.1  ahoka 		goto count_mean;
   1401  1.1  ahoka 	}
   1402  1.1  ahoka 	else if (EBHDR_LEB_NO_HDR == leb_status) {
   1403  1.1  ahoka 		err = scan_add_to_queue(si, pebnr, erase_cnt, &si->erased);
   1404  1.1  ahoka 		return err;
   1405  1.1  ahoka 	}
   1406  1.1  ahoka 
   1407  1.1  ahoka 	err = nand_scan_add_to_used(ebh, si, ebhdr, pebnr);
   1408  1.1  ahoka 	if (err)
   1409  1.1  ahoka 		return err;
   1410  1.1  ahoka 
   1411  1.1  ahoka 	max_serial = le64toh(ebhdr->u.nand_hdr.serial);
   1412  1.1  ahoka 	if (max_serial > *ebh->max_serial) {
   1413  1.1  ahoka 		*ebh->max_serial = max_serial;
   1414  1.1  ahoka 	}
   1415  1.1  ahoka 
   1416  1.1  ahoka count_mean:
   1417  1.1  ahoka 	si->sum_of_ec += erase_cnt;
   1418  1.1  ahoka 	si->num_of_eb++;
   1419  1.1  ahoka 
   1420  1.1  ahoka 	return err;
   1421  1.1  ahoka }
   1422  1.1  ahoka 
   1423  1.1  ahoka /**
   1424  1.1  ahoka  * chfs_scan - scans the media and returns informations about it
   1425  1.1  ahoka  * @ebh: chfs eraseblock handler
   1426  1.1  ahoka  *
   1427  1.1  ahoka  * This function scans through the media and returns information about it or if
   1428  1.1  ahoka  * it fails NULL will be returned.
   1429  1.1  ahoka  */
   1430  1.1  ahoka struct chfs_scan_info *
   1431  1.1  ahoka chfs_scan(struct chfs_ebh *ebh)
   1432  1.1  ahoka {
   1433  1.1  ahoka 	struct chfs_scan_info *si;
   1434  1.1  ahoka 	struct chfs_eb_hdr *ebhdr;
   1435  1.1  ahoka 	int pebnr, err;
   1436  1.1  ahoka 
   1437  1.1  ahoka 	si = kmem_alloc(sizeof(*si), KM_SLEEP);
   1438  1.1  ahoka 
   1439  1.1  ahoka 	TAILQ_INIT(&si->corrupted);
   1440  1.1  ahoka 	TAILQ_INIT(&si->free);
   1441  1.1  ahoka 	TAILQ_INIT(&si->erase);
   1442  1.1  ahoka 	TAILQ_INIT(&si->erased);
   1443  1.1  ahoka 	RB_INIT(&si->used);
   1444  1.1  ahoka 	si->bad_peb_cnt = 0;
   1445  1.1  ahoka 	si->num_of_eb = 0;
   1446  1.1  ahoka 	si->sum_of_ec = 0;
   1447  1.1  ahoka 
   1448  1.1  ahoka 	ebhdr = kmem_alloc(sizeof(*ebhdr), KM_SLEEP);
   1449  1.1  ahoka 
   1450  1.1  ahoka 	for (pebnr = 0; pebnr < ebh->peb_nr; pebnr++) {
   1451  1.1  ahoka 		dbg_ebh("processing PEB %d\n", pebnr);
   1452  1.1  ahoka 		err = ebh->ops->process_eb(ebh, si, pebnr, ebhdr);
   1453  1.1  ahoka 		if (err < 0)
   1454  1.1  ahoka 			goto out_ebhdr;
   1455  1.1  ahoka 	}
   1456  1.1  ahoka 	kmem_free(ebhdr, sizeof(*ebhdr));
   1457  1.1  ahoka 	dbg_ebh("[CHFS_SCAN] scanning information collected\n");
   1458  1.1  ahoka 	return si;
   1459  1.1  ahoka 
   1460  1.1  ahoka out_ebhdr:
   1461  1.1  ahoka 	kmem_free(ebhdr, sizeof(*ebhdr));
   1462  1.1  ahoka 	kmem_free(si, sizeof(*si));
   1463  1.1  ahoka 	return NULL;
   1464  1.1  ahoka }
   1465  1.1  ahoka 
   1466  1.1  ahoka /**
   1467  1.1  ahoka  * scan_info_destroy - frees all lists and trees in the scanning information
   1468  1.1  ahoka  * @si: the scanning information
   1469  1.1  ahoka  */
   1470  1.1  ahoka void
   1471  1.1  ahoka scan_info_destroy(struct chfs_scan_info *si)
   1472  1.1  ahoka {
   1473  1.1  ahoka 	EBH_QUEUE_DESTROY(&si->corrupted,
   1474  1.1  ahoka 	    struct chfs_scan_leb, u.queue);
   1475  1.1  ahoka 
   1476  1.1  ahoka 	EBH_QUEUE_DESTROY(&si->erase,
   1477  1.1  ahoka 	    struct chfs_scan_leb, u.queue);
   1478  1.1  ahoka 
   1479  1.1  ahoka 	EBH_QUEUE_DESTROY(&si->erased,
   1480  1.1  ahoka 	    struct chfs_scan_leb, u.queue);
   1481  1.1  ahoka 
   1482  1.1  ahoka 	EBH_QUEUE_DESTROY(&si->free,
   1483  1.1  ahoka 	    struct chfs_scan_leb, u.queue);
   1484  1.1  ahoka 
   1485  1.1  ahoka 	EBH_TREE_DESTROY(scan_leb_used_rbtree,
   1486  1.1  ahoka 	    &si->used, struct chfs_scan_leb);
   1487  1.1  ahoka 
   1488  1.1  ahoka 	kmem_free(si, sizeof(*si));
   1489  1.1  ahoka 	dbg_ebh("[SCAN_INFO_DESTROY] scanning information destroyed\n");
   1490  1.1  ahoka }
   1491  1.1  ahoka 
   1492  1.1  ahoka /**
   1493  1.1  ahoka  * scan_media - scan media
   1494  1.1  ahoka  *
   1495  1.1  ahoka  * @ebh - chfs eraseblock handler
   1496  1.1  ahoka  *
   1497  1.1  ahoka  * Returns zero in case of success, error code in case of fail.
   1498  1.1  ahoka  */
   1499  1.1  ahoka 
   1500  1.1  ahoka int
   1501  1.1  ahoka scan_media(struct chfs_ebh *ebh)
   1502  1.1  ahoka {
   1503  1.1  ahoka 	int err, i, avg_ec;
   1504  1.1  ahoka 	struct chfs_scan_info *si;
   1505  1.1  ahoka 	struct chfs_scan_leb *sleb;
   1506  1.1  ahoka 
   1507  1.1  ahoka 	si = chfs_scan(ebh);
   1508  1.1  ahoka 	/*
   1509  1.1  ahoka 	 * Process the scan info, manage the eraseblock lists
   1510  1.1  ahoka 	 */
   1511  1.1  ahoka 	mutex_init(&ebh->ltree_lock, MUTEX_DEFAULT, IPL_NONE);
   1512  1.1  ahoka 	mutex_init(&ebh->erase_lock, MUTEX_DEFAULT, IPL_NONE);
   1513  1.1  ahoka 	RB_INIT(&ebh->ltree);
   1514  1.1  ahoka 	RB_INIT(&ebh->free);
   1515  1.1  ahoka 	RB_INIT(&ebh->in_use);
   1516  1.1  ahoka 	TAILQ_INIT(&ebh->to_erase);
   1517  1.1  ahoka 	TAILQ_INIT(&ebh->fully_erased);
   1518  1.1  ahoka 	mutex_init(&ebh->alc_mutex, MUTEX_DEFAULT, IPL_NONE);
   1519  1.1  ahoka 
   1520  1.1  ahoka 	ebh->peb_nr -= si->bad_peb_cnt;
   1521  1.1  ahoka 
   1522  1.1  ahoka 	/*
   1523  1.1  ahoka 	 * Create background thread for erasing
   1524  1.1  ahoka 	 */
   1525  1.1  ahoka 	erase_thread_start(ebh);
   1526  1.1  ahoka 
   1527  1.1  ahoka 	ebh->lmap = kmem_alloc(ebh->peb_nr * sizeof(int), KM_SLEEP);
   1528  1.1  ahoka 
   1529  1.1  ahoka 	for (i = 0; i < ebh->peb_nr; i++) {
   1530  1.1  ahoka 		ebh->lmap[i] = EBH_LEB_UNMAPPED;
   1531  1.1  ahoka 	}
   1532  1.1  ahoka 
   1533  1.1  ahoka 	if (si->num_of_eb == 0) {
   1534  1.1  ahoka 		/* The flash contains no data. */
   1535  1.1  ahoka 		avg_ec = 0;
   1536  1.1  ahoka 	}
   1537  1.1  ahoka 	else {
   1538  1.1  ahoka 		avg_ec = (int) (si->sum_of_ec / si->num_of_eb);
   1539  1.1  ahoka 	}
   1540  1.1  ahoka 	dbg_ebh("num_of_eb: %d\n", si->num_of_eb);
   1541  1.1  ahoka 
   1542  1.1  ahoka 	mutex_enter(&ebh->erase_lock);
   1543  1.1  ahoka 
   1544  1.1  ahoka 	RB_FOREACH(sleb, scan_leb_used_rbtree, &si->used) {
   1545  1.1  ahoka 		ebh->lmap[sleb->lnr] = sleb->pebnr;
   1546  1.1  ahoka 		err = add_peb_to_in_use(ebh, sleb->pebnr, sleb->erase_cnt);
   1547  1.1  ahoka 		if (err)
   1548  1.1  ahoka 			goto out_free;
   1549  1.1  ahoka 	}
   1550  1.1  ahoka 
   1551  1.1  ahoka 	TAILQ_FOREACH(sleb, &si->erased, u.queue) {
   1552  1.1  ahoka 		err = add_peb_to_erase_queue(ebh, sleb->pebnr, avg_ec,
   1553  1.1  ahoka 		    &ebh->fully_erased);
   1554  1.1  ahoka 		if (err)
   1555  1.1  ahoka 			goto out_free;
   1556  1.1  ahoka 	}
   1557  1.1  ahoka 
   1558  1.1  ahoka 	TAILQ_FOREACH(sleb, &si->erase, u.queue) {
   1559  1.1  ahoka 		err = add_peb_to_erase_queue(ebh, sleb->pebnr, avg_ec,
   1560  1.1  ahoka 		    &ebh->to_erase);
   1561  1.1  ahoka 		if (err)
   1562  1.1  ahoka 			goto out_free;
   1563  1.1  ahoka 	}
   1564  1.1  ahoka 
   1565  1.1  ahoka 	TAILQ_FOREACH(sleb, &si->free, u.queue) {
   1566  1.1  ahoka 		err = add_peb_to_free(ebh, sleb->pebnr, sleb->erase_cnt);
   1567  1.1  ahoka 		if (err)
   1568  1.1  ahoka 			goto out_free;
   1569  1.1  ahoka 	}
   1570  1.1  ahoka 
   1571  1.1  ahoka 	TAILQ_FOREACH(sleb, &si->corrupted, u.queue) {
   1572  1.1  ahoka 		err = add_peb_to_erase_queue(ebh, sleb->pebnr, avg_ec,
   1573  1.1  ahoka 		    &ebh->to_erase);
   1574  1.1  ahoka 		if (err)
   1575  1.1  ahoka 			goto out_free;
   1576  1.1  ahoka 	}
   1577  1.1  ahoka 	mutex_exit(&ebh->erase_lock);
   1578  1.1  ahoka 	scan_info_destroy(si);
   1579  1.1  ahoka 	return 0;
   1580  1.1  ahoka 
   1581  1.1  ahoka out_free:
   1582  1.1  ahoka 	mutex_exit(&ebh->erase_lock);
   1583  1.1  ahoka 	kmem_free(ebh->lmap, ebh->peb_nr * sizeof(int));
   1584  1.1  ahoka 	scan_info_destroy(si);
   1585  1.1  ahoka 	dbg_ebh("[SCAN_MEDIA] returning with error: %d\n", err);
   1586  1.1  ahoka 	return err;
   1587  1.1  ahoka }
   1588  1.1  ahoka 
   1589  1.1  ahoka /*****************************************************************************/
   1590  1.1  ahoka /* End of Scan related operations					     */
   1591  1.1  ahoka /*****************************************************************************/
   1592  1.1  ahoka 
   1593  1.1  ahoka /**
   1594  1.1  ahoka  * ebh_open - opens mtd device and init ereaseblock header
   1595  1.1  ahoka  * @ebh: eraseblock handler
   1596  1.1  ahoka  * @flash_nr: flash device number to use
   1597  1.1  ahoka  *
   1598  1.1  ahoka  * Returns zero in case of success, error code in case of fail.
   1599  1.1  ahoka  */
   1600  1.1  ahoka int
   1601  1.1  ahoka ebh_open(struct chfs_ebh *ebh, dev_t dev)
   1602  1.1  ahoka {
   1603  1.1  ahoka 	int err;
   1604  1.1  ahoka 
   1605  1.1  ahoka 	ebh->flash_dev = flash_get_device(dev);
   1606  1.1  ahoka 	if (!ebh->flash_dev) {
   1607  1.1  ahoka 		aprint_error("ebh_open: cant get flash device\n");
   1608  1.1  ahoka 		return ENODEV;
   1609  1.1  ahoka 	}
   1610  1.1  ahoka 
   1611  1.1  ahoka 	ebh->flash_if = flash_get_interface(dev);
   1612  1.1  ahoka 	if (!ebh->flash_if) {
   1613  1.1  ahoka 		aprint_error("ebh_open: cant get flash interface\n");
   1614  1.1  ahoka 		return ENODEV;
   1615  1.1  ahoka 	}
   1616  1.1  ahoka 
   1617  1.1  ahoka 	ebh->flash_size = flash_get_size(dev);
   1618  1.1  ahoka 	ebh->peb_nr = ebh->flash_size / ebh->flash_if->erasesize;
   1619  1.1  ahoka //	ebh->peb_nr = ebh->flash_if->size / ebh->flash_if->erasesize;
   1620  1.1  ahoka 	/* Set up flash operations based on flash type */
   1621  1.1  ahoka 	ebh->ops = kmem_alloc(sizeof(struct chfs_ebh_ops), KM_SLEEP);
   1622  1.1  ahoka 
   1623  1.1  ahoka 	switch (ebh->flash_if->type) {
   1624  1.1  ahoka 	case FLASH_TYPE_NOR:
   1625  1.1  ahoka 		ebh->eb_size = ebh->flash_if->erasesize -
   1626  1.1  ahoka 		    CHFS_EB_EC_HDR_SIZE - CHFS_EB_HDR_NOR_SIZE;
   1627  1.1  ahoka 
   1628  1.1  ahoka 		ebh->ops->read_eb_hdr = nor_read_eb_hdr;
   1629  1.1  ahoka 		ebh->ops->write_eb_hdr = nor_write_eb_hdr;
   1630  1.1  ahoka 		ebh->ops->check_eb_hdr = nor_check_eb_hdr;
   1631  1.1  ahoka 		ebh->ops->mark_eb_hdr_dirty_flash =
   1632  1.1  ahoka 		    nor_mark_eb_hdr_dirty_flash;
   1633  1.1  ahoka 		ebh->ops->invalidate_eb_hdr = nor_invalidate_eb_hdr;
   1634  1.1  ahoka 		ebh->ops->mark_eb_hdr_free = mark_eb_hdr_free;
   1635  1.1  ahoka 
   1636  1.1  ahoka 		ebh->ops->process_eb = nor_process_eb;
   1637  1.1  ahoka 
   1638  1.1  ahoka 		ebh->ops->create_eb_hdr = nor_create_eb_hdr;
   1639  1.1  ahoka 		ebh->ops->calc_data_offs = nor_calc_data_offs;
   1640  1.1  ahoka 
   1641  1.1  ahoka 		ebh->max_serial = NULL;
   1642  1.1  ahoka 		break;
   1643  1.1  ahoka 	case FLASH_TYPE_NAND:
   1644  1.1  ahoka 		ebh->eb_size = ebh->flash_if->erasesize -
   1645  1.1  ahoka 		    2 * ebh->flash_if->page_size;
   1646  1.1  ahoka 
   1647  1.1  ahoka 		ebh->ops->read_eb_hdr = nand_read_eb_hdr;
   1648  1.1  ahoka 		ebh->ops->write_eb_hdr = nand_write_eb_hdr;
   1649  1.1  ahoka 		ebh->ops->check_eb_hdr = nand_check_eb_hdr;
   1650  1.1  ahoka 		ebh->ops->mark_eb_hdr_free = mark_eb_hdr_free;
   1651  1.1  ahoka 		ebh->ops->mark_eb_hdr_dirty_flash = NULL;
   1652  1.1  ahoka 		ebh->ops->invalidate_eb_hdr = NULL;
   1653  1.1  ahoka 
   1654  1.1  ahoka 		ebh->ops->process_eb = nand_process_eb;
   1655  1.1  ahoka 
   1656  1.1  ahoka 		ebh->ops->create_eb_hdr = nand_create_eb_hdr;
   1657  1.1  ahoka 		ebh->ops->calc_data_offs = nand_calc_data_offs;
   1658  1.1  ahoka 
   1659  1.1  ahoka 		ebh->max_serial = kmem_alloc(sizeof(uint64_t), KM_SLEEP);
   1660  1.1  ahoka 
   1661  1.1  ahoka 		*ebh->max_serial = 0;
   1662  1.1  ahoka 		break;
   1663  1.1  ahoka 	default:
   1664  1.1  ahoka 		return 1;
   1665  1.1  ahoka 	}
   1666  1.1  ahoka 	printf("opening ebh: eb_size: %zu\n", ebh->eb_size);
   1667  1.1  ahoka 	err = scan_media(ebh);
   1668  1.1  ahoka 	if (err) {
   1669  1.1  ahoka 		dbg_ebh("Scan failed.");
   1670  1.1  ahoka 		kmem_free(ebh->ops, sizeof(struct chfs_ebh_ops));
   1671  1.1  ahoka 		kmem_free(ebh, sizeof(struct chfs_ebh));
   1672  1.1  ahoka 		return err;
   1673  1.1  ahoka 	}
   1674  1.1  ahoka 	return 0;
   1675  1.1  ahoka }
   1676  1.1  ahoka 
   1677  1.1  ahoka /**
   1678  1.1  ahoka  * ebh_close - close ebh
   1679  1.1  ahoka  * @ebh: eraseblock handler
   1680  1.1  ahoka  * Returns zero in case of success, error code in case of fail.
   1681  1.1  ahoka  */
   1682  1.1  ahoka int
   1683  1.1  ahoka ebh_close(struct chfs_ebh *ebh)
   1684  1.1  ahoka {
   1685  1.1  ahoka 	erase_thread_stop(ebh);
   1686  1.1  ahoka 
   1687  1.1  ahoka 	EBH_TREE_DESTROY(peb_free_rbtree, &ebh->free, struct chfs_peb);
   1688  1.1  ahoka 	EBH_TREE_DESTROY(peb_in_use_rbtree, &ebh->in_use, struct chfs_peb);
   1689  1.1  ahoka 
   1690  1.1  ahoka 	EBH_QUEUE_DESTROY(&ebh->fully_erased, struct chfs_peb, u.queue);
   1691  1.1  ahoka 	EBH_QUEUE_DESTROY(&ebh->to_erase, struct chfs_peb, u.queue);
   1692  1.1  ahoka 
   1693  1.1  ahoka 	/* XXX HACK, see ebh.h */
   1694  1.1  ahoka 	EBH_TREE_DESTROY_MUTEX(ltree_rbtree, &ebh->ltree,
   1695  1.1  ahoka 	    struct chfs_ltree_entry);
   1696  1.1  ahoka 
   1697  1.1  ahoka 	KASSERT(!mutex_owned(&ebh->ltree_lock));
   1698  1.1  ahoka 	KASSERT(!mutex_owned(&ebh->alc_mutex));
   1699  1.1  ahoka 	KASSERT(!mutex_owned(&ebh->erase_lock));
   1700  1.1  ahoka 
   1701  1.1  ahoka 	mutex_destroy(&ebh->ltree_lock);
   1702  1.1  ahoka 	mutex_destroy(&ebh->alc_mutex);
   1703  1.1  ahoka 	mutex_destroy(&ebh->erase_lock);
   1704  1.1  ahoka 
   1705  1.1  ahoka 	kmem_free(ebh->ops, sizeof(struct chfs_ebh_ops));
   1706  1.1  ahoka 	kmem_free(ebh, sizeof(struct chfs_ebh));
   1707  1.1  ahoka 
   1708  1.1  ahoka 	return 0;
   1709  1.1  ahoka }
   1710  1.1  ahoka 
   1711  1.1  ahoka /**
   1712  1.1  ahoka  * ebh_read_leb - read data from leb
   1713  1.1  ahoka  * @ebh: eraseblock handler
   1714  1.1  ahoka  * @lnr: logical eraseblock number
   1715  1.1  ahoka  * @buf: buffer to read to
   1716  1.1  ahoka  * @offset: offset from where to read
   1717  1.1  ahoka  * @len: bytes number to read
   1718  1.1  ahoka  *
   1719  1.1  ahoka  * Returns zero in case of success, error code in case of fail.
   1720  1.1  ahoka  */
   1721  1.1  ahoka int
   1722  1.1  ahoka ebh_read_leb(struct chfs_ebh *ebh, int lnr, char *buf, uint32_t offset,
   1723  1.1  ahoka     size_t len, size_t *retlen)
   1724  1.1  ahoka {
   1725  1.1  ahoka 	int err, pebnr;
   1726  1.1  ahoka 	off_t data_offset;
   1727  1.1  ahoka 
   1728  1.1  ahoka 	KASSERT(offset + len <= ebh->eb_size);
   1729  1.1  ahoka 
   1730  1.1  ahoka 	err = leb_read_lock(ebh, lnr);
   1731  1.1  ahoka 	if (err)
   1732  1.1  ahoka 		return err;
   1733  1.1  ahoka 	pebnr = ebh->lmap[lnr];
   1734  1.1  ahoka 	/* If PEB is not mapped the buffer is filled with 0xFF */
   1735  1.1  ahoka 	if (EBH_LEB_UNMAPPED == pebnr) {
   1736  1.1  ahoka 		leb_read_unlock(ebh, lnr);
   1737  1.1  ahoka 		memset(buf, 0xFF, len);
   1738  1.1  ahoka 		return 0;
   1739  1.1  ahoka 	}
   1740  1.1  ahoka 
   1741  1.1  ahoka 	/* Read data */
   1742  1.1  ahoka 	data_offset = ebh->ops->calc_data_offs(ebh, pebnr, offset);
   1743  1.1  ahoka 	err = flash_read(ebh->flash_dev, data_offset, len, retlen,
   1744  1.1  ahoka 	    (unsigned char *) buf);
   1745  1.1  ahoka 	if (err)
   1746  1.1  ahoka 		goto out_free;
   1747  1.1  ahoka 
   1748  1.1  ahoka 	KASSERT(len == *retlen);
   1749  1.1  ahoka 
   1750  1.1  ahoka 	leb_read_unlock(ebh, lnr);
   1751  1.1  ahoka 	return err;
   1752  1.1  ahoka 
   1753  1.1  ahoka out_free:
   1754  1.1  ahoka 	leb_read_unlock(ebh, lnr);
   1755  1.1  ahoka 	return err;
   1756  1.1  ahoka }
   1757  1.1  ahoka 
   1758  1.1  ahoka /**
   1759  1.1  ahoka  * get_peb: get a free physical eraseblock
   1760  1.1  ahoka  * @ebh - chfs eraseblock handler
   1761  1.1  ahoka  *
   1762  1.1  ahoka  * This function gets a free eraseblock from the ebh->free RB-tree.
   1763  1.1  ahoka  * The fist entry will be returned and deleted from the tree.
   1764  1.1  ahoka  * The entries sorted by the erase counters, so the PEB with the smallest
   1765  1.1  ahoka  * erase counter will be added back.
   1766  1.1  ahoka  * If something goes bad a negative value will be returned.
   1767  1.1  ahoka  */
   1768  1.1  ahoka int
   1769  1.1  ahoka get_peb(struct chfs_ebh *ebh)
   1770  1.1  ahoka {
   1771  1.1  ahoka 	int err, pebnr;
   1772  1.1  ahoka 	struct chfs_peb *peb;
   1773  1.1  ahoka 
   1774  1.1  ahoka retry:
   1775  1.1  ahoka 	mutex_enter(&ebh->erase_lock);
   1776  1.1  ahoka 	//dbg_ebh("LOCK: ebh->erase_lock spin locked in get_peb()\n");
   1777  1.1  ahoka 	if (RB_EMPTY(&ebh->free)) {
   1778  1.1  ahoka 		/*There is no more free PEBs in the tree*/
   1779  1.1  ahoka 		if (TAILQ_EMPTY(&ebh->to_erase) &&
   1780  1.1  ahoka 		    TAILQ_EMPTY(&ebh->fully_erased)) {
   1781  1.1  ahoka 			mutex_exit(&ebh->erase_lock);
   1782  1.1  ahoka 			//dbg_ebh("UNLOCK: ebh->erase_lock spin unlocked in get_peb()\n");
   1783  1.1  ahoka 			return ENOSPC;
   1784  1.1  ahoka 		}
   1785  1.1  ahoka 		err = free_peb(ebh);
   1786  1.1  ahoka 
   1787  1.1  ahoka 		mutex_exit(&ebh->erase_lock);
   1788  1.1  ahoka 		//dbg_ebh("UNLOCK: ebh->erase_lock spin unlocked in get_peb()\n");
   1789  1.1  ahoka 
   1790  1.1  ahoka 		if (err)
   1791  1.1  ahoka 			return err;
   1792  1.1  ahoka 		goto retry;
   1793  1.1  ahoka 	}
   1794  1.1  ahoka 	peb = RB_MIN(peb_free_rbtree, &ebh->free);
   1795  1.1  ahoka 	pebnr = peb->pebnr;
   1796  1.1  ahoka 	RB_REMOVE(peb_free_rbtree, &ebh->free, peb);
   1797  1.1  ahoka 	err = add_peb_to_in_use(ebh, peb->pebnr, peb->erase_cnt);
   1798  1.1  ahoka 	if (err)
   1799  1.1  ahoka 		pebnr = err;
   1800  1.1  ahoka 
   1801  1.1  ahoka 	kmem_free(peb, sizeof(struct chfs_peb));
   1802  1.1  ahoka 
   1803  1.1  ahoka 	mutex_exit(&ebh->erase_lock);
   1804  1.1  ahoka 	//dbg_ebh("UNLOCK: ebh->erase_lock spin unlocked in get_peb()\n");
   1805  1.1  ahoka 
   1806  1.1  ahoka 	return pebnr;
   1807  1.1  ahoka }
   1808  1.1  ahoka 
   1809  1.1  ahoka /**
   1810  1.1  ahoka  * ebh_write_leb - write data to leb
   1811  1.1  ahoka  * @ebh: eraseblock handler
   1812  1.1  ahoka  * @lnr: logical eraseblock number
   1813  1.1  ahoka  * @buf: data to write
   1814  1.1  ahoka  * @offset: offset where to write
   1815  1.1  ahoka  * @len: bytes number to write
   1816  1.1  ahoka  *
   1817  1.1  ahoka  * Returns zero in case of success, error code in case of fail.
   1818  1.1  ahoka  */
   1819  1.1  ahoka int
   1820  1.1  ahoka ebh_write_leb(struct chfs_ebh *ebh, int lnr, char *buf, uint32_t offset,
   1821  1.1  ahoka     size_t len, size_t *retlen)
   1822  1.1  ahoka {
   1823  1.1  ahoka 	int err, pebnr, retries = 0;
   1824  1.1  ahoka 	off_t data_offset;
   1825  1.1  ahoka 	struct chfs_eb_hdr *ebhdr;
   1826  1.1  ahoka 
   1827  1.1  ahoka 	dbg("offset: %d | len: %zu | (offset+len): %zu "
   1828  1.1  ahoka 	    " | ebsize: %zu\n", offset, len, (offset+len), ebh->eb_size);
   1829  1.1  ahoka 
   1830  1.1  ahoka 	KASSERT(offset + len <= ebh->eb_size);
   1831  1.1  ahoka 
   1832  1.1  ahoka 	err = leb_write_lock(ebh, lnr);
   1833  1.1  ahoka 	if (err)
   1834  1.1  ahoka 		return err;
   1835  1.1  ahoka 
   1836  1.1  ahoka 	pebnr = ebh->lmap[lnr];
   1837  1.1  ahoka 	/* If the LEB is mapped write out data */
   1838  1.1  ahoka 	if (pebnr != EBH_LEB_UNMAPPED) {
   1839  1.1  ahoka 		data_offset = ebh->ops->calc_data_offs(ebh, pebnr, offset);
   1840  1.1  ahoka 		err = flash_write(ebh->flash_dev, data_offset, len, retlen,
   1841  1.1  ahoka 		    (unsigned char *) buf);
   1842  1.1  ahoka 
   1843  1.1  ahoka 		if (err) {
   1844  1.1  ahoka 			chfs_err("error %d while writing %zu bytes to PEB "
   1845  1.1  ahoka 			    "%d:%ju, written %zu bytes\n",
   1846  1.1  ahoka 			    err, len, pebnr, (uintmax_t )offset, *retlen);
   1847  1.1  ahoka 		} else {
   1848  1.1  ahoka 			KASSERT(len == *retlen);
   1849  1.1  ahoka 		}
   1850  1.1  ahoka 
   1851  1.1  ahoka 		leb_write_unlock(ebh, lnr);
   1852  1.1  ahoka 		return err;
   1853  1.1  ahoka 	}
   1854  1.1  ahoka 
   1855  1.1  ahoka 	/*
   1856  1.1  ahoka 	 * If the LEB is unmapped, get a free PEB and write the
   1857  1.1  ahoka 	 * eraseblock header first
   1858  1.1  ahoka 	 */
   1859  1.1  ahoka 	ebhdr = kmem_alloc(sizeof(struct chfs_eb_hdr), KM_SLEEP);
   1860  1.1  ahoka 
   1861  1.1  ahoka 	/* Setting up eraseblock header properties */
   1862  1.1  ahoka 	ebh->ops->create_eb_hdr(ebhdr, lnr);
   1863  1.1  ahoka 
   1864  1.1  ahoka retry:
   1865  1.1  ahoka 	/* Getting a physical eraseblock from the wear leveling system */
   1866  1.1  ahoka 	pebnr = get_peb(ebh);
   1867  1.1  ahoka 	if (pebnr < 0) {
   1868  1.1  ahoka 		leb_write_unlock(ebh, lnr);
   1869  1.1  ahoka 		kmem_free(ebhdr, sizeof(struct chfs_eb_hdr));
   1870  1.1  ahoka 		return pebnr;
   1871  1.1  ahoka 	}
   1872  1.1  ahoka 
   1873  1.1  ahoka 	/* Write the eraseblock header to the media */
   1874  1.1  ahoka 	err = ebh->ops->write_eb_hdr(ebh, pebnr, ebhdr);
   1875  1.1  ahoka 	if (err) {
   1876  1.1  ahoka 		chfs_warn(
   1877  1.1  ahoka 			"error writing eraseblock header: LEB %d , PEB %d\n",
   1878  1.1  ahoka 			lnr, pebnr);
   1879  1.1  ahoka 		goto write_error;
   1880  1.1  ahoka 	}
   1881  1.1  ahoka 
   1882  1.1  ahoka 	/* Write out data */
   1883  1.1  ahoka 	if (len) {
   1884  1.1  ahoka 		data_offset = ebh->ops->calc_data_offs(ebh, pebnr, offset);
   1885  1.1  ahoka 		err = flash_write(ebh->flash_dev,
   1886  1.1  ahoka 		    data_offset, len, retlen, (unsigned char *) buf);
   1887  1.1  ahoka 		if (err) {
   1888  1.1  ahoka 			chfs_err("error %d while writing %zu bytes to PEB "
   1889  1.1  ahoka 			    " %d:%ju, written %zu bytes\n",
   1890  1.1  ahoka 			    err, len, pebnr, (uintmax_t )offset, *retlen);
   1891  1.1  ahoka 			goto write_error;
   1892  1.1  ahoka 		}
   1893  1.1  ahoka 	}
   1894  1.1  ahoka 
   1895  1.1  ahoka 	ebh->lmap[lnr] = pebnr;
   1896  1.1  ahoka 	leb_write_unlock(ebh, lnr);
   1897  1.1  ahoka 	kmem_free(ebhdr, sizeof(struct chfs_eb_hdr));
   1898  1.1  ahoka 
   1899  1.1  ahoka 	return 0;
   1900  1.1  ahoka 
   1901  1.1  ahoka write_error: err = release_peb(ebh, pebnr);
   1902  1.1  ahoka 	// max retries (NOW: 2)
   1903  1.1  ahoka 	if (err || CHFS_MAX_GET_PEB_RETRIES < ++retries) {
   1904  1.1  ahoka 		leb_write_unlock(ebh, lnr);
   1905  1.1  ahoka 		kmem_free(ebhdr, sizeof(struct chfs_eb_hdr));
   1906  1.1  ahoka 		return err;
   1907  1.1  ahoka 	}
   1908  1.1  ahoka 	goto retry;
   1909  1.1  ahoka }
   1910  1.1  ahoka 
   1911  1.1  ahoka /**
   1912  1.1  ahoka  * ebh_erase_leb - erase a leb
   1913  1.1  ahoka  * @ebh: eraseblock handler
   1914  1.1  ahoka  * @lnr: leb number
   1915  1.1  ahoka  *
   1916  1.1  ahoka  * Returns zero in case of success, error code in case of fail.
   1917  1.1  ahoka  */
   1918  1.1  ahoka int
   1919  1.1  ahoka ebh_erase_leb(struct chfs_ebh *ebh, int lnr)
   1920  1.1  ahoka {
   1921  1.1  ahoka 	int err, pebnr;
   1922  1.1  ahoka 
   1923  1.1  ahoka 	leb_write_lock(ebh, lnr);
   1924  1.1  ahoka 
   1925  1.1  ahoka 	pebnr = ebh->lmap[lnr];
   1926  1.1  ahoka 	if (pebnr < 0) {
   1927  1.1  ahoka 		leb_write_unlock(ebh, lnr);
   1928  1.1  ahoka 		return EBH_LEB_UNMAPPED;
   1929  1.1  ahoka 	}
   1930  1.1  ahoka 	err = release_peb(ebh, pebnr);
   1931  1.1  ahoka 	if (err)
   1932  1.1  ahoka 		goto out_unlock;
   1933  1.1  ahoka 
   1934  1.1  ahoka 	ebh->lmap[lnr] = EBH_LEB_UNMAPPED;
   1935  1.1  ahoka 	cv_signal(&ebh->bg_erase.eth_wakeup);
   1936  1.1  ahoka out_unlock:
   1937  1.1  ahoka 	leb_write_unlock(ebh, lnr);
   1938  1.1  ahoka 	return err;
   1939  1.1  ahoka }
   1940  1.1  ahoka 
   1941  1.1  ahoka /**
   1942  1.1  ahoka  * ebh_map_leb - maps a PEB to LEB
   1943  1.1  ahoka  * @ebh: eraseblock handler
   1944  1.1  ahoka  * @lnr: leb number
   1945  1.1  ahoka  *
   1946  1.1  ahoka  * Returns zero on success, error code in case of fail
   1947  1.1  ahoka  */
   1948  1.1  ahoka int
   1949  1.1  ahoka ebh_map_leb(struct chfs_ebh *ebh, int lnr)
   1950  1.1  ahoka {
   1951  1.1  ahoka 	int err, pebnr, retries = 0;
   1952  1.1  ahoka 	struct chfs_eb_hdr *ebhdr;
   1953  1.1  ahoka 
   1954  1.1  ahoka 	ebhdr = kmem_alloc(sizeof(struct chfs_eb_hdr), KM_SLEEP);
   1955  1.1  ahoka 
   1956  1.1  ahoka 	err = leb_write_lock(ebh, lnr);
   1957  1.1  ahoka 	if (err)
   1958  1.1  ahoka 		return err;
   1959  1.1  ahoka 
   1960  1.1  ahoka retry:
   1961  1.1  ahoka 	pebnr = get_peb(ebh);
   1962  1.1  ahoka 	if (pebnr < 0) {
   1963  1.1  ahoka 		err = pebnr;
   1964  1.1  ahoka 		goto out_unlock;
   1965  1.1  ahoka 	}
   1966  1.1  ahoka 
   1967  1.1  ahoka 	ebh->ops->create_eb_hdr(ebhdr, lnr);
   1968  1.1  ahoka 
   1969  1.1  ahoka 	err = ebh->ops->write_eb_hdr(ebh, pebnr, ebhdr);
   1970  1.1  ahoka 	if (err) {
   1971  1.1  ahoka 		chfs_warn(
   1972  1.1  ahoka 			"error writing eraseblock header: LEB %d , PEB %d\n",
   1973  1.1  ahoka 			lnr, pebnr);
   1974  1.1  ahoka 		goto write_error;
   1975  1.1  ahoka 	}
   1976  1.1  ahoka 
   1977  1.1  ahoka 	ebh->lmap[lnr] = pebnr;
   1978  1.1  ahoka 
   1979  1.1  ahoka out_unlock:
   1980  1.1  ahoka 	leb_write_unlock(ebh, lnr);
   1981  1.1  ahoka 	return err;
   1982  1.1  ahoka 
   1983  1.1  ahoka write_error:
   1984  1.1  ahoka 	err = release_peb(ebh, pebnr);
   1985  1.1  ahoka 	// max retries (NOW: 2)
   1986  1.1  ahoka 	if (err || CHFS_MAX_GET_PEB_RETRIES < ++retries) {
   1987  1.1  ahoka 		leb_write_unlock(ebh, lnr);
   1988  1.1  ahoka 		kmem_free(ebhdr, sizeof(struct chfs_eb_hdr));
   1989  1.1  ahoka 		return err;
   1990  1.1  ahoka 	}
   1991  1.1  ahoka 	goto retry;
   1992  1.1  ahoka }
   1993  1.1  ahoka 
   1994  1.1  ahoka /**
   1995  1.1  ahoka  * ebh_unmap_leb -
   1996  1.1  ahoka  * @ebh: eraseblock handler
   1997  1.1  ahoka  * @lnr: leb number
   1998  1.1  ahoka  *
   1999  1.1  ahoka  * Retruns zero on success, error code in case of fail.
   2000  1.1  ahoka  */
   2001  1.1  ahoka int
   2002  1.1  ahoka ebh_unmap_leb(struct chfs_ebh *ebh, int lnr)
   2003  1.1  ahoka {
   2004  1.1  ahoka 	int err;
   2005  1.1  ahoka 
   2006  1.1  ahoka 	if (ebh_is_mapped(ebh, lnr) < 0)
   2007  1.1  ahoka 		/* If the eraseblock already unmapped */
   2008  1.1  ahoka 		return 0;
   2009  1.1  ahoka 
   2010  1.1  ahoka 	err = ebh_erase_leb(ebh, lnr);
   2011  1.1  ahoka 
   2012  1.1  ahoka 	return err;
   2013  1.1  ahoka }
   2014  1.1  ahoka 
   2015  1.1  ahoka /**
   2016  1.1  ahoka  * ebh_is_mapped - check if a PEB is mapped to @lnr
   2017  1.1  ahoka  * @ebh: eraseblock handler
   2018  1.1  ahoka  * @lnr: leb number
   2019  1.1  ahoka  *
   2020  1.1  ahoka  * Retruns 0 if the logical eraseblock is mapped, negative error code otherwise.
   2021  1.1  ahoka  */
   2022  1.1  ahoka int
   2023  1.1  ahoka ebh_is_mapped(struct chfs_ebh *ebh, int lnr)
   2024  1.1  ahoka {
   2025  1.1  ahoka 	int err, result;
   2026  1.1  ahoka 	err = leb_read_lock(ebh, lnr);
   2027  1.1  ahoka 	if (err)
   2028  1.1  ahoka 		return err;
   2029  1.1  ahoka 
   2030  1.1  ahoka 	result = ebh->lmap[lnr];
   2031  1.1  ahoka 	leb_read_unlock(ebh, lnr);
   2032  1.1  ahoka 
   2033  1.1  ahoka 	return result;
   2034  1.1  ahoka }
   2035  1.1  ahoka 
   2036  1.1  ahoka /**
   2037  1.1  ahoka  * ebh_change_leb - write the LEB to another PEB
   2038  1.1  ahoka  * @ebh: eraseblock handler
   2039  1.1  ahoka  * @lnr: leb number
   2040  1.1  ahoka  * @buf: data to write
   2041  1.1  ahoka  * @len: length of data
   2042  1.1  ahoka  * Returns zero in case of success, error code in case of fail.
   2043  1.1  ahoka  */
   2044  1.1  ahoka int
   2045  1.1  ahoka ebh_change_leb(struct chfs_ebh *ebh, int lnr, char *buf, size_t len,
   2046  1.1  ahoka     size_t *retlen)
   2047  1.1  ahoka {
   2048  1.1  ahoka 	int err, pebnr, pebnr_old, retries = 0;
   2049  1.1  ahoka 	off_t data_offset;
   2050  1.1  ahoka 
   2051  1.1  ahoka 	struct chfs_peb *peb = NULL;
   2052  1.1  ahoka 	struct chfs_eb_hdr *ebhdr;
   2053  1.1  ahoka 
   2054  1.1  ahoka 	if (ebh_is_mapped(ebh, lnr) < 0)
   2055  1.1  ahoka 		return EBH_LEB_UNMAPPED;
   2056  1.1  ahoka 
   2057  1.1  ahoka 	if (len == 0) {
   2058  1.1  ahoka 		err = ebh_unmap_leb(ebh, lnr);
   2059  1.1  ahoka 		if (err)
   2060  1.1  ahoka 			return err;
   2061  1.1  ahoka 		return ebh_map_leb(ebh, lnr);
   2062  1.1  ahoka 	}
   2063  1.1  ahoka 
   2064  1.1  ahoka 	ebhdr = kmem_alloc(sizeof(struct chfs_eb_hdr), KM_SLEEP);
   2065  1.1  ahoka 
   2066  1.1  ahoka 	pebnr_old = ebh->lmap[lnr];
   2067  1.1  ahoka 
   2068  1.1  ahoka 	mutex_enter(&ebh->alc_mutex);
   2069  1.1  ahoka 	err = leb_write_lock(ebh, lnr);
   2070  1.1  ahoka 	if (err)
   2071  1.1  ahoka 		goto out_mutex;
   2072  1.1  ahoka 
   2073  1.1  ahoka 	if (ebh->ops->mark_eb_hdr_dirty_flash) {
   2074  1.1  ahoka 		err = ebh->ops->mark_eb_hdr_dirty_flash(ebh, pebnr_old, lnr);
   2075  1.1  ahoka 		if (err)
   2076  1.1  ahoka 			goto out_unlock;
   2077  1.1  ahoka 	}
   2078  1.1  ahoka 
   2079  1.1  ahoka 	/* Setting up eraseblock header properties */
   2080  1.1  ahoka 	ebh->ops->create_eb_hdr(ebhdr, lnr);
   2081  1.1  ahoka 
   2082  1.1  ahoka retry:
   2083  1.1  ahoka 	/* Getting a physical eraseblock from the wear leveling system */
   2084  1.1  ahoka 	pebnr = get_peb(ebh);
   2085  1.1  ahoka 	if (pebnr < 0) {
   2086  1.1  ahoka 		leb_write_unlock(ebh, lnr);
   2087  1.1  ahoka 		mutex_exit(&ebh->alc_mutex);
   2088  1.1  ahoka 		kmem_free(ebhdr, sizeof(struct chfs_eb_hdr));
   2089  1.1  ahoka 		return pebnr;
   2090  1.1  ahoka 	}
   2091  1.1  ahoka 
   2092  1.1  ahoka 	err = ebh->ops->write_eb_hdr(ebh, pebnr, ebhdr);
   2093  1.1  ahoka 	if (err) {
   2094  1.1  ahoka 		chfs_warn(
   2095  1.1  ahoka 			"error writing eraseblock header: LEB %d , PEB %d",
   2096  1.1  ahoka 			lnr, pebnr);
   2097  1.1  ahoka 		goto write_error;
   2098  1.1  ahoka 	}
   2099  1.1  ahoka 
   2100  1.1  ahoka 	/* Write out data */
   2101  1.1  ahoka 	data_offset = ebh->ops->calc_data_offs(ebh, pebnr, 0);
   2102  1.1  ahoka 	err = flash_write(ebh->flash_dev, data_offset, len, retlen,
   2103  1.1  ahoka 	    (unsigned char *) buf);
   2104  1.1  ahoka 	if (err) {
   2105  1.1  ahoka 		chfs_err("error %d while writing %zu bytes to PEB %d:%ju,"
   2106  1.1  ahoka 		    " written %zu bytes",
   2107  1.1  ahoka 		    err, len, pebnr, (uintmax_t)data_offset, *retlen);
   2108  1.1  ahoka 		goto write_error;
   2109  1.1  ahoka 	}
   2110  1.1  ahoka 
   2111  1.1  ahoka 	ebh->lmap[lnr] = pebnr;
   2112  1.1  ahoka 
   2113  1.1  ahoka 	if (ebh->ops->invalidate_eb_hdr) {
   2114  1.1  ahoka 		err = ebh->ops->invalidate_eb_hdr(ebh, pebnr_old);
   2115  1.1  ahoka 		if (err)
   2116  1.1  ahoka 			goto out_unlock;
   2117  1.1  ahoka 	}
   2118  1.1  ahoka 	peb = find_peb_in_use(ebh, pebnr_old);
   2119  1.1  ahoka 	err = release_peb(ebh, peb->pebnr);
   2120  1.1  ahoka 
   2121  1.1  ahoka out_unlock:
   2122  1.1  ahoka 	leb_write_unlock(ebh, lnr);
   2123  1.1  ahoka 
   2124  1.1  ahoka out_mutex:
   2125  1.1  ahoka 	mutex_exit(&ebh->alc_mutex);
   2126  1.1  ahoka 	kmem_free(ebhdr, sizeof(struct chfs_eb_hdr));
   2127  1.1  ahoka 	kmem_free(peb, sizeof(struct chfs_peb));
   2128  1.1  ahoka 	return err;
   2129  1.1  ahoka 
   2130  1.1  ahoka write_error:
   2131  1.1  ahoka 	err = release_peb(ebh, pebnr);
   2132  1.1  ahoka 	//max retries (NOW: 2)
   2133  1.1  ahoka 	if (err || CHFS_MAX_GET_PEB_RETRIES < ++retries) {
   2134  1.1  ahoka 		leb_write_unlock(ebh, lnr);
   2135  1.1  ahoka 		mutex_exit(&ebh->alc_mutex);
   2136  1.1  ahoka 		kmem_free(ebhdr, sizeof(struct chfs_eb_hdr));
   2137  1.1  ahoka 		return err;
   2138  1.1  ahoka 	}
   2139  1.1  ahoka 	goto retry;
   2140  1.1  ahoka }
   2141  1.1  ahoka 
   2142