xref: /netbsd-src/sys/dev/flash/flash.c (revision b2a8932dbe9fdfd3d41d60d0a04b9a3ba294763d)
1 /*	$NetBSD: flash.c,v 1.14 2017/11/13 17:35:58 jmcneill 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.14 2017/11/13 17:35:58 jmcneill 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_ia(device, "flashbus", &arg, flash_print);
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
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
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
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
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
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 lenght 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
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
469 flashdump(dev_t dev, daddr_t blkno, void *va, size_t size)
470 {
471 	return EACCES;
472 }
473 
474 bool
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 *
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 *
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
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
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
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
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
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
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
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
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
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 = config_init_component(cfdriver_ioconf_flash,
651 		    cfattach_ioconf_flash, cfdata_ioconf_flash);
652 		if (error)
653 			return error;
654 		error = devsw_attach("flash", &flash_bdevsw, &bmaj,
655 		    &flash_cdevsw, &cmaj);
656 		if (error)
657 			config_fini_component(cfdriver_ioconf_flash,
658 			    cfattach_ioconf_flash, cfdata_ioconf_flash);
659 #endif
660 		return error;
661 	case MODULE_CMD_FINI:
662 #ifdef _MODULE
663 		devsw_detach(&flash_bdevsw, &flash_cdevsw);
664 		error = config_fini_component(cfdriver_ioconf_flash,
665 		    cfattach_ioconf_flash, cfdata_ioconf_flash);
666 #endif
667 		return error;
668 	default:
669 		return ENOTTY;
670 	}
671 }
672