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