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