2 * Copyright (C) 2009-2012 Semihalf
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 /* Simulated NAND controller driver */
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #include <sys/malloc.h>
41 #include <dev/nand/nand.h>
42 #include <dev/nand/nandsim.h>
43 #include <dev/nand/nandsim_chip.h>
44 #include <dev/nand/nandsim_log.h>
45 #include <dev/nand/nandsim_swap.h>
48 struct sim_ctrl_conf ctrls[MAX_SIM_DEV];
50 static struct cdev *nandsim_dev;
51 static d_ioctl_t nandsim_ioctl;
53 static void nandsim_init_sim_param(struct sim_param *);
54 static int nandsim_create_ctrl(struct sim_ctrl *);
55 static int nandsim_destroy_ctrl(int);
56 static int nandsim_ctrl_status(struct sim_ctrl *);
57 static int nandsim_create_chip(struct sim_chip *);
58 static int nandsim_destroy_chip(struct sim_ctrl_chip *);
59 static int nandsim_chip_status(struct sim_chip *);
60 static int nandsim_start_ctrl(int);
61 static int nandsim_stop_ctrl(int);
62 static int nandsim_inject_error(struct sim_error *);
63 static int nandsim_get_block_state(struct sim_block_state *);
64 static int nandsim_set_block_state(struct sim_block_state *);
65 static int nandsim_modify(struct sim_mod *);
66 static int nandsim_dump(struct sim_dump *);
67 static int nandsim_restore(struct sim_dump *);
68 static int nandsim_freeze(struct sim_ctrl_chip *);
69 static void nandsim_print_log(struct sim_log *);
70 static struct nandsim_chip *get_nandsim_chip(uint8_t, uint8_t);
72 static struct cdevsw nandsim_cdevsw = {
73 .d_version = D_VERSION,
74 .d_flags = D_NEEDGIANT,
75 .d_ioctl = nandsim_ioctl,
80 nandsim_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
81 int flags, struct thread *td)
86 case NANDSIM_SIM_PARAM:
87 nandsim_init_sim_param((struct sim_param *)data);
89 case NANDSIM_CREATE_CTRL:
90 ret = nandsim_create_ctrl((struct sim_ctrl *)data);
92 case NANDSIM_DESTROY_CTRL:
93 ret = nandsim_destroy_ctrl(*(int *)data);
95 case NANDSIM_STATUS_CTRL:
96 ret = nandsim_ctrl_status((struct sim_ctrl *)data);
98 case NANDSIM_CREATE_CHIP:
99 ret = nandsim_create_chip((struct sim_chip *)data);
101 case NANDSIM_DESTROY_CHIP:
102 ret = nandsim_destroy_chip((struct sim_ctrl_chip *)data);
104 case NANDSIM_STATUS_CHIP:
105 ret = nandsim_chip_status((struct sim_chip *)data);
108 ret = nandsim_modify((struct sim_mod *)data);
110 case NANDSIM_START_CTRL:
111 ret = nandsim_start_ctrl(*(int *)data);
113 case NANDSIM_STOP_CTRL:
114 ret = nandsim_stop_ctrl(*(int *)data);
116 case NANDSIM_INJECT_ERROR:
117 ret = nandsim_inject_error((struct sim_error *)data);
119 case NANDSIM_SET_BLOCK_STATE:
120 ret = nandsim_set_block_state((struct sim_block_state *)data);
122 case NANDSIM_GET_BLOCK_STATE:
123 ret = nandsim_get_block_state((struct sim_block_state *)data);
125 case NANDSIM_PRINT_LOG:
126 nandsim_print_log((struct sim_log *)data);
129 ret = nandsim_dump((struct sim_dump *)data);
131 case NANDSIM_RESTORE:
132 ret = nandsim_restore((struct sim_dump *)data);
135 ret = nandsim_freeze((struct sim_ctrl_chip *)data);
146 nandsim_init_sim_param(struct sim_param *param)
152 nand_debug(NDBG_SIM,"log level:%d output %d", param->log_level,
154 nandsim_log_level = param->log_level;
155 nandsim_log_output = param->log_output;
159 nandsim_create_ctrl(struct sim_ctrl *ctrl)
161 struct sim_ctrl_conf *sim_ctrl;
163 nand_debug(NDBG_SIM,"create controller num:%d cs:%d",ctrl->num,
166 if (ctrl->num >= MAX_SIM_DEV) {
170 sim_ctrl = &ctrls[ctrl->num];
171 if(sim_ctrl->created)
174 sim_ctrl->num = ctrl->num;
175 sim_ctrl->num_cs = ctrl->num_cs;
176 sim_ctrl->ecc = ctrl->ecc;
177 memcpy(sim_ctrl->ecc_layout, ctrl->ecc_layout,
178 MAX_ECC_BYTES * sizeof(ctrl->ecc_layout[0]));
179 strlcpy(sim_ctrl->filename, ctrl->filename,
181 sim_ctrl->created = 1;
187 nandsim_destroy_ctrl(int ctrl_num)
190 nand_debug(NDBG_SIM,"destroy controller num:%d", ctrl_num);
192 if (ctrl_num >= MAX_SIM_DEV) {
196 if (!ctrls[ctrl_num].created) {
200 if (ctrls[ctrl_num].running) {
204 memset(&ctrls[ctrl_num], 0, sizeof(ctrls[ctrl_num]));
210 nandsim_ctrl_status(struct sim_ctrl *ctrl)
213 nand_debug(NDBG_SIM,"status controller num:%d cs:%d",ctrl->num,
216 if (ctrl->num >= MAX_SIM_DEV) {
220 ctrl->num_cs = ctrls[ctrl->num].num_cs;
221 ctrl->ecc = ctrls[ctrl->num].ecc;
222 memcpy(ctrl->ecc_layout, ctrls[ctrl->num].ecc_layout,
223 MAX_ECC_BYTES * sizeof(ctrl->ecc_layout[0]));
224 strlcpy(ctrl->filename, ctrls[ctrl->num].filename,
226 ctrl->running = ctrls[ctrl->num].running;
227 ctrl->created = ctrls[ctrl->num].created;
233 nandsim_create_chip(struct sim_chip *chip)
235 struct sim_chip *sim_chip;
237 nand_debug(NDBG_SIM,"create chip num:%d at ctrl:%d", chip->num,
240 if (chip->ctrl_num >= MAX_SIM_DEV ||
241 chip->num >= MAX_CTRL_CS) {
245 if (ctrls[chip->ctrl_num].chips[chip->num]) {
249 sim_chip = malloc(sizeof(*sim_chip), M_NANDSIM,
251 if (sim_chip == NULL) {
255 memcpy(sim_chip, chip, sizeof(*sim_chip));
256 ctrls[chip->ctrl_num].chips[chip->num] = sim_chip;
257 sim_chip->created = 1;
263 nandsim_destroy_chip(struct sim_ctrl_chip *chip)
265 struct sim_ctrl_conf *ctrl_conf;
267 nand_debug(NDBG_SIM,"destroy chip num:%d at ctrl:%d", chip->chip_num,
270 if (chip->ctrl_num >= MAX_SIM_DEV ||
271 chip->chip_num >= MAX_CTRL_CS)
274 ctrl_conf = &ctrls[chip->ctrl_num];
276 if (!ctrl_conf->created || !ctrl_conf->chips[chip->chip_num])
279 if (ctrl_conf->running)
282 free(ctrl_conf->chips[chip->chip_num], M_NANDSIM);
283 ctrl_conf->chips[chip->chip_num] = NULL;
289 nandsim_chip_status(struct sim_chip *chip)
291 struct sim_ctrl_conf *ctrl_conf;
293 nand_debug(NDBG_SIM,"status for chip num:%d at ctrl:%d", chip->num,
296 if (chip->ctrl_num >= MAX_SIM_DEV &&
297 chip->num >= MAX_CTRL_CS)
300 ctrl_conf = &ctrls[chip->ctrl_num];
301 if (!ctrl_conf->chips[chip->num])
304 memcpy(chip, ctrl_conf->chips[chip->num], sizeof(*chip));
310 nandsim_start_ctrl(int num)
312 device_t nexus, ndev;
313 devclass_t nexus_devclass;
316 nand_debug(NDBG_SIM,"start ctlr num:%d", num);
318 if (num >= MAX_SIM_DEV)
321 if (!ctrls[num].created)
324 if (ctrls[num].running)
327 /* We will add our device as a child of the nexus0 device */
328 if (!(nexus_devclass = devclass_find("nexus")) ||
329 !(nexus = devclass_get_device(nexus_devclass, 0)))
333 * Create a newbus device representing this frontend instance
335 * XXX powerpc nexus doesn't implement bus_add_child, so child
336 * must be added by device_add_child().
338 #if defined(__powerpc__)
339 ndev = device_add_child(nexus, "nandsim", num);
341 ndev = BUS_ADD_CHILD(nexus, 0, "nandsim", num);
347 ret = device_probe_and_attach(ndev);
351 ctrls[num].sim_ctrl_dev = ndev;
352 ctrls[num].running = 1;
359 nandsim_stop_ctrl(int num)
362 devclass_t nexus_devclass;
365 nand_debug(NDBG_SIM,"stop controller num:%d", num);
367 if (num >= MAX_SIM_DEV) {
371 if (!ctrls[num].created || !ctrls[num].running) {
375 /* We will add our device as a child of the nexus0 device */
376 if (!(nexus_devclass = devclass_find("nexus")) ||
377 !(nexus = devclass_get_device(nexus_devclass, 0))) {
382 if (ctrls[num].sim_ctrl_dev) {
383 ret = device_delete_child(nexus, ctrls[num].sim_ctrl_dev);
384 ctrls[num].sim_ctrl_dev = NULL;
388 ctrls[num].running = 0;
393 static struct nandsim_chip *
394 get_nandsim_chip(uint8_t ctrl_num, uint8_t chip_num)
396 struct nandsim_softc *sc;
398 if (!ctrls[ctrl_num].sim_ctrl_dev)
401 sc = device_get_softc(ctrls[ctrl_num].sim_ctrl_dev);
402 return (sc->chips[chip_num]);
406 nandsim_print_log(struct sim_log *sim_log)
408 struct nandsim_softc *sc;
411 if (!ctrls[sim_log->ctrl_num].sim_ctrl_dev)
414 sc = device_get_softc(ctrls[sim_log->ctrl_num].sim_ctrl_dev);
416 len1 = strlen(&sc->log_buff[sc->log_idx + 1]);
417 if (len1 >= sim_log->len)
419 copyout(&sc->log_buff[sc->log_idx + 1], sim_log->log, len1);
420 len2 = strlen(sc->log_buff);
421 if (len2 >= (sim_log->len - len1))
422 len2 = (sim_log->len - len1);
423 copyout(sc->log_buff, &sim_log->log[len1], len2);
424 sim_log->len = len1 + len2;
429 nandsim_inject_error(struct sim_error *error)
431 struct nandsim_chip *chip;
432 struct block_space *bs;
433 struct onfi_params *param;
434 int page, page_size, block, offset;
436 nand_debug(NDBG_SIM,"inject error for chip %d at ctrl %d\n",
437 error->chip_num, error->ctrl_num);
439 if (error->ctrl_num >= MAX_SIM_DEV ||
440 error->chip_num >= MAX_CTRL_CS)
443 if (!ctrls[error->ctrl_num].created || !ctrls[error->ctrl_num].running)
446 chip = get_nandsim_chip(error->ctrl_num, error->chip_num);
447 param = &chip->params;
448 page_size = param->bytes_per_page + param->spare_bytes_per_page;
449 block = error->page_num / param->pages_per_block;
450 page = error->page_num % param->pages_per_block;
452 bs = get_bs(chip->swap, block, 1);
456 offset = (page * page_size) + error->column;
457 memset(&bs->blk_ptr[offset], error->pattern, error->len);
463 nandsim_set_block_state(struct sim_block_state *bs)
465 struct onfi_params *params;
466 struct nandsim_chip *chip;
469 nand_debug(NDBG_SIM,"set block state for %d:%d block %d\n",
470 bs->chip_num, bs->ctrl_num, bs->block_num);
472 if (bs->ctrl_num >= MAX_SIM_DEV ||
473 bs->chip_num >= MAX_CTRL_CS)
476 chip = get_nandsim_chip(bs->ctrl_num, bs->chip_num);
477 params = &chip->params;
478 blocks = params->luns * params->blocks_per_lun;
480 if (bs->block_num > blocks)
483 chip->blk_state[bs->block_num].is_bad = bs->state;
485 if (bs->wearout >= 0)
486 chip->blk_state[bs->block_num].wear_lev = bs->wearout;
492 nandsim_get_block_state(struct sim_block_state *bs)
494 struct onfi_params *params;
495 struct nandsim_chip *chip;
498 if (bs->ctrl_num >= MAX_SIM_DEV ||
499 bs->chip_num >= MAX_CTRL_CS)
502 nand_debug(NDBG_SIM,"get block state for %d:%d block %d\n",
503 bs->chip_num, bs->ctrl_num, bs->block_num);
505 chip = get_nandsim_chip(bs->ctrl_num, bs->chip_num);
506 params = &chip->params;
507 blocks = params->luns * params->blocks_per_lun;
509 if (bs->block_num > blocks)
512 bs->state = chip->blk_state[bs->block_num].is_bad;
513 bs->wearout = chip->blk_state[bs->block_num].wear_lev;
519 nandsim_dump(struct sim_dump *dump)
521 struct nandsim_chip *chip;
522 struct block_space *bs;
525 nand_debug(NDBG_SIM,"dump chip %d %d\n", dump->ctrl_num, dump->chip_num);
527 if (dump->ctrl_num >= MAX_SIM_DEV ||
528 dump->chip_num >= MAX_CTRL_CS)
531 chip = get_nandsim_chip(dump->ctrl_num, dump->chip_num);
532 blk_size = chip->cg.block_size +
533 (chip->cg.oob_size * chip->cg.pgs_per_blk);
535 bs = get_bs(chip->swap, dump->block_num, 0);
539 if (dump->len > blk_size)
540 dump->len = blk_size;
542 copyout(bs->blk_ptr, dump->data, dump->len);
548 nandsim_restore(struct sim_dump *dump)
550 struct nandsim_chip *chip;
551 struct block_space *bs;
554 nand_debug(NDBG_SIM,"restore chip %d %d\n", dump->ctrl_num,
557 if (dump->ctrl_num >= MAX_SIM_DEV ||
558 dump->chip_num >= MAX_CTRL_CS)
561 chip = get_nandsim_chip(dump->ctrl_num, dump->chip_num);
562 blk_size = chip->cg.block_size +
563 (chip->cg.oob_size * chip->cg.pgs_per_blk);
565 bs = get_bs(chip->swap, dump->block_num, 1);
569 if (dump->len > blk_size)
570 dump->len = blk_size;
573 copyin(dump->data, bs->blk_ptr, dump->len);
579 nandsim_freeze(struct sim_ctrl_chip *ctrl_chip)
581 struct nandsim_chip *chip;
583 if (ctrl_chip->ctrl_num >= MAX_SIM_DEV ||
584 ctrl_chip->chip_num >= MAX_CTRL_CS)
587 chip = get_nandsim_chip(ctrl_chip->ctrl_num, ctrl_chip->chip_num);
588 nandsim_chip_freeze(chip);
594 nandsim_modify(struct sim_mod *mod)
596 struct sim_chip *sim_conf = NULL;
597 struct nandsim_chip *sim_chip = NULL;
599 nand_debug(NDBG_SIM,"modify ctlr %d chip %d", mod->ctrl_num,
602 if (mod->field != SIM_MOD_LOG_LEVEL) {
603 if (mod->ctrl_num >= MAX_SIM_DEV ||
604 mod->chip_num >= MAX_CTRL_CS)
607 sim_conf = ctrls[mod->ctrl_num].chips[mod->chip_num];
608 sim_chip = get_nandsim_chip(mod->ctrl_num, mod->chip_num);
611 switch (mod->field) {
612 case SIM_MOD_LOG_LEVEL:
613 nandsim_log_level = mod->new_value;
615 case SIM_MOD_ERASE_TIME:
616 sim_conf->erase_time = sim_chip->erase_delay = mod->new_value;
618 case SIM_MOD_PROG_TIME:
619 sim_conf->prog_time = sim_chip->prog_delay = mod->new_value;
621 case SIM_MOD_READ_TIME:
622 sim_conf->read_time = sim_chip->read_delay = mod->new_value;
624 case SIM_MOD_ERROR_RATIO:
625 sim_conf->error_ratio = mod->new_value;
626 sim_chip->error_ratio = mod->new_value;
635 nandsim_modevent(module_t mod __unused, int type, void *data __unused)
637 struct sim_ctrl_chip chip_ctrl;
642 nandsim_dev = make_dev(&nandsim_cdevsw, 0,
643 UID_ROOT, GID_WHEEL, 0600, "nandsim.ioctl");
646 for (i = 0; i < MAX_SIM_DEV; i++) {
647 nandsim_stop_ctrl(i);
648 chip_ctrl.ctrl_num = i;
649 for (j = 0; j < MAX_CTRL_CS; j++) {
650 chip_ctrl.chip_num = j;
651 nandsim_destroy_chip(&chip_ctrl);
653 nandsim_destroy_ctrl(i);
655 destroy_dev(nandsim_dev);
665 DEV_MODULE(nandsim, nandsim_modevent, NULL);
666 MODULE_VERSION(nandsim, 1);
667 MODULE_DEPEND(nandsim, nand, 1, 1, 1);
668 MODULE_DEPEND(nandsim, alq, 1, 1, 1);