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