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