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