Home | History | Annotate | Line # | Download | only in scsipi
sd.c revision 1.18.2.11
      1   1.18.2.2  mycroft /*
      2   1.18.2.2  mycroft  * Written by Julian Elischer (julian (at) dialix.oz.au)
      3        1.8  deraadt  * for TRW Financial Systems for use under the MACH(2.5) operating system.
      4        1.1      cgd  *
      5        1.1      cgd  * TRW Financial Systems, in accordance with their agreement with Carnegie
      6        1.1      cgd  * Mellon University, makes this software available to CMU to distribute
      7        1.1      cgd  * or use in any manner that they see fit as long as this message is kept with
      8        1.1      cgd  * the software. For this reason TFS also grants any other persons or
      9        1.1      cgd  * organisations permission to use or modify this software.
     10        1.1      cgd  *
     11        1.1      cgd  * TFS supplies this software to be publicly redistributed
     12        1.1      cgd  * on the understanding that TFS is not responsible for the correct
     13        1.1      cgd  * functioning of this software in any circumstances.
     14        1.9      cgd  *
     15   1.18.2.2  mycroft  * Ported to run under 386BSD by Julian Elischer (julian (at) dialix.oz.au) Sept 1992
     16   1.18.2.2  mycroft  *
     17  1.18.2.11  mycroft  *      $Id: sd.c,v 1.18.2.11 1994/02/06 10:06:35 mycroft Exp $
     18        1.1      cgd  */
     19        1.1      cgd 
     20   1.18.2.2  mycroft #include <sys/types.h>
     21   1.18.2.2  mycroft #include <sys/param.h>
     22   1.18.2.2  mycroft #include <sys/dkbad.h>
     23   1.18.2.2  mycroft #include <sys/systm.h>
     24   1.18.2.2  mycroft #include <sys/conf.h>
     25   1.18.2.2  mycroft #include <sys/file.h>
     26   1.18.2.2  mycroft #include <sys/stat.h>
     27   1.18.2.2  mycroft #include <sys/ioctl.h>
     28   1.18.2.2  mycroft #include <sys/buf.h>
     29   1.18.2.2  mycroft #include <sys/uio.h>
     30   1.18.2.2  mycroft #include <sys/malloc.h>
     31   1.18.2.2  mycroft #include <sys/errno.h>
     32   1.18.2.2  mycroft #include <sys/device.h>
     33   1.18.2.2  mycroft #include <sys/disklabel.h>
     34   1.18.2.3  mycroft #include <sys/disk.h>
     35   1.18.2.2  mycroft 
     36   1.18.2.2  mycroft #include <scsi/scsi_all.h>
     37   1.18.2.2  mycroft #include <scsi/scsi_disk.h>
     38   1.18.2.2  mycroft #include <scsi/scsiconf.h>
     39   1.18.2.2  mycroft 
     40   1.18.2.2  mycroft #ifdef	DDB
     41   1.18.2.2  mycroft int     Debugger();
     42   1.18.2.2  mycroft #else	/* DDB */
     43   1.18.2.2  mycroft #define Debugger()
     44   1.18.2.2  mycroft #endif	/* DDB */
     45        1.1      cgd 
     46        1.1      cgd #define	SDOUTSTANDING	2
     47   1.18.2.7  mycroft #define	SDRETRIES	4
     48        1.1      cgd 
     49   1.18.2.3  mycroft #define MAKESDDEV(maj, unit, part)	(makedev(maj,(unit<<3)|part))
     50   1.18.2.7  mycroft #define SDPART(z)	(minor(z) & 0x07)
     51   1.18.2.3  mycroft #define SDUNIT(z)	(minor(z) >> 3)
     52        1.1      cgd #define	RAW_PART	3
     53        1.1      cgd 
     54   1.18.2.2  mycroft struct sd_data {
     55   1.18.2.9  mycroft 	struct device sc_dev;
     56   1.18.2.3  mycroft 	struct dkdevice sc_dk;
     57   1.18.2.3  mycroft 
     58   1.18.2.2  mycroft 	u_int32 flags;
     59   1.18.2.2  mycroft #define	SDINIT		0x04	/* device has been init'd */
     60   1.18.2.2  mycroft #define SDHAVELABEL	0x10	/* have read the label */
     61   1.18.2.2  mycroft #define SDDOSPART	0x20	/* Have read the DOS partition table */
     62   1.18.2.2  mycroft #define SDWRITEPROT	0x40	/* Device in readonly mode (S/W) */
     63   1.18.2.2  mycroft 	struct scsi_link *sc_link;	/* contains our targ, lun etc. */
     64   1.18.2.2  mycroft 	u_int32 ad_info;	/* info about the adapter */
     65   1.18.2.2  mycroft 	u_int32 cmdscount;	/* cmds allowed outstanding by board */
     66   1.18.2.2  mycroft 	boolean wlabel;		/* label is writable */
     67   1.18.2.2  mycroft 	struct disk_parms {
     68   1.18.2.7  mycroft 		u_char heads;		/* Number of heads */
     69   1.18.2.7  mycroft 		u_int16 cyls;		/* Number of cylinders */
     70   1.18.2.7  mycroft 		u_char sectors;		/* Number of sectors/track */
     71   1.18.2.7  mycroft 		u_int32 blksize;	/* Number of bytes/sector */
     72   1.18.2.7  mycroft 		u_long disksize;	/* total number sectors */
     73   1.18.2.2  mycroft 	} params;
     74   1.18.2.2  mycroft 	u_int32 partflags[MAXPARTITIONS];	/* per partition flags */
     75   1.18.2.2  mycroft #define SDOPEN	0x01
     76   1.18.2.2  mycroft 	u_int32 openparts;		/* one bit for each open partition */
     77   1.18.2.2  mycroft 	u_int32 xfer_block_wait;
     78   1.18.2.7  mycroft 	struct buf buf_queue;
     79   1.18.2.4  mycroft };
     80   1.18.2.4  mycroft 
     81   1.18.2.4  mycroft void sdattach __P((struct device *, struct device *, void *));
     82   1.18.2.2  mycroft 
     83   1.18.2.7  mycroft struct cfdriver sdcd =
     84   1.18.2.4  mycroft { NULL, "sd", scsi_targmatch, sdattach, DV_DISK, sizeof(struct sd_data) };
     85   1.18.2.4  mycroft 
     86   1.18.2.4  mycroft int sdgetdisklabel __P((struct sd_data *));
     87   1.18.2.4  mycroft int sd_get_parms __P((struct sd_data *, int));
     88   1.18.2.6  mycroft void sdstrategy __P((struct buf *));
     89   1.18.2.6  mycroft void sdstart __P((int));
     90   1.18.2.6  mycroft 
     91   1.18.2.6  mycroft struct dkdriver sddkdriver = { sdstrategy };
     92   1.18.2.6  mycroft 
     93   1.18.2.6  mycroft struct scsi_device sd_switch =
     94   1.18.2.6  mycroft {
     95  1.18.2.10  mycroft 	NULL,			/* Use default error handler */
     96  1.18.2.10  mycroft 	sdstart,		/* have a queue, served by this */
     97  1.18.2.10  mycroft 	NULL,			/* have no async handler */
     98  1.18.2.10  mycroft 	NULL,			/* Use default 'done' routine */
     99  1.18.2.10  mycroft 	"sd",
    100  1.18.2.10  mycroft 	0
    101   1.18.2.6  mycroft };
    102        1.1      cgd 
    103        1.3  deraadt /*
    104        1.3  deraadt  * The routine called by the low level scsi routine when it discovers
    105   1.18.2.2  mycroft  * a device suitable for this driver.
    106        1.3  deraadt  */
    107   1.18.2.4  mycroft void
    108   1.18.2.4  mycroft sdattach(parent, self, aux)
    109   1.18.2.4  mycroft 	struct device *parent, *self;
    110   1.18.2.4  mycroft 	void *aux;
    111        1.1      cgd {
    112   1.18.2.5  mycroft 	struct sd_data *sd = (struct sd_data *)self;
    113   1.18.2.5  mycroft 	struct disk_parms *dp = &sd->params;
    114   1.18.2.4  mycroft 	struct scsi_link *sc_link = aux;
    115        1.3  deraadt 
    116   1.18.2.2  mycroft 	SC_DEBUG(sc_link, SDEV_DB2, ("sdattach: "));
    117        1.3  deraadt 
    118   1.18.2.2  mycroft 	/*
    119   1.18.2.2  mycroft 	 * Store information needed to contact our base driver
    120   1.18.2.2  mycroft 	 */
    121   1.18.2.2  mycroft 	sd->sc_link = sc_link;
    122   1.18.2.2  mycroft 	sc_link->device = &sd_switch;
    123   1.18.2.4  mycroft 	sc_link->dev_unit = self->dv_unit;
    124   1.18.2.2  mycroft 
    125   1.18.2.6  mycroft 	sd->sc_dk.dk_driver = &sddkdriver;
    126   1.18.2.9  mycroft 	dk_establish(&sd->sc_dk, &sd->sc_dev);
    127   1.18.2.6  mycroft 
    128   1.18.2.2  mycroft 	if (sd->sc_link->adapter->adapter_info) {
    129   1.18.2.2  mycroft 		sd->ad_info = ((*(sd->sc_link->adapter->adapter_info)) (sc_link->adapter_softc));
    130   1.18.2.2  mycroft 		sd->cmdscount = sd->ad_info & AD_INF_MAX_CMDS;
    131   1.18.2.7  mycroft 		if (sd->cmdscount > SDOUTSTANDING)
    132        1.1      cgd 			sd->cmdscount = SDOUTSTANDING;
    133        1.3  deraadt 	} else {
    134        1.1      cgd 		sd->ad_info = 1;
    135   1.18.2.2  mycroft 		sd->cmdscount = 1;
    136   1.18.2.2  mycroft 	}
    137   1.18.2.2  mycroft 	sc_link->opennings = sd->cmdscount;
    138   1.18.2.7  mycroft 
    139        1.3  deraadt 	/*
    140        1.3  deraadt 	 * Use the subdriver to request information regarding
    141        1.3  deraadt 	 * the drive. We cannot use interrupts yet, so the
    142        1.3  deraadt 	 * request must specify this.
    143        1.3  deraadt 	 */
    144   1.18.2.4  mycroft 	sd_get_parms(sd, SCSI_NOSLEEP | SCSI_NOMASK);
    145   1.18.2.6  mycroft 	printf(": %dMB, %d cyl, %d head, %d sec, %d bytes/sec\n",
    146   1.18.2.7  mycroft 	       dp->disksize / ((1024L * 1024L) / dp->blksize),
    147   1.18.2.7  mycroft 	       dp->cyls, dp->heads, dp->sectors, dp->blksize);
    148        1.1      cgd 	sd->flags |= SDINIT;
    149        1.1      cgd }
    150        1.1      cgd 
    151        1.3  deraadt /*
    152   1.18.2.2  mycroft  * open the device. Make sure the partition info is a up-to-date as can be.
    153        1.3  deraadt  */
    154        1.3  deraadt int
    155   1.18.2.2  mycroft sdopen(dev)
    156   1.18.2.4  mycroft 	dev_t dev;
    157        1.1      cgd {
    158   1.18.2.7  mycroft 	int error = 0;
    159   1.18.2.4  mycroft 	int unit, part;
    160        1.3  deraadt 	struct sd_data *sd;
    161   1.18.2.2  mycroft 	struct scsi_link *sc_link;
    162        1.1      cgd 
    163   1.18.2.3  mycroft 	unit = SDUNIT(dev);
    164   1.18.2.3  mycroft 	part = SDPART(dev);
    165   1.18.2.3  mycroft 
    166   1.18.2.4  mycroft 	if (unit >= sdcd.cd_ndevs)
    167   1.18.2.3  mycroft 		return ENXIO;
    168   1.18.2.4  mycroft 	sd = sdcd.cd_devs[unit];
    169   1.18.2.2  mycroft 	/*
    170   1.18.2.2  mycroft 	 * Make sure the disk has been initialised
    171   1.18.2.2  mycroft 	 * At some point in the future, get the scsi driver
    172   1.18.2.2  mycroft 	 * to look for a new device if we are not initted
    173   1.18.2.2  mycroft 	 */
    174   1.18.2.4  mycroft 	if (!sd || !(sd->flags & SDINIT))
    175   1.18.2.3  mycroft 		return ENXIO;
    176   1.18.2.3  mycroft 
    177   1.18.2.2  mycroft 	sc_link = sd->sc_link;
    178        1.3  deraadt 
    179   1.18.2.2  mycroft 	SC_DEBUG(sc_link, SDEV_DB1,
    180   1.18.2.2  mycroft 	    ("sdopen: dev=0x%x (unit %d (of %d),partition %d)\n"
    181   1.18.2.4  mycroft 		,dev, unit, sdcd.cd_ndevs, part));
    182        1.3  deraadt 
    183        1.3  deraadt 	/*
    184   1.18.2.2  mycroft 	 * If it's been invalidated, then forget the label
    185        1.3  deraadt 	 */
    186   1.18.2.2  mycroft 	if (!(sc_link->flags & SDEV_MEDIA_LOADED)) {
    187   1.18.2.2  mycroft 		sd->flags &= ~SDHAVELABEL;
    188        1.3  deraadt 
    189   1.18.2.2  mycroft 		/*
    190   1.18.2.7  mycroft 		 * If somebody still has it open, then forbid re-entry.
    191   1.18.2.2  mycroft 		 */
    192   1.18.2.7  mycroft 		if (sd->openparts)
    193   1.18.2.7  mycroft 			return ENXIO;
    194   1.18.2.2  mycroft 	}
    195   1.18.2.4  mycroft 
    196        1.3  deraadt 	/*
    197   1.18.2.7  mycroft 	 * "unit attention" errors should occur here if the
    198   1.18.2.7  mycroft 	 * drive has been restarted or the pack changed.
    199   1.18.2.7  mycroft 	 * just ingnore the result, it's a decoy instruction
    200   1.18.2.7  mycroft 	 * The error code will act on the error though
    201   1.18.2.7  mycroft 	 * and invalidate any media information we had.
    202   1.18.2.7  mycroft 	 */
    203   1.18.2.7  mycroft 	scsi_test_unit_ready(sc_link, SCSI_SILENT);
    204   1.18.2.7  mycroft 
    205   1.18.2.7  mycroft 	/*
    206   1.18.2.2  mycroft 	 * In case it is a funny one, tell it to start
    207   1.18.2.7  mycroft 	 * not needed for most hard drives (ignore failure)
    208        1.3  deraadt 	 */
    209   1.18.2.2  mycroft 	scsi_start_unit(sc_link, SCSI_ERR_OK | SCSI_SILENT);
    210        1.3  deraadt 
    211        1.3  deraadt 	/*
    212   1.18.2.2  mycroft 	 * Check that it is still responding and ok.
    213   1.18.2.2  mycroft 	 */
    214   1.18.2.7  mycroft 	sc_link->flags |= SDEV_OPEN;	/* unit attn becomes an err now */
    215   1.18.2.2  mycroft 	if (scsi_test_unit_ready(sc_link, 0)) {
    216   1.18.2.2  mycroft 		SC_DEBUG(sc_link, SDEV_DB3, ("device not reponding\n"));
    217   1.18.2.7  mycroft 		error = ENXIO;
    218   1.18.2.2  mycroft 		goto bad;
    219       1.11   davidb 	}
    220   1.18.2.2  mycroft 	SC_DEBUG(sc_link, SDEV_DB3, ("device ok\n"));
    221        1.3  deraadt 
    222   1.18.2.7  mycroft 	/* Lock the pack in. */
    223   1.18.2.7  mycroft 	scsi_prevent(sc_link, PR_PREVENT, SCSI_ERR_OK | SCSI_SILENT);
    224   1.18.2.7  mycroft 
    225        1.3  deraadt 	/*
    226        1.3  deraadt 	 * Load the physical device parameters
    227        1.3  deraadt 	 */
    228   1.18.2.7  mycroft 	if (sd_get_parms(sd, 0)) {
    229   1.18.2.7  mycroft 		error = ENXIO;
    230   1.18.2.2  mycroft 		goto bad;
    231        1.1      cgd 	}
    232   1.18.2.2  mycroft 	SC_DEBUG(sc_link, SDEV_DB3, ("Params loaded "));
    233   1.18.2.2  mycroft 
    234        1.3  deraadt 	/*
    235   1.18.2.2  mycroft 	 * Load the partition info if not already loaded.
    236        1.3  deraadt 	 */
    237   1.18.2.7  mycroft 	if ((error = sdgetdisklabel(sd)) && (part != RAW_PART))
    238   1.18.2.2  mycroft 		goto bad;
    239   1.18.2.2  mycroft 	SC_DEBUG(sc_link, SDEV_DB3, ("Disklabel loaded "));
    240   1.18.2.4  mycroft 
    241        1.3  deraadt 	/*
    242        1.3  deraadt 	 * Check the partition is legal
    243        1.3  deraadt 	 */
    244   1.18.2.7  mycroft 	if (part >= sd->sc_dk.dk_label.d_npartitions &&
    245   1.18.2.7  mycroft 	    part != RAW_PART) {
    246   1.18.2.7  mycroft 		error = ENXIO;
    247   1.18.2.2  mycroft 		goto bad;
    248        1.1      cgd 	}
    249   1.18.2.2  mycroft 	SC_DEBUG(sc_link, SDEV_DB3, ("partition ok"));
    250        1.3  deraadt 
    251        1.3  deraadt 	/*
    252        1.3  deraadt 	 *  Check that the partition exists
    253        1.3  deraadt 	 */
    254   1.18.2.7  mycroft 	if (sd->sc_dk.dk_label.d_partitions[part].p_fstype == FS_UNUSED &&
    255   1.18.2.7  mycroft 	    part != RAW_PART) {
    256   1.18.2.7  mycroft 		error = ENXIO;
    257   1.18.2.2  mycroft 		goto bad;
    258        1.1      cgd 	}
    259        1.1      cgd 	sd->partflags[part] |= SDOPEN;
    260        1.1      cgd 	sd->openparts |= (1 << part);
    261   1.18.2.7  mycroft 	SC_DEBUG(sc_link, SDEV_DB3, ("open complete\n"));
    262   1.18.2.7  mycroft 	sc_link->flags |= SDEV_MEDIA_LOADED;
    263        1.3  deraadt 	return 0;
    264        1.1      cgd 
    265   1.18.2.2  mycroft bad:
    266   1.18.2.7  mycroft 	if (!sd->openparts) {
    267   1.18.2.2  mycroft 		scsi_prevent(sc_link, PR_ALLOW, SCSI_ERR_OK | SCSI_SILENT);
    268   1.18.2.2  mycroft 		sc_link->flags &= ~SDEV_OPEN;
    269        1.1      cgd 	}
    270   1.18.2.7  mycroft 	return error;
    271        1.1      cgd }
    272        1.1      cgd 
    273        1.3  deraadt /*
    274   1.18.2.2  mycroft  * close the device.. only called if we are the LAST occurence of an open
    275   1.18.2.2  mycroft  * device.  Convenient now but usually a pain.
    276        1.3  deraadt  */
    277   1.18.2.2  mycroft int
    278   1.18.2.2  mycroft sdclose(dev)
    279   1.18.2.4  mycroft 	dev_t dev;
    280        1.1      cgd {
    281   1.18.2.4  mycroft 	int unit, part;
    282   1.18.2.2  mycroft 	struct sd_data *sd;
    283   1.18.2.2  mycroft 
    284   1.18.2.3  mycroft 	unit = SDUNIT(dev);
    285   1.18.2.3  mycroft 	part = SDPART(dev);
    286   1.18.2.4  mycroft 	sd = sdcd.cd_devs[unit];
    287   1.18.2.2  mycroft 	sd->partflags[part] &= ~SDOPEN;
    288   1.18.2.2  mycroft 	sd->openparts &= ~(1 << part);
    289   1.18.2.7  mycroft 	if (!sd->openparts) {
    290   1.18.2.7  mycroft 		scsi_prevent(sd->sc_link, PR_ALLOW, SCSI_ERR_OK | SCSI_SILENT);
    291   1.18.2.2  mycroft 		sd->sc_link->flags &= ~SDEV_OPEN;
    292   1.18.2.4  mycroft 	}
    293   1.18.2.2  mycroft 	return 0;
    294        1.1      cgd }
    295        1.1      cgd 
    296        1.3  deraadt /*
    297        1.3  deraadt  * trim the size of the transfer if needed, called by physio
    298        1.3  deraadt  * basically the smaller of our max and the scsi driver's
    299        1.3  deraadt  * minphys (note we have no max)
    300   1.18.2.2  mycroft  *
    301   1.18.2.2  mycroft  * Trim buffer length if buffer-size is bigger than page size
    302        1.3  deraadt  */
    303   1.18.2.2  mycroft void
    304   1.18.2.2  mycroft sdminphys(bp)
    305   1.18.2.2  mycroft 	struct buf *bp;
    306        1.3  deraadt {
    307   1.18.2.4  mycroft 	register struct sd_data *sd = sdcd.cd_devs[SDUNIT(bp->b_dev)];
    308   1.18.2.4  mycroft 
    309   1.18.2.4  mycroft 	(sd->sc_link->adapter->scsi_minphys) (bp);
    310        1.3  deraadt }
    311        1.1      cgd 
    312        1.3  deraadt /*
    313   1.18.2.2  mycroft  * Actually translate the requested transfer into one the physical driver
    314   1.18.2.2  mycroft  * can understand.  The transfer is described by a buf and will include
    315        1.3  deraadt  * only one physical transfer.
    316        1.3  deraadt  */
    317   1.18.2.2  mycroft void
    318   1.18.2.2  mycroft sdstrategy(bp)
    319   1.18.2.2  mycroft 	struct buf *bp;
    320        1.1      cgd {
    321   1.18.2.2  mycroft 	struct buf *dp;
    322   1.18.2.4  mycroft 	int opri;
    323        1.3  deraadt 	struct sd_data *sd;
    324   1.18.2.4  mycroft 	int unit;
    325        1.1      cgd 
    326   1.18.2.4  mycroft 	unit = SDUNIT(bp->b_dev);
    327   1.18.2.4  mycroft 	sd = sdcd.cd_devs[unit];
    328   1.18.2.2  mycroft 	SC_DEBUG(sd->sc_link, SDEV_DB2, ("sdstrategy "));
    329   1.18.2.2  mycroft 	SC_DEBUG(sd->sc_link, SDEV_DB1,
    330   1.18.2.2  mycroft 	    (" %d bytes @ blk%d\n", bp->b_bcount, bp->b_blkno));
    331        1.1      cgd 	sdminphys(bp);
    332   1.18.2.2  mycroft 	/*
    333   1.18.2.2  mycroft 	 * If the device has been made invalid, error out
    334   1.18.2.2  mycroft 	 */
    335   1.18.2.2  mycroft 	if (!(sd->sc_link->flags & SDEV_MEDIA_LOADED)) {
    336   1.18.2.2  mycroft 		sd->flags &= ~SDHAVELABEL;
    337        1.1      cgd 		bp->b_error = EIO;
    338        1.1      cgd 		goto bad;
    339        1.1      cgd 	}
    340   1.18.2.2  mycroft 	/*
    341   1.18.2.2  mycroft 	 * "soft" write protect check
    342   1.18.2.2  mycroft 	 */
    343        1.1      cgd 	if ((sd->flags & SDWRITEPROT) && (bp->b_flags & B_READ) == 0) {
    344        1.1      cgd 		bp->b_error = EROFS;
    345        1.1      cgd 		goto bad;
    346        1.1      cgd 	}
    347   1.18.2.2  mycroft 	/*
    348   1.18.2.2  mycroft 	 * If it's a null transfer, return immediatly
    349   1.18.2.2  mycroft 	 */
    350   1.18.2.7  mycroft 	if (bp->b_bcount == 0)
    351        1.1      cgd 		goto done;
    352        1.3  deraadt 	/*
    353        1.3  deraadt 	 * Decide which unit and partition we are talking about
    354        1.3  deraadt 	 * only raw is ok if no label
    355        1.3  deraadt 	 */
    356   1.18.2.3  mycroft 	if (SDPART(bp->b_dev) != RAW_PART) {
    357        1.3  deraadt 		if (!(sd->flags & SDHAVELABEL)) {
    358        1.1      cgd 			bp->b_error = EIO;
    359        1.1      cgd 			goto bad;
    360        1.1      cgd 		}
    361        1.1      cgd 		/*
    362        1.1      cgd 		 * do bounds checking, adjust transfer. if error, process.
    363        1.1      cgd 		 * if end of partition, just return
    364        1.1      cgd 		 */
    365   1.18.2.3  mycroft 		if (bounds_check_with_label(bp, &sd->sc_dk.dk_label, sd->wlabel) <= 0)
    366        1.1      cgd 			goto done;
    367        1.1      cgd 		/* otherwise, process transfer request */
    368        1.1      cgd 	}
    369   1.18.2.2  mycroft 	opri = splbio();
    370   1.18.2.2  mycroft 	dp = &sd->buf_queue;
    371        1.1      cgd 
    372   1.18.2.2  mycroft 	/*
    373   1.18.2.2  mycroft 	 * Place it in the queue of disk activities for this disk
    374   1.18.2.2  mycroft 	 */
    375        1.1      cgd 	disksort(dp, bp);
    376        1.1      cgd 
    377        1.3  deraadt 	/*
    378        1.3  deraadt 	 * Tell the device to get going on the transfer if it's
    379        1.3  deraadt 	 * not doing anything, otherwise just wait for completion
    380        1.3  deraadt 	 */
    381        1.1      cgd 	sdstart(unit);
    382        1.1      cgd 
    383        1.1      cgd 	splx(opri);
    384   1.18.2.6  mycroft 	return;
    385   1.18.2.7  mycroft 
    386        1.1      cgd bad:
    387        1.1      cgd 	bp->b_flags |= B_ERROR;
    388        1.1      cgd done:
    389   1.18.2.2  mycroft 
    390   1.18.2.2  mycroft 	/*
    391   1.18.2.2  mycroft 	 * Correctly set the buf to indicate a completed xfer
    392   1.18.2.2  mycroft 	 */
    393   1.18.2.2  mycroft 	bp->b_resid = bp->b_bcount;
    394        1.1      cgd 	biodone(bp);
    395        1.1      cgd }
    396        1.1      cgd 
    397        1.3  deraadt /*
    398        1.3  deraadt  * sdstart looks to see if there is a buf waiting for the device
    399        1.3  deraadt  * and that the device is not already busy. If both are true,
    400   1.18.2.2  mycroft  * It dequeues the buf and creates a scsi command to perform the
    401   1.18.2.2  mycroft  * transfer in the buf. The transfer request will call scsi_done
    402        1.3  deraadt  * on completion, which will in turn call this routine again
    403        1.3  deraadt  * so that the next queued transfer is performed.
    404        1.3  deraadt  * The bufs are queued by the strategy routine (sdstrategy)
    405   1.18.2.2  mycroft  *
    406        1.3  deraadt  * This routine is also called after other non-queued requests
    407        1.3  deraadt  * have been made of the scsi driver, to ensure that the queue
    408        1.3  deraadt  * continues to be drained.
    409   1.18.2.2  mycroft  *
    410        1.3  deraadt  * must be called at the correct (highish) spl level
    411   1.18.2.2  mycroft  * sdstart() is called at splbio from sdstrategy and scsi_done
    412        1.3  deraadt  */
    413   1.18.2.2  mycroft void
    414   1.18.2.2  mycroft sdstart(unit)
    415   1.18.2.4  mycroft 	int unit;
    416   1.18.2.2  mycroft {
    417   1.18.2.4  mycroft 	register struct sd_data *sd = sdcd.cd_devs[unit];
    418   1.18.2.2  mycroft 	register struct	scsi_link *sc_link = sd->sc_link;
    419   1.18.2.2  mycroft 	struct buf *bp = 0;
    420   1.18.2.2  mycroft 	struct buf *dp;
    421        1.3  deraadt 	struct scsi_rw_big cmd;
    422   1.18.2.7  mycroft 	int blkno, nblks;
    423        1.3  deraadt 	struct partition *p;
    424        1.3  deraadt 
    425   1.18.2.7  mycroft 	SC_DEBUG(sc_link, SDEV_DB2, ("sdstart%d ", unit));
    426        1.3  deraadt 	/*
    427   1.18.2.2  mycroft 	 * Check if the device has room for another command
    428        1.3  deraadt 	 */
    429   1.18.2.2  mycroft 	while (sc_link->opennings) {
    430   1.18.2.2  mycroft 		/*
    431   1.18.2.2  mycroft 		 * there is excess capacity, but a special waits
    432   1.18.2.2  mycroft 		 * It'll need the adapter as soon as we clear out of the
    433   1.18.2.2  mycroft 		 * way and let it run (user level wait).
    434   1.18.2.2  mycroft 		 */
    435   1.18.2.6  mycroft 		if (sc_link->flags & SDEV_WAITING)
    436   1.18.2.2  mycroft 			return;
    437   1.18.2.6  mycroft 
    438   1.18.2.2  mycroft 		/*
    439   1.18.2.2  mycroft 		 * See if there is a buf with work for us to do..
    440   1.18.2.2  mycroft 		 */
    441   1.18.2.2  mycroft 		dp = &sd->buf_queue;
    442   1.18.2.6  mycroft 		if ((bp = dp->b_actf) == NULL)	/* yes, an assign */
    443   1.18.2.2  mycroft 			return;
    444  1.18.2.11  mycroft 		dp->b_actf = bp->b_actf;
    445        1.1      cgd 
    446   1.18.2.2  mycroft 		/*
    447   1.18.2.6  mycroft 		 * If the device has become invalid, abort all the
    448   1.18.2.2  mycroft 		 * reads and writes until all files have been closed and
    449   1.18.2.2  mycroft 		 * re-openned
    450   1.18.2.2  mycroft 		 */
    451   1.18.2.2  mycroft 		if (!(sc_link->flags & SDEV_MEDIA_LOADED)) {
    452   1.18.2.2  mycroft 			sd->flags &= ~SDHAVELABEL;
    453   1.18.2.2  mycroft 			goto bad;
    454   1.18.2.2  mycroft 		}
    455   1.18.2.6  mycroft 
    456   1.18.2.2  mycroft 		/*
    457   1.18.2.2  mycroft 		 * We have a buf, now we know we are going to go through
    458   1.18.2.2  mycroft 		 * With this thing..
    459   1.18.2.2  mycroft 		 *
    460   1.18.2.2  mycroft 		 *  First, translate the block to absolute
    461   1.18.2.2  mycroft 		 */
    462   1.18.2.7  mycroft 		blkno = bp->b_blkno / (sd->params.blksize / DEV_BSIZE);
    463   1.18.2.7  mycroft 		if (SDPART(bp->b_dev) != RAW_PART) {
    464   1.18.2.7  mycroft 			p = &sd->sc_dk.dk_label.d_partitions[SDPART(bp->b_dev)];
    465   1.18.2.7  mycroft 			blkno += p->p_offset;
    466   1.18.2.7  mycroft 		}
    467   1.18.2.8  mycroft 		nblks = (bp->b_bcount + (sd->params.blksize - 1)) / (sd->params.blksize);
    468        1.1      cgd 
    469   1.18.2.2  mycroft 		/*
    470   1.18.2.2  mycroft 		 *  Fill out the scsi command
    471   1.18.2.2  mycroft 		 */
    472   1.18.2.2  mycroft 		bzero(&cmd, sizeof(cmd));
    473   1.18.2.7  mycroft 		cmd.op_code = (bp->b_flags & B_READ) ? READ_BIG : WRITE_BIG;
    474   1.18.2.2  mycroft 		cmd.addr_3 = (blkno & 0xff000000) >> 24;
    475   1.18.2.2  mycroft 		cmd.addr_2 = (blkno & 0xff0000) >> 16;
    476   1.18.2.2  mycroft 		cmd.addr_1 = (blkno & 0xff00) >> 8;
    477   1.18.2.2  mycroft 		cmd.addr_0 = blkno & 0xff;
    478   1.18.2.7  mycroft 		cmd.length2 = (nblks & 0xff00) >> 8;
    479   1.18.2.7  mycroft 		cmd.length1 = (nblks & 0xff);
    480   1.18.2.6  mycroft 
    481   1.18.2.2  mycroft 		/*
    482   1.18.2.2  mycroft 		 * Call the routine that chats with the adapter.
    483   1.18.2.2  mycroft 		 * Note: we cannot sleep as we may be an interrupt
    484   1.18.2.2  mycroft 		 */
    485   1.18.2.6  mycroft 		if (scsi_scsi_cmd(sc_link, (struct scsi_generic *) &cmd,
    486   1.18.2.6  mycroft 				  sizeof(cmd), (u_char *) bp->b_un.b_addr,
    487   1.18.2.7  mycroft 				  bp->b_bcount, SDRETRIES, 10000, bp,
    488   1.18.2.6  mycroft 				  SCSI_NOSLEEP | ((bp->b_flags & B_READ) ?
    489   1.18.2.6  mycroft 			    	  SCSI_DATA_IN : SCSI_DATA_OUT))
    490   1.18.2.6  mycroft 		    != SUCCESSFULLY_QUEUED) {
    491   1.18.2.2  mycroft bad:
    492   1.18.2.9  mycroft 			printf("%s: not queued", sd->sc_dev.dv_xname);
    493        1.1      cgd 			bp->b_error = EIO;
    494   1.18.2.2  mycroft 			bp->b_flags |= B_ERROR;
    495   1.18.2.2  mycroft 			biodone(bp);
    496        1.3  deraadt 		}
    497   1.18.2.2  mycroft 	}
    498        1.1      cgd }
    499        1.3  deraadt 
    500        1.3  deraadt /*
    501        1.3  deraadt  * Perform special action on behalf of the user
    502        1.3  deraadt  * Knows about the internals of this device
    503        1.3  deraadt  */
    504   1.18.2.2  mycroft int
    505   1.18.2.7  mycroft sdioctl(dev, cmd, addr, flag)
    506   1.18.2.7  mycroft 	dev_t dev;
    507   1.18.2.7  mycroft 	int cmd;
    508   1.18.2.7  mycroft 	caddr_t addr;
    509   1.18.2.7  mycroft 	int flag;
    510        1.1      cgd {
    511   1.18.2.7  mycroft 	int error = 0;
    512   1.18.2.7  mycroft 	int unit, part;
    513   1.18.2.2  mycroft 	register struct sd_data *sd;
    514        1.1      cgd 
    515   1.18.2.2  mycroft 	/*
    516   1.18.2.2  mycroft 	 * Find the device that the user is talking about
    517   1.18.2.2  mycroft 	 */
    518   1.18.2.3  mycroft 	unit = SDUNIT(dev);
    519   1.18.2.3  mycroft 	part = SDPART(dev);
    520   1.18.2.4  mycroft 	sd = sdcd.cd_devs[unit];
    521   1.18.2.2  mycroft 	SC_DEBUG(sd->sc_link, SDEV_DB1, ("sdioctl (0x%x)", cmd));
    522        1.3  deraadt 
    523   1.18.2.2  mycroft 	/*
    524   1.18.2.2  mycroft 	 * If the device is not valid.. abandon ship
    525   1.18.2.2  mycroft 	 */
    526   1.18.2.2  mycroft 	if (!(sd->sc_link->flags & SDEV_MEDIA_LOADED))
    527   1.18.2.3  mycroft 		return EIO;
    528   1.18.2.2  mycroft 	switch (cmd) {
    529        1.3  deraadt 
    530        1.1      cgd 	case DIOCSBAD:
    531   1.18.2.7  mycroft 		return EINVAL;
    532   1.18.2.2  mycroft 
    533        1.1      cgd 	case DIOCGDINFO:
    534   1.18.2.3  mycroft 		*(struct disklabel *) addr = sd->sc_dk.dk_label;
    535   1.18.2.7  mycroft 		return 0;
    536   1.18.2.2  mycroft 
    537        1.3  deraadt 	case DIOCGPART:
    538   1.18.2.3  mycroft 		((struct partinfo *) addr)->disklab = &sd->sc_dk.dk_label;
    539   1.18.2.2  mycroft 		((struct partinfo *) addr)->part =
    540   1.18.2.3  mycroft 		    &sd->sc_dk.dk_label.d_partitions[SDPART(dev)];
    541   1.18.2.7  mycroft 		return 0;
    542   1.18.2.2  mycroft 
    543        1.3  deraadt 	case DIOCSDINFO:
    544        1.3  deraadt 		if ((flag & FWRITE) == 0)
    545   1.18.2.7  mycroft 			return EBADF;
    546   1.18.2.7  mycroft 		error = setdisklabel(&sd->sc_dk.dk_label,
    547   1.18.2.7  mycroft 				     (struct disklabel *)addr,
    548   1.18.2.2  mycroft 			/*(sd->flags & DKFL_BSDLABEL) ? sd->openparts : */ 0,
    549   1.18.2.7  mycroft 				     &sd->sc_dk.dk_cpulabel);
    550   1.18.2.7  mycroft 		if (!error)
    551        1.1      cgd 			sd->flags |= SDHAVELABEL;
    552   1.18.2.7  mycroft 		return error;
    553   1.18.2.2  mycroft 
    554        1.3  deraadt 	case DIOCWLABEL:
    555        1.1      cgd 		sd->flags &= ~SDWRITEPROT;
    556        1.3  deraadt 		if ((flag & FWRITE) == 0)
    557   1.18.2.7  mycroft 			return EBADF;
    558   1.18.2.7  mycroft 		sd->wlabel = *(boolean *) addr;
    559   1.18.2.7  mycroft 		return 0;
    560   1.18.2.2  mycroft 
    561        1.3  deraadt 	case DIOCWDINFO:
    562        1.1      cgd 		sd->flags &= ~SDWRITEPROT;
    563        1.3  deraadt 		if ((flag & FWRITE) == 0)
    564   1.18.2.7  mycroft 			return EBADF;
    565   1.18.2.7  mycroft 		error = setdisklabel(&sd->sc_dk.dk_label,
    566   1.18.2.7  mycroft 				     (struct disklabel *)addr,
    567   1.18.2.7  mycroft 		    /*(sd->flags & SDHAVELABEL) ? sd->openparts : */ 0,
    568   1.18.2.7  mycroft 		    		     &sd->sc_dk.dk_cpulabel);
    569   1.18.2.7  mycroft 		if (error)
    570   1.18.2.7  mycroft 			return error;
    571        1.1      cgd 
    572   1.18.2.7  mycroft 		{
    573   1.18.2.7  mycroft 			boolean wlab;
    574        1.1      cgd 
    575   1.18.2.7  mycroft 			/* ok - write will succeed */
    576   1.18.2.7  mycroft 			sd->flags |= SDHAVELABEL;
    577   1.18.2.7  mycroft 
    578   1.18.2.7  mycroft 			/* simulate opening partition 0 so write succeeds */
    579   1.18.2.7  mycroft 			sd->openparts |= (1 << 0);	/* XXX */
    580   1.18.2.7  mycroft 			wlab = sd->wlabel;
    581   1.18.2.7  mycroft 			sd->wlabel = 1;
    582   1.18.2.7  mycroft 			error = writedisklabel(dev, sdstrategy,
    583   1.18.2.7  mycroft 			    &sd->sc_dk.dk_label,
    584   1.18.2.7  mycroft 			    &sd->sc_dk.dk_cpulabel);
    585   1.18.2.7  mycroft 			sd->wlabel = wlab;
    586   1.18.2.7  mycroft 			return error;
    587   1.18.2.7  mycroft 		}
    588   1.18.2.2  mycroft 
    589        1.1      cgd 	default:
    590   1.18.2.7  mycroft 		if (part != RAW_PART)
    591   1.18.2.7  mycroft 			return ENOTTY;
    592   1.18.2.7  mycroft 		return scsi_do_ioctl(sd->sc_link, cmd, addr, flag);
    593   1.18.2.7  mycroft 	}
    594   1.18.2.7  mycroft #ifdef DIAGNOSTIC
    595   1.18.2.7  mycroft 	panic("sdioctl: impossible");
    596   1.18.2.7  mycroft #endif
    597        1.1      cgd }
    598        1.1      cgd 
    599        1.3  deraadt /*
    600        1.3  deraadt  * Load the label information on the named device
    601        1.3  deraadt  */
    602   1.18.2.2  mycroft int
    603   1.18.2.4  mycroft sdgetdisklabel(sd)
    604   1.18.2.4  mycroft 	struct sd_data *sd;
    605        1.1      cgd {
    606   1.18.2.7  mycroft 	char *errstring;
    607   1.18.2.2  mycroft 
    608   1.18.2.2  mycroft 	/*
    609   1.18.2.2  mycroft 	 * If the inflo is already loaded, use it
    610   1.18.2.2  mycroft 	 */
    611   1.18.2.2  mycroft 	if (sd->flags & SDHAVELABEL)
    612   1.18.2.3  mycroft 		return 0;
    613        1.3  deraadt 
    614   1.18.2.3  mycroft 	bzero(&sd->sc_dk.dk_label, sizeof(struct disklabel));
    615   1.18.2.6  mycroft 	bzero(&sd->sc_dk.dk_cpulabel, sizeof(struct cpu_disklabel));
    616        1.3  deraadt 	/*
    617   1.18.2.2  mycroft 	 * make partition 3 the whole disk in case of failure then get pdinfo
    618   1.18.2.2  mycroft 	 * for historical reasons, make part a same as raw part
    619        1.3  deraadt 	 */
    620   1.18.2.3  mycroft 	sd->sc_dk.dk_label.d_partitions[0].p_offset = 0;
    621   1.18.2.7  mycroft 	sd->sc_dk.dk_label.d_partitions[0].p_size
    622   1.18.2.7  mycroft 	    = sd->params.disksize * (sd->params.blksize / DEV_BSIZE);
    623   1.18.2.7  mycroft 	sd->sc_dk.dk_label.d_partitions[0].p_fstype = 9;	/* XXXX */
    624   1.18.2.3  mycroft 	sd->sc_dk.dk_label.d_partitions[RAW_PART].p_offset = 0;
    625   1.18.2.7  mycroft 	sd->sc_dk.dk_label.d_partitions[RAW_PART].p_size
    626   1.18.2.7  mycroft 	    = sd->params.disksize * (sd->params.blksize / DEV_BSIZE);
    627   1.18.2.3  mycroft 	sd->sc_dk.dk_label.d_npartitions = MAXPARTITIONS;
    628   1.18.2.7  mycroft 
    629   1.18.2.7  mycroft 	sd->sc_dk.dk_label.d_secsize = sd->params.blksize;
    630   1.18.2.3  mycroft 	sd->sc_dk.dk_label.d_ntracks = sd->params.heads;
    631   1.18.2.3  mycroft 	sd->sc_dk.dk_label.d_nsectors = sd->params.sectors;
    632   1.18.2.3  mycroft 	sd->sc_dk.dk_label.d_ncylinders = sd->params.cyls;
    633   1.18.2.3  mycroft 	sd->sc_dk.dk_label.d_secpercyl = sd->params.heads * sd->params.sectors;
    634   1.18.2.3  mycroft 	if (sd->sc_dk.dk_label.d_secpercyl == 0) {
    635   1.18.2.3  mycroft 		sd->sc_dk.dk_label.d_secpercyl = 100;
    636   1.18.2.2  mycroft 		/* as long as it's not 0 - readdisklabel divides by it (?) */
    637        1.1      cgd 	}
    638   1.18.2.7  mycroft 
    639   1.18.2.2  mycroft 	/*
    640   1.18.2.2  mycroft 	 * Call the generic disklabel extraction routine
    641   1.18.2.2  mycroft 	 */
    642   1.18.2.9  mycroft 	if (errstring = readdisklabel(MAKESDDEV(0, sd->sc_dev.dv_unit,
    643   1.18.2.4  mycroft 				      RAW_PART), sdstrategy,
    644   1.18.2.4  mycroft 	    			      &sd->sc_dk.dk_label,
    645   1.18.2.4  mycroft 				      &sd->sc_dk.dk_cpulabel)) {
    646   1.18.2.9  mycroft 		printf("%s: %s\n", sd->sc_dev.dv_xname, errstring);
    647        1.3  deraadt 		return ENXIO;
    648        1.1      cgd 	}
    649        1.3  deraadt 	sd->flags |= SDHAVELABEL;	/* WE HAVE IT ALL NOW */
    650   1.18.2.2  mycroft 	return 0;
    651        1.1      cgd }
    652        1.1      cgd 
    653        1.3  deraadt /*
    654        1.3  deraadt  * Find out from the device what it's capacity is
    655        1.3  deraadt  */
    656   1.18.2.2  mycroft u_int32
    657   1.18.2.4  mycroft sd_size(sd, flags)
    658   1.18.2.4  mycroft 	struct sd_data *sd;
    659   1.18.2.4  mycroft 	int flags;
    660        1.1      cgd {
    661        1.1      cgd 	struct scsi_read_cap_data rdcap;
    662        1.1      cgd 	struct scsi_read_capacity scsi_cmd;
    663   1.18.2.2  mycroft 	u_int32 size;
    664        1.1      cgd 
    665        1.3  deraadt 	/*
    666        1.3  deraadt 	 * make up a scsi command and ask the scsi driver to do
    667        1.3  deraadt 	 * it for you.
    668        1.3  deraadt 	 */
    669        1.1      cgd 	bzero(&scsi_cmd, sizeof(scsi_cmd));
    670        1.1      cgd 	scsi_cmd.op_code = READ_CAPACITY;
    671        1.1      cgd 
    672        1.3  deraadt 	/*
    673        1.3  deraadt 	 * If the command works, interpret the result as a 4 byte
    674        1.3  deraadt 	 * number of blocks
    675        1.3  deraadt 	 */
    676   1.18.2.4  mycroft 	if (scsi_scsi_cmd(sd->sc_link, (struct scsi_generic *) &scsi_cmd,
    677   1.18.2.4  mycroft 			  sizeof(scsi_cmd), (u_char *) &rdcap, sizeof(rdcap),
    678   1.18.2.7  mycroft 			  SDRETRIES, 2000, NULL, flags | SCSI_DATA_IN) != 0) {
    679   1.18.2.9  mycroft 		printf("%s: could not get size\n", sd->sc_dev.dv_xname);
    680   1.18.2.3  mycroft 		return 0;
    681        1.1      cgd 	} else {
    682   1.18.2.2  mycroft 		size = rdcap.addr_0 + 1;
    683        1.1      cgd 		size += rdcap.addr_1 << 8;
    684        1.1      cgd 		size += rdcap.addr_2 << 16;
    685        1.1      cgd 		size += rdcap.addr_3 << 24;
    686        1.1      cgd 	}
    687   1.18.2.3  mycroft 	return size;
    688        1.3  deraadt }
    689        1.3  deraadt 
    690        1.3  deraadt /*
    691        1.3  deraadt  * Tell the device to map out a defective block
    692        1.3  deraadt  */
    693        1.3  deraadt int
    694   1.18.2.4  mycroft sd_reassign_blocks(sd, block)
    695   1.18.2.4  mycroft 	struct sd_data *sd;
    696   1.18.2.4  mycroft 	int block;
    697        1.1      cgd {
    698        1.3  deraadt 	struct scsi_reassign_blocks scsi_cmd;
    699   1.18.2.2  mycroft 	struct scsi_reassign_blocks_data rbdata;
    700        1.1      cgd 
    701        1.1      cgd 	bzero(&scsi_cmd, sizeof(scsi_cmd));
    702        1.1      cgd 	bzero(&rbdata, sizeof(rbdata));
    703        1.1      cgd 	scsi_cmd.op_code = REASSIGN_BLOCKS;
    704        1.1      cgd 
    705        1.1      cgd 	rbdata.length_msb = 0;
    706        1.1      cgd 	rbdata.length_lsb = sizeof(rbdata.defect_descriptor[0]);
    707        1.1      cgd 	rbdata.defect_descriptor[0].dlbaddr_3 = ((block >> 24) & 0xff);
    708        1.1      cgd 	rbdata.defect_descriptor[0].dlbaddr_2 = ((block >> 16) & 0xff);
    709   1.18.2.2  mycroft 	rbdata.defect_descriptor[0].dlbaddr_1 = ((block >> 8) & 0xff);
    710   1.18.2.2  mycroft 	rbdata.defect_descriptor[0].dlbaddr_0 = ((block) & 0xff);
    711        1.1      cgd 
    712   1.18.2.4  mycroft 	return scsi_scsi_cmd(sd->sc_link, (struct scsi_generic *) &scsi_cmd,
    713   1.18.2.4  mycroft 			     sizeof(scsi_cmd), (u_char *) &rbdata,
    714   1.18.2.7  mycroft 			     sizeof(rbdata), SDRETRIES, 5000, NULL,
    715   1.18.2.4  mycroft 			     SCSI_DATA_OUT);
    716        1.1      cgd }
    717   1.18.2.4  mycroft 
    718        1.1      cgd #define b2tol(a)	(((unsigned)(a##_1) << 8) + (unsigned)a##_0 )
    719        1.1      cgd 
    720        1.3  deraadt /*
    721   1.18.2.7  mycroft  * Get the scsi driver to send a full inquiry to the * device and use the
    722   1.18.2.7  mycroft  * results to fill out the disk parameter structure.
    723        1.3  deraadt  */
    724   1.18.2.2  mycroft int
    725   1.18.2.4  mycroft sd_get_parms(sd, flags)
    726   1.18.2.4  mycroft 	struct sd_data *sd;
    727   1.18.2.4  mycroft 	int flags;
    728        1.1      cgd {
    729        1.1      cgd 	struct disk_parms *disk_parms = &sd->params;
    730   1.18.2.2  mycroft 	struct scsi_mode_sense scsi_cmd;
    731        1.3  deraadt 	struct scsi_mode_sense_data {
    732   1.18.2.2  mycroft 		struct scsi_mode_header header;
    733   1.18.2.2  mycroft 		struct blk_desc blk_desc;
    734   1.18.2.2  mycroft 		union disk_pages pages;
    735        1.3  deraadt 	} scsi_sense;
    736   1.18.2.2  mycroft 	u_int32 sectors;
    737        1.1      cgd 
    738   1.18.2.2  mycroft 	/*
    739   1.18.2.2  mycroft 	 * First check if we have it all loaded
    740   1.18.2.2  mycroft 	 */
    741   1.18.2.7  mycroft 	if (sd->sc_link->flags & SDEV_MEDIA_LOADED)
    742        1.3  deraadt 		return 0;
    743        1.3  deraadt 
    744   1.18.2.2  mycroft 	/*
    745   1.18.2.2  mycroft 	 * do a "mode sense page 4"
    746   1.18.2.2  mycroft 	 */
    747        1.1      cgd 	bzero(&scsi_cmd, sizeof(scsi_cmd));
    748        1.1      cgd 	scsi_cmd.op_code = MODE_SENSE;
    749   1.18.2.2  mycroft 	scsi_cmd.page = 4;
    750        1.1      cgd 	scsi_cmd.length = 0x20;
    751        1.3  deraadt 	/*
    752        1.3  deraadt 	 * If the command worked, use the results to fill out
    753        1.3  deraadt 	 * the parameter structure
    754        1.3  deraadt 	 */
    755   1.18.2.4  mycroft 	if (scsi_scsi_cmd(sd->sc_link, (struct scsi_generic *) &scsi_cmd,
    756   1.18.2.4  mycroft 			  sizeof(scsi_cmd), (u_char *) &scsi_sense,
    757   1.18.2.7  mycroft 			  sizeof(scsi_sense), SDRETRIES, 2000, NULL,
    758   1.18.2.4  mycroft 			  flags | SCSI_DATA_IN) != 0) {
    759   1.18.2.2  mycroft 
    760   1.18.2.9  mycroft 		printf("%s: could not mode sense", sd->sc_dev.dv_xname);
    761   1.18.2.4  mycroft 		printf(" (4); using ficticious geometry\n");
    762   1.18.2.2  mycroft 		/*
    763   1.18.2.2  mycroft 		 * use adaptec standard ficticious geometry
    764   1.18.2.2  mycroft 		 * this depends on which controller (e.g. 1542C is
    765   1.18.2.2  mycroft 		 * different. but we have to put SOMETHING here..)
    766   1.18.2.2  mycroft 		 */
    767   1.18.2.4  mycroft 		sectors = sd_size(sd, flags);
    768        1.1      cgd 		disk_parms->heads = 64;
    769        1.1      cgd 		disk_parms->sectors = 32;
    770   1.18.2.2  mycroft 		disk_parms->cyls = sectors / (64 * 32);
    771   1.18.2.7  mycroft 		disk_parms->blksize = 512;
    772   1.18.2.2  mycroft 		disk_parms->disksize = sectors;
    773        1.3  deraadt 	} else {
    774   1.18.2.2  mycroft 
    775   1.18.2.2  mycroft 		SC_DEBUG(sd->sc_link, SDEV_DB3,
    776   1.18.2.2  mycroft 		    ("%d cyls, %d heads, %d precomp, %d red_write, %d land_zone\n",
    777   1.18.2.2  mycroft 			_3btol(&scsi_sense.pages.rigid_geometry.ncyl_2),
    778        1.1      cgd 			scsi_sense.pages.rigid_geometry.nheads,
    779        1.1      cgd 			b2tol(scsi_sense.pages.rigid_geometry.st_cyl_wp),
    780        1.1      cgd 			b2tol(scsi_sense.pages.rigid_geometry.st_cyl_rwc),
    781   1.18.2.2  mycroft 			b2tol(scsi_sense.pages.rigid_geometry.land_zone)));
    782        1.1      cgd 
    783        1.3  deraadt 		/*
    784        1.3  deraadt 		 * KLUDGE!!(for zone recorded disks)
    785        1.3  deraadt 		 * give a number of sectors so that sec * trks * cyls
    786   1.18.2.2  mycroft 		 * is <= disk_size
    787   1.18.2.2  mycroft 		 * can lead to wasted space! THINK ABOUT THIS !
    788        1.3  deraadt 		 */
    789        1.1      cgd 		disk_parms->heads = scsi_sense.pages.rigid_geometry.nheads;
    790   1.18.2.2  mycroft 		disk_parms->cyls = _3btol(&scsi_sense.pages.rigid_geometry.ncyl_2);
    791   1.18.2.7  mycroft 		disk_parms->blksize = _3btol(scsi_sense.blk_desc.blklen);
    792        1.1      cgd 
    793   1.18.2.4  mycroft 		sectors = sd_size(sd, flags);
    794   1.18.2.2  mycroft 		disk_parms->disksize = sectors;
    795   1.18.2.2  mycroft 		sectors /= (disk_parms->heads * disk_parms->cyls);
    796   1.18.2.2  mycroft 		disk_parms->sectors = sectors;	/* dubious on SCSI *//*XXX */
    797        1.1      cgd 	}
    798   1.18.2.2  mycroft 	sd->sc_link->flags |= SDEV_MEDIA_LOADED;
    799        1.3  deraadt 	return 0;
    800        1.1      cgd }
    801        1.1      cgd 
    802        1.3  deraadt int
    803   1.18.2.2  mycroft sdsize(dev_t dev)
    804        1.1      cgd {
    805   1.18.2.3  mycroft 	int unit = SDUNIT(dev), part = SDPART(dev), val;
    806        1.3  deraadt 	struct sd_data *sd;
    807        1.1      cgd 
    808   1.18.2.4  mycroft 	if (unit >= sdcd.cd_ndevs)
    809   1.18.2.2  mycroft 		return -1;
    810   1.18.2.4  mycroft 	sd = sdcd.cd_devs[unit];
    811   1.18.2.4  mycroft 	if (!sd || !(sd->flags & SDINIT))
    812   1.18.2.2  mycroft 		return -1;
    813   1.18.2.4  mycroft 
    814   1.18.2.4  mycroft 	if ((sd->flags & SDHAVELABEL) == 0) {
    815   1.18.2.2  mycroft 		val = sdopen(MAKESDDEV(major(dev), unit, RAW_PART), FREAD, S_IFBLK, 0);
    816   1.18.2.2  mycroft 		if (val != 0)
    817   1.18.2.2  mycroft 			return -1;
    818   1.18.2.2  mycroft 	}
    819   1.18.2.7  mycroft 	if (sd->flags & SDWRITEPROT)
    820   1.18.2.7  mycroft 		return -1;
    821   1.18.2.6  mycroft 	return sd->sc_dk.dk_label.d_partitions[part].p_size;
    822        1.3  deraadt }
    823        1.3  deraadt 
    824        1.3  deraadt 
    825   1.18.2.2  mycroft #define SCSIDUMP 1
    826   1.18.2.2  mycroft #undef	SCSIDUMP
    827   1.18.2.2  mycroft #define NOT_TRUSTED 1
    828        1.3  deraadt 
    829   1.18.2.2  mycroft #ifdef SCSIDUMP
    830   1.18.2.2  mycroft #include <vm/vm.h>
    831   1.18.2.2  mycroft 
    832   1.18.2.2  mycroft static struct scsi_xfer sx;
    833   1.18.2.2  mycroft #define	MAXTRANSFER 8		/* 1 page at a time */
    834        1.3  deraadt 
    835        1.3  deraadt /*
    836   1.18.2.2  mycroft  * dump all of physical memory into the partition specified, starting
    837   1.18.2.2  mycroft  * at offset 'dumplo' into the partition.
    838        1.3  deraadt  */
    839        1.3  deraadt int
    840   1.18.2.2  mycroft sddump(dev_t dev)
    841   1.18.2.2  mycroft {				/* dump core after a system crash */
    842   1.18.2.2  mycroft 	register struct sd_data *sd;	/* disk unit to do the IO */
    843   1.18.2.2  mycroft 	int32	num;		/* number of sectors to write */
    844   1.18.2.2  mycroft 	u_int32	unit, part;
    845   1.18.2.2  mycroft 	int32	blkoff, blknum, blkcnt = MAXTRANSFER;
    846   1.18.2.2  mycroft 	int32	nblocks;
    847   1.18.2.2  mycroft 	char	*addr;
    848   1.18.2.2  mycroft 	struct	scsi_rw_big cmd;
    849   1.18.2.2  mycroft 	extern	int Maxmem;
    850   1.18.2.2  mycroft 	static	int sddoingadump = 0;
    851   1.18.2.2  mycroft #define MAPTO CADDR1
    852   1.18.2.2  mycroft 	extern	caddr_t MAPTO;	/* map the page we are about to write, here */
    853   1.18.2.2  mycroft 	struct	scsi_xfer *xs = &sx;
    854   1.18.2.2  mycroft 	int	retval;
    855   1.18.2.2  mycroft 	int	c;
    856   1.18.2.2  mycroft 
    857   1.18.2.2  mycroft 	addr = (char *) 0;	/* starting address */
    858   1.18.2.2  mycroft 
    859   1.18.2.2  mycroft 	/* toss any characters present prior to dump */
    860   1.18.2.2  mycroft 	while ((c = sgetc(1)) && (c != 0x100)); /*syscons and pccons differ */
    861   1.18.2.2  mycroft 
    862   1.18.2.2  mycroft 	/* size of memory to dump */
    863   1.18.2.2  mycroft 	num = Maxmem;
    864   1.18.2.3  mycroft 	unit = SDUNIT(dev);	/* eventually support floppies? */
    865   1.18.2.3  mycroft 	part = SDPART(dev);	/* file system */
    866   1.18.2.2  mycroft 	/* check for acceptable drive number */
    867   1.18.2.4  mycroft 	if (unit >= sdcd.cd_ndevs)
    868   1.18.2.3  mycroft 		return ENXIO;
    869        1.3  deraadt 
    870        1.3  deraadt 	sd = sd_data[unit];
    871   1.18.2.2  mycroft 	if (!sd)
    872   1.18.2.3  mycroft 		return ENXIO;
    873   1.18.2.2  mycroft 	/* was it ever initialized etc. ? */
    874   1.18.2.2  mycroft 	if (!(sd->flags & SDINIT))
    875   1.18.2.3  mycroft 		return ENXIO;
    876   1.18.2.2  mycroft 	if (sd->sc_link->flags & SDEV_MEDIA_LOADED != SDEV_MEDIA_LOADED)
    877   1.18.2.3  mycroft 		return ENXIO;
    878   1.18.2.2  mycroft 	if (sd->flags & SDWRITEPROT)
    879   1.18.2.3  mycroft 		return ENXIO;
    880   1.18.2.2  mycroft 
    881   1.18.2.2  mycroft 	/* Convert to disk sectors */
    882   1.18.2.3  mycroft 	num = (u_int32) num * NBPG / sd->sc_dk.dk_label.d_secsize;
    883   1.18.2.2  mycroft 
    884   1.18.2.2  mycroft 	/* check if controller active */
    885   1.18.2.2  mycroft 	if (sddoingadump)
    886   1.18.2.3  mycroft 		return EFAULT;
    887   1.18.2.2  mycroft 
    888   1.18.2.3  mycroft 	nblocks = sd->sc_dk.dk_label.d_partitions[part].p_size;
    889   1.18.2.3  mycroft 	blkoff = sd->sc_dk.dk_label.d_partitions[part].p_offset;
    890   1.18.2.2  mycroft 
    891   1.18.2.2  mycroft 	/* check transfer bounds against partition size */
    892   1.18.2.2  mycroft 	if ((dumplo < 0) || ((dumplo + num) > nblocks))
    893   1.18.2.3  mycroft 		return EINVAL;
    894   1.18.2.2  mycroft 
    895   1.18.2.2  mycroft 	sddoingadump = 1;
    896   1.18.2.2  mycroft 
    897   1.18.2.2  mycroft 	blknum = dumplo + blkoff;
    898   1.18.2.2  mycroft 	/* blkcnt = initialise_me; */
    899   1.18.2.2  mycroft 	while (num > 0) {
    900   1.18.2.2  mycroft 		pmap_enter(kernel_pmap,
    901   1.18.2.2  mycroft 		    MAPTO,
    902   1.18.2.2  mycroft 		    trunc_page(addr),
    903   1.18.2.2  mycroft 		    VM_PROT_READ,
    904   1.18.2.2  mycroft 		    TRUE);
    905   1.18.2.2  mycroft #ifndef	NOT_TRUSTED
    906   1.18.2.2  mycroft 		/*
    907   1.18.2.2  mycroft 		 *  Fill out the scsi command
    908   1.18.2.2  mycroft 		 */
    909   1.18.2.2  mycroft 		bzero(&cmd, sizeof(cmd));
    910   1.18.2.2  mycroft 		cmd.op_code = WRITE_BIG;
    911   1.18.2.2  mycroft 		cmd.addr_3 = (blknum & 0xff000000) >> 24;
    912   1.18.2.2  mycroft 		cmd.addr_2 = (blknum & 0xff0000) >> 16;
    913   1.18.2.2  mycroft 		cmd.addr_1 = (blknum & 0xff00) >> 8;
    914   1.18.2.2  mycroft 		cmd.addr_0 = blknum & 0xff;
    915   1.18.2.2  mycroft 		cmd.length2 = (blkcnt & 0xff00) >> 8;
    916   1.18.2.2  mycroft 		cmd.length1 = (blkcnt & 0xff);
    917   1.18.2.2  mycroft 		/*
    918   1.18.2.2  mycroft 		 * Fill out the scsi_xfer structure
    919   1.18.2.2  mycroft 		 *    Note: we cannot sleep as we may be an interrupt
    920   1.18.2.2  mycroft 		 * don't use scsi_scsi_cmd() as it may want
    921   1.18.2.2  mycroft 		 * to wait for an xs.
    922   1.18.2.2  mycroft 		 */
    923   1.18.2.2  mycroft 		bzero(xs, sizeof(sx));
    924   1.18.2.2  mycroft 		xs->flags |= SCSI_NOMASK | SCSI_NOSLEEP | INUSE;
    925   1.18.2.2  mycroft 		xs->sc_link = sd->sc_link;
    926   1.18.2.7  mycroft 		xs->retries = SDRETRIES;
    927   1.18.2.2  mycroft 		xs->timeout = 10000;	/* 10000 millisecs for a disk ! */
    928   1.18.2.2  mycroft 		xs->cmd = (struct scsi_generic *) &cmd;
    929   1.18.2.2  mycroft 		xs->cmdlen = sizeof(cmd);
    930   1.18.2.2  mycroft 		xs->resid = blkcnt * 512;
    931   1.18.2.2  mycroft 		xs->error = XS_NOERROR;
    932   1.18.2.2  mycroft 		xs->bp = 0;
    933   1.18.2.2  mycroft 		xs->data = (u_char *) MAPTO;
    934   1.18.2.2  mycroft 		xs->datalen = blkcnt * 512;
    935        1.1      cgd 
    936   1.18.2.2  mycroft 		/*
    937   1.18.2.2  mycroft 		 * Pass all this info to the scsi driver.
    938   1.18.2.2  mycroft 		 */
    939   1.18.2.2  mycroft 		retval = (*(sd->sc_link->adapter->scsi_cmd)) (xs);
    940   1.18.2.2  mycroft 		switch (retval) {
    941   1.18.2.2  mycroft 		case SUCCESSFULLY_QUEUED:
    942   1.18.2.2  mycroft 		case HAD_ERROR:
    943   1.18.2.3  mycroft 			return ENXIO;		/* we said not to sleep! */
    944   1.18.2.2  mycroft 		case COMPLETE:
    945   1.18.2.2  mycroft 			break;
    946   1.18.2.2  mycroft 		default:
    947   1.18.2.3  mycroft 			return ENXIO;		/* we said not to sleep! */
    948   1.18.2.2  mycroft 		}
    949   1.18.2.2  mycroft #else	/* NOT_TRUSTED */
    950   1.18.2.2  mycroft 		/* lets just talk about this first... */
    951   1.18.2.2  mycroft 		printf("sd%d: dump addr 0x%x, blk %d\n", unit, addr, blknum);
    952   1.18.2.2  mycroft #endif	/* NOT_TRUSTED */
    953   1.18.2.2  mycroft 
    954   1.18.2.2  mycroft 		if ((unsigned) addr % (1024 * 1024) == 0)
    955   1.18.2.2  mycroft 			printf("%d ", num / 2048);
    956   1.18.2.2  mycroft 		/* update block count */
    957   1.18.2.2  mycroft 		num -= blkcnt;
    958   1.18.2.2  mycroft 		blknum += blkcnt;
    959   1.18.2.2  mycroft 		(int) addr += 512 * blkcnt;
    960   1.18.2.2  mycroft 
    961   1.18.2.2  mycroft 		/* operator aborting dump? */
    962   1.18.2.2  mycroft 		if ((c = sgetc(1)) && (c != 0x100))
    963   1.18.2.3  mycroft 			return EINTR;
    964   1.18.2.2  mycroft 	}
    965   1.18.2.3  mycroft 	return 0;
    966        1.1      cgd }
    967   1.18.2.2  mycroft #else	/* SCSIDUMP */
    968   1.18.2.2  mycroft int
    969        1.1      cgd sddump()
    970        1.1      cgd {
    971   1.18.2.2  mycroft 	printf("\nsddump()        -- not implemented\n");
    972   1.18.2.2  mycroft 	DELAY(60000000);	/* 60 seconds */
    973        1.3  deraadt 	return -1;
    974        1.1      cgd }
    975   1.18.2.2  mycroft #endif	/* SCSIDUMP */
    976