Home | History | Annotate | Line # | Download | only in dev
ld.c revision 1.83
      1  1.83   mlelstv /*	$NetBSD: ld.c,v 1.83 2015/05/02 08:00:08 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.83   mlelstv __KERNEL_RCSID(0, "$NetBSD: ld.c,v 1.83 2015/05/02 08:00:08 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.83   mlelstv static void	ld_start(device_t);
     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.1        ad 
     74   1.1        ad extern struct	cfdriver ld_cd;
     75   1.1        ad 
     76  1.29   thorpej static dev_type_open(ldopen);
     77  1.29   thorpej static dev_type_close(ldclose);
     78  1.29   thorpej static dev_type_read(ldread);
     79  1.29   thorpej static dev_type_write(ldwrite);
     80  1.29   thorpej static dev_type_ioctl(ldioctl);
     81  1.29   thorpej static dev_type_strategy(ldstrategy);
     82  1.29   thorpej static dev_type_dump(lddump);
     83  1.29   thorpej static dev_type_size(ldsize);
     84  1.16   gehenna 
     85  1.16   gehenna const struct bdevsw ld_bdevsw = {
     86  1.72  dholland 	.d_open = ldopen,
     87  1.72  dholland 	.d_close = ldclose,
     88  1.72  dholland 	.d_strategy = ldstrategy,
     89  1.72  dholland 	.d_ioctl = ldioctl,
     90  1.72  dholland 	.d_dump = lddump,
     91  1.72  dholland 	.d_psize = ldsize,
     92  1.73  dholland 	.d_discard = nodiscard,
     93  1.72  dholland 	.d_flag = D_DISK
     94  1.16   gehenna };
     95  1.16   gehenna 
     96  1.16   gehenna const struct cdevsw ld_cdevsw = {
     97  1.72  dholland 	.d_open = ldopen,
     98  1.72  dholland 	.d_close = ldclose,
     99  1.72  dholland 	.d_read = ldread,
    100  1.72  dholland 	.d_write = ldwrite,
    101  1.72  dholland 	.d_ioctl = ldioctl,
    102  1.72  dholland 	.d_stop = nostop,
    103  1.72  dholland 	.d_tty = notty,
    104  1.72  dholland 	.d_poll = nopoll,
    105  1.72  dholland 	.d_mmap = nommap,
    106  1.72  dholland 	.d_kqfilter = nokqfilter,
    107  1.74  dholland 	.d_discard = nodiscard,
    108  1.72  dholland 	.d_flag = D_DISK
    109  1.16   gehenna };
    110  1.16   gehenna 
    111  1.83   mlelstv static struct	dkdriver lddkdriver = {
    112  1.83   mlelstv 	.d_open = ldopen,
    113  1.83   mlelstv 	.d_close = ldclose,
    114  1.83   mlelstv 	.d_strategy = ldstrategy,
    115  1.83   mlelstv 	.d_iosize = ld_iosize,
    116  1.83   mlelstv 	.d_minphys  = ldminphys,
    117  1.83   mlelstv 	.d_diskstart = ld_start,
    118  1.83   mlelstv 	.d_dumpblocks = ld_dumpblocks,
    119  1.83   mlelstv 	.d_lastclose = ld_lastclose
    120  1.83   mlelstv };
    121   1.1        ad 
    122   1.1        ad void
    123   1.1        ad ldattach(struct ld_softc *sc)
    124   1.1        ad {
    125  1.83   mlelstv 	device_t self = sc->sc_dv;
    126  1.83   mlelstv 	struct dk_softc *dksc = &sc->sc_dksc;
    127   1.1        ad 
    128  1.53        ad 	mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_VM);
    129  1.44        ad 
    130   1.7        ad 	if ((sc->sc_flags & LDF_ENABLED) == 0) {
    131   1.7        ad 		return;
    132   1.7        ad 	}
    133   1.7        ad 
    134  1.83   mlelstv 	/* Initialise dk and disk structure. */
    135  1.83   mlelstv 	dk_init(dksc, self, DKTYPE_LD);
    136  1.83   mlelstv 	disk_init(&dksc->sc_dkdev, dksc->sc_xname, &lddkdriver);
    137  1.83   mlelstv 
    138  1.83   mlelstv 	/* Attach the device into the rnd source list. */
    139  1.83   mlelstv 	rnd_attach_source(&sc->sc_rnd_source, dksc->sc_xname,
    140  1.83   mlelstv 	    RND_TYPE_DISK, RND_FLAG_DEFAULT);
    141   1.1        ad 
    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.83   mlelstv 	bufq_alloc(&dksc->sc_bufq, BUFQ_DISK_DEFAULT_STRAT, 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.24   thorpej 	int s;
    172   1.7        ad 
    173   1.7        ad 	s = splbio();
    174  1.37  christos 	sc->sc_maxqueuecnt = xmax;
    175   1.7        ad 	splx(s);
    176   1.7        ad 
    177  1.24   thorpej 	return (0);
    178   1.7        ad }
    179   1.7        ad 
    180   1.7        ad int
    181   1.7        ad ldbegindetach(struct ld_softc *sc, int flags)
    182   1.7        ad {
    183  1.83   mlelstv 	struct dk_softc *dksc = &sc->sc_dksc;
    184  1.24   thorpej 	int s, rv = 0;
    185   1.7        ad 
    186   1.7        ad 	if ((sc->sc_flags & LDF_ENABLED) == 0)
    187   1.7        ad 		return (0);
    188   1.3        ad 
    189  1.83   mlelstv 	rv = disk_begindetach(&dksc->sc_dkdev, ld_lastclose, dksc->sc_dev, flags);
    190  1.66    dyoung 
    191  1.66    dyoung 	if (rv != 0)
    192  1.66    dyoung 		return rv;
    193   1.3        ad 
    194   1.3        ad 	s = splbio();
    195  1.24   thorpej 	sc->sc_maxqueuecnt = 0;
    196  1.83   mlelstv 
    197  1.83   mlelstv 	dk_detach(dksc);
    198  1.83   mlelstv 
    199  1.24   thorpej 	while (sc->sc_queuecnt > 0) {
    200  1.24   thorpej 		sc->sc_flags |= LDF_DRAIN;
    201  1.24   thorpej 		rv = tsleep(&sc->sc_queuecnt, PRIBIO, "lddrn", 0);
    202  1.24   thorpej 		if (rv)
    203  1.24   thorpej 			break;
    204  1.24   thorpej 	}
    205   1.3        ad 	splx(s);
    206   1.7        ad 
    207   1.7        ad 	return (rv);
    208   1.3        ad }
    209   1.3        ad 
    210   1.2        ad void
    211   1.7        ad ldenddetach(struct ld_softc *sc)
    212   1.2        ad {
    213  1.83   mlelstv 	struct dk_softc *dksc = &sc->sc_dksc;
    214  1.13  drochner 	int s, bmaj, cmaj, i, mn;
    215   1.2        ad 
    216   1.7        ad 	if ((sc->sc_flags & LDF_ENABLED) == 0)
    217   1.7        ad 		return;
    218   1.7        ad 
    219   1.2        ad 	/* Wait for commands queued with the hardware to complete. */
    220   1.2        ad 	if (sc->sc_queuecnt != 0)
    221   1.7        ad 		if (tsleep(&sc->sc_queuecnt, PRIBIO, "lddtch", 30 * hz))
    222  1.83   mlelstv 			printf("%s: not drained\n", dksc->sc_xname);
    223   1.2        ad 
    224   1.2        ad 	/* Locate the major numbers. */
    225  1.16   gehenna 	bmaj = bdevsw_lookup_major(&ld_bdevsw);
    226  1.16   gehenna 	cmaj = cdevsw_lookup_major(&ld_cdevsw);
    227   1.2        ad 
    228   1.2        ad 	/* Kill off any queued buffers. */
    229   1.2        ad 	s = splbio();
    230  1.83   mlelstv 	bufq_drain(dksc->sc_bufq);
    231  1.36      yamt 	splx(s);
    232  1.36      yamt 
    233  1.83   mlelstv 	bufq_free(dksc->sc_bufq);
    234   1.2        ad 
    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.2        ad 	/* Unhook the entropy source. */
    250   1.2        ad 	rnd_detach_source(&sc->sc_rnd_source);
    251   1.2        ad 
    252  1.56  jmcneill 	/* Deregister with PMF */
    253  1.83   mlelstv 	pmf_device_deregister(dksc->sc_dev);
    254  1.56  jmcneill 
    255  1.24   thorpej 	/*
    256  1.24   thorpej 	 * XXX We can't really flush the cache here, beceause the
    257  1.24   thorpej 	 * XXX device may already be non-existent from the controller's
    258  1.24   thorpej 	 * XXX perspective.
    259  1.24   thorpej 	 */
    260  1.24   thorpej #if 0
    261   1.2        ad 	/* Flush the device's cache. */
    262   1.2        ad 	if (sc->sc_flush != NULL)
    263  1.62    simonb 		if ((*sc->sc_flush)(sc, 0) != 0)
    264  1.83   mlelstv 			aprint_error_dev(dksc->sc_dev, "unable to flush cache\n");
    265  1.24   thorpej #endif
    266  1.61        ws 	mutex_destroy(&sc->sc_mutex);
    267   1.2        ad }
    268   1.2        ad 
    269   1.8     lukem /* ARGSUSED */
    270  1.55  jmcneill static bool
    271  1.67  jmcneill ld_suspend(device_t dev, const pmf_qual_t *qual)
    272  1.67  jmcneill {
    273  1.67  jmcneill 	return ld_shutdown(dev, 0);
    274  1.67  jmcneill }
    275  1.67  jmcneill 
    276  1.67  jmcneill /* ARGSUSED */
    277  1.67  jmcneill static bool
    278  1.55  jmcneill ld_shutdown(device_t dev, int flags)
    279   1.1        ad {
    280  1.55  jmcneill 	struct ld_softc *sc = device_private(dev);
    281  1.83   mlelstv 	struct dk_softc *dksc = &sc->sc_dksc;
    282   1.1        ad 
    283  1.62    simonb 	if (sc->sc_flush != NULL && (*sc->sc_flush)(sc, LDFL_POLL) != 0) {
    284  1.83   mlelstv 		printf("%s: unable to flush cache\n", dksc->sc_xname);
    285  1.55  jmcneill 		return false;
    286   1.1        ad 	}
    287  1.55  jmcneill 
    288  1.55  jmcneill 	return true;
    289   1.1        ad }
    290   1.1        ad 
    291   1.8     lukem /* ARGSUSED */
    292  1.29   thorpej static int
    293  1.42  christos ldopen(dev_t dev, int flags, int fmt, struct lwp *l)
    294   1.1        ad {
    295   1.1        ad 	struct ld_softc *sc;
    296  1.83   mlelstv 	struct dk_softc *dksc;
    297  1.83   mlelstv 	int unit;
    298   1.1        ad 
    299   1.1        ad 	unit = DISKUNIT(dev);
    300  1.59   tsutsui 	if ((sc = device_lookup_private(&ld_cd, unit)) == NULL)
    301   1.1        ad 		return (ENXIO);
    302  1.83   mlelstv 	dksc = &sc->sc_dksc;
    303   1.1        ad 
    304  1.83   mlelstv 	return dk_open(dksc, dev, flags, fmt, l);
    305   1.1        ad }
    306   1.1        ad 
    307  1.66    dyoung static int
    308  1.83   mlelstv ld_lastclose(device_t self)
    309  1.66    dyoung {
    310  1.66    dyoung 	struct ld_softc *sc = device_private(self);
    311  1.83   mlelstv 
    312  1.66    dyoung 	if (sc->sc_flush != NULL && (*sc->sc_flush)(sc, 0) != 0)
    313  1.66    dyoung 		aprint_error_dev(self, "unable to flush cache\n");
    314  1.83   mlelstv 
    315  1.66    dyoung 	return 0;
    316  1.83   mlelstv }
    317  1.66    dyoung 
    318   1.8     lukem /* ARGSUSED */
    319  1.29   thorpej static int
    320  1.42  christos ldclose(dev_t dev, int flags, int fmt, struct lwp *l)
    321   1.1        ad {
    322   1.1        ad 	struct ld_softc *sc;
    323  1.83   mlelstv 	struct dk_softc *dksc;
    324  1.83   mlelstv 	int unit;
    325   1.1        ad 
    326   1.1        ad 	unit = DISKUNIT(dev);
    327  1.59   tsutsui 	sc = device_lookup_private(&ld_cd, unit);
    328  1.83   mlelstv 	dksc = &sc->sc_dksc;
    329  1.30   thorpej 
    330  1.83   mlelstv 	return dk_close(dksc, dev, flags, fmt, l);
    331   1.1        ad }
    332   1.1        ad 
    333   1.8     lukem /* ARGSUSED */
    334  1.29   thorpej static int
    335  1.42  christos ldread(dev_t dev, struct uio *uio, int ioflag)
    336   1.1        ad {
    337   1.1        ad 
    338   1.1        ad 	return (physio(ldstrategy, NULL, dev, B_READ, ldminphys, uio));
    339   1.1        ad }
    340   1.1        ad 
    341   1.8     lukem /* ARGSUSED */
    342  1.29   thorpej static int
    343  1.42  christos ldwrite(dev_t dev, struct uio *uio, int ioflag)
    344   1.1        ad {
    345   1.1        ad 
    346   1.1        ad 	return (physio(ldstrategy, NULL, dev, B_WRITE, ldminphys, uio));
    347   1.1        ad }
    348   1.1        ad 
    349   1.8     lukem /* ARGSUSED */
    350  1.29   thorpej static int
    351  1.46  christos ldioctl(dev_t dev, u_long cmd, void *addr, int32_t flag, struct lwp *l)
    352   1.1        ad {
    353   1.1        ad 	struct ld_softc *sc;
    354  1.83   mlelstv 	struct dk_softc *dksc;
    355  1.80  christos 	int unit, error;
    356   1.1        ad 
    357   1.1        ad 	unit = DISKUNIT(dev);
    358  1.59   tsutsui 	sc = device_lookup_private(&ld_cd, unit);
    359  1.83   mlelstv 	dksc = &sc->sc_dksc;
    360   1.1        ad 
    361  1.83   mlelstv 	error = disk_ioctl(&dksc->sc_dkdev, dev, cmd, addr, flag, l);
    362  1.83   mlelstv 	if (error != EPASSTHROUGH)
    363  1.83   mlelstv 		return (error);
    364  1.83   mlelstv 
    365  1.83   mlelstv 	error = dk_ioctl(dksc, dev, cmd, addr, flag, l);
    366  1.43       riz 	if (error != EPASSTHROUGH)
    367  1.43       riz 		return (error);
    368  1.43       riz 
    369  1.47      tron 	error = 0;
    370  1.83   mlelstv 
    371   1.1        ad 	switch (cmd) {
    372  1.32   thorpej 	case DIOCCACHESYNC:
    373  1.32   thorpej 		/*
    374  1.32   thorpej 		 * XXX Do we really need to care about having a writable
    375  1.32   thorpej 		 * file descriptor here?
    376  1.32   thorpej 		 */
    377  1.32   thorpej 		if ((flag & FWRITE) == 0)
    378  1.32   thorpej 			error = EBADF;
    379  1.32   thorpej 		else if (sc->sc_flush)
    380  1.62    simonb 			error = (*sc->sc_flush)(sc, 0);
    381  1.32   thorpej 		else
    382  1.32   thorpej 			error = 0;	/* XXX Error out instead? */
    383  1.32   thorpej 		break;
    384   1.1        ad 	default:
    385   1.1        ad 		error = ENOTTY;
    386   1.1        ad 		break;
    387   1.1        ad 	}
    388   1.1        ad 
    389   1.1        ad 	return (error);
    390   1.1        ad }
    391   1.1        ad 
    392  1.29   thorpej static void
    393   1.1        ad ldstrategy(struct buf *bp)
    394   1.1        ad {
    395   1.1        ad 	struct ld_softc *sc;
    396  1.83   mlelstv 	struct dk_softc *dksc;
    397  1.83   mlelstv 	int unit;
    398   1.2        ad 
    399  1.83   mlelstv 	unit = DISKUNIT(bp->b_dev);
    400  1.83   mlelstv 	sc = device_lookup_private(&ld_cd, unit);
    401  1.83   mlelstv 	dksc = &sc->sc_dksc;
    402  1.23   thorpej 
    403  1.83   mlelstv 	return dk_strategy(dksc, bp);
    404  1.23   thorpej }
    405  1.23   thorpej 
    406  1.23   thorpej static void
    407  1.83   mlelstv ld_start(device_t dev)
    408  1.23   thorpej {
    409  1.83   mlelstv 	struct ld_softc *sc = device_private(dev);
    410  1.83   mlelstv 	struct dk_softc *dksc = &sc->sc_dksc;
    411  1.83   mlelstv 	struct buf *bp;
    412  1.23   thorpej 	int error;
    413   1.1        ad 
    414  1.44        ad 	mutex_enter(&sc->sc_mutex);
    415  1.44        ad 
    416  1.23   thorpej 	while (sc->sc_queuecnt < sc->sc_maxqueuecnt) {
    417  1.23   thorpej 		/* See if there is work to do. */
    418  1.83   mlelstv 		if ((bp = bufq_peek(dksc->sc_bufq)) == NULL)
    419  1.23   thorpej 			break;
    420  1.23   thorpej 
    421  1.83   mlelstv 		disk_busy(&dksc->sc_dkdev);
    422  1.23   thorpej 		sc->sc_queuecnt++;
    423  1.23   thorpej 
    424  1.23   thorpej 		if (__predict_true((error = (*sc->sc_start)(sc, bp)) == 0)) {
    425  1.23   thorpej 			/*
    426  1.23   thorpej 			 * The back-end is running the job; remove it from
    427  1.23   thorpej 			 * the queue.
    428  1.23   thorpej 			 */
    429  1.83   mlelstv 			(void) bufq_get(dksc->sc_bufq);
    430  1.23   thorpej 		} else  {
    431  1.83   mlelstv 			disk_unbusy(&dksc->sc_dkdev, 0, (bp->b_flags & B_READ));
    432  1.23   thorpej 			sc->sc_queuecnt--;
    433  1.23   thorpej 			if (error == EAGAIN) {
    434  1.23   thorpej 				/*
    435  1.23   thorpej 				 * Temporary resource shortage in the
    436  1.23   thorpej 				 * back-end; just defer the job until
    437  1.23   thorpej 				 * later.
    438  1.23   thorpej 				 *
    439  1.23   thorpej 				 * XXX We might consider a watchdog timer
    440  1.23   thorpej 				 * XXX to make sure we are kicked into action.
    441  1.23   thorpej 				 */
    442  1.23   thorpej 				break;
    443  1.23   thorpej 			} else {
    444  1.83   mlelstv 				(void) bufq_get(dksc->sc_bufq);
    445  1.23   thorpej 				bp->b_error = error;
    446  1.23   thorpej 				bp->b_resid = bp->b_bcount;
    447  1.44        ad 				mutex_exit(&sc->sc_mutex);
    448  1.23   thorpej 				biodone(bp);
    449  1.44        ad 				mutex_enter(&sc->sc_mutex);
    450  1.23   thorpej 			}
    451  1.23   thorpej 		}
    452   1.1        ad 	}
    453  1.44        ad 
    454  1.44        ad 	mutex_exit(&sc->sc_mutex);
    455   1.1        ad }
    456   1.1        ad 
    457   1.1        ad void
    458   1.1        ad lddone(struct ld_softc *sc, struct buf *bp)
    459   1.1        ad {
    460  1.83   mlelstv 	struct dk_softc *dksc = &sc->sc_dksc;
    461   1.1        ad 
    462  1.83   mlelstv 	dk_done(dksc, bp);
    463   1.1        ad 
    464  1.44        ad 	mutex_enter(&sc->sc_mutex);
    465   1.7        ad 	if (--sc->sc_queuecnt <= sc->sc_maxqueuecnt) {
    466  1.24   thorpej 		if ((sc->sc_flags & LDF_DRAIN) != 0) {
    467  1.24   thorpej 			sc->sc_flags &= ~LDF_DRAIN;
    468   1.7        ad 			wakeup(&sc->sc_queuecnt);
    469  1.24   thorpej 		}
    470  1.44        ad 		mutex_exit(&sc->sc_mutex);
    471  1.83   mlelstv 		ld_start(dksc->sc_dev);
    472  1.44        ad 	} else
    473  1.44        ad 		mutex_exit(&sc->sc_mutex);
    474   1.1        ad }
    475   1.1        ad 
    476  1.29   thorpej static int
    477   1.1        ad ldsize(dev_t dev)
    478   1.1        ad {
    479   1.1        ad 	struct ld_softc *sc;
    480  1.83   mlelstv 	struct dk_softc *dksc;
    481  1.83   mlelstv 	int unit;
    482   1.1        ad 
    483   1.1        ad 	unit = DISKUNIT(dev);
    484  1.59   tsutsui 	if ((sc = device_lookup_private(&ld_cd, unit)) == NULL)
    485   1.1        ad 		return (ENODEV);
    486  1.83   mlelstv 	dksc = &sc->sc_dksc;
    487  1.83   mlelstv 
    488   1.1        ad 	if ((sc->sc_flags & LDF_ENABLED) == 0)
    489   1.1        ad 		return (ENODEV);
    490   1.1        ad 
    491  1.83   mlelstv 	return dk_size(dksc, dev);
    492   1.1        ad }
    493   1.1        ad 
    494   1.1        ad /*
    495   1.1        ad  * Take a dump.
    496   1.1        ad  */
    497  1.29   thorpej static int
    498  1.83   mlelstv lddump(dev_t dev, daddr_t blkno, void *va, size_t size)
    499   1.1        ad {
    500   1.1        ad 	struct ld_softc *sc;
    501  1.83   mlelstv 	struct dk_softc *dksc;
    502  1.83   mlelstv 	int unit;
    503   1.1        ad 
    504   1.1        ad 	unit = DISKUNIT(dev);
    505  1.59   tsutsui 	if ((sc = device_lookup_private(&ld_cd, unit)) == NULL)
    506   1.1        ad 		return (ENXIO);
    507  1.83   mlelstv 	dksc = &sc->sc_dksc;
    508  1.83   mlelstv 
    509   1.1        ad 	if ((sc->sc_flags & LDF_ENABLED) == 0)
    510   1.1        ad 		return (ENODEV);
    511  1.83   mlelstv 
    512  1.83   mlelstv 	return dk_dump(dksc, dev, blkno, va, size);
    513  1.83   mlelstv }
    514  1.83   mlelstv 
    515  1.83   mlelstv static int
    516  1.83   mlelstv ld_dumpblocks(device_t dev, void *va, daddr_t blkno, int nblk)
    517  1.83   mlelstv {
    518  1.83   mlelstv 	struct ld_softc *sc = device_private(dev);
    519  1.83   mlelstv 
    520   1.3        ad 	if (sc->sc_dump == NULL)
    521   1.3        ad 		return (ENXIO);
    522   1.3        ad 
    523  1.83   mlelstv 	return (*sc->sc_dump)(sc, va, blkno, nblk);
    524   1.1        ad }
    525   1.1        ad 
    526   1.1        ad /*
    527   1.1        ad  * Adjust the size of a transfer.
    528   1.1        ad  */
    529   1.1        ad static void
    530   1.1        ad ldminphys(struct buf *bp)
    531   1.1        ad {
    532  1.83   mlelstv 	int unit;
    533   1.1        ad 	struct ld_softc *sc;
    534   1.1        ad 
    535  1.83   mlelstv 	unit = DISKUNIT(bp->b_dev);
    536  1.83   mlelstv 	sc = device_lookup_private(&ld_cd, unit);
    537   1.1        ad 
    538  1.83   mlelstv 	ld_iosize(sc->sc_dv, &bp->b_bcount);
    539   1.1        ad 	minphys(bp);
    540   1.1        ad }
    541  1.43       riz 
    542  1.43       riz static void
    543  1.83   mlelstv ld_iosize(device_t d, int *countp)
    544  1.83   mlelstv {
    545  1.83   mlelstv 	struct ld_softc *sc = device_private(d);
    546  1.83   mlelstv 
    547  1.83   mlelstv 	if (*countp > sc->sc_maxxfer)
    548  1.83   mlelstv 		*countp = sc->sc_maxxfer;
    549  1.83   mlelstv }
    550  1.83   mlelstv 
    551  1.83   mlelstv static void
    552  1.83   mlelstv ld_fake_geometry(struct ld_softc *sc)
    553  1.83   mlelstv {
    554  1.83   mlelstv 	uint64_t ncyl;
    555  1.83   mlelstv 
    556  1.83   mlelstv 	if (sc->sc_secperunit <= 528 * 2048)		/* 528MB */
    557  1.83   mlelstv 		sc->sc_nheads = 16;
    558  1.83   mlelstv 	else if (sc->sc_secperunit <= 1024 * 2048)	/* 1GB */
    559  1.83   mlelstv 		sc->sc_nheads = 32;
    560  1.83   mlelstv 	else if (sc->sc_secperunit <= 21504 * 2048)	/* 21GB */
    561  1.83   mlelstv 		sc->sc_nheads = 64;
    562  1.83   mlelstv 	else if (sc->sc_secperunit <= 43008 * 2048)	/* 42GB */
    563  1.83   mlelstv 		sc->sc_nheads = 128;
    564  1.83   mlelstv 	else
    565  1.83   mlelstv 		sc->sc_nheads = 255;
    566  1.83   mlelstv 
    567  1.83   mlelstv 	sc->sc_nsectors = 63;
    568  1.83   mlelstv 	sc->sc_ncylinders = INT_MAX;
    569  1.83   mlelstv 	ncyl = sc->sc_secperunit /
    570  1.83   mlelstv 	    (sc->sc_nheads * sc->sc_nsectors);
    571  1.83   mlelstv 	if (ncyl < INT_MAX)
    572  1.83   mlelstv 		sc->sc_ncylinders = (int)ncyl;
    573  1.83   mlelstv }
    574  1.83   mlelstv 
    575  1.83   mlelstv static void
    576  1.83   mlelstv ld_set_geometry(struct ld_softc *sc)
    577  1.43       riz {
    578  1.83   mlelstv 	struct dk_softc *dksc = &sc->sc_dksc;
    579  1.83   mlelstv 	struct disk_geom *dg = &dksc->sc_dkdev.dk_geom;
    580  1.83   mlelstv 	char tbuf[9];
    581  1.83   mlelstv 
    582  1.83   mlelstv 	format_bytes(tbuf, sizeof(tbuf), sc->sc_secperunit *
    583  1.83   mlelstv 	    sc->sc_secsize);
    584  1.83   mlelstv 	aprint_normal_dev(dksc->sc_dev, "%s, %d cyl, %d head, %d sec, "
    585  1.83   mlelstv 	    "%d bytes/sect x %"PRIu64" sectors\n",
    586  1.83   mlelstv 	    tbuf, sc->sc_ncylinders, sc->sc_nheads,
    587  1.83   mlelstv 	    sc->sc_nsectors, sc->sc_secsize, sc->sc_secperunit);
    588  1.43       riz 
    589  1.71  christos 	memset(dg, 0, sizeof(*dg));
    590  1.83   mlelstv 	dg->dg_secperunit = sc->sc_secperunit;
    591  1.83   mlelstv 	dg->dg_secsize = sc->sc_secsize;
    592  1.83   mlelstv 	dg->dg_nsectors = sc->sc_nsectors;
    593  1.83   mlelstv 	dg->dg_ntracks = sc->sc_nheads;
    594  1.83   mlelstv 	dg->dg_ncylinders = sc->sc_ncylinders;
    595  1.43       riz 
    596  1.83   mlelstv 	disk_set_info(dksc->sc_dev, &dksc->sc_dkdev, NULL);
    597  1.43       riz }
    598  1.45       riz 
    599  1.45       riz static void
    600  1.65    cegger ld_config_interrupts(device_t d)
    601  1.45       riz {
    602  1.60      cube 	struct ld_softc *sc = device_private(d);
    603  1.83   mlelstv 	struct dk_softc *dksc = &sc->sc_dksc;
    604  1.83   mlelstv 
    605  1.83   mlelstv 	dkwedge_discover(&dksc->sc_dkdev);
    606  1.45       riz }
    607