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