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