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