Home | History | Annotate | Line # | Download | only in scsipi
scsiconf.c revision 1.56
      1 /*	$NetBSD: scsiconf.c,v 1.56 1996/04/22 01:46:09 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 
    301 	{{T_OPTICAL, T_REMOV,
    302 	 "EPSON   ", "OMD-5010        ", "3.08"}, SDEV_NOLUNS},
    303 
    304 	{{T_DIRECT, T_FIXED,
    305 	 "DEC     ", "RZ55     (C) DEC", ""},     SDEV_AUTOSAVE},
    306 	{{T_DIRECT, T_FIXED,
    307 	 "EMULEX  ", "MD21/S2     ESDI", "A00"},  SDEV_FORCELUNS},
    308 	{{T_DIRECT, T_FIXED,
    309 	 "MAXTOR  ", "XT-3280         ", ""},     SDEV_NOLUNS},
    310 	{{T_DIRECT, T_FIXED,
    311 	 "MAXTOR  ", "XT-4380S        ", ""},     SDEV_NOLUNS},
    312 	{{T_DIRECT, T_FIXED,
    313 	 "MAXTOR  ", "MXT-1240S       ", ""},     SDEV_NOLUNS},
    314 	{{T_DIRECT, T_FIXED,
    315 	 "MAXTOR  ", "XT-4170S        ", ""},     SDEV_NOLUNS},
    316 	{{T_DIRECT, T_FIXED,
    317 	 "MAXTOR  ", "XT-8760S        ", ""},     SDEV_NOLUNS},
    318 	{{T_DIRECT, T_FIXED,
    319 	 "MAXTOR  ", "LXT-213S        ", ""},     SDEV_NOLUNS},
    320 	{{T_DIRECT, T_FIXED,
    321 	 "MAXTOR  ", "LXT-213S SUN0207", ""},     SDEV_NOLUNS},
    322 	{{T_DIRECT, T_FIXED,
    323 	 "MAXTOR  ", "LXT-200S        ", ""},     SDEV_NOLUNS},
    324 	{{T_DIRECT, T_FIXED,
    325 	 "MST     ", "SnapLink        ", ""},     SDEV_NOLUNS},
    326 	{{T_DIRECT, T_FIXED,
    327 	 "NEC     ", "D3847           ", "0307"}, SDEV_NOLUNS},
    328 	{{T_DIRECT, T_FIXED,
    329 	 "QUANTUM ", "LPS525S         ", ""},     SDEV_NOLUNS},
    330 	{{T_DIRECT, T_FIXED,
    331 	 "QUANTUM ", "P105S 910-10-94x", ""},     SDEV_NOLUNS},
    332 	{{T_DIRECT, T_FIXED,
    333 	 "QUANTUM ", "PD1225S         ", ""},     SDEV_NOLUNS},
    334 	{{T_DIRECT, T_FIXED,
    335 	 "QUANTUM ", "PD210S   SUN0207", ""},     SDEV_NOLUNS},
    336 	{{T_DIRECT, T_FIXED,
    337 	 "RODIME  ", "RO3000S         ", ""},     SDEV_NOLUNS},
    338 	{{T_DIRECT, T_FIXED,
    339 	 "SEAGATE ", "ST157N          ", ""},     SDEV_NOLUNS},
    340 	{{T_DIRECT, T_FIXED,
    341 	 "SEAGATE ", "ST296           ", ""},     SDEV_NOLUNS},
    342 	{{T_DIRECT, T_FIXED,
    343 	 "SEAGATE ", "ST296N          ", ""},     SDEV_NOLUNS},
    344 	{{T_DIRECT, T_FIXED,
    345 	 "TOSHIBA ", "MK538FB         ", "6027"}, SDEV_NOLUNS},
    346 
    347 
    348 	/* XXX: QIC-36 tape behind Emulex adapter.  Very broken. */
    349 	{{T_SEQUENTIAL, T_REMOV,
    350 	 "        ", "                ", "    "}, SDEV_NOLUNS},
    351 	{{T_SEQUENTIAL, T_REMOV,
    352 	 "CALIPER ", "CP150           ", ""},     SDEV_NOLUNS},
    353 	{{T_SEQUENTIAL, T_REMOV,
    354 	 "EXABYTE ", "EXB-8200        ", ""},     SDEV_NOLUNS},
    355 	{{T_SEQUENTIAL, T_REMOV,
    356 	 "SONY    ", "SDT-2000        ", "2.09"}, SDEV_NOLUNS},
    357 	{{T_SEQUENTIAL, T_REMOV,
    358 	 "SONY    ", "SDT-5000        ", "3."},   SDEV_NOSYNCWIDE},
    359 	{{T_SEQUENTIAL, T_REMOV,
    360 	 "SONY    ", "SDT-5200        ", "3."},   SDEV_NOLUNS},
    361 	{{T_SEQUENTIAL, T_REMOV,
    362 	 "TANDBERG", " TDC 3600       ", ""},     SDEV_NOLUNS},
    363 	/* Following entry reported as a Tandberg 3600; ref. PR1933 */
    364 	{{T_SEQUENTIAL, T_REMOV,
    365 	 "ARCHIVE ", "VIPER 150  21247", ""},     SDEV_NOLUNS},
    366 	{{T_SEQUENTIAL, T_REMOV,
    367 	 "WANGTEK ", "5099ES SCSI",      ""},     SDEV_NOLUNS},
    368 	{{T_SEQUENTIAL, T_REMOV,
    369 	 "WANGTEK ", "5150ES SCSI",      ""},     SDEV_NOLUNS},
    370 	{{T_SEQUENTIAL, T_REMOV,
    371 	 "WangDAT ", "Model 1300      ", "02.4"}, SDEV_NOSYNCWIDE},
    372 	{{T_SEQUENTIAL, T_REMOV,
    373 	 "WangDAT ", "Model 2600      ", "01.7"}, SDEV_NOSYNCWIDE},
    374 	{{T_SEQUENTIAL, T_REMOV,
    375 	 "WangDAT ", "Model 3200      ", "02.2"}, SDEV_NOSYNCWIDE},
    376 };
    377 
    378 /*
    379  * Print out autoconfiguration information for a subdevice.
    380  *
    381  * This is a slight abuse of 'standard' autoconfiguration semantics,
    382  * because 'print' functions don't normally print the colon and
    383  * device information.  However, in this case that's better than
    384  * either printing redundant information before the attach message,
    385  * or having the device driver call a special function to print out
    386  * the standard device information.
    387  */
    388 int
    389 scsibusprint(aux, pnp)
    390 	void *aux;
    391 	char *pnp;
    392 {
    393 	struct scsibus_attach_args *sa = aux;
    394 	struct scsi_inquiry_data *inqbuf;
    395 	u_int8_t type;
    396 	boolean removable;
    397 	char *dtype, *qtype;
    398 	char vendor[33], product[65], revision[17];
    399 	int target, lun;
    400 
    401 	if (pnp != NULL)
    402 		printf("%s", pnp);
    403 
    404 	inqbuf = sa->sa_inqbuf;
    405 
    406         target = sa->sa_sc_link->target;
    407         lun = sa->sa_sc_link->lun;
    408 
    409         type = inqbuf->device & SID_TYPE;
    410         removable = inqbuf->dev_qual2 & SID_REMOVABLE ? 1 : 0;
    411 
    412 	/*
    413 	 * Figure out basic device type and qualifier.
    414 	 */
    415 	dtype = 0;
    416 	switch (inqbuf->device & SID_QUAL) {
    417 	case SID_QUAL_LU_OK:
    418 		qtype = "";
    419 		break;
    420 
    421 	case SID_QUAL_LU_OFFLINE:
    422 		qtype = " offline";
    423 		break;
    424 
    425 	case SID_QUAL_RSVD:
    426 	case SID_QUAL_BAD_LU:
    427 		panic("scsibusprint: impossible qualifier");
    428 
    429 	default:
    430 		qtype = "";
    431 		dtype = "vendor-unique";
    432 		break;
    433 	}
    434 	if (dtype == 0) {
    435 		switch (type) {
    436 		case T_DIRECT:
    437 			dtype = "direct";
    438 			break;
    439 		case T_SEQUENTIAL:
    440 			dtype = "sequential";
    441 			break;
    442 		case T_PRINTER:
    443 			dtype = "printer";
    444 			break;
    445 		case T_PROCESSOR:
    446 			dtype = "processor";
    447 			break;
    448 		case T_CDROM:
    449 			dtype = "cdrom";
    450 			break;
    451 		case T_WORM:
    452 			dtype = "worm";
    453 			break;
    454 		case T_SCANNER:
    455 			dtype = "scanner";
    456 			break;
    457 		case T_OPTICAL:
    458 			dtype = "optical";
    459 			break;
    460 		case T_CHANGER:
    461 			dtype = "changer";
    462 			break;
    463 		case T_COMM:
    464 			dtype = "communication";
    465 			break;
    466 		case T_NODEVICE:
    467 			panic("scsibusprint: impossible device type");
    468 		default:
    469 			dtype = "unknown";
    470 			break;
    471 		}
    472 	}
    473 
    474         scsi_strvis(vendor, inqbuf->vendor, 8);
    475         scsi_strvis(product, inqbuf->product, 16);
    476         scsi_strvis(revision, inqbuf->revision, 4);
    477 
    478         printf(" targ %d lun %d: <%s, %s, %s> SCSI%d %d/%s %s%s",
    479             target, lun, vendor, product, revision,
    480             inqbuf->version & SID_ANSII, type, dtype,
    481             removable ? "removable" : "fixed", qtype);
    482 
    483 	return (UNCONF);
    484 }
    485 
    486 /*
    487  * given a target and lu, ask the device what
    488  * it is, and find the correct driver table
    489  * entry.
    490  */
    491 void
    492 scsi_probedev(scsi, target, lun)
    493 	struct scsibus_softc *scsi;
    494 	int target, lun;
    495 {
    496 	struct scsi_link *sc_link;
    497 	static struct scsi_inquiry_data inqbuf;
    498 	struct scsi_quirk_inquiry_pattern *finger;
    499 	int checkdtype, priority;
    500 	struct scsibus_attach_args sa;
    501 	struct cfdata *cf;
    502 
    503 	/* Skip this slot if it is already attached. */
    504 	if (scsi->sc_link[target][lun])
    505 		return;
    506 
    507 	sc_link = malloc(sizeof(*sc_link), M_DEVBUF, M_NOWAIT);
    508 	*sc_link = *scsi->adapter_link;
    509 	sc_link->target = target;
    510 	sc_link->lun = lun;
    511 	sc_link->device = &probe_switch;
    512 
    513 	/*
    514 	 * Ask the device what it is
    515 	 */
    516 #ifdef SCSIDEBUG
    517 	if (target == DEBUGTARGET && lun == DEBUGLUN)
    518 		sc_link->flags |= DEBUGLEVEL;
    519 #endif /* SCSIDEBUG */
    520 
    521 	(void) scsi_test_unit_ready(sc_link,
    522 	    SCSI_AUTOCONF | SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_NOT_READY | SCSI_IGNORE_MEDIA_CHANGE);
    523 
    524 #ifdef SCSI_2_DEF
    525 	/* some devices need to be told to go to SCSI2 */
    526 	/* However some just explode if you tell them this.. leave it out */
    527 	scsi_change_def(sc_link, SCSI_AUTOCONF | SCSI_SILENT);
    528 #endif /* SCSI_2_DEF */
    529 
    530 	/* Now go ask the device all about itself. */
    531 	bzero(&inqbuf, sizeof(inqbuf));
    532 	if (scsi_inquire(sc_link, &inqbuf, SCSI_AUTOCONF) != 0)
    533 		goto bad;
    534 
    535 	{
    536 		int len = inqbuf.additional_length;
    537 		while (len < 3)
    538 			inqbuf.unused[len++] = '\0';
    539 		while (len < 3 + 28)
    540 			inqbuf.unused[len++] = ' ';
    541 	}
    542 
    543 	finger = (struct scsi_quirk_inquiry_pattern *)scsi_inqmatch(&inqbuf,
    544 	    (caddr_t)scsi_quirk_patterns,
    545 	    sizeof(scsi_quirk_patterns)/sizeof(scsi_quirk_patterns[0]),
    546 	    sizeof(scsi_quirk_patterns[0]), &priority);
    547 	if (priority != 0)
    548 		sc_link->quirks |= finger->quirks;
    549 	if ((inqbuf.version & SID_ANSII) == 0 &&
    550 	    (sc_link->quirks & SDEV_FORCELUNS) == 0)
    551 		sc_link->quirks |= SDEV_NOLUNS;
    552 
    553 	if ((sc_link->quirks & SDEV_NOLUNS) == 0)
    554 		scsi->moreluns |= (1 << target);
    555 
    556 	/*
    557 	 * note what BASIC type of device it is
    558 	 */
    559 	if ((inqbuf.dev_qual2 & SID_REMOVABLE) != 0)
    560 		sc_link->flags |= SDEV_REMOVABLE;
    561 
    562 	/*
    563 	 * Any device qualifier that has the top bit set (qualifier&4 != 0)
    564 	 * is vendor specific and won't match in this switch.
    565 	 * All we do here is throw out bad/negative responses.
    566 	 */
    567 	checkdtype = 0;
    568 	switch (inqbuf.device & SID_QUAL) {
    569 	case SID_QUAL_LU_OK:
    570 	case SID_QUAL_LU_OFFLINE:
    571 		checkdtype = 1;
    572 		break;
    573 
    574 	case SID_QUAL_RSVD:
    575 	case SID_QUAL_BAD_LU:
    576 		goto bad;
    577 
    578 	default:
    579 		break;
    580 	}
    581 	if (checkdtype) {
    582 		switch (inqbuf.device & SID_TYPE) {
    583 		case T_DIRECT:
    584 		case T_SEQUENTIAL:
    585 		case T_PRINTER:
    586 		case T_PROCESSOR:
    587 		case T_CDROM:
    588 		case T_WORM:
    589 		case T_SCANNER:
    590 		case T_OPTICAL:
    591 		case T_CHANGER:
    592 		case T_COMM:
    593 		default:
    594 			break;
    595 		case T_NODEVICE:
    596 			goto bad;
    597 		}
    598 	}
    599 
    600 	sa.sa_sc_link = sc_link;
    601 	sa.sa_inqbuf = &inqbuf;
    602 
    603 	if ((cf = config_search(scsibussubmatch, (struct device *)scsi, &sa)) != 0) {
    604 		scsi->sc_link[target][lun] = sc_link;
    605 		config_attach((struct device *)scsi, cf, &sa, scsibusprint);
    606 	} else {
    607 		scsibusprint(&sa, scsi->sc_dev.dv_xname);
    608 		printf(" not configured\n");
    609 		goto bad;
    610 	}
    611 
    612 	return;
    613 
    614 bad:
    615 	free(sc_link, M_DEVBUF);
    616 	return;
    617 }
    618 
    619 /*
    620  * Return a priority based on how much of the inquiry data matches
    621  * the patterns for the particular driver.
    622  */
    623 caddr_t
    624 scsi_inqmatch(inqbuf, base, nmatches, matchsize, bestpriority)
    625 	struct scsi_inquiry_data *inqbuf;
    626 	caddr_t base;
    627 	int nmatches, matchsize;
    628 	int *bestpriority;
    629 {
    630 	u_int8_t type;
    631 	boolean removable;
    632 	caddr_t bestmatch;
    633 
    634 	/* Include the qualifier to catch vendor-unique types. */
    635 	type = inqbuf->device;
    636 	removable = inqbuf->dev_qual2 & SID_REMOVABLE ? T_REMOV : T_FIXED;
    637 
    638 	for (*bestpriority = 0, bestmatch = 0; nmatches--; base += matchsize) {
    639 		struct scsi_inquiry_pattern *match = (void *)base;
    640 		int priority, len;
    641 
    642 		if (type != match->type)
    643 			continue;
    644 		if (removable != match->removable)
    645 			continue;
    646 		priority = 2;
    647 		len = strlen(match->vendor);
    648 		if (bcmp(inqbuf->vendor, match->vendor, len))
    649 			continue;
    650 		priority += len;
    651 		len = strlen(match->product);
    652 		if (bcmp(inqbuf->product, match->product, len))
    653 			continue;
    654 		priority += len;
    655 		len = strlen(match->revision);
    656 		if (bcmp(inqbuf->revision, match->revision, len))
    657 			continue;
    658 		priority += len;
    659 
    660 #if SCSIDEBUG
    661 		printf("scsi_inqmatch: %d/%d/%d <%s, %s, %s>\n",
    662 		    priority, match->type, match->removable,
    663 		    match->vendor, match->product, match->revision);
    664 #endif
    665 		if (priority > *bestpriority) {
    666 			*bestpriority = priority;
    667 			bestmatch = base;
    668 		}
    669 	}
    670 
    671 	return (bestmatch);
    672 }
    673