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