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