flash.c revision 1.17 1 /* $NetBSD: flash.c,v 1.17 2021/08/07 16:19:10 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2011 Department of Software Engineering,
5 * University of Szeged, Hungary
6 * Copyright (c) 2011 Adam Hoka <ahoka (at) NetBSD.org>
7 * Copyright (c) 2010 David Tengeri <dtengeri (at) 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.17 2021/08/07 16:19:10 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 CFARGS(.iattr = "flashbus"));
245 }
246
247 /**
248 * flash_open - open the character device
249 * Checks if there is a driver registered to the minor number of the open
250 * request.
251 */
252 int
253 flashopen(dev_t dev, int flags, int fmt, lwp_t *l)
254 {
255 int unit = minor(dev);
256 struct flash_softc *sc;
257
258 FLDPRINTFN(1, ("flash: opening device unit %d\n", unit));
259
260 if ((sc = device_lookup_private(&flash_cd, unit)) == NULL)
261 return ENXIO;
262
263 /* TODO return eperm if want to open for writing a read only dev */
264
265 /* reset buffer length */
266 // sc->sc_cache->fc_len = 0;
267
268 return 0;
269 }
270
271 /**
272 * flash_close - close device
273 * We don't have to release any resources, so just return 0.
274 */
275 int
276 flashclose(dev_t dev, int flags, int fmt, lwp_t *l)
277 {
278 int unit = minor(dev);
279 struct flash_softc *sc;
280 int err;
281
282 FLDPRINTFN(1, ("flash: closing flash device unit %d\n", unit));
283
284 if ((sc = device_lookup_private(&flash_cd, unit)) == NULL)
285 return ENXIO;
286
287 if (!sc->sc_readonly) {
288 err = flash_sync(sc->sc_dev);
289 if (err)
290 return err;
291 }
292
293 return 0;
294 }
295
296 /**
297 * flash_read - read from character device
298 * This function uses the registered driver's read function to read the
299 * requested length to * a buffer and then moves this buffer to userspace.
300 */
301 int
302 flashread(dev_t dev, struct uio * const uio, int flag)
303 {
304 return physio(flashstrategy, NULL, dev, B_READ, minphys, uio);
305 }
306
307 /**
308 * flash_write - write to character device
309 * This function moves the data into a buffer from userspace to kernel space,
310 * then uses the registered driver's write function to write out the data to
311 * the media.
312 */
313 int
314 flashwrite(dev_t dev, struct uio * const uio, int flag)
315 {
316 return physio(flashstrategy, NULL, dev, B_WRITE, minphys, uio);
317 }
318
319 void
320 flashstrategy(struct buf * const bp)
321 {
322 struct flash_softc *sc;
323 const struct flash_interface *flash_if;
324 const struct flash_partition *part;
325 int unit, device_blks;
326
327 unit = minor(bp->b_dev);
328 sc = device_lookup_private(&flash_cd, unit);
329 if (sc == NULL) {
330 bp->b_error = ENXIO;
331 goto done;
332 }
333
334 flash_if = sc->flash_if;
335 part = &sc->sc_partinfo;
336
337 /* divider */
338 KASSERT(flash_if->writesize != 0);
339
340 aprint_debug_dev(sc->sc_dev, "flash_strategy()\n");
341
342 if (!(bp->b_flags & B_READ) && sc->sc_readonly) {
343 bp->b_error = EACCES;
344 goto done;
345 }
346
347 /* check if length is not negative */
348 if (bp->b_blkno < 0) {
349 bp->b_error = EINVAL;
350 goto done;
351 }
352
353 /* zero length i/o */
354 if (bp->b_bcount == 0) {
355 goto done;
356 }
357
358 device_blks = sc->sc_partinfo.part_size / DEV_BSIZE;
359 KASSERT(part->part_offset % DEV_BSIZE == 0);
360 bp->b_rawblkno = bp->b_blkno + (part->part_offset / DEV_BSIZE);
361
362 if (bounds_check_with_mediasize(bp, DEV_BSIZE, device_blks) <= 0) {
363 goto done;
364 }
365
366 bp->b_resid = bp->b_bcount;
367 flash_if->submit(sc->sc_parent_dev, bp);
368
369 return;
370 done:
371 bp->b_resid = bp->b_bcount;
372 biodone(bp);
373 }
374
375 /*
376 * Handle the ioctl for the device
377 */
378 int
379 flashioctl(dev_t dev, u_long command, void * const data, int flags, lwp_t *l)
380 {
381 struct flash_erase_params *ep;
382 struct flash_info_params *ip;
383 struct flash_dump_params *dp;
384 struct flash_badblock_params *bbp;
385 struct flash_erase_instruction ei;
386 struct flash_softc *sc;
387 int unit, err;
388 size_t retlen;
389 flash_off_t offset;
390 bool bad;
391
392 unit = minor(dev);
393 if ((sc = device_lookup_private(&flash_cd, unit)) == NULL)
394 return ENXIO;
395
396 err = 0;
397 switch (command) {
398 case FLASH_ERASE_BLOCK:
399 /**
400 * Set up an erase instruction then call the registered
401 * driver's erase operation.
402 */
403 ep = data;
404
405 if (sc->sc_readonly) {
406 return EACCES;
407 }
408
409 ei.ei_addr = ep->ep_addr;
410 ei.ei_len = ep->ep_len;
411 ei.ei_callback = NULL;
412
413 err = flash_erase(sc->sc_dev, &ei);
414 if (err) {
415 return err;
416 }
417
418 break;
419 case FLASH_BLOCK_ISBAD:
420 /**
421 * Set up an erase instruction then call the registered
422 * driver's erase operation.
423 */
424 bbp = data;
425
426 err = flash_block_isbad(sc->sc_dev, bbp->bbp_addr, &bad);
427 if (err) {
428 return err;
429 }
430 bbp->bbp_isbad = bad;
431
432 break;
433 case FLASH_BLOCK_MARKBAD:
434 bbp = data;
435
436 err = flash_block_markbad(sc->sc_dev, bbp->bbp_addr);
437
438 break;
439 case FLASH_DUMP:
440 dp = data;
441 offset = dp->dp_block * sc->flash_if->erasesize;
442 FLDPRINTF(("Reading from block: %jd len: %jd\n",
443 (intmax_t )dp->dp_block, (intmax_t )dp->dp_len));
444 err = flash_read(sc->sc_parent_dev, offset, dp->dp_len,
445 &retlen, dp->dp_buf);
446 if (err)
447 return err;
448 if (retlen != dp->dp_len) {
449 dp->dp_len = -1;
450 dp->dp_buf = NULL;
451 }
452
453 break;
454 case FLASH_GET_INFO:
455 ip = data;
456
457 ip->ip_page_size = sc->flash_if->page_size;
458 ip->ip_erase_size = sc->flash_if->erasesize;
459 ip->ip_flash_type = sc->flash_if->type;
460 ip->ip_flash_size = sc->sc_partinfo.part_size;
461 break;
462 default:
463 err = ENODEV;
464 }
465
466 return err;
467 }
468
469 int
470 flashdump(dev_t dev, daddr_t blkno, void *va, size_t size)
471 {
472 return EACCES;
473 }
474
475 bool
476 flash_shutdown(device_t self, int how)
477 {
478 struct flash_softc * const sc = device_private(self);
479
480 if ((how & RB_NOSYNC) == 0 && !sc->sc_readonly)
481 flash_sync(self);
482
483 return true;
484 }
485
486 const struct flash_interface *
487 flash_get_interface(dev_t dev)
488 {
489 struct flash_softc *sc;
490 int unit;
491
492 unit = minor(dev);
493 if ((sc = device_lookup_private(&flash_cd, unit)) == NULL)
494 return NULL;
495
496 return sc->flash_if;
497 }
498
499 const struct flash_softc *
500 flash_get_softc(dev_t dev)
501 {
502 struct flash_softc *sc;
503 int unit;
504
505 unit = minor(dev);
506 sc = device_lookup_private(&flash_cd, unit);
507
508 return sc;
509 }
510
511 device_t
512 flash_get_device(dev_t dev)
513 {
514 struct flash_softc *sc;
515 int unit;
516
517 unit = minor(dev);
518 sc = device_lookup_private(&flash_cd, unit);
519
520 return sc->sc_dev;
521 }
522
523 flash_size_t
524 flash_get_size(dev_t dev)
525 {
526 const struct flash_softc *sc;
527
528 sc = flash_get_softc(dev);
529
530 return sc->sc_partinfo.part_size;
531 }
532
533 int
534 flash_erase(device_t self, struct flash_erase_instruction * const ei)
535 {
536 struct flash_softc * const sc = device_private(self);
537 KASSERT(ei != NULL);
538 struct flash_erase_instruction e = *ei;
539
540 if (sc->sc_readonly)
541 return EACCES;
542
543 /* adjust for flash partition */
544 e.ei_addr += sc->sc_partinfo.part_offset;
545
546 /* bounds check for flash partition */
547 if (e.ei_addr + e.ei_len > sc->sc_partinfo.part_size +
548 sc->sc_partinfo.part_offset)
549 return EINVAL;
550
551 return sc->flash_if->erase(device_parent(self), &e);
552 }
553
554 int
555 flash_read(device_t self, flash_off_t offset, size_t len, size_t * const retlen,
556 uint8_t * const buf)
557 {
558 struct flash_softc * const sc = device_private(self);
559
560 offset += sc->sc_partinfo.part_offset;
561
562 if (offset + len > sc->sc_partinfo.part_size +
563 sc->sc_partinfo.part_offset)
564 return EINVAL;
565
566 return sc->flash_if->read(device_parent(self),
567 offset, len, retlen, buf);
568 }
569
570 int
571 flash_write(device_t self, flash_off_t offset, size_t len,
572 size_t * const retlen, const uint8_t * const buf)
573 {
574 struct flash_softc * const sc = device_private(self);
575
576 if (sc->sc_readonly)
577 return EACCES;
578
579 offset += sc->sc_partinfo.part_offset;
580
581 if (offset + len > sc->sc_partinfo.part_size +
582 sc->sc_partinfo.part_offset)
583 return EINVAL;
584
585 return sc->flash_if->write(device_parent(self),
586 offset, len, retlen, buf);
587 }
588
589 int
590 flash_block_markbad(device_t self, flash_off_t offset)
591 {
592 struct flash_softc * const sc = device_private(self);
593
594 if (sc->sc_readonly)
595 return EACCES;
596
597 offset += sc->sc_partinfo.part_offset;
598
599 if (offset + sc->flash_if->erasesize >=
600 sc->sc_partinfo.part_size +
601 sc->sc_partinfo.part_offset)
602 return EINVAL;
603
604 return sc->flash_if->block_markbad(device_parent(self), offset);
605 }
606
607 int
608 flash_block_isbad(device_t self, flash_off_t offset, bool * const bad)
609 {
610 struct flash_softc * const sc = device_private(self);
611
612 offset += sc->sc_partinfo.part_offset;
613
614 if (offset + sc->flash_if->erasesize >
615 sc->sc_partinfo.part_size +
616 sc->sc_partinfo.part_offset)
617 return EINVAL;
618
619 return sc->flash_if->block_isbad(device_parent(self), offset, bad);
620 }
621
622 int
623 flash_sync(device_t self)
624 {
625 struct flash_softc * const sc = device_private(self);
626
627 if (sc->sc_readonly)
628 return EACCES;
629
630 /* noop now TODO: implement */
631 return 0;
632 }
633
634 MODULE(MODULE_CLASS_DRIVER, flash, NULL);
635
636 #ifdef _MODULE
637 #include "ioconf.c"
638 #endif
639
640 static int
641 flash_modcmd(modcmd_t cmd, void *opaque)
642 {
643 int error = 0;
644 #ifdef _MODULE
645 int bmaj = -1, cmaj = -1;
646 #endif
647
648 switch (cmd) {
649 case MODULE_CMD_INIT:
650 #ifdef _MODULE
651 error = config_init_component(cfdriver_ioconf_flash,
652 cfattach_ioconf_flash, cfdata_ioconf_flash);
653 if (error)
654 return error;
655 error = devsw_attach("flash", &flash_bdevsw, &bmaj,
656 &flash_cdevsw, &cmaj);
657 if (error)
658 config_fini_component(cfdriver_ioconf_flash,
659 cfattach_ioconf_flash, cfdata_ioconf_flash);
660 #endif
661 return error;
662 case MODULE_CMD_FINI:
663 #ifdef _MODULE
664 devsw_detach(&flash_bdevsw, &flash_cdevsw);
665 error = config_fini_component(cfdriver_ioconf_flash,
666 cfattach_ioconf_flash, cfdata_ioconf_flash);
667 #endif
668 return error;
669 default:
670 return ENOTTY;
671 }
672 }
673