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