Home | History | Annotate | Line # | Download | only in dev
ld.c revision 1.91
      1  1.91   mlelstv /*	$NetBSD: ld.c,v 1.91 2015/08/18 04:20:25 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.91   mlelstv __KERNEL_RCSID(0, "$NetBSD: ld.c,v 1.91 2015/08/18 04:20:25 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.83   mlelstv 	dk_detach(dksc);
    201  1.83   mlelstv 
    202  1.24   thorpej 	while (sc->sc_queuecnt > 0) {
    203  1.24   thorpej 		sc->sc_flags |= LDF_DRAIN;
    204  1.85   mlelstv 		cv_wait(&sc->sc_drain, &sc->sc_mutex);
    205  1.24   thorpej 	}
    206  1.85   mlelstv 	mutex_exit(&sc->sc_mutex);
    207   1.7        ad 
    208   1.7        ad 	return (rv);
    209   1.3        ad }
    210   1.3        ad 
    211   1.2        ad void
    212   1.7        ad ldenddetach(struct ld_softc *sc)
    213   1.2        ad {
    214  1.83   mlelstv 	struct dk_softc *dksc = &sc->sc_dksc;
    215  1.85   mlelstv 	int bmaj, cmaj, i, mn;
    216   1.2        ad 
    217   1.7        ad 	if ((sc->sc_flags & LDF_ENABLED) == 0)
    218   1.7        ad 		return;
    219   1.7        ad 
    220  1.85   mlelstv 	mutex_enter(&sc->sc_mutex);
    221  1.85   mlelstv 
    222   1.2        ad 	/* Wait for commands queued with the hardware to complete. */
    223  1.86   mlelstv 	if (sc->sc_queuecnt != 0) {
    224  1.86   mlelstv 		if (cv_timedwait(&sc->sc_drain, &sc->sc_mutex, 30 * hz))
    225  1.83   mlelstv 			printf("%s: not drained\n", dksc->sc_xname);
    226  1.86   mlelstv 	}
    227   1.2        ad 
    228   1.2        ad 	/* Kill off any queued buffers. */
    229  1.83   mlelstv 	bufq_drain(dksc->sc_bufq);
    230  1.85   mlelstv 	mutex_exit(&sc->sc_mutex);
    231  1.36      yamt 
    232  1.83   mlelstv 	bufq_free(dksc->sc_bufq);
    233   1.2        ad 
    234  1.85   mlelstv 	/* Locate the major numbers. */
    235  1.85   mlelstv 	bmaj = bdevsw_lookup_major(&ld_bdevsw);
    236  1.85   mlelstv 	cmaj = cdevsw_lookup_major(&ld_cdevsw);
    237  1.85   mlelstv 
    238   1.2        ad 	/* Nuke the vnodes for any open instances. */
    239  1.13  drochner 	for (i = 0; i < MAXPARTITIONS; i++) {
    240  1.83   mlelstv 		mn = DISKMINOR(device_unit(dksc->sc_dev), i);
    241  1.13  drochner 		vdevgone(bmaj, mn, mn, VBLK);
    242  1.13  drochner 		vdevgone(cmaj, mn, mn, VCHR);
    243  1.13  drochner 	}
    244  1.13  drochner 
    245  1.30   thorpej 	/* Delete all of our wedges. */
    246  1.83   mlelstv 	dkwedge_delall(&dksc->sc_dkdev);
    247  1.30   thorpej 
    248   1.2        ad 	/* Detach from the disk list. */
    249  1.83   mlelstv 	disk_detach(&dksc->sc_dkdev);
    250  1.83   mlelstv 	disk_destroy(&dksc->sc_dkdev);
    251   1.2        ad 
    252   1.2        ad 	/* Unhook the entropy source. */
    253   1.2        ad 	rnd_detach_source(&sc->sc_rnd_source);
    254   1.2        ad 
    255  1.56  jmcneill 	/* Deregister with PMF */
    256  1.83   mlelstv 	pmf_device_deregister(dksc->sc_dev);
    257  1.56  jmcneill 
    258  1.24   thorpej 	/*
    259  1.24   thorpej 	 * XXX We can't really flush the cache here, beceause the
    260  1.24   thorpej 	 * XXX device may already be non-existent from the controller's
    261  1.24   thorpej 	 * XXX perspective.
    262  1.24   thorpej 	 */
    263  1.24   thorpej #if 0
    264   1.2        ad 	/* Flush the device's cache. */
    265   1.2        ad 	if (sc->sc_flush != NULL)
    266  1.62    simonb 		if ((*sc->sc_flush)(sc, 0) != 0)
    267  1.87   mlelstv 			device_printf(dksc->sc_dev, "unable to flush cache\n");
    268  1.24   thorpej #endif
    269  1.85   mlelstv 	cv_destroy(&sc->sc_drain);
    270  1.61        ws 	mutex_destroy(&sc->sc_mutex);
    271   1.2        ad }
    272   1.2        ad 
    273   1.8     lukem /* ARGSUSED */
    274  1.55  jmcneill static bool
    275  1.67  jmcneill ld_suspend(device_t dev, const pmf_qual_t *qual)
    276  1.67  jmcneill {
    277  1.67  jmcneill 	return ld_shutdown(dev, 0);
    278  1.67  jmcneill }
    279  1.67  jmcneill 
    280  1.67  jmcneill /* ARGSUSED */
    281  1.67  jmcneill static bool
    282  1.55  jmcneill ld_shutdown(device_t dev, int flags)
    283   1.1        ad {
    284  1.55  jmcneill 	struct ld_softc *sc = device_private(dev);
    285  1.83   mlelstv 	struct dk_softc *dksc = &sc->sc_dksc;
    286   1.1        ad 
    287  1.62    simonb 	if (sc->sc_flush != NULL && (*sc->sc_flush)(sc, LDFL_POLL) != 0) {
    288  1.87   mlelstv 		device_printf(dksc->sc_dev, "unable to flush cache\n");
    289  1.55  jmcneill 		return false;
    290   1.1        ad 	}
    291  1.55  jmcneill 
    292  1.55  jmcneill 	return true;
    293   1.1        ad }
    294   1.1        ad 
    295   1.8     lukem /* ARGSUSED */
    296  1.29   thorpej static int
    297  1.42  christos ldopen(dev_t dev, int flags, int fmt, struct lwp *l)
    298   1.1        ad {
    299   1.1        ad 	struct ld_softc *sc;
    300  1.83   mlelstv 	struct dk_softc *dksc;
    301  1.83   mlelstv 	int unit;
    302   1.1        ad 
    303   1.1        ad 	unit = DISKUNIT(dev);
    304  1.59   tsutsui 	if ((sc = device_lookup_private(&ld_cd, unit)) == NULL)
    305   1.1        ad 		return (ENXIO);
    306  1.83   mlelstv 	dksc = &sc->sc_dksc;
    307   1.1        ad 
    308  1.83   mlelstv 	return dk_open(dksc, dev, flags, fmt, l);
    309   1.1        ad }
    310   1.1        ad 
    311  1.66    dyoung static int
    312  1.83   mlelstv ld_lastclose(device_t self)
    313  1.66    dyoung {
    314  1.66    dyoung 	struct ld_softc *sc = device_private(self);
    315  1.84     skrll 
    316  1.66    dyoung 	if (sc->sc_flush != NULL && (*sc->sc_flush)(sc, 0) != 0)
    317  1.87   mlelstv 		device_printf(self, "unable to flush cache\n");
    318  1.84     skrll 
    319  1.66    dyoung 	return 0;
    320  1.84     skrll }
    321  1.66    dyoung 
    322   1.8     lukem /* ARGSUSED */
    323  1.29   thorpej static int
    324  1.42  christos ldclose(dev_t dev, int flags, int fmt, struct lwp *l)
    325   1.1        ad {
    326   1.1        ad 	struct ld_softc *sc;
    327  1.83   mlelstv 	struct dk_softc *dksc;
    328  1.83   mlelstv 	int unit;
    329   1.1        ad 
    330   1.1        ad 	unit = DISKUNIT(dev);
    331  1.59   tsutsui 	sc = device_lookup_private(&ld_cd, unit);
    332  1.83   mlelstv 	dksc = &sc->sc_dksc;
    333  1.30   thorpej 
    334  1.83   mlelstv 	return dk_close(dksc, dev, flags, fmt, l);
    335   1.1        ad }
    336   1.1        ad 
    337   1.8     lukem /* ARGSUSED */
    338  1.29   thorpej static int
    339  1.42  christos ldread(dev_t dev, struct uio *uio, int ioflag)
    340   1.1        ad {
    341   1.1        ad 
    342   1.1        ad 	return (physio(ldstrategy, NULL, dev, B_READ, ldminphys, uio));
    343   1.1        ad }
    344   1.1        ad 
    345   1.8     lukem /* ARGSUSED */
    346  1.29   thorpej static int
    347  1.42  christos ldwrite(dev_t dev, struct uio *uio, int ioflag)
    348   1.1        ad {
    349   1.1        ad 
    350   1.1        ad 	return (physio(ldstrategy, NULL, dev, B_WRITE, ldminphys, uio));
    351   1.1        ad }
    352   1.1        ad 
    353   1.8     lukem /* ARGSUSED */
    354  1.29   thorpej static int
    355  1.46  christos ldioctl(dev_t dev, u_long cmd, void *addr, int32_t flag, struct lwp *l)
    356   1.1        ad {
    357   1.1        ad 	struct ld_softc *sc;
    358  1.83   mlelstv 	struct dk_softc *dksc;
    359  1.80  christos 	int unit, error;
    360   1.1        ad 
    361   1.1        ad 	unit = DISKUNIT(dev);
    362  1.59   tsutsui 	sc = device_lookup_private(&ld_cd, unit);
    363  1.83   mlelstv 	dksc = &sc->sc_dksc;
    364   1.1        ad 
    365  1.83   mlelstv 	error = disk_ioctl(&dksc->sc_dkdev, dev, cmd, addr, flag, l);
    366  1.83   mlelstv 	if (error != EPASSTHROUGH)
    367  1.83   mlelstv 		return (error);
    368  1.83   mlelstv 
    369  1.83   mlelstv 	error = dk_ioctl(dksc, dev, cmd, addr, flag, l);
    370  1.43       riz 	if (error != EPASSTHROUGH)
    371  1.43       riz 		return (error);
    372  1.43       riz 
    373  1.47      tron 	error = 0;
    374  1.83   mlelstv 
    375   1.1        ad 	switch (cmd) {
    376  1.32   thorpej 	case DIOCCACHESYNC:
    377  1.32   thorpej 		/*
    378  1.32   thorpej 		 * XXX Do we really need to care about having a writable
    379  1.32   thorpej 		 * file descriptor here?
    380  1.32   thorpej 		 */
    381  1.32   thorpej 		if ((flag & FWRITE) == 0)
    382  1.32   thorpej 			error = EBADF;
    383  1.32   thorpej 		else if (sc->sc_flush)
    384  1.62    simonb 			error = (*sc->sc_flush)(sc, 0);
    385  1.32   thorpej 		else
    386  1.32   thorpej 			error = 0;	/* XXX Error out instead? */
    387  1.32   thorpej 		break;
    388   1.1        ad 	default:
    389   1.1        ad 		error = ENOTTY;
    390   1.1        ad 		break;
    391   1.1        ad 	}
    392   1.1        ad 
    393   1.1        ad 	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.83   mlelstv 	return 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.1        ad 		return (ENODEV);
    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.1        ad 		return (ENODEV);
    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