scsiconf.c revision 1.49 1 /* $NetBSD: scsiconf.c,v 1.49 1996/02/18 20:32:43 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 "EMULEX ", "MD21/S2 ESDI", "A00"}, SDEV_FORCELUNS},
299 {{T_DIRECT, T_FIXED,
300 "MAXTOR ", "XT-3280 ", ""}, SDEV_NOLUNS},
301 {{T_DIRECT, T_FIXED,
302 "MAXTOR ", "XT-4380S ", ""}, SDEV_NOLUNS},
303 {{T_DIRECT, T_FIXED,
304 "MAXTOR ", "MXT-1240S ", ""}, SDEV_NOLUNS},
305 {{T_DIRECT, T_FIXED,
306 "MAXTOR ", "XT-4170S ", ""}, SDEV_NOLUNS},
307 {{T_DIRECT, T_FIXED,
308 "MAXTOR ", "XT-8760S ", ""}, SDEV_NOLUNS},
309 {{T_DIRECT, T_FIXED,
310 "MAXTOR ", "LXT-213S ", ""}, SDEV_NOLUNS},
311 {{T_DIRECT, T_FIXED,
312 "MAXTOR ", "LXT-213S SUN0207", ""}, SDEV_NOLUNS},
313 {{T_DIRECT, T_FIXED,
314 "MST ", "SnapLink ", ""}, SDEV_NOLUNS},
315 {{T_DIRECT, T_FIXED,
316 "QUANTUM ", "LPS525S ", ""}, SDEV_NOLUNS},
317 {{T_DIRECT, T_FIXED,
318 "QUANTUM ", "P105S 910-10-94x", ""}, SDEV_NOLUNS},
319 {{T_DIRECT, T_FIXED,
320 "QUANTUM ", "PD1225S ", ""}, SDEV_NOLUNS},
321 {{T_DIRECT, T_FIXED,
322 "QUANTUM ", "PD210S SUN0207", ""}, SDEV_NOLUNS},
323 {{T_DIRECT, T_FIXED,
324 "RODIME ", "RO3000S ", ""}, SDEV_NOLUNS},
325 {{T_DIRECT, T_FIXED,
326 "SEAGATE ", "ST157N ", ""}, SDEV_NOLUNS},
327 {{T_DIRECT, T_FIXED,
328 "SEAGATE ", "ST296 ", ""}, SDEV_NOLUNS},
329 {{T_DIRECT, T_FIXED,
330 "SEAGATE ", "ST296N ", ""}, SDEV_NOLUNS},
331 {{T_DIRECT, T_FIXED,
332 "TOSHIBA ", "MK538FB ", "6027"}, SDEV_NOLUNS},
333
334 /* XXX: QIC-36 tape behind Emulex adapter. Very broken. */
335 {{T_SEQUENTIAL, T_REMOV,
336 " ", " ", " "}, SDEV_NOLUNS},
337 {{T_SEQUENTIAL, T_REMOV,
338 "CALIPER ", "CP150 ", ""}, SDEV_NOLUNS},
339 {{T_SEQUENTIAL, T_REMOV,
340 "EXABYTE ", "EXB-8200 ", ""}, SDEV_NOLUNS},
341 {{T_SEQUENTIAL, T_REMOV,
342 "SONY ", "SDT-2000 ", "2.09"}, SDEV_NOLUNS},
343 {{T_SEQUENTIAL, T_REMOV,
344 "SONY ", "SDT-5000 ", "3."}, SDEV_NOSYNCWIDE},
345 {{T_SEQUENTIAL, T_REMOV,
346 "SONY ", "SDT-5200 ", "3."}, SDEV_NOLUNS},
347 {{T_SEQUENTIAL, T_REMOV,
348 "TANDBERG", " TDC 3600 ", ""}, SDEV_NOLUNS},
349 /* Following entry reported as a Tandberg 3600; ref. PR1933 */
350 {{T_SEQUENTIAL, T_REMOV,
351 "ARCHIVE ", "VIPER 150 21247", ""}, SDEV_NOLUNS},
352 {{T_SEQUENTIAL, T_REMOV,
353 "WANGTEK ", "5099ES SCSI", ""}, SDEV_NOLUNS},
354 {{T_SEQUENTIAL, T_REMOV,
355 "WANGTEK ", "5150ES SCSI", ""}, SDEV_NOLUNS},
356 {{T_SEQUENTIAL, T_REMOV,
357 "WangDAT ", "Model 1300 ", "02.4"}, SDEV_NOSYNCWIDE},
358 {{T_SEQUENTIAL, T_REMOV,
359 "WangDAT ", "Model 2600 ", "01.7"}, SDEV_NOSYNCWIDE},
360 {{T_SEQUENTIAL, T_REMOV,
361 "WangDAT ", "Model 3200 ", "02.2"}, SDEV_NOSYNCWIDE},
362 };
363
364 /*
365 * given a target and lu, ask the device what
366 * it is, and find the correct driver table
367 * entry.
368 */
369 void
370 scsi_probedev(scsi, target, lun)
371 struct scsibus_softc *scsi;
372 int target, lun;
373 {
374 struct scsi_link *sc_link;
375 static struct scsi_inquiry_data inqbuf;
376 struct scsi_quirk_inquiry_pattern *finger;
377 int priority;
378 u_int8_t type;
379 boolean removable;
380 char *dtype, *qtype;
381 char vendor[33], product[65], revision[17];
382 struct scsibus_attach_args sa;
383 struct cfdata *cf;
384
385 /* Skip this slot if it is already attached. */
386 if (scsi->sc_link[target][lun])
387 return;
388
389 sc_link = malloc(sizeof(*sc_link), M_DEVBUF, M_NOWAIT);
390 *sc_link = *scsi->adapter_link;
391 sc_link->target = target;
392 sc_link->lun = lun;
393 sc_link->device = &probe_switch;
394
395 /*
396 * Ask the device what it is
397 */
398 #ifdef SCSIDEBUG
399 if (target == DEBUGTARGET && lun == DEBUGLUN)
400 sc_link->flags |= DEBUGLEVEL;
401 #endif /* SCSIDEBUG */
402
403 (void) scsi_test_unit_ready(sc_link,
404 SCSI_AUTOCONF | SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_NOT_READY | SCSI_IGNORE_MEDIA_CHANGE);
405
406 #ifdef SCSI_2_DEF
407 /* some devices need to be told to go to SCSI2 */
408 /* However some just explode if you tell them this.. leave it out */
409 scsi_change_def(sc_link, SCSI_AUTOCONF | SCSI_SILENT);
410 #endif /* SCSI_2_DEF */
411
412 /* Now go ask the device all about itself. */
413 bzero(&inqbuf, sizeof(inqbuf));
414 if (scsi_inquire(sc_link, &inqbuf, SCSI_AUTOCONF) != 0)
415 goto bad;
416
417 {
418 int len = inqbuf.additional_length;
419 while (len < 3)
420 inqbuf.unused[len++] = '\0';
421 while (len < 3 + 28)
422 inqbuf.unused[len++] = ' ';
423 }
424
425 finger = (struct scsi_quirk_inquiry_pattern *)scsi_inqmatch(&inqbuf,
426 (caddr_t)scsi_quirk_patterns,
427 sizeof(scsi_quirk_patterns)/sizeof(scsi_quirk_patterns[0]),
428 sizeof(scsi_quirk_patterns[0]), &priority);
429 if (priority != 0)
430 sc_link->quirks |= finger->quirks;
431 if ((inqbuf.version & SID_ANSII) == 0 &&
432 (sc_link->quirks & SDEV_FORCELUNS) == 0)
433 sc_link->quirks |= SDEV_NOLUNS;
434
435 if ((sc_link->quirks & SDEV_NOLUNS) == 0)
436 scsi->moreluns |= (1 << target);
437
438 /*
439 * note what BASIC type of device it is
440 */
441 type = inqbuf.device & SID_TYPE;
442 removable = inqbuf.dev_qual2 & SID_REMOVABLE ? 1 : 0;
443
444 if (removable)
445 sc_link->flags |= SDEV_REMOVABLE;
446
447 /*
448 * Any device qualifier that has the top bit set (qualifier&4 != 0)
449 * is vendor specific and won't match in this switch.
450 */
451 dtype = 0;
452 switch (inqbuf.device & SID_QUAL) {
453 case SID_QUAL_LU_OK:
454 qtype = "";
455 break;
456
457 case SID_QUAL_LU_OFFLINE:
458 qtype = " offline";
459 break;
460
461 case SID_QUAL_RSVD:
462 case SID_QUAL_BAD_LU:
463 goto bad;
464
465 default:
466 qtype = "";
467 dtype = "vendor-unique";
468 break;
469 }
470 if (dtype == 0) {
471 switch (type) {
472 case T_DIRECT:
473 dtype = "direct";
474 break;
475 case T_SEQUENTIAL:
476 dtype = "sequential";
477 break;
478 case T_PRINTER:
479 dtype = "printer";
480 break;
481 case T_PROCESSOR:
482 dtype = "processor";
483 break;
484 case T_CDROM:
485 dtype = "cdrom";
486 break;
487 case T_WORM:
488 dtype = "worm";
489 break;
490 case T_SCANNER:
491 dtype = "scanner";
492 break;
493 case T_OPTICAL:
494 dtype = "optical";
495 break;
496 case T_CHANGER:
497 dtype = "changer";
498 break;
499 case T_COMM:
500 dtype = "communication";
501 break;
502 case T_NODEVICE:
503 goto bad;
504 default:
505 dtype = "unknown";
506 break;
507 }
508 }
509
510 scsi_strvis(vendor, inqbuf.vendor, 8);
511 scsi_strvis(product, inqbuf.product, 16);
512 scsi_strvis(revision, inqbuf.revision, 4);
513
514 printf("%s targ %d lun %d: <%s, %s, %s> SCSI%d %d/%s %s%s\n",
515 ((struct device *)sc_link->adapter_softc)->dv_xname,
516 target, lun, vendor, product, revision,
517 inqbuf.version & SID_ANSII, type, dtype,
518 removable ? "removable" : "fixed", qtype);
519
520 sa.sa_sc_link = sc_link;
521 sa.sa_inqbuf = &inqbuf;
522
523 if ((cf = config_search(scsibussubmatch, (struct device *)scsi, &sa)) != 0) {
524 scsi->sc_link[target][lun] = sc_link;
525 config_attach((struct device *)scsi, cf, &sa, NULL);
526 } else
527 goto bad;
528
529 return;
530
531 bad:
532 free(sc_link, M_DEVBUF);
533 return;
534 }
535
536 /*
537 * Return a priority based on how much of the inquiry data matches
538 * the patterns for the particular driver.
539 */
540 caddr_t
541 scsi_inqmatch(inqbuf, base, nmatches, matchsize, bestpriority)
542 struct scsi_inquiry_data *inqbuf;
543 caddr_t base;
544 int nmatches, matchsize;
545 int *bestpriority;
546 {
547 u_int8_t type;
548 boolean removable;
549 caddr_t bestmatch;
550
551 /* Include the qualifier to catch vendor-unique types. */
552 type = inqbuf->device;
553 removable = inqbuf->dev_qual2 & SID_REMOVABLE ? T_REMOV : T_FIXED;
554
555 for (*bestpriority = 0, bestmatch = 0; nmatches--; base += matchsize) {
556 struct scsi_inquiry_pattern *match = (void *)base;
557 int priority, len;
558
559 if (type != match->type)
560 continue;
561 if (removable != match->removable)
562 continue;
563 priority = 2;
564 len = strlen(match->vendor);
565 if (bcmp(inqbuf->vendor, match->vendor, len))
566 continue;
567 priority += len;
568 len = strlen(match->product);
569 if (bcmp(inqbuf->product, match->product, len))
570 continue;
571 priority += len;
572 len = strlen(match->revision);
573 if (bcmp(inqbuf->revision, match->revision, len))
574 continue;
575 priority += len;
576
577 #if SCSIDEBUG
578 printf("scsi_inqmatch: %d/%d/%d <%s, %s, %s>\n",
579 priority, match->type, match->removable,
580 match->vendor, match->product, match->revision);
581 #endif
582 if (priority > *bestpriority) {
583 *bestpriority = priority;
584 bestmatch = base;
585 }
586 }
587
588 return (bestmatch);
589 }
590