Home | History | Annotate | Line # | Download | only in scsipi
scsiconf.c revision 1.156.2.2
      1 /*	$NetBSD: scsiconf.c,v 1.156.2.2 2001/06/21 20:05:52 nathanw Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998, 1999 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/lwp.h>
     62 #include <sys/proc.h>
     63 #include <sys/kthread.h>
     64 #include <sys/malloc.h>
     65 #include <sys/device.h>
     66 #include <sys/conf.h>
     67 #include <sys/fcntl.h>
     68 #include <sys/scsiio.h>
     69 
     70 #include <dev/scsipi/scsi_all.h>
     71 #include <dev/scsipi/scsipi_all.h>
     72 #include <dev/scsipi/scsiconf.h>
     73 
     74 #include "locators.h"
     75 
     76 const struct scsipi_periphsw scsi_probe_dev = {
     77 	NULL,
     78 	NULL,
     79 	NULL,
     80 	NULL,
     81 };
     82 
     83 int	scsi_probe_device __P((struct scsibus_softc *, int, int));
     84 
     85 int	scsibusmatch __P((struct device *, struct cfdata *, void *));
     86 void	scsibusattach __P((struct device *, struct device *, void *));
     87 int	scsibusactivate __P((struct device *, enum devact));
     88 int	scsibusdetach __P((struct device *, int flags));
     89 
     90 int	scsibussubmatch __P((struct device *, struct cfdata *, void *));
     91 
     92 struct cfattach scsibus_ca = {
     93 	sizeof(struct scsibus_softc), scsibusmatch, scsibusattach,
     94 	    scsibusdetach, scsibusactivate,
     95 };
     96 
     97 extern struct cfdriver scsibus_cd;
     98 
     99 int	scsibusprint __P((void *, const char *));
    100 void	scsibus_config_interrupts __P((struct device *));
    101 
    102 cdev_decl(scsibus);
    103 
    104 const struct scsipi_bustype scsi_bustype = {
    105 	SCSIPI_BUSTYPE_SCSI,
    106 	scsi_scsipi_cmd,
    107 	scsipi_interpret_sense,
    108 	scsi_print_addr,
    109 	scsi_kill_pending,
    110 };
    111 
    112 int
    113 scsiprint(aux, pnp)
    114 	void *aux;
    115 	const char *pnp;
    116 {
    117 	struct scsipi_channel *chan = aux;
    118 	struct scsipi_adapter *adapt = chan->chan_adapter;
    119 
    120 	/* only "scsibus"es can attach to "scsi"s; easy. */
    121 	if (pnp)
    122 		printf("scsibus at %s", pnp);
    123 
    124 	/* don't print channel if the controller says there can be only one. */
    125 	if (adapt->adapt_nchannels != 1)
    126 		printf(" channel %d", chan->chan_channel);
    127 
    128 	return (UNCONF);
    129 }
    130 
    131 int
    132 scsibusmatch(parent, cf, aux)
    133 	struct device *parent;
    134 	struct cfdata *cf;
    135 	void *aux;
    136 {
    137 	struct scsipi_channel *chan = aux;
    138 
    139 	if (chan->type != BUS_SCSI)
    140 		return 0;
    141 
    142 	if (cf->cf_loc[SCSICF_CHANNEL] != chan->chan_channel &&
    143 	    cf->cf_loc[SCSICF_CHANNEL] != SCSICF_CHANNEL_DEFAULT)
    144 		return (0);
    145 
    146 	return (1);
    147 }
    148 
    149 void
    150 scsibusattach(parent, self, aux)
    151 	struct device *parent, *self;
    152 	void *aux;
    153 {
    154 	struct scsibus_softc *sc = (void *) self;
    155 	struct scsipi_channel *chan = aux;
    156 
    157 	sc->sc_channel = chan;
    158 
    159 	/* Initialize the channel structure first */
    160 	if (scsipi_channel_init(chan)) {
    161 		printf(": failed to init channel\n");
    162 		return;
    163 	}
    164 
    165 	printf(": %d targets, %d luns per target\n",
    166 	    chan->chan_ntargets, chan->chan_nluns);
    167 
    168 
    169 	/*
    170 	 * Defer configuration of the children until interrupts
    171 	 * are enabled.
    172 	 */
    173 	config_interrupts(self, scsibus_config_interrupts);
    174 }
    175 
    176 void
    177 scsibus_config_interrupts(self)
    178 	struct device *self;
    179 {
    180 	struct scsibus_softc *sc = (void *) self;
    181 
    182 #ifndef SCSI_DELAY
    183 #define SCSI_DELAY 2
    184 #endif
    185 
    186 	if ((sc->sc_channel->chan_flags & SCSIPI_CHAN_NOSETTLE) == 0 &&
    187 	    SCSI_DELAY > 0) {
    188 		printf("%s: waiting %d seconds for devices to settle...\n",
    189 		    self->dv_xname, SCSI_DELAY);
    190 		/* ...an identifier we know no one will use... */
    191 		(void) tsleep(scsibus_config_interrupts, PRIBIO,
    192 		    "scsidly", SCSI_DELAY * hz);
    193 	}
    194 
    195 	scsi_probe_bus(sc, -1, -1);
    196 }
    197 
    198 int
    199 scsibussubmatch(parent, cf, aux)
    200 	struct device *parent;
    201 	struct cfdata *cf;
    202 	void *aux;
    203 {
    204 	struct scsipibus_attach_args *sa = aux;
    205 	struct scsipi_periph *periph = sa->sa_periph;
    206 
    207 	if (cf->cf_loc[SCSIBUSCF_TARGET] != SCSIBUSCF_TARGET_DEFAULT &&
    208 	    cf->cf_loc[SCSIBUSCF_TARGET] != periph->periph_target)
    209 		return (0);
    210 	if (cf->cf_loc[SCSIBUSCF_LUN] != SCSIBUSCF_LUN_DEFAULT &&
    211 	    cf->cf_loc[SCSIBUSCF_LUN] != periph->periph_lun)
    212 		return (0);
    213 	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
    214 }
    215 
    216 int
    217 scsibusactivate(self, act)
    218 	struct device *self;
    219 	enum devact act;
    220 {
    221 	struct scsibus_softc *sc = (void *) self;
    222 	struct scsipi_channel *chan = sc->sc_channel;
    223 	struct scsipi_periph *periph;
    224 	int target, lun, error = 0, s;
    225 
    226 	s = splbio();
    227 	switch (act) {
    228 	case DVACT_ACTIVATE:
    229 		error = EOPNOTSUPP;
    230 		break;
    231 
    232 	case DVACT_DEACTIVATE:
    233 		for (target = 0; target < chan->chan_ntargets;
    234 		     target++) {
    235 			if (target == chan->chan_id)
    236 				continue;
    237 			for (lun = 0; lun < chan->chan_nluns; lun++) {
    238 				periph = scsipi_lookup_periph(chan,
    239 				    target, lun);
    240 				if (periph == NULL)
    241 					continue;
    242 				error = config_deactivate(periph->periph_dev);
    243 				if (error)
    244 					goto out;
    245 			}
    246 		}
    247 		break;
    248 	}
    249  out:
    250 	splx(s);
    251 	return (error);
    252 }
    253 
    254 int
    255 scsibusdetach(self, flags)
    256 	struct device *self;
    257 	int flags;
    258 {
    259 	struct scsibus_softc *sc = (void *) self;
    260 	struct scsipi_channel *chan = sc->sc_channel;
    261 	struct scsipi_periph *periph;
    262 	int target, lun, error;
    263 
    264 	/*
    265 	 * Shut down the channel.
    266 	 */
    267 	scsipi_channel_shutdown(chan);
    268 
    269 	/*
    270 	 * Now detach all of the periphs.
    271 	 */
    272 	for (target = 0; target < chan->chan_ntargets; target++) {
    273 		if (target == chan->chan_id)
    274 			continue;
    275 		for (lun = 0; lun < chan->chan_nluns; lun++) {
    276 			periph = scsipi_lookup_periph(chan, target, lun);
    277 			if (periph == NULL)
    278 				continue;
    279 			error = config_detach(periph->periph_dev, flags);
    280 			if (error)
    281 				return (error);
    282 			scsipi_remove_periph(chan, periph);
    283 			free(periph, M_DEVBUF);
    284 		}
    285 	}
    286 	return (0);
    287 }
    288 
    289 /*
    290  * Probe the requested scsi bus. It must be already set up.
    291  * target and lun optionally narrow the search if not -1
    292  */
    293 int
    294 scsi_probe_bus(sc, target, lun)
    295 	struct scsibus_softc *sc;
    296 	int target, lun;
    297 {
    298 	struct scsipi_channel *chan = sc->sc_channel;
    299 	int maxtarget, mintarget, maxlun, minlun;
    300 	int error;
    301 
    302 	if (target == -1) {
    303 		maxtarget = chan->chan_ntargets - 1;
    304 		mintarget = 0;
    305 	} else {
    306 		if (target < 0 || target >= chan->chan_ntargets)
    307 			return (EINVAL);
    308 		maxtarget = mintarget = target;
    309 	}
    310 
    311 	if (lun == -1) {
    312 		maxlun = chan->chan_nluns - 1;
    313 		minlun = 0;
    314 	} else {
    315 		if (lun < 0 || lun >= chan->chan_nluns)
    316 			return (EINVAL);
    317 		maxlun = minlun = lun;
    318 	}
    319 
    320 	/*
    321 	 * Some HBAs provide an abstracted view of the bus; give them an
    322 	 * oppertunity to re-scan it before we do.
    323 	 */
    324 	if (chan->chan_adapter->adapt_ioctl != NULL)
    325 		(*chan->chan_adapter->adapt_ioctl)(chan, SCBUSIOLLSCAN, NULL,
    326 		    0, curproc->l_proc);
    327 
    328 	if ((error = scsipi_adapter_addref(chan->chan_adapter)) != 0)
    329 		return (error);
    330 	for (target = mintarget; target <= maxtarget; target++) {
    331 		if (target == chan->chan_id)
    332 			continue;
    333 		for (lun = minlun; lun <= maxlun; lun++) {
    334 			/*
    335 			 * See if there's a device present, and configure it.
    336 			 */
    337 			if (scsi_probe_device(sc, target, lun) == 0)
    338 				break;
    339 			/* otherwise something says we should look further */
    340 		}
    341 
    342 		/*
    343 		 * Now that we've discovered all of the LUNs on this
    344 		 * I_T Nexus, update the xfer mode for all of them
    345 		 * that we know about.
    346 		 */
    347 		scsipi_set_xfer_mode(chan, target, 1);
    348 	}
    349 	scsipi_adapter_delref(chan->chan_adapter);
    350 	return (0);
    351 }
    352 
    353 /*
    354  * Print out autoconfiguration information for a subdevice.
    355  *
    356  * This is a slight abuse of 'standard' autoconfiguration semantics,
    357  * because 'print' functions don't normally print the colon and
    358  * device information.  However, in this case that's better than
    359  * either printing redundant information before the attach message,
    360  * or having the device driver call a special function to print out
    361  * the standard device information.
    362  */
    363 int
    364 scsibusprint(aux, pnp)
    365 	void *aux;
    366 	const char *pnp;
    367 {
    368 	struct scsipibus_attach_args *sa = aux;
    369 	struct scsipi_inquiry_pattern *inqbuf;
    370 	u_int8_t type;
    371 	const char *dtype, *qtype;
    372 	char vendor[33], product[65], revision[17];
    373 	int target, lun;
    374 
    375 	if (pnp != NULL)
    376 		printf("%s", pnp);
    377 
    378 	inqbuf = &sa->sa_inqbuf;
    379 
    380 	target = sa->sa_periph->periph_target;
    381 	lun = sa->sa_periph->periph_lun;
    382 	type = inqbuf->type & SID_TYPE;
    383 
    384 	/*
    385 	 * Figure out basic device type and qualifier.
    386 	 */
    387 	dtype = 0;
    388 	switch (inqbuf->type & SID_QUAL) {
    389 	case SID_QUAL_LU_PRESENT:
    390 		qtype = "";
    391 		break;
    392 
    393 	case SID_QUAL_LU_NOTPRESENT:
    394 		qtype = " offline";
    395 		break;
    396 
    397 	case SID_QUAL_reserved:
    398 	case SID_QUAL_LU_NOT_SUPP:
    399 		panic("scsibusprint: impossible qualifier");
    400 
    401 	default:
    402 		qtype = "";
    403 		dtype = "vendor-unique";
    404 		break;
    405 	}
    406 	if (dtype == NULL)
    407 		dtype = scsipi_dtype(type);
    408 
    409 	scsipi_strvis(vendor, 33, inqbuf->vendor, 8);
    410 	scsipi_strvis(product, 65, inqbuf->product, 16);
    411 	scsipi_strvis(revision, 17, inqbuf->revision, 4);
    412 
    413 	printf(" target %d lun %d: <%s, %s, %s> SCSI%d %d/%s %s%s",
    414 	    target, lun, vendor, product, revision,
    415 	    sa->scsipi_info.scsi_version & SID_ANSII, type, dtype,
    416 	    inqbuf->removable ? "removable" : "fixed", qtype);
    417 
    418 	return (UNCONF);
    419 }
    420 
    421 const struct scsi_quirk_inquiry_pattern scsi_quirk_patterns[] = {
    422 	{{T_CDROM, T_REMOV,
    423 	 "CHINON  ", "CD-ROM CDS-431  ", ""},     PQUIRK_NOLUNS},
    424 	{{T_CDROM, T_REMOV,
    425 	 "Chinon  ", "CD-ROM CDS-525  ", ""},     PQUIRK_NOLUNS},
    426 	{{T_CDROM, T_REMOV,
    427 	 "CHINON  ", "CD-ROM CDS-535  ", ""},     PQUIRK_NOLUNS},
    428 	{{T_CDROM, T_REMOV,
    429 	 "DEC     ", "RRD42   (C) DEC ", ""},     PQUIRK_NOLUNS},
    430 	{{T_CDROM, T_REMOV,
    431 	 "DENON   ", "DRD-25X         ", "V"},    PQUIRK_NOLUNS},
    432 	{{T_CDROM, T_REMOV,
    433 	 "GENERIC ", "CRD-BP2         ", ""},     PQUIRK_NOLUNS},
    434 	{{T_CDROM, T_REMOV,
    435 	 "HP      ", "C4324/C4325     ", ""},     PQUIRK_NOLUNS},
    436 	{{T_CDROM, T_REMOV,
    437 	 "IMS     ", "CDD521/10       ", "2.06"}, PQUIRK_NOLUNS},
    438 	{{T_CDROM, T_REMOV,
    439 	 "MATSHITA", "CD-ROM CR-5XX   ", "1.0b"}, PQUIRK_NOLUNS},
    440 	{{T_CDROM, T_REMOV,
    441 	 "MEDAVIS ", "RENO CD-ROMX2A  ", ""},     PQUIRK_NOLUNS},
    442 	{{T_CDROM, T_REMOV,
    443 	 "MEDIAVIS", "CDR-H93MV       ", "1.3"},  PQUIRK_NOLUNS},
    444 	{{T_CDROM, T_REMOV,
    445 	 "NEC     ", "CD-ROM DRIVE:55 ", ""},     PQUIRK_NOLUNS},
    446 	{{T_CDROM, T_REMOV,
    447 	 "NEC     ", "CD-ROM DRIVE:83 ", ""},     PQUIRK_NOLUNS},
    448 	{{T_CDROM, T_REMOV,
    449 	 "NEC     ", "CD-ROM DRIVE:84 ", ""},     PQUIRK_NOLUNS},
    450 	{{T_CDROM, T_REMOV,
    451 	 "NEC     ", "CD-ROM DRIVE:841", ""},     PQUIRK_NOLUNS},
    452 	{{T_CDROM, T_REMOV,
    453 	 "PIONEER ", "CD-ROM DR-124X  ", "1.01"}, PQUIRK_NOLUNS},
    454 	{{T_CDROM, T_REMOV,
    455 	 "SONY    ", "CD-ROM CDU-541  ", ""},     PQUIRK_NOLUNS},
    456 	{{T_CDROM, T_REMOV,
    457 	 "SONY    ", "CD-ROM CDU-55S  ", ""},     PQUIRK_NOLUNS},
    458 	{{T_CDROM, T_REMOV,
    459 	 "SONY    ", "CD-ROM CDU-561  ", ""},     PQUIRK_NOLUNS},
    460 	{{T_CDROM, T_REMOV,
    461 	 "SONY    ", "CD-ROM CDU-8003A", ""},     PQUIRK_NOLUNS},
    462 	{{T_CDROM, T_REMOV,
    463 	 "SONY    ", "CD-ROM CDU-8012 ", ""},     PQUIRK_NOLUNS},
    464 	{{T_CDROM, T_REMOV,
    465 	 "TEAC    ", "CD-ROM          ", "1.06"}, PQUIRK_NOLUNS},
    466 	{{T_CDROM, T_REMOV,
    467 	 "TEAC    ", "CD-ROM CD-56S   ", "1.0B"}, PQUIRK_NOLUNS},
    468 	{{T_CDROM, T_REMOV,
    469 	 "TEXEL   ", "CD-ROM          ", "1.06"}, PQUIRK_NOLUNS},
    470 	{{T_CDROM, T_REMOV,
    471 	 "TEXEL   ", "CD-ROM DM-XX24 K", "1.09"}, PQUIRK_NOLUNS},
    472 	{{T_CDROM, T_REMOV,
    473 	 "TEXEL   ", "CD-ROM DM-XX24 K", "1.10"}, PQUIRK_NOLUNS},
    474 	{{T_CDROM, T_REMOV,
    475 	 "TOSHIBA ", "XM-4101TASUNSLCD", "1755"}, PQUIRK_NOLUNS|PQUIRK_NOSYNC},
    476 	/* "IBM CDRM00201     !F" 0724 is an IBM OEM Toshiba XM-4101BME */
    477 	{{T_CDROM, T_REMOV,
    478 	 "IBM     ", "CDRM00201     !F", "0724"}, PQUIRK_NOLUNS|PQUIRK_NOSYNC},
    479 	{{T_CDROM, T_REMOV,
    480 	 "ShinaKen", "CD-ROM DM-3x1S", "1.04"},   PQUIRK_NOLUNS},
    481 	{{T_CDROM, T_REMOV,
    482 	 "JVC     ", "R2626",            ""},     PQUIRK_NOLUNS},
    483 	{{T_CDROM, T_REMOV,
    484 	 "YAMAHA", "CRW8424S",           ""},     PQUIRK_NOLUNS},
    485 	{{T_CDROM, T_REMOV,
    486 	 "VMware", "Virtual",           "1.0"},
    487 				PQUIRK_NOSTARTUNIT|PQUIRK_NODOORLOCK},
    488 
    489 	{{T_DIRECT, T_FIXED,
    490 	 "MICROP  ", "1588-15MBSUN0669", ""},     PQUIRK_AUTOSAVE},
    491 	{{T_DIRECT, T_FIXED,
    492 	 "MICROP  ", "2217-15MQ1091501", ""},     PQUIRK_NOSYNCCACHE},
    493 	{{T_OPTICAL, T_REMOV,
    494 	 "EPSON   ", "OMD-5010        ", "3.08"}, PQUIRK_NOLUNS},
    495 	{{T_DIRECT, T_FIXED,
    496 	 "TOSHIBA ","CD-ROM XM-3401TA", "0283"},  PQUIRK_CDROM|PQUIRK_NOLUNS},
    497 	{{T_DIRECT, T_FIXED,
    498 	 "TOSHIBA ", "CD-ROM DRIVE:XM", "1971"}, PQUIRK_CDROM|PQUIRK_NOLUNS},
    499 	{{T_DIRECT, T_FIXED,
    500 	 "ADAPTEC ", "AEC-4412BD",       "1.2A"}, PQUIRK_NOMODESENSE},
    501 	{{T_DIRECT, T_FIXED,
    502 	 "DEC     ", "RZ55     (C) DEC", ""},     PQUIRK_AUTOSAVE},
    503 	{{T_DIRECT, T_FIXED,
    504 	 "EMULEX  ", "MD21/S2     ESDI", "A00"},  PQUIRK_FORCELUNS|PQUIRK_AUTOSAVE},
    505 	/* Gives non-media hardware failure in response to start-unit command */
    506 	{{T_DIRECT, T_FIXED,
    507 	 "HITACHI", "DK515C",		"CP16"},  PQUIRK_NOSTARTUNIT},
    508 	{{T_DIRECT, T_FIXED,
    509 	 "HITACHI", "DK515C",		"CP15"},  PQUIRK_NOSTARTUNIT},
    510 	{{T_DIRECT, T_FIXED,
    511 	 "HP      ", "C372",             ""},     PQUIRK_NOTAG},
    512 	{{T_DIRECT, T_FIXED,
    513 	 "IBMRAID ", "0662S",		 ""},     PQUIRK_AUTOSAVE},
    514 	{{T_DIRECT, T_FIXED,
    515 	 "IBM     ", "0663H",		 ""},     PQUIRK_AUTOSAVE},
    516 	{{T_DIRECT, T_FIXED,
    517 	 "IBM",	     "0664",		 ""},     PQUIRK_AUTOSAVE},
    518 	{{T_DIRECT, T_FIXED,
    519 	 "IBM     ", "H3171-S2",	 ""},	  PQUIRK_NOLUNS|PQUIRK_AUTOSAVE},
    520 	{{T_DIRECT, T_FIXED,
    521 	 "IBM     ", "KZ-C",		 ""},	  PQUIRK_AUTOSAVE},
    522 	/* Broken IBM disk */
    523 	{{T_DIRECT, T_FIXED,
    524 	 ""	   , "DFRSS2F",		 ""},	  PQUIRK_AUTOSAVE},
    525 	{{T_DIRECT, T_REMOV,
    526 	 "MPL     ", "MC-DISK-        ", ""},     PQUIRK_NOLUNS},
    527 	{{T_DIRECT, T_FIXED,
    528 	 "MAXTOR  ", "XT-3280         ", ""},     PQUIRK_NOLUNS},
    529 	{{T_DIRECT, T_FIXED,
    530 	 "MAXTOR  ", "XT-4380S        ", ""},     PQUIRK_NOLUNS},
    531 	{{T_DIRECT, T_FIXED,
    532 	 "MAXTOR  ", "MXT-1240S       ", ""},     PQUIRK_NOLUNS},
    533 	{{T_DIRECT, T_FIXED,
    534 	 "MAXTOR  ", "XT-4170S        ", ""},     PQUIRK_NOLUNS},
    535 	{{T_DIRECT, T_FIXED,
    536 	 "MAXTOR  ", "XT-8760S",         ""},     PQUIRK_NOLUNS},
    537 	{{T_DIRECT, T_FIXED,
    538 	 "MAXTOR  ", "LXT-213S        ", ""},     PQUIRK_NOLUNS},
    539 	{{T_DIRECT, T_FIXED,
    540 	 "MAXTOR  ", "LXT-213S SUN0207", ""},     PQUIRK_NOLUNS},
    541 	{{T_DIRECT, T_FIXED,
    542 	 "MAXTOR  ", "LXT-200S        ", ""},     PQUIRK_NOLUNS},
    543 	{{T_DIRECT, T_FIXED,
    544 	 "MEGADRV ", "EV1000",           ""},     PQUIRK_NOMODESENSE},
    545 	{{T_DIRECT, T_FIXED,
    546 	 "MST     ", "SnapLink        ", ""},     PQUIRK_NOLUNS},
    547 	{{T_DIRECT, T_FIXED,
    548 	 "NEC     ", "D3847           ", "0307"}, PQUIRK_NOLUNS},
    549 	{{T_DIRECT, T_FIXED,
    550 	 "QUANTUM ", "ELS85S          ", ""},     PQUIRK_AUTOSAVE},
    551 	{{T_DIRECT, T_FIXED,
    552 	 "QUANTUM ", "LPS525S         ", ""},     PQUIRK_NOLUNS},
    553 	{{T_DIRECT, T_FIXED,
    554 	 "QUANTUM ", "P105S 910-10-94x", ""},     PQUIRK_NOLUNS},
    555 	{{T_DIRECT, T_FIXED,
    556 	 "QUANTUM ", "PD1225S         ", ""},     PQUIRK_NOLUNS},
    557 	{{T_DIRECT, T_FIXED,
    558 	 "QUANTUM ", "PD210S   SUN0207", ""},     PQUIRK_NOLUNS},
    559 	{{T_DIRECT, T_FIXED,
    560 	 "RODIME  ", "RO3000S         ", ""},     PQUIRK_NOLUNS},
    561 	{{T_DIRECT, T_FIXED,
    562 	 "SEAGATE ", "ST125N          ", ""},     PQUIRK_NOLUNS},
    563 	{{T_DIRECT, T_FIXED,
    564 	 "SEAGATE ", "ST157N          ", ""},     PQUIRK_NOLUNS},
    565 	{{T_DIRECT, T_FIXED,
    566 	 "SEAGATE ", "ST296           ", ""},     PQUIRK_NOLUNS},
    567 	{{T_DIRECT, T_FIXED,
    568 	 "SEAGATE ", "ST296N          ", ""},     PQUIRK_NOLUNS},
    569 	{{T_DIRECT, T_FIXED,
    570 	 "SEAGATE ", "ST19171",          ""},     PQUIRK_NOMODESENSE},
    571 	{{T_DIRECT, T_FIXED,
    572 	 "SEAGATE ", "ST34501FC       ", ""},     PQUIRK_NOMODESENSE},
    573 	{{T_DIRECT, T_FIXED,
    574 	 "TOSHIBA ", "MK538FB         ", "6027"}, PQUIRK_NOLUNS},
    575 	{{T_DIRECT, T_FIXED,
    576 	 "VMware", "Virtual",           "1.0"},
    577 				PQUIRK_NOSTARTUNIT|PQUIRK_NODOORLOCK},
    578 
    579 	{{T_DIRECT, T_REMOV,
    580 	 "iomega", "jaz 1GB", 		 ""},	  PQUIRK_NOMODESENSE},
    581 	{{T_DIRECT, T_REMOV,
    582 	 "IOMEGA", "ZIP 100",		 ""},	  PQUIRK_NOMODESENSE},
    583 	{{T_DIRECT, T_REMOV,
    584 	 "IOMEGA", "ZIP 100",		 "J.03"}, PQUIRK_NOMODESENSE|PQUIRK_NOLUNS},
    585 	/* Letting the motor run kills floppy drives and disks quite fast. */
    586 	{{T_DIRECT, T_REMOV,
    587 	 "TEAC", "FC-1",		 ""},	  PQUIRK_NOSTARTUNIT},
    588 
    589 	{{T_DIRECT, T_REMOV,
    590 	 "Y-E DATA", "USB-FDU",          "3.04"}, PQUIRK_NOMODESENSE},
    591 	{{T_DIRECT, T_REMOV,
    592 	 "TEAC", "FD-05PUB",             "1026"}, PQUIRK_NOMODESENSE},
    593 
    594 	/* XXX: QIC-36 tape behind Emulex adapter.  Very broken. */
    595 	{{T_SEQUENTIAL, T_REMOV,
    596 	 "        ", "                ", "    "}, PQUIRK_NOLUNS},
    597 	{{T_SEQUENTIAL, T_REMOV,
    598 	 "CALIPER ", "CP150           ", ""},     PQUIRK_NOLUNS},
    599 	{{T_SEQUENTIAL, T_REMOV,
    600 	 "EXABYTE ", "EXB-8200        ", ""},     PQUIRK_NOLUNS},
    601 	{{T_SEQUENTIAL, T_REMOV,
    602 	 "SONY    ", "GY-10C          ", ""},     PQUIRK_NOLUNS},
    603 	{{T_SEQUENTIAL, T_REMOV,
    604 	 "SONY    ", "SDT-2000        ", "2.09"}, PQUIRK_NOLUNS},
    605 	{{T_SEQUENTIAL, T_REMOV,
    606 	 "SONY    ", "SDT-5000        ", "3."},   PQUIRK_NOSYNC|PQUIRK_NOWIDE},
    607 	{{T_SEQUENTIAL, T_REMOV,
    608 	 "SONY    ", "SDT-5200        ", "3."},   PQUIRK_NOLUNS},
    609 	{{T_SEQUENTIAL, T_REMOV,
    610 	 "TANDBERG", " TDC 3600       ", ""},     PQUIRK_NOLUNS},
    611 	/* Following entry reported as a Tandberg 3600; ref. PR1933 */
    612 	{{T_SEQUENTIAL, T_REMOV,
    613 	 "ARCHIVE ", "VIPER 150  21247", ""},     PQUIRK_NOLUNS},
    614 	/* Following entry for a Cipher ST150S; ref. PR4171 */
    615 	{{T_SEQUENTIAL, T_REMOV,
    616 	 "ARCHIVE ", "VIPER 1500 21247", "2.2G"}, PQUIRK_NOLUNS},
    617 	{{T_SEQUENTIAL, T_REMOV,
    618 	 "ARCHIVE ", "Python 28454-XXX", ""},     PQUIRK_NOLUNS},
    619 	{{T_SEQUENTIAL, T_REMOV,
    620 	 "WANGTEK ", "5099ES SCSI",      ""},     PQUIRK_NOLUNS},
    621 	{{T_SEQUENTIAL, T_REMOV,
    622 	 "WANGTEK ", "5150ES SCSI",      ""},     PQUIRK_NOLUNS},
    623 	{{T_SEQUENTIAL, T_REMOV,
    624 	 "WANGTEK ", "SCSI-36",		 ""},     PQUIRK_NOLUNS},
    625 	{{T_SEQUENTIAL, T_REMOV,
    626 	 "WangDAT ", "Model 1300      ", "02.4"}, PQUIRK_NOSYNC|PQUIRK_NOWIDE},
    627 	{{T_SEQUENTIAL, T_REMOV,
    628 	 "WangDAT ", "Model 2600      ", "01.7"}, PQUIRK_NOSYNC|PQUIRK_NOWIDE},
    629 	{{T_SEQUENTIAL, T_REMOV,
    630 	 "WangDAT ", "Model 3200      ", "02.2"}, PQUIRK_NOSYNC|PQUIRK_NOWIDE},
    631 	{{T_SEQUENTIAL, T_REMOV,
    632 	 "TEAC    ", "MT-2ST/N50      ", ""},     PQUIRK_NOLUNS},
    633 
    634 	{{T_SCANNER, T_FIXED,
    635 	 "RICOH   ", "IS60            ", "1R08"}, PQUIRK_NOLUNS},
    636 	{{T_SCANNER, T_FIXED,
    637 	 "UMAX    ", "Astra 1200S     ", "V2.9"}, PQUIRK_NOLUNS},
    638 	{{T_SCANNER, T_FIXED,
    639 	 "UMAX    ", "Astra 1220S     ", ""},     PQUIRK_NOLUNS},
    640 	{{T_SCANNER, T_FIXED,
    641 	 "UMAX    ", "UMAX S-6E       ", "V2.0"}, PQUIRK_NOLUNS},
    642 	{{T_SCANNER, T_FIXED,
    643 	 "UMAX    ", "UMAX S-12       ", "V2.1"}, PQUIRK_NOLUNS},
    644 	{{T_SCANNER, T_FIXED,
    645 	 "ULTIMA  ", "A6000C          ", ""}, PQUIRK_NOLUNS},
    646 	{{T_PROCESSOR, T_FIXED,
    647 	 "SYMBIOS", "", ""},     PQUIRK_NOLUNS},
    648 	{{T_PROCESSOR, T_FIXED,
    649 	 "LITRONIC", "PCMCIA          ", ""},     PQUIRK_NOLUNS},
    650 	{{T_CHANGER, T_REMOV,
    651 	 "SONY    ", "CDL1100         ", ""},     PQUIRK_NOLUNS},
    652 	{{T_ENCLOSURE, T_FIXED,
    653 	 "SUN     ", "SENA            ", ""}, PQUIRK_NOLUNS},
    654 };
    655 
    656 /*
    657  * given a target and lun, ask the device what
    658  * it is, and find the correct driver table
    659  * entry.
    660  */
    661 int
    662 scsi_probe_device(sc, target, lun)
    663 	struct scsibus_softc *sc;
    664 	int target, lun;
    665 {
    666 	struct scsipi_channel *chan = sc->sc_channel;
    667 	struct scsipi_periph *periph;
    668 	struct scsipi_inquiry_data inqbuf;
    669 	struct scsi_quirk_inquiry_pattern *finger;
    670 	int checkdtype, priority, docontinue, quirks;
    671 	struct scsipibus_attach_args sa;
    672 	struct cfdata *cf;
    673 
    674 	/*
    675 	 * Assume no more luns to search after this one.
    676 	 * If we successfully get Inquiry data and after
    677 	 * merging quirks we find we can probe for more
    678 	 * luns, we will.
    679 	 */
    680 	docontinue = 0;
    681 
    682 	/* Skip this slot if it is already attached. */
    683 	if (scsipi_lookup_periph(chan, target, lun) != NULL)
    684 		return (docontinue);
    685 
    686 	periph = scsipi_alloc_periph(M_NOWAIT);
    687 	if (periph == NULL) {
    688 #ifdef	DIAGNOSTIC
    689 		printf("%s: cannot allocate periph for target %d lun %d\n",
    690 		    sc->sc_dev.dv_xname, target, lun);
    691 #endif
    692 		return (ENOMEM);
    693 	}
    694 	periph->periph_channel = chan;
    695 	periph->periph_switch = &scsi_probe_dev;
    696 
    697 	periph->periph_target = target;
    698 	periph->periph_lun = lun;
    699 	periph->periph_quirks = chan->chan_defquirks;
    700 
    701 #ifdef SCSIPI_DEBUG
    702 	if (SCSIPI_DEBUG_TYPE == SCSIPI_BUSTYPE_SCSI &&
    703 	    SCSIPI_DEBUG_TARGET == target &&
    704 	    SCSIPI_DEBUG_LUN == lun)
    705 		periph->periph_dbflags |= SCSIPI_DEBUG_FLAGS;
    706 #endif
    707 
    708 	/*
    709 	 * Ask the device what it is
    710 	 */
    711 	(void) scsipi_test_unit_ready(periph,
    712 	    XS_CTL_DISCOVERY | XS_CTL_IGNORE_ILLEGAL_REQUEST |
    713 	    XS_CTL_IGNORE_NOT_READY | XS_CTL_IGNORE_MEDIA_CHANGE);
    714 
    715 #ifdef SCSI_2_DEF
    716 	/* some devices need to be told to go to SCSI2 */
    717 	/* However some just explode if you tell them this.. leave it out */
    718 	scsi_change_def(periph, XS_CTL_DISCOVERY | XS_CTL_SILENT);
    719 #endif /* SCSI_2_DEF */
    720 
    721 	/* Now go ask the device all about itself. */
    722 	memset(&inqbuf, 0, sizeof(inqbuf));
    723 	if (scsipi_inquire(periph, &inqbuf,
    724 	    XS_CTL_DISCOVERY | XS_CTL_DATA_ONSTACK) != 0)
    725 		goto bad;
    726 	{
    727 		u_int8_t *extension = &inqbuf.flags1;
    728 		int len = inqbuf.additional_length;
    729 		while (len < 3)
    730 			extension[len++] = '\0';
    731 		while (len < 3 + 28)
    732 			extension[len++] = ' ';
    733 		while (len < 3 + 28 + 20)
    734 			extension[len++] = '\0';
    735 		while (len < 3 + 28 + 20 + 1)
    736 			extension[len++] = '\0';
    737 		while (len < 3 + 28 + 20 + 1 + 1)
    738 			extension[len++] = '\0';
    739 		while (len < 3 + 28 + 20 + 1 + 1 + (8*2))
    740 			extension[len++] = ' ';
    741 	}
    742 
    743 	periph->periph_type = inqbuf.device & SID_TYPE;
    744 	if (inqbuf.dev_qual2 & SID_REMOVABLE)
    745 		periph->periph_flags |= PERIPH_REMOVABLE;
    746 	periph->periph_version = inqbuf.version & SID_ANSII;
    747 
    748 	/*
    749 	 * Any device qualifier that has the top bit set (qualifier&4 != 0)
    750 	 * is vendor specific and won't match in this switch.
    751 	 * All we do here is throw out bad/negative responses.
    752 	 */
    753 	checkdtype = 0;
    754 	switch (inqbuf.device & SID_QUAL) {
    755 	case SID_QUAL_LU_PRESENT:
    756 	case SID_QUAL_LU_NOTPRESENT:
    757 		checkdtype = 1;
    758 		break;
    759 
    760 	case SID_QUAL_reserved:
    761 	case SID_QUAL_LU_NOT_SUPP:
    762 		goto bad;
    763 
    764 	default:
    765 		break;
    766 	}
    767 
    768 	/* Let the adapter driver handle the device separatley if it wants. */
    769 	if (chan->chan_adapter->adapt_accesschk != NULL &&
    770 	    (*chan->chan_adapter->adapt_accesschk)(periph, &sa.sa_inqbuf))
    771 		goto bad;
    772 
    773 	if (checkdtype) {
    774 		switch (periph->periph_type) {
    775 		case T_DIRECT:
    776 		case T_SEQUENTIAL:
    777 		case T_PRINTER:
    778 		case T_PROCESSOR:
    779 		case T_WORM:
    780 		case T_CDROM:
    781 		case T_SCANNER:
    782 		case T_OPTICAL:
    783 		case T_CHANGER:
    784 		case T_COMM:
    785 		case T_IT8_1:
    786 		case T_IT8_2:
    787 		case T_STORARRAY:
    788 		case T_ENCLOSURE:
    789 		case T_SIMPLE_DIRECT:
    790 		case T_OPTIC_CARD_RW:
    791 		case T_OBJECT_STORED:
    792 		default:
    793 			break;
    794 		case T_NODEVICE:
    795 			goto bad;
    796 		}
    797 	}
    798 
    799 	sa.sa_periph = periph;
    800 	sa.sa_inqbuf.type = inqbuf.device;
    801 	sa.sa_inqbuf.removable = inqbuf.dev_qual2 & SID_REMOVABLE ?
    802 	    T_REMOV : T_FIXED;
    803 	sa.sa_inqbuf.vendor = inqbuf.vendor;
    804 	sa.sa_inqbuf.product = inqbuf.product;
    805 	sa.sa_inqbuf.revision = inqbuf.revision;
    806 	sa.scsipi_info.scsi_version = inqbuf.version;
    807 	sa.sa_inqptr = &inqbuf;
    808 
    809 	finger = (struct scsi_quirk_inquiry_pattern *)scsipi_inqmatch(
    810 	    &sa.sa_inqbuf, (caddr_t)scsi_quirk_patterns,
    811 	    sizeof(scsi_quirk_patterns)/sizeof(scsi_quirk_patterns[0]),
    812 	    sizeof(scsi_quirk_patterns[0]), &priority);
    813 
    814 	if (finger != NULL)
    815 		quirks = finger->quirks;
    816 	else
    817 		quirks = 0;
    818 
    819 	/*
    820 	 * Determine the operating mode capabilities of the device.
    821 	 */
    822 	if (periph->periph_version >= 2) {
    823 		if ((inqbuf.flags3 & SID_CmdQue) != 0 &&
    824 		    (quirks & PQUIRK_NOTAG) == 0)
    825 			periph->periph_cap |= PERIPH_CAP_TQING;
    826 		if ((inqbuf.flags3 & SID_Linked) != 0)
    827 			periph->periph_cap |= PERIPH_CAP_LINKCMDS;
    828 		if ((inqbuf.flags3 & SID_Sync) != 0 &&
    829 		    (quirks & PQUIRK_NOSYNC) == 0)
    830 			periph->periph_cap |= PERIPH_CAP_SYNC;
    831 		if ((inqbuf.flags3 & SID_WBus16) != 0 &&
    832 		    (quirks & PQUIRK_NOWIDE) == 0)
    833 			periph->periph_cap |= PERIPH_CAP_WIDE16;
    834 		if ((inqbuf.flags3 & SID_WBus32) != 0 &&
    835 		    (quirks & PQUIRK_NOWIDE) == 0)
    836 			periph->periph_cap |= PERIPH_CAP_WIDE32;
    837 		if ((inqbuf.flags3 & SID_SftRe) != 0)
    838 			periph->periph_cap |= PERIPH_CAP_SFTRESET;
    839 		if ((inqbuf.flags3 & SID_RelAdr) != 0)
    840 			periph->periph_cap |= PERIPH_CAP_RELADR;
    841 	}
    842 
    843 	/*
    844 	 * Now apply any quirks from the table.
    845 	 */
    846 	periph->periph_quirks |= quirks;
    847 	if (periph->periph_version == 0 &&
    848 	    (periph->periph_quirks & PQUIRK_FORCELUNS) == 0)
    849 		periph->periph_quirks |= PQUIRK_NOLUNS;
    850 
    851 	if (periph->periph_quirks & PQUIRK_CDROM) {
    852 		periph->periph_quirks ^= PQUIRK_CDROM;
    853 		inqbuf.dev_qual2 |= SID_REMOVABLE;
    854 		sa.sa_inqbuf.type = inqbuf.device = ((inqbuf.device & ~SID_REMOVABLE) | T_CDROM);
    855 		sa.sa_inqbuf.removable = T_REMOV;
    856 	}
    857 
    858 	if ((periph->periph_quirks & PQUIRK_NOLUNS) == 0)
    859 		docontinue = 1;
    860 
    861 	if ((cf = config_search(scsibussubmatch, &sc->sc_dev, &sa)) != NULL) {
    862 		scsipi_insert_periph(chan, periph);
    863 		/*
    864 		 * XXX Can't assign periph_dev here, because we'll
    865 		 * XXX need it before config_attach() returns.  Must
    866 		 * XXX assign it in periph driver.
    867 		 */
    868 		(void) config_attach(&sc->sc_dev, cf, &sa, scsibusprint);
    869 	} else {
    870 		scsibusprint(&sa, sc->sc_dev.dv_xname);
    871 		printf(" not configured\n");
    872 		goto bad;
    873 	}
    874 
    875 	return (docontinue);
    876 
    877 bad:
    878 	free(periph, M_DEVBUF);
    879 	return (docontinue);
    880 }
    881 
    882 /****** Entry points for user control of the SCSI bus. ******/
    883 
    884 int
    885 scsibusopen(dev, flag, fmt, p)
    886 	dev_t dev;
    887 	int flag, fmt;
    888 	struct proc *p;
    889 {
    890 	struct scsibus_softc *sc;
    891 	int error, unit = minor(dev);
    892 
    893 	if (unit >= scsibus_cd.cd_ndevs ||
    894 	    (sc = scsibus_cd.cd_devs[unit]) == NULL)
    895 		return (ENXIO);
    896 
    897 	if (sc->sc_flags & SCSIBUSF_OPEN)
    898 		return (EBUSY);
    899 
    900 	if ((error = scsipi_adapter_addref(sc->sc_channel->chan_adapter)) != 0)
    901 		return (error);
    902 
    903 	sc->sc_flags |= SCSIBUSF_OPEN;
    904 
    905 	return (0);
    906 }
    907 
    908 int
    909 scsibusclose(dev, flag, fmt, p)
    910 	dev_t dev;
    911 	int flag, fmt;
    912 	struct proc *p;
    913 {
    914 	struct scsibus_softc *sc = scsibus_cd.cd_devs[minor(dev)];
    915 
    916 	scsipi_adapter_delref(sc->sc_channel->chan_adapter);
    917 
    918 	sc->sc_flags &= ~SCSIBUSF_OPEN;
    919 
    920 	return (0);
    921 }
    922 
    923 int
    924 scsibusioctl(dev, cmd, addr, flag, p)
    925 	dev_t dev;
    926 	u_long cmd;
    927 	caddr_t addr;
    928 	int flag;
    929 	struct proc *p;
    930 {
    931 	struct scsibus_softc *sc = scsibus_cd.cd_devs[minor(dev)];
    932 	struct scsipi_channel *chan = sc->sc_channel;
    933 	int error;
    934 
    935 	/*
    936 	 * Enforce write permission for ioctls that change the
    937 	 * state of the bus.  Host adapter specific ioctls must
    938 	 * be checked by the adapter driver.
    939 	 */
    940 	switch (cmd) {
    941 	case SCBUSIOSCAN:
    942 	case SCBUSIORESET:
    943 		if ((flag & FWRITE) == 0)
    944 			return (EBADF);
    945 	}
    946 
    947 	switch (cmd) {
    948 	case SCBUSIOSCAN:
    949 	    {
    950 		struct scbusioscan_args *a =
    951 		    (struct scbusioscan_args *)addr;
    952 
    953 		error = scsi_probe_bus(sc, a->sa_target, a->sa_lun);
    954 		break;
    955 	    }
    956 
    957 	case SCBUSIORESET:
    958 		/* FALLTHROUGH */
    959 	default:
    960 		if (chan->chan_adapter->adapt_ioctl == NULL)
    961 			error = ENOTTY;
    962 		else
    963 			error = (*chan->chan_adapter->adapt_ioctl)(chan,
    964 			    cmd, addr, flag, p);
    965 		break;
    966 	}
    967 
    968 	return (error);
    969 }
    970