Home | History | Annotate | Line # | Download | only in scsipi
scsiconf.c revision 1.58
      1 /*	$NetBSD: scsiconf.c,v 1.58 1996/07/05 16:19:08 christos 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 #if 0
     59 #if NCALS > 0
     60 	{ T_PROCESSOR, T_FIXED, 1,
     61 	  0, 0, 0 },
     62 #endif	/* NCALS */
     63 #if NBLL > 0
     64 	{ T_PROCESSOR, T_FIXED, 1,
     65 	  "AEG     ", "READER          ", "V1.0" },
     66 #endif	/* NBLL */
     67 #if NKIL > 0
     68 	{ T_SCANNER, T_FIXED, 0,
     69 	  "KODAK   ", "IL Scanner 900  ", 0 },
     70 #endif	/* NKIL */
     71 #endif
     72 
     73 /*
     74  * Declarations
     75  */
     76 void scsi_probedev __P((struct scsibus_softc *, int, int));
     77 int scsi_probe_bus __P((int bus, int target, int lun));
     78 
     79 struct scsi_device probe_switch = {
     80 	NULL,
     81 	NULL,
     82 	NULL,
     83 	NULL,
     84 };
     85 
     86 int scsibusmatch __P((struct device *, void *, void *));
     87 void scsibusattach __P((struct device *, struct device *, void *));
     88 int scsibussubmatch __P((struct device *, void *, void *));
     89 
     90 struct cfattach scsibus_ca = {
     91 	sizeof(struct scsibus_softc), scsibusmatch, scsibusattach
     92 };
     93 
     94 struct cfdriver scsibus_cd = {
     95 	NULL, "scsibus", DV_DULL
     96 };
     97 
     98 int scsibusprint __P((void *, char *));
     99 
    100 int
    101 scsibusmatch(parent, match, aux)
    102         struct device *parent;
    103         void *match, *aux;
    104 {
    105 
    106 	return 1;
    107 }
    108 
    109 /*
    110  * The routine called by the adapter boards to get all their
    111  * devices configured in.
    112  */
    113 void
    114 scsibusattach(parent, self, aux)
    115         struct device *parent, *self;
    116         void *aux;
    117 {
    118 	struct scsibus_softc *sb = (struct scsibus_softc *)self;
    119 	struct scsi_link *sc_link_proto = aux;
    120 
    121 	sc_link_proto->scsibus = sb->sc_dev.dv_unit;
    122 	sb->adapter_link = sc_link_proto;
    123 	printf("\n");
    124 
    125 #if defined(SCSI_DELAY) && SCSI_DELAY > 2
    126 	printf("%s: waiting for scsi devices to settle\n",
    127 		sb->sc_dev.dv_xname);
    128 #else	/* SCSI_DELAY > 2 */
    129 #undef	SCSI_DELAY
    130 #define SCSI_DELAY 2
    131 #endif	/* SCSI_DELAY */
    132 	delay(1000000 * SCSI_DELAY);
    133 
    134 	scsi_probe_bus(sb->sc_dev.dv_unit, -1, -1);
    135 }
    136 
    137 int
    138 scsibussubmatch(parent, match, aux)
    139 	struct device *parent;
    140 	void *match, *aux;
    141 {
    142 	struct cfdata *cf = match;
    143 	struct scsibus_attach_args *sa = aux;
    144 	struct scsi_link *sc_link = sa->sa_sc_link;
    145 
    146 	if (cf->cf_loc[0] != -1 && cf->cf_loc[0] != sc_link->target)
    147 		return 0;
    148 	if (cf->cf_loc[1] != -1 && cf->cf_loc[1] != sc_link->lun)
    149 		return 0;
    150 	return ((*cf->cf_attach->ca_match)(parent, match, aux));
    151 }
    152 
    153 /*
    154  * Probe the requested scsi bus. It must be already set up.
    155  * -1 requests all set up scsi busses.
    156  * target and lun optionally narrow the search if not -1
    157  */
    158 int
    159 scsi_probe_busses(bus, target, lun)
    160 	int bus, target, lun;
    161 {
    162 
    163 	if (bus == -1) {
    164 		for (bus = 0; bus < scsibus_cd.cd_ndevs; bus++)
    165 			if (scsibus_cd.cd_devs[bus])
    166 				scsi_probe_bus(bus, target, lun);
    167 		return 0;
    168 	} else {
    169 		return scsi_probe_bus(bus, target, lun);
    170 	}
    171 }
    172 
    173 /*
    174  * Probe the requested scsi bus. It must be already set up.
    175  * target and lun optionally narrow the search if not -1
    176  */
    177 int
    178 scsi_probe_bus(bus, target, lun)
    179 	int bus, target, lun;
    180 {
    181 	struct scsibus_softc *scsi;
    182 	int maxtarget, mintarget, maxlun, minlun;
    183 	u_int8_t scsi_addr;
    184 
    185 	if (bus < 0 || bus >= scsibus_cd.cd_ndevs)
    186 		return ENXIO;
    187 	scsi = scsibus_cd.cd_devs[bus];
    188 	if (!scsi)
    189 		return ENXIO;
    190 
    191 	scsi_addr = scsi->adapter_link->adapter_target;
    192 
    193 	if (target == -1) {
    194 		maxtarget = 7;
    195 		mintarget = 0;
    196 	} else {
    197 		if (target < 0 || target > 7)
    198 			return EINVAL;
    199 		maxtarget = mintarget = target;
    200 	}
    201 
    202 	if (lun == -1) {
    203 		maxlun = 7;
    204 		minlun = 0;
    205 	} else {
    206 		if (lun < 0 || lun > 7)
    207 			return EINVAL;
    208 		maxlun = minlun = lun;
    209 	}
    210 
    211 	for (target = mintarget; target <= maxtarget; target++) {
    212 		if (target == scsi_addr)
    213 			continue;
    214 		for (lun = minlun; lun <= maxlun; lun++) {
    215 			/*
    216 			 * See if there's a device present, and configure it.
    217 			 */
    218 			scsi_probedev(scsi, target, lun);
    219 			if ((scsi->moreluns & (1 << target)) == 0)
    220 				break;
    221 			/* otherwise something says we should look further */
    222 		}
    223 	}
    224 	return 0;
    225 }
    226 
    227 void
    228 scsi_strvis(dst, src, len)
    229 	u_char *dst, *src;
    230 	int len;
    231 {
    232 
    233 	/* Trim leading and trailing blanks and NULs. */
    234 	while (len > 0 && (src[0] == ' ' || src[0] == '\0'))
    235 		++src, --len;
    236 	while (len > 0 && (src[len-1] == ' ' || src[len-1] == '\0'))
    237 		--len;
    238 
    239 	while (len > 0) {
    240 		if (*src < 0x20 || *src >= 0x80) {
    241 			/* non-printable characters */
    242 			*dst++ = '\\';
    243 			*dst++ = ((*src & 0300) >> 6) + '0';
    244 			*dst++ = ((*src & 0070) >> 3) + '0';
    245 			*dst++ = ((*src & 0007) >> 0) + '0';
    246 		} else if (*src == '\\') {
    247 			/* quote characters */
    248 			*dst++ = '\\';
    249 			*dst++ = '\\';
    250 		} else {
    251 			/* normal characters */
    252 			*dst++ = *src;
    253 		}
    254 		++src, --len;
    255 	}
    256 
    257 	*dst++ = 0;
    258 }
    259 
    260 struct scsi_quirk_inquiry_pattern {
    261 	struct scsi_inquiry_pattern pattern;
    262 	u_int8_t quirks;
    263 };
    264 
    265 struct scsi_quirk_inquiry_pattern scsi_quirk_patterns[] = {
    266 	{{T_CDROM, T_REMOV,
    267 	 "CHINON  ", "CD-ROM CDS-431  ", ""},     SDEV_NOLUNS},
    268 	{{T_CDROM, T_REMOV,
    269 	 "Chinon  ", "CD-ROM CDS-525  ", ""},     SDEV_NOLUNS},
    270 	{{T_CDROM, T_REMOV,
    271 	 "CHINON  ", "CD-ROM CDS-535  ", ""},     SDEV_NOLUNS},
    272 	{{T_CDROM, T_REMOV,
    273 	 "DENON   ", "DRD-25X         ", "V"},    SDEV_NOLUNS},
    274 	{{T_CDROM, T_REMOV,
    275 	 "IMS     ", "CDD521/10       ", "2.06"}, SDEV_NOLUNS},
    276 	{{T_CDROM, T_REMOV,
    277 	 "MEDIAVIS", "CDR-H93MV       ", "1.31"}, SDEV_NOLUNS},
    278 	{{T_CDROM, T_REMOV,
    279 	 "NEC     ", "CD-ROM DRIVE:55 ", ""},     SDEV_NOLUNS},
    280 	{{T_CDROM, T_REMOV,
    281 	 "NEC     ", "CD-ROM DRIVE:83 ", ""},     SDEV_NOLUNS},
    282 	{{T_CDROM, T_REMOV,
    283 	 "NEC     ", "CD-ROM DRIVE:84 ", ""},     SDEV_NOLUNS},
    284 	{{T_CDROM, T_REMOV,
    285 	 "NEC     ", "CD-ROM DRIVE:841", ""},     SDEV_NOLUNS},
    286 	{{T_CDROM, T_REMOV,
    287 	 "SONY    ", "CD-ROM CDU-541  ", ""},     SDEV_NOLUNS},
    288 	{{T_CDROM, T_REMOV,
    289 	 "SONY    ", "CD-ROM CDU-55S  ", ""},     SDEV_NOLUNS},
    290 	{{T_CDROM, T_REMOV,
    291 	 "SONY    ", "CD-ROM CDU-8003A", ""},     SDEV_NOLUNS},
    292 	{{T_CDROM, T_REMOV,
    293 	 "SONY    ", "CD-ROM CDU-8012 ", ""},     SDEV_NOLUNS},
    294 	{{T_CDROM, T_REMOV,
    295 	 "TEAC    ", "CD-ROM          ", "1.06"}, SDEV_NOLUNS},
    296 	{{T_CDROM, T_REMOV,
    297 	 "TEXEL   ", "CD-ROM          ", "1.06"}, SDEV_NOLUNS},
    298 	{{T_CDROM, T_REMOV,
    299 	 "TEXEL   ", "CD-ROM DM-XX24 K", "1.10"}, SDEV_NOLUNS},
    300 	{{T_CDROM, T_REMOV,
    301 	 "ShinaKen", "CD-ROM DM-3x1S", "1.04"}, SDEV_NOLUNS},
    302 
    303 
    304 	{{T_OPTICAL, T_REMOV,
    305 	 "EPSON   ", "OMD-5010        ", "3.08"}, SDEV_NOLUNS},
    306 
    307 	{{T_DIRECT, T_FIXED,
    308 	 "DEC     ", "RZ55     (C) DEC", ""},     SDEV_AUTOSAVE},
    309 	{{T_DIRECT, T_FIXED,
    310 	 "EMULEX  ", "MD21/S2     ESDI", "A00"},  SDEV_FORCELUNS},
    311 	{{T_DIRECT, T_FIXED,
    312 	 "MAXTOR  ", "XT-3280         ", ""},     SDEV_NOLUNS},
    313 	{{T_DIRECT, T_FIXED,
    314 	 "MAXTOR  ", "XT-4380S        ", ""},     SDEV_NOLUNS},
    315 	{{T_DIRECT, T_FIXED,
    316 	 "MAXTOR  ", "MXT-1240S       ", ""},     SDEV_NOLUNS},
    317 	{{T_DIRECT, T_FIXED,
    318 	 "MAXTOR  ", "XT-4170S        ", ""},     SDEV_NOLUNS},
    319 	{{T_DIRECT, T_FIXED,
    320 	 "MAXTOR  ", "XT-8760S        ", ""},     SDEV_NOLUNS},
    321 	{{T_DIRECT, T_FIXED,
    322 	 "MAXTOR  ", "LXT-213S        ", ""},     SDEV_NOLUNS},
    323 	{{T_DIRECT, T_FIXED,
    324 	 "MAXTOR  ", "LXT-213S SUN0207", ""},     SDEV_NOLUNS},
    325 	{{T_DIRECT, T_FIXED,
    326 	 "MAXTOR  ", "LXT-200S        ", ""},     SDEV_NOLUNS},
    327 	{{T_DIRECT, T_FIXED,
    328 	 "MST     ", "SnapLink        ", ""},     SDEV_NOLUNS},
    329 	{{T_DIRECT, T_FIXED,
    330 	 "NEC     ", "D3847           ", "0307"}, SDEV_NOLUNS},
    331 	{{T_DIRECT, T_FIXED,
    332 	 "QUANTUM ", "LPS525S         ", ""},     SDEV_NOLUNS},
    333 	{{T_DIRECT, T_FIXED,
    334 	 "QUANTUM ", "P105S 910-10-94x", ""},     SDEV_NOLUNS},
    335 	{{T_DIRECT, T_FIXED,
    336 	 "QUANTUM ", "PD1225S         ", ""},     SDEV_NOLUNS},
    337 	{{T_DIRECT, T_FIXED,
    338 	 "QUANTUM ", "PD210S   SUN0207", ""},     SDEV_NOLUNS},
    339 	{{T_DIRECT, T_FIXED,
    340 	 "RODIME  ", "RO3000S         ", ""},     SDEV_NOLUNS},
    341 	{{T_DIRECT, T_FIXED,
    342 	 "SEAGATE ", "ST157N          ", ""},     SDEV_NOLUNS},
    343 	{{T_DIRECT, T_FIXED,
    344 	 "SEAGATE ", "ST296           ", ""},     SDEV_NOLUNS},
    345 	{{T_DIRECT, T_FIXED,
    346 	 "SEAGATE ", "ST296N          ", ""},     SDEV_NOLUNS},
    347 	{{T_DIRECT, T_FIXED,
    348 	 "TOSHIBA ", "MK538FB         ", "6027"}, SDEV_NOLUNS},
    349 	{{T_DIRECT, T_REMOV,
    350 	 "iomega", "jaz 1GB", 		 ""},	  SDEV_NOMODESENSE},
    351 
    352 	/* XXX: QIC-36 tape behind Emulex adapter.  Very broken. */
    353 	{{T_SEQUENTIAL, T_REMOV,
    354 	 "        ", "                ", "    "}, SDEV_NOLUNS},
    355 	{{T_SEQUENTIAL, T_REMOV,
    356 	 "CALIPER ", "CP150           ", ""},     SDEV_NOLUNS},
    357 	{{T_SEQUENTIAL, T_REMOV,
    358 	 "EXABYTE ", "EXB-8200        ", ""},     SDEV_NOLUNS},
    359 	{{T_SEQUENTIAL, T_REMOV,
    360 	 "SONY    ", "SDT-2000        ", "2.09"}, SDEV_NOLUNS},
    361 	{{T_SEQUENTIAL, T_REMOV,
    362 	 "SONY    ", "SDT-5000        ", "3."},   SDEV_NOSYNCWIDE},
    363 	{{T_SEQUENTIAL, T_REMOV,
    364 	 "SONY    ", "SDT-5200        ", "3."},   SDEV_NOLUNS},
    365 	{{T_SEQUENTIAL, T_REMOV,
    366 	 "TANDBERG", " TDC 3600       ", ""},     SDEV_NOLUNS},
    367 	/* Following entry reported as a Tandberg 3600; ref. PR1933 */
    368 	{{T_SEQUENTIAL, T_REMOV,
    369 	 "ARCHIVE ", "VIPER 150  21247", ""},     SDEV_NOLUNS},
    370 	{{T_SEQUENTIAL, T_REMOV,
    371 	 "WANGTEK ", "5099ES SCSI",      ""},     SDEV_NOLUNS},
    372 	{{T_SEQUENTIAL, T_REMOV,
    373 	 "WANGTEK ", "5150ES SCSI",      ""},     SDEV_NOLUNS},
    374 	{{T_SEQUENTIAL, T_REMOV,
    375 	 "WangDAT ", "Model 1300      ", "02.4"}, SDEV_NOSYNCWIDE},
    376 	{{T_SEQUENTIAL, T_REMOV,
    377 	 "WangDAT ", "Model 2600      ", "01.7"}, SDEV_NOSYNCWIDE},
    378 	{{T_SEQUENTIAL, T_REMOV,
    379 	 "WangDAT ", "Model 3200      ", "02.2"}, SDEV_NOSYNCWIDE},
    380 };
    381 
    382 /*
    383  * Print out autoconfiguration information for a subdevice.
    384  *
    385  * This is a slight abuse of 'standard' autoconfiguration semantics,
    386  * because 'print' functions don't normally print the colon and
    387  * device information.  However, in this case that's better than
    388  * either printing redundant information before the attach message,
    389  * or having the device driver call a special function to print out
    390  * the standard device information.
    391  */
    392 int
    393 scsibusprint(aux, pnp)
    394 	void *aux;
    395 	char *pnp;
    396 {
    397 	struct scsibus_attach_args *sa = aux;
    398 	struct scsi_inquiry_data *inqbuf;
    399 	u_int8_t type;
    400 	boolean removable;
    401 	char *dtype, *qtype;
    402 	char vendor[33], product[65], revision[17];
    403 	int target, lun;
    404 
    405 	if (pnp != NULL)
    406 		printf("%s", pnp);
    407 
    408 	inqbuf = sa->sa_inqbuf;
    409 
    410         target = sa->sa_sc_link->target;
    411         lun = sa->sa_sc_link->lun;
    412 
    413         type = inqbuf->device & SID_TYPE;
    414         removable = inqbuf->dev_qual2 & SID_REMOVABLE ? 1 : 0;
    415 
    416 	/*
    417 	 * Figure out basic device type and qualifier.
    418 	 */
    419 	dtype = 0;
    420 	switch (inqbuf->device & SID_QUAL) {
    421 	case SID_QUAL_LU_OK:
    422 		qtype = "";
    423 		break;
    424 
    425 	case SID_QUAL_LU_OFFLINE:
    426 		qtype = " offline";
    427 		break;
    428 
    429 	case SID_QUAL_RSVD:
    430 	case SID_QUAL_BAD_LU:
    431 		panic("scsibusprint: impossible qualifier");
    432 
    433 	default:
    434 		qtype = "";
    435 		dtype = "vendor-unique";
    436 		break;
    437 	}
    438 	if (dtype == 0) {
    439 		switch (type) {
    440 		case T_DIRECT:
    441 			dtype = "direct";
    442 			break;
    443 		case T_SEQUENTIAL:
    444 			dtype = "sequential";
    445 			break;
    446 		case T_PRINTER:
    447 			dtype = "printer";
    448 			break;
    449 		case T_PROCESSOR:
    450 			dtype = "processor";
    451 			break;
    452 		case T_CDROM:
    453 			dtype = "cdrom";
    454 			break;
    455 		case T_WORM:
    456 			dtype = "worm";
    457 			break;
    458 		case T_SCANNER:
    459 			dtype = "scanner";
    460 			break;
    461 		case T_OPTICAL:
    462 			dtype = "optical";
    463 			break;
    464 		case T_CHANGER:
    465 			dtype = "changer";
    466 			break;
    467 		case T_COMM:
    468 			dtype = "communication";
    469 			break;
    470 		case T_NODEVICE:
    471 			panic("scsibusprint: impossible device type");
    472 		default:
    473 			dtype = "unknown";
    474 			break;
    475 		}
    476 	}
    477 
    478         scsi_strvis(vendor, inqbuf->vendor, 8);
    479         scsi_strvis(product, inqbuf->product, 16);
    480         scsi_strvis(revision, inqbuf->revision, 4);
    481 
    482         printf(" targ %d lun %d: <%s, %s, %s> SCSI%d %d/%s %s%s",
    483             target, lun, vendor, product, revision,
    484             inqbuf->version & SID_ANSII, type, dtype,
    485             removable ? "removable" : "fixed", qtype);
    486 
    487 	return (UNCONF);
    488 }
    489 
    490 /*
    491  * given a target and lu, ask the device what
    492  * it is, and find the correct driver table
    493  * entry.
    494  */
    495 void
    496 scsi_probedev(scsi, target, lun)
    497 	struct scsibus_softc *scsi;
    498 	int target, lun;
    499 {
    500 	struct scsi_link *sc_link;
    501 	static struct scsi_inquiry_data inqbuf;
    502 	struct scsi_quirk_inquiry_pattern *finger;
    503 	int checkdtype, priority;
    504 	struct scsibus_attach_args sa;
    505 	struct cfdata *cf;
    506 
    507 	/* Skip this slot if it is already attached. */
    508 	if (scsi->sc_link[target][lun])
    509 		return;
    510 
    511 	sc_link = malloc(sizeof(*sc_link), M_DEVBUF, M_NOWAIT);
    512 	*sc_link = *scsi->adapter_link;
    513 	sc_link->target = target;
    514 	sc_link->lun = lun;
    515 	sc_link->device = &probe_switch;
    516 
    517 	/*
    518 	 * Ask the device what it is
    519 	 */
    520 #ifdef SCSIDEBUG
    521 	if (target == DEBUGTARGET && lun == DEBUGLUN)
    522 		sc_link->flags |= DEBUGLEVEL;
    523 #endif /* SCSIDEBUG */
    524 
    525 	(void) scsi_test_unit_ready(sc_link,
    526 	    SCSI_AUTOCONF | SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_NOT_READY | SCSI_IGNORE_MEDIA_CHANGE);
    527 
    528 #ifdef SCSI_2_DEF
    529 	/* some devices need to be told to go to SCSI2 */
    530 	/* However some just explode if you tell them this.. leave it out */
    531 	scsi_change_def(sc_link, SCSI_AUTOCONF | SCSI_SILENT);
    532 #endif /* SCSI_2_DEF */
    533 
    534 	/* Now go ask the device all about itself. */
    535 	bzero(&inqbuf, sizeof(inqbuf));
    536 	if (scsi_inquire(sc_link, &inqbuf, SCSI_AUTOCONF) != 0)
    537 		goto bad;
    538 
    539 	{
    540 		int len = inqbuf.additional_length;
    541 		while (len < 3)
    542 			inqbuf.unused[len++] = '\0';
    543 		while (len < 3 + 28)
    544 			inqbuf.unused[len++] = ' ';
    545 	}
    546 
    547 	finger = (struct scsi_quirk_inquiry_pattern *)scsi_inqmatch(&inqbuf,
    548 	    (caddr_t)scsi_quirk_patterns,
    549 	    sizeof(scsi_quirk_patterns)/sizeof(scsi_quirk_patterns[0]),
    550 	    sizeof(scsi_quirk_patterns[0]), &priority);
    551 	if (priority != 0)
    552 		sc_link->quirks |= finger->quirks;
    553 	if ((inqbuf.version & SID_ANSII) == 0 &&
    554 	    (sc_link->quirks & SDEV_FORCELUNS) == 0)
    555 		sc_link->quirks |= SDEV_NOLUNS;
    556 
    557 	if ((sc_link->quirks & SDEV_NOLUNS) == 0)
    558 		scsi->moreluns |= (1 << target);
    559 
    560 	/*
    561 	 * note what BASIC type of device it is
    562 	 */
    563 	if ((inqbuf.dev_qual2 & SID_REMOVABLE) != 0)
    564 		sc_link->flags |= SDEV_REMOVABLE;
    565 
    566 	/*
    567 	 * Any device qualifier that has the top bit set (qualifier&4 != 0)
    568 	 * is vendor specific and won't match in this switch.
    569 	 * All we do here is throw out bad/negative responses.
    570 	 */
    571 	checkdtype = 0;
    572 	switch (inqbuf.device & SID_QUAL) {
    573 	case SID_QUAL_LU_OK:
    574 	case SID_QUAL_LU_OFFLINE:
    575 		checkdtype = 1;
    576 		break;
    577 
    578 	case SID_QUAL_RSVD:
    579 	case SID_QUAL_BAD_LU:
    580 		goto bad;
    581 
    582 	default:
    583 		break;
    584 	}
    585 	if (checkdtype) {
    586 		switch (inqbuf.device & SID_TYPE) {
    587 		case T_DIRECT:
    588 		case T_SEQUENTIAL:
    589 		case T_PRINTER:
    590 		case T_PROCESSOR:
    591 		case T_CDROM:
    592 		case T_WORM:
    593 		case T_SCANNER:
    594 		case T_OPTICAL:
    595 		case T_CHANGER:
    596 		case T_COMM:
    597 		default:
    598 			break;
    599 		case T_NODEVICE:
    600 			goto bad;
    601 		}
    602 	}
    603 
    604 	sa.sa_sc_link = sc_link;
    605 	sa.sa_inqbuf = &inqbuf;
    606 
    607 	if ((cf = config_search(scsibussubmatch, (struct device *)scsi, &sa)) != 0) {
    608 		scsi->sc_link[target][lun] = sc_link;
    609 		config_attach((struct device *)scsi, cf, &sa, scsibusprint);
    610 	} else {
    611 		scsibusprint(&sa, scsi->sc_dev.dv_xname);
    612 		printf(" not configured\n");
    613 		goto bad;
    614 	}
    615 
    616 	return;
    617 
    618 bad:
    619 	free(sc_link, M_DEVBUF);
    620 	return;
    621 }
    622 
    623 /*
    624  * Return a priority based on how much of the inquiry data matches
    625  * the patterns for the particular driver.
    626  */
    627 caddr_t
    628 scsi_inqmatch(inqbuf, base, nmatches, matchsize, bestpriority)
    629 	struct scsi_inquiry_data *inqbuf;
    630 	caddr_t base;
    631 	int nmatches, matchsize;
    632 	int *bestpriority;
    633 {
    634 	u_int8_t type;
    635 	boolean removable;
    636 	caddr_t bestmatch;
    637 
    638 	/* Include the qualifier to catch vendor-unique types. */
    639 	type = inqbuf->device;
    640 	removable = inqbuf->dev_qual2 & SID_REMOVABLE ? T_REMOV : T_FIXED;
    641 
    642 	for (*bestpriority = 0, bestmatch = 0; nmatches--; base += matchsize) {
    643 		struct scsi_inquiry_pattern *match = (void *)base;
    644 		int priority, len;
    645 
    646 		if (type != match->type)
    647 			continue;
    648 		if (removable != match->removable)
    649 			continue;
    650 		priority = 2;
    651 		len = strlen(match->vendor);
    652 		if (bcmp(inqbuf->vendor, match->vendor, len))
    653 			continue;
    654 		priority += len;
    655 		len = strlen(match->product);
    656 		if (bcmp(inqbuf->product, match->product, len))
    657 			continue;
    658 		priority += len;
    659 		len = strlen(match->revision);
    660 		if (bcmp(inqbuf->revision, match->revision, len))
    661 			continue;
    662 		priority += len;
    663 
    664 #if SCSIDEBUG
    665 		printf("scsi_inqmatch: %d/%d/%d <%s, %s, %s>\n",
    666 		    priority, match->type, match->removable,
    667 		    match->vendor, match->product, match->revision);
    668 #endif
    669 		if (priority > *bestpriority) {
    670 			*bestpriority = priority;
    671 			bestmatch = base;
    672 		}
    673 	}
    674 
    675 	return (bestmatch);
    676 }
    677