scsiconf.c revision 1.9.3.4 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.4 1993/11/24 20:33: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 /*
318 * We already know what the device is. We use a
319 * special matching routine which insists that the
320 * cfdata is of the right type rather than putting
321 * more intelligence in individual match routines for
322 * each high-level driver. We must have
323 * scsi_targmatch() do all of the comparisons, or we
324 * could get stuck in an infinite loop trying the same
325 * device repeatedly. We use the `fordriver' field of
326 * the scsi_link for now, rather than inventing a new
327 * structure just for the config_search().
328 */
329 if (bestmatch) {
330 sc_link->fordriver = bestmatch->attach_rtn;
331 if (config_found((struct device *)scsi,
332 sc_link, NULL)) {
333 scsi->sc_link[targ][lun] = sc_link;
334 sc_link = NULL; /* it's been used */
335 } else
336 printf("No matching config entry.\n");
337 } else
338 printf("No matching driver.\n");
339 if (!maybe_more)/* nothing suggests we'll find more */
340 break; /* nothing here, skip to next targ */
341 /* otherwise something says we should look further */
342 }
343 }
344 if (sc_link)
345 free(sc_link, M_TEMP);
346 return 0;
347 }
348
349 int
350 scsi_targmatch(parent, cf, aux)
351 struct device *parent;
352 struct cfdata *cf;
353 void *aux;
354 {
355 struct scsi_link *sc_link = aux;
356 void (*attach_rtn) () = sc_link->fordriver;
357
358 if (cf->cf_driver->cd_attach != attach_rtn)
359 return 0;
360 if (cf->cf_loc[0] != -1 && cf->cf_loc[0] != sc_link->target)
361 return 0;
362 if (cf->cf_loc[1] != -1 && cf->cf_loc[1] != sc_link->lun)
363 return 0;
364
365 return 1;
366 }
367
368 /*
369 * given a target and lu, ask the device what
370 * it is, and find the correct driver table
371 * entry.
372 */
373 struct scsidevs *
374 scsi_probedev(sc_link, maybe_more)
375 boolean *maybe_more;
376 struct scsi_link *sc_link;
377 {
378 u_int8 target = sc_link->target;
379 u_int8 lun = sc_link->lun;
380 struct scsi_adapter *scsi_adapter = sc_link->adapter;
381 struct scsidevs *bestmatch = NULL;
382 char *dtype = NULL, *desc;
383 char *qtype;
384 static struct scsi_inquiry_data inqbuf;
385 u_int32 len, qualifier, type;
386 boolean remov;
387 char manu[32];
388 char model[32];
389 char version[32];
390
391 bzero(&inqbuf, sizeof(inqbuf));
392 /*
393 * Ask the device what it is
394 */
395 #ifdef SCSIDEBUG
396 if (target == DEBUGTARG && lu == DEBUGLUN)
397 sc_link->flags |= DEBUGLEVEL;
398 else
399 sc_link->flags &= ~(SDEV_DB1 | SDEV_DB2 | SDEV_DB3 | SDEV_DB4);
400 #endif /* SCSIDEBUG */
401 /* catch unit attn */
402 scsi_test_unit_ready(sc_link, SCSI_NOSLEEP | SCSI_NOMASK | SCSI_SILENT);
403 #ifdef DOUBTFULL
404 switch (scsi_test_unit_ready(sc_link, SCSI_NOSLEEP | SCSI_NOMASK | SCSI_SILENT)) {
405 case 0: /* said it WAS ready */
406 case EBUSY: /* replied 'NOT READY' but WAS present, continue */
407 case ENXIO:
408 break;
409 case EIO: /* device timed out */
410 case EINVAL: /* Lun not supported */
411 default:
412 return NULL;
413
414 }
415 #endif /*DOUBTFULL*/
416 #ifdef SCSI_2_DEF
417 /* some devices need to be told to go to SCSI2 */
418 /* However some just explode if you tell them this.. leave it out */
419 scsi_change_def(sc_link, SCSI_NOSLEEP | SCSI_NOMASK | SCSI_SILENT);
420 #endif /*SCSI_2_DEF */
421
422 /* Now go ask the device all about itself */
423 if (scsi_inquire(sc_link, &inqbuf, SCSI_NOSLEEP | SCSI_NOMASK) != 0)
424 return NULL;
425
426 /*
427 * note what BASIC type of device it is
428 */
429 type = inqbuf.device & SID_TYPE;
430 qualifier = inqbuf.device & SID_QUAL;
431 remov = inqbuf.dev_qual2 & SID_REMOVABLE;
432
433 /*
434 * Any device qualifier that has the top bit set (qualifier&4 != 0)
435 * is vendor specific and won't match in this switch.
436 */
437 switch (qualifier) {
438 case SID_QUAL_LU_OK:
439 qtype = "";
440 break;
441
442 case SID_QUAL_LU_OFFLINE:
443 qtype = ", Unit not Connected!";
444 break;
445
446 case SID_QUAL_RSVD:
447 qtype = ", Reserved Peripheral Qualifier!";
448 *maybe_more = 1;
449 return NULL;
450 break;
451
452 case SID_QUAL_BAD_LU:
453 /*
454 * Check for a non-existent unit. If the device is returning
455 * this much, then we must set the flag that has
456 * the searchers keep looking on other luns.
457 */
458 qtype = ", The Target can't support this Unit!";
459 *maybe_more = 1;
460 return NULL;
461
462 default:
463 dtype = "vendor specific";
464 qtype = "";
465 *maybe_more = 1;
466 break;
467 }
468 if (dtype == 0) {
469 switch (type) {
470 case T_DIRECT:
471 dtype = "direct";
472 break;
473 case T_SEQUENTIAL:
474 dtype = "sequential";
475 break;
476 case T_PRINTER:
477 dtype = "printer";
478 break;
479 case T_PROCESSOR:
480 dtype = "processor";
481 break;
482 case T_READONLY:
483 dtype = "readonly";
484 break;
485 case T_WORM:
486 dtype = "worm";
487 break;
488 case T_SCANNER:
489 dtype = "scanner";
490 break;
491 case T_OPTICAL:
492 dtype = "optical";
493 break;
494 case T_CHANGER:
495 dtype = "changer";
496 break;
497 case T_COMM:
498 dtype = "communication";
499 break;
500 case T_NODEVICE:
501 *maybe_more = 1;
502 return NULL;
503 default:
504 dtype = NULL;
505 break;
506 }
507 }
508
509 /*
510 * Then if it's advanced enough, more detailed
511 * information
512 */
513 if ((inqbuf.version & SID_ANSII) > 0) {
514 if ((len = inqbuf.additional_length
515 + ((char *) inqbuf.unused
516 - (char *) &inqbuf))
517 > (sizeof(struct scsi_inquiry_data) - 1))
518 len = sizeof(struct scsi_inquiry_data) - 1;
519 desc = inqbuf.vendor;
520 desc[len - (desc - (char *) &inqbuf)] = 0;
521 strncpy(manu, inqbuf.vendor, 8);
522 manu[8] = 0;
523 strncpy(model, inqbuf.product, 16);
524 model[16] = 0;
525 strncpy(version, inqbuf.revision, 4);
526 version[4] = 0;
527 } else
528 /*
529 * If not advanced enough, use default values
530 */
531 {
532 desc = "early protocol device";
533 strncpy(manu, "unknown", 8);
534 strncpy(model, "unknown", 16);
535 strncpy(version, "????", 4);
536 }
537 printf("%s targ %d lun %d: <%s%s%s> SCSI%d ",
538 ((struct device *)sc_link->adapter_softc)->dv_xname,
539 target, lun, manu, model, version,
540 inqbuf.version & SID_ANSII);
541 if (dtype)
542 printf("%s", dtype);
543 else
544 printf("type %d", type);
545 printf(" %s\n", remov ? "removable" : "fixed");
546 if (qtype[0])
547 printf("%s targ %d lun %d: qualifier %d(%s)\n",
548 ((struct device *)sc_link->adapter_softc)->dv_xname,
549 target, lun, qualifier, qtype);
550
551 /*
552 * Try make as good a match as possible with
553 * available sub drivers
554 */
555 bestmatch = selectdev(qualifier, type, remov ? T_REMOV : T_FIXED,
556 manu, model, version);
557 if (bestmatch && bestmatch->flags & SC_MORE_LUS)
558 *maybe_more = 1;
559 return bestmatch;
560 }
561
562 /*
563 * Try make as good a match as possible with
564 * available sub drivers
565 */
566 struct scsidevs *
567 selectdev(qualifier, type, remov, manu, model, rev)
568 u_int32 qualifier, type;
569 boolean remov;
570 char *manu, *model, *rev;
571 {
572 u_int32 numents = (sizeof(knowndevs) / sizeof(struct scsidevs)) - 1;
573 u_int32 count = 0;
574 u_int32 bestmatches = 0;
575 struct scsidevs *bestmatch = (struct scsidevs *) 0;
576 struct scsidevs *thisentry = knowndevs;
577
578 type |= qualifier; /* why? */
579
580 thisentry--;
581 while (count++ < numents) {
582 thisentry++;
583 if (type != thisentry->type)
584 continue;
585 if (bestmatches < 1) {
586 bestmatches = 1;
587 bestmatch = thisentry;
588 }
589 if (remov != thisentry->removable)
590 continue;
591 if (bestmatches < 2) {
592 bestmatches = 2;
593 bestmatch = thisentry;
594 }
595 if (thisentry->flags & SC_SHOWME)
596 printf("\n%s-\n%s-", thisentry->manufacturer, manu);
597 if (strcmp(thisentry->manufacturer, manu))
598 continue;
599 if (bestmatches < 3) {
600 bestmatches = 3;
601 bestmatch = thisentry;
602 }
603 if (thisentry->flags & SC_SHOWME)
604 printf("\n%s-\n%s-", thisentry->model, model);
605 if (strcmp(thisentry->model, model))
606 continue;
607 if (bestmatches < 4) {
608 bestmatches = 4;
609 bestmatch = thisentry;
610 }
611 if (thisentry->flags & SC_SHOWME)
612 printf("\n%s-\n%s-", thisentry->version, rev);
613 if (strcmp(thisentry->version, rev))
614 continue;
615 if (bestmatches < 5) {
616 bestmatches = 5;
617 bestmatch = thisentry;
618 break;
619 }
620 }
621 #if NUK > 0
622 if (!bestmatch)
623 bestmatch = &unknowndev;
624 #endif
625 return bestmatch;
626 }
627