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