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