Home | History | Annotate | Line # | Download | only in scsipi
scsiconf.c revision 1.10
      1   1.1      cgd /*
      2   1.1      cgd  * Written by Julian Elischer (julian (at) tfs.com)
      3   1.5  deraadt  * for TRW Financial Systems for use under the MACH(2.5) operating system.
      4   1.2  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.7      cgd  *
     16  1.10  mycroft  *	$Id: scsiconf.c,v 1.10 1993/12/17 08:50:49 mycroft Exp $
     17   1.1      cgd  */
     18   1.1      cgd 
     19  1.10  mycroft #include <sys/types.h>
     20  1.10  mycroft #include <sys/param.h>
     21  1.10  mycroft #include <sys/systm.h>
     22  1.10  mycroft #include <sys/errno.h>
     23  1.10  mycroft #include <sys/ioctl.h>
     24  1.10  mycroft #include <sys/buf.h>
     25  1.10  mycroft #include <sys/proc.h>
     26  1.10  mycroft #include <sys/user.h>
     27  1.10  mycroft #include <sys/dkbad.h>
     28  1.10  mycroft #include <sys/disklabel.h>
     29  1.10  mycroft 
     30  1.10  mycroft #include <scsi/scsi_all.h>
     31  1.10  mycroft #include <scsi/scsiconf.h>
     32   1.1      cgd 
     33   1.1      cgd #include "st.h"
     34   1.1      cgd #include "sd.h"
     35   1.1      cgd #include "ch.h"
     36   1.1      cgd #include "cd.h"
     37   1.1      cgd #define	NBLL 0
     38   1.1      cgd #define	NCALS 0
     39   1.1      cgd #define	NKIL 0
     40   1.1      cgd 
     41   1.1      cgd #if NSD > 0
     42   1.2  deraadt extern int sdattach();
     43   1.1      cgd #endif NSD
     44   1.1      cgd #if NST > 0
     45   1.2  deraadt extern int stattach();
     46   1.1      cgd #endif NST
     47   1.1      cgd #if NCH > 0
     48   1.2  deraadt extern int chattach();
     49   1.1      cgd #endif NCH
     50   1.1      cgd #if NCD > 0
     51   1.2  deraadt extern int cdattach();
     52   1.1      cgd #endif NCD
     53   1.1      cgd #if NBLL > 0
     54   1.2  deraadt extern int bllattach();
     55   1.1      cgd #endif NBLL
     56   1.1      cgd #if NCALS > 0
     57   1.2  deraadt extern int calsattach();
     58   1.1      cgd #endif NCALS
     59   1.1      cgd #if NKIL > 0
     60   1.2  deraadt extern int kil_attach();
     61   1.1      cgd #endif NKIL
     62   1.1      cgd 
     63   1.2  deraadt struct scsidevs knowndevs[] = {
     64   1.1      cgd #if NSD > 0
     65   1.2  deraadt 	{
     66   1.2  deraadt 		SC_TSD, T_DIRECT, T_FIXED, "standard", "any" ,"any",
     67   1.2  deraadt 		sdattach, "sd" ,SC_ONE_LU
     68   1.2  deraadt 	}, {
     69   1.2  deraadt 		SC_TSD, T_DIRECT, T_FIXED, "MAXTOR  ", "XT-4170S	", "B5A ",
     70   1.2  deraadt 		sdattach, "mx1", SC_ONE_LU
     71   1.2  deraadt 	},
     72   1.1      cgd #endif NSD
     73   1.1      cgd #if NST > 0
     74   1.2  deraadt 	{
     75   1.2  deraadt 		SC_TST, T_SEQUENTIAL, T_REMOV, "standard", "any", "any",
     76   1.2  deraadt 		stattach, "st" ,SC_ONE_LU
     77   1.2  deraadt 	},
     78   1.1      cgd #endif NST
     79   1.2  deraadt #if NCD > 0
     80   1.2  deraadt 	{
     81   1.2  deraadt 		SC_TCD, T_READONLY, T_REMOV, "SONY    ", "CD-ROM CDU-8012 ", "3.1a",
     82   1.2  deraadt 		cdattach, "cd", SC_ONE_LU
     83   1.8  deraadt 	}, {
     84   1.8  deraadt 		SC_TCD, T_READONLY, T_REMOV, "PIONEER ", "CD-ROM DRM-600  ", "any",
     85   1.8  deraadt 		cdattach, "cd", SC_MORE_LUS
     86   1.2  deraadt 	},
     87   1.2  deraadt #endif NCD
     88   1.1      cgd #if NCALS > 0
     89   1.2  deraadt 	{
     90   1.2  deraadt 		-1, T_PROCESSOR, T_FIXED, "standard" , "any" ,"any",
     91   1.2  deraadt 		calsattach, "cals", SC_MORE_LUS
     92   1.8  deraadt 	}
     93   1.1      cgd #endif NCALS
     94   1.1      cgd #if NCH > 0
     95   1.2  deraadt 	{
     96   1.2  deraadt 		-1, T_CHANGER, T_REMOV, "standard", "any", "any",
     97   1.2  deraadt 		chattach, "ch", SC_ONE_LU
     98   1.2  deraadt 	},
     99   1.1      cgd #endif NCH
    100   1.1      cgd #if NBLL > 0
    101   1.2  deraadt 	{
    102   1.2  deraadt 		-1, T_PROCESSOR, T_FIXED, "AEG     ", "READER	  ", "V1.0",
    103   1.2  deraadt 		bllattach, "bll", SC_MORE_LUS
    104   1.2  deraadt 	},
    105   1.1      cgd #endif NBLL
    106   1.1      cgd #if NKIL > 0
    107   1.2  deraadt 	{
    108   1.2  deraadt 		-1, T_SCANNER, T_FIXED, "KODAK   ", "IL Scanner 900  ", "any",
    109   1.2  deraadt 		kil_attach, "kil", SC_ONE_LU
    110   1.2  deraadt 	},
    111   1.1      cgd #endif NKIL
    112   1.2  deraadt };
    113   1.2  deraadt 
    114   1.2  deraadt /* controls debug level within the scsi subsystem: see scsiconf.h */
    115   1.2  deraadt int scsi_debug	= 0;
    116   1.1      cgd 
    117   1.2  deraadt struct scsidevs *
    118   1.2  deraadt scsi_probe(int masunit, struct scsi_switch *sw, int physid, int type, int want)
    119   1.1      cgd {
    120   1.2  deraadt 	static struct scsi_inquiry_data inqbuf;
    121   1.2  deraadt 	struct scsidevs *ret = (struct scsidevs *)0;
    122   1.2  deraadt 	int targ = physid >> 3;
    123   1.2  deraadt 	int lun = physid & 7;
    124   1.2  deraadt 	char *qtype=NULL, *dtype=NULL, *desc;
    125   1.2  deraadt 	char manu[9], model[17], revision[5];
    126   1.2  deraadt 	int len;
    127   1.2  deraadt 
    128   1.2  deraadt 	bzero(&inqbuf, sizeof inqbuf);
    129   1.2  deraadt 
    130   1.2  deraadt 	/*printf("probe: %s%d targ %d lun %d\n",
    131   1.2  deraadt 		sw->name, masunit, targ, lun);*/
    132   1.2  deraadt 
    133   1.2  deraadt 	if( scsi_ready(masunit, targ, lun, sw,
    134   1.2  deraadt 	    SCSI_NOSLEEP | SCSI_NOMASK) != COMPLETE)
    135   1.9  deraadt 		return (struct scsidevs *)-1;
    136   1.2  deraadt 
    137   1.2  deraadt 	if( scsi_inquire(masunit, targ, lun, sw, (u_char *)&inqbuf,
    138   1.2  deraadt 	    SCSI_NOSLEEP | SCSI_NOMASK) != COMPLETE)
    139   1.5  deraadt 		return (struct scsidevs *)0;
    140   1.2  deraadt 
    141   1.2  deraadt 	if( inqbuf.device_qualifier==3 && inqbuf.device_type==T_NODEVICE)
    142   1.5  deraadt 		return (struct scsidevs *)0;
    143   1.2  deraadt 
    144   1.2  deraadt 	switch(inqbuf.device_qualifier) {
    145   1.2  deraadt 	case 0:
    146   1.2  deraadt 		qtype = "";
    147   1.2  deraadt 		break;
    148   1.2  deraadt 	case 1:
    149   1.2  deraadt 		qtype = "Unit not Connected!";
    150   1.2  deraadt 		break;
    151   1.2  deraadt 	case 2:
    152   1.2  deraadt 		qtype =", Reserved Peripheral Qualifier!";
    153   1.2  deraadt 		break;
    154   1.2  deraadt 	case 3:
    155   1.2  deraadt 		qtype = ", The Target can't support this Unit!";
    156   1.2  deraadt 		break;
    157   1.2  deraadt 	default:
    158   1.2  deraadt 		dtype = "vendor specific";
    159   1.2  deraadt 		qtype = "";
    160   1.2  deraadt 		break;
    161   1.2  deraadt 	}
    162   1.2  deraadt 
    163   1.2  deraadt 	if (dtype == NULL) {
    164   1.2  deraadt 		switch(inqbuf.device_type) {
    165   1.2  deraadt 		case T_DIRECT:
    166   1.2  deraadt 			dtype = "direct";
    167   1.2  deraadt 			break;
    168   1.2  deraadt 		case T_SEQUENTIAL:
    169   1.2  deraadt 			dtype = "seq";
    170   1.2  deraadt 			break;
    171   1.2  deraadt 		case T_PRINTER:
    172   1.2  deraadt 			dtype = "pr";
    173   1.2  deraadt 			break;
    174   1.2  deraadt 		case T_PROCESSOR:
    175   1.2  deraadt 			dtype = "cpu";
    176   1.2  deraadt 			break;
    177   1.2  deraadt 		case T_READONLY:
    178   1.2  deraadt 			dtype = "ro";
    179   1.2  deraadt 			break;
    180   1.2  deraadt 		case T_WORM:
    181   1.2  deraadt 			dtype = "worm";
    182   1.2  deraadt 			break;
    183   1.2  deraadt 		case T_SCANNER:
    184   1.2  deraadt 			dtype = "scan";
    185   1.2  deraadt 			break;
    186   1.2  deraadt 		case T_OPTICAL:
    187   1.2  deraadt 			dtype = "optic";
    188   1.2  deraadt 			break;
    189   1.2  deraadt 		case T_CHANGER:
    190   1.2  deraadt 			dtype = "changer";
    191   1.2  deraadt 			break;
    192   1.2  deraadt 		case T_COMM:
    193   1.2  deraadt 			dtype = "comm";
    194   1.2  deraadt 			break;
    195   1.2  deraadt 		default:
    196   1.2  deraadt 			dtype = "???";
    197   1.2  deraadt 			break;
    198   1.1      cgd 		}
    199   1.1      cgd 	}
    200   1.2  deraadt 
    201   1.2  deraadt 	if(inqbuf.ansii_version > 0) {
    202   1.2  deraadt 		len = inqbuf.additional_length +
    203   1.2  deraadt 			((char *)inqbuf.unused - (char *)&inqbuf);
    204   1.2  deraadt 		if( len > sizeof(struct scsi_inquiry_data) - 1)
    205   1.2  deraadt 			len = sizeof(struct scsi_inquiry_data) - 1;
    206   1.2  deraadt 		desc = inqbuf.vendor;
    207   1.2  deraadt 		desc[len-(desc-(char *)&inqbuf)] = 0;
    208   1.2  deraadt 		strncpy(manu, inqbuf.vendor, sizeof inqbuf.vendor);
    209   1.2  deraadt 		manu[sizeof inqbuf.vendor] = '\0';
    210   1.2  deraadt 		strncpy(model, inqbuf.product, sizeof inqbuf.product);
    211   1.2  deraadt 		model[sizeof inqbuf.product] = '\0';
    212   1.3  mycroft 		strncpy(revision, inqbuf.revision, sizeof inqbuf.revision);
    213   1.3  mycroft 		revision[sizeof inqbuf.revision] = '\0';
    214   1.2  deraadt 	} else {
    215   1.2  deraadt 		desc = "early protocol device";
    216   1.2  deraadt 		strcpy(manu, "????");
    217   1.2  deraadt 		strcpy(model, "");
    218   1.3  mycroft 		strcpy(revision, "");
    219   1.2  deraadt 	}
    220   1.2  deraadt 
    221   1.2  deraadt 	if(want)
    222   1.2  deraadt 		goto print;
    223   1.2  deraadt 
    224   1.2  deraadt 	ret = selectdev(masunit, targ, lun, sw, inqbuf.device_qualifier,
    225   1.3  mycroft 		inqbuf.device_type, inqbuf.removable, manu, model, revision, type);
    226   1.2  deraadt 	if(sw->printed[targ] & (1<<lun))
    227   1.2  deraadt 		return ret;
    228   1.2  deraadt 
    229   1.2  deraadt print:
    230   1.2  deraadt 	printf("%s%d targ %d lun %d: type %d(%s) %s <%s%s%s> SCSI%d\n",
    231   1.2  deraadt 		sw->name, masunit, targ, lun,
    232   1.2  deraadt 		inqbuf.device_type, dtype,
    233   1.2  deraadt 		inqbuf.removable ? "removable" : "fixed",
    234   1.3  mycroft 		manu, model, revision, inqbuf.ansii_version);
    235   1.2  deraadt 	if(qtype[0])
    236   1.2  deraadt 		printf("%s%d targ %d lun %d: qualifier %d(%s)\n",
    237   1.2  deraadt 			sw->name, masunit, targ, lun,
    238   1.2  deraadt 			inqbuf.device_qualifier, qtype);
    239   1.9  deraadt 	sw->printed[targ] |= (1<<lun);
    240   1.2  deraadt 	return ret;
    241   1.1      cgd }
    242   1.1      cgd 
    243   1.2  deraadt void
    244   1.2  deraadt scsi_warn(int masunit, int mytarg, struct scsi_switch *sw)
    245   1.1      cgd {
    246   1.2  deraadt 	struct scsidevs *match = (struct scsidevs *)0;
    247   1.2  deraadt 	int physid;
    248   1.2  deraadt 	int targ, lun;
    249   1.1      cgd 
    250   1.2  deraadt 	for(targ=0; targ<8; targ++) {
    251   1.2  deraadt 		if(targ==mytarg)
    252   1.1      cgd 			continue;
    253   1.2  deraadt 		for(lun=0; lun<8; lun++) {
    254   1.2  deraadt 			/* check if device already used, or empty */
    255   1.2  deraadt 			if( sw->empty[targ] & (1<<lun) )
    256   1.2  deraadt 				continue;
    257   1.2  deraadt 			if( sw->used[targ] & (1<<lun) )
    258   1.2  deraadt 				continue;
    259   1.2  deraadt 
    260   1.2  deraadt 			physid = targ*8 + lun;
    261   1.2  deraadt 			match = scsi_probe(masunit, sw, physid, 0, 0);
    262   1.2  deraadt 
    263   1.2  deraadt 			if(match == (struct scsidevs *)-1) {
    264   1.2  deraadt 				if(lun==0)
    265   1.2  deraadt 					sw->empty[targ] = 0xff;
    266   1.2  deraadt 				else
    267   1.2  deraadt 					sw->empty[targ] = 0xff;
    268   1.2  deraadt 				continue;
    269   1.2  deraadt 			}
    270   1.2  deraadt 			if(match) {
    271   1.2  deraadt 				targ = physid >> 3;
    272   1.2  deraadt 				lun = physid & 7;
    273   1.2  deraadt 				if(match->flags & SC_MORE_LUS)
    274   1.2  deraadt 					sw->empty[targ] |= (1<<lun);
    275   1.2  deraadt 				else
    276   1.2  deraadt 					sw->empty[targ] = 0xff;
    277   1.2  deraadt 			}
    278   1.2  deraadt 		}
    279   1.1      cgd 	}
    280   1.1      cgd }
    281   1.1      cgd 
    282   1.2  deraadt /*
    283   1.2  deraadt  * not quite perfect. If we have two "drive ?" entries, this will
    284   1.2  deraadt  * probe through all the devices twice. It should have realized that
    285   1.2  deraadt  * any device that is not found the first time won't exist later on.
    286   1.2  deraadt  */
    287   1.2  deraadt int
    288   1.2  deraadt scsi_attach(int masunit, int mytarg, struct scsi_switch *sw,
    289   1.2  deraadt 	int *physid, int *unit, int type)
    290   1.1      cgd {
    291   1.2  deraadt 	struct scsidevs *match = (struct scsidevs *)0;
    292   1.2  deraadt 	int targ, lun;
    293   1.2  deraadt 	int ret=0;
    294   1.2  deraadt 
    295   1.2  deraadt 	/*printf("%s%d probing at targ %d lun %d..\n",
    296   1.2  deraadt 		sw->name, masunit, *physid >> 3, *physid & 7);*/
    297   1.1      cgd 
    298   1.2  deraadt 	if( *physid!=-1 ) {
    299   1.2  deraadt 		targ = *physid >> 3;
    300   1.2  deraadt 		lun = *physid & 7;
    301   1.1      cgd 
    302   1.2  deraadt 		if( (sw->empty[targ] & (1<<lun)) || (sw->used[targ] & (1<<lun)) )
    303   1.5  deraadt 			return 0;
    304   1.1      cgd 
    305   1.2  deraadt 		match = scsi_probe(masunit, sw, *physid, type, 0);
    306   1.2  deraadt 		if(match == (struct scsidevs *)-1) {
    307   1.2  deraadt 			match = (struct scsidevs *)0;
    308   1.2  deraadt 			if(lun==0)
    309   1.2  deraadt 				sw->empty[targ] = 0xff;
    310   1.2  deraadt 			else
    311   1.2  deraadt 				sw->empty[targ] |= (1<<lun);
    312   1.5  deraadt 			return 0;
    313   1.2  deraadt 		}
    314   1.1      cgd 
    315   1.2  deraadt 		if(!match)
    316   1.5  deraadt 			return 0;
    317   1.1      cgd 
    318   1.6  deraadt 		ret = (*(match->attach_rtn))(masunit, sw, *physid, unit);
    319   1.2  deraadt 		goto success;
    320   1.1      cgd 	}
    321   1.1      cgd 
    322   1.2  deraadt 	for(targ=0; targ<8; targ++) {
    323   1.2  deraadt 		if(targ==mytarg)
    324   1.2  deraadt 			continue;
    325   1.2  deraadt 		for(lun=0; lun<8; lun++) {
    326   1.2  deraadt 			if( (sw->empty[targ] & (1<<lun)) || (sw->used[targ] & (1<<lun)) )
    327   1.2  deraadt 				continue;
    328   1.2  deraadt 
    329   1.2  deraadt 			*physid = targ*8 + lun;
    330   1.2  deraadt 			match = scsi_probe(masunit, sw, *physid, type, 0);
    331   1.2  deraadt 			if( match==(struct scsidevs *)-1) {
    332   1.2  deraadt 				if(lun==0)
    333   1.2  deraadt 					sw->empty[targ] = 0xff;
    334   1.2  deraadt 				else
    335   1.2  deraadt 					sw->empty[targ] |= (1<<lun);
    336   1.2  deraadt 				match = (struct scsidevs *)0;
    337   1.2  deraadt 				continue;
    338   1.2  deraadt 			}
    339   1.9  deraadt 			if(!match)
    340   1.4  deraadt 				break;
    341   1.6  deraadt 			ret = (*(match->attach_rtn))(masunit, sw, *physid, unit);
    342   1.5  deraadt 			if(ret)
    343   1.2  deraadt 				goto success;
    344   1.5  deraadt 			return 0;
    345   1.2  deraadt 		}
    346   1.2  deraadt 	}
    347   1.2  deraadt 	*physid = -1;	/* failed... */
    348   1.5  deraadt 	return 0;
    349   1.2  deraadt 
    350   1.2  deraadt success:
    351   1.2  deraadt 	targ = *physid >> 3;
    352   1.2  deraadt 	lun = *physid & 7;
    353   1.9  deraadt 	if(match->flags & SC_MORE_LUS)
    354   1.2  deraadt 		sw->used[targ] |= (1<<lun);
    355   1.9  deraadt 	else
    356   1.2  deraadt 		sw->used[targ] = 0xff;
    357   1.5  deraadt 	return ret;
    358   1.1      cgd }
    359   1.1      cgd 
    360   1.2  deraadt /*
    361   1.2  deraadt  * Try make as good a match as possible with
    362   1.2  deraadt  * available sub drivers
    363   1.2  deraadt  */
    364   1.2  deraadt struct scsidevs *
    365   1.2  deraadt selectdev(int unit, int target, int lu, struct scsi_switch *sw, int qual,
    366   1.2  deraadt 	int dtype, int remov, char *manu, char *model, char *rev, int type)
    367   1.2  deraadt {
    368   1.2  deraadt 	struct scsidevs *sdbest = (struct scsidevs *)0;
    369   1.2  deraadt 	struct scsidevs *sdent = knowndevs;
    370   1.2  deraadt 	int numents = sizeof(knowndevs)/sizeof(struct scsidevs);
    371   1.2  deraadt 	int count = 0, sdbestes = 0;
    372   1.2  deraadt 
    373   1.2  deraadt 	dtype |= (qual << 5);
    374   1.2  deraadt 
    375   1.2  deraadt 	sdent--;
    376   1.2  deraadt 	while( count++ < numents) {
    377   1.2  deraadt 		sdent++;
    378   1.2  deraadt 		if(dtype != sdent->dtype)
    379   1.2  deraadt 			continue;
    380   1.2  deraadt 		if(type != sdent->type)
    381   1.1      cgd 			continue;
    382   1.2  deraadt 		if(sdbestes < 1) {
    383   1.2  deraadt 			sdbestes = 1;
    384   1.2  deraadt 			sdbest = sdent;
    385   1.1      cgd 		}
    386   1.2  deraadt 		if(remov != sdent->removable)
    387   1.1      cgd 			continue;
    388   1.2  deraadt 		if(sdbestes < 2) {
    389   1.2  deraadt 			sdbestes = 2;
    390   1.2  deraadt 			sdbest = sdent;
    391   1.2  deraadt 		}
    392   1.2  deraadt 		if(sdent->flags & SC_SHOWME)
    393   1.2  deraadt 			printf("\n%s-\n%s-", sdent->manufacturer, manu);
    394   1.2  deraadt 		if(strcmp(sdent->manufacturer, manu))
    395   1.1      cgd 			continue;
    396   1.2  deraadt 		if(sdbestes < 3) {
    397   1.2  deraadt 			sdbestes = 3;
    398   1.2  deraadt 			sdbest = sdent;
    399   1.2  deraadt 		}
    400   1.2  deraadt 		if(sdent->flags & SC_SHOWME)
    401   1.2  deraadt 			printf("\n%s-\n%s-",sdent->model, model);
    402   1.2  deraadt 		if(strcmp(sdent->model, model))
    403   1.1      cgd 			continue;
    404   1.2  deraadt 		if(sdbestes < 4) {
    405   1.2  deraadt 			sdbestes = 4;
    406   1.2  deraadt 			sdbest = sdent;
    407   1.2  deraadt 		}
    408   1.2  deraadt 		if(sdent->flags & SC_SHOWME)
    409   1.2  deraadt 			printf("\n%s-\n%s-",sdent->version, rev);
    410   1.2  deraadt 		if(strcmp(sdent->version, rev))
    411   1.1      cgd 			continue;
    412   1.2  deraadt 		if(sdbestes < 5) {
    413   1.2  deraadt 			sdbestes = 5;
    414   1.2  deraadt 			sdbest = sdent;
    415   1.1      cgd 			break;
    416   1.1      cgd 		}
    417   1.1      cgd 	}
    418   1.2  deraadt 	return sdbest;
    419   1.1      cgd }
    420   1.1      cgd 
    421   1.2  deraadt /*
    422   1.2  deraadt  * Do a scsi operation asking a device if it is
    423   1.2  deraadt  * ready. Use the scsi_cmd routine in the switch
    424   1.2  deraadt  * table.
    425   1.2  deraadt  */
    426   1.2  deraadt int
    427   1.2  deraadt scsi_ready(int unit, int target, int lu,
    428   1.2  deraadt 	struct scsi_switch *sw, int flags)
    429   1.1      cgd {
    430   1.2  deraadt 	struct scsi_test_unit_ready scsi_cmd;
    431   1.2  deraadt 	struct scsi_xfer scsi_xfer;
    432   1.1      cgd 	volatile int rval;
    433   1.2  deraadt 	int key;
    434   1.1      cgd 
    435   1.1      cgd 	bzero(&scsi_cmd, sizeof(scsi_cmd));
    436   1.1      cgd 	bzero(&scsi_xfer, sizeof(scsi_xfer));
    437   1.1      cgd 	scsi_cmd.op_code = TEST_UNIT_READY;
    438   1.1      cgd 
    439   1.2  deraadt 	scsi_xfer.flags = flags | INUSE;
    440   1.2  deraadt 	scsi_xfer.adapter = unit;
    441   1.2  deraadt 	scsi_xfer.targ = target;
    442   1.2  deraadt 	scsi_xfer.lu = lu;
    443   1.2  deraadt 	scsi_xfer.cmd = (struct scsi_generic *)&scsi_cmd;
    444   1.2  deraadt 	scsi_xfer.retries = 8;
    445   1.2  deraadt 	scsi_xfer.timeout = 10000;
    446   1.2  deraadt 	scsi_xfer.cmdlen = sizeof(scsi_cmd);
    447   1.2  deraadt 	scsi_xfer.data = 0;
    448   1.2  deraadt 	scsi_xfer.datalen = 0;
    449   1.2  deraadt 	scsi_xfer.resid = 0;
    450   1.2  deraadt 	scsi_xfer.when_done = 0;
    451   1.2  deraadt 	scsi_xfer.done_arg = 0;
    452   1.2  deraadt retry:	scsi_xfer.error = 0;
    453   1.2  deraadt 
    454   1.2  deraadt 	/* don't use interrupts! */
    455   1.2  deraadt 
    456   1.2  deraadt 	rval = (*(sw->scsi_cmd))(&scsi_xfer);
    457   1.2  deraadt 	if (rval != COMPLETE) {
    458   1.2  deraadt 		if(scsi_debug) {
    459   1.2  deraadt 			printf("scsi error, rval = 0x%x\n", rval);
    460   1.2  deraadt 			printf("code from driver: 0x%x\n", scsi_xfer.error);
    461   1.2  deraadt 		}
    462   1.2  deraadt 		switch(scsi_xfer.error) {
    463   1.2  deraadt 		case XS_SENSE:
    464   1.2  deraadt 			/*
    465   1.2  deraadt 			 * Any sense value is illegal except UNIT ATTENTION
    466   1.2  deraadt 			 * In which case we need to check again to get the
    467   1.2  deraadt 			 * correct response. (especially exabytes)
    468   1.2  deraadt 			 */
    469   1.2  deraadt 			if(scsi_xfer.sense.error_class == 7 ) {
    470   1.1      cgd 				key = scsi_xfer.sense.ext.extended.sense_key ;
    471   1.2  deraadt 				switch(key) {
    472   1.2  deraadt 				case 2:	/* not ready BUT PRESENT! */
    473   1.2  deraadt 					return(COMPLETE);
    474   1.2  deraadt 				case 6:
    475   1.1      cgd 					spinwait(1000);
    476   1.2  deraadt 					if(scsi_xfer.retries--) {
    477   1.1      cgd 						scsi_xfer.flags &= ~ITSDONE;
    478   1.1      cgd 						goto retry;
    479   1.1      cgd 					}
    480   1.1      cgd 					return(COMPLETE);
    481   1.1      cgd 				default:
    482   1.1      cgd 					if(scsi_debug)
    483   1.2  deraadt 						printf("%d:%d,key=%x.", target,
    484   1.2  deraadt 							lu, key);
    485   1.1      cgd 				}
    486   1.1      cgd 			}
    487   1.1      cgd 			return(HAD_ERROR);
    488   1.2  deraadt 		case XS_BUSY:
    489   1.1      cgd 			spinwait(1000);
    490   1.2  deraadt 			if(scsi_xfer.retries--) {
    491   1.1      cgd 				scsi_xfer.flags &= ~ITSDONE;
    492   1.1      cgd 				goto retry;
    493   1.1      cgd 			}
    494   1.2  deraadt 			return COMPLETE;	/* it's busy so it's there */
    495   1.2  deraadt 		case XS_TIMEOUT:
    496   1.1      cgd 		default:
    497   1.2  deraadt 			return HAD_ERROR;
    498   1.1      cgd 		}
    499   1.1      cgd 	}
    500   1.2  deraadt 	return COMPLETE;
    501   1.1      cgd }
    502   1.2  deraadt 
    503   1.2  deraadt /*
    504   1.2  deraadt  * Do a scsi operation asking a device what it is
    505   1.2  deraadt  * Use the scsi_cmd routine in the switch table.
    506   1.2  deraadt  */
    507   1.2  deraadt int
    508   1.2  deraadt scsi_inquire(int unit, int target, int lu, struct scsi_switch *sw,
    509   1.2  deraadt 	u_char *inqbuf, int flags)
    510   1.1      cgd {
    511   1.2  deraadt 	struct scsi_inquiry scsi_cmd;
    512   1.2  deraadt 	struct scsi_xfer scsi_xfer;
    513   1.1      cgd 
    514   1.1      cgd 	bzero(&scsi_cmd, sizeof(scsi_cmd));
    515   1.1      cgd 	bzero(&scsi_xfer, sizeof(scsi_xfer));
    516   1.1      cgd 	scsi_cmd.op_code = INQUIRY;
    517   1.1      cgd 	scsi_cmd.length = sizeof(struct scsi_inquiry_data);
    518   1.1      cgd 
    519   1.2  deraadt 	scsi_xfer.flags = flags | SCSI_DATA_IN | INUSE;
    520   1.2  deraadt 	scsi_xfer.adapter = unit;
    521   1.2  deraadt 	scsi_xfer.targ = target;
    522   1.2  deraadt 	scsi_xfer.lu = lu;
    523   1.2  deraadt 	scsi_xfer.retries = 8;
    524   1.2  deraadt 	scsi_xfer.timeout = 10000;
    525   1.2  deraadt 	scsi_xfer.cmd = (struct scsi_generic *)&scsi_cmd;
    526   1.2  deraadt 	scsi_xfer.cmdlen =  sizeof(struct scsi_inquiry);
    527   1.2  deraadt 	scsi_xfer.data = inqbuf;
    528   1.2  deraadt 	scsi_xfer.datalen = sizeof(struct scsi_inquiry_data);
    529   1.2  deraadt 	scsi_xfer.resid = sizeof(struct scsi_inquiry_data);
    530   1.2  deraadt 	scsi_xfer.when_done = 0;
    531   1.2  deraadt 	scsi_xfer.done_arg = 0;
    532   1.2  deraadt 
    533   1.2  deraadt retry:
    534   1.2  deraadt 	scsi_xfer.error=0;
    535   1.2  deraadt 	/* don't use interrupts! */
    536   1.2  deraadt 
    537   1.2  deraadt 	if ((*(sw->scsi_cmd))(&scsi_xfer) != COMPLETE) {
    538   1.2  deraadt 		if(scsi_debug)
    539   1.2  deraadt 			printf("inquiry had error(0x%x) ",scsi_xfer.error);
    540   1.2  deraadt 		switch(scsi_xfer.error) {
    541   1.2  deraadt 		case XS_NOERROR:
    542   1.2  deraadt 			break;
    543   1.2  deraadt 		case XS_SENSE:
    544   1.2  deraadt 			/*
    545   1.2  deraadt 			 * Any sense value is illegal except UNIT ATTENTION
    546   1.2  deraadt 			 * In which case we need to check again to get the
    547   1.2  deraadt 			 * correct response. (especially exabytes)
    548   1.2  deraadt 			 */
    549   1.2  deraadt 			if( scsi_xfer.sense.error_class==7 &&
    550   1.2  deraadt 			    scsi_xfer.sense.ext.extended.sense_key==6) {
    551   1.2  deraadt 				/* it's changed so it's there */
    552   1.1      cgd 				spinwait(1000);
    553   1.2  deraadt 				if(scsi_xfer.retries--) {
    554   1.2  deraadt 					scsi_xfer.flags &= ~ITSDONE;
    555   1.2  deraadt 					goto retry;
    556   1.1      cgd 				}
    557   1.2  deraadt 				return COMPLETE;
    558   1.1      cgd 			}
    559   1.2  deraadt 			return HAD_ERROR;
    560   1.2  deraadt 		case XS_BUSY:
    561   1.1      cgd 			spinwait(1000);
    562   1.2  deraadt 			if(scsi_xfer.retries--) {
    563   1.1      cgd 				scsi_xfer.flags &= ~ITSDONE;
    564   1.1      cgd 				goto retry;
    565   1.1      cgd 			}
    566   1.2  deraadt 		case XS_TIMEOUT:
    567   1.1      cgd 		default:
    568   1.1      cgd 			return(HAD_ERROR);
    569   1.1      cgd 		}
    570   1.1      cgd 	}
    571   1.2  deraadt 	return COMPLETE;
    572   1.1      cgd }
    573   1.1      cgd 
    574   1.2  deraadt /*
    575   1.2  deraadt  * convert a physical address to 3 bytes,
    576   1.2  deraadt  * MSB at the lowest address,
    577   1.2  deraadt  * LSB at the highest.
    578   1.2  deraadt  */
    579   1.2  deraadt void
    580   1.2  deraadt lto3b(u_long val, u_char *bytes)
    581   1.1      cgd {
    582   1.1      cgd 	*bytes++ = (val&0xff0000)>>16;
    583   1.1      cgd 	*bytes++ = (val&0xff00)>>8;
    584   1.1      cgd 	*bytes = val&0xff;
    585   1.1      cgd }
    586   1.1      cgd 
    587   1.2  deraadt /*
    588   1.2  deraadt  * The reverse of lto3b
    589   1.2  deraadt  */
    590   1.2  deraadt u_long
    591   1.2  deraadt _3btol(u_char *bytes)
    592   1.1      cgd {
    593   1.2  deraadt 	u_long rc;
    594   1.2  deraadt 
    595   1.1      cgd 	rc = (*bytes++ << 16);
    596   1.1      cgd 	rc += (*bytes++ << 8);
    597   1.1      cgd 	rc += *bytes;
    598   1.2  deraadt 	return rc;
    599   1.1      cgd }
    600   1.1      cgd 
    601