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