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