Home | History | Annotate | Line # | Download | only in scsipi
scsiconf.c revision 1.129
      1 /*	$NetBSD: scsiconf.c,v 1.129 1999/10/10 18:49:15 hwr Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Charles M. Hannum; Jason R. Thorpe of the Numerical Aerospace
      9  * Simulation Facility, NASA Ames Research Center.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *        This product includes software developed by the NetBSD
     22  *        Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * Originally written by Julian Elischer (julian (at) tfs.com)
     42  * for TRW Financial Systems for use under the MACH(2.5) operating system.
     43  *
     44  * TRW Financial Systems, in accordance with their agreement with Carnegie
     45  * Mellon University, makes this software available to CMU to distribute
     46  * or use in any manner that they see fit as long as this message is kept with
     47  * the software. For this reason TFS also grants any other persons or
     48  * organisations permission to use or modify this software.
     49  *
     50  * TFS supplies this software to be publicly redistributed
     51  * on the understanding that TFS is not responsible for the correct
     52  * functioning of this software in any circumstances.
     53  *
     54  * Ported to run under 386BSD by Julian Elischer (julian (at) tfs.com) Sept 1992
     55  */
     56 
     57 #include <sys/types.h>
     58 #include <sys/param.h>
     59 #include <sys/systm.h>
     60 #include <sys/kernel.h>
     61 #include <sys/proc.h>
     62 #include <sys/malloc.h>
     63 #include <sys/device.h>
     64 #include <sys/conf.h>
     65 #include <sys/fcntl.h>
     66 #include <sys/scsiio.h>
     67 
     68 #include <dev/scsipi/scsi_all.h>
     69 #include <dev/scsipi/scsipi_all.h>
     70 #include <dev/scsipi/scsiconf.h>
     71 
     72 #include "locators.h"
     73 
     74 #if 0
     75 #if NCALS > 0
     76 	{ T_PROCESSOR, T_FIXED, 1,
     77 	  0, 0, 0 },
     78 #endif	/* NCALS */
     79 #if NBLL > 0
     80 	{ T_PROCESSOR, T_FIXED, 1,
     81 	  "AEG     ", "READER          ", "V1.0" },
     82 #endif	/* NBLL */
     83 #if NKIL > 0
     84 	{ T_SCANNER, T_FIXED, 0,
     85 	  "KODAK   ", "IL Scanner 900  ", 0 },
     86 #endif	/* NKIL */
     87 #endif
     88 
     89 /*
     90  * Declarations
     91  */
     92 int scsi_probedev __P((struct scsibus_softc *, int, int));
     93 int scsi_probe_bus __P((int bus, int target, int lun));
     94 
     95 struct scsipi_device probe_switch = {
     96 	NULL,
     97 	NULL,
     98 	NULL,
     99 	NULL,
    100 };
    101 
    102 int scsibusmatch __P((struct device *, struct cfdata *, void *));
    103 void scsibusattach __P((struct device *, struct device *, void *));
    104 int scsibusactivate __P((struct device *, enum devact));
    105 int scsibusdetach __P((struct device *, int flags));
    106 
    107 int scsibussubmatch __P((struct device *, struct cfdata *, void *));
    108 
    109 struct cfattach scsibus_ca = {
    110 	sizeof(struct scsibus_softc), scsibusmatch, scsibusattach,
    111 	    scsibusdetach, scsibusactivate,
    112 };
    113 
    114 extern struct cfdriver scsibus_cd;
    115 
    116 int scsibusprint __P((void *, const char *));
    117 void scsibus_config_interrupts __P((struct device *));
    118 
    119 cdev_decl(scsibus);
    120 
    121 int
    122 scsiprint(aux, pnp)
    123 	void *aux;
    124 	const char *pnp;
    125 {
    126 	struct scsipi_link *l = aux;
    127 
    128 	/* only "scsibus"es can attach to "scsi"s; easy. */
    129 	if (pnp)
    130 		printf("scsibus at %s", pnp);
    131 
    132 	/* don't print channel if the controller says there can be only one. */
    133 	if (l->scsipi_scsi.channel != SCSI_CHANNEL_ONLY_ONE)
    134 		printf(" channel %d", l->scsipi_scsi.channel);
    135 
    136 	return (UNCONF);
    137 }
    138 
    139 int
    140 scsibusmatch(parent, cf, aux)
    141 	struct device *parent;
    142 	struct cfdata *cf;
    143 	void *aux;
    144 {
    145 	struct scsipi_link *l = aux;
    146 	int channel;
    147 
    148 	/*
    149 	 * Allow single-channel controllers to specify their channel
    150 	 * in a special way, so that it's not printed.
    151 	 */
    152 	channel = (l->scsipi_scsi.channel != SCSI_CHANNEL_ONLY_ONE) ?
    153 	    l->scsipi_scsi.channel : 0;
    154 
    155 	if (cf->cf_loc[SCSICF_CHANNEL] != channel &&
    156 	    cf->cf_loc[SCSICF_CHANNEL] != SCSICF_CHANNEL_DEFAULT)
    157 		return (0);
    158 
    159 	return (1);
    160 }
    161 
    162 /*
    163  * The routine called by the adapter boards to get all their
    164  * devices configured in.
    165  */
    166 void
    167 scsibusattach(parent, self, aux)
    168 	struct device *parent, *self;
    169 	void *aux;
    170 {
    171 	struct scsibus_softc *sb = (struct scsibus_softc *)self;
    172 	struct scsipi_link *sc_link_proto = aux;
    173 	size_t nbytes;
    174 	int i;
    175 
    176 	sc_link_proto->scsipi_scsi.scsibus = sb->sc_dev.dv_unit;
    177 	sc_link_proto->scsipi_cmd = scsi_scsipi_cmd;
    178 	sc_link_proto->scsipi_interpret_sense = scsipi_interpret_sense;
    179 	sc_link_proto->sc_print_addr = scsi_print_addr;
    180 
    181 	sb->adapter_link = sc_link_proto;
    182 	sb->sc_maxtarget = sc_link_proto->scsipi_scsi.max_target;
    183 	sb->sc_maxlun = sc_link_proto->scsipi_scsi.max_lun;
    184 	printf(": %d targets, %d luns per target\n",
    185 	    sb->sc_maxtarget + 1, sb->sc_maxlun + 1);
    186 
    187 	/* Initialize shared data. */
    188 	scsipi_init();
    189 
    190 	nbytes = (sb->sc_maxtarget + 1) * sizeof(struct scsipi_link **);
    191 	sb->sc_link = (struct scsipi_link ***)malloc(nbytes, M_DEVBUF,
    192 	    M_NOWAIT);
    193 	if (sb->sc_link == NULL)
    194 		panic("scsibusattach: can't allocate target links");
    195 
    196 	nbytes = (((int) sb->sc_maxlun) + 1) * sizeof(struct scsipi_link *);
    197 	for (i = 0; i <= sb->sc_maxtarget; i++) {
    198 		sb->sc_link[i] = (struct scsipi_link **)malloc(nbytes,
    199 		    M_DEVBUF, M_NOWAIT);
    200 		if (sb->sc_link[i] == NULL)
    201 			panic("scsibusattach: can't allocate lun links");
    202 		bzero(sb->sc_link[i], nbytes);
    203 	}
    204 
    205 	/*
    206 	 * Defer configuration of the children until interrupts
    207 	 * are enabled.
    208 	 */
    209 	config_interrupts(self, scsibus_config_interrupts);
    210 }
    211 
    212 void
    213 scsibus_config_interrupts(self)
    214 	struct device *self;
    215 {
    216 
    217 #if defined(SCSI_DELAY) && SCSI_DELAY > 2
    218 #else	/* SCSI_DELAY > 2 */
    219 #undef	SCSI_DELAY
    220 #define SCSI_DELAY 2
    221 #endif	/* SCSI_DELAY */
    222 
    223 	if (SCSI_DELAY > 0) {
    224 		printf("%s: waiting %d seconds for devices to settle...\n",
    225 		    self->dv_xname, SCSI_DELAY);
    226 		/* ...an identifier we know no one will use... */
    227 		(void) tsleep(scsibus_config_interrupts, PRIBIO,
    228 		    "scsidly", SCSI_DELAY * hz);
    229 	}
    230 
    231 	scsi_probe_bus(self->dv_unit, -1, -1);
    232 }
    233 
    234 int
    235 scsibussubmatch(parent, cf, aux)
    236 	struct device *parent;
    237 	struct cfdata *cf;
    238 	void *aux;
    239 {
    240 	struct scsipibus_attach_args *sa = aux;
    241 	struct scsipi_link *sc_link = sa->sa_sc_link;
    242 
    243 	if (cf->cf_loc[SCSIBUSCF_TARGET] != SCSIBUSCF_TARGET_DEFAULT &&
    244 	    cf->cf_loc[SCSIBUSCF_TARGET] != sc_link->scsipi_scsi.target)
    245 		return (0);
    246 	if (cf->cf_loc[SCSIBUSCF_LUN] != SCSIBUSCF_LUN_DEFAULT &&
    247 	    cf->cf_loc[SCSIBUSCF_LUN] != sc_link->scsipi_scsi.lun)
    248 		return (0);
    249 	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
    250 }
    251 
    252 int
    253 scsibusactivate(self, act)
    254 	struct device *self;
    255 	enum devact act;
    256 {
    257 	struct scsibus_softc *sc = (struct scsibus_softc *) self;
    258 	struct scsipi_link *sc_link;
    259 	int target, lun, error = 0, s;
    260 
    261 	s = splbio();
    262 	switch (act) {
    263 	case DVACT_ACTIVATE:
    264 		error = EOPNOTSUPP;
    265 		break;
    266 
    267 	case DVACT_DEACTIVATE:
    268 		for (target = 0; target <= sc->sc_maxtarget; target++) {
    269 			if (target ==
    270 			    sc->adapter_link->scsipi_scsi.adapter_target)
    271 				continue;
    272 			for (lun = 0; lun <= sc->sc_maxlun; lun++) {
    273 				sc_link = sc->sc_link[target][lun];
    274 				if (sc_link == NULL)
    275 					continue;
    276 				error =
    277 				    config_deactivate(sc_link->device_softc);
    278 				if (error)
    279 					goto out;
    280 			}
    281 		}
    282 		break;
    283 	}
    284  out:
    285 	splx(s);
    286 	return (error);
    287 }
    288 
    289 int
    290 scsibusdetach(self, flags)
    291 	struct device *self;
    292 	int flags;
    293 {
    294 	struct scsibus_softc *sc = (struct scsibus_softc *) self;
    295 	struct scsipi_link *sc_link;
    296 	int target, lun, error;
    297 
    298 	for (target = 0; target <= sc->sc_maxtarget; target++) {
    299 		if (target == sc->adapter_link->scsipi_scsi.adapter_target)
    300 			continue;
    301 		for (lun = 0; lun <= sc->sc_maxlun; lun++) {
    302 			sc_link = sc->sc_link[target][lun];
    303 			if (sc_link == NULL)
    304 				continue;
    305 			error = config_detach(sc_link->device_softc, flags);
    306 			if (error)
    307 				return (error);
    308 			free(sc_link, M_DEVBUF);
    309 			sc->sc_link[target][lun] = NULL;
    310 		}
    311 	}
    312 	return (0);
    313 }
    314 
    315 /*
    316  * Probe the requested scsi bus. It must be already set up.
    317  * -1 requests all set up scsi busses.
    318  * target and lun optionally narrow the search if not -1
    319  */
    320 int
    321 scsi_probe_busses(bus, target, lun)
    322 	int bus, target, lun;
    323 {
    324 
    325 	if (bus == -1) {
    326 		for (bus = 0; bus < scsibus_cd.cd_ndevs; bus++)
    327 			if (scsibus_cd.cd_devs[bus])
    328 				scsi_probe_bus(bus, target, lun);
    329 		return (0);
    330 	} else
    331 		return (scsi_probe_bus(bus, target, lun));
    332 }
    333 
    334 /*
    335  * Probe the requested scsi bus. It must be already set up.
    336  * target and lun optionally narrow the search if not -1
    337  */
    338 int
    339 scsi_probe_bus(bus, target, lun)
    340 	int bus, target, lun;
    341 {
    342 	struct scsibus_softc *scsi;
    343 	int maxtarget, mintarget, maxlun, minlun;
    344 	u_int8_t scsi_addr;
    345 	int error;
    346 
    347 	if (bus < 0 || bus >= scsibus_cd.cd_ndevs)
    348 		return (ENXIO);
    349 	scsi = scsibus_cd.cd_devs[bus];
    350 	if (scsi == NULL)
    351 		return (ENXIO);
    352 
    353 	scsi_addr = scsi->adapter_link->scsipi_scsi.adapter_target;
    354 
    355 	if (target == -1) {
    356 		maxtarget = scsi->sc_maxtarget;
    357 		mintarget = 0;
    358 	} else {
    359 		if (target < 0 || target > scsi->sc_maxtarget)
    360 			return (EINVAL);
    361 		maxtarget = mintarget = target;
    362 	}
    363 
    364 	if (lun == -1) {
    365 		maxlun = scsi->sc_maxlun;
    366 		minlun = 0;
    367 	} else {
    368 		if (lun < 0 || lun > scsi->sc_maxlun)
    369 			return (EINVAL);
    370 		maxlun = minlun = lun;
    371 	}
    372 
    373 	if ((error = scsipi_adapter_addref(scsi->adapter_link)) != 0)
    374 		return (error);
    375 	for (target = mintarget; target <= maxtarget; target++) {
    376 		if (target == scsi_addr)
    377 			continue;
    378 		for (lun = minlun; lun <= maxlun; lun++) {
    379 			/*
    380 			 * See if there's a device present, and configure it.
    381 			 */
    382 			if (scsi_probedev(scsi, target, lun) == 0) {
    383 				break;
    384 			}
    385 			/* otherwise something says we should look further */
    386 		}
    387 	}
    388 	scsipi_adapter_delref(scsi->adapter_link);
    389 	return (0);
    390 }
    391 
    392 /*
    393  * Print out autoconfiguration information for a subdevice.
    394  *
    395  * This is a slight abuse of 'standard' autoconfiguration semantics,
    396  * because 'print' functions don't normally print the colon and
    397  * device information.  However, in this case that's better than
    398  * either printing redundant information before the attach message,
    399  * or having the device driver call a special function to print out
    400  * the standard device information.
    401  */
    402 int
    403 scsibusprint(aux, pnp)
    404 	void *aux;
    405 	const char *pnp;
    406 {
    407 	struct scsipibus_attach_args *sa = aux;
    408 	struct scsipi_inquiry_pattern *inqbuf;
    409 	u_int8_t type;
    410 	char *dtype, *qtype;
    411 	char vendor[33], product[65], revision[17];
    412 	int target, lun;
    413 
    414 	if (pnp != NULL)
    415 		printf("%s", pnp);
    416 
    417 	inqbuf = &sa->sa_inqbuf;
    418 
    419 	target = sa->sa_sc_link->scsipi_scsi.target;
    420 	lun = sa->sa_sc_link->scsipi_scsi.lun;
    421 
    422 	type = inqbuf->type & SID_TYPE;
    423 
    424 	/*
    425 	 * Figure out basic device type and qualifier.
    426 	 */
    427 	dtype = 0;
    428 	switch (inqbuf->type & SID_QUAL) {
    429 	case SID_QUAL_LU_OK:
    430 		qtype = "";
    431 		break;
    432 
    433 	case SID_QUAL_LU_OFFLINE:
    434 		qtype = " offline";
    435 		break;
    436 
    437 	case SID_QUAL_RSVD:
    438 	case SID_QUAL_BAD_LU:
    439 		panic("scsibusprint: impossible qualifier");
    440 
    441 	default:
    442 		qtype = "";
    443 		dtype = "vendor-unique";
    444 		break;
    445 	}
    446 	if (dtype == 0)
    447 		dtype = scsipi_dtype(type);
    448 
    449 	scsipi_strvis(vendor, 33, inqbuf->vendor, 8);
    450 	scsipi_strvis(product, 65, inqbuf->product, 16);
    451 	scsipi_strvis(revision, 17, inqbuf->revision, 4);
    452 
    453 	printf(" targ %d lun %d: <%s, %s, %s> SCSI%d %d/%s %s%s",
    454 	    target, lun, vendor, product, revision,
    455 	    sa->scsipi_info.scsi_version & SID_ANSII, type, dtype,
    456 	    inqbuf->removable ? "removable" : "fixed", qtype);
    457 
    458 	return (UNCONF);
    459 }
    460 
    461 struct scsi_quirk_inquiry_pattern scsi_quirk_patterns[] = {
    462 	{{T_CDROM, T_REMOV,
    463 	 "CHINON  ", "CD-ROM CDS-431  ", ""},     SDEV_NOLUNS},
    464 	{{T_CDROM, T_REMOV,
    465 	 "Chinon  ", "CD-ROM CDS-525  ", ""},     SDEV_NOLUNS},
    466 	{{T_CDROM, T_REMOV,
    467 	 "CHINON  ", "CD-ROM CDS-535  ", ""},     SDEV_NOLUNS},
    468 	{{T_CDROM, T_REMOV,
    469 	 "DEC     ", "RRD42   (C) DEC ", ""},     SDEV_NOLUNS},
    470 	{{T_CDROM, T_REMOV,
    471 	 "DENON   ", "DRD-25X         ", "V"},    SDEV_NOLUNS},
    472 	{{T_CDROM, T_REMOV,
    473 	 "HP      ", "C4324/C4325     ", ""},     SDEV_NOLUNS},
    474 	{{T_CDROM, T_REMOV,
    475 	 "IMS     ", "CDD521/10       ", "2.06"}, SDEV_NOLUNS},
    476 	{{T_CDROM, T_REMOV,
    477 	 "MATSHITA", "CD-ROM CR-5XX   ", "1.0b"}, SDEV_NOLUNS},
    478 	{{T_CDROM, T_REMOV,
    479 	 "MEDAVIS ", "RENO CD-ROMX2A  ", ""},     SDEV_NOLUNS},
    480 	{{T_CDROM, T_REMOV,
    481 	 "MEDIAVIS", "CDR-H93MV       ", "1.3"},  SDEV_NOLUNS},
    482 	{{T_CDROM, T_REMOV,
    483 	 "NEC     ", "CD-ROM DRIVE:55 ", ""},     SDEV_NOLUNS},
    484 	{{T_CDROM, T_REMOV,
    485 	 "NEC     ", "CD-ROM DRIVE:83 ", ""},     SDEV_NOLUNS},
    486 	{{T_CDROM, T_REMOV,
    487 	 "NEC     ", "CD-ROM DRIVE:84 ", ""},     SDEV_NOLUNS},
    488 	{{T_CDROM, T_REMOV,
    489 	 "NEC     ", "CD-ROM DRIVE:841", ""},     SDEV_NOLUNS},
    490 	{{T_CDROM, T_REMOV,
    491 	 "PIONEER ", "CD-ROM DR-124X  ", "1.01"}, SDEV_NOLUNS},
    492 	{{T_CDROM, T_REMOV,
    493 	 "SONY    ", "CD-ROM CDU-541  ", ""},     SDEV_NOLUNS},
    494 	{{T_CDROM, T_REMOV,
    495 	 "SONY    ", "CD-ROM CDU-55S  ", ""},     SDEV_NOLUNS},
    496 	{{T_CDROM, T_REMOV,
    497 	 "SONY    ", "CD-ROM CDU-8003A", ""},     SDEV_NOLUNS},
    498 	{{T_CDROM, T_REMOV,
    499 	 "SONY    ", "CD-ROM CDU-8012 ", ""},     SDEV_NOLUNS},
    500 	{{T_CDROM, T_REMOV,
    501 	 "TEAC    ", "CD-ROM          ", "1.06"}, SDEV_NOLUNS},
    502 	{{T_CDROM, T_REMOV,
    503 	 "TEAC    ", "CD-ROM CD-56S   ", "1.0B"}, SDEV_NOLUNS},
    504 	{{T_CDROM, T_REMOV,
    505 	 "TEXEL   ", "CD-ROM          ", "1.06"}, SDEV_NOLUNS},
    506 	{{T_CDROM, T_REMOV,
    507 	 "TEXEL   ", "CD-ROM DM-XX24 K", "1.09"}, SDEV_NOLUNS},
    508 	{{T_CDROM, T_REMOV,
    509 	 "TEXEL   ", "CD-ROM DM-XX24 K", "1.10"}, SDEV_NOLUNS},
    510 	{{T_CDROM, T_REMOV,
    511 	 "TOSHIBA ", "XM-4101TASUNSLCD", "1755"}, SDEV_NOLUNS},
    512 	{{T_CDROM, T_REMOV,
    513 	 "ShinaKen", "CD-ROM DM-3x1S", "1.04"}, SDEV_NOLUNS},
    514 	{{T_CDROM, T_REMOV,
    515 	 "JVC     ", "R2626",            ""},     SDEV_NOLUNS},
    516 	{{T_DIRECT, T_FIXED,
    517 	 "MICROP  ", "1588-15MBSUN0669", ""},     SDEV_AUTOSAVE},
    518 	{{T_DIRECT, T_FIXED,
    519 	 "MICROP  ", "2217-15MQ1091501", ""},     SDEV_NOSYNCCACHE},
    520 	{{T_OPTICAL, T_REMOV,
    521 	 "EPSON   ", "OMD-5010        ", "3.08"}, SDEV_NOLUNS},
    522 
    523 	{{T_DIRECT, T_FIXED,
    524 	"TOSHIBA ", "CD-ROM XM-3401TA", "0283"}, ADEV_CDROM|SDEV_NOLUNS},
    525 	{{T_DIRECT, T_FIXED,
    526 	 "ADAPTEC ", "AEC-4412BD",       "1.2A"}, SDEV_NOMODESENSE},
    527 	{{T_DIRECT, T_FIXED,
    528 	 "DEC     ", "RZ55     (C) DEC", ""},     SDEV_AUTOSAVE},
    529 	{{T_DIRECT, T_FIXED,
    530 	 "EMULEX  ", "MD21/S2     ESDI", "A00"},  SDEV_FORCELUNS|SDEV_AUTOSAVE},
    531 	/* Gives non-media hardware failure in response to start-unit command */
    532 	{{T_DIRECT, T_FIXED,
    533 	 "HITACHI", "DK515C",		"CP16"},  SDEV_NOSTARTUNIT},
    534 	{{T_DIRECT, T_FIXED,
    535 	 "HITACHI", "DK515C",		"CP15"},  SDEV_NOSTARTUNIT},
    536 	{{T_DIRECT, T_FIXED,
    537 	 "HP      ", "C372",             ""},     SDEV_NOTAG},
    538 	{{T_DIRECT, T_FIXED,
    539 	 "IBMRAID ", "0662S",		 ""},     SDEV_AUTOSAVE},
    540 	{{T_DIRECT, T_FIXED,
    541 	 "IBM     ", "0663H",		 ""},     SDEV_AUTOSAVE},
    542 	{{T_DIRECT, T_FIXED,
    543 	 "IBM",	     "0664",		 ""},     SDEV_AUTOSAVE},
    544 	{{T_DIRECT, T_FIXED,
    545 	 "IBM     ", "H3171-S2",	 ""},	  SDEV_NOLUNS|SDEV_AUTOSAVE},
    546 	{{T_DIRECT, T_FIXED,
    547 	 "IBM     ", "KZ-C",		 ""},	  SDEV_AUTOSAVE},
    548 	/* Broken IBM disk */
    549 	{{T_DIRECT, T_FIXED,
    550 	 ""	   , "DFRSS2F",		 ""},	  SDEV_AUTOSAVE},
    551 	{{T_DIRECT, T_REMOV,
    552 	 "MPL     ", "MC-DISK-        ", ""},     SDEV_NOLUNS},
    553 	{{T_DIRECT, T_FIXED,
    554 	 "MAXTOR  ", "XT-3280         ", ""},     SDEV_NOLUNS},
    555 	{{T_DIRECT, T_FIXED,
    556 	 "MAXTOR  ", "XT-4380S        ", ""},     SDEV_NOLUNS},
    557 	{{T_DIRECT, T_FIXED,
    558 	 "MAXTOR  ", "MXT-1240S       ", ""},     SDEV_NOLUNS},
    559 	{{T_DIRECT, T_FIXED,
    560 	 "MAXTOR  ", "XT-4170S        ", ""},     SDEV_NOLUNS},
    561 	{{T_DIRECT, T_FIXED,
    562 	 "MAXTOR  ", "XT-8760S",         ""},     SDEV_NOLUNS},
    563 	{{T_DIRECT, T_FIXED,
    564 	 "MAXTOR  ", "LXT-213S        ", ""},     SDEV_NOLUNS},
    565 	{{T_DIRECT, T_FIXED,
    566 	 "MAXTOR  ", "LXT-213S SUN0207", ""},     SDEV_NOLUNS},
    567 	{{T_DIRECT, T_FIXED,
    568 	 "MAXTOR  ", "LXT-200S        ", ""},     SDEV_NOLUNS},
    569 	{{T_DIRECT, T_FIXED,
    570 	 "MEGADRV ", "EV1000",           ""},     SDEV_NOMODESENSE},
    571 	{{T_DIRECT, T_FIXED,
    572 	 "MST     ", "SnapLink        ", ""},     SDEV_NOLUNS},
    573 	{{T_DIRECT, T_FIXED,
    574 	 "NEC     ", "D3847           ", "0307"}, SDEV_NOLUNS},
    575 	{{T_DIRECT, T_FIXED,
    576 	 "QUANTUM ", "ELS85S          ", ""},     SDEV_AUTOSAVE},
    577 	{{T_DIRECT, T_FIXED,
    578 	 "QUANTUM ", "LPS525S         ", ""},     SDEV_NOLUNS},
    579 	{{T_DIRECT, T_FIXED,
    580 	 "QUANTUM ", "P105S 910-10-94x", ""},     SDEV_NOLUNS},
    581 	{{T_DIRECT, T_FIXED,
    582 	 "QUANTUM ", "PD1225S         ", ""},     SDEV_NOLUNS},
    583 	{{T_DIRECT, T_FIXED,
    584 	 "QUANTUM ", "PD210S   SUN0207", ""},     SDEV_NOLUNS},
    585 	{{T_DIRECT, T_FIXED,
    586 	 "RODIME  ", "RO3000S         ", ""},     SDEV_NOLUNS},
    587 	{{T_DIRECT, T_FIXED,
    588 	 "SEAGATE ", "ST125N          ", ""},     SDEV_NOLUNS},
    589 	{{T_DIRECT, T_FIXED,
    590 	 "SEAGATE ", "ST157N          ", ""},     SDEV_NOLUNS},
    591 	{{T_DIRECT, T_FIXED,
    592 	 "SEAGATE ", "ST296           ", ""},     SDEV_NOLUNS},
    593 	{{T_DIRECT, T_FIXED,
    594 	 "SEAGATE ", "ST296N          ", ""},     SDEV_NOLUNS},
    595 	{{T_DIRECT, T_FIXED,
    596 	 "SEAGATE ", "ST19171",          ""},     SDEV_NOMODESENSE},
    597 	{{T_DIRECT, T_FIXED,
    598 	 "SEAGATE ", "ST34501FC       ", ""},     SDEV_NOMODESENSE},
    599 	{{T_DIRECT, T_FIXED,
    600 	 "TOSHIBA ", "MK538FB         ", "6027"}, SDEV_NOLUNS},
    601 	{{T_DIRECT, T_REMOV,
    602 	 "iomega", "jaz 1GB", 		 ""},	  SDEV_NOMODESENSE},
    603 	{{T_DIRECT, T_REMOV,
    604 	 "IOMEGA", "ZIP 100",		 ""},	  SDEV_NOMODESENSE},
    605 	{{T_DIRECT, T_REMOV,
    606 	 "IOMEGA", "ZIP 100",		 "J.03"}, SDEV_NOMODESENSE|SDEV_NOLUNS},
    607 	/* Letting the motor run kills floppy drives and disks quite fast. */
    608 	{{T_DIRECT, T_REMOV,
    609 	 "TEAC", "FC-1",		 ""},	  SDEV_NOSTARTUNIT},
    610 
    611 	/* XXX: QIC-36 tape behind Emulex adapter.  Very broken. */
    612 	{{T_SEQUENTIAL, T_REMOV,
    613 	 "        ", "                ", "    "}, SDEV_NOLUNS},
    614 	{{T_SEQUENTIAL, T_REMOV,
    615 	 "CALIPER ", "CP150           ", ""},     SDEV_NOLUNS},
    616 	{{T_SEQUENTIAL, T_REMOV,
    617 	 "EXABYTE ", "EXB-8200        ", ""},     SDEV_NOLUNS},
    618 	{{T_SEQUENTIAL, T_REMOV,
    619 	 "SONY    ", "GY-10C          ", ""},     SDEV_NOLUNS},
    620 	{{T_SEQUENTIAL, T_REMOV,
    621 	 "SONY    ", "SDT-2000        ", "2.09"}, SDEV_NOLUNS},
    622 	{{T_SEQUENTIAL, T_REMOV,
    623 	 "SONY    ", "SDT-5000        ", "3."},   SDEV_NOSYNC|SDEV_NOWIDE},
    624 	{{T_SEQUENTIAL, T_REMOV,
    625 	 "SONY    ", "SDT-5200        ", "3."},   SDEV_NOLUNS},
    626 	{{T_SEQUENTIAL, T_REMOV,
    627 	 "TANDBERG", " TDC 3600       ", ""},     SDEV_NOLUNS},
    628 	/* Following entry reported as a Tandberg 3600; ref. PR1933 */
    629 	{{T_SEQUENTIAL, T_REMOV,
    630 	 "ARCHIVE ", "VIPER 150  21247", ""},     SDEV_NOLUNS},
    631 	/* Following entry for a Cipher ST150S; ref. PR4171 */
    632 	{{T_SEQUENTIAL, T_REMOV,
    633 	 "ARCHIVE ", "VIPER 1500 21247", "2.2G"}, SDEV_NOLUNS},
    634 	{{T_SEQUENTIAL, T_REMOV,
    635 	 "ARCHIVE ", "Python 28454-XXX", ""},     SDEV_NOLUNS},
    636 	{{T_SEQUENTIAL, T_REMOV,
    637 	 "WANGTEK ", "5099ES SCSI",      ""},     SDEV_NOLUNS},
    638 	{{T_SEQUENTIAL, T_REMOV,
    639 	 "WANGTEK ", "5150ES SCSI",      ""},     SDEV_NOLUNS},
    640 	{{T_SEQUENTIAL, T_REMOV,
    641 	 "WANGTEK ", "SCSI-36",		 ""},     SDEV_NOLUNS},
    642 	{{T_SEQUENTIAL, T_REMOV,
    643 	 "WangDAT ", "Model 1300      ", "02.4"}, SDEV_NOSYNC|SDEV_NOWIDE},
    644 	{{T_SEQUENTIAL, T_REMOV,
    645 	 "WangDAT ", "Model 2600      ", "01.7"}, SDEV_NOSYNC|SDEV_NOWIDE},
    646 	{{T_SEQUENTIAL, T_REMOV,
    647 	 "WangDAT ", "Model 3200      ", "02.2"}, SDEV_NOSYNC|SDEV_NOWIDE},
    648 
    649 	{{T_SCANNER, T_FIXED,
    650 	 "RICOH   ", "IS60            ", "1R08"}, SDEV_NOLUNS},
    651 	{{T_SCANNER, T_FIXED,
    652 	 "UMAX    ", "Astra 1200S     ", "V2.9"}, SDEV_NOLUNS},
    653 	{{T_SCANNER, T_FIXED,
    654 	 "UMAX    ", "Astra 1220S     ", ""}, SDEV_NOLUNS},
    655 	{{T_SCANNER, T_FIXED,
    656 	 "UMAX    ", "UMAX S-6E       ", "V2.0"}, SDEV_NOLUNS},
    657 	{{T_SCANNER, T_FIXED,
    658 	 "UMAX    ", "UMAX S-12       ", "V2.1"}, SDEV_NOLUNS},
    659 
    660 	{{T_PROCESSOR, T_FIXED,
    661 	 "LITRONIC", "PCMCIA          ", ""},     SDEV_NOLUNS},
    662 
    663 	{{T_CHANGER, T_REMOV,
    664 	 "SONY    ", "CDL1100         ", ""},     SDEV_NOLUNS},
    665 
    666 	{{T_ENCLOSURE, T_FIXED,
    667 	 "SUN     ", "SENA            ", "1.07"}, SDEV_NOLUNS},
    668 };
    669 
    670 /*
    671  * given a target and lun, ask the device what
    672  * it is, and find the correct driver table
    673  * entry.
    674  */
    675 int
    676 scsi_probedev(scsi, target, lun)
    677 	struct scsibus_softc *scsi;
    678 	int target, lun;
    679 {
    680 	struct scsipi_link *sc_link;
    681 	static struct scsipi_inquiry_data inqbuf;
    682 	struct scsi_quirk_inquiry_pattern *finger;
    683 	int checkdtype, priority, docontinue;
    684 	struct scsipibus_attach_args sa;
    685 	struct cfdata *cf;
    686 
    687 	/*
    688 	 * Assume no more luns to search after this one.
    689 	 * If we successfully get Inquiry data and after
    690 	 * merging quirks we find we can probe for more
    691 	 * luns, we will.
    692 	 */
    693 	docontinue = 0;
    694 
    695 	/* Skip this slot if it is already attached. */
    696 	if (scsi->sc_link[target][lun] != NULL)
    697 		return (docontinue);
    698 
    699 	sc_link = malloc(sizeof(*sc_link), M_DEVBUF, M_NOWAIT);
    700 	*sc_link = *scsi->adapter_link;
    701 	sc_link->active = 0;
    702 	sc_link->scsipi_scsi.target = target;
    703 	sc_link->scsipi_scsi.lun = lun;
    704 	sc_link->device = &probe_switch;
    705 	TAILQ_INIT(&sc_link->pending_xfers);
    706 
    707 	/*
    708 	 * Ask the device what it is
    709 	 */
    710 #if defined(SCSIDEBUG) && DEBUGTYPE == BUS_SCSI
    711 	if (target == DEBUGTARGET && lun == DEBUGLUN)
    712 		sc_link->flags |= DEBUGLEVEL;
    713 #endif /* SCSIDEBUG */
    714 
    715 	(void) scsipi_test_unit_ready(sc_link,
    716 	    XS_CTL_DISCOVERY | XS_CTL_IGNORE_ILLEGAL_REQUEST |
    717 	    XS_CTL_IGNORE_NOT_READY | XS_CTL_IGNORE_MEDIA_CHANGE);
    718 
    719 #ifdef SCSI_2_DEF
    720 	/* some devices need to be told to go to SCSI2 */
    721 	/* However some just explode if you tell them this.. leave it out */
    722 	scsi_change_def(sc_link, XS_CTL_DISCOVERY | XS_CTL_SILENT);
    723 #endif /* SCSI_2_DEF */
    724 
    725 	/* Now go ask the device all about itself. */
    726 	bzero(&inqbuf, sizeof(inqbuf));
    727 	if (scsipi_inquire(sc_link, &inqbuf, XS_CTL_DISCOVERY) != 0)
    728 		goto bad;
    729 
    730 	{
    731 		int len = inqbuf.additional_length;
    732 		while (len < 3)
    733 			inqbuf.unused[len++] = '\0';
    734 		while (len < 3 + 28)
    735 			inqbuf.unused[len++] = ' ';
    736 	}
    737 
    738 	sa.sa_sc_link = sc_link;
    739 	sa.sa_inqbuf.type = inqbuf.device;
    740 	sa.sa_inqbuf.removable = inqbuf.dev_qual2 & SID_REMOVABLE ?
    741 	    T_REMOV : T_FIXED;
    742 	sa.sa_inqbuf.vendor = inqbuf.vendor;
    743 	sa.sa_inqbuf.product = inqbuf.product;
    744 	sa.sa_inqbuf.revision = inqbuf.revision;
    745 	sa.scsipi_info.scsi_version = inqbuf.version;
    746 
    747 	finger = (struct scsi_quirk_inquiry_pattern *)scsipi_inqmatch(
    748 	    &sa.sa_inqbuf, (caddr_t)scsi_quirk_patterns,
    749 	    sizeof(scsi_quirk_patterns)/sizeof(scsi_quirk_patterns[0]),
    750 	    sizeof(scsi_quirk_patterns[0]), &priority);
    751 
    752 	/*
    753 	 * Based upon the inquiry flags we got back, and if we're
    754 	 * at SCSI-2 or better, set some limiting quirks.
    755 	 */
    756 	if ((inqbuf.version & SID_ANSII) >= 2) {
    757 		if ((inqbuf.flags & SID_CmdQue) == 0)
    758 			sc_link->quirks |= SDEV_NOTAG;
    759 		if ((inqbuf.flags & SID_Sync) == 0)
    760 			sc_link->quirks |= SDEV_NOSYNC;
    761 		if ((inqbuf.flags & SID_WBus16) == 0)
    762 			sc_link->quirks |= SDEV_NOWIDE;
    763 	}
    764 	/*
    765 	 * Now apply any quirks from the table.
    766 	 */
    767 	if (priority != 0)
    768 		sc_link->quirks |= finger->quirks;
    769 	if ((inqbuf.version & SID_ANSII) == 0 &&
    770 	    (sc_link->quirks & SDEV_FORCELUNS) == 0)
    771 		sc_link->quirks |= SDEV_NOLUNS;
    772 	sc_link->scsipi_scsi.scsi_version = inqbuf.version;
    773 
    774 	if ((sc_link->quirks & SDEV_NOLUNS) == 0)
    775 		docontinue = 1;
    776 
    777 	/*
    778 	 * note what BASIC type of device it is
    779 	 */
    780 	if ((inqbuf.dev_qual2 & SID_REMOVABLE) != 0)
    781 		sc_link->flags |= SDEV_REMOVABLE;
    782 
    783 	/*
    784 	 * Any device qualifier that has the top bit set (qualifier&4 != 0)
    785 	 * is vendor specific and won't match in this switch.
    786 	 * All we do here is throw out bad/negative responses.
    787 	 */
    788 	checkdtype = 0;
    789 	switch (inqbuf.device & SID_QUAL) {
    790 	case SID_QUAL_LU_OK:
    791 	case SID_QUAL_LU_OFFLINE:
    792 		checkdtype = 1;
    793 		break;
    794 
    795 	case SID_QUAL_RSVD:
    796 	case SID_QUAL_BAD_LU:
    797 		goto bad;
    798 
    799 	default:
    800 		break;
    801 	}
    802 	if (checkdtype)
    803 		switch (inqbuf.device & SID_TYPE) {
    804 		case T_DIRECT:
    805 		case T_SEQUENTIAL:
    806 		case T_PRINTER:
    807 		case T_PROCESSOR:
    808 		case T_WORM:
    809 		case T_CDROM:
    810 		case T_SCANNER:
    811 		case T_OPTICAL:
    812 		case T_CHANGER:
    813 		case T_COMM:
    814 		case T_IT8_1:
    815 		case T_IT8_2:
    816 		case T_STORARRAY:
    817 		case T_ENCLOSURE:
    818 		default:
    819 			break;
    820 		case T_NODEVICE:
    821 			goto bad;
    822 		}
    823 
    824 	if ((cf = config_search(scsibussubmatch, (struct device *)scsi,
    825 	    &sa)) != NULL) {
    826 		scsi->sc_link[target][lun] = sc_link;
    827 		config_attach((struct device *)scsi, cf, &sa, scsibusprint);
    828 	} else {
    829 		scsibusprint(&sa, scsi->sc_dev.dv_xname);
    830 		printf(" not configured\n");
    831 		goto bad;
    832 	}
    833 
    834 	return (docontinue);
    835 
    836 bad:
    837 	free(sc_link, M_DEVBUF);
    838 	return (docontinue);
    839 }
    840 
    841 /****** Entry points for user control of the SCSI bus. ******/
    842 
    843 int
    844 scsibusopen(dev, flag, fmt, p)
    845 	dev_t dev;
    846 	int flag, fmt;
    847 	struct proc *p;
    848 {
    849 	struct scsibus_softc *sc;
    850 	int error, unit = minor(dev);
    851 
    852 	if (unit >= scsibus_cd.cd_ndevs ||
    853 	    (sc = scsibus_cd.cd_devs[unit]) == NULL)
    854 		return (ENXIO);
    855 
    856 	if (sc->sc_flags & SCSIBUSF_OPEN)
    857 		return (EBUSY);
    858 
    859 	if ((error = scsipi_adapter_addref(sc->adapter_link)) != 0)
    860 		return (error);
    861 
    862 	sc->sc_flags |= SCSIBUSF_OPEN;
    863 
    864 	return (0);
    865 }
    866 
    867 int
    868 scsibusclose(dev, flag, fmt, p)
    869 	dev_t dev;
    870 	int flag, fmt;
    871 	struct proc *p;
    872 {
    873 	struct scsibus_softc *sc = scsibus_cd.cd_devs[minor(dev)];
    874 
    875 	scsipi_adapter_delref(sc->adapter_link);
    876 
    877 	sc->sc_flags &= ~SCSIBUSF_OPEN;
    878 
    879 	return (0);
    880 }
    881 
    882 int
    883 scsibusioctl(dev, cmd, addr, flag, p)
    884 	dev_t dev;
    885 	u_long cmd;
    886 	caddr_t addr;
    887 	int flag;
    888 	struct proc *p;
    889 {
    890 	struct scsibus_softc *sc = scsibus_cd.cd_devs[minor(dev)];
    891 	struct scsipi_link *sc_link = sc->adapter_link;
    892 	int error;
    893 
    894 	/*
    895 	 * Enforce write permission for ioctls that change the
    896 	 * state of the bus.  Host adapter specific ioctls must
    897 	 * be checked by the adapter driver.
    898 	 */
    899 	switch (cmd) {
    900 	case SCBUSIOSCAN:
    901 	case SCBUSIORESET:
    902 		if ((flag & FWRITE) == 0)
    903 			return (EBADF);
    904 	}
    905 
    906 	switch (cmd) {
    907 	case SCBUSIOSCAN:
    908 	    {
    909 		struct scbusioscan_args *a =
    910 		    (struct scbusioscan_args *)addr;
    911 
    912 		/* XXX Change interface to this function. */
    913 		error = scsi_probe_busses(minor(dev), a->sa_target,
    914 		    a->sa_lun);
    915 		break;
    916 	    }
    917 
    918 	case SCBUSIORESET:
    919 		/* FALLTHROUGH */
    920 	default:
    921 		if (sc_link->adapter->scsipi_ioctl == NULL)
    922 			error = ENOTTY;
    923 		else
    924 			error = (*sc_link->adapter->scsipi_ioctl)(sc_link,
    925 			    cmd, addr, flag, p);
    926 		break;
    927 	}
    928 
    929 	return (error);
    930 }
    931