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