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