Home | History | Annotate | Line # | Download | only in scsipi
scsiconf.c revision 1.295
      1 /*	$NetBSD: scsiconf.c,v 1.295 2022/01/28 14:02:45 jakllsch Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998, 1999, 2004 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  *
     20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * Originally written by Julian Elischer (julian (at) tfs.com)
     35  * for TRW Financial Systems for use under the MACH(2.5) operating system.
     36  *
     37  * TRW Financial Systems, in accordance with their agreement with Carnegie
     38  * Mellon University, makes this software available to CMU to distribute
     39  * or use in any manner that they see fit as long as this message is kept with
     40  * the software. For this reason TFS also grants any other persons or
     41  * organisations permission to use or modify this software.
     42  *
     43  * TFS supplies this software to be publicly redistributed
     44  * on the understanding that TFS is not responsible for the correct
     45  * functioning of this software in any circumstances.
     46  *
     47  * Ported to run under 386BSD by Julian Elischer (julian (at) tfs.com) Sept 1992
     48  */
     49 
     50 #include <sys/cdefs.h>
     51 __KERNEL_RCSID(0, "$NetBSD: scsiconf.c,v 1.295 2022/01/28 14:02:45 jakllsch Exp $");
     52 
     53 #include <sys/param.h>
     54 #include <sys/systm.h>
     55 #include <sys/kernel.h>
     56 #include <sys/proc.h>
     57 #include <sys/kthread.h>
     58 #include <sys/malloc.h>
     59 #include <sys/mutex.h>
     60 #include <sys/once.h>
     61 #include <sys/device.h>
     62 #include <sys/conf.h>
     63 #include <sys/fcntl.h>
     64 #include <sys/scsiio.h>
     65 #include <sys/queue.h>
     66 #include <sys/atomic.h>
     67 #include <sys/kmem.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 static const struct scsipi_periphsw scsi_probe_dev = {
     76 	NULL,
     77 	NULL,
     78 	NULL,
     79 	NULL,
     80 };
     81 
     82 struct scsi_initq {
     83 	struct scsipi_channel *sc_channel;
     84 	TAILQ_ENTRY(scsi_initq) scsi_initq;
     85 };
     86 
     87 static ONCE_DECL(scsi_conf_ctrl);
     88 static TAILQ_HEAD(, scsi_initq)	scsi_initq_head;
     89 static kmutex_t			scsibus_qlock;
     90 static kcondvar_t		scsibus_qcv;
     91 
     92 static int	scsi_probe_device(struct scsibus_softc *, int, int);
     93 
     94 static int	scsibusmatch(device_t, cfdata_t, void *);
     95 static void	scsibusattach(device_t, device_t, void *);
     96 static int	scsibusdetach(device_t, int flags);
     97 static int	scsibusrescan(device_t, const char *, const int *);
     98 static void	scsidevdetached(device_t, device_t);
     99 
    100 CFATTACH_DECL3_NEW(scsibus, sizeof(struct scsibus_softc),
    101     scsibusmatch, scsibusattach, scsibusdetach, NULL,
    102     scsibusrescan, scsidevdetached, DVF_DETACH_SHUTDOWN);
    103 
    104 extern struct cfdriver scsibus_cd;
    105 
    106 static dev_type_open(scsibusopen);
    107 static dev_type_close(scsibusclose);
    108 static dev_type_ioctl(scsibusioctl);
    109 
    110 const struct cdevsw scsibus_cdevsw = {
    111 	.d_open = scsibusopen,
    112 	.d_close = scsibusclose,
    113 	.d_read = noread,
    114 	.d_write = nowrite,
    115 	.d_ioctl = scsibusioctl,
    116 	.d_stop = nostop,
    117 	.d_tty = notty,
    118 	.d_poll = nopoll,
    119 	.d_mmap = nommap,
    120 	.d_kqfilter = nokqfilter,
    121 	.d_discard = nodiscard,
    122 	.d_flag = D_OTHER | D_MPSAFE
    123 };
    124 
    125 static int	scsibusprint(void *, const char *);
    126 static void	scsibus_discover_thread(void *);
    127 static void	scsibus_config(struct scsibus_softc *);
    128 
    129 const struct scsipi_bustype scsi_bustype = {
    130 	.bustype_type = SCSIPI_BUSTYPE_BUSTYPE(SCSIPI_BUSTYPE_SCSI,
    131 	    SCSIPI_BUSTYPE_SCSI_PSCSI),
    132 	.bustype_cmd = scsi_scsipi_cmd,
    133 	.bustype_interpret_sense = scsipi_interpret_sense,
    134 	.bustype_printaddr = scsi_print_addr,
    135 	.bustype_kill_pending = scsi_kill_pending,
    136 	.bustype_async_event_xfer_mode = scsi_async_event_xfer_mode,
    137 };
    138 
    139 const struct scsipi_bustype scsi_fc_bustype = {
    140 	.bustype_type = SCSIPI_BUSTYPE_BUSTYPE(SCSIPI_BUSTYPE_SCSI,
    141 	    SCSIPI_BUSTYPE_SCSI_FC),
    142 	.bustype_cmd = scsi_scsipi_cmd,
    143 	.bustype_interpret_sense = scsipi_interpret_sense,
    144 	.bustype_printaddr = scsi_print_addr,
    145 	.bustype_kill_pending = scsi_kill_pending,
    146 	.bustype_async_event_xfer_mode = scsi_fc_sas_async_event_xfer_mode,
    147 };
    148 
    149 const struct scsipi_bustype scsi_sas_bustype = {
    150 	.bustype_type = SCSIPI_BUSTYPE_BUSTYPE(SCSIPI_BUSTYPE_SCSI,
    151 	    SCSIPI_BUSTYPE_SCSI_SAS),
    152 	.bustype_cmd = scsi_scsipi_cmd,
    153 	.bustype_interpret_sense = scsipi_interpret_sense,
    154 	.bustype_printaddr = scsi_print_addr,
    155 	.bustype_kill_pending = scsi_kill_pending,
    156 	.bustype_async_event_xfer_mode = scsi_fc_sas_async_event_xfer_mode,
    157 };
    158 
    159 const struct scsipi_bustype scsi_usb_bustype = {
    160 	.bustype_type = SCSIPI_BUSTYPE_BUSTYPE(SCSIPI_BUSTYPE_SCSI,
    161 	    SCSIPI_BUSTYPE_SCSI_USB),
    162 	.bustype_cmd = scsi_scsipi_cmd,
    163 	.bustype_interpret_sense = scsipi_interpret_sense,
    164 	.bustype_printaddr = scsi_print_addr,
    165 	.bustype_kill_pending = scsi_kill_pending,
    166 	.bustype_async_event_xfer_mode = NULL,
    167 };
    168 
    169 static int
    170 scsibus_init(void)
    171 {
    172 
    173 	TAILQ_INIT(&scsi_initq_head);
    174 	mutex_init(&scsibus_qlock, MUTEX_DEFAULT, IPL_NONE);
    175 	cv_init(&scsibus_qcv, "scsinitq");
    176 	return 0;
    177 }
    178 
    179 int
    180 scsiprint(void *aux, const char *pnp)
    181 {
    182 	struct scsipi_channel *chan = aux;
    183 	struct scsipi_adapter *adapt = chan->chan_adapter;
    184 
    185 	/* only "scsibus"es can attach to "scsi"s; easy. */
    186 	if (pnp)
    187 		aprint_normal("scsibus at %s", pnp);
    188 
    189 	/* don't print channel if the controller says there can be only one. */
    190 	if (adapt->adapt_nchannels != 1)
    191 		aprint_normal(" channel %d", chan->chan_channel);
    192 
    193 	return (UNCONF);
    194 }
    195 
    196 static int
    197 scsibusmatch(device_t parent, cfdata_t cf, void *aux)
    198 {
    199 	struct scsipi_channel *chan = aux;
    200 
    201 	if (SCSIPI_BUSTYPE_TYPE(chan->chan_bustype->bustype_type) !=
    202 	    SCSIPI_BUSTYPE_SCSI)
    203 		return 0;
    204 
    205 	if (cf->cf_loc[SCSICF_CHANNEL] != chan->chan_channel &&
    206 	    cf->cf_loc[SCSICF_CHANNEL] != SCSICF_CHANNEL_DEFAULT)
    207 		return (0);
    208 
    209 	return (1);
    210 }
    211 
    212 static void
    213 scsibusattach(device_t parent, device_t self, void *aux)
    214 {
    215 	struct scsibus_softc *sc = device_private(self);
    216 	struct scsipi_channel *chan = aux;
    217 	struct scsi_initq *scsi_initq;
    218 
    219 	if (!pmf_device_register(self, NULL, NULL))
    220 		aprint_error_dev(self, "couldn't establish power handler\n");
    221 
    222 	sc->sc_dev = self;
    223 	sc->sc_channel = chan;
    224 	chan->chan_name = device_xname(sc->sc_dev);
    225 
    226 	aprint_naive(": SCSI bus\n");
    227 	aprint_normal(": %d target%s, %d lun%s per target\n",
    228 	    chan->chan_ntargets,
    229 	    chan->chan_ntargets == 1 ? "" : "s",
    230 	    chan->chan_nluns,
    231 	    chan->chan_nluns == 1 ? "" : "s");
    232 
    233 	/*
    234 	 * XXX
    235 	 * newer adapters support more than 256 outstanding commands
    236 	 * per periph and don't use the tag (they eventually allocate one
    237 	 * internally). Right now scsipi always allocate a tag and
    238 	 * is limited to 256 tags, per scsi specs.
    239 	 * this should be revisited
    240 	 */
    241 	if (chan->chan_flags & SCSIPI_CHAN_OPENINGS) {
    242 		if (chan->chan_max_periph > 256)
    243 			chan->chan_max_periph = 256;
    244 	} else {
    245 		if (chan->chan_adapter->adapt_max_periph > 256)
    246 			chan->chan_adapter->adapt_max_periph = 256;
    247 	}
    248 
    249 	if (atomic_inc_uint_nv(&chan_running(chan)) == 1)
    250 		mutex_init(chan_mtx(chan), MUTEX_DEFAULT, IPL_BIO);
    251 
    252 	cv_init(&chan->chan_cv_thr, "scshut");
    253 	cv_init(&chan->chan_cv_comp, "sccomp");
    254 	cv_init(&chan->chan_cv_xs, "xscmd");
    255 
    256 	if (scsipi_adapter_addref(chan->chan_adapter))
    257 		return;
    258 
    259 	RUN_ONCE(&scsi_conf_ctrl, scsibus_init);
    260 
    261 	/* Initialize the channel structure first */
    262 	chan->chan_init_cb = NULL;
    263 	chan->chan_init_cb_arg = NULL;
    264 
    265 	scsi_initq = malloc(sizeof(struct scsi_initq), M_DEVBUF, M_WAITOK);
    266 	scsi_initq->sc_channel = chan;
    267 	TAILQ_INSERT_TAIL(&scsi_initq_head, scsi_initq, scsi_initq);
    268         config_pending_incr(sc->sc_dev);
    269 	if (scsipi_channel_init(chan)) {
    270 		aprint_error_dev(sc->sc_dev, "failed to init channel\n");
    271 		return;
    272 	}
    273 
    274         /*
    275          * Create the discover thread
    276          */
    277         if (kthread_create(PRI_NONE, 0, NULL, scsibus_discover_thread, sc,
    278             &chan->chan_dthread, "%s-d", chan->chan_name)) {
    279                 aprint_error_dev(sc->sc_dev, "unable to create discovery "
    280 		    "thread for channel %d\n", chan->chan_channel);
    281                 return;
    282         }
    283 }
    284 
    285 static void
    286 scsibus_discover_thread(void *arg)
    287 {
    288 	struct scsibus_softc *sc = arg;
    289 
    290 	scsibus_config(sc);
    291 	sc->sc_channel->chan_dthread = NULL;
    292 	kthread_exit(0);
    293 }
    294 
    295 static void
    296 scsibus_config(struct scsibus_softc *sc)
    297 {
    298 	struct scsipi_channel *chan = sc->sc_channel;
    299 	struct scsi_initq *scsi_initq;
    300 
    301 #ifndef SCSI_DELAY
    302 #define SCSI_DELAY 2
    303 #endif
    304 	if ((chan->chan_flags & SCSIPI_CHAN_NOSETTLE) == 0 &&
    305 	    SCSI_DELAY > 0) {
    306 		aprint_normal_dev(sc->sc_dev,
    307 		    "waiting %d seconds for devices to settle...\n",
    308 		    SCSI_DELAY);
    309 		/* ...an identifier we know no one will use... */
    310 		kpause("scsidly", false, SCSI_DELAY * hz, NULL);
    311 	}
    312 
    313 	/* Make sure the devices probe in scsibus order to avoid jitter. */
    314 	mutex_enter(&scsibus_qlock);
    315 	for (;;) {
    316 		scsi_initq = TAILQ_FIRST(&scsi_initq_head);
    317 		if (scsi_initq->sc_channel == chan)
    318 			break;
    319 		cv_wait(&scsibus_qcv, &scsibus_qlock);
    320 	}
    321 	mutex_exit(&scsibus_qlock);
    322 
    323 	scsi_probe_bus(sc, -1, -1);
    324 
    325 	mutex_enter(&scsibus_qlock);
    326 	TAILQ_REMOVE(&scsi_initq_head, scsi_initq, scsi_initq);
    327 	cv_broadcast(&scsibus_qcv);
    328 	mutex_exit(&scsibus_qlock);
    329 
    330 	free(scsi_initq, M_DEVBUF);
    331 
    332 	scsipi_adapter_delref(chan->chan_adapter);
    333 
    334 	config_pending_decr(sc->sc_dev);
    335 }
    336 
    337 static int
    338 scsibusdetach(device_t self, int flags)
    339 {
    340 	struct scsibus_softc *sc = device_private(self);
    341 	struct scsipi_channel *chan = sc->sc_channel;
    342 	int error;
    343 
    344 	/*
    345 	 * Defer while discovery thread is running
    346 	 */
    347 	while (chan->chan_dthread != NULL)
    348 		kpause("scsibusdet", false, hz, NULL);
    349 
    350 	/*
    351 	 * Detach all of the periphs.
    352 	 */
    353 	error = scsipi_target_detach(chan, -1, -1, flags);
    354 	if (error)
    355 		return error;
    356 
    357 	pmf_device_deregister(self);
    358 
    359 	/*
    360 	 * Shut down the channel.
    361 	 */
    362 	scsipi_channel_shutdown(chan);
    363 
    364 	cv_destroy(&chan->chan_cv_xs);
    365 	cv_destroy(&chan->chan_cv_comp);
    366 	cv_destroy(&chan->chan_cv_thr);
    367 
    368 	if (atomic_dec_uint_nv(&chan_running(chan)) == 0)
    369 		mutex_destroy(chan_mtx(chan));
    370 
    371 	return 0;
    372 }
    373 
    374 static int
    375 lun_compar(const void *a, const void *b)
    376 {
    377 	const uint16_t * const la = a, * const lb = b;
    378 
    379 	if (*la < *lb)
    380 		return -1;
    381 	if (*la > *lb)
    382 		return 1;
    383 	return 0;
    384 }
    385 
    386 static int
    387 scsi_report_luns(struct scsibus_softc *sc, int target,
    388     uint16_t ** const luns, size_t *nluns)
    389 {
    390 	struct scsi_report_luns replun;
    391 	struct scsi_report_luns_header *rlr;
    392 	struct scsi_report_luns_lun *lunp;
    393 
    394 	struct scsipi_channel *chan = sc->sc_channel;
    395 	struct scsipi_inquiry_data inqbuf;
    396 	struct scsipi_periph *periph;
    397 	uint16_t tmp;
    398 
    399 	int error;
    400 	size_t i, rlrlen;
    401 
    402 	periph = scsipi_alloc_periph(M_WAITOK);
    403 	periph->periph_channel = chan;
    404 	periph->periph_switch = &scsi_probe_dev;
    405 
    406 	periph->periph_target = target;
    407 	periph->periph_lun = 0;
    408 	periph->periph_quirks = chan->chan_defquirks;
    409 
    410 	if ((error = scsipi_inquire(periph, &inqbuf,
    411 	    XS_CTL_DISCOVERY | XS_CTL_SILENT)))
    412 		goto end2;
    413 	periph->periph_version = inqbuf.version & SID_ANSII;
    414 	if (periph->periph_version < 3) {
    415 		error = ENOTSUP;
    416 		goto end2;
    417 	}
    418 
    419 	rlrlen = sizeof(*rlr) + sizeof(*lunp) * 1;
    420 
    421 again:
    422 	rlr = kmem_zalloc(rlrlen, KM_SLEEP);
    423 
    424 	replun.opcode = SCSI_REPORT_LUNS;
    425 	replun.selectreport = SELECTREPORT_NORMAL;
    426 	_lto4b(rlrlen, replun.alloclen);
    427 
    428 	error = scsipi_command(periph, (void *)&replun, sizeof(replun),
    429 	    (void *)rlr, rlrlen, SCSIPIRETRIES, 10000, NULL,
    430 	    XS_CTL_DATA_IN | XS_CTL_DISCOVERY | XS_CTL_SILENT);
    431 	if (error)
    432 		goto end;
    433 
    434 	if (sizeof(*rlr) + _4btol(rlr->length) > rlrlen &&
    435 	    sizeof(*rlr) + _4btol(rlr->length) <= 32) {
    436 	    	const size_t old_rlrlen = rlrlen;
    437 		rlrlen = sizeof(*rlr) + uimin(_4btol(rlr->length),
    438 		    16383 * sizeof(*lunp));
    439 		kmem_free(rlr, old_rlrlen);
    440 		rlr = NULL;
    441 		goto again;
    442 	}
    443 
    444 	KASSERT(nluns != NULL);
    445 	*nluns = (rlrlen - sizeof(*rlr)) / sizeof(*lunp);
    446 
    447 	KASSERT(luns != NULL);
    448 	*luns = kmem_alloc(*nluns * sizeof(**luns), KM_SLEEP);
    449 
    450 	for (i = 0; i < *nluns; i++) {
    451 		lunp = &((struct scsi_report_luns_lun *)&rlr[1])[i];
    452 		switch (lunp->lun[0] & 0xC0) {
    453 		default:
    454 			scsi_print_addr(periph);
    455 			printf("LUN %016"PRIx64" ignored\n", _8btol(lunp->lun));
    456 			(*luns)[i] = 0;
    457 			break;
    458 		case 0x40:
    459 			(*luns)[i] = _2btol(&lunp->lun[0]) & 0x3FFF;
    460 			break;
    461 		case 0x00:
    462 			(*luns)[i] = _2btol(&lunp->lun[0]) & 0x00FF;
    463 			break;
    464 		}
    465 	}
    466 
    467 	kheapsort(*luns, *nluns, sizeof(**luns), lun_compar, &tmp);
    468 
    469 end:
    470 	if (rlr)
    471 		kmem_free(rlr, rlrlen);
    472 end2:
    473 	scsipi_free_periph(periph);
    474 	return error;
    475 }
    476 
    477 /*
    478  * Probe the requested scsi bus. It must be already set up.
    479  * target and lun optionally narrow the search if not -1
    480  */
    481 int
    482 scsi_probe_bus(struct scsibus_softc *sc, int target, int lun)
    483 {
    484 	struct scsipi_channel *chan = sc->sc_channel;
    485 	uint16_t *luns;
    486 	size_t nluns = 0; /* XXXGCC */
    487 	int maxtarget, mintarget, maxlun, minlun;
    488 	int error;
    489 
    490 	if (target == -1) {
    491 		maxtarget = chan->chan_ntargets - 1;
    492 		mintarget = 0;
    493 	} else {
    494 		if (target < 0 || target >= chan->chan_ntargets)
    495 			return (EINVAL);
    496 		maxtarget = mintarget = target;
    497 	}
    498 
    499 	if (lun == -1) {
    500 		maxlun = chan->chan_nluns - 1;
    501 		minlun = 0;
    502 	} else {
    503 		if (lun < 0 || lun >= chan->chan_nluns)
    504 			return (EINVAL);
    505 		maxlun = minlun = lun;
    506 	}
    507 
    508 	/*
    509 	 * Some HBAs provide an abstracted view of the bus; give them an
    510 	 * opportunity to re-scan it before we do.
    511 	 */
    512 	scsipi_adapter_ioctl(chan, SCBUSIOLLSCAN, NULL, 0, curproc);
    513 
    514 	if ((error = scsipi_adapter_addref(chan->chan_adapter)) != 0)
    515 		goto ret;
    516 	for (target = mintarget; target <= maxtarget; target++) {
    517 		if (target == chan->chan_id)
    518 			continue;
    519 		if (scsi_report_luns(sc, target, &luns, &nluns) == 0) {
    520 			for (size_t i = 0; i < nluns; i++)
    521 				if (luns[i] >= minlun && luns[i] <= maxlun)
    522 					scsi_probe_device(sc, target, luns[i]);
    523 		} else
    524 		for (lun = minlun; lun <= maxlun; lun++) {
    525 			/*
    526 			 * See if there's a device present, and configure it.
    527 			 */
    528 			if (scsi_probe_device(sc, target, lun) == 0)
    529 				break;
    530 			/* otherwise something says we should look further */
    531 		}
    532 
    533 		if (luns != NULL) {
    534 			kmem_free(luns, sizeof(*luns) * nluns);
    535 			luns = NULL;
    536 			nluns = 0;
    537 		}
    538 
    539 		/*
    540 		 * Now that we've discovered all of the LUNs on this
    541 		 * I_T Nexus, update the xfer mode for all of them
    542 		 * that we know about.
    543 		 */
    544 		scsipi_set_xfer_mode(chan, target, 1);
    545 	}
    546 
    547 	scsipi_adapter_delref(chan->chan_adapter);
    548 ret:
    549 	return (error);
    550 }
    551 
    552 static int
    553 scsibusrescan(device_t sc, const char *ifattr, const int *locators)
    554 {
    555 
    556 	KASSERT(ifattr && !strcmp(ifattr, "scsibus"));
    557 	KASSERT(locators);
    558 
    559 	return (scsi_probe_bus(device_private(sc),
    560 		locators[SCSIBUSCF_TARGET], locators[SCSIBUSCF_LUN]));
    561 }
    562 
    563 static void
    564 scsidevdetached(device_t self, device_t child)
    565 {
    566 	struct scsibus_softc *sc = device_private(self);
    567 	struct scsipi_channel *chan = sc->sc_channel;
    568 	struct scsipi_periph *periph;
    569 	int target, lun;
    570 
    571 	target = device_locator(child, SCSIBUSCF_TARGET);
    572 	lun = device_locator(child, SCSIBUSCF_LUN);
    573 
    574 	mutex_enter(chan_mtx(chan));
    575 
    576 	periph = scsipi_lookup_periph_locked(chan, target, lun);
    577 	KASSERT(periph != NULL && periph->periph_dev == child);
    578 
    579 	scsipi_remove_periph(chan, periph);
    580 	scsipi_free_periph(periph);
    581 
    582 	mutex_exit(chan_mtx(chan));
    583 }
    584 
    585 /*
    586  * Print out autoconfiguration information for a subdevice.
    587  *
    588  * This is a slight abuse of 'standard' autoconfiguration semantics,
    589  * because 'print' functions don't normally print the colon and
    590  * device information.  However, in this case that's better than
    591  * either printing redundant information before the attach message,
    592  * or having the device driver call a special function to print out
    593  * the standard device information.
    594  */
    595 static int
    596 scsibusprint(void *aux, const char *pnp)
    597 {
    598 	struct scsipibus_attach_args *sa = aux;
    599 	struct scsipi_inquiry_pattern *inqbuf;
    600 	u_int8_t type;
    601 	const char *dtype;
    602 	char vendor[33], product[65], revision[17];
    603 	int target, lun;
    604 
    605 	if (pnp != NULL)
    606 		aprint_normal("%s", pnp);
    607 
    608 	inqbuf = &sa->sa_inqbuf;
    609 
    610 	target = sa->sa_periph->periph_target;
    611 	lun = sa->sa_periph->periph_lun;
    612 	type = inqbuf->type & SID_TYPE;
    613 
    614 	dtype = scsipi_dtype(type);
    615 
    616 	strnvisx(vendor, sizeof(vendor), inqbuf->vendor, 8,
    617 	    VIS_TRIM|VIS_SAFE|VIS_OCTAL);
    618 	strnvisx(product, sizeof(product), inqbuf->product, 16,
    619 	    VIS_TRIM|VIS_SAFE|VIS_OCTAL);
    620 	strnvisx(revision, sizeof(revision), inqbuf->revision, 4,
    621 	    VIS_TRIM|VIS_SAFE|VIS_OCTAL);
    622 
    623 	aprint_normal(" target %d lun %d: <%s, %s, %s> %s %s%s",
    624 		      target, lun, vendor, product, revision, dtype,
    625 		      inqbuf->removable ? "removable" : "fixed",
    626 		      (sa->sa_periph->periph_opcs != NULL)
    627 		        ? " timeout-info" : "");
    628 
    629 	return (UNCONF);
    630 }
    631 
    632 static const struct scsi_quirk_inquiry_pattern scsi_quirk_patterns[] = {
    633 	{{T_DIRECT, T_REMOV,
    634 	 "Apple   ", "iPod            ", ""},	  PQUIRK_START},
    635 	{{T_CDROM, T_REMOV,
    636 	 "CHINON  ", "CD-ROM CDS-431  ", ""},     PQUIRK_NOLUNS},
    637 	{{T_CDROM, T_REMOV,
    638 	 "CHINON  ", "CD-ROM CDS-435  ", ""},     PQUIRK_NOLUNS},
    639 	{{T_CDROM, T_REMOV,
    640 	 "Chinon  ", "CD-ROM CDS-525  ", ""},     PQUIRK_NOLUNS},
    641 	{{T_CDROM, T_REMOV,
    642 	 "CHINON  ", "CD-ROM CDS-535  ", ""},     PQUIRK_NOLUNS},
    643 	{{T_CDROM, T_REMOV,
    644 	 "DEC     ", "RRD42   (C) DEC ", ""},     PQUIRK_NOLUNS},
    645 	{{T_CDROM, T_REMOV,
    646 	 "DENON   ", "DRD-25X         ", "V"},    PQUIRK_NOLUNS},
    647 	{{T_CDROM, T_REMOV,
    648 	 "GENERIC ", "CRD-BP2         ", ""},     PQUIRK_NOLUNS},
    649 	{{T_CDROM, T_REMOV,
    650 	 "HP      ", "C4324/C4325     ", ""},     PQUIRK_NOLUNS},
    651 	{{T_CDROM, T_REMOV,
    652 	 "IMS     ", "CDD521/10       ", "2.06"}, PQUIRK_NOLUNS},
    653 	{{T_CDROM, T_REMOV,
    654 	 "MATSHITA", "CD-ROM CR-5XX   ", "1.0b"}, PQUIRK_NOLUNS},
    655 	{{T_CDROM, T_REMOV,
    656 	 "MEDAVIS ", "RENO CD-ROMX2A  ", ""},     PQUIRK_NOLUNS},
    657 	{{T_CDROM, T_REMOV,
    658 	 "MEDIAVIS", "CDR-H93MV       ", "1.3"},  PQUIRK_NOLUNS},
    659 	{{T_CDROM, T_REMOV,
    660 	 "NEC     ", "CD-ROM DRIVE:502", ""},     PQUIRK_NOLUNS},
    661 	{{T_CDROM, T_REMOV,
    662 	 "NEC     ", "CD-ROM DRIVE:55 ", ""},     PQUIRK_NOLUNS},
    663 	{{T_CDROM, T_REMOV,
    664 	 "NEC     ", "CD-ROM DRIVE:83 ", ""},     PQUIRK_NOLUNS},
    665 	{{T_CDROM, T_REMOV,
    666 	 "NEC     ", "CD-ROM DRIVE:84 ", ""},     PQUIRK_NOLUNS},
    667 	{{T_CDROM, T_REMOV,
    668 	 "NEC     ", "CD-ROM DRIVE:841", ""},     PQUIRK_NOLUNS},
    669         {{T_CDROM, T_REMOV,
    670 	 "OLYMPUS ", "CDS620E         ", "1.1d"},
    671 			       PQUIRK_NOLUNS|PQUIRK_NOSYNC|PQUIRK_NOCAPACITY},
    672 	{{T_CDROM, T_REMOV,
    673 	 "PIONEER ", "CD-ROM DR-124X  ", "1.01"}, PQUIRK_NOLUNS},
    674         {{T_CDROM, T_REMOV,
    675          "PLEXTOR ", "CD-ROM PX-4XCS  ", "1.01"},
    676                                PQUIRK_NOLUNS|PQUIRK_NOSYNC},
    677 	{{T_CDROM, T_REMOV,
    678 	 "SONY    ", "CD-ROM CDU-541  ", ""},     PQUIRK_NOLUNS},
    679 	{{T_CDROM, T_REMOV,
    680 	 "SONY    ", "CD-ROM CDU-55S  ", ""},     PQUIRK_NOLUNS},
    681 	{{T_CDROM, T_REMOV,
    682 	 "SONY    ", "CD-ROM CDU-561  ", ""},     PQUIRK_NOLUNS},
    683 	{{T_CDROM, T_REMOV,
    684 	 "SONY    ", "CD-ROM CDU-76S", ""},
    685 				PQUIRK_NOLUNS|PQUIRK_NOSYNC|PQUIRK_NOWIDE},
    686 	{{T_CDROM, T_REMOV,
    687 	 "SONY    ", "CD-ROM CDU-8003A", ""},     PQUIRK_NOLUNS},
    688 	{{T_CDROM, T_REMOV,
    689 	 "SONY    ", "CD-ROM CDU-8012 ", ""},     PQUIRK_NOLUNS},
    690 	{{T_CDROM, T_REMOV,
    691 	 "TEAC    ", "CD-ROM          ", "1.06"}, PQUIRK_NOLUNS},
    692 	{{T_CDROM, T_REMOV,
    693 	 "TEAC    ", "CD-ROM CD-56S   ", "1.0B"}, PQUIRK_NOLUNS},
    694 	{{T_CDROM, T_REMOV,
    695 	 "TEXEL   ", "CD-ROM          ", "1.06"}, PQUIRK_NOLUNS},
    696 	{{T_CDROM, T_REMOV,
    697 	 "TEXEL   ", "CD-ROM DM-XX24 K", "1.09"}, PQUIRK_NOLUNS},
    698 	{{T_CDROM, T_REMOV,
    699 	 "TEXEL   ", "CD-ROM DM-XX24 K", "1.10"}, PQUIRK_NOLUNS},
    700 	{{T_CDROM, T_REMOV,
    701 	 "TOSHIBA ", "XM-4101TASUNSLCD", ""}, PQUIRK_NOLUNS|PQUIRK_NOSYNC},
    702 	/* "IBM CDRM00201     !F" 0724 is an IBM OEM Toshiba XM-4101BME */
    703 	{{T_CDROM, T_REMOV,
    704 	 "IBM     ", "CDRM00201     !F", "0724"}, PQUIRK_NOLUNS|PQUIRK_NOSYNC},
    705 	{{T_CDROM, T_REMOV,
    706 	 "ShinaKen", "CD-ROM DM-3x1S",   "1.04"}, PQUIRK_NOLUNS},
    707 	{{T_CDROM, T_REMOV,
    708 	 "JVC     ", "R2626",            ""},     PQUIRK_NOLUNS},
    709 	{{T_CDROM, T_REMOV,
    710 	 "YAMAHA", "CRW8424S",           ""},     PQUIRK_NOLUNS},
    711 	{{T_CDROM, T_REMOV,
    712 	 "NEC     ", "CD-ROM DRIVE:222", ""},	  PQUIRK_NOLUNS|PQUIRK_NOSYNC},
    713 
    714 	{{T_DIRECT, T_FIXED,
    715 	 "MICROP  ", "1588-15MBSUN0669", ""},     PQUIRK_AUTOSAVE},
    716 	{{T_DIRECT, T_FIXED,
    717 	 "MICROP  ", "2217-15MQ1091501", ""},     PQUIRK_NOSYNCCACHE},
    718 	{{T_OPTICAL, T_REMOV,
    719 	 "EPSON   ", "OMD-5010        ", "3.08"}, PQUIRK_NOLUNS},
    720 	{{T_DIRECT, T_FIXED,
    721 	 "ADAPTEC ", "AEC-4412BD",       "1.2A"}, PQUIRK_NOMODESENSE},
    722 	{{T_DIRECT, T_FIXED,
    723 	 "ADAPTEC ", "ACB-4000",         ""},     PQUIRK_FORCELUNS|PQUIRK_AUTOSAVE|PQUIRK_NOMODESENSE},
    724 	{{T_DIRECT, T_FIXED,
    725 	 "DEC     ", "RZ55     (C) DEC", ""},     PQUIRK_AUTOSAVE},
    726 	{{T_DIRECT, T_FIXED,
    727 	 "EMULEX  ", "MD21/S2     ESDI", "A00"},
    728 				PQUIRK_FORCELUNS|PQUIRK_AUTOSAVE},
    729 	{{T_DIRECT, T_FIXED,
    730 	 "MICROP",  "1548-15MZ1077801",  "HZ2P"}, PQUIRK_NOTAG},
    731 	{{T_DIRECT, T_FIXED,
    732 	 "HP      ", "C372",             ""},     PQUIRK_NOTAG},
    733 	{{T_DIRECT, T_FIXED,
    734 	 "IBMRAID ", "0662S",		 ""},     PQUIRK_AUTOSAVE},
    735 	{{T_DIRECT, T_FIXED,
    736 	 "IBM     ", "0663H",		 ""},     PQUIRK_AUTOSAVE},
    737 	{{T_DIRECT, T_FIXED,
    738 	 "IBM",	     "0664",		 ""},     PQUIRK_AUTOSAVE},
    739 	{{T_DIRECT, T_FIXED,
    740 	/* improperly report DT-only sync mode */
    741 	 "IBM     ", "DXHS36D",		 ""},
    742 				PQUIRK_CAP_SYNC|PQUIRK_CAP_WIDE16},
    743 	{{T_DIRECT, T_FIXED,
    744 	 "IBM     ", "DXHS18Y",		 ""},
    745 				PQUIRK_CAP_SYNC|PQUIRK_CAP_WIDE16},
    746 	{{T_DIRECT, T_FIXED,
    747 	 "IBM     ", "H3171-S2",	 ""},
    748 				PQUIRK_NOLUNS|PQUIRK_AUTOSAVE},
    749 	{{T_DIRECT, T_FIXED,
    750 	 "IBM     ", "KZ-C",		 ""},	  PQUIRK_AUTOSAVE},
    751 	/* Broken IBM disk */
    752 	{{T_DIRECT, T_FIXED,
    753 	 ""	   , "DFRSS2F",		 ""},	  PQUIRK_AUTOSAVE},
    754 	{{T_DIRECT, T_FIXED,
    755 	 "Initio  ", "",		 ""},	  PQUIRK_NOBIGMODESENSE},
    756 	{{T_DIRECT, T_FIXED,
    757 	 "JMicron ", "Generic         ", ""},	  PQUIRK_NOFUA},
    758 	{{T_DIRECT, T_REMOV,
    759 	 "MPL     ", "MC-DISK-        ", ""},     PQUIRK_NOLUNS},
    760 	{{T_DIRECT, T_FIXED,
    761 	 "MAXTOR  ", "XT-3280         ", ""},     PQUIRK_NOLUNS},
    762 	{{T_DIRECT, T_FIXED,
    763 	 "MAXTOR  ", "XT-4380S        ", ""},     PQUIRK_NOLUNS},
    764 	{{T_DIRECT, T_FIXED,
    765 	 "MAXTOR  ", "MXT-1240S       ", ""},     PQUIRK_NOLUNS},
    766 	{{T_DIRECT, T_FIXED,
    767 	 "MAXTOR  ", "XT-4170S        ", ""},     PQUIRK_NOLUNS},
    768 	{{T_DIRECT, T_FIXED,
    769 	 "MAXTOR  ", "XT-8760S",         ""},     PQUIRK_NOLUNS},
    770 	{{T_DIRECT, T_FIXED,
    771 	 "MAXTOR  ", "LXT-213S        ", ""},     PQUIRK_NOLUNS},
    772 	{{T_DIRECT, T_FIXED,
    773 	 "MAXTOR  ", "LXT-213S SUN0207", ""},     PQUIRK_NOLUNS},
    774 	{{T_DIRECT, T_FIXED,
    775 	 "MAXTOR  ", "LXT-200S        ", ""},     PQUIRK_NOLUNS},
    776 	{{T_DIRECT, T_FIXED,
    777 	 "MEGADRV ", "EV1000",           ""},     PQUIRK_NOMODESENSE},
    778 	{{T_DIRECT, T_FIXED,
    779 	 "MICROP", "1991-27MZ",          ""},     PQUIRK_NOTAG},
    780 	{{T_DIRECT, T_FIXED,
    781 	 "MST     ", "SnapLink        ", ""},     PQUIRK_NOLUNS},
    782 	{{T_DIRECT, T_FIXED,
    783 	 "NEC     ", "D3847           ", "0307"}, PQUIRK_NOLUNS},
    784 	{{T_DIRECT, T_FIXED,
    785 	 "QUANTUM ", "ELS85S          ", ""},     PQUIRK_AUTOSAVE},
    786 	{{T_DIRECT, T_FIXED,
    787 	 "QUANTUM ", "LPS525S         ", ""},     PQUIRK_NOLUNS},
    788 	{{T_DIRECT, T_FIXED,
    789 	 "QUANTUM ", "P105S 910-10-94x", ""},     PQUIRK_NOLUNS},
    790 	{{T_DIRECT, T_FIXED,
    791 	 "QUANTUM ", "PD1225S         ", ""},     PQUIRK_NOLUNS},
    792 	{{T_DIRECT, T_FIXED,
    793 	 "QUANTUM ", "PD210S   SUN0207", ""},     PQUIRK_NOLUNS},
    794 	{{T_DIRECT, T_FIXED,
    795 	 "QUANTUM ", "ATLAS IV 9 WLS", "0A0A"},   PQUIRK_CAP_NODT},
    796 	{{T_DIRECT, T_FIXED,
    797 	 "RODIME  ", "RO3000S         ", ""},     PQUIRK_NOLUNS},
    798 	{{T_DIRECT, T_FIXED,
    799 	 "SEAGATE ", "ST125N          ", ""},     PQUIRK_NOLUNS},
    800 	{{T_DIRECT, T_FIXED,
    801 	 "SEAGATE ", "ST157N          ", ""},     PQUIRK_NOLUNS},
    802 	{{T_DIRECT, T_FIXED,
    803 	 "SEAGATE ", "ST296           ", ""},     PQUIRK_NOLUNS},
    804 	{{T_DIRECT, T_FIXED,
    805 	 "SEAGATE ", "ST296N          ", ""},     PQUIRK_NOLUNS},
    806 	{{T_DIRECT, T_FIXED,
    807 	 "SEAGATE ", "ST318404LC      ", ""},     PQUIRK_NOLUNS},
    808 	{{T_DIRECT, T_FIXED,
    809 	 "SEAGATE ", "ST336753LC      ", ""},     PQUIRK_NOLUNS},
    810 	{{T_DIRECT, T_FIXED,
    811 	 "SEAGATE ", "ST336753LW      ", ""},     PQUIRK_NOLUNS},
    812 	{{T_DIRECT, T_FIXED,
    813 	 "SEAGATE ", "ST336754LC      ", ""},     PQUIRK_NOLUNS},
    814 	{{T_DIRECT, T_FIXED,
    815 	 "SEAGATE ", "ST39236LC       ", ""},     PQUIRK_NOLUNS},
    816 	{{T_DIRECT, T_FIXED,
    817 	 "SEAGATE ", "ST15150N        ", ""},     PQUIRK_NOTAG},
    818 	{{T_DIRECT, T_FIXED,
    819 	 "SEAGATE ", "ST19171",          ""},     PQUIRK_NOMODESENSE},
    820 	{{T_DIRECT, T_FIXED,
    821 	 "SEAGATE ", "ST32430N",         ""},     PQUIRK_CAP_SYNC},
    822 	{{T_DIRECT, T_FIXED,
    823 	 "SEAGATE ", "ST34501FC       ", ""},     PQUIRK_NOMODESENSE},
    824 	{{T_DIRECT, T_FIXED,
    825 	 "SEAGATE ", "SX910800N",        ""},     PQUIRK_NOTAG},
    826 	{{T_DIRECT, T_FIXED,
    827 	 "TOSHIBA ", "MK538FB         ", "6027"}, PQUIRK_NOLUNS},
    828 	{{T_DIRECT, T_FIXED,
    829 	 "MICROP  ", "1924",          ""},     PQUIRK_CAP_SYNC},
    830 	{{T_DIRECT, T_FIXED,
    831 	 "FUJITSU ", "M2266",         ""},     PQUIRK_CAP_SYNC},
    832 	{{T_DIRECT, T_FIXED,
    833 	 "FUJITSU ", "M2624S-512      ", ""},     PQUIRK_CAP_SYNC},
    834 	{{T_DIRECT, T_FIXED,
    835 	 "SEAGATE ", "SX336704LC"   , ""}, PQUIRK_CAP_SYNC | PQUIRK_CAP_WIDE16},
    836 	{{T_DIRECT, T_FIXED,
    837 	 "SEAGATE ", "SX173404LC",       ""},     PQUIRK_CAP_SYNC | PQUIRK_CAP_WIDE16},
    838 
    839 	{{T_DIRECT, T_REMOV,
    840 	 "IOMEGA", "ZIP 100",		 "J.03"}, PQUIRK_NOLUNS|PQUIRK_NOSYNC},
    841 	{{T_DIRECT, T_REMOV,
    842 	 "INSITE", "I325VM",             ""},     PQUIRK_NOLUNS},
    843 
    844 	/* XXX: QIC-36 tape behind Emulex adapter.  Very broken. */
    845 	{{T_SEQUENTIAL, T_REMOV,
    846 	 "        ", "                ", "    "}, PQUIRK_NOLUNS},
    847 	{{T_SEQUENTIAL, T_REMOV,
    848 	 "EMULEX  ", "MT-02 QIC       ", ""},     PQUIRK_NOLUNS},
    849 	{{T_SEQUENTIAL, T_REMOV,
    850 	 "CALIPER ", "CP150           ", ""},     PQUIRK_NOLUNS},
    851 	{{T_SEQUENTIAL, T_REMOV,
    852 	 "EXABYTE ", "EXB-8200        ", ""},     PQUIRK_NOLUNS},
    853 	{{T_SEQUENTIAL, T_REMOV,
    854 	 "SONY    ", "GY-10C          ", ""},     PQUIRK_NOLUNS},
    855 	{{T_SEQUENTIAL, T_REMOV,
    856 	 "SONY    ", "SDT-2000        ", "2.09"}, PQUIRK_NOLUNS},
    857 	{{T_SEQUENTIAL, T_REMOV,
    858 	 "SONY    ", "SDT-5000        ", "3."},   PQUIRK_NOSYNC|PQUIRK_NOWIDE},
    859 	{{T_SEQUENTIAL, T_REMOV,
    860 	 "SONY    ", "SDT-5200        ", "3."},   PQUIRK_NOLUNS},
    861 	{{T_SEQUENTIAL, T_REMOV,
    862 	 "TANDBERG", " TDC 3600       ", ""},     PQUIRK_NOLUNS},
    863 	/* Following entry reported as a Tandberg 3600; ref. PR1933 */
    864 	{{T_SEQUENTIAL, T_REMOV,
    865 	 "ARCHIVE ", "VIPER 150  21247", ""},     PQUIRK_NOLUNS},
    866 	/* Following entry for a Cipher ST150S; ref. PR4171 */
    867 	{{T_SEQUENTIAL, T_REMOV,
    868 	 "ARCHIVE ", "VIPER 1500 21247", "2.2G"}, PQUIRK_NOLUNS},
    869 	{{T_SEQUENTIAL, T_REMOV,
    870 	 "ARCHIVE ", "Python 28454-XXX", ""},     PQUIRK_NOLUNS},
    871 	{{T_SEQUENTIAL, T_REMOV,
    872 	 "WANGTEK ", "5099ES SCSI",      ""},     PQUIRK_NOLUNS},
    873 	{{T_SEQUENTIAL, T_REMOV,
    874 	 "WANGTEK ", "5150ES SCSI",      ""},     PQUIRK_NOLUNS},
    875 	{{T_SEQUENTIAL, T_REMOV,
    876 	 "WANGTEK ", "SCSI-36",		 ""},     PQUIRK_NOLUNS},
    877 	{{T_SEQUENTIAL, T_REMOV,
    878 	 "WangDAT ", "Model 1300      ", "02.4"}, PQUIRK_NOSYNC|PQUIRK_NOWIDE},
    879 	{{T_SEQUENTIAL, T_REMOV,
    880 	 "WangDAT ", "Model 2600      ", "01.7"}, PQUIRK_NOSYNC|PQUIRK_NOWIDE},
    881 	{{T_SEQUENTIAL, T_REMOV,
    882 	 "WangDAT ", "Model 3200      ", "02.2"}, PQUIRK_NOSYNC|PQUIRK_NOWIDE},
    883 	{{T_SEQUENTIAL, T_REMOV,
    884 	 "TEAC    ", "MT-2ST/N50      ", ""},     PQUIRK_NOLUNS},
    885 
    886 	{{T_SCANNER, T_FIXED,
    887 	 "RICOH   ", "IS60            ", "1R08"}, PQUIRK_NOLUNS},
    888 	{{T_SCANNER, T_FIXED,
    889 	 "UMAX    ", "Astra 1200S     ", "V2.9"}, PQUIRK_NOLUNS},
    890 	{{T_SCANNER, T_FIXED,
    891 	 "UMAX    ", "Astra 1220S     ", ""},     PQUIRK_NOLUNS},
    892 	{{T_SCANNER, T_FIXED,
    893 	 "UMAX    ", "UMAX S-6E       ", "V2.0"}, PQUIRK_NOLUNS},
    894 	{{T_SCANNER, T_FIXED,
    895 	 "UMAX    ", "UMAX S-12       ", "V2.1"}, PQUIRK_NOLUNS},
    896 	{{T_SCANNER, T_FIXED,
    897 	 "ULTIMA  ", "A6000C          ", ""},     PQUIRK_NOLUNS},
    898 	{{T_PROCESSOR, T_FIXED,
    899 	 "ESG-SHV",  "SCA HSBP M15",     ""},     PQUIRK_NOLUNS},
    900 	{{T_PROCESSOR, T_FIXED,
    901 	 "SYMBIOS",  "",                 ""},     PQUIRK_NOLUNS},
    902 	{{T_PROCESSOR, T_FIXED,
    903 	 "LITRONIC", "PCMCIA          ", ""},     PQUIRK_NOLUNS},
    904 	{{T_CHANGER, T_REMOV,
    905 	 "SONY    ", "CDL1100         ", ""},     PQUIRK_NOLUNS},
    906 	{{T_ENCLOSURE, T_FIXED,
    907 	 "SUN     ", "SENA            ", ""},     PQUIRK_NOLUNS},
    908 };
    909 
    910 /*
    911  * given a target and lun, ask the device what
    912  * it is, and find the correct driver table
    913  * entry.
    914  */
    915 static int
    916 scsi_probe_device(struct scsibus_softc *sc, int target, int lun)
    917 {
    918 	struct scsipi_channel *chan = sc->sc_channel;
    919 	struct scsipi_periph *periph;
    920 	struct scsipi_inquiry_data inqbuf;
    921 	const struct scsi_quirk_inquiry_pattern *finger;
    922 	int checkdtype, priority, docontinue, quirks;
    923 	struct scsipibus_attach_args sa;
    924 	cfdata_t cf;
    925 	int locs[SCSIBUSCF_NLOCS];
    926 
    927 	/*
    928 	 * Assume no more LUNs to search after this one.
    929 	 * If we successfully get Inquiry data and after
    930 	 * merging quirks we find we can probe for more
    931 	 * LUNs, we will.
    932 	 */
    933 	docontinue = 0;
    934 
    935 	/* Skip this slot if it is already attached. */
    936 	if (scsipi_lookup_periph(chan, target, lun) != NULL)
    937 		return (docontinue);
    938 
    939 	periph = scsipi_alloc_periph(M_WAITOK);
    940 	periph->periph_channel = chan;
    941 	periph->periph_switch = &scsi_probe_dev;
    942 
    943 	periph->periph_target = target;
    944 	periph->periph_lun = lun;
    945 	periph->periph_quirks = chan->chan_defquirks;
    946 
    947 #ifdef SCSIPI_DEBUG
    948 	if (SCSIPI_DEBUG_TYPE == SCSIPI_BUSTYPE_SCSI &&
    949 	    SCSIPI_DEBUG_TARGET == target &&
    950 	    SCSIPI_DEBUG_LUN == lun)
    951 		periph->periph_dbflags |= SCSIPI_DEBUG_FLAGS;
    952 #endif
    953 
    954 	/*
    955 	 * Ask the device what it is
    956 	 */
    957 
    958 #ifdef SCSI_2_DEF
    959 	/* some devices need to be told to go to SCSI2 */
    960 	/* However some just explode if you tell them this.. leave it out */
    961 	scsi_change_def(periph, XS_CTL_DISCOVERY | XS_CTL_SILENT);
    962 #endif /* SCSI_2_DEF */
    963 
    964 	/* Now go ask the device all about itself. */
    965 	memset(&inqbuf, 0, sizeof(inqbuf));
    966 	{
    967 		u_int8_t *extension = &inqbuf.flags1;
    968 		int len = 0;
    969 		while (len < 3)
    970 			extension[len++] = '\0';
    971 		while (len < 3 + 28)
    972 			extension[len++] = ' ';
    973 		while (len < 3 + 28 + 20)
    974 			extension[len++] = '\0';
    975 		while (len < 3 + 28 + 20 + 1)
    976 			extension[len++] = '\0';
    977 		while (len < 3 + 28 + 20 + 1 + 1)
    978 			extension[len++] = '\0';
    979 		while (len < 3 + 28 + 20 + 1 + 1 + (8*2))
    980 			extension[len++] = ' ';
    981 	}
    982 	if (scsipi_inquire(periph, &inqbuf, XS_CTL_DISCOVERY | XS_CTL_SILENT))
    983 		goto bad;
    984 
    985 	periph->periph_type = inqbuf.device & SID_TYPE;
    986 	if (inqbuf.dev_qual2 & SID_REMOVABLE)
    987 		periph->periph_flags |= PERIPH_REMOVABLE;
    988 	periph->periph_version = inqbuf.version & SID_ANSII;
    989 
    990 	/*
    991 	 * Any device qualifier that has the top bit set (qualifier&4 != 0)
    992 	 * is vendor specific and won't match in this switch.
    993 	 * All we do here is throw out bad/negative responses.
    994 	 */
    995 	checkdtype = 0;
    996 	switch (inqbuf.device & SID_QUAL) {
    997 	case SID_QUAL_LU_PRESENT:
    998 		checkdtype = 1;
    999 		break;
   1000 
   1001 	case SID_QUAL_LU_NOTPRESENT:
   1002 	case SID_QUAL_reserved:
   1003 	case SID_QUAL_LU_NOT_SUPP:
   1004 		goto bad;
   1005 
   1006 	default:
   1007 		break;
   1008 	}
   1009 
   1010 	/* Let the adapter driver handle the device separately if it wants. */
   1011 	if (chan->chan_adapter->adapt_accesschk != NULL &&
   1012 	    (*chan->chan_adapter->adapt_accesschk)(periph, &sa.sa_inqbuf))
   1013 		goto bad;
   1014 
   1015 	if (checkdtype) {
   1016 		switch (periph->periph_type) {
   1017 		case T_DIRECT:
   1018 		case T_SEQUENTIAL:
   1019 		case T_PRINTER:
   1020 		case T_PROCESSOR:
   1021 		case T_WORM:
   1022 		case T_CDROM:
   1023 		case T_SCANNER:
   1024 		case T_OPTICAL:
   1025 		case T_CHANGER:
   1026 		case T_COMM:
   1027 		case T_IT8_1:
   1028 		case T_IT8_2:
   1029 		case T_STORARRAY:
   1030 		case T_ENCLOSURE:
   1031 		case T_SIMPLE_DIRECT:
   1032 		case T_OPTIC_CARD_RW:
   1033 		case T_OBJECT_STORED:
   1034 		default:
   1035 			break;
   1036 		case T_NODEVICE:
   1037 			goto bad;
   1038 		}
   1039 	}
   1040 
   1041 	sa.sa_periph = periph;
   1042 	sa.sa_inqbuf.type = inqbuf.device;
   1043 	sa.sa_inqbuf.removable = inqbuf.dev_qual2 & SID_REMOVABLE ?
   1044 	    T_REMOV : T_FIXED;
   1045 	sa.sa_inqbuf.vendor = inqbuf.vendor;
   1046 	sa.sa_inqbuf.product = inqbuf.product;
   1047 	sa.sa_inqbuf.revision = inqbuf.revision;
   1048 	sa.scsipi_info.scsi_version = inqbuf.version;
   1049 	sa.sa_inqptr = &inqbuf;
   1050 
   1051 	finger = scsipi_inqmatch(
   1052 	    &sa.sa_inqbuf, scsi_quirk_patterns,
   1053 	    sizeof(scsi_quirk_patterns)/sizeof(scsi_quirk_patterns[0]),
   1054 	    sizeof(scsi_quirk_patterns[0]), &priority);
   1055 
   1056 	if (finger != NULL)
   1057 		quirks = finger->quirks;
   1058 	else
   1059 		quirks = 0;
   1060 
   1061 	/*
   1062 	 * Determine the operating mode capabilities of the device.
   1063 	 */
   1064 	if (periph->periph_version >= 2) {
   1065 		if ((inqbuf.flags3 & SID_CmdQue) != 0 &&
   1066 		    (quirks & PQUIRK_NOTAG) == 0)
   1067 			periph->periph_cap |= PERIPH_CAP_TQING;
   1068 		if ((inqbuf.flags3 & SID_Linked) != 0)
   1069 			periph->periph_cap |= PERIPH_CAP_LINKCMDS;
   1070 		if ((inqbuf.flags3 & SID_Sync) != 0 &&
   1071 		    (quirks & PQUIRK_NOSYNC) == 0)
   1072 			periph->periph_cap |= PERIPH_CAP_SYNC;
   1073 		if ((inqbuf.flags3 & SID_WBus16) != 0 &&
   1074 		    (quirks & PQUIRK_NOWIDE) == 0)
   1075 			periph->periph_cap |= PERIPH_CAP_WIDE16;
   1076 		if ((inqbuf.flags3 & SID_WBus32) != 0 &&
   1077 		    (quirks & PQUIRK_NOWIDE) == 0)
   1078 			periph->periph_cap |= PERIPH_CAP_WIDE32;
   1079 		if ((inqbuf.flags3 & SID_SftRe) != 0)
   1080 			periph->periph_cap |= PERIPH_CAP_SFTRESET;
   1081 		if ((inqbuf.flags3 & SID_RelAdr) != 0)
   1082 			periph->periph_cap |= PERIPH_CAP_RELADR;
   1083 		/* SPC-2 */
   1084 		if (periph->periph_version >= 3 &&
   1085 		    !(quirks & PQUIRK_CAP_NODT)){
   1086 			/*
   1087 			 * Report ST clocking though CAP_WIDExx/CAP_SYNC.
   1088 			 * If the device only supports DT, clear these
   1089 			 * flags (DT implies SYNC and WIDE)
   1090 			 */
   1091 			switch (inqbuf.flags4 & SID_Clocking) {
   1092 			case SID_CLOCKING_DT_ONLY:
   1093 				periph->periph_cap &=
   1094 				    ~(PERIPH_CAP_SYNC |
   1095 				      PERIPH_CAP_WIDE16 |
   1096 				      PERIPH_CAP_WIDE32);
   1097 				/* FALLTHROUGH */
   1098 			case SID_CLOCKING_SD_DT:
   1099 				periph->periph_cap |= PERIPH_CAP_DT;
   1100 				break;
   1101 			default: /* ST only or invalid */
   1102 				/* nothing to do */
   1103 				break;
   1104 			}
   1105 		}
   1106 		if (periph->periph_version >= 3) {
   1107 			if (inqbuf.flags4 & SID_IUS)
   1108 				periph->periph_cap |= PERIPH_CAP_IUS;
   1109 			if (inqbuf.flags4 & SID_QAS)
   1110 				periph->periph_cap |= PERIPH_CAP_QAS;
   1111 		}
   1112 	}
   1113 	if (quirks & PQUIRK_CAP_SYNC)
   1114 		periph->periph_cap |= PERIPH_CAP_SYNC;
   1115 	if (quirks & PQUIRK_CAP_WIDE16)
   1116 		periph->periph_cap |= PERIPH_CAP_WIDE16;
   1117 
   1118 	/*
   1119 	 * Now apply any quirks from the table.
   1120 	 */
   1121 	periph->periph_quirks |= quirks;
   1122 	if (periph->periph_version == 0 &&
   1123 	    (periph->periph_quirks & PQUIRK_FORCELUNS) == 0)
   1124 		periph->periph_quirks |= PQUIRK_NOLUNS;
   1125 
   1126 	if ((periph->periph_quirks & PQUIRK_NOLUNS) == 0)
   1127 		docontinue = 1;
   1128 
   1129 	locs[SCSIBUSCF_TARGET] = target;
   1130 	locs[SCSIBUSCF_LUN] = lun;
   1131 
   1132 	KERNEL_LOCK(1, NULL);
   1133 	if ((cf = config_search(sc->sc_dev, &sa,
   1134 				CFARGS(.submatch = config_stdsubmatch,
   1135 				       .locators = locs))) != NULL) {
   1136 		scsipi_insert_periph(chan, periph);
   1137 
   1138 		/*
   1139 		 * Determine supported opcodes and timeouts if available.
   1140 		 * Only do this on peripherals reporting SCSI version 3
   1141 		 * or greater - this command isn't in the SCSI-2 spec. and
   1142 		 * it causes either timeouts or peripherals disappearing
   1143 		 * when sent to some SCSI-1 or SCSI-2 peripherals.
   1144 		 */
   1145 		if (periph->periph_version >= 3)
   1146 			scsipi_get_opcodeinfo(periph);
   1147 
   1148 		/*
   1149 		 * XXX Can't assign periph_dev here, because we'll
   1150 		 * XXX need it before config_attach() returns.  Must
   1151 		 * XXX assign it in periph driver.
   1152 		 */
   1153 		config_attach(sc->sc_dev, cf, &sa, scsibusprint,
   1154 		    CFARGS(.locators = locs));
   1155 		KERNEL_UNLOCK_ONE(NULL);
   1156 	} else {
   1157 		scsibusprint(&sa, device_xname(sc->sc_dev));
   1158 		aprint_normal(" not configured\n");
   1159 		KERNEL_UNLOCK_ONE(NULL);
   1160 		goto bad;
   1161 	}
   1162 
   1163 	return (docontinue);
   1164 
   1165 bad:
   1166 	scsipi_free_periph(periph);
   1167 	return (docontinue);
   1168 }
   1169 
   1170 /****** Entry points for user control of the SCSI bus. ******/
   1171 
   1172 static int
   1173 scsibusopen(dev_t dev, int flag, int fmt,
   1174     struct lwp *l)
   1175 {
   1176 	struct scsibus_softc *sc;
   1177 	int error, unit = minor(dev);
   1178 
   1179 	sc = device_lookup_private(&scsibus_cd, unit);
   1180 	if (sc == NULL)
   1181 		return (ENXIO);
   1182 
   1183 	if (sc->sc_flags & SCSIBUSF_OPEN)
   1184 		return (EBUSY);
   1185 
   1186 	if ((error = scsipi_adapter_addref(sc->sc_channel->chan_adapter)) != 0)
   1187 		return (error);
   1188 
   1189 	sc->sc_flags |= SCSIBUSF_OPEN;
   1190 
   1191 	return (0);
   1192 }
   1193 
   1194 static int
   1195 scsibusclose(dev_t dev, int flag, int fmt,
   1196     struct lwp *l)
   1197 {
   1198 	struct scsibus_softc *sc;
   1199 
   1200 	sc = device_lookup_private(&scsibus_cd, minor(dev));
   1201 	scsipi_adapter_delref(sc->sc_channel->chan_adapter);
   1202 
   1203 	sc->sc_flags &= ~SCSIBUSF_OPEN;
   1204 
   1205 	return (0);
   1206 }
   1207 
   1208 static int
   1209 scsibusioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
   1210 {
   1211 	struct scsibus_softc *sc;
   1212 	struct scsipi_channel *chan;
   1213 	int error;
   1214 
   1215 	sc = device_lookup_private(&scsibus_cd, minor(dev));
   1216 	chan = sc->sc_channel;
   1217 
   1218 	/*
   1219 	 * Enforce write permission for ioctls that change the
   1220 	 * state of the bus.  Host adapter specific ioctls must
   1221 	 * be checked by the adapter driver.
   1222 	 */
   1223 	switch (cmd) {
   1224 	case SCBUSIOSCAN:
   1225 	case SCBUSIODETACH:
   1226 	case SCBUSIORESET:
   1227 		if ((flag & FWRITE) == 0)
   1228 			return (EBADF);
   1229 	}
   1230 
   1231 	switch (cmd) {
   1232 	case SCBUSIOSCAN:
   1233 	    {
   1234 		struct scbusioscan_args *a =
   1235 		    (struct scbusioscan_args *)addr;
   1236 
   1237 		error = scsi_probe_bus(sc, a->sa_target, a->sa_lun);
   1238 		break;
   1239 	    }
   1240 
   1241 	case SCBUSIODETACH:
   1242 	    {
   1243 		struct scbusiodetach_args *a =
   1244 		    (struct scbusiodetach_args *)addr;
   1245 
   1246 		error = scsipi_target_detach(chan, a->sa_target, a->sa_lun, 0);
   1247 		break;
   1248 	    }
   1249 
   1250 
   1251 	case SCBUSIORESET:
   1252 		/* FALLTHROUGH */
   1253 	default:
   1254 		error = scsipi_adapter_ioctl(chan, cmd, addr, flag, l->l_proc);
   1255 		break;
   1256 	}
   1257 
   1258 	return (error);
   1259 }
   1260