xref: /netbsd-src/sys/dev/scsipi/scsiconf.c (revision dc306354b0b29af51801a7632f1e95265a68cd81)
1 /*	$NetBSD: scsiconf.c,v 1.122 1998/12/10 18:13:29 mjacob Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Charles M. Hannum; Jason R. Thorpe of the Numerical Aerospace
9  * Simulation Facility, NASA Ames Research Center.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *        This product includes software developed by the NetBSD
22  *        Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 /*
41  * Originally written by Julian Elischer (julian@tfs.com)
42  * for TRW Financial Systems for use under the MACH(2.5) operating system.
43  *
44  * TRW Financial Systems, in accordance with their agreement with Carnegie
45  * Mellon University, makes this software available to CMU to distribute
46  * or use in any manner that they see fit as long as this message is kept with
47  * the software. For this reason TFS also grants any other persons or
48  * organisations permission to use or modify this software.
49  *
50  * TFS supplies this software to be publicly redistributed
51  * on the understanding that TFS is not responsible for the correct
52  * functioning of this software in any circumstances.
53  *
54  * Ported to run under 386BSD by Julian Elischer (julian@tfs.com) Sept 1992
55  */
56 
57 #include <sys/types.h>
58 #include <sys/param.h>
59 #include <sys/systm.h>
60 #include <sys/malloc.h>
61 #include <sys/device.h>
62 #include <sys/conf.h>
63 #include <sys/fcntl.h>
64 #include <sys/scsiio.h>
65 
66 #include <dev/scsipi/scsi_all.h>
67 #include <dev/scsipi/scsipi_all.h>
68 #include <dev/scsipi/scsiconf.h>
69 
70 #include "locators.h"
71 
72 #if 0
73 #if NCALS > 0
74 	{ T_PROCESSOR, T_FIXED, 1,
75 	  0, 0, 0 },
76 #endif	/* NCALS */
77 #if NBLL > 0
78 	{ T_PROCESSOR, T_FIXED, 1,
79 	  "AEG     ", "READER          ", "V1.0" },
80 #endif	/* NBLL */
81 #if NKIL > 0
82 	{ T_SCANNER, T_FIXED, 0,
83 	  "KODAK   ", "IL Scanner 900  ", 0 },
84 #endif	/* NKIL */
85 #endif
86 
87 /*
88  * Declarations
89  */
90 int scsi_probedev __P((struct scsibus_softc *, int, int));
91 int scsi_probe_bus __P((int bus, int target, int lun));
92 
93 struct scsipi_device probe_switch = {
94 	NULL,
95 	NULL,
96 	NULL,
97 	NULL,
98 };
99 
100 int scsibusmatch __P((struct device *, struct cfdata *, void *));
101 void scsibusattach __P((struct device *, struct device *, void *));
102 int scsibussubmatch __P((struct device *, struct cfdata *, void *));
103 
104 struct cfattach scsibus_ca = {
105 	sizeof(struct scsibus_softc), scsibusmatch, scsibusattach
106 };
107 
108 extern struct cfdriver scsibus_cd;
109 
110 int scsibusprint __P((void *, const char *));
111 
112 cdev_decl(scsibus);
113 
114 int
115 scsiprint(aux, pnp)
116 	void *aux;
117 	const char *pnp;
118 {
119 	struct scsipi_link *l = aux;
120 
121 	/* only "scsibus"es can attach to "scsi"s; easy. */
122 	if (pnp)
123 		printf("scsibus at %s", pnp);
124 
125 	/* don't print channel if the controller says there can be only one. */
126 	if (l->scsipi_scsi.channel != SCSI_CHANNEL_ONLY_ONE)
127 		printf(" channel %d", l->scsipi_scsi.channel);
128 
129 	return (UNCONF);
130 }
131 
132 int
133 scsibusmatch(parent, cf, aux)
134 	struct device *parent;
135 	struct cfdata *cf;
136 	void *aux;
137 {
138 	struct scsipi_link *l = aux;
139 	int channel;
140 
141 	/*
142 	 * Allow single-channel controllers to specify their channel
143 	 * in a special way, so that it's not printed.
144 	 */
145 	channel = (l->scsipi_scsi.channel != SCSI_CHANNEL_ONLY_ONE) ?
146 	    l->scsipi_scsi.channel : 0;
147 
148 	if (cf->cf_loc[SCSICF_CHANNEL] != channel &&
149 	    cf->cf_loc[SCSICF_CHANNEL] != SCSICF_CHANNEL_DEFAULT)
150 		return (0);
151 
152 	return (1);
153 }
154 
155 /*
156  * The routine called by the adapter boards to get all their
157  * devices configured in.
158  */
159 void
160 scsibusattach(parent, self, aux)
161 	struct device *parent, *self;
162 	void *aux;
163 {
164 	struct scsibus_softc *sb = (struct scsibus_softc *)self;
165 	struct scsipi_link *sc_link_proto = aux;
166 	size_t nbytes;
167 	int i;
168 
169 	sc_link_proto->scsipi_scsi.scsibus = sb->sc_dev.dv_unit;
170 	sc_link_proto->scsipi_cmd = scsi_scsipi_cmd;
171 	sc_link_proto->scsipi_interpret_sense = scsipi_interpret_sense;
172 	sc_link_proto->sc_print_addr = scsi_print_addr;
173 
174 	sb->adapter_link = sc_link_proto;
175 	sb->sc_maxtarget = sc_link_proto->scsipi_scsi.max_target;
176 	sb->sc_maxlun = sc_link_proto->scsipi_scsi.max_lun;
177 	printf(": %d targets, %d luns per target\n",
178 	    sb->sc_maxtarget + 1, sb->sc_maxlun + 1);
179 
180 	/* Initialize shared data. */
181 	scsipi_init();
182 
183 	nbytes = (sb->sc_maxtarget + 1) * sizeof(struct scsipi_link **);
184 	sb->sc_link = (struct scsipi_link ***)malloc(nbytes, M_DEVBUF,
185 	    M_NOWAIT);
186 	if (sb->sc_link == NULL)
187 		panic("scsibusattach: can't allocate target links");
188 
189 	nbytes = (((int) sb->sc_maxlun) + 1) * sizeof(struct scsipi_link *);
190 	for (i = 0; i <= sb->sc_maxtarget; i++) {
191 		sb->sc_link[i] = (struct scsipi_link **)malloc(nbytes,
192 		    M_DEVBUF, M_NOWAIT);
193 		if (sb->sc_link[i] == NULL)
194 			panic("scsibusattach: can't allocate lun links");
195 		bzero(sb->sc_link[i], nbytes);
196 	}
197 
198 #if defined(SCSI_DELAY) && SCSI_DELAY > 2
199 	printf("%s: waiting for scsi devices to settle\n",
200 	    sb->sc_dev.dv_xname);
201 #else	/* SCSI_DELAY > 2 */
202 #undef	SCSI_DELAY
203 #define SCSI_DELAY 2
204 #endif	/* SCSI_DELAY */
205 	delay(1000000 * SCSI_DELAY);
206 
207 	scsi_probe_bus(sb->sc_dev.dv_unit, -1, -1);
208 }
209 
210 int
211 scsibussubmatch(parent, cf, aux)
212 	struct device *parent;
213 	struct cfdata *cf;
214 	void *aux;
215 {
216 	struct scsipibus_attach_args *sa = aux;
217 	struct scsipi_link *sc_link = sa->sa_sc_link;
218 
219 	if (cf->cf_loc[SCSIBUSCF_TARGET] != SCSIBUSCF_TARGET_DEFAULT &&
220 	    cf->cf_loc[SCSIBUSCF_TARGET] != sc_link->scsipi_scsi.target)
221 		return (0);
222 	if (cf->cf_loc[SCSIBUSCF_LUN] != SCSIBUSCF_LUN_DEFAULT &&
223 	    cf->cf_loc[SCSIBUSCF_LUN] != sc_link->scsipi_scsi.lun)
224 		return (0);
225 	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
226 }
227 
228 /*
229  * Probe the requested scsi bus. It must be already set up.
230  * -1 requests all set up scsi busses.
231  * target and lun optionally narrow the search if not -1
232  */
233 int
234 scsi_probe_busses(bus, target, lun)
235 	int bus, target, lun;
236 {
237 
238 	if (bus == -1) {
239 		for (bus = 0; bus < scsibus_cd.cd_ndevs; bus++)
240 			if (scsibus_cd.cd_devs[bus])
241 				scsi_probe_bus(bus, target, lun);
242 		return (0);
243 	} else
244 		return (scsi_probe_bus(bus, target, lun));
245 }
246 
247 /*
248  * Probe the requested scsi bus. It must be already set up.
249  * target and lun optionally narrow the search if not -1
250  */
251 int
252 scsi_probe_bus(bus, target, lun)
253 	int bus, target, lun;
254 {
255 	struct scsibus_softc *scsi;
256 	int maxtarget, mintarget, maxlun, minlun;
257 	u_int8_t scsi_addr;
258 	int error;
259 
260 	if (bus < 0 || bus >= scsibus_cd.cd_ndevs)
261 		return (ENXIO);
262 	scsi = scsibus_cd.cd_devs[bus];
263 	if (scsi == NULL)
264 		return (ENXIO);
265 
266 	scsi_addr = scsi->adapter_link->scsipi_scsi.adapter_target;
267 
268 	if (target == -1) {
269 		maxtarget = scsi->sc_maxtarget;
270 		mintarget = 0;
271 	} else {
272 		if (target < 0 || target > scsi->sc_maxtarget)
273 			return (EINVAL);
274 		maxtarget = mintarget = target;
275 	}
276 
277 	if (lun == -1) {
278 		maxlun = scsi->sc_maxlun;
279 		minlun = 0;
280 	} else {
281 		if (lun < 0 || lun > scsi->sc_maxlun)
282 			return (EINVAL);
283 		maxlun = minlun = lun;
284 	}
285 
286 	if ((error = scsipi_adapter_addref(scsi->adapter_link)) != 0)
287 		return (error);
288 	for (target = mintarget; target <= maxtarget; target++) {
289 		if (target == scsi_addr)
290 			continue;
291 		for (lun = minlun; lun <= maxlun; lun++) {
292 			/*
293 			 * See if there's a device present, and configure it.
294 			 */
295 			if (scsi_probedev(scsi, target, lun) == 0) {
296 				break;
297 			}
298 			/* otherwise something says we should look further */
299 		}
300 	}
301 	scsipi_adapter_delref(scsi->adapter_link);
302 	return (0);
303 }
304 
305 /*
306  * Print out autoconfiguration information for a subdevice.
307  *
308  * This is a slight abuse of 'standard' autoconfiguration semantics,
309  * because 'print' functions don't normally print the colon and
310  * device information.  However, in this case that's better than
311  * either printing redundant information before the attach message,
312  * or having the device driver call a special function to print out
313  * the standard device information.
314  */
315 int
316 scsibusprint(aux, pnp)
317 	void *aux;
318 	const char *pnp;
319 {
320 	struct scsipibus_attach_args *sa = aux;
321 	struct scsipi_inquiry_pattern *inqbuf;
322 	u_int8_t type;
323 	char *dtype, *qtype;
324 	char vendor[33], product[65], revision[17];
325 	int target, lun;
326 
327 	if (pnp != NULL)
328 		printf("%s", pnp);
329 
330 	inqbuf = &sa->sa_inqbuf;
331 
332 	target = sa->sa_sc_link->scsipi_scsi.target;
333 	lun = sa->sa_sc_link->scsipi_scsi.lun;
334 
335 	type = inqbuf->type & SID_TYPE;
336 
337 	/*
338 	 * Figure out basic device type and qualifier.
339 	 */
340 	dtype = 0;
341 	switch (inqbuf->type & SID_QUAL) {
342 	case SID_QUAL_LU_OK:
343 		qtype = "";
344 		break;
345 
346 	case SID_QUAL_LU_OFFLINE:
347 		qtype = " offline";
348 		break;
349 
350 	case SID_QUAL_RSVD:
351 	case SID_QUAL_BAD_LU:
352 		panic("scsibusprint: impossible qualifier");
353 
354 	default:
355 		qtype = "";
356 		dtype = "vendor-unique";
357 		break;
358 	}
359 	if (dtype == 0)
360 		dtype = scsipi_dtype(type);
361 
362 	scsipi_strvis(vendor, 33, inqbuf->vendor, 8);
363 	scsipi_strvis(product, 65, inqbuf->product, 16);
364 	scsipi_strvis(revision, 17, inqbuf->revision, 4);
365 
366 	printf(" targ %d lun %d: <%s, %s, %s> SCSI%d %d/%s %s%s",
367 	    target, lun, vendor, product, revision,
368 	    sa->scsipi_info.scsi_version & SID_ANSII, type, dtype,
369 	    inqbuf->removable ? "removable" : "fixed", qtype);
370 
371 	return (UNCONF);
372 }
373 
374 struct scsi_quirk_inquiry_pattern scsi_quirk_patterns[] = {
375 	{{T_CDROM, T_REMOV,
376 	 "CHINON  ", "CD-ROM CDS-431  ", ""},     SDEV_NOLUNS},
377 	{{T_CDROM, T_REMOV,
378 	 "Chinon  ", "CD-ROM CDS-525  ", ""},     SDEV_NOLUNS},
379 	{{T_CDROM, T_REMOV,
380 	 "CHINON  ", "CD-ROM CDS-535  ", ""},     SDEV_NOLUNS},
381 	{{T_CDROM, T_REMOV,
382 	 "DEC     ", "RRD42   (C) DEC ", ""},     SDEV_NOLUNS},
383 	{{T_CDROM, T_REMOV,
384 	 "DENON   ", "DRD-25X         ", "V"},    SDEV_NOLUNS},
385 	{{T_CDROM, T_REMOV,
386 	 "HP      ", "C4324/C4325     ", ""},     SDEV_NOLUNS},
387 	{{T_CDROM, T_REMOV,
388 	 "IMS     ", "CDD521/10       ", "2.06"}, SDEV_NOLUNS},
389 	{{T_CDROM, T_REMOV,
390 	 "MATSHITA", "CD-ROM CR-5XX   ", "1.0b"}, SDEV_NOLUNS},
391 	{{T_CDROM, T_REMOV,
392 	 "MEDAVIS ", "RENO CD-ROMX2A  ", ""},     SDEV_NOLUNS},
393 	{{T_CDROM, T_REMOV,
394 	 "MEDIAVIS", "CDR-H93MV       ", "1.3"},  SDEV_NOLUNS},
395 	{{T_CDROM, T_REMOV,
396 	 "NEC     ", "CD-ROM DRIVE:55 ", ""},     SDEV_NOLUNS},
397 	{{T_CDROM, T_REMOV,
398 	 "NEC     ", "CD-ROM DRIVE:83 ", ""},     SDEV_NOLUNS},
399 	{{T_CDROM, T_REMOV,
400 	 "NEC     ", "CD-ROM DRIVE:84 ", ""},     SDEV_NOLUNS},
401 	{{T_CDROM, T_REMOV,
402 	 "NEC     ", "CD-ROM DRIVE:841", ""},     SDEV_NOLUNS},
403 	{{T_CDROM, T_REMOV,
404 	 "PIONEER ", "CD-ROM DR-124X  ", "1.01"}, SDEV_NOLUNS},
405 	{{T_CDROM, T_REMOV,
406 	 "SONY    ", "CD-ROM CDU-541  ", ""},     SDEV_NOLUNS},
407 	{{T_CDROM, T_REMOV,
408 	 "SONY    ", "CD-ROM CDU-55S  ", ""},     SDEV_NOLUNS},
409 	{{T_CDROM, T_REMOV,
410 	 "SONY    ", "CD-ROM CDU-8003A", ""},     SDEV_NOLUNS},
411 	{{T_CDROM, T_REMOV,
412 	 "SONY    ", "CD-ROM CDU-8012 ", ""},     SDEV_NOLUNS},
413 	{{T_CDROM, T_REMOV,
414 	 "TEAC    ", "CD-ROM          ", "1.06"}, SDEV_NOLUNS},
415 	{{T_CDROM, T_REMOV,
416 	 "TEAC    ", "CD-ROM CD-56S   ", "1.0B"}, SDEV_NOLUNS},
417 	{{T_CDROM, T_REMOV,
418 	 "TEXEL   ", "CD-ROM          ", "1.06"}, SDEV_NOLUNS},
419 	{{T_CDROM, T_REMOV,
420 	 "TEXEL   ", "CD-ROM DM-XX24 K", "1.10"}, SDEV_NOLUNS},
421 	{{T_CDROM, T_REMOV,
422 	 "TOSHIBA ", "XM-4101TASUNSLCD", "1755"}, SDEV_NOLUNS},
423 	{{T_CDROM, T_REMOV,
424 	 "ShinaKen", "CD-ROM DM-3x1S", "1.04"}, SDEV_NOLUNS},
425 	{{T_CDROM, T_REMOV,
426 	 "JVC     ", "R2626           ", "1.55"}, SDEV_NOLUNS},
427 	{{T_DIRECT, T_FIXED,
428 	 "MICROP  ", "1588-15MBSUN0669", ""},     SDEV_AUTOSAVE},
429 	{{T_DIRECT, T_FIXED,
430 	 "MICROP  ", "2217-15MQ1091501", ""},     SDEV_NOSYNCCACHE},
431 	{{T_OPTICAL, T_REMOV,
432 	 "EPSON   ", "OMD-5010        ", "3.08"}, SDEV_NOLUNS},
433 
434 	{{T_DIRECT, T_FIXED,
435 	 "ADAPTEC ", "AEC-4412BD",       "1.2A"}, SDEV_NOMODESENSE},
436 	{{T_DIRECT, T_FIXED,
437 	 "DEC     ", "RZ55     (C) DEC", ""},     SDEV_AUTOSAVE},
438 	{{T_DIRECT, T_FIXED,
439 	 "EMULEX  ", "MD21/S2     ESDI", "A00"},  SDEV_FORCELUNS|SDEV_AUTOSAVE},
440 	/* Gives non-media hardware failure in response to start-unit command */
441 	{{T_DIRECT, T_FIXED,
442 	 "HITACHI", "DK515C",		"CP16"},  SDEV_NOSTARTUNIT},
443 	{{T_DIRECT, T_FIXED,
444 	 "HITACHI", "DK515C",		"CP15"},  SDEV_NOSTARTUNIT},
445 	{{T_DIRECT, T_FIXED,
446 	 "HP      ", "C372",             ""},     SDEV_NOTAG},
447 	{{T_DIRECT, T_FIXED,
448 	 "IBMRAID ", "0662S",		 ""},     SDEV_AUTOSAVE},
449 	{{T_DIRECT, T_FIXED,
450 	 "IBM     ", "0663H",		 ""},     SDEV_AUTOSAVE},
451 	{{T_DIRECT, T_FIXED,
452 	 "IBM",	     "0664",		 ""},     SDEV_AUTOSAVE},
453 	{{T_DIRECT, T_FIXED,
454 	 "IBM     ", "H3171-S2",	 ""},	  SDEV_NOLUNS|SDEV_AUTOSAVE},
455 	{{T_DIRECT, T_FIXED,
456 	 "IBM     ", "KZ-C",		 ""},	  SDEV_AUTOSAVE},
457 	/* Broken IBM disk */
458 	{{T_DIRECT, T_FIXED,
459 	 ""	   , "DFRSS2F",		 ""},	  SDEV_AUTOSAVE},
460 	{{T_DIRECT, T_REMOV,
461 	 "MPL     ", "MC-DISK-        ", ""},     SDEV_NOLUNS},
462 	{{T_DIRECT, T_FIXED,
463 	 "MAXTOR  ", "XT-3280         ", ""},     SDEV_NOLUNS},
464 	{{T_DIRECT, T_FIXED,
465 	 "MAXTOR  ", "XT-4380S        ", ""},     SDEV_NOLUNS},
466 	{{T_DIRECT, T_FIXED,
467 	 "MAXTOR  ", "MXT-1240S       ", ""},     SDEV_NOLUNS},
468 	{{T_DIRECT, T_FIXED,
469 	 "MAXTOR  ", "XT-4170S        ", ""},     SDEV_NOLUNS},
470 	{{T_DIRECT, T_FIXED,
471 	 "MAXTOR  ", "XT-8760S",         ""},     SDEV_NOLUNS},
472 	{{T_DIRECT, T_FIXED,
473 	 "MAXTOR  ", "LXT-213S        ", ""},     SDEV_NOLUNS},
474 	{{T_DIRECT, T_FIXED,
475 	 "MAXTOR  ", "LXT-213S SUN0207", ""},     SDEV_NOLUNS},
476 	{{T_DIRECT, T_FIXED,
477 	 "MAXTOR  ", "LXT-200S        ", ""},     SDEV_NOLUNS},
478 	{{T_DIRECT, T_FIXED,
479 	 "MEGADRV ", "EV1000",           ""},     SDEV_NOMODESENSE},
480 	{{T_DIRECT, T_FIXED,
481 	 "MST     ", "SnapLink        ", ""},     SDEV_NOLUNS},
482 	{{T_DIRECT, T_FIXED,
483 	 "NEC     ", "D3847           ", "0307"}, SDEV_NOLUNS},
484 	{{T_DIRECT, T_FIXED,
485 	 "QUANTUM ", "ELS85S          ", ""},     SDEV_AUTOSAVE},
486 	{{T_DIRECT, T_FIXED,
487 	 "QUANTUM ", "LPS525S         ", ""},     SDEV_NOLUNS},
488 	{{T_DIRECT, T_FIXED,
489 	 "QUANTUM ", "P105S 910-10-94x", ""},     SDEV_NOLUNS},
490 	{{T_DIRECT, T_FIXED,
491 	 "QUANTUM ", "PD1225S         ", ""},     SDEV_NOLUNS},
492 	{{T_DIRECT, T_FIXED,
493 	 "QUANTUM ", "PD210S   SUN0207", ""},     SDEV_NOLUNS},
494 	{{T_DIRECT, T_FIXED,
495 	 "RODIME  ", "RO3000S         ", ""},     SDEV_NOLUNS},
496 	{{T_DIRECT, T_FIXED,
497 	 "SEAGATE ", "ST125N          ", ""},     SDEV_NOLUNS},
498 	{{T_DIRECT, T_FIXED,
499 	 "SEAGATE ", "ST157N          ", ""},     SDEV_NOLUNS},
500 	{{T_DIRECT, T_FIXED,
501 	 "SEAGATE ", "ST296           ", ""},     SDEV_NOLUNS},
502 	{{T_DIRECT, T_FIXED,
503 	 "SEAGATE ", "ST296N          ", ""},     SDEV_NOLUNS},
504 	{{T_DIRECT, T_FIXED,
505 	 "SEAGATE ", "ST19171",          ""},     SDEV_NOMODESENSE},
506 	{{T_DIRECT, T_FIXED,
507 	 "SEAGATE ", "ST34501FC       ", ""},     SDEV_NOMODESENSE},
508 	{{T_DIRECT, T_FIXED,
509 	 "TOSHIBA ", "MK538FB         ", "6027"}, SDEV_NOLUNS},
510 	{{T_DIRECT, T_REMOV,
511 	 "iomega", "jaz 1GB", 		 ""},	  SDEV_NOMODESENSE},
512 	{{T_DIRECT, T_REMOV,
513 	 "IOMEGA", "ZIP 100",		 ""},	  SDEV_NOMODESENSE},
514 	{{T_DIRECT, T_REMOV,
515 	 "IOMEGA", "ZIP 100",		 "J.03"}, SDEV_NOMODESENSE|SDEV_NOLUNS},
516 	/* Letting the motor run kills floppy drives and disks quite fast. */
517 	{{T_DIRECT, T_REMOV,
518 	 "TEAC", "FC-1",		 ""},	  SDEV_NOSTARTUNIT},
519 
520 	/* XXX: QIC-36 tape behind Emulex adapter.  Very broken. */
521 	{{T_SEQUENTIAL, T_REMOV,
522 	 "        ", "                ", "    "}, SDEV_NOLUNS},
523 	{{T_SEQUENTIAL, T_REMOV,
524 	 "CALIPER ", "CP150           ", ""},     SDEV_NOLUNS},
525 	{{T_SEQUENTIAL, T_REMOV,
526 	 "EXABYTE ", "EXB-8200        ", ""},     SDEV_NOLUNS},
527 	{{T_SEQUENTIAL, T_REMOV,
528 	 "SONY    ", "GY-10C          ", ""},     SDEV_NOLUNS},
529 	{{T_SEQUENTIAL, T_REMOV,
530 	 "SONY    ", "SDT-2000        ", "2.09"}, SDEV_NOLUNS},
531 	{{T_SEQUENTIAL, T_REMOV,
532 	 "SONY    ", "SDT-5000        ", "3."},   SDEV_NOSYNC|SDEV_NOWIDE},
533 	{{T_SEQUENTIAL, T_REMOV,
534 	 "SONY    ", "SDT-5200        ", "3."},   SDEV_NOLUNS},
535 	{{T_SEQUENTIAL, T_REMOV,
536 	 "TANDBERG", " TDC 3600       ", ""},     SDEV_NOLUNS},
537 	/* Following entry reported as a Tandberg 3600; ref. PR1933 */
538 	{{T_SEQUENTIAL, T_REMOV,
539 	 "ARCHIVE ", "VIPER 150  21247", ""},     SDEV_NOLUNS},
540 	/* Following entry for a Cipher ST150S; ref. PR4171 */
541 	{{T_SEQUENTIAL, T_REMOV,
542 	 "ARCHIVE ", "VIPER 1500 21247", "2.2G"}, SDEV_NOLUNS},
543 	{{T_SEQUENTIAL, T_REMOV,
544 	 "ARCHIVE ", "Python 28454-XXX", ""},     SDEV_NOLUNS},
545 	{{T_SEQUENTIAL, T_REMOV,
546 	 "WANGTEK ", "5099ES SCSI",      ""},     SDEV_NOLUNS},
547 	{{T_SEQUENTIAL, T_REMOV,
548 	 "WANGTEK ", "5150ES SCSI",      ""},     SDEV_NOLUNS},
549 	{{T_SEQUENTIAL, T_REMOV,
550 	 "WangDAT ", "Model 1300      ", "02.4"}, SDEV_NOSYNC|SDEV_NOWIDE},
551 	{{T_SEQUENTIAL, T_REMOV,
552 	 "WangDAT ", "Model 2600      ", "01.7"}, SDEV_NOSYNC|SDEV_NOWIDE},
553 	{{T_SEQUENTIAL, T_REMOV,
554 	 "WangDAT ", "Model 3200      ", "02.2"}, SDEV_NOSYNC|SDEV_NOWIDE},
555 
556 	{{T_SCANNER, T_FIXED,
557 	 "RICOH   ", "IS60            ", "1R08"}, SDEV_NOLUNS},
558 	{{T_SCANNER, T_FIXED,
559 	 "UMAX    ", "Astra 1200S     ", "V2.9"}, SDEV_NOLUNS},
560 	{{T_SCANNER, T_FIXED,
561 	 "UMAX    ", "Astra 1220S     ", "V1.2"}, SDEV_NOLUNS},
562 	{{T_SCANNER, T_FIXED,
563 	 "UMAX    ", "UMAX S-6E       ", "V2.0"}, SDEV_NOLUNS},
564 	{{T_SCANNER, T_FIXED,
565 	 "UMAX    ", "UMAX S-12       ", "V2.1"}, SDEV_NOLUNS},
566 
567 	{{T_PROCESSOR, T_FIXED,
568 	 "LITRONIC", "PCMCIA          ", ""},     SDEV_NOLUNS},
569 
570 	{{T_CHANGER, T_REMOV,
571 	 "SONY    ", "CDL1100         ", ""},     SDEV_NOLUNS},
572 
573 	{{T_ENCLOSURE, T_FIXED,
574 	 "SUN     ", "SENA            ", "1.07"}, SDEV_NOLUNS},
575 };
576 
577 /*
578  * given a target and lun, ask the device what
579  * it is, and find the correct driver table
580  * entry.
581  */
582 int
583 scsi_probedev(scsi, target, lun)
584 	struct scsibus_softc *scsi;
585 	int target, lun;
586 {
587 	struct scsipi_link *sc_link;
588 	static struct scsipi_inquiry_data inqbuf;
589 	struct scsi_quirk_inquiry_pattern *finger;
590 	int checkdtype, priority, docontinue;
591 	struct scsipibus_attach_args sa;
592 	struct cfdata *cf;
593 
594 	/*
595 	 * Assume no more luns to search after this one.
596 	 * If we successfully get Inquiry data and after
597 	 * merging quirks we find we can probe for more
598 	 * luns, we will.
599 	 */
600 	docontinue = 0;
601 
602 	/* Skip this slot if it is already attached. */
603 	if (scsi->sc_link[target][lun] != NULL)
604 		return (docontinue);
605 
606 	sc_link = malloc(sizeof(*sc_link), M_DEVBUF, M_NOWAIT);
607 	*sc_link = *scsi->adapter_link;
608 	sc_link->scsipi_scsi.target = target;
609 	sc_link->scsipi_scsi.lun = lun;
610 	sc_link->device = &probe_switch;
611 	TAILQ_INIT(&sc_link->pending_xfers);
612 
613 	/*
614 	 * Ask the device what it is
615 	 */
616 #if defined(SCSIDEBUG) && DEBUGTYPE == BUS_SCSI
617 	if (target == DEBUGTARGET && lun == DEBUGLUN)
618 		sc_link->flags |= DEBUGLEVEL;
619 #endif /* SCSIDEBUG */
620 
621 	(void) scsipi_test_unit_ready(sc_link,
622 	    SCSI_AUTOCONF | SCSI_IGNORE_ILLEGAL_REQUEST |
623 	    SCSI_IGNORE_NOT_READY | SCSI_IGNORE_MEDIA_CHANGE);
624 
625 #ifdef SCSI_2_DEF
626 	/* some devices need to be told to go to SCSI2 */
627 	/* However some just explode if you tell them this.. leave it out */
628 	scsi_change_def(sc_link, SCSI_AUTOCONF | SCSI_SILENT);
629 #endif /* SCSI_2_DEF */
630 
631 	/* Now go ask the device all about itself. */
632 	bzero(&inqbuf, sizeof(inqbuf));
633 	if (scsipi_inquire(sc_link, &inqbuf, SCSI_AUTOCONF) != 0)
634 		goto bad;
635 
636 	{
637 		int len = inqbuf.additional_length;
638 		while (len < 3)
639 			inqbuf.unused[len++] = '\0';
640 		while (len < 3 + 28)
641 			inqbuf.unused[len++] = ' ';
642 	}
643 
644 	sa.sa_sc_link = sc_link;
645 	sa.sa_inqbuf.type = inqbuf.device;
646 	sa.sa_inqbuf.removable = inqbuf.dev_qual2 & SID_REMOVABLE ?
647 	    T_REMOV : T_FIXED;
648 	sa.sa_inqbuf.vendor = inqbuf.vendor;
649 	sa.sa_inqbuf.product = inqbuf.product;
650 	sa.sa_inqbuf.revision = inqbuf.revision;
651 	sa.scsipi_info.scsi_version = inqbuf.version;
652 
653 	finger = (struct scsi_quirk_inquiry_pattern *)scsipi_inqmatch(
654 	    &sa.sa_inqbuf, (caddr_t)scsi_quirk_patterns,
655 	    sizeof(scsi_quirk_patterns)/sizeof(scsi_quirk_patterns[0]),
656 	    sizeof(scsi_quirk_patterns[0]), &priority);
657 
658 	/*
659 	 * Based upon the inquiry flags we got back, and if we're
660 	 * at SCSI-2 or better, set some limiting quirks.
661 	 */
662 	if ((inqbuf.version & SID_ANSII) >= 2) {
663 		if ((inqbuf.flags & SID_CmdQue) == 0)
664 			sc_link->quirks |= SDEV_NOTAG;
665 		if ((inqbuf.flags & SID_Sync) == 0)
666 			sc_link->quirks |= SDEV_NOSYNC;
667 		if ((inqbuf.flags & SID_WBus16) == 0)
668 			sc_link->quirks |= SDEV_NOWIDE;
669 	}
670 	/*
671 	 * Now apply any quirks from the table.
672 	 */
673 	if (priority != 0)
674 		sc_link->quirks |= finger->quirks;
675 	if ((inqbuf.version & SID_ANSII) == 0 &&
676 	    (sc_link->quirks & SDEV_FORCELUNS) == 0)
677 		sc_link->quirks |= SDEV_NOLUNS;
678 	sc_link->scsipi_scsi.scsi_version = inqbuf.version;
679 
680 	if ((sc_link->quirks & SDEV_NOLUNS) == 0)
681 		docontinue = 1;
682 
683 	/*
684 	 * note what BASIC type of device it is
685 	 */
686 	if ((inqbuf.dev_qual2 & SID_REMOVABLE) != 0)
687 		sc_link->flags |= SDEV_REMOVABLE;
688 
689 	/*
690 	 * Any device qualifier that has the top bit set (qualifier&4 != 0)
691 	 * is vendor specific and won't match in this switch.
692 	 * All we do here is throw out bad/negative responses.
693 	 */
694 	checkdtype = 0;
695 	switch (inqbuf.device & SID_QUAL) {
696 	case SID_QUAL_LU_OK:
697 	case SID_QUAL_LU_OFFLINE:
698 		checkdtype = 1;
699 		break;
700 
701 	case SID_QUAL_RSVD:
702 	case SID_QUAL_BAD_LU:
703 		goto bad;
704 
705 	default:
706 		break;
707 	}
708 	if (checkdtype)
709 		switch (inqbuf.device & SID_TYPE) {
710 		case T_DIRECT:
711 		case T_SEQUENTIAL:
712 		case T_PRINTER:
713 		case T_PROCESSOR:
714 		case T_WORM:
715 		case T_CDROM:
716 		case T_SCANNER:
717 		case T_OPTICAL:
718 		case T_CHANGER:
719 		case T_COMM:
720 		case T_IT8_1:
721 		case T_IT8_2:
722 		case T_STORARRAY:
723 		case T_ENCLOSURE:
724 		default:
725 			break;
726 		case T_NODEVICE:
727 			goto bad;
728 		}
729 
730 	if ((cf = config_search(scsibussubmatch, (struct device *)scsi,
731 	    &sa)) != NULL) {
732 		scsi->sc_link[target][lun] = sc_link;
733 		config_attach((struct device *)scsi, cf, &sa, scsibusprint);
734 	} else {
735 		scsibusprint(&sa, scsi->sc_dev.dv_xname);
736 		printf(" not configured\n");
737 		goto bad;
738 	}
739 
740 	return (docontinue);
741 
742 bad:
743 	free(sc_link, M_DEVBUF);
744 	return (docontinue);
745 }
746 
747 /****** Entry points for user control of the SCSI bus. ******/
748 
749 int
750 scsibusopen(dev, flag, fmt, p)
751 	dev_t dev;
752 	int flag, fmt;
753 	struct proc *p;
754 {
755 	struct scsibus_softc *sc;
756 	int error, unit = minor(dev);
757 
758 	if (unit >= scsibus_cd.cd_ndevs ||
759 	    (sc = scsibus_cd.cd_devs[unit]) == NULL)
760 		return (ENXIO);
761 
762 	if (sc->sc_flags & SCSIBUSF_OPEN)
763 		return (EBUSY);
764 
765 	if ((error = scsipi_adapter_addref(sc->adapter_link)) != 0)
766 		return (error);
767 
768 	sc->sc_flags |= SCSIBUSF_OPEN;
769 
770 	return (0);
771 }
772 
773 int
774 scsibusclose(dev, flag, fmt, p)
775 	dev_t dev;
776 	int flag, fmt;
777 	struct proc *p;
778 {
779 	struct scsibus_softc *sc = scsibus_cd.cd_devs[minor(dev)];
780 
781 	scsipi_adapter_delref(sc->adapter_link);
782 
783 	sc->sc_flags &= ~SCSIBUSF_OPEN;
784 
785 	return (0);
786 }
787 
788 int
789 scsibusioctl(dev, cmd, addr, flag, p)
790 	dev_t dev;
791 	u_long cmd;
792 	caddr_t addr;
793 	int flag;
794 	struct proc *p;
795 {
796 	struct scsibus_softc *sc = scsibus_cd.cd_devs[minor(dev)];
797 	struct scsipi_link *sc_link = sc->adapter_link;
798 	int error;
799 
800 	/*
801 	 * Enforce write permission for ioctls that change the
802 	 * state of the bus.  Host adapter specific ioctls must
803 	 * be checked by the adapter driver.
804 	 */
805 	switch (cmd) {
806 	case SCBUSIOSCAN:
807 	case SCBUSIORESET:
808 		if ((flag & FWRITE) == 0)
809 			return (EBADF);
810 	}
811 
812 	switch (cmd) {
813 	case SCBUSIOSCAN:
814 	    {
815 		struct scbusioscan_args *a =
816 		    (struct scbusioscan_args *)addr;
817 
818 		/* XXX Change interface to this function. */
819 		error = scsi_probe_busses(minor(dev), a->sa_target,
820 		    a->sa_lun);
821 		break;
822 	    }
823 
824 	case SCBUSIORESET:
825 		/* FALLTHROUGH */
826 	default:
827 		if (sc_link->adapter->scsipi_ioctl == NULL)
828 			error = ENOTTY;
829 		else
830 			error = (*sc_link->adapter->scsipi_ioctl)(sc_link,
831 			    cmd, addr, flag, p);
832 		break;
833 	}
834 
835 	return (error);
836 }
837