Home | History | Annotate | Line # | Download | only in dev
ld.c revision 1.72.2.2
      1  1.72.2.2       tls /*	$NetBSD: ld.c,v 1.72.2.2 2014/08/10 06:54:50 tls 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.72.2.2       tls __KERNEL_RCSID(0, "$NetBSD: ld.c,v 1.72.2.2 2014/08/10 06:54:50 tls 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 #include <sys/rnd.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	ldgetdefaultlabel(struct ld_softc *, struct disklabel *);
     64       1.1        ad static void	ldgetdisklabel(struct ld_softc *);
     65       1.1        ad static void	ldminphys(struct buf *bp);
     66      1.67  jmcneill static bool	ld_suspend(device_t, const pmf_qual_t *);
     67      1.55  jmcneill static bool	ld_shutdown(device_t, int);
     68      1.44        ad static void	ldstart(struct ld_softc *, struct buf *);
     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.66    dyoung static int	ldlastclose(device_t);
     72       1.1        ad 
     73       1.1        ad extern struct	cfdriver ld_cd;
     74       1.1        ad 
     75      1.29   thorpej static dev_type_open(ldopen);
     76      1.29   thorpej static dev_type_close(ldclose);
     77      1.29   thorpej static dev_type_read(ldread);
     78      1.29   thorpej static dev_type_write(ldwrite);
     79      1.29   thorpej static dev_type_ioctl(ldioctl);
     80      1.29   thorpej static dev_type_strategy(ldstrategy);
     81      1.29   thorpej static dev_type_dump(lddump);
     82      1.29   thorpej static dev_type_size(ldsize);
     83      1.16   gehenna 
     84      1.16   gehenna const struct bdevsw ld_bdevsw = {
     85      1.72  dholland 	.d_open = ldopen,
     86      1.72  dholland 	.d_close = ldclose,
     87      1.72  dholland 	.d_strategy = ldstrategy,
     88      1.72  dholland 	.d_ioctl = ldioctl,
     89      1.72  dholland 	.d_dump = lddump,
     90      1.72  dholland 	.d_psize = ldsize,
     91  1.72.2.2       tls 	.d_discard = nodiscard,
     92      1.72  dholland 	.d_flag = D_DISK
     93      1.16   gehenna };
     94      1.16   gehenna 
     95      1.16   gehenna const struct cdevsw ld_cdevsw = {
     96      1.72  dholland 	.d_open = ldopen,
     97      1.72  dholland 	.d_close = ldclose,
     98      1.72  dholland 	.d_read = ldread,
     99      1.72  dholland 	.d_write = ldwrite,
    100      1.72  dholland 	.d_ioctl = ldioctl,
    101      1.72  dholland 	.d_stop = nostop,
    102      1.72  dholland 	.d_tty = notty,
    103      1.72  dholland 	.d_poll = nopoll,
    104      1.72  dholland 	.d_mmap = nommap,
    105      1.72  dholland 	.d_kqfilter = nokqfilter,
    106  1.72.2.2       tls 	.d_discard = nodiscard,
    107      1.72  dholland 	.d_flag = D_DISK
    108      1.16   gehenna };
    109      1.16   gehenna 
    110      1.30   thorpej static struct	dkdriver lddkdriver = { ldstrategy, ldminphys };
    111       1.1        ad 
    112       1.1        ad void
    113       1.1        ad ldattach(struct ld_softc *sc)
    114       1.1        ad {
    115      1.37  christos 	char tbuf[9];
    116       1.1        ad 
    117      1.53        ad 	mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_VM);
    118      1.44        ad 
    119       1.7        ad 	if ((sc->sc_flags & LDF_ENABLED) == 0) {
    120      1.63      tron 		aprint_normal_dev(sc->sc_dv, "disabled\n");
    121       1.7        ad 		return;
    122       1.7        ad 	}
    123       1.7        ad 
    124       1.1        ad 	/* Initialise and attach the disk structure. */
    125      1.63      tron 	disk_init(&sc->sc_dk, device_xname(sc->sc_dv), &lddkdriver);
    126       1.1        ad 	disk_attach(&sc->sc_dk);
    127       1.1        ad 
    128       1.1        ad 	if (sc->sc_maxxfer > MAXPHYS)
    129       1.1        ad 		sc->sc_maxxfer = MAXPHYS;
    130       1.9        ad 
    131      1.19   thorpej 	/* Build synthetic geometry if necessary. */
    132      1.19   thorpej 	if (sc->sc_nheads == 0 || sc->sc_nsectors == 0 ||
    133      1.19   thorpej 	    sc->sc_ncylinders == 0) {
    134      1.28       dbj 		uint64_t ncyl;
    135      1.28       dbj 
    136      1.19   thorpej 		if (sc->sc_secperunit <= 528 * 2048)		/* 528MB */
    137      1.19   thorpej 			sc->sc_nheads = 16;
    138      1.19   thorpej 		else if (sc->sc_secperunit <= 1024 * 2048)	/* 1GB */
    139      1.19   thorpej 			sc->sc_nheads = 32;
    140      1.19   thorpej 		else if (sc->sc_secperunit <= 21504 * 2048)	/* 21GB */
    141      1.19   thorpej 			sc->sc_nheads = 64;
    142      1.19   thorpej 		else if (sc->sc_secperunit <= 43008 * 2048)	/* 42GB */
    143      1.19   thorpej 			sc->sc_nheads = 128;
    144      1.19   thorpej 		else
    145      1.19   thorpej 			sc->sc_nheads = 255;
    146      1.19   thorpej 
    147      1.19   thorpej 		sc->sc_nsectors = 63;
    148      1.28       dbj 		sc->sc_ncylinders = INT_MAX;
    149      1.35     perry 		ncyl = sc->sc_secperunit /
    150      1.19   thorpej 		    (sc->sc_nheads * sc->sc_nsectors);
    151      1.28       dbj 		if (ncyl < INT_MAX)
    152      1.28       dbj 			sc->sc_ncylinders = (int)ncyl;
    153      1.19   thorpej 	}
    154       1.1        ad 
    155      1.37  christos 	format_bytes(tbuf, sizeof(tbuf), sc->sc_secperunit *
    156       1.1        ad 	    sc->sc_secsize);
    157      1.63      tron 	aprint_normal_dev(sc->sc_dv, "%s, %d cyl, %d head, %d sec, "
    158      1.63      tron 	    "%d bytes/sect x %"PRIu64" sectors\n",
    159      1.57    cegger 	    tbuf, sc->sc_ncylinders, sc->sc_nheads,
    160       1.1        ad 	    sc->sc_nsectors, sc->sc_secsize, sc->sc_secperunit);
    161      1.68  kiyohara 	sc->sc_disksize512 = sc->sc_secperunit * sc->sc_secsize / DEV_BSIZE;
    162       1.1        ad 
    163      1.71  christos 	ld_set_geometry(sc);
    164      1.43       riz 
    165       1.1        ad 	/* Attach the device into the rnd source list. */
    166      1.63      tron 	rnd_attach_source(&sc->sc_rnd_source, device_xname(sc->sc_dv),
    167  1.72.2.1       tls 	    RND_TYPE_DISK, RND_FLAG_DEFAULT);
    168       1.1        ad 
    169      1.55  jmcneill 	/* Register with PMF */
    170      1.67  jmcneill 	if (!pmf_device_register1(sc->sc_dv, ld_suspend, NULL, ld_shutdown))
    171      1.63      tron 		aprint_error_dev(sc->sc_dv,
    172      1.55  jmcneill 		    "couldn't establish power handler\n");
    173      1.55  jmcneill 
    174      1.38      yamt 	bufq_alloc(&sc->sc_bufq, BUFQ_DISK_DEFAULT_STRAT, BUFQ_SORT_RAWBLOCK);
    175      1.30   thorpej 
    176      1.30   thorpej 	/* Discover wedges on this disk. */
    177      1.63      tron 	config_interrupts(sc->sc_dv, ld_config_interrupts);
    178       1.1        ad }
    179       1.1        ad 
    180       1.3        ad int
    181      1.37  christos ldadjqparam(struct ld_softc *sc, int xmax)
    182       1.3        ad {
    183      1.24   thorpej 	int s;
    184       1.7        ad 
    185       1.7        ad 	s = splbio();
    186      1.37  christos 	sc->sc_maxqueuecnt = xmax;
    187       1.7        ad 	splx(s);
    188       1.7        ad 
    189      1.24   thorpej 	return (0);
    190       1.7        ad }
    191       1.7        ad 
    192       1.7        ad int
    193       1.7        ad ldbegindetach(struct ld_softc *sc, int flags)
    194       1.7        ad {
    195      1.24   thorpej 	int s, rv = 0;
    196       1.7        ad 
    197       1.7        ad 	if ((sc->sc_flags & LDF_ENABLED) == 0)
    198       1.7        ad 		return (0);
    199       1.3        ad 
    200      1.66    dyoung 	rv = disk_begindetach(&sc->sc_dk, ldlastclose, sc->sc_dv, flags);
    201      1.66    dyoung 
    202      1.66    dyoung 	if (rv != 0)
    203      1.66    dyoung 		return rv;
    204       1.3        ad 
    205       1.3        ad 	s = splbio();
    206      1.24   thorpej 	sc->sc_maxqueuecnt = 0;
    207       1.7        ad 	sc->sc_flags |= LDF_DETACH;
    208      1.24   thorpej 	while (sc->sc_queuecnt > 0) {
    209      1.24   thorpej 		sc->sc_flags |= LDF_DRAIN;
    210      1.24   thorpej 		rv = tsleep(&sc->sc_queuecnt, PRIBIO, "lddrn", 0);
    211      1.24   thorpej 		if (rv)
    212      1.24   thorpej 			break;
    213      1.24   thorpej 	}
    214       1.3        ad 	splx(s);
    215       1.7        ad 
    216       1.7        ad 	return (rv);
    217       1.3        ad }
    218       1.3        ad 
    219       1.2        ad void
    220       1.7        ad ldenddetach(struct ld_softc *sc)
    221       1.2        ad {
    222      1.13  drochner 	int s, bmaj, cmaj, i, mn;
    223       1.2        ad 
    224       1.7        ad 	if ((sc->sc_flags & LDF_ENABLED) == 0)
    225       1.7        ad 		return;
    226       1.7        ad 
    227       1.2        ad 	/* Wait for commands queued with the hardware to complete. */
    228       1.2        ad 	if (sc->sc_queuecnt != 0)
    229       1.7        ad 		if (tsleep(&sc->sc_queuecnt, PRIBIO, "lddtch", 30 * hz))
    230      1.63      tron 			printf("%s: not drained\n", device_xname(sc->sc_dv));
    231       1.2        ad 
    232       1.2        ad 	/* Locate the major numbers. */
    233      1.16   gehenna 	bmaj = bdevsw_lookup_major(&ld_bdevsw);
    234      1.16   gehenna 	cmaj = cdevsw_lookup_major(&ld_cdevsw);
    235       1.2        ad 
    236       1.2        ad 	/* Kill off any queued buffers. */
    237       1.2        ad 	s = splbio();
    238      1.38      yamt 	bufq_drain(sc->sc_bufq);
    239      1.36      yamt 	splx(s);
    240      1.36      yamt 
    241      1.38      yamt 	bufq_free(sc->sc_bufq);
    242       1.2        ad 
    243       1.2        ad 	/* Nuke the vnodes for any open instances. */
    244      1.13  drochner 	for (i = 0; i < MAXPARTITIONS; i++) {
    245      1.63      tron 		mn = DISKMINOR(device_unit(sc->sc_dv), i);
    246      1.13  drochner 		vdevgone(bmaj, mn, mn, VBLK);
    247      1.13  drochner 		vdevgone(cmaj, mn, mn, VCHR);
    248      1.13  drochner 	}
    249      1.13  drochner 
    250      1.30   thorpej 	/* Delete all of our wedges. */
    251      1.30   thorpej 	dkwedge_delall(&sc->sc_dk);
    252      1.30   thorpej 
    253       1.2        ad 	/* Detach from the disk list. */
    254       1.2        ad 	disk_detach(&sc->sc_dk);
    255      1.50        ad 	disk_destroy(&sc->sc_dk);
    256       1.2        ad 
    257       1.2        ad 	/* Unhook the entropy source. */
    258       1.2        ad 	rnd_detach_source(&sc->sc_rnd_source);
    259       1.2        ad 
    260      1.56  jmcneill 	/* Deregister with PMF */
    261      1.63      tron 	pmf_device_deregister(sc->sc_dv);
    262      1.56  jmcneill 
    263      1.24   thorpej 	/*
    264      1.24   thorpej 	 * XXX We can't really flush the cache here, beceause the
    265      1.24   thorpej 	 * XXX device may already be non-existent from the controller's
    266      1.24   thorpej 	 * XXX perspective.
    267      1.24   thorpej 	 */
    268      1.24   thorpej #if 0
    269       1.2        ad 	/* Flush the device's cache. */
    270       1.2        ad 	if (sc->sc_flush != NULL)
    271      1.62    simonb 		if ((*sc->sc_flush)(sc, 0) != 0)
    272      1.70       chs 			aprint_error_dev(sc->sc_dv, "unable to flush cache\n");
    273      1.24   thorpej #endif
    274      1.61        ws 	mutex_destroy(&sc->sc_mutex);
    275       1.2        ad }
    276       1.2        ad 
    277       1.8     lukem /* ARGSUSED */
    278      1.55  jmcneill static bool
    279      1.67  jmcneill ld_suspend(device_t dev, const pmf_qual_t *qual)
    280      1.67  jmcneill {
    281      1.67  jmcneill 	return ld_shutdown(dev, 0);
    282      1.67  jmcneill }
    283      1.67  jmcneill 
    284      1.67  jmcneill /* ARGSUSED */
    285      1.67  jmcneill static bool
    286      1.55  jmcneill ld_shutdown(device_t dev, int flags)
    287       1.1        ad {
    288      1.55  jmcneill 	struct ld_softc *sc = device_private(dev);
    289       1.1        ad 
    290      1.62    simonb 	if (sc->sc_flush != NULL && (*sc->sc_flush)(sc, LDFL_POLL) != 0) {
    291      1.55  jmcneill 		printf("%s: unable to flush cache\n", device_xname(dev));
    292      1.55  jmcneill 		return false;
    293       1.1        ad 	}
    294      1.55  jmcneill 
    295      1.55  jmcneill 	return true;
    296       1.1        ad }
    297       1.1        ad 
    298       1.8     lukem /* ARGSUSED */
    299      1.29   thorpej static int
    300      1.42  christos ldopen(dev_t dev, int flags, int fmt, struct lwp *l)
    301       1.1        ad {
    302       1.1        ad 	struct ld_softc *sc;
    303      1.30   thorpej 	int error, unit, part;
    304       1.1        ad 
    305       1.1        ad 	unit = DISKUNIT(dev);
    306      1.59   tsutsui 	if ((sc = device_lookup_private(&ld_cd, unit)) == NULL)
    307       1.1        ad 		return (ENXIO);
    308       1.1        ad 	if ((sc->sc_flags & LDF_ENABLED) == 0)
    309       1.1        ad 		return (ENODEV);
    310       1.1        ad 	part = DISKPART(dev);
    311      1.30   thorpej 
    312      1.48        ad 	mutex_enter(&sc->sc_dk.dk_openlock);
    313       1.1        ad 
    314      1.22   thorpej 	if (sc->sc_dk.dk_openmask == 0) {
    315      1.22   thorpej 		/* Load the partition info if not already loaded. */
    316      1.22   thorpej 		if ((sc->sc_flags & LDF_VLABEL) == 0)
    317      1.22   thorpej 			ldgetdisklabel(sc);
    318      1.22   thorpej 	}
    319       1.1        ad 
    320       1.1        ad 	/* Check that the partition exists. */
    321       1.1        ad 	if (part != RAW_PART && (part >= sc->sc_dk.dk_label->d_npartitions ||
    322       1.1        ad 	    sc->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
    323      1.30   thorpej 		error = ENXIO;
    324      1.30   thorpej 		goto bad1;
    325       1.1        ad 	}
    326       1.1        ad 
    327       1.1        ad 	/* Ensure only one open at a time. */
    328       1.1        ad 	switch (fmt) {
    329       1.1        ad 	case S_IFCHR:
    330       1.1        ad 		sc->sc_dk.dk_copenmask |= (1 << part);
    331       1.1        ad 		break;
    332       1.1        ad 	case S_IFBLK:
    333       1.1        ad 		sc->sc_dk.dk_bopenmask |= (1 << part);
    334       1.1        ad 		break;
    335       1.1        ad 	}
    336       1.1        ad 	sc->sc_dk.dk_openmask =
    337       1.1        ad 	    sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
    338       1.1        ad 
    339      1.48        ad 	error = 0;
    340      1.30   thorpej  bad1:
    341      1.48        ad 	mutex_exit(&sc->sc_dk.dk_openlock);
    342      1.30   thorpej 	return (error);
    343       1.1        ad }
    344       1.1        ad 
    345      1.66    dyoung static int
    346      1.66    dyoung ldlastclose(device_t self)
    347      1.66    dyoung {
    348      1.66    dyoung 	struct ld_softc *sc = device_private(self);
    349      1.66    dyoung 
    350      1.66    dyoung 	if (sc->sc_flush != NULL && (*sc->sc_flush)(sc, 0) != 0)
    351      1.66    dyoung 		aprint_error_dev(self, "unable to flush cache\n");
    352      1.66    dyoung 	if ((sc->sc_flags & LDF_KLABEL) == 0)
    353      1.66    dyoung 		sc->sc_flags &= ~LDF_VLABEL;
    354      1.66    dyoung 
    355      1.66    dyoung 	return 0;
    356      1.66    dyoung }
    357      1.66    dyoung 
    358       1.8     lukem /* ARGSUSED */
    359      1.29   thorpej static int
    360      1.42  christos ldclose(dev_t dev, int flags, int fmt, struct lwp *l)
    361       1.1        ad {
    362       1.1        ad 	struct ld_softc *sc;
    363      1.48        ad 	int part, unit;
    364       1.1        ad 
    365       1.1        ad 	unit = DISKUNIT(dev);
    366       1.1        ad 	part = DISKPART(dev);
    367      1.59   tsutsui 	sc = device_lookup_private(&ld_cd, unit);
    368      1.30   thorpej 
    369      1.48        ad 	mutex_enter(&sc->sc_dk.dk_openlock);
    370       1.1        ad 
    371       1.1        ad 	switch (fmt) {
    372       1.1        ad 	case S_IFCHR:
    373       1.1        ad 		sc->sc_dk.dk_copenmask &= ~(1 << part);
    374       1.1        ad 		break;
    375       1.1        ad 	case S_IFBLK:
    376       1.1        ad 		sc->sc_dk.dk_bopenmask &= ~(1 << part);
    377       1.1        ad 		break;
    378       1.1        ad 	}
    379       1.1        ad 	sc->sc_dk.dk_openmask =
    380       1.1        ad 	    sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
    381       1.1        ad 
    382      1.66    dyoung 	if (sc->sc_dk.dk_openmask == 0)
    383      1.66    dyoung 		ldlastclose(sc->sc_dv);
    384       1.1        ad 
    385      1.48        ad 	mutex_exit(&sc->sc_dk.dk_openlock);
    386       1.1        ad 	return (0);
    387       1.1        ad }
    388       1.1        ad 
    389       1.8     lukem /* ARGSUSED */
    390      1.29   thorpej static int
    391      1.42  christos ldread(dev_t dev, struct uio *uio, int ioflag)
    392       1.1        ad {
    393       1.1        ad 
    394       1.1        ad 	return (physio(ldstrategy, NULL, dev, B_READ, ldminphys, uio));
    395       1.1        ad }
    396       1.1        ad 
    397       1.8     lukem /* ARGSUSED */
    398      1.29   thorpej static int
    399      1.42  christos ldwrite(dev_t dev, struct uio *uio, int ioflag)
    400       1.1        ad {
    401       1.1        ad 
    402       1.1        ad 	return (physio(ldstrategy, NULL, dev, B_WRITE, ldminphys, uio));
    403       1.1        ad }
    404       1.1        ad 
    405       1.8     lukem /* ARGSUSED */
    406      1.29   thorpej static int
    407      1.46  christos ldioctl(dev_t dev, u_long cmd, void *addr, int32_t flag, struct lwp *l)
    408       1.1        ad {
    409       1.1        ad 	struct ld_softc *sc;
    410      1.52   xtraeme 	int part, unit, error;
    411       1.4      fvdl #ifdef __HAVE_OLD_DISKLABEL
    412       1.6    itojun 	struct disklabel newlabel;
    413       1.4      fvdl #endif
    414       1.6    itojun 	struct disklabel *lp;
    415       1.1        ad 
    416       1.1        ad 	unit = DISKUNIT(dev);
    417       1.1        ad 	part = DISKPART(dev);
    418      1.59   tsutsui 	sc = device_lookup_private(&ld_cd, unit);
    419       1.1        ad 
    420      1.43       riz 	error = disk_ioctl(&sc->sc_dk, cmd, addr, flag, l);
    421      1.43       riz 	if (error != EPASSTHROUGH)
    422      1.43       riz 		return (error);
    423      1.43       riz 
    424      1.47      tron 	error = 0;
    425       1.1        ad 	switch (cmd) {
    426       1.1        ad 	case DIOCGDINFO:
    427       1.1        ad 		memcpy(addr, sc->sc_dk.dk_label, sizeof(struct disklabel));
    428       1.1        ad 		return (0);
    429       1.1        ad 
    430       1.4      fvdl #ifdef __HAVE_OLD_DISKLABEL
    431       1.4      fvdl 	case ODIOCGDINFO:
    432       1.4      fvdl 		newlabel = *(sc->sc_dk.dk_label);
    433       1.4      fvdl 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
    434       1.5      fvdl 			return ENOTTY;
    435       1.4      fvdl 		memcpy(addr, &newlabel, sizeof(struct olddisklabel));
    436       1.4      fvdl 		return (0);
    437       1.4      fvdl #endif
    438       1.4      fvdl 
    439       1.1        ad 	case DIOCGPART:
    440       1.1        ad 		((struct partinfo *)addr)->disklab = sc->sc_dk.dk_label;
    441       1.1        ad 		((struct partinfo *)addr)->part =
    442       1.1        ad 		    &sc->sc_dk.dk_label->d_partitions[part];
    443       1.1        ad 		break;
    444       1.1        ad 
    445       1.1        ad 	case DIOCWDINFO:
    446       1.1        ad 	case DIOCSDINFO:
    447       1.4      fvdl #ifdef __HAVE_OLD_DISKLABEL
    448       1.4      fvdl 	case ODIOCWDINFO:
    449       1.4      fvdl 	case ODIOCSDINFO:
    450       1.4      fvdl 
    451       1.4      fvdl 		if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
    452       1.4      fvdl 			memset(&newlabel, 0, sizeof newlabel);
    453       1.4      fvdl 			memcpy(&newlabel, addr, sizeof (struct olddisklabel));
    454       1.4      fvdl 			lp = &newlabel;
    455       1.4      fvdl 		} else
    456       1.4      fvdl #endif
    457       1.4      fvdl 		lp = (struct disklabel *)addr;
    458       1.4      fvdl 
    459       1.1        ad 		if ((flag & FWRITE) == 0)
    460       1.1        ad 			return (EBADF);
    461       1.1        ad 
    462      1.48        ad 		mutex_enter(&sc->sc_dk.dk_openlock);
    463       1.1        ad 		sc->sc_flags |= LDF_LABELLING;
    464       1.1        ad 
    465       1.1        ad 		error = setdisklabel(sc->sc_dk.dk_label,
    466       1.4      fvdl 		    lp, /*sc->sc_dk.dk_openmask : */0,
    467       1.1        ad 		    sc->sc_dk.dk_cpulabel);
    468       1.4      fvdl 		if (error == 0 && (cmd == DIOCWDINFO
    469       1.4      fvdl #ifdef __HAVE_OLD_DISKLABEL
    470       1.4      fvdl 		    || cmd == ODIOCWDINFO
    471       1.4      fvdl #endif
    472       1.4      fvdl 		    ))
    473       1.1        ad 			error = writedisklabel(
    474      1.35     perry 			    MAKEDISKDEV(major(dev), DISKUNIT(dev), RAW_PART),
    475      1.35     perry 			    ldstrategy, sc->sc_dk.dk_label,
    476       1.1        ad 			    sc->sc_dk.dk_cpulabel);
    477       1.1        ad 
    478       1.1        ad 		sc->sc_flags &= ~LDF_LABELLING;
    479      1.48        ad 		mutex_exit(&sc->sc_dk.dk_openlock);
    480       1.1        ad 		break;
    481       1.1        ad 
    482      1.22   thorpej 	case DIOCKLABEL:
    483      1.22   thorpej 		if ((flag & FWRITE) == 0)
    484      1.22   thorpej 			return (EBADF);
    485      1.22   thorpej 		if (*(int *)addr)
    486      1.22   thorpej 			sc->sc_flags |= LDF_KLABEL;
    487      1.22   thorpej 		else
    488      1.22   thorpej 			sc->sc_flags &= ~LDF_KLABEL;
    489      1.22   thorpej 		break;
    490      1.22   thorpej 
    491       1.1        ad 	case DIOCWLABEL:
    492       1.1        ad 		if ((flag & FWRITE) == 0)
    493       1.1        ad 			return (EBADF);
    494       1.1        ad 		if (*(int *)addr)
    495       1.1        ad 			sc->sc_flags |= LDF_WLABEL;
    496       1.1        ad 		else
    497       1.1        ad 			sc->sc_flags &= ~LDF_WLABEL;
    498       1.1        ad 		break;
    499       1.1        ad 
    500       1.1        ad 	case DIOCGDEFLABEL:
    501       1.1        ad 		ldgetdefaultlabel(sc, (struct disklabel *)addr);
    502       1.1        ad 		break;
    503       1.4      fvdl 
    504       1.4      fvdl #ifdef __HAVE_OLD_DISKLABEL
    505       1.4      fvdl 	case ODIOCGDEFLABEL:
    506       1.4      fvdl 		ldgetdefaultlabel(sc, &newlabel);
    507       1.4      fvdl 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
    508       1.5      fvdl 			return ENOTTY;
    509       1.4      fvdl 		memcpy(addr, &newlabel, sizeof (struct olddisklabel));
    510       1.4      fvdl 		break;
    511       1.4      fvdl #endif
    512       1.1        ad 
    513      1.32   thorpej 	case DIOCCACHESYNC:
    514      1.32   thorpej 		/*
    515      1.32   thorpej 		 * XXX Do we really need to care about having a writable
    516      1.32   thorpej 		 * file descriptor here?
    517      1.32   thorpej 		 */
    518      1.32   thorpej 		if ((flag & FWRITE) == 0)
    519      1.32   thorpej 			error = EBADF;
    520      1.32   thorpej 		else if (sc->sc_flush)
    521      1.62    simonb 			error = (*sc->sc_flush)(sc, 0);
    522      1.32   thorpej 		else
    523      1.32   thorpej 			error = 0;	/* XXX Error out instead? */
    524      1.32   thorpej 		break;
    525      1.32   thorpej 
    526      1.30   thorpej 	case DIOCAWEDGE:
    527      1.30   thorpej 	    {
    528      1.30   thorpej 	    	struct dkwedge_info *dkw = (void *) addr;
    529      1.30   thorpej 
    530      1.30   thorpej 		if ((flag & FWRITE) == 0)
    531      1.30   thorpej 			return (EBADF);
    532      1.30   thorpej 
    533      1.30   thorpej 		/* If the ioctl happens here, the parent is us. */
    534      1.63      tron 		strlcpy(dkw->dkw_parent, device_xname(sc->sc_dv),
    535      1.57    cegger 			sizeof(dkw->dkw_parent));
    536      1.30   thorpej 		return (dkwedge_add(dkw));
    537      1.30   thorpej 	    }
    538      1.35     perry 
    539      1.30   thorpej 	case DIOCDWEDGE:
    540      1.30   thorpej 	    {
    541      1.30   thorpej 	    	struct dkwedge_info *dkw = (void *) addr;
    542      1.30   thorpej 
    543      1.30   thorpej 		if ((flag & FWRITE) == 0)
    544      1.30   thorpej 			return (EBADF);
    545      1.30   thorpej 
    546      1.30   thorpej 		/* If the ioctl happens here, the parent is us. */
    547      1.63      tron 		strlcpy(dkw->dkw_parent, device_xname(sc->sc_dv),
    548      1.57    cegger 			sizeof(dkw->dkw_parent));
    549      1.30   thorpej 		return (dkwedge_del(dkw));
    550      1.30   thorpej 	    }
    551      1.35     perry 
    552      1.30   thorpej 	case DIOCLWEDGES:
    553      1.30   thorpej 	    {
    554      1.30   thorpej 	    	struct dkwedge_list *dkwl = (void *) addr;
    555      1.30   thorpej 
    556      1.39  christos 		return (dkwedge_list(&sc->sc_dk, dkwl, l));
    557      1.30   thorpej 	    }
    558      1.51   xtraeme 	case DIOCGSTRATEGY:
    559      1.51   xtraeme 	    {
    560      1.51   xtraeme 		struct disk_strategy *dks = (void *)addr;
    561      1.51   xtraeme 
    562      1.52   xtraeme 		mutex_enter(&sc->sc_mutex);
    563      1.51   xtraeme 		strlcpy(dks->dks_name, bufq_getstrategyname(sc->sc_bufq),
    564      1.51   xtraeme 		    sizeof(dks->dks_name));
    565      1.52   xtraeme 		mutex_exit(&sc->sc_mutex);
    566      1.51   xtraeme 		dks->dks_paramlen = 0;
    567      1.51   xtraeme 
    568      1.51   xtraeme 		return 0;
    569      1.51   xtraeme 	    }
    570      1.51   xtraeme 	case DIOCSSTRATEGY:
    571      1.51   xtraeme 	    {
    572      1.51   xtraeme 		struct disk_strategy *dks = (void *)addr;
    573      1.51   xtraeme 		struct bufq_state *new, *old;
    574      1.30   thorpej 
    575      1.51   xtraeme 		if ((flag & FWRITE) == 0)
    576      1.51   xtraeme 			return EPERM;
    577      1.51   xtraeme 
    578      1.51   xtraeme 		if (dks->dks_param != NULL)
    579      1.51   xtraeme 			return EINVAL;
    580      1.51   xtraeme 
    581      1.51   xtraeme 		dks->dks_name[sizeof(dks->dks_name) - 1] = 0; /* ensure term */
    582      1.51   xtraeme 		error = bufq_alloc(&new, dks->dks_name,
    583      1.51   xtraeme 		    BUFQ_EXACT|BUFQ_SORT_RAWBLOCK);
    584      1.51   xtraeme 		if (error)
    585      1.51   xtraeme 			return error;
    586      1.51   xtraeme 
    587      1.52   xtraeme 		mutex_enter(&sc->sc_mutex);
    588      1.51   xtraeme 		old = sc->sc_bufq;
    589      1.51   xtraeme 		bufq_move(new, old);
    590      1.51   xtraeme 		sc->sc_bufq = new;
    591      1.52   xtraeme 		mutex_exit(&sc->sc_mutex);
    592      1.51   xtraeme 		bufq_free(old);
    593      1.51   xtraeme 
    594      1.51   xtraeme 		return 0;
    595      1.51   xtraeme 	    }
    596       1.1        ad 	default:
    597       1.1        ad 		error = ENOTTY;
    598       1.1        ad 		break;
    599       1.1        ad 	}
    600       1.1        ad 
    601       1.1        ad 	return (error);
    602       1.1        ad }
    603       1.1        ad 
    604      1.29   thorpej static void
    605       1.1        ad ldstrategy(struct buf *bp)
    606       1.1        ad {
    607       1.1        ad 	struct ld_softc *sc;
    608      1.23   thorpej 	struct disklabel *lp;
    609      1.23   thorpej 	daddr_t blkno;
    610      1.23   thorpej 	int s, part;
    611       1.1        ad 
    612      1.59   tsutsui 	sc = device_lookup_private(&ld_cd, DISKUNIT(bp->b_dev));
    613      1.23   thorpej 	part = DISKPART(bp->b_dev);
    614       1.1        ad 
    615       1.7        ad 	if ((sc->sc_flags & LDF_DETACH) != 0) {
    616       1.2        ad 		bp->b_error = EIO;
    617      1.49        ad 		goto done;
    618       1.2        ad 	}
    619       1.2        ad 
    620       1.1        ad 	lp = sc->sc_dk.dk_label;
    621       1.1        ad 
    622       1.1        ad 	/*
    623       1.1        ad 	 * The transfer must be a whole number of blocks and the offset must
    624       1.1        ad 	 * not be negative.
    625       1.1        ad 	 */
    626       1.1        ad 	if ((bp->b_bcount % lp->d_secsize) != 0 || bp->b_blkno < 0) {
    627      1.23   thorpej 		bp->b_error = EINVAL;
    628      1.49        ad 		goto done;
    629       1.1        ad 	}
    630       1.1        ad 
    631      1.23   thorpej 	/* If it's a null transfer, return immediately. */
    632      1.23   thorpej 	if (bp->b_bcount == 0)
    633      1.23   thorpej 		goto done;
    634       1.1        ad 
    635       1.1        ad 	/*
    636       1.1        ad 	 * Do bounds checking and adjust the transfer.  If error, process.
    637       1.1        ad 	 * If past the end of partition, just return.
    638       1.1        ad 	 */
    639      1.68  kiyohara 	if (part == RAW_PART) {
    640      1.68  kiyohara 		if (bounds_check_with_mediasize(bp, DEV_BSIZE,
    641      1.68  kiyohara 		    sc->sc_disksize512) <= 0)
    642      1.68  kiyohara 			goto done;
    643      1.68  kiyohara 	} else {
    644      1.68  kiyohara 		if (bounds_check_with_label(&sc->sc_dk, bp,
    645      1.68  kiyohara 		    (sc->sc_flags & (LDF_WLABEL | LDF_LABELLING)) != 0) <= 0)
    646      1.68  kiyohara 			goto done;
    647       1.1        ad 	}
    648       1.1        ad 
    649       1.1        ad 	/*
    650      1.23   thorpej 	 * Convert the block number to absolute and put it in terms
    651      1.23   thorpej 	 * of the device's logical block size.
    652       1.1        ad 	 */
    653      1.23   thorpej 	if (lp->d_secsize == DEV_BSIZE)
    654      1.23   thorpej 		blkno = bp->b_blkno;
    655      1.23   thorpej 	else if (lp->d_secsize > DEV_BSIZE)
    656      1.23   thorpej 		blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
    657       1.1        ad 	else
    658      1.23   thorpej 		blkno = bp->b_blkno * (DEV_BSIZE / lp->d_secsize);
    659       1.1        ad 
    660      1.11   thorpej 	if (part != RAW_PART)
    661      1.23   thorpej 		blkno += lp->d_partitions[part].p_offset;
    662      1.23   thorpej 
    663      1.23   thorpej 	bp->b_rawblkno = blkno;
    664       1.1        ad 
    665       1.1        ad 	s = splbio();
    666      1.44        ad 	ldstart(sc, bp);
    667       1.1        ad 	splx(s);
    668      1.23   thorpej 	return;
    669      1.23   thorpej 
    670      1.23   thorpej  done:
    671      1.23   thorpej 	bp->b_resid = bp->b_bcount;
    672      1.23   thorpej 	biodone(bp);
    673      1.23   thorpej }
    674      1.23   thorpej 
    675      1.23   thorpej static void
    676      1.44        ad ldstart(struct ld_softc *sc, struct buf *bp)
    677      1.23   thorpej {
    678      1.23   thorpej 	int error;
    679       1.1        ad 
    680      1.44        ad 	mutex_enter(&sc->sc_mutex);
    681      1.44        ad 
    682      1.44        ad 	if (bp != NULL)
    683      1.64      yamt 		bufq_put(sc->sc_bufq, bp);
    684      1.44        ad 
    685      1.23   thorpej 	while (sc->sc_queuecnt < sc->sc_maxqueuecnt) {
    686      1.23   thorpej 		/* See if there is work to do. */
    687      1.64      yamt 		if ((bp = bufq_peek(sc->sc_bufq)) == NULL)
    688      1.23   thorpej 			break;
    689      1.23   thorpej 
    690      1.23   thorpej 		disk_busy(&sc->sc_dk);
    691      1.23   thorpej 		sc->sc_queuecnt++;
    692      1.23   thorpej 
    693      1.23   thorpej 		if (__predict_true((error = (*sc->sc_start)(sc, bp)) == 0)) {
    694      1.23   thorpej 			/*
    695      1.23   thorpej 			 * The back-end is running the job; remove it from
    696      1.23   thorpej 			 * the queue.
    697      1.23   thorpej 			 */
    698      1.64      yamt 			(void) bufq_get(sc->sc_bufq);
    699      1.23   thorpej 		} else  {
    700      1.23   thorpej 			disk_unbusy(&sc->sc_dk, 0, (bp->b_flags & B_READ));
    701      1.23   thorpej 			sc->sc_queuecnt--;
    702      1.23   thorpej 			if (error == EAGAIN) {
    703      1.23   thorpej 				/*
    704      1.23   thorpej 				 * Temporary resource shortage in the
    705      1.23   thorpej 				 * back-end; just defer the job until
    706      1.23   thorpej 				 * later.
    707      1.23   thorpej 				 *
    708      1.23   thorpej 				 * XXX We might consider a watchdog timer
    709      1.23   thorpej 				 * XXX to make sure we are kicked into action.
    710      1.23   thorpej 				 */
    711      1.23   thorpej 				break;
    712      1.23   thorpej 			} else {
    713      1.64      yamt 				(void) bufq_get(sc->sc_bufq);
    714      1.23   thorpej 				bp->b_error = error;
    715      1.23   thorpej 				bp->b_resid = bp->b_bcount;
    716      1.44        ad 				mutex_exit(&sc->sc_mutex);
    717      1.23   thorpej 				biodone(bp);
    718      1.44        ad 				mutex_enter(&sc->sc_mutex);
    719      1.23   thorpej 			}
    720      1.23   thorpej 		}
    721       1.1        ad 	}
    722      1.44        ad 
    723      1.44        ad 	mutex_exit(&sc->sc_mutex);
    724       1.1        ad }
    725       1.1        ad 
    726       1.1        ad void
    727       1.1        ad lddone(struct ld_softc *sc, struct buf *bp)
    728       1.1        ad {
    729       1.1        ad 
    730      1.49        ad 	if (bp->b_error != 0) {
    731       1.1        ad 		diskerr(bp, "ld", "error", LOG_PRINTF, 0, sc->sc_dk.dk_label);
    732       1.1        ad 		printf("\n");
    733       1.1        ad 	}
    734       1.1        ad 
    735      1.18       mrg 	disk_unbusy(&sc->sc_dk, bp->b_bcount - bp->b_resid,
    736      1.18       mrg 	    (bp->b_flags & B_READ));
    737       1.1        ad 	rnd_add_uint32(&sc->sc_rnd_source, bp->b_rawblkno);
    738       1.1        ad 	biodone(bp);
    739       1.1        ad 
    740      1.44        ad 	mutex_enter(&sc->sc_mutex);
    741       1.7        ad 	if (--sc->sc_queuecnt <= sc->sc_maxqueuecnt) {
    742      1.24   thorpej 		if ((sc->sc_flags & LDF_DRAIN) != 0) {
    743      1.24   thorpej 			sc->sc_flags &= ~LDF_DRAIN;
    744       1.7        ad 			wakeup(&sc->sc_queuecnt);
    745      1.24   thorpej 		}
    746      1.44        ad 		mutex_exit(&sc->sc_mutex);
    747      1.44        ad 		ldstart(sc, NULL);
    748      1.44        ad 	} else
    749      1.44        ad 		mutex_exit(&sc->sc_mutex);
    750       1.1        ad }
    751       1.1        ad 
    752      1.29   thorpej static int
    753       1.1        ad ldsize(dev_t dev)
    754       1.1        ad {
    755       1.1        ad 	struct ld_softc *sc;
    756       1.1        ad 	int part, unit, omask, size;
    757       1.1        ad 
    758       1.1        ad 	unit = DISKUNIT(dev);
    759      1.59   tsutsui 	if ((sc = device_lookup_private(&ld_cd, unit)) == NULL)
    760       1.1        ad 		return (ENODEV);
    761       1.1        ad 	if ((sc->sc_flags & LDF_ENABLED) == 0)
    762       1.1        ad 		return (ENODEV);
    763       1.1        ad 	part = DISKPART(dev);
    764       1.1        ad 
    765       1.1        ad 	omask = sc->sc_dk.dk_openmask & (1 << part);
    766       1.1        ad 
    767       1.1        ad 	if (omask == 0 && ldopen(dev, 0, S_IFBLK, NULL) != 0)
    768       1.1        ad 		return (-1);
    769       1.1        ad 	else if (sc->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
    770       1.1        ad 		size = -1;
    771       1.1        ad 	else
    772       1.1        ad 		size = sc->sc_dk.dk_label->d_partitions[part].p_size *
    773       1.1        ad 		    (sc->sc_dk.dk_label->d_secsize / DEV_BSIZE);
    774       1.1        ad 	if (omask == 0 && ldclose(dev, 0, S_IFBLK, NULL) != 0)
    775       1.1        ad 		return (-1);
    776       1.1        ad 
    777       1.1        ad 	return (size);
    778       1.1        ad }
    779       1.1        ad 
    780       1.1        ad /*
    781       1.1        ad  * Load the label information from the specified device.
    782       1.1        ad  */
    783       1.1        ad static void
    784       1.1        ad ldgetdisklabel(struct ld_softc *sc)
    785       1.1        ad {
    786       1.1        ad 	const char *errstring;
    787       1.1        ad 
    788       1.1        ad 	ldgetdefaultlabel(sc, sc->sc_dk.dk_label);
    789       1.1        ad 
    790       1.1        ad 	/* Call the generic disklabel extraction routine. */
    791      1.63      tron 	errstring = readdisklabel(MAKEDISKDEV(0, device_unit(sc->sc_dv),
    792      1.40   thorpej 	    RAW_PART), ldstrategy, sc->sc_dk.dk_label, sc->sc_dk.dk_cpulabel);
    793       1.1        ad 	if (errstring != NULL)
    794      1.63      tron 		printf("%s: %s\n", device_xname(sc->sc_dv), errstring);
    795      1.22   thorpej 
    796      1.22   thorpej 	/* In-core label now valid. */
    797      1.22   thorpej 	sc->sc_flags |= LDF_VLABEL;
    798       1.1        ad }
    799       1.1        ad 
    800       1.1        ad /*
    801       1.1        ad  * Construct a ficticious label.
    802       1.1        ad  */
    803       1.1        ad static void
    804       1.1        ad ldgetdefaultlabel(struct ld_softc *sc, struct disklabel *lp)
    805       1.1        ad {
    806       1.1        ad 
    807       1.1        ad 	memset(lp, 0, sizeof(struct disklabel));
    808       1.1        ad 
    809       1.1        ad 	lp->d_secsize = sc->sc_secsize;
    810       1.1        ad 	lp->d_ntracks = sc->sc_nheads;
    811       1.1        ad 	lp->d_nsectors = sc->sc_nsectors;
    812       1.1        ad 	lp->d_ncylinders = sc->sc_ncylinders;
    813       1.1        ad 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
    814       1.1        ad 	lp->d_type = DTYPE_LD;
    815      1.21    itojun 	strlcpy(lp->d_typename, "unknown", sizeof(lp->d_typename));
    816      1.21    itojun 	strlcpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
    817       1.1        ad 	lp->d_secperunit = sc->sc_secperunit;
    818       1.1        ad 	lp->d_rpm = 7200;
    819       1.1        ad 	lp->d_interleave = 1;
    820       1.1        ad 	lp->d_flags = 0;
    821       1.1        ad 
    822       1.1        ad 	lp->d_partitions[RAW_PART].p_offset = 0;
    823       1.1        ad 	lp->d_partitions[RAW_PART].p_size =
    824       1.1        ad 	    lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
    825       1.1        ad 	lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
    826       1.1        ad 	lp->d_npartitions = RAW_PART + 1;
    827       1.1        ad 
    828       1.1        ad 	lp->d_magic = DISKMAGIC;
    829       1.1        ad 	lp->d_magic2 = DISKMAGIC;
    830       1.1        ad 	lp->d_checksum = dkcksum(lp);
    831       1.1        ad }
    832       1.1        ad 
    833       1.1        ad /*
    834       1.1        ad  * Take a dump.
    835       1.1        ad  */
    836      1.29   thorpej static int
    837      1.46  christos lddump(dev_t dev, daddr_t blkno, void *vav, size_t size)
    838       1.1        ad {
    839      1.46  christos 	char *va = vav;
    840       1.1        ad 	struct ld_softc *sc;
    841       1.1        ad 	struct disklabel *lp;
    842       1.1        ad 	int unit, part, nsects, sectoff, towrt, nblk, maxblkcnt, rv;
    843       1.1        ad 	static int dumping;
    844       1.1        ad 
    845       1.1        ad 	unit = DISKUNIT(dev);
    846      1.59   tsutsui 	if ((sc = device_lookup_private(&ld_cd, unit)) == NULL)
    847       1.1        ad 		return (ENXIO);
    848       1.1        ad 	if ((sc->sc_flags & LDF_ENABLED) == 0)
    849       1.1        ad 		return (ENODEV);
    850       1.3        ad 	if (sc->sc_dump == NULL)
    851       1.3        ad 		return (ENXIO);
    852       1.3        ad 
    853       1.3        ad 	/* Check if recursive dump; if so, punt. */
    854       1.3        ad 	if (dumping)
    855       1.3        ad 		return (EFAULT);
    856       1.3        ad 	dumping = 1;
    857       1.1        ad 
    858       1.1        ad 	/* Convert to disk sectors.  Request must be a multiple of size. */
    859       1.3        ad 	part = DISKPART(dev);
    860       1.1        ad 	lp = sc->sc_dk.dk_label;
    861       1.1        ad 	if ((size % lp->d_secsize) != 0)
    862       1.1        ad 		return (EFAULT);
    863       1.1        ad 	towrt = size / lp->d_secsize;
    864       1.1        ad 	blkno = dbtob(blkno) / lp->d_secsize;	/* blkno in DEV_BSIZE units */
    865       1.1        ad 
    866       1.1        ad 	nsects = lp->d_partitions[part].p_size;
    867       1.1        ad 	sectoff = lp->d_partitions[part].p_offset;
    868       1.1        ad 
    869       1.1        ad 	/* Check transfer bounds against partition size. */
    870       1.1        ad 	if ((blkno < 0) || ((blkno + towrt) > nsects))
    871       1.1        ad 		return (EINVAL);
    872       1.1        ad 
    873       1.1        ad 	/* Offset block number to start of partition. */
    874       1.1        ad 	blkno += sectoff;
    875       1.1        ad 
    876       1.1        ad 	/* Start dumping and return when done. */
    877       1.3        ad 	maxblkcnt = sc->sc_maxxfer / sc->sc_secsize - 1;
    878       1.1        ad 	while (towrt > 0) {
    879       1.3        ad 		nblk = min(maxblkcnt, towrt);
    880       1.1        ad 
    881       1.1        ad 		if ((rv = (*sc->sc_dump)(sc, va, blkno, nblk)) != 0)
    882       1.1        ad 			return (rv);
    883       1.1        ad 
    884       1.1        ad 		towrt -= nblk;
    885       1.1        ad 		blkno += nblk;
    886       1.1        ad 		va += nblk * sc->sc_secsize;
    887       1.1        ad 	}
    888       1.1        ad 
    889       1.1        ad 	dumping = 0;
    890       1.1        ad 	return (0);
    891       1.1        ad }
    892       1.1        ad 
    893       1.1        ad /*
    894       1.1        ad  * Adjust the size of a transfer.
    895       1.1        ad  */
    896       1.1        ad static void
    897       1.1        ad ldminphys(struct buf *bp)
    898       1.1        ad {
    899       1.1        ad 	struct ld_softc *sc;
    900       1.1        ad 
    901      1.59   tsutsui 	sc = device_lookup_private(&ld_cd, DISKUNIT(bp->b_dev));
    902       1.1        ad 
    903       1.1        ad 	if (bp->b_bcount > sc->sc_maxxfer)
    904       1.1        ad 		bp->b_bcount = sc->sc_maxxfer;
    905       1.1        ad 	minphys(bp);
    906       1.1        ad }
    907      1.43       riz 
    908      1.43       riz static void
    909      1.71  christos ld_set_geometry(struct ld_softc *ld)
    910      1.43       riz {
    911      1.71  christos 	struct disk_geom *dg = &ld->sc_dk.dk_geom;
    912      1.43       riz 
    913      1.71  christos 	memset(dg, 0, sizeof(*dg));
    914      1.43       riz 
    915      1.71  christos 	dg->dg_secperunit = ld->sc_secperunit;
    916      1.71  christos 	dg->dg_secsize = ld->sc_secsize;
    917      1.71  christos 	dg->dg_nsectors = ld->sc_nsectors;
    918      1.71  christos 	dg->dg_ntracks = ld->sc_nheads;
    919      1.71  christos 	dg->dg_ncylinders = ld->sc_ncylinders;
    920      1.43       riz 
    921      1.71  christos 	disk_set_info(ld->sc_dv, &ld->sc_dk, NULL);
    922      1.43       riz }
    923      1.45       riz 
    924      1.45       riz static void
    925      1.65    cegger ld_config_interrupts(device_t d)
    926      1.45       riz {
    927      1.60      cube 	struct ld_softc *sc = device_private(d);
    928      1.45       riz 	dkwedge_discover(&sc->sc_dk);
    929      1.45       riz }
    930