Home | History | Annotate | Line # | Download | only in scsipi
sd.c revision 1.19
      1   1.1      cgd /*
      2   1.1      cgd  * Written by Julian Elischer (julian (at) tfs.com)
      3   1.8  deraadt  * for TRW Financial Systems for use under the MACH(2.5) operating system.
      4   1.3  deraadt  * Hacked by Theo de Raadt <deraadt (at) fsa.ca>
      5   1.1      cgd  *
      6   1.1      cgd  * TRW Financial Systems, in accordance with their agreement with Carnegie
      7   1.1      cgd  * Mellon University, makes this software available to CMU to distribute
      8   1.1      cgd  * or use in any manner that they see fit as long as this message is kept with
      9   1.1      cgd  * the software. For this reason TFS also grants any other persons or
     10   1.1      cgd  * organisations permission to use or modify this software.
     11   1.1      cgd  *
     12   1.1      cgd  * TFS supplies this software to be publicly redistributed
     13   1.1      cgd  * on the understanding that TFS is not responsible for the correct
     14   1.1      cgd  * functioning of this software in any circumstances.
     15   1.9      cgd  *
     16  1.19  mycroft  *	$Id: sd.c,v 1.19 1993/12/17 08:50:56 mycroft Exp $
     17   1.1      cgd  */
     18   1.1      cgd 
     19   1.3  deraadt #include "sd.h"
     20   1.3  deraadt 
     21  1.19  mycroft #include <sys/types.h>
     22  1.19  mycroft #include <sys/param.h>
     23  1.19  mycroft #include <sys/dkbad.h>
     24  1.19  mycroft #include <sys/systm.h>
     25  1.19  mycroft #include <sys/conf.h>
     26  1.19  mycroft #include <sys/proc.h>
     27  1.19  mycroft #include <sys/file.h>
     28  1.19  mycroft #include <sys/stat.h>
     29  1.19  mycroft #include <sys/ioctl.h>
     30  1.19  mycroft #include <sys/buf.h>
     31  1.19  mycroft #include <sys/uio.h>
     32  1.19  mycroft #include <sys/malloc.h>
     33  1.19  mycroft #include <sys/errno.h>
     34  1.19  mycroft #include <sys/disklabel.h>
     35  1.19  mycroft 
     36  1.19  mycroft #include <scsi/scsi_all.h>
     37  1.19  mycroft #include <scsi/scsi_disk.h>
     38  1.19  mycroft #include <scsi/scsiconf.h>
     39  1.19  mycroft #include <scsi/sddefs.h>
     40   1.3  deraadt 
     41   1.3  deraadt long int sdstrats, sdqueues;
     42   1.1      cgd 
     43   1.1      cgd #define SPLSD splbio
     44   1.1      cgd #define ESUCCESS 0
     45   1.1      cgd 
     46   1.3  deraadt #define SECSIZE		512
     47   1.1      cgd #define PDLOCATION	29
     48   1.1      cgd #define BOOTRECORDSIGNATURE			(0x55aa & 0x00ff)
     49   1.1      cgd #define	SDOUTSTANDING	2
     50   1.1      cgd #define SDQSIZE		4
     51   1.1      cgd #define	SD_RETRIES	4
     52   1.1      cgd 
     53   1.3  deraadt #define MAKESDDEV(maj, unit, part)	(makedev(maj, ((unit<<3)+part)))
     54   1.1      cgd #define	UNITSHIFT	3
     55   1.1      cgd #define PARTITION(z)	(minor(z) & 0x07)
     56   1.1      cgd #define	RAW_PART	3
     57   1.1      cgd #define UNIT(z)		(  (minor(z) >> UNITSHIFT) )
     58  1.13  deraadt 
     59  1.13  deraadt #undef	NSD
     60  1.14  deraadt #define NSD		( makedev(1,0) >> UNITSHIFT)
     61   1.1      cgd 
     62   1.1      cgd #define WHOLE_DISK(unit) ( (unit << UNITSHIFT) + RAW_PART )
     63   1.1      cgd 
     64   1.3  deraadt struct sd_data *sd_data[NSD];
     65   1.3  deraadt int sd_debug = 0;
     66   1.1      cgd 
     67   1.3  deraadt /*
     68   1.3  deraadt  * The routine called by the low level scsi routine when it discovers
     69   1.3  deraadt  * A device suitable for this driver
     70   1.3  deraadt  */
     71   1.3  deraadt int
     72   1.8  deraadt sdattach(int masunit, struct scsi_switch *sw, int physid, int *unit)
     73   1.1      cgd {
     74   1.3  deraadt 	struct scsi_xfer *sd_scsi_xfer;
     75   1.3  deraadt 	struct disk_parms *dp;
     76   1.3  deraadt 	struct sd_data *sd;
     77   1.1      cgd 	unsigned char *tbl;
     78   1.3  deraadt 	long int ad_info;
     79   1.3  deraadt 	int targ, lun, i;
     80   1.3  deraadt 
     81   1.3  deraadt 	targ = physid >> 3;
     82   1.3  deraadt 	lun = physid & 7;
     83   1.3  deraadt 
     84   1.3  deraadt 	/*printf("sdattach: sd%d at %s%d target %d lun %d\n",
     85   1.8  deraadt 		*unit, sw->name, masunit, targ, lun);*/
     86   1.3  deraadt 
     87   1.8  deraadt 	if(*unit == -1) {
     88   1.8  deraadt 		for(i=0; i<NSD && *unit==-1; i++)
     89  1.17      cgd 			if(sd_data[i]==NULL)
     90   1.8  deraadt 				*unit = i;
     91   1.8  deraadt 	}
     92   1.8  deraadt 	if(*unit > NSD || *unit==-1)
     93   1.8  deraadt 		return 0;
     94   1.8  deraadt 	if(sd_data[*unit])
     95   1.8  deraadt 		return 0;
     96   1.3  deraadt 
     97   1.8  deraadt 	sd = sd_data[*unit] = (struct sd_data *)malloc(sizeof *sd,
     98   1.3  deraadt 		M_TEMP, M_NOWAIT);
     99   1.3  deraadt 	if(!sd)
    100   1.8  deraadt 		return 0;
    101   1.3  deraadt 	bzero(sd, sizeof *sd);
    102   1.3  deraadt 
    103   1.3  deraadt 	/* store information needed to contact our base driver */
    104   1.3  deraadt 	sd->sc_sw = sw;
    105   1.3  deraadt 	sd->ctlr = masunit;
    106   1.3  deraadt 	sd->targ = targ;
    107   1.3  deraadt 	sd->lu = lun;
    108   1.3  deraadt 
    109   1.3  deraadt 	dp = &(sd->params);
    110   1.3  deraadt 	if(scsi_debug & PRINTROUTINES)
    111   1.3  deraadt 		printf("sdattach: ");
    112   1.1      cgd 
    113   1.3  deraadt 	if(sd->sc_sw->adapter_info) {
    114   1.3  deraadt 		sd->ad_info = ( (*(sd->sc_sw->adapter_info))(masunit));
    115   1.1      cgd 		sd->cmdscount =	sd->ad_info & AD_INF_MAX_CMDS;
    116   1.1      cgd 		if(sd->cmdscount > SDOUTSTANDING)
    117   1.1      cgd 			sd->cmdscount = SDOUTSTANDING;
    118   1.3  deraadt 	} else {
    119   1.1      cgd 		sd->ad_info = 1;
    120   1.1      cgd 		sd->cmdscount =	1;
    121   1.1      cgd 	}
    122   1.1      cgd 
    123   1.1      cgd 	i = sd->cmdscount;
    124   1.3  deraadt 	sd_scsi_xfer = (struct scsi_xfer *)malloc(sizeof(struct scsi_xfer) * i,
    125   1.3  deraadt 		M_TEMP, M_NOWAIT);
    126   1.3  deraadt 	while(i--) {
    127   1.3  deraadt 		sd_scsi_xfer->next = sd->freexfer;
    128   1.3  deraadt 		sd->freexfer = sd_scsi_xfer;
    129   1.1      cgd 		sd_scsi_xfer++;
    130   1.1      cgd 	}
    131   1.3  deraadt 
    132   1.3  deraadt 	/*
    133   1.3  deraadt 	 * Use the subdriver to request information regarding
    134   1.3  deraadt 	 * the drive. We cannot use interrupts yet, so the
    135   1.3  deraadt 	 * request must specify this.
    136   1.3  deraadt 	 */
    137   1.8  deraadt 	sd_get_parms(*unit,  SCSI_NOSLEEP |  SCSI_NOMASK);
    138   1.7  deraadt 	printf("sd%d at %s%d targ %d lun %d: %dMB %d cyl, %d head, %d sec, %d byte/sec\n",
    139   1.8  deraadt 		*unit, sw->name, masunit, targ, lun,
    140   1.3  deraadt 		(dp->cyls*dp->heads*dp->sectors*dp->secsiz)/ (1024*1024),
    141   1.3  deraadt 		dp->cyls, dp->heads, dp->sectors, dp->secsiz);
    142   1.3  deraadt 
    143   1.1      cgd 	sd->flags |= SDINIT;
    144   1.8  deraadt 	return 1;
    145   1.1      cgd }
    146   1.1      cgd 
    147   1.1      cgd 
    148   1.3  deraadt /*
    149   1.3  deraadt  * open the device. Make sure the partition info
    150   1.3  deraadt  * is a up-to-date as can be.
    151   1.3  deraadt  */
    152   1.3  deraadt int
    153   1.3  deraadt sdopen(int dev)
    154   1.1      cgd {
    155   1.3  deraadt 	struct disk_parms disk_parms;
    156   1.3  deraadt 	struct sd_data *sd;
    157   1.1      cgd 	int errcode = 0;
    158   1.1      cgd 	int unit, part;
    159   1.1      cgd 
    160   1.1      cgd 	unit = UNIT(dev);
    161   1.1      cgd 	part = PARTITION(dev);
    162   1.1      cgd 	if(scsi_debug & (PRINTROUTINES | TRACEOPENS))
    163   1.3  deraadt 		printf("sdopen: dev=0x%x (unit %d (of %d),partition %d)\n",
    164   1.3  deraadt 			dev, unit, NSD, part);
    165   1.1      cgd 
    166   1.3  deraadt 	if(unit > NSD)
    167   1.3  deraadt 		return ENXIO;
    168   1.3  deraadt 	if( !sd_data[unit]) {
    169   1.3  deraadt 		if(scsi_debug & PRINTROUTINES)
    170   1.3  deraadt 			printf("nonexistant!\n");
    171   1.3  deraadt 		return ENXIO;
    172   1.3  deraadt 	}
    173   1.3  deraadt 
    174   1.3  deraadt 	sd = sd_data[unit];
    175   1.4  deraadt 	if(!sd)
    176   1.4  deraadt 		return ENXIO;
    177   1.3  deraadt 	if( !(sd->flags & SDVALID) )
    178   1.3  deraadt 		return ENXIO;
    179   1.3  deraadt 
    180   1.3  deraadt 	/*
    181   1.3  deraadt 	 * Make sure the disk has been initialised.
    182   1.3  deraadt 	 * XXX get the scsi driver to look for a new device if
    183   1.3  deraadt 	 * we are not initted, like SunOS
    184   1.3  deraadt 	 */
    185   1.3  deraadt 	if( !(sd->flags & SDINIT))
    186   1.3  deraadt 		return ENXIO;
    187   1.3  deraadt 
    188   1.3  deraadt 	/*
    189   1.3  deraadt 	 * If it's been invalidated, and not everybody has
    190   1.3  deraadt 	 * closed it then forbid re-entry.
    191   1.3  deraadt 	 */
    192   1.3  deraadt 	if( !(sd->flags & SDVALID) && sd->openparts)
    193   1.3  deraadt 		return ENXIO;
    194   1.3  deraadt 
    195   1.3  deraadt 	/*
    196   1.3  deraadt 	 * Check that it is still responding and ok.
    197   1.3  deraadt 	 * "unit attention errors should occur here if the drive
    198   1.3  deraadt 	 * has been restarted or the pack changed
    199   1.3  deraadt 	 */
    200   1.1      cgd 	if(scsi_debug & TRACEOPENS)
    201   1.1      cgd 		printf("device is ");
    202   1.3  deraadt 
    203   1.3  deraadt 	/*
    204   1.3  deraadt 	 * In case it is a funny one, tell it to start
    205   1.3  deraadt 	 * not needed for most hard drives (ignore failure)
    206  1.11   davidb 	 *
    207  1.11   davidb 	 * This needs to be done BEFORE the test_unit_ready - davidb/simonb
    208   1.3  deraadt 	 */
    209   1.3  deraadt 	sd_start_unit(unit, SCSI_ERR_OK|SCSI_SILENT);
    210   1.1      cgd 	if(scsi_debug & TRACEOPENS)
    211   1.1      cgd 		printf("started ");
    212  1.11   davidb 
    213  1.11   davidb 	if (sd_test_unit_ready(unit, 0)) {
    214  1.11   davidb 		if(scsi_debug & TRACEOPENS)
    215  1.11   davidb 			printf("not responding\n");
    216  1.11   davidb 		return ENXIO;
    217  1.11   davidb 	}
    218  1.11   davidb 	if(scsi_debug & TRACEOPENS)
    219  1.11   davidb 		printf("ok\n");
    220   1.3  deraadt 
    221   1.3  deraadt 	/*
    222   1.3  deraadt 	 * Load the physical device parameters
    223   1.3  deraadt 	 */
    224   1.3  deraadt 	sd_get_parms(unit, 0);			/* sets SDVALID */
    225   1.3  deraadt 	if( sd->params.secsiz != SECSIZE) {
    226   1.3  deraadt 		printf("sd%d: Can't deal with %d bytes logical blocks\n",
    227   1.3  deraadt 			unit, sd->params.secsiz);
    228   1.3  deraadt 		return ENXIO;
    229   1.1      cgd 	}
    230   1.1      cgd 	if(scsi_debug & TRACEOPENS)
    231   1.1      cgd 		printf("Params loaded ");
    232   1.3  deraadt 
    233   1.3  deraadt 	/*
    234   1.3  deraadt 	 * Load the partition info if not already loaded
    235   1.3  deraadt 	 */
    236   1.3  deraadt 	sd_prevent(unit, PR_PREVENT, SCSI_ERR_OK|SCSI_SILENT);
    237   1.3  deraadt 	if( (errcode=sdgetdisklabel(unit)) && (part != RAW_PART)) {
    238   1.3  deraadt 		sd_prevent(unit, PR_ALLOW, SCSI_ERR_OK|SCSI_SILENT);
    239   1.3  deraadt 		return errcode;
    240   1.1      cgd 	}
    241   1.1      cgd 	if(scsi_debug & TRACEOPENS)
    242   1.1      cgd 		printf("Disklabel loaded ");
    243   1.3  deraadt 
    244   1.3  deraadt 	/*
    245   1.3  deraadt 	 * Check the partition is legal
    246   1.3  deraadt 	 */
    247   1.1      cgd 	if ( part >= MAXPARTITIONS ) {
    248   1.3  deraadt 		sd_prevent(unit, PR_ALLOW, SCSI_ERR_OK|SCSI_SILENT);
    249   1.3  deraadt 		return ENXIO;
    250   1.1      cgd 	}
    251   1.1      cgd 	if(scsi_debug & TRACEOPENS)
    252   1.1      cgd 		printf("ok");
    253   1.3  deraadt 
    254   1.3  deraadt 	/*
    255   1.3  deraadt 	 *  Check that the partition exists
    256   1.3  deraadt 	 */
    257   1.3  deraadt 	if( sd->disklabel.d_partitions[part].p_size==0 && part!=RAW_PART) {
    258   1.3  deraadt 		sd_prevent(unit, PR_ALLOW, SCSI_ERR_OK|SCSI_SILENT);
    259   1.3  deraadt 		return ENXIO;
    260   1.1      cgd 	}
    261   1.3  deraadt 
    262   1.1      cgd 	sd->partflags[part] |= SDOPEN;
    263   1.1      cgd 	sd->openparts |= (1 << part);
    264   1.1      cgd 	if(scsi_debug & TRACEOPENS)
    265   1.3  deraadt 		printf("open %d %d\n", sdstrats, sdqueues);
    266   1.3  deraadt 	return 0;
    267   1.1      cgd }
    268   1.1      cgd 
    269   1.3  deraadt /*
    270   1.3  deraadt  * Get ownership of a scsi_xfer
    271   1.3  deraadt  * If need be, sleep on it, until it comes free
    272   1.3  deraadt  */
    273   1.3  deraadt struct scsi_xfer *
    274   1.3  deraadt sd_get_xs(int unit, int flags)
    275   1.1      cgd {
    276   1.3  deraadt 	struct sd_data *sd = sd_data[unit];
    277   1.1      cgd 	struct scsi_xfer *xs;
    278   1.3  deraadt 	int s;
    279   1.1      cgd 
    280   1.3  deraadt 	if(flags & (SCSI_NOSLEEP |  SCSI_NOMASK)) {
    281   1.3  deraadt 		if (xs = sd->freexfer) {
    282   1.3  deraadt 			sd->freexfer = xs->next;
    283   1.1      cgd 			xs->flags = 0;
    284   1.1      cgd 		}
    285   1.3  deraadt 	} else {
    286   1.1      cgd 		s = SPLSD();
    287   1.3  deraadt 		while (!(xs = sd->freexfer)) {
    288   1.3  deraadt 			sd->blockwait++;  /* someone waiting! */
    289  1.18  mycroft 			tsleep((caddr_t)&sd->freexfer, PRIBIO+1, "sd_get_xs",0);
    290   1.3  deraadt 			sd->blockwait--;
    291   1.1      cgd 		}
    292   1.3  deraadt 		sd->freexfer = xs->next;
    293   1.1      cgd 		splx(s);
    294   1.1      cgd 		xs->flags = 0;
    295   1.1      cgd 	}
    296   1.3  deraadt 	return xs;
    297   1.1      cgd }
    298   1.1      cgd 
    299   1.3  deraadt /*
    300   1.3  deraadt  * Free a scsi_xfer, wake processes waiting for it
    301   1.3  deraadt  */
    302   1.3  deraadt void
    303   1.3  deraadt sd_free_xs(int unit, struct scsi_xfer *xs, int flags)
    304   1.1      cgd {
    305   1.3  deraadt 	struct sd_data *sd = sd_data[unit];
    306   1.3  deraadt 	int s;
    307   1.1      cgd 
    308   1.3  deraadt 	if(flags & SCSI_NOMASK) {
    309   1.3  deraadt 		if (sd->blockwait) {
    310   1.1      cgd 			printf("doing a wakeup from NOMASK mode\n");
    311   1.3  deraadt 			wakeup((caddr_t)&sd->freexfer);
    312   1.1      cgd 		}
    313   1.3  deraadt 		xs->next = sd->freexfer;
    314   1.3  deraadt 		sd->freexfer = xs;
    315   1.3  deraadt 	} else {
    316   1.1      cgd 		s = SPLSD();
    317   1.3  deraadt 		if (sd->blockwait)
    318   1.3  deraadt 			wakeup((caddr_t)&sd->freexfer);
    319   1.3  deraadt 		xs->next = sd->freexfer;
    320   1.3  deraadt 		sd->freexfer = xs;
    321   1.1      cgd 		splx(s);
    322   1.1      cgd 	}
    323   1.1      cgd }
    324   1.1      cgd 
    325   1.3  deraadt /*
    326   1.3  deraadt  * trim the size of the transfer if needed, called by physio
    327   1.3  deraadt  * basically the smaller of our max and the scsi driver's
    328   1.3  deraadt  * minphys (note we have no max)
    329   1.3  deraadt  */
    330   1.3  deraadt void
    331   1.3  deraadt sdminphys(struct buf *bp)
    332   1.3  deraadt {
    333   1.3  deraadt 	(*(sd_data[UNIT(bp->b_dev)]->sc_sw->scsi_minphys))(bp);
    334   1.3  deraadt }
    335   1.1      cgd 
    336   1.3  deraadt /*
    337   1.3  deraadt  * Actually translate the requested transfer into
    338   1.3  deraadt  * one the physical driver can understand
    339   1.3  deraadt  * The transfer is described by a buf and will include
    340   1.3  deraadt  * only one physical transfer.
    341   1.3  deraadt  */
    342   1.3  deraadt int
    343   1.3  deraadt sdstrategy(struct buf *bp)
    344   1.1      cgd {
    345   1.3  deraadt 	struct sd_data *sd;
    346   1.1      cgd 	unsigned int opri;
    347   1.3  deraadt 	struct	buf *dp;
    348   1.3  deraadt 	int unit;
    349   1.1      cgd 
    350   1.1      cgd 	sdstrats++;
    351   1.1      cgd 	unit = UNIT((bp->b_dev));
    352   1.3  deraadt 
    353   1.3  deraadt 	if(unit > NSD) {
    354   1.3  deraadt 		printf("sdstrategy bailout: %d %d\n", unit, NSD);
    355   1.3  deraadt 		bp->b_error = EIO;
    356   1.3  deraadt 		goto bad;
    357   1.3  deraadt 	}
    358   1.3  deraadt 	if( !sd_data[unit]) {
    359   1.3  deraadt 		printf("sdstrategy bailout\n");
    360   1.3  deraadt 		bp->b_error = EIO;
    361   1.3  deraadt 		goto bad;
    362   1.3  deraadt 	}
    363   1.3  deraadt 
    364   1.3  deraadt 	sd = sd_data[unit];
    365   1.3  deraadt 	if(scsi_debug & PRINTROUTINES)
    366   1.3  deraadt 		printf("\nsdstrategy ");
    367   1.3  deraadt 	if(scsi_debug & SHOWREQUESTS)
    368   1.3  deraadt 		printf("sd%d: %d bytes @ blk%d\n",
    369   1.3  deraadt 			unit, bp->b_bcount, bp->b_blkno);
    370   1.3  deraadt 
    371  1.16   andrew 	/* Reject non block-aligned transfers */
    372  1.16   andrew 	if (bp->b_bcount % SECSIZE) {
    373  1.16   andrew 		bp->b_error = EINVAL;
    374  1.16   andrew 		goto bad;
    375  1.16   andrew 	}
    376  1.16   andrew 
    377   1.1      cgd 	sdminphys(bp);
    378   1.3  deraadt 
    379   1.3  deraadt 	/* If the device has been made invalid, error out */
    380   1.3  deraadt 	if(!(sd->flags & SDVALID)) {
    381   1.1      cgd 		bp->b_error = EIO;
    382   1.1      cgd 		goto bad;
    383   1.1      cgd 	}
    384   1.3  deraadt 
    385   1.3  deraadt 	/* "soft" write protect check */
    386   1.1      cgd 	if ((sd->flags & SDWRITEPROT) && (bp->b_flags & B_READ) == 0) {
    387   1.1      cgd 		bp->b_error = EROFS;
    388   1.1      cgd 		goto bad;
    389   1.1      cgd 	}
    390   1.3  deraadt 
    391   1.3  deraadt 	/* If it's a null transfer, return immediately */
    392   1.1      cgd 	if (bp->b_bcount == 0)
    393   1.1      cgd 		goto done;
    394   1.1      cgd 
    395   1.3  deraadt 	/*
    396   1.3  deraadt 	 * Decide which unit and partition we are talking about
    397   1.3  deraadt 	 * only raw is ok if no label
    398   1.3  deraadt 	 */
    399   1.3  deraadt 	if(PARTITION(bp->b_dev) != RAW_PART) {
    400   1.3  deraadt 		if (!(sd->flags & SDHAVELABEL)) {
    401   1.1      cgd 			bp->b_error = EIO;
    402   1.1      cgd 			goto bad;
    403   1.1      cgd 		}
    404   1.1      cgd 
    405   1.1      cgd 		/*
    406   1.1      cgd 		 * do bounds checking, adjust transfer. if error, process.
    407   1.1      cgd 		 * if end of partition, just return
    408   1.1      cgd 		 */
    409   1.3  deraadt 		if (bounds_check_with_label(bp, &sd->disklabel, sd->wlabel) <= 0)
    410   1.1      cgd 			goto done;
    411   1.1      cgd 		/* otherwise, process transfer request */
    412   1.1      cgd 	}
    413   1.1      cgd 
    414   1.1      cgd 	opri = SPLSD();
    415   1.3  deraadt 	dp = &(sd_data[unit]->sdbuf);
    416   1.1      cgd 
    417   1.3  deraadt 	/* Place it in the queue of disk activities for this disk */
    418   1.1      cgd 	disksort(dp, bp);
    419   1.1      cgd 
    420   1.3  deraadt 	/*
    421   1.3  deraadt 	 * Tell the device to get going on the transfer if it's
    422   1.3  deraadt 	 * not doing anything, otherwise just wait for completion
    423   1.3  deraadt 	 */
    424   1.1      cgd 	sdstart(unit);
    425   1.1      cgd 
    426   1.1      cgd 	splx(opri);
    427   1.1      cgd 	return;
    428   1.1      cgd bad:
    429   1.1      cgd 	bp->b_flags |= B_ERROR;
    430   1.1      cgd done:
    431   1.3  deraadt 	/* Correctly set the buf to indicate a completed xfer */
    432   1.1      cgd   	bp->b_resid = bp->b_bcount;
    433   1.1      cgd 	biodone(bp);
    434   1.1      cgd 	return;
    435   1.1      cgd }
    436   1.1      cgd 
    437   1.3  deraadt /*
    438   1.3  deraadt  * sdstart looks to see if there is a buf waiting for the device
    439   1.3  deraadt  * and that the device is not already busy. If both are true,
    440   1.3  deraadt  * It deques the buf and creates a scsi command to perform the
    441   1.3  deraadt  * transfer in the buf. The transfer request will call sd_done
    442   1.3  deraadt  * on completion, which will in turn call this routine again
    443   1.3  deraadt  * so that the next queued transfer is performed.
    444   1.3  deraadt  * The bufs are queued by the strategy routine (sdstrategy)
    445   1.3  deraadt  * This routine is also called after other non-queued requests
    446   1.3  deraadt  * have been made of the scsi driver, to ensure that the queue
    447   1.3  deraadt  * continues to be drained.
    448   1.3  deraadt  * must be called at the correct (highish) spl level
    449   1.3  deraadt  * sdstart() is called at SPLSD from sdstrategy and sd_done
    450   1.3  deraadt  */
    451   1.3  deraadt void
    452   1.3  deraadt sdstart(int unit)
    453   1.3  deraadt {
    454   1.3  deraadt 	register struct buf *bp = 0, *dp;
    455   1.3  deraadt 	struct sd_data *sd = sd_data[unit];
    456   1.3  deraadt 	struct scsi_rw_big cmd;
    457   1.3  deraadt 	struct scsi_xfer *xs;
    458   1.3  deraadt 	struct partition *p;
    459   1.3  deraadt 	int drivecount, blkno, nblk;
    460   1.3  deraadt 
    461   1.3  deraadt 	if(scsi_debug & PRINTROUTINES)
    462   1.3  deraadt 		printf("sdstart%d ", unit);
    463   1.3  deraadt 
    464   1.3  deraadt 	sd = sd_data[unit];
    465   1.4  deraadt 	if(!sd)
    466   1.4  deraadt 		return;
    467   1.3  deraadt 
    468   1.3  deraadt 	/*
    469   1.3  deraadt 	 * See if there is a buf to do and we are not already
    470   1.3  deraadt 	 * doing one
    471   1.3  deraadt 	 */
    472   1.3  deraadt 	if(!sd->freexfer)
    473   1.1      cgd 		return;    /* none for us, unit already underway */
    474   1.1      cgd 
    475   1.3  deraadt 	if(sd->blockwait)    /* there is one, but a special waits */
    476   1.1      cgd 		return;	/* give the special that's waiting a chance to run */
    477   1.1      cgd 
    478   1.1      cgd 
    479   1.3  deraadt 	dp = &(sd_data[unit]->sdbuf);
    480   1.1      cgd 	if ((bp = dp->b_actf) != NULL)	/* yes, an assign */
    481   1.1      cgd 		dp->b_actf = bp->av_forw;
    482   1.1      cgd 	else
    483   1.1      cgd 		return;
    484   1.3  deraadt 
    485   1.3  deraadt 	xs=sd_get_xs(unit, 0);		/* ok we can grab it */
    486   1.3  deraadt 	xs->flags = INUSE;		/* Now ours */
    487   1.1      cgd 
    488   1.3  deraadt 	/*
    489   1.3  deraadt 	 *  If the device has become invalid, abort all the reads
    490   1.3  deraadt 	 * and writes until all files have been closed and re-openned
    491   1.3  deraadt 	 */
    492   1.3  deraadt 	if( !(sd->flags & SDVALID) ) {
    493   1.1      cgd 		xs->error = XS_DRIVER_STUFFUP;
    494   1.1      cgd 		sd_done(unit,xs);  /* clean up (calls sdstart) */
    495   1.1      cgd 		return ;
    496   1.1      cgd 	}
    497   1.3  deraadt 
    498   1.3  deraadt 	/*
    499   1.3  deraadt 	 * We have a buf, now we should move the data into
    500   1.3  deraadt 	 * a scsi_xfer definition and try start it
    501   1.3  deraadt 	 *  First, translate the block to absolute
    502   1.3  deraadt 	 */
    503   1.1      cgd 	p = sd->disklabel.d_partitions + PARTITION(bp->b_dev);
    504   1.1      cgd 	blkno = bp->b_blkno + p->p_offset;
    505   1.1      cgd 	nblk = (bp->b_bcount + 511) >> 9;
    506   1.1      cgd 
    507   1.3  deraadt 	/* Fill out the scsi command */
    508   1.1      cgd 	bzero(&cmd, sizeof(cmd));
    509   1.3  deraadt 	cmd.op_code = (bp->b_flags & B_READ) ? READ_BIG : WRITE_BIG;
    510   1.3  deraadt 	cmd.addr_3 = (blkno & 0xff000000) >> 24;
    511   1.3  deraadt 	cmd.addr_2 = (blkno & 0xff0000) >> 16;
    512   1.3  deraadt 	cmd.addr_1 = (blkno & 0xff00) >> 8;
    513   1.3  deraadt 	cmd.addr_0 = blkno & 0xff;
    514   1.3  deraadt 	cmd.length2 = (nblk & 0xff00) >> 8;
    515   1.3  deraadt 	cmd.length1 = (nblk & 0xff);
    516   1.3  deraadt 
    517   1.3  deraadt 	/*
    518   1.3  deraadt 	 * Fill out the scsi_xfer structure
    519   1.3  deraadt 	 * Note: we cannot sleep as we may be an interrupt
    520   1.3  deraadt 	 */
    521   1.3  deraadt 	xs->flags |= SCSI_NOSLEEP;
    522   1.3  deraadt 	xs->adapter = sd->ctlr;
    523   1.3  deraadt 	xs->targ = sd->targ;
    524   1.3  deraadt 	xs->lu = sd->lu;
    525   1.3  deraadt 	xs->retries = SD_RETRIES;
    526   1.3  deraadt 	xs->timeout = 10000;		/* 10000 millisecs for a disk !*/
    527   1.3  deraadt 	xs->cmd = (struct scsi_generic *)&cmd;
    528   1.3  deraadt 	xs->cmdlen = sizeof(cmd);
    529   1.3  deraadt 	xs->resid = bp->b_bcount;
    530   1.3  deraadt 	xs->when_done = sd_done;
    531   1.3  deraadt 	xs->done_arg = unit;
    532   1.3  deraadt 	xs->done_arg2 = (int)xs;
    533   1.3  deraadt 	xs->error = XS_NOERROR;
    534   1.3  deraadt 	xs->bp = bp;
    535   1.3  deraadt 	xs->data = (u_char *)bp->b_un.b_addr;
    536   1.3  deraadt 	xs->datalen = bp->b_bcount;
    537   1.1      cgd 
    538   1.3  deraadt 	/* Pass all this info to the scsi driver */
    539   1.3  deraadt 	if ( (*(sd->sc_sw->scsi_cmd))(xs) != SUCCESSFULLY_QUEUED) {
    540   1.1      cgd 		printf("sd%d: oops not queued",unit);
    541   1.1      cgd 		xs->error = XS_DRIVER_STUFFUP;
    542   1.3  deraadt 		sd_done(unit, xs);	/* clean up (calls sdstart) */
    543   1.1      cgd 	}
    544   1.1      cgd 	sdqueues++;
    545   1.1      cgd }
    546   1.1      cgd 
    547   1.3  deraadt /*
    548   1.3  deraadt  * This routine is called by the scsi interrupt when
    549   1.3  deraadt  * the transfer is complete.
    550   1.3  deraadt  */
    551   1.3  deraadt int
    552   1.3  deraadt sd_done(int unit, struct scsi_xfer *xs)
    553   1.3  deraadt {
    554   1.3  deraadt 	struct buf *bp;
    555   1.3  deraadt 	int retval, retries = 0;
    556   1.1      cgd 
    557   1.3  deraadt 	if(scsi_debug & PRINTROUTINES)
    558   1.3  deraadt 		printf("sd_done%d ",unit);
    559   1.3  deraadt 	if( !(xs->flags & INUSE))
    560   1.1      cgd 		panic("scsi_xfer not in use!");
    561   1.3  deraadt 	if(bp = xs->bp) {
    562   1.3  deraadt 		switch(xs->error) {
    563   1.3  deraadt 		case XS_NOERROR:
    564   1.1      cgd 			bp->b_error = 0;
    565   1.1      cgd 			bp->b_resid = 0;
    566   1.1      cgd 			break;
    567   1.3  deraadt 		case XS_SENSE:
    568   1.1      cgd 			retval = (sd_interpret_sense(unit,xs));
    569   1.3  deraadt 			if(retval) {
    570   1.1      cgd 				bp->b_flags |= B_ERROR;
    571   1.1      cgd 				bp->b_error = retval;
    572   1.1      cgd 			}
    573   1.1      cgd 			break;
    574   1.3  deraadt 		case XS_TIMEOUT:
    575   1.1      cgd 			printf("sd%d timeout\n",unit);
    576   1.3  deraadt 		case XS_BUSY:	/* should retry -- how? */
    577   1.3  deraadt 			/*
    578   1.3  deraadt 			 * SHOULD put buf back at head of queue
    579   1.3  deraadt 			 * and decrement retry count in (*xs)
    580   1.3  deraadt 			 * HOWEVER, this should work as a kludge
    581   1.3  deraadt 			 */
    582   1.3  deraadt 			if(xs->retries--) {
    583   1.1      cgd 				xs->error = XS_NOERROR;
    584   1.1      cgd 				xs->flags &= ~ITSDONE;
    585   1.3  deraadt 				if( (*(sd_data[unit]->sc_sw->scsi_cmd))(xs)
    586   1.3  deraadt 				    == SUCCESSFULLY_QUEUED) {
    587   1.3  deraadt 					/* don't wake the job, ok? */
    588   1.1      cgd 					return;
    589   1.1      cgd 				}
    590   1.1      cgd 				xs->flags |= ITSDONE;
    591   1.1      cgd 			} /* fall through */
    592   1.1      cgd 
    593   1.3  deraadt 		case XS_DRIVER_STUFFUP:
    594   1.1      cgd 			bp->b_flags |= B_ERROR;
    595   1.1      cgd 			bp->b_error = EIO;
    596   1.1      cgd 			break;
    597   1.1      cgd 		default:
    598   1.3  deraadt 			printf("sd%d: unknown error category from scsi driver\n", unit);
    599   1.3  deraadt 		}
    600   1.1      cgd 		biodone(bp);
    601   1.3  deraadt 		sd_free_xs(unit, xs, 0);
    602   1.3  deraadt 		sdstart(unit);		/* If there's anything waiting.. do it */
    603   1.3  deraadt 	} else
    604  1.15   andrew 		wakeup((caddr_t)xs);
    605   1.1      cgd }
    606   1.3  deraadt 
    607   1.3  deraadt /*
    608   1.3  deraadt  * Perform special action on behalf of the user
    609   1.3  deraadt  * Knows about the internals of this device
    610   1.3  deraadt  */
    611   1.3  deraadt int
    612   1.1      cgd sdioctl(dev_t dev, int cmd, caddr_t addr, int flag)
    613   1.1      cgd {
    614   1.1      cgd 	/* struct sd_cmd_buf *args;*/
    615   1.3  deraadt 	struct scsi_format_parms *fparms;
    616  1.10  deraadt 	struct cpu_disklabel osdep;
    617   1.3  deraadt 	extern struct proc *curproc;
    618   1.3  deraadt 	register struct sd_data *sd;
    619   1.3  deraadt 	unsigned char unit, part;
    620   1.1      cgd 	unsigned int opri;
    621   1.3  deraadt 	int error = 0, x;
    622   1.1      cgd 
    623   1.3  deraadt 	/* Find the device that the user is talking about */
    624   1.1      cgd 	unit = UNIT(dev);
    625   1.1      cgd 	part = PARTITION(dev);
    626   1.3  deraadt 	if(scsi_debug & PRINTROUTINES)
    627   1.3  deraadt 		printf("sdioctl%d ",unit);
    628   1.1      cgd 
    629   1.3  deraadt 	/* If the device is not valid.. abandon ship */
    630   1.3  deraadt 	if(unit > NSD)
    631   1.3  deraadt 		return EIO;
    632   1.4  deraadt 	sd = sd_data[unit];
    633   1.3  deraadt 	if(sd==NULL)
    634   1.3  deraadt 		return EIO;
    635   1.1      cgd 
    636   1.3  deraadt 	if(!(sd->flags & SDVALID))
    637   1.3  deraadt 		return EIO;
    638   1.3  deraadt 
    639   1.3  deraadt 	switch(cmd) {
    640   1.3  deraadt 	case DIOCWFORMAT:
    641   1.3  deraadt 		if( suser(curproc->p_ucred, &curproc->p_acflag))
    642   1.3  deraadt 			return EPERM;
    643   1.3  deraadt 
    644   1.3  deraadt 		x = splbio();
    645   1.3  deraadt 		if(sd->formatting)
    646   1.3  deraadt 			return EBUSY;
    647   1.3  deraadt 		sd->formatting = 1;
    648   1.3  deraadt 		(void)splx(x);
    649   1.3  deraadt 
    650   1.3  deraadt 		fparms = (struct scsi_format_parms *)malloc(sizeof *fparms,
    651   1.3  deraadt 			M_TEMP, M_NOWAIT);
    652   1.3  deraadt 		if(!fparms) {
    653   1.3  deraadt 			error = EAGAIN;
    654   1.3  deraadt 			goto unlock;
    655   1.3  deraadt 		}
    656   1.3  deraadt 
    657   1.3  deraadt 		if(copyin(&addr, fparms, sizeof fparms)!=0) {
    658   1.3  deraadt 			free(fparms, M_TEMP);
    659   1.3  deraadt 			error = EFAULT;
    660   1.3  deraadt 			goto unlock;
    661   1.3  deraadt 		}
    662   1.3  deraadt 		error = sd_format(unit, fparms, 0, 0);
    663   1.3  deraadt 		if(!error && copyout(&addr, fparms, sizeof fparms) )
    664   1.3  deraadt 			error = EFAULT;
    665   1.3  deraadt 		free(fparms, M_TEMP);
    666   1.3  deraadt unlock:
    667   1.3  deraadt 		x = splbio();
    668   1.3  deraadt 		sd->formatting = 0;
    669   1.3  deraadt 		(void)splx(x);
    670   1.3  deraadt 
    671   1.3  deraadt 		break;
    672   1.3  deraadt 	case DIOCRFORMAT:
    673   1.3  deraadt 		error = EINVAL;
    674   1.3  deraadt 		break;
    675   1.1      cgd 	case DIOCSBAD:
    676   1.3  deraadt 		error = EINVAL;
    677   1.1      cgd 		break;
    678   1.1      cgd 	case DIOCGDINFO:
    679   1.1      cgd 		*(struct disklabel *)addr = sd->disklabel;
    680   1.1      cgd 		break;
    681   1.3  deraadt 	case DIOCGPART:
    682   1.3  deraadt 		((struct partinfo *)addr)->disklab = &sd->disklabel;
    683   1.3  deraadt 		((struct partinfo *)addr)->part =
    684   1.3  deraadt 		    &sd->disklabel.d_partitions[PARTITION(dev)];
    685   1.3  deraadt 		break;
    686   1.3  deraadt 	case DIOCSDINFO:
    687   1.3  deraadt 		if ((flag & FWRITE) == 0)
    688   1.3  deraadt 			error = EBADF;
    689   1.3  deraadt 		else {
    690   1.3  deraadt 			error = setdisklabel(&sd->disklabel, (struct disklabel *)addr,
    691   1.3  deraadt 			    /*(sd->flags & DKFL_BSDLABEL) ? sd->openparts : */0,
    692  1.10  deraadt 			    &sd->cpudisklabel);
    693   1.3  deraadt 		}
    694   1.3  deraadt 		if (error == 0)
    695   1.1      cgd 			sd->flags |= SDHAVELABEL;
    696   1.3  deraadt 		break;
    697   1.3  deraadt 	case DIOCWLABEL:
    698   1.1      cgd 		sd->flags &= ~SDWRITEPROT;
    699   1.3  deraadt 		if ((flag & FWRITE) == 0)
    700   1.3  deraadt 			error = EBADF;
    701   1.3  deraadt 		else
    702   1.3  deraadt 			sd->wlabel = *(int *)addr;
    703   1.3  deraadt 		break;
    704   1.3  deraadt 	case DIOCWDINFO:
    705   1.1      cgd 		sd->flags &= ~SDWRITEPROT;
    706   1.3  deraadt 		if ((flag & FWRITE) == 0)
    707   1.3  deraadt 			error = EBADF;
    708   1.3  deraadt 		else {
    709   1.3  deraadt 			if ((error = setdisklabel(&sd->disklabel,
    710   1.3  deraadt 			    (struct disklabel *)addr,
    711   1.3  deraadt 			    /*(sd->flags & SDHAVELABEL) ? sd->openparts :*/0,
    712  1.10  deraadt 			    &sd->cpudisklabel)) == 0) {
    713   1.3  deraadt 				int wlab;
    714   1.1      cgd 
    715   1.1      cgd 				sd->flags |= SDHAVELABEL; /* ok write will succeed */
    716   1.1      cgd 
    717   1.3  deraadt 				/* simulate opening partition 0 so write succeeds */
    718   1.3  deraadt 				sd->openparts |= (1 << 0);	    /* XXX */
    719   1.3  deraadt 				wlab = sd->wlabel;
    720   1.3  deraadt 				sd->wlabel = 1;
    721   1.3  deraadt 				error = writedisklabel(dev, sdstrategy,
    722  1.10  deraadt 					&sd->disklabel, &sd->cpudisklabel);
    723   1.3  deraadt 				sd->wlabel = wlab;
    724   1.3  deraadt 			}
    725   1.1      cgd 		}
    726   1.3  deraadt 		break;
    727   1.1      cgd 	default:
    728   1.1      cgd 		error = ENOTTY;
    729   1.1      cgd 		break;
    730   1.1      cgd 	}
    731   1.3  deraadt 	return error;
    732   1.1      cgd }
    733   1.1      cgd 
    734   1.1      cgd 
    735   1.3  deraadt /*
    736   1.3  deraadt  * Load the label information on the named device
    737   1.3  deraadt  */
    738   1.3  deraadt int
    739   1.3  deraadt sdgetdisklabel(u_char unit)
    740   1.1      cgd {
    741   1.3  deraadt 	struct sd_data *sd = sd_data[unit];
    742   1.1      cgd 	/*unsigned int n, m;*/
    743   1.1      cgd 	char *errstring;
    744  1.10  deraadt 	struct cpu_disklabel osdep;
    745   1.1      cgd 
    746   1.3  deraadt 	/* If the inflo is already loaded, use it */
    747   1.3  deraadt 	if(sd->flags & SDHAVELABEL)
    748   1.3  deraadt 		return ESUCCESS;
    749   1.3  deraadt 
    750   1.3  deraadt 	bzero(&sd->disklabel, sizeof(struct disklabel));
    751   1.3  deraadt 	/*
    752   1.3  deraadt 	 * make partition 3 the whole disk in case of failure
    753   1.3  deraadt 	 * then get pdinfo
    754   1.3  deraadt 	 */
    755   1.1      cgd 	sd->disklabel.d_partitions[0].p_offset = 0;
    756   1.1      cgd 	sd->disklabel.d_partitions[0].p_size = sd->params.disksize;
    757   1.1      cgd 	sd->disklabel.d_partitions[RAW_PART].p_offset = 0;
    758   1.1      cgd 	sd->disklabel.d_partitions[RAW_PART].p_size = sd->params.disksize;
    759   1.1      cgd 	sd->disklabel.d_npartitions = MAXPARTITIONS;
    760  1.16   andrew 	sd->disklabel.d_secsize = SECSIZE; /* as long as it's not 0 */
    761   1.1      cgd 	sd->disklabel.d_ntracks = sd->params.heads;
    762   1.1      cgd 	sd->disklabel.d_nsectors = sd->params.sectors;
    763   1.1      cgd 	sd->disklabel.d_ncylinders = sd->params.cyls;
    764   1.1      cgd 	sd->disklabel.d_secpercyl = sd->params.heads * sd->params.sectors;
    765   1.3  deraadt 	if (sd->disklabel.d_secpercyl == 0) {
    766   1.3  deraadt 		/* as long as it's not 0 because readdisklabel() divides by it */
    767   1.1      cgd 		sd->disklabel.d_secpercyl = 100;
    768   1.1      cgd 	}
    769   1.1      cgd 
    770   1.3  deraadt 	/* all the generic disklabel extraction routine */
    771   1.3  deraadt 	if(errstring = readdisklabel(makedev(0 ,(unit<<UNITSHIFT )+3),
    772  1.10  deraadt 	    sdstrategy, &sd->disklabel, &sd->cpudisklabel)) {
    773   1.1      cgd 		printf("sd%d: %s\n",unit, errstring);
    774   1.3  deraadt 		return ENXIO;
    775   1.1      cgd 	}
    776   1.1      cgd 
    777   1.3  deraadt 	/* leave partition 2 "open" for raw I/O */
    778   1.3  deraadt 
    779   1.3  deraadt 	sd->flags |= SDHAVELABEL;	/* WE HAVE IT ALL NOW */
    780   1.3  deraadt 	return ESUCCESS;
    781   1.1      cgd }
    782   1.1      cgd 
    783   1.3  deraadt /*
    784   1.3  deraadt  * Find out from the device what it's capacity is
    785   1.3  deraadt  */
    786   1.3  deraadt int
    787   1.3  deraadt sd_size(int unit, int flags)
    788   1.1      cgd {
    789   1.1      cgd 	struct scsi_read_cap_data rdcap;
    790   1.1      cgd 	struct scsi_read_capacity scsi_cmd;
    791   1.1      cgd 	int size;
    792   1.1      cgd 
    793   1.3  deraadt 	/*
    794   1.3  deraadt 	 * make up a scsi command and ask the scsi driver to do
    795   1.3  deraadt 	 * it for you.
    796   1.3  deraadt 	 */
    797   1.1      cgd 	bzero(&scsi_cmd, sizeof(scsi_cmd));
    798   1.1      cgd 	scsi_cmd.op_code = READ_CAPACITY;
    799   1.1      cgd 
    800   1.3  deraadt 	/*
    801   1.3  deraadt 	 * If the command works, interpret the result as a 4 byte
    802   1.3  deraadt 	 * number of blocks
    803   1.3  deraadt 	 */
    804   1.3  deraadt 	if (sd_scsi_cmd(unit, (struct scsi_generic *)&scsi_cmd,
    805   1.3  deraadt 	    sizeof(scsi_cmd), (u_char *)&rdcap, sizeof(rdcap), 2000, flags) != 0) {
    806   1.1      cgd 		printf("could not get size of unit %d\n", unit);
    807   1.3  deraadt 		return 0;
    808   1.1      cgd 	} else {
    809   1.1      cgd 		size = rdcap.addr_0 + 1 ;
    810   1.1      cgd 		size += rdcap.addr_1 << 8;
    811   1.1      cgd 		size += rdcap.addr_2 << 16;
    812   1.1      cgd 		size += rdcap.addr_3 << 24;
    813   1.1      cgd 	}
    814   1.3  deraadt 	return size;
    815   1.1      cgd }
    816   1.1      cgd 
    817   1.3  deraadt /*
    818   1.3  deraadt  * Get scsi driver to send a "are you ready?" command
    819   1.3  deraadt  */
    820   1.3  deraadt int
    821   1.3  deraadt sd_test_unit_ready(int unit, int flags)
    822   1.1      cgd {
    823   1.1      cgd 	struct	scsi_test_unit_ready scsi_cmd;
    824   1.1      cgd 
    825   1.1      cgd 	bzero(&scsi_cmd, sizeof(scsi_cmd));
    826   1.1      cgd 	scsi_cmd.op_code = TEST_UNIT_READY;
    827   1.1      cgd 
    828   1.3  deraadt 	return sd_scsi_cmd(unit, (struct scsi_generic *)&scsi_cmd,
    829   1.3  deraadt 	    sizeof(scsi_cmd), 0, 0, 100000, flags);
    830   1.3  deraadt }
    831   1.3  deraadt 
    832   1.3  deraadt /*
    833   1.3  deraadt  * format disk
    834   1.3  deraadt  */
    835   1.3  deraadt int
    836   1.3  deraadt sd_format(int unit, struct scsi_format_parms *f, int flags, int type)
    837   1.3  deraadt {
    838   1.3  deraadt 	struct scsi_prevent scsi_cmd;
    839   1.3  deraadt 
    840   1.3  deraadt 	bzero(&scsi_cmd, sizeof(scsi_cmd));
    841   1.3  deraadt 	scsi_cmd.op_code = FORMAT_DISK;
    842   1.3  deraadt 	scsi_cmd.prevent=  type;
    843   1.3  deraadt 	return sd_scsi_cmd(unit, (struct scsi_generic *)&scsi_cmd,
    844   1.3  deraadt 	    sizeof(scsi_cmd), (u_char *)f, sizeof *f, 500000000, flags);
    845   1.1      cgd }
    846   1.1      cgd 
    847   1.3  deraadt /*
    848   1.3  deraadt  * Prevent or allow the user to remove the tape
    849   1.3  deraadt  */
    850   1.3  deraadt int
    851   1.3  deraadt sd_prevent(int unit, int type, int flags)
    852   1.1      cgd {
    853   1.1      cgd 	struct	scsi_prevent	scsi_cmd;
    854   1.1      cgd 
    855   1.1      cgd 	bzero(&scsi_cmd, sizeof(scsi_cmd));
    856   1.1      cgd 	scsi_cmd.op_code = PREVENT_ALLOW;
    857   1.1      cgd 	scsi_cmd.prevent=type;
    858   1.3  deraadt 	return sd_scsi_cmd(unit, (struct scsi_generic *)&scsi_cmd,
    859   1.3  deraadt 	    sizeof(scsi_cmd), 0, 0, 5000, flags);
    860   1.3  deraadt }
    861   1.3  deraadt 
    862   1.3  deraadt /*
    863   1.3  deraadt  * Get scsi driver to send a "start up" command
    864   1.3  deraadt  */
    865   1.3  deraadt int
    866   1.3  deraadt sd_start_unit(int unit, int flags)
    867   1.1      cgd {
    868   1.1      cgd 	struct scsi_start_stop scsi_cmd;
    869   1.1      cgd 
    870   1.1      cgd 	bzero(&scsi_cmd, sizeof(scsi_cmd));
    871   1.1      cgd 	scsi_cmd.op_code = START_STOP;
    872   1.1      cgd 	scsi_cmd.start = 1;
    873   1.1      cgd 
    874   1.3  deraadt 	return sd_scsi_cmd(unit, (struct scsi_generic *)&scsi_cmd,
    875   1.3  deraadt 	    sizeof(scsi_cmd), 0, 0, 2000, flags);
    876   1.3  deraadt }
    877   1.3  deraadt 
    878   1.3  deraadt /*
    879   1.3  deraadt  * Tell the device to map out a defective block
    880   1.3  deraadt  */
    881   1.3  deraadt int
    882   1.3  deraadt sd_reassign_blocks(int unit, int block)
    883   1.1      cgd {
    884   1.3  deraadt 	struct scsi_reassign_blocks_data rbdata;
    885   1.3  deraadt 	struct scsi_reassign_blocks scsi_cmd;
    886   1.1      cgd 
    887   1.1      cgd 	bzero(&scsi_cmd, sizeof(scsi_cmd));
    888   1.1      cgd 	bzero(&rbdata, sizeof(rbdata));
    889   1.1      cgd 	scsi_cmd.op_code = REASSIGN_BLOCKS;
    890   1.1      cgd 
    891   1.1      cgd 	rbdata.length_msb = 0;
    892   1.1      cgd 	rbdata.length_lsb = sizeof(rbdata.defect_descriptor[0]);
    893   1.1      cgd 	rbdata.defect_descriptor[0].dlbaddr_3 = ((block >> 24) & 0xff);
    894   1.1      cgd 	rbdata.defect_descriptor[0].dlbaddr_2 = ((block >> 16) & 0xff);
    895   1.1      cgd 	rbdata.defect_descriptor[0].dlbaddr_1 = ((block >>  8) & 0xff);
    896   1.1      cgd 	rbdata.defect_descriptor[0].dlbaddr_0 = ((block      ) & 0xff);
    897   1.1      cgd 
    898   1.3  deraadt 	return sd_scsi_cmd(unit, (struct scsi_generic *)&scsi_cmd,
    899   1.3  deraadt 	    sizeof(scsi_cmd), (u_char *)&rbdata, sizeof(rbdata), 5000, 0);
    900   1.1      cgd }
    901   1.1      cgd 
    902   1.1      cgd #define b2tol(a)	(((unsigned)(a##_1) << 8) + (unsigned)a##_0 )
    903   1.1      cgd 
    904   1.3  deraadt /*
    905   1.3  deraadt  * Get the scsi driver to send a full inquiry to the
    906   1.3  deraadt  * device and use the results to fill out the disk
    907   1.3  deraadt  * parameter structure.
    908   1.3  deraadt  */
    909   1.3  deraadt int
    910   1.3  deraadt sd_get_parms(int unit, int flags)
    911   1.1      cgd {
    912   1.3  deraadt 	struct sd_data *sd = sd_data[unit];
    913   1.1      cgd 	struct disk_parms *disk_parms = &sd->params;
    914   1.3  deraadt 	struct scsi_mode_sense	scsi_cmd;
    915   1.3  deraadt 	struct scsi_mode_sense_data {
    916   1.3  deraadt 		struct	scsi_mode_header header;
    917   1.3  deraadt 		struct	blk_desc blk_desc;
    918   1.3  deraadt 		union	disk_pages pages;
    919   1.3  deraadt 	} scsi_sense;
    920   1.1      cgd 	int sectors;
    921   1.1      cgd 
    922   1.3  deraadt 	/* First check if we have it all loaded */
    923   1.4  deraadt 	if(!sd)
    924   1.4  deraadt 		return 0;
    925   1.3  deraadt 	if(sd->flags & SDVALID)
    926   1.3  deraadt 		return 0;
    927   1.3  deraadt 
    928   1.3  deraadt 	/* First do a mode sense page 3 */
    929   1.3  deraadt 	if (sd_debug) {
    930   1.1      cgd 		bzero(&scsi_cmd, sizeof(scsi_cmd));
    931   1.1      cgd 		scsi_cmd.op_code = MODE_SENSE;
    932   1.1      cgd 		scsi_cmd.page_code = 3;
    933   1.1      cgd 		scsi_cmd.length = 0x24;
    934   1.3  deraadt 
    935   1.3  deraadt 		/*
    936   1.3  deraadt 		 * do the command, but we don't need the results
    937   1.3  deraadt 		 * just print them for our interest's sake
    938   1.3  deraadt 		 */
    939   1.3  deraadt 		if (sd_scsi_cmd(unit, (struct scsi_generic *)&scsi_cmd,
    940   1.3  deraadt 		    sizeof(scsi_cmd), (u_char *)&scsi_sense, sizeof(scsi_sense),
    941   1.3  deraadt 		    2000, flags) != 0) {
    942   1.1      cgd 			printf("could not mode sense (3) for unit %d\n", unit);
    943   1.3  deraadt 			return ENXIO;
    944   1.1      cgd 		}
    945   1.3  deraadt 		printf("unit %d: %d trk/zn, %d altsec/zn, %d alttrk/zn, %d alttrk/lun\n",
    946   1.3  deraadt 			unit, b2tol(scsi_sense.pages.disk_format.trk_z),
    947   1.1      cgd 			b2tol(scsi_sense.pages.disk_format.alt_sec),
    948   1.1      cgd 			b2tol(scsi_sense.pages.disk_format.alt_trk_z),
    949   1.1      cgd 			b2tol(scsi_sense.pages.disk_format.alt_trk_v));
    950   1.3  deraadt 		printf("  %d sec/trk, %d byte/sec, %d interleave, %d %d bytes/log_blk\n",
    951   1.1      cgd 			b2tol(scsi_sense.pages.disk_format.ph_sec_t),
    952   1.1      cgd 			b2tol(scsi_sense.pages.disk_format.bytes_s),
    953   1.1      cgd 			b2tol(scsi_sense.pages.disk_format.interleave),
    954   1.1      cgd 			sd_size(unit, flags),
    955   1.3  deraadt 			_3btol((u_char *)scsi_sense.blk_desc.blklen));
    956   1.1      cgd 	}
    957   1.1      cgd 
    958   1.1      cgd 
    959   1.3  deraadt 	/* do a "mode sense page 4" */
    960   1.1      cgd 	bzero(&scsi_cmd, sizeof(scsi_cmd));
    961   1.1      cgd 	scsi_cmd.op_code = MODE_SENSE;
    962   1.1      cgd 	scsi_cmd.page_code = 4;
    963   1.1      cgd 	scsi_cmd.length = 0x20;
    964   1.3  deraadt 
    965   1.3  deraadt 	/*
    966   1.3  deraadt 	 * If the command worked, use the results to fill out
    967   1.3  deraadt 	 * the parameter structure
    968   1.3  deraadt 	 */
    969   1.3  deraadt 	if (sd_scsi_cmd(unit, (struct scsi_generic *)&scsi_cmd,
    970   1.3  deraadt 	    sizeof(scsi_cmd), (u_char *)&scsi_sense, sizeof(scsi_sense),
    971   1.3  deraadt 	    2000, flags) != 0) {
    972   1.1      cgd 		printf("could not mode sense (4) for unit %d\n", unit);
    973   1.1      cgd 		printf(" using ficticious geometry\n");
    974   1.1      cgd 		sectors = sd_size(unit, flags);
    975   1.1      cgd 		disk_parms->heads = 64;
    976   1.1      cgd 		disk_parms->sectors = 32;
    977   1.1      cgd 		disk_parms->cyls = sectors/(64 * 32);
    978   1.1      cgd 		disk_parms->secsiz = SECSIZE;
    979   1.3  deraadt 	} else {
    980   1.3  deraadt 		if (sd_debug) {
    981   1.3  deraadt 			printf("  %d cyl, %d head, %d precomp, %d redwrite, %d land\n",
    982   1.3  deraadt 			_3btol((u_char *)&scsi_sense.pages.rigid_geometry.ncyl_2),
    983   1.1      cgd 			scsi_sense.pages.rigid_geometry.nheads,
    984   1.1      cgd 			b2tol(scsi_sense.pages.rigid_geometry.st_cyl_wp),
    985   1.1      cgd 			b2tol(scsi_sense.pages.rigid_geometry.st_cyl_rwc),
    986   1.1      cgd 			b2tol(scsi_sense.pages.rigid_geometry.land_zone));
    987   1.1      cgd 		}
    988   1.1      cgd 
    989   1.3  deraadt 		/*
    990   1.3  deraadt 		 * KLUDGE!!(for zone recorded disks)
    991   1.3  deraadt 		 * give a number of sectors so that sec * trks * cyls
    992   1.3  deraadt 		 * is <= disk_size
    993   1.3  deraadt 		 */
    994   1.1      cgd 		disk_parms->heads = scsi_sense.pages.rigid_geometry.nheads;
    995   1.3  deraadt 		disk_parms->cyls =
    996   1.3  deraadt 			_3btol((u_char *)&scsi_sense.pages.rigid_geometry.ncyl_2);
    997   1.3  deraadt 		disk_parms->secsiz = _3btol((u_char *)&scsi_sense.blk_desc.blklen);
    998   1.1      cgd 
    999   1.1      cgd 		sectors = sd_size(unit, flags);
   1000   1.1      cgd 		sectors /= disk_parms->cyls;
   1001   1.1      cgd 		sectors /= disk_parms->heads;
   1002   1.1      cgd 		disk_parms->sectors = sectors; /* dubious on SCSI*/
   1003   1.1      cgd 	}
   1004   1.1      cgd 
   1005   1.3  deraadt 	disk_parms->disksize = disk_parms->sectors * disk_parms->heads *
   1006   1.3  deraadt 		disk_parms->cyls;
   1007   1.1      cgd 	sd->flags |= SDVALID;
   1008   1.3  deraadt 	return 0;
   1009   1.1      cgd }
   1010   1.1      cgd 
   1011   1.3  deraadt /*
   1012   1.3  deraadt  * close the device.. only called if we are the LAST
   1013   1.3  deraadt  * occurence of an open device
   1014   1.3  deraadt  */
   1015   1.3  deraadt int
   1016   1.3  deraadt sdclose(dev_t dev)
   1017   1.1      cgd {
   1018   1.3  deraadt 	struct sd_data *sd;
   1019   1.1      cgd 	unsigned char unit, part;
   1020   1.1      cgd 	unsigned int old_priority;
   1021   1.1      cgd 
   1022   1.1      cgd 	unit = UNIT(dev);
   1023   1.1      cgd 	part = PARTITION(dev);
   1024   1.3  deraadt 	sd = sd_data[unit];
   1025   1.3  deraadt 	sd->partflags[part] &= ~SDOPEN;
   1026   1.3  deraadt 	sd->openparts &= ~(1 << part);
   1027   1.3  deraadt 	if(sd->openparts == 0)
   1028   1.3  deraadt 		sd_prevent(unit, PR_ALLOW, SCSI_SILENT|SCSI_ERR_OK);
   1029   1.3  deraadt 	return 0;
   1030   1.3  deraadt }
   1031   1.3  deraadt 
   1032   1.3  deraadt /*
   1033   1.3  deraadt  * ask the scsi driver to perform a command for us.
   1034   1.3  deraadt  * Call it through the switch table, and tell it which
   1035   1.3  deraadt  * sub-unit we want, and what target and lu we wish to
   1036   1.3  deraadt  * talk to. Also tell it where to find the command
   1037   1.3  deraadt  * how long int is.
   1038   1.3  deraadt  * Also tell it where to read/write the data, and how
   1039   1.3  deraadt  * long the data is supposed to be
   1040   1.3  deraadt  */
   1041   1.3  deraadt int
   1042   1.3  deraadt sd_scsi_cmd(int unit, struct scsi_generic *scsi_cmd, int cmdlen,
   1043   1.3  deraadt 	u_char *data_addr, int datalen, int timeout, int flags)
   1044   1.1      cgd {
   1045   1.3  deraadt 	struct sd_data *sd = sd_data[unit];
   1046   1.1      cgd 	struct	scsi_xfer *xs;
   1047   1.3  deraadt 	int retval, s;
   1048   1.3  deraadt 
   1049   1.3  deraadt 	if(scsi_debug & PRINTROUTINES)
   1050   1.3  deraadt 		printf("\nsd_scsi_cmd%d ",unit);
   1051   1.3  deraadt 	if(!sd->sc_sw) {
   1052   1.3  deraadt 		printf("sd%d: not set up\n",unit);
   1053   1.3  deraadt 		return EINVAL;
   1054   1.3  deraadt 	}
   1055   1.3  deraadt 
   1056   1.3  deraadt 	xs = sd_get_xs(unit,flags); /* should wait unless booting */
   1057   1.3  deraadt 	if(!xs) {
   1058   1.3  deraadt 		printf("sd_scsi_cmd%d: controller busy"
   1059   1.3  deraadt  				" (this should never happen)\n",unit);
   1060   1.3  deraadt 		return EBUSY;
   1061   1.3  deraadt 	}
   1062   1.3  deraadt 
   1063   1.3  deraadt 	xs->flags |= INUSE;
   1064   1.3  deraadt 	xs->flags	|=	flags;
   1065   1.3  deraadt 	xs->adapter =	sd->ctlr;
   1066   1.3  deraadt 	xs->targ =	sd->targ;
   1067   1.3  deraadt 	xs->lu =	sd->lu;
   1068   1.3  deraadt 	xs->retries =	SD_RETRIES;
   1069   1.3  deraadt 	xs->timeout =	timeout;
   1070   1.3  deraadt 	xs->cmd =	scsi_cmd;
   1071   1.3  deraadt 	xs->cmdlen =	cmdlen;
   1072   1.3  deraadt 	xs->data =	data_addr;
   1073   1.3  deraadt 	xs->datalen =	datalen;
   1074   1.3  deraadt 	xs->resid =	datalen;
   1075   1.3  deraadt 	xs->when_done =	(flags & SCSI_NOMASK) ?(int (*)())0 : sd_done;
   1076   1.3  deraadt 	xs->done_arg =	unit;
   1077   1.3  deraadt 	xs->done_arg2 =	(int)xs;
   1078   1.3  deraadt 
   1079   1.3  deraadt retry:
   1080   1.3  deraadt 	xs->error =	XS_NOERROR;
   1081   1.3  deraadt 	xs->bp =	0;
   1082   1.3  deraadt 	retval = (*(sd->sc_sw->scsi_cmd))(xs);
   1083   1.3  deraadt 	switch(retval) {
   1084   1.3  deraadt 	case SUCCESSFULLY_QUEUED:
   1085   1.3  deraadt 		s = splbio();
   1086   1.3  deraadt 		while(!(xs->flags & ITSDONE))
   1087  1.18  mycroft 			tsleep((caddr_t)xs, PRIBIO+1, "sd_cmd", 0);
   1088   1.3  deraadt 		splx(s);
   1089   1.3  deraadt 	case HAD_ERROR:
   1090   1.3  deraadt 		/*printf("err = %d ", xs->error);*/
   1091   1.3  deraadt 		switch(xs->error) {
   1092   1.3  deraadt 		case XS_NOERROR:
   1093   1.3  deraadt 			retval = ESUCCESS;
   1094   1.3  deraadt 			break;
   1095   1.3  deraadt 		case XS_SENSE:
   1096   1.3  deraadt 			retval = sd_interpret_sense(unit, xs);
   1097   1.1      cgd 			break;
   1098   1.3  deraadt 		case XS_DRIVER_STUFFUP:
   1099   1.3  deraadt 			retval = EIO;
   1100   1.1      cgd 			break;
   1101   1.3  deraadt 		case XS_TIMEOUT:
   1102   1.3  deraadt 		case XS_BUSY:
   1103   1.3  deraadt 			if(xs->retries-- ) {
   1104   1.1      cgd 				xs->flags &= ~ITSDONE;
   1105   1.1      cgd 				goto retry;
   1106   1.1      cgd 			}
   1107   1.1      cgd 			retval = EIO;
   1108   1.1      cgd 			break;
   1109   1.1      cgd 		default:
   1110   1.1      cgd 			retval = EIO;
   1111   1.3  deraadt 			printf("sd%d: unknown error category from scsi driver\n", unit);
   1112   1.3  deraadt 		}
   1113   1.3  deraadt 		break;
   1114   1.3  deraadt 	case COMPLETE:
   1115   1.3  deraadt 		retval = ESUCCESS;
   1116   1.3  deraadt 		break;
   1117   1.3  deraadt 	case 	TRY_AGAIN_LATER:
   1118   1.3  deraadt 		if(xs->retries-- ) {
   1119   1.3  deraadt 			xs->flags &= ~ITSDONE;
   1120   1.3  deraadt 			goto retry;
   1121   1.1      cgd 		}
   1122   1.3  deraadt 		retval = EIO;
   1123   1.3  deraadt 		break;
   1124   1.3  deraadt 	default:
   1125   1.3  deraadt 		retval = EIO;
   1126   1.1      cgd 	}
   1127   1.3  deraadt 
   1128   1.3  deraadt 	sd_free_xs(unit, xs, flags);
   1129   1.3  deraadt 	sdstart(unit);			/* check if anything is waiting fr the xs */
   1130   1.3  deraadt 	return retval;
   1131   1.1      cgd }
   1132   1.3  deraadt 
   1133   1.3  deraadt /*
   1134   1.3  deraadt  * Look at the returned sense and act on the error and detirmine
   1135   1.3  deraadt  * The unix error number to pass back... (0 = report no error)
   1136   1.3  deraadt  */
   1137   1.3  deraadt int
   1138   1.3  deraadt sd_interpret_sense(int unit, struct scsi_xfer *xs)
   1139   1.3  deraadt {
   1140   1.3  deraadt 	struct sd_data *sd = sd_data[unit];
   1141   1.3  deraadt 	struct scsi_sense_data *sense;
   1142   1.3  deraadt 	int key, silent;
   1143   1.3  deraadt 
   1144   1.3  deraadt 	/* If the flags say errs are ok, then always return ok. */
   1145   1.3  deraadt 	if (xs->flags & SCSI_ERR_OK)
   1146   1.3  deraadt 		return ESUCCESS;
   1147   1.1      cgd 	silent = (xs->flags & SCSI_SILENT);
   1148   1.1      cgd 
   1149   1.1      cgd 	sense = &(xs->sense);
   1150   1.3  deraadt 	switch(sense->error_class) {
   1151   1.1      cgd 	case 7:
   1152   1.3  deraadt 		key = sense->ext.extended.sense_key;
   1153   1.3  deraadt 		switch(key) {
   1154   1.3  deraadt 		case 0x0:
   1155   1.3  deraadt 			return ESUCCESS;
   1156   1.3  deraadt 		case 0x1:
   1157   1.3  deraadt 			if(!silent) {
   1158   1.1      cgd 				printf("sd%d: soft error(corrected) ", unit);
   1159   1.3  deraadt 				if(sense->valid) {
   1160   1.1      cgd 			  		printf("block no. %d (decimal)",
   1161  1.12  deraadt 				  		(sense->ext.extended.info[0] <<24) |
   1162  1.12  deraadt 				  		(sense->ext.extended.info[1] <<16) |
   1163  1.12  deraadt 				  		(sense->ext.extended.info[2] <<8) |
   1164   1.3  deraadt 				  		(sense->ext.extended.info[3] ));
   1165   1.1      cgd 				}
   1166   1.1      cgd 				printf("\n");
   1167   1.1      cgd 			}
   1168   1.3  deraadt 			return ESUCCESS;
   1169   1.3  deraadt 		case 0x2:
   1170   1.1      cgd 			if(!silent)
   1171   1.3  deraadt 				printf("sd%d: not ready\n ", unit);
   1172   1.3  deraadt 			return ENODEV;
   1173   1.3  deraadt 		case 0x3:
   1174   1.3  deraadt 			if(!silent) {
   1175   1.1      cgd 				printf("sd%d: medium error ", unit);
   1176   1.3  deraadt 				if(sense->valid) {
   1177   1.1      cgd 			  		printf("block no. %d (decimal)",
   1178  1.12  deraadt 				  		(sense->ext.extended.info[0] <<24) |
   1179  1.12  deraadt 				  		(sense->ext.extended.info[1] <<16) |
   1180  1.12  deraadt 				  		(sense->ext.extended.info[2] <<8) |
   1181   1.3  deraadt 				  		(sense->ext.extended.info[3] ));
   1182   1.1      cgd 				}
   1183   1.1      cgd 				printf("\n");
   1184   1.1      cgd 			}
   1185   1.3  deraadt 			return EIO;
   1186   1.3  deraadt 		case 0x4:
   1187   1.3  deraadt 			if(!silent)
   1188   1.3  deraadt 				printf("sd%d: non-media hardware failure\n ", unit);
   1189   1.3  deraadt 			return EIO;
   1190   1.3  deraadt 		case 0x5:
   1191   1.3  deraadt 			if(!silent)
   1192   1.3  deraadt 				printf("sd%d: illegal request\n ", unit);
   1193   1.3  deraadt 			return EINVAL;
   1194   1.3  deraadt 		case 0x6:
   1195   1.3  deraadt 			/*
   1196   1.3  deraadt 			 * If we are not open, then this is not an error
   1197   1.3  deraadt 			 * as we don't have state yet. Either way, make
   1198   1.3  deraadt 			 * sure that we don't have any residual state
   1199   1.3  deraadt 			 */
   1200   1.1      cgd 			if(!silent)
   1201   1.6  mycroft 				printf("sd%d: reset\n", unit);
   1202   1.3  deraadt 			sd->flags &= ~(SDVALID | SDHAVELABEL);
   1203   1.3  deraadt 			if (sd->openparts)
   1204   1.3  deraadt 				return EIO;
   1205   1.3  deraadt 			return ESUCCESS;	/* not an error if nothing's open */
   1206   1.3  deraadt 		case 0x7:
   1207   1.3  deraadt 			if(!silent) {
   1208   1.3  deraadt 				printf("sd%d: attempted protection violation ", unit);
   1209   1.3  deraadt 				if(sense->valid) {
   1210   1.1      cgd 					printf("block no. %d (decimal)\n",
   1211  1.12  deraadt 				  		(sense->ext.extended.info[0] <<24) |
   1212  1.12  deraadt 				  		(sense->ext.extended.info[1] <<16) |
   1213  1.12  deraadt 				  		(sense->ext.extended.info[2] <<8) |
   1214   1.3  deraadt 				  		(sense->ext.extended.info[3] ));
   1215   1.1      cgd 				}
   1216   1.1      cgd 				printf("\n");
   1217   1.1      cgd 			}
   1218   1.3  deraadt 			return EACCES;
   1219   1.3  deraadt 		case 0x8:
   1220   1.3  deraadt 			if(!silent) {
   1221   1.3  deraadt 				printf("sd%d: block wrong state (worm)\n ", unit);
   1222   1.3  deraadt 				if(sense->valid) {
   1223   1.1      cgd 			  		printf("block no. %d (decimal)\n",
   1224  1.12  deraadt 				  		(sense->ext.extended.info[0] <<24) |
   1225  1.12  deraadt 				  		(sense->ext.extended.info[1] <<16) |
   1226  1.12  deraadt 				  		(sense->ext.extended.info[2] <<8) |
   1227   1.3  deraadt 				  		(sense->ext.extended.info[3] ));
   1228   1.1      cgd 				}
   1229   1.1      cgd 				printf("\n");
   1230   1.1      cgd 			}
   1231   1.3  deraadt 			return EIO;
   1232   1.3  deraadt 		case 0x9:
   1233   1.3  deraadt 			if(!silent)
   1234   1.3  deraadt 				printf("sd%d: vendor unique\n", unit);
   1235   1.3  deraadt 			return EIO;
   1236   1.3  deraadt 		case 0xa:
   1237   1.1      cgd 			if(!silent)
   1238   1.3  deraadt 				printf("sd%d: copy aborted\n ", unit);
   1239   1.3  deraadt 			return EIO;
   1240   1.3  deraadt 		case 0xb:
   1241   1.3  deraadt 			if(!silent)
   1242   1.3  deraadt 				printf("sd%d: command aborted\n ", unit);
   1243   1.3  deraadt 			return EIO;
   1244   1.3  deraadt 		case 0xc:
   1245   1.3  deraadt 			if(!silent) {
   1246   1.3  deraadt 				printf("sd%d: search returned\n ", unit);
   1247   1.3  deraadt 				if(sense->valid) {
   1248   1.1      cgd 			  		printf("block no. %d (decimal)\n",
   1249  1.12  deraadt 				  		(sense->ext.extended.info[0] <<24) |
   1250  1.12  deraadt 				  		(sense->ext.extended.info[1] <<16) |
   1251  1.12  deraadt 				  		(sense->ext.extended.info[2] <<8) |
   1252   1.3  deraadt 				  		(sense->ext.extended.info[3] ));
   1253   1.1      cgd 				}
   1254   1.1      cgd 				printf("\n");
   1255   1.1      cgd 			}
   1256   1.3  deraadt 			return ESUCCESS;
   1257   1.3  deraadt 		case 0xd:
   1258   1.1      cgd 			if(!silent)
   1259   1.3  deraadt 				printf("sd%d: volume overflow\n ", unit);
   1260   1.3  deraadt 			return ENOSPC;
   1261   1.3  deraadt 		case 0xe:
   1262   1.3  deraadt 			if(!silent) {
   1263   1.3  deraadt 				printf("sd%d: verify miscompare\n ", unit);
   1264   1.3  deraadt 				if(sense->valid) {
   1265   1.1      cgd 			  		printf("block no. %d (decimal)\n",
   1266  1.12  deraadt 				  		(sense->ext.extended.info[0] <<24) |
   1267  1.12  deraadt 				  		(sense->ext.extended.info[1] <<16) |
   1268  1.12  deraadt 				  		(sense->ext.extended.info[2] <<8) |
   1269   1.3  deraadt 				  		(sense->ext.extended.info[3] ));
   1270   1.1      cgd 				}
   1271   1.1      cgd 				printf("\n");
   1272   1.1      cgd 			}
   1273   1.3  deraadt 			return EIO;
   1274   1.3  deraadt 		case 0xf:
   1275   1.3  deraadt 			if(!silent)
   1276   1.3  deraadt 				printf("sd%d: unknown error key\n ", unit);
   1277   1.3  deraadt 			return EIO;
   1278   1.1      cgd 		}
   1279   1.1      cgd 		break;
   1280   1.1      cgd 	case 0:
   1281   1.1      cgd 	case 1:
   1282   1.1      cgd 	case 2:
   1283   1.1      cgd 	case 3:
   1284   1.1      cgd 	case 4:
   1285   1.1      cgd 	case 5:
   1286   1.1      cgd 	case 6:
   1287   1.3  deraadt 		if(!silent)printf("sd%d: error class %d code %d\n", unit,
   1288   1.3  deraadt 			sense->error_class, sense->error_code);
   1289   1.1      cgd 		if(sense->valid)
   1290   1.3  deraadt 			if(!silent)
   1291   1.3  deraadt 				printf("block no. %d (decimal)\n",
   1292  1.12  deraadt 					(sense->ext.unextended.blockhi <<16)
   1293  1.12  deraadt 					+ (sense->ext.unextended.blockmed <<8)
   1294   1.3  deraadt 					+ (sense->ext.unextended.blocklow ));
   1295   1.3  deraadt 		return EIO;
   1296   1.1      cgd 	}
   1297   1.3  deraadt 	return 0;		/* XXX? */
   1298   1.1      cgd }
   1299   1.1      cgd 
   1300   1.1      cgd int
   1301   1.1      cgd sdsize(dev_t dev)
   1302   1.1      cgd {
   1303   1.1      cgd 	int unit = UNIT(dev), part = PARTITION(dev), val;
   1304   1.1      cgd 	struct sd_data *sd;
   1305   1.1      cgd 
   1306   1.1      cgd 	if (unit >= NSD)
   1307   1.3  deraadt 		return -1;
   1308   1.3  deraadt 	if(!sd_data[unit])
   1309   1.3  deraadt 		return -1;
   1310   1.3  deraadt 
   1311   1.3  deraadt 	sd = sd_data[unit];
   1312   1.3  deraadt 	if((sd->flags & SDINIT) == 0)
   1313   1.3  deraadt 		return -1;
   1314   1.3  deraadt 
   1315   1.3  deraadt 	if( sd==0 || (sd->flags & SDHAVELABEL)==0 )
   1316   1.3  deraadt 		val = sdopen(MAKESDDEV(major(dev), unit, RAW_PART));
   1317   1.3  deraadt 	if ( val!=0 || sd->flags & SDWRITEPROT)
   1318   1.3  deraadt 		return -1;
   1319   1.1      cgd 
   1320   1.3  deraadt 	return (int)sd->disklabel.d_partitions[part].p_size;
   1321   1.1      cgd }
   1322   1.1      cgd 
   1323   1.1      cgd sddump()
   1324   1.1      cgd {
   1325   1.3  deraadt 	printf("sddump() -- not implemented\n");
   1326   1.3  deraadt 	return -1;
   1327   1.1      cgd }
   1328