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