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