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