scsiconf.c revision 1.298 1 /* $NetBSD: scsiconf.c,v 1.298 2022/02/05 17:32:59 hannken 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.298 2022/02/05 17:32:59 hannken 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 memset(&replun, 0, sizeof(replun));
403
404 periph = scsipi_alloc_periph(M_WAITOK);
405 periph->periph_channel = chan;
406 periph->periph_switch = &scsi_probe_dev;
407
408 periph->periph_target = target;
409 periph->periph_lun = 0;
410 periph->periph_quirks = chan->chan_defquirks;
411
412 if ((error = scsipi_inquire(periph, &inqbuf,
413 XS_CTL_DISCOVERY | XS_CTL_SILENT)))
414 goto end2;
415 periph->periph_version = inqbuf.version & SID_ANSII;
416 if (periph->periph_version < 3) {
417 error = ENOTSUP;
418 goto end2;
419 }
420
421 rlrlen = sizeof(*rlr) + sizeof(*lunp) * 1;
422
423 again:
424 rlr = kmem_zalloc(rlrlen, KM_SLEEP);
425
426 replun.opcode = SCSI_REPORT_LUNS;
427 replun.selectreport = SELECTREPORT_NORMAL;
428 _lto4b(rlrlen, replun.alloclen);
429
430 error = scsipi_command(periph, (void *)&replun, sizeof(replun),
431 (void *)rlr, rlrlen, SCSIPIRETRIES, 10000, NULL,
432 XS_CTL_DATA_IN | XS_CTL_DISCOVERY | XS_CTL_SILENT);
433 if (error)
434 goto end;
435
436 if (sizeof(*rlr) + _4btol(rlr->length) > rlrlen &&
437 sizeof(*rlr) + _4btol(rlr->length) <= 32) {
438 const size_t old_rlrlen = rlrlen;
439 rlrlen = sizeof(*rlr) + uimin(_4btol(rlr->length),
440 16383 * sizeof(*lunp));
441 kmem_free(rlr, old_rlrlen);
442 rlr = NULL;
443 goto again;
444 }
445
446 KASSERT(nluns != NULL);
447 *nluns = (rlrlen - sizeof(*rlr)) / sizeof(*lunp);
448
449 KASSERT(luns != NULL);
450 *luns = kmem_alloc(*nluns * sizeof(**luns), KM_SLEEP);
451
452 for (i = 0; i < *nluns; i++) {
453 lunp = &((struct scsi_report_luns_lun *)&rlr[1])[i];
454 switch (lunp->lun[0] & 0xC0) {
455 default:
456 scsi_print_addr(periph);
457 printf("LUN %016"PRIx64" ignored\n", _8btol(lunp->lun));
458 (*luns)[i] = 0;
459 break;
460 case 0x40:
461 (*luns)[i] = _2btol(&lunp->lun[0]) & 0x3FFF;
462 break;
463 case 0x00:
464 (*luns)[i] = _2btol(&lunp->lun[0]) & 0x00FF;
465 break;
466 }
467 }
468
469 kheapsort(*luns, *nluns, sizeof(**luns), lun_compar, &tmp);
470
471 end:
472 if (rlr)
473 kmem_free(rlr, rlrlen);
474 end2:
475 scsipi_free_periph(periph);
476 return error;
477 }
478
479 static void
480 scsi_discover_luns(struct scsibus_softc *sc, int target, int minlun, int maxlun)
481 {
482 uint16_t *luns = NULL; /* XXX gcc */
483 size_t nluns = 0; /* XXX gcc */
484
485 if (scsi_report_luns(sc, target, &luns, &nluns) == 0) {
486 for (size_t i = 0; i < nluns; i++)
487 if (luns[i] >= minlun && luns[i] <= maxlun)
488 scsi_probe_device(sc, target, luns[i]);
489 kmem_free(luns, sizeof(*luns) * nluns);
490 return;
491 }
492
493 for (int lun = minlun; lun <= maxlun; lun++) {
494 /*
495 * See if there's a device present, and configure it.
496 */
497 if (scsi_probe_device(sc, target, lun) == 0)
498 break;
499 /* otherwise something says we should look further */
500 }
501 }
502
503 /*
504 * Probe the requested scsi bus. It must be already set up.
505 * target and lun optionally narrow the search if not -1
506 */
507 int
508 scsi_probe_bus(struct scsibus_softc *sc, int target, int lun)
509 {
510 struct scsipi_channel *chan = sc->sc_channel;
511 int maxtarget, mintarget, maxlun, minlun;
512 int error;
513
514 if (target == -1) {
515 maxtarget = chan->chan_ntargets - 1;
516 mintarget = 0;
517 } else {
518 if (target < 0 || target >= chan->chan_ntargets)
519 return (EINVAL);
520 maxtarget = mintarget = target;
521 }
522
523 if (lun == -1) {
524 maxlun = chan->chan_nluns - 1;
525 minlun = 0;
526 } else {
527 if (lun < 0 || lun >= chan->chan_nluns)
528 return (EINVAL);
529 maxlun = minlun = lun;
530 }
531
532 /*
533 * Some HBAs provide an abstracted view of the bus; give them an
534 * opportunity to re-scan it before we do.
535 */
536 scsipi_adapter_ioctl(chan, SCBUSIOLLSCAN, NULL, 0, curproc);
537
538 if ((error = scsipi_adapter_addref(chan->chan_adapter)) != 0)
539 goto ret;
540 for (target = mintarget; target <= maxtarget; target++) {
541 if (target == chan->chan_id)
542 continue;
543
544 scsi_discover_luns(sc, target, minlun, maxlun);
545
546 /*
547 * Now that we've discovered all of the LUNs on this
548 * I_T Nexus, update the xfer mode for all of them
549 * that we know about.
550 */
551 scsipi_set_xfer_mode(chan, target, 1);
552 }
553
554 scsipi_adapter_delref(chan->chan_adapter);
555 ret:
556 return (error);
557 }
558
559 static int
560 scsibusrescan(device_t sc, const char *ifattr, const int *locators)
561 {
562
563 KASSERT(ifattr && !strcmp(ifattr, "scsibus"));
564 KASSERT(locators);
565
566 return (scsi_probe_bus(device_private(sc),
567 locators[SCSIBUSCF_TARGET], locators[SCSIBUSCF_LUN]));
568 }
569
570 static void
571 scsidevdetached(device_t self, device_t child)
572 {
573 struct scsibus_softc *sc = device_private(self);
574 struct scsipi_channel *chan = sc->sc_channel;
575 struct scsipi_periph *periph;
576 int target, lun;
577
578 target = device_locator(child, SCSIBUSCF_TARGET);
579 lun = device_locator(child, SCSIBUSCF_LUN);
580
581 mutex_enter(chan_mtx(chan));
582
583 periph = scsipi_lookup_periph_locked(chan, target, lun);
584 KASSERT(periph != NULL && periph->periph_dev == child);
585
586 scsipi_remove_periph(chan, periph);
587 scsipi_free_periph(periph);
588
589 mutex_exit(chan_mtx(chan));
590 }
591
592 /*
593 * Print out autoconfiguration information for a subdevice.
594 *
595 * This is a slight abuse of 'standard' autoconfiguration semantics,
596 * because 'print' functions don't normally print the colon and
597 * device information. However, in this case that's better than
598 * either printing redundant information before the attach message,
599 * or having the device driver call a special function to print out
600 * the standard device information.
601 */
602 static int
603 scsibusprint(void *aux, const char *pnp)
604 {
605 struct scsipibus_attach_args *sa = aux;
606 struct scsipi_inquiry_pattern *inqbuf;
607 u_int8_t type;
608 const char *dtype;
609 char vendor[33], product[65], revision[17];
610 int target, lun;
611
612 if (pnp != NULL)
613 aprint_normal("%s", pnp);
614
615 inqbuf = &sa->sa_inqbuf;
616
617 target = sa->sa_periph->periph_target;
618 lun = sa->sa_periph->periph_lun;
619 type = inqbuf->type & SID_TYPE;
620
621 dtype = scsipi_dtype(type);
622
623 strnvisx(vendor, sizeof(vendor), inqbuf->vendor, 8,
624 VIS_TRIM|VIS_SAFE|VIS_OCTAL);
625 strnvisx(product, sizeof(product), inqbuf->product, 16,
626 VIS_TRIM|VIS_SAFE|VIS_OCTAL);
627 strnvisx(revision, sizeof(revision), inqbuf->revision, 4,
628 VIS_TRIM|VIS_SAFE|VIS_OCTAL);
629
630 aprint_normal(" target %d lun %d: <%s, %s, %s> %s %s%s",
631 target, lun, vendor, product, revision, dtype,
632 inqbuf->removable ? "removable" : "fixed",
633 (sa->sa_periph->periph_opcs != NULL)
634 ? " timeout-info" : "");
635
636 return (UNCONF);
637 }
638
639 static const struct scsi_quirk_inquiry_pattern scsi_quirk_patterns[] = {
640 {{T_DIRECT, T_REMOV,
641 "Apple ", "iPod ", ""}, PQUIRK_START},
642 {{T_CDROM, T_REMOV,
643 "CHINON ", "CD-ROM CDS-431 ", ""}, PQUIRK_NOLUNS},
644 {{T_CDROM, T_REMOV,
645 "CHINON ", "CD-ROM CDS-435 ", ""}, PQUIRK_NOLUNS},
646 {{T_CDROM, T_REMOV,
647 "Chinon ", "CD-ROM CDS-525 ", ""}, PQUIRK_NOLUNS},
648 {{T_CDROM, T_REMOV,
649 "CHINON ", "CD-ROM CDS-535 ", ""}, PQUIRK_NOLUNS},
650 {{T_CDROM, T_REMOV,
651 "DEC ", "RRD42 (C) DEC ", ""}, PQUIRK_NOLUNS},
652 {{T_CDROM, T_REMOV,
653 "DENON ", "DRD-25X ", "V"}, PQUIRK_NOLUNS},
654 {{T_CDROM, T_REMOV,
655 "GENERIC ", "CRD-BP2 ", ""}, PQUIRK_NOLUNS},
656 {{T_CDROM, T_REMOV,
657 "HP ", "C4324/C4325 ", ""}, PQUIRK_NOLUNS},
658 {{T_CDROM, T_REMOV,
659 "IMS ", "CDD521/10 ", "2.06"}, PQUIRK_NOLUNS},
660 {{T_CDROM, T_REMOV,
661 "MATSHITA", "CD-ROM CR-5XX ", "1.0b"}, PQUIRK_NOLUNS},
662 {{T_CDROM, T_REMOV,
663 "MEDAVIS ", "RENO CD-ROMX2A ", ""}, PQUIRK_NOLUNS},
664 {{T_CDROM, T_REMOV,
665 "MEDIAVIS", "CDR-H93MV ", "1.3"}, PQUIRK_NOLUNS},
666 {{T_CDROM, T_REMOV,
667 "NEC ", "CD-ROM DRIVE:502", ""}, PQUIRK_NOLUNS},
668 {{T_CDROM, T_REMOV,
669 "NEC ", "CD-ROM DRIVE:55 ", ""}, PQUIRK_NOLUNS},
670 {{T_CDROM, T_REMOV,
671 "NEC ", "CD-ROM DRIVE:83 ", ""}, PQUIRK_NOLUNS},
672 {{T_CDROM, T_REMOV,
673 "NEC ", "CD-ROM DRIVE:84 ", ""}, PQUIRK_NOLUNS},
674 {{T_CDROM, T_REMOV,
675 "NEC ", "CD-ROM DRIVE:841", ""}, PQUIRK_NOLUNS},
676 {{T_CDROM, T_REMOV,
677 "OLYMPUS ", "CDS620E ", "1.1d"},
678 PQUIRK_NOLUNS|PQUIRK_NOSYNC|PQUIRK_NOCAPACITY},
679 {{T_CDROM, T_REMOV,
680 "PIONEER ", "CD-ROM DR-124X ", "1.01"}, PQUIRK_NOLUNS},
681 {{T_CDROM, T_REMOV,
682 "PLEXTOR ", "CD-ROM PX-4XCS ", "1.01"},
683 PQUIRK_NOLUNS|PQUIRK_NOSYNC},
684 {{T_CDROM, T_REMOV,
685 "SONY ", "CD-ROM CDU-541 ", ""}, PQUIRK_NOLUNS},
686 {{T_CDROM, T_REMOV,
687 "SONY ", "CD-ROM CDU-55S ", ""}, PQUIRK_NOLUNS},
688 {{T_CDROM, T_REMOV,
689 "SONY ", "CD-ROM CDU-561 ", ""}, PQUIRK_NOLUNS},
690 {{T_CDROM, T_REMOV,
691 "SONY ", "CD-ROM CDU-76S", ""},
692 PQUIRK_NOLUNS|PQUIRK_NOSYNC|PQUIRK_NOWIDE},
693 {{T_CDROM, T_REMOV,
694 "SONY ", "CD-ROM CDU-8003A", ""}, PQUIRK_NOLUNS},
695 {{T_CDROM, T_REMOV,
696 "SONY ", "CD-ROM CDU-8012 ", ""}, PQUIRK_NOLUNS},
697 {{T_CDROM, T_REMOV,
698 "TEAC ", "CD-ROM ", "1.06"}, PQUIRK_NOLUNS},
699 {{T_CDROM, T_REMOV,
700 "TEAC ", "CD-ROM CD-56S ", "1.0B"}, PQUIRK_NOLUNS},
701 {{T_CDROM, T_REMOV,
702 "TEXEL ", "CD-ROM ", "1.06"}, PQUIRK_NOLUNS},
703 {{T_CDROM, T_REMOV,
704 "TEXEL ", "CD-ROM DM-XX24 K", "1.09"}, PQUIRK_NOLUNS},
705 {{T_CDROM, T_REMOV,
706 "TEXEL ", "CD-ROM DM-XX24 K", "1.10"}, PQUIRK_NOLUNS},
707 {{T_CDROM, T_REMOV,
708 "TOSHIBA ", "XM-4101TASUNSLCD", ""}, PQUIRK_NOLUNS|PQUIRK_NOSYNC},
709 /* "IBM CDRM00201 !F" 0724 is an IBM OEM Toshiba XM-4101BME */
710 {{T_CDROM, T_REMOV,
711 "IBM ", "CDRM00201 !F", "0724"}, PQUIRK_NOLUNS|PQUIRK_NOSYNC},
712 {{T_CDROM, T_REMOV,
713 "ShinaKen", "CD-ROM DM-3x1S", "1.04"}, PQUIRK_NOLUNS},
714 {{T_CDROM, T_REMOV,
715 "JVC ", "R2626", ""}, PQUIRK_NOLUNS},
716 {{T_CDROM, T_REMOV,
717 "YAMAHA", "CRW8424S", ""}, PQUIRK_NOLUNS},
718 {{T_CDROM, T_REMOV,
719 "NEC ", "CD-ROM DRIVE:222", ""}, PQUIRK_NOLUNS|PQUIRK_NOSYNC},
720
721 {{T_DIRECT, T_FIXED,
722 "MICROP ", "1588-15MBSUN0669", ""}, PQUIRK_AUTOSAVE},
723 {{T_DIRECT, T_FIXED,
724 "MICROP ", "2217-15MQ1091501", ""}, PQUIRK_NOSYNCCACHE},
725 {{T_OPTICAL, T_REMOV,
726 "EPSON ", "OMD-5010 ", "3.08"}, PQUIRK_NOLUNS},
727 {{T_DIRECT, T_FIXED,
728 "ADAPTEC ", "AEC-4412BD", "1.2A"}, PQUIRK_NOMODESENSE},
729 {{T_DIRECT, T_FIXED,
730 "ADAPTEC ", "ACB-4000", ""}, PQUIRK_FORCELUNS|PQUIRK_AUTOSAVE|PQUIRK_NOMODESENSE},
731 {{T_DIRECT, T_FIXED,
732 "DEC ", "RZ55 (C) DEC", ""}, PQUIRK_AUTOSAVE},
733 {{T_DIRECT, T_FIXED,
734 "EMULEX ", "MD21/S2 ESDI", "A00"},
735 PQUIRK_FORCELUNS|PQUIRK_AUTOSAVE},
736 {{T_DIRECT, T_FIXED,
737 "MICROP", "1548-15MZ1077801", "HZ2P"}, PQUIRK_NOTAG},
738 {{T_DIRECT, T_FIXED,
739 "HP ", "C372", ""}, PQUIRK_NOTAG},
740 {{T_DIRECT, T_FIXED,
741 "IBMRAID ", "0662S", ""}, PQUIRK_AUTOSAVE},
742 {{T_DIRECT, T_FIXED,
743 "IBM ", "0663H", ""}, PQUIRK_AUTOSAVE},
744 {{T_DIRECT, T_FIXED,
745 "IBM", "0664", ""}, PQUIRK_AUTOSAVE},
746 {{T_DIRECT, T_FIXED,
747 /* improperly report DT-only sync mode */
748 "IBM ", "DXHS36D", ""},
749 PQUIRK_CAP_SYNC|PQUIRK_CAP_WIDE16},
750 {{T_DIRECT, T_FIXED,
751 "IBM ", "DXHS18Y", ""},
752 PQUIRK_CAP_SYNC|PQUIRK_CAP_WIDE16},
753 {{T_DIRECT, T_FIXED,
754 "IBM ", "H3171-S2", ""},
755 PQUIRK_NOLUNS|PQUIRK_AUTOSAVE},
756 {{T_DIRECT, T_FIXED,
757 "IBM ", "KZ-C", ""}, PQUIRK_AUTOSAVE},
758 /* Broken IBM disk */
759 {{T_DIRECT, T_FIXED,
760 "" , "DFRSS2F", ""}, PQUIRK_AUTOSAVE},
761 {{T_DIRECT, T_FIXED,
762 "Initio ", "", ""}, PQUIRK_NOBIGMODESENSE},
763 {{T_DIRECT, T_FIXED,
764 "JMicron ", "Generic ", ""}, PQUIRK_NOFUA},
765 {{T_DIRECT, T_REMOV,
766 "MPL ", "MC-DISK- ", ""}, PQUIRK_NOLUNS},
767 {{T_DIRECT, T_FIXED,
768 "MAXTOR ", "XT-3280 ", ""}, PQUIRK_NOLUNS},
769 {{T_DIRECT, T_FIXED,
770 "MAXTOR ", "XT-4380S ", ""}, PQUIRK_NOLUNS},
771 {{T_DIRECT, T_FIXED,
772 "MAXTOR ", "MXT-1240S ", ""}, PQUIRK_NOLUNS},
773 {{T_DIRECT, T_FIXED,
774 "MAXTOR ", "XT-4170S ", ""}, PQUIRK_NOLUNS},
775 {{T_DIRECT, T_FIXED,
776 "MAXTOR ", "XT-8760S", ""}, PQUIRK_NOLUNS},
777 {{T_DIRECT, T_FIXED,
778 "MAXTOR ", "LXT-213S ", ""}, PQUIRK_NOLUNS},
779 {{T_DIRECT, T_FIXED,
780 "MAXTOR ", "LXT-213S SUN0207", ""}, PQUIRK_NOLUNS},
781 {{T_DIRECT, T_FIXED,
782 "MAXTOR ", "LXT-200S ", ""}, PQUIRK_NOLUNS},
783 {{T_DIRECT, T_FIXED,
784 "MEGADRV ", "EV1000", ""}, PQUIRK_NOMODESENSE},
785 {{T_DIRECT, T_FIXED,
786 "MICROP", "1991-27MZ", ""}, PQUIRK_NOTAG},
787 {{T_DIRECT, T_FIXED,
788 "MST ", "SnapLink ", ""}, PQUIRK_NOLUNS},
789 {{T_DIRECT, T_FIXED,
790 "NEC ", "D3847 ", "0307"}, PQUIRK_NOLUNS},
791 {{T_DIRECT, T_FIXED,
792 "QUANTUM ", "ELS85S ", ""}, PQUIRK_AUTOSAVE},
793 {{T_DIRECT, T_FIXED,
794 "QUANTUM ", "LPS525S ", ""}, PQUIRK_NOLUNS},
795 {{T_DIRECT, T_FIXED,
796 "QUANTUM ", "P105S 910-10-94x", ""}, PQUIRK_NOLUNS},
797 {{T_DIRECT, T_FIXED,
798 "QUANTUM ", "PD1225S ", ""}, PQUIRK_NOLUNS},
799 {{T_DIRECT, T_FIXED,
800 "QUANTUM ", "PD210S SUN0207", ""}, PQUIRK_NOLUNS},
801 {{T_DIRECT, T_FIXED,
802 "QUANTUM ", "ATLAS IV 9 WLS", "0A0A"}, PQUIRK_CAP_NODT},
803 {{T_DIRECT, T_FIXED,
804 "RODIME ", "RO3000S ", ""}, PQUIRK_NOLUNS},
805 {{T_DIRECT, T_FIXED,
806 "SEAGATE ", "ST125N ", ""}, PQUIRK_NOLUNS},
807 {{T_DIRECT, T_FIXED,
808 "SEAGATE ", "ST157N ", ""}, PQUIRK_NOLUNS},
809 {{T_DIRECT, T_FIXED,
810 "SEAGATE ", "ST296 ", ""}, PQUIRK_NOLUNS},
811 {{T_DIRECT, T_FIXED,
812 "SEAGATE ", "ST296N ", ""}, PQUIRK_NOLUNS},
813 {{T_DIRECT, T_FIXED,
814 "SEAGATE ", "ST318404LC ", ""}, PQUIRK_NOLUNS},
815 {{T_DIRECT, T_FIXED,
816 "SEAGATE ", "ST336753LC ", ""}, PQUIRK_NOLUNS},
817 {{T_DIRECT, T_FIXED,
818 "SEAGATE ", "ST336753LW ", ""}, PQUIRK_NOLUNS},
819 {{T_DIRECT, T_FIXED,
820 "SEAGATE ", "ST336754LC ", ""}, PQUIRK_NOLUNS},
821 {{T_DIRECT, T_FIXED,
822 "SEAGATE ", "ST39236LC ", ""}, PQUIRK_NOLUNS},
823 {{T_DIRECT, T_FIXED,
824 "SEAGATE ", "ST15150N ", ""}, PQUIRK_NOTAG},
825 {{T_DIRECT, T_FIXED,
826 "SEAGATE ", "ST19171", ""}, PQUIRK_NOMODESENSE},
827 {{T_DIRECT, T_FIXED,
828 "SEAGATE ", "ST32430N", ""}, PQUIRK_CAP_SYNC},
829 {{T_DIRECT, T_FIXED,
830 "SEAGATE ", "ST34501FC ", ""}, PQUIRK_NOMODESENSE},
831 {{T_DIRECT, T_FIXED,
832 "SEAGATE ", "SX910800N", ""}, PQUIRK_NOTAG},
833 {{T_DIRECT, T_FIXED,
834 "TOSHIBA ", "MK538FB ", "6027"}, PQUIRK_NOLUNS},
835 {{T_DIRECT, T_FIXED,
836 "MICROP ", "1924", ""}, PQUIRK_CAP_SYNC},
837 {{T_DIRECT, T_FIXED,
838 "FUJITSU ", "M2266", ""}, PQUIRK_CAP_SYNC},
839 {{T_DIRECT, T_FIXED,
840 "FUJITSU ", "M2624S-512 ", ""}, PQUIRK_CAP_SYNC},
841 {{T_DIRECT, T_FIXED,
842 "SEAGATE ", "SX336704LC" , ""}, PQUIRK_CAP_SYNC | PQUIRK_CAP_WIDE16},
843 {{T_DIRECT, T_FIXED,
844 "SEAGATE ", "SX173404LC", ""}, PQUIRK_CAP_SYNC | PQUIRK_CAP_WIDE16},
845
846 {{T_DIRECT, T_REMOV,
847 "IOMEGA", "ZIP 100", "J.03"}, PQUIRK_NOLUNS|PQUIRK_NOSYNC},
848 {{T_DIRECT, T_REMOV,
849 "INSITE", "I325VM", ""}, PQUIRK_NOLUNS},
850
851 /* XXX: QIC-36 tape behind Emulex adapter. Very broken. */
852 {{T_SEQUENTIAL, T_REMOV,
853 " ", " ", " "}, PQUIRK_NOLUNS},
854 {{T_SEQUENTIAL, T_REMOV,
855 "EMULEX ", "MT-02 QIC ", ""}, PQUIRK_NOLUNS},
856 {{T_SEQUENTIAL, T_REMOV,
857 "CALIPER ", "CP150 ", ""}, PQUIRK_NOLUNS},
858 {{T_SEQUENTIAL, T_REMOV,
859 "EXABYTE ", "EXB-8200 ", ""}, PQUIRK_NOLUNS},
860 {{T_SEQUENTIAL, T_REMOV,
861 "SONY ", "GY-10C ", ""}, PQUIRK_NOLUNS},
862 {{T_SEQUENTIAL, T_REMOV,
863 "SONY ", "SDT-2000 ", "2.09"}, PQUIRK_NOLUNS},
864 {{T_SEQUENTIAL, T_REMOV,
865 "SONY ", "SDT-5000 ", "3."}, PQUIRK_NOSYNC|PQUIRK_NOWIDE},
866 {{T_SEQUENTIAL, T_REMOV,
867 "SONY ", "SDT-5200 ", "3."}, PQUIRK_NOLUNS},
868 {{T_SEQUENTIAL, T_REMOV,
869 "TANDBERG", " TDC 3600 ", ""}, PQUIRK_NOLUNS},
870 /* Following entry reported as a Tandberg 3600; ref. PR1933 */
871 {{T_SEQUENTIAL, T_REMOV,
872 "ARCHIVE ", "VIPER 150 21247", ""}, PQUIRK_NOLUNS},
873 /* Following entry for a Cipher ST150S; ref. PR4171 */
874 {{T_SEQUENTIAL, T_REMOV,
875 "ARCHIVE ", "VIPER 1500 21247", "2.2G"}, PQUIRK_NOLUNS},
876 {{T_SEQUENTIAL, T_REMOV,
877 "ARCHIVE ", "Python 28454-XXX", ""}, PQUIRK_NOLUNS},
878 {{T_SEQUENTIAL, T_REMOV,
879 "WANGTEK ", "5099ES SCSI", ""}, PQUIRK_NOLUNS},
880 {{T_SEQUENTIAL, T_REMOV,
881 "WANGTEK ", "5150ES SCSI", ""}, PQUIRK_NOLUNS},
882 {{T_SEQUENTIAL, T_REMOV,
883 "WANGTEK ", "SCSI-36", ""}, PQUIRK_NOLUNS},
884 {{T_SEQUENTIAL, T_REMOV,
885 "WangDAT ", "Model 1300 ", "02.4"}, PQUIRK_NOSYNC|PQUIRK_NOWIDE},
886 {{T_SEQUENTIAL, T_REMOV,
887 "WangDAT ", "Model 2600 ", "01.7"}, PQUIRK_NOSYNC|PQUIRK_NOWIDE},
888 {{T_SEQUENTIAL, T_REMOV,
889 "WangDAT ", "Model 3200 ", "02.2"}, PQUIRK_NOSYNC|PQUIRK_NOWIDE},
890 {{T_SEQUENTIAL, T_REMOV,
891 "TEAC ", "MT-2ST/N50 ", ""}, PQUIRK_NOLUNS},
892
893 {{T_SCANNER, T_FIXED,
894 "RICOH ", "IS60 ", "1R08"}, PQUIRK_NOLUNS},
895 {{T_SCANNER, T_FIXED,
896 "UMAX ", "Astra 1200S ", "V2.9"}, PQUIRK_NOLUNS},
897 {{T_SCANNER, T_FIXED,
898 "UMAX ", "Astra 1220S ", ""}, PQUIRK_NOLUNS},
899 {{T_SCANNER, T_FIXED,
900 "UMAX ", "UMAX S-6E ", "V2.0"}, PQUIRK_NOLUNS},
901 {{T_SCANNER, T_FIXED,
902 "UMAX ", "UMAX S-12 ", "V2.1"}, PQUIRK_NOLUNS},
903 {{T_SCANNER, T_FIXED,
904 "ULTIMA ", "A6000C ", ""}, PQUIRK_NOLUNS},
905 {{T_PROCESSOR, T_FIXED,
906 "ESG-SHV", "SCA HSBP M15", ""}, PQUIRK_NOLUNS},
907 {{T_PROCESSOR, T_FIXED,
908 "SYMBIOS", "", ""}, PQUIRK_NOLUNS},
909 {{T_PROCESSOR, T_FIXED,
910 "LITRONIC", "PCMCIA ", ""}, PQUIRK_NOLUNS},
911 {{T_CHANGER, T_REMOV,
912 "SONY ", "CDL1100 ", ""}, PQUIRK_NOLUNS},
913 {{T_ENCLOSURE, T_FIXED,
914 "SUN ", "SENA ", ""}, PQUIRK_NOLUNS},
915 };
916
917 /*
918 * given a target and lun, ask the device what
919 * it is, and find the correct driver table
920 * entry.
921 */
922 static int
923 scsi_probe_device(struct scsibus_softc *sc, int target, int lun)
924 {
925 struct scsipi_channel *chan = sc->sc_channel;
926 struct scsipi_periph *periph;
927 struct scsipi_inquiry_data inqbuf;
928 const struct scsi_quirk_inquiry_pattern *finger;
929 int checkdtype, priority, docontinue, quirks;
930 struct scsipibus_attach_args sa;
931 cfdata_t cf;
932 int locs[SCSIBUSCF_NLOCS];
933
934 /*
935 * Assume no more LUNs to search after this one.
936 * If we successfully get Inquiry data and after
937 * merging quirks we find we can probe for more
938 * LUNs, we will.
939 */
940 docontinue = 0;
941
942 /* Skip this slot if it is already attached. */
943 if (scsipi_lookup_periph(chan, target, lun) != NULL)
944 return (docontinue);
945
946 periph = scsipi_alloc_periph(M_WAITOK);
947 periph->periph_channel = chan;
948 periph->periph_switch = &scsi_probe_dev;
949
950 periph->periph_target = target;
951 periph->periph_lun = lun;
952 periph->periph_quirks = chan->chan_defquirks;
953
954 #ifdef SCSIPI_DEBUG
955 if (SCSIPI_DEBUG_TYPE == SCSIPI_BUSTYPE_SCSI &&
956 SCSIPI_DEBUG_TARGET == target &&
957 SCSIPI_DEBUG_LUN == lun)
958 periph->periph_dbflags |= SCSIPI_DEBUG_FLAGS;
959 #endif
960
961 /*
962 * Ask the device what it is
963 */
964
965 #ifdef SCSI_2_DEF
966 /* some devices need to be told to go to SCSI2 */
967 /* However some just explode if you tell them this.. leave it out */
968 scsi_change_def(periph, XS_CTL_DISCOVERY | XS_CTL_SILENT);
969 #endif /* SCSI_2_DEF */
970
971 /* Now go ask the device all about itself. */
972 memset(&inqbuf, 0, sizeof(inqbuf));
973 {
974 u_int8_t *extension = &inqbuf.flags1;
975 int len = 0;
976 while (len < 3)
977 extension[len++] = '\0';
978 while (len < 3 + 28)
979 extension[len++] = ' ';
980 while (len < 3 + 28 + 20)
981 extension[len++] = '\0';
982 while (len < 3 + 28 + 20 + 1)
983 extension[len++] = '\0';
984 while (len < 3 + 28 + 20 + 1 + 1)
985 extension[len++] = '\0';
986 while (len < 3 + 28 + 20 + 1 + 1 + (8*2))
987 extension[len++] = ' ';
988 }
989 if (scsipi_inquire(periph, &inqbuf, XS_CTL_DISCOVERY | XS_CTL_SILENT))
990 goto bad;
991
992 periph->periph_type = inqbuf.device & SID_TYPE;
993 if (inqbuf.dev_qual2 & SID_REMOVABLE)
994 periph->periph_flags |= PERIPH_REMOVABLE;
995 periph->periph_version = inqbuf.version & SID_ANSII;
996
997 /*
998 * Any device qualifier that has the top bit set (qualifier&4 != 0)
999 * is vendor specific and won't match in this switch.
1000 * All we do here is throw out bad/negative responses.
1001 */
1002 checkdtype = 0;
1003 switch (inqbuf.device & SID_QUAL) {
1004 case SID_QUAL_LU_PRESENT:
1005 checkdtype = 1;
1006 break;
1007
1008 case SID_QUAL_LU_NOTPRESENT:
1009 case SID_QUAL_reserved:
1010 case SID_QUAL_LU_NOT_SUPP:
1011 goto bad;
1012
1013 default:
1014 break;
1015 }
1016
1017 /* Let the adapter driver handle the device separately if it wants. */
1018 if (chan->chan_adapter->adapt_accesschk != NULL &&
1019 (*chan->chan_adapter->adapt_accesschk)(periph, &sa.sa_inqbuf))
1020 goto bad;
1021
1022 if (checkdtype) {
1023 switch (periph->periph_type) {
1024 case T_DIRECT:
1025 case T_SEQUENTIAL:
1026 case T_PRINTER:
1027 case T_PROCESSOR:
1028 case T_WORM:
1029 case T_CDROM:
1030 case T_SCANNER:
1031 case T_OPTICAL:
1032 case T_CHANGER:
1033 case T_COMM:
1034 case T_IT8_1:
1035 case T_IT8_2:
1036 case T_STORARRAY:
1037 case T_ENCLOSURE:
1038 case T_SIMPLE_DIRECT:
1039 case T_OPTIC_CARD_RW:
1040 case T_OBJECT_STORED:
1041 default:
1042 break;
1043 case T_NODEVICE:
1044 goto bad;
1045 }
1046 }
1047
1048 sa.sa_periph = periph;
1049 sa.sa_inqbuf.type = inqbuf.device;
1050 sa.sa_inqbuf.removable = inqbuf.dev_qual2 & SID_REMOVABLE ?
1051 T_REMOV : T_FIXED;
1052 sa.sa_inqbuf.vendor = inqbuf.vendor;
1053 sa.sa_inqbuf.product = inqbuf.product;
1054 sa.sa_inqbuf.revision = inqbuf.revision;
1055 sa.scsipi_info.scsi_version = inqbuf.version;
1056 sa.sa_inqptr = &inqbuf;
1057
1058 finger = scsipi_inqmatch(
1059 &sa.sa_inqbuf, scsi_quirk_patterns,
1060 sizeof(scsi_quirk_patterns)/sizeof(scsi_quirk_patterns[0]),
1061 sizeof(scsi_quirk_patterns[0]), &priority);
1062
1063 if (finger != NULL)
1064 quirks = finger->quirks;
1065 else
1066 quirks = 0;
1067
1068 /*
1069 * Determine the operating mode capabilities of the device.
1070 */
1071 if (periph->periph_version >= 2) {
1072 if ((inqbuf.flags3 & SID_CmdQue) != 0 &&
1073 (quirks & PQUIRK_NOTAG) == 0)
1074 periph->periph_cap |= PERIPH_CAP_TQING;
1075 if ((inqbuf.flags3 & SID_Linked) != 0)
1076 periph->periph_cap |= PERIPH_CAP_LINKCMDS;
1077 if ((inqbuf.flags3 & SID_Sync) != 0 &&
1078 (quirks & PQUIRK_NOSYNC) == 0)
1079 periph->periph_cap |= PERIPH_CAP_SYNC;
1080 if ((inqbuf.flags3 & SID_WBus16) != 0 &&
1081 (quirks & PQUIRK_NOWIDE) == 0)
1082 periph->periph_cap |= PERIPH_CAP_WIDE16;
1083 if ((inqbuf.flags3 & SID_WBus32) != 0 &&
1084 (quirks & PQUIRK_NOWIDE) == 0)
1085 periph->periph_cap |= PERIPH_CAP_WIDE32;
1086 if ((inqbuf.flags3 & SID_SftRe) != 0)
1087 periph->periph_cap |= PERIPH_CAP_SFTRESET;
1088 if ((inqbuf.flags3 & SID_RelAdr) != 0)
1089 periph->periph_cap |= PERIPH_CAP_RELADR;
1090 /* SPC-2 */
1091 if (periph->periph_version >= 3 &&
1092 !(quirks & PQUIRK_CAP_NODT)){
1093 /*
1094 * Report ST clocking though CAP_WIDExx/CAP_SYNC.
1095 * If the device only supports DT, clear these
1096 * flags (DT implies SYNC and WIDE)
1097 */
1098 switch (inqbuf.flags4 & SID_Clocking) {
1099 case SID_CLOCKING_DT_ONLY:
1100 periph->periph_cap &=
1101 ~(PERIPH_CAP_SYNC |
1102 PERIPH_CAP_WIDE16 |
1103 PERIPH_CAP_WIDE32);
1104 /* FALLTHROUGH */
1105 case SID_CLOCKING_SD_DT:
1106 periph->periph_cap |= PERIPH_CAP_DT;
1107 break;
1108 default: /* ST only or invalid */
1109 /* nothing to do */
1110 break;
1111 }
1112 }
1113 if (periph->periph_version >= 3) {
1114 if (inqbuf.flags4 & SID_IUS)
1115 periph->periph_cap |= PERIPH_CAP_IUS;
1116 if (inqbuf.flags4 & SID_QAS)
1117 periph->periph_cap |= PERIPH_CAP_QAS;
1118 }
1119 }
1120 if (quirks & PQUIRK_CAP_SYNC)
1121 periph->periph_cap |= PERIPH_CAP_SYNC;
1122 if (quirks & PQUIRK_CAP_WIDE16)
1123 periph->periph_cap |= PERIPH_CAP_WIDE16;
1124
1125 /*
1126 * Now apply any quirks from the table.
1127 */
1128 periph->periph_quirks |= quirks;
1129 if (periph->periph_version == 0 &&
1130 (periph->periph_quirks & PQUIRK_FORCELUNS) == 0)
1131 periph->periph_quirks |= PQUIRK_NOLUNS;
1132
1133 if ((periph->periph_quirks & PQUIRK_NOLUNS) == 0)
1134 docontinue = 1;
1135
1136 locs[SCSIBUSCF_TARGET] = target;
1137 locs[SCSIBUSCF_LUN] = lun;
1138
1139 KERNEL_LOCK(1, NULL);
1140 if ((cf = config_search(sc->sc_dev, &sa,
1141 CFARGS(.submatch = config_stdsubmatch,
1142 .locators = locs))) != NULL) {
1143 scsipi_insert_periph(chan, periph);
1144
1145 /*
1146 * Determine supported opcodes and timeouts if available.
1147 * Only do this on peripherals reporting SCSI version 3
1148 * or greater - this command isn't in the SCSI-2 spec. and
1149 * it causes either timeouts or peripherals disappearing
1150 * when sent to some SCSI-1 or SCSI-2 peripherals.
1151 */
1152 if (periph->periph_version >= 3)
1153 scsipi_get_opcodeinfo(periph);
1154
1155 /*
1156 * XXX Can't assign periph_dev here, because we'll
1157 * XXX need it before config_attach() returns. Must
1158 * XXX assign it in periph driver.
1159 */
1160 config_attach(sc->sc_dev, cf, &sa, scsibusprint,
1161 CFARGS(.locators = locs));
1162 KERNEL_UNLOCK_ONE(NULL);
1163 } else {
1164 scsibusprint(&sa, device_xname(sc->sc_dev));
1165 aprint_normal(" not configured\n");
1166 KERNEL_UNLOCK_ONE(NULL);
1167 goto bad;
1168 }
1169
1170 return (docontinue);
1171
1172 bad:
1173 scsipi_free_periph(periph);
1174 return (docontinue);
1175 }
1176
1177 /****** Entry points for user control of the SCSI bus. ******/
1178
1179 static int
1180 scsibusopen(dev_t dev, int flag, int fmt,
1181 struct lwp *l)
1182 {
1183 struct scsibus_softc *sc;
1184 int error, unit = minor(dev);
1185
1186 sc = device_lookup_private(&scsibus_cd, unit);
1187 if (sc == NULL)
1188 return (ENXIO);
1189
1190 if (sc->sc_flags & SCSIBUSF_OPEN)
1191 return (EBUSY);
1192
1193 if ((error = scsipi_adapter_addref(sc->sc_channel->chan_adapter)) != 0)
1194 return (error);
1195
1196 sc->sc_flags |= SCSIBUSF_OPEN;
1197
1198 return (0);
1199 }
1200
1201 static int
1202 scsibusclose(dev_t dev, int flag, int fmt,
1203 struct lwp *l)
1204 {
1205 struct scsibus_softc *sc;
1206
1207 sc = device_lookup_private(&scsibus_cd, minor(dev));
1208 scsipi_adapter_delref(sc->sc_channel->chan_adapter);
1209
1210 sc->sc_flags &= ~SCSIBUSF_OPEN;
1211
1212 return (0);
1213 }
1214
1215 static int
1216 scsibusioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
1217 {
1218 struct scsibus_softc *sc;
1219 struct scsipi_channel *chan;
1220 int error;
1221
1222 sc = device_lookup_private(&scsibus_cd, minor(dev));
1223 chan = sc->sc_channel;
1224
1225 /*
1226 * Enforce write permission for ioctls that change the
1227 * state of the bus. Host adapter specific ioctls must
1228 * be checked by the adapter driver.
1229 */
1230 switch (cmd) {
1231 case SCBUSIOSCAN:
1232 case SCBUSIODETACH:
1233 case SCBUSIORESET:
1234 if ((flag & FWRITE) == 0)
1235 return (EBADF);
1236 }
1237
1238 switch (cmd) {
1239 case SCBUSIOSCAN:
1240 {
1241 struct scbusioscan_args *a =
1242 (struct scbusioscan_args *)addr;
1243
1244 error = scsi_probe_bus(sc, a->sa_target, a->sa_lun);
1245 break;
1246 }
1247
1248 case SCBUSIODETACH:
1249 {
1250 struct scbusiodetach_args *a =
1251 (struct scbusiodetach_args *)addr;
1252
1253 error = scsipi_target_detach(chan, a->sa_target, a->sa_lun, 0);
1254 break;
1255 }
1256
1257
1258 case SCBUSIORESET:
1259 /* FALLTHROUGH */
1260 default:
1261 error = scsipi_adapter_ioctl(chan, cmd, addr, flag, l->l_proc);
1262 break;
1263 }
1264
1265 return (error);
1266 }
1267