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