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