Home | History | Annotate | Line # | Download | only in scsipi
sd.c revision 1.269.6.5
      1 /*	$NetBSD: sd.c,v 1.269.6.5 2008/06/29 09:33:10 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.5 2008/06/29 09:33:10 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 	sd = device_lookup_private(&sd_cd, unit);
    440 	if (sd == NULL)
    441 		return (ENXIO);
    442 
    443 	if (!device_is_active(&sd->sc_dev))
    444 		return (ENODEV);
    445 
    446 	part = SDPART(dev);
    447 
    448 	mutex_enter(&sd->sc_dk.dk_openlock);
    449 
    450 	/*
    451 	 * If there are wedges, and this is not RAW_PART, then we
    452 	 * need to fail.
    453 	 */
    454 	if (sd->sc_dk.dk_nwedges != 0 && part != RAW_PART) {
    455 		error = EBUSY;
    456 		goto bad1;
    457 	}
    458 
    459 	periph = sd->sc_periph;
    460 	adapt = periph->periph_channel->chan_adapter;
    461 
    462 	SC_DEBUG(periph, SCSIPI_DB1,
    463 	    ("sdopen: dev=0x%x (unit %d (of %d), partition %d)\n", dev, unit,
    464 	    sd_cd.cd_ndevs, part));
    465 
    466 	/*
    467 	 * If this is the first open of this device, add a reference
    468 	 * to the adapter.
    469 	 */
    470 	if (sd->sc_dk.dk_openmask == 0 &&
    471 	    (error = scsipi_adapter_addref(adapt)) != 0)
    472 		goto bad1;
    473 
    474 	if ((periph->periph_flags & PERIPH_OPEN) != 0) {
    475 		/*
    476 		 * If any partition is open, but the disk has been invalidated,
    477 		 * disallow further opens of non-raw partition
    478 		 */
    479 		if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0 &&
    480 		    (part != RAW_PART || fmt != S_IFCHR)) {
    481 			error = EIO;
    482 			goto bad2;
    483 		}
    484 	} else {
    485 		int silent;
    486 
    487 		if (part == RAW_PART && fmt == S_IFCHR)
    488 			silent = XS_CTL_SILENT;
    489 		else
    490 			silent = 0;
    491 
    492 		/* Check that it is still responding and ok. */
    493 		error = scsipi_test_unit_ready(periph,
    494 		    XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_MEDIA_CHANGE |
    495 		    silent);
    496 
    497 		/*
    498 		 * Start the pack spinning if necessary. Always allow the
    499 		 * raw parition to be opened, for raw IOCTLs. Data transfers
    500 		 * will check for SDEV_MEDIA_LOADED.
    501 		 */
    502 		if (error == EIO) {
    503 			int error2;
    504 
    505 			error2 = scsipi_start(periph, SSS_START, silent);
    506 			switch (error2) {
    507 			case 0:
    508 				error = 0;
    509 				break;
    510 			case EIO:
    511 			case EINVAL:
    512 				break;
    513 			default:
    514 				error = error2;
    515 				break;
    516 			}
    517 		}
    518 		if (error) {
    519 			if (silent)
    520 				goto out;
    521 			goto bad2;
    522 		}
    523 
    524 		periph->periph_flags |= PERIPH_OPEN;
    525 
    526 		if (periph->periph_flags & PERIPH_REMOVABLE) {
    527 			/* Lock the pack in. */
    528 			error = scsipi_prevent(periph, SPAMR_PREVENT_DT,
    529 			    XS_CTL_IGNORE_ILLEGAL_REQUEST |
    530 			    XS_CTL_IGNORE_MEDIA_CHANGE |
    531 			    XS_CTL_SILENT);
    532 			if (error)
    533 				goto bad3;
    534 		}
    535 
    536 		if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) {
    537 			int param_error;
    538 			periph->periph_flags |= PERIPH_MEDIA_LOADED;
    539 
    540 			/*
    541 			 * Load the physical device parameters.
    542 			 *
    543 			 * Note that if media is present but unformatted,
    544 			 * we allow the open (so that it can be formatted!).
    545 			 * The drive should refuse real I/O, if the media is
    546 			 * unformatted.
    547 			 */
    548 			if ((param_error = sd_get_parms(sd, &sd->params, 0))
    549 			     == SDGP_RESULT_OFFLINE) {
    550 				error = ENXIO;
    551 				periph->periph_flags &= ~PERIPH_MEDIA_LOADED;
    552 				goto bad3;
    553 			}
    554 			SC_DEBUG(periph, SCSIPI_DB3, ("Params loaded "));
    555 
    556 			/* Load the partition info if not already loaded. */
    557 			if (param_error == 0) {
    558 				if ((sdgetdisklabel(sd) != 0) && (part != RAW_PART)) {
    559 					error = EIO;
    560 					goto bad3;
    561 				}
    562 				SC_DEBUG(periph, SCSIPI_DB3,
    563 				     ("Disklabel loaded "));
    564 			}
    565 		}
    566 	}
    567 
    568 	/* Check that the partition exists. */
    569 	if (part != RAW_PART &&
    570 	    (part >= sd->sc_dk.dk_label->d_npartitions ||
    571 	     sd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
    572 		error = ENXIO;
    573 		goto bad3;
    574 	}
    575 
    576  out:	/* Insure only one open at a time. */
    577 	switch (fmt) {
    578 	case S_IFCHR:
    579 		sd->sc_dk.dk_copenmask |= (1 << part);
    580 		break;
    581 	case S_IFBLK:
    582 		sd->sc_dk.dk_bopenmask |= (1 << part);
    583 		break;
    584 	}
    585 	sd->sc_dk.dk_openmask =
    586 	    sd->sc_dk.dk_copenmask | sd->sc_dk.dk_bopenmask;
    587 
    588 	SC_DEBUG(periph, SCSIPI_DB3, ("open complete\n"));
    589 	mutex_exit(&sd->sc_dk.dk_openlock);
    590 	return (0);
    591 
    592  bad3:
    593 	if (sd->sc_dk.dk_openmask == 0) {
    594 		if (periph->periph_flags & PERIPH_REMOVABLE)
    595 			scsipi_prevent(periph, SPAMR_ALLOW,
    596 			    XS_CTL_IGNORE_ILLEGAL_REQUEST |
    597 			    XS_CTL_IGNORE_MEDIA_CHANGE |
    598 			    XS_CTL_SILENT);
    599 		periph->periph_flags &= ~PERIPH_OPEN;
    600 	}
    601 
    602  bad2:
    603 	if (sd->sc_dk.dk_openmask == 0)
    604 		scsipi_adapter_delref(adapt);
    605 
    606  bad1:
    607 	mutex_exit(&sd->sc_dk.dk_openlock);
    608 	return (error);
    609 }
    610 
    611 /*
    612  * close the device.. only called if we are the LAST occurence of an open
    613  * device.  Convenient now but usually a pain.
    614  */
    615 static int
    616 sdclose(dev_t dev, int flag, int fmt, struct lwp *l)
    617 {
    618 	struct sd_softc *sd = device_lookup_private(&sd_cd, SDUNIT(dev));
    619 	struct scsipi_periph *periph = sd->sc_periph;
    620 	struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
    621 	int part = SDPART(dev);
    622 
    623 	mutex_enter(&sd->sc_dk.dk_openlock);
    624 	switch (fmt) {
    625 	case S_IFCHR:
    626 		sd->sc_dk.dk_copenmask &= ~(1 << part);
    627 		break;
    628 	case S_IFBLK:
    629 		sd->sc_dk.dk_bopenmask &= ~(1 << part);
    630 		break;
    631 	}
    632 	sd->sc_dk.dk_openmask =
    633 	    sd->sc_dk.dk_copenmask | sd->sc_dk.dk_bopenmask;
    634 
    635 	if (sd->sc_dk.dk_openmask == 0) {
    636 		/*
    637 		 * If the disk cache needs flushing, and the disk supports
    638 		 * it, do it now.
    639 		 */
    640 		if ((sd->flags & SDF_DIRTY) != 0) {
    641 			if (sd_flush(sd, 0)) {
    642 				aprint_error_dev(&sd->sc_dev, "cache synchronization failed\n");
    643 				sd->flags &= ~SDF_FLUSHING;
    644 			} else
    645 				sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY);
    646 		}
    647 
    648 		scsipi_wait_drain(periph);
    649 
    650 		if (periph->periph_flags & PERIPH_REMOVABLE)
    651 			scsipi_prevent(periph, SPAMR_ALLOW,
    652 			    XS_CTL_IGNORE_ILLEGAL_REQUEST |
    653 			    XS_CTL_IGNORE_NOT_READY |
    654 			    XS_CTL_SILENT);
    655 		periph->periph_flags &= ~PERIPH_OPEN;
    656 
    657 		scsipi_wait_drain(periph);
    658 
    659 		scsipi_adapter_delref(adapt);
    660 	}
    661 
    662 	mutex_exit(&sd->sc_dk.dk_openlock);
    663 	return (0);
    664 }
    665 
    666 /*
    667  * Actually translate the requested transfer into one the physical driver
    668  * can understand.  The transfer is described by a buf and will include
    669  * only one physical transfer.
    670  */
    671 static void
    672 sdstrategy(struct buf *bp)
    673 {
    674 	struct sd_softc *sd = device_lookup_private(&sd_cd, SDUNIT(bp->b_dev));
    675 	struct scsipi_periph *periph = sd->sc_periph;
    676 	struct disklabel *lp;
    677 	daddr_t blkno;
    678 	int s;
    679 	bool sector_aligned;
    680 
    681 	SC_DEBUG(sd->sc_periph, SCSIPI_DB2, ("sdstrategy "));
    682 	SC_DEBUG(sd->sc_periph, SCSIPI_DB1,
    683 	    ("%d bytes @ blk %" PRId64 "\n", bp->b_bcount, bp->b_blkno));
    684 	/*
    685 	 * If the device has been made invalid, error out
    686 	 */
    687 	if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0 ||
    688 	    !device_is_active(&sd->sc_dev)) {
    689 		if (periph->periph_flags & PERIPH_OPEN)
    690 			bp->b_error = EIO;
    691 		else
    692 			bp->b_error = ENODEV;
    693 		goto done;
    694 	}
    695 
    696 	lp = sd->sc_dk.dk_label;
    697 
    698 	/*
    699 	 * The transfer must be a whole number of blocks, offset must not be
    700 	 * negative.
    701 	 */
    702 	if (lp->d_secsize == DEV_BSIZE) {
    703 		sector_aligned = (bp->b_bcount & (DEV_BSIZE - 1)) == 0;
    704 	} else {
    705 		sector_aligned = (bp->b_bcount % lp->d_secsize) == 0;
    706 	}
    707 	if (!sector_aligned || bp->b_blkno < 0) {
    708 		bp->b_error = EINVAL;
    709 		goto done;
    710 	}
    711 	/*
    712 	 * If it's a null transfer, return immediatly
    713 	 */
    714 	if (bp->b_bcount == 0)
    715 		goto done;
    716 
    717 	/*
    718 	 * Do bounds checking, adjust transfer. if error, process.
    719 	 * If end of partition, just return.
    720 	 */
    721 	if (SDPART(bp->b_dev) == RAW_PART) {
    722 		if (bounds_check_with_mediasize(bp, DEV_BSIZE,
    723 		    sd->params.disksize512) <= 0)
    724 			goto done;
    725 	} else {
    726 		if (bounds_check_with_label(&sd->sc_dk, bp,
    727 		    (sd->flags & (SDF_WLABEL|SDF_LABELLING)) != 0) <= 0)
    728 			goto done;
    729 	}
    730 
    731 	/*
    732 	 * Now convert the block number to absolute and put it in
    733 	 * terms of the device's logical block size.
    734 	 */
    735 	if (lp->d_secsize == DEV_BSIZE)
    736 		blkno = bp->b_blkno;
    737 	else if (lp->d_secsize > DEV_BSIZE)
    738 		blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
    739 	else
    740 		blkno = bp->b_blkno * (DEV_BSIZE / lp->d_secsize);
    741 
    742 	if (SDPART(bp->b_dev) != RAW_PART)
    743 		blkno += lp->d_partitions[SDPART(bp->b_dev)].p_offset;
    744 
    745 	bp->b_rawblkno = blkno;
    746 
    747 	s = splbio();
    748 
    749 	/*
    750 	 * Place it in the queue of disk activities for this disk.
    751 	 *
    752 	 * XXX Only do disksort() if the current operating mode does not
    753 	 * XXX include tagged queueing.
    754 	 */
    755 	BUFQ_PUT(sd->buf_queue, bp);
    756 
    757 	/*
    758 	 * Tell the device to get going on the transfer if it's
    759 	 * not doing anything, otherwise just wait for completion
    760 	 */
    761 	sdstart(sd->sc_periph);
    762 
    763 	splx(s);
    764 	return;
    765 
    766 done:
    767 	/*
    768 	 * Correctly set the buf to indicate a completed xfer
    769 	 */
    770 	bp->b_resid = bp->b_bcount;
    771 	biodone(bp);
    772 }
    773 
    774 /*
    775  * sdstart looks to see if there is a buf waiting for the device
    776  * and that the device is not already busy. If both are true,
    777  * It dequeues the buf and creates a scsi command to perform the
    778  * transfer in the buf. The transfer request will call scsipi_done
    779  * on completion, which will in turn call this routine again
    780  * so that the next queued transfer is performed.
    781  * The bufs are queued by the strategy routine (sdstrategy)
    782  *
    783  * This routine is also called after other non-queued requests
    784  * have been made of the scsi driver, to ensure that the queue
    785  * continues to be drained.
    786  *
    787  * must be called at the correct (highish) spl level
    788  * sdstart() is called at splbio from sdstrategy, sdrestart and scsipi_done
    789  */
    790 static void
    791 sdstart(struct scsipi_periph *periph)
    792 {
    793 	struct sd_softc *sd = (void *)periph->periph_dev;
    794 	struct disklabel *lp = sd->sc_dk.dk_label;
    795 	struct buf *bp = 0;
    796 	struct scsipi_rw_16 cmd16;
    797 	struct scsipi_rw_10 cmd_big;
    798 	struct scsi_rw_6 cmd_small;
    799 	struct scsipi_generic *cmdp;
    800 	struct scsipi_xfer *xs;
    801 	int nblks, cmdlen, error, flags;
    802 
    803 	SC_DEBUG(periph, SCSIPI_DB2, ("sdstart "));
    804 	/*
    805 	 * Check if the device has room for another command
    806 	 */
    807 	while (periph->periph_active < periph->periph_openings) {
    808 		/*
    809 		 * there is excess capacity, but a special waits
    810 		 * It'll need the adapter as soon as we clear out of the
    811 		 * way and let it run (user level wait).
    812 		 */
    813 		if (periph->periph_flags & PERIPH_WAITING) {
    814 			periph->periph_flags &= ~PERIPH_WAITING;
    815 			wakeup((void *)periph);
    816 			return;
    817 		}
    818 
    819 		/*
    820 		 * If the device has become invalid, abort all the
    821 		 * reads and writes until all files have been closed and
    822 		 * re-opened
    823 		 */
    824 		if (__predict_false(
    825 		    (periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)) {
    826 			if ((bp = BUFQ_GET(sd->buf_queue)) != NULL) {
    827 				bp->b_error = EIO;
    828 				bp->b_resid = bp->b_bcount;
    829 				biodone(bp);
    830 				continue;
    831 			} else {
    832 				return;
    833 			}
    834 		}
    835 
    836 		/*
    837 		 * See if there is a buf with work for us to do..
    838 		 */
    839 		if ((bp = BUFQ_PEEK(sd->buf_queue)) == NULL)
    840 			return;
    841 
    842 		/*
    843 		 * We have a buf, now we should make a command.
    844 		 */
    845 
    846 		if (lp->d_secsize == DEV_BSIZE)
    847 			nblks = bp->b_bcount >> DEV_BSHIFT;
    848 		else
    849 			nblks = howmany(bp->b_bcount, lp->d_secsize);
    850 
    851 		/*
    852 		 * Fill out the scsi command.  Use the smallest CDB possible
    853 		 * (6-byte, 10-byte, or 16-byte).
    854 		 */
    855 		if (((bp->b_rawblkno & 0x1fffff) == bp->b_rawblkno) &&
    856 		    ((nblks & 0xff) == nblks) &&
    857 		    !(periph->periph_quirks & PQUIRK_ONLYBIG)) {
    858 			/* 6-byte CDB */
    859 			memset(&cmd_small, 0, sizeof(cmd_small));
    860 			cmd_small.opcode = (bp->b_flags & B_READ) ?
    861 			    SCSI_READ_6_COMMAND : SCSI_WRITE_6_COMMAND;
    862 			_lto3b(bp->b_rawblkno, cmd_small.addr);
    863 			cmd_small.length = nblks & 0xff;
    864 			cmdlen = sizeof(cmd_small);
    865 			cmdp = (struct scsipi_generic *)&cmd_small;
    866 		} else if ((bp->b_rawblkno & 0xffffffff) == bp->b_rawblkno) {
    867 			/* 10-byte CDB */
    868 			memset(&cmd_big, 0, sizeof(cmd_big));
    869 			cmd_big.opcode = (bp->b_flags & B_READ) ?
    870 			    READ_10 : WRITE_10;
    871 			_lto4b(bp->b_rawblkno, cmd_big.addr);
    872 			_lto2b(nblks, cmd_big.length);
    873 			cmdlen = sizeof(cmd_big);
    874 			cmdp = (struct scsipi_generic *)&cmd_big;
    875 		} else {
    876 			/* 16-byte CDB */
    877 			memset(&cmd16, 0, sizeof(cmd16));
    878 			cmd16.opcode = (bp->b_flags & B_READ) ?
    879 			    READ_16 : WRITE_16;
    880 			_lto8b(bp->b_rawblkno, cmd16.addr);
    881 			_lto4b(nblks, cmd16.length);
    882 			cmdlen = sizeof(cmd16);
    883 			cmdp = (struct scsipi_generic *)&cmd16;
    884 		}
    885 
    886 		/* Instrumentation. */
    887 		disk_busy(&sd->sc_dk);
    888 
    889 		/*
    890 		 * Mark the disk dirty so that the cache will be
    891 		 * flushed on close.
    892 		 */
    893 		if ((bp->b_flags & B_READ) == 0)
    894 			sd->flags |= SDF_DIRTY;
    895 
    896 		/*
    897 		 * Figure out what flags to use.
    898 		 */
    899 		flags = XS_CTL_NOSLEEP|XS_CTL_ASYNC|XS_CTL_SIMPLE_TAG;
    900 		if (bp->b_flags & B_READ)
    901 			flags |= XS_CTL_DATA_IN;
    902 		else
    903 			flags |= XS_CTL_DATA_OUT;
    904 
    905 		/*
    906 		 * Call the routine that chats with the adapter.
    907 		 * Note: we cannot sleep as we may be an interrupt
    908 		 */
    909 		xs = scsipi_make_xs(periph, cmdp, cmdlen,
    910 		    (u_char *)bp->b_data, bp->b_bcount,
    911 		    SDRETRIES, SD_IO_TIMEOUT, bp, flags);
    912 		if (__predict_false(xs == NULL)) {
    913 			/*
    914 			 * out of memory. Keep this buffer in the queue, and
    915 			 * retry later.
    916 			 */
    917 			callout_reset(&sd->sc_callout, hz / 2, sdrestart,
    918 			    periph);
    919 			return;
    920 		}
    921 		/*
    922 		 * need to dequeue the buffer before queuing the command,
    923 		 * because cdstart may be called recursively from the
    924 		 * HBA driver
    925 		 */
    926 #ifdef DIAGNOSTIC
    927 		if (BUFQ_GET(sd->buf_queue) != bp)
    928 			panic("sdstart(): dequeued wrong buf");
    929 #else
    930 		BUFQ_GET(sd->buf_queue);
    931 #endif
    932 		error = scsipi_execute_xs(xs);
    933 		/* with a scsipi_xfer preallocated, scsipi_command can't fail */
    934 		KASSERT(error == 0);
    935 	}
    936 }
    937 
    938 static void
    939 sdrestart(void *v)
    940 {
    941 	int s = splbio();
    942 	sdstart((struct scsipi_periph *)v);
    943 	splx(s);
    944 }
    945 
    946 static void
    947 sddone(struct scsipi_xfer *xs, int error)
    948 {
    949 	struct sd_softc *sd = (void *)xs->xs_periph->periph_dev;
    950 	struct buf *bp = xs->bp;
    951 
    952 	if (sd->flags & SDF_FLUSHING) {
    953 		/* Flush completed, no longer dirty. */
    954 		sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY);
    955 	}
    956 
    957 	if (bp) {
    958 		bp->b_error = error;
    959 		bp->b_resid = xs->resid;
    960 		if (error) {
    961 			/* on a read/write error bp->b_resid is zero, so fix */
    962 			bp->b_resid = bp->b_bcount;
    963 		}
    964 
    965 		disk_unbusy(&sd->sc_dk, bp->b_bcount - bp->b_resid,
    966 		    (bp->b_flags & B_READ));
    967 #if NRND > 0
    968 		rnd_add_uint32(&sd->rnd_source, bp->b_rawblkno);
    969 #endif
    970 
    971 		biodone(bp);
    972 	}
    973 }
    974 
    975 static void
    976 sdminphys(struct buf *bp)
    977 {
    978 	struct sd_softc *sd = device_lookup_private(&sd_cd, SDUNIT(bp->b_dev));
    979 	long xmax;
    980 
    981 	/*
    982 	 * If the device is ancient, we want to make sure that
    983 	 * the transfer fits into a 6-byte cdb.
    984 	 *
    985 	 * XXX Note that the SCSI-I spec says that 256-block transfers
    986 	 * are allowed in a 6-byte read/write, and are specified
    987 	 * by settng the "length" to 0.  However, we're conservative
    988 	 * here, allowing only 255-block transfers in case an
    989 	 * ancient device gets confused by length == 0.  A length of 0
    990 	 * in a 10-byte read/write actually means 0 blocks.
    991 	 */
    992 	if ((sd->flags & SDF_ANCIENT) &&
    993 	    ((sd->sc_periph->periph_flags &
    994 	    (PERIPH_REMOVABLE | PERIPH_MEDIA_LOADED)) != PERIPH_REMOVABLE)) {
    995 		xmax = sd->sc_dk.dk_label->d_secsize * 0xff;
    996 
    997 		if (bp->b_bcount > xmax)
    998 			bp->b_bcount = xmax;
    999 	}
   1000 
   1001 	scsipi_adapter_minphys(sd->sc_periph->periph_channel, bp);
   1002 }
   1003 
   1004 static int
   1005 sdread(dev_t dev, struct uio *uio, int ioflag)
   1006 {
   1007 
   1008 	return (physio(sdstrategy, NULL, dev, B_READ, sdminphys, uio));
   1009 }
   1010 
   1011 static int
   1012 sdwrite(dev_t dev, struct uio *uio, int ioflag)
   1013 {
   1014 
   1015 	return (physio(sdstrategy, NULL, dev, B_WRITE, sdminphys, uio));
   1016 }
   1017 
   1018 /*
   1019  * Perform special action on behalf of the user
   1020  * Knows about the internals of this device
   1021  */
   1022 static int
   1023 sdioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
   1024 {
   1025 	struct sd_softc *sd = device_lookup_private(&sd_cd, SDUNIT(dev));
   1026 	struct scsipi_periph *periph = sd->sc_periph;
   1027 	int part = SDPART(dev);
   1028 	int error = 0;
   1029 #ifdef __HAVE_OLD_DISKLABEL
   1030 	struct disklabel *newlabel = NULL;
   1031 #endif
   1032 
   1033 	SC_DEBUG(sd->sc_periph, SCSIPI_DB2, ("sdioctl 0x%lx ", cmd));
   1034 
   1035 	/*
   1036 	 * If the device is not valid, some IOCTLs can still be
   1037 	 * handled on the raw partition. Check this here.
   1038 	 */
   1039 	if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) {
   1040 		switch (cmd) {
   1041 		case DIOCKLABEL:
   1042 		case DIOCWLABEL:
   1043 		case DIOCLOCK:
   1044 		case DIOCEJECT:
   1045 		case ODIOCEJECT:
   1046 		case DIOCGCACHE:
   1047 		case DIOCSCACHE:
   1048 		case SCIOCIDENTIFY:
   1049 		case OSCIOCIDENTIFY:
   1050 		case SCIOCCOMMAND:
   1051 		case SCIOCDEBUG:
   1052 			if (part == RAW_PART)
   1053 				break;
   1054 		/* FALLTHROUGH */
   1055 		default:
   1056 			if ((periph->periph_flags & PERIPH_OPEN) == 0)
   1057 				return (ENODEV);
   1058 			else
   1059 				return (EIO);
   1060 		}
   1061 	}
   1062 
   1063 	switch (cmd) {
   1064 	case DIOCGDINFO:
   1065 		*(struct disklabel *)addr = *(sd->sc_dk.dk_label);
   1066 		return (0);
   1067 
   1068 #ifdef __HAVE_OLD_DISKLABEL
   1069 	case ODIOCGDINFO:
   1070 		newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
   1071 		if (newlabel == NULL)
   1072 			return EIO;
   1073 		memcpy(newlabel, sd->sc_dk.dk_label, sizeof (*newlabel));
   1074 		if (newlabel->d_npartitions <= OLDMAXPARTITIONS)
   1075 			memcpy(addr, newlabel, sizeof (struct olddisklabel));
   1076 		else
   1077 			error = ENOTTY;
   1078 		free(newlabel, M_TEMP);
   1079 		return error;
   1080 #endif
   1081 
   1082 	case DIOCGPART:
   1083 		((struct partinfo *)addr)->disklab = sd->sc_dk.dk_label;
   1084 		((struct partinfo *)addr)->part =
   1085 		    &sd->sc_dk.dk_label->d_partitions[part];
   1086 		return (0);
   1087 
   1088 	case DIOCWDINFO:
   1089 	case DIOCSDINFO:
   1090 #ifdef __HAVE_OLD_DISKLABEL
   1091 	case ODIOCWDINFO:
   1092 	case ODIOCSDINFO:
   1093 #endif
   1094 	{
   1095 		struct disklabel *lp;
   1096 
   1097 		if ((flag & FWRITE) == 0)
   1098 			return (EBADF);
   1099 
   1100 #ifdef __HAVE_OLD_DISKLABEL
   1101  		if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
   1102 			newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
   1103 			if (newlabel == NULL)
   1104 				return EIO;
   1105 			memset(newlabel, 0, sizeof newlabel);
   1106 			memcpy(newlabel, addr, sizeof (struct olddisklabel));
   1107 			lp = newlabel;
   1108 		} else
   1109 #endif
   1110 		lp = (struct disklabel *)addr;
   1111 
   1112 		mutex_enter(&sd->sc_dk.dk_openlock);
   1113 		sd->flags |= SDF_LABELLING;
   1114 
   1115 		error = setdisklabel(sd->sc_dk.dk_label,
   1116 		    lp, /*sd->sc_dk.dk_openmask : */0,
   1117 		    sd->sc_dk.dk_cpulabel);
   1118 		if (error == 0) {
   1119 			if (cmd == DIOCWDINFO
   1120 #ifdef __HAVE_OLD_DISKLABEL
   1121 			    || cmd == ODIOCWDINFO
   1122 #endif
   1123 			   )
   1124 				error = writedisklabel(SDLABELDEV(dev),
   1125 				    sdstrategy, sd->sc_dk.dk_label,
   1126 				    sd->sc_dk.dk_cpulabel);
   1127 		}
   1128 
   1129 		sd->flags &= ~SDF_LABELLING;
   1130 		mutex_exit(&sd->sc_dk.dk_openlock);
   1131 #ifdef __HAVE_OLD_DISKLABEL
   1132 		if (newlabel != NULL)
   1133 			free(newlabel, M_TEMP);
   1134 #endif
   1135 		return (error);
   1136 	}
   1137 
   1138 	case DIOCKLABEL:
   1139 		if (*(int *)addr)
   1140 			periph->periph_flags |= PERIPH_KEEP_LABEL;
   1141 		else
   1142 			periph->periph_flags &= ~PERIPH_KEEP_LABEL;
   1143 		return (0);
   1144 
   1145 	case DIOCWLABEL:
   1146 		if ((flag & FWRITE) == 0)
   1147 			return (EBADF);
   1148 		if (*(int *)addr)
   1149 			sd->flags |= SDF_WLABEL;
   1150 		else
   1151 			sd->flags &= ~SDF_WLABEL;
   1152 		return (0);
   1153 
   1154 	case DIOCLOCK:
   1155 		if (periph->periph_flags & PERIPH_REMOVABLE)
   1156 			return (scsipi_prevent(periph,
   1157 			    (*(int *)addr) ?
   1158 			    SPAMR_PREVENT_DT : SPAMR_ALLOW, 0));
   1159 		else
   1160 			return (ENOTTY);
   1161 
   1162 	case DIOCEJECT:
   1163 		if ((periph->periph_flags & PERIPH_REMOVABLE) == 0)
   1164 			return (ENOTTY);
   1165 		if (*(int *)addr == 0) {
   1166 			/*
   1167 			 * Don't force eject: check that we are the only
   1168 			 * partition open. If so, unlock it.
   1169 			 */
   1170 			if ((sd->sc_dk.dk_openmask & ~(1 << part)) == 0 &&
   1171 			    sd->sc_dk.dk_bopenmask + sd->sc_dk.dk_copenmask ==
   1172 			    sd->sc_dk.dk_openmask) {
   1173 				error = scsipi_prevent(periph, SPAMR_ALLOW,
   1174 				    XS_CTL_IGNORE_NOT_READY);
   1175 				if (error)
   1176 					return (error);
   1177 			} else {
   1178 				return (EBUSY);
   1179 			}
   1180 		}
   1181 		/* FALLTHROUGH */
   1182 	case ODIOCEJECT:
   1183 		return ((periph->periph_flags & PERIPH_REMOVABLE) == 0 ?
   1184 		    ENOTTY : scsipi_start(periph, SSS_STOP|SSS_LOEJ, 0));
   1185 
   1186 	case DIOCGDEFLABEL:
   1187 		sdgetdefaultlabel(sd, (struct disklabel *)addr);
   1188 		return (0);
   1189 
   1190 #ifdef __HAVE_OLD_DISKLABEL
   1191 	case ODIOCGDEFLABEL:
   1192 		newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
   1193 		if (newlabel == NULL)
   1194 			return EIO;
   1195 		sdgetdefaultlabel(sd, newlabel);
   1196 		if (newlabel->d_npartitions <= OLDMAXPARTITIONS)
   1197 			memcpy(addr, newlabel, sizeof (struct olddisklabel));
   1198 		else
   1199 			error = ENOTTY;
   1200 		free(newlabel, M_TEMP);
   1201 		return error;
   1202 #endif
   1203 
   1204 	case DIOCGCACHE:
   1205 		return (sd_getcache(sd, (int *) addr));
   1206 
   1207 	case DIOCSCACHE:
   1208 		if ((flag & FWRITE) == 0)
   1209 			return (EBADF);
   1210 		return (sd_setcache(sd, *(int *) addr));
   1211 
   1212 	case DIOCCACHESYNC:
   1213 		/*
   1214 		 * XXX Do we really need to care about having a writable
   1215 		 * file descriptor here?
   1216 		 */
   1217 		if ((flag & FWRITE) == 0)
   1218 			return (EBADF);
   1219 		if (((sd->flags & SDF_DIRTY) != 0 || *(int *)addr != 0)) {
   1220 			error = sd_flush(sd, 0);
   1221 			if (error)
   1222 				sd->flags &= ~SDF_FLUSHING;
   1223 			else
   1224 				sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY);
   1225 		} else
   1226 			error = 0;
   1227 		return (error);
   1228 
   1229 	case DIOCAWEDGE:
   1230 	    {
   1231 	    	struct dkwedge_info *dkw = (void *) addr;
   1232 
   1233 		if ((flag & FWRITE) == 0)
   1234 			return (EBADF);
   1235 
   1236 		/* If the ioctl happens here, the parent is us. */
   1237 		strlcpy(dkw->dkw_parent, device_xname(&sd->sc_dev), sizeof(dkw->dkw_parent));
   1238 		return (dkwedge_add(dkw));
   1239 	    }
   1240 
   1241 	case DIOCDWEDGE:
   1242 	    {
   1243 	    	struct dkwedge_info *dkw = (void *) addr;
   1244 
   1245 		if ((flag & FWRITE) == 0)
   1246 			return (EBADF);
   1247 
   1248 		/* If the ioctl happens here, the parent is us. */
   1249 		strlcpy(dkw->dkw_parent, device_xname(&sd->sc_dev), sizeof(dkw->dkw_parent));
   1250 		return (dkwedge_del(dkw));
   1251 	    }
   1252 
   1253 	case DIOCLWEDGES:
   1254 	    {
   1255 	    	struct dkwedge_list *dkwl = (void *) addr;
   1256 
   1257 		return (dkwedge_list(&sd->sc_dk, dkwl, l));
   1258 	    }
   1259 
   1260 	default:
   1261 		if (part != RAW_PART)
   1262 			return (ENOTTY);
   1263 		return (scsipi_do_ioctl(periph, dev, cmd, addr, flag, l));
   1264 	}
   1265 
   1266 #ifdef DIAGNOSTIC
   1267 	panic("sdioctl: impossible");
   1268 #endif
   1269 }
   1270 
   1271 static void
   1272 sdgetdefaultlabel(struct sd_softc *sd, struct disklabel *lp)
   1273 {
   1274 
   1275 	memset(lp, 0, sizeof(struct disklabel));
   1276 
   1277 	lp->d_secsize = sd->params.blksize;
   1278 	lp->d_ntracks = sd->params.heads;
   1279 	lp->d_nsectors = sd->params.sectors;
   1280 	lp->d_ncylinders = sd->params.cyls;
   1281 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
   1282 
   1283 	switch (scsipi_periph_bustype(sd->sc_periph)) {
   1284 	case SCSIPI_BUSTYPE_SCSI:
   1285 		lp->d_type = DTYPE_SCSI;
   1286 		break;
   1287 	case SCSIPI_BUSTYPE_ATAPI:
   1288 		lp->d_type = DTYPE_ATAPI;
   1289 		break;
   1290 	}
   1291 	/*
   1292 	 * XXX
   1293 	 * We could probe the mode pages to figure out what kind of disc it is.
   1294 	 * Is this worthwhile?
   1295 	 */
   1296 	strncpy(lp->d_typename, sd->name, 16);
   1297 	strncpy(lp->d_packname, "fictitious", 16);
   1298 	lp->d_secperunit = sd->params.disksize;
   1299 	lp->d_rpm = sd->params.rot_rate;
   1300 	lp->d_interleave = 1;
   1301 	lp->d_flags = sd->sc_periph->periph_flags & PERIPH_REMOVABLE ?
   1302 	    D_REMOVABLE : 0;
   1303 
   1304 	lp->d_partitions[RAW_PART].p_offset = 0;
   1305 	lp->d_partitions[RAW_PART].p_size = lp->d_secperunit;
   1306 	lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
   1307 	lp->d_npartitions = RAW_PART + 1;
   1308 
   1309 	lp->d_magic = DISKMAGIC;
   1310 	lp->d_magic2 = DISKMAGIC;
   1311 	lp->d_checksum = dkcksum(lp);
   1312 }
   1313 
   1314 
   1315 /*
   1316  * Load the label information on the named device
   1317  */
   1318 static int
   1319 sdgetdisklabel(struct sd_softc *sd)
   1320 {
   1321 	struct disklabel *lp = sd->sc_dk.dk_label;
   1322 	const char *errstring;
   1323 
   1324 	memset(sd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel));
   1325 
   1326 	sdgetdefaultlabel(sd, lp);
   1327 
   1328 	if (lp->d_secpercyl == 0) {
   1329 		lp->d_secpercyl = 100;
   1330 		/* as long as it's not 0 - readdisklabel divides by it (?) */
   1331 	}
   1332 
   1333 	/*
   1334 	 * Call the generic disklabel extraction routine
   1335 	 */
   1336 	errstring = readdisklabel(MAKESDDEV(0, device_unit(&sd->sc_dev),
   1337 	    RAW_PART), sdstrategy, lp, sd->sc_dk.dk_cpulabel);
   1338 	if (errstring) {
   1339 		aprint_error_dev(&sd->sc_dev, "%s\n", errstring);
   1340 		return EIO;
   1341 	}
   1342 	return 0;
   1343 }
   1344 
   1345 static void
   1346 sd_shutdown(void *arg)
   1347 {
   1348 	struct sd_softc *sd = arg;
   1349 
   1350 	/*
   1351 	 * If the disk cache needs to be flushed, and the disk supports
   1352 	 * it, flush it.  We're cold at this point, so we poll for
   1353 	 * completion.
   1354 	 */
   1355 	if ((sd->flags & SDF_DIRTY) != 0) {
   1356 		if (sd_flush(sd, XS_CTL_NOSLEEP|XS_CTL_POLL)) {
   1357 			aprint_error_dev(&sd->sc_dev, "cache synchronization failed\n");
   1358 			sd->flags &= ~SDF_FLUSHING;
   1359 		} else
   1360 			sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY);
   1361 	}
   1362 }
   1363 
   1364 static bool
   1365 sd_suspend(device_t dv PMF_FN_ARGS)
   1366 {
   1367 	struct sd_softc *sd = device_private(dv);
   1368 
   1369 	sd_shutdown(sd); /* XXX no need to poll */
   1370 	return true;
   1371 }
   1372 
   1373 /*
   1374  * Check Errors
   1375  */
   1376 static int
   1377 sd_interpret_sense(struct scsipi_xfer *xs)
   1378 {
   1379 	struct scsipi_periph *periph = xs->xs_periph;
   1380 	struct scsi_sense_data *sense = &xs->sense.scsi_sense;
   1381 	struct sd_softc *sd = (void *)periph->periph_dev;
   1382 	int s, error, retval = EJUSTRETURN;
   1383 
   1384 	/*
   1385 	 * If the periph is already recovering, just do the normal
   1386 	 * error processing.
   1387 	 */
   1388 	if (periph->periph_flags & PERIPH_RECOVERING)
   1389 		return (retval);
   1390 
   1391 	/*
   1392 	 * Ignore errors from accessing illegal fields (e.g. trying to
   1393 	 * lock the door of a digicam, which doesn't have a door that
   1394 	 * can be locked) for the SCSI_PREVENT_ALLOW_MEDIUM_REMOVAL command.
   1395 	 */
   1396 	if (xs->cmd->opcode == SCSI_PREVENT_ALLOW_MEDIUM_REMOVAL &&
   1397 	    SSD_SENSE_KEY(sense->flags) == SKEY_ILLEGAL_REQUEST &&
   1398 	    sense->asc == 0x24 &&
   1399 	    sense->ascq == 0x00) { /* Illegal field in CDB */
   1400 		if (!(xs->xs_control & XS_CTL_SILENT)) {
   1401 			scsipi_printaddr(periph);
   1402 			printf("no door lock\n");
   1403 		}
   1404 		xs->xs_control |= XS_CTL_IGNORE_ILLEGAL_REQUEST;
   1405 		return (retval);
   1406 	}
   1407 
   1408 
   1409 
   1410 	/*
   1411 	 * If the device is not open yet, let the generic code handle it.
   1412 	 */
   1413 	if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
   1414 		return (retval);
   1415 
   1416 	/*
   1417 	 * If it isn't a extended or extended/deferred error, let
   1418 	 * the generic code handle it.
   1419 	 */
   1420 	if (SSD_RCODE(sense->response_code) != SSD_RCODE_CURRENT &&
   1421 	    SSD_RCODE(sense->response_code) != SSD_RCODE_DEFERRED)
   1422 		return (retval);
   1423 
   1424 	if (SSD_SENSE_KEY(sense->flags) == SKEY_NOT_READY &&
   1425 	    sense->asc == 0x4) {
   1426 		if (sense->ascq == 0x01)	{
   1427 			/*
   1428 			 * Unit In The Process Of Becoming Ready.
   1429 			 */
   1430 			printf("%s: waiting for pack to spin up...\n",
   1431 			    device_xname(&sd->sc_dev));
   1432 			if (!callout_pending(&periph->periph_callout))
   1433 				scsipi_periph_freeze(periph, 1);
   1434 			callout_reset(&periph->periph_callout,
   1435 			    5 * hz, scsipi_periph_timed_thaw, periph);
   1436 			retval = ERESTART;
   1437 		} else if (sense->ascq == 0x02) {
   1438 			printf("%s: pack is stopped, restarting...\n",
   1439 			    device_xname(&sd->sc_dev));
   1440 			s = splbio();
   1441 			periph->periph_flags |= PERIPH_RECOVERING;
   1442 			splx(s);
   1443 			error = scsipi_start(periph, SSS_START,
   1444 			    XS_CTL_URGENT|XS_CTL_HEAD_TAG|
   1445 			    XS_CTL_THAW_PERIPH|XS_CTL_FREEZE_PERIPH);
   1446 			if (error) {
   1447 				aprint_error_dev(&sd->sc_dev, "unable to restart pack\n");
   1448 				retval = error;
   1449 			} else
   1450 				retval = ERESTART;
   1451 			s = splbio();
   1452 			periph->periph_flags &= ~PERIPH_RECOVERING;
   1453 			splx(s);
   1454 		}
   1455 	}
   1456 	if (SSD_SENSE_KEY(sense->flags) == SKEY_MEDIUM_ERROR &&
   1457 	    sense->asc == 0x31 &&
   1458 	    sense->ascq == 0x00)	{ /* maybe for any asq ? */
   1459 		/* Medium Format Corrupted */
   1460 		retval = EFTYPE;
   1461 	}
   1462 	return (retval);
   1463 }
   1464 
   1465 
   1466 static int
   1467 sdsize(dev_t dev)
   1468 {
   1469 	struct sd_softc *sd;
   1470 	int part, unit, omask;
   1471 	int size;
   1472 
   1473 	unit = SDUNIT(dev);
   1474 	sd = device_lookup_private(&sd_cd, unit);
   1475 	if (sd == NULL)
   1476 		return (-1);
   1477 
   1478 	if (!device_is_active(&sd->sc_dev))
   1479 		return (-1);
   1480 
   1481 	part = SDPART(dev);
   1482 	omask = sd->sc_dk.dk_openmask & (1 << part);
   1483 
   1484 	if (omask == 0 && sdopen(dev, 0, S_IFBLK, NULL) != 0)
   1485 		return (-1);
   1486 	if ((sd->sc_periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
   1487 		size = -1;
   1488 	else if (sd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
   1489 		size = -1;
   1490 	else
   1491 		size = sd->sc_dk.dk_label->d_partitions[part].p_size *
   1492 		    (sd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
   1493 	if (omask == 0 && sdclose(dev, 0, S_IFBLK, NULL) != 0)
   1494 		return (-1);
   1495 	return (size);
   1496 }
   1497 
   1498 /* #define SD_DUMP_NOT_TRUSTED if you just want to watch */
   1499 static struct scsipi_xfer sx;
   1500 static int sddoingadump;
   1501 
   1502 /*
   1503  * dump all of physical memory into the partition specified, starting
   1504  * at offset 'dumplo' into the partition.
   1505  */
   1506 static int
   1507 sddump(dev_t dev, daddr_t blkno, void *va, size_t size)
   1508 {
   1509 	struct sd_softc *sd;	/* disk unit to do the I/O */
   1510 	struct disklabel *lp;	/* disk's disklabel */
   1511 	int	unit, part;
   1512 	int	sectorsize;	/* size of a disk sector */
   1513 	int	nsects;		/* number of sectors in partition */
   1514 	int	sectoff;	/* sector offset of partition */
   1515 	int	totwrt;		/* total number of sectors left to write */
   1516 	int	nwrt;		/* current number of sectors to write */
   1517 	struct scsipi_rw_10 cmd;	/* write command */
   1518 	struct scsipi_xfer *xs;	/* ... convenience */
   1519 	struct scsipi_periph *periph;
   1520 	struct scsipi_channel *chan;
   1521 
   1522 	/* Check if recursive dump; if so, punt. */
   1523 	if (sddoingadump)
   1524 		return (EFAULT);
   1525 
   1526 	/* Mark as active early. */
   1527 	sddoingadump = 1;
   1528 
   1529 	unit = SDUNIT(dev);	/* Decompose unit & partition. */
   1530 	part = SDPART(dev);
   1531 
   1532 	/* Check for acceptable drive number. */
   1533 	sd = device_lookup_private(&sd_cd, unit);
   1534 	if (sd == NULL)
   1535 		return (ENXIO);
   1536 
   1537 	if (!device_is_active(&sd->sc_dev))
   1538 		return (ENODEV);
   1539 
   1540 	periph = sd->sc_periph;
   1541 	chan = periph->periph_channel;
   1542 
   1543 	/* Make sure it was initialized. */
   1544 	if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
   1545 		return (ENXIO);
   1546 
   1547 	/* Convert to disk sectors.  Request must be a multiple of size. */
   1548 	lp = sd->sc_dk.dk_label;
   1549 	sectorsize = lp->d_secsize;
   1550 	if ((size % sectorsize) != 0)
   1551 		return (EFAULT);
   1552 	totwrt = size / sectorsize;
   1553 	blkno = dbtob(blkno) / sectorsize;	/* blkno in DEV_BSIZE units */
   1554 
   1555 	nsects = lp->d_partitions[part].p_size;
   1556 	sectoff = lp->d_partitions[part].p_offset;
   1557 
   1558 	/* Check transfer bounds against partition size. */
   1559 	if ((blkno < 0) || ((blkno + totwrt) > nsects))
   1560 		return (EINVAL);
   1561 
   1562 	/* Offset block number to start of partition. */
   1563 	blkno += sectoff;
   1564 
   1565 	xs = &sx;
   1566 
   1567 	while (totwrt > 0) {
   1568 		nwrt = totwrt;		/* XXX */
   1569 #ifndef	SD_DUMP_NOT_TRUSTED
   1570 		/*
   1571 		 *  Fill out the scsi command
   1572 		 */
   1573 		memset(&cmd, 0, sizeof(cmd));
   1574 		cmd.opcode = WRITE_10;
   1575 		_lto4b(blkno, cmd.addr);
   1576 		_lto2b(nwrt, cmd.length);
   1577 		/*
   1578 		 * Fill out the scsipi_xfer structure
   1579 		 *    Note: we cannot sleep as we may be an interrupt
   1580 		 * don't use scsipi_command() as it may want to wait
   1581 		 * for an xs.
   1582 		 */
   1583 		memset(xs, 0, sizeof(sx));
   1584 		xs->xs_control |= XS_CTL_NOSLEEP | XS_CTL_POLL |
   1585 		    XS_CTL_DATA_OUT;
   1586 		xs->xs_status = 0;
   1587 		xs->xs_periph = periph;
   1588 		xs->xs_retries = SDRETRIES;
   1589 		xs->timeout = 10000;	/* 10000 millisecs for a disk ! */
   1590 		xs->cmd = (struct scsipi_generic *)&cmd;
   1591 		xs->cmdlen = sizeof(cmd);
   1592 		xs->resid = nwrt * sectorsize;
   1593 		xs->error = XS_NOERROR;
   1594 		xs->bp = 0;
   1595 		xs->data = va;
   1596 		xs->datalen = nwrt * sectorsize;
   1597 		callout_init(&xs->xs_callout, 0);
   1598 
   1599 		/*
   1600 		 * Pass all this info to the scsi driver.
   1601 		 */
   1602 		scsipi_adapter_request(chan, ADAPTER_REQ_RUN_XFER, xs);
   1603 		if ((xs->xs_status & XS_STS_DONE) == 0 ||
   1604 		    xs->error != XS_NOERROR)
   1605 			return (EIO);
   1606 #else	/* SD_DUMP_NOT_TRUSTED */
   1607 		/* Let's just talk about this first... */
   1608 		printf("sd%d: dump addr 0x%x, blk %d\n", unit, va, blkno);
   1609 		delay(500 * 1000);	/* half a second */
   1610 #endif	/* SD_DUMP_NOT_TRUSTED */
   1611 
   1612 		/* update block count */
   1613 		totwrt -= nwrt;
   1614 		blkno += nwrt;
   1615 		va = (char *)va + sectorsize * nwrt;
   1616 	}
   1617 	sddoingadump = 0;
   1618 	return (0);
   1619 }
   1620 
   1621 static int
   1622 sd_mode_sense(struct sd_softc *sd, u_int8_t byte2, void *sense, size_t size,
   1623     int page, int flags, int *big)
   1624 {
   1625 
   1626 	if ((sd->sc_periph->periph_quirks & PQUIRK_ONLYBIG) &&
   1627 	    !(sd->sc_periph->periph_quirks & PQUIRK_NOBIGMODESENSE)) {
   1628 		*big = 1;
   1629 		return scsipi_mode_sense_big(sd->sc_periph, byte2, page, sense,
   1630 		    size + sizeof(struct scsi_mode_parameter_header_10),
   1631 		    flags | XS_CTL_DATA_ONSTACK, SDRETRIES, 6000);
   1632 	} else {
   1633 		*big = 0;
   1634 		return scsipi_mode_sense(sd->sc_periph, byte2, page, sense,
   1635 		    size + sizeof(struct scsi_mode_parameter_header_6),
   1636 		    flags | XS_CTL_DATA_ONSTACK, SDRETRIES, 6000);
   1637 	}
   1638 }
   1639 
   1640 static int
   1641 sd_mode_select(struct sd_softc *sd, u_int8_t byte2, void *sense, size_t size,
   1642     int flags, int big)
   1643 {
   1644 
   1645 	if (big) {
   1646 		struct scsi_mode_parameter_header_10 *header = sense;
   1647 
   1648 		_lto2b(0, header->data_length);
   1649 		return scsipi_mode_select_big(sd->sc_periph, byte2, sense,
   1650 		    size + sizeof(struct scsi_mode_parameter_header_10),
   1651 		    flags | XS_CTL_DATA_ONSTACK, SDRETRIES, 6000);
   1652 	} else {
   1653 		struct scsi_mode_parameter_header_6 *header = sense;
   1654 
   1655 		header->data_length = 0;
   1656 		return scsipi_mode_select(sd->sc_periph, byte2, sense,
   1657 		    size + sizeof(struct scsi_mode_parameter_header_6),
   1658 		    flags | XS_CTL_DATA_ONSTACK, SDRETRIES, 6000);
   1659 	}
   1660 }
   1661 
   1662 /*
   1663  * sd_validate_blksize:
   1664  *
   1665  *	Validate the block size.  Print error if periph is specified,
   1666  */
   1667 static int
   1668 sd_validate_blksize(struct scsipi_periph *periph, int len)
   1669 {
   1670 
   1671 	switch (len) {
   1672 	case 256:
   1673 	case 512:
   1674 	case 1024:
   1675 	case 2048:
   1676 	case 4096:
   1677 		return 1;
   1678 	}
   1679 
   1680 	if (periph) {
   1681 		scsipi_printaddr(periph);
   1682 		printf("%s sector size: 0x%x.  Defaulting to %d bytes.\n",
   1683 		    (len ^ (1 << (ffs(len) - 1))) ?
   1684 		    "preposterous" : "unsupported",
   1685 		    len, SD_DEFAULT_BLKSIZE);
   1686 	}
   1687 
   1688 	return 0;
   1689 }
   1690 
   1691 /*
   1692  * sd_read_capacity:
   1693  *
   1694  *	Find out from the device what its capacity is.
   1695  */
   1696 static u_int64_t
   1697 sd_read_capacity(struct scsipi_periph *periph, int *blksize, int flags)
   1698 {
   1699 	union {
   1700 		struct scsipi_read_capacity_10 cmd;
   1701 		struct scsipi_read_capacity_16 cmd16;
   1702 	} cmd;
   1703 	union {
   1704 		struct scsipi_read_capacity_10_data data;
   1705 		struct scsipi_read_capacity_16_data data16;
   1706 	} *datap;
   1707 	uint64_t rv;
   1708 
   1709 	memset(&cmd, 0, sizeof(cmd));
   1710 	cmd.cmd.opcode = READ_CAPACITY_10;
   1711 
   1712 	/*
   1713 	 * Don't allocate data buffer on stack;
   1714 	 * The lower driver layer might use the same stack and
   1715 	 * if it uses region which is in the same cacheline,
   1716 	 * cache flush ops against the data buffer won't work properly.
   1717 	 */
   1718 	datap = malloc(sizeof(*datap), M_TEMP, M_WAITOK);
   1719 	if (datap == NULL)
   1720 		return 0;
   1721 
   1722 	/*
   1723 	 * If the command works, interpret the result as a 4 byte
   1724 	 * number of blocks
   1725 	 */
   1726 	rv = 0;
   1727 	memset(datap, 0, sizeof(datap->data));
   1728 	if (scsipi_command(periph, (void *)&cmd.cmd, sizeof(cmd.cmd),
   1729 	    (void *)datap, sizeof(datap->data), SCSIPIRETRIES, 20000, NULL,
   1730 	    flags | XS_CTL_DATA_IN | XS_CTL_SILENT) != 0)
   1731 		goto out;
   1732 
   1733 	if (_4btol(datap->data.addr) != 0xffffffff) {
   1734 		*blksize = _4btol(datap->data.length);
   1735 		rv = _4btol(datap->data.addr) + 1;
   1736 		goto out;
   1737 	}
   1738 
   1739 	/*
   1740 	 * Device is larger than can be reflected by READ CAPACITY (10).
   1741 	 * Try READ CAPACITY (16).
   1742 	 */
   1743 
   1744 	memset(&cmd, 0, sizeof(cmd));
   1745 	cmd.cmd16.opcode = READ_CAPACITY_16;
   1746 	cmd.cmd16.byte2 = SRC16_SERVICE_ACTION;
   1747 	_lto4b(sizeof(datap->data16), cmd.cmd16.len);
   1748 
   1749 	memset(datap, 0, sizeof(datap->data16));
   1750 	if (scsipi_command(periph, (void *)&cmd.cmd16, sizeof(cmd.cmd16),
   1751 	    (void *)datap, sizeof(datap->data16), SCSIPIRETRIES, 20000, NULL,
   1752 	    flags | XS_CTL_DATA_IN | XS_CTL_SILENT) != 0)
   1753 		goto out;
   1754 
   1755 	*blksize = _4btol(datap->data16.length);
   1756 	rv = _8btol(datap->data16.addr) + 1;
   1757 
   1758  out:
   1759 	free(datap, M_TEMP);
   1760 	return rv;
   1761 }
   1762 
   1763 static int
   1764 sd_get_simplifiedparms(struct sd_softc *sd, struct disk_parms *dp, int flags)
   1765 {
   1766 	struct {
   1767 		struct scsi_mode_parameter_header_6 header;
   1768 		/* no block descriptor */
   1769 		u_int8_t pg_code; /* page code (should be 6) */
   1770 		u_int8_t pg_length; /* page length (should be 11) */
   1771 		u_int8_t wcd; /* bit0: cache disable */
   1772 		u_int8_t lbs[2]; /* logical block size */
   1773 		u_int8_t size[5]; /* number of log. blocks */
   1774 		u_int8_t pp; /* power/performance */
   1775 		u_int8_t flags;
   1776 		u_int8_t resvd;
   1777 	} scsipi_sense;
   1778 	u_int64_t blocks;
   1779 	int error, blksize;
   1780 
   1781 	/*
   1782 	 * sd_read_capacity (ie "read capacity") and mode sense page 6
   1783 	 * give the same information. Do both for now, and check
   1784 	 * for consistency.
   1785 	 * XXX probably differs for removable media
   1786 	 */
   1787 	dp->blksize = SD_DEFAULT_BLKSIZE;
   1788 	if ((blocks = sd_read_capacity(sd->sc_periph, &blksize, flags)) == 0)
   1789 		return (SDGP_RESULT_OFFLINE);		/* XXX? */
   1790 
   1791 	error = scsipi_mode_sense(sd->sc_periph, SMS_DBD, 6,
   1792 	    &scsipi_sense.header, sizeof(scsipi_sense),
   1793 	    flags | XS_CTL_DATA_ONSTACK, SDRETRIES, 6000);
   1794 
   1795 	if (error != 0)
   1796 		return (SDGP_RESULT_OFFLINE);		/* XXX? */
   1797 
   1798 	dp->blksize = blksize;
   1799 	if (!sd_validate_blksize(NULL, dp->blksize))
   1800 		dp->blksize = _2btol(scsipi_sense.lbs);
   1801 	if (!sd_validate_blksize(sd->sc_periph, dp->blksize))
   1802 		dp->blksize = SD_DEFAULT_BLKSIZE;
   1803 
   1804 	/*
   1805 	 * Create a pseudo-geometry.
   1806 	 */
   1807 	dp->heads = 64;
   1808 	dp->sectors = 32;
   1809 	dp->cyls = blocks / (dp->heads * dp->sectors);
   1810 	dp->disksize = _5btol(scsipi_sense.size);
   1811 	if (dp->disksize <= UINT32_MAX && dp->disksize != blocks) {
   1812 		printf("RBC size: mode sense=%llu, get cap=%llu\n",
   1813 		       (unsigned long long)dp->disksize,
   1814 		       (unsigned long long)blocks);
   1815 		dp->disksize = blocks;
   1816 	}
   1817 	dp->disksize512 = (dp->disksize * dp->blksize) / DEV_BSIZE;
   1818 
   1819 	return (SDGP_RESULT_OK);
   1820 }
   1821 
   1822 /*
   1823  * Get the scsi driver to send a full inquiry to the * device and use the
   1824  * results to fill out the disk parameter structure.
   1825  */
   1826 static int
   1827 sd_get_capacity(struct sd_softc *sd, struct disk_parms *dp, int flags)
   1828 {
   1829 	u_int64_t blocks;
   1830 	int error, blksize;
   1831 #if 0
   1832 	int i;
   1833 	u_int8_t *p;
   1834 #endif
   1835 
   1836 	dp->disksize = blocks = sd_read_capacity(sd->sc_periph, &blksize,
   1837 	    flags);
   1838 	if (blocks == 0) {
   1839 		struct scsipi_read_format_capacities cmd;
   1840 		struct {
   1841 			struct scsipi_capacity_list_header header;
   1842 			struct scsipi_capacity_descriptor desc;
   1843 		} __packed data;
   1844 
   1845 		memset(&cmd, 0, sizeof(cmd));
   1846 		memset(&data, 0, sizeof(data));
   1847 		cmd.opcode = READ_FORMAT_CAPACITIES;
   1848 		_lto2b(sizeof(data), cmd.length);
   1849 
   1850 		error = scsipi_command(sd->sc_periph,
   1851 		    (void *)&cmd, sizeof(cmd), (void *)&data, sizeof(data),
   1852 		    SDRETRIES, 20000, NULL,
   1853 		    flags | XS_CTL_DATA_IN | XS_CTL_DATA_ONSTACK);
   1854 		if (error == EFTYPE) {
   1855 			/* Medium Format Corrupted, handle as not formatted */
   1856 			return (SDGP_RESULT_UNFORMATTED);
   1857 		}
   1858 		if (error || data.header.length == 0)
   1859 			return (SDGP_RESULT_OFFLINE);
   1860 
   1861 #if 0
   1862 printf("rfc: length=%d\n", data.header.length);
   1863 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");
   1864 #endif
   1865 		switch (data.desc.byte5 & SCSIPI_CAP_DESC_CODE_MASK) {
   1866 		case SCSIPI_CAP_DESC_CODE_RESERVED:
   1867 		case SCSIPI_CAP_DESC_CODE_FORMATTED:
   1868 			break;
   1869 
   1870 		case SCSIPI_CAP_DESC_CODE_UNFORMATTED:
   1871 			return (SDGP_RESULT_UNFORMATTED);
   1872 
   1873 		case SCSIPI_CAP_DESC_CODE_NONE:
   1874 			return (SDGP_RESULT_OFFLINE);
   1875 		}
   1876 
   1877 		dp->disksize = blocks = _4btol(data.desc.nblks);
   1878 		if (blocks == 0)
   1879 			return (SDGP_RESULT_OFFLINE);		/* XXX? */
   1880 
   1881 		blksize = _3btol(data.desc.blklen);
   1882 
   1883 	} else if (!sd_validate_blksize(NULL, blksize)) {
   1884 		struct sd_mode_sense_data scsipi_sense;
   1885 		int big, bsize;
   1886 		struct scsi_general_block_descriptor *bdesc;
   1887 
   1888 		memset(&scsipi_sense, 0, sizeof(scsipi_sense));
   1889 		error = sd_mode_sense(sd, 0, &scsipi_sense,
   1890 		    sizeof(scsipi_sense.blk_desc), 0, flags | XS_CTL_SILENT, &big);
   1891 		if (!error) {
   1892 			if (big) {
   1893 				bdesc = (void *)(&scsipi_sense.header.big + 1);
   1894 				bsize = _2btol(scsipi_sense.header.big.blk_desc_len);
   1895 			} else {
   1896 				bdesc = (void *)(&scsipi_sense.header.small + 1);
   1897 				bsize = scsipi_sense.header.small.blk_desc_len;
   1898 			}
   1899 
   1900 #if 0
   1901 printf("page 0 sense:"); for (i = sizeof(scsipi_sense), p = (void *)&scsipi_sense; i; i--, p++) printf(" %02x", *p); printf("\n");
   1902 printf("page 0 bsize=%d\n", bsize);
   1903 printf("page 0 ok\n");
   1904 #endif
   1905 
   1906 			if (bsize >= 8) {
   1907 				blksize = _3btol(bdesc->blklen);
   1908 			}
   1909 		}
   1910 	}
   1911 
   1912 	if (!sd_validate_blksize(sd->sc_periph, blksize))
   1913 		blksize = SD_DEFAULT_BLKSIZE;
   1914 
   1915 	dp->blksize = blksize;
   1916 	dp->disksize512 = (blocks * dp->blksize) / DEV_BSIZE;
   1917 	return (0);
   1918 }
   1919 
   1920 static int
   1921 sd_get_parms_page4(struct sd_softc *sd, struct disk_parms *dp, int flags)
   1922 {
   1923 	struct sd_mode_sense_data scsipi_sense;
   1924 	int error;
   1925 	int big, byte2;
   1926 	size_t poffset;
   1927 	union scsi_disk_pages *pages;
   1928 
   1929 	byte2 = SMS_DBD;
   1930 again:
   1931 	memset(&scsipi_sense, 0, sizeof(scsipi_sense));
   1932 	error = sd_mode_sense(sd, byte2, &scsipi_sense,
   1933 	    (byte2 ? 0 : sizeof(scsipi_sense.blk_desc)) +
   1934 	    sizeof(scsipi_sense.pages.rigid_geometry), 4,
   1935 	    flags | XS_CTL_SILENT, &big);
   1936 	if (error) {
   1937 		if (byte2 == SMS_DBD) {
   1938 			/* No result; try once more with DBD off */
   1939 			byte2 = 0;
   1940 			goto again;
   1941 		}
   1942 		return (error);
   1943 	}
   1944 
   1945 	if (big) {
   1946 		poffset = sizeof scsipi_sense.header.big;
   1947 		poffset += _2btol(scsipi_sense.header.big.blk_desc_len);
   1948 	} else {
   1949 		poffset = sizeof scsipi_sense.header.small;
   1950 		poffset += scsipi_sense.header.small.blk_desc_len;
   1951 	}
   1952 
   1953 	if (poffset > sizeof(scsipi_sense) - sizeof(pages->rigid_geometry))
   1954 		return ERESTART;
   1955 
   1956 	pages = (void *)((u_long)&scsipi_sense + poffset);
   1957 #if 0
   1958 	{
   1959 		size_t i;
   1960 		u_int8_t *p;
   1961 
   1962 		printf("page 4 sense:");
   1963 		for (i = sizeof(scsipi_sense), p = (void *)&scsipi_sense; i;
   1964 		    i--, p++)
   1965 			printf(" %02x", *p);
   1966 		printf("\n");
   1967 		printf("page 4 pg_code=%d sense=%p/%p\n",
   1968 		    pages->rigid_geometry.pg_code, &scsipi_sense, pages);
   1969 	}
   1970 #endif
   1971 
   1972 	if ((pages->rigid_geometry.pg_code & PGCODE_MASK) != 4)
   1973 		return (ERESTART);
   1974 
   1975 	SC_DEBUG(sd->sc_periph, SCSIPI_DB3,
   1976 	    ("%d cyls, %d heads, %d precomp, %d red_write, %d land_zone\n",
   1977 	    _3btol(pages->rigid_geometry.ncyl),
   1978 	    pages->rigid_geometry.nheads,
   1979 	    _2btol(pages->rigid_geometry.st_cyl_wp),
   1980 	    _2btol(pages->rigid_geometry.st_cyl_rwc),
   1981 	    _2btol(pages->rigid_geometry.land_zone)));
   1982 
   1983 	/*
   1984 	 * KLUDGE!! (for zone recorded disks)
   1985 	 * give a number of sectors so that sec * trks * cyls
   1986 	 * is <= disk_size
   1987 	 * can lead to wasted space! THINK ABOUT THIS !
   1988 	 */
   1989 	dp->heads = pages->rigid_geometry.nheads;
   1990 	dp->cyls = _3btol(pages->rigid_geometry.ncyl);
   1991 	if (dp->heads == 0 || dp->cyls == 0)
   1992 		return (ERESTART);
   1993 	dp->sectors = dp->disksize / (dp->heads * dp->cyls);	/* XXX */
   1994 
   1995 	dp->rot_rate = _2btol(pages->rigid_geometry.rpm);
   1996 	if (dp->rot_rate == 0)
   1997 		dp->rot_rate = 3600;
   1998 
   1999 #if 0
   2000 printf("page 4 ok\n");
   2001 #endif
   2002 	return (0);
   2003 }
   2004 
   2005 static int
   2006 sd_get_parms_page5(struct sd_softc *sd, struct disk_parms *dp, int flags)
   2007 {
   2008 	struct sd_mode_sense_data scsipi_sense;
   2009 	int error;
   2010 	int big, byte2;
   2011 	size_t poffset;
   2012 	union scsi_disk_pages *pages;
   2013 
   2014 	byte2 = SMS_DBD;
   2015 again:
   2016 	memset(&scsipi_sense, 0, sizeof(scsipi_sense));
   2017 	error = sd_mode_sense(sd, 0, &scsipi_sense,
   2018 	    (byte2 ? 0 : sizeof(scsipi_sense.blk_desc)) +
   2019 	    sizeof(scsipi_sense.pages.flex_geometry), 5,
   2020 	    flags | XS_CTL_SILENT, &big);
   2021 	if (error) {
   2022 		if (byte2 == SMS_DBD) {
   2023 			/* No result; try once more with DBD off */
   2024 			byte2 = 0;
   2025 			goto again;
   2026 		}
   2027 		return (error);
   2028 	}
   2029 
   2030 	if (big) {
   2031 		poffset = sizeof scsipi_sense.header.big;
   2032 		poffset += _2btol(scsipi_sense.header.big.blk_desc_len);
   2033 	} else {
   2034 		poffset = sizeof scsipi_sense.header.small;
   2035 		poffset += scsipi_sense.header.small.blk_desc_len;
   2036 	}
   2037 
   2038 	if (poffset > sizeof(scsipi_sense) - sizeof(pages->flex_geometry))
   2039 		return ERESTART;
   2040 
   2041 	pages = (void *)((u_long)&scsipi_sense + poffset);
   2042 #if 0
   2043 	{
   2044 		size_t i;
   2045 		u_int8_t *p;
   2046 
   2047 		printf("page 5 sense:");
   2048 		for (i = sizeof(scsipi_sense), p = (void *)&scsipi_sense; i;
   2049 		    i--, p++)
   2050 			printf(" %02x", *p);
   2051 		printf("\n");
   2052 		printf("page 5 pg_code=%d sense=%p/%p\n",
   2053 		    pages->flex_geometry.pg_code, &scsipi_sense, pages);
   2054 	}
   2055 #endif
   2056 
   2057 	if ((pages->flex_geometry.pg_code & PGCODE_MASK) != 5)
   2058 		return (ERESTART);
   2059 
   2060 	SC_DEBUG(sd->sc_periph, SCSIPI_DB3,
   2061 	    ("%d cyls, %d heads, %d sec, %d bytes/sec\n",
   2062 	    _3btol(pages->flex_geometry.ncyl),
   2063 	    pages->flex_geometry.nheads,
   2064 	    pages->flex_geometry.ph_sec_tr,
   2065 	    _2btol(pages->flex_geometry.bytes_s)));
   2066 
   2067 	dp->heads = pages->flex_geometry.nheads;
   2068 	dp->cyls = _2btol(pages->flex_geometry.ncyl);
   2069 	dp->sectors = pages->flex_geometry.ph_sec_tr;
   2070 	if (dp->heads == 0 || dp->cyls == 0 || dp->sectors == 0)
   2071 		return (ERESTART);
   2072 
   2073 	dp->rot_rate = _2btol(pages->rigid_geometry.rpm);
   2074 	if (dp->rot_rate == 0)
   2075 		dp->rot_rate = 3600;
   2076 
   2077 #if 0
   2078 printf("page 5 ok\n");
   2079 #endif
   2080 	return (0);
   2081 }
   2082 
   2083 static int
   2084 sd_get_parms(struct sd_softc *sd, struct disk_parms *dp, int flags)
   2085 {
   2086 	int error;
   2087 
   2088 	/*
   2089 	 * If offline, the SDEV_MEDIA_LOADED flag will be
   2090 	 * cleared by the caller if necessary.
   2091 	 */
   2092 	if (sd->type == T_SIMPLE_DIRECT) {
   2093 		error = sd_get_simplifiedparms(sd, dp, flags);
   2094 		if (!error)
   2095 			disk_blocksize(&sd->sc_dk, dp->blksize);
   2096 		return (error);
   2097 	}
   2098 
   2099 	error = sd_get_capacity(sd, dp, flags);
   2100 	if (error)
   2101 		return (error);
   2102 
   2103 	disk_blocksize(&sd->sc_dk, dp->blksize);
   2104 
   2105 	if (sd->type == T_OPTICAL)
   2106 		goto page0;
   2107 
   2108 	if (sd->sc_periph->periph_flags & PERIPH_REMOVABLE) {
   2109 		if (!sd_get_parms_page5(sd, dp, flags) ||
   2110 		    !sd_get_parms_page4(sd, dp, flags))
   2111 			return (SDGP_RESULT_OK);
   2112 	} else {
   2113 		if (!sd_get_parms_page4(sd, dp, flags) ||
   2114 		    !sd_get_parms_page5(sd, dp, flags))
   2115 			return (SDGP_RESULT_OK);
   2116 	}
   2117 
   2118 page0:
   2119 	printf("%s: fabricating a geometry\n", device_xname(&sd->sc_dev));
   2120 	/* Try calling driver's method for figuring out geometry. */
   2121 	if (!sd->sc_periph->periph_channel->chan_adapter->adapt_getgeom ||
   2122 	    !(*sd->sc_periph->periph_channel->chan_adapter->adapt_getgeom)
   2123 		(sd->sc_periph, dp, dp->disksize)) {
   2124 		/*
   2125 		 * Use adaptec standard fictitious geometry
   2126 		 * this depends on which controller (e.g. 1542C is
   2127 		 * different. but we have to put SOMETHING here..)
   2128 		 */
   2129 		dp->heads = 64;
   2130 		dp->sectors = 32;
   2131 		dp->cyls = dp->disksize / (64 * 32);
   2132 	}
   2133 	dp->rot_rate = 3600;
   2134 	return (SDGP_RESULT_OK);
   2135 }
   2136 
   2137 static int
   2138 sd_flush(struct sd_softc *sd, int flags)
   2139 {
   2140 	struct scsipi_periph *periph = sd->sc_periph;
   2141 	struct scsi_synchronize_cache_10 cmd;
   2142 
   2143 	/*
   2144 	 * If the device is SCSI-2, issue a SYNCHRONIZE CACHE.
   2145 	 * We issue with address 0 length 0, which should be
   2146 	 * interpreted by the device as "all remaining blocks
   2147 	 * starting at address 0".  We ignore ILLEGAL REQUEST
   2148 	 * in the event that the command is not supported by
   2149 	 * the device, and poll for completion so that we know
   2150 	 * that the cache has actually been flushed.
   2151 	 *
   2152 	 * Unless, that is, the device can't handle the SYNCHRONIZE CACHE
   2153 	 * command, as indicated by our quirks flags.
   2154 	 *
   2155 	 * XXX What about older devices?
   2156 	 */
   2157 	if (periph->periph_version < 2 ||
   2158 	    (periph->periph_quirks & PQUIRK_NOSYNCCACHE))
   2159 		return (0);
   2160 
   2161 	sd->flags |= SDF_FLUSHING;
   2162 	memset(&cmd, 0, sizeof(cmd));
   2163 	cmd.opcode = SCSI_SYNCHRONIZE_CACHE_10;
   2164 
   2165 	return (scsipi_command(periph, (void *)&cmd, sizeof(cmd), 0, 0,
   2166 	    SDRETRIES, 100000, NULL, flags | XS_CTL_IGNORE_ILLEGAL_REQUEST));
   2167 }
   2168 
   2169 static int
   2170 sd_getcache(struct sd_softc *sd, int *bitsp)
   2171 {
   2172 	struct scsipi_periph *periph = sd->sc_periph;
   2173 	struct sd_mode_sense_data scsipi_sense;
   2174 	int error, bits = 0;
   2175 	int big;
   2176 	union scsi_disk_pages *pages;
   2177 
   2178 	if (periph->periph_version < 2)
   2179 		return (EOPNOTSUPP);
   2180 
   2181 	memset(&scsipi_sense, 0, sizeof(scsipi_sense));
   2182 	error = sd_mode_sense(sd, SMS_DBD, &scsipi_sense,
   2183 	    sizeof(scsipi_sense.pages.caching_params), 8, 0, &big);
   2184 	if (error)
   2185 		return (error);
   2186 
   2187 	if (big)
   2188 		pages = (void *)(&scsipi_sense.header.big + 1);
   2189 	else
   2190 		pages = (void *)(&scsipi_sense.header.small + 1);
   2191 
   2192 	if ((pages->caching_params.flags & CACHING_RCD) == 0)
   2193 		bits |= DKCACHE_READ;
   2194 	if (pages->caching_params.flags & CACHING_WCE)
   2195 		bits |= DKCACHE_WRITE;
   2196 	if (pages->caching_params.pg_code & PGCODE_PS)
   2197 		bits |= DKCACHE_SAVE;
   2198 
   2199 	memset(&scsipi_sense, 0, sizeof(scsipi_sense));
   2200 	error = sd_mode_sense(sd, SMS_DBD, &scsipi_sense,
   2201 	    sizeof(scsipi_sense.pages.caching_params),
   2202 	    SMS_PCTRL_CHANGEABLE|8, 0, &big);
   2203 	if (error == 0) {
   2204 		if (big)
   2205 			pages = (void *)(&scsipi_sense.header.big + 1);
   2206 		else
   2207 			pages = (void *)(&scsipi_sense.header.small + 1);
   2208 
   2209 		if (pages->caching_params.flags & CACHING_RCD)
   2210 			bits |= DKCACHE_RCHANGE;
   2211 		if (pages->caching_params.flags & CACHING_WCE)
   2212 			bits |= DKCACHE_WCHANGE;
   2213 	}
   2214 
   2215 	*bitsp = bits;
   2216 
   2217 	return (0);
   2218 }
   2219 
   2220 static int
   2221 sd_setcache(struct sd_softc *sd, int bits)
   2222 {
   2223 	struct scsipi_periph *periph = sd->sc_periph;
   2224 	struct sd_mode_sense_data scsipi_sense;
   2225 	int error;
   2226 	uint8_t oflags, byte2 = 0;
   2227 	int big;
   2228 	union scsi_disk_pages *pages;
   2229 
   2230 	if (periph->periph_version < 2)
   2231 		return (EOPNOTSUPP);
   2232 
   2233 	memset(&scsipi_sense, 0, sizeof(scsipi_sense));
   2234 	error = sd_mode_sense(sd, SMS_DBD, &scsipi_sense,
   2235 	    sizeof(scsipi_sense.pages.caching_params), 8, 0, &big);
   2236 	if (error)
   2237 		return (error);
   2238 
   2239 	if (big)
   2240 		pages = (void *)(&scsipi_sense.header.big + 1);
   2241 	else
   2242 		pages = (void *)(&scsipi_sense.header.small + 1);
   2243 
   2244 	oflags = pages->caching_params.flags;
   2245 
   2246 	if (bits & DKCACHE_READ)
   2247 		pages->caching_params.flags &= ~CACHING_RCD;
   2248 	else
   2249 		pages->caching_params.flags |= CACHING_RCD;
   2250 
   2251 	if (bits & DKCACHE_WRITE)
   2252 		pages->caching_params.flags |= CACHING_WCE;
   2253 	else
   2254 		pages->caching_params.flags &= ~CACHING_WCE;
   2255 
   2256 	if (oflags == pages->caching_params.flags)
   2257 		return (0);
   2258 
   2259 	pages->caching_params.pg_code &= PGCODE_MASK;
   2260 
   2261 	if (bits & DKCACHE_SAVE)
   2262 		byte2 |= SMS_SP;
   2263 
   2264 	return (sd_mode_select(sd, byte2|SMS_PF, &scsipi_sense,
   2265 	    sizeof(struct scsi_mode_page_header) +
   2266 	    pages->caching_params.pg_length, 0, big));
   2267 }
   2268 
   2269 static void
   2270 sd_set_properties(struct sd_softc *sd)
   2271 {
   2272 	prop_dictionary_t disk_info, odisk_info, geom;
   2273 
   2274 	disk_info = prop_dictionary_create();
   2275 
   2276 	geom = prop_dictionary_create();
   2277 
   2278 	prop_dictionary_set_uint64(geom, "sectors-per-unit",
   2279 	    sd->params.disksize);
   2280 
   2281 	prop_dictionary_set_uint32(geom, "sector-size",
   2282 	    sd->params.blksize);
   2283 
   2284 	prop_dictionary_set_uint16(geom, "sectors-per-track",
   2285 	    sd->params.sectors);
   2286 
   2287 	prop_dictionary_set_uint16(geom, "tracks-per-cylinder",
   2288 	    sd->params.heads);
   2289 
   2290 	prop_dictionary_set_uint64(geom, "cylinders-per-unit",
   2291 	    sd->params.cyls);
   2292 
   2293 	prop_dictionary_set(disk_info, "geometry", geom);
   2294 	prop_object_release(geom);
   2295 
   2296 	prop_dictionary_set(device_properties(&sd->sc_dev),
   2297 	    "disk-info", disk_info);
   2298 
   2299 	/*
   2300 	 * Don't release disk_info here; we keep a reference to it.
   2301 	 * disk_detach() will release it when we go away.
   2302 	 */
   2303 
   2304 	odisk_info = sd->sc_dk.dk_info;
   2305 	sd->sc_dk.dk_info = disk_info;
   2306 	if (odisk_info)
   2307 		prop_object_release(odisk_info);
   2308 }
   2309