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