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