Home | History | Annotate | Line # | Download | only in scsipi
scsi_base.c revision 1.46
      1  1.46    mjacob /*	$NetBSD: scsi_base.c,v 1.46 1997/08/20 18:19:12 mjacob Exp $	*/
      2  1.14       cgd 
      3   1.1   mycroft /*
      4  1.43   mycroft  * Copyright (c) 1994, 1995, 1997 Charles M. Hannum.  All rights reserved.
      5   1.9   mycroft  *
      6   1.9   mycroft  * Redistribution and use in source and binary forms, with or without
      7   1.9   mycroft  * modification, are permitted provided that the following conditions
      8   1.9   mycroft  * are met:
      9   1.9   mycroft  * 1. Redistributions of source code must retain the above copyright
     10   1.9   mycroft  *    notice, this list of conditions and the following disclaimer.
     11   1.9   mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     12   1.9   mycroft  *    notice, this list of conditions and the following disclaimer in the
     13   1.9   mycroft  *    documentation and/or other materials provided with the distribution.
     14   1.9   mycroft  * 3. All advertising materials mentioning features or use of this software
     15   1.9   mycroft  *    must display the following acknowledgement:
     16  1.43   mycroft  *	This product includes software developed by Charles M. Hannum.
     17   1.9   mycroft  * 4. The name of the author may not be used to endorse or promote products
     18   1.9   mycroft  *    derived from this software without specific prior written permission.
     19   1.9   mycroft  *
     20   1.9   mycroft  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21   1.9   mycroft  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22   1.9   mycroft  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23   1.9   mycroft  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24   1.9   mycroft  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25   1.9   mycroft  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26   1.9   mycroft  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27   1.9   mycroft  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28   1.9   mycroft  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29   1.9   mycroft  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30   1.9   mycroft  */
     31   1.9   mycroft 
     32   1.9   mycroft /*
     33  1.46    mjacob  * Additions for detail SCSI error printing are
     34  1.46    mjacob  * Copyright (c) 1997 by Matthew Jacob.
     35  1.46    mjacob  */
     36  1.46    mjacob 
     37  1.46    mjacob /*
     38   1.9   mycroft  * Originally written by Julian Elischer (julian (at) dialix.oz.au)
     39   1.1   mycroft  */
     40   1.1   mycroft 
     41   1.1   mycroft #include <sys/types.h>
     42   1.1   mycroft #include <sys/param.h>
     43  1.30   thorpej #include <sys/systm.h>
     44  1.11   mycroft #include <sys/kernel.h>
     45   1.1   mycroft #include <sys/buf.h>
     46   1.1   mycroft #include <sys/uio.h>
     47   1.1   mycroft #include <sys/malloc.h>
     48   1.1   mycroft #include <sys/errno.h>
     49   1.1   mycroft #include <sys/device.h>
     50  1.33  christos #include <sys/proc.h>
     51   1.2   mycroft 
     52   1.1   mycroft #include <scsi/scsi_all.h>
     53   1.1   mycroft #include <scsi/scsi_disk.h>
     54   1.1   mycroft #include <scsi/scsiconf.h>
     55   1.1   mycroft 
     56  1.12   mycroft LIST_HEAD(xs_free_list, scsi_xfer) xs_free_list;
     57   1.1   mycroft 
     58  1.33  christos static __inline struct scsi_xfer *scsi_make_xs __P((struct scsi_link *,
     59  1.33  christos 						    struct scsi_generic *,
     60  1.33  christos 						    int cmdlen,
     61  1.33  christos 						    u_char *data_addr,
     62  1.33  christos 						    int datalen,
     63  1.33  christos 						    int retries,
     64  1.33  christos 						    int timeout,
     65  1.33  christos 						    struct buf *,
     66  1.33  christos 						    int flags));
     67  1.33  christos 
     68  1.33  christos int sc_err1 __P((struct scsi_xfer *, int));
     69  1.33  christos int scsi_interpret_sense __P((struct scsi_xfer *));
     70  1.46    mjacob #ifdef	SCSIVERBOSE
     71  1.46    mjacob char *scsi_decode_sense __P((void *, int));
     72  1.46    mjacob #endif
     73  1.33  christos 
     74   1.1   mycroft /*
     75   1.1   mycroft  * Get a scsi transfer structure for the caller. Charge the structure
     76   1.1   mycroft  * to the device that is referenced by the sc_link structure. If the
     77   1.1   mycroft  * sc_link structure has no 'credits' then the device already has the
     78   1.1   mycroft  * maximum number or outstanding operations under way. In this stage,
     79   1.1   mycroft  * wait on the structure so that when one is freed, we are awoken again
     80   1.1   mycroft  * If the SCSI_NOSLEEP flag is set, then do not wait, but rather, return
     81   1.1   mycroft  * a NULL pointer, signifying that no slots were available
     82   1.1   mycroft  * Note in the link structure, that we are waiting on it.
     83   1.1   mycroft  */
     84   1.1   mycroft 
     85   1.1   mycroft struct scsi_xfer *
     86  1.22   mycroft scsi_get_xs(sc_link, flags)
     87   1.2   mycroft 	struct scsi_link *sc_link;	/* who to charge the xs to */
     88  1.11   mycroft 	int flags;			/* if this call can sleep */
     89   1.1   mycroft {
     90   1.2   mycroft 	struct scsi_xfer *xs;
     91  1.11   mycroft 	int s;
     92   1.1   mycroft 
     93  1.22   mycroft 	SC_DEBUG(sc_link, SDEV_DB3, ("scsi_get_xs\n"));
     94   1.1   mycroft 	s = splbio();
     95  1.22   mycroft 	while (sc_link->openings <= 0) {
     96   1.1   mycroft 		SC_DEBUG(sc_link, SDEV_DB3, ("sleeping\n"));
     97  1.22   mycroft 		if ((flags & SCSI_NOSLEEP) != 0) {
     98   1.1   mycroft 			splx(s);
     99   1.1   mycroft 			return 0;
    100   1.1   mycroft 		}
    101   1.1   mycroft 		sc_link->flags |= SDEV_WAITING;
    102  1.12   mycroft 		(void) tsleep(sc_link, PRIBIO, "getxs", 0);
    103   1.1   mycroft 	}
    104  1.22   mycroft 	sc_link->openings--;
    105  1.33  christos 	if ((xs = xs_free_list.lh_first) != NULL) {
    106  1.12   mycroft 		LIST_REMOVE(xs, free_list);
    107   1.1   mycroft 		splx(s);
    108   1.1   mycroft 	} else {
    109   1.1   mycroft 		splx(s);
    110   1.1   mycroft 		SC_DEBUG(sc_link, SDEV_DB3, ("making\n"));
    111  1.12   mycroft 		xs = malloc(sizeof(*xs), M_DEVBUF,
    112  1.22   mycroft 		    ((flags & SCSI_NOSLEEP) != 0 ? M_NOWAIT : M_WAITOK));
    113  1.12   mycroft 		if (!xs) {
    114   1.1   mycroft 			sc_print_addr(sc_link);
    115  1.40  christos 			printf("cannot allocate scsi xs\n");
    116  1.12   mycroft 			return 0;
    117   1.1   mycroft 		}
    118   1.1   mycroft 	}
    119  1.22   mycroft 
    120   1.1   mycroft 	SC_DEBUG(sc_link, SDEV_DB3, ("returning\n"));
    121  1.22   mycroft 	xs->flags = INUSE | flags;
    122  1.11   mycroft 	return xs;
    123   1.1   mycroft }
    124   1.1   mycroft 
    125   1.1   mycroft /*
    126   1.1   mycroft  * Given a scsi_xfer struct, and a device (referenced through sc_link)
    127   1.1   mycroft  * return the struct to the free pool and credit the device with it
    128   1.1   mycroft  * If another process is waiting for an xs, do a wakeup, let it proceed
    129   1.1   mycroft  */
    130   1.1   mycroft void
    131  1.22   mycroft scsi_free_xs(xs, flags)
    132   1.1   mycroft 	struct scsi_xfer *xs;
    133  1.11   mycroft 	int flags;
    134   1.1   mycroft {
    135  1.22   mycroft 	struct scsi_link *sc_link = xs->sc_link;
    136  1.22   mycroft 
    137  1.22   mycroft 	xs->flags &= ~INUSE;
    138  1.12   mycroft 	LIST_INSERT_HEAD(&xs_free_list, xs, free_list);
    139   1.1   mycroft 
    140  1.22   mycroft 	SC_DEBUG(sc_link, SDEV_DB3, ("scsi_free_xs\n"));
    141   1.1   mycroft 	/* if was 0 and someone waits, wake them up */
    142  1.22   mycroft 	sc_link->openings++;
    143  1.22   mycroft 	if ((sc_link->flags & SDEV_WAITING) != 0) {
    144   1.3   mycroft 		sc_link->flags &= ~SDEV_WAITING;
    145  1.22   mycroft 		wakeup(sc_link);
    146   1.3   mycroft 	} else {
    147   1.1   mycroft 		if (sc_link->device->start) {
    148   1.1   mycroft 			SC_DEBUG(sc_link, SDEV_DB2, ("calling private start()\n"));
    149  1.17   mycroft 			(*(sc_link->device->start)) (sc_link->device_softc);
    150   1.1   mycroft 		}
    151   1.3   mycroft 	}
    152   1.1   mycroft }
    153   1.1   mycroft 
    154   1.1   mycroft /*
    155  1.22   mycroft  * Make a scsi_xfer, and return a pointer to it.
    156  1.22   mycroft  */
    157  1.22   mycroft static __inline struct scsi_xfer *
    158  1.22   mycroft scsi_make_xs(sc_link, scsi_cmd, cmdlen, data_addr, datalen,
    159  1.33  christos 	     retries, timeout, bp, flags)
    160  1.22   mycroft 	struct scsi_link *sc_link;
    161  1.22   mycroft 	struct scsi_generic *scsi_cmd;
    162  1.22   mycroft 	int cmdlen;
    163  1.22   mycroft 	u_char *data_addr;
    164  1.22   mycroft 	int datalen;
    165  1.22   mycroft 	int retries;
    166  1.22   mycroft 	int timeout;
    167  1.22   mycroft 	struct buf *bp;
    168  1.22   mycroft 	int flags;
    169  1.22   mycroft {
    170  1.22   mycroft 	struct scsi_xfer *xs;
    171  1.22   mycroft 
    172  1.22   mycroft 	if ((xs = scsi_get_xs(sc_link, flags)) == NULL)
    173  1.22   mycroft 		return NULL;
    174  1.22   mycroft 
    175  1.22   mycroft 	/*
    176  1.22   mycroft 	 * Fill out the scsi_xfer structure.  We don't know whose context
    177  1.22   mycroft 	 * the cmd is in, so copy it.
    178  1.22   mycroft 	 */
    179  1.22   mycroft 	xs->sc_link = sc_link;
    180  1.22   mycroft 	bcopy(scsi_cmd, &xs->cmdstore, cmdlen);
    181  1.22   mycroft 	xs->cmd = &xs->cmdstore;
    182  1.22   mycroft 	xs->cmdlen = cmdlen;
    183  1.22   mycroft 	xs->data = data_addr;
    184  1.22   mycroft 	xs->datalen = datalen;
    185  1.22   mycroft 	xs->retries = retries;
    186  1.22   mycroft 	xs->timeout = timeout;
    187  1.22   mycroft 	xs->bp = bp;
    188  1.38   thorpej 
    189  1.38   thorpej 	/*
    190  1.38   thorpej 	 * Set the LUN in the CDB if we have an older device.  We also
    191  1.38   thorpej 	 * set it for more modern SCSI-II devices "just in case".
    192  1.38   thorpej 	 */
    193  1.38   thorpej 	if ((sc_link->scsi_version & SID_ANSII) <= 2)
    194  1.38   thorpej 		xs->cmd->bytes[0] |=
    195  1.38   thorpej 		    ((sc_link->lun << SCSI_CMD_LUN_SHIFT) & SCSI_CMD_LUN_MASK);
    196  1.22   mycroft 
    197  1.22   mycroft 	return xs;
    198  1.22   mycroft }
    199  1.22   mycroft 
    200  1.22   mycroft /*
    201   1.1   mycroft  * Find out from the device what its capacity is.
    202   1.1   mycroft  */
    203  1.22   mycroft u_long
    204   1.1   mycroft scsi_size(sc_link, flags)
    205   1.1   mycroft 	struct scsi_link *sc_link;
    206  1.11   mycroft 	int flags;
    207   1.1   mycroft {
    208   1.1   mycroft 	struct scsi_read_cap_data rdcap;
    209   1.1   mycroft 	struct scsi_read_capacity scsi_cmd;
    210   1.1   mycroft 
    211   1.1   mycroft 	/*
    212   1.1   mycroft 	 * make up a scsi command and ask the scsi driver to do
    213   1.1   mycroft 	 * it for you.
    214   1.1   mycroft 	 */
    215   1.1   mycroft 	bzero(&scsi_cmd, sizeof(scsi_cmd));
    216  1.22   mycroft 	scsi_cmd.opcode = READ_CAPACITY;
    217   1.1   mycroft 
    218   1.1   mycroft 	/*
    219   1.1   mycroft 	 * If the command works, interpret the result as a 4 byte
    220   1.1   mycroft 	 * number of blocks
    221   1.1   mycroft 	 */
    222  1.22   mycroft 	if (scsi_scsi_cmd(sc_link, (struct scsi_generic *)&scsi_cmd,
    223  1.22   mycroft 			  sizeof(scsi_cmd), (u_char *)&rdcap, sizeof(rdcap),
    224   1.2   mycroft 			  2, 20000, NULL, flags | SCSI_DATA_IN) != 0) {
    225   1.1   mycroft 		sc_print_addr(sc_link);
    226  1.40  christos 		printf("could not get size\n");
    227  1.11   mycroft 		return 0;
    228   1.1   mycroft 	}
    229  1.34   mycroft 
    230  1.34   mycroft 	return _4btol(rdcap.addr) + 1;
    231   1.1   mycroft }
    232   1.1   mycroft 
    233   1.1   mycroft /*
    234   1.1   mycroft  * Get scsi driver to send a "are you ready?" command
    235   1.1   mycroft  */
    236   1.2   mycroft int
    237   1.1   mycroft scsi_test_unit_ready(sc_link, flags)
    238   1.1   mycroft 	struct scsi_link *sc_link;
    239  1.11   mycroft 	int flags;
    240   1.1   mycroft {
    241   1.1   mycroft 	struct scsi_test_unit_ready scsi_cmd;
    242   1.1   mycroft 
    243   1.1   mycroft 	bzero(&scsi_cmd, sizeof(scsi_cmd));
    244  1.22   mycroft 	scsi_cmd.opcode = TEST_UNIT_READY;
    245   1.1   mycroft 
    246   1.2   mycroft 	return scsi_scsi_cmd(sc_link, (struct scsi_generic *) &scsi_cmd,
    247  1.22   mycroft 			     sizeof(scsi_cmd), 0, 0, 2, 10000, NULL, flags);
    248   1.1   mycroft }
    249   1.1   mycroft 
    250   1.1   mycroft /*
    251   1.1   mycroft  * Do a scsi operation, asking a device to run as SCSI-II if it can.
    252   1.1   mycroft  */
    253   1.2   mycroft int
    254   1.1   mycroft scsi_change_def(sc_link, flags)
    255   1.1   mycroft 	struct scsi_link *sc_link;
    256  1.11   mycroft 	int flags;
    257   1.1   mycroft {
    258   1.1   mycroft 	struct scsi_changedef scsi_cmd;
    259   1.1   mycroft 
    260   1.1   mycroft 	bzero(&scsi_cmd, sizeof(scsi_cmd));
    261  1.22   mycroft 	scsi_cmd.opcode = CHANGE_DEFINITION;
    262   1.1   mycroft 	scsi_cmd.how = SC_SCSI_2;
    263   1.1   mycroft 
    264   1.2   mycroft 	return scsi_scsi_cmd(sc_link, (struct scsi_generic *) &scsi_cmd,
    265   1.2   mycroft 			     sizeof(scsi_cmd), 0, 0, 2, 100000, NULL, flags);
    266   1.1   mycroft }
    267   1.1   mycroft 
    268   1.1   mycroft /*
    269   1.1   mycroft  * Do a scsi operation asking a device what it is
    270   1.1   mycroft  * Use the scsi_cmd routine in the switch table.
    271   1.1   mycroft  */
    272   1.2   mycroft int
    273   1.1   mycroft scsi_inquire(sc_link, inqbuf, flags)
    274   1.1   mycroft 	struct scsi_link *sc_link;
    275   1.1   mycroft 	struct scsi_inquiry_data *inqbuf;
    276  1.11   mycroft 	int flags;
    277   1.1   mycroft {
    278   1.1   mycroft 	struct scsi_inquiry scsi_cmd;
    279   1.1   mycroft 
    280   1.1   mycroft 	bzero(&scsi_cmd, sizeof(scsi_cmd));
    281  1.22   mycroft 	scsi_cmd.opcode = INQUIRY;
    282  1.42   thorpej 	scsi_cmd.length = sizeof(struct scsi_inquiry_data);
    283   1.1   mycroft 
    284  1.42   thorpej 	return scsi_scsi_cmd(sc_link, (struct scsi_generic *) &scsi_cmd,
    285  1.42   thorpej 			     sizeof(scsi_cmd), (u_char *) inqbuf,
    286  1.42   thorpej 			     sizeof(struct scsi_inquiry_data), 2, 10000, NULL,
    287  1.42   thorpej 			     SCSI_DATA_IN | flags);
    288   1.1   mycroft }
    289   1.1   mycroft 
    290   1.1   mycroft /*
    291   1.1   mycroft  * Prevent or allow the user to remove the media
    292   1.1   mycroft  */
    293   1.2   mycroft int
    294   1.1   mycroft scsi_prevent(sc_link, type, flags)
    295   1.1   mycroft 	struct scsi_link *sc_link;
    296  1.11   mycroft 	int type, flags;
    297   1.1   mycroft {
    298   1.1   mycroft 	struct scsi_prevent scsi_cmd;
    299   1.1   mycroft 
    300   1.1   mycroft 	bzero(&scsi_cmd, sizeof(scsi_cmd));
    301  1.22   mycroft 	scsi_cmd.opcode = PREVENT_ALLOW;
    302   1.1   mycroft 	scsi_cmd.how = type;
    303   1.2   mycroft 	return scsi_scsi_cmd(sc_link, (struct scsi_generic *) &scsi_cmd,
    304   1.2   mycroft 			     sizeof(scsi_cmd), 0, 0, 2, 5000, NULL, flags);
    305   1.1   mycroft }
    306   1.1   mycroft 
    307   1.1   mycroft /*
    308   1.1   mycroft  * Get scsi driver to send a "start up" command
    309   1.1   mycroft  */
    310   1.2   mycroft int
    311  1.10   mycroft scsi_start(sc_link, type, flags)
    312   1.1   mycroft 	struct scsi_link *sc_link;
    313  1.11   mycroft 	int type, flags;
    314   1.1   mycroft {
    315   1.1   mycroft 	struct scsi_start_stop scsi_cmd;
    316   1.1   mycroft 
    317   1.1   mycroft 	bzero(&scsi_cmd, sizeof(scsi_cmd));
    318  1.22   mycroft 	scsi_cmd.opcode = START_STOP;
    319  1.22   mycroft 	scsi_cmd.byte2 = 0x00;
    320  1.10   mycroft 	scsi_cmd.how = type;
    321   1.2   mycroft 	return scsi_scsi_cmd(sc_link, (struct scsi_generic *) &scsi_cmd,
    322  1.10   mycroft 			     sizeof(scsi_cmd), 0, 0, 2,
    323  1.22   mycroft 			     type == SSS_START ? 30000 : 10000, NULL, flags);
    324   1.1   mycroft }
    325   1.1   mycroft 
    326   1.1   mycroft /*
    327   1.1   mycroft  * This routine is called by the scsi interrupt when the transfer is complete.
    328   1.1   mycroft  */
    329   1.1   mycroft void
    330   1.1   mycroft scsi_done(xs)
    331   1.1   mycroft 	struct scsi_xfer *xs;
    332   1.1   mycroft {
    333   1.1   mycroft 	struct scsi_link *sc_link = xs->sc_link;
    334  1.43   mycroft 	struct buf *bp;
    335  1.11   mycroft 	int error;
    336   1.1   mycroft 
    337   1.1   mycroft 	SC_DEBUG(sc_link, SDEV_DB2, ("scsi_done\n"));
    338   1.1   mycroft #ifdef	SCSIDEBUG
    339  1.22   mycroft 	if ((sc_link->flags & SDEV_DB1) != 0)
    340   1.1   mycroft 		show_scsi_cmd(xs);
    341  1.22   mycroft #endif /* SCSIDEBUG */
    342  1.22   mycroft 
    343   1.1   mycroft 	/*
    344   1.1   mycroft  	 * If it's a user level request, bypass all usual completion processing,
    345   1.1   mycroft  	 * let the user work it out.. We take reponsibility for freeing the
    346   1.1   mycroft  	 * xs when the user returns. (and restarting the device's queue).
    347   1.1   mycroft  	 */
    348  1.22   mycroft 	if ((xs->flags & SCSI_USER) != 0) {
    349   1.1   mycroft 		SC_DEBUG(sc_link, SDEV_DB3, ("calling user done()\n"));
    350   1.1   mycroft 		scsi_user_done(xs); /* to take a copy of the sense etc. */
    351   1.1   mycroft 		SC_DEBUG(sc_link, SDEV_DB3, ("returned from user done()\n "));
    352  1.19   mycroft 
    353  1.22   mycroft 		scsi_free_xs(xs, SCSI_NOSLEEP); /* restarts queue too */
    354   1.1   mycroft 		SC_DEBUG(sc_link, SDEV_DB3, ("returning to adapter\n"));
    355   1.1   mycroft 		return;
    356   1.1   mycroft 	}
    357  1.22   mycroft 
    358  1.41   thorpej 	if (!((xs->flags & (SCSI_NOSLEEP | SCSI_POLL)) == SCSI_NOSLEEP)) {
    359   1.1   mycroft 		/*
    360   1.1   mycroft 		 * if it's a normal upper level request, then ask
    361   1.1   mycroft 		 * the upper level code to handle error checking
    362   1.1   mycroft 		 * rather than doing it here at interrupt time
    363   1.1   mycroft 		 */
    364   1.1   mycroft 		wakeup(xs);
    365   1.1   mycroft 		return;
    366   1.1   mycroft 	}
    367  1.43   mycroft 
    368   1.1   mycroft 	/*
    369   1.1   mycroft 	 * Go and handle errors now.
    370  1.22   mycroft 	 * If it returns ERESTART then we should RETRY
    371   1.1   mycroft 	 */
    372  1.22   mycroft retry:
    373  1.43   mycroft 	error = sc_err1(xs, 1);
    374  1.43   mycroft 	if (error == ERESTART) {
    375  1.22   mycroft 		switch ((*(sc_link->adapter->scsi_cmd)) (xs)) {
    376  1.22   mycroft 		case SUCCESSFULLY_QUEUED:
    377   1.1   mycroft 			return;
    378  1.22   mycroft 
    379  1.22   mycroft 		case TRY_AGAIN_LATER:
    380  1.22   mycroft 			xs->error = XS_BUSY;
    381  1.22   mycroft 		case COMPLETE:
    382  1.22   mycroft 			goto retry;
    383  1.22   mycroft 		}
    384   1.1   mycroft 	}
    385  1.43   mycroft 
    386  1.43   mycroft 	bp = xs->bp;
    387  1.43   mycroft 	if (bp) {
    388  1.43   mycroft 		if (error) {
    389  1.43   mycroft 			bp->b_error = error;
    390  1.43   mycroft 			bp->b_flags |= B_ERROR;
    391  1.43   mycroft 			bp->b_resid = bp->b_bcount;
    392  1.43   mycroft 		} else {
    393  1.43   mycroft 			bp->b_error = 0;
    394  1.43   mycroft 			bp->b_resid = xs->resid;
    395  1.43   mycroft 		}
    396  1.43   mycroft 	}
    397  1.31   thorpej 	if (sc_link->device->done) {
    398  1.31   thorpej 		/*
    399  1.31   thorpej 		 * Tell the device the operation is actually complete.
    400  1.31   thorpej 		 * No more will happen with this xfer.  This for
    401  1.31   thorpej 		 * notification of the upper-level driver only; they
    402  1.31   thorpej 		 * won't be returning any meaningful information to us.
    403  1.31   thorpej 		 */
    404  1.43   mycroft 		(*sc_link->device->done)(xs);
    405  1.31   thorpej 	}
    406  1.22   mycroft 	scsi_free_xs(xs, SCSI_NOSLEEP);
    407  1.43   mycroft 	if (bp)
    408  1.43   mycroft 		biodone(bp);
    409   1.1   mycroft }
    410   1.1   mycroft 
    411  1.22   mycroft int
    412  1.22   mycroft scsi_execute_xs(xs)
    413  1.22   mycroft 	struct scsi_xfer *xs;
    414   1.1   mycroft {
    415  1.11   mycroft 	int error;
    416  1.11   mycroft 	int s;
    417   1.1   mycroft 
    418  1.22   mycroft 	xs->flags &= ~ITSDONE;
    419  1.22   mycroft 	xs->error = XS_NOERROR;
    420  1.22   mycroft 	xs->resid = xs->datalen;
    421   1.1   mycroft 
    422   1.1   mycroft retry:
    423   1.1   mycroft 	/*
    424   1.1   mycroft 	 * Do the transfer. If we are polling we will return:
    425   1.1   mycroft 	 * COMPLETE,  Was poll, and scsi_done has been called
    426   1.1   mycroft 	 * TRY_AGAIN_LATER, Adapter short resources, try again
    427   1.1   mycroft 	 *
    428   1.1   mycroft 	 * if under full steam (interrupts) it will return:
    429   1.1   mycroft 	 * SUCCESSFULLY_QUEUED, will do a wakeup when complete
    430   1.1   mycroft 	 * TRY_AGAIN_LATER, (as for polling)
    431   1.1   mycroft 	 * After the wakeup, we must still check if it succeeded
    432   1.1   mycroft 	 *
    433  1.41   thorpej 	 * If we have a SCSI_NOSLEEP (typically because we have a buf)
    434  1.41   thorpej 	 * we just return.  All the error proccessing and the buffer
    435  1.41   thorpej 	 * code both expect us to return straight to them, so as soon
    436  1.41   thorpej 	 * as the command is queued, return.
    437   1.1   mycroft 	 */
    438  1.22   mycroft 	switch ((*(xs->sc_link->adapter->scsi_cmd)) (xs)) {
    439   1.1   mycroft 	case SUCCESSFULLY_QUEUED:
    440  1.41   thorpej 		if ((xs->flags & (SCSI_NOSLEEP | SCSI_POLL)) == SCSI_NOSLEEP)
    441  1.22   mycroft 			return EJUSTRETURN;
    442  1.41   thorpej #ifdef DIAGNOSTIC
    443  1.41   thorpej 		if (xs->flags & SCSI_NOSLEEP)
    444  1.41   thorpej 			panic("scsi_execute_xs: NOSLEEP and POLL");
    445  1.41   thorpej #endif
    446   1.1   mycroft 		s = splbio();
    447  1.22   mycroft 		while ((xs->flags & ITSDONE) == 0)
    448   1.4   mycroft 			tsleep(xs, PRIBIO + 1, "scsi_scsi_cmd", 0);
    449   1.1   mycroft 		splx(s);
    450   1.1   mycroft 	case COMPLETE:		/* Polling command completed ok */
    451  1.22   mycroft 		if (xs->bp)
    452  1.22   mycroft 			return EJUSTRETURN;
    453  1.22   mycroft 	doit:
    454  1.24   mycroft 		SC_DEBUG(xs->sc_link, SDEV_DB3, ("back in cmd()\n"));
    455  1.22   mycroft 		if ((error = sc_err1(xs, 0)) != ERESTART)
    456  1.22   mycroft 			return error;
    457  1.22   mycroft 		goto retry;
    458   1.1   mycroft 
    459   1.1   mycroft 	case TRY_AGAIN_LATER:	/* adapter resource shortage */
    460  1.22   mycroft 		xs->error = XS_BUSY;
    461  1.22   mycroft 		goto doit;
    462  1.22   mycroft 
    463   1.1   mycroft 	default:
    464  1.22   mycroft 		panic("scsi_execute_xs: invalid return code");
    465   1.1   mycroft 	}
    466  1.22   mycroft 
    467  1.22   mycroft #ifdef DIAGNOSTIC
    468  1.22   mycroft 	panic("scsi_execute_xs: impossible");
    469  1.22   mycroft #endif
    470  1.33  christos 	return EINVAL;
    471  1.22   mycroft }
    472  1.22   mycroft 
    473  1.22   mycroft /*
    474  1.22   mycroft  * ask the scsi driver to perform a command for us.
    475  1.22   mycroft  * tell it where to read/write the data, and how
    476  1.22   mycroft  * long the data is supposed to be. If we have  a buf
    477  1.22   mycroft  * to associate with the transfer, we need that too.
    478  1.22   mycroft  */
    479  1.22   mycroft int
    480  1.22   mycroft scsi_scsi_cmd(sc_link, scsi_cmd, cmdlen, data_addr, datalen,
    481  1.22   mycroft     retries, timeout, bp, flags)
    482  1.22   mycroft 	struct scsi_link *sc_link;
    483  1.22   mycroft 	struct scsi_generic *scsi_cmd;
    484  1.22   mycroft 	int cmdlen;
    485  1.22   mycroft 	u_char *data_addr;
    486  1.22   mycroft 	int datalen;
    487  1.22   mycroft 	int retries;
    488  1.22   mycroft 	int timeout;
    489  1.22   mycroft 	struct buf *bp;
    490  1.22   mycroft 	int flags;
    491  1.22   mycroft {
    492  1.22   mycroft 	struct scsi_xfer *xs;
    493  1.22   mycroft 	int error;
    494  1.22   mycroft 
    495  1.22   mycroft 	SC_DEBUG(sc_link, SDEV_DB2, ("scsi_cmd\n"));
    496  1.22   mycroft 
    497  1.25   mycroft #ifdef DIAGNOSTIC
    498  1.25   mycroft 	if (bp != 0 && (flags & SCSI_NOSLEEP) == 0)
    499  1.25   mycroft 		panic("scsi_scsi_cmd: buffer without nosleep");
    500  1.25   mycroft #endif
    501  1.25   mycroft 
    502  1.22   mycroft 	if ((xs = scsi_make_xs(sc_link, scsi_cmd, cmdlen, data_addr, datalen,
    503  1.22   mycroft 	    retries, timeout, bp, flags)) == NULL)
    504  1.22   mycroft 		return ENOMEM;
    505  1.22   mycroft 
    506  1.22   mycroft 	if ((error = scsi_execute_xs(xs)) == EJUSTRETURN)
    507  1.22   mycroft 		return 0;
    508  1.22   mycroft 
    509   1.1   mycroft 	/*
    510   1.1   mycroft 	 * we have finished with the xfer stuct, free it and
    511   1.1   mycroft 	 * check if anyone else needs to be started up.
    512   1.1   mycroft 	 */
    513  1.22   mycroft 	scsi_free_xs(xs, flags);
    514  1.11   mycroft 	return error;
    515   1.1   mycroft }
    516   1.1   mycroft 
    517   1.2   mycroft int
    518  1.22   mycroft sc_err1(xs, async)
    519   1.1   mycroft 	struct scsi_xfer *xs;
    520  1.22   mycroft 	int async;
    521   1.1   mycroft {
    522  1.11   mycroft 	int error;
    523   1.1   mycroft 
    524   1.1   mycroft 	SC_DEBUG(xs->sc_link, SDEV_DB3, ("sc_err1,err = 0x%x \n", xs->error));
    525  1.22   mycroft 
    526   1.1   mycroft 	/*
    527   1.1   mycroft 	 * If it has a buf, we might be working with
    528   1.1   mycroft 	 * a request from the buffer cache or some other
    529   1.1   mycroft 	 * piece of code that requires us to process
    530   1.1   mycroft 	 * errors at inetrrupt time. We have probably
    531   1.1   mycroft 	 * been called by scsi_done()
    532   1.1   mycroft 	 */
    533   1.1   mycroft 	switch (xs->error) {
    534   1.1   mycroft 	case XS_NOERROR:	/* nearly always hit this one */
    535  1.11   mycroft 		error = 0;
    536   1.1   mycroft 		break;
    537   1.1   mycroft 
    538   1.1   mycroft 	case XS_SENSE:
    539  1.22   mycroft 		if ((error = scsi_interpret_sense(xs)) == ERESTART)
    540  1.22   mycroft 			goto retry;
    541  1.21   mycroft 		SC_DEBUG(xs->sc_link, SDEV_DB3,
    542  1.21   mycroft 		    ("scsi_interpret_sense returned %d\n", error));
    543   1.1   mycroft 		break;
    544   1.1   mycroft 
    545   1.1   mycroft 	case XS_BUSY:
    546  1.22   mycroft 		if (xs->retries) {
    547  1.22   mycroft 			if ((xs->flags & SCSI_POLL) != 0)
    548  1.22   mycroft 				delay(1000000);
    549  1.22   mycroft 			else if ((xs->flags & SCSI_NOSLEEP) == 0)
    550  1.22   mycroft 				tsleep(&lbolt, PRIBIO, "scbusy", 0);
    551  1.22   mycroft 			else
    552  1.22   mycroft #if 0
    553  1.22   mycroft 				timeout(scsi_requeue, xs, hz);
    554  1.22   mycroft #else
    555  1.22   mycroft 				goto lose;
    556  1.22   mycroft #endif
    557  1.22   mycroft 		}
    558   1.1   mycroft 	case XS_TIMEOUT:
    559  1.22   mycroft 	retry:
    560   1.1   mycroft 		if (xs->retries--) {
    561   1.1   mycroft 			xs->error = XS_NOERROR;
    562   1.1   mycroft 			xs->flags &= ~ITSDONE;
    563  1.22   mycroft 			return ERESTART;
    564   1.1   mycroft 		}
    565   1.1   mycroft 	case XS_DRIVER_STUFFUP:
    566  1.22   mycroft 	lose:
    567  1.22   mycroft 		error = EIO;
    568  1.22   mycroft 		break;
    569  1.22   mycroft 
    570  1.22   mycroft 	case XS_SELTIMEOUT:
    571  1.22   mycroft 		/* XXX Disable device? */
    572  1.11   mycroft 		error = EIO;
    573   1.1   mycroft 		break;
    574  1.21   mycroft 
    575   1.1   mycroft 	default:
    576   1.1   mycroft 		sc_print_addr(xs->sc_link);
    577  1.40  christos 		printf("unknown error category from scsi driver\n");
    578  1.21   mycroft 		error = EIO;
    579  1.21   mycroft 		break;
    580  1.21   mycroft 	}
    581  1.21   mycroft 
    582  1.22   mycroft 	return error;
    583   1.1   mycroft }
    584   1.1   mycroft 
    585   1.1   mycroft /*
    586   1.1   mycroft  * Look at the returned sense and act on the error, determining
    587   1.1   mycroft  * the unix error number to pass back.  (0 = report no error)
    588   1.1   mycroft  *
    589   1.1   mycroft  * THIS IS THE DEFAULT ERROR HANDLER
    590   1.1   mycroft  */
    591   1.2   mycroft int
    592   1.1   mycroft scsi_interpret_sense(xs)
    593   1.1   mycroft 	struct scsi_xfer *xs;
    594   1.1   mycroft {
    595   1.1   mycroft 	struct scsi_sense_data *sense;
    596   1.1   mycroft 	struct scsi_link *sc_link = xs->sc_link;
    597  1.22   mycroft 	u_int8_t key;
    598  1.22   mycroft 	u_int32_t info;
    599   1.2   mycroft 	int error;
    600  1.46    mjacob #ifndef	SCSIVERBOSE
    601  1.22   mycroft 	static char *error_mes[] = {
    602  1.22   mycroft 		"soft error (corrected)",
    603  1.22   mycroft 		"not ready", "medium error",
    604  1.22   mycroft 		"non-media hardware failure", "illegal request",
    605  1.22   mycroft 		"unit attention", "readonly device",
    606  1.22   mycroft 		"no data found", "vendor unique",
    607  1.22   mycroft 		"copy aborted", "command aborted",
    608  1.22   mycroft 		"search returned equal", "volume overflow",
    609  1.22   mycroft 		"verify miscompare", "unknown error key"
    610   1.1   mycroft 	};
    611  1.46    mjacob #endif
    612   1.1   mycroft 
    613  1.11   mycroft 	sense = &xs->sense;
    614   1.1   mycroft #ifdef	SCSIDEBUG
    615  1.22   mycroft 	if ((sc_link->flags & SDEV_DB1) != 0) {
    616  1.22   mycroft 		int count;
    617  1.45      fair 		printf("code 0x%x valid 0x%x ",
    618   1.1   mycroft 		    sense->error_code & SSD_ERRCODE,
    619   1.1   mycroft 		    sense->error_code & SSD_ERRCODE_VALID ? 1 : 0);
    620  1.45      fair 		printf("seg 0x%x key 0x%x ili 0x%x eom 0x%x fmark 0x%x\n",
    621  1.34   mycroft 		    sense->segment,
    622  1.34   mycroft 		    sense->flags & SSD_KEY,
    623  1.34   mycroft 		    sense->flags & SSD_ILI ? 1 : 0,
    624  1.34   mycroft 		    sense->flags & SSD_EOM ? 1 : 0,
    625  1.34   mycroft 		    sense->flags & SSD_FILEMARK ? 1 : 0);
    626  1.45      fair 		printf("info: 0x%x 0x%x 0x%x 0x%x followed by %d extra bytes\n",
    627  1.34   mycroft 		    sense->info[0],
    628  1.34   mycroft 		    sense->info[1],
    629  1.34   mycroft 		    sense->info[2],
    630  1.34   mycroft 		    sense->info[3],
    631  1.34   mycroft 		    sense->extra_len);
    632  1.40  christos 		printf("extra: ");
    633  1.34   mycroft 		for (count = 0; count < sense->extra_len; count++)
    634  1.45      fair 			printf("0x%x ", sense->extra_bytes[count]);
    635  1.40  christos 		printf("\n");
    636   1.1   mycroft 	}
    637  1.46    mjacob #endif	/* SCSIDEBUG */
    638   1.1   mycroft 	/*
    639   1.1   mycroft 	 * If the device has it's own error handler, call it first.
    640   1.1   mycroft 	 * If it returns a legit error value, return that, otherwise
    641   1.1   mycroft 	 * it wants us to continue with normal error processing.
    642   1.1   mycroft 	 */
    643   1.1   mycroft 	if (sc_link->device->err_handler) {
    644   1.1   mycroft 		SC_DEBUG(sc_link, SDEV_DB2, ("calling private err_handler()\n"));
    645   1.2   mycroft 		error = (*sc_link->device->err_handler) (xs);
    646   1.2   mycroft 		if (error != -1)
    647   1.2   mycroft 			return error;		/* error >= 0  better ? */
    648   1.1   mycroft 	}
    649   1.1   mycroft 	/* otherwise use the default */
    650   1.1   mycroft 	switch (sense->error_code & SSD_ERRCODE) {
    651   1.1   mycroft 		/*
    652   1.1   mycroft 		 * If it's code 70, use the extended stuff and interpret the key
    653   1.1   mycroft 		 */
    654   1.1   mycroft 	case 0x71:		/* delayed error */
    655   1.1   mycroft 		sc_print_addr(sc_link);
    656  1.34   mycroft 		key = sense->flags & SSD_KEY;
    657  1.46    mjacob 		printf(" DEFERRED ERROR, key = 0x%x\n", key);
    658  1.46    mjacob 		/* FALLTHROUGH */
    659   1.1   mycroft 	case 0x70:
    660  1.34   mycroft 		if ((sense->error_code & SSD_ERRCODE_VALID) != 0)
    661  1.34   mycroft 			info = _4btol(sense->info);
    662  1.34   mycroft 		else
    663   1.1   mycroft 			info = 0;
    664  1.34   mycroft 		key = sense->flags & SSD_KEY;
    665   1.1   mycroft 
    666  1.22   mycroft 		switch (key) {
    667  1.23   mycroft 		case 0x0:	/* NO SENSE */
    668  1.22   mycroft 		case 0x1:	/* RECOVERED ERROR */
    669  1.22   mycroft 			if (xs->resid == xs->datalen)
    670  1.22   mycroft 				xs->resid = 0;	/* not short read */
    671  1.22   mycroft 		case 0xc:	/* EQUAL */
    672  1.22   mycroft 			error = 0;
    673  1.22   mycroft 			break;
    674  1.22   mycroft 		case 0x2:	/* NOT READY */
    675  1.22   mycroft 			if ((sc_link->flags & SDEV_REMOVABLE) != 0)
    676  1.22   mycroft 				sc_link->flags &= ~SDEV_MEDIA_LOADED;
    677  1.22   mycroft 			if ((xs->flags & SCSI_IGNORE_NOT_READY) != 0)
    678  1.22   mycroft 				return 0;
    679  1.22   mycroft 			if ((xs->flags & SCSI_SILENT) != 0)
    680  1.22   mycroft 				return EIO;
    681  1.22   mycroft 			error = EIO;
    682  1.22   mycroft 			break;
    683  1.22   mycroft 		case 0x5:	/* ILLEGAL REQUEST */
    684  1.22   mycroft 			if ((xs->flags & SCSI_IGNORE_ILLEGAL_REQUEST) != 0)
    685  1.22   mycroft 				return 0;
    686  1.37  christos 			if ((xs->flags & SCSI_SILENT) != 0)
    687  1.37  christos 				return EIO;
    688  1.22   mycroft 			error = EINVAL;
    689  1.22   mycroft 			break;
    690  1.22   mycroft 		case 0x6:	/* UNIT ATTENTION */
    691  1.22   mycroft 			if ((sc_link->flags & SDEV_REMOVABLE) != 0)
    692  1.22   mycroft 				sc_link->flags &= ~SDEV_MEDIA_LOADED;
    693  1.22   mycroft 			if ((xs->flags & SCSI_IGNORE_MEDIA_CHANGE) != 0 ||
    694  1.22   mycroft 			    /* XXX Should reupload any transient state. */
    695  1.22   mycroft 			    (sc_link->flags & SDEV_REMOVABLE) == 0)
    696  1.22   mycroft 				return ERESTART;
    697  1.22   mycroft 			if ((xs->flags & SCSI_SILENT) != 0)
    698  1.22   mycroft 				return EIO;
    699  1.22   mycroft 			error = EIO;
    700  1.22   mycroft 			break;
    701  1.22   mycroft 		case 0x7:	/* DATA PROTECT */
    702  1.22   mycroft 			error = EACCES;
    703  1.22   mycroft 			break;
    704  1.22   mycroft 		case 0x8:	/* BLANK CHECK */
    705  1.22   mycroft 			error = 0;
    706  1.22   mycroft 			break;
    707  1.32    briggs 		case 0xb:	/* COMMAND ABORTED */
    708  1.32    briggs 			error = ERESTART;
    709  1.32    briggs 			break;
    710  1.22   mycroft 		case 0xd:	/* VOLUME OVERFLOW */
    711  1.22   mycroft 			error = ENOSPC;
    712  1.22   mycroft 			break;
    713  1.22   mycroft 		default:
    714  1.22   mycroft 			error = EIO;
    715  1.22   mycroft 			break;
    716  1.22   mycroft 		}
    717  1.22   mycroft 
    718  1.46    mjacob 
    719  1.46    mjacob #ifdef	SCSIVERBOSE
    720  1.46    mjacob 		scsi_print_sense(xs, 0);
    721  1.46    mjacob #else
    722  1.22   mycroft 		if (key) {
    723   1.1   mycroft 			sc_print_addr(sc_link);
    724  1.40  christos 			printf("%s", error_mes[key - 1]);
    725  1.22   mycroft 			if ((sense->error_code & SSD_ERRCODE_VALID) != 0) {
    726   1.1   mycroft 				switch (key) {
    727  1.23   mycroft 				case 0x2:	/* NOT READY */
    728  1.23   mycroft 				case 0x5:	/* ILLEGAL REQUEST */
    729  1.23   mycroft 				case 0x6:	/* UNIT ATTENTION */
    730   1.1   mycroft 				case 0x7:	/* DATA PROTECT */
    731   1.1   mycroft 					break;
    732   1.1   mycroft 				case 0x8:	/* BLANK CHECK */
    733  1.40  christos 					printf(", requested size: %d (decimal)",
    734   1.1   mycroft 					    info);
    735  1.32    briggs 					break;
    736  1.32    briggs 				case 0xb:
    737  1.32    briggs 					if (xs->retries)
    738  1.40  christos 						printf(", retrying");
    739  1.40  christos 					printf(", cmd 0x%x, info 0x%x",
    740  1.32    briggs 						xs->cmd->opcode, info);
    741   1.1   mycroft 					break;
    742   1.1   mycroft 				default:
    743  1.40  christos 					printf(", info = %d (decimal)", info);
    744   1.1   mycroft 				}
    745  1.25   mycroft 			}
    746  1.34   mycroft 			if (sense->extra_len != 0) {
    747  1.25   mycroft 				int n;
    748  1.40  christos 				printf(", data =");
    749  1.34   mycroft 				for (n = 0; n < sense->extra_len; n++)
    750  1.44  matthias 					printf(" %02x", sense->cmd_spec_info[n]);
    751   1.1   mycroft 			}
    752  1.40  christos 			printf("\n");
    753   1.1   mycroft 		}
    754  1.46    mjacob #endif
    755  1.22   mycroft 		return error;
    756  1.22   mycroft 
    757   1.1   mycroft 	/*
    758   1.1   mycroft 	 * Not code 70, just report it
    759   1.1   mycroft 	 */
    760   1.1   mycroft 	default:
    761  1.22   mycroft 		sc_print_addr(sc_link);
    762  1.40  christos 		printf("error code %d",
    763  1.22   mycroft 		    sense->error_code & SSD_ERRCODE);
    764  1.22   mycroft 		if ((sense->error_code & SSD_ERRCODE_VALID) != 0) {
    765  1.34   mycroft 			struct scsi_sense_data_unextended *usense =
    766  1.34   mycroft 			    (struct scsi_sense_data_unextended *)sense;
    767  1.40  christos 			printf(" at block no. %d (decimal)",
    768  1.34   mycroft 			    _3btol(usense->block));
    769   1.1   mycroft 		}
    770  1.40  christos 		printf("\n");
    771  1.11   mycroft 		return EIO;
    772   1.1   mycroft 	}
    773   1.1   mycroft }
    774   1.1   mycroft 
    775   1.1   mycroft /*
    776   1.1   mycroft  * Utility routines often used in SCSI stuff
    777   1.1   mycroft  */
    778   1.1   mycroft 
    779   1.1   mycroft 
    780   1.1   mycroft /*
    781   1.1   mycroft  * Print out the scsi_link structure's address info.
    782   1.1   mycroft  */
    783   1.1   mycroft void
    784   1.1   mycroft sc_print_addr(sc_link)
    785  1.22   mycroft 	struct scsi_link *sc_link;
    786   1.1   mycroft {
    787   1.1   mycroft 
    788  1.40  christos 	printf("%s(%s:%d:%d): ",
    789  1.22   mycroft 	    sc_link->device_softc ?
    790  1.22   mycroft 	    ((struct device *)sc_link->device_softc)->dv_xname : "probe",
    791  1.22   mycroft 	    ((struct device *)sc_link->adapter_softc)->dv_xname,
    792  1.22   mycroft 	    sc_link->target, sc_link->lun);
    793   1.1   mycroft }
    794   1.1   mycroft 
    795  1.46    mjacob #ifdef	SCSIVERBOSE
    796  1.46    mjacob static const char *sense_keys[16] = {
    797  1.46    mjacob 	"No Additional Sense",
    798  1.46    mjacob 	"Soft Error",
    799  1.46    mjacob 	"Not Ready",
    800  1.46    mjacob 	"Media Error",
    801  1.46    mjacob 	"Hardware Error",
    802  1.46    mjacob 	"Illegal Request",
    803  1.46    mjacob 	"Unit Attention",
    804  1.46    mjacob 	"Write Protected",
    805  1.46    mjacob 	"Blank Check",
    806  1.46    mjacob 	"Vendor Unique",
    807  1.46    mjacob 	"Copy Aborted",
    808  1.46    mjacob 	"Aborted Command",
    809  1.46    mjacob 	"Equal Error",
    810  1.46    mjacob 	"Volume Overflow",
    811  1.46    mjacob 	"Miscompare Error",
    812  1.46    mjacob 	"Reserved"
    813  1.46    mjacob };
    814  1.46    mjacob static const struct {
    815  1.46    mjacob 	unsigned char asc;
    816  1.46    mjacob 	unsigned char ascq;
    817  1.46    mjacob 	char *description;
    818  1.46    mjacob } adesc[] = {
    819  1.46    mjacob { 0x00, 0x00, "No Additional Sense Information" },
    820  1.46    mjacob { 0x00, 0x01, "Filemark Detected" },
    821  1.46    mjacob { 0x00, 0x02, "End-Of-Partition/Medium Detected" },
    822  1.46    mjacob { 0x00, 0x03, "Setmark Detected" },
    823  1.46    mjacob { 0x00, 0x04, "Beginning-Of-Partition/Medium Detected" },
    824  1.46    mjacob { 0x00, 0x05, "End-Of-Data Detected" },
    825  1.46    mjacob { 0x00, 0x06, "I/O Process Terminated" },
    826  1.46    mjacob { 0x00, 0x11, "Audio Play Operation In Progress" },
    827  1.46    mjacob { 0x00, 0x12, "Audio Play Operation Paused" },
    828  1.46    mjacob { 0x00, 0x13, "Audio Play Operation Successfully Completed" },
    829  1.46    mjacob { 0x00, 0x14, "Audio Play Operation Stopped Due to Error" },
    830  1.46    mjacob { 0x00, 0x15, "No Current Audio Status To Return" },
    831  1.46    mjacob { 0x01, 0x00, "No Index/Sector Signal" },
    832  1.46    mjacob { 0x02, 0x00, "No Seek Complete" },
    833  1.46    mjacob { 0x03, 0x00, "Peripheral Device Write Fault" },
    834  1.46    mjacob { 0x03, 0x01, "No Write Current" },
    835  1.46    mjacob { 0x03, 0x02, "Excessive Write Errors" },
    836  1.46    mjacob { 0x04, 0x00, "Logical Unit Not Ready, Cause Not Reportable" },
    837  1.46    mjacob { 0x04, 0x01, "Logical Unit Is in Process Of Becoming Ready" },
    838  1.46    mjacob { 0x04, 0x02, "Logical Unit Not Ready, Initialization Command Required" },
    839  1.46    mjacob { 0x04, 0x03, "Logical Unit Not Ready, Manual Intervention Required" },
    840  1.46    mjacob { 0x04, 0x04, "Logical Unit Not Ready, Format In Progress" },
    841  1.46    mjacob { 0x05, 0x00, "Logical Unit Does Not Respond To Selection" },
    842  1.46    mjacob { 0x06, 0x00, "No Reference Position Found" },
    843  1.46    mjacob { 0x07, 0x00, "Multiple Peripheral Devices Selected" },
    844  1.46    mjacob { 0x08, 0x00, "Logical Unit Communication Failure" },
    845  1.46    mjacob { 0x08, 0x01, "Logical Unit Communication Timeout" },
    846  1.46    mjacob { 0x08, 0x02, "Logical Unit Communication Parity Error" },
    847  1.46    mjacob { 0x09, 0x00, "Track Following Error" },
    848  1.46    mjacob { 0x09, 0x01, "Tracking Servo Failure" },
    849  1.46    mjacob { 0x09, 0x02, "Focus Servo Failure" },
    850  1.46    mjacob { 0x09, 0x03, "Spindle Servo Failure" },
    851  1.46    mjacob { 0x0A, 0x00, "Error Log Overflow" },
    852  1.46    mjacob { 0x0C, 0x00, "Write Error" },
    853  1.46    mjacob { 0x0C, 0x01, "Write Error Recovered with Auto Reallocation" },
    854  1.46    mjacob { 0x0C, 0x02, "Write Error - Auto Reallocate Failed" },
    855  1.46    mjacob { 0x10, 0x00, "ID CRC Or ECC Error" },
    856  1.46    mjacob { 0x11, 0x00, "Unrecovered Read Error" },
    857  1.46    mjacob { 0x11, 0x01, "Read Retried Exhausted" },
    858  1.46    mjacob { 0x11, 0x02, "Error Too Long To Correct" },
    859  1.46    mjacob { 0x11, 0x03, "Multiple Read Errors" },
    860  1.46    mjacob { 0x11, 0x04, "Unrecovered Read Error - Auto Reallocate Failed" },
    861  1.46    mjacob { 0x11, 0x05, "L-EC Uncorrectable Error" },
    862  1.46    mjacob { 0x11, 0x06, "CIRC Unrecovered Error" },
    863  1.46    mjacob { 0x11, 0x07, "Data Resynchronization Error" },
    864  1.46    mjacob { 0x11, 0x08, "Incomplete Block Found" },
    865  1.46    mjacob { 0x11, 0x09, "No Gap Found" },
    866  1.46    mjacob { 0x11, 0x0A, "Miscorrected Error" },
    867  1.46    mjacob { 0x11, 0x0B, "Uncorrected Read Error - Recommend Reassignment" },
    868  1.46    mjacob { 0x11, 0x0C, "Uncorrected Read Error - Recommend Rewrite the Data" },
    869  1.46    mjacob { 0x12, 0x00, "Address Mark Not Found for ID Field" },
    870  1.46    mjacob { 0x13, 0x00, "Address Mark Not Found for Data Field" },
    871  1.46    mjacob { 0x14, 0x00, "Recorded Entity Not Found" },
    872  1.46    mjacob { 0x14, 0x01, "Record Not Found" },
    873  1.46    mjacob { 0x14, 0x02, "Filemark or Setmark Not Found" },
    874  1.46    mjacob { 0x14, 0x03, "End-Of-Data Not Found" },
    875  1.46    mjacob { 0x14, 0x04, "Block Sequence Error" },
    876  1.46    mjacob { 0x15, 0x00, "Random Positioning Error" },
    877  1.46    mjacob { 0x15, 0x01, "Mechanical Positioning Error" },
    878  1.46    mjacob { 0x15, 0x02, "Positioning Error Detected By Read of Medium" },
    879  1.46    mjacob { 0x16, 0x00, "Data Synchronization Mark Error" },
    880  1.46    mjacob { 0x17, 0x00, "Recovered Data With No Error Correction Applied" },
    881  1.46    mjacob { 0x17, 0x01, "Recovered Data With Retries" },
    882  1.46    mjacob { 0x17, 0x02, "Recovered Data With Positive Head Offset" },
    883  1.46    mjacob { 0x17, 0x03, "Recovered Data With Negative Head Offset" },
    884  1.46    mjacob { 0x17, 0x04, "Recovered Data With Retries and/or CIRC Applied" },
    885  1.46    mjacob { 0x17, 0x05, "Recovered Data Using Previous Sector ID" },
    886  1.46    mjacob { 0x17, 0x06, "Recovered Data Without ECC - Data Auto-Reallocated" },
    887  1.46    mjacob { 0x17, 0x07, "Recovered Data Without ECC - Recommend Reassignment" },
    888  1.46    mjacob { 0x17, 0x08, "Recovered Data Without ECC - Recommend Rewrite" },
    889  1.46    mjacob { 0x18, 0x00, "Recovered Data With Error Correction Applied" },
    890  1.46    mjacob { 0x18, 0x01, "Recovered Data With Error Correction & Retries Applied" },
    891  1.46    mjacob { 0x18, 0x02, "Recovered Data - Data Auto-Reallocated" },
    892  1.46    mjacob { 0x18, 0x03, "Recovered Data With CIRC" },
    893  1.46    mjacob { 0x18, 0x04, "Recovered Data With LEC" },
    894  1.46    mjacob { 0x18, 0x05, "Recovered Data - Recommend Reassignment" },
    895  1.46    mjacob { 0x18, 0x06, "Recovered Data - Recommend Rewrite" },
    896  1.46    mjacob { 0x19, 0x00, "Defect List Error" },
    897  1.46    mjacob { 0x19, 0x01, "Defect List Not Available" },
    898  1.46    mjacob { 0x19, 0x02, "Defect List Error in Primary List" },
    899  1.46    mjacob { 0x19, 0x03, "Defect List Error in Grown List" },
    900  1.46    mjacob { 0x1A, 0x00, "Parameter List Length Error" },
    901  1.46    mjacob { 0x1B, 0x00, "Synchronous Data Transfer Error" },
    902  1.46    mjacob { 0x1C, 0x00, "Defect List Not Found" },
    903  1.46    mjacob { 0x1C, 0x01, "Primary Defect List Not Found" },
    904  1.46    mjacob { 0x1C, 0x02, "Grown Defect List Not Found" },
    905  1.46    mjacob { 0x1D, 0x00, "Miscompare During Verify Operation" },
    906  1.46    mjacob { 0x1E, 0x00, "Recovered ID with ECC" },
    907  1.46    mjacob { 0x20, 0x00, "Invalid Command Operation Code" },
    908  1.46    mjacob { 0x21, 0x00, "Logical Block Address Out of Range" },
    909  1.46    mjacob { 0x21, 0x01, "Invalid Element Address" },
    910  1.46    mjacob { 0x22, 0x00, "Illegal Function (Should 20 00, 24 00, or 26 00)" },
    911  1.46    mjacob { 0x24, 0x00, "Illegal Field in CDB" },
    912  1.46    mjacob { 0x25, 0x00, "Logical Unit Not Supported" },
    913  1.46    mjacob { 0x26, 0x00, "Invalid Field In Parameter List" },
    914  1.46    mjacob { 0x26, 0x01, "Parameter Not Supported" },
    915  1.46    mjacob { 0x26, 0x02, "Parameter Value Invalid" },
    916  1.46    mjacob { 0x26, 0x03, "Threshold Parameters Not Supported" },
    917  1.46    mjacob { 0x27, 0x00, "Write Protected" },
    918  1.46    mjacob { 0x28, 0x00, "Not Ready To Ready Transition (Medium May Have Changed)" },
    919  1.46    mjacob { 0x28, 0x01, "Import Or Export Element Accessed" },
    920  1.46    mjacob { 0x29, 0x00, "Power On, Reset, or Bus Device Reset Occurred" },
    921  1.46    mjacob { 0x2A, 0x00, "Parameters Changed" },
    922  1.46    mjacob { 0x2A, 0x01, "Mode Parameters Changed" },
    923  1.46    mjacob { 0x2A, 0x02, "Log Parameters Changed" },
    924  1.46    mjacob { 0x2B, 0x00, "Copy Cannot Execute Since Host Cannot Disconnect" },
    925  1.46    mjacob { 0x2C, 0x00, "Command Sequence Error" },
    926  1.46    mjacob { 0x2C, 0x01, "Too Many Windows Specified" },
    927  1.46    mjacob { 0x2C, 0x02, "Invalid Combination of Windows Specified" },
    928  1.46    mjacob { 0x2D, 0x00, "Overwrite Error On Update In Place" },
    929  1.46    mjacob { 0x2F, 0x00, "Commands Cleared By Another Initiator" },
    930  1.46    mjacob { 0x30, 0x00, "Incompatible Medium Installed" },
    931  1.46    mjacob { 0x30, 0x01, "Cannot Read Medium - Unknown Format" },
    932  1.46    mjacob { 0x30, 0x02, "Cannot Read Medium - Incompatible Format" },
    933  1.46    mjacob { 0x30, 0x03, "Cleaning Cartridge Installed" },
    934  1.46    mjacob { 0x31, 0x00, "Medium Format Corrupted" },
    935  1.46    mjacob { 0x31, 0x01, "Format Command Failed" },
    936  1.46    mjacob { 0x32, 0x00, "No Defect Spare Location Available" },
    937  1.46    mjacob { 0x32, 0x01, "Defect List Update Failure" },
    938  1.46    mjacob { 0x33, 0x00, "Tape Length Error" },
    939  1.46    mjacob { 0x36, 0x00, "Ribbon, Ink, or Toner Failure" },
    940  1.46    mjacob { 0x37, 0x00, "Rounded Parameter" },
    941  1.46    mjacob { 0x39, 0x00, "Saving Parameters Not Supported" },
    942  1.46    mjacob { 0x3A, 0x00, "Medium Not Present" },
    943  1.46    mjacob { 0x3B, 0x00, "Positioning Error" },
    944  1.46    mjacob { 0x3B, 0x01, "Tape Position Error At Beginning-of-Medium" },
    945  1.46    mjacob { 0x3B, 0x02, "Tape Position Error At End-of-Medium" },
    946  1.46    mjacob { 0x3B, 0x03, "Tape or Electronic Vertical Forms Unit Not Ready" },
    947  1.46    mjacob { 0x3B, 0x04, "Slew Failure" },
    948  1.46    mjacob { 0x3B, 0x05, "Paper Jam" },
    949  1.46    mjacob { 0x3B, 0x06, "Failed To Sense Top-Of-Form" },
    950  1.46    mjacob { 0x3B, 0x07, "Failed To Sense Bottom-Of-Form" },
    951  1.46    mjacob { 0x3B, 0x08, "Reposition Error" },
    952  1.46    mjacob { 0x3B, 0x09, "Read Past End Of Medium" },
    953  1.46    mjacob { 0x3B, 0x0A, "Read Past Begining Of Medium" },
    954  1.46    mjacob { 0x3B, 0x0B, "Position Past End Of Medium" },
    955  1.46    mjacob { 0x3B, 0x0C, "Position Past Beginning Of Medium" },
    956  1.46    mjacob { 0x3B, 0x0D, "Medium Destination Element Full" },
    957  1.46    mjacob { 0x3B, 0x0E, "Medium Source Element Empty" },
    958  1.46    mjacob { 0x3D, 0x00, "Invalid Bits In IDENTFY Message" },
    959  1.46    mjacob { 0x3E, 0x00, "Logical Unit Has Not Self-Configured Yet" },
    960  1.46    mjacob { 0x3F, 0x00, "Target Operating Conditions Have Changed" },
    961  1.46    mjacob { 0x3F, 0x01, "Microcode Has Changed" },
    962  1.46    mjacob { 0x3F, 0x02, "Changed Operating Definition" },
    963  1.46    mjacob { 0x3F, 0x03, "INQUIRY Data Has Changed" },
    964  1.46    mjacob { 0x40, 0x00, "RAM FAILURE (Should Use 40 NN)" },
    965  1.46    mjacob { 0x41, 0x00, "Data Path FAILURE (Should Use 40 NN)" },
    966  1.46    mjacob { 0x42, 0x00, "Power-On or Self-Test FAILURE (Should Use 40 NN)" },
    967  1.46    mjacob { 0x43, 0x00, "Message Error" },
    968  1.46    mjacob { 0x44, 0x00, "Internal Target Failure" },
    969  1.46    mjacob { 0x45, 0x00, "Select Or Reselect Failure" },
    970  1.46    mjacob { 0x46, 0x00, "Unsuccessful Soft Reset" },
    971  1.46    mjacob { 0x47, 0x00, "SCSI Parity Error" },
    972  1.46    mjacob { 0x48, 0x00, "INITIATOR DETECTED ERROR Message Received" },
    973  1.46    mjacob { 0x49, 0x00, "Invalid Message Error" },
    974  1.46    mjacob { 0x4A, 0x00, "Command Phase Error" },
    975  1.46    mjacob { 0x4B, 0x00, "Data Phase Error" },
    976  1.46    mjacob { 0x4C, 0x00, "Logical Unit Failed Self-Configuration" },
    977  1.46    mjacob { 0x4E, 0x00, "Overlapped Commands Attempted" },
    978  1.46    mjacob { 0x50, 0x00, "Write Append Error" },
    979  1.46    mjacob { 0x50, 0x01, "Write Append Position Error" },
    980  1.46    mjacob { 0x50, 0x02, "Position Error Related To Timing" },
    981  1.46    mjacob { 0x51, 0x00, "Erase Failure" },
    982  1.46    mjacob { 0x52, 0x00, "Cartridge Fault" },
    983  1.46    mjacob { 0x53, 0x00, "Media Load or Eject Failed" },
    984  1.46    mjacob { 0x53, 0x01, "Unload Tape Failure" },
    985  1.46    mjacob { 0x53, 0x02, "Medium Removal Prevented" },
    986  1.46    mjacob { 0x54, 0x00, "SCSI To Host System Interface Failure" },
    987  1.46    mjacob { 0x55, 0x00, "System Resource Failure" },
    988  1.46    mjacob { 0x57, 0x00, "Unable To Recover Table-Of-Contents" },
    989  1.46    mjacob { 0x58, 0x00, "Generation Does Not Exist" },
    990  1.46    mjacob { 0x59, 0x00, "Updated Block Read" },
    991  1.46    mjacob { 0x5A, 0x00, "Operator Request or State Change Input (Unspecified)" },
    992  1.46    mjacob { 0x5A, 0x01, "Operator Medium Removal Requested" },
    993  1.46    mjacob { 0x5A, 0x02, "Operator Selected Write Protect" },
    994  1.46    mjacob { 0x5A, 0x03, "Operator Selected Write Permit" },
    995  1.46    mjacob { 0x5B, 0x00, "Log Exception" },
    996  1.46    mjacob { 0x5B, 0x01, "Threshold Condition Met" },
    997  1.46    mjacob { 0x5B, 0x02, "Log Counter At Maximum" },
    998  1.46    mjacob { 0x5B, 0x03, "Log List Codes Exhausted" },
    999  1.46    mjacob { 0x5C, 0x00, "RPL Status Change" },
   1000  1.46    mjacob { 0x5C, 0x01, "Spindles Synchronized" },
   1001  1.46    mjacob { 0x5C, 0x02, "Spindles Not Synchronized" },
   1002  1.46    mjacob { 0x60, 0x00, "Lamp Failure" },
   1003  1.46    mjacob { 0x61, 0x00, "Video Acquisition Error" },
   1004  1.46    mjacob { 0x61, 0x01, "Unable To Acquire Video" },
   1005  1.46    mjacob { 0x61, 0x02, "Out Of Focus" },
   1006  1.46    mjacob { 0x62, 0x00, "Scan Head Positioning Error" },
   1007  1.46    mjacob { 0x63, 0x00, "End Of User Area Encountered On This Track" },
   1008  1.46    mjacob { 0x64, 0x00, "Illegal Mode For This Track" },
   1009  1.46    mjacob { 0x00, 0x00, (char *) 0 }
   1010  1.46    mjacob };
   1011  1.46    mjacob 
   1012  1.46    mjacob static inline void
   1013  1.46    mjacob asc2ascii(unsigned char asc, unsigned char ascq, char *result)
   1014  1.46    mjacob {
   1015  1.46    mjacob 	register int i = 0;
   1016  1.46    mjacob 
   1017  1.46    mjacob 	while (adesc[i].description != (char *) 0) {
   1018  1.46    mjacob 		if (adesc[i].asc == asc && adesc[i].ascq == ascq) {
   1019  1.46    mjacob 			break;
   1020  1.46    mjacob 		}
   1021  1.46    mjacob 		i++;
   1022  1.46    mjacob 	}
   1023  1.46    mjacob 	if (adesc[i].description == (char *) 0) {
   1024  1.46    mjacob 		if (asc == 0x40 && ascq != 0) {
   1025  1.46    mjacob 			(void) sprintf(result,
   1026  1.46    mjacob 			    "Diagnostic Failure on Component 0x%02x",
   1027  1.46    mjacob 			    ascq & 0xff);
   1028  1.46    mjacob 		} else {
   1029  1.46    mjacob 			(void) sprintf(result, "ASC 0x%02x ASCQ 0x%02x",
   1030  1.46    mjacob 			    asc & 0xff, ascq & 0xff);
   1031  1.46    mjacob 		}
   1032  1.46    mjacob 	} else {
   1033  1.46    mjacob 		(void) strcpy(result, adesc[i].description);
   1034  1.46    mjacob 	}
   1035  1.46    mjacob }
   1036  1.46    mjacob 
   1037  1.46    mjacob void
   1038  1.46    mjacob scsi_print_sense(xs, verbosity)
   1039  1.46    mjacob 	struct scsi_xfer *xs;
   1040  1.46    mjacob 	int verbosity;
   1041  1.46    mjacob {
   1042  1.46    mjacob 	int32_t info;
   1043  1.46    mjacob 	register int i, j, k;
   1044  1.46    mjacob 	char *sbs, *s;
   1045  1.46    mjacob 
   1046  1.46    mjacob 	sc_print_addr(xs->sc_link);
   1047  1.46    mjacob 	s = (char *) &xs->sense;
   1048  1.46    mjacob 	printf(" Check Condition on opcode %x\n", xs->cmd->opcode);
   1049  1.46    mjacob 
   1050  1.46    mjacob 	/*
   1051  1.46    mjacob 	 * Basics- print out SENSE KEY
   1052  1.46    mjacob 	 */
   1053  1.46    mjacob 	printf("    SENSE KEY:  %s", scsi_decode_sense(s, 0));
   1054  1.46    mjacob 
   1055  1.46    mjacob 	/*
   1056  1.46    mjacob  	 * Print out, unqualified but aligned, FMK, EOM and ILI status.
   1057  1.46    mjacob 	 */
   1058  1.46    mjacob 	if (s[2] & 0xe0) {
   1059  1.46    mjacob 		char pad;
   1060  1.46    mjacob 		printf("\n              ");
   1061  1.46    mjacob 		pad = ' ';
   1062  1.46    mjacob 		if (s[2] & SSD_FILEMARK) {
   1063  1.46    mjacob 			printf("%c Filemark Detected", pad);
   1064  1.46    mjacob 			pad = ',';
   1065  1.46    mjacob 		}
   1066  1.46    mjacob 		if (s[2] & SSD_EOM) {
   1067  1.46    mjacob 			printf("%c EOM Detected", pad);
   1068  1.46    mjacob 			pad = ',';
   1069  1.46    mjacob 		}
   1070  1.46    mjacob 		if (s[2] & SSD_ILI) {
   1071  1.46    mjacob 			printf("%c Incorrect Length Indicator Set", pad);
   1072  1.46    mjacob 		}
   1073  1.46    mjacob 	}
   1074  1.46    mjacob 
   1075  1.46    mjacob 	/*
   1076  1.46    mjacob 	 * Now we should figure out, based upon device type, how
   1077  1.46    mjacob 	 * to format the information field. Unfortunately, that's
   1078  1.46    mjacob 	 * not convenient here, so we'll print it as a signed
   1079  1.46    mjacob 	 * 32 bit integer.
   1080  1.46    mjacob 	 */
   1081  1.46    mjacob 	info = _4btol(&s[3]);
   1082  1.46    mjacob 	if (info) {
   1083  1.46    mjacob 		printf("\n   INFO FIELD:  %d", info);
   1084  1.46    mjacob 	}
   1085  1.46    mjacob 
   1086  1.46    mjacob 	/*
   1087  1.46    mjacob 	 * Now we check additional length to see whether there is
   1088  1.46    mjacob 	 * more information to extract.
   1089  1.46    mjacob 	 */
   1090  1.46    mjacob 
   1091  1.46    mjacob 	/* enough for command specific information? */
   1092  1.46    mjacob 	if (s[7] < 4) {
   1093  1.46    mjacob 		printf("\n");
   1094  1.46    mjacob 		return;
   1095  1.46    mjacob 	}
   1096  1.46    mjacob 	info = _4btol(&s[8]);
   1097  1.46    mjacob 	if (info) {
   1098  1.46    mjacob 		printf("\n COMMAND INFO:  %d (0x%x)", info, info);
   1099  1.46    mjacob 	}
   1100  1.46    mjacob 
   1101  1.46    mjacob 	/*
   1102  1.46    mjacob 	 * Decode ASC && ASCQ info, plus FRU, plus the rest...
   1103  1.46    mjacob 	 */
   1104  1.46    mjacob 
   1105  1.46    mjacob 	sbs = scsi_decode_sense(s, 1);
   1106  1.46    mjacob 	if (sbs) {
   1107  1.46    mjacob 		printf("\n     ASC/ASCQ:  %s", sbs);
   1108  1.46    mjacob 	}
   1109  1.46    mjacob 	if (s[14] != 0) {
   1110  1.46    mjacob 		printf("\n     FRU CODE:  0x%x\n", s[14] & 0xff);
   1111  1.46    mjacob 	}
   1112  1.46    mjacob 	sbs = scsi_decode_sense(s, 3);
   1113  1.46    mjacob 	if (sbs) {
   1114  1.46    mjacob 		printf("\n         SKSV:  %s", sbs);
   1115  1.46    mjacob 	}
   1116  1.46    mjacob 	printf("\n");
   1117  1.46    mjacob 	if (verbosity == 0) {
   1118  1.46    mjacob 		printf("\n");
   1119  1.46    mjacob 		return;
   1120  1.46    mjacob 	}
   1121  1.46    mjacob 
   1122  1.46    mjacob 	/*
   1123  1.46    mjacob 	 * Now figure whether we should print any additional informtion.
   1124  1.46    mjacob 	 *
   1125  1.46    mjacob 	 * Where should we start from? If we had SKSV data,
   1126  1.46    mjacob 	 * start from offset 18, else from offset 15.
   1127  1.46    mjacob 	 *
   1128  1.46    mjacob 	 * From that point until the end of the buffer, check for any
   1129  1.46    mjacob 	 * nonzero data. If we have some, go back and print the lot,
   1130  1.46    mjacob 	 * otherwise we're done.
   1131  1.46    mjacob 	 */
   1132  1.46    mjacob 	if (sbs) {
   1133  1.46    mjacob 		i = 18;
   1134  1.46    mjacob 	} else {
   1135  1.46    mjacob 		i = 15;
   1136  1.46    mjacob 	}
   1137  1.46    mjacob 	for (j = i; j < sizeof (xs->sense); j++) {
   1138  1.46    mjacob 		if (s[j])
   1139  1.46    mjacob 			break;
   1140  1.46    mjacob 	}
   1141  1.46    mjacob 	if (j == sizeof (xs->sense))
   1142  1.46    mjacob 		return;
   1143  1.46    mjacob 
   1144  1.46    mjacob 	printf("\n Additional Sense Information (byte %d out...):\n", i);
   1145  1.46    mjacob 	if (i == 15) {
   1146  1.46    mjacob 		printf("\n\t%2d:", i);
   1147  1.46    mjacob 		k = 7;
   1148  1.46    mjacob 	} else {
   1149  1.46    mjacob 		printf("\n\t%2d:", i);
   1150  1.46    mjacob 		k = 2;
   1151  1.46    mjacob 		j -= 2;
   1152  1.46    mjacob 	}
   1153  1.46    mjacob 	while (j > 0) {
   1154  1.46    mjacob 		if (i >= sizeof (xs->sense))
   1155  1.46    mjacob 			break;
   1156  1.46    mjacob 		if (k == 8) {
   1157  1.46    mjacob 			k = 0;
   1158  1.46    mjacob 			printf("\n\t%2d:", i);
   1159  1.46    mjacob 		}
   1160  1.46    mjacob 		printf(" 0x%02x", s[i] & 0xff);
   1161  1.46    mjacob 		k++;
   1162  1.46    mjacob 		j--;
   1163  1.46    mjacob 		i++;
   1164  1.46    mjacob 	}
   1165  1.46    mjacob 	printf("\n\n");
   1166  1.46    mjacob }
   1167  1.46    mjacob 
   1168  1.46    mjacob char *
   1169  1.46    mjacob scsi_decode_sense(void *sinfo, int flag)
   1170  1.46    mjacob {
   1171  1.46    mjacob 	unsigned char *snsbuf;
   1172  1.46    mjacob 	unsigned char skey;
   1173  1.46    mjacob 	static char rqsbuf[132];
   1174  1.46    mjacob 
   1175  1.46    mjacob 	skey = 0;
   1176  1.46    mjacob 
   1177  1.46    mjacob 	snsbuf = (unsigned char *) sinfo;
   1178  1.46    mjacob 	if (flag == 0 || flag == 2 || flag == 3) {
   1179  1.46    mjacob 		skey = snsbuf[2] & 0xf;
   1180  1.46    mjacob 	}
   1181  1.46    mjacob 	if (flag == 0) {		/* Sense Key Only */
   1182  1.46    mjacob 		(void) strcpy(rqsbuf, sense_keys[skey]);
   1183  1.46    mjacob 		return (rqsbuf);
   1184  1.46    mjacob 	} else if (flag == 1) {		/* ASC/ASCQ Only */
   1185  1.46    mjacob 		asc2ascii(snsbuf[12], snsbuf[13], rqsbuf);
   1186  1.46    mjacob 		return (rqsbuf);
   1187  1.46    mjacob 	} else  if (flag == 2) {	/* Sense Key && ASC/ASCQ */
   1188  1.46    mjacob 		auto char localbuf[64];
   1189  1.46    mjacob 		asc2ascii(snsbuf[12], snsbuf[13], localbuf);
   1190  1.46    mjacob 		(void) sprintf(rqsbuf, "%s, %s", sense_keys[skey], localbuf);
   1191  1.46    mjacob 		return (rqsbuf);
   1192  1.46    mjacob 	} else if (flag == 3  && snsbuf[7] >= 9 && (snsbuf[15] & 0x80)) {
   1193  1.46    mjacob 		/*
   1194  1.46    mjacob 		 * SKSV Data
   1195  1.46    mjacob 		 */
   1196  1.46    mjacob 		switch (skey) {
   1197  1.46    mjacob 		case 0x5:	/* Illegal Request */
   1198  1.46    mjacob 			if (snsbuf[15] & 0x8) {
   1199  1.46    mjacob 				(void) sprintf(rqsbuf,
   1200  1.46    mjacob 				    "Error in %s, Offset %d, bit %d",
   1201  1.46    mjacob 				    (snsbuf[15] & 0x40)? "CDB" : "Parameters",
   1202  1.46    mjacob 				    (snsbuf[16] & 0xff) << 8 |
   1203  1.46    mjacob 				    (snsbuf[17] & 0xff), snsbuf[15] & 0xf);
   1204  1.46    mjacob 			} else {
   1205  1.46    mjacob 				(void) sprintf(rqsbuf,
   1206  1.46    mjacob 				    "Error in %s, Offset %d",
   1207  1.46    mjacob 				    (snsbuf[15] & 0x40)? "CDB" : "Parameters",
   1208  1.46    mjacob 				    (snsbuf[16] & 0xff) << 8 |
   1209  1.46    mjacob 				    (snsbuf[17] & 0xff));
   1210  1.46    mjacob 			}
   1211  1.46    mjacob 			return (rqsbuf);
   1212  1.46    mjacob 		case 0x1:
   1213  1.46    mjacob 		case 0x3:
   1214  1.46    mjacob 		case 0x4:
   1215  1.46    mjacob 			(void) sprintf(rqsbuf, "Actual Retry Count: %d",
   1216  1.46    mjacob 			    (snsbuf[16] & 0xff) << 8 | (snsbuf[17] & 0xff));
   1217  1.46    mjacob 			return (rqsbuf);
   1218  1.46    mjacob 		case 0x2:
   1219  1.46    mjacob 			(void) sprintf(rqsbuf, "Progress Indicator: %d",
   1220  1.46    mjacob 			    (snsbuf[16] & 0xff) << 8 | (snsbuf[17] & 0xff));
   1221  1.46    mjacob 			return (rqsbuf);
   1222  1.46    mjacob 		default:
   1223  1.46    mjacob 			break;
   1224  1.46    mjacob 		}
   1225  1.46    mjacob 	}
   1226  1.46    mjacob 	return ((char *) 0);
   1227  1.46    mjacob }
   1228  1.46    mjacob 
   1229  1.46    mjacob #endif
   1230   1.1   mycroft #ifdef	SCSIDEBUG
   1231   1.1   mycroft /*
   1232   1.1   mycroft  * Given a scsi_xfer, dump the request, in all it's glory
   1233   1.1   mycroft  */
   1234   1.1   mycroft void
   1235   1.1   mycroft show_scsi_xs(xs)
   1236   1.1   mycroft 	struct scsi_xfer *xs;
   1237   1.1   mycroft {
   1238  1.40  christos 	printf("xs(%p): ", xs);
   1239  1.40  christos 	printf("flg(0x%x)", xs->flags);
   1240  1.40  christos 	printf("sc_link(%p)", xs->sc_link);
   1241  1.40  christos 	printf("retr(0x%x)", xs->retries);
   1242  1.40  christos 	printf("timo(0x%x)", xs->timeout);
   1243  1.40  christos 	printf("cmd(%p)", xs->cmd);
   1244  1.40  christos 	printf("len(0x%x)", xs->cmdlen);
   1245  1.40  christos 	printf("data(%p)", xs->data);
   1246  1.40  christos 	printf("len(0x%x)", xs->datalen);
   1247  1.40  christos 	printf("res(0x%x)", xs->resid);
   1248  1.40  christos 	printf("err(0x%x)", xs->error);
   1249  1.40  christos 	printf("bp(%p)", xs->bp);
   1250   1.1   mycroft 	show_scsi_cmd(xs);
   1251   1.1   mycroft }
   1252   1.1   mycroft 
   1253   1.1   mycroft void
   1254  1.15   deraadt show_scsi_cmd(xs)
   1255  1.15   deraadt 	struct scsi_xfer *xs;
   1256   1.1   mycroft {
   1257   1.1   mycroft 	u_char *b = (u_char *) xs->cmd;
   1258   1.1   mycroft 	int     i = 0;
   1259   1.1   mycroft 
   1260   1.1   mycroft 	sc_print_addr(xs->sc_link);
   1261  1.40  christos 	printf("command: ");
   1262   1.1   mycroft 
   1263  1.22   mycroft 	if ((xs->flags & SCSI_RESET) == 0) {
   1264   1.1   mycroft 		while (i < xs->cmdlen) {
   1265   1.1   mycroft 			if (i)
   1266  1.40  christos 				printf(",");
   1267  1.45      fair 			printf("0x%x", b[i++]);
   1268   1.1   mycroft 		}
   1269  1.40  christos 		printf("-[%d bytes]\n", xs->datalen);
   1270   1.1   mycroft 		if (xs->datalen)
   1271   1.1   mycroft 			show_mem(xs->data, min(64, xs->datalen));
   1272   1.2   mycroft 	} else
   1273  1.40  christos 		printf("-RESET-\n");
   1274   1.1   mycroft }
   1275   1.1   mycroft 
   1276   1.1   mycroft void
   1277   1.1   mycroft show_mem(address, num)
   1278  1.24   mycroft 	u_char *address;
   1279  1.24   mycroft 	int num;
   1280   1.1   mycroft {
   1281  1.24   mycroft 	int x;
   1282  1.24   mycroft 
   1283  1.40  christos 	printf("------------------------------");
   1284  1.24   mycroft 	for (x = 0; x < num; x++) {
   1285  1.24   mycroft 		if ((x % 16) == 0)
   1286  1.40  christos 			printf("\n%03d: ", x);
   1287  1.40  christos 		printf("%02x ", *address++);
   1288   1.1   mycroft 	}
   1289  1.40  christos 	printf("\n------------------------------\n");
   1290   1.1   mycroft }
   1291   1.1   mycroft #endif /*SCSIDEBUG */
   1292