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