Home | History | Annotate | Line # | Download | only in dev
ld.c revision 1.86
      1  1.86   mlelstv /*	$NetBSD: ld.c,v 1.86 2015/08/16 14:07:19 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.86   mlelstv __KERNEL_RCSID(0, "$NetBSD: ld.c,v 1.86 2015/08/16 14:07:19 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.85   mlelstv 	cv_init(&sc->sc_drain, "lddrain");
    130  1.44        ad 
    131   1.7        ad 	if ((sc->sc_flags & LDF_ENABLED) == 0) {
    132   1.7        ad 		return;
    133   1.7        ad 	}
    134   1.7        ad 
    135  1.83   mlelstv 	/* Initialise dk and disk structure. */
    136  1.83   mlelstv 	dk_init(dksc, self, DKTYPE_LD);
    137  1.83   mlelstv 	disk_init(&dksc->sc_dkdev, dksc->sc_xname, &lddkdriver);
    138  1.83   mlelstv 
    139  1.83   mlelstv 	/* Attach the device into the rnd source list. */
    140  1.83   mlelstv 	rnd_attach_source(&sc->sc_rnd_source, dksc->sc_xname,
    141  1.83   mlelstv 	    RND_TYPE_DISK, RND_FLAG_DEFAULT);
    142   1.1        ad 
    143   1.1        ad 	if (sc->sc_maxxfer > MAXPHYS)
    144   1.1        ad 		sc->sc_maxxfer = MAXPHYS;
    145   1.9        ad 
    146  1.19   thorpej 	/* Build synthetic geometry if necessary. */
    147  1.19   thorpej 	if (sc->sc_nheads == 0 || sc->sc_nsectors == 0 ||
    148  1.83   mlelstv 	    sc->sc_ncylinders == 0)
    149  1.83   mlelstv 	    ld_fake_geometry(sc);
    150  1.19   thorpej 
    151  1.68  kiyohara 	sc->sc_disksize512 = sc->sc_secperunit * sc->sc_secsize / DEV_BSIZE;
    152   1.1        ad 
    153  1.83   mlelstv 	/* Attach dk and disk subsystems */
    154  1.83   mlelstv 	dk_attach(dksc);
    155  1.83   mlelstv 	disk_attach(&dksc->sc_dkdev);
    156  1.71  christos 	ld_set_geometry(sc);
    157  1.43       riz 
    158  1.83   mlelstv 	bufq_alloc(&dksc->sc_bufq, BUFQ_DISK_DEFAULT_STRAT, BUFQ_SORT_RAWBLOCK);
    159   1.1        ad 
    160  1.55  jmcneill 	/* Register with PMF */
    161  1.83   mlelstv 	if (!pmf_device_register1(dksc->sc_dev, ld_suspend, NULL, ld_shutdown))
    162  1.83   mlelstv 		aprint_error_dev(dksc->sc_dev,
    163  1.55  jmcneill 		    "couldn't establish power handler\n");
    164  1.55  jmcneill 
    165  1.30   thorpej 	/* Discover wedges on this disk. */
    166  1.63      tron 	config_interrupts(sc->sc_dv, ld_config_interrupts);
    167   1.1        ad }
    168   1.1        ad 
    169   1.3        ad int
    170  1.37  christos ldadjqparam(struct ld_softc *sc, int xmax)
    171   1.3        ad {
    172   1.7        ad 
    173  1.85   mlelstv 	mutex_enter(&sc->sc_mutex);
    174  1.37  christos 	sc->sc_maxqueuecnt = xmax;
    175  1.85   mlelstv 	mutex_exit(&sc->sc_mutex);
    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.85   mlelstv 	int 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.85   mlelstv 	mutex_enter(&sc->sc_mutex);
    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.85   mlelstv 		cv_wait(&sc->sc_drain, &sc->sc_mutex);
    202  1.24   thorpej 	}
    203  1.85   mlelstv 	mutex_exit(&sc->sc_mutex);
    204   1.7        ad 
    205   1.7        ad 	return (rv);
    206   1.3        ad }
    207   1.3        ad 
    208   1.2        ad void
    209   1.7        ad ldenddetach(struct ld_softc *sc)
    210   1.2        ad {
    211  1.83   mlelstv 	struct dk_softc *dksc = &sc->sc_dksc;
    212  1.85   mlelstv 	int bmaj, cmaj, i, mn;
    213   1.2        ad 
    214   1.7        ad 	if ((sc->sc_flags & LDF_ENABLED) == 0)
    215   1.7        ad 		return;
    216   1.7        ad 
    217  1.85   mlelstv 	mutex_enter(&sc->sc_mutex);
    218  1.85   mlelstv 
    219   1.2        ad 	/* Wait for commands queued with the hardware to complete. */
    220  1.86   mlelstv 	if (sc->sc_queuecnt != 0) {
    221  1.86   mlelstv 		if (cv_timedwait(&sc->sc_drain, &sc->sc_mutex, 30 * hz))
    222  1.83   mlelstv 			printf("%s: not drained\n", dksc->sc_xname);
    223  1.86   mlelstv 	}
    224   1.2        ad 
    225   1.2        ad 	/* Kill off any queued buffers. */
    226  1.83   mlelstv 	bufq_drain(dksc->sc_bufq);
    227  1.85   mlelstv 	mutex_exit(&sc->sc_mutex);
    228  1.36      yamt 
    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.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.85   mlelstv 	cv_destroy(&sc->sc_drain);
    267  1.61        ws 	mutex_destroy(&sc->sc_mutex);
    268   1.2        ad }
    269   1.2        ad 
    270   1.8     lukem /* ARGSUSED */
    271  1.55  jmcneill static bool
    272  1.67  jmcneill ld_suspend(device_t dev, const pmf_qual_t *qual)
    273  1.67  jmcneill {
    274  1.67  jmcneill 	return ld_shutdown(dev, 0);
    275  1.67  jmcneill }
    276  1.67  jmcneill 
    277  1.67  jmcneill /* ARGSUSED */
    278  1.67  jmcneill static bool
    279  1.55  jmcneill ld_shutdown(device_t dev, int flags)
    280   1.1        ad {
    281  1.55  jmcneill 	struct ld_softc *sc = device_private(dev);
    282  1.83   mlelstv 	struct dk_softc *dksc = &sc->sc_dksc;
    283   1.1        ad 
    284  1.62    simonb 	if (sc->sc_flush != NULL && (*sc->sc_flush)(sc, LDFL_POLL) != 0) {
    285  1.83   mlelstv 		printf("%s: unable to flush cache\n", dksc->sc_xname);
    286  1.55  jmcneill 		return false;
    287   1.1        ad 	}
    288  1.55  jmcneill 
    289  1.55  jmcneill 	return true;
    290   1.1        ad }
    291   1.1        ad 
    292   1.8     lukem /* ARGSUSED */
    293  1.29   thorpej static int
    294  1.42  christos ldopen(dev_t dev, int flags, int fmt, struct lwp *l)
    295   1.1        ad {
    296   1.1        ad 	struct ld_softc *sc;
    297  1.83   mlelstv 	struct dk_softc *dksc;
    298  1.83   mlelstv 	int unit;
    299   1.1        ad 
    300   1.1        ad 	unit = DISKUNIT(dev);
    301  1.59   tsutsui 	if ((sc = device_lookup_private(&ld_cd, unit)) == NULL)
    302   1.1        ad 		return (ENXIO);
    303  1.83   mlelstv 	dksc = &sc->sc_dksc;
    304   1.1        ad 
    305  1.83   mlelstv 	return dk_open(dksc, dev, flags, fmt, l);
    306   1.1        ad }
    307   1.1        ad 
    308  1.66    dyoung static int
    309  1.83   mlelstv ld_lastclose(device_t self)
    310  1.66    dyoung {
    311  1.66    dyoung 	struct ld_softc *sc = device_private(self);
    312  1.84     skrll 
    313  1.66    dyoung 	if (sc->sc_flush != NULL && (*sc->sc_flush)(sc, 0) != 0)
    314  1.66    dyoung 		aprint_error_dev(self, "unable to flush cache\n");
    315  1.84     skrll 
    316  1.66    dyoung 	return 0;
    317  1.84     skrll }
    318  1.66    dyoung 
    319   1.8     lukem /* ARGSUSED */
    320  1.29   thorpej static int
    321  1.42  christos ldclose(dev_t dev, int flags, int fmt, struct lwp *l)
    322   1.1        ad {
    323   1.1        ad 	struct ld_softc *sc;
    324  1.83   mlelstv 	struct dk_softc *dksc;
    325  1.83   mlelstv 	int unit;
    326   1.1        ad 
    327   1.1        ad 	unit = DISKUNIT(dev);
    328  1.59   tsutsui 	sc = device_lookup_private(&ld_cd, unit);
    329  1.83   mlelstv 	dksc = &sc->sc_dksc;
    330  1.30   thorpej 
    331  1.83   mlelstv 	return dk_close(dksc, dev, flags, fmt, l);
    332   1.1        ad }
    333   1.1        ad 
    334   1.8     lukem /* ARGSUSED */
    335  1.29   thorpej static int
    336  1.42  christos ldread(dev_t dev, struct uio *uio, int ioflag)
    337   1.1        ad {
    338   1.1        ad 
    339   1.1        ad 	return (physio(ldstrategy, NULL, dev, B_READ, ldminphys, uio));
    340   1.1        ad }
    341   1.1        ad 
    342   1.8     lukem /* ARGSUSED */
    343  1.29   thorpej static int
    344  1.42  christos ldwrite(dev_t dev, struct uio *uio, int ioflag)
    345   1.1        ad {
    346   1.1        ad 
    347   1.1        ad 	return (physio(ldstrategy, NULL, dev, B_WRITE, ldminphys, uio));
    348   1.1        ad }
    349   1.1        ad 
    350   1.8     lukem /* ARGSUSED */
    351  1.29   thorpej static int
    352  1.46  christos ldioctl(dev_t dev, u_long cmd, void *addr, int32_t flag, struct lwp *l)
    353   1.1        ad {
    354   1.1        ad 	struct ld_softc *sc;
    355  1.83   mlelstv 	struct dk_softc *dksc;
    356  1.80  christos 	int unit, error;
    357   1.1        ad 
    358   1.1        ad 	unit = DISKUNIT(dev);
    359  1.59   tsutsui 	sc = device_lookup_private(&ld_cd, unit);
    360  1.83   mlelstv 	dksc = &sc->sc_dksc;
    361   1.1        ad 
    362  1.83   mlelstv 	error = disk_ioctl(&dksc->sc_dkdev, dev, cmd, addr, flag, l);
    363  1.83   mlelstv 	if (error != EPASSTHROUGH)
    364  1.83   mlelstv 		return (error);
    365  1.83   mlelstv 
    366  1.83   mlelstv 	error = dk_ioctl(dksc, dev, cmd, addr, flag, l);
    367  1.43       riz 	if (error != EPASSTHROUGH)
    368  1.43       riz 		return (error);
    369  1.43       riz 
    370  1.47      tron 	error = 0;
    371  1.83   mlelstv 
    372   1.1        ad 	switch (cmd) {
    373  1.32   thorpej 	case DIOCCACHESYNC:
    374  1.32   thorpej 		/*
    375  1.32   thorpej 		 * XXX Do we really need to care about having a writable
    376  1.32   thorpej 		 * file descriptor here?
    377  1.32   thorpej 		 */
    378  1.32   thorpej 		if ((flag & FWRITE) == 0)
    379  1.32   thorpej 			error = EBADF;
    380  1.32   thorpej 		else if (sc->sc_flush)
    381  1.62    simonb 			error = (*sc->sc_flush)(sc, 0);
    382  1.32   thorpej 		else
    383  1.32   thorpej 			error = 0;	/* XXX Error out instead? */
    384  1.32   thorpej 		break;
    385   1.1        ad 	default:
    386   1.1        ad 		error = ENOTTY;
    387   1.1        ad 		break;
    388   1.1        ad 	}
    389   1.1        ad 
    390   1.1        ad 	return (error);
    391   1.1        ad }
    392   1.1        ad 
    393  1.29   thorpej static void
    394   1.1        ad ldstrategy(struct buf *bp)
    395   1.1        ad {
    396   1.1        ad 	struct ld_softc *sc;
    397  1.83   mlelstv 	struct dk_softc *dksc;
    398  1.83   mlelstv 	int unit;
    399   1.2        ad 
    400  1.83   mlelstv 	unit = DISKUNIT(bp->b_dev);
    401  1.83   mlelstv 	sc = device_lookup_private(&ld_cd, unit);
    402  1.83   mlelstv 	dksc = &sc->sc_dksc;
    403  1.23   thorpej 
    404  1.83   mlelstv 	return dk_strategy(dksc, bp);
    405  1.23   thorpej }
    406  1.23   thorpej 
    407  1.23   thorpej static void
    408  1.83   mlelstv ld_start(device_t dev)
    409  1.23   thorpej {
    410  1.83   mlelstv 	struct ld_softc *sc = device_private(dev);
    411  1.83   mlelstv 	struct dk_softc *dksc = &sc->sc_dksc;
    412  1.83   mlelstv 	struct buf *bp;
    413  1.23   thorpej 	int error;
    414   1.1        ad 
    415  1.44        ad 	mutex_enter(&sc->sc_mutex);
    416  1.44        ad 
    417  1.23   thorpej 	while (sc->sc_queuecnt < sc->sc_maxqueuecnt) {
    418  1.23   thorpej 		/* See if there is work to do. */
    419  1.83   mlelstv 		if ((bp = bufq_peek(dksc->sc_bufq)) == NULL)
    420  1.23   thorpej 			break;
    421  1.23   thorpej 
    422  1.83   mlelstv 		disk_busy(&dksc->sc_dkdev);
    423  1.23   thorpej 		sc->sc_queuecnt++;
    424  1.23   thorpej 
    425  1.23   thorpej 		if (__predict_true((error = (*sc->sc_start)(sc, bp)) == 0)) {
    426  1.23   thorpej 			/*
    427  1.23   thorpej 			 * The back-end is running the job; remove it from
    428  1.23   thorpej 			 * the queue.
    429  1.23   thorpej 			 */
    430  1.83   mlelstv 			(void) bufq_get(dksc->sc_bufq);
    431  1.23   thorpej 		} else  {
    432  1.83   mlelstv 			disk_unbusy(&dksc->sc_dkdev, 0, (bp->b_flags & B_READ));
    433  1.23   thorpej 			sc->sc_queuecnt--;
    434  1.23   thorpej 			if (error == EAGAIN) {
    435  1.23   thorpej 				/*
    436  1.23   thorpej 				 * Temporary resource shortage in the
    437  1.23   thorpej 				 * back-end; just defer the job until
    438  1.23   thorpej 				 * later.
    439  1.23   thorpej 				 *
    440  1.23   thorpej 				 * XXX We might consider a watchdog timer
    441  1.23   thorpej 				 * XXX to make sure we are kicked into action.
    442  1.23   thorpej 				 */
    443  1.23   thorpej 				break;
    444  1.23   thorpej 			} else {
    445  1.83   mlelstv 				(void) bufq_get(dksc->sc_bufq);
    446  1.23   thorpej 				bp->b_error = error;
    447  1.23   thorpej 				bp->b_resid = bp->b_bcount;
    448  1.44        ad 				mutex_exit(&sc->sc_mutex);
    449  1.23   thorpej 				biodone(bp);
    450  1.44        ad 				mutex_enter(&sc->sc_mutex);
    451  1.23   thorpej 			}
    452  1.23   thorpej 		}
    453   1.1        ad 	}
    454  1.44        ad 
    455  1.44        ad 	mutex_exit(&sc->sc_mutex);
    456   1.1        ad }
    457   1.1        ad 
    458   1.1        ad void
    459   1.1        ad lddone(struct ld_softc *sc, struct buf *bp)
    460   1.1        ad {
    461  1.83   mlelstv 	struct dk_softc *dksc = &sc->sc_dksc;
    462   1.1        ad 
    463  1.83   mlelstv 	dk_done(dksc, bp);
    464   1.1        ad 
    465  1.44        ad 	mutex_enter(&sc->sc_mutex);
    466   1.7        ad 	if (--sc->sc_queuecnt <= sc->sc_maxqueuecnt) {
    467  1.24   thorpej 		if ((sc->sc_flags & LDF_DRAIN) != 0) {
    468  1.24   thorpej 			sc->sc_flags &= ~LDF_DRAIN;
    469   1.7        ad 			wakeup(&sc->sc_queuecnt);
    470  1.24   thorpej 		}
    471  1.44        ad 		mutex_exit(&sc->sc_mutex);
    472  1.83   mlelstv 		ld_start(dksc->sc_dev);
    473  1.44        ad 	} else
    474  1.44        ad 		mutex_exit(&sc->sc_mutex);
    475   1.1        ad }
    476   1.1        ad 
    477  1.29   thorpej static int
    478   1.1        ad ldsize(dev_t dev)
    479   1.1        ad {
    480   1.1        ad 	struct ld_softc *sc;
    481  1.83   mlelstv 	struct dk_softc *dksc;
    482  1.83   mlelstv 	int unit;
    483   1.1        ad 
    484   1.1        ad 	unit = DISKUNIT(dev);
    485  1.59   tsutsui 	if ((sc = device_lookup_private(&ld_cd, unit)) == NULL)
    486   1.1        ad 		return (ENODEV);
    487  1.83   mlelstv 	dksc = &sc->sc_dksc;
    488  1.83   mlelstv 
    489   1.1        ad 	if ((sc->sc_flags & LDF_ENABLED) == 0)
    490   1.1        ad 		return (ENODEV);
    491   1.1        ad 
    492  1.83   mlelstv 	return dk_size(dksc, dev);
    493   1.1        ad }
    494   1.1        ad 
    495   1.1        ad /*
    496   1.1        ad  * Take a dump.
    497   1.1        ad  */
    498  1.29   thorpej static int
    499  1.83   mlelstv lddump(dev_t dev, daddr_t blkno, void *va, size_t size)
    500   1.1        ad {
    501   1.1        ad 	struct ld_softc *sc;
    502  1.83   mlelstv 	struct dk_softc *dksc;
    503  1.83   mlelstv 	int unit;
    504   1.1        ad 
    505   1.1        ad 	unit = DISKUNIT(dev);
    506  1.59   tsutsui 	if ((sc = device_lookup_private(&ld_cd, unit)) == NULL)
    507   1.1        ad 		return (ENXIO);
    508  1.83   mlelstv 	dksc = &sc->sc_dksc;
    509  1.83   mlelstv 
    510   1.1        ad 	if ((sc->sc_flags & LDF_ENABLED) == 0)
    511   1.1        ad 		return (ENODEV);
    512  1.83   mlelstv 
    513  1.83   mlelstv 	return dk_dump(dksc, dev, blkno, va, size);
    514  1.83   mlelstv }
    515  1.83   mlelstv 
    516  1.83   mlelstv static int
    517  1.83   mlelstv ld_dumpblocks(device_t dev, void *va, daddr_t blkno, int nblk)
    518  1.83   mlelstv {
    519  1.83   mlelstv 	struct ld_softc *sc = device_private(dev);
    520  1.84     skrll 
    521   1.3        ad 	if (sc->sc_dump == NULL)
    522   1.3        ad 		return (ENXIO);
    523   1.3        ad 
    524  1.83   mlelstv 	return (*sc->sc_dump)(sc, va, blkno, nblk);
    525   1.1        ad }
    526   1.1        ad 
    527   1.1        ad /*
    528   1.1        ad  * Adjust the size of a transfer.
    529   1.1        ad  */
    530   1.1        ad static void
    531   1.1        ad ldminphys(struct buf *bp)
    532   1.1        ad {
    533  1.83   mlelstv 	int unit;
    534   1.1        ad 	struct ld_softc *sc;
    535   1.1        ad 
    536  1.83   mlelstv 	unit = DISKUNIT(bp->b_dev);
    537  1.83   mlelstv 	sc = device_lookup_private(&ld_cd, unit);
    538   1.1        ad 
    539  1.83   mlelstv 	ld_iosize(sc->sc_dv, &bp->b_bcount);
    540   1.1        ad 	minphys(bp);
    541   1.1        ad }
    542  1.43       riz 
    543  1.43       riz static void
    544  1.83   mlelstv ld_iosize(device_t d, int *countp)
    545  1.83   mlelstv {
    546  1.83   mlelstv 	struct ld_softc *sc = device_private(d);
    547  1.83   mlelstv 
    548  1.83   mlelstv 	if (*countp > sc->sc_maxxfer)
    549  1.83   mlelstv 		*countp = sc->sc_maxxfer;
    550  1.83   mlelstv }
    551  1.83   mlelstv 
    552  1.83   mlelstv static void
    553  1.83   mlelstv ld_fake_geometry(struct ld_softc *sc)
    554  1.83   mlelstv {
    555  1.83   mlelstv 	uint64_t ncyl;
    556  1.83   mlelstv 
    557  1.83   mlelstv 	if (sc->sc_secperunit <= 528 * 2048)		/* 528MB */
    558  1.83   mlelstv 		sc->sc_nheads = 16;
    559  1.83   mlelstv 	else if (sc->sc_secperunit <= 1024 * 2048)	/* 1GB */
    560  1.83   mlelstv 		sc->sc_nheads = 32;
    561  1.83   mlelstv 	else if (sc->sc_secperunit <= 21504 * 2048)	/* 21GB */
    562  1.83   mlelstv 		sc->sc_nheads = 64;
    563  1.83   mlelstv 	else if (sc->sc_secperunit <= 43008 * 2048)	/* 42GB */
    564  1.83   mlelstv 		sc->sc_nheads = 128;
    565  1.83   mlelstv 	else
    566  1.83   mlelstv 		sc->sc_nheads = 255;
    567  1.83   mlelstv 
    568  1.83   mlelstv 	sc->sc_nsectors = 63;
    569  1.83   mlelstv 	sc->sc_ncylinders = INT_MAX;
    570  1.83   mlelstv 	ncyl = sc->sc_secperunit /
    571  1.83   mlelstv 	    (sc->sc_nheads * sc->sc_nsectors);
    572  1.83   mlelstv 	if (ncyl < INT_MAX)
    573  1.83   mlelstv 		sc->sc_ncylinders = (int)ncyl;
    574  1.83   mlelstv }
    575  1.83   mlelstv 
    576  1.83   mlelstv static void
    577  1.83   mlelstv ld_set_geometry(struct ld_softc *sc)
    578  1.43       riz {
    579  1.83   mlelstv 	struct dk_softc *dksc = &sc->sc_dksc;
    580  1.83   mlelstv 	struct disk_geom *dg = &dksc->sc_dkdev.dk_geom;
    581  1.83   mlelstv 	char tbuf[9];
    582  1.83   mlelstv 
    583  1.83   mlelstv 	format_bytes(tbuf, sizeof(tbuf), sc->sc_secperunit *
    584  1.83   mlelstv 	    sc->sc_secsize);
    585  1.83   mlelstv 	aprint_normal_dev(dksc->sc_dev, "%s, %d cyl, %d head, %d sec, "
    586  1.83   mlelstv 	    "%d bytes/sect x %"PRIu64" sectors\n",
    587  1.83   mlelstv 	    tbuf, sc->sc_ncylinders, sc->sc_nheads,
    588  1.83   mlelstv 	    sc->sc_nsectors, sc->sc_secsize, sc->sc_secperunit);
    589  1.43       riz 
    590  1.71  christos 	memset(dg, 0, sizeof(*dg));
    591  1.83   mlelstv 	dg->dg_secperunit = sc->sc_secperunit;
    592  1.83   mlelstv 	dg->dg_secsize = sc->sc_secsize;
    593  1.83   mlelstv 	dg->dg_nsectors = sc->sc_nsectors;
    594  1.83   mlelstv 	dg->dg_ntracks = sc->sc_nheads;
    595  1.83   mlelstv 	dg->dg_ncylinders = sc->sc_ncylinders;
    596  1.43       riz 
    597  1.83   mlelstv 	disk_set_info(dksc->sc_dev, &dksc->sc_dkdev, NULL);
    598  1.43       riz }
    599  1.45       riz 
    600  1.45       riz static void
    601  1.65    cegger ld_config_interrupts(device_t d)
    602  1.45       riz {
    603  1.60      cube 	struct ld_softc *sc = device_private(d);
    604  1.83   mlelstv 	struct dk_softc *dksc = &sc->sc_dksc;
    605  1.83   mlelstv 
    606  1.83   mlelstv 	dkwedge_discover(&dksc->sc_dkdev);
    607  1.45       riz }
    608