xref: /netbsd-src/sys/dev/scsipi/scsiconf.c (revision 17dd36da8292193180754d5047c0926dbb56818c)
1 /*	$NetBSD: scsiconf.c,v 1.156 2001/02/26 22:31:27 fvdl 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/kernel.h>
61 #include <sys/proc.h>
62 #include <sys/malloc.h>
63 #include <sys/device.h>
64 #include <sys/conf.h>
65 #include <sys/fcntl.h>
66 #include <sys/scsiio.h>
67 
68 #include <dev/scsipi/scsi_all.h>
69 #include <dev/scsipi/scsipi_all.h>
70 #include <dev/scsipi/scsiconf.h>
71 
72 #include "locators.h"
73 
74 #if 0
75 #if NCALS > 0
76 	{ T_PROCESSOR, T_FIXED, 1,
77 	  0, 0, 0 },
78 #endif	/* NCALS */
79 #if NBLL > 0
80 	{ T_PROCESSOR, T_FIXED, 1,
81 	  "AEG     ", "READER          ", "V1.0" },
82 #endif	/* NBLL */
83 #if NKIL > 0
84 	{ T_SCANNER, T_FIXED, 0,
85 	  "KODAK   ", "IL Scanner 900  ", 0 },
86 #endif	/* NKIL */
87 #endif
88 
89 /*
90  * Declarations
91  */
92 int scsi_probedev __P((struct scsibus_softc *, int, int));
93 int scsi_probe_bus __P((int bus, int target, int lun));
94 
95 struct scsipi_device probe_switch = {
96 	NULL,
97 	NULL,
98 	NULL,
99 	NULL,
100 };
101 
102 int scsibusmatch __P((struct device *, struct cfdata *, void *));
103 void scsibusattach __P((struct device *, struct device *, void *));
104 int scsibusactivate __P((struct device *, enum devact));
105 int scsibusdetach __P((struct device *, int flags));
106 
107 int scsibussubmatch __P((struct device *, struct cfdata *, void *));
108 
109 struct cfattach scsibus_ca = {
110 	sizeof(struct scsibus_softc), scsibusmatch, scsibusattach,
111 	    scsibusdetach, scsibusactivate,
112 };
113 
114 extern struct cfdriver scsibus_cd;
115 
116 int scsibusprint __P((void *, const char *));
117 void scsibus_config_interrupts __P((struct device *));
118 
119 cdev_decl(scsibus);
120 
121 int
122 scsibusmatch(parent, cf, aux)
123 	struct device *parent;
124 	struct cfdata *cf;
125 	void *aux;
126 {
127 	struct scsipi_link *l = aux;
128 	int channel;
129 
130 	if (l->type != BUS_SCSI)
131 		return (0);
132 
133 	/*
134 	 * Allow single-channel controllers to specify their channel
135 	 * in a special way, so that it's not printed.
136 	 */
137 	channel = (l->scsipi_scsi.channel != SCSI_CHANNEL_ONLY_ONE) ?
138 	    l->scsipi_scsi.channel : 0;
139 
140 	if (cf->cf_loc[SCSICF_CHANNEL] != channel &&
141 	    cf->cf_loc[SCSICF_CHANNEL] != SCSICF_CHANNEL_DEFAULT)
142 		return (0);
143 
144 	return (1);
145 }
146 
147 /*
148  * The routine called by the adapter boards to get all their
149  * devices configured in.
150  */
151 void
152 scsibusattach(parent, self, aux)
153 	struct device *parent, *self;
154 	void *aux;
155 {
156 	struct scsibus_softc *sb = (struct scsibus_softc *)self;
157 	struct scsipi_link *sc_link_proto = aux;
158 	size_t nbytes;
159 	int i;
160 
161 	sc_link_proto->scsipi_scsi.scsibus = sb->sc_dev.dv_unit;
162 	sc_link_proto->scsipi_cmd = scsi_scsipi_cmd;
163 	sc_link_proto->scsipi_interpret_sense = scsipi_interpret_sense;
164 	sc_link_proto->sc_print_addr = scsi_print_addr;
165 	sc_link_proto->scsipi_kill_pending = scsi_kill_pending;
166 
167 	sb->adapter_link = sc_link_proto;
168 	sb->sc_maxtarget = sc_link_proto->scsipi_scsi.max_target;
169 	sb->sc_maxlun = sc_link_proto->scsipi_scsi.max_lun;
170 	printf(": %d targets, %d luns per target\n",
171 	    sb->sc_maxtarget + 1, sb->sc_maxlun + 1);
172 
173 	/* Initialize shared data. */
174 	scsipi_init();
175 
176 	nbytes = (sb->sc_maxtarget + 1) * sizeof(struct scsipi_link **);
177 	sb->sc_link = (struct scsipi_link ***)malloc(nbytes, M_DEVBUF,
178 	    M_NOWAIT);
179 	if (sb->sc_link == NULL)
180 		panic("scsibusattach: can't allocate target links");
181 
182 	nbytes = (((int) sb->sc_maxlun) + 1) * sizeof(struct scsipi_link *);
183 	for (i = 0; i <= sb->sc_maxtarget; i++) {
184 		sb->sc_link[i] = (struct scsipi_link **)malloc(nbytes,
185 		    M_DEVBUF, M_NOWAIT);
186 		if (sb->sc_link[i] == NULL)
187 			panic("scsibusattach: can't allocate lun links");
188 		bzero(sb->sc_link[i], nbytes);
189 	}
190 
191 	/*
192 	 * Defer configuration of the children until interrupts
193 	 * are enabled.
194 	 */
195 	config_interrupts(self, scsibus_config_interrupts);
196 }
197 
198 void
199 scsibus_config_interrupts(self)
200 	struct device *self;
201 {
202 #ifndef SCSI_DELAY
203 #define SCSI_DELAY 2
204 #endif
205 	if (SCSI_DELAY > 0) {
206 		printf("%s: waiting %d seconds for devices to settle...\n",
207 		    self->dv_xname, SCSI_DELAY);
208 		/* ...an identifier we know no one will use... */
209 		(void) tsleep(scsibus_config_interrupts, PRIBIO,
210 		    "scsidly", SCSI_DELAY * hz);
211 	}
212 
213 	scsi_probe_bus(self->dv_unit, -1, -1);
214 }
215 
216 int
217 scsibussubmatch(parent, cf, aux)
218 	struct device *parent;
219 	struct cfdata *cf;
220 	void *aux;
221 {
222 	struct scsipibus_attach_args *sa = aux;
223 	struct scsipi_link *sc_link = sa->sa_sc_link;
224 
225 	if (cf->cf_loc[SCSIBUSCF_TARGET] != SCSIBUSCF_TARGET_DEFAULT &&
226 	    cf->cf_loc[SCSIBUSCF_TARGET] != sc_link->scsipi_scsi.target)
227 		return (0);
228 	if (cf->cf_loc[SCSIBUSCF_LUN] != SCSIBUSCF_LUN_DEFAULT &&
229 	    cf->cf_loc[SCSIBUSCF_LUN] != sc_link->scsipi_scsi.lun)
230 		return (0);
231 	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
232 }
233 
234 int
235 scsibusactivate(self, act)
236 	struct device *self;
237 	enum devact act;
238 {
239 	struct scsibus_softc *sc = (struct scsibus_softc *) self;
240 	struct scsipi_link *sc_link;
241 	int target, lun, error = 0, s;
242 
243 	s = splbio();
244 	switch (act) {
245 	case DVACT_ACTIVATE:
246 		error = EOPNOTSUPP;
247 		break;
248 
249 	case DVACT_DEACTIVATE:
250 		for (target = 0; target <= sc->sc_maxtarget; target++) {
251 			if (target ==
252 			    sc->adapter_link->scsipi_scsi.adapter_target)
253 				continue;
254 			for (lun = 0; lun <= sc->sc_maxlun; lun++) {
255 				sc_link = sc->sc_link[target][lun];
256 				if (sc_link == NULL)
257 					continue;
258 				error =
259 				    config_deactivate(sc_link->device_softc);
260 				if (error)
261 					goto out;
262 			}
263 		}
264 		break;
265 	}
266  out:
267 	splx(s);
268 	return (error);
269 }
270 
271 int
272 scsibusdetach(self, flags)
273 	struct device *self;
274 	int flags;
275 {
276 	struct scsibus_softc *sc = (struct scsibus_softc *) self;
277 	struct scsipi_link *sc_link;
278 	int target, lun, error;
279 
280 	for (target = 0; target <= sc->sc_maxtarget; target++) {
281 		if (target == sc->adapter_link->scsipi_scsi.adapter_target)
282 			continue;
283 		for (lun = 0; lun <= sc->sc_maxlun; lun++) {
284 			sc_link = sc->sc_link[target][lun];
285 			if (sc_link == NULL)
286 				continue;
287 			error = config_detach(sc_link->device_softc, flags);
288 			if (error)
289 				return (error);
290 			free(sc_link, M_DEVBUF);
291 			sc->sc_link[target][lun] = NULL;
292 		}
293 	}
294 	return (0);
295 }
296 
297 /*
298  * Probe the requested scsi bus. It must be already set up.
299  * -1 requests all set up scsi busses.
300  * target and lun optionally narrow the search if not -1
301  */
302 int
303 scsi_probe_busses(bus, target, lun)
304 	int bus, target, lun;
305 {
306 
307 	if (bus == -1) {
308 		for (bus = 0; bus < scsibus_cd.cd_ndevs; bus++)
309 			if (scsibus_cd.cd_devs[bus])
310 				scsi_probe_bus(bus, target, lun);
311 		return (0);
312 	} else
313 		return (scsi_probe_bus(bus, target, lun));
314 }
315 
316 /*
317  * Probe the requested scsi bus. It must be already set up.
318  * target and lun optionally narrow the search if not -1
319  */
320 int
321 scsi_probe_bus(bus, target, lun)
322 	int bus, target, lun;
323 {
324 	struct scsibus_softc *scsi;
325 	struct scsipi_link *sc_link;
326 	int maxtarget, mintarget, maxlun, minlun;
327 	u_int8_t scsi_addr;
328 	int error;
329 
330 	if (bus < 0 || bus >= scsibus_cd.cd_ndevs)
331 		return (ENXIO);
332 	scsi = scsibus_cd.cd_devs[bus];
333 	if (scsi == NULL)
334 		return (ENXIO);
335 
336 	sc_link = scsi->adapter_link;
337 	scsi_addr = sc_link->scsipi_scsi.adapter_target;
338 
339 	if (target == -1) {
340 		maxtarget = scsi->sc_maxtarget;
341 		mintarget = 0;
342 	} else {
343 		if (target < 0 || target > scsi->sc_maxtarget)
344 			return (EINVAL);
345 		maxtarget = mintarget = target;
346 	}
347 
348 	if (lun == -1) {
349 		maxlun = scsi->sc_maxlun;
350 		minlun = 0;
351 	} else {
352 		if (lun < 0 || lun > scsi->sc_maxlun)
353 			return (EINVAL);
354 		maxlun = minlun = lun;
355 	}
356 
357 	/*
358 	 * Some HBAs provide an abstracted view of the bus; give them an
359 	 * oppertunity to re-scan it before we do.
360 	 */
361 	if (sc_link->adapter->scsipi_ioctl != NULL)
362 		(*sc_link->adapter->scsipi_ioctl)(sc_link, SCBUSIOLLSCAN, NULL,
363 		    0, curproc);
364 
365 	if ((error = scsipi_adapter_addref(sc_link)) != 0)
366 		return (error);
367 	for (target = mintarget; target <= maxtarget; target++) {
368 		if (target == scsi_addr)
369 			continue;
370 		for (lun = minlun; lun <= maxlun; lun++) {
371 			/*
372 			 * See if there's a device present, and configure it.
373 			 */
374 			if (scsi_probedev(scsi, target, lun) == 0) {
375 				break;
376 			}
377 			/* otherwise something says we should look further */
378 		}
379 	}
380 	scsipi_adapter_delref(sc_link);
381 	return (0);
382 }
383 
384 /*
385  * Print out autoconfiguration information for a subdevice.
386  *
387  * This is a slight abuse of 'standard' autoconfiguration semantics,
388  * because 'print' functions don't normally print the colon and
389  * device information.  However, in this case that's better than
390  * either printing redundant information before the attach message,
391  * or having the device driver call a special function to print out
392  * the standard device information.
393  */
394 int
395 scsibusprint(aux, pnp)
396 	void *aux;
397 	const char *pnp;
398 {
399 	struct scsipibus_attach_args *sa = aux;
400 	struct scsipi_inquiry_pattern *inqbuf;
401 	u_int8_t type;
402 	char *dtype, *qtype;
403 	char vendor[33], product[65], revision[17];
404 	int target, lun;
405 
406 	if (pnp != NULL)
407 		printf("%s", pnp);
408 
409 	inqbuf = &sa->sa_inqbuf;
410 
411 	target = sa->sa_sc_link->scsipi_scsi.target;
412 	lun = sa->sa_sc_link->scsipi_scsi.lun;
413 
414 	type = inqbuf->type & SID_TYPE;
415 
416 	/*
417 	 * Figure out basic device type and qualifier.
418 	 */
419 	dtype = 0;
420 	switch (inqbuf->type & SID_QUAL) {
421 	case SID_QUAL_LU_OK:
422 		qtype = "";
423 		break;
424 
425 	case SID_QUAL_LU_OFFLINE:
426 		qtype = " offline";
427 		break;
428 
429 	case SID_QUAL_RSVD:
430 	case SID_QUAL_BAD_LU:
431 		panic("scsibusprint: impossible qualifier");
432 
433 	default:
434 		qtype = "";
435 		dtype = "vendor-unique";
436 		break;
437 	}
438 	if (dtype == 0)
439 		dtype = scsipi_dtype(type);
440 
441 	scsipi_strvis(vendor, 33, inqbuf->vendor, 8);
442 	scsipi_strvis(product, 65, inqbuf->product, 16);
443 	scsipi_strvis(revision, 17, inqbuf->revision, 4);
444 
445 	printf(" target %d lun %d: <%s, %s, %s> SCSI%d %d/%s %s%s",
446 	    target, lun, vendor, product, revision,
447 	    sa->scsipi_info.scsi_version & SID_ANSII, type, dtype,
448 	    inqbuf->removable ? "removable" : "fixed", qtype);
449 
450 	return (UNCONF);
451 }
452 
453 const struct scsi_quirk_inquiry_pattern scsi_quirk_patterns[] = {
454 	{{T_CDROM, T_REMOV,
455 	 "CHINON  ", "CD-ROM CDS-431  ", ""},     SDEV_NOLUNS},
456 	{{T_CDROM, T_REMOV,
457 	 "Chinon  ", "CD-ROM CDS-525  ", ""},     SDEV_NOLUNS},
458 	{{T_CDROM, T_REMOV,
459 	 "CHINON  ", "CD-ROM CDS-535  ", ""},     SDEV_NOLUNS},
460 	{{T_CDROM, T_REMOV,
461 	 "DEC     ", "RRD42   (C) DEC ", ""},     SDEV_NOLUNS},
462 	{{T_CDROM, T_REMOV,
463 	 "DENON   ", "DRD-25X         ", "V"},    SDEV_NOLUNS},
464 	{{T_CDROM, T_REMOV,
465 	 "GENERIC ", "CRD-BP2         ", ""},     SDEV_NOLUNS},
466 	{{T_CDROM, T_REMOV,
467 	 "HP      ", "C4324/C4325     ", ""},     SDEV_NOLUNS},
468 	{{T_CDROM, T_REMOV,
469 	 "IMS     ", "CDD521/10       ", "2.06"}, SDEV_NOLUNS},
470 	{{T_CDROM, T_REMOV,
471 	 "MATSHITA", "CD-ROM CR-5XX   ", "1.0b"}, SDEV_NOLUNS},
472 	{{T_CDROM, T_REMOV,
473 	 "MEDAVIS ", "RENO CD-ROMX2A  ", ""},     SDEV_NOLUNS},
474 	{{T_CDROM, T_REMOV,
475 	 "MEDIAVIS", "CDR-H93MV       ", "1.3"},  SDEV_NOLUNS},
476 	{{T_CDROM, T_REMOV,
477 	 "NEC     ", "CD-ROM DRIVE:55 ", ""},     SDEV_NOLUNS},
478 	{{T_CDROM, T_REMOV,
479 	 "NEC     ", "CD-ROM DRIVE:83 ", ""},     SDEV_NOLUNS},
480 	{{T_CDROM, T_REMOV,
481 	 "NEC     ", "CD-ROM DRIVE:84 ", ""},     SDEV_NOLUNS},
482 	{{T_CDROM, T_REMOV,
483 	 "NEC     ", "CD-ROM DRIVE:841", ""},     SDEV_NOLUNS},
484 	{{T_CDROM, T_REMOV,
485 	 "PIONEER ", "CD-ROM DR-124X  ", "1.01"}, SDEV_NOLUNS},
486 	{{T_CDROM, T_REMOV,
487 	 "SONY    ", "CD-ROM CDU-541  ", ""},     SDEV_NOLUNS},
488 	{{T_CDROM, T_REMOV,
489 	 "SONY    ", "CD-ROM CDU-55S  ", ""},     SDEV_NOLUNS},
490 	{{T_CDROM, T_REMOV,
491 	 "SONY    ", "CD-ROM CDU-561  ", ""},     SDEV_NOLUNS},
492 	{{T_CDROM, T_REMOV,
493 	 "SONY    ", "CD-ROM CDU-8003A", ""},     SDEV_NOLUNS},
494 	{{T_CDROM, T_REMOV,
495 	 "SONY    ", "CD-ROM CDU-8012 ", ""},     SDEV_NOLUNS},
496 	{{T_CDROM, T_REMOV,
497 	 "TEAC    ", "CD-ROM          ", "1.06"}, SDEV_NOLUNS},
498 	{{T_CDROM, T_REMOV,
499 	 "TEAC    ", "CD-ROM CD-56S   ", "1.0B"}, SDEV_NOLUNS},
500 	{{T_CDROM, T_REMOV,
501 	 "TEXEL   ", "CD-ROM          ", "1.06"}, SDEV_NOLUNS},
502 	{{T_CDROM, T_REMOV,
503 	 "TEXEL   ", "CD-ROM DM-XX24 K", "1.09"}, SDEV_NOLUNS},
504 	{{T_CDROM, T_REMOV,
505 	 "TEXEL   ", "CD-ROM DM-XX24 K", "1.10"}, SDEV_NOLUNS},
506 	{{T_CDROM, T_REMOV,
507 	 "TOSHIBA ", "XM-4101TASUNSLCD", "1755"}, SDEV_NOLUNS|SDEV_NOSYNC},
508 	{{T_CDROM, T_REMOV,
509 	 "ShinaKen", "CD-ROM DM-3x1S", "1.04"}, SDEV_NOLUNS},
510 	{{T_CDROM, T_REMOV,
511 	 "JVC     ", "R2626",            ""},     SDEV_NOLUNS},
512 	{{T_CDROM, T_REMOV,
513 	 "YAMAHA", "CRW8424S",           ""},     SDEV_NOLUNS},
514 	{{T_CDROM, T_REMOV,
515 	 "VMware", "Virtual",           "1.0"},
516 				SDEV_NOSTARTUNIT|ADEV_NODOORLOCK},
517 
518 	{{T_DIRECT, T_FIXED,
519 	 "MICROP  ", "1588-15MBSUN0669", ""},     SDEV_AUTOSAVE},
520 	{{T_DIRECT, T_FIXED,
521 	 "MICROP  ", "2217-15MQ1091501", ""},     SDEV_NOSYNCCACHE},
522 	{{T_OPTICAL, T_REMOV,
523 	 "EPSON   ", "OMD-5010        ", "3.08"}, SDEV_NOLUNS},
524 	{{T_DIRECT, T_FIXED,
525 	"TOSHIBA ", "CD-ROM XM-3401TA", "0283"}, SDEV_CDROM|SDEV_NOLUNS},
526 	{{T_DIRECT, T_FIXED,
527 	"TOSHIBA ", "CD-ROM DRIVE:XM", "1971"}, SDEV_CDROM|SDEV_NOLUNS},
528 	{{T_DIRECT, T_FIXED,
529 	 "ADAPTEC ", "AEC-4412BD",       "1.2A"}, SDEV_NOMODESENSE},
530 	{{T_DIRECT, T_FIXED,
531 	 "DEC     ", "RZ55     (C) DEC", ""},     SDEV_AUTOSAVE},
532 	{{T_DIRECT, T_FIXED,
533 	 "EMULEX  ", "MD21/S2     ESDI", "A00"},  SDEV_FORCELUNS|SDEV_AUTOSAVE},
534 	/* Gives non-media hardware failure in response to start-unit command */
535 	{{T_DIRECT, T_FIXED,
536 	 "HITACHI", "DK515C",		"CP16"},  SDEV_NOSTARTUNIT},
537 	{{T_DIRECT, T_FIXED,
538 	 "HITACHI", "DK515C",		"CP15"},  SDEV_NOSTARTUNIT},
539 	{{T_DIRECT, T_FIXED,
540 	 "HP      ", "C372",             ""},     SDEV_NOTAG},
541 	{{T_DIRECT, T_FIXED,
542 	 "IBMRAID ", "0662S",		 ""},     SDEV_AUTOSAVE},
543 	{{T_DIRECT, T_FIXED,
544 	 "IBM     ", "0663H",		 ""},     SDEV_AUTOSAVE},
545 	{{T_DIRECT, T_FIXED,
546 	 "IBM",	     "0664",		 ""},     SDEV_AUTOSAVE},
547 	{{T_DIRECT, T_FIXED,
548 	 "IBM     ", "H3171-S2",	 ""},	  SDEV_NOLUNS|SDEV_AUTOSAVE},
549 	{{T_DIRECT, T_FIXED,
550 	 "IBM     ", "KZ-C",		 ""},	  SDEV_AUTOSAVE},
551 	/* Broken IBM disk */
552 	{{T_DIRECT, T_FIXED,
553 	 ""	   , "DFRSS2F",		 ""},	  SDEV_AUTOSAVE},
554 	{{T_DIRECT, T_REMOV,
555 	 "MPL     ", "MC-DISK-        ", ""},     SDEV_NOLUNS},
556 	{{T_DIRECT, T_FIXED,
557 	 "MAXTOR  ", "XT-3280         ", ""},     SDEV_NOLUNS},
558 	{{T_DIRECT, T_FIXED,
559 	 "MAXTOR  ", "XT-4380S        ", ""},     SDEV_NOLUNS},
560 	{{T_DIRECT, T_FIXED,
561 	 "MAXTOR  ", "MXT-1240S       ", ""},     SDEV_NOLUNS},
562 	{{T_DIRECT, T_FIXED,
563 	 "MAXTOR  ", "XT-4170S        ", ""},     SDEV_NOLUNS},
564 	{{T_DIRECT, T_FIXED,
565 	 "MAXTOR  ", "XT-8760S",         ""},     SDEV_NOLUNS},
566 	{{T_DIRECT, T_FIXED,
567 	 "MAXTOR  ", "LXT-213S        ", ""},     SDEV_NOLUNS},
568 	{{T_DIRECT, T_FIXED,
569 	 "MAXTOR  ", "LXT-213S SUN0207", ""},     SDEV_NOLUNS},
570 	{{T_DIRECT, T_FIXED,
571 	 "MAXTOR  ", "LXT-200S        ", ""},     SDEV_NOLUNS},
572 	{{T_DIRECT, T_FIXED,
573 	 "MEGADRV ", "EV1000",           ""},     SDEV_NOMODESENSE},
574 	{{T_DIRECT, T_FIXED,
575 	 "MST     ", "SnapLink        ", ""},     SDEV_NOLUNS},
576 	{{T_DIRECT, T_FIXED,
577 	 "NEC     ", "D3847           ", "0307"}, SDEV_NOLUNS},
578 	{{T_DIRECT, T_FIXED,
579 	 "QUANTUM ", "ELS85S          ", ""},     SDEV_AUTOSAVE},
580 	{{T_DIRECT, T_FIXED,
581 	 "QUANTUM ", "LPS525S         ", ""},     SDEV_NOLUNS},
582 	{{T_DIRECT, T_FIXED,
583 	 "QUANTUM ", "P105S 910-10-94x", ""},     SDEV_NOLUNS},
584 	{{T_DIRECT, T_FIXED,
585 	 "QUANTUM ", "PD1225S         ", ""},     SDEV_NOLUNS},
586 	{{T_DIRECT, T_FIXED,
587 	 "QUANTUM ", "PD210S   SUN0207", ""},     SDEV_NOLUNS},
588 	{{T_DIRECT, T_FIXED,
589 	 "RODIME  ", "RO3000S         ", ""},     SDEV_NOLUNS},
590 	{{T_DIRECT, T_FIXED,
591 	 "SEAGATE ", "ST125N          ", ""},     SDEV_NOLUNS},
592 	{{T_DIRECT, T_FIXED,
593 	 "SEAGATE ", "ST157N          ", ""},     SDEV_NOLUNS},
594 	{{T_DIRECT, T_FIXED,
595 	 "SEAGATE ", "ST296           ", ""},     SDEV_NOLUNS},
596 	{{T_DIRECT, T_FIXED,
597 	 "SEAGATE ", "ST296N          ", ""},     SDEV_NOLUNS},
598 	{{T_DIRECT, T_FIXED,
599 	 "SEAGATE ", "ST19171",          ""},     SDEV_NOMODESENSE},
600 	{{T_DIRECT, T_FIXED,
601 	 "SEAGATE ", "ST34501FC       ", ""},     SDEV_NOMODESENSE},
602 	{{T_DIRECT, T_FIXED,
603 	 "TOSHIBA ", "MK538FB         ", "6027"}, SDEV_NOLUNS},
604 	{{T_DIRECT, T_FIXED,
605 	 "VMware", "Virtual",           "1.0"},
606 				SDEV_NOSTARTUNIT|ADEV_NODOORLOCK},
607 
608 	{{T_DIRECT, T_REMOV,
609 	 "iomega", "jaz 1GB", 		 ""},	  SDEV_NOMODESENSE},
610 	{{T_DIRECT, T_REMOV,
611 	 "IOMEGA", "ZIP 100",		 ""},	  SDEV_NOMODESENSE},
612 	{{T_DIRECT, T_REMOV,
613 	 "IOMEGA", "ZIP 100",		 "J.03"}, SDEV_NOMODESENSE|SDEV_NOLUNS},
614 	/* Letting the motor run kills floppy drives and disks quite fast. */
615 	{{T_DIRECT, T_REMOV,
616 	 "TEAC", "FC-1",		 ""},	  SDEV_NOSTARTUNIT},
617 
618 	{{T_DIRECT, T_REMOV,
619 	 "Y-E DATA", "USB-FDU",		 "3.04"}, SDEV_NOMODESENSE},
620 	{{T_DIRECT, T_REMOV,
621 	 "TEAC", "FD-05PUB",		 "1026"}, SDEV_NOMODESENSE},
622 
623 	/* XXX: QIC-36 tape behind Emulex adapter.  Very broken. */
624 	{{T_SEQUENTIAL, T_REMOV,
625 	 "        ", "                ", "    "}, SDEV_NOLUNS},
626 	{{T_SEQUENTIAL, T_REMOV,
627 	 "CALIPER ", "CP150           ", ""},     SDEV_NOLUNS},
628 	{{T_SEQUENTIAL, T_REMOV,
629 	 "EXABYTE ", "EXB-8200        ", ""},     SDEV_NOLUNS},
630 	{{T_SEQUENTIAL, T_REMOV,
631 	 "SONY    ", "GY-10C          ", ""},     SDEV_NOLUNS},
632 	{{T_SEQUENTIAL, T_REMOV,
633 	 "SONY    ", "SDT-2000        ", "2.09"}, SDEV_NOLUNS},
634 	{{T_SEQUENTIAL, T_REMOV,
635 	 "SONY    ", "SDT-5000        ", "3."},   SDEV_NOSYNC|SDEV_NOWIDE},
636 	{{T_SEQUENTIAL, T_REMOV,
637 	 "SONY    ", "SDT-5200        ", "3."},   SDEV_NOLUNS},
638 	{{T_SEQUENTIAL, T_REMOV,
639 	 "TANDBERG", " TDC 3600       ", ""},     SDEV_NOLUNS},
640 	/* Following entry reported as a Tandberg 3600; ref. PR1933 */
641 	{{T_SEQUENTIAL, T_REMOV,
642 	 "ARCHIVE ", "VIPER 150  21247", ""},     SDEV_NOLUNS},
643 	/* Following entry for a Cipher ST150S; ref. PR4171 */
644 	{{T_SEQUENTIAL, T_REMOV,
645 	 "ARCHIVE ", "VIPER 1500 21247", "2.2G"}, SDEV_NOLUNS},
646 	{{T_SEQUENTIAL, T_REMOV,
647 	 "ARCHIVE ", "Python 28454-XXX", ""},     SDEV_NOLUNS},
648 	{{T_SEQUENTIAL, T_REMOV,
649 	 "WANGTEK ", "5099ES SCSI",      ""},     SDEV_NOLUNS},
650 	{{T_SEQUENTIAL, T_REMOV,
651 	 "WANGTEK ", "5150ES SCSI",      ""},     SDEV_NOLUNS},
652 	{{T_SEQUENTIAL, T_REMOV,
653 	 "WANGTEK ", "SCSI-36",		 ""},     SDEV_NOLUNS},
654 	{{T_SEQUENTIAL, T_REMOV,
655 	 "WangDAT ", "Model 1300      ", "02.4"}, SDEV_NOSYNC|SDEV_NOWIDE},
656 	{{T_SEQUENTIAL, T_REMOV,
657 	 "WangDAT ", "Model 2600      ", "01.7"}, SDEV_NOSYNC|SDEV_NOWIDE},
658 	{{T_SEQUENTIAL, T_REMOV,
659 	 "WangDAT ", "Model 3200      ", "02.2"}, SDEV_NOSYNC|SDEV_NOWIDE},
660 	{{T_SEQUENTIAL, T_REMOV,
661 	 "TEAC    ", "MT-2ST/N50      ", ""},     SDEV_NOLUNS},
662 
663 	{{T_SCANNER, T_FIXED,
664 	 "RICOH   ", "IS60            ", "1R08"}, SDEV_NOLUNS},
665 	{{T_SCANNER, T_FIXED,
666 	 "UMAX    ", "Astra 1200S     ", "V2.9"}, SDEV_NOLUNS},
667 	{{T_SCANNER, T_FIXED,
668 	 "UMAX    ", "Astra 1220S     ", ""}, SDEV_NOLUNS},
669 	{{T_SCANNER, T_FIXED,
670 	 "UMAX    ", "UMAX S-6E       ", "V2.0"}, SDEV_NOLUNS},
671 	{{T_SCANNER, T_FIXED,
672 	 "UMAX    ", "UMAX S-12       ", "V2.1"}, SDEV_NOLUNS},
673 	{{T_SCANNER, T_FIXED,
674 	 "ULTIMA  ", "A6000C          ", ""}, SDEV_NOLUNS},
675 	{{T_PROCESSOR, T_FIXED,
676 	 "SYMBIOS", "", ""},     SDEV_NOLUNS},
677 	{{T_PROCESSOR, T_FIXED,
678 	 "LITRONIC", "PCMCIA          ", ""},     SDEV_NOLUNS},
679 	{{T_CHANGER, T_REMOV,
680 	 "SONY    ", "CDL1100         ", ""},     SDEV_NOLUNS},
681 	{{T_ENCLOSURE, T_FIXED,
682 	 "SUN     ", "SENA            ", ""},     SDEV_NOLUNS},
683 };
684 
685 /*
686  * given a target and lun, ask the device what
687  * it is, and find the correct driver table
688  * entry.
689  */
690 int
691 scsi_probedev(scsi, target, lun)
692 	struct scsibus_softc *scsi;
693 	int target, lun;
694 {
695 	struct scsipi_link *sc_link;
696 	struct scsipi_inquiry_data inqbuf;
697 	struct scsi_quirk_inquiry_pattern *finger;
698 	int checkdtype, priority, docontinue;
699 	struct scsipibus_attach_args sa;
700 	struct cfdata *cf;
701 
702 	/*
703 	 * Assume no more luns to search after this one.
704 	 * If we successfully get Inquiry data and after
705 	 * merging quirks we find we can probe for more
706 	 * luns, we will.
707 	 */
708 	docontinue = 0;
709 
710 	/* Skip this slot if it is already attached. */
711 	if (scsi->sc_link[target][lun] != NULL)
712 		return (docontinue);
713 
714 	sc_link = malloc(sizeof(*sc_link), M_DEVBUF, M_NOWAIT);
715 	*sc_link = *scsi->adapter_link;
716 	sc_link->active = 0;
717 	sc_link->scsipi_scsi.target = target;
718 	sc_link->scsipi_scsi.lun = lun;
719 	sc_link->device = &probe_switch;
720 	TAILQ_INIT(&sc_link->pending_xfers);
721 
722 	/*
723 	 * Ask the device what it is
724 	 */
725 #if defined(SCSIDEBUG) && DEBUGTYPE == BUS_SCSI
726 	if (target == DEBUGTARGET && lun == DEBUGLUN)
727 		sc_link->flags |= DEBUGLEVEL;
728 #endif /* SCSIDEBUG */
729 
730 	(void) scsipi_test_unit_ready(sc_link,
731 	    XS_CTL_DISCOVERY | XS_CTL_IGNORE_ILLEGAL_REQUEST |
732 	    XS_CTL_IGNORE_NOT_READY | XS_CTL_IGNORE_MEDIA_CHANGE);
733 
734 #ifdef SCSI_2_DEF
735 	/* some devices need to be told to go to SCSI2 */
736 	/* However some just explode if you tell them this.. leave it out */
737 	scsi_change_def(sc_link, XS_CTL_DISCOVERY | XS_CTL_SILENT);
738 #endif /* SCSI_2_DEF */
739 
740 	/* Now go ask the device all about itself. */
741 	bzero(&inqbuf, sizeof(inqbuf));
742 	if (scsipi_inquire(sc_link, &inqbuf,
743 	    XS_CTL_DISCOVERY | XS_CTL_DATA_ONSTACK) != 0)
744 		goto bad;
745 
746 	{
747 		u_int8_t *extension = &inqbuf.flags1;
748 		int len = inqbuf.additional_length;
749 		while (len < 3)
750 			extension[len++] = '\0';
751 		while (len < 3 + 28)
752 			extension[len++] = ' ';
753 		while (len < 3 + 28 + 20)
754 			extension[len++] = '\0';
755 		while (len < 3 + 28 + 20 + 1)
756 			extension[len++] = '\0';
757 		while (len < 3 + 28 + 20 + 1 + 1)
758 			extension[len++] = '\0';
759 		while (len < 3 + 28 + 20 + 1 + 1 + (8*2))
760 			extension[len++] = ' ';
761 	}
762 
763 	sa.sa_sc_link = sc_link;
764 	sa.sa_inqbuf.type = inqbuf.device;
765 	sa.sa_inqbuf.removable = inqbuf.dev_qual2 & SID_REMOVABLE ?
766 	    T_REMOV : T_FIXED;
767 	sa.sa_inqbuf.vendor = inqbuf.vendor;
768 	sa.sa_inqbuf.product = inqbuf.product;
769 	sa.sa_inqbuf.revision = inqbuf.revision;
770 	sa.scsipi_info.scsi_version = inqbuf.version;
771 	sa.sa_inqptr = &inqbuf;
772 
773 	finger = (struct scsi_quirk_inquiry_pattern *)scsipi_inqmatch(
774 	    &sa.sa_inqbuf, (caddr_t)scsi_quirk_patterns,
775 	    sizeof(scsi_quirk_patterns)/sizeof(scsi_quirk_patterns[0]),
776 	    sizeof(scsi_quirk_patterns[0]), &priority);
777 
778 	/*
779 	 * Based upon the inquiry flags we got back, and if we're
780 	 * at SCSI-2 or better, set some limiting quirks.
781 	 */
782 	if ((inqbuf.version & SID_ANSII) >= 2) {
783 		if ((inqbuf.flags3 & SID_CmdQue) == 0)
784 			sc_link->quirks |= SDEV_NOTAG;
785 		if ((inqbuf.flags3 & SID_Sync) == 0)
786 			sc_link->quirks |= SDEV_NOSYNC;
787 		if ((inqbuf.flags3 & SID_WBus16) == 0)
788 			sc_link->quirks |= SDEV_NOWIDE;
789 	} else {
790 		/*
791 		 * Turn off command tagging for SCSI 1 devices.
792 		 * If the need arises, we could define a `SDEV_FORCETAG'
793 		 * quirk and set it for pre-SCSI2 devices that actually
794 		 * do support tagged commands.
795 		 */
796 		sc_link->quirks |= SDEV_NOTAG;
797 	}
798 
799 	/*
800 	 * Now apply any quirks from the table.
801 	 */
802 	if (priority != 0)
803 		sc_link->quirks |= finger->quirks;
804 	if ((inqbuf.version & SID_ANSII) == 0 &&
805 	    (sc_link->quirks & SDEV_FORCELUNS) == 0)
806 		sc_link->quirks |= SDEV_NOLUNS;
807 	sc_link->scsipi_scsi.scsi_version = inqbuf.version;
808 
809 	if (sc_link->quirks & SDEV_CDROM) {
810 		sc_link->quirks ^= SDEV_CDROM;
811 		inqbuf.dev_qual2 |= SID_REMOVABLE;
812 		sa.sa_inqbuf.type = inqbuf.device = ((inqbuf.device & ~SID_REMOVABLE) | T_CDROM);
813 		sa.sa_inqbuf.removable = T_REMOV;
814 
815 	}
816 
817 	if ((sc_link->quirks & SDEV_NOLUNS) == 0)
818 		docontinue = 1;
819 
820 	/*
821 	 * note what BASIC type of device it is
822 	 */
823 	if ((inqbuf.dev_qual2 & SID_REMOVABLE) != 0)
824 		sc_link->flags |= SDEV_REMOVABLE;
825 
826 	/*
827 	 * Any device qualifier that has the top bit set (qualifier&4 != 0)
828 	 * is vendor specific and won't match in this switch.
829 	 * All we do here is throw out bad/negative responses.
830 	 */
831 	checkdtype = 0;
832 	switch (inqbuf.device & SID_QUAL) {
833 	case SID_QUAL_LU_OK:
834 	case SID_QUAL_LU_OFFLINE:
835 		checkdtype = 1;
836 		break;
837 
838 	case SID_QUAL_RSVD:
839 	case SID_QUAL_BAD_LU:
840 		goto bad;
841 
842 	default:
843 		break;
844 	}
845 
846 	/* Let the adapter driver handle the device separatley if it wants. */
847 	if (sc_link->adapter->scsipi_accesschk != NULL &&
848 	    (*sc_link->adapter->scsipi_accesschk)(sc_link, &sa.sa_inqbuf))
849 		goto bad;
850 
851 	if (checkdtype) {
852 		switch (inqbuf.device & SID_TYPE) {
853 		case T_DIRECT:
854 		case T_SEQUENTIAL:
855 		case T_PRINTER:
856 		case T_PROCESSOR:
857 		case T_WORM:
858 		case T_CDROM:
859 		case T_SCANNER:
860 		case T_OPTICAL:
861 		case T_CHANGER:
862 		case T_COMM:
863 		case T_IT8_1:
864 		case T_IT8_2:
865 		case T_STORARRAY:
866 		case T_ENCLOSURE:
867 		case T_SIMPLE_DIRECT:
868 		case T_OPTIC_CARD_RW:
869 		case T_OBJECT_STORED:
870 		default:
871 			break;
872 		case T_NODEVICE:
873 			goto bad;
874 		}
875 		/*
876 		 * At this point we can also tell the adapter that it
877 		 * may negotiate things as appropriate.
878 		 */
879 		if (sc_link->adapter->scsipi_ioctl) {
880 			struct scbusaccel_args s;
881 			s.sa_target = target;
882 			s.sa_lun = lun;
883 			s.sa_flags = 0;
884 			if ((sc_link->quirks & SDEV_NOTAG) == 0)
885 				s.sa_flags |= SC_ACCEL_TAGS;
886 			if ((sc_link->quirks & SDEV_NOSYNC) == 0)
887 				s.sa_flags |= SC_ACCEL_SYNC;
888 			if ((sc_link->quirks & SDEV_NOWIDE) == 0)
889 				s.sa_flags |= SC_ACCEL_WIDE;
890 			(void) (*sc_link->adapter->scsipi_ioctl)
891 			    (sc_link, SCBUSACCEL, (caddr_t)&s, FWRITE, curproc);
892 		}
893 	}
894 
895 	if ((cf = config_search(scsibussubmatch, (struct device *)scsi,
896 	    &sa)) != NULL) {
897 		scsi->sc_link[target][lun] = sc_link;
898 		config_attach((struct device *)scsi, cf, &sa, scsibusprint);
899 	} else {
900 		scsibusprint(&sa, scsi->sc_dev.dv_xname);
901 		printf(" not configured\n");
902 		goto bad;
903 	}
904 
905 	return (docontinue);
906 
907 bad:
908 	free(sc_link, M_DEVBUF);
909 	return (docontinue);
910 }
911 
912 /****** Entry points for user control of the SCSI bus. ******/
913 
914 int
915 scsibusopen(dev, flag, fmt, p)
916 	dev_t dev;
917 	int flag, fmt;
918 	struct proc *p;
919 {
920 	struct scsibus_softc *sc;
921 	int error, unit = minor(dev);
922 
923 	if (unit >= scsibus_cd.cd_ndevs ||
924 	    (sc = scsibus_cd.cd_devs[unit]) == NULL)
925 		return (ENXIO);
926 
927 	if (sc->sc_flags & SCSIBUSF_OPEN)
928 		return (EBUSY);
929 
930 	if ((error = scsipi_adapter_addref(sc->adapter_link)) != 0)
931 		return (error);
932 
933 	sc->sc_flags |= SCSIBUSF_OPEN;
934 
935 	return (0);
936 }
937 
938 int
939 scsibusclose(dev, flag, fmt, p)
940 	dev_t dev;
941 	int flag, fmt;
942 	struct proc *p;
943 {
944 	struct scsibus_softc *sc = scsibus_cd.cd_devs[minor(dev)];
945 
946 	scsipi_adapter_delref(sc->adapter_link);
947 
948 	sc->sc_flags &= ~SCSIBUSF_OPEN;
949 
950 	return (0);
951 }
952 
953 int
954 scsibusioctl(dev, cmd, addr, flag, p)
955 	dev_t dev;
956 	u_long cmd;
957 	caddr_t addr;
958 	int flag;
959 	struct proc *p;
960 {
961 	struct scsibus_softc *sc = scsibus_cd.cd_devs[minor(dev)];
962 	struct scsipi_link *sc_link = sc->adapter_link;
963 	int error;
964 
965 	/*
966 	 * Enforce write permission for ioctls that change the
967 	 * state of the bus.  Host adapter specific ioctls must
968 	 * be checked by the adapter driver.
969 	 */
970 	switch (cmd) {
971 	case SCBUSIOSCAN:
972 	case SCBUSIORESET:
973 		if ((flag & FWRITE) == 0)
974 			return (EBADF);
975 	}
976 
977 	switch (cmd) {
978 	case SCBUSIOSCAN:
979 	    {
980 		struct scbusioscan_args *a =
981 		    (struct scbusioscan_args *)addr;
982 
983 		/* XXX Change interface to this function. */
984 		error = scsi_probe_busses(minor(dev), a->sa_target,
985 		    a->sa_lun);
986 		break;
987 	    }
988 
989 	case SCBUSIORESET:
990 		/* FALLTHROUGH */
991 	default:
992 		if (sc_link->adapter->scsipi_ioctl == NULL)
993 			error = ENOTTY;
994 		else
995 			error = (*sc_link->adapter->scsipi_ioctl)(sc_link,
996 			    cmd, addr, flag, p);
997 		break;
998 	}
999 
1000 	return (error);
1001 }
1002