Home | History | Annotate | Line # | Download | only in scsipi
scsipi_base.c revision 1.29
      1 /*	$NetBSD: scsipi_base.c,v 1.29 2000/01/17 17:59:48 bouyer Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Charles M. Hannum.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include "opt_scsi.h"
     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/pool.h>
     49 #include <sys/errno.h>
     50 #include <sys/device.h>
     51 #include <sys/proc.h>
     52 
     53 #include <dev/scsipi/scsipi_all.h>
     54 #include <dev/scsipi/scsipi_disk.h>
     55 #include <dev/scsipi/scsipiconf.h>
     56 #include <dev/scsipi/scsipi_base.h>
     57 
     58 struct pool scsipi_xfer_pool;
     59 
     60 int	sc_err1 __P((struct scsipi_xfer *, int));
     61 
     62 /*
     63  * Called when a scsibus is attached to initialize global data.
     64  */
     65 void
     66 scsipi_init()
     67 {
     68 	static int scsipi_init_done;
     69 
     70 	if (scsipi_init_done)
     71 		return;
     72 	scsipi_init_done = 1;
     73 
     74 	/* Initialize the scsipi_xfer pool. */
     75 	pool_init(&scsipi_xfer_pool, sizeof(struct scsipi_xfer), 0,
     76 	    0, 0, "scxspl", 0, NULL, NULL, M_DEVBUF);
     77 }
     78 
     79 /*
     80  * Get a scsipi transfer structure for the caller. Charge the structure
     81  * to the device that is referenced by the sc_link structure. If the
     82  * sc_link structure has no 'credits' then the device already has the
     83  * maximum number or outstanding operations under way. In this stage,
     84  * wait on the structure so that when one is freed, we are awoken again
     85  * If the XS_CTL_NOSLEEP flag is set, then do not wait, but rather, return
     86  * a NULL pointer, signifying that no slots were available
     87  * Note in the link structure, that we are waiting on it.
     88  */
     89 
     90 struct scsipi_xfer *
     91 scsipi_get_xs(sc_link, flags)
     92 	struct scsipi_link *sc_link;	/* who to charge the xs to */
     93 	int flags;			/* if this call can sleep */
     94 {
     95 	struct scsipi_xfer *xs;
     96 	int s;
     97 
     98 	SC_DEBUG(sc_link, SDEV_DB3, ("scsipi_get_xs\n"));
     99 
    100 	/*
    101 	 * If we're cold, make sure we poll.
    102 	 */
    103 	if (cold)
    104 		flags |= XS_CTL_NOSLEEP | XS_CTL_POLL;
    105 
    106 	s = splbio();
    107 	while (sc_link->active >= sc_link->openings) {
    108 		SC_DEBUG(sc_link, SDEV_DB3, ("sleeping\n"));
    109 		if ((flags & XS_CTL_NOSLEEP) != 0) {
    110 			splx(s);
    111 			return (0);
    112 		}
    113 		sc_link->flags |= SDEV_WAITING;
    114 		(void)tsleep(sc_link, PRIBIO, "getxs", 0);
    115 	}
    116 	SC_DEBUG(sc_link, SDEV_DB3, ("calling pool_get\n"));
    117 	xs = pool_get(&scsipi_xfer_pool,
    118 	    ((flags & XS_CTL_NOSLEEP) != 0 ? PR_NOWAIT : PR_WAITOK));
    119 	if (xs != NULL)
    120 		sc_link->active++;
    121 	else {
    122 		(*sc_link->sc_print_addr)(sc_link);
    123 		printf("cannot allocate scsipi xs\n");
    124 	}
    125 	splx(s);
    126 
    127 	SC_DEBUG(sc_link, SDEV_DB3, ("returning\n"));
    128 
    129 	/*
    130 	 * zeroes out the command, as ATAPI may use longer commands
    131 	 * than SCSI
    132 	 */
    133 	if (xs != NULL) {
    134 		xs->xs_control = flags;
    135 		TAILQ_INSERT_TAIL(&sc_link->pending_xfers, xs, device_q);
    136 		bzero(&xs->cmdstore, sizeof(xs->cmdstore));
    137 	}
    138 	return (xs);
    139 }
    140 
    141 /*
    142  * Given a scsipi_xfer struct, and a device (referenced through sc_link)
    143  * return the struct to the free pool and credit the device with it
    144  * If another process is waiting for an xs, do a wakeup, let it proceed
    145  *
    146  * MUST BE CALLED AT splbio()!!
    147  */
    148 void
    149 scsipi_free_xs(xs, flags)
    150 	struct scsipi_xfer *xs;
    151 	int flags;
    152 {
    153 	struct scsipi_link *sc_link = xs->sc_link;
    154 
    155 	TAILQ_REMOVE(&sc_link->pending_xfers, xs, device_q);
    156 	if (TAILQ_FIRST(&sc_link->pending_xfers) == NULL &&
    157 	    (sc_link->flags & SDEV_WAITDRAIN) != 0) {
    158 		sc_link->flags &= ~SDEV_WAITDRAIN;
    159 		wakeup(&sc_link->pending_xfers);
    160 	}
    161 	pool_put(&scsipi_xfer_pool, xs);
    162 
    163 	SC_DEBUG(sc_link, SDEV_DB3, ("scsipi_free_xs\n"));
    164 	/* if was 0 and someone waits, wake them up */
    165 	sc_link->active--;
    166 	if ((sc_link->flags & SDEV_WAITING) != 0) {
    167 		sc_link->flags &= ~SDEV_WAITING;
    168 		wakeup(sc_link);
    169 	} else {
    170 		if (sc_link->device->start) {
    171 			SC_DEBUG(sc_link, SDEV_DB2,
    172 			    ("calling private start()\n"));
    173 			(*(sc_link->device->start))(sc_link->device_softc);
    174 		}
    175 	}
    176 }
    177 
    178 /*
    179  * Wait for a scsipi_link's pending xfers to drain.
    180  */
    181 void
    182 scsipi_wait_drain(sc_link)
    183 	struct scsipi_link *sc_link;
    184 {
    185 	int s;
    186 
    187 	s = splbio();
    188 	while (TAILQ_FIRST(&sc_link->pending_xfers) != NULL) {
    189 		sc_link->flags |= SDEV_WAITDRAIN;
    190 		(void) tsleep(&sc_link->pending_xfers, PRIBIO, "sxdrn", 0);
    191 	}
    192 	splx(s);
    193 }
    194 
    195 /*
    196  * Kill off all pending xfers for a scsipi_link.
    197  *
    198  * Must be called at splbio().
    199  */
    200 void
    201 scsipi_kill_pending(sc_link)
    202 	struct scsipi_link *sc_link;
    203 {
    204 
    205 	(*sc_link->scsipi_kill_pending)(sc_link);
    206 #ifdef DIAGNOSTIC
    207 	if (TAILQ_FIRST(&sc_link->pending_xfers) != NULL)
    208 		panic("scsipi_kill_pending");
    209 #endif
    210 }
    211 
    212 /*
    213  * Look at the returned sense and act on the error, determining
    214  * the unix error number to pass back.  (0 = report no error)
    215  *
    216  * THIS IS THE DEFAULT ERROR HANDLER FOR SCSI DEVICES
    217  */
    218 int
    219 scsipi_interpret_sense(xs)
    220 	struct scsipi_xfer *xs;
    221 {
    222 	struct scsipi_sense_data *sense;
    223 	struct scsipi_link *sc_link = xs->sc_link;
    224 	u_int8_t key;
    225 	u_int32_t info;
    226 	int error;
    227 #ifndef	SCSIVERBOSE
    228 	static char *error_mes[] = {
    229 		"soft error (corrected)",
    230 		"not ready", "medium error",
    231 		"non-media hardware failure", "illegal request",
    232 		"unit attention", "readonly device",
    233 		"no data found", "vendor unique",
    234 		"copy aborted", "command aborted",
    235 		"search returned equal", "volume overflow",
    236 		"verify miscompare", "unknown error key"
    237 	};
    238 #endif
    239 
    240 	sense = &xs->sense.scsi_sense;
    241 #ifdef	SCSIDEBUG
    242 	if ((sc_link->flags & SDEV_DB1) != 0) {
    243 		int count;
    244 		printf("code 0x%x valid 0x%x ",
    245 			sense->error_code & SSD_ERRCODE,
    246 			sense->error_code & SSD_ERRCODE_VALID ? 1 : 0);
    247 		printf("seg 0x%x key 0x%x ili 0x%x eom 0x%x fmark 0x%x\n",
    248 			sense->segment,
    249 			sense->flags & SSD_KEY,
    250 			sense->flags & SSD_ILI ? 1 : 0,
    251 			sense->flags & SSD_EOM ? 1 : 0,
    252 			sense->flags & SSD_FILEMARK ? 1 : 0);
    253 		printf("info: 0x%x 0x%x 0x%x 0x%x followed by %d extra bytes\n",
    254 			sense->info[0],
    255 			sense->info[1],
    256 			sense->info[2],
    257 			sense->info[3],
    258 			sense->extra_len);
    259 		printf("extra: ");
    260 		for (count = 0; count < ADD_BYTES_LIM(sense); count++)
    261 			printf("0x%x ", sense->cmd_spec_info[count]);
    262 		printf("\n");
    263 	}
    264 #endif	/* SCSIDEBUG */
    265 	/*
    266 	 * If the device has it's own error handler, call it first.
    267 	 * If it returns a legit error value, return that, otherwise
    268 	 * it wants us to continue with normal error processing.
    269 	 */
    270 	if (sc_link->device->err_handler) {
    271 		SC_DEBUG(sc_link, SDEV_DB2,
    272 		    ("calling private err_handler()\n"));
    273 		error = (*sc_link->device->err_handler)(xs);
    274 		if (error != SCSIRET_CONTINUE)
    275 			return (error);		/* error >= 0  better ? */
    276 	}
    277 	/* otherwise use the default */
    278 	switch (sense->error_code & SSD_ERRCODE) {
    279 		/*
    280 		 * If it's code 70, use the extended stuff and
    281 		 * interpret the key
    282 		 */
    283 	case 0x71:		/* delayed error */
    284 		sc_link->sc_print_addr(sc_link);
    285 		key = sense->flags & SSD_KEY;
    286 		printf(" DEFERRED ERROR, key = 0x%x\n", key);
    287 		/* FALLTHROUGH */
    288 	case 0x70:
    289 		if ((sense->error_code & SSD_ERRCODE_VALID) != 0)
    290 			info = _4btol(sense->info);
    291 		else
    292 			info = 0;
    293 		key = sense->flags & SSD_KEY;
    294 
    295 		switch (key) {
    296 		case SKEY_NO_SENSE:
    297 		case SKEY_RECOVERED_ERROR:
    298 			if (xs->resid == xs->datalen && xs->datalen) {
    299 				/*
    300 				 * Why is this here?
    301 				 */
    302 				xs->resid = 0;	/* not short read */
    303 			}
    304 		case SKEY_EQUAL:
    305 			error = 0;
    306 			break;
    307 		case SKEY_NOT_READY:
    308 			if ((sc_link->flags & SDEV_REMOVABLE) != 0)
    309 				sc_link->flags &= ~SDEV_MEDIA_LOADED;
    310 			if ((xs->xs_control & XS_CTL_IGNORE_NOT_READY) != 0)
    311 				return (0);
    312 			if (sense->add_sense_code == 0x3A &&
    313 			    sense->add_sense_code_qual == 0x00)
    314 				error = ENODEV; /* Medium not present */
    315 			else
    316 				error = EIO;
    317 			if ((xs->xs_control & XS_CTL_SILENT) != 0)
    318 				return (error);
    319 			break;
    320 		case SKEY_ILLEGAL_REQUEST:
    321 			if ((xs->xs_control &
    322 			     XS_CTL_IGNORE_ILLEGAL_REQUEST) != 0)
    323 				return (0);
    324 			/*
    325 			 * Handle the case where a device reports
    326 			 * Logical Unit Not Supported during discovery.
    327 			 */
    328 			if ((xs->xs_control & XS_CTL_DISCOVERY) != 0 &&
    329 			    sense->add_sense_code == 0x25 &&
    330 			    sense->add_sense_code_qual == 0x00)
    331 				return (EINVAL);
    332 			if ((xs->xs_control & XS_CTL_SILENT) != 0)
    333 				return (EIO);
    334 			error = EINVAL;
    335 			break;
    336 		case SKEY_UNIT_ATTENTION:
    337 			if (sense->add_sense_code == 0x29 &&
    338 			    sense->add_sense_code_qual == 0x00)
    339 				return (ERESTART); /* device or bus reset */
    340 			if ((sc_link->flags & SDEV_REMOVABLE) != 0)
    341 				sc_link->flags &= ~SDEV_MEDIA_LOADED;
    342 			if ((xs->xs_control &
    343 			     XS_CTL_IGNORE_MEDIA_CHANGE) != 0 ||
    344 				/* XXX Should reupload any transient state. */
    345 				(sc_link->flags & SDEV_REMOVABLE) == 0)
    346 				return (ERESTART);
    347 			if ((xs->xs_control & XS_CTL_SILENT) != 0)
    348 				return (EIO);
    349 			error = EIO;
    350 			break;
    351 		case SKEY_WRITE_PROTECT:
    352 			error = EROFS;
    353 			break;
    354 		case SKEY_BLANK_CHECK:
    355 			error = 0;
    356 			break;
    357 		case SKEY_ABORTED_COMMAND:
    358 			error = ERESTART;
    359 			break;
    360 		case SKEY_VOLUME_OVERFLOW:
    361 			error = ENOSPC;
    362 			break;
    363 		default:
    364 			error = EIO;
    365 			break;
    366 		}
    367 
    368 #ifdef SCSIVERBOSE
    369 		if ((xs->xs_control & XS_CTL_SILENT) == 0)
    370 			scsipi_print_sense(xs, 0);
    371 #else
    372 		if (key) {
    373 			sc_link->sc_print_addr(sc_link);
    374 			printf("%s", error_mes[key - 1]);
    375 			if ((sense->error_code & SSD_ERRCODE_VALID) != 0) {
    376 				switch (key) {
    377 				case SKEY_NOT_READY:
    378 				case SKEY_ILLEGAL_REQUEST:
    379 				case SKEY_UNIT_ATTENTION:
    380 				case SKEY_WRITE_PROTECT:
    381 					break;
    382 				case SKEY_BLANK_CHECK:
    383 					printf(", requested size: %d (decimal)",
    384 					    info);
    385 					break;
    386 				case SKEY_ABORTED_COMMAND:
    387 					if (xs->retries)
    388 						printf(", retrying");
    389 					printf(", cmd 0x%x, info 0x%x",
    390 					    xs->cmd->opcode, info);
    391 					break;
    392 				default:
    393 					printf(", info = %d (decimal)", info);
    394 				}
    395 			}
    396 			if (sense->extra_len != 0) {
    397 				int n;
    398 				printf(", data =");
    399 				for (n = 0; n < sense->extra_len; n++)
    400 					printf(" %02x",
    401 					    sense->cmd_spec_info[n]);
    402 			}
    403 			printf("\n");
    404 		}
    405 #endif
    406 		return (error);
    407 
    408 	/*
    409 	 * Not code 70, just report it
    410 	 */
    411 	default:
    412 #if	defined(SCSIDEBUG) || defined(DEBUG)
    413 	{
    414 		static char *uc = "undecodable sense error";
    415 		int i;
    416 		u_int8_t *cptr = (u_int8_t *) sense;
    417 		sc_link->sc_print_addr(sc_link);
    418 		if (xs->cmd == &xs->cmdstore) {
    419 			printf("%s for opcode 0x%x, data=",
    420 			    uc, xs->cmdstore.opcode);
    421 		} else {
    422 			printf("%s, data=", uc);
    423 		}
    424 		for (i = 0; i < sizeof (sense); i++)
    425 			printf(" 0x%02x", *(cptr++) & 0xff);
    426 		printf("\n");
    427 	}
    428 #else
    429 		sc_link->sc_print_addr(sc_link);
    430 		printf("Sense Error Code 0x%x",
    431 			sense->error_code & SSD_ERRCODE);
    432 		if ((sense->error_code & SSD_ERRCODE_VALID) != 0) {
    433 			struct scsipi_sense_data_unextended *usense =
    434 			    (struct scsipi_sense_data_unextended *)sense;
    435 			printf(" at block no. %d (decimal)",
    436 			    _3btol(usense->block));
    437 		}
    438 		printf("\n");
    439 #endif
    440 		return (EIO);
    441 	}
    442 }
    443 
    444 /*
    445  * Find out from the device what its capacity is.
    446  */
    447 u_long
    448 scsipi_size(sc_link, flags)
    449 	struct scsipi_link *sc_link;
    450 	int flags;
    451 {
    452 	struct scsipi_read_cap_data rdcap;
    453 	struct scsipi_read_capacity scsipi_cmd;
    454 
    455 	/*
    456 	 * make up a scsipi command and ask the scsipi driver to do
    457 	 * it for you.
    458 	 */
    459 	bzero(&scsipi_cmd, sizeof(scsipi_cmd));
    460 	scsipi_cmd.opcode = READ_CAPACITY;
    461 
    462 	/*
    463 	 * If the command works, interpret the result as a 4 byte
    464 	 * number of blocks
    465 	 */
    466 	if (scsipi_command(sc_link, (struct scsipi_generic *)&scsipi_cmd,
    467 	    sizeof(scsipi_cmd), (u_char *)&rdcap, sizeof(rdcap),
    468 	    SCSIPIRETRIES, 20000, NULL, flags | XS_CTL_DATA_IN) != 0) {
    469 		sc_link->sc_print_addr(sc_link);
    470 		printf("could not get size\n");
    471 		return (0);
    472 	}
    473 
    474 	return (_4btol(rdcap.addr) + 1);
    475 }
    476 
    477 /*
    478  * Get scsipi driver to send a "are you ready?" command
    479  */
    480 int
    481 scsipi_test_unit_ready(sc_link, flags)
    482 	struct scsipi_link *sc_link;
    483 	int flags;
    484 {
    485 	struct scsipi_test_unit_ready scsipi_cmd;
    486 
    487 	/* some ATAPI drives don't support TEST_UNIT_READY. Sigh */
    488 	if (sc_link->quirks & ADEV_NOTUR)
    489 		return (0);
    490 
    491 	bzero(&scsipi_cmd, sizeof(scsipi_cmd));
    492 	scsipi_cmd.opcode = TEST_UNIT_READY;
    493 
    494 	return (scsipi_command(sc_link,
    495 	    (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd),
    496 	    0, 0, SCSIPIRETRIES, 10000, NULL, flags));
    497 }
    498 
    499 /*
    500  * Do a scsipi operation asking a device what it is
    501  * Use the scsipi_cmd routine in the switch table.
    502  * XXX actually this is only used for scsi devices, because I have the feeling
    503  * that some atapi CDROM may not implement it, althouh it marked as mandatory
    504  * in the atapi specs.
    505  */
    506 int
    507 scsipi_inquire(sc_link, inqbuf, flags)
    508 	struct scsipi_link *sc_link;
    509 	struct scsipi_inquiry_data *inqbuf;
    510 	int flags;
    511 {
    512 	struct scsipi_inquiry scsipi_cmd;
    513 
    514 	bzero(&scsipi_cmd, sizeof(scsipi_cmd));
    515 	scsipi_cmd.opcode = INQUIRY;
    516 	scsipi_cmd.length = sizeof(struct scsipi_inquiry_data);
    517 
    518 	return (scsipi_command(sc_link,
    519 	    (struct scsipi_generic *) &scsipi_cmd, sizeof(scsipi_cmd),
    520 	    (u_char *) inqbuf, sizeof(struct scsipi_inquiry_data),
    521 	    SCSIPIRETRIES, 10000, NULL, XS_CTL_DATA_IN | flags));
    522 }
    523 
    524 /*
    525  * Prevent or allow the user to remove the media
    526  */
    527 int
    528 scsipi_prevent(sc_link, type, flags)
    529 	struct scsipi_link *sc_link;
    530 	int type, flags;
    531 {
    532 	struct scsipi_prevent scsipi_cmd;
    533 
    534 	if (sc_link->quirks & ADEV_NODOORLOCK)
    535 		return (0);
    536 
    537 	bzero(&scsipi_cmd, sizeof(scsipi_cmd));
    538 	scsipi_cmd.opcode = PREVENT_ALLOW;
    539 	scsipi_cmd.how = type;
    540 	return (scsipi_command(sc_link,
    541 	    (struct scsipi_generic *) &scsipi_cmd, sizeof(scsipi_cmd),
    542 	    0, 0, SCSIPIRETRIES, 5000, NULL, flags));
    543 }
    544 
    545 /*
    546  * Get scsipi driver to send a "start up" command
    547  */
    548 int
    549 scsipi_start(sc_link, type, flags)
    550 	struct scsipi_link *sc_link;
    551 	int type, flags;
    552 {
    553 	struct scsipi_start_stop scsipi_cmd;
    554 
    555 	if (sc_link->quirks & SDEV_NOSTARTUNIT)
    556 		return 0;
    557 
    558 	bzero(&scsipi_cmd, sizeof(scsipi_cmd));
    559 	scsipi_cmd.opcode = START_STOP;
    560 	scsipi_cmd.byte2 = 0x00;
    561 	scsipi_cmd.how = type;
    562 	return (scsipi_command(sc_link,
    563 	    (struct scsipi_generic *) &scsipi_cmd, sizeof(scsipi_cmd),
    564 	    0, 0, SCSIPIRETRIES, (type & SSS_START) ? 60000 : 10000,
    565 	    NULL, flags));
    566 }
    567 
    568 /*
    569  * This routine is called by the scsipi interrupt when the transfer is
    570  * complete.
    571  */
    572 void
    573 scsipi_done(xs)
    574 	struct scsipi_xfer *xs;
    575 {
    576 	struct scsipi_link *sc_link = xs->sc_link;
    577 	struct buf *bp;
    578 	int error;
    579 
    580 	SC_DEBUG(sc_link, SDEV_DB2, ("scsipi_done\n"));
    581 #ifdef	SCSIDEBUG
    582 	if ((sc_link->flags & SDEV_DB1) != 0)
    583 		show_scsipi_cmd(xs);
    584 #endif /* SCSIDEBUG */
    585 
    586 	/*
    587 	 * If it's a user level request, bypass all usual completion
    588 	 * processing, let the user work it out..  We take
    589 	 * reponsibility for freeing the xs when the user returns.
    590 	 * (and restarting the device's queue).
    591 	 */
    592 	if ((xs->xs_control & XS_CTL_USERCMD) != 0) {
    593 		SC_DEBUG(sc_link, SDEV_DB3, ("calling user done()\n"));
    594 		scsipi_user_done(xs); /* to take a copy of the sense etc. */
    595 		SC_DEBUG(sc_link, SDEV_DB3, ("returned from user done()\n "));
    596 
    597 		/*
    598 		 * If this was an asynchronous operation (i.e. adapter
    599 		 * returned SUCCESSFULLY_QUEUED when the command was
    600 		 * submitted), we need to free the scsipi_xfer here.
    601 		 */
    602 		if (xs->xs_control & XS_CTL_ASYNC)
    603 			scsipi_free_xs(xs, XS_CTL_NOSLEEP);
    604 		SC_DEBUG(sc_link, SDEV_DB3, ("returning to adapter\n"));
    605 		return;
    606 	}
    607 
    608 	if ((xs->xs_control & XS_CTL_ASYNC) == 0) {
    609 		/*
    610 		 * if it's a normal upper level request, then ask
    611 		 * the upper level code to handle error checking
    612 		 * rather than doing it here at interrupt time
    613 		 */
    614 		wakeup(xs);
    615 		return;
    616 	}
    617 
    618 	/*
    619 	 * Go and handle errors now.
    620 	 * If it returns ERESTART then we should RETRY
    621 	 */
    622 retry:
    623 	error = sc_err1(xs, 1);
    624 	if (error == ERESTART) {
    625 		switch (scsipi_command_direct(xs)) {
    626 		case SUCCESSFULLY_QUEUED:
    627 			return;
    628 
    629 		case TRY_AGAIN_LATER:
    630 			xs->error = XS_BUSY;
    631 		case COMPLETE:
    632 			goto retry;
    633 		}
    634 	}
    635 
    636 	bp = xs->bp;
    637 	if (bp) {
    638 		if (error) {
    639 			bp->b_error = error;
    640 			bp->b_flags |= B_ERROR;
    641 			bp->b_resid = bp->b_bcount;
    642 		} else {
    643 			bp->b_error = 0;
    644 			bp->b_resid = xs->resid;
    645 		}
    646 	}
    647 	if (sc_link->device->done) {
    648 		/*
    649 		 * Tell the device the operation is actually complete.
    650 		 * No more will happen with this xfer.  This for
    651 		 * notification of the upper-level driver only; they
    652 		 * won't be returning any meaningful information to us.
    653 		 */
    654 		(*sc_link->device->done)(xs);
    655 	}
    656 	/*
    657 	 * If this was an asynchronous operation (i.e. adapter
    658 	 * returned SUCCESSFULLY_QUEUED when the command was
    659 	 * submitted), we need to free the scsipi_xfer here.
    660 	 */
    661 	if (xs->xs_control & XS_CTL_ASYNC) {
    662 		int s = splbio();
    663 		scsipi_free_xs(xs, XS_CTL_NOSLEEP);
    664 		splx(s);
    665 	}
    666 	if (bp)
    667 		biodone(bp);
    668 }
    669 
    670 int
    671 scsipi_execute_xs(xs)
    672 	struct scsipi_xfer *xs;
    673 {
    674 	int async;
    675 	int error;
    676 	int s;
    677 
    678 	xs->xs_status &= ~XS_STS_DONE;
    679 	xs->error = XS_NOERROR;
    680 	xs->resid = xs->datalen;
    681 	xs->status = 0;
    682 
    683 retry:
    684 	/*
    685 	 * Do the transfer. If we are polling we will return:
    686 	 * COMPLETE,  Was poll, and scsipi_done has been called
    687 	 * TRY_AGAIN_LATER, Adapter short resources, try again
    688 	 *
    689 	 * if under full steam (interrupts) it will return:
    690 	 * SUCCESSFULLY_QUEUED, will do a wakeup when complete
    691 	 * TRY_AGAIN_LATER, (as for polling)
    692 	 * After the wakeup, we must still check if it succeeded
    693 	 *
    694 	 * If we have a XS_CTL_ASYNC (typically because we have a buf)
    695 	 * we just return.  All the error proccessing and the buffer
    696 	 * code both expect us to return straight to them, so as soon
    697 	 * as the command is queued, return.
    698 	 */
    699 #ifdef SCSIDEBUG
    700 	if (xs->sc_link->flags & SDEV_DB3) {
    701 		printf("scsipi_exec_cmd: ");
    702 		show_scsipi_xs(xs);
    703 		printf("\n");
    704 	}
    705 #endif
    706 	async = (xs->xs_control & XS_CTL_ASYNC);
    707 	switch (scsipi_command_direct(xs)) {
    708 	case SUCCESSFULLY_QUEUED:
    709 		if (async) {
    710 			/* scsipi_done() will free the scsipi_xfer. */
    711 			return (EJUSTRETURN);
    712 		}
    713 #ifdef DIAGNOSTIC
    714 		if (xs->xs_control & XS_CTL_ASYNC)
    715 			panic("scsipi_execute_xs: ASYNC and POLL");
    716 #endif
    717 		s = splbio();
    718 		while ((xs->xs_status & XS_STS_DONE) == 0)
    719 			tsleep(xs, PRIBIO + 1, "scsipi_cmd", 0);
    720 		splx(s);
    721 	case COMPLETE:		/* Polling command completed ok */
    722 		if (xs->bp)
    723 			return (0);
    724 	doit:
    725 		SC_DEBUG(xs->sc_link, SDEV_DB3, ("back in cmd()\n"));
    726 		if ((error = sc_err1(xs, 0)) != ERESTART)
    727 			return (error);
    728 		goto retry;
    729 
    730 	case TRY_AGAIN_LATER:	/* adapter resource shortage */
    731 		xs->error = XS_BUSY;
    732 		goto doit;
    733 
    734 	default:
    735 		panic("scsipi_execute_xs: invalid return code");
    736 	}
    737 
    738 #ifdef DIAGNOSTIC
    739 	panic("scsipi_execute_xs: impossible");
    740 #endif
    741 	return (EINVAL);
    742 }
    743 
    744 int
    745 sc_err1(xs, async)
    746 	struct scsipi_xfer *xs;
    747 	int async;
    748 {
    749 	int error;
    750 
    751 	SC_DEBUG(xs->sc_link, SDEV_DB3, ("sc_err1,err = 0x%x \n", xs->error));
    752 
    753 	/*
    754 	 * If it has a buf, we might be working with
    755 	 * a request from the buffer cache or some other
    756 	 * piece of code that requires us to process
    757 	 * errors at inetrrupt time. We have probably
    758 	 * been called by scsipi_done()
    759 	 */
    760 	switch (xs->error) {
    761 	case XS_NOERROR:	/* nearly always hit this one */
    762 		error = 0;
    763 		break;
    764 
    765 	case XS_SENSE:
    766 	case XS_SHORTSENSE:
    767 		if ((error = (*xs->sc_link->scsipi_interpret_sense)(xs)) ==
    768 		    ERESTART)
    769 			goto retry;
    770 		SC_DEBUG(xs->sc_link, SDEV_DB3,
    771 		    ("scsipi_interpret_sense returned %d\n", error));
    772 		break;
    773 
    774 	case XS_BUSY:
    775 		if (xs->retries) {
    776 			if ((xs->xs_control & XS_CTL_POLL) != 0)
    777 				delay(1000000);
    778 			else if ((xs->xs_control & (XS_CTL_NOSLEEP|XS_CTL_DISCOVERY)) == 0)
    779 				tsleep(&lbolt, PRIBIO, "scbusy", 0);
    780 			else
    781 #if 0
    782 				timeout(scsipi_requeue, xs, hz);
    783 #else
    784 				goto lose;
    785 #endif
    786 		}
    787 	case XS_TIMEOUT:
    788 	retry:
    789 		if (xs->retries) {
    790 			xs->retries--;
    791 			xs->error = XS_NOERROR;
    792 			xs->xs_status &= ~XS_STS_DONE;
    793 			return (ERESTART);
    794 		}
    795 	case XS_DRIVER_STUFFUP:
    796 	lose:
    797 		error = EIO;
    798 		break;
    799 
    800 	case XS_SELTIMEOUT:
    801 		/* XXX Disable device? */
    802 		error = EIO;
    803 		break;
    804 
    805 	case XS_RESET:
    806 		if (xs->retries) {
    807 			SC_DEBUG(xs->sc_link, SDEV_DB3,
    808 			    ("restarting command destroyed by reset\n"));
    809 			goto retry;
    810 		}
    811 		error = EIO;
    812 		break;
    813 
    814 	default:
    815 		(*xs->sc_link->sc_print_addr)(xs->sc_link);
    816 		printf("unknown error category from scsipi driver\n");
    817 		error = EIO;
    818 		break;
    819 	}
    820 
    821 	return (error);
    822 }
    823 
    824 /*
    825  * Add a reference to the adapter pointed to by the provided
    826  * link, enabling the adapter if necessary.
    827  */
    828 int
    829 scsipi_adapter_addref(link)
    830 	struct scsipi_link *link;
    831 {
    832 	struct scsipi_adapter *adapter = link->adapter;
    833 	int s, error = 0;
    834 
    835 	s = splbio();
    836 	if (adapter->scsipi_refcnt++ == 0 &&
    837 	    adapter->scsipi_enable != NULL) {
    838 		error = (*adapter->scsipi_enable)(link->adapter_softc, 1);
    839 		if (error)
    840 			adapter->scsipi_refcnt--;
    841 	}
    842 	splx(s);
    843 	return (error);
    844 }
    845 
    846 /*
    847  * Delete a reference to the adapter pointed to by the provided
    848  * link, disabling the adapter if possible.
    849  */
    850 void
    851 scsipi_adapter_delref(link)
    852 	struct scsipi_link *link;
    853 {
    854 	struct scsipi_adapter *adapter = link->adapter;
    855 	int s;
    856 
    857 	s = splbio();
    858 	if (adapter->scsipi_refcnt-- == 1 &&
    859 	    adapter->scsipi_enable != NULL)
    860 		(void) (*adapter->scsipi_enable)(link->adapter_softc, 0);
    861 	splx(s);
    862 }
    863 
    864 #ifdef	SCSIDEBUG
    865 /*
    866  * Given a scsipi_xfer, dump the request, in all it's glory
    867  */
    868 void
    869 show_scsipi_xs(xs)
    870 	struct scsipi_xfer *xs;
    871 {
    872 
    873 	printf("xs(%p): ", xs);
    874 	printf("xs_control(0x%08x)", xs->xs_control);
    875 	printf("xs_status(0x%08x)", xs->xs_status);
    876 	printf("sc_link(%p)", xs->sc_link);
    877 	printf("retr(0x%x)", xs->retries);
    878 	printf("timo(0x%x)", xs->timeout);
    879 	printf("cmd(%p)", xs->cmd);
    880 	printf("len(0x%x)", xs->cmdlen);
    881 	printf("data(%p)", xs->data);
    882 	printf("len(0x%x)", xs->datalen);
    883 	printf("res(0x%x)", xs->resid);
    884 	printf("err(0x%x)", xs->error);
    885 	printf("bp(%p)", xs->bp);
    886 	show_scsipi_cmd(xs);
    887 }
    888 
    889 void
    890 show_scsipi_cmd(xs)
    891 	struct scsipi_xfer *xs;
    892 {
    893 	u_char *b = (u_char *) xs->cmd;
    894 	int i = 0;
    895 
    896 	(*xs->sc_link->sc_print_addr)(xs->sc_link);
    897 	printf("command: ");
    898 
    899 	if ((xs->xs_control & XS_CTL_RESET) == 0) {
    900 		while (i < xs->cmdlen) {
    901 			if (i)
    902 				printf(",");
    903 			printf("0x%x", b[i++]);
    904 		}
    905 		printf("-[%d bytes]\n", xs->datalen);
    906 		if (xs->datalen)
    907 			show_mem(xs->data, min(64, xs->datalen));
    908 	} else
    909 		printf("-RESET-\n");
    910 }
    911 
    912 void
    913 show_mem(address, num)
    914 	u_char *address;
    915 	int num;
    916 {
    917 	int x;
    918 
    919 	printf("------------------------------");
    920 	for (x = 0; x < num; x++) {
    921 		if ((x % 16) == 0)
    922 			printf("\n%03d: ", x);
    923 		printf("%02x ", *address++);
    924 	}
    925 	printf("\n------------------------------\n");
    926 }
    927 #endif /*SCSIDEBUG */
    928