ld.c revision 1.100.4.2 1 /* $NetBSD: ld.c,v 1.100.4.2 2017/04/29 11:12:14 pgoyette Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Doran and Charles M. Hannum.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Disk driver for use by RAID controllers.
34 */
35
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: ld.c,v 1.100.4.2 2017/04/29 11:12:14 pgoyette Exp $");
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/device.h>
43 #include <sys/queue.h>
44 #include <sys/proc.h>
45 #include <sys/buf.h>
46 #include <sys/bufq.h>
47 #include <sys/endian.h>
48 #include <sys/disklabel.h>
49 #include <sys/disk.h>
50 #include <sys/dkio.h>
51 #include <sys/stat.h>
52 #include <sys/conf.h>
53 #include <sys/fcntl.h>
54 #include <sys/vnode.h>
55 #include <sys/syslog.h>
56 #include <sys/mutex.h>
57 #include <sys/module.h>
58 #include <sys/reboot.h>
59
60 #include <dev/ldvar.h>
61
62 static void ldminphys(struct buf *bp);
63 static bool ld_suspend(device_t, const pmf_qual_t *);
64 static bool ld_shutdown(device_t, int);
65 static int ld_diskstart(device_t, struct buf *bp);
66 static void ld_iosize(device_t, int *);
67 static int ld_dumpblocks(device_t, void *, daddr_t, int);
68 static void ld_fake_geometry(struct ld_softc *);
69 static void ld_set_geometry(struct ld_softc *);
70 static void ld_config_interrupts (device_t);
71 static int ld_lastclose(device_t);
72 static int ld_discard(device_t, off_t, off_t);
73 static int ld_flush(device_t, bool);
74
75 extern struct cfdriver ld_cd;
76
77 static dev_type_open(ldopen);
78 static dev_type_close(ldclose);
79 static dev_type_read(ldread);
80 static dev_type_write(ldwrite);
81 static dev_type_ioctl(ldioctl);
82 static dev_type_strategy(ldstrategy);
83 static dev_type_dump(lddump);
84 static dev_type_size(ldsize);
85 static dev_type_discard(lddiscard);
86
87 const struct bdevsw ld_bdevsw = {
88 DEVSW_MODULE_INIT
89 .d_open = ldopen,
90 .d_close = ldclose,
91 .d_strategy = ldstrategy,
92 .d_ioctl = ldioctl,
93 .d_dump = lddump,
94 .d_psize = ldsize,
95 .d_discard = lddiscard,
96 .d_flag = D_DISK | D_MPSAFE
97 };
98
99 const struct cdevsw ld_cdevsw = {
100 DEVSW_MODULE_INIT
101 .d_open = ldopen,
102 .d_close = ldclose,
103 .d_read = ldread,
104 .d_write = ldwrite,
105 .d_ioctl = ldioctl,
106 .d_stop = nostop,
107 .d_tty = notty,
108 .d_poll = nopoll,
109 .d_mmap = nommap,
110 .d_kqfilter = nokqfilter,
111 .d_discard = lddiscard,
112 .d_flag = D_DISK | D_MPSAFE
113 };
114
115 static struct dkdriver lddkdriver = {
116 .d_open = ldopen,
117 .d_close = ldclose,
118 .d_strategy = ldstrategy,
119 .d_iosize = ld_iosize,
120 .d_minphys = ldminphys,
121 .d_diskstart = ld_diskstart,
122 .d_dumpblocks = ld_dumpblocks,
123 .d_lastclose = ld_lastclose,
124 .d_discard = ld_discard
125 };
126
127 void
128 ldattach(struct ld_softc *sc, const char *default_strategy)
129 {
130 device_t self = sc->sc_dv;
131 struct dk_softc *dksc = &sc->sc_dksc;
132
133 mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_VM);
134 cv_init(&sc->sc_drain, "lddrain");
135
136 if ((sc->sc_flags & LDF_ENABLED) == 0) {
137 return;
138 }
139
140 /* Initialise dk and disk structure. */
141 dk_init(dksc, self, DKTYPE_LD);
142 disk_init(&dksc->sc_dkdev, dksc->sc_xname, &lddkdriver);
143
144 if (sc->sc_maxxfer > MAXPHYS)
145 sc->sc_maxxfer = MAXPHYS;
146
147 /* Build synthetic geometry if necessary. */
148 if (sc->sc_nheads == 0 || sc->sc_nsectors == 0 ||
149 sc->sc_ncylinders == 0)
150 ld_fake_geometry(sc);
151
152 sc->sc_disksize512 = sc->sc_secperunit * sc->sc_secsize / DEV_BSIZE;
153
154 /* Attach dk and disk subsystems */
155 dk_attach(dksc);
156 disk_attach(&dksc->sc_dkdev);
157 ld_set_geometry(sc);
158
159 bufq_alloc(&dksc->sc_bufq, default_strategy, BUFQ_SORT_RAWBLOCK);
160
161 /* Register with PMF */
162 if (!pmf_device_register1(dksc->sc_dev, ld_suspend, NULL, ld_shutdown))
163 aprint_error_dev(dksc->sc_dev,
164 "couldn't establish power handler\n");
165
166 /* Discover wedges on this disk. */
167 config_interrupts(sc->sc_dv, ld_config_interrupts);
168 }
169
170 int
171 ldadjqparam(struct ld_softc *sc, int xmax)
172 {
173
174 mutex_enter(&sc->sc_mutex);
175 sc->sc_maxqueuecnt = xmax;
176 mutex_exit(&sc->sc_mutex);
177
178 return (0);
179 }
180
181 int
182 ldbegindetach(struct ld_softc *sc, int flags)
183 {
184 struct dk_softc *dksc = &sc->sc_dksc;
185 int rv = 0;
186
187 if ((sc->sc_flags & LDF_ENABLED) == 0)
188 return (0);
189
190 rv = disk_begindetach(&dksc->sc_dkdev, ld_lastclose, dksc->sc_dev, flags);
191
192 if (rv != 0)
193 return rv;
194
195 mutex_enter(&sc->sc_mutex);
196 sc->sc_maxqueuecnt = 0;
197
198 while (sc->sc_queuecnt > 0) {
199 sc->sc_flags |= LDF_DRAIN;
200 cv_wait(&sc->sc_drain, &sc->sc_mutex);
201 }
202 mutex_exit(&sc->sc_mutex);
203
204 return (rv);
205 }
206
207 void
208 ldenddetach(struct ld_softc *sc)
209 {
210 struct dk_softc *dksc = &sc->sc_dksc;
211 int bmaj, cmaj, i, mn;
212
213 if ((sc->sc_flags & LDF_ENABLED) == 0)
214 return;
215
216 mutex_enter(&sc->sc_mutex);
217
218 /* Wait for commands queued with the hardware to complete. */
219 if (sc->sc_queuecnt != 0) {
220 if (cv_timedwait(&sc->sc_drain, &sc->sc_mutex, 30 * hz))
221 printf("%s: not drained\n", dksc->sc_xname);
222 }
223 mutex_exit(&sc->sc_mutex);
224
225 /* Kill off any queued buffers. */
226 dk_drain(dksc);
227 bufq_free(dksc->sc_bufq);
228
229 /* Locate the major numbers. */
230 bmaj = bdevsw_lookup_major(&ld_bdevsw);
231 cmaj = cdevsw_lookup_major(&ld_cdevsw);
232
233 /* Nuke the vnodes for any open instances. */
234 for (i = 0; i < MAXPARTITIONS; i++) {
235 mn = DISKMINOR(device_unit(dksc->sc_dev), i);
236 vdevgone(bmaj, mn, mn, VBLK);
237 vdevgone(cmaj, mn, mn, VCHR);
238 }
239
240 /* Delete all of our wedges. */
241 dkwedge_delall(&dksc->sc_dkdev);
242
243 /* Detach from the disk list. */
244 disk_detach(&dksc->sc_dkdev);
245 disk_destroy(&dksc->sc_dkdev);
246
247 dk_detach(dksc);
248
249 /* Deregister with PMF */
250 pmf_device_deregister(dksc->sc_dev);
251
252 /*
253 * XXX We can't really flush the cache here, because the
254 * XXX device may already be non-existent from the controller's
255 * XXX perspective.
256 */
257 #if 0
258 ld_flush(dksc->sc_dev, false);
259 #endif
260 cv_destroy(&sc->sc_drain);
261 mutex_destroy(&sc->sc_mutex);
262 }
263
264 /* ARGSUSED */
265 static bool
266 ld_suspend(device_t dev, const pmf_qual_t *qual)
267 {
268 return ld_shutdown(dev, 0);
269 }
270
271 /* ARGSUSED */
272 static bool
273 ld_shutdown(device_t dev, int flags)
274 {
275 if ((flags & RB_NOSYNC) == 0 && ld_flush(dev, true) != 0)
276 return false;
277
278 return true;
279 }
280
281 /* ARGSUSED */
282 static int
283 ldopen(dev_t dev, int flags, int fmt, struct lwp *l)
284 {
285 device_t self;
286 struct ld_softc *sc;
287 struct dk_softc *dksc;
288 int unit;
289 int error;
290
291 unit = DISKUNIT(dev);
292 self = device_lookup_acquire(&ld_cd, unit);
293 if (self == NULL)
294 return ENXIO;
295 sc = device_private(self);
296 dksc = &sc->sc_dksc;
297
298 error = dk_open(dksc, dev, flags, fmt, l);
299 device_release(self);
300 return error;
301 }
302
303 static int
304 ld_lastclose(device_t self)
305 {
306 ld_flush(self, false);
307
308 return 0;
309 }
310
311 /* ARGSUSED */
312 static int
313 ldclose(dev_t dev, int flags, int fmt, struct lwp *l)
314 {
315 device_t self;
316 struct ld_softc *sc;
317 struct dk_softc *dksc;
318 int unit;
319 int error;
320
321 unit = DISKUNIT(dev);
322 self = device_lookup_acquire(&ld_cd, unit);
323 if (self == NULL)
324 return ENXIO;
325 sc = device_private(self);
326 dksc = &sc->sc_dksc;
327
328 error = dk_close(dksc, dev, flags, fmt, l);
329 device_release(self);
330 return error;
331 }
332
333 /* ARGSUSED */
334 static int
335 ldread(dev_t dev, struct uio *uio, int ioflag)
336 {
337
338 return (physio(ldstrategy, NULL, dev, B_READ, ldminphys, uio));
339 }
340
341 /* ARGSUSED */
342 static int
343 ldwrite(dev_t dev, struct uio *uio, int ioflag)
344 {
345
346 return (physio(ldstrategy, NULL, dev, B_WRITE, ldminphys, uio));
347 }
348
349 /* ARGSUSED */
350 static int
351 ldioctl(dev_t dev, u_long cmd, void *addr, int32_t flag, struct lwp *l)
352 {
353 device_t self;
354 struct ld_softc *sc;
355 struct dk_softc *dksc;
356 int unit, error;
357
358 unit = DISKUNIT(dev);
359 self = device_lookup_acquire(&ld_cd, unit);
360 if (self == NULL)
361 return ENXIO;
362 sc = device_private(self);
363 dksc = &sc->sc_dksc;
364
365 error = dk_ioctl(dksc, dev, cmd, addr, flag, l);
366 if (error != EPASSTHROUGH) {
367 device_release(self);
368 return (error);
369 }
370
371 error = 0;
372
373 /*
374 * Some common checks so that individual attachments wouldn't need
375 * to duplicate them.
376 */
377 switch (cmd) {
378 case DIOCCACHESYNC:
379 /*
380 * XXX Do we really need to care about having a writable
381 * file descriptor here?
382 */
383 if ((flag & FWRITE) == 0)
384 error = EBADF;
385 else
386 error = 0;
387 break;
388 }
389
390 if (error != 0)
391 return (error);
392
393 if (sc->sc_ioctl) {
394 error = (*sc->sc_ioctl)(sc, cmd, addr, flag, 0);
395 if (error != EPASSTHROUGH)
396 return (error);
397 }
398
399 /* something not handled by the attachment */
400 return dk_ioctl(dksc, dev, cmd, addr, flag, l);
401 }
402
403 /*
404 * Flush the device's cache.
405 */
406 static int
407 ld_flush(device_t self, bool poll)
408 {
409 int error = 0;
410 struct ld_softc *sc = device_private(self);
411
412 if (sc->sc_ioctl) {
413 error = (*sc->sc_ioctl)(sc, DIOCCACHESYNC, NULL, 0, poll);
414 if (error != 0)
415 device_printf(self, "unable to flush cache\n");
416 }
417
418 return error;
419 }
420
421 static void
422 ldstrategy(struct buf *bp)
423 {
424 device_t self;
425 struct ld_softc *sc;
426 struct dk_softc *dksc;
427 int unit;
428
429 unit = DISKUNIT(bp->b_dev);
430 self = device_lookup_acquire(&ld_cd, unit);
431 if (self == NULL)
432 return;
433 sc = device_private(self);
434 dksc = &sc->sc_dksc;
435
436 dk_strategy(dksc, bp);
437 device_release(self);
438 }
439
440 static int
441 ld_diskstart(device_t dev, struct buf *bp)
442 {
443 struct ld_softc *sc;
444 int error;
445
446 device_acquire(dev);
447 sc = device_private(dev);
448 if (sc->sc_queuecnt >= sc->sc_maxqueuecnt) {
449 device_release(dev);
450 return EAGAIN;
451 }
452
453 mutex_enter(&sc->sc_mutex);
454
455 if (sc->sc_queuecnt >= sc->sc_maxqueuecnt)
456 error = EAGAIN;
457 else {
458 error = (*sc->sc_start)(sc, bp);
459 if (error == 0)
460 sc->sc_queuecnt++;
461 }
462
463 mutex_exit(&sc->sc_mutex);
464
465 device_release(dev);
466 return error;
467 }
468
469 void
470 lddone(struct ld_softc *sc, struct buf *bp)
471 {
472 struct dk_softc *dksc = &sc->sc_dksc;
473
474 dk_done(dksc, bp);
475
476 mutex_enter(&sc->sc_mutex);
477 if (--sc->sc_queuecnt <= sc->sc_maxqueuecnt) {
478 if ((sc->sc_flags & LDF_DRAIN) != 0) {
479 sc->sc_flags &= ~LDF_DRAIN;
480 cv_broadcast(&sc->sc_drain);
481 }
482 mutex_exit(&sc->sc_mutex);
483 dk_start(dksc, NULL);
484 } else
485 mutex_exit(&sc->sc_mutex);
486 }
487
488 static int
489 ldsize(dev_t dev)
490 {
491 device_t self;
492 struct ld_softc *sc;
493 struct dk_softc *dksc;
494 int unit;
495 int error;
496
497 unit = DISKUNIT(dev);
498 self = device_lookup_acquire(&ld_cd, unit);
499 if (self == NULL)
500 return (-1);
501 sc = device_private(self);
502 dksc = &sc->sc_dksc;
503
504 if ((sc->sc_flags & LDF_ENABLED) == 0)
505 return (-1);
506
507 return dk_size(dksc, dev);
508
509 device_release(self);
510 return error;
511 }
512
513 /*
514 * Take a dump.
515 */
516 static int
517 lddump(dev_t dev, daddr_t blkno, void *va, size_t size)
518 {
519 device_t self;
520 struct ld_softc *sc;
521 struct dk_softc *dksc;
522 int unit;
523 int error;
524
525 unit = DISKUNIT(dev);
526 self = device_lookup_acquire(&ld_cd, unit);
527 if (self == NULL)
528 return ENXIO;
529 sc = device_private(self);
530 dksc = &sc->sc_dksc;
531
532 if ((sc->sc_flags & LDF_ENABLED) == 0)
533 error = (ENODEV);
534 else
535 error = dk_dump(dksc, dev, blkno, va, size);
536
537 device_release(self);
538 return error;
539 }
540
541 static int
542 ld_dumpblocks(device_t dev, void *va, daddr_t blkno, int nblk)
543 {
544 struct ld_softc *sc;
545 int error;
546
547 device_acquire(dev);
548 sc = device_private(dev);
549 if (sc->sc_dump == NULL)
550 error = ENODEV;
551 else
552 error = (*sc->sc_dump)(sc, va, blkno, nblk);
553
554 device_release(dev);
555 return error;
556 }
557
558 /*
559 * Adjust the size of a transfer.
560 */
561 static void
562 ldminphys(struct buf *bp)
563 {
564 device_t self;
565 int unit;
566 struct ld_softc *sc;
567
568 unit = DISKUNIT(bp->b_dev);
569 self = device_lookup_acquire(&ld_cd, unit);
570 if (self == NULL)
571 return;
572 sc = device_private(self);
573
574 ld_iosize(sc->sc_dv, &bp->b_bcount);
575 minphys(bp);
576 device_release(self);
577 }
578
579 static void
580 ld_iosize(device_t d, int *countp)
581 {
582 struct ld_softc *sc;
583
584 device_acquire(d);
585 sc = device_private(d);
586
587 if (*countp > sc->sc_maxxfer)
588 *countp = sc->sc_maxxfer;
589
590 device_release(d);
591 }
592
593 static void
594 ld_fake_geometry(struct ld_softc *sc)
595 {
596 uint64_t ncyl;
597
598 if (sc->sc_secperunit <= 528 * 2048) /* 528MB */
599 sc->sc_nheads = 16;
600 else if (sc->sc_secperunit <= 1024 * 2048) /* 1GB */
601 sc->sc_nheads = 32;
602 else if (sc->sc_secperunit <= 21504 * 2048) /* 21GB */
603 sc->sc_nheads = 64;
604 else if (sc->sc_secperunit <= 43008 * 2048) /* 42GB */
605 sc->sc_nheads = 128;
606 else
607 sc->sc_nheads = 255;
608
609 sc->sc_nsectors = 63;
610 sc->sc_ncylinders = INT_MAX;
611 ncyl = sc->sc_secperunit /
612 (sc->sc_nheads * sc->sc_nsectors);
613 if (ncyl < INT_MAX)
614 sc->sc_ncylinders = (int)ncyl;
615 }
616
617 static void
618 ld_set_geometry(struct ld_softc *sc)
619 {
620 struct dk_softc *dksc = &sc->sc_dksc;
621 struct disk_geom *dg = &dksc->sc_dkdev.dk_geom;
622 char tbuf[9];
623
624 format_bytes(tbuf, sizeof(tbuf), sc->sc_secperunit *
625 sc->sc_secsize);
626 aprint_normal_dev(dksc->sc_dev, "%s, %d cyl, %d head, %d sec, "
627 "%d bytes/sect x %"PRIu64" sectors\n",
628 tbuf, sc->sc_ncylinders, sc->sc_nheads,
629 sc->sc_nsectors, sc->sc_secsize, sc->sc_secperunit);
630
631 memset(dg, 0, sizeof(*dg));
632 dg->dg_secperunit = sc->sc_secperunit;
633 dg->dg_secsize = sc->sc_secsize;
634 dg->dg_nsectors = sc->sc_nsectors;
635 dg->dg_ntracks = sc->sc_nheads;
636 dg->dg_ncylinders = sc->sc_ncylinders;
637
638 disk_set_info(dksc->sc_dev, &dksc->sc_dkdev, NULL);
639 }
640
641 static void
642 ld_config_interrupts(device_t d)
643 {
644 struct ld_softc *sc;
645 struct dk_softc *dksc;
646
647 device_acquire(d);
648 sc = device_private(d);
649 dksc = &sc->sc_dksc;
650 dkwedge_discover(&dksc->sc_dkdev);
651 device_release(d);
652 }
653
654 static int
655 ld_discard(device_t dev, off_t pos, off_t len)
656 {
657 struct ld_softc *sc;
658 int error;
659
660 device_acquire(dev);
661 sc = device_private(dev);
662 if (sc->sc_discard == NULL)
663 error = (ENODEV);
664 else
665 error = (*sc->sc_discard)(sc, pos, len);
666 device_release(dev);
667 return error;
668 }
669
670 static int
671 lddiscard(dev_t dev, off_t pos, off_t len)
672 {
673 device_t self;
674 struct ld_softc *sc;
675 struct dk_softc *dksc;
676 int unit;
677 int error;
678
679 unit = DISKUNIT(dev);
680 self = device_lookup_acquire(&ld_cd, unit);
681 if (self == NULL)
682 return ENXIO;
683 sc = device_private(self);
684 dksc = &sc->sc_dksc;
685
686 error = dk_discard(dksc, dev, pos, len);
687 device_release(self);
688 return error;
689 }
690
691 MODULE(MODULE_CLASS_DRIVER, ld, "dk_subr");
692
693 #ifdef _MODULE
694 CFDRIVER_DECL(ld, DV_DISK, NULL);
695 #endif
696
697 static int
698 ld_modcmd(modcmd_t cmd, void *opaque)
699 {
700 #ifdef _MODULE
701 devmajor_t bmajor, cmajor;
702 #endif
703 int error = 0;
704
705 #ifdef _MODULE
706 switch (cmd) {
707 case MODULE_CMD_INIT:
708 bmajor = cmajor = -1;
709 error = devsw_attach(ld_cd.cd_name, &ld_bdevsw, &bmajor,
710 &ld_cdevsw, &cmajor);
711 if (error)
712 break;
713 error = config_cfdriver_attach(&ld_cd);
714 break;
715 case MODULE_CMD_FINI:
716 error = config_cfdriver_detach(&ld_cd);
717 if (error)
718 break;
719 devsw_detach(&ld_bdevsw, &ld_cdevsw);
720 break;
721 default:
722 error = ENOTTY;
723 break;
724 }
725 #endif
726
727 return error;
728 }
729