Home | History | Annotate | Line # | Download | only in scsipi
scsipi_base.c revision 1.9
      1  1.9   scottr /*	$NetBSD: scsipi_base.c,v 1.9 1998/09/14 05:49:21 scottr Exp $	*/
      2  1.2   bouyer 
      3  1.8  mycroft /*-
      4  1.8  mycroft  * Copyright (c) 1998 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.8  mycroft  * by Charles M. Hannum.
      9  1.2   bouyer  *
     10  1.2   bouyer  * Redistribution and use in source and binary forms, with or without
     11  1.2   bouyer  * modification, are permitted provided that the following conditions
     12  1.2   bouyer  * are met:
     13  1.2   bouyer  * 1. Redistributions of source code must retain the above copyright
     14  1.2   bouyer  *    notice, this list of conditions and the following disclaimer.
     15  1.2   bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.2   bouyer  *    notice, this list of conditions and the following disclaimer in the
     17  1.2   bouyer  *    documentation and/or other materials provided with the distribution.
     18  1.2   bouyer  * 3. All advertising materials mentioning features or use of this software
     19  1.2   bouyer  *    must display the following acknowledgement:
     20  1.8  mycroft  *        This product includes software developed by the NetBSD
     21  1.8  mycroft  *        Foundation, Inc. and its contributors.
     22  1.8  mycroft  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.8  mycroft  *    contributors may be used to endorse or promote products derived
     24  1.8  mycroft  *    from this software without specific prior written permission.
     25  1.2   bouyer  *
     26  1.8  mycroft  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.8  mycroft  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.8  mycroft  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.8  mycroft  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.8  mycroft  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.8  mycroft  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.8  mycroft  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.8  mycroft  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.8  mycroft  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.8  mycroft  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.8  mycroft  * POSSIBILITY OF SUCH DAMAGE.
     37  1.2   bouyer  */
     38  1.2   bouyer 
     39  1.2   bouyer #include <sys/types.h>
     40  1.2   bouyer #include <sys/param.h>
     41  1.2   bouyer #include <sys/systm.h>
     42  1.2   bouyer #include <sys/kernel.h>
     43  1.2   bouyer #include <sys/buf.h>
     44  1.2   bouyer #include <sys/uio.h>
     45  1.2   bouyer #include <sys/malloc.h>
     46  1.6  thorpej #include <sys/pool.h>
     47  1.2   bouyer #include <sys/errno.h>
     48  1.2   bouyer #include <sys/device.h>
     49  1.2   bouyer #include <sys/proc.h>
     50  1.2   bouyer 
     51  1.2   bouyer #include <dev/scsipi/scsipi_all.h>
     52  1.2   bouyer #include <dev/scsipi/scsi_all.h>
     53  1.2   bouyer #include <dev/scsipi/scsipi_disk.h>
     54  1.2   bouyer #include <dev/scsipi/scsipiconf.h>
     55  1.2   bouyer #include <dev/scsipi/scsipi_base.h>
     56  1.2   bouyer 
     57  1.6  thorpej struct pool scsipi_xfer_pool;
     58  1.6  thorpej 
     59  1.3    enami int	sc_err1 __P((struct scsipi_xfer *, int));
     60  1.2   bouyer 
     61  1.2   bouyer /*
     62  1.6  thorpej  * Called when a scsibus is attached to initialize global data.
     63  1.6  thorpej  */
     64  1.6  thorpej void
     65  1.6  thorpej scsipi_init()
     66  1.6  thorpej {
     67  1.6  thorpej 	static int scsipi_init_done;
     68  1.6  thorpej 
     69  1.6  thorpej 	if (scsipi_init_done)
     70  1.6  thorpej 		return;
     71  1.6  thorpej 	scsipi_init_done = 1;
     72  1.6  thorpej 
     73  1.6  thorpej 	/* Initialize the scsipi_xfer pool. */
     74  1.6  thorpej 	pool_init(&scsipi_xfer_pool, sizeof(struct scsipi_xfer), 0,
     75  1.6  thorpej 	    0, 0, "scxspl", 0, NULL, NULL, M_DEVBUF);
     76  1.6  thorpej }
     77  1.6  thorpej 
     78  1.6  thorpej /*
     79  1.2   bouyer  * Get a scsipi transfer structure for the caller. Charge the structure
     80  1.3    enami  * to the device that is referenced by the sc_link structure. If the
     81  1.2   bouyer  * sc_link structure has no 'credits' then the device already has the
     82  1.2   bouyer  * maximum number or outstanding operations under way. In this stage,
     83  1.2   bouyer  * wait on the structure so that when one is freed, we are awoken again
     84  1.2   bouyer  * If the SCSI_NOSLEEP flag is set, then do not wait, but rather, return
     85  1.2   bouyer  * a NULL pointer, signifying that no slots were available
     86  1.2   bouyer  * Note in the link structure, that we are waiting on it.
     87  1.2   bouyer  */
     88  1.2   bouyer 
     89  1.2   bouyer struct scsipi_xfer *
     90  1.2   bouyer scsipi_get_xs(sc_link, flags)
     91  1.2   bouyer 	struct scsipi_link *sc_link;	/* who to charge the xs to */
     92  1.2   bouyer 	int flags;			/* if this call can sleep */
     93  1.2   bouyer {
     94  1.2   bouyer 	struct scsipi_xfer *xs;
     95  1.2   bouyer 	int s;
     96  1.2   bouyer 
     97  1.2   bouyer 	SC_DEBUG(sc_link, SDEV_DB3, ("scsipi_get_xs\n"));
     98  1.6  thorpej 
     99  1.2   bouyer 	s = splbio();
    100  1.2   bouyer 	while (sc_link->openings <= 0) {
    101  1.2   bouyer 		SC_DEBUG(sc_link, SDEV_DB3, ("sleeping\n"));
    102  1.2   bouyer 		if ((flags & SCSI_NOSLEEP) != 0) {
    103  1.2   bouyer 			splx(s);
    104  1.3    enami 			return (0);
    105  1.2   bouyer 		}
    106  1.2   bouyer 		sc_link->flags |= SDEV_WAITING;
    107  1.3    enami 		(void)tsleep(sc_link, PRIBIO, "getxs", 0);
    108  1.2   bouyer 	}
    109  1.6  thorpej 	SC_DEBUG(sc_link, SDEV_DB3, ("calling pool_get\n"));
    110  1.6  thorpej 	xs = pool_get(&scsipi_xfer_pool,
    111  1.6  thorpej 	    ((flags & SCSI_NOSLEEP) != 0 ? PR_NOWAIT : PR_WAITOK));
    112  1.6  thorpej 	if (xs != NULL)
    113  1.6  thorpej 		sc_link->openings--;
    114  1.6  thorpej 	else {
    115  1.6  thorpej 		(*sc_link->sc_print_addr)(sc_link);
    116  1.6  thorpej 		printf("cannot allocate scsipi xs\n");
    117  1.2   bouyer 	}
    118  1.6  thorpej 	splx(s);
    119  1.2   bouyer 
    120  1.2   bouyer 	SC_DEBUG(sc_link, SDEV_DB3, ("returning\n"));
    121  1.6  thorpej 
    122  1.3    enami 	/*
    123  1.7   scottr 	 * zeroes out the command, as ATAPI may use longer commands
    124  1.3    enami 	 * than SCSI
    125  1.3    enami 	 */
    126  1.7   scottr 	if (xs != NULL) {
    127  1.7   scottr 		xs->flags = INUSE | flags;
    128  1.7   scottr 		bzero(&xs->cmdstore, sizeof(xs->cmdstore));
    129  1.7   scottr 	}
    130  1.3    enami 	return (xs);
    131  1.2   bouyer }
    132  1.2   bouyer 
    133  1.2   bouyer /*
    134  1.2   bouyer  * Given a scsipi_xfer struct, and a device (referenced through sc_link)
    135  1.2   bouyer  * return the struct to the free pool and credit the device with it
    136  1.2   bouyer  * If another process is waiting for an xs, do a wakeup, let it proceed
    137  1.6  thorpej  *
    138  1.6  thorpej  * MUST BE CALLED AT splbio()!!
    139  1.2   bouyer  */
    140  1.3    enami void
    141  1.2   bouyer scsipi_free_xs(xs, flags)
    142  1.2   bouyer 	struct scsipi_xfer *xs;
    143  1.2   bouyer 	int flags;
    144  1.2   bouyer {
    145  1.2   bouyer 	struct scsipi_link *sc_link = xs->sc_link;
    146  1.2   bouyer 
    147  1.2   bouyer 	xs->flags &= ~INUSE;
    148  1.6  thorpej 	pool_put(&scsipi_xfer_pool, xs);
    149  1.2   bouyer 
    150  1.2   bouyer 	SC_DEBUG(sc_link, SDEV_DB3, ("scsipi_free_xs\n"));
    151  1.2   bouyer 	/* if was 0 and someone waits, wake them up */
    152  1.2   bouyer 	sc_link->openings++;
    153  1.2   bouyer 	if ((sc_link->flags & SDEV_WAITING) != 0) {
    154  1.2   bouyer 		sc_link->flags &= ~SDEV_WAITING;
    155  1.2   bouyer 		wakeup(sc_link);
    156  1.2   bouyer 	} else {
    157  1.2   bouyer 		if (sc_link->device->start) {
    158  1.3    enami 			SC_DEBUG(sc_link, SDEV_DB2,
    159  1.3    enami 			    ("calling private start()\n"));
    160  1.3    enami 			(*(sc_link->device->start))(sc_link->device_softc);
    161  1.2   bouyer 		}
    162  1.2   bouyer 	}
    163  1.2   bouyer }
    164  1.2   bouyer 
    165  1.2   bouyer /*
    166  1.2   bouyer  * Find out from the device what its capacity is.
    167  1.2   bouyer  */
    168  1.2   bouyer u_long
    169  1.2   bouyer scsipi_size(sc_link, flags)
    170  1.2   bouyer 	struct scsipi_link *sc_link;
    171  1.2   bouyer 	int flags;
    172  1.2   bouyer {
    173  1.2   bouyer 	struct scsipi_read_cap_data rdcap;
    174  1.2   bouyer 	struct scsipi_read_capacity scsipi_cmd;
    175  1.2   bouyer 
    176  1.2   bouyer 	/*
    177  1.2   bouyer 	 * make up a scsipi command and ask the scsipi driver to do
    178  1.2   bouyer 	 * it for you.
    179  1.2   bouyer 	 */
    180  1.2   bouyer 	bzero(&scsipi_cmd, sizeof(scsipi_cmd));
    181  1.2   bouyer 	scsipi_cmd.opcode = READ_CAPACITY;
    182  1.2   bouyer 
    183  1.2   bouyer 	/*
    184  1.2   bouyer 	 * If the command works, interpret the result as a 4 byte
    185  1.2   bouyer 	 * number of blocks
    186  1.2   bouyer 	 */
    187  1.4  thorpej 	if (scsipi_command(sc_link, (struct scsipi_generic *)&scsipi_cmd,
    188  1.3    enami 	    sizeof(scsipi_cmd), (u_char *)&rdcap, sizeof(rdcap),
    189  1.3    enami 	    2, 20000, NULL, flags | SCSI_DATA_IN) != 0) {
    190  1.2   bouyer 		sc_link->sc_print_addr(sc_link);
    191  1.2   bouyer 		printf("could not get size\n");
    192  1.3    enami 		return (0);
    193  1.2   bouyer 	}
    194  1.2   bouyer 
    195  1.3    enami 	return (_4btol(rdcap.addr) + 1);
    196  1.2   bouyer }
    197  1.2   bouyer 
    198  1.2   bouyer /*
    199  1.2   bouyer  * Get scsipi driver to send a "are you ready?" command
    200  1.2   bouyer  */
    201  1.3    enami int
    202  1.2   bouyer scsipi_test_unit_ready(sc_link, flags)
    203  1.2   bouyer 	struct scsipi_link *sc_link;
    204  1.2   bouyer 	int flags;
    205  1.2   bouyer {
    206  1.2   bouyer 	struct scsipi_test_unit_ready scsipi_cmd;
    207  1.2   bouyer 
    208  1.2   bouyer 	/* some ATAPI drives don't support TEST_UNIT_READY. Sigh */
    209  1.2   bouyer 	if (sc_link->quirks & ADEV_NOTUR)
    210  1.3    enami 		return (0);
    211  1.2   bouyer 
    212  1.2   bouyer 	bzero(&scsipi_cmd, sizeof(scsipi_cmd));
    213  1.2   bouyer 	scsipi_cmd.opcode = TEST_UNIT_READY;
    214  1.2   bouyer 
    215  1.4  thorpej 	return (scsipi_command(sc_link,
    216  1.3    enami 	    (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd),
    217  1.3    enami 	    0, 0, 2, 10000, NULL, flags));
    218  1.2   bouyer }
    219  1.2   bouyer 
    220  1.2   bouyer /*
    221  1.2   bouyer  * Do a scsipi operation asking a device what it is
    222  1.2   bouyer  * Use the scsipi_cmd routine in the switch table.
    223  1.2   bouyer  * XXX actually this is only used for scsi devices, because I have the feeling
    224  1.2   bouyer  * that some atapi CDROM may not implement it, althouh it marked as mandatory
    225  1.2   bouyer  * in the atapi specs.
    226  1.2   bouyer  */
    227  1.3    enami int
    228  1.2   bouyer scsipi_inquire(sc_link, inqbuf, flags)
    229  1.2   bouyer 	struct scsipi_link *sc_link;
    230  1.2   bouyer 	struct scsipi_inquiry_data *inqbuf;
    231  1.2   bouyer 	int flags;
    232  1.2   bouyer {
    233  1.2   bouyer 	struct scsipi_inquiry scsipi_cmd;
    234  1.2   bouyer 
    235  1.2   bouyer 	bzero(&scsipi_cmd, sizeof(scsipi_cmd));
    236  1.2   bouyer 	scsipi_cmd.opcode = INQUIRY;
    237  1.2   bouyer 	scsipi_cmd.length = sizeof(struct scsipi_inquiry_data);
    238  1.2   bouyer 
    239  1.4  thorpej 	return (scsipi_command(sc_link,
    240  1.3    enami 	    (struct scsipi_generic *) &scsipi_cmd, sizeof(scsipi_cmd),
    241  1.3    enami 	    (u_char *) inqbuf, sizeof(struct scsipi_inquiry_data),
    242  1.3    enami 	    2, 10000, NULL, SCSI_DATA_IN | flags));
    243  1.2   bouyer }
    244  1.2   bouyer 
    245  1.2   bouyer /*
    246  1.2   bouyer  * Prevent or allow the user to remove the media
    247  1.2   bouyer  */
    248  1.3    enami int
    249  1.2   bouyer scsipi_prevent(sc_link, type, flags)
    250  1.2   bouyer 	struct scsipi_link *sc_link;
    251  1.2   bouyer 	int type, flags;
    252  1.2   bouyer {
    253  1.2   bouyer 	struct scsipi_prevent scsipi_cmd;
    254  1.2   bouyer 
    255  1.2   bouyer 	if (sc_link->quirks & ADEV_NODOORLOCK)
    256  1.3    enami 		return (0);
    257  1.2   bouyer 
    258  1.2   bouyer 	bzero(&scsipi_cmd, sizeof(scsipi_cmd));
    259  1.2   bouyer 	scsipi_cmd.opcode = PREVENT_ALLOW;
    260  1.2   bouyer 	scsipi_cmd.how = type;
    261  1.4  thorpej 	return (scsipi_command(sc_link,
    262  1.3    enami 	    (struct scsipi_generic *) &scsipi_cmd, sizeof(scsipi_cmd),
    263  1.3    enami 	    0, 0, 2, 5000, NULL, flags));
    264  1.2   bouyer }
    265  1.2   bouyer 
    266  1.2   bouyer /*
    267  1.2   bouyer  * Get scsipi driver to send a "start up" command
    268  1.2   bouyer  */
    269  1.3    enami int
    270  1.2   bouyer scsipi_start(sc_link, type, flags)
    271  1.2   bouyer 	struct scsipi_link *sc_link;
    272  1.2   bouyer 	int type, flags;
    273  1.2   bouyer {
    274  1.2   bouyer 	struct scsipi_start_stop scsipi_cmd;
    275  1.2   bouyer 
    276  1.2   bouyer 	bzero(&scsipi_cmd, sizeof(scsipi_cmd));
    277  1.2   bouyer 	scsipi_cmd.opcode = START_STOP;
    278  1.2   bouyer 	scsipi_cmd.byte2 = 0x00;
    279  1.2   bouyer 	scsipi_cmd.how = type;
    280  1.4  thorpej 	return (scsipi_command(sc_link,
    281  1.3    enami 	    (struct scsipi_generic *) &scsipi_cmd, sizeof(scsipi_cmd),
    282  1.3    enami 	    0, 0, 2, type == SSS_START ? 30000 : 10000, NULL, flags));
    283  1.2   bouyer }
    284  1.2   bouyer 
    285  1.2   bouyer /*
    286  1.3    enami  * This routine is called by the scsipi interrupt when the transfer is
    287  1.3    enami  * complete.
    288  1.2   bouyer  */
    289  1.3    enami void
    290  1.2   bouyer scsipi_done(xs)
    291  1.2   bouyer 	struct scsipi_xfer *xs;
    292  1.2   bouyer {
    293  1.2   bouyer 	struct scsipi_link *sc_link = xs->sc_link;
    294  1.2   bouyer 	struct buf *bp;
    295  1.2   bouyer 	int error;
    296  1.2   bouyer 
    297  1.2   bouyer 	SC_DEBUG(sc_link, SDEV_DB2, ("scsipi_done\n"));
    298  1.2   bouyer #ifdef	SCSIDEBUG
    299  1.2   bouyer 	if ((sc_link->flags & SDEV_DB1) != 0)
    300  1.2   bouyer 		show_scsipi_cmd(xs);
    301  1.2   bouyer #endif /* SCSIDEBUG */
    302  1.2   bouyer 
    303  1.2   bouyer 	/*
    304  1.3    enami 	 * If it's a user level request, bypass all usual completion
    305  1.3    enami 	 * processing, let the user work it out..  We take
    306  1.3    enami 	 * reponsibility for freeing the xs when the user returns.
    307  1.3    enami 	 * (and restarting the device's queue).
    308  1.3    enami 	 */
    309  1.2   bouyer 	if ((xs->flags & SCSI_USER) != 0) {
    310  1.2   bouyer 		SC_DEBUG(sc_link, SDEV_DB3, ("calling user done()\n"));
    311  1.2   bouyer 		scsipi_user_done(xs); /* to take a copy of the sense etc. */
    312  1.2   bouyer 		SC_DEBUG(sc_link, SDEV_DB3, ("returned from user done()\n "));
    313  1.2   bouyer 
    314  1.9   scottr 		/*
    315  1.9   scottr 		 * If this was an asynchronous operation (i.e. adapter
    316  1.9   scottr 		 * returned SUCCESSFULLY_QUEUED when the command was
    317  1.9   scottr 		 * submitted), we need to free the scsipi_xfer here.
    318  1.9   scottr 		 */
    319  1.9   scottr 		if (xs->flags & SCSI_ASYNCREQ)
    320  1.9   scottr 			scsipi_free_xs(xs, SCSI_NOSLEEP);
    321  1.2   bouyer 		SC_DEBUG(sc_link, SDEV_DB3, ("returning to adapter\n"));
    322  1.2   bouyer 		return;
    323  1.2   bouyer 	}
    324  1.2   bouyer 
    325  1.2   bouyer 	if (!((xs->flags & (SCSI_NOSLEEP | SCSI_POLL)) == SCSI_NOSLEEP)) {
    326  1.2   bouyer 		/*
    327  1.2   bouyer 		 * if it's a normal upper level request, then ask
    328  1.2   bouyer 		 * the upper level code to handle error checking
    329  1.2   bouyer 		 * rather than doing it here at interrupt time
    330  1.2   bouyer 		 */
    331  1.2   bouyer 		wakeup(xs);
    332  1.2   bouyer 		return;
    333  1.2   bouyer 	}
    334  1.2   bouyer 
    335  1.2   bouyer 	/*
    336  1.2   bouyer 	 * Go and handle errors now.
    337  1.2   bouyer 	 * If it returns ERESTART then we should RETRY
    338  1.2   bouyer 	 */
    339  1.2   bouyer retry:
    340  1.2   bouyer 	error = sc_err1(xs, 1);
    341  1.3    enami 	if (error == ERESTART)
    342  1.4  thorpej 		switch (scsipi_command_direct(xs)) {
    343  1.2   bouyer 		case SUCCESSFULLY_QUEUED:
    344  1.2   bouyer 			return;
    345  1.2   bouyer 
    346  1.2   bouyer 		case TRY_AGAIN_LATER:
    347  1.2   bouyer 			xs->error = XS_BUSY;
    348  1.2   bouyer 		case COMPLETE:
    349  1.2   bouyer 			goto retry;
    350  1.2   bouyer 		}
    351  1.2   bouyer 
    352  1.2   bouyer 	bp = xs->bp;
    353  1.2   bouyer 	if (bp) {
    354  1.2   bouyer 		if (error) {
    355  1.2   bouyer 			bp->b_error = error;
    356  1.2   bouyer 			bp->b_flags |= B_ERROR;
    357  1.2   bouyer 			bp->b_resid = bp->b_bcount;
    358  1.2   bouyer 		} else {
    359  1.2   bouyer 			bp->b_error = 0;
    360  1.2   bouyer 			bp->b_resid = xs->resid;
    361  1.2   bouyer 		}
    362  1.2   bouyer 	}
    363  1.2   bouyer 	if (sc_link->device->done) {
    364  1.2   bouyer 		/*
    365  1.2   bouyer 		 * Tell the device the operation is actually complete.
    366  1.2   bouyer 		 * No more will happen with this xfer.  This for
    367  1.2   bouyer 		 * notification of the upper-level driver only; they
    368  1.2   bouyer 		 * won't be returning any meaningful information to us.
    369  1.2   bouyer 		 */
    370  1.2   bouyer 		(*sc_link->device->done)(xs);
    371  1.2   bouyer 	}
    372  1.9   scottr 	/*
    373  1.9   scottr 	 * If this was an asynchronous operation (i.e. adapter
    374  1.9   scottr 	 * returned SUCCESSFULLY_QUEUED when the command was
    375  1.9   scottr 	 * submitted), we need to free the scsipi_xfer here.
    376  1.9   scottr 	 */
    377  1.9   scottr 	if (xs->flags & SCSI_ASYNCREQ)
    378  1.9   scottr 		scsipi_free_xs(xs, SCSI_NOSLEEP);
    379  1.2   bouyer 	if (bp)
    380  1.2   bouyer 		biodone(bp);
    381  1.2   bouyer }
    382  1.2   bouyer 
    383  1.2   bouyer int
    384  1.2   bouyer scsipi_execute_xs(xs)
    385  1.2   bouyer 	struct scsipi_xfer *xs;
    386  1.2   bouyer {
    387  1.2   bouyer 	int error;
    388  1.2   bouyer 	int s;
    389  1.2   bouyer 
    390  1.9   scottr 	xs->flags &= ~(ITSDONE|SCSI_ASYNCREQ);
    391  1.2   bouyer 	xs->error = XS_NOERROR;
    392  1.2   bouyer 	xs->resid = xs->datalen;
    393  1.5  thorpej 	xs->status = 0;
    394  1.2   bouyer 
    395  1.2   bouyer retry:
    396  1.2   bouyer 	/*
    397  1.2   bouyer 	 * Do the transfer. If we are polling we will return:
    398  1.2   bouyer 	 * COMPLETE,  Was poll, and scsipi_done has been called
    399  1.2   bouyer 	 * TRY_AGAIN_LATER, Adapter short resources, try again
    400  1.3    enami 	 *
    401  1.2   bouyer 	 * if under full steam (interrupts) it will return:
    402  1.2   bouyer 	 * SUCCESSFULLY_QUEUED, will do a wakeup when complete
    403  1.2   bouyer 	 * TRY_AGAIN_LATER, (as for polling)
    404  1.2   bouyer 	 * After the wakeup, we must still check if it succeeded
    405  1.3    enami 	 *
    406  1.2   bouyer 	 * If we have a SCSI_NOSLEEP (typically because we have a buf)
    407  1.2   bouyer 	 * we just return.  All the error proccessing and the buffer
    408  1.2   bouyer 	 * code both expect us to return straight to them, so as soon
    409  1.2   bouyer 	 * as the command is queued, return.
    410  1.2   bouyer 	 */
    411  1.2   bouyer #ifdef SCSIDEBUG
    412  1.2   bouyer 	if (xs->sc_link->flags & SDEV_DB3) {
    413  1.2   bouyer 		printf("scsipi_exec_cmd: ");
    414  1.2   bouyer 		show_scsipi_xs(xs);
    415  1.2   bouyer 		printf("\n");
    416  1.2   bouyer 	}
    417  1.2   bouyer #endif
    418  1.4  thorpej 	switch (scsipi_command_direct(xs)) {
    419  1.2   bouyer 	case SUCCESSFULLY_QUEUED:
    420  1.9   scottr 		if ((xs->flags & (SCSI_NOSLEEP | SCSI_POLL)) == SCSI_NOSLEEP) {
    421  1.9   scottr 			/*
    422  1.9   scottr 			 * The request will complete asynchronously.  In this
    423  1.9   scottr 			 * case, we need scsipi_done() to free the scsipi_xfer.
    424  1.9   scottr 			 */
    425  1.9   scottr 			xs->flags |= SCSI_ASYNCREQ;
    426  1.3    enami 			return (EJUSTRETURN);
    427  1.9   scottr 		}
    428  1.2   bouyer #ifdef DIAGNOSTIC
    429  1.2   bouyer 		if (xs->flags & SCSI_NOSLEEP)
    430  1.2   bouyer 			panic("scsipi_execute_xs: NOSLEEP and POLL");
    431  1.2   bouyer #endif
    432  1.2   bouyer 		s = splbio();
    433  1.2   bouyer 		while ((xs->flags & ITSDONE) == 0)
    434  1.2   bouyer 			tsleep(xs, PRIBIO + 1, "scsipi_cmd", 0);
    435  1.2   bouyer 		splx(s);
    436  1.2   bouyer 	case COMPLETE:		/* Polling command completed ok */
    437  1.2   bouyer 		if (xs->bp)
    438  1.9   scottr 			return (0);
    439  1.2   bouyer 	doit:
    440  1.2   bouyer 		SC_DEBUG(xs->sc_link, SDEV_DB3, ("back in cmd()\n"));
    441  1.2   bouyer 		if ((error = sc_err1(xs, 0)) != ERESTART)
    442  1.3    enami 			return (error);
    443  1.2   bouyer 		goto retry;
    444  1.2   bouyer 
    445  1.2   bouyer 	case TRY_AGAIN_LATER:	/* adapter resource shortage */
    446  1.2   bouyer 		xs->error = XS_BUSY;
    447  1.2   bouyer 		goto doit;
    448  1.2   bouyer 
    449  1.2   bouyer 	default:
    450  1.2   bouyer 		panic("scsipi_execute_xs: invalid return code");
    451  1.2   bouyer 	}
    452  1.2   bouyer 
    453  1.2   bouyer #ifdef DIAGNOSTIC
    454  1.2   bouyer 	panic("scsipi_execute_xs: impossible");
    455  1.2   bouyer #endif
    456  1.3    enami 	return (EINVAL);
    457  1.2   bouyer }
    458  1.2   bouyer 
    459  1.3    enami int
    460  1.2   bouyer sc_err1(xs, async)
    461  1.2   bouyer 	struct scsipi_xfer *xs;
    462  1.2   bouyer 	int async;
    463  1.2   bouyer {
    464  1.2   bouyer 	int error;
    465  1.2   bouyer 
    466  1.2   bouyer 	SC_DEBUG(xs->sc_link, SDEV_DB3, ("sc_err1,err = 0x%x \n", xs->error));
    467  1.2   bouyer 
    468  1.2   bouyer 	/*
    469  1.2   bouyer 	 * If it has a buf, we might be working with
    470  1.2   bouyer 	 * a request from the buffer cache or some other
    471  1.2   bouyer 	 * piece of code that requires us to process
    472  1.2   bouyer 	 * errors at inetrrupt time. We have probably
    473  1.2   bouyer 	 * been called by scsipi_done()
    474  1.2   bouyer 	 */
    475  1.2   bouyer 	switch (xs->error) {
    476  1.2   bouyer 	case XS_NOERROR:	/* nearly always hit this one */
    477  1.2   bouyer 		error = 0;
    478  1.2   bouyer 		break;
    479  1.2   bouyer 
    480  1.2   bouyer 	case XS_SENSE:
    481  1.3    enami 		if ((error = (*xs->sc_link->scsipi_interpret_sense)(xs)) ==
    482  1.3    enami 		    ERESTART)
    483  1.2   bouyer 			goto retry;
    484  1.2   bouyer 		SC_DEBUG(xs->sc_link, SDEV_DB3,
    485  1.2   bouyer 		    ("scsipi_interpret_sense returned %d\n", error));
    486  1.2   bouyer 		break;
    487  1.2   bouyer 
    488  1.2   bouyer 	case XS_BUSY:
    489  1.2   bouyer 		if (xs->retries) {
    490  1.2   bouyer 			if ((xs->flags & SCSI_POLL) != 0)
    491  1.2   bouyer 				delay(1000000);
    492  1.2   bouyer 			else if ((xs->flags & SCSI_NOSLEEP) == 0)
    493  1.2   bouyer 				tsleep(&lbolt, PRIBIO, "scbusy", 0);
    494  1.2   bouyer 			else
    495  1.2   bouyer #if 0
    496  1.2   bouyer 				timeout(scsipi_requeue, xs, hz);
    497  1.2   bouyer #else
    498  1.2   bouyer 				goto lose;
    499  1.2   bouyer #endif
    500  1.2   bouyer 		}
    501  1.2   bouyer 	case XS_TIMEOUT:
    502  1.2   bouyer 	retry:
    503  1.2   bouyer 		if (xs->retries--) {
    504  1.2   bouyer 			xs->error = XS_NOERROR;
    505  1.2   bouyer 			xs->flags &= ~ITSDONE;
    506  1.3    enami 			return (ERESTART);
    507  1.2   bouyer 		}
    508  1.2   bouyer 	case XS_DRIVER_STUFFUP:
    509  1.2   bouyer 	lose:
    510  1.2   bouyer 		error = EIO;
    511  1.2   bouyer 		break;
    512  1.2   bouyer 
    513  1.2   bouyer 	case XS_SELTIMEOUT:
    514  1.2   bouyer 		/* XXX Disable device? */
    515  1.2   bouyer 		error = EIO;
    516  1.2   bouyer 		break;
    517  1.2   bouyer 
    518  1.2   bouyer 	default:
    519  1.3    enami 		(*xs->sc_link->sc_print_addr)(xs->sc_link);
    520  1.2   bouyer 		printf("unknown error category from scsipi driver\n");
    521  1.2   bouyer 		error = EIO;
    522  1.2   bouyer 		break;
    523  1.2   bouyer 	}
    524  1.2   bouyer 
    525  1.3    enami 	return (error);
    526  1.2   bouyer }
    527  1.2   bouyer 
    528  1.2   bouyer #ifdef	SCSIDEBUG
    529  1.2   bouyer /*
    530  1.2   bouyer  * Given a scsipi_xfer, dump the request, in all it's glory
    531  1.2   bouyer  */
    532  1.2   bouyer void
    533  1.2   bouyer show_scsipi_xs(xs)
    534  1.2   bouyer 	struct scsipi_xfer *xs;
    535  1.2   bouyer {
    536  1.3    enami 
    537  1.2   bouyer 	printf("xs(%p): ", xs);
    538  1.2   bouyer 	printf("flg(0x%x)", xs->flags);
    539  1.2   bouyer 	printf("sc_link(%p)", xs->sc_link);
    540  1.2   bouyer 	printf("retr(0x%x)", xs->retries);
    541  1.2   bouyer 	printf("timo(0x%x)", xs->timeout);
    542  1.2   bouyer 	printf("cmd(%p)", xs->cmd);
    543  1.2   bouyer 	printf("len(0x%x)", xs->cmdlen);
    544  1.2   bouyer 	printf("data(%p)", xs->data);
    545  1.2   bouyer 	printf("len(0x%x)", xs->datalen);
    546  1.2   bouyer 	printf("res(0x%x)", xs->resid);
    547  1.2   bouyer 	printf("err(0x%x)", xs->error);
    548  1.2   bouyer 	printf("bp(%p)", xs->bp);
    549  1.2   bouyer 	show_scsipi_cmd(xs);
    550  1.2   bouyer }
    551  1.2   bouyer 
    552  1.2   bouyer void
    553  1.2   bouyer show_scsipi_cmd(xs)
    554  1.2   bouyer 	struct scsipi_xfer *xs;
    555  1.2   bouyer {
    556  1.2   bouyer 	u_char *b = (u_char *) xs->cmd;
    557  1.3    enami 	int i = 0;
    558  1.2   bouyer 
    559  1.3    enami 	(*xs->sc_link->sc_print_addr)(xs->sc_link);
    560  1.2   bouyer 	printf("command: ");
    561  1.2   bouyer 
    562  1.2   bouyer 	if ((xs->flags & SCSI_RESET) == 0) {
    563  1.2   bouyer 		while (i < xs->cmdlen) {
    564  1.2   bouyer 			if (i)
    565  1.2   bouyer 				printf(",");
    566  1.2   bouyer 			printf("0x%x", b[i++]);
    567  1.2   bouyer 		}
    568  1.2   bouyer 		printf("-[%d bytes]\n", xs->datalen);
    569  1.2   bouyer 		if (xs->datalen)
    570  1.2   bouyer 			show_mem(xs->data, min(64, xs->datalen));
    571  1.2   bouyer 	} else
    572  1.2   bouyer 		printf("-RESET-\n");
    573  1.2   bouyer }
    574  1.2   bouyer 
    575  1.2   bouyer void
    576  1.2   bouyer show_mem(address, num)
    577  1.2   bouyer 	u_char *address;
    578  1.2   bouyer 	int num;
    579  1.2   bouyer {
    580  1.2   bouyer 	int x;
    581  1.2   bouyer 
    582  1.2   bouyer 	printf("------------------------------");
    583  1.2   bouyer 	for (x = 0; x < num; x++) {
    584  1.2   bouyer 		if ((x % 16) == 0)
    585  1.2   bouyer 			printf("\n%03d: ", x);
    586  1.2   bouyer 		printf("%02x ", *address++);
    587  1.2   bouyer 	}
    588  1.2   bouyer 	printf("\n------------------------------\n");
    589  1.2   bouyer }
    590  1.2   bouyer #endif /*SCSIDEBUG */
    591  1.2   bouyer 
    592