Home | History | Annotate | Line # | Download | only in scsipi
scsipi_base.c revision 1.124
      1 /*	$NetBSD: scsipi_base.c,v 1.124 2005/01/31 21:13:16 reinoud Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998, 1999, 2000, 2002, 2003, 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; by Jason R. Thorpe of the Numerical Aerospace
      9  * Simulation Facility, NASA Ames Research Center.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *        This product includes software developed by the NetBSD
     22  *        Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 #include <sys/cdefs.h>
     41 __KERNEL_RCSID(0, "$NetBSD: scsipi_base.c,v 1.124 2005/01/31 21:13:16 reinoud Exp $");
     42 
     43 #include "opt_scsi.h"
     44 
     45 #include <sys/param.h>
     46 #include <sys/systm.h>
     47 #include <sys/kernel.h>
     48 #include <sys/buf.h>
     49 #include <sys/uio.h>
     50 #include <sys/malloc.h>
     51 #include <sys/pool.h>
     52 #include <sys/errno.h>
     53 #include <sys/device.h>
     54 #include <sys/proc.h>
     55 #include <sys/kthread.h>
     56 #include <sys/hash.h>
     57 
     58 #include <uvm/uvm_extern.h>
     59 
     60 #include <dev/scsipi/scsipi_all.h>
     61 #include <dev/scsipi/scsipi_disk.h>
     62 #include <dev/scsipi/scsipiconf.h>
     63 #include <dev/scsipi/scsipi_base.h>
     64 
     65 #include <dev/scsipi/scsi_all.h>
     66 #include <dev/scsipi/scsi_message.h>
     67 
     68 static int	scsipi_complete(struct scsipi_xfer *);
     69 static void	scsipi_request_sense(struct scsipi_xfer *);
     70 static int	scsipi_enqueue(struct scsipi_xfer *);
     71 static void	scsipi_run_queue(struct scsipi_channel *chan);
     72 
     73 static void	scsipi_completion_thread(void *);
     74 
     75 static void	scsipi_get_tag(struct scsipi_xfer *);
     76 static void	scsipi_put_tag(struct scsipi_xfer *);
     77 
     78 static int	scsipi_get_resource(struct scsipi_channel *);
     79 static void	scsipi_put_resource(struct scsipi_channel *);
     80 
     81 static void	scsipi_async_event_max_openings(struct scsipi_channel *,
     82 		    struct scsipi_max_openings *);
     83 static void	scsipi_async_event_xfer_mode(struct scsipi_channel *,
     84 		    struct scsipi_xfer_mode *);
     85 static void	scsipi_async_event_channel_reset(struct scsipi_channel *);
     86 
     87 static struct pool scsipi_xfer_pool;
     88 
     89 /*
     90  * scsipi_init:
     91  *
     92  *	Called when a scsibus or atapibus is attached to the system
     93  *	to initialize shared data structures.
     94  */
     95 void
     96 scsipi_init(void)
     97 {
     98 	static int scsipi_init_done;
     99 
    100 	if (scsipi_init_done)
    101 		return;
    102 	scsipi_init_done = 1;
    103 
    104 	/* Initialize the scsipi_xfer pool. */
    105 	pool_init(&scsipi_xfer_pool, sizeof(struct scsipi_xfer), 0,
    106 	    0, 0, "scxspl", NULL);
    107 	if (pool_prime(&scsipi_xfer_pool,
    108 	    PAGE_SIZE / sizeof(struct scsipi_xfer)) == ENOMEM) {
    109 		printf("WARNING: not enough memory for scsipi_xfer_pool\n");
    110 	}
    111 }
    112 
    113 /*
    114  * scsipi_channel_init:
    115  *
    116  *	Initialize a scsipi_channel when it is attached.
    117  */
    118 int
    119 scsipi_channel_init(struct scsipi_channel *chan)
    120 {
    121 	int i;
    122 
    123 	/* Initialize shared data. */
    124 	scsipi_init();
    125 
    126 	/* Initialize the queues. */
    127 	TAILQ_INIT(&chan->chan_queue);
    128 	TAILQ_INIT(&chan->chan_complete);
    129 
    130 	for (i = 0; i < SCSIPI_CHAN_PERIPH_BUCKETS; i++)
    131 		LIST_INIT(&chan->chan_periphtab[i]);
    132 
    133 	/*
    134 	 * Create the asynchronous completion thread.
    135 	 */
    136 	kthread_create(scsipi_create_completion_thread, chan);
    137 	return (0);
    138 }
    139 
    140 /*
    141  * scsipi_channel_shutdown:
    142  *
    143  *	Shutdown a scsipi_channel.
    144  */
    145 void
    146 scsipi_channel_shutdown(struct scsipi_channel *chan)
    147 {
    148 
    149 	/*
    150 	 * Shut down the completion thread.
    151 	 */
    152 	chan->chan_tflags |= SCSIPI_CHANT_SHUTDOWN;
    153 	wakeup(&chan->chan_complete);
    154 
    155 	/*
    156 	 * Now wait for the thread to exit.
    157 	 */
    158 	while (chan->chan_thread != NULL)
    159 		(void) tsleep(&chan->chan_thread, PRIBIO, "scshut", 0);
    160 }
    161 
    162 static uint32_t
    163 scsipi_chan_periph_hash(uint64_t t, uint64_t l)
    164 {
    165 	uint32_t hash;
    166 
    167 	hash = hash32_buf(&t, sizeof(t), HASH32_BUF_INIT);
    168 	hash = hash32_buf(&l, sizeof(l), hash);
    169 
    170 	return (hash & SCSIPI_CHAN_PERIPH_HASHMASK);
    171 }
    172 
    173 /*
    174  * scsipi_insert_periph:
    175  *
    176  *	Insert a periph into the channel.
    177  */
    178 void
    179 scsipi_insert_periph(struct scsipi_channel *chan, struct scsipi_periph *periph)
    180 {
    181 	uint32_t hash;
    182 	int s;
    183 
    184 	hash = scsipi_chan_periph_hash(periph->periph_target,
    185 	    periph->periph_lun);
    186 
    187 	s = splbio();
    188 	LIST_INSERT_HEAD(&chan->chan_periphtab[hash], periph, periph_hash);
    189 	splx(s);
    190 }
    191 
    192 /*
    193  * scsipi_remove_periph:
    194  *
    195  *	Remove a periph from the channel.
    196  */
    197 void
    198 scsipi_remove_periph(struct scsipi_channel *chan, struct scsipi_periph *periph)
    199 {
    200 	int s;
    201 
    202 	s = splbio();
    203 	LIST_REMOVE(periph, periph_hash);
    204 	splx(s);
    205 }
    206 
    207 /*
    208  * scsipi_lookup_periph:
    209  *
    210  *	Lookup a periph on the specified channel.
    211  */
    212 struct scsipi_periph *
    213 scsipi_lookup_periph(struct scsipi_channel *chan, int target, int lun)
    214 {
    215 	struct scsipi_periph *periph;
    216 	uint32_t hash;
    217 	int s;
    218 
    219 	if (target >= chan->chan_ntargets ||
    220 	    lun >= chan->chan_nluns)
    221 		return (NULL);
    222 
    223 	hash = scsipi_chan_periph_hash(target, lun);
    224 
    225 	s = splbio();
    226 	LIST_FOREACH(periph, &chan->chan_periphtab[hash], periph_hash) {
    227 		if (periph->periph_target == target &&
    228 		    periph->periph_lun == lun)
    229 			break;
    230 	}
    231 	splx(s);
    232 
    233 	return (periph);
    234 }
    235 
    236 /*
    237  * scsipi_get_resource:
    238  *
    239  *	Allocate a single xfer `resource' from the channel.
    240  *
    241  *	NOTE: Must be called at splbio().
    242  */
    243 static int
    244 scsipi_get_resource(struct scsipi_channel *chan)
    245 {
    246 	struct scsipi_adapter *adapt = chan->chan_adapter;
    247 
    248 	if (chan->chan_flags & SCSIPI_CHAN_OPENINGS) {
    249 		if (chan->chan_openings > 0) {
    250 			chan->chan_openings--;
    251 			return (1);
    252 		}
    253 		return (0);
    254 	}
    255 
    256 	if (adapt->adapt_openings > 0) {
    257 		adapt->adapt_openings--;
    258 		return (1);
    259 	}
    260 	return (0);
    261 }
    262 
    263 /*
    264  * scsipi_grow_resources:
    265  *
    266  *	Attempt to grow resources for a channel.  If this succeeds,
    267  *	we allocate one for our caller.
    268  *
    269  *	NOTE: Must be called at splbio().
    270  */
    271 static __inline int
    272 scsipi_grow_resources(struct scsipi_channel *chan)
    273 {
    274 
    275 	if (chan->chan_flags & SCSIPI_CHAN_CANGROW) {
    276 		if ((chan->chan_flags & SCSIPI_CHAN_TACTIVE) == 0) {
    277 			scsipi_adapter_request(chan,
    278 			    ADAPTER_REQ_GROW_RESOURCES, NULL);
    279 			return (scsipi_get_resource(chan));
    280 		}
    281 		/*
    282 		 * ask the channel thread to do it. It'll have to thaw the
    283 		 * queue
    284 		 */
    285 		scsipi_channel_freeze(chan, 1);
    286 		chan->chan_tflags |= SCSIPI_CHANT_GROWRES;
    287 		wakeup(&chan->chan_complete);
    288 		return (0);
    289 	}
    290 
    291 	return (0);
    292 }
    293 
    294 /*
    295  * scsipi_put_resource:
    296  *
    297  *	Free a single xfer `resource' to the channel.
    298  *
    299  *	NOTE: Must be called at splbio().
    300  */
    301 static void
    302 scsipi_put_resource(struct scsipi_channel *chan)
    303 {
    304 	struct scsipi_adapter *adapt = chan->chan_adapter;
    305 
    306 	if (chan->chan_flags & SCSIPI_CHAN_OPENINGS)
    307 		chan->chan_openings++;
    308 	else
    309 		adapt->adapt_openings++;
    310 }
    311 
    312 /*
    313  * scsipi_get_tag:
    314  *
    315  *	Get a tag ID for the specified xfer.
    316  *
    317  *	NOTE: Must be called at splbio().
    318  */
    319 static void
    320 scsipi_get_tag(struct scsipi_xfer *xs)
    321 {
    322 	struct scsipi_periph *periph = xs->xs_periph;
    323 	int bit, tag;
    324 	u_int word;
    325 
    326 	bit = 0;	/* XXX gcc */
    327 	for (word = 0; word < PERIPH_NTAGWORDS; word++) {
    328 		bit = ffs(periph->periph_freetags[word]);
    329 		if (bit != 0)
    330 			break;
    331 	}
    332 #ifdef DIAGNOSTIC
    333 	if (word == PERIPH_NTAGWORDS) {
    334 		scsipi_printaddr(periph);
    335 		printf("no free tags\n");
    336 		panic("scsipi_get_tag");
    337 	}
    338 #endif
    339 
    340 	bit -= 1;
    341 	periph->periph_freetags[word] &= ~(1 << bit);
    342 	tag = (word << 5) | bit;
    343 
    344 	/* XXX Should eventually disallow this completely. */
    345 	if (tag >= periph->periph_openings) {
    346 		scsipi_printaddr(periph);
    347 		printf("WARNING: tag %d greater than available openings %d\n",
    348 		    tag, periph->periph_openings);
    349 	}
    350 
    351 	xs->xs_tag_id = tag;
    352 }
    353 
    354 /*
    355  * scsipi_put_tag:
    356  *
    357  *	Put the tag ID for the specified xfer back into the pool.
    358  *
    359  *	NOTE: Must be called at splbio().
    360  */
    361 static void
    362 scsipi_put_tag(struct scsipi_xfer *xs)
    363 {
    364 	struct scsipi_periph *periph = xs->xs_periph;
    365 	int word, bit;
    366 
    367 	word = xs->xs_tag_id >> 5;
    368 	bit = xs->xs_tag_id & 0x1f;
    369 
    370 	periph->periph_freetags[word] |= (1 << bit);
    371 }
    372 
    373 /*
    374  * scsipi_get_xs:
    375  *
    376  *	Allocate an xfer descriptor and associate it with the
    377  *	specified peripherial.  If the peripherial has no more
    378  *	available command openings, we either block waiting for
    379  *	one to become available, or fail.
    380  */
    381 struct scsipi_xfer *
    382 scsipi_get_xs(struct scsipi_periph *periph, int flags)
    383 {
    384 	struct scsipi_xfer *xs;
    385 	int s;
    386 
    387 	SC_DEBUG(periph, SCSIPI_DB3, ("scsipi_get_xs\n"));
    388 
    389 	KASSERT(!cold);
    390 
    391 #ifdef DIAGNOSTIC
    392 	/*
    393 	 * URGENT commands can never be ASYNC.
    394 	 */
    395 	if ((flags & (XS_CTL_URGENT|XS_CTL_ASYNC)) ==
    396 	    (XS_CTL_URGENT|XS_CTL_ASYNC)) {
    397 		scsipi_printaddr(periph);
    398 		printf("URGENT and ASYNC\n");
    399 		panic("scsipi_get_xs");
    400 	}
    401 #endif
    402 
    403 	s = splbio();
    404 	/*
    405 	 * Wait for a command opening to become available.  Rules:
    406 	 *
    407 	 *	- All xfers must wait for an available opening.
    408 	 *	  Exception: URGENT xfers can proceed when
    409 	 *	  active == openings, because we use the opening
    410 	 *	  of the command we're recovering for.
    411 	 *	- if the periph has sense pending, only URGENT & REQSENSE
    412 	 *	  xfers may proceed.
    413 	 *
    414 	 *	- If the periph is recovering, only URGENT xfers may
    415 	 *	  proceed.
    416 	 *
    417 	 *	- If the periph is currently executing a recovery
    418 	 *	  command, URGENT commands must block, because only
    419 	 *	  one recovery command can execute at a time.
    420 	 */
    421 	for (;;) {
    422 		if (flags & XS_CTL_URGENT) {
    423 			if (periph->periph_active > periph->periph_openings)
    424 				goto wait_for_opening;
    425 			if (periph->periph_flags & PERIPH_SENSE) {
    426 				if ((flags & XS_CTL_REQSENSE) == 0)
    427 					goto wait_for_opening;
    428 			} else {
    429 				if ((periph->periph_flags &
    430 				    PERIPH_RECOVERY_ACTIVE) != 0)
    431 					goto wait_for_opening;
    432 				periph->periph_flags |= PERIPH_RECOVERY_ACTIVE;
    433 			}
    434 			break;
    435 		}
    436 		if (periph->periph_active >= periph->periph_openings ||
    437 		    (periph->periph_flags & PERIPH_RECOVERING) != 0)
    438 			goto wait_for_opening;
    439 		periph->periph_active++;
    440 		break;
    441 
    442  wait_for_opening:
    443 		if (flags & XS_CTL_NOSLEEP) {
    444 			splx(s);
    445 			return (NULL);
    446 		}
    447 		SC_DEBUG(periph, SCSIPI_DB3, ("sleeping\n"));
    448 		periph->periph_flags |= PERIPH_WAITING;
    449 		(void) tsleep(periph, PRIBIO, "getxs", 0);
    450 	}
    451 	SC_DEBUG(periph, SCSIPI_DB3, ("calling pool_get\n"));
    452 	xs = pool_get(&scsipi_xfer_pool,
    453 	    ((flags & XS_CTL_NOSLEEP) != 0 ? PR_NOWAIT : PR_WAITOK));
    454 	if (xs == NULL) {
    455 		if (flags & XS_CTL_URGENT) {
    456 			if ((flags & XS_CTL_REQSENSE) == 0)
    457 				periph->periph_flags &= ~PERIPH_RECOVERY_ACTIVE;
    458 		} else
    459 			periph->periph_active--;
    460 		scsipi_printaddr(periph);
    461 		printf("unable to allocate %sscsipi_xfer\n",
    462 		    (flags & XS_CTL_URGENT) ? "URGENT " : "");
    463 	}
    464 	splx(s);
    465 
    466 	SC_DEBUG(periph, SCSIPI_DB3, ("returning\n"));
    467 
    468 	if (xs != NULL) {
    469 		memset(xs, 0, sizeof(*xs));
    470 		callout_init(&xs->xs_callout);
    471 		xs->xs_periph = periph;
    472 		xs->xs_control = flags;
    473 		xs->xs_status = 0;
    474 		s = splbio();
    475 		TAILQ_INSERT_TAIL(&periph->periph_xferq, xs, device_q);
    476 		splx(s);
    477 	}
    478 	return (xs);
    479 }
    480 
    481 /*
    482  * scsipi_put_xs:
    483  *
    484  *	Release an xfer descriptor, decreasing the outstanding command
    485  *	count for the peripherial.  If there is a thread waiting for
    486  *	an opening, wake it up.  If not, kick any queued I/O the
    487  *	peripherial may have.
    488  *
    489  *	NOTE: Must be called at splbio().
    490  */
    491 void
    492 scsipi_put_xs(struct scsipi_xfer *xs)
    493 {
    494 	struct scsipi_periph *periph = xs->xs_periph;
    495 	int flags = xs->xs_control;
    496 
    497 	SC_DEBUG(periph, SCSIPI_DB3, ("scsipi_free_xs\n"));
    498 
    499 	TAILQ_REMOVE(&periph->periph_xferq, xs, device_q);
    500 	pool_put(&scsipi_xfer_pool, xs);
    501 
    502 #ifdef DIAGNOSTIC
    503 	if ((periph->periph_flags & PERIPH_RECOVERY_ACTIVE) != 0 &&
    504 	    periph->periph_active == 0) {
    505 		scsipi_printaddr(periph);
    506 		printf("recovery without a command to recovery for\n");
    507 		panic("scsipi_put_xs");
    508 	}
    509 #endif
    510 
    511 	if (flags & XS_CTL_URGENT) {
    512 		if ((flags & XS_CTL_REQSENSE) == 0)
    513 			periph->periph_flags &= ~PERIPH_RECOVERY_ACTIVE;
    514 	} else
    515 		periph->periph_active--;
    516 	if (periph->periph_active == 0 &&
    517 	    (periph->periph_flags & PERIPH_WAITDRAIN) != 0) {
    518 		periph->periph_flags &= ~PERIPH_WAITDRAIN;
    519 		wakeup(&periph->periph_active);
    520 	}
    521 
    522 	if (periph->periph_flags & PERIPH_WAITING) {
    523 		periph->periph_flags &= ~PERIPH_WAITING;
    524 		wakeup(periph);
    525 	} else {
    526 		if (periph->periph_switch->psw_start != NULL &&
    527 		    (periph->periph_dev->dv_flags & DVF_ACTIVE)) {
    528 			SC_DEBUG(periph, SCSIPI_DB2,
    529 			    ("calling private start()\n"));
    530 			(*periph->periph_switch->psw_start)(periph);
    531 		}
    532 	}
    533 }
    534 
    535 /*
    536  * scsipi_channel_freeze:
    537  *
    538  *	Freeze a channel's xfer queue.
    539  */
    540 void
    541 scsipi_channel_freeze(struct scsipi_channel *chan, int count)
    542 {
    543 	int s;
    544 
    545 	s = splbio();
    546 	chan->chan_qfreeze += count;
    547 	splx(s);
    548 }
    549 
    550 /*
    551  * scsipi_channel_thaw:
    552  *
    553  *	Thaw a channel's xfer queue.
    554  */
    555 void
    556 scsipi_channel_thaw(struct scsipi_channel *chan, int count)
    557 {
    558 	int s;
    559 
    560 	s = splbio();
    561 	chan->chan_qfreeze -= count;
    562 	/*
    563 	 * Don't let the freeze count go negative.
    564 	 *
    565 	 * Presumably the adapter driver could keep track of this,
    566 	 * but it might just be easier to do this here so as to allow
    567 	 * multiple callers, including those outside the adapter driver.
    568 	 */
    569 	if (chan->chan_qfreeze < 0) {
    570 		chan->chan_qfreeze = 0;
    571 	}
    572 	splx(s);
    573 	/*
    574 	 * Kick the channel's queue here.  Note, we may be running in
    575 	 * interrupt context (softclock or HBA's interrupt), so the adapter
    576 	 * driver had better not sleep.
    577 	 */
    578 	if (chan->chan_qfreeze == 0)
    579 		scsipi_run_queue(chan);
    580 }
    581 
    582 /*
    583  * scsipi_channel_timed_thaw:
    584  *
    585  *	Thaw a channel after some time has expired. This will also
    586  * 	run the channel's queue if the freeze count has reached 0.
    587  */
    588 void
    589 scsipi_channel_timed_thaw(void *arg)
    590 {
    591 	struct scsipi_channel *chan = arg;
    592 
    593 	scsipi_channel_thaw(chan, 1);
    594 }
    595 
    596 /*
    597  * scsipi_periph_freeze:
    598  *
    599  *	Freeze a device's xfer queue.
    600  */
    601 void
    602 scsipi_periph_freeze(struct scsipi_periph *periph, int count)
    603 {
    604 	int s;
    605 
    606 	s = splbio();
    607 	periph->periph_qfreeze += count;
    608 	splx(s);
    609 }
    610 
    611 /*
    612  * scsipi_periph_thaw:
    613  *
    614  *	Thaw a device's xfer queue.
    615  */
    616 void
    617 scsipi_periph_thaw(struct scsipi_periph *periph, int count)
    618 {
    619 	int s;
    620 
    621 	s = splbio();
    622 	periph->periph_qfreeze -= count;
    623 #ifdef DIAGNOSTIC
    624 	if (periph->periph_qfreeze < 0) {
    625 		static const char pc[] = "periph freeze count < 0";
    626 		scsipi_printaddr(periph);
    627 		printf("%s\n", pc);
    628 		panic(pc);
    629 	}
    630 #endif
    631 	if (periph->periph_qfreeze == 0 &&
    632 	    (periph->periph_flags & PERIPH_WAITING) != 0)
    633 		wakeup(periph);
    634 	splx(s);
    635 }
    636 
    637 /*
    638  * scsipi_periph_timed_thaw:
    639  *
    640  *	Thaw a device after some time has expired.
    641  */
    642 void
    643 scsipi_periph_timed_thaw(void *arg)
    644 {
    645 	int s;
    646 	struct scsipi_periph *periph = arg;
    647 
    648 	callout_stop(&periph->periph_callout);
    649 
    650 	s = splbio();
    651 	scsipi_periph_thaw(periph, 1);
    652 	if ((periph->periph_channel->chan_flags & SCSIPI_CHAN_TACTIVE) == 0) {
    653 		/*
    654 		 * Kick the channel's queue here.  Note, we're running in
    655 		 * interrupt context (softclock), so the adapter driver
    656 		 * had better not sleep.
    657 		 */
    658 		scsipi_run_queue(periph->periph_channel);
    659 	} else {
    660 		/*
    661 		 * Tell the completion thread to kick the channel's queue here.
    662 		 */
    663 		periph->periph_channel->chan_tflags |= SCSIPI_CHANT_KICK;
    664 		wakeup(&periph->periph_channel->chan_complete);
    665 	}
    666 	splx(s);
    667 }
    668 
    669 /*
    670  * scsipi_wait_drain:
    671  *
    672  *	Wait for a periph's pending xfers to drain.
    673  */
    674 void
    675 scsipi_wait_drain(struct scsipi_periph *periph)
    676 {
    677 	int s;
    678 
    679 	s = splbio();
    680 	while (periph->periph_active != 0) {
    681 		periph->periph_flags |= PERIPH_WAITDRAIN;
    682 		(void) tsleep(&periph->periph_active, PRIBIO, "sxdrn", 0);
    683 	}
    684 	splx(s);
    685 }
    686 
    687 /*
    688  * scsipi_kill_pending:
    689  *
    690  *	Kill off all pending xfers for a periph.
    691  *
    692  *	NOTE: Must be called at splbio().
    693  */
    694 void
    695 scsipi_kill_pending(struct scsipi_periph *periph)
    696 {
    697 
    698 	(*periph->periph_channel->chan_bustype->bustype_kill_pending)(periph);
    699 	scsipi_wait_drain(periph);
    700 }
    701 
    702 /*
    703  * scsipi_print_cdb:
    704  * prints a command descriptor block (for debug purpose, error messages,
    705  * SCSIPI_VERBOSE, ...)
    706  */
    707 void
    708 scsipi_print_cdb(struct scsipi_generic *cmd)
    709 {
    710 	int i, j;
    711 
    712  	printf("0x%02x", cmd->opcode);
    713 
    714  	switch (CDB_GROUPID(cmd->opcode)) {
    715  	case CDB_GROUPID_0:
    716  		j = CDB_GROUP0;
    717  		break;
    718  	case CDB_GROUPID_1:
    719  		j = CDB_GROUP1;
    720  		break;
    721  	case CDB_GROUPID_2:
    722  		j = CDB_GROUP2;
    723  		break;
    724  	case CDB_GROUPID_3:
    725  		j = CDB_GROUP3;
    726  		break;
    727  	case CDB_GROUPID_4:
    728  		j = CDB_GROUP4;
    729  		break;
    730  	case CDB_GROUPID_5:
    731  		j = CDB_GROUP5;
    732  		break;
    733  	case CDB_GROUPID_6:
    734  		j = CDB_GROUP6;
    735  		break;
    736  	case CDB_GROUPID_7:
    737  		j = CDB_GROUP7;
    738  		break;
    739  	default:
    740  		j = 0;
    741  	}
    742  	if (j == 0)
    743  		j = sizeof (cmd->bytes);
    744  	for (i = 0; i < j-1; i++) /* already done the opcode */
    745  		printf(" %02x", cmd->bytes[i]);
    746 }
    747 
    748 /*
    749  * scsipi_interpret_sense:
    750  *
    751  *	Look at the returned sense and act on the error, determining
    752  *	the unix error number to pass back.  (0 = report no error)
    753  *
    754  *	NOTE: If we return ERESTART, we are expected to haved
    755  *	thawed the device!
    756  *
    757  *	THIS IS THE DEFAULT ERROR HANDLER FOR SCSI DEVICES.
    758  */
    759 int
    760 scsipi_interpret_sense(struct scsipi_xfer *xs)
    761 {
    762 	struct scsipi_sense_data *sense;
    763 	struct scsipi_periph *periph = xs->xs_periph;
    764 	u_int8_t key;
    765 	int error;
    766 #ifndef	SCSIVERBOSE
    767 	u_int32_t info;
    768 	static char *error_mes[] = {
    769 		"soft error (corrected)",
    770 		"not ready", "medium error",
    771 		"non-media hardware failure", "illegal request",
    772 		"unit attention", "readonly device",
    773 		"no data found", "vendor unique",
    774 		"copy aborted", "command aborted",
    775 		"search returned equal", "volume overflow",
    776 		"verify miscompare", "unknown error key"
    777 	};
    778 #endif
    779 
    780 	sense = &xs->sense.scsi_sense;
    781 #ifdef SCSIPI_DEBUG
    782 	if (periph->periph_flags & SCSIPI_DB1) {
    783 		int count;
    784 		scsipi_printaddr(periph);
    785 		printf(" sense debug information:\n");
    786 		printf("\tcode 0x%x valid 0x%x\n",
    787 			sense->error_code & SSD_ERRCODE,
    788 			sense->error_code & SSD_ERRCODE_VALID ? 1 : 0);
    789 		printf("\tseg 0x%x key 0x%x ili 0x%x eom 0x%x fmark 0x%x\n",
    790 			sense->segment,
    791 			sense->flags & SSD_KEY,
    792 			sense->flags & SSD_ILI ? 1 : 0,
    793 			sense->flags & SSD_EOM ? 1 : 0,
    794 			sense->flags & SSD_FILEMARK ? 1 : 0);
    795 		printf("\ninfo: 0x%x 0x%x 0x%x 0x%x followed by %d "
    796 			"extra bytes\n",
    797 			sense->info[0],
    798 			sense->info[1],
    799 			sense->info[2],
    800 			sense->info[3],
    801 			sense->extra_len);
    802 		printf("\textra: ");
    803 		for (count = 0; count < ADD_BYTES_LIM(sense); count++)
    804 			printf("0x%x ", sense->cmd_spec_info[count]);
    805 		printf("\n");
    806 	}
    807 #endif
    808 
    809 	/*
    810 	 * If the periph has it's own error handler, call it first.
    811 	 * If it returns a legit error value, return that, otherwise
    812 	 * it wants us to continue with normal error processing.
    813 	 */
    814 	if (periph->periph_switch->psw_error != NULL) {
    815 		SC_DEBUG(periph, SCSIPI_DB2,
    816 		    ("calling private err_handler()\n"));
    817 		error = (*periph->periph_switch->psw_error)(xs);
    818 		if (error != EJUSTRETURN)
    819 			return (error);
    820 	}
    821 	/* otherwise use the default */
    822 	switch (sense->error_code & SSD_ERRCODE) {
    823 
    824 		/*
    825 		 * Old SCSI-1 and SASI devices respond with
    826 		 * codes other than 70.
    827 		 */
    828 	case 0x00:		/* no error (command completed OK) */
    829 		return (0);
    830 	case 0x04:		/* drive not ready after it was selected */
    831 		if ((periph->periph_flags & PERIPH_REMOVABLE) != 0)
    832 			periph->periph_flags &= ~PERIPH_MEDIA_LOADED;
    833 		if ((xs->xs_control & XS_CTL_IGNORE_NOT_READY) != 0)
    834 			return (0);
    835 		/* XXX - display some sort of error here? */
    836 		return (EIO);
    837 	case 0x20:		/* invalid command */
    838 		if ((xs->xs_control &
    839 		     XS_CTL_IGNORE_ILLEGAL_REQUEST) != 0)
    840 			return (0);
    841 		return (EINVAL);
    842 	case 0x25:		/* invalid LUN (Adaptec ACB-4000) */
    843 		return (EACCES);
    844 
    845 		/*
    846 		 * If it's code 70, use the extended stuff and
    847 		 * interpret the key
    848 		 */
    849 	case 0x71:		/* delayed error */
    850 		scsipi_printaddr(periph);
    851 		key = sense->flags & SSD_KEY;
    852 		printf(" DEFERRED ERROR, key = 0x%x\n", key);
    853 		/* FALLTHROUGH */
    854 	case 0x70:
    855 #ifndef	SCSIVERBOSE
    856 		if ((sense->error_code & SSD_ERRCODE_VALID) != 0)
    857 			info = _4btol(sense->info);
    858 		else
    859 			info = 0;
    860 #endif
    861 		key = sense->flags & SSD_KEY;
    862 
    863 		switch (key) {
    864 		case SKEY_NO_SENSE:
    865 		case SKEY_RECOVERED_ERROR:
    866 			if (xs->resid == xs->datalen && xs->datalen) {
    867 				/*
    868 				 * Why is this here?
    869 				 */
    870 				xs->resid = 0;	/* not short read */
    871 			}
    872 		case SKEY_EQUAL:
    873 			error = 0;
    874 			break;
    875 		case SKEY_NOT_READY:
    876 			if ((periph->periph_flags & PERIPH_REMOVABLE) != 0)
    877 				periph->periph_flags &= ~PERIPH_MEDIA_LOADED;
    878 			if ((xs->xs_control & XS_CTL_IGNORE_NOT_READY) != 0)
    879 				return (0);
    880 			if (sense->add_sense_code == 0x3A) {
    881 				error = ENODEV; /* Medium not present */
    882 				if (xs->xs_control & XS_CTL_SILENT_NODEV)
    883 					return (error);
    884 			} else
    885 				error = EIO;
    886 			if ((xs->xs_control & XS_CTL_SILENT) != 0)
    887 				return (error);
    888 			break;
    889 		case SKEY_ILLEGAL_REQUEST:
    890 			if ((xs->xs_control &
    891 			     XS_CTL_IGNORE_ILLEGAL_REQUEST) != 0)
    892 				return (0);
    893 			/*
    894 			 * Handle the case where a device reports
    895 			 * Logical Unit Not Supported during discovery.
    896 			 */
    897 			if ((xs->xs_control & XS_CTL_DISCOVERY) != 0 &&
    898 			    sense->add_sense_code == 0x25 &&
    899 			    sense->add_sense_code_qual == 0x00)
    900 				return (EINVAL);
    901 			if ((xs->xs_control & XS_CTL_SILENT) != 0)
    902 				return (EIO);
    903 			error = EINVAL;
    904 			break;
    905 		case SKEY_UNIT_ATTENTION:
    906 			if (sense->add_sense_code == 0x29 &&
    907 			    sense->add_sense_code_qual == 0x00) {
    908 				/* device or bus reset */
    909 				return (ERESTART);
    910 			}
    911 			if ((periph->periph_flags & PERIPH_REMOVABLE) != 0)
    912 				periph->periph_flags &= ~PERIPH_MEDIA_LOADED;
    913 			if ((xs->xs_control &
    914 			     XS_CTL_IGNORE_MEDIA_CHANGE) != 0 ||
    915 				/* XXX Should reupload any transient state. */
    916 				(periph->periph_flags &
    917 				 PERIPH_REMOVABLE) == 0) {
    918 				return (ERESTART);
    919 			}
    920 			if ((xs->xs_control & XS_CTL_SILENT) != 0)
    921 				return (EIO);
    922 			error = EIO;
    923 			break;
    924 		case SKEY_WRITE_PROTECT:
    925 			error = EROFS;
    926 			break;
    927 		case SKEY_BLANK_CHECK:
    928 			error = 0;
    929 			break;
    930 		case SKEY_ABORTED_COMMAND:
    931 			if (xs->xs_retries != 0) {
    932 				xs->xs_retries--;
    933 				error = ERESTART;
    934 			} else
    935 				error = EIO;
    936 			break;
    937 		case SKEY_VOLUME_OVERFLOW:
    938 			error = ENOSPC;
    939 			break;
    940 		default:
    941 			error = EIO;
    942 			break;
    943 		}
    944 
    945 #ifdef SCSIVERBOSE
    946 		if (key && (xs->xs_control & XS_CTL_SILENT) == 0)
    947 			scsipi_print_sense(xs, 0);
    948 #else
    949 		if (key) {
    950 			scsipi_printaddr(periph);
    951 			printf("%s", error_mes[key - 1]);
    952 			if ((sense->error_code & SSD_ERRCODE_VALID) != 0) {
    953 				switch (key) {
    954 				case SKEY_NOT_READY:
    955 				case SKEY_ILLEGAL_REQUEST:
    956 				case SKEY_UNIT_ATTENTION:
    957 				case SKEY_WRITE_PROTECT:
    958 					break;
    959 				case SKEY_BLANK_CHECK:
    960 					printf(", requested size: %d (decimal)",
    961 					    info);
    962 					break;
    963 				case SKEY_ABORTED_COMMAND:
    964 					if (xs->xs_retries)
    965 						printf(", retrying");
    966 					printf(", cmd 0x%x, info 0x%x",
    967 					    xs->cmd->opcode, info);
    968 					break;
    969 				default:
    970 					printf(", info = %d (decimal)", info);
    971 				}
    972 			}
    973 			if (sense->extra_len != 0) {
    974 				int n;
    975 				printf(", data =");
    976 				for (n = 0; n < sense->extra_len; n++)
    977 					printf(" %02x",
    978 					    sense->cmd_spec_info[n]);
    979 			}
    980 			printf("\n");
    981 		}
    982 #endif
    983 		return (error);
    984 
    985 	/*
    986 	 * Some other code, just report it
    987 	 */
    988 	default:
    989 #if    defined(SCSIDEBUG) || defined(DEBUG)
    990 	{
    991 		static char *uc = "undecodable sense error";
    992 		int i;
    993 		u_int8_t *cptr = (u_int8_t *) sense;
    994 		scsipi_printaddr(periph);
    995 		if (xs->cmd == &xs->cmdstore) {
    996 			printf("%s for opcode 0x%x, data=",
    997 			    uc, xs->cmdstore.opcode);
    998 		} else {
    999 			printf("%s, data=", uc);
   1000 		}
   1001 		for (i = 0; i < sizeof (sense); i++)
   1002 			printf(" 0x%02x", *(cptr++) & 0xff);
   1003 		printf("\n");
   1004 	}
   1005 #else
   1006 		scsipi_printaddr(periph);
   1007 		printf("Sense Error Code 0x%x",
   1008 			sense->error_code & SSD_ERRCODE);
   1009 		if ((sense->error_code & SSD_ERRCODE_VALID) != 0) {
   1010 			struct scsipi_sense_data_unextended *usense =
   1011 			    (struct scsipi_sense_data_unextended *)sense;
   1012 			printf(" at block no. %d (decimal)",
   1013 			    _3btol(usense->block));
   1014 		}
   1015 		printf("\n");
   1016 #endif
   1017 		return (EIO);
   1018 	}
   1019 }
   1020 
   1021 /*
   1022  * scsipi_size:
   1023  *
   1024  *	Find out from the device what its capacity is.
   1025  */
   1026 u_int64_t
   1027 scsipi_size(struct scsipi_periph *periph, int flags)
   1028 {
   1029 	union {
   1030 		struct scsipi_read_capacity_10 cmd;
   1031 		struct scsipi_read_capacity_16 cmd16;
   1032 	} cmd;
   1033 	union {
   1034 		struct scsipi_read_capacity_10_data data;
   1035 		struct scsipi_read_capacity_16_data data16;
   1036 	} data;
   1037 
   1038 	memset(&cmd, 0, sizeof(cmd));
   1039 	cmd.cmd.opcode = READ_CAPACITY_10;
   1040 
   1041 	/*
   1042 	 * If the command works, interpret the result as a 4 byte
   1043 	 * number of blocks
   1044 	 */
   1045 	if (scsipi_command(periph, (void *)&cmd.cmd, sizeof(cmd.cmd),
   1046 	    (void *)&data.data, sizeof(data.data), SCSIPIRETRIES, 20000, NULL,
   1047 	    flags | XS_CTL_DATA_IN | XS_CTL_DATA_ONSTACK | XS_CTL_SILENT) != 0)
   1048 		return (0);
   1049 
   1050 	if (_4btol(data.data.addr) != 0xffffffff)
   1051 		return (_4btol(data.data.addr) + 1);
   1052 
   1053 	/*
   1054 	 * Device is larger than can be reflected by READ CAPACITY (10).
   1055 	 * Try READ CAPACITY (16).
   1056 	 */
   1057 
   1058 	memset(&cmd, 0, sizeof(cmd));
   1059 	cmd.cmd16.opcode = READ_CAPACITY_16;
   1060 	cmd.cmd16.byte2 = SRC16_SERVICE_ACTION;
   1061 	_lto4b(sizeof(data.data16), cmd.cmd16.len);
   1062 
   1063 	if (scsipi_command(periph, (void *)&cmd.cmd16, sizeof(cmd.cmd16),
   1064 	    (void *)&data.data16, sizeof(data.data16), SCSIPIRETRIES, 20000,
   1065 	    NULL,
   1066 	    flags | XS_CTL_DATA_IN | XS_CTL_DATA_ONSTACK | XS_CTL_SILENT) != 0)
   1067 		return (0);
   1068 
   1069 	return (_8btol(data.data16.addr) + 1);
   1070 }
   1071 
   1072 /*
   1073  * scsipi_test_unit_ready:
   1074  *
   1075  *	Issue a `test unit ready' request.
   1076  */
   1077 int
   1078 scsipi_test_unit_ready(struct scsipi_periph *periph, int flags)
   1079 {
   1080 	struct scsipi_test_unit_ready cmd;
   1081 	int retries;
   1082 
   1083 	/* some ATAPI drives don't support TEST_UNIT_READY. Sigh */
   1084 	if (periph->periph_quirks & PQUIRK_NOTUR)
   1085 		return (0);
   1086 
   1087 	if (flags & XS_CTL_DISCOVERY)
   1088 		retries = 0;
   1089 	else
   1090 		retries = SCSIPIRETRIES;
   1091 
   1092 	memset(&cmd, 0, sizeof(cmd));
   1093 	cmd.opcode = TEST_UNIT_READY;
   1094 
   1095 	return (scsipi_command(periph, (void *)&cmd, sizeof(cmd), 0, 0,
   1096 	    retries, 10000, NULL, flags));
   1097 }
   1098 
   1099 /*
   1100  * scsipi_inquire:
   1101  *
   1102  *	Ask the device about itself.
   1103  */
   1104 int
   1105 scsipi_inquire(struct scsipi_periph *periph, struct scsipi_inquiry_data *inqbuf,
   1106     int flags)
   1107 {
   1108 	struct scsipi_inquiry cmd;
   1109 	int error;
   1110 	int retries;
   1111 
   1112 	if (flags & XS_CTL_DISCOVERY)
   1113 		retries = 0;
   1114 	else
   1115 		retries = SCSIPIRETRIES;
   1116 
   1117 	/*
   1118 	 * If we request more data than the device can provide, it SHOULD just
   1119 	 * return a short reponse.  However, some devices error with an
   1120 	 * ILLEGAL REQUEST sense code, and yet others have even more special
   1121 	 * failture modes (such as the GL641USB flash adapter, which goes loony
   1122 	 * and sends corrupted CRCs).  To work around this, and to bring our
   1123 	 * behavior more in line with other OSes, we do a shorter inquiry,
   1124 	 * covering all the SCSI-2 information, first, and then request more
   1125 	 * data iff the "additional length" field indicates there is more.
   1126 	 * - mycroft, 2003/10/16
   1127 	 */
   1128 	memset(&cmd, 0, sizeof(cmd));
   1129 	cmd.opcode = INQUIRY;
   1130 	cmd.length = SCSIPI_INQUIRY_LENGTH_SCSI2;
   1131 	error = scsipi_command(periph, (void *)&cmd, sizeof(cmd),
   1132 	    (void *)inqbuf, SCSIPI_INQUIRY_LENGTH_SCSI2, retries,
   1133 	    10000, NULL, flags | XS_CTL_DATA_IN);
   1134 	if (!error &&
   1135 	    inqbuf->additional_length > SCSIPI_INQUIRY_LENGTH_SCSI2 - 4) {
   1136 #if 0
   1137 printf("inquire: addlen=%d, retrying\n", inqbuf->additional_length);
   1138 #endif
   1139 		cmd.length = SCSIPI_INQUIRY_LENGTH_SCSI3;
   1140 		error = scsipi_command(periph, (void *)&cmd, sizeof(cmd),
   1141 		    (void *)inqbuf, SCSIPI_INQUIRY_LENGTH_SCSI3, retries,
   1142 		    10000, NULL, flags | XS_CTL_DATA_IN);
   1143 #if 0
   1144 printf("inquire: error=%d\n", error);
   1145 #endif
   1146 	}
   1147 
   1148 #ifdef SCSI_OLD_NOINQUIRY
   1149 	/*
   1150 	 * Kludge for the Adaptec ACB-4000 SCSI->MFM translator.
   1151 	 * This board doesn't support the INQUIRY command at all.
   1152 	 */
   1153 	if (error == EINVAL || error == EACCES) {
   1154 		/*
   1155 		 * Conjure up an INQUIRY response.
   1156 		 */
   1157 		inqbuf->device = (error == EINVAL ?
   1158 			 SID_QUAL_LU_PRESENT :
   1159 			 SID_QUAL_LU_NOTPRESENT) | T_DIRECT;
   1160 		inqbuf->dev_qual2 = 0;
   1161 		inqbuf->version = 0;
   1162 		inqbuf->response_format = SID_FORMAT_SCSI1;
   1163 		inqbuf->additional_length = SCSIPI_INQUIRY_LENGTH_SCSI2 - 4;
   1164 		inqbuf->flags1 = inqbuf->flags2 = inqbuf->flags3 = 0;
   1165 		memcpy(inqbuf->vendor, "ADAPTEC ACB-4000            ", 28);
   1166 		error = 0;
   1167 	}
   1168 
   1169 	/*
   1170 	 * Kludge for the Emulex MT-02 SCSI->QIC translator.
   1171 	 * This board gives an empty response to an INQUIRY command.
   1172 	 */
   1173 	else if (error == 0 &&
   1174 	    inqbuf->device == (SID_QUAL_LU_PRESENT | T_DIRECT) &&
   1175 	    inqbuf->dev_qual2 == 0 &&
   1176 	    inqbuf->version == 0 &&
   1177 	    inqbuf->response_format == SID_FORMAT_SCSI1) {
   1178 		/*
   1179 		 * Fill out the INQUIRY response.
   1180 		 */
   1181 		inqbuf->device = (SID_QUAL_LU_PRESENT | T_SEQUENTIAL);
   1182 		inqbuf->dev_qual2 = SID_REMOVABLE;
   1183 		inqbuf->additional_length = SCSIPI_INQUIRY_LENGTH_SCSI2 - 4;
   1184 		inqbuf->flags1 = inqbuf->flags2 = inqbuf->flags3 = 0;
   1185 		memcpy(inqbuf->vendor, "EMULEX  MT-02 QIC           ", 28);
   1186 	}
   1187 #endif /* SCSI_OLD_NOINQUIRY */
   1188 
   1189 	return error;
   1190 }
   1191 
   1192 /*
   1193  * scsipi_prevent:
   1194  *
   1195  *	Prevent or allow the user to remove the media
   1196  */
   1197 int
   1198 scsipi_prevent(struct scsipi_periph *periph, int type, int flags)
   1199 {
   1200 	struct scsipi_prevent cmd;
   1201 
   1202 	memset(&cmd, 0, sizeof(cmd));
   1203 	cmd.opcode = PREVENT_ALLOW;
   1204 	cmd.how = type;
   1205 
   1206 	return (scsipi_command(periph, (void *)&cmd, sizeof(cmd), 0, 0,
   1207 	    SCSIPIRETRIES, 5000, NULL, flags));
   1208 }
   1209 
   1210 /*
   1211  * scsipi_start:
   1212  *
   1213  *	Send a START UNIT.
   1214  */
   1215 int
   1216 scsipi_start(struct scsipi_periph *periph, int type, int flags)
   1217 {
   1218 	struct scsipi_start_stop cmd;
   1219 
   1220 	memset(&cmd, 0, sizeof(cmd));
   1221 	cmd.opcode = START_STOP;
   1222 	cmd.byte2 = 0x00;
   1223 	cmd.how = type;
   1224 
   1225 	return (scsipi_command(periph, (void *)&cmd, sizeof(cmd), 0, 0,
   1226 	    SCSIPIRETRIES, (type & SSS_START) ? 60000 : 10000, NULL, flags));
   1227 }
   1228 
   1229 /*
   1230  * scsipi_mode_sense, scsipi_mode_sense_big:
   1231  *	get a sense page from a device
   1232  */
   1233 
   1234 int
   1235 scsipi_mode_sense(struct scsipi_periph *periph, int byte2, int page,
   1236     struct scsipi_mode_header *data, int len, int flags, int retries,
   1237     int timeout)
   1238 {
   1239 	struct scsipi_mode_sense cmd;
   1240 
   1241 	memset(&cmd, 0, sizeof(cmd));
   1242 	cmd.opcode = MODE_SENSE;
   1243 	cmd.byte2 = byte2;
   1244 	cmd.page = page;
   1245 	cmd.length = len & 0xff;
   1246 
   1247 	return (scsipi_command(periph, (void *)&cmd, sizeof(cmd),
   1248 	    (void *)data, len, retries, timeout, NULL, flags | XS_CTL_DATA_IN));
   1249 }
   1250 
   1251 int
   1252 scsipi_mode_sense_big(struct scsipi_periph *periph, int byte2, int page,
   1253     struct scsipi_mode_header_big *data, int len, int flags, int retries,
   1254     int timeout)
   1255 {
   1256 	struct scsipi_mode_sense_big cmd;
   1257 
   1258 	memset(&cmd, 0, sizeof(cmd));
   1259 	cmd.opcode = MODE_SENSE_BIG;
   1260 	cmd.byte2 = byte2;
   1261 	cmd.page = page;
   1262 	_lto2b(len, cmd.length);
   1263 
   1264 	return (scsipi_command(periph, (void *)&cmd, sizeof(cmd),
   1265 	    (void *)data, len, retries, timeout, NULL, flags | XS_CTL_DATA_IN));
   1266 }
   1267 
   1268 int
   1269 scsipi_mode_select(struct scsipi_periph *periph, int byte2,
   1270     struct scsipi_mode_header *data, int len, int flags, int retries,
   1271     int timeout)
   1272 {
   1273 	struct scsipi_mode_select cmd;
   1274 
   1275 	memset(&cmd, 0, sizeof(cmd));
   1276 	cmd.opcode = MODE_SELECT;
   1277 	cmd.byte2 = byte2;
   1278 	cmd.length = len & 0xff;
   1279 
   1280 	return (scsipi_command(periph, (void *)&cmd, sizeof(cmd),
   1281 	    (void *)data, len, retries, timeout, NULL, flags | XS_CTL_DATA_OUT));
   1282 }
   1283 
   1284 int
   1285 scsipi_mode_select_big(struct scsipi_periph *periph, int byte2,
   1286     struct scsipi_mode_header_big *data, int len, int flags, int retries,
   1287     int timeout)
   1288 {
   1289 	struct scsipi_mode_select_big cmd;
   1290 
   1291 	memset(&cmd, 0, sizeof(cmd));
   1292 	cmd.opcode = MODE_SELECT_BIG;
   1293 	cmd.byte2 = byte2;
   1294 	_lto2b(len, cmd.length);
   1295 
   1296 	return (scsipi_command(periph, (void *)&cmd, sizeof(cmd),
   1297 	    (void *)data, len, retries, timeout, NULL, flags | XS_CTL_DATA_OUT));
   1298 }
   1299 
   1300 /*
   1301  * scsipi_done:
   1302  *
   1303  *	This routine is called by an adapter's interrupt handler when
   1304  *	an xfer is completed.
   1305  */
   1306 void
   1307 scsipi_done(struct scsipi_xfer *xs)
   1308 {
   1309 	struct scsipi_periph *periph = xs->xs_periph;
   1310 	struct scsipi_channel *chan = periph->periph_channel;
   1311 	int s, freezecnt;
   1312 
   1313 	SC_DEBUG(periph, SCSIPI_DB2, ("scsipi_done\n"));
   1314 #ifdef SCSIPI_DEBUG
   1315 	if (periph->periph_dbflags & SCSIPI_DB1)
   1316 		show_scsipi_cmd(xs);
   1317 #endif
   1318 
   1319 	s = splbio();
   1320 	/*
   1321 	 * The resource this command was using is now free.
   1322 	 */
   1323 	scsipi_put_resource(chan);
   1324 	xs->xs_periph->periph_sent--;
   1325 
   1326 	/*
   1327 	 * If the command was tagged, free the tag.
   1328 	 */
   1329 	if (XS_CTL_TAGTYPE(xs) != 0)
   1330 		scsipi_put_tag(xs);
   1331 	else
   1332 		periph->periph_flags &= ~PERIPH_UNTAG;
   1333 
   1334 	/* Mark the command as `done'. */
   1335 	xs->xs_status |= XS_STS_DONE;
   1336 
   1337 #ifdef DIAGNOSTIC
   1338 	if ((xs->xs_control & (XS_CTL_ASYNC|XS_CTL_POLL)) ==
   1339 	    (XS_CTL_ASYNC|XS_CTL_POLL))
   1340 		panic("scsipi_done: ASYNC and POLL");
   1341 #endif
   1342 
   1343 	/*
   1344 	 * If the xfer had an error of any sort, freeze the
   1345 	 * periph's queue.  Freeze it again if we were requested
   1346 	 * to do so in the xfer.
   1347 	 */
   1348 	freezecnt = 0;
   1349 	if (xs->error != XS_NOERROR)
   1350 		freezecnt++;
   1351 	if (xs->xs_control & XS_CTL_FREEZE_PERIPH)
   1352 		freezecnt++;
   1353 	if (freezecnt != 0)
   1354 		scsipi_periph_freeze(periph, freezecnt);
   1355 
   1356 	/*
   1357 	 * record the xfer with a pending sense, in case a SCSI reset is
   1358 	 * received before the thread is waked up.
   1359 	 */
   1360 	if (xs->error == XS_BUSY && xs->status == SCSI_CHECK) {
   1361 		periph->periph_flags |= PERIPH_SENSE;
   1362 		periph->periph_xscheck = xs;
   1363 	}
   1364 
   1365 	/*
   1366 	 * If this was an xfer that was not to complete asynchronously,
   1367 	 * let the requesting thread perform error checking/handling
   1368 	 * in its context.
   1369 	 */
   1370 	if ((xs->xs_control & XS_CTL_ASYNC) == 0) {
   1371 		splx(s);
   1372 		/*
   1373 		 * If it's a polling job, just return, to unwind the
   1374 		 * call graph.  We don't need to restart the queue,
   1375 		 * because pollings jobs are treated specially, and
   1376 		 * are really only used during crash dumps anyway
   1377 		 * (XXX or during boot-time autconfiguration of
   1378 		 * ATAPI devices).
   1379 		 */
   1380 		if (xs->xs_control & XS_CTL_POLL)
   1381 			return;
   1382 		wakeup(xs);
   1383 		goto out;
   1384 	}
   1385 
   1386 	/*
   1387 	 * Catch the extremely common case of I/O completing
   1388 	 * without error; no use in taking a context switch
   1389 	 * if we can handle it in interrupt context.
   1390 	 */
   1391 	if (xs->error == XS_NOERROR) {
   1392 		splx(s);
   1393 		(void) scsipi_complete(xs);
   1394 		goto out;
   1395 	}
   1396 
   1397 	/*
   1398 	 * There is an error on this xfer.  Put it on the channel's
   1399 	 * completion queue, and wake up the completion thread.
   1400 	 */
   1401 	TAILQ_INSERT_TAIL(&chan->chan_complete, xs, channel_q);
   1402 	splx(s);
   1403 	wakeup(&chan->chan_complete);
   1404 
   1405  out:
   1406 	/*
   1407 	 * If there are more xfers on the channel's queue, attempt to
   1408 	 * run them.
   1409 	 */
   1410 	scsipi_run_queue(chan);
   1411 }
   1412 
   1413 /*
   1414  * scsipi_complete:
   1415  *
   1416  *	Completion of a scsipi_xfer.  This is the guts of scsipi_done().
   1417  *
   1418  *	NOTE: This routine MUST be called with valid thread context
   1419  *	except for the case where the following two conditions are
   1420  *	true:
   1421  *
   1422  *		xs->error == XS_NOERROR
   1423  *		XS_CTL_ASYNC is set in xs->xs_control
   1424  *
   1425  *	The semantics of this routine can be tricky, so here is an
   1426  *	explanation:
   1427  *
   1428  *		0		Xfer completed successfully.
   1429  *
   1430  *		ERESTART	Xfer had an error, but was restarted.
   1431  *
   1432  *		anything else	Xfer had an error, return value is Unix
   1433  *				errno.
   1434  *
   1435  *	If the return value is anything but ERESTART:
   1436  *
   1437  *		- If XS_CTL_ASYNC is set, `xs' has been freed back to
   1438  *		  the pool.
   1439  *		- If there is a buf associated with the xfer,
   1440  *		  it has been biodone()'d.
   1441  */
   1442 static int
   1443 scsipi_complete(struct scsipi_xfer *xs)
   1444 {
   1445 	struct scsipi_periph *periph = xs->xs_periph;
   1446 	struct scsipi_channel *chan = periph->periph_channel;
   1447 	int error, s;
   1448 
   1449 #ifdef DIAGNOSTIC
   1450 	if ((xs->xs_control & XS_CTL_ASYNC) != 0 && xs->bp == NULL)
   1451 		panic("scsipi_complete: XS_CTL_ASYNC but no buf");
   1452 #endif
   1453 	/*
   1454 	 * If command terminated with a CHECK CONDITION, we need to issue a
   1455 	 * REQUEST_SENSE command. Once the REQUEST_SENSE has been processed
   1456 	 * we'll have the real status.
   1457 	 * Must be processed at splbio() to avoid missing a SCSI bus reset
   1458 	 * for this command.
   1459 	 */
   1460 	s = splbio();
   1461 	if (xs->error == XS_BUSY && xs->status == SCSI_CHECK) {
   1462 		/* request sense for a request sense ? */
   1463 		if (xs->xs_control & XS_CTL_REQSENSE) {
   1464 			scsipi_printaddr(periph);
   1465 			printf("request sense for a request sense ?\n");
   1466 			/* XXX maybe we should reset the device ? */
   1467 			/* we've been frozen because xs->error != XS_NOERROR */
   1468 			scsipi_periph_thaw(periph, 1);
   1469 			splx(s);
   1470 			if (xs->resid < xs->datalen) {
   1471 				printf("we read %d bytes of sense anyway:\n",
   1472 				    xs->datalen - xs->resid);
   1473 #ifdef SCSIVERBOSE
   1474 				scsipi_print_sense_data((void *)xs->data, 0);
   1475 #endif
   1476 			}
   1477 			return EINVAL;
   1478 		}
   1479 		scsipi_request_sense(xs);
   1480 	}
   1481 	splx(s);
   1482 
   1483 	/*
   1484 	 * If it's a user level request, bypass all usual completion
   1485 	 * processing, let the user work it out..
   1486 	 */
   1487 	if ((xs->xs_control & XS_CTL_USERCMD) != 0) {
   1488 		SC_DEBUG(periph, SCSIPI_DB3, ("calling user done()\n"));
   1489 		if (xs->error != XS_NOERROR)
   1490 			scsipi_periph_thaw(periph, 1);
   1491 		scsipi_user_done(xs);
   1492 		SC_DEBUG(periph, SCSIPI_DB3, ("returned from user done()\n "));
   1493 		return 0;
   1494 	}
   1495 
   1496 	switch (xs->error) {
   1497 	case XS_NOERROR:
   1498 		error = 0;
   1499 		break;
   1500 
   1501 	case XS_SENSE:
   1502 	case XS_SHORTSENSE:
   1503 		error = (*chan->chan_bustype->bustype_interpret_sense)(xs);
   1504 		break;
   1505 
   1506 	case XS_RESOURCE_SHORTAGE:
   1507 		/*
   1508 		 * XXX Should freeze channel's queue.
   1509 		 */
   1510 		scsipi_printaddr(periph);
   1511 		printf("adapter resource shortage\n");
   1512 		/* FALLTHROUGH */
   1513 
   1514 	case XS_BUSY:
   1515 		if (xs->error == XS_BUSY && xs->status == SCSI_QUEUE_FULL) {
   1516 			struct scsipi_max_openings mo;
   1517 
   1518 			/*
   1519 			 * We set the openings to active - 1, assuming that
   1520 			 * the command that got us here is the first one that
   1521 			 * can't fit into the device's queue.  If that's not
   1522 			 * the case, I guess we'll find out soon enough.
   1523 			 */
   1524 			mo.mo_target = periph->periph_target;
   1525 			mo.mo_lun = periph->periph_lun;
   1526 			if (periph->periph_active < periph->periph_openings)
   1527 				mo.mo_openings = periph->periph_active - 1;
   1528 			else
   1529 				mo.mo_openings = periph->periph_openings - 1;
   1530 #ifdef DIAGNOSTIC
   1531 			if (mo.mo_openings < 0) {
   1532 				scsipi_printaddr(periph);
   1533 				printf("QUEUE FULL resulted in < 0 openings\n");
   1534 				panic("scsipi_done");
   1535 			}
   1536 #endif
   1537 			if (mo.mo_openings == 0) {
   1538 				scsipi_printaddr(periph);
   1539 				printf("QUEUE FULL resulted in 0 openings\n");
   1540 				mo.mo_openings = 1;
   1541 			}
   1542 			scsipi_async_event(chan, ASYNC_EVENT_MAX_OPENINGS, &mo);
   1543 			error = ERESTART;
   1544 		} else if (xs->xs_retries != 0) {
   1545 			xs->xs_retries--;
   1546 			/*
   1547 			 * Wait one second, and try again.
   1548 			 */
   1549 			if ((xs->xs_control & XS_CTL_POLL) ||
   1550 			    (chan->chan_flags & SCSIPI_CHAN_TACTIVE) == 0) {
   1551 				delay(1000000);
   1552 			} else if (!callout_pending(&periph->periph_callout)) {
   1553 				scsipi_periph_freeze(periph, 1);
   1554 				callout_reset(&periph->periph_callout,
   1555 				    hz, scsipi_periph_timed_thaw, periph);
   1556 			}
   1557 			error = ERESTART;
   1558 		} else
   1559 			error = EBUSY;
   1560 		break;
   1561 
   1562 	case XS_REQUEUE:
   1563 		error = ERESTART;
   1564 		break;
   1565 
   1566 	case XS_SELTIMEOUT:
   1567 	case XS_TIMEOUT:
   1568 		/*
   1569 		 * If the device hasn't gone away, honor retry counts.
   1570 		 *
   1571 		 * Note that if we're in the middle of probing it,
   1572 		 * it won't be found because it isn't here yet so
   1573 		 * we won't honor the retry count in that case.
   1574 		 */
   1575 		if (scsipi_lookup_periph(chan, periph->periph_target,
   1576 		    periph->periph_lun) && xs->xs_retries != 0) {
   1577 			xs->xs_retries--;
   1578 			error = ERESTART;
   1579 		} else
   1580 			error = EIO;
   1581 		break;
   1582 
   1583 	case XS_RESET:
   1584 		if (xs->xs_control & XS_CTL_REQSENSE) {
   1585 			/*
   1586 			 * request sense interrupted by reset: signal it
   1587 			 * with EINTR return code.
   1588 			 */
   1589 			error = EINTR;
   1590 		} else {
   1591 			if (xs->xs_retries != 0) {
   1592 				xs->xs_retries--;
   1593 				error = ERESTART;
   1594 			} else
   1595 				error = EIO;
   1596 		}
   1597 		break;
   1598 
   1599 	case XS_DRIVER_STUFFUP:
   1600 		scsipi_printaddr(periph);
   1601 		printf("generic HBA error\n");
   1602 		error = EIO;
   1603 		break;
   1604 	default:
   1605 		scsipi_printaddr(periph);
   1606 		printf("invalid return code from adapter: %d\n", xs->error);
   1607 		error = EIO;
   1608 		break;
   1609 	}
   1610 
   1611 	s = splbio();
   1612 	if (error == ERESTART) {
   1613 		/*
   1614 		 * If we get here, the periph has been thawed and frozen
   1615 		 * again if we had to issue recovery commands.  Alternatively,
   1616 		 * it may have been frozen again and in a timed thaw.  In
   1617 		 * any case, we thaw the periph once we re-enqueue the
   1618 		 * command.  Once the periph is fully thawed, it will begin
   1619 		 * operation again.
   1620 		 */
   1621 		xs->error = XS_NOERROR;
   1622 		xs->status = SCSI_OK;
   1623 		xs->xs_status &= ~XS_STS_DONE;
   1624 		xs->xs_requeuecnt++;
   1625 		error = scsipi_enqueue(xs);
   1626 		if (error == 0) {
   1627 			scsipi_periph_thaw(periph, 1);
   1628 			splx(s);
   1629 			return (ERESTART);
   1630 		}
   1631 	}
   1632 
   1633 	/*
   1634 	 * scsipi_done() freezes the queue if not XS_NOERROR.
   1635 	 * Thaw it here.
   1636 	 */
   1637 	if (xs->error != XS_NOERROR)
   1638 		scsipi_periph_thaw(periph, 1);
   1639 
   1640 	if (periph->periph_switch->psw_done)
   1641 		periph->periph_switch->psw_done(xs, error);
   1642 
   1643 	if (xs->xs_control & XS_CTL_ASYNC)
   1644 		scsipi_put_xs(xs);
   1645 	splx(s);
   1646 
   1647 	return (error);
   1648 }
   1649 
   1650 /*
   1651  * Issue a request sense for the given scsipi_xfer. Called when the xfer
   1652  * returns with a CHECK_CONDITION status. Must be called in valid thread
   1653  * context and at splbio().
   1654  */
   1655 
   1656 static void
   1657 scsipi_request_sense(struct scsipi_xfer *xs)
   1658 {
   1659 	struct scsipi_periph *periph = xs->xs_periph;
   1660 	int flags, error;
   1661 	struct scsipi_sense cmd;
   1662 
   1663 	periph->periph_flags |= PERIPH_SENSE;
   1664 
   1665 	/* if command was polling, request sense will too */
   1666 	flags = xs->xs_control & XS_CTL_POLL;
   1667 	/* Polling commands can't sleep */
   1668 	if (flags)
   1669 		flags |= XS_CTL_NOSLEEP;
   1670 
   1671 	flags |= XS_CTL_REQSENSE | XS_CTL_URGENT | XS_CTL_DATA_IN |
   1672 	    XS_CTL_THAW_PERIPH | XS_CTL_FREEZE_PERIPH;
   1673 
   1674 	memset(&cmd, 0, sizeof(cmd));
   1675 	cmd.opcode = REQUEST_SENSE;
   1676 	cmd.length = sizeof(struct scsipi_sense_data);
   1677 
   1678 	error = scsipi_command(periph, (void *)&cmd, sizeof(cmd),
   1679 	    (void *)&xs->sense.scsi_sense, sizeof(struct scsipi_sense_data),
   1680 	    0, 1000, NULL, flags);
   1681 	periph->periph_flags &= ~PERIPH_SENSE;
   1682 	periph->periph_xscheck = NULL;
   1683 	switch (error) {
   1684 	case 0:
   1685 		/* we have a valid sense */
   1686 		xs->error = XS_SENSE;
   1687 		return;
   1688 	case EINTR:
   1689 		/* REQUEST_SENSE interrupted by bus reset. */
   1690 		xs->error = XS_RESET;
   1691 		return;
   1692 	case EIO:
   1693 		 /* request sense coudn't be performed */
   1694 		/*
   1695 		 * XXX this isn't quite right but we don't have anything
   1696 		 * better for now
   1697 		 */
   1698 		xs->error = XS_DRIVER_STUFFUP;
   1699 		return;
   1700 	default:
   1701 		 /* Notify that request sense failed. */
   1702 		xs->error = XS_DRIVER_STUFFUP;
   1703 		scsipi_printaddr(periph);
   1704 		printf("request sense failed with error %d\n", error);
   1705 		return;
   1706 	}
   1707 }
   1708 
   1709 /*
   1710  * scsipi_enqueue:
   1711  *
   1712  *	Enqueue an xfer on a channel.
   1713  */
   1714 static int
   1715 scsipi_enqueue(struct scsipi_xfer *xs)
   1716 {
   1717 	struct scsipi_channel *chan = xs->xs_periph->periph_channel;
   1718 	struct scsipi_xfer *qxs;
   1719 	int s;
   1720 
   1721 	s = splbio();
   1722 
   1723 	/*
   1724 	 * If the xfer is to be polled, and there are already jobs on
   1725 	 * the queue, we can't proceed.
   1726 	 */
   1727 	if ((xs->xs_control & XS_CTL_POLL) != 0 &&
   1728 	    TAILQ_FIRST(&chan->chan_queue) != NULL) {
   1729 		splx(s);
   1730 		xs->error = XS_DRIVER_STUFFUP;
   1731 		return (EAGAIN);
   1732 	}
   1733 
   1734 	/*
   1735 	 * If we have an URGENT xfer, it's an error recovery command
   1736 	 * and it should just go on the head of the channel's queue.
   1737 	 */
   1738 	if (xs->xs_control & XS_CTL_URGENT) {
   1739 		TAILQ_INSERT_HEAD(&chan->chan_queue, xs, channel_q);
   1740 		goto out;
   1741 	}
   1742 
   1743 	/*
   1744 	 * If this xfer has already been on the queue before, we
   1745 	 * need to reinsert it in the correct order.  That order is:
   1746 	 *
   1747 	 *	Immediately before the first xfer for this periph
   1748 	 *	with a requeuecnt less than xs->xs_requeuecnt.
   1749 	 *
   1750 	 * Failing that, at the end of the queue.  (We'll end up
   1751 	 * there naturally.)
   1752 	 */
   1753 	if (xs->xs_requeuecnt != 0) {
   1754 		for (qxs = TAILQ_FIRST(&chan->chan_queue); qxs != NULL;
   1755 		     qxs = TAILQ_NEXT(qxs, channel_q)) {
   1756 			if (qxs->xs_periph == xs->xs_periph &&
   1757 			    qxs->xs_requeuecnt < xs->xs_requeuecnt)
   1758 				break;
   1759 		}
   1760 		if (qxs != NULL) {
   1761 			TAILQ_INSERT_AFTER(&chan->chan_queue, qxs, xs,
   1762 			    channel_q);
   1763 			goto out;
   1764 		}
   1765 	}
   1766 	TAILQ_INSERT_TAIL(&chan->chan_queue, xs, channel_q);
   1767  out:
   1768 	if (xs->xs_control & XS_CTL_THAW_PERIPH)
   1769 		scsipi_periph_thaw(xs->xs_periph, 1);
   1770 	splx(s);
   1771 	return (0);
   1772 }
   1773 
   1774 /*
   1775  * scsipi_run_queue:
   1776  *
   1777  *	Start as many xfers as possible running on the channel.
   1778  */
   1779 static void
   1780 scsipi_run_queue(struct scsipi_channel *chan)
   1781 {
   1782 	struct scsipi_xfer *xs;
   1783 	struct scsipi_periph *periph;
   1784 	int s;
   1785 
   1786 	for (;;) {
   1787 		s = splbio();
   1788 
   1789 		/*
   1790 		 * If the channel is frozen, we can't do any work right
   1791 		 * now.
   1792 		 */
   1793 		if (chan->chan_qfreeze != 0) {
   1794 			splx(s);
   1795 			return;
   1796 		}
   1797 
   1798 		/*
   1799 		 * Look for work to do, and make sure we can do it.
   1800 		 */
   1801 		for (xs = TAILQ_FIRST(&chan->chan_queue); xs != NULL;
   1802 		     xs = TAILQ_NEXT(xs, channel_q)) {
   1803 			periph = xs->xs_periph;
   1804 
   1805 			if ((periph->periph_sent >= periph->periph_openings) ||
   1806 			    periph->periph_qfreeze != 0 ||
   1807 			    (periph->periph_flags & PERIPH_UNTAG) != 0)
   1808 				continue;
   1809 
   1810 			if ((periph->periph_flags &
   1811 			    (PERIPH_RECOVERING | PERIPH_SENSE)) != 0 &&
   1812 			    (xs->xs_control & XS_CTL_URGENT) == 0)
   1813 				continue;
   1814 
   1815 			/*
   1816 			 * We can issue this xfer!
   1817 			 */
   1818 			goto got_one;
   1819 		}
   1820 
   1821 		/*
   1822 		 * Can't find any work to do right now.
   1823 		 */
   1824 		splx(s);
   1825 		return;
   1826 
   1827  got_one:
   1828 		/*
   1829 		 * Have an xfer to run.  Allocate a resource from
   1830 		 * the adapter to run it.  If we can't allocate that
   1831 		 * resource, we don't dequeue the xfer.
   1832 		 */
   1833 		if (scsipi_get_resource(chan) == 0) {
   1834 			/*
   1835 			 * Adapter is out of resources.  If the adapter
   1836 			 * supports it, attempt to grow them.
   1837 			 */
   1838 			if (scsipi_grow_resources(chan) == 0) {
   1839 				/*
   1840 				 * Wasn't able to grow resources,
   1841 				 * nothing more we can do.
   1842 				 */
   1843 				if (xs->xs_control & XS_CTL_POLL) {
   1844 					scsipi_printaddr(xs->xs_periph);
   1845 					printf("polling command but no "
   1846 					    "adapter resources");
   1847 					/* We'll panic shortly... */
   1848 				}
   1849 				splx(s);
   1850 
   1851 				/*
   1852 				 * XXX: We should be able to note that
   1853 				 * XXX: that resources are needed here!
   1854 				 */
   1855 				return;
   1856 			}
   1857 			/*
   1858 			 * scsipi_grow_resources() allocated the resource
   1859 			 * for us.
   1860 			 */
   1861 		}
   1862 
   1863 		/*
   1864 		 * We have a resource to run this xfer, do it!
   1865 		 */
   1866 		TAILQ_REMOVE(&chan->chan_queue, xs, channel_q);
   1867 
   1868 		/*
   1869 		 * If the command is to be tagged, allocate a tag ID
   1870 		 * for it.
   1871 		 */
   1872 		if (XS_CTL_TAGTYPE(xs) != 0)
   1873 			scsipi_get_tag(xs);
   1874 		else
   1875 			periph->periph_flags |= PERIPH_UNTAG;
   1876 		periph->periph_sent++;
   1877 		splx(s);
   1878 
   1879 		scsipi_adapter_request(chan, ADAPTER_REQ_RUN_XFER, xs);
   1880 	}
   1881 #ifdef DIAGNOSTIC
   1882 	panic("scsipi_run_queue: impossible");
   1883 #endif
   1884 }
   1885 
   1886 /*
   1887  * scsipi_execute_xs:
   1888  *
   1889  *	Begin execution of an xfer, waiting for it to complete, if necessary.
   1890  */
   1891 int
   1892 scsipi_execute_xs(struct scsipi_xfer *xs)
   1893 {
   1894 	struct scsipi_periph *periph = xs->xs_periph;
   1895 	struct scsipi_channel *chan = periph->periph_channel;
   1896 	int oasync, async, poll, error, s;
   1897 
   1898 	KASSERT(!cold);
   1899 
   1900 	(chan->chan_bustype->bustype_cmd)(xs);
   1901 
   1902 	if (xs->xs_control & XS_CTL_DATA_ONSTACK) {
   1903 #if 1
   1904 		if (xs->xs_control & XS_CTL_ASYNC)
   1905 			panic("scsipi_execute_xs: on stack and async");
   1906 #endif
   1907 		/*
   1908 		 * If the I/O buffer is allocated on stack, the
   1909 		 * process must NOT be swapped out, as the device will
   1910 		 * be accessing the stack.
   1911 		 */
   1912 		PHOLD(curlwp);
   1913 	}
   1914 
   1915 	xs->xs_status &= ~XS_STS_DONE;
   1916 	xs->error = XS_NOERROR;
   1917 	xs->resid = xs->datalen;
   1918 	xs->status = SCSI_OK;
   1919 
   1920 #ifdef SCSIPI_DEBUG
   1921 	if (xs->xs_periph->periph_dbflags & SCSIPI_DB3) {
   1922 		printf("scsipi_execute_xs: ");
   1923 		show_scsipi_xs(xs);
   1924 		printf("\n");
   1925 	}
   1926 #endif
   1927 
   1928 	/*
   1929 	 * Deal with command tagging:
   1930 	 *
   1931 	 *	- If the device's current operating mode doesn't
   1932 	 *	  include tagged queueing, clear the tag mask.
   1933 	 *
   1934 	 *	- If the device's current operating mode *does*
   1935 	 *	  include tagged queueing, set the tag_type in
   1936 	 *	  the xfer to the appropriate byte for the tag
   1937 	 *	  message.
   1938 	 */
   1939 	if ((PERIPH_XFER_MODE(periph) & PERIPH_CAP_TQING) == 0 ||
   1940 		(xs->xs_control & XS_CTL_REQSENSE)) {
   1941 		xs->xs_control &= ~XS_CTL_TAGMASK;
   1942 		xs->xs_tag_type = 0;
   1943 	} else {
   1944 		/*
   1945 		 * If the request doesn't specify a tag, give Head
   1946 		 * tags to URGENT operations and Ordered tags to
   1947 		 * everything else.
   1948 		 */
   1949 		if (XS_CTL_TAGTYPE(xs) == 0) {
   1950 			if (xs->xs_control & XS_CTL_URGENT)
   1951 				xs->xs_control |= XS_CTL_HEAD_TAG;
   1952 			else
   1953 				xs->xs_control |= XS_CTL_ORDERED_TAG;
   1954 		}
   1955 
   1956 		switch (XS_CTL_TAGTYPE(xs)) {
   1957 		case XS_CTL_ORDERED_TAG:
   1958 			xs->xs_tag_type = MSG_ORDERED_Q_TAG;
   1959 			break;
   1960 
   1961 		case XS_CTL_SIMPLE_TAG:
   1962 			xs->xs_tag_type = MSG_SIMPLE_Q_TAG;
   1963 			break;
   1964 
   1965 		case XS_CTL_HEAD_TAG:
   1966 			xs->xs_tag_type = MSG_HEAD_OF_Q_TAG;
   1967 			break;
   1968 
   1969 		default:
   1970 			scsipi_printaddr(periph);
   1971 			printf("invalid tag mask 0x%08x\n",
   1972 			    XS_CTL_TAGTYPE(xs));
   1973 			panic("scsipi_execute_xs");
   1974 		}
   1975 	}
   1976 
   1977 	/* If the adaptor wants us to poll, poll. */
   1978 	if (chan->chan_adapter->adapt_flags & SCSIPI_ADAPT_POLL_ONLY)
   1979 		xs->xs_control |= XS_CTL_POLL;
   1980 
   1981 	/*
   1982 	 * If we don't yet have a completion thread, or we are to poll for
   1983 	 * completion, clear the ASYNC flag.
   1984 	 */
   1985 	oasync =  (xs->xs_control & XS_CTL_ASYNC);
   1986 	if (chan->chan_thread == NULL || (xs->xs_control & XS_CTL_POLL) != 0)
   1987 		xs->xs_control &= ~XS_CTL_ASYNC;
   1988 
   1989 	async = (xs->xs_control & XS_CTL_ASYNC);
   1990 	poll = (xs->xs_control & XS_CTL_POLL);
   1991 
   1992 #ifdef DIAGNOSTIC
   1993 	if (oasync != 0 && xs->bp == NULL)
   1994 		panic("scsipi_execute_xs: XS_CTL_ASYNC but no buf");
   1995 #endif
   1996 
   1997 	/*
   1998 	 * Enqueue the transfer.  If we're not polling for completion, this
   1999 	 * should ALWAYS return `no error'.
   2000 	 */
   2001 	error = scsipi_enqueue(xs);
   2002 	if (error) {
   2003 		if (poll == 0) {
   2004 			scsipi_printaddr(periph);
   2005 			printf("not polling, but enqueue failed with %d\n",
   2006 			    error);
   2007 			panic("scsipi_execute_xs");
   2008 		}
   2009 
   2010 		scsipi_printaddr(periph);
   2011 		printf("should have flushed queue?\n");
   2012 		goto free_xs;
   2013 	}
   2014 
   2015  restarted:
   2016 	scsipi_run_queue(chan);
   2017 
   2018 	/*
   2019 	 * The xfer is enqueued, and possibly running.  If it's to be
   2020 	 * completed asynchronously, just return now.
   2021 	 */
   2022 	if (async)
   2023 		return (0);
   2024 
   2025 	/*
   2026 	 * Not an asynchronous command; wait for it to complete.
   2027 	 */
   2028 	s = splbio();
   2029 	while ((xs->xs_status & XS_STS_DONE) == 0) {
   2030 		if (poll) {
   2031 			scsipi_printaddr(periph);
   2032 			printf("polling command not done\n");
   2033 			panic("scsipi_execute_xs");
   2034 		}
   2035 		(void) tsleep(xs, PRIBIO, "xscmd", 0);
   2036 	}
   2037 	splx(s);
   2038 
   2039 	/*
   2040 	 * Command is complete.  scsipi_done() has awakened us to perform
   2041 	 * the error handling.
   2042 	 */
   2043 	error = scsipi_complete(xs);
   2044 	if (error == ERESTART)
   2045 		goto restarted;
   2046 
   2047 	/*
   2048 	 * If it was meant to run async and we cleared aync ourselve,
   2049 	 * don't return an error here. It has already been handled
   2050 	 */
   2051 	if (oasync)
   2052 		error = 0;
   2053 	/*
   2054 	 * Command completed successfully or fatal error occurred.  Fall
   2055 	 * into....
   2056 	 */
   2057  free_xs:
   2058 	if (xs->xs_control & XS_CTL_DATA_ONSTACK)
   2059 		PRELE(curlwp);
   2060 
   2061 	s = splbio();
   2062 	scsipi_put_xs(xs);
   2063 	splx(s);
   2064 
   2065 	/*
   2066 	 * Kick the queue, keep it running in case it stopped for some
   2067 	 * reason.
   2068 	 */
   2069 	scsipi_run_queue(chan);
   2070 
   2071 	return (error);
   2072 }
   2073 
   2074 /*
   2075  * scsipi_completion_thread:
   2076  *
   2077  *	This is the completion thread.  We wait for errors on
   2078  *	asynchronous xfers, and perform the error handling
   2079  *	function, restarting the command, if necessary.
   2080  */
   2081 static void
   2082 scsipi_completion_thread(void *arg)
   2083 {
   2084 	struct scsipi_channel *chan = arg;
   2085 	struct scsipi_xfer *xs;
   2086 	int s;
   2087 
   2088 	if (chan->chan_init_cb)
   2089 		(*chan->chan_init_cb)(chan, chan->chan_init_cb_arg);
   2090 
   2091 	s = splbio();
   2092 	chan->chan_flags |= SCSIPI_CHAN_TACTIVE;
   2093 	splx(s);
   2094 	for (;;) {
   2095 		s = splbio();
   2096 		xs = TAILQ_FIRST(&chan->chan_complete);
   2097 		if (xs == NULL && chan->chan_tflags  == 0) {
   2098 			/* nothing to do; wait */
   2099 			(void) tsleep(&chan->chan_complete, PRIBIO,
   2100 			    "sccomp", 0);
   2101 			splx(s);
   2102 			continue;
   2103 		}
   2104 		if (chan->chan_tflags & SCSIPI_CHANT_CALLBACK) {
   2105 			/* call chan_callback from thread context */
   2106 			chan->chan_tflags &= ~SCSIPI_CHANT_CALLBACK;
   2107 			chan->chan_callback(chan, chan->chan_callback_arg);
   2108 			splx(s);
   2109 			continue;
   2110 		}
   2111 		if (chan->chan_tflags & SCSIPI_CHANT_GROWRES) {
   2112 			/* attempt to get more openings for this channel */
   2113 			chan->chan_tflags &= ~SCSIPI_CHANT_GROWRES;
   2114 			scsipi_adapter_request(chan,
   2115 			    ADAPTER_REQ_GROW_RESOURCES, NULL);
   2116 			scsipi_channel_thaw(chan, 1);
   2117 			splx(s);
   2118 			continue;
   2119 		}
   2120 		if (chan->chan_tflags & SCSIPI_CHANT_KICK) {
   2121 			/* explicitly run the queues for this channel */
   2122 			chan->chan_tflags &= ~SCSIPI_CHANT_KICK;
   2123 			scsipi_run_queue(chan);
   2124 			splx(s);
   2125 			continue;
   2126 		}
   2127 		if (chan->chan_tflags & SCSIPI_CHANT_SHUTDOWN) {
   2128 			splx(s);
   2129 			break;
   2130 		}
   2131 		if (xs) {
   2132 			TAILQ_REMOVE(&chan->chan_complete, xs, channel_q);
   2133 			splx(s);
   2134 
   2135 			/*
   2136 			 * Have an xfer with an error; process it.
   2137 			 */
   2138 			(void) scsipi_complete(xs);
   2139 
   2140 			/*
   2141 			 * Kick the queue; keep it running if it was stopped
   2142 			 * for some reason.
   2143 			 */
   2144 			scsipi_run_queue(chan);
   2145 		} else {
   2146 			splx(s);
   2147 		}
   2148 	}
   2149 
   2150 	chan->chan_thread = NULL;
   2151 
   2152 	/* In case parent is waiting for us to exit. */
   2153 	wakeup(&chan->chan_thread);
   2154 
   2155 	kthread_exit(0);
   2156 }
   2157 
   2158 /*
   2159  * scsipi_create_completion_thread:
   2160  *
   2161  *	Callback to actually create the completion thread.
   2162  */
   2163 void
   2164 scsipi_create_completion_thread(void *arg)
   2165 {
   2166 	struct scsipi_channel *chan = arg;
   2167 	struct scsipi_adapter *adapt = chan->chan_adapter;
   2168 
   2169 	if (kthread_create1(scsipi_completion_thread, chan,
   2170 	    &chan->chan_thread, "%s", chan->chan_name)) {
   2171 		printf("%s: unable to create completion thread for "
   2172 		    "channel %d\n", adapt->adapt_dev->dv_xname,
   2173 		    chan->chan_channel);
   2174 		panic("scsipi_create_completion_thread");
   2175 	}
   2176 }
   2177 
   2178 /*
   2179  * scsipi_thread_call_callback:
   2180  *
   2181  * 	request to call a callback from the completion thread
   2182  */
   2183 int
   2184 scsipi_thread_call_callback(struct scsipi_channel *chan,
   2185     void (*callback)(struct scsipi_channel *, void *), void *arg)
   2186 {
   2187 	int s;
   2188 
   2189 	s = splbio();
   2190 	if ((chan->chan_flags & SCSIPI_CHAN_TACTIVE) == 0) {
   2191 		/* kernel thread doesn't exist yet */
   2192 		splx(s);
   2193 		return ESRCH;
   2194 	}
   2195 	if (chan->chan_tflags & SCSIPI_CHANT_CALLBACK) {
   2196 		splx(s);
   2197 		return EBUSY;
   2198 	}
   2199 	scsipi_channel_freeze(chan, 1);
   2200 	chan->chan_callback = callback;
   2201 	chan->chan_callback_arg = arg;
   2202 	chan->chan_tflags |= SCSIPI_CHANT_CALLBACK;
   2203 	wakeup(&chan->chan_complete);
   2204 	splx(s);
   2205 	return(0);
   2206 }
   2207 
   2208 /*
   2209  * scsipi_async_event:
   2210  *
   2211  *	Handle an asynchronous event from an adapter.
   2212  */
   2213 void
   2214 scsipi_async_event(struct scsipi_channel *chan, scsipi_async_event_t event,
   2215     void *arg)
   2216 {
   2217 	int s;
   2218 
   2219 	s = splbio();
   2220 	switch (event) {
   2221 	case ASYNC_EVENT_MAX_OPENINGS:
   2222 		scsipi_async_event_max_openings(chan,
   2223 		    (struct scsipi_max_openings *)arg);
   2224 		break;
   2225 
   2226 	case ASYNC_EVENT_XFER_MODE:
   2227 		scsipi_async_event_xfer_mode(chan,
   2228 		    (struct scsipi_xfer_mode *)arg);
   2229 		break;
   2230 	case ASYNC_EVENT_RESET:
   2231 		scsipi_async_event_channel_reset(chan);
   2232 		break;
   2233 	}
   2234 	splx(s);
   2235 }
   2236 
   2237 /*
   2238  * scsipi_print_xfer_mode:
   2239  *
   2240  *	Print a periph's capabilities.
   2241  */
   2242 void
   2243 scsipi_print_xfer_mode(struct scsipi_periph *periph)
   2244 {
   2245 	int period, freq, speed, mbs;
   2246 
   2247 	if ((periph->periph_flags & PERIPH_MODE_VALID) == 0)
   2248 		return;
   2249 
   2250 	aprint_normal("%s: ", periph->periph_dev->dv_xname);
   2251 	if (periph->periph_mode & (PERIPH_CAP_SYNC | PERIPH_CAP_DT)) {
   2252 		period = scsipi_sync_factor_to_period(periph->periph_period);
   2253 		aprint_normal("sync (%d.%02dns offset %d)",
   2254 		    period / 100, period % 100, periph->periph_offset);
   2255 	} else
   2256 		aprint_normal("async");
   2257 
   2258 	if (periph->periph_mode & PERIPH_CAP_WIDE32)
   2259 		aprint_normal(", 32-bit");
   2260 	else if (periph->periph_mode & (PERIPH_CAP_WIDE16 | PERIPH_CAP_DT))
   2261 		aprint_normal(", 16-bit");
   2262 	else
   2263 		aprint_normal(", 8-bit");
   2264 
   2265 	if (periph->periph_mode & (PERIPH_CAP_SYNC | PERIPH_CAP_DT)) {
   2266 		freq = scsipi_sync_factor_to_freq(periph->periph_period);
   2267 		speed = freq;
   2268 		if (periph->periph_mode & PERIPH_CAP_WIDE32)
   2269 			speed *= 4;
   2270 		else if (periph->periph_mode &
   2271 		    (PERIPH_CAP_WIDE16 | PERIPH_CAP_DT))
   2272 			speed *= 2;
   2273 		mbs = speed / 1000;
   2274 		if (mbs > 0)
   2275 			aprint_normal(" (%d.%03dMB/s)", mbs, speed % 1000);
   2276 		else
   2277 			aprint_normal(" (%dKB/s)", speed % 1000);
   2278 	}
   2279 
   2280 	aprint_normal(" transfers");
   2281 
   2282 	if (periph->periph_mode & PERIPH_CAP_TQING)
   2283 		aprint_normal(", tagged queueing");
   2284 
   2285 	aprint_normal("\n");
   2286 }
   2287 
   2288 /*
   2289  * scsipi_async_event_max_openings:
   2290  *
   2291  *	Update the maximum number of outstanding commands a
   2292  *	device may have.
   2293  */
   2294 static void
   2295 scsipi_async_event_max_openings(struct scsipi_channel *chan,
   2296     struct scsipi_max_openings *mo)
   2297 {
   2298 	struct scsipi_periph *periph;
   2299 	int minlun, maxlun;
   2300 
   2301 	if (mo->mo_lun == -1) {
   2302 		/*
   2303 		 * Wildcarded; apply it to all LUNs.
   2304 		 */
   2305 		minlun = 0;
   2306 		maxlun = chan->chan_nluns - 1;
   2307 	} else
   2308 		minlun = maxlun = mo->mo_lun;
   2309 
   2310 	/* XXX This could really suck with a large LUN space. */
   2311 	for (; minlun <= maxlun; minlun++) {
   2312 		periph = scsipi_lookup_periph(chan, mo->mo_target, minlun);
   2313 		if (periph == NULL)
   2314 			continue;
   2315 
   2316 		if (mo->mo_openings < periph->periph_openings)
   2317 			periph->periph_openings = mo->mo_openings;
   2318 		else if (mo->mo_openings > periph->periph_openings &&
   2319 		    (periph->periph_flags & PERIPH_GROW_OPENINGS) != 0)
   2320 			periph->periph_openings = mo->mo_openings;
   2321 	}
   2322 }
   2323 
   2324 /*
   2325  * scsipi_async_event_xfer_mode:
   2326  *
   2327  *	Update the xfer mode for all periphs sharing the
   2328  *	specified I_T Nexus.
   2329  */
   2330 static void
   2331 scsipi_async_event_xfer_mode(struct scsipi_channel *chan,
   2332     struct scsipi_xfer_mode *xm)
   2333 {
   2334 	struct scsipi_periph *periph;
   2335 	int lun, announce, mode, period, offset;
   2336 
   2337 	for (lun = 0; lun < chan->chan_nluns; lun++) {
   2338 		periph = scsipi_lookup_periph(chan, xm->xm_target, lun);
   2339 		if (periph == NULL)
   2340 			continue;
   2341 		announce = 0;
   2342 
   2343 		/*
   2344 		 * Clamp the xfer mode down to this periph's capabilities.
   2345 		 */
   2346 		mode = xm->xm_mode & periph->periph_cap;
   2347 		if (mode & PERIPH_CAP_SYNC) {
   2348 			period = xm->xm_period;
   2349 			offset = xm->xm_offset;
   2350 		} else {
   2351 			period = 0;
   2352 			offset = 0;
   2353 		}
   2354 
   2355 		/*
   2356 		 * If we do not have a valid xfer mode yet, or the parameters
   2357 		 * are different, announce them.
   2358 		 */
   2359 		if ((periph->periph_flags & PERIPH_MODE_VALID) == 0 ||
   2360 		    periph->periph_mode != mode ||
   2361 		    periph->periph_period != period ||
   2362 		    periph->periph_offset != offset)
   2363 			announce = 1;
   2364 
   2365 		periph->periph_mode = mode;
   2366 		periph->periph_period = period;
   2367 		periph->periph_offset = offset;
   2368 		periph->periph_flags |= PERIPH_MODE_VALID;
   2369 
   2370 		if (announce)
   2371 			scsipi_print_xfer_mode(periph);
   2372 	}
   2373 }
   2374 
   2375 /*
   2376  * scsipi_set_xfer_mode:
   2377  *
   2378  *	Set the xfer mode for the specified I_T Nexus.
   2379  */
   2380 void
   2381 scsipi_set_xfer_mode(struct scsipi_channel *chan, int target, int immed)
   2382 {
   2383 	struct scsipi_xfer_mode xm;
   2384 	struct scsipi_periph *itperiph;
   2385 	int lun, s;
   2386 
   2387 	/*
   2388 	 * Go to the minimal xfer mode.
   2389 	 */
   2390 	xm.xm_target = target;
   2391 	xm.xm_mode = 0;
   2392 	xm.xm_period = 0;			/* ignored */
   2393 	xm.xm_offset = 0;			/* ignored */
   2394 
   2395 	/*
   2396 	 * Find the first LUN we know about on this I_T Nexus.
   2397 	 */
   2398 	for (itperiph = NULL, lun = 0; lun < chan->chan_nluns; lun++) {
   2399 		itperiph = scsipi_lookup_periph(chan, target, lun);
   2400 		if (itperiph != NULL)
   2401 			break;
   2402 	}
   2403 	if (itperiph != NULL) {
   2404 		xm.xm_mode = itperiph->periph_cap;
   2405 		/*
   2406 		 * Now issue the request to the adapter.
   2407 		 */
   2408 		s = splbio();
   2409 		scsipi_adapter_request(chan, ADAPTER_REQ_SET_XFER_MODE, &xm);
   2410 		splx(s);
   2411 		/*
   2412 		 * If we want this to happen immediately, issue a dummy
   2413 		 * command, since most adapters can't really negotiate unless
   2414 		 * they're executing a job.
   2415 		 */
   2416 		if (immed != 0) {
   2417 			(void) scsipi_test_unit_ready(itperiph,
   2418 			    XS_CTL_DISCOVERY | XS_CTL_IGNORE_ILLEGAL_REQUEST |
   2419 			    XS_CTL_IGNORE_NOT_READY |
   2420 			    XS_CTL_IGNORE_MEDIA_CHANGE);
   2421 		}
   2422 	}
   2423 }
   2424 
   2425 /*
   2426  * scsipi_channel_reset:
   2427  *
   2428  *	handle scsi bus reset
   2429  * called at splbio
   2430  */
   2431 static void
   2432 scsipi_async_event_channel_reset(struct scsipi_channel *chan)
   2433 {
   2434 	struct scsipi_xfer *xs, *xs_next;
   2435 	struct scsipi_periph *periph;
   2436 	int target, lun;
   2437 
   2438 	/*
   2439 	 * Channel has been reset. Also mark as reset pending REQUEST_SENSE
   2440 	 * commands; as the sense is not available any more.
   2441 	 * can't call scsipi_done() from here, as the command has not been
   2442 	 * sent to the adapter yet (this would corrupt accounting).
   2443 	 */
   2444 
   2445 	for (xs = TAILQ_FIRST(&chan->chan_queue); xs != NULL; xs = xs_next) {
   2446 		xs_next = TAILQ_NEXT(xs, channel_q);
   2447 		if (xs->xs_control & XS_CTL_REQSENSE) {
   2448 			TAILQ_REMOVE(&chan->chan_queue, xs, channel_q);
   2449 			xs->error = XS_RESET;
   2450 			if ((xs->xs_control & XS_CTL_ASYNC) != 0)
   2451 				TAILQ_INSERT_TAIL(&chan->chan_complete, xs,
   2452 				    channel_q);
   2453 		}
   2454 	}
   2455 	wakeup(&chan->chan_complete);
   2456 	/* Catch xs with pending sense which may not have a REQSENSE xs yet */
   2457 	for (target = 0; target < chan->chan_ntargets; target++) {
   2458 		if (target == chan->chan_id)
   2459 			continue;
   2460 		for (lun = 0; lun <  chan->chan_nluns; lun++) {
   2461 			periph = scsipi_lookup_periph(chan, target, lun);
   2462 			if (periph) {
   2463 				xs = periph->periph_xscheck;
   2464 				if (xs)
   2465 					xs->error = XS_RESET;
   2466 			}
   2467 		}
   2468 	}
   2469 }
   2470 
   2471 /*
   2472  * scsipi_target_detach:
   2473  *
   2474  *	detach all periph associated with a I_T
   2475  * 	must be called from valid thread context
   2476  */
   2477 int
   2478 scsipi_target_detach(struct scsipi_channel *chan, int target, int lun,
   2479     int flags)
   2480 {
   2481 	struct scsipi_periph *periph;
   2482 	int ctarget, mintarget, maxtarget;
   2483 	int clun, minlun, maxlun;
   2484 	int error;
   2485 
   2486 	if (target == -1) {
   2487 		mintarget = 0;
   2488 		maxtarget = chan->chan_ntargets;
   2489 	} else {
   2490 		if (target == chan->chan_id)
   2491 			return EINVAL;
   2492 		if (target < 0 || target >= chan->chan_ntargets)
   2493 			return EINVAL;
   2494 		mintarget = target;
   2495 		maxtarget = target + 1;
   2496 	}
   2497 
   2498 	if (lun == -1) {
   2499 		minlun = 0;
   2500 		maxlun = chan->chan_nluns;
   2501 	} else {
   2502 		if (lun < 0 || lun >= chan->chan_nluns)
   2503 			return EINVAL;
   2504 		minlun = lun;
   2505 		maxlun = lun + 1;
   2506 	}
   2507 
   2508 	for (ctarget = mintarget; ctarget < maxtarget; ctarget++) {
   2509 		if (ctarget == chan->chan_id)
   2510 			continue;
   2511 
   2512 		for (clun = minlun; clun < maxlun; clun++) {
   2513 			periph = scsipi_lookup_periph(chan, ctarget, clun);
   2514 			if (periph == NULL)
   2515 				continue;
   2516 			error = config_detach(periph->periph_dev, flags);
   2517 			if (error)
   2518 				return (error);
   2519 		}
   2520 	}
   2521 	return(0);
   2522 }
   2523 
   2524 /*
   2525  * scsipi_adapter_addref:
   2526  *
   2527  *	Add a reference to the adapter pointed to by the provided
   2528  *	link, enabling the adapter if necessary.
   2529  */
   2530 int
   2531 scsipi_adapter_addref(struct scsipi_adapter *adapt)
   2532 {
   2533 	int s, error = 0;
   2534 
   2535 	s = splbio();
   2536 	if (adapt->adapt_refcnt++ == 0 && adapt->adapt_enable != NULL) {
   2537 		error = (*adapt->adapt_enable)(adapt->adapt_dev, 1);
   2538 		if (error)
   2539 			adapt->adapt_refcnt--;
   2540 	}
   2541 	splx(s);
   2542 	return (error);
   2543 }
   2544 
   2545 /*
   2546  * scsipi_adapter_delref:
   2547  *
   2548  *	Delete a reference to the adapter pointed to by the provided
   2549  *	link, disabling the adapter if possible.
   2550  */
   2551 void
   2552 scsipi_adapter_delref(struct scsipi_adapter *adapt)
   2553 {
   2554 	int s;
   2555 
   2556 	s = splbio();
   2557 	if (adapt->adapt_refcnt-- == 1 && adapt->adapt_enable != NULL)
   2558 		(void) (*adapt->adapt_enable)(adapt->adapt_dev, 0);
   2559 	splx(s);
   2560 }
   2561 
   2562 static struct scsipi_syncparam {
   2563 	int	ss_factor;
   2564 	int	ss_period;	/* ns * 100 */
   2565 } scsipi_syncparams[] = {
   2566 	{ 0x08,		 625 },	/* FAST-160 (Ultra320) */
   2567 	{ 0x09,		1250 },	/* FAST-80 (Ultra160) */
   2568 	{ 0x0a,		2500 },	/* FAST-40 40MHz (Ultra2) */
   2569 	{ 0x0b,		3030 },	/* FAST-40 33MHz (Ultra2) */
   2570 	{ 0x0c,		5000 },	/* FAST-20 (Ultra) */
   2571 };
   2572 static const int scsipi_nsyncparams =
   2573     sizeof(scsipi_syncparams) / sizeof(scsipi_syncparams[0]);
   2574 
   2575 int
   2576 scsipi_sync_period_to_factor(int period /* ns * 100 */)
   2577 {
   2578 	int i;
   2579 
   2580 	for (i = 0; i < scsipi_nsyncparams; i++) {
   2581 		if (period <= scsipi_syncparams[i].ss_period)
   2582 			return (scsipi_syncparams[i].ss_factor);
   2583 	}
   2584 
   2585 	return ((period / 100) / 4);
   2586 }
   2587 
   2588 int
   2589 scsipi_sync_factor_to_period(int factor)
   2590 {
   2591 	int i;
   2592 
   2593 	for (i = 0; i < scsipi_nsyncparams; i++) {
   2594 		if (factor == scsipi_syncparams[i].ss_factor)
   2595 			return (scsipi_syncparams[i].ss_period);
   2596 	}
   2597 
   2598 	return ((factor * 4) * 100);
   2599 }
   2600 
   2601 int
   2602 scsipi_sync_factor_to_freq(int factor)
   2603 {
   2604 	int i;
   2605 
   2606 	for (i = 0; i < scsipi_nsyncparams; i++) {
   2607 		if (factor == scsipi_syncparams[i].ss_factor)
   2608 			return (100000000 / scsipi_syncparams[i].ss_period);
   2609 	}
   2610 
   2611 	return (10000000 / ((factor * 4) * 10));
   2612 }
   2613 
   2614 #ifdef SCSIPI_DEBUG
   2615 /*
   2616  * Given a scsipi_xfer, dump the request, in all it's glory
   2617  */
   2618 void
   2619 show_scsipi_xs(struct scsipi_xfer *xs)
   2620 {
   2621 
   2622 	printf("xs(%p): ", xs);
   2623 	printf("xs_control(0x%08x)", xs->xs_control);
   2624 	printf("xs_status(0x%08x)", xs->xs_status);
   2625 	printf("periph(%p)", xs->xs_periph);
   2626 	printf("retr(0x%x)", xs->xs_retries);
   2627 	printf("timo(0x%x)", xs->timeout);
   2628 	printf("cmd(%p)", xs->cmd);
   2629 	printf("len(0x%x)", xs->cmdlen);
   2630 	printf("data(%p)", xs->data);
   2631 	printf("len(0x%x)", xs->datalen);
   2632 	printf("res(0x%x)", xs->resid);
   2633 	printf("err(0x%x)", xs->error);
   2634 	printf("bp(%p)", xs->bp);
   2635 	show_scsipi_cmd(xs);
   2636 }
   2637 
   2638 void
   2639 show_scsipi_cmd(struct scsipi_xfer *xs)
   2640 {
   2641 	uint8_t *b = (uint8_t *) xs->cmd;
   2642 	int i = 0;
   2643 
   2644 	scsipi_printaddr(xs->xs_periph);
   2645 	printf(" command: ");
   2646 
   2647 	if ((xs->xs_control & XS_CTL_RESET) == 0) {
   2648 		while (i < xs->cmdlen) {
   2649 			if (i)
   2650 				printf(",");
   2651 			printf("0x%x", b[i++]);
   2652 		}
   2653 		printf("-[%d bytes]\n", xs->datalen);
   2654 		if (xs->datalen)
   2655 			show_mem(xs->data, min(64, xs->datalen));
   2656 	} else
   2657 		printf("-RESET-\n");
   2658 }
   2659 
   2660 void
   2661 show_mem(uint8_t *address, int num)
   2662 {
   2663 	int x;
   2664 
   2665 	printf("------------------------------");
   2666 	for (x = 0; x < num; x++) {
   2667 		if ((x % 16) == 0)
   2668 			printf("\n%03d: ", x);
   2669 		printf("%02x ", *address++);
   2670 	}
   2671 	printf("\n------------------------------\n");
   2672 }
   2673 #endif /* SCSIPI_DEBUG */
   2674