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