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