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