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