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