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