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