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