Home | History | Annotate | Line # | Download | only in scsipi
scsi_base.c revision 1.31
      1 /*	$NetBSD: scsi_base.c,v 1.31 1996/01/12 22:43:29 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1994, 1995 Charles 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 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  * Originally written by Julian Elischer (julian (at) dialix.oz.au)
     34  */
     35 
     36 #include <sys/types.h>
     37 #include <sys/param.h>
     38 #include <sys/systm.h>
     39 #include <sys/kernel.h>
     40 #include <sys/buf.h>
     41 #include <sys/uio.h>
     42 #include <sys/malloc.h>
     43 #include <sys/errno.h>
     44 #include <sys/device.h>
     45 
     46 #include <scsi/scsi_all.h>
     47 #include <scsi/scsi_disk.h>
     48 #include <scsi/scsiconf.h>
     49 
     50 void scsi_error __P((struct scsi_xfer *, int));
     51 
     52 LIST_HEAD(xs_free_list, scsi_xfer) xs_free_list;
     53 
     54 /*
     55  * Get a scsi transfer structure for the caller. Charge the structure
     56  * to the device that is referenced by the sc_link structure. If the
     57  * sc_link structure has no 'credits' then the device already has the
     58  * maximum number or outstanding operations under way. In this stage,
     59  * wait on the structure so that when one is freed, we are awoken again
     60  * If the SCSI_NOSLEEP flag is set, then do not wait, but rather, return
     61  * a NULL pointer, signifying that no slots were available
     62  * Note in the link structure, that we are waiting on it.
     63  */
     64 
     65 struct scsi_xfer *
     66 scsi_get_xs(sc_link, flags)
     67 	struct scsi_link *sc_link;	/* who to charge the xs to */
     68 	int flags;			/* if this call can sleep */
     69 {
     70 	struct scsi_xfer *xs;
     71 	int s;
     72 
     73 	SC_DEBUG(sc_link, SDEV_DB3, ("scsi_get_xs\n"));
     74 	s = splbio();
     75 	while (sc_link->openings <= 0) {
     76 		SC_DEBUG(sc_link, SDEV_DB3, ("sleeping\n"));
     77 		if ((flags & SCSI_NOSLEEP) != 0) {
     78 			splx(s);
     79 			return 0;
     80 		}
     81 		sc_link->flags |= SDEV_WAITING;
     82 		(void) tsleep(sc_link, PRIBIO, "getxs", 0);
     83 	}
     84 	sc_link->openings--;
     85 	if (xs = xs_free_list.lh_first) {
     86 		LIST_REMOVE(xs, free_list);
     87 		splx(s);
     88 	} else {
     89 		splx(s);
     90 		SC_DEBUG(sc_link, SDEV_DB3, ("making\n"));
     91 		xs = malloc(sizeof(*xs), M_DEVBUF,
     92 		    ((flags & SCSI_NOSLEEP) != 0 ? M_NOWAIT : M_WAITOK));
     93 		if (!xs) {
     94 			sc_print_addr(sc_link);
     95 			printf("cannot allocate scsi xs\n");
     96 			return 0;
     97 		}
     98 	}
     99 
    100 	SC_DEBUG(sc_link, SDEV_DB3, ("returning\n"));
    101 	xs->flags = INUSE | flags;
    102 	return xs;
    103 }
    104 
    105 /*
    106  * Given a scsi_xfer struct, and a device (referenced through sc_link)
    107  * return the struct to the free pool and credit the device with it
    108  * If another process is waiting for an xs, do a wakeup, let it proceed
    109  */
    110 void
    111 scsi_free_xs(xs, flags)
    112 	struct scsi_xfer *xs;
    113 	int flags;
    114 {
    115 	struct scsi_link *sc_link = xs->sc_link;
    116 
    117 	xs->flags &= ~INUSE;
    118 	LIST_INSERT_HEAD(&xs_free_list, xs, free_list);
    119 
    120 	SC_DEBUG(sc_link, SDEV_DB3, ("scsi_free_xs\n"));
    121 	/* if was 0 and someone waits, wake them up */
    122 	sc_link->openings++;
    123 	if ((sc_link->flags & SDEV_WAITING) != 0) {
    124 		sc_link->flags &= ~SDEV_WAITING;
    125 		wakeup(sc_link);
    126 	} else {
    127 		if (sc_link->device->start) {
    128 			SC_DEBUG(sc_link, SDEV_DB2, ("calling private start()\n"));
    129 			(*(sc_link->device->start)) (sc_link->device_softc);
    130 		}
    131 	}
    132 }
    133 
    134 /*
    135  * Make a scsi_xfer, and return a pointer to it.
    136  */
    137 static __inline struct scsi_xfer *
    138 scsi_make_xs(sc_link, scsi_cmd, cmdlen, data_addr, datalen,
    139     retries, timeout, bp, flags)
    140 	struct scsi_link *sc_link;
    141 	struct scsi_generic *scsi_cmd;
    142 	int cmdlen;
    143 	u_char *data_addr;
    144 	int datalen;
    145 	int retries;
    146 	int timeout;
    147 	struct buf *bp;
    148 	int flags;
    149 {
    150 	struct scsi_xfer *xs;
    151 
    152 	if ((xs = scsi_get_xs(sc_link, flags)) == NULL)
    153 		return NULL;
    154 
    155 	/*
    156 	 * Fill out the scsi_xfer structure.  We don't know whose context
    157 	 * the cmd is in, so copy it.
    158 	 */
    159 	xs->sc_link = sc_link;
    160 	bcopy(scsi_cmd, &xs->cmdstore, cmdlen);
    161 	xs->cmd = &xs->cmdstore;
    162 	xs->cmdlen = cmdlen;
    163 	xs->data = data_addr;
    164 	xs->datalen = datalen;
    165 	xs->retries = retries;
    166 	xs->timeout = timeout;
    167 	xs->bp = bp;
    168 
    169 	return xs;
    170 }
    171 
    172 /*
    173  * Find out from the device what its capacity is.
    174  */
    175 u_long
    176 scsi_size(sc_link, flags)
    177 	struct scsi_link *sc_link;
    178 	int flags;
    179 {
    180 	struct scsi_read_cap_data rdcap;
    181 	struct scsi_read_capacity scsi_cmd;
    182 	u_long size;
    183 
    184 	/*
    185 	 * make up a scsi command and ask the scsi driver to do
    186 	 * it for you.
    187 	 */
    188 	bzero(&scsi_cmd, sizeof(scsi_cmd));
    189 	scsi_cmd.opcode = READ_CAPACITY;
    190 
    191 	/*
    192 	 * If the command works, interpret the result as a 4 byte
    193 	 * number of blocks
    194 	 */
    195 	if (scsi_scsi_cmd(sc_link, (struct scsi_generic *)&scsi_cmd,
    196 			  sizeof(scsi_cmd), (u_char *)&rdcap, sizeof(rdcap),
    197 			  2, 20000, NULL, flags | SCSI_DATA_IN) != 0) {
    198 		sc_print_addr(sc_link);
    199 		printf("could not get size\n");
    200 		return 0;
    201 	} else {
    202 		size = rdcap.addr_0 + 1;
    203 		size += rdcap.addr_1 << 8;
    204 		size += rdcap.addr_2 << 16;
    205 		size += rdcap.addr_3 << 24;
    206 	}
    207 	return size;
    208 }
    209 
    210 /*
    211  * Get scsi driver to send a "are you ready?" command
    212  */
    213 int
    214 scsi_test_unit_ready(sc_link, flags)
    215 	struct scsi_link *sc_link;
    216 	int flags;
    217 {
    218 	struct scsi_test_unit_ready scsi_cmd;
    219 
    220 	bzero(&scsi_cmd, sizeof(scsi_cmd));
    221 	scsi_cmd.opcode = TEST_UNIT_READY;
    222 
    223 	return scsi_scsi_cmd(sc_link, (struct scsi_generic *) &scsi_cmd,
    224 			     sizeof(scsi_cmd), 0, 0, 2, 10000, NULL, flags);
    225 }
    226 
    227 /*
    228  * Do a scsi operation, asking a device to run as SCSI-II if it can.
    229  */
    230 int
    231 scsi_change_def(sc_link, flags)
    232 	struct scsi_link *sc_link;
    233 	int flags;
    234 {
    235 	struct scsi_changedef scsi_cmd;
    236 
    237 	bzero(&scsi_cmd, sizeof(scsi_cmd));
    238 	scsi_cmd.opcode = CHANGE_DEFINITION;
    239 	scsi_cmd.how = SC_SCSI_2;
    240 
    241 	return scsi_scsi_cmd(sc_link, (struct scsi_generic *) &scsi_cmd,
    242 			     sizeof(scsi_cmd), 0, 0, 2, 100000, NULL, flags);
    243 }
    244 
    245 /*
    246  * Do a scsi operation asking a device what it is
    247  * Use the scsi_cmd routine in the switch table.
    248  */
    249 int
    250 scsi_inquire(sc_link, inqbuf, flags)
    251 	struct scsi_link *sc_link;
    252 	struct scsi_inquiry_data *inqbuf;
    253 	int flags;
    254 {
    255 	struct scsi_inquiry scsi_cmd;
    256 
    257 	bzero(&scsi_cmd, sizeof(scsi_cmd));
    258 	scsi_cmd.opcode = INQUIRY;
    259 	scsi_cmd.length = sizeof(struct scsi_inquiry_data);
    260 
    261 	return scsi_scsi_cmd(sc_link, (struct scsi_generic *) &scsi_cmd,
    262 			     sizeof(scsi_cmd), (u_char *) inqbuf,
    263 			     sizeof(struct scsi_inquiry_data), 2, 10000, NULL,
    264 			     SCSI_DATA_IN | flags);
    265 }
    266 
    267 /*
    268  * Prevent or allow the user to remove the media
    269  */
    270 int
    271 scsi_prevent(sc_link, type, flags)
    272 	struct scsi_link *sc_link;
    273 	int type, flags;
    274 {
    275 	struct scsi_prevent scsi_cmd;
    276 
    277 	bzero(&scsi_cmd, sizeof(scsi_cmd));
    278 	scsi_cmd.opcode = PREVENT_ALLOW;
    279 	scsi_cmd.how = type;
    280 	return scsi_scsi_cmd(sc_link, (struct scsi_generic *) &scsi_cmd,
    281 			     sizeof(scsi_cmd), 0, 0, 2, 5000, NULL, flags);
    282 }
    283 
    284 /*
    285  * Get scsi driver to send a "start up" command
    286  */
    287 int
    288 scsi_start(sc_link, type, flags)
    289 	struct scsi_link *sc_link;
    290 	int type, flags;
    291 {
    292 	struct scsi_start_stop scsi_cmd;
    293 
    294 	bzero(&scsi_cmd, sizeof(scsi_cmd));
    295 	scsi_cmd.opcode = START_STOP;
    296 	scsi_cmd.byte2 = 0x00;
    297 	scsi_cmd.how = type;
    298 	return scsi_scsi_cmd(sc_link, (struct scsi_generic *) &scsi_cmd,
    299 			     sizeof(scsi_cmd), 0, 0, 2,
    300 			     type == SSS_START ? 30000 : 10000, NULL, flags);
    301 }
    302 
    303 /*
    304  * This routine is called by the scsi interrupt when the transfer is complete.
    305  */
    306 void
    307 scsi_done(xs)
    308 	struct scsi_xfer *xs;
    309 {
    310 	struct scsi_link *sc_link = xs->sc_link;
    311 	int error;
    312 
    313 	SC_DEBUG(sc_link, SDEV_DB2, ("scsi_done\n"));
    314 #ifdef	SCSIDEBUG
    315 	if ((sc_link->flags & SDEV_DB1) != 0)
    316 		show_scsi_cmd(xs);
    317 #endif /* SCSIDEBUG */
    318 
    319 	/*
    320  	 * If it's a user level request, bypass all usual completion processing,
    321  	 * let the user work it out.. We take reponsibility for freeing the
    322  	 * xs when the user returns. (and restarting the device's queue).
    323  	 */
    324 	if ((xs->flags & SCSI_USER) != 0) {
    325 		SC_DEBUG(sc_link, SDEV_DB3, ("calling user done()\n"));
    326 		scsi_user_done(xs); /* to take a copy of the sense etc. */
    327 		SC_DEBUG(sc_link, SDEV_DB3, ("returned from user done()\n "));
    328 
    329 		scsi_free_xs(xs, SCSI_NOSLEEP); /* restarts queue too */
    330 		SC_DEBUG(sc_link, SDEV_DB3, ("returning to adapter\n"));
    331 		return;
    332 	}
    333 
    334 	/*
    335 	 * If the device has it's own done routine, call it first.
    336 	 * If it returns a legit error value, return that, otherwise
    337 	 * it wants us to continue with normal processing.
    338 	 *
    339 	 * Make sure the upper-level driver knows that this might not
    340 	 * actually be the last time they hear from us.  We need to get
    341 	 * status back.
    342 	 */
    343 	if (sc_link->device->done) {
    344 		SC_DEBUG(sc_link, SDEV_DB2, ("calling private done()\n"));
    345 		error = (*sc_link->device->done)(xs, 0);
    346 		if (error == EJUSTRETURN)
    347 			goto done;
    348 		SC_DEBUG(sc_link, SDEV_DB3, ("continuing with generic done()\n"));
    349 	}
    350 	if (xs->bp == NULL) {
    351 		/*
    352 		 * if it's a normal upper level request, then ask
    353 		 * the upper level code to handle error checking
    354 		 * rather than doing it here at interrupt time
    355 		 */
    356 		wakeup(xs);
    357 		return;
    358 	}
    359 	/*
    360 	 * Go and handle errors now.
    361 	 * If it returns ERESTART then we should RETRY
    362 	 */
    363 retry:
    364 	if (sc_err1(xs, 1) == ERESTART) {
    365 		switch ((*(sc_link->adapter->scsi_cmd)) (xs)) {
    366 		case SUCCESSFULLY_QUEUED:
    367 			return;
    368 
    369 		case TRY_AGAIN_LATER:
    370 			xs->error = XS_BUSY;
    371 		case COMPLETE:
    372 			goto retry;
    373 		}
    374 	}
    375 done:
    376 	if (sc_link->device->done) {
    377 		/*
    378 		 * Tell the device the operation is actually complete.
    379 		 * No more will happen with this xfer.  This for
    380 		 * notification of the upper-level driver only; they
    381 		 * won't be returning any meaningful information to us.
    382 		 */
    383 		(void)(*sc_link->device->done)(xs, 1);
    384 	}
    385 	scsi_free_xs(xs, SCSI_NOSLEEP);
    386 }
    387 
    388 int
    389 scsi_execute_xs(xs)
    390 	struct scsi_xfer *xs;
    391 {
    392 	int error;
    393 	int s;
    394 
    395 	xs->flags &= ~ITSDONE;
    396 	xs->error = XS_NOERROR;
    397 	xs->resid = xs->datalen;
    398 
    399 retry:
    400 	/*
    401 	 * Do the transfer. If we are polling we will return:
    402 	 * COMPLETE,  Was poll, and scsi_done has been called
    403 	 * TRY_AGAIN_LATER, Adapter short resources, try again
    404 	 *
    405 	 * if under full steam (interrupts) it will return:
    406 	 * SUCCESSFULLY_QUEUED, will do a wakeup when complete
    407 	 * TRY_AGAIN_LATER, (as for polling)
    408 	 * After the wakeup, we must still check if it succeeded
    409 	 *
    410 	 * If we have a bp however, all the error proccessing
    411 	 * and the buffer code both expect us to return straight
    412 	 * to them, so as soon as the command is queued, return
    413 	 */
    414 	switch ((*(xs->sc_link->adapter->scsi_cmd)) (xs)) {
    415 	case SUCCESSFULLY_QUEUED:
    416 		if (xs->bp)
    417 			return EJUSTRETURN;
    418 		s = splbio();
    419 		while ((xs->flags & ITSDONE) == 0)
    420 			tsleep(xs, PRIBIO + 1, "scsi_scsi_cmd", 0);
    421 		splx(s);
    422 	case COMPLETE:		/* Polling command completed ok */
    423 		if (xs->bp)
    424 			return EJUSTRETURN;
    425 	doit:
    426 		SC_DEBUG(xs->sc_link, SDEV_DB3, ("back in cmd()\n"));
    427 		if ((error = sc_err1(xs, 0)) != ERESTART)
    428 			return error;
    429 		goto retry;
    430 
    431 	case TRY_AGAIN_LATER:	/* adapter resource shortage */
    432 		xs->error = XS_BUSY;
    433 		goto doit;
    434 
    435 	default:
    436 		panic("scsi_execute_xs: invalid return code");
    437 	}
    438 
    439 #ifdef DIAGNOSTIC
    440 	panic("scsi_execute_xs: impossible");
    441 #endif
    442 }
    443 
    444 /*
    445  * ask the scsi driver to perform a command for us.
    446  * tell it where to read/write the data, and how
    447  * long the data is supposed to be. If we have  a buf
    448  * to associate with the transfer, we need that too.
    449  */
    450 int
    451 scsi_scsi_cmd(sc_link, scsi_cmd, cmdlen, data_addr, datalen,
    452     retries, timeout, bp, flags)
    453 	struct scsi_link *sc_link;
    454 	struct scsi_generic *scsi_cmd;
    455 	int cmdlen;
    456 	u_char *data_addr;
    457 	int datalen;
    458 	int retries;
    459 	int timeout;
    460 	struct buf *bp;
    461 	int flags;
    462 {
    463 	struct scsi_xfer *xs;
    464 	int error;
    465 
    466 	SC_DEBUG(sc_link, SDEV_DB2, ("scsi_cmd\n"));
    467 
    468 #ifdef DIAGNOSTIC
    469 	if (bp != 0 && (flags & SCSI_NOSLEEP) == 0)
    470 		panic("scsi_scsi_cmd: buffer without nosleep");
    471 #endif
    472 
    473 	if ((xs = scsi_make_xs(sc_link, scsi_cmd, cmdlen, data_addr, datalen,
    474 	    retries, timeout, bp, flags)) == NULL)
    475 		return ENOMEM;
    476 
    477 	if ((error = scsi_execute_xs(xs)) == EJUSTRETURN)
    478 		return 0;
    479 
    480 	/*
    481 	 * we have finished with the xfer stuct, free it and
    482 	 * check if anyone else needs to be started up.
    483 	 */
    484 	scsi_free_xs(xs, flags);
    485 	return error;
    486 }
    487 
    488 int
    489 sc_err1(xs, async)
    490 	struct scsi_xfer *xs;
    491 	int async;
    492 {
    493 	int error;
    494 
    495 	SC_DEBUG(xs->sc_link, SDEV_DB3, ("sc_err1,err = 0x%x \n", xs->error));
    496 
    497 	/*
    498 	 * If it has a buf, we might be working with
    499 	 * a request from the buffer cache or some other
    500 	 * piece of code that requires us to process
    501 	 * errors at inetrrupt time. We have probably
    502 	 * been called by scsi_done()
    503 	 */
    504 	switch (xs->error) {
    505 	case XS_NOERROR:	/* nearly always hit this one */
    506 		error = 0;
    507 		break;
    508 
    509 	case XS_SENSE:
    510 		if ((error = scsi_interpret_sense(xs)) == ERESTART)
    511 			goto retry;
    512 		SC_DEBUG(xs->sc_link, SDEV_DB3,
    513 		    ("scsi_interpret_sense returned %d\n", error));
    514 		break;
    515 
    516 	case XS_BUSY:
    517 		if (xs->retries) {
    518 			if ((xs->flags & SCSI_POLL) != 0)
    519 				delay(1000000);
    520 			else if ((xs->flags & SCSI_NOSLEEP) == 0)
    521 				tsleep(&lbolt, PRIBIO, "scbusy", 0);
    522 			else
    523 #if 0
    524 				timeout(scsi_requeue, xs, hz);
    525 #else
    526 				goto lose;
    527 #endif
    528 		}
    529 	case XS_TIMEOUT:
    530 	retry:
    531 		if (xs->retries--) {
    532 			xs->error = XS_NOERROR;
    533 			xs->flags &= ~ITSDONE;
    534 			return ERESTART;
    535 		}
    536 	case XS_DRIVER_STUFFUP:
    537 	lose:
    538 		error = EIO;
    539 		break;
    540 
    541 	case XS_SELTIMEOUT:
    542 		/* XXX Disable device? */
    543 		error = EIO;
    544 		break;
    545 
    546 	default:
    547 		sc_print_addr(xs->sc_link);
    548 		printf("unknown error category from scsi driver\n");
    549 		error = EIO;
    550 		break;
    551 	}
    552 
    553 	scsi_error(xs, error);
    554 	return error;
    555 }
    556 
    557 void
    558 scsi_error(xs, error)
    559 	struct scsi_xfer *xs;
    560 	int error;
    561 {
    562 	struct buf *bp = xs->bp;
    563 
    564 	if (bp) {
    565 		if (error) {
    566 			bp->b_error = error;
    567 			bp->b_flags |= B_ERROR;
    568 			bp->b_resid = bp->b_bcount;
    569 		} else {
    570 			bp->b_error = 0;
    571 			bp->b_resid = xs->resid;
    572 		}
    573 		biodone(bp);
    574 	}
    575 }
    576 
    577 /*
    578  * Look at the returned sense and act on the error, determining
    579  * the unix error number to pass back.  (0 = report no error)
    580  *
    581  * THIS IS THE DEFAULT ERROR HANDLER
    582  */
    583 int
    584 scsi_interpret_sense(xs)
    585 	struct scsi_xfer *xs;
    586 {
    587 	struct scsi_sense_data *sense;
    588 	struct scsi_link *sc_link = xs->sc_link;
    589 	u_int8_t key;
    590 	u_int32_t info;
    591 	int error;
    592 
    593 	static char *error_mes[] = {
    594 		"soft error (corrected)",
    595 		"not ready", "medium error",
    596 		"non-media hardware failure", "illegal request",
    597 		"unit attention", "readonly device",
    598 		"no data found", "vendor unique",
    599 		"copy aborted", "command aborted",
    600 		"search returned equal", "volume overflow",
    601 		"verify miscompare", "unknown error key"
    602 	};
    603 
    604 	sense = &xs->sense;
    605 #ifdef	SCSIDEBUG
    606 	if ((sc_link->flags & SDEV_DB1) != 0) {
    607 		int count;
    608 		printf("code%x valid%x ",
    609 		    sense->error_code & SSD_ERRCODE,
    610 		    sense->error_code & SSD_ERRCODE_VALID ? 1 : 0);
    611 		printf("seg%x key%x ili%x eom%x fmark%x\n",
    612 		    sense->extended_segment,
    613 		    sense->extended_flags & SSD_KEY,
    614 		    sense->extended_flags & SSD_ILI ? 1 : 0,
    615 		    sense->extended_flags & SSD_EOM ? 1 : 0,
    616 		    sense->extended_flags & SSD_FILEMARK ? 1 : 0);
    617 		printf("info: %x %x %x %x followed by %d extra bytes\n",
    618 		    sense->extended_info[0],
    619 		    sense->extended_info[1],
    620 		    sense->extended_info[2],
    621 		    sense->extended_info[3],
    622 		    sense->extended_extra_len);
    623 		printf("extra: ");
    624 		for (count = 0; count < sense->extended_extra_len; count++)
    625 			printf("%x ", sense->extended_extra_bytes[count]);
    626 		printf("\n");
    627 	}
    628 #endif	/*SCSIDEBUG */
    629 	/*
    630 	 * If the device has it's own error handler, call it first.
    631 	 * If it returns a legit error value, return that, otherwise
    632 	 * it wants us to continue with normal error processing.
    633 	 */
    634 	if (sc_link->device->err_handler) {
    635 		SC_DEBUG(sc_link, SDEV_DB2, ("calling private err_handler()\n"));
    636 		error = (*sc_link->device->err_handler) (xs);
    637 		if (error != -1)
    638 			return error;		/* error >= 0  better ? */
    639 	}
    640 	/* otherwise use the default */
    641 	switch (sense->error_code & SSD_ERRCODE) {
    642 		/*
    643 		 * If it's code 70, use the extended stuff and interpret the key
    644 		 */
    645 	case 0x71:		/* delayed error */
    646 		sc_print_addr(sc_link);
    647 		key = sense->extended_flags & SSD_KEY;
    648 		printf(" DELAYED ERROR, key = 0x%x\n", key);
    649 	case 0x70:
    650 		if ((sense->error_code & SSD_ERRCODE_VALID) != 0) {
    651 			bcopy(sense->extended_info, &info, sizeof info);
    652 			info = ntohl(info);
    653 		} else
    654 			info = 0;
    655 		key = sense->extended_flags & SSD_KEY;
    656 
    657 		switch (key) {
    658 		case 0x0:	/* NO SENSE */
    659 		case 0x1:	/* RECOVERED ERROR */
    660 			if (xs->resid == xs->datalen)
    661 				xs->resid = 0;	/* not short read */
    662 		case 0xc:	/* EQUAL */
    663 			error = 0;
    664 			break;
    665 		case 0x2:	/* NOT READY */
    666 			if ((sc_link->flags & SDEV_REMOVABLE) != 0)
    667 				sc_link->flags &= ~SDEV_MEDIA_LOADED;
    668 			if ((xs->flags & SCSI_IGNORE_NOT_READY) != 0)
    669 				return 0;
    670 			if ((xs->flags & SCSI_SILENT) != 0)
    671 				return EIO;
    672 			error = EIO;
    673 			break;
    674 		case 0x5:	/* ILLEGAL REQUEST */
    675 			if ((xs->flags & SCSI_IGNORE_ILLEGAL_REQUEST) != 0)
    676 				return 0;
    677 			error = EINVAL;
    678 			break;
    679 		case 0x6:	/* UNIT ATTENTION */
    680 			if ((sc_link->flags & SDEV_REMOVABLE) != 0)
    681 				sc_link->flags &= ~SDEV_MEDIA_LOADED;
    682 			if ((xs->flags & SCSI_IGNORE_MEDIA_CHANGE) != 0 ||
    683 			    /* XXX Should reupload any transient state. */
    684 			    (sc_link->flags & SDEV_REMOVABLE) == 0)
    685 				return ERESTART;
    686 			if ((xs->flags & SCSI_SILENT) != 0)
    687 				return EIO;
    688 			error = EIO;
    689 			break;
    690 		case 0x7:	/* DATA PROTECT */
    691 			error = EACCES;
    692 			break;
    693 		case 0x8:	/* BLANK CHECK */
    694 			error = 0;
    695 			break;
    696 		case 0xd:	/* VOLUME OVERFLOW */
    697 			error = ENOSPC;
    698 			break;
    699 		default:
    700 			error = EIO;
    701 			break;
    702 		}
    703 
    704 		if (key) {
    705 			sc_print_addr(sc_link);
    706 			printf("%s", error_mes[key - 1]);
    707 			if ((sense->error_code & SSD_ERRCODE_VALID) != 0) {
    708 				switch (key) {
    709 				case 0x2:	/* NOT READY */
    710 				case 0x5:	/* ILLEGAL REQUEST */
    711 				case 0x6:	/* UNIT ATTENTION */
    712 				case 0x7:	/* DATA PROTECT */
    713 					break;
    714 				case 0x8:	/* BLANK CHECK */
    715 					printf(", requested size: %d (decimal)",
    716 					    info);
    717 					break;
    718 				default:
    719 					printf(", info = %d (decimal)", info);
    720 				}
    721 			}
    722 			if (sense->extended_extra_len != 0) {
    723 				int n;
    724 				printf(", data =");
    725 				for (n = 0; n < sense->extended_extra_len; n++)
    726 					printf(" %02x", sense->extended_extra_bytes[n]);
    727 			}
    728 			printf("\n");
    729 		}
    730 		return error;
    731 
    732 	/*
    733 	 * Not code 70, just report it
    734 	 */
    735 	default:
    736 		sc_print_addr(sc_link);
    737 		printf("error code %d",
    738 		    sense->error_code & SSD_ERRCODE);
    739 		if ((sense->error_code & SSD_ERRCODE_VALID) != 0) {
    740 			printf(" at block no. %d (decimal)",
    741 			    (sense->XXX_unextended_blockhi << 16) +
    742 			    (sense->XXX_unextended_blockmed << 8) +
    743 			    (sense->XXX_unextended_blocklow));
    744 		}
    745 		printf("\n");
    746 		return EIO;
    747 	}
    748 }
    749 
    750 /*
    751  * Utility routines often used in SCSI stuff
    752  */
    753 
    754 /*
    755  * convert a physical address to 3 bytes,
    756  * MSB at the lowest address,
    757  * LSB at the highest.
    758  */
    759 void
    760 lto3b(val, bytes)
    761 	u_int32_t val;
    762 	u_int8_t *bytes;
    763 {
    764 
    765 	*bytes++ = (val >> 16) & 0xff;
    766 	*bytes++ = (val >> 8) & 0xff;
    767 	*bytes = val & 0xff;
    768 }
    769 
    770 /*
    771  * The reverse of lto3b
    772  */
    773 u_int32_t
    774 _3btol(bytes)
    775 	u_int8_t *bytes;
    776 {
    777 	u_int32_t rc;
    778 
    779 	rc = (*bytes++ << 16);
    780 	rc += (*bytes++ << 8);
    781 	rc += *bytes;
    782 	return (rc);
    783 }
    784 
    785 /*
    786  * Print out the scsi_link structure's address info.
    787  */
    788 void
    789 sc_print_addr(sc_link)
    790 	struct scsi_link *sc_link;
    791 {
    792 
    793 	printf("%s(%s:%d:%d): ",
    794 	    sc_link->device_softc ?
    795 	    ((struct device *)sc_link->device_softc)->dv_xname : "probe",
    796 	    ((struct device *)sc_link->adapter_softc)->dv_xname,
    797 	    sc_link->target, sc_link->lun);
    798 }
    799 
    800 #ifdef	SCSIDEBUG
    801 /*
    802  * Given a scsi_xfer, dump the request, in all it's glory
    803  */
    804 void
    805 show_scsi_xs(xs)
    806 	struct scsi_xfer *xs;
    807 {
    808 	printf("xs(0x%x): ", xs);
    809 	printf("flg(0x%x)", xs->flags);
    810 	printf("sc_link(0x%x)", xs->sc_link);
    811 	printf("retr(0x%x)", xs->retries);
    812 	printf("timo(0x%x)", xs->timeout);
    813 	printf("cmd(0x%x)", xs->cmd);
    814 	printf("len(0x%x)", xs->cmdlen);
    815 	printf("data(0x%x)", xs->data);
    816 	printf("len(0x%x)", xs->datalen);
    817 	printf("res(0x%x)", xs->resid);
    818 	printf("err(0x%x)", xs->error);
    819 	printf("bp(0x%x)", xs->bp);
    820 	show_scsi_cmd(xs);
    821 }
    822 
    823 void
    824 show_scsi_cmd(xs)
    825 	struct scsi_xfer *xs;
    826 {
    827 	u_char *b = (u_char *) xs->cmd;
    828 	int     i = 0;
    829 
    830 	sc_print_addr(xs->sc_link);
    831 	printf("command: ");
    832 
    833 	if ((xs->flags & SCSI_RESET) == 0) {
    834 		while (i < xs->cmdlen) {
    835 			if (i)
    836 				printf(",");
    837 			printf("%x", b[i++]);
    838 		}
    839 		printf("-[%d bytes]\n", xs->datalen);
    840 		if (xs->datalen)
    841 			show_mem(xs->data, min(64, xs->datalen));
    842 	} else
    843 		printf("-RESET-\n");
    844 }
    845 
    846 void
    847 show_mem(address, num)
    848 	u_char *address;
    849 	int num;
    850 {
    851 	int x;
    852 
    853 	printf("------------------------------");
    854 	for (x = 0; x < num; x++) {
    855 		if ((x % 16) == 0)
    856 			printf("\n%03d: ", x);
    857 		printf("%02x ", *address++);
    858 	}
    859 	printf("\n------------------------------\n");
    860 }
    861 #endif /*SCSIDEBUG */
    862