Home | History | Annotate | Line # | Download | only in scsipi
scsiconf.c revision 1.279
      1 /*	$NetBSD: scsiconf.c,v 1.279 2017/03/18 08:05:40 tsutsui Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998, 1999, 2004 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  *
     20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * Originally written by Julian Elischer (julian (at) tfs.com)
     35  * for TRW Financial Systems for use under the MACH(2.5) operating system.
     36  *
     37  * TRW Financial Systems, in accordance with their agreement with Carnegie
     38  * Mellon University, makes this software available to CMU to distribute
     39  * or use in any manner that they see fit as long as this message is kept with
     40  * the software. For this reason TFS also grants any other persons or
     41  * organisations permission to use or modify this software.
     42  *
     43  * TFS supplies this software to be publicly redistributed
     44  * on the understanding that TFS is not responsible for the correct
     45  * functioning of this software in any circumstances.
     46  *
     47  * Ported to run under 386BSD by Julian Elischer (julian (at) tfs.com) Sept 1992
     48  */
     49 
     50 #include <sys/cdefs.h>
     51 __KERNEL_RCSID(0, "$NetBSD: scsiconf.c,v 1.279 2017/03/18 08:05:40 tsutsui Exp $");
     52 
     53 #include <sys/param.h>
     54 #include <sys/systm.h>
     55 #include <sys/kernel.h>
     56 #include <sys/proc.h>
     57 #include <sys/kthread.h>
     58 #include <sys/malloc.h>
     59 #include <sys/mutex.h>
     60 #include <sys/once.h>
     61 #include <sys/device.h>
     62 #include <sys/conf.h>
     63 #include <sys/fcntl.h>
     64 #include <sys/scsiio.h>
     65 #include <sys/queue.h>
     66 #include <sys/atomic.h>
     67 
     68 #include <dev/scsipi/scsi_all.h>
     69 #include <dev/scsipi/scsipi_all.h>
     70 #include <dev/scsipi/scsiconf.h>
     71 
     72 #include "locators.h"
     73 
     74 static const struct scsipi_periphsw scsi_probe_dev = {
     75 	NULL,
     76 	NULL,
     77 	NULL,
     78 	NULL,
     79 };
     80 
     81 struct scsi_initq {
     82 	struct scsipi_channel *sc_channel;
     83 	TAILQ_ENTRY(scsi_initq) scsi_initq;
     84 };
     85 
     86 static ONCE_DECL(scsi_conf_ctrl);
     87 static TAILQ_HEAD(, scsi_initq)	scsi_initq_head;
     88 static kmutex_t			scsibus_qlock;
     89 static kcondvar_t		scsibus_qcv;
     90 
     91 static int	scsi_probe_device(struct scsibus_softc *, int, int);
     92 
     93 static int	scsibusmatch(device_t, cfdata_t, void *);
     94 static void	scsibusattach(device_t, device_t, void *);
     95 static int	scsibusdetach(device_t, int flags);
     96 static int	scsibusrescan(device_t, const char *, const int *);
     97 static void	scsidevdetached(device_t, device_t);
     98 
     99 CFATTACH_DECL3_NEW(scsibus, sizeof(struct scsibus_softc),
    100     scsibusmatch, scsibusattach, scsibusdetach, NULL,
    101     scsibusrescan, scsidevdetached, DVF_DETACH_SHUTDOWN);
    102 
    103 extern struct cfdriver scsibus_cd;
    104 
    105 static dev_type_open(scsibusopen);
    106 static dev_type_close(scsibusclose);
    107 static dev_type_ioctl(scsibusioctl);
    108 
    109 const struct cdevsw scsibus_cdevsw = {
    110 	.d_open = scsibusopen,
    111 	.d_close = scsibusclose,
    112 	.d_read = noread,
    113 	.d_write = nowrite,
    114 	.d_ioctl = scsibusioctl,
    115 	.d_stop = nostop,
    116 	.d_tty = notty,
    117 	.d_poll = nopoll,
    118 	.d_mmap = nommap,
    119 	.d_kqfilter = nokqfilter,
    120 	.d_discard = nodiscard,
    121 	.d_flag = D_OTHER | D_MPSAFE
    122 };
    123 
    124 static int	scsibusprint(void *, const char *);
    125 static void	scsibus_discover_thread(void *);
    126 static void	scsibus_config(struct scsibus_softc *);
    127 
    128 const struct scsipi_bustype scsi_bustype = {
    129 	SCSIPI_BUSTYPE_BUSTYPE(SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_PSCSI),
    130 	scsi_scsipi_cmd,
    131 	scsipi_interpret_sense,
    132 	scsi_print_addr,
    133 	scsi_kill_pending,
    134 	scsi_async_event_xfer_mode,
    135 };
    136 
    137 const struct scsipi_bustype scsi_fc_bustype = {
    138 	SCSIPI_BUSTYPE_BUSTYPE(SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_FC),
    139 	scsi_scsipi_cmd,
    140 	scsipi_interpret_sense,
    141 	scsi_print_addr,
    142 	scsi_kill_pending,
    143 	scsi_fc_sas_async_event_xfer_mode,
    144 };
    145 
    146 const struct scsipi_bustype scsi_sas_bustype = {
    147 	SCSIPI_BUSTYPE_BUSTYPE(SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_SAS),
    148 	scsi_scsipi_cmd,
    149 	scsipi_interpret_sense,
    150 	scsi_print_addr,
    151 	scsi_kill_pending,
    152 	scsi_fc_sas_async_event_xfer_mode,
    153 };
    154 
    155 const struct scsipi_bustype scsi_usb_bustype = {
    156 	SCSIPI_BUSTYPE_BUSTYPE(SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_USB),
    157 	scsi_scsipi_cmd,
    158 	scsipi_interpret_sense,
    159 	scsi_print_addr,
    160 	scsi_kill_pending,
    161 	NULL,
    162 };
    163 
    164 static int
    165 scsibus_init(void)
    166 {
    167 
    168 	TAILQ_INIT(&scsi_initq_head);
    169 	mutex_init(&scsibus_qlock, MUTEX_DEFAULT, IPL_NONE);
    170 	cv_init(&scsibus_qcv, "scsinitq");
    171 	return 0;
    172 }
    173 
    174 int
    175 scsiprint(void *aux, const char *pnp)
    176 {
    177 	struct scsipi_channel *chan = aux;
    178 	struct scsipi_adapter *adapt = chan->chan_adapter;
    179 
    180 	/* only "scsibus"es can attach to "scsi"s; easy. */
    181 	if (pnp)
    182 		aprint_normal("scsibus at %s", pnp);
    183 
    184 	/* don't print channel if the controller says there can be only one. */
    185 	if (adapt->adapt_nchannels != 1)
    186 		aprint_normal(" channel %d", chan->chan_channel);
    187 
    188 	return (UNCONF);
    189 }
    190 
    191 static int
    192 scsibusmatch(device_t parent, cfdata_t cf, void *aux)
    193 {
    194 	struct scsipi_channel *chan = aux;
    195 
    196 	if (SCSIPI_BUSTYPE_TYPE(chan->chan_bustype->bustype_type) !=
    197 	    SCSIPI_BUSTYPE_SCSI)
    198 		return 0;
    199 
    200 	if (cf->cf_loc[SCSICF_CHANNEL] != chan->chan_channel &&
    201 	    cf->cf_loc[SCSICF_CHANNEL] != SCSICF_CHANNEL_DEFAULT)
    202 		return (0);
    203 
    204 	return (1);
    205 }
    206 
    207 static void
    208 scsibusattach(device_t parent, device_t self, void *aux)
    209 {
    210 	struct scsibus_softc *sc = device_private(self);
    211 	struct scsipi_channel *chan = aux;
    212 	struct scsi_initq *scsi_initq;
    213 
    214 	if (!pmf_device_register(self, NULL, NULL))
    215 		aprint_error_dev(self, "couldn't establish power handler\n");
    216 
    217 	sc->sc_dev = self;
    218 	sc->sc_channel = chan;
    219 	chan->chan_name = device_xname(sc->sc_dev);
    220 
    221 	aprint_naive(": SCSI bus\n");
    222 	aprint_normal(": %d target%s, %d lun%s per target\n",
    223 	    chan->chan_ntargets,
    224 	    chan->chan_ntargets == 1 ? "" : "s",
    225 	    chan->chan_nluns,
    226 	    chan->chan_nluns == 1 ? "" : "s");
    227 
    228 	/*
    229 	 * XXX
    230 	 * newer adapters support more than 256 outstanding commands
    231 	 * per periph and don't use the tag (they eventually allocate one
    232 	 * internally). Right now scsipi always allocate a tag and
    233 	 * is limited to 256 tags, per scsi specs.
    234 	 * this should be revisited
    235 	 */
    236 	if (chan->chan_flags & SCSIPI_CHAN_OPENINGS) {
    237 		if (chan->chan_max_periph > 256)
    238 			chan->chan_max_periph = 256;
    239 	} else {
    240 		if (chan->chan_adapter->adapt_max_periph > 256)
    241 			chan->chan_adapter->adapt_max_periph = 256;
    242 	}
    243 
    244 	if (atomic_inc_uint_nv(&chan_running(chan)) == 1)
    245 		mutex_init(chan_mtx(chan), MUTEX_DEFAULT, IPL_BIO);
    246 
    247 	cv_init(&chan->chan_cv_thr, "scshut");
    248 	cv_init(&chan->chan_cv_comp, "sccomp");
    249 	cv_init(&chan->chan_cv_xs, "xscmd");
    250 
    251 	if (scsipi_adapter_addref(chan->chan_adapter))
    252 		return;
    253 
    254 	RUN_ONCE(&scsi_conf_ctrl, scsibus_init);
    255 
    256 	/* Initialize the channel structure first */
    257 	chan->chan_init_cb = NULL;
    258 	chan->chan_init_cb_arg = NULL;
    259 
    260 	scsi_initq = malloc(sizeof(struct scsi_initq), M_DEVBUF, M_WAITOK);
    261 	scsi_initq->sc_channel = chan;
    262 	TAILQ_INSERT_TAIL(&scsi_initq_head, scsi_initq, scsi_initq);
    263         config_pending_incr(sc->sc_dev);
    264 	if (scsipi_channel_init(chan)) {
    265 		aprint_error_dev(sc->sc_dev, "failed to init channel\n");
    266 		return;
    267 	}
    268 
    269         /*
    270          * Create the discover thread
    271          */
    272         if (kthread_create(PRI_NONE, 0, NULL, scsibus_discover_thread, sc,
    273             NULL, "%s-d", chan->chan_name)) {
    274                 aprint_error_dev(sc->sc_dev, "unable to create discovery "
    275 		    "thread for channel %d\n", chan->chan_channel);
    276                 return;
    277         }
    278 }
    279 
    280 static void
    281 scsibus_discover_thread(void *arg)
    282 {
    283 	struct scsibus_softc *sc = arg;
    284 
    285 	scsibus_config(sc);
    286 	kthread_exit(0);
    287 }
    288 
    289 static void
    290 scsibus_config(struct scsibus_softc *sc)
    291 {
    292 	struct scsipi_channel *chan = sc->sc_channel;
    293 	struct scsi_initq *scsi_initq;
    294 
    295 #ifndef SCSI_DELAY
    296 #define SCSI_DELAY 2
    297 #endif
    298 	if ((chan->chan_flags & SCSIPI_CHAN_NOSETTLE) == 0 &&
    299 	    SCSI_DELAY > 0) {
    300 		aprint_normal_dev(sc->sc_dev,
    301 		    "waiting %d seconds for devices to settle...\n",
    302 		    SCSI_DELAY);
    303 		/* ...an identifier we know no one will use... */
    304 		kpause("scsidly", false, SCSI_DELAY * hz, NULL);
    305 	}
    306 
    307 	/* Make sure the devices probe in scsibus order to avoid jitter. */
    308 	mutex_enter(&scsibus_qlock);
    309 	for (;;) {
    310 		scsi_initq = TAILQ_FIRST(&scsi_initq_head);
    311 		if (scsi_initq->sc_channel == chan)
    312 			break;
    313 		cv_wait(&scsibus_qcv, &scsibus_qlock);
    314 	}
    315 	mutex_exit(&scsibus_qlock);
    316 
    317 	scsi_probe_bus(sc, -1, -1);
    318 
    319 	mutex_enter(&scsibus_qlock);
    320 	TAILQ_REMOVE(&scsi_initq_head, scsi_initq, scsi_initq);
    321 	cv_broadcast(&scsibus_qcv);
    322 	mutex_exit(&scsibus_qlock);
    323 
    324 	free(scsi_initq, M_DEVBUF);
    325 
    326 	scsipi_adapter_delref(chan->chan_adapter);
    327 
    328 	config_pending_decr(sc->sc_dev);
    329 }
    330 
    331 static int
    332 scsibusdetach(device_t self, int flags)
    333 {
    334 	struct scsibus_softc *sc = device_private(self);
    335 	struct scsipi_channel *chan = sc->sc_channel;
    336 	struct scsipi_periph *periph;
    337 	int ctarget, clun;
    338 	struct scsipi_xfer *xs;
    339 	int error;
    340 
    341 	/*
    342 	 * Detach all of the periphs.
    343 	 */
    344 	if ((error = scsipi_target_detach(chan, -1, -1, flags)) != 0)
    345 		return error;
    346 
    347 	pmf_device_deregister(self);
    348 
    349 	/*
    350 	 * Process outstanding commands (which will never complete as the
    351 	 * controller is gone).
    352 	 *
    353 	 * XXX Surely this is redundant?  If we get this far, the
    354 	 * XXX peripherals have all been detached.
    355 	 */
    356 	for (ctarget = 0; ctarget < chan->chan_ntargets; ctarget++) {
    357 		if (ctarget == chan->chan_id)
    358 			continue;
    359 		for (clun = 0; clun < chan->chan_nluns; clun++) {
    360 			periph = scsipi_lookup_periph(chan, ctarget, clun);
    361 			if (periph == NULL)
    362 				continue;
    363 			TAILQ_FOREACH(xs, &periph->periph_xferq, device_q) {
    364 				callout_stop(&xs->xs_callout);
    365 				xs->error = XS_DRIVER_STUFFUP;
    366 				scsipi_done(xs);
    367 			}
    368 		}
    369 	}
    370 
    371 	/*
    372 	 * Now shut down the channel.
    373 	 */
    374 	scsipi_channel_shutdown(chan);
    375 
    376 	cv_destroy(&chan->chan_cv_xs);
    377 	cv_destroy(&chan->chan_cv_comp);
    378 	cv_destroy(&chan->chan_cv_thr);
    379 
    380 	if (atomic_dec_uint_nv(&chan_running(chan)) == 0)
    381 		mutex_destroy(chan_mtx(chan));
    382 
    383 	return 0;
    384 }
    385 
    386 /*
    387  * Probe the requested scsi bus. It must be already set up.
    388  * target and lun optionally narrow the search if not -1
    389  */
    390 int
    391 scsi_probe_bus(struct scsibus_softc *sc, int target, int lun)
    392 {
    393 	struct scsipi_channel *chan = sc->sc_channel;
    394 	int maxtarget, mintarget, maxlun, minlun;
    395 	int error;
    396 
    397 	if (target == -1) {
    398 		maxtarget = chan->chan_ntargets - 1;
    399 		mintarget = 0;
    400 	} else {
    401 		if (target < 0 || target >= chan->chan_ntargets)
    402 			return (EINVAL);
    403 		maxtarget = mintarget = target;
    404 	}
    405 
    406 	if (lun == -1) {
    407 		maxlun = chan->chan_nluns - 1;
    408 		minlun = 0;
    409 	} else {
    410 		if (lun < 0 || lun >= chan->chan_nluns)
    411 			return (EINVAL);
    412 		maxlun = minlun = lun;
    413 	}
    414 
    415 	/*
    416 	 * Some HBAs provide an abstracted view of the bus; give them an
    417 	 * oppertunity to re-scan it before we do.
    418 	 */
    419 	scsipi_adapter_ioctl(chan, SCBUSIOLLSCAN, NULL, 0, curproc);
    420 
    421 	if ((error = scsipi_adapter_addref(chan->chan_adapter)) != 0)
    422 		goto ret;
    423 	for (target = mintarget; target <= maxtarget; target++) {
    424 		if (target == chan->chan_id)
    425 			continue;
    426 		for (lun = minlun; lun <= maxlun; lun++) {
    427 			/*
    428 			 * See if there's a device present, and configure it.
    429 			 */
    430 			if (scsi_probe_device(sc, target, lun) == 0)
    431 				break;
    432 			/* otherwise something says we should look further */
    433 		}
    434 
    435 		/*
    436 		 * Now that we've discovered all of the LUNs on this
    437 		 * I_T Nexus, update the xfer mode for all of them
    438 		 * that we know about.
    439 		 */
    440 		scsipi_set_xfer_mode(chan, target, 1);
    441 	}
    442 	scsipi_adapter_delref(chan->chan_adapter);
    443 ret:
    444 	return (error);
    445 }
    446 
    447 static int
    448 scsibusrescan(device_t sc, const char *ifattr,
    449     const int *locators)
    450 {
    451 
    452 	KASSERT(ifattr && !strcmp(ifattr, "scsibus"));
    453 	KASSERT(locators);
    454 
    455 	return (scsi_probe_bus(device_private(sc),
    456 		locators[SCSIBUSCF_TARGET], locators[SCSIBUSCF_LUN]));
    457 }
    458 
    459 static void
    460 scsidevdetached(device_t self, device_t child)
    461 {
    462 	struct scsibus_softc *sc = device_private(self);
    463 	struct scsipi_channel *chan = sc->sc_channel;
    464 	struct scsipi_periph *periph;
    465 	int target, lun;
    466 
    467 	target = device_locator(child, SCSIBUSCF_TARGET);
    468 	lun = device_locator(child, SCSIBUSCF_LUN);
    469 
    470 	mutex_enter(chan_mtx(chan));
    471 
    472 	periph = scsipi_lookup_periph_locked(chan, target, lun);
    473 	KASSERT(periph != NULL && periph->periph_dev == child);
    474 
    475 	scsipi_remove_periph(chan, periph);
    476 	scsipi_free_periph(periph);
    477 
    478 	mutex_exit(chan_mtx(chan));
    479 }
    480 
    481 /*
    482  * Print out autoconfiguration information for a subdevice.
    483  *
    484  * This is a slight abuse of 'standard' autoconfiguration semantics,
    485  * because 'print' functions don't normally print the colon and
    486  * device information.  However, in this case that's better than
    487  * either printing redundant information before the attach message,
    488  * or having the device driver call a special function to print out
    489  * the standard device information.
    490  */
    491 static int
    492 scsibusprint(void *aux, const char *pnp)
    493 {
    494 	struct scsipibus_attach_args *sa = aux;
    495 	struct scsipi_inquiry_pattern *inqbuf;
    496 	u_int8_t type;
    497 	const char *dtype;
    498 	char vendor[33], product[65], revision[17];
    499 	int target, lun;
    500 
    501 	if (pnp != NULL)
    502 		aprint_normal("%s", pnp);
    503 
    504 	inqbuf = &sa->sa_inqbuf;
    505 
    506 	target = sa->sa_periph->periph_target;
    507 	lun = sa->sa_periph->periph_lun;
    508 	type = inqbuf->type & SID_TYPE;
    509 
    510 	dtype = scsipi_dtype(type);
    511 
    512 	strnvisx(vendor, sizeof(vendor), inqbuf->vendor, 8,
    513 	    VIS_TRIM|VIS_SAFE|VIS_OCTAL);
    514 	strnvisx(product, sizeof(product), inqbuf->product, 16,
    515 	    VIS_TRIM|VIS_SAFE|VIS_OCTAL);
    516 	strnvisx(revision, sizeof(revision), inqbuf->revision, 4,
    517 	    VIS_TRIM|VIS_SAFE|VIS_OCTAL);
    518 
    519 	aprint_normal(" target %d lun %d: <%s, %s, %s> %s %s",
    520 	    target, lun, vendor, product, revision, dtype,
    521 	    inqbuf->removable ? "removable" : "fixed");
    522 
    523 	return (UNCONF);
    524 }
    525 
    526 static const struct scsi_quirk_inquiry_pattern scsi_quirk_patterns[] = {
    527 	{{T_DIRECT, T_REMOV,
    528 	 "Apple   ", "iPod            ", ""},	  PQUIRK_START},
    529 	{{T_CDROM, T_REMOV,
    530 	 "CHINON  ", "CD-ROM CDS-431  ", ""},     PQUIRK_NOLUNS},
    531 	{{T_CDROM, T_REMOV,
    532 	 "CHINON  ", "CD-ROM CDS-435  ", ""},     PQUIRK_NOLUNS},
    533 	{{T_CDROM, T_REMOV,
    534 	 "Chinon  ", "CD-ROM CDS-525  ", ""},     PQUIRK_NOLUNS},
    535 	{{T_CDROM, T_REMOV,
    536 	 "CHINON  ", "CD-ROM CDS-535  ", ""},     PQUIRK_NOLUNS},
    537 	{{T_CDROM, T_REMOV,
    538 	 "DEC     ", "RRD42   (C) DEC ", ""},     PQUIRK_NOLUNS},
    539 	{{T_CDROM, T_REMOV,
    540 	 "DENON   ", "DRD-25X         ", "V"},    PQUIRK_NOLUNS},
    541 	{{T_CDROM, T_REMOV,
    542 	 "GENERIC ", "CRD-BP2         ", ""},     PQUIRK_NOLUNS},
    543 	{{T_CDROM, T_REMOV,
    544 	 "HP      ", "C4324/C4325     ", ""},     PQUIRK_NOLUNS},
    545 	{{T_CDROM, T_REMOV,
    546 	 "IMS     ", "CDD521/10       ", "2.06"}, PQUIRK_NOLUNS},
    547 	{{T_CDROM, T_REMOV,
    548 	 "MATSHITA", "CD-ROM CR-5XX   ", "1.0b"}, PQUIRK_NOLUNS},
    549 	{{T_CDROM, T_REMOV,
    550 	 "MEDAVIS ", "RENO CD-ROMX2A  ", ""},     PQUIRK_NOLUNS},
    551 	{{T_CDROM, T_REMOV,
    552 	 "MEDIAVIS", "CDR-H93MV       ", "1.3"},  PQUIRK_NOLUNS},
    553 	{{T_CDROM, T_REMOV,
    554 	 "NEC     ", "CD-ROM DRIVE:502", ""},     PQUIRK_NOLUNS},
    555 	{{T_CDROM, T_REMOV,
    556 	 "NEC     ", "CD-ROM DRIVE:55 ", ""},     PQUIRK_NOLUNS},
    557 	{{T_CDROM, T_REMOV,
    558 	 "NEC     ", "CD-ROM DRIVE:83 ", ""},     PQUIRK_NOLUNS},
    559 	{{T_CDROM, T_REMOV,
    560 	 "NEC     ", "CD-ROM DRIVE:84 ", ""},     PQUIRK_NOLUNS},
    561 	{{T_CDROM, T_REMOV,
    562 	 "NEC     ", "CD-ROM DRIVE:841", ""},     PQUIRK_NOLUNS},
    563         {{T_CDROM, T_REMOV,
    564 	 "OLYMPUS ", "CDS620E         ", "1.1d"},
    565 			       PQUIRK_NOLUNS|PQUIRK_NOSYNC|PQUIRK_NOCAPACITY},
    566 	{{T_CDROM, T_REMOV,
    567 	 "PIONEER ", "CD-ROM DR-124X  ", "1.01"}, PQUIRK_NOLUNS},
    568         {{T_CDROM, T_REMOV,
    569          "PLEXTOR ", "CD-ROM PX-4XCS  ", "1.01"},
    570                                PQUIRK_NOLUNS|PQUIRK_NOSYNC},
    571 	{{T_CDROM, T_REMOV,
    572 	 "SONY    ", "CD-ROM CDU-541  ", ""},     PQUIRK_NOLUNS},
    573 	{{T_CDROM, T_REMOV,
    574 	 "SONY    ", "CD-ROM CDU-55S  ", ""},     PQUIRK_NOLUNS},
    575 	{{T_CDROM, T_REMOV,
    576 	 "SONY    ", "CD-ROM CDU-561  ", ""},     PQUIRK_NOLUNS},
    577 	{{T_CDROM, T_REMOV,
    578 	 "SONY    ", "CD-ROM CDU-76S", ""},
    579 				PQUIRK_NOLUNS|PQUIRK_NOSYNC|PQUIRK_NOWIDE},
    580 	{{T_CDROM, T_REMOV,
    581 	 "SONY    ", "CD-ROM CDU-8003A", ""},     PQUIRK_NOLUNS},
    582 	{{T_CDROM, T_REMOV,
    583 	 "SONY    ", "CD-ROM CDU-8012 ", ""},     PQUIRK_NOLUNS},
    584 	{{T_CDROM, T_REMOV,
    585 	 "TEAC    ", "CD-ROM          ", "1.06"}, PQUIRK_NOLUNS},
    586 	{{T_CDROM, T_REMOV,
    587 	 "TEAC    ", "CD-ROM CD-56S   ", "1.0B"}, PQUIRK_NOLUNS},
    588 	{{T_CDROM, T_REMOV,
    589 	 "TEXEL   ", "CD-ROM          ", "1.06"}, PQUIRK_NOLUNS},
    590 	{{T_CDROM, T_REMOV,
    591 	 "TEXEL   ", "CD-ROM DM-XX24 K", "1.09"}, PQUIRK_NOLUNS},
    592 	{{T_CDROM, T_REMOV,
    593 	 "TEXEL   ", "CD-ROM DM-XX24 K", "1.10"}, PQUIRK_NOLUNS},
    594 	{{T_CDROM, T_REMOV,
    595 	 "TOSHIBA ", "XM-4101TASUNSLCD", ""}, PQUIRK_NOLUNS|PQUIRK_NOSYNC},
    596 	/* "IBM CDRM00201     !F" 0724 is an IBM OEM Toshiba XM-4101BME */
    597 	{{T_CDROM, T_REMOV,
    598 	 "IBM     ", "CDRM00201     !F", "0724"}, PQUIRK_NOLUNS|PQUIRK_NOSYNC},
    599 	{{T_CDROM, T_REMOV,
    600 	 "ShinaKen", "CD-ROM DM-3x1S",   "1.04"}, PQUIRK_NOLUNS},
    601 	{{T_CDROM, T_REMOV,
    602 	 "JVC     ", "R2626",            ""},     PQUIRK_NOLUNS},
    603 	{{T_CDROM, T_REMOV,
    604 	 "YAMAHA", "CRW8424S",           ""},     PQUIRK_NOLUNS},
    605 	{{T_CDROM, T_REMOV,
    606 	 "NEC     ", "CD-ROM DRIVE:222", ""},	  PQUIRK_NOLUNS|PQUIRK_NOSYNC},
    607 
    608 	{{T_DIRECT, T_FIXED,
    609 	 "MICROP  ", "1588-15MBSUN0669", ""},     PQUIRK_AUTOSAVE},
    610 	{{T_DIRECT, T_FIXED,
    611 	 "MICROP  ", "2217-15MQ1091501", ""},     PQUIRK_NOSYNCCACHE},
    612 	{{T_OPTICAL, T_REMOV,
    613 	 "EPSON   ", "OMD-5010        ", "3.08"}, PQUIRK_NOLUNS},
    614 	{{T_DIRECT, T_FIXED,
    615 	 "ADAPTEC ", "AEC-4412BD",       "1.2A"}, PQUIRK_NOMODESENSE},
    616 	{{T_DIRECT, T_FIXED,
    617 	 "ADAPTEC ", "ACB-4000",         ""},     PQUIRK_FORCELUNS|PQUIRK_AUTOSAVE|PQUIRK_NOMODESENSE},
    618 	{{T_DIRECT, T_FIXED,
    619 	 "DEC     ", "RZ55     (C) DEC", ""},     PQUIRK_AUTOSAVE},
    620 	{{T_DIRECT, T_FIXED,
    621 	 "EMULEX  ", "MD21/S2     ESDI", "A00"},
    622 				PQUIRK_FORCELUNS|PQUIRK_AUTOSAVE},
    623 	{{T_DIRECT, T_FIXED,
    624 	 "MICROP",  "1548-15MZ1077801",  "HZ2P"}, PQUIRK_NOTAG},
    625 	{{T_DIRECT, T_FIXED,
    626 	 "HP      ", "C372",             ""},     PQUIRK_NOTAG},
    627 	{{T_DIRECT, T_FIXED,
    628 	 "IBMRAID ", "0662S",		 ""},     PQUIRK_AUTOSAVE},
    629 	{{T_DIRECT, T_FIXED,
    630 	 "IBM     ", "0663H",		 ""},     PQUIRK_AUTOSAVE},
    631 	{{T_DIRECT, T_FIXED,
    632 	 "IBM",	     "0664",		 ""},     PQUIRK_AUTOSAVE},
    633 	{{T_DIRECT, T_FIXED,
    634 	/* improperly report DT-only sync mode */
    635 	 "IBM     ", "DXHS36D",		 ""},
    636 				PQUIRK_CAP_SYNC|PQUIRK_CAP_WIDE16},
    637 	{{T_DIRECT, T_FIXED,
    638 	 "IBM     ", "DXHS18Y",		 ""},
    639 				PQUIRK_CAP_SYNC|PQUIRK_CAP_WIDE16},
    640 	{{T_DIRECT, T_FIXED,
    641 	 "IBM     ", "H3171-S2",	 ""},
    642 				PQUIRK_NOLUNS|PQUIRK_AUTOSAVE},
    643 	{{T_DIRECT, T_FIXED,
    644 	 "IBM     ", "KZ-C",		 ""},	  PQUIRK_AUTOSAVE},
    645 	/* Broken IBM disk */
    646 	{{T_DIRECT, T_FIXED,
    647 	 ""	   , "DFRSS2F",		 ""},	  PQUIRK_AUTOSAVE},
    648 	{{T_DIRECT, T_FIXED,
    649 	 "Initio  ", "",		 ""},	  PQUIRK_NOBIGMODESENSE},
    650 	{{T_DIRECT, T_REMOV,
    651 	 "MPL     ", "MC-DISK-        ", ""},     PQUIRK_NOLUNS},
    652 	{{T_DIRECT, T_FIXED,
    653 	 "MAXTOR  ", "XT-3280         ", ""},     PQUIRK_NOLUNS},
    654 	{{T_DIRECT, T_FIXED,
    655 	 "MAXTOR  ", "XT-4380S        ", ""},     PQUIRK_NOLUNS},
    656 	{{T_DIRECT, T_FIXED,
    657 	 "MAXTOR  ", "MXT-1240S       ", ""},     PQUIRK_NOLUNS},
    658 	{{T_DIRECT, T_FIXED,
    659 	 "MAXTOR  ", "XT-4170S        ", ""},     PQUIRK_NOLUNS},
    660 	{{T_DIRECT, T_FIXED,
    661 	 "MAXTOR  ", "XT-8760S",         ""},     PQUIRK_NOLUNS},
    662 	{{T_DIRECT, T_FIXED,
    663 	 "MAXTOR  ", "LXT-213S        ", ""},     PQUIRK_NOLUNS},
    664 	{{T_DIRECT, T_FIXED,
    665 	 "MAXTOR  ", "LXT-213S SUN0207", ""},     PQUIRK_NOLUNS},
    666 	{{T_DIRECT, T_FIXED,
    667 	 "MAXTOR  ", "LXT-200S        ", ""},     PQUIRK_NOLUNS},
    668 	{{T_DIRECT, T_FIXED,
    669 	 "MEGADRV ", "EV1000",           ""},     PQUIRK_NOMODESENSE},
    670 	{{T_DIRECT, T_FIXED,
    671 	 "MICROP", "1991-27MZ",          ""},     PQUIRK_NOTAG},
    672 	{{T_DIRECT, T_FIXED,
    673 	 "MST     ", "SnapLink        ", ""},     PQUIRK_NOLUNS},
    674 	{{T_DIRECT, T_FIXED,
    675 	 "NEC     ", "D3847           ", "0307"}, PQUIRK_NOLUNS},
    676 	{{T_DIRECT, T_FIXED,
    677 	 "QUANTUM ", "ELS85S          ", ""},     PQUIRK_AUTOSAVE},
    678 	{{T_DIRECT, T_FIXED,
    679 	 "QUANTUM ", "LPS525S         ", ""},     PQUIRK_NOLUNS},
    680 	{{T_DIRECT, T_FIXED,
    681 	 "QUANTUM ", "P105S 910-10-94x", ""},     PQUIRK_NOLUNS},
    682 	{{T_DIRECT, T_FIXED,
    683 	 "QUANTUM ", "PD1225S         ", ""},     PQUIRK_NOLUNS},
    684 	{{T_DIRECT, T_FIXED,
    685 	 "QUANTUM ", "PD210S   SUN0207", ""},     PQUIRK_NOLUNS},
    686 	{{T_DIRECT, T_FIXED,
    687 	 "QUANTUM ", "ATLAS IV 9 WLS", "0A0A"},   PQUIRK_CAP_NODT},
    688 	{{T_DIRECT, T_FIXED,
    689 	 "RODIME  ", "RO3000S         ", ""},     PQUIRK_NOLUNS},
    690 	{{T_DIRECT, T_FIXED,
    691 	 "SEAGATE ", "ST125N          ", ""},     PQUIRK_NOLUNS},
    692 	{{T_DIRECT, T_FIXED,
    693 	 "SEAGATE ", "ST157N          ", ""},     PQUIRK_NOLUNS},
    694 	{{T_DIRECT, T_FIXED,
    695 	 "SEAGATE ", "ST296           ", ""},     PQUIRK_NOLUNS},
    696 	{{T_DIRECT, T_FIXED,
    697 	 "SEAGATE ", "ST296N          ", ""},     PQUIRK_NOLUNS},
    698 	{{T_DIRECT, T_FIXED,
    699 	 "SEAGATE ", "ST318404LC      ", ""},     PQUIRK_NOLUNS},
    700 	{{T_DIRECT, T_FIXED,
    701 	 "SEAGATE ", "ST39236LC       ", ""},     PQUIRK_NOLUNS},
    702 	{{T_DIRECT, T_FIXED,
    703 	 "SEAGATE ", "ST15150N        ", ""},     PQUIRK_NOTAG},
    704 	{{T_DIRECT, T_FIXED,
    705 	 "SEAGATE ", "ST19171",          ""},     PQUIRK_NOMODESENSE},
    706 	{{T_DIRECT, T_FIXED,
    707 	 "SEAGATE ", "ST32430N",         ""},     PQUIRK_CAP_SYNC},
    708 	{{T_DIRECT, T_FIXED,
    709 	 "SEAGATE ", "ST34501FC       ", ""},     PQUIRK_NOMODESENSE},
    710 	{{T_DIRECT, T_FIXED,
    711 	 "SEAGATE ", "SX910800N",        ""},     PQUIRK_NOTAG},
    712 	{{T_DIRECT, T_FIXED,
    713 	 "TOSHIBA ", "MK538FB         ", "6027"}, PQUIRK_NOLUNS},
    714 	{{T_DIRECT, T_FIXED,
    715 	 "MICROP  ", "1924",          ""},     PQUIRK_CAP_SYNC},
    716 	{{T_DIRECT, T_FIXED,
    717 	 "FUJITSU ", "M2266",         ""},     PQUIRK_CAP_SYNC},
    718 	{{T_DIRECT, T_FIXED,
    719 	 "FUJITSU ", "M2624S-512      ", ""},     PQUIRK_CAP_SYNC},
    720 	{{T_DIRECT, T_FIXED,
    721 	 "SEAGATE ", "SX336704LC"   , ""}, PQUIRK_CAP_SYNC | PQUIRK_CAP_WIDE16},
    722 	{{T_DIRECT, T_FIXED,
    723 	 "SEAGATE ", "SX173404LC",       ""},     PQUIRK_CAP_SYNC | PQUIRK_CAP_WIDE16},
    724 
    725 	{{T_DIRECT, T_REMOV,
    726 	 "IOMEGA", "ZIP 100",		 "J.03"}, PQUIRK_NOLUNS|PQUIRK_NOSYNC},
    727 	{{T_DIRECT, T_REMOV,
    728 	 "INSITE", "I325VM",             ""},     PQUIRK_NOLUNS},
    729 
    730 	/* XXX: QIC-36 tape behind Emulex adapter.  Very broken. */
    731 	{{T_SEQUENTIAL, T_REMOV,
    732 	 "        ", "                ", "    "}, PQUIRK_NOLUNS},
    733 	{{T_SEQUENTIAL, T_REMOV,
    734 	 "EMULEX  ", "MT-02 QIC       ", ""},     PQUIRK_NOLUNS},
    735 	{{T_SEQUENTIAL, T_REMOV,
    736 	 "CALIPER ", "CP150           ", ""},     PQUIRK_NOLUNS},
    737 	{{T_SEQUENTIAL, T_REMOV,
    738 	 "EXABYTE ", "EXB-8200        ", ""},     PQUIRK_NOLUNS},
    739 	{{T_SEQUENTIAL, T_REMOV,
    740 	 "SONY    ", "GY-10C          ", ""},     PQUIRK_NOLUNS},
    741 	{{T_SEQUENTIAL, T_REMOV,
    742 	 "SONY    ", "SDT-2000        ", "2.09"}, PQUIRK_NOLUNS},
    743 	{{T_SEQUENTIAL, T_REMOV,
    744 	 "SONY    ", "SDT-5000        ", "3."},   PQUIRK_NOSYNC|PQUIRK_NOWIDE},
    745 	{{T_SEQUENTIAL, T_REMOV,
    746 	 "SONY    ", "SDT-5200        ", "3."},   PQUIRK_NOLUNS},
    747 	{{T_SEQUENTIAL, T_REMOV,
    748 	 "TANDBERG", " TDC 3600       ", ""},     PQUIRK_NOLUNS},
    749 	/* Following entry reported as a Tandberg 3600; ref. PR1933 */
    750 	{{T_SEQUENTIAL, T_REMOV,
    751 	 "ARCHIVE ", "VIPER 150  21247", ""},     PQUIRK_NOLUNS},
    752 	/* Following entry for a Cipher ST150S; ref. PR4171 */
    753 	{{T_SEQUENTIAL, T_REMOV,
    754 	 "ARCHIVE ", "VIPER 1500 21247", "2.2G"}, PQUIRK_NOLUNS},
    755 	{{T_SEQUENTIAL, T_REMOV,
    756 	 "ARCHIVE ", "Python 28454-XXX", ""},     PQUIRK_NOLUNS},
    757 	{{T_SEQUENTIAL, T_REMOV,
    758 	 "WANGTEK ", "5099ES SCSI",      ""},     PQUIRK_NOLUNS},
    759 	{{T_SEQUENTIAL, T_REMOV,
    760 	 "WANGTEK ", "5150ES SCSI",      ""},     PQUIRK_NOLUNS},
    761 	{{T_SEQUENTIAL, T_REMOV,
    762 	 "WANGTEK ", "SCSI-36",		 ""},     PQUIRK_NOLUNS},
    763 	{{T_SEQUENTIAL, T_REMOV,
    764 	 "WangDAT ", "Model 1300      ", "02.4"}, PQUIRK_NOSYNC|PQUIRK_NOWIDE},
    765 	{{T_SEQUENTIAL, T_REMOV,
    766 	 "WangDAT ", "Model 2600      ", "01.7"}, PQUIRK_NOSYNC|PQUIRK_NOWIDE},
    767 	{{T_SEQUENTIAL, T_REMOV,
    768 	 "WangDAT ", "Model 3200      ", "02.2"}, PQUIRK_NOSYNC|PQUIRK_NOWIDE},
    769 	{{T_SEQUENTIAL, T_REMOV,
    770 	 "TEAC    ", "MT-2ST/N50      ", ""},     PQUIRK_NOLUNS},
    771 
    772 	{{T_SCANNER, T_FIXED,
    773 	 "RICOH   ", "IS60            ", "1R08"}, PQUIRK_NOLUNS},
    774 	{{T_SCANNER, T_FIXED,
    775 	 "UMAX    ", "Astra 1200S     ", "V2.9"}, PQUIRK_NOLUNS},
    776 	{{T_SCANNER, T_FIXED,
    777 	 "UMAX    ", "Astra 1220S     ", ""},     PQUIRK_NOLUNS},
    778 	{{T_SCANNER, T_FIXED,
    779 	 "UMAX    ", "UMAX S-6E       ", "V2.0"}, PQUIRK_NOLUNS},
    780 	{{T_SCANNER, T_FIXED,
    781 	 "UMAX    ", "UMAX S-12       ", "V2.1"}, PQUIRK_NOLUNS},
    782 	{{T_SCANNER, T_FIXED,
    783 	 "ULTIMA  ", "A6000C          ", ""},     PQUIRK_NOLUNS},
    784 	{{T_PROCESSOR, T_FIXED,
    785 	 "ESG-SHV",  "SCA HSBP M15",     ""},     PQUIRK_NOLUNS},
    786 	{{T_PROCESSOR, T_FIXED,
    787 	 "SYMBIOS",  "",                 ""},     PQUIRK_NOLUNS},
    788 	{{T_PROCESSOR, T_FIXED,
    789 	 "LITRONIC", "PCMCIA          ", ""},     PQUIRK_NOLUNS},
    790 	{{T_CHANGER, T_REMOV,
    791 	 "SONY    ", "CDL1100         ", ""},     PQUIRK_NOLUNS},
    792 	{{T_ENCLOSURE, T_FIXED,
    793 	 "SUN     ", "SENA            ", ""},     PQUIRK_NOLUNS},
    794 };
    795 
    796 /*
    797  * given a target and lun, ask the device what
    798  * it is, and find the correct driver table
    799  * entry.
    800  */
    801 static int
    802 scsi_probe_device(struct scsibus_softc *sc, int target, int lun)
    803 {
    804 	struct scsipi_channel *chan = sc->sc_channel;
    805 	struct scsipi_periph *periph;
    806 	struct scsipi_inquiry_data inqbuf;
    807 	const struct scsi_quirk_inquiry_pattern *finger;
    808 	int checkdtype, priority, docontinue, quirks;
    809 	struct scsipibus_attach_args sa;
    810 	cfdata_t cf;
    811 	int locs[SCSIBUSCF_NLOCS];
    812 
    813 	/*
    814 	 * Assume no more luns to search after this one.
    815 	 * If we successfully get Inquiry data and after
    816 	 * merging quirks we find we can probe for more
    817 	 * luns, we will.
    818 	 */
    819 	docontinue = 0;
    820 
    821 	/* Skip this slot if it is already attached. */
    822 	if (scsipi_lookup_periph(chan, target, lun) != NULL)
    823 		return (docontinue);
    824 
    825 	periph = scsipi_alloc_periph(M_NOWAIT);
    826 	if (periph == NULL) {
    827 #ifdef	DIAGNOSTIC
    828 		aprint_error_dev(sc->sc_dev,
    829 		    "cannot allocate periph for target %d lun %d\n",
    830 		    target, lun);
    831 #endif
    832 		return (ENOMEM);
    833 	}
    834 	periph->periph_channel = chan;
    835 	periph->periph_switch = &scsi_probe_dev;
    836 
    837 	periph->periph_target = target;
    838 	periph->periph_lun = lun;
    839 	periph->periph_quirks = chan->chan_defquirks;
    840 
    841 #ifdef SCSIPI_DEBUG
    842 	if (SCSIPI_DEBUG_TYPE == SCSIPI_BUSTYPE_SCSI &&
    843 	    SCSIPI_DEBUG_TARGET == target &&
    844 	    SCSIPI_DEBUG_LUN == lun)
    845 		periph->periph_dbflags |= SCSIPI_DEBUG_FLAGS;
    846 #endif
    847 
    848 	/*
    849 	 * Ask the device what it is
    850 	 */
    851 
    852 #ifdef SCSI_2_DEF
    853 	/* some devices need to be told to go to SCSI2 */
    854 	/* However some just explode if you tell them this.. leave it out */
    855 	scsi_change_def(periph, XS_CTL_DISCOVERY | XS_CTL_SILENT);
    856 #endif /* SCSI_2_DEF */
    857 
    858 	/* Now go ask the device all about itself. */
    859 	memset(&inqbuf, 0, sizeof(inqbuf));
    860 	{
    861 		u_int8_t *extension = &inqbuf.flags1;
    862 		int len = 0;
    863 		while (len < 3)
    864 			extension[len++] = '\0';
    865 		while (len < 3 + 28)
    866 			extension[len++] = ' ';
    867 		while (len < 3 + 28 + 20)
    868 			extension[len++] = '\0';
    869 		while (len < 3 + 28 + 20 + 1)
    870 			extension[len++] = '\0';
    871 		while (len < 3 + 28 + 20 + 1 + 1)
    872 			extension[len++] = '\0';
    873 		while (len < 3 + 28 + 20 + 1 + 1 + (8*2))
    874 			extension[len++] = ' ';
    875 	}
    876 	if (scsipi_inquire(periph, &inqbuf, XS_CTL_DISCOVERY | XS_CTL_SILENT))
    877 		goto bad;
    878 
    879 	periph->periph_type = inqbuf.device & SID_TYPE;
    880 	if (inqbuf.dev_qual2 & SID_REMOVABLE)
    881 		periph->periph_flags |= PERIPH_REMOVABLE;
    882 	periph->periph_version = inqbuf.version & SID_ANSII;
    883 
    884 	/*
    885 	 * Any device qualifier that has the top bit set (qualifier&4 != 0)
    886 	 * is vendor specific and won't match in this switch.
    887 	 * All we do here is throw out bad/negative responses.
    888 	 */
    889 	checkdtype = 0;
    890 	switch (inqbuf.device & SID_QUAL) {
    891 	case SID_QUAL_LU_PRESENT:
    892 		checkdtype = 1;
    893 		break;
    894 
    895 	case SID_QUAL_LU_NOTPRESENT:
    896 	case SID_QUAL_reserved:
    897 	case SID_QUAL_LU_NOT_SUPP:
    898 		goto bad;
    899 
    900 	default:
    901 		break;
    902 	}
    903 
    904 	/* Let the adapter driver handle the device separatley if it wants. */
    905 	if (chan->chan_adapter->adapt_accesschk != NULL &&
    906 	    (*chan->chan_adapter->adapt_accesschk)(periph, &sa.sa_inqbuf))
    907 		goto bad;
    908 
    909 	if (checkdtype) {
    910 		switch (periph->periph_type) {
    911 		case T_DIRECT:
    912 		case T_SEQUENTIAL:
    913 		case T_PRINTER:
    914 		case T_PROCESSOR:
    915 		case T_WORM:
    916 		case T_CDROM:
    917 		case T_SCANNER:
    918 		case T_OPTICAL:
    919 		case T_CHANGER:
    920 		case T_COMM:
    921 		case T_IT8_1:
    922 		case T_IT8_2:
    923 		case T_STORARRAY:
    924 		case T_ENCLOSURE:
    925 		case T_SIMPLE_DIRECT:
    926 		case T_OPTIC_CARD_RW:
    927 		case T_OBJECT_STORED:
    928 		default:
    929 			break;
    930 		case T_NODEVICE:
    931 			goto bad;
    932 		}
    933 	}
    934 
    935 	sa.sa_periph = periph;
    936 	sa.sa_inqbuf.type = inqbuf.device;
    937 	sa.sa_inqbuf.removable = inqbuf.dev_qual2 & SID_REMOVABLE ?
    938 	    T_REMOV : T_FIXED;
    939 	sa.sa_inqbuf.vendor = inqbuf.vendor;
    940 	sa.sa_inqbuf.product = inqbuf.product;
    941 	sa.sa_inqbuf.revision = inqbuf.revision;
    942 	sa.scsipi_info.scsi_version = inqbuf.version;
    943 	sa.sa_inqptr = &inqbuf;
    944 
    945 	finger = scsipi_inqmatch(
    946 	    &sa.sa_inqbuf, scsi_quirk_patterns,
    947 	    sizeof(scsi_quirk_patterns)/sizeof(scsi_quirk_patterns[0]),
    948 	    sizeof(scsi_quirk_patterns[0]), &priority);
    949 
    950 	if (finger != NULL)
    951 		quirks = finger->quirks;
    952 	else
    953 		quirks = 0;
    954 
    955 	/*
    956 	 * Determine the operating mode capabilities of the device.
    957 	 */
    958 	if (periph->periph_version >= 2) {
    959 		if ((inqbuf.flags3 & SID_CmdQue) != 0 &&
    960 		    (quirks & PQUIRK_NOTAG) == 0)
    961 			periph->periph_cap |= PERIPH_CAP_TQING;
    962 		if ((inqbuf.flags3 & SID_Linked) != 0)
    963 			periph->periph_cap |= PERIPH_CAP_LINKCMDS;
    964 		if ((inqbuf.flags3 & SID_Sync) != 0 &&
    965 		    (quirks & PQUIRK_NOSYNC) == 0)
    966 			periph->periph_cap |= PERIPH_CAP_SYNC;
    967 		if ((inqbuf.flags3 & SID_WBus16) != 0 &&
    968 		    (quirks & PQUIRK_NOWIDE) == 0)
    969 			periph->periph_cap |= PERIPH_CAP_WIDE16;
    970 		if ((inqbuf.flags3 & SID_WBus32) != 0 &&
    971 		    (quirks & PQUIRK_NOWIDE) == 0)
    972 			periph->periph_cap |= PERIPH_CAP_WIDE32;
    973 		if ((inqbuf.flags3 & SID_SftRe) != 0)
    974 			periph->periph_cap |= PERIPH_CAP_SFTRESET;
    975 		if ((inqbuf.flags3 & SID_RelAdr) != 0)
    976 			periph->periph_cap |= PERIPH_CAP_RELADR;
    977 		/* SPC-2 */
    978 		if (periph->periph_version >= 3 &&
    979 		    !(quirks & PQUIRK_CAP_NODT)){
    980 			/*
    981 			 * Report ST clocking though CAP_WIDExx/CAP_SYNC.
    982 			 * If the device only supports DT, clear these
    983 			 * flags (DT implies SYNC and WIDE)
    984 			 */
    985 			switch (inqbuf.flags4 & SID_Clocking) {
    986 			case SID_CLOCKING_DT_ONLY:
    987 				periph->periph_cap &=
    988 				    ~(PERIPH_CAP_SYNC |
    989 				      PERIPH_CAP_WIDE16 |
    990 				      PERIPH_CAP_WIDE32);
    991 				/* FALLTHROUGH */
    992 			case SID_CLOCKING_SD_DT:
    993 				periph->periph_cap |= PERIPH_CAP_DT;
    994 				break;
    995 			default: /* ST only or invalid */
    996 				/* nothing to do */
    997 				break;
    998 			}
    999 		}
   1000 		if (periph->periph_version >= 3) {
   1001 			if (inqbuf.flags4 & SID_IUS)
   1002 				periph->periph_cap |= PERIPH_CAP_IUS;
   1003 			if (inqbuf.flags4 & SID_QAS)
   1004 				periph->periph_cap |= PERIPH_CAP_QAS;
   1005 		}
   1006 	}
   1007 	if (quirks & PQUIRK_CAP_SYNC)
   1008 		periph->periph_cap |= PERIPH_CAP_SYNC;
   1009 	if (quirks & PQUIRK_CAP_WIDE16)
   1010 		periph->periph_cap |= PERIPH_CAP_WIDE16;
   1011 
   1012 	/*
   1013 	 * Now apply any quirks from the table.
   1014 	 */
   1015 	periph->periph_quirks |= quirks;
   1016 	if (periph->periph_version == 0 &&
   1017 	    (periph->periph_quirks & PQUIRK_FORCELUNS) == 0)
   1018 		periph->periph_quirks |= PQUIRK_NOLUNS;
   1019 
   1020 	if ((periph->periph_quirks & PQUIRK_NOLUNS) == 0)
   1021 		docontinue = 1;
   1022 
   1023 	locs[SCSIBUSCF_TARGET] = target;
   1024 	locs[SCSIBUSCF_LUN] = lun;
   1025 
   1026 	if ((cf = config_search_loc(config_stdsubmatch, sc->sc_dev,
   1027 	     "scsibus", locs, &sa)) != NULL) {
   1028 		scsipi_insert_periph(chan, periph);
   1029 		/*
   1030 		 * XXX Can't assign periph_dev here, because we'll
   1031 		 * XXX need it before config_attach() returns.  Must
   1032 		 * XXX assign it in periph driver.
   1033 		 */
   1034 		config_attach_loc(sc->sc_dev, cf, locs, &sa,
   1035 					 scsibusprint);
   1036 	} else {
   1037 		scsibusprint(&sa, device_xname(sc->sc_dev));
   1038 		aprint_normal(" not configured\n");
   1039 		goto bad;
   1040 	}
   1041 
   1042 	return (docontinue);
   1043 
   1044 bad:
   1045 	scsipi_free_periph(periph);
   1046 	return (docontinue);
   1047 }
   1048 
   1049 /****** Entry points for user control of the SCSI bus. ******/
   1050 
   1051 static int
   1052 scsibusopen(dev_t dev, int flag, int fmt,
   1053     struct lwp *l)
   1054 {
   1055 	struct scsibus_softc *sc;
   1056 	int error, unit = minor(dev);
   1057 
   1058 	sc = device_lookup_private(&scsibus_cd, unit);
   1059 	if (sc == NULL)
   1060 		return (ENXIO);
   1061 
   1062 	if (sc->sc_flags & SCSIBUSF_OPEN)
   1063 		return (EBUSY);
   1064 
   1065 	if ((error = scsipi_adapter_addref(sc->sc_channel->chan_adapter)) != 0)
   1066 		return (error);
   1067 
   1068 	sc->sc_flags |= SCSIBUSF_OPEN;
   1069 
   1070 	return (0);
   1071 }
   1072 
   1073 static int
   1074 scsibusclose(dev_t dev, int flag, int fmt,
   1075     struct lwp *l)
   1076 {
   1077 	struct scsibus_softc *sc;
   1078 
   1079 	sc = device_lookup_private(&scsibus_cd, minor(dev));
   1080 	scsipi_adapter_delref(sc->sc_channel->chan_adapter);
   1081 
   1082 	sc->sc_flags &= ~SCSIBUSF_OPEN;
   1083 
   1084 	return (0);
   1085 }
   1086 
   1087 static int
   1088 scsibusioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
   1089 {
   1090 	struct scsibus_softc *sc;
   1091 	struct scsipi_channel *chan;
   1092 	int error;
   1093 
   1094 	sc = device_lookup_private(&scsibus_cd, minor(dev));
   1095 	chan = sc->sc_channel;
   1096 
   1097 	/*
   1098 	 * Enforce write permission for ioctls that change the
   1099 	 * state of the bus.  Host adapter specific ioctls must
   1100 	 * be checked by the adapter driver.
   1101 	 */
   1102 	switch (cmd) {
   1103 	case SCBUSIOSCAN:
   1104 	case SCBUSIODETACH:
   1105 	case SCBUSIORESET:
   1106 		if ((flag & FWRITE) == 0)
   1107 			return (EBADF);
   1108 	}
   1109 
   1110 	switch (cmd) {
   1111 	case SCBUSIOSCAN:
   1112 	    {
   1113 		struct scbusioscan_args *a =
   1114 		    (struct scbusioscan_args *)addr;
   1115 
   1116 		error = scsi_probe_bus(sc, a->sa_target, a->sa_lun);
   1117 		break;
   1118 	    }
   1119 
   1120 	case SCBUSIODETACH:
   1121 	    {
   1122 		struct scbusiodetach_args *a =
   1123 		    (struct scbusiodetach_args *)addr;
   1124 
   1125 		error = scsipi_target_detach(chan, a->sa_target, a->sa_lun, 0);
   1126 		break;
   1127 	    }
   1128 
   1129 
   1130 	case SCBUSIORESET:
   1131 		/* FALLTHROUGH */
   1132 	default:
   1133 		error = scsipi_adapter_ioctl(chan, cmd, addr, flag, l->l_proc);
   1134 		break;
   1135 	}
   1136 
   1137 	return (error);
   1138 }
   1139