Home | History | Annotate | Line # | Download | only in scsipi
sd.c revision 1.269.6.3
      1 /*	$NetBSD: sd.c,v 1.269.6.3 2008/04/06 09:58:51 mjf Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998, 2003, 2004 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Charles M. Hannum.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Originally written by Julian Elischer (julian (at) dialix.oz.au)
     41  * for TRW Financial Systems for use under the MACH(2.5) operating system.
     42  *
     43  * TRW Financial Systems, in accordance with their agreement with Carnegie
     44  * Mellon University, makes this software available to CMU to distribute
     45  * or use in any manner that they see fit as long as this message is kept with
     46  * the software. For this reason TFS also grants any other persons or
     47  * organisations permission to use or modify this software.
     48  *
     49  * TFS supplies this software to be publicly redistributed
     50  * on the understanding that TFS is not responsible for the correct
     51  * functioning of this software in any circumstances.
     52  *
     53  * Ported to run under 386BSD by Julian Elischer (julian (at) dialix.oz.au) Sept 1992
     54  */
     55 
     56 #include <sys/cdefs.h>
     57 __KERNEL_RCSID(0, "$NetBSD: sd.c,v 1.269.6.3 2008/04/06 09:58:51 mjf Exp $");
     58 
     59 #include "opt_scsi.h"
     60 #include "rnd.h"
     61 
     62 #include <sys/param.h>
     63 #include <sys/systm.h>
     64 #include <sys/kernel.h>
     65 #include <sys/file.h>
     66 #include <sys/stat.h>
     67 #include <sys/ioctl.h>
     68 #include <sys/scsiio.h>
     69 #include <sys/buf.h>
     70 #include <sys/bufq.h>
     71 #include <sys/uio.h>
     72 #include <sys/malloc.h>
     73 #include <sys/errno.h>
     74 #include <sys/device.h>
     75 #include <sys/disklabel.h>
     76 #include <sys/disk.h>
     77 #include <sys/proc.h>
     78 #include <sys/conf.h>
     79 #include <sys/vnode.h>
     80 #if NRND > 0
     81 #include <sys/rnd.h>
     82 #endif
     83 
     84 #include <dev/scsipi/scsi_spc.h>
     85 #include <dev/scsipi/scsipi_all.h>
     86 #include <dev/scsipi/scsi_all.h>
     87 #include <dev/scsipi/scsipi_disk.h>
     88 #include <dev/scsipi/scsi_disk.h>
     89 #include <dev/scsipi/scsiconf.h>
     90 #include <dev/scsipi/scsipi_base.h>
     91 #include <dev/scsipi/sdvar.h>
     92 
     93 #include <prop/proplib.h>
     94 
     95 #define	SDUNIT(dev)			DISKUNIT(dev)
     96 #define	SDPART(dev)			DISKPART(dev)
     97 #define	SDMINOR(unit, part)		DISKMINOR(unit, part)
     98 #define	MAKESDDEV(maj, unit, part)	MAKEDISKDEV(maj, unit, part)
     99 
    100 #define	SDLABELDEV(dev)	(MAKESDDEV(major(dev), SDUNIT(dev), RAW_PART))
    101 
    102 #define	SD_DEFAULT_BLKSIZE	512
    103 
    104 static void	sdminphys(struct buf *);
    105 static void	sdgetdefaultlabel(struct sd_softc *, struct disklabel *);
    106 static int	sdgetdisklabel(struct sd_softc *);
    107 static void	sdstart(struct scsipi_periph *);
    108 static void	sdrestart(void *);
    109 static void	sddone(struct scsipi_xfer *, int);
    110 static bool	sd_suspend(device_t PMF_FN_PROTO);
    111 static void	sd_shutdown(void *);
    112 static int	sd_interpret_sense(struct scsipi_xfer *);
    113 
    114 static int	sd_mode_sense(struct sd_softc *, u_int8_t, void *, size_t, int,
    115 		    int, int *);
    116 static int	sd_mode_select(struct sd_softc *, u_int8_t, void *, size_t, int,
    117 		    int);
    118 static int	sd_validate_blksize(struct scsipi_periph *, int);
    119 static u_int64_t sd_read_capacity(struct scsipi_periph *, int *, int flags);
    120 static int	sd_get_simplifiedparms(struct sd_softc *, struct disk_parms *,
    121 		    int);
    122 static int	sd_get_capacity(struct sd_softc *, struct disk_parms *, int);
    123 static int	sd_get_parms(struct sd_softc *, struct disk_parms *, int);
    124 static int	sd_get_parms_page4(struct sd_softc *, struct disk_parms *,
    125 		    int);
    126 static int	sd_get_parms_page5(struct sd_softc *, struct disk_parms *,
    127 		    int);
    128 
    129 static int	sd_flush(struct sd_softc *, int);
    130 static int	sd_getcache(struct sd_softc *, int *);
    131 static int	sd_setcache(struct sd_softc *, int);
    132 
    133 static int	sdmatch(struct device *, struct cfdata *, void *);
    134 static void	sdattach(struct device *, struct device *, void *);
    135 static int	sdactivate(struct device *, enum devact);
    136 static int	sddetach(struct device *, int);
    137 static void	sd_set_properties(struct sd_softc *);
    138 
    139 CFATTACH_DECL(sd, sizeof(struct sd_softc), sdmatch, sdattach, sddetach,
    140     sdactivate);
    141 
    142 extern struct cfdriver sd_cd;
    143 
    144 static const struct scsipi_inquiry_pattern sd_patterns[] = {
    145 	{T_DIRECT, T_FIXED,
    146 	 "",         "",                 ""},
    147 	{T_DIRECT, T_REMOV,
    148 	 "",         "",                 ""},
    149 	{T_OPTICAL, T_FIXED,
    150 	 "",         "",                 ""},
    151 	{T_OPTICAL, T_REMOV,
    152 	 "",         "",                 ""},
    153 	{T_SIMPLE_DIRECT, T_FIXED,
    154 	 "",         "",                 ""},
    155 	{T_SIMPLE_DIRECT, T_REMOV,
    156 	 "",         "",                 ""},
    157 };
    158 
    159 static dev_type_open(sdopen);
    160 static dev_type_close(sdclose);
    161 static dev_type_read(sdread);
    162 static dev_type_write(sdwrite);
    163 static dev_type_ioctl(sdioctl);
    164 static dev_type_strategy(sdstrategy);
    165 static dev_type_dump(sddump);
    166 static dev_type_size(sdsize);
    167 
    168 const struct bdevsw sd_bdevsw = {
    169 	sdopen, sdclose, sdstrategy, sdioctl, sddump, sdsize, D_DISK
    170 };
    171 
    172 const struct cdevsw sd_cdevsw = {
    173 	sdopen, sdclose, sdread, sdwrite, sdioctl,
    174 	nostop, notty, nopoll, nommap, nokqfilter, D_DISK
    175 };
    176 
    177 static struct dkdriver sddkdriver = { sdstrategy, sdminphys };
    178 
    179 static const struct scsipi_periphsw sd_switch = {
    180 	sd_interpret_sense,	/* check our error handler first */
    181 	sdstart,		/* have a queue, served by this */
    182 	NULL,			/* have no async handler */
    183 	sddone,			/* deal with stats at interrupt time */
    184 };
    185 
    186 struct sd_mode_sense_data {
    187 	/*
    188 	 * XXX
    189 	 * We are not going to parse this as-is -- it just has to be large
    190 	 * enough.
    191 	 */
    192 	union {
    193 		struct scsi_mode_parameter_header_6 small;
    194 		struct scsi_mode_parameter_header_10 big;
    195 	} header;
    196 	struct scsi_general_block_descriptor blk_desc;
    197 	union scsi_disk_pages pages;
    198 };
    199 
    200 /*
    201  * The routine called by the low level scsi routine when it discovers
    202  * A device suitable for this driver
    203  */
    204 static int
    205 sdmatch(struct device *parent, struct cfdata *match,
    206     void *aux)
    207 {
    208 	struct scsipibus_attach_args *sa = aux;
    209 	int priority;
    210 
    211 	(void)scsipi_inqmatch(&sa->sa_inqbuf,
    212 	    sd_patterns, sizeof(sd_patterns) / sizeof(sd_patterns[0]),
    213 	    sizeof(sd_patterns[0]), &priority);
    214 
    215 	return (priority);
    216 }
    217 
    218 /*
    219  * Attach routine common to atapi & scsi.
    220  */
    221 static void
    222 sdattach(struct device *parent, struct device *self, void *aux)
    223 {
    224 	struct sd_softc *sd = device_private(self);
    225 	struct scsipibus_attach_args *sa = aux;
    226 	struct scsipi_periph *periph = sa->sa_periph;
    227 	int error, result;
    228 	struct disk_parms *dp = &sd->params;
    229 	char pbuf[9];
    230 	int i;
    231 	uint16_t np;
    232 	int cmajor, bmajor;
    233 
    234 	SC_DEBUG(periph, SCSIPI_DB2, ("sdattach: "));
    235 
    236 	sd->type = (sa->sa_inqbuf.type & SID_TYPE);
    237 	strncpy(sd->name, sa->sa_inqbuf.product, sizeof(sd->name));
    238 	if (sd->type == T_SIMPLE_DIRECT)
    239 		periph->periph_quirks |= PQUIRK_ONLYBIG | PQUIRK_NOBIGMODESENSE;
    240 
    241 	if (scsipi_periph_bustype(sa->sa_periph) == SCSIPI_BUSTYPE_SCSI &&
    242 	    periph->periph_version == 0)
    243 		sd->flags |= SDF_ANCIENT;
    244 
    245 	bufq_alloc(&sd->buf_queue, BUFQ_DISK_DEFAULT_STRAT, BUFQ_SORT_RAWBLOCK);
    246 
    247 	callout_init(&sd->sc_callout, 0);
    248 
    249 	/*
    250 	 * Store information needed to contact our base driver
    251 	 */
    252 	sd->sc_periph = periph;
    253 
    254 	periph->periph_dev = &sd->sc_dev;
    255 	periph->periph_switch = &sd_switch;
    256 
    257         /*
    258          * Increase our openings to the maximum-per-periph
    259          * supported by the adapter.  This will either be
    260          * clamped down or grown by the adapter if necessary.
    261          */
    262 	periph->periph_openings =
    263 	    SCSIPI_CHAN_MAX_PERIPH(periph->periph_channel);
    264 	periph->periph_flags |= PERIPH_GROW_OPENINGS;
    265 
    266 	/*
    267 	 * Initialize and attach the disk structure.
    268 	 */
    269 	disk_init(&sd->sc_dk, sd->sc_dev.dv_xname, &sddkdriver);
    270 	disk_attach(&sd->sc_dk);
    271 
    272 	/*
    273 	 * Use the subdriver to request information regarding the drive.
    274 	 */
    275 	aprint_naive("\n");
    276 	aprint_normal("\n");
    277 
    278 	error = scsipi_test_unit_ready(periph,
    279 	    XS_CTL_DISCOVERY | XS_CTL_IGNORE_ILLEGAL_REQUEST |
    280 	    XS_CTL_IGNORE_MEDIA_CHANGE | XS_CTL_SILENT_NODEV);
    281 
    282 	if (error)
    283 		result = SDGP_RESULT_OFFLINE;
    284 	else
    285 		result = sd_get_parms(sd, &sd->params, XS_CTL_DISCOVERY);
    286 	aprint_normal("%s: ", sd->sc_dev.dv_xname);
    287 	switch (result) {
    288 	case SDGP_RESULT_OK:
    289 		format_bytes(pbuf, sizeof(pbuf),
    290 		    (u_int64_t)dp->disksize * dp->blksize);
    291 	        aprint_normal(
    292 		"%s, %ld cyl, %ld head, %ld sec, %ld bytes/sect x %llu sectors",
    293 		    pbuf, dp->cyls, dp->heads, dp->sectors, dp->blksize,
    294 		    (unsigned long long)dp->disksize);
    295 		break;
    296 
    297 	case SDGP_RESULT_OFFLINE:
    298 		aprint_normal("drive offline");
    299 		break;
    300 
    301 	case SDGP_RESULT_UNFORMATTED:
    302 		aprint_normal("unformatted media");
    303 		break;
    304 
    305 #ifdef DIAGNOSTIC
    306 	default:
    307 		panic("sdattach: unknown result from get_parms");
    308 		break;
    309 #endif
    310 	}
    311 	aprint_normal("\n");
    312 
    313 	/*
    314 	 * Establish a shutdown hook so that we can ensure that
    315 	 * our data has actually made it onto the platter at
    316 	 * shutdown time.  Note that this relies on the fact
    317 	 * that the shutdown hook code puts us at the head of
    318 	 * the list (thus guaranteeing that our hook runs before
    319 	 * our ancestors').
    320 	 */
    321 	if ((sd->sc_sdhook =
    322 	    shutdownhook_establish(sd_shutdown, sd)) == NULL)
    323 		aprint_error("%s: WARNING: unable to establish shutdown hook\n",
    324 			sd->sc_dev.dv_xname);
    325 
    326 	if (!pmf_device_register(self, sd_suspend, NULL))
    327 		aprint_error_dev(self, "couldn't establish power handler\n");
    328 
    329 #if NRND > 0
    330 	/*
    331 	 * attach the device into the random source list
    332 	 */
    333 	rnd_attach_source(&sd->rnd_source, sd->sc_dev.dv_xname,
    334 			  RND_TYPE_DISK, 0);
    335 #endif
    336 
    337 	/* Discover wedges on this disk. */
    338 	dkwedge_discover(&sd->sc_dk);
    339 
    340 	sd_set_properties(sd);
    341 
    342 	np = sd->sc_dk.dk_label->d_npartitions;
    343 
    344 	/* locate the major numbers */
    345 	bmajor = bdevsw_lookup_major(&sd_bdevsw);
    346 	cmajor = cdevsw_lookup_major(&sd_cdevsw);
    347 	for (i = 0; i < 16; i++) {
    348 		device_register_name(
    349 		    MAKEDISKDEV(bmajor, device_unit(&sd->sc_dev), i),
    350 			&sd->sc_dev, false, DEV_DISK,
    351 			"sd%d%c", device_unit(&sd->sc_dev), 'a'+i);
    352 
    353 		device_register_name(
    354 		    MAKEDISKDEV(cmajor, device_unit(&sd->sc_dev), i),
    355 			&sd->sc_dev, true, DEV_DISK,
    356 			"rsd%d%c", device_unit(&sd->sc_dev), 'a' + i);
    357 	}
    358 }
    359 
    360 static int
    361 sdactivate(struct device *self, enum devact act)
    362 {
    363 	int rv = 0;
    364 
    365 	switch (act) {
    366 	case DVACT_ACTIVATE:
    367 		rv = EOPNOTSUPP;
    368 		break;
    369 
    370 	case DVACT_DEACTIVATE:
    371 		/*
    372 		 * Nothing to do; we key off the device's DVF_ACTIVE.
    373 		 */
    374 		break;
    375 	}
    376 	return (rv);
    377 }
    378 
    379 static int
    380 sddetach(struct device *self, int flags)
    381 {
    382 	struct sd_softc *sd = device_private(self);
    383 	int s, bmaj, cmaj, i, mn;
    384 
    385 	device_deregister_all(self);
    386 
    387 	/* locate the major number */
    388 	bmaj = bdevsw_lookup_major(&sd_bdevsw);
    389 	cmaj = cdevsw_lookup_major(&sd_cdevsw);
    390 
    391 	/*
    392 	 * Nuke the vnodes for any open instances and deregister
    393 	 * any device node names.
    394 	 */
    395 	for (i = 0; i < MAXPARTITIONS; i++) {
    396 		mn = SDMINOR(device_unit(self), i);
    397 		vdevgone(bmaj, mn, mn, VBLK);
    398 		vdevgone(cmaj, mn, mn, VCHR);
    399 	}
    400 
    401 	/* kill any pending restart */
    402 	callout_stop(&sd->sc_callout);
    403 
    404 	/* Delete all of our wedges. */
    405 	dkwedge_delall(&sd->sc_dk);
    406 
    407 	s = splbio();
    408 
    409 	/* Kill off any queued buffers. */
    410 	bufq_drain(sd->buf_queue);
    411 
    412 	bufq_free(sd->buf_queue);
    413 
    414 	/* Kill off any pending commands. */
    415 	scsipi_kill_pending(sd->sc_periph);
    416 
    417 	splx(s);
    418 
    419 	/* Detach from the disk list. */
    420 	disk_detach(&sd->sc_dk);
    421 	disk_destroy(&sd->sc_dk);
    422 
    423 	pmf_device_deregister(self);
    424 	shutdownhook_disestablish(sd->sc_sdhook);
    425 
    426 #if NRND > 0
    427 	/* Unhook the entropy source. */
    428 	rnd_detach_source(&sd->rnd_source);
    429 #endif
    430 
    431 	return (0);
    432 }
    433 
    434 /*
    435  * open the device. Make sure the partition info is a up-to-date as can be.
    436  */
    437 static int
    438 sdopen(dev_t dev, int flag, int fmt, struct lwp *l)
    439 {
    440 	struct sd_softc *sd;
    441 	struct scsipi_periph *periph;
    442 	struct scsipi_adapter *adapt;
    443 	int unit, part;
    444 	int error;
    445 
    446 	unit = SDUNIT(dev);
    447 	if (unit >= sd_cd.cd_ndevs)
    448 		return (ENXIO);
    449 	sd = sd_cd.cd_devs[unit];
    450 	if (sd == NULL)
    451 		return (ENXIO);
    452 
    453 	if (!device_is_active(&sd->sc_dev))
    454 		return (ENODEV);
    455 
    456 	part = SDPART(dev);
    457 
    458 	mutex_enter(&sd->sc_dk.dk_openlock);
    459 
    460 	/*
    461 	 * If there are wedges, and this is not RAW_PART, then we
    462 	 * need to fail.
    463 	 */
    464 	if (sd->sc_dk.dk_nwedges != 0 && part != RAW_PART) {
    465 		error = EBUSY;
    466 		goto bad1;
    467 	}
    468 
    469 	periph = sd->sc_periph;
    470 	adapt = periph->periph_channel->chan_adapter;
    471 
    472 	SC_DEBUG(periph, SCSIPI_DB1,
    473 	    ("sdopen: dev=0x%x (unit %d (of %d), partition %d)\n", dev, unit,
    474 	    sd_cd.cd_ndevs, part));
    475 
    476 	/*
    477 	 * If this is the first open of this device, add a reference
    478 	 * to the adapter.
    479 	 */
    480 	if (sd->sc_dk.dk_openmask == 0 &&
    481 	    (error = scsipi_adapter_addref(adapt)) != 0)
    482 		goto bad1;
    483 
    484 	if ((periph->periph_flags & PERIPH_OPEN) != 0) {
    485 		/*
    486 		 * If any partition is open, but the disk has been invalidated,
    487 		 * disallow further opens of non-raw partition
    488 		 */
    489 		if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0 &&
    490 		    (part != RAW_PART || fmt != S_IFCHR)) {
    491 			error = EIO;
    492 			goto bad2;
    493 		}
    494 	} else {
    495 		int silent;
    496 
    497 		if (part == RAW_PART && fmt == S_IFCHR)
    498 			silent = XS_CTL_SILENT;
    499 		else
    500 			silent = 0;
    501 
    502 		/* Check that it is still responding and ok. */
    503 		error = scsipi_test_unit_ready(periph,
    504 		    XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_MEDIA_CHANGE |
    505 		    silent);
    506 
    507 		/*
    508 		 * Start the pack spinning if necessary. Always allow the
    509 		 * raw parition to be opened, for raw IOCTLs. Data transfers
    510 		 * will check for SDEV_MEDIA_LOADED.
    511 		 */
    512 		if (error == EIO) {
    513 			int error2;
    514 
    515 			error2 = scsipi_start(periph, SSS_START, silent);
    516 			switch (error2) {
    517 			case 0:
    518 				error = 0;
    519 				break;
    520 			case EIO:
    521 			case EINVAL:
    522 				break;
    523 			default:
    524 				error = error2;
    525 				break;
    526 			}
    527 		}
    528 		if (error) {
    529 			if (silent)
    530 				goto out;
    531 			goto bad2;
    532 		}
    533 
    534 		periph->periph_flags |= PERIPH_OPEN;
    535 
    536 		if (periph->periph_flags & PERIPH_REMOVABLE) {
    537 			/* Lock the pack in. */
    538 			error = scsipi_prevent(periph, SPAMR_PREVENT_DT,
    539 			    XS_CTL_IGNORE_ILLEGAL_REQUEST |
    540 			    XS_CTL_IGNORE_MEDIA_CHANGE |
    541 			    XS_CTL_SILENT);
    542 			if (error)
    543 				goto bad3;
    544 		}
    545 
    546 		if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) {
    547 			int param_error;
    548 			periph->periph_flags |= PERIPH_MEDIA_LOADED;
    549 
    550 			/*
    551 			 * Load the physical device parameters.
    552 			 *
    553 			 * Note that if media is present but unformatted,
    554 			 * we allow the open (so that it can be formatted!).
    555 			 * The drive should refuse real I/O, if the media is
    556 			 * unformatted.
    557 			 */
    558 			if ((param_error = sd_get_parms(sd, &sd->params, 0))
    559 			     == SDGP_RESULT_OFFLINE) {
    560 				error = ENXIO;
    561 				periph->periph_flags &= ~PERIPH_MEDIA_LOADED;
    562 				goto bad3;
    563 			}
    564 			SC_DEBUG(periph, SCSIPI_DB3, ("Params loaded "));
    565 
    566 			/* Load the partition info if not already loaded. */
    567 			if (param_error == 0) {
    568 				if ((sdgetdisklabel(sd) != 0) && (part != RAW_PART)) {
    569 					error = EIO;
    570 					goto bad3;
    571 				}
    572 				SC_DEBUG(periph, SCSIPI_DB3,
    573 				     ("Disklabel loaded "));
    574 			}
    575 		}
    576 	}
    577 
    578 	/* Check that the partition exists. */
    579 	if (part != RAW_PART &&
    580 	    (part >= sd->sc_dk.dk_label->d_npartitions ||
    581 	     sd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
    582 		error = ENXIO;
    583 		goto bad3;
    584 	}
    585 
    586  out:	/* Insure only one open at a time. */
    587 	switch (fmt) {
    588 	case S_IFCHR:
    589 		sd->sc_dk.dk_copenmask |= (1 << part);
    590 		break;
    591 	case S_IFBLK:
    592 		sd->sc_dk.dk_bopenmask |= (1 << part);
    593 		break;
    594 	}
    595 	sd->sc_dk.dk_openmask =
    596 	    sd->sc_dk.dk_copenmask | sd->sc_dk.dk_bopenmask;
    597 
    598 	SC_DEBUG(periph, SCSIPI_DB3, ("open complete\n"));
    599 	mutex_exit(&sd->sc_dk.dk_openlock);
    600 	return (0);
    601 
    602  bad3:
    603 	if (sd->sc_dk.dk_openmask == 0) {
    604 		if (periph->periph_flags & PERIPH_REMOVABLE)
    605 			scsipi_prevent(periph, SPAMR_ALLOW,
    606 			    XS_CTL_IGNORE_ILLEGAL_REQUEST |
    607 			    XS_CTL_IGNORE_MEDIA_CHANGE |
    608 			    XS_CTL_SILENT);
    609 		periph->periph_flags &= ~PERIPH_OPEN;
    610 	}
    611 
    612  bad2:
    613 	if (sd->sc_dk.dk_openmask == 0)
    614 		scsipi_adapter_delref(adapt);
    615 
    616  bad1:
    617 	mutex_exit(&sd->sc_dk.dk_openlock);
    618 	return (error);
    619 }
    620 
    621 /*
    622  * close the device.. only called if we are the LAST occurence of an open
    623  * device.  Convenient now but usually a pain.
    624  */
    625 static int
    626 sdclose(dev_t dev, int flag, int fmt, struct lwp *l)
    627 {
    628 	struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(dev)];
    629 	struct scsipi_periph *periph = sd->sc_periph;
    630 	struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
    631 	int part = SDPART(dev);
    632 
    633 	mutex_enter(&sd->sc_dk.dk_openlock);
    634 	switch (fmt) {
    635 	case S_IFCHR:
    636 		sd->sc_dk.dk_copenmask &= ~(1 << part);
    637 		break;
    638 	case S_IFBLK:
    639 		sd->sc_dk.dk_bopenmask &= ~(1 << part);
    640 		break;
    641 	}
    642 	sd->sc_dk.dk_openmask =
    643 	    sd->sc_dk.dk_copenmask | sd->sc_dk.dk_bopenmask;
    644 
    645 	if (sd->sc_dk.dk_openmask == 0) {
    646 		/*
    647 		 * If the disk cache needs flushing, and the disk supports
    648 		 * it, do it now.
    649 		 */
    650 		if ((sd->flags & SDF_DIRTY) != 0) {
    651 			if (sd_flush(sd, 0)) {
    652 				printf("%s: cache synchronization failed\n",
    653 				    sd->sc_dev.dv_xname);
    654 				sd->flags &= ~SDF_FLUSHING;
    655 			} else
    656 				sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY);
    657 		}
    658 
    659 		scsipi_wait_drain(periph);
    660 
    661 		if (periph->periph_flags & PERIPH_REMOVABLE)
    662 			scsipi_prevent(periph, SPAMR_ALLOW,
    663 			    XS_CTL_IGNORE_ILLEGAL_REQUEST |
    664 			    XS_CTL_IGNORE_NOT_READY |
    665 			    XS_CTL_SILENT);
    666 		periph->periph_flags &= ~PERIPH_OPEN;
    667 
    668 		scsipi_wait_drain(periph);
    669 
    670 		scsipi_adapter_delref(adapt);
    671 	}
    672 
    673 	mutex_exit(&sd->sc_dk.dk_openlock);
    674 	return (0);
    675 }
    676 
    677 /*
    678  * Actually translate the requested transfer into one the physical driver
    679  * can understand.  The transfer is described by a buf and will include
    680  * only one physical transfer.
    681  */
    682 static void
    683 sdstrategy(struct buf *bp)
    684 {
    685 	struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(bp->b_dev)];
    686 	struct scsipi_periph *periph = sd->sc_periph;
    687 	struct disklabel *lp;
    688 	daddr_t blkno;
    689 	int s;
    690 	bool sector_aligned;
    691 
    692 	SC_DEBUG(sd->sc_periph, SCSIPI_DB2, ("sdstrategy "));
    693 	SC_DEBUG(sd->sc_periph, SCSIPI_DB1,
    694 	    ("%d bytes @ blk %" PRId64 "\n", bp->b_bcount, bp->b_blkno));
    695 	/*
    696 	 * If the device has been made invalid, error out
    697 	 */
    698 	if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0 ||
    699 	    !device_is_active(&sd->sc_dev)) {
    700 		if (periph->periph_flags & PERIPH_OPEN)
    701 			bp->b_error = EIO;
    702 		else
    703 			bp->b_error = ENODEV;
    704 		goto done;
    705 	}
    706 
    707 	lp = sd->sc_dk.dk_label;
    708 
    709 	/*
    710 	 * The transfer must be a whole number of blocks, offset must not be
    711 	 * negative.
    712 	 */
    713 	if (lp->d_secsize == DEV_BSIZE) {
    714 		sector_aligned = (bp->b_bcount & (DEV_BSIZE - 1)) == 0;
    715 	} else {
    716 		sector_aligned = (bp->b_bcount % lp->d_secsize) == 0;
    717 	}
    718 	if (!sector_aligned || bp->b_blkno < 0) {
    719 		bp->b_error = EINVAL;
    720 		goto done;
    721 	}
    722 	/*
    723 	 * If it's a null transfer, return immediatly
    724 	 */
    725 	if (bp->b_bcount == 0)
    726 		goto done;
    727 
    728 	/*
    729 	 * Do bounds checking, adjust transfer. if error, process.
    730 	 * If end of partition, just return.
    731 	 */
    732 	if (SDPART(bp->b_dev) == RAW_PART) {
    733 		if (bounds_check_with_mediasize(bp, DEV_BSIZE,
    734 		    sd->params.disksize512) <= 0)
    735 			goto done;
    736 	} else {
    737 		if (bounds_check_with_label(&sd->sc_dk, bp,
    738 		    (sd->flags & (SDF_WLABEL|SDF_LABELLING)) != 0) <= 0)
    739 			goto done;
    740 	}
    741 
    742 	/*
    743 	 * Now convert the block number to absolute and put it in
    744 	 * terms of the device's logical block size.
    745 	 */
    746 	if (lp->d_secsize == DEV_BSIZE)
    747 		blkno = bp->b_blkno;
    748 	else if (lp->d_secsize > DEV_BSIZE)
    749 		blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
    750 	else
    751 		blkno = bp->b_blkno * (DEV_BSIZE / lp->d_secsize);
    752 
    753 	if (SDPART(bp->b_dev) != RAW_PART)
    754 		blkno += lp->d_partitions[SDPART(bp->b_dev)].p_offset;
    755 
    756 	bp->b_rawblkno = blkno;
    757 
    758 	s = splbio();
    759 
    760 	/*
    761 	 * Place it in the queue of disk activities for this disk.
    762 	 *
    763 	 * XXX Only do disksort() if the current operating mode does not
    764 	 * XXX include tagged queueing.
    765 	 */
    766 	BUFQ_PUT(sd->buf_queue, bp);
    767 
    768 	/*
    769 	 * Tell the device to get going on the transfer if it's
    770 	 * not doing anything, otherwise just wait for completion
    771 	 */
    772 	sdstart(sd->sc_periph);
    773 
    774 	splx(s);
    775 	return;
    776 
    777 done:
    778 	/*
    779 	 * Correctly set the buf to indicate a completed xfer
    780 	 */
    781 	bp->b_resid = bp->b_bcount;
    782 	biodone(bp);
    783 }
    784 
    785 /*
    786  * sdstart looks to see if there is a buf waiting for the device
    787  * and that the device is not already busy. If both are true,
    788  * It dequeues the buf and creates a scsi command to perform the
    789  * transfer in the buf. The transfer request will call scsipi_done
    790  * on completion, which will in turn call this routine again
    791  * so that the next queued transfer is performed.
    792  * The bufs are queued by the strategy routine (sdstrategy)
    793  *
    794  * This routine is also called after other non-queued requests
    795  * have been made of the scsi driver, to ensure that the queue
    796  * continues to be drained.
    797  *
    798  * must be called at the correct (highish) spl level
    799  * sdstart() is called at splbio from sdstrategy, sdrestart and scsipi_done
    800  */
    801 static void
    802 sdstart(struct scsipi_periph *periph)
    803 {
    804 	struct sd_softc *sd = (void *)periph->periph_dev;
    805 	struct disklabel *lp = sd->sc_dk.dk_label;
    806 	struct buf *bp = 0;
    807 	struct scsipi_rw_16 cmd16;
    808 	struct scsipi_rw_10 cmd_big;
    809 	struct scsi_rw_6 cmd_small;
    810 	struct scsipi_generic *cmdp;
    811 	struct scsipi_xfer *xs;
    812 	int nblks, cmdlen, error, flags;
    813 
    814 	SC_DEBUG(periph, SCSIPI_DB2, ("sdstart "));
    815 	/*
    816 	 * Check if the device has room for another command
    817 	 */
    818 	while (periph->periph_active < periph->periph_openings) {
    819 		/*
    820 		 * there is excess capacity, but a special waits
    821 		 * It'll need the adapter as soon as we clear out of the
    822 		 * way and let it run (user level wait).
    823 		 */
    824 		if (periph->periph_flags & PERIPH_WAITING) {
    825 			periph->periph_flags &= ~PERIPH_WAITING;
    826 			wakeup((void *)periph);
    827 			return;
    828 		}
    829 
    830 		/*
    831 		 * If the device has become invalid, abort all the
    832 		 * reads and writes until all files have been closed and
    833 		 * re-opened
    834 		 */
    835 		if (__predict_false(
    836 		    (periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)) {
    837 			if ((bp = BUFQ_GET(sd->buf_queue)) != NULL) {
    838 				bp->b_error = EIO;
    839 				bp->b_resid = bp->b_bcount;
    840 				biodone(bp);
    841 				continue;
    842 			} else {
    843 				return;
    844 			}
    845 		}
    846 
    847 		/*
    848 		 * See if there is a buf with work for us to do..
    849 		 */
    850 		if ((bp = BUFQ_PEEK(sd->buf_queue)) == NULL)
    851 			return;
    852 
    853 		/*
    854 		 * We have a buf, now we should make a command.
    855 		 */
    856 
    857 		if (lp->d_secsize == DEV_BSIZE)
    858 			nblks = bp->b_bcount >> DEV_BSHIFT;
    859 		else
    860 			nblks = howmany(bp->b_bcount, lp->d_secsize);
    861 
    862 		/*
    863 		 * Fill out the scsi command.  Use the smallest CDB possible
    864 		 * (6-byte, 10-byte, or 16-byte).
    865 		 */
    866 		if (((bp->b_rawblkno & 0x1fffff) == bp->b_rawblkno) &&
    867 		    ((nblks & 0xff) == nblks) &&
    868 		    !(periph->periph_quirks & PQUIRK_ONLYBIG)) {
    869 			/* 6-byte CDB */
    870 			memset(&cmd_small, 0, sizeof(cmd_small));
    871 			cmd_small.opcode = (bp->b_flags & B_READ) ?
    872 			    SCSI_READ_6_COMMAND : SCSI_WRITE_6_COMMAND;
    873 			_lto3b(bp->b_rawblkno, cmd_small.addr);
    874 			cmd_small.length = nblks & 0xff;
    875 			cmdlen = sizeof(cmd_small);
    876 			cmdp = (struct scsipi_generic *)&cmd_small;
    877 		} else if ((bp->b_rawblkno & 0xffffffff) == bp->b_rawblkno) {
    878 			/* 10-byte CDB */
    879 			memset(&cmd_big, 0, sizeof(cmd_big));
    880 			cmd_big.opcode = (bp->b_flags & B_READ) ?
    881 			    READ_10 : WRITE_10;
    882 			_lto4b(bp->b_rawblkno, cmd_big.addr);
    883 			_lto2b(nblks, cmd_big.length);
    884 			cmdlen = sizeof(cmd_big);
    885 			cmdp = (struct scsipi_generic *)&cmd_big;
    886 		} else {
    887 			/* 16-byte CDB */
    888 			memset(&cmd16, 0, sizeof(cmd16));
    889 			cmd16.opcode = (bp->b_flags & B_READ) ?
    890 			    READ_16 : WRITE_16;
    891 			_lto8b(bp->b_rawblkno, cmd16.addr);
    892 			_lto4b(nblks, cmd16.length);
    893 			cmdlen = sizeof(cmd16);
    894 			cmdp = (struct scsipi_generic *)&cmd16;
    895 		}
    896 
    897 		/* Instrumentation. */
    898 		disk_busy(&sd->sc_dk);
    899 
    900 		/*
    901 		 * Mark the disk dirty so that the cache will be
    902 		 * flushed on close.
    903 		 */
    904 		if ((bp->b_flags & B_READ) == 0)
    905 			sd->flags |= SDF_DIRTY;
    906 
    907 		/*
    908 		 * Figure out what flags to use.
    909 		 */
    910 		flags = XS_CTL_NOSLEEP|XS_CTL_ASYNC|XS_CTL_SIMPLE_TAG;
    911 		if (bp->b_flags & B_READ)
    912 			flags |= XS_CTL_DATA_IN;
    913 		else
    914 			flags |= XS_CTL_DATA_OUT;
    915 
    916 		/*
    917 		 * Call the routine that chats with the adapter.
    918 		 * Note: we cannot sleep as we may be an interrupt
    919 		 */
    920 		xs = scsipi_make_xs(periph, cmdp, cmdlen,
    921 		    (u_char *)bp->b_data, bp->b_bcount,
    922 		    SDRETRIES, SD_IO_TIMEOUT, bp, flags);
    923 		if (__predict_false(xs == NULL)) {
    924 			/*
    925 			 * out of memory. Keep this buffer in the queue, and
    926 			 * retry later.
    927 			 */
    928 			callout_reset(&sd->sc_callout, hz / 2, sdrestart,
    929 			    periph);
    930 			return;
    931 		}
    932 		/*
    933 		 * need to dequeue the buffer before queuing the command,
    934 		 * because cdstart may be called recursively from the
    935 		 * HBA driver
    936 		 */
    937 #ifdef DIAGNOSTIC
    938 		if (BUFQ_GET(sd->buf_queue) != bp)
    939 			panic("sdstart(): dequeued wrong buf");
    940 #else
    941 		BUFQ_GET(sd->buf_queue);
    942 #endif
    943 		error = scsipi_execute_xs(xs);
    944 		/* with a scsipi_xfer preallocated, scsipi_command can't fail */
    945 		KASSERT(error == 0);
    946 	}
    947 }
    948 
    949 static void
    950 sdrestart(void *v)
    951 {
    952 	int s = splbio();
    953 	sdstart((struct scsipi_periph *)v);
    954 	splx(s);
    955 }
    956 
    957 static void
    958 sddone(struct scsipi_xfer *xs, int error)
    959 {
    960 	struct sd_softc *sd = (void *)xs->xs_periph->periph_dev;
    961 	struct buf *bp = xs->bp;
    962 
    963 	if (sd->flags & SDF_FLUSHING) {
    964 		/* Flush completed, no longer dirty. */
    965 		sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY);
    966 	}
    967 
    968 	if (bp) {
    969 		bp->b_error = error;
    970 		bp->b_resid = xs->resid;
    971 		if (error) {
    972 			/* on a read/write error bp->b_resid is zero, so fix */
    973 			bp->b_resid = bp->b_bcount;
    974 		}
    975 
    976 		disk_unbusy(&sd->sc_dk, bp->b_bcount - bp->b_resid,
    977 		    (bp->b_flags & B_READ));
    978 #if NRND > 0
    979 		rnd_add_uint32(&sd->rnd_source, bp->b_rawblkno);
    980 #endif
    981 
    982 		biodone(bp);
    983 	}
    984 }
    985 
    986 static void
    987 sdminphys(struct buf *bp)
    988 {
    989 	struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(bp->b_dev)];
    990 	long xmax;
    991 
    992 	/*
    993 	 * If the device is ancient, we want to make sure that
    994 	 * the transfer fits into a 6-byte cdb.
    995 	 *
    996 	 * XXX Note that the SCSI-I spec says that 256-block transfers
    997 	 * are allowed in a 6-byte read/write, and are specified
    998 	 * by settng the "length" to 0.  However, we're conservative
    999 	 * here, allowing only 255-block transfers in case an
   1000 	 * ancient device gets confused by length == 0.  A length of 0
   1001 	 * in a 10-byte read/write actually means 0 blocks.
   1002 	 */
   1003 	if ((sd->flags & SDF_ANCIENT) &&
   1004 	    ((sd->sc_periph->periph_flags &
   1005 	    (PERIPH_REMOVABLE | PERIPH_MEDIA_LOADED)) != PERIPH_REMOVABLE)) {
   1006 		xmax = sd->sc_dk.dk_label->d_secsize * 0xff;
   1007 
   1008 		if (bp->b_bcount > xmax)
   1009 			bp->b_bcount = xmax;
   1010 	}
   1011 
   1012 	scsipi_adapter_minphys(sd->sc_periph->periph_channel, bp);
   1013 }
   1014 
   1015 static int
   1016 sdread(dev_t dev, struct uio *uio, int ioflag)
   1017 {
   1018 
   1019 	return (physio(sdstrategy, NULL, dev, B_READ, sdminphys, uio));
   1020 }
   1021 
   1022 static int
   1023 sdwrite(dev_t dev, struct uio *uio, int ioflag)
   1024 {
   1025 
   1026 	return (physio(sdstrategy, NULL, dev, B_WRITE, sdminphys, uio));
   1027 }
   1028 
   1029 /*
   1030  * Perform special action on behalf of the user
   1031  * Knows about the internals of this device
   1032  */
   1033 static int
   1034 sdioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
   1035 {
   1036 	struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(dev)];
   1037 	struct scsipi_periph *periph = sd->sc_periph;
   1038 	int part = SDPART(dev);
   1039 	int error = 0;
   1040 #ifdef __HAVE_OLD_DISKLABEL
   1041 	struct disklabel *newlabel = NULL;
   1042 #endif
   1043 
   1044 	SC_DEBUG(sd->sc_periph, SCSIPI_DB2, ("sdioctl 0x%lx ", cmd));
   1045 
   1046 	/*
   1047 	 * If the device is not valid, some IOCTLs can still be
   1048 	 * handled on the raw partition. Check this here.
   1049 	 */
   1050 	if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) {
   1051 		switch (cmd) {
   1052 		case DIOCKLABEL:
   1053 		case DIOCWLABEL:
   1054 		case DIOCLOCK:
   1055 		case DIOCEJECT:
   1056 		case ODIOCEJECT:
   1057 		case DIOCGCACHE:
   1058 		case DIOCSCACHE:
   1059 		case SCIOCIDENTIFY:
   1060 		case OSCIOCIDENTIFY:
   1061 		case SCIOCCOMMAND:
   1062 		case SCIOCDEBUG:
   1063 			if (part == RAW_PART)
   1064 				break;
   1065 		/* FALLTHROUGH */
   1066 		default:
   1067 			if ((periph->periph_flags & PERIPH_OPEN) == 0)
   1068 				return (ENODEV);
   1069 			else
   1070 				return (EIO);
   1071 		}
   1072 	}
   1073 
   1074 	switch (cmd) {
   1075 	case DIOCGDINFO:
   1076 		*(struct disklabel *)addr = *(sd->sc_dk.dk_label);
   1077 		return (0);
   1078 
   1079 #ifdef __HAVE_OLD_DISKLABEL
   1080 	case ODIOCGDINFO:
   1081 		newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
   1082 		if (newlabel == NULL)
   1083 			return EIO;
   1084 		memcpy(newlabel, sd->sc_dk.dk_label, sizeof (*newlabel));
   1085 		if (newlabel->d_npartitions <= OLDMAXPARTITIONS)
   1086 			memcpy(addr, newlabel, sizeof (struct olddisklabel));
   1087 		else
   1088 			error = ENOTTY;
   1089 		free(newlabel, M_TEMP);
   1090 		return error;
   1091 #endif
   1092 
   1093 	case DIOCGPART:
   1094 		((struct partinfo *)addr)->disklab = sd->sc_dk.dk_label;
   1095 		((struct partinfo *)addr)->part =
   1096 		    &sd->sc_dk.dk_label->d_partitions[part];
   1097 		return (0);
   1098 
   1099 	case DIOCWDINFO:
   1100 	case DIOCSDINFO:
   1101 #ifdef __HAVE_OLD_DISKLABEL
   1102 	case ODIOCWDINFO:
   1103 	case ODIOCSDINFO:
   1104 #endif
   1105 	{
   1106 		struct disklabel *lp;
   1107 
   1108 		if ((flag & FWRITE) == 0)
   1109 			return (EBADF);
   1110 
   1111 #ifdef __HAVE_OLD_DISKLABEL
   1112  		if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
   1113 			newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
   1114 			if (newlabel == NULL)
   1115 				return EIO;
   1116 			memset(newlabel, 0, sizeof newlabel);
   1117 			memcpy(newlabel, addr, sizeof (struct olddisklabel));
   1118 			lp = newlabel;
   1119 		} else
   1120 #endif
   1121 		lp = (struct disklabel *)addr;
   1122 
   1123 		mutex_enter(&sd->sc_dk.dk_openlock);
   1124 		sd->flags |= SDF_LABELLING;
   1125 
   1126 		error = setdisklabel(sd->sc_dk.dk_label,
   1127 		    lp, /*sd->sc_dk.dk_openmask : */0,
   1128 		    sd->sc_dk.dk_cpulabel);
   1129 		if (error == 0) {
   1130 			if (cmd == DIOCWDINFO
   1131 #ifdef __HAVE_OLD_DISKLABEL
   1132 			    || cmd == ODIOCWDINFO
   1133 #endif
   1134 			   )
   1135 				error = writedisklabel(SDLABELDEV(dev),
   1136 				    sdstrategy, sd->sc_dk.dk_label,
   1137 				    sd->sc_dk.dk_cpulabel);
   1138 		}
   1139 
   1140 		sd->flags &= ~SDF_LABELLING;
   1141 		mutex_exit(&sd->sc_dk.dk_openlock);
   1142 #ifdef __HAVE_OLD_DISKLABEL
   1143 		if (newlabel != NULL)
   1144 			free(newlabel, M_TEMP);
   1145 #endif
   1146 		return (error);
   1147 	}
   1148 
   1149 	case DIOCKLABEL:
   1150 		if (*(int *)addr)
   1151 			periph->periph_flags |= PERIPH_KEEP_LABEL;
   1152 		else
   1153 			periph->periph_flags &= ~PERIPH_KEEP_LABEL;
   1154 		return (0);
   1155 
   1156 	case DIOCWLABEL:
   1157 		if ((flag & FWRITE) == 0)
   1158 			return (EBADF);
   1159 		if (*(int *)addr)
   1160 			sd->flags |= SDF_WLABEL;
   1161 		else
   1162 			sd->flags &= ~SDF_WLABEL;
   1163 		return (0);
   1164 
   1165 	case DIOCLOCK:
   1166 		if (periph->periph_flags & PERIPH_REMOVABLE)
   1167 			return (scsipi_prevent(periph,
   1168 			    (*(int *)addr) ?
   1169 			    SPAMR_PREVENT_DT : SPAMR_ALLOW, 0));
   1170 		else
   1171 			return (ENOTTY);
   1172 
   1173 	case DIOCEJECT:
   1174 		if ((periph->periph_flags & PERIPH_REMOVABLE) == 0)
   1175 			return (ENOTTY);
   1176 		if (*(int *)addr == 0) {
   1177 			/*
   1178 			 * Don't force eject: check that we are the only
   1179 			 * partition open. If so, unlock it.
   1180 			 */
   1181 			if ((sd->sc_dk.dk_openmask & ~(1 << part)) == 0 &&
   1182 			    sd->sc_dk.dk_bopenmask + sd->sc_dk.dk_copenmask ==
   1183 			    sd->sc_dk.dk_openmask) {
   1184 				error = scsipi_prevent(periph, SPAMR_ALLOW,
   1185 				    XS_CTL_IGNORE_NOT_READY);
   1186 				if (error)
   1187 					return (error);
   1188 			} else {
   1189 				return (EBUSY);
   1190 			}
   1191 		}
   1192 		/* FALLTHROUGH */
   1193 	case ODIOCEJECT:
   1194 		return ((periph->periph_flags & PERIPH_REMOVABLE) == 0 ?
   1195 		    ENOTTY : scsipi_start(periph, SSS_STOP|SSS_LOEJ, 0));
   1196 
   1197 	case DIOCGDEFLABEL:
   1198 		sdgetdefaultlabel(sd, (struct disklabel *)addr);
   1199 		return (0);
   1200 
   1201 #ifdef __HAVE_OLD_DISKLABEL
   1202 	case ODIOCGDEFLABEL:
   1203 		newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
   1204 		if (newlabel == NULL)
   1205 			return EIO;
   1206 		sdgetdefaultlabel(sd, newlabel);
   1207 		if (newlabel->d_npartitions <= OLDMAXPARTITIONS)
   1208 			memcpy(addr, newlabel, sizeof (struct olddisklabel));
   1209 		else
   1210 			error = ENOTTY;
   1211 		free(newlabel, M_TEMP);
   1212 		return error;
   1213 #endif
   1214 
   1215 	case DIOCGCACHE:
   1216 		return (sd_getcache(sd, (int *) addr));
   1217 
   1218 	case DIOCSCACHE:
   1219 		if ((flag & FWRITE) == 0)
   1220 			return (EBADF);
   1221 		return (sd_setcache(sd, *(int *) addr));
   1222 
   1223 	case DIOCCACHESYNC:
   1224 		/*
   1225 		 * XXX Do we really need to care about having a writable
   1226 		 * file descriptor here?
   1227 		 */
   1228 		if ((flag & FWRITE) == 0)
   1229 			return (EBADF);
   1230 		if (((sd->flags & SDF_DIRTY) != 0 || *(int *)addr != 0)) {
   1231 			error = sd_flush(sd, 0);
   1232 			if (error)
   1233 				sd->flags &= ~SDF_FLUSHING;
   1234 			else
   1235 				sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY);
   1236 		} else
   1237 			error = 0;
   1238 		return (error);
   1239 
   1240 	case DIOCAWEDGE:
   1241 	    {
   1242 	    	struct dkwedge_info *dkw = (void *) addr;
   1243 
   1244 		if ((flag & FWRITE) == 0)
   1245 			return (EBADF);
   1246 
   1247 		/* If the ioctl happens here, the parent is us. */
   1248 		strcpy(dkw->dkw_parent, sd->sc_dev.dv_xname);
   1249 		return (dkwedge_add(dkw));
   1250 	    }
   1251 
   1252 	case DIOCDWEDGE:
   1253 	    {
   1254 	    	struct dkwedge_info *dkw = (void *) addr;
   1255 
   1256 		if ((flag & FWRITE) == 0)
   1257 			return (EBADF);
   1258 
   1259 		/* If the ioctl happens here, the parent is us. */
   1260 		strcpy(dkw->dkw_parent, sd->sc_dev.dv_xname);
   1261 		return (dkwedge_del(dkw));
   1262 	    }
   1263 
   1264 	case DIOCLWEDGES:
   1265 	    {
   1266 	    	struct dkwedge_list *dkwl = (void *) addr;
   1267 
   1268 		return (dkwedge_list(&sd->sc_dk, dkwl, l));
   1269 	    }
   1270 
   1271 	default:
   1272 		if (part != RAW_PART)
   1273 			return (ENOTTY);
   1274 		return (scsipi_do_ioctl(periph, dev, cmd, addr, flag, l));
   1275 	}
   1276 
   1277 #ifdef DIAGNOSTIC
   1278 	panic("sdioctl: impossible");
   1279 #endif
   1280 }
   1281 
   1282 static void
   1283 sdgetdefaultlabel(struct sd_softc *sd, struct disklabel *lp)
   1284 {
   1285 
   1286 	memset(lp, 0, sizeof(struct disklabel));
   1287 
   1288 	lp->d_secsize = sd->params.blksize;
   1289 	lp->d_ntracks = sd->params.heads;
   1290 	lp->d_nsectors = sd->params.sectors;
   1291 	lp->d_ncylinders = sd->params.cyls;
   1292 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
   1293 
   1294 	switch (scsipi_periph_bustype(sd->sc_periph)) {
   1295 	case SCSIPI_BUSTYPE_SCSI:
   1296 		lp->d_type = DTYPE_SCSI;
   1297 		break;
   1298 	case SCSIPI_BUSTYPE_ATAPI:
   1299 		lp->d_type = DTYPE_ATAPI;
   1300 		break;
   1301 	}
   1302 	/*
   1303 	 * XXX
   1304 	 * We could probe the mode pages to figure out what kind of disc it is.
   1305 	 * Is this worthwhile?
   1306 	 */
   1307 	strncpy(lp->d_typename, sd->name, 16);
   1308 	strncpy(lp->d_packname, "fictitious", 16);
   1309 	lp->d_secperunit = sd->params.disksize;
   1310 	lp->d_rpm = sd->params.rot_rate;
   1311 	lp->d_interleave = 1;
   1312 	lp->d_flags = sd->sc_periph->periph_flags & PERIPH_REMOVABLE ?
   1313 	    D_REMOVABLE : 0;
   1314 
   1315 	lp->d_partitions[RAW_PART].p_offset = 0;
   1316 	lp->d_partitions[RAW_PART].p_size = lp->d_secperunit;
   1317 	lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
   1318 	lp->d_npartitions = RAW_PART + 1;
   1319 
   1320 	lp->d_magic = DISKMAGIC;
   1321 	lp->d_magic2 = DISKMAGIC;
   1322 	lp->d_checksum = dkcksum(lp);
   1323 }
   1324 
   1325 
   1326 /*
   1327  * Load the label information on the named device
   1328  */
   1329 static int
   1330 sdgetdisklabel(struct sd_softc *sd)
   1331 {
   1332 	struct disklabel *lp = sd->sc_dk.dk_label;
   1333 	const char *errstring;
   1334 
   1335 	memset(sd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel));
   1336 
   1337 	sdgetdefaultlabel(sd, lp);
   1338 
   1339 	if (lp->d_secpercyl == 0) {
   1340 		lp->d_secpercyl = 100;
   1341 		/* as long as it's not 0 - readdisklabel divides by it (?) */
   1342 	}
   1343 
   1344 	/*
   1345 	 * Call the generic disklabel extraction routine
   1346 	 */
   1347 	errstring = readdisklabel(MAKESDDEV(0, device_unit(&sd->sc_dev),
   1348 	    RAW_PART), sdstrategy, lp, sd->sc_dk.dk_cpulabel);
   1349 	if (errstring) {
   1350 		printf("%s: %s\n", sd->sc_dev.dv_xname, errstring);
   1351 		return EIO;
   1352 	}
   1353 	return 0;
   1354 }
   1355 
   1356 static void
   1357 sd_shutdown(void *arg)
   1358 {
   1359 	struct sd_softc *sd = arg;
   1360 
   1361 	/*
   1362 	 * If the disk cache needs to be flushed, and the disk supports
   1363 	 * it, flush it.  We're cold at this point, so we poll for
   1364 	 * completion.
   1365 	 */
   1366 	if ((sd->flags & SDF_DIRTY) != 0) {
   1367 		if (sd_flush(sd, XS_CTL_NOSLEEP|XS_CTL_POLL)) {
   1368 			printf("%s: cache synchronization failed\n",
   1369 			    sd->sc_dev.dv_xname);
   1370 			sd->flags &= ~SDF_FLUSHING;
   1371 		} else
   1372 			sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY);
   1373 	}
   1374 }
   1375 
   1376 static bool
   1377 sd_suspend(device_t dv PMF_FN_ARGS)
   1378 {
   1379 	struct sd_softc *sd = device_private(dv);
   1380 
   1381 	sd_shutdown(sd); /* XXX no need to poll */
   1382 	return true;
   1383 }
   1384 
   1385 /*
   1386  * Check Errors
   1387  */
   1388 static int
   1389 sd_interpret_sense(struct scsipi_xfer *xs)
   1390 {
   1391 	struct scsipi_periph *periph = xs->xs_periph;
   1392 	struct scsi_sense_data *sense = &xs->sense.scsi_sense;
   1393 	struct sd_softc *sd = (void *)periph->periph_dev;
   1394 	int s, error, retval = EJUSTRETURN;
   1395 
   1396 	/*
   1397 	 * If the periph is already recovering, just do the normal
   1398 	 * error processing.
   1399 	 */
   1400 	if (periph->periph_flags & PERIPH_RECOVERING)
   1401 		return (retval);
   1402 
   1403 	/*
   1404 	 * Ignore errors from accessing illegal fields (e.g. trying to
   1405 	 * lock the door of a digicam, which doesn't have a door that
   1406 	 * can be locked) for the SCSI_PREVENT_ALLOW_MEDIUM_REMOVAL command.
   1407 	 */
   1408 	if (xs->cmd->opcode == SCSI_PREVENT_ALLOW_MEDIUM_REMOVAL &&
   1409 	    SSD_SENSE_KEY(sense->flags) == SKEY_ILLEGAL_REQUEST &&
   1410 	    sense->asc == 0x24 &&
   1411 	    sense->ascq == 0x00) { /* Illegal field in CDB */
   1412 		if (!(xs->xs_control & XS_CTL_SILENT)) {
   1413 			scsipi_printaddr(periph);
   1414 			printf("no door lock\n");
   1415 		}
   1416 		xs->xs_control |= XS_CTL_IGNORE_ILLEGAL_REQUEST;
   1417 		return (retval);
   1418 	}
   1419 
   1420 
   1421 
   1422 	/*
   1423 	 * If the device is not open yet, let the generic code handle it.
   1424 	 */
   1425 	if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
   1426 		return (retval);
   1427 
   1428 	/*
   1429 	 * If it isn't a extended or extended/deferred error, let
   1430 	 * the generic code handle it.
   1431 	 */
   1432 	if (SSD_RCODE(sense->response_code) != SSD_RCODE_CURRENT &&
   1433 	    SSD_RCODE(sense->response_code) != SSD_RCODE_DEFERRED)
   1434 		return (retval);
   1435 
   1436 	if (SSD_SENSE_KEY(sense->flags) == SKEY_NOT_READY &&
   1437 	    sense->asc == 0x4) {
   1438 		if (sense->ascq == 0x01)	{
   1439 			/*
   1440 			 * Unit In The Process Of Becoming Ready.
   1441 			 */
   1442 			printf("%s: waiting for pack to spin up...\n",
   1443 			    sd->sc_dev.dv_xname);
   1444 			if (!callout_pending(&periph->periph_callout))
   1445 				scsipi_periph_freeze(periph, 1);
   1446 			callout_reset(&periph->periph_callout,
   1447 			    5 * hz, scsipi_periph_timed_thaw, periph);
   1448 			retval = ERESTART;
   1449 		} else if (sense->ascq == 0x02) {
   1450 			printf("%s: pack is stopped, restarting...\n",
   1451 			    sd->sc_dev.dv_xname);
   1452 			s = splbio();
   1453 			periph->periph_flags |= PERIPH_RECOVERING;
   1454 			splx(s);
   1455 			error = scsipi_start(periph, SSS_START,
   1456 			    XS_CTL_URGENT|XS_CTL_HEAD_TAG|
   1457 			    XS_CTL_THAW_PERIPH|XS_CTL_FREEZE_PERIPH);
   1458 			if (error) {
   1459 				printf("%s: unable to restart pack\n",
   1460 				    sd->sc_dev.dv_xname);
   1461 				retval = error;
   1462 			} else
   1463 				retval = ERESTART;
   1464 			s = splbio();
   1465 			periph->periph_flags &= ~PERIPH_RECOVERING;
   1466 			splx(s);
   1467 		}
   1468 	}
   1469 	if (SSD_SENSE_KEY(sense->flags) == SKEY_MEDIUM_ERROR &&
   1470 	    sense->asc == 0x31 &&
   1471 	    sense->ascq == 0x00)	{ /* maybe for any asq ? */
   1472 		/* Medium Format Corrupted */
   1473 		retval = EFTYPE;
   1474 	}
   1475 	return (retval);
   1476 }
   1477 
   1478 
   1479 static int
   1480 sdsize(dev_t dev)
   1481 {
   1482 	struct sd_softc *sd;
   1483 	int part, unit, omask;
   1484 	int size;
   1485 
   1486 	unit = SDUNIT(dev);
   1487 	if (unit >= sd_cd.cd_ndevs)
   1488 		return (-1);
   1489 	sd = sd_cd.cd_devs[unit];
   1490 	if (sd == NULL)
   1491 		return (-1);
   1492 
   1493 	if (!device_is_active(&sd->sc_dev))
   1494 		return (-1);
   1495 
   1496 	part = SDPART(dev);
   1497 	omask = sd->sc_dk.dk_openmask & (1 << part);
   1498 
   1499 	if (omask == 0 && sdopen(dev, 0, S_IFBLK, NULL) != 0)
   1500 		return (-1);
   1501 	if ((sd->sc_periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
   1502 		size = -1;
   1503 	else if (sd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
   1504 		size = -1;
   1505 	else
   1506 		size = sd->sc_dk.dk_label->d_partitions[part].p_size *
   1507 		    (sd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
   1508 	if (omask == 0 && sdclose(dev, 0, S_IFBLK, NULL) != 0)
   1509 		return (-1);
   1510 	return (size);
   1511 }
   1512 
   1513 /* #define SD_DUMP_NOT_TRUSTED if you just want to watch */
   1514 static struct scsipi_xfer sx;
   1515 static int sddoingadump;
   1516 
   1517 /*
   1518  * dump all of physical memory into the partition specified, starting
   1519  * at offset 'dumplo' into the partition.
   1520  */
   1521 static int
   1522 sddump(dev_t dev, daddr_t blkno, void *va, size_t size)
   1523 {
   1524 	struct sd_softc *sd;	/* disk unit to do the I/O */
   1525 	struct disklabel *lp;	/* disk's disklabel */
   1526 	int	unit, part;
   1527 	int	sectorsize;	/* size of a disk sector */
   1528 	int	nsects;		/* number of sectors in partition */
   1529 	int	sectoff;	/* sector offset of partition */
   1530 	int	totwrt;		/* total number of sectors left to write */
   1531 	int	nwrt;		/* current number of sectors to write */
   1532 	struct scsipi_rw_10 cmd;	/* write command */
   1533 	struct scsipi_xfer *xs;	/* ... convenience */
   1534 	struct scsipi_periph *periph;
   1535 	struct scsipi_channel *chan;
   1536 
   1537 	/* Check if recursive dump; if so, punt. */
   1538 	if (sddoingadump)
   1539 		return (EFAULT);
   1540 
   1541 	/* Mark as active early. */
   1542 	sddoingadump = 1;
   1543 
   1544 	unit = SDUNIT(dev);	/* Decompose unit & partition. */
   1545 	part = SDPART(dev);
   1546 
   1547 	/* Check for acceptable drive number. */
   1548 	if (unit >= sd_cd.cd_ndevs || (sd = sd_cd.cd_devs[unit]) == NULL)
   1549 		return (ENXIO);
   1550 
   1551 	if (!device_is_active(&sd->sc_dev))
   1552 		return (ENODEV);
   1553 
   1554 	periph = sd->sc_periph;
   1555 	chan = periph->periph_channel;
   1556 
   1557 	/* Make sure it was initialized. */
   1558 	if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
   1559 		return (ENXIO);
   1560 
   1561 	/* Convert to disk sectors.  Request must be a multiple of size. */
   1562 	lp = sd->sc_dk.dk_label;
   1563 	sectorsize = lp->d_secsize;
   1564 	if ((size % sectorsize) != 0)
   1565 		return (EFAULT);
   1566 	totwrt = size / sectorsize;
   1567 	blkno = dbtob(blkno) / sectorsize;	/* blkno in DEV_BSIZE units */
   1568 
   1569 	nsects = lp->d_partitions[part].p_size;
   1570 	sectoff = lp->d_partitions[part].p_offset;
   1571 
   1572 	/* Check transfer bounds against partition size. */
   1573 	if ((blkno < 0) || ((blkno + totwrt) > nsects))
   1574 		return (EINVAL);
   1575 
   1576 	/* Offset block number to start of partition. */
   1577 	blkno += sectoff;
   1578 
   1579 	xs = &sx;
   1580 
   1581 	while (totwrt > 0) {
   1582 		nwrt = totwrt;		/* XXX */
   1583 #ifndef	SD_DUMP_NOT_TRUSTED
   1584 		/*
   1585 		 *  Fill out the scsi command
   1586 		 */
   1587 		memset(&cmd, 0, sizeof(cmd));
   1588 		cmd.opcode = WRITE_10;
   1589 		_lto4b(blkno, cmd.addr);
   1590 		_lto2b(nwrt, cmd.length);
   1591 		/*
   1592 		 * Fill out the scsipi_xfer structure
   1593 		 *    Note: we cannot sleep as we may be an interrupt
   1594 		 * don't use scsipi_command() as it may want to wait
   1595 		 * for an xs.
   1596 		 */
   1597 		memset(xs, 0, sizeof(sx));
   1598 		xs->xs_control |= XS_CTL_NOSLEEP | XS_CTL_POLL |
   1599 		    XS_CTL_DATA_OUT;
   1600 		xs->xs_status = 0;
   1601 		xs->xs_periph = periph;
   1602 		xs->xs_retries = SDRETRIES;
   1603 		xs->timeout = 10000;	/* 10000 millisecs for a disk ! */
   1604 		xs->cmd = (struct scsipi_generic *)&cmd;
   1605 		xs->cmdlen = sizeof(cmd);
   1606 		xs->resid = nwrt * sectorsize;
   1607 		xs->error = XS_NOERROR;
   1608 		xs->bp = 0;
   1609 		xs->data = va;
   1610 		xs->datalen = nwrt * sectorsize;
   1611 		callout_init(&xs->xs_callout, 0);
   1612 
   1613 		/*
   1614 		 * Pass all this info to the scsi driver.
   1615 		 */
   1616 		scsipi_adapter_request(chan, ADAPTER_REQ_RUN_XFER, xs);
   1617 		if ((xs->xs_status & XS_STS_DONE) == 0 ||
   1618 		    xs->error != XS_NOERROR)
   1619 			return (EIO);
   1620 #else	/* SD_DUMP_NOT_TRUSTED */
   1621 		/* Let's just talk about this first... */
   1622 		printf("sd%d: dump addr 0x%x, blk %d\n", unit, va, blkno);
   1623 		delay(500 * 1000);	/* half a second */
   1624 #endif	/* SD_DUMP_NOT_TRUSTED */
   1625 
   1626 		/* update block count */
   1627 		totwrt -= nwrt;
   1628 		blkno += nwrt;
   1629 		va = (char *)va + sectorsize * nwrt;
   1630 	}
   1631 	sddoingadump = 0;
   1632 	return (0);
   1633 }
   1634 
   1635 static int
   1636 sd_mode_sense(struct sd_softc *sd, u_int8_t byte2, void *sense, size_t size,
   1637     int page, int flags, int *big)
   1638 {
   1639 
   1640 	if ((sd->sc_periph->periph_quirks & PQUIRK_ONLYBIG) &&
   1641 	    !(sd->sc_periph->periph_quirks & PQUIRK_NOBIGMODESENSE)) {
   1642 		*big = 1;
   1643 		return scsipi_mode_sense_big(sd->sc_periph, byte2, page, sense,
   1644 		    size + sizeof(struct scsi_mode_parameter_header_10),
   1645 		    flags | XS_CTL_DATA_ONSTACK, SDRETRIES, 6000);
   1646 	} else {
   1647 		*big = 0;
   1648 		return scsipi_mode_sense(sd->sc_periph, byte2, page, sense,
   1649 		    size + sizeof(struct scsi_mode_parameter_header_6),
   1650 		    flags | XS_CTL_DATA_ONSTACK, SDRETRIES, 6000);
   1651 	}
   1652 }
   1653 
   1654 static int
   1655 sd_mode_select(struct sd_softc *sd, u_int8_t byte2, void *sense, size_t size,
   1656     int flags, int big)
   1657 {
   1658 
   1659 	if (big) {
   1660 		struct scsi_mode_parameter_header_10 *header = sense;
   1661 
   1662 		_lto2b(0, header->data_length);
   1663 		return scsipi_mode_select_big(sd->sc_periph, byte2, sense,
   1664 		    size + sizeof(struct scsi_mode_parameter_header_10),
   1665 		    flags | XS_CTL_DATA_ONSTACK, SDRETRIES, 6000);
   1666 	} else {
   1667 		struct scsi_mode_parameter_header_6 *header = sense;
   1668 
   1669 		header->data_length = 0;
   1670 		return scsipi_mode_select(sd->sc_periph, byte2, sense,
   1671 		    size + sizeof(struct scsi_mode_parameter_header_6),
   1672 		    flags | XS_CTL_DATA_ONSTACK, SDRETRIES, 6000);
   1673 	}
   1674 }
   1675 
   1676 /*
   1677  * sd_validate_blksize:
   1678  *
   1679  *	Validate the block size.  Print error if periph is specified,
   1680  */
   1681 static int
   1682 sd_validate_blksize(struct scsipi_periph *periph, int len)
   1683 {
   1684 
   1685 	switch (len) {
   1686 	case 256:
   1687 	case 512:
   1688 	case 1024:
   1689 	case 2048:
   1690 	case 4096:
   1691 		return 1;
   1692 	}
   1693 
   1694 	if (periph) {
   1695 		scsipi_printaddr(periph);
   1696 		printf("%s sector size: 0x%x.  Defaulting to %d bytes.\n",
   1697 		    (len ^ (1 << (ffs(len) - 1))) ?
   1698 		    "preposterous" : "unsupported",
   1699 		    len, SD_DEFAULT_BLKSIZE);
   1700 	}
   1701 
   1702 	return 0;
   1703 }
   1704 
   1705 /*
   1706  * sd_read_capacity:
   1707  *
   1708  *	Find out from the device what its capacity is.
   1709  */
   1710 static u_int64_t
   1711 sd_read_capacity(struct scsipi_periph *periph, int *blksize, int flags)
   1712 {
   1713 	union {
   1714 		struct scsipi_read_capacity_10 cmd;
   1715 		struct scsipi_read_capacity_16 cmd16;
   1716 	} cmd;
   1717 	union {
   1718 		struct scsipi_read_capacity_10_data data;
   1719 		struct scsipi_read_capacity_16_data data16;
   1720 	} *datap;
   1721 	uint64_t rv;
   1722 
   1723 	memset(&cmd, 0, sizeof(cmd));
   1724 	cmd.cmd.opcode = READ_CAPACITY_10;
   1725 
   1726 	/*
   1727 	 * Don't allocate data buffer on stack;
   1728 	 * The lower driver layer might use the same stack and
   1729 	 * if it uses region which is in the same cacheline,
   1730 	 * cache flush ops against the data buffer won't work properly.
   1731 	 */
   1732 	datap = malloc(sizeof(*datap), M_TEMP, M_WAITOK);
   1733 	if (datap == NULL)
   1734 		return 0;
   1735 
   1736 	/*
   1737 	 * If the command works, interpret the result as a 4 byte
   1738 	 * number of blocks
   1739 	 */
   1740 	rv = 0;
   1741 	memset(datap, 0, sizeof(datap->data));
   1742 	if (scsipi_command(periph, (void *)&cmd.cmd, sizeof(cmd.cmd),
   1743 	    (void *)datap, sizeof(datap->data), SCSIPIRETRIES, 20000, NULL,
   1744 	    flags | XS_CTL_DATA_IN | XS_CTL_SILENT) != 0)
   1745 		goto out;
   1746 
   1747 	if (_4btol(datap->data.addr) != 0xffffffff) {
   1748 		*blksize = _4btol(datap->data.length);
   1749 		rv = _4btol(datap->data.addr) + 1;
   1750 		goto out;
   1751 	}
   1752 
   1753 	/*
   1754 	 * Device is larger than can be reflected by READ CAPACITY (10).
   1755 	 * Try READ CAPACITY (16).
   1756 	 */
   1757 
   1758 	memset(&cmd, 0, sizeof(cmd));
   1759 	cmd.cmd16.opcode = READ_CAPACITY_16;
   1760 	cmd.cmd16.byte2 = SRC16_SERVICE_ACTION;
   1761 	_lto4b(sizeof(datap->data16), cmd.cmd16.len);
   1762 
   1763 	memset(datap, 0, sizeof(datap->data16));
   1764 	if (scsipi_command(periph, (void *)&cmd.cmd16, sizeof(cmd.cmd16),
   1765 	    (void *)datap, sizeof(datap->data16), SCSIPIRETRIES, 20000, NULL,
   1766 	    flags | XS_CTL_DATA_IN | XS_CTL_SILENT) != 0)
   1767 		goto out;
   1768 
   1769 	*blksize = _4btol(datap->data16.length);
   1770 	rv = _8btol(datap->data16.addr) + 1;
   1771 
   1772  out:
   1773 	free(datap, M_TEMP);
   1774 	return rv;
   1775 }
   1776 
   1777 static int
   1778 sd_get_simplifiedparms(struct sd_softc *sd, struct disk_parms *dp, int flags)
   1779 {
   1780 	struct {
   1781 		struct scsi_mode_parameter_header_6 header;
   1782 		/* no block descriptor */
   1783 		u_int8_t pg_code; /* page code (should be 6) */
   1784 		u_int8_t pg_length; /* page length (should be 11) */
   1785 		u_int8_t wcd; /* bit0: cache disable */
   1786 		u_int8_t lbs[2]; /* logical block size */
   1787 		u_int8_t size[5]; /* number of log. blocks */
   1788 		u_int8_t pp; /* power/performance */
   1789 		u_int8_t flags;
   1790 		u_int8_t resvd;
   1791 	} scsipi_sense;
   1792 	u_int64_t blocks;
   1793 	int error, blksize;
   1794 
   1795 	/*
   1796 	 * sd_read_capacity (ie "read capacity") and mode sense page 6
   1797 	 * give the same information. Do both for now, and check
   1798 	 * for consistency.
   1799 	 * XXX probably differs for removable media
   1800 	 */
   1801 	dp->blksize = SD_DEFAULT_BLKSIZE;
   1802 	if ((blocks = sd_read_capacity(sd->sc_periph, &blksize, flags)) == 0)
   1803 		return (SDGP_RESULT_OFFLINE);		/* XXX? */
   1804 
   1805 	error = scsipi_mode_sense(sd->sc_periph, SMS_DBD, 6,
   1806 	    &scsipi_sense.header, sizeof(scsipi_sense),
   1807 	    flags | XS_CTL_DATA_ONSTACK, SDRETRIES, 6000);
   1808 
   1809 	if (error != 0)
   1810 		return (SDGP_RESULT_OFFLINE);		/* XXX? */
   1811 
   1812 	dp->blksize = blksize;
   1813 	if (!sd_validate_blksize(NULL, dp->blksize))
   1814 		dp->blksize = _2btol(scsipi_sense.lbs);
   1815 	if (!sd_validate_blksize(sd->sc_periph, dp->blksize))
   1816 		dp->blksize = SD_DEFAULT_BLKSIZE;
   1817 
   1818 	/*
   1819 	 * Create a pseudo-geometry.
   1820 	 */
   1821 	dp->heads = 64;
   1822 	dp->sectors = 32;
   1823 	dp->cyls = blocks / (dp->heads * dp->sectors);
   1824 	dp->disksize = _5btol(scsipi_sense.size);
   1825 	if (dp->disksize <= UINT32_MAX && dp->disksize != blocks) {
   1826 		printf("RBC size: mode sense=%llu, get cap=%llu\n",
   1827 		       (unsigned long long)dp->disksize,
   1828 		       (unsigned long long)blocks);
   1829 		dp->disksize = blocks;
   1830 	}
   1831 	dp->disksize512 = (dp->disksize * dp->blksize) / DEV_BSIZE;
   1832 
   1833 	return (SDGP_RESULT_OK);
   1834 }
   1835 
   1836 /*
   1837  * Get the scsi driver to send a full inquiry to the * device and use the
   1838  * results to fill out the disk parameter structure.
   1839  */
   1840 static int
   1841 sd_get_capacity(struct sd_softc *sd, struct disk_parms *dp, int flags)
   1842 {
   1843 	u_int64_t blocks;
   1844 	int error, blksize;
   1845 #if 0
   1846 	int i;
   1847 	u_int8_t *p;
   1848 #endif
   1849 
   1850 	dp->disksize = blocks = sd_read_capacity(sd->sc_periph, &blksize,
   1851 	    flags);
   1852 	if (blocks == 0) {
   1853 		struct scsipi_read_format_capacities cmd;
   1854 		struct {
   1855 			struct scsipi_capacity_list_header header;
   1856 			struct scsipi_capacity_descriptor desc;
   1857 		} __packed data;
   1858 
   1859 		memset(&cmd, 0, sizeof(cmd));
   1860 		memset(&data, 0, sizeof(data));
   1861 		cmd.opcode = READ_FORMAT_CAPACITIES;
   1862 		_lto2b(sizeof(data), cmd.length);
   1863 
   1864 		error = scsipi_command(sd->sc_periph,
   1865 		    (void *)&cmd, sizeof(cmd), (void *)&data, sizeof(data),
   1866 		    SDRETRIES, 20000, NULL,
   1867 		    flags | XS_CTL_DATA_IN | XS_CTL_DATA_ONSTACK);
   1868 		if (error == EFTYPE) {
   1869 			/* Medium Format Corrupted, handle as not formatted */
   1870 			return (SDGP_RESULT_UNFORMATTED);
   1871 		}
   1872 		if (error || data.header.length == 0)
   1873 			return (SDGP_RESULT_OFFLINE);
   1874 
   1875 #if 0
   1876 printf("rfc: length=%d\n", data.header.length);
   1877 printf("rfc result:"); for (i = sizeof(struct scsipi_capacity_list_header) + data.header.length, p = (void *)&data; i; i--, p++) printf(" %02x", *p); printf("\n");
   1878 #endif
   1879 		switch (data.desc.byte5 & SCSIPI_CAP_DESC_CODE_MASK) {
   1880 		case SCSIPI_CAP_DESC_CODE_RESERVED:
   1881 		case SCSIPI_CAP_DESC_CODE_FORMATTED:
   1882 			break;
   1883 
   1884 		case SCSIPI_CAP_DESC_CODE_UNFORMATTED:
   1885 			return (SDGP_RESULT_UNFORMATTED);
   1886 
   1887 		case SCSIPI_CAP_DESC_CODE_NONE:
   1888 			return (SDGP_RESULT_OFFLINE);
   1889 		}
   1890 
   1891 		dp->disksize = blocks = _4btol(data.desc.nblks);
   1892 		if (blocks == 0)
   1893 			return (SDGP_RESULT_OFFLINE);		/* XXX? */
   1894 
   1895 		blksize = _3btol(data.desc.blklen);
   1896 
   1897 	} else if (!sd_validate_blksize(NULL, blksize)) {
   1898 		struct sd_mode_sense_data scsipi_sense;
   1899 		int big, bsize;
   1900 		struct scsi_general_block_descriptor *bdesc;
   1901 
   1902 		memset(&scsipi_sense, 0, sizeof(scsipi_sense));
   1903 		error = sd_mode_sense(sd, 0, &scsipi_sense,
   1904 		    sizeof(scsipi_sense.blk_desc), 0, flags | XS_CTL_SILENT, &big);
   1905 		if (!error) {
   1906 			if (big) {
   1907 				bdesc = (void *)(&scsipi_sense.header.big + 1);
   1908 				bsize = _2btol(scsipi_sense.header.big.blk_desc_len);
   1909 			} else {
   1910 				bdesc = (void *)(&scsipi_sense.header.small + 1);
   1911 				bsize = scsipi_sense.header.small.blk_desc_len;
   1912 			}
   1913 
   1914 #if 0
   1915 printf("page 0 sense:"); for (i = sizeof(scsipi_sense), p = (void *)&scsipi_sense; i; i--, p++) printf(" %02x", *p); printf("\n");
   1916 printf("page 0 bsize=%d\n", bsize);
   1917 printf("page 0 ok\n");
   1918 #endif
   1919 
   1920 			if (bsize >= 8) {
   1921 				blksize = _3btol(bdesc->blklen);
   1922 			}
   1923 		}
   1924 	}
   1925 
   1926 	if (!sd_validate_blksize(sd->sc_periph, blksize))
   1927 		blksize = SD_DEFAULT_BLKSIZE;
   1928 
   1929 	dp->blksize = blksize;
   1930 	dp->disksize512 = (blocks * dp->blksize) / DEV_BSIZE;
   1931 	return (0);
   1932 }
   1933 
   1934 static int
   1935 sd_get_parms_page4(struct sd_softc *sd, struct disk_parms *dp, int flags)
   1936 {
   1937 	struct sd_mode_sense_data scsipi_sense;
   1938 	int error;
   1939 	int big, byte2;
   1940 	size_t poffset;
   1941 	union scsi_disk_pages *pages;
   1942 
   1943 	byte2 = SMS_DBD;
   1944 again:
   1945 	memset(&scsipi_sense, 0, sizeof(scsipi_sense));
   1946 	error = sd_mode_sense(sd, byte2, &scsipi_sense,
   1947 	    (byte2 ? 0 : sizeof(scsipi_sense.blk_desc)) +
   1948 	    sizeof(scsipi_sense.pages.rigid_geometry), 4,
   1949 	    flags | XS_CTL_SILENT, &big);
   1950 	if (error) {
   1951 		if (byte2 == SMS_DBD) {
   1952 			/* No result; try once more with DBD off */
   1953 			byte2 = 0;
   1954 			goto again;
   1955 		}
   1956 		return (error);
   1957 	}
   1958 
   1959 	if (big) {
   1960 		poffset = sizeof scsipi_sense.header.big;
   1961 		poffset += _2btol(scsipi_sense.header.big.blk_desc_len);
   1962 	} else {
   1963 		poffset = sizeof scsipi_sense.header.small;
   1964 		poffset += scsipi_sense.header.small.blk_desc_len;
   1965 	}
   1966 
   1967 	if (poffset > sizeof(scsipi_sense) - sizeof(pages->rigid_geometry))
   1968 		return ERESTART;
   1969 
   1970 	pages = (void *)((u_long)&scsipi_sense + poffset);
   1971 #if 0
   1972 	{
   1973 		size_t i;
   1974 		u_int8_t *p;
   1975 
   1976 		printf("page 4 sense:");
   1977 		for (i = sizeof(scsipi_sense), p = (void *)&scsipi_sense; i;
   1978 		    i--, p++)
   1979 			printf(" %02x", *p);
   1980 		printf("\n");
   1981 		printf("page 4 pg_code=%d sense=%p/%p\n",
   1982 		    pages->rigid_geometry.pg_code, &scsipi_sense, pages);
   1983 	}
   1984 #endif
   1985 
   1986 	if ((pages->rigid_geometry.pg_code & PGCODE_MASK) != 4)
   1987 		return (ERESTART);
   1988 
   1989 	SC_DEBUG(sd->sc_periph, SCSIPI_DB3,
   1990 	    ("%d cyls, %d heads, %d precomp, %d red_write, %d land_zone\n",
   1991 	    _3btol(pages->rigid_geometry.ncyl),
   1992 	    pages->rigid_geometry.nheads,
   1993 	    _2btol(pages->rigid_geometry.st_cyl_wp),
   1994 	    _2btol(pages->rigid_geometry.st_cyl_rwc),
   1995 	    _2btol(pages->rigid_geometry.land_zone)));
   1996 
   1997 	/*
   1998 	 * KLUDGE!! (for zone recorded disks)
   1999 	 * give a number of sectors so that sec * trks * cyls
   2000 	 * is <= disk_size
   2001 	 * can lead to wasted space! THINK ABOUT THIS !
   2002 	 */
   2003 	dp->heads = pages->rigid_geometry.nheads;
   2004 	dp->cyls = _3btol(pages->rigid_geometry.ncyl);
   2005 	if (dp->heads == 0 || dp->cyls == 0)
   2006 		return (ERESTART);
   2007 	dp->sectors = dp->disksize / (dp->heads * dp->cyls);	/* XXX */
   2008 
   2009 	dp->rot_rate = _2btol(pages->rigid_geometry.rpm);
   2010 	if (dp->rot_rate == 0)
   2011 		dp->rot_rate = 3600;
   2012 
   2013 #if 0
   2014 printf("page 4 ok\n");
   2015 #endif
   2016 	return (0);
   2017 }
   2018 
   2019 static int
   2020 sd_get_parms_page5(struct sd_softc *sd, struct disk_parms *dp, int flags)
   2021 {
   2022 	struct sd_mode_sense_data scsipi_sense;
   2023 	int error;
   2024 	int big, byte2;
   2025 	size_t poffset;
   2026 	union scsi_disk_pages *pages;
   2027 
   2028 	byte2 = SMS_DBD;
   2029 again:
   2030 	memset(&scsipi_sense, 0, sizeof(scsipi_sense));
   2031 	error = sd_mode_sense(sd, 0, &scsipi_sense,
   2032 	    (byte2 ? 0 : sizeof(scsipi_sense.blk_desc)) +
   2033 	    sizeof(scsipi_sense.pages.flex_geometry), 5,
   2034 	    flags | XS_CTL_SILENT, &big);
   2035 	if (error) {
   2036 		if (byte2 == SMS_DBD) {
   2037 			/* No result; try once more with DBD off */
   2038 			byte2 = 0;
   2039 			goto again;
   2040 		}
   2041 		return (error);
   2042 	}
   2043 
   2044 	if (big) {
   2045 		poffset = sizeof scsipi_sense.header.big;
   2046 		poffset += _2btol(scsipi_sense.header.big.blk_desc_len);
   2047 	} else {
   2048 		poffset = sizeof scsipi_sense.header.small;
   2049 		poffset += scsipi_sense.header.small.blk_desc_len;
   2050 	}
   2051 
   2052 	if (poffset > sizeof(scsipi_sense) - sizeof(pages->flex_geometry))
   2053 		return ERESTART;
   2054 
   2055 	pages = (void *)((u_long)&scsipi_sense + poffset);
   2056 #if 0
   2057 	{
   2058 		size_t i;
   2059 		u_int8_t *p;
   2060 
   2061 		printf("page 5 sense:");
   2062 		for (i = sizeof(scsipi_sense), p = (void *)&scsipi_sense; i;
   2063 		    i--, p++)
   2064 			printf(" %02x", *p);
   2065 		printf("\n");
   2066 		printf("page 5 pg_code=%d sense=%p/%p\n",
   2067 		    pages->flex_geometry.pg_code, &scsipi_sense, pages);
   2068 	}
   2069 #endif
   2070 
   2071 	if ((pages->flex_geometry.pg_code & PGCODE_MASK) != 5)
   2072 		return (ERESTART);
   2073 
   2074 	SC_DEBUG(sd->sc_periph, SCSIPI_DB3,
   2075 	    ("%d cyls, %d heads, %d sec, %d bytes/sec\n",
   2076 	    _3btol(pages->flex_geometry.ncyl),
   2077 	    pages->flex_geometry.nheads,
   2078 	    pages->flex_geometry.ph_sec_tr,
   2079 	    _2btol(pages->flex_geometry.bytes_s)));
   2080 
   2081 	dp->heads = pages->flex_geometry.nheads;
   2082 	dp->cyls = _2btol(pages->flex_geometry.ncyl);
   2083 	dp->sectors = pages->flex_geometry.ph_sec_tr;
   2084 	if (dp->heads == 0 || dp->cyls == 0 || dp->sectors == 0)
   2085 		return (ERESTART);
   2086 
   2087 	dp->rot_rate = _2btol(pages->rigid_geometry.rpm);
   2088 	if (dp->rot_rate == 0)
   2089 		dp->rot_rate = 3600;
   2090 
   2091 #if 0
   2092 printf("page 5 ok\n");
   2093 #endif
   2094 	return (0);
   2095 }
   2096 
   2097 static int
   2098 sd_get_parms(struct sd_softc *sd, struct disk_parms *dp, int flags)
   2099 {
   2100 	int error;
   2101 
   2102 	/*
   2103 	 * If offline, the SDEV_MEDIA_LOADED flag will be
   2104 	 * cleared by the caller if necessary.
   2105 	 */
   2106 	if (sd->type == T_SIMPLE_DIRECT) {
   2107 		error = sd_get_simplifiedparms(sd, dp, flags);
   2108 		if (!error)
   2109 			disk_blocksize(&sd->sc_dk, dp->blksize);
   2110 		return (error);
   2111 	}
   2112 
   2113 	error = sd_get_capacity(sd, dp, flags);
   2114 	if (error)
   2115 		return (error);
   2116 
   2117 	disk_blocksize(&sd->sc_dk, dp->blksize);
   2118 
   2119 	if (sd->type == T_OPTICAL)
   2120 		goto page0;
   2121 
   2122 	if (sd->sc_periph->periph_flags & PERIPH_REMOVABLE) {
   2123 		if (!sd_get_parms_page5(sd, dp, flags) ||
   2124 		    !sd_get_parms_page4(sd, dp, flags))
   2125 			return (SDGP_RESULT_OK);
   2126 	} else {
   2127 		if (!sd_get_parms_page4(sd, dp, flags) ||
   2128 		    !sd_get_parms_page5(sd, dp, flags))
   2129 			return (SDGP_RESULT_OK);
   2130 	}
   2131 
   2132 page0:
   2133 	printf("%s: fabricating a geometry\n", sd->sc_dev.dv_xname);
   2134 	/* Try calling driver's method for figuring out geometry. */
   2135 	if (!sd->sc_periph->periph_channel->chan_adapter->adapt_getgeom ||
   2136 	    !(*sd->sc_periph->periph_channel->chan_adapter->adapt_getgeom)
   2137 		(sd->sc_periph, dp, dp->disksize)) {
   2138 		/*
   2139 		 * Use adaptec standard fictitious geometry
   2140 		 * this depends on which controller (e.g. 1542C is
   2141 		 * different. but we have to put SOMETHING here..)
   2142 		 */
   2143 		dp->heads = 64;
   2144 		dp->sectors = 32;
   2145 		dp->cyls = dp->disksize / (64 * 32);
   2146 	}
   2147 	dp->rot_rate = 3600;
   2148 	return (SDGP_RESULT_OK);
   2149 }
   2150 
   2151 static int
   2152 sd_flush(struct sd_softc *sd, int flags)
   2153 {
   2154 	struct scsipi_periph *periph = sd->sc_periph;
   2155 	struct scsi_synchronize_cache_10 cmd;
   2156 
   2157 	/*
   2158 	 * If the device is SCSI-2, issue a SYNCHRONIZE CACHE.
   2159 	 * We issue with address 0 length 0, which should be
   2160 	 * interpreted by the device as "all remaining blocks
   2161 	 * starting at address 0".  We ignore ILLEGAL REQUEST
   2162 	 * in the event that the command is not supported by
   2163 	 * the device, and poll for completion so that we know
   2164 	 * that the cache has actually been flushed.
   2165 	 *
   2166 	 * Unless, that is, the device can't handle the SYNCHRONIZE CACHE
   2167 	 * command, as indicated by our quirks flags.
   2168 	 *
   2169 	 * XXX What about older devices?
   2170 	 */
   2171 	if (periph->periph_version < 2 ||
   2172 	    (periph->periph_quirks & PQUIRK_NOSYNCCACHE))
   2173 		return (0);
   2174 
   2175 	sd->flags |= SDF_FLUSHING;
   2176 	memset(&cmd, 0, sizeof(cmd));
   2177 	cmd.opcode = SCSI_SYNCHRONIZE_CACHE_10;
   2178 
   2179 	return (scsipi_command(periph, (void *)&cmd, sizeof(cmd), 0, 0,
   2180 	    SDRETRIES, 100000, NULL, flags | XS_CTL_IGNORE_ILLEGAL_REQUEST));
   2181 }
   2182 
   2183 static int
   2184 sd_getcache(struct sd_softc *sd, int *bitsp)
   2185 {
   2186 	struct scsipi_periph *periph = sd->sc_periph;
   2187 	struct sd_mode_sense_data scsipi_sense;
   2188 	int error, bits = 0;
   2189 	int big;
   2190 	union scsi_disk_pages *pages;
   2191 
   2192 	if (periph->periph_version < 2)
   2193 		return (EOPNOTSUPP);
   2194 
   2195 	memset(&scsipi_sense, 0, sizeof(scsipi_sense));
   2196 	error = sd_mode_sense(sd, SMS_DBD, &scsipi_sense,
   2197 	    sizeof(scsipi_sense.pages.caching_params), 8, 0, &big);
   2198 	if (error)
   2199 		return (error);
   2200 
   2201 	if (big)
   2202 		pages = (void *)(&scsipi_sense.header.big + 1);
   2203 	else
   2204 		pages = (void *)(&scsipi_sense.header.small + 1);
   2205 
   2206 	if ((pages->caching_params.flags & CACHING_RCD) == 0)
   2207 		bits |= DKCACHE_READ;
   2208 	if (pages->caching_params.flags & CACHING_WCE)
   2209 		bits |= DKCACHE_WRITE;
   2210 	if (pages->caching_params.pg_code & PGCODE_PS)
   2211 		bits |= DKCACHE_SAVE;
   2212 
   2213 	memset(&scsipi_sense, 0, sizeof(scsipi_sense));
   2214 	error = sd_mode_sense(sd, SMS_DBD, &scsipi_sense,
   2215 	    sizeof(scsipi_sense.pages.caching_params),
   2216 	    SMS_PCTRL_CHANGEABLE|8, 0, &big);
   2217 	if (error == 0) {
   2218 		if (big)
   2219 			pages = (void *)(&scsipi_sense.header.big + 1);
   2220 		else
   2221 			pages = (void *)(&scsipi_sense.header.small + 1);
   2222 
   2223 		if (pages->caching_params.flags & CACHING_RCD)
   2224 			bits |= DKCACHE_RCHANGE;
   2225 		if (pages->caching_params.flags & CACHING_WCE)
   2226 			bits |= DKCACHE_WCHANGE;
   2227 	}
   2228 
   2229 	*bitsp = bits;
   2230 
   2231 	return (0);
   2232 }
   2233 
   2234 static int
   2235 sd_setcache(struct sd_softc *sd, int bits)
   2236 {
   2237 	struct scsipi_periph *periph = sd->sc_periph;
   2238 	struct sd_mode_sense_data scsipi_sense;
   2239 	int error;
   2240 	uint8_t oflags, byte2 = 0;
   2241 	int big;
   2242 	union scsi_disk_pages *pages;
   2243 
   2244 	if (periph->periph_version < 2)
   2245 		return (EOPNOTSUPP);
   2246 
   2247 	memset(&scsipi_sense, 0, sizeof(scsipi_sense));
   2248 	error = sd_mode_sense(sd, SMS_DBD, &scsipi_sense,
   2249 	    sizeof(scsipi_sense.pages.caching_params), 8, 0, &big);
   2250 	if (error)
   2251 		return (error);
   2252 
   2253 	if (big)
   2254 		pages = (void *)(&scsipi_sense.header.big + 1);
   2255 	else
   2256 		pages = (void *)(&scsipi_sense.header.small + 1);
   2257 
   2258 	oflags = pages->caching_params.flags;
   2259 
   2260 	if (bits & DKCACHE_READ)
   2261 		pages->caching_params.flags &= ~CACHING_RCD;
   2262 	else
   2263 		pages->caching_params.flags |= CACHING_RCD;
   2264 
   2265 	if (bits & DKCACHE_WRITE)
   2266 		pages->caching_params.flags |= CACHING_WCE;
   2267 	else
   2268 		pages->caching_params.flags &= ~CACHING_WCE;
   2269 
   2270 	if (oflags == pages->caching_params.flags)
   2271 		return (0);
   2272 
   2273 	pages->caching_params.pg_code &= PGCODE_MASK;
   2274 
   2275 	if (bits & DKCACHE_SAVE)
   2276 		byte2 |= SMS_SP;
   2277 
   2278 	return (sd_mode_select(sd, byte2|SMS_PF, &scsipi_sense,
   2279 	    sizeof(struct scsi_mode_page_header) +
   2280 	    pages->caching_params.pg_length, 0, big));
   2281 }
   2282 
   2283 static void
   2284 sd_set_properties(struct sd_softc *sd)
   2285 {
   2286 	prop_dictionary_t disk_info, odisk_info, geom;
   2287 
   2288 	disk_info = prop_dictionary_create();
   2289 
   2290 	geom = prop_dictionary_create();
   2291 
   2292 	prop_dictionary_set_uint64(geom, "sectors-per-unit",
   2293 	    sd->params.disksize);
   2294 
   2295 	prop_dictionary_set_uint32(geom, "sector-size",
   2296 	    sd->params.blksize);
   2297 
   2298 	prop_dictionary_set_uint16(geom, "sectors-per-track",
   2299 	    sd->params.sectors);
   2300 
   2301 	prop_dictionary_set_uint16(geom, "tracks-per-cylinder",
   2302 	    sd->params.heads);
   2303 
   2304 	prop_dictionary_set_uint64(geom, "cylinders-per-unit",
   2305 	    sd->params.cyls);
   2306 
   2307 	prop_dictionary_set(disk_info, "geometry", geom);
   2308 	prop_object_release(geom);
   2309 
   2310 	prop_dictionary_set(device_properties(&sd->sc_dev),
   2311 	    "disk-info", disk_info);
   2312 
   2313 	/*
   2314 	 * Don't release disk_info here; we keep a reference to it.
   2315 	 * disk_detach() will release it when we go away.
   2316 	 */
   2317 
   2318 	odisk_info = sd->sc_dk.dk_info;
   2319 	sd->sc_dk.dk_info = disk_info;
   2320 	if (odisk_info)
   2321 		prop_object_release(odisk_info);
   2322 }
   2323