Home | History | Annotate | Line # | Download | only in scsipi
scsiconf.c revision 1.12
      1 /*
      2  * Copyright (c) 1994 Charles Hannum.  All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  * 1. Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  * 2. Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  * 3. All advertising materials mentioning features or use of this software
     13  *    must display the following acknowledgement:
     14  *	This product includes software developed by Charles Hannum.
     15  * 4. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  *
     29  *      $Id: scsiconf.c,v 1.12 1994/03/29 04:29:37 mycroft Exp $
     30  */
     31 
     32 /*
     33  * Originally written by Julian Elischer (julian (at) tfs.com)
     34  * for TRW Financial Systems for use under the MACH(2.5) operating system.
     35  *
     36  * TRW Financial Systems, in accordance with their agreement with Carnegie
     37  * Mellon University, makes this software available to CMU to distribute
     38  * or use in any manner that they see fit as long as this message is kept with
     39  * the software. For this reason TFS also grants any other persons or
     40  * organisations permission to use or modify this software.
     41  *
     42  * TFS supplies this software to be publicly redistributed
     43  * on the understanding that TFS is not responsible for the correct
     44  * functioning of this software in any circumstances.
     45  *
     46  * Ported to run under 386BSD by Julian Elischer (julian (at) tfs.com) Sept 1992
     47  */
     48 
     49 #include <sys/types.h>
     50 #include <sys/param.h>
     51 #include <sys/systm.h>
     52 #include <sys/malloc.h>
     53 #include <sys/device.h>
     54 
     55 #include <scsi/scsi_all.h>
     56 #include <scsi/scsiconf.h>
     57 
     58 #if defined(i386) && !defined(NEWCONFIG)
     59 #include <i386/isa/isa_device.h>
     60 #endif
     61 
     62 #include "st.h"
     63 #include "sd.h"
     64 #include "ch.h"
     65 #include "cd.h"
     66 #include "uk.h"
     67 #include "su.h"
     68 
     69 #ifdef TFS
     70 #include "bll.h"
     71 #include "cals.h"
     72 #include "kil.h"
     73 #include "scan.h"
     74 #else /* TFS */
     75 #define	NBLL 0
     76 #define	NCALS 0
     77 #define	NKIL 0
     78 #define	NSCAN 0
     79 #endif /* TFS */
     80 
     81 /*
     82  * The structure of known drivers for autoconfiguration
     83  */
     84 struct scsidevs {
     85 	char   *devname;
     86 	u_int32 type;
     87 	boolean removable;
     88 	char    flags;		/* 1 show my comparisons during boot(debug) */
     89 	char   *manufacturer;
     90 	char   *model;
     91 	char   *version;
     92 };
     93 
     94 #define SC_SHOWME	0x01
     95 #define	SC_ONE_LU	0x00
     96 #define	SC_MORE_LUS	0x02
     97 
     98 #if	NUK > 0
     99 static struct scsidevs unknowndev = {
    100 	"uk", -1, 0, SC_MORE_LUS,
    101 	"standard", "any", "any"
    102 };
    103 #endif 	/*NUK*/
    104 
    105 static struct scsidevs knowndevs[] = {
    106 #if NSD > 0
    107 	{ "sd", T_DIRECT, T_FIXED, SC_ONE_LU,
    108 	  "standard", "any", "any" },
    109 	{ "sd", T_DIRECT, T_FIXED, SC_ONE_LU,
    110 	  "MAXTOR  ", "XT-4170S        ", "B5A " },
    111 #endif	/* NSD */
    112 #if NST > 0
    113 	{ "st", T_SEQUENTIAL, T_REMOV, SC_ONE_LU,
    114 	  "standard", "any", "any" },
    115 #endif	/* NST */
    116 #if NCALS > 0
    117 	{ "cals", T_PROCESSOR, T_FIXED, SC_MORE_LUS,
    118 	  "standard", "any", "any" },
    119 #endif	/* NCALS */
    120 #if NCH > 0
    121 	{ "ch", T_CHANGER, T_REMOV, SC_ONE_LU,
    122 	  "standard", "any", "any" },
    123 #endif	/* NCH */
    124 #if NCD > 0
    125 #ifndef UKTEST	/* make cdroms unrecognised to test the uk driver */
    126 	{ "cd", T_READONLY, T_REMOV, SC_ONE_LU,
    127 	  "SONY    ", "CD-ROM CDU-8012 ", "3.1a" },
    128 	{ "cd", T_READONLY, T_REMOV, SC_MORE_LUS,
    129 	  "PIONEER ", "CD-ROM DRM-600  ", "any" },
    130 #endif
    131 #endif	/* NCD */
    132 #if NBLL > 0
    133 	{ "bll", T_PROCESSOR, T_FIXED, SC_MORE_LUS,
    134 	  "AEG     ", "READER          ", "V1.0" },
    135 #endif	/* NBLL */
    136 #if NKIL > 0
    137 	{ "kil", T_SCANNER, T_FIXED, SC_ONE_LU,
    138 	  "KODAK   ", "IL Scanner 900  ", "any" },
    139 #endif	/* NKIL */
    140 	{ 0 }
    141 };
    142 
    143 /*
    144  * Declarations
    145  */
    146 struct scsidevs *scsi_probedev();
    147 struct scsidevs *selectdev();
    148 int scsi_probe_bus __P((int bus, int targ, int lun));
    149 
    150 struct scsi_device probe_switch = {
    151 	NULL,
    152 	NULL,
    153 	NULL,
    154 	NULL,
    155 	"probe",
    156 	0,
    157 };
    158 
    159 int scsibusmatch __P((struct device *, struct cfdata *, void *));
    160 void scsibusattach __P((struct device *, struct device *, void *));
    161 
    162 struct cfdriver scsibuscd = {
    163 	NULL, "scsibus", scsibusmatch, scsibusattach, DV_DULL,
    164 	sizeof(struct scsibus_data)
    165 };
    166 
    167 int
    168 scsibusmatch(parent, cf, aux)
    169         struct device *parent;
    170         struct cfdata *cf;
    171         void *aux;
    172 {
    173 
    174 	return 1;
    175 }
    176 
    177 /*
    178  * The routine called by the adapter boards to get all their
    179  * devices configured in.
    180  */
    181 void
    182 scsibusattach(parent, self, aux)
    183         struct device *parent, *self;
    184         void *aux;
    185 {
    186 	struct scsibus_data *sb = (struct scsibus_data *)self;
    187 	struct scsi_link *sc_link_proto = aux;
    188 
    189 	sc_link_proto->scsibus = sb->sc_dev.dv_unit;
    190 	sb->adapter_link = sc_link_proto;
    191 	printf("\n");
    192 
    193 #if defined(SCSI_DELAY) && SCSI_DELAY > 2
    194 	printf("%s: waiting for scsi devices to settle\n",
    195 		sb->sc_dev.dv_xname);
    196 #else	/* SCSI_DELAY > 2 */
    197 #undef	SCSI_DELAY
    198 #define SCSI_DELAY 2
    199 #endif	/* SCSI_DELAY */
    200 	delay(1000000 * SCSI_DELAY);
    201 
    202 	scsi_probe_bus(sb->sc_dev.dv_unit, -1, -1);
    203 }
    204 
    205 /*
    206  * Probe the requested scsi bus. It must be already set up.
    207  * -1 requests all set up scsi busses.
    208  * targ and lun optionally narrow the search if not -1
    209  */
    210 int
    211 scsi_probe_busses(bus, targ, lun)
    212 	int bus, targ, lun;
    213 {
    214 
    215 	if (bus == -1) {
    216 		for (bus = 0; bus < scsibuscd.cd_ndevs; bus++)
    217 			if (scsibuscd.cd_devs[bus])
    218 				scsi_probe_bus(bus, targ, lun);
    219 		return 0;
    220 	} else {
    221 		return scsi_probe_bus(bus, targ, lun);
    222 	}
    223 }
    224 
    225 /*
    226  * Probe the requested scsi bus. It must be already set up.
    227  * targ and lun optionally narrow the search if not -1
    228  */
    229 int
    230 scsi_probe_bus(bus, targ, lun)
    231 	int bus, targ, lun;
    232 {
    233 	struct scsibus_data *scsi;
    234 	int	maxtarg, mintarg, maxlun, minlun;
    235 	struct scsi_link *sc_link_proto;
    236 	u_int8  scsi_addr ;
    237 	struct scsidevs *bestmatch = NULL;
    238 	struct scsi_link *sc_link = NULL;
    239 	boolean maybe_more;
    240 
    241 	if (bus < 0 || bus >= scsibuscd.cd_ndevs)
    242 		return ENXIO;
    243 	scsi = scsibuscd.cd_devs[bus];
    244 	if (!scsi)
    245 		return ENXIO;
    246 
    247 	sc_link_proto = scsi->adapter_link;
    248 	scsi_addr = sc_link_proto->adapter_targ;
    249 
    250 	if (targ == -1) {
    251 		maxtarg = 7;
    252 		mintarg = 0;
    253 	} else {
    254 		if (targ < 0 || targ > 7)
    255 			return EINVAL;
    256 		maxtarg = mintarg = targ;
    257 	}
    258 
    259 	if (lun == -1) {
    260 		maxlun = 7;
    261 		minlun = 0;
    262 	} else {
    263 		if (lun < 0 || lun > 7)
    264 			return EINVAL;
    265 		maxlun = minlun = lun;
    266 	}
    267 
    268 	for (targ = mintarg; targ <= maxtarg; targ++) {
    269 		maybe_more = 0;	/* by default only check 1 lun */
    270 #if 0 /* XXXX */
    271 		if (targ == scsi_addr)
    272 			continue;
    273 #endif
    274 		for (lun = minlun; lun <= maxlun; lun++) {
    275 			/*
    276 			 * The spot appears to already have something
    277 			 * linked in, skip past it. Must be doing a 'reprobe'
    278 			 */
    279 			if (scsi->sc_link[targ][lun]) {
    280 				/* don't do this one, but check other luns */
    281 				maybe_more = 1;
    282 				continue;
    283 			}
    284 			/*
    285 			 * If we presently don't have a link block
    286 			 * then allocate one to use while probing
    287 			 */
    288 			if (!sc_link) {
    289 				sc_link = malloc(sizeof(*sc_link), M_TEMP, M_NOWAIT);
    290 				*sc_link = *sc_link_proto;	/* struct copy */
    291 				sc_link->opennings = 1;
    292 				sc_link->device = &probe_switch;
    293 			}
    294 			sc_link->target = targ;
    295 			sc_link->lun = lun;
    296 			bestmatch = scsi_probedev(sc_link, &maybe_more);
    297 			/*
    298 			 * We already know what the device is.  We use a
    299 			 * special matching routine which insists that the
    300 			 * cfdata is of the right type rather than putting
    301 			 * more intelligence in individual match routines for
    302 			 * each high-level driver.  We must have
    303 			 * scsi_targmatch() do all of the comparisons, or we
    304 			 * could get stuck in an infinite loop trying the same
    305 			 * device repeatedly.  We use the `fordriver' field of
    306 			 * the scsi_link for now, rather than inventing a new
    307 			 * structure just for the config_search().
    308 			 */
    309 			if (bestmatch) {
    310 				sc_link->fordriver = bestmatch->devname;
    311 				if (config_found((struct device *)scsi,
    312 						 sc_link, NULL)) {
    313 					scsi->sc_link[targ][lun] = sc_link;
    314 					sc_link = NULL;	/* it's been used */
    315 				} else
    316 					printf("No matching config entry.\n");
    317 			}
    318 			if (!maybe_more)/* nothing suggests we'll find more */
    319 				break;	/* nothing here, skip to next targ */
    320 			/* otherwise something says we should look further */
    321 		}
    322 	}
    323 	if (sc_link)
    324 		free(sc_link, M_TEMP);
    325 	return 0;
    326 }
    327 
    328 int
    329 #ifndef i386
    330 scsi_targmatch(parent, cf, aux)
    331 	struct device *parent;
    332 	struct cfdata *cf;
    333 	void *aux;
    334 {
    335 #else
    336 scsi_targmatch(parent, self, aux)
    337 	struct device *parent, *self;
    338 	void *aux;
    339 {
    340 	struct cfdata *cf = self->dv_cfdata;
    341 #endif
    342 	struct scsi_link *sc_link = aux;
    343 	char *devname = sc_link->fordriver;
    344 #if !defined(i386) || defined(NEWCONFIG)
    345 
    346 #define	cf_target cf_loc[0]
    347 #define	cf_lun cf_loc[1]
    348 	if (cf->cf_target != -1 && cf->cf_target != sc_link->target)
    349 		return 0;
    350 	if (cf->cf_lun != -1 && cf->cf_lun != sc_link->lun)
    351 		return 0;
    352 #undef cf_target
    353 #undef cf_lun
    354 #else
    355 	struct isa_device *id = (void *)cf->cf_loc;
    356 
    357 	if (id->id_physid != -1 &&
    358 	    id->id_physid != ((sc_link->target << 3) | sc_link->lun))
    359 		return 0;
    360 #endif
    361 	if (strcmp(cf->cf_driver->cd_name, devname))
    362 		return 0;
    363 
    364 	return 1;
    365 }
    366 
    367 /*
    368  * given a target and lu, ask the device what
    369  * it is, and find the correct driver table
    370  * entry.
    371  */
    372 struct scsidevs *
    373 scsi_probedev(sc_link, maybe_more)
    374 	boolean *maybe_more;
    375 	struct scsi_link *sc_link;
    376 {
    377 	u_int8  target = sc_link->target;
    378 	u_int8  lun = sc_link->lun;
    379 	struct scsi_adapter *scsi_adapter = sc_link->adapter;
    380 	struct scsidevs *bestmatch = NULL;
    381 	char   *dtype = NULL, *desc;
    382 	char   *qtype;
    383 	static struct scsi_inquiry_data inqbuf;
    384 	u_int32 len, qualifier, type;
    385 	boolean remov;
    386 	char    manu[32];
    387 	char    model[32];
    388 	char    version[32];
    389 
    390 	bzero(&inqbuf, sizeof(inqbuf));
    391 	/*
    392 	 * Ask the device what it is
    393 	 */
    394 #ifdef	SCSIDEBUG
    395 	if (target == DEBUGTARG && lun == DEBUGLUN)
    396 		sc_link->flags |= DEBUGLEVEL;
    397 	else
    398 		sc_link->flags &= ~(SDEV_DB1 | SDEV_DB2 | SDEV_DB3 | SDEV_DB4);
    399 #endif	/* SCSIDEBUG */
    400 	/* catch unit attn */
    401 	scsi_test_unit_ready(sc_link, SCSI_NOSLEEP | SCSI_NOMASK | SCSI_SILENT);
    402 #ifdef	DOUBTFULL
    403 	switch (scsi_test_unit_ready(sc_link, SCSI_NOSLEEP | SCSI_NOMASK | SCSI_SILENT)) {
    404 	case 0:		/* said it WAS ready */
    405 	case EBUSY:		/* replied 'NOT READY' but WAS present, continue */
    406 	case ENXIO:
    407 		break;
    408 	case EIO:		/* device timed out */
    409 	case EINVAL:		/* Lun not supported */
    410 	default:
    411 		return NULL;
    412 
    413 	}
    414 #endif	/*DOUBTFULL*/
    415 #ifdef	SCSI_2_DEF
    416 	/* some devices need to be told to go to SCSI2 */
    417 	/* However some just explode if you tell them this.. leave it out */
    418 	scsi_change_def(sc_link, SCSI_NOSLEEP | SCSI_NOMASK | SCSI_SILENT);
    419 #endif /*SCSI_2_DEF */
    420 
    421 	/* Now go ask the device all about itself */
    422 	if (scsi_inquire(sc_link, &inqbuf, SCSI_NOSLEEP | SCSI_NOMASK) != 0)
    423 		return NULL;
    424 
    425 	/*
    426 	 * note what BASIC type of device it is
    427 	 */
    428 	type = inqbuf.device & SID_TYPE;
    429 	qualifier = inqbuf.device & SID_QUAL;
    430 	remov = inqbuf.dev_qual2 & SID_REMOVABLE;
    431 
    432 	/*
    433 	 * Any device qualifier that has the top bit set (qualifier&4 != 0)
    434 	 * is vendor specific and won't match in this switch.
    435 	 */
    436 	switch (qualifier) {
    437 	case SID_QUAL_LU_OK:
    438 		qtype = "";
    439 		break;
    440 
    441 	case SID_QUAL_LU_OFFLINE:
    442 		qtype = ", Unit not Connected!";
    443 		break;
    444 
    445 	case SID_QUAL_RSVD:
    446 		qtype = ", Reserved Peripheral Qualifier!";
    447 		*maybe_more = 1;
    448 		return NULL;
    449 		break;
    450 
    451 	case SID_QUAL_BAD_LU:
    452 		/*
    453 		 * Check for a non-existent unit.  If the device is returning
    454 		 * this much, then we must set the flag that has
    455 		 * the searchers keep looking on other luns.
    456 		 */
    457 		qtype = ", The Target can't support this Unit!";
    458 		*maybe_more = 1;
    459 		return NULL;
    460 
    461 	default:
    462 		dtype = "vendor specific";
    463 		qtype = "";
    464 		*maybe_more = 1;
    465 		break;
    466 	}
    467 	if (dtype == 0) {
    468 		switch (type) {
    469 		case T_DIRECT:
    470 			dtype = "direct";
    471 			break;
    472 		case T_SEQUENTIAL:
    473 			dtype = "sequential";
    474 			break;
    475 		case T_PRINTER:
    476 			dtype = "printer";
    477 			break;
    478 		case T_PROCESSOR:
    479 			dtype = "processor";
    480 			break;
    481 		case T_READONLY:
    482 			dtype = "readonly";
    483 			break;
    484 		case T_WORM:
    485 			dtype = "worm";
    486 			break;
    487 		case T_SCANNER:
    488 			dtype = "scanner";
    489 			break;
    490 		case T_OPTICAL:
    491 			dtype = "optical";
    492 			break;
    493 		case T_CHANGER:
    494 			dtype = "changer";
    495 			break;
    496 		case T_COMM:
    497 			dtype = "communication";
    498 			break;
    499 		case T_NODEVICE:
    500 			*maybe_more = 1;
    501 			return NULL;
    502 		default:
    503 			dtype = NULL;
    504 			break;
    505 		}
    506 	}
    507 
    508 	/*
    509 	 * Then if it's advanced enough, more detailed
    510 	 * information
    511 	 */
    512 	if ((inqbuf.version & SID_ANSII) > 0) {
    513 		if ((len = inqbuf.additional_length
    514 			+ ((char *) inqbuf.unused
    515 			    - (char *) &inqbuf))
    516 		    > (sizeof(struct scsi_inquiry_data) - 1))
    517 			        len = sizeof(struct scsi_inquiry_data) - 1;
    518 		desc = inqbuf.vendor;
    519 		desc[len - (desc - (char *) &inqbuf)] = 0;
    520 		strncpy(manu, inqbuf.vendor, 8);
    521 		manu[8] = 0;
    522 		strncpy(model, inqbuf.product, 16);
    523 		model[16] = 0;
    524 		strncpy(version, inqbuf.revision, 4);
    525 		version[4] = 0;
    526 	} else
    527 		/*
    528 		 * If not advanced enough, use default values
    529 		 */
    530 	{
    531 		desc = "early protocol device";
    532 		strncpy(manu, "unknown", 8);
    533 		strncpy(model, "unknown", 16);
    534 		strncpy(version, "????", 4);
    535 	}
    536 	printf("%s targ %d lun %d: <%s%s%s> SCSI%d ",
    537 		((struct device *)sc_link->adapter_softc)->dv_xname,
    538 		target, lun, manu, model, version,
    539 		inqbuf.version & SID_ANSII);
    540 	if (dtype)
    541 		printf("%s", dtype);
    542 	else
    543 		printf("type %d", type);
    544 	printf(" %s\n", remov ? "removable" : "fixed");
    545 	if (qtype[0])
    546 		printf("%s targ %d lun %d: qualifier %d(%s)\n",
    547 			((struct device *)sc_link->adapter_softc)->dv_xname,
    548 			target, lun, qualifier, qtype);
    549 
    550 	/*
    551 	 * Try make as good a match as possible with
    552 	 * available sub drivers
    553 	 */
    554 	bestmatch = selectdev(qualifier, type, remov ? T_REMOV : T_FIXED,
    555 			manu, model, version);
    556 	if (bestmatch && bestmatch->flags & SC_MORE_LUS)
    557 		*maybe_more = 1;
    558 	return bestmatch;
    559 }
    560 
    561 /*
    562  * Try make as good a match as possible with
    563  * available sub drivers
    564  */
    565 struct scsidevs *
    566 selectdev(qualifier, type, remov, manu, model, rev)
    567 	u_int32 qualifier, type;
    568 	boolean remov;
    569 	char   *manu, *model, *rev;
    570 {
    571 	u_int32 numents = (sizeof(knowndevs) / sizeof(struct scsidevs)) - 1;
    572 	u_int32 count = 0;
    573 	u_int32 bestmatches = 0;
    574 	struct scsidevs *bestmatch = (struct scsidevs *) 0;
    575 	struct scsidevs *thisentry = knowndevs;
    576 
    577 	type |= qualifier;	/* why? */
    578 
    579 	thisentry--;
    580 	while (count++ < numents) {
    581 		thisentry++;
    582 		if (type != thisentry->type)
    583 			continue;
    584 		if (bestmatches < 1) {
    585 			bestmatches = 1;
    586 			bestmatch = thisentry;
    587 		}
    588 		if (remov != thisentry->removable)
    589 			continue;
    590 		if (bestmatches < 2) {
    591 			bestmatches = 2;
    592 			bestmatch = thisentry;
    593 		}
    594 		if (thisentry->flags & SC_SHOWME)
    595 			printf("\n%s-\n%s-", thisentry->manufacturer, manu);
    596 		if (strcmp(thisentry->manufacturer, manu))
    597 			continue;
    598 		if (bestmatches < 3) {
    599 			bestmatches = 3;
    600 			bestmatch = thisentry;
    601 		}
    602 		if (thisentry->flags & SC_SHOWME)
    603 			printf("\n%s-\n%s-", thisentry->model, model);
    604 		if (strcmp(thisentry->model, model))
    605 			continue;
    606 		if (bestmatches < 4) {
    607 			bestmatches = 4;
    608 			bestmatch = thisentry;
    609 		}
    610 		if (thisentry->flags & SC_SHOWME)
    611 			printf("\n%s-\n%s-", thisentry->version, rev);
    612 		if (strcmp(thisentry->version, rev))
    613 			continue;
    614 		if (bestmatches < 5) {
    615 			bestmatches = 5;
    616 			bestmatch = thisentry;
    617 			break;
    618 		}
    619 	}
    620 #if NUK > 0
    621 	if (!bestmatch)
    622 		bestmatch = &unknowndev;
    623 #endif
    624 	if (!bestmatch)
    625 		printf("No matching driver.\n");
    626 	return bestmatch;
    627 }
    628