Home | History | Annotate | Line # | Download | only in scsipi
sd.c revision 1.343
      1 /*	$NetBSD: sd.c,v 1.343 2025/03/05 00:41:00 jakllsch 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)
     47  * Sept 1992
     48  */
     49 
     50 #include <sys/cdefs.h>
     51 __KERNEL_RCSID(0, "$NetBSD: sd.c,v 1.343 2025/03/05 00:41:00 jakllsch Exp $");
     52 
     53 #ifdef _KERNEL_OPT
     54 #include "opt_scsi.h"
     55 #endif
     56 
     57 #include <sys/param.h>
     58 #include <sys/systm.h>
     59 #include <sys/kernel.h>
     60 #include <sys/file.h>
     61 #include <sys/stat.h>
     62 #include <sys/ioctl.h>
     63 #include <sys/scsiio.h>
     64 #include <sys/buf.h>
     65 #include <sys/bufq.h>
     66 #include <sys/uio.h>
     67 #include <sys/malloc.h>
     68 #include <sys/errno.h>
     69 #include <sys/device.h>
     70 #include <sys/disklabel.h>
     71 #include <sys/disk.h>
     72 #include <sys/proc.h>
     73 #include <sys/conf.h>
     74 #include <sys/vnode.h>
     75 
     76 #include <dev/scsipi/scsi_spc.h>
     77 #include <dev/scsipi/scsipi_all.h>
     78 #include <dev/scsipi/scsi_all.h>
     79 #include <dev/scsipi/scsipi_disk.h>
     80 #include <dev/scsipi/scsi_disk.h>
     81 #include <dev/scsipi/scsiconf.h>
     82 #include <dev/scsipi/scsipi_base.h>
     83 #include <dev/scsipi/sdvar.h>
     84 
     85 #include <prop/proplib.h>
     86 
     87 #define	SDUNIT(dev)			DISKUNIT(dev)
     88 #define	SDPART(dev)			DISKPART(dev)
     89 #define	SDMINOR(unit, part)		DISKMINOR(unit, part)
     90 #define	MAKESDDEV(maj, unit, part)	MAKEDISKDEV(maj, unit, part)
     91 
     92 #define	SDLABELDEV(dev)	(MAKESDDEV(major(dev), SDUNIT(dev), RAW_PART))
     93 
     94 #define	SD_DEFAULT_BLKSIZE	512
     95 
     96 static void	sdminphys(struct buf *);
     97 static void	sdstart(struct scsipi_periph *);
     98 static void	sdrestart(void *);
     99 static void	sddone(struct scsipi_xfer *, int);
    100 static bool	sd_suspend(device_t, const pmf_qual_t *);
    101 static bool	sd_shutdown(device_t, int);
    102 static int	sd_interpret_sense(struct scsipi_xfer *);
    103 static int	sd_diskstart(device_t, struct buf *);
    104 static int	sd_dumpblocks(device_t, void *, daddr_t, int);
    105 static void	sd_iosize(device_t, int *);
    106 static int	sd_lastclose(device_t);
    107 static int	sd_firstopen(device_t, dev_t, int, int);
    108 static void	sd_label(device_t, struct disklabel *);
    109 static int	sd_discard(device_t, off_t, off_t);
    110 
    111 static int	sd_mode_sense(struct sd_softc *, u_int8_t, void *, size_t, int,
    112 		    int, int *);
    113 static int	sd_mode_select(struct sd_softc *, u_int8_t, void *, size_t,
    114 		    int, int);
    115 static int	sd_validate_blksize(struct scsipi_periph *, int);
    116 static u_int64_t sd_read_capacity(struct sd_softc *, int *, int flags);
    117 static int	sd_get_simplifiedparms(struct sd_softc *, struct disk_parms *,
    118 		    int);
    119 static int	sd_get_capacity(struct sd_softc *, struct disk_parms *, int);
    120 static int	sd_get_parms(struct sd_softc *, struct disk_parms *, int);
    121 static int	sd_get_parms_page4(struct sd_softc *, struct disk_parms *,
    122 		    int);
    123 static int	sd_get_parms_page5(struct sd_softc *, struct disk_parms *,
    124 		    int);
    125 
    126 static int	sd_flush(struct sd_softc *, int);
    127 static int	sd_getcache(struct sd_softc *, int *);
    128 static int	sd_setcache(struct sd_softc *, int);
    129 
    130 static int	sdmatch(device_t, cfdata_t, void *);
    131 static void	sdattach(device_t, device_t, void *);
    132 static int	sddetach(device_t, int);
    133 static void	sd_set_geometry(struct sd_softc *);
    134 
    135 CFATTACH_DECL3_NEW(sd, sizeof(struct sd_softc), sdmatch, sdattach, sddetach,
    136     NULL, NULL, NULL, DVF_DETACH_SHUTDOWN);
    137 
    138 extern struct cfdriver sd_cd;
    139 
    140 static const struct scsipi_inquiry_pattern sd_patterns[] = {
    141 	{T_DIRECT, T_FIXED,
    142 	 "",         "",                 ""},
    143 	{T_DIRECT, T_REMOV,
    144 	 "",         "",                 ""},
    145 	{T_OPTICAL, T_FIXED,
    146 	 "",         "",                 ""},
    147 	{T_OPTICAL, T_REMOV,
    148 	 "",         "",                 ""},
    149 	{T_SIMPLE_DIRECT, T_FIXED,
    150 	 "",         "",                 ""},
    151 	{T_SIMPLE_DIRECT, T_REMOV,
    152 	 "",         "",                 ""},
    153 };
    154 
    155 static dev_type_open(sdopen);
    156 static dev_type_close(sdclose);
    157 static dev_type_read(sdread);
    158 static dev_type_write(sdwrite);
    159 static dev_type_ioctl(sdioctl);
    160 static dev_type_strategy(sdstrategy);
    161 static dev_type_dump(sddump);
    162 static dev_type_size(sdsize);
    163 static dev_type_discard(sddiscard);
    164 
    165 const struct bdevsw sd_bdevsw = {
    166 	.d_open = sdopen,
    167 	.d_close = sdclose,
    168 	.d_strategy = sdstrategy,
    169 	.d_ioctl = sdioctl,
    170 	.d_dump = sddump,
    171 	.d_psize = sdsize,
    172 	.d_discard = sddiscard,
    173 	.d_cfdriver = &sd_cd,
    174 	.d_devtounit = disklabel_dev_unit,
    175 	.d_flag = D_DISK | D_MPSAFE
    176 };
    177 
    178 const struct cdevsw sd_cdevsw = {
    179 	.d_open = sdopen,
    180 	.d_close = sdclose,
    181 	.d_read = sdread,
    182 	.d_write = sdwrite,
    183 	.d_ioctl = sdioctl,
    184 	.d_stop = nostop,
    185 	.d_tty = notty,
    186 	.d_poll = nopoll,
    187 	.d_mmap = nommap,
    188 	.d_kqfilter = nokqfilter,
    189 	.d_discard = sddiscard,
    190 	.d_cfdriver = &sd_cd,
    191 	.d_devtounit = disklabel_dev_unit,
    192 	.d_flag = D_DISK | D_MPSAFE
    193 };
    194 
    195 static const struct dkdriver sddkdriver = {
    196 	.d_open = sdopen,
    197 	.d_close = sdclose,
    198 	.d_strategy = sdstrategy,
    199 	.d_minphys = sdminphys,
    200 	.d_diskstart = sd_diskstart,
    201 	.d_dumpblocks = sd_dumpblocks,
    202 	.d_iosize = sd_iosize,
    203 	.d_firstopen = sd_firstopen,
    204 	.d_lastclose = sd_lastclose,
    205 	.d_label = sd_label,
    206 	.d_discard = sd_discard,
    207 };
    208 
    209 static const struct scsipi_periphsw sd_switch = {
    210 	sd_interpret_sense,	/* check our error handler first */
    211 	sdstart,		/* have a queue, served by this */
    212 	NULL,			/* have no async handler */
    213 	sddone,			/* deal with stats at interrupt time */
    214 };
    215 
    216 struct sd_mode_sense_data {
    217 	/*
    218 	 * XXX
    219 	 * We are not going to parse this as-is -- it just has to be large
    220 	 * enough.
    221 	 */
    222 	union {
    223 		struct scsi_mode_parameter_header_6 small;
    224 		struct scsi_mode_parameter_header_10 big;
    225 	} header;
    226 	struct scsi_general_block_descriptor blk_desc;
    227 	union scsi_disk_pages pages;
    228 };
    229 
    230 /*
    231  * The routine called by the low level scsi routine when it discovers
    232  * A device suitable for this driver
    233  */
    234 static int
    235 sdmatch(device_t parent, cfdata_t match,
    236     void *aux)
    237 {
    238 	struct scsipibus_attach_args *sa = aux;
    239 	int priority;
    240 
    241 	(void)scsipi_inqmatch(&sa->sa_inqbuf,
    242 	    sd_patterns, sizeof(sd_patterns) / sizeof(sd_patterns[0]),
    243 	    sizeof(sd_patterns[0]), &priority);
    244 
    245 	return (priority);
    246 }
    247 
    248 /*
    249  * Attach routine common to atapi & scsi.
    250  */
    251 static void
    252 sdattach(device_t parent, device_t self, void *aux)
    253 {
    254 	struct sd_softc *sd = device_private(self);
    255 	struct dk_softc *dksc = &sd->sc_dksc;
    256 	struct scsipibus_attach_args *sa = aux;
    257 	struct scsipi_periph *periph = sa->sa_periph;
    258 	int error, result, dtype;
    259 	struct disk_parms *dp = &sd->params;
    260 	char pbuf[9];
    261 
    262 	SC_DEBUG(periph, SCSIPI_DB2, ("sdattach: "));
    263 
    264 	sd->type = (sa->sa_inqbuf.type & SID_TYPE);
    265 	memcpy(sd->name, sa->sa_inqbuf.product, uimin(16, sizeof(sd->name)));
    266 	memcpy(sd->typename, sa->sa_inqbuf.product,
    267 	    uimin(16, sizeof(sd->typename)));
    268 
    269 	if (sd->type == T_SIMPLE_DIRECT) {
    270 		periph->periph_quirks |= PQUIRK_ONLYBIG;
    271 		periph->periph_quirks |= PQUIRK_NOBIGMODESENSE;
    272 	}
    273 
    274 	switch (SCSIPI_BUSTYPE_TYPE(scsipi_periph_bustype(sa->sa_periph))) {
    275 	case SCSIPI_BUSTYPE_SCSI:
    276 		dtype = DKTYPE_SCSI;
    277 		if (periph->periph_version == 0)
    278 			sd->flags |= SDF_ANCIENT;
    279 		break;
    280 	case SCSIPI_BUSTYPE_ATAPI:
    281 		dtype = DKTYPE_ATAPI;
    282 		break;
    283 	default:
    284 		dtype = DKTYPE_UNKNOWN;
    285 		break;
    286 	}
    287 
    288 	/* Initialize dk and disk structure. */
    289 	dk_init(dksc, self, dtype);
    290 	disk_init(&dksc->sc_dkdev, dksc->sc_xname, &sddkdriver);
    291 
    292 	/* Attach dk and disk subsystems */
    293 	dk_attach(dksc);
    294 	disk_attach(&dksc->sc_dkdev);
    295 
    296 	bufq_alloc(&dksc->sc_bufq, BUFQ_DISK_DEFAULT_STRAT,
    297 	    BUFQ_SORT_RAWBLOCK);
    298 
    299 	callout_init(&sd->sc_callout, 0);
    300 
    301 	/*
    302 	 * Store information needed to contact our base driver
    303 	 */
    304 	sd->sc_periph = periph;
    305 
    306 	periph->periph_dev = dksc->sc_dev;
    307 	periph->periph_switch = &sd_switch;
    308 
    309 	/*
    310 	 * Increase our openings to the maximum-per-periph
    311 	 * supported by the adapter.  This will either be
    312 	 * clamped down or grown by the adapter if necessary.
    313 	 */
    314 	periph->periph_openings =
    315 	    SCSIPI_CHAN_MAX_PERIPH(periph->periph_channel);
    316 	periph->periph_flags |= PERIPH_GROW_OPENINGS;
    317 
    318 	/*
    319 	 * Use the subdriver to request information regarding the drive.
    320 	 */
    321 	aprint_naive("\n");
    322 	aprint_normal("\n");
    323 
    324 	if (periph->periph_quirks & PQUIRK_START)
    325 		(void)scsipi_start(periph, SSS_START, XS_CTL_SILENT);
    326 
    327 	error = scsipi_test_unit_ready(periph,
    328 	    XS_CTL_DISCOVERY | XS_CTL_IGNORE_ILLEGAL_REQUEST |
    329 	    XS_CTL_IGNORE_MEDIA_CHANGE | XS_CTL_SILENT_NODEV);
    330 	if (error)
    331 		result = SDGP_RESULT_OFFLINE;
    332 	else
    333 		result = sd_get_parms(sd, &sd->params, XS_CTL_DISCOVERY);
    334 
    335 	aprint_normal_dev(dksc->sc_dev, "");
    336 	switch (result) {
    337 	case SDGP_RESULT_OK:
    338 		format_bytes(pbuf, sizeof(pbuf),
    339 		    (u_int64_t)dp->disksize * dp->blksize);
    340 		aprint_normal("%s, %ld cyl, %ld head, %ld sec, %ld bytes/sect"
    341 		    " x %llu sectors",
    342 		    pbuf, dp->cyls, dp->heads, dp->sectors, dp->blksize,
    343 		    (unsigned long long)dp->disksize);
    344 		if (dp->lbppbe) {
    345 			aprint_normal(" (%lu bytes/physsect", dp->blksize <<
    346 			    dp->lbppbe);
    347 			if (dp->lalba)
    348 				aprint_normal("; first aligned sector %u",
    349 				    dp->lalba);
    350 			aprint_normal(")");
    351 		}
    352 		break;
    353 
    354 	case SDGP_RESULT_OFFLINE:
    355 		aprint_normal("drive offline");
    356 		break;
    357 
    358 	case SDGP_RESULT_UNFORMATTED:
    359 		aprint_normal("unformatted media");
    360 		break;
    361 
    362 #ifdef DIAGNOSTIC
    363 	default:
    364 		panic("sdattach: unknown result from get_parms");
    365 		break;
    366 #endif
    367 	}
    368 	aprint_normal("\n");
    369 
    370 	/* Discover wedges on this disk if it is online */
    371 	if (result == SDGP_RESULT_OK)
    372 		dkwedge_discover(&dksc->sc_dkdev);
    373 
    374 	/*
    375 	 * Establish a shutdown hook so that we can ensure that
    376 	 * our data has actually made it onto the platter at
    377 	 * shutdown time.  Note that this relies on the fact
    378 	 * that the shutdown hooks at the "leaves" of the device tree
    379 	 * are run, first (thus guaranteeing that our hook runs before
    380 	 * our ancestors').
    381 	 */
    382 	if (!pmf_device_register1(self, sd_suspend, NULL, sd_shutdown))
    383 		aprint_error_dev(self, "couldn't establish power handler\n");
    384 }
    385 
    386 static int
    387 sddetach(device_t self, int flags)
    388 {
    389 	struct sd_softc *sd = device_private(self);
    390 	struct dk_softc *dksc = &sd->sc_dksc;
    391 	struct scsipi_periph *periph = sd->sc_periph;
    392 	struct scsipi_channel *chan = periph->periph_channel;
    393 	int bmaj, cmaj, i, mn, rc;
    394 
    395 	rc = disk_begindetach(&dksc->sc_dkdev, sd_lastclose, self, flags);
    396 	if (rc)
    397 		return rc;
    398 
    399 	/* locate the major number */
    400 	bmaj = bdevsw_lookup_major(&sd_bdevsw);
    401 	cmaj = cdevsw_lookup_major(&sd_cdevsw);
    402 
    403 	/* Nuke the vnodes for any open instances */
    404 	for (i = 0; i < MAXPARTITIONS; i++) {
    405 		mn = SDMINOR(device_unit(self), i);
    406 		vdevgone(bmaj, mn, mn, VBLK);
    407 		vdevgone(cmaj, mn, mn, VCHR);
    408 	}
    409 
    410 	/* kill any pending restart */
    411 	callout_halt(&sd->sc_callout, NULL);
    412 
    413 	dk_drain(dksc);
    414 
    415 	/* Kill off any pending commands. */
    416 	mutex_enter(chan_mtx(chan));
    417 	scsipi_kill_pending(periph);
    418 	mutex_exit(chan_mtx(chan));
    419 
    420 	bufq_free(dksc->sc_bufq);
    421 
    422 	/* Delete all of our wedges. */
    423 	dkwedge_delall(&dksc->sc_dkdev);
    424 
    425 	/* Detach from the disk list. */
    426 	disk_detach(&dksc->sc_dkdev);
    427 	disk_destroy(&dksc->sc_dkdev);
    428 
    429 	dk_detach(dksc);
    430 
    431 	callout_destroy(&sd->sc_callout);
    432 
    433 	pmf_device_deregister(self);
    434 
    435 	return (0);
    436 }
    437 
    438 /*
    439  * Serialized by caller
    440  */
    441 static int
    442 sd_firstopen(device_t self, dev_t dev, int flag, int fmt)
    443 {
    444 	struct sd_softc *sd = device_private(self);
    445 	struct scsipi_periph *periph = sd->sc_periph;
    446 	struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
    447 	int error, silent;
    448 	int part, removable;
    449 
    450 	part = SDPART(dev);
    451 
    452 	error = scsipi_adapter_addref(adapt);
    453 	if (error)
    454 		return error;
    455 
    456 	if ((part == RAW_PART && fmt == S_IFCHR) || (flag & FSILENT))
    457 		silent = XS_CTL_SILENT;
    458 	else
    459 		silent = 0;
    460 
    461 	/* Check that it is still responding and ok. */
    462 	error = scsipi_test_unit_ready(periph,
    463 	    XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_MEDIA_CHANGE |
    464 	    silent);
    465 
    466 	/*
    467 	 * Start the pack spinning if necessary. Always allow the
    468 	 * raw partition to be opened, for raw IOCTLs. Data transfers
    469 	 * will check for SDEV_MEDIA_LOADED.
    470 	 */
    471 	if (error == EIO) {
    472 		error = scsipi_start(periph, SSS_START, silent);
    473 		if (error == EINVAL)
    474 			error = EIO;
    475 	}
    476 	if (error)
    477 		goto bad;
    478 
    479 	removable = (periph->periph_flags & PERIPH_REMOVABLE) != 0;
    480 	if (removable) {
    481 		/* Lock the pack in. */
    482 		error = scsipi_prevent(periph, SPAMR_PREVENT_DT,
    483 		    XS_CTL_IGNORE_ILLEGAL_REQUEST |
    484 		    XS_CTL_IGNORE_MEDIA_CHANGE |
    485 		    XS_CTL_SILENT);
    486 		if (error)
    487 			goto bad;
    488 	}
    489 
    490 	if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) {
    491 		int param_error;
    492 
    493 		/*
    494 		 * Load the physical device parameters.
    495 		 *
    496 		 * Note that if media is present but unformatted,
    497 		 * we allow the open (so that it can be formatted!).
    498 		 * The drive should refuse real I/O, if the media is
    499 		 * unformatted.
    500 		 */
    501 		param_error = sd_get_parms(sd, &sd->params, 0);
    502 		if (param_error == SDGP_RESULT_OFFLINE) {
    503 			error = ENXIO;
    504 			goto bad2;
    505 		}
    506 		periph->periph_flags |= PERIPH_MEDIA_LOADED;
    507 
    508 		SC_DEBUG(periph, SCSIPI_DB3, ("Params loaded "));
    509 	}
    510 
    511 	periph->periph_flags |= PERIPH_OPEN;
    512 	return 0;
    513 
    514 bad2:
    515 	if (removable)
    516 		scsipi_prevent(periph, SPAMR_ALLOW,
    517 		    XS_CTL_IGNORE_ILLEGAL_REQUEST |
    518 		    XS_CTL_IGNORE_MEDIA_CHANGE |
    519 		    XS_CTL_SILENT);
    520 
    521 bad:
    522 	scsipi_adapter_delref(adapt);
    523 	return error;
    524 }
    525 
    526 /*
    527  * open the device. Make sure the partition info is a up-to-date as can be.
    528  */
    529 static int
    530 sdopen(dev_t dev, int flag, int fmt, struct lwp *l)
    531 {
    532 	struct sd_softc *sd;
    533 	struct dk_softc *dksc;
    534 	struct scsipi_periph *periph;
    535 	int unit, part;
    536 	int error;
    537 
    538 	unit = SDUNIT(dev);
    539 	sd = device_lookup_private(&sd_cd, unit);
    540 	if (sd == NULL)
    541 		return (ENXIO);
    542 	dksc = &sd->sc_dksc;
    543 
    544 	if (!device_is_active(dksc->sc_dev))
    545 		return (ENODEV);
    546 
    547 	periph = sd->sc_periph;
    548 	part = SDPART(dev);
    549 
    550 	SC_DEBUG(periph, SCSIPI_DB1,
    551 	    ("sdopen: dev=0x%"PRIx64" (unit %d (of %d), partition %d)\n",
    552 	    dev, unit, sd_cd.cd_ndevs, SDPART(dev)));
    553 
    554 	/*
    555 	 * If any partition is open, but the disk has been invalidated,
    556 	 * disallow further opens of non-raw partition
    557 	 */
    558 	if ((periph->periph_flags & (PERIPH_OPEN | PERIPH_MEDIA_LOADED)) ==
    559 	    PERIPH_OPEN) {
    560 		if (part != RAW_PART || fmt != S_IFCHR)
    561 			return EIO;
    562 	}
    563 
    564 	error = dk_open(dksc, dev, flag, fmt, l);
    565 
    566 	SC_DEBUG(periph, SCSIPI_DB3, ("open complete\n"));
    567 
    568 	return error;
    569 }
    570 
    571 /*
    572  * Serialized by caller
    573  */
    574 static int
    575 sd_lastclose(device_t self)
    576 {
    577 	struct sd_softc *sd = device_private(self);
    578 	struct dk_softc *dksc = &sd->sc_dksc;
    579 	struct scsipi_periph *periph = sd->sc_periph;
    580 	struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
    581 
    582 	/*
    583 	 * If the disk cache needs flushing, and the disk supports
    584 	 * it, do it now.
    585 	 */
    586 	if ((sd->flags & SDF_DIRTY) != 0) {
    587 		if (sd_flush(sd, 0)) {
    588 			aprint_error_dev(dksc->sc_dev,
    589 				"cache synchronization failed\n");
    590 			sd->flags &= ~SDF_FLUSHING;
    591 		} else
    592 			sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY);
    593 	}
    594 
    595 	scsipi_wait_drain(periph);
    596 
    597 	if (periph->periph_flags & PERIPH_REMOVABLE)
    598 		scsipi_prevent(periph, SPAMR_ALLOW,
    599 		    XS_CTL_IGNORE_ILLEGAL_REQUEST |
    600 		    XS_CTL_IGNORE_NOT_READY |
    601 		    XS_CTL_SILENT);
    602 	periph->periph_flags &= ~PERIPH_OPEN;
    603 
    604 	scsipi_wait_drain(periph);
    605 
    606 	scsipi_adapter_delref(adapt);
    607 
    608 	return 0;
    609 }
    610 
    611 /*
    612  * close the device.. only called if we are the LAST occurrence 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;
    619 	struct dk_softc *dksc;
    620 	int unit;
    621 
    622 	unit = SDUNIT(dev);
    623 	sd = device_lookup_private(&sd_cd, unit);
    624 	dksc = &sd->sc_dksc;
    625 
    626 	return dk_close(dksc, dev, flag, fmt, l);
    627 }
    628 
    629 /*
    630  * Actually translate the requested transfer into one the physical driver
    631  * can understand.  The transfer is described by a buf and will include
    632  * only one physical transfer.
    633  */
    634 static void
    635 sdstrategy(struct buf *bp)
    636 {
    637 	struct sd_softc *sd = device_lookup_private(&sd_cd, SDUNIT(bp->b_dev));
    638 	struct dk_softc *dksc = &sd->sc_dksc;
    639 	struct scsipi_periph *periph = sd->sc_periph;
    640 
    641 	SC_DEBUG(sd->sc_periph, SCSIPI_DB2, ("sdstrategy "));
    642 	SC_DEBUG(sd->sc_periph, SCSIPI_DB1,
    643 	    ("%d bytes @ blk %" PRId64 "\n", bp->b_bcount, bp->b_blkno));
    644 
    645 	/*
    646 	 * If the device has been made invalid, error out
    647 	 */
    648 	if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0 ||
    649 	    !device_is_active(dksc->sc_dev)) {
    650 		if (periph->periph_flags & PERIPH_OPEN)
    651 			bp->b_error = EIO;
    652 		else
    653 			bp->b_error = ENODEV;
    654 
    655 		bp->b_resid = bp->b_bcount;
    656 		biodone(bp);
    657 		return;
    658 	}
    659 
    660 	dk_strategy(dksc, bp);
    661 }
    662 
    663 /*
    664  * Issue single I/O command
    665  *
    666  * Called from dk_start and implicitly from dk_strategy
    667  */
    668 static int
    669 sd_diskstart(device_t dev, struct buf *bp)
    670 {
    671 	struct sd_softc *sd = device_private(dev);
    672 	struct scsipi_periph *periph = sd->sc_periph;
    673 	struct scsipi_channel *chan = periph->periph_channel;
    674 	struct scsipi_rw_16 cmd16;
    675 	struct scsipi_rw_10 cmd_big;
    676 	struct scsi_rw_6 cmd_small;
    677 	struct scsipi_generic *cmdp;
    678 	struct scsipi_xfer *xs;
    679 	int error, flags, nblks, cmdlen;
    680 	int cdb_flags;
    681 	bool havefua = !(periph->periph_quirks & PQUIRK_NOFUA);
    682 
    683 	mutex_enter(chan_mtx(chan));
    684 
    685 	if (periph->periph_active >= periph->periph_openings) {
    686 		error = EAGAIN;
    687 		goto out;
    688 	}
    689 
    690 	/*
    691 	 * there is excess capacity, but a special waits
    692 	 * It'll need the adapter as soon as we clear out of the
    693 	 * way and let it run (user level wait).
    694 	 */
    695 	if (periph->periph_flags & PERIPH_WAITING) {
    696 		periph->periph_flags &= ~PERIPH_WAITING;
    697 		cv_broadcast(periph_cv_periph(periph));
    698 		error = EAGAIN;
    699 		goto out;
    700 	}
    701 
    702 	/*
    703 	 * If the device has become invalid, abort all the
    704 	 * reads and writes until all files have been closed and
    705 	 * re-opened.
    706 	 */
    707 	if (__predict_false(
    708 	    (periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)) {
    709 		error = EIO;
    710 		goto out;
    711 	}
    712 
    713 	/*
    714 	 * Mark the disk dirty so that the cache will be
    715 	 * flushed on close.
    716 	 */
    717 	if ((bp->b_flags & B_READ) == 0)
    718 		sd->flags |= SDF_DIRTY;
    719 
    720 	if (sd->params.blksize == DEV_BSIZE)
    721 		nblks = bp->b_bcount >> DEV_BSHIFT;
    722 	else
    723 		nblks = howmany(bp->b_bcount, sd->params.blksize);
    724 
    725 	/*
    726 	 * Pass FUA and/or DPO if requested. Must be done before CDB
    727 	 * selection, as 6-byte CDB doesn't support the flags.
    728 	 */
    729 	cdb_flags = 0;
    730 	if (havefua) {
    731 		if (bp->b_flags & B_MEDIA_FUA)
    732 			cdb_flags |= SRWB_FUA;
    733 
    734 		if (bp->b_flags & B_MEDIA_DPO)
    735 			cdb_flags |= SRWB_DPO;
    736 	}
    737 
    738 	/*
    739 	 * Fill out the scsi command.  Use the smallest CDB possible
    740 	 * (6-byte, 10-byte, or 16-byte). If we need FUA or DPO,
    741 	 * need to use 10-byte or bigger, as the 6-byte doesn't support
    742 	 * the flags.
    743 	 */
    744 	if (((bp->b_rawblkno & 0x1fffff) == bp->b_rawblkno) &&
    745 	    ((nblks & 0xff) == nblks) &&
    746 	    !(periph->periph_quirks & PQUIRK_ONLYBIG) &&
    747 	    !cdb_flags) {
    748 		/* 6-byte CDB */
    749 		memset(&cmd_small, 0, sizeof(cmd_small));
    750 		cmd_small.opcode = (bp->b_flags & B_READ) ?
    751 		    SCSI_READ_6_COMMAND : SCSI_WRITE_6_COMMAND;
    752 		_lto3b(bp->b_rawblkno, cmd_small.addr);
    753 		cmd_small.length = nblks & 0xff;
    754 		cmdlen = sizeof(cmd_small);
    755 		cmdp = (struct scsipi_generic *)&cmd_small;
    756 	} else if ((bp->b_rawblkno & 0xffffffff) == bp->b_rawblkno) {
    757 		/* 10-byte CDB */
    758 		memset(&cmd_big, 0, sizeof(cmd_big));
    759 		cmd_big.opcode = (bp->b_flags & B_READ) ?
    760 		    READ_10 : WRITE_10;
    761 		_lto4b(bp->b_rawblkno, cmd_big.addr);
    762 		_lto2b(nblks, cmd_big.length);
    763 		cmdlen = sizeof(cmd_big);
    764 		cmdp = (struct scsipi_generic *)&cmd_big;
    765 	} else {
    766 		/* 16-byte CDB */
    767 		memset(&cmd16, 0, sizeof(cmd16));
    768 		cmd16.opcode = (bp->b_flags & B_READ) ?
    769 		    READ_16 : WRITE_16;
    770 		_lto8b(bp->b_rawblkno, cmd16.addr);
    771 		_lto4b(nblks, cmd16.length);
    772 		cmdlen = sizeof(cmd16);
    773 		cmdp = (struct scsipi_generic *)&cmd16;
    774 	}
    775 
    776 	if (cdb_flags)
    777 		cmdp->bytes[0] = cdb_flags;
    778 
    779 	/*
    780 	 * Figure out what flags to use.
    781 	 */
    782 	flags = XS_CTL_NOSLEEP|XS_CTL_ASYNC|XS_CTL_SIMPLE_TAG;
    783 	if (bp->b_flags & B_READ)
    784 		flags |= XS_CTL_DATA_IN;
    785 	else
    786 		flags |= XS_CTL_DATA_OUT;
    787 
    788 	/*
    789 	 * Call the routine that chats with the adapter.
    790 	 * Note: we cannot sleep as we may be an interrupt
    791 	 */
    792 	xs = scsipi_make_xs_locked(periph, cmdp, cmdlen,
    793 	    (u_char *)bp->b_data, bp->b_bcount,
    794 	    SDRETRIES, SD_IO_TIMEOUT, bp, flags);
    795 	if (__predict_false(xs == NULL)) {
    796 		/*
    797 		 * out of memory. Keep this buffer in the queue, and
    798 		 * retry later.
    799 		 */
    800 		callout_reset(&sd->sc_callout, hz / 2, sdrestart, sd);
    801 		error = EAGAIN;
    802 		goto out;
    803 	}
    804 
    805 	error = scsipi_execute_xs(xs);
    806 	/* with a scsipi_xfer preallocated, scsipi_command can't fail */
    807 	KASSERT(error == 0);
    808 
    809 out:
    810 	mutex_exit(chan_mtx(chan));
    811 
    812 	return error;
    813 }
    814 
    815 /*
    816  * Recover I/O request after memory shortage
    817  *
    818  * Called from callout
    819  */
    820 static void
    821 sdrestart(void *v)
    822 {
    823 	struct sd_softc *sd = v;
    824 	struct dk_softc *dksc = &sd->sc_dksc;
    825 
    826 	dk_start(dksc, NULL);
    827 }
    828 
    829 /*
    830  * Recover I/O request after memory shortage
    831  *
    832  * Called from scsipi midlayer when resources have been freed
    833  * with channel lock held
    834  */
    835 static void
    836 sdstart(struct scsipi_periph *periph)
    837 {
    838 	struct sd_softc *sd = device_private(periph->periph_dev);
    839 	struct dk_softc *dksc = &sd->sc_dksc;
    840 	struct scsipi_channel *chan = periph->periph_channel;
    841 
    842 	/*
    843 	 * release channel lock as dk_start may need to acquire
    844 	 * other locks
    845 	 *
    846 	 * sdstart is called from scsipi_put_xs and all its callers
    847 	 * release the lock afterwards. So releasing it here
    848 	 * doesn't matter.
    849 	 */
    850 	mutex_exit(chan_mtx(chan));
    851 
    852 	dk_start(dksc, NULL);
    853 
    854 	mutex_enter(chan_mtx(chan));
    855 }
    856 
    857 static void
    858 sddone(struct scsipi_xfer *xs, int error)
    859 {
    860 	struct sd_softc *sd = device_private(xs->xs_periph->periph_dev);
    861 	struct dk_softc *dksc = &sd->sc_dksc;
    862 	struct buf *bp = xs->bp;
    863 
    864 	if (sd->flags & SDF_FLUSHING) {
    865 		/* Flush completed, no longer dirty. */
    866 		sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY);
    867 	}
    868 
    869 	if (bp) {
    870 		bp->b_error = error;
    871 		bp->b_resid = xs->resid;
    872 		if (error) {
    873 			/* on a read/write error bp->b_resid is zero, so fix */
    874 			bp->b_resid = bp->b_bcount;
    875 		}
    876 
    877 		dk_done(dksc, bp);
    878 		/* dk_start is called from scsipi_complete */
    879 	}
    880 }
    881 
    882 static void
    883 sdminphys(struct buf *bp)
    884 {
    885 	struct sd_softc *sd = device_lookup_private(&sd_cd, SDUNIT(bp->b_dev));
    886 	struct dk_softc *dksc = &sd->sc_dksc;
    887 	long xmax;
    888 
    889 	/*
    890 	 * If the device is ancient, we want to make sure that
    891 	 * the transfer fits into a 6-byte cdb.
    892 	 *
    893 	 * XXX Note that the SCSI-I spec says that 256-block transfers
    894 	 * are allowed in a 6-byte read/write, and are specified
    895 	 * by setting the "length" to 0.  However, we're conservative
    896 	 * here, allowing only 255-block transfers in case an
    897 	 * ancient device gets confused by length == 0.  A length of 0
    898 	 * in a 10-byte read/write actually means 0 blocks.
    899 	 */
    900 	if ((sd->flags & SDF_ANCIENT) &&
    901 	    ((sd->sc_periph->periph_flags &
    902 	    (PERIPH_REMOVABLE | PERIPH_MEDIA_LOADED)) != PERIPH_REMOVABLE)) {
    903 		xmax = dksc->sc_dkdev.dk_geom.dg_secsize * 0xff;
    904 
    905 		if (bp->b_bcount > xmax)
    906 			bp->b_bcount = xmax;
    907 	}
    908 
    909 	scsipi_adapter_minphys(sd->sc_periph->periph_channel, bp);
    910 }
    911 
    912 static void
    913 sd_iosize(device_t dev, int *count)
    914 {
    915 	struct buf B;
    916 	int bmaj;
    917 
    918 	bmaj       = bdevsw_lookup_major(&sd_bdevsw);
    919 	B.b_dev    = MAKESDDEV(bmaj,device_unit(dev),RAW_PART);
    920 	B.b_bcount = *count;
    921 
    922 	sdminphys(&B);
    923 
    924 	*count = B.b_bcount;
    925 }
    926 
    927 static int
    928 sdread(dev_t dev, struct uio *uio, int ioflag)
    929 {
    930 
    931 	return (physio(sdstrategy, NULL, dev, B_READ, sdminphys, uio));
    932 }
    933 
    934 static int
    935 sdwrite(dev_t dev, struct uio *uio, int ioflag)
    936 {
    937 
    938 	return (physio(sdstrategy, NULL, dev, B_WRITE, sdminphys, uio));
    939 }
    940 
    941 /*
    942  * Perform special action on behalf of the user
    943  * Knows about the internals of this device
    944  */
    945 static int
    946 sdioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
    947 {
    948 	struct sd_softc *sd = device_lookup_private(&sd_cd, SDUNIT(dev));
    949 	struct dk_softc *dksc = &sd->sc_dksc;
    950 	struct scsipi_periph *periph = sd->sc_periph;
    951 
    952 	int part = SDPART(dev);
    953 	int error;
    954 
    955 	SC_DEBUG(sd->sc_periph, SCSIPI_DB2, ("sdioctl 0x%lx ", cmd));
    956 
    957 	/*
    958 	 * If the device is not valid, some IOCTLs can still be
    959 	 * handled on the raw partition. Check this here.
    960 	 */
    961 	if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0 &&
    962 	    part != RAW_PART)
    963 		return (EIO);
    964 
    965 	switch (cmd) {
    966 	case DIOCLOCK:
    967 		if (periph->periph_flags & PERIPH_REMOVABLE)
    968 			return (scsipi_prevent(periph,
    969 			    (*(int *)addr) ?
    970 			    SPAMR_PREVENT_DT : SPAMR_ALLOW, 0));
    971 		else
    972 			return (ENOTTY);
    973 
    974 	case DIOCEJECT:
    975 		if ((periph->periph_flags & PERIPH_REMOVABLE) == 0)
    976 			return (ENOTTY);
    977 		if (*(int *)addr == 0) {
    978 			int pmask = __BIT(part);
    979 			/*
    980 			 * Don't force eject: check that we are the only
    981 			 * partition open. If so, unlock it.
    982 			 */
    983 			if (DK_BUSY(dksc, pmask) == 0) {
    984 				error = scsipi_prevent(periph, SPAMR_ALLOW,
    985 				    XS_CTL_IGNORE_NOT_READY);
    986 				if (error)
    987 					return (error);
    988 			} else {
    989 				return (EBUSY);
    990 			}
    991 		}
    992 		/* FALLTHROUGH */
    993 	case ODIOCEJECT:
    994 		return ((periph->periph_flags & PERIPH_REMOVABLE) == 0 ?
    995 		    ENOTTY : scsipi_start(periph, SSS_STOP|SSS_LOEJ, 0));
    996 
    997 	case DIOCGCACHE:
    998 		return (sd_getcache(sd, (int *) addr));
    999 
   1000 	case DIOCSCACHE:
   1001 		if ((flag & FWRITE) == 0)
   1002 			return (EBADF);
   1003 		return (sd_setcache(sd, *(int *) addr));
   1004 
   1005 	case DIOCCACHESYNC:
   1006 		/*
   1007 		 * XXX Do we really need to care about having a writable
   1008 		 * file descriptor here?
   1009 		 */
   1010 		if ((flag & FWRITE) == 0)
   1011 			return (EBADF);
   1012 		if (((sd->flags & SDF_DIRTY) != 0 || *(int *)addr != 0)) {
   1013 			error = sd_flush(sd, 0);
   1014 			if (error) {
   1015 				sd->flags &= ~SDF_FLUSHING;
   1016 				return (error);
   1017 			}
   1018 			sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY);
   1019 		}
   1020 		return (0);
   1021 
   1022 	case DIOCGSECTORALIGN: {
   1023 		struct disk_sectoralign *dsa = addr;
   1024 
   1025 		dsa->dsa_alignment = 1u << sd->params.lbppbe;
   1026 		dsa->dsa_firstaligned = sd->params.lalba;
   1027 		if (part != RAW_PART) {
   1028 			struct disklabel *lp = dksc->sc_dkdev.dk_label;
   1029 			daddr_t offset = lp->d_partitions[part].p_offset;
   1030 			uint32_t r = offset % dsa->dsa_alignment;
   1031 
   1032 			if (r < dsa->dsa_firstaligned)
   1033 				dsa->dsa_firstaligned = dsa->dsa_firstaligned
   1034 				    - r;
   1035 			else
   1036 				dsa->dsa_firstaligned = (dsa->dsa_firstaligned
   1037 				    + dsa->dsa_alignment) - r;
   1038 		}
   1039 		dsa->dsa_firstaligned %= dsa->dsa_alignment;
   1040 
   1041 		return 0;
   1042 	}
   1043 
   1044 	default:
   1045 		error = dk_ioctl(dksc, dev, cmd, addr, flag, l);
   1046 		if (error == ENOTTY) {
   1047 			error = scsipi_do_ioctl(periph, dev, cmd, addr, flag,
   1048 			    l);
   1049 		}
   1050 		return (error);
   1051 	}
   1052 
   1053 #ifdef DIAGNOSTIC
   1054 	panic("sdioctl: impossible");
   1055 #endif
   1056 }
   1057 
   1058 static void
   1059 sd_label(device_t self, struct disklabel *lp)
   1060 {
   1061 	struct sd_softc *sd = device_private(self);
   1062 
   1063 	strncpy(lp->d_typename, sd->name, 16);
   1064 	lp->d_rpm = sd->params.rot_rate;
   1065 	if (sd->sc_periph->periph_flags & PERIPH_REMOVABLE)
   1066 		lp->d_flags |= D_REMOVABLE;
   1067 }
   1068 
   1069 static int
   1070 sd_unmap(struct sd_softc *sd, off_t pos, off_t len)
   1071 {
   1072 	struct scsi_unmap_10 cmd;
   1073 	struct scsi_unmap_10_data data;
   1074 	int flags = 0;
   1075 	uint64_t bno;
   1076 	uint32_t size;
   1077 
   1078 	/* round the start up and the end down */
   1079 	bno = (pos + sd->params.blksize - 1) / sd->params.blksize;
   1080 	size = ((pos + len) / sd->params.blksize) - bno;
   1081 
   1082 	if (size == 0)
   1083 		return 0;
   1084 
   1085 	memset(&data, 0, sizeof(data));
   1086 	_lto2b(sizeof(data) - 2, data.unmap_data_length);
   1087 	_lto2b(sizeof(data) - 8, data.unmap_block_descriptor_data_length);
   1088 	_lto8b(bno, data.unmap_block_descriptor[0].addr);
   1089 	_lto4b(size, data.unmap_block_descriptor[0].len);
   1090 
   1091 	memset(&cmd, 0, sizeof(cmd));
   1092 	cmd.opcode = UNMAP_10;
   1093 	cmd.byte2 = 0;
   1094 	_lto2b(sizeof(data), cmd.length);
   1095 
   1096 	scsipi_command(sd->sc_periph,
   1097 	    (void *)&cmd, sizeof(cmd), (void *)&data, sizeof(data),
   1098 	    SDRETRIES, 2000000, NULL,
   1099 	    flags | XS_CTL_DATA_OUT);
   1100 
   1101 	return 0;
   1102 }
   1103 
   1104 static int
   1105 sd_discard(device_t self, off_t pos, off_t len)
   1106 {
   1107 	struct sd_softc *sd = device_private(self);
   1108 	if (sd->flags & SDF_LBPU) {
   1109 		return sd_unmap(sd, pos, len);
   1110 	}
   1111 	return ENODEV;
   1112 }
   1113 
   1114 static bool
   1115 sd_shutdown(device_t self, int how)
   1116 {
   1117 	struct sd_softc *sd = device_private(self);
   1118 	struct dk_softc *dksc = &sd->sc_dksc;
   1119 
   1120 	/*
   1121 	 * If the disk cache needs to be flushed, and the disk supports
   1122 	 * it, flush it.  We're cold at this point, so we poll for
   1123 	 * completion.
   1124 	 */
   1125 	if ((sd->flags & SDF_DIRTY) != 0) {
   1126 		if (sd_flush(sd, XS_CTL_NOSLEEP|XS_CTL_POLL)) {
   1127 			aprint_error_dev(dksc->sc_dev,
   1128 				"cache synchronization failed\n");
   1129 			sd->flags &= ~SDF_FLUSHING;
   1130 		} else
   1131 			sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY);
   1132 	}
   1133 	return true;
   1134 }
   1135 
   1136 static bool
   1137 sd_suspend(device_t dv, const pmf_qual_t *qual)
   1138 {
   1139 	return sd_shutdown(dv, boothowto); /* XXX no need to poll */
   1140 }
   1141 
   1142 /*
   1143  * Check Errors
   1144  */
   1145 static int
   1146 sd_interpret_sense(struct scsipi_xfer *xs)
   1147 {
   1148 	struct scsipi_periph *periph = xs->xs_periph;
   1149 	struct scsipi_channel *chan = periph->periph_channel;
   1150 	struct scsi_sense_data *sense = &xs->sense.scsi_sense;
   1151 	struct sd_softc *sd = device_private(periph->periph_dev);
   1152 	struct dk_softc *dksc = &sd->sc_dksc;
   1153 	int error, retval = EJUSTRETURN;
   1154 
   1155 	/*
   1156 	 * If the periph is already recovering, just do the normal
   1157 	 * error processing.
   1158 	 */
   1159 	if (periph->periph_flags & PERIPH_RECOVERING)
   1160 		return (retval);
   1161 
   1162 	/*
   1163 	 * Ignore errors from accessing illegal fields (e.g. trying to
   1164 	 * lock the door of a digicam, which doesn't have a door that
   1165 	 * can be locked) for the SCSI_PREVENT_ALLOW_MEDIUM_REMOVAL command.
   1166 	 */
   1167 	if (xs->cmd->opcode == SCSI_PREVENT_ALLOW_MEDIUM_REMOVAL &&
   1168 	    SSD_SENSE_KEY(sense->flags) == SKEY_ILLEGAL_REQUEST &&
   1169 	    sense->asc == 0x24 &&
   1170 	    sense->ascq == 0x00) { /* Illegal field in CDB */
   1171 		if (!(xs->xs_control & XS_CTL_SILENT)) {
   1172 			scsipi_printaddr(periph);
   1173 			printf("no door lock\n");
   1174 		}
   1175 		xs->xs_control |= XS_CTL_IGNORE_ILLEGAL_REQUEST;
   1176 		return (retval);
   1177 	}
   1178 
   1179 	/*
   1180 	 * If it isn't a extended or extended/deferred error, let
   1181 	 * the generic code handle it.
   1182 	 */
   1183 	if (SSD_RCODE(sense->response_code) != SSD_RCODE_CURRENT &&
   1184 	    SSD_RCODE(sense->response_code) != SSD_RCODE_DEFERRED)
   1185 		return (retval);
   1186 
   1187 	if (SSD_SENSE_KEY(sense->flags) == SKEY_NOT_READY &&
   1188 	    sense->asc == 0x4) {
   1189 		if (sense->ascq == 0x01)	{
   1190 			/*
   1191 			 * Unit In The Process Of Becoming Ready.
   1192 			 */
   1193 			printf("%s: waiting for pack to spin up...\n",
   1194 			    dksc->sc_xname);
   1195 			if (!callout_pending(&periph->periph_callout))
   1196 				scsipi_periph_freeze(periph, 1);
   1197 			callout_reset(&periph->periph_callout,
   1198 			    5 * hz, scsipi_periph_timed_thaw, periph);
   1199 			retval = ERESTART;
   1200 		} else if (sense->ascq == 0x02) {
   1201 			printf("%s: pack is stopped, restarting...\n",
   1202 			    dksc->sc_xname);
   1203 			mutex_enter(chan_mtx(chan));
   1204 			periph->periph_flags |= PERIPH_RECOVERING;
   1205 			mutex_exit(chan_mtx(chan));
   1206 			error = scsipi_start(periph, SSS_START,
   1207 			    XS_CTL_URGENT|XS_CTL_HEAD_TAG|
   1208 			    XS_CTL_THAW_PERIPH|XS_CTL_FREEZE_PERIPH);
   1209 			if (error) {
   1210 				aprint_error_dev(dksc->sc_dev,
   1211 					"unable to restart pack\n");
   1212 				retval = error;
   1213 			} else
   1214 				retval = ERESTART;
   1215 			mutex_enter(chan_mtx(chan));
   1216 			periph->periph_flags &= ~PERIPH_RECOVERING;
   1217 			mutex_exit(chan_mtx(chan));
   1218 		}
   1219 	}
   1220 	if (SSD_SENSE_KEY(sense->flags) == SKEY_MEDIUM_ERROR &&
   1221 	    sense->asc == 0x31 &&
   1222 	    sense->ascq == 0x00)	{ /* maybe for any asq ? */
   1223 		/* Medium Format Corrupted */
   1224 		retval = EFTYPE;
   1225 	}
   1226 	return (retval);
   1227 }
   1228 
   1229 
   1230 static int
   1231 sdsize(dev_t dev)
   1232 {
   1233 	struct sd_softc *sd;
   1234 	struct dk_softc *dksc;
   1235 	int unit;
   1236 
   1237 	unit = SDUNIT(dev);
   1238 	sd = device_lookup_private(&sd_cd, unit);
   1239 	if (sd == NULL)
   1240 		return (-1);
   1241 	dksc = &sd->sc_dksc;
   1242 
   1243 	if (!device_is_active(dksc->sc_dev))
   1244 		return (-1);
   1245 
   1246 	return dk_size(dksc, dev);
   1247 }
   1248 
   1249 /* #define SD_DUMP_NOT_TRUSTED if you just want to watch */
   1250 static struct scsipi_xfer sx;
   1251 
   1252 /*
   1253  * dump all of physical memory into the partition specified, starting
   1254  * at offset 'dumplo' into the partition.
   1255  */
   1256 static int
   1257 sddump(dev_t dev, daddr_t blkno, void *va, size_t size)
   1258 {
   1259 	struct sd_softc *sd;
   1260 	struct dk_softc *dksc;
   1261 	struct scsipi_periph *periph;
   1262 	int unit;
   1263 
   1264 	unit = SDUNIT(dev);
   1265 	if ((sd = device_lookup_private(&sd_cd, unit)) == NULL)
   1266 		return (ENXIO);
   1267 	dksc = &sd->sc_dksc;
   1268 
   1269 	if (!device_is_active(dksc->sc_dev))
   1270 		return (ENODEV);
   1271 
   1272 	periph = sd->sc_periph;
   1273 
   1274 	/* Make sure it was initialized. */
   1275 	if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
   1276 		return (ENXIO);
   1277 
   1278 	return dk_dump(dksc, dev, blkno, va, size, 0);
   1279 }
   1280 
   1281 static int
   1282 sd_dumpblocks(device_t dev, void *va, daddr_t blkno, int nblk)
   1283 {
   1284 	struct sd_softc *sd = device_private(dev);
   1285 	struct dk_softc *dksc = &sd->sc_dksc;
   1286 	struct disk_geom *dg = &dksc->sc_dkdev.dk_geom;
   1287 	struct scsipi_rw_10 cmd;	/* write command */
   1288 	struct scsipi_xfer *xs;		/* ... convenience */
   1289 	struct scsipi_periph *periph;
   1290 	struct scsipi_channel *chan;
   1291 	size_t sectorsize;
   1292 
   1293 	periph = sd->sc_periph;
   1294 	chan = periph->periph_channel;
   1295 
   1296 	sectorsize = dg->dg_secsize;
   1297 
   1298 	xs = &sx;
   1299 
   1300 #ifndef	SD_DUMP_NOT_TRUSTED
   1301 	/*
   1302 	 *  Fill out the scsi command
   1303 	 */
   1304 	memset(&cmd, 0, sizeof(cmd));
   1305 	cmd.opcode = WRITE_10;
   1306 	_lto4b(blkno, cmd.addr);
   1307 	_lto2b(nblk, cmd.length);
   1308 	/*
   1309 	 * Fill out the scsipi_xfer structure
   1310 	 *    Note: we cannot sleep as we may be an interrupt
   1311 	 * don't use scsipi_command() as it may want to wait
   1312 	 * for an xs.
   1313 	 */
   1314 	memset(xs, 0, sizeof(sx));
   1315 	xs->xs_control |= XS_CTL_NOSLEEP | XS_CTL_POLL |
   1316 	    XS_CTL_DATA_OUT;
   1317 	xs->xs_status = 0;
   1318 	xs->xs_periph = periph;
   1319 	xs->xs_retries = SDRETRIES;
   1320 	xs->timeout = 10000;	/* 10000 millisecs for a disk ! */
   1321 	xs->cmd = (struct scsipi_generic *)&cmd;
   1322 	xs->cmdlen = sizeof(cmd);
   1323 	xs->resid = nblk * sectorsize;
   1324 	xs->error = XS_NOERROR;
   1325 	xs->bp = 0;
   1326 	xs->data = va;
   1327 	xs->datalen = nblk * sectorsize;
   1328 	callout_init(&xs->xs_callout, 0);
   1329 
   1330 	/*
   1331 	 * Pass all this info to the scsi driver.
   1332 	 */
   1333 	scsipi_adapter_request(chan, ADAPTER_REQ_RUN_XFER, xs);
   1334 	if ((xs->xs_status & XS_STS_DONE) == 0 ||
   1335 	    xs->error != XS_NOERROR)
   1336 		return (EIO);
   1337 #else	/* SD_DUMP_NOT_TRUSTED */
   1338 	/* Let's just talk about this first... */
   1339 	printf("sd%d: dump addr 0x%x, blk %d\n", unit, va, blkno);
   1340 	delay(500 * 1000);	/* half a second */
   1341 #endif	/* SD_DUMP_NOT_TRUSTED */
   1342 
   1343 	return (0);
   1344 }
   1345 
   1346 static int
   1347 sd_mode_sense(struct sd_softc *sd, u_int8_t byte2, void *sense, size_t size,
   1348     int page, int flags, int *big)
   1349 {
   1350 
   1351 	if ((sd->sc_periph->periph_quirks & PQUIRK_ONLYBIG) &&
   1352 	    !(sd->sc_periph->periph_quirks & PQUIRK_NOBIGMODESENSE)) {
   1353 		*big = 1;
   1354 		return scsipi_mode_sense_big(sd->sc_periph, byte2, page, sense,
   1355 		    size + sizeof(struct scsi_mode_parameter_header_10),
   1356 		    flags, SDRETRIES, 6000);
   1357 	} else {
   1358 		*big = 0;
   1359 		return scsipi_mode_sense(sd->sc_periph, byte2, page, sense,
   1360 		    size + sizeof(struct scsi_mode_parameter_header_6),
   1361 		    flags, SDRETRIES, 6000);
   1362 	}
   1363 }
   1364 
   1365 static int
   1366 sd_mode_select(struct sd_softc *sd, u_int8_t byte2, void *sense, size_t size,
   1367     int flags, int big)
   1368 {
   1369 
   1370 	if (big) {
   1371 		struct scsi_mode_parameter_header_10 *header = sense;
   1372 
   1373 		_lto2b(0, header->data_length);
   1374 		return scsipi_mode_select_big(sd->sc_periph, byte2, sense,
   1375 		    size + sizeof(struct scsi_mode_parameter_header_10),
   1376 		    flags, SDRETRIES, 6000);
   1377 	} else {
   1378 		struct scsi_mode_parameter_header_6 *header = sense;
   1379 
   1380 		header->data_length = 0;
   1381 		return scsipi_mode_select(sd->sc_periph, byte2, sense,
   1382 		    size + sizeof(struct scsi_mode_parameter_header_6),
   1383 		    flags, SDRETRIES, 6000);
   1384 	}
   1385 }
   1386 
   1387 /*
   1388  * sd_validate_blksize:
   1389  *
   1390  *	Validate the block size.  Print error if periph is specified,
   1391  */
   1392 static int
   1393 sd_validate_blksize(struct scsipi_periph *periph, int len)
   1394 {
   1395 
   1396 	if (len >= 256 && powerof2(len) && len <= MAXPHYS) {
   1397 		return 1;
   1398 	}
   1399 
   1400 	if (periph) {
   1401 		scsipi_printaddr(periph);
   1402 		printf("%s sector size: 0x%x.  Defaulting to %d bytes.\n",
   1403 		    !powerof2(len) ?
   1404 		    "preposterous" : "unsupported",
   1405 		    len, SD_DEFAULT_BLKSIZE);
   1406 	}
   1407 
   1408 	return 0;
   1409 }
   1410 
   1411 /*
   1412  * sd_read_capacity:
   1413  *
   1414  *	Find out from the device what its capacity is.
   1415  */
   1416 static u_int64_t
   1417 sd_read_capacity(struct sd_softc *sd, int *blksize, int flags)
   1418 {
   1419 	struct scsipi_periph *periph = sd->sc_periph;
   1420 	union {
   1421 		struct scsipi_read_capacity_10 cmd;
   1422 		struct scsipi_read_capacity_16 cmd16;
   1423 	} cmd;
   1424 	union {
   1425 		struct scsipi_read_capacity_10_data data;
   1426 		struct scsipi_read_capacity_16_data data16;
   1427 	} *datap;
   1428 	uint64_t rv;
   1429 
   1430 	sd->params.lbppbe = 0;
   1431 	sd->params.lalba = 0;
   1432 
   1433 	memset(&cmd, 0, sizeof(cmd));
   1434 	cmd.cmd.opcode = READ_CAPACITY_10;
   1435 
   1436 	/*
   1437 	 * Don't allocate data buffer on stack;
   1438 	 * The lower driver layer might use the same stack and
   1439 	 * if it uses region which is in the same cacheline,
   1440 	 * cache flush ops against the data buffer won't work properly.
   1441 	 */
   1442 	datap = malloc(sizeof(*datap), M_TEMP, M_WAITOK);
   1443 	if (datap == NULL)
   1444 		return 0;
   1445 
   1446 	if (periph->periph_version >= 5) /* SPC-3 */
   1447 		goto rc16;
   1448 
   1449 	/*
   1450 	 * If the command works, interpret the result as a 4 byte
   1451 	 * number of blocks
   1452 	 */
   1453 	rv = 0;
   1454 	memset(datap, 0, sizeof(datap->data));
   1455 	if (scsipi_command(periph, (void *)&cmd.cmd, sizeof(cmd.cmd),
   1456 	    (void *)datap, sizeof(datap->data), SCSIPIRETRIES, 20000, NULL,
   1457 	    flags | XS_CTL_DATA_IN | XS_CTL_SILENT) != 0)
   1458 		goto out;
   1459 
   1460 	if (_4btol(datap->data.addr) != 0xffffffff) {
   1461 		*blksize = _4btol(datap->data.length);
   1462 		rv = _4btol(datap->data.addr) + 1;
   1463 		goto out;
   1464 	}
   1465 
   1466 	/*
   1467 	 * Device is larger than can be reflected by READ CAPACITY (10).
   1468 	 * Try READ CAPACITY (16).
   1469 	 */
   1470 
   1471  rc16:
   1472 	memset(&cmd, 0, sizeof(cmd));
   1473 	cmd.cmd16.opcode = READ_CAPACITY_16;
   1474 	cmd.cmd16.byte2 = SRC16_SERVICE_ACTION;
   1475 	_lto4b(sizeof(datap->data16), cmd.cmd16.len);
   1476 
   1477 	memset(datap, 0, sizeof(datap->data16));
   1478 	if (scsipi_command(periph, (void *)&cmd.cmd16, sizeof(cmd.cmd16),
   1479 	    (void *)datap, sizeof(datap->data16), SCSIPIRETRIES, 20000, NULL,
   1480 	    flags | XS_CTL_DATA_IN | XS_CTL_SILENT) != 0)
   1481 		goto out;
   1482 
   1483 	*blksize = _4btol(datap->data16.length);
   1484 	rv = _8btol(datap->data16.addr) + 1;
   1485 	sd->params.lbppbe = datap->data16.byte14 & SRC16D_LBPPB_EXPONENT;
   1486 	sd->params.lalba = _2btol(datap->data16.lowest_aligned) & SRC16D_LALBA;
   1487 	if (_2btol(datap->data16.lowest_aligned) & SRC16D_LBPME) {
   1488 		sd->flags |= SDF_LBPME;
   1489 	} else {
   1490 		sd->flags &= ~SDF_LBPME;
   1491 	}
   1492 
   1493  out:
   1494 	free(datap, M_TEMP);
   1495 	return rv;
   1496 }
   1497 
   1498 static int
   1499 sd_get_simplifiedparms(struct sd_softc *sd, struct disk_parms *dp, int flags)
   1500 {
   1501 	struct {
   1502 		struct scsi_mode_parameter_header_6 header;
   1503 		/* no block descriptor */
   1504 		u_int8_t pg_code; /* page code (should be 6) */
   1505 		u_int8_t pg_length; /* page length (should be 11) */
   1506 		u_int8_t wcd; /* bit0: cache disable */
   1507 		u_int8_t lbs[2]; /* logical block size */
   1508 		u_int8_t size[5]; /* number of log. blocks */
   1509 		u_int8_t pp; /* power/performance */
   1510 		u_int8_t flags;
   1511 		u_int8_t resvd;
   1512 	} scsipi_sense;
   1513 	u_int64_t blocks;
   1514 	int error, blksize;
   1515 
   1516 	/*
   1517 	 * sd_read_capacity (ie "read capacity") and mode sense page 6
   1518 	 * give the same information. Do both for now, and check
   1519 	 * for consistency.
   1520 	 * XXX probably differs for removable media
   1521 	 */
   1522 	dp->blksize = SD_DEFAULT_BLKSIZE;
   1523 	if ((blocks = sd_read_capacity(sd, &blksize, flags)) == 0)
   1524 		return (SDGP_RESULT_OFFLINE);		/* XXX? */
   1525 
   1526 	error = scsipi_mode_sense(sd->sc_periph, SMS_DBD, 6,
   1527 	    &scsipi_sense.header, sizeof(scsipi_sense),
   1528 	    flags, SDRETRIES, 6000);
   1529 
   1530 	if (error != 0)
   1531 		return (SDGP_RESULT_OFFLINE);		/* XXX? */
   1532 
   1533 	dp->blksize = blksize;
   1534 	if (!sd_validate_blksize(NULL, dp->blksize))
   1535 		dp->blksize = _2btol(scsipi_sense.lbs);
   1536 	if (!sd_validate_blksize(sd->sc_periph, dp->blksize))
   1537 		dp->blksize = SD_DEFAULT_BLKSIZE;
   1538 
   1539 	/*
   1540 	 * Create a pseudo-geometry.
   1541 	 */
   1542 	dp->heads = 64;
   1543 	dp->sectors = 32;
   1544 	dp->cyls = blocks / (dp->heads * dp->sectors);
   1545 	dp->disksize = _5btol(scsipi_sense.size);
   1546 	if (dp->disksize <= UINT32_MAX && dp->disksize != blocks) {
   1547 		printf("RBC size: mode sense=%llu, get cap=%llu\n",
   1548 		       (unsigned long long)dp->disksize,
   1549 		       (unsigned long long)blocks);
   1550 		dp->disksize = blocks;
   1551 	}
   1552 	dp->disksize512 = (dp->disksize * dp->blksize) / DEV_BSIZE;
   1553 
   1554 	return (SDGP_RESULT_OK);
   1555 }
   1556 
   1557 /*
   1558  * Get the scsi driver to send a full inquiry to the * device and use the
   1559  * results to fill out the disk parameter structure.
   1560  */
   1561 static int
   1562 sd_get_capacity(struct sd_softc *sd, struct disk_parms *dp, int flags)
   1563 {
   1564 	u_int64_t blocks;
   1565 	int error, blksize;
   1566 #if 0
   1567 	int i;
   1568 	u_int8_t *p;
   1569 #endif
   1570 
   1571 	dp->disksize = blocks = sd_read_capacity(sd, &blksize, flags);
   1572 	if (blocks == 0) {
   1573 		struct scsipi_read_format_capacities cmd;
   1574 		struct {
   1575 			struct scsipi_capacity_list_header header;
   1576 			struct scsipi_capacity_descriptor desc;
   1577 		} __packed data;
   1578 
   1579 		memset(&cmd, 0, sizeof(cmd));
   1580 		memset(&data, 0, sizeof(data));
   1581 		cmd.opcode = READ_FORMAT_CAPACITIES;
   1582 		_lto2b(sizeof(data), cmd.length);
   1583 
   1584 		error = scsipi_command(sd->sc_periph,
   1585 		    (void *)&cmd, sizeof(cmd), (void *)&data, sizeof(data),
   1586 		    SDRETRIES, 20000, NULL,
   1587 		    flags | XS_CTL_DATA_IN);
   1588 		if (error == EFTYPE) {
   1589 			/* Medium Format Corrupted, handle as not formatted */
   1590 			return (SDGP_RESULT_UNFORMATTED);
   1591 		}
   1592 		if (error || data.header.length == 0)
   1593 			return (SDGP_RESULT_OFFLINE);
   1594 
   1595 #if 0
   1596 printf("rfc: length=%d\n", data.header.length);
   1597 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");
   1598 #endif
   1599 		switch (data.desc.byte5 & SCSIPI_CAP_DESC_CODE_MASK) {
   1600 		case SCSIPI_CAP_DESC_CODE_RESERVED:
   1601 		case SCSIPI_CAP_DESC_CODE_FORMATTED:
   1602 			break;
   1603 
   1604 		case SCSIPI_CAP_DESC_CODE_UNFORMATTED:
   1605 			return (SDGP_RESULT_UNFORMATTED);
   1606 
   1607 		case SCSIPI_CAP_DESC_CODE_NONE:
   1608 			return (SDGP_RESULT_OFFLINE);
   1609 		}
   1610 
   1611 		dp->disksize = blocks = _4btol(data.desc.nblks);
   1612 		if (blocks == 0)
   1613 			return (SDGP_RESULT_OFFLINE);		/* XXX? */
   1614 
   1615 		blksize = _3btol(data.desc.blklen);
   1616 
   1617 	} else if (!sd_validate_blksize(NULL, blksize)) {
   1618 		struct sd_mode_sense_data scsipi_sense;
   1619 		int big, bsize;
   1620 		struct scsi_general_block_descriptor *bdesc;
   1621 
   1622 		memset(&scsipi_sense, 0, sizeof(scsipi_sense));
   1623 		error = sd_mode_sense(sd, 0, &scsipi_sense,
   1624 		    sizeof(scsipi_sense.blk_desc), 0, flags | XS_CTL_SILENT,
   1625 		    &big);
   1626 		if (error)
   1627 			goto next;
   1628 		if (big) {
   1629 			bdesc = (void *)(&scsipi_sense.header.big + 1);
   1630 			bsize = _2btol(scsipi_sense.header.big.blk_desc_len);
   1631 		} else {
   1632 			bdesc = (void *)(&scsipi_sense.header.small + 1);
   1633 			bsize = scsipi_sense.header.small.blk_desc_len;
   1634 		}
   1635 
   1636 #if 0
   1637 printf("page 0 sense:"); for (i = sizeof(scsipi_sense), p = (void *)&scsipi_sense; i; i--, p++) printf(" %02x", *p); printf("\n");
   1638 printf("page 0 bsize=%d\n", bsize);
   1639 printf("page 0 ok\n");
   1640 #endif
   1641 
   1642 		if (bsize >= 8) {
   1643 			blksize = _3btol(bdesc->blklen);
   1644 		}
   1645 	}
   1646 next:
   1647 	if (!sd_validate_blksize(sd->sc_periph, blksize))
   1648 		blksize = SD_DEFAULT_BLKSIZE;
   1649 
   1650 	dp->blksize = blksize;
   1651 	dp->disksize512 = (blocks * dp->blksize) / DEV_BSIZE;
   1652 
   1653 	if ((sd->flags & SDF_LBPME) == 0)
   1654 		goto end;
   1655 	struct scsipi_inquiry cmd;
   1656 	struct scsi_vpd_logical_block_provisioning vpdbuf;
   1657 	memset(&cmd, 0, sizeof(cmd));
   1658 	cmd.opcode = INQUIRY;
   1659 	cmd.length = sizeof(vpdbuf);
   1660 	cmd.byte2 |= SINQ_EVPD;
   1661 	cmd.pagecode = SINQ_VPD_LOGICAL_PROV;
   1662 
   1663 	sd->flags &= ~SDF_LBPU;
   1664 	if (scsipi_command(sd->sc_periph, (void *)&cmd, sizeof(cmd),
   1665 		(void *)&vpdbuf, sizeof(vpdbuf),
   1666 		SDRETRIES, 100000, NULL,
   1667 		flags | XS_CTL_DATA_IN | XS_CTL_IGNORE_ILLEGAL_REQUEST))
   1668 		goto end;
   1669 
   1670 	if (vpdbuf.flags & VPD_LBP_LBPU)
   1671 		sd->flags |= SDF_LBPU;
   1672 
   1673 end:
   1674 	return (0);
   1675 }
   1676 
   1677 static int
   1678 sd_get_parms_page4(struct sd_softc *sd, struct disk_parms *dp, int flags)
   1679 {
   1680 	struct sd_mode_sense_data scsipi_sense;
   1681 	int error;
   1682 	int big, byte2;
   1683 	size_t poffset;
   1684 	union scsi_disk_pages *pages;
   1685 
   1686 	byte2 = SMS_DBD;
   1687 again:
   1688 	memset(&scsipi_sense, 0, sizeof(scsipi_sense));
   1689 	error = sd_mode_sense(sd, byte2, &scsipi_sense,
   1690 	    (byte2 ? 0 : sizeof(scsipi_sense.blk_desc)) +
   1691 	    sizeof(scsipi_sense.pages.rigid_geometry), 4,
   1692 	    flags | XS_CTL_SILENT, &big);
   1693 	if (error) {
   1694 		if (byte2 == SMS_DBD) {
   1695 			/* No result; try once more with DBD off */
   1696 			byte2 = 0;
   1697 			goto again;
   1698 		}
   1699 		return (error);
   1700 	}
   1701 
   1702 	if (big) {
   1703 		poffset = sizeof scsipi_sense.header.big;
   1704 		poffset += _2btol(scsipi_sense.header.big.blk_desc_len);
   1705 	} else {
   1706 		poffset = sizeof scsipi_sense.header.small;
   1707 		poffset += scsipi_sense.header.small.blk_desc_len;
   1708 	}
   1709 
   1710 	if (poffset > sizeof(scsipi_sense) - sizeof(pages->rigid_geometry))
   1711 		return ERESTART;
   1712 
   1713 	pages = (void *)((u_long)&scsipi_sense + poffset);
   1714 #if 0
   1715 	{
   1716 		size_t i;
   1717 		u_int8_t *p;
   1718 
   1719 		printf("page 4 sense:");
   1720 		for (i = sizeof(scsipi_sense), p = (void *)&scsipi_sense; i;
   1721 		    i--, p++)
   1722 			printf(" %02x", *p);
   1723 		printf("\n");
   1724 		printf("page 4 pg_code=%d sense=%p/%p\n",
   1725 		    pages->rigid_geometry.pg_code, &scsipi_sense, pages);
   1726 	}
   1727 #endif
   1728 
   1729 	if ((pages->rigid_geometry.pg_code & PGCODE_MASK) != 4)
   1730 		return (ERESTART);
   1731 
   1732 	SC_DEBUG(sd->sc_periph, SCSIPI_DB3,
   1733 	    ("%d cyls, %d heads, %d precomp, %d red_write, %d land_zone\n",
   1734 	    _3btol(pages->rigid_geometry.ncyl),
   1735 	    pages->rigid_geometry.nheads,
   1736 	    _2btol(pages->rigid_geometry.st_cyl_wp),
   1737 	    _2btol(pages->rigid_geometry.st_cyl_rwc),
   1738 	    _2btol(pages->rigid_geometry.land_zone)));
   1739 
   1740 	/*
   1741 	 * KLUDGE!! (for zone recorded disks)
   1742 	 * give a number of sectors so that sec * trks * cyls
   1743 	 * is <= disk_size
   1744 	 * can lead to wasted space! THINK ABOUT THIS !
   1745 	 */
   1746 	dp->heads = pages->rigid_geometry.nheads;
   1747 	dp->cyls = _3btol(pages->rigid_geometry.ncyl);
   1748 	if (dp->heads == 0 || dp->cyls == 0)
   1749 		return (ERESTART);
   1750 	dp->sectors = dp->disksize / (dp->heads * dp->cyls);	/* XXX */
   1751 
   1752 	dp->rot_rate = _2btol(pages->rigid_geometry.rpm);
   1753 	if (dp->rot_rate == 0)
   1754 		dp->rot_rate = 3600;
   1755 
   1756 #if 0
   1757 printf("page 4 ok\n");
   1758 #endif
   1759 	return (0);
   1760 }
   1761 
   1762 static int
   1763 sd_get_parms_page5(struct sd_softc *sd, struct disk_parms *dp, int flags)
   1764 {
   1765 	struct sd_mode_sense_data scsipi_sense;
   1766 	int error;
   1767 	int big, byte2;
   1768 	size_t poffset;
   1769 	union scsi_disk_pages *pages;
   1770 
   1771 	byte2 = SMS_DBD;
   1772 again:
   1773 	memset(&scsipi_sense, 0, sizeof(scsipi_sense));
   1774 	error = sd_mode_sense(sd, 0, &scsipi_sense,
   1775 	    (byte2 ? 0 : sizeof(scsipi_sense.blk_desc)) +
   1776 	    sizeof(scsipi_sense.pages.flex_geometry), 5,
   1777 	    flags | XS_CTL_SILENT, &big);
   1778 	if (error) {
   1779 		if (byte2 == SMS_DBD) {
   1780 			/* No result; try once more with DBD off */
   1781 			byte2 = 0;
   1782 			goto again;
   1783 		}
   1784 		return (error);
   1785 	}
   1786 
   1787 	if (big) {
   1788 		poffset = sizeof scsipi_sense.header.big;
   1789 		poffset += _2btol(scsipi_sense.header.big.blk_desc_len);
   1790 	} else {
   1791 		poffset = sizeof scsipi_sense.header.small;
   1792 		poffset += scsipi_sense.header.small.blk_desc_len;
   1793 	}
   1794 
   1795 	if (poffset > sizeof(scsipi_sense) - sizeof(pages->flex_geometry))
   1796 		return ERESTART;
   1797 
   1798 	pages = (void *)((u_long)&scsipi_sense + poffset);
   1799 #if 0
   1800 	{
   1801 		size_t i;
   1802 		u_int8_t *p;
   1803 
   1804 		printf("page 5 sense:");
   1805 		for (i = sizeof(scsipi_sense), p = (void *)&scsipi_sense; i;
   1806 		    i--, p++)
   1807 			printf(" %02x", *p);
   1808 		printf("\n");
   1809 		printf("page 5 pg_code=%d sense=%p/%p\n",
   1810 		    pages->flex_geometry.pg_code, &scsipi_sense, pages);
   1811 	}
   1812 #endif
   1813 
   1814 	if ((pages->flex_geometry.pg_code & PGCODE_MASK) != 5)
   1815 		return (ERESTART);
   1816 
   1817 	SC_DEBUG(sd->sc_periph, SCSIPI_DB3,
   1818 	    ("%d cyls, %d heads, %d sec, %d bytes/sec\n",
   1819 	    _3btol(pages->flex_geometry.ncyl),
   1820 	    pages->flex_geometry.nheads,
   1821 	    pages->flex_geometry.ph_sec_tr,
   1822 	    _2btol(pages->flex_geometry.bytes_s)));
   1823 
   1824 	dp->heads = pages->flex_geometry.nheads;
   1825 	dp->cyls = _2btol(pages->flex_geometry.ncyl);
   1826 	dp->sectors = pages->flex_geometry.ph_sec_tr;
   1827 	if (dp->heads == 0 || dp->cyls == 0 || dp->sectors == 0)
   1828 		return (ERESTART);
   1829 
   1830 	dp->rot_rate = _2btol(pages->rigid_geometry.rpm);
   1831 	if (dp->rot_rate == 0)
   1832 		dp->rot_rate = 3600;
   1833 
   1834 #if 0
   1835 printf("page 5 ok\n");
   1836 #endif
   1837 	return (0);
   1838 }
   1839 
   1840 static int
   1841 sd_get_parms(struct sd_softc *sd, struct disk_parms *dp, int flags)
   1842 {
   1843 	struct dk_softc *dksc = &sd->sc_dksc;
   1844 	int error;
   1845 
   1846 	/*
   1847 	 * If offline, the SDEV_MEDIA_LOADED flag will be
   1848 	 * cleared by the caller if necessary.
   1849 	 */
   1850 	if (sd->type == T_SIMPLE_DIRECT) {
   1851 		error = sd_get_simplifiedparms(sd, dp, flags);
   1852 		if (!error)
   1853 			goto setprops;
   1854 		return (error);
   1855 	}
   1856 
   1857 	error = sd_get_capacity(sd, dp, flags);
   1858 	if (error)
   1859 		return (error);
   1860 
   1861 	if (sd->type == T_OPTICAL)
   1862 		goto page0;
   1863 
   1864 	if (sd->sc_periph->periph_flags & PERIPH_REMOVABLE) {
   1865 		if (!sd_get_parms_page5(sd, dp, flags) ||
   1866 		    !sd_get_parms_page4(sd, dp, flags))
   1867 			goto setprops;
   1868 	} else {
   1869 		if (!sd_get_parms_page4(sd, dp, flags) ||
   1870 		    !sd_get_parms_page5(sd, dp, flags))
   1871 			goto setprops;
   1872 	}
   1873 
   1874 page0:
   1875 	printf("%s: fabricating a geometry\n", dksc->sc_xname);
   1876 	/* Try calling driver's method for figuring out geometry. */
   1877 	if (!sd->sc_periph->periph_channel->chan_adapter->adapt_getgeom ||
   1878 	    !(*sd->sc_periph->periph_channel->chan_adapter->adapt_getgeom)
   1879 		(sd->sc_periph, dp, dp->disksize)) {
   1880 		/*
   1881 		 * Use adaptec standard fictitious geometry
   1882 		 * this depends on which controller (e.g. 1542C is
   1883 		 * different. but we have to put SOMETHING here..)
   1884 		 */
   1885 		dp->heads = 64;
   1886 		dp->sectors = 32;
   1887 		dp->cyls = dp->disksize / (64 * 32);
   1888 	}
   1889 	dp->rot_rate = 3600;
   1890 
   1891 setprops:
   1892 	sd_set_geometry(sd);
   1893 
   1894 	return (SDGP_RESULT_OK);
   1895 }
   1896 
   1897 static int
   1898 sd_flush(struct sd_softc *sd, int flags)
   1899 {
   1900 	struct scsipi_periph *periph = sd->sc_periph;
   1901 	struct scsi_synchronize_cache_10 cmd;
   1902 
   1903 	/*
   1904 	 * If the device is SCSI-2, issue a SYNCHRONIZE CACHE.
   1905 	 * We issue with address 0 length 0, which should be
   1906 	 * interpreted by the device as "all remaining blocks
   1907 	 * starting at address 0".  We ignore ILLEGAL REQUEST
   1908 	 * in the event that the command is not supported by
   1909 	 * the device, and poll for completion so that we know
   1910 	 * that the cache has actually been flushed.
   1911 	 *
   1912 	 * Unless, that is, the device can't handle the SYNCHRONIZE CACHE
   1913 	 * command, as indicated by our quirks flags.
   1914 	 *
   1915 	 * XXX What about older devices?
   1916 	 */
   1917 	if (periph->periph_version < 2 ||
   1918 	    (periph->periph_quirks & PQUIRK_NOSYNCCACHE))
   1919 		return (0);
   1920 
   1921 	sd->flags |= SDF_FLUSHING;
   1922 	memset(&cmd, 0, sizeof(cmd));
   1923 	cmd.opcode = SCSI_SYNCHRONIZE_CACHE_10;
   1924 
   1925 	return (scsipi_command(periph, (void *)&cmd, sizeof(cmd), 0, 0,
   1926 	    SDRETRIES, 100000, NULL, flags | XS_CTL_IGNORE_ILLEGAL_REQUEST));
   1927 }
   1928 
   1929 static int
   1930 sd_getcache(struct sd_softc *sd, int *bitsp)
   1931 {
   1932 	struct scsipi_periph *periph = sd->sc_periph;
   1933 	struct sd_mode_sense_data scsipi_sense;
   1934 	int error, bits = 0;
   1935 	int big;
   1936 	union scsi_disk_pages *pages;
   1937 	uint8_t dev_spec;
   1938 
   1939 	/* only SCSI-2 and later supported */
   1940 	if (periph->periph_version < 2)
   1941 		return (EOPNOTSUPP);
   1942 
   1943 	memset(&scsipi_sense, 0, sizeof(scsipi_sense));
   1944 	error = sd_mode_sense(sd, SMS_DBD, &scsipi_sense,
   1945 	    sizeof(scsipi_sense.pages.caching_params), 8, XS_CTL_SILENT, &big);
   1946 	if (error)
   1947 		return (error);
   1948 
   1949 	if (big) {
   1950 		pages = (void *)(&scsipi_sense.header.big + 1);
   1951 		dev_spec = scsipi_sense.header.big.dev_spec;
   1952 	} else {
   1953 		pages = (void *)(&scsipi_sense.header.small + 1);
   1954 		dev_spec = scsipi_sense.header.small.dev_spec;
   1955 	}
   1956 
   1957 	if ((pages->caching_params.flags & CACHING_RCD) == 0)
   1958 		bits |= DKCACHE_READ;
   1959 	if (pages->caching_params.flags & CACHING_WCE)
   1960 		bits |= DKCACHE_WRITE;
   1961 	if (pages->caching_params.pg_code & PGCODE_PS)
   1962 		bits |= DKCACHE_SAVE;
   1963 
   1964 	/*
   1965 	 * Support for FUA/DPO, defined starting with SCSI-2. Use only
   1966 	 * if device claims to support it, according to the MODE SENSE.
   1967 	 */
   1968 	if (!(periph->periph_quirks & PQUIRK_NOFUA) &&
   1969 	    ISSET(dev_spec, SMH_DSP_DPOFUA))
   1970 		bits |= DKCACHE_FUA | DKCACHE_DPO;
   1971 
   1972 	memset(&scsipi_sense, 0, sizeof(scsipi_sense));
   1973 	error = sd_mode_sense(sd, SMS_DBD, &scsipi_sense,
   1974 	    sizeof(scsipi_sense.pages.caching_params),
   1975 	    SMS_PCTRL_CHANGEABLE|8, XS_CTL_SILENT, &big);
   1976 	if (error == 0) {
   1977 		if (big)
   1978 			pages = (void *)(&scsipi_sense.header.big + 1);
   1979 		else
   1980 			pages = (void *)(&scsipi_sense.header.small + 1);
   1981 
   1982 		if (pages->caching_params.flags & CACHING_RCD)
   1983 			bits |= DKCACHE_RCHANGE;
   1984 		if (pages->caching_params.flags & CACHING_WCE)
   1985 			bits |= DKCACHE_WCHANGE;
   1986 	}
   1987 
   1988 	*bitsp = bits;
   1989 
   1990 	return (0);
   1991 }
   1992 
   1993 static int
   1994 sd_setcache(struct sd_softc *sd, int bits)
   1995 {
   1996 	struct scsipi_periph *periph = sd->sc_periph;
   1997 	struct sd_mode_sense_data scsipi_sense;
   1998 	int error;
   1999 	uint8_t oflags, byte2 = 0;
   2000 	int big;
   2001 	union scsi_disk_pages *pages;
   2002 
   2003 	if (periph->periph_version < 2)
   2004 		return (EOPNOTSUPP);
   2005 
   2006 	memset(&scsipi_sense, 0, sizeof(scsipi_sense));
   2007 	error = sd_mode_sense(sd, SMS_DBD, &scsipi_sense,
   2008 	    sizeof(scsipi_sense.pages.caching_params), 8, 0, &big);
   2009 	if (error)
   2010 		return (error);
   2011 
   2012 	if (big)
   2013 		pages = (void *)(&scsipi_sense.header.big + 1);
   2014 	else
   2015 		pages = (void *)(&scsipi_sense.header.small + 1);
   2016 
   2017 	oflags = pages->caching_params.flags;
   2018 
   2019 	if (bits & DKCACHE_READ)
   2020 		pages->caching_params.flags &= ~CACHING_RCD;
   2021 	else
   2022 		pages->caching_params.flags |= CACHING_RCD;
   2023 
   2024 	if (bits & DKCACHE_WRITE)
   2025 		pages->caching_params.flags |= CACHING_WCE;
   2026 	else
   2027 		pages->caching_params.flags &= ~CACHING_WCE;
   2028 
   2029 	if (oflags == pages->caching_params.flags)
   2030 		return (0);
   2031 
   2032 	pages->caching_params.pg_code &= PGCODE_MASK;
   2033 
   2034 	if (bits & DKCACHE_SAVE)
   2035 		byte2 |= SMS_SP;
   2036 
   2037 	return (sd_mode_select(sd, byte2|SMS_PF, &scsipi_sense,
   2038 	    sizeof(struct scsi_mode_page_header) +
   2039 	    pages->caching_params.pg_length, 0, big));
   2040 }
   2041 
   2042 static void
   2043 sd_set_geometry(struct sd_softc *sd)
   2044 {
   2045 	struct dk_softc *dksc = &sd->sc_dksc;
   2046 	struct disk_geom *dg = &dksc->sc_dkdev.dk_geom;
   2047 
   2048 	memset(dg, 0, sizeof(*dg));
   2049 
   2050 	dg->dg_secperunit = sd->params.disksize;
   2051 	dg->dg_secsize = sd->params.blksize;
   2052 	dg->dg_nsectors = sd->params.sectors;
   2053 	dg->dg_ntracks = sd->params.heads;
   2054 	dg->dg_ncylinders = sd->params.cyls;
   2055 
   2056 	disk_set_info(dksc->sc_dev, &dksc->sc_dkdev, sd->typename);
   2057 }
   2058 
   2059 static int
   2060 sddiscard(dev_t dev, off_t pos, off_t len)
   2061 {
   2062 	struct sd_softc *sd;
   2063 	int unit;
   2064 
   2065 	unit = SDUNIT(dev);
   2066 	sd = device_lookup_private(&sd_cd, unit);
   2067 
   2068 	return dk_discard(&sd->sc_dksc, dev, pos, len);
   2069 }
   2070 
   2071