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