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