Home | History | Annotate | Line # | Download | only in dev
ld.c revision 1.92
      1  1.92   mlelstv /*	$NetBSD: ld.c,v 1.92 2015/08/27 05:51:50 mlelstv 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.92   mlelstv __KERNEL_RCSID(0, "$NetBSD: ld.c,v 1.92 2015/08/27 05:51:50 mlelstv 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.82  riastrad #include <sys/rndsource.h>
     58   1.1        ad 
     59   1.1        ad #include <dev/ldvar.h>
     60   1.1        ad 
     61  1.43       riz #include <prop/proplib.h>
     62  1.43       riz 
     63   1.1        ad static void	ldminphys(struct buf *bp);
     64  1.67  jmcneill static bool	ld_suspend(device_t, const pmf_qual_t *);
     65  1.55  jmcneill static bool	ld_shutdown(device_t, int);
     66  1.89   mlelstv static int	ld_diskstart(device_t, struct buf *bp);
     67  1.83   mlelstv static void	ld_iosize(device_t, int *);
     68  1.83   mlelstv static int	ld_dumpblocks(device_t, void *, daddr_t, int);
     69  1.83   mlelstv static void	ld_fake_geometry(struct ld_softc *);
     70  1.71  christos static void	ld_set_geometry(struct ld_softc *);
     71  1.65    cegger static void	ld_config_interrupts (device_t);
     72  1.83   mlelstv static int	ld_lastclose(device_t);
     73  1.90  jakllsch static int	ld_discard(device_t, off_t, off_t);
     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.1        ad ldattach(struct ld_softc *sc)
    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.83   mlelstv 	/* Attach the device into the rnd source list. */
    143  1.83   mlelstv 	rnd_attach_source(&sc->sc_rnd_source, dksc->sc_xname,
    144  1.83   mlelstv 	    RND_TYPE_DISK, RND_FLAG_DEFAULT);
    145   1.1        ad 
    146   1.1        ad 	if (sc->sc_maxxfer > MAXPHYS)
    147   1.1        ad 		sc->sc_maxxfer = MAXPHYS;
    148   1.9        ad 
    149  1.19   thorpej 	/* Build synthetic geometry if necessary. */
    150  1.19   thorpej 	if (sc->sc_nheads == 0 || sc->sc_nsectors == 0 ||
    151  1.83   mlelstv 	    sc->sc_ncylinders == 0)
    152  1.83   mlelstv 	    ld_fake_geometry(sc);
    153  1.19   thorpej 
    154  1.68  kiyohara 	sc->sc_disksize512 = sc->sc_secperunit * sc->sc_secsize / DEV_BSIZE;
    155   1.1        ad 
    156  1.83   mlelstv 	/* Attach dk and disk subsystems */
    157  1.83   mlelstv 	dk_attach(dksc);
    158  1.83   mlelstv 	disk_attach(&dksc->sc_dkdev);
    159  1.71  christos 	ld_set_geometry(sc);
    160  1.43       riz 
    161  1.83   mlelstv 	bufq_alloc(&dksc->sc_bufq, BUFQ_DISK_DEFAULT_STRAT, BUFQ_SORT_RAWBLOCK);
    162   1.1        ad 
    163  1.55  jmcneill 	/* Register with PMF */
    164  1.83   mlelstv 	if (!pmf_device_register1(dksc->sc_dev, ld_suspend, NULL, ld_shutdown))
    165  1.83   mlelstv 		aprint_error_dev(dksc->sc_dev,
    166  1.55  jmcneill 		    "couldn't establish power handler\n");
    167  1.55  jmcneill 
    168  1.30   thorpej 	/* Discover wedges on this disk. */
    169  1.63      tron 	config_interrupts(sc->sc_dv, ld_config_interrupts);
    170   1.1        ad }
    171   1.1        ad 
    172   1.3        ad int
    173  1.37  christos ldadjqparam(struct ld_softc *sc, int xmax)
    174   1.3        ad {
    175   1.7        ad 
    176  1.85   mlelstv 	mutex_enter(&sc->sc_mutex);
    177  1.37  christos 	sc->sc_maxqueuecnt = xmax;
    178  1.85   mlelstv 	mutex_exit(&sc->sc_mutex);
    179   1.7        ad 
    180  1.24   thorpej 	return (0);
    181   1.7        ad }
    182   1.7        ad 
    183   1.7        ad int
    184   1.7        ad ldbegindetach(struct ld_softc *sc, int flags)
    185   1.7        ad {
    186  1.83   mlelstv 	struct dk_softc *dksc = &sc->sc_dksc;
    187  1.85   mlelstv 	int rv = 0;
    188   1.7        ad 
    189   1.7        ad 	if ((sc->sc_flags & LDF_ENABLED) == 0)
    190   1.7        ad 		return (0);
    191   1.3        ad 
    192  1.83   mlelstv 	rv = disk_begindetach(&dksc->sc_dkdev, ld_lastclose, dksc->sc_dev, flags);
    193  1.66    dyoung 
    194  1.66    dyoung 	if (rv != 0)
    195  1.66    dyoung 		return rv;
    196   1.3        ad 
    197  1.85   mlelstv 	mutex_enter(&sc->sc_mutex);
    198  1.24   thorpej 	sc->sc_maxqueuecnt = 0;
    199  1.83   mlelstv 
    200  1.24   thorpej 	while (sc->sc_queuecnt > 0) {
    201  1.24   thorpej 		sc->sc_flags |= LDF_DRAIN;
    202  1.85   mlelstv 		cv_wait(&sc->sc_drain, &sc->sc_mutex);
    203  1.24   thorpej 	}
    204  1.85   mlelstv 	mutex_exit(&sc->sc_mutex);
    205   1.7        ad 
    206   1.7        ad 	return (rv);
    207   1.3        ad }
    208   1.3        ad 
    209   1.2        ad void
    210   1.7        ad ldenddetach(struct ld_softc *sc)
    211   1.2        ad {
    212  1.83   mlelstv 	struct dk_softc *dksc = &sc->sc_dksc;
    213  1.85   mlelstv 	int bmaj, cmaj, i, mn;
    214   1.2        ad 
    215   1.7        ad 	if ((sc->sc_flags & LDF_ENABLED) == 0)
    216   1.7        ad 		return;
    217   1.7        ad 
    218  1.85   mlelstv 	mutex_enter(&sc->sc_mutex);
    219  1.85   mlelstv 
    220   1.2        ad 	/* Wait for commands queued with the hardware to complete. */
    221  1.86   mlelstv 	if (sc->sc_queuecnt != 0) {
    222  1.86   mlelstv 		if (cv_timedwait(&sc->sc_drain, &sc->sc_mutex, 30 * hz))
    223  1.83   mlelstv 			printf("%s: not drained\n", dksc->sc_xname);
    224  1.86   mlelstv 	}
    225  1.92   mlelstv 	mutex_exit(&sc->sc_mutex);
    226   1.2        ad 
    227   1.2        ad 	/* Kill off any queued buffers. */
    228  1.92   mlelstv 	dk_drain(dksc);
    229  1.83   mlelstv 	bufq_free(dksc->sc_bufq);
    230   1.2        ad 
    231  1.85   mlelstv 	/* Locate the major numbers. */
    232  1.85   mlelstv 	bmaj = bdevsw_lookup_major(&ld_bdevsw);
    233  1.85   mlelstv 	cmaj = cdevsw_lookup_major(&ld_cdevsw);
    234  1.85   mlelstv 
    235   1.2        ad 	/* Nuke the vnodes for any open instances. */
    236  1.13  drochner 	for (i = 0; i < MAXPARTITIONS; i++) {
    237  1.83   mlelstv 		mn = DISKMINOR(device_unit(dksc->sc_dev), i);
    238  1.13  drochner 		vdevgone(bmaj, mn, mn, VBLK);
    239  1.13  drochner 		vdevgone(cmaj, mn, mn, VCHR);
    240  1.13  drochner 	}
    241  1.13  drochner 
    242  1.30   thorpej 	/* Delete all of our wedges. */
    243  1.83   mlelstv 	dkwedge_delall(&dksc->sc_dkdev);
    244  1.30   thorpej 
    245   1.2        ad 	/* Detach from the disk list. */
    246  1.83   mlelstv 	disk_detach(&dksc->sc_dkdev);
    247  1.83   mlelstv 	disk_destroy(&dksc->sc_dkdev);
    248   1.2        ad 
    249  1.92   mlelstv 	dk_detach(dksc);
    250  1.92   mlelstv 
    251   1.2        ad 	/* Unhook the entropy source. */
    252   1.2        ad 	rnd_detach_source(&sc->sc_rnd_source);
    253   1.2        ad 
    254  1.56  jmcneill 	/* Deregister with PMF */
    255  1.83   mlelstv 	pmf_device_deregister(dksc->sc_dev);
    256  1.56  jmcneill 
    257  1.24   thorpej 	/*
    258  1.24   thorpej 	 * XXX We can't really flush the cache here, beceause the
    259  1.24   thorpej 	 * XXX device may already be non-existent from the controller's
    260  1.24   thorpej 	 * XXX perspective.
    261  1.24   thorpej 	 */
    262  1.24   thorpej #if 0
    263   1.2        ad 	/* Flush the device's cache. */
    264   1.2        ad 	if (sc->sc_flush != NULL)
    265  1.62    simonb 		if ((*sc->sc_flush)(sc, 0) != 0)
    266  1.87   mlelstv 			device_printf(dksc->sc_dev, "unable to flush cache\n");
    267  1.24   thorpej #endif
    268  1.85   mlelstv 	cv_destroy(&sc->sc_drain);
    269  1.61        ws 	mutex_destroy(&sc->sc_mutex);
    270   1.2        ad }
    271   1.2        ad 
    272   1.8     lukem /* ARGSUSED */
    273  1.55  jmcneill static bool
    274  1.67  jmcneill ld_suspend(device_t dev, const pmf_qual_t *qual)
    275  1.67  jmcneill {
    276  1.67  jmcneill 	return ld_shutdown(dev, 0);
    277  1.67  jmcneill }
    278  1.67  jmcneill 
    279  1.67  jmcneill /* ARGSUSED */
    280  1.67  jmcneill static bool
    281  1.55  jmcneill ld_shutdown(device_t dev, int flags)
    282   1.1        ad {
    283  1.55  jmcneill 	struct ld_softc *sc = device_private(dev);
    284  1.83   mlelstv 	struct dk_softc *dksc = &sc->sc_dksc;
    285   1.1        ad 
    286  1.62    simonb 	if (sc->sc_flush != NULL && (*sc->sc_flush)(sc, LDFL_POLL) != 0) {
    287  1.87   mlelstv 		device_printf(dksc->sc_dev, "unable to flush cache\n");
    288  1.55  jmcneill 		return false;
    289   1.1        ad 	}
    290  1.55  jmcneill 
    291  1.55  jmcneill 	return true;
    292   1.1        ad }
    293   1.1        ad 
    294   1.8     lukem /* ARGSUSED */
    295  1.29   thorpej static int
    296  1.42  christos ldopen(dev_t dev, int flags, int fmt, struct lwp *l)
    297   1.1        ad {
    298   1.1        ad 	struct ld_softc *sc;
    299  1.83   mlelstv 	struct dk_softc *dksc;
    300  1.83   mlelstv 	int unit;
    301   1.1        ad 
    302   1.1        ad 	unit = DISKUNIT(dev);
    303  1.59   tsutsui 	if ((sc = device_lookup_private(&ld_cd, unit)) == NULL)
    304   1.1        ad 		return (ENXIO);
    305  1.83   mlelstv 	dksc = &sc->sc_dksc;
    306   1.1        ad 
    307  1.83   mlelstv 	return dk_open(dksc, dev, flags, fmt, l);
    308   1.1        ad }
    309   1.1        ad 
    310  1.66    dyoung static int
    311  1.83   mlelstv ld_lastclose(device_t self)
    312  1.66    dyoung {
    313  1.66    dyoung 	struct ld_softc *sc = device_private(self);
    314  1.84     skrll 
    315  1.66    dyoung 	if (sc->sc_flush != NULL && (*sc->sc_flush)(sc, 0) != 0)
    316  1.87   mlelstv 		device_printf(self, "unable to flush cache\n");
    317  1.84     skrll 
    318  1.66    dyoung 	return 0;
    319  1.84     skrll }
    320  1.66    dyoung 
    321   1.8     lukem /* ARGSUSED */
    322  1.29   thorpej static int
    323  1.42  christos ldclose(dev_t dev, int flags, int fmt, struct lwp *l)
    324   1.1        ad {
    325   1.1        ad 	struct ld_softc *sc;
    326  1.83   mlelstv 	struct dk_softc *dksc;
    327  1.83   mlelstv 	int unit;
    328   1.1        ad 
    329   1.1        ad 	unit = DISKUNIT(dev);
    330  1.59   tsutsui 	sc = device_lookup_private(&ld_cd, unit);
    331  1.83   mlelstv 	dksc = &sc->sc_dksc;
    332  1.30   thorpej 
    333  1.83   mlelstv 	return dk_close(dksc, dev, flags, fmt, l);
    334   1.1        ad }
    335   1.1        ad 
    336   1.8     lukem /* ARGSUSED */
    337  1.29   thorpej static int
    338  1.42  christos ldread(dev_t dev, struct uio *uio, int ioflag)
    339   1.1        ad {
    340   1.1        ad 
    341   1.1        ad 	return (physio(ldstrategy, NULL, dev, B_READ, ldminphys, uio));
    342   1.1        ad }
    343   1.1        ad 
    344   1.8     lukem /* ARGSUSED */
    345  1.29   thorpej static int
    346  1.42  christos ldwrite(dev_t dev, struct uio *uio, int ioflag)
    347   1.1        ad {
    348   1.1        ad 
    349   1.1        ad 	return (physio(ldstrategy, NULL, dev, B_WRITE, ldminphys, uio));
    350   1.1        ad }
    351   1.1        ad 
    352   1.8     lukem /* ARGSUSED */
    353  1.29   thorpej static int
    354  1.46  christos ldioctl(dev_t dev, u_long cmd, void *addr, int32_t flag, struct lwp *l)
    355   1.1        ad {
    356   1.1        ad 	struct ld_softc *sc;
    357  1.83   mlelstv 	struct dk_softc *dksc;
    358  1.80  christos 	int unit, error;
    359   1.1        ad 
    360   1.1        ad 	unit = DISKUNIT(dev);
    361  1.59   tsutsui 	sc = device_lookup_private(&ld_cd, unit);
    362  1.83   mlelstv 	dksc = &sc->sc_dksc;
    363   1.1        ad 
    364  1.83   mlelstv 	error = disk_ioctl(&dksc->sc_dkdev, dev, cmd, addr, flag, l);
    365  1.83   mlelstv 	if (error != EPASSTHROUGH)
    366  1.83   mlelstv 		return (error);
    367  1.83   mlelstv 
    368  1.83   mlelstv 	error = dk_ioctl(dksc, dev, cmd, addr, flag, l);
    369  1.43       riz 	if (error != EPASSTHROUGH)
    370  1.43       riz 		return (error);
    371  1.43       riz 
    372  1.47      tron 	error = 0;
    373  1.83   mlelstv 
    374   1.1        ad 	switch (cmd) {
    375  1.32   thorpej 	case DIOCCACHESYNC:
    376  1.32   thorpej 		/*
    377  1.32   thorpej 		 * XXX Do we really need to care about having a writable
    378  1.32   thorpej 		 * file descriptor here?
    379  1.32   thorpej 		 */
    380  1.32   thorpej 		if ((flag & FWRITE) == 0)
    381  1.32   thorpej 			error = EBADF;
    382  1.32   thorpej 		else if (sc->sc_flush)
    383  1.62    simonb 			error = (*sc->sc_flush)(sc, 0);
    384  1.32   thorpej 		else
    385  1.32   thorpej 			error = 0;	/* XXX Error out instead? */
    386  1.32   thorpej 		break;
    387   1.1        ad 	default:
    388   1.1        ad 		error = ENOTTY;
    389   1.1        ad 		break;
    390   1.1        ad 	}
    391   1.1        ad 
    392   1.1        ad 	return (error);
    393   1.1        ad }
    394   1.1        ad 
    395  1.29   thorpej static void
    396   1.1        ad ldstrategy(struct buf *bp)
    397   1.1        ad {
    398   1.1        ad 	struct ld_softc *sc;
    399  1.83   mlelstv 	struct dk_softc *dksc;
    400  1.83   mlelstv 	int unit;
    401   1.2        ad 
    402  1.83   mlelstv 	unit = DISKUNIT(bp->b_dev);
    403  1.83   mlelstv 	sc = device_lookup_private(&ld_cd, unit);
    404  1.83   mlelstv 	dksc = &sc->sc_dksc;
    405  1.23   thorpej 
    406  1.83   mlelstv 	return dk_strategy(dksc, bp);
    407  1.23   thorpej }
    408  1.23   thorpej 
    409  1.89   mlelstv static int
    410  1.89   mlelstv ld_diskstart(device_t dev, struct buf *bp)
    411  1.23   thorpej {
    412  1.83   mlelstv 	struct ld_softc *sc = device_private(dev);
    413  1.23   thorpej 	int error;
    414   1.1        ad 
    415  1.89   mlelstv 	if (sc->sc_queuecnt >= sc->sc_maxqueuecnt)
    416  1.89   mlelstv 		return EAGAIN;
    417  1.89   mlelstv 
    418  1.44        ad 	mutex_enter(&sc->sc_mutex);
    419  1.44        ad 
    420  1.89   mlelstv 	if (sc->sc_queuecnt >= sc->sc_maxqueuecnt)
    421  1.89   mlelstv 		error = EAGAIN;
    422  1.89   mlelstv 	else {
    423  1.89   mlelstv 		error = (*sc->sc_start)(sc, bp);
    424  1.89   mlelstv 		if (error == 0)
    425  1.89   mlelstv 			sc->sc_queuecnt++;
    426   1.1        ad 	}
    427  1.44        ad 
    428  1.44        ad 	mutex_exit(&sc->sc_mutex);
    429  1.89   mlelstv 
    430  1.89   mlelstv 	return error;
    431   1.1        ad }
    432   1.1        ad 
    433   1.1        ad void
    434   1.1        ad lddone(struct ld_softc *sc, struct buf *bp)
    435   1.1        ad {
    436  1.83   mlelstv 	struct dk_softc *dksc = &sc->sc_dksc;
    437   1.1        ad 
    438  1.83   mlelstv 	dk_done(dksc, bp);
    439   1.1        ad 
    440  1.44        ad 	mutex_enter(&sc->sc_mutex);
    441   1.7        ad 	if (--sc->sc_queuecnt <= sc->sc_maxqueuecnt) {
    442  1.24   thorpej 		if ((sc->sc_flags & LDF_DRAIN) != 0) {
    443  1.24   thorpej 			sc->sc_flags &= ~LDF_DRAIN;
    444  1.88   mlelstv 			cv_broadcast(&sc->sc_drain);
    445  1.24   thorpej 		}
    446  1.44        ad 		mutex_exit(&sc->sc_mutex);
    447  1.89   mlelstv 		dk_start(dksc, NULL);
    448  1.44        ad 	} else
    449  1.44        ad 		mutex_exit(&sc->sc_mutex);
    450   1.1        ad }
    451   1.1        ad 
    452  1.29   thorpej static int
    453   1.1        ad ldsize(dev_t dev)
    454   1.1        ad {
    455   1.1        ad 	struct ld_softc *sc;
    456  1.83   mlelstv 	struct dk_softc *dksc;
    457  1.83   mlelstv 	int unit;
    458   1.1        ad 
    459   1.1        ad 	unit = DISKUNIT(dev);
    460  1.59   tsutsui 	if ((sc = device_lookup_private(&ld_cd, unit)) == NULL)
    461   1.1        ad 		return (ENODEV);
    462  1.83   mlelstv 	dksc = &sc->sc_dksc;
    463  1.83   mlelstv 
    464   1.1        ad 	if ((sc->sc_flags & LDF_ENABLED) == 0)
    465   1.1        ad 		return (ENODEV);
    466   1.1        ad 
    467  1.83   mlelstv 	return dk_size(dksc, dev);
    468   1.1        ad }
    469   1.1        ad 
    470   1.1        ad /*
    471   1.1        ad  * Take a dump.
    472   1.1        ad  */
    473  1.29   thorpej static int
    474  1.83   mlelstv lddump(dev_t dev, daddr_t blkno, void *va, size_t size)
    475   1.1        ad {
    476   1.1        ad 	struct ld_softc *sc;
    477  1.83   mlelstv 	struct dk_softc *dksc;
    478  1.83   mlelstv 	int unit;
    479   1.1        ad 
    480   1.1        ad 	unit = DISKUNIT(dev);
    481  1.59   tsutsui 	if ((sc = device_lookup_private(&ld_cd, unit)) == NULL)
    482   1.1        ad 		return (ENXIO);
    483  1.83   mlelstv 	dksc = &sc->sc_dksc;
    484  1.83   mlelstv 
    485   1.1        ad 	if ((sc->sc_flags & LDF_ENABLED) == 0)
    486   1.1        ad 		return (ENODEV);
    487  1.83   mlelstv 
    488  1.83   mlelstv 	return dk_dump(dksc, dev, blkno, va, size);
    489  1.83   mlelstv }
    490  1.83   mlelstv 
    491  1.83   mlelstv static int
    492  1.83   mlelstv ld_dumpblocks(device_t dev, void *va, daddr_t blkno, int nblk)
    493  1.83   mlelstv {
    494  1.83   mlelstv 	struct ld_softc *sc = device_private(dev);
    495  1.84     skrll 
    496   1.3        ad 	if (sc->sc_dump == NULL)
    497  1.91   mlelstv 		return (ENODEV);
    498   1.3        ad 
    499  1.83   mlelstv 	return (*sc->sc_dump)(sc, va, blkno, nblk);
    500   1.1        ad }
    501   1.1        ad 
    502   1.1        ad /*
    503   1.1        ad  * Adjust the size of a transfer.
    504   1.1        ad  */
    505   1.1        ad static void
    506   1.1        ad ldminphys(struct buf *bp)
    507   1.1        ad {
    508  1.83   mlelstv 	int unit;
    509   1.1        ad 	struct ld_softc *sc;
    510   1.1        ad 
    511  1.83   mlelstv 	unit = DISKUNIT(bp->b_dev);
    512  1.83   mlelstv 	sc = device_lookup_private(&ld_cd, unit);
    513   1.1        ad 
    514  1.83   mlelstv 	ld_iosize(sc->sc_dv, &bp->b_bcount);
    515   1.1        ad 	minphys(bp);
    516   1.1        ad }
    517  1.43       riz 
    518  1.43       riz static void
    519  1.83   mlelstv ld_iosize(device_t d, int *countp)
    520  1.83   mlelstv {
    521  1.83   mlelstv 	struct ld_softc *sc = device_private(d);
    522  1.83   mlelstv 
    523  1.83   mlelstv 	if (*countp > sc->sc_maxxfer)
    524  1.83   mlelstv 		*countp = sc->sc_maxxfer;
    525  1.83   mlelstv }
    526  1.83   mlelstv 
    527  1.83   mlelstv static void
    528  1.83   mlelstv ld_fake_geometry(struct ld_softc *sc)
    529  1.83   mlelstv {
    530  1.83   mlelstv 	uint64_t ncyl;
    531  1.83   mlelstv 
    532  1.83   mlelstv 	if (sc->sc_secperunit <= 528 * 2048)		/* 528MB */
    533  1.83   mlelstv 		sc->sc_nheads = 16;
    534  1.83   mlelstv 	else if (sc->sc_secperunit <= 1024 * 2048)	/* 1GB */
    535  1.83   mlelstv 		sc->sc_nheads = 32;
    536  1.83   mlelstv 	else if (sc->sc_secperunit <= 21504 * 2048)	/* 21GB */
    537  1.83   mlelstv 		sc->sc_nheads = 64;
    538  1.83   mlelstv 	else if (sc->sc_secperunit <= 43008 * 2048)	/* 42GB */
    539  1.83   mlelstv 		sc->sc_nheads = 128;
    540  1.83   mlelstv 	else
    541  1.83   mlelstv 		sc->sc_nheads = 255;
    542  1.83   mlelstv 
    543  1.83   mlelstv 	sc->sc_nsectors = 63;
    544  1.83   mlelstv 	sc->sc_ncylinders = INT_MAX;
    545  1.83   mlelstv 	ncyl = sc->sc_secperunit /
    546  1.83   mlelstv 	    (sc->sc_nheads * sc->sc_nsectors);
    547  1.83   mlelstv 	if (ncyl < INT_MAX)
    548  1.83   mlelstv 		sc->sc_ncylinders = (int)ncyl;
    549  1.83   mlelstv }
    550  1.83   mlelstv 
    551  1.83   mlelstv static void
    552  1.83   mlelstv ld_set_geometry(struct ld_softc *sc)
    553  1.43       riz {
    554  1.83   mlelstv 	struct dk_softc *dksc = &sc->sc_dksc;
    555  1.83   mlelstv 	struct disk_geom *dg = &dksc->sc_dkdev.dk_geom;
    556  1.83   mlelstv 	char tbuf[9];
    557  1.83   mlelstv 
    558  1.83   mlelstv 	format_bytes(tbuf, sizeof(tbuf), sc->sc_secperunit *
    559  1.83   mlelstv 	    sc->sc_secsize);
    560  1.83   mlelstv 	aprint_normal_dev(dksc->sc_dev, "%s, %d cyl, %d head, %d sec, "
    561  1.83   mlelstv 	    "%d bytes/sect x %"PRIu64" sectors\n",
    562  1.83   mlelstv 	    tbuf, sc->sc_ncylinders, sc->sc_nheads,
    563  1.83   mlelstv 	    sc->sc_nsectors, sc->sc_secsize, sc->sc_secperunit);
    564  1.43       riz 
    565  1.71  christos 	memset(dg, 0, sizeof(*dg));
    566  1.83   mlelstv 	dg->dg_secperunit = sc->sc_secperunit;
    567  1.83   mlelstv 	dg->dg_secsize = sc->sc_secsize;
    568  1.83   mlelstv 	dg->dg_nsectors = sc->sc_nsectors;
    569  1.83   mlelstv 	dg->dg_ntracks = sc->sc_nheads;
    570  1.83   mlelstv 	dg->dg_ncylinders = sc->sc_ncylinders;
    571  1.43       riz 
    572  1.83   mlelstv 	disk_set_info(dksc->sc_dev, &dksc->sc_dkdev, NULL);
    573  1.43       riz }
    574  1.45       riz 
    575  1.45       riz static void
    576  1.65    cegger ld_config_interrupts(device_t d)
    577  1.45       riz {
    578  1.60      cube 	struct ld_softc *sc = device_private(d);
    579  1.83   mlelstv 	struct dk_softc *dksc = &sc->sc_dksc;
    580  1.83   mlelstv 
    581  1.83   mlelstv 	dkwedge_discover(&dksc->sc_dkdev);
    582  1.45       riz }
    583  1.90  jakllsch 
    584  1.90  jakllsch static int
    585  1.90  jakllsch ld_discard(device_t dev, off_t pos, off_t len)
    586  1.90  jakllsch {
    587  1.90  jakllsch 	struct ld_softc *sc = device_private(dev);
    588  1.90  jakllsch 
    589  1.90  jakllsch 	if (sc->sc_discard == NULL)
    590  1.91   mlelstv 		return (ENODEV);
    591  1.90  jakllsch 
    592  1.90  jakllsch 	return (*sc->sc_discard)(sc, pos, len);
    593  1.90  jakllsch }
    594  1.90  jakllsch 
    595  1.90  jakllsch static int
    596  1.90  jakllsch lddiscard(dev_t dev, off_t pos, off_t len)
    597  1.90  jakllsch {
    598  1.90  jakllsch 	struct ld_softc *sc;
    599  1.90  jakllsch 	struct dk_softc *dksc;
    600  1.90  jakllsch 	int unit;
    601  1.90  jakllsch 
    602  1.90  jakllsch 	unit = DISKUNIT(dev);
    603  1.90  jakllsch 	sc = device_lookup_private(&ld_cd, unit);
    604  1.90  jakllsch 	dksc = &sc->sc_dksc;
    605  1.90  jakllsch 
    606  1.90  jakllsch 	return dk_discard(dksc, dev, pos, len);
    607  1.90  jakllsch }
    608