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