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