Home | History | Annotate | Line # | Download | only in scsipi
scsiconf.c revision 1.296
      1 /*	$NetBSD: scsiconf.c,v 1.296 2022/01/28 18:23:28 christos 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.296 2022/01/28 18:23:28 christos 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 static void
    478 scsi_discover_luns(struct scsibus_softc *sc, int target, int minlun, int maxlun)
    479 {
    480 	uint16_t *luns;
    481 	size_t nluns;
    482 
    483 	if (scsi_report_luns(sc, target, &luns, &nluns) == 0) {
    484 		for (size_t i = 0; i < nluns; i++)
    485 			if (luns[i] >= minlun && luns[i] <= maxlun)
    486 				scsi_probe_device(sc, target, luns[i]);
    487 		kmem_free(luns, sizeof(*luns) * nluns);
    488 		return;
    489 	}
    490 
    491 	for (int lun = minlun; lun <= maxlun; lun++) {
    492 		/*
    493 		 * See if there's a device present, and configure it.
    494 		 */
    495 		if (scsi_probe_device(sc, target, lun) == 0)
    496 			break;
    497 		/* otherwise something says we should look further */
    498 	}
    499 }
    500 
    501 /*
    502  * Probe the requested scsi bus. It must be already set up.
    503  * target and lun optionally narrow the search if not -1
    504  */
    505 int
    506 scsi_probe_bus(struct scsibus_softc *sc, int target, int lun)
    507 {
    508 	struct scsipi_channel *chan = sc->sc_channel;
    509 	int maxtarget, mintarget, maxlun, minlun;
    510 	int error;
    511 
    512 	if (target == -1) {
    513 		maxtarget = chan->chan_ntargets - 1;
    514 		mintarget = 0;
    515 	} else {
    516 		if (target < 0 || target >= chan->chan_ntargets)
    517 			return (EINVAL);
    518 		maxtarget = mintarget = target;
    519 	}
    520 
    521 	if (lun == -1) {
    522 		maxlun = chan->chan_nluns - 1;
    523 		minlun = 0;
    524 	} else {
    525 		if (lun < 0 || lun >= chan->chan_nluns)
    526 			return (EINVAL);
    527 		maxlun = minlun = lun;
    528 	}
    529 
    530 	/*
    531 	 * Some HBAs provide an abstracted view of the bus; give them an
    532 	 * opportunity to re-scan it before we do.
    533 	 */
    534 	scsipi_adapter_ioctl(chan, SCBUSIOLLSCAN, NULL, 0, curproc);
    535 
    536 	if ((error = scsipi_adapter_addref(chan->chan_adapter)) != 0)
    537 		goto ret;
    538 	for (target = mintarget; target <= maxtarget; target++) {
    539 		if (target == chan->chan_id)
    540 			continue;
    541 
    542 		scsi_discover_luns(sc, target, minlun, maxlun);
    543 
    544 		/*
    545 		 * Now that we've discovered all of the LUNs on this
    546 		 * I_T Nexus, update the xfer mode for all of them
    547 		 * that we know about.
    548 		 */
    549 		scsipi_set_xfer_mode(chan, target, 1);
    550 	}
    551 
    552 	scsipi_adapter_delref(chan->chan_adapter);
    553 ret:
    554 	return (error);
    555 }
    556 
    557 static int
    558 scsibusrescan(device_t sc, const char *ifattr, const int *locators)
    559 {
    560 
    561 	KASSERT(ifattr && !strcmp(ifattr, "scsibus"));
    562 	KASSERT(locators);
    563 
    564 	return (scsi_probe_bus(device_private(sc),
    565 		locators[SCSIBUSCF_TARGET], locators[SCSIBUSCF_LUN]));
    566 }
    567 
    568 static void
    569 scsidevdetached(device_t self, device_t child)
    570 {
    571 	struct scsibus_softc *sc = device_private(self);
    572 	struct scsipi_channel *chan = sc->sc_channel;
    573 	struct scsipi_periph *periph;
    574 	int target, lun;
    575 
    576 	target = device_locator(child, SCSIBUSCF_TARGET);
    577 	lun = device_locator(child, SCSIBUSCF_LUN);
    578 
    579 	mutex_enter(chan_mtx(chan));
    580 
    581 	periph = scsipi_lookup_periph_locked(chan, target, lun);
    582 	KASSERT(periph != NULL && periph->periph_dev == child);
    583 
    584 	scsipi_remove_periph(chan, periph);
    585 	scsipi_free_periph(periph);
    586 
    587 	mutex_exit(chan_mtx(chan));
    588 }
    589 
    590 /*
    591  * Print out autoconfiguration information for a subdevice.
    592  *
    593  * This is a slight abuse of 'standard' autoconfiguration semantics,
    594  * because 'print' functions don't normally print the colon and
    595  * device information.  However, in this case that's better than
    596  * either printing redundant information before the attach message,
    597  * or having the device driver call a special function to print out
    598  * the standard device information.
    599  */
    600 static int
    601 scsibusprint(void *aux, const char *pnp)
    602 {
    603 	struct scsipibus_attach_args *sa = aux;
    604 	struct scsipi_inquiry_pattern *inqbuf;
    605 	u_int8_t type;
    606 	const char *dtype;
    607 	char vendor[33], product[65], revision[17];
    608 	int target, lun;
    609 
    610 	if (pnp != NULL)
    611 		aprint_normal("%s", pnp);
    612 
    613 	inqbuf = &sa->sa_inqbuf;
    614 
    615 	target = sa->sa_periph->periph_target;
    616 	lun = sa->sa_periph->periph_lun;
    617 	type = inqbuf->type & SID_TYPE;
    618 
    619 	dtype = scsipi_dtype(type);
    620 
    621 	strnvisx(vendor, sizeof(vendor), inqbuf->vendor, 8,
    622 	    VIS_TRIM|VIS_SAFE|VIS_OCTAL);
    623 	strnvisx(product, sizeof(product), inqbuf->product, 16,
    624 	    VIS_TRIM|VIS_SAFE|VIS_OCTAL);
    625 	strnvisx(revision, sizeof(revision), inqbuf->revision, 4,
    626 	    VIS_TRIM|VIS_SAFE|VIS_OCTAL);
    627 
    628 	aprint_normal(" target %d lun %d: <%s, %s, %s> %s %s%s",
    629 		      target, lun, vendor, product, revision, dtype,
    630 		      inqbuf->removable ? "removable" : "fixed",
    631 		      (sa->sa_periph->periph_opcs != NULL)
    632 		        ? " timeout-info" : "");
    633 
    634 	return (UNCONF);
    635 }
    636 
    637 static const struct scsi_quirk_inquiry_pattern scsi_quirk_patterns[] = {
    638 	{{T_DIRECT, T_REMOV,
    639 	 "Apple   ", "iPod            ", ""},	  PQUIRK_START},
    640 	{{T_CDROM, T_REMOV,
    641 	 "CHINON  ", "CD-ROM CDS-431  ", ""},     PQUIRK_NOLUNS},
    642 	{{T_CDROM, T_REMOV,
    643 	 "CHINON  ", "CD-ROM CDS-435  ", ""},     PQUIRK_NOLUNS},
    644 	{{T_CDROM, T_REMOV,
    645 	 "Chinon  ", "CD-ROM CDS-525  ", ""},     PQUIRK_NOLUNS},
    646 	{{T_CDROM, T_REMOV,
    647 	 "CHINON  ", "CD-ROM CDS-535  ", ""},     PQUIRK_NOLUNS},
    648 	{{T_CDROM, T_REMOV,
    649 	 "DEC     ", "RRD42   (C) DEC ", ""},     PQUIRK_NOLUNS},
    650 	{{T_CDROM, T_REMOV,
    651 	 "DENON   ", "DRD-25X         ", "V"},    PQUIRK_NOLUNS},
    652 	{{T_CDROM, T_REMOV,
    653 	 "GENERIC ", "CRD-BP2         ", ""},     PQUIRK_NOLUNS},
    654 	{{T_CDROM, T_REMOV,
    655 	 "HP      ", "C4324/C4325     ", ""},     PQUIRK_NOLUNS},
    656 	{{T_CDROM, T_REMOV,
    657 	 "IMS     ", "CDD521/10       ", "2.06"}, PQUIRK_NOLUNS},
    658 	{{T_CDROM, T_REMOV,
    659 	 "MATSHITA", "CD-ROM CR-5XX   ", "1.0b"}, PQUIRK_NOLUNS},
    660 	{{T_CDROM, T_REMOV,
    661 	 "MEDAVIS ", "RENO CD-ROMX2A  ", ""},     PQUIRK_NOLUNS},
    662 	{{T_CDROM, T_REMOV,
    663 	 "MEDIAVIS", "CDR-H93MV       ", "1.3"},  PQUIRK_NOLUNS},
    664 	{{T_CDROM, T_REMOV,
    665 	 "NEC     ", "CD-ROM DRIVE:502", ""},     PQUIRK_NOLUNS},
    666 	{{T_CDROM, T_REMOV,
    667 	 "NEC     ", "CD-ROM DRIVE:55 ", ""},     PQUIRK_NOLUNS},
    668 	{{T_CDROM, T_REMOV,
    669 	 "NEC     ", "CD-ROM DRIVE:83 ", ""},     PQUIRK_NOLUNS},
    670 	{{T_CDROM, T_REMOV,
    671 	 "NEC     ", "CD-ROM DRIVE:84 ", ""},     PQUIRK_NOLUNS},
    672 	{{T_CDROM, T_REMOV,
    673 	 "NEC     ", "CD-ROM DRIVE:841", ""},     PQUIRK_NOLUNS},
    674         {{T_CDROM, T_REMOV,
    675 	 "OLYMPUS ", "CDS620E         ", "1.1d"},
    676 			       PQUIRK_NOLUNS|PQUIRK_NOSYNC|PQUIRK_NOCAPACITY},
    677 	{{T_CDROM, T_REMOV,
    678 	 "PIONEER ", "CD-ROM DR-124X  ", "1.01"}, PQUIRK_NOLUNS},
    679         {{T_CDROM, T_REMOV,
    680          "PLEXTOR ", "CD-ROM PX-4XCS  ", "1.01"},
    681                                PQUIRK_NOLUNS|PQUIRK_NOSYNC},
    682 	{{T_CDROM, T_REMOV,
    683 	 "SONY    ", "CD-ROM CDU-541  ", ""},     PQUIRK_NOLUNS},
    684 	{{T_CDROM, T_REMOV,
    685 	 "SONY    ", "CD-ROM CDU-55S  ", ""},     PQUIRK_NOLUNS},
    686 	{{T_CDROM, T_REMOV,
    687 	 "SONY    ", "CD-ROM CDU-561  ", ""},     PQUIRK_NOLUNS},
    688 	{{T_CDROM, T_REMOV,
    689 	 "SONY    ", "CD-ROM CDU-76S", ""},
    690 				PQUIRK_NOLUNS|PQUIRK_NOSYNC|PQUIRK_NOWIDE},
    691 	{{T_CDROM, T_REMOV,
    692 	 "SONY    ", "CD-ROM CDU-8003A", ""},     PQUIRK_NOLUNS},
    693 	{{T_CDROM, T_REMOV,
    694 	 "SONY    ", "CD-ROM CDU-8012 ", ""},     PQUIRK_NOLUNS},
    695 	{{T_CDROM, T_REMOV,
    696 	 "TEAC    ", "CD-ROM          ", "1.06"}, PQUIRK_NOLUNS},
    697 	{{T_CDROM, T_REMOV,
    698 	 "TEAC    ", "CD-ROM CD-56S   ", "1.0B"}, PQUIRK_NOLUNS},
    699 	{{T_CDROM, T_REMOV,
    700 	 "TEXEL   ", "CD-ROM          ", "1.06"}, PQUIRK_NOLUNS},
    701 	{{T_CDROM, T_REMOV,
    702 	 "TEXEL   ", "CD-ROM DM-XX24 K", "1.09"}, PQUIRK_NOLUNS},
    703 	{{T_CDROM, T_REMOV,
    704 	 "TEXEL   ", "CD-ROM DM-XX24 K", "1.10"}, PQUIRK_NOLUNS},
    705 	{{T_CDROM, T_REMOV,
    706 	 "TOSHIBA ", "XM-4101TASUNSLCD", ""}, PQUIRK_NOLUNS|PQUIRK_NOSYNC},
    707 	/* "IBM CDRM00201     !F" 0724 is an IBM OEM Toshiba XM-4101BME */
    708 	{{T_CDROM, T_REMOV,
    709 	 "IBM     ", "CDRM00201     !F", "0724"}, PQUIRK_NOLUNS|PQUIRK_NOSYNC},
    710 	{{T_CDROM, T_REMOV,
    711 	 "ShinaKen", "CD-ROM DM-3x1S",   "1.04"}, PQUIRK_NOLUNS},
    712 	{{T_CDROM, T_REMOV,
    713 	 "JVC     ", "R2626",            ""},     PQUIRK_NOLUNS},
    714 	{{T_CDROM, T_REMOV,
    715 	 "YAMAHA", "CRW8424S",           ""},     PQUIRK_NOLUNS},
    716 	{{T_CDROM, T_REMOV,
    717 	 "NEC     ", "CD-ROM DRIVE:222", ""},	  PQUIRK_NOLUNS|PQUIRK_NOSYNC},
    718 
    719 	{{T_DIRECT, T_FIXED,
    720 	 "MICROP  ", "1588-15MBSUN0669", ""},     PQUIRK_AUTOSAVE},
    721 	{{T_DIRECT, T_FIXED,
    722 	 "MICROP  ", "2217-15MQ1091501", ""},     PQUIRK_NOSYNCCACHE},
    723 	{{T_OPTICAL, T_REMOV,
    724 	 "EPSON   ", "OMD-5010        ", "3.08"}, PQUIRK_NOLUNS},
    725 	{{T_DIRECT, T_FIXED,
    726 	 "ADAPTEC ", "AEC-4412BD",       "1.2A"}, PQUIRK_NOMODESENSE},
    727 	{{T_DIRECT, T_FIXED,
    728 	 "ADAPTEC ", "ACB-4000",         ""},     PQUIRK_FORCELUNS|PQUIRK_AUTOSAVE|PQUIRK_NOMODESENSE},
    729 	{{T_DIRECT, T_FIXED,
    730 	 "DEC     ", "RZ55     (C) DEC", ""},     PQUIRK_AUTOSAVE},
    731 	{{T_DIRECT, T_FIXED,
    732 	 "EMULEX  ", "MD21/S2     ESDI", "A00"},
    733 				PQUIRK_FORCELUNS|PQUIRK_AUTOSAVE},
    734 	{{T_DIRECT, T_FIXED,
    735 	 "MICROP",  "1548-15MZ1077801",  "HZ2P"}, PQUIRK_NOTAG},
    736 	{{T_DIRECT, T_FIXED,
    737 	 "HP      ", "C372",             ""},     PQUIRK_NOTAG},
    738 	{{T_DIRECT, T_FIXED,
    739 	 "IBMRAID ", "0662S",		 ""},     PQUIRK_AUTOSAVE},
    740 	{{T_DIRECT, T_FIXED,
    741 	 "IBM     ", "0663H",		 ""},     PQUIRK_AUTOSAVE},
    742 	{{T_DIRECT, T_FIXED,
    743 	 "IBM",	     "0664",		 ""},     PQUIRK_AUTOSAVE},
    744 	{{T_DIRECT, T_FIXED,
    745 	/* improperly report DT-only sync mode */
    746 	 "IBM     ", "DXHS36D",		 ""},
    747 				PQUIRK_CAP_SYNC|PQUIRK_CAP_WIDE16},
    748 	{{T_DIRECT, T_FIXED,
    749 	 "IBM     ", "DXHS18Y",		 ""},
    750 				PQUIRK_CAP_SYNC|PQUIRK_CAP_WIDE16},
    751 	{{T_DIRECT, T_FIXED,
    752 	 "IBM     ", "H3171-S2",	 ""},
    753 				PQUIRK_NOLUNS|PQUIRK_AUTOSAVE},
    754 	{{T_DIRECT, T_FIXED,
    755 	 "IBM     ", "KZ-C",		 ""},	  PQUIRK_AUTOSAVE},
    756 	/* Broken IBM disk */
    757 	{{T_DIRECT, T_FIXED,
    758 	 ""	   , "DFRSS2F",		 ""},	  PQUIRK_AUTOSAVE},
    759 	{{T_DIRECT, T_FIXED,
    760 	 "Initio  ", "",		 ""},	  PQUIRK_NOBIGMODESENSE},
    761 	{{T_DIRECT, T_FIXED,
    762 	 "JMicron ", "Generic         ", ""},	  PQUIRK_NOFUA},
    763 	{{T_DIRECT, T_REMOV,
    764 	 "MPL     ", "MC-DISK-        ", ""},     PQUIRK_NOLUNS},
    765 	{{T_DIRECT, T_FIXED,
    766 	 "MAXTOR  ", "XT-3280         ", ""},     PQUIRK_NOLUNS},
    767 	{{T_DIRECT, T_FIXED,
    768 	 "MAXTOR  ", "XT-4380S        ", ""},     PQUIRK_NOLUNS},
    769 	{{T_DIRECT, T_FIXED,
    770 	 "MAXTOR  ", "MXT-1240S       ", ""},     PQUIRK_NOLUNS},
    771 	{{T_DIRECT, T_FIXED,
    772 	 "MAXTOR  ", "XT-4170S        ", ""},     PQUIRK_NOLUNS},
    773 	{{T_DIRECT, T_FIXED,
    774 	 "MAXTOR  ", "XT-8760S",         ""},     PQUIRK_NOLUNS},
    775 	{{T_DIRECT, T_FIXED,
    776 	 "MAXTOR  ", "LXT-213S        ", ""},     PQUIRK_NOLUNS},
    777 	{{T_DIRECT, T_FIXED,
    778 	 "MAXTOR  ", "LXT-213S SUN0207", ""},     PQUIRK_NOLUNS},
    779 	{{T_DIRECT, T_FIXED,
    780 	 "MAXTOR  ", "LXT-200S        ", ""},     PQUIRK_NOLUNS},
    781 	{{T_DIRECT, T_FIXED,
    782 	 "MEGADRV ", "EV1000",           ""},     PQUIRK_NOMODESENSE},
    783 	{{T_DIRECT, T_FIXED,
    784 	 "MICROP", "1991-27MZ",          ""},     PQUIRK_NOTAG},
    785 	{{T_DIRECT, T_FIXED,
    786 	 "MST     ", "SnapLink        ", ""},     PQUIRK_NOLUNS},
    787 	{{T_DIRECT, T_FIXED,
    788 	 "NEC     ", "D3847           ", "0307"}, PQUIRK_NOLUNS},
    789 	{{T_DIRECT, T_FIXED,
    790 	 "QUANTUM ", "ELS85S          ", ""},     PQUIRK_AUTOSAVE},
    791 	{{T_DIRECT, T_FIXED,
    792 	 "QUANTUM ", "LPS525S         ", ""},     PQUIRK_NOLUNS},
    793 	{{T_DIRECT, T_FIXED,
    794 	 "QUANTUM ", "P105S 910-10-94x", ""},     PQUIRK_NOLUNS},
    795 	{{T_DIRECT, T_FIXED,
    796 	 "QUANTUM ", "PD1225S         ", ""},     PQUIRK_NOLUNS},
    797 	{{T_DIRECT, T_FIXED,
    798 	 "QUANTUM ", "PD210S   SUN0207", ""},     PQUIRK_NOLUNS},
    799 	{{T_DIRECT, T_FIXED,
    800 	 "QUANTUM ", "ATLAS IV 9 WLS", "0A0A"},   PQUIRK_CAP_NODT},
    801 	{{T_DIRECT, T_FIXED,
    802 	 "RODIME  ", "RO3000S         ", ""},     PQUIRK_NOLUNS},
    803 	{{T_DIRECT, T_FIXED,
    804 	 "SEAGATE ", "ST125N          ", ""},     PQUIRK_NOLUNS},
    805 	{{T_DIRECT, T_FIXED,
    806 	 "SEAGATE ", "ST157N          ", ""},     PQUIRK_NOLUNS},
    807 	{{T_DIRECT, T_FIXED,
    808 	 "SEAGATE ", "ST296           ", ""},     PQUIRK_NOLUNS},
    809 	{{T_DIRECT, T_FIXED,
    810 	 "SEAGATE ", "ST296N          ", ""},     PQUIRK_NOLUNS},
    811 	{{T_DIRECT, T_FIXED,
    812 	 "SEAGATE ", "ST318404LC      ", ""},     PQUIRK_NOLUNS},
    813 	{{T_DIRECT, T_FIXED,
    814 	 "SEAGATE ", "ST336753LC      ", ""},     PQUIRK_NOLUNS},
    815 	{{T_DIRECT, T_FIXED,
    816 	 "SEAGATE ", "ST336753LW      ", ""},     PQUIRK_NOLUNS},
    817 	{{T_DIRECT, T_FIXED,
    818 	 "SEAGATE ", "ST336754LC      ", ""},     PQUIRK_NOLUNS},
    819 	{{T_DIRECT, T_FIXED,
    820 	 "SEAGATE ", "ST39236LC       ", ""},     PQUIRK_NOLUNS},
    821 	{{T_DIRECT, T_FIXED,
    822 	 "SEAGATE ", "ST15150N        ", ""},     PQUIRK_NOTAG},
    823 	{{T_DIRECT, T_FIXED,
    824 	 "SEAGATE ", "ST19171",          ""},     PQUIRK_NOMODESENSE},
    825 	{{T_DIRECT, T_FIXED,
    826 	 "SEAGATE ", "ST32430N",         ""},     PQUIRK_CAP_SYNC},
    827 	{{T_DIRECT, T_FIXED,
    828 	 "SEAGATE ", "ST34501FC       ", ""},     PQUIRK_NOMODESENSE},
    829 	{{T_DIRECT, T_FIXED,
    830 	 "SEAGATE ", "SX910800N",        ""},     PQUIRK_NOTAG},
    831 	{{T_DIRECT, T_FIXED,
    832 	 "TOSHIBA ", "MK538FB         ", "6027"}, PQUIRK_NOLUNS},
    833 	{{T_DIRECT, T_FIXED,
    834 	 "MICROP  ", "1924",          ""},     PQUIRK_CAP_SYNC},
    835 	{{T_DIRECT, T_FIXED,
    836 	 "FUJITSU ", "M2266",         ""},     PQUIRK_CAP_SYNC},
    837 	{{T_DIRECT, T_FIXED,
    838 	 "FUJITSU ", "M2624S-512      ", ""},     PQUIRK_CAP_SYNC},
    839 	{{T_DIRECT, T_FIXED,
    840 	 "SEAGATE ", "SX336704LC"   , ""}, PQUIRK_CAP_SYNC | PQUIRK_CAP_WIDE16},
    841 	{{T_DIRECT, T_FIXED,
    842 	 "SEAGATE ", "SX173404LC",       ""},     PQUIRK_CAP_SYNC | PQUIRK_CAP_WIDE16},
    843 
    844 	{{T_DIRECT, T_REMOV,
    845 	 "IOMEGA", "ZIP 100",		 "J.03"}, PQUIRK_NOLUNS|PQUIRK_NOSYNC},
    846 	{{T_DIRECT, T_REMOV,
    847 	 "INSITE", "I325VM",             ""},     PQUIRK_NOLUNS},
    848 
    849 	/* XXX: QIC-36 tape behind Emulex adapter.  Very broken. */
    850 	{{T_SEQUENTIAL, T_REMOV,
    851 	 "        ", "                ", "    "}, PQUIRK_NOLUNS},
    852 	{{T_SEQUENTIAL, T_REMOV,
    853 	 "EMULEX  ", "MT-02 QIC       ", ""},     PQUIRK_NOLUNS},
    854 	{{T_SEQUENTIAL, T_REMOV,
    855 	 "CALIPER ", "CP150           ", ""},     PQUIRK_NOLUNS},
    856 	{{T_SEQUENTIAL, T_REMOV,
    857 	 "EXABYTE ", "EXB-8200        ", ""},     PQUIRK_NOLUNS},
    858 	{{T_SEQUENTIAL, T_REMOV,
    859 	 "SONY    ", "GY-10C          ", ""},     PQUIRK_NOLUNS},
    860 	{{T_SEQUENTIAL, T_REMOV,
    861 	 "SONY    ", "SDT-2000        ", "2.09"}, PQUIRK_NOLUNS},
    862 	{{T_SEQUENTIAL, T_REMOV,
    863 	 "SONY    ", "SDT-5000        ", "3."},   PQUIRK_NOSYNC|PQUIRK_NOWIDE},
    864 	{{T_SEQUENTIAL, T_REMOV,
    865 	 "SONY    ", "SDT-5200        ", "3."},   PQUIRK_NOLUNS},
    866 	{{T_SEQUENTIAL, T_REMOV,
    867 	 "TANDBERG", " TDC 3600       ", ""},     PQUIRK_NOLUNS},
    868 	/* Following entry reported as a Tandberg 3600; ref. PR1933 */
    869 	{{T_SEQUENTIAL, T_REMOV,
    870 	 "ARCHIVE ", "VIPER 150  21247", ""},     PQUIRK_NOLUNS},
    871 	/* Following entry for a Cipher ST150S; ref. PR4171 */
    872 	{{T_SEQUENTIAL, T_REMOV,
    873 	 "ARCHIVE ", "VIPER 1500 21247", "2.2G"}, PQUIRK_NOLUNS},
    874 	{{T_SEQUENTIAL, T_REMOV,
    875 	 "ARCHIVE ", "Python 28454-XXX", ""},     PQUIRK_NOLUNS},
    876 	{{T_SEQUENTIAL, T_REMOV,
    877 	 "WANGTEK ", "5099ES SCSI",      ""},     PQUIRK_NOLUNS},
    878 	{{T_SEQUENTIAL, T_REMOV,
    879 	 "WANGTEK ", "5150ES SCSI",      ""},     PQUIRK_NOLUNS},
    880 	{{T_SEQUENTIAL, T_REMOV,
    881 	 "WANGTEK ", "SCSI-36",		 ""},     PQUIRK_NOLUNS},
    882 	{{T_SEQUENTIAL, T_REMOV,
    883 	 "WangDAT ", "Model 1300      ", "02.4"}, PQUIRK_NOSYNC|PQUIRK_NOWIDE},
    884 	{{T_SEQUENTIAL, T_REMOV,
    885 	 "WangDAT ", "Model 2600      ", "01.7"}, PQUIRK_NOSYNC|PQUIRK_NOWIDE},
    886 	{{T_SEQUENTIAL, T_REMOV,
    887 	 "WangDAT ", "Model 3200      ", "02.2"}, PQUIRK_NOSYNC|PQUIRK_NOWIDE},
    888 	{{T_SEQUENTIAL, T_REMOV,
    889 	 "TEAC    ", "MT-2ST/N50      ", ""},     PQUIRK_NOLUNS},
    890 
    891 	{{T_SCANNER, T_FIXED,
    892 	 "RICOH   ", "IS60            ", "1R08"}, PQUIRK_NOLUNS},
    893 	{{T_SCANNER, T_FIXED,
    894 	 "UMAX    ", "Astra 1200S     ", "V2.9"}, PQUIRK_NOLUNS},
    895 	{{T_SCANNER, T_FIXED,
    896 	 "UMAX    ", "Astra 1220S     ", ""},     PQUIRK_NOLUNS},
    897 	{{T_SCANNER, T_FIXED,
    898 	 "UMAX    ", "UMAX S-6E       ", "V2.0"}, PQUIRK_NOLUNS},
    899 	{{T_SCANNER, T_FIXED,
    900 	 "UMAX    ", "UMAX S-12       ", "V2.1"}, PQUIRK_NOLUNS},
    901 	{{T_SCANNER, T_FIXED,
    902 	 "ULTIMA  ", "A6000C          ", ""},     PQUIRK_NOLUNS},
    903 	{{T_PROCESSOR, T_FIXED,
    904 	 "ESG-SHV",  "SCA HSBP M15",     ""},     PQUIRK_NOLUNS},
    905 	{{T_PROCESSOR, T_FIXED,
    906 	 "SYMBIOS",  "",                 ""},     PQUIRK_NOLUNS},
    907 	{{T_PROCESSOR, T_FIXED,
    908 	 "LITRONIC", "PCMCIA          ", ""},     PQUIRK_NOLUNS},
    909 	{{T_CHANGER, T_REMOV,
    910 	 "SONY    ", "CDL1100         ", ""},     PQUIRK_NOLUNS},
    911 	{{T_ENCLOSURE, T_FIXED,
    912 	 "SUN     ", "SENA            ", ""},     PQUIRK_NOLUNS},
    913 };
    914 
    915 /*
    916  * given a target and lun, ask the device what
    917  * it is, and find the correct driver table
    918  * entry.
    919  */
    920 static int
    921 scsi_probe_device(struct scsibus_softc *sc, int target, int lun)
    922 {
    923 	struct scsipi_channel *chan = sc->sc_channel;
    924 	struct scsipi_periph *periph;
    925 	struct scsipi_inquiry_data inqbuf;
    926 	const struct scsi_quirk_inquiry_pattern *finger;
    927 	int checkdtype, priority, docontinue, quirks;
    928 	struct scsipibus_attach_args sa;
    929 	cfdata_t cf;
    930 	int locs[SCSIBUSCF_NLOCS];
    931 
    932 	/*
    933 	 * Assume no more LUNs to search after this one.
    934 	 * If we successfully get Inquiry data and after
    935 	 * merging quirks we find we can probe for more
    936 	 * LUNs, we will.
    937 	 */
    938 	docontinue = 0;
    939 
    940 	/* Skip this slot if it is already attached. */
    941 	if (scsipi_lookup_periph(chan, target, lun) != NULL)
    942 		return (docontinue);
    943 
    944 	periph = scsipi_alloc_periph(M_WAITOK);
    945 	periph->periph_channel = chan;
    946 	periph->periph_switch = &scsi_probe_dev;
    947 
    948 	periph->periph_target = target;
    949 	periph->periph_lun = lun;
    950 	periph->periph_quirks = chan->chan_defquirks;
    951 
    952 #ifdef SCSIPI_DEBUG
    953 	if (SCSIPI_DEBUG_TYPE == SCSIPI_BUSTYPE_SCSI &&
    954 	    SCSIPI_DEBUG_TARGET == target &&
    955 	    SCSIPI_DEBUG_LUN == lun)
    956 		periph->periph_dbflags |= SCSIPI_DEBUG_FLAGS;
    957 #endif
    958 
    959 	/*
    960 	 * Ask the device what it is
    961 	 */
    962 
    963 #ifdef SCSI_2_DEF
    964 	/* some devices need to be told to go to SCSI2 */
    965 	/* However some just explode if you tell them this.. leave it out */
    966 	scsi_change_def(periph, XS_CTL_DISCOVERY | XS_CTL_SILENT);
    967 #endif /* SCSI_2_DEF */
    968 
    969 	/* Now go ask the device all about itself. */
    970 	memset(&inqbuf, 0, sizeof(inqbuf));
    971 	{
    972 		u_int8_t *extension = &inqbuf.flags1;
    973 		int len = 0;
    974 		while (len < 3)
    975 			extension[len++] = '\0';
    976 		while (len < 3 + 28)
    977 			extension[len++] = ' ';
    978 		while (len < 3 + 28 + 20)
    979 			extension[len++] = '\0';
    980 		while (len < 3 + 28 + 20 + 1)
    981 			extension[len++] = '\0';
    982 		while (len < 3 + 28 + 20 + 1 + 1)
    983 			extension[len++] = '\0';
    984 		while (len < 3 + 28 + 20 + 1 + 1 + (8*2))
    985 			extension[len++] = ' ';
    986 	}
    987 	if (scsipi_inquire(periph, &inqbuf, XS_CTL_DISCOVERY | XS_CTL_SILENT))
    988 		goto bad;
    989 
    990 	periph->periph_type = inqbuf.device & SID_TYPE;
    991 	if (inqbuf.dev_qual2 & SID_REMOVABLE)
    992 		periph->periph_flags |= PERIPH_REMOVABLE;
    993 	periph->periph_version = inqbuf.version & SID_ANSII;
    994 
    995 	/*
    996 	 * Any device qualifier that has the top bit set (qualifier&4 != 0)
    997 	 * is vendor specific and won't match in this switch.
    998 	 * All we do here is throw out bad/negative responses.
    999 	 */
   1000 	checkdtype = 0;
   1001 	switch (inqbuf.device & SID_QUAL) {
   1002 	case SID_QUAL_LU_PRESENT:
   1003 		checkdtype = 1;
   1004 		break;
   1005 
   1006 	case SID_QUAL_LU_NOTPRESENT:
   1007 	case SID_QUAL_reserved:
   1008 	case SID_QUAL_LU_NOT_SUPP:
   1009 		goto bad;
   1010 
   1011 	default:
   1012 		break;
   1013 	}
   1014 
   1015 	/* Let the adapter driver handle the device separately if it wants. */
   1016 	if (chan->chan_adapter->adapt_accesschk != NULL &&
   1017 	    (*chan->chan_adapter->adapt_accesschk)(periph, &sa.sa_inqbuf))
   1018 		goto bad;
   1019 
   1020 	if (checkdtype) {
   1021 		switch (periph->periph_type) {
   1022 		case T_DIRECT:
   1023 		case T_SEQUENTIAL:
   1024 		case T_PRINTER:
   1025 		case T_PROCESSOR:
   1026 		case T_WORM:
   1027 		case T_CDROM:
   1028 		case T_SCANNER:
   1029 		case T_OPTICAL:
   1030 		case T_CHANGER:
   1031 		case T_COMM:
   1032 		case T_IT8_1:
   1033 		case T_IT8_2:
   1034 		case T_STORARRAY:
   1035 		case T_ENCLOSURE:
   1036 		case T_SIMPLE_DIRECT:
   1037 		case T_OPTIC_CARD_RW:
   1038 		case T_OBJECT_STORED:
   1039 		default:
   1040 			break;
   1041 		case T_NODEVICE:
   1042 			goto bad;
   1043 		}
   1044 	}
   1045 
   1046 	sa.sa_periph = periph;
   1047 	sa.sa_inqbuf.type = inqbuf.device;
   1048 	sa.sa_inqbuf.removable = inqbuf.dev_qual2 & SID_REMOVABLE ?
   1049 	    T_REMOV : T_FIXED;
   1050 	sa.sa_inqbuf.vendor = inqbuf.vendor;
   1051 	sa.sa_inqbuf.product = inqbuf.product;
   1052 	sa.sa_inqbuf.revision = inqbuf.revision;
   1053 	sa.scsipi_info.scsi_version = inqbuf.version;
   1054 	sa.sa_inqptr = &inqbuf;
   1055 
   1056 	finger = scsipi_inqmatch(
   1057 	    &sa.sa_inqbuf, scsi_quirk_patterns,
   1058 	    sizeof(scsi_quirk_patterns)/sizeof(scsi_quirk_patterns[0]),
   1059 	    sizeof(scsi_quirk_patterns[0]), &priority);
   1060 
   1061 	if (finger != NULL)
   1062 		quirks = finger->quirks;
   1063 	else
   1064 		quirks = 0;
   1065 
   1066 	/*
   1067 	 * Determine the operating mode capabilities of the device.
   1068 	 */
   1069 	if (periph->periph_version >= 2) {
   1070 		if ((inqbuf.flags3 & SID_CmdQue) != 0 &&
   1071 		    (quirks & PQUIRK_NOTAG) == 0)
   1072 			periph->periph_cap |= PERIPH_CAP_TQING;
   1073 		if ((inqbuf.flags3 & SID_Linked) != 0)
   1074 			periph->periph_cap |= PERIPH_CAP_LINKCMDS;
   1075 		if ((inqbuf.flags3 & SID_Sync) != 0 &&
   1076 		    (quirks & PQUIRK_NOSYNC) == 0)
   1077 			periph->periph_cap |= PERIPH_CAP_SYNC;
   1078 		if ((inqbuf.flags3 & SID_WBus16) != 0 &&
   1079 		    (quirks & PQUIRK_NOWIDE) == 0)
   1080 			periph->periph_cap |= PERIPH_CAP_WIDE16;
   1081 		if ((inqbuf.flags3 & SID_WBus32) != 0 &&
   1082 		    (quirks & PQUIRK_NOWIDE) == 0)
   1083 			periph->periph_cap |= PERIPH_CAP_WIDE32;
   1084 		if ((inqbuf.flags3 & SID_SftRe) != 0)
   1085 			periph->periph_cap |= PERIPH_CAP_SFTRESET;
   1086 		if ((inqbuf.flags3 & SID_RelAdr) != 0)
   1087 			periph->periph_cap |= PERIPH_CAP_RELADR;
   1088 		/* SPC-2 */
   1089 		if (periph->periph_version >= 3 &&
   1090 		    !(quirks & PQUIRK_CAP_NODT)){
   1091 			/*
   1092 			 * Report ST clocking though CAP_WIDExx/CAP_SYNC.
   1093 			 * If the device only supports DT, clear these
   1094 			 * flags (DT implies SYNC and WIDE)
   1095 			 */
   1096 			switch (inqbuf.flags4 & SID_Clocking) {
   1097 			case SID_CLOCKING_DT_ONLY:
   1098 				periph->periph_cap &=
   1099 				    ~(PERIPH_CAP_SYNC |
   1100 				      PERIPH_CAP_WIDE16 |
   1101 				      PERIPH_CAP_WIDE32);
   1102 				/* FALLTHROUGH */
   1103 			case SID_CLOCKING_SD_DT:
   1104 				periph->periph_cap |= PERIPH_CAP_DT;
   1105 				break;
   1106 			default: /* ST only or invalid */
   1107 				/* nothing to do */
   1108 				break;
   1109 			}
   1110 		}
   1111 		if (periph->periph_version >= 3) {
   1112 			if (inqbuf.flags4 & SID_IUS)
   1113 				periph->periph_cap |= PERIPH_CAP_IUS;
   1114 			if (inqbuf.flags4 & SID_QAS)
   1115 				periph->periph_cap |= PERIPH_CAP_QAS;
   1116 		}
   1117 	}
   1118 	if (quirks & PQUIRK_CAP_SYNC)
   1119 		periph->periph_cap |= PERIPH_CAP_SYNC;
   1120 	if (quirks & PQUIRK_CAP_WIDE16)
   1121 		periph->periph_cap |= PERIPH_CAP_WIDE16;
   1122 
   1123 	/*
   1124 	 * Now apply any quirks from the table.
   1125 	 */
   1126 	periph->periph_quirks |= quirks;
   1127 	if (periph->periph_version == 0 &&
   1128 	    (periph->periph_quirks & PQUIRK_FORCELUNS) == 0)
   1129 		periph->periph_quirks |= PQUIRK_NOLUNS;
   1130 
   1131 	if ((periph->periph_quirks & PQUIRK_NOLUNS) == 0)
   1132 		docontinue = 1;
   1133 
   1134 	locs[SCSIBUSCF_TARGET] = target;
   1135 	locs[SCSIBUSCF_LUN] = lun;
   1136 
   1137 	KERNEL_LOCK(1, NULL);
   1138 	if ((cf = config_search(sc->sc_dev, &sa,
   1139 				CFARGS(.submatch = config_stdsubmatch,
   1140 				       .locators = locs))) != NULL) {
   1141 		scsipi_insert_periph(chan, periph);
   1142 
   1143 		/*
   1144 		 * Determine supported opcodes and timeouts if available.
   1145 		 * Only do this on peripherals reporting SCSI version 3
   1146 		 * or greater - this command isn't in the SCSI-2 spec. and
   1147 		 * it causes either timeouts or peripherals disappearing
   1148 		 * when sent to some SCSI-1 or SCSI-2 peripherals.
   1149 		 */
   1150 		if (periph->periph_version >= 3)
   1151 			scsipi_get_opcodeinfo(periph);
   1152 
   1153 		/*
   1154 		 * XXX Can't assign periph_dev here, because we'll
   1155 		 * XXX need it before config_attach() returns.  Must
   1156 		 * XXX assign it in periph driver.
   1157 		 */
   1158 		config_attach(sc->sc_dev, cf, &sa, scsibusprint,
   1159 		    CFARGS(.locators = locs));
   1160 		KERNEL_UNLOCK_ONE(NULL);
   1161 	} else {
   1162 		scsibusprint(&sa, device_xname(sc->sc_dev));
   1163 		aprint_normal(" not configured\n");
   1164 		KERNEL_UNLOCK_ONE(NULL);
   1165 		goto bad;
   1166 	}
   1167 
   1168 	return (docontinue);
   1169 
   1170 bad:
   1171 	scsipi_free_periph(periph);
   1172 	return (docontinue);
   1173 }
   1174 
   1175 /****** Entry points for user control of the SCSI bus. ******/
   1176 
   1177 static int
   1178 scsibusopen(dev_t dev, int flag, int fmt,
   1179     struct lwp *l)
   1180 {
   1181 	struct scsibus_softc *sc;
   1182 	int error, unit = minor(dev);
   1183 
   1184 	sc = device_lookup_private(&scsibus_cd, unit);
   1185 	if (sc == NULL)
   1186 		return (ENXIO);
   1187 
   1188 	if (sc->sc_flags & SCSIBUSF_OPEN)
   1189 		return (EBUSY);
   1190 
   1191 	if ((error = scsipi_adapter_addref(sc->sc_channel->chan_adapter)) != 0)
   1192 		return (error);
   1193 
   1194 	sc->sc_flags |= SCSIBUSF_OPEN;
   1195 
   1196 	return (0);
   1197 }
   1198 
   1199 static int
   1200 scsibusclose(dev_t dev, int flag, int fmt,
   1201     struct lwp *l)
   1202 {
   1203 	struct scsibus_softc *sc;
   1204 
   1205 	sc = device_lookup_private(&scsibus_cd, minor(dev));
   1206 	scsipi_adapter_delref(sc->sc_channel->chan_adapter);
   1207 
   1208 	sc->sc_flags &= ~SCSIBUSF_OPEN;
   1209 
   1210 	return (0);
   1211 }
   1212 
   1213 static int
   1214 scsibusioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
   1215 {
   1216 	struct scsibus_softc *sc;
   1217 	struct scsipi_channel *chan;
   1218 	int error;
   1219 
   1220 	sc = device_lookup_private(&scsibus_cd, minor(dev));
   1221 	chan = sc->sc_channel;
   1222 
   1223 	/*
   1224 	 * Enforce write permission for ioctls that change the
   1225 	 * state of the bus.  Host adapter specific ioctls must
   1226 	 * be checked by the adapter driver.
   1227 	 */
   1228 	switch (cmd) {
   1229 	case SCBUSIOSCAN:
   1230 	case SCBUSIODETACH:
   1231 	case SCBUSIORESET:
   1232 		if ((flag & FWRITE) == 0)
   1233 			return (EBADF);
   1234 	}
   1235 
   1236 	switch (cmd) {
   1237 	case SCBUSIOSCAN:
   1238 	    {
   1239 		struct scbusioscan_args *a =
   1240 		    (struct scbusioscan_args *)addr;
   1241 
   1242 		error = scsi_probe_bus(sc, a->sa_target, a->sa_lun);
   1243 		break;
   1244 	    }
   1245 
   1246 	case SCBUSIODETACH:
   1247 	    {
   1248 		struct scbusiodetach_args *a =
   1249 		    (struct scbusiodetach_args *)addr;
   1250 
   1251 		error = scsipi_target_detach(chan, a->sa_target, a->sa_lun, 0);
   1252 		break;
   1253 	    }
   1254 
   1255 
   1256 	case SCBUSIORESET:
   1257 		/* FALLTHROUGH */
   1258 	default:
   1259 		error = scsipi_adapter_ioctl(chan, cmd, addr, flag, l->l_proc);
   1260 		break;
   1261 	}
   1262 
   1263 	return (error);
   1264 }
   1265