scsiconf.c revision 1.50 1 /* $NetBSD: scsiconf.c,v 1.50 1996/02/22 23:37:27 mycroft Exp $ */
2
3 /*
4 * Copyright (c) 1994 Charles Hannum. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Charles Hannum.
17 * 4. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Originally written by Julian Elischer (julian (at) tfs.com)
34 * for TRW Financial Systems for use under the MACH(2.5) operating system.
35 *
36 * TRW Financial Systems, in accordance with their agreement with Carnegie
37 * Mellon University, makes this software available to CMU to distribute
38 * or use in any manner that they see fit as long as this message is kept with
39 * the software. For this reason TFS also grants any other persons or
40 * organisations permission to use or modify this software.
41 *
42 * TFS supplies this software to be publicly redistributed
43 * on the understanding that TFS is not responsible for the correct
44 * functioning of this software in any circumstances.
45 *
46 * Ported to run under 386BSD by Julian Elischer (julian (at) tfs.com) Sept 1992
47 */
48
49 #include <sys/types.h>
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/malloc.h>
53 #include <sys/device.h>
54 #include <sys/cpu.h>
55
56 #include <scsi/scsi_all.h>
57 #include <scsi/scsiconf.h>
58
59 #if 0
60 #if NCALS > 0
61 { T_PROCESSOR, T_FIXED, 1,
62 0, 0, 0 },
63 #endif /* NCALS */
64 #if NBLL > 0
65 { T_PROCESSOR, T_FIXED, 1,
66 "AEG ", "READER ", "V1.0" },
67 #endif /* NBLL */
68 #if NKIL > 0
69 { T_SCANNER, T_FIXED, 0,
70 "KODAK ", "IL Scanner 900 ", 0 },
71 #endif /* NKIL */
72 #endif
73
74 /*
75 * Declarations
76 */
77 void scsi_probedev __P((struct scsibus_softc *, int, int));
78 int scsi_probe_bus __P((int bus, int target, int lun));
79
80 struct scsi_device probe_switch = {
81 NULL,
82 NULL,
83 NULL,
84 NULL,
85 };
86
87 int scsibusmatch __P((struct device *, void *, void *));
88 void scsibusattach __P((struct device *, struct device *, void *));
89 int scsibussubmatch __P((struct device *, void *, void *));
90
91 struct cfdriver scsibuscd = {
92 NULL, "scsibus", scsibusmatch, scsibusattach, DV_DULL,
93 sizeof(struct scsibus_softc)
94 };
95
96 int
97 scsibusmatch(parent, match, aux)
98 struct device *parent;
99 void *match, *aux;
100 {
101
102 return 1;
103 }
104
105 /*
106 * The routine called by the adapter boards to get all their
107 * devices configured in.
108 */
109 void
110 scsibusattach(parent, self, aux)
111 struct device *parent, *self;
112 void *aux;
113 {
114 struct scsibus_softc *sb = (struct scsibus_softc *)self;
115 struct scsi_link *sc_link_proto = aux;
116
117 sc_link_proto->scsibus = sb->sc_dev.dv_unit;
118 sb->adapter_link = sc_link_proto;
119 printf("\n");
120
121 #if defined(SCSI_DELAY) && SCSI_DELAY > 2
122 printf("%s: waiting for scsi devices to settle\n",
123 sb->sc_dev.dv_xname);
124 #else /* SCSI_DELAY > 2 */
125 #undef SCSI_DELAY
126 #define SCSI_DELAY 2
127 #endif /* SCSI_DELAY */
128 delay(1000000 * SCSI_DELAY);
129
130 scsi_probe_bus(sb->sc_dev.dv_unit, -1, -1);
131 }
132
133 int
134 scsibussubmatch(parent, match, aux)
135 struct device *parent;
136 void *match, *aux;
137 {
138 struct cfdata *cf = match;
139 struct scsibus_attach_args *sa = aux;
140 struct scsi_link *sc_link = sa->sa_sc_link;
141
142 if (cf->cf_loc[0] != -1 && cf->cf_loc[0] != sc_link->target)
143 return 0;
144 if (cf->cf_loc[1] != -1 && cf->cf_loc[1] != sc_link->lun)
145 return 0;
146 return ((*cf->cf_driver->cd_match)(parent, match, aux));
147 }
148
149 /*
150 * Probe the requested scsi bus. It must be already set up.
151 * -1 requests all set up scsi busses.
152 * target and lun optionally narrow the search if not -1
153 */
154 int
155 scsi_probe_busses(bus, target, lun)
156 int bus, target, lun;
157 {
158
159 if (bus == -1) {
160 for (bus = 0; bus < scsibuscd.cd_ndevs; bus++)
161 if (scsibuscd.cd_devs[bus])
162 scsi_probe_bus(bus, target, lun);
163 return 0;
164 } else {
165 return scsi_probe_bus(bus, target, lun);
166 }
167 }
168
169 /*
170 * Probe the requested scsi bus. It must be already set up.
171 * target and lun optionally narrow the search if not -1
172 */
173 int
174 scsi_probe_bus(bus, target, lun)
175 int bus, target, lun;
176 {
177 struct scsibus_softc *scsi;
178 int maxtarget, mintarget, maxlun, minlun;
179 u_int8_t scsi_addr;
180
181 if (bus < 0 || bus >= scsibuscd.cd_ndevs)
182 return ENXIO;
183 scsi = scsibuscd.cd_devs[bus];
184 if (!scsi)
185 return ENXIO;
186
187 scsi_addr = scsi->adapter_link->adapter_target;
188
189 if (target == -1) {
190 maxtarget = 7;
191 mintarget = 0;
192 } else {
193 if (target < 0 || target > 7)
194 return EINVAL;
195 maxtarget = mintarget = target;
196 }
197
198 if (lun == -1) {
199 maxlun = 7;
200 minlun = 0;
201 } else {
202 if (lun < 0 || lun > 7)
203 return EINVAL;
204 maxlun = minlun = lun;
205 }
206
207 for (target = mintarget; target <= maxtarget; target++) {
208 if (target == scsi_addr)
209 continue;
210 for (lun = minlun; lun <= maxlun; lun++) {
211 /*
212 * See if there's a device present, and configure it.
213 */
214 scsi_probedev(scsi, target, lun);
215 if ((scsi->moreluns & (1 << target)) == 0)
216 break;
217 /* otherwise something says we should look further */
218 }
219 }
220 return 0;
221 }
222
223 void
224 scsi_strvis(dst, src, len)
225 u_char *dst, *src;
226 int len;
227 {
228
229 /* Trim leading and trailing blanks and NULs. */
230 while (len > 0 && (src[0] == ' ' || src[0] == '\0'))
231 ++src, --len;
232 while (len > 0 && (src[len-1] == ' ' || src[len-1] == '\0'))
233 --len;
234
235 while (len > 0) {
236 if (*src < 0x20 || *src >= 0x80) {
237 /* non-printable characters */
238 *dst++ = '\\';
239 *dst++ = ((*src & 0300) >> 6) + '0';
240 *dst++ = ((*src & 0070) >> 3) + '0';
241 *dst++ = ((*src & 0007) >> 0) + '0';
242 } else if (*src == '\\') {
243 /* quote characters */
244 *dst++ = '\\';
245 *dst++ = '\\';
246 } else {
247 /* normal characters */
248 *dst++ = *src;
249 }
250 ++src, --len;
251 }
252
253 *dst++ = 0;
254 }
255
256 struct scsi_quirk_inquiry_pattern {
257 struct scsi_inquiry_pattern pattern;
258 u_int8_t quirks;
259 };
260
261 struct scsi_quirk_inquiry_pattern scsi_quirk_patterns[] = {
262 {{T_CDROM, T_REMOV,
263 "CHINON ", "CD-ROM CDS-431 ", ""}, SDEV_NOLUNS},
264 {{T_CDROM, T_REMOV,
265 "Chinon ", "CD-ROM CDS-525 ", ""}, SDEV_NOLUNS},
266 {{T_CDROM, T_REMOV,
267 "CHINON ", "CD-ROM CDS-535 ", ""}, SDEV_NOLUNS},
268 {{T_CDROM, T_REMOV,
269 "DENON ", "DRD-25X ", "V"}, SDEV_NOLUNS},
270 {{T_CDROM, T_REMOV,
271 "IMS ", "CDD521/10 ", "2.06"}, SDEV_NOLUNS},
272 {{T_CDROM, T_REMOV,
273 "MEDIAVIS", "CDR-H93MV ", "1.31"}, SDEV_NOLUNS},
274 {{T_CDROM, T_REMOV,
275 "NEC ", "CD-ROM DRIVE:55 ", ""}, SDEV_NOLUNS},
276 {{T_CDROM, T_REMOV,
277 "NEC ", "CD-ROM DRIVE:83 ", ""}, SDEV_NOLUNS},
278 {{T_CDROM, T_REMOV,
279 "NEC ", "CD-ROM DRIVE:84 ", ""}, SDEV_NOLUNS},
280 {{T_CDROM, T_REMOV,
281 "NEC ", "CD-ROM DRIVE:841", ""}, SDEV_NOLUNS},
282 {{T_CDROM, T_REMOV,
283 "SONY ", "CD-ROM CDU-541 ", ""}, SDEV_NOLUNS},
284 {{T_CDROM, T_REMOV,
285 "SONY ", "CD-ROM CDU-55S ", ""}, SDEV_NOLUNS},
286 {{T_CDROM, T_REMOV,
287 "SONY ", "CD-ROM CDU-8003A", ""}, SDEV_NOLUNS},
288 {{T_CDROM, T_REMOV,
289 "SONY ", "CD-ROM CDU-8012 ", ""}, SDEV_NOLUNS},
290 {{T_CDROM, T_REMOV,
291 "TEAC ", "CD-ROM ", "1.06"}, SDEV_NOLUNS},
292 {{T_CDROM, T_REMOV,
293 "TEXEL ", "CD-ROM ", "1.06"}, SDEV_NOLUNS},
294 {{T_CDROM, T_REMOV,
295 "TEXEL ", "CD-ROM DM-XX24 K", "1.10"}, SDEV_NOLUNS},
296
297 {{T_DIRECT, T_FIXED,
298 "DEC ", "RZ55 (C) DEC", ""}, SDEV_AUTOSAVE},
299 {{T_DIRECT, T_FIXED,
300 "EMULEX ", "MD21/S2 ESDI", "A00"}, SDEV_FORCELUNS},
301 {{T_DIRECT, T_FIXED,
302 "MAXTOR ", "XT-3280 ", ""}, SDEV_NOLUNS},
303 {{T_DIRECT, T_FIXED,
304 "MAXTOR ", "XT-4380S ", ""}, SDEV_NOLUNS},
305 {{T_DIRECT, T_FIXED,
306 "MAXTOR ", "MXT-1240S ", ""}, SDEV_NOLUNS},
307 {{T_DIRECT, T_FIXED,
308 "MAXTOR ", "XT-4170S ", ""}, SDEV_NOLUNS},
309 {{T_DIRECT, T_FIXED,
310 "MAXTOR ", "XT-8760S ", ""}, SDEV_NOLUNS},
311 {{T_DIRECT, T_FIXED,
312 "MAXTOR ", "LXT-213S ", ""}, SDEV_NOLUNS},
313 {{T_DIRECT, T_FIXED,
314 "MAXTOR ", "LXT-213S SUN0207", ""}, SDEV_NOLUNS},
315 {{T_DIRECT, T_FIXED,
316 "MST ", "SnapLink ", ""}, SDEV_NOLUNS},
317 {{T_DIRECT, T_FIXED,
318 "QUANTUM ", "LPS525S ", ""}, SDEV_NOLUNS},
319 {{T_DIRECT, T_FIXED,
320 "QUANTUM ", "P105S 910-10-94x", ""}, SDEV_NOLUNS},
321 {{T_DIRECT, T_FIXED,
322 "QUANTUM ", "PD1225S ", ""}, SDEV_NOLUNS},
323 {{T_DIRECT, T_FIXED,
324 "QUANTUM ", "PD210S SUN0207", ""}, SDEV_NOLUNS},
325 {{T_DIRECT, T_FIXED,
326 "RODIME ", "RO3000S ", ""}, SDEV_NOLUNS},
327 {{T_DIRECT, T_FIXED,
328 "SEAGATE ", "ST157N ", ""}, SDEV_NOLUNS},
329 {{T_DIRECT, T_FIXED,
330 "SEAGATE ", "ST296 ", ""}, SDEV_NOLUNS},
331 {{T_DIRECT, T_FIXED,
332 "SEAGATE ", "ST296N ", ""}, SDEV_NOLUNS},
333 {{T_DIRECT, T_FIXED,
334 "TOSHIBA ", "MK538FB ", "6027"}, SDEV_NOLUNS},
335
336 /* XXX: QIC-36 tape behind Emulex adapter. Very broken. */
337 {{T_SEQUENTIAL, T_REMOV,
338 " ", " ", " "}, SDEV_NOLUNS},
339 {{T_SEQUENTIAL, T_REMOV,
340 "CALIPER ", "CP150 ", ""}, SDEV_NOLUNS},
341 {{T_SEQUENTIAL, T_REMOV,
342 "EXABYTE ", "EXB-8200 ", ""}, SDEV_NOLUNS},
343 {{T_SEQUENTIAL, T_REMOV,
344 "SONY ", "SDT-2000 ", "2.09"}, SDEV_NOLUNS},
345 {{T_SEQUENTIAL, T_REMOV,
346 "SONY ", "SDT-5000 ", "3."}, SDEV_NOSYNCWIDE},
347 {{T_SEQUENTIAL, T_REMOV,
348 "SONY ", "SDT-5200 ", "3."}, SDEV_NOLUNS},
349 {{T_SEQUENTIAL, T_REMOV,
350 "TANDBERG", " TDC 3600 ", ""}, SDEV_NOLUNS},
351 /* Following entry reported as a Tandberg 3600; ref. PR1933 */
352 {{T_SEQUENTIAL, T_REMOV,
353 "ARCHIVE ", "VIPER 150 21247", ""}, SDEV_NOLUNS},
354 {{T_SEQUENTIAL, T_REMOV,
355 "WANGTEK ", "5099ES SCSI", ""}, SDEV_NOLUNS},
356 {{T_SEQUENTIAL, T_REMOV,
357 "WANGTEK ", "5150ES SCSI", ""}, SDEV_NOLUNS},
358 {{T_SEQUENTIAL, T_REMOV,
359 "WangDAT ", "Model 1300 ", "02.4"}, SDEV_NOSYNCWIDE},
360 {{T_SEQUENTIAL, T_REMOV,
361 "WangDAT ", "Model 2600 ", "01.7"}, SDEV_NOSYNCWIDE},
362 {{T_SEQUENTIAL, T_REMOV,
363 "WangDAT ", "Model 3200 ", "02.2"}, SDEV_NOSYNCWIDE},
364 };
365
366 /*
367 * given a target and lu, ask the device what
368 * it is, and find the correct driver table
369 * entry.
370 */
371 void
372 scsi_probedev(scsi, target, lun)
373 struct scsibus_softc *scsi;
374 int target, lun;
375 {
376 struct scsi_link *sc_link;
377 static struct scsi_inquiry_data inqbuf;
378 struct scsi_quirk_inquiry_pattern *finger;
379 int priority;
380 u_int8_t type;
381 boolean removable;
382 char *dtype, *qtype;
383 char vendor[33], product[65], revision[17];
384 struct scsibus_attach_args sa;
385 struct cfdata *cf;
386
387 /* Skip this slot if it is already attached. */
388 if (scsi->sc_link[target][lun])
389 return;
390
391 sc_link = malloc(sizeof(*sc_link), M_DEVBUF, M_NOWAIT);
392 *sc_link = *scsi->adapter_link;
393 sc_link->target = target;
394 sc_link->lun = lun;
395 sc_link->device = &probe_switch;
396
397 /*
398 * Ask the device what it is
399 */
400 #ifdef SCSIDEBUG
401 if (target == DEBUGTARGET && lun == DEBUGLUN)
402 sc_link->flags |= DEBUGLEVEL;
403 #endif /* SCSIDEBUG */
404
405 (void) scsi_test_unit_ready(sc_link,
406 SCSI_AUTOCONF | SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_NOT_READY | SCSI_IGNORE_MEDIA_CHANGE);
407
408 #ifdef SCSI_2_DEF
409 /* some devices need to be told to go to SCSI2 */
410 /* However some just explode if you tell them this.. leave it out */
411 scsi_change_def(sc_link, SCSI_AUTOCONF | SCSI_SILENT);
412 #endif /* SCSI_2_DEF */
413
414 /* Now go ask the device all about itself. */
415 bzero(&inqbuf, sizeof(inqbuf));
416 if (scsi_inquire(sc_link, &inqbuf, SCSI_AUTOCONF) != 0)
417 goto bad;
418
419 {
420 int len = inqbuf.additional_length;
421 while (len < 3)
422 inqbuf.unused[len++] = '\0';
423 while (len < 3 + 28)
424 inqbuf.unused[len++] = ' ';
425 }
426
427 finger = (struct scsi_quirk_inquiry_pattern *)scsi_inqmatch(&inqbuf,
428 (caddr_t)scsi_quirk_patterns,
429 sizeof(scsi_quirk_patterns)/sizeof(scsi_quirk_patterns[0]),
430 sizeof(scsi_quirk_patterns[0]), &priority);
431 if (priority != 0)
432 sc_link->quirks |= finger->quirks;
433 if ((inqbuf.version & SID_ANSII) == 0 &&
434 (sc_link->quirks & SDEV_FORCELUNS) == 0)
435 sc_link->quirks |= SDEV_NOLUNS;
436
437 if ((sc_link->quirks & SDEV_NOLUNS) == 0)
438 scsi->moreluns |= (1 << target);
439
440 /*
441 * note what BASIC type of device it is
442 */
443 type = inqbuf.device & SID_TYPE;
444 removable = inqbuf.dev_qual2 & SID_REMOVABLE ? 1 : 0;
445
446 if (removable)
447 sc_link->flags |= SDEV_REMOVABLE;
448
449 /*
450 * Any device qualifier that has the top bit set (qualifier&4 != 0)
451 * is vendor specific and won't match in this switch.
452 */
453 dtype = 0;
454 switch (inqbuf.device & SID_QUAL) {
455 case SID_QUAL_LU_OK:
456 qtype = "";
457 break;
458
459 case SID_QUAL_LU_OFFLINE:
460 qtype = " offline";
461 break;
462
463 case SID_QUAL_RSVD:
464 case SID_QUAL_BAD_LU:
465 goto bad;
466
467 default:
468 qtype = "";
469 dtype = "vendor-unique";
470 break;
471 }
472 if (dtype == 0) {
473 switch (type) {
474 case T_DIRECT:
475 dtype = "direct";
476 break;
477 case T_SEQUENTIAL:
478 dtype = "sequential";
479 break;
480 case T_PRINTER:
481 dtype = "printer";
482 break;
483 case T_PROCESSOR:
484 dtype = "processor";
485 break;
486 case T_CDROM:
487 dtype = "cdrom";
488 break;
489 case T_WORM:
490 dtype = "worm";
491 break;
492 case T_SCANNER:
493 dtype = "scanner";
494 break;
495 case T_OPTICAL:
496 dtype = "optical";
497 break;
498 case T_CHANGER:
499 dtype = "changer";
500 break;
501 case T_COMM:
502 dtype = "communication";
503 break;
504 case T_NODEVICE:
505 goto bad;
506 default:
507 dtype = "unknown";
508 break;
509 }
510 }
511
512 scsi_strvis(vendor, inqbuf.vendor, 8);
513 scsi_strvis(product, inqbuf.product, 16);
514 scsi_strvis(revision, inqbuf.revision, 4);
515
516 printf("%s targ %d lun %d: <%s, %s, %s> SCSI%d %d/%s %s%s\n",
517 ((struct device *)sc_link->adapter_softc)->dv_xname,
518 target, lun, vendor, product, revision,
519 inqbuf.version & SID_ANSII, type, dtype,
520 removable ? "removable" : "fixed", qtype);
521
522 sa.sa_sc_link = sc_link;
523 sa.sa_inqbuf = &inqbuf;
524
525 if ((cf = config_search(scsibussubmatch, (struct device *)scsi, &sa)) != 0) {
526 scsi->sc_link[target][lun] = sc_link;
527 config_attach((struct device *)scsi, cf, &sa, NULL);
528 } else
529 goto bad;
530
531 return;
532
533 bad:
534 free(sc_link, M_DEVBUF);
535 return;
536 }
537
538 /*
539 * Return a priority based on how much of the inquiry data matches
540 * the patterns for the particular driver.
541 */
542 caddr_t
543 scsi_inqmatch(inqbuf, base, nmatches, matchsize, bestpriority)
544 struct scsi_inquiry_data *inqbuf;
545 caddr_t base;
546 int nmatches, matchsize;
547 int *bestpriority;
548 {
549 u_int8_t type;
550 boolean removable;
551 caddr_t bestmatch;
552
553 /* Include the qualifier to catch vendor-unique types. */
554 type = inqbuf->device;
555 removable = inqbuf->dev_qual2 & SID_REMOVABLE ? T_REMOV : T_FIXED;
556
557 for (*bestpriority = 0, bestmatch = 0; nmatches--; base += matchsize) {
558 struct scsi_inquiry_pattern *match = (void *)base;
559 int priority, len;
560
561 if (type != match->type)
562 continue;
563 if (removable != match->removable)
564 continue;
565 priority = 2;
566 len = strlen(match->vendor);
567 if (bcmp(inqbuf->vendor, match->vendor, len))
568 continue;
569 priority += len;
570 len = strlen(match->product);
571 if (bcmp(inqbuf->product, match->product, len))
572 continue;
573 priority += len;
574 len = strlen(match->revision);
575 if (bcmp(inqbuf->revision, match->revision, len))
576 continue;
577 priority += len;
578
579 #if SCSIDEBUG
580 printf("scsi_inqmatch: %d/%d/%d <%s, %s, %s>\n",
581 priority, match->type, match->removable,
582 match->vendor, match->product, match->revision);
583 #endif
584 if (priority > *bestpriority) {
585 *bestpriority = priority;
586 bestmatch = base;
587 }
588 }
589
590 return (bestmatch);
591 }
592