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