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