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