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