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