scsiconf.c revision 1.9.3.3 1 /*
2 * Written by Julian Elischer (julian (at) tfs.com)
3 * for TRW Financial Systems for use under the MACH(2.5) operating system.
4 *
5 * TRW Financial Systems, in accordance with their agreement with Carnegie
6 * Mellon University, makes this software available to CMU to distribute
7 * or use in any manner that they see fit as long as this message is kept with
8 * the software. For this reason TFS also grants any other persons or
9 * organisations permission to use or modify this software.
10 *
11 * TFS supplies this software to be publicly redistributed
12 * on the understanding that TFS is not responsible for the correct
13 * functioning of this software in any circumstances.
14 *
15 * Ported to run under 386BSD by Julian Elischer (julian (at) tfs.com) Sept 1992
16 *
17 * $Id: scsiconf.c,v 1.9.3.3 1993/11/24 19:19:44 mycroft Exp $
18 */
19
20 #include <sys/types.h>
21 #include <sys/param.h>
22 #include <sys/systm.h>
23 #include <sys/malloc.h>
24 #include <sys/device.h>
25
26 #include "st.h"
27 #include "sd.h"
28 #include "ch.h"
29 #include "cd.h"
30 #include "uk.h"
31 #include "su.h"
32
33 #include <scsi/scsi_all.h>
34 #include <scsi/scsiconf.h>
35
36 #ifdef TFS
37 #include "bll.h"
38 #include "cals.h"
39 #include "kil.h"
40 #include "scan.h"
41 #else /* TFS */
42 #define NBLL 0
43 #define NCALS 0
44 #define NKIL 0
45 #define NSCAN 0
46 #endif /* TFS */
47
48 #if NSD > 0
49 extern sdattach();
50 #endif /* NSD */
51 #if NST > 0
52 extern stattach();
53 #endif /* NST */
54 #if NCH > 0
55 extern chattach();
56 #endif /* NCH */
57 #if NCD > 0
58 extern cdattach();
59 #endif /* NCD */
60 #if NBLL > 0
61 extern bllattach();
62 #endif /* NBLL */
63 #if NCALS > 0
64 extern calsattach();
65 #endif /* NCALS */
66 #if NKIL > 0
67 extern kil_attach();
68 #endif /* NKIL */
69 #if NUK > 0
70 extern ukattach();
71 #endif /* NUK */
72
73 /*
74 * The structure of known drivers for autoconfiguration
75 */
76 struct scsidevs {
77 u_int32 type;
78 boolean removable;
79 char *manufacturer;
80 char *model;
81 char *version;
82 int(*attach_rtn) ();
83 char *devname;
84 char flags; /* 1 show my comparisons during boot(debug) */
85 };
86
87 #define SC_SHOWME 0x01
88 #define SC_ONE_LU 0x00
89 #define SC_MORE_LUS 0x02
90
91 #if NUK > 0
92 static struct scsidevs unknowndev = {
93 -1, 0, "standard", "any"
94 ,"any", ukattach, "uk", SC_MORE_LUS
95 };
96 #endif /*NUK*/
97 static struct scsidevs knowndevs[] =
98 {
99 #if NSD > 0
100 {
101 T_DIRECT, T_FIXED, "standard", "any"
102 ,"any", sdattach, "sd", SC_ONE_LU
103 },
104 {
105 T_DIRECT, T_FIXED, "MAXTOR ", "XT-4170S "
106 ,"B5A ", sdattach, "mx1", SC_ONE_LU
107 },
108 #endif /* NSD */
109 #if NST > 0
110 {
111 T_SEQUENTIAL, T_REMOV, "standard", "any"
112 ,"any", stattach, "st", SC_ONE_LU
113 },
114 #endif /* NST */
115 #if NCALS > 0
116 {
117 T_PROCESSOR, T_FIXED, "standard", "any"
118 ,"any", calsattach, "cals", SC_MORE_LUS
119 },
120 #endif /* NCALS */
121 #if NCH > 0
122 {
123 T_CHANGER, T_REMOV, "standard", "any"
124 ,"any", chattach, "ch", SC_ONE_LU
125 },
126 #endif /* NCH */
127 #if NCD > 0
128 #ifndef UKTEST /* make cdroms unrecognised to test the uk driver */
129 {
130 T_READONLY, T_REMOV, "SONY ", "CD-ROM CDU-8012 "
131 ,"3.1a", cdattach, "cd", SC_ONE_LU
132 },
133 {
134 T_READONLY, T_REMOV, "PIONEER ", "CD-ROM DRM-600 "
135 ,"any", cdattach, "cd", SC_MORE_LUS
136 },
137 #endif
138 #endif /* NCD */
139 #if NBLL > 0
140 {
141 T_PROCESSOR, T_FIXED, "AEG ", "READER "
142 ,"V1.0", bllattach, "bll", SC_MORE_LUS
143 },
144 #endif /* NBLL */
145 #if NKIL > 0
146 {
147 T_SCANNER, T_FIXED, "KODAK ", "IL Scanner 900 "
148 ,"any", kil_attach, "kil", SC_ONE_LU
149 },
150 #endif /* NKIL */
151
152 {
153 0
154 }
155 };
156
157 /*
158 * Declarations
159 */
160 struct scsidevs *scsi_probedev();
161 struct scsidevs *selectdev();
162 int scsi_probe_bus __P((int bus, int targ, int lun));
163
164 struct scsi_device probe_switch =
165 {
166 NULL,
167 NULL,
168 NULL,
169 NULL,
170 "probe",
171 0,
172 };
173
174 int scsibusmatch __P((struct device *, struct cfdata *, void *));
175 void scsibusattach __P((struct device *, struct device *, void *));
176
177 struct cfdriver scsibuscd =
178 {
179 NULL,
180 "scsibus",
181 scsibusmatch,
182 scsibusattach,
183 DV_DULL,
184 sizeof(struct scsibus_data)
185 };
186
187 int
188 scsibusmatch(parent, cf, aux)
189 struct device *parent;
190 struct cfdata *cf;
191 void *aux;
192 {
193
194 return 1;
195 }
196
197 /*
198 * The routine called by the adapter boards to get all their
199 * devices configured in.
200 */
201 void
202 scsibusattach(parent, self, aux)
203 struct device *parent, *self;
204 void *aux;
205 {
206 struct scsibus_data *sb = (struct scsibus_data *)self;
207 struct scsi_link *sc_link_proto = aux;
208
209 sc_link_proto->scsibus = sb->sc_dev.dv_unit;
210 sb->adapter_link = sc_link_proto;
211 printf("\n");
212
213 #if defined(SCSI_DELAY) && SCSI_DELAY > 2
214 printf("%s: waiting for scsi devices to settle\n",
215 sb->sc_dev.dv_xname);
216 #else /* SCSI_DELAY > 2 */
217 #undef SCSI_DELAY
218 #define SCSI_DELAY 2
219 #endif /* SCSI_DELAY */
220 DELAY(1000000 * SCSI_DELAY);
221
222 scsi_probe_bus(sb->sc_dev.dv_unit, -1, -1);
223 }
224
225 /*
226 * Probe the requested scsi bus. It must be already set up.
227 * -1 requests all set up scsi busses.
228 * targ and lun optionally narrow the search if not -1
229 */
230 int
231 scsi_probe_busses(bus, targ, lun)
232 int bus, targ, lun;
233 {
234
235 if (bus == -1) {
236 for (bus = 0; bus < scsibuscd.cd_ndevs; bus++)
237 if (scsibuscd.cd_devs[bus])
238 scsi_probe_bus(bus, targ, lun);
239 return 0;
240 } else {
241 return scsi_probe_bus(bus, targ, lun);
242 }
243 }
244
245 /*
246 * Probe the requested scsi bus. It must be already set up.
247 * targ and lun optionally narrow the search if not -1
248 */
249 int
250 scsi_probe_bus(bus, targ, lun)
251 int bus, targ, lun;
252 {
253 struct scsibus_data *scsi;
254 int maxtarg, mintarg, maxlun, minlun;
255 struct scsi_link *sc_link_proto;
256 u_int8 scsi_addr ;
257 struct scsidevs *bestmatch = NULL;
258 struct scsi_link *sc_link = NULL;
259 boolean maybe_more;
260
261 if (bus < 0 || bus >= scsibuscd.cd_ndevs)
262 return ENXIO;
263 scsi = scsibuscd.cd_devs[bus];
264 if (!scsi)
265 return ENXIO;
266
267 sc_link_proto = scsi->adapter_link;
268 scsi_addr = sc_link_proto->adapter_targ;
269
270 if (targ == -1) {
271 maxtarg = 7;
272 mintarg = 0;
273 } else {
274 if (targ < 0 || targ > 7)
275 return EINVAL;
276 maxtarg = mintarg = targ;
277 }
278
279 if (lun == -1) {
280 maxlun = 7;
281 minlun = 0;
282 } else {
283 if (lun < 0 || lun > 7)
284 return EINVAL;
285 maxlun = minlun = lun;
286 }
287
288 for (targ = mintarg; targ <= maxtarg; targ++) {
289 maybe_more = 0; /* by default only check 1 lun */
290 #if 0 /* XXXX */
291 if (targ == scsi_addr)
292 continue;
293 #endif
294 for (lun = minlun; lun <= maxlun; lun++) {
295 /*
296 * The spot appears to already have something
297 * linked in, skip past it. Must be doing a 'reprobe'
298 */
299 if (scsi->sc_link[targ][lun]) {
300 /* don't do this one, but check other luns */
301 maybe_more = 1;
302 continue;
303 }
304 /*
305 * If we presently don't have a link block
306 * then allocate one to use while probing
307 */
308 if (!sc_link) {
309 sc_link = malloc(sizeof(*sc_link), M_TEMP, M_NOWAIT);
310 *sc_link = *sc_link_proto; /* struct copy */
311 sc_link->opennings = 1;
312 sc_link->device = &probe_switch;
313 }
314 sc_link->target = targ;
315 sc_link->lun = lun;
316 bestmatch = scsi_probedev(sc_link, &maybe_more);
317 if (!bestmatch)
318 continue;
319 if (!scsi_attachdev(scsi, sc_link,
320 bestmatch->attach_rtn))
321 continue;
322 scsi->sc_link[targ][lun] = sc_link;
323 sc_link = NULL; /* it's been used */
324 if (!(maybe_more)) /* nothing suggests we'll find more */
325 break; /* nothing here, skip to next targ */
326 /* otherwise something says we should look further */
327 }
328 }
329 if (sc_link)
330 free(sc_link, M_TEMP);
331 return 0;
332 }
333
334 int
335 scsi_targmatch(parent, cf, aux)
336 struct device *parent;
337 struct cfdata *cf;
338 void *aux;
339 {
340 struct scsi_link *sc_link = aux;
341 void (*attach_rtn) () = sc_link->fordriver;
342
343 if (cf->cf_driver->cd_attach != attach_rtn)
344 return 0;
345 if (cf->cf_loc[0] != -1 && cf->cf_loc[0] != sc_link->target)
346 return 0;
347 if (cf->cf_loc[1] != -1 && cf->cf_loc[1] != sc_link->lun)
348 return 0;
349
350 return 1;
351 }
352
353 int
354 scsi_attachdev(scsi, sc_link, attach_rtn)
355 struct scsibus_data *scsi;
356 struct scsi_link *sc_link;
357 void (*attach_rtn) ();
358 {
359 /*
360 * We already know what the device is. We use a special matching
361 * routine which insists that the cfdata is of the right type rather
362 * than putting more intelligence in individual match routines for
363 * each high-level driver. We must have scsi_targmatch() do all of
364 * the comparisons, or we could get stuck in an infinite loop trying
365 * the same device over and over. We use the `fordriver' field of
366 * the scsi_link for now, rather than inventing a new structure just
367 * for the config_search().
368 */
369 sc_link->fordriver = attach_rtn;
370 if (config_found((struct device *)scsi, sc_link, NULL))
371 return 1;
372
373 printf("No matching config file entry.\n");
374 return 0;
375 }
376
377 /*
378 * given a target and lu, ask the device what
379 * it is, and find the correct driver table
380 * entry.
381 */
382 struct scsidevs *
383 scsi_probedev(sc_link, maybe_more)
384 boolean *maybe_more;
385 struct scsi_link *sc_link;
386 {
387 u_int8 target = sc_link->target;
388 u_int8 lun = sc_link->lun;
389 struct scsi_adapter *scsi_adapter = sc_link->adapter;
390 struct scsidevs *bestmatch = NULL;
391 char *dtype = NULL, *desc;
392 char *qtype;
393 static struct scsi_inquiry_data inqbuf;
394 u_int32 len, qualifier, type;
395 boolean remov;
396 char manu[32];
397 char model[32];
398 char version[32];
399
400 bzero(&inqbuf, sizeof(inqbuf));
401 /*
402 * Ask the device what it is
403 */
404 #ifdef SCSIDEBUG
405 if (target == DEBUGTARG && lu == DEBUGLUN)
406 sc_link->flags |= DEBUGLEVEL;
407 else
408 sc_link->flags &= ~(SDEV_DB1 | SDEV_DB2 | SDEV_DB3 | SDEV_DB4);
409 #endif /* SCSIDEBUG */
410 /* catch unit attn */
411 scsi_test_unit_ready(sc_link, SCSI_NOSLEEP | SCSI_NOMASK | SCSI_SILENT);
412 #ifdef DOUBTFULL
413 switch (scsi_test_unit_ready(sc_link, SCSI_NOSLEEP | SCSI_NOMASK | SCSI_SILENT)) {
414 case 0: /* said it WAS ready */
415 case EBUSY: /* replied 'NOT READY' but WAS present, continue */
416 case ENXIO:
417 break;
418 case EIO: /* device timed out */
419 case EINVAL: /* Lun not supported */
420 default:
421 return NULL;
422
423 }
424 #endif /*DOUBTFULL*/
425 #ifdef SCSI_2_DEF
426 /* some devices need to be told to go to SCSI2 */
427 /* However some just explode if you tell them this.. leave it out */
428 scsi_change_def(sc_link, SCSI_NOSLEEP | SCSI_NOMASK | SCSI_SILENT);
429 #endif /*SCSI_2_DEF */
430
431 /* Now go ask the device all about itself */
432 if (scsi_inquire(sc_link, &inqbuf, SCSI_NOSLEEP | SCSI_NOMASK) != 0)
433 return NULL;
434
435 /*
436 * note what BASIC type of device it is
437 */
438 type = inqbuf.device & SID_TYPE;
439 qualifier = inqbuf.device & SID_QUAL;
440 remov = inqbuf.dev_qual2 & SID_REMOVABLE;
441
442 /*
443 * Any device qualifier that has the top bit set (qualifier&4 != 0)
444 * is vendor specific and won't match in this switch.
445 */
446 switch (qualifier) {
447 case SID_QUAL_LU_OK:
448 qtype = "";
449 break;
450
451 case SID_QUAL_LU_OFFLINE:
452 qtype = ", Unit not Connected!";
453 break;
454
455 case SID_QUAL_RSVD:
456 qtype = ", Reserved Peripheral Qualifier!";
457 *maybe_more = 1;
458 return NULL;
459 break;
460
461 case SID_QUAL_BAD_LU:
462 /*
463 * Check for a non-existent unit. If the device is returning
464 * this much, then we must set the flag that has
465 * the searchers keep looking on other luns.
466 */
467 qtype = ", The Target can't support this Unit!";
468 *maybe_more = 1;
469 return NULL;
470
471 default:
472 dtype = "vendor specific";
473 qtype = "";
474 *maybe_more = 1;
475 break;
476 }
477 if (dtype == 0) {
478 switch (type) {
479 case T_DIRECT:
480 dtype = "direct";
481 break;
482 case T_SEQUENTIAL:
483 dtype = "sequential";
484 break;
485 case T_PRINTER:
486 dtype = "printer";
487 break;
488 case T_PROCESSOR:
489 dtype = "processor";
490 break;
491 case T_READONLY:
492 dtype = "readonly";
493 break;
494 case T_WORM:
495 dtype = "worm";
496 break;
497 case T_SCANNER:
498 dtype = "scanner";
499 break;
500 case T_OPTICAL:
501 dtype = "optical";
502 break;
503 case T_CHANGER:
504 dtype = "changer";
505 break;
506 case T_COMM:
507 dtype = "communication";
508 break;
509 case T_NODEVICE:
510 *maybe_more = 1;
511 return NULL;
512 default:
513 dtype = NULL;
514 break;
515 }
516 }
517
518 /*
519 * Then if it's advanced enough, more detailed
520 * information
521 */
522 if ((inqbuf.version & SID_ANSII) > 0) {
523 if ((len = inqbuf.additional_length
524 + ((char *) inqbuf.unused
525 - (char *) &inqbuf))
526 > (sizeof(struct scsi_inquiry_data) - 1))
527 len = sizeof(struct scsi_inquiry_data) - 1;
528 desc = inqbuf.vendor;
529 desc[len - (desc - (char *) &inqbuf)] = 0;
530 strncpy(manu, inqbuf.vendor, 8);
531 manu[8] = 0;
532 strncpy(model, inqbuf.product, 16);
533 model[16] = 0;
534 strncpy(version, inqbuf.revision, 4);
535 version[4] = 0;
536 } else
537 /*
538 * If not advanced enough, use default values
539 */
540 {
541 desc = "early protocol device";
542 strncpy(manu, "unknown", 8);
543 strncpy(model, "unknown", 16);
544 strncpy(version, "????", 4);
545 }
546 printf("%s targ %d lun %d: <%s%s%s> SCSI%d ",
547 ((struct device *)sc_link->adapter_softc)->dv_xname,
548 target, lun, manu, model, version,
549 inqbuf.version & SID_ANSII);
550 if (dtype)
551 printf("%s", dtype);
552 else
553 printf("type %d", type);
554 printf(" %s\n", remov ? "removable" : "fixed");
555 if (qtype[0])
556 printf("%s targ %d lun %d: qualifier %d(%s)\n",
557 ((struct device *)sc_link->adapter_softc)->dv_xname,
558 target, lun, qualifier, qtype);
559
560 /*
561 * Try make as good a match as possible with
562 * available sub drivers
563 */
564 bestmatch = selectdev(qualifier, type, remov ? T_REMOV : T_FIXED,
565 manu, model, version);
566 if (bestmatch && bestmatch->flags & SC_MORE_LUS)
567 *maybe_more = 1;
568 return bestmatch;
569 }
570
571 /*
572 * Try make as good a match as possible with
573 * available sub drivers
574 */
575 struct scsidevs *
576 selectdev(qualifier, type, remov, manu, model, rev)
577 u_int32 qualifier, type;
578 boolean remov;
579 char *manu, *model, *rev;
580 {
581 u_int32 numents = (sizeof(knowndevs) / sizeof(struct scsidevs)) - 1;
582 u_int32 count = 0;
583 u_int32 bestmatches = 0;
584 struct scsidevs *bestmatch = (struct scsidevs *) 0;
585 struct scsidevs *thisentry = knowndevs;
586
587 type |= qualifier; /* why? */
588
589 thisentry--;
590 while (count++ < numents) {
591 thisentry++;
592 if (type != thisentry->type)
593 continue;
594 if (bestmatches < 1) {
595 bestmatches = 1;
596 bestmatch = thisentry;
597 }
598 if (remov != thisentry->removable)
599 continue;
600 if (bestmatches < 2) {
601 bestmatches = 2;
602 bestmatch = thisentry;
603 }
604 if (thisentry->flags & SC_SHOWME)
605 printf("\n%s-\n%s-", thisentry->manufacturer, manu);
606 if (strcmp(thisentry->manufacturer, manu))
607 continue;
608 if (bestmatches < 3) {
609 bestmatches = 3;
610 bestmatch = thisentry;
611 }
612 if (thisentry->flags & SC_SHOWME)
613 printf("\n%s-\n%s-", thisentry->model, model);
614 if (strcmp(thisentry->model, model))
615 continue;
616 if (bestmatches < 4) {
617 bestmatches = 4;
618 bestmatch = thisentry;
619 }
620 if (thisentry->flags & SC_SHOWME)
621 printf("\n%s-\n%s-", thisentry->version, rev);
622 if (strcmp(thisentry->version, rev))
623 continue;
624 if (bestmatches < 5) {
625 bestmatches = 5;
626 bestmatch = thisentry;
627 break;
628 }
629 }
630 if (!bestmatch) {
631 #if NUK > 0
632 bestmatch = &unknowndev;
633 #else
634 printf("No explicit device driver match.\n");
635 #endif
636 }
637 return bestmatch;
638 }
639