xref: /netbsd-src/sys/dev/flash/flash.c (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 /*	$NetBSD: flash.c,v 1.16 2021/04/24 23:36:53 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 2011 Department of Software Engineering,
5  *		      University of Szeged, Hungary
6  * Copyright (c) 2011 Adam Hoka <ahoka@NetBSD.org>
7  * Copyright (c) 2010 David Tengeri <dtengeri@inf.u-szeged.hu>
8  * All rights reserved.
9  *
10  * This code is derived from software contributed to The NetBSD Foundation
11  * by the Department of Software Engineering, University of Szeged, Hungary
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 /*-
36  * Framework for storage devices based on Flash technology
37  */
38 
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: flash.c,v 1.16 2021/04/24 23:36:53 thorpej Exp $");
41 
42 #include <sys/param.h>
43 #include <sys/types.h>
44 #include <sys/proc.h>
45 #include <sys/errno.h>
46 #include <sys/ioctl.h>
47 #include <sys/device.h>
48 #include <sys/conf.h>
49 #include <sys/kmem.h>
50 #include <sys/uio.h>
51 #include <sys/kernel.h>
52 
53 #include <sys/atomic.h>
54 #include <sys/buf.h>
55 #include <sys/bufq.h>
56 #include <sys/disk.h>
57 #include <sys/disklabel.h>
58 #include <sys/malloc.h>
59 #include <sys/reboot.h>
60 
61 #include "ioconf.h"
62 
63 #include <sys/flashio.h>
64 #include "flash.h"
65 
66 #ifdef FLASH_DEBUG
67 int flashdebug = FLASH_DEBUG;
68 #endif
69 
70 dev_type_open(flashopen);
71 dev_type_close(flashclose);
72 dev_type_read(flashread);
73 dev_type_write(flashwrite);
74 dev_type_ioctl(flashioctl);
75 dev_type_strategy(flashstrategy);
76 dev_type_dump(flashdump);
77 
78 int flash_print(void *aux, const char *pnp);
79 
80 bool flash_shutdown(device_t dev, int how);
81 int flash_nsectors(struct buf *bp);
82 int flash_sector(struct buf *bp);
83 
84 int flash_match(device_t parent, cfdata_t match, void *aux);
85 void flash_attach(device_t parent, device_t self, void *aux);
86 int flash_detach(device_t device, int flags);
87 
88 CFATTACH_DECL_NEW(flash, sizeof(struct flash_softc),
89     flash_match, flash_attach, flash_detach, NULL);
90 
91 /**
92  * Block device's operation
93  */
94 const struct bdevsw flash_bdevsw = {
95 	.d_open = flashopen,
96 	.d_close = flashclose,
97 	.d_strategy = flashstrategy,
98 	.d_ioctl = flashioctl,
99 	.d_dump = flashdump,
100 	.d_psize = nosize,
101 	.d_discard = nodiscard,	/* XXX this driver probably wants a discard */
102 	.d_flag = D_DISK | D_MPSAFE
103 };
104 
105 /**
106  * Character device's operations
107  */
108 const struct cdevsw flash_cdevsw = {
109 	.d_open = flashopen,
110 	.d_close = flashclose,
111 	.d_read = flashread,
112 	.d_write = flashwrite,
113 	.d_ioctl = flashioctl,
114 	.d_stop = nostop,
115 	.d_tty = notty,
116 	.d_poll = nopoll,
117 	.d_mmap = nommap,
118 	.d_kqfilter = nokqfilter,
119 	.d_discard = nodiscard,
120 	.d_flag = D_DISK | D_MPSAFE
121 };
122 
123 /* ARGSUSED */
124 int
125 flash_match(device_t parent, cfdata_t match, void *aux)
126 {
127 	/* pseudo device, always attaches */
128 	return 1;
129 }
130 
131 /* ARGSUSED */
132 void
133 flash_attach(device_t parent, device_t self, void *aux)
134 {
135 	struct flash_softc * const sc = device_private(self);
136 	struct flash_attach_args * const faa = aux;
137 	char pbuf[2][sizeof("9999 KB")];
138 
139 	sc->sc_dev = self;
140 	sc->sc_parent_dev = parent;
141 	sc->flash_if = faa->flash_if;
142 	sc->sc_partinfo = faa->partinfo;
143 	sc->hw_softc = device_private(parent);
144 
145 	format_bytes(pbuf[0], sizeof(pbuf[0]), sc->sc_partinfo.part_size);
146 	format_bytes(pbuf[1], sizeof(pbuf[1]), sc->flash_if->erasesize);
147 
148 	aprint_naive("\n");
149 
150 	aprint_normal(": partition");
151 	if (sc->sc_partinfo.part_name != NULL)
152 		aprint_normal(" \"%s\"", sc->sc_partinfo.part_name);
153 
154 	aprint_normal(", size %s, offset %#jx",
155 		pbuf[0], (uintmax_t)sc->sc_partinfo.part_offset);
156 
157 	if (sc->sc_partinfo.part_flags & FLASH_PART_READONLY) {
158 		sc->sc_readonly = true;
159 		aprint_normal(", read only");
160 	} else {
161 		sc->sc_readonly = false;
162 	}
163 
164 	aprint_normal("\n");
165 
166 	if (sc->sc_partinfo.part_size == 0) {
167 		aprint_error_dev(self,
168 		    "partition size must be larger than 0\n");
169 		return;
170 	}
171 
172 	switch (sc->flash_if->type) {
173 	case FLASH_TYPE_NOR:
174 		aprint_normal_dev(sc->sc_dev,
175 		    "erase size %s bytes, write size %d bytes\n",
176 		    pbuf[1], sc->flash_if->writesize);
177 		break;
178 
179 	case FLASH_TYPE_NAND:
180 	default:
181 		aprint_normal_dev(sc->sc_dev,
182 		    "erase size %s, page size %d bytes, write size %d bytes\n",
183 		    pbuf[1], sc->flash_if->page_size,
184 		    sc->flash_if->writesize);
185 		break;
186 	}
187 
188 	if (!pmf_device_register1(sc->sc_dev, NULL, NULL, flash_shutdown))
189 		aprint_error_dev(sc->sc_dev,
190 		    "couldn't establish power handler\n");
191 }
192 
193 int
194 flash_detach(device_t device, int flags)
195 {
196 	struct flash_softc * const sc = device_private(device);
197 
198 	pmf_device_deregister(sc->sc_dev);
199 
200 	/* freeing flash_if is our responsibility */
201 	kmem_free(sc->flash_if, sizeof(*sc->flash_if));
202 
203 	return 0;
204 }
205 
206 int
207 flash_print(void *aux, const char *pnp)
208 {
209 	struct flash_attach_args *arg;
210 	const char *type;
211 
212 	if (pnp != NULL) {
213 		arg = aux;
214 		switch (arg->flash_if->type) {
215 		case FLASH_TYPE_NOR:
216 			type = "NOR";
217 			break;
218 		case FLASH_TYPE_NAND:
219 			type = "NAND";
220 			break;
221 		default:
222 			panic("flash_print: unknown type %d",
223 			    arg->flash_if->type);
224 		}
225 		aprint_normal("%s flash at %s", type, pnp);
226 	}
227 	return UNCONF;
228 }
229 
230 device_t
231 flash_attach_mi(struct flash_interface * const flash_if, device_t device)
232 {
233 	struct flash_attach_args arg;
234 
235 #ifdef DIAGNOSTIC
236 	if (flash_if == NULL) {
237 		aprint_error("flash_attach_mi: NULL\n");
238 		return 0;
239 	}
240 #endif
241 	arg.flash_if = flash_if;
242 
243 	return config_found(device, &arg, flash_print,
244 	    CFARG_IATTR, "flashbus",
245 	    CFARG_EOL);
246 }
247 
248 /**
249  * flash_open - open the character device
250  * Checks if there is a driver registered to the minor number of the open
251  * request.
252  */
253 int
254 flashopen(dev_t dev, int flags, int fmt, lwp_t *l)
255 {
256 	int unit = minor(dev);
257 	struct flash_softc *sc;
258 
259 	FLDPRINTFN(1, ("flash: opening device unit %d\n", unit));
260 
261 	if ((sc = device_lookup_private(&flash_cd, unit)) == NULL)
262 		return ENXIO;
263 
264 	/* TODO return eperm if want to open for writing a read only dev */
265 
266 	/* reset buffer length */
267 //	sc->sc_cache->fc_len = 0;
268 
269 	return 0;
270 }
271 
272 /**
273  * flash_close - close device
274  * We don't have to release any resources, so just return 0.
275  */
276 int
277 flashclose(dev_t dev, int flags, int fmt, lwp_t *l)
278 {
279 	int unit = minor(dev);
280 	struct flash_softc *sc;
281 	int err;
282 
283 	FLDPRINTFN(1, ("flash: closing flash device unit %d\n", unit));
284 
285 	if ((sc = device_lookup_private(&flash_cd, unit)) == NULL)
286 		return ENXIO;
287 
288 	if (!sc->sc_readonly) {
289 		err = flash_sync(sc->sc_dev);
290 		if (err)
291 			return err;
292 	}
293 
294 	return 0;
295 }
296 
297 /**
298  * flash_read - read from character device
299  * This function uses the registered driver's read function to read the
300  * requested length to * a buffer and then moves this buffer to userspace.
301  */
302 int
303 flashread(dev_t dev, struct uio * const uio, int flag)
304 {
305 	return physio(flashstrategy, NULL, dev, B_READ, minphys, uio);
306 }
307 
308 /**
309  * flash_write - write to character device
310  * This function moves the data into a buffer from userspace to kernel space,
311  * then uses the registered driver's write function to write out the data to
312  * the media.
313  */
314 int
315 flashwrite(dev_t dev, struct uio * const uio, int flag)
316 {
317 	return physio(flashstrategy, NULL, dev, B_WRITE, minphys, uio);
318 }
319 
320 void
321 flashstrategy(struct buf * const bp)
322 {
323 	struct flash_softc *sc;
324 	const struct flash_interface *flash_if;
325 	const struct flash_partition *part;
326 	int unit, device_blks;
327 
328 	unit = minor(bp->b_dev);
329 	sc = device_lookup_private(&flash_cd, unit);
330 	if (sc == NULL) {
331 		bp->b_error = ENXIO;
332 		goto done;
333 	}
334 
335 	flash_if = sc->flash_if;
336 	part = &sc->sc_partinfo;
337 
338 	/* divider */
339 	KASSERT(flash_if->writesize != 0);
340 
341 	aprint_debug_dev(sc->sc_dev, "flash_strategy()\n");
342 
343 	if (!(bp->b_flags & B_READ) && sc->sc_readonly) {
344 		bp->b_error = EACCES;
345 		goto done;
346 	}
347 
348 	/* check if length is not negative */
349 	if (bp->b_blkno < 0) {
350 		bp->b_error = EINVAL;
351 		goto done;
352 	}
353 
354 	/* zero length i/o */
355 	if (bp->b_bcount == 0) {
356 		goto done;
357 	}
358 
359 	device_blks = sc->sc_partinfo.part_size / DEV_BSIZE;
360 	KASSERT(part->part_offset % DEV_BSIZE == 0);
361 	bp->b_rawblkno = bp->b_blkno + (part->part_offset / DEV_BSIZE);
362 
363 	if (bounds_check_with_mediasize(bp, DEV_BSIZE, device_blks) <= 0) {
364 		goto done;
365 	}
366 
367 	bp->b_resid = bp->b_bcount;
368 	flash_if->submit(sc->sc_parent_dev, bp);
369 
370 	return;
371 done:
372 	bp->b_resid = bp->b_bcount;
373 	biodone(bp);
374 }
375 
376 /*
377  * Handle the ioctl for the device
378  */
379 int
380 flashioctl(dev_t dev, u_long command, void * const data, int flags, lwp_t *l)
381 {
382 	struct flash_erase_params *ep;
383 	struct flash_info_params *ip;
384 	struct flash_dump_params *dp;
385 	struct flash_badblock_params *bbp;
386 	struct flash_erase_instruction ei;
387 	struct flash_softc *sc;
388 	int unit, err;
389 	size_t retlen;
390 	flash_off_t offset;
391 	bool bad;
392 
393 	unit = minor(dev);
394 	if ((sc = device_lookup_private(&flash_cd, unit)) == NULL)
395 		return ENXIO;
396 
397 	err = 0;
398 	switch (command) {
399 	case FLASH_ERASE_BLOCK:
400 		/**
401 		 * Set up an erase instruction then call the registered
402 		 * driver's erase operation.
403 		 */
404 		ep = data;
405 
406 		if (sc->sc_readonly) {
407 			return EACCES;
408 		}
409 
410 		ei.ei_addr = ep->ep_addr;
411 		ei.ei_len = ep->ep_len;
412 		ei.ei_callback = NULL;
413 
414 		err = flash_erase(sc->sc_dev, &ei);
415 		if (err) {
416 			return err;
417 		}
418 
419 		break;
420 	case FLASH_BLOCK_ISBAD:
421 		/**
422 		 * Set up an erase instruction then call the registered
423 		 * driver's erase operation.
424 		 */
425 		bbp = data;
426 
427 		err = flash_block_isbad(sc->sc_dev, bbp->bbp_addr, &bad);
428 		if (err) {
429 			return err;
430 		}
431 		bbp->bbp_isbad = bad;
432 
433 		break;
434 	case FLASH_BLOCK_MARKBAD:
435 		bbp = data;
436 
437 		err = flash_block_markbad(sc->sc_dev, bbp->bbp_addr);
438 
439 		break;
440 	case FLASH_DUMP:
441 		dp = data;
442 		offset = dp->dp_block * sc->flash_if->erasesize;
443 		FLDPRINTF(("Reading from block: %jd len: %jd\n",
444 			(intmax_t )dp->dp_block, (intmax_t )dp->dp_len));
445 		err = flash_read(sc->sc_parent_dev, offset, dp->dp_len,
446 		    &retlen, dp->dp_buf);
447 		if (err)
448 			return err;
449 		if (retlen != dp->dp_len) {
450 			dp->dp_len = -1;
451 			dp->dp_buf = NULL;
452 		}
453 
454 		break;
455 	case FLASH_GET_INFO:
456 		ip = data;
457 
458 		ip->ip_page_size = sc->flash_if->page_size;
459 		ip->ip_erase_size = sc->flash_if->erasesize;
460 		ip->ip_flash_type = sc->flash_if->type;
461 		ip->ip_flash_size = sc->sc_partinfo.part_size;
462 		break;
463 	default:
464 		err = ENODEV;
465 	}
466 
467 	return err;
468 }
469 
470 int
471 flashdump(dev_t dev, daddr_t blkno, void *va, size_t size)
472 {
473 	return EACCES;
474 }
475 
476 bool
477 flash_shutdown(device_t self, int how)
478 {
479 	struct flash_softc * const sc = device_private(self);
480 
481 	if ((how & RB_NOSYNC) == 0 && !sc->sc_readonly)
482 		flash_sync(self);
483 
484 	return true;
485 }
486 
487 const struct flash_interface *
488 flash_get_interface(dev_t dev)
489 {
490 	struct flash_softc *sc;
491 	int unit;
492 
493 	unit = minor(dev);
494 	if ((sc = device_lookup_private(&flash_cd, unit)) == NULL)
495 		return NULL;
496 
497 	return sc->flash_if;
498 }
499 
500 const struct flash_softc *
501 flash_get_softc(dev_t dev)
502 {
503 	struct flash_softc *sc;
504 	int unit;
505 
506 	unit = minor(dev);
507 	sc = device_lookup_private(&flash_cd, unit);
508 
509 	return sc;
510 }
511 
512 device_t
513 flash_get_device(dev_t dev)
514 {
515 	struct flash_softc *sc;
516 	int unit;
517 
518 	unit = minor(dev);
519 	sc = device_lookup_private(&flash_cd, unit);
520 
521 	return sc->sc_dev;
522 }
523 
524 flash_size_t
525 flash_get_size(dev_t dev)
526 {
527 	const struct flash_softc *sc;
528 
529 	sc = flash_get_softc(dev);
530 
531 	return sc->sc_partinfo.part_size;
532 }
533 
534 int
535 flash_erase(device_t self, struct flash_erase_instruction * const ei)
536 {
537 	struct flash_softc * const sc = device_private(self);
538 	KASSERT(ei != NULL);
539 	struct flash_erase_instruction e = *ei;
540 
541 	if (sc->sc_readonly)
542 		return EACCES;
543 
544 	/* adjust for flash partition */
545 	e.ei_addr += sc->sc_partinfo.part_offset;
546 
547 	/* bounds check for flash partition */
548 	if (e.ei_addr + e.ei_len > sc->sc_partinfo.part_size +
549 	    sc->sc_partinfo.part_offset)
550 		return EINVAL;
551 
552 	return sc->flash_if->erase(device_parent(self), &e);
553 }
554 
555 int
556 flash_read(device_t self, flash_off_t offset, size_t len, size_t * const retlen,
557     uint8_t * const buf)
558 {
559 	struct flash_softc * const sc = device_private(self);
560 
561 	offset += sc->sc_partinfo.part_offset;
562 
563 	if (offset + len > sc->sc_partinfo.part_size +
564 	    sc->sc_partinfo.part_offset)
565 		return EINVAL;
566 
567 	return sc->flash_if->read(device_parent(self),
568 	    offset, len, retlen, buf);
569 }
570 
571 int
572 flash_write(device_t self, flash_off_t offset, size_t len,
573     size_t * const retlen, const uint8_t * const buf)
574 {
575 	struct flash_softc * const sc = device_private(self);
576 
577 	if (sc->sc_readonly)
578 		return EACCES;
579 
580 	offset += sc->sc_partinfo.part_offset;
581 
582 	if (offset + len > sc->sc_partinfo.part_size +
583 	    sc->sc_partinfo.part_offset)
584 		return EINVAL;
585 
586 	return sc->flash_if->write(device_parent(self),
587 	    offset, len, retlen, buf);
588 }
589 
590 int
591 flash_block_markbad(device_t self, flash_off_t offset)
592 {
593 	struct flash_softc * const sc = device_private(self);
594 
595 	if (sc->sc_readonly)
596 		return EACCES;
597 
598 	offset += sc->sc_partinfo.part_offset;
599 
600 	if (offset + sc->flash_if->erasesize >=
601 	    sc->sc_partinfo.part_size +
602 	    sc->sc_partinfo.part_offset)
603 		return EINVAL;
604 
605 	return sc->flash_if->block_markbad(device_parent(self), offset);
606 }
607 
608 int
609 flash_block_isbad(device_t self, flash_off_t offset, bool * const bad)
610 {
611 	struct flash_softc * const sc = device_private(self);
612 
613 	offset += sc->sc_partinfo.part_offset;
614 
615 	if (offset + sc->flash_if->erasesize >
616 	    sc->sc_partinfo.part_size +
617 	    sc->sc_partinfo.part_offset)
618 		return EINVAL;
619 
620 	return sc->flash_if->block_isbad(device_parent(self), offset, bad);
621 }
622 
623 int
624 flash_sync(device_t self)
625 {
626 	struct flash_softc * const sc = device_private(self);
627 
628 	if (sc->sc_readonly)
629 		return EACCES;
630 
631 	/* noop now TODO: implement */
632 	return 0;
633 }
634 
635 MODULE(MODULE_CLASS_DRIVER, flash, NULL);
636 
637 #ifdef _MODULE
638 #include "ioconf.c"
639 #endif
640 
641 static int
642 flash_modcmd(modcmd_t cmd, void *opaque)
643 {
644 	int error = 0;
645 #ifdef _MODULE
646 	int bmaj = -1, cmaj = -1;
647 #endif
648 
649 	switch (cmd) {
650 	case MODULE_CMD_INIT:
651 #ifdef _MODULE
652 		error = config_init_component(cfdriver_ioconf_flash,
653 		    cfattach_ioconf_flash, cfdata_ioconf_flash);
654 		if (error)
655 			return error;
656 		error = devsw_attach("flash", &flash_bdevsw, &bmaj,
657 		    &flash_cdevsw, &cmaj);
658 		if (error)
659 			config_fini_component(cfdriver_ioconf_flash,
660 			    cfattach_ioconf_flash, cfdata_ioconf_flash);
661 #endif
662 		return error;
663 	case MODULE_CMD_FINI:
664 #ifdef _MODULE
665 		devsw_detach(&flash_bdevsw, &flash_cdevsw);
666 		error = config_fini_component(cfdriver_ioconf_flash,
667 		    cfattach_ioconf_flash, cfdata_ioconf_flash);
668 #endif
669 		return error;
670 	default:
671 		return ENOTTY;
672 	}
673 }
674