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