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